blob: 1facfa3d8766d6e8248e872af532c201f89ce357 [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 "busybox.h"
Rob Landleyd921b2e2006-08-03 15:41:12 +000017#include <getopt.h>
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +000018
19typedef struct ftp_host_info_s {
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +000020 char *user;
21 char *password;
Eric Andersen04d055f2003-11-03 21:20:18 +000022 struct sockaddr_in *s_in;
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +000023} ftp_host_info_t;
24
Glenn L McGrath266c1f52003-12-20 03:19:27 +000025static char verbose_flag = 0;
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +000026static char do_continue = 0;
27
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +000028static int ftpcmd(const char *s1, const char *s2, FILE *stream, char *buf)
29{
30 if (verbose_flag) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000031 bb_error_msg("cmd %s%s", s1, s2);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +000032 }
33
34 if (s1) {
35 if (s2) {
Paul Fox146e83a2005-07-19 21:26:57 +000036 fprintf(stream, "%s%s\r\n", s1, s2);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +000037 } else {
Paul Fox146e83a2005-07-19 21:26:57 +000038 fprintf(stream, "%s\r\n", s1);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +000039 }
40 }
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +000041 do {
Glenn L McGrath5ec58282004-05-04 10:43:34 +000042 char *buf_ptr;
43
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +000044 if (fgets(buf, 510, stream) == NULL) {
Denis Vlasenko13858992006-10-08 12:49:22 +000045 bb_perror_msg_and_die("fgets");
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +000046 }
Glenn L McGrath5ec58282004-05-04 10:43:34 +000047 buf_ptr = strstr(buf, "\r\n");
48 if (buf_ptr) {
49 *buf_ptr = '\0';
50 }
Denis Vlasenko13858992006-10-08 12:49:22 +000051 } while (!isdigit(buf[0]) || buf[3] != ' ');
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +000052
Denis Vlasenko13858992006-10-08 12:49:22 +000053 return xatou(buf);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +000054}
55
Eric Andersen04d055f2003-11-03 21:20:18 +000056static int xconnect_ftpdata(ftp_host_info_t *server, const char *buf)
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +000057{
58 char *buf_ptr;
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +000059 unsigned short port_num;
60
61 buf_ptr = strrchr(buf, ',');
62 *buf_ptr = '\0';
Denis Vlasenko13858992006-10-08 12:49:22 +000063 port_num = xatoul_range(buf_ptr + 1, 0, 255);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +000064
65 buf_ptr = strrchr(buf, ',');
66 *buf_ptr = '\0';
Denis Vlasenko13858992006-10-08 12:49:22 +000067 port_num += xatoul_range(buf_ptr + 1, 0, 255) * 256;
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +000068
Denis Vlasenko13858992006-10-08 12:49:22 +000069 server->s_in->sin_port = htons(port_num);
Denis Vlasenko14579152006-10-26 01:09:46 +000070 return xconnect_tcp_v4(server->s_in);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +000071}
72
73static FILE *ftp_login(ftp_host_info_t *server)
74{
75 FILE *control_stream;
76 char buf[512];
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +000077
78 /* Connect to the command socket */
Denis Vlasenko14579152006-10-26 01:09:46 +000079 control_stream = fdopen(xconnect_tcp_v4(server->s_in), "r+");
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +000080 if (control_stream == NULL) {
Denis Vlasenkoa6dbb082006-10-12 19:29:44 +000081 bb_perror_msg_and_die("cannot open control stream");
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +000082 }
83
84 if (ftpcmd(NULL, NULL, control_stream, buf) != 220) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000085 bb_error_msg_and_die("%s", buf + 4);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +000086 }
87
88 /* Login to the server */
89 switch (ftpcmd("USER ", server->user, control_stream, buf)) {
90 case 230:
91 break;
92 case 331:
93 if (ftpcmd("PASS ", server->password, control_stream, buf) != 230) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000094 bb_error_msg_and_die("PASS error: %s", buf + 4);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +000095 }
96 break;
97 default:
Manuel Novoa III cad53642003-03-19 09:13:01 +000098 bb_error_msg_and_die("USER error: %s", buf + 4);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +000099 }
100
101 ftpcmd("TYPE I", NULL, control_stream, buf);
102
Denis Vlasenko079f8af2006-11-27 16:49:31 +0000103 return control_stream;
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000104}
105
Rob Landleyb1c3fbc2006-05-04 19:52:28 +0000106#if !ENABLE_FTPGET
Denis Vlasenko7039a662006-10-08 17:54:47 +0000107int ftp_receive(ftp_host_info_t *server, FILE *control_stream,
108 const char *local_path, char *server_path);
Rob Landleyb1c3fbc2006-05-04 19:52:28 +0000109#else
Denis Vlasenko7039a662006-10-08 17:54:47 +0000110static
111int ftp_receive(ftp_host_info_t *server, FILE *control_stream,
Eric Andersen04d055f2003-11-03 21:20:18 +0000112 const char *local_path, char *server_path)
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000113{
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000114 char buf[512];
115 off_t filesize = 0;
116 int fd_data;
Glenn L McGrath236e93d2003-12-20 05:43:34 +0000117 int fd_local = -1;
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000118 off_t beg_range = 0;
119
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000120 /* Connect to the data socket */
121 if (ftpcmd("PASV", NULL, control_stream, buf) != 227) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000122 bb_error_msg_and_die("PASV error: %s", buf + 4);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000123 }
Eric Andersen04d055f2003-11-03 21:20:18 +0000124 fd_data = xconnect_ftpdata(server, buf);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000125
126 if (ftpcmd("SIZE ", server_path, control_stream, buf) == 213) {
Denis Vlasenkod686a042006-11-27 14:43:21 +0000127 filesize = BB_STRTOOFF(buf + 4, NULL, 10);
128 if (errno || filesize < 0)
Eric Andersen24794452004-03-06 22:11:45 +0000129 bb_error_msg_and_die("SIZE error: %s", buf + 4);
Rob Landleybc059bc2006-01-10 06:36:00 +0000130 } else {
131 filesize = -1;
132 do_continue = 0;
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000133 }
134
Glenn L McGrath236e93d2003-12-20 05:43:34 +0000135 if ((local_path[0] == '-') && (local_path[1] == '\0')) {
Eric Andersen70060d22004-03-27 10:02:48 +0000136 fd_local = STDOUT_FILENO;
Glenn L McGrath236e93d2003-12-20 05:43:34 +0000137 do_continue = 0;
138 }
139
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000140 if (do_continue) {
141 struct stat sbuf;
Glenn L McGrath236e93d2003-12-20 05:43:34 +0000142 if (lstat(local_path, &sbuf) < 0) {
Denis Vlasenko7039a662006-10-08 17:54:47 +0000143 bb_perror_msg_and_die("lstat");
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000144 }
145 if (sbuf.st_size > 0) {
146 beg_range = sbuf.st_size;
147 } else {
148 do_continue = 0;
149 }
150 }
151
152 if (do_continue) {
Denis Vlasenkocf30cc82006-11-24 14:53:18 +0000153 sprintf(buf, "REST %"OFF_FMT"d", beg_range);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000154 if (ftpcmd(buf, NULL, control_stream, buf) != 350) {
155 do_continue = 0;
156 } else {
157 filesize -= beg_range;
158 }
159 }
160
161 if (ftpcmd("RETR ", server_path, control_stream, buf) > 150) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000162 bb_error_msg_and_die("RETR error: %s", buf + 4);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000163 }
164
Glenn L McGrath1643f412002-12-18 02:47:40 +0000165 /* only make a local file if we know that one exists on the remote server */
Glenn L McGrath236e93d2003-12-20 05:43:34 +0000166 if (fd_local == -1) {
167 if (do_continue) {
Rob Landleyd921b2e2006-08-03 15:41:12 +0000168 fd_local = xopen(local_path, O_APPEND | O_WRONLY);
Glenn L McGrath236e93d2003-12-20 05:43:34 +0000169 } else {
Denis Vlasenkocf749bc2006-11-26 15:45:17 +0000170 fd_local = xopen(local_path, O_CREAT | O_TRUNC | O_WRONLY);
Glenn L McGrath236e93d2003-12-20 05:43:34 +0000171 }
Glenn L McGrath1643f412002-12-18 02:47:40 +0000172 }
173
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000174 /* Copy the file */
Rob Landleybc059bc2006-01-10 06:36:00 +0000175 if (filesize != -1) {
176 if (-1 == bb_copyfd_size(fd_data, fd_local, filesize))
177 exit(EXIT_FAILURE);
178 } else {
179 if (-1 == bb_copyfd_eof(fd_data, fd_local))
180 exit(EXIT_FAILURE);
Glenn L McGrath25fe94f2002-12-13 08:20:44 +0000181 }
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000182
183 /* close it all down */
184 close(fd_data);
185 if (ftpcmd(NULL, NULL, control_stream, buf) != 226) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000186 bb_error_msg_and_die("ftp error: %s", buf + 4);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000187 }
188 ftpcmd("QUIT", NULL, control_stream, buf);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000189
Denis Vlasenko079f8af2006-11-27 16:49:31 +0000190 return EXIT_SUCCESS;
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000191}
192#endif
193
Rob Landleyb1c3fbc2006-05-04 19:52:28 +0000194#if !ENABLE_FTPPUT
Denis Vlasenko7039a662006-10-08 17:54:47 +0000195int ftp_send(ftp_host_info_t *server, FILE *control_stream,
196 const char *server_path, char *local_path);
Rob Landleyb1c3fbc2006-05-04 19:52:28 +0000197#else
Denis Vlasenko7039a662006-10-08 17:54:47 +0000198static
199int ftp_send(ftp_host_info_t *server, FILE *control_stream,
Eric Andersen04d055f2003-11-03 21:20:18 +0000200 const char *server_path, char *local_path)
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000201{
202 struct stat sbuf;
203 char buf[512];
204 int fd_data;
205 int fd_local;
206 int response;
207
208 /* Connect to the data socket */
209 if (ftpcmd("PASV", NULL, control_stream, buf) != 227) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000210 bb_error_msg_and_die("PASV error: %s", buf + 4);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000211 }
Eric Andersen04d055f2003-11-03 21:20:18 +0000212 fd_data = xconnect_ftpdata(server, buf);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000213
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000214 /* get the local file */
Glenn L McGrath236e93d2003-12-20 05:43:34 +0000215 if ((local_path[0] == '-') && (local_path[1] == '\0')) {
Eric Andersen70060d22004-03-27 10:02:48 +0000216 fd_local = STDIN_FILENO;
Glenn L McGrath236e93d2003-12-20 05:43:34 +0000217 } else {
Rob Landleyd921b2e2006-08-03 15:41:12 +0000218 fd_local = xopen(local_path, O_RDONLY);
Glenn L McGrath236e93d2003-12-20 05:43:34 +0000219 fstat(fd_local, &sbuf);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000220
Glenn L McGrath236e93d2003-12-20 05:43:34 +0000221 sprintf(buf, "ALLO %lu", (unsigned long)sbuf.st_size);
222 response = ftpcmd(buf, NULL, control_stream, buf);
223 switch (response) {
224 case 200:
225 case 202:
226 break;
227 default:
228 close(fd_local);
229 bb_error_msg_and_die("ALLO error: %s", buf + 4);
230 break;
231 }
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000232 }
Rob Landley6f037222005-11-08 00:52:31 +0000233 response = ftpcmd("STOR ", server_path, control_stream, buf);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000234 switch (response) {
235 case 125:
236 case 150:
237 break;
238 default:
239 close(fd_local);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000240 bb_error_msg_and_die("STOR error: %s", buf + 4);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000241 }
242
243 /* transfer the file */
Glenn L McGrath7ffe1332003-11-21 22:24:57 +0000244 if (bb_copyfd_eof(fd_local, fd_data) == -1) {
Glenn L McGrath25fe94f2002-12-13 08:20:44 +0000245 exit(EXIT_FAILURE);
246 }
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000247
248 /* close it all down */
249 close(fd_data);
250 if (ftpcmd(NULL, NULL, control_stream, buf) != 226) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000251 bb_error_msg_and_die("error: %s", buf + 4);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000252 }
253 ftpcmd("QUIT", NULL, control_stream, buf);
254
Denis Vlasenko079f8af2006-11-27 16:49:31 +0000255 return EXIT_SUCCESS;
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000256}
257#endif
258
Glenn L McGrathb51eb262003-12-19 10:37:52 +0000259#define FTPGETPUT_OPT_CONTINUE 1
260#define FTPGETPUT_OPT_VERBOSE 2
261#define FTPGETPUT_OPT_USER 4
262#define FTPGETPUT_OPT_PASSWORD 8
263#define FTPGETPUT_OPT_PORT 16
264
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +0000265#if ENABLE_FEATURE_FTPGETPUT_LONG_OPTIONS
Glenn L McGrathb51eb262003-12-19 10:37:52 +0000266static const struct option ftpgetput_long_options[] = {
267 {"continue", 1, NULL, 'c'},
268 {"verbose", 0, NULL, 'v'},
269 {"username", 1, NULL, 'u'},
270 {"password", 1, NULL, 'p'},
271 {"port", 1, NULL, 'P'},
272 {0, 0, 0, 0}
273};
Rob Landleyff97ee92006-06-02 19:03:01 +0000274#else
275#define ftpgetput_long_options 0
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +0000276#endif
Glenn L McGrathb51eb262003-12-19 10:37:52 +0000277
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000278int ftpgetput_main(int argc, char **argv)
279{
280 /* content-length of the file */
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000281 unsigned opt;
Glenn L McGrath266c1f52003-12-20 03:19:27 +0000282 char *port = "ftp";
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000283
284 /* socket to ftp server */
285 FILE *control_stream;
Eric Andersen04d055f2003-11-03 21:20:18 +0000286 struct sockaddr_in s_in;
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000287
288 /* continue a prev transfer (-c) */
289 ftp_host_info_t *server;
290
Eric Andersen04d055f2003-11-03 21:20:18 +0000291 int (*ftp_action)(ftp_host_info_t *, FILE *, const char *, char *) = NULL;
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000292
Glenn L McGrath266c1f52003-12-20 03:19:27 +0000293 /* Check to see if the command is ftpget or ftput */
Denis Vlasenko8f8f2682006-10-03 21:00:43 +0000294 if (ENABLE_FTPPUT && (!ENABLE_FTPGET || applet_name[3] == 'p')) {
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000295 ftp_action = ftp_send;
Glenn L McGrath266c1f52003-12-20 03:19:27 +0000296 }
Denis Vlasenko8f8f2682006-10-03 21:00:43 +0000297 if (ENABLE_FTPGET && (!ENABLE_FTPPUT || applet_name[3] == 'g')) {
Bernhard Reutner-Fischere0387a62006-06-07 13:31:59 +0000298 ftp_action = ftp_receive;
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000299 }
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000300
301 /* Set default values */
Glenn L McGrath266c1f52003-12-20 03:19:27 +0000302 server = xmalloc(sizeof(ftp_host_info_t));
303 server->user = "anonymous";
304 server->password = "busybox@";
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000305 verbose_flag = 0;
306
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000307 /*
308 * Decipher the command line
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000309 */
Rob Landleyff97ee92006-06-02 19:03:01 +0000310 if (ENABLE_FEATURE_FTPGETPUT_LONG_OPTIONS)
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000311 applet_long_options = ftpgetput_long_options;
Rob Landleyff97ee92006-06-02 19:03:01 +0000312
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000313 opt = getopt32(argc, argv, "cvu:p:P:", &server->user, &server->password, &port);
Glenn L McGrath236e93d2003-12-20 05:43:34 +0000314
315 /* Process the non-option command line arguments */
316 if (argc - optind != 3) {
317 bb_show_usage();
318 }
319
Glenn L McGrathb51eb262003-12-19 10:37:52 +0000320 if (opt & FTPGETPUT_OPT_CONTINUE) {
321 do_continue = 1;
322 }
323 if (opt & FTPGETPUT_OPT_VERBOSE) {
324 verbose_flag = 1;
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000325 }
326
Eric Andersen04d055f2003-11-03 21:20:18 +0000327 /* We want to do exactly _one_ DNS lookup, since some
328 * sites (i.e. ftp.us.debian.org) use round-robin DNS
329 * and we want to connect to only one IP... */
330 server->s_in = &s_in;
Glenn L McGrath266c1f52003-12-20 03:19:27 +0000331 bb_lookup_host(&s_in, argv[optind]);
Glenn L McGrath036dbaa2004-01-17 05:03:31 +0000332 s_in.sin_port = bb_lookup_port(port, "tcp", 21);
Eric Andersen04d055f2003-11-03 21:20:18 +0000333 if (verbose_flag) {
Glenn L McGrath266c1f52003-12-20 03:19:27 +0000334 printf("Connecting to %s[%s]:%d\n",
335 argv[optind], inet_ntoa(s_in.sin_addr), ntohs(s_in.sin_port));
Eric Andersen04d055f2003-11-03 21:20:18 +0000336 }
337
338 /* Connect/Setup/Configure the FTP session */
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000339 control_stream = ftp_login(server);
340
Denis Vlasenko079f8af2006-11-27 16:49:31 +0000341 return ftp_action(server, control_stream, argv[optind + 1], argv[optind + 2]);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000342}