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 = atoi(buf_ptr + 1);
65 buf_ptr = strrchr(buf, ',');
67 port_num += atoi(buf_ptr + 1) * 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 #define ftp_receive 0
109 static int ftp_receive(ftp_host_info_t *server, FILE *control_stream,
110 const char *local_path, char *server_path)
118 /* Connect to the data socket */
119 if (ftpcmd("PASV", NULL, control_stream, buf) != 227) {
120 bb_error_msg_and_die("PASV error: %s", buf + 4);
122 fd_data = xconnect_ftpdata(server, buf);
124 if (ftpcmd("SIZE ", server_path, control_stream, buf) == 213) {
125 unsigned long value=filesize;
126 if (safe_strtoul(buf + 4, &value))
127 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("fstat()");
144 if (sbuf.st_size > 0) {
145 beg_range = sbuf.st_size;
152 sprintf(buf, "REST %ld", (long)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);
196 static int ftp_send(ftp_host_info_t *server, FILE *control_stream,
197 const char *server_path, char *local_path)
205 /* Connect to the data socket */
206 if (ftpcmd("PASV", NULL, control_stream, buf) != 227) {
207 bb_error_msg_and_die("PASV error: %s", buf + 4);
209 fd_data = xconnect_ftpdata(server, buf);
211 /* get the local file */
212 if ((local_path[0] == '-') && (local_path[1] == '\0')) {
213 fd_local = STDIN_FILENO;
215 fd_local = xopen(local_path, O_RDONLY);
216 fstat(fd_local, &sbuf);
218 sprintf(buf, "ALLO %lu", (unsigned long)sbuf.st_size);
219 response = ftpcmd(buf, NULL, control_stream, buf);
226 bb_error_msg_and_die("ALLO error: %s", buf + 4);
230 response = ftpcmd("STOR ", server_path, control_stream, buf);
237 bb_error_msg_and_die("STOR error: %s", buf + 4);
240 /* transfer the file */
241 if (bb_copyfd_eof(fd_local, fd_data) == -1) {
245 /* close it all down */
247 if (ftpcmd(NULL, NULL, control_stream, buf) != 226) {
248 bb_error_msg_and_die("error: %s", buf + 4);
250 ftpcmd("QUIT", NULL, control_stream, buf);
252 return(EXIT_SUCCESS);
256 #define FTPGETPUT_OPT_CONTINUE 1
257 #define FTPGETPUT_OPT_VERBOSE 2
258 #define FTPGETPUT_OPT_USER 4
259 #define FTPGETPUT_OPT_PASSWORD 8
260 #define FTPGETPUT_OPT_PORT 16
262 #if ENABLE_FEATURE_FTPGETPUT_LONG_OPTIONS
263 static const struct option ftpgetput_long_options[] = {
264 {"continue", 1, NULL, 'c'},
265 {"verbose", 0, NULL, 'v'},
266 {"username", 1, NULL, 'u'},
267 {"password", 1, NULL, 'p'},
268 {"port", 1, NULL, 'P'},
272 #define ftpgetput_long_options 0
275 int ftpgetput_main(int argc, char **argv)
277 /* content-length of the file */
281 /* socket to ftp server */
282 FILE *control_stream;
283 struct sockaddr_in s_in;
285 /* continue a prev transfer (-c) */
286 ftp_host_info_t *server;
288 int (*ftp_action)(ftp_host_info_t *, FILE *, const char *, char *) = NULL;
290 /* Check to see if the command is ftpget or ftput */
291 if (ENABLE_FTPPUT && (!ENABLE_FTPGET || applet_name[3] == 'p')) {
292 ftp_action = ftp_send;
294 if (ENABLE_FTPGET && (!ENABLE_FTPPUT || applet_name[3] == 'g')) {
295 ftp_action = ftp_receive;
298 /* Set default values */
299 server = xmalloc(sizeof(ftp_host_info_t));
300 server->user = "anonymous";
301 server->password = "busybox@";
305 * Decipher the command line
307 if (ENABLE_FEATURE_FTPGETPUT_LONG_OPTIONS)
308 applet_long_options = ftpgetput_long_options;
310 opt = getopt32(argc, argv, "cvu:p:P:", &server->user, &server->password, &port);
312 /* Process the non-option command line arguments */
313 if (argc - optind != 3) {
317 if (opt & FTPGETPUT_OPT_CONTINUE) {
320 if (opt & FTPGETPUT_OPT_VERBOSE) {
324 /* We want to do exactly _one_ DNS lookup, since some
325 * sites (i.e. ftp.us.debian.org) use round-robin DNS
326 * and we want to connect to only one IP... */
327 server->s_in = &s_in;
328 bb_lookup_host(&s_in, argv[optind]);
329 s_in.sin_port = bb_lookup_port(port, "tcp", 21);
331 printf("Connecting to %s[%s]:%d\n",
332 argv[optind], inet_ntoa(s_in.sin_addr), ntohs(s_in.sin_port));
335 /* Connect/Setup/Configure the FTP session */
336 control_stream = ftp_login(server);
338 return(ftp_action(server, control_stream, argv[optind + 1], argv[optind + 2]));