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: "\n -c Continue previous transfer" |
| 43 | //usage: "\n -v Verbose" |
Denys Vlasenko | cc1bb60 | 2012-03-19 12:22:57 +0100 | [diff] [blame] | 44 | //usage: "\n -u USER Username" |
| 45 | //usage: "\n -p PASS Password" |
| 46 | //usage: "\n -P NUM Port" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 47 | //usage: |
| 48 | //usage:#define ftpput_trivial_usage |
| 49 | //usage: "[OPTIONS] HOST [REMOTE_FILE] LOCAL_FILE" |
| 50 | //usage:#define ftpput_full_usage "\n\n" |
Denys Vlasenko | cc1bb60 | 2012-03-19 12:22:57 +0100 | [diff] [blame] | 51 | //usage: "Upload a file to a FTP server\n" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 52 | //usage: "\n -v Verbose" |
Denys Vlasenko | cc1bb60 | 2012-03-19 12:22:57 +0100 | [diff] [blame] | 53 | //usage: "\n -u USER Username" |
| 54 | //usage: "\n -p PASS Password" |
| 55 | //usage: "\n -P NUM Port number" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 56 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 57 | #include "libbb.h" |
Denys Vlasenko | e6a2f4c | 2016-04-21 16:26:30 +0200 | [diff] [blame] | 58 | #include "common_bufsiz.h" |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 59 | |
Denis Vlasenko | 0e7940a | 2008-03-29 07:37:42 +0000 | [diff] [blame] | 60 | struct globals { |
Denis Vlasenko | b6aae0f | 2007-01-29 22:51:25 +0000 | [diff] [blame] | 61 | const char *user; |
| 62 | const char *password; |
Denis Vlasenko | 8e9ccba | 2007-01-11 16:50:23 +0000 | [diff] [blame] | 63 | struct len_and_sockaddr *lsa; |
Denis Vlasenko | 7cb808e | 2008-03-29 07:40:35 +0000 | [diff] [blame] | 64 | FILE *control_stream; |
Denis Vlasenko | 0e7940a | 2008-03-29 07:37:42 +0000 | [diff] [blame] | 65 | int verbose_flag; |
| 66 | int do_continue; |
Denys Vlasenko | 60a9414 | 2011-05-13 20:57:01 +0200 | [diff] [blame] | 67 | char buf[4]; /* actually [BUFSZ] */ |
Denys Vlasenko | 98a4c7c | 2010-02-04 15:00:15 +0100 | [diff] [blame] | 68 | } FIX_ALIASING; |
Denys Vlasenko | e6a2f4c | 2016-04-21 16:26:30 +0200 | [diff] [blame] | 69 | #define G (*(struct globals*)bb_common_bufsiz1) |
Denis Vlasenko | 0e7940a | 2008-03-29 07:37:42 +0000 | [diff] [blame] | 70 | enum { BUFSZ = COMMON_BUFSIZE - offsetof(struct globals, buf) }; |
Denis Vlasenko | 7cb808e | 2008-03-29 07:40:35 +0000 | [diff] [blame] | 71 | #define user (G.user ) |
| 72 | #define password (G.password ) |
| 73 | #define lsa (G.lsa ) |
| 74 | #define control_stream (G.control_stream) |
| 75 | #define verbose_flag (G.verbose_flag ) |
| 76 | #define do_continue (G.do_continue ) |
| 77 | #define buf (G.buf ) |
Denys Vlasenko | ab3964d | 2015-10-13 14:50:20 +0200 | [diff] [blame] | 78 | #define INIT_G() do { \ |
Denys Vlasenko | 47cfbf3 | 2016-04-21 18:18:48 +0200 | [diff] [blame] | 79 | setup_common_bufsiz(); \ |
Denys Vlasenko | ab3964d | 2015-10-13 14:50:20 +0200 | [diff] [blame] | 80 | BUILD_BUG_ON(sizeof(G) > COMMON_BUFSIZE); \ |
| 81 | } while (0) |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 82 | |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 83 | |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 84 | static void ftp_die(const char *msg) NORETURN; |
Denis Vlasenko | 0e7940a | 2008-03-29 07:37:42 +0000 | [diff] [blame] | 85 | static void ftp_die(const char *msg) |
Denis Vlasenko | 562dc24 | 2007-01-03 21:55:50 +0000 | [diff] [blame] | 86 | { |
Denis Vlasenko | 7cb808e | 2008-03-29 07:40:35 +0000 | [diff] [blame] | 87 | char *cp = buf; /* buf holds peer's response */ |
Denis Vlasenko | 0e7940a | 2008-03-29 07:37:42 +0000 | [diff] [blame] | 88 | |
Denis Vlasenko | 562dc24 | 2007-01-03 21:55:50 +0000 | [diff] [blame] | 89 | /* Guard against garbage from remote server */ |
Denis Vlasenko | 0e7940a | 2008-03-29 07:37:42 +0000 | [diff] [blame] | 90 | while (*cp >= ' ' && *cp < '\x7f') |
| 91 | cp++; |
Denis Vlasenko | 7cb808e | 2008-03-29 07:40:35 +0000 | [diff] [blame] | 92 | *cp = '\0'; |
| 93 | bb_error_msg_and_die("unexpected server response%s%s: %s", |
| 94 | (msg ? " to " : ""), (msg ? msg : ""), buf); |
Denis Vlasenko | 562dc24 | 2007-01-03 21:55:50 +0000 | [diff] [blame] | 95 | } |
| 96 | |
Denis Vlasenko | 7cb808e | 2008-03-29 07:40:35 +0000 | [diff] [blame] | 97 | static int ftpcmd(const char *s1, const char *s2) |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 98 | { |
Denis Vlasenko | 562dc24 | 2007-01-03 21:55:50 +0000 | [diff] [blame] | 99 | unsigned n; |
Denis Vlasenko | 7cb808e | 2008-03-29 07:40:35 +0000 | [diff] [blame] | 100 | |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 101 | if (verbose_flag) { |
Denis Vlasenko | 3821fb1 | 2007-01-11 16:51:21 +0000 | [diff] [blame] | 102 | bb_error_msg("cmd %s %s", s1, s2); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | if (s1) { |
Denis Vlasenko | 7cb808e | 2008-03-29 07:40:35 +0000 | [diff] [blame] | 106 | fprintf(control_stream, (s2 ? "%s %s\r\n" : "%s %s\r\n"+3), |
| 107 | s1, s2); |
| 108 | fflush(control_stream); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 109 | } |
Denis Vlasenko | 0e7940a | 2008-03-29 07:37:42 +0000 | [diff] [blame] | 110 | |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 111 | do { |
Denys Vlasenko | 60a9414 | 2011-05-13 20:57:01 +0200 | [diff] [blame] | 112 | strcpy(buf, "EOF"); /* for ftp_die */ |
Denis Vlasenko | 7cb808e | 2008-03-29 07:40:35 +0000 | [diff] [blame] | 113 | if (fgets(buf, BUFSZ - 2, control_stream) == NULL) { |
| 114 | ftp_die(NULL); |
Glenn L McGrath | 5ec5828 | 2004-05-04 10:43:34 +0000 | [diff] [blame] | 115 | } |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 116 | } while (!isdigit(buf[0]) || buf[3] != ' '); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 117 | |
Denis Vlasenko | 562dc24 | 2007-01-03 21:55:50 +0000 | [diff] [blame] | 118 | buf[3] = '\0'; |
| 119 | n = xatou(buf); |
| 120 | buf[3] = ' '; |
| 121 | return n; |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 122 | } |
| 123 | |
Denis Vlasenko | 7cb808e | 2008-03-29 07:40:35 +0000 | [diff] [blame] | 124 | static void ftp_login(void) |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 125 | { |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 126 | /* Connect to the command socket */ |
Denis Vlasenko | 0e7940a | 2008-03-29 07:37:42 +0000 | [diff] [blame] | 127 | control_stream = fdopen(xconnect_stream(lsa), "r+"); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 128 | if (control_stream == NULL) { |
Denis Vlasenko | 8e9ccba | 2007-01-11 16:50:23 +0000 | [diff] [blame] | 129 | /* fdopen failed - extremely unlikely */ |
Denis Vlasenko | 562dc24 | 2007-01-03 21:55:50 +0000 | [diff] [blame] | 130 | bb_perror_nomsg_and_die(); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 131 | } |
| 132 | |
Denis Vlasenko | 7cb808e | 2008-03-29 07:40:35 +0000 | [diff] [blame] | 133 | if (ftpcmd(NULL, NULL) != 220) { |
Denis Vlasenko | 0e7940a | 2008-03-29 07:37:42 +0000 | [diff] [blame] | 134 | ftp_die(NULL); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | /* Login to the server */ |
Denis Vlasenko | 7cb808e | 2008-03-29 07:40:35 +0000 | [diff] [blame] | 138 | switch (ftpcmd("USER", user)) { |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 139 | case 230: |
| 140 | break; |
| 141 | case 331: |
Denis Vlasenko | 7cb808e | 2008-03-29 07:40:35 +0000 | [diff] [blame] | 142 | if (ftpcmd("PASS", password) != 230) { |
Denis Vlasenko | 0e7940a | 2008-03-29 07:37:42 +0000 | [diff] [blame] | 143 | ftp_die("PASS"); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 144 | } |
| 145 | break; |
| 146 | default: |
Denis Vlasenko | 0e7940a | 2008-03-29 07:37:42 +0000 | [diff] [blame] | 147 | ftp_die("USER"); |
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 | ftpcmd("TYPE I", NULL); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 151 | } |
| 152 | |
Denis Vlasenko | 0e7940a | 2008-03-29 07:37:42 +0000 | [diff] [blame] | 153 | static int xconnect_ftpdata(void) |
Denis Vlasenko | 6c615a6 | 2008-03-28 22:11:49 +0000 | [diff] [blame] | 154 | { |
Denys Vlasenko | 1783ffa | 2018-02-06 15:48:12 +0100 | [diff] [blame] | 155 | int port_num; |
Denis Vlasenko | 6c615a6 | 2008-03-28 22:11:49 +0000 | [diff] [blame] | 156 | |
Denys Vlasenko | 1783ffa | 2018-02-06 15:48:12 +0100 | [diff] [blame] | 157 | if (ENABLE_FEATURE_IPV6 && ftpcmd("EPSV", NULL) == 229) { |
| 158 | /* good */ |
| 159 | } else if (ftpcmd("PASV", NULL) != 227) { |
| 160 | ftp_die("PASV"); |
Denys Vlasenko | 05b84d4 | 2018-02-04 19:08:34 +0100 | [diff] [blame] | 161 | } |
Denys Vlasenko | 1783ffa | 2018-02-06 15:48:12 +0100 | [diff] [blame] | 162 | port_num = parse_pasv_epsv(buf); |
| 163 | if (port_num < 0) |
| 164 | ftp_die("PASV"); |
Denis Vlasenko | 6c615a6 | 2008-03-28 22:11:49 +0000 | [diff] [blame] | 165 | |
Denys Vlasenko | ca18311 | 2011-04-07 17:52:20 +0200 | [diff] [blame] | 166 | set_nport(&lsa->u.sa, htons(port_num)); |
Denis Vlasenko | 0e7940a | 2008-03-29 07:37:42 +0000 | [diff] [blame] | 167 | return xconnect_stream(lsa); |
Denis Vlasenko | 6c615a6 | 2008-03-28 22:11:49 +0000 | [diff] [blame] | 168 | } |
| 169 | |
Denis Vlasenko | 7cb808e | 2008-03-29 07:40:35 +0000 | [diff] [blame] | 170 | static int pump_data_and_QUIT(int from, int to) |
| 171 | { |
| 172 | /* copy the file */ |
| 173 | if (bb_copyfd_eof(from, to) == -1) { |
| 174 | /* error msg is already printed by bb_copyfd_eof */ |
| 175 | return EXIT_FAILURE; |
| 176 | } |
| 177 | |
| 178 | /* close data connection */ |
| 179 | close(from); /* don't know which one is that, so we close both */ |
| 180 | close(to); |
| 181 | |
| 182 | /* does server confirm that transfer is finished? */ |
| 183 | if (ftpcmd(NULL, NULL) != 226) { |
| 184 | ftp_die(NULL); |
| 185 | } |
| 186 | ftpcmd("QUIT", NULL); |
| 187 | |
| 188 | return EXIT_SUCCESS; |
| 189 | } |
| 190 | |
Rob Landley | b1c3fbc | 2006-05-04 19:52:28 +0000 | [diff] [blame] | 191 | #if !ENABLE_FTPGET |
Denis Vlasenko | 7cb808e | 2008-03-29 07:40:35 +0000 | [diff] [blame] | 192 | int ftp_receive(const char *local_path, char *server_path); |
Rob Landley | b1c3fbc | 2006-05-04 19:52:28 +0000 | [diff] [blame] | 193 | #else |
Denis Vlasenko | 7039a66 | 2006-10-08 17:54:47 +0000 | [diff] [blame] | 194 | static |
Denis Vlasenko | 7cb808e | 2008-03-29 07:40:35 +0000 | [diff] [blame] | 195 | int ftp_receive(const char *local_path, char *server_path) |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 196 | { |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 197 | int fd_data; |
Glenn L McGrath | 236e93d | 2003-12-20 05:43:34 +0000 | [diff] [blame] | 198 | int fd_local = -1; |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 199 | off_t beg_range = 0; |
| 200 | |
Denis Vlasenko | 6c615a6 | 2008-03-28 22:11:49 +0000 | [diff] [blame] | 201 | /* connect to the data socket */ |
Denis Vlasenko | 0e7940a | 2008-03-29 07:37:42 +0000 | [diff] [blame] | 202 | fd_data = xconnect_ftpdata(); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 203 | |
Denis Vlasenko | 7cb808e | 2008-03-29 07:40:35 +0000 | [diff] [blame] | 204 | if (ftpcmd("SIZE", server_path) != 213) { |
Rob Landley | bc059bc | 2006-01-10 06:36:00 +0000 | [diff] [blame] | 205 | do_continue = 0; |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 206 | } |
| 207 | |
Denis Vlasenko | 9f73944 | 2006-12-16 23:49:13 +0000 | [diff] [blame] | 208 | if (LONE_DASH(local_path)) { |
Eric Andersen | 70060d2 | 2004-03-27 10:02:48 +0000 | [diff] [blame] | 209 | fd_local = STDOUT_FILENO; |
Glenn L McGrath | 236e93d | 2003-12-20 05:43:34 +0000 | [diff] [blame] | 210 | do_continue = 0; |
| 211 | } |
| 212 | |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 213 | if (do_continue) { |
| 214 | struct stat sbuf; |
Denis Vlasenko | 6c615a6 | 2008-03-28 22:11:49 +0000 | [diff] [blame] | 215 | /* lstat would be wrong here! */ |
| 216 | if (stat(local_path, &sbuf) < 0) { |
| 217 | bb_perror_msg_and_die("stat"); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 218 | } |
| 219 | if (sbuf.st_size > 0) { |
| 220 | beg_range = sbuf.st_size; |
| 221 | } else { |
| 222 | do_continue = 0; |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | if (do_continue) { |
Denys Vlasenko | a3aa3e3 | 2009-12-11 12:36:10 +0100 | [diff] [blame] | 227 | sprintf(buf, "REST %"OFF_FMT"u", beg_range); |
Denis Vlasenko | 7cb808e | 2008-03-29 07:40:35 +0000 | [diff] [blame] | 228 | if (ftpcmd(buf, NULL) != 350) { |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 229 | do_continue = 0; |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 230 | } |
| 231 | } |
| 232 | |
Denis Vlasenko | 7cb808e | 2008-03-29 07:40:35 +0000 | [diff] [blame] | 233 | if (ftpcmd("RETR", server_path) > 150) { |
Denis Vlasenko | 0e7940a | 2008-03-29 07:37:42 +0000 | [diff] [blame] | 234 | ftp_die("RETR"); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 235 | } |
| 236 | |
Denis Vlasenko | 7cb808e | 2008-03-29 07:40:35 +0000 | [diff] [blame] | 237 | /* create local file _after_ we know that remote file exists */ |
Glenn L McGrath | 236e93d | 2003-12-20 05:43:34 +0000 | [diff] [blame] | 238 | if (fd_local == -1) { |
Denis Vlasenko | 6c615a6 | 2008-03-28 22:11:49 +0000 | [diff] [blame] | 239 | fd_local = xopen(local_path, |
| 240 | do_continue ? (O_APPEND | O_WRONLY) |
| 241 | : (O_CREAT | O_TRUNC | O_WRONLY) |
| 242 | ); |
Glenn L McGrath | 1643f41 | 2002-12-18 02:47:40 +0000 | [diff] [blame] | 243 | } |
| 244 | |
Denis Vlasenko | 7cb808e | 2008-03-29 07:40:35 +0000 | [diff] [blame] | 245 | return pump_data_and_QUIT(fd_data, fd_local); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 246 | } |
| 247 | #endif |
| 248 | |
Rob Landley | b1c3fbc | 2006-05-04 19:52:28 +0000 | [diff] [blame] | 249 | #if !ENABLE_FTPPUT |
Denis Vlasenko | 7cb808e | 2008-03-29 07:40:35 +0000 | [diff] [blame] | 250 | int ftp_send(const char *server_path, char *local_path); |
Rob Landley | b1c3fbc | 2006-05-04 19:52:28 +0000 | [diff] [blame] | 251 | #else |
Denis Vlasenko | 7039a66 | 2006-10-08 17:54:47 +0000 | [diff] [blame] | 252 | static |
Denis Vlasenko | 7cb808e | 2008-03-29 07:40:35 +0000 | [diff] [blame] | 253 | int ftp_send(const char *server_path, char *local_path) |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 254 | { |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 255 | int fd_data; |
| 256 | int fd_local; |
| 257 | int response; |
| 258 | |
Denis Vlasenko | 6c615a6 | 2008-03-28 22:11:49 +0000 | [diff] [blame] | 259 | /* connect to the data socket */ |
Denis Vlasenko | 0e7940a | 2008-03-29 07:37:42 +0000 | [diff] [blame] | 260 | fd_data = xconnect_ftpdata(); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 261 | |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 262 | /* get the local file */ |
Denis Vlasenko | 9f73944 | 2006-12-16 23:49:13 +0000 | [diff] [blame] | 263 | fd_local = STDIN_FILENO; |
Denis Vlasenko | 0e7940a | 2008-03-29 07:37:42 +0000 | [diff] [blame] | 264 | if (NOT_LONE_DASH(local_path)) |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 265 | fd_local = xopen(local_path, O_RDONLY); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 266 | |
Denis Vlasenko | 7cb808e | 2008-03-29 07:40:35 +0000 | [diff] [blame] | 267 | response = ftpcmd("STOR", server_path); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 268 | switch (response) { |
| 269 | case 125: |
| 270 | case 150: |
| 271 | break; |
| 272 | default: |
Denis Vlasenko | 0e7940a | 2008-03-29 07:37:42 +0000 | [diff] [blame] | 273 | ftp_die("STOR"); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 274 | } |
| 275 | |
Denis Vlasenko | 7cb808e | 2008-03-29 07:40:35 +0000 | [diff] [blame] | 276 | return pump_data_and_QUIT(fd_local, fd_data); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 277 | } |
| 278 | #endif |
| 279 | |
Bernhard Reutner-Fischer | 01d23ad | 2006-05-26 20:19:22 +0000 | [diff] [blame] | 280 | #if ENABLE_FEATURE_FTPGETPUT_LONG_OPTIONS |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 281 | static const char ftpgetput_longopts[] ALIGN1 = |
Denis Vlasenko | bdc88fd | 2007-07-23 17:14:14 +0000 | [diff] [blame] | 282 | "continue\0" Required_argument "c" |
| 283 | "verbose\0" No_argument "v" |
| 284 | "username\0" Required_argument "u" |
| 285 | "password\0" Required_argument "p" |
| 286 | "port\0" Required_argument "P" |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 287 | ; |
Bernhard Reutner-Fischer | 01d23ad | 2006-05-26 20:19:22 +0000 | [diff] [blame] | 288 | #endif |
Glenn L McGrath | b51eb26 | 2003-12-19 10:37:52 +0000 | [diff] [blame] | 289 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 290 | int ftpgetput_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 291 | int ftpgetput_main(int argc UNUSED_PARAM, char **argv) |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 292 | { |
Denis Vlasenko | 562dc24 | 2007-01-03 21:55:50 +0000 | [diff] [blame] | 293 | const char *port = "ftp"; |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 294 | /* socket to ftp server */ |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 295 | |
Denis Vlasenko | 562dc24 | 2007-01-03 21:55:50 +0000 | [diff] [blame] | 296 | #if ENABLE_FTPPUT && !ENABLE_FTPGET |
| 297 | # define ftp_action ftp_send |
| 298 | #elif ENABLE_FTPGET && !ENABLE_FTPPUT |
| 299 | # define ftp_action ftp_receive |
| 300 | #else |
Denis Vlasenko | 7cb808e | 2008-03-29 07:40:35 +0000 | [diff] [blame] | 301 | int (*ftp_action)(const char *, char *) = ftp_send; |
Denis Vlasenko | 0e7940a | 2008-03-29 07:37:42 +0000 | [diff] [blame] | 302 | |
Glenn L McGrath | 266c1f5 | 2003-12-20 03:19:27 +0000 | [diff] [blame] | 303 | /* Check to see if the command is ftpget or ftput */ |
Denis Vlasenko | 562dc24 | 2007-01-03 21:55:50 +0000 | [diff] [blame] | 304 | if (applet_name[3] == 'g') { |
Bernhard Reutner-Fischer | e0387a6 | 2006-06-07 13:31:59 +0000 | [diff] [blame] | 305 | ftp_action = ftp_receive; |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 306 | } |
Denis Vlasenko | 562dc24 | 2007-01-03 21:55:50 +0000 | [diff] [blame] | 307 | #endif |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 308 | |
Denis Vlasenko | 0e7940a | 2008-03-29 07:37:42 +0000 | [diff] [blame] | 309 | INIT_G(); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 310 | /* Set default values */ |
Denis Vlasenko | 0e7940a | 2008-03-29 07:37:42 +0000 | [diff] [blame] | 311 | user = "anonymous"; |
Denys Vlasenko | 32c3e3a | 2018-04-07 13:22:52 +0200 | [diff] [blame] | 312 | password = "busybox"; |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 313 | |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 314 | /* |
| 315 | * Decipher the command line |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 316 | */ |
Denys Vlasenko | 22542ec | 2017-08-08 21:55:02 +0200 | [diff] [blame] | 317 | /* must have 2 to 3 params; -v and -c count */ |
| 318 | #define OPTSTRING "^cvu:p:P:" "\0" "-2:?3:vv:cc" |
Denys Vlasenko | 036585a | 2017-08-08 16:38:18 +0200 | [diff] [blame] | 319 | #if ENABLE_FEATURE_FTPGETPUT_LONG_OPTIONS |
Denys Vlasenko | 22542ec | 2017-08-08 21:55:02 +0200 | [diff] [blame] | 320 | getopt32long(argv, OPTSTRING, ftpgetput_longopts, |
Denys Vlasenko | 036585a | 2017-08-08 16:38:18 +0200 | [diff] [blame] | 321 | #else |
Denys Vlasenko | 22542ec | 2017-08-08 21:55:02 +0200 | [diff] [blame] | 322 | getopt32(argv, OPTSTRING, |
Denys Vlasenko | 036585a | 2017-08-08 16:38:18 +0200 | [diff] [blame] | 323 | #endif |
| 324 | &user, &password, &port, &verbose_flag, &do_continue |
| 325 | ); |
Denis Vlasenko | 562dc24 | 2007-01-03 21:55:50 +0000 | [diff] [blame] | 326 | argv += optind; |
Glenn L McGrath | 236e93d | 2003-12-20 05:43:34 +0000 | [diff] [blame] | 327 | |
Eric Andersen | 04d055f | 2003-11-03 21:20:18 +0000 | [diff] [blame] | 328 | /* We want to do exactly _one_ DNS lookup, since some |
| 329 | * sites (i.e. ftp.us.debian.org) use round-robin DNS |
| 330 | * and we want to connect to only one IP... */ |
Denis Vlasenko | 0e7940a | 2008-03-29 07:37:42 +0000 | [diff] [blame] | 331 | lsa = xhost2sockaddr(argv[0], bb_lookup_port(port, "tcp", 21)); |
Eric Andersen | 04d055f | 2003-11-03 21:20:18 +0000 | [diff] [blame] | 332 | if (verbose_flag) { |
Denis Vlasenko | 85629f0 | 2007-01-22 09:36:41 +0000 | [diff] [blame] | 333 | printf("Connecting to %s (%s)\n", argv[0], |
Denis Vlasenko | 0e7940a | 2008-03-29 07:37:42 +0000 | [diff] [blame] | 334 | xmalloc_sockaddr2dotted(&lsa->u.sa)); |
Eric Andersen | 04d055f | 2003-11-03 21:20:18 +0000 | [diff] [blame] | 335 | } |
| 336 | |
Denis Vlasenko | 7cb808e | 2008-03-29 07:40:35 +0000 | [diff] [blame] | 337 | ftp_login(); |
Vladimir Dronnikov | 1dacfbb | 2009-10-23 23:34:43 +0200 | [diff] [blame] | 338 | return ftp_action(argv[1], argv[2] ? argv[2] : argv[1]); |
Glenn L McGrath | 02d7cbf | 2002-12-13 02:43:50 +0000 | [diff] [blame] | 339 | } |