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;
24 char buf[1]; /* actually [BUF_SIZE] */
26 #define G (*(struct globals*)&bb_common_bufsiz1)
27 enum { BUFSZ = COMMON_BUFSIZE - offsetof(struct globals, buf) };
28 struct BUG_G_too_big {
29 char BUG_G_too_big[sizeof(G) <= COMMON_BUFSIZE ? 1 : -1];
31 #define user (G.user )
32 #define password (G.password )
34 #define verbose_flag (G.verbose_flag)
35 #define do_continue (G.do_continue )
37 #define INIT_G() do { \
41 static void ftp_die(const char *msg) ATTRIBUTE_NORETURN;
42 static void ftp_die(const char *msg)
44 const char *cp = buf; /* buf holds peer's response */
46 /* Guard against garbage from remote server */
47 while (*cp >= ' ' && *cp < '\x7f')
49 bb_error_msg_and_die("unexpected server response%s%s: %.*s",
50 msg ? " to " : "", msg ? msg : "",
51 (int)(cp - buf), buf);
54 static int ftpcmd(const char *s1, const char *s2, FILE *stream)
58 bb_error_msg("cmd %s %s", s1, s2);
62 fprintf(stream, (s2 ? "%s %s\r\n" : "%s %s\r\n"+3), s1, s2);
68 if (fgets(buf, BUFSZ - 2, stream) == NULL) {
69 bb_perror_msg_and_die("fgets");
71 buf_ptr = strstr(buf, "\r\n");
75 } while (!isdigit(buf[0]) || buf[3] != ' ');
83 static FILE *ftp_login(void)
87 /* Connect to the command socket */
88 control_stream = fdopen(xconnect_stream(lsa), "r+");
89 if (control_stream == NULL) {
90 /* fdopen failed - extremely unlikely */
91 bb_perror_nomsg_and_die();
94 if (ftpcmd(NULL, NULL, control_stream) != 220) {
98 /* Login to the server */
99 switch (ftpcmd("USER", user, control_stream)) {
103 if (ftpcmd("PASS", password, control_stream) != 230) {
111 ftpcmd("TYPE I", NULL, control_stream);
113 return control_stream;
116 static int xconnect_ftpdata(void)
121 /* Response is "NNN garbageN1,N2,N3,N4,P1,P2[)garbage]
122 * Server's IP is N1.N2.N3.N4 (we ignore it)
123 * Server's port for data connection is P1*256+P2 */
124 buf_ptr = strrchr(buf, ')');
125 if (buf_ptr) *buf_ptr = '\0';
127 buf_ptr = strrchr(buf, ',');
129 port_num = xatoul_range(buf_ptr + 1, 0, 255);
131 buf_ptr = strrchr(buf, ',');
133 port_num += xatoul_range(buf_ptr + 1, 0, 255) * 256;
135 set_nport(lsa, htons(port_num));
136 return xconnect_stream(lsa);
140 int ftp_receive(FILE *control_stream,
141 const char *local_path, char *server_path);
144 int ftp_receive(FILE *control_stream,
145 const char *local_path, char *server_path)
147 #define filesize ((off_t)-1)
153 TODO: PASV command will not work for IPv6. RFC2428 describes
154 IPv6-capable "extended PASV" - EPSV.
156 "EPSV [protocol]" asks server to bind to and listen on a data port
157 in specified protocol. Protocol is 1 for IPv4, 2 for IPv6.
158 If not specified, defaults to "same as used for control connection".
159 If server understood you, it should answer "229 <some text>(|||port|)"
160 where "|" are literal pipe chars and "port" is ASCII decimal port#.
162 There is also an IPv6-capable replacement for PORT (EPRT),
163 but we don't need that.
165 TODO: fold in sending of PASV/EPSV and parsing of response into
166 xconnect_ftpdata(). (Also, need to stop ignoring IP address in PASV
170 /* connect to the data socket */
171 if (ftpcmd("PASV", NULL, control_stream) != 227) {
174 fd_data = xconnect_ftpdata();
176 if (ftpcmd("SIZE", server_path, control_stream) != 213) {
180 if (LONE_DASH(local_path)) {
181 fd_local = STDOUT_FILENO;
187 /* lstat would be wrong here! */
188 if (stat(local_path, &sbuf) < 0) {
189 bb_perror_msg_and_die("stat");
191 if (sbuf.st_size > 0) {
192 beg_range = sbuf.st_size;
199 sprintf(buf, "REST %"OFF_FMT"d", beg_range);
200 if (ftpcmd(buf, NULL, control_stream) != 350) {
205 if (ftpcmd("RETR", server_path, control_stream) > 150) {
209 /* make local _after_ we know that remote file exists */
210 if (fd_local == -1) {
211 fd_local = xopen(local_path,
212 do_continue ? (O_APPEND | O_WRONLY)
213 : (O_CREAT | O_TRUNC | O_WRONLY)
217 // TODO: merge tail of ftp_receive and ftp_send starting from here
220 if (bb_copyfd_eof(fd_data, fd_local) == -1) {
221 /* error msg is already printed by bb_copyfd_eof */
225 /* close it all down */
227 if (ftpcmd(NULL, NULL, control_stream) != 226) {
230 ftpcmd("QUIT", NULL, control_stream);
237 int ftp_send(FILE *control_stream,
238 const char *server_path, char *local_path);
241 int ftp_send(FILE *control_stream,
242 const char *server_path, char *local_path)
248 /* connect to the data socket */
249 if (ftpcmd("PASV", NULL, control_stream) != 227) {
252 fd_data = xconnect_ftpdata();
254 /* get the local file */
255 fd_local = STDIN_FILENO;
256 if (NOT_LONE_DASH(local_path))
257 fd_local = xopen(local_path, O_RDONLY);
259 response = ftpcmd("STOR", server_path, control_stream);
268 /* transfer the file */
269 if (bb_copyfd_eof(fd_local, fd_data) == -1) {
270 /* error msg is already printed by bb_copyfd_eof */
274 /* close it all down */
276 if (ftpcmd(NULL, NULL, control_stream) != 226) {
279 ftpcmd("QUIT", NULL, control_stream);
285 #define FTPGETPUT_OPT_CONTINUE 1
286 #define FTPGETPUT_OPT_VERBOSE 2
287 #define FTPGETPUT_OPT_USER 4
288 #define FTPGETPUT_OPT_PASSWORD 8
289 #define FTPGETPUT_OPT_PORT 16
291 #if ENABLE_FEATURE_FTPGETPUT_LONG_OPTIONS
292 static const char ftpgetput_longopts[] ALIGN1 =
293 "continue\0" Required_argument "c"
294 "verbose\0" No_argument "v"
295 "username\0" Required_argument "u"
296 "password\0" Required_argument "p"
297 "port\0" Required_argument "P"
301 int ftpgetput_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
302 int ftpgetput_main(int argc ATTRIBUTE_UNUSED, char **argv)
305 const char *port = "ftp";
306 /* socket to ftp server */
307 FILE *control_stream;
309 #if ENABLE_FTPPUT && !ENABLE_FTPGET
310 # define ftp_action ftp_send
311 #elif ENABLE_FTPGET && !ENABLE_FTPPUT
312 # define ftp_action ftp_receive
314 int (*ftp_action)(FILE *, const char *, char *) = ftp_send;
316 /* Check to see if the command is ftpget or ftput */
317 if (applet_name[3] == 'g') {
318 ftp_action = ftp_receive;
323 /* Set default values */
325 password = "busybox@";
328 * Decipher the command line
330 #if ENABLE_FEATURE_FTPGETPUT_LONG_OPTIONS
331 applet_long_options = ftpgetput_longopts;
333 opt_complementary = "=3:vv:cc"; /* must have 3 params; -v and -c count */
334 opt = getopt32(argv, "cvu:p:P:", &user, &password, &port,
335 &verbose_flag, &do_continue);
338 /* We want to do exactly _one_ DNS lookup, since some
339 * sites (i.e. ftp.us.debian.org) use round-robin DNS
340 * and we want to connect to only one IP... */
341 lsa = xhost2sockaddr(argv[0], bb_lookup_port(port, "tcp", 21));
343 printf("Connecting to %s (%s)\n", argv[0],
344 xmalloc_sockaddr2dotted(&lsa->u.sa));
347 /* Connect/Setup/Configure the FTP session */
348 control_stream = ftp_login();
350 return ftp_action(control_stream, argv[1], argv[2]);