Added
Link Here
|
1 |
--- server/dhcpd.c.orig Wed Jan 15 00:15:24 2003 |
2 |
+++ server/dhcpd.c Sun May 30 23:14:42 2004 |
3 |
@@ -56,6 +56,22 @@ |
4 |
#include "version.h" |
5 |
#include <omapip/omapip_p.h> |
6 |
|
7 |
+#if defined (PARANOIA) |
8 |
+#include <sys/types.h> |
9 |
+#include <unistd.h> |
10 |
+#include <pwd.h> |
11 |
+/* get around the ISC declaration of group */ |
12 |
+#define group real_group |
13 |
+#include <grp.h> |
14 |
+#undef group |
15 |
+#endif /* PARANOIA */ |
16 |
+#if defined (JAIL) |
17 |
+#include <sys/param.h> |
18 |
+#include <sys/jail.h> |
19 |
+#include <netinet/in.h> |
20 |
+#include <arpa/inet.h> |
21 |
+#endif /* JAIL */ |
22 |
+ |
23 |
static void usage PROTO ((void)); |
24 |
|
25 |
TIME cur_time; |
26 |
@@ -204,6 +220,35 @@ |
27 |
omapi_object_dereference (&listener, MDL); |
28 |
} |
29 |
|
30 |
+#if defined (PARANOIA) |
31 |
+/* to be used in one of two possible scenarios */ |
32 |
+static void setup_chroot (char *chroot_dir) |
33 |
+{ |
34 |
+ if (geteuid ()) |
35 |
+ log_fatal ("you must be root to use chroot"); |
36 |
+ if (chroot (chroot_dir)) |
37 |
+ log_fatal ("chroot(\"%s\"): %m", chroot_dir); |
38 |
+ if (chdir ("/")) |
39 |
+ /* probably permission denied */ |
40 |
+ log_fatal ("chdir(\"/\"): %m"); |
41 |
+} |
42 |
+#endif /* PARANOIA */ |
43 |
+ |
44 |
+#if defined (JAIL) |
45 |
+static void setup_jail (char *chroot_dir, char *hostname, u_int32_t ip_number) |
46 |
+{ |
47 |
+ struct jail j; |
48 |
+ |
49 |
+ j.version = 0; |
50 |
+ j.path = chroot_dir; |
51 |
+ j.hostname = hostname; |
52 |
+ j.ip_number = ip_number; |
53 |
+ |
54 |
+ if (jail (&j) < 0) |
55 |
+ log_fatal ("jail(%s, %s): %m", chroot_dir, hostname); |
56 |
+} |
57 |
+#endif /* JAIL */ |
58 |
+ |
59 |
int main (argc, argv, envp) |
60 |
int argc; |
61 |
char **argv, **envp; |
62 |
@@ -236,6 +281,20 @@ |
63 |
char *traceinfile = (char *)0; |
64 |
char *traceoutfile = (char *)0; |
65 |
#endif |
66 |
+#if defined (PARANOIA) |
67 |
+ char *set_user = 0; |
68 |
+ char *set_group = 0; |
69 |
+ uid_t set_uid = 0; |
70 |
+ gid_t set_gid = 0; |
71 |
+ int early_chroot = 0; |
72 |
+#endif /* PARANOIA */ |
73 |
+#if defined (PARANOIA) || defined (JAIL) |
74 |
+ char *set_chroot = 0; |
75 |
+#endif /* PARANOIA || JAIL */ |
76 |
+#if defined (JAIL) |
77 |
+ char *set_jail = 0; |
78 |
+ u_int32_t jail_ip_address = 0; /* Good as long as it's IPv4 ... */ |
79 |
+#endif /* JAIL */ |
80 |
|
81 |
/* Make sure we have stdin, stdout and stderr. */ |
82 |
status = open ("/dev/null", O_RDWR); |
83 |
@@ -298,6 +357,35 @@ |
84 |
if (++i == argc) |
85 |
usage (); |
86 |
server = argv [i]; |
87 |
+#if defined (PARANOIA) |
88 |
+ } else if (!strcmp (argv [i], "-user")) { |
89 |
+ if (++i == argc) |
90 |
+ usage (); |
91 |
+ set_user = argv [i]; |
92 |
+ } else if (!strcmp (argv [i], "-group")) { |
93 |
+ if (++i == argc) |
94 |
+ usage (); |
95 |
+ set_group = argv [i]; |
96 |
+ } else if (!strcmp (argv [i], "-early_chroot")) { |
97 |
+ early_chroot = 1; |
98 |
+#endif /* PARANOIA */ |
99 |
+#if defined (PARANOIA) || defined (JAIL) |
100 |
+ } else if (!strcmp (argv [i], "-chroot")) { |
101 |
+ if (++i == argc) |
102 |
+ usage (); |
103 |
+ set_chroot = argv [i]; |
104 |
+#endif /* PARANOIA || JAIL */ |
105 |
+#if defined (JAIL) |
106 |
+ } else if (!strcmp (argv [i], "-jail")) { |
107 |
+ if (++i == argc) |
108 |
+ usage (); |
109 |
+ set_jail = argv [i]; |
110 |
+ if (++i == argc) |
111 |
+ usage (); |
112 |
+ if (ascii2addr (AF_INET, argv[i], &jail_ip_address) < 0) |
113 |
+ usage(); |
114 |
+ jail_ip_address = ntohl (jail_ip_address); |
115 |
+#endif /* JAIL */ |
116 |
} else if (!strcmp (argv [i], "-cf")) { |
117 |
if (++i == argc) |
118 |
usage (); |
119 |
@@ -397,6 +485,57 @@ |
120 |
trace_seed_stop, MDL); |
121 |
#endif |
122 |
|
123 |
+#if defined (PARANOIA) |
124 |
+ /* get user and group info if those options were given */ |
125 |
+ if (set_user) { |
126 |
+ struct passwd *tmp_pwd; |
127 |
+ |
128 |
+ if (geteuid ()) |
129 |
+ log_fatal ("you must be root to set user"); |
130 |
+ |
131 |
+ if (!(tmp_pwd = getpwnam (set_user))) |
132 |
+ log_fatal ("no such user: %s", set_user); |
133 |
+ |
134 |
+ set_uid = tmp_pwd->pw_uid; |
135 |
+ |
136 |
+ /* use the user's group as the default gid */ |
137 |
+ if (!set_group) |
138 |
+ set_gid = tmp_pwd->pw_gid; |
139 |
+ } |
140 |
+ |
141 |
+ if (set_group) { |
142 |
+/* get around the ISC declaration of group */ |
143 |
+#define group real_group |
144 |
+ struct group *tmp_grp; |
145 |
+ |
146 |
+ if (geteuid ()) |
147 |
+ log_fatal ("you must be root to set group"); |
148 |
+ |
149 |
+ if (!(tmp_grp = getgrnam (set_group))) |
150 |
+ log_fatal ("no such group: %s", set_group); |
151 |
+ |
152 |
+ set_gid = tmp_grp->gr_gid; |
153 |
+#undef group |
154 |
+ } |
155 |
+#endif /* PARANOIA */ |
156 |
+#if defined (JAIL) |
157 |
+ if (set_jail) { |
158 |
+ /* Initialize icmp support... */ |
159 |
+ if (!cftest && !lftest) |
160 |
+ icmp_startup (1, lease_pinged); |
161 |
+ if(!set_chroot) |
162 |
+ set_chroot = "/"; |
163 |
+ setup_jail (set_chroot, set_jail, jail_ip_address); |
164 |
+ } |
165 |
+#endif /* JAIL */ |
166 |
+#if defined (PARANOIA) && defined (JAIL) |
167 |
+ else |
168 |
+#endif /* PARANOIA && JAIL */ |
169 |
+#if defined (PARANOIA) |
170 |
+ if (early_chroot && set_chroot) |
171 |
+ setup_chroot (set_chroot); |
172 |
+#endif /* PARANOIA */ |
173 |
+ |
174 |
/* Default to the DHCP/BOOTP port. */ |
175 |
if (!local_port) |
176 |
{ |
177 |
@@ -471,6 +610,9 @@ |
178 |
#endif |
179 |
|
180 |
/* Initialize icmp support... */ |
181 |
+#if defined (JAIL) |
182 |
+ if (!set_jail) |
183 |
+#endif /* JAIL */ |
184 |
if (!cftest && !lftest) |
185 |
icmp_startup (1, lease_pinged); |
186 |
|
187 |
@@ -500,6 +642,14 @@ |
188 |
|
189 |
postconf_initialization (quiet); |
190 |
|
191 |
+#if defined (PARANOIA) |
192 |
+#if defined (JAIL) |
193 |
+ if (!set_jail) |
194 |
+#endif /* JAIL */ |
195 |
+ if (!early_chroot && set_chroot) |
196 |
+ setup_chroot (set_chroot); |
197 |
+#endif /* PARANOIA */ |
198 |
+ |
199 |
/* test option should cause an early exit */ |
200 |
if (cftest && !lftest) |
201 |
exit(0); |
202 |
@@ -542,7 +692,22 @@ |
203 |
else if (pid) |
204 |
exit (0); |
205 |
} |
206 |
+ |
207 |
+#if defined (PARANOIA) |
208 |
+ /* change uid to the specified one */ |
209 |
+ if (set_gid) { |
210 |
+ if (setgroups (0, (void *)0)) |
211 |
+ log_fatal ("setgroups: %m"); |
212 |
+ if (setgid (set_gid)) |
213 |
+ log_fatal ("setgid(%d): %m", (int) set_gid); |
214 |
+ } |
215 |
|
216 |
+ if (set_uid) { |
217 |
+ if (setuid (set_uid)) |
218 |
+ log_fatal ("setuid(%d): %m", (int) set_uid); |
219 |
+ } |
220 |
+#endif /* PARANOIA */ |
221 |
+ |
222 |
/* Read previous pid file. */ |
223 |
if ((i = open (path_dhcpd_pid, O_RDONLY)) >= 0) { |
224 |
status = read (i, pbuf, (sizeof pbuf) - 1); |
225 |
@@ -886,8 +1051,24 @@ |
226 |
log_info (copyright); |
227 |
log_info (arr); |
228 |
|
229 |
- log_fatal ("Usage: dhcpd [-p <UDP port #>] [-d] [-f]%s%s%s%s", |
230 |
+ log_fatal ("Usage: dhcpd [-p <UDP port #>] [-d] [-f]%s%s%s%s%s%s%s", |
231 |
"\n [-cf config-file] [-lf lease-file]", |
232 |
+ |
233 |
+#if defined (PARANOIA) |
234 |
+ /* meld into the following string */ |
235 |
+ "\n [-user user] [-group group]", |
236 |
+ "\n [-chroot dir] [-early_chroot]", |
237 |
+#else /* PARANOIA */ |
238 |
+ "", "", |
239 |
+#endif /* PARANOIA */ |
240 |
+ |
241 |
+#if defined (JAIL) |
242 |
+ /* then also these ones */ |
243 |
+ "\n [-jail name ip]", |
244 |
+#else /* JAIL */ |
245 |
+ "", |
246 |
+#endif /* JAIL */ |
247 |
+ |
248 |
#if defined (TRACING) |
249 |
"\n [-tf trace-output-file]", |
250 |
"\n [-play trace-input-file]", |