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>
49 typedef struct ftp_host_info_s {
56 static char verbose_flag;
57 static char do_continue = 0;
59 static ftp_host_info_t *ftp_init(void)
61 ftp_host_info_t *host;
63 host = xcalloc(1, sizeof(ftp_host_info_t));
65 /* Set the default port */
66 if (getservbyname("ftp", "tcp")) {
71 host->user = "anonymous";
72 host->password = "busybox@";
77 static int ftpcmd(const char *s1, const char *s2, FILE *stream, char *buf)
80 error_msg("cmd %s%s", s1, s2);
85 fprintf(stream, "%s%s\n", s1, s2);
87 fprintf(stream, "%s\n", s1);
92 if (fgets(buf, 510, stream) == NULL) {
93 perror_msg_and_die("fgets()");
95 } while (! isdigit(buf[0]) || buf[3] != ' ');
100 static int xconnect_ftpdata(const char *target_host, const char *buf)
104 unsigned short port_num;
106 buf_ptr = strrchr(buf, ',');
108 port_num = atoi(buf_ptr + 1);
110 buf_ptr = strrchr(buf, ',');
112 port_num += atoi(buf_ptr + 1) * 256;
114 sprintf(data_port, "%d", port_num);
115 return(xconnect(target_host, data_port));
118 static FILE *ftp_login(ftp_host_info_t *server)
120 FILE *control_stream;
124 /* Connect to the command socket */
125 control_fd = xconnect(server->host, server->port);
126 control_stream = fdopen(control_fd, "r+");
127 if (control_stream == NULL) {
128 perror_msg_and_die("Couldnt open control stream");
131 if (ftpcmd(NULL, NULL, control_stream, buf) != 220) {
132 error_msg_and_die("%s", buf + 4);
135 /* Login to the server */
136 switch (ftpcmd("USER ", server->user, control_stream, buf)) {
140 if (ftpcmd("PASS ", server->password, control_stream, buf) != 230) {
141 error_msg_and_die("PASS error: %s", buf + 4);
145 error_msg_and_die("USER error: %s", buf + 4);
148 ftpcmd("TYPE I", NULL, control_stream, buf);
150 return(control_stream);
154 static int ftp_recieve(FILE *control_stream, const char *host, const char *local_path, char *server_path)
164 filename = get_last_path_component(server_path);
165 local_file = concat_path_file(local_path, filename);
167 /* Connect to the data socket */
168 if (ftpcmd("PASV", NULL, control_stream, buf) != 227) {
169 error_msg_and_die("PASV error: %s", buf + 4);
171 fd_data = xconnect_ftpdata(host, buf);
173 if (ftpcmd("SIZE ", server_path, control_stream, buf) == 213) {
174 filesize = atol(buf + 4);
179 if (lstat(local_file, &sbuf) < 0) {
180 perror_msg_and_die("fstat()");
182 if (sbuf.st_size > 0) {
183 beg_range = sbuf.st_size;
190 sprintf(buf, "REST %ld", beg_range);
191 if (ftpcmd(buf, NULL, control_stream, buf) != 350) {
194 filesize -= beg_range;
198 if (ftpcmd("RETR ", server_path, control_stream, buf) > 150) {
199 error_msg_and_die("RETR error: %s", buf + 4);
202 /* only make a local file if we know that one exists on the remote server */
204 fd_local = xopen(local_file, O_APPEND | O_WRONLY);
206 fd_local = xopen(local_file, O_CREAT | O_TRUNC | O_WRONLY);
210 if (copyfd(fd_data, fd_local, filesize) == -1) {
214 /* close it all down */
216 if (ftpcmd(NULL, NULL, control_stream, buf) != 226) {
217 error_msg_and_die("ftp error: %s", buf + 4);
219 ftpcmd("QUIT", NULL, control_stream, buf);
221 return(EXIT_SUCCESS);
226 static int ftp_send(FILE *control_stream, const char *host, const char *server_path, char *local_path)
234 /* Connect to the data socket */
235 if (ftpcmd("PASV", NULL, control_stream, buf) != 227) {
236 error_msg_and_die("PASV error: %s", buf + 4);
238 fd_data = xconnect_ftpdata(host, buf);
240 if (ftpcmd("CWD ", server_path, control_stream, buf) != 250) {
241 error_msg_and_die("CWD error: %s", buf + 4);
244 /* get the local file */
245 fd_local = xopen(local_path, O_RDONLY);
246 fstat(fd_local, &sbuf);
248 sprintf(buf, "ALLO %lu", sbuf.st_size);
249 response = ftpcmd(buf, NULL, control_stream, buf);
256 error_msg_and_die("ALLO error: %s", buf + 4);
260 response = ftpcmd("STOR ", local_path, control_stream, buf);
267 error_msg_and_die("STOR error: %s", buf + 4);
270 /* transfer the file */
271 if (copyfd(fd_local, fd_data, 0) == -1) {
275 /* close it all down */
277 if (ftpcmd(NULL, NULL, control_stream, buf) != 226) {
278 error_msg_and_die("error: %s", buf + 4);
280 ftpcmd("QUIT", NULL, control_stream, buf);
282 return(EXIT_SUCCESS);
286 int ftpgetput_main(int argc, char **argv)
288 /* content-length of the file */
289 int option_index = -1;
292 /* socket to ftp server */
293 FILE *control_stream;
295 /* continue a prev transfer (-c) */
296 ftp_host_info_t *server;
298 int (*ftp_action)(FILE *, const char *, const char *, char *) = NULL;
300 struct option long_options[] = {
301 {"username", 1, NULL, 'u'},
302 {"password", 1, NULL, 'p'},
303 {"port", 1, NULL, 'P'},
304 {"continue", 1, NULL, 'c'},
305 {"verbose", 0, NULL, 'v'},
310 if (applet_name[3] == 'p') {
311 ftp_action = ftp_send;
315 if (applet_name[3] == 'g') {
316 ftp_action = ftp_recieve;
320 /* Set default values */
325 * Decipher the command line
327 while ((opt = getopt_long(argc, argv, "u:p:P:cv", long_options, &option_index)) != EOF) {
333 server->user = optarg;
336 server->password = optarg;
339 server->port = optarg;
350 * Process the non-option command line arguments
352 if (argc - optind != 3) {
356 /* Connect/Setup/Configure the FTP session */
357 server->host = argv[optind];
358 control_stream = ftp_login(server);
360 return(ftp_action(control_stream, argv[optind], argv[optind + 1], argv[optind + 2]));
365 c-file-style: "linux"