3 * $Id: daemon.c,v 1.7 1997/09/17 10:23:23 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.7 1997/09/17 10:23:23 mdw
33 * Fix a typo. Port numbers are in network order now, so don't change them.
35 * Revision 1.6 1997/09/09 18:17:06 mdw
36 * Allow default port to be given as a service name or port number.
38 * Revision 1.5 1997/08/20 16:17:10 mdw
39 * More sensible restart routine: `_reinit' functions replaced by `_end' and
42 * Revision 1.4 1997/08/07 10:00:37 mdw
43 * (Log entry for previous version is bogus.) Read netgroups database.
44 * Give up privileges permanently on startup.
46 * Revision 1.2 1997/08/04 10:24:21 mdw
47 * Sources placed under CVS control.
49 * Revision 1.1 1997/07/21 13:47:50 mdw
54 /*----- Header files ------------------------------------------------------*/
56 /* --- ANSI headers --- */
65 /* --- Unix headers --- */
67 #include <sys/types.h>
69 #include <sys/socket.h>
71 #include <netinet/in.h>
73 #include <arpa/inet.h>
79 /* --- Local headers --- */
95 /*----- Arbitrary constants -----------------------------------------------*/
97 #define daemon__awakeEvery (30 * 60) /* Awaken this often to rescan */
99 /*----- Static variables --------------------------------------------------*/
101 static int daemon__running
= 0; /* Am I running as a daemon? */
102 static int daemon__port
= -1; /* No particular port yet */
103 static volatile sig_atomic_t daemon__rescan
= 0; /* Rescan as soon as poss */
104 #define daemon__signum daemon__rescan /* Alias for readbility */
105 static int daemon__readKey
= 0; /* Have I read a key? */
106 static unsigned char daemon__key
[IDEA_KEYSIZE
]; /* encryption key */
107 static jmp_buf daemon__dieBuf
; /* Jump here to kill the daemon */
109 /*----- Main code ---------------------------------------------------------*/
111 /* --- @daemon_usePort@ --- *
113 * Arguments: @int port@ = port to use, please
117 * Use: Instructs the daemon to listen to the given port.
120 void daemon_usePort(int port
)
125 /* --- @daemon_readKey@ --- *
127 * Arguments: @const char *kf@ = name of file containing key
131 * Use: Instructs the daemon to read the named key file.
134 void daemon_readKey(const char *kf
)
138 if (!daemon__running
)
141 if ((fp
= fopen(kf
, "r")) == 0) {
142 syslog(LOG_WARNING
, "couldn't read key file: %e");
145 tx_getBits(daemon__key
, 128, fp
);
151 /* --- @daemon__readConfig@ --- *
153 * Arguments: @const char *cf@ = pointer to configuration file to use
155 * Returns: Zero if it worked, nonzero if it hurt...
157 * Use: Reads the configuration file, and other things.
160 static int daemon__readConfig(const char *cf
)
165 if ((fp
= fopen(cf
, "r")) == 0)
170 if (!daemon__readKey
)
171 daemon_readKey(file_KEY
);
173 T( trace(TRACE_DAEMON
, "daemon: read config file"); )
177 /* --- @daemon__restart@ --- *
179 * Arguments: @int sig@ = the signal number
183 * Use: Handles signals. Causes the configuration file to be reread.
186 static void daemon__restart(int sig
)
189 signal(sig
, daemon__restart
);
192 /* --- @daemon__die@ --- *
194 * Arguments: @int sig@ = the signal number
196 * Returns: via @longjmp@
198 * Use: Handles other signals. Causes the daemon to die.
201 static void daemon__die(int sig
)
203 daemon__signum
= sig
;
204 longjmp(daemon__dieBuf
, 1);
207 /* --- @daemon__read@ --- *
209 * Arguments: @int fd@ = socket handle
213 * Use: Examines a buffer, and returns a response.
216 void daemon__read(int fd
)
218 unsigned char buff
[65536]; /* Buffer for incoming packets */
219 unsigned char rpl
[crp_size
]; /* Buffer for outgoing replies */
220 struct sockaddr_in sin
; /* Address of packet sender */
221 char sender
[64]; /* Sender's hostname (resolved) */
222 unsigned char sk
[IDEA_KEYSIZE
]; /* Session key for reply */
223 request rq
; /* Request buffer for verification */
225 /* --- Read the message --- */
228 int slen
= sizeof(sin
);
230 if (recvfrom(fd
, (char *)buff
, sizeof(buff
), 0,
231 (struct sockaddr
*)&sin
, &slen
) < 0) {
232 T( trace(TRACE_DAEMON
, "daemon: error reading packet: %s",
234 syslog(LOG_INFO
, "duff packet received: %e");
239 /* --- Resolve the host name --- */
242 struct hostent
*he
= gethostbyaddr((char *)&sin
.sin_addr
,
243 sizeof(sin
.sin_addr
),
247 he ? he
->h_name
: inet_ntoa(sin
.sin_addr
),
249 syslog(LOG_DEBUG
, "packet received from %s", sender
);
250 T( trace(TRACE_DAEMON
, "daemon: received request from %s", sender
); )
253 /* --- Unpack the block --- */
255 if (crypt_unpackRequest(&rq
, buff
, daemon__key
, sk
, rpl
) == 0) {
257 T( trace(TRACE_DAEMON
, "daemon: received corrupt or invalid request"); )
258 syslog(LOG_INFO
, "packet from %s rejected", sender
);
263 /* --- Fill in the sender's address in the request block --- */
265 rq
.host
= sin
.sin_addr
;
267 /* --- Build a reply block --- */
270 int answer
= rule_check(&rq
);
271 syslog(LOG_INFO
, "request from %s for %i to become %i to run %s %s",
272 sender
, rq
.from
, rq
.to
, rq
.cmd
, answer ?
"granted" : "denied");
273 crypt_packReply(rpl
, sk
, answer
);
277 /* --- Send the reply off --- */
279 sendto(fd
, (char *)rpl
, crp_size
, 0, (struct sockaddr
*)&sin
, sizeof(sin
));
280 T( trace(TRACE_DAEMON
, "daemon: reply sent"); )
284 /* --- @daemon_init@ --- *
286 * Arguments: @const char *cf@ = pointer to name of configuration file
287 * @int port@ = port to listen to, or %$-1$% for default
291 * Use: Starts `become' up in daemon mode.
294 void daemon_init(const char *cf
, int port
)
298 /* --- Remove my root privileges --- *
300 * Just in case there's anything dodgy in my configuration file, or the
301 * user wants me to start on a funny port.
306 /* --- Initialise bits of the program --- */
316 openlog(quis(), 0, LOG_DAEMON
);
317 syslog(LOG_NOTICE
, "starting up");
319 if (daemon__readConfig(cf
))
320 die("couldn't read configuration file");
322 /* --- Decide on a port to use --- *
324 * If I don't have a port yet (e.g., from the configuration file) then
325 * look it up in /etc/services under whatever name I was started as.
328 if (daemon__port
== 0) {
329 struct servent
*se
= getservbyname(quis(), "udp");
331 die("no idea which port to use");
332 daemon__port
= se
->s_port
;
335 /* --- Now set up a socket --- */
338 struct sockaddr_in sin
;
340 if ((s
= socket(PF_INET
, SOCK_DGRAM
, 0)) == -1)
341 die("couldn't create socket: %s", strerror(errno
));
342 sin
.sin_family
= AF_INET
;
343 sin
.sin_port
= daemon__port
;
344 sin
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
345 if (bind(s
, (struct sockaddr
*)&sin
, sizeof(sin
))) {
346 die("couldn't bind socket to port %i: %s",
347 ntohs(daemon__port
), strerror(errno
));
351 /* --- Fork off into the sunset --- */
358 /* --- Make a background process --- */
361 die("couldn't fork daemon: %s", strerror(errno
));
365 /* --- Disconnect from the terminal --- */
369 /* --- Write my process id to a file --- */
371 if ((fp
= fopen(file_PID
, "w")) != 0) {
372 fprintf(fp
, "%lu\n", (unsigned long)getpid());
375 T( trace(TRACE_DAEMON
, "daemon: forked to pid %li", (long)getpid()); )
379 /* --- Program in daemon death mode --- */
381 if (setjmp(daemon__dieBuf
)) {
383 if (daemon__signum
== SIGQUIT
&& tracing() & TRACE_RULE
) {
385 signal(SIGQUIT
, daemon__die
);
389 T( trace(TRACE_DAEMON
, "daemon: killed by signal %i",
391 syslog(LOG_NOTICE
, "killed by signal type %i", daemon__signum
);
397 /* --- Set signal handlers --- */
399 signal(SIGHUP
, daemon__restart
);
400 signal(SIGQUIT
, daemon__die
);
401 signal(SIGINT
, daemon__die
);
402 signal(SIGTERM
, daemon__die
);
403 signal(SIGSEGV
, daemon__die
);
404 signal(SIGFPE
, daemon__die
);
405 signal(SIGBUS
, daemon__die
);
408 /* --- Now wait for something exciting to happen --- *
410 * Actually, every so often (5 minutes, perhaps) I need to wake up and
411 * rescan the users to see whether they've changed. Time to play with
418 /* --- Find when I am, and thus when I need to be awoken again --- */
420 when
= time(0) + daemon__awakeEvery
;
426 /* --- Set up the file descriptor tables --- */
431 /* --- Now wait for something interesting --- */
433 T( trace(TRACE_DAEMON
, "daemon: waiting for requests"); )
434 i
= select(FD_SETSIZE
, &fds
, 0, 0, 0);
436 /* --- Now, see if I need to rescan the config --- */
438 if (daemon__rescan
|| time(0) - when
> 0) {
440 syslog(LOG_INFO
, "rescanning configuration file");
451 if (daemon__readConfig(cf
))
452 syslog(LOG_ERR
, "error reading configuration file");
453 when
= time(0) + daemon__awakeEvery
;
456 /* --- Read the data from the request --- */
464 /*----- That's all, folks -------------------------------------------------*/