blob: 23a24139b79b24bdf44d226a046cee813c9a3341 [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 Vlasenkoaa9b1822008-03-17 09:10:39 +000019 * tftpd added by Denys Vlasenko
20 *
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 Vlasenko87f3b262007-09-07 13:43:28 +000028#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-Fischerb25f98a2006-06-10 14:15:03 +000032
Glenn L McGrathad117d82001-10-05 04:40:37 +000033/* opcodes we support */
Glenn L McGrathad117d82001-10-05 04:40:37 +000034#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 Vlasenko8e9ccba2007-01-11 16:50:23 +000041#if ENABLE_FEATURE_TFTP_GET && !ENABLE_FEATURE_TFTP_PUT
Denis Vlasenkobf678d52007-05-09 12:50:08 +000042#define USE_GETPUT(...)
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +000043#define CMD_GET(cmd) 1
44#define CMD_PUT(cmd) 0
45#elif !ENABLE_FEATURE_TFTP_GET && ENABLE_FEATURE_TFTP_PUT
Denis Vlasenkobf678d52007-05-09 12:50:08 +000046#define USE_GETPUT(...)
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +000047#define CMD_GET(cmd) 0
48#define CMD_PUT(cmd) 1
"Vladimir N. Oleynik"86ac0722005-10-17 10:47:19 +000049#else
Denis Vlasenkobf678d52007-05-09 12:50:08 +000050#define USE_GETPUT(...) __VA_ARGS__
Denis Vlasenkoaa9b1822008-03-17 09:10:39 +000051/* masks coming from getopt32 */
Denis Vlasenko31635552007-01-20 16:54:19 +000052#define CMD_GET(cmd) ((cmd) & 1)
53#define CMD_PUT(cmd) ((cmd) & 2)
"Vladimir N. Oleynik"86ac0722005-10-17 10:47:19 +000054#endif
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +000055/* NB: in the code below
Denis Vlasenkoa04561f2007-05-08 23:12:21 +000056 * CMD_GET(cmd) and CMD_PUT(cmd) are mutually exclusive
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +000057 */
"Vladimir N. Oleynik"86ac0722005-10-17 10:47:19 +000058
Eric Andersen76fa8ea2001-08-20 17:47:49 +000059
Denis Vlasenko04291bc2006-11-21 10:15:25 +000060#if ENABLE_FEATURE_TFTP_BLOCKSIZE
Glenn L McGrathad117d82001-10-05 04:40:37 +000061
Eric Andersenc7bda1c2004-03-15 08:29:22 +000062static int tftp_blocksize_check(int blocksize, int bufsize)
Glenn L McGrathad117d82001-10-05 04:40:37 +000063{
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000064 /* Check if the blocksize is valid:
Glenn L McGrathad117d82001-10-05 04:40:37 +000065 * RFC2348 says between 8 and 65464,
66 * but our implementation makes it impossible
67 * to use blocksizes smaller than 22 octets.
68 */
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +000069 if ((bufsize && (blocksize > bufsize))
Denis Vlasenkoaa9b1822008-03-17 09:10:39 +000070 || (blocksize < 24) || (blocksize > 65564)
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +000071 ) {
Bernhard Reutner-Fischerb25f98a2006-06-10 14:15:03 +000072 bb_error_msg("bad blocksize");
73 return 0;
Glenn L McGrathad117d82001-10-05 04:40:37 +000074 }
Glenn L McGrathad117d82001-10-05 04:40:37 +000075 return blocksize;
76}
77
Denis Vlasenkoa04561f2007-05-08 23:12:21 +000078static char *tftp_option_get(char *buf, int len, const char *option)
Glenn L McGrathad117d82001-10-05 04:40:37 +000079{
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000080 int opt_val = 0;
Glenn L McGrathad117d82001-10-05 04:40:37 +000081 int opt_found = 0;
82 int k;
Eric Andersenc7bda1c2004-03-15 08:29:22 +000083
Denis Vlasenkoaa9b1822008-03-17 09:10:39 +000084 /* buf points to:
85 * "opt_name<NUL>opt_val<NUL>opt_name2<NUL>opt_val2<NUL>..." */
86
Glenn L McGrathad117d82001-10-05 04:40:37 +000087 while (len > 0) {
Denis Vlasenkoaa9b1822008-03-17 09:10:39 +000088 /* Make sure options are terminated correctly */
Bernhard Reutner-Fischerb25f98a2006-06-10 14:15:03 +000089 for (k = 0; k < len; k++) {
90 if (buf[k] == '\0') {
Denis Vlasenkoa04561f2007-05-08 23:12:21 +000091 goto nul_found;
Glenn L McGrathad117d82001-10-05 04:40:37 +000092 }
93 }
Denis Vlasenkoa04561f2007-05-08 23:12:21 +000094 return NULL;
95 nul_found:
Denis Vlasenkoaa9b1822008-03-17 09:10:39 +000096 if (opt_val == 0) { /* it's "name" part */
Glenn L McGrathad117d82001-10-05 04:40:37 +000097 if (strcasecmp(buf, option) == 0) {
Bernhard Reutner-Fischerb25f98a2006-06-10 14:15:03 +000098 opt_found = 1;
Glenn L McGrathad117d82001-10-05 04:40:37 +000099 }
Denis Vlasenkoa04561f2007-05-08 23:12:21 +0000100 } else if (opt_found) {
101 return buf;
Glenn L McGrathad117d82001-10-05 04:40:37 +0000102 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000103
Glenn L McGrathad117d82001-10-05 04:40:37 +0000104 k++;
Glenn L McGrathad117d82001-10-05 04:40:37 +0000105 buf += k;
106 len -= k;
Glenn L McGrathad117d82001-10-05 04:40:37 +0000107 opt_val ^= 1;
108 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000109
Glenn L McGrathad117d82001-10-05 04:40:37 +0000110 return NULL;
111}
112
113#endif
114
Denis Vlasenkoaa9b1822008-03-17 09:10:39 +0000115static int tftp_protocol(
116 USE_GETPUT(int cmd,)
117 len_and_sockaddr *our_lsa,
Denis Vlasenko0850cda2007-02-07 23:20:32 +0000118 len_and_sockaddr *peer_lsa,
Denis Vlasenkoaa9b1822008-03-17 09:10:39 +0000119 USE_TFTP(const char *remote_file,)
120 int local_fd,
121 int blocksize)
Mark Whitley450736c2001-03-02 19:08:50 +0000122{
Denis Vlasenkoaa9b1822008-03-17 09:10:39 +0000123#if !ENABLE_TFTP
124#define remote_file NULL
125#endif
Denis Vlasenko87f3b262007-09-07 13:43:28 +0000126 struct pollfd pfd[1];
Denis Vlasenkoaa9b1822008-03-17 09:10:39 +0000127#define socket_fd (pfd[0].fd)
Bernhard Reutner-Fischer62f98562006-06-10 14:32:56 +0000128 int len;
Denis Vlasenkoa04561f2007-05-08 23:12:21 +0000129 int send_len;
130 USE_FEATURE_TFTP_BLOCKSIZE(smallint want_option_ack = 0;)
131 smallint finished = 0;
132 uint16_t opcode;
Denis Vlasenkoaa9b1822008-03-17 09:10:39 +0000133 uint16_t block_nr;
Denis Vlasenkoa04561f2007-05-08 23:12:21 +0000134 uint16_t recv_blk;
Denis Vlasenko87f3b262007-09-07 13:43:28 +0000135 int retries, waittime_ms;
Denis Vlasenkoaa9b1822008-03-17 09:10:39 +0000136 int tftp_bufsize = blocksize + 4;
Bernhard Reutner-Fischerb25f98a2006-06-10 14:15:03 +0000137 char *cp;
Eric Andersen744a1942001-11-10 11:16:39 +0000138 /* Can't use RESERVE_CONFIG_BUFFER here since the allocation
139 * size varies meaning BUFFERS_GO_ON_STACK would fail */
Denis Vlasenko10f7dd12006-12-17 01:14:08 +0000140 /* 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 Vlasenkoaa9b1822008-03-17 09:10:39 +0000142 char *xbuf = xmalloc(tftp_bufsize);
Denis Vlasenko10f7dd12006-12-17 01:14:08 +0000143 char *rbuf = xmalloc(tftp_bufsize);
Mark Whitley450736c2001-03-02 19:08:50 +0000144
Denis Vlasenkoaa9b1822008-03-17 09:10:39 +0000145 socket_fd = xsocket(peer_lsa->u.sa.sa_family, SOCK_DGRAM, 0);
146 setsockopt_reuseaddr(socket_fd);
Denis Vlasenko6536a9b2007-01-12 10:35:23 +0000147
Denis Vlasenkoaa9b1822008-03-17 09:10:39 +0000148 if (!ENABLE_TFTP || our_lsa) {
149 /* tftpd */
150 block_nr = 0;
Mark Whitley450736c2001-03-02 19:08:50 +0000151
Denis Vlasenkoaa9b1822008-03-17 09:10:39 +0000152 /* 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 McGrathad117d82001-10-05 04:40:37 +0000160
Denis Vlasenko04291bc2006-11-21 10:15:25 +0000161#if ENABLE_FEATURE_TFTP_BLOCKSIZE
Denis Vlasenkoaa9b1822008-03-17 09:10:39 +0000162 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 Vlasenkoa04561f2007-05-08 23:12:21 +0000195 bb_error_msg("remote filename is too long");
196 goto ret;
197 }
Denis Vlasenkoaa9b1822008-03-17 09:10:39 +0000198 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 McGrathad117d82001-10-05 04:40:37 +0000218#endif
Denis Vlasenkoaa9b1822008-03-17 09:10:39 +0000219 /* First packet is built, so skip packet generation */
220 goto send_pkt;
221 }
Mark Whitley450736c2001-03-02 19:08:50 +0000222
Denis Vlasenkobf678d52007-05-09 12:50:08 +0000223 /* Using mostly goto's - continue/break will be less clear
224 * in where we actually jump to */
Denis Vlasenkoa04561f2007-05-08 23:12:21 +0000225 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 Vlasenkoaa9b1822008-03-17 09:10:39 +0000234 len = full_read(local_fd, cp, tftp_bufsize - 4);
Denis Vlasenkoa04561f2007-05-08 23:12:21 +0000235 if (len < 0) {
236 bb_perror_msg(bb_msg_read_error);
237 goto ret;
Mark Whitley450736c2001-03-02 19:08:50 +0000238 }
Denis Vlasenkoa04561f2007-05-08 23:12:21 +0000239 if (len != (tftp_bufsize - 4)) {
240 finished = 1;
241 }
242 cp += len;
Mark Whitley450736c2001-03-02 19:08:50 +0000243 }
Denis Vlasenkoa04561f2007-05-08 23:12:21 +0000244 send_pkt:
245 /* Send packet */
246 *((uint16_t*)xbuf) = htons(opcode); /* fill in opcode part */
Denis Vlasenkobf678d52007-05-09 12:50:08 +0000247 send_len = cp - xbuf;
248 /* NB: send_len value is preserved in code below
249 * for potential resend */
Paul Fox40f0bcf2007-09-06 17:52:22 +0000250
251 retries = TFTP_NUM_RETRIES; /* re-initialize */
Denis Vlasenko87f3b262007-09-07 13:43:28 +0000252 waittime_ms = TFTP_TIMEOUT_MS;
Paul Fox40f0bcf2007-09-06 17:52:22 +0000253
Denis Vlasenkoa04561f2007-05-08 23:12:21 +0000254 send_again:
Denis Vlasenko04291bc2006-11-21 10:15:25 +0000255#if ENABLE_DEBUG_TFTP
Denis Vlasenkobf678d52007-05-09 12:50:08 +0000256 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 Andersen76fa8ea2001-08-20 17:47:49 +0000260#endif
Denis Vlasenkoaa9b1822008-03-17 09:10:39 +0000261 xsendto(socket_fd, xbuf, send_len, &peer_lsa->u.sa, peer_lsa->len);
Denis Vlasenkobf678d52007-05-09 12:50:08 +0000262 /* Was it final ACK? then exit */
263 if (finished && (opcode == TFTP_ACK))
264 goto ret;
Mark Whitley450736c2001-03-02 19:08:50 +0000265
Denis Vlasenko2c916522007-01-12 14:57:37 +0000266 recv_again:
Denis Vlasenkobf678d52007-05-09 12:50:08 +0000267 /* Receive packet */
Denis Vlasenkoaa9b1822008-03-17 09:10:39 +0000268 /*pfd[0].fd = socket_fd;*/
Denis Vlasenko87f3b262007-09-07 13:43:28 +0000269 pfd[0].events = POLLIN;
Denis Vlasenko5d61e712007-09-27 10:09:59 +0000270 switch (safe_poll(pfd, 1, waittime_ms)) {
Denis Vlasenkobf678d52007-05-09 12:50:08 +0000271 case 1:
Denis Vlasenkoaa9b1822008-03-17 09:10:39 +0000272 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 Vlasenkobf678d52007-05-09 12:50:08 +0000287 if (len < 0) {
Denis Vlasenkoaa9b1822008-03-17 09:10:39 +0000288 bb_perror_msg("read");
Denis Vlasenkoa04561f2007-05-08 23:12:21 +0000289 goto ret;
Mark Whitley450736c2001-03-02 19:08:50 +0000290 }
Denis Vlasenkobf678d52007-05-09 12:50:08 +0000291 goto process_pkt;
292 case 0:
Paul Fox40f0bcf2007-09-06 17:52:22 +0000293 retries--;
294 if (retries == 0) {
295 bb_error_msg("timeout");
Denis Vlasenkobf678d52007-05-09 12:50:08 +0000296 goto ret;
297 }
Paul Fox40f0bcf2007-09-06 17:52:22 +0000298
299 /* exponential backoff with limit */
Denis Vlasenko87f3b262007-09-07 13:43:28 +0000300 waittime_ms += waittime_ms/2;
301 if (waittime_ms > TFTP_MAXTIMEOUT_MS) {
302 waittime_ms = TFTP_MAXTIMEOUT_MS;
Paul Fox40f0bcf2007-09-06 17:52:22 +0000303 }
304
Denis Vlasenkobf678d52007-05-09 12:50:08 +0000305 goto send_again; /* resend last sent pkt */
306 default:
Denis Vlasenko5d61e712007-09-27 10:09:59 +0000307 /*bb_perror_msg("poll"); - done in safe_poll */
Denis Vlasenkobf678d52007-05-09 12:50:08 +0000308 goto ret;
309 }
310 process_pkt:
Denis Vlasenkoa04561f2007-05-08 23:12:21 +0000311 /* Process recv'ed packet */
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000312 opcode = ntohs( ((uint16_t*)rbuf)[0] );
Denis Vlasenkoa04561f2007-05-08 23:12:21 +0000313 recv_blk = ntohs( ((uint16_t*)rbuf)[1] );
Mark Whitley450736c2001-03-02 19:08:50 +0000314
Denis Vlasenko04291bc2006-11-21 10:15:25 +0000315#if ENABLE_DEBUG_TFTP
Denis Vlasenkoa04561f2007-05-08 23:12:21 +0000316 fprintf(stderr, "received %d bytes: %04x %04x\n", len, opcode, recv_blk);
Eric Andersen76fa8ea2001-08-20 17:47:49 +0000317#endif
Mark Whitley450736c2001-03-02 19:08:50 +0000318
Glenn L McGrathad117d82001-10-05 04:40:37 +0000319 if (opcode == TFTP_ERROR) {
Denis Vlasenkoa04561f2007-05-08 23:12:21 +0000320 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 Vlasenko6ca409e2007-08-12 20:58:27 +0000329 "bad option"
Denis Vlasenkoa04561f2007-05-08 23:12:21 +0000330 };
Denis Vlasenko80b8b392007-06-25 10:55:35 +0000331
Denis Vlasenkoa04561f2007-05-08 23:12:21 +0000332 const char *msg = "";
Glenn L McGrathad117d82001-10-05 04:40:37 +0000333
Denis Vlasenko10f7dd12006-12-17 01:14:08 +0000334 if (rbuf[4] != '\0') {
335 msg = &rbuf[4];
336 rbuf[tftp_bufsize - 1] = '\0';
Denis Vlasenko80b8b392007-06-25 10:55:35 +0000337 } else if (recv_blk < ARRAY_SIZE(errcode_str)) {
Denis Vlasenkoa04561f2007-05-08 23:12:21 +0000338 msg = errcode_str[recv_blk];
Glenn L McGrathad117d82001-10-05 04:40:37 +0000339 }
Denis Vlasenkoa04561f2007-05-08 23:12:21 +0000340 bb_error_msg("server error: (%u) %s", recv_blk, msg);
341 goto ret;
Glenn L McGrathad117d82001-10-05 04:40:37 +0000342 }
Denis Vlasenkoa04561f2007-05-08 23:12:21 +0000343
Denis Vlasenko04291bc2006-11-21 10:15:25 +0000344#if ENABLE_FEATURE_TFTP_BLOCKSIZE
Glenn L McGrathad117d82001-10-05 04:40:37 +0000345 if (want_option_ack) {
Bernhard Reutner-Fischerb25f98a2006-06-10 14:15:03 +0000346 want_option_ack = 0;
Glenn L McGrathad117d82001-10-05 04:40:37 +0000347
Bernhard Reutner-Fischerb25f98a2006-06-10 14:15:03 +0000348 if (opcode == TFTP_OACK) {
Bernhard Reutner-Fischerb25f98a2006-06-10 14:15:03 +0000349 /* server seems to support options */
Bernhard Reutner-Fischerb25f98a2006-06-10 14:15:03 +0000350 char *res;
Glenn L McGrathad117d82001-10-05 04:40:37 +0000351
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000352 res = tftp_option_get(&rbuf[2], len - 2, "blksize");
Bernhard Reutner-Fischerb25f98a2006-06-10 14:15:03 +0000353 if (res) {
Denis Vlasenko13858992006-10-08 12:49:22 +0000354 int blksize = xatoi_u(res);
Denis Vlasenkoa04561f2007-05-08 23:12:21 +0000355 if (!tftp_blocksize_check(blksize, tftp_bufsize - 4)) {
Denis Vlasenkobf678d52007-05-09 12:50:08 +0000356 /* 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 Vlasenko6ca409e2007-08-12 20:58:27 +0000360 static const uint8_t error_8[4] = { 0,TFTP_ERROR, 0,8 };
Denis Vlasenkoaa9b1822008-03-17 09:10:39 +0000361 xsendto(socket_fd, error_8, 4, &peer_lsa->u.sa, peer_lsa->len);
Denis Vlasenkoa04561f2007-05-08 23:12:21 +0000362 bb_error_msg("server proposes bad blksize %d, exiting", blksize);
Denis Vlasenkoa04561f2007-05-08 23:12:21 +0000363 goto ret;
Bernhard Reutner-Fischerb25f98a2006-06-10 14:15:03 +0000364 }
Denis Vlasenkoa04561f2007-05-08 23:12:21 +0000365#if ENABLE_DEBUG_TFTP
366 fprintf(stderr, "using blksize %u\n",
367 blksize);
368#endif
369 tftp_bufsize = blksize + 4;
Denis Vlasenkobf678d52007-05-09 12:50:08 +0000370 /* Send ACK for OACK ("block" no: 0) */
371 block_nr = 0;
Denis Vlasenkoa04561f2007-05-08 23:12:21 +0000372 continue;
Bernhard Reutner-Fischerb25f98a2006-06-10 14:15:03 +0000373 }
Denis Vlasenkoa04561f2007-05-08 23:12:21 +0000374 /* 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-Fischerb25f98a2006-06-10 14:15:03 +0000378 }
Glenn L McGrathad117d82001-10-05 04:40:37 +0000379
Denis Vlasenkoa04561f2007-05-08 23:12:21 +0000380 bb_error_msg("blksize is not supported by server"
381 " - reverting to 512");
Bernhard Reutner-Fischerb25f98a2006-06-10 14:15:03 +0000382 tftp_bufsize = TFTP_BLOCKSIZE_DEFAULT + 4;
Glenn L McGrathad117d82001-10-05 04:40:37 +0000383 }
384#endif
Denis Vlasenkoa04561f2007-05-08 23:12:21 +0000385 /* block_nr is already advanced to next block# we expect
386 * to get / block# we are about to send next time */
Glenn L McGrathad117d82001-10-05 04:40:37 +0000387
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000388 if (CMD_GET(cmd) && (opcode == TFTP_DATA)) {
Denis Vlasenkoa04561f2007-05-08 23:12:21 +0000389 if (recv_blk == block_nr) {
Denis Vlasenkoaa9b1822008-03-17 09:10:39 +0000390 len = full_write(local_fd, &rbuf[4], len - 4);
Mark Whitley450736c2001-03-02 19:08:50 +0000391 if (len < 0) {
Bernhard Reutner-Fischer1b9d7c92006-06-03 22:45:37 +0000392 bb_perror_msg(bb_msg_write_error);
Denis Vlasenkoa04561f2007-05-08 23:12:21 +0000393 goto ret;
Mark Whitley450736c2001-03-02 19:08:50 +0000394 }
Eric Andersen76fa8ea2001-08-20 17:47:49 +0000395 if (len != (tftp_bufsize - 4)) {
Denis Vlasenkoa04561f2007-05-08 23:12:21 +0000396 finished = 1;
Mark Whitley450736c2001-03-02 19:08:50 +0000397 }
Denis Vlasenkoa04561f2007-05-08 23:12:21 +0000398 continue; /* send ACK */
Mark Whitley450736c2001-03-02 19:08:50 +0000399 }
Denis Vlasenkoa04561f2007-05-08 23:12:21 +0000400 if (recv_blk == (block_nr - 1)) {
Paul Fox1d4c88c2005-07-20 19:49:15 +0000401 /* Server lost our TFTP_ACK. Resend it */
Denis Vlasenkoa04561f2007-05-08 23:12:21 +0000402 block_nr = recv_blk;
Paul Fox1d4c88c2005-07-20 19:49:15 +0000403 continue;
Denis Vlasenko4b924f32007-05-30 00:29:55 +0000404 }
Mark Whitley450736c2001-03-02 19:08:50 +0000405 }
406
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000407 if (CMD_PUT(cmd) && (opcode == TFTP_ACK)) {
Denis Vlasenkoaa9b1822008-03-17 09:10:39 +0000408 /* did peer ACK our last DATA pkt? */
Denis Vlasenkoa04561f2007-05-08 23:12:21 +0000409 if (recv_blk == (uint16_t) (block_nr - 1)) {
410 if (finished)
411 goto ret;
412 continue; /* send next block */
Mark Whitley450736c2001-03-02 19:08:50 +0000413 }
414 }
Denis Vlasenkoa04561f2007-05-08 23:12:21 +0000415 /* 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 Vlasenkoaa9b1822008-03-17 09:10:39 +0000426 } /* end of "while (1)" */
Denis Vlasenkoa04561f2007-05-08 23:12:21 +0000427 ret:
Denis Vlasenko2c916522007-01-12 14:57:37 +0000428 if (ENABLE_FEATURE_CLEAN_UP) {
Denis Vlasenkoaa9b1822008-03-17 09:10:39 +0000429 close(socket_fd);
Denis Vlasenko2c916522007-01-12 14:57:37 +0000430 free(xbuf);
431 free(rbuf);
432 }
Denis Vlasenkoa04561f2007-05-08 23:12:21 +0000433 return finished == 0; /* returns 1 on failure */
Mark Whitley450736c2001-03-02 19:08:50 +0000434}
435
Denis Vlasenkoaa9b1822008-03-17 09:10:39 +0000436#if ENABLE_TFTP
437
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000438int tftp_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenko68404f12008-03-17 09:00:54 +0000439int tftp_main(int argc ATTRIBUTE_UNUSED, char **argv)
Mark Whitley450736c2001-03-02 19:08:50 +0000440{
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000441 len_and_sockaddr *peer_lsa;
Denis Vlasenkoaa9b1822008-03-17 09:10:39 +0000442 const char *local_file = NULL;
443 const char *remote_file = NULL;
Glenn L McGrath036dbaa2004-01-17 05:03:31 +0000444 int port;
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000445 USE_GETPUT(int cmd;)
Denis Vlasenkoaa9b1822008-03-17 09:10:39 +0000446 int local_fd;
Eric Andersen76fa8ea2001-08-20 17:47:49 +0000447 int flags = 0;
Eric Andersen76fa8ea2001-08-20 17:47:49 +0000448 int result;
Glenn L McGrathad117d82001-10-05 04:40:37 +0000449 int blocksize = TFTP_BLOCKSIZE_DEFAULT;
Mark Whitley450736c2001-03-02 19:08:50 +0000450
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000451 /* -p or -g is mandatory, and they are mutually exclusive */
452 opt_complementary = "" USE_FEATURE_TFTP_GET("g:") USE_FEATURE_TFTP_PUT("p:")
Denis Vlasenko1d426652008-03-17 09:09:09 +0000453 USE_GETPUT("g--p:p--g:")
454 USE_FEATURE_TFTP_BLOCKSIZE("b+");
Glenn L McGrathad117d82001-10-05 04:40:37 +0000455
Denis Vlasenkofe7cd642007-08-18 15:32:12 +0000456 USE_GETPUT(cmd =) getopt32(argv,
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000457 USE_FEATURE_TFTP_GET("g") USE_FEATURE_TFTP_PUT("p")
458 "l:r:" USE_FEATURE_TFTP_BLOCKSIZE("b:"),
Denis Vlasenkoaa9b1822008-03-17 09:10:39 +0000459 &local_file, &remote_file
Denis Vlasenko1d426652008-03-17 09:09:09 +0000460 USE_FEATURE_TFTP_BLOCKSIZE(, &blocksize));
Denis Vlasenkoa04561f2007-05-08 23:12:21 +0000461 argv += optind;
Bernhard Reutner-Fischerb25f98a2006-06-10 14:15:03 +0000462
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000463 flags = O_RDONLY;
464 if (CMD_GET(cmd))
Bernhard Reutner-Fischerb25f98a2006-06-10 14:15:03 +0000465 flags = O_WRONLY | O_CREAT | O_TRUNC;
Bernhard Reutner-Fischerb25f98a2006-06-10 14:15:03 +0000466
Denis Vlasenko04291bc2006-11-21 10:15:25 +0000467#if ENABLE_FEATURE_TFTP_BLOCKSIZE
Denis Vlasenko1d426652008-03-17 09:09:09 +0000468 if (!tftp_blocksize_check(blocksize, 0))
469 return EXIT_FAILURE;
"Vladimir N. Oleynik"86ac0722005-10-17 10:47:19 +0000470#endif
Mark Whitley450736c2001-03-02 19:08:50 +0000471
Denis Vlasenkoaa9b1822008-03-17 09:10:39 +0000472 if (!local_file)
473 local_file = remote_file;
474 if (!remote_file)
475 remote_file = local_file;
Denis Vlasenkoa04561f2007-05-08 23:12:21 +0000476 /* Error if filename or host is not known */
Denis Vlasenkoaa9b1822008-03-17 09:10:39 +0000477 if (!remote_file || !argv[0])
Bernhard Reutner-Fischerb25f98a2006-06-10 14:15:03 +0000478 bb_show_usage();
"Vladimir N. Oleynik"86ac0722005-10-17 10:47:19 +0000479
Denis Vlasenkoaa9b1822008-03-17 09:10:39 +0000480 local_fd = CMD_GET(cmd) ? STDOUT_FILENO : STDIN_FILENO;
481 if (!LONE_DASH(local_file)) {
482 local_fd = xopen(local_file, flags);
Mark Whitley450736c2001-03-02 19:08:50 +0000483 }
484
Denis Vlasenkoa04561f2007-05-08 23:12:21 +0000485 port = bb_lookup_port(argv[1], "udp", 69);
486 peer_lsa = xhost2sockaddr(argv[0], port);
Eric Andersen76fa8ea2001-08-20 17:47:49 +0000487
Denis Vlasenko04291bc2006-11-21 10:15:25 +0000488#if ENABLE_DEBUG_TFTP
Denis Vlasenkoaa9b1822008-03-17 09:10:39 +0000489 fprintf(stderr, "using server '%s', remote_file '%s', local_file '%s'\n",
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000490 xmalloc_sockaddr2dotted(&peer_lsa->u.sa),
Denis Vlasenkoaa9b1822008-03-17 09:10:39 +0000491 remote_file, local_file);
Eric Andersen76fa8ea2001-08-20 17:47:49 +0000492#endif
493
Denis Vlasenkoaa9b1822008-03-17 09:10:39 +0000494 result = tftp_protocol(
495 USE_GETPUT(cmd,)
496 NULL /* our_lsa*/,
497 peer_lsa,
498 remote_file, local_fd, blocksize);
Mark Whitley450736c2001-03-02 19:08:50 +0000499
Denis Vlasenkoa04561f2007-05-08 23:12:21 +0000500 if (ENABLE_FEATURE_CLEAN_UP)
Denis Vlasenkoaa9b1822008-03-17 09:10:39 +0000501 close(local_fd);
502 if (result != EXIT_SUCCESS && !LONE_DASH(local_file) && CMD_GET(cmd)) {
503 unlink(local_file);
Eric Andersena66a43e2002-04-13 09:30:25 +0000504 }
Denis Vlasenko000b9ba2006-10-05 23:12:49 +0000505 return result;
Glenn L McGrathad117d82001-10-05 04:40:37 +0000506}
Denis Vlasenko31635552007-01-20 16:54:19 +0000507
Denis Vlasenkoaa9b1822008-03-17 09:10:39 +0000508#endif /* ENABLE_TFTP */
509
510#if ENABLE_TFTPD
511
512/* TODO: libbb candidate? */
513static 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
526int tftpd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
527int 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 Vlasenko31635552007-01-20 16:54:19 +0000610#endif /* ENABLE_FEATURE_TFTP_GET || ENABLE_FEATURE_TFTP_PUT */