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