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 (8 kb)"
19 //config: Retrieve a remote file via FTP.
21 //config:config FTPPUT
22 //config: bool "ftpput (7.7 kb)"
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 //applet:IF_FTPGET(APPLET_ODDNAME(ftpget, ftpgetput, BB_DIR_USR_BIN, BB_SUID_DROP, ftpget))
33 //applet:IF_FTPPUT(APPLET_ODDNAME(ftpput, ftpgetput, BB_DIR_USR_BIN, BB_SUID_DROP, ftpput))
35 //kbuild:lib-$(CONFIG_FTPGET) += ftpgetput.o
36 //kbuild:lib-$(CONFIG_FTPPUT) += ftpgetput.o
38 //usage:#define ftpget_trivial_usage
39 //usage: "[OPTIONS] HOST [LOCAL_FILE] REMOTE_FILE"
40 //usage:#define ftpget_full_usage "\n\n"
41 //usage: "Download a file via FTP\n"
42 //usage: "\n -c Continue previous transfer"
43 //usage: "\n -v Verbose"
44 //usage: "\n -u USER Username"
45 //usage: "\n -p PASS Password"
46 //usage: "\n -P NUM Port"
48 //usage:#define ftpput_trivial_usage
49 //usage: "[OPTIONS] HOST [REMOTE_FILE] LOCAL_FILE"
50 //usage:#define ftpput_full_usage "\n\n"
51 //usage: "Upload a file to a FTP server\n"
52 //usage: "\n -v Verbose"
53 //usage: "\n -u USER Username"
54 //usage: "\n -p PASS Password"
55 //usage: "\n -P NUM Port number"
58 #include "common_bufsiz.h"
63 struct len_and_sockaddr *lsa;
67 char buf[4]; /* actually [BUFSZ] */
69 #define G (*(struct globals*)bb_common_bufsiz1)
70 enum { BUFSZ = COMMON_BUFSIZE - offsetof(struct globals, buf) };
71 #define user (G.user )
72 #define password (G.password )
74 #define control_stream (G.control_stream)
75 #define verbose_flag (G.verbose_flag )
76 #define do_continue (G.do_continue )
78 #define INIT_G() do { \
79 setup_common_bufsiz(); \
80 BUILD_BUG_ON(sizeof(G) > COMMON_BUFSIZE); \
84 static void ftp_die(const char *msg) NORETURN;
85 static void ftp_die(const char *msg)
87 char *cp = buf; /* buf holds peer's response */
89 /* Guard against garbage from remote server */
90 while (*cp >= ' ' && *cp < '\x7f')
93 bb_error_msg_and_die("unexpected server response%s%s: %s",
94 (msg ? " to " : ""), (msg ? msg : ""), buf);
97 static int ftpcmd(const char *s1, const char *s2)
102 bb_error_msg("cmd %s %s", s1, s2);
106 fprintf(control_stream, (s2 ? "%s %s\r\n" : "%s %s\r\n"+3),
108 fflush(control_stream);
112 strcpy(buf, "EOF"); /* for ftp_die */
113 if (fgets(buf, BUFSZ - 2, control_stream) == NULL) {
116 } while (!isdigit(buf[0]) || buf[3] != ' ');
124 static void ftp_login(void)
126 /* Connect to the command socket */
127 control_stream = fdopen(xconnect_stream(lsa), "r+");
128 if (control_stream == NULL) {
129 /* fdopen failed - extremely unlikely */
130 bb_perror_nomsg_and_die();
133 if (ftpcmd(NULL, NULL) != 220) {
137 /* Login to the server */
138 switch (ftpcmd("USER", user)) {
142 if (ftpcmd("PASS", password) != 230) {
150 ftpcmd("TYPE I", NULL);
153 static int xconnect_ftpdata(void)
157 if (ENABLE_FEATURE_IPV6 && ftpcmd("EPSV", NULL) == 229) {
159 } else if (ftpcmd("PASV", NULL) != 227) {
162 port_num = parse_pasv_epsv(buf);
166 set_nport(&lsa->u.sa, htons(port_num));
167 return xconnect_stream(lsa);
170 static int pump_data_and_QUIT(int from, int to)
173 if (bb_copyfd_eof(from, to) == -1) {
174 /* error msg is already printed by bb_copyfd_eof */
178 /* close data connection */
179 close(from); /* don't know which one is that, so we close both */
182 /* does server confirm that transfer is finished? */
183 if (ftpcmd(NULL, NULL) != 226) {
186 ftpcmd("QUIT", NULL);
192 int ftp_receive(const char *local_path, char *server_path);
195 int ftp_receive(const char *local_path, char *server_path)
201 /* connect to the data socket */
202 fd_data = xconnect_ftpdata();
204 if (ftpcmd("SIZE", server_path) != 213) {
208 if (LONE_DASH(local_path)) {
209 fd_local = STDOUT_FILENO;
215 /* lstat would be wrong here! */
216 if (stat(local_path, &sbuf) < 0) {
217 bb_perror_msg_and_die("stat");
219 if (sbuf.st_size > 0) {
220 beg_range = sbuf.st_size;
227 sprintf(buf, "REST %"OFF_FMT"u", beg_range);
228 if (ftpcmd(buf, NULL) != 350) {
233 if (ftpcmd("RETR", server_path) > 150) {
237 /* create local file _after_ we know that remote file exists */
238 if (fd_local == -1) {
239 fd_local = xopen(local_path,
240 do_continue ? (O_APPEND | O_WRONLY)
241 : (O_CREAT | O_TRUNC | O_WRONLY)
245 return pump_data_and_QUIT(fd_data, fd_local);
250 int ftp_send(const char *server_path, char *local_path);
253 int ftp_send(const char *server_path, char *local_path)
259 /* connect to the data socket */
260 fd_data = xconnect_ftpdata();
262 /* get the local file */
263 fd_local = STDIN_FILENO;
264 if (NOT_LONE_DASH(local_path))
265 fd_local = xopen(local_path, O_RDONLY);
267 response = ftpcmd("STOR", server_path);
276 return pump_data_and_QUIT(fd_local, fd_data);
280 #if ENABLE_FEATURE_FTPGETPUT_LONG_OPTIONS
281 static const char ftpgetput_longopts[] ALIGN1 =
282 "continue\0" Required_argument "c"
283 "verbose\0" No_argument "v"
284 "username\0" Required_argument "u"
285 "password\0" Required_argument "p"
286 "port\0" Required_argument "P"
290 int ftpgetput_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
291 int ftpgetput_main(int argc UNUSED_PARAM, char **argv)
293 const char *port = "ftp";
294 /* socket to ftp server */
296 #if ENABLE_FTPPUT && !ENABLE_FTPGET
297 # define ftp_action ftp_send
298 #elif ENABLE_FTPGET && !ENABLE_FTPPUT
299 # define ftp_action ftp_receive
301 int (*ftp_action)(const char *, char *) = ftp_send;
303 /* Check to see if the command is ftpget or ftput */
304 if (applet_name[3] == 'g') {
305 ftp_action = ftp_receive;
310 /* Set default values */
312 password = "busybox@";
315 * Decipher the command line
317 /* must have 2 to 3 params; -v and -c count */
318 #define OPTSTRING "^cvu:p:P:" "\0" "-2:?3:vv:cc"
319 #if ENABLE_FEATURE_FTPGETPUT_LONG_OPTIONS
320 getopt32long(argv, OPTSTRING, ftpgetput_longopts,
322 getopt32(argv, OPTSTRING,
324 &user, &password, &port, &verbose_flag, &do_continue
328 /* We want to do exactly _one_ DNS lookup, since some
329 * sites (i.e. ftp.us.debian.org) use round-robin DNS
330 * and we want to connect to only one IP... */
331 lsa = xhost2sockaddr(argv[0], bb_lookup_port(port, "tcp", 21));
333 printf("Connecting to %s (%s)\n", argv[0],
334 xmalloc_sockaddr2dotted(&lsa->u.sa));
338 return ftp_action(argv[1], argv[2] ? argv[2] : argv[1]);