2 # Pass data from stdin to an FTP server
5 require './fsdump-lib.pl';
7 # Parse args, and get password
8 select(STDERR); $| = 1; select(STDOUT);
11 if ($ARGV[3] =~ /touch/) {
15 if (defined($ENV{'DUMP_PASSWORD'})) {
16 $pass = $ENV{'DUMP_PASSWORD'};
19 open(TTY, "+</dev/tty");
20 print TTY "Password: ";
26 # Read rmt protocol messages
30 if ($line =~ /^O(.*)/) {
31 # File to open specified .. connect to FTP server
35 &open_socket($host, 21, "SOCK", \$err);
36 &error_exit("FTP connection failed : $err") if ($err);
37 &ftp_command("", 2, \$err) ||
38 &error_exit("FTP prompt failed : $err");
41 @urv = &ftp_command("USER $user", [ 2, 3 ], \$err);
42 @urv || &error_exit("FTP login failed : $err");
43 if (int($urv[1]/100) == 3) {
44 &ftp_command("PASS $pass", 2, \$err) ||
45 &error_exit("FTP login failed : $err");
47 &ftp_command("TYPE I", 2, \$err) ||
48 &error_exit("FTP file type failed : $err");
50 # Work out what we are doing
52 if (($perms & 0100) || ($perms & 01000) ||
53 (($perms & 01) || ($perms & 02)) && $touched) {
57 elsif ($perms & 02000) {
66 &error_exit("Unknown permissions $perms");
70 elsif ($line =~ /^W(\d+)/) {
74 &open_ftp_file($mode);
76 #$opened || &error_exit("FTP connection not opened yet");
77 read(STDIN, $buf, $len);
78 $wrote = (print CON $buf);
79 print "A".($wrote ? $len : 0)."\n";
81 elsif ($line =~ /^R(\d+)/) {
82 # Read from to FTP server
87 $read = read(CON, $buf, $len);
93 print "E",int($!),"\n";
94 print "Read failed : $!\n";
97 elsif ($line =~ /^C/) {
98 # Close FTP connection
102 &ftp_command("", 2, \$err) ||
103 &error_exit("FTP close failed : $err");
105 &ftp_command("QUIT", 2, \$err) ||
106 &error_exit("FTP quit failed : $err");
116 print "E1\nUnknown command $line\n";
122 local $err = &html_tags_to_text(join("", @_));
123 print STDERR $err,"\n";
128 sub html_tags_to_text
131 $rv =~ s/<tt>|<\/tt>//g;
132 $rv =~ s/<b>|<\/b>//g;
133 $rv =~ s/<i>|<\/i>//g;
134 $rv =~ s/<u>|<\/u>//g;
135 $rv =~ s/<pre>|<\/pre>//g;
146 local $pasv = &ftp_command("PASV", 2, \$err);
147 $pasv || &error_exit("FTP port failed : $err");
148 $pasv =~ /\(([0-9,]+)\)/;
149 local @n = split(/,/ , $1);
150 &open_socket("$n[0].$n[1].$n[2].$n[3]", $n[4]*256 + $n[5],
152 &error_exit("FTP port failed : $err");
156 &ftp_command("RETR $file", 1, \$err) ||
157 &error_exit("FTP read failed : $err");
161 # Create new file if requested by the client, or if
162 # the touch command was specified by the caller
163 &ftp_command("STOR $file", 1, \$err) ||
164 &error_exit("FTP write failed : $err");
169 # Otherwise append to the file
170 &ftp_command("APPE $file", 1, \$err) ||
171 &error_exit("FTP write failed : $err");