Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * ftpget |
| 4 | * |
| 5 | * Mini implementation of FTP to retrieve a remote file. |
| 6 | * |
| 7 | * Copyright (C) 2002 Jeff Angielski, The PTR Group <jeff@theptrgroup.com> |
| 8 | * Copyright (C) 2002 Glenn McGrath <bug1@optushome.com.au> |
| 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 | } |
| 73 | |
| 74 | do { |
| 75 | if (fgets(buf, 510, stream) == NULL) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 76 | bb_perror_msg_and_die("fgets()"); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 77 | } |
| 78 | } while (! isdigit(buf[0]) || buf[3] != ' '); |
| 79 | |
| 80 | return atoi(buf); |
| 81 | } |
| 82 | |
Eric Andersen | 04d055f | 2003-11-03 21:20:18 +0000 | [diff] [blame] | 83 | 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] | 84 | { |
| 85 | char *buf_ptr; |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 86 | unsigned short port_num; |
| 87 | |
| 88 | buf_ptr = strrchr(buf, ','); |
| 89 | *buf_ptr = '\0'; |
| 90 | port_num = atoi(buf_ptr + 1); |
| 91 | |
| 92 | buf_ptr = strrchr(buf, ','); |
| 93 | *buf_ptr = '\0'; |
| 94 | port_num += atoi(buf_ptr + 1) * 256; |
| 95 | |
Eric Andersen | 04d055f | 2003-11-03 21:20:18 +0000 | [diff] [blame] | 96 | server->s_in->sin_port=htons(port_num); |
| 97 | return(xconnect(server->s_in)); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | static FILE *ftp_login(ftp_host_info_t *server) |
| 101 | { |
| 102 | FILE *control_stream; |
| 103 | char buf[512]; |
| 104 | int control_fd; |
| 105 | |
| 106 | /* Connect to the command socket */ |
Eric Andersen | 04d055f | 2003-11-03 21:20:18 +0000 | [diff] [blame] | 107 | control_fd = xconnect(server->s_in); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 108 | control_stream = fdopen(control_fd, "r+"); |
| 109 | if (control_stream == NULL) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 110 | bb_perror_msg_and_die("Couldnt open control stream"); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | if (ftpcmd(NULL, NULL, control_stream, buf) != 220) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 114 | bb_error_msg_and_die("%s", buf + 4); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | /* Login to the server */ |
| 118 | switch (ftpcmd("USER ", server->user, control_stream, buf)) { |
| 119 | case 230: |
| 120 | break; |
| 121 | case 331: |
| 122 | if (ftpcmd("PASS ", server->password, control_stream, buf) != 230) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 123 | bb_error_msg_and_die("PASS error: %s", buf + 4); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 124 | } |
| 125 | break; |
| 126 | default: |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 127 | bb_error_msg_and_die("USER error: %s", buf + 4); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | ftpcmd("TYPE I", NULL, control_stream, buf); |
| 131 | |
| 132 | return(control_stream); |
| 133 | } |
| 134 | |
| 135 | #ifdef CONFIG_FTPGET |
Eric Andersen | 04d055f | 2003-11-03 21:20:18 +0000 | [diff] [blame] | 136 | static int ftp_recieve(ftp_host_info_t *server, FILE *control_stream, |
| 137 | const char *local_path, char *server_path) |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 138 | { |
| 139 | char *filename; |
| 140 | char *local_file; |
| 141 | char buf[512]; |
| 142 | off_t filesize = 0; |
| 143 | int fd_data; |
| 144 | int fd_local; |
| 145 | off_t beg_range = 0; |
| 146 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 147 | filename = bb_get_last_path_component(server_path); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 148 | local_file = concat_path_file(local_path, filename); |
| 149 | |
| 150 | /* Connect to the data socket */ |
| 151 | if (ftpcmd("PASV", NULL, control_stream, buf) != 227) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 152 | bb_error_msg_and_die("PASV error: %s", buf + 4); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 153 | } |
Eric Andersen | 04d055f | 2003-11-03 21:20:18 +0000 | [diff] [blame] | 154 | fd_data = xconnect_ftpdata(server, buf); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 155 | |
| 156 | if (ftpcmd("SIZE ", server_path, control_stream, buf) == 213) { |
| 157 | filesize = atol(buf + 4); |
| 158 | } |
| 159 | |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 160 | if (do_continue) { |
| 161 | struct stat sbuf; |
Glenn L McGrath | 1643f41 | 2002-12-18 02:47:40 +0000 | [diff] [blame] | 162 | if (lstat(local_file, &sbuf) < 0) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 163 | bb_perror_msg_and_die("fstat()"); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 164 | } |
| 165 | if (sbuf.st_size > 0) { |
| 166 | beg_range = sbuf.st_size; |
| 167 | } else { |
| 168 | do_continue = 0; |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | if (do_continue) { |
Eric Andersen | 2a41ec6 | 2003-06-20 09:22:12 +0000 | [diff] [blame] | 173 | sprintf(buf, "REST %ld", (long)beg_range); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 174 | if (ftpcmd(buf, NULL, control_stream, buf) != 350) { |
| 175 | do_continue = 0; |
| 176 | } else { |
| 177 | filesize -= beg_range; |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | if (ftpcmd("RETR ", server_path, control_stream, buf) > 150) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 182 | bb_error_msg_and_die("RETR error: %s", buf + 4); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 183 | } |
| 184 | |
Glenn L McGrath | 1643f41 | 2002-12-18 02:47:40 +0000 | [diff] [blame] | 185 | /* only make a local file if we know that one exists on the remote server */ |
| 186 | if (do_continue) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 187 | fd_local = bb_xopen(local_file, O_APPEND | O_WRONLY); |
Glenn L McGrath | 1643f41 | 2002-12-18 02:47:40 +0000 | [diff] [blame] | 188 | } else { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 189 | fd_local = bb_xopen(local_file, O_CREAT | O_TRUNC | O_WRONLY); |
Glenn L McGrath | 1643f41 | 2002-12-18 02:47:40 +0000 | [diff] [blame] | 190 | } |
| 191 | |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 192 | /* Copy the file */ |
Glenn L McGrath | 7ffe133 | 2003-11-21 22:24:57 +0000 | [diff] [blame] | 193 | if (bb_copyfd_size(fd_data, fd_local, filesize) == -1) { |
Glenn L McGrath | 25fe94f | 2002-12-13 08:20:44 +0000 | [diff] [blame] | 194 | exit(EXIT_FAILURE); |
| 195 | } |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 196 | |
| 197 | /* close it all down */ |
| 198 | close(fd_data); |
| 199 | if (ftpcmd(NULL, NULL, control_stream, buf) != 226) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 200 | bb_error_msg_and_die("ftp error: %s", buf + 4); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 201 | } |
| 202 | ftpcmd("QUIT", NULL, control_stream, buf); |
| 203 | |
| 204 | return(EXIT_SUCCESS); |
| 205 | } |
| 206 | #endif |
| 207 | |
| 208 | #ifdef CONFIG_FTPPUT |
Eric Andersen | 04d055f | 2003-11-03 21:20:18 +0000 | [diff] [blame] | 209 | static int ftp_send(ftp_host_info_t *server, FILE *control_stream, |
| 210 | const char *server_path, char *local_path) |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 211 | { |
| 212 | struct stat sbuf; |
| 213 | char buf[512]; |
| 214 | int fd_data; |
| 215 | int fd_local; |
| 216 | int response; |
| 217 | |
| 218 | /* Connect to the data socket */ |
| 219 | if (ftpcmd("PASV", NULL, control_stream, buf) != 227) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 220 | bb_error_msg_and_die("PASV error: %s", buf + 4); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 221 | } |
Eric Andersen | 04d055f | 2003-11-03 21:20:18 +0000 | [diff] [blame] | 222 | fd_data = xconnect_ftpdata(server, buf); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 223 | |
| 224 | if (ftpcmd("CWD ", server_path, control_stream, buf) != 250) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 225 | bb_error_msg_and_die("CWD error: %s", buf + 4); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | /* get the local file */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 229 | fd_local = bb_xopen(local_path, O_RDONLY); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 230 | fstat(fd_local, &sbuf); |
| 231 | |
Eric Andersen | 2a41ec6 | 2003-06-20 09:22:12 +0000 | [diff] [blame] | 232 | sprintf(buf, "ALLO %lu", (unsigned long)sbuf.st_size); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 233 | response = ftpcmd(buf, NULL, control_stream, buf); |
| 234 | switch (response) { |
| 235 | case 200: |
| 236 | case 202: |
| 237 | break; |
| 238 | default: |
| 239 | close(fd_local); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 240 | bb_error_msg_and_die("ALLO error: %s", buf + 4); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 241 | break; |
| 242 | } |
| 243 | |
| 244 | response = ftpcmd("STOR ", local_path, control_stream, buf); |
| 245 | switch (response) { |
| 246 | case 125: |
| 247 | case 150: |
| 248 | break; |
| 249 | default: |
| 250 | close(fd_local); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 251 | bb_error_msg_and_die("STOR error: %s", buf + 4); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | /* transfer the file */ |
Glenn L McGrath | 7ffe133 | 2003-11-21 22:24:57 +0000 | [diff] [blame] | 255 | if (bb_copyfd_eof(fd_local, fd_data) == -1) { |
Glenn L McGrath | 25fe94f | 2002-12-13 08:20:44 +0000 | [diff] [blame] | 256 | exit(EXIT_FAILURE); |
| 257 | } |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 258 | |
| 259 | /* close it all down */ |
| 260 | close(fd_data); |
| 261 | if (ftpcmd(NULL, NULL, control_stream, buf) != 226) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 262 | bb_error_msg_and_die("error: %s", buf + 4); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 263 | } |
| 264 | ftpcmd("QUIT", NULL, control_stream, buf); |
| 265 | |
| 266 | return(EXIT_SUCCESS); |
| 267 | } |
| 268 | #endif |
| 269 | |
Glenn L McGrath | b51eb26 | 2003-12-19 10:37:52 +0000 | [diff] [blame] | 270 | #define FTPGETPUT_OPT_CONTINUE 1 |
| 271 | #define FTPGETPUT_OPT_VERBOSE 2 |
| 272 | #define FTPGETPUT_OPT_USER 4 |
| 273 | #define FTPGETPUT_OPT_PASSWORD 8 |
| 274 | #define FTPGETPUT_OPT_PORT 16 |
| 275 | |
| 276 | static const struct option ftpgetput_long_options[] = { |
| 277 | {"continue", 1, NULL, 'c'}, |
| 278 | {"verbose", 0, NULL, 'v'}, |
| 279 | {"username", 1, NULL, 'u'}, |
| 280 | {"password", 1, NULL, 'p'}, |
| 281 | {"port", 1, NULL, 'P'}, |
| 282 | {0, 0, 0, 0} |
| 283 | }; |
| 284 | |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 285 | int ftpgetput_main(int argc, char **argv) |
| 286 | { |
| 287 | /* content-length of the file */ |
Glenn L McGrath | b51eb26 | 2003-12-19 10:37:52 +0000 | [diff] [blame] | 288 | unsigned long opt; |
Glenn L McGrath | 266c1f5 | 2003-12-20 03:19:27 +0000 | [diff] [blame^] | 289 | char *port = "ftp"; |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 290 | |
| 291 | /* socket to ftp server */ |
| 292 | FILE *control_stream; |
Eric Andersen | 04d055f | 2003-11-03 21:20:18 +0000 | [diff] [blame] | 293 | struct sockaddr_in s_in; |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 294 | |
| 295 | /* continue a prev transfer (-c) */ |
| 296 | ftp_host_info_t *server; |
| 297 | |
Eric Andersen | 04d055f | 2003-11-03 21:20:18 +0000 | [diff] [blame] | 298 | 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] | 299 | |
Glenn L McGrath | 266c1f5 | 2003-12-20 03:19:27 +0000 | [diff] [blame^] | 300 | /* Check to see if the command is ftpget or ftput */ |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 301 | #ifdef CONFIG_FTPPUT |
Glenn L McGrath | 266c1f5 | 2003-12-20 03:19:27 +0000 | [diff] [blame^] | 302 | # ifdef CONFIG_FTPGET |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 303 | if (bb_applet_name[3] == 'p') { |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 304 | ftp_action = ftp_send; |
Glenn L McGrath | 266c1f5 | 2003-12-20 03:19:27 +0000 | [diff] [blame^] | 305 | } |
| 306 | # else |
| 307 | ftp_action = ftp_send; |
| 308 | # endif |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 309 | #endif |
| 310 | #ifdef CONFIG_FTPGET |
Glenn L McGrath | 266c1f5 | 2003-12-20 03:19:27 +0000 | [diff] [blame^] | 311 | # ifdef CONFIG_FTPPUT |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 312 | if (bb_applet_name[3] == 'g') { |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 313 | ftp_action = ftp_recieve; |
| 314 | } |
Glenn L McGrath | 266c1f5 | 2003-12-20 03:19:27 +0000 | [diff] [blame^] | 315 | # else |
| 316 | ftp_action = ftp_recieve; |
| 317 | # endif |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 318 | #endif |
| 319 | |
| 320 | /* Set default values */ |
Glenn L McGrath | 266c1f5 | 2003-12-20 03:19:27 +0000 | [diff] [blame^] | 321 | server = xmalloc(sizeof(ftp_host_info_t)); |
| 322 | server->user = "anonymous"; |
| 323 | server->password = "busybox@"; |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 324 | verbose_flag = 0; |
| 325 | |
| 326 | /* |
| 327 | * Decipher the command line |
| 328 | */ |
Glenn L McGrath | b51eb26 | 2003-12-19 10:37:52 +0000 | [diff] [blame] | 329 | bb_applet_long_options = ftpgetput_long_options; |
Glenn L McGrath | 266c1f5 | 2003-12-20 03:19:27 +0000 | [diff] [blame^] | 330 | opt = bb_getopt_ulflags(argc, argv, "cvu:p:P:", &server->user, &server->password, &port); |
Glenn L McGrath | b51eb26 | 2003-12-19 10:37:52 +0000 | [diff] [blame] | 331 | if (opt & FTPGETPUT_OPT_CONTINUE) { |
| 332 | do_continue = 1; |
| 333 | } |
| 334 | if (opt & FTPGETPUT_OPT_VERBOSE) { |
| 335 | verbose_flag = 1; |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | /* |
| 339 | * Process the non-option command line arguments |
| 340 | */ |
| 341 | if (argc - optind != 3) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 342 | bb_show_usage(); |
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]); |
| 350 | s_in.sin_port = bb_lookup_port(port, 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 | */ |