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> |
| 46 | |
| 47 | #include "busybox.h" |
| 48 | |
| 49 | typedef struct ftp_host_info_s { |
| 50 | char *host; |
| 51 | char *port; |
| 52 | char *user; |
| 53 | char *password; |
| 54 | } ftp_host_info_t; |
| 55 | |
| 56 | static char verbose_flag; |
| 57 | static char do_continue = 0; |
| 58 | |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 59 | static ftp_host_info_t *ftp_init(void) |
| 60 | { |
| 61 | ftp_host_info_t *host; |
| 62 | |
| 63 | host = xcalloc(1, sizeof(ftp_host_info_t)); |
| 64 | |
| 65 | /* Set the default port */ |
| 66 | if (getservbyname("ftp", "tcp")) { |
| 67 | host->port = "ftp"; |
| 68 | } else { |
| 69 | host->port = "21"; |
| 70 | } |
| 71 | host->user = "anonymous"; |
| 72 | host->password = "busybox@"; |
| 73 | |
| 74 | return(host); |
| 75 | } |
| 76 | |
| 77 | static int ftpcmd(const char *s1, const char *s2, FILE *stream, char *buf) |
| 78 | { |
| 79 | if (verbose_flag) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 80 | bb_error_msg("cmd %s%s", s1, s2); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | if (s1) { |
| 84 | if (s2) { |
| 85 | fprintf(stream, "%s%s\n", s1, s2); |
| 86 | } else { |
| 87 | fprintf(stream, "%s\n", s1); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | do { |
| 92 | if (fgets(buf, 510, stream) == NULL) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 93 | bb_perror_msg_and_die("fgets()"); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 94 | } |
| 95 | } while (! isdigit(buf[0]) || buf[3] != ' '); |
| 96 | |
| 97 | return atoi(buf); |
| 98 | } |
| 99 | |
| 100 | static int xconnect_ftpdata(const char *target_host, const char *buf) |
| 101 | { |
| 102 | char *buf_ptr; |
| 103 | char data_port[6]; |
| 104 | unsigned short port_num; |
| 105 | |
| 106 | buf_ptr = strrchr(buf, ','); |
| 107 | *buf_ptr = '\0'; |
| 108 | port_num = atoi(buf_ptr + 1); |
| 109 | |
| 110 | buf_ptr = strrchr(buf, ','); |
| 111 | *buf_ptr = '\0'; |
| 112 | port_num += atoi(buf_ptr + 1) * 256; |
| 113 | |
| 114 | sprintf(data_port, "%d", port_num); |
| 115 | return(xconnect(target_host, data_port)); |
| 116 | } |
| 117 | |
| 118 | static FILE *ftp_login(ftp_host_info_t *server) |
| 119 | { |
| 120 | FILE *control_stream; |
| 121 | char buf[512]; |
| 122 | int control_fd; |
| 123 | |
| 124 | /* Connect to the command socket */ |
| 125 | control_fd = xconnect(server->host, server->port); |
| 126 | control_stream = fdopen(control_fd, "r+"); |
| 127 | if (control_stream == NULL) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 128 | bb_perror_msg_and_die("Couldnt open control stream"); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | if (ftpcmd(NULL, NULL, control_stream, buf) != 220) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 132 | bb_error_msg_and_die("%s", buf + 4); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | /* Login to the server */ |
| 136 | switch (ftpcmd("USER ", server->user, control_stream, buf)) { |
| 137 | case 230: |
| 138 | break; |
| 139 | case 331: |
| 140 | if (ftpcmd("PASS ", server->password, control_stream, buf) != 230) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 141 | bb_error_msg_and_die("PASS error: %s", buf + 4); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 142 | } |
| 143 | break; |
| 144 | default: |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 145 | bb_error_msg_and_die("USER error: %s", buf + 4); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | ftpcmd("TYPE I", NULL, control_stream, buf); |
| 149 | |
| 150 | return(control_stream); |
| 151 | } |
| 152 | |
| 153 | #ifdef CONFIG_FTPGET |
| 154 | static int ftp_recieve(FILE *control_stream, const char *host, const char *local_path, char *server_path) |
| 155 | { |
| 156 | char *filename; |
| 157 | char *local_file; |
| 158 | char buf[512]; |
| 159 | off_t filesize = 0; |
| 160 | int fd_data; |
| 161 | int fd_local; |
| 162 | off_t beg_range = 0; |
| 163 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 164 | filename = bb_get_last_path_component(server_path); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 165 | local_file = concat_path_file(local_path, filename); |
| 166 | |
| 167 | /* Connect to the data socket */ |
| 168 | if (ftpcmd("PASV", NULL, control_stream, buf) != 227) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 169 | bb_error_msg_and_die("PASV error: %s", buf + 4); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 170 | } |
| 171 | fd_data = xconnect_ftpdata(host, buf); |
| 172 | |
| 173 | if (ftpcmd("SIZE ", server_path, control_stream, buf) == 213) { |
| 174 | filesize = atol(buf + 4); |
| 175 | } |
| 176 | |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 177 | if (do_continue) { |
| 178 | struct stat sbuf; |
Glenn L McGrath | 1643f41 | 2002-12-18 02:47:40 +0000 | [diff] [blame] | 179 | if (lstat(local_file, &sbuf) < 0) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 180 | bb_perror_msg_and_die("fstat()"); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 181 | } |
| 182 | if (sbuf.st_size > 0) { |
| 183 | beg_range = sbuf.st_size; |
| 184 | } else { |
| 185 | do_continue = 0; |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | if (do_continue) { |
| 190 | sprintf(buf, "REST %ld", beg_range); |
| 191 | if (ftpcmd(buf, NULL, control_stream, buf) != 350) { |
| 192 | do_continue = 0; |
| 193 | } else { |
| 194 | filesize -= beg_range; |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | if (ftpcmd("RETR ", server_path, control_stream, buf) > 150) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 199 | bb_error_msg_and_die("RETR error: %s", buf + 4); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 200 | } |
| 201 | |
Glenn L McGrath | 1643f41 | 2002-12-18 02:47:40 +0000 | [diff] [blame] | 202 | /* only make a local file if we know that one exists on the remote server */ |
| 203 | if (do_continue) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 204 | fd_local = bb_xopen(local_file, O_APPEND | O_WRONLY); |
Glenn L McGrath | 1643f41 | 2002-12-18 02:47:40 +0000 | [diff] [blame] | 205 | } else { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 206 | 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] | 207 | } |
| 208 | |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 209 | /* Copy the file */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 210 | if (bb_copyfd(fd_data, fd_local, filesize) == -1) { |
Glenn L McGrath | 25fe94f | 2002-12-13 08:20:44 +0000 | [diff] [blame] | 211 | exit(EXIT_FAILURE); |
| 212 | } |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 213 | |
| 214 | /* close it all down */ |
| 215 | close(fd_data); |
| 216 | if (ftpcmd(NULL, NULL, control_stream, buf) != 226) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 217 | bb_error_msg_and_die("ftp error: %s", buf + 4); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 218 | } |
| 219 | ftpcmd("QUIT", NULL, control_stream, buf); |
| 220 | |
| 221 | return(EXIT_SUCCESS); |
| 222 | } |
| 223 | #endif |
| 224 | |
| 225 | #ifdef CONFIG_FTPPUT |
Glenn L McGrath | a67dffe | 2002-12-13 05:57:46 +0000 | [diff] [blame] | 226 | static int ftp_send(FILE *control_stream, const char *host, const char *server_path, char *local_path) |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 227 | { |
| 228 | struct stat sbuf; |
| 229 | char buf[512]; |
| 230 | int fd_data; |
| 231 | int fd_local; |
| 232 | int response; |
| 233 | |
| 234 | /* Connect to the data socket */ |
| 235 | if (ftpcmd("PASV", NULL, control_stream, buf) != 227) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 236 | bb_error_msg_and_die("PASV error: %s", buf + 4); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 237 | } |
| 238 | fd_data = xconnect_ftpdata(host, buf); |
| 239 | |
| 240 | if (ftpcmd("CWD ", server_path, control_stream, buf) != 250) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 241 | bb_error_msg_and_die("CWD error: %s", buf + 4); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | /* get the local file */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 245 | fd_local = bb_xopen(local_path, O_RDONLY); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 246 | fstat(fd_local, &sbuf); |
| 247 | |
| 248 | sprintf(buf, "ALLO %lu", sbuf.st_size); |
| 249 | response = ftpcmd(buf, NULL, control_stream, buf); |
| 250 | switch (response) { |
| 251 | case 200: |
| 252 | case 202: |
| 253 | break; |
| 254 | default: |
| 255 | close(fd_local); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 256 | bb_error_msg_and_die("ALLO error: %s", buf + 4); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 257 | break; |
| 258 | } |
| 259 | |
| 260 | response = ftpcmd("STOR ", local_path, control_stream, buf); |
| 261 | switch (response) { |
| 262 | case 125: |
| 263 | case 150: |
| 264 | break; |
| 265 | default: |
| 266 | close(fd_local); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 267 | bb_error_msg_and_die("STOR error: %s", buf + 4); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | /* transfer the file */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 271 | if (bb_copyfd(fd_local, fd_data, 0) == -1) { |
Glenn L McGrath | 25fe94f | 2002-12-13 08:20:44 +0000 | [diff] [blame] | 272 | exit(EXIT_FAILURE); |
| 273 | } |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 274 | |
| 275 | /* close it all down */ |
| 276 | close(fd_data); |
| 277 | if (ftpcmd(NULL, NULL, control_stream, buf) != 226) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 278 | bb_error_msg_and_die("error: %s", buf + 4); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 279 | } |
| 280 | ftpcmd("QUIT", NULL, control_stream, buf); |
| 281 | |
| 282 | return(EXIT_SUCCESS); |
| 283 | } |
| 284 | #endif |
| 285 | |
| 286 | int ftpgetput_main(int argc, char **argv) |
| 287 | { |
| 288 | /* content-length of the file */ |
| 289 | int option_index = -1; |
| 290 | int opt; |
| 291 | |
| 292 | /* socket to ftp server */ |
| 293 | FILE *control_stream; |
| 294 | |
| 295 | /* continue a prev transfer (-c) */ |
| 296 | ftp_host_info_t *server; |
| 297 | |
| 298 | int (*ftp_action)(FILE *, const char *, const char *, char *) = NULL; |
| 299 | |
| 300 | struct option long_options[] = { |
| 301 | {"username", 1, NULL, 'u'}, |
| 302 | {"password", 1, NULL, 'p'}, |
| 303 | {"port", 1, NULL, 'P'}, |
| 304 | {"continue", 1, NULL, 'c'}, |
| 305 | {"verbose", 0, NULL, 'v'}, |
| 306 | {0, 0, 0, 0} |
| 307 | }; |
| 308 | |
| 309 | #ifdef CONFIG_FTPPUT |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 310 | if (bb_applet_name[3] == 'p') { |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 311 | ftp_action = ftp_send; |
| 312 | } |
| 313 | #endif |
| 314 | #ifdef CONFIG_FTPGET |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 315 | if (bb_applet_name[3] == 'g') { |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 316 | ftp_action = ftp_recieve; |
| 317 | } |
| 318 | #endif |
| 319 | |
| 320 | /* Set default values */ |
| 321 | server = ftp_init(); |
| 322 | verbose_flag = 0; |
| 323 | |
| 324 | /* |
| 325 | * Decipher the command line |
| 326 | */ |
| 327 | while ((opt = getopt_long(argc, argv, "u:p:P:cv", long_options, &option_index)) != EOF) { |
| 328 | switch(opt) { |
| 329 | case 'c': |
| 330 | do_continue = 1; |
| 331 | break; |
| 332 | case 'u': |
| 333 | server->user = optarg; |
| 334 | break; |
| 335 | case 'p': |
| 336 | server->password = optarg; |
| 337 | break; |
| 338 | case 'P': |
| 339 | server->port = optarg; |
| 340 | break; |
| 341 | case 'v': |
| 342 | verbose_flag = 1; |
| 343 | break; |
| 344 | default: |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 345 | bb_show_usage(); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 346 | } |
| 347 | } |
| 348 | |
| 349 | /* |
| 350 | * Process the non-option command line arguments |
| 351 | */ |
| 352 | if (argc - optind != 3) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 353 | bb_show_usage(); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | /* Connect/Setup/Configure the FTP session */ |
| 357 | server->host = argv[optind]; |
| 358 | control_stream = ftp_login(server); |
| 359 | |
| 360 | return(ftp_action(control_stream, argv[optind], argv[optind + 1], argv[optind + 2])); |
| 361 | } |
| 362 | |
| 363 | /* |
| 364 | Local Variables: |
| 365 | c-file-style: "linux" |
| 366 | c-basic-offset: 4 |
| 367 | tab-width: 4 |
| 368 | End: |
| 369 | */ |