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