1 /* $NetBSD: conf.c,v 1.24 2016/04/04 15:52:56 christos Exp $ */
4 * Copyright (c) 2015 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
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.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
35 #include <sys/cdefs.h>
36 __RCSID("$NetBSD: conf.c,v 1.24 2016/04/04 15:52:56 christos Exp $");
55 #include <arpa/inet.h>
56 #include <netinet/in.h>
58 #include <sys/socket.h>
68 sa_family_t sif_family;
74 ((const struct sockaddr_if *)(const void *)(a))->sif_name
76 static int conf_is_interface(const char *);
85 while (*ep && !isspace((unsigned char)*ep))
87 while (*ep && isspace((unsigned char)*ep))
93 getnum(const char *f, size_t l, bool local, void *rp, const char *name,
100 if (strcmp(p, "*") == 0) {
104 if (strcmp(p, "=") == 0) {
111 im = strtoi(p, NULL, 0, 0, INT_MAX, &e);
119 (*lfun)(LOG_ERR, "%s: %s, %zu: Bad number for %s [%s]", __func__, f, l,
123 (*lfun)(LOG_ERR, "%s: %s, %zu: `=' for %s not allowed in local config",
124 __func__, f, l, name);
130 getnfail(const char *f, size_t l, bool local, struct conf *c, const char *p)
132 return getnum(f, l, local, &c->c_nfail, "nfail", p);
136 getsecs(const char *f, size_t l, bool local, struct conf *c, const char *p)
143 if (strcmp(p, "*") == 0) {
144 c->c_duration = FSTAR;
147 if (strcmp(p, "=") == 0) {
150 c->c_duration = FEQUAL;
154 im = strtoi(p, &ep, 0, 0, INT_MAX, &e);
180 c->c_duration = (int)tot;
186 (*lfun)(LOG_ERR, "%s: %s, %zu: Bad number [%s]", __func__, f, l, p);
189 (*lfun)(LOG_ERR, "%s: %s, %zu: `=' duration not allowed in local"
190 " config", __func__, f, l);
196 getport(const char *f, size_t l, bool local, void *r, const char *p)
200 // XXX: Pass in the proto instead
201 if ((sv = getservbyname(p, "tcp")) != NULL) {
202 *(int *)r = ntohs(sv->s_port);
205 if ((sv = getservbyname(p, "udp")) != NULL) {
206 *(int *)r = ntohs(sv->s_port);
210 return getnum(f, l, local, r, "service", p);
214 getmask(const char *f, size_t l, bool local, const char **p, int *mask)
219 if ((d = strchr(s, ':')) != NULL) {
223 if ((d = strchr(s, '/')) == NULL) {
229 return getnum(f, l, local, mask, "mask", d);
233 gethostport(const char *f, size_t l, bool local, struct conf *c, const char *p)
235 char *d; // XXX: Ok to write to string.
236 in_port_t *port = NULL;
239 if (strcmp(p, "*") == 0) {
245 if ((d = strchr(p, ']')) != NULL) {
252 if (getmask(f, l, local, &pstr, &c->c_lmask) == -1)
256 struct sockaddr_in6 *sin6 = (void *)&c->c_ss;
258 (*lfun)(LOG_DEBUG, "%s: host6 %s", __func__, p);
259 if (strcmp(p, "*") != 0) {
260 if (inet_pton(AF_INET6, p, &sin6->sin6_addr) == -1)
262 sin6->sin6_family = AF_INET6;
263 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
264 sin6->sin6_len = sizeof(*sin6);
266 port = &sin6->sin6_port;
268 } else if (pstr != p || strchr(p, '.') || conf_is_interface(p)) {
271 struct sockaddr_in *sin = (void *)&c->c_ss;
272 struct sockaddr_if *sif = (void *)&c->c_ss;
274 (*lfun)(LOG_DEBUG, "%s: host4 %s", __func__, p);
275 if (strcmp(p, "*") != 0) {
276 if (conf_is_interface(p)) {
280 (*lfun)(LOG_DEBUG, "%s: interface %s",
282 if (c->c_lmask != FSTAR)
284 sif->sif_family = AF_MAX;
285 strlcpy(sif->sif_name, p,
286 sizeof(sif->sif_name));
287 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
288 sif->sif_len = sizeof(*sif);
290 port = &sif->sif_port;
291 } else if (inet_pton(AF_INET, p, &sin->sin_addr) != -1)
293 sin->sin_family = AF_INET;
294 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
295 sin->sin_len = sizeof(*sin);
297 port = &sin->sin_port;
303 if (getport(f, l, local, &c->c_port, pstr) == -1)
306 if (port && c->c_port != FSTAR && c->c_port != FEQUAL)
307 *port = htons((in_port_t)c->c_port);
310 (*lfun)(LOG_ERR, "%s: %s, %zu: Bad address [%s]", __func__, f, l, pstr);
313 (*lfun)(LOG_ERR, "%s: %s, %zu: Can't specify mask %d with "
314 "interface [%s]", __func__, f, l, c->c_lmask, p);
317 (*lfun)(LOG_ERR, "%s: %s, %zu: Interface spec does not make sense "
318 "with remote config [%s]", __func__, f, l, p);
323 getproto(const char *f, size_t l, bool local __unused, struct conf *c,
326 if (strcmp(p, "stream") == 0) {
327 c->c_proto = IPPROTO_TCP;
330 if (strcmp(p, "dgram") == 0) {
331 c->c_proto = IPPROTO_UDP;
334 return getnum(f, l, local, &c->c_proto, "protocol", p);
338 getfamily(const char *f, size_t l, bool local __unused, struct conf *c,
341 if (strncmp(p, "tcp", 3) == 0 || strncmp(p, "udp", 3) == 0) {
342 c->c_family = p[3] == '6' ? AF_INET6 : AF_INET;
345 return getnum(f, l, local, &c->c_family, "family", p);
349 getuid(const char *f, size_t l, bool local __unused, struct conf *c,
354 if ((pw = getpwnam(p)) != NULL) {
355 c->c_uid = (int)pw->pw_uid;
359 return getnum(f, l, local, &c->c_uid, "user", p);
364 getname(const char *f, size_t l, bool local, struct conf *c,
367 if (getmask(f, l, local, &p, &c->c_rmask) == -1)
370 if (strcmp(p, "*") == 0) {
371 strlcpy(c->c_name, rulename, CONFNAMESZ);
374 if (strcmp(p, "=") == 0) {
381 snprintf(c->c_name, CONFNAMESZ, "%s%s", *p == '-' ? rulename : "", p);
384 (*lfun)(LOG_ERR, "%s: %s, %zu: `=' name not allowed in local"
385 " config", __func__, f, l);
390 getvalue(const char *f, size_t l, bool local, void *r, char **p,
391 int (*fun)(const char *, size_t, bool, struct conf *, const char *))
396 return (*fun)(f, l, local, r, ep);
401 conf_parseline(const char *f, size_t l, char *p, struct conf *c, bool local)
405 while (*p && isspace((unsigned char)*p))
408 memset(c, 0, sizeof(*c));
409 e = getvalue(f, l, local, c, &p, gethostport);
411 e = getvalue(f, l, local, c, &p, getproto);
413 e = getvalue(f, l, local, c, &p, getfamily);
415 e = getvalue(f, l, local, c, &p, getuid);
417 e = getvalue(f, l, local, c, &p, getname);
419 e = getvalue(f, l, local, c, &p, getnfail);
421 e = getvalue(f, l, local, c, &p, getsecs);
428 conf_sort(const void *v1, const void *v2)
430 const struct conf *c1 = v1;
431 const struct conf *c2 = v2;
433 #define CMP(a, b, f) \
434 if ((a)->f > (b)->f) return -1; \
435 else if ((a)->f < (b)->f) return 1
437 CMP(c1, c2, c_ss.ss_family);
438 CMP(c1, c2, c_lmask);
440 CMP(c1, c2, c_proto);
441 CMP(c1, c2, c_family);
442 CMP(c1, c2, c_rmask);
449 conf_is_interface(const char *name)
451 const struct ifaddrs *ifa;
453 for (ifa = ifas; ifa; ifa = ifa->ifa_next)
454 if (strcmp(ifa->ifa_name, name) == 0)
459 #define MASK(m) ((uint32_t)~((1 << (32 - (m))) - 1))
462 conf_amask_eq(const void *v1, const void *v2, size_t len, int mask)
464 const uint32_t *a1 = v1;
465 const uint32_t *a2 = v2;
472 if (memcmp(v1, v2, len) == 0)
477 (*lfun)(LOG_CRIT, "%s: Internal error: bad mask %d", __func__,
484 for (size_t i = 0; i < len; i++) {
486 m = htonl((uint32_t)~0);
489 m = htonl(MASK(mask));
493 if ((a1[i] & m) != (a2[i] & m))
499 char b1[256], b2[256];
501 blhexdump(b1, sizeof(b1), "a1", v1, len);
502 blhexdump(b2, sizeof(b2), "a2", v2, len);
503 (*lfun)(LOG_DEBUG, "%s: %s != %s [0x%x]", __func__,
510 * Apply the mask to the given address
513 conf_apply_mask(void *v, size_t len, int mask)
522 (*lfun)(LOG_CRIT, "%s: Internal error: bad mask %d", __func__,
530 for (size_t i = 0; i < len; i++) {
532 m = htonl((uint32_t)~0);
535 m = htonl(MASK(mask));
544 * apply the mask and the port to the address given
547 conf_addr_set(struct conf *c, const struct sockaddr_storage *ss)
549 struct sockaddr_in *sin;
550 struct sockaddr_in6 *sin6;
555 c->c_lmask = c->c_rmask;
558 if (c->c_ss.ss_family != c->c_family) {
559 (*lfun)(LOG_CRIT, "%s: Internal error: mismatched family "
560 "%u != %u", __func__, c->c_ss.ss_family, c->c_family);
564 switch (c->c_ss.ss_family) {
566 sin = (void *)&c->c_ss;
567 port = &sin->sin_port;
568 addr = &sin->sin_addr;
569 alen = sizeof(sin->sin_addr);
572 sin6 = (void *)&c->c_ss;
573 port = &sin6->sin6_port;
574 addr = &sin6->sin6_addr;
575 alen = sizeof(sin6->sin6_addr);
578 (*lfun)(LOG_CRIT, "%s: Internal error: bad family %u",
579 __func__, c->c_ss.ss_family);
583 *port = htons((in_port_t)c->c_port);
584 conf_apply_mask(addr, alen, c->c_lmask);
585 if (c->c_lmask == FSTAR)
586 c->c_lmask = (int)(alen * 8);
589 sockaddr_snprintf(buf, sizeof(buf), "%a:%p", (void *)&c->c_ss);
590 (*lfun)(LOG_DEBUG, "Applied address %s", buf);
595 * Compared two addresses for equality applying the mask
598 conf_inet_eq(const void *v1, const void *v2, int mask)
600 const struct sockaddr *sa1 = v1;
601 const struct sockaddr *sa2 = v2;
604 if (sa1->sa_family != sa2->sa_family)
607 switch (sa1->sa_family) {
609 const struct sockaddr_in *s1 = v1;
610 const struct sockaddr_in *s2 = v2;
611 size = sizeof(s1->sin_addr);
618 const struct sockaddr_in6 *s1 = v1;
619 const struct sockaddr_in6 *s2 = v2;
620 size = sizeof(s1->sin6_addr);
627 (*lfun)(LOG_CRIT, "%s: Internal error: bad family %u",
628 __func__, sa1->sa_family);
632 return conf_amask_eq(v1, v2, size, mask);
636 conf_addr_in_interface(const struct sockaddr_storage *s1,
637 const struct sockaddr_storage *s2, int mask)
639 const char *name = SIF_NAME(s2);
640 const struct ifaddrs *ifa;
642 for (ifa = ifas; ifa; ifa = ifa->ifa_next) {
643 if ((ifa->ifa_flags & IFF_UP) == 0)
646 if (strcmp(ifa->ifa_name, name) != 0)
649 if (s1->ss_family != ifa->ifa_addr->sa_family)
653 switch (s1->ss_family) {
656 eq = conf_inet_eq(ifa->ifa_addr, s1, mask);
659 (*lfun)(LOG_ERR, "Bad family %u", s1->ss_family);
669 conf_addr_eq(const struct sockaddr_storage *s1,
670 const struct sockaddr_storage *s2, int mask)
672 switch (s2->ss_family) {
676 return conf_addr_in_interface(s1, s2, mask);
679 return conf_inet_eq(s1, s2, mask);
681 (*lfun)(LOG_CRIT, "%s: Internal error: bad family %u",
682 __func__, s1->ss_family);
688 conf_eq(const struct conf *c1, const struct conf *c2)
691 if (!conf_addr_eq(&c1->c_ss, &c2->c_ss, c2->c_lmask))
694 #define CMP(a, b, f) \
695 if ((a)->f != (b)->f && (b)->f != FSTAR && (b)->f != FEQUAL) { \
697 (*lfun)(LOG_DEBUG, "%s: %s fail %d != %d", __func__, \
698 __STRING(f), (a)->f, (b)->f); \
702 CMP(c1, c2, c_proto);
703 CMP(c1, c2, c_family);
710 conf_num(char *b, size_t l, int n)
718 snprintf(b, l, "%d", n);
724 fmtname(const char *n) {
725 size_t l = strlen(rulename);
728 if (strncmp(n, rulename, l) == 0) {
740 fmtport(char *b, size_t l, int port)
747 if (b[0] == '\0' || strcmp(b, "*") == 0)
748 snprintf(b, l, "%d", port);
750 snprintf(buf, sizeof(buf), ":%d", port);
756 fmtmask(char *b, size_t l, int fam, int mask)
764 if (strcmp(b, "=") == 0)
787 snprintf(buf, sizeof(buf), "/%d", mask);
793 conf_namemask(char *b, size_t l, const struct conf *c)
795 strlcpy(b, fmtname(c->c_name), l);
796 fmtmask(b, l, c->c_family, c->c_rmask);
801 conf_print(char *buf, size_t len, const char *pref, const char *delim,
802 const struct conf *c)
804 char ha[128], hb[32], b[5][64];
807 #define N(n, v) conf_num(b[n], sizeof(b[n]), (v))
809 switch (c->c_ss.ss_family) {
811 snprintf(ha, sizeof(ha), "*");
814 snprintf(ha, sizeof(ha), "%s", SIF_NAME(&c->c_ss));
817 sockaddr_snprintf(ha, sizeof(ha), "%a", (const void *)&c->c_ss);
821 fmtmask(ha, sizeof(ha), c->c_family, c->c_lmask);
822 fmtport(ha, sizeof(ha), c->c_port);
824 sp = *delim == '\t' ? 20 : -1;
827 snprintf(buf, len, "%s%*.*s%s%s%s" "%s%s%s%s"
829 pref, sp, sp, ha, delim, N(0, c->c_proto), delim,
830 N(1, c->c_family), delim, N(2, c->c_uid), delim,
831 conf_namemask(hb, sizeof(hb), c), delim,
832 N(3, c->c_nfail), delim, N(4, c->c_duration));
834 snprintf(buf, len, "%starget:%s, proto:%s, family:%s, "
835 "uid:%s, name:%s, nfail:%s, duration:%s", pref,
836 ha, N(0, c->c_proto), N(1, c->c_family), N(2, c->c_uid),
837 conf_namemask(hb, sizeof(hb), c),
838 N(3, c->c_nfail), N(4, c->c_duration));
843 * Apply the local config match to the result
846 conf_apply(struct conf *c, const struct conf *sc)
851 (*lfun)(LOG_DEBUG, "%s: %s", __func__,
852 conf_print(buf, sizeof(buf), "merge:\t", "", sc));
853 (*lfun)(LOG_DEBUG, "%s: %s", __func__,
854 conf_print(buf, sizeof(buf), "to:\t", "", c));
856 memcpy(c->c_name, sc->c_name, CONFNAMESZ);
857 c->c_uid = sc->c_uid;
858 c->c_rmask = sc->c_rmask;
859 c->c_nfail = sc->c_nfail;
860 c->c_duration = sc->c_duration;
863 (*lfun)(LOG_DEBUG, "%s: %s", __func__,
864 conf_print(buf, sizeof(buf), "result:\t", "", c));
868 * Merge a remote configuration to the result
871 conf_merge(struct conf *c, const struct conf *sc)
876 (*lfun)(LOG_DEBUG, "%s: %s", __func__,
877 conf_print(buf, sizeof(buf), "merge:\t", "", sc));
878 (*lfun)(LOG_DEBUG, "%s: %s", __func__,
879 conf_print(buf, sizeof(buf), "to:\t", "", c));
883 memcpy(c->c_name, sc->c_name, CONFNAMESZ);
884 if (sc->c_uid != FEQUAL)
885 c->c_uid = sc->c_uid;
886 if (sc->c_rmask != FEQUAL)
887 c->c_lmask = c->c_rmask = sc->c_rmask;
888 if (sc->c_nfail != FEQUAL)
889 c->c_nfail = sc->c_nfail;
890 if (sc->c_duration != FEQUAL)
891 c->c_duration = sc->c_duration;
893 (*lfun)(LOG_DEBUG, "%s: %s", __func__,
894 conf_print(buf, sizeof(buf), "result:\t", "", c));
898 confset_init(struct confset *cs)
906 confset_grow(struct confset *cs)
911 tc = realloc(cs->cs_c, cs->cs_m * sizeof(*cs->cs_c));
913 (*lfun)(LOG_ERR, "%s: Can't grow confset (%m)", __func__);
921 confset_get(struct confset *cs)
923 return &cs->cs_c[cs->cs_n];
927 confset_full(const struct confset *cs)
929 return cs->cs_n == cs->cs_m;
933 confset_sort(struct confset *cs)
935 qsort(cs->cs_c, cs->cs_n, sizeof(*cs->cs_c), conf_sort);
939 confset_add(struct confset *cs)
945 confset_free(struct confset *cs)
952 confset_replace(struct confset *dc, struct confset *sc)
962 confset_list(const struct confset *cs, const char *msg, const char *where)
966 (*lfun)(LOG_DEBUG, "[%s]", msg);
967 (*lfun)(LOG_DEBUG, "%20.20s\ttype\tproto\towner\tname\tnfail\tduration",
969 for (size_t i = 0; i < cs->cs_n; i++)
970 (*lfun)(LOG_DEBUG, "%s", conf_print(buf, sizeof(buf), "", "\t",
975 * Match a configuration against the given list and apply the function
976 * to it, returning the matched entry number.
979 confset_match(const struct confset *cs, struct conf *c,
980 void (*fun)(struct conf *, const struct conf *))
985 for (i = 0; i < cs->cs_n; i++) {
987 (*lfun)(LOG_DEBUG, "%s", conf_print(buf, sizeof(buf),
988 "check:\t", "", &cs->cs_c[i]));
989 if (conf_eq(c, &cs->cs_c[i])) {
991 (*lfun)(LOG_DEBUG, "%s",
992 conf_print(buf, sizeof(buf),
993 "found:\t", "", &cs->cs_c[i]));
994 (*fun)(c, &cs->cs_c[i]);
1002 conf_find(int fd, uid_t uid, const struct sockaddr_storage *rss,
1007 struct sockaddr_storage lss;
1011 memset(cr, 0, sizeof(*cr));
1013 memset(&lss, 0, slen);
1014 if (getsockname(fd, (void *)&lss, &slen) == -1) {
1015 (*lfun)(LOG_ERR, "getsockname failed (%m)");
1019 slen = sizeof(proto);
1020 if (getsockopt(fd, SOL_SOCKET, SO_TYPE, &proto, &slen) == -1) {
1021 (*lfun)(LOG_ERR, "getsockopt failed (%m)");
1026 sockaddr_snprintf(buf, sizeof(buf), "%a:%p", (void *)&lss);
1027 (*lfun)(LOG_DEBUG, "listening socket: %s", buf);
1032 cr->c_proto = IPPROTO_TCP;
1035 cr->c_proto = IPPROTO_UDP;
1038 (*lfun)(LOG_ERR, "unsupported protocol %d", proto);
1042 switch (lss.ss_family) {
1044 cr->c_port = ntohs(((struct sockaddr_in *)&lss)->sin_port);
1047 cr->c_port = ntohs(((struct sockaddr_in6 *)&lss)->sin6_port);
1050 (*lfun)(LOG_ERR, "unsupported family %d", lss.ss_family);
1055 cr->c_lmask = FSTAR;
1056 cr->c_uid = (int)uid;
1057 cr->c_family = lss.ss_family;
1058 cr->c_name[0] = '\0';
1059 cr->c_rmask = FSTAR;
1060 cr->c_nfail = FSTAR;
1061 cr->c_duration = FSTAR;
1064 (*lfun)(LOG_DEBUG, "%s", conf_print(buf, sizeof(buf),
1065 "look:\t", "", cr));
1067 /* match the local config */
1068 i = confset_match(&lconf, cr, conf_apply);
1069 if (i == lconf.cs_n) {
1071 (*lfun)(LOG_DEBUG, "not found");
1075 conf_addr_set(cr, rss);
1076 /* match the remote config */
1077 confset_match(&rconf, cr, conf_merge);
1078 /* to apply the mask */
1079 conf_addr_set(cr, &cr->c_ss);
1086 conf_parse(const char *f)
1091 struct confset lc, rc, *cs;
1093 if ((fp = fopen(f, "r")) == NULL) {
1094 (*lfun)(LOG_ERR, "%s: Cannot open `%s' (%m)", __func__, f);
1103 for (; (line = fparseln(fp, &len, &lineno, NULL, 0)) != NULL;
1108 if (strcmp(line, "[local]") == 0) {
1112 if (strcmp(line, "[remote]") == 0) {
1117 if (confset_full(cs)) {
1118 if (confset_grow(cs) == -1) {
1125 if (conf_parseline(f, lineno, line, confset_get(cs),
1135 confset_replace(&rconf, &rc);
1136 confset_replace(&lconf, &lc);
1139 confset_list(&lconf, "local", "target");
1140 confset_list(&rconf, "remote", "source");