c4f2d992 |
1 | /* -*-c-*- |
2 | * |
3 | * $Id: daemon.c,v 1.1 1997/07/21 13:47:50 mdw Exp $ |
4 | * |
5 | * Running a `become' daemon |
6 | * |
7 | * (c) 1997 EBI |
8 | */ |
9 | |
10 | /*----- Licencing notice --------------------------------------------------* |
11 | * |
12 | * This file is part of `become' |
13 | * |
14 | * `Become' is free software; you can redistribute it and/or modify |
15 | * it under the terms of the GNU General Public License as published by |
16 | * the Free Software Foundation; either version 2 of the License, or |
17 | * (at your option) any later version. |
18 | * |
19 | * `Become' is distributed in the hope that it will be useful, |
20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
22 | * GNU General Public License for more details. |
23 | * |
24 | * You should have received a copy of the GNU General Public License |
25 | * along with `become'; if not, write to the Free Software |
26 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
27 | */ |
28 | |
29 | /*----- Revision history --------------------------------------------------* |
30 | * |
31 | * $Log: daemon.c,v $ |
32 | * Revision 1.1 1997/07/21 13:47:50 mdw |
33 | * Initial revision |
34 | * |
35 | */ |
36 | |
37 | /*----- Header files ------------------------------------------------------*/ |
38 | |
39 | /* --- ANSI headers --- */ |
40 | |
41 | #include <errno.h> |
42 | #include <signal.h> |
43 | #include <setjmp.h> |
44 | #include <stdio.h> |
45 | #include <stdlib.h> |
46 | #include <string.h> |
47 | |
48 | /* --- Unix headers --- */ |
49 | |
50 | #include <sys/types.h> |
51 | #include <sys/time.h> |
52 | #include <sys/socket.h> |
53 | |
54 | #include <netinet/in.h> |
55 | |
56 | #include <arpa/inet.h> |
57 | |
58 | #include <netdb.h> |
59 | #include <syslog.h> |
60 | #include <unistd.h> |
61 | |
62 | /* --- Local headers --- */ |
63 | |
64 | #include "become.h" |
65 | #include "config.h" |
66 | #include "crypt.h" |
67 | #include "daemon.h" |
68 | #include "idea.h" |
69 | #include "lexer.h" |
70 | #include "name.h" |
71 | #include "parser.h" |
72 | #include "rule.h" |
73 | #include "tx.h" |
74 | #include "userdb.h" |
75 | #include "utils.h" |
76 | |
77 | /*----- Arbitrary constants -----------------------------------------------*/ |
78 | |
79 | #define daemon__awakeEvery (5 * 60) /* Awaken this often to rescan */ |
80 | |
81 | /*----- Static variables --------------------------------------------------*/ |
82 | |
83 | static int daemon__running = 0; /* Am I running as a daemon? */ |
84 | static int daemon__port = -1; /* No particular port yet */ |
85 | static volatile sig_atomic_t daemon__rescan = 0; /* Rescan as soon as poss */ |
86 | #define daemon__signum daemon__rescan /* Alias for readbility */ |
87 | static int daemon__readKey = 0; /* Have I read a key? */ |
88 | static unsigned char daemon__key[IDEA_KEYSIZE]; /* encryption key */ |
89 | static jmp_buf daemon__dieBuf; /* Jump here to kill the daemon */ |
90 | |
91 | /*----- Main code ---------------------------------------------------------*/ |
92 | |
93 | /* --- @daemon_usePort@ --- * |
94 | * |
95 | * Arguments: @int port@ = port to use, please |
96 | * |
97 | * Returns: --- |
98 | * |
99 | * Use: Instructs the daemon to listen to the given port. |
100 | */ |
101 | |
102 | void daemon_usePort(int port) |
103 | { |
104 | daemon__port = port; |
105 | } |
106 | |
107 | /* --- @daemon_readKey@ --- * |
108 | * |
109 | * Arguments: @const char *kf@ = name of file containing key |
110 | * |
111 | * Returns: --- |
112 | * |
113 | * Use: Instructs the daemon to read the named key file. |
114 | */ |
115 | |
116 | void daemon_readKey(const char *kf) |
117 | { |
118 | FILE *fp; |
119 | |
120 | if (!daemon__running) |
121 | return; |
122 | |
123 | if ((fp = fopen(kf, "r")) == 0) { |
124 | syslog(LOG_WARNING, "couldn't read key file: %e"); |
125 | return; |
126 | } |
127 | tx_getBits(daemon__key, 128, fp); |
128 | fclose(fp); |
129 | daemon__readKey = 1; |
130 | return; |
131 | } |
132 | |
133 | /* --- @daemon__readConfig@ --- * |
134 | * |
135 | * Arguments: @const char *cf@ = pointer to configuration file to use |
136 | * |
137 | * Returns: Zero if it worked, nonzero if it hurt... |
138 | * |
139 | * Use: Reads the configuration file, and other things. |
140 | */ |
141 | |
142 | static int daemon__readConfig(const char *cf) |
143 | { |
144 | FILE *fp; |
145 | |
146 | daemon__readKey = 0; |
147 | if ((fp = fopen(cf, "r")) == 0) |
148 | return (-1); |
149 | lexer_scan(fp); |
150 | yyparse(); |
151 | fclose(fp); |
152 | if (!daemon__readKey) |
153 | daemon_readKey(file_KEY); |
154 | daemon__rescan = 0; |
155 | return (0); |
156 | } |
157 | |
158 | /* --- @daemon__restart@ --- * |
159 | * |
160 | * Arguments: @int sig@ = the signal number |
161 | * |
162 | * Returns: --- |
163 | * |
164 | * Use: Handles signals. Causes the configuration file to be reread. |
165 | */ |
166 | |
167 | static void daemon__restart(int sig) |
168 | { |
169 | daemon__rescan = 1; |
170 | signal(sig, daemon__restart); |
171 | } |
172 | |
173 | /* --- @daemon__die@ --- * |
174 | * |
175 | * Arguments: @int sig@ = the signal number |
176 | * |
177 | * Returns: via @longjmp@ |
178 | * |
179 | * Use: Handles other signals. Causes the daemon to die. |
180 | */ |
181 | |
182 | static void daemon__die(int sig) |
183 | { |
184 | daemon__signum = sig; |
185 | longjmp(daemon__dieBuf, 1); |
186 | } |
187 | |
188 | /* --- @daemon__read@ --- * |
189 | * |
190 | * Arguments: @int fd@ = socket handle |
191 | * |
192 | * Returns: --- |
193 | * |
194 | * Use: Examines a buffer, and returns a response. |
195 | */ |
196 | |
197 | void daemon__read(int fd) |
198 | { |
199 | unsigned char buff[65536]; /* Buffer for incoming packets */ |
200 | unsigned char rpl[crp_size]; /* Buffer for outgoing replies */ |
201 | struct sockaddr_in sin; /* Address of packet sender */ |
202 | char sender[64]; /* Sender's hostname (resolved) */ |
203 | unsigned char sk[IDEA_KEYSIZE]; /* Session key for reply */ |
204 | request rq; /* Request buffer for verification */ |
205 | |
206 | /* --- Read the message --- */ |
207 | |
208 | { |
209 | int slen = sizeof(sin); |
210 | |
211 | if (recvfrom(fd, (char *)buff, sizeof(buff), 0, |
212 | (struct sockaddr *)&sin, &slen) < 0) { |
213 | syslog(LOG_INFO, "duff packet received: %e"); |
214 | return; |
215 | } |
216 | } |
217 | |
218 | /* --- Resolve the host name --- */ |
219 | |
220 | { |
221 | struct hostent *he = gethostbyaddr((char *)&sin.sin_addr, |
222 | sizeof(sin.sin_addr), |
223 | AF_INET); |
224 | sender[0] = 0; |
225 | strncat(sender, |
226 | he ? he->h_name : inet_ntoa(sin.sin_addr), |
227 | sizeof(sender)); |
228 | syslog(LOG_DEBUG, "packet received from %s", sender); |
229 | } |
230 | |
231 | /* --- Unpack the block --- */ |
232 | |
233 | if (crypt_unpackRequest(&rq, buff, daemon__key, sk, rpl) == 0) { |
234 | burn(buff); |
235 | syslog(LOG_INFO, "packet from %s rejected", sender); |
236 | return; |
237 | } |
238 | burn(buff); |
239 | |
240 | /* --- Fill in the sender's address in the request block --- */ |
241 | |
242 | rq.host = sin.sin_addr; |
243 | |
244 | /* --- Build a reply block --- */ |
245 | |
246 | { |
247 | int answer = rule_check(&rq); |
248 | syslog(LOG_INFO, "request from %s for %i to become %i to run %s %s", |
249 | sender, rq.from, rq.to, rq.cmd, answer ? "granted" : "denied"); |
250 | crypt_packReply(rpl, sk, answer); |
251 | burn(sk); |
252 | } |
253 | |
254 | /* --- Send the reply off --- */ |
255 | |
256 | sendto(fd, (char *)rpl, crp_size, 0, (struct sockaddr *)&sin, sizeof(sin)); |
257 | burn(rpl); |
258 | } |
259 | |
260 | /* --- @daemon_init@ --- * |
261 | * |
262 | * Arguments: @const char *cf@ = pointer to name of configuration file |
263 | * @int port@ = port to listen to, or %$-1$% for default |
264 | * |
265 | * Returns: Never. |
266 | * |
267 | * Use: Starts `become' up in daemon mode. |
268 | */ |
269 | |
270 | void daemon_init(const char *cf, int port) |
271 | { |
272 | int s; |
273 | |
274 | /* --- Remove my root privileges --- * |
275 | * |
276 | * Just in case there's anything dodgy in my configuration file, or the |
277 | * user wants me to start on a funny port. |
278 | */ |
279 | |
280 | seteuid(getuid()); |
281 | |
282 | /* --- Initialise bits of the program --- */ |
283 | |
284 | daemon__running = 1; |
285 | daemon__port = port; |
286 | userdb_init(); |
287 | userdb_local(); |
288 | userdb_yp(); |
289 | name_init(); |
290 | rule_init(); |
291 | openlog(quis(), 0, LOG_DAEMON); |
292 | syslog(LOG_NOTICE, "starting up"); |
293 | |
294 | if (daemon__readConfig(cf)) |
295 | die("couldn't read configuration file"); |
296 | |
297 | /* --- Decide on a port to use --- * |
298 | * |
299 | * If I don't have a port yet (e.g., from the configuration file) then |
300 | * look it up in /etc/services under whatever name I was started as. |
301 | */ |
302 | |
303 | if (daemon__port <= 0) { |
304 | struct servent *se = getservbyname(quis(), "udp"); |
305 | if (!se) |
306 | die("no idea which port to use"); |
307 | daemon__port = ntohs(se->s_port); |
308 | } |
309 | |
310 | /* --- Now set up a socket --- */ |
311 | |
312 | { |
313 | struct sockaddr_in sin; |
314 | |
315 | if ((s = socket(PF_INET, SOCK_DGRAM, 0)) == -1) |
316 | die("couldn't create socket: %s", strerror(errno)); |
317 | sin.sin_family = AF_INET; |
318 | sin.sin_port = htons(daemon__port); |
319 | sin.sin_addr.s_addr = htonl(INADDR_ANY); |
320 | if (bind(s, (struct sockaddr *)&sin, sizeof(sin))) |
321 | die("couldn't bind socket to port: %s", strerror(errno)); |
322 | } |
323 | |
324 | /* --- Fork off into the sunset --- */ |
325 | |
326 | #ifdef NDEBUG |
327 | { |
328 | int pid = fork(); |
329 | FILE *fp; |
330 | |
331 | /* --- Make a background process --- */ |
332 | |
333 | if (pid == -1) |
334 | die("couldn't fork daemon: %s", strerror(errno)); |
335 | else if (pid != 0) |
336 | return; |
337 | |
338 | /* --- Disconnect from the terminal --- */ |
339 | |
340 | setsid(); |
341 | |
342 | /* --- Write my process id to a file --- */ |
343 | |
344 | if ((fp = fopen(file_PID, "w")) != 0) { |
345 | fprintf(fp, "%lu\n", (unsigned long)getpid()); |
346 | fclose(fp); |
347 | } |
348 | } |
349 | #endif |
350 | |
351 | /* --- Program in daemon death mode --- */ |
352 | |
353 | if (setjmp(daemon__dieBuf)) { |
354 | syslog(LOG_NOTICE, "killed by signal type %i", daemon__signum); |
355 | remove(file_PID); |
356 | exit(0); |
357 | } |
358 | |
359 | /* --- Set signal handlers --- */ |
360 | |
361 | signal(SIGHUP, daemon__restart); |
362 | signal(SIGQUIT, daemon__restart); |
363 | signal(SIGINT, daemon__die); |
364 | signal(SIGTERM, daemon__die); |
365 | signal(SIGSEGV, daemon__die); |
366 | signal(SIGFPE, daemon__die); |
367 | signal(SIGBUS, daemon__die); |
368 | |
369 | /* --- Now wait for something exciting to happen --- * |
370 | * |
371 | * Actually, every so often (5 minutes, perhaps) I need to wake up and |
372 | * rescan the users to see whether they've changed. Time to play with |
373 | * @select@. |
374 | */ |
375 | |
376 | { |
377 | time_t when; |
378 | |
379 | /* --- Find when I am, and thus when I need to be awoken again --- */ |
380 | |
381 | when = time(0) + daemon__awakeEvery; |
382 | |
383 | for (;;) { |
384 | fd_set fds; |
385 | int i; |
386 | |
387 | /* --- Set up the file descriptor tables --- */ |
388 | |
389 | FD_ZERO(&fds); |
390 | FD_SET(s, &fds); |
391 | |
392 | /* --- Now wait for something interesting --- */ |
393 | |
394 | i = select(FD_SETSIZE, &fds, 0, 0, 0); |
395 | |
396 | /* --- Now, see if I need to rescan the config --- */ |
397 | |
398 | if (daemon__rescan || time(0) - when > 0) { |
399 | daemon__rescan = 0; |
400 | syslog(LOG_INFO, "rescanning configuration file"); |
401 | userdb_reinit(); |
402 | userdb_local(); |
403 | userdb_yp(); |
404 | rule_reinit(); |
405 | name_reinit(); |
406 | if (daemon__readConfig(cf)) |
407 | syslog(LOG_ERR, "error reading configuration file"); |
408 | when = time(0) + daemon__awakeEvery; |
409 | } |
410 | |
411 | /* --- Read the data from the request --- */ |
412 | |
413 | if (i > 0) |
414 | daemon__read(s); |
415 | } |
416 | } |
417 | } |
418 | |
419 | /*----- That's all, folks -------------------------------------------------*/ |