blob: c0ecddac1cab4e208999d2eb12a37e7ab87296aa [file] [log] [blame]
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +00001/* vi: set sw=4 ts=4: */
2/*
Eric Andersenc7bda1c2004-03-15 08:29:22 +00003 * ftpget
4 *
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +00005 * Mini implementation of FTP to retrieve a remote file.
6 *
7 * Copyright (C) 2002 Jeff Angielski, The PTR Group <jeff@theptrgroup.com>
Denis Vlasenko0beaff82007-09-21 13:16:32 +00008 * Copyright (C) 2002 Glenn McGrath
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +00009 *
10 * Based on wget.c by Chip Rosenthal Covad Communications
11 * <chip@laserlink.net>
12 *
Rob Landley6f037222005-11-08 00:52:31 +000013 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +000014 */
15
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000016#include "libbb.h"
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +000017
Denis Vlasenko0e7940a2008-03-29 07:37:42 +000018struct globals {
Denis Vlasenkob6aae0f2007-01-29 22:51:25 +000019 const char *user;
20 const char *password;
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +000021 struct len_and_sockaddr *lsa;
Denis Vlasenko7cb808e2008-03-29 07:40:35 +000022 FILE *control_stream;
Denis Vlasenko0e7940a2008-03-29 07:37:42 +000023 int verbose_flag;
24 int do_continue;
Denis Vlasenko7cb808e2008-03-29 07:40:35 +000025 char buf[1]; /* actually [BUFSZ] */
Denis Vlasenko0e7940a2008-03-29 07:37:42 +000026};
27#define G (*(struct globals*)&bb_common_bufsiz1)
28enum { BUFSZ = COMMON_BUFSIZE - offsetof(struct globals, buf) };
29struct BUG_G_too_big {
Denis Vlasenko7049ff82008-06-25 09:53:17 +000030 char BUG_G_too_big[sizeof(G) <= COMMON_BUFSIZE ? 1 : -1];
Denis Vlasenko0e7940a2008-03-29 07:37:42 +000031};
Denis Vlasenko7cb808e2008-03-29 07:40:35 +000032#define user (G.user )
33#define password (G.password )
34#define lsa (G.lsa )
35#define control_stream (G.control_stream)
36#define verbose_flag (G.verbose_flag )
37#define do_continue (G.do_continue )
38#define buf (G.buf )
Denis Vlasenko7049ff82008-06-25 09:53:17 +000039#define INIT_G() do { } while (0)
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +000040
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +000041
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000042static void ftp_die(const char *msg) NORETURN;
Denis Vlasenko0e7940a2008-03-29 07:37:42 +000043static void ftp_die(const char *msg)
Denis Vlasenko562dc242007-01-03 21:55:50 +000044{
Denis Vlasenko7cb808e2008-03-29 07:40:35 +000045 char *cp = buf; /* buf holds peer's response */
Denis Vlasenko0e7940a2008-03-29 07:37:42 +000046
Denis Vlasenko562dc242007-01-03 21:55:50 +000047 /* Guard against garbage from remote server */
Denis Vlasenko0e7940a2008-03-29 07:37:42 +000048 while (*cp >= ' ' && *cp < '\x7f')
49 cp++;
Denis Vlasenko7cb808e2008-03-29 07:40:35 +000050 *cp = '\0';
51 bb_error_msg_and_die("unexpected server response%s%s: %s",
52 (msg ? " to " : ""), (msg ? msg : ""), buf);
Denis Vlasenko562dc242007-01-03 21:55:50 +000053}
54
Denis Vlasenko7cb808e2008-03-29 07:40:35 +000055static int ftpcmd(const char *s1, const char *s2)
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +000056{
Denis Vlasenko562dc242007-01-03 21:55:50 +000057 unsigned n;
Denis Vlasenko7cb808e2008-03-29 07:40:35 +000058
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +000059 if (verbose_flag) {
Denis Vlasenko3821fb12007-01-11 16:51:21 +000060 bb_error_msg("cmd %s %s", s1, s2);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +000061 }
62
63 if (s1) {
Denis Vlasenko7cb808e2008-03-29 07:40:35 +000064 fprintf(control_stream, (s2 ? "%s %s\r\n" : "%s %s\r\n"+3),
65 s1, s2);
66 fflush(control_stream);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +000067 }
Denis Vlasenko0e7940a2008-03-29 07:37:42 +000068
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +000069 do {
Denis Vlasenko7cb808e2008-03-29 07:40:35 +000070 strcpy(buf, "EOF");
71 if (fgets(buf, BUFSZ - 2, control_stream) == NULL) {
72 ftp_die(NULL);
Glenn L McGrath5ec58282004-05-04 10:43:34 +000073 }
Denis Vlasenko13858992006-10-08 12:49:22 +000074 } while (!isdigit(buf[0]) || buf[3] != ' ');
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +000075
Denis Vlasenko562dc242007-01-03 21:55:50 +000076 buf[3] = '\0';
77 n = xatou(buf);
78 buf[3] = ' ';
79 return n;
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +000080}
81
Denis Vlasenko7cb808e2008-03-29 07:40:35 +000082static void ftp_login(void)
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +000083{
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +000084 /* Connect to the command socket */
Denis Vlasenko0e7940a2008-03-29 07:37:42 +000085 control_stream = fdopen(xconnect_stream(lsa), "r+");
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +000086 if (control_stream == NULL) {
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +000087 /* fdopen failed - extremely unlikely */
Denis Vlasenko562dc242007-01-03 21:55:50 +000088 bb_perror_nomsg_and_die();
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +000089 }
90
Denis Vlasenko7cb808e2008-03-29 07:40:35 +000091 if (ftpcmd(NULL, NULL) != 220) {
Denis Vlasenko0e7940a2008-03-29 07:37:42 +000092 ftp_die(NULL);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +000093 }
94
95 /* Login to the server */
Denis Vlasenko7cb808e2008-03-29 07:40:35 +000096 switch (ftpcmd("USER", user)) {
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +000097 case 230:
98 break;
99 case 331:
Denis Vlasenko7cb808e2008-03-29 07:40:35 +0000100 if (ftpcmd("PASS", password) != 230) {
Denis Vlasenko0e7940a2008-03-29 07:37:42 +0000101 ftp_die("PASS");
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000102 }
103 break;
104 default:
Denis Vlasenko0e7940a2008-03-29 07:37:42 +0000105 ftp_die("USER");
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000106 }
107
Denis Vlasenko7cb808e2008-03-29 07:40:35 +0000108 ftpcmd("TYPE I", NULL);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000109}
110
Denis Vlasenko0e7940a2008-03-29 07:37:42 +0000111static int xconnect_ftpdata(void)
Denis Vlasenko6c615a62008-03-28 22:11:49 +0000112{
113 char *buf_ptr;
114 unsigned port_num;
115
Denis Vlasenko7cb808e2008-03-29 07:40:35 +0000116/*
117TODO: PASV command will not work for IPv6. RFC2428 describes
118IPv6-capable "extended PASV" - EPSV.
119
120"EPSV [protocol]" asks server to bind to and listen on a data port
121in specified protocol. Protocol is 1 for IPv4, 2 for IPv6.
122If not specified, defaults to "same as used for control connection".
123If server understood you, it should answer "229 <some text>(|||port|)"
124where "|" are literal pipe chars and "port" is ASCII decimal port#.
125
126There is also an IPv6-capable replacement for PORT (EPRT),
127but we don't need that.
128
129NB: PASV may still work for some servers even over IPv6.
130For example, vsftp happily answers
131"227 Entering Passive Mode (0,0,0,0,n,n)" and proceeds as usual.
132
133TODO2: need to stop ignoring IP address in PASV response.
134*/
135
136 if (ftpcmd("PASV", NULL) != 227) {
137 ftp_die("PASV");
138 }
139
Denis Vlasenko6c615a62008-03-28 22:11:49 +0000140 /* Response is "NNN garbageN1,N2,N3,N4,P1,P2[)garbage]
141 * Server's IP is N1.N2.N3.N4 (we ignore it)
142 * Server's port for data connection is P1*256+P2 */
143 buf_ptr = strrchr(buf, ')');
144 if (buf_ptr) *buf_ptr = '\0';
145
146 buf_ptr = strrchr(buf, ',');
147 *buf_ptr = '\0';
148 port_num = xatoul_range(buf_ptr + 1, 0, 255);
149
150 buf_ptr = strrchr(buf, ',');
151 *buf_ptr = '\0';
152 port_num += xatoul_range(buf_ptr + 1, 0, 255) * 256;
153
Denis Vlasenko0e7940a2008-03-29 07:37:42 +0000154 set_nport(lsa, htons(port_num));
155 return xconnect_stream(lsa);
Denis Vlasenko6c615a62008-03-28 22:11:49 +0000156}
157
Denis Vlasenko7cb808e2008-03-29 07:40:35 +0000158static int pump_data_and_QUIT(int from, int to)
159{
160 /* copy the file */
161 if (bb_copyfd_eof(from, to) == -1) {
162 /* error msg is already printed by bb_copyfd_eof */
163 return EXIT_FAILURE;
164 }
165
166 /* close data connection */
167 close(from); /* don't know which one is that, so we close both */
168 close(to);
169
170 /* does server confirm that transfer is finished? */
171 if (ftpcmd(NULL, NULL) != 226) {
172 ftp_die(NULL);
173 }
174 ftpcmd("QUIT", NULL);
175
176 return EXIT_SUCCESS;
177}
178
Rob Landleyb1c3fbc2006-05-04 19:52:28 +0000179#if !ENABLE_FTPGET
Denis Vlasenko7cb808e2008-03-29 07:40:35 +0000180int ftp_receive(const char *local_path, char *server_path);
Rob Landleyb1c3fbc2006-05-04 19:52:28 +0000181#else
Denis Vlasenko7039a662006-10-08 17:54:47 +0000182static
Denis Vlasenko7cb808e2008-03-29 07:40:35 +0000183int ftp_receive(const char *local_path, char *server_path)
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000184{
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000185 int fd_data;
Glenn L McGrath236e93d2003-12-20 05:43:34 +0000186 int fd_local = -1;
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000187 off_t beg_range = 0;
188
Denis Vlasenko6c615a62008-03-28 22:11:49 +0000189 /* connect to the data socket */
Denis Vlasenko0e7940a2008-03-29 07:37:42 +0000190 fd_data = xconnect_ftpdata();
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000191
Denis Vlasenko7cb808e2008-03-29 07:40:35 +0000192 if (ftpcmd("SIZE", server_path) != 213) {
Rob Landleybc059bc2006-01-10 06:36:00 +0000193 do_continue = 0;
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000194 }
195
Denis Vlasenko9f739442006-12-16 23:49:13 +0000196 if (LONE_DASH(local_path)) {
Eric Andersen70060d22004-03-27 10:02:48 +0000197 fd_local = STDOUT_FILENO;
Glenn L McGrath236e93d2003-12-20 05:43:34 +0000198 do_continue = 0;
199 }
200
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000201 if (do_continue) {
202 struct stat sbuf;
Denis Vlasenko6c615a62008-03-28 22:11:49 +0000203 /* lstat would be wrong here! */
204 if (stat(local_path, &sbuf) < 0) {
205 bb_perror_msg_and_die("stat");
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000206 }
207 if (sbuf.st_size > 0) {
208 beg_range = sbuf.st_size;
209 } else {
210 do_continue = 0;
211 }
212 }
213
214 if (do_continue) {
Denis Vlasenkocf30cc82006-11-24 14:53:18 +0000215 sprintf(buf, "REST %"OFF_FMT"d", beg_range);
Denis Vlasenko7cb808e2008-03-29 07:40:35 +0000216 if (ftpcmd(buf, NULL) != 350) {
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000217 do_continue = 0;
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000218 }
219 }
220
Denis Vlasenko7cb808e2008-03-29 07:40:35 +0000221 if (ftpcmd("RETR", server_path) > 150) {
Denis Vlasenko0e7940a2008-03-29 07:37:42 +0000222 ftp_die("RETR");
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000223 }
224
Denis Vlasenko7cb808e2008-03-29 07:40:35 +0000225 /* create local file _after_ we know that remote file exists */
Glenn L McGrath236e93d2003-12-20 05:43:34 +0000226 if (fd_local == -1) {
Denis Vlasenko6c615a62008-03-28 22:11:49 +0000227 fd_local = xopen(local_path,
228 do_continue ? (O_APPEND | O_WRONLY)
229 : (O_CREAT | O_TRUNC | O_WRONLY)
230 );
Glenn L McGrath1643f412002-12-18 02:47:40 +0000231 }
232
Denis Vlasenko7cb808e2008-03-29 07:40:35 +0000233 return pump_data_and_QUIT(fd_data, fd_local);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000234}
235#endif
236
Rob Landleyb1c3fbc2006-05-04 19:52:28 +0000237#if !ENABLE_FTPPUT
Denis Vlasenko7cb808e2008-03-29 07:40:35 +0000238int ftp_send(const char *server_path, char *local_path);
Rob Landleyb1c3fbc2006-05-04 19:52:28 +0000239#else
Denis Vlasenko7039a662006-10-08 17:54:47 +0000240static
Denis Vlasenko7cb808e2008-03-29 07:40:35 +0000241int ftp_send(const char *server_path, char *local_path)
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000242{
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000243 int fd_data;
244 int fd_local;
245 int response;
246
Denis Vlasenko6c615a62008-03-28 22:11:49 +0000247 /* connect to the data socket */
Denis Vlasenko0e7940a2008-03-29 07:37:42 +0000248 fd_data = xconnect_ftpdata();
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000249
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000250 /* get the local file */
Denis Vlasenko9f739442006-12-16 23:49:13 +0000251 fd_local = STDIN_FILENO;
Denis Vlasenko0e7940a2008-03-29 07:37:42 +0000252 if (NOT_LONE_DASH(local_path))
Rob Landleyd921b2e2006-08-03 15:41:12 +0000253 fd_local = xopen(local_path, O_RDONLY);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000254
Denis Vlasenko7cb808e2008-03-29 07:40:35 +0000255 response = ftpcmd("STOR", server_path);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000256 switch (response) {
257 case 125:
258 case 150:
259 break;
260 default:
Denis Vlasenko0e7940a2008-03-29 07:37:42 +0000261 ftp_die("STOR");
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000262 }
263
Denis Vlasenko7cb808e2008-03-29 07:40:35 +0000264 return pump_data_and_QUIT(fd_local, fd_data);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000265}
266#endif
267
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +0000268#if ENABLE_FEATURE_FTPGETPUT_LONG_OPTIONS
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000269static const char ftpgetput_longopts[] ALIGN1 =
Denis Vlasenkobdc88fd2007-07-23 17:14:14 +0000270 "continue\0" Required_argument "c"
271 "verbose\0" No_argument "v"
272 "username\0" Required_argument "u"
273 "password\0" Required_argument "p"
274 "port\0" Required_argument "P"
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000275 ;
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +0000276#endif
Glenn L McGrathb51eb262003-12-19 10:37:52 +0000277
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000278int ftpgetput_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000279int ftpgetput_main(int argc UNUSED_PARAM, char **argv)
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000280{
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000281 unsigned opt;
Denis Vlasenko562dc242007-01-03 21:55:50 +0000282 const char *port = "ftp";
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000283 /* socket to ftp server */
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000284
Denis Vlasenko562dc242007-01-03 21:55:50 +0000285#if ENABLE_FTPPUT && !ENABLE_FTPGET
286# define ftp_action ftp_send
287#elif ENABLE_FTPGET && !ENABLE_FTPPUT
288# define ftp_action ftp_receive
289#else
Denis Vlasenko7cb808e2008-03-29 07:40:35 +0000290 int (*ftp_action)(const char *, char *) = ftp_send;
Denis Vlasenko0e7940a2008-03-29 07:37:42 +0000291
Glenn L McGrath266c1f52003-12-20 03:19:27 +0000292 /* Check to see if the command is ftpget or ftput */
Denis Vlasenko562dc242007-01-03 21:55:50 +0000293 if (applet_name[3] == 'g') {
Bernhard Reutner-Fischere0387a62006-06-07 13:31:59 +0000294 ftp_action = ftp_receive;
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000295 }
Denis Vlasenko562dc242007-01-03 21:55:50 +0000296#endif
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000297
Denis Vlasenko0e7940a2008-03-29 07:37:42 +0000298 INIT_G();
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000299 /* Set default values */
Denis Vlasenko0e7940a2008-03-29 07:37:42 +0000300 user = "anonymous";
301 password = "busybox@";
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000302
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000303 /*
304 * Decipher the command line
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000305 */
Denis Vlasenkoc61852a2006-11-29 11:09:43 +0000306#if ENABLE_FEATURE_FTPGETPUT_LONG_OPTIONS
Denis Vlasenkobdc88fd2007-07-23 17:14:14 +0000307 applet_long_options = ftpgetput_longopts;
Denis Vlasenkoc61852a2006-11-29 11:09:43 +0000308#endif
Vladimir Dronnikov1dacfbb2009-10-23 23:34:43 +0200309 opt_complementary = "-2:vv:cc"; /* must have 2 to 3 params; -v and -c count */
Denis Vlasenko0e7940a2008-03-29 07:37:42 +0000310 opt = getopt32(argv, "cvu:p:P:", &user, &password, &port,
311 &verbose_flag, &do_continue);
Denis Vlasenko562dc242007-01-03 21:55:50 +0000312 argv += optind;
Glenn L McGrath236e93d2003-12-20 05:43:34 +0000313
Eric Andersen04d055f2003-11-03 21:20:18 +0000314 /* We want to do exactly _one_ DNS lookup, since some
315 * sites (i.e. ftp.us.debian.org) use round-robin DNS
316 * and we want to connect to only one IP... */
Denis Vlasenko0e7940a2008-03-29 07:37:42 +0000317 lsa = xhost2sockaddr(argv[0], bb_lookup_port(port, "tcp", 21));
Eric Andersen04d055f2003-11-03 21:20:18 +0000318 if (verbose_flag) {
Denis Vlasenko85629f02007-01-22 09:36:41 +0000319 printf("Connecting to %s (%s)\n", argv[0],
Denis Vlasenko0e7940a2008-03-29 07:37:42 +0000320 xmalloc_sockaddr2dotted(&lsa->u.sa));
Eric Andersen04d055f2003-11-03 21:20:18 +0000321 }
322
Denis Vlasenko7cb808e2008-03-29 07:40:35 +0000323 ftp_login();
Vladimir Dronnikov1dacfbb2009-10-23 23:34:43 +0200324 return ftp_action(argv[1], argv[2] ? argv[2] : argv[1]);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000325}