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.
18 typedef struct ftp_host_info_s {
21 struct len_and_sockaddr *lsa;
24 static smallint verbose_flag;
25 static smallint do_continue;
27 static void ftp_die(const char *msg, const char *remote) ATTRIBUTE_NORETURN;
28 static void ftp_die(const char *msg, const char *remote)
30 /* Guard against garbage from remote server */
31 const char *cp = remote;
32 while (*cp >= ' ' && *cp < '\x7f') cp++;
33 bb_error_msg_and_die("unexpected server response%s%s: %.*s",
34 msg ? " to " : "", msg ? msg : "",
35 (int)(cp - remote), remote);
39 static int ftpcmd(const char *s1, const char *s2, FILE *stream, char *buf)
43 bb_error_msg("cmd %s %s", s1, s2);
48 fprintf(stream, "%s %s\r\n", s1, s2);
50 fprintf(stream, "%s\r\n", s1);
56 if (fgets(buf, 510, stream) == NULL) {
57 bb_perror_msg_and_die("fgets");
59 buf_ptr = strstr(buf, "\r\n");
63 } while (!isdigit(buf[0]) || buf[3] != ' ');
71 static int xconnect_ftpdata(ftp_host_info_t *server, char *buf)
76 /* Response is "NNN garbageN1,N2,N3,N4,P1,P2[)garbage]
77 * Server's IP is N1.N2.N3.N4 (we ignore it)
78 * Server's port for data connection is P1*256+P2 */
79 buf_ptr = strrchr(buf, ')');
80 if (buf_ptr) *buf_ptr = '\0';
82 buf_ptr = strrchr(buf, ',');
84 port_num = xatoul_range(buf_ptr + 1, 0, 255);
86 buf_ptr = strrchr(buf, ',');
88 port_num += xatoul_range(buf_ptr + 1, 0, 255) * 256;
90 set_nport(server->lsa, htons(port_num));
91 return xconnect_stream(server->lsa);
94 static FILE *ftp_login(ftp_host_info_t *server)
99 /* Connect to the command socket */
100 control_stream = fdopen(xconnect_stream(server->lsa), "r+");
101 if (control_stream == NULL) {
102 /* fdopen failed - extremely unlikely */
103 bb_perror_nomsg_and_die();
106 if (ftpcmd(NULL, NULL, control_stream, buf) != 220) {
110 /* Login to the server */
111 switch (ftpcmd("USER", server->user, control_stream, buf)) {
115 if (ftpcmd("PASS", server->password, control_stream, buf) != 230) {
116 ftp_die("PASS", buf);
120 ftp_die("USER", buf);
123 ftpcmd("TYPE I", NULL, control_stream, buf);
125 return control_stream;
129 int ftp_receive(ftp_host_info_t *server, FILE *control_stream,
130 const char *local_path, char *server_path);
133 int ftp_receive(ftp_host_info_t *server, FILE *control_stream,
134 const char *local_path, char *server_path)
137 /* I think 'filesize' usage here is bogus. Let's see... */
138 //off_t filesize = -1;
139 #define filesize ((off_t)-1)
144 /* Connect to the data socket */
145 if (ftpcmd("PASV", NULL, control_stream, buf) != 227) {
146 ftp_die("PASV", buf);
148 fd_data = xconnect_ftpdata(server, buf);
150 if (ftpcmd("SIZE", server_path, control_stream, buf) == 213) {
151 //filesize = BB_STRTOOFF(buf + 4, NULL, 10);
152 //if (errno || filesize < 0)
153 // ftp_die("SIZE", buf);
158 if (LONE_DASH(local_path)) {
159 fd_local = STDOUT_FILENO;
165 if (lstat(local_path, &sbuf) < 0) {
166 bb_perror_msg_and_die("lstat");
168 if (sbuf.st_size > 0) {
169 beg_range = sbuf.st_size;
176 sprintf(buf, "REST %"OFF_FMT"d", beg_range);
177 if (ftpcmd(buf, NULL, control_stream, buf) != 350) {
180 //if (filesize != -1)
181 // filesize -= beg_range;
185 if (ftpcmd("RETR", server_path, control_stream, buf) > 150) {
186 ftp_die("RETR", buf);
189 /* only make a local file if we know that one exists on the remote server */
190 if (fd_local == -1) {
192 fd_local = xopen(local_path, O_APPEND | O_WRONLY);
194 fd_local = xopen(local_path, O_CREAT | O_TRUNC | O_WRONLY);
199 if (filesize != -1) {
200 if (bb_copyfd_size(fd_data, fd_local, filesize) == -1)
203 if (bb_copyfd_eof(fd_data, fd_local) == -1)
207 /* close it all down */
209 if (ftpcmd(NULL, NULL, control_stream, buf) != 226) {
212 ftpcmd("QUIT", NULL, control_stream, buf);
219 int ftp_send(ftp_host_info_t *server, FILE *control_stream,
220 const char *server_path, char *local_path);
223 int ftp_send(ftp_host_info_t *server, FILE *control_stream,
224 const char *server_path, char *local_path)
232 /* Connect to the data socket */
233 if (ftpcmd("PASV", NULL, control_stream, buf) != 227) {
234 ftp_die("PASV", buf);
236 fd_data = xconnect_ftpdata(server, buf);
238 /* get the local file */
239 fd_local = STDIN_FILENO;
240 if (NOT_LONE_DASH(local_path)) {
241 fd_local = xopen(local_path, O_RDONLY);
242 fstat(fd_local, &sbuf);
244 sprintf(buf, "ALLO %"OFF_FMT"u", sbuf.st_size);
245 response = ftpcmd(buf, NULL, control_stream, buf);
252 ftp_die("ALLO", buf);
256 response = ftpcmd("STOR", server_path, control_stream, buf);
263 ftp_die("STOR", buf);
266 /* transfer the file */
267 if (bb_copyfd_eof(fd_local, fd_data) == -1) {
271 /* close it all down */
273 if (ftpcmd(NULL, NULL, control_stream, buf) != 226) {
274 ftp_die("close", buf);
276 ftpcmd("QUIT", NULL, control_stream, buf);
282 #define FTPGETPUT_OPT_CONTINUE 1
283 #define FTPGETPUT_OPT_VERBOSE 2
284 #define FTPGETPUT_OPT_USER 4
285 #define FTPGETPUT_OPT_PASSWORD 8
286 #define FTPGETPUT_OPT_PORT 16
288 #if ENABLE_FEATURE_FTPGETPUT_LONG_OPTIONS
289 static const char ftpgetput_longopts[] ALIGN1 =
290 "continue\0" Required_argument "c"
291 "verbose\0" No_argument "v"
292 "username\0" Required_argument "u"
293 "password\0" Required_argument "p"
294 "port\0" Required_argument "P"
298 int ftpgetput_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
299 int ftpgetput_main(int argc ATTRIBUTE_UNUSED, char **argv)
301 /* content-length of the file */
303 const char *port = "ftp";
304 /* socket to ftp server */
305 FILE *control_stream;
306 /* continue previous transfer (-c) */
307 ftp_host_info_t *server;
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)(ftp_host_info_t *, FILE *, const char *, char *) = ftp_send;
315 /* Check to see if the command is ftpget or ftput */
316 if (applet_name[3] == 'g') {
317 ftp_action = ftp_receive;
321 /* Set default values */
322 server = xmalloc(sizeof(*server));
323 server->user = "anonymous";
324 server->password = "busybox@";
327 * Decipher the command line
329 #if ENABLE_FEATURE_FTPGETPUT_LONG_OPTIONS
330 applet_long_options = ftpgetput_longopts;
332 opt_complementary = "=3"; /* must have 3 params */
333 opt = getopt32(argv, "cvu:p:P:", &server->user, &server->password, &port);
336 /* Process the non-option command line arguments */
337 if (opt & FTPGETPUT_OPT_CONTINUE) {
340 if (opt & FTPGETPUT_OPT_VERBOSE) {
344 /* We want to do exactly _one_ DNS lookup, since some
345 * sites (i.e. ftp.us.debian.org) use round-robin DNS
346 * and we want to connect to only one IP... */
347 server->lsa = xhost2sockaddr(argv[0], bb_lookup_port(port, "tcp", 21));
349 printf("Connecting to %s (%s)\n", argv[0],
350 xmalloc_sockaddr2dotted(&server->lsa->u.sa));
353 /* Connect/Setup/Configure the FTP session */
354 control_stream = ftp_login(server);
356 return ftp_action(server, control_stream, argv[1], argv[2]);