blob: 2fd50e4c4aca0fc7851e38319f1bd1a4c4025dc3 [file] [log] [blame]
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +00001/* vi: set sw=4 ts=4: */
2/*
3 * ftpget
4 *
5 * Mini implementation of FTP to retrieve a remote file.
6 *
7 * Copyright (C) 2002 Jeff Angielski, The PTR Group <jeff@theptrgroup.com>
8 * Copyright (C) 2002 Glenn McGrath <bug1@optushome.com.au>
9 *
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 {
52 char *host;
53 char *port;
54 char *user;
55 char *password;
Eric Andersen04d055f2003-11-03 21:20:18 +000056 struct sockaddr_in *s_in;
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +000057} ftp_host_info_t;
58
59static char verbose_flag;
60static char do_continue = 0;
61
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +000062static ftp_host_info_t *ftp_init(void)
63{
64 ftp_host_info_t *host;
65
66 host = xcalloc(1, sizeof(ftp_host_info_t));
67
68 /* Set the default port */
69 if (getservbyname("ftp", "tcp")) {
70 host->port = "ftp";
71 } else {
72 host->port = "21";
73 }
74 host->user = "anonymous";
75 host->password = "busybox@";
76
77 return(host);
78}
79
80static int ftpcmd(const char *s1, const char *s2, FILE *stream, char *buf)
81{
82 if (verbose_flag) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000083 bb_error_msg("cmd %s%s", s1, s2);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +000084 }
85
86 if (s1) {
87 if (s2) {
88 fprintf(stream, "%s%s\n", s1, s2);
89 } else {
90 fprintf(stream, "%s\n", s1);
91 }
92 }
93
94 do {
95 if (fgets(buf, 510, stream) == NULL) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000096 bb_perror_msg_and_die("fgets()");
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +000097 }
98 } while (! isdigit(buf[0]) || buf[3] != ' ');
99
100 return atoi(buf);
101}
102
Eric Andersen04d055f2003-11-03 21:20:18 +0000103static int xconnect_ftpdata(ftp_host_info_t *server, const char *buf)
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000104{
105 char *buf_ptr;
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000106 unsigned short port_num;
107
108 buf_ptr = strrchr(buf, ',');
109 *buf_ptr = '\0';
110 port_num = atoi(buf_ptr + 1);
111
112 buf_ptr = strrchr(buf, ',');
113 *buf_ptr = '\0';
114 port_num += atoi(buf_ptr + 1) * 256;
115
Eric Andersen04d055f2003-11-03 21:20:18 +0000116 server->s_in->sin_port=htons(port_num);
117 return(xconnect(server->s_in));
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000118}
119
120static FILE *ftp_login(ftp_host_info_t *server)
121{
122 FILE *control_stream;
123 char buf[512];
124 int control_fd;
125
126 /* Connect to the command socket */
Eric Andersen04d055f2003-11-03 21:20:18 +0000127 control_fd = xconnect(server->s_in);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000128 control_stream = fdopen(control_fd, "r+");
129 if (control_stream == NULL) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000130 bb_perror_msg_and_die("Couldnt open control stream");
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000131 }
132
133 if (ftpcmd(NULL, NULL, control_stream, buf) != 220) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000134 bb_error_msg_and_die("%s", buf + 4);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000135 }
136
137 /* Login to the server */
138 switch (ftpcmd("USER ", server->user, control_stream, buf)) {
139 case 230:
140 break;
141 case 331:
142 if (ftpcmd("PASS ", server->password, control_stream, buf) != 230) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000143 bb_error_msg_and_die("PASS error: %s", buf + 4);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000144 }
145 break;
146 default:
Manuel Novoa III cad53642003-03-19 09:13:01 +0000147 bb_error_msg_and_die("USER error: %s", buf + 4);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000148 }
149
150 ftpcmd("TYPE I", NULL, control_stream, buf);
151
152 return(control_stream);
153}
154
155#ifdef CONFIG_FTPGET
Eric Andersen04d055f2003-11-03 21:20:18 +0000156static int ftp_recieve(ftp_host_info_t *server, FILE *control_stream,
157 const char *local_path, char *server_path)
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000158{
159 char *filename;
160 char *local_file;
161 char buf[512];
162 off_t filesize = 0;
163 int fd_data;
164 int fd_local;
165 off_t beg_range = 0;
166
Manuel Novoa III cad53642003-03-19 09:13:01 +0000167 filename = bb_get_last_path_component(server_path);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000168 local_file = concat_path_file(local_path, filename);
169
170 /* Connect to the data socket */
171 if (ftpcmd("PASV", NULL, control_stream, buf) != 227) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000172 bb_error_msg_and_die("PASV error: %s", buf + 4);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000173 }
Eric Andersen04d055f2003-11-03 21:20:18 +0000174 fd_data = xconnect_ftpdata(server, buf);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000175
176 if (ftpcmd("SIZE ", server_path, control_stream, buf) == 213) {
177 filesize = atol(buf + 4);
178 }
179
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000180 if (do_continue) {
181 struct stat sbuf;
Glenn L McGrath1643f412002-12-18 02:47:40 +0000182 if (lstat(local_file, &sbuf) < 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000183 bb_perror_msg_and_die("fstat()");
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000184 }
185 if (sbuf.st_size > 0) {
186 beg_range = sbuf.st_size;
187 } else {
188 do_continue = 0;
189 }
190 }
191
192 if (do_continue) {
Eric Andersen2a41ec62003-06-20 09:22:12 +0000193 sprintf(buf, "REST %ld", (long)beg_range);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000194 if (ftpcmd(buf, NULL, control_stream, buf) != 350) {
195 do_continue = 0;
196 } else {
197 filesize -= beg_range;
198 }
199 }
200
201 if (ftpcmd("RETR ", server_path, control_stream, buf) > 150) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000202 bb_error_msg_and_die("RETR error: %s", buf + 4);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000203 }
204
Glenn L McGrath1643f412002-12-18 02:47:40 +0000205 /* only make a local file if we know that one exists on the remote server */
206 if (do_continue) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000207 fd_local = bb_xopen(local_file, O_APPEND | O_WRONLY);
Glenn L McGrath1643f412002-12-18 02:47:40 +0000208 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000209 fd_local = bb_xopen(local_file, O_CREAT | O_TRUNC | O_WRONLY);
Glenn L McGrath1643f412002-12-18 02:47:40 +0000210 }
211
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000212 /* Copy the file */
Glenn L McGrath7ffe1332003-11-21 22:24:57 +0000213 if (bb_copyfd_size(fd_data, fd_local, filesize) == -1) {
Glenn L McGrath25fe94f2002-12-13 08:20:44 +0000214 exit(EXIT_FAILURE);
215 }
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000216
217 /* close it all down */
218 close(fd_data);
219 if (ftpcmd(NULL, NULL, control_stream, buf) != 226) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000220 bb_error_msg_and_die("ftp error: %s", buf + 4);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000221 }
222 ftpcmd("QUIT", NULL, control_stream, buf);
223
224 return(EXIT_SUCCESS);
225}
226#endif
227
228#ifdef CONFIG_FTPPUT
Eric Andersen04d055f2003-11-03 21:20:18 +0000229static int ftp_send(ftp_host_info_t *server, FILE *control_stream,
230 const char *server_path, char *local_path)
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000231{
232 struct stat sbuf;
233 char buf[512];
234 int fd_data;
235 int fd_local;
236 int response;
237
238 /* Connect to the data socket */
239 if (ftpcmd("PASV", NULL, control_stream, buf) != 227) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000240 bb_error_msg_and_die("PASV error: %s", buf + 4);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000241 }
Eric Andersen04d055f2003-11-03 21:20:18 +0000242 fd_data = xconnect_ftpdata(server, buf);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000243
244 if (ftpcmd("CWD ", server_path, control_stream, buf) != 250) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000245 bb_error_msg_and_die("CWD error: %s", buf + 4);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000246 }
247
248 /* get the local file */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000249 fd_local = bb_xopen(local_path, O_RDONLY);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000250 fstat(fd_local, &sbuf);
251
Eric Andersen2a41ec62003-06-20 09:22:12 +0000252 sprintf(buf, "ALLO %lu", (unsigned long)sbuf.st_size);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000253 response = ftpcmd(buf, NULL, control_stream, buf);
254 switch (response) {
255 case 200:
256 case 202:
257 break;
258 default:
259 close(fd_local);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000260 bb_error_msg_and_die("ALLO error: %s", buf + 4);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000261 break;
262 }
263
264 response = ftpcmd("STOR ", local_path, control_stream, buf);
265 switch (response) {
266 case 125:
267 case 150:
268 break;
269 default:
270 close(fd_local);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000271 bb_error_msg_and_die("STOR error: %s", buf + 4);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000272 }
273
274 /* transfer the file */
Glenn L McGrath7ffe1332003-11-21 22:24:57 +0000275 if (bb_copyfd_eof(fd_local, fd_data) == -1) {
Glenn L McGrath25fe94f2002-12-13 08:20:44 +0000276 exit(EXIT_FAILURE);
277 }
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000278
279 /* close it all down */
280 close(fd_data);
281 if (ftpcmd(NULL, NULL, control_stream, buf) != 226) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000282 bb_error_msg_and_die("error: %s", buf + 4);
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000283 }
284 ftpcmd("QUIT", NULL, control_stream, buf);
285
286 return(EXIT_SUCCESS);
287}
288#endif
289
290int ftpgetput_main(int argc, char **argv)
291{
292 /* content-length of the file */
293 int option_index = -1;
294 int opt;
295
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
305 struct option long_options[] = {
306 {"username", 1, NULL, 'u'},
307 {"password", 1, NULL, 'p'},
308 {"port", 1, NULL, 'P'},
309 {"continue", 1, NULL, 'c'},
310 {"verbose", 0, NULL, 'v'},
311 {0, 0, 0, 0}
312 };
313
314#ifdef CONFIG_FTPPUT
Manuel Novoa III cad53642003-03-19 09:13:01 +0000315 if (bb_applet_name[3] == 'p') {
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000316 ftp_action = ftp_send;
317 }
318#endif
319#ifdef CONFIG_FTPGET
Manuel Novoa III cad53642003-03-19 09:13:01 +0000320 if (bb_applet_name[3] == 'g') {
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000321 ftp_action = ftp_recieve;
322 }
323#endif
324
325 /* Set default values */
326 server = ftp_init();
327 verbose_flag = 0;
328
329 /*
330 * Decipher the command line
331 */
Eric Andersen04d055f2003-11-03 21:20:18 +0000332 server->port = "21";
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000333 while ((opt = getopt_long(argc, argv, "u:p:P:cv", long_options, &option_index)) != EOF) {
334 switch(opt) {
335 case 'c':
336 do_continue = 1;
337 break;
338 case 'u':
339 server->user = optarg;
340 break;
341 case 'p':
342 server->password = optarg;
343 break;
344 case 'P':
345 server->port = optarg;
346 break;
347 case 'v':
348 verbose_flag = 1;
349 break;
350 default:
Manuel Novoa III cad53642003-03-19 09:13:01 +0000351 bb_show_usage();
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000352 }
353 }
354
355 /*
356 * Process the non-option command line arguments
357 */
358 if (argc - optind != 3) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000359 bb_show_usage();
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000360 }
361
Eric Andersen04d055f2003-11-03 21:20:18 +0000362 /* We want to do exactly _one_ DNS lookup, since some
363 * sites (i.e. ftp.us.debian.org) use round-robin DNS
364 * and we want to connect to only one IP... */
365 server->s_in = &s_in;
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000366 server->host = argv[optind];
Eric Andersen04d055f2003-11-03 21:20:18 +0000367 bb_lookup_host(&s_in, server->host, NULL);
368 if (verbose_flag) {
369 fprintf(stdout, "Connecting to %s[%s]:%s\n",
370 server->host, inet_ntoa(s_in.sin_addr), server->port);
371 }
372
373 /* Connect/Setup/Configure the FTP session */
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000374 control_stream = ftp_login(server);
375
Eric Andersen04d055f2003-11-03 21:20:18 +0000376 return(ftp_action(server, control_stream, argv[optind + 1], argv[optind + 2]));
Glenn L McGrath02d7cbf2002-12-13 02:43:50 +0000377}
378
379/*
380Local Variables:
381c-file-style: "linux"
382c-basic-offset: 4
383tab-width: 4
384End:
385*/