1 /* ftpget.c - Fetch file(s) from ftp server
2 *
3 * Copyright 2016 Rob Landley <rob@landley.net>
4 *
5 * No standard for the command, but see https://www.ietf.org/rfc/rfc959.txt
6 * TODO: local can be -
7 * TEST: -g -s (when local and remote exist) -gc, -sc
8 * zero length file
9
10 USE_FTPGET(NEWTOY(ftpget, "<2>3P:cp:u:vgslLmMdD[-gs][!gslLmMdD][!clL]", TOYFLAG_USR|TOYFLAG_BIN))
11 USE_FTPPUT(OLDTOY(ftpput, ftpget, TOYFLAG_USR|TOYFLAG_BIN))
12
13 config FTPGET
14 bool "ftpget"
15 default y
16 help
17 usage: ftpget [-cvgslLmMdD] [-P PORT] [-p PASSWORD] [-u USER] HOST [LOCAL] REMOTE
18
19 Talk to ftp server. By default get REMOTE file via passive anonymous
20 transfer, optionally saving under a LOCAL name. Can also send, list, etc.
21
22 -c Continue partial transfer
23 -p Use PORT instead of "21"
24 -P Use PASSWORD instead of "ftpget@"
25 -u Use USER instead of "anonymous"
26 -v Verbose
27
28 Ways to interact with FTP server:
29 -d Delete file
30 -D Remove directory
31 -g Get file (default)
32 -l List directory
33 -L List (filenames only)
34 -m Move file on server from LOCAL to REMOTE
35 -M mkdir
36 -s Send file
37
38 config FTPPUT
39 bool "ftpput"
40 default y
41 help
42 An ftpget that defaults to -s instead of -g
43 */
44
45 #define FOR_ftpget
46 #include "toys.h"
47
48 GLOBALS(
49 char *u, *p, *P;
50
51 int fd;
52 )
53
54 // we should get one line of data, but it may be in multiple chunks
xread2line(int fd,char * buf,int len)55 static int xread2line(int fd, char *buf, int len)
56 {
57 int i, total = 0;
58
59 len--;
60 while (total<len && (i = xread(fd, buf, len-total))) {
61 total += i;
62 if (buf[total-1] == '\n') break;
63 }
64 if (total>=len) error_exit("overflow");
65 while (total--)
66 if (buf[total]=='\r' || buf[total]=='\n') buf[total] = 0;
67 else break;
68 if (toys.optflags & FLAG_v) fprintf(stderr, "%s\n", toybuf);
69
70 return total+1;
71 }
72
ftp_line(char * cmd,char * arg,int must)73 static int ftp_line(char *cmd, char *arg, int must)
74 {
75 int rc = 0;
76
77 if (cmd) {
78 char *s = "%s %s\r\n"+3*(!arg);
79 if (toys.optflags & FLAG_v) fprintf(stderr, s, cmd, arg);
80 dprintf(TT.fd, s, cmd, arg);
81 }
82 if (must>=0) {
83 xread2line(TT.fd, toybuf, sizeof(toybuf));
84 if (!sscanf(toybuf, "%d", &rc) || (must && rc != must))
85 error_exit_raw(toybuf);
86 }
87
88 return rc;
89 }
90
ftpget_main(void)91 void ftpget_main(void)
92 {
93 struct sockaddr_in6 si6;
94 int rc, ii = 1, port = 0;
95 socklen_t sl = sizeof(si6);
96 char *s, *remote = toys.optargs[2];
97 unsigned long long lenl = 0, lenr;
98
99 if (!(toys.optflags&(FLAG_v-1)))
100 toys.optflags |= (toys.which->name[3]=='g') ? FLAG_g : FLAG_s;
101
102 if (!TT.u) TT.u = "anonymous";
103 if (!TT.P) TT.P = "ftpget@";
104 if (!TT.p) TT.p = "21";
105 if (!remote) remote = toys.optargs[1];
106
107 // connect
108 TT.fd = xconnect(xgetaddrinfo(*toys.optargs, TT.p, 0, SOCK_STREAM, 0,
109 AI_ADDRCONFIG));
110 if (getpeername(TT.fd, (void *)&si6, &sl)) perror_exit("getpeername");
111
112 // Login
113 ftp_line(0, 0, 220);
114 rc = ftp_line("USER", TT.u, 0);
115 if (rc == 331) rc = ftp_line("PASS", TT.P, 0);
116 if (rc != 230) error_exit_raw(toybuf);
117
118 if (toys.optflags & FLAG_m) {
119 if (toys.optc != 3) error_exit("-m FROM TO");
120 ftp_line("RNFR", toys.optargs[1], 350);
121 ftp_line("RNTO", toys.optargs[2], 250);
122 } else if (toys.optflags & FLAG_M) ftp_line("MKD", toys.optargs[1], 257);
123 else if (toys.optflags & FLAG_d) ftp_line("DELE", toys.optargs[1], 250);
124 else if (toys.optflags & FLAG_D) ftp_line("RMD", toys.optargs[1], 250);
125 else {
126 int get = !(toys.optflags&FLAG_s), cnt = toys.optflags&FLAG_c;
127 char *cmd;
128
129 // Only do passive binary transfers
130 ftp_line("TYPE", "I", 0);
131 rc = ftp_line("PASV", 0, 0);
132
133 // PASV means the server opens a port you connect to instead of the server
134 // dialing back to the client. (Still insane, but less so.) So need port #
135
136 // PASV output is "227 PASV ok (x,x,x,x,p1,p2)" where x,x,x,x is the IP addr
137 // (must match the server you're talking to???) and port is (256*p1)+p2
138 s = 0;
139 if (rc==227) for (s = toybuf; (s = strchr(s, ',')); s++) {
140 int p1, got = 0;
141
142 sscanf(s, ",%u,%u)%n", &p1, &port, &got);
143 if (!got) continue;
144 port += 256*p1;
145 break;
146 }
147 if (!s || port<1 || port>65535) error_exit_raw(toybuf);
148 si6.sin6_port = SWAP_BE16(port); // same field size/offset for v4 and v6
149 port = xsocket(si6.sin6_family, SOCK_STREAM, 0);
150 if (connect(port, (void *)&si6, sizeof(si6))) perror_exit("connect");
151
152 // RETR blocks until file data read from data port, so use SIZE to check
153 // if file exists before creating local copy
154 lenr = 0;
155 if (toys.optflags&(FLAG_s|FLAG_g)) {
156 if (ftp_line("SIZE", remote, 0) == 213)
157 sscanf(toybuf, "%*u %llu", &lenr);
158 else if (get) error_exit("no %s", remote);
159 }
160
161 // Open file for reading or writing
162 if (toys.optflags & (FLAG_g|FLAG_s)) {
163 if (strcmp(toys.optargs[1], "-"))
164 ii = xcreate(toys.optargs[1],
165 get ? (cnt ? O_APPEND : O_TRUNC)|O_CREAT|O_WRONLY : O_RDONLY, 0666);
166 lenl = fdlength(ii);
167 }
168 if (get) {
169 cmd = "REST";
170 if (toys.optflags&FLAG_l) cmd = "LIST";
171 if (toys.optflags&FLAG_L) cmd = "NLST";
172 if (cnt) {
173 char buf[32];
174
175 if (lenl>=lenr) goto done;
176 sprintf(buf, "%llu", lenl);
177 ftp_line("REST", buf, 350);
178 } else lenl = 0;
179
180 ftp_line(cmd, remote, -1);
181 lenl += xsendfile(port, ii);
182 ftp_line(0, 0, (toys.optflags&FLAG_g) ? 226 : 150);
183 } else if (toys.optflags & FLAG_s) {
184 cmd = "STOR";
185 if (cnt && lenr) {
186 cmd = "APPE";
187 xlseek(ii, lenl, SEEK_SET);
188 } else lenr = 0;
189 ftp_line(cmd, remote, 150);
190 lenr += xsendfile(ii, port);
191 close(port);
192 }
193 if (toys.optflags&(FLAG_g|FLAG_s))
194 if (lenl != lenr) error_exit("short %lld/%lld", lenl, lenr);
195 }
196 ftp_line("QUIT", 0, 0);
197
198 done:
199 if (CFG_TOYBOX_FREE) {
200 if (ii!=1) xclose(ii);
201 xclose(port);
202 xclose(TT.fd);
203 }
204 }
205