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