Simon Kelley | d1ced3a | 2018-01-01 22:18:03 +0000 | [diff] [blame] | 1 | /* dnsmasq is Copyright (c) 2000-2018 Simon Kelley |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 2 | |
| 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 Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 5 | the Free Software Foundation; version 2 dated June, 1991, or |
| 6 | (at your option) version 3 dated 29 June, 2007. |
| 7 | |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 8 | 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 Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 12 | |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame] | 13 | 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 Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 15 | */ |
| 16 | |
| 17 | #include "dnsmasq.h" |
| 18 | |
| 19 | #ifdef HAVE_TFTP |
| 20 | |
Simon Kelley | 8bc4cec | 2012-07-03 21:04:11 +0100 | [diff] [blame] | 21 | static struct tftp_file *check_tftp_fileperm(ssize_t *len, char *prefix); |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 22 | static void free_transfer(struct tftp_transfer *transfer); |
Rosen Penev | 50a2841 | 2017-06-27 22:27:02 +0100 | [diff] [blame] | 23 | static ssize_t tftp_err(int err, char *packet, char *message, char *file); |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 24 | static ssize_t tftp_err_oops(char *packet, char *file); |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 25 | static ssize_t get_block(char *packet, struct tftp_transfer *transfer); |
| 26 | static char *next(char **p, char *end); |
Simon Kelley | 42fb815 | 2012-04-20 17:15:01 +0100 | [diff] [blame] | 27 | static void sanitise(char *buf); |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 28 | |
| 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 Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 42 | void tftp_request(struct listener *listen, time_t now) |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 43 | { |
| 44 | ssize_t len; |
| 45 | char *packet = daemon->packet; |
| 46 | char *filename, *mode, *p, *end, *opt; |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 47 | union mysockaddr addr, peer; |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 48 | struct msghdr msg; |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 49 | struct iovec iov; |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 50 | struct ifreq ifr; |
Simon Kelley | 8bc4cec | 2012-07-03 21:04:11 +0100 | [diff] [blame] | 51 | int is_err = 1, if_index = 0, mtu = 0; |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 52 | struct iname *tmp; |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 53 | struct tftp_transfer *transfer; |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 54 | int port = daemon->start_tftp_port; /* may be zero to use ephemeral port */ |
| 55 | #if defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DONT) |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 56 | int mtuflag = IP_PMTUDISC_DONT; |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 57 | #endif |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 58 | char namebuff[IF_NAMESIZE]; |
Simon Kelley | 52d4abf | 2012-03-21 21:39:48 +0000 | [diff] [blame] | 59 | char *name = NULL; |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 60 | char *prefix = daemon->tftp_prefix; |
| 61 | struct tftp_prefix *pref; |
Simon Kelley | e25db1f | 2013-01-29 22:10:26 +0000 | [diff] [blame] | 62 | struct all_addr addra; |
Simon Kelley | 2329bef | 2013-12-03 13:41:16 +0000 | [diff] [blame] | 63 | #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 Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 69 | union { |
| 70 | struct cmsghdr align; /* this ensures alignment */ |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 71 | #ifdef HAVE_IPV6 |
| 72 | char control6[CMSG_SPACE(sizeof(struct in6_pktinfo))]; |
| 73 | #endif |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 74 | #if defined(HAVE_LINUX_NETWORK) |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 75 | char control[CMSG_SPACE(sizeof(struct in_pktinfo))]; |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 76 | #elif defined(HAVE_SOLARIS_NETWORK) |
| 77 | char control[CMSG_SPACE(sizeof(unsigned int))]; |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 78 | #elif defined(IP_RECVDSTADDR) && defined(IP_RECVIF) |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 79 | 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 Kelley | 2329bef | 2013-12-03 13:41:16 +0000 | [diff] [blame] | 99 | |
| 100 | /* Can always get recvd interface for IPv6 */ |
| 101 | if (!check_dest) |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 102 | { |
Simon Kelley | 52d4abf | 2012-03-21 21:39:48 +0000 | [diff] [blame] | 103 | if (listen->iface) |
| 104 | { |
| 105 | addr = listen->iface->addr; |
Simon Kelley | 52d4abf | 2012-03-21 21:39:48 +0000 | [diff] [blame] | 106 | name = listen->iface->name; |
Simon Kelley | bec366b | 2016-02-24 22:03:26 +0000 | [diff] [blame] | 107 | mtu = listen->iface->mtu; |
| 108 | if (daemon->tftp_mtu != 0 && daemon->tftp_mtu < mtu) |
| 109 | mtu = daemon->tftp_mtu; |
Simon Kelley | 52d4abf | 2012-03-21 21:39:48 +0000 | [diff] [blame] | 110 | } |
| 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 Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 119 | } |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 120 | else |
| 121 | { |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 122 | struct cmsghdr *cmptr; |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 123 | |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 124 | if (msg.msg_controllen < sizeof(struct cmsghdr)) |
| 125 | return; |
| 126 | |
| 127 | addr.sa.sa_family = listen->family; |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 128 | |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 129 | #if defined(HAVE_LINUX_NETWORK) |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 130 | if (listen->family == AF_INET) |
| 131 | for (cmptr = CMSG_FIRSTHDR(&msg); cmptr; cmptr = CMSG_NXTHDR(&msg, cmptr)) |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 132 | if (cmptr->cmsg_level == IPPROTO_IP && cmptr->cmsg_type == IP_PKTINFO) |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 133 | { |
| 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 Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 146 | { |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 147 | union { |
| 148 | unsigned char *c; |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 149 | struct in_addr *a; |
| 150 | unsigned int *i; |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 151 | } p; |
| 152 | p.c = CMSG_DATA(cmptr); |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 153 | 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 Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 157 | } |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 158 | |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 159 | #elif defined(IP_RECVDSTADDR) && defined(IP_RECVIF) |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 160 | 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 Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 174 | |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 175 | #endif |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 176 | |
| 177 | #ifdef HAVE_IPV6 |
| 178 | if (listen->family == AF_INET6) |
| 179 | { |
| 180 | for (cmptr = CMSG_FIRSTHDR(&msg); cmptr; cmptr = CMSG_NXTHDR(&msg, cmptr)) |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 181 | if (cmptr->cmsg_level == IPPROTO_IPV6 && cmptr->cmsg_type == daemon->v6pktinfo) |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 182 | { |
| 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 Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 194 | |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 195 | if (!indextoname(listen->tftpfd, if_index, namebuff)) |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 196 | return; |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 197 | |
| 198 | name = namebuff; |
Simon Kelley | e25db1f | 2013-01-29 22:10:26 +0000 | [diff] [blame] | 199 | |
| 200 | addra.addr.addr4 = addr.in.sin_addr; |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 201 | |
| 202 | #ifdef HAVE_IPV6 |
| 203 | if (listen->family == AF_INET6) |
Simon Kelley | e25db1f | 2013-01-29 22:10:26 +0000 | [diff] [blame] | 204 | addra.addr.addr6 = addr.in6.sin6_addr; |
| 205 | #endif |
| 206 | |
Simon Kelley | 2937f8a | 2013-07-29 19:49:07 +0100 | [diff] [blame] | 207 | if (daemon->tftp_interfaces) |
Simon Kelley | 3169daa | 2012-08-13 17:39:57 +0100 | [diff] [blame] | 208 | { |
Simon Kelley | 2937f8a | 2013-07-29 19:49:07 +0100 | [diff] [blame] | 209 | /* 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 Kelley | 3169daa | 2012-08-13 17:39:57 +0100 | [diff] [blame] | 215 | return; |
| 216 | } |
Simon Kelley | 2937f8a | 2013-07-29 19:49:07 +0100 | [diff] [blame] | 217 | 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 Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 229 | #ifdef HAVE_DHCP |
Simon Kelley | 2937f8a | 2013-07-29 19:49:07 +0100 | [diff] [blame] | 230 | /* 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 Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 234 | #endif |
Simon Kelley | 2937f8a | 2013-07-29 19:49:07 +0100 | [diff] [blame] | 235 | } |
| 236 | |
Simon Kelley | 316e273 | 2010-01-22 20:16:09 +0000 | [diff] [blame] | 237 | strncpy(ifr.ifr_name, name, IF_NAMESIZE); |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 238 | if (ioctl(listen->tftpfd, SIOCGIFMTU, &ifr) != -1) |
Simon Kelley | bec366b | 2016-02-24 22:03:26 +0000 | [diff] [blame] | 239 | { |
| 240 | mtu = ifr.ifr_mtu; |
| 241 | if (daemon->tftp_mtu != 0 && daemon->tftp_mtu < mtu) |
| 242 | mtu = daemon->tftp_mtu; |
| 243 | } |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 244 | } |
Stefan Tomanek | 7aa970e | 2015-04-01 17:55:07 +0100 | [diff] [blame] | 245 | |
Simon Kelley | bec366b | 2016-02-24 22:03:26 +0000 | [diff] [blame] | 246 | /* Failed to get interface mtu - can use configured value. */ |
| 247 | if (mtu == 0) |
| 248 | mtu = daemon->tftp_mtu; |
| 249 | |
Simon Kelley | 52d4abf | 2012-03-21 21:39:48 +0000 | [diff] [blame] | 250 | 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 Kelley | 8bc4cec | 2012-07-03 21:04:11 +0100 | [diff] [blame] | 255 | prefix = pref->prefix; |
Simon Kelley | 52d4abf | 2012-03-21 21:39:48 +0000 | [diff] [blame] | 256 | } |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 257 | |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 258 | if (listen->family == AF_INET) |
Simon Kelley | 7de060b | 2011-08-26 17:24:52 +0100 | [diff] [blame] | 259 | { |
| 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 Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 265 | #ifdef HAVE_IPV6 |
| 266 | else |
| 267 | { |
| 268 | addr.in6.sin6_port = htons(port); |
| 269 | addr.in6.sin6_flowinfo = 0; |
Simon Kelley | 7de060b | 2011-08-26 17:24:52 +0100 | [diff] [blame] | 270 | addr.in6.sin6_scope_id = 0; |
| 271 | #ifdef HAVE_SOCKADDR_SA_LEN |
| 272 | addr.in6.sin6_len = sizeof(addr.in6); |
| 273 | #endif |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 274 | } |
| 275 | #endif |
| 276 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 277 | if (!(transfer = whine_malloc(sizeof(struct tftp_transfer)))) |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 278 | return; |
| 279 | |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 280 | if ((transfer->sockfd = socket(listen->family, SOCK_DGRAM, 0)) == -1) |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 281 | { |
| 282 | free(transfer); |
| 283 | return; |
| 284 | } |
| 285 | |
| 286 | transfer->peer = peer; |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 287 | transfer->timeout = now + 2; |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 288 | transfer->backoff = 1; |
| 289 | transfer->block = 1; |
| 290 | transfer->blocksize = 512; |
Simon Kelley | 9e03894 | 2008-05-30 20:06:34 +0100 | [diff] [blame] | 291 | transfer->offset = 0; |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 292 | transfer->file = NULL; |
| 293 | transfer->opt_blocksize = transfer->opt_transize = 0; |
Simon Kelley | 9e03894 | 2008-05-30 20:06:34 +0100 | [diff] [blame] | 294 | transfer->netascii = transfer->carrylf = 0; |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 295 | |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 296 | prettyprint_addr(&peer, daemon->addrbuff); |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 297 | |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 298 | /* if we have a nailed-down range, iterate until we find a free one. */ |
| 299 | while (1) |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 300 | { |
Simon Kelley | 7de060b | 2011-08-26 17:24:52 +0100 | [diff] [blame] | 301 | if (bind(transfer->sockfd, &addr.sa, sa_len(&addr)) == -1 || |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 302 | #if defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DONT) |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 303 | setsockopt(transfer->sockfd, IPPROTO_IP, IP_MTU_DISCOVER, &mtuflag, sizeof(mtuflag)) == -1 || |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 304 | #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 Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 311 | 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 Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 317 | continue; |
| 318 | } |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 319 | my_syslog(MS_TFTP | LOG_ERR, _("unable to get free port for TFTP")); |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 320 | } |
| 321 | free_transfer(transfer); |
| 322 | return; |
| 323 | } |
| 324 | break; |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 325 | } |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 326 | |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 327 | 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 Kelley | 9e03894 | 2008-05-30 20:06:34 +0100 | [diff] [blame] | 333 | (strcasecmp(mode, "octet") != 0 && strcasecmp(mode, "netascii") != 0)) |
Simon Kelley | 6a69ab5 | 2012-04-24 14:42:26 +0100 | [diff] [blame] | 334 | { |
| 335 | len = tftp_err(ERR_ILL, packet, _("unsupported request from %s"), daemon->addrbuff); |
| 336 | is_err = 1; |
| 337 | } |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 338 | else |
| 339 | { |
Simon Kelley | 9e03894 | 2008-05-30 20:06:34 +0100 | [diff] [blame] | 340 | if (strcasecmp(mode, "netascii") == 0) |
| 341 | transfer->netascii = 1; |
| 342 | |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 343 | while ((opt = next(&p, end))) |
| 344 | { |
Simon Kelley | 77e94da | 2009-08-31 17:32:17 +0100 | [diff] [blame] | 345 | if (strcasecmp(opt, "blksize") == 0) |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 346 | { |
Simon Kelley | 8bc4cec | 2012-07-03 21:04:11 +0100 | [diff] [blame] | 347 | if ((opt = next(&p, end)) && !option_bool(OPT_TFTP_NOBLOCK)) |
Simon Kelley | 77e94da | 2009-08-31 17:32:17 +0100 | [diff] [blame] | 348 | { |
Simon Kelley | d1377fa | 2016-03-04 21:32:21 +0000 | [diff] [blame] | 349 | /* 32 bytes for IP, UDP and TFTP headers, 52 bytes for IPv6 */ |
| 350 | int overhead = (listen->family == AF_INET) ? 32 : 52; |
Simon Kelley | 77e94da | 2009-08-31 17:32:17 +0100 | [diff] [blame] | 351 | 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 Kelley | d1377fa | 2016-03-04 21:32:21 +0000 | [diff] [blame] | 356 | if (mtu != 0 && transfer->blocksize > (unsigned)mtu - overhead) |
| 357 | transfer->blocksize = (unsigned)mtu - overhead; |
Simon Kelley | 77e94da | 2009-08-31 17:32:17 +0100 | [diff] [blame] | 358 | transfer->opt_blocksize = 1; |
| 359 | transfer->block = 0; |
| 360 | } |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 361 | } |
Simon Kelley | 77e94da | 2009-08-31 17:32:17 +0100 | [diff] [blame] | 362 | else if (strcasecmp(opt, "tsize") == 0 && next(&p, end) && !transfer->netascii) |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 363 | { |
| 364 | transfer->opt_transize = 1; |
| 365 | transfer->block = 0; |
| 366 | } |
| 367 | } |
| 368 | |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 369 | /* cope with backslashes from windows boxen. */ |
Simon Kelley | 61ce600 | 2012-04-20 21:28:49 +0100 | [diff] [blame] | 370 | for (p = filename; *p; p++) |
| 371 | if (*p == '\\') |
| 372 | *p = '/'; |
| 373 | else if (option_bool(OPT_TFTP_LC)) |
| 374 | *p = tolower(*p); |
| 375 | |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 376 | strcpy(daemon->namebuff, "/"); |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 377 | if (prefix) |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 378 | { |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 379 | if (prefix[0] == '/') |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 380 | daemon->namebuff[0] = 0; |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 381 | strncat(daemon->namebuff, prefix, (MAXDNAME-1) - strlen(daemon->namebuff)); |
| 382 | if (prefix[strlen(prefix)-1] != '/') |
Simon Kelley | 77e94da | 2009-08-31 17:32:17 +0100 | [diff] [blame] | 383 | strncat(daemon->namebuff, "/", (MAXDNAME-1) - strlen(daemon->namebuff)); |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 384 | |
Floris Bos | 60704f5 | 2017-04-09 22:22:49 +0100 | [diff] [blame] | 385 | if (option_bool(OPT_TFTP_APREF_IP)) |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 386 | { |
| 387 | size_t oldlen = strlen(daemon->namebuff); |
| 388 | struct stat statbuf; |
| 389 | |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 390 | strncat(daemon->namebuff, daemon->addrbuff, (MAXDNAME-1) - strlen(daemon->namebuff)); |
Simon Kelley | 77e94da | 2009-08-31 17:32:17 +0100 | [diff] [blame] | 391 | strncat(daemon->namebuff, "/", (MAXDNAME-1) - strlen(daemon->namebuff)); |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 392 | |
| 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 | } |
Floris Bos | 503c609 | 2017-04-09 23:07:13 +0100 | [diff] [blame] | 397 | |
Floris Bos | 60704f5 | 2017-04-09 22:22:49 +0100 | [diff] [blame] | 398 | if (option_bool(OPT_TFTP_APREF_MAC)) |
| 399 | { |
| 400 | unsigned char *macaddr = NULL; |
| 401 | unsigned char macbuf[DHCP_CHADDR_MAX]; |
Floris Bos | 503c609 | 2017-04-09 23:07:13 +0100 | [diff] [blame] | 402 | |
Floris Bos | 60704f5 | 2017-04-09 22:22:49 +0100 | [diff] [blame] | 403 | #ifdef HAVE_DHCP |
| 404 | if (daemon->dhcp && peer.sa.sa_family == AF_INET) |
| 405 | { |
| 406 | /* Check if the client IP is in our lease database */ |
| 407 | struct dhcp_lease *lease = lease_find_by_addr(peer.in.sin_addr); |
| 408 | if (lease && lease->hwaddr_type == ARPHRD_ETHER && lease->hwaddr_len == ETHER_ADDR_LEN) |
| 409 | macaddr = lease->hwaddr; |
| 410 | } |
| 411 | #endif |
Floris Bos | 503c609 | 2017-04-09 23:07:13 +0100 | [diff] [blame] | 412 | |
Floris Bos | 60704f5 | 2017-04-09 22:22:49 +0100 | [diff] [blame] | 413 | /* If no luck, try to find in ARP table. This only works if client is in same (V)LAN */ |
| 414 | if (!macaddr && find_mac(&peer, macbuf, 1, now) > 0) |
Floris Bos | 503c609 | 2017-04-09 23:07:13 +0100 | [diff] [blame] | 415 | macaddr = macbuf; |
| 416 | |
Floris Bos | 60704f5 | 2017-04-09 22:22:49 +0100 | [diff] [blame] | 417 | if (macaddr) |
| 418 | { |
| 419 | size_t oldlen = strlen(daemon->namebuff); |
| 420 | struct stat statbuf; |
| 421 | |
| 422 | snprintf(daemon->namebuff + oldlen, (MAXDNAME-1) - oldlen, "%.2x-%.2x-%.2x-%.2x-%.2x-%.2x/", |
| 423 | macaddr[0], macaddr[1], macaddr[2], macaddr[3], macaddr[4], macaddr[5]); |
Floris Bos | 503c609 | 2017-04-09 23:07:13 +0100 | [diff] [blame] | 424 | |
Floris Bos | 60704f5 | 2017-04-09 22:22:49 +0100 | [diff] [blame] | 425 | /* remove unique-directory if it doesn't exist */ |
| 426 | if (stat(daemon->namebuff, &statbuf) == -1 || !S_ISDIR(statbuf.st_mode)) |
| 427 | daemon->namebuff[oldlen] = 0; |
| 428 | } |
| 429 | } |
Floris Bos | 503c609 | 2017-04-09 23:07:13 +0100 | [diff] [blame] | 430 | |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 431 | /* Absolute pathnames OK if they match prefix */ |
| 432 | if (filename[0] == '/') |
| 433 | { |
| 434 | if (strstr(filename, daemon->namebuff) == filename) |
| 435 | daemon->namebuff[0] = 0; |
| 436 | else |
| 437 | filename++; |
| 438 | } |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 439 | } |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 440 | else if (filename[0] == '/') |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 441 | daemon->namebuff[0] = 0; |
Simon Kelley | 77e94da | 2009-08-31 17:32:17 +0100 | [diff] [blame] | 442 | strncat(daemon->namebuff, filename, (MAXDNAME-1) - strlen(daemon->namebuff)); |
Floris Bos | 503c609 | 2017-04-09 23:07:13 +0100 | [diff] [blame] | 443 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 444 | /* check permissions and open file */ |
Simon Kelley | 8bc4cec | 2012-07-03 21:04:11 +0100 | [diff] [blame] | 445 | if ((transfer->file = check_tftp_fileperm(&len, prefix))) |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 446 | { |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 447 | if ((len = get_block(packet, transfer)) == -1) |
| 448 | len = tftp_err_oops(packet, daemon->namebuff); |
| 449 | else |
| 450 | is_err = 0; |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 451 | } |
| 452 | } |
| 453 | |
| 454 | while (sendto(transfer->sockfd, packet, len, 0, |
Simon Kelley | 0436360 | 2012-04-27 10:11:51 +0100 | [diff] [blame] | 455 | (struct sockaddr *)&peer, sa_len(&peer)) == -1 && errno == EINTR); |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 456 | |
| 457 | if (is_err) |
| 458 | free_transfer(transfer); |
| 459 | else |
| 460 | { |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 461 | transfer->next = daemon->tftp_trans; |
| 462 | daemon->tftp_trans = transfer; |
| 463 | } |
| 464 | } |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 465 | |
Simon Kelley | 8bc4cec | 2012-07-03 21:04:11 +0100 | [diff] [blame] | 466 | static struct tftp_file *check_tftp_fileperm(ssize_t *len, char *prefix) |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 467 | { |
| 468 | char *packet = daemon->packet, *namebuff = daemon->namebuff; |
| 469 | struct tftp_file *file; |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 470 | struct tftp_transfer *t; |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 471 | uid_t uid = geteuid(); |
| 472 | struct stat statbuf; |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 473 | int fd = -1; |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 474 | |
| 475 | /* trick to ban moving out of the subtree */ |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 476 | if (prefix && strstr(namebuff, "/../")) |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 477 | goto perm; |
| 478 | |
| 479 | if ((fd = open(namebuff, O_RDONLY)) == -1) |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 480 | { |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 481 | if (errno == ENOENT) |
| 482 | { |
| 483 | *len = tftp_err(ERR_FNF, packet, _("file %s not found"), namebuff); |
| 484 | return NULL; |
| 485 | } |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 486 | else if (errno == EACCES) |
| 487 | goto perm; |
| 488 | else |
| 489 | goto oops; |
| 490 | } |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 491 | |
| 492 | /* stat the file descriptor to avoid stat->open races */ |
| 493 | if (fstat(fd, &statbuf) == -1) |
| 494 | goto oops; |
| 495 | |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 496 | /* running as root, must be world-readable */ |
| 497 | if (uid == 0) |
| 498 | { |
| 499 | if (!(statbuf.st_mode & S_IROTH)) |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 500 | goto perm; |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 501 | } |
| 502 | /* in secure mode, must be owned by user running dnsmasq */ |
Simon Kelley | 8bc4cec | 2012-07-03 21:04:11 +0100 | [diff] [blame] | 503 | else if (option_bool(OPT_TFTP_SECURE) && uid != statbuf.st_uid) |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 504 | goto perm; |
| 505 | |
Josh Soref | 730c674 | 2017-02-06 16:14:04 +0000 | [diff] [blame] | 506 | /* If we're doing many transfers from the same file, only |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 507 | open it once this saves lots of file descriptors |
| 508 | when mass-booting a big cluster, for instance. |
| 509 | Be conservative and only share when inode and name match |
| 510 | this keeps error messages sane. */ |
| 511 | for (t = daemon->tftp_trans; t; t = t->next) |
| 512 | if (t->file->dev == statbuf.st_dev && |
| 513 | t->file->inode == statbuf.st_ino && |
| 514 | strcmp(t->file->filename, namebuff) == 0) |
| 515 | { |
| 516 | close(fd); |
| 517 | t->file->refcount++; |
| 518 | return t->file; |
| 519 | } |
| 520 | |
| 521 | if (!(file = whine_malloc(sizeof(struct tftp_file) + strlen(namebuff) + 1))) |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 522 | { |
| 523 | errno = ENOMEM; |
| 524 | goto oops; |
| 525 | } |
| 526 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 527 | file->fd = fd; |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 528 | file->size = statbuf.st_size; |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 529 | file->dev = statbuf.st_dev; |
| 530 | file->inode = statbuf.st_ino; |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 531 | file->refcount = 1; |
| 532 | strcpy(file->filename, namebuff); |
| 533 | return file; |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 534 | |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 535 | perm: |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 536 | errno = EACCES; |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 537 | *len = tftp_err(ERR_PERM, packet, _("cannot access %s: %s"), namebuff); |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 538 | if (fd != -1) |
| 539 | close(fd); |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 540 | return NULL; |
| 541 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 542 | oops: |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 543 | *len = tftp_err_oops(packet, namebuff); |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 544 | if (fd != -1) |
| 545 | close(fd); |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 546 | return NULL; |
| 547 | } |
| 548 | |
Simon Kelley | b842bc9 | 2015-07-12 21:09:11 +0100 | [diff] [blame] | 549 | void check_tftp_listeners(time_t now) |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 550 | { |
| 551 | struct tftp_transfer *transfer, *tmp, **up; |
| 552 | ssize_t len; |
| 553 | |
| 554 | struct ack { |
| 555 | unsigned short op, block; |
| 556 | } *mess = (struct ack *)daemon->packet; |
| 557 | |
| 558 | /* Check for activity on any existing transfers */ |
| 559 | for (transfer = daemon->tftp_trans, up = &daemon->tftp_trans; transfer; transfer = tmp) |
| 560 | { |
| 561 | tmp = transfer->next; |
| 562 | |
Simon Kelley | 6a69ab5 | 2012-04-24 14:42:26 +0100 | [diff] [blame] | 563 | prettyprint_addr(&transfer->peer, daemon->addrbuff); |
| 564 | |
Simon Kelley | b842bc9 | 2015-07-12 21:09:11 +0100 | [diff] [blame] | 565 | if (poll_check(transfer->sockfd, POLLIN)) |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 566 | { |
| 567 | /* we overwrote the buffer... */ |
| 568 | daemon->srv_save = NULL; |
| 569 | |
| 570 | if ((len = recv(transfer->sockfd, daemon->packet, daemon->packet_buff_sz, 0)) >= (ssize_t)sizeof(struct ack)) |
| 571 | { |
| 572 | if (ntohs(mess->op) == OP_ACK && ntohs(mess->block) == (unsigned short)transfer->block) |
| 573 | { |
| 574 | /* Got ack, ensure we take the (re)transmit path */ |
| 575 | transfer->timeout = now; |
| 576 | transfer->backoff = 0; |
Simon Kelley | 9e03894 | 2008-05-30 20:06:34 +0100 | [diff] [blame] | 577 | if (transfer->block++ != 0) |
| 578 | transfer->offset += transfer->blocksize - transfer->expansion; |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 579 | } |
| 580 | else if (ntohs(mess->op) == OP_ERR) |
| 581 | { |
| 582 | char *p = daemon->packet + sizeof(struct ack); |
| 583 | char *end = daemon->packet + len; |
| 584 | char *err = next(&p, end); |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 585 | |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 586 | /* Sanitise error message */ |
| 587 | if (!err) |
| 588 | err = ""; |
| 589 | else |
Simon Kelley | 7a14dfe | 2012-04-20 20:50:42 +0100 | [diff] [blame] | 590 | sanitise(err); |
Simon Kelley | 6a69ab5 | 2012-04-24 14:42:26 +0100 | [diff] [blame] | 591 | |
Simon Kelley | 316e273 | 2010-01-22 20:16:09 +0000 | [diff] [blame] | 592 | my_syslog(MS_TFTP | LOG_ERR, _("error %d %s received from %s"), |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 593 | (int)ntohs(mess->block), err, |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 594 | daemon->addrbuff); |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 595 | |
| 596 | /* Got err, ensure we take abort */ |
| 597 | transfer->timeout = now; |
| 598 | transfer->backoff = 100; |
| 599 | } |
| 600 | } |
| 601 | } |
| 602 | |
| 603 | if (difftime(now, transfer->timeout) >= 0.0) |
| 604 | { |
| 605 | int endcon = 0; |
| 606 | |
| 607 | /* timeout, retransmit */ |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 608 | transfer->timeout += 1 + (1<<transfer->backoff); |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 609 | |
| 610 | /* we overwrote the buffer... */ |
| 611 | daemon->srv_save = NULL; |
| 612 | |
| 613 | if ((len = get_block(daemon->packet, transfer)) == -1) |
| 614 | { |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 615 | len = tftp_err_oops(daemon->packet, transfer->file->filename); |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 616 | endcon = 1; |
| 617 | } |
Simon Kelley | 42fb815 | 2012-04-20 17:15:01 +0100 | [diff] [blame] | 618 | /* don't complain about timeout when we're awaiting the last |
| 619 | ACK, some clients never send it */ |
Simon Kelley | b4b9308 | 2013-06-19 10:31:23 +0100 | [diff] [blame] | 620 | else if (++transfer->backoff > 7 && len != 0) |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 621 | { |
Simon Kelley | 42fb815 | 2012-04-20 17:15:01 +0100 | [diff] [blame] | 622 | endcon = 1; |
| 623 | len = 0; |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 624 | } |
Simon Kelley | 42fb815 | 2012-04-20 17:15:01 +0100 | [diff] [blame] | 625 | |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 626 | if (len != 0) |
| 627 | while(sendto(transfer->sockfd, daemon->packet, len, 0, |
Simon Kelley | 0436360 | 2012-04-27 10:11:51 +0100 | [diff] [blame] | 628 | (struct sockaddr *)&transfer->peer, sa_len(&transfer->peer)) == -1 && errno == EINTR); |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 629 | |
| 630 | if (endcon || len == 0) |
| 631 | { |
Simon Kelley | 42fb815 | 2012-04-20 17:15:01 +0100 | [diff] [blame] | 632 | strcpy(daemon->namebuff, transfer->file->filename); |
| 633 | sanitise(daemon->namebuff); |
| 634 | my_syslog(MS_TFTP | LOG_INFO, endcon ? _("failed sending %s to %s") : _("sent %s to %s"), daemon->namebuff, daemon->addrbuff); |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 635 | /* unlink */ |
| 636 | *up = tmp; |
Simon Kelley | 231d061 | 2012-04-27 13:50:45 +0100 | [diff] [blame] | 637 | if (endcon) |
| 638 | free_transfer(transfer); |
| 639 | else |
| 640 | { |
| 641 | /* put on queue to be sent to script and deleted */ |
| 642 | transfer->next = daemon->tftp_done_trans; |
| 643 | daemon->tftp_done_trans = transfer; |
| 644 | } |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 645 | continue; |
| 646 | } |
| 647 | } |
| 648 | |
| 649 | up = &transfer->next; |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 650 | } |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 651 | } |
| 652 | |
| 653 | static void free_transfer(struct tftp_transfer *transfer) |
| 654 | { |
| 655 | close(transfer->sockfd); |
| 656 | if (transfer->file && (--transfer->file->refcount) == 0) |
| 657 | { |
| 658 | close(transfer->file->fd); |
| 659 | free(transfer->file); |
| 660 | } |
| 661 | free(transfer); |
| 662 | } |
| 663 | |
| 664 | static char *next(char **p, char *end) |
| 665 | { |
| 666 | char *ret = *p; |
| 667 | size_t len; |
| 668 | |
| 669 | if (*(end-1) != 0 || |
| 670 | *p == end || |
| 671 | (len = strlen(ret)) == 0) |
| 672 | return NULL; |
| 673 | |
| 674 | *p += len + 1; |
| 675 | return ret; |
| 676 | } |
| 677 | |
Simon Kelley | 42fb815 | 2012-04-20 17:15:01 +0100 | [diff] [blame] | 678 | static void sanitise(char *buf) |
| 679 | { |
Simon Kelley | 7a14dfe | 2012-04-20 20:50:42 +0100 | [diff] [blame] | 680 | unsigned char *q, *r; |
| 681 | for (q = r = (unsigned char *)buf; *r; r++) |
Simon Kelley | 11263a4 | 2012-04-27 14:00:55 +0100 | [diff] [blame] | 682 | if (isprint((int)*r)) |
Simon Kelley | 7a14dfe | 2012-04-20 20:50:42 +0100 | [diff] [blame] | 683 | *(q++) = *r; |
| 684 | *q = 0; |
Simon Kelley | 42fb815 | 2012-04-20 17:15:01 +0100 | [diff] [blame] | 685 | |
Simon Kelley | 7a14dfe | 2012-04-20 20:50:42 +0100 | [diff] [blame] | 686 | } |
Simon Kelley | 42fb815 | 2012-04-20 17:15:01 +0100 | [diff] [blame] | 687 | |
Simon Kelley | 294d36d | 2016-07-06 21:30:25 +0100 | [diff] [blame] | 688 | #define MAXMESSAGE 500 /* limit to make packet < 512 bytes and definitely smaller than buffer */ |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 689 | static ssize_t tftp_err(int err, char *packet, char *message, char *file) |
| 690 | { |
| 691 | struct errmess { |
| 692 | unsigned short op, err; |
| 693 | char message[]; |
| 694 | } *mess = (struct errmess *)packet; |
Simon Kelley | 294d36d | 2016-07-06 21:30:25 +0100 | [diff] [blame] | 695 | ssize_t len, ret = 4; |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 696 | char *errstr = strerror(errno); |
Simon Kelley | 42fb815 | 2012-04-20 17:15:01 +0100 | [diff] [blame] | 697 | |
Simon Kelley | fa78573 | 2016-07-22 20:56:01 +0100 | [diff] [blame] | 698 | memset(packet, 0, daemon->packet_buff_sz); |
Simon Kelley | 42fb815 | 2012-04-20 17:15:01 +0100 | [diff] [blame] | 699 | sanitise(file); |
Simon Kelley | fa78573 | 2016-07-22 20:56:01 +0100 | [diff] [blame] | 700 | |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 701 | mess->op = htons(OP_ERR); |
| 702 | mess->err = htons(err); |
Simon Kelley | 294d36d | 2016-07-06 21:30:25 +0100 | [diff] [blame] | 703 | len = snprintf(mess->message, MAXMESSAGE, message, file, errstr); |
| 704 | ret += (len < MAXMESSAGE) ? len + 1 : MAXMESSAGE; /* include terminating zero */ |
| 705 | |
Simon Kelley | 316e273 | 2010-01-22 20:16:09 +0000 | [diff] [blame] | 706 | my_syslog(MS_TFTP | LOG_ERR, "%s", mess->message); |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 707 | |
| 708 | return ret; |
| 709 | } |
| 710 | |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 711 | static ssize_t tftp_err_oops(char *packet, char *file) |
| 712 | { |
Simon Kelley | 42fb815 | 2012-04-20 17:15:01 +0100 | [diff] [blame] | 713 | /* May have >1 refs to file, so potentially mangle a copy of the name */ |
| 714 | strcpy(daemon->namebuff, file); |
| 715 | return tftp_err(ERR_NOTDEF, packet, _("cannot read %s: %s"), daemon->namebuff); |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 716 | } |
| 717 | |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 718 | /* return -1 for error, zero for done. */ |
| 719 | static ssize_t get_block(char *packet, struct tftp_transfer *transfer) |
| 720 | { |
Simon Kelley | fa78573 | 2016-07-22 20:56:01 +0100 | [diff] [blame] | 721 | memset(packet, 0, daemon->packet_buff_sz); |
| 722 | |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 723 | if (transfer->block == 0) |
| 724 | { |
| 725 | /* send OACK */ |
| 726 | char *p; |
| 727 | struct oackmess { |
| 728 | unsigned short op; |
| 729 | char data[]; |
| 730 | } *mess = (struct oackmess *)packet; |
| 731 | |
| 732 | p = mess->data; |
| 733 | mess->op = htons(OP_OACK); |
| 734 | if (transfer->opt_blocksize) |
| 735 | { |
| 736 | p += (sprintf(p, "blksize") + 1); |
Rosen Penev | cbd29e5 | 2017-06-27 22:29:51 +0100 | [diff] [blame] | 737 | p += (sprintf(p, "%u", transfer->blocksize) + 1); |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 738 | } |
| 739 | if (transfer->opt_transize) |
| 740 | { |
| 741 | p += (sprintf(p,"tsize") + 1); |
| 742 | p += (sprintf(p, "%u", (unsigned int)transfer->file->size) + 1); |
| 743 | } |
| 744 | |
| 745 | return p - packet; |
| 746 | } |
| 747 | else |
| 748 | { |
| 749 | /* send data packet */ |
| 750 | struct datamess { |
| 751 | unsigned short op, block; |
| 752 | unsigned char data[]; |
| 753 | } *mess = (struct datamess *)packet; |
| 754 | |
Simon Kelley | 9e03894 | 2008-05-30 20:06:34 +0100 | [diff] [blame] | 755 | size_t size = transfer->file->size - transfer->offset; |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 756 | |
Simon Kelley | 9e03894 | 2008-05-30 20:06:34 +0100 | [diff] [blame] | 757 | if (transfer->offset > transfer->file->size) |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 758 | return 0; /* finished */ |
| 759 | |
| 760 | if (size > transfer->blocksize) |
| 761 | size = transfer->blocksize; |
| 762 | |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 763 | mess->op = htons(OP_DATA); |
| 764 | mess->block = htons((unsigned short)(transfer->block)); |
| 765 | |
Simon Kelley | 9e03894 | 2008-05-30 20:06:34 +0100 | [diff] [blame] | 766 | if (lseek(transfer->file->fd, transfer->offset, SEEK_SET) == (off_t)-1 || |
| 767 | !read_write(transfer->file->fd, mess->data, size, 1)) |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 768 | return -1; |
Simon Kelley | 9e03894 | 2008-05-30 20:06:34 +0100 | [diff] [blame] | 769 | |
| 770 | transfer->expansion = 0; |
| 771 | |
| 772 | /* Map '\n' to CR-LF in netascii mode */ |
| 773 | if (transfer->netascii) |
| 774 | { |
| 775 | size_t i; |
| 776 | int newcarrylf; |
| 777 | |
| 778 | for (i = 0, newcarrylf = 0; i < size; i++) |
| 779 | if (mess->data[i] == '\n' && ( i != 0 || !transfer->carrylf)) |
| 780 | { |
Simon Kelley | 7de060b | 2011-08-26 17:24:52 +0100 | [diff] [blame] | 781 | transfer->expansion++; |
| 782 | |
| 783 | if (size != transfer->blocksize) |
Simon Kelley | 9e03894 | 2008-05-30 20:06:34 +0100 | [diff] [blame] | 784 | size++; /* room in this block */ |
Simon Kelley | 7de060b | 2011-08-26 17:24:52 +0100 | [diff] [blame] | 785 | else if (i == size - 1) |
| 786 | newcarrylf = 1; /* don't expand LF again if it moves to the next block */ |
| 787 | |
Simon Kelley | 9e03894 | 2008-05-30 20:06:34 +0100 | [diff] [blame] | 788 | /* make space and insert CR */ |
| 789 | memmove(&mess->data[i+1], &mess->data[i], size - (i + 1)); |
| 790 | mess->data[i] = '\r'; |
| 791 | |
| 792 | i++; |
| 793 | } |
| 794 | transfer->carrylf = newcarrylf; |
| 795 | |
| 796 | } |
| 797 | |
| 798 | return size + 4; |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 799 | } |
| 800 | } |
| 801 | |
Simon Kelley | a953096 | 2012-03-20 22:07:35 +0000 | [diff] [blame] | 802 | |
| 803 | int do_tftp_script_run(void) |
| 804 | { |
| 805 | struct tftp_transfer *transfer; |
| 806 | |
| 807 | if ((transfer = daemon->tftp_done_trans)) |
| 808 | { |
| 809 | daemon->tftp_done_trans = transfer->next; |
| 810 | #ifdef HAVE_SCRIPT |
| 811 | queue_tftp(transfer->file->size, transfer->file->filename, &transfer->peer); |
| 812 | #endif |
| 813 | free_transfer(transfer); |
| 814 | return 1; |
| 815 | } |
| 816 | |
| 817 | return 0; |
| 818 | } |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 819 | #endif |