1 /* vi: set sw=4 ts=4: */
5 * Mini implementation of FTP to retrieve a remote file.
7 * Copyright (C) 2002 Jeff Angielski, The PTR Group <jeff@theptrgroup.com>
8 * Copyright (C) 2002 Glenn McGrath
10 * Based on wget.c by Chip Rosenthal Covad Communications
11 * <chip@laserlink.net>
13 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
21 struct len_and_sockaddr *lsa;
25 char buf[1]; /* actually [BUFSZ] */
27 #define G (*(struct globals*)&bb_common_bufsiz1)
28 enum { BUFSZ = COMMON_BUFSIZE - offsetof(struct globals, buf) };
29 struct BUG_G_too_big {
30 char BUG_G_too_big[sizeof(G) <= COMMON_BUFSIZE ? 1 : -1];
32 #define user (G.user )
33 #define password (G.password )
35 #define control_stream (G.control_stream)
36 #define verbose_flag (G.verbose_flag )
37 #define do_continue (G.do_continue )
39 #define INIT_G() do { \
43 static void ftp_die(const char *msg) ATTRIBUTE_NORETURN;
44 static void ftp_die(const char *msg)
46 char *cp = buf; /* buf holds peer's response */
48 /* Guard against garbage from remote server */
49 while (*cp >= ' ' && *cp < '\x7f')
52 bb_error_msg_and_die("unexpected server response%s%s: %s",
53 (msg ? " to " : ""), (msg ? msg : ""), buf);
56 static int ftpcmd(const char *s1, const char *s2)
61 bb_error_msg("cmd %s %s", s1, s2);
65 fprintf(control_stream, (s2 ? "%s %s\r\n" : "%s %s\r\n"+3),
67 fflush(control_stream);
72 if (fgets(buf, BUFSZ - 2, control_stream) == NULL) {
75 } while (!isdigit(buf[0]) || buf[3] != ' ');
83 static void ftp_login(void)
85 /* Connect to the command socket */
86 control_stream = fdopen(xconnect_stream(lsa), "r+");
87 if (control_stream == NULL) {
88 /* fdopen failed - extremely unlikely */
89 bb_perror_nomsg_and_die();
92 if (ftpcmd(NULL, NULL) != 220) {
96 /* Login to the server */
97 switch (ftpcmd("USER", user)) {
101 if (ftpcmd("PASS", password) != 230) {
109 ftpcmd("TYPE I", NULL);
112 static int xconnect_ftpdata(void)
118 TODO: PASV command will not work for IPv6. RFC2428 describes
119 IPv6-capable "extended PASV" - EPSV.
121 "EPSV [protocol]" asks server to bind to and listen on a data port
122 in specified protocol. Protocol is 1 for IPv4, 2 for IPv6.
123 If not specified, defaults to "same as used for control connection".
124 If server understood you, it should answer "229 <some text>(|||port|)"
125 where "|" are literal pipe chars and "port" is ASCII decimal port#.
127 There is also an IPv6-capable replacement for PORT (EPRT),
128 but we don't need that.
130 NB: PASV may still work for some servers even over IPv6.
131 For example, vsftp happily answers
132 "227 Entering Passive Mode (0,0,0,0,n,n)" and proceeds as usual.
134 TODO2: need to stop ignoring IP address in PASV response.
137 if (ftpcmd("PASV", NULL) != 227) {
141 /* Response is "NNN garbageN1,N2,N3,N4,P1,P2[)garbage]
142 * Server's IP is N1.N2.N3.N4 (we ignore it)
143 * Server's port for data connection is P1*256+P2 */
144 buf_ptr = strrchr(buf, ')');
145 if (buf_ptr) *buf_ptr = '\0';
147 buf_ptr = strrchr(buf, ',');
149 port_num = xatoul_range(buf_ptr + 1, 0, 255);
151 buf_ptr = strrchr(buf, ',');
153 port_num += xatoul_range(buf_ptr + 1, 0, 255) * 256;
155 set_nport(lsa, htons(port_num));
156 return xconnect_stream(lsa);
159 static int pump_data_and_QUIT(int from, int to)
162 if (bb_copyfd_eof(from, to) == -1) {
163 /* error msg is already printed by bb_copyfd_eof */
167 /* close data connection */
168 close(from); /* don't know which one is that, so we close both */
171 /* does server confirm that transfer is finished? */
172 if (ftpcmd(NULL, NULL) != 226) {
175 ftpcmd("QUIT", NULL);
181 int ftp_receive(const char *local_path, char *server_path);
184 int ftp_receive(const char *local_path, char *server_path)
190 /* connect to the data socket */
191 fd_data = xconnect_ftpdata();
193 if (ftpcmd("SIZE", server_path) != 213) {
197 if (LONE_DASH(local_path)) {
198 fd_local = STDOUT_FILENO;
204 /* lstat would be wrong here! */
205 if (stat(local_path, &sbuf) < 0) {
206 bb_perror_msg_and_die("stat");
208 if (sbuf.st_size > 0) {
209 beg_range = sbuf.st_size;
216 sprintf(buf, "REST %"OFF_FMT"d", beg_range);
217 if (ftpcmd(buf, NULL) != 350) {
222 if (ftpcmd("RETR", server_path) > 150) {
226 /* create local file _after_ we know that remote file exists */
227 if (fd_local == -1) {
228 fd_local = xopen(local_path,
229 do_continue ? (O_APPEND | O_WRONLY)
230 : (O_CREAT | O_TRUNC | O_WRONLY)
234 return pump_data_and_QUIT(fd_data, fd_local);
239 int ftp_send(const char *server_path, char *local_path);
242 int ftp_send(const char *server_path, char *local_path)
248 /* connect to the data socket */
249 fd_data = xconnect_ftpdata();
251 /* get the local file */
252 fd_local = STDIN_FILENO;
253 if (NOT_LONE_DASH(local_path))
254 fd_local = xopen(local_path, O_RDONLY);
256 response = ftpcmd("STOR", server_path);
265 return pump_data_and_QUIT(fd_local, fd_data);
269 #if ENABLE_FEATURE_FTPGETPUT_LONG_OPTIONS
270 static const char ftpgetput_longopts[] ALIGN1 =
271 "continue\0" Required_argument "c"
272 "verbose\0" No_argument "v"
273 "username\0" Required_argument "u"
274 "password\0" Required_argument "p"
275 "port\0" Required_argument "P"
279 int ftpgetput_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
280 int ftpgetput_main(int argc ATTRIBUTE_UNUSED, char **argv)
283 const char *port = "ftp";
284 /* socket to ftp server */
286 #if ENABLE_FTPPUT && !ENABLE_FTPGET
287 # define ftp_action ftp_send
288 #elif ENABLE_FTPGET && !ENABLE_FTPPUT
289 # define ftp_action ftp_receive
291 int (*ftp_action)(const char *, char *) = ftp_send;
293 /* Check to see if the command is ftpget or ftput */
294 if (applet_name[3] == 'g') {
295 ftp_action = ftp_receive;
300 /* Set default values */
302 password = "busybox@";
305 * Decipher the command line
307 #if ENABLE_FEATURE_FTPGETPUT_LONG_OPTIONS
308 applet_long_options = ftpgetput_longopts;
310 opt_complementary = "=3:vv:cc"; /* must have 3 params; -v and -c count */
311 opt = getopt32(argv, "cvu:p:P:", &user, &password, &port,
312 &verbose_flag, &do_continue);
315 /* We want to do exactly _one_ DNS lookup, since some
316 * sites (i.e. ftp.us.debian.org) use round-robin DNS
317 * and we want to connect to only one IP... */
318 lsa = xhost2sockaddr(argv[0], bb_lookup_port(port, "tcp", 21));
320 printf("Connecting to %s (%s)\n", argv[0],
321 xmalloc_sockaddr2dotted(&lsa->u.sa));
325 return ftp_action(argv[1], argv[2]);