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