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