blob: 3f98e07f33b389b85e00a6ed16293ce28fd7826a [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
Denys Vlasenko72089cf2017-07-21 09:50:55 +020019//config: Retrieve a remote file via FTP.
Denys Vlasenko47367e12016-11-23 09:05:14 +010020//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
Denys Vlasenko72089cf2017-07-21 09:50:55 +020025//config: Store a remote file via FTP.
Denys Vlasenko47367e12016-11-23 09:05:14 +010026//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: "\n -c Continue previous transfer"
43//usage: "\n -v Verbose"
Denys Vlasenkocc1bb602012-03-19 12:22:57 +010044//usage: "\n -u USER Username"
45//usage: "\n -p PASS Password"
46//usage: "\n -P NUM Port"
Pere Orga5bc8c002011-04-11 03:29:49 +020047//usage:
48//usage:#define ftpput_trivial_usage
49//usage: "[OPTIONS] HOST [REMOTE_FILE] LOCAL_FILE"
50//usage:#define ftpput_full_usage "\n\n"
Denys Vlasenkocc1bb602012-03-19 12:22:57 +010051//usage: "Upload a file to a FTP server\n"
Pere Orga5bc8c002011-04-11 03:29:49 +020052//usage: "\n -v Verbose"
Denys Vlasenkocc1bb602012-03-19 12:22:57 +010053//usage: "\n -u USER Username"
54//usage: "\n -p PASS Password"
55//usage: "\n -P NUM Port number"
Pere Orga5bc8c002011-04-11 03:29:49 +020056
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000057#include "libbb.h"
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +020058#include "common_bufsiz.h"
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +000059
Denis Vlasenko0e7940a2008-03-29 07:37:42 +000060struct globals {
Denis Vlasenkob6aae0f2007-01-29 22:51:25 +000061 const char *user;
62 const char *password;
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +000063 struct len_and_sockaddr *lsa;
Denis Vlasenko7cb808e2008-03-29 07:40:35 +000064 FILE *control_stream;
Denis Vlasenko0e7940a2008-03-29 07:37:42 +000065 int verbose_flag;
66 int do_continue;
Denys Vlasenko60a94142011-05-13 20:57:01 +020067 char buf[4]; /* actually [BUFSZ] */
Denys Vlasenko98a4c7c2010-02-04 15:00:15 +010068} FIX_ALIASING;
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +020069#define G (*(struct globals*)bb_common_bufsiz1)
Denis Vlasenko0e7940a2008-03-29 07:37:42 +000070enum { BUFSZ = COMMON_BUFSIZE - offsetof(struct globals, buf) };
Denis Vlasenko7cb808e2008-03-29 07:40:35 +000071#define user (G.user )
72#define password (G.password )
73#define lsa (G.lsa )
74#define control_stream (G.control_stream)
75#define verbose_flag (G.verbose_flag )
76#define do_continue (G.do_continue )
77#define buf (G.buf )
Denys Vlasenkoab3964d2015-10-13 14:50:20 +020078#define INIT_G() do { \
Denys Vlasenko47cfbf32016-04-21 18:18:48 +020079 setup_common_bufsiz(); \
Denys Vlasenkoab3964d2015-10-13 14:50:20 +020080 BUILD_BUG_ON(sizeof(G) > COMMON_BUFSIZE); \
81} while (0)
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +000082
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +000083
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000084static void ftp_die(const char *msg) NORETURN;
Denis Vlasenko0e7940a2008-03-29 07:37:42 +000085static void ftp_die(const char *msg)
Denis Vlasenko562dc242007-01-03 21:55:50 +000086{
Denis Vlasenko7cb808e2008-03-29 07:40:35 +000087 char *cp = buf; /* buf holds peer's response */
Denis Vlasenko0e7940a2008-03-29 07:37:42 +000088
Denis Vlasenko562dc242007-01-03 21:55:50 +000089 /* Guard against garbage from remote server */
Denis Vlasenko0e7940a2008-03-29 07:37:42 +000090 while (*cp >= ' ' && *cp < '\x7f')
91 cp++;
Denis Vlasenko7cb808e2008-03-29 07:40:35 +000092 *cp = '\0';
93 bb_error_msg_and_die("unexpected server response%s%s: %s",
94 (msg ? " to " : ""), (msg ? msg : ""), buf);
Denis Vlasenko562dc242007-01-03 21:55:50 +000095}
96
Denis Vlasenko7cb808e2008-03-29 07:40:35 +000097static int ftpcmd(const char *s1, const char *s2)
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +000098{
Denis Vlasenko562dc242007-01-03 21:55:50 +000099 unsigned n;
Denis Vlasenko7cb808e2008-03-29 07:40:35 +0000100
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000101 if (verbose_flag) {
Denis Vlasenko3821fb12007-01-11 16:51:21 +0000102 bb_error_msg("cmd %s %s", s1, s2);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000103 }
104
105 if (s1) {
Denis Vlasenko7cb808e2008-03-29 07:40:35 +0000106 fprintf(control_stream, (s2 ? "%s %s\r\n" : "%s %s\r\n"+3),
107 s1, s2);
108 fflush(control_stream);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000109 }
Denis Vlasenko0e7940a2008-03-29 07:37:42 +0000110
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000111 do {
Denys Vlasenko60a94142011-05-13 20:57:01 +0200112 strcpy(buf, "EOF"); /* for ftp_die */
Denis Vlasenko7cb808e2008-03-29 07:40:35 +0000113 if (fgets(buf, BUFSZ - 2, control_stream) == NULL) {
114 ftp_die(NULL);
Glenn L McGrath5ec58282004-05-04 10:43:34 +0000115 }
Denis Vlasenko13858992006-10-08 12:49:22 +0000116 } while (!isdigit(buf[0]) || buf[3] != ' ');
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000117
Denis Vlasenko562dc242007-01-03 21:55:50 +0000118 buf[3] = '\0';
119 n = xatou(buf);
120 buf[3] = ' ';
121 return n;
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000122}
123
Denis Vlasenko7cb808e2008-03-29 07:40:35 +0000124static void ftp_login(void)
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000125{
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000126 /* Connect to the command socket */
Denis Vlasenko0e7940a2008-03-29 07:37:42 +0000127 control_stream = fdopen(xconnect_stream(lsa), "r+");
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000128 if (control_stream == NULL) {
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000129 /* fdopen failed - extremely unlikely */
Denis Vlasenko562dc242007-01-03 21:55:50 +0000130 bb_perror_nomsg_and_die();
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000131 }
132
Denis Vlasenko7cb808e2008-03-29 07:40:35 +0000133 if (ftpcmd(NULL, NULL) != 220) {
Denis Vlasenko0e7940a2008-03-29 07:37:42 +0000134 ftp_die(NULL);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000135 }
136
137 /* Login to the server */
Denis Vlasenko7cb808e2008-03-29 07:40:35 +0000138 switch (ftpcmd("USER", user)) {
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000139 case 230:
140 break;
141 case 331:
Denis Vlasenko7cb808e2008-03-29 07:40:35 +0000142 if (ftpcmd("PASS", password) != 230) {
Denis Vlasenko0e7940a2008-03-29 07:37:42 +0000143 ftp_die("PASS");
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000144 }
145 break;
146 default:
Denis Vlasenko0e7940a2008-03-29 07:37:42 +0000147 ftp_die("USER");
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000148 }
149
Denis Vlasenko7cb808e2008-03-29 07:40:35 +0000150 ftpcmd("TYPE I", NULL);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000151}
152
Denis Vlasenko0e7940a2008-03-29 07:37:42 +0000153static int xconnect_ftpdata(void)
Denis Vlasenko6c615a62008-03-28 22:11:49 +0000154{
155 char *buf_ptr;
156 unsigned port_num;
157
Denis Vlasenko7cb808e2008-03-29 07:40:35 +0000158/*
159TODO: PASV command will not work for IPv6. RFC2428 describes
160IPv6-capable "extended PASV" - EPSV.
161
162"EPSV [protocol]" asks server to bind to and listen on a data port
163in specified protocol. Protocol is 1 for IPv4, 2 for IPv6.
164If not specified, defaults to "same as used for control connection".
165If server understood you, it should answer "229 <some text>(|||port|)"
166where "|" are literal pipe chars and "port" is ASCII decimal port#.
167
168There is also an IPv6-capable replacement for PORT (EPRT),
169but we don't need that.
170
171NB: PASV may still work for some servers even over IPv6.
172For example, vsftp happily answers
173"227 Entering Passive Mode (0,0,0,0,n,n)" and proceeds as usual.
174
175TODO2: need to stop ignoring IP address in PASV response.
176*/
Denys Vlasenkodaa64322018-02-04 18:49:31 +0100177 //if (ftpcmd("EPSV", NULL) != 229) {
178 if (ftpcmd("PASV", NULL) != 227) {
179 ftp_die("PASV");
180 }
Denis Vlasenko7cb808e2008-03-29 07:40:35 +0000181
Denys Vlasenkodaa64322018-02-04 18:49:31 +0100182 /* Response is "NNN garbageN1,N2,N3,N4,P1,P2[)garbage]
183 * Server's IP is N1.N2.N3.N4 (we ignore it)
184 * Server's port for data connection is P1*256+P2 */
185 buf_ptr = strrchr(buf, ')');
186 if (buf_ptr) *buf_ptr = '\0';
Denis Vlasenko7cb808e2008-03-29 07:40:35 +0000187
Denys Vlasenkodaa64322018-02-04 18:49:31 +0100188 buf_ptr = strrchr(buf, ',');
189 *buf_ptr = '\0';
190 port_num = xatoul_range(buf_ptr + 1, 0, 255);
Denis Vlasenko6c615a62008-03-28 22:11:49 +0000191
Denys Vlasenkodaa64322018-02-04 18:49:31 +0100192 buf_ptr = strrchr(buf, ',');
193 *buf_ptr = '\0';
194 port_num += xatoul_range(buf_ptr + 1, 0, 255) * 256;
195 //}
Denis Vlasenko6c615a62008-03-28 22:11:49 +0000196
Denys Vlasenkoca183112011-04-07 17:52:20 +0200197 set_nport(&lsa->u.sa, htons(port_num));
Denis Vlasenko0e7940a2008-03-29 07:37:42 +0000198 return xconnect_stream(lsa);
Denis Vlasenko6c615a62008-03-28 22:11:49 +0000199}
200
Denis Vlasenko7cb808e2008-03-29 07:40:35 +0000201static int pump_data_and_QUIT(int from, int to)
202{
203 /* copy the file */
204 if (bb_copyfd_eof(from, to) == -1) {
205 /* error msg is already printed by bb_copyfd_eof */
206 return EXIT_FAILURE;
207 }
208
209 /* close data connection */
210 close(from); /* don't know which one is that, so we close both */
211 close(to);
212
213 /* does server confirm that transfer is finished? */
214 if (ftpcmd(NULL, NULL) != 226) {
215 ftp_die(NULL);
216 }
217 ftpcmd("QUIT", NULL);
218
219 return EXIT_SUCCESS;
220}
221
Rob Landleyb1c3fbc2006-05-04 19:52:28 +0000222#if !ENABLE_FTPGET
Denis Vlasenko7cb808e2008-03-29 07:40:35 +0000223int ftp_receive(const char *local_path, char *server_path);
Rob Landleyb1c3fbc2006-05-04 19:52:28 +0000224#else
Denis Vlasenko7039a662006-10-08 17:54:47 +0000225static
Denis Vlasenko7cb808e2008-03-29 07:40:35 +0000226int ftp_receive(const char *local_path, char *server_path)
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000227{
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000228 int fd_data;
Glenn L McGrath236e93d2003-12-20 05:43:34 +0000229 int fd_local = -1;
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000230 off_t beg_range = 0;
231
Denis Vlasenko6c615a62008-03-28 22:11:49 +0000232 /* connect to the data socket */
Denis Vlasenko0e7940a2008-03-29 07:37:42 +0000233 fd_data = xconnect_ftpdata();
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000234
Denis Vlasenko7cb808e2008-03-29 07:40:35 +0000235 if (ftpcmd("SIZE", server_path) != 213) {
Rob Landleybc059bc2006-01-10 06:36:00 +0000236 do_continue = 0;
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000237 }
238
Denis Vlasenko9f739442006-12-16 23:49:13 +0000239 if (LONE_DASH(local_path)) {
Eric Andersen70060d22004-03-27 10:02:48 +0000240 fd_local = STDOUT_FILENO;
Glenn L McGrath236e93d2003-12-20 05:43:34 +0000241 do_continue = 0;
242 }
243
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000244 if (do_continue) {
245 struct stat sbuf;
Denis Vlasenko6c615a62008-03-28 22:11:49 +0000246 /* lstat would be wrong here! */
247 if (stat(local_path, &sbuf) < 0) {
248 bb_perror_msg_and_die("stat");
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000249 }
250 if (sbuf.st_size > 0) {
251 beg_range = sbuf.st_size;
252 } else {
253 do_continue = 0;
254 }
255 }
256
257 if (do_continue) {
Denys Vlasenkoa3aa3e32009-12-11 12:36:10 +0100258 sprintf(buf, "REST %"OFF_FMT"u", beg_range);
Denis Vlasenko7cb808e2008-03-29 07:40:35 +0000259 if (ftpcmd(buf, NULL) != 350) {
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000260 do_continue = 0;
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000261 }
262 }
263
Denis Vlasenko7cb808e2008-03-29 07:40:35 +0000264 if (ftpcmd("RETR", server_path) > 150) {
Denis Vlasenko0e7940a2008-03-29 07:37:42 +0000265 ftp_die("RETR");
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000266 }
267
Denis Vlasenko7cb808e2008-03-29 07:40:35 +0000268 /* create local file _after_ we know that remote file exists */
Glenn L McGrath236e93d2003-12-20 05:43:34 +0000269 if (fd_local == -1) {
Denis Vlasenko6c615a62008-03-28 22:11:49 +0000270 fd_local = xopen(local_path,
271 do_continue ? (O_APPEND | O_WRONLY)
272 : (O_CREAT | O_TRUNC | O_WRONLY)
273 );
Glenn L McGrath1643f412002-12-18 02:47:40 +0000274 }
275
Denis Vlasenko7cb808e2008-03-29 07:40:35 +0000276 return pump_data_and_QUIT(fd_data, fd_local);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000277}
278#endif
279
Rob Landleyb1c3fbc2006-05-04 19:52:28 +0000280#if !ENABLE_FTPPUT
Denis Vlasenko7cb808e2008-03-29 07:40:35 +0000281int ftp_send(const char *server_path, char *local_path);
Rob Landleyb1c3fbc2006-05-04 19:52:28 +0000282#else
Denis Vlasenko7039a662006-10-08 17:54:47 +0000283static
Denis Vlasenko7cb808e2008-03-29 07:40:35 +0000284int ftp_send(const char *server_path, char *local_path)
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000285{
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000286 int fd_data;
287 int fd_local;
288 int response;
289
Denis Vlasenko6c615a62008-03-28 22:11:49 +0000290 /* connect to the data socket */
Denis Vlasenko0e7940a2008-03-29 07:37:42 +0000291 fd_data = xconnect_ftpdata();
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000292
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000293 /* get the local file */
Denis Vlasenko9f739442006-12-16 23:49:13 +0000294 fd_local = STDIN_FILENO;
Denis Vlasenko0e7940a2008-03-29 07:37:42 +0000295 if (NOT_LONE_DASH(local_path))
Rob Landleyd921b2e2006-08-03 15:41:12 +0000296 fd_local = xopen(local_path, O_RDONLY);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000297
Denis Vlasenko7cb808e2008-03-29 07:40:35 +0000298 response = ftpcmd("STOR", server_path);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000299 switch (response) {
300 case 125:
301 case 150:
302 break;
303 default:
Denis Vlasenko0e7940a2008-03-29 07:37:42 +0000304 ftp_die("STOR");
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000305 }
306
Denis Vlasenko7cb808e2008-03-29 07:40:35 +0000307 return pump_data_and_QUIT(fd_local, fd_data);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000308}
309#endif
310
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +0000311#if ENABLE_FEATURE_FTPGETPUT_LONG_OPTIONS
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000312static const char ftpgetput_longopts[] ALIGN1 =
Denis Vlasenkobdc88fd2007-07-23 17:14:14 +0000313 "continue\0" Required_argument "c"
314 "verbose\0" No_argument "v"
315 "username\0" Required_argument "u"
316 "password\0" Required_argument "p"
317 "port\0" Required_argument "P"
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000318 ;
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +0000319#endif
Glenn L McGrathb51eb262003-12-19 10:37:52 +0000320
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000321int ftpgetput_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000322int ftpgetput_main(int argc UNUSED_PARAM, char **argv)
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000323{
Denis Vlasenko562dc242007-01-03 21:55:50 +0000324 const char *port = "ftp";
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000325 /* socket to ftp server */
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000326
Denis Vlasenko562dc242007-01-03 21:55:50 +0000327#if ENABLE_FTPPUT && !ENABLE_FTPGET
328# define ftp_action ftp_send
329#elif ENABLE_FTPGET && !ENABLE_FTPPUT
330# define ftp_action ftp_receive
331#else
Denis Vlasenko7cb808e2008-03-29 07:40:35 +0000332 int (*ftp_action)(const char *, char *) = ftp_send;
Denis Vlasenko0e7940a2008-03-29 07:37:42 +0000333
Glenn L McGrath266c1f52003-12-20 03:19:27 +0000334 /* Check to see if the command is ftpget or ftput */
Denis Vlasenko562dc242007-01-03 21:55:50 +0000335 if (applet_name[3] == 'g') {
Bernhard Reutner-Fischere0387a62006-06-07 13:31:59 +0000336 ftp_action = ftp_receive;
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000337 }
Denis Vlasenko562dc242007-01-03 21:55:50 +0000338#endif
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000339
Denis Vlasenko0e7940a2008-03-29 07:37:42 +0000340 INIT_G();
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000341 /* Set default values */
Denis Vlasenko0e7940a2008-03-29 07:37:42 +0000342 user = "anonymous";
343 password = "busybox@";
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000344
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000345 /*
346 * Decipher the command line
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000347 */
Denys Vlasenko22542ec2017-08-08 21:55:02 +0200348 /* must have 2 to 3 params; -v and -c count */
349#define OPTSTRING "^cvu:p:P:" "\0" "-2:?3:vv:cc"
Denys Vlasenko036585a2017-08-08 16:38:18 +0200350#if ENABLE_FEATURE_FTPGETPUT_LONG_OPTIONS
Denys Vlasenko22542ec2017-08-08 21:55:02 +0200351 getopt32long(argv, OPTSTRING, ftpgetput_longopts,
Denys Vlasenko036585a2017-08-08 16:38:18 +0200352#else
Denys Vlasenko22542ec2017-08-08 21:55:02 +0200353 getopt32(argv, OPTSTRING,
Denys Vlasenko036585a2017-08-08 16:38:18 +0200354#endif
355 &user, &password, &port, &verbose_flag, &do_continue
356 );
Denis Vlasenko562dc242007-01-03 21:55:50 +0000357 argv += optind;
Glenn L McGrath236e93d2003-12-20 05:43:34 +0000358
Eric Andersen04d055f2003-11-03 21:20:18 +0000359 /* We want to do exactly _one_ DNS lookup, since some
360 * sites (i.e. ftp.us.debian.org) use round-robin DNS
361 * and we want to connect to only one IP... */
Denis Vlasenko0e7940a2008-03-29 07:37:42 +0000362 lsa = xhost2sockaddr(argv[0], bb_lookup_port(port, "tcp", 21));
Eric Andersen04d055f2003-11-03 21:20:18 +0000363 if (verbose_flag) {
Denis Vlasenko85629f02007-01-22 09:36:41 +0000364 printf("Connecting to %s (%s)\n", argv[0],
Denis Vlasenko0e7940a2008-03-29 07:37:42 +0000365 xmalloc_sockaddr2dotted(&lsa->u.sa));
Eric Andersen04d055f2003-11-03 21:20:18 +0000366 }
367
Denis Vlasenko7cb808e2008-03-29 07:40:35 +0000368 ftp_login();
Vladimir Dronnikov1dacfbb2009-10-23 23:34:43 +0200369 return ftp_action(argv[1], argv[2] ? argv[2] : argv[1]);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000370}