blob: 7a0ba7772ee723911055cffcce99f08e46b16fea [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
125#ifdef CONFIG_FTPGET
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000126static int ftp_recieve(ftp_host_info_t *server, FILE *control_stream,
Eric Andersen04d055f2003-11-03 21:20:18 +0000127 const char *local_path, char *server_path)
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000128{
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000129 char buf[512];
130 off_t filesize = 0;
131 int fd_data;
Glenn L McGrath236e93d2003-12-20 05:43:34 +0000132 int fd_local = -1;
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000133 off_t beg_range = 0;
134
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000135 /* Connect to the data socket */
136 if (ftpcmd("PASV", NULL, control_stream, buf) != 227) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000137 bb_error_msg_and_die("PASV error: %s", buf + 4);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000138 }
Eric Andersen04d055f2003-11-03 21:20:18 +0000139 fd_data = xconnect_ftpdata(server, buf);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000140
141 if (ftpcmd("SIZE ", server_path, control_stream, buf) == 213) {
Eric Andersen24794452004-03-06 22:11:45 +0000142 unsigned long value=filesize;
Eric Andersenca65ca72004-03-15 08:46:37 +0000143 if (safe_strtoul(buf + 4, &value))
Eric Andersen24794452004-03-06 22:11:45 +0000144 bb_error_msg_and_die("SIZE error: %s", buf + 4);
145 filesize = value;
Rob Landleybc059bc2006-01-10 06:36:00 +0000146 } else {
147 filesize = -1;
148 do_continue = 0;
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000149 }
150
Glenn L McGrath236e93d2003-12-20 05:43:34 +0000151 if ((local_path[0] == '-') && (local_path[1] == '\0')) {
Eric Andersen70060d22004-03-27 10:02:48 +0000152 fd_local = STDOUT_FILENO;
Glenn L McGrath236e93d2003-12-20 05:43:34 +0000153 do_continue = 0;
154 }
155
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000156 if (do_continue) {
157 struct stat sbuf;
Glenn L McGrath236e93d2003-12-20 05:43:34 +0000158 if (lstat(local_path, &sbuf) < 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000159 bb_perror_msg_and_die("fstat()");
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000160 }
161 if (sbuf.st_size > 0) {
162 beg_range = sbuf.st_size;
163 } else {
164 do_continue = 0;
165 }
166 }
167
168 if (do_continue) {
Eric Andersen2a41ec62003-06-20 09:22:12 +0000169 sprintf(buf, "REST %ld", (long)beg_range);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000170 if (ftpcmd(buf, NULL, control_stream, buf) != 350) {
171 do_continue = 0;
172 } else {
173 filesize -= beg_range;
174 }
175 }
176
177 if (ftpcmd("RETR ", server_path, control_stream, buf) > 150) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000178 bb_error_msg_and_die("RETR error: %s", buf + 4);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000179 }
180
Glenn L McGrath1643f412002-12-18 02:47:40 +0000181 /* only make a local file if we know that one exists on the remote server */
Glenn L McGrath236e93d2003-12-20 05:43:34 +0000182 if (fd_local == -1) {
183 if (do_continue) {
184 fd_local = bb_xopen(local_path, O_APPEND | O_WRONLY);
185 } else {
186 fd_local = bb_xopen(local_path, O_CREAT | O_TRUNC | O_WRONLY);
187 }
Glenn L McGrath1643f412002-12-18 02:47:40 +0000188 }
189
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000190 /* Copy the file */
Rob Landleybc059bc2006-01-10 06:36:00 +0000191 if (filesize != -1) {
192 if (-1 == bb_copyfd_size(fd_data, fd_local, filesize))
193 exit(EXIT_FAILURE);
194 } else {
195 if (-1 == bb_copyfd_eof(fd_data, fd_local))
196 exit(EXIT_FAILURE);
Glenn L McGrath25fe94f2002-12-13 08:20:44 +0000197 }
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000198
199 /* close it all down */
200 close(fd_data);
201 if (ftpcmd(NULL, NULL, control_stream, buf) != 226) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000202 bb_error_msg_and_die("ftp error: %s", buf + 4);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000203 }
204 ftpcmd("QUIT", NULL, control_stream, buf);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000205
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000206 return(EXIT_SUCCESS);
207}
208#endif
209
210#ifdef CONFIG_FTPPUT
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000211static int ftp_send(ftp_host_info_t *server, FILE *control_stream,
Eric Andersen04d055f2003-11-03 21:20:18 +0000212 const char *server_path, char *local_path)
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000213{
214 struct stat sbuf;
215 char buf[512];
216 int fd_data;
217 int fd_local;
218 int response;
219
220 /* Connect to the data socket */
221 if (ftpcmd("PASV", NULL, control_stream, buf) != 227) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000222 bb_error_msg_and_die("PASV error: %s", buf + 4);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000223 }
Eric Andersen04d055f2003-11-03 21:20:18 +0000224 fd_data = xconnect_ftpdata(server, buf);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000225
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000226 /* get the local file */
Glenn L McGrath236e93d2003-12-20 05:43:34 +0000227 if ((local_path[0] == '-') && (local_path[1] == '\0')) {
Eric Andersen70060d22004-03-27 10:02:48 +0000228 fd_local = STDIN_FILENO;
Glenn L McGrath236e93d2003-12-20 05:43:34 +0000229 } else {
230 fd_local = bb_xopen(local_path, O_RDONLY);
231 fstat(fd_local, &sbuf);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000232
Glenn L McGrath236e93d2003-12-20 05:43:34 +0000233 sprintf(buf, "ALLO %lu", (unsigned long)sbuf.st_size);
234 response = ftpcmd(buf, NULL, control_stream, buf);
235 switch (response) {
236 case 200:
237 case 202:
238 break;
239 default:
240 close(fd_local);
241 bb_error_msg_and_die("ALLO error: %s", buf + 4);
242 break;
243 }
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000244 }
Rob Landley6f037222005-11-08 00:52:31 +0000245 response = ftpcmd("STOR ", server_path, control_stream, buf);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000246 switch (response) {
247 case 125:
248 case 150:
249 break;
250 default:
251 close(fd_local);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000252 bb_error_msg_and_die("STOR error: %s", buf + 4);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000253 }
254
255 /* transfer the file */
Glenn L McGrath7ffe1332003-11-21 22:24:57 +0000256 if (bb_copyfd_eof(fd_local, fd_data) == -1) {
Glenn L McGrath25fe94f2002-12-13 08:20:44 +0000257 exit(EXIT_FAILURE);
258 }
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000259
260 /* close it all down */
261 close(fd_data);
262 if (ftpcmd(NULL, NULL, control_stream, buf) != 226) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000263 bb_error_msg_and_die("error: %s", buf + 4);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000264 }
265 ftpcmd("QUIT", NULL, control_stream, buf);
266
267 return(EXIT_SUCCESS);
268}
269#endif
270
Glenn L McGrathb51eb262003-12-19 10:37:52 +0000271#define FTPGETPUT_OPT_CONTINUE 1
272#define FTPGETPUT_OPT_VERBOSE 2
273#define FTPGETPUT_OPT_USER 4
274#define FTPGETPUT_OPT_PASSWORD 8
275#define FTPGETPUT_OPT_PORT 16
276
277static const struct option ftpgetput_long_options[] = {
278 {"continue", 1, NULL, 'c'},
279 {"verbose", 0, NULL, 'v'},
280 {"username", 1, NULL, 'u'},
281 {"password", 1, NULL, 'p'},
282 {"port", 1, NULL, 'P'},
283 {0, 0, 0, 0}
284};
285
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 */
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000302#ifdef CONFIG_FTPPUT
Glenn L McGrath266c1f52003-12-20 03:19:27 +0000303# ifdef CONFIG_FTPGET
Manuel Novoa III cad53642003-03-19 09:13:01 +0000304 if (bb_applet_name[3] == 'p') {
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000305 ftp_action = ftp_send;
Glenn L McGrath266c1f52003-12-20 03:19:27 +0000306 }
307# else
308 ftp_action = ftp_send;
309# endif
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000310#endif
311#ifdef CONFIG_FTPGET
Glenn L McGrath266c1f52003-12-20 03:19:27 +0000312# ifdef CONFIG_FTPPUT
Manuel Novoa III cad53642003-03-19 09:13:01 +0000313 if (bb_applet_name[3] == 'g') {
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000314 ftp_action = ftp_recieve;
315 }
Glenn L McGrath266c1f52003-12-20 03:19:27 +0000316# else
317 ftp_action = ftp_recieve;
318# endif
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000319#endif
320
321 /* Set default values */
Glenn L McGrath266c1f52003-12-20 03:19:27 +0000322 server = xmalloc(sizeof(ftp_host_info_t));
323 server->user = "anonymous";
324 server->password = "busybox@";
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000325 verbose_flag = 0;
326
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000327 /*
328 * Decipher the command line
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000329 */
Glenn L McGrathb51eb262003-12-19 10:37:52 +0000330 bb_applet_long_options = ftpgetput_long_options;
Glenn L McGrath266c1f52003-12-20 03:19:27 +0000331 opt = bb_getopt_ulflags(argc, argv, "cvu:p:P:", &server->user, &server->password, &port);
Glenn L McGrath236e93d2003-12-20 05:43:34 +0000332
333 /* Process the non-option command line arguments */
334 if (argc - optind != 3) {
335 bb_show_usage();
336 }
337
Glenn L McGrathb51eb262003-12-19 10:37:52 +0000338 if (opt & FTPGETPUT_OPT_CONTINUE) {
339 do_continue = 1;
340 }
341 if (opt & FTPGETPUT_OPT_VERBOSE) {
342 verbose_flag = 1;
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000343 }
344
Eric Andersen04d055f2003-11-03 21:20:18 +0000345 /* We want to do exactly _one_ DNS lookup, since some
346 * sites (i.e. ftp.us.debian.org) use round-robin DNS
347 * and we want to connect to only one IP... */
348 server->s_in = &s_in;
Glenn L McGrath266c1f52003-12-20 03:19:27 +0000349 bb_lookup_host(&s_in, argv[optind]);
Glenn L McGrath036dbaa2004-01-17 05:03:31 +0000350 s_in.sin_port = bb_lookup_port(port, "tcp", 21);
Eric Andersen04d055f2003-11-03 21:20:18 +0000351 if (verbose_flag) {
Glenn L McGrath266c1f52003-12-20 03:19:27 +0000352 printf("Connecting to %s[%s]:%d\n",
353 argv[optind], inet_ntoa(s_in.sin_addr), ntohs(s_in.sin_port));
Eric Andersen04d055f2003-11-03 21:20:18 +0000354 }
355
356 /* Connect/Setup/Configure the FTP session */
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000357 control_stream = ftp_login(server);
358
Eric Andersen04d055f2003-11-03 21:20:18 +0000359 return(ftp_action(server, control_stream, argv[optind + 1], argv[optind + 2]));
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000360}
361
362/*
363Local Variables:
364c-file-style: "linux"
365c-basic-offset: 4
366tab-width: 4
367End:
368*/