1 /* $KAME: rtsol.c,v 1.27 2003/10/05 00:09:36 itojun Exp $ */
4 * SPDX-License-Identifier: BSD-3-Clause
6 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
7 * Copyright (C) 2011 Hiroki Sato
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the project nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 #include <sys/param.h>
38 #include <sys/capsicum.h>
39 #include <sys/queue.h>
40 #include <sys/socket.h>
46 #include <net/route.h>
47 #include <net/if_dl.h>
49 #define __BSD_VISIBLE 1 /* IN6ADDR_LINKLOCAL_ALLROUTERS_INIT */
50 #include <netinet/in.h>
52 #include <netinet/ip6.h>
53 #include <netinet6/ip6_var.h>
54 #include <netinet/icmp6.h>
56 #include <arpa/inet.h>
58 #include <capsicum_helpers.h>
72 static char rsid[IFNAMSIZ + 1 + sizeof(DNSINFO_ORIGIN_LABEL) + 1 + NI_MAXHOST];
73 struct ifinfo_head_t ifinfo_head = TAILQ_HEAD_INITIALIZER(ifinfo_head);
75 static void call_script(const char *const *, struct script_msg_head_t *);
76 static size_t dname_labeldec(char *, size_t, const char *);
77 static struct ra_opt *find_raopt(struct rainfo *, int, void *, size_t);
78 static int ra_opt_rdnss_dispatch(struct ifinfo *, struct rainfo *,
79 struct script_msg_head_t *, struct script_msg_head_t *);
80 static char *make_rsid(const char *, const char *, struct rainfo *);
82 #define _ARGS_MANAGED managedconf_script, ifi->ifname
83 #define _ARGS_OTHER otherconf_script, ifi->ifname
84 #define _ARGS_RESADD resolvconf_script, "-a", rsid
85 #define _ARGS_RESDEL resolvconf_script, "-d", rsid
87 #define CALL_SCRIPT(name, sm_head) do { \
88 const char *const sarg[] = { _ARGS_##name, NULL }; \
89 call_script(sarg, sm_head); \
92 #define ELM_MALLOC(p, error_action) do { \
93 p = malloc(sizeof(*p)); \
95 warnmsg(LOG_ERR, __func__, "malloc failed: %s", \
99 memset(p, 0, sizeof(*p)); \
105 struct icmp6_filter filt;
109 if ((sock = socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6)) < 0) {
110 warnmsg(LOG_ERR, __func__, "socket: %s", strerror(errno));
114 /* Provide info about the receiving interface. */
116 if (setsockopt(sock, IPPROTO_IPV6, IPV6_RECVPKTINFO, &on,
118 warnmsg(LOG_ERR, __func__, "setsockopt(IPV6_RECVPKTINFO): %s",
123 /* Include the hop limit from the received header. */
125 if (setsockopt(sock, IPPROTO_IPV6, IPV6_RECVHOPLIMIT, &on,
127 warnmsg(LOG_ERR, __func__, "setsockopt(IPV6_RECVHOPLIMIT): %s",
132 /* Filter out everything except for Router Advertisements. */
133 ICMP6_FILTER_SETBLOCKALL(&filt);
134 ICMP6_FILTER_SETPASS(ND_ROUTER_ADVERT, &filt);
135 if (setsockopt(sock, IPPROTO_ICMPV6, ICMP6_FILTER, &filt,
136 sizeof(filt)) == -1) {
137 warnmsg(LOG_ERR, __func__, "setsockopt(ICMP6_FILTER): %s",
142 cap_rights_init(&rights, CAP_EVENT, CAP_RECV);
143 if (caph_rights_limit(sock, &rights) < 0) {
144 warnmsg(LOG_ERR, __func__, "caph_rights_limit(): %s",
158 rtsol_input(int sock)
160 uint8_t cmsg[CMSG_SPACE(sizeof(struct in6_pktinfo)) +
161 CMSG_SPACE(sizeof(int))];
164 struct sockaddr_in6 from;
165 char answer[1500], ntopbuf[INET6_ADDRSTRLEN], ifnamebuf[IFNAMSIZ];
166 int l, ifindex = 0, *hlimp = NULL;
168 struct in6_pktinfo *pi = NULL;
169 struct ifinfo *ifi = NULL;
170 struct ra_opt *rao = NULL;
171 struct icmp6_hdr *icp;
172 struct nd_router_advert *nd_ra;
176 struct in6_addr *addr;
177 struct nd_opt_hdr *ndo;
178 struct nd_opt_rdnss *rdnss;
179 struct nd_opt_dnssl *dnssl;
181 char nsbuf[INET6_ADDRSTRLEN + 1 + IFNAMSIZ + 1];
182 char dname[NI_MAXHOST];
183 struct timespec lifetime, now;
184 int newent_rai, newent_rao;
186 memset(&hdr, 0, sizeof(hdr));
189 hdr.msg_name = &from;
190 hdr.msg_namelen = sizeof(from);
191 hdr.msg_control = cmsg;
192 hdr.msg_controllen = sizeof(cmsg);
194 iov.iov_base = (caddr_t)answer;
195 iov.iov_len = sizeof(answer);
197 if ((msglen = recvmsg(sock, &hdr, 0)) < 0) {
198 warnmsg(LOG_ERR, __func__, "recvmsg: %s", strerror(errno));
202 /* Extract control message info. */
203 for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(&hdr); cm != NULL;
204 cm = (struct cmsghdr *)CMSG_NXTHDR(&hdr, cm)) {
205 if (cm->cmsg_level == IPPROTO_IPV6 &&
206 cm->cmsg_type == IPV6_PKTINFO &&
207 cm->cmsg_len == CMSG_LEN(sizeof(struct in6_pktinfo))) {
208 pi = (struct in6_pktinfo *)(void *)(CMSG_DATA(cm));
209 ifindex = pi->ipi6_ifindex;
211 if (cm->cmsg_level == IPPROTO_IPV6 &&
212 cm->cmsg_type == IPV6_HOPLIMIT &&
213 cm->cmsg_len == CMSG_LEN(sizeof(int)))
214 hlimp = (int *)(void *)CMSG_DATA(cm);
218 warnmsg(LOG_ERR, __func__,
219 "failed to get receiving interface");
223 warnmsg(LOG_ERR, __func__,
224 "failed to get receiving hop limit");
228 if ((size_t)msglen < sizeof(struct nd_router_advert)) {
229 warnmsg(LOG_INFO, __func__,
230 "packet size(%zd) is too short", msglen);
234 icp = (struct icmp6_hdr *)iov.iov_base;
235 if (icp->icmp6_type != ND_ROUTER_ADVERT) {
237 * this should not happen because we configured a filter
238 * that only passes RAs on the receiving socket.
240 warnmsg(LOG_ERR, __func__,
241 "invalid icmp type(%d) from %s on %s", icp->icmp6_type,
242 inet_ntop(AF_INET6, &from.sin6_addr, ntopbuf,
244 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
248 if (icp->icmp6_code != 0) {
249 warnmsg(LOG_INFO, __func__,
250 "invalid icmp code(%d) from %s on %s", icp->icmp6_code,
251 inet_ntop(AF_INET6, &from.sin6_addr, ntopbuf,
253 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
258 warnmsg(LOG_INFO, __func__,
259 "invalid RA with hop limit(%d) from %s on %s",
261 inet_ntop(AF_INET6, &from.sin6_addr, ntopbuf,
263 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
267 if (pi && !IN6_IS_ADDR_LINKLOCAL(&from.sin6_addr)) {
268 warnmsg(LOG_INFO, __func__,
269 "invalid RA with non link-local source from %s on %s",
270 inet_ntop(AF_INET6, &from.sin6_addr, ntopbuf,
272 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
276 /* xxx: more validation? */
278 if ((ifi = find_ifinfo(pi->ipi6_ifindex)) == NULL) {
279 warnmsg(LOG_DEBUG, __func__,
280 "received RA from %s on an unexpected IF(%s)",
281 inet_ntop(AF_INET6, &from.sin6_addr, ntopbuf,
283 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
287 warnmsg(LOG_DEBUG, __func__,
288 "received RA from %s on %s, state is %d",
289 inet_ntop(AF_INET6, &from.sin6_addr, ntopbuf, sizeof(ntopbuf)),
290 ifi->ifname, ifi->state);
292 nd_ra = (struct nd_router_advert *)icp;
295 * Process the "M bit."
296 * If the value of ManagedConfigFlag changes from FALSE to TRUE, the
297 * host should invoke the stateful autoconfiguration protocol,
298 * requesting information.
299 * [RFC 4861 Section 4.2]
300 * XXX ??? [draft-ietf-v6ops-dhcpv6-slaac-problem-07]
302 if (((nd_ra->nd_ra_flags_reserved) & ND_RA_FLAG_MANAGED) &&
303 !ifi->managedconfig) {
304 warnmsg(LOG_DEBUG, __func__,
305 "ManagedConfigFlag on %s is turned on", ifi->ifname);
306 ifi->managedconfig = 1;
307 CALL_SCRIPT(MANAGED, NULL);
311 * Process the "O bit."
312 * If the value of OtherConfigFlag changes from FALSE to TRUE, the
313 * host should invoke the stateful autoconfiguration protocol,
314 * requesting information unless the "M bit" was set as well in
315 * which case the "O bit" is redundant.
316 * [RFC 4861 Section 4.2]
318 if (((nd_ra->nd_ra_flags_reserved) & ND_RA_FLAG_OTHER) &&
320 warnmsg(LOG_DEBUG, __func__,
321 "OtherConfigFlag on %s is turned on", ifi->ifname);
322 ifi->otherconfig = 1;
323 if (!ifi->managedconfig)
324 CALL_SCRIPT(OTHER, NULL);
326 clock_gettime(CLOCK_MONOTONIC_FAST, &now);
328 rai = find_rainfo(ifi, &from);
330 ELM_MALLOC(rai, exit(1));
331 rai->rai_ifinfo = ifi;
332 TAILQ_INIT(&rai->rai_ra_opt);
333 rai->rai_saddr.sin6_family = AF_INET6;
334 rai->rai_saddr.sin6_len = sizeof(rai->rai_saddr);
335 memcpy(&rai->rai_saddr.sin6_addr, &from.sin6_addr,
336 sizeof(rai->rai_saddr.sin6_addr));
340 #define RA_OPT_NEXT_HDR(x) (struct nd_opt_hdr *)((char *)(x) + \
341 (((struct nd_opt_hdr *)(x))->nd_opt_len * 8))
342 /* Process RA options. */
343 warnmsg(LOG_DEBUG, __func__, "Processing RA");
344 raoptp = (char *)icp + sizeof(struct nd_router_advert);
345 while (raoptp < (char *)icp + msglen) {
346 ndo = (struct nd_opt_hdr *)raoptp;
347 warnmsg(LOG_DEBUG, __func__, "ndo = %p", raoptp);
348 warnmsg(LOG_DEBUG, __func__, "ndo->nd_opt_type = %d",
350 warnmsg(LOG_DEBUG, __func__, "ndo->nd_opt_len = %d",
353 if (ndo->nd_opt_len == 0) {
354 warnmsg(LOG_INFO, __func__, "invalid option length 0.");
357 if ((char *)RA_OPT_NEXT_HDR(raoptp) > (char *)icp + msglen) {
358 warnmsg(LOG_INFO, __func__, "option length overflow.");
362 switch (ndo->nd_opt_type) {
364 rdnss = (struct nd_opt_rdnss *)raoptp;
366 /* Optlen sanity check (Section 5.3.1 in RFC 6106) */
367 if (rdnss->nd_opt_rdnss_len < 3) {
368 warnmsg(LOG_INFO, __func__,
369 "too short RDNSS option"
370 "in RA from %s was ignored.",
371 inet_ntop(AF_INET6, &from.sin6_addr,
372 ntopbuf, sizeof(ntopbuf)));
376 addr = (struct in6_addr *)(void *)(raoptp + sizeof(*rdnss));
377 while ((char *)addr < (char *)RA_OPT_NEXT_HDR(raoptp)) {
378 if (inet_ntop(AF_INET6, addr, ntopbuf,
379 sizeof(ntopbuf)) == NULL) {
380 warnmsg(LOG_INFO, __func__,
381 "an invalid address in RDNSS option"
382 " in RA from %s was ignored.",
383 inet_ntop(AF_INET6, &from.sin6_addr,
384 ntopbuf, sizeof(ntopbuf)));
388 if (IN6_IS_ADDR_LINKLOCAL(addr))
389 /* XXX: % has to be escaped here */
390 l = snprintf(nsbuf, sizeof(nsbuf),
395 l = snprintf(nsbuf, sizeof(nsbuf),
397 if (l < 0 || (size_t)l >= sizeof(nsbuf)) {
398 warnmsg(LOG_ERR, __func__,
399 "address copying error in "
400 "RDNSS option: %d.", l);
404 warnmsg(LOG_DEBUG, __func__, "nsbuf = %s",
408 rao = find_raopt(rai, ndo->nd_opt_type, nsbuf,
411 ELM_MALLOC(rao, break);
412 rao->rao_type = ndo->nd_opt_type;
413 rao->rao_len = strlen(nsbuf);
414 rao->rao_msg = strdup(nsbuf);
415 if (rao->rao_msg == NULL) {
416 warnmsg(LOG_ERR, __func__,
425 /* Set expiration timer */
426 memset(&rao->rao_expire, 0,
427 sizeof(rao->rao_expire));
428 memset(&lifetime, 0, sizeof(lifetime));
430 ntohl(rdnss->nd_opt_rdnss_lifetime);
431 TS_ADD(&now, &lifetime, &rao->rao_expire);
434 TAILQ_INSERT_TAIL(&rai->rai_ra_opt,
440 dnssl = (struct nd_opt_dnssl *)raoptp;
442 /* Optlen sanity check (Section 5.3.1 in RFC 6106) */
443 if (dnssl->nd_opt_dnssl_len < 2) {
444 warnmsg(LOG_INFO, __func__,
445 "too short DNSSL option"
446 "in RA from %s was ignored.",
447 inet_ntop(AF_INET6, &from.sin6_addr,
448 ntopbuf, sizeof(ntopbuf)));
453 * Ensure NUL-termination in DNSSL in case of
456 p = (char *)RA_OPT_NEXT_HDR(raoptp);
459 p = raoptp + sizeof(*dnssl);
460 while (1 < (len = dname_labeldec(dname, sizeof(dname),
462 /* length == 1 means empty string */
463 warnmsg(LOG_DEBUG, __func__, "dname = %s",
467 rao = find_raopt(rai, ndo->nd_opt_type, dname,
470 ELM_MALLOC(rao, break);
471 rao->rao_type = ndo->nd_opt_type;
472 rao->rao_len = strlen(dname);
473 rao->rao_msg = strdup(dname);
474 if (rao->rao_msg == NULL) {
475 warnmsg(LOG_ERR, __func__,
484 /* Set expiration timer */
485 memset(&rao->rao_expire, 0,
486 sizeof(rao->rao_expire));
487 memset(&lifetime, 0, sizeof(lifetime));
489 ntohl(dnssl->nd_opt_dnssl_lifetime);
490 TS_ADD(&now, &lifetime, &rao->rao_expire);
493 TAILQ_INSERT_TAIL(&rai->rai_ra_opt,
499 /* nothing to do for other options */
502 raoptp = (char *)RA_OPT_NEXT_HDR(raoptp);
505 TAILQ_INSERT_TAIL(&ifi->ifi_rainfo, rai, rai_next);
510 switch (ifi->state) {
511 case IFS_IDLE: /* should be ignored */
512 case IFS_DELAY: /* right? */
515 ifi->state = IFS_IDLE;
517 rtsol_timer_update(ifi);
522 static char resstr_ns_prefix[] = "nameserver ";
523 static char resstr_sh_prefix[] = "search ";
524 static char resstr_nl[] = "\n";
525 static char resstr_sp[] = " ";
528 ra_opt_handler(struct ifinfo *ifi)
532 struct script_msg *smp1, *smp2, *smp3;
534 struct script_msg_head_t sm_rdnss_head =
535 TAILQ_HEAD_INITIALIZER(sm_rdnss_head);
536 struct script_msg_head_t sm_dnssl_head =
537 TAILQ_HEAD_INITIALIZER(sm_dnssl_head);
542 dlen = strlen(resstr_sh_prefix) + strlen(resstr_nl);
543 clock_gettime(CLOCK_MONOTONIC_FAST, &now);
546 * All options from multiple RAs with the same or different
547 * source addresses on a single interface will be gathered and
548 * handled, not overridden. [RFC 4861 6.3.4]
550 TAILQ_FOREACH(rai, &ifi->ifi_rainfo, rai_next) {
551 TAILQ_FOREACH(rao, &rai->rai_ra_opt, rao_next) {
552 switch (rao->rao_type) {
554 if (TS_CMP(&now, &rao->rao_expire, >)) {
555 warnmsg(LOG_INFO, __func__,
556 "expired rdnss entry: %s",
557 (char *)rao->rao_msg);
560 ELM_MALLOC(smp1, continue);
561 ELM_MALLOC(smp2, goto free1);
562 ELM_MALLOC(smp3, goto free2);
563 smp1->sm_msg = resstr_ns_prefix;
564 TAILQ_INSERT_TAIL(&sm_rdnss_head, smp1,
566 smp2->sm_msg = rao->rao_msg;
567 TAILQ_INSERT_TAIL(&sm_rdnss_head, smp2,
569 smp3->sm_msg = resstr_nl;
570 TAILQ_INSERT_TAIL(&sm_rdnss_head, smp3,
572 ifi->ifi_rdnss = IFI_DNSOPT_STATE_RECEIVED;
575 if (TS_CMP(&now, &rao->rao_expire, >)) {
576 warnmsg(LOG_INFO, __func__,
577 "expired dnssl entry: %s",
578 (char *)rao->rao_msg);
582 /* Check resolv.conf(5) restrictions. */
584 warnmsg(LOG_INFO, __func__,
585 "dnssl entry exceeding maximum count (%d>6)"
586 ": %s", dcount, (char *)rao->rao_msg);
589 if (256 < dlen + strlen(rao->rao_msg) +
591 warnmsg(LOG_INFO, __func__,
592 "dnssl entry exceeding maximum length "
593 "(>256): %s", (char *)rao->rao_msg);
596 ELM_MALLOC(smp1, continue);
597 ELM_MALLOC(smp2, goto free1);
598 if (TAILQ_EMPTY(&sm_dnssl_head)) {
599 ELM_MALLOC(smp3, goto free2);
600 smp3->sm_msg = resstr_sh_prefix;
601 TAILQ_INSERT_TAIL(&sm_dnssl_head, smp3,
604 smp1->sm_msg = rao->rao_msg;
605 TAILQ_INSERT_TAIL(&sm_dnssl_head, smp1,
607 smp2->sm_msg = resstr_sp;
608 TAILQ_INSERT_TAIL(&sm_dnssl_head, smp2,
610 dlen += strlen(rao->rao_msg) +
612 ifi->ifi_dnssl = IFI_DNSOPT_STATE_RECEIVED;
621 /* Call the script for each information source. */
623 ra_opt_rdnss_dispatch(ifi, rai, &sm_rdnss_head,
626 /* Call the script for each interface. */
628 ra_opt_rdnss_dispatch(ifi, NULL, &sm_rdnss_head,
634 make_rsid(const char *ifname, const char *origin, struct rainfo *rai)
636 char hbuf[NI_MAXHOST];
639 sprintf(rsid, "%s:%s", ifname, origin);
641 if (!IN6_IS_ADDR_LINKLOCAL(&rai->rai_saddr.sin6_addr))
643 if (getnameinfo((struct sockaddr *)&rai->rai_saddr,
644 rai->rai_saddr.sin6_len, hbuf, sizeof(hbuf), NULL, 0,
645 NI_NUMERICHOST) != 0)
647 sprintf(rsid, "%s:%s:[%s]", ifname, origin, hbuf);
649 warnmsg(LOG_DEBUG, __func__, "rsid = [%s]", rsid);
654 ra_opt_rdnss_dispatch(struct ifinfo *ifi, struct rainfo *rai,
655 struct script_msg_head_t *sm_rdnss_head,
656 struct script_msg_head_t *sm_dnssl_head)
658 struct script_msg *smp1;
663 /* Add \n for DNSSL list. */
664 if (!TAILQ_EMPTY(sm_dnssl_head)) {
665 ELM_MALLOC(smp1, goto ra_opt_rdnss_freeit);
666 smp1->sm_msg = resstr_nl;
667 TAILQ_INSERT_TAIL(sm_dnssl_head, smp1, sm_next);
669 TAILQ_CONCAT(sm_rdnss_head, sm_dnssl_head, sm_next);
671 r = make_rsid(ifi->ifname, DNSINFO_ORIGIN_LABEL, uflag ? rai : NULL);
673 warnmsg(LOG_ERR, __func__, "make_rsid() failed. "
674 "Script was not invoked.");
676 goto ra_opt_rdnss_freeit;
678 if (!TAILQ_EMPTY(sm_rdnss_head))
679 CALL_SCRIPT(RESADD, sm_rdnss_head);
680 else if (ifi->ifi_rdnss == IFI_DNSOPT_STATE_RECEIVED ||
681 ifi->ifi_dnssl == IFI_DNSOPT_STATE_RECEIVED) {
682 CALL_SCRIPT(RESDEL, NULL);
683 ifi->ifi_rdnss = IFI_DNSOPT_STATE_NOINFO;
684 ifi->ifi_dnssl = IFI_DNSOPT_STATE_NOINFO;
688 /* Clear script message queue. */
689 if (!TAILQ_EMPTY(sm_rdnss_head)) {
690 while ((smp1 = TAILQ_FIRST(sm_rdnss_head)) != NULL) {
691 TAILQ_REMOVE(sm_rdnss_head, smp1, sm_next);
695 if (!TAILQ_EMPTY(sm_dnssl_head)) {
696 while ((smp1 = TAILQ_FIRST(sm_dnssl_head)) != NULL) {
697 TAILQ_REMOVE(sm_dnssl_head, smp1, sm_next);
704 static struct ra_opt *
705 find_raopt(struct rainfo *rai, int type, void *msg, size_t len)
709 TAILQ_FOREACH(rao, &rai->rai_ra_opt, rao_next) {
710 if (rao->rao_type == type &&
711 rao->rao_len == strlen(msg) &&
712 memcmp(rao->rao_msg, msg, len) == 0)
720 call_script(const char *const argv[], struct script_msg_head_t *sm_head)
722 struct script_msg *smp;
729 wfd = cap_script_run(capscript, argv);
731 warnmsg(LOG_ERR, __func__,
732 "failed to run %s: %s", argv[0], strerror(errno));
736 if (sm_head != NULL) {
737 TAILQ_FOREACH(smp, sm_head, sm_next) {
738 len = strlen(smp->sm_msg);
739 warnmsg(LOG_DEBUG, __func__, "write to child = %s(%zd)",
741 if (write(wfd, smp->sm_msg, len) != len) {
742 warnmsg(LOG_ERR, __func__,
743 "write to child failed: %s",
752 if (cap_script_wait(capscript, &status) != 0)
753 warnmsg(LOG_ERR, __func__, "wait(): %s", strerror(errno));
755 warnmsg(LOG_DEBUG, __func__, "script \"%s\" status %d",
759 /* Decode domain name label encoding in RFC 1035 Section 3.1 */
761 dname_labeldec(char *dst, size_t dlen, const char *src)
764 const char *src_origin;
765 const char *src_last;
766 const char *dst_origin;
769 src_last = strchr(src, '\0');
771 memset(dst, '\0', dlen);
772 while ((len = (*src++) & 0x3f) &&
773 src + len <= src_last &&
774 len + (dst == dst_origin ? 0 : 1) < dlen) {
775 if (dst != dst_origin) {
779 warnmsg(LOG_DEBUG, __func__, "labellen = %zd", len);
780 memcpy(dst, src, len);
788 * XXX validate that domain name only contains valid characters
789 * for two reasons: 1) correctness, 2) we do not want to pass
790 * possible malicious, unescaped characters like `` to a script
791 * or program that could be exploited that way.
794 return (src - src_origin);