Browse Source

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>
OPENBSD_3_6
henning 20 years ago
parent
commit
f5548494ff
5 changed files with 102 additions and 21 deletions
  1. +18
    -15
      src/usr.sbin/ntpd/client.c
  2. +2
    -1
      src/usr.sbin/ntpd/config.c
  3. +55
    -1
      src/usr.sbin/ntpd/ntp.c
  4. +22
    -2
      src/usr.sbin/ntpd/ntpd.c
  5. +5
    -2
      src/usr.sbin/ntpd/ntpd.h

+ 18
- 15
src/usr.sbin/ntpd/client.c View File

@ -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> * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
@ -30,14 +30,25 @@ int client_update(struct ntp_peer *);
int int
client_peer_init(struct ntp_peer *p) 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_in *sa_in;
struct sockaddr_in6 *sa_in6; struct sockaddr_in6 *sa_in6;
struct ntp_addr *h; 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) { for (h = p->addr; h != NULL; h = h->next) {
switch (h->ss.ss_family) { switch (h->ss.ss_family) {
case AF_INET: case AF_INET:
@ -51,7 +62,7 @@ client_peer_init(struct ntp_peer *p)
sa_in6->sin6_port = htons(123); sa_in6->sin6_port = htons(123);
break; break;
default: default:
fatal("king bula sez: wrong AF in client_peer_init");
fatal("king bula sez: wrong AF in client_addr_init");
/* not reached */ /* 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) (p->query->fd = socket(p->addr->ss.ss_family, SOCK_DGRAM, 0)) == -1)
fatal("client_query socket"); fatal("client_query socket");
p->query->msg.status = MODE_CLIENT | (NTP_VERSION << 3);
p->state = STATE_NONE;
p->next = time(NULL); p->next = time(NULL);
p->shift = 0;
p->trustlevel = TRUSTLEVEL_PATHETIC;
return (0); return (0);
} }
@ -75,12 +82,8 @@ client_nextaddr(struct ntp_peer *p)
close(p->query->fd); close(p->query->fd);
if (p->addr_head.a == NULL) { 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) if ((p->addr = p->addr->next) == NULL)


+ 2
- 1
src/usr.sbin/ntpd/config.c View File

@ -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> * 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)); bzero(&hints, sizeof(hints));
hints.ai_family = PF_UNSPEC; hints.ai_family = PF_UNSPEC;
hints.ai_socktype = SOCK_DGRAM; /* DUMMY */ hints.ai_socktype = SOCK_DGRAM; /* DUMMY */
res_init(); /* XXX */
error = getaddrinfo(s, NULL, &hints, &res0); error = getaddrinfo(s, NULL, &hints, &res0);
if (error) { if (error) {
log_warnx("could not parse \"%s\": %s", s, log_warnx("could not parse \"%s\": %s", s,


+ 55
- 1
src/usr.sbin/ntpd/ntp.c View File

@ -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> * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
@ -245,6 +245,10 @@ ntp_dispatch_imsg(void)
{ {
struct imsg imsg; struct imsg imsg;
int n; 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) if ((n = imsg_read(&ibuf_main)) == -1)
return (-1); return (-1);
@ -262,6 +266,47 @@ ntp_dispatch_imsg(void)
break; break;
switch (imsg.hdr.type) { 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: default:
break; break;
} }
@ -300,3 +345,12 @@ ntp_adjtime(void)
TAILQ_FOREACH(p, &conf->ntp_peers, entry) TAILQ_FOREACH(p, &conf->ntp_peers, entry)
p->update.good = 0; 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);
}

+ 22
- 2
src/usr.sbin/ntpd/ntpd.c View File

@ -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> * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
@ -213,8 +213,11 @@ int
dispatch_imsg(void) dispatch_imsg(void)
{ {
struct imsg imsg; struct imsg imsg;
int n;
int n, cnt;
double d; double d;
char *name;
struct ntp_addr *h, *hn;
struct buf *buf;
if ((n = imsg_read(&ibuf)) == -1) if ((n = imsg_read(&ibuf)) == -1)
return (-1); return (-1);
@ -237,6 +240,23 @@ dispatch_imsg(void)
fatal("invalid IMSG_ADJTIME received"); fatal("invalid IMSG_ADJTIME received");
memcpy(&d, imsg.data, sizeof(d)); memcpy(&d, imsg.data, sizeof(d));
ntpd_adjtime(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: default:
break; break;
} }


+ 5
- 2
src/usr.sbin/ntpd/ntpd.h View File

@ -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> * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
@ -150,7 +150,8 @@ struct imsgbuf {
enum imsg_type { enum imsg_type {
IMSG_NONE, IMSG_NONE,
IMSG_ADJTIME
IMSG_ADJTIME,
IMSG_HOST_DNS
}; };
struct imsg_hdr { struct imsg_hdr {
@ -201,6 +202,7 @@ void imsg_free(struct imsg *);
/* ntp.c */ /* ntp.c */
pid_t ntp_main(int[2], struct ntpd_conf *); pid_t ntp_main(int[2], struct ntpd_conf *);
void ntp_adjtime(void); void ntp_adjtime(void);
void ntp_host_dns(char *, u_int32_t);
/* parse.y */ /* parse.y */
int parse_config(char *, struct ntpd_conf *); int parse_config(char *, struct ntpd_conf *);
@ -222,6 +224,7 @@ int server_dispatch(int, struct ntpd_conf *);
/* client.c */ /* client.c */
int client_peer_init(struct ntp_peer *); int client_peer_init(struct ntp_peer *);
int client_addr_init(struct ntp_peer *);
int client_nextaddr(struct ntp_peer *); int client_nextaddr(struct ntp_peer *);
int client_query(struct ntp_peer *); int client_query(struct ntp_peer *);
int client_dispatch(struct ntp_peer *); int client_dispatch(struct ntp_peer *);


Loading…
Cancel
Save