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