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