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>
31 #include <netinet/in.h>
33 #include <sys/socket.h>
34 #include <arpa/inet.h>
38 typedef struct ftp_host_info_s {
41 struct sockaddr_in *s_in;
44 static char verbose_flag = 0;
45 static char do_continue = 0;
47 static int ftpcmd(const char *s1, const char *s2, FILE *stream, char *buf)
50 bb_error_msg("cmd %s%s", s1, s2);
55 fprintf(stream, "%s%s\r\n", s1, s2);
57 fprintf(stream, "%s\r\n", s1);
63 if (fgets(buf, 510, stream) == NULL) {
64 bb_perror_msg_and_die("fgets()");
66 buf_ptr = strstr(buf, "\r\n");
70 } while (! isdigit(buf[0]) || buf[3] != ' ');
75 static int xconnect_ftpdata(ftp_host_info_t *server, const char *buf)
78 unsigned short port_num;
80 buf_ptr = strrchr(buf, ',');
82 port_num = atoi(buf_ptr + 1);
84 buf_ptr = strrchr(buf, ',');
86 port_num += atoi(buf_ptr + 1) * 256;
88 server->s_in->sin_port=htons(port_num);
89 return(xconnect(server->s_in));
92 static FILE *ftp_login(ftp_host_info_t *server)
97 /* Connect to the command socket */
98 control_stream = fdopen(xconnect(server->s_in), "r+");
99 if (control_stream == NULL) {
100 bb_perror_msg_and_die("Couldnt open control stream");
103 if (ftpcmd(NULL, NULL, control_stream, buf) != 220) {
104 bb_error_msg_and_die("%s", buf + 4);
107 /* Login to the server */
108 switch (ftpcmd("USER ", server->user, control_stream, buf)) {
112 if (ftpcmd("PASS ", server->password, control_stream, buf) != 230) {
113 bb_error_msg_and_die("PASS error: %s", buf + 4);
117 bb_error_msg_and_die("USER error: %s", buf + 4);
120 ftpcmd("TYPE I", NULL, control_stream, buf);
122 return(control_stream);
126 static int ftp_recieve(ftp_host_info_t *server, FILE *control_stream,
127 const char *local_path, char *server_path)
135 /* Connect to the data socket */
136 if (ftpcmd("PASV", NULL, control_stream, buf) != 227) {
137 bb_error_msg_and_die("PASV error: %s", buf + 4);
139 fd_data = xconnect_ftpdata(server, buf);
141 if (ftpcmd("SIZE ", server_path, control_stream, buf) == 213) {
142 unsigned long value=filesize;
143 if (safe_strtoul(buf + 4, &value))
144 bb_error_msg_and_die("SIZE error: %s", buf + 4);
151 if ((local_path[0] == '-') && (local_path[1] == '\0')) {
152 fd_local = STDOUT_FILENO;
158 if (lstat(local_path, &sbuf) < 0) {
159 bb_perror_msg_and_die("fstat()");
161 if (sbuf.st_size > 0) {
162 beg_range = sbuf.st_size;
169 sprintf(buf, "REST %ld", (long)beg_range);
170 if (ftpcmd(buf, NULL, control_stream, buf) != 350) {
173 filesize -= beg_range;
177 if (ftpcmd("RETR ", server_path, control_stream, buf) > 150) {
178 bb_error_msg_and_die("RETR error: %s", buf + 4);
181 /* only make a local file if we know that one exists on the remote server */
182 if (fd_local == -1) {
184 fd_local = bb_xopen(local_path, O_APPEND | O_WRONLY);
186 fd_local = bb_xopen(local_path, O_CREAT | O_TRUNC | O_WRONLY);
191 if (filesize != -1) {
192 if (-1 == bb_copyfd_size(fd_data, fd_local, filesize))
195 if (-1 == bb_copyfd_eof(fd_data, fd_local))
199 /* close it all down */
201 if (ftpcmd(NULL, NULL, control_stream, buf) != 226) {
202 bb_error_msg_and_die("ftp error: %s", buf + 4);
204 ftpcmd("QUIT", NULL, control_stream, buf);
206 return(EXIT_SUCCESS);
211 static int ftp_send(ftp_host_info_t *server, FILE *control_stream,
212 const char *server_path, char *local_path)
220 /* Connect to the data socket */
221 if (ftpcmd("PASV", NULL, control_stream, buf) != 227) {
222 bb_error_msg_and_die("PASV error: %s", buf + 4);
224 fd_data = xconnect_ftpdata(server, buf);
226 /* get the local file */
227 if ((local_path[0] == '-') && (local_path[1] == '\0')) {
228 fd_local = STDIN_FILENO;
230 fd_local = bb_xopen(local_path, O_RDONLY);
231 fstat(fd_local, &sbuf);
233 sprintf(buf, "ALLO %lu", (unsigned long)sbuf.st_size);
234 response = ftpcmd(buf, NULL, control_stream, buf);
241 bb_error_msg_and_die("ALLO error: %s", buf + 4);
245 response = ftpcmd("STOR ", server_path, control_stream, buf);
252 bb_error_msg_and_die("STOR error: %s", buf + 4);
255 /* transfer the file */
256 if (bb_copyfd_eof(fd_local, fd_data) == -1) {
260 /* close it all down */
262 if (ftpcmd(NULL, NULL, control_stream, buf) != 226) {
263 bb_error_msg_and_die("error: %s", buf + 4);
265 ftpcmd("QUIT", NULL, control_stream, buf);
267 return(EXIT_SUCCESS);
271 #define FTPGETPUT_OPT_CONTINUE 1
272 #define FTPGETPUT_OPT_VERBOSE 2
273 #define FTPGETPUT_OPT_USER 4
274 #define FTPGETPUT_OPT_PASSWORD 8
275 #define FTPGETPUT_OPT_PORT 16
277 static const struct option ftpgetput_long_options[] = {
278 {"continue", 1, NULL, 'c'},
279 {"verbose", 0, NULL, 'v'},
280 {"username", 1, NULL, 'u'},
281 {"password", 1, NULL, 'p'},
282 {"port", 1, NULL, 'P'},
286 int ftpgetput_main(int argc, char **argv)
288 /* content-length of the file */
292 /* socket to ftp server */
293 FILE *control_stream;
294 struct sockaddr_in s_in;
296 /* continue a prev transfer (-c) */
297 ftp_host_info_t *server;
299 int (*ftp_action)(ftp_host_info_t *, FILE *, const char *, char *) = NULL;
301 /* Check to see if the command is ftpget or ftput */
303 # ifdef CONFIG_FTPGET
304 if (bb_applet_name[3] == 'p') {
305 ftp_action = ftp_send;
308 ftp_action = ftp_send;
312 # ifdef CONFIG_FTPPUT
313 if (bb_applet_name[3] == 'g') {
314 ftp_action = ftp_recieve;
317 ftp_action = ftp_recieve;
321 /* Set default values */
322 server = xmalloc(sizeof(ftp_host_info_t));
323 server->user = "anonymous";
324 server->password = "busybox@";
328 * Decipher the command line
330 bb_applet_long_options = ftpgetput_long_options;
331 opt = bb_getopt_ulflags(argc, argv, "cvu:p:P:", &server->user, &server->password, &port);
333 /* Process the non-option command line arguments */
334 if (argc - optind != 3) {
338 if (opt & FTPGETPUT_OPT_CONTINUE) {
341 if (opt & FTPGETPUT_OPT_VERBOSE) {
345 /* We want to do exactly _one_ DNS lookup, since some
346 * sites (i.e. ftp.us.debian.org) use round-robin DNS
347 * and we want to connect to only one IP... */
348 server->s_in = &s_in;
349 bb_lookup_host(&s_in, argv[optind]);
350 s_in.sin_port = bb_lookup_port(port, "tcp", 21);
352 printf("Connecting to %s[%s]:%d\n",
353 argv[optind], inet_ntoa(s_in.sin_addr), ntohs(s_in.sin_port));
356 /* Connect/Setup/Configure the FTP session */
357 control_stream = ftp_login(server);
359 return(ftp_action(server, control_stream, argv[optind + 1], argv[optind + 2]));
364 c-file-style: "linux"