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.
19 typedef struct ftp_host_info_s {
22 struct sockaddr_in *s_in;
25 static char verbose_flag = 0;
26 static char do_continue = 0;
28 static int ftpcmd(const char *s1, const char *s2, FILE *stream, char *buf)
31 bb_error_msg("cmd %s%s", s1, s2);
36 fprintf(stream, "%s%s\r\n", s1, s2);
38 fprintf(stream, "%s\r\n", s1);
44 if (fgets(buf, 510, stream) == NULL) {
45 bb_perror_msg_and_die("fgets");
47 buf_ptr = strstr(buf, "\r\n");
51 } while (!isdigit(buf[0]) || buf[3] != ' ');
56 static int xconnect_ftpdata(ftp_host_info_t *server, const char *buf)
59 unsigned short port_num;
61 buf_ptr = strrchr(buf, ',');
63 port_num = xatoul_range(buf_ptr + 1, 0, 255);
65 buf_ptr = strrchr(buf, ',');
67 port_num += xatoul_range(buf_ptr + 1, 0, 255) * 256;
69 server->s_in->sin_port = htons(port_num);
70 return xconnect_tcp_v4(server->s_in);
73 static FILE *ftp_login(ftp_host_info_t *server)
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");
84 if (ftpcmd(NULL, NULL, control_stream, buf) != 220) {
85 bb_error_msg_and_die("%s", buf + 4);
88 /* Login to the server */
89 switch (ftpcmd("USER ", server->user, control_stream, buf)) {
93 if (ftpcmd("PASS ", server->password, control_stream, buf) != 230) {
94 bb_error_msg_and_die("PASS error: %s", buf + 4);
98 bb_error_msg_and_die("USER error: %s", buf + 4);
101 ftpcmd("TYPE I", NULL, control_stream, buf);
103 return control_stream;
107 int ftp_receive(ftp_host_info_t *server, FILE *control_stream,
108 const char *local_path, char *server_path);
111 int ftp_receive(ftp_host_info_t *server, FILE *control_stream,
112 const char *local_path, char *server_path)
120 /* Connect to the data socket */
121 if (ftpcmd("PASV", NULL, control_stream, buf) != 227) {
122 bb_error_msg_and_die("PASV error: %s", buf + 4);
124 fd_data = xconnect_ftpdata(server, buf);
126 if (ftpcmd("SIZE ", server_path, control_stream, buf) == 213) {
127 filesize = BB_STRTOOFF(buf + 4, NULL, 10);
128 if (errno || filesize < 0)
129 bb_error_msg_and_die("SIZE error: %s", buf + 4);
135 if ((local_path[0] == '-') && (local_path[1] == '\0')) {
136 fd_local = STDOUT_FILENO;
142 if (lstat(local_path, &sbuf) < 0) {
143 bb_perror_msg_and_die("lstat");
145 if (sbuf.st_size > 0) {
146 beg_range = sbuf.st_size;
153 sprintf(buf, "REST %"OFF_FMT"d", beg_range);
154 if (ftpcmd(buf, NULL, control_stream, buf) != 350) {
157 filesize -= beg_range;
161 if (ftpcmd("RETR ", server_path, control_stream, buf) > 150) {
162 bb_error_msg_and_die("RETR error: %s", buf + 4);
165 /* only make a local file if we know that one exists on the remote server */
166 if (fd_local == -1) {
168 fd_local = xopen(local_path, O_APPEND | O_WRONLY);
170 fd_local = xopen(local_path, O_CREAT | O_TRUNC | O_WRONLY);
175 if (filesize != -1) {
176 if (-1 == bb_copyfd_size(fd_data, fd_local, filesize))
179 if (-1 == bb_copyfd_eof(fd_data, fd_local))
183 /* close it all down */
185 if (ftpcmd(NULL, NULL, control_stream, buf) != 226) {
186 bb_error_msg_and_die("ftp error: %s", buf + 4);
188 ftpcmd("QUIT", NULL, control_stream, buf);
195 int ftp_send(ftp_host_info_t *server, FILE *control_stream,
196 const char *server_path, char *local_path);
199 int ftp_send(ftp_host_info_t *server, FILE *control_stream,
200 const char *server_path, char *local_path)
208 /* Connect to the data socket */
209 if (ftpcmd("PASV", NULL, control_stream, buf) != 227) {
210 bb_error_msg_and_die("PASV error: %s", buf + 4);
212 fd_data = xconnect_ftpdata(server, buf);
214 /* get the local file */
215 if ((local_path[0] == '-') && (local_path[1] == '\0')) {
216 fd_local = STDIN_FILENO;
218 fd_local = xopen(local_path, O_RDONLY);
219 fstat(fd_local, &sbuf);
221 sprintf(buf, "ALLO %lu", (unsigned long)sbuf.st_size);
222 response = ftpcmd(buf, NULL, control_stream, buf);
229 bb_error_msg_and_die("ALLO error: %s", buf + 4);
233 response = ftpcmd("STOR ", server_path, control_stream, buf);
240 bb_error_msg_and_die("STOR error: %s", buf + 4);
243 /* transfer the file */
244 if (bb_copyfd_eof(fd_local, fd_data) == -1) {
248 /* close it all down */
250 if (ftpcmd(NULL, NULL, control_stream, buf) != 226) {
251 bb_error_msg_and_die("error: %s", buf + 4);
253 ftpcmd("QUIT", NULL, control_stream, buf);
259 #define FTPGETPUT_OPT_CONTINUE 1
260 #define FTPGETPUT_OPT_VERBOSE 2
261 #define FTPGETPUT_OPT_USER 4
262 #define FTPGETPUT_OPT_PASSWORD 8
263 #define FTPGETPUT_OPT_PORT 16
265 #if ENABLE_FEATURE_FTPGETPUT_LONG_OPTIONS
266 static const struct option ftpgetput_long_options[] = {
267 { "continue", 1, NULL, 'c' },
268 { "verbose", 0, NULL, 'v' },
269 { "username", 1, NULL, 'u' },
270 { "password", 1, NULL, 'p' },
271 { "port", 1, NULL, 'P' },
276 int ftpgetput_main(int argc, char **argv)
278 /* content-length of the file */
282 /* socket to ftp server */
283 FILE *control_stream;
284 struct sockaddr_in s_in;
286 /* continue a prev transfer (-c) */
287 ftp_host_info_t *server;
289 int (*ftp_action)(ftp_host_info_t *, FILE *, const char *, char *) = NULL;
291 /* Check to see if the command is ftpget or ftput */
292 if (ENABLE_FTPPUT && (!ENABLE_FTPGET || applet_name[3] == 'p')) {
293 ftp_action = ftp_send;
295 if (ENABLE_FTPGET && (!ENABLE_FTPPUT || applet_name[3] == 'g')) {
296 ftp_action = ftp_receive;
299 /* Set default values */
300 server = xmalloc(sizeof(ftp_host_info_t));
301 server->user = "anonymous";
302 server->password = "busybox@";
306 * Decipher the command line
308 #if ENABLE_FEATURE_FTPGETPUT_LONG_OPTIONS
309 applet_long_options = ftpgetput_long_options;
311 opt = getopt32(argc, argv, "cvu:p:P:", &server->user, &server->password, &port);
313 /* Process the non-option command line arguments */
314 if (argc - optind != 3) {
318 if (opt & FTPGETPUT_OPT_CONTINUE) {
321 if (opt & FTPGETPUT_OPT_VERBOSE) {
325 /* We want to do exactly _one_ DNS lookup, since some
326 * sites (i.e. ftp.us.debian.org) use round-robin DNS
327 * and we want to connect to only one IP... */
328 server->s_in = &s_in;
329 bb_lookup_host(&s_in, argv[optind]);
330 s_in.sin_port = bb_lookup_port(port, "tcp", 21);
332 printf("Connecting to %s[%s]:%d\n",
333 argv[optind], inet_ntoa(s_in.sin_addr), ntohs(s_in.sin_port));
336 /* Connect/Setup/Configure the FTP session */
337 control_stream = ftp_login(server);
339 return ftp_action(server, control_stream, argv[optind + 1], argv[optind + 2]);