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