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