1 /* $NetBSD: options.c,v 1.3 1997/10/09 21:20:39 christos Exp $ */ 2 3 /* 4 * General skeleton for adding options to the access control language. The 5 * features offered by this module are documented in the hosts_options(5) 6 * manual page (source file: hosts_options.5, "nroff -man" format). 7 * 8 * Notes and warnings for those who want to add features: 9 * 10 * In case of errors, abort options processing and deny access. There are too 11 * many irreversible side effects to make error recovery feasible. For 12 * example, it makes no sense to continue after we have already changed the 13 * userid. 14 * 15 * In case of errors, do not terminate the process: the routines might be 16 * called from a long-running daemon that should run forever. Instead, call 17 * tcpd_jump() which does a non-local goto back into the hosts_access() 18 * routine. 19 * 20 * In case of severe errors, use clean_exit() instead of directly calling 21 * exit(), or the inetd may loop on an UDP request. 22 * 23 * In verification mode (for example, with the "tcpdmatch" command) the 24 * "dry_run" flag is set. In this mode, an option function should just "say" 25 * what it is going to do instead of really doing it. 26 * 27 * Some option functions do not return (for example, the twist option passes 28 * control to another program). In verification mode (dry_run flag is set) 29 * such options should clear the "dry_run" flag to inform the caller of this 30 * course of action. 31 */ 32 33 #include <sys/cdefs.h> 34 #ifndef lint 35 #if 0 36 static char sccsid[] = "@(#) options.c 1.17 96/02/11 17:01:31"; 37 #else 38 __RCSID("$NetBSD: options.c,v 1.3 1997/10/09 21:20:39 christos Exp $"); 39 #endif 40 #endif 41 42 /* System libraries. */ 43 44 #include <sys/types.h> 45 #include <sys/param.h> 46 #include <sys/socket.h> 47 #include <sys/stat.h> 48 #include <netinet/in.h> 49 #include <netdb.h> 50 #include <stdio.h> 51 #include <syslog.h> 52 #include <unistd.h> 53 #include <stdlib.h> 54 #include <pwd.h> 55 #include <grp.h> 56 #include <ctype.h> 57 #include <setjmp.h> 58 #include <string.h> 59 60 #ifndef MAXPATHNAMELEN 61 #define MAXPATHNAMELEN BUFSIZ 62 #endif 63 64 /* Local stuff. */ 65 66 #include "tcpd.h" 67 68 /* Options runtime support. */ 69 70 int dry_run = 0; /* flag set in verification mode */ 71 extern jmp_buf tcpd_buf; /* tcpd_jump() support */ 72 73 /* Options parser support. */ 74 75 static char whitespace_eq[] = "= \t\r\n"; 76 #define whitespace (whitespace_eq + 1) 77 78 static char *get_field /* chew :-delimited field off string */ 79 __P((char *)); 80 static char *chop_string /* strip leading and trailing blanks */ 81 __P((char *)); 82 struct syslog_names; 83 static int severity_map 84 __P((struct syslog_names *, char *)); 85 86 /* List of functions that implement the options. Add yours here. */ 87 88 static void user_option /* execute "user name.group" option */ 89 __P((char *, struct request_info *)); 90 static void group_option /* execute "group name" option */ 91 __P((char *, struct request_info *)); 92 static void umask_option /* execute "umask mask" option */ 93 __P((char *, struct request_info *)); 94 static void linger_option /* execute "linger time" option */ 95 __P((char *, struct request_info *)); 96 static void keepalive_option /* execute "keepalive" option */ 97 __P((char *, struct request_info *)); 98 static void spawn_option /* execute "spawn command" option */ 99 __P((char *, struct request_info *)); 100 static void twist_option /* execute "twist command" option */ 101 __P((char *, struct request_info *)); 102 static void rfc931_option /* execute "rfc931" option */ 103 __P((char *, struct request_info *)); 104 static void setenv_option /* execute "setenv name value" */ 105 __P((char *, struct request_info *)); 106 static void nice_option /* execute "nice" option */ 107 __P((char *, struct request_info *)); 108 static void severity_option /* execute "severity value" */ 109 __P((char *, struct request_info *)); 110 static void allow_option /* execute "allow" option */ 111 __P((char *, struct request_info *)); 112 static void deny_option /* execute "deny" option */ 113 __P((char *, struct request_info *)); 114 static void banners_option /* execute "banners path" option */ 115 __P((char *, struct request_info *)); 116 117 /* Structure of the options table. */ 118 119 struct option { 120 char *name; /* keyword name, case is ignored */ 121 void (*func) /* function that does the real work */ 122 __P((char *, struct request_info *)); 123 int flags; /* see below... */ 124 }; 125 126 #define NEED_ARG (1<<1) /* option requires argument */ 127 #define USE_LAST (1<<2) /* option must be last */ 128 #define OPT_ARG (1<<3) /* option has optional argument */ 129 #define EXPAND_ARG (1<<4) /* do %x expansion on argument */ 130 131 #define need_arg(o) ((o)->flags & NEED_ARG) 132 #define opt_arg(o) ((o)->flags & OPT_ARG) 133 #define permit_arg(o) ((o)->flags & (NEED_ARG | OPT_ARG)) 134 #define use_last(o) ((o)->flags & USE_LAST) 135 #define expand_arg(o) ((o)->flags & EXPAND_ARG) 136 137 /* List of known keywords. Add yours here. */ 138 139 static struct option option_table[] = { 140 { "user", user_option, NEED_ARG }, 141 { "group", group_option, NEED_ARG }, 142 { "umask", umask_option, NEED_ARG }, 143 { "linger", linger_option, NEED_ARG }, 144 { "keepalive", keepalive_option, 0 }, 145 { "spawn", spawn_option, NEED_ARG | EXPAND_ARG }, 146 { "twist", twist_option, NEED_ARG | EXPAND_ARG | USE_LAST }, 147 { "rfc931", rfc931_option, OPT_ARG }, 148 { "setenv", setenv_option, NEED_ARG | EXPAND_ARG }, 149 { "nice", nice_option, OPT_ARG }, 150 { "severity", severity_option, NEED_ARG }, 151 { "allow", allow_option, USE_LAST }, 152 { "deny", deny_option, USE_LAST }, 153 { "banners", banners_option, NEED_ARG }, 154 { NULL, NULL, 0 } 155 }; 156 157 /* process_options - process access control options */ 158 159 void process_options(options, request) 160 char *options; 161 struct request_info *request; 162 { 163 char *key; 164 char *value; 165 char *curr_opt; 166 char *next_opt; 167 struct option *op; 168 char bf[BUFSIZ]; 169 170 for (curr_opt = get_field(options); curr_opt; curr_opt = next_opt) { 171 next_opt = get_field((char *) 0); 172 173 /* 174 * Separate the option into name and value parts. For backwards 175 * compatibility we ignore exactly one '=' between name and value. 176 */ 177 curr_opt = chop_string(curr_opt); 178 if (*(value = curr_opt + strcspn(curr_opt, whitespace_eq))) { 179 if (*value != '=') { 180 *value++ = 0; 181 value += strspn(value, whitespace); 182 } 183 if (*value == '=') { 184 *value++ = 0; 185 value += strspn(value, whitespace); 186 } 187 } 188 if (*value == 0) 189 value = 0; 190 key = curr_opt; 191 192 /* 193 * Disallow missing option names (and empty option fields). 194 */ 195 if (*key == 0) 196 tcpd_jump("missing option name"); 197 198 /* 199 * Lookup the option-specific info and do some common error checks. 200 * Delegate option-specific processing to the specific functions. 201 */ 202 203 for (op = option_table; op->name && STR_NE(op->name, key); op++) 204 /* VOID */ ; 205 if (op->name == 0) 206 tcpd_jump("bad option name: \"%s\"", key); 207 if (!value && need_arg(op)) 208 tcpd_jump("option \"%s\" requires value", key); 209 if (value && !permit_arg(op)) 210 tcpd_jump("option \"%s\" requires no value", key); 211 if (next_opt && use_last(op)) 212 tcpd_jump("option \"%s\" must be at end", key); 213 if (value && expand_arg(op)) 214 value = chop_string(percent_x(bf, sizeof(bf), value, request)); 215 if (hosts_access_verbose) 216 syslog(LOG_DEBUG, "option: %s %s", key, value ? value : ""); 217 (*(op->func)) (value, request); 218 } 219 } 220 221 /* allow_option - grant access */ 222 223 /* ARGSUSED */ 224 225 static void allow_option(value, request) 226 char *value; 227 struct request_info *request; 228 { 229 longjmp(tcpd_buf, AC_PERMIT); 230 } 231 232 /* deny_option - deny access */ 233 234 /* ARGSUSED */ 235 236 static void deny_option(value, request) 237 char *value; 238 struct request_info *request; 239 { 240 longjmp(tcpd_buf, AC_DENY); 241 } 242 243 /* banners_option - expand %<char>, terminate each line with CRLF */ 244 245 static void banners_option(value, request) 246 char *value; 247 struct request_info *request; 248 { 249 char path[MAXPATHNAMELEN]; 250 char ibuf[BUFSIZ]; 251 char obuf[2 * BUFSIZ]; 252 struct stat st; 253 int ch; 254 FILE *fp; 255 256 (void)snprintf(path, sizeof path, "%s/%s", value, eval_daemon(request)); 257 if ((fp = fopen(path, "r")) != 0) { 258 while ((ch = fgetc(fp)) == 0) 259 write(request->fd, "", 1); 260 ungetc(ch, fp); 261 while (fgets(ibuf, sizeof(ibuf) - 2, fp)) { 262 if (split_at(ibuf, '\n')) 263 strcat(ibuf, "\r\n"); /* XXX strcat is safe */ 264 percent_x(obuf, sizeof(obuf), ibuf, request); 265 write(request->fd, obuf, strlen(obuf)); 266 } 267 fclose(fp); 268 } else if (stat(value, &st) < 0) { 269 tcpd_warn("%s: %m", value); 270 } 271 } 272 273 /* group_option - switch group id */ 274 275 /* ARGSUSED */ 276 277 static void group_option(value, request) 278 char *value; 279 struct request_info *request; 280 { 281 struct group *grp; 282 283 if ((grp = getgrnam(value)) == 0) 284 tcpd_jump("unknown group: \"%s\"", value); 285 endgrent(); 286 287 if (dry_run == 0 && setgid(grp->gr_gid)) 288 tcpd_jump("setgid(%s): %m", value); 289 } 290 291 /* user_option - switch user id */ 292 293 /* ARGSUSED */ 294 295 static void user_option(value, request) 296 char *value; 297 struct request_info *request; 298 { 299 struct passwd *pwd; 300 char *group; 301 302 if ((group = split_at(value, '.')) != 0) 303 group_option(group, request); 304 if ((pwd = getpwnam(value)) == 0) 305 tcpd_jump("unknown user: \"%s\"", value); 306 endpwent(); 307 308 if (dry_run == 0 && setuid(pwd->pw_uid)) 309 tcpd_jump("setuid(%s): %m", value); 310 } 311 312 /* umask_option - set file creation mask */ 313 314 /* ARGSUSED */ 315 316 static void umask_option(value, request) 317 char *value; 318 struct request_info *request; 319 { 320 unsigned mask; 321 char junk; 322 323 if (sscanf(value, "%o%c", &mask, &junk) != 1 || (mask & 0777) != mask) 324 tcpd_jump("bad umask value: \"%s\"", value); 325 (void) umask(mask); 326 } 327 328 /* spawn_option - spawn a shell command and wait */ 329 330 /* ARGSUSED */ 331 332 static void spawn_option(value, request) 333 char *value; 334 struct request_info *request; 335 { 336 if (dry_run == 0) 337 shell_cmd(value); 338 } 339 340 /* linger_option - set the socket linger time (Marc Boucher <marc@cam.org>) */ 341 342 /* ARGSUSED */ 343 344 static void linger_option(value, request) 345 char *value; 346 struct request_info *request; 347 { 348 struct linger linger; 349 char junk; 350 351 if (sscanf(value, "%d%c", &linger.l_linger, &junk) != 1 352 || linger.l_linger < 0) 353 tcpd_jump("bad linger value: \"%s\"", value); 354 if (dry_run == 0) { 355 linger.l_onoff = (linger.l_linger != 0); 356 if (setsockopt(request->fd, SOL_SOCKET, SO_LINGER, (char *) &linger, 357 sizeof(linger)) < 0) 358 tcpd_warn("setsockopt SO_LINGER %d: %m", linger.l_linger); 359 } 360 } 361 362 /* keepalive_option - set the socket keepalive option */ 363 364 /* ARGSUSED */ 365 366 static void keepalive_option(value, request) 367 char *value; 368 struct request_info *request; 369 { 370 static int on = 1; 371 372 if (dry_run == 0 && setsockopt(request->fd, SOL_SOCKET, SO_KEEPALIVE, 373 (char *) &on, sizeof(on)) < 0) 374 tcpd_warn("setsockopt SO_KEEPALIVE: %m"); 375 } 376 377 /* nice_option - set nice value */ 378 379 /* ARGSUSED */ 380 381 static void nice_option(value, request) 382 char *value; 383 struct request_info *request; 384 { 385 int niceval = 10; 386 char junk; 387 388 if (value != 0 && sscanf(value, "%d%c", &niceval, &junk) != 1) 389 tcpd_jump("bad nice value: \"%s\"", value); 390 if (dry_run == 0 && nice(niceval) < 0) 391 tcpd_warn("nice(%d): %m", niceval); 392 } 393 394 /* twist_option - replace process by shell command */ 395 396 static void twist_option(value, request) 397 char *value; 398 struct request_info *request; 399 { 400 char *error; 401 402 if (dry_run != 0) { 403 dry_run = 0; 404 } else { 405 if (resident > 0) 406 tcpd_jump("twist option in resident process"); 407 408 syslog(deny_severity, "twist %s to %s", eval_client(request), value); 409 410 /* Before switching to the shell, set up stdin, stdout and stderr. */ 411 412 #define maybe_dup2(from, to) ((from == to) ? to : (close(to), dup(from))) 413 414 if (maybe_dup2(request->fd, 0) != 0 || 415 maybe_dup2(request->fd, 1) != 1 || 416 maybe_dup2(request->fd, 2) != 2) { 417 error = "twist_option: dup: %m"; 418 } else { 419 if (request->fd > 2) 420 close(request->fd); 421 (void) execl("/bin/sh", "sh", "-c", value, (char *) 0); 422 error = "twist_option: /bin/sh: %m"; 423 } 424 425 /* Something went wrong: we MUST terminate the process. */ 426 427 tcpd_warn(error); 428 clean_exit(request); 429 } 430 } 431 432 /* rfc931_option - look up remote user name */ 433 434 static void rfc931_option(value, request) 435 char *value; 436 struct request_info *request; 437 { 438 int timeout; 439 char junk; 440 441 if (value != 0) { 442 if (sscanf(value, "%d%c", &timeout, &junk) != 1 || timeout <= 0) 443 tcpd_jump("bad rfc931 timeout: \"%s\"", value); 444 rfc931_timeout = timeout; 445 } 446 (void) eval_user(request); 447 } 448 449 /* setenv_option - set environment variable */ 450 451 /* ARGSUSED */ 452 453 static void setenv_option(value, request) 454 char *value; 455 struct request_info *request; 456 { 457 char *var_value; 458 459 if (*(var_value = value + strcspn(value, whitespace))) 460 *var_value++ = 0; 461 if (setenv(chop_string(value), chop_string(var_value), 1)) 462 tcpd_jump("memory allocation failure"); 463 } 464 465 /* 466 * The severity option goes last because it comes with a huge amount of ugly 467 * #ifdefs and tables. 468 */ 469 470 struct syslog_names { 471 char *name; 472 int value; 473 }; 474 475 static struct syslog_names log_fac[] = { 476 #ifdef LOG_KERN 477 { "kern", LOG_KERN }, 478 #endif 479 #ifdef LOG_USER 480 { "user", LOG_USER }, 481 #endif 482 #ifdef LOG_MAIL 483 { "mail", LOG_MAIL }, 484 #endif 485 #ifdef LOG_DAEMON 486 { "daemon", LOG_DAEMON }, 487 #endif 488 #ifdef LOG_AUTH 489 { "auth", LOG_AUTH }, 490 #endif 491 #ifdef LOG_LPR 492 { "lpr", LOG_LPR }, 493 #endif 494 #ifdef LOG_NEWS 495 { "news", LOG_NEWS }, 496 #endif 497 #ifdef LOG_UUCP 498 { "uucp", LOG_UUCP }, 499 #endif 500 #ifdef LOG_CRON 501 { "cron", LOG_CRON }, 502 #endif 503 #ifdef LOG_LOCAL0 504 { "local0", LOG_LOCAL0 }, 505 #endif 506 #ifdef LOG_LOCAL1 507 { "local1", LOG_LOCAL1 }, 508 #endif 509 #ifdef LOG_LOCAL2 510 { "local2", LOG_LOCAL2 }, 511 #endif 512 #ifdef LOG_LOCAL3 513 { "local3", LOG_LOCAL3 }, 514 #endif 515 #ifdef LOG_LOCAL4 516 { "local4", LOG_LOCAL4 }, 517 #endif 518 #ifdef LOG_LOCAL5 519 { "local5", LOG_LOCAL5 }, 520 #endif 521 #ifdef LOG_LOCAL6 522 { "local6", LOG_LOCAL6 }, 523 #endif 524 #ifdef LOG_LOCAL7 525 { "local7", LOG_LOCAL7 }, 526 #endif 527 { NULL, 0 } 528 }; 529 530 static struct syslog_names log_sev[] = { 531 #ifdef LOG_EMERG 532 { "emerg", LOG_EMERG }, 533 #endif 534 #ifdef LOG_ALERT 535 { "alert", LOG_ALERT }, 536 #endif 537 #ifdef LOG_CRIT 538 { "crit", LOG_CRIT }, 539 #endif 540 #ifdef LOG_ERR 541 { "err", LOG_ERR }, 542 #endif 543 #ifdef LOG_WARNING 544 { "warning", LOG_WARNING }, 545 #endif 546 #ifdef LOG_NOTICE 547 { "notice", LOG_NOTICE }, 548 #endif 549 #ifdef LOG_INFO 550 { "info", LOG_INFO }, 551 #endif 552 #ifdef LOG_DEBUG 553 { "debug", LOG_DEBUG }, 554 #endif 555 { NULL, 0 } 556 }; 557 558 /* severity_map - lookup facility or severity value */ 559 560 static int severity_map(table, name) 561 struct syslog_names *table; 562 char *name; 563 { 564 struct syslog_names *t; 565 566 for (t = table; t->name; t++) 567 if (STR_EQ(t->name, name)) 568 return (t->value); 569 tcpd_jump("bad syslog facility or severity: \"%s\"", name); 570 /* NOTREACHED */ 571 return -1; 572 } 573 574 /* severity_option - change logging severity for this event (Dave Mitchell) */ 575 576 /* ARGSUSED */ 577 578 static void severity_option(value, request) 579 char *value; 580 struct request_info *request; 581 { 582 char *level = split_at(value, '.'); 583 584 allow_severity = deny_severity = level ? 585 severity_map(log_fac, value) | severity_map(log_sev, level) : 586 severity_map(log_sev, value); 587 } 588 589 /* get_field - return pointer to next field in string */ 590 591 static char *get_field(string) 592 char *string; 593 { 594 static char *last = ""; 595 char *src; 596 char *dst; 597 char *ret; 598 int ch; 599 600 /* 601 * This function returns pointers to successive fields within a given 602 * string. ":" is the field separator; warn if the rule ends in one. It 603 * replaces a "\:" sequence by ":", without treating the result of 604 * substitution as field terminator. A null argument means resume search 605 * where the previous call terminated. This function destroys its 606 * argument. 607 * 608 * Work from explicit source or from memory. While processing \: we 609 * overwrite the input. This way we do not have to maintain buffers for 610 * copies of input fields. 611 */ 612 613 src = dst = ret = (string ? string : last); 614 if (src[0] == 0) 615 return (0); 616 617 while ((ch = *src) != '\0') { 618 if (ch == ':') { 619 if (*++src == 0) 620 tcpd_warn("rule ends in \":\""); 621 break; 622 } 623 if (ch == '\\' && src[1] == ':') 624 src++; 625 *dst++ = *src++; 626 } 627 last = src; 628 *dst = 0; 629 return (ret); 630 } 631 632 /* chop_string - strip leading and trailing blanks from string */ 633 634 static char *chop_string(string) 635 register char *string; 636 { 637 char *start = NULL; 638 char *end = NULL; 639 char *cp; 640 641 for (cp = string; *cp; cp++) { 642 if (!isspace(*cp)) { 643 if (start == 0) 644 start = cp; 645 end = cp; 646 } 647 } 648 return (start ? (end[1] = 0, start) : cp); 649 } 650