blob: b967cd7d9fefd8df2129787a58896b5e0b42aa82 [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 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +020013 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +000014 */
Denys Vlasenko47367e12016-11-23 09:05:14 +010015//config:config FTPGET
Denys Vlasenko4eed2c62017-07-18 22:01:24 +020016//config: bool "ftpget (8 kb)"
Denys Vlasenko47367e12016-11-23 09:05:14 +010017//config: default y
18//config: help
19//config: Retrieve a remote file via FTP.
20//config:
21//config:config FTPPUT
Denys Vlasenko4eed2c62017-07-18 22:01:24 +020022//config: bool "ftpput (7.7 kb)"
Denys Vlasenko47367e12016-11-23 09:05:14 +010023//config: default y
24//config: help
25//config: Store a remote file via FTP.
26//config:
27//config:config FEATURE_FTPGETPUT_LONG_OPTIONS
28//config: bool "Enable long options in ftpget/ftpput"
29//config: default y
30//config: depends on LONG_OPTS && (FTPGET || FTPPUT)
Denys Vlasenko47367e12016-11-23 09:05:14 +010031
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))
34
35//kbuild:lib-$(CONFIG_FTPGET) += ftpgetput.o
36//kbuild:lib-$(CONFIG_FTPPUT) += ftpgetput.o
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +000037
Pere Orga5bc8c002011-04-11 03:29:49 +020038//usage:#define ftpget_trivial_usage
39//usage: "[OPTIONS] HOST [LOCAL_FILE] REMOTE_FILE"
40//usage:#define ftpget_full_usage "\n\n"
Denys Vlasenkocc1bb602012-03-19 12:22:57 +010041//usage: "Download a file via FTP\n"
Pere Orga5bc8c002011-04-11 03:29:49 +020042//usage: IF_FEATURE_FTPGETPUT_LONG_OPTIONS(
Denys Vlasenkocc1bb602012-03-19 12:22:57 +010043//usage: "\n -c,--continue Continue previous transfer"
44//usage: "\n -v,--verbose Verbose"
45//usage: "\n -u,--username USER Username"
46//usage: "\n -p,--password PASS Password"
47//usage: "\n -P,--port NUM Port"
Pere Orga5bc8c002011-04-11 03:29:49 +020048//usage: )
49//usage: IF_NOT_FEATURE_FTPGETPUT_LONG_OPTIONS(
50//usage: "\n -c Continue previous transfer"
51//usage: "\n -v Verbose"
Denys Vlasenkocc1bb602012-03-19 12:22:57 +010052//usage: "\n -u USER Username"
53//usage: "\n -p PASS Password"
54//usage: "\n -P NUM Port"
Pere Orga5bc8c002011-04-11 03:29:49 +020055//usage: )
56//usage:
57//usage:#define ftpput_trivial_usage
58//usage: "[OPTIONS] HOST [REMOTE_FILE] LOCAL_FILE"
59//usage:#define ftpput_full_usage "\n\n"
Denys Vlasenkocc1bb602012-03-19 12:22:57 +010060//usage: "Upload a file to a FTP server\n"
Pere Orga5bc8c002011-04-11 03:29:49 +020061//usage: IF_FEATURE_FTPGETPUT_LONG_OPTIONS(
Denys Vlasenkocc1bb602012-03-19 12:22:57 +010062//usage: "\n -v,--verbose Verbose"
63//usage: "\n -u,--username USER Username"
64//usage: "\n -p,--password PASS Password"
65//usage: "\n -P,--port NUM Port"
Pere Orga5bc8c002011-04-11 03:29:49 +020066//usage: )
67//usage: IF_NOT_FEATURE_FTPGETPUT_LONG_OPTIONS(
68//usage: "\n -v Verbose"
Denys Vlasenkocc1bb602012-03-19 12:22:57 +010069//usage: "\n -u USER Username"
70//usage: "\n -p PASS Password"
71//usage: "\n -P NUM Port number"
Pere Orga5bc8c002011-04-11 03:29:49 +020072//usage: )
73
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000074#include "libbb.h"
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +020075#include "common_bufsiz.h"
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +000076
Denis Vlasenko0e7940a2008-03-29 07:37:42 +000077struct globals {
Denis Vlasenkob6aae0f2007-01-29 22:51:25 +000078 const char *user;
79 const char *password;
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +000080 struct len_and_sockaddr *lsa;
Denis Vlasenko7cb808e2008-03-29 07:40:35 +000081 FILE *control_stream;
Denis Vlasenko0e7940a2008-03-29 07:37:42 +000082 int verbose_flag;
83 int do_continue;
Denys Vlasenko60a94142011-05-13 20:57:01 +020084 char buf[4]; /* actually [BUFSZ] */
Denys Vlasenko98a4c7c2010-02-04 15:00:15 +010085} FIX_ALIASING;
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +020086#define G (*(struct globals*)bb_common_bufsiz1)
Denis Vlasenko0e7940a2008-03-29 07:37:42 +000087enum { BUFSZ = COMMON_BUFSIZE - offsetof(struct globals, buf) };
Denis Vlasenko7cb808e2008-03-29 07:40:35 +000088#define user (G.user )
89#define password (G.password )
90#define lsa (G.lsa )
91#define control_stream (G.control_stream)
92#define verbose_flag (G.verbose_flag )
93#define do_continue (G.do_continue )
94#define buf (G.buf )
Denys Vlasenkoab3964d2015-10-13 14:50:20 +020095#define INIT_G() do { \
Denys Vlasenko47cfbf32016-04-21 18:18:48 +020096 setup_common_bufsiz(); \
Denys Vlasenkoab3964d2015-10-13 14:50:20 +020097 BUILD_BUG_ON(sizeof(G) > COMMON_BUFSIZE); \
98} while (0)
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +000099
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000100
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000101static void ftp_die(const char *msg) NORETURN;
Denis Vlasenko0e7940a2008-03-29 07:37:42 +0000102static void ftp_die(const char *msg)
Denis Vlasenko562dc242007-01-03 21:55:50 +0000103{
Denis Vlasenko7cb808e2008-03-29 07:40:35 +0000104 char *cp = buf; /* buf holds peer's response */
Denis Vlasenko0e7940a2008-03-29 07:37:42 +0000105
Denis Vlasenko562dc242007-01-03 21:55:50 +0000106 /* Guard against garbage from remote server */
Denis Vlasenko0e7940a2008-03-29 07:37:42 +0000107 while (*cp >= ' ' && *cp < '\x7f')
108 cp++;
Denis Vlasenko7cb808e2008-03-29 07:40:35 +0000109 *cp = '\0';
110 bb_error_msg_and_die("unexpected server response%s%s: %s",
111 (msg ? " to " : ""), (msg ? msg : ""), buf);
Denis Vlasenko562dc242007-01-03 21:55:50 +0000112}
113
Denis Vlasenko7cb808e2008-03-29 07:40:35 +0000114static int ftpcmd(const char *s1, const char *s2)
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000115{
Denis Vlasenko562dc242007-01-03 21:55:50 +0000116 unsigned n;
Denis Vlasenko7cb808e2008-03-29 07:40:35 +0000117
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000118 if (verbose_flag) {
Denis Vlasenko3821fb12007-01-11 16:51:21 +0000119 bb_error_msg("cmd %s %s", s1, s2);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000120 }
121
122 if (s1) {
Denis Vlasenko7cb808e2008-03-29 07:40:35 +0000123 fprintf(control_stream, (s2 ? "%s %s\r\n" : "%s %s\r\n"+3),
124 s1, s2);
125 fflush(control_stream);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000126 }
Denis Vlasenko0e7940a2008-03-29 07:37:42 +0000127
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000128 do {
Denys Vlasenko60a94142011-05-13 20:57:01 +0200129 strcpy(buf, "EOF"); /* for ftp_die */
Denis Vlasenko7cb808e2008-03-29 07:40:35 +0000130 if (fgets(buf, BUFSZ - 2, control_stream) == NULL) {
131 ftp_die(NULL);
Glenn L McGrath5ec58282004-05-04 10:43:34 +0000132 }
Denis Vlasenko13858992006-10-08 12:49:22 +0000133 } while (!isdigit(buf[0]) || buf[3] != ' ');
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000134
Denis Vlasenko562dc242007-01-03 21:55:50 +0000135 buf[3] = '\0';
136 n = xatou(buf);
137 buf[3] = ' ';
138 return n;
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000139}
140
Denis Vlasenko7cb808e2008-03-29 07:40:35 +0000141static void ftp_login(void)
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000142{
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000143 /* Connect to the command socket */
Denis Vlasenko0e7940a2008-03-29 07:37:42 +0000144 control_stream = fdopen(xconnect_stream(lsa), "r+");
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000145 if (control_stream == NULL) {
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000146 /* fdopen failed - extremely unlikely */
Denis Vlasenko562dc242007-01-03 21:55:50 +0000147 bb_perror_nomsg_and_die();
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000148 }
149
Denis Vlasenko7cb808e2008-03-29 07:40:35 +0000150 if (ftpcmd(NULL, NULL) != 220) {
Denis Vlasenko0e7940a2008-03-29 07:37:42 +0000151 ftp_die(NULL);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000152 }
153
154 /* Login to the server */
Denis Vlasenko7cb808e2008-03-29 07:40:35 +0000155 switch (ftpcmd("USER", user)) {
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000156 case 230:
157 break;
158 case 331:
Denis Vlasenko7cb808e2008-03-29 07:40:35 +0000159 if (ftpcmd("PASS", password) != 230) {
Denis Vlasenko0e7940a2008-03-29 07:37:42 +0000160 ftp_die("PASS");
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000161 }
162 break;
163 default:
Denis Vlasenko0e7940a2008-03-29 07:37:42 +0000164 ftp_die("USER");
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000165 }
166
Denis Vlasenko7cb808e2008-03-29 07:40:35 +0000167 ftpcmd("TYPE I", NULL);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000168}
169
Denis Vlasenko0e7940a2008-03-29 07:37:42 +0000170static int xconnect_ftpdata(void)
Denis Vlasenko6c615a62008-03-28 22:11:49 +0000171{
172 char *buf_ptr;
173 unsigned port_num;
174
Denis Vlasenko7cb808e2008-03-29 07:40:35 +0000175/*
176TODO: PASV command will not work for IPv6. RFC2428 describes
177IPv6-capable "extended PASV" - EPSV.
178
179"EPSV [protocol]" asks server to bind to and listen on a data port
180in specified protocol. Protocol is 1 for IPv4, 2 for IPv6.
181If not specified, defaults to "same as used for control connection".
182If server understood you, it should answer "229 <some text>(|||port|)"
183where "|" are literal pipe chars and "port" is ASCII decimal port#.
184
185There is also an IPv6-capable replacement for PORT (EPRT),
186but we don't need that.
187
188NB: PASV may still work for some servers even over IPv6.
189For example, vsftp happily answers
190"227 Entering Passive Mode (0,0,0,0,n,n)" and proceeds as usual.
191
192TODO2: need to stop ignoring IP address in PASV response.
193*/
194
195 if (ftpcmd("PASV", NULL) != 227) {
196 ftp_die("PASV");
197 }
198
Denis Vlasenko6c615a62008-03-28 22:11:49 +0000199 /* Response is "NNN garbageN1,N2,N3,N4,P1,P2[)garbage]
200 * Server's IP is N1.N2.N3.N4 (we ignore it)
201 * Server's port for data connection is P1*256+P2 */
202 buf_ptr = strrchr(buf, ')');
203 if (buf_ptr) *buf_ptr = '\0';
204
205 buf_ptr = strrchr(buf, ',');
206 *buf_ptr = '\0';
207 port_num = xatoul_range(buf_ptr + 1, 0, 255);
208
209 buf_ptr = strrchr(buf, ',');
210 *buf_ptr = '\0';
211 port_num += xatoul_range(buf_ptr + 1, 0, 255) * 256;
212
Denys Vlasenkoca183112011-04-07 17:52:20 +0200213 set_nport(&lsa->u.sa, htons(port_num));
Denis Vlasenko0e7940a2008-03-29 07:37:42 +0000214 return xconnect_stream(lsa);
Denis Vlasenko6c615a62008-03-28 22:11:49 +0000215}
216
Denis Vlasenko7cb808e2008-03-29 07:40:35 +0000217static int pump_data_and_QUIT(int from, int to)
218{
219 /* copy the file */
220 if (bb_copyfd_eof(from, to) == -1) {
221 /* error msg is already printed by bb_copyfd_eof */
222 return EXIT_FAILURE;
223 }
224
225 /* close data connection */
226 close(from); /* don't know which one is that, so we close both */
227 close(to);
228
229 /* does server confirm that transfer is finished? */
230 if (ftpcmd(NULL, NULL) != 226) {
231 ftp_die(NULL);
232 }
233 ftpcmd("QUIT", NULL);
234
235 return EXIT_SUCCESS;
236}
237
Rob Landleyb1c3fbc2006-05-04 19:52:28 +0000238#if !ENABLE_FTPGET
Denis Vlasenko7cb808e2008-03-29 07:40:35 +0000239int ftp_receive(const char *local_path, char *server_path);
Rob Landleyb1c3fbc2006-05-04 19:52:28 +0000240#else
Denis Vlasenko7039a662006-10-08 17:54:47 +0000241static
Denis Vlasenko7cb808e2008-03-29 07:40:35 +0000242int ftp_receive(const char *local_path, char *server_path)
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000243{
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000244 int fd_data;
Glenn L McGrath236e93d2003-12-20 05:43:34 +0000245 int fd_local = -1;
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000246 off_t beg_range = 0;
247
Denis Vlasenko6c615a62008-03-28 22:11:49 +0000248 /* connect to the data socket */
Denis Vlasenko0e7940a2008-03-29 07:37:42 +0000249 fd_data = xconnect_ftpdata();
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000250
Denis Vlasenko7cb808e2008-03-29 07:40:35 +0000251 if (ftpcmd("SIZE", server_path) != 213) {
Rob Landleybc059bc2006-01-10 06:36:00 +0000252 do_continue = 0;
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000253 }
254
Denis Vlasenko9f739442006-12-16 23:49:13 +0000255 if (LONE_DASH(local_path)) {
Eric Andersen70060d22004-03-27 10:02:48 +0000256 fd_local = STDOUT_FILENO;
Glenn L McGrath236e93d2003-12-20 05:43:34 +0000257 do_continue = 0;
258 }
259
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000260 if (do_continue) {
261 struct stat sbuf;
Denis Vlasenko6c615a62008-03-28 22:11:49 +0000262 /* lstat would be wrong here! */
263 if (stat(local_path, &sbuf) < 0) {
264 bb_perror_msg_and_die("stat");
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000265 }
266 if (sbuf.st_size > 0) {
267 beg_range = sbuf.st_size;
268 } else {
269 do_continue = 0;
270 }
271 }
272
273 if (do_continue) {
Denys Vlasenkoa3aa3e32009-12-11 12:36:10 +0100274 sprintf(buf, "REST %"OFF_FMT"u", beg_range);
Denis Vlasenko7cb808e2008-03-29 07:40:35 +0000275 if (ftpcmd(buf, NULL) != 350) {
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000276 do_continue = 0;
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000277 }
278 }
279
Denis Vlasenko7cb808e2008-03-29 07:40:35 +0000280 if (ftpcmd("RETR", server_path) > 150) {
Denis Vlasenko0e7940a2008-03-29 07:37:42 +0000281 ftp_die("RETR");
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000282 }
283
Denis Vlasenko7cb808e2008-03-29 07:40:35 +0000284 /* create local file _after_ we know that remote file exists */
Glenn L McGrath236e93d2003-12-20 05:43:34 +0000285 if (fd_local == -1) {
Denis Vlasenko6c615a62008-03-28 22:11:49 +0000286 fd_local = xopen(local_path,
287 do_continue ? (O_APPEND | O_WRONLY)
288 : (O_CREAT | O_TRUNC | O_WRONLY)
289 );
Glenn L McGrath1643f412002-12-18 02:47:40 +0000290 }
291
Denis Vlasenko7cb808e2008-03-29 07:40:35 +0000292 return pump_data_and_QUIT(fd_data, fd_local);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000293}
294#endif
295
Rob Landleyb1c3fbc2006-05-04 19:52:28 +0000296#if !ENABLE_FTPPUT
Denis Vlasenko7cb808e2008-03-29 07:40:35 +0000297int ftp_send(const char *server_path, char *local_path);
Rob Landleyb1c3fbc2006-05-04 19:52:28 +0000298#else
Denis Vlasenko7039a662006-10-08 17:54:47 +0000299static
Denis Vlasenko7cb808e2008-03-29 07:40:35 +0000300int ftp_send(const char *server_path, char *local_path)
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000301{
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000302 int fd_data;
303 int fd_local;
304 int response;
305
Denis Vlasenko6c615a62008-03-28 22:11:49 +0000306 /* connect to the data socket */
Denis Vlasenko0e7940a2008-03-29 07:37:42 +0000307 fd_data = xconnect_ftpdata();
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000308
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000309 /* get the local file */
Denis Vlasenko9f739442006-12-16 23:49:13 +0000310 fd_local = STDIN_FILENO;
Denis Vlasenko0e7940a2008-03-29 07:37:42 +0000311 if (NOT_LONE_DASH(local_path))
Rob Landleyd921b2e2006-08-03 15:41:12 +0000312 fd_local = xopen(local_path, O_RDONLY);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000313
Denis Vlasenko7cb808e2008-03-29 07:40:35 +0000314 response = ftpcmd("STOR", server_path);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000315 switch (response) {
316 case 125:
317 case 150:
318 break;
319 default:
Denis Vlasenko0e7940a2008-03-29 07:37:42 +0000320 ftp_die("STOR");
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000321 }
322
Denis Vlasenko7cb808e2008-03-29 07:40:35 +0000323 return pump_data_and_QUIT(fd_local, fd_data);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000324}
325#endif
326
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +0000327#if ENABLE_FEATURE_FTPGETPUT_LONG_OPTIONS
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000328static const char ftpgetput_longopts[] ALIGN1 =
Denis Vlasenkobdc88fd2007-07-23 17:14:14 +0000329 "continue\0" Required_argument "c"
330 "verbose\0" No_argument "v"
331 "username\0" Required_argument "u"
332 "password\0" Required_argument "p"
333 "port\0" Required_argument "P"
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000334 ;
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +0000335#endif
Glenn L McGrathb51eb262003-12-19 10:37:52 +0000336
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000337int ftpgetput_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000338int ftpgetput_main(int argc UNUSED_PARAM, char **argv)
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000339{
Denis Vlasenko562dc242007-01-03 21:55:50 +0000340 const char *port = "ftp";
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000341 /* socket to ftp server */
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000342
Denis Vlasenko562dc242007-01-03 21:55:50 +0000343#if ENABLE_FTPPUT && !ENABLE_FTPGET
344# define ftp_action ftp_send
345#elif ENABLE_FTPGET && !ENABLE_FTPPUT
346# define ftp_action ftp_receive
347#else
Denis Vlasenko7cb808e2008-03-29 07:40:35 +0000348 int (*ftp_action)(const char *, char *) = ftp_send;
Denis Vlasenko0e7940a2008-03-29 07:37:42 +0000349
Glenn L McGrath266c1f52003-12-20 03:19:27 +0000350 /* Check to see if the command is ftpget or ftput */
Denis Vlasenko562dc242007-01-03 21:55:50 +0000351 if (applet_name[3] == 'g') {
Bernhard Reutner-Fischere0387a62006-06-07 13:31:59 +0000352 ftp_action = ftp_receive;
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000353 }
Denis Vlasenko562dc242007-01-03 21:55:50 +0000354#endif
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000355
Denis Vlasenko0e7940a2008-03-29 07:37:42 +0000356 INIT_G();
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000357 /* Set default values */
Denis Vlasenko0e7940a2008-03-29 07:37:42 +0000358 user = "anonymous";
359 password = "busybox@";
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000360
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000361 /*
362 * Decipher the command line
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000363 */
Denis Vlasenkoc61852a2006-11-29 11:09:43 +0000364#if ENABLE_FEATURE_FTPGETPUT_LONG_OPTIONS
Denis Vlasenkobdc88fd2007-07-23 17:14:14 +0000365 applet_long_options = ftpgetput_longopts;
Denis Vlasenkoc61852a2006-11-29 11:09:43 +0000366#endif
Vladimir Dronnikov1dacfbb2009-10-23 23:34:43 +0200367 opt_complementary = "-2:vv:cc"; /* must have 2 to 3 params; -v and -c count */
Denys Vlasenko60a94142011-05-13 20:57:01 +0200368 getopt32(argv, "cvu:p:P:", &user, &password, &port,
Denis Vlasenko0e7940a2008-03-29 07:37:42 +0000369 &verbose_flag, &do_continue);
Denis Vlasenko562dc242007-01-03 21:55:50 +0000370 argv += optind;
Glenn L McGrath236e93d2003-12-20 05:43:34 +0000371
Eric Andersen04d055f2003-11-03 21:20:18 +0000372 /* We want to do exactly _one_ DNS lookup, since some
373 * sites (i.e. ftp.us.debian.org) use round-robin DNS
374 * and we want to connect to only one IP... */
Denis Vlasenko0e7940a2008-03-29 07:37:42 +0000375 lsa = xhost2sockaddr(argv[0], bb_lookup_port(port, "tcp", 21));
Eric Andersen04d055f2003-11-03 21:20:18 +0000376 if (verbose_flag) {
Denis Vlasenko85629f02007-01-22 09:36:41 +0000377 printf("Connecting to %s (%s)\n", argv[0],
Denis Vlasenko0e7940a2008-03-29 07:37:42 +0000378 xmalloc_sockaddr2dotted(&lsa->u.sa));
Eric Andersen04d055f2003-11-03 21:20:18 +0000379 }
380
Denis Vlasenko7cb808e2008-03-29 07:40:35 +0000381 ftp_login();
Vladimir Dronnikov1dacfbb2009-10-23 23:34:43 +0200382 return ftp_action(argv[1], argv[2] ? argv[2] : argv[1]);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000383}