/* $OpenBSD: config.c,v 1.5 2004/07/07 03:53:14 henning Exp $ */ /* * Copyright (c) 2003, 2004 Henning Brauer * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. * * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ #include #include #include #include #include #include #include #include "ntpd.h" struct ntp_addr *host_v4(const char *, u_int8_t *); struct ntp_addr *host_v6(const char *); struct ntp_addr *host_dns(const char *, u_int8_t *); int check_file_secrecy(int fd, const char *fname) { struct stat st; if (fstat(fd, &st)) { log_warn("cannot stat %s", fname); return (-1); } if (st.st_uid != 0 && st.st_uid != getuid()) { log_warnx("%s: owner not root or current user", fname); return (-1); } if (st.st_mode & (S_IRWXG | S_IRWXO)) { log_warnx("%s: group/world readable/writeable", fname); return (-1); } return (0); } struct ntp_addr * host(const char *s, u_int8_t *len) { int mask; char *p, *q, *ps; struct ntp_addr *h = NULL; if ((p = strrchr(s, '/')) != NULL) { errno = 0; mask = strtol(p+1, &q, 0); if (errno == ERANGE || !q || *q || mask > 128 || q == (p+1)) { log_warnx("invalid netmask"); return (NULL); } if ((ps = malloc(strlen(s) - strlen(p) + 1)) == NULL) fatal("host: malloc"); strlcpy(ps, s, strlen(s) - strlen(p) + 1); } else { if ((ps = strdup(s)) == NULL) fatal("host: strdup"); mask = 128; } /* IPv4 address? */ if (h == NULL) h = host_v4(s, len); /* IPv6 address? */ if (h == NULL) { h = host_v6(ps); *len = mask; } /* Hostname? */ if (h == NULL) h = host_dns(ps, len); free(ps); return (h); } struct ntp_addr * host_v4(const char *s, u_int8_t *len) { struct in_addr ina; struct sockaddr_in *sa_in; struct ntp_addr *h; int bits = 32; bzero(&ina, sizeof(struct in_addr)); if (strrchr(s, '/') != NULL) { if ((bits = inet_net_pton(AF_INET, s, &ina, sizeof(ina))) == -1) return (NULL); } else { if (inet_pton(AF_INET, s, &ina) != 1) return (NULL); } if ((h = calloc(1, sizeof(struct ntp_addr))) == NULL) fatal(NULL); sa_in = (struct sockaddr_in *)&h->ss; sa_in->sin_len = sizeof(struct sockaddr_in); sa_in->sin_family = AF_INET; sa_in->sin_addr.s_addr = ina.s_addr; *len = bits; return (h); } struct ntp_addr * host_v6(const char *s) { struct addrinfo hints, *res; struct sockaddr_in6 *sa_in6; struct ntp_addr *h = NULL; bzero(&hints, sizeof(hints)); hints.ai_family = AF_INET6; hints.ai_socktype = SOCK_DGRAM; /*dummy*/ hints.ai_flags = AI_NUMERICHOST; if (getaddrinfo(s, "0", &hints, &res) == 0) { if ((h = calloc(1, sizeof(struct ntp_addr))) == NULL) fatal(NULL); sa_in6 = (struct sockaddr_in6 *)&h->ss; sa_in6->sin6_len = sizeof(struct sockaddr_in6); sa_in6->sin6_family = AF_INET6; memcpy(&sa_in6->sin6_addr, &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr, sizeof(sa_in6->sin6_addr)); sa_in6->sin6_scope_id = ((struct sockaddr_in6 *)res->ai_addr)->sin6_scope_id; freeaddrinfo(res); } return (h); } struct ntp_addr * host_dns(const char *s, u_int8_t *len) { struct addrinfo hints, *res0, *res; int error; struct sockaddr_in *sa_in; struct sockaddr_in6 *sa_in6; struct ntp_addr *h, *hh = NULL; memset(&hints, 0, sizeof(hints)); hints.ai_family = PF_UNSPEC; hints.ai_socktype = SOCK_STREAM; /* DUMMY */ error = getaddrinfo(s, NULL, &hints, &res0); if (error) return (NULL); for (res = res0; res; res = res->ai_next) { if (res->ai_family != AF_INET && res->ai_family != AF_INET6) continue; if ((h = calloc(1, sizeof(struct ntp_addr))) == NULL) fatal(NULL); h->ss.ss_family = res->ai_family; if (res->ai_family == AF_INET) { sa_in = (struct sockaddr_in *)&h->ss; sa_in->sin_len = sizeof(struct sockaddr_in); sa_in->sin_addr.s_addr = ((struct sockaddr_in *) res->ai_addr)->sin_addr.s_addr; *len = 32; } else { sa_in6 = (struct sockaddr_in6 *)&h->ss; sa_in6->sin6_len = sizeof(struct sockaddr_in6); memcpy(&sa_in6->sin6_addr, &((struct sockaddr_in6 *) res->ai_addr)->sin6_addr, sizeof(struct in6_addr)); *len = 128; } h->next = hh; hh = h; } freeaddrinfo(res0); return (hh); }