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