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