1 /* ftpget.c - Fetch file(s) from ftp server
3 * Copyright 2016 Rob Landley <rob@landley.net>
5 * No standard for the command, but see https://www.ietf.org/rfc/rfc959.txt
7 * TEST: -g -s (when local and remote exist) -gc, -sc
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))
17 usage: ftpget [-cvgslLmMdD] [-P PORT] [-p PASSWORD] [-u USER] HOST [LOCAL] REMOTE
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.
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"
28 Ways to interact with FTP server:
33 -L List (filenames only)
34 -m Move file on server from LOCAL to REMOTE
42 An ftpget that defaults to -s instead of -g
56 // we should get one line of data, but it may be in multiple chunks
57 static int xread2line(int fd, char *buf, int len)
62 while (total<len && (i = xread(fd, buf, len-total))) {
64 if (buf[total-1] == '\n') break;
66 if (total>=len) error_exit("overflow");
68 if (buf[total]=='\r' || buf[total]=='\n') buf[total] = 0;
70 if (toys.optflags & FLAG_v) fprintf(stderr, "%s\n", toybuf);
75 static int ftp_line(char *cmd, char *arg, int must)
80 char *s = "%s %s\r\n"+3*(!arg);
81 if (toys.optflags & FLAG_v) fprintf(stderr, s, cmd, arg);
82 dprintf(TT.fd, s, cmd, arg);
85 xread2line(TT.fd, toybuf, sizeof(toybuf));
86 if (!sscanf(toybuf, "%d", &rc) || (must && rc != must))
87 error_exit_raw(toybuf);
93 void ftpget_main(void)
95 struct sockaddr_in6 si6;
97 socklen_t sl = sizeof(si6);
98 char *s, *remote = toys.optargs[2];
99 unsigned long long lenl = 0, lenr;
101 if (!(toys.optflags&(FLAG_v-1)))
102 toys.optflags |= (toys.which->name[3]=='g') ? FLAG_g : FLAG_s;
104 if (!TT.user) TT.user = "anonymous";
105 if (!TT.password) TT.password = "ftpget@";
106 if (!TT.port) TT.port = "21";
107 if (!remote) remote = toys.optargs[1];
110 TT.fd = xconnect(*toys.optargs, TT.port, 0, SOCK_STREAM, 0, AI_ADDRCONFIG);
111 if (getpeername(TT.fd, (void *)&si6, &sl)) perror_exit("getpeername");
115 rc = ftp_line("USER", TT.user, 0);
116 if (rc == 331) rc = ftp_line("PASS", TT.password, 0);
117 if (rc != 230) error_exit_raw(toybuf);
119 if (toys.optflags & FLAG_m) {
120 if (toys.optc != 3) error_exit("-m FROM TO");
121 ftp_line("RNFR", toys.optargs[1], 350);
122 ftp_line("RNTO", toys.optargs[2], 250);
123 } else if (toys.optflags & FLAG_M) ftp_line("MKD", toys.optargs[1], 257);
124 else if (toys.optflags & FLAG_d) ftp_line("DELE", toys.optargs[1], 250);
125 else if (toys.optflags & FLAG_D) ftp_line("RMD", toys.optargs[1], 250);
127 int get = !(toys.optflags&FLAG_s), cnt = toys.optflags&FLAG_c;
130 // Only do passive binary transfers
131 ftp_line("TYPE", "I", 0);
132 rc = ftp_line("PASV", 0, 0);
134 // PASV means the server opens a port you connect to instead of the server
135 // dialing back to the client. (Still insane, but less so.) So need port #
137 // PASV output is "227 PASV ok (x,x,x,x,p1,p2)" where x,x,x,x is the IP addr
138 // (must match the server you're talking to???) and port is (256*p1)+p2
140 if (rc==227) for (s = toybuf; (s = strchr(s, ',')); s++) {
143 sscanf(s, ",%u,%u)%n", &p1, &port, &got);
148 if (!s || port<1 || port>65535) error_exit_raw(toybuf);
149 si6.sin6_port = SWAP_BE16(port); // same field size/offset for v4 and v6
150 port = xsocket(si6.sin6_family, SOCK_STREAM, 0);
151 if (connect(port, (void *)&si6, sizeof(si6))) perror_exit("connect");
153 // RETR blocks until file data read from data port, so use SIZE to check
154 // if file exists before creating local copy
156 if (toys.optflags&(FLAG_s|FLAG_g)) {
157 if (ftp_line("SIZE", remote, 0) == 213)
158 sscanf(toybuf, "%*u %llu", &lenr);
159 else if (get) error_exit("no %s", remote);
162 // Open file for reading or writing
163 if (toys.optflags & (FLAG_g|FLAG_s)) {
164 if (strcmp(toys.optargs[1], "-"))
165 ii = xcreate(toys.optargs[1],
166 get ? (cnt ? O_APPEND : O_TRUNC)|O_CREAT|O_WRONLY : O_RDONLY, 0666);
171 if (toys.optflags&FLAG_l) cmd = "LIST";
172 if (toys.optflags&FLAG_L) cmd = "NLST";
176 if (lenl>=lenr) goto done;
177 sprintf(buf, "%llu", lenl);
178 ftp_line("REST", buf, 350);
181 ftp_line(cmd, remote, -1);
182 lenl += xsendfile(port, ii);
183 ftp_line(0, 0, (toys.optflags&FLAG_g) ? 226 : 150);
184 } else if (toys.optflags & FLAG_s) {
188 xlseek(ii, lenl, SEEK_SET);
190 ftp_line(cmd, remote, 150);
191 lenr += xsendfile(ii, port);
194 if (toys.optflags&(FLAG_g|FLAG_s))
195 if (lenl != lenr) error_exit("short %lld/%lld", lenl, lenr);
197 ftp_line("QUIT", 0, 0);
200 if (CFG_TOYBOX_FREE) {
201 if (ii!=1) xclose(ii);