]> git.ipfire.org Git - people/ms/ipfire-3.x.git/blob - ntp/patches/ntp-4.2.6p4-droproot.patch
1d953d18b98f04f879d5d9f4efdd279d2d9a5997
[people/ms/ipfire-3.x.git] / ntp / patches / ntp-4.2.6p4-droproot.patch
1 diff -up ntp-4.2.6p4/html/ntpdate.html.droproot ntp-4.2.6p4/html/ntpdate.html
2 --- ntp-4.2.6p4/html/ntpdate.html.droproot 2011-07-11 04:18:25.000000000 +0200
3 +++ ntp-4.2.6p4/html/ntpdate.html 2011-10-05 15:47:29.643634928 +0200
4 @@ -18,7 +18,7 @@
5 <hr>
6 <p>Disclaimer: The functionality of this program is now available in the <tt>ntpd</tt> program. See the <tt>-q</tt> command line option in the <a href="ntpd.html"><tt>ntpd</tt> - Network Time Protocol (NTP) daemon</a> page. After a suitable period of mourning, the <tt>ntpdate</tt> program is to be retired from this distribution</p>
7 <h4>Synopsis</h4>
8 - <tt>ntpdate [ -46bBdqsuv ] [ -a <i>key</i> ] [ -e <i>authdelay</i> ] [ -k <i>keyfile</i> ] [ -o <i>version</i> ] [ -p <i>samples</i> ] [ -t <i>timeout</i> ] <i>server</i> [ ... ]</tt>
9 + <tt>ntpdate [ -46bBdqsuv ] [ -a <i>key</i> ] [ -e <i>authdelay</i> ] [ -k <i>keyfile</i> ] [ -o <i>version</i> ] [ -p <i>samples</i> ] [ -t <i>timeout</i> ] [ -U <i>user_name</i> ] <i>server</i> [ ... ]</tt>
10 <h4>Description</h4>
11 <p><tt>ntpdate</tt> sets the local date and time by polling the Network Time Protocol (NTP) server(s) given as the <i>server</i> 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 <tt>ntpdate</tt> depends on the number of servers, the number of polls each time it is run and the interval between runs.</p>
12 <p><tt>ntpdate</tt> 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 <tt>ntpd</tt>. It is also possible to run <tt>ntpdate</tt> from a <tt>cron</tt> script. However, it is important to note that <tt>ntpdate</tt> with contrived <tt>cron</tt> scripts is no substitute for the NTP daemon, which uses sophisticated algorithms to maximize accuracy and reliability while minimizing resource use. Finally, since <tt>ntpdate</tt> does not discipline the host clock frequency as does <tt>ntpd</tt>, the accuracy using <tt>ntpdate</tt> is limited.</p>
13 @@ -58,6 +58,10 @@
14 <dd>Direct <tt>ntpdate</tt> 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 <tt>-d</tt> option always uses unprivileged ports.
15 <dt><tt>-<i>v</i></tt>
16 <dd>Be verbose. This option will cause <tt>ntpdate</tt>'s version identification string to be logged.
17 + <dt><tt>-U <i>user_name</i></tt></dt>
18 + <dd>ntpdate process drops root privileges and changes user ID to
19 + <i>user_name</i> and group ID to the primary group of
20 + <i>server_user</i>.
21 </dl>
22 <h4>Diagnostics</h4>
23 <tt>ntpdate</tt>'s exit status is zero if it finds a server and updates the clock, and nonzero otherwise.
24 diff -up ntp-4.2.6p4/ntpdate/ntpdate.c.droproot ntp-4.2.6p4/ntpdate/ntpdate.c
25 --- ntp-4.2.6p4/ntpdate/ntpdate.c.droproot 2011-05-25 07:06:09.000000000 +0200
26 +++ ntp-4.2.6p4/ntpdate/ntpdate.c 2011-10-05 15:45:39.570555972 +0200
27 @@ -49,6 +49,12 @@
28
29 #include <arpa/inet.h>
30
31 +/* Linux capabilities */
32 +#include <sys/capability.h>
33 +#include <sys/prctl.h>
34 +#include <pwd.h>
35 +#include <grp.h>
36 +
37 #ifdef SYS_VXWORKS
38 # include "ioLib.h"
39 # include "sockLib.h"
40 @@ -153,6 +159,11 @@ int simple_query = 0;
41 int unpriv_port = 0;
42
43 /*
44 + * Use capabilities to drop privileges and switch uids
45 + */
46 +char *server_user;
47 +
48 +/*
49 * Program name.
50 */
51 char *progname;
52 @@ -294,6 +305,88 @@ void clear_globals()
53 static ni_namelist *getnetinfoservers (void);
54 #endif
55
56 +/* This patch is adapted (copied) from Chris Wings drop root patch
57 + * for xntpd.
58 + */
59 +void drop_root(uid_t server_uid, gid_t server_gid)
60 +{
61 + cap_t caps;
62 +
63 + if (prctl(PR_SET_KEEPCAPS, 1)) {
64 + if (syslogit) {
65 + msyslog(LOG_ERR, "prctl(PR_SET_KEEPCAPS, 1) failed");
66 + }
67 + else {
68 + fprintf(stderr, "prctl(PR_SET_KEEPCAPS, 1) failed.\n");
69 + }
70 + exit(1);
71 + }
72 +
73 + if ( setgroups(0, NULL) == -1 ) {
74 + if (syslogit) {
75 + msyslog(LOG_ERR, "setgroups failed.");
76 + }
77 + else {
78 + fprintf(stderr, "setgroups failed.\n");
79 + }
80 + exit(1);
81 + }
82 +
83 + if ( setegid(server_gid) == -1 || seteuid(server_uid) == -1 ) {
84 + if (syslogit) {
85 + msyslog(LOG_ERR, "setegid/seteuid to uid=%d/gid=%d failed.", server_uid,
86 + server_gid);
87 + }
88 + else {
89 + fprintf(stderr, "setegid/seteuid to uid=%d/gid=%d failed.\n", server_uid,
90 + server_gid);
91 + }
92 + exit(1);
93 + }
94 +
95 + caps = cap_from_text("cap_sys_time=epi");
96 + if (caps == NULL) {
97 + if (syslogit) {
98 + msyslog(LOG_ERR, "cap_from_text failed.");
99 + }
100 + else {
101 + fprintf(stderr, "cap_from_text failed.\n");
102 + }
103 + exit(1);
104 + }
105 +
106 + if (cap_set_proc(caps) == -1) {
107 + if (syslogit) {
108 + msyslog(LOG_ERR, "cap_set_proc failed.");
109 + }
110 + else {
111 + fprintf(stderr, "cap_set_proc failed.\n");
112 + }
113 + exit(1);
114 + }
115 +
116 + /* Try to free the memory from cap_from_text */
117 + cap_free( caps );
118 +
119 + if ( setregid(server_gid, server_gid) == -1 ||
120 + setreuid(server_uid, server_uid) == -1 ) {
121 + if (syslogit) {
122 + msyslog(LOG_ERR, "setregid/setreuid to uid=%d/gid=%d failed.",
123 + server_uid, server_gid);
124 + }
125 + else {
126 + fprintf(stderr, "setregid/setreuid to uid=%d/gid=%d failed.\n",
127 + server_uid, server_gid);
128 + }
129 + exit(1);
130 + }
131 +
132 + if (syslogit) {
133 + msyslog(LOG_DEBUG, "running as uid(%d)/gid(%d) euid(%d)/egid(%d).",
134 + getuid(), getgid(), geteuid(), getegid());
135 + }
136 +}
137 +
138 /*
139 * Main program. Initialize us and loop waiting for I/O and/or
140 * timer expiries.
141 @@ -341,6 +434,8 @@ ntpdatemain (
142
143 init_lib(); /* sets up ipv4_works, ipv6_works */
144
145 + server_user = NULL;
146 +
147 /* Check to see if we have IPv6. Otherwise default to IPv4 */
148 if (!ipv6_works)
149 ai_fam_templ = AF_INET;
150 @@ -352,7 +447,7 @@ ntpdatemain (
151 /*
152 * Decode argument list
153 */
154 - while ((c = ntp_getopt(argc, argv, "46a:bBde:k:o:p:qst:uv")) != EOF)
155 + while ((c = ntp_getopt(argc, argv, "46a:bBde:k:o:p:qst:uvU:")) != EOF)
156 switch (c)
157 {
158 case '4':
159 @@ -429,6 +524,14 @@ ntpdatemain (
160 case 'u':
161 unpriv_port = 1;
162 break;
163 + case 'U':
164 + if (ntp_optarg) {
165 + server_user = strdup(ntp_optarg);
166 + }
167 + else {
168 + ++errflg;
169 + }
170 + break;
171 case '?':
172 ++errflg;
173 break;
174 @@ -438,7 +541,7 @@ ntpdatemain (
175
176 if (errflg) {
177 (void) fprintf(stderr,
178 - "usage: %s [-46bBdqsuv] [-a key#] [-e delay] [-k file] [-p samples] [-o version#] [-t timeo] server ...\n",
179 + "usage: %s [-46bBdqsuv] [-a key#] [-e delay] [-k file] [-p samples] [-o version#] [-t timeo] [-U username] server ...\n",
180 progname);
181 exit(2);
182 }
183 @@ -544,6 +647,24 @@ ntpdatemain (
184 initializing = 0;
185 was_alarmed = 0;
186
187 + if (server_user) {
188 + struct passwd *pwd = NULL;
189 +
190 + /* Lookup server_user uid/gid before chroot/chdir */
191 + pwd = getpwnam( server_user );
192 + if ( pwd == NULL ) {
193 + if (syslogit) {
194 + msyslog(LOG_ERR, "Failed to lookup user '%s'.", server_user);
195 + }
196 + else {
197 + fprintf(stderr, "Failed to lookup user '%s'.\n", server_user);
198 + }
199 + exit(1);
200 + }
201 + drop_root(pwd->pw_uid, pwd->pw_gid);
202 + }
203 +
204 +
205 while (complete_servers < sys_numservers) {
206 #ifdef HAVE_POLL_H
207 struct pollfd* rdfdes;