3 * $Id: daemon.c,v 1.6 1997/09/09 18:17:06 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.6 1997/09/09 18:17:06 mdw
33 * Allow default port to be given as a service name or port number.
35 * Revision 1.5 1997/08/20 16:17:10 mdw
36 * More sensible restart routine: `_reinit' functions replaced by `_end' and
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.
43 * Revision 1.2 1997/08/04 10:24:21 mdw
44 * Sources placed under CVS control.
46 * Revision 1.1 1997/07/21 13:47:50 mdw
51 /*----- Header files ------------------------------------------------------*/
53 /* --- ANSI headers --- */
62 /* --- Unix headers --- */
64 #include <sys/types.h>
66 #include <sys/socket.h>
68 #include <netinet/in.h>
70 #include <arpa/inet.h>
76 /* --- Local headers --- */
92 /*----- Arbitrary constants -----------------------------------------------*/
94 #define daemon__awakeEvery (30 * 60) /* Awaken this often to rescan */
96 /*----- Static variables --------------------------------------------------*/
98 static int daemon__running
= 0; /* Am I running as a daemon? */
99 static int daemon__port
= -1; /* No particular port yet */
100 static volatile sig_atomic_t daemon__rescan
= 0; /* Rescan as soon as poss */
101 #define daemon__signum daemon__rescan /* Alias for readbility */
102 static int daemon__readKey
= 0; /* Have I read a key? */
103 static unsigned char daemon__key
[IDEA_KEYSIZE
]; /* encryption key */
104 static jmp_buf daemon__dieBuf
; /* Jump here to kill the daemon */
106 /*----- Main code ---------------------------------------------------------*/
108 /* --- @daemon_usePort@ --- *
110 * Arguments: @int port@ = port to use, please
114 * Use: Instructs the daemon to listen to the given port.
117 void daemon_usePort(int port
)
122 /* --- @daemon_readKey@ --- *
124 * Arguments: @const char *kf@ = name of file containing key
128 * Use: Instructs the daemon to read the named key file.
131 void daemon_readKey(const char *kf
)
135 if (!daemon__running
)
138 if ((fp
= fopen(kf
, "r")) == 0) {
139 syslog(LOG_WARNING
, "couldn't read key file: %e");
142 tx_getBits(daemon__key
, 128, fp
);
148 /* --- @daemon__readConfig@ --- *
150 * Arguments: @const char *cf@ = pointer to configuration file to use
152 * Returns: Zero if it worked, nonzero if it hurt...
154 * Use: Reads the configuration file, and other things.
157 static int daemon__readConfig(const char *cf
)
162 if ((fp
= fopen(cf
, "r")) == 0)
167 if (!daemon__readKey
)
168 daemon_readKey(file_KEY
);
170 T( trace(TRACE_DAEMON
, "daemon: read config file"); )
174 /* --- @daemon__restart@ --- *
176 * Arguments: @int sig@ = the signal number
180 * Use: Handles signals. Causes the configuration file to be reread.
183 static void daemon__restart(int sig
)
186 signal(sig
, daemon__restart
);
189 /* --- @daemon__die@ --- *
191 * Arguments: @int sig@ = the signal number
193 * Returns: via @longjmp@
195 * Use: Handles other signals. Causes the daemon to die.
198 static void daemon__die(int sig
)
200 daemon__signum
= sig
;
201 longjmp(daemon__dieBuf
, 1);
204 /* --- @daemon__read@ --- *
206 * Arguments: @int fd@ = socket handle
210 * Use: Examines a buffer, and returns a response.
213 void daemon__read(int fd
)
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 */
222 /* --- Read the message --- */
225 int slen
= sizeof(sin
);
227 if (recvfrom(fd
, (char *)buff
, sizeof(buff
), 0,
228 (struct sockaddr
*)&sin
, &slen
) < 0) {
229 T( trace(TRACE_DAEMON
, "daemon: error reading packet: %s",
231 syslog(LOG_INFO
, "duff packet received: %e");
236 /* --- Resolve the host name --- */
239 struct hostent
*he
= gethostbyaddr((char *)&sin
.sin_addr
,
240 sizeof(sin
.sin_addr
),
244 he ? he
->h_name
: inet_ntoa(sin
.sin_addr
),
246 syslog(LOG_DEBUG
, "packet received from %s", sender
);
247 T( trace(TRACE_DAEMON
, "daemon: received request from %s", sender
); )
250 /* --- Unpack the block --- */
252 if (crypt_unpackRequest(&rq
, buff
, daemon__key
, sk
, rpl
) == 0) {
254 T( trace(TRACE_DAEMON
, "daemon: received corrupt or invalid request"); )
255 syslog(LOG_INFO
, "packet from %s rejected", sender
);
260 /* --- Fill in the sender's address in the request block --- */
262 rq
.host
= sin
.sin_addr
;
264 /* --- Build a reply block --- */
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
);
274 /* --- Send the reply off --- */
276 sendto(fd
, (char *)rpl
, crp_size
, 0, (struct sockaddr
*)&sin
, sizeof(sin
));
277 T( trace(TRACE_DAEMON
, "daemon: reply sent"); )
281 /* --- @daemon_init@ --- *
283 * Arguments: @const char *cf@ = pointer to name of configuration file
284 * @int port@ = port to listen to, or %$-1$% for default
288 * Use: Starts `become' up in daemon mode.
291 void daemon_init(const char *cf
, int port
)
295 /* --- Remove my root privileges --- *
297 * Just in case there's anything dodgy in my configuration file, or the
298 * user wants me to start on a funny port.
303 /* --- Initialise bits of the program --- */
313 openlog(quis(), 0, LOG_DAEMON
);
314 syslog(LOG_NOTICE
, "starting up");
316 if (daemon__readConfig(cf
))
317 die("couldn't read configuration file");
319 /* --- Decide on a port to use --- *
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.
325 if (daemon__port
<= 0) {
326 struct servent
*se
= getservbyname(quis(), "udp");
328 die("no idea which port to use");
329 daemon__port
= se
->s_port
;
332 /* --- Now set up a socket --- */
335 struct sockaddr_in sin
;
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
));
346 /* --- Fork off into the sunset --- */
353 /* --- Make a background process --- */
356 die("couldn't fork daemon: %s", strerror(errno
));
360 /* --- Disconnect from the terminal --- */
364 /* --- Write my process id to a file --- */
366 if ((fp
= fopen(file_PID
, "w")) != 0) {
367 fprintf(fp
, "%lu\n", (unsigned long)getpid());
370 T( trace(TRACE_DAEMON
, "daemon: forked to pid %li", (long)getpid()); )
374 /* --- Program in daemon death mode --- */
376 if (setjmp(daemon__dieBuf
)) {
378 if (daemon__signum
== SIGQUIT
&& tracing() & TRACE_RULE
) {
380 signal(SIGQUIT
, daemon__die
);
384 T( trace(TRACE_DAEMON
, "daemon: killed by signal %i",
386 syslog(LOG_NOTICE
, "killed by signal type %i", daemon__signum
);
392 /* --- Set signal handlers --- */
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
);
403 /* --- Now wait for something exciting to happen --- *
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
413 /* --- Find when I am, and thus when I need to be awoken again --- */
415 when
= time(0) + daemon__awakeEvery
;
421 /* --- Set up the file descriptor tables --- */
426 /* --- Now wait for something interesting --- */
428 T( trace(TRACE_DAEMON
, "daemon: waiting for requests"); )
429 i
= select(FD_SETSIZE
, &fds
, 0, 0, 0);
431 /* --- Now, see if I need to rescan the config --- */
433 if (daemon__rescan
|| time(0) - when
> 0) {
435 syslog(LOG_INFO
, "rescanning configuration file");
446 if (daemon__readConfig(cf
))
447 syslog(LOG_ERR
, "error reading configuration file");
448 when
= time(0) + daemon__awakeEvery
;
451 /* --- Read the data from the request --- */
459 /*----- That's all, folks -------------------------------------------------*/