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