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