blob: 618c4062e07234336e09a6689a265763a4d7981c [file] [log] [blame]
Simon Kelleyc49778d2016-01-06 18:52:33 +00001/* dnsmasq is Copyright (c) 2000-2016 Simon Kelley
Simon Kelley832af0b2007-01-21 20:01:28 +00002
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
Simon Kelley824af852008-02-12 20:43:05 +00005 the Free Software Foundation; version 2 dated June, 1991, or
6 (at your option) version 3 dated 29 June, 2007.
7
Simon Kelley832af0b2007-01-21 20:01:28 +00008 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
Simon Kelley824af852008-02-12 20:43:05 +000012
Simon Kelley73a08a22009-02-05 20:28:08 +000013 You should have received a copy of the GNU General Public License
14 along with this program. If not, see <http://www.gnu.org/licenses/>.
Simon Kelley832af0b2007-01-21 20:01:28 +000015*/
16
17#include "dnsmasq.h"
18
19#ifdef HAVE_TFTP
20
Simon Kelley8bc4cec2012-07-03 21:04:11 +010021static struct tftp_file *check_tftp_fileperm(ssize_t *len, char *prefix);
Simon Kelley832af0b2007-01-21 20:01:28 +000022static void free_transfer(struct tftp_transfer *transfer);
23static ssize_t tftp_err(int err, char *packet, char *mess, char *file);
Simon Kelley1b7ecd12007-02-05 14:57:57 +000024static ssize_t tftp_err_oops(char *packet, char *file);
Simon Kelley832af0b2007-01-21 20:01:28 +000025static ssize_t get_block(char *packet, struct tftp_transfer *transfer);
26static char *next(char **p, char *end);
Simon Kelley42fb8152012-04-20 17:15:01 +010027static void sanitise(char *buf);
Simon Kelley832af0b2007-01-21 20:01:28 +000028
29#define OP_RRQ 1
30#define OP_WRQ 2
31#define OP_DATA 3
32#define OP_ACK 4
33#define OP_ERR 5
34#define OP_OACK 6
35
36#define ERR_NOTDEF 0
37#define ERR_FNF 1
38#define ERR_PERM 2
39#define ERR_FULL 3
40#define ERR_ILL 4
41
Simon Kelley5aabfc72007-08-29 11:24:47 +010042void tftp_request(struct listener *listen, time_t now)
Simon Kelley832af0b2007-01-21 20:01:28 +000043{
44 ssize_t len;
45 char *packet = daemon->packet;
46 char *filename, *mode, *p, *end, *opt;
Simon Kelley28866e92011-02-14 20:19:14 +000047 union mysockaddr addr, peer;
Simon Kelley832af0b2007-01-21 20:01:28 +000048 struct msghdr msg;
Simon Kelley832af0b2007-01-21 20:01:28 +000049 struct iovec iov;
Simon Kelley1f15b812009-10-13 17:49:32 +010050 struct ifreq ifr;
Simon Kelley8bc4cec2012-07-03 21:04:11 +010051 int is_err = 1, if_index = 0, mtu = 0;
Simon Kelley832af0b2007-01-21 20:01:28 +000052 struct iname *tmp;
Simon Kelley5aabfc72007-08-29 11:24:47 +010053 struct tftp_transfer *transfer;
Simon Kelley824af852008-02-12 20:43:05 +000054 int port = daemon->start_tftp_port; /* may be zero to use ephemeral port */
55#if defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DONT)
Simon Kelley1f15b812009-10-13 17:49:32 +010056 int mtuflag = IP_PMTUDISC_DONT;
Simon Kelley824af852008-02-12 20:43:05 +000057#endif
Simon Kelley8ef5ada2010-06-03 19:42:45 +010058 char namebuff[IF_NAMESIZE];
Simon Kelley52d4abf2012-03-21 21:39:48 +000059 char *name = NULL;
Simon Kelley8ef5ada2010-06-03 19:42:45 +010060 char *prefix = daemon->tftp_prefix;
61 struct tftp_prefix *pref;
Simon Kelleye25db1f2013-01-29 22:10:26 +000062 struct all_addr addra;
Simon Kelley2329bef2013-12-03 13:41:16 +000063#ifdef HAVE_IPV6
64 /* Can always get recvd interface for IPv6 */
65 int check_dest = !option_bool(OPT_NOWILD) || listen->family == AF_INET6;
66#else
67 int check_dest = !option_bool(OPT_NOWILD);
68#endif
Simon Kelley832af0b2007-01-21 20:01:28 +000069 union {
70 struct cmsghdr align; /* this ensures alignment */
Simon Kelley28866e92011-02-14 20:19:14 +000071#ifdef HAVE_IPV6
72 char control6[CMSG_SPACE(sizeof(struct in6_pktinfo))];
73#endif
Simon Kelley824af852008-02-12 20:43:05 +000074#if defined(HAVE_LINUX_NETWORK)
Simon Kelley832af0b2007-01-21 20:01:28 +000075 char control[CMSG_SPACE(sizeof(struct in_pktinfo))];
Simon Kelley824af852008-02-12 20:43:05 +000076#elif defined(HAVE_SOLARIS_NETWORK)
77 char control[CMSG_SPACE(sizeof(unsigned int))];
Simon Kelley7622fc02009-06-04 20:32:05 +010078#elif defined(IP_RECVDSTADDR) && defined(IP_RECVIF)
Simon Kelley832af0b2007-01-21 20:01:28 +000079 char control[CMSG_SPACE(sizeof(struct sockaddr_dl))];
80#endif
81 } control_u;
82
83 msg.msg_controllen = sizeof(control_u);
84 msg.msg_control = control_u.control;
85 msg.msg_flags = 0;
86 msg.msg_name = &peer;
87 msg.msg_namelen = sizeof(peer);
88 msg.msg_iov = &iov;
89 msg.msg_iovlen = 1;
90
91 iov.iov_base = packet;
92 iov.iov_len = daemon->packet_buff_sz;
93
94 /* we overwrote the buffer... */
95 daemon->srv_save = NULL;
96
97 if ((len = recvmsg(listen->tftpfd, &msg, 0)) < 2)
98 return;
Simon Kelley2329bef2013-12-03 13:41:16 +000099
100 /* Can always get recvd interface for IPv6 */
101 if (!check_dest)
Simon Kelley1f15b812009-10-13 17:49:32 +0100102 {
Simon Kelley52d4abf2012-03-21 21:39:48 +0000103 if (listen->iface)
104 {
105 addr = listen->iface->addr;
Simon Kelley52d4abf2012-03-21 21:39:48 +0000106 name = listen->iface->name;
Simon Kelleybec366b2016-02-24 22:03:26 +0000107 mtu = listen->iface->mtu;
108 if (daemon->tftp_mtu != 0 && daemon->tftp_mtu < mtu)
109 mtu = daemon->tftp_mtu;
Simon Kelley52d4abf2012-03-21 21:39:48 +0000110 }
111 else
112 {
113 /* we're listening on an address that doesn't appear on an interface,
114 ask the kernel what the socket is bound to */
115 socklen_t tcp_len = sizeof(union mysockaddr);
116 if (getsockname(listen->tftpfd, (struct sockaddr *)&addr, &tcp_len) == -1)
117 return;
118 }
Simon Kelley1f15b812009-10-13 17:49:32 +0100119 }
Simon Kelley1b7ecd12007-02-05 14:57:57 +0000120 else
121 {
Simon Kelley7622fc02009-06-04 20:32:05 +0100122 struct cmsghdr *cmptr;
Simon Kelley8ef5ada2010-06-03 19:42:45 +0100123
Simon Kelley28866e92011-02-14 20:19:14 +0000124 if (msg.msg_controllen < sizeof(struct cmsghdr))
125 return;
126
127 addr.sa.sa_family = listen->family;
Simon Kelley1b7ecd12007-02-05 14:57:57 +0000128
Simon Kelley832af0b2007-01-21 20:01:28 +0000129#if defined(HAVE_LINUX_NETWORK)
Simon Kelley28866e92011-02-14 20:19:14 +0000130 if (listen->family == AF_INET)
131 for (cmptr = CMSG_FIRSTHDR(&msg); cmptr; cmptr = CMSG_NXTHDR(&msg, cmptr))
Simon Kelleyc72daea2012-01-05 21:33:27 +0000132 if (cmptr->cmsg_level == IPPROTO_IP && cmptr->cmsg_type == IP_PKTINFO)
Simon Kelley28866e92011-02-14 20:19:14 +0000133 {
134 union {
135 unsigned char *c;
136 struct in_pktinfo *p;
137 } p;
138 p.c = CMSG_DATA(cmptr);
139 addr.in.sin_addr = p.p->ipi_spec_dst;
140 if_index = p.p->ipi_ifindex;
141 }
142
143#elif defined(HAVE_SOLARIS_NETWORK)
144 if (listen->family == AF_INET)
145 for (cmptr = CMSG_FIRSTHDR(&msg); cmptr; cmptr = CMSG_NXTHDR(&msg, cmptr))
Simon Kelley1b7ecd12007-02-05 14:57:57 +0000146 {
Simon Kelley8ef5ada2010-06-03 19:42:45 +0100147 union {
148 unsigned char *c;
Simon Kelley28866e92011-02-14 20:19:14 +0000149 struct in_addr *a;
150 unsigned int *i;
Simon Kelley8ef5ada2010-06-03 19:42:45 +0100151 } p;
152 p.c = CMSG_DATA(cmptr);
Simon Kelley28866e92011-02-14 20:19:14 +0000153 if (cmptr->cmsg_level == IPPROTO_IP && cmptr->cmsg_type == IP_RECVDSTADDR)
154 addr.in.sin_addr = *(p.a);
155 else if (cmptr->cmsg_level == IPPROTO_IP && cmptr->cmsg_type == IP_RECVIF)
156 if_index = *(p.i);
Simon Kelley1b7ecd12007-02-05 14:57:57 +0000157 }
Simon Kelley1b7ecd12007-02-05 14:57:57 +0000158
Simon Kelley832af0b2007-01-21 20:01:28 +0000159#elif defined(IP_RECVDSTADDR) && defined(IP_RECVIF)
Simon Kelley28866e92011-02-14 20:19:14 +0000160 if (listen->family == AF_INET)
161 for (cmptr = CMSG_FIRSTHDR(&msg); cmptr; cmptr = CMSG_NXTHDR(&msg, cmptr))
162 {
163 union {
164 unsigned char *c;
165 struct in_addr *a;
166 struct sockaddr_dl *s;
167 } p;
168 p.c = CMSG_DATA(cmptr);
169 if (cmptr->cmsg_level == IPPROTO_IP && cmptr->cmsg_type == IP_RECVDSTADDR)
170 addr.in.sin_addr = *(p.a);
171 else if (cmptr->cmsg_level == IPPROTO_IP && cmptr->cmsg_type == IP_RECVIF)
172 if_index = p.s->sdl_index;
173 }
Simon Kelley8ef5ada2010-06-03 19:42:45 +0100174
Simon Kelley824af852008-02-12 20:43:05 +0000175#endif
Simon Kelley28866e92011-02-14 20:19:14 +0000176
177#ifdef HAVE_IPV6
178 if (listen->family == AF_INET6)
179 {
180 for (cmptr = CMSG_FIRSTHDR(&msg); cmptr; cmptr = CMSG_NXTHDR(&msg, cmptr))
Simon Kelleyc72daea2012-01-05 21:33:27 +0000181 if (cmptr->cmsg_level == IPPROTO_IPV6 && cmptr->cmsg_type == daemon->v6pktinfo)
Simon Kelley28866e92011-02-14 20:19:14 +0000182 {
183 union {
184 unsigned char *c;
185 struct in6_pktinfo *p;
186 } p;
187 p.c = CMSG_DATA(cmptr);
188
189 addr.in6.sin6_addr = p.p->ipi6_addr;
190 if_index = p.p->ipi6_ifindex;
191 }
192 }
193#endif
Simon Kelley1b7ecd12007-02-05 14:57:57 +0000194
Simon Kelley28866e92011-02-14 20:19:14 +0000195 if (!indextoname(listen->tftpfd, if_index, namebuff))
Simon Kelley1b7ecd12007-02-05 14:57:57 +0000196 return;
Simon Kelley8ef5ada2010-06-03 19:42:45 +0100197
198 name = namebuff;
Simon Kelleye25db1f2013-01-29 22:10:26 +0000199
200 addra.addr.addr4 = addr.in.sin_addr;
Simon Kelley28866e92011-02-14 20:19:14 +0000201
202#ifdef HAVE_IPV6
203 if (listen->family == AF_INET6)
Simon Kelleye25db1f2013-01-29 22:10:26 +0000204 addra.addr.addr6 = addr.in6.sin6_addr;
205#endif
206
Simon Kelley2937f8a2013-07-29 19:49:07 +0100207 if (daemon->tftp_interfaces)
Simon Kelley3169daa2012-08-13 17:39:57 +0100208 {
Simon Kelley2937f8a2013-07-29 19:49:07 +0100209 /* dedicated tftp interface list */
210 for (tmp = daemon->tftp_interfaces; tmp; tmp = tmp->next)
211 if (tmp->name && wildcard_match(tmp->name, name))
212 break;
213
214 if (!tmp)
Simon Kelley3169daa2012-08-13 17:39:57 +0100215 return;
216 }
Simon Kelley2937f8a2013-07-29 19:49:07 +0100217 else
218 {
219 /* Do the same as DHCP */
220 if (!iface_check(listen->family, &addra, name, NULL))
221 {
222 if (!option_bool(OPT_CLEVERBIND))
223 enumerate_interfaces(0);
224 if (!loopback_exception(listen->tftpfd, listen->family, &addra, name) &&
225 !label_exception(if_index, listen->family, &addra) )
226 return;
227 }
228
Simon Kelley8ef5ada2010-06-03 19:42:45 +0100229#ifdef HAVE_DHCP
Simon Kelley2937f8a2013-07-29 19:49:07 +0100230 /* allowed interfaces are the same as for DHCP */
231 for (tmp = daemon->dhcp_except; tmp; tmp = tmp->next)
232 if (tmp->name && wildcard_match(tmp->name, name))
233 return;
Simon Kelley8ef5ada2010-06-03 19:42:45 +0100234#endif
Simon Kelley2937f8a2013-07-29 19:49:07 +0100235 }
236
Simon Kelley316e2732010-01-22 20:16:09 +0000237 strncpy(ifr.ifr_name, name, IF_NAMESIZE);
Simon Kelley1f15b812009-10-13 17:49:32 +0100238 if (ioctl(listen->tftpfd, SIOCGIFMTU, &ifr) != -1)
Simon Kelleybec366b2016-02-24 22:03:26 +0000239 {
240 mtu = ifr.ifr_mtu;
241 if (daemon->tftp_mtu != 0 && daemon->tftp_mtu < mtu)
242 mtu = daemon->tftp_mtu;
243 }
Simon Kelley1b7ecd12007-02-05 14:57:57 +0000244 }
Stefan Tomanek7aa970e2015-04-01 17:55:07 +0100245
Simon Kelleybec366b2016-02-24 22:03:26 +0000246 /* Failed to get interface mtu - can use configured value. */
247 if (mtu == 0)
248 mtu = daemon->tftp_mtu;
249
Simon Kelley52d4abf2012-03-21 21:39:48 +0000250 if (name)
251 {
252 /* check for per-interface prefix */
253 for (pref = daemon->if_prefix; pref; pref = pref->next)
254 if (strcmp(pref->interface, name) == 0)
Simon Kelley8bc4cec2012-07-03 21:04:11 +0100255 prefix = pref->prefix;
Simon Kelley52d4abf2012-03-21 21:39:48 +0000256 }
Simon Kelley8ef5ada2010-06-03 19:42:45 +0100257
Simon Kelley28866e92011-02-14 20:19:14 +0000258 if (listen->family == AF_INET)
Simon Kelley7de060b2011-08-26 17:24:52 +0100259 {
260 addr.in.sin_port = htons(port);
261#ifdef HAVE_SOCKADDR_SA_LEN
262 addr.in.sin_len = sizeof(addr.in);
263#endif
264 }
Simon Kelley28866e92011-02-14 20:19:14 +0000265#ifdef HAVE_IPV6
266 else
267 {
268 addr.in6.sin6_port = htons(port);
269 addr.in6.sin6_flowinfo = 0;
Simon Kelley7de060b2011-08-26 17:24:52 +0100270 addr.in6.sin6_scope_id = 0;
271#ifdef HAVE_SOCKADDR_SA_LEN
272 addr.in6.sin6_len = sizeof(addr.in6);
273#endif
Simon Kelley28866e92011-02-14 20:19:14 +0000274 }
275#endif
276
Simon Kelley5aabfc72007-08-29 11:24:47 +0100277 if (!(transfer = whine_malloc(sizeof(struct tftp_transfer))))
Simon Kelley832af0b2007-01-21 20:01:28 +0000278 return;
279
Simon Kelley28866e92011-02-14 20:19:14 +0000280 if ((transfer->sockfd = socket(listen->family, SOCK_DGRAM, 0)) == -1)
Simon Kelley832af0b2007-01-21 20:01:28 +0000281 {
282 free(transfer);
283 return;
284 }
285
286 transfer->peer = peer;
Simon Kelley5aabfc72007-08-29 11:24:47 +0100287 transfer->timeout = now + 2;
Simon Kelley832af0b2007-01-21 20:01:28 +0000288 transfer->backoff = 1;
289 transfer->block = 1;
290 transfer->blocksize = 512;
Simon Kelley9e038942008-05-30 20:06:34 +0100291 transfer->offset = 0;
Simon Kelley832af0b2007-01-21 20:01:28 +0000292 transfer->file = NULL;
293 transfer->opt_blocksize = transfer->opt_transize = 0;
Simon Kelley9e038942008-05-30 20:06:34 +0100294 transfer->netascii = transfer->carrylf = 0;
Simon Kelley28866e92011-02-14 20:19:14 +0000295
Simon Kelleyc72daea2012-01-05 21:33:27 +0000296 prettyprint_addr(&peer, daemon->addrbuff);
Simon Kelley28866e92011-02-14 20:19:14 +0000297
Simon Kelley824af852008-02-12 20:43:05 +0000298 /* if we have a nailed-down range, iterate until we find a free one. */
299 while (1)
Simon Kelley832af0b2007-01-21 20:01:28 +0000300 {
Simon Kelley7de060b2011-08-26 17:24:52 +0100301 if (bind(transfer->sockfd, &addr.sa, sa_len(&addr)) == -1 ||
Simon Kelley824af852008-02-12 20:43:05 +0000302#if defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DONT)
Simon Kelleyc72daea2012-01-05 21:33:27 +0000303 setsockopt(transfer->sockfd, IPPROTO_IP, IP_MTU_DISCOVER, &mtuflag, sizeof(mtuflag)) == -1 ||
Simon Kelley824af852008-02-12 20:43:05 +0000304#endif
305 !fix_fd(transfer->sockfd))
306 {
307 if (errno == EADDRINUSE && daemon->start_tftp_port != 0)
308 {
309 if (++port <= daemon->end_tftp_port)
310 {
Simon Kelley28866e92011-02-14 20:19:14 +0000311 if (listen->family == AF_INET)
312 addr.in.sin_port = htons(port);
313#ifdef HAVE_IPV6
314 else
315 addr.in6.sin6_port = htons(port);
316#endif
Simon Kelley824af852008-02-12 20:43:05 +0000317 continue;
318 }
Simon Kelley7622fc02009-06-04 20:32:05 +0100319 my_syslog(MS_TFTP | LOG_ERR, _("unable to get free port for TFTP"));
Simon Kelley824af852008-02-12 20:43:05 +0000320 }
321 free_transfer(transfer);
322 return;
323 }
324 break;
Simon Kelley832af0b2007-01-21 20:01:28 +0000325 }
Simon Kelley824af852008-02-12 20:43:05 +0000326
Simon Kelley832af0b2007-01-21 20:01:28 +0000327 p = packet + 2;
328 end = packet + len;
329
330 if (ntohs(*((unsigned short *)packet)) != OP_RRQ ||
331 !(filename = next(&p, end)) ||
332 !(mode = next(&p, end)) ||
Simon Kelley9e038942008-05-30 20:06:34 +0100333 (strcasecmp(mode, "octet") != 0 && strcasecmp(mode, "netascii") != 0))
Simon Kelley6a69ab52012-04-24 14:42:26 +0100334 {
335 len = tftp_err(ERR_ILL, packet, _("unsupported request from %s"), daemon->addrbuff);
336 is_err = 1;
337 }
Simon Kelley832af0b2007-01-21 20:01:28 +0000338 else
339 {
Simon Kelley9e038942008-05-30 20:06:34 +0100340 if (strcasecmp(mode, "netascii") == 0)
341 transfer->netascii = 1;
342
Simon Kelley832af0b2007-01-21 20:01:28 +0000343 while ((opt = next(&p, end)))
344 {
Simon Kelley77e94da2009-08-31 17:32:17 +0100345 if (strcasecmp(opt, "blksize") == 0)
Simon Kelley832af0b2007-01-21 20:01:28 +0000346 {
Simon Kelley8bc4cec2012-07-03 21:04:11 +0100347 if ((opt = next(&p, end)) && !option_bool(OPT_TFTP_NOBLOCK))
Simon Kelley77e94da2009-08-31 17:32:17 +0100348 {
Simon Kelleyd1377fa2016-03-04 21:32:21 +0000349 /* 32 bytes for IP, UDP and TFTP headers, 52 bytes for IPv6 */
350 int overhead = (listen->family == AF_INET) ? 32 : 52;
Simon Kelley77e94da2009-08-31 17:32:17 +0100351 transfer->blocksize = atoi(opt);
352 if (transfer->blocksize < 1)
353 transfer->blocksize = 1;
354 if (transfer->blocksize > (unsigned)daemon->packet_buff_sz - 4)
355 transfer->blocksize = (unsigned)daemon->packet_buff_sz - 4;
Simon Kelleyd1377fa2016-03-04 21:32:21 +0000356 if (mtu != 0 && transfer->blocksize > (unsigned)mtu - overhead)
357 transfer->blocksize = (unsigned)mtu - overhead;
Simon Kelley77e94da2009-08-31 17:32:17 +0100358 transfer->opt_blocksize = 1;
359 transfer->block = 0;
360 }
Simon Kelley832af0b2007-01-21 20:01:28 +0000361 }
Simon Kelley77e94da2009-08-31 17:32:17 +0100362 else if (strcasecmp(opt, "tsize") == 0 && next(&p, end) && !transfer->netascii)
Simon Kelley832af0b2007-01-21 20:01:28 +0000363 {
364 transfer->opt_transize = 1;
365 transfer->block = 0;
366 }
367 }
368
Simon Kelley1f15b812009-10-13 17:49:32 +0100369 /* cope with backslashes from windows boxen. */
Simon Kelley61ce6002012-04-20 21:28:49 +0100370 for (p = filename; *p; p++)
371 if (*p == '\\')
372 *p = '/';
373 else if (option_bool(OPT_TFTP_LC))
374 *p = tolower(*p);
375
Simon Kelleyf2621c72007-04-29 19:47:21 +0100376 strcpy(daemon->namebuff, "/");
Simon Kelley8ef5ada2010-06-03 19:42:45 +0100377 if (prefix)
Simon Kelley832af0b2007-01-21 20:01:28 +0000378 {
Simon Kelley8ef5ada2010-06-03 19:42:45 +0100379 if (prefix[0] == '/')
Simon Kelleyf2621c72007-04-29 19:47:21 +0100380 daemon->namebuff[0] = 0;
Simon Kelley8ef5ada2010-06-03 19:42:45 +0100381 strncat(daemon->namebuff, prefix, (MAXDNAME-1) - strlen(daemon->namebuff));
382 if (prefix[strlen(prefix)-1] != '/')
Simon Kelley77e94da2009-08-31 17:32:17 +0100383 strncat(daemon->namebuff, "/", (MAXDNAME-1) - strlen(daemon->namebuff));
Simon Kelley5aabfc72007-08-29 11:24:47 +0100384
Simon Kelley8bc4cec2012-07-03 21:04:11 +0100385 if (option_bool(OPT_TFTP_APREF))
Simon Kelley5aabfc72007-08-29 11:24:47 +0100386 {
387 size_t oldlen = strlen(daemon->namebuff);
388 struct stat statbuf;
389
Simon Kelleyc72daea2012-01-05 21:33:27 +0000390 strncat(daemon->namebuff, daemon->addrbuff, (MAXDNAME-1) - strlen(daemon->namebuff));
Simon Kelley77e94da2009-08-31 17:32:17 +0100391 strncat(daemon->namebuff, "/", (MAXDNAME-1) - strlen(daemon->namebuff));
Simon Kelley5aabfc72007-08-29 11:24:47 +0100392
393 /* remove unique-directory if it doesn't exist */
394 if (stat(daemon->namebuff, &statbuf) == -1 || !S_ISDIR(statbuf.st_mode))
395 daemon->namebuff[oldlen] = 0;
396 }
397
Simon Kelleyf2621c72007-04-29 19:47:21 +0100398 /* Absolute pathnames OK if they match prefix */
399 if (filename[0] == '/')
400 {
401 if (strstr(filename, daemon->namebuff) == filename)
402 daemon->namebuff[0] = 0;
403 else
404 filename++;
405 }
Simon Kelley832af0b2007-01-21 20:01:28 +0000406 }
Simon Kelleyf2621c72007-04-29 19:47:21 +0100407 else if (filename[0] == '/')
Simon Kelley832af0b2007-01-21 20:01:28 +0000408 daemon->namebuff[0] = 0;
Simon Kelley77e94da2009-08-31 17:32:17 +0100409 strncat(daemon->namebuff, filename, (MAXDNAME-1) - strlen(daemon->namebuff));
Simon Kelley832af0b2007-01-21 20:01:28 +0000410
Simon Kelley5aabfc72007-08-29 11:24:47 +0100411 /* check permissions and open file */
Simon Kelley8bc4cec2012-07-03 21:04:11 +0100412 if ((transfer->file = check_tftp_fileperm(&len, prefix)))
Simon Kelley832af0b2007-01-21 20:01:28 +0000413 {
Simon Kelley1b7ecd12007-02-05 14:57:57 +0000414 if ((len = get_block(packet, transfer)) == -1)
415 len = tftp_err_oops(packet, daemon->namebuff);
416 else
417 is_err = 0;
Simon Kelley832af0b2007-01-21 20:01:28 +0000418 }
419 }
420
421 while (sendto(transfer->sockfd, packet, len, 0,
Simon Kelley04363602012-04-27 10:11:51 +0100422 (struct sockaddr *)&peer, sa_len(&peer)) == -1 && errno == EINTR);
Simon Kelley832af0b2007-01-21 20:01:28 +0000423
424 if (is_err)
425 free_transfer(transfer);
426 else
427 {
Simon Kelley832af0b2007-01-21 20:01:28 +0000428 transfer->next = daemon->tftp_trans;
429 daemon->tftp_trans = transfer;
430 }
431}
Simon Kelley1b7ecd12007-02-05 14:57:57 +0000432
Simon Kelley8bc4cec2012-07-03 21:04:11 +0100433static struct tftp_file *check_tftp_fileperm(ssize_t *len, char *prefix)
Simon Kelley1b7ecd12007-02-05 14:57:57 +0000434{
435 char *packet = daemon->packet, *namebuff = daemon->namebuff;
436 struct tftp_file *file;
Simon Kelley5aabfc72007-08-29 11:24:47 +0100437 struct tftp_transfer *t;
Simon Kelley1b7ecd12007-02-05 14:57:57 +0000438 uid_t uid = geteuid();
439 struct stat statbuf;
Simon Kelley5aabfc72007-08-29 11:24:47 +0100440 int fd = -1;
Simon Kelley1b7ecd12007-02-05 14:57:57 +0000441
442 /* trick to ban moving out of the subtree */
Simon Kelley8ef5ada2010-06-03 19:42:45 +0100443 if (prefix && strstr(namebuff, "/../"))
Simon Kelley5aabfc72007-08-29 11:24:47 +0100444 goto perm;
445
446 if ((fd = open(namebuff, O_RDONLY)) == -1)
Simon Kelley1b7ecd12007-02-05 14:57:57 +0000447 {
Simon Kelley5aabfc72007-08-29 11:24:47 +0100448 if (errno == ENOENT)
449 {
450 *len = tftp_err(ERR_FNF, packet, _("file %s not found"), namebuff);
451 return NULL;
452 }
Simon Kelley1b7ecd12007-02-05 14:57:57 +0000453 else if (errno == EACCES)
454 goto perm;
455 else
456 goto oops;
457 }
Simon Kelley5aabfc72007-08-29 11:24:47 +0100458
459 /* stat the file descriptor to avoid stat->open races */
460 if (fstat(fd, &statbuf) == -1)
461 goto oops;
462
Simon Kelley1b7ecd12007-02-05 14:57:57 +0000463 /* running as root, must be world-readable */
464 if (uid == 0)
465 {
466 if (!(statbuf.st_mode & S_IROTH))
Simon Kelley5aabfc72007-08-29 11:24:47 +0100467 goto perm;
Simon Kelley1b7ecd12007-02-05 14:57:57 +0000468 }
469 /* in secure mode, must be owned by user running dnsmasq */
Simon Kelley8bc4cec2012-07-03 21:04:11 +0100470 else if (option_bool(OPT_TFTP_SECURE) && uid != statbuf.st_uid)
Simon Kelley5aabfc72007-08-29 11:24:47 +0100471 goto perm;
472
473 /* If we're doing many tranfers from the same file, only
474 open it once this saves lots of file descriptors
475 when mass-booting a big cluster, for instance.
476 Be conservative and only share when inode and name match
477 this keeps error messages sane. */
478 for (t = daemon->tftp_trans; t; t = t->next)
479 if (t->file->dev == statbuf.st_dev &&
480 t->file->inode == statbuf.st_ino &&
481 strcmp(t->file->filename, namebuff) == 0)
482 {
483 close(fd);
484 t->file->refcount++;
485 return t->file;
486 }
487
488 if (!(file = whine_malloc(sizeof(struct tftp_file) + strlen(namebuff) + 1)))
Simon Kelley1b7ecd12007-02-05 14:57:57 +0000489 {
490 errno = ENOMEM;
491 goto oops;
492 }
493
Simon Kelley5aabfc72007-08-29 11:24:47 +0100494 file->fd = fd;
Simon Kelley1b7ecd12007-02-05 14:57:57 +0000495 file->size = statbuf.st_size;
Simon Kelley5aabfc72007-08-29 11:24:47 +0100496 file->dev = statbuf.st_dev;
497 file->inode = statbuf.st_ino;
Simon Kelley1b7ecd12007-02-05 14:57:57 +0000498 file->refcount = 1;
499 strcpy(file->filename, namebuff);
500 return file;
Simon Kelley5aabfc72007-08-29 11:24:47 +0100501
Simon Kelley1b7ecd12007-02-05 14:57:57 +0000502 perm:
Simon Kelley5aabfc72007-08-29 11:24:47 +0100503 errno = EACCES;
Simon Kelley1b7ecd12007-02-05 14:57:57 +0000504 *len = tftp_err(ERR_PERM, packet, _("cannot access %s: %s"), namebuff);
Simon Kelley5aabfc72007-08-29 11:24:47 +0100505 if (fd != -1)
506 close(fd);
Simon Kelley1b7ecd12007-02-05 14:57:57 +0000507 return NULL;
508
Simon Kelley5aabfc72007-08-29 11:24:47 +0100509 oops:
Simon Kelley1b7ecd12007-02-05 14:57:57 +0000510 *len = tftp_err_oops(packet, namebuff);
Simon Kelley5aabfc72007-08-29 11:24:47 +0100511 if (fd != -1)
512 close(fd);
Simon Kelley1b7ecd12007-02-05 14:57:57 +0000513 return NULL;
514}
515
Simon Kelleyb842bc92015-07-12 21:09:11 +0100516void check_tftp_listeners(time_t now)
Simon Kelley832af0b2007-01-21 20:01:28 +0000517{
518 struct tftp_transfer *transfer, *tmp, **up;
519 ssize_t len;
520
521 struct ack {
522 unsigned short op, block;
523 } *mess = (struct ack *)daemon->packet;
524
525 /* Check for activity on any existing transfers */
526 for (transfer = daemon->tftp_trans, up = &daemon->tftp_trans; transfer; transfer = tmp)
527 {
528 tmp = transfer->next;
529
Simon Kelley6a69ab52012-04-24 14:42:26 +0100530 prettyprint_addr(&transfer->peer, daemon->addrbuff);
531
Simon Kelleyb842bc92015-07-12 21:09:11 +0100532 if (poll_check(transfer->sockfd, POLLIN))
Simon Kelley832af0b2007-01-21 20:01:28 +0000533 {
534 /* we overwrote the buffer... */
535 daemon->srv_save = NULL;
536
537 if ((len = recv(transfer->sockfd, daemon->packet, daemon->packet_buff_sz, 0)) >= (ssize_t)sizeof(struct ack))
538 {
539 if (ntohs(mess->op) == OP_ACK && ntohs(mess->block) == (unsigned short)transfer->block)
540 {
541 /* Got ack, ensure we take the (re)transmit path */
542 transfer->timeout = now;
543 transfer->backoff = 0;
Simon Kelley9e038942008-05-30 20:06:34 +0100544 if (transfer->block++ != 0)
545 transfer->offset += transfer->blocksize - transfer->expansion;
Simon Kelley832af0b2007-01-21 20:01:28 +0000546 }
547 else if (ntohs(mess->op) == OP_ERR)
548 {
549 char *p = daemon->packet + sizeof(struct ack);
550 char *end = daemon->packet + len;
551 char *err = next(&p, end);
Simon Kelley28866e92011-02-14 20:19:14 +0000552
Simon Kelley832af0b2007-01-21 20:01:28 +0000553 /* Sanitise error message */
554 if (!err)
555 err = "";
556 else
Simon Kelley7a14dfe2012-04-20 20:50:42 +0100557 sanitise(err);
Simon Kelley6a69ab52012-04-24 14:42:26 +0100558
Simon Kelley316e2732010-01-22 20:16:09 +0000559 my_syslog(MS_TFTP | LOG_ERR, _("error %d %s received from %s"),
Simon Kelleyf2621c72007-04-29 19:47:21 +0100560 (int)ntohs(mess->block), err,
Simon Kelleyc72daea2012-01-05 21:33:27 +0000561 daemon->addrbuff);
Simon Kelley832af0b2007-01-21 20:01:28 +0000562
563 /* Got err, ensure we take abort */
564 transfer->timeout = now;
565 transfer->backoff = 100;
566 }
567 }
568 }
569
570 if (difftime(now, transfer->timeout) >= 0.0)
571 {
572 int endcon = 0;
573
574 /* timeout, retransmit */
Simon Kelley5aabfc72007-08-29 11:24:47 +0100575 transfer->timeout += 1 + (1<<transfer->backoff);
Simon Kelley832af0b2007-01-21 20:01:28 +0000576
577 /* we overwrote the buffer... */
578 daemon->srv_save = NULL;
579
580 if ((len = get_block(daemon->packet, transfer)) == -1)
581 {
Simon Kelley1b7ecd12007-02-05 14:57:57 +0000582 len = tftp_err_oops(daemon->packet, transfer->file->filename);
Simon Kelley832af0b2007-01-21 20:01:28 +0000583 endcon = 1;
584 }
Simon Kelley42fb8152012-04-20 17:15:01 +0100585 /* don't complain about timeout when we're awaiting the last
586 ACK, some clients never send it */
Simon Kelleyb4b93082013-06-19 10:31:23 +0100587 else if (++transfer->backoff > 7 && len != 0)
Simon Kelley832af0b2007-01-21 20:01:28 +0000588 {
Simon Kelley42fb8152012-04-20 17:15:01 +0100589 endcon = 1;
590 len = 0;
Simon Kelley832af0b2007-01-21 20:01:28 +0000591 }
Simon Kelley42fb8152012-04-20 17:15:01 +0100592
Simon Kelley832af0b2007-01-21 20:01:28 +0000593 if (len != 0)
594 while(sendto(transfer->sockfd, daemon->packet, len, 0,
Simon Kelley04363602012-04-27 10:11:51 +0100595 (struct sockaddr *)&transfer->peer, sa_len(&transfer->peer)) == -1 && errno == EINTR);
Simon Kelley832af0b2007-01-21 20:01:28 +0000596
597 if (endcon || len == 0)
598 {
Simon Kelley42fb8152012-04-20 17:15:01 +0100599 strcpy(daemon->namebuff, transfer->file->filename);
600 sanitise(daemon->namebuff);
601 my_syslog(MS_TFTP | LOG_INFO, endcon ? _("failed sending %s to %s") : _("sent %s to %s"), daemon->namebuff, daemon->addrbuff);
Simon Kelley832af0b2007-01-21 20:01:28 +0000602 /* unlink */
603 *up = tmp;
Simon Kelley231d0612012-04-27 13:50:45 +0100604 if (endcon)
605 free_transfer(transfer);
606 else
607 {
608 /* put on queue to be sent to script and deleted */
609 transfer->next = daemon->tftp_done_trans;
610 daemon->tftp_done_trans = transfer;
611 }
Simon Kelley832af0b2007-01-21 20:01:28 +0000612 continue;
613 }
614 }
615
616 up = &transfer->next;
Simon Kelley28866e92011-02-14 20:19:14 +0000617 }
Simon Kelley832af0b2007-01-21 20:01:28 +0000618}
619
620static void free_transfer(struct tftp_transfer *transfer)
621{
622 close(transfer->sockfd);
623 if (transfer->file && (--transfer->file->refcount) == 0)
624 {
625 close(transfer->file->fd);
626 free(transfer->file);
627 }
628 free(transfer);
629}
630
631static char *next(char **p, char *end)
632{
633 char *ret = *p;
634 size_t len;
635
636 if (*(end-1) != 0 ||
637 *p == end ||
638 (len = strlen(ret)) == 0)
639 return NULL;
640
641 *p += len + 1;
642 return ret;
643}
644
Simon Kelley42fb8152012-04-20 17:15:01 +0100645static void sanitise(char *buf)
646{
Simon Kelley7a14dfe2012-04-20 20:50:42 +0100647 unsigned char *q, *r;
648 for (q = r = (unsigned char *)buf; *r; r++)
Simon Kelley11263a42012-04-27 14:00:55 +0100649 if (isprint((int)*r))
Simon Kelley7a14dfe2012-04-20 20:50:42 +0100650 *(q++) = *r;
651 *q = 0;
Simon Kelley42fb8152012-04-20 17:15:01 +0100652
Simon Kelley7a14dfe2012-04-20 20:50:42 +0100653}
Simon Kelley42fb8152012-04-20 17:15:01 +0100654
Simon Kelley294d36d2016-07-06 21:30:25 +0100655#define MAXMESSAGE 500 /* limit to make packet < 512 bytes and definitely smaller than buffer */
Simon Kelley832af0b2007-01-21 20:01:28 +0000656static ssize_t tftp_err(int err, char *packet, char *message, char *file)
657{
658 struct errmess {
659 unsigned short op, err;
660 char message[];
661 } *mess = (struct errmess *)packet;
Simon Kelley294d36d2016-07-06 21:30:25 +0100662 ssize_t len, ret = 4;
Simon Kelley832af0b2007-01-21 20:01:28 +0000663 char *errstr = strerror(errno);
Simon Kelley42fb8152012-04-20 17:15:01 +0100664
Simon Kelleyfa785732016-07-22 20:56:01 +0100665 memset(packet, 0, daemon->packet_buff_sz);
Simon Kelley42fb8152012-04-20 17:15:01 +0100666 sanitise(file);
Simon Kelleyfa785732016-07-22 20:56:01 +0100667
Simon Kelley832af0b2007-01-21 20:01:28 +0000668 mess->op = htons(OP_ERR);
669 mess->err = htons(err);
Simon Kelley294d36d2016-07-06 21:30:25 +0100670 len = snprintf(mess->message, MAXMESSAGE, message, file, errstr);
671 ret += (len < MAXMESSAGE) ? len + 1 : MAXMESSAGE; /* include terminating zero */
672
Simon Kelley316e2732010-01-22 20:16:09 +0000673 my_syslog(MS_TFTP | LOG_ERR, "%s", mess->message);
Simon Kelley832af0b2007-01-21 20:01:28 +0000674
675 return ret;
676}
677
Simon Kelley1b7ecd12007-02-05 14:57:57 +0000678static ssize_t tftp_err_oops(char *packet, char *file)
679{
Simon Kelley42fb8152012-04-20 17:15:01 +0100680 /* May have >1 refs to file, so potentially mangle a copy of the name */
681 strcpy(daemon->namebuff, file);
682 return tftp_err(ERR_NOTDEF, packet, _("cannot read %s: %s"), daemon->namebuff);
Simon Kelley1b7ecd12007-02-05 14:57:57 +0000683}
684
Simon Kelley832af0b2007-01-21 20:01:28 +0000685/* return -1 for error, zero for done. */
686static ssize_t get_block(char *packet, struct tftp_transfer *transfer)
687{
Simon Kelleyfa785732016-07-22 20:56:01 +0100688 memset(packet, 0, daemon->packet_buff_sz);
689
Simon Kelley832af0b2007-01-21 20:01:28 +0000690 if (transfer->block == 0)
691 {
692 /* send OACK */
693 char *p;
694 struct oackmess {
695 unsigned short op;
696 char data[];
697 } *mess = (struct oackmess *)packet;
698
699 p = mess->data;
700 mess->op = htons(OP_OACK);
701 if (transfer->opt_blocksize)
702 {
703 p += (sprintf(p, "blksize") + 1);
704 p += (sprintf(p, "%d", transfer->blocksize) + 1);
705 }
706 if (transfer->opt_transize)
707 {
708 p += (sprintf(p,"tsize") + 1);
709 p += (sprintf(p, "%u", (unsigned int)transfer->file->size) + 1);
710 }
711
712 return p - packet;
713 }
714 else
715 {
716 /* send data packet */
717 struct datamess {
718 unsigned short op, block;
719 unsigned char data[];
720 } *mess = (struct datamess *)packet;
721
Simon Kelley9e038942008-05-30 20:06:34 +0100722 size_t size = transfer->file->size - transfer->offset;
Simon Kelley832af0b2007-01-21 20:01:28 +0000723
Simon Kelley9e038942008-05-30 20:06:34 +0100724 if (transfer->offset > transfer->file->size)
Simon Kelley832af0b2007-01-21 20:01:28 +0000725 return 0; /* finished */
726
727 if (size > transfer->blocksize)
728 size = transfer->blocksize;
729
Simon Kelley832af0b2007-01-21 20:01:28 +0000730 mess->op = htons(OP_DATA);
731 mess->block = htons((unsigned short)(transfer->block));
732
Simon Kelley9e038942008-05-30 20:06:34 +0100733 if (lseek(transfer->file->fd, transfer->offset, SEEK_SET) == (off_t)-1 ||
734 !read_write(transfer->file->fd, mess->data, size, 1))
Simon Kelley832af0b2007-01-21 20:01:28 +0000735 return -1;
Simon Kelley9e038942008-05-30 20:06:34 +0100736
737 transfer->expansion = 0;
738
739 /* Map '\n' to CR-LF in netascii mode */
740 if (transfer->netascii)
741 {
742 size_t i;
743 int newcarrylf;
744
745 for (i = 0, newcarrylf = 0; i < size; i++)
746 if (mess->data[i] == '\n' && ( i != 0 || !transfer->carrylf))
747 {
Simon Kelley7de060b2011-08-26 17:24:52 +0100748 transfer->expansion++;
749
750 if (size != transfer->blocksize)
Simon Kelley9e038942008-05-30 20:06:34 +0100751 size++; /* room in this block */
Simon Kelley7de060b2011-08-26 17:24:52 +0100752 else if (i == size - 1)
753 newcarrylf = 1; /* don't expand LF again if it moves to the next block */
754
Simon Kelley9e038942008-05-30 20:06:34 +0100755 /* make space and insert CR */
756 memmove(&mess->data[i+1], &mess->data[i], size - (i + 1));
757 mess->data[i] = '\r';
758
759 i++;
760 }
761 transfer->carrylf = newcarrylf;
762
763 }
764
765 return size + 4;
Simon Kelley832af0b2007-01-21 20:01:28 +0000766 }
767}
768
Simon Kelleya9530962012-03-20 22:07:35 +0000769
770int do_tftp_script_run(void)
771{
772 struct tftp_transfer *transfer;
773
774 if ((transfer = daemon->tftp_done_trans))
775 {
776 daemon->tftp_done_trans = transfer->next;
777#ifdef HAVE_SCRIPT
778 queue_tftp(transfer->file->size, transfer->file->filename, &transfer->peer);
779#endif
780 free_transfer(transfer);
781 return 1;
782 }
783
784 return 0;
785}
Simon Kelley832af0b2007-01-21 20:01:28 +0000786#endif