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