-
Notifications
You must be signed in to change notification settings - Fork 6
/
ftpd.e
2331 lines (2005 loc) · 58.8 KB
/
ftpd.e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
** simple ftpd
** Amiga E version
*/
OPT LARGE,MODULE
CONST LISTENQ=100
CONST EINTR=4
CONST EWOULDBLOCK=35
CONST MAX_LINE=255
CONST FIONBIO=$8004667e
CONST TIMEOUT=300
ENUM CMDTYPE_LIST=1,CMDTYPE_NLST=2,CMDTYPE_MLSD=3
ENUM MODE_UPLOAD, MODE_DOWNLOAD, MODE_FULLSERVER
MODULE 'socket',
'net/netdb',
'net/in',
'net/socket',
'dos/dostags',
'dos/dosextens',
'dos/datetime',
'dos/dos',
'devices/timer',
'exec/tasks',
'exec/alerts',
'asyncio',
'libraries/asyncio',
'*stringlist',
'*axobjects',
'*axenums',
'*tooltypes',
'*miscfuncs',
'*bcd'
EXPORT OBJECT ftpData
rest:LONG
tcount:LONG
scount:LONG
hostName:PTR TO CHAR
workingPath:PTR TO CHAR
uploadPath:PTR TO CHAR
subDirPath:PTR TO CHAR
fileList:PTR TO stdlist
port:LONG
dataPorts:PTR TO LONG
restPos:LONG
sockId:LONG
aePuts:LONG
conPuts:LONG
readChar:LONG
sCheckInput:LONG
uploadFileStart:LONG
uploadFileEnd:LONG
uploadFileProgress:LONG
downloadFileStart:LONG
downloadFileEnd:LONG
downloadFileProgress:LONG
checkDownloadRatio:LONG
fileDupeCheck:LONG
ftpCheckConnection:LONG
callersLog:LONG
processMessages:LONG
startFileCheck:LONG
waitFileCheck:LONG
getSigs:LONG
ftpAuth:LONG
ftpDir:LONG
getPath:LONG
findFile:LONG
authUser[255]:ARRAY OF CHAR
transType:CHAR
confNames:PTR TO stringlist
confDirs:PTR TO stringlist
confNums:PTR TO stdlist
confULBlock:PTR TO stringlist
currentConf:LONG
setReuseAddr:INT
filenameCAPS:INT
mode:INT
ENDOBJECT
PROC errno(sb)
MOVE.L sb,A6
JSR -$A2(A6) ->errno
ENDPROC D0
PROC waitSelect(sb,s,readfds,writefds,exceptfds,timeout,signals)
MOVEM.L D1-D7/A0-A6,-(A7)
MOVE.L sb,A6
MOVE.L readfds,A0
MOVE.L writefds,A1
MOVE.L exceptfds,A2
MOVE.L timeout,A3
MOVE.L signals,D1
JSR -$7e(A6)
MOVEM.L (A7)+,D1-D7/A0-A6
ENDPROC D0
PROC ioctlSocket(sb,s,v,tags)
MOVEM.L D1-D7/A0-A6,-(A7)
MOVE.L sb,A6
MOVE.L s,D0
MOVE.L v,D1
MOVE.L tags,A0
JSR -$72(A6)
MOVEM.L (A7)+,D1-D7/A0-A6
ENDPROC D0
PROC recv(sb,s,buf,len,flags)
MOVEM.L D1-D7/A0-A6,-(A7)
MOVE.L sb,A6
MOVE.L s,D0
MOVE.L buf,A0
MOVE.L len,D1
MOVE.L flags,D2
JSR -$4E(A6) ->recv
MOVEM.L (A7)+,D1-D7/A0-A6
ENDPROC D0
PROC send(sb,s,msg,len,flags)
MOVEM.L D1-D7/A0-A6,-(A7)
MOVE.L sb,A6
MOVE.L s,D0
MOVE.L msg,A0
MOVE.L len,D1
MOVE.L flags,D2
JSR -$42(A6) ->send
MOVEM.L (A7)+,D1-D7/A0-A6
ENDPROC D0
PROC accept(sb,s,addr,addrlen)
MOVEM.L D1-D7/A0-A6,-(A7)
MOVE.L sb,A6
MOVE.L s,D0
MOVE.L addr,A0
MOVE.L addrlen,A1
JSR -$30(A6) ->Accept(s,addr,addrlen)
MOVEM.L (A7)+,D1-D7/A0-A6
ENDPROC D0
PROC releaseSocket(sb,fd,id)
MOVEM.L D1-D7/A0-A6,-(A7)
MOVE.L sb,A6
MOVE.L fd,D0
MOVE.L id,D1
JSR -$96(A6) ->ReleaseSocket(fd,id)
MOVEM.L (A7)+,D1-D7/A0-A6
ENDPROC D0
/*PROC releaseCopyOfSocket(sb,fd,id)
MOVEM.L D1-D7/A0-A6,-(A7)
MOVE.L sb,A6
MOVE.L fd,D0
MOVE.L id,D1
JSR -$9C(A6) ->ReleaseCopyOfSocket(fd,id)
MOVEM.L (A7)+,D1-D7/A0-A6
ENDPROC D0*/
PROC closeSocket(sb,s)
MOVEM.L D1-D7/A0-A6,-(A7)
MOVE.L sb,A6
MOVE.L s,D0
JSR -$78(A6) ->CloseSocket(s)
MOVEM.L (A7)+,D1-D7/A0-A6
ENDPROC D0
PROC socket(sb,domain, type, protocol)
MOVEM.L D1-D7/A0-A6,-(A7)
MOVE.L sb,A6
MOVE.L domain,D0
MOVE.L type,D1
MOVE.L protocol,D2
JSR -$1E(A6) ->Socket(domain,type,protocol)
MOVEM.L (A7)+,D1-D7/A0-A6
ENDPROC D0
PROC bind(sb,s, name, namelen)
MOVEM.L D1-D7/A0-A6,-(A7)
MOVE.L sb,A6
MOVE.L s,D0
MOVE.L name,A0
MOVE.L namelen,D1
JSR -$24(A6) ->Bind(domain,type,protocol)
MOVEM.L (A7)+,D1-D7/A0-A6
ENDPROC D0
PROC listen(sb,s, backlog)
MOVEM.L D1-D7/A0-A6,-(A7)
MOVE.L sb,A6
MOVE.L s,D0
MOVE.L backlog,D1
JSR -$2A(A6) ->Listen(s,backlog)
MOVEM.L (A7)+,D1-D7/A0-A6
ENDPROC D0
PROC getHostByName(sb,name)
MOVEM.L D1-D7/A0-A6,-(A7)
MOVE.L sb,A6
MOVE.L name,A0
JSR -$D2(A6) ->GetHostByName(name)
MOVEM.L (A7)+,D1-D7/A0-A6
ENDPROC D0
PROC obtainSocket(sb,id,domain, type, protocol)
MOVEM.L D1-D7/A0-A6,-(A7)
MOVE.L sb,A6
MOVE.L id,D0
MOVE.L domain,D1
MOVE.L type,D2
MOVE.L protocol,D3
JSR -$90(A6) ->ObtainSocket(id,domain,type,protocol)
MOVEM.L (A7)+,D1-D7/A0-A6
ENDPROC D0
PROC getSockOpt(sb,s,level,optname,optval,optlen )
MOVEM.L D1-D7/A0-A6,-(A7)
MOVE.L sb,A6
MOVE.L s,D0
MOVE.L level,D1
MOVE.L optname,D2
MOVE.L optval,A0
MOVE.L optlen,A1
JSR -$60(A6) ->getSockOpt(s,level,optname,optval,optlen)
MOVEM.L (A7)+,D1-D7/A0-A6
ENDPROC D0
PROC setSockOpt(sb,s,level,optname,optval,optlen )
MOVEM.L D1-D7/A0-A6,-(A7)
MOVE.L sb,A6
MOVE.L s,D0
MOVE.L level,D1
MOVE.L optname,D2
MOVE.L optval,A0
MOVE.L optlen,D3
JSR -$5A(A6) ->setSockOpt(s,level,optname,optval,optlen)
MOVEM.L (A7)+,D1-D7/A0-A6
ENDPROC D0
PROC uploadFileStart(ftpData:PTR TO ftpData,fn,resumepos)
DEF fs
fs:=ftpData.uploadFileStart
MOVE.L fn,-(A7)
MOVE.L resumepos,-(A7)
fs()
ADD.L #8,A7
ENDPROC
PROC checkDownloadRatio(ftpData:PTR TO ftpData,fn,flen,res:PTR TO CHAR)
DEF cdr
cdr:=ftpData.checkDownloadRatio
MOVE.L fn,-(A7)
MOVE.L flen,-(A7)
MOVE.L res,-(A7)
cdr()
ADD.L #12,A7
ENDPROC D0
PROC downloadFileStart(ftpData:PTR TO ftpData,fn,flen,pos)
DEF fs
fs:=ftpData.downloadFileStart
MOVE.L fn,-(A7)
MOVE.L flen,-(A7)
MOVE.L pos,-(A7)
fs()
ADD.L #12,A7
ENDPROC
PROC uploadFileEnd(ftpData:PTR TO ftpData,fn,result)
DEF fe
fe:=ftpData.uploadFileEnd
MOVE.L fn,-(A7)
MOVE.L result,-(A7)
fe()
ADDQ.L #8,A7
ENDPROC
PROC downloadFileEnd(ftpData:PTR TO ftpData,fn,result)
DEF fe
fe:=ftpData.downloadFileEnd
MOVE.L fn,-(A7)
MOVE.L result,-(A7)
fe()
ADDQ.L #8,A7
ENDPROC
PROC uploadFileProgress(ftpData:PTR TO ftpData,fn,pos,cps)
DEF fp
fp:=ftpData.uploadFileProgress
MOVE.L fn,-(A7)
MOVE.L pos,-(A7)
MOVE.L cps,-(A7)
fp()
ADD.L #12,A7
ENDPROC
PROC downloadFileProgress(ftpData:PTR TO ftpData,fn,pos,cps)
DEF fp
fp:=ftpData.downloadFileProgress
MOVE.L fn,-(A7)
MOVE.L pos,-(A7)
MOVE.L cps,-(A7)
fp()
ADD.L #12,A7
ENDPROC
PROC fileDupeCheck(ftpData:PTR TO ftpData,fn)
DEF fdc
fdc:=ftpData.fileDupeCheck
MOVE.L fn,-(A7)
fdc()
ADDQ.L #4,A7
ENDPROC D0
PROC getPath(ftpData:PTR TO ftpData,confNum,subDir:PTR TO CHAR,outpath)
DEF gp
gp:=ftpData.getPath
gp(confNum,subDir,outpath)
ENDPROC
PROC findFile(ftpData:PTR TO ftpData,fileName,subDirs,outpath)
DEF ff
ff:=ftpData.findFile
ff(fileName,subDirs,outpath)
ENDPROC
PROC aePuts(ftpData:PTR TO ftpData, s:PTR TO CHAR)
DEF puts
puts:=ftpData.aePuts
IF puts
MOVE.L s,-(A7)
CLR.L -(A7)
puts()
ADDQ.L #8,A7
ENDIF
ENDPROC
PROC conPuts(ftpData:PTR TO ftpData, s:PTR TO CHAR)
DEF puts
puts:=ftpData.conPuts
MOVE.L s,-(A7)
MOVEQ.L #-1,D0
MOVE.L D0,-(A7)
CLR.L -(A7)
puts()
ADD.L #12,A7
ENDPROC
PROC sCheckInput(ftpData:PTR TO ftpData)
DEF chk
chk:=ftpData.sCheckInput
ENDPROC chk()
PROC callersLog(ftpData:PTR TO ftpData,logtext)
DEF clog
clog:=ftpData.callersLog
clog(logtext,TRUE)
ENDPROC
PROC processMessages(ftpData:PTR TO ftpData)
DEF procm
procm:=ftpData.processMessages
procm()
ENDPROC
PROC getSigs(ftpData:PTR TO ftpData)
DEF gsigs
gsigs:=ftpData.getSigs
ENDPROC gsigs()
PROC readChar(ftpData:PTR TO ftpData)
DEF rdChar,r
rdChar:=ftpData.readChar
CLR.L -(A7)
CLR.L -(A7)
r:=rdChar()
ADDQ.L #8,A7
ENDPROC r
PROC doAuth(ftpData:PTR TO ftpData,userName:PTR TO CHAR,password:PTR TO CHAR)
DEF fAuth,r
fAuth:=ftpData.ftpAuth
IF fAuth<>NIL
MOVE.L userName,-(A7)
MOVE.L password,-(A7)
r:=fAuth()
ADDQ.L #8,A7
ELSE
r:=TRUE
ENDIF
ENDPROC r
PROC startFileCheck(ftpData:PTR TO ftpData,filename:PTR TO CHAR,success)
DEF startCheck
startCheck:=ftpData.startFileCheck
IF startCheck<>NIL
startCheck(filename,success)
ENDIF
ENDPROC
PROC waitFileCheck(ftpData:PTR TO ftpData,timeout)
DEF waitCheck
DEF res=0
waitCheck:=ftpData.waitFileCheck
IF waitCheck<>NIL
res:=waitCheck(timeout)
ENDIF
ENDPROC res
PROC freeFileList(fileList:PTR TO stdlist)
DEF i
DEF item:PTR TO flagFileItem
IF fileList<>NIL
FOR i:=0 TO fileList.count()-1
item:=fileList.item(i)
END item
ENDFOR
END fileList
ENDIF
ENDPROC
PROC calcCPS(pd,t,t2)
DEF cps,td
DEF bcdVal[8]:ARRAY OF CHAR
cps:=0
td:=t2-t
IF td
convertToBCD(pd,bcdVal)
mulBCD(bcdVal,50)
cps:=divBCD(bcdVal,td)
ELSE
cps:=pd
ENDIF
ENDPROC cps
PROC invalidFn(fn:PTR TO CHAR)
IF StrLen(fn)>12 THEN RETURN TRUE
IF((InStr(fn,'%')>=0) OR ((InStr(fn,'#'))>=0) OR ((InStr(fn,'?'))>=0) OR ((InStr(fn,' '))>=0) OR ((InStr(fn,'/'))>=0) OR
((InStr(fn,'('))>=0) OR ((InStr(fn,')'))>=0) OR ((InStr(fn,':'))>=0) OR ((InStr(fn,'*'))>=0)) THEN TRUE
ENDPROC FALSE
->setting SO_REUSEADDR on windows allows the same socket to be opened twice - BAD BAD BAD!
PROC testSocket(sb,ftpData:PTR TO ftpData,port)
DEF r
DEF sock1,sock2
r,sock1:=openSocket(sb,port,1,ftpData)
IF r
r,sock2:=openSocket(sb,port,1,ftpData)
IF r
ftpData.setReuseAddr:=FALSE
closeSocket(sb,sock2)
ENDIF
closeSocket(sb,sock1)
ENDIF
ENDPROC
PROC getFileName(ftpData:PTR TO ftpData, filename:PTR TO CHAR,outFilename:PTR TO CHAR)
DEF i
DEF fileList:PTR TO stdlist
DEF item:PTR TO flagFileItem
StrCopy(outFilename,'')
fileList:=ftpData.fileList
IF fileList<>NIL
FOR i:=0 TO fileList.count()-1
item:=fileList.item(i)
IF StriCmp(FilePart(item.fileName),filename) THEN StrCopy(outFilename,item.fileName)
ENDFOR
ELSEIF ftpData.findFile<>NIL
findFile(ftpData,filename,ftpData.subDirPath,outFilename)
ENDIF
ENDPROC
PROC openSocket(sb,port, reuseable,ftpData:PTR TO ftpData)
DEF server_s
DEF servaddr=0:PTR TO sockaddr_in
DEF tempStr[255]:STRING
DEF optval:PTR TO LONG,optlen:PTR TO LONG
servaddr:=NEW servaddr
IF((server_s:=socket(sb,AF_INET, SOCK_STREAM, 0)) < 0)
StringF(tempStr,'/XFTP: Error creating listening socket. (\d)\b\n',errno(sb))
aePuts(ftpData,tempStr)
END servaddr
RETURN FALSE,-1
ENDIF
IF reuseable
IF ftpData.setReuseAddr
IF setSockOpt(sb,server_s, SOL_SOCKET, SO_REUSEADDR, [1]:LONG, 4)<>0
StringF(tempStr,'/XFTP: error setting socket options SO_REUSEADDR, error=\d\b\n',errno(sb))
aePuts(ftpData,tempStr)
ENDIF
ENDIF
optval:=NEW [0,0]:LONG
optlen:=NEW [8]:LONG
IF getSockOpt(sb,server_s, SOL_SOCKET, SO_LINGER, optval,optlen)<>0
StringF(tempStr,'/XFTP: error getting socket options SO_LINGER, error=\d\b\n',errno(sb))
aePuts(ftpData,tempStr)
ENDIF
IF optlen[0]=4
optval[0]:=$10000
ELSEIF optlen[0]=8
optval[0]:=1
optval[1]:=0
ELSE
aePuts(ftpData,'/XFTP: error setting socket options SO_LINGER, bad size\b\n')
ENDIF
IF setSockOpt(sb,server_s, SOL_SOCKET, SO_LINGER, optval,optlen[0])<>0
StringF(tempStr,'/XFTP: error setting socket options SO_LINGER, error=\d\b\n',errno(sb))
aePuts(ftpData,tempStr)
ENDIF
END optval[2]
END optlen[1]
ENDIF
servaddr.sin_len:=SIZEOF sockaddr_in
servaddr.sin_family:=AF_INET
servaddr.sin_port:=port
servaddr.sin_addr:=INADDR_ANY
IF(bind(sb,server_s, servaddr, SIZEOF sockaddr_in) < 0)
->StringF(tempStr,'/XFTP: Error calling bind() for port \d, error=\d\b\n',port,errno(sb));
->aePuts(ftpData,tempStr)
closeSocket(sb,server_s)
END servaddr
RETURN FALSE,-1
ENDIF
IF(listen(sb,server_s, LISTENQ) < 0)
StringF(tempStr,'/XFTP: Error calling listen() for port \d, error=\d\b\n',port,errno(sb));
aePuts(ftpData,tempStr)
closeSocket(sb,server_s)
END servaddr
RETURN FALSE,-1
ENDIF
END servaddr
ftpData.scount:=ftpData.scount+1
ENDPROC TRUE,server_s
PROC readLine(ftpData:PTR TO ftpData,sb,sockd,vptr:PTR TO CHAR, maxlen)
DEF n, rc
DEF c[1]:STRING
DEF buffer:PTR TO CHAR
DEF tv:timeval
DEF fds,res
DEF sigs=0
DEF sigsPtr=0
DEF string[255]:STRING
buffer:=vptr
FOR n:=0 TO maxlen-1
tv.secs:=TIMEOUT
tv.micro:=0
fds:=NEW [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]:LONG
setSingleFDS(fds,sockd)
IF ftpData<>NIL THEN sigs:=getSigs(ftpData)
IF sigs<>0 THEN sigsPtr:={sigs}
res:=waitSelect(sb,sockd+1,fds,NIL,NIL,tv,sigsPtr)
IF ftpData<>NIL THEN processMessages(ftpData)
END fds[32]
CONT (res<1) AND (sigs<>0)
IF res<=0 THEN RETURN -1
rc:=recv(sb,sockd, c, 1,0)
IF ( rc = 1 )
CONT c[] = "\b"
EXIT c[] = "\n"
buffer[]++:=c[]
ELSEIF ( rc = 0 )
EXIT TRUE
ELSE
rc:=errno(sb)
CONT rc = EINTR
RETURN -1
ENDIF
ENDFOR
buffer[]:=0
ENDPROC n
PROC writeLine(sb,sockd, vptr:PTR TO CHAR, n)
send(sb,sockd, vptr, n,0)
ENDPROC n
PROC writeLineEx(sb,sockd, vptr:PTR TO CHAR)
ENDPROC writeLine(sb,sockd, vptr, StrLen(vptr))
PROC getFileDate(s: PTR TO CHAR,outdate:PTR TO datestamp)
DEF fBlock: fileinfoblock
DEF fLock
DEF res=FALSE
IF((fLock:=Lock(s,ACCESS_READ)))=NIL
RETURN res
ENDIF
IF((fBlock:=AllocDosObject(DOS_FIB,NIL)))=NIL
UnLock(fLock)
RETURN res
ENDIF
IF(Examine(fLock,fBlock))
CopyMem(fBlock.datestamp,outdate,SIZEOF datestamp)
res:=TRUE
ENDIF
UnLock(fLock)
FreeDosObject(DOS_FIB,fBlock)
ENDPROC res
PROC formatFileDate(dts: PTR TO datestamp,outDateStr)
DEF datestr[10]:STRING
DEF timestr[10]:STRING
DEF dt:datetime
CopyMem(dts,dt.stamp,SIZEOF datestamp)
dt.format:=FORMAT_DOS
dt.flags:=0
dt.strday:=0
dt.strdate:=datestr
dt.strtime:=timestr
IF DateToStr(dt)
StringF(outDateStr,'\s[3]-\s[2]-\s[2]\s[2] \s[2]:\s[2]',datestr+3,datestr,IF dt.stamp.days>=8035 THEN '20' ELSE '19',datestr+7,timestr,timestr+3)
RETURN TRUE
ENDIF
ENDPROC FALSE
PROC formatFileDate2(dts: PTR TO datestamp,outDateStr)
DEF datestr[10]:STRING
DEF timestr[10]:STRING
DEF dt:datetime
CopyMem(dts,dt.stamp,SIZEOF datestamp)
dt.format:=FORMAT_USA
dt.flags:=0
dt.strday:=0
dt.strdate:=datestr
dt.strtime:=timestr
IF DateToStr(dt)
StringF(outDateStr,'\s[2]\s[2]\s[2]\s[2]\s[2]\s[2]\s[2]',IF dt.stamp.days>=8035 THEN '20' ELSE '19',datestr+6,datestr,datestr+3,timestr,timestr+3,timestr+6)
RETURN TRUE
ENDIF
ENDPROC FALSE
PROC cmdPasv(sb,ftp_c,serverHost:PTR TO CHAR,ftpData:PTR TO ftpData)
DEF temp[255]:STRING
DEF addr: LONG
DEF hostEnt: PTR TO hostent
DEF r,data_c,data_s
DEF i,port
DEF tv:timeval
DEF fds,res
DEF sigs=0
DEF sigsPtr=0
hostEnt:=getHostByName(sb,serverHost)
addr:=Long(hostEnt.h_addr_list)
addr:=Long(addr)
ftpData.rest:=0
i:=0
r:=0
REPEAT
port:=ftpData.dataPorts[i]
r,data_s:=openSocket(sb,port,1,ftpData)
i++
UNTIL (r OR (i>=ListLen(ftpData.dataPorts)))
IF r=FALSE
writeLineEx(sb,ftp_c, '425 Can''t open data connection\b\n')
RETURN -1,-1
ENDIF
StringF(temp,'227 Entering Passive Mode (\d,\d,\d,\d,\d,\d)\b\n',Shr(addr AND $FF000000,24)AND $FF,Shr(addr AND $FF0000,16) AND $FF,Shr(addr AND $FF00,8) AND $FF,addr AND $FF,Shr(port,8) AND $FF,port AND $FF)
->WriteF(temp)
writeLineEx(sb,ftp_c, temp)
ftpData.restPos:=0
REPEAT
tv.secs:=TIMEOUT
tv.micro:=0
fds:=NEW [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]:LONG
setSingleFDS(fds,data_s)
IF ftpData<>NIL THEN sigs:=getSigs(ftpData)
IF sigs<>0 THEN sigsPtr:={sigs}
res:=waitSelect(sb,data_s+1,fds,NIL,NIL,tv,sigsPtr)
IF ftpData<>NIL THEN processMessages(ftpData)
END fds[32]
IF (res<1) AND (sigs=0)
ftpData.scount:=ftpData.scount-1
closeSocket(sb,data_s)
writeLineEx(sb,ftp_c, '425 Timed out waiting for data connection\b\n')
RETURN -1,-1
ENDIF
UNTIL res>0
IF((data_c:=accept(sb,data_s, NIL, NIL) ) < 0)
->aePuts(ftpData,'/XFTP: Error calling accept()\b\n')
ftpData.scount:=ftpData.scount-1
closeSocket(sb,data_s)
writeLineEx(sb,ftp_c, '425 Can''t open data connection\b\n')
RETURN -1,-1
ELSE
ftpData.scount:=ftpData.scount+1
/*StringF(temp,'Data connection at port \d accepted\b\n', port)
aePuts(ftpData,temp)*/
RETURN data_s,data_c
ENDIF
ENDPROC
PROC uploadDir(sb,data_c, ftpData:PTR TO ftpData, cmdType)
DEF lock
DEF f_info: PTR TO fileinfoblock
DEF tempstr[255]:STRING
DEF dirline[255]:STRING
DEF size,i
IF(StrLen(ftpData.workingPath)=0) THEN RETURN
f_info:=AllocDosObject(DOS_FIB,NIL)
IF(f_info)=NIL THEN RETURN
lock:=Lock(ftpData.workingPath,ACCESS_READ)
IF(lock)=0
FreeDosObject(DOS_FIB,f_info)
RETURN
ENDIF
IF(Examine(lock,f_info))=0
UnLock(lock)
FreeDosObject(DOS_FIB,f_info)
RETURN FALSE
ENDIF
IF(f_info.direntrytype>0)
WHILE((ExNext(lock,f_info))<>0)
size:=f_info.size
IF cmdType=CMDTYPE_MLSD
formatFileDate2(f_info.datestamp,tempstr)
StringF(dirline,'type=file;size=\d;modify=\s;perm=rw; \s\b\n',size,tempstr,f_info.filename)
ELSEIF cmdType=CMDTYPE_NLST
StrCopy(dirline,f_info.filename)
ELSE /*cmdtype = CMDTYPE_LIST as default */
formatFileDate(f_info.datestamp,tempstr)
StringF(dirline,'-rw-rw-rw- 1 root root \r\d[10] \s \s\b\n',size,tempstr,f_info.filename)
ENDIF
writeLineEx(sb,data_c, dirline)
ENDWHILE
ENDIF
UnLock(lock)
FreeDosObject(DOS_FIB,f_info)
ENDPROC TRUE
PROC downloadDir(sb,data_c, ftpData:PTR TO ftpData,cmdType)
DEF lock
DEF f_info: PTR TO fileinfoblock
DEF tempstr[255]:STRING
DEF dirline[255]:STRING
DEF fn
DEF i,size
DEF item:PTR TO flagFileItem
DEF fileList:PTR TO stdlist
fileList:=ftpData.fileList
f_info:=AllocDosObject(DOS_FIB,NIL)
IF(f_info)=NIL THEN RETURN
FOR i:=0 TO fileList.count()-1
item:=fileList.item(i)
fn:=item.fileName
lock:=Lock(fn,ACCESS_READ)
size:=0
StrCopy(tempstr,'JAN-01-0001 00:00')
IF(lock)<>0
IF(Examine(lock,f_info))<>0
size:=f_info.size
ENDIF
UnLock(lock)
ENDIF
IF cmdType=CMDTYPE_MLSD
formatFileDate2(f_info.datestamp,tempstr)
StringF(dirline,'Type=file;Size=\d;Modify=\s;Perm=r; \s\b\n',size,tempstr,f_info.filename)
ELSEIF cmdType=CMDTYPE_NLST
StrCopy(dirline,f_info.filename)
ELSE /*cmdType=CMDTYPE_LIST as default*/
formatFileDate(f_info.datestamp,tempstr)
StringF(dirline,'-r--r--r-- 1 root root \r\d[10] \s \s\b\n',size,tempstr,f_info.filename)
ENDIF
writeLineEx(sb,data_c, dirline)
ENDFOR
FreeDosObject(DOS_FIB,f_info)
ENDPROC TRUE
PROC fullDir(sb,data_c, ftpData:PTR TO ftpData,cmdType)
DEF i
DEF tempstr[255]:STRING
DEF dirline[255]:STRING
DEF filedate:datestamp
IF (ftpData.currentConf=0)
dateTimeToDateStamp(getSystemTime(),filedate)
FOR i:=0 TO ftpData.confNames.count()-1
IF cmdType=CMDTYPE_MLSD
formatFileDate2(filedate,tempstr)
StringF(dirline,'type=dir;Modify=\s;perm=rw; \r\z\d[3]-\s\b\n',tempstr,i+1,ftpData.confNames.item(i))
ELSEIF cmdType=CMDTYPE_NLST
StringF(dirline,'\r\z\d[3]-\s\b\n',ftpData.confNames.item(i))
ELSE /*cmdtype = CMDTYPE_LIST as default */
formatFileDate(filedate,tempstr)
StringF(dirline,'drw-rw-rw- 1 root root \r\d[10] \s \r\z\d[3]-\s\b\n',0,tempstr,i+1,ftpData.confNames.item(i))
ENDIF
writeLineEx(sb,data_c, dirline)
ENDFOR
RETURN TRUE
ENDIF
IF ftpData.callersLog
callersLog(ftpData,'\tDirectory Scan via ftp')
ENDIF
makeConfFileList(ftpData,cmdType,sb,data_c)
ENDPROC
PROC sendFileDetails(filename:PTR TO CHAR, fileSize, fileDate, cmdType, sb, data_c, output)
DEF filedatets:datestamp
DEF tempstr[255]:STRING
DEF dirline[255]:STRING
dateTimeToDateStamp(fileDate,filedatets)
IF fileSize=-1
IF cmdType=CMDTYPE_MLSD
formatFileDate2(filedatets,tempstr)
StringF(dirline,'Type=dir;Modify=\s;Perm=r; \s\b\n',tempstr,FilePart(filename))
ELSEIF cmdType=CMDTYPE_NLST
StrCopy(dirline,filename)
ELSE /*cmdType=CMDTYPE_LIST as default*/
formatFileDate(filedatets,tempstr)
StringF(dirline,'dr--r--r-- 1 root root \r\d[10] \s \s\b\n',0,tempstr,FilePart(filename))
ENDIF
ELSE
IF cmdType=CMDTYPE_MLSD
formatFileDate2(filedatets,tempstr)
StringF(dirline,'Type=file;Size=\d;Modify=\s;Perm=r; \s\b\n',fileSize,tempstr,FilePart(filename))
ELSEIF cmdType=CMDTYPE_NLST
StrCopy(dirline,filename)
ELSE /*cmdType=CMDTYPE_LIST as default*/
formatFileDate(filedatets,tempstr)
StringF(dirline,'-r--r--r-- 1 root root \r\d[10] \s \s\b\n',fileSize,tempstr,FilePart(filename))
ENDIF
ENDIF
IF output
IF ((EstrLen(output)+EstrLen(dirline))>StrMax(output))
writeLineEx(sb,data_c, output)
StrCopy(output,'')
ENDIF
StrAdd(output,dirline)
ELSE
writeLineEx(sb,data_c, dirline)
ENDIF
ENDPROC
PROC makeList(path:PTR TO CHAR,dirCache:PTR TO CHAR,fib:PTR TO fileinfoblock, startDate, cmdType, sb, data_c)
DEF fLock
DEF d,fh
DEF dirLine[40]:STRING
DEF tmp[10]:STRING
DEF c=FALSE
DEF mem,fs,p,cnt
DEF output
output:=String(10000)
fs:=FileLength(dirCache)
IF fs>0
/*IF (mem:=New(fs+1))
fh:=Open(dirCache,MODE_OLDFILE)
Read(fh,mem,fs)
p:=0
WHILE (p<fs)
StrCopy(dirLine,'')
IF (cnt:=InStr(mem+p,'\n'))>=0
StrCopy(dirLine,mem+p,cnt)
p:=p+cnt+1
ELSE
StrCopy(dirLine,mem+p)
p:=fs
ENDIF
IF StrLen(dirLine)>18
c:=TRUE
StrCopy(tmp,'$')
StrAdd(tmp,dirLine,8)
d:=Val(tmp)
IF d>=startDate
StrCopy(tmp,'$')
StrAdd(tmp,dirLine+9,8)
sendFileDetails(dirLine+18,Val(tmp),d,cmdType, sb, data_c, output)
ENDIF
ENDIF
ENDWHILE
Close(fh)
Dispose(mem)
ELSE*/
fh:=Open(dirCache,MODE_OLDFILE)
IF fh<>0
WHILE(Fgets(fh,dirLine,40)<>NIL)
IF StrLen(dirLine)>18
c:=TRUE
StrCopy(tmp,'$')
StrAdd(tmp,dirLine,8)
d:=Val(tmp)
IF d>=startDate
StrCopy(tmp,'$')
StrAdd(tmp,dirLine+9,8)
sendFileDetails(dirLine+18,Val(tmp),d,cmdType, sb, data_c, output)
ENDIF
ENDIF
ENDWHILE
Close(fh)
ENDIF
//ENDIF
IF (output ANDALSO (EstrLen(output)>0))
writeLineEx(sb,data_c, output)
ENDIF
IF c
DisposeLink(output)
RETURN
ENDIF
ENDIF
IF((fLock:=Lock(path,ACCESS_READ)))
IF((Examine(fLock,fib)))
IF(fib.direntrytype > 0)
WHILE(ExNext(fLock,fib))
IF StrCmp(fib.filename,'.dircache')=FALSE
d:=dateStampToDateTime(fib.datestamp)
IF d>=startDate
sendFileDetails(fib.filename,IF fib.direntrytype>0 THEN -1 ELSE fib.size,d,cmdType, sb, data_c, output)
ENDIF
ENDIF
ENDWHILE
ENDIF
ENDIF
UnLock(fLock)
ENDIF
IF (output ANDALSO (EstrLen(output)>0)) THEN writeLineEx(sb,data_c, output)
DisposeLink(output)
ENDPROC
PROC makeFtpListFromDlDirs(ftpData:PTR TO ftpData, startDate, cmdType, sb, data_c)
DEF path[255]:STRING