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> |
Denis Vlasenko | 0beaff8 | 2007-09-21 13:16:32 +0000 | [diff] [blame] | 8 | * Copyright (C) 2002 Glenn McGrath |
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 | |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 16 | #include <getopt.h> |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 17 | #include "libbb.h" |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 18 | |
| 19 | typedef struct ftp_host_info_s { |
Denis Vlasenko | b6aae0f | 2007-01-29 22:51:25 +0000 | [diff] [blame] | 20 | const char *user; |
| 21 | const char *password; |
Denis Vlasenko | 8e9ccba | 2007-01-11 16:50:23 +0000 | [diff] [blame] | 22 | struct len_and_sockaddr *lsa; |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 23 | } ftp_host_info_t; |
| 24 | |
Denis Vlasenko | 3821fb1 | 2007-01-11 16:51:21 +0000 | [diff] [blame] | 25 | static smallint verbose_flag; |
| 26 | static smallint do_continue; |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 27 | |
Denis Vlasenko | 562dc24 | 2007-01-03 21:55:50 +0000 | [diff] [blame] | 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 | |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 40 | static int ftpcmd(const char *s1, const char *s2, FILE *stream, char *buf) |
| 41 | { |
Denis Vlasenko | 562dc24 | 2007-01-03 21:55:50 +0000 | [diff] [blame] | 42 | unsigned n; |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 43 | if (verbose_flag) { |
Denis Vlasenko | 3821fb1 | 2007-01-11 16:51:21 +0000 | [diff] [blame] | 44 | bb_error_msg("cmd %s %s", s1, s2); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | if (s1) { |
| 48 | if (s2) { |
Denis Vlasenko | 562dc24 | 2007-01-03 21:55:50 +0000 | [diff] [blame] | 49 | fprintf(stream, "%s %s\r\n", s1, s2); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 50 | } else { |
Paul Fox | 146e83a | 2005-07-19 21:26:57 +0000 | [diff] [blame] | 51 | fprintf(stream, "%s\r\n", s1); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 52 | } |
| 53 | } |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 54 | do { |
Glenn L McGrath | 5ec5828 | 2004-05-04 10:43:34 +0000 | [diff] [blame] | 55 | char *buf_ptr; |
| 56 | |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 57 | if (fgets(buf, 510, stream) == NULL) { |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 58 | bb_perror_msg_and_die("fgets"); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 59 | } |
Glenn L McGrath | 5ec5828 | 2004-05-04 10:43:34 +0000 | [diff] [blame] | 60 | buf_ptr = strstr(buf, "\r\n"); |
| 61 | if (buf_ptr) { |
| 62 | *buf_ptr = '\0'; |
| 63 | } |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 64 | } while (!isdigit(buf[0]) || buf[3] != ' '); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 65 | |
Denis Vlasenko | 562dc24 | 2007-01-03 21:55:50 +0000 | [diff] [blame] | 66 | buf[3] = '\0'; |
| 67 | n = xatou(buf); |
| 68 | buf[3] = ' '; |
| 69 | return n; |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Denis Vlasenko | 562dc24 | 2007-01-03 21:55:50 +0000 | [diff] [blame] | 72 | static int xconnect_ftpdata(ftp_host_info_t *server, char *buf) |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 73 | { |
| 74 | char *buf_ptr; |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 75 | unsigned short port_num; |
| 76 | |
Denis Vlasenko | 562dc24 | 2007-01-03 21:55:50 +0000 | [diff] [blame] | 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 | |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 83 | buf_ptr = strrchr(buf, ','); |
| 84 | *buf_ptr = '\0'; |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 85 | port_num = xatoul_range(buf_ptr + 1, 0, 255); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 86 | |
| 87 | buf_ptr = strrchr(buf, ','); |
| 88 | *buf_ptr = '\0'; |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 89 | port_num += xatoul_range(buf_ptr + 1, 0, 255) * 256; |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 90 | |
Denis Vlasenko | 5d68724 | 2007-01-12 20:59:31 +0000 | [diff] [blame] | 91 | set_nport(server->lsa, htons(port_num)); |
Denis Vlasenko | 8e9ccba | 2007-01-11 16:50:23 +0000 | [diff] [blame] | 92 | return xconnect_stream(server->lsa); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | static FILE *ftp_login(ftp_host_info_t *server) |
| 96 | { |
| 97 | FILE *control_stream; |
| 98 | char buf[512]; |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 99 | |
| 100 | /* Connect to the command socket */ |
Denis Vlasenko | 8e9ccba | 2007-01-11 16:50:23 +0000 | [diff] [blame] | 101 | control_stream = fdopen(xconnect_stream(server->lsa), "r+"); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 102 | if (control_stream == NULL) { |
Denis Vlasenko | 8e9ccba | 2007-01-11 16:50:23 +0000 | [diff] [blame] | 103 | /* fdopen failed - extremely unlikely */ |
Denis Vlasenko | 562dc24 | 2007-01-03 21:55:50 +0000 | [diff] [blame] | 104 | bb_perror_nomsg_and_die(); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | if (ftpcmd(NULL, NULL, control_stream, buf) != 220) { |
Denis Vlasenko | 562dc24 | 2007-01-03 21:55:50 +0000 | [diff] [blame] | 108 | ftp_die(NULL, buf); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | /* Login to the server */ |
Denis Vlasenko | 562dc24 | 2007-01-03 21:55:50 +0000 | [diff] [blame] | 112 | switch (ftpcmd("USER", server->user, control_stream, buf)) { |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 113 | case 230: |
| 114 | break; |
| 115 | case 331: |
Denis Vlasenko | 562dc24 | 2007-01-03 21:55:50 +0000 | [diff] [blame] | 116 | if (ftpcmd("PASS", server->password, control_stream, buf) != 230) { |
| 117 | ftp_die("PASS", buf); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 118 | } |
| 119 | break; |
| 120 | default: |
Denis Vlasenko | 562dc24 | 2007-01-03 21:55:50 +0000 | [diff] [blame] | 121 | ftp_die("USER", buf); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | ftpcmd("TYPE I", NULL, control_stream, buf); |
| 125 | |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 126 | return control_stream; |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 127 | } |
| 128 | |
Rob Landley | b1c3fbc | 2006-05-04 19:52:28 +0000 | [diff] [blame] | 129 | #if !ENABLE_FTPGET |
Denis Vlasenko | 7039a66 | 2006-10-08 17:54:47 +0000 | [diff] [blame] | 130 | int ftp_receive(ftp_host_info_t *server, FILE *control_stream, |
| 131 | const char *local_path, char *server_path); |
Rob Landley | b1c3fbc | 2006-05-04 19:52:28 +0000 | [diff] [blame] | 132 | #else |
Denis Vlasenko | 7039a66 | 2006-10-08 17:54:47 +0000 | [diff] [blame] | 133 | static |
| 134 | int ftp_receive(ftp_host_info_t *server, FILE *control_stream, |
Eric Andersen | 04d055f | 2003-11-03 21:20:18 +0000 | [diff] [blame] | 135 | const char *local_path, char *server_path) |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 136 | { |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 137 | char buf[512]; |
Denis Vlasenko | 714701c | 2006-12-22 00:21:07 +0000 | [diff] [blame] | 138 | /* I think 'filesize' usage here is bogus. Let's see... */ |
| 139 | //off_t filesize = -1; |
| 140 | #define filesize ((off_t)-1) |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 141 | int fd_data; |
Glenn L McGrath | 236e93d | 2003-12-20 05:43:34 +0000 | [diff] [blame] | 142 | int fd_local = -1; |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 143 | off_t beg_range = 0; |
| 144 | |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 145 | /* Connect to the data socket */ |
| 146 | if (ftpcmd("PASV", NULL, control_stream, buf) != 227) { |
Denis Vlasenko | 562dc24 | 2007-01-03 21:55:50 +0000 | [diff] [blame] | 147 | ftp_die("PASV", buf); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 148 | } |
Eric Andersen | 04d055f | 2003-11-03 21:20:18 +0000 | [diff] [blame] | 149 | fd_data = xconnect_ftpdata(server, buf); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 150 | |
Denis Vlasenko | 562dc24 | 2007-01-03 21:55:50 +0000 | [diff] [blame] | 151 | if (ftpcmd("SIZE", server_path, control_stream, buf) == 213) { |
Denis Vlasenko | 714701c | 2006-12-22 00:21:07 +0000 | [diff] [blame] | 152 | //filesize = BB_STRTOOFF(buf + 4, NULL, 10); |
| 153 | //if (errno || filesize < 0) |
Denis Vlasenko | 562dc24 | 2007-01-03 21:55:50 +0000 | [diff] [blame] | 154 | // ftp_die("SIZE", buf); |
Rob Landley | bc059bc | 2006-01-10 06:36:00 +0000 | [diff] [blame] | 155 | } else { |
Rob Landley | bc059bc | 2006-01-10 06:36:00 +0000 | [diff] [blame] | 156 | do_continue = 0; |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 157 | } |
| 158 | |
Denis Vlasenko | 9f73944 | 2006-12-16 23:49:13 +0000 | [diff] [blame] | 159 | if (LONE_DASH(local_path)) { |
Eric Andersen | 70060d2 | 2004-03-27 10:02:48 +0000 | [diff] [blame] | 160 | fd_local = STDOUT_FILENO; |
Glenn L McGrath | 236e93d | 2003-12-20 05:43:34 +0000 | [diff] [blame] | 161 | do_continue = 0; |
| 162 | } |
| 163 | |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 164 | if (do_continue) { |
| 165 | struct stat sbuf; |
Glenn L McGrath | 236e93d | 2003-12-20 05:43:34 +0000 | [diff] [blame] | 166 | if (lstat(local_path, &sbuf) < 0) { |
Denis Vlasenko | 7039a66 | 2006-10-08 17:54:47 +0000 | [diff] [blame] | 167 | bb_perror_msg_and_die("lstat"); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 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) { |
Denis Vlasenko | cf30cc8 | 2006-11-24 14:53:18 +0000 | [diff] [blame] | 177 | sprintf(buf, "REST %"OFF_FMT"d", beg_range); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 178 | if (ftpcmd(buf, NULL, control_stream, buf) != 350) { |
| 179 | do_continue = 0; |
| 180 | } else { |
Denis Vlasenko | 714701c | 2006-12-22 00:21:07 +0000 | [diff] [blame] | 181 | //if (filesize != -1) |
| 182 | // filesize -= beg_range; |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 183 | } |
| 184 | } |
| 185 | |
Denis Vlasenko | 562dc24 | 2007-01-03 21:55:50 +0000 | [diff] [blame] | 186 | if (ftpcmd("RETR", server_path, control_stream, buf) > 150) { |
| 187 | ftp_die("RETR", buf); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 188 | } |
| 189 | |
Glenn L McGrath | 1643f41 | 2002-12-18 02:47:40 +0000 | [diff] [blame] | 190 | /* 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] | 191 | if (fd_local == -1) { |
| 192 | if (do_continue) { |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 193 | fd_local = xopen(local_path, O_APPEND | O_WRONLY); |
Glenn L McGrath | 236e93d | 2003-12-20 05:43:34 +0000 | [diff] [blame] | 194 | } else { |
Denis Vlasenko | cf749bc | 2006-11-26 15:45:17 +0000 | [diff] [blame] | 195 | fd_local = xopen(local_path, O_CREAT | O_TRUNC | O_WRONLY); |
Glenn L McGrath | 236e93d | 2003-12-20 05:43:34 +0000 | [diff] [blame] | 196 | } |
Glenn L McGrath | 1643f41 | 2002-12-18 02:47:40 +0000 | [diff] [blame] | 197 | } |
| 198 | |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 199 | /* Copy the file */ |
Rob Landley | bc059bc | 2006-01-10 06:36:00 +0000 | [diff] [blame] | 200 | if (filesize != -1) { |
Denis Vlasenko | 714701c | 2006-12-22 00:21:07 +0000 | [diff] [blame] | 201 | if (bb_copyfd_size(fd_data, fd_local, filesize) == -1) |
| 202 | return EXIT_FAILURE; |
Rob Landley | bc059bc | 2006-01-10 06:36:00 +0000 | [diff] [blame] | 203 | } else { |
Denis Vlasenko | 714701c | 2006-12-22 00:21:07 +0000 | [diff] [blame] | 204 | if (bb_copyfd_eof(fd_data, fd_local) == -1) |
| 205 | return EXIT_FAILURE; |
Glenn L McGrath | 25fe94f | 2002-12-13 08:20:44 +0000 | [diff] [blame] | 206 | } |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 207 | |
| 208 | /* close it all down */ |
| 209 | close(fd_data); |
| 210 | if (ftpcmd(NULL, NULL, control_stream, buf) != 226) { |
Denis Vlasenko | 562dc24 | 2007-01-03 21:55:50 +0000 | [diff] [blame] | 211 | ftp_die(NULL, buf); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 212 | } |
| 213 | ftpcmd("QUIT", NULL, control_stream, buf); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 214 | |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 215 | return EXIT_SUCCESS; |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 216 | } |
| 217 | #endif |
| 218 | |
Rob Landley | b1c3fbc | 2006-05-04 19:52:28 +0000 | [diff] [blame] | 219 | #if !ENABLE_FTPPUT |
Denis Vlasenko | 7039a66 | 2006-10-08 17:54:47 +0000 | [diff] [blame] | 220 | int ftp_send(ftp_host_info_t *server, FILE *control_stream, |
| 221 | const char *server_path, char *local_path); |
Rob Landley | b1c3fbc | 2006-05-04 19:52:28 +0000 | [diff] [blame] | 222 | #else |
Denis Vlasenko | 7039a66 | 2006-10-08 17:54:47 +0000 | [diff] [blame] | 223 | static |
| 224 | int ftp_send(ftp_host_info_t *server, FILE *control_stream, |
Eric Andersen | 04d055f | 2003-11-03 21:20:18 +0000 | [diff] [blame] | 225 | const char *server_path, char *local_path) |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 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) { |
Denis Vlasenko | 562dc24 | 2007-01-03 21:55:50 +0000 | [diff] [blame] | 235 | ftp_die("PASV", buf); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 236 | } |
Eric Andersen | 04d055f | 2003-11-03 21:20:18 +0000 | [diff] [blame] | 237 | fd_data = xconnect_ftpdata(server, buf); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 238 | |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 239 | /* get the local file */ |
Denis Vlasenko | 9f73944 | 2006-12-16 23:49:13 +0000 | [diff] [blame] | 240 | fd_local = STDIN_FILENO; |
| 241 | if (NOT_LONE_DASH(local_path)) { |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 242 | fd_local = xopen(local_path, O_RDONLY); |
Glenn L McGrath | 236e93d | 2003-12-20 05:43:34 +0000 | [diff] [blame] | 243 | fstat(fd_local, &sbuf); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 244 | |
Denis Vlasenko | 562dc24 | 2007-01-03 21:55:50 +0000 | [diff] [blame] | 245 | sprintf(buf, "ALLO %"OFF_FMT"u", sbuf.st_size); |
Glenn L McGrath | 236e93d | 2003-12-20 05:43:34 +0000 | [diff] [blame] | 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); |
Denis Vlasenko | 562dc24 | 2007-01-03 21:55:50 +0000 | [diff] [blame] | 253 | ftp_die("ALLO", buf); |
Glenn L McGrath | 236e93d | 2003-12-20 05:43:34 +0000 | [diff] [blame] | 254 | break; |
| 255 | } |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 256 | } |
Denis Vlasenko | 562dc24 | 2007-01-03 21:55:50 +0000 | [diff] [blame] | 257 | response = ftpcmd("STOR", server_path, control_stream, buf); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 258 | switch (response) { |
| 259 | case 125: |
| 260 | case 150: |
| 261 | break; |
| 262 | default: |
| 263 | close(fd_local); |
Denis Vlasenko | 562dc24 | 2007-01-03 21:55:50 +0000 | [diff] [blame] | 264 | ftp_die("STOR", buf); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | /* transfer the file */ |
Glenn L McGrath | 7ffe133 | 2003-11-21 22:24:57 +0000 | [diff] [blame] | 268 | if (bb_copyfd_eof(fd_local, fd_data) == -1) { |
Glenn L McGrath | 25fe94f | 2002-12-13 08:20:44 +0000 | [diff] [blame] | 269 | exit(EXIT_FAILURE); |
| 270 | } |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 271 | |
| 272 | /* close it all down */ |
| 273 | close(fd_data); |
| 274 | if (ftpcmd(NULL, NULL, control_stream, buf) != 226) { |
Denis Vlasenko | 562dc24 | 2007-01-03 21:55:50 +0000 | [diff] [blame] | 275 | ftp_die("close", buf); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 276 | } |
| 277 | ftpcmd("QUIT", NULL, control_stream, buf); |
| 278 | |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 279 | return EXIT_SUCCESS; |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 280 | } |
| 281 | #endif |
| 282 | |
Glenn L McGrath | b51eb26 | 2003-12-19 10:37:52 +0000 | [diff] [blame] | 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 | |
Bernhard Reutner-Fischer | 01d23ad | 2006-05-26 20:19:22 +0000 | [diff] [blame] | 289 | #if ENABLE_FEATURE_FTPGETPUT_LONG_OPTIONS |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 290 | static const char ftpgetput_longopts[] ALIGN1 = |
Denis Vlasenko | bdc88fd | 2007-07-23 17:14:14 +0000 | [diff] [blame] | 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" |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 296 | ; |
Bernhard Reutner-Fischer | 01d23ad | 2006-05-26 20:19:22 +0000 | [diff] [blame] | 297 | #endif |
Glenn L McGrath | b51eb26 | 2003-12-19 10:37:52 +0000 | [diff] [blame] | 298 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 299 | int ftpgetput_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 300 | int ftpgetput_main(int argc, char **argv) |
| 301 | { |
| 302 | /* content-length of the file */ |
Denis Vlasenko | 67b23e6 | 2006-10-03 21:00:06 +0000 | [diff] [blame] | 303 | unsigned opt; |
Denis Vlasenko | 562dc24 | 2007-01-03 21:55:50 +0000 | [diff] [blame] | 304 | const char *port = "ftp"; |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 305 | /* socket to ftp server */ |
| 306 | FILE *control_stream; |
Denis Vlasenko | 562dc24 | 2007-01-03 21:55:50 +0000 | [diff] [blame] | 307 | /* continue previous transfer (-c) */ |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 308 | ftp_host_info_t *server; |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 309 | |
Denis Vlasenko | 562dc24 | 2007-01-03 21:55:50 +0000 | [diff] [blame] | 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; |
Glenn L McGrath | 266c1f5 | 2003-12-20 03:19:27 +0000 | [diff] [blame] | 316 | /* Check to see if the command is ftpget or ftput */ |
Denis Vlasenko | 562dc24 | 2007-01-03 21:55:50 +0000 | [diff] [blame] | 317 | if (applet_name[3] == 'g') { |
Bernhard Reutner-Fischer | e0387a6 | 2006-06-07 13:31:59 +0000 | [diff] [blame] | 318 | ftp_action = ftp_receive; |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 319 | } |
Denis Vlasenko | 562dc24 | 2007-01-03 21:55:50 +0000 | [diff] [blame] | 320 | #endif |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 321 | |
| 322 | /* Set default values */ |
Denis Vlasenko | 4fa5e8b | 2007-01-11 22:39:25 +0000 | [diff] [blame] | 323 | server = xmalloc(sizeof(*server)); |
Glenn L McGrath | 266c1f5 | 2003-12-20 03:19:27 +0000 | [diff] [blame] | 324 | server->user = "anonymous"; |
| 325 | server->password = "busybox@"; |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 326 | |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 327 | /* |
| 328 | * Decipher the command line |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 329 | */ |
Denis Vlasenko | c61852a | 2006-11-29 11:09:43 +0000 | [diff] [blame] | 330 | #if ENABLE_FEATURE_FTPGETPUT_LONG_OPTIONS |
Denis Vlasenko | bdc88fd | 2007-07-23 17:14:14 +0000 | [diff] [blame] | 331 | applet_long_options = ftpgetput_longopts; |
Denis Vlasenko | c61852a | 2006-11-29 11:09:43 +0000 | [diff] [blame] | 332 | #endif |
Denis Vlasenko | 562dc24 | 2007-01-03 21:55:50 +0000 | [diff] [blame] | 333 | opt_complementary = "=3"; /* must have 3 params */ |
Denis Vlasenko | fe7cd64 | 2007-08-18 15:32:12 +0000 | [diff] [blame] | 334 | opt = getopt32(argv, "cvu:p:P:", &server->user, &server->password, &port); |
Denis Vlasenko | 562dc24 | 2007-01-03 21:55:50 +0000 | [diff] [blame] | 335 | argv += optind; |
Glenn L McGrath | 236e93d | 2003-12-20 05:43:34 +0000 | [diff] [blame] | 336 | |
| 337 | /* Process the non-option command line arguments */ |
Glenn L McGrath | b51eb26 | 2003-12-19 10:37:52 +0000 | [diff] [blame] | 338 | if (opt & FTPGETPUT_OPT_CONTINUE) { |
| 339 | do_continue = 1; |
| 340 | } |
| 341 | if (opt & FTPGETPUT_OPT_VERBOSE) { |
| 342 | verbose_flag = 1; |
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... */ |
Denis Vlasenko | 42823d5 | 2007-02-04 02:39:08 +0000 | [diff] [blame] | 348 | server->lsa = xhost2sockaddr(argv[0], bb_lookup_port(port, "tcp", 21)); |
Eric Andersen | 04d055f | 2003-11-03 21:20:18 +0000 | [diff] [blame] | 349 | if (verbose_flag) { |
Denis Vlasenko | 85629f0 | 2007-01-22 09:36:41 +0000 | [diff] [blame] | 350 | printf("Connecting to %s (%s)\n", argv[0], |
Denis Vlasenko | a27a11b | 2007-08-18 14:16:39 +0000 | [diff] [blame] | 351 | xmalloc_sockaddr2dotted(&server->lsa->sa)); |
Eric Andersen | 04d055f | 2003-11-03 21:20:18 +0000 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | /* Connect/Setup/Configure the FTP session */ |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 355 | control_stream = ftp_login(server); |
| 356 | |
Denis Vlasenko | 562dc24 | 2007-01-03 21:55:50 +0000 | [diff] [blame] | 357 | return ftp_action(server, control_stream, argv[1], argv[2]); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 358 | } |