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