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