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@optushome.com.au>
10 * Based on wget.c by Chip Rosenthal Covad Communications
11 * <chip@laserlink.net>
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 * General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 #include <sys/types.h>
30 #include <sys/ioctl.h>
44 #include <netinet/in.h>
46 #include <sys/socket.h>
47 #include <arpa/inet.h>
51 typedef struct ftp_host_info_s {
54 struct sockaddr_in *s_in;
57 static char verbose_flag = 0;
58 static char do_continue = 0;
60 static int ftpcmd(const char *s1, const char *s2, FILE *stream, char *buf)
63 bb_error_msg("cmd %s%s", s1, s2);
68 fprintf(stream, "%s%s\n", s1, s2);
70 fprintf(stream, "%s\n", s1);
75 if (fgets(buf, 510, stream) == NULL) {
76 bb_perror_msg_and_die("fgets()");
78 } while (! isdigit(buf[0]) || buf[3] != ' ');
83 static int xconnect_ftpdata(ftp_host_info_t *server, const char *buf)
86 unsigned short port_num;
88 buf_ptr = strrchr(buf, ',');
90 port_num = atoi(buf_ptr + 1);
92 buf_ptr = strrchr(buf, ',');
94 port_num += atoi(buf_ptr + 1) * 256;
96 server->s_in->sin_port=htons(port_num);
97 return(xconnect(server->s_in));
100 static FILE *ftp_login(ftp_host_info_t *server)
102 FILE *control_stream;
105 /* Connect to the command socket */
106 control_stream = fdopen(xconnect(server->s_in), "r+");
107 if (control_stream == NULL) {
108 bb_perror_msg_and_die("Couldnt open control stream");
111 if (ftpcmd(NULL, NULL, control_stream, buf) != 220) {
112 bb_error_msg_and_die("%s", buf + 4);
115 /* Login to the server */
116 switch (ftpcmd("USER ", server->user, control_stream, buf)) {
120 if (ftpcmd("PASS ", server->password, control_stream, buf) != 230) {
121 bb_error_msg_and_die("PASS error: %s", buf + 4);
125 bb_error_msg_and_die("USER error: %s", buf + 4);
128 ftpcmd("TYPE I", NULL, control_stream, buf);
130 return(control_stream);
134 static int ftp_recieve(ftp_host_info_t *server, FILE *control_stream,
135 const char *local_path, char *server_path)
143 /* Connect to the data socket */
144 if (ftpcmd("PASV", NULL, control_stream, buf) != 227) {
145 bb_error_msg_and_die("PASV error: %s", buf + 4);
147 fd_data = xconnect_ftpdata(server, buf);
149 if (ftpcmd("SIZE ", server_path, control_stream, buf) == 213) {
150 unsigned long value=filesize;
151 if (safe_strtoul(buf + 4, &value))
152 bb_error_msg_and_die("SIZE error: %s", buf + 4);
156 if ((local_path[0] == '-') && (local_path[1] == '\0')) {
157 fd_local = STDOUT_FILENO;
163 if (lstat(local_path, &sbuf) < 0) {
164 bb_perror_msg_and_die("fstat()");
166 if (sbuf.st_size > 0) {
167 beg_range = sbuf.st_size;
174 sprintf(buf, "REST %ld", (long)beg_range);
175 if (ftpcmd(buf, NULL, control_stream, buf) != 350) {
178 filesize -= beg_range;
182 if (ftpcmd("RETR ", server_path, control_stream, buf) > 150) {
183 bb_error_msg_and_die("RETR error: %s", buf + 4);
186 /* only make a local file if we know that one exists on the remote server */
187 if (fd_local == -1) {
189 fd_local = bb_xopen(local_path, O_APPEND | O_WRONLY);
191 fd_local = bb_xopen(local_path, O_CREAT | O_TRUNC | O_WRONLY);
196 if (bb_copyfd_size(fd_data, fd_local, filesize) == -1) {
200 /* close it all down */
202 if (ftpcmd(NULL, NULL, control_stream, buf) != 226) {
203 bb_error_msg_and_die("ftp error: %s", buf + 4);
205 ftpcmd("QUIT", NULL, control_stream, buf);
207 return(EXIT_SUCCESS);
212 static int ftp_send(ftp_host_info_t *server, FILE *control_stream,
213 const char *server_path, char *local_path)
221 /* Connect to the data socket */
222 if (ftpcmd("PASV", NULL, control_stream, buf) != 227) {
223 bb_error_msg_and_die("PASV error: %s", buf + 4);
225 fd_data = xconnect_ftpdata(server, buf);
227 if (ftpcmd("CWD ", server_path, control_stream, buf) != 250) {
228 bb_error_msg_and_die("CWD error: %s", buf + 4);
231 /* get the local file */
232 if ((local_path[0] == '-') && (local_path[1] == '\0')) {
233 fd_local = STDIN_FILENO;
235 fd_local = bb_xopen(local_path, O_RDONLY);
236 fstat(fd_local, &sbuf);
238 sprintf(buf, "ALLO %lu", (unsigned long)sbuf.st_size);
239 response = ftpcmd(buf, NULL, control_stream, buf);
246 bb_error_msg_and_die("ALLO error: %s", buf + 4);
250 response = ftpcmd("STOR ", local_path, control_stream, buf);
257 bb_error_msg_and_die("STOR error: %s", buf + 4);
260 /* transfer the file */
261 if (bb_copyfd_eof(fd_local, fd_data) == -1) {
265 /* close it all down */
267 if (ftpcmd(NULL, NULL, control_stream, buf) != 226) {
268 bb_error_msg_and_die("error: %s", buf + 4);
270 ftpcmd("QUIT", NULL, control_stream, buf);
272 return(EXIT_SUCCESS);
276 #define FTPGETPUT_OPT_CONTINUE 1
277 #define FTPGETPUT_OPT_VERBOSE 2
278 #define FTPGETPUT_OPT_USER 4
279 #define FTPGETPUT_OPT_PASSWORD 8
280 #define FTPGETPUT_OPT_PORT 16
282 static const struct option ftpgetput_long_options[] = {
283 {"continue", 1, NULL, 'c'},
284 {"verbose", 0, NULL, 'v'},
285 {"username", 1, NULL, 'u'},
286 {"password", 1, NULL, 'p'},
287 {"port", 1, NULL, 'P'},
291 int ftpgetput_main(int argc, char **argv)
293 /* content-length of the file */
297 /* socket to ftp server */
298 FILE *control_stream;
299 struct sockaddr_in s_in;
301 /* continue a prev transfer (-c) */
302 ftp_host_info_t *server;
304 int (*ftp_action)(ftp_host_info_t *, FILE *, const char *, char *) = NULL;
306 /* Check to see if the command is ftpget or ftput */
308 # ifdef CONFIG_FTPGET
309 if (bb_applet_name[3] == 'p') {
310 ftp_action = ftp_send;
313 ftp_action = ftp_send;
317 # ifdef CONFIG_FTPPUT
318 if (bb_applet_name[3] == 'g') {
319 ftp_action = ftp_recieve;
322 ftp_action = ftp_recieve;
326 /* Set default values */
327 server = xmalloc(sizeof(ftp_host_info_t));
328 server->user = "anonymous";
329 server->password = "busybox@";
333 * Decipher the command line
335 bb_applet_long_options = ftpgetput_long_options;
336 opt = bb_getopt_ulflags(argc, argv, "cvu:p:P:", &server->user, &server->password, &port);
338 /* Process the non-option command line arguments */
339 if (argc - optind != 3) {
343 if (opt & FTPGETPUT_OPT_CONTINUE) {
346 if (opt & FTPGETPUT_OPT_VERBOSE) {
350 /* We want to do exactly _one_ DNS lookup, since some
351 * sites (i.e. ftp.us.debian.org) use round-robin DNS
352 * and we want to connect to only one IP... */
353 server->s_in = &s_in;
354 bb_lookup_host(&s_in, argv[optind]);
355 s_in.sin_port = bb_lookup_port(port, "tcp", 21);
357 printf("Connecting to %s[%s]:%d\n",
358 argv[optind], inet_ntoa(s_in.sin_addr), ntohs(s_in.sin_port));
361 /* Connect/Setup/Configure the FTP session */
362 control_stream = ftp_login(server);
364 return(ftp_action(server, control_stream, argv[optind + 1], argv[optind + 2]));
369 c-file-style: "linux"