1 /* $NetBSD: options.c,v 1.10 2000/10/04 16:24:49 sommerfeld 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.10 2000/10/04 16:24:49 sommerfeld 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 if (dry_run != 0) { 401 dry_run = 0; 402 } else { 403 if (resident > 0) 404 tcpd_jump("twist option in resident process"); 405 406 syslog(deny_severity, "twist %s to %s", eval_client(request), value); 407 408 /* Before switching to the shell, set up stdin, stdout and stderr. */ 409 410 #define maybe_dup2(from, to) ((from == to) ? to : (close(to), dup(from))) 411 412 if (maybe_dup2(request->fd, 0) != 0 || 413 maybe_dup2(request->fd, 1) != 1 || 414 maybe_dup2(request->fd, 2) != 2) { 415 tcpd_warn("twist_option: dup: %m"); 416 } else { 417 if (request->fd > 2) 418 close(request->fd); 419 (void) execl("/bin/sh", "sh", "-c", value, (char *) 0); 420 tcpd_warn("twist_option: /bin/sh: %m"); 421 } 422 423 /* Something went wrong: we MUST terminate the process. */ 424 clean_exit(request); 425 } 426 } 427 428 /* rfc931_option - look up remote user name */ 429 430 static void rfc931_option(value, request) 431 char *value; 432 struct request_info *request; 433 { 434 int timeout; 435 char junk; 436 437 if (value != 0) { 438 if (sscanf(value, "%d%c", &timeout, &junk) != 1 || timeout <= 0) 439 tcpd_jump("bad rfc931 timeout: \"%s\"", value); 440 rfc931_timeout = timeout; 441 } 442 (void) eval_user(request); 443 } 444 445 /* setenv_option - set environment variable */ 446 447 /* ARGSUSED */ 448 449 static void setenv_option(value, request) 450 char *value; 451 struct request_info *request; 452 { 453 char *var_value; 454 455 if (*(var_value = value + strcspn(value, whitespace))) 456 *var_value++ = 0; 457 if (setenv(chop_string(value), chop_string(var_value), 1)) 458 tcpd_jump("memory allocation failure"); 459 } 460 461 /* 462 * The severity option goes last because it comes with a huge amount of ugly 463 * #ifdefs and tables. 464 */ 465 466 struct syslog_names { 467 char *name; 468 int value; 469 }; 470 471 static struct syslog_names log_fac[] = { 472 #ifdef LOG_KERN 473 { "kern", LOG_KERN }, 474 #endif 475 #ifdef LOG_USER 476 { "user", LOG_USER }, 477 #endif 478 #ifdef LOG_MAIL 479 { "mail", LOG_MAIL }, 480 #endif 481 #ifdef LOG_DAEMON 482 { "daemon", LOG_DAEMON }, 483 #endif 484 #ifdef LOG_AUTH 485 { "auth", LOG_AUTH }, 486 #endif 487 #ifdef LOG_LPR 488 { "lpr", LOG_LPR }, 489 #endif 490 #ifdef LOG_NEWS 491 { "news", LOG_NEWS }, 492 #endif 493 #ifdef LOG_UUCP 494 { "uucp", LOG_UUCP }, 495 #endif 496 #ifdef LOG_CRON 497 { "cron", LOG_CRON }, 498 #endif 499 #ifdef LOG_AUTHPRIV 500 { "authpriv", LOG_AUTHPRIV }, 501 #endif 502 #ifdef LOG_FTP 503 { "ftp", LOG_FTP }, 504 #endif 505 #ifdef LOG_LOCAL0 506 { "local0", LOG_LOCAL0 }, 507 #endif 508 #ifdef LOG_LOCAL1 509 { "local1", LOG_LOCAL1 }, 510 #endif 511 #ifdef LOG_LOCAL2 512 { "local2", LOG_LOCAL2 }, 513 #endif 514 #ifdef LOG_LOCAL3 515 { "local3", LOG_LOCAL3 }, 516 #endif 517 #ifdef LOG_LOCAL4 518 { "local4", LOG_LOCAL4 }, 519 #endif 520 #ifdef LOG_LOCAL5 521 { "local5", LOG_LOCAL5 }, 522 #endif 523 #ifdef LOG_LOCAL6 524 { "local6", LOG_LOCAL6 }, 525 #endif 526 #ifdef LOG_LOCAL7 527 { "local7", LOG_LOCAL7 }, 528 #endif 529 { NULL, 0 } 530 }; 531 532 static struct syslog_names log_sev[] = { 533 #ifdef LOG_EMERG 534 { "emerg", LOG_EMERG }, 535 #endif 536 #ifdef LOG_ALERT 537 { "alert", LOG_ALERT }, 538 #endif 539 #ifdef LOG_CRIT 540 { "crit", LOG_CRIT }, 541 #endif 542 #ifdef LOG_ERR 543 { "err", LOG_ERR }, 544 #endif 545 #ifdef LOG_WARNING 546 { "warning", LOG_WARNING }, 547 #endif 548 #ifdef LOG_NOTICE 549 { "notice", LOG_NOTICE }, 550 #endif 551 #ifdef LOG_INFO 552 { "info", LOG_INFO }, 553 #endif 554 #ifdef LOG_DEBUG 555 { "debug", LOG_DEBUG }, 556 #endif 557 { NULL, 0 } 558 }; 559 560 /* severity_map - lookup facility or severity value */ 561 562 static int severity_map(table, name) 563 struct syslog_names *table; 564 char *name; 565 { 566 struct syslog_names *t; 567 568 for (t = table; t->name; t++) 569 if (STR_EQ(t->name, name)) 570 return (t->value); 571 tcpd_jump("bad syslog facility or severity: \"%s\"", name); 572 /* NOTREACHED */ 573 return -1; 574 } 575 576 /* severity_option - change logging severity for this event (Dave Mitchell) */ 577 578 /* ARGSUSED */ 579 580 static void severity_option(value, request) 581 char *value; 582 struct request_info *request; 583 { 584 char *level = split_at(value, '.'); 585 586 allow_severity = deny_severity = level ? 587 severity_map(log_fac, value) | severity_map(log_sev, level) : 588 severity_map(log_sev, value); 589 } 590 591 /* get_field - return pointer to next field in string */ 592 593 static char *get_field(string) 594 char *string; 595 { 596 static char *last = ""; 597 char *src; 598 char *dst; 599 char *ret; 600 int ch; 601 602 /* 603 * This function returns pointers to successive fields within a given 604 * string. ":" is the field separator; warn if the rule ends in one. It 605 * replaces a "\:" sequence by ":", without treating the result of 606 * substitution as field terminator. A null argument means resume search 607 * where the previous call terminated. This function destroys its 608 * argument. 609 * 610 * Work from explicit source or from memory. While processing \: we 611 * overwrite the input. This way we do not have to maintain buffers for 612 * copies of input fields. 613 */ 614 615 src = dst = ret = (string ? string : last); 616 if (src[0] == 0) 617 return (0); 618 619 while ((ch = *src) != '\0') { 620 if (ch == ':') { 621 if (*++src == 0) 622 tcpd_warn("rule ends in \":\""); 623 break; 624 } 625 if (ch == '\\' && src[1] == ':') 626 src++; 627 *dst++ = *src++; 628 } 629 last = src; 630 *dst = 0; 631 return (ret); 632 } 633 634 /* chop_string - strip leading and trailing blanks from string */ 635 636 static char *chop_string(string) 637 register char *string; 638 { 639 char *start = NULL; 640 char *end = NULL; 641 char *cp; 642 643 for (cp = string; *cp; cp++) { 644 if (!isspace((unsigned char) *cp)) { 645 if (start == 0) 646 start = cp; 647 end = cp; 648 } 649 } 650 return (start ? (end[1] = 0, start) : cp); 651 } 652