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