8724739b55fc0b3c4cd76b4e88efd196b04dd70e
[oweals/busybox.git] / networking / ftpgetput.c
1 /* 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>
46 #include <sys/socket.h>
47 #include <arpa/inet.h>
48
49 #include "busybox.h"
50
51 typedef struct ftp_host_info_s {
52         char *host;
53         char *port;
54         char *user;
55         char *password;
56         struct sockaddr_in *s_in;
57 } ftp_host_info_t;
58
59 static char verbose_flag;
60 static char do_continue = 0;
61
62 static 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
80 static int ftpcmd(const char *s1, const char *s2, FILE *stream, char *buf)
81 {
82         if (verbose_flag) {
83                 bb_error_msg("cmd %s%s", s1, s2);
84         }
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) {
96                         bb_perror_msg_and_die("fgets()");
97                 }
98         } while (! isdigit(buf[0]) || buf[3] != ' ');
99
100         return atoi(buf);
101 }
102
103 static int xconnect_ftpdata(ftp_host_info_t *server, const char *buf)
104 {
105         char *buf_ptr;
106         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
116         server->s_in->sin_port=htons(port_num);
117         return(xconnect(server->s_in));
118 }
119
120 static 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 */
127         control_fd = xconnect(server->s_in);
128         control_stream = fdopen(control_fd, "r+");
129         if (control_stream == NULL) {
130                 bb_perror_msg_and_die("Couldnt open control stream");
131         }
132
133         if (ftpcmd(NULL, NULL, control_stream, buf) != 220) {
134                 bb_error_msg_and_die("%s", buf + 4);
135         }
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) {
143                         bb_error_msg_and_die("PASS error: %s", buf + 4);
144                 }
145                 break;
146         default:
147                 bb_error_msg_and_die("USER error: %s", buf + 4);
148         }
149
150         ftpcmd("TYPE I", NULL, control_stream, buf);
151
152         return(control_stream);
153 }
154
155 #ifdef CONFIG_FTPGET
156 static int ftp_recieve(ftp_host_info_t *server, FILE *control_stream, 
157                 const char *local_path, char *server_path)
158 {
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
167         filename = bb_get_last_path_component(server_path);
168         local_file = concat_path_file(local_path, filename);
169
170         /* Connect to the data socket */
171         if (ftpcmd("PASV", NULL, control_stream, buf) != 227) {
172                 bb_error_msg_and_die("PASV error: %s", buf + 4);
173         }
174         fd_data = xconnect_ftpdata(server, buf);
175
176         if (ftpcmd("SIZE ", server_path, control_stream, buf) == 213) {
177                 filesize = atol(buf + 4);
178         }
179
180         if (do_continue) {
181                 struct stat sbuf;
182                 if (lstat(local_file, &sbuf) < 0) {
183                         bb_perror_msg_and_die("fstat()");
184                 }
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) {
193                 sprintf(buf, "REST %ld", (long)beg_range);
194                 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) {
202                 bb_error_msg_and_die("RETR error: %s", buf + 4);
203         }
204
205         /* only make a local file if we know that one exists on the remote server */
206         if (do_continue) {
207                 fd_local = bb_xopen(local_file, O_APPEND | O_WRONLY);
208         } else {
209                 fd_local = bb_xopen(local_file, O_CREAT | O_TRUNC | O_WRONLY);
210         }
211
212         /* Copy the file */
213         if (bb_copyfd_size(fd_data, fd_local, filesize) == -1) {
214                 exit(EXIT_FAILURE);
215         }
216
217         /* close it all down */
218         close(fd_data);
219         if (ftpcmd(NULL, NULL, control_stream, buf) != 226) {
220                 bb_error_msg_and_die("ftp error: %s", buf + 4);
221         }
222         ftpcmd("QUIT", NULL, control_stream, buf);
223         
224         return(EXIT_SUCCESS);
225 }
226 #endif
227
228 #ifdef CONFIG_FTPPUT
229 static int ftp_send(ftp_host_info_t *server, FILE *control_stream, 
230                 const char *server_path, char *local_path)
231 {
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) {
240                 bb_error_msg_and_die("PASV error: %s", buf + 4);
241         }
242         fd_data = xconnect_ftpdata(server, buf);
243
244         if (ftpcmd("CWD ", server_path, control_stream, buf) != 250) {
245                 bb_error_msg_and_die("CWD error: %s", buf + 4);
246         }
247
248         /* get the local file */
249         fd_local = bb_xopen(local_path, O_RDONLY);
250         fstat(fd_local, &sbuf);
251
252         sprintf(buf, "ALLO %lu", (unsigned long)sbuf.st_size);
253         response = ftpcmd(buf, NULL, control_stream, buf);
254         switch (response) {
255         case 200:
256         case 202:
257                 break;
258         default:
259                 close(fd_local);
260                 bb_error_msg_and_die("ALLO error: %s", buf + 4);
261                 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);
271                 bb_error_msg_and_die("STOR error: %s", buf + 4);
272         }
273
274         /* transfer the file  */
275         if (bb_copyfd_eof(fd_local, fd_data) == -1) {
276                 exit(EXIT_FAILURE);
277         }
278
279         /* close it all down */
280         close(fd_data);
281         if (ftpcmd(NULL, NULL, control_stream, buf) != 226) {
282                 bb_error_msg_and_die("error: %s", buf + 4);
283         }
284         ftpcmd("QUIT", NULL, control_stream, buf);
285
286         return(EXIT_SUCCESS);
287 }
288 #endif
289
290 #define FTPGETPUT_OPT_CONTINUE  1
291 #define FTPGETPUT_OPT_VERBOSE   2
292 #define FTPGETPUT_OPT_USER      4
293 #define FTPGETPUT_OPT_PASSWORD  8
294 #define FTPGETPUT_OPT_PORT      16
295
296 static const struct option ftpgetput_long_options[] = {
297         {"continue", 1, NULL, 'c'},
298         {"verbose", 0, NULL, 'v'},
299         {"username", 1, NULL, 'u'},
300         {"password", 1, NULL, 'p'},
301         {"port", 1, NULL, 'P'},
302         {0, 0, 0, 0}
303 };
304
305 int ftpgetput_main(int argc, char **argv)
306 {
307         /* content-length of the file */
308         unsigned long opt;
309
310         /* socket to ftp server */
311         FILE *control_stream;
312         struct sockaddr_in s_in;
313
314         /* continue a prev transfer (-c) */
315         ftp_host_info_t *server;
316
317         int (*ftp_action)(ftp_host_info_t *, FILE *, const char *, char *) = NULL;
318
319 #ifdef CONFIG_FTPPUT
320         if (bb_applet_name[3] == 'p') {
321                 ftp_action = ftp_send;
322         } 
323 #endif
324 #ifdef CONFIG_FTPGET
325         if (bb_applet_name[3] == 'g') {
326                 ftp_action = ftp_recieve;
327         }
328 #endif
329
330         /* Set default values */
331         server = ftp_init();
332         verbose_flag = 0;
333
334         /* 
335          * Decipher the command line 
336          */
337         server->port = "21";
338         bb_applet_long_options = ftpgetput_long_options;
339         opt = bb_getopt_ulflags(argc, argv, "cvu:p:P:", &server->user, &server->password, &server->port);
340         if (opt & FTPGETPUT_OPT_CONTINUE) {
341                 do_continue = 1;
342         }
343         if (opt & FTPGETPUT_OPT_VERBOSE) {
344                 verbose_flag = 1;
345         }
346
347         /*
348          * Process the non-option command line arguments
349          */
350         if (argc - optind != 3) {
351                 bb_show_usage();
352         }
353
354         /* We want to do exactly _one_ DNS lookup, since some
355          * sites (i.e. ftp.us.debian.org) use round-robin DNS
356          * and we want to connect to only one IP... */
357         server->s_in = &s_in;
358         server->host = argv[optind];
359         bb_lookup_host(&s_in, server->host, NULL);
360         if (verbose_flag) {
361                 fprintf(stdout, "Connecting to %s[%s]:%s\n",
362                                 server->host, inet_ntoa(s_in.sin_addr), server->port);
363         }
364
365         /*  Connect/Setup/Configure the FTP session */
366         control_stream = ftp_login(server);
367
368         return(ftp_action(server, control_stream, argv[optind + 1], argv[optind + 2]));
369 }
370
371 /*
372 Local Variables:
373 c-file-style: "linux"
374 c-basic-offset: 4
375 tab-width: 4
376 End:
377 */