blob: f285e8b31889634c3bfea379a708cee9689cb306 [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
16#include <sys/types.h>
17#include <sys/ioctl.h>
18#include <sys/time.h>
19#include <sys/stat.h>
20
21#include <ctype.h>
22#include <errno.h>
23#include <fcntl.h>
24#include <getopt.h>
25#include <stdio.h>
26#include <stdlib.h>
27#include <signal.h>
28#include <string.h>
29#include <unistd.h>
30
31#include <netinet/in.h>
32#include <netdb.h>
Eric Andersen04d055f2003-11-03 21:20:18 +000033#include <sys/socket.h>
34#include <arpa/inet.h>
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +000035
36#include "busybox.h"
37
38typedef struct ftp_host_info_s {
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +000039 char *user;
40 char *password;
Eric Andersen04d055f2003-11-03 21:20:18 +000041 struct sockaddr_in *s_in;
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +000042} ftp_host_info_t;
43
Glenn L McGrath266c1f52003-12-20 03:19:27 +000044static char verbose_flag = 0;
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +000045static char do_continue = 0;
46
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +000047static int ftpcmd(const char *s1, const char *s2, FILE *stream, char *buf)
48{
49 if (verbose_flag) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000050 bb_error_msg("cmd %s%s", s1, s2);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +000051 }
52
53 if (s1) {
54 if (s2) {
Paul Fox146e83a2005-07-19 21:26:57 +000055 fprintf(stream, "%s%s\r\n", s1, s2);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +000056 } else {
Paul Fox146e83a2005-07-19 21:26:57 +000057 fprintf(stream, "%s\r\n", s1);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +000058 }
59 }
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +000060 do {
Glenn L McGrath5ec58282004-05-04 10:43:34 +000061 char *buf_ptr;
62
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +000063 if (fgets(buf, 510, stream) == NULL) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000064 bb_perror_msg_and_die("fgets()");
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +000065 }
Glenn L McGrath5ec58282004-05-04 10:43:34 +000066 buf_ptr = strstr(buf, "\r\n");
67 if (buf_ptr) {
68 *buf_ptr = '\0';
69 }
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +000070 } while (! isdigit(buf[0]) || buf[3] != ' ');
71
72 return atoi(buf);
73}
74
Eric Andersen04d055f2003-11-03 21:20:18 +000075static int xconnect_ftpdata(ftp_host_info_t *server, const char *buf)
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +000076{
77 char *buf_ptr;
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +000078 unsigned short port_num;
79
80 buf_ptr = strrchr(buf, ',');
81 *buf_ptr = '\0';
82 port_num = atoi(buf_ptr + 1);
83
84 buf_ptr = strrchr(buf, ',');
85 *buf_ptr = '\0';
86 port_num += atoi(buf_ptr + 1) * 256;
87
Eric Andersen04d055f2003-11-03 21:20:18 +000088 server->s_in->sin_port=htons(port_num);
89 return(xconnect(server->s_in));
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +000090}
91
92static FILE *ftp_login(ftp_host_info_t *server)
93{
94 FILE *control_stream;
95 char buf[512];
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +000096
97 /* Connect to the command socket */
Glenn L McGrath236e93d2003-12-20 05:43:34 +000098 control_stream = fdopen(xconnect(server->s_in), "r+");
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +000099 if (control_stream == NULL) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000100 bb_perror_msg_and_die("Couldnt open control stream");
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000101 }
102
103 if (ftpcmd(NULL, NULL, control_stream, buf) != 220) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000104 bb_error_msg_and_die("%s", buf + 4);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000105 }
106
107 /* Login to the server */
108 switch (ftpcmd("USER ", server->user, control_stream, buf)) {
109 case 230:
110 break;
111 case 331:
112 if (ftpcmd("PASS ", server->password, control_stream, buf) != 230) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000113 bb_error_msg_and_die("PASS error: %s", buf + 4);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000114 }
115 break;
116 default:
Manuel Novoa III cad53642003-03-19 09:13:01 +0000117 bb_error_msg_and_die("USER error: %s", buf + 4);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000118 }
119
120 ftpcmd("TYPE I", NULL, control_stream, buf);
121
122 return(control_stream);
123}
124
Rob Landleyb1c3fbc2006-05-04 19:52:28 +0000125#if !ENABLE_FTPGET
126#define ftp_receive 0
127#else
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000128static int ftp_recieve(ftp_host_info_t *server, FILE *control_stream,
Eric Andersen04d055f2003-11-03 21:20:18 +0000129 const char *local_path, char *server_path)
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000130{
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000131 char buf[512];
132 off_t filesize = 0;
133 int fd_data;
Glenn L McGrath236e93d2003-12-20 05:43:34 +0000134 int fd_local = -1;
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000135 off_t beg_range = 0;
136
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000137 /* Connect to the data socket */
138 if (ftpcmd("PASV", NULL, control_stream, buf) != 227) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000139 bb_error_msg_and_die("PASV error: %s", buf + 4);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000140 }
Eric Andersen04d055f2003-11-03 21:20:18 +0000141 fd_data = xconnect_ftpdata(server, buf);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000142
143 if (ftpcmd("SIZE ", server_path, control_stream, buf) == 213) {
Eric Andersen24794452004-03-06 22:11:45 +0000144 unsigned long value=filesize;
Eric Andersenca65ca72004-03-15 08:46:37 +0000145 if (safe_strtoul(buf + 4, &value))
Eric Andersen24794452004-03-06 22:11:45 +0000146 bb_error_msg_and_die("SIZE error: %s", buf + 4);
147 filesize = value;
Rob Landleybc059bc2006-01-10 06:36:00 +0000148 } else {
149 filesize = -1;
150 do_continue = 0;
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000151 }
152
Glenn L McGrath236e93d2003-12-20 05:43:34 +0000153 if ((local_path[0] == '-') && (local_path[1] == '\0')) {
Eric Andersen70060d22004-03-27 10:02:48 +0000154 fd_local = STDOUT_FILENO;
Glenn L McGrath236e93d2003-12-20 05:43:34 +0000155 do_continue = 0;
156 }
157
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000158 if (do_continue) {
159 struct stat sbuf;
Glenn L McGrath236e93d2003-12-20 05:43:34 +0000160 if (lstat(local_path, &sbuf) < 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000161 bb_perror_msg_and_die("fstat()");
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000162 }
163 if (sbuf.st_size > 0) {
164 beg_range = sbuf.st_size;
165 } else {
166 do_continue = 0;
167 }
168 }
169
170 if (do_continue) {
Eric Andersen2a41ec62003-06-20 09:22:12 +0000171 sprintf(buf, "REST %ld", (long)beg_range);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000172 if (ftpcmd(buf, NULL, control_stream, buf) != 350) {
173 do_continue = 0;
174 } else {
175 filesize -= beg_range;
176 }
177 }
178
179 if (ftpcmd("RETR ", server_path, control_stream, buf) > 150) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000180 bb_error_msg_and_die("RETR error: %s", buf + 4);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000181 }
182
Glenn L McGrath1643f412002-12-18 02:47:40 +0000183 /* only make a local file if we know that one exists on the remote server */
Glenn L McGrath236e93d2003-12-20 05:43:34 +0000184 if (fd_local == -1) {
185 if (do_continue) {
186 fd_local = bb_xopen(local_path, O_APPEND | O_WRONLY);
187 } else {
188 fd_local = bb_xopen(local_path, O_CREAT | O_TRUNC | O_WRONLY);
189 }
Glenn L McGrath1643f412002-12-18 02:47:40 +0000190 }
191
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000192 /* Copy the file */
Rob Landleybc059bc2006-01-10 06:36:00 +0000193 if (filesize != -1) {
194 if (-1 == bb_copyfd_size(fd_data, fd_local, filesize))
195 exit(EXIT_FAILURE);
196 } else {
197 if (-1 == bb_copyfd_eof(fd_data, fd_local))
198 exit(EXIT_FAILURE);
Glenn L McGrath25fe94f2002-12-13 08:20:44 +0000199 }
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000200
201 /* close it all down */
202 close(fd_data);
203 if (ftpcmd(NULL, NULL, control_stream, buf) != 226) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000204 bb_error_msg_and_die("ftp error: %s", buf + 4);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000205 }
206 ftpcmd("QUIT", NULL, control_stream, buf);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000207
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000208 return(EXIT_SUCCESS);
209}
210#endif
211
Rob Landleyb1c3fbc2006-05-04 19:52:28 +0000212#if !ENABLE_FTPPUT
213#define ftp_send 0
214#else
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000215static int ftp_send(ftp_host_info_t *server, FILE *control_stream,
Eric Andersen04d055f2003-11-03 21:20:18 +0000216 const char *server_path, char *local_path)
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000217{
218 struct stat sbuf;
219 char buf[512];
220 int fd_data;
221 int fd_local;
222 int response;
223
224 /* Connect to the data socket */
225 if (ftpcmd("PASV", NULL, control_stream, buf) != 227) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000226 bb_error_msg_and_die("PASV error: %s", buf + 4);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000227 }
Eric Andersen04d055f2003-11-03 21:20:18 +0000228 fd_data = xconnect_ftpdata(server, buf);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000229
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000230 /* get the local file */
Glenn L McGrath236e93d2003-12-20 05:43:34 +0000231 if ((local_path[0] == '-') && (local_path[1] == '\0')) {
Eric Andersen70060d22004-03-27 10:02:48 +0000232 fd_local = STDIN_FILENO;
Glenn L McGrath236e93d2003-12-20 05:43:34 +0000233 } else {
234 fd_local = bb_xopen(local_path, O_RDONLY);
235 fstat(fd_local, &sbuf);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000236
Glenn L McGrath236e93d2003-12-20 05:43:34 +0000237 sprintf(buf, "ALLO %lu", (unsigned long)sbuf.st_size);
238 response = ftpcmd(buf, NULL, control_stream, buf);
239 switch (response) {
240 case 200:
241 case 202:
242 break;
243 default:
244 close(fd_local);
245 bb_error_msg_and_die("ALLO error: %s", buf + 4);
246 break;
247 }
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000248 }
Rob Landley6f037222005-11-08 00:52:31 +0000249 response = ftpcmd("STOR ", server_path, control_stream, buf);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000250 switch (response) {
251 case 125:
252 case 150:
253 break;
254 default:
255 close(fd_local);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000256 bb_error_msg_and_die("STOR error: %s", buf + 4);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000257 }
258
259 /* transfer the file */
Glenn L McGrath7ffe1332003-11-21 22:24:57 +0000260 if (bb_copyfd_eof(fd_local, fd_data) == -1) {
Glenn L McGrath25fe94f2002-12-13 08:20:44 +0000261 exit(EXIT_FAILURE);
262 }
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000263
264 /* close it all down */
265 close(fd_data);
266 if (ftpcmd(NULL, NULL, control_stream, buf) != 226) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000267 bb_error_msg_and_die("error: %s", buf + 4);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000268 }
269 ftpcmd("QUIT", NULL, control_stream, buf);
270
271 return(EXIT_SUCCESS);
272}
273#endif
274
Glenn L McGrathb51eb262003-12-19 10:37:52 +0000275#define FTPGETPUT_OPT_CONTINUE 1
276#define FTPGETPUT_OPT_VERBOSE 2
277#define FTPGETPUT_OPT_USER 4
278#define FTPGETPUT_OPT_PASSWORD 8
279#define FTPGETPUT_OPT_PORT 16
280
281static const struct option ftpgetput_long_options[] = {
282 {"continue", 1, NULL, 'c'},
283 {"verbose", 0, NULL, 'v'},
284 {"username", 1, NULL, 'u'},
285 {"password", 1, NULL, 'p'},
286 {"port", 1, NULL, 'P'},
287 {0, 0, 0, 0}
288};
289
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000290int ftpgetput_main(int argc, char **argv)
291{
292 /* content-length of the file */
Glenn L McGrathb51eb262003-12-19 10:37:52 +0000293 unsigned long opt;
Glenn L McGrath266c1f52003-12-20 03:19:27 +0000294 char *port = "ftp";
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000295
296 /* socket to ftp server */
297 FILE *control_stream;
Eric Andersen04d055f2003-11-03 21:20:18 +0000298 struct sockaddr_in s_in;
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000299
300 /* continue a prev transfer (-c) */
301 ftp_host_info_t *server;
302
Eric Andersen04d055f2003-11-03 21:20:18 +0000303 int (*ftp_action)(ftp_host_info_t *, FILE *, const char *, char *) = NULL;
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000304
Glenn L McGrath266c1f52003-12-20 03:19:27 +0000305 /* Check to see if the command is ftpget or ftput */
Rob Landleyb1c3fbc2006-05-04 19:52:28 +0000306 if (ENABLE_FTPPUT && (!ENABLE_FTPGET || bb_applet_name[3] == 'p')) {
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000307 ftp_action = ftp_send;
Glenn L McGrath266c1f52003-12-20 03:19:27 +0000308 }
Rob Landleyb1c3fbc2006-05-04 19:52:28 +0000309 if (ENABLE_FTPGET && (!ENABLE_FTPPUT || bb_applet_name[3] == 'g')) {
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000310 ftp_action = ftp_recieve;
311 }
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000312
313 /* Set default values */
Glenn L McGrath266c1f52003-12-20 03:19:27 +0000314 server = xmalloc(sizeof(ftp_host_info_t));
315 server->user = "anonymous";
316 server->password = "busybox@";
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000317 verbose_flag = 0;
318
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000319 /*
320 * Decipher the command line
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000321 */
Glenn L McGrathb51eb262003-12-19 10:37:52 +0000322 bb_applet_long_options = ftpgetput_long_options;
Glenn L McGrath266c1f52003-12-20 03:19:27 +0000323 opt = bb_getopt_ulflags(argc, argv, "cvu:p:P:", &server->user, &server->password, &port);
Glenn L McGrath236e93d2003-12-20 05:43:34 +0000324
325 /* Process the non-option command line arguments */
326 if (argc - optind != 3) {
327 bb_show_usage();
328 }
329
Glenn L McGrathb51eb262003-12-19 10:37:52 +0000330 if (opt & FTPGETPUT_OPT_CONTINUE) {
331 do_continue = 1;
332 }
333 if (opt & FTPGETPUT_OPT_VERBOSE) {
334 verbose_flag = 1;
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000335 }
336
Eric Andersen04d055f2003-11-03 21:20:18 +0000337 /* We want to do exactly _one_ DNS lookup, since some
338 * sites (i.e. ftp.us.debian.org) use round-robin DNS
339 * and we want to connect to only one IP... */
340 server->s_in = &s_in;
Glenn L McGrath266c1f52003-12-20 03:19:27 +0000341 bb_lookup_host(&s_in, argv[optind]);
Glenn L McGrath036dbaa2004-01-17 05:03:31 +0000342 s_in.sin_port = bb_lookup_port(port, "tcp", 21);
Eric Andersen04d055f2003-11-03 21:20:18 +0000343 if (verbose_flag) {
Glenn L McGrath266c1f52003-12-20 03:19:27 +0000344 printf("Connecting to %s[%s]:%d\n",
345 argv[optind], inet_ntoa(s_in.sin_addr), ntohs(s_in.sin_port));
Eric Andersen04d055f2003-11-03 21:20:18 +0000346 }
347
348 /* Connect/Setup/Configure the FTP session */
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000349 control_stream = ftp_login(server);
350
Eric Andersen04d055f2003-11-03 21:20:18 +0000351 return(ftp_action(server, control_stream, argv[optind + 1], argv[optind + 2]));
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000352}
353
354/*
355Local Variables:
356c-file-style: "linux"
357c-basic-offset: 4
358tab-width: 4
359End:
360*/