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(server->s_in);
73 static FILE *ftp_login(ftp_host_info_t *server)
78 /* Connect to the command socket */
79 control_stream = fdopen(xconnect(server->s_in), "r+");
80 if (control_stream == NULL) {
81 bb_perror_msg_and_die("Couldnt 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 if (SAFE_STRTOOFF(buf + 4, &filesize))
128 bb_error_msg_and_die("SIZE error: %s", buf + 4);
134 if ((local_path[0] == '-') && (local_path[1] == '\0')) {
135 fd_local = STDOUT_FILENO;
141 if (lstat(local_path, &sbuf) < 0) {
142 bb_perror_msg_and_die("lstat");
144 if (sbuf.st_size > 0) {
145 beg_range = sbuf.st_size;
152 sprintf(buf, "REST "OFF_FMT, beg_range);
153 if (ftpcmd(buf, NULL, control_stream, buf) != 350) {
156 filesize -= beg_range;
160 if (ftpcmd("RETR ", server_path, control_stream, buf) > 150) {
161 bb_error_msg_and_die("RETR error: %s", buf + 4);
164 /* only make a local file if we know that one exists on the remote server */
165 if (fd_local == -1) {
167 fd_local = xopen(local_path, O_APPEND | O_WRONLY);
169 fd_local = xopen3(local_path, O_CREAT | O_TRUNC | O_WRONLY, 0777);
174 if (filesize != -1) {
175 if (-1 == bb_copyfd_size(fd_data, fd_local, filesize))
178 if (-1 == bb_copyfd_eof(fd_data, fd_local))
182 /* close it all down */
184 if (ftpcmd(NULL, NULL, control_stream, buf) != 226) {
185 bb_error_msg_and_die("ftp error: %s", buf + 4);
187 ftpcmd("QUIT", NULL, control_stream, buf);
189 return(EXIT_SUCCESS);
194 int ftp_send(ftp_host_info_t *server, FILE *control_stream,
195 const char *server_path, char *local_path);
198 int ftp_send(ftp_host_info_t *server, FILE *control_stream,
199 const char *server_path, char *local_path)
207 /* Connect to the data socket */
208 if (ftpcmd("PASV", NULL, control_stream, buf) != 227) {
209 bb_error_msg_and_die("PASV error: %s", buf + 4);
211 fd_data = xconnect_ftpdata(server, buf);
213 /* get the local file */
214 if ((local_path[0] == '-') && (local_path[1] == '\0')) {
215 fd_local = STDIN_FILENO;
217 fd_local = xopen(local_path, O_RDONLY);
218 fstat(fd_local, &sbuf);
220 sprintf(buf, "ALLO %lu", (unsigned long)sbuf.st_size);
221 response = ftpcmd(buf, NULL, control_stream, buf);
228 bb_error_msg_and_die("ALLO error: %s", buf + 4);
232 response = ftpcmd("STOR ", server_path, control_stream, buf);
239 bb_error_msg_and_die("STOR error: %s", buf + 4);
242 /* transfer the file */
243 if (bb_copyfd_eof(fd_local, fd_data) == -1) {
247 /* close it all down */
249 if (ftpcmd(NULL, NULL, control_stream, buf) != 226) {
250 bb_error_msg_and_die("error: %s", buf + 4);
252 ftpcmd("QUIT", NULL, control_stream, buf);
254 return(EXIT_SUCCESS);
258 #define FTPGETPUT_OPT_CONTINUE 1
259 #define FTPGETPUT_OPT_VERBOSE 2
260 #define FTPGETPUT_OPT_USER 4
261 #define FTPGETPUT_OPT_PASSWORD 8
262 #define FTPGETPUT_OPT_PORT 16
264 #if ENABLE_FEATURE_FTPGETPUT_LONG_OPTIONS
265 static const struct option ftpgetput_long_options[] = {
266 {"continue", 1, NULL, 'c'},
267 {"verbose", 0, NULL, 'v'},
268 {"username", 1, NULL, 'u'},
269 {"password", 1, NULL, 'p'},
270 {"port", 1, NULL, 'P'},
274 #define ftpgetput_long_options 0
277 int ftpgetput_main(int argc, char **argv)
279 /* content-length of the file */
283 /* socket to ftp server */
284 FILE *control_stream;
285 struct sockaddr_in s_in;
287 /* continue a prev transfer (-c) */
288 ftp_host_info_t *server;
290 int (*ftp_action)(ftp_host_info_t *, FILE *, const char *, char *) = NULL;
292 /* Check to see if the command is ftpget or ftput */
293 if (ENABLE_FTPPUT && (!ENABLE_FTPGET || applet_name[3] == 'p')) {
294 ftp_action = ftp_send;
296 if (ENABLE_FTPGET && (!ENABLE_FTPPUT || applet_name[3] == 'g')) {
297 ftp_action = ftp_receive;
300 /* Set default values */
301 server = xmalloc(sizeof(ftp_host_info_t));
302 server->user = "anonymous";
303 server->password = "busybox@";
307 * Decipher the command line
309 if (ENABLE_FEATURE_FTPGETPUT_LONG_OPTIONS)
310 applet_long_options = ftpgetput_long_options;
312 opt = getopt32(argc, argv, "cvu:p:P:", &server->user, &server->password, &port);
314 /* Process the non-option command line arguments */
315 if (argc - optind != 3) {
319 if (opt & FTPGETPUT_OPT_CONTINUE) {
322 if (opt & FTPGETPUT_OPT_VERBOSE) {
326 /* We want to do exactly _one_ DNS lookup, since some
327 * sites (i.e. ftp.us.debian.org) use round-robin DNS
328 * and we want to connect to only one IP... */
329 server->s_in = &s_in;
330 bb_lookup_host(&s_in, argv[optind]);
331 s_in.sin_port = bb_lookup_port(port, "tcp", 21);
333 printf("Connecting to %s[%s]:%d\n",
334 argv[optind], inet_ntoa(s_in.sin_addr), ntohs(s_in.sin_port));
337 /* Connect/Setup/Configure the FTP session */
338 control_stream = ftp_login(server);
340 return(ftp_action(server, control_stream, argv[optind + 1], argv[optind + 2]));