1 /* vi: set sw=4 ts=4: */
5 * Mini implementation of FTP to retrieve a remote file.
7 * Copyright (C) 2002 Jeff Angielski, The PTR Group <jeff@theptrgroup.com>
8 * Copyright (C) 2002 Glenn McGrath <bug1@iinet.net.au>
10 * Based on wget.c by Chip Rosenthal Covad Communications
11 * <chip@laserlink.net>
13 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
16 #include <sys/types.h>
17 #include <sys/ioctl.h>
30 #include <netinet/in.h>
32 #include <sys/socket.h>
33 #include <arpa/inet.h>
37 typedef struct ftp_host_info_s {
40 struct sockaddr_in *s_in;
43 static char verbose_flag = 0;
44 static char do_continue = 0;
46 static int ftpcmd(const char *s1, const char *s2, FILE *stream, char *buf)
49 bb_error_msg("cmd %s%s", s1, s2);
54 fprintf(stream, "%s%s\r\n", s1, s2);
56 fprintf(stream, "%s\r\n", s1);
62 if (fgets(buf, 510, stream) == NULL) {
63 bb_perror_msg_and_die("fgets()");
65 buf_ptr = strstr(buf, "\r\n");
69 } while (! isdigit(buf[0]) || buf[3] != ' ');
74 static int xconnect_ftpdata(ftp_host_info_t *server, const char *buf)
77 unsigned short port_num;
79 buf_ptr = strrchr(buf, ',');
81 port_num = atoi(buf_ptr + 1);
83 buf_ptr = strrchr(buf, ',');
85 port_num += atoi(buf_ptr + 1) * 256;
87 server->s_in->sin_port=htons(port_num);
88 return(xconnect(server->s_in));
91 static FILE *ftp_login(ftp_host_info_t *server)
96 /* Connect to the command socket */
97 control_stream = fdopen(xconnect(server->s_in), "r+");
98 if (control_stream == NULL) {
99 bb_perror_msg_and_die("Couldnt open control stream");
102 if (ftpcmd(NULL, NULL, control_stream, buf) != 220) {
103 bb_error_msg_and_die("%s", buf + 4);
106 /* Login to the server */
107 switch (ftpcmd("USER ", server->user, control_stream, buf)) {
111 if (ftpcmd("PASS ", server->password, control_stream, buf) != 230) {
112 bb_error_msg_and_die("PASS error: %s", buf + 4);
116 bb_error_msg_and_die("USER error: %s", buf + 4);
119 ftpcmd("TYPE I", NULL, control_stream, buf);
121 return(control_stream);
125 #define ftp_receive 0
127 static int ftp_recieve(ftp_host_info_t *server, FILE *control_stream,
128 const char *local_path, char *server_path)
136 /* Connect to the data socket */
137 if (ftpcmd("PASV", NULL, control_stream, buf) != 227) {
138 bb_error_msg_and_die("PASV error: %s", buf + 4);
140 fd_data = xconnect_ftpdata(server, buf);
142 if (ftpcmd("SIZE ", server_path, control_stream, buf) == 213) {
143 unsigned long value=filesize;
144 if (safe_strtoul(buf + 4, &value))
145 bb_error_msg_and_die("SIZE error: %s", buf + 4);
152 if ((local_path[0] == '-') && (local_path[1] == '\0')) {
153 fd_local = STDOUT_FILENO;
159 if (lstat(local_path, &sbuf) < 0) {
160 bb_perror_msg_and_die("fstat()");
162 if (sbuf.st_size > 0) {
163 beg_range = sbuf.st_size;
170 sprintf(buf, "REST %ld", (long)beg_range);
171 if (ftpcmd(buf, NULL, control_stream, buf) != 350) {
174 filesize -= beg_range;
178 if (ftpcmd("RETR ", server_path, control_stream, buf) > 150) {
179 bb_error_msg_and_die("RETR error: %s", buf + 4);
182 /* only make a local file if we know that one exists on the remote server */
183 if (fd_local == -1) {
185 fd_local = bb_xopen(local_path, O_APPEND | O_WRONLY);
187 fd_local = bb_xopen(local_path, O_CREAT | O_TRUNC | O_WRONLY);
192 if (filesize != -1) {
193 if (-1 == bb_copyfd_size(fd_data, fd_local, filesize))
196 if (-1 == bb_copyfd_eof(fd_data, fd_local))
200 /* close it all down */
202 if (ftpcmd(NULL, NULL, control_stream, buf) != 226) {
203 bb_error_msg_and_die("ftp error: %s", buf + 4);
205 ftpcmd("QUIT", NULL, control_stream, buf);
207 return(EXIT_SUCCESS);
214 static int ftp_send(ftp_host_info_t *server, FILE *control_stream,
215 const char *server_path, char *local_path)
223 /* Connect to the data socket */
224 if (ftpcmd("PASV", NULL, control_stream, buf) != 227) {
225 bb_error_msg_and_die("PASV error: %s", buf + 4);
227 fd_data = xconnect_ftpdata(server, buf);
229 /* get the local file */
230 if ((local_path[0] == '-') && (local_path[1] == '\0')) {
231 fd_local = STDIN_FILENO;
233 fd_local = bb_xopen(local_path, O_RDONLY);
234 fstat(fd_local, &sbuf);
236 sprintf(buf, "ALLO %lu", (unsigned long)sbuf.st_size);
237 response = ftpcmd(buf, NULL, control_stream, buf);
244 bb_error_msg_and_die("ALLO error: %s", buf + 4);
248 response = ftpcmd("STOR ", server_path, control_stream, buf);
255 bb_error_msg_and_die("STOR error: %s", buf + 4);
258 /* transfer the file */
259 if (bb_copyfd_eof(fd_local, fd_data) == -1) {
263 /* close it all down */
265 if (ftpcmd(NULL, NULL, control_stream, buf) != 226) {
266 bb_error_msg_and_die("error: %s", buf + 4);
268 ftpcmd("QUIT", NULL, control_stream, buf);
270 return(EXIT_SUCCESS);
274 #define FTPGETPUT_OPT_CONTINUE 1
275 #define FTPGETPUT_OPT_VERBOSE 2
276 #define FTPGETPUT_OPT_USER 4
277 #define FTPGETPUT_OPT_PASSWORD 8
278 #define FTPGETPUT_OPT_PORT 16
280 #if ENABLE_FEATURE_FTPGETPUT_LONG_OPTIONS
281 static const struct option ftpgetput_long_options[] = {
282 {"continue", 1, NULL, 'c'},
283 {"verbose", 0, NULL, 'v'},
284 {"username", 1, NULL, 'u'},
285 {"password", 1, NULL, 'p'},
286 {"port", 1, NULL, 'P'},
291 int ftpgetput_main(int argc, char **argv)
293 /* content-length of the file */
297 /* socket to ftp server */
298 FILE *control_stream;
299 struct sockaddr_in s_in;
301 /* continue a prev transfer (-c) */
302 ftp_host_info_t *server;
304 int (*ftp_action)(ftp_host_info_t *, FILE *, const char *, char *) = NULL;
306 /* Check to see if the command is ftpget or ftput */
307 if (ENABLE_FTPPUT && (!ENABLE_FTPGET || bb_applet_name[3] == 'p')) {
308 ftp_action = ftp_send;
310 if (ENABLE_FTPGET && (!ENABLE_FTPPUT || bb_applet_name[3] == 'g')) {
311 ftp_action = ftp_recieve;
314 /* Set default values */
315 server = xmalloc(sizeof(ftp_host_info_t));
316 server->user = "anonymous";
317 server->password = "busybox@";
321 * Decipher the command line
323 #if ENABLE_FEATURE_FTPGETPUT_LONG_OPTIONS
324 bb_applet_long_options = ftpgetput_long_options;
326 opt = bb_getopt_ulflags(argc, argv, "cvu:p:P:", &server->user, &server->password, &port);
328 /* Process the non-option command line arguments */
329 if (argc - optind != 3) {
333 if (opt & FTPGETPUT_OPT_CONTINUE) {
336 if (opt & FTPGETPUT_OPT_VERBOSE) {
340 /* We want to do exactly _one_ DNS lookup, since some
341 * sites (i.e. ftp.us.debian.org) use round-robin DNS
342 * and we want to connect to only one IP... */
343 server->s_in = &s_in;
344 bb_lookup_host(&s_in, argv[optind]);
345 s_in.sin_port = bb_lookup_port(port, "tcp", 21);
347 printf("Connecting to %s[%s]:%d\n",
348 argv[optind], inet_ntoa(s_in.sin_addr), ntohs(s_in.sin_port));
351 /* Connect/Setup/Configure the FTP session */
352 control_stream = ftp_login(server);
354 return(ftp_action(server, control_stream, argv[optind + 1], argv[optind + 2]));