1d5b186248867c5b452e72ebf36da6f9e7865c7e
[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@iinet.net.au>
9  *
10  * Based on wget.c by Chip Rosenthal Covad Communications
11  * <chip@laserlink.net>
12  *
13  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
14  */
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>
33 #include <sys/socket.h>
34 #include <arpa/inet.h>
35
36 #include "busybox.h"
37
38 typedef struct ftp_host_info_s {
39         char *user;
40         char *password;
41         struct sockaddr_in *s_in;
42 } ftp_host_info_t;
43
44 static char verbose_flag = 0;
45 static char do_continue = 0;
46
47 static int ftpcmd(const char *s1, const char *s2, FILE *stream, char *buf)
48 {
49         if (verbose_flag) {
50                 bb_error_msg("cmd %s%s", s1, s2);
51         }
52
53         if (s1) {
54                 if (s2) {
55                         fprintf(stream, "%s%s\r\n", s1, s2);
56                 } else {
57                         fprintf(stream, "%s\r\n", s1);
58                 }
59         }
60         do {
61                 char *buf_ptr;
62
63                 if (fgets(buf, 510, stream) == NULL) {
64                         bb_perror_msg_and_die("fgets()");
65                 }
66                 buf_ptr = strstr(buf, "\r\n");
67                 if (buf_ptr) {
68                         *buf_ptr = '\0';
69                 }
70         } while (! isdigit(buf[0]) || buf[3] != ' ');
71
72         return atoi(buf);
73 }
74
75 static int xconnect_ftpdata(ftp_host_info_t *server, const char *buf)
76 {
77         char *buf_ptr;
78         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
88         server->s_in->sin_port=htons(port_num);
89         return(xconnect(server->s_in));
90 }
91
92 static FILE *ftp_login(ftp_host_info_t *server)
93 {
94         FILE *control_stream;
95         char buf[512];
96
97         /* Connect to the command socket */
98         control_stream = fdopen(xconnect(server->s_in), "r+");
99         if (control_stream == NULL) {
100                 bb_perror_msg_and_die("Couldnt open control stream");
101         }
102
103         if (ftpcmd(NULL, NULL, control_stream, buf) != 220) {
104                 bb_error_msg_and_die("%s", buf + 4);
105         }
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) {
113                         bb_error_msg_and_die("PASS error: %s", buf + 4);
114                 }
115                 break;
116         default:
117                 bb_error_msg_and_die("USER error: %s", buf + 4);
118         }
119
120         ftpcmd("TYPE I", NULL, control_stream, buf);
121
122         return(control_stream);
123 }
124
125 #ifdef CONFIG_FTPGET
126 static int ftp_recieve(ftp_host_info_t *server, FILE *control_stream,
127                 const char *local_path, char *server_path)
128 {
129         char buf[512];
130         off_t filesize = 0;
131         int fd_data;
132         int fd_local = -1;
133         off_t beg_range = 0;
134
135         /* Connect to the data socket */
136         if (ftpcmd("PASV", NULL, control_stream, buf) != 227) {
137                 bb_error_msg_and_die("PASV error: %s", buf + 4);
138         }
139         fd_data = xconnect_ftpdata(server, buf);
140
141         if (ftpcmd("SIZE ", server_path, control_stream, buf) == 213) {
142                 unsigned long value=filesize;
143                 if (safe_strtoul(buf + 4, &value))
144                         bb_error_msg_and_die("SIZE error: %s", buf + 4);
145                 filesize = value;
146         }
147
148         if ((local_path[0] == '-') && (local_path[1] == '\0')) {
149                 fd_local = STDOUT_FILENO;
150                 do_continue = 0;
151         }
152
153         if (do_continue) {
154                 struct stat sbuf;
155                 if (lstat(local_path, &sbuf) < 0) {
156                         bb_perror_msg_and_die("fstat()");
157                 }
158                 if (sbuf.st_size > 0) {
159                         beg_range = sbuf.st_size;
160                 } else {
161                         do_continue = 0;
162                 }
163         }
164
165         if (do_continue) {
166                 sprintf(buf, "REST %ld", (long)beg_range);
167                 if (ftpcmd(buf, NULL, control_stream, buf) != 350) {
168                         do_continue = 0;
169                 } else {
170                         filesize -= beg_range;
171                 }
172         }
173
174         if (ftpcmd("RETR ", server_path, control_stream, buf) > 150) {
175                 bb_error_msg_and_die("RETR error: %s", buf + 4);
176         }
177
178         /* only make a local file if we know that one exists on the remote server */
179         if (fd_local == -1) {
180                 if (do_continue) {
181                         fd_local = bb_xopen(local_path, O_APPEND | O_WRONLY);
182                 } else {
183                         fd_local = bb_xopen(local_path, O_CREAT | O_TRUNC | O_WRONLY);
184                 }
185         }
186
187         /* Copy the file */
188         if (bb_copyfd_size(fd_data, fd_local, filesize) == -1) {
189                 exit(EXIT_FAILURE);
190         }
191
192         /* close it all down */
193         close(fd_data);
194         if (ftpcmd(NULL, NULL, control_stream, buf) != 226) {
195                 bb_error_msg_and_die("ftp error: %s", buf + 4);
196         }
197         ftpcmd("QUIT", NULL, control_stream, buf);
198
199         return(EXIT_SUCCESS);
200 }
201 #endif
202
203 #ifdef CONFIG_FTPPUT
204 static int ftp_send(ftp_host_info_t *server, FILE *control_stream,
205                 const char *server_path, char *local_path)
206 {
207         struct stat sbuf;
208         char buf[512];
209         int fd_data;
210         int fd_local;
211         int response;
212
213         /*  Connect to the data socket */
214         if (ftpcmd("PASV", NULL, control_stream, buf) != 227) {
215                 bb_error_msg_and_die("PASV error: %s", buf + 4);
216         }
217         fd_data = xconnect_ftpdata(server, buf);
218
219         /* get the local file */
220         if ((local_path[0] == '-') && (local_path[1] == '\0')) {
221                 fd_local = STDIN_FILENO;
222         } else {
223                 fd_local = bb_xopen(local_path, O_RDONLY);
224                 fstat(fd_local, &sbuf);
225
226                 sprintf(buf, "ALLO %lu", (unsigned long)sbuf.st_size);
227                 response = ftpcmd(buf, NULL, control_stream, buf);
228                 switch (response) {
229                 case 200:
230                 case 202:
231                         break;
232                 default:
233                         close(fd_local);
234                         bb_error_msg_and_die("ALLO error: %s", buf + 4);
235                         break;
236                 }
237         }
238         response = ftpcmd("STOR ", server_path, control_stream, buf);
239         switch (response) {
240         case 125:
241         case 150:
242                 break;
243         default:
244                 close(fd_local);
245                 bb_error_msg_and_die("STOR error: %s", buf + 4);
246         }
247
248         /* transfer the file  */
249         if (bb_copyfd_eof(fd_local, fd_data) == -1) {
250                 exit(EXIT_FAILURE);
251         }
252
253         /* close it all down */
254         close(fd_data);
255         if (ftpcmd(NULL, NULL, control_stream, buf) != 226) {
256                 bb_error_msg_and_die("error: %s", buf + 4);
257         }
258         ftpcmd("QUIT", NULL, control_stream, buf);
259
260         return(EXIT_SUCCESS);
261 }
262 #endif
263
264 #define FTPGETPUT_OPT_CONTINUE  1
265 #define FTPGETPUT_OPT_VERBOSE   2
266 #define FTPGETPUT_OPT_USER      4
267 #define FTPGETPUT_OPT_PASSWORD  8
268 #define FTPGETPUT_OPT_PORT      16
269
270 static const struct option ftpgetput_long_options[] = {
271         {"continue", 1, NULL, 'c'},
272         {"verbose", 0, NULL, 'v'},
273         {"username", 1, NULL, 'u'},
274         {"password", 1, NULL, 'p'},
275         {"port", 1, NULL, 'P'},
276         {0, 0, 0, 0}
277 };
278
279 int ftpgetput_main(int argc, char **argv)
280 {
281         /* content-length of the file */
282         unsigned long opt;
283         char *port = "ftp";
284
285         /* socket to ftp server */
286         FILE *control_stream;
287         struct sockaddr_in s_in;
288
289         /* continue a prev transfer (-c) */
290         ftp_host_info_t *server;
291
292         int (*ftp_action)(ftp_host_info_t *, FILE *, const char *, char *) = NULL;
293
294         /* Check to see if the command is ftpget or ftput */
295 #ifdef CONFIG_FTPPUT
296 # ifdef CONFIG_FTPGET
297         if (bb_applet_name[3] == 'p') {
298                 ftp_action = ftp_send;
299         }
300 # else
301         ftp_action = ftp_send;
302 # endif
303 #endif
304 #ifdef CONFIG_FTPGET
305 # ifdef CONFIG_FTPPUT
306         if (bb_applet_name[3] == 'g') {
307                 ftp_action = ftp_recieve;
308         }
309 # else
310         ftp_action = ftp_recieve;
311 # endif
312 #endif
313
314         /* Set default values */
315         server = xmalloc(sizeof(ftp_host_info_t));
316         server->user = "anonymous";
317         server->password = "busybox@";
318         verbose_flag = 0;
319
320         /*
321          * Decipher the command line
322          */
323         bb_applet_long_options = ftpgetput_long_options;
324         opt = bb_getopt_ulflags(argc, argv, "cvu:p:P:", &server->user, &server->password, &port);
325
326         /* Process the non-option command line arguments */
327         if (argc - optind != 3) {
328                 bb_show_usage();
329         }
330
331         if (opt & FTPGETPUT_OPT_CONTINUE) {
332                 do_continue = 1;
333         }
334         if (opt & FTPGETPUT_OPT_VERBOSE) {
335                 verbose_flag = 1;
336         }
337
338         /* We want to do exactly _one_ DNS lookup, since some
339          * sites (i.e. ftp.us.debian.org) use round-robin DNS
340          * and we want to connect to only one IP... */
341         server->s_in = &s_in;
342         bb_lookup_host(&s_in, argv[optind]);
343         s_in.sin_port = bb_lookup_port(port, "tcp", 21);
344         if (verbose_flag) {
345                 printf("Connecting to %s[%s]:%d\n",
346                                 argv[optind], inet_ntoa(s_in.sin_addr), ntohs(s_in.sin_port));
347         }
348
349         /*  Connect/Setup/Configure the FTP session */
350         control_stream = ftp_login(server);
351
352         return(ftp_action(server, control_stream, argv[optind + 1], argv[optind + 2]));
353 }
354
355 /*
356 Local Variables:
357 c-file-style: "linux"
358 c-basic-offset: 4
359 tab-width: 4
360 End:
361 */