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