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