diff --git a/src/usr.sbin/ntpd/config.c b/src/usr.sbin/ntpd/config.c index 0594b01e..07403092 100644 --- a/src/usr.sbin/ntpd/config.c +++ b/src/usr.sbin/ntpd/config.c @@ -1,4 +1,4 @@ -/* $OpenBSD: config.c,v 1.30 2019/05/28 06:49:46 otto Exp $ */ +/* $OpenBSD: config.c,v 1.31 2019/06/12 05:04:45 otto Exp $ */ /* * Copyright (c) 2003, 2004 Henning Brauer @@ -126,13 +126,13 @@ host_dns1(const char *s, struct ntp_addr **hn, int notauth) } int -host_dns(const char *s, struct ntp_addr **hn) +host_dns(const char *s, int synced, struct ntp_addr **hn) { int error, save_opts; log_debug("trying to resolve %s", s); error = host_dns1(s, hn, 0); - if (error <= 0) { + if (!synced && error <= 0) { log_debug("no luck, trying to resolve %s without checking", s); save_opts = _res.options; _res.options |= RES_USE_CD; diff --git a/src/usr.sbin/ntpd/ntp.c b/src/usr.sbin/ntpd/ntp.c index e1052fab..3c77e6fa 100644 --- a/src/usr.sbin/ntpd/ntp.c +++ b/src/usr.sbin/ntpd/ntp.c @@ -1,4 +1,4 @@ -/* $OpenBSD: ntp.c,v 1.153 2019/06/09 08:40:54 otto Exp $ */ +/* $OpenBSD: ntp.c,v 1.154 2019/06/12 05:04:45 otto Exp $ */ /* * Copyright (c) 2003, 2004 Henning Brauer @@ -95,12 +95,10 @@ ntp_main(struct ntpd_conf *nconf, struct passwd *pw, int argc, char **argv) start_child(NTPDNS_PROC_NAME, pipe_dns[1], argc, argv); - /* in this case the parent didn't init logging and didn't daemonize */ - if (nconf->settime && !nconf->debug) { - log_init(nconf->debug, LOG_DAEMON); - if (setsid() == -1) - fatal("setsid"); - } + log_init(nconf->debug, LOG_DAEMON); + log_setverbose(nconf->verbose); + if (!nconf->debug && setsid() == -1) + fatal("setsid"); log_procinit("ntp"); if ((se = getservbyname("ntp", "udp")) == NULL) @@ -446,9 +444,11 @@ ntp_dispatch_imsg(void) if (n == 1 && !conf->status.synced) { log_info("clock is now synced"); conf->status.synced = 1; + priv_dns(IMSG_SYNCED, NULL, 0); } else if (n == 0 && conf->status.synced) { log_info("clock is now unsynced"); conf->status.synced = 0; + priv_dns(IMSG_UNSYNCED, NULL, 0); } break; case IMSG_CONSTRAINT_RESULT: @@ -762,9 +762,10 @@ priv_settime(double offset) void priv_dns(int cmd, char *name, u_int32_t peerid) { - u_int16_t dlen; + u_int16_t dlen = 0; - dlen = strlen(name) + 1; + if (name != NULL) + dlen = strlen(name) + 1; imsg_compose(ibuf_dns, cmd, peerid, 0, -1, name, dlen); } diff --git a/src/usr.sbin/ntpd/ntp_dns.c b/src/usr.sbin/ntpd/ntp_dns.c index 1ac65a45..cf79af5c 100644 --- a/src/usr.sbin/ntpd/ntp_dns.c +++ b/src/usr.sbin/ntpd/ntp_dns.c @@ -1,4 +1,4 @@ -/* $OpenBSD: ntp_dns.c,v 1.21 2019/05/28 06:49:46 otto Exp $ */ +/* $OpenBSD: ntp_dns.c,v 1.22 2019/06/12 05:04:45 otto Exp $ */ /* * Copyright (c) 2003-2008 Henning Brauer @@ -39,7 +39,7 @@ volatile sig_atomic_t quit_dns = 0; struct imsgbuf *ibuf_dns; void sighdlr_dns(int); -int dns_dispatch_imsg(void); +int dns_dispatch_imsg(struct ntpd_conf *); void sighdlr_dns(int sig) @@ -62,12 +62,10 @@ ntp_dns(struct ntpd_conf *nconf, struct passwd *pw) if (setpriority(PRIO_PROCESS, 0, 0) == -1) log_warn("could not set priority"); - /* in this case the parent didn't init logging and didn't daemonize */ - if (nconf->settime && !nconf->debug) { - log_init(nconf->debug, LOG_DAEMON); - if (setsid() == -1) - fatal("setsid"); - } + log_init(nconf->debug, LOG_DAEMON); + log_setverbose(nconf->verbose); + if (!nconf->debug && setsid() == -1) + fatal("setsid"); log_procinit("dns"); if ((nullfd = open("/dev/null", O_RDWR, 0)) == -1) @@ -119,7 +117,7 @@ ntp_dns(struct ntpd_conf *nconf, struct passwd *pw) if (nfds > 0 && pfd[0].revents & POLLIN) { nfds--; - if (dns_dispatch_imsg() == -1) + if (dns_dispatch_imsg(nconf) == -1) quit_dns = 1; } } @@ -130,7 +128,7 @@ ntp_dns(struct ntpd_conf *nconf, struct passwd *pw) } int -dns_dispatch_imsg(void) +dns_dispatch_imsg(struct ntpd_conf *nconf) { struct imsg imsg; int n, cnt; @@ -164,7 +162,8 @@ dns_dispatch_imsg(void) if (name[len] != '\0' || strlen(name) != len) fatalx("invalid %s received", str); - if ((cnt = host_dns(name, &hn)) == -1) + if ((cnt = host_dns(name, nconf->status.synced, + &hn)) == -1) break; buf = imsg_create(ibuf_dns, imsg.hdr.type, imsg.hdr.peerid, 0, @@ -190,6 +189,12 @@ dns_dispatch_imsg(void) if (buf) imsg_close(ibuf_dns, buf); break; + case IMSG_SYNCED: + nconf->status.synced = 1; + break; + case IMSG_UNSYNCED: + nconf->status.synced = 0; + break; default: break; } diff --git a/src/usr.sbin/ntpd/ntpd.c b/src/usr.sbin/ntpd/ntpd.c index 84117207..8bac94cd 100644 --- a/src/usr.sbin/ntpd/ntpd.c +++ b/src/usr.sbin/ntpd/ntpd.c @@ -1,4 +1,4 @@ -/* $OpenBSD: ntpd.c,v 1.121 2019/06/09 08:40:54 otto Exp $ */ +/* $OpenBSD: ntpd.c,v 1.122 2019/06/12 05:04:45 otto Exp $ */ /* * Copyright (c) 2003, 2004 Henning Brauer @@ -179,7 +179,8 @@ main(int argc, char *argv[]) } /* log to stderr until daemonized */ - log_init(lconf.debug ? lconf.debug : 1, LOG_DAEMON); + log_init(1, LOG_DAEMON); + log_setverbose(lconf.verbose); argc -= optind; argv += optind; diff --git a/src/usr.sbin/ntpd/ntpd.h b/src/usr.sbin/ntpd/ntpd.h index e7b52805..c80d3cd9 100644 --- a/src/usr.sbin/ntpd/ntpd.h +++ b/src/usr.sbin/ntpd/ntpd.h @@ -1,4 +1,4 @@ -/* $OpenBSD: ntpd.h,v 1.141 2019/06/09 08:40:54 otto Exp $ */ +/* $OpenBSD: ntpd.h,v 1.142 2019/06/12 05:04:45 otto Exp $ */ /* * Copyright (c) 2003, 2004 Henning Brauer @@ -305,7 +305,9 @@ enum imsg_type { IMSG_CTL_SHOW_SENSORS, IMSG_CTL_SHOW_SENSORS_END, IMSG_CTL_SHOW_ALL, - IMSG_CTL_SHOW_ALL_END + IMSG_CTL_SHOW_ALL_END, + IMSG_SYNCED, + IMSG_UNSYNCED }; enum ctl_actions { @@ -335,7 +337,7 @@ int parse_config(const char *, struct ntpd_conf *); /* config.c */ void host(const char *, struct ntp_addr **); -int host_dns(const char *, struct ntp_addr **); +int host_dns(const char *, int, struct ntp_addr **); void host_dns_free(struct ntp_addr *); struct ntp_peer *new_peer(void); struct ntp_conf_sensor *new_sensor(char *); diff --git a/src/usr.sbin/ntpd/parse.y b/src/usr.sbin/ntpd/parse.y index 6b436d55..c5b5cbf8 100644 --- a/src/usr.sbin/ntpd/parse.y +++ b/src/usr.sbin/ntpd/parse.y @@ -1,4 +1,4 @@ -/* $OpenBSD: parse.y,v 1.71 2019/02/13 22:57:08 deraadt Exp $ */ +/* $OpenBSD: parse.y,v 1.72 2019/06/12 05:04:45 otto Exp $ */ /* * Copyright (c) 2002, 2003, 2004 Henning Brauer @@ -109,7 +109,7 @@ main : LISTEN ON address listen_opts { struct ntp_addr *h, *next; if ((h = $3->a) == NULL && - (host_dns($3->name, &h) == -1 || !h)) { + (host_dns($3->name, 0, &h) == -1 || !h)) { yyerror("could not resolve \"%s\"", $3->name); free($3->name); free($3);