summaryrefslogtreecommitdiff
path: root/usr.sbin/ntpd/ntpd.c
diff options
context:
space:
mode:
authorOtto Moerbeek <otto@cvs.openbsd.org>2019-06-09 08:40:55 +0000
committerOtto Moerbeek <otto@cvs.openbsd.org>2019-06-09 08:40:55 +0000
commit9f4ffea3819bc735cf63731deff395cb6a76edd2 (patch)
tree08be550a2842c074a48d1c10318662cdeaeb861c /usr.sbin/ntpd/ntpd.c
parentf037d828dd9b975182496e111782ed1023900564 (diff)
Introducing autmatic settime mode: if some preconditions are met
(booting, constraint(s) defined) set the time but only if the clock should be moved forward by more than a minute, based on ntp replies that satisfied the constraints. Tested by many; ok deraadt@
Diffstat (limited to 'usr.sbin/ntpd/ntpd.c')
-rw-r--r--usr.sbin/ntpd/ntpd.c25
1 files changed, 23 insertions, 2 deletions
diff --git a/usr.sbin/ntpd/ntpd.c b/usr.sbin/ntpd/ntpd.c
index 44c4f72f3fb..84117207f1e 100644
--- a/usr.sbin/ntpd/ntpd.c
+++ b/usr.sbin/ntpd/ntpd.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: ntpd.c,v 1.120 2019/01/14 16:30:21 florian Exp $ */
+/* $OpenBSD: ntpd.c,v 1.121 2019/06/09 08:40:54 otto Exp $ */
/*
* Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
@@ -20,6 +20,7 @@
#include <sys/types.h>
#include <sys/resource.h>
#include <sys/socket.h>
+#include <sys/sysctl.h>
#include <sys/wait.h>
#include <sys/un.h>
#include <netinet/in.h>
@@ -41,6 +42,7 @@
void sighdlr(int);
__dead void usage(void);
+int auto_preconditions(const struct ntpd_conf *);
int main(int, char *[]);
void check_child(void);
int dispatch_imsg(struct ntpd_conf *, int, char **);
@@ -102,6 +104,19 @@ usage(void)
exit(1);
}
+int
+auto_preconditions(const struct ntpd_conf *cnf)
+{
+ int mib[2] = { CTL_KERN, KERN_SECURELVL };
+ int constraints, securelevel;
+ size_t sz = sizeof(int);
+
+ if (sysctl(mib, 2, &securelevel, &sz, NULL, 0) < 0)
+ err(1, "sysctl");
+ constraints = !TAILQ_EMPTY(&cnf->constraints);
+ return !cnf->settime && constraints && securelevel == 0;
+}
+
#define POLL_MAX 8
#define PFD_PIPE 0
#define PFD_MAX 1
@@ -185,6 +200,10 @@ main(int argc, char *argv[])
if ((pw = getpwnam(NTPD_USER)) == NULL)
errx(1, "unknown user %s", NTPD_USER);
+ lconf.automatic = auto_preconditions(&lconf);
+ if (lconf.automatic)
+ lconf.settime = 1;
+
if (pname != NULL) {
/* Remove our proc arguments, so child doesn't need to. */
if (sanitize_argv(&argc0, &argv0) == -1)
@@ -209,7 +228,6 @@ main(int argc, char *argv[])
if (setpriority(PRIO_PROCESS, 0, -20) == -1)
warn("can't set priority");
-
reset_adjtime();
if (!lconf.settime) {
log_init(lconf.debug, LOG_DAEMON);
@@ -495,6 +513,9 @@ ntpd_settime(double d)
char buf[80];
time_t tval;
+ if (d == 0)
+ return;
+
if (gettimeofday(&curtime, NULL) == -1) {
log_warn("gettimeofday");
return;