Bernhard Reutner-Fischer | dac7ff1 | 2006-04-12 17:55:51 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* ------------------------------------------------------------------------- |
| 3 | * tftp.c |
| 4 | * |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 5 | * A simple tftp client/server for busybox. |
Bernhard Reutner-Fischer | dac7ff1 | 2006-04-12 17:55:51 +0000 | [diff] [blame] | 6 | * Tries to follow RFC1350. |
| 7 | * Only "octet" mode supported. |
| 8 | * Optional blocksize negotiation (RFC2347 + RFC2348) |
| 9 | * |
| 10 | * Copyright (C) 2001 Magnus Damm <damm@opensource.se> |
| 11 | * |
| 12 | * Parts of the code based on: |
| 13 | * |
| 14 | * atftp: Copyright (C) 2000 Jean-Pierre Lefebvre <helix@step.polymtl.ca> |
| 15 | * and Remi Lefebvre <remi@debian.org> |
| 16 | * |
| 17 | * utftp: Copyright (C) 1999 Uwe Ohse <uwe@ohse.de> |
| 18 | * |
Denis Vlasenko | 78c5656 | 2008-03-18 00:11:46 +0000 | [diff] [blame] | 19 | * tftpd added by Denys Vlasenko & Vladimir Dronnikov |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 20 | * |
Bernhard Reutner-Fischer | dac7ff1 | 2006-04-12 17:55:51 +0000 | [diff] [blame] | 21 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
| 22 | * ------------------------------------------------------------------------- */ |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 23 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 24 | #include "libbb.h" |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 25 | |
Denis Vlasenko | 3163555 | 2007-01-20 16:54:19 +0000 | [diff] [blame] | 26 | #if ENABLE_FEATURE_TFTP_GET || ENABLE_FEATURE_TFTP_PUT |
| 27 | |
Denis Vlasenko | 4824cca | 2008-03-21 18:29:01 +0000 | [diff] [blame] | 28 | #define TFTP_BLKSIZE_DEFAULT 512 /* according to RFC 1350, don't change */ |
| 29 | #define TFTP_BLKSIZE_DEFAULT_STR "512" |
| 30 | #define TFTP_TIMEOUT_MS 50 |
| 31 | #define TFTP_MAXTIMEOUT_MS 2000 |
| 32 | #define TFTP_NUM_RETRIES 12 /* number of backed-off retries */ |
Bernhard Reutner-Fischer | b25f98a | 2006-06-10 14:15:03 +0000 | [diff] [blame] | 33 | |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 34 | /* opcodes we support */ |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 35 | #define TFTP_RRQ 1 |
| 36 | #define TFTP_WRQ 2 |
| 37 | #define TFTP_DATA 3 |
| 38 | #define TFTP_ACK 4 |
| 39 | #define TFTP_ERROR 5 |
| 40 | #define TFTP_OACK 6 |
| 41 | |
Denis Vlasenko | 71c9f01 | 2008-03-19 09:43:50 +0000 | [diff] [blame] | 42 | /* error codes sent over network (we use only 0, 3 and 8) */ |
| 43 | /* generic (error message is included in the packet) */ |
Denis Vlasenko | dd9228b | 2008-03-19 05:00:05 +0000 | [diff] [blame] | 44 | #define ERR_UNSPEC 0 |
| 45 | #define ERR_NOFILE 1 |
| 46 | #define ERR_ACCESS 2 |
Denis Vlasenko | 71c9f01 | 2008-03-19 09:43:50 +0000 | [diff] [blame] | 47 | /* disk full or allocation exceeded */ |
Denis Vlasenko | dd9228b | 2008-03-19 05:00:05 +0000 | [diff] [blame] | 48 | #define ERR_WRITE 3 |
| 49 | #define ERR_OP 4 |
| 50 | #define ERR_BAD_ID 5 |
| 51 | #define ERR_EXIST 6 |
| 52 | #define ERR_BAD_USER 7 |
| 53 | #define ERR_BAD_OPT 8 |
| 54 | |
Denis Vlasenko | 403a5a2 | 2008-03-19 13:07:00 +0000 | [diff] [blame] | 55 | /* masks coming from getopt32 */ |
| 56 | enum { |
| 57 | TFTP_OPT_GET = (1 << 0), |
| 58 | TFTP_OPT_PUT = (1 << 1), |
| 59 | /* pseudo option: if set, it's tftpd */ |
| 60 | TFTPD_OPT = (1 << 7) * ENABLE_TFTPD, |
| 61 | TFTPD_OPT_r = (1 << 8) * ENABLE_TFTPD, |
| 62 | TFTPD_OPT_c = (1 << 9) * ENABLE_TFTPD, |
| 63 | TFTPD_OPT_u = (1 << 10) * ENABLE_TFTPD, |
| 64 | }; |
| 65 | |
Denis Vlasenko | 8e9ccba | 2007-01-11 16:50:23 +0000 | [diff] [blame] | 66 | #if ENABLE_FEATURE_TFTP_GET && !ENABLE_FEATURE_TFTP_PUT |
Denis Vlasenko | bf678d5 | 2007-05-09 12:50:08 +0000 | [diff] [blame] | 67 | #define USE_GETPUT(...) |
Denis Vlasenko | 8e9ccba | 2007-01-11 16:50:23 +0000 | [diff] [blame] | 68 | #define CMD_GET(cmd) 1 |
| 69 | #define CMD_PUT(cmd) 0 |
| 70 | #elif !ENABLE_FEATURE_TFTP_GET && ENABLE_FEATURE_TFTP_PUT |
Denis Vlasenko | bf678d5 | 2007-05-09 12:50:08 +0000 | [diff] [blame] | 71 | #define USE_GETPUT(...) |
Denis Vlasenko | 8e9ccba | 2007-01-11 16:50:23 +0000 | [diff] [blame] | 72 | #define CMD_GET(cmd) 0 |
| 73 | #define CMD_PUT(cmd) 1 |
"Vladimir N. Oleynik" | 86ac072 | 2005-10-17 10:47:19 +0000 | [diff] [blame] | 74 | #else |
Denis Vlasenko | bf678d5 | 2007-05-09 12:50:08 +0000 | [diff] [blame] | 75 | #define USE_GETPUT(...) __VA_ARGS__ |
Denis Vlasenko | 403a5a2 | 2008-03-19 13:07:00 +0000 | [diff] [blame] | 76 | #define CMD_GET(cmd) ((cmd) & TFTP_OPT_GET) |
| 77 | #define CMD_PUT(cmd) ((cmd) & TFTP_OPT_PUT) |
"Vladimir N. Oleynik" | 86ac072 | 2005-10-17 10:47:19 +0000 | [diff] [blame] | 78 | #endif |
Denis Vlasenko | 8e9ccba | 2007-01-11 16:50:23 +0000 | [diff] [blame] | 79 | /* NB: in the code below |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 80 | * CMD_GET(cmd) and CMD_PUT(cmd) are mutually exclusive |
Denis Vlasenko | 8e9ccba | 2007-01-11 16:50:23 +0000 | [diff] [blame] | 81 | */ |
"Vladimir N. Oleynik" | 86ac072 | 2005-10-17 10:47:19 +0000 | [diff] [blame] | 82 | |
Denis Vlasenko | dd9228b | 2008-03-19 05:00:05 +0000 | [diff] [blame] | 83 | |
| 84 | struct globals { |
| 85 | /* u16 TFTP_ERROR; u16 reason; both network-endian, then error text: */ |
| 86 | uint8_t error_pkt[4 + 32]; |
Denis Vlasenko | 403a5a2 | 2008-03-19 13:07:00 +0000 | [diff] [blame] | 87 | char *user_opt; |
Denis Vlasenko | 71c9f01 | 2008-03-19 09:43:50 +0000 | [diff] [blame] | 88 | /* used in tftpd_main(), a bit big for stack: */ |
Denis Vlasenko | dd9228b | 2008-03-19 05:00:05 +0000 | [diff] [blame] | 89 | char block_buf[TFTP_BLKSIZE_DEFAULT]; |
| 90 | }; |
| 91 | #define G (*(struct globals*)&bb_common_bufsiz1) |
| 92 | #define block_buf (G.block_buf ) |
Denis Vlasenko | 403a5a2 | 2008-03-19 13:07:00 +0000 | [diff] [blame] | 93 | #define user_opt (G.user_opt ) |
Denis Vlasenko | dd9228b | 2008-03-19 05:00:05 +0000 | [diff] [blame] | 94 | #define error_pkt (G.error_pkt ) |
| 95 | #define INIT_G() \ |
| 96 | do { \ |
| 97 | } while (0) |
| 98 | |
| 99 | #define error_pkt_reason (error_pkt[3]) |
| 100 | #define error_pkt_str (error_pkt + 4) |
Denis Vlasenko | d7e6af2 | 2008-03-18 01:13:11 +0000 | [diff] [blame] | 101 | |
Eric Andersen | 76fa8ea | 2001-08-20 17:47:49 +0000 | [diff] [blame] | 102 | |
Denis Vlasenko | 04291bc | 2006-11-21 10:15:25 +0000 | [diff] [blame] | 103 | #if ENABLE_FEATURE_TFTP_BLOCKSIZE |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 104 | |
Denis Vlasenko | dd9228b | 2008-03-19 05:00:05 +0000 | [diff] [blame] | 105 | static int tftp_blksize_check(const char *blksize_str, int maxsize) |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 106 | { |
Denis Vlasenko | dd9228b | 2008-03-19 05:00:05 +0000 | [diff] [blame] | 107 | /* Check if the blksize is valid: |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 108 | * RFC2348 says between 8 and 65464, |
| 109 | * but our implementation makes it impossible |
Denis Vlasenko | dd9228b | 2008-03-19 05:00:05 +0000 | [diff] [blame] | 110 | * to use blksizes smaller than 22 octets. */ |
| 111 | unsigned blksize = bb_strtou(blksize_str, NULL, 10); |
| 112 | if (errno |
| 113 | || (blksize < 24) || (blksize > maxsize) |
Denis Vlasenko | 8e9ccba | 2007-01-11 16:50:23 +0000 | [diff] [blame] | 114 | ) { |
Denis Vlasenko | dd9228b | 2008-03-19 05:00:05 +0000 | [diff] [blame] | 115 | bb_error_msg("bad blocksize '%s'", blksize_str); |
| 116 | return -1; |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 117 | } |
Denis Vlasenko | dd9228b | 2008-03-19 05:00:05 +0000 | [diff] [blame] | 118 | #if ENABLE_DEBUG_TFTP |
| 119 | bb_error_msg("using blksize %u", blksize); |
| 120 | #endif |
| 121 | return blksize; |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 122 | } |
| 123 | |
Denis Vlasenko | dd9228b | 2008-03-19 05:00:05 +0000 | [diff] [blame] | 124 | static char *tftp_get_blksize(char *buf, int len) |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 125 | { |
Denis Vlasenko | dd9228b | 2008-03-19 05:00:05 +0000 | [diff] [blame] | 126 | #define option "blksize" |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 127 | int opt_val = 0; |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 128 | int opt_found = 0; |
| 129 | int k; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 130 | |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 131 | /* buf points to: |
| 132 | * "opt_name<NUL>opt_val<NUL>opt_name2<NUL>opt_val2<NUL>..." */ |
| 133 | |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 134 | while (len > 0) { |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 135 | /* Make sure options are terminated correctly */ |
Bernhard Reutner-Fischer | b25f98a | 2006-06-10 14:15:03 +0000 | [diff] [blame] | 136 | for (k = 0; k < len; k++) { |
| 137 | if (buf[k] == '\0') { |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 138 | goto nul_found; |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 139 | } |
| 140 | } |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 141 | return NULL; |
| 142 | nul_found: |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 143 | if (opt_val == 0) { /* it's "name" part */ |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 144 | if (strcasecmp(buf, option) == 0) { |
Bernhard Reutner-Fischer | b25f98a | 2006-06-10 14:15:03 +0000 | [diff] [blame] | 145 | opt_found = 1; |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 146 | } |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 147 | } else if (opt_found) { |
| 148 | return buf; |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 149 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 150 | |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 151 | k++; |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 152 | buf += k; |
| 153 | len -= k; |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 154 | opt_val ^= 1; |
| 155 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 156 | |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 157 | return NULL; |
Denis Vlasenko | dd9228b | 2008-03-19 05:00:05 +0000 | [diff] [blame] | 158 | #undef option |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | #endif |
| 162 | |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 163 | static int tftp_protocol( |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 164 | len_and_sockaddr *our_lsa, |
Denis Vlasenko | 0850cda | 2007-02-07 23:20:32 +0000 | [diff] [blame] | 165 | len_and_sockaddr *peer_lsa, |
Denis Vlasenko | 403a5a2 | 2008-03-19 13:07:00 +0000 | [diff] [blame] | 166 | const char *local_file, |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 167 | USE_TFTP(const char *remote_file,) |
Denis Vlasenko | dd9228b | 2008-03-19 05:00:05 +0000 | [diff] [blame] | 168 | int blksize) |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 169 | { |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 170 | #if !ENABLE_TFTP |
| 171 | #define remote_file NULL |
| 172 | #endif |
Denis Vlasenko | 87f3b26 | 2007-09-07 13:43:28 +0000 | [diff] [blame] | 173 | struct pollfd pfd[1]; |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 174 | #define socket_fd (pfd[0].fd) |
Bernhard Reutner-Fischer | 62f9856 | 2006-06-10 14:32:56 +0000 | [diff] [blame] | 175 | int len; |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 176 | int send_len; |
| 177 | USE_FEATURE_TFTP_BLOCKSIZE(smallint want_option_ack = 0;) |
| 178 | smallint finished = 0; |
| 179 | uint16_t opcode; |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 180 | uint16_t block_nr; |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 181 | uint16_t recv_blk; |
Denis Vlasenko | 403a5a2 | 2008-03-19 13:07:00 +0000 | [diff] [blame] | 182 | int open_mode, local_fd; |
Denis Vlasenko | 87f3b26 | 2007-09-07 13:43:28 +0000 | [diff] [blame] | 183 | int retries, waittime_ms; |
Denis Vlasenko | dd9228b | 2008-03-19 05:00:05 +0000 | [diff] [blame] | 184 | int io_bufsize = blksize + 4; |
Denis Vlasenko | 4824cca | 2008-03-21 18:29:01 +0000 | [diff] [blame] | 185 | char *cp; |
Eric Andersen | 744a194 | 2001-11-10 11:16:39 +0000 | [diff] [blame] | 186 | /* Can't use RESERVE_CONFIG_BUFFER here since the allocation |
| 187 | * size varies meaning BUFFERS_GO_ON_STACK would fail */ |
Denis Vlasenko | 10f7dd1 | 2006-12-17 01:14:08 +0000 | [diff] [blame] | 188 | /* We must keep the transmit and receive buffers seperate */ |
| 189 | /* In case we rcv a garbage pkt and we need to rexmit the last pkt */ |
Denis Vlasenko | dd9228b | 2008-03-19 05:00:05 +0000 | [diff] [blame] | 190 | char *xbuf = xmalloc(io_bufsize); |
| 191 | char *rbuf = xmalloc(io_bufsize); |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 192 | |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 193 | socket_fd = xsocket(peer_lsa->u.sa.sa_family, SOCK_DGRAM, 0); |
| 194 | setsockopt_reuseaddr(socket_fd); |
Denis Vlasenko | 6536a9b | 2007-01-12 10:35:23 +0000 | [diff] [blame] | 195 | |
Denis Vlasenko | 78c5656 | 2008-03-18 00:11:46 +0000 | [diff] [blame] | 196 | block_nr = 1; |
Denis Vlasenko | 4824cca | 2008-03-21 18:29:01 +0000 | [diff] [blame] | 197 | cp = xbuf + 2; |
Denis Vlasenko | 7a60133 | 2008-03-19 13:24:13 +0000 | [diff] [blame] | 198 | |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 199 | if (!ENABLE_TFTP || our_lsa) { |
| 200 | /* tftpd */ |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 201 | |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 202 | /* Create a socket which is: |
| 203 | * 1. bound to IP:port peer sent 1st datagram to, |
| 204 | * 2. connected to peer's IP:port |
| 205 | * This way we will answer from the IP:port peer |
| 206 | * expects, will not get any other packets on |
| 207 | * the socket, and also plain read/write will work. */ |
| 208 | xbind(socket_fd, &our_lsa->u.sa, our_lsa->len); |
| 209 | xconnect(socket_fd, &peer_lsa->u.sa, peer_lsa->len); |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 210 | |
Denis Vlasenko | dd9228b | 2008-03-19 05:00:05 +0000 | [diff] [blame] | 211 | /* Is there an error already? Send pkt and bail out */ |
| 212 | if (error_pkt_reason || error_pkt_str[0]) |
| 213 | goto send_err_pkt; |
| 214 | |
Denis Vlasenko | 403a5a2 | 2008-03-19 13:07:00 +0000 | [diff] [blame] | 215 | if (CMD_GET(option_mask32)) { |
Denis Vlasenko | 78c5656 | 2008-03-18 00:11:46 +0000 | [diff] [blame] | 216 | /* it's upload - we must ACK 1st packet (with filename) |
| 217 | * as if it's "block 0" */ |
| 218 | block_nr = 0; |
| 219 | } |
| 220 | |
Denis Vlasenko | 7a60133 | 2008-03-19 13:24:13 +0000 | [diff] [blame] | 221 | if (user_opt) { |
| 222 | struct passwd *pw = getpwnam(user_opt); |
| 223 | if (!pw) |
| 224 | bb_error_msg_and_die("unknown user '%s'", user_opt); |
| 225 | change_identity(pw); /* initgroups, setgid, setuid */ |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | /* Open local file (must be after changing user) */ |
| 230 | if (CMD_PUT(option_mask32)) { |
| 231 | open_mode = O_RDONLY; |
| 232 | } else { |
| 233 | open_mode = O_WRONLY | O_TRUNC | O_CREAT; |
| 234 | #if ENABLE_TFTPD |
| 235 | if ((option_mask32 & (TFTPD_OPT+TFTPD_OPT_c)) == TFTPD_OPT) { |
| 236 | /* tftpd without -c */ |
| 237 | open_mode = O_WRONLY | O_TRUNC; |
| 238 | } |
| 239 | #endif |
| 240 | } |
| 241 | if (!(option_mask32 & TFTPD_OPT)) { |
| 242 | local_fd = CMD_GET(option_mask32) ? STDOUT_FILENO : STDIN_FILENO; |
| 243 | if (NOT_LONE_DASH(local_file)) |
| 244 | local_fd = xopen(local_file, open_mode); |
| 245 | } else { |
| 246 | local_fd = open_or_warn(local_file, open_mode); |
| 247 | if (local_fd < 0) { |
| 248 | /*error_pkt_reason = ERR_NOFILE/ERR_ACCESS?*/ |
Denis Vlasenko | 5599502 | 2008-05-18 22:28:26 +0000 | [diff] [blame] | 249 | strcpy((char*)error_pkt_str, "can't open file"); |
Denis Vlasenko | 7a60133 | 2008-03-19 13:24:13 +0000 | [diff] [blame] | 250 | goto send_err_pkt; |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | if (!ENABLE_TFTP || our_lsa) { |
| 255 | #if ENABLE_FEATURE_TFTP_BLOCKSIZE |
| 256 | if (blksize != TFTP_BLKSIZE_DEFAULT) { |
Denis Vlasenko | 4824cca | 2008-03-21 18:29:01 +0000 | [diff] [blame] | 257 | /* Create and send OACK packet. */ |
| 258 | /* For the download case, block_nr is still 1 - |
| 259 | * we expect 1st ACK from peer to be for (block_nr-1), |
| 260 | * that is, for "block 0" which is our OACK pkt */ |
| 261 | opcode = TFTP_OACK; |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 262 | goto add_blksize_opt; |
| 263 | } |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 264 | #endif |
Denis Vlasenko | 4824cca | 2008-03-21 18:29:01 +0000 | [diff] [blame] | 265 | } |
| 266 | else { |
| 267 | /* Removing it, or using if() statement instead may lead to |
| 268 | * "warning: null argument where non-null required": */ |
| 269 | #if ENABLE_TFTP |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 270 | /* tftp */ |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 271 | |
| 272 | /* We can't (and don't really need to) bind the socket: |
| 273 | * we don't know from which local IP datagrams will be sent, |
| 274 | * but kernel will pick the same IP every time (unless routing |
| 275 | * table is changed), thus peer will see dgrams consistently |
| 276 | * coming from the same IP. |
| 277 | * We would like to connect the socket, but since peer's |
| 278 | * UDP code can be less perfect than ours, _peer's_ IP:port |
| 279 | * in replies may differ from IP:port we used to send |
| 280 | * our first packet. We can connect() only when we get |
| 281 | * first reply. */ |
| 282 | |
| 283 | /* build opcode */ |
| 284 | opcode = TFTP_WRQ; |
Denis Vlasenko | 403a5a2 | 2008-03-19 13:07:00 +0000 | [diff] [blame] | 285 | if (CMD_GET(option_mask32)) { |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 286 | opcode = TFTP_RRQ; |
| 287 | } |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 288 | /* add filename and mode */ |
| 289 | /* fill in packet if the filename fits into xbuf */ |
| 290 | len = strlen(remote_file) + 1; |
Denis Vlasenko | dd9228b | 2008-03-19 05:00:05 +0000 | [diff] [blame] | 291 | if (2 + len + sizeof("octet") >= io_bufsize) { |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 292 | bb_error_msg("remote filename is too long"); |
| 293 | goto ret; |
| 294 | } |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 295 | strcpy(cp, remote_file); |
| 296 | cp += len; |
| 297 | /* add "mode" part of the package */ |
| 298 | strcpy(cp, "octet"); |
| 299 | cp += sizeof("octet"); |
| 300 | |
| 301 | #if ENABLE_FEATURE_TFTP_BLOCKSIZE |
Denis Vlasenko | 4824cca | 2008-03-21 18:29:01 +0000 | [diff] [blame] | 302 | if (blksize == TFTP_BLKSIZE_DEFAULT) |
| 303 | goto send_pkt; |
| 304 | |
| 305 | /* Non-standard blocksize: add option to pkt */ |
| 306 | if ((&xbuf[io_bufsize - 1] - cp) < sizeof("blksize NNNNN")) { |
| 307 | bb_error_msg("remote filename is too long"); |
| 308 | goto ret; |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 309 | } |
Denis Vlasenko | 4824cca | 2008-03-21 18:29:01 +0000 | [diff] [blame] | 310 | want_option_ack = 1; |
| 311 | #endif |
| 312 | #endif /* ENABLE_TFTP */ |
| 313 | |
| 314 | #if ENABLE_FEATURE_TFTP_BLOCKSIZE |
| 315 | add_blksize_opt: |
| 316 | /* add "blksize", <nul>, blksize, <nul> */ |
| 317 | strcpy(cp, "blksize"); |
| 318 | cp += sizeof("blksize"); |
| 319 | cp += snprintf(cp, 6, "%d", blksize) + 1; |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 320 | #endif |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 321 | /* First packet is built, so skip packet generation */ |
| 322 | goto send_pkt; |
| 323 | } |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 324 | |
Denis Vlasenko | bf678d5 | 2007-05-09 12:50:08 +0000 | [diff] [blame] | 325 | /* Using mostly goto's - continue/break will be less clear |
| 326 | * in where we actually jump to */ |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 327 | while (1) { |
| 328 | /* Build ACK or DATA */ |
| 329 | cp = xbuf + 2; |
| 330 | *((uint16_t*)cp) = htons(block_nr); |
| 331 | cp += 2; |
| 332 | block_nr++; |
| 333 | opcode = TFTP_ACK; |
Denis Vlasenko | 403a5a2 | 2008-03-19 13:07:00 +0000 | [diff] [blame] | 334 | if (CMD_PUT(option_mask32)) { |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 335 | opcode = TFTP_DATA; |
Denis Vlasenko | dd9228b | 2008-03-19 05:00:05 +0000 | [diff] [blame] | 336 | len = full_read(local_fd, cp, blksize); |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 337 | if (len < 0) { |
Denis Vlasenko | dd9228b | 2008-03-19 05:00:05 +0000 | [diff] [blame] | 338 | goto send_read_err_pkt; |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 339 | } |
Denis Vlasenko | dd9228b | 2008-03-19 05:00:05 +0000 | [diff] [blame] | 340 | if (len != blksize) { |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 341 | finished = 1; |
| 342 | } |
| 343 | cp += len; |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 344 | } |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 345 | send_pkt: |
| 346 | /* Send packet */ |
| 347 | *((uint16_t*)xbuf) = htons(opcode); /* fill in opcode part */ |
Denis Vlasenko | bf678d5 | 2007-05-09 12:50:08 +0000 | [diff] [blame] | 348 | send_len = cp - xbuf; |
| 349 | /* NB: send_len value is preserved in code below |
| 350 | * for potential resend */ |
Paul Fox | 40f0bcf | 2007-09-06 17:52:22 +0000 | [diff] [blame] | 351 | |
| 352 | retries = TFTP_NUM_RETRIES; /* re-initialize */ |
Denis Vlasenko | 87f3b26 | 2007-09-07 13:43:28 +0000 | [diff] [blame] | 353 | waittime_ms = TFTP_TIMEOUT_MS; |
Paul Fox | 40f0bcf | 2007-09-06 17:52:22 +0000 | [diff] [blame] | 354 | |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 355 | send_again: |
Denis Vlasenko | 04291bc | 2006-11-21 10:15:25 +0000 | [diff] [blame] | 356 | #if ENABLE_DEBUG_TFTP |
Denis Vlasenko | bf678d5 | 2007-05-09 12:50:08 +0000 | [diff] [blame] | 357 | fprintf(stderr, "sending %u bytes\n", send_len); |
| 358 | for (cp = xbuf; cp < &xbuf[send_len]; cp++) |
| 359 | fprintf(stderr, "%02x ", (unsigned char) *cp); |
| 360 | fprintf(stderr, "\n"); |
Eric Andersen | 76fa8ea | 2001-08-20 17:47:49 +0000 | [diff] [blame] | 361 | #endif |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 362 | xsendto(socket_fd, xbuf, send_len, &peer_lsa->u.sa, peer_lsa->len); |
Denis Vlasenko | bf678d5 | 2007-05-09 12:50:08 +0000 | [diff] [blame] | 363 | /* Was it final ACK? then exit */ |
| 364 | if (finished && (opcode == TFTP_ACK)) |
| 365 | goto ret; |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 366 | |
Denis Vlasenko | 2c91652 | 2007-01-12 14:57:37 +0000 | [diff] [blame] | 367 | recv_again: |
Denis Vlasenko | bf678d5 | 2007-05-09 12:50:08 +0000 | [diff] [blame] | 368 | /* Receive packet */ |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 369 | /*pfd[0].fd = socket_fd;*/ |
Denis Vlasenko | 87f3b26 | 2007-09-07 13:43:28 +0000 | [diff] [blame] | 370 | pfd[0].events = POLLIN; |
Denis Vlasenko | 5d61e71 | 2007-09-27 10:09:59 +0000 | [diff] [blame] | 371 | switch (safe_poll(pfd, 1, waittime_ms)) { |
Denis Vlasenko | dd9228b | 2008-03-19 05:00:05 +0000 | [diff] [blame] | 372 | default: |
| 373 | /*bb_perror_msg("poll"); - done in safe_poll */ |
| 374 | goto ret; |
Denis Vlasenko | bf678d5 | 2007-05-09 12:50:08 +0000 | [diff] [blame] | 375 | case 0: |
Paul Fox | 40f0bcf | 2007-09-06 17:52:22 +0000 | [diff] [blame] | 376 | retries--; |
| 377 | if (retries == 0) { |
| 378 | bb_error_msg("timeout"); |
Denis Vlasenko | dd9228b | 2008-03-19 05:00:05 +0000 | [diff] [blame] | 379 | goto ret; /* no err packet sent */ |
Denis Vlasenko | bf678d5 | 2007-05-09 12:50:08 +0000 | [diff] [blame] | 380 | } |
Paul Fox | 40f0bcf | 2007-09-06 17:52:22 +0000 | [diff] [blame] | 381 | |
| 382 | /* exponential backoff with limit */ |
Denis Vlasenko | 87f3b26 | 2007-09-07 13:43:28 +0000 | [diff] [blame] | 383 | waittime_ms += waittime_ms/2; |
| 384 | if (waittime_ms > TFTP_MAXTIMEOUT_MS) { |
| 385 | waittime_ms = TFTP_MAXTIMEOUT_MS; |
Paul Fox | 40f0bcf | 2007-09-06 17:52:22 +0000 | [diff] [blame] | 386 | } |
| 387 | |
Denis Vlasenko | bf678d5 | 2007-05-09 12:50:08 +0000 | [diff] [blame] | 388 | goto send_again; /* resend last sent pkt */ |
Denis Vlasenko | dd9228b | 2008-03-19 05:00:05 +0000 | [diff] [blame] | 389 | case 1: |
| 390 | if (!our_lsa) { |
| 391 | /* tftp (not tftpd!) receiving 1st packet */ |
| 392 | our_lsa = ((void*)(ptrdiff_t)-1); /* not NULL */ |
| 393 | len = recvfrom(socket_fd, rbuf, io_bufsize, 0, |
| 394 | &peer_lsa->u.sa, &peer_lsa->len); |
| 395 | /* Our first dgram went to port 69 |
| 396 | * but reply may come from different one. |
| 397 | * Remember and use this new port (and IP) */ |
| 398 | if (len >= 0) |
| 399 | xconnect(socket_fd, &peer_lsa->u.sa, peer_lsa->len); |
| 400 | } else { |
| 401 | /* tftpd, or not the very first packet: |
| 402 | * socket is connect()ed, can just read from it. */ |
| 403 | /* Don't full_read()! |
| 404 | * This is not TCP, one read == one pkt! */ |
| 405 | len = safe_read(socket_fd, rbuf, io_bufsize); |
| 406 | } |
| 407 | if (len < 0) { |
| 408 | goto send_read_err_pkt; |
| 409 | } |
| 410 | if (len < 4) { /* too small? */ |
| 411 | goto recv_again; |
| 412 | } |
Denis Vlasenko | bf678d5 | 2007-05-09 12:50:08 +0000 | [diff] [blame] | 413 | } |
Denis Vlasenko | dd9228b | 2008-03-19 05:00:05 +0000 | [diff] [blame] | 414 | |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 415 | /* Process recv'ed packet */ |
Denis Vlasenko | 8e9ccba | 2007-01-11 16:50:23 +0000 | [diff] [blame] | 416 | opcode = ntohs( ((uint16_t*)rbuf)[0] ); |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 417 | recv_blk = ntohs( ((uint16_t*)rbuf)[1] ); |
Denis Vlasenko | 04291bc | 2006-11-21 10:15:25 +0000 | [diff] [blame] | 418 | #if ENABLE_DEBUG_TFTP |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 419 | fprintf(stderr, "received %d bytes: %04x %04x\n", len, opcode, recv_blk); |
Eric Andersen | 76fa8ea | 2001-08-20 17:47:49 +0000 | [diff] [blame] | 420 | #endif |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 421 | |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 422 | if (opcode == TFTP_ERROR) { |
Denis Vlasenko | dd9228b | 2008-03-19 05:00:05 +0000 | [diff] [blame] | 423 | static const char errcode_str[] = |
| 424 | "\0" |
| 425 | "file not found\0" |
| 426 | "access violation\0" |
| 427 | "disk full\0" |
| 428 | "bad operation\0" |
| 429 | "unknown transfer id\0" |
| 430 | "file already exists\0" |
| 431 | "no such user\0" |
| 432 | "bad option"; |
Denis Vlasenko | 80b8b39 | 2007-06-25 10:55:35 +0000 | [diff] [blame] | 433 | |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 434 | const char *msg = ""; |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 435 | |
Denis Vlasenko | d7e6af2 | 2008-03-18 01:13:11 +0000 | [diff] [blame] | 436 | if (len > 4 && rbuf[4] != '\0') { |
Denis Vlasenko | 10f7dd1 | 2006-12-17 01:14:08 +0000 | [diff] [blame] | 437 | msg = &rbuf[4]; |
Denis Vlasenko | dd9228b | 2008-03-19 05:00:05 +0000 | [diff] [blame] | 438 | rbuf[io_bufsize - 1] = '\0'; /* paranoia */ |
| 439 | } else if (recv_blk <= 8) { |
| 440 | msg = nth_string(errcode_str, recv_blk); |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 441 | } |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 442 | bb_error_msg("server error: (%u) %s", recv_blk, msg); |
| 443 | goto ret; |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 444 | } |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 445 | |
Denis Vlasenko | 04291bc | 2006-11-21 10:15:25 +0000 | [diff] [blame] | 446 | #if ENABLE_FEATURE_TFTP_BLOCKSIZE |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 447 | if (want_option_ack) { |
Bernhard Reutner-Fischer | b25f98a | 2006-06-10 14:15:03 +0000 | [diff] [blame] | 448 | want_option_ack = 0; |
Bernhard Reutner-Fischer | b25f98a | 2006-06-10 14:15:03 +0000 | [diff] [blame] | 449 | if (opcode == TFTP_OACK) { |
Bernhard Reutner-Fischer | b25f98a | 2006-06-10 14:15:03 +0000 | [diff] [blame] | 450 | /* server seems to support options */ |
Bernhard Reutner-Fischer | b25f98a | 2006-06-10 14:15:03 +0000 | [diff] [blame] | 451 | char *res; |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 452 | |
Denis Vlasenko | dd9228b | 2008-03-19 05:00:05 +0000 | [diff] [blame] | 453 | res = tftp_get_blksize(&rbuf[2], len - 2); |
Bernhard Reutner-Fischer | b25f98a | 2006-06-10 14:15:03 +0000 | [diff] [blame] | 454 | if (res) { |
Denis Vlasenko | dd9228b | 2008-03-19 05:00:05 +0000 | [diff] [blame] | 455 | blksize = tftp_blksize_check(res, blksize); |
| 456 | if (blksize < 0) { |
| 457 | error_pkt_reason = ERR_BAD_OPT; |
| 458 | goto send_err_pkt; |
Bernhard Reutner-Fischer | b25f98a | 2006-06-10 14:15:03 +0000 | [diff] [blame] | 459 | } |
Denis Vlasenko | dd9228b | 2008-03-19 05:00:05 +0000 | [diff] [blame] | 460 | io_bufsize = blksize + 4; |
Denis Vlasenko | bf678d5 | 2007-05-09 12:50:08 +0000 | [diff] [blame] | 461 | /* Send ACK for OACK ("block" no: 0) */ |
| 462 | block_nr = 0; |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 463 | continue; |
Bernhard Reutner-Fischer | b25f98a | 2006-06-10 14:15:03 +0000 | [diff] [blame] | 464 | } |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 465 | /* rfc2347: |
| 466 | * "An option not acknowledged by the server |
| 467 | * must be ignored by the client and server |
| 468 | * as if it were never requested." */ |
Bernhard Reutner-Fischer | b25f98a | 2006-06-10 14:15:03 +0000 | [diff] [blame] | 469 | } |
Denis Vlasenko | dd9228b | 2008-03-19 05:00:05 +0000 | [diff] [blame] | 470 | bb_error_msg("server only supports blocksize of 512"); |
| 471 | blksize = TFTP_BLKSIZE_DEFAULT; |
| 472 | io_bufsize = TFTP_BLKSIZE_DEFAULT + 4; |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 473 | } |
| 474 | #endif |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 475 | /* block_nr is already advanced to next block# we expect |
| 476 | * to get / block# we are about to send next time */ |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 477 | |
Denis Vlasenko | 403a5a2 | 2008-03-19 13:07:00 +0000 | [diff] [blame] | 478 | if (CMD_GET(option_mask32) && (opcode == TFTP_DATA)) { |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 479 | if (recv_blk == block_nr) { |
Denis Vlasenko | 71c9f01 | 2008-03-19 09:43:50 +0000 | [diff] [blame] | 480 | int sz = full_write(local_fd, &rbuf[4], len - 4); |
| 481 | if (sz != len - 4) { |
Denis Vlasenko | 5599502 | 2008-05-18 22:28:26 +0000 | [diff] [blame] | 482 | strcpy((char*)error_pkt_str, bb_msg_write_error); |
Denis Vlasenko | dd9228b | 2008-03-19 05:00:05 +0000 | [diff] [blame] | 483 | error_pkt_reason = ERR_WRITE; |
| 484 | goto send_err_pkt; |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 485 | } |
Denis Vlasenko | 71c9f01 | 2008-03-19 09:43:50 +0000 | [diff] [blame] | 486 | if (sz != blksize) { |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 487 | finished = 1; |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 488 | } |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 489 | continue; /* send ACK */ |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 490 | } |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 491 | if (recv_blk == (block_nr - 1)) { |
Paul Fox | 1d4c88c | 2005-07-20 19:49:15 +0000 | [diff] [blame] | 492 | /* Server lost our TFTP_ACK. Resend it */ |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 493 | block_nr = recv_blk; |
Paul Fox | 1d4c88c | 2005-07-20 19:49:15 +0000 | [diff] [blame] | 494 | continue; |
Denis Vlasenko | 4b924f3 | 2007-05-30 00:29:55 +0000 | [diff] [blame] | 495 | } |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 496 | } |
| 497 | |
Denis Vlasenko | 403a5a2 | 2008-03-19 13:07:00 +0000 | [diff] [blame] | 498 | if (CMD_PUT(option_mask32) && (opcode == TFTP_ACK)) { |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 499 | /* did peer ACK our last DATA pkt? */ |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 500 | if (recv_blk == (uint16_t) (block_nr - 1)) { |
| 501 | if (finished) |
| 502 | goto ret; |
| 503 | continue; /* send next block */ |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 504 | } |
| 505 | } |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 506 | /* Awww... recv'd packet is not recognized! */ |
| 507 | goto recv_again; |
| 508 | /* why recv_again? - rfc1123 says: |
| 509 | * "The sender (i.e., the side originating the DATA packets) |
| 510 | * must never resend the current DATA packet on receipt |
| 511 | * of a duplicate ACK". |
| 512 | * DATA pkts are resent ONLY on timeout. |
| 513 | * Thus "goto send_again" will ba a bad mistake above. |
| 514 | * See: |
| 515 | * http://en.wikipedia.org/wiki/Sorcerer's_Apprentice_Syndrome |
| 516 | */ |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 517 | } /* end of "while (1)" */ |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 518 | ret: |
Denis Vlasenko | 2c91652 | 2007-01-12 14:57:37 +0000 | [diff] [blame] | 519 | if (ENABLE_FEATURE_CLEAN_UP) { |
Denis Vlasenko | 403a5a2 | 2008-03-19 13:07:00 +0000 | [diff] [blame] | 520 | close(local_fd); |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 521 | close(socket_fd); |
Denis Vlasenko | 2c91652 | 2007-01-12 14:57:37 +0000 | [diff] [blame] | 522 | free(xbuf); |
| 523 | free(rbuf); |
| 524 | } |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 525 | return finished == 0; /* returns 1 on failure */ |
Denis Vlasenko | dd9228b | 2008-03-19 05:00:05 +0000 | [diff] [blame] | 526 | |
| 527 | send_read_err_pkt: |
Denis Vlasenko | 5599502 | 2008-05-18 22:28:26 +0000 | [diff] [blame] | 528 | strcpy((char*)error_pkt_str, bb_msg_read_error); |
Denis Vlasenko | dd9228b | 2008-03-19 05:00:05 +0000 | [diff] [blame] | 529 | send_err_pkt: |
Denis Vlasenko | 71c9f01 | 2008-03-19 09:43:50 +0000 | [diff] [blame] | 530 | if (error_pkt_str[0]) |
Denis Vlasenko | 5599502 | 2008-05-18 22:28:26 +0000 | [diff] [blame] | 531 | bb_error_msg((char*)error_pkt_str); |
Denis Vlasenko | dd9228b | 2008-03-19 05:00:05 +0000 | [diff] [blame] | 532 | error_pkt[1] = TFTP_ERROR; |
Denis Vlasenko | 5599502 | 2008-05-18 22:28:26 +0000 | [diff] [blame] | 533 | xsendto(socket_fd, error_pkt, 4 + 1 + strlen((char*)error_pkt_str), |
Denis Vlasenko | dd9228b | 2008-03-19 05:00:05 +0000 | [diff] [blame] | 534 | &peer_lsa->u.sa, peer_lsa->len); |
| 535 | return EXIT_FAILURE; |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 536 | } |
| 537 | |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 538 | #if ENABLE_TFTP |
| 539 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 540 | int tftp_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 541 | int tftp_main(int argc ATTRIBUTE_UNUSED, char **argv) |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 542 | { |
Denis Vlasenko | 8e9ccba | 2007-01-11 16:50:23 +0000 | [diff] [blame] | 543 | len_and_sockaddr *peer_lsa; |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 544 | const char *local_file = NULL; |
| 545 | const char *remote_file = NULL; |
Denis Vlasenko | 4824cca | 2008-03-21 18:29:01 +0000 | [diff] [blame] | 546 | #if ENABLE_FEATURE_TFTP_BLOCKSIZE |
| 547 | const char *blksize_str = TFTP_BLKSIZE_DEFAULT_STR; |
| 548 | #endif |
Denis Vlasenko | 403a5a2 | 2008-03-19 13:07:00 +0000 | [diff] [blame] | 549 | int blksize; |
| 550 | int result; |
| 551 | int port; |
| 552 | USE_GETPUT(int opt;) |
Denis Vlasenko | dd9228b | 2008-03-19 05:00:05 +0000 | [diff] [blame] | 553 | |
| 554 | INIT_G(); |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 555 | |
Denis Vlasenko | 8e9ccba | 2007-01-11 16:50:23 +0000 | [diff] [blame] | 556 | /* -p or -g is mandatory, and they are mutually exclusive */ |
| 557 | opt_complementary = "" USE_FEATURE_TFTP_GET("g:") USE_FEATURE_TFTP_PUT("p:") |
Denis Vlasenko | dd9228b | 2008-03-19 05:00:05 +0000 | [diff] [blame] | 558 | USE_GETPUT("g--p:p--g:"); |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 559 | |
Denis Vlasenko | 403a5a2 | 2008-03-19 13:07:00 +0000 | [diff] [blame] | 560 | USE_GETPUT(opt =) getopt32(argv, |
Denis Vlasenko | 8e9ccba | 2007-01-11 16:50:23 +0000 | [diff] [blame] | 561 | USE_FEATURE_TFTP_GET("g") USE_FEATURE_TFTP_PUT("p") |
| 562 | "l:r:" USE_FEATURE_TFTP_BLOCKSIZE("b:"), |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 563 | &local_file, &remote_file |
Denis Vlasenko | dd9228b | 2008-03-19 05:00:05 +0000 | [diff] [blame] | 564 | USE_FEATURE_TFTP_BLOCKSIZE(, &blksize_str)); |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 565 | argv += optind; |
Bernhard Reutner-Fischer | b25f98a | 2006-06-10 14:15:03 +0000 | [diff] [blame] | 566 | |
Denis Vlasenko | 04291bc | 2006-11-21 10:15:25 +0000 | [diff] [blame] | 567 | #if ENABLE_FEATURE_TFTP_BLOCKSIZE |
Denis Vlasenko | dd9228b | 2008-03-19 05:00:05 +0000 | [diff] [blame] | 568 | /* Check if the blksize is valid: |
| 569 | * RFC2348 says between 8 and 65464 */ |
| 570 | blksize = tftp_blksize_check(blksize_str, 65564); |
| 571 | if (blksize < 0) { |
| 572 | //bb_error_msg("bad block size"); |
Denis Vlasenko | 1d42665 | 2008-03-17 09:09:09 +0000 | [diff] [blame] | 573 | return EXIT_FAILURE; |
Denis Vlasenko | dd9228b | 2008-03-19 05:00:05 +0000 | [diff] [blame] | 574 | } |
Denis Vlasenko | 4824cca | 2008-03-21 18:29:01 +0000 | [diff] [blame] | 575 | #else |
| 576 | blksize = TFTP_BLKSIZE_DEFAULT; |
"Vladimir N. Oleynik" | 86ac072 | 2005-10-17 10:47:19 +0000 | [diff] [blame] | 577 | #endif |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 578 | |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 579 | if (!local_file) |
| 580 | local_file = remote_file; |
| 581 | if (!remote_file) |
| 582 | remote_file = local_file; |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 583 | /* Error if filename or host is not known */ |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 584 | if (!remote_file || !argv[0]) |
Bernhard Reutner-Fischer | b25f98a | 2006-06-10 14:15:03 +0000 | [diff] [blame] | 585 | bb_show_usage(); |
"Vladimir N. Oleynik" | 86ac072 | 2005-10-17 10:47:19 +0000 | [diff] [blame] | 586 | |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 587 | port = bb_lookup_port(argv[1], "udp", 69); |
| 588 | peer_lsa = xhost2sockaddr(argv[0], port); |
Eric Andersen | 76fa8ea | 2001-08-20 17:47:49 +0000 | [diff] [blame] | 589 | |
Denis Vlasenko | 04291bc | 2006-11-21 10:15:25 +0000 | [diff] [blame] | 590 | #if ENABLE_DEBUG_TFTP |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 591 | fprintf(stderr, "using server '%s', remote_file '%s', local_file '%s'\n", |
Bernhard Reutner-Fischer | 8c69afd | 2008-01-29 10:33:34 +0000 | [diff] [blame] | 592 | xmalloc_sockaddr2dotted(&peer_lsa->u.sa), |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 593 | remote_file, local_file); |
Eric Andersen | 76fa8ea | 2001-08-20 17:47:49 +0000 | [diff] [blame] | 594 | #endif |
| 595 | |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 596 | result = tftp_protocol( |
Denis Vlasenko | 403a5a2 | 2008-03-19 13:07:00 +0000 | [diff] [blame] | 597 | NULL /* our_lsa*/, peer_lsa, |
| 598 | local_file, remote_file, |
| 599 | blksize); |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 600 | |
Denis Vlasenko | 403a5a2 | 2008-03-19 13:07:00 +0000 | [diff] [blame] | 601 | if (result != EXIT_SUCCESS && NOT_LONE_DASH(local_file) && CMD_GET(opt)) { |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 602 | unlink(local_file); |
Eric Andersen | a66a43e | 2002-04-13 09:30:25 +0000 | [diff] [blame] | 603 | } |
Denis Vlasenko | 000b9ba | 2006-10-05 23:12:49 +0000 | [diff] [blame] | 604 | return result; |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 605 | } |
Denis Vlasenko | 3163555 | 2007-01-20 16:54:19 +0000 | [diff] [blame] | 606 | |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 607 | #endif /* ENABLE_TFTP */ |
| 608 | |
| 609 | #if ENABLE_TFTPD |
| 610 | |
| 611 | /* TODO: libbb candidate? */ |
| 612 | static len_and_sockaddr *get_sock_lsa(int s) |
| 613 | { |
| 614 | len_and_sockaddr *lsa; |
| 615 | socklen_t len = 0; |
| 616 | |
| 617 | if (getsockname(s, NULL, &len) != 0) |
| 618 | return NULL; |
| 619 | lsa = xzalloc(LSA_LEN_SIZE + len); |
| 620 | lsa->len = len; |
| 621 | getsockname(s, &lsa->u.sa, &lsa->len); |
| 622 | return lsa; |
| 623 | } |
| 624 | |
| 625 | int tftpd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | d7e6af2 | 2008-03-18 01:13:11 +0000 | [diff] [blame] | 626 | int tftpd_main(int argc ATTRIBUTE_UNUSED, char **argv) |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 627 | { |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 628 | len_and_sockaddr *our_lsa; |
| 629 | len_and_sockaddr *peer_lsa; |
Denis Vlasenko | 403a5a2 | 2008-03-19 13:07:00 +0000 | [diff] [blame] | 630 | char *local_file, *mode; |
Denis Vlasenko | dd9228b | 2008-03-19 05:00:05 +0000 | [diff] [blame] | 631 | const char *error_msg; |
Denis Vlasenko | 403a5a2 | 2008-03-19 13:07:00 +0000 | [diff] [blame] | 632 | int opt, result, opcode; |
Denis Vlasenko | 0a0180c | 2008-03-19 23:37:32 +0000 | [diff] [blame] | 633 | int blksize = TFTP_BLKSIZE_DEFAULT; |
Denis Vlasenko | dd9228b | 2008-03-19 05:00:05 +0000 | [diff] [blame] | 634 | |
| 635 | INIT_G(); |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 636 | |
| 637 | our_lsa = get_sock_lsa(STDIN_FILENO); |
| 638 | if (!our_lsa) |
| 639 | bb_perror_msg_and_die("stdin is not a socket"); |
| 640 | peer_lsa = xzalloc(LSA_LEN_SIZE + our_lsa->len); |
| 641 | peer_lsa->len = our_lsa->len; |
| 642 | |
Denis Vlasenko | 403a5a2 | 2008-03-19 13:07:00 +0000 | [diff] [blame] | 643 | /* Shifting to not collide with TFTP_OPTs */ |
| 644 | opt = option_mask32 = TFTPD_OPT | (getopt32(argv, "rcu:", &user_opt) << 8); |
Denis Vlasenko | d7e6af2 | 2008-03-18 01:13:11 +0000 | [diff] [blame] | 645 | argv += optind; |
| 646 | if (argv[0]) |
| 647 | xchdir(argv[0]); |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 648 | |
| 649 | result = recv_from_to(STDIN_FILENO, block_buf, sizeof(block_buf), |
| 650 | 0 /* flags */, |
| 651 | &peer_lsa->u.sa, &our_lsa->u.sa, our_lsa->len); |
| 652 | |
Denis Vlasenko | dd9228b | 2008-03-19 05:00:05 +0000 | [diff] [blame] | 653 | error_msg = "malformed packet"; |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 654 | opcode = ntohs(*(uint16_t*)block_buf); |
| 655 | if (result < 4 || result >= sizeof(block_buf) |
| 656 | || block_buf[result-1] != '\0' |
Denis Vlasenko | 4824cca | 2008-03-21 18:29:01 +0000 | [diff] [blame] | 657 | || (USE_FEATURE_TFTP_PUT(opcode != TFTP_RRQ) /* not download */ |
Denis Vlasenko | d7e6af2 | 2008-03-18 01:13:11 +0000 | [diff] [blame] | 658 | USE_GETPUT(&&) |
| 659 | USE_FEATURE_TFTP_GET(opcode != TFTP_WRQ) /* not upload */ |
| 660 | ) |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 661 | ) { |
Denis Vlasenko | dd9228b | 2008-03-19 05:00:05 +0000 | [diff] [blame] | 662 | goto err; |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 663 | } |
Denis Vlasenko | 403a5a2 | 2008-03-19 13:07:00 +0000 | [diff] [blame] | 664 | local_file = block_buf + 2; |
| 665 | if (local_file[0] == '.' || strstr(local_file, "/.")) { |
Denis Vlasenko | 0a0180c | 2008-03-19 23:37:32 +0000 | [diff] [blame] | 666 | error_msg = "dot in file name"; |
Denis Vlasenko | dd9228b | 2008-03-19 05:00:05 +0000 | [diff] [blame] | 667 | goto err; |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 668 | } |
Denis Vlasenko | 403a5a2 | 2008-03-19 13:07:00 +0000 | [diff] [blame] | 669 | mode = local_file + strlen(local_file) + 1; |
| 670 | if (mode >= block_buf + result || strcmp(mode, "octet") != 0) { |
Denis Vlasenko | dd9228b | 2008-03-19 05:00:05 +0000 | [diff] [blame] | 671 | goto err; |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 672 | } |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 673 | #if ENABLE_FEATURE_TFTP_BLOCKSIZE |
Denis Vlasenko | dd9228b | 2008-03-19 05:00:05 +0000 | [diff] [blame] | 674 | { |
| 675 | char *res; |
| 676 | char *opt_str = mode + sizeof("octet"); |
| 677 | int opt_len = block_buf + result - opt_str; |
| 678 | if (opt_len > 0) { |
| 679 | res = tftp_get_blksize(opt_str, opt_len); |
| 680 | if (res) { |
| 681 | blksize = tftp_blksize_check(res, 65564); |
| 682 | if (blksize < 0) { |
| 683 | error_pkt_reason = ERR_BAD_OPT; |
| 684 | /* will just send error pkt */ |
| 685 | goto do_proto; |
| 686 | } |
| 687 | } |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 688 | } |
| 689 | } |
| 690 | #endif |
Denis Vlasenko | d7e6af2 | 2008-03-18 01:13:11 +0000 | [diff] [blame] | 691 | |
Denis Vlasenko | d7e6af2 | 2008-03-18 01:13:11 +0000 | [diff] [blame] | 692 | if (!ENABLE_FEATURE_TFTP_PUT || opcode == TFTP_WRQ) { |
Denis Vlasenko | 403a5a2 | 2008-03-19 13:07:00 +0000 | [diff] [blame] | 693 | if (opt & TFTPD_OPT_r) { |
Denis Vlasenko | 71c9f01 | 2008-03-19 09:43:50 +0000 | [diff] [blame] | 694 | /* This would mean "disk full" - not true */ |
| 695 | /*error_pkt_reason = ERR_WRITE;*/ |
| 696 | error_msg = bb_msg_write_error; |
| 697 | goto err; |
Denis Vlasenko | dd9228b | 2008-03-19 05:00:05 +0000 | [diff] [blame] | 698 | } |
Denis Vlasenko | 403a5a2 | 2008-03-19 13:07:00 +0000 | [diff] [blame] | 699 | USE_GETPUT(option_mask32 |= TFTP_OPT_GET;) /* will receive file's data */ |
| 700 | } else { |
| 701 | USE_GETPUT(option_mask32 |= TFTP_OPT_PUT;) /* will send file's data */ |
Denis Vlasenko | dd9228b | 2008-03-19 05:00:05 +0000 | [diff] [blame] | 702 | } |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 703 | |
| 704 | close(STDIN_FILENO); /* close old, possibly wildcard socket */ |
| 705 | /* tftp_protocol() will create new one, bound to particular local IP */ |
Denis Vlasenko | dd9228b | 2008-03-19 05:00:05 +0000 | [diff] [blame] | 706 | |
| 707 | /* NB: if error_pkt_str or error_pkt_reason is set up, |
| 708 | * tftp_protocol() just sends one error pkt and returns */ |
| 709 | do_proto: |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 710 | result = tftp_protocol( |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 711 | our_lsa, peer_lsa, |
Denis Vlasenko | 403a5a2 | 2008-03-19 13:07:00 +0000 | [diff] [blame] | 712 | local_file, USE_TFTP(NULL /*remote_file*/,) |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 713 | blksize |
| 714 | ); |
| 715 | |
| 716 | return result; |
Denis Vlasenko | 403a5a2 | 2008-03-19 13:07:00 +0000 | [diff] [blame] | 717 | err: |
Denis Vlasenko | 5599502 | 2008-05-18 22:28:26 +0000 | [diff] [blame] | 718 | strcpy((char*)error_pkt_str, error_msg); |
Denis Vlasenko | 403a5a2 | 2008-03-19 13:07:00 +0000 | [diff] [blame] | 719 | goto do_proto; |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 720 | } |
| 721 | |
| 722 | #endif /* ENABLE_TFTPD */ |
| 723 | |
Denis Vlasenko | 3163555 | 2007-01-20 16:54:19 +0000 | [diff] [blame] | 724 | #endif /* ENABLE_FEATURE_TFTP_GET || ENABLE_FEATURE_TFTP_PUT */ |