Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 3 | * ftpget |
| 4 | * |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 5 | * Mini implementation of FTP to retrieve a remote file. |
| 6 | * |
| 7 | * Copyright (C) 2002 Jeff Angielski, The PTR Group <jeff@theptrgroup.com> |
Glenn L McGrath | c6992fe | 2004-04-25 05:11:19 +0000 | [diff] [blame] | 8 | * Copyright (C) 2002 Glenn McGrath <bug1@iinet.net.au> |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 9 | * |
| 10 | * Based on wget.c by Chip Rosenthal Covad Communications |
| 11 | * <chip@laserlink.net> |
| 12 | * |
Rob Landley | 6f03722 | 2005-11-08 00:52:31 +0000 | [diff] [blame] | 13 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 14 | */ |
| 15 | |
| 16 | #include <sys/types.h> |
| 17 | #include <sys/ioctl.h> |
| 18 | #include <sys/time.h> |
| 19 | #include <sys/stat.h> |
| 20 | |
| 21 | #include <ctype.h> |
| 22 | #include <errno.h> |
| 23 | #include <fcntl.h> |
| 24 | #include <getopt.h> |
| 25 | #include <stdio.h> |
| 26 | #include <stdlib.h> |
| 27 | #include <signal.h> |
| 28 | #include <string.h> |
| 29 | #include <unistd.h> |
| 30 | |
| 31 | #include <netinet/in.h> |
| 32 | #include <netdb.h> |
Eric Andersen | 04d055f | 2003-11-03 21:20:18 +0000 | [diff] [blame] | 33 | #include <sys/socket.h> |
| 34 | #include <arpa/inet.h> |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 35 | |
| 36 | #include "busybox.h" |
| 37 | |
| 38 | typedef struct ftp_host_info_s { |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 39 | char *user; |
| 40 | char *password; |
Eric Andersen | 04d055f | 2003-11-03 21:20:18 +0000 | [diff] [blame] | 41 | struct sockaddr_in *s_in; |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 42 | } ftp_host_info_t; |
| 43 | |
Glenn L McGrath | 266c1f5 | 2003-12-20 03:19:27 +0000 | [diff] [blame] | 44 | static char verbose_flag = 0; |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 45 | static char do_continue = 0; |
| 46 | |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 47 | static int ftpcmd(const char *s1, const char *s2, FILE *stream, char *buf) |
| 48 | { |
| 49 | if (verbose_flag) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 50 | bb_error_msg("cmd %s%s", s1, s2); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | if (s1) { |
| 54 | if (s2) { |
Paul Fox | 146e83a | 2005-07-19 21:26:57 +0000 | [diff] [blame] | 55 | fprintf(stream, "%s%s\r\n", s1, s2); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 56 | } else { |
Paul Fox | 146e83a | 2005-07-19 21:26:57 +0000 | [diff] [blame] | 57 | fprintf(stream, "%s\r\n", s1); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 58 | } |
| 59 | } |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 60 | do { |
Glenn L McGrath | 5ec5828 | 2004-05-04 10:43:34 +0000 | [diff] [blame] | 61 | char *buf_ptr; |
| 62 | |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 63 | if (fgets(buf, 510, stream) == NULL) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 64 | bb_perror_msg_and_die("fgets()"); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 65 | } |
Glenn L McGrath | 5ec5828 | 2004-05-04 10:43:34 +0000 | [diff] [blame] | 66 | buf_ptr = strstr(buf, "\r\n"); |
| 67 | if (buf_ptr) { |
| 68 | *buf_ptr = '\0'; |
| 69 | } |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 70 | } while (! isdigit(buf[0]) || buf[3] != ' '); |
| 71 | |
| 72 | return atoi(buf); |
| 73 | } |
| 74 | |
Eric Andersen | 04d055f | 2003-11-03 21:20:18 +0000 | [diff] [blame] | 75 | static int xconnect_ftpdata(ftp_host_info_t *server, const char *buf) |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 76 | { |
| 77 | char *buf_ptr; |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 78 | unsigned short port_num; |
| 79 | |
| 80 | buf_ptr = strrchr(buf, ','); |
| 81 | *buf_ptr = '\0'; |
| 82 | port_num = atoi(buf_ptr + 1); |
| 83 | |
| 84 | buf_ptr = strrchr(buf, ','); |
| 85 | *buf_ptr = '\0'; |
| 86 | port_num += atoi(buf_ptr + 1) * 256; |
| 87 | |
Eric Andersen | 04d055f | 2003-11-03 21:20:18 +0000 | [diff] [blame] | 88 | server->s_in->sin_port=htons(port_num); |
| 89 | return(xconnect(server->s_in)); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | static FILE *ftp_login(ftp_host_info_t *server) |
| 93 | { |
| 94 | FILE *control_stream; |
| 95 | char buf[512]; |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 96 | |
| 97 | /* Connect to the command socket */ |
Glenn L McGrath | 236e93d | 2003-12-20 05:43:34 +0000 | [diff] [blame] | 98 | control_stream = fdopen(xconnect(server->s_in), "r+"); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 99 | if (control_stream == NULL) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 100 | bb_perror_msg_and_die("Couldnt open control stream"); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | if (ftpcmd(NULL, NULL, control_stream, buf) != 220) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 104 | bb_error_msg_and_die("%s", buf + 4); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | /* Login to the server */ |
| 108 | switch (ftpcmd("USER ", server->user, control_stream, buf)) { |
| 109 | case 230: |
| 110 | break; |
| 111 | case 331: |
| 112 | if (ftpcmd("PASS ", server->password, control_stream, buf) != 230) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 113 | bb_error_msg_and_die("PASS error: %s", buf + 4); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 114 | } |
| 115 | break; |
| 116 | default: |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 117 | bb_error_msg_and_die("USER error: %s", buf + 4); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | ftpcmd("TYPE I", NULL, control_stream, buf); |
| 121 | |
| 122 | return(control_stream); |
| 123 | } |
| 124 | |
| 125 | #ifdef CONFIG_FTPGET |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 126 | static int ftp_recieve(ftp_host_info_t *server, FILE *control_stream, |
Eric Andersen | 04d055f | 2003-11-03 21:20:18 +0000 | [diff] [blame] | 127 | const char *local_path, char *server_path) |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 128 | { |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 129 | char buf[512]; |
| 130 | off_t filesize = 0; |
| 131 | int fd_data; |
Glenn L McGrath | 236e93d | 2003-12-20 05:43:34 +0000 | [diff] [blame] | 132 | int fd_local = -1; |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 133 | off_t beg_range = 0; |
| 134 | |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 135 | /* Connect to the data socket */ |
| 136 | if (ftpcmd("PASV", NULL, control_stream, buf) != 227) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 137 | bb_error_msg_and_die("PASV error: %s", buf + 4); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 138 | } |
Eric Andersen | 04d055f | 2003-11-03 21:20:18 +0000 | [diff] [blame] | 139 | fd_data = xconnect_ftpdata(server, buf); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 140 | |
| 141 | if (ftpcmd("SIZE ", server_path, control_stream, buf) == 213) { |
Eric Andersen | 2479445 | 2004-03-06 22:11:45 +0000 | [diff] [blame] | 142 | unsigned long value=filesize; |
Eric Andersen | ca65ca7 | 2004-03-15 08:46:37 +0000 | [diff] [blame] | 143 | if (safe_strtoul(buf + 4, &value)) |
Eric Andersen | 2479445 | 2004-03-06 22:11:45 +0000 | [diff] [blame] | 144 | bb_error_msg_and_die("SIZE error: %s", buf + 4); |
| 145 | filesize = value; |
Rob Landley | bc059bc | 2006-01-10 06:36:00 +0000 | [diff] [blame] | 146 | } else { |
| 147 | filesize = -1; |
| 148 | do_continue = 0; |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 149 | } |
| 150 | |
Glenn L McGrath | 236e93d | 2003-12-20 05:43:34 +0000 | [diff] [blame] | 151 | if ((local_path[0] == '-') && (local_path[1] == '\0')) { |
Eric Andersen | 70060d2 | 2004-03-27 10:02:48 +0000 | [diff] [blame] | 152 | fd_local = STDOUT_FILENO; |
Glenn L McGrath | 236e93d | 2003-12-20 05:43:34 +0000 | [diff] [blame] | 153 | do_continue = 0; |
| 154 | } |
| 155 | |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 156 | if (do_continue) { |
| 157 | struct stat sbuf; |
Glenn L McGrath | 236e93d | 2003-12-20 05:43:34 +0000 | [diff] [blame] | 158 | if (lstat(local_path, &sbuf) < 0) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 159 | bb_perror_msg_and_die("fstat()"); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 160 | } |
| 161 | if (sbuf.st_size > 0) { |
| 162 | beg_range = sbuf.st_size; |
| 163 | } else { |
| 164 | do_continue = 0; |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | if (do_continue) { |
Eric Andersen | 2a41ec6 | 2003-06-20 09:22:12 +0000 | [diff] [blame] | 169 | sprintf(buf, "REST %ld", (long)beg_range); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 170 | if (ftpcmd(buf, NULL, control_stream, buf) != 350) { |
| 171 | do_continue = 0; |
| 172 | } else { |
| 173 | filesize -= beg_range; |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | if (ftpcmd("RETR ", server_path, control_stream, buf) > 150) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 178 | bb_error_msg_and_die("RETR error: %s", buf + 4); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 179 | } |
| 180 | |
Glenn L McGrath | 1643f41 | 2002-12-18 02:47:40 +0000 | [diff] [blame] | 181 | /* only make a local file if we know that one exists on the remote server */ |
Glenn L McGrath | 236e93d | 2003-12-20 05:43:34 +0000 | [diff] [blame] | 182 | if (fd_local == -1) { |
| 183 | if (do_continue) { |
| 184 | fd_local = bb_xopen(local_path, O_APPEND | O_WRONLY); |
| 185 | } else { |
| 186 | fd_local = bb_xopen(local_path, O_CREAT | O_TRUNC | O_WRONLY); |
| 187 | } |
Glenn L McGrath | 1643f41 | 2002-12-18 02:47:40 +0000 | [diff] [blame] | 188 | } |
| 189 | |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 190 | /* Copy the file */ |
Rob Landley | bc059bc | 2006-01-10 06:36:00 +0000 | [diff] [blame] | 191 | if (filesize != -1) { |
| 192 | if (-1 == bb_copyfd_size(fd_data, fd_local, filesize)) |
| 193 | exit(EXIT_FAILURE); |
| 194 | } else { |
| 195 | if (-1 == bb_copyfd_eof(fd_data, fd_local)) |
| 196 | exit(EXIT_FAILURE); |
Glenn L McGrath | 25fe94f | 2002-12-13 08:20:44 +0000 | [diff] [blame] | 197 | } |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 198 | |
| 199 | /* close it all down */ |
| 200 | close(fd_data); |
| 201 | if (ftpcmd(NULL, NULL, control_stream, buf) != 226) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 202 | bb_error_msg_and_die("ftp error: %s", buf + 4); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 203 | } |
| 204 | ftpcmd("QUIT", NULL, control_stream, buf); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 205 | |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 206 | return(EXIT_SUCCESS); |
| 207 | } |
| 208 | #endif |
| 209 | |
| 210 | #ifdef CONFIG_FTPPUT |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 211 | static int ftp_send(ftp_host_info_t *server, FILE *control_stream, |
Eric Andersen | 04d055f | 2003-11-03 21:20:18 +0000 | [diff] [blame] | 212 | const char *server_path, char *local_path) |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 213 | { |
| 214 | struct stat sbuf; |
| 215 | char buf[512]; |
| 216 | int fd_data; |
| 217 | int fd_local; |
| 218 | int response; |
| 219 | |
| 220 | /* Connect to the data socket */ |
| 221 | if (ftpcmd("PASV", NULL, control_stream, buf) != 227) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 222 | bb_error_msg_and_die("PASV error: %s", buf + 4); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 223 | } |
Eric Andersen | 04d055f | 2003-11-03 21:20:18 +0000 | [diff] [blame] | 224 | fd_data = xconnect_ftpdata(server, buf); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 225 | |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 226 | /* get the local file */ |
Glenn L McGrath | 236e93d | 2003-12-20 05:43:34 +0000 | [diff] [blame] | 227 | if ((local_path[0] == '-') && (local_path[1] == '\0')) { |
Eric Andersen | 70060d2 | 2004-03-27 10:02:48 +0000 | [diff] [blame] | 228 | fd_local = STDIN_FILENO; |
Glenn L McGrath | 236e93d | 2003-12-20 05:43:34 +0000 | [diff] [blame] | 229 | } else { |
| 230 | fd_local = bb_xopen(local_path, O_RDONLY); |
| 231 | fstat(fd_local, &sbuf); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 232 | |
Glenn L McGrath | 236e93d | 2003-12-20 05:43:34 +0000 | [diff] [blame] | 233 | sprintf(buf, "ALLO %lu", (unsigned long)sbuf.st_size); |
| 234 | response = ftpcmd(buf, NULL, control_stream, buf); |
| 235 | switch (response) { |
| 236 | case 200: |
| 237 | case 202: |
| 238 | break; |
| 239 | default: |
| 240 | close(fd_local); |
| 241 | bb_error_msg_and_die("ALLO error: %s", buf + 4); |
| 242 | break; |
| 243 | } |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 244 | } |
Rob Landley | 6f03722 | 2005-11-08 00:52:31 +0000 | [diff] [blame] | 245 | response = ftpcmd("STOR ", server_path, control_stream, buf); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 246 | switch (response) { |
| 247 | case 125: |
| 248 | case 150: |
| 249 | break; |
| 250 | default: |
| 251 | close(fd_local); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 252 | bb_error_msg_and_die("STOR error: %s", buf + 4); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | /* transfer the file */ |
Glenn L McGrath | 7ffe133 | 2003-11-21 22:24:57 +0000 | [diff] [blame] | 256 | if (bb_copyfd_eof(fd_local, fd_data) == -1) { |
Glenn L McGrath | 25fe94f | 2002-12-13 08:20:44 +0000 | [diff] [blame] | 257 | exit(EXIT_FAILURE); |
| 258 | } |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 259 | |
| 260 | /* close it all down */ |
| 261 | close(fd_data); |
| 262 | if (ftpcmd(NULL, NULL, control_stream, buf) != 226) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 263 | bb_error_msg_and_die("error: %s", buf + 4); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 264 | } |
| 265 | ftpcmd("QUIT", NULL, control_stream, buf); |
| 266 | |
| 267 | return(EXIT_SUCCESS); |
| 268 | } |
| 269 | #endif |
| 270 | |
Glenn L McGrath | b51eb26 | 2003-12-19 10:37:52 +0000 | [diff] [blame] | 271 | #define FTPGETPUT_OPT_CONTINUE 1 |
| 272 | #define FTPGETPUT_OPT_VERBOSE 2 |
| 273 | #define FTPGETPUT_OPT_USER 4 |
| 274 | #define FTPGETPUT_OPT_PASSWORD 8 |
| 275 | #define FTPGETPUT_OPT_PORT 16 |
| 276 | |
| 277 | static const struct option ftpgetput_long_options[] = { |
| 278 | {"continue", 1, NULL, 'c'}, |
| 279 | {"verbose", 0, NULL, 'v'}, |
| 280 | {"username", 1, NULL, 'u'}, |
| 281 | {"password", 1, NULL, 'p'}, |
| 282 | {"port", 1, NULL, 'P'}, |
| 283 | {0, 0, 0, 0} |
| 284 | }; |
| 285 | |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 286 | int ftpgetput_main(int argc, char **argv) |
| 287 | { |
| 288 | /* content-length of the file */ |
Glenn L McGrath | b51eb26 | 2003-12-19 10:37:52 +0000 | [diff] [blame] | 289 | unsigned long opt; |
Glenn L McGrath | 266c1f5 | 2003-12-20 03:19:27 +0000 | [diff] [blame] | 290 | char *port = "ftp"; |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 291 | |
| 292 | /* socket to ftp server */ |
| 293 | FILE *control_stream; |
Eric Andersen | 04d055f | 2003-11-03 21:20:18 +0000 | [diff] [blame] | 294 | struct sockaddr_in s_in; |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 295 | |
| 296 | /* continue a prev transfer (-c) */ |
| 297 | ftp_host_info_t *server; |
| 298 | |
Eric Andersen | 04d055f | 2003-11-03 21:20:18 +0000 | [diff] [blame] | 299 | int (*ftp_action)(ftp_host_info_t *, FILE *, const char *, char *) = NULL; |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 300 | |
Glenn L McGrath | 266c1f5 | 2003-12-20 03:19:27 +0000 | [diff] [blame] | 301 | /* Check to see if the command is ftpget or ftput */ |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 302 | #ifdef CONFIG_FTPPUT |
Glenn L McGrath | 266c1f5 | 2003-12-20 03:19:27 +0000 | [diff] [blame] | 303 | # ifdef CONFIG_FTPGET |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 304 | if (bb_applet_name[3] == 'p') { |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 305 | ftp_action = ftp_send; |
Glenn L McGrath | 266c1f5 | 2003-12-20 03:19:27 +0000 | [diff] [blame] | 306 | } |
| 307 | # else |
| 308 | ftp_action = ftp_send; |
| 309 | # endif |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 310 | #endif |
| 311 | #ifdef CONFIG_FTPGET |
Glenn L McGrath | 266c1f5 | 2003-12-20 03:19:27 +0000 | [diff] [blame] | 312 | # ifdef CONFIG_FTPPUT |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 313 | if (bb_applet_name[3] == 'g') { |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 314 | ftp_action = ftp_recieve; |
| 315 | } |
Glenn L McGrath | 266c1f5 | 2003-12-20 03:19:27 +0000 | [diff] [blame] | 316 | # else |
| 317 | ftp_action = ftp_recieve; |
| 318 | # endif |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 319 | #endif |
| 320 | |
| 321 | /* Set default values */ |
Glenn L McGrath | 266c1f5 | 2003-12-20 03:19:27 +0000 | [diff] [blame] | 322 | server = xmalloc(sizeof(ftp_host_info_t)); |
| 323 | server->user = "anonymous"; |
| 324 | server->password = "busybox@"; |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 325 | verbose_flag = 0; |
| 326 | |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 327 | /* |
| 328 | * Decipher the command line |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 329 | */ |
Glenn L McGrath | b51eb26 | 2003-12-19 10:37:52 +0000 | [diff] [blame] | 330 | bb_applet_long_options = ftpgetput_long_options; |
Glenn L McGrath | 266c1f5 | 2003-12-20 03:19:27 +0000 | [diff] [blame] | 331 | opt = bb_getopt_ulflags(argc, argv, "cvu:p:P:", &server->user, &server->password, &port); |
Glenn L McGrath | 236e93d | 2003-12-20 05:43:34 +0000 | [diff] [blame] | 332 | |
| 333 | /* Process the non-option command line arguments */ |
| 334 | if (argc - optind != 3) { |
| 335 | bb_show_usage(); |
| 336 | } |
| 337 | |
Glenn L McGrath | b51eb26 | 2003-12-19 10:37:52 +0000 | [diff] [blame] | 338 | if (opt & FTPGETPUT_OPT_CONTINUE) { |
| 339 | do_continue = 1; |
| 340 | } |
| 341 | if (opt & FTPGETPUT_OPT_VERBOSE) { |
| 342 | verbose_flag = 1; |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 343 | } |
| 344 | |
Eric Andersen | 04d055f | 2003-11-03 21:20:18 +0000 | [diff] [blame] | 345 | /* We want to do exactly _one_ DNS lookup, since some |
| 346 | * sites (i.e. ftp.us.debian.org) use round-robin DNS |
| 347 | * and we want to connect to only one IP... */ |
| 348 | server->s_in = &s_in; |
Glenn L McGrath | 266c1f5 | 2003-12-20 03:19:27 +0000 | [diff] [blame] | 349 | bb_lookup_host(&s_in, argv[optind]); |
Glenn L McGrath | 036dbaa | 2004-01-17 05:03:31 +0000 | [diff] [blame] | 350 | s_in.sin_port = bb_lookup_port(port, "tcp", 21); |
Eric Andersen | 04d055f | 2003-11-03 21:20:18 +0000 | [diff] [blame] | 351 | if (verbose_flag) { |
Glenn L McGrath | 266c1f5 | 2003-12-20 03:19:27 +0000 | [diff] [blame] | 352 | printf("Connecting to %s[%s]:%d\n", |
| 353 | argv[optind], inet_ntoa(s_in.sin_addr), ntohs(s_in.sin_port)); |
Eric Andersen | 04d055f | 2003-11-03 21:20:18 +0000 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | /* Connect/Setup/Configure the FTP session */ |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 357 | control_stream = ftp_login(server); |
| 358 | |
Eric Andersen | 04d055f | 2003-11-03 21:20:18 +0000 | [diff] [blame] | 359 | return(ftp_action(server, control_stream, argv[optind + 1], argv[optind + 2])); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | /* |
| 363 | Local Variables: |
| 364 | c-file-style: "linux" |
| 365 | c-basic-offset: 4 |
| 366 | tab-width: 4 |
| 367 | End: |
| 368 | */ |