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 | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 19 | * tftpd added by Denys Vlasenko |
| 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 | 87f3b26 | 2007-09-07 13:43:28 +0000 | [diff] [blame] | 28 | #define TFTP_BLOCKSIZE_DEFAULT 512 /* according to RFC 1350, don't change */ |
| 29 | #define TFTP_TIMEOUT_MS 50 |
| 30 | #define TFTP_MAXTIMEOUT_MS 2000 |
| 31 | #define TFTP_NUM_RETRIES 12 /* number of backed-off retries */ |
Bernhard Reutner-Fischer | b25f98a | 2006-06-10 14:15:03 +0000 | [diff] [blame] | 32 | |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 33 | /* opcodes we support */ |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 34 | #define TFTP_RRQ 1 |
| 35 | #define TFTP_WRQ 2 |
| 36 | #define TFTP_DATA 3 |
| 37 | #define TFTP_ACK 4 |
| 38 | #define TFTP_ERROR 5 |
| 39 | #define TFTP_OACK 6 |
| 40 | |
Denis Vlasenko | 8e9ccba | 2007-01-11 16:50:23 +0000 | [diff] [blame] | 41 | #if ENABLE_FEATURE_TFTP_GET && !ENABLE_FEATURE_TFTP_PUT |
Denis Vlasenko | bf678d5 | 2007-05-09 12:50:08 +0000 | [diff] [blame] | 42 | #define USE_GETPUT(...) |
Denis Vlasenko | 8e9ccba | 2007-01-11 16:50:23 +0000 | [diff] [blame] | 43 | #define CMD_GET(cmd) 1 |
| 44 | #define CMD_PUT(cmd) 0 |
| 45 | #elif !ENABLE_FEATURE_TFTP_GET && ENABLE_FEATURE_TFTP_PUT |
Denis Vlasenko | bf678d5 | 2007-05-09 12:50:08 +0000 | [diff] [blame] | 46 | #define USE_GETPUT(...) |
Denis Vlasenko | 8e9ccba | 2007-01-11 16:50:23 +0000 | [diff] [blame] | 47 | #define CMD_GET(cmd) 0 |
| 48 | #define CMD_PUT(cmd) 1 |
"Vladimir N. Oleynik" | 86ac072 | 2005-10-17 10:47:19 +0000 | [diff] [blame] | 49 | #else |
Denis Vlasenko | bf678d5 | 2007-05-09 12:50:08 +0000 | [diff] [blame] | 50 | #define USE_GETPUT(...) __VA_ARGS__ |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 51 | /* masks coming from getopt32 */ |
Denis Vlasenko | 3163555 | 2007-01-20 16:54:19 +0000 | [diff] [blame] | 52 | #define CMD_GET(cmd) ((cmd) & 1) |
| 53 | #define CMD_PUT(cmd) ((cmd) & 2) |
"Vladimir N. Oleynik" | 86ac072 | 2005-10-17 10:47:19 +0000 | [diff] [blame] | 54 | #endif |
Denis Vlasenko | 8e9ccba | 2007-01-11 16:50:23 +0000 | [diff] [blame] | 55 | /* NB: in the code below |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 56 | * CMD_GET(cmd) and CMD_PUT(cmd) are mutually exclusive |
Denis Vlasenko | 8e9ccba | 2007-01-11 16:50:23 +0000 | [diff] [blame] | 57 | */ |
"Vladimir N. Oleynik" | 86ac072 | 2005-10-17 10:47:19 +0000 | [diff] [blame] | 58 | |
Eric Andersen | 76fa8ea | 2001-08-20 17:47:49 +0000 | [diff] [blame] | 59 | |
Denis Vlasenko | 04291bc | 2006-11-21 10:15:25 +0000 | [diff] [blame] | 60 | #if ENABLE_FEATURE_TFTP_BLOCKSIZE |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 61 | |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 62 | static int tftp_blocksize_check(int blocksize, int bufsize) |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 63 | { |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 64 | /* Check if the blocksize is valid: |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 65 | * RFC2348 says between 8 and 65464, |
| 66 | * but our implementation makes it impossible |
| 67 | * to use blocksizes smaller than 22 octets. |
| 68 | */ |
Denis Vlasenko | 8e9ccba | 2007-01-11 16:50:23 +0000 | [diff] [blame] | 69 | if ((bufsize && (blocksize > bufsize)) |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 70 | || (blocksize < 24) || (blocksize > 65564) |
Denis Vlasenko | 8e9ccba | 2007-01-11 16:50:23 +0000 | [diff] [blame] | 71 | ) { |
Bernhard Reutner-Fischer | b25f98a | 2006-06-10 14:15:03 +0000 | [diff] [blame] | 72 | bb_error_msg("bad blocksize"); |
| 73 | return 0; |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 74 | } |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 75 | return blocksize; |
| 76 | } |
| 77 | |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 78 | static char *tftp_option_get(char *buf, int len, const char *option) |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 79 | { |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 80 | int opt_val = 0; |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 81 | int opt_found = 0; |
| 82 | int k; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 83 | |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 84 | /* buf points to: |
| 85 | * "opt_name<NUL>opt_val<NUL>opt_name2<NUL>opt_val2<NUL>..." */ |
| 86 | |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 87 | while (len > 0) { |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 88 | /* Make sure options are terminated correctly */ |
Bernhard Reutner-Fischer | b25f98a | 2006-06-10 14:15:03 +0000 | [diff] [blame] | 89 | for (k = 0; k < len; k++) { |
| 90 | if (buf[k] == '\0') { |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 91 | goto nul_found; |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 92 | } |
| 93 | } |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 94 | return NULL; |
| 95 | nul_found: |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 96 | if (opt_val == 0) { /* it's "name" part */ |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 97 | if (strcasecmp(buf, option) == 0) { |
Bernhard Reutner-Fischer | b25f98a | 2006-06-10 14:15:03 +0000 | [diff] [blame] | 98 | opt_found = 1; |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 99 | } |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 100 | } else if (opt_found) { |
| 101 | return buf; |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 102 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 103 | |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 104 | k++; |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 105 | buf += k; |
| 106 | len -= k; |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 107 | opt_val ^= 1; |
| 108 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 109 | |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 110 | return NULL; |
| 111 | } |
| 112 | |
| 113 | #endif |
| 114 | |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 115 | static int tftp_protocol( |
| 116 | USE_GETPUT(int cmd,) |
| 117 | len_and_sockaddr *our_lsa, |
Denis Vlasenko | 0850cda | 2007-02-07 23:20:32 +0000 | [diff] [blame] | 118 | len_and_sockaddr *peer_lsa, |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 119 | USE_TFTP(const char *remote_file,) |
| 120 | int local_fd, |
| 121 | int blocksize) |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 122 | { |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 123 | #if !ENABLE_TFTP |
| 124 | #define remote_file NULL |
| 125 | #endif |
Denis Vlasenko | 87f3b26 | 2007-09-07 13:43:28 +0000 | [diff] [blame] | 126 | struct pollfd pfd[1]; |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 127 | #define socket_fd (pfd[0].fd) |
Bernhard Reutner-Fischer | 62f9856 | 2006-06-10 14:32:56 +0000 | [diff] [blame] | 128 | int len; |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 129 | int send_len; |
| 130 | USE_FEATURE_TFTP_BLOCKSIZE(smallint want_option_ack = 0;) |
| 131 | smallint finished = 0; |
| 132 | uint16_t opcode; |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 133 | uint16_t block_nr; |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 134 | uint16_t recv_blk; |
Denis Vlasenko | 87f3b26 | 2007-09-07 13:43:28 +0000 | [diff] [blame] | 135 | int retries, waittime_ms; |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 136 | int tftp_bufsize = blocksize + 4; |
Bernhard Reutner-Fischer | b25f98a | 2006-06-10 14:15:03 +0000 | [diff] [blame] | 137 | char *cp; |
Eric Andersen | 744a194 | 2001-11-10 11:16:39 +0000 | [diff] [blame] | 138 | /* Can't use RESERVE_CONFIG_BUFFER here since the allocation |
| 139 | * size varies meaning BUFFERS_GO_ON_STACK would fail */ |
Denis Vlasenko | 10f7dd1 | 2006-12-17 01:14:08 +0000 | [diff] [blame] | 140 | /* We must keep the transmit and receive buffers seperate */ |
| 141 | /* In case we rcv a garbage pkt and we need to rexmit the last pkt */ |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 142 | char *xbuf = xmalloc(tftp_bufsize); |
Denis Vlasenko | 10f7dd1 | 2006-12-17 01:14:08 +0000 | [diff] [blame] | 143 | char *rbuf = xmalloc(tftp_bufsize); |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 144 | |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 145 | socket_fd = xsocket(peer_lsa->u.sa.sa_family, SOCK_DGRAM, 0); |
| 146 | setsockopt_reuseaddr(socket_fd); |
Denis Vlasenko | 6536a9b | 2007-01-12 10:35:23 +0000 | [diff] [blame] | 147 | |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 148 | if (!ENABLE_TFTP || our_lsa) { |
| 149 | /* tftpd */ |
| 150 | block_nr = 0; |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 151 | |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 152 | /* Create a socket which is: |
| 153 | * 1. bound to IP:port peer sent 1st datagram to, |
| 154 | * 2. connected to peer's IP:port |
| 155 | * This way we will answer from the IP:port peer |
| 156 | * expects, will not get any other packets on |
| 157 | * the socket, and also plain read/write will work. */ |
| 158 | xbind(socket_fd, &our_lsa->u.sa, our_lsa->len); |
| 159 | xconnect(socket_fd, &peer_lsa->u.sa, peer_lsa->len); |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 160 | |
Denis Vlasenko | 04291bc | 2006-11-21 10:15:25 +0000 | [diff] [blame] | 161 | #if ENABLE_FEATURE_TFTP_BLOCKSIZE |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 162 | if (blocksize != TFTP_BLOCKSIZE_DEFAULT) { |
| 163 | /* Create and send OACK packet */ |
| 164 | opcode = TFTP_OACK; |
| 165 | cp = xbuf + 2; |
| 166 | goto add_blksize_opt; |
| 167 | } |
| 168 | /* else: just fall into while (1) loop below */ |
| 169 | #endif |
| 170 | } else { |
| 171 | /* tftp */ |
| 172 | block_nr = 1; |
| 173 | |
| 174 | /* We can't (and don't really need to) bind the socket: |
| 175 | * we don't know from which local IP datagrams will be sent, |
| 176 | * but kernel will pick the same IP every time (unless routing |
| 177 | * table is changed), thus peer will see dgrams consistently |
| 178 | * coming from the same IP. |
| 179 | * We would like to connect the socket, but since peer's |
| 180 | * UDP code can be less perfect than ours, _peer's_ IP:port |
| 181 | * in replies may differ from IP:port we used to send |
| 182 | * our first packet. We can connect() only when we get |
| 183 | * first reply. */ |
| 184 | |
| 185 | /* build opcode */ |
| 186 | opcode = TFTP_WRQ; |
| 187 | if (CMD_GET(cmd)) { |
| 188 | opcode = TFTP_RRQ; |
| 189 | } |
| 190 | cp = xbuf + 2; |
| 191 | /* add filename and mode */ |
| 192 | /* fill in packet if the filename fits into xbuf */ |
| 193 | len = strlen(remote_file) + 1; |
| 194 | if (2 + len + sizeof("octet") >= tftp_bufsize) { |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 195 | bb_error_msg("remote filename is too long"); |
| 196 | goto ret; |
| 197 | } |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 198 | strcpy(cp, remote_file); |
| 199 | cp += len; |
| 200 | /* add "mode" part of the package */ |
| 201 | strcpy(cp, "octet"); |
| 202 | cp += sizeof("octet"); |
| 203 | |
| 204 | #if ENABLE_FEATURE_TFTP_BLOCKSIZE |
| 205 | if (blocksize != TFTP_BLOCKSIZE_DEFAULT) { |
| 206 | /* rfc2348 says that 65464 is a max allowed value */ |
| 207 | if ((&xbuf[tftp_bufsize - 1] - cp) < sizeof("blksize NNNNN")) { |
| 208 | bb_error_msg("remote filename is too long"); |
| 209 | goto ret; |
| 210 | } |
| 211 | want_option_ack = 1; |
| 212 | add_blksize_opt: |
| 213 | /* add "blksize", <nul>, blocksize, <nul> */ |
| 214 | strcpy(cp, "blksize"); |
| 215 | cp += sizeof("blksize"); |
| 216 | cp += snprintf(cp, 6, "%d", blocksize) + 1; |
| 217 | } |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 218 | #endif |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 219 | /* First packet is built, so skip packet generation */ |
| 220 | goto send_pkt; |
| 221 | } |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 222 | |
Denis Vlasenko | bf678d5 | 2007-05-09 12:50:08 +0000 | [diff] [blame] | 223 | /* Using mostly goto's - continue/break will be less clear |
| 224 | * in where we actually jump to */ |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 225 | while (1) { |
| 226 | /* Build ACK or DATA */ |
| 227 | cp = xbuf + 2; |
| 228 | *((uint16_t*)cp) = htons(block_nr); |
| 229 | cp += 2; |
| 230 | block_nr++; |
| 231 | opcode = TFTP_ACK; |
| 232 | if (CMD_PUT(cmd)) { |
| 233 | opcode = TFTP_DATA; |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 234 | len = full_read(local_fd, cp, tftp_bufsize - 4); |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 235 | if (len < 0) { |
| 236 | bb_perror_msg(bb_msg_read_error); |
| 237 | goto ret; |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 238 | } |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 239 | if (len != (tftp_bufsize - 4)) { |
| 240 | finished = 1; |
| 241 | } |
| 242 | cp += len; |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 243 | } |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 244 | send_pkt: |
| 245 | /* Send packet */ |
| 246 | *((uint16_t*)xbuf) = htons(opcode); /* fill in opcode part */ |
Denis Vlasenko | bf678d5 | 2007-05-09 12:50:08 +0000 | [diff] [blame] | 247 | send_len = cp - xbuf; |
| 248 | /* NB: send_len value is preserved in code below |
| 249 | * for potential resend */ |
Paul Fox | 40f0bcf | 2007-09-06 17:52:22 +0000 | [diff] [blame] | 250 | |
| 251 | retries = TFTP_NUM_RETRIES; /* re-initialize */ |
Denis Vlasenko | 87f3b26 | 2007-09-07 13:43:28 +0000 | [diff] [blame] | 252 | waittime_ms = TFTP_TIMEOUT_MS; |
Paul Fox | 40f0bcf | 2007-09-06 17:52:22 +0000 | [diff] [blame] | 253 | |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 254 | send_again: |
Denis Vlasenko | 04291bc | 2006-11-21 10:15:25 +0000 | [diff] [blame] | 255 | #if ENABLE_DEBUG_TFTP |
Denis Vlasenko | bf678d5 | 2007-05-09 12:50:08 +0000 | [diff] [blame] | 256 | fprintf(stderr, "sending %u bytes\n", send_len); |
| 257 | for (cp = xbuf; cp < &xbuf[send_len]; cp++) |
| 258 | fprintf(stderr, "%02x ", (unsigned char) *cp); |
| 259 | fprintf(stderr, "\n"); |
Eric Andersen | 76fa8ea | 2001-08-20 17:47:49 +0000 | [diff] [blame] | 260 | #endif |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 261 | 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] | 262 | /* Was it final ACK? then exit */ |
| 263 | if (finished && (opcode == TFTP_ACK)) |
| 264 | goto ret; |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 265 | |
Denis Vlasenko | 2c91652 | 2007-01-12 14:57:37 +0000 | [diff] [blame] | 266 | recv_again: |
Denis Vlasenko | bf678d5 | 2007-05-09 12:50:08 +0000 | [diff] [blame] | 267 | /* Receive packet */ |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 268 | /*pfd[0].fd = socket_fd;*/ |
Denis Vlasenko | 87f3b26 | 2007-09-07 13:43:28 +0000 | [diff] [blame] | 269 | pfd[0].events = POLLIN; |
Denis Vlasenko | 5d61e71 | 2007-09-27 10:09:59 +0000 | [diff] [blame] | 270 | switch (safe_poll(pfd, 1, waittime_ms)) { |
Denis Vlasenko | bf678d5 | 2007-05-09 12:50:08 +0000 | [diff] [blame] | 271 | case 1: |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 272 | if (!our_lsa) { |
| 273 | /* tftp (not tftpd!) receiving 1st packet */ |
| 274 | our_lsa = ((void*)(ptrdiff_t)-1); /* not NULL */ |
| 275 | len = recvfrom(socket_fd, rbuf, tftp_bufsize, 0, |
| 276 | &peer_lsa->u.sa, &peer_lsa->len); |
| 277 | /* Our first dgram went to port 69 |
| 278 | * but reply may come from different one. |
| 279 | * Remember and use this new port (and IP) */ |
| 280 | if (len >= 0) |
| 281 | xconnect(socket_fd, &peer_lsa->u.sa, peer_lsa->len); |
| 282 | } else { |
| 283 | /* tftpd, or not the very first packet: |
| 284 | * socket is connect()ed, can just read from it. */ |
| 285 | len = safe_read(socket_fd, rbuf, tftp_bufsize); |
| 286 | } |
Denis Vlasenko | bf678d5 | 2007-05-09 12:50:08 +0000 | [diff] [blame] | 287 | if (len < 0) { |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 288 | bb_perror_msg("read"); |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 289 | goto ret; |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 290 | } |
Denis Vlasenko | bf678d5 | 2007-05-09 12:50:08 +0000 | [diff] [blame] | 291 | goto process_pkt; |
| 292 | case 0: |
Paul Fox | 40f0bcf | 2007-09-06 17:52:22 +0000 | [diff] [blame] | 293 | retries--; |
| 294 | if (retries == 0) { |
| 295 | bb_error_msg("timeout"); |
Denis Vlasenko | bf678d5 | 2007-05-09 12:50:08 +0000 | [diff] [blame] | 296 | goto ret; |
| 297 | } |
Paul Fox | 40f0bcf | 2007-09-06 17:52:22 +0000 | [diff] [blame] | 298 | |
| 299 | /* exponential backoff with limit */ |
Denis Vlasenko | 87f3b26 | 2007-09-07 13:43:28 +0000 | [diff] [blame] | 300 | waittime_ms += waittime_ms/2; |
| 301 | if (waittime_ms > TFTP_MAXTIMEOUT_MS) { |
| 302 | waittime_ms = TFTP_MAXTIMEOUT_MS; |
Paul Fox | 40f0bcf | 2007-09-06 17:52:22 +0000 | [diff] [blame] | 303 | } |
| 304 | |
Denis Vlasenko | bf678d5 | 2007-05-09 12:50:08 +0000 | [diff] [blame] | 305 | goto send_again; /* resend last sent pkt */ |
| 306 | default: |
Denis Vlasenko | 5d61e71 | 2007-09-27 10:09:59 +0000 | [diff] [blame] | 307 | /*bb_perror_msg("poll"); - done in safe_poll */ |
Denis Vlasenko | bf678d5 | 2007-05-09 12:50:08 +0000 | [diff] [blame] | 308 | goto ret; |
| 309 | } |
| 310 | process_pkt: |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 311 | /* Process recv'ed packet */ |
Denis Vlasenko | 8e9ccba | 2007-01-11 16:50:23 +0000 | [diff] [blame] | 312 | opcode = ntohs( ((uint16_t*)rbuf)[0] ); |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 313 | recv_blk = ntohs( ((uint16_t*)rbuf)[1] ); |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 314 | |
Denis Vlasenko | 04291bc | 2006-11-21 10:15:25 +0000 | [diff] [blame] | 315 | #if ENABLE_DEBUG_TFTP |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 316 | fprintf(stderr, "received %d bytes: %04x %04x\n", len, opcode, recv_blk); |
Eric Andersen | 76fa8ea | 2001-08-20 17:47:49 +0000 | [diff] [blame] | 317 | #endif |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 318 | |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 319 | if (opcode == TFTP_ERROR) { |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 320 | static const char *const errcode_str[] = { |
| 321 | "", |
| 322 | "file not found", |
| 323 | "access violation", |
| 324 | "disk full", |
| 325 | "illegal TFTP operation", |
| 326 | "unknown transfer id", |
| 327 | "file already exists", |
| 328 | "no such user", |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 329 | "bad option" |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 330 | }; |
Denis Vlasenko | 80b8b39 | 2007-06-25 10:55:35 +0000 | [diff] [blame] | 331 | |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 332 | const char *msg = ""; |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 333 | |
Denis Vlasenko | 10f7dd1 | 2006-12-17 01:14:08 +0000 | [diff] [blame] | 334 | if (rbuf[4] != '\0') { |
| 335 | msg = &rbuf[4]; |
| 336 | rbuf[tftp_bufsize - 1] = '\0'; |
Denis Vlasenko | 80b8b39 | 2007-06-25 10:55:35 +0000 | [diff] [blame] | 337 | } else if (recv_blk < ARRAY_SIZE(errcode_str)) { |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 338 | msg = errcode_str[recv_blk]; |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 339 | } |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 340 | bb_error_msg("server error: (%u) %s", recv_blk, msg); |
| 341 | goto ret; |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 342 | } |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 343 | |
Denis Vlasenko | 04291bc | 2006-11-21 10:15:25 +0000 | [diff] [blame] | 344 | #if ENABLE_FEATURE_TFTP_BLOCKSIZE |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 345 | if (want_option_ack) { |
Bernhard Reutner-Fischer | b25f98a | 2006-06-10 14:15:03 +0000 | [diff] [blame] | 346 | want_option_ack = 0; |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 347 | |
Bernhard Reutner-Fischer | b25f98a | 2006-06-10 14:15:03 +0000 | [diff] [blame] | 348 | if (opcode == TFTP_OACK) { |
Bernhard Reutner-Fischer | b25f98a | 2006-06-10 14:15:03 +0000 | [diff] [blame] | 349 | /* server seems to support options */ |
Bernhard Reutner-Fischer | b25f98a | 2006-06-10 14:15:03 +0000 | [diff] [blame] | 350 | char *res; |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 351 | |
Denis Vlasenko | 8e9ccba | 2007-01-11 16:50:23 +0000 | [diff] [blame] | 352 | res = tftp_option_get(&rbuf[2], len - 2, "blksize"); |
Bernhard Reutner-Fischer | b25f98a | 2006-06-10 14:15:03 +0000 | [diff] [blame] | 353 | if (res) { |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 354 | int blksize = xatoi_u(res); |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 355 | if (!tftp_blocksize_check(blksize, tftp_bufsize - 4)) { |
Denis Vlasenko | bf678d5 | 2007-05-09 12:50:08 +0000 | [diff] [blame] | 356 | /* send ERROR 8 to server... */ |
| 357 | /* htons can be impossible to use in const initializer: */ |
| 358 | /*static const uint16_t error_8[2] = { htons(TFTP_ERROR), htons(8) };*/ |
| 359 | /* thus we open-code big-endian layout */ |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 360 | static const uint8_t error_8[4] = { 0,TFTP_ERROR, 0,8 }; |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 361 | xsendto(socket_fd, error_8, 4, &peer_lsa->u.sa, peer_lsa->len); |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 362 | bb_error_msg("server proposes bad blksize %d, exiting", blksize); |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 363 | goto ret; |
Bernhard Reutner-Fischer | b25f98a | 2006-06-10 14:15:03 +0000 | [diff] [blame] | 364 | } |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 365 | #if ENABLE_DEBUG_TFTP |
| 366 | fprintf(stderr, "using blksize %u\n", |
| 367 | blksize); |
| 368 | #endif |
| 369 | tftp_bufsize = blksize + 4; |
Denis Vlasenko | bf678d5 | 2007-05-09 12:50:08 +0000 | [diff] [blame] | 370 | /* Send ACK for OACK ("block" no: 0) */ |
| 371 | block_nr = 0; |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 372 | continue; |
Bernhard Reutner-Fischer | b25f98a | 2006-06-10 14:15:03 +0000 | [diff] [blame] | 373 | } |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 374 | /* rfc2347: |
| 375 | * "An option not acknowledged by the server |
| 376 | * must be ignored by the client and server |
| 377 | * as if it were never requested." */ |
Bernhard Reutner-Fischer | b25f98a | 2006-06-10 14:15:03 +0000 | [diff] [blame] | 378 | } |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 379 | |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 380 | bb_error_msg("blksize is not supported by server" |
| 381 | " - reverting to 512"); |
Bernhard Reutner-Fischer | b25f98a | 2006-06-10 14:15:03 +0000 | [diff] [blame] | 382 | tftp_bufsize = TFTP_BLOCKSIZE_DEFAULT + 4; |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 383 | } |
| 384 | #endif |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 385 | /* block_nr is already advanced to next block# we expect |
| 386 | * to get / block# we are about to send next time */ |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 387 | |
Denis Vlasenko | 8e9ccba | 2007-01-11 16:50:23 +0000 | [diff] [blame] | 388 | if (CMD_GET(cmd) && (opcode == TFTP_DATA)) { |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 389 | if (recv_blk == block_nr) { |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 390 | len = full_write(local_fd, &rbuf[4], len - 4); |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 391 | if (len < 0) { |
Bernhard Reutner-Fischer | 1b9d7c9 | 2006-06-03 22:45:37 +0000 | [diff] [blame] | 392 | bb_perror_msg(bb_msg_write_error); |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 393 | goto ret; |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 394 | } |
Eric Andersen | 76fa8ea | 2001-08-20 17:47:49 +0000 | [diff] [blame] | 395 | if (len != (tftp_bufsize - 4)) { |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 396 | finished = 1; |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 397 | } |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 398 | continue; /* send ACK */ |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 399 | } |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 400 | if (recv_blk == (block_nr - 1)) { |
Paul Fox | 1d4c88c | 2005-07-20 19:49:15 +0000 | [diff] [blame] | 401 | /* Server lost our TFTP_ACK. Resend it */ |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 402 | block_nr = recv_blk; |
Paul Fox | 1d4c88c | 2005-07-20 19:49:15 +0000 | [diff] [blame] | 403 | continue; |
Denis Vlasenko | 4b924f3 | 2007-05-30 00:29:55 +0000 | [diff] [blame] | 404 | } |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 405 | } |
| 406 | |
Denis Vlasenko | 8e9ccba | 2007-01-11 16:50:23 +0000 | [diff] [blame] | 407 | if (CMD_PUT(cmd) && (opcode == TFTP_ACK)) { |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 408 | /* did peer ACK our last DATA pkt? */ |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 409 | if (recv_blk == (uint16_t) (block_nr - 1)) { |
| 410 | if (finished) |
| 411 | goto ret; |
| 412 | continue; /* send next block */ |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 413 | } |
| 414 | } |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 415 | /* Awww... recv'd packet is not recognized! */ |
| 416 | goto recv_again; |
| 417 | /* why recv_again? - rfc1123 says: |
| 418 | * "The sender (i.e., the side originating the DATA packets) |
| 419 | * must never resend the current DATA packet on receipt |
| 420 | * of a duplicate ACK". |
| 421 | * DATA pkts are resent ONLY on timeout. |
| 422 | * Thus "goto send_again" will ba a bad mistake above. |
| 423 | * See: |
| 424 | * http://en.wikipedia.org/wiki/Sorcerer's_Apprentice_Syndrome |
| 425 | */ |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 426 | } /* end of "while (1)" */ |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 427 | ret: |
Denis Vlasenko | 2c91652 | 2007-01-12 14:57:37 +0000 | [diff] [blame] | 428 | if (ENABLE_FEATURE_CLEAN_UP) { |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 429 | close(socket_fd); |
Denis Vlasenko | 2c91652 | 2007-01-12 14:57:37 +0000 | [diff] [blame] | 430 | free(xbuf); |
| 431 | free(rbuf); |
| 432 | } |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 433 | return finished == 0; /* returns 1 on failure */ |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 434 | } |
| 435 | |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 436 | #if ENABLE_TFTP |
| 437 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 438 | int tftp_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 439 | int tftp_main(int argc ATTRIBUTE_UNUSED, char **argv) |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 440 | { |
Denis Vlasenko | 8e9ccba | 2007-01-11 16:50:23 +0000 | [diff] [blame] | 441 | len_and_sockaddr *peer_lsa; |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 442 | const char *local_file = NULL; |
| 443 | const char *remote_file = NULL; |
Glenn L McGrath | 036dbaa | 2004-01-17 05:03:31 +0000 | [diff] [blame] | 444 | int port; |
Denis Vlasenko | 8e9ccba | 2007-01-11 16:50:23 +0000 | [diff] [blame] | 445 | USE_GETPUT(int cmd;) |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 446 | int local_fd; |
Eric Andersen | 76fa8ea | 2001-08-20 17:47:49 +0000 | [diff] [blame] | 447 | int flags = 0; |
Eric Andersen | 76fa8ea | 2001-08-20 17:47:49 +0000 | [diff] [blame] | 448 | int result; |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 449 | int blocksize = TFTP_BLOCKSIZE_DEFAULT; |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 450 | |
Denis Vlasenko | 8e9ccba | 2007-01-11 16:50:23 +0000 | [diff] [blame] | 451 | /* -p or -g is mandatory, and they are mutually exclusive */ |
| 452 | opt_complementary = "" USE_FEATURE_TFTP_GET("g:") USE_FEATURE_TFTP_PUT("p:") |
Denis Vlasenko | 1d42665 | 2008-03-17 09:09:09 +0000 | [diff] [blame] | 453 | USE_GETPUT("g--p:p--g:") |
| 454 | USE_FEATURE_TFTP_BLOCKSIZE("b+"); |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 455 | |
Denis Vlasenko | fe7cd64 | 2007-08-18 15:32:12 +0000 | [diff] [blame] | 456 | USE_GETPUT(cmd =) getopt32(argv, |
Denis Vlasenko | 8e9ccba | 2007-01-11 16:50:23 +0000 | [diff] [blame] | 457 | USE_FEATURE_TFTP_GET("g") USE_FEATURE_TFTP_PUT("p") |
| 458 | "l:r:" USE_FEATURE_TFTP_BLOCKSIZE("b:"), |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 459 | &local_file, &remote_file |
Denis Vlasenko | 1d42665 | 2008-03-17 09:09:09 +0000 | [diff] [blame] | 460 | USE_FEATURE_TFTP_BLOCKSIZE(, &blocksize)); |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 461 | argv += optind; |
Bernhard Reutner-Fischer | b25f98a | 2006-06-10 14:15:03 +0000 | [diff] [blame] | 462 | |
Denis Vlasenko | 8e9ccba | 2007-01-11 16:50:23 +0000 | [diff] [blame] | 463 | flags = O_RDONLY; |
| 464 | if (CMD_GET(cmd)) |
Bernhard Reutner-Fischer | b25f98a | 2006-06-10 14:15:03 +0000 | [diff] [blame] | 465 | flags = O_WRONLY | O_CREAT | O_TRUNC; |
Bernhard Reutner-Fischer | b25f98a | 2006-06-10 14:15:03 +0000 | [diff] [blame] | 466 | |
Denis Vlasenko | 04291bc | 2006-11-21 10:15:25 +0000 | [diff] [blame] | 467 | #if ENABLE_FEATURE_TFTP_BLOCKSIZE |
Denis Vlasenko | 1d42665 | 2008-03-17 09:09:09 +0000 | [diff] [blame] | 468 | if (!tftp_blocksize_check(blocksize, 0)) |
| 469 | return EXIT_FAILURE; |
"Vladimir N. Oleynik" | 86ac072 | 2005-10-17 10:47:19 +0000 | [diff] [blame] | 470 | #endif |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 471 | |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 472 | if (!local_file) |
| 473 | local_file = remote_file; |
| 474 | if (!remote_file) |
| 475 | remote_file = local_file; |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 476 | /* Error if filename or host is not known */ |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 477 | if (!remote_file || !argv[0]) |
Bernhard Reutner-Fischer | b25f98a | 2006-06-10 14:15:03 +0000 | [diff] [blame] | 478 | bb_show_usage(); |
"Vladimir N. Oleynik" | 86ac072 | 2005-10-17 10:47:19 +0000 | [diff] [blame] | 479 | |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 480 | local_fd = CMD_GET(cmd) ? STDOUT_FILENO : STDIN_FILENO; |
| 481 | if (!LONE_DASH(local_file)) { |
| 482 | local_fd = xopen(local_file, flags); |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 483 | } |
| 484 | |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 485 | port = bb_lookup_port(argv[1], "udp", 69); |
| 486 | peer_lsa = xhost2sockaddr(argv[0], port); |
Eric Andersen | 76fa8ea | 2001-08-20 17:47:49 +0000 | [diff] [blame] | 487 | |
Denis Vlasenko | 04291bc | 2006-11-21 10:15:25 +0000 | [diff] [blame] | 488 | #if ENABLE_DEBUG_TFTP |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 489 | 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] | 490 | xmalloc_sockaddr2dotted(&peer_lsa->u.sa), |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 491 | remote_file, local_file); |
Eric Andersen | 76fa8ea | 2001-08-20 17:47:49 +0000 | [diff] [blame] | 492 | #endif |
| 493 | |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 494 | result = tftp_protocol( |
| 495 | USE_GETPUT(cmd,) |
| 496 | NULL /* our_lsa*/, |
| 497 | peer_lsa, |
| 498 | remote_file, local_fd, blocksize); |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 499 | |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 500 | if (ENABLE_FEATURE_CLEAN_UP) |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 501 | close(local_fd); |
| 502 | if (result != EXIT_SUCCESS && !LONE_DASH(local_file) && CMD_GET(cmd)) { |
| 503 | unlink(local_file); |
Eric Andersen | a66a43e | 2002-04-13 09:30:25 +0000 | [diff] [blame] | 504 | } |
Denis Vlasenko | 000b9ba | 2006-10-05 23:12:49 +0000 | [diff] [blame] | 505 | return result; |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 506 | } |
Denis Vlasenko | 3163555 | 2007-01-20 16:54:19 +0000 | [diff] [blame] | 507 | |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 508 | #endif /* ENABLE_TFTP */ |
| 509 | |
| 510 | #if ENABLE_TFTPD |
| 511 | |
| 512 | /* TODO: libbb candidate? */ |
| 513 | static len_and_sockaddr *get_sock_lsa(int s) |
| 514 | { |
| 515 | len_and_sockaddr *lsa; |
| 516 | socklen_t len = 0; |
| 517 | |
| 518 | if (getsockname(s, NULL, &len) != 0) |
| 519 | return NULL; |
| 520 | lsa = xzalloc(LSA_LEN_SIZE + len); |
| 521 | lsa->len = len; |
| 522 | getsockname(s, &lsa->u.sa, &lsa->len); |
| 523 | return lsa; |
| 524 | } |
| 525 | |
| 526 | int tftpd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 527 | int tftpd_main(int argc ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED) |
| 528 | { |
| 529 | struct stat statbuf; |
| 530 | char block_buf[TFTP_BLOCKSIZE_DEFAULT]; |
| 531 | len_and_sockaddr *our_lsa; |
| 532 | len_and_sockaddr *peer_lsa; |
| 533 | char *filename, *mode, *opt_str; |
| 534 | int result, opcode, cmd, req_modebits, open_mode, local_fd, blksize; |
| 535 | |
| 536 | our_lsa = get_sock_lsa(STDIN_FILENO); |
| 537 | if (!our_lsa) |
| 538 | bb_perror_msg_and_die("stdin is not a socket"); |
| 539 | peer_lsa = xzalloc(LSA_LEN_SIZE + our_lsa->len); |
| 540 | peer_lsa->len = our_lsa->len; |
| 541 | |
| 542 | if (argv[1]) |
| 543 | xchdir(argv[1]); |
| 544 | |
| 545 | result = recv_from_to(STDIN_FILENO, block_buf, sizeof(block_buf), |
| 546 | 0 /* flags */, |
| 547 | &peer_lsa->u.sa, &our_lsa->u.sa, our_lsa->len); |
| 548 | |
| 549 | opcode = ntohs(*(uint16_t*)block_buf); |
| 550 | if (result < 4 || result >= sizeof(block_buf) |
| 551 | || block_buf[result-1] != '\0' |
| 552 | || (opcode != TFTP_RRQ && opcode != TFTP_WRQ) |
| 553 | ) { |
| 554 | bb_error_msg_and_die("malformed packet"); |
| 555 | } |
| 556 | filename = block_buf + 2; |
| 557 | if (filename[0] == '.' || strstr(filename, "/.")) { |
| 558 | bb_error_msg_and_die("dot in filename"); |
| 559 | } |
| 560 | mode = filename + strlen(filename) + 1; |
| 561 | if (mode >= block_buf + sizeof(block_buf) |
| 562 | || strcmp(mode, "octet") != 0 |
| 563 | ) { |
| 564 | bb_error_msg_and_die("malformed packet"); |
| 565 | } |
| 566 | blksize = TFTP_BLOCKSIZE_DEFAULT; |
| 567 | #if ENABLE_FEATURE_TFTP_BLOCKSIZE |
| 568 | opt_str = mode + 6; |
| 569 | if (opt_str < block_buf + sizeof(block_buf)) { |
| 570 | char *res = tftp_option_get(opt_str, block_buf + sizeof(block_buf) - opt_str, "blksize"); |
| 571 | if (res) { |
| 572 | int sz = xatoi_u(res); |
| 573 | if (tftp_blocksize_check(sz, 0)) |
| 574 | blksize = sz; |
| 575 | } |
| 576 | } |
| 577 | #endif |
| 578 | xstat(filename, &statbuf); |
| 579 | /* if opcode == TFTP_WRQ: */ |
| 580 | cmd = 1; /* CMD_GET: we will receive file's data */ |
| 581 | req_modebits = 0222; /* writable by anyone */ |
| 582 | open_mode = O_WRONLY | O_TRUNC; |
| 583 | if (opcode == TFTP_RRQ) { |
| 584 | cmd = 2; /* CMD_PUT */ |
| 585 | req_modebits = 0444; /* readable by anyone */ |
| 586 | open_mode = O_RDONLY; |
| 587 | } |
| 588 | if (!S_ISREG(statbuf.st_mode) |
| 589 | || (statbuf.st_mode & req_modebits) != req_modebits |
| 590 | ) { |
| 591 | bb_error_msg_and_die("access to '%s' is denied", filename); |
| 592 | } |
| 593 | local_fd = xopen(filename, open_mode); |
| 594 | |
| 595 | close(STDIN_FILENO); /* close old, possibly wildcard socket */ |
| 596 | /* tftp_protocol() will create new one, bound to particular local IP */ |
| 597 | result = tftp_protocol( |
| 598 | USE_GETPUT(cmd,) |
| 599 | our_lsa, peer_lsa, |
| 600 | USE_TFTP(NULL /*remote_file*/,) |
| 601 | local_fd, |
| 602 | blksize |
| 603 | ); |
| 604 | |
| 605 | return result; |
| 606 | } |
| 607 | |
| 608 | #endif /* ENABLE_TFTPD */ |
| 609 | |
Denis Vlasenko | 3163555 | 2007-01-20 16:54:19 +0000 | [diff] [blame] | 610 | #endif /* ENABLE_FEATURE_TFTP_GET || ENABLE_FEATURE_TFTP_PUT */ |