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