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/ioctl.h>
26 #include <sys/socket.h>
30 typedef struct ftp_host_info_s {
33 struct sockaddr_in *s_in;
36 static char verbose_flag = 0;
37 static char do_continue = 0;
39 static int ftpcmd(const char *s1, const char *s2, FILE *stream, char *buf)
42 bb_error_msg("cmd %s%s", s1, s2);
47 fprintf(stream, "%s%s\r\n", s1, s2);
49 fprintf(stream, "%s\r\n", s1);
55 if (fgets(buf, 510, stream) == NULL) {
56 bb_perror_msg_and_die("fgets()");
58 buf_ptr = strstr(buf, "\r\n");
62 } while (! isdigit(buf[0]) || buf[3] != ' ');
67 static int xconnect_ftpdata(ftp_host_info_t *server, const char *buf)
70 unsigned short port_num;
72 buf_ptr = strrchr(buf, ',');
74 port_num = atoi(buf_ptr + 1);
76 buf_ptr = strrchr(buf, ',');
78 port_num += atoi(buf_ptr + 1) * 256;
80 server->s_in->sin_port=htons(port_num);
81 return(xconnect(server->s_in));
84 static FILE *ftp_login(ftp_host_info_t *server)
89 /* Connect to the command socket */
90 control_stream = fdopen(xconnect(server->s_in), "r+");
91 if (control_stream == NULL) {
92 bb_perror_msg_and_die("Couldnt open control stream");
95 if (ftpcmd(NULL, NULL, control_stream, buf) != 220) {
96 bb_error_msg_and_die("%s", buf + 4);
99 /* Login to the server */
100 switch (ftpcmd("USER ", server->user, control_stream, buf)) {
104 if (ftpcmd("PASS ", server->password, control_stream, buf) != 230) {
105 bb_error_msg_and_die("PASS error: %s", buf + 4);
109 bb_error_msg_and_die("USER error: %s", buf + 4);
112 ftpcmd("TYPE I", NULL, control_stream, buf);
114 return(control_stream);
118 #define ftp_receive 0
120 static int ftp_recieve(ftp_host_info_t *server, FILE *control_stream,
121 const char *local_path, char *server_path)
129 /* Connect to the data socket */
130 if (ftpcmd("PASV", NULL, control_stream, buf) != 227) {
131 bb_error_msg_and_die("PASV error: %s", buf + 4);
133 fd_data = xconnect_ftpdata(server, buf);
135 if (ftpcmd("SIZE ", server_path, control_stream, buf) == 213) {
136 unsigned long value=filesize;
137 if (safe_strtoul(buf + 4, &value))
138 bb_error_msg_and_die("SIZE error: %s", buf + 4);
145 if ((local_path[0] == '-') && (local_path[1] == '\0')) {
146 fd_local = STDOUT_FILENO;
152 if (lstat(local_path, &sbuf) < 0) {
153 bb_perror_msg_and_die("fstat()");
155 if (sbuf.st_size > 0) {
156 beg_range = sbuf.st_size;
163 sprintf(buf, "REST %ld", (long)beg_range);
164 if (ftpcmd(buf, NULL, control_stream, buf) != 350) {
167 filesize -= beg_range;
171 if (ftpcmd("RETR ", server_path, control_stream, buf) > 150) {
172 bb_error_msg_and_die("RETR error: %s", buf + 4);
175 /* only make a local file if we know that one exists on the remote server */
176 if (fd_local == -1) {
178 fd_local = bb_xopen(local_path, O_APPEND | O_WRONLY);
180 fd_local = bb_xopen(local_path, O_CREAT | O_TRUNC | O_WRONLY);
185 if (filesize != -1) {
186 if (-1 == bb_copyfd_size(fd_data, fd_local, filesize))
189 if (-1 == bb_copyfd_eof(fd_data, fd_local))
193 /* close it all down */
195 if (ftpcmd(NULL, NULL, control_stream, buf) != 226) {
196 bb_error_msg_and_die("ftp error: %s", buf + 4);
198 ftpcmd("QUIT", NULL, control_stream, buf);
200 return(EXIT_SUCCESS);
207 static int ftp_send(ftp_host_info_t *server, FILE *control_stream,
208 const char *server_path, char *local_path)
216 /* Connect to the data socket */
217 if (ftpcmd("PASV", NULL, control_stream, buf) != 227) {
218 bb_error_msg_and_die("PASV error: %s", buf + 4);
220 fd_data = xconnect_ftpdata(server, buf);
222 /* get the local file */
223 if ((local_path[0] == '-') && (local_path[1] == '\0')) {
224 fd_local = STDIN_FILENO;
226 fd_local = bb_xopen(local_path, O_RDONLY);
227 fstat(fd_local, &sbuf);
229 sprintf(buf, "ALLO %lu", (unsigned long)sbuf.st_size);
230 response = ftpcmd(buf, NULL, control_stream, buf);
237 bb_error_msg_and_die("ALLO error: %s", buf + 4);
241 response = ftpcmd("STOR ", server_path, control_stream, buf);
248 bb_error_msg_and_die("STOR error: %s", buf + 4);
251 /* transfer the file */
252 if (bb_copyfd_eof(fd_local, fd_data) == -1) {
256 /* close it all down */
258 if (ftpcmd(NULL, NULL, control_stream, buf) != 226) {
259 bb_error_msg_and_die("error: %s", buf + 4);
261 ftpcmd("QUIT", NULL, control_stream, buf);
263 return(EXIT_SUCCESS);
267 #define FTPGETPUT_OPT_CONTINUE 1
268 #define FTPGETPUT_OPT_VERBOSE 2
269 #define FTPGETPUT_OPT_USER 4
270 #define FTPGETPUT_OPT_PASSWORD 8
271 #define FTPGETPUT_OPT_PORT 16
273 #if ENABLE_FEATURE_FTPGETPUT_LONG_OPTIONS
274 static const struct option ftpgetput_long_options[] = {
275 {"continue", 1, NULL, 'c'},
276 {"verbose", 0, NULL, 'v'},
277 {"username", 1, NULL, 'u'},
278 {"password", 1, NULL, 'p'},
279 {"port", 1, NULL, 'P'},
283 #define ftpgetput_long_options 0
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 */
302 if (ENABLE_FTPPUT && (!ENABLE_FTPGET || bb_applet_name[3] == 'p')) {
303 ftp_action = ftp_send;
305 if (ENABLE_FTPGET && (!ENABLE_FTPPUT || bb_applet_name[3] == 'g')) {
306 ftp_action = ftp_recieve;
309 /* Set default values */
310 server = xmalloc(sizeof(ftp_host_info_t));
311 server->user = "anonymous";
312 server->password = "busybox@";
316 * Decipher the command line
318 if (ENABLE_FEATURE_FTPGETPUT_LONG_OPTIONS)
319 bb_applet_long_options = ftpgetput_long_options;
321 opt = bb_getopt_ulflags(argc, argv, "cvu:p:P:", &server->user, &server->password, &port);
323 /* Process the non-option command line arguments */
324 if (argc - optind != 3) {
328 if (opt & FTPGETPUT_OPT_CONTINUE) {
331 if (opt & FTPGETPUT_OPT_VERBOSE) {
335 /* We want to do exactly _one_ DNS lookup, since some
336 * sites (i.e. ftp.us.debian.org) use round-robin DNS
337 * and we want to connect to only one IP... */
338 server->s_in = &s_in;
339 bb_lookup_host(&s_in, argv[optind]);
340 s_in.sin_port = bb_lookup_port(port, "tcp", 21);
342 printf("Connecting to %s[%s]:%d\n",
343 argv[optind], inet_ntoa(s_in.sin_addr), ntohs(s_in.sin_port));
346 /* Connect/Setup/Configure the FTP session */
347 control_stream = ftp_login(server);
349 return(ftp_action(server, control_stream, argv[optind + 1], argv[optind + 2]));