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.
16 //usage:#define ftpget_trivial_usage
17 //usage: "[OPTIONS] HOST [LOCAL_FILE] REMOTE_FILE"
18 //usage:#define ftpget_full_usage "\n\n"
19 //usage: "Retrieve a remote file via FTP\n"
21 //usage: IF_FEATURE_FTPGETPUT_LONG_OPTIONS(
22 //usage: "\n -c,--continue Continue previous transfer"
23 //usage: "\n -v,--verbose Verbose"
24 //usage: "\n -u,--username Username"
25 //usage: "\n -p,--password Password"
26 //usage: "\n -P,--port Port number"
28 //usage: IF_NOT_FEATURE_FTPGETPUT_LONG_OPTIONS(
29 //usage: "\n -c Continue previous transfer"
30 //usage: "\n -v Verbose"
31 //usage: "\n -u Username"
32 //usage: "\n -p Password"
33 //usage: "\n -P Port number"
36 //usage:#define ftpput_trivial_usage
37 //usage: "[OPTIONS] HOST [REMOTE_FILE] LOCAL_FILE"
38 //usage:#define ftpput_full_usage "\n\n"
39 //usage: "Store a local file on a remote machine via FTP\n"
41 //usage: IF_FEATURE_FTPGETPUT_LONG_OPTIONS(
42 //usage: "\n -v,--verbose Verbose"
43 //usage: "\n -u,--username Username"
44 //usage: "\n -p,--password Password"
45 //usage: "\n -P,--port Port number"
47 //usage: IF_NOT_FEATURE_FTPGETPUT_LONG_OPTIONS(
48 //usage: "\n -v Verbose"
49 //usage: "\n -u Username"
50 //usage: "\n -p Password"
51 //usage: "\n -P Port number"
59 struct len_and_sockaddr *lsa;
63 char buf[1]; /* actually [BUFSZ] */
65 #define G (*(struct globals*)&bb_common_bufsiz1)
66 enum { BUFSZ = COMMON_BUFSIZE - offsetof(struct globals, buf) };
67 struct BUG_G_too_big {
68 char BUG_G_too_big[sizeof(G) <= COMMON_BUFSIZE ? 1 : -1];
70 #define user (G.user )
71 #define password (G.password )
73 #define control_stream (G.control_stream)
74 #define verbose_flag (G.verbose_flag )
75 #define do_continue (G.do_continue )
77 #define INIT_G() do { } while (0)
80 static void ftp_die(const char *msg) NORETURN;
81 static void ftp_die(const char *msg)
83 char *cp = buf; /* buf holds peer's response */
85 /* Guard against garbage from remote server */
86 while (*cp >= ' ' && *cp < '\x7f')
89 bb_error_msg_and_die("unexpected server response%s%s: %s",
90 (msg ? " to " : ""), (msg ? msg : ""), buf);
93 static int ftpcmd(const char *s1, const char *s2)
98 bb_error_msg("cmd %s %s", s1, s2);
102 fprintf(control_stream, (s2 ? "%s %s\r\n" : "%s %s\r\n"+3),
104 fflush(control_stream);
109 if (fgets(buf, BUFSZ - 2, control_stream) == NULL) {
112 } while (!isdigit(buf[0]) || buf[3] != ' ');
120 static void ftp_login(void)
122 /* Connect to the command socket */
123 control_stream = fdopen(xconnect_stream(lsa), "r+");
124 if (control_stream == NULL) {
125 /* fdopen failed - extremely unlikely */
126 bb_perror_nomsg_and_die();
129 if (ftpcmd(NULL, NULL) != 220) {
133 /* Login to the server */
134 switch (ftpcmd("USER", user)) {
138 if (ftpcmd("PASS", password) != 230) {
146 ftpcmd("TYPE I", NULL);
149 static int xconnect_ftpdata(void)
155 TODO: PASV command will not work for IPv6. RFC2428 describes
156 IPv6-capable "extended PASV" - EPSV.
158 "EPSV [protocol]" asks server to bind to and listen on a data port
159 in specified protocol. Protocol is 1 for IPv4, 2 for IPv6.
160 If not specified, defaults to "same as used for control connection".
161 If server understood you, it should answer "229 <some text>(|||port|)"
162 where "|" are literal pipe chars and "port" is ASCII decimal port#.
164 There is also an IPv6-capable replacement for PORT (EPRT),
165 but we don't need that.
167 NB: PASV may still work for some servers even over IPv6.
168 For example, vsftp happily answers
169 "227 Entering Passive Mode (0,0,0,0,n,n)" and proceeds as usual.
171 TODO2: need to stop ignoring IP address in PASV response.
174 if (ftpcmd("PASV", NULL) != 227) {
178 /* Response is "NNN garbageN1,N2,N3,N4,P1,P2[)garbage]
179 * Server's IP is N1.N2.N3.N4 (we ignore it)
180 * Server's port for data connection is P1*256+P2 */
181 buf_ptr = strrchr(buf, ')');
182 if (buf_ptr) *buf_ptr = '\0';
184 buf_ptr = strrchr(buf, ',');
186 port_num = xatoul_range(buf_ptr + 1, 0, 255);
188 buf_ptr = strrchr(buf, ',');
190 port_num += xatoul_range(buf_ptr + 1, 0, 255) * 256;
192 set_nport(&lsa->u.sa, htons(port_num));
193 return xconnect_stream(lsa);
196 static int pump_data_and_QUIT(int from, int to)
199 if (bb_copyfd_eof(from, to) == -1) {
200 /* error msg is already printed by bb_copyfd_eof */
204 /* close data connection */
205 close(from); /* don't know which one is that, so we close both */
208 /* does server confirm that transfer is finished? */
209 if (ftpcmd(NULL, NULL) != 226) {
212 ftpcmd("QUIT", NULL);
218 int ftp_receive(const char *local_path, char *server_path);
221 int ftp_receive(const char *local_path, char *server_path)
227 /* connect to the data socket */
228 fd_data = xconnect_ftpdata();
230 if (ftpcmd("SIZE", server_path) != 213) {
234 if (LONE_DASH(local_path)) {
235 fd_local = STDOUT_FILENO;
241 /* lstat would be wrong here! */
242 if (stat(local_path, &sbuf) < 0) {
243 bb_perror_msg_and_die("stat");
245 if (sbuf.st_size > 0) {
246 beg_range = sbuf.st_size;
253 sprintf(buf, "REST %"OFF_FMT"u", beg_range);
254 if (ftpcmd(buf, NULL) != 350) {
259 if (ftpcmd("RETR", server_path) > 150) {
263 /* create local file _after_ we know that remote file exists */
264 if (fd_local == -1) {
265 fd_local = xopen(local_path,
266 do_continue ? (O_APPEND | O_WRONLY)
267 : (O_CREAT | O_TRUNC | O_WRONLY)
271 return pump_data_and_QUIT(fd_data, fd_local);
276 int ftp_send(const char *server_path, char *local_path);
279 int ftp_send(const char *server_path, char *local_path)
285 /* connect to the data socket */
286 fd_data = xconnect_ftpdata();
288 /* get the local file */
289 fd_local = STDIN_FILENO;
290 if (NOT_LONE_DASH(local_path))
291 fd_local = xopen(local_path, O_RDONLY);
293 response = ftpcmd("STOR", server_path);
302 return pump_data_and_QUIT(fd_local, fd_data);
306 #if ENABLE_FEATURE_FTPGETPUT_LONG_OPTIONS
307 static const char ftpgetput_longopts[] ALIGN1 =
308 "continue\0" Required_argument "c"
309 "verbose\0" No_argument "v"
310 "username\0" Required_argument "u"
311 "password\0" Required_argument "p"
312 "port\0" Required_argument "P"
316 int ftpgetput_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
317 int ftpgetput_main(int argc UNUSED_PARAM, char **argv)
320 const char *port = "ftp";
321 /* socket to ftp server */
323 #if ENABLE_FTPPUT && !ENABLE_FTPGET
324 # define ftp_action ftp_send
325 #elif ENABLE_FTPGET && !ENABLE_FTPPUT
326 # define ftp_action ftp_receive
328 int (*ftp_action)(const char *, char *) = ftp_send;
330 /* Check to see if the command is ftpget or ftput */
331 if (applet_name[3] == 'g') {
332 ftp_action = ftp_receive;
337 /* Set default values */
339 password = "busybox@";
342 * Decipher the command line
344 #if ENABLE_FEATURE_FTPGETPUT_LONG_OPTIONS
345 applet_long_options = ftpgetput_longopts;
347 opt_complementary = "-2:vv:cc"; /* must have 2 to 3 params; -v and -c count */
348 opt = getopt32(argv, "cvu:p:P:", &user, &password, &port,
349 &verbose_flag, &do_continue);
352 /* We want to do exactly _one_ DNS lookup, since some
353 * sites (i.e. ftp.us.debian.org) use round-robin DNS
354 * and we want to connect to only one IP... */
355 lsa = xhost2sockaddr(argv[0], bb_lookup_port(port, "tcp", 21));
357 printf("Connecting to %s (%s)\n", argv[0],
358 xmalloc_sockaddr2dotted(&lsa->u.sa));
362 return ftp_action(argv[1], argv[2] ? argv[2] : argv[1]);