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 int copyfd_chunk(int fd1, int fd2, const off_t chunksize)
68 remaining = chunksize;
74 if ((chunksize > BUFSIZ) || (chunksize == 0)) {
80 nread = safe_read(fd1, buffer, size);
84 perror_msg_and_die("read error");
90 nwritten = full_write(fd2, buffer, nread);
92 if (nwritten != nread) {
93 error_msg_and_die("Unable to write all data");
97 remaining -= nwritten;
99 } while (remaining != 0);
104 static ftp_host_info_t *ftp_init(void)
106 ftp_host_info_t *host;
108 host = xcalloc(1, sizeof(ftp_host_info_t));
110 /* Set the default port */
111 if (getservbyname("ftp", "tcp")) {
116 host->user = "anonymous";
117 host->password = "busybox@";
122 static int ftpcmd(const char *s1, const char *s2, FILE *stream, char *buf)
125 error_msg("cmd %s%s", s1, s2);
130 fprintf(stream, "%s%s\n", s1, s2);
132 fprintf(stream, "%s\n", s1);
137 if (fgets(buf, 510, stream) == NULL) {
138 perror_msg_and_die("fgets()");
140 } while (! isdigit(buf[0]) || buf[3] != ' ');
145 static int xconnect_ftpdata(const char *target_host, const char *buf)
149 unsigned short port_num;
151 buf_ptr = strrchr(buf, ',');
153 port_num = atoi(buf_ptr + 1);
155 buf_ptr = strrchr(buf, ',');
157 port_num += atoi(buf_ptr + 1) * 256;
159 sprintf(data_port, "%d", port_num);
160 return(xconnect(target_host, data_port));
163 static FILE *ftp_login(ftp_host_info_t *server)
165 FILE *control_stream;
169 /* Connect to the command socket */
170 control_fd = xconnect(server->host, server->port);
171 control_stream = fdopen(control_fd, "r+");
172 if (control_stream == NULL) {
173 perror_msg_and_die("Couldnt open control stream");
176 if (ftpcmd(NULL, NULL, control_stream, buf) != 220) {
177 error_msg_and_die("%s", buf + 4);
180 /* Login to the server */
181 switch (ftpcmd("USER ", server->user, control_stream, buf)) {
185 if (ftpcmd("PASS ", server->password, control_stream, buf) != 230) {
186 error_msg_and_die("PASS error: %s", buf + 4);
190 error_msg_and_die("USER error: %s", buf + 4);
193 ftpcmd("TYPE I", NULL, control_stream, buf);
195 return(control_stream);
199 static int ftp_recieve(FILE *control_stream, const char *host, const char *local_path, char *server_path)
209 filename = get_last_path_component(server_path);
210 local_file = concat_path_file(local_path, filename);
212 /* Connect to the data socket */
213 if (ftpcmd("PASV", NULL, control_stream, buf) != 227) {
214 error_msg_and_die("PASV error: %s", buf + 4);
216 fd_data = xconnect_ftpdata(host, buf);
218 if (ftpcmd("SIZE ", server_path, control_stream, buf) == 213) {
219 filesize = atol(buf + 4);
222 /* only make a local file if we know that one exists on the remote server */
224 fd_local = xopen(local_file, O_APPEND | O_WRONLY);
226 fd_local = xopen(local_file, O_CREAT | O_TRUNC | O_WRONLY);
231 if (fstat(fd_local, &sbuf) < 0) {
232 perror_msg_and_die("fstat()");
234 if (sbuf.st_size > 0) {
235 beg_range = sbuf.st_size;
242 sprintf(buf, "REST %ld", beg_range);
243 if (ftpcmd(buf, NULL, control_stream, buf) != 350) {
246 filesize -= beg_range;
250 if (ftpcmd("RETR ", server_path, control_stream, buf) > 150) {
251 error_msg_and_die("RETR error: %s", buf + 4);
255 copyfd_chunk(fd_data, fd_local, filesize);
257 /* close it all down */
259 if (ftpcmd(NULL, NULL, control_stream, buf) != 226) {
260 error_msg_and_die("ftp error: %s", buf + 4);
262 ftpcmd("QUIT", NULL, control_stream, buf);
264 return(EXIT_SUCCESS);
269 static int ftp_send(FILE *control_stream, const char *host, const char *server_path, char *local_path)
277 /* Connect to the data socket */
278 if (ftpcmd("PASV", NULL, control_stream, buf) != 227) {
279 error_msg_and_die("PASV error: %s", buf + 4);
281 fd_data = xconnect_ftpdata(host, buf);
283 if (ftpcmd("CWD ", server_path, control_stream, buf) != 250) {
284 error_msg_and_die("CWD error: %s", buf + 4);
287 /* get the local file */
288 fd_local = xopen(local_path, O_RDONLY);
289 fstat(fd_local, &sbuf);
291 sprintf(buf, "ALLO %lu", sbuf.st_size);
292 response = ftpcmd(buf, NULL, control_stream, buf);
299 error_msg_and_die("ALLO error: %s", buf + 4);
303 response = ftpcmd("STOR ", local_path, control_stream, buf);
310 error_msg_and_die("STOR error: %s", buf + 4);
313 /* transfer the file */
314 copyfd_chunk(fd_local, fd_data, 0);
316 /* close it all down */
318 if (ftpcmd(NULL, NULL, control_stream, buf) != 226) {
319 error_msg_and_die("error: %s", buf + 4);
321 ftpcmd("QUIT", NULL, control_stream, buf);
323 return(EXIT_SUCCESS);
327 int ftpgetput_main(int argc, char **argv)
329 /* content-length of the file */
330 int option_index = -1;
333 /* socket to ftp server */
334 FILE *control_stream;
336 /* continue a prev transfer (-c) */
337 ftp_host_info_t *server;
339 int (*ftp_action)(FILE *, const char *, const char *, char *) = NULL;
341 struct option long_options[] = {
342 {"username", 1, NULL, 'u'},
343 {"password", 1, NULL, 'p'},
344 {"port", 1, NULL, 'P'},
345 {"continue", 1, NULL, 'c'},
346 {"verbose", 0, NULL, 'v'},
351 if (applet_name[3] == 'p') {
352 ftp_action = ftp_send;
356 if (applet_name[3] == 'g') {
357 ftp_action = ftp_recieve;
361 /* Set default values */
366 * Decipher the command line
368 while ((opt = getopt_long(argc, argv, "u:p:P:cv", long_options, &option_index)) != EOF) {
374 server->user = optarg;
377 server->password = optarg;
380 server->port = optarg;
391 * Process the non-option command line arguments
393 if (argc - optind != 3) {
397 /* Connect/Setup/Configure the FTP session */
398 server->host = argv[optind];
399 control_stream = ftp_login(server);
401 return(ftp_action(control_stream, argv[optind], argv[optind + 1], argv[optind + 2]));
406 c-file-style: "linux"