1 /* vi: set sw=4 ts=4: */
5 * Mini implementation of FTP to retrieve a remote file.
7 * Copyright (C) 2002 Jeff Angielski, The PTR Group <jeff@theptrgroup.com>
8 * Copyright (C) 2002 Glenn McGrath
10 * Based on wget.c by Chip Rosenthal Covad Communications
11 * <chip@laserlink.net>
13 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
15 //config:config FTPGET
16 //config: bool "ftpget"
19 //config: Retrieve a remote file via FTP.
21 //config:config FTPPUT
22 //config: bool "ftpput"
25 //config: Store a remote file via FTP.
27 //config:config FEATURE_FTPGETPUT_LONG_OPTIONS
28 //config: bool "Enable long options in ftpget/ftpput"
30 //config: depends on LONG_OPTS && (FTPGET || FTPPUT)
32 //config: Support long options for the ftpget/ftpput applet.
34 //applet:IF_FTPGET(APPLET_ODDNAME(ftpget, ftpgetput, BB_DIR_USR_BIN, BB_SUID_DROP, ftpget))
35 //applet:IF_FTPPUT(APPLET_ODDNAME(ftpput, ftpgetput, BB_DIR_USR_BIN, BB_SUID_DROP, ftpput))
37 //kbuild:lib-$(CONFIG_FTPGET) += ftpgetput.o
38 //kbuild:lib-$(CONFIG_FTPPUT) += ftpgetput.o
40 //usage:#define ftpget_trivial_usage
41 //usage: "[OPTIONS] HOST [LOCAL_FILE] REMOTE_FILE"
42 //usage:#define ftpget_full_usage "\n\n"
43 //usage: "Download a file via FTP\n"
44 //usage: IF_FEATURE_FTPGETPUT_LONG_OPTIONS(
45 //usage: "\n -c,--continue Continue previous transfer"
46 //usage: "\n -v,--verbose Verbose"
47 //usage: "\n -u,--username USER Username"
48 //usage: "\n -p,--password PASS Password"
49 //usage: "\n -P,--port NUM Port"
51 //usage: IF_NOT_FEATURE_FTPGETPUT_LONG_OPTIONS(
52 //usage: "\n -c Continue previous transfer"
53 //usage: "\n -v Verbose"
54 //usage: "\n -u USER Username"
55 //usage: "\n -p PASS Password"
56 //usage: "\n -P NUM Port"
59 //usage:#define ftpput_trivial_usage
60 //usage: "[OPTIONS] HOST [REMOTE_FILE] LOCAL_FILE"
61 //usage:#define ftpput_full_usage "\n\n"
62 //usage: "Upload a file to a FTP server\n"
63 //usage: IF_FEATURE_FTPGETPUT_LONG_OPTIONS(
64 //usage: "\n -v,--verbose Verbose"
65 //usage: "\n -u,--username USER Username"
66 //usage: "\n -p,--password PASS Password"
67 //usage: "\n -P,--port NUM Port"
69 //usage: IF_NOT_FEATURE_FTPGETPUT_LONG_OPTIONS(
70 //usage: "\n -v Verbose"
71 //usage: "\n -u USER Username"
72 //usage: "\n -p PASS Password"
73 //usage: "\n -P NUM Port number"
77 #include "common_bufsiz.h"
82 struct len_and_sockaddr *lsa;
86 char buf[4]; /* actually [BUFSZ] */
88 #define G (*(struct globals*)bb_common_bufsiz1)
89 enum { BUFSZ = COMMON_BUFSIZE - offsetof(struct globals, buf) };
90 #define user (G.user )
91 #define password (G.password )
93 #define control_stream (G.control_stream)
94 #define verbose_flag (G.verbose_flag )
95 #define do_continue (G.do_continue )
97 #define INIT_G() do { \
98 setup_common_bufsiz(); \
99 BUILD_BUG_ON(sizeof(G) > COMMON_BUFSIZE); \
103 static void ftp_die(const char *msg) NORETURN;
104 static void ftp_die(const char *msg)
106 char *cp = buf; /* buf holds peer's response */
108 /* Guard against garbage from remote server */
109 while (*cp >= ' ' && *cp < '\x7f')
112 bb_error_msg_and_die("unexpected server response%s%s: %s",
113 (msg ? " to " : ""), (msg ? msg : ""), buf);
116 static int ftpcmd(const char *s1, const char *s2)
121 bb_error_msg("cmd %s %s", s1, s2);
125 fprintf(control_stream, (s2 ? "%s %s\r\n" : "%s %s\r\n"+3),
127 fflush(control_stream);
131 strcpy(buf, "EOF"); /* for ftp_die */
132 if (fgets(buf, BUFSZ - 2, control_stream) == NULL) {
135 } while (!isdigit(buf[0]) || buf[3] != ' ');
143 static void ftp_login(void)
145 /* Connect to the command socket */
146 control_stream = fdopen(xconnect_stream(lsa), "r+");
147 if (control_stream == NULL) {
148 /* fdopen failed - extremely unlikely */
149 bb_perror_nomsg_and_die();
152 if (ftpcmd(NULL, NULL) != 220) {
156 /* Login to the server */
157 switch (ftpcmd("USER", user)) {
161 if (ftpcmd("PASS", password) != 230) {
169 ftpcmd("TYPE I", NULL);
172 static int xconnect_ftpdata(void)
178 TODO: PASV command will not work for IPv6. RFC2428 describes
179 IPv6-capable "extended PASV" - EPSV.
181 "EPSV [protocol]" asks server to bind to and listen on a data port
182 in specified protocol. Protocol is 1 for IPv4, 2 for IPv6.
183 If not specified, defaults to "same as used for control connection".
184 If server understood you, it should answer "229 <some text>(|||port|)"
185 where "|" are literal pipe chars and "port" is ASCII decimal port#.
187 There is also an IPv6-capable replacement for PORT (EPRT),
188 but we don't need that.
190 NB: PASV may still work for some servers even over IPv6.
191 For example, vsftp happily answers
192 "227 Entering Passive Mode (0,0,0,0,n,n)" and proceeds as usual.
194 TODO2: need to stop ignoring IP address in PASV response.
197 if (ftpcmd("PASV", NULL) != 227) {
201 /* Response is "NNN garbageN1,N2,N3,N4,P1,P2[)garbage]
202 * Server's IP is N1.N2.N3.N4 (we ignore it)
203 * Server's port for data connection is P1*256+P2 */
204 buf_ptr = strrchr(buf, ')');
205 if (buf_ptr) *buf_ptr = '\0';
207 buf_ptr = strrchr(buf, ',');
209 port_num = xatoul_range(buf_ptr + 1, 0, 255);
211 buf_ptr = strrchr(buf, ',');
213 port_num += xatoul_range(buf_ptr + 1, 0, 255) * 256;
215 set_nport(&lsa->u.sa, htons(port_num));
216 return xconnect_stream(lsa);
219 static int pump_data_and_QUIT(int from, int to)
222 if (bb_copyfd_eof(from, to) == -1) {
223 /* error msg is already printed by bb_copyfd_eof */
227 /* close data connection */
228 close(from); /* don't know which one is that, so we close both */
231 /* does server confirm that transfer is finished? */
232 if (ftpcmd(NULL, NULL) != 226) {
235 ftpcmd("QUIT", NULL);
241 int ftp_receive(const char *local_path, char *server_path);
244 int ftp_receive(const char *local_path, char *server_path)
250 /* connect to the data socket */
251 fd_data = xconnect_ftpdata();
253 if (ftpcmd("SIZE", server_path) != 213) {
257 if (LONE_DASH(local_path)) {
258 fd_local = STDOUT_FILENO;
264 /* lstat would be wrong here! */
265 if (stat(local_path, &sbuf) < 0) {
266 bb_perror_msg_and_die("stat");
268 if (sbuf.st_size > 0) {
269 beg_range = sbuf.st_size;
276 sprintf(buf, "REST %"OFF_FMT"u", beg_range);
277 if (ftpcmd(buf, NULL) != 350) {
282 if (ftpcmd("RETR", server_path) > 150) {
286 /* create local file _after_ we know that remote file exists */
287 if (fd_local == -1) {
288 fd_local = xopen(local_path,
289 do_continue ? (O_APPEND | O_WRONLY)
290 : (O_CREAT | O_TRUNC | O_WRONLY)
294 return pump_data_and_QUIT(fd_data, fd_local);
299 int ftp_send(const char *server_path, char *local_path);
302 int ftp_send(const char *server_path, char *local_path)
308 /* connect to the data socket */
309 fd_data = xconnect_ftpdata();
311 /* get the local file */
312 fd_local = STDIN_FILENO;
313 if (NOT_LONE_DASH(local_path))
314 fd_local = xopen(local_path, O_RDONLY);
316 response = ftpcmd("STOR", server_path);
325 return pump_data_and_QUIT(fd_local, fd_data);
329 #if ENABLE_FEATURE_FTPGETPUT_LONG_OPTIONS
330 static const char ftpgetput_longopts[] ALIGN1 =
331 "continue\0" Required_argument "c"
332 "verbose\0" No_argument "v"
333 "username\0" Required_argument "u"
334 "password\0" Required_argument "p"
335 "port\0" Required_argument "P"
339 int ftpgetput_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
340 int ftpgetput_main(int argc UNUSED_PARAM, char **argv)
342 const char *port = "ftp";
343 /* socket to ftp server */
345 #if ENABLE_FTPPUT && !ENABLE_FTPGET
346 # define ftp_action ftp_send
347 #elif ENABLE_FTPGET && !ENABLE_FTPPUT
348 # define ftp_action ftp_receive
350 int (*ftp_action)(const char *, char *) = ftp_send;
352 /* Check to see if the command is ftpget or ftput */
353 if (applet_name[3] == 'g') {
354 ftp_action = ftp_receive;
359 /* Set default values */
361 password = "busybox@";
364 * Decipher the command line
366 #if ENABLE_FEATURE_FTPGETPUT_LONG_OPTIONS
367 applet_long_options = ftpgetput_longopts;
369 opt_complementary = "-2:vv:cc"; /* must have 2 to 3 params; -v and -c count */
370 getopt32(argv, "cvu:p:P:", &user, &password, &port,
371 &verbose_flag, &do_continue);
374 /* We want to do exactly _one_ DNS lookup, since some
375 * sites (i.e. ftp.us.debian.org) use round-robin DNS
376 * and we want to connect to only one IP... */
377 lsa = xhost2sockaddr(argv[0], bb_lookup_port(port, "tcp", 21));
379 printf("Connecting to %s (%s)\n", argv[0],
380 xmalloc_sockaddr2dotted(&lsa->u.sa));
384 return ftp_action(argv[1], argv[2] ? argv[2] : argv[1]);