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