blob: 799dd99039a5aded485d5b01c0882e956fe258a7 [file] [log] [blame]
Bernhard Reutner-Fischerdac7ff12006-04-12 17:55:51 +00001/* vi: set sw=4 ts=4: */
2/* -------------------------------------------------------------------------
3 * tftp.c
4 *
Denis Vlasenkoaa9b1822008-03-17 09:10:39 +00005 * A simple tftp client/server for busybox.
Bernhard Reutner-Fischerdac7ff12006-04-12 17:55:51 +00006 * 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 Vlasenko78c56562008-03-18 00:11:46 +000019 * tftpd added by Denys Vlasenko & Vladimir Dronnikov
Denis Vlasenkoaa9b1822008-03-17 09:10:39 +000020 *
Bernhard Reutner-Fischerdac7ff12006-04-12 17:55:51 +000021 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
22 * ------------------------------------------------------------------------- */
Mark Whitley450736c2001-03-02 19:08:50 +000023
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000024#include "libbb.h"
Mark Whitley450736c2001-03-02 19:08:50 +000025
Denis Vlasenko31635552007-01-20 16:54:19 +000026#if ENABLE_FEATURE_TFTP_GET || ENABLE_FEATURE_TFTP_PUT
27
Denis Vlasenko4824cca2008-03-21 18:29:01 +000028#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-Fischerb25f98a2006-06-10 14:15:03 +000033
Glenn L McGrathad117d82001-10-05 04:40:37 +000034/* opcodes we support */
Glenn L McGrathad117d82001-10-05 04:40:37 +000035#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 Vlasenko8474cd32008-06-16 07:12:19 +000042/* error codes sent over network (we use only 0, 1, 3 and 8) */
Denis Vlasenko71c9f012008-03-19 09:43:50 +000043/* generic (error message is included in the packet) */
Denis Vlasenkodd9228b2008-03-19 05:00:05 +000044#define ERR_UNSPEC 0
45#define ERR_NOFILE 1
46#define ERR_ACCESS 2
Denis Vlasenko71c9f012008-03-19 09:43:50 +000047/* disk full or allocation exceeded */
Denis Vlasenkodd9228b2008-03-19 05:00:05 +000048#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 Vlasenko403a5a22008-03-19 13:07:00 +000055/* masks coming from getopt32 */
56enum {
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 Vlasenko8e9ccba2007-01-11 16:50:23 +000066#if ENABLE_FEATURE_TFTP_GET && !ENABLE_FEATURE_TFTP_PUT
Denis Vlasenkobf678d52007-05-09 12:50:08 +000067#define USE_GETPUT(...)
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +000068#define CMD_GET(cmd) 1
69#define CMD_PUT(cmd) 0
70#elif !ENABLE_FEATURE_TFTP_GET && ENABLE_FEATURE_TFTP_PUT
Denis Vlasenkobf678d52007-05-09 12:50:08 +000071#define USE_GETPUT(...)
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +000072#define CMD_GET(cmd) 0
73#define CMD_PUT(cmd) 1
"Vladimir N. Oleynik"86ac0722005-10-17 10:47:19 +000074#else
Denis Vlasenkobf678d52007-05-09 12:50:08 +000075#define USE_GETPUT(...) __VA_ARGS__
Denis Vlasenko403a5a22008-03-19 13:07:00 +000076#define CMD_GET(cmd) ((cmd) & TFTP_OPT_GET)
77#define CMD_PUT(cmd) ((cmd) & TFTP_OPT_PUT)
"Vladimir N. Oleynik"86ac0722005-10-17 10:47:19 +000078#endif
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +000079/* NB: in the code below
Denis Vlasenkoa04561f2007-05-08 23:12:21 +000080 * CMD_GET(cmd) and CMD_PUT(cmd) are mutually exclusive
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +000081 */
"Vladimir N. Oleynik"86ac0722005-10-17 10:47:19 +000082
Denis Vlasenkodd9228b2008-03-19 05:00:05 +000083
84struct globals {
85 /* u16 TFTP_ERROR; u16 reason; both network-endian, then error text: */
86 uint8_t error_pkt[4 + 32];
Denis Vlasenko403a5a22008-03-19 13:07:00 +000087 char *user_opt;
Denis Vlasenko71c9f012008-03-19 09:43:50 +000088 /* used in tftpd_main(), a bit big for stack: */
Denis Vlasenkodd9228b2008-03-19 05:00:05 +000089 char block_buf[TFTP_BLKSIZE_DEFAULT];
90};
91#define G (*(struct globals*)&bb_common_bufsiz1)
92#define block_buf (G.block_buf )
Denis Vlasenko403a5a22008-03-19 13:07:00 +000093#define user_opt (G.user_opt )
Denis Vlasenkodd9228b2008-03-19 05:00:05 +000094#define error_pkt (G.error_pkt )
Denis Vlasenko7049ff82008-06-25 09:53:17 +000095#define INIT_G() do { } while (0)
Denis Vlasenkodd9228b2008-03-19 05:00:05 +000096
97#define error_pkt_reason (error_pkt[3])
98#define error_pkt_str (error_pkt + 4)
Denis Vlasenkod7e6af22008-03-18 01:13:11 +000099
Eric Andersen76fa8ea2001-08-20 17:47:49 +0000100
Denis Vlasenko04291bc2006-11-21 10:15:25 +0000101#if ENABLE_FEATURE_TFTP_BLOCKSIZE
Glenn L McGrathad117d82001-10-05 04:40:37 +0000102
Denis Vlasenkodd9228b2008-03-19 05:00:05 +0000103static int tftp_blksize_check(const char *blksize_str, int maxsize)
Glenn L McGrathad117d82001-10-05 04:40:37 +0000104{
Denis Vlasenkodd9228b2008-03-19 05:00:05 +0000105 /* Check if the blksize is valid:
Glenn L McGrathad117d82001-10-05 04:40:37 +0000106 * RFC2348 says between 8 and 65464,
107 * but our implementation makes it impossible
Denis Vlasenkodd9228b2008-03-19 05:00:05 +0000108 * to use blksizes smaller than 22 octets. */
109 unsigned blksize = bb_strtou(blksize_str, NULL, 10);
110 if (errno
111 || (blksize < 24) || (blksize > maxsize)
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000112 ) {
Denis Vlasenkodd9228b2008-03-19 05:00:05 +0000113 bb_error_msg("bad blocksize '%s'", blksize_str);
114 return -1;
Glenn L McGrathad117d82001-10-05 04:40:37 +0000115 }
Denis Vlasenko35a064b2008-11-06 00:49:59 +0000116#if ENABLE_TFTP_DEBUG
Denis Vlasenkodd9228b2008-03-19 05:00:05 +0000117 bb_error_msg("using blksize %u", blksize);
118#endif
119 return blksize;
Glenn L McGrathad117d82001-10-05 04:40:37 +0000120}
121
Denis Vlasenko8474cd32008-06-16 07:12:19 +0000122static char *tftp_get_option(const char *option, char *buf, int len)
Glenn L McGrathad117d82001-10-05 04:40:37 +0000123{
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000124 int opt_val = 0;
Glenn L McGrathad117d82001-10-05 04:40:37 +0000125 int opt_found = 0;
126 int k;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000127
Denis Vlasenkoaa9b1822008-03-17 09:10:39 +0000128 /* buf points to:
129 * "opt_name<NUL>opt_val<NUL>opt_name2<NUL>opt_val2<NUL>..." */
130
Glenn L McGrathad117d82001-10-05 04:40:37 +0000131 while (len > 0) {
Denis Vlasenkoaa9b1822008-03-17 09:10:39 +0000132 /* Make sure options are terminated correctly */
Bernhard Reutner-Fischerb25f98a2006-06-10 14:15:03 +0000133 for (k = 0; k < len; k++) {
134 if (buf[k] == '\0') {
Denis Vlasenkoa04561f2007-05-08 23:12:21 +0000135 goto nul_found;
Glenn L McGrathad117d82001-10-05 04:40:37 +0000136 }
137 }
Denis Vlasenkoa04561f2007-05-08 23:12:21 +0000138 return NULL;
139 nul_found:
Denis Vlasenkoaa9b1822008-03-17 09:10:39 +0000140 if (opt_val == 0) { /* it's "name" part */
Glenn L McGrathad117d82001-10-05 04:40:37 +0000141 if (strcasecmp(buf, option) == 0) {
Bernhard Reutner-Fischerb25f98a2006-06-10 14:15:03 +0000142 opt_found = 1;
Glenn L McGrathad117d82001-10-05 04:40:37 +0000143 }
Denis Vlasenkoa04561f2007-05-08 23:12:21 +0000144 } else if (opt_found) {
145 return buf;
Glenn L McGrathad117d82001-10-05 04:40:37 +0000146 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000147
Glenn L McGrathad117d82001-10-05 04:40:37 +0000148 k++;
Glenn L McGrathad117d82001-10-05 04:40:37 +0000149 buf += k;
150 len -= k;
Glenn L McGrathad117d82001-10-05 04:40:37 +0000151 opt_val ^= 1;
152 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000153
Glenn L McGrathad117d82001-10-05 04:40:37 +0000154 return NULL;
155}
156
157#endif
158
Denis Vlasenkoaa9b1822008-03-17 09:10:39 +0000159static int tftp_protocol(
Denis Vlasenkoaa9b1822008-03-17 09:10:39 +0000160 len_and_sockaddr *our_lsa,
Denis Vlasenko0850cda2007-02-07 23:20:32 +0000161 len_and_sockaddr *peer_lsa,
Denis Vlasenko8474cd32008-06-16 07:12:19 +0000162 const char *local_file
163 USE_TFTP(, const char *remote_file)
164 USE_FEATURE_TFTP_BLOCKSIZE(USE_TFTPD(, void *tsize))
165 USE_FEATURE_TFTP_BLOCKSIZE(, int blksize))
Mark Whitley450736c2001-03-02 19:08:50 +0000166{
Denis Vlasenkoaa9b1822008-03-17 09:10:39 +0000167#if !ENABLE_TFTP
168#define remote_file NULL
169#endif
Denis Vlasenko8474cd32008-06-16 07:12:19 +0000170#if !(ENABLE_FEATURE_TFTP_BLOCKSIZE && ENABLE_TFTPD)
171#define tsize NULL
172#endif
173#if !ENABLE_FEATURE_TFTP_BLOCKSIZE
174 enum { blksize = TFTP_BLKSIZE_DEFAULT };
175#endif
176
Denis Vlasenko87f3b262007-09-07 13:43:28 +0000177 struct pollfd pfd[1];
Denis Vlasenkoaa9b1822008-03-17 09:10:39 +0000178#define socket_fd (pfd[0].fd)
Bernhard Reutner-Fischer62f98562006-06-10 14:32:56 +0000179 int len;
Denis Vlasenkoa04561f2007-05-08 23:12:21 +0000180 int send_len;
181 USE_FEATURE_TFTP_BLOCKSIZE(smallint want_option_ack = 0;)
182 smallint finished = 0;
183 uint16_t opcode;
Denis Vlasenkoaa9b1822008-03-17 09:10:39 +0000184 uint16_t block_nr;
Denis Vlasenkoa04561f2007-05-08 23:12:21 +0000185 uint16_t recv_blk;
Denis Vlasenko403a5a22008-03-19 13:07:00 +0000186 int open_mode, local_fd;
Denis Vlasenko87f3b262007-09-07 13:43:28 +0000187 int retries, waittime_ms;
Denis Vlasenkodd9228b2008-03-19 05:00:05 +0000188 int io_bufsize = blksize + 4;
Denis Vlasenko4824cca2008-03-21 18:29:01 +0000189 char *cp;
Eric Andersen744a1942001-11-10 11:16:39 +0000190 /* Can't use RESERVE_CONFIG_BUFFER here since the allocation
191 * size varies meaning BUFFERS_GO_ON_STACK would fail */
Denis Vlasenko10f7dd12006-12-17 01:14:08 +0000192 /* We must keep the transmit and receive buffers seperate */
193 /* In case we rcv a garbage pkt and we need to rexmit the last pkt */
Denis Vlasenkodd9228b2008-03-19 05:00:05 +0000194 char *xbuf = xmalloc(io_bufsize);
195 char *rbuf = xmalloc(io_bufsize);
Mark Whitley450736c2001-03-02 19:08:50 +0000196
Denis Vlasenkoaa9b1822008-03-17 09:10:39 +0000197 socket_fd = xsocket(peer_lsa->u.sa.sa_family, SOCK_DGRAM, 0);
198 setsockopt_reuseaddr(socket_fd);
Denis Vlasenko6536a9b2007-01-12 10:35:23 +0000199
Denis Vlasenko78c56562008-03-18 00:11:46 +0000200 block_nr = 1;
Denis Vlasenko4824cca2008-03-21 18:29:01 +0000201 cp = xbuf + 2;
Denis Vlasenko7a601332008-03-19 13:24:13 +0000202
Denis Vlasenkoaa9b1822008-03-17 09:10:39 +0000203 if (!ENABLE_TFTP || our_lsa) {
204 /* tftpd */
Mark Whitley450736c2001-03-02 19:08:50 +0000205
Denis Vlasenkoaa9b1822008-03-17 09:10:39 +0000206 /* Create a socket which is:
207 * 1. bound to IP:port peer sent 1st datagram to,
208 * 2. connected to peer's IP:port
209 * This way we will answer from the IP:port peer
210 * expects, will not get any other packets on
211 * the socket, and also plain read/write will work. */
212 xbind(socket_fd, &our_lsa->u.sa, our_lsa->len);
213 xconnect(socket_fd, &peer_lsa->u.sa, peer_lsa->len);
Glenn L McGrathad117d82001-10-05 04:40:37 +0000214
Denis Vlasenkodd9228b2008-03-19 05:00:05 +0000215 /* Is there an error already? Send pkt and bail out */
216 if (error_pkt_reason || error_pkt_str[0])
217 goto send_err_pkt;
218
Denis Vlasenko403a5a22008-03-19 13:07:00 +0000219 if (CMD_GET(option_mask32)) {
Denis Vlasenko78c56562008-03-18 00:11:46 +0000220 /* it's upload - we must ACK 1st packet (with filename)
221 * as if it's "block 0" */
222 block_nr = 0;
223 }
224
Denis Vlasenko7a601332008-03-19 13:24:13 +0000225 if (user_opt) {
Denis Vlasenkod7a805e2008-12-03 19:05:55 +0000226 struct passwd *pw = xgetpwnam(user_opt);
Denis Vlasenko7a601332008-03-19 13:24:13 +0000227 change_identity(pw); /* initgroups, setgid, setuid */
228 }
229 }
230
231 /* Open local file (must be after changing user) */
232 if (CMD_PUT(option_mask32)) {
233 open_mode = O_RDONLY;
234 } else {
235 open_mode = O_WRONLY | O_TRUNC | O_CREAT;
236#if ENABLE_TFTPD
237 if ((option_mask32 & (TFTPD_OPT+TFTPD_OPT_c)) == TFTPD_OPT) {
238 /* tftpd without -c */
239 open_mode = O_WRONLY | O_TRUNC;
240 }
241#endif
242 }
243 if (!(option_mask32 & TFTPD_OPT)) {
244 local_fd = CMD_GET(option_mask32) ? STDOUT_FILENO : STDIN_FILENO;
245 if (NOT_LONE_DASH(local_file))
246 local_fd = xopen(local_file, open_mode);
247 } else {
Denis Vlasenko8474cd32008-06-16 07:12:19 +0000248 local_fd = open(local_file, open_mode);
Denis Vlasenko7a601332008-03-19 13:24:13 +0000249 if (local_fd < 0) {
Denis Vlasenko8474cd32008-06-16 07:12:19 +0000250 error_pkt_reason = ERR_NOFILE;
Denis Vlasenko55995022008-05-18 22:28:26 +0000251 strcpy((char*)error_pkt_str, "can't open file");
Denis Vlasenko7a601332008-03-19 13:24:13 +0000252 goto send_err_pkt;
253 }
254 }
255
256 if (!ENABLE_TFTP || our_lsa) {
Denis Vlasenko31e12862008-06-16 07:32:40 +0000257/* gcc 4.3.1 would NOT optimize it out as it should! */
258#if ENABLE_FEATURE_TFTP_BLOCKSIZE
Denis Vlasenko8474cd32008-06-16 07:12:19 +0000259 if (blksize != TFTP_BLKSIZE_DEFAULT || tsize) {
Denis Vlasenko4824cca2008-03-21 18:29:01 +0000260 /* Create and send OACK packet. */
261 /* For the download case, block_nr is still 1 -
262 * we expect 1st ACK from peer to be for (block_nr-1),
263 * that is, for "block 0" which is our OACK pkt */
264 opcode = TFTP_OACK;
Denis Vlasenkoaa9b1822008-03-17 09:10:39 +0000265 goto add_blksize_opt;
266 }
Denis Vlasenko31e12862008-06-16 07:32:40 +0000267#endif
Denis Vlasenko8474cd32008-06-16 07:12:19 +0000268 } else {
269/* Removing it, or using if() statement instead of #if may lead to
Denis Vlasenko4824cca2008-03-21 18:29:01 +0000270 * "warning: null argument where non-null required": */
271#if ENABLE_TFTP
Denis Vlasenkoaa9b1822008-03-17 09:10:39 +0000272 /* tftp */
Denis Vlasenkoaa9b1822008-03-17 09:10:39 +0000273
274 /* We can't (and don't really need to) bind the socket:
275 * we don't know from which local IP datagrams will be sent,
276 * but kernel will pick the same IP every time (unless routing
277 * table is changed), thus peer will see dgrams consistently
278 * coming from the same IP.
279 * We would like to connect the socket, but since peer's
280 * UDP code can be less perfect than ours, _peer's_ IP:port
281 * in replies may differ from IP:port we used to send
282 * our first packet. We can connect() only when we get
283 * first reply. */
284
285 /* build opcode */
286 opcode = TFTP_WRQ;
Denis Vlasenko403a5a22008-03-19 13:07:00 +0000287 if (CMD_GET(option_mask32)) {
Denis Vlasenkoaa9b1822008-03-17 09:10:39 +0000288 opcode = TFTP_RRQ;
289 }
Denis Vlasenkoaa9b1822008-03-17 09:10:39 +0000290 /* add filename and mode */
291 /* fill in packet if the filename fits into xbuf */
292 len = strlen(remote_file) + 1;
Denis Vlasenkodd9228b2008-03-19 05:00:05 +0000293 if (2 + len + sizeof("octet") >= io_bufsize) {
Denis Vlasenkoa04561f2007-05-08 23:12:21 +0000294 bb_error_msg("remote filename is too long");
295 goto ret;
296 }
Denis Vlasenkoaa9b1822008-03-17 09:10:39 +0000297 strcpy(cp, remote_file);
298 cp += len;
299 /* add "mode" part of the package */
300 strcpy(cp, "octet");
301 cp += sizeof("octet");
302
Denis Vlasenko31e12862008-06-16 07:32:40 +0000303#if ENABLE_FEATURE_TFTP_BLOCKSIZE
Denis Vlasenko4824cca2008-03-21 18:29:01 +0000304 if (blksize == TFTP_BLKSIZE_DEFAULT)
305 goto send_pkt;
306
307 /* Non-standard blocksize: add option to pkt */
308 if ((&xbuf[io_bufsize - 1] - cp) < sizeof("blksize NNNNN")) {
309 bb_error_msg("remote filename is too long");
310 goto ret;
Denis Vlasenkoaa9b1822008-03-17 09:10:39 +0000311 }
Denis Vlasenko31e12862008-06-16 07:32:40 +0000312 want_option_ack = 1;
313#endif
Denis Vlasenko4824cca2008-03-21 18:29:01 +0000314#endif /* ENABLE_TFTP */
315
Denis Vlasenko31e12862008-06-16 07:32:40 +0000316#if ENABLE_FEATURE_TFTP_BLOCKSIZE
Denis Vlasenko4824cca2008-03-21 18:29:01 +0000317 add_blksize_opt:
Denis Vlasenko8474cd32008-06-16 07:12:19 +0000318#if ENABLE_TFTPD
319 if (tsize) {
320 struct stat st;
321 /* add "tsize", <nul>, size, <nul> */
322 strcpy(cp, "tsize");
323 cp += sizeof("tsize");
324 fstat(local_fd, &st);
325 cp += snprintf(cp, 10, "%u", (int) st.st_size) + 1;
326 }
Glenn L McGrathad117d82001-10-05 04:40:37 +0000327#endif
Denis Vlasenko8474cd32008-06-16 07:12:19 +0000328 if (blksize != TFTP_BLKSIZE_DEFAULT) {
329 /* add "blksize", <nul>, blksize, <nul> */
330 strcpy(cp, "blksize");
331 cp += sizeof("blksize");
332 cp += snprintf(cp, 6, "%d", blksize) + 1;
333 }
Denis Vlasenko31e12862008-06-16 07:32:40 +0000334#endif
Denis Vlasenkoaa9b1822008-03-17 09:10:39 +0000335 /* First packet is built, so skip packet generation */
336 goto send_pkt;
337 }
Mark Whitley450736c2001-03-02 19:08:50 +0000338
Denis Vlasenkobf678d52007-05-09 12:50:08 +0000339 /* Using mostly goto's - continue/break will be less clear
340 * in where we actually jump to */
Denis Vlasenkoa04561f2007-05-08 23:12:21 +0000341 while (1) {
342 /* Build ACK or DATA */
343 cp = xbuf + 2;
344 *((uint16_t*)cp) = htons(block_nr);
345 cp += 2;
346 block_nr++;
347 opcode = TFTP_ACK;
Denis Vlasenko403a5a22008-03-19 13:07:00 +0000348 if (CMD_PUT(option_mask32)) {
Denis Vlasenkoa04561f2007-05-08 23:12:21 +0000349 opcode = TFTP_DATA;
Denis Vlasenkodd9228b2008-03-19 05:00:05 +0000350 len = full_read(local_fd, cp, blksize);
Denis Vlasenkoa04561f2007-05-08 23:12:21 +0000351 if (len < 0) {
Denis Vlasenkodd9228b2008-03-19 05:00:05 +0000352 goto send_read_err_pkt;
Mark Whitley450736c2001-03-02 19:08:50 +0000353 }
Denis Vlasenkodd9228b2008-03-19 05:00:05 +0000354 if (len != blksize) {
Denis Vlasenkoa04561f2007-05-08 23:12:21 +0000355 finished = 1;
356 }
357 cp += len;
Mark Whitley450736c2001-03-02 19:08:50 +0000358 }
Denis Vlasenkoa04561f2007-05-08 23:12:21 +0000359 send_pkt:
360 /* Send packet */
361 *((uint16_t*)xbuf) = htons(opcode); /* fill in opcode part */
Denis Vlasenkobf678d52007-05-09 12:50:08 +0000362 send_len = cp - xbuf;
363 /* NB: send_len value is preserved in code below
364 * for potential resend */
Paul Fox40f0bcf2007-09-06 17:52:22 +0000365
366 retries = TFTP_NUM_RETRIES; /* re-initialize */
Denis Vlasenko87f3b262007-09-07 13:43:28 +0000367 waittime_ms = TFTP_TIMEOUT_MS;
Paul Fox40f0bcf2007-09-06 17:52:22 +0000368
Denis Vlasenkoa04561f2007-05-08 23:12:21 +0000369 send_again:
Denis Vlasenko35a064b2008-11-06 00:49:59 +0000370#if ENABLE_TFTP_DEBUG
Denis Vlasenkobf678d52007-05-09 12:50:08 +0000371 fprintf(stderr, "sending %u bytes\n", send_len);
372 for (cp = xbuf; cp < &xbuf[send_len]; cp++)
373 fprintf(stderr, "%02x ", (unsigned char) *cp);
374 fprintf(stderr, "\n");
Eric Andersen76fa8ea2001-08-20 17:47:49 +0000375#endif
Denis Vlasenkoaa9b1822008-03-17 09:10:39 +0000376 xsendto(socket_fd, xbuf, send_len, &peer_lsa->u.sa, peer_lsa->len);
Denis Vlasenkobf678d52007-05-09 12:50:08 +0000377 /* Was it final ACK? then exit */
378 if (finished && (opcode == TFTP_ACK))
379 goto ret;
Mark Whitley450736c2001-03-02 19:08:50 +0000380
Denis Vlasenko2c916522007-01-12 14:57:37 +0000381 recv_again:
Denis Vlasenkobf678d52007-05-09 12:50:08 +0000382 /* Receive packet */
Denis Vlasenkoaa9b1822008-03-17 09:10:39 +0000383 /*pfd[0].fd = socket_fd;*/
Denis Vlasenko87f3b262007-09-07 13:43:28 +0000384 pfd[0].events = POLLIN;
Denis Vlasenko5d61e712007-09-27 10:09:59 +0000385 switch (safe_poll(pfd, 1, waittime_ms)) {
Denis Vlasenkodd9228b2008-03-19 05:00:05 +0000386 default:
387 /*bb_perror_msg("poll"); - done in safe_poll */
388 goto ret;
Denis Vlasenkobf678d52007-05-09 12:50:08 +0000389 case 0:
Paul Fox40f0bcf2007-09-06 17:52:22 +0000390 retries--;
391 if (retries == 0) {
392 bb_error_msg("timeout");
Denis Vlasenkodd9228b2008-03-19 05:00:05 +0000393 goto ret; /* no err packet sent */
Denis Vlasenkobf678d52007-05-09 12:50:08 +0000394 }
Paul Fox40f0bcf2007-09-06 17:52:22 +0000395
396 /* exponential backoff with limit */
Denis Vlasenko87f3b262007-09-07 13:43:28 +0000397 waittime_ms += waittime_ms/2;
398 if (waittime_ms > TFTP_MAXTIMEOUT_MS) {
399 waittime_ms = TFTP_MAXTIMEOUT_MS;
Paul Fox40f0bcf2007-09-06 17:52:22 +0000400 }
401
Denis Vlasenkobf678d52007-05-09 12:50:08 +0000402 goto send_again; /* resend last sent pkt */
Denis Vlasenkodd9228b2008-03-19 05:00:05 +0000403 case 1:
404 if (!our_lsa) {
405 /* tftp (not tftpd!) receiving 1st packet */
406 our_lsa = ((void*)(ptrdiff_t)-1); /* not NULL */
407 len = recvfrom(socket_fd, rbuf, io_bufsize, 0,
408 &peer_lsa->u.sa, &peer_lsa->len);
409 /* Our first dgram went to port 69
410 * but reply may come from different one.
411 * Remember and use this new port (and IP) */
412 if (len >= 0)
413 xconnect(socket_fd, &peer_lsa->u.sa, peer_lsa->len);
414 } else {
415 /* tftpd, or not the very first packet:
416 * socket is connect()ed, can just read from it. */
417 /* Don't full_read()!
418 * This is not TCP, one read == one pkt! */
419 len = safe_read(socket_fd, rbuf, io_bufsize);
420 }
421 if (len < 0) {
422 goto send_read_err_pkt;
423 }
424 if (len < 4) { /* too small? */
425 goto recv_again;
426 }
Denis Vlasenkobf678d52007-05-09 12:50:08 +0000427 }
Denis Vlasenkodd9228b2008-03-19 05:00:05 +0000428
Denis Vlasenkoa04561f2007-05-08 23:12:21 +0000429 /* Process recv'ed packet */
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000430 opcode = ntohs( ((uint16_t*)rbuf)[0] );
Denis Vlasenkoa04561f2007-05-08 23:12:21 +0000431 recv_blk = ntohs( ((uint16_t*)rbuf)[1] );
Denis Vlasenko35a064b2008-11-06 00:49:59 +0000432#if ENABLE_TFTP_DEBUG
Denis Vlasenkoa04561f2007-05-08 23:12:21 +0000433 fprintf(stderr, "received %d bytes: %04x %04x\n", len, opcode, recv_blk);
Eric Andersen76fa8ea2001-08-20 17:47:49 +0000434#endif
Glenn L McGrathad117d82001-10-05 04:40:37 +0000435 if (opcode == TFTP_ERROR) {
Denis Vlasenko8474cd32008-06-16 07:12:19 +0000436 static const char errcode_str[] ALIGN1 =
Denis Vlasenkodd9228b2008-03-19 05:00:05 +0000437 "\0"
438 "file not found\0"
439 "access violation\0"
440 "disk full\0"
441 "bad operation\0"
442 "unknown transfer id\0"
443 "file already exists\0"
444 "no such user\0"
445 "bad option";
Denis Vlasenko80b8b392007-06-25 10:55:35 +0000446
Denis Vlasenkoa04561f2007-05-08 23:12:21 +0000447 const char *msg = "";
Glenn L McGrathad117d82001-10-05 04:40:37 +0000448
Denis Vlasenkod7e6af22008-03-18 01:13:11 +0000449 if (len > 4 && rbuf[4] != '\0') {
Denis Vlasenko10f7dd12006-12-17 01:14:08 +0000450 msg = &rbuf[4];
Denis Vlasenkodd9228b2008-03-19 05:00:05 +0000451 rbuf[io_bufsize - 1] = '\0'; /* paranoia */
452 } else if (recv_blk <= 8) {
453 msg = nth_string(errcode_str, recv_blk);
Glenn L McGrathad117d82001-10-05 04:40:37 +0000454 }
Denis Vlasenkoa04561f2007-05-08 23:12:21 +0000455 bb_error_msg("server error: (%u) %s", recv_blk, msg);
456 goto ret;
Glenn L McGrathad117d82001-10-05 04:40:37 +0000457 }
Denis Vlasenkoa04561f2007-05-08 23:12:21 +0000458
Denis Vlasenko04291bc2006-11-21 10:15:25 +0000459#if ENABLE_FEATURE_TFTP_BLOCKSIZE
Glenn L McGrathad117d82001-10-05 04:40:37 +0000460 if (want_option_ack) {
Bernhard Reutner-Fischerb25f98a2006-06-10 14:15:03 +0000461 want_option_ack = 0;
Bernhard Reutner-Fischerb25f98a2006-06-10 14:15:03 +0000462 if (opcode == TFTP_OACK) {
Bernhard Reutner-Fischerb25f98a2006-06-10 14:15:03 +0000463 /* server seems to support options */
Bernhard Reutner-Fischerb25f98a2006-06-10 14:15:03 +0000464 char *res;
Glenn L McGrathad117d82001-10-05 04:40:37 +0000465
Denis Vlasenko8474cd32008-06-16 07:12:19 +0000466 res = tftp_get_option("blksize", &rbuf[2], len - 2);
Bernhard Reutner-Fischerb25f98a2006-06-10 14:15:03 +0000467 if (res) {
Denis Vlasenkodd9228b2008-03-19 05:00:05 +0000468 blksize = tftp_blksize_check(res, blksize);
469 if (blksize < 0) {
470 error_pkt_reason = ERR_BAD_OPT;
471 goto send_err_pkt;
Bernhard Reutner-Fischerb25f98a2006-06-10 14:15:03 +0000472 }
Denis Vlasenkodd9228b2008-03-19 05:00:05 +0000473 io_bufsize = blksize + 4;
Denis Vlasenkobf678d52007-05-09 12:50:08 +0000474 /* Send ACK for OACK ("block" no: 0) */
475 block_nr = 0;
Denis Vlasenkoa04561f2007-05-08 23:12:21 +0000476 continue;
Bernhard Reutner-Fischerb25f98a2006-06-10 14:15:03 +0000477 }
Denis Vlasenkoa04561f2007-05-08 23:12:21 +0000478 /* rfc2347:
479 * "An option not acknowledged by the server
480 * must be ignored by the client and server
481 * as if it were never requested." */
Bernhard Reutner-Fischerb25f98a2006-06-10 14:15:03 +0000482 }
Denis Vlasenkodd9228b2008-03-19 05:00:05 +0000483 bb_error_msg("server only supports blocksize of 512");
484 blksize = TFTP_BLKSIZE_DEFAULT;
485 io_bufsize = TFTP_BLKSIZE_DEFAULT + 4;
Glenn L McGrathad117d82001-10-05 04:40:37 +0000486 }
487#endif
Denis Vlasenkoa04561f2007-05-08 23:12:21 +0000488 /* block_nr is already advanced to next block# we expect
489 * to get / block# we are about to send next time */
Glenn L McGrathad117d82001-10-05 04:40:37 +0000490
Denis Vlasenko403a5a22008-03-19 13:07:00 +0000491 if (CMD_GET(option_mask32) && (opcode == TFTP_DATA)) {
Denis Vlasenkoa04561f2007-05-08 23:12:21 +0000492 if (recv_blk == block_nr) {
Denis Vlasenko71c9f012008-03-19 09:43:50 +0000493 int sz = full_write(local_fd, &rbuf[4], len - 4);
494 if (sz != len - 4) {
Denis Vlasenko55995022008-05-18 22:28:26 +0000495 strcpy((char*)error_pkt_str, bb_msg_write_error);
Denis Vlasenkodd9228b2008-03-19 05:00:05 +0000496 error_pkt_reason = ERR_WRITE;
497 goto send_err_pkt;
Mark Whitley450736c2001-03-02 19:08:50 +0000498 }
Denis Vlasenko71c9f012008-03-19 09:43:50 +0000499 if (sz != blksize) {
Denis Vlasenkoa04561f2007-05-08 23:12:21 +0000500 finished = 1;
Mark Whitley450736c2001-03-02 19:08:50 +0000501 }
Denis Vlasenkoa04561f2007-05-08 23:12:21 +0000502 continue; /* send ACK */
Mark Whitley450736c2001-03-02 19:08:50 +0000503 }
Denis Vlasenkoa04561f2007-05-08 23:12:21 +0000504 if (recv_blk == (block_nr - 1)) {
Paul Fox1d4c88c2005-07-20 19:49:15 +0000505 /* Server lost our TFTP_ACK. Resend it */
Denis Vlasenkoa04561f2007-05-08 23:12:21 +0000506 block_nr = recv_blk;
Paul Fox1d4c88c2005-07-20 19:49:15 +0000507 continue;
Denis Vlasenko4b924f32007-05-30 00:29:55 +0000508 }
Mark Whitley450736c2001-03-02 19:08:50 +0000509 }
510
Denis Vlasenko403a5a22008-03-19 13:07:00 +0000511 if (CMD_PUT(option_mask32) && (opcode == TFTP_ACK)) {
Denis Vlasenkoaa9b1822008-03-17 09:10:39 +0000512 /* did peer ACK our last DATA pkt? */
Denis Vlasenkoa04561f2007-05-08 23:12:21 +0000513 if (recv_blk == (uint16_t) (block_nr - 1)) {
514 if (finished)
515 goto ret;
516 continue; /* send next block */
Mark Whitley450736c2001-03-02 19:08:50 +0000517 }
518 }
Denis Vlasenkoa04561f2007-05-08 23:12:21 +0000519 /* Awww... recv'd packet is not recognized! */
520 goto recv_again;
521 /* why recv_again? - rfc1123 says:
522 * "The sender (i.e., the side originating the DATA packets)
523 * must never resend the current DATA packet on receipt
524 * of a duplicate ACK".
525 * DATA pkts are resent ONLY on timeout.
526 * Thus "goto send_again" will ba a bad mistake above.
527 * See:
528 * http://en.wikipedia.org/wiki/Sorcerer's_Apprentice_Syndrome
529 */
Denis Vlasenkoaa9b1822008-03-17 09:10:39 +0000530 } /* end of "while (1)" */
Denis Vlasenkoa04561f2007-05-08 23:12:21 +0000531 ret:
Denis Vlasenko2c916522007-01-12 14:57:37 +0000532 if (ENABLE_FEATURE_CLEAN_UP) {
Denis Vlasenko403a5a22008-03-19 13:07:00 +0000533 close(local_fd);
Denis Vlasenkoaa9b1822008-03-17 09:10:39 +0000534 close(socket_fd);
Denis Vlasenko2c916522007-01-12 14:57:37 +0000535 free(xbuf);
536 free(rbuf);
537 }
Denis Vlasenkoa04561f2007-05-08 23:12:21 +0000538 return finished == 0; /* returns 1 on failure */
Denis Vlasenkodd9228b2008-03-19 05:00:05 +0000539
540 send_read_err_pkt:
Denis Vlasenko55995022008-05-18 22:28:26 +0000541 strcpy((char*)error_pkt_str, bb_msg_read_error);
Denis Vlasenkodd9228b2008-03-19 05:00:05 +0000542 send_err_pkt:
Denis Vlasenko71c9f012008-03-19 09:43:50 +0000543 if (error_pkt_str[0])
Denis Vlasenko55995022008-05-18 22:28:26 +0000544 bb_error_msg((char*)error_pkt_str);
Denis Vlasenkodd9228b2008-03-19 05:00:05 +0000545 error_pkt[1] = TFTP_ERROR;
Denis Vlasenko55995022008-05-18 22:28:26 +0000546 xsendto(socket_fd, error_pkt, 4 + 1 + strlen((char*)error_pkt_str),
Denis Vlasenkodd9228b2008-03-19 05:00:05 +0000547 &peer_lsa->u.sa, peer_lsa->len);
548 return EXIT_FAILURE;
Denis Vlasenko8474cd32008-06-16 07:12:19 +0000549#undef remote_file
550#undef tsize
Mark Whitley450736c2001-03-02 19:08:50 +0000551}
552
Denis Vlasenkoaa9b1822008-03-17 09:10:39 +0000553#if ENABLE_TFTP
554
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000555int tftp_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000556int tftp_main(int argc UNUSED_PARAM, char **argv)
Mark Whitley450736c2001-03-02 19:08:50 +0000557{
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000558 len_and_sockaddr *peer_lsa;
Denis Vlasenkoaa9b1822008-03-17 09:10:39 +0000559 const char *local_file = NULL;
560 const char *remote_file = NULL;
Denis Vlasenko4824cca2008-03-21 18:29:01 +0000561#if ENABLE_FEATURE_TFTP_BLOCKSIZE
562 const char *blksize_str = TFTP_BLKSIZE_DEFAULT_STR;
Denis Vlasenko403a5a22008-03-19 13:07:00 +0000563 int blksize;
Denis Vlasenko8474cd32008-06-16 07:12:19 +0000564#endif
Denis Vlasenko403a5a22008-03-19 13:07:00 +0000565 int result;
566 int port;
567 USE_GETPUT(int opt;)
Denis Vlasenkodd9228b2008-03-19 05:00:05 +0000568
569 INIT_G();
Mark Whitley450736c2001-03-02 19:08:50 +0000570
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000571 /* -p or -g is mandatory, and they are mutually exclusive */
572 opt_complementary = "" USE_FEATURE_TFTP_GET("g:") USE_FEATURE_TFTP_PUT("p:")
Denis Vlasenkodd9228b2008-03-19 05:00:05 +0000573 USE_GETPUT("g--p:p--g:");
Glenn L McGrathad117d82001-10-05 04:40:37 +0000574
Denis Vlasenko403a5a22008-03-19 13:07:00 +0000575 USE_GETPUT(opt =) getopt32(argv,
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000576 USE_FEATURE_TFTP_GET("g") USE_FEATURE_TFTP_PUT("p")
577 "l:r:" USE_FEATURE_TFTP_BLOCKSIZE("b:"),
Denis Vlasenkoaa9b1822008-03-17 09:10:39 +0000578 &local_file, &remote_file
Denis Vlasenkodd9228b2008-03-19 05:00:05 +0000579 USE_FEATURE_TFTP_BLOCKSIZE(, &blksize_str));
Denis Vlasenkoa04561f2007-05-08 23:12:21 +0000580 argv += optind;
Bernhard Reutner-Fischerb25f98a2006-06-10 14:15:03 +0000581
Denis Vlasenko04291bc2006-11-21 10:15:25 +0000582#if ENABLE_FEATURE_TFTP_BLOCKSIZE
Denis Vlasenkodd9228b2008-03-19 05:00:05 +0000583 /* Check if the blksize is valid:
584 * RFC2348 says between 8 and 65464 */
585 blksize = tftp_blksize_check(blksize_str, 65564);
586 if (blksize < 0) {
587 //bb_error_msg("bad block size");
Denis Vlasenko1d426652008-03-17 09:09:09 +0000588 return EXIT_FAILURE;
Denis Vlasenkodd9228b2008-03-19 05:00:05 +0000589 }
"Vladimir N. Oleynik"86ac0722005-10-17 10:47:19 +0000590#endif
Mark Whitley450736c2001-03-02 19:08:50 +0000591
Denis Vlasenkoaa9b1822008-03-17 09:10:39 +0000592 if (!local_file)
593 local_file = remote_file;
594 if (!remote_file)
595 remote_file = local_file;
Denis Vlasenkoa04561f2007-05-08 23:12:21 +0000596 /* Error if filename or host is not known */
Denis Vlasenkoaa9b1822008-03-17 09:10:39 +0000597 if (!remote_file || !argv[0])
Bernhard Reutner-Fischerb25f98a2006-06-10 14:15:03 +0000598 bb_show_usage();
"Vladimir N. Oleynik"86ac0722005-10-17 10:47:19 +0000599
Denis Vlasenkoa04561f2007-05-08 23:12:21 +0000600 port = bb_lookup_port(argv[1], "udp", 69);
601 peer_lsa = xhost2sockaddr(argv[0], port);
Eric Andersen76fa8ea2001-08-20 17:47:49 +0000602
Denis Vlasenko35a064b2008-11-06 00:49:59 +0000603#if ENABLE_TFTP_DEBUG
Denis Vlasenkoaa9b1822008-03-17 09:10:39 +0000604 fprintf(stderr, "using server '%s', remote_file '%s', local_file '%s'\n",
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000605 xmalloc_sockaddr2dotted(&peer_lsa->u.sa),
Denis Vlasenkoaa9b1822008-03-17 09:10:39 +0000606 remote_file, local_file);
Eric Andersen76fa8ea2001-08-20 17:47:49 +0000607#endif
608
Denis Vlasenkoaa9b1822008-03-17 09:10:39 +0000609 result = tftp_protocol(
Denis Vlasenko8474cd32008-06-16 07:12:19 +0000610 NULL /*our_lsa*/, peer_lsa,
611 local_file, remote_file
612 USE_FEATURE_TFTP_BLOCKSIZE(USE_TFTPD(, NULL /*tsize*/))
613 USE_FEATURE_TFTP_BLOCKSIZE(, blksize)
614 );
Mark Whitley450736c2001-03-02 19:08:50 +0000615
Denis Vlasenko403a5a22008-03-19 13:07:00 +0000616 if (result != EXIT_SUCCESS && NOT_LONE_DASH(local_file) && CMD_GET(opt)) {
Denis Vlasenkoaa9b1822008-03-17 09:10:39 +0000617 unlink(local_file);
Eric Andersena66a43e2002-04-13 09:30:25 +0000618 }
Denis Vlasenko000b9ba2006-10-05 23:12:49 +0000619 return result;
Glenn L McGrathad117d82001-10-05 04:40:37 +0000620}
Denis Vlasenko31635552007-01-20 16:54:19 +0000621
Denis Vlasenkoaa9b1822008-03-17 09:10:39 +0000622#endif /* ENABLE_TFTP */
623
624#if ENABLE_TFTPD
625
626/* TODO: libbb candidate? */
627static len_and_sockaddr *get_sock_lsa(int s)
628{
629 len_and_sockaddr *lsa;
630 socklen_t len = 0;
631
632 if (getsockname(s, NULL, &len) != 0)
633 return NULL;
634 lsa = xzalloc(LSA_LEN_SIZE + len);
635 lsa->len = len;
636 getsockname(s, &lsa->u.sa, &lsa->len);
637 return lsa;
638}
639
640int tftpd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000641int tftpd_main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenkoaa9b1822008-03-17 09:10:39 +0000642{
Denis Vlasenkoaa9b1822008-03-17 09:10:39 +0000643 len_and_sockaddr *our_lsa;
644 len_and_sockaddr *peer_lsa;
Denis Vlasenko403a5a22008-03-19 13:07:00 +0000645 char *local_file, *mode;
Denis Vlasenkodd9228b2008-03-19 05:00:05 +0000646 const char *error_msg;
Denis Vlasenko403a5a22008-03-19 13:07:00 +0000647 int opt, result, opcode;
Denis Vlasenko8474cd32008-06-16 07:12:19 +0000648 USE_FEATURE_TFTP_BLOCKSIZE(int blksize = TFTP_BLKSIZE_DEFAULT;)
649 USE_FEATURE_TFTP_BLOCKSIZE(char *tsize = NULL;)
Denis Vlasenkodd9228b2008-03-19 05:00:05 +0000650
651 INIT_G();
Denis Vlasenkoaa9b1822008-03-17 09:10:39 +0000652
653 our_lsa = get_sock_lsa(STDIN_FILENO);
Denis Vlasenko5a897632008-11-01 00:22:24 +0000654 if (!our_lsa) {
655 /* This is confusing:
656 *bb_error_msg_and_die("stdin is not a socket");
657 * Better: */
658 bb_show_usage();
659 /* Help text says that tftpd must be used as inetd service,
660 * which is by far the most usual cause of get_sock_lsa
661 * failure */
662 }
Denis Vlasenkoaa9b1822008-03-17 09:10:39 +0000663 peer_lsa = xzalloc(LSA_LEN_SIZE + our_lsa->len);
664 peer_lsa->len = our_lsa->len;
665
Denis Vlasenko403a5a22008-03-19 13:07:00 +0000666 /* Shifting to not collide with TFTP_OPTs */
667 opt = option_mask32 = TFTPD_OPT | (getopt32(argv, "rcu:", &user_opt) << 8);
Denis Vlasenkod7e6af22008-03-18 01:13:11 +0000668 argv += optind;
669 if (argv[0])
670 xchdir(argv[0]);
Denis Vlasenkoaa9b1822008-03-17 09:10:39 +0000671
672 result = recv_from_to(STDIN_FILENO, block_buf, sizeof(block_buf),
673 0 /* flags */,
674 &peer_lsa->u.sa, &our_lsa->u.sa, our_lsa->len);
675
Denis Vlasenkodd9228b2008-03-19 05:00:05 +0000676 error_msg = "malformed packet";
Denis Vlasenkoaa9b1822008-03-17 09:10:39 +0000677 opcode = ntohs(*(uint16_t*)block_buf);
678 if (result < 4 || result >= sizeof(block_buf)
679 || block_buf[result-1] != '\0'
Denis Vlasenko4824cca2008-03-21 18:29:01 +0000680 || (USE_FEATURE_TFTP_PUT(opcode != TFTP_RRQ) /* not download */
Denis Vlasenkod7e6af22008-03-18 01:13:11 +0000681 USE_GETPUT(&&)
682 USE_FEATURE_TFTP_GET(opcode != TFTP_WRQ) /* not upload */
683 )
Denis Vlasenkoaa9b1822008-03-17 09:10:39 +0000684 ) {
Denis Vlasenkodd9228b2008-03-19 05:00:05 +0000685 goto err;
Denis Vlasenkoaa9b1822008-03-17 09:10:39 +0000686 }
Denis Vlasenko403a5a22008-03-19 13:07:00 +0000687 local_file = block_buf + 2;
688 if (local_file[0] == '.' || strstr(local_file, "/.")) {
Denis Vlasenko0a0180c2008-03-19 23:37:32 +0000689 error_msg = "dot in file name";
Denis Vlasenkodd9228b2008-03-19 05:00:05 +0000690 goto err;
Denis Vlasenkoaa9b1822008-03-17 09:10:39 +0000691 }
Denis Vlasenko403a5a22008-03-19 13:07:00 +0000692 mode = local_file + strlen(local_file) + 1;
693 if (mode >= block_buf + result || strcmp(mode, "octet") != 0) {
Denis Vlasenkodd9228b2008-03-19 05:00:05 +0000694 goto err;
Denis Vlasenkoaa9b1822008-03-17 09:10:39 +0000695 }
Denis Vlasenkoaa9b1822008-03-17 09:10:39 +0000696#if ENABLE_FEATURE_TFTP_BLOCKSIZE
Denis Vlasenkodd9228b2008-03-19 05:00:05 +0000697 {
698 char *res;
699 char *opt_str = mode + sizeof("octet");
700 int opt_len = block_buf + result - opt_str;
701 if (opt_len > 0) {
Denis Vlasenko8474cd32008-06-16 07:12:19 +0000702 res = tftp_get_option("blksize", opt_str, opt_len);
Denis Vlasenkodd9228b2008-03-19 05:00:05 +0000703 if (res) {
704 blksize = tftp_blksize_check(res, 65564);
705 if (blksize < 0) {
706 error_pkt_reason = ERR_BAD_OPT;
707 /* will just send error pkt */
708 goto do_proto;
709 }
710 }
Denis Vlasenko8474cd32008-06-16 07:12:19 +0000711 /* did client ask us about file size? */
712 tsize = tftp_get_option("tsize", opt_str, opt_len);
Denis Vlasenkoaa9b1822008-03-17 09:10:39 +0000713 }
714 }
715#endif
Denis Vlasenkod7e6af22008-03-18 01:13:11 +0000716
Denis Vlasenkod7e6af22008-03-18 01:13:11 +0000717 if (!ENABLE_FEATURE_TFTP_PUT || opcode == TFTP_WRQ) {
Denis Vlasenko403a5a22008-03-19 13:07:00 +0000718 if (opt & TFTPD_OPT_r) {
Denis Vlasenko71c9f012008-03-19 09:43:50 +0000719 /* This would mean "disk full" - not true */
720 /*error_pkt_reason = ERR_WRITE;*/
721 error_msg = bb_msg_write_error;
722 goto err;
Denis Vlasenkodd9228b2008-03-19 05:00:05 +0000723 }
Denis Vlasenko403a5a22008-03-19 13:07:00 +0000724 USE_GETPUT(option_mask32 |= TFTP_OPT_GET;) /* will receive file's data */
725 } else {
726 USE_GETPUT(option_mask32 |= TFTP_OPT_PUT;) /* will send file's data */
Denis Vlasenkodd9228b2008-03-19 05:00:05 +0000727 }
Denis Vlasenkoaa9b1822008-03-17 09:10:39 +0000728
Denis Vlasenkodd9228b2008-03-19 05:00:05 +0000729 /* NB: if error_pkt_str or error_pkt_reason is set up,
730 * tftp_protocol() just sends one error pkt and returns */
Denis Vlasenko8474cd32008-06-16 07:12:19 +0000731
Denis Vlasenkodd9228b2008-03-19 05:00:05 +0000732 do_proto:
Denis Vlasenko8474cd32008-06-16 07:12:19 +0000733 close(STDIN_FILENO); /* close old, possibly wildcard socket */
734 /* tftp_protocol() will create new one, bound to particular local IP */
Denis Vlasenkoaa9b1822008-03-17 09:10:39 +0000735 result = tftp_protocol(
Denis Vlasenkoaa9b1822008-03-17 09:10:39 +0000736 our_lsa, peer_lsa,
Denis Vlasenko8474cd32008-06-16 07:12:19 +0000737 local_file USE_TFTP(, NULL /*remote_file*/)
738 USE_FEATURE_TFTP_BLOCKSIZE(, tsize)
739 USE_FEATURE_TFTP_BLOCKSIZE(, blksize)
Denis Vlasenkoaa9b1822008-03-17 09:10:39 +0000740 );
741
742 return result;
Denis Vlasenko403a5a22008-03-19 13:07:00 +0000743 err:
Denis Vlasenko55995022008-05-18 22:28:26 +0000744 strcpy((char*)error_pkt_str, error_msg);
Denis Vlasenko403a5a22008-03-19 13:07:00 +0000745 goto do_proto;
Denis Vlasenkoaa9b1822008-03-17 09:10:39 +0000746}
747
748#endif /* ENABLE_TFTPD */
749
Denis Vlasenko31635552007-01-20 16:54:19 +0000750#endif /* ENABLE_FEATURE_TFTP_GET || ENABLE_FEATURE_TFTP_PUT */