| 1 | /* -*-c-*- |
| 2 | * |
| 3 | * $Id: daemon.c,v 1.5 1997/08/20 16:17:10 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.5 1997/08/20 16:17:10 mdw |
| 33 | * More sensible restart routine: `_reinit' functions replaced by `_end' and |
| 34 | * `_init' functions. |
| 35 | * |
| 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. |
| 39 | * |
| 40 | * Revision 1.2 1997/08/04 10:24:21 mdw |
| 41 | * Sources placed under CVS control. |
| 42 | * |
| 43 | * Revision 1.1 1997/07/21 13:47:50 mdw |
| 44 | * Initial revision |
| 45 | * |
| 46 | */ |
| 47 | |
| 48 | /*----- Header files ------------------------------------------------------*/ |
| 49 | |
| 50 | /* --- ANSI headers --- */ |
| 51 | |
| 52 | #include <errno.h> |
| 53 | #include <signal.h> |
| 54 | #include <setjmp.h> |
| 55 | #include <stdio.h> |
| 56 | #include <stdlib.h> |
| 57 | #include <string.h> |
| 58 | |
| 59 | /* --- Unix headers --- */ |
| 60 | |
| 61 | #include <sys/types.h> |
| 62 | #include <sys/time.h> |
| 63 | #include <sys/socket.h> |
| 64 | |
| 65 | #include <netinet/in.h> |
| 66 | |
| 67 | #include <arpa/inet.h> |
| 68 | |
| 69 | #include <netdb.h> |
| 70 | #include <syslog.h> |
| 71 | #include <unistd.h> |
| 72 | |
| 73 | /* --- Local headers --- */ |
| 74 | |
| 75 | #include "become.h" |
| 76 | #include "config.h" |
| 77 | #include "crypt.h" |
| 78 | #include "daemon.h" |
| 79 | #include "idea.h" |
| 80 | #include "lexer.h" |
| 81 | #include "name.h" |
| 82 | #include "netg.h" |
| 83 | #include "parser.h" |
| 84 | #include "rule.h" |
| 85 | #include "tx.h" |
| 86 | #include "userdb.h" |
| 87 | #include "utils.h" |
| 88 | |
| 89 | /*----- Arbitrary constants -----------------------------------------------*/ |
| 90 | |
| 91 | #define daemon__awakeEvery (30 * 60) /* Awaken this often to rescan */ |
| 92 | |
| 93 | /*----- Static variables --------------------------------------------------*/ |
| 94 | |
| 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 */ |
| 102 | |
| 103 | /*----- Main code ---------------------------------------------------------*/ |
| 104 | |
| 105 | /* --- @daemon_usePort@ --- * |
| 106 | * |
| 107 | * Arguments: @int port@ = port to use, please |
| 108 | * |
| 109 | * Returns: --- |
| 110 | * |
| 111 | * Use: Instructs the daemon to listen to the given port. |
| 112 | */ |
| 113 | |
| 114 | void daemon_usePort(int port) |
| 115 | { |
| 116 | daemon__port = port; |
| 117 | } |
| 118 | |
| 119 | /* --- @daemon_readKey@ --- * |
| 120 | * |
| 121 | * Arguments: @const char *kf@ = name of file containing key |
| 122 | * |
| 123 | * Returns: --- |
| 124 | * |
| 125 | * Use: Instructs the daemon to read the named key file. |
| 126 | */ |
| 127 | |
| 128 | void daemon_readKey(const char *kf) |
| 129 | { |
| 130 | FILE *fp; |
| 131 | |
| 132 | if (!daemon__running) |
| 133 | return; |
| 134 | |
| 135 | if ((fp = fopen(kf, "r")) == 0) { |
| 136 | syslog(LOG_WARNING, "couldn't read key file: %e"); |
| 137 | return; |
| 138 | } |
| 139 | tx_getBits(daemon__key, 128, fp); |
| 140 | fclose(fp); |
| 141 | daemon__readKey = 1; |
| 142 | return; |
| 143 | } |
| 144 | |
| 145 | /* --- @daemon__readConfig@ --- * |
| 146 | * |
| 147 | * Arguments: @const char *cf@ = pointer to configuration file to use |
| 148 | * |
| 149 | * Returns: Zero if it worked, nonzero if it hurt... |
| 150 | * |
| 151 | * Use: Reads the configuration file, and other things. |
| 152 | */ |
| 153 | |
| 154 | static int daemon__readConfig(const char *cf) |
| 155 | { |
| 156 | FILE *fp; |
| 157 | |
| 158 | daemon__readKey = 0; |
| 159 | if ((fp = fopen(cf, "r")) == 0) |
| 160 | return (-1); |
| 161 | lexer_scan(fp); |
| 162 | yyparse(); |
| 163 | fclose(fp); |
| 164 | if (!daemon__readKey) |
| 165 | daemon_readKey(file_KEY); |
| 166 | daemon__rescan = 0; |
| 167 | T( trace(TRACE_DAEMON, "daemon: read config file"); ) |
| 168 | return (0); |
| 169 | } |
| 170 | |
| 171 | /* --- @daemon__restart@ --- * |
| 172 | * |
| 173 | * Arguments: @int sig@ = the signal number |
| 174 | * |
| 175 | * Returns: --- |
| 176 | * |
| 177 | * Use: Handles signals. Causes the configuration file to be reread. |
| 178 | */ |
| 179 | |
| 180 | static void daemon__restart(int sig) |
| 181 | { |
| 182 | daemon__rescan = 1; |
| 183 | signal(sig, daemon__restart); |
| 184 | } |
| 185 | |
| 186 | /* --- @daemon__die@ --- * |
| 187 | * |
| 188 | * Arguments: @int sig@ = the signal number |
| 189 | * |
| 190 | * Returns: via @longjmp@ |
| 191 | * |
| 192 | * Use: Handles other signals. Causes the daemon to die. |
| 193 | */ |
| 194 | |
| 195 | static void daemon__die(int sig) |
| 196 | { |
| 197 | daemon__signum = sig; |
| 198 | longjmp(daemon__dieBuf, 1); |
| 199 | } |
| 200 | |
| 201 | /* --- @daemon__read@ --- * |
| 202 | * |
| 203 | * Arguments: @int fd@ = socket handle |
| 204 | * |
| 205 | * Returns: --- |
| 206 | * |
| 207 | * Use: Examines a buffer, and returns a response. |
| 208 | */ |
| 209 | |
| 210 | void daemon__read(int fd) |
| 211 | { |
| 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 */ |
| 218 | |
| 219 | /* --- Read the message --- */ |
| 220 | |
| 221 | { |
| 222 | int slen = sizeof(sin); |
| 223 | |
| 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", |
| 227 | strerror(errno)); ) |
| 228 | syslog(LOG_INFO, "duff packet received: %e"); |
| 229 | return; |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | /* --- Resolve the host name --- */ |
| 234 | |
| 235 | { |
| 236 | struct hostent *he = gethostbyaddr((char *)&sin.sin_addr, |
| 237 | sizeof(sin.sin_addr), |
| 238 | AF_INET); |
| 239 | sender[0] = 0; |
| 240 | strncat(sender, |
| 241 | he ? he->h_name : inet_ntoa(sin.sin_addr), |
| 242 | sizeof(sender)); |
| 243 | syslog(LOG_DEBUG, "packet received from %s", sender); |
| 244 | T( trace(TRACE_DAEMON, "daemon: received request from %s", sender); ) |
| 245 | } |
| 246 | |
| 247 | /* --- Unpack the block --- */ |
| 248 | |
| 249 | if (crypt_unpackRequest(&rq, buff, daemon__key, sk, rpl) == 0) { |
| 250 | burn(buff); |
| 251 | T( trace(TRACE_DAEMON, "daemon: received corrupt or invalid request"); ) |
| 252 | syslog(LOG_INFO, "packet from %s rejected", sender); |
| 253 | return; |
| 254 | } |
| 255 | burn(buff); |
| 256 | |
| 257 | /* --- Fill in the sender's address in the request block --- */ |
| 258 | |
| 259 | rq.host = sin.sin_addr; |
| 260 | |
| 261 | /* --- Build a reply block --- */ |
| 262 | |
| 263 | { |
| 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); |
| 268 | burn(sk); |
| 269 | } |
| 270 | |
| 271 | /* --- Send the reply off --- */ |
| 272 | |
| 273 | sendto(fd, (char *)rpl, crp_size, 0, (struct sockaddr *)&sin, sizeof(sin)); |
| 274 | T( trace(TRACE_DAEMON, "daemon: reply sent"); ) |
| 275 | burn(rpl); |
| 276 | } |
| 277 | |
| 278 | /* --- @daemon_init@ --- * |
| 279 | * |
| 280 | * Arguments: @const char *cf@ = pointer to name of configuration file |
| 281 | * @int port@ = port to listen to, or %$-1$% for default |
| 282 | * |
| 283 | * Returns: Never. |
| 284 | * |
| 285 | * Use: Starts `become' up in daemon mode. |
| 286 | */ |
| 287 | |
| 288 | void daemon_init(const char *cf, int port) |
| 289 | { |
| 290 | int s; |
| 291 | |
| 292 | /* --- Remove my root privileges --- * |
| 293 | * |
| 294 | * Just in case there's anything dodgy in my configuration file, or the |
| 295 | * user wants me to start on a funny port. |
| 296 | */ |
| 297 | |
| 298 | setuid(getuid()); |
| 299 | |
| 300 | /* --- Initialise bits of the program --- */ |
| 301 | |
| 302 | daemon__running = 1; |
| 303 | daemon__port = port; |
| 304 | userdb_init(); |
| 305 | userdb_local(); |
| 306 | userdb_yp(); |
| 307 | netg_init(); |
| 308 | name_init(); |
| 309 | rule_init(); |
| 310 | openlog(quis(), 0, LOG_DAEMON); |
| 311 | syslog(LOG_NOTICE, "starting up"); |
| 312 | |
| 313 | if (daemon__readConfig(cf)) |
| 314 | die("couldn't read configuration file"); |
| 315 | |
| 316 | /* --- Decide on a port to use --- * |
| 317 | * |
| 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. |
| 320 | */ |
| 321 | |
| 322 | if (daemon__port <= 0) { |
| 323 | struct servent *se = getservbyname(quis(), "udp"); |
| 324 | if (!se) |
| 325 | die("no idea which port to use"); |
| 326 | daemon__port = ntohs(se->s_port); |
| 327 | } |
| 328 | |
| 329 | /* --- Now set up a socket --- */ |
| 330 | |
| 331 | { |
| 332 | struct sockaddr_in sin; |
| 333 | |
| 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)); |
| 341 | } |
| 342 | |
| 343 | /* --- Fork off into the sunset --- */ |
| 344 | |
| 345 | #ifdef NDEBUG |
| 346 | { |
| 347 | int pid = fork(); |
| 348 | FILE *fp; |
| 349 | |
| 350 | /* --- Make a background process --- */ |
| 351 | |
| 352 | if (pid == -1) |
| 353 | die("couldn't fork daemon: %s", strerror(errno)); |
| 354 | else if (pid != 0) |
| 355 | return; |
| 356 | |
| 357 | /* --- Disconnect from the terminal --- */ |
| 358 | |
| 359 | setsid(); |
| 360 | |
| 361 | /* --- Write my process id to a file --- */ |
| 362 | |
| 363 | if ((fp = fopen(file_PID, "w")) != 0) { |
| 364 | fprintf(fp, "%lu\n", (unsigned long)getpid()); |
| 365 | fclose(fp); |
| 366 | } |
| 367 | T( trace(TRACE_DAEMON, "daemon: forked to pid %li", (long)getpid()); ) |
| 368 | } |
| 369 | #endif |
| 370 | |
| 371 | /* --- Program in daemon death mode --- */ |
| 372 | |
| 373 | if (setjmp(daemon__dieBuf)) { |
| 374 | #ifdef TRACING |
| 375 | if (daemon__signum == SIGQUIT && tracing() & TRACE_RULE) { |
| 376 | T( rule_dump(); ) |
| 377 | signal(SIGQUIT, daemon__die); |
| 378 | } else |
| 379 | #endif |
| 380 | { |
| 381 | T( trace(TRACE_DAEMON, "daemon: killed by signal %i", |
| 382 | daemon__signum); ) |
| 383 | syslog(LOG_NOTICE, "killed by signal type %i", daemon__signum); |
| 384 | remove(file_PID); |
| 385 | exit(0); |
| 386 | } |
| 387 | } else { |
| 388 | |
| 389 | /* --- Set signal handlers --- */ |
| 390 | |
| 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); |
| 398 | } |
| 399 | |
| 400 | /* --- Now wait for something exciting to happen --- * |
| 401 | * |
| 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 |
| 404 | * @select@. |
| 405 | */ |
| 406 | |
| 407 | { |
| 408 | time_t when; |
| 409 | |
| 410 | /* --- Find when I am, and thus when I need to be awoken again --- */ |
| 411 | |
| 412 | when = time(0) + daemon__awakeEvery; |
| 413 | |
| 414 | for (;;) { |
| 415 | fd_set fds; |
| 416 | int i; |
| 417 | |
| 418 | /* --- Set up the file descriptor tables --- */ |
| 419 | |
| 420 | FD_ZERO(&fds); |
| 421 | FD_SET(s, &fds); |
| 422 | |
| 423 | /* --- Now wait for something interesting --- */ |
| 424 | |
| 425 | T( trace(TRACE_DAEMON, "daemon: waiting for requests"); ) |
| 426 | i = select(FD_SETSIZE, &fds, 0, 0, 0); |
| 427 | |
| 428 | /* --- Now, see if I need to rescan the config --- */ |
| 429 | |
| 430 | if (daemon__rescan || time(0) - when > 0) { |
| 431 | daemon__rescan = 0; |
| 432 | syslog(LOG_INFO, "rescanning configuration file"); |
| 433 | name_end(); |
| 434 | rule_end(); |
| 435 | netg_end(); |
| 436 | userdb_end(); |
| 437 | userdb_init(); |
| 438 | userdb_local(); |
| 439 | userdb_yp(); |
| 440 | netg_init(); |
| 441 | rule_init(); |
| 442 | name_init(); |
| 443 | if (daemon__readConfig(cf)) |
| 444 | syslog(LOG_ERR, "error reading configuration file"); |
| 445 | when = time(0) + daemon__awakeEvery; |
| 446 | } |
| 447 | |
| 448 | /* --- Read the data from the request --- */ |
| 449 | |
| 450 | if (i > 0) |
| 451 | daemon__read(s); |
| 452 | } |
| 453 | } |
| 454 | } |
| 455 | |
| 456 | /*----- That's all, folks -------------------------------------------------*/ |