blob: 697955e823b46ef735ef188fd7a0b7626d9a7e83 [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/*
Denys Vlasenko05b84d42018-02-04 19:08:34 +0100159PASV command will not work for IPv6. RFC2428 describes
Denis Vlasenko7cb808e2008-03-29 07:40:35 +0000160IPv6-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.
Denis Vlasenko7cb808e2008-03-29 07:40:35 +0000174*/
Denys Vlasenko05b84d42018-02-04 19:08:34 +0100175 if (!ENABLE_FEATURE_IPV6
176 || ftpcmd("EPSV", NULL) != 229
177 ) {
178/* maybe also go straight to PAST if lsa->u.sa.sa_family == AF_INET? */
Denys Vlasenkodaa64322018-02-04 18:49:31 +0100179 if (ftpcmd("PASV", NULL) != 227) {
180 ftp_die("PASV");
181 }
Denis Vlasenko7cb808e2008-03-29 07:40:35 +0000182
Denys Vlasenko05b84d42018-02-04 19:08:34 +0100183 /* Response is "NNN garbageN1,N2,N3,N4,P1,P2[)garbage]"
Denys Vlasenkodaa64322018-02-04 18:49:31 +0100184 * Server's IP is N1.N2.N3.N4 (we ignore it)
185 * Server's port for data connection is P1*256+P2 */
186 buf_ptr = strrchr(buf, ')');
187 if (buf_ptr) *buf_ptr = '\0';
Denis Vlasenko7cb808e2008-03-29 07:40:35 +0000188
Denys Vlasenkodaa64322018-02-04 18:49:31 +0100189 buf_ptr = strrchr(buf, ',');
190 *buf_ptr = '\0';
191 port_num = xatoul_range(buf_ptr + 1, 0, 255);
Denis Vlasenko6c615a62008-03-28 22:11:49 +0000192
Denys Vlasenkodaa64322018-02-04 18:49:31 +0100193 buf_ptr = strrchr(buf, ',');
194 *buf_ptr = '\0';
195 port_num += xatoul_range(buf_ptr + 1, 0, 255) * 256;
Denys Vlasenko05b84d42018-02-04 19:08:34 +0100196 } else {
197 /* Response is "NNN garbage(|||P1|)"
198 * Server's port for data connection is P1 */
199 buf_ptr = strrchr(buf, '|');
200 if (buf_ptr) *buf_ptr = '\0';
201
202 buf_ptr = strrchr(buf, '|');
203 *buf_ptr = '\0';
204 port_num = xatoul_range(buf_ptr + 1, 0, 65535);
205 }
Denis Vlasenko6c615a62008-03-28 22:11:49 +0000206
Denys Vlasenkoca183112011-04-07 17:52:20 +0200207 set_nport(&lsa->u.sa, htons(port_num));
Denis Vlasenko0e7940a2008-03-29 07:37:42 +0000208 return xconnect_stream(lsa);
Denis Vlasenko6c615a62008-03-28 22:11:49 +0000209}
210
Denis Vlasenko7cb808e2008-03-29 07:40:35 +0000211static int pump_data_and_QUIT(int from, int to)
212{
213 /* copy the file */
214 if (bb_copyfd_eof(from, to) == -1) {
215 /* error msg is already printed by bb_copyfd_eof */
216 return EXIT_FAILURE;
217 }
218
219 /* close data connection */
220 close(from); /* don't know which one is that, so we close both */
221 close(to);
222
223 /* does server confirm that transfer is finished? */
224 if (ftpcmd(NULL, NULL) != 226) {
225 ftp_die(NULL);
226 }
227 ftpcmd("QUIT", NULL);
228
229 return EXIT_SUCCESS;
230}
231
Rob Landleyb1c3fbc2006-05-04 19:52:28 +0000232#if !ENABLE_FTPGET
Denis Vlasenko7cb808e2008-03-29 07:40:35 +0000233int ftp_receive(const char *local_path, char *server_path);
Rob Landleyb1c3fbc2006-05-04 19:52:28 +0000234#else
Denis Vlasenko7039a662006-10-08 17:54:47 +0000235static
Denis Vlasenko7cb808e2008-03-29 07:40:35 +0000236int ftp_receive(const char *local_path, char *server_path)
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000237{
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000238 int fd_data;
Glenn L McGrath236e93d2003-12-20 05:43:34 +0000239 int fd_local = -1;
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000240 off_t beg_range = 0;
241
Denis Vlasenko6c615a62008-03-28 22:11:49 +0000242 /* connect to the data socket */
Denis Vlasenko0e7940a2008-03-29 07:37:42 +0000243 fd_data = xconnect_ftpdata();
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000244
Denis Vlasenko7cb808e2008-03-29 07:40:35 +0000245 if (ftpcmd("SIZE", server_path) != 213) {
Rob Landleybc059bc2006-01-10 06:36:00 +0000246 do_continue = 0;
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000247 }
248
Denis Vlasenko9f739442006-12-16 23:49:13 +0000249 if (LONE_DASH(local_path)) {
Eric Andersen70060d22004-03-27 10:02:48 +0000250 fd_local = STDOUT_FILENO;
Glenn L McGrath236e93d2003-12-20 05:43:34 +0000251 do_continue = 0;
252 }
253
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000254 if (do_continue) {
255 struct stat sbuf;
Denis Vlasenko6c615a62008-03-28 22:11:49 +0000256 /* lstat would be wrong here! */
257 if (stat(local_path, &sbuf) < 0) {
258 bb_perror_msg_and_die("stat");
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000259 }
260 if (sbuf.st_size > 0) {
261 beg_range = sbuf.st_size;
262 } else {
263 do_continue = 0;
264 }
265 }
266
267 if (do_continue) {
Denys Vlasenkoa3aa3e32009-12-11 12:36:10 +0100268 sprintf(buf, "REST %"OFF_FMT"u", beg_range);
Denis Vlasenko7cb808e2008-03-29 07:40:35 +0000269 if (ftpcmd(buf, NULL) != 350) {
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000270 do_continue = 0;
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000271 }
272 }
273
Denis Vlasenko7cb808e2008-03-29 07:40:35 +0000274 if (ftpcmd("RETR", server_path) > 150) {
Denis Vlasenko0e7940a2008-03-29 07:37:42 +0000275 ftp_die("RETR");
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000276 }
277
Denis Vlasenko7cb808e2008-03-29 07:40:35 +0000278 /* create local file _after_ we know that remote file exists */
Glenn L McGrath236e93d2003-12-20 05:43:34 +0000279 if (fd_local == -1) {
Denis Vlasenko6c615a62008-03-28 22:11:49 +0000280 fd_local = xopen(local_path,
281 do_continue ? (O_APPEND | O_WRONLY)
282 : (O_CREAT | O_TRUNC | O_WRONLY)
283 );
Glenn L McGrath1643f412002-12-18 02:47:40 +0000284 }
285
Denis Vlasenko7cb808e2008-03-29 07:40:35 +0000286 return pump_data_and_QUIT(fd_data, fd_local);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000287}
288#endif
289
Rob Landleyb1c3fbc2006-05-04 19:52:28 +0000290#if !ENABLE_FTPPUT
Denis Vlasenko7cb808e2008-03-29 07:40:35 +0000291int ftp_send(const char *server_path, char *local_path);
Rob Landleyb1c3fbc2006-05-04 19:52:28 +0000292#else
Denis Vlasenko7039a662006-10-08 17:54:47 +0000293static
Denis Vlasenko7cb808e2008-03-29 07:40:35 +0000294int ftp_send(const char *server_path, char *local_path)
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000295{
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000296 int fd_data;
297 int fd_local;
298 int response;
299
Denis Vlasenko6c615a62008-03-28 22:11:49 +0000300 /* connect to the data socket */
Denis Vlasenko0e7940a2008-03-29 07:37:42 +0000301 fd_data = xconnect_ftpdata();
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000302
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000303 /* get the local file */
Denis Vlasenko9f739442006-12-16 23:49:13 +0000304 fd_local = STDIN_FILENO;
Denis Vlasenko0e7940a2008-03-29 07:37:42 +0000305 if (NOT_LONE_DASH(local_path))
Rob Landleyd921b2e2006-08-03 15:41:12 +0000306 fd_local = xopen(local_path, O_RDONLY);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000307
Denis Vlasenko7cb808e2008-03-29 07:40:35 +0000308 response = ftpcmd("STOR", server_path);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000309 switch (response) {
310 case 125:
311 case 150:
312 break;
313 default:
Denis Vlasenko0e7940a2008-03-29 07:37:42 +0000314 ftp_die("STOR");
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000315 }
316
Denis Vlasenko7cb808e2008-03-29 07:40:35 +0000317 return pump_data_and_QUIT(fd_local, fd_data);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000318}
319#endif
320
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +0000321#if ENABLE_FEATURE_FTPGETPUT_LONG_OPTIONS
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000322static const char ftpgetput_longopts[] ALIGN1 =
Denis Vlasenkobdc88fd2007-07-23 17:14:14 +0000323 "continue\0" Required_argument "c"
324 "verbose\0" No_argument "v"
325 "username\0" Required_argument "u"
326 "password\0" Required_argument "p"
327 "port\0" Required_argument "P"
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000328 ;
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +0000329#endif
Glenn L McGrathb51eb262003-12-19 10:37:52 +0000330
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000331int ftpgetput_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000332int ftpgetput_main(int argc UNUSED_PARAM, char **argv)
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000333{
Denis Vlasenko562dc242007-01-03 21:55:50 +0000334 const char *port = "ftp";
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000335 /* socket to ftp server */
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000336
Denis Vlasenko562dc242007-01-03 21:55:50 +0000337#if ENABLE_FTPPUT && !ENABLE_FTPGET
338# define ftp_action ftp_send
339#elif ENABLE_FTPGET && !ENABLE_FTPPUT
340# define ftp_action ftp_receive
341#else
Denis Vlasenko7cb808e2008-03-29 07:40:35 +0000342 int (*ftp_action)(const char *, char *) = ftp_send;
Denis Vlasenko0e7940a2008-03-29 07:37:42 +0000343
Glenn L McGrath266c1f52003-12-20 03:19:27 +0000344 /* Check to see if the command is ftpget or ftput */
Denis Vlasenko562dc242007-01-03 21:55:50 +0000345 if (applet_name[3] == 'g') {
Bernhard Reutner-Fischere0387a62006-06-07 13:31:59 +0000346 ftp_action = ftp_receive;
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000347 }
Denis Vlasenko562dc242007-01-03 21:55:50 +0000348#endif
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000349
Denis Vlasenko0e7940a2008-03-29 07:37:42 +0000350 INIT_G();
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000351 /* Set default values */
Denis Vlasenko0e7940a2008-03-29 07:37:42 +0000352 user = "anonymous";
353 password = "busybox@";
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000354
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000355 /*
356 * Decipher the command line
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000357 */
Denys Vlasenko22542ec2017-08-08 21:55:02 +0200358 /* must have 2 to 3 params; -v and -c count */
359#define OPTSTRING "^cvu:p:P:" "\0" "-2:?3:vv:cc"
Denys Vlasenko036585a2017-08-08 16:38:18 +0200360#if ENABLE_FEATURE_FTPGETPUT_LONG_OPTIONS
Denys Vlasenko22542ec2017-08-08 21:55:02 +0200361 getopt32long(argv, OPTSTRING, ftpgetput_longopts,
Denys Vlasenko036585a2017-08-08 16:38:18 +0200362#else
Denys Vlasenko22542ec2017-08-08 21:55:02 +0200363 getopt32(argv, OPTSTRING,
Denys Vlasenko036585a2017-08-08 16:38:18 +0200364#endif
365 &user, &password, &port, &verbose_flag, &do_continue
366 );
Denis Vlasenko562dc242007-01-03 21:55:50 +0000367 argv += optind;
Glenn L McGrath236e93d2003-12-20 05:43:34 +0000368
Eric Andersen04d055f2003-11-03 21:20:18 +0000369 /* We want to do exactly _one_ DNS lookup, since some
370 * sites (i.e. ftp.us.debian.org) use round-robin DNS
371 * and we want to connect to only one IP... */
Denis Vlasenko0e7940a2008-03-29 07:37:42 +0000372 lsa = xhost2sockaddr(argv[0], bb_lookup_port(port, "tcp", 21));
Eric Andersen04d055f2003-11-03 21:20:18 +0000373 if (verbose_flag) {
Denis Vlasenko85629f02007-01-22 09:36:41 +0000374 printf("Connecting to %s (%s)\n", argv[0],
Denis Vlasenko0e7940a2008-03-29 07:37:42 +0000375 xmalloc_sockaddr2dotted(&lsa->u.sa));
Eric Andersen04d055f2003-11-03 21:20:18 +0000376 }
377
Denis Vlasenko7cb808e2008-03-29 07:40:35 +0000378 ftp_login();
Vladimir Dronnikov1dacfbb2009-10-23 23:34:43 +0200379 return ftp_action(argv[1], argv[2] ? argv[2] : argv[1]);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000380}