-
Notifications
You must be signed in to change notification settings - Fork 1
/
OS32-FTPd
executable file
·1905 lines (1680 loc) · 57 KB
/
OS32-FTPd
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
#!/bin/bash
## vim:set nowrap:\
## vim:set syntax=tcl:\
## FreeBSD, no symlink... ##############################################\
TCL=$(ls {/usr,/usr/local,/opt}/bin/expect* 2>/dev/null | head -1) #\
[ -n "$TCL" ] || exec echo "Cannot find tclsh" #\
exec $TCL "$0" ${1+"$@"} #\
########################################################################
#
# TODO
#
# - ABOR for Ctrl/C.
# - Binary and contiguous files?
# - Don't "die" on the client.
# - Don't leave the dimmer on.
# - Return errors to the client.
#
package require Expect
package require ftpd
package require md5
namespace eval OS32 {
namespace export CheckReadOnly
namespace export CheckNotAllowed
namespace export CheckOperatorBusy
namespace export BottomHalf
namespace export BeginData
namespace export SendData
namespace export EndData
namespace export ReplyControl
namespace export CurrentWorkingDirectory
namespace export InitialAccount
namespace export CdBlacklist
namespace export WildFile
namespace export ShowACL
namespace export HaveACE
namespace export TrafficLight
namespace export Dimmer
namespace export awkward
variable ftpcmd ""
variable sock ""
namespace eval config {
#
# "Compiled-in" defaults:.
#
# - No VT100 colours on the console.
# - No VT100 colours in error messages to the client.
# - Loopback only, no external users.
# - High numbered port, no root required.
# - "Usernames" are the numeric OS/32 accounts, e.g. 25.
# - MTM takes care of password checking.
# - Assuming "simh kit" distribution.
#
array set vt100 { red ""
green ""
on "\x1b\[2m"
off "\x1b\[0m"
normal "\x1b\[0m" }
variable host "localhost"
variable port 2121
array set access {}
array set passwords {}
variable allowip 127.0.0.1
variable accountlimit 1023
variable cdblacklist 255
variable welcome "OS/32 FTP server"
variable ostype "OS/32 Version 8.1"
variable contact "$tcl_platform(user)@[info hostname]"
variable simulator "./id32"
variable timeout 10
variable defaultvolume SYS:
variable debugconfig true
variable numericlogins true
variable autostart true
#
# Some of these will be overridden by ftpd.config. The user can
# further override them with their own shadow.config, e.g. to log
# in using their everyday username.
#
}
;######################################################################
#
# Unsupported commands
#
rename ::ftpd::Fs {}
proc ::ftpd::command::MKD { sock prm } {
set ftpcmd MKD
CheckNotAllowed $sock $ftpcmd
CheckReadOnly $sock $ftpcmd
ReplyControl $sock 500 "Make directory not supported by OS/32"
}
proc ::ftpd::command::XMKD { sock prm } {
set ftpcmd XMKD
CheckNotAllowed $sock $ftpcmd
CheckReadOnly $sock $ftpcmd
ReplyControl $sock 500 "Make directory not supported by OS/32"
}
proc ::ftpd::command::RMD { sock prm } {
set ftpcmd RMD
CheckNotAllowed $sock $ftpcmd
CheckReadOnly $sock $ftpcmd
ReplyControl $sock 500 "Remove directory not supported by OS/32"
}
proc ::ftpd::command::XRMD { sock prm } {
set ftpcmd XRMD
CheckNotAllowed $sock $ftpcmd
CheckReadOnly $sock $ftpcmd
ReplyControl $sock 500 "Remove directory not supported by OS/32"
}
proc ::ftpd::command::SIZE { sock prm } {
set ftpcmd SIZE
CheckNotAllowed $sock $ftpcmd
ReplyControl $sock 500 "File size not supported (record oriented filesystem)"
}
;######################################################################
#
# Possible TODOs, but rarely used.
#
proc ::ftpd::command::MDTM {sock prm} {
set ftpcmd MDTM
CheckNotAllowed $sock $ftpcmd
ReplyControl $sock 500 "Rarely used"
}
proc ::ftpd::command::APPE {sock prm} {
set ftpcmd STOU
CheckNotAllowed $sock $ftpcmd
CheckReadOnly $sock $ftpcmd
ReplyControl $sock 500 "Rarely used"
}
proc ::ftpd::command::STOU {sock prm} {
set ftpcmd STOU
CheckNotAllowed $sock $ftpcmd
CheckReadOnly $sock $ftpcmd
ReplyControl $sock 500 "Rarely used"
}
;######################################################################
#
# CWD -- Change working directory
#
# Format is VOLUME:/ACCOUNT. VOLUME starts with a letter, followed by
# a maximum of three letters or digits. ACCOUNT is an unsigned 16 bit
# integer (i.e. less than 65636).
#
# If the request includes a volume name, we schedule a BottomHalf to
# run a CHECKVOL script, with awk-like pattern matching to determine
# success or failure. The final response is returned with the
# ReplyControl proc.
#
namespace eval CWD {
upvar #0 [namespace parent]::sock sock
proc ::ftpd::command::CWD { sock dir } {
::OS32::CWD::TopHalf $sock $dir CWD
}
proc TopHalf { sock dir ftpcmd } {
CheckNotAllowed $sock $ftpcmd
upvar #0 ::ftpd::$sock data
# Handle immediate errors
set dir [string toupper $dir]
if { ![regexp {^([A-Z][A-Z0-9]{0,3}:?)?/?([0-9]+)?$} $dir -> vol act] } {
ReplyControl $sock 550 "Not a directory (must be NUMBER, VOLUME, or VOLUME:/NUMBER)"
return
}
if { $act != "" } {
if { $act > 65535 } {
ReplyControl $sock 550 "Not a directory, account must be less than 65536"
return
}
if { [CdBlacklist $act] } {
ReplyControl $sock 550 "Access denied, cd $act is not allowed"
return
}
if { ![HaveACE $sock $act] && ![HaveACE $sock *] } {
ReplyControl $sock 550 "Access denied, your ACL is [ShowACL $sock]"
return
}
}
# Immediate success
if { $vol == "" } {
set data(cwd) $act
ReplyControl $sock 250 "Directory is now [CurrentWorkingDirectory $sock]"
return
}
# Or bottom half if we have to check the volume exists
CheckOperatorBusy $sock
set data(rvol) $vol
if { $act != "" } { set data(ract) $act }
BottomHalf $sock $ftpcmd "FTP CHECKVOL,[lindex [split $vol :] 0]"
}
proc LinePatternMatch { line } {
awkward $line {
BEGIN {
variable sock
variable dimmeris
upvar #0 ::ftpd::$sock data
}
"^\\*=== BEGIN CHECKVOL (.*) ===" {
Dimmer on $0
}
"^\\*?(....-ERR.*)" {
if { $dimmeris == "off" } {
ReplyControl $sock 450 "Command line error, try again"
} else {
set data(error) [list 550 $1]
}
}
"^\\*VOLUME (.*) OK" {
}
"^\\*VOLUME (.*) NOT FOUND" {
set data(error) [list 550 "No such volume, $1, or disk not marked on"]
}
"^\\*=== END CHECKVOL (.*) ===" {
Dimmer off $0
if { [info exists data(error)] } {
if { [info exists data(ract)] } {
unset data(ract)
}
foreach { n mess } $data(error) break
ReplyControl $sock $n $mess
unset data(rvol)
unset data(error)
} else {
if { [info exists data(ract)] } {
set data(cwd) $data(ract)
unset data(ract)
}
set data(vol) $data(rvol)
if { ![string match *: $data(vol)] } { append data(vol) : }
unset data(rvol)
ReplyControl $sock 250 "Directory is now [CurrentWorkingDirectory $sock]"
}
}
}
}
}
;######################################################################
#
# CDUP -- Go up one directory level
#
# OS/32 directories cannot be nested, so we'll return to the initial
# or default directory instead.
#
namespace eval CDUP {
proc ::ftpd::command::CDUP { sock list } {
CheckNotAllowed $sock CDUP
::ftpd::command::CWD $sock [InitialAccount $sock]
# mention CheckNotAllowed to match ::ftpd::command above
}
}
;######################################################################
#
# DELE -- Delete file
#
# Delete a file, again through a CSS wrapper.
#
namespace eval DELE {
# Bring parent variables into scope
upvar #0 [namespace parent]::ftpcmd ftpcmd
upvar #0 [namespace parent]::sock sock
proc ::ftpd::command::DELE { sock fnam } {
::OS32::DELE::TopHalf $sock $fnam DELE
}
proc TopHalf { sock fnam ftpcmd } {
CheckReadOnly $sock $ftpcmd
CheckNotAllowed $sock $ftpcmd
CheckOperatorBusy $sock
set fnam [WildFile $sock [string toupper $fnam] $ftpcmd]
upvar #0 ::ftpd::$sock data
set data(fnam) $fnam
BottomHalf $sock $ftpcmd "FTP DELETE,$fnam"
}
proc LinePatternMatch { line } {
awkward $line {
BEGIN {
variable ftpcmd
variable sock
variable dimmeris
upvar #0 ::ftpd::$sock data
}
"^\\*=== BEGIN DELETE (.*) ===" {
Dimmer on $0
}
"^\\*(DELE-ERR TYPE=PROT)" {
set data(error) [list 550 "Delete error, file is protected"]
}
"^\\*(DELE-ERR TYPE=NAME)" {
set data(error) [list 550 "File not found, $data(fnam)"]
}
"^\\*(DELE-ERR.*)" {
set data(error) [list 550 "Delete error: $1"]
}
"^\\*?(....-ERR.*)" {
if { $dimmeris == "off" } {
ReplyControl $sock 450 "Command line error, try again"
} else {
set data(error) [list 550 $1]
}
}
"^\\*=== END DELETE (.*) ===" {
Dimmer off $0
if { [info exists data(error)] } {
foreach { n mess } $data(error) break
ReplyControl $sock $n $mess
unset data(error)
} else {
ReplyControl $sock 250 "Delete $data(fnam) successful"
}
unset data(fnam)
}
}
}
}
;######################################################################
#
# LIST -- List files
#
# Invoke DISPLAY FILES through a CSS wrapper. We allow UNIX and OS/32
# wildcards. Asterisks are converted to dashes, question marks to
# asterisks.
#
# This command uses the passive mode socket, calling BeginData and
# EndData to transmit the directory list, or ReplyControl on error.
#
namespace eval LIST {
# Bring parent variables into scope
upvar #0 [namespace parent]::ftpcmd ftpcmd
upvar #0 [namespace parent]::sock sock
proc ::ftpd::command::LIST { sock fnam } {
::OS32::LIST::TopHalf $sock $fnam LIST
}
proc TopHalf { sock fnam ftpcmd } {
CheckNotAllowed $sock $ftpcmd
CheckOperatorBusy $sock
set fnam [WildFile $sock [string toupper $fnam] $ftpcmd]
upvar #0 ::ftpd::$sock data
set data(fnam) $fnam
BottomHalf $sock $ftpcmd "FTP DIR,$fnam"
}
proc LinePatternMatch { line } {
awkward $line {
BEGIN {
variable ftpcmd
variable sock
variable dimmeris
upvar #0 ::ftpd::$sock data
}
"^\\*=== BEGIN DIR .*? ===" {
Dimmer on $0
}
"^\\*FILE.S. NOT FOUND ON .*" {
set data(error) [list 550 "File(s) not found, $data(fnam)"]
}
"^\\*?(....-ERR.*)" {
if { $dimmeris == "off" } {
ReplyControl $sock 450 "Command line error, try again"
} else {
set data(error) [list 550 $1]
}
}
"^\\*(VOLUME= .*)" {
BeginData $sock 150 "Transmitting DIR $data(fnam)"
if { $ftpcmd == "LIST" } {
SendData $sock "$1"
}
}
"^\\* (FILENAME...........+)" {
if { $ftpcmd == "LIST" } {
SendData $sock " $1"
}
}
"^\\* (........).(...)/(.....) (.. .+)" {
if { $ftpcmd == "LIST" } {
SendData $sock " $1.$2/$3 $4"
}
if { $ftpcmd == "NLST" } {
set nam [string trim $1]
set ext [string trim $2]
SendData $sock [string tolower "$nam.$ext"]
}
}
"^\\*=== END DIR .*? ===" {
Dimmer off $0
if { [info exists data(error)] } {
foreach { n mess } $data(error) break
ReplyControl $sock $n $mess
unset data(error)
} else {
EndData $sock 226 "DIR $data(fnam) received"
}
unset data(fnam)
}
}
}
}
;######################################################################
#
# NLST -- List file names
#
# This is a variation of the LIST command that only returns file
# names. It is is typically invoked by the FTP client's mget command
# to resolve remote wildcards.
#
namespace eval NLST {
proc ::ftpd::command::NLST { sock fnam } {
set ftpcmd NLST
CheckNotAllowed $sock $ftpcmd
::OS32::LIST::TopHalf $sock $fnam $ftpcmd
}
proc LinePatternMatch { line } {
::OS32::LIST::LinePatternMatch $line
}
}
;######################################################################
#
# PASS -- Check password
#
# There are three options:
#
# - If the username is listed in the passwords array, do an MD5 check.
# - If the username is numeric, do a straight MTM signon.
# - If the username is not numeric, do an account lookup, then go MTM.
#
namespace eval PASS {
proc ::ftpd::command::PASS { sock pass } {
upvar #0 ::ftpd::$sock data
upvar #0 ::OS32::mtmport mtmport
upvar #0 ::OS32::config::numericlogins numericlogins
upvar #0 ::OS32::config::access access
upvar #0 ::OS32::config::passwords passwords
set user $data(user)
set data(pass) $pass
set data(acl) -vt
#
# There's no particular reason we couldn't let new clients in
# even though the operator is busy (we're not going to be using
# *send* on the console and MTM auth goes through a telnet
# session). But it makes sense to put up the "do not disturb"
# sign so we're not messing up the operator's application
# prompts.
#
CheckOperatorBusy $sock
#
# Numeric logins can be disabled because they are too easily
# enumerated.
#
if { [string is integer $user] && !$numericlogins } {
TrafficLight red "FTP USER $user FAILED LOGIN (numeric logins disabled)"
ReplyControl $sock 530 "Access denied"
return
}
#
# Named users must be listed in the access array so we can
# assign them to numeric accounts. However, we DO NOT want to
# tell the client that their username is invalid -- that's bad
# practice.
#
if { ![info exists access($user)] && ![string is integer $user] } {
TrafficLight red "FTP USER $user FAILED LOGIN (not in config::access)"
ReplyControl $sock 530 "Access denied"
return
}
#
# On the other hand, you might have a need to explicitly block
# some users. In that case it makes sense to tell them off.
#
if { [info exists access($user)] && ![llength [set access($user)]] } {
TrafficLight red "FTP USER $user FAILED LOGIN (empty acl)"
ReplyControl $sock 530 "User $user is denied ftp access"
return
}
#
# Set up defaults. Named users start in the first account that
# is listed in their acl. Numeric users will be confined to the
# account they sign on with. But don't let them default their
# way past the cd blacklist.
#
set data(vol) $::OS32::config::defaultvolume
set data(cwd) -1
if { [string is integer $user] } {
set data(acl) "$user -vt"
set data(cwd) $user
if { [CdBlacklist $data(cwd)] } {
TrafficLight red "FTP USER $user FAILED LOGIN (numeric login on cd blacklist)"
ReplyControl $sock 530 "Access denied"
return
}
} else {
set data(acl) [set access($user)]
set data(cwd) [InitialAccount $sock]
if { [CdBlacklist $data(cwd)] } {
TrafficLight red "FTP USER $user FAILED LOGIN (initial account on cd blacklist)"
ReplyControl $sock 530 "Access denied"
return
}
}
if { $data(cwd) < 0 } {
TrafficLight red "FTP USER $user FAILED LOGIN (no account)"
ReplyControl $sock 530 "Access denied"
return
}
#
# If the user/account is listed in passwords, compare md5
# hashes. Note that we salt with the username to prevent hash
# collisions, and we also terminate with a newline because the
# echo command pipes one in by default.
#
# $ echo "myid/my password" | md5sum
#
if { [info exists passwords($user)] } {
set md5 [::OS32::PasswordHash $user $pass]
if { [array names passwords -exact $user] == $user &&
[set passwords($user)] == $md5 } {
TrafficLight green "FTP USER $user LOGGED IN USING MD5"
ReplyControl $sock 230 "Password accepted, directory is [CurrentWorkingDirectory $sock]"
set data(access) 1
return
} else {
TrafficLight red "FTP USER $user FAILED LOGIN USING MD5"
ReplyControl $sock 530 "Access denied"
return
}
}
#
# No password? Then we'll use MTM to authenticate. This shells
# out to a separate expect script.
#
if { $mtmport > 0 } {
if { [string is integer $user] } {
set acct $user
} else {
set acct $data(cwd)
}
set erno [::OS32::PASS::AuthenticateMTM $acct $pass ermes]
if { $erno == 0 } {
TrafficLight green "FTP USER $user LOGGED IN USING MTM"
ReplyControl $sock 230 "MTM login successful, directory is [CurrentWorkingDirectory $sock]"
set data(access) 1
return
} else {
TrafficLight red "FTP USER $user FAILED LOGIN USING MTM ($ermes)"
ReplyControl $sock 530 "Access denied"
return
}
}
#
# Now what?
#
ReplyControl $sock 530 "Access denied"
TrafficLight red "FTP USER $user FAILED LOGIN (numeric login, no MTM, out of options)"
return
}
proc AuthenticateMTM { acct pass errvar } {
upvar 1 $errvar ermes
upvar #0 ::OS32::config::accountlimit accountlimit
# MTM accounts must be <= 255 by default, but the limit can be
# increased with the ACTUTY ACCOUNTS command. Account 255 is
# forbidden, regardless of the limit, as it owns owns the MTM
# Authorized User File. Account 0 is permitted but definitely
# not recommended for FTP use.
if { $acct < 0 || $acct > $accountlimit || $acct == 255 } {
set ermes "out of range account"
return -1
}
# According to the EOU help on PASSWORD: "All alphabetic,
# numeric and special characters, except blanks, commas, or
# semi-colons are allowed." I'm also going to reject control
# characters and nasty shell escapes out of hand.
set badchar "\x00-\x20\x7f-\xff"
append badchar {,;\\|$^`\[\]\{\}\"}
if { [regexp "\[$badchar\]" $pass] } {
set ermes "bad characters in password"
return -1
}
set exp [info nameofexecutable]
catch {
# Yes, password could be visible to ps. That's unfortunate...
upvar #0 ::OS32::mtmport mtmport
set out [exec $exp mtmcheckpass.tcl $mtmport $acct $pass]
} err
if { [regexp {\*\*\* FAIL ([0-9]+) \((.*?)\) \*\*\*} $err -> n reas] } {
set ermes [string totitle $reas]
return $n
} elseif { ![regexp {\*\*\* SUCCESS \*\*\*} $err] } {
puts stderr ""
puts stderr "=========================================================================="
puts stderr " FIXME -- MTM password check returned success, but output says otherwise"
puts stderr $err
puts stderr "=========================================================================="
puts stderr ""
return -1
} else {
set ermes ""
return 0
}
}
}
;######################################################################
#
# PWD -- Print working directory
#
# Returns the current "working directory", i.e. volume and account,
# to the client. We're overriding the built-in version because we're
# not doing it UNIX style.
#
namespace eval PWD {
proc ::ftpd::command::PWD { sock argument } {
CheckNotAllowed $sock PWD
upvar #0 ::ftpd::$sock data
set cwd [CurrentWorkingDirectory $sock]
ReplyControl $sock 250 "Current directory is $cwd"
}
}
proc CurrentWorkingDirectory { sock } {
upvar #0 ::ftpd::$sock data
set vol $data(vol)
set cwd $data(cwd)
set acl [ShowACL $sock]
return "$vol/$cwd, ACL=$acl"
}
;######################################################################
#
# RETR -- Retrieve file
#
# TYPE the file, and capture the meanful bits. Because OS/32 has no
# built-in TYPE command, we are using a CSS to run COPY32 and
# copy the file to the console. It also wraps the output in
# handy ===BEGIN===/===END=== markers.
#
namespace eval RETR {
upvar #0 [namespace parent]::ftpcmd ftpcmd
upvar #0 [namespace parent]::sock sock
proc ::ftpd::command::RETR { sock fnam } {
::OS32::RETR::TopHalf $sock $fnam RETR
}
proc TopHalf { sock fnam ftpcmd } {
CheckNotAllowed $sock $ftpcmd
CheckOperatorBusy $sock
set fnam [WildFile $sock [string toupper $fnam] $ftpcmd]
upvar #0 ::ftpd::$sock data
set data(fnam) $fnam
BottomHalf $sock RETR "FTP COPYOUT,$fnam"
}
proc LinePatternMatch { line } {
awkward $line {
BEGIN {
variable ftpcmd
variable sock
variable dimmeris
upvar #0 ::ftpd::$sock data
}
"^\\*=== BEGIN COPYOUT .*? ===" {
Dimmer on $0
BeginData $sock 150 "Transmitting $data(fnam)"
set data(nrec) 0
set data(nchar) 0
}
"^\\*FILE NOT FOUND" {
set data(error) [list 550 "File not found, $data(fnam)"]
}
"^FILE STUCK\$" {
Dimmer off $0
if { ![info exists data(nrec)] } {
set data(nrec) 0
set data(nchar) 0
}
ReplyControl $sock 551 "Whoops, $data(fnam) got stuck at $data(nchar) characters ($data(nrec) records)"
unset data(nrec)
unset data(nchar)
}
"^\\*?(....-ERR.*)" {
if { $dimmeris == "off" } {
ReplyControl $sock 450 "Command line error, try again"
} else {
set data(error) [list 550 $1]
}
}
"^\\*.*\\.BG:(PERKIN-ELMER OS/32 COPY)" {
# ignore
}
"^\\*.*\\.BG:END OF TASK" {
# ignore
}
"^\\*.*\\.BG:(.*)" {
SendData $sock "$1"
incr data(nrec)
incr data(nchar) [string length "$1"]
incr data(nchar) 2 ;# CRLF
}
"=== END COPYOUT .*? ===" {
Dimmer off $0
if { ![info exists data(nrec)] } {
set data(nrec) 0
set data(nchar) 0
}
if { [info exists data(error)] } {
foreach { n mess } $data(error) break
ReplyControl $sock $n $mess
unset data(error)
} else {
EndData $sock 226 "$data(fnam) downloaded, $data(nchar) characters, $data(nrec) records"
}
unset data(nrec)
unset data(nchar)
}
}
}
}
;######################################################################
#
# RNFR/RNTO -- Rename file
#
namespace eval RNFR {
upvar #0 [namespace parent]::ftpcmd ftpcmd
upvar #0 [namespace parent]::sock sock
upvar #0 [namespace parent]::osline osline
proc ::ftpd::command::RNFR { sock fnam } {
::OS32::RNFR::TopHalf $sock $fnam RNFR
}
proc TopHalf { sock fnam ftpcmd } {
CheckReadOnly $sock $ftpcmd
CheckNotAllowed $sock $ftpcmd
upvar #0 ::ftpd::$sock data
set fnam [WildFile $sock [string toupper $fnam] $ftpcmd]
set data(fnam1) $fnam
ReplyControl $sock 350 "RNFR OK"
}
}
namespace eval RNTO {
upvar #0 [namespace parent]::ftpcmd ftpcmd
upvar #0 [namespace parent]::sock sock
upvar #0 [namespace parent]::osline osline
proc ::ftpd::command::RNTO { sock fnam } {
::OS32::RNTO::TopHalf $sock $fnam RNTO
}
proc TopHalf { sock fnam ftpcmd } {
CheckReadOnly $sock $ftpcmd
CheckNotAllowed $sock $ftpcmd
CheckOperatorBusy $sock
upvar #0 ::ftpd::$sock data
if { ![info exists $data(fnam1)] } {
ReplyControl $sock 503 "Bad command sequence, missing RNFR"
return
}
set fnam [WildFile $sock [string toupper $fnam] $ftpcmd]
set data(fnam2) $fnam
BottomHalf $sock $ftpcmd "FTP RENAME,$data(fnam1),$data(fnam2)"
}
proc LinePatternMatch { line } {
awkward $line {
BEGIN {
variable ftpcmd
variable sock
variable dimmeris
upvar #0 ::ftpd::$sock data
}
"^\\*=== BEGIN RENAME (.*) (.*) ===" {
Dimmer on $0
}
"^\\*(ASGN-ERR TYPE=NAME.*)" {
set data(error) [list 550 "File not found, $data(fnam1)"]
}
"^\\*(RENM-ERR TYPE=NAME.*)" {
set data(error) [list 550 "Destination $data(fnam2) already exists, $data(fnam1) not renamed"]
}
"^\\*?(....-ERR.*)" {
if { $dimmeris == "off" } {
ReplyControl $sock 450 "Command line error, try again"
} else {
set data(error) [list 550 $1]
}
}
"^\\*(FILE RENAMED)" {
# ignore
}
"^\\*=== END RENAME (.*) (.*) ===" {
Dimmer off $0
if { [info exists data(error)] } {
ReplyControl $sock $data(error)
unset data(error)
} else {
ReplyControl $sock 250 "$data(fnam1) renamed $data(fnam2)"
}
unset data(fnam1)
unset data(fnam2)
}
}
}
}
;######################################################################
#
# STOR -- Store file
#
# Invoke EDIT/32 through a CSS that allocates and renames the
# temporary files, and cleans up after itself. This one is
# complicated by the fact that we do not know the record length in
# the top half, and we cannot receive data while in that top half
# socket event.
#
# This is also where we introduce a proc called PromptPatternMatch,
# which checks for prompts, which we get before we receive a carriage
# return that can trigger LinePatternMatch.
#
namespace eval STOR {
upvar #0 [namespace parent]::ftpcmd ftpcmd
upvar #0 [namespace parent]::sock sock
upvar #0 [namespace parent]::osline osline
proc ::ftpd::command::STOR { sock fnam } {
::OS32::STOR::TopHalf $sock $fnam STOR
}
proc TopHalf { sock fnam ftpcmd } {
CheckReadOnly $sock $ftpcmd
CheckNotAllowed $sock $ftpcmd
CheckOperatorBusy $sock
upvar #0 ::ftpd::$sock data
set fnam [WildFile $sock [string toupper $fnam] $ftpcmd]
set data(fnam) $fnam
BottomHalf $sock $ftpcmd "FTP $ftpcmd,$fnam"
}
proc LinePatternMatch { line } {
awkward $line {
BEGIN {
variable ftpcmd
variable sock
variable dimmeris
upvar #0 ::ftpd::$sock data
}
# Now we're in the bottom half, we can read all the data,
# and work out record length.
"^\\*STOR (EXISTS|NEWFILE)" {
# Check additional permission to *replace* the file
if { $1 == "EXISTS" } {
if { [HaveACE $sock RO] && ![HaveACE $sock $ftpcmd+] } {
ReplyControl $sock 550 "Permission denied (lack permission to replace existing file)"
return
}
}
Dimmer on $0
BeginData $sock 150 "Transmitting $data(fnam)"
set data(storme) [ReceiveData $sock]
set data(nrec) [llength $data(storme)]
set data(reclen) 80
foreach line $data(storme) {
set len [string length $line]
if { $len > $data(reclen) } { set data(reclen) $len }
}
if { [expr $data(reclen)%4] > 0 } {
incr data(reclen) [expr 4-$data(reclen)%4]
}
set data(nchar) 0
set data(ccc) 0
# Now start the copyin process
send -- "FTP COPYIN,$data(fnam),$data(reclen)\r"
}
"^\\*=== BEGIN COPYIN .*? ===" {
}
"^FILE STUCK\$" {
Dimmer off $0
ReplyControl $sock 551 "Whoops, $data(fnam) got stuck at record $data(ccc) of $data(nrec)"
}
"^\\*(DELE-ERR TYPE=PROT)" {
set data(error) [list 550 "Cannot upload file because it is write protected"]
}
"^\\*?(....-ERR.*)" {
if { $dimmeris == "off" } {
ReplyControl $sock 450 "Command line error, try again"
} else {
set data(error) [list 550 $1]
}
}
"^\\*=== END COPYIN .*? ===" {
Dimmer off $0
if { [info exists data(error)] } {
foreach { n mess } $data(error) break
ReplyControl $sock $n $mess
unset data(error)
} else {
EndData $sock 226 "$data(fnam) uploaded, $data(nchar) characters, $data(ccc) records, $data(reclen) chars reclen"
}
unset data(reclen)
unset data(nrec)
unset data(nchar)
unset data(ccc)
}
}
}
proc PromptPatternMatch { line } {
awkward $line {
BEGIN {
variable sock
upvar #0 ::ftpd::$sock data