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 /* If chunksize == 0 read till end of file */
60 static int copyfd_chunk(int fd1, int fd2, off_t chunksize)
68 if ((chunksize > BUFSIZ) || (chunksize == 0)) {
74 nread = safe_read(fd1, buffer, size);
78 perror_msg_and_die("read error");
84 nwritten = full_write(fd2, buffer, nread);
86 if (nwritten != nread) {
87 error_msg_and_die("Unable to write all data");
91 chunksize -= nwritten;
99 static ftp_host_info_t *ftp_init(void)
101 ftp_host_info_t *host;
103 host = xcalloc(1, sizeof(ftp_host_info_t));
105 /* Set the default port */
106 if (getservbyname("ftp", "tcp")) {
111 host->user = "anonymous";
112 host->password = "busybox@";
117 static int ftpcmd(const char *s1, const char *s2, FILE *stream, char *buf)
120 error_msg("cmd %s%s", s1, s2);
125 fprintf(stream, "%s%s\n", s1, s2);
127 fprintf(stream, "%s\n", s1);
132 if (fgets(buf, 510, stream) == NULL) {
133 perror_msg_and_die("fgets()");
135 } while (! isdigit(buf[0]) || buf[3] != ' ');
140 static int xconnect_ftpdata(const char *target_host, const char *buf)
144 unsigned short port_num;
146 buf_ptr = strrchr(buf, ',');
148 port_num = atoi(buf_ptr + 1);
150 buf_ptr = strrchr(buf, ',');
152 port_num += atoi(buf_ptr + 1) * 256;
154 sprintf(data_port, "%d", port_num);
155 return(xconnect(target_host, data_port));
158 static FILE *ftp_login(ftp_host_info_t *server)
160 FILE *control_stream;
164 /* Connect to the command socket */
165 control_fd = xconnect(server->host, server->port);
166 control_stream = fdopen(control_fd, "r+");
167 if (control_stream == NULL) {
168 perror_msg_and_die("Couldnt open control stream");
171 if (ftpcmd(NULL, NULL, control_stream, buf) != 220) {
172 error_msg_and_die("%s", buf + 4);
175 /* Login to the server */
176 switch (ftpcmd("USER ", server->user, control_stream, buf)) {
180 if (ftpcmd("PASS ", server->password, control_stream, buf) != 230) {
181 error_msg_and_die("PASS error: %s", buf + 4);
185 error_msg_and_die("USER error: %s", buf + 4);
188 ftpcmd("TYPE I", NULL, control_stream, buf);
190 return(control_stream);
194 static int ftp_recieve(FILE *control_stream, const char *host, const char *local_path, char *server_path)
204 filename = get_last_path_component(server_path);
205 local_file = concat_path_file(local_path, filename);
207 /* Connect to the data socket */
208 if (ftpcmd("PASV", NULL, control_stream, buf) != 227) {
209 error_msg_and_die("PASV error: %s", buf + 4);
211 fd_data = xconnect_ftpdata(host, buf);
213 if (ftpcmd("SIZE ", server_path, control_stream, buf) == 213) {
214 filesize = atol(buf + 4);
217 /* only make a local file if we know that one exists on the remote server */
219 fd_local = xopen(local_file, O_APPEND | O_WRONLY);
221 fd_local = xopen(local_file, O_CREAT | O_TRUNC | O_WRONLY);
226 if (fstat(fd_local, &sbuf) < 0) {
227 perror_msg_and_die("fstat()");
229 if (sbuf.st_size > 0) {
230 beg_range = sbuf.st_size;
237 sprintf(buf, "REST %ld", beg_range);
238 if (ftpcmd(buf, NULL, control_stream, buf) != 350) {
241 filesize -= beg_range;
245 if (ftpcmd("RETR ", server_path, control_stream, buf) > 150) {
246 error_msg_and_die("RETR error: %s", buf + 4);
250 copyfd_chunk(fd_data, fd_local, filesize);
252 /* close it all down */
254 if (ftpcmd(NULL, NULL, control_stream, buf) != 226) {
255 error_msg_and_die("ftp error: %s", buf + 4);
257 ftpcmd("QUIT", NULL, control_stream, buf);
259 return(EXIT_SUCCESS);
264 static int ftp_send(FILE *control_stream, const char *host, const char *local_path, char *server_path)
272 /* Connect to the data socket */
273 if (ftpcmd("PASV", NULL, control_stream, buf) != 227) {
274 error_msg_and_die("PASV error: %s", buf + 4);
276 fd_data = xconnect_ftpdata(host, buf);
278 if (ftpcmd("CWD ", server_path, control_stream, buf) != 250) {
279 error_msg_and_die("CWD error: %s", buf + 4);
282 /* get the local file */
283 fd_local = xopen(local_path, O_RDONLY);
284 fstat(fd_local, &sbuf);
286 sprintf(buf, "ALLO %lu", sbuf.st_size);
287 response = ftpcmd(buf, NULL, control_stream, buf);
294 error_msg_and_die("ALLO error: %s", buf + 4);
298 response = ftpcmd("STOR ", local_path, control_stream, buf);
305 error_msg_and_die("STOR error: %s", buf + 4);
308 /* transfer the file */
309 copyfd_chunk(fd_local, fd_data, 0);
311 /* close it all down */
313 if (ftpcmd(NULL, NULL, control_stream, buf) != 226) {
314 error_msg_and_die("error: %s", buf + 4);
316 ftpcmd("QUIT", NULL, control_stream, buf);
318 return(EXIT_SUCCESS);
322 int ftpgetput_main(int argc, char **argv)
324 /* content-length of the file */
325 int option_index = -1;
328 /* socket to ftp server */
329 FILE *control_stream;
331 /* continue a prev transfer (-c) */
332 ftp_host_info_t *server;
334 int (*ftp_action)(FILE *, const char *, const char *, char *) = NULL;
336 struct option long_options[] = {
337 {"username", 1, NULL, 'u'},
338 {"password", 1, NULL, 'p'},
339 {"port", 1, NULL, 'P'},
340 {"continue", 1, NULL, 'c'},
341 {"verbose", 0, NULL, 'v'},
346 if (applet_name[3] == 'p') {
347 ftp_action = ftp_send;
351 if (applet_name[3] == 'g') {
352 ftp_action = ftp_recieve;
356 /* Set default values */
361 * Decipher the command line
363 while ((opt = getopt_long(argc, argv, "u:p:P:cv", long_options, &option_index)) != EOF) {
369 server->user = optarg;
372 server->password = optarg;
375 server->port = optarg;
386 * Process the non-option command line arguments
388 if (argc - optind != 3) {
392 /* Connect/Setup/Configure the FTP session */
393 server->host = argv[optind];
394 control_stream = ftp_login(server);
396 return(ftp_action(control_stream, argv[optind], argv[optind + 1], argv[optind + 2]));
401 c-file-style: "linux"