[821] | 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@iinet.net.au>
|
---|
| 9 | *
|
---|
| 10 | * Based on wget.c by Chip Rosenthal Covad Communications
|
---|
| 11 | * <chip@laserlink.net>
|
---|
| 12 | *
|
---|
| 13 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
|
---|
| 14 | */
|
---|
| 15 |
|
---|
| 16 | #include <getopt.h>
|
---|
[1765] | 17 | #include "libbb.h"
|
---|
[821] | 18 |
|
---|
| 19 | typedef struct ftp_host_info_s {
|
---|
[1765] | 20 | const char *user;
|
---|
| 21 | const char *password;
|
---|
| 22 | struct len_and_sockaddr *lsa;
|
---|
[821] | 23 | } ftp_host_info_t;
|
---|
| 24 |
|
---|
[1765] | 25 | static smallint verbose_flag;
|
---|
| 26 | static smallint do_continue;
|
---|
[821] | 27 |
|
---|
[1765] | 28 | static void ftp_die(const char *msg, const char *remote) ATTRIBUTE_NORETURN;
|
---|
| 29 | static void ftp_die(const char *msg, const char *remote)
|
---|
| 30 | {
|
---|
| 31 | /* Guard against garbage from remote server */
|
---|
| 32 | const char *cp = remote;
|
---|
| 33 | while (*cp >= ' ' && *cp < '\x7f') cp++;
|
---|
| 34 | bb_error_msg_and_die("unexpected server response%s%s: %.*s",
|
---|
| 35 | msg ? " to " : "", msg ? msg : "",
|
---|
| 36 | (int)(cp - remote), remote);
|
---|
| 37 | }
|
---|
| 38 |
|
---|
| 39 |
|
---|
[821] | 40 | static int ftpcmd(const char *s1, const char *s2, FILE *stream, char *buf)
|
---|
| 41 | {
|
---|
[1765] | 42 | unsigned n;
|
---|
[821] | 43 | if (verbose_flag) {
|
---|
[1765] | 44 | bb_error_msg("cmd %s %s", s1, s2);
|
---|
[821] | 45 | }
|
---|
| 46 |
|
---|
| 47 | if (s1) {
|
---|
| 48 | if (s2) {
|
---|
[1765] | 49 | fprintf(stream, "%s %s\r\n", s1, s2);
|
---|
[821] | 50 | } else {
|
---|
| 51 | fprintf(stream, "%s\r\n", s1);
|
---|
| 52 | }
|
---|
| 53 | }
|
---|
| 54 | do {
|
---|
| 55 | char *buf_ptr;
|
---|
| 56 |
|
---|
| 57 | if (fgets(buf, 510, stream) == NULL) {
|
---|
[1765] | 58 | bb_perror_msg_and_die("fgets");
|
---|
[821] | 59 | }
|
---|
| 60 | buf_ptr = strstr(buf, "\r\n");
|
---|
| 61 | if (buf_ptr) {
|
---|
| 62 | *buf_ptr = '\0';
|
---|
| 63 | }
|
---|
[1765] | 64 | } while (!isdigit(buf[0]) || buf[3] != ' ');
|
---|
[821] | 65 |
|
---|
[1765] | 66 | buf[3] = '\0';
|
---|
| 67 | n = xatou(buf);
|
---|
| 68 | buf[3] = ' ';
|
---|
| 69 | return n;
|
---|
[821] | 70 | }
|
---|
| 71 |
|
---|
[1765] | 72 | static int xconnect_ftpdata(ftp_host_info_t *server, char *buf)
|
---|
[821] | 73 | {
|
---|
| 74 | char *buf_ptr;
|
---|
| 75 | unsigned short port_num;
|
---|
| 76 |
|
---|
[1765] | 77 | /* Response is "NNN garbageN1,N2,N3,N4,P1,P2[)garbage]
|
---|
| 78 | * Server's IP is N1.N2.N3.N4 (we ignore it)
|
---|
| 79 | * Server's port for data connection is P1*256+P2 */
|
---|
| 80 | buf_ptr = strrchr(buf, ')');
|
---|
| 81 | if (buf_ptr) *buf_ptr = '\0';
|
---|
| 82 |
|
---|
[821] | 83 | buf_ptr = strrchr(buf, ',');
|
---|
| 84 | *buf_ptr = '\0';
|
---|
[1765] | 85 | port_num = xatoul_range(buf_ptr + 1, 0, 255);
|
---|
[821] | 86 |
|
---|
| 87 | buf_ptr = strrchr(buf, ',');
|
---|
| 88 | *buf_ptr = '\0';
|
---|
[1765] | 89 | port_num += xatoul_range(buf_ptr + 1, 0, 255) * 256;
|
---|
[821] | 90 |
|
---|
[1765] | 91 | set_nport(server->lsa, htons(port_num));
|
---|
| 92 | return xconnect_stream(server->lsa);
|
---|
[821] | 93 | }
|
---|
| 94 |
|
---|
| 95 | static FILE *ftp_login(ftp_host_info_t *server)
|
---|
| 96 | {
|
---|
| 97 | FILE *control_stream;
|
---|
| 98 | char buf[512];
|
---|
| 99 |
|
---|
| 100 | /* Connect to the command socket */
|
---|
[1765] | 101 | control_stream = fdopen(xconnect_stream(server->lsa), "r+");
|
---|
[821] | 102 | if (control_stream == NULL) {
|
---|
[1765] | 103 | /* fdopen failed - extremely unlikely */
|
---|
| 104 | bb_perror_nomsg_and_die();
|
---|
[821] | 105 | }
|
---|
| 106 |
|
---|
| 107 | if (ftpcmd(NULL, NULL, control_stream, buf) != 220) {
|
---|
[1765] | 108 | ftp_die(NULL, buf);
|
---|
[821] | 109 | }
|
---|
| 110 |
|
---|
| 111 | /* Login to the server */
|
---|
[1765] | 112 | switch (ftpcmd("USER", server->user, control_stream, buf)) {
|
---|
[821] | 113 | case 230:
|
---|
| 114 | break;
|
---|
| 115 | case 331:
|
---|
[1765] | 116 | if (ftpcmd("PASS", server->password, control_stream, buf) != 230) {
|
---|
| 117 | ftp_die("PASS", buf);
|
---|
[821] | 118 | }
|
---|
| 119 | break;
|
---|
| 120 | default:
|
---|
[1765] | 121 | ftp_die("USER", buf);
|
---|
[821] | 122 | }
|
---|
| 123 |
|
---|
| 124 | ftpcmd("TYPE I", NULL, control_stream, buf);
|
---|
| 125 |
|
---|
[1765] | 126 | return control_stream;
|
---|
[821] | 127 | }
|
---|
| 128 |
|
---|
| 129 | #if !ENABLE_FTPGET
|
---|
[1765] | 130 | int ftp_receive(ftp_host_info_t *server, FILE *control_stream,
|
---|
| 131 | const char *local_path, char *server_path);
|
---|
[821] | 132 | #else
|
---|
[1765] | 133 | static
|
---|
| 134 | int ftp_receive(ftp_host_info_t *server, FILE *control_stream,
|
---|
[821] | 135 | const char *local_path, char *server_path)
|
---|
| 136 | {
|
---|
| 137 | char buf[512];
|
---|
[1765] | 138 | /* I think 'filesize' usage here is bogus. Let's see... */
|
---|
| 139 | //off_t filesize = -1;
|
---|
| 140 | #define filesize ((off_t)-1)
|
---|
[821] | 141 | int fd_data;
|
---|
| 142 | int fd_local = -1;
|
---|
| 143 | off_t beg_range = 0;
|
---|
| 144 |
|
---|
| 145 | /* Connect to the data socket */
|
---|
| 146 | if (ftpcmd("PASV", NULL, control_stream, buf) != 227) {
|
---|
[1765] | 147 | ftp_die("PASV", buf);
|
---|
[821] | 148 | }
|
---|
| 149 | fd_data = xconnect_ftpdata(server, buf);
|
---|
| 150 |
|
---|
[1765] | 151 | if (ftpcmd("SIZE", server_path, control_stream, buf) == 213) {
|
---|
| 152 | //filesize = BB_STRTOOFF(buf + 4, NULL, 10);
|
---|
| 153 | //if (errno || filesize < 0)
|
---|
| 154 | // ftp_die("SIZE", buf);
|
---|
[821] | 155 | } else {
|
---|
| 156 | do_continue = 0;
|
---|
| 157 | }
|
---|
| 158 |
|
---|
[1765] | 159 | if (LONE_DASH(local_path)) {
|
---|
[821] | 160 | fd_local = STDOUT_FILENO;
|
---|
| 161 | do_continue = 0;
|
---|
| 162 | }
|
---|
| 163 |
|
---|
| 164 | if (do_continue) {
|
---|
| 165 | struct stat sbuf;
|
---|
| 166 | if (lstat(local_path, &sbuf) < 0) {
|
---|
[1765] | 167 | bb_perror_msg_and_die("lstat");
|
---|
[821] | 168 | }
|
---|
| 169 | if (sbuf.st_size > 0) {
|
---|
| 170 | beg_range = sbuf.st_size;
|
---|
| 171 | } else {
|
---|
| 172 | do_continue = 0;
|
---|
| 173 | }
|
---|
| 174 | }
|
---|
| 175 |
|
---|
| 176 | if (do_continue) {
|
---|
[1765] | 177 | sprintf(buf, "REST %"OFF_FMT"d", beg_range);
|
---|
[821] | 178 | if (ftpcmd(buf, NULL, control_stream, buf) != 350) {
|
---|
| 179 | do_continue = 0;
|
---|
| 180 | } else {
|
---|
[1765] | 181 | //if (filesize != -1)
|
---|
| 182 | // filesize -= beg_range;
|
---|
[821] | 183 | }
|
---|
| 184 | }
|
---|
| 185 |
|
---|
[1765] | 186 | if (ftpcmd("RETR", server_path, control_stream, buf) > 150) {
|
---|
| 187 | ftp_die("RETR", buf);
|
---|
[821] | 188 | }
|
---|
| 189 |
|
---|
| 190 | /* only make a local file if we know that one exists on the remote server */
|
---|
| 191 | if (fd_local == -1) {
|
---|
| 192 | if (do_continue) {
|
---|
[1765] | 193 | fd_local = xopen(local_path, O_APPEND | O_WRONLY);
|
---|
[821] | 194 | } else {
|
---|
[1765] | 195 | fd_local = xopen(local_path, O_CREAT | O_TRUNC | O_WRONLY);
|
---|
[821] | 196 | }
|
---|
| 197 | }
|
---|
| 198 |
|
---|
| 199 | /* Copy the file */
|
---|
| 200 | if (filesize != -1) {
|
---|
[1765] | 201 | if (bb_copyfd_size(fd_data, fd_local, filesize) == -1)
|
---|
| 202 | return EXIT_FAILURE;
|
---|
[821] | 203 | } else {
|
---|
[1765] | 204 | if (bb_copyfd_eof(fd_data, fd_local) == -1)
|
---|
| 205 | return EXIT_FAILURE;
|
---|
[821] | 206 | }
|
---|
| 207 |
|
---|
| 208 | /* close it all down */
|
---|
| 209 | close(fd_data);
|
---|
| 210 | if (ftpcmd(NULL, NULL, control_stream, buf) != 226) {
|
---|
[1765] | 211 | ftp_die(NULL, buf);
|
---|
[821] | 212 | }
|
---|
| 213 | ftpcmd("QUIT", NULL, control_stream, buf);
|
---|
| 214 |
|
---|
[1765] | 215 | return EXIT_SUCCESS;
|
---|
[821] | 216 | }
|
---|
| 217 | #endif
|
---|
| 218 |
|
---|
| 219 | #if !ENABLE_FTPPUT
|
---|
[1765] | 220 | int ftp_send(ftp_host_info_t *server, FILE *control_stream,
|
---|
| 221 | const char *server_path, char *local_path);
|
---|
[821] | 222 | #else
|
---|
[1765] | 223 | static
|
---|
| 224 | int ftp_send(ftp_host_info_t *server, FILE *control_stream,
|
---|
[821] | 225 | const char *server_path, char *local_path)
|
---|
| 226 | {
|
---|
| 227 | struct stat sbuf;
|
---|
| 228 | char buf[512];
|
---|
| 229 | int fd_data;
|
---|
| 230 | int fd_local;
|
---|
| 231 | int response;
|
---|
| 232 |
|
---|
| 233 | /* Connect to the data socket */
|
---|
| 234 | if (ftpcmd("PASV", NULL, control_stream, buf) != 227) {
|
---|
[1765] | 235 | ftp_die("PASV", buf);
|
---|
[821] | 236 | }
|
---|
| 237 | fd_data = xconnect_ftpdata(server, buf);
|
---|
| 238 |
|
---|
| 239 | /* get the local file */
|
---|
[1765] | 240 | fd_local = STDIN_FILENO;
|
---|
| 241 | if (NOT_LONE_DASH(local_path)) {
|
---|
| 242 | fd_local = xopen(local_path, O_RDONLY);
|
---|
[821] | 243 | fstat(fd_local, &sbuf);
|
---|
| 244 |
|
---|
[1765] | 245 | sprintf(buf, "ALLO %"OFF_FMT"u", sbuf.st_size);
|
---|
[821] | 246 | response = ftpcmd(buf, NULL, control_stream, buf);
|
---|
| 247 | switch (response) {
|
---|
| 248 | case 200:
|
---|
| 249 | case 202:
|
---|
| 250 | break;
|
---|
| 251 | default:
|
---|
| 252 | close(fd_local);
|
---|
[1765] | 253 | ftp_die("ALLO", buf);
|
---|
[821] | 254 | break;
|
---|
| 255 | }
|
---|
| 256 | }
|
---|
[1765] | 257 | response = ftpcmd("STOR", server_path, control_stream, buf);
|
---|
[821] | 258 | switch (response) {
|
---|
| 259 | case 125:
|
---|
| 260 | case 150:
|
---|
| 261 | break;
|
---|
| 262 | default:
|
---|
| 263 | close(fd_local);
|
---|
[1765] | 264 | ftp_die("STOR", buf);
|
---|
[821] | 265 | }
|
---|
| 266 |
|
---|
| 267 | /* transfer the file */
|
---|
| 268 | if (bb_copyfd_eof(fd_local, fd_data) == -1) {
|
---|
| 269 | exit(EXIT_FAILURE);
|
---|
| 270 | }
|
---|
| 271 |
|
---|
| 272 | /* close it all down */
|
---|
| 273 | close(fd_data);
|
---|
| 274 | if (ftpcmd(NULL, NULL, control_stream, buf) != 226) {
|
---|
[1765] | 275 | ftp_die("close", buf);
|
---|
[821] | 276 | }
|
---|
| 277 | ftpcmd("QUIT", NULL, control_stream, buf);
|
---|
| 278 |
|
---|
[1765] | 279 | return EXIT_SUCCESS;
|
---|
[821] | 280 | }
|
---|
| 281 | #endif
|
---|
| 282 |
|
---|
| 283 | #define FTPGETPUT_OPT_CONTINUE 1
|
---|
| 284 | #define FTPGETPUT_OPT_VERBOSE 2
|
---|
| 285 | #define FTPGETPUT_OPT_USER 4
|
---|
| 286 | #define FTPGETPUT_OPT_PASSWORD 8
|
---|
| 287 | #define FTPGETPUT_OPT_PORT 16
|
---|
| 288 |
|
---|
| 289 | #if ENABLE_FEATURE_FTPGETPUT_LONG_OPTIONS
|
---|
[1765] | 290 | static const char ftpgetput_longopts[] ALIGN1 =
|
---|
| 291 | "continue\0" Required_argument "c"
|
---|
| 292 | "verbose\0" No_argument "v"
|
---|
| 293 | "username\0" Required_argument "u"
|
---|
| 294 | "password\0" Required_argument "p"
|
---|
| 295 | "port\0" Required_argument "P"
|
---|
| 296 | ;
|
---|
[821] | 297 | #endif
|
---|
| 298 |
|
---|
[1765] | 299 | int ftpgetput_main(int argc, char **argv);
|
---|
[821] | 300 | int ftpgetput_main(int argc, char **argv)
|
---|
| 301 | {
|
---|
| 302 | /* content-length of the file */
|
---|
[1765] | 303 | unsigned opt;
|
---|
| 304 | const char *port = "ftp";
|
---|
[821] | 305 | /* socket to ftp server */
|
---|
| 306 | FILE *control_stream;
|
---|
[1765] | 307 | /* continue previous transfer (-c) */
|
---|
[821] | 308 | ftp_host_info_t *server;
|
---|
| 309 |
|
---|
[1765] | 310 | #if ENABLE_FTPPUT && !ENABLE_FTPGET
|
---|
| 311 | # define ftp_action ftp_send
|
---|
| 312 | #elif ENABLE_FTPGET && !ENABLE_FTPPUT
|
---|
| 313 | # define ftp_action ftp_receive
|
---|
| 314 | #else
|
---|
| 315 | int (*ftp_action)(ftp_host_info_t *, FILE *, const char *, char *) = ftp_send;
|
---|
[821] | 316 | /* Check to see if the command is ftpget or ftput */
|
---|
[1765] | 317 | if (applet_name[3] == 'g') {
|
---|
[821] | 318 | ftp_action = ftp_receive;
|
---|
| 319 | }
|
---|
[1765] | 320 | #endif
|
---|
[821] | 321 |
|
---|
| 322 | /* Set default values */
|
---|
[1765] | 323 | server = xmalloc(sizeof(*server));
|
---|
[821] | 324 | server->user = "anonymous";
|
---|
| 325 | server->password = "busybox@";
|
---|
| 326 |
|
---|
| 327 | /*
|
---|
| 328 | * Decipher the command line
|
---|
| 329 | */
|
---|
[1765] | 330 | #if ENABLE_FEATURE_FTPGETPUT_LONG_OPTIONS
|
---|
| 331 | applet_long_options = ftpgetput_longopts;
|
---|
| 332 | #endif
|
---|
| 333 | opt_complementary = "=3"; /* must have 3 params */
|
---|
| 334 | opt = getopt32(argv, "cvu:p:P:", &server->user, &server->password, &port);
|
---|
| 335 | argv += optind;
|
---|
[821] | 336 |
|
---|
| 337 | /* Process the non-option command line arguments */
|
---|
| 338 | if (opt & FTPGETPUT_OPT_CONTINUE) {
|
---|
| 339 | do_continue = 1;
|
---|
| 340 | }
|
---|
| 341 | if (opt & FTPGETPUT_OPT_VERBOSE) {
|
---|
| 342 | verbose_flag = 1;
|
---|
| 343 | }
|
---|
| 344 |
|
---|
| 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... */
|
---|
[1765] | 348 | server->lsa = xhost2sockaddr(argv[0], bb_lookup_port(port, "tcp", 21));
|
---|
[821] | 349 | if (verbose_flag) {
|
---|
[1765] | 350 | printf("Connecting to %s (%s)\n", argv[0],
|
---|
| 351 | xmalloc_sockaddr2dotted(&server->lsa->sa));
|
---|
[821] | 352 | }
|
---|
| 353 |
|
---|
| 354 | /* Connect/Setup/Configure the FTP session */
|
---|
| 355 | control_stream = ftp_login(server);
|
---|
| 356 |
|
---|
[1765] | 357 | return ftp_action(server, control_stream, argv[1], argv[2]);
|
---|
[821] | 358 | }
|
---|