blob: e866a7754ac72127470358613d37842a21381bdd [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*/
177
178 if (ftpcmd("PASV", NULL) != 227) {
179 ftp_die("PASV");
180 }
181
Denis Vlasenko6c615a62008-03-28 22:11:49 +0000182 /* 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';
187
188 buf_ptr = strrchr(buf, ',');
189 *buf_ptr = '\0';
190 port_num = xatoul_range(buf_ptr + 1, 0, 255);
191
192 buf_ptr = strrchr(buf, ',');
193 *buf_ptr = '\0';
194 port_num += xatoul_range(buf_ptr + 1, 0, 255) * 256;
195
Denys Vlasenkoca183112011-04-07 17:52:20 +0200196 set_nport(&lsa->u.sa, htons(port_num));
Denis Vlasenko0e7940a2008-03-29 07:37:42 +0000197 return xconnect_stream(lsa);
Denis Vlasenko6c615a62008-03-28 22:11:49 +0000198}
199
Denis Vlasenko7cb808e2008-03-29 07:40:35 +0000200static int pump_data_and_QUIT(int from, int to)
201{
202 /* copy the file */
203 if (bb_copyfd_eof(from, to) == -1) {
204 /* error msg is already printed by bb_copyfd_eof */
205 return EXIT_FAILURE;
206 }
207
208 /* close data connection */
209 close(from); /* don't know which one is that, so we close both */
210 close(to);
211
212 /* does server confirm that transfer is finished? */
213 if (ftpcmd(NULL, NULL) != 226) {
214 ftp_die(NULL);
215 }
216 ftpcmd("QUIT", NULL);
217
218 return EXIT_SUCCESS;
219}
220
Rob Landleyb1c3fbc2006-05-04 19:52:28 +0000221#if !ENABLE_FTPGET
Denis Vlasenko7cb808e2008-03-29 07:40:35 +0000222int ftp_receive(const char *local_path, char *server_path);
Rob Landleyb1c3fbc2006-05-04 19:52:28 +0000223#else
Denis Vlasenko7039a662006-10-08 17:54:47 +0000224static
Denis Vlasenko7cb808e2008-03-29 07:40:35 +0000225int ftp_receive(const char *local_path, char *server_path)
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000226{
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000227 int fd_data;
Glenn L McGrath236e93d2003-12-20 05:43:34 +0000228 int fd_local = -1;
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000229 off_t beg_range = 0;
230
Denis Vlasenko6c615a62008-03-28 22:11:49 +0000231 /* connect to the data socket */
Denis Vlasenko0e7940a2008-03-29 07:37:42 +0000232 fd_data = xconnect_ftpdata();
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000233
Denis Vlasenko7cb808e2008-03-29 07:40:35 +0000234 if (ftpcmd("SIZE", server_path) != 213) {
Rob Landleybc059bc2006-01-10 06:36:00 +0000235 do_continue = 0;
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000236 }
237
Denis Vlasenko9f739442006-12-16 23:49:13 +0000238 if (LONE_DASH(local_path)) {
Eric Andersen70060d22004-03-27 10:02:48 +0000239 fd_local = STDOUT_FILENO;
Glenn L McGrath236e93d2003-12-20 05:43:34 +0000240 do_continue = 0;
241 }
242
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000243 if (do_continue) {
244 struct stat sbuf;
Denis Vlasenko6c615a62008-03-28 22:11:49 +0000245 /* lstat would be wrong here! */
246 if (stat(local_path, &sbuf) < 0) {
247 bb_perror_msg_and_die("stat");
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000248 }
249 if (sbuf.st_size > 0) {
250 beg_range = sbuf.st_size;
251 } else {
252 do_continue = 0;
253 }
254 }
255
256 if (do_continue) {
Denys Vlasenkoa3aa3e32009-12-11 12:36:10 +0100257 sprintf(buf, "REST %"OFF_FMT"u", beg_range);
Denis Vlasenko7cb808e2008-03-29 07:40:35 +0000258 if (ftpcmd(buf, NULL) != 350) {
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000259 do_continue = 0;
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000260 }
261 }
262
Denis Vlasenko7cb808e2008-03-29 07:40:35 +0000263 if (ftpcmd("RETR", server_path) > 150) {
Denis Vlasenko0e7940a2008-03-29 07:37:42 +0000264 ftp_die("RETR");
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000265 }
266
Denis Vlasenko7cb808e2008-03-29 07:40:35 +0000267 /* create local file _after_ we know that remote file exists */
Glenn L McGrath236e93d2003-12-20 05:43:34 +0000268 if (fd_local == -1) {
Denis Vlasenko6c615a62008-03-28 22:11:49 +0000269 fd_local = xopen(local_path,
270 do_continue ? (O_APPEND | O_WRONLY)
271 : (O_CREAT | O_TRUNC | O_WRONLY)
272 );
Glenn L McGrath1643f412002-12-18 02:47:40 +0000273 }
274
Denis Vlasenko7cb808e2008-03-29 07:40:35 +0000275 return pump_data_and_QUIT(fd_data, fd_local);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000276}
277#endif
278
Rob Landleyb1c3fbc2006-05-04 19:52:28 +0000279#if !ENABLE_FTPPUT
Denis Vlasenko7cb808e2008-03-29 07:40:35 +0000280int ftp_send(const char *server_path, char *local_path);
Rob Landleyb1c3fbc2006-05-04 19:52:28 +0000281#else
Denis Vlasenko7039a662006-10-08 17:54:47 +0000282static
Denis Vlasenko7cb808e2008-03-29 07:40:35 +0000283int ftp_send(const char *server_path, char *local_path)
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000284{
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000285 int fd_data;
286 int fd_local;
287 int response;
288
Denis Vlasenko6c615a62008-03-28 22:11:49 +0000289 /* connect to the data socket */
Denis Vlasenko0e7940a2008-03-29 07:37:42 +0000290 fd_data = xconnect_ftpdata();
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000291
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000292 /* get the local file */
Denis Vlasenko9f739442006-12-16 23:49:13 +0000293 fd_local = STDIN_FILENO;
Denis Vlasenko0e7940a2008-03-29 07:37:42 +0000294 if (NOT_LONE_DASH(local_path))
Rob Landleyd921b2e2006-08-03 15:41:12 +0000295 fd_local = xopen(local_path, O_RDONLY);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000296
Denis Vlasenko7cb808e2008-03-29 07:40:35 +0000297 response = ftpcmd("STOR", server_path);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000298 switch (response) {
299 case 125:
300 case 150:
301 break;
302 default:
Denis Vlasenko0e7940a2008-03-29 07:37:42 +0000303 ftp_die("STOR");
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000304 }
305
Denis Vlasenko7cb808e2008-03-29 07:40:35 +0000306 return pump_data_and_QUIT(fd_local, fd_data);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000307}
308#endif
309
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +0000310#if ENABLE_FEATURE_FTPGETPUT_LONG_OPTIONS
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000311static const char ftpgetput_longopts[] ALIGN1 =
Denis Vlasenkobdc88fd2007-07-23 17:14:14 +0000312 "continue\0" Required_argument "c"
313 "verbose\0" No_argument "v"
314 "username\0" Required_argument "u"
315 "password\0" Required_argument "p"
316 "port\0" Required_argument "P"
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000317 ;
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +0000318#endif
Glenn L McGrathb51eb262003-12-19 10:37:52 +0000319
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000320int ftpgetput_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000321int ftpgetput_main(int argc UNUSED_PARAM, char **argv)
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000322{
Denis Vlasenko562dc242007-01-03 21:55:50 +0000323 const char *port = "ftp";
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000324 /* socket to ftp server */
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000325
Denis Vlasenko562dc242007-01-03 21:55:50 +0000326#if ENABLE_FTPPUT && !ENABLE_FTPGET
327# define ftp_action ftp_send
328#elif ENABLE_FTPGET && !ENABLE_FTPPUT
329# define ftp_action ftp_receive
330#else
Denis Vlasenko7cb808e2008-03-29 07:40:35 +0000331 int (*ftp_action)(const char *, char *) = ftp_send;
Denis Vlasenko0e7940a2008-03-29 07:37:42 +0000332
Glenn L McGrath266c1f52003-12-20 03:19:27 +0000333 /* Check to see if the command is ftpget or ftput */
Denis Vlasenko562dc242007-01-03 21:55:50 +0000334 if (applet_name[3] == 'g') {
Bernhard Reutner-Fischere0387a62006-06-07 13:31:59 +0000335 ftp_action = ftp_receive;
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000336 }
Denis Vlasenko562dc242007-01-03 21:55:50 +0000337#endif
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000338
Denis Vlasenko0e7940a2008-03-29 07:37:42 +0000339 INIT_G();
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000340 /* Set default values */
Denis Vlasenko0e7940a2008-03-29 07:37:42 +0000341 user = "anonymous";
342 password = "busybox@";
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000343
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000344 /*
345 * Decipher the command line
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000346 */
Denys Vlasenko22542ec2017-08-08 21:55:02 +0200347 /* must have 2 to 3 params; -v and -c count */
348#define OPTSTRING "^cvu:p:P:" "\0" "-2:?3:vv:cc"
Denys Vlasenko036585a2017-08-08 16:38:18 +0200349#if ENABLE_FEATURE_FTPGETPUT_LONG_OPTIONS
Denys Vlasenko22542ec2017-08-08 21:55:02 +0200350 getopt32long(argv, OPTSTRING, ftpgetput_longopts,
Denys Vlasenko036585a2017-08-08 16:38:18 +0200351#else
Denys Vlasenko22542ec2017-08-08 21:55:02 +0200352 getopt32(argv, OPTSTRING,
Denys Vlasenko036585a2017-08-08 16:38:18 +0200353#endif
354 &user, &password, &port, &verbose_flag, &do_continue
355 );
Denis Vlasenko562dc242007-01-03 21:55:50 +0000356 argv += optind;
Glenn L McGrath236e93d2003-12-20 05:43:34 +0000357
Eric Andersen04d055f2003-11-03 21:20:18 +0000358 /* We want to do exactly _one_ DNS lookup, since some
359 * sites (i.e. ftp.us.debian.org) use round-robin DNS
360 * and we want to connect to only one IP... */
Denis Vlasenko0e7940a2008-03-29 07:37:42 +0000361 lsa = xhost2sockaddr(argv[0], bb_lookup_port(port, "tcp", 21));
Eric Andersen04d055f2003-11-03 21:20:18 +0000362 if (verbose_flag) {
Denis Vlasenko85629f02007-01-22 09:36:41 +0000363 printf("Connecting to %s (%s)\n", argv[0],
Denis Vlasenko0e7940a2008-03-29 07:37:42 +0000364 xmalloc_sockaddr2dotted(&lsa->u.sa));
Eric Andersen04d055f2003-11-03 21:20:18 +0000365 }
366
Denis Vlasenko7cb808e2008-03-29 07:40:35 +0000367 ftp_login();
Vladimir Dronnikov1dacfbb2009-10-23 23:34:43 +0200368 return ftp_action(argv[1], argv[2] ? argv[2] : argv[1]);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000369}