diff options
author | Henning Brauer <henning@cvs.openbsd.org> | 2004-08-12 16:34:00 +0000 |
---|---|---|
committer | Henning Brauer <henning@cvs.openbsd.org> | 2004-08-12 16:34:00 +0000 |
commit | fc24ce82f48fa6c395878397f5c90bee81ee3aac (patch) | |
tree | a84370b0d2a75fb2e9e074578f01cb2132bc911e /usr.sbin/ntpd | |
parent | 89736c35ab1e1c84fb217a5e36c9caa883ae8834 (diff) |
do not try to getaddrinfo() in the unprivileged process, send an imsg
asking the privileged one to do it. sends back an imsg with the
resulting addresses in a bunch of struct sockaddr_storage in the data
part.
this should fix all remaining issues with dns (non-)availability at
ntpd startup, be it due to named on localhost or something else.
tested by marco@ and Chris Paul <chris.paul@sentinare.com>
Diffstat (limited to 'usr.sbin/ntpd')
-rw-r--r-- | usr.sbin/ntpd/client.c | 33 | ||||
-rw-r--r-- | usr.sbin/ntpd/config.c | 3 | ||||
-rw-r--r-- | usr.sbin/ntpd/ntp.c | 56 | ||||
-rw-r--r-- | usr.sbin/ntpd/ntpd.c | 24 | ||||
-rw-r--r-- | usr.sbin/ntpd/ntpd.h | 7 |
5 files changed, 102 insertions, 21 deletions
diff --git a/usr.sbin/ntpd/client.c b/usr.sbin/ntpd/client.c index a1882953252..254bae52b94 100644 --- a/usr.sbin/ntpd/client.c +++ b/usr.sbin/ntpd/client.c @@ -1,4 +1,4 @@ -/* $OpenBSD: client.c,v 1.29 2004/07/28 16:38:43 henning Exp $ */ +/* $OpenBSD: client.c,v 1.30 2004/08/12 16:33:59 henning Exp $ */ /* * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org> @@ -31,13 +31,24 @@ int client_update(struct ntp_peer *); int client_peer_init(struct ntp_peer *p) { + if ((p->query = calloc(1, sizeof(struct ntp_query))) == NULL) + fatal("client_query calloc"); + p->query->fd = -1; + p->query->msg.status = MODE_CLIENT | (NTP_VERSION << 3); + p->state = STATE_NONE; + p->shift = 0; + p->trustlevel = TRUSTLEVEL_PATHETIC; + + return (client_addr_init(p)); +} + +int +client_addr_init(struct ntp_peer *p) +{ struct sockaddr_in *sa_in; struct sockaddr_in6 *sa_in6; struct ntp_addr *h; - if ((p->query = calloc(1, sizeof(struct ntp_query))) == NULL) - fatal("client_query calloc"); - for (h = p->addr; h != NULL; h = h->next) { switch (h->ss.ss_family) { case AF_INET: @@ -51,7 +62,7 @@ client_peer_init(struct ntp_peer *p) sa_in6->sin6_port = htons(123); break; default: - fatal("king bula sez: wrong AF in client_peer_init"); + fatal("king bula sez: wrong AF in client_addr_init"); /* not reached */ } } @@ -60,11 +71,7 @@ client_peer_init(struct ntp_peer *p) (p->query->fd = socket(p->addr->ss.ss_family, SOCK_DGRAM, 0)) == -1) fatal("client_query socket"); - p->query->msg.status = MODE_CLIENT | (NTP_VERSION << 3); - p->state = STATE_NONE; p->next = time(NULL); - p->shift = 0; - p->trustlevel = TRUSTLEVEL_PATHETIC; return (0); } @@ -75,12 +82,8 @@ client_nextaddr(struct ntp_peer *p) close(p->query->fd); if (p->addr_head.a == NULL) { - if (host_dns(p->addr_head.name, &p->addr_head.a) > 0) { - p->addr = p->addr_head.a; - p->shift = 0; - p->trustlevel = TRUSTLEVEL_PATHETIC; - } else - return (-1); + ntp_host_dns(p->addr_head.name, p->id); + return (-1); } if ((p->addr = p->addr->next) == NULL) diff --git a/usr.sbin/ntpd/config.c b/usr.sbin/ntpd/config.c index 140052de363..2b01da1766c 100644 --- a/usr.sbin/ntpd/config.c +++ b/usr.sbin/ntpd/config.c @@ -1,4 +1,4 @@ -/* $OpenBSD: config.c,v 1.10 2004/08/10 12:41:15 henning Exp $ */ +/* $OpenBSD: config.c,v 1.11 2004/08/12 16:33:59 henning Exp $ */ /* * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org> @@ -124,6 +124,7 @@ host_dns(const char *s, struct ntp_addr **hn) bzero(&hints, sizeof(hints)); hints.ai_family = PF_UNSPEC; hints.ai_socktype = SOCK_DGRAM; /* DUMMY */ + res_init(); /* XXX */ error = getaddrinfo(s, NULL, &hints, &res0); if (error) { log_warnx("could not parse \"%s\": %s", s, diff --git a/usr.sbin/ntpd/ntp.c b/usr.sbin/ntpd/ntp.c index 5326c28f5e5..cadbc32f036 100644 --- a/usr.sbin/ntpd/ntp.c +++ b/usr.sbin/ntpd/ntp.c @@ -1,4 +1,4 @@ -/* $OpenBSD: ntp.c,v 1.25 2004/07/18 12:59:41 henning Exp $ */ +/* $OpenBSD: ntp.c,v 1.26 2004/08/12 16:33:59 henning Exp $ */ /* * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org> @@ -245,6 +245,10 @@ ntp_dispatch_imsg(void) { struct imsg imsg; int n; + struct ntp_peer *peer, *npeer; + u_int16_t dlen; + u_char *p; + struct ntp_addr *h; if ((n = imsg_read(&ibuf_main)) == -1) return (-1); @@ -262,6 +266,47 @@ ntp_dispatch_imsg(void) break; switch (imsg.hdr.type) { + case IMSG_HOST_DNS: + TAILQ_FOREACH(peer, &conf->ntp_peers, entry) + if (peer->id == imsg.hdr.peerid) + break; + if (peer == NULL) + fatal("IMSG_HOST_DNS with invalid peerID"); + if (peer->addr != NULL) { + log_warnx("IMSG_HOST_DNS but addr != NULL!"); + break; + } + dlen = imsg.hdr.len - IMSG_HEADER_SIZE; + p = (u_char *)imsg.data; + while (dlen >= sizeof(struct sockaddr_storage)) { + if ((h = calloc(1, sizeof(struct ntp_addr))) == + NULL) + fatal(NULL); + memcpy(&h->ss, p, sizeof(h->ss)); + p += sizeof(h->ss); + dlen -= sizeof(h->ss); + if (peer->addr_head.pool) { + npeer = new_peer(); + h->next = NULL; + npeer->addr = h; + npeer->addr_head.a = h; + client_peer_init(npeer); + TAILQ_INSERT_TAIL(&conf->ntp_peers, + npeer, entry); + } else { + h->next = peer->addr; + peer->addr = h; + peer->addr_head.a = peer->addr; + } + } + if (dlen != 0) + fatal("IMSG_HOST_DNS: dlen != 0"); + if (peer->addr_head.pool) { + TAILQ_REMOVE(&conf->ntp_peers, peer, entry); + free(peer); + } else + client_addr_init(peer); + break; default: break; } @@ -300,3 +345,12 @@ ntp_adjtime(void) TAILQ_FOREACH(p, &conf->ntp_peers, entry) p->update.good = 0; } + +void +ntp_host_dns(char *name, u_int32_t peerid) +{ + u_int16_t dlen; + + dlen = strlen(name) + 1; + imsg_compose(&ibuf_main, IMSG_HOST_DNS, peerid, name, dlen); +} diff --git a/usr.sbin/ntpd/ntpd.c b/usr.sbin/ntpd/ntpd.c index a3dfbd75d6d..b717945616a 100644 --- a/usr.sbin/ntpd/ntpd.c +++ b/usr.sbin/ntpd/ntpd.c @@ -1,4 +1,4 @@ -/* $OpenBSD: ntpd.c,v 1.13 2004/07/13 11:16:22 henning Exp $ */ +/* $OpenBSD: ntpd.c,v 1.14 2004/08/12 16:33:59 henning Exp $ */ /* * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org> @@ -213,8 +213,11 @@ int dispatch_imsg(void) { struct imsg imsg; - int n; + int n, cnt; double d; + char *name; + struct ntp_addr *h, *hn; + struct buf *buf; if ((n = imsg_read(&ibuf)) == -1) return (-1); @@ -237,6 +240,23 @@ dispatch_imsg(void) fatal("invalid IMSG_ADJTIME received"); memcpy(&d, imsg.data, sizeof(d)); ntpd_adjtime(d); + break; + case IMSG_HOST_DNS: + name = imsg.data; + if (imsg.hdr.len != strlen(name) + 1 + IMSG_HEADER_SIZE) + fatal("invalid IMSG_HOST_DNS received"); + if ((cnt = host_dns(name, &hn)) > 0) { + buf = imsg_create(&ibuf, IMSG_HOST_DNS, + imsg.hdr.peerid, + cnt * sizeof(struct sockaddr_storage)); + if (buf == NULL) + break; + for (h = hn; h != NULL; h = h->next) { + imsg_add(buf, &h->ss, sizeof(h->ss)); + } + imsg_close(&ibuf, buf); + } + break; default: break; } diff --git a/usr.sbin/ntpd/ntpd.h b/usr.sbin/ntpd/ntpd.h index db3efbc8032..a1d3537f2f4 100644 --- a/usr.sbin/ntpd/ntpd.h +++ b/usr.sbin/ntpd/ntpd.h @@ -1,4 +1,4 @@ -/* $OpenBSD: ntpd.h,v 1.32 2004/08/10 12:41:15 henning Exp $ */ +/* $OpenBSD: ntpd.h,v 1.33 2004/08/12 16:33:59 henning Exp $ */ /* * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org> @@ -150,7 +150,8 @@ struct imsgbuf { enum imsg_type { IMSG_NONE, - IMSG_ADJTIME + IMSG_ADJTIME, + IMSG_HOST_DNS }; struct imsg_hdr { @@ -201,6 +202,7 @@ void imsg_free(struct imsg *); /* ntp.c */ pid_t ntp_main(int[2], struct ntpd_conf *); void ntp_adjtime(void); +void ntp_host_dns(char *, u_int32_t); /* parse.y */ int parse_config(char *, struct ntpd_conf *); @@ -222,6 +224,7 @@ int server_dispatch(int, struct ntpd_conf *); /* client.c */ int client_peer_init(struct ntp_peer *); +int client_addr_init(struct ntp_peer *); int client_nextaddr(struct ntp_peer *); int client_query(struct ntp_peer *); int client_dispatch(struct ntp_peer *); |