4cebbc71c3c9e530bc5b923fedb003538c837e10
[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
47 #include "busybox.h"
48
49 typedef struct ftp_host_info_s {
50         char *host;
51         char *port;
52         char *user;
53         char *password;
54 } ftp_host_info_t;
55
56 static char verbose_flag;
57 static char do_continue = 0;
58
59 /* If chunksize == 0 read till end of file */
60 static int copyfd_chunk(int fd1, int fd2, off_t chunksize)
61 {
62         size_t nread;
63         size_t nwritten;
64         size_t size;
65         char buffer[BUFSIZ];
66
67         do {
68                 if ((chunksize > BUFSIZ) || (chunksize == 0)) {
69                         size = BUFSIZ;
70                 } else {
71                         size = chunksize;
72                 }
73
74                 nread = safe_read(fd1, buffer, size);
75
76                 if (nread < 0) {
77                         if (chunksize) {
78                                 perror_msg_and_die("read error");
79                         } else {
80                                 return(0);
81                         }
82                 }
83
84                 nwritten = full_write(fd2, buffer, nread);
85
86                 if (nwritten != nread) {
87                         error_msg_and_die("Unable to write all data");
88                 }
89
90                 if (chunksize) {
91                         chunksize -= nwritten;
92                 }
93
94         } while (chunksize);
95
96         return 0;
97 }
98
99 static ftp_host_info_t *ftp_init(void)
100 {
101         ftp_host_info_t *host;
102
103         host = xcalloc(1, sizeof(ftp_host_info_t));
104
105         /* Set the default port */
106         if (getservbyname("ftp", "tcp")) {
107                 host->port = "ftp";
108         } else {
109                 host->port = "21";
110         }
111         host->user = "anonymous";
112         host->password = "busybox@";
113
114         return(host);
115 }
116
117 static int ftpcmd(const char *s1, const char *s2, FILE *stream, char *buf)
118 {
119         if (verbose_flag) {
120                 error_msg("cmd %s%s", s1, s2);
121         }
122
123         if (s1) {
124                 if (s2) {
125                         fprintf(stream, "%s%s\n", s1, s2);
126                 } else {
127                         fprintf(stream, "%s\n", s1);
128                 }
129         }
130
131         do {
132                 if (fgets(buf, 510, stream) == NULL) {
133                         perror_msg_and_die("fgets()");
134                 }
135         } while (! isdigit(buf[0]) || buf[3] != ' ');
136
137         return atoi(buf);
138 }
139
140 static int xconnect_ftpdata(const char *target_host, const char *buf)
141 {
142         char *buf_ptr;
143         char data_port[6];
144         unsigned short port_num;
145
146         buf_ptr = strrchr(buf, ',');
147         *buf_ptr = '\0';
148         port_num = atoi(buf_ptr + 1);
149
150         buf_ptr = strrchr(buf, ',');
151         *buf_ptr = '\0';
152         port_num += atoi(buf_ptr + 1) * 256;
153
154         sprintf(data_port, "%d", port_num);
155         return(xconnect(target_host, data_port));
156 }
157
158 static FILE *ftp_login(ftp_host_info_t *server)
159 {
160         FILE *control_stream;
161         char buf[512];
162         int control_fd;
163
164         /* Connect to the command socket */
165         control_fd = xconnect(server->host, server->port);
166         control_stream = fdopen(control_fd, "r+");
167         if (control_stream == NULL) {
168                 perror_msg_and_die("Couldnt open control stream");
169         }
170
171         if (ftpcmd(NULL, NULL, control_stream, buf) != 220) {
172                 error_msg_and_die("%s", buf + 4);
173         }
174
175         /*  Login to the server */
176         switch (ftpcmd("USER ", server->user, control_stream, buf)) {
177         case 230:
178                 break;
179         case 331:
180                 if (ftpcmd("PASS ", server->password, control_stream, buf) != 230) {
181                         error_msg_and_die("PASS error: %s", buf + 4);
182                 }
183                 break;
184         default:
185                 error_msg_and_die("USER error: %s", buf + 4);
186         }
187
188         ftpcmd("TYPE I", NULL, control_stream, buf);
189
190         return(control_stream);
191 }
192
193 #ifdef CONFIG_FTPGET
194 static int ftp_recieve(FILE *control_stream, const char *host, const char *local_path, char *server_path)
195 {
196         char *filename;
197         char *local_file;
198         char buf[512];
199         off_t filesize = 0;
200         int fd_data;
201         int fd_local;
202         off_t beg_range = 0;
203
204         filename = get_last_path_component(server_path);
205         local_file = concat_path_file(local_path, filename);
206
207         /* Connect to the data socket */
208         if (ftpcmd("PASV", NULL, control_stream, buf) != 227) {
209                 error_msg_and_die("PASV error: %s", buf + 4);
210         }
211         fd_data = xconnect_ftpdata(host, buf);
212
213         if (ftpcmd("SIZE ", server_path, control_stream, buf) == 213) {
214                 filesize = atol(buf + 4);
215         }
216
217         /* only make a local file if we know that one exists on the remote server */
218         if (do_continue) {
219                 fd_local = xopen(local_file, O_APPEND | O_WRONLY);
220         } else {
221                 fd_local = xopen(local_file, O_CREAT | O_TRUNC | O_WRONLY);
222         }
223
224         if (do_continue) {
225                 struct stat sbuf;
226                 if (fstat(fd_local, &sbuf) < 0) {
227                         perror_msg_and_die("fstat()");
228                 }
229                 if (sbuf.st_size > 0) {
230                         beg_range = sbuf.st_size;
231                 } else {
232                         do_continue = 0;
233                 }
234         }
235
236         if (do_continue) {
237                 sprintf(buf, "REST %ld", beg_range);
238                 if (ftpcmd(buf, NULL, control_stream, buf) != 350) {
239                         do_continue = 0;
240                 } else {
241                         filesize -= beg_range;
242                 }
243         }
244
245         if (ftpcmd("RETR ", server_path, control_stream, buf) > 150) {
246                 error_msg_and_die("RETR error: %s", buf + 4);
247         }
248
249         /* Copy the file */
250         copyfd_chunk(fd_data, fd_local, filesize);
251
252         /* close it all down */
253         close(fd_data);
254         if (ftpcmd(NULL, NULL, control_stream, buf) != 226) {
255                 error_msg_and_die("ftp error: %s", buf + 4);
256         }
257         ftpcmd("QUIT", NULL, control_stream, buf);
258         
259         return(EXIT_SUCCESS);
260 }
261 #endif
262
263 #ifdef CONFIG_FTPPUT
264 static int ftp_send(FILE *control_stream, const char *host, const char *local_path, char *server_path)
265 {
266         struct stat sbuf;
267         char buf[512];
268         int fd_data;
269         int fd_local;
270         int response;
271
272         /*  Connect to the data socket */
273         if (ftpcmd("PASV", NULL, control_stream, buf) != 227) {
274                 error_msg_and_die("PASV error: %s", buf + 4);
275         }
276         fd_data = xconnect_ftpdata(host, buf);
277
278         if (ftpcmd("CWD ", server_path, control_stream, buf) != 250) {
279                 error_msg_and_die("CWD error: %s", buf + 4);
280         }
281
282         /* get the local file */
283         fd_local = xopen(local_path, O_RDONLY);
284         fstat(fd_local, &sbuf);
285
286         sprintf(buf, "ALLO %lu", sbuf.st_size);
287         response = ftpcmd(buf, NULL, control_stream, buf);
288         switch (response) {
289         case 200:
290         case 202:
291                 break;
292         default:
293                 close(fd_local);
294                 error_msg_and_die("ALLO error: %s", buf + 4);
295                 break;
296         }
297
298         response = ftpcmd("STOR ", local_path, control_stream, buf);
299         switch (response) {
300         case 125:
301         case 150:
302                 break;
303         default:
304                 close(fd_local);
305                 error_msg_and_die("STOR error: %s", buf + 4);
306         }
307
308         /* transfer the file  */
309         copyfd_chunk(fd_local, fd_data, 0);
310
311         /* close it all down */
312         close(fd_data);
313         if (ftpcmd(NULL, NULL, control_stream, buf) != 226) {
314                 error_msg_and_die("error: %s", buf + 4);
315         }
316         ftpcmd("QUIT", NULL, control_stream, buf);
317
318         return(EXIT_SUCCESS);
319 }
320 #endif
321
322 int ftpgetput_main(int argc, char **argv)
323 {
324         /* content-length of the file */
325         int option_index = -1;
326         int opt;
327
328         /* socket to ftp server */
329         FILE *control_stream;
330
331         /* continue a prev transfer (-c) */
332         ftp_host_info_t *server;
333
334         int (*ftp_action)(FILE *, const char *, const char *, char *) = NULL;
335
336         struct option long_options[] = {
337                 {"username", 1, NULL, 'u'},
338                 {"password", 1, NULL, 'p'},
339                 {"port", 1, NULL, 'P'},
340                 {"continue", 1, NULL, 'c'},
341                 {"verbose", 0, NULL, 'v'},
342                 {0, 0, 0, 0}
343         };
344
345 #ifdef CONFIG_FTPPUT
346         if (applet_name[3] == 'p') {
347                 ftp_action = ftp_send;
348         } 
349 #endif
350 #ifdef CONFIG_FTPGET
351         if (applet_name[3] == 'g') {
352                 ftp_action = ftp_recieve;
353         }
354 #endif
355
356         /* Set default values */
357         server = ftp_init();
358         verbose_flag = 0;
359
360         /* 
361          * Decipher the command line 
362          */
363         while ((opt = getopt_long(argc, argv, "u:p:P:cv", long_options, &option_index)) != EOF) {
364                 switch(opt) {
365                 case 'c':
366                         do_continue = 1;
367                         break;
368                 case 'u':
369                         server->user = optarg;
370                         break;
371                 case 'p':
372                         server->password = optarg;
373                         break;
374                 case 'P':
375                         server->port = optarg;
376                         break;
377                 case 'v':
378                         verbose_flag = 1;
379                         break;
380                 default:
381                         show_usage();
382                 }
383         }
384
385         /*
386          * Process the non-option command line arguments
387          */
388         if (argc - optind != 3) {
389                 show_usage();
390         }
391
392         /*  Connect/Setup/Configure the FTP session */
393         server->host = argv[optind];
394         control_stream = ftp_login(server);
395
396         return(ftp_action(control_stream, argv[optind], argv[optind + 1], argv[optind + 2]));
397 }
398
399 /*
400 Local Variables:
401 c-file-style: "linux"
402 c-basic-offset: 4
403 tab-width: 4
404 End:
405 */