]> git.pld-linux.org Git - packages/ntp.git/blame - ntp-4.2.4p0-droproot.patch
- fixed Group
[packages/ntp.git] / ntp-4.2.4p0-droproot.patch
CommitLineData
6fca7355
ER
1--- ntp-4.2.4p0/ntpdate/ntpdate.c.droproot 2007-02-22 12:02:08.000000000 +0100
2+++ ntp-4.2.4p0/ntpdate/ntpdate.c 2007-03-07 16:06:26.000000000 +0100
3@@ -53,6 +53,12 @@
4
5 #include <arpa/inet.h>
6
7+/* Linux capabilities */
8+#include <sys/capability.h>
9+#include <sys/prctl.h>
10+#include <pwd.h>
11+#include <grp.h>
12+
13 #ifdef SYS_VXWORKS
14 # include "ioLib.h"
15 # include "sockLib.h"
16@@ -159,6 +165,11 @@
17 int unpriv_port = 0;
18
19 /*
20+ * Use capabilities to drop privileges and switch uids
21+ */
22+char *server_user;
23+
24+/*
25 * Program name.
26 */
27 char *progname;
28@@ -301,6 +312,88 @@
29 static ni_namelist *getnetinfoservers P((void));
30 #endif
31
32+/* This patch is adapted (copied) from Chris Wings drop root patch
33+ * for xntpd.
34+ */
35+void drop_root(uid_t server_uid, gid_t server_gid)
36+{
37+ cap_t caps;
38+
39+ if (prctl(PR_SET_KEEPCAPS, 1)) {
40+ if (syslogit) {
41+ msyslog(LOG_ERR, "prctl(PR_SET_KEEPCAPS, 1) failed");
42+ }
43+ else {
44+ fprintf(stderr, "prctl(PR_SET_KEEPCAPS, 1) failed.\n");
45+ }
46+ exit(1);
47+ }
48+
49+ if ( setgroups(0, NULL) == -1 ) {
50+ if (syslogit) {
51+ msyslog(LOG_ERR, "setgroups failed.");
52+ }
53+ else {
54+ fprintf(stderr, "setgroups failed.\n");
55+ }
56+ exit(1);
57+ }
58+
59+ if ( setegid(server_gid) == -1 || seteuid(server_uid) == -1 ) {
60+ if (syslogit) {
61+ msyslog(LOG_ERR, "setegid/seteuid to uid=%d/gid=%d failed.", server_uid,
62+ server_gid);
63+ }
64+ else {
65+ fprintf(stderr, "setegid/seteuid to uid=%d/gid=%d failed.\n", server_uid,
66+ server_gid);
67+ }
68+ exit(1);
69+ }
70+
71+ caps = cap_from_text("cap_sys_time=epi");
72+ if (caps == NULL) {
73+ if (syslogit) {
74+ msyslog(LOG_ERR, "cap_from_text failed.");
75+ }
76+ else {
77+ fprintf(stderr, "cap_from_text failed.\n");
78+ }
79+ exit(1);
80+ }
81+
82+ if (cap_set_proc(caps) == -1) {
83+ if (syslogit) {
84+ msyslog(LOG_ERR, "cap_set_proc failed.");
85+ }
86+ else {
87+ fprintf(stderr, "cap_set_proc failed.\n");
88+ }
89+ exit(1);
90+ }
91+
92+ /* Try to free the memory from cap_from_text */
93+ cap_free( caps );
94+
95+ if ( setregid(server_gid, server_gid) == -1 ||
96+ setreuid(server_uid, server_uid) == -1 ) {
97+ if (syslogit) {
98+ msyslog(LOG_ERR, "setregid/setreuid to uid=%d/gid=%d failed.",
99+ server_uid, server_gid);
100+ }
101+ else {
102+ fprintf(stderr, "setregid/setreuid to uid=%d/gid=%d failed.\n",
103+ server_uid, server_gid);
104+ }
105+ exit(1);
106+ }
107+
108+ if (syslogit) {
109+ msyslog(LOG_DEBUG, "running as uid(%d)/gid(%d) euid(%d)/egid(%d).",
110+ getuid(), getgid(), geteuid(), getegid());
111+ }
112+}
113+
114 /*
115 * Main program. Initialize us and loop waiting for I/O and/or
116 * timer expiries.
117@@ -354,7 +447,7 @@
118 clear_globals();
119 #endif
120
121-
122+ server_user = NULL;
123 /* Check to see if we have IPv6. Otherwise force the -4 flag */
124 if (isc_net_probeipv6() != ISC_R_SUCCESS) {
125 ai_fam_templ = AF_INET;
126@@ -367,7 +460,7 @@
127 /*
128 * Decode argument list
129 */
130- while ((c = ntp_getopt(argc, argv, "46a:bBde:k:o:p:qst:uv")) != EOF)
131+ while ((c = ntp_getopt(argc, argv, "46a:bBde:k:o:p:qst:uvU:")) != EOF)
132 switch (c)
133 {
134 case '4':
135@@ -445,6 +538,14 @@
136 case 'u':
137 unpriv_port = 1;
138 break;
139+ case 'U':
140+ if (ntp_optarg) {
141+ server_user = strdup(ntp_optarg);
142+ }
143+ else {
144+ ++errflg;
145+ }
146+ break;
147 case '?':
148 ++errflg;
149 break;
150@@ -454,7 +555,7 @@
151
152 if (errflg) {
153 (void) fprintf(stderr,
154- "usage: %s [-46bBdqsuv] [-a key#] [-e delay] [-k file] [-p samples] [-o version#] [-t timeo] server ...\n",
155+ "usage: %s [-46bBdqsuv] [-a key#] [-e delay] [-k file] [-p samples] [-o version#] [-t timeo] [-U username] server ...\n",
156 progname);
157 exit(2);
158 }
159@@ -574,6 +675,24 @@
160 initializing = 0;
161 was_alarmed = 0;
162
163+ if (server_user) {
164+ struct passwd *pwd = NULL;
165+
166+ /* Lookup server_user uid/gid before chroot/chdir */
167+ pwd = getpwnam( server_user );
168+ if ( pwd == NULL ) {
169+ if (syslogit) {
170+ msyslog(LOG_ERR, "Failed to lookup user '%s'.", server_user);
171+ }
172+ else {
173+ fprintf(stderr, "Failed to lookup user '%s'.\n", server_user);
174+ }
175+ exit(1);
176+ }
177+ drop_root(pwd->pw_uid, pwd->pw_gid);
178+ }
179+
180+
181 while (complete_servers < sys_numservers) {
182 #ifdef HAVE_POLL_H
183 struct pollfd* rdfdes;
184--- ntp-4.2.4p0/html/ntpdate.html.droproot 2006-12-28 13:02:58.000000000 +0100
185+++ ntp-4.2.4p0/html/ntpdate.html 2007-03-07 15:57:33.000000000 +0100
186@@ -18,7 +18,7 @@
187 <hr>
188 <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>
189 <h4>Synopsis</h4>
190- <tt>ntpdate [ -bBdoqsuv ] [ -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>
191+ <tt>ntpdate [ -bBdoqsuv ] [ -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>
192 <h4>Description</h4>
193 <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.
194 <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>
195@@ -58,6 +58,11 @@
196 <dd>Direct <tt>ntpdate</tt> to use an unprivileged port or outgoing packets. This is most useful when behind a firewall that blocks incoming traffic to privileged ports, and you want to synchronise with hosts beyond the firewall. Note that the <tt>-d</tt> option always uses unprivileged ports.
197 <dt><tt>-<i>v</i></tt>
198 <dd>Be verbose. This option will cause <tt>ntpdate</tt>'s version identification string to be logged.
199+
200+ <dt><tt>-U <i>user_name</i></tt></dt>
201+ <dd>ntpdate process drops root privileges and changes user ID to
202+ <i>user_name</i> and group ID to the primary group of
203+ <i>server_user</i>.
204 </dl>
205 <h4>Diagnostics</h4>
206 <tt>ntpdate</tt>'s exit status is zero if it finds a server and updates the clock, and nonzero otherwise.
This page took 0.042301 seconds and 4 git commands to generate.