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 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 16 | #include "libbb.h" |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 17 | |
Denis Vlasenko | 0e7940a | 2008-03-29 07:37:42 +0000 | [diff] [blame] | 18 | struct globals { |
Denis Vlasenko | b6aae0f | 2007-01-29 22:51:25 +0000 | [diff] [blame] | 19 | const char *user; |
| 20 | const char *password; |
Denis Vlasenko | 8e9ccba | 2007-01-11 16:50:23 +0000 | [diff] [blame] | 21 | struct len_and_sockaddr *lsa; |
Denis Vlasenko | 7cb808e | 2008-03-29 07:40:35 +0000 | [diff] [blame] | 22 | FILE *control_stream; |
Denis Vlasenko | 0e7940a | 2008-03-29 07:37:42 +0000 | [diff] [blame] | 23 | int verbose_flag; |
| 24 | int do_continue; |
Denis Vlasenko | 7cb808e | 2008-03-29 07:40:35 +0000 | [diff] [blame] | 25 | char buf[1]; /* actually [BUFSZ] */ |
Denis Vlasenko | 0e7940a | 2008-03-29 07:37:42 +0000 | [diff] [blame] | 26 | }; |
| 27 | #define G (*(struct globals*)&bb_common_bufsiz1) |
| 28 | enum { BUFSZ = COMMON_BUFSIZE - offsetof(struct globals, buf) }; |
| 29 | struct BUG_G_too_big { |
| 30 | char BUG_G_too_big[sizeof(G) <= COMMON_BUFSIZE ? 1 : -1]; |
| 31 | }; |
Denis Vlasenko | 7cb808e | 2008-03-29 07:40:35 +0000 | [diff] [blame] | 32 | #define user (G.user ) |
| 33 | #define password (G.password ) |
| 34 | #define lsa (G.lsa ) |
| 35 | #define control_stream (G.control_stream) |
| 36 | #define verbose_flag (G.verbose_flag ) |
| 37 | #define do_continue (G.do_continue ) |
| 38 | #define buf (G.buf ) |
Denis Vlasenko | 0e7940a | 2008-03-29 07:37:42 +0000 | [diff] [blame] | 39 | #define INIT_G() do { \ |
| 40 | } while (0) |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 41 | |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 42 | |
Denis Vlasenko | 0e7940a | 2008-03-29 07:37:42 +0000 | [diff] [blame] | 43 | static void ftp_die(const char *msg) ATTRIBUTE_NORETURN; |
| 44 | static void ftp_die(const char *msg) |
Denis Vlasenko | 562dc24 | 2007-01-03 21:55:50 +0000 | [diff] [blame] | 45 | { |
Denis Vlasenko | 7cb808e | 2008-03-29 07:40:35 +0000 | [diff] [blame] | 46 | char *cp = buf; /* buf holds peer's response */ |
Denis Vlasenko | 0e7940a | 2008-03-29 07:37:42 +0000 | [diff] [blame] | 47 | |
Denis Vlasenko | 562dc24 | 2007-01-03 21:55:50 +0000 | [diff] [blame] | 48 | /* Guard against garbage from remote server */ |
Denis Vlasenko | 0e7940a | 2008-03-29 07:37:42 +0000 | [diff] [blame] | 49 | while (*cp >= ' ' && *cp < '\x7f') |
| 50 | cp++; |
Denis Vlasenko | 7cb808e | 2008-03-29 07:40:35 +0000 | [diff] [blame] | 51 | *cp = '\0'; |
| 52 | bb_error_msg_and_die("unexpected server response%s%s: %s", |
| 53 | (msg ? " to " : ""), (msg ? msg : ""), buf); |
Denis Vlasenko | 562dc24 | 2007-01-03 21:55:50 +0000 | [diff] [blame] | 54 | } |
| 55 | |
Denis Vlasenko | 7cb808e | 2008-03-29 07:40:35 +0000 | [diff] [blame] | 56 | static int ftpcmd(const char *s1, const char *s2) |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 57 | { |
Denis Vlasenko | 562dc24 | 2007-01-03 21:55:50 +0000 | [diff] [blame] | 58 | unsigned n; |
Denis Vlasenko | 7cb808e | 2008-03-29 07:40:35 +0000 | [diff] [blame] | 59 | |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 60 | if (verbose_flag) { |
Denis Vlasenko | 3821fb1 | 2007-01-11 16:51:21 +0000 | [diff] [blame] | 61 | bb_error_msg("cmd %s %s", s1, s2); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | if (s1) { |
Denis Vlasenko | 7cb808e | 2008-03-29 07:40:35 +0000 | [diff] [blame] | 65 | fprintf(control_stream, (s2 ? "%s %s\r\n" : "%s %s\r\n"+3), |
| 66 | s1, s2); |
| 67 | fflush(control_stream); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 68 | } |
Denis Vlasenko | 0e7940a | 2008-03-29 07:37:42 +0000 | [diff] [blame] | 69 | |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 70 | do { |
Denis Vlasenko | 7cb808e | 2008-03-29 07:40:35 +0000 | [diff] [blame] | 71 | strcpy(buf, "EOF"); |
| 72 | if (fgets(buf, BUFSZ - 2, control_stream) == NULL) { |
| 73 | ftp_die(NULL); |
Glenn L McGrath | 5ec5828 | 2004-05-04 10:43:34 +0000 | [diff] [blame] | 74 | } |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 75 | } while (!isdigit(buf[0]) || buf[3] != ' '); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 76 | |
Denis Vlasenko | 562dc24 | 2007-01-03 21:55:50 +0000 | [diff] [blame] | 77 | buf[3] = '\0'; |
| 78 | n = xatou(buf); |
| 79 | buf[3] = ' '; |
| 80 | return n; |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 81 | } |
| 82 | |
Denis Vlasenko | 7cb808e | 2008-03-29 07:40:35 +0000 | [diff] [blame] | 83 | static void ftp_login(void) |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 84 | { |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 85 | /* Connect to the command socket */ |
Denis Vlasenko | 0e7940a | 2008-03-29 07:37:42 +0000 | [diff] [blame] | 86 | control_stream = fdopen(xconnect_stream(lsa), "r+"); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 87 | if (control_stream == NULL) { |
Denis Vlasenko | 8e9ccba | 2007-01-11 16:50:23 +0000 | [diff] [blame] | 88 | /* fdopen failed - extremely unlikely */ |
Denis Vlasenko | 562dc24 | 2007-01-03 21:55:50 +0000 | [diff] [blame] | 89 | bb_perror_nomsg_and_die(); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 90 | } |
| 91 | |
Denis Vlasenko | 7cb808e | 2008-03-29 07:40:35 +0000 | [diff] [blame] | 92 | if (ftpcmd(NULL, NULL) != 220) { |
Denis Vlasenko | 0e7940a | 2008-03-29 07:37:42 +0000 | [diff] [blame] | 93 | ftp_die(NULL); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | /* Login to the server */ |
Denis Vlasenko | 7cb808e | 2008-03-29 07:40:35 +0000 | [diff] [blame] | 97 | switch (ftpcmd("USER", user)) { |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 98 | case 230: |
| 99 | break; |
| 100 | case 331: |
Denis Vlasenko | 7cb808e | 2008-03-29 07:40:35 +0000 | [diff] [blame] | 101 | if (ftpcmd("PASS", password) != 230) { |
Denis Vlasenko | 0e7940a | 2008-03-29 07:37:42 +0000 | [diff] [blame] | 102 | ftp_die("PASS"); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 103 | } |
| 104 | break; |
| 105 | default: |
Denis Vlasenko | 0e7940a | 2008-03-29 07:37:42 +0000 | [diff] [blame] | 106 | ftp_die("USER"); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 107 | } |
| 108 | |
Denis Vlasenko | 7cb808e | 2008-03-29 07:40:35 +0000 | [diff] [blame] | 109 | ftpcmd("TYPE I", NULL); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 110 | } |
| 111 | |
Denis Vlasenko | 0e7940a | 2008-03-29 07:37:42 +0000 | [diff] [blame] | 112 | static int xconnect_ftpdata(void) |
Denis Vlasenko | 6c615a6 | 2008-03-28 22:11:49 +0000 | [diff] [blame] | 113 | { |
| 114 | char *buf_ptr; |
| 115 | unsigned port_num; |
| 116 | |
Denis Vlasenko | 7cb808e | 2008-03-29 07:40:35 +0000 | [diff] [blame] | 117 | /* |
| 118 | TODO: PASV command will not work for IPv6. RFC2428 describes |
| 119 | IPv6-capable "extended PASV" - EPSV. |
| 120 | |
| 121 | "EPSV [protocol]" asks server to bind to and listen on a data port |
| 122 | in specified protocol. Protocol is 1 for IPv4, 2 for IPv6. |
| 123 | If not specified, defaults to "same as used for control connection". |
| 124 | If server understood you, it should answer "229 <some text>(|||port|)" |
| 125 | where "|" are literal pipe chars and "port" is ASCII decimal port#. |
| 126 | |
| 127 | There is also an IPv6-capable replacement for PORT (EPRT), |
| 128 | but we don't need that. |
| 129 | |
| 130 | NB: PASV may still work for some servers even over IPv6. |
| 131 | For example, vsftp happily answers |
| 132 | "227 Entering Passive Mode (0,0,0,0,n,n)" and proceeds as usual. |
| 133 | |
| 134 | TODO2: need to stop ignoring IP address in PASV response. |
| 135 | */ |
| 136 | |
| 137 | if (ftpcmd("PASV", NULL) != 227) { |
| 138 | ftp_die("PASV"); |
| 139 | } |
| 140 | |
Denis Vlasenko | 6c615a6 | 2008-03-28 22:11:49 +0000 | [diff] [blame] | 141 | /* Response is "NNN garbageN1,N2,N3,N4,P1,P2[)garbage] |
| 142 | * Server's IP is N1.N2.N3.N4 (we ignore it) |
| 143 | * Server's port for data connection is P1*256+P2 */ |
| 144 | buf_ptr = strrchr(buf, ')'); |
| 145 | if (buf_ptr) *buf_ptr = '\0'; |
| 146 | |
| 147 | buf_ptr = strrchr(buf, ','); |
| 148 | *buf_ptr = '\0'; |
| 149 | port_num = xatoul_range(buf_ptr + 1, 0, 255); |
| 150 | |
| 151 | buf_ptr = strrchr(buf, ','); |
| 152 | *buf_ptr = '\0'; |
| 153 | port_num += xatoul_range(buf_ptr + 1, 0, 255) * 256; |
| 154 | |
Denis Vlasenko | 0e7940a | 2008-03-29 07:37:42 +0000 | [diff] [blame] | 155 | set_nport(lsa, htons(port_num)); |
| 156 | return xconnect_stream(lsa); |
Denis Vlasenko | 6c615a6 | 2008-03-28 22:11:49 +0000 | [diff] [blame] | 157 | } |
| 158 | |
Denis Vlasenko | 7cb808e | 2008-03-29 07:40:35 +0000 | [diff] [blame] | 159 | static int pump_data_and_QUIT(int from, int to) |
| 160 | { |
| 161 | /* copy the file */ |
| 162 | if (bb_copyfd_eof(from, to) == -1) { |
| 163 | /* error msg is already printed by bb_copyfd_eof */ |
| 164 | return EXIT_FAILURE; |
| 165 | } |
| 166 | |
| 167 | /* close data connection */ |
| 168 | close(from); /* don't know which one is that, so we close both */ |
| 169 | close(to); |
| 170 | |
| 171 | /* does server confirm that transfer is finished? */ |
| 172 | if (ftpcmd(NULL, NULL) != 226) { |
| 173 | ftp_die(NULL); |
| 174 | } |
| 175 | ftpcmd("QUIT", NULL); |
| 176 | |
| 177 | return EXIT_SUCCESS; |
| 178 | } |
| 179 | |
Rob Landley | b1c3fbc | 2006-05-04 19:52:28 +0000 | [diff] [blame] | 180 | #if !ENABLE_FTPGET |
Denis Vlasenko | 7cb808e | 2008-03-29 07:40:35 +0000 | [diff] [blame] | 181 | int ftp_receive(const char *local_path, char *server_path); |
Rob Landley | b1c3fbc | 2006-05-04 19:52:28 +0000 | [diff] [blame] | 182 | #else |
Denis Vlasenko | 7039a66 | 2006-10-08 17:54:47 +0000 | [diff] [blame] | 183 | static |
Denis Vlasenko | 7cb808e | 2008-03-29 07:40:35 +0000 | [diff] [blame] | 184 | int ftp_receive(const char *local_path, char *server_path) |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 185 | { |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 186 | int fd_data; |
Glenn L McGrath | 236e93d | 2003-12-20 05:43:34 +0000 | [diff] [blame] | 187 | int fd_local = -1; |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 188 | off_t beg_range = 0; |
| 189 | |
Denis Vlasenko | 6c615a6 | 2008-03-28 22:11:49 +0000 | [diff] [blame] | 190 | /* connect to the data socket */ |
Denis Vlasenko | 0e7940a | 2008-03-29 07:37:42 +0000 | [diff] [blame] | 191 | fd_data = xconnect_ftpdata(); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 192 | |
Denis Vlasenko | 7cb808e | 2008-03-29 07:40:35 +0000 | [diff] [blame] | 193 | if (ftpcmd("SIZE", server_path) != 213) { |
Rob Landley | bc059bc | 2006-01-10 06:36:00 +0000 | [diff] [blame] | 194 | do_continue = 0; |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 195 | } |
| 196 | |
Denis Vlasenko | 9f73944 | 2006-12-16 23:49:13 +0000 | [diff] [blame] | 197 | if (LONE_DASH(local_path)) { |
Eric Andersen | 70060d2 | 2004-03-27 10:02:48 +0000 | [diff] [blame] | 198 | fd_local = STDOUT_FILENO; |
Glenn L McGrath | 236e93d | 2003-12-20 05:43:34 +0000 | [diff] [blame] | 199 | do_continue = 0; |
| 200 | } |
| 201 | |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 202 | if (do_continue) { |
| 203 | struct stat sbuf; |
Denis Vlasenko | 6c615a6 | 2008-03-28 22:11:49 +0000 | [diff] [blame] | 204 | /* lstat would be wrong here! */ |
| 205 | if (stat(local_path, &sbuf) < 0) { |
| 206 | bb_perror_msg_and_die("stat"); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 207 | } |
| 208 | if (sbuf.st_size > 0) { |
| 209 | beg_range = sbuf.st_size; |
| 210 | } else { |
| 211 | do_continue = 0; |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | if (do_continue) { |
Denis Vlasenko | cf30cc8 | 2006-11-24 14:53:18 +0000 | [diff] [blame] | 216 | sprintf(buf, "REST %"OFF_FMT"d", beg_range); |
Denis Vlasenko | 7cb808e | 2008-03-29 07:40:35 +0000 | [diff] [blame] | 217 | if (ftpcmd(buf, NULL) != 350) { |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 218 | do_continue = 0; |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 219 | } |
| 220 | } |
| 221 | |
Denis Vlasenko | 7cb808e | 2008-03-29 07:40:35 +0000 | [diff] [blame] | 222 | if (ftpcmd("RETR", server_path) > 150) { |
Denis Vlasenko | 0e7940a | 2008-03-29 07:37:42 +0000 | [diff] [blame] | 223 | ftp_die("RETR"); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 224 | } |
| 225 | |
Denis Vlasenko | 7cb808e | 2008-03-29 07:40:35 +0000 | [diff] [blame] | 226 | /* create local file _after_ we know that remote file exists */ |
Glenn L McGrath | 236e93d | 2003-12-20 05:43:34 +0000 | [diff] [blame] | 227 | if (fd_local == -1) { |
Denis Vlasenko | 6c615a6 | 2008-03-28 22:11:49 +0000 | [diff] [blame] | 228 | fd_local = xopen(local_path, |
| 229 | do_continue ? (O_APPEND | O_WRONLY) |
| 230 | : (O_CREAT | O_TRUNC | O_WRONLY) |
| 231 | ); |
Glenn L McGrath | 1643f41 | 2002-12-18 02:47:40 +0000 | [diff] [blame] | 232 | } |
| 233 | |
Denis Vlasenko | 7cb808e | 2008-03-29 07:40:35 +0000 | [diff] [blame] | 234 | return pump_data_and_QUIT(fd_data, fd_local); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 235 | } |
| 236 | #endif |
| 237 | |
Rob Landley | b1c3fbc | 2006-05-04 19:52:28 +0000 | [diff] [blame] | 238 | #if !ENABLE_FTPPUT |
Denis Vlasenko | 7cb808e | 2008-03-29 07:40:35 +0000 | [diff] [blame] | 239 | int ftp_send(const char *server_path, char *local_path); |
Rob Landley | b1c3fbc | 2006-05-04 19:52:28 +0000 | [diff] [blame] | 240 | #else |
Denis Vlasenko | 7039a66 | 2006-10-08 17:54:47 +0000 | [diff] [blame] | 241 | static |
Denis Vlasenko | 7cb808e | 2008-03-29 07:40:35 +0000 | [diff] [blame] | 242 | int ftp_send(const char *server_path, char *local_path) |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 243 | { |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 244 | int fd_data; |
| 245 | int fd_local; |
| 246 | int response; |
| 247 | |
Denis Vlasenko | 6c615a6 | 2008-03-28 22:11:49 +0000 | [diff] [blame] | 248 | /* connect to the data socket */ |
Denis Vlasenko | 0e7940a | 2008-03-29 07:37:42 +0000 | [diff] [blame] | 249 | fd_data = xconnect_ftpdata(); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 250 | |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 251 | /* get the local file */ |
Denis Vlasenko | 9f73944 | 2006-12-16 23:49:13 +0000 | [diff] [blame] | 252 | fd_local = STDIN_FILENO; |
Denis Vlasenko | 0e7940a | 2008-03-29 07:37:42 +0000 | [diff] [blame] | 253 | if (NOT_LONE_DASH(local_path)) |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 254 | fd_local = xopen(local_path, O_RDONLY); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 255 | |
Denis Vlasenko | 7cb808e | 2008-03-29 07:40:35 +0000 | [diff] [blame] | 256 | response = ftpcmd("STOR", server_path); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 257 | switch (response) { |
| 258 | case 125: |
| 259 | case 150: |
| 260 | break; |
| 261 | default: |
Denis Vlasenko | 0e7940a | 2008-03-29 07:37:42 +0000 | [diff] [blame] | 262 | ftp_die("STOR"); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 263 | } |
| 264 | |
Denis Vlasenko | 7cb808e | 2008-03-29 07:40:35 +0000 | [diff] [blame] | 265 | return pump_data_and_QUIT(fd_local, fd_data); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 266 | } |
| 267 | #endif |
| 268 | |
Bernhard Reutner-Fischer | 01d23ad | 2006-05-26 20:19:22 +0000 | [diff] [blame] | 269 | #if ENABLE_FEATURE_FTPGETPUT_LONG_OPTIONS |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 270 | static const char ftpgetput_longopts[] ALIGN1 = |
Denis Vlasenko | bdc88fd | 2007-07-23 17:14:14 +0000 | [diff] [blame] | 271 | "continue\0" Required_argument "c" |
| 272 | "verbose\0" No_argument "v" |
| 273 | "username\0" Required_argument "u" |
| 274 | "password\0" Required_argument "p" |
| 275 | "port\0" Required_argument "P" |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 276 | ; |
Bernhard Reutner-Fischer | 01d23ad | 2006-05-26 20:19:22 +0000 | [diff] [blame] | 277 | #endif |
Glenn L McGrath | b51eb26 | 2003-12-19 10:37:52 +0000 | [diff] [blame] | 278 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 279 | int ftpgetput_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 280 | int ftpgetput_main(int argc ATTRIBUTE_UNUSED, char **argv) |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 281 | { |
Denis Vlasenko | 67b23e6 | 2006-10-03 21:00:06 +0000 | [diff] [blame] | 282 | unsigned opt; |
Denis Vlasenko | 562dc24 | 2007-01-03 21:55:50 +0000 | [diff] [blame] | 283 | const char *port = "ftp"; |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 284 | /* socket to ftp server */ |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 285 | |
Denis Vlasenko | 562dc24 | 2007-01-03 21:55:50 +0000 | [diff] [blame] | 286 | #if ENABLE_FTPPUT && !ENABLE_FTPGET |
| 287 | # define ftp_action ftp_send |
| 288 | #elif ENABLE_FTPGET && !ENABLE_FTPPUT |
| 289 | # define ftp_action ftp_receive |
| 290 | #else |
Denis Vlasenko | 7cb808e | 2008-03-29 07:40:35 +0000 | [diff] [blame] | 291 | int (*ftp_action)(const char *, char *) = ftp_send; |
Denis Vlasenko | 0e7940a | 2008-03-29 07:37:42 +0000 | [diff] [blame] | 292 | |
Glenn L McGrath | 266c1f5 | 2003-12-20 03:19:27 +0000 | [diff] [blame] | 293 | /* Check to see if the command is ftpget or ftput */ |
Denis Vlasenko | 562dc24 | 2007-01-03 21:55:50 +0000 | [diff] [blame] | 294 | if (applet_name[3] == 'g') { |
Bernhard Reutner-Fischer | e0387a6 | 2006-06-07 13:31:59 +0000 | [diff] [blame] | 295 | ftp_action = ftp_receive; |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 296 | } |
Denis Vlasenko | 562dc24 | 2007-01-03 21:55:50 +0000 | [diff] [blame] | 297 | #endif |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 298 | |
Denis Vlasenko | 0e7940a | 2008-03-29 07:37:42 +0000 | [diff] [blame] | 299 | INIT_G(); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 300 | /* Set default values */ |
Denis Vlasenko | 0e7940a | 2008-03-29 07:37:42 +0000 | [diff] [blame] | 301 | user = "anonymous"; |
| 302 | password = "busybox@"; |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 303 | |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 304 | /* |
| 305 | * Decipher the command line |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 306 | */ |
Denis Vlasenko | c61852a | 2006-11-29 11:09:43 +0000 | [diff] [blame] | 307 | #if ENABLE_FEATURE_FTPGETPUT_LONG_OPTIONS |
Denis Vlasenko | bdc88fd | 2007-07-23 17:14:14 +0000 | [diff] [blame] | 308 | applet_long_options = ftpgetput_longopts; |
Denis Vlasenko | c61852a | 2006-11-29 11:09:43 +0000 | [diff] [blame] | 309 | #endif |
Denis Vlasenko | 0e7940a | 2008-03-29 07:37:42 +0000 | [diff] [blame] | 310 | opt_complementary = "=3:vv:cc"; /* must have 3 params; -v and -c count */ |
| 311 | opt = getopt32(argv, "cvu:p:P:", &user, &password, &port, |
| 312 | &verbose_flag, &do_continue); |
Denis Vlasenko | 562dc24 | 2007-01-03 21:55:50 +0000 | [diff] [blame] | 313 | argv += optind; |
Glenn L McGrath | 236e93d | 2003-12-20 05:43:34 +0000 | [diff] [blame] | 314 | |
Eric Andersen | 04d055f | 2003-11-03 21:20:18 +0000 | [diff] [blame] | 315 | /* We want to do exactly _one_ DNS lookup, since some |
| 316 | * sites (i.e. ftp.us.debian.org) use round-robin DNS |
| 317 | * and we want to connect to only one IP... */ |
Denis Vlasenko | 0e7940a | 2008-03-29 07:37:42 +0000 | [diff] [blame] | 318 | lsa = xhost2sockaddr(argv[0], bb_lookup_port(port, "tcp", 21)); |
Eric Andersen | 04d055f | 2003-11-03 21:20:18 +0000 | [diff] [blame] | 319 | if (verbose_flag) { |
Denis Vlasenko | 85629f0 | 2007-01-22 09:36:41 +0000 | [diff] [blame] | 320 | printf("Connecting to %s (%s)\n", argv[0], |
Denis Vlasenko | 0e7940a | 2008-03-29 07:37:42 +0000 | [diff] [blame] | 321 | xmalloc_sockaddr2dotted(&lsa->u.sa)); |
Eric Andersen | 04d055f | 2003-11-03 21:20:18 +0000 | [diff] [blame] | 322 | } |
| 323 | |
Denis Vlasenko | 7cb808e | 2008-03-29 07:40:35 +0000 | [diff] [blame] | 324 | ftp_login(); |
| 325 | return ftp_action(argv[1], argv[2]); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 326 | } |