3 * $Id: daemon.c,v 1.5 1997/08/20 16:17:10 mdw Exp $
5 * Running a `become' daemon
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of `become'
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.
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.
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.
29 /*----- Revision history --------------------------------------------------*
32 * Revision 1.5 1997/08/20 16:17:10 mdw
33 * More sensible restart routine: `_reinit' functions replaced by `_end' and
36 * Revision 1.4 1997/08/07 10:00:37 mdw
37 * (Log entry for previous version is bogus.) Read netgroups database.
38 * Give up privileges permanently on startup.
40 * Revision 1.2 1997/08/04 10:24:21 mdw
41 * Sources placed under CVS control.
43 * Revision 1.1 1997/07/21 13:47:50 mdw
48 /*----- Header files ------------------------------------------------------*/
50 /* --- ANSI headers --- */
59 /* --- Unix headers --- */
61 #include <sys/types.h>
63 #include <sys/socket.h>
65 #include <netinet/in.h>
67 #include <arpa/inet.h>
73 /* --- Local headers --- */
89 /*----- Arbitrary constants -----------------------------------------------*/
91 #define daemon__awakeEvery (30 * 60) /* Awaken this often to rescan */
93 /*----- Static variables --------------------------------------------------*/
95 static int daemon__running
= 0; /* Am I running as a daemon? */
96 static int daemon__port
= -1; /* No particular port yet */
97 static volatile sig_atomic_t daemon__rescan
= 0; /* Rescan as soon as poss */
98 #define daemon__signum daemon__rescan /* Alias for readbility */
99 static int daemon__readKey
= 0; /* Have I read a key? */
100 static unsigned char daemon__key
[IDEA_KEYSIZE
]; /* encryption key */
101 static jmp_buf daemon__dieBuf
; /* Jump here to kill the daemon */
103 /*----- Main code ---------------------------------------------------------*/
105 /* --- @daemon_usePort@ --- *
107 * Arguments: @int port@ = port to use, please
111 * Use: Instructs the daemon to listen to the given port.
114 void daemon_usePort(int port
)
119 /* --- @daemon_readKey@ --- *
121 * Arguments: @const char *kf@ = name of file containing key
125 * Use: Instructs the daemon to read the named key file.
128 void daemon_readKey(const char *kf
)
132 if (!daemon__running
)
135 if ((fp
= fopen(kf
, "r")) == 0) {
136 syslog(LOG_WARNING
, "couldn't read key file: %e");
139 tx_getBits(daemon__key
, 128, fp
);
145 /* --- @daemon__readConfig@ --- *
147 * Arguments: @const char *cf@ = pointer to configuration file to use
149 * Returns: Zero if it worked, nonzero if it hurt...
151 * Use: Reads the configuration file, and other things.
154 static int daemon__readConfig(const char *cf
)
159 if ((fp
= fopen(cf
, "r")) == 0)
164 if (!daemon__readKey
)
165 daemon_readKey(file_KEY
);
167 T( trace(TRACE_DAEMON
, "daemon: read config file"); )
171 /* --- @daemon__restart@ --- *
173 * Arguments: @int sig@ = the signal number
177 * Use: Handles signals. Causes the configuration file to be reread.
180 static void daemon__restart(int sig
)
183 signal(sig
, daemon__restart
);
186 /* --- @daemon__die@ --- *
188 * Arguments: @int sig@ = the signal number
190 * Returns: via @longjmp@
192 * Use: Handles other signals. Causes the daemon to die.
195 static void daemon__die(int sig
)
197 daemon__signum
= sig
;
198 longjmp(daemon__dieBuf
, 1);
201 /* --- @daemon__read@ --- *
203 * Arguments: @int fd@ = socket handle
207 * Use: Examines a buffer, and returns a response.
210 void daemon__read(int fd
)
212 unsigned char buff
[65536]; /* Buffer for incoming packets */
213 unsigned char rpl
[crp_size
]; /* Buffer for outgoing replies */
214 struct sockaddr_in sin
; /* Address of packet sender */
215 char sender
[64]; /* Sender's hostname (resolved) */
216 unsigned char sk
[IDEA_KEYSIZE
]; /* Session key for reply */
217 request rq
; /* Request buffer for verification */
219 /* --- Read the message --- */
222 int slen
= sizeof(sin
);
224 if (recvfrom(fd
, (char *)buff
, sizeof(buff
), 0,
225 (struct sockaddr
*)&sin
, &slen
) < 0) {
226 T( trace(TRACE_DAEMON
, "daemon: error reading packet: %s",
228 syslog(LOG_INFO
, "duff packet received: %e");
233 /* --- Resolve the host name --- */
236 struct hostent
*he
= gethostbyaddr((char *)&sin
.sin_addr
,
237 sizeof(sin
.sin_addr
),
241 he ? he
->h_name
: inet_ntoa(sin
.sin_addr
),
243 syslog(LOG_DEBUG
, "packet received from %s", sender
);
244 T( trace(TRACE_DAEMON
, "daemon: received request from %s", sender
); )
247 /* --- Unpack the block --- */
249 if (crypt_unpackRequest(&rq
, buff
, daemon__key
, sk
, rpl
) == 0) {
251 T( trace(TRACE_DAEMON
, "daemon: received corrupt or invalid request"); )
252 syslog(LOG_INFO
, "packet from %s rejected", sender
);
257 /* --- Fill in the sender's address in the request block --- */
259 rq
.host
= sin
.sin_addr
;
261 /* --- Build a reply block --- */
264 int answer
= rule_check(&rq
);
265 syslog(LOG_INFO
, "request from %s for %i to become %i to run %s %s",
266 sender
, rq
.from
, rq
.to
, rq
.cmd
, answer ?
"granted" : "denied");
267 crypt_packReply(rpl
, sk
, answer
);
271 /* --- Send the reply off --- */
273 sendto(fd
, (char *)rpl
, crp_size
, 0, (struct sockaddr
*)&sin
, sizeof(sin
));
274 T( trace(TRACE_DAEMON
, "daemon: reply sent"); )
278 /* --- @daemon_init@ --- *
280 * Arguments: @const char *cf@ = pointer to name of configuration file
281 * @int port@ = port to listen to, or %$-1$% for default
285 * Use: Starts `become' up in daemon mode.
288 void daemon_init(const char *cf
, int port
)
292 /* --- Remove my root privileges --- *
294 * Just in case there's anything dodgy in my configuration file, or the
295 * user wants me to start on a funny port.
300 /* --- Initialise bits of the program --- */
310 openlog(quis(), 0, LOG_DAEMON
);
311 syslog(LOG_NOTICE
, "starting up");
313 if (daemon__readConfig(cf
))
314 die("couldn't read configuration file");
316 /* --- Decide on a port to use --- *
318 * If I don't have a port yet (e.g., from the configuration file) then
319 * look it up in /etc/services under whatever name I was started as.
322 if (daemon__port
<= 0) {
323 struct servent
*se
= getservbyname(quis(), "udp");
325 die("no idea which port to use");
326 daemon__port
= ntohs(se
->s_port
);
329 /* --- Now set up a socket --- */
332 struct sockaddr_in sin
;
334 if ((s
= socket(PF_INET
, SOCK_DGRAM
, 0)) == -1)
335 die("couldn't create socket: %s", strerror(errno
));
336 sin
.sin_family
= AF_INET
;
337 sin
.sin_port
= htons(daemon__port
);
338 sin
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
339 if (bind(s
, (struct sockaddr
*)&sin
, sizeof(sin
)))
340 die("couldn't bind socket to port: %s", strerror(errno
));
343 /* --- Fork off into the sunset --- */
350 /* --- Make a background process --- */
353 die("couldn't fork daemon: %s", strerror(errno
));
357 /* --- Disconnect from the terminal --- */
361 /* --- Write my process id to a file --- */
363 if ((fp
= fopen(file_PID
, "w")) != 0) {
364 fprintf(fp
, "%lu\n", (unsigned long)getpid());
367 T( trace(TRACE_DAEMON
, "daemon: forked to pid %li", (long)getpid()); )
371 /* --- Program in daemon death mode --- */
373 if (setjmp(daemon__dieBuf
)) {
375 if (daemon__signum
== SIGQUIT
&& tracing() & TRACE_RULE
) {
377 signal(SIGQUIT
, daemon__die
);
381 T( trace(TRACE_DAEMON
, "daemon: killed by signal %i",
383 syslog(LOG_NOTICE
, "killed by signal type %i", daemon__signum
);
389 /* --- Set signal handlers --- */
391 signal(SIGHUP
, daemon__restart
);
392 signal(SIGQUIT
, daemon__die
);
393 signal(SIGINT
, daemon__die
);
394 signal(SIGTERM
, daemon__die
);
395 signal(SIGSEGV
, daemon__die
);
396 signal(SIGFPE
, daemon__die
);
397 signal(SIGBUS
, daemon__die
);
400 /* --- Now wait for something exciting to happen --- *
402 * Actually, every so often (5 minutes, perhaps) I need to wake up and
403 * rescan the users to see whether they've changed. Time to play with
410 /* --- Find when I am, and thus when I need to be awoken again --- */
412 when
= time(0) + daemon__awakeEvery
;
418 /* --- Set up the file descriptor tables --- */
423 /* --- Now wait for something interesting --- */
425 T( trace(TRACE_DAEMON
, "daemon: waiting for requests"); )
426 i
= select(FD_SETSIZE
, &fds
, 0, 0, 0);
428 /* --- Now, see if I need to rescan the config --- */
430 if (daemon__rescan
|| time(0) - when
> 0) {
432 syslog(LOG_INFO
, "rescanning configuration file");
443 if (daemon__readConfig(cf
))
444 syslog(LOG_ERR
, "error reading configuration file");
445 when
= time(0) + daemon__awakeEvery
;
448 /* --- Read the data from the request --- */
456 /*----- That's all, folks -------------------------------------------------*/