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 {
56 struct sockaddr_in *s_in;
59 static char verbose_flag;
60 static char do_continue = 0;
62 static ftp_host_info_t *ftp_init(void)
64 ftp_host_info_t *host;
66 host = xcalloc(1, sizeof(ftp_host_info_t));
68 /* Set the default port */
69 if (getservbyname("ftp", "tcp")) {
74 host->user = "anonymous";
75 host->password = "busybox@";
80 static int ftpcmd(const char *s1, const char *s2, FILE *stream, char *buf)
83 bb_error_msg("cmd %s%s", s1, s2);
88 fprintf(stream, "%s%s\n", s1, s2);
90 fprintf(stream, "%s\n", s1);
95 if (fgets(buf, 510, stream) == NULL) {
96 bb_perror_msg_and_die("fgets()");
98 } while (! isdigit(buf[0]) || buf[3] != ' ');
103 static int xconnect_ftpdata(ftp_host_info_t *server, const char *buf)
106 unsigned short port_num;
108 buf_ptr = strrchr(buf, ',');
110 port_num = atoi(buf_ptr + 1);
112 buf_ptr = strrchr(buf, ',');
114 port_num += atoi(buf_ptr + 1) * 256;
116 server->s_in->sin_port=htons(port_num);
117 return(xconnect(server->s_in));
120 static FILE *ftp_login(ftp_host_info_t *server)
122 FILE *control_stream;
126 /* Connect to the command socket */
127 control_fd = xconnect(server->s_in);
128 control_stream = fdopen(control_fd, "r+");
129 if (control_stream == NULL) {
130 bb_perror_msg_and_die("Couldnt open control stream");
133 if (ftpcmd(NULL, NULL, control_stream, buf) != 220) {
134 bb_error_msg_and_die("%s", buf + 4);
137 /* Login to the server */
138 switch (ftpcmd("USER ", server->user, control_stream, buf)) {
142 if (ftpcmd("PASS ", server->password, control_stream, buf) != 230) {
143 bb_error_msg_and_die("PASS error: %s", buf + 4);
147 bb_error_msg_and_die("USER error: %s", buf + 4);
150 ftpcmd("TYPE I", NULL, control_stream, buf);
152 return(control_stream);
156 static int ftp_recieve(ftp_host_info_t *server, FILE *control_stream,
157 const char *local_path, char *server_path)
167 filename = bb_get_last_path_component(server_path);
168 local_file = concat_path_file(local_path, filename);
170 /* Connect to the data socket */
171 if (ftpcmd("PASV", NULL, control_stream, buf) != 227) {
172 bb_error_msg_and_die("PASV error: %s", buf + 4);
174 fd_data = xconnect_ftpdata(server, buf);
176 if (ftpcmd("SIZE ", server_path, control_stream, buf) == 213) {
177 filesize = atol(buf + 4);
182 if (lstat(local_file, &sbuf) < 0) {
183 bb_perror_msg_and_die("fstat()");
185 if (sbuf.st_size > 0) {
186 beg_range = sbuf.st_size;
193 sprintf(buf, "REST %ld", (long)beg_range);
194 if (ftpcmd(buf, NULL, control_stream, buf) != 350) {
197 filesize -= beg_range;
201 if (ftpcmd("RETR ", server_path, control_stream, buf) > 150) {
202 bb_error_msg_and_die("RETR error: %s", buf + 4);
205 /* only make a local file if we know that one exists on the remote server */
207 fd_local = bb_xopen(local_file, O_APPEND | O_WRONLY);
209 fd_local = bb_xopen(local_file, O_CREAT | O_TRUNC | O_WRONLY);
213 if (bb_copyfd(fd_data, fd_local, filesize) == -1) {
217 /* close it all down */
219 if (ftpcmd(NULL, NULL, control_stream, buf) != 226) {
220 bb_error_msg_and_die("ftp error: %s", buf + 4);
222 ftpcmd("QUIT", NULL, control_stream, buf);
224 return(EXIT_SUCCESS);
229 static int ftp_send(ftp_host_info_t *server, FILE *control_stream,
230 const char *server_path, char *local_path)
238 /* Connect to the data socket */
239 if (ftpcmd("PASV", NULL, control_stream, buf) != 227) {
240 bb_error_msg_and_die("PASV error: %s", buf + 4);
242 fd_data = xconnect_ftpdata(server, buf);
244 if (ftpcmd("CWD ", server_path, control_stream, buf) != 250) {
245 bb_error_msg_and_die("CWD error: %s", buf + 4);
248 /* get the local file */
249 fd_local = bb_xopen(local_path, O_RDONLY);
250 fstat(fd_local, &sbuf);
252 sprintf(buf, "ALLO %lu", (unsigned long)sbuf.st_size);
253 response = ftpcmd(buf, NULL, control_stream, buf);
260 bb_error_msg_and_die("ALLO error: %s", buf + 4);
264 response = ftpcmd("STOR ", local_path, control_stream, buf);
271 bb_error_msg_and_die("STOR error: %s", buf + 4);
274 /* transfer the file */
275 if (bb_copyfd(fd_local, fd_data, 0) == -1) {
279 /* close it all down */
281 if (ftpcmd(NULL, NULL, control_stream, buf) != 226) {
282 bb_error_msg_and_die("error: %s", buf + 4);
284 ftpcmd("QUIT", NULL, control_stream, buf);
286 return(EXIT_SUCCESS);
290 int ftpgetput_main(int argc, char **argv)
292 /* content-length of the file */
293 int option_index = -1;
296 /* socket to ftp server */
297 FILE *control_stream;
298 struct sockaddr_in s_in;
300 /* continue a prev transfer (-c) */
301 ftp_host_info_t *server;
303 int (*ftp_action)(ftp_host_info_t *, FILE *, const char *, char *) = NULL;
305 struct option long_options[] = {
306 {"username", 1, NULL, 'u'},
307 {"password", 1, NULL, 'p'},
308 {"port", 1, NULL, 'P'},
309 {"continue", 1, NULL, 'c'},
310 {"verbose", 0, NULL, 'v'},
315 if (bb_applet_name[3] == 'p') {
316 ftp_action = ftp_send;
320 if (bb_applet_name[3] == 'g') {
321 ftp_action = ftp_recieve;
325 /* Set default values */
330 * Decipher the command line
333 while ((opt = getopt_long(argc, argv, "u:p:P:cv", long_options, &option_index)) != EOF) {
339 server->user = optarg;
342 server->password = optarg;
345 server->port = optarg;
356 * Process the non-option command line arguments
358 if (argc - optind != 3) {
362 /* We want to do exactly _one_ DNS lookup, since some
363 * sites (i.e. ftp.us.debian.org) use round-robin DNS
364 * and we want to connect to only one IP... */
365 server->s_in = &s_in;
366 server->host = argv[optind];
367 bb_lookup_host(&s_in, server->host, NULL);
369 fprintf(stdout, "Connecting to %s[%s]:%s\n",
370 server->host, inet_ntoa(s_in.sin_addr), server->port);
373 /* Connect/Setup/Configure the FTP session */
374 control_stream = ftp_login(server);
376 return(ftp_action(server, control_stream, argv[optind + 1], argv[optind + 2]));
381 c-file-style: "linux"