blob: 2ef0b2c44f192c2d38347eff48e06cbc6588f238 [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 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 * General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 *
27 */
28
29#include <sys/types.h>
30#include <sys/ioctl.h>
31#include <sys/time.h>
32#include <sys/stat.h>
33
34#include <ctype.h>
35#include <errno.h>
36#include <fcntl.h>
37#include <getopt.h>
38#include <stdio.h>
39#include <stdlib.h>
40#include <signal.h>
41#include <string.h>
42#include <unistd.h>
43
44#include <netinet/in.h>
45#include <netdb.h>
Eric Andersen04d055f2003-11-03 21:20:18 +000046#include <sys/socket.h>
47#include <arpa/inet.h>
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +000048
49#include "busybox.h"
50
51typedef struct ftp_host_info_s {
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +000052 char *user;
53 char *password;
Eric Andersen04d055f2003-11-03 21:20:18 +000054 struct sockaddr_in *s_in;
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +000055} ftp_host_info_t;
56
Glenn L McGrath266c1f52003-12-20 03:19:27 +000057static char verbose_flag = 0;
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +000058static char do_continue = 0;
59
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +000060static int ftpcmd(const char *s1, const char *s2, FILE *stream, char *buf)
61{
62 if (verbose_flag) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000063 bb_error_msg("cmd %s%s", s1, s2);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +000064 }
65
66 if (s1) {
67 if (s2) {
68 fprintf(stream, "%s%s\n", s1, s2);
69 } else {
70 fprintf(stream, "%s\n", s1);
71 }
72 }
73
74 do {
75 if (fgets(buf, 510, stream) == NULL) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000076 bb_perror_msg_and_die("fgets()");
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +000077 }
78 } while (! isdigit(buf[0]) || buf[3] != ' ');
79
80 return atoi(buf);
81}
82
Eric Andersen04d055f2003-11-03 21:20:18 +000083static int xconnect_ftpdata(ftp_host_info_t *server, const char *buf)
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +000084{
85 char *buf_ptr;
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +000086 unsigned short port_num;
87
88 buf_ptr = strrchr(buf, ',');
89 *buf_ptr = '\0';
90 port_num = atoi(buf_ptr + 1);
91
92 buf_ptr = strrchr(buf, ',');
93 *buf_ptr = '\0';
94 port_num += atoi(buf_ptr + 1) * 256;
95
Eric Andersen04d055f2003-11-03 21:20:18 +000096 server->s_in->sin_port=htons(port_num);
97 return(xconnect(server->s_in));
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +000098}
99
100static FILE *ftp_login(ftp_host_info_t *server)
101{
102 FILE *control_stream;
103 char buf[512];
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000104
105 /* Connect to the command socket */
Glenn L McGrath236e93d2003-12-20 05:43:34 +0000106 control_stream = fdopen(xconnect(server->s_in), "r+");
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000107 if (control_stream == NULL) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000108 bb_perror_msg_and_die("Couldnt open control stream");
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000109 }
110
111 if (ftpcmd(NULL, NULL, control_stream, buf) != 220) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000112 bb_error_msg_and_die("%s", buf + 4);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000113 }
114
115 /* Login to the server */
116 switch (ftpcmd("USER ", server->user, control_stream, buf)) {
117 case 230:
118 break;
119 case 331:
120 if (ftpcmd("PASS ", server->password, control_stream, buf) != 230) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000121 bb_error_msg_and_die("PASS error: %s", buf + 4);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000122 }
123 break;
124 default:
Manuel Novoa III cad53642003-03-19 09:13:01 +0000125 bb_error_msg_and_die("USER error: %s", buf + 4);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000126 }
127
128 ftpcmd("TYPE I", NULL, control_stream, buf);
129
130 return(control_stream);
131}
132
133#ifdef CONFIG_FTPGET
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000134static int ftp_recieve(ftp_host_info_t *server, FILE *control_stream,
Eric Andersen04d055f2003-11-03 21:20:18 +0000135 const char *local_path, char *server_path)
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000136{
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000137 char buf[512];
138 off_t filesize = 0;
139 int fd_data;
Glenn L McGrath236e93d2003-12-20 05:43:34 +0000140 int fd_local = -1;
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000141 off_t beg_range = 0;
142
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000143 /* Connect to the data socket */
144 if (ftpcmd("PASV", NULL, control_stream, buf) != 227) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000145 bb_error_msg_and_die("PASV error: %s", buf + 4);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000146 }
Eric Andersen04d055f2003-11-03 21:20:18 +0000147 fd_data = xconnect_ftpdata(server, buf);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000148
149 if (ftpcmd("SIZE ", server_path, control_stream, buf) == 213) {
Eric Andersen24794452004-03-06 22:11:45 +0000150 unsigned long value=filesize;
Eric Andersenca65ca72004-03-15 08:46:37 +0000151 if (safe_strtoul(buf + 4, &value))
Eric Andersen24794452004-03-06 22:11:45 +0000152 bb_error_msg_and_die("SIZE error: %s", buf + 4);
153 filesize = value;
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000154 }
155
Glenn L McGrath236e93d2003-12-20 05:43:34 +0000156 if ((local_path[0] == '-') && (local_path[1] == '\0')) {
Eric Andersen70060d22004-03-27 10:02:48 +0000157 fd_local = STDOUT_FILENO;
Glenn L McGrath236e93d2003-12-20 05:43:34 +0000158 do_continue = 0;
159 }
160
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000161 if (do_continue) {
162 struct stat sbuf;
Glenn L McGrath236e93d2003-12-20 05:43:34 +0000163 if (lstat(local_path, &sbuf) < 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000164 bb_perror_msg_and_die("fstat()");
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000165 }
166 if (sbuf.st_size > 0) {
167 beg_range = sbuf.st_size;
168 } else {
169 do_continue = 0;
170 }
171 }
172
173 if (do_continue) {
Eric Andersen2a41ec62003-06-20 09:22:12 +0000174 sprintf(buf, "REST %ld", (long)beg_range);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000175 if (ftpcmd(buf, NULL, control_stream, buf) != 350) {
176 do_continue = 0;
177 } else {
178 filesize -= beg_range;
179 }
180 }
181
182 if (ftpcmd("RETR ", server_path, control_stream, buf) > 150) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000183 bb_error_msg_and_die("RETR error: %s", buf + 4);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000184 }
185
Glenn L McGrath1643f412002-12-18 02:47:40 +0000186 /* only make a local file if we know that one exists on the remote server */
Glenn L McGrath236e93d2003-12-20 05:43:34 +0000187 if (fd_local == -1) {
188 if (do_continue) {
189 fd_local = bb_xopen(local_path, O_APPEND | O_WRONLY);
190 } else {
191 fd_local = bb_xopen(local_path, O_CREAT | O_TRUNC | O_WRONLY);
192 }
Glenn L McGrath1643f412002-12-18 02:47:40 +0000193 }
194
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000195 /* Copy the file */
Glenn L McGrath7ffe1332003-11-21 22:24:57 +0000196 if (bb_copyfd_size(fd_data, fd_local, filesize) == -1) {
Glenn L McGrath25fe94f2002-12-13 08:20:44 +0000197 exit(EXIT_FAILURE);
198 }
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000199
200 /* close it all down */
201 close(fd_data);
202 if (ftpcmd(NULL, NULL, control_stream, buf) != 226) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000203 bb_error_msg_and_die("ftp error: %s", buf + 4);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000204 }
205 ftpcmd("QUIT", NULL, control_stream, buf);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000206
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000207 return(EXIT_SUCCESS);
208}
209#endif
210
211#ifdef CONFIG_FTPPUT
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000212static int ftp_send(ftp_host_info_t *server, FILE *control_stream,
Eric Andersen04d055f2003-11-03 21:20:18 +0000213 const char *server_path, char *local_path)
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000214{
215 struct stat sbuf;
216 char buf[512];
217 int fd_data;
218 int fd_local;
219 int response;
220
221 /* Connect to the data socket */
222 if (ftpcmd("PASV", NULL, control_stream, buf) != 227) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000223 bb_error_msg_and_die("PASV error: %s", buf + 4);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000224 }
Eric Andersen04d055f2003-11-03 21:20:18 +0000225 fd_data = xconnect_ftpdata(server, buf);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000226
227 if (ftpcmd("CWD ", server_path, control_stream, buf) != 250) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000228 bb_error_msg_and_die("CWD error: %s", buf + 4);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000229 }
230
231 /* get the local file */
Glenn L McGrath236e93d2003-12-20 05:43:34 +0000232 if ((local_path[0] == '-') && (local_path[1] == '\0')) {
Eric Andersen70060d22004-03-27 10:02:48 +0000233 fd_local = STDIN_FILENO;
Glenn L McGrath236e93d2003-12-20 05:43:34 +0000234 } else {
235 fd_local = bb_xopen(local_path, O_RDONLY);
236 fstat(fd_local, &sbuf);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000237
Glenn L McGrath236e93d2003-12-20 05:43:34 +0000238 sprintf(buf, "ALLO %lu", (unsigned long)sbuf.st_size);
239 response = ftpcmd(buf, NULL, control_stream, buf);
240 switch (response) {
241 case 200:
242 case 202:
243 break;
244 default:
245 close(fd_local);
246 bb_error_msg_and_die("ALLO error: %s", buf + 4);
247 break;
248 }
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000249 }
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000250 response = ftpcmd("STOR ", local_path, control_stream, buf);
251 switch (response) {
252 case 125:
253 case 150:
254 break;
255 default:
256 close(fd_local);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000257 bb_error_msg_and_die("STOR error: %s", buf + 4);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000258 }
259
260 /* transfer the file */
Glenn L McGrath7ffe1332003-11-21 22:24:57 +0000261 if (bb_copyfd_eof(fd_local, fd_data) == -1) {
Glenn L McGrath25fe94f2002-12-13 08:20:44 +0000262 exit(EXIT_FAILURE);
263 }
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000264
265 /* close it all down */
266 close(fd_data);
267 if (ftpcmd(NULL, NULL, control_stream, buf) != 226) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000268 bb_error_msg_and_die("error: %s", buf + 4);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000269 }
270 ftpcmd("QUIT", NULL, control_stream, buf);
271
272 return(EXIT_SUCCESS);
273}
274#endif
275
Glenn L McGrathb51eb262003-12-19 10:37:52 +0000276#define FTPGETPUT_OPT_CONTINUE 1
277#define FTPGETPUT_OPT_VERBOSE 2
278#define FTPGETPUT_OPT_USER 4
279#define FTPGETPUT_OPT_PASSWORD 8
280#define FTPGETPUT_OPT_PORT 16
281
282static const struct option ftpgetput_long_options[] = {
283 {"continue", 1, NULL, 'c'},
284 {"verbose", 0, NULL, 'v'},
285 {"username", 1, NULL, 'u'},
286 {"password", 1, NULL, 'p'},
287 {"port", 1, NULL, 'P'},
288 {0, 0, 0, 0}
289};
290
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000291int ftpgetput_main(int argc, char **argv)
292{
293 /* content-length of the file */
Glenn L McGrathb51eb262003-12-19 10:37:52 +0000294 unsigned long opt;
Glenn L McGrath266c1f52003-12-20 03:19:27 +0000295 char *port = "ftp";
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000296
297 /* socket to ftp server */
298 FILE *control_stream;
Eric Andersen04d055f2003-11-03 21:20:18 +0000299 struct sockaddr_in s_in;
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000300
301 /* continue a prev transfer (-c) */
302 ftp_host_info_t *server;
303
Eric Andersen04d055f2003-11-03 21:20:18 +0000304 int (*ftp_action)(ftp_host_info_t *, FILE *, const char *, char *) = NULL;
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000305
Glenn L McGrath266c1f52003-12-20 03:19:27 +0000306 /* Check to see if the command is ftpget or ftput */
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000307#ifdef CONFIG_FTPPUT
Glenn L McGrath266c1f52003-12-20 03:19:27 +0000308# ifdef CONFIG_FTPGET
Manuel Novoa III cad53642003-03-19 09:13:01 +0000309 if (bb_applet_name[3] == 'p') {
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000310 ftp_action = ftp_send;
Glenn L McGrath266c1f52003-12-20 03:19:27 +0000311 }
312# else
313 ftp_action = ftp_send;
314# endif
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000315#endif
316#ifdef CONFIG_FTPGET
Glenn L McGrath266c1f52003-12-20 03:19:27 +0000317# ifdef CONFIG_FTPPUT
Manuel Novoa III cad53642003-03-19 09:13:01 +0000318 if (bb_applet_name[3] == 'g') {
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000319 ftp_action = ftp_recieve;
320 }
Glenn L McGrath266c1f52003-12-20 03:19:27 +0000321# else
322 ftp_action = ftp_recieve;
323# endif
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000324#endif
325
326 /* Set default values */
Glenn L McGrath266c1f52003-12-20 03:19:27 +0000327 server = xmalloc(sizeof(ftp_host_info_t));
328 server->user = "anonymous";
329 server->password = "busybox@";
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000330 verbose_flag = 0;
331
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000332 /*
333 * Decipher the command line
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000334 */
Glenn L McGrathb51eb262003-12-19 10:37:52 +0000335 bb_applet_long_options = ftpgetput_long_options;
Glenn L McGrath266c1f52003-12-20 03:19:27 +0000336 opt = bb_getopt_ulflags(argc, argv, "cvu:p:P:", &server->user, &server->password, &port);
Glenn L McGrath236e93d2003-12-20 05:43:34 +0000337
338 /* Process the non-option command line arguments */
339 if (argc - optind != 3) {
340 bb_show_usage();
341 }
342
Glenn L McGrathb51eb262003-12-19 10:37:52 +0000343 if (opt & FTPGETPUT_OPT_CONTINUE) {
344 do_continue = 1;
345 }
346 if (opt & FTPGETPUT_OPT_VERBOSE) {
347 verbose_flag = 1;
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000348 }
349
Eric Andersen04d055f2003-11-03 21:20:18 +0000350 /* We want to do exactly _one_ DNS lookup, since some
351 * sites (i.e. ftp.us.debian.org) use round-robin DNS
352 * and we want to connect to only one IP... */
353 server->s_in = &s_in;
Glenn L McGrath266c1f52003-12-20 03:19:27 +0000354 bb_lookup_host(&s_in, argv[optind]);
Glenn L McGrath036dbaa2004-01-17 05:03:31 +0000355 s_in.sin_port = bb_lookup_port(port, "tcp", 21);
Eric Andersen04d055f2003-11-03 21:20:18 +0000356 if (verbose_flag) {
Glenn L McGrath266c1f52003-12-20 03:19:27 +0000357 printf("Connecting to %s[%s]:%d\n",
358 argv[optind], inet_ntoa(s_in.sin_addr), ntohs(s_in.sin_port));
Eric Andersen04d055f2003-11-03 21:20:18 +0000359 }
360
361 /* Connect/Setup/Configure the FTP session */
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000362 control_stream = ftp_login(server);
363
Eric Andersen04d055f2003-11-03 21:20:18 +0000364 return(ftp_action(server, control_stream, argv[optind + 1], argv[optind + 2]));
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000365}
366
367/*
368Local Variables:
369c-file-style: "linux"
370c-basic-offset: 4
371tab-width: 4
372End:
373*/