diff -up ntp-4.2.6p4/html/ntpdate.html.droproot ntp-4.2.6p4/html/ntpdate.html --- ntp-4.2.6p4/html/ntpdate.html.droproot 2011-07-11 04:18:25.000000000 +0200 +++ ntp-4.2.6p4/html/ntpdate.html 2011-10-05 15:47:29.643634928 +0200 @@ -18,7 +18,7 @@

Disclaimer: The functionality of this program is now available in the ntpd program. See the -q command line option in the ntpd - Network Time Protocol (NTP) daemon page. After a suitable period of mourning, the ntpdate program is to be retired from this distribution

Synopsis

- ntpdate [ -46bBdqsuv ] [ -a key ] [ -e authdelay ] [ -k keyfile ] [ -o version ] [ -p samples ] [ -t timeout ] server [ ... ] + ntpdate [ -46bBdqsuv ] [ -a key ] [ -e authdelay ] [ -k keyfile ] [ -o version ] [ -p samples ] [ -t timeout ] [ -U user_name ] server [ ... ]

Description

ntpdate sets the local date and time by polling the Network Time Protocol (NTP) server(s) given as the server arguments to determine the correct time. It must be run as root on the local host. A number of samples are obtained from each of the servers specified and a subset of the NTP clock filter and selection algorithms are applied to select the best of these. Note that the accuracy and reliability of ntpdate depends on the number of servers, the number of polls each time it is run and the interval between runs.

ntpdate can be run manually as necessary to set the host clock, or it can be run from the host startup script to set the clock at boot time. This is useful in some cases to set the clock initially before starting the NTP daemon ntpd. It is also possible to run ntpdate from a cron script. However, it is important to note that ntpdate with contrived cron scripts is no substitute for the NTP daemon, which uses sophisticated algorithms to maximize accuracy and reliability while minimizing resource use. Finally, since ntpdate does not discipline the host clock frequency as does ntpd, the accuracy using ntpdate is limited.

@@ -58,6 +58,10 @@
Direct ntpdate to use an unprivileged port for outgoing packets. This is most useful when behind a firewall that blocks incoming traffic to privileged ports, and you want to synchronize with hosts beyond the firewall. Note that the -d option always uses unprivileged ports.
-v
Be verbose. This option will cause ntpdate's version identification string to be logged. +
-U user_name
+
ntpdate process drops root privileges and changes user ID to + user_name and group ID to the primary group of + server_user.

Diagnostics

ntpdate's exit status is zero if it finds a server and updates the clock, and nonzero otherwise. diff -up ntp-4.2.6p4/ntpdate/ntpdate.c.droproot ntp-4.2.6p4/ntpdate/ntpdate.c --- ntp-4.2.6p4/ntpdate/ntpdate.c.droproot 2011-05-25 07:06:09.000000000 +0200 +++ ntp-4.2.6p4/ntpdate/ntpdate.c 2011-10-05 15:45:39.570555972 +0200 @@ -49,6 +49,12 @@ #include +/* Linux capabilities */ +#include +#include +#include +#include + #ifdef SYS_VXWORKS # include "ioLib.h" # include "sockLib.h" @@ -153,6 +159,11 @@ int simple_query = 0; int unpriv_port = 0; /* + * Use capabilities to drop privileges and switch uids + */ +char *server_user; + +/* * Program name. */ char *progname; @@ -294,6 +305,88 @@ void clear_globals() static ni_namelist *getnetinfoservers (void); #endif +/* This patch is adapted (copied) from Chris Wings drop root patch + * for xntpd. + */ +void drop_root(uid_t server_uid, gid_t server_gid) +{ + cap_t caps; + + if (prctl(PR_SET_KEEPCAPS, 1)) { + if (syslogit) { + msyslog(LOG_ERR, "prctl(PR_SET_KEEPCAPS, 1) failed"); + } + else { + fprintf(stderr, "prctl(PR_SET_KEEPCAPS, 1) failed.\n"); + } + exit(1); + } + + if ( setgroups(0, NULL) == -1 ) { + if (syslogit) { + msyslog(LOG_ERR, "setgroups failed."); + } + else { + fprintf(stderr, "setgroups failed.\n"); + } + exit(1); + } + + if ( setegid(server_gid) == -1 || seteuid(server_uid) == -1 ) { + if (syslogit) { + msyslog(LOG_ERR, "setegid/seteuid to uid=%d/gid=%d failed.", server_uid, + server_gid); + } + else { + fprintf(stderr, "setegid/seteuid to uid=%d/gid=%d failed.\n", server_uid, + server_gid); + } + exit(1); + } + + caps = cap_from_text("cap_sys_time=epi"); + if (caps == NULL) { + if (syslogit) { + msyslog(LOG_ERR, "cap_from_text failed."); + } + else { + fprintf(stderr, "cap_from_text failed.\n"); + } + exit(1); + } + + if (cap_set_proc(caps) == -1) { + if (syslogit) { + msyslog(LOG_ERR, "cap_set_proc failed."); + } + else { + fprintf(stderr, "cap_set_proc failed.\n"); + } + exit(1); + } + + /* Try to free the memory from cap_from_text */ + cap_free( caps ); + + if ( setregid(server_gid, server_gid) == -1 || + setreuid(server_uid, server_uid) == -1 ) { + if (syslogit) { + msyslog(LOG_ERR, "setregid/setreuid to uid=%d/gid=%d failed.", + server_uid, server_gid); + } + else { + fprintf(stderr, "setregid/setreuid to uid=%d/gid=%d failed.\n", + server_uid, server_gid); + } + exit(1); + } + + if (syslogit) { + msyslog(LOG_DEBUG, "running as uid(%d)/gid(%d) euid(%d)/egid(%d).", + getuid(), getgid(), geteuid(), getegid()); + } +} + /* * Main program. Initialize us and loop waiting for I/O and/or * timer expiries. @@ -341,6 +434,8 @@ ntpdatemain ( init_lib(); /* sets up ipv4_works, ipv6_works */ + server_user = NULL; + /* Check to see if we have IPv6. Otherwise default to IPv4 */ if (!ipv6_works) ai_fam_templ = AF_INET; @@ -352,7 +447,7 @@ ntpdatemain ( /* * Decode argument list */ - while ((c = ntp_getopt(argc, argv, "46a:bBde:k:o:p:qst:uv")) != EOF) + while ((c = ntp_getopt(argc, argv, "46a:bBde:k:o:p:qst:uvU:")) != EOF) switch (c) { case '4': @@ -429,6 +524,14 @@ ntpdatemain ( case 'u': unpriv_port = 1; break; + case 'U': + if (ntp_optarg) { + server_user = strdup(ntp_optarg); + } + else { + ++errflg; + } + break; case '?': ++errflg; break; @@ -438,7 +541,7 @@ ntpdatemain ( if (errflg) { (void) fprintf(stderr, - "usage: %s [-46bBdqsuv] [-a key#] [-e delay] [-k file] [-p samples] [-o version#] [-t timeo] server ...\n", + "usage: %s [-46bBdqsuv] [-a key#] [-e delay] [-k file] [-p samples] [-o version#] [-t timeo] [-U username] server ...\n", progname); exit(2); } @@ -544,6 +647,24 @@ ntpdatemain ( initializing = 0; was_alarmed = 0; + if (server_user) { + struct passwd *pwd = NULL; + + /* Lookup server_user uid/gid before chroot/chdir */ + pwd = getpwnam( server_user ); + if ( pwd == NULL ) { + if (syslogit) { + msyslog(LOG_ERR, "Failed to lookup user '%s'.", server_user); + } + else { + fprintf(stderr, "Failed to lookup user '%s'.\n", server_user); + } + exit(1); + } + drop_root(pwd->pw_uid, pwd->pw_gid); + } + + while (complete_servers < sys_numservers) { #ifdef HAVE_POLL_H struct pollfd* rdfdes;