blob: 9f3c789766730c29c8d7a4c7cb9e79323ffe6d0b [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>
Glenn L McGrathc6992fe2004-04-25 05:11:19 +00008 * Copyright (C) 2002 Glenn McGrath <bug1@iinet.net.au>
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +00009 *
10 * Based on wget.c by Chip Rosenthal Covad Communications
11 * <chip@laserlink.net>
12 *
Rob Landley6f037222005-11-08 00:52:31 +000013 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +000014 */
15
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +000016#include <sys/ioctl.h>
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +000017
18#include <ctype.h>
19#include <errno.h>
20#include <fcntl.h>
21#include <getopt.h>
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +000022#include <signal.h>
23#include <string.h>
24#include <unistd.h>
25
Eric Andersen04d055f2003-11-03 21:20:18 +000026#include <sys/socket.h>
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +000027
28#include "busybox.h"
29
30typedef struct ftp_host_info_s {
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +000031 char *user;
32 char *password;
Eric Andersen04d055f2003-11-03 21:20:18 +000033 struct sockaddr_in *s_in;
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +000034} ftp_host_info_t;
35
Glenn L McGrath266c1f52003-12-20 03:19:27 +000036static char verbose_flag = 0;
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +000037static char do_continue = 0;
38
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +000039static int ftpcmd(const char *s1, const char *s2, FILE *stream, char *buf)
40{
41 if (verbose_flag) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000042 bb_error_msg("cmd %s%s", s1, s2);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +000043 }
44
45 if (s1) {
46 if (s2) {
Paul Fox146e83a2005-07-19 21:26:57 +000047 fprintf(stream, "%s%s\r\n", s1, s2);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +000048 } else {
Paul Fox146e83a2005-07-19 21:26:57 +000049 fprintf(stream, "%s\r\n", s1);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +000050 }
51 }
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +000052 do {
Glenn L McGrath5ec58282004-05-04 10:43:34 +000053 char *buf_ptr;
54
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +000055 if (fgets(buf, 510, stream) == NULL) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000056 bb_perror_msg_and_die("fgets()");
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +000057 }
Glenn L McGrath5ec58282004-05-04 10:43:34 +000058 buf_ptr = strstr(buf, "\r\n");
59 if (buf_ptr) {
60 *buf_ptr = '\0';
61 }
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +000062 } while (! isdigit(buf[0]) || buf[3] != ' ');
63
64 return atoi(buf);
65}
66
Eric Andersen04d055f2003-11-03 21:20:18 +000067static int xconnect_ftpdata(ftp_host_info_t *server, const char *buf)
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +000068{
69 char *buf_ptr;
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +000070 unsigned short port_num;
71
72 buf_ptr = strrchr(buf, ',');
73 *buf_ptr = '\0';
74 port_num = atoi(buf_ptr + 1);
75
76 buf_ptr = strrchr(buf, ',');
77 *buf_ptr = '\0';
78 port_num += atoi(buf_ptr + 1) * 256;
79
Eric Andersen04d055f2003-11-03 21:20:18 +000080 server->s_in->sin_port=htons(port_num);
81 return(xconnect(server->s_in));
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +000082}
83
84static FILE *ftp_login(ftp_host_info_t *server)
85{
86 FILE *control_stream;
87 char buf[512];
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +000088
89 /* Connect to the command socket */
Glenn L McGrath236e93d2003-12-20 05:43:34 +000090 control_stream = fdopen(xconnect(server->s_in), "r+");
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +000091 if (control_stream == NULL) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000092 bb_perror_msg_and_die("Couldnt open control stream");
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +000093 }
94
95 if (ftpcmd(NULL, NULL, control_stream, buf) != 220) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000096 bb_error_msg_and_die("%s", buf + 4);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +000097 }
98
99 /* Login to the server */
100 switch (ftpcmd("USER ", server->user, control_stream, buf)) {
101 case 230:
102 break;
103 case 331:
104 if (ftpcmd("PASS ", server->password, control_stream, buf) != 230) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000105 bb_error_msg_and_die("PASS error: %s", buf + 4);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000106 }
107 break;
108 default:
Manuel Novoa III cad53642003-03-19 09:13:01 +0000109 bb_error_msg_and_die("USER error: %s", buf + 4);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000110 }
111
112 ftpcmd("TYPE I", NULL, control_stream, buf);
113
114 return(control_stream);
115}
116
Rob Landleyb1c3fbc2006-05-04 19:52:28 +0000117#if !ENABLE_FTPGET
118#define ftp_receive 0
119#else
Bernhard Reutner-Fischere0387a62006-06-07 13:31:59 +0000120static int ftp_receive(ftp_host_info_t *server, FILE *control_stream,
Eric Andersen04d055f2003-11-03 21:20:18 +0000121 const char *local_path, char *server_path)
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000122{
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000123 char buf[512];
124 off_t filesize = 0;
125 int fd_data;
Glenn L McGrath236e93d2003-12-20 05:43:34 +0000126 int fd_local = -1;
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000127 off_t beg_range = 0;
128
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000129 /* Connect to the data socket */
130 if (ftpcmd("PASV", NULL, control_stream, buf) != 227) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000131 bb_error_msg_and_die("PASV error: %s", buf + 4);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000132 }
Eric Andersen04d055f2003-11-03 21:20:18 +0000133 fd_data = xconnect_ftpdata(server, buf);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000134
135 if (ftpcmd("SIZE ", server_path, control_stream, buf) == 213) {
Eric Andersen24794452004-03-06 22:11:45 +0000136 unsigned long value=filesize;
Eric Andersenca65ca72004-03-15 08:46:37 +0000137 if (safe_strtoul(buf + 4, &value))
Eric Andersen24794452004-03-06 22:11:45 +0000138 bb_error_msg_and_die("SIZE error: %s", buf + 4);
139 filesize = value;
Rob Landleybc059bc2006-01-10 06:36:00 +0000140 } else {
141 filesize = -1;
142 do_continue = 0;
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000143 }
144
Glenn L McGrath236e93d2003-12-20 05:43:34 +0000145 if ((local_path[0] == '-') && (local_path[1] == '\0')) {
Eric Andersen70060d22004-03-27 10:02:48 +0000146 fd_local = STDOUT_FILENO;
Glenn L McGrath236e93d2003-12-20 05:43:34 +0000147 do_continue = 0;
148 }
149
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000150 if (do_continue) {
151 struct stat sbuf;
Glenn L McGrath236e93d2003-12-20 05:43:34 +0000152 if (lstat(local_path, &sbuf) < 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000153 bb_perror_msg_and_die("fstat()");
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000154 }
155 if (sbuf.st_size > 0) {
156 beg_range = sbuf.st_size;
157 } else {
158 do_continue = 0;
159 }
160 }
161
162 if (do_continue) {
Eric Andersen2a41ec62003-06-20 09:22:12 +0000163 sprintf(buf, "REST %ld", (long)beg_range);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000164 if (ftpcmd(buf, NULL, control_stream, buf) != 350) {
165 do_continue = 0;
166 } else {
167 filesize -= beg_range;
168 }
169 }
170
171 if (ftpcmd("RETR ", server_path, control_stream, buf) > 150) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000172 bb_error_msg_and_die("RETR error: %s", buf + 4);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000173 }
174
Glenn L McGrath1643f412002-12-18 02:47:40 +0000175 /* only make a local file if we know that one exists on the remote server */
Glenn L McGrath236e93d2003-12-20 05:43:34 +0000176 if (fd_local == -1) {
177 if (do_continue) {
178 fd_local = bb_xopen(local_path, O_APPEND | O_WRONLY);
179 } else {
180 fd_local = bb_xopen(local_path, O_CREAT | O_TRUNC | O_WRONLY);
181 }
Glenn L McGrath1643f412002-12-18 02:47:40 +0000182 }
183
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000184 /* Copy the file */
Rob Landleybc059bc2006-01-10 06:36:00 +0000185 if (filesize != -1) {
186 if (-1 == bb_copyfd_size(fd_data, fd_local, filesize))
187 exit(EXIT_FAILURE);
188 } else {
189 if (-1 == bb_copyfd_eof(fd_data, fd_local))
190 exit(EXIT_FAILURE);
Glenn L McGrath25fe94f2002-12-13 08:20:44 +0000191 }
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000192
193 /* close it all down */
194 close(fd_data);
195 if (ftpcmd(NULL, NULL, control_stream, buf) != 226) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000196 bb_error_msg_and_die("ftp error: %s", buf + 4);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000197 }
198 ftpcmd("QUIT", NULL, control_stream, buf);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000199
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000200 return(EXIT_SUCCESS);
201}
202#endif
203
Rob Landleyb1c3fbc2006-05-04 19:52:28 +0000204#if !ENABLE_FTPPUT
205#define ftp_send 0
206#else
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000207static int ftp_send(ftp_host_info_t *server, FILE *control_stream,
Eric Andersen04d055f2003-11-03 21:20:18 +0000208 const char *server_path, char *local_path)
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000209{
210 struct stat sbuf;
211 char buf[512];
212 int fd_data;
213 int fd_local;
214 int response;
215
216 /* Connect to the data socket */
217 if (ftpcmd("PASV", NULL, control_stream, buf) != 227) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000218 bb_error_msg_and_die("PASV error: %s", buf + 4);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000219 }
Eric Andersen04d055f2003-11-03 21:20:18 +0000220 fd_data = xconnect_ftpdata(server, buf);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000221
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000222 /* get the local file */
Glenn L McGrath236e93d2003-12-20 05:43:34 +0000223 if ((local_path[0] == '-') && (local_path[1] == '\0')) {
Eric Andersen70060d22004-03-27 10:02:48 +0000224 fd_local = STDIN_FILENO;
Glenn L McGrath236e93d2003-12-20 05:43:34 +0000225 } else {
226 fd_local = bb_xopen(local_path, O_RDONLY);
227 fstat(fd_local, &sbuf);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000228
Glenn L McGrath236e93d2003-12-20 05:43:34 +0000229 sprintf(buf, "ALLO %lu", (unsigned long)sbuf.st_size);
230 response = ftpcmd(buf, NULL, control_stream, buf);
231 switch (response) {
232 case 200:
233 case 202:
234 break;
235 default:
236 close(fd_local);
237 bb_error_msg_and_die("ALLO error: %s", buf + 4);
238 break;
239 }
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000240 }
Rob Landley6f037222005-11-08 00:52:31 +0000241 response = ftpcmd("STOR ", server_path, control_stream, buf);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000242 switch (response) {
243 case 125:
244 case 150:
245 break;
246 default:
247 close(fd_local);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000248 bb_error_msg_and_die("STOR error: %s", buf + 4);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000249 }
250
251 /* transfer the file */
Glenn L McGrath7ffe1332003-11-21 22:24:57 +0000252 if (bb_copyfd_eof(fd_local, fd_data) == -1) {
Glenn L McGrath25fe94f2002-12-13 08:20:44 +0000253 exit(EXIT_FAILURE);
254 }
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000255
256 /* close it all down */
257 close(fd_data);
258 if (ftpcmd(NULL, NULL, control_stream, buf) != 226) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000259 bb_error_msg_and_die("error: %s", buf + 4);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000260 }
261 ftpcmd("QUIT", NULL, control_stream, buf);
262
263 return(EXIT_SUCCESS);
264}
265#endif
266
Glenn L McGrathb51eb262003-12-19 10:37:52 +0000267#define FTPGETPUT_OPT_CONTINUE 1
268#define FTPGETPUT_OPT_VERBOSE 2
269#define FTPGETPUT_OPT_USER 4
270#define FTPGETPUT_OPT_PASSWORD 8
271#define FTPGETPUT_OPT_PORT 16
272
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +0000273#if ENABLE_FEATURE_FTPGETPUT_LONG_OPTIONS
Glenn L McGrathb51eb262003-12-19 10:37:52 +0000274static const struct option ftpgetput_long_options[] = {
275 {"continue", 1, NULL, 'c'},
276 {"verbose", 0, NULL, 'v'},
277 {"username", 1, NULL, 'u'},
278 {"password", 1, NULL, 'p'},
279 {"port", 1, NULL, 'P'},
280 {0, 0, 0, 0}
281};
Rob Landleyff97ee92006-06-02 19:03:01 +0000282#else
283#define ftpgetput_long_options 0
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +0000284#endif
Glenn L McGrathb51eb262003-12-19 10:37:52 +0000285
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000286int ftpgetput_main(int argc, char **argv)
287{
288 /* content-length of the file */
Glenn L McGrathb51eb262003-12-19 10:37:52 +0000289 unsigned long opt;
Glenn L McGrath266c1f52003-12-20 03:19:27 +0000290 char *port = "ftp";
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000291
292 /* socket to ftp server */
293 FILE *control_stream;
Eric Andersen04d055f2003-11-03 21:20:18 +0000294 struct sockaddr_in s_in;
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000295
296 /* continue a prev transfer (-c) */
297 ftp_host_info_t *server;
298
Eric Andersen04d055f2003-11-03 21:20:18 +0000299 int (*ftp_action)(ftp_host_info_t *, FILE *, const char *, char *) = NULL;
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000300
Glenn L McGrath266c1f52003-12-20 03:19:27 +0000301 /* Check to see if the command is ftpget or ftput */
Rob Landleyb1c3fbc2006-05-04 19:52:28 +0000302 if (ENABLE_FTPPUT && (!ENABLE_FTPGET || bb_applet_name[3] == 'p')) {
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000303 ftp_action = ftp_send;
Glenn L McGrath266c1f52003-12-20 03:19:27 +0000304 }
Rob Landleyb1c3fbc2006-05-04 19:52:28 +0000305 if (ENABLE_FTPGET && (!ENABLE_FTPPUT || bb_applet_name[3] == 'g')) {
Bernhard Reutner-Fischere0387a62006-06-07 13:31:59 +0000306 ftp_action = ftp_receive;
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000307 }
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000308
309 /* Set default values */
Glenn L McGrath266c1f52003-12-20 03:19:27 +0000310 server = xmalloc(sizeof(ftp_host_info_t));
311 server->user = "anonymous";
312 server->password = "busybox@";
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000313 verbose_flag = 0;
314
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000315 /*
316 * Decipher the command line
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000317 */
Rob Landleyff97ee92006-06-02 19:03:01 +0000318 if (ENABLE_FEATURE_FTPGETPUT_LONG_OPTIONS)
319 bb_applet_long_options = ftpgetput_long_options;
320
Glenn L McGrath266c1f52003-12-20 03:19:27 +0000321 opt = bb_getopt_ulflags(argc, argv, "cvu:p:P:", &server->user, &server->password, &port);
Glenn L McGrath236e93d2003-12-20 05:43:34 +0000322
323 /* Process the non-option command line arguments */
324 if (argc - optind != 3) {
325 bb_show_usage();
326 }
327
Glenn L McGrathb51eb262003-12-19 10:37:52 +0000328 if (opt & FTPGETPUT_OPT_CONTINUE) {
329 do_continue = 1;
330 }
331 if (opt & FTPGETPUT_OPT_VERBOSE) {
332 verbose_flag = 1;
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000333 }
334
Eric Andersen04d055f2003-11-03 21:20:18 +0000335 /* We want to do exactly _one_ DNS lookup, since some
336 * sites (i.e. ftp.us.debian.org) use round-robin DNS
337 * and we want to connect to only one IP... */
338 server->s_in = &s_in;
Glenn L McGrath266c1f52003-12-20 03:19:27 +0000339 bb_lookup_host(&s_in, argv[optind]);
Glenn L McGrath036dbaa2004-01-17 05:03:31 +0000340 s_in.sin_port = bb_lookup_port(port, "tcp", 21);
Eric Andersen04d055f2003-11-03 21:20:18 +0000341 if (verbose_flag) {
Glenn L McGrath266c1f52003-12-20 03:19:27 +0000342 printf("Connecting to %s[%s]:%d\n",
343 argv[optind], inet_ntoa(s_in.sin_addr), ntohs(s_in.sin_port));
Eric Andersen04d055f2003-11-03 21:20:18 +0000344 }
345
346 /* Connect/Setup/Configure the FTP session */
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000347 control_stream = ftp_login(server);
348
Eric Andersen04d055f2003-11-03 21:20:18 +0000349 return(ftp_action(server, control_stream, argv[optind + 1], argv[optind + 2]));
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000350}