inetd.c revision 48991
1/* 2 * Copyright (c) 1983, 1991, 1993, 1994 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34#ifndef lint 35static const char copyright[] = 36"@(#) Copyright (c) 1983, 1991, 1993, 1994\n\ 37 The Regents of the University of California. All rights reserved.\n"; 38#endif /* not lint */ 39 40#ifndef lint 41#if 0 42static char sccsid[] = "@(#)from: inetd.c 8.4 (Berkeley) 4/13/94"; 43#endif 44static const char rcsid[] = 45 "$Id: inetd.c,v 1.67 1999/07/22 16:10:40 sheldonh Exp $"; 46#endif /* not lint */ 47 48/* 49 * Inetd - Internet super-server 50 * 51 * This program invokes all internet services as needed. Connection-oriented 52 * services are invoked each time a connection is made, by creating a process. 53 * This process is passed the connection as file descriptor 0 and is expected 54 * to do a getpeername to find out the source host and port. 55 * 56 * Datagram oriented services are invoked when a datagram 57 * arrives; a process is created and passed a pending message 58 * on file descriptor 0. Datagram servers may either connect 59 * to their peer, freeing up the original socket for inetd 60 * to receive further messages on, or ``take over the socket'', 61 * processing all arriving datagrams and, eventually, timing 62 * out. The first type of server is said to be ``multi-threaded''; 63 * the second type of server ``single-threaded''. 64 * 65 * Inetd uses a configuration file which is read at startup 66 * and, possibly, at some later time in response to a hangup signal. 67 * The configuration file is ``free format'' with fields given in the 68 * order shown below. Continuation lines for an entry must being with 69 * a space or tab. All fields must be present in each entry. 70 * 71 * service name must be in /etc/services or must 72 * name a tcpmux service 73 * socket type stream/dgram/raw/rdm/seqpacket 74 * protocol must be in /etc/protocols 75 * wait/nowait single-threaded/multi-threaded 76 * user user to run daemon as 77 * server program full path name 78 * server program arguments maximum of MAXARGS (20) 79 * 80 * TCP services without official port numbers are handled with the 81 * RFC1078-based tcpmux internal service. Tcpmux listens on port 1 for 82 * requests. When a connection is made from a foreign host, the service 83 * requested is passed to tcpmux, which looks it up in the servtab list 84 * and returns the proper entry for the service. Tcpmux returns a 85 * negative reply if the service doesn't exist, otherwise the invoked 86 * server is expected to return the positive reply if the service type in 87 * inetd.conf file has the prefix "tcpmux/". If the service type has the 88 * prefix "tcpmux/+", tcpmux will return the positive reply for the 89 * process; this is for compatibility with older server code, and also 90 * allows you to invoke programs that use stdin/stdout without putting any 91 * special server code in them. Services that use tcpmux are "nowait" 92 * because they do not have a well-known port and hence cannot listen 93 * for new requests. 94 * 95 * For RPC services 96 * service name/version must be in /etc/rpc 97 * socket type stream/dgram/raw/rdm/seqpacket 98 * protocol must be in /etc/protocols 99 * wait/nowait single-threaded/multi-threaded 100 * user user to run daemon as 101 * server program full path name 102 * server program arguments maximum of MAXARGS 103 * 104 * Comment lines are indicated by a `#' in column 1. 105 */ 106#include <sys/param.h> 107#include <sys/ioctl.h> 108#include <sys/wait.h> 109#include <sys/time.h> 110#include <sys/resource.h> 111 112#include <netinet/in.h> 113#include <netinet/tcp.h> 114#include <arpa/inet.h> 115#include <rpc/rpc.h> 116#include <rpc/pmap_clnt.h> 117 118#include <errno.h> 119#include <err.h> 120#include <fcntl.h> 121#include <grp.h> 122#include <netdb.h> 123#include <pwd.h> 124#include <signal.h> 125#include <stdio.h> 126#include <stdlib.h> 127#include <string.h> 128#include <syslog.h> 129#include <tcpd.h> 130#include <unistd.h> 131#include <libutil.h> 132#include <sysexits.h> 133 134#include "inetd.h" 135#include "pathnames.h" 136 137#ifndef LIBWRAP_ALLOW_FACILITY 138# define LIBWRAP_ALLOW_FACILITY LOG_AUTH 139#endif 140#ifndef LIBWRAP_ALLOW_SEVERITY 141# define LIBWRAP_ALLOW_SEVERITY LOG_INFO 142#endif 143#ifndef LIBWRAP_DENY_FACILITY 144# define LIBWRAP_DENY_FACILITY LOG_AUTH 145#endif 146#ifndef LIBWRAP_DENY_SEVERITY 147# define LIBWRAP_DENY_SEVERITY LOG_WARNING 148#endif 149 150#define ISWRAP(sep) \ 151 ( ((wrap_ex && !(sep)->se_bi) || (wrap_bi && (sep)->se_bi)) \ 152 && ( ((sep)->se_accept && (sep)->se_socktype == SOCK_STREAM) \ 153 || (sep)->se_socktype == SOCK_DGRAM)) 154 155#ifdef LOGIN_CAP 156#include <login_cap.h> 157 158/* see init.c */ 159#define RESOURCE_RC "daemon" 160 161#endif 162 163#ifndef MAXCHILD 164#define MAXCHILD -1 /* maximum number of this service 165 < 0 = no limit */ 166#endif 167 168#ifndef MAXCPM 169#define MAXCPM -1 /* rate limit invocations from a 170 single remote address, 171 < 0 = no limit */ 172#endif 173 174#define TOOMANY 256 /* don't start more than TOOMANY */ 175#define CNT_INTVL 60 /* servers in CNT_INTVL sec. */ 176#define RETRYTIME (60*10) /* retry after bind or server fail */ 177#define MAX_MAXCHLD 32767 /* max allowable max children */ 178 179#define SIGBLOCK (sigmask(SIGCHLD)|sigmask(SIGHUP)|sigmask(SIGALRM)) 180 181int allow_severity; 182int deny_severity; 183int wrap_ex = 0; 184int wrap_bi = 0; 185int debug = 0; 186int log = 0; 187int maxsock; /* highest-numbered descriptor */ 188fd_set allsock; 189int options; 190int timingout; 191int toomany = TOOMANY; 192int maxchild = MAXCHILD; 193int maxcpm = MAXCPM; 194struct servent *sp; 195struct rpcent *rpc; 196struct in_addr bind_address; 197int signalpipe[2]; 198#ifdef SANITY_CHECK 199int nsock; 200#endif 201 202struct servtab *servtab; 203 204extern struct biltin biltins[]; 205 206#define NUMINT (sizeof(intab) / sizeof(struct inent)) 207char *CONFIG = _PATH_INETDCONF; 208char *pid_file = _PATH_INETDPID; 209 210#ifdef OLD_SETPROCTITLE 211char **Argv; 212char *LastArg; 213#endif 214 215int 216getvalue(arg, value, whine) 217 char *arg, *whine; 218 int *value; 219{ 220 int tmp; 221 char *p; 222 223 tmp = strtol(arg, &p, 0); 224 if (tmp < 1 || *p) { 225 syslog(LOG_ERR, whine, arg); 226 return 1; /* failure */ 227 } 228 *value = tmp; 229 return 0; /* success */ 230} 231 232int 233main(argc, argv, envp) 234 int argc; 235 char *argv[], *envp[]; 236{ 237 struct servtab *sep; 238 struct passwd *pwd; 239 struct group *grp; 240 struct sigaction sa, saalrm, sachld, sahup, sapipe; 241 int tmpint, ch, dofork; 242 pid_t pid; 243 char buf[50]; 244#ifdef LOGIN_CAP 245 login_cap_t *lc = NULL; 246#endif 247 struct request_info req; 248 int denied; 249 char *service = NULL; 250 char *pnm; 251 struct sockaddr_in peer; 252 int i; 253 254 255#ifdef OLD_SETPROCTITLE 256 Argv = argv; 257 if (envp == 0 || *envp == 0) 258 envp = argv; 259 while (*envp) 260 envp++; 261 LastArg = envp[-1] + strlen(envp[-1]); 262#endif 263 264 openlog("inetd", LOG_PID | LOG_NOWAIT, LOG_DAEMON); 265 266 bind_address.s_addr = htonl(INADDR_ANY); 267 while ((ch = getopt(argc, argv, "dlwWR:a:c:C:p:")) != -1) 268 switch(ch) { 269 case 'd': 270 debug = 1; 271 options |= SO_DEBUG; 272 break; 273 case 'l': 274 log = 1; 275 break; 276 case 'R': 277 getvalue(optarg, &toomany, 278 "-R %s: bad value for service invocation rate"); 279 break; 280 case 'c': 281 getvalue(optarg, &maxchild, 282 "-c %s: bad value for maximum children"); 283 break; 284 case 'C': 285 getvalue(optarg, &maxcpm, 286 "-C %s: bad value for maximum children/minute"); 287 break; 288 case 'a': 289 if (!inet_aton(optarg, &bind_address)) { 290 syslog(LOG_ERR, 291 "-a %s: invalid IP address", optarg); 292 exit(EX_USAGE); 293 } 294 break; 295 case 'p': 296 pid_file = optarg; 297 break; 298 case 'w': 299 wrap_ex++; 300 break; 301 case 'W': 302 wrap_bi++; 303 break; 304 case '?': 305 default: 306 syslog(LOG_ERR, 307 "usage: inetd [-dlwW] [-a address] [-R rate]" 308 " [-c maximum] [-C rate]" 309 " [-p pidfile] [conf-file]"); 310 exit(EX_USAGE); 311 } 312 argc -= optind; 313 argv += optind; 314 315 if (argc > 0) 316 CONFIG = argv[0]; 317 if (debug == 0) { 318 FILE *fp; 319 if (daemon(0, 0) < 0) { 320 syslog(LOG_WARNING, "daemon(0,0) failed: %m"); 321 } 322 /* 323 * In case somebody has started inetd manually, we need to 324 * clear the logname, so that old servers run as root do not 325 * get the user's logname.. 326 */ 327 if (setlogin("") < 0) { 328 syslog(LOG_WARNING, "cannot clear logname: %m"); 329 /* no big deal if it fails.. */ 330 } 331 pid = getpid(); 332 fp = fopen(pid_file, "w"); 333 if (fp) { 334 fprintf(fp, "%ld\n", (long)pid); 335 fclose(fp); 336 } else { 337 syslog(LOG_WARNING, "%s: %m", pid_file); 338 } 339 } 340 sa.sa_flags = 0; 341 sigemptyset(&sa.sa_mask); 342 sigaddset(&sa.sa_mask, SIGALRM); 343 sigaddset(&sa.sa_mask, SIGCHLD); 344 sigaddset(&sa.sa_mask, SIGHUP); 345 sa.sa_handler = flag_retry; 346 sigaction(SIGALRM, &sa, &saalrm); 347 config(); 348 sa.sa_handler = flag_config; 349 sigaction(SIGHUP, &sa, &sahup); 350 sa.sa_handler = flag_reapchild; 351 sigaction(SIGCHLD, &sa, &sachld); 352 sa.sa_handler = SIG_IGN; 353 sigaction(SIGPIPE, &sa, &sapipe); 354 355 { 356 /* space for daemons to overwrite environment for ps */ 357#define DUMMYSIZE 100 358 char dummy[DUMMYSIZE]; 359 360 (void)memset(dummy, 'x', DUMMYSIZE - 1); 361 dummy[DUMMYSIZE - 1] = '\0'; 362 (void)setenv("inetd_dummy", dummy, 1); 363 } 364 365 if (pipe(signalpipe) != 0) { 366 syslog(LOG_ERR, "pipe: %%m"); 367 exit(EX_OSERR); 368 } 369 FD_SET(signalpipe[0], &allsock); 370#ifdef SANITY_CHECK 371 nsock++; 372#endif 373 if (signalpipe[0] > maxsock) 374 maxsock = signalpipe[0]; 375 if (signalpipe[1] > maxsock) 376 maxsock = signalpipe[1]; 377 378 for (;;) { 379 int n, ctrl; 380 fd_set readable; 381 382#ifdef SANITY_CHECK 383 if (nsock == 0) { 384 syslog(LOG_ERR, "%s: nsock=0", __FUNCTION__); 385 exit(EX_SOFTWARE); 386 } 387#endif 388 readable = allsock; 389 if ((n = select(maxsock + 1, &readable, (fd_set *)0, 390 (fd_set *)0, (struct timeval *)0)) <= 0) { 391 if (n < 0 && errno != EINTR) { 392 syslog(LOG_WARNING, "select: %m"); 393 sleep(1); 394 } 395 continue; 396 } 397 /* handle any queued signal flags */ 398 if (FD_ISSET(signalpipe[0], &readable)) { 399 int n; 400 if (ioctl(signalpipe[0], FIONREAD, &n) != 0) { 401 syslog(LOG_ERR, "ioctl: %m"); 402 exit(EX_OSERR); 403 } 404 while (--n >= 0) { 405 char c; 406 if (read(signalpipe[0], &c, 1) != 1) { 407 syslog(LOG_ERR, "read: %m"); 408 exit(EX_OSERR); 409 } 410 if (debug) 411 warnx("Handling signal flag %c", c); 412 switch(c) { 413 case 'A': /* sigalrm */ 414 retry(); 415 break; 416 case 'C': /* sigchld */ 417 reapchild(); 418 break; 419 case 'H': /* sighup */ 420 config(); 421 break; 422 } 423 } 424 } 425 for (sep = servtab; n && sep; sep = sep->se_next) 426 if (sep->se_fd != -1 && FD_ISSET(sep->se_fd, &readable)) { 427 n--; 428 if (debug) 429 warnx("someone wants %s", sep->se_service); 430 if (sep->se_accept && sep->se_socktype == SOCK_STREAM) { 431 ctrl = accept(sep->se_fd, (struct sockaddr *)0, 432 (int *)0); 433 if (debug) 434 warnx("accept, ctrl %d", ctrl); 435 if (ctrl < 0) { 436 if (errno != EINTR) 437 syslog(LOG_WARNING, 438 "accept (for %s): %m", 439 sep->se_service); 440 if (sep->se_accept && 441 sep->se_socktype == SOCK_STREAM) 442 close(ctrl); 443 continue; 444 } 445 if (cpmip(sep, ctrl) < 0) { 446 close(ctrl); 447 continue; 448 } 449 } else 450 ctrl = sep->se_fd; 451 if (log && !ISWRAP(sep)) { 452 pnm = "unknown"; 453 i = sizeof peer; 454 if (getpeername(ctrl, (struct sockaddr *) 455 &peer, &i)) { 456 i = sizeof peer; 457 if (recvfrom(ctrl, buf, sizeof(buf), 458 MSG_PEEK, 459 (struct sockaddr *)&peer, &i) >= 0) 460 pnm = inet_ntoa(peer.sin_addr); 461 } 462 else 463 pnm = inet_ntoa(peer.sin_addr); 464 syslog(LOG_INFO,"%s from %s", sep->se_service, pnm); 465 } 466 (void) sigblock(SIGBLOCK); 467 pid = 0; 468 /* 469 * Fork for all external services, builtins which need to 470 * fork and anything we're wrapping (as wrapping might 471 * block or use hosts_options(5) twist). 472 */ 473 dofork = !sep->se_bi || sep->se_bi->bi_fork || ISWRAP(sep); 474 if (dofork) { 475 if (sep->se_count++ == 0) 476 (void)gettimeofday(&sep->se_time, (struct timezone *)NULL); 477 else if (sep->se_count >= toomany) { 478 struct timeval now; 479 480 (void)gettimeofday(&now, (struct timezone *)NULL); 481 if (now.tv_sec - sep->se_time.tv_sec > 482 CNT_INTVL) { 483 sep->se_time = now; 484 sep->se_count = 1; 485 } else { 486 syslog(LOG_ERR, 487 "%s/%s server failing (looping), service terminated", 488 sep->se_service, sep->se_proto); 489 close_sep(sep); 490 sigsetmask(0L); 491 if (!timingout) { 492 timingout = 1; 493 alarm(RETRYTIME); 494 } 495 continue; 496 } 497 } 498 pid = fork(); 499 } 500 if (pid < 0) { 501 syslog(LOG_ERR, "fork: %m"); 502 if (sep->se_accept && 503 sep->se_socktype == SOCK_STREAM) 504 close(ctrl); 505 sigsetmask(0L); 506 sleep(1); 507 continue; 508 } 509 if (pid) 510 addchild(sep, pid); 511 sigsetmask(0L); 512 if (pid == 0) { 513 if (dofork) { 514 if (debug) 515 warnx("+ closing from %d", maxsock); 516 for (tmpint = maxsock; tmpint > 2; tmpint--) 517 if (tmpint != ctrl) 518 (void) close(tmpint); 519 sigaction(SIGALRM, &saalrm, (struct sigaction *)0); 520 sigaction(SIGCHLD, &sachld, (struct sigaction *)0); 521 sigaction(SIGHUP, &sahup, (struct sigaction *)0); 522 /* SIGPIPE reset before exec */ 523 } 524 /* 525 * Call tcpmux to find the real service to exec. 526 */ 527 if (sep->se_bi && 528 sep->se_bi->bi_fn == (void (*)()) tcpmux) { 529 sep = tcpmux(ctrl); 530 if (sep == NULL) { 531 close(ctrl); 532 _exit(0); 533 } 534 } 535 if (ISWRAP(sep)) { 536 inetd_setproctitle("wrapping", ctrl); 537 service = sep->se_server_name ? 538 sep->se_server_name : sep->se_service; 539 request_init(&req, RQ_DAEMON, service, RQ_FILE, ctrl, NULL); 540 fromhost(&req); 541 deny_severity = LIBWRAP_DENY_FACILITY|LIBWRAP_DENY_SEVERITY; 542 allow_severity = LIBWRAP_ALLOW_FACILITY|LIBWRAP_ALLOW_SEVERITY; 543 denied = !hosts_access(&req); 544 if (denied) { 545 syslog(deny_severity, 546 "refused connection from %.500s, service %s (%s)", 547 eval_client(&req), service, sep->se_proto); 548 if (sep->se_socktype != SOCK_STREAM) 549 recv(ctrl, buf, sizeof (buf), 0); 550 if (dofork) 551 _exit(0); 552 } 553 if (log) { 554 syslog(allow_severity, 555 "connection from %.500s, service %s (%s)", 556 eval_client(&req), service, sep->se_proto); 557 } 558 } 559 if (sep->se_bi) { 560 (*sep->se_bi->bi_fn)(ctrl, sep); 561 } else { 562 if (debug) 563 warnx("%d execl %s", 564 getpid(), sep->se_server); 565 dup2(ctrl, 0); 566 close(ctrl); 567 dup2(0, 1); 568 dup2(0, 2); 569 if ((pwd = getpwnam(sep->se_user)) == NULL) { 570 syslog(LOG_ERR, 571 "%s/%s: %s: No such user", 572 sep->se_service, sep->se_proto, 573 sep->se_user); 574 if (sep->se_socktype != SOCK_STREAM) 575 recv(0, buf, sizeof (buf), 0); 576 _exit(EX_NOUSER); 577 } 578 grp = NULL; 579 if ( sep->se_group != NULL 580 && (grp = getgrnam(sep->se_group)) == NULL 581 ) { 582 syslog(LOG_ERR, 583 "%s/%s: %s: No such group", 584 sep->se_service, sep->se_proto, 585 sep->se_group); 586 if (sep->se_socktype != SOCK_STREAM) 587 recv(0, buf, sizeof (buf), 0); 588 _exit(EX_NOUSER); 589 } 590 if (grp != NULL) 591 pwd->pw_gid = grp->gr_gid; 592#ifdef LOGIN_CAP 593 if ((lc = login_getclass(sep->se_class)) == NULL) { 594 /* error syslogged by getclass */ 595 syslog(LOG_ERR, 596 "%s/%s: %s: login class error", 597 sep->se_service, sep->se_proto, 598 sep->se_class); 599 if (sep->se_socktype != SOCK_STREAM) 600 recv(0, buf, sizeof (buf), 0); 601 _exit(EX_NOUSER); 602 } 603#endif 604 if (setsid() < 0) { 605 syslog(LOG_ERR, 606 "%s: can't setsid(): %m", 607 sep->se_service); 608 /* _exit(EX_OSERR); not fatal yet */ 609 } 610#ifdef LOGIN_CAP 611 if (setusercontext(lc, pwd, pwd->pw_uid, 612 LOGIN_SETALL) != 0) { 613 syslog(LOG_ERR, 614 "%s: can't setusercontext(..%s..): %m", 615 sep->se_service, sep->se_user); 616 _exit(EX_OSERR); 617 } 618#else 619 if (pwd->pw_uid) { 620 if (setlogin(sep->se_user) < 0) { 621 syslog(LOG_ERR, 622 "%s: can't setlogin(%s): %m", 623 sep->se_service, sep->se_user); 624 /* _exit(EX_OSERR); not yet */ 625 } 626 if (setgid(pwd->pw_gid) < 0) { 627 syslog(LOG_ERR, 628 "%s: can't set gid %d: %m", 629 sep->se_service, pwd->pw_gid); 630 _exit(EX_OSERR); 631 } 632 (void) initgroups(pwd->pw_name, 633 pwd->pw_gid); 634 if (setuid(pwd->pw_uid) < 0) { 635 syslog(LOG_ERR, 636 "%s: can't set uid %d: %m", 637 sep->se_service, pwd->pw_uid); 638 _exit(EX_OSERR); 639 } 640 } 641#endif 642 sigaction(SIGPIPE, &sapipe, 643 (struct sigaction *)0); 644 execv(sep->se_server, sep->se_argv); 645 syslog(LOG_ERR, 646 "cannot execute %s: %m", sep->se_server); 647 if (sep->se_socktype != SOCK_STREAM) 648 recv(0, buf, sizeof (buf), 0); 649 } 650 if (dofork) 651 _exit(0); 652 } 653 if (sep->se_accept && sep->se_socktype == SOCK_STREAM) 654 close(ctrl); 655 } 656 } 657} 658 659/* 660 * Add a signal flag to the signal flag queue for later handling 661 */ 662 663void flag_signal(c) 664 char c; 665{ 666 if (write(signalpipe[1], &c, 1) != 1) { 667 syslog(LOG_ERR, "write: %m"); 668 _exit(EX_OSERR); 669 } 670} 671 672/* 673 * Record a new child pid for this service. If we've reached the 674 * limit on children, then stop accepting incoming requests. 675 */ 676 677void 678addchild(struct servtab *sep, pid_t pid) 679{ 680#ifdef SANITY_CHECK 681 if (sep->se_numchild >= sep->se_maxchild) { 682 syslog(LOG_ERR, "%s: %d >= %d", 683 __FUNCTION__, sep->se_numchild, sep->se_maxchild); 684 exit(EX_SOFTWARE); 685 } 686#endif 687 if (sep->se_maxchild == 0) 688 return; 689 sep->se_pids[sep->se_numchild++] = pid; 690 if (sep->se_numchild == sep->se_maxchild) 691 disable(sep); 692} 693 694/* 695 * Some child process has exited. See if it's on somebody's list. 696 */ 697 698void 699flag_reapchild(signo) 700 int signo; 701{ 702 flag_signal('C'); 703} 704 705void 706reapchild() 707{ 708 int k, status; 709 pid_t pid; 710 struct servtab *sep; 711 712 for (;;) { 713 pid = wait3(&status, WNOHANG, (struct rusage *)0); 714 if (pid <= 0) 715 break; 716 if (debug) 717 warnx("%d reaped, status %#x", pid, status); 718 for (sep = servtab; sep; sep = sep->se_next) { 719 for (k = 0; k < sep->se_numchild; k++) 720 if (sep->se_pids[k] == pid) 721 break; 722 if (k == sep->se_numchild) 723 continue; 724 if (sep->se_numchild == sep->se_maxchild) 725 enable(sep); 726 sep->se_pids[k] = sep->se_pids[--sep->se_numchild]; 727 if (status) 728 syslog(LOG_WARNING, 729 "%s[%d]: exit status 0x%x", 730 sep->se_server, pid, status); 731 break; 732 } 733 } 734} 735 736void 737flag_config(signo) 738 int signo; 739{ 740 flag_signal('H'); 741} 742 743void config() 744{ 745 struct servtab *sep, *new, **sepp; 746 long omask; 747 748 if (!setconfig()) { 749 syslog(LOG_ERR, "%s: %m", CONFIG); 750 return; 751 } 752 for (sep = servtab; sep; sep = sep->se_next) 753 sep->se_checked = 0; 754 while ((new = getconfigent())) { 755 if (getpwnam(new->se_user) == NULL) { 756 syslog(LOG_ERR, 757 "%s/%s: No such user '%s', service ignored", 758 new->se_service, new->se_proto, new->se_user); 759 continue; 760 } 761 if (new->se_group && getgrnam(new->se_group) == NULL) { 762 syslog(LOG_ERR, 763 "%s/%s: No such group '%s', service ignored", 764 new->se_service, new->se_proto, new->se_group); 765 continue; 766 } 767#ifdef LOGIN_CAP 768 if (login_getclass(new->se_class) == NULL) { 769 /* error syslogged by getclass */ 770 syslog(LOG_ERR, 771 "%s/%s: %s: login class error, service ignored", 772 new->se_service, new->se_proto, new->se_class); 773 continue; 774 } 775#endif 776 for (sep = servtab; sep; sep = sep->se_next) 777 if (strcmp(sep->se_service, new->se_service) == 0 && 778 strcmp(sep->se_proto, new->se_proto) == 0) 779 break; 780 if (sep != 0) { 781 int i; 782 783#define SWAP(a, b) { typeof(a) c = a; a = b; b = c; } 784 omask = sigblock(SIGBLOCK); 785 /* copy over outstanding child pids */ 786 if (sep->se_maxchild && new->se_maxchild) { 787 new->se_numchild = sep->se_numchild; 788 if (new->se_numchild > new->se_maxchild) 789 new->se_numchild = new->se_maxchild; 790 memcpy(new->se_pids, sep->se_pids, 791 new->se_numchild * sizeof(*new->se_pids)); 792 } 793 SWAP(sep->se_pids, new->se_pids); 794 sep->se_maxchild = new->se_maxchild; 795 sep->se_numchild = new->se_numchild; 796 sep->se_maxcpm = new->se_maxcpm; 797 /* might need to turn on or off service now */ 798 if (sep->se_fd >= 0) { 799 if (sep->se_maxchild 800 && sep->se_numchild == sep->se_maxchild) { 801 if (FD_ISSET(sep->se_fd, &allsock)) 802 disable(sep); 803 } else { 804 if (!FD_ISSET(sep->se_fd, &allsock)) 805 enable(sep); 806 } 807 } 808 sep->se_accept = new->se_accept; 809 SWAP(sep->se_user, new->se_user); 810 SWAP(sep->se_group, new->se_group); 811#ifdef LOGIN_CAP 812 SWAP(sep->se_class, new->se_class); 813#endif 814 SWAP(sep->se_server, new->se_server); 815 SWAP(sep->se_server_name, new->se_server_name); 816 for (i = 0; i < MAXARGV; i++) 817 SWAP(sep->se_argv[i], new->se_argv[i]); 818 sigsetmask(omask); 819 freeconfig(new); 820 if (debug) 821 print_service("REDO", sep); 822 } else { 823 sep = enter(new); 824 if (debug) 825 print_service("ADD ", sep); 826 } 827 sep->se_checked = 1; 828 if (ISMUX(sep)) { 829 sep->se_fd = -1; 830 continue; 831 } 832 if (!sep->se_rpc) { 833 sp = getservbyname(sep->se_service, sep->se_proto); 834 if (sp == 0) { 835 syslog(LOG_ERR, "%s/%s: unknown service", 836 sep->se_service, sep->se_proto); 837 sep->se_checked = 0; 838 continue; 839 } 840 if (sp->s_port != sep->se_ctrladdr.sin_port) { 841 sep->se_ctrladdr.sin_family = AF_INET; 842 sep->se_ctrladdr.sin_addr = bind_address; 843 sep->se_ctrladdr.sin_port = sp->s_port; 844 if (sep->se_fd >= 0) 845 close_sep(sep); 846 } 847 } else { 848 rpc = getrpcbyname(sep->se_service); 849 if (rpc == 0) { 850 syslog(LOG_ERR, "%s/%s unknown RPC service.", 851 sep->se_service, sep->se_proto); 852 if (sep->se_fd != -1) 853 (void) close(sep->se_fd); 854 sep->se_fd = -1; 855 continue; 856 } 857 if (rpc->r_number != sep->se_rpc_prog) { 858 if (sep->se_rpc_prog) 859 unregisterrpc(sep); 860 sep->se_rpc_prog = rpc->r_number; 861 if (sep->se_fd != -1) 862 (void) close(sep->se_fd); 863 sep->se_fd = -1; 864 } 865 } 866 if (sep->se_fd == -1) 867 setup(sep); 868 } 869 endconfig(); 870 /* 871 * Purge anything not looked at above. 872 */ 873 omask = sigblock(SIGBLOCK); 874 sepp = &servtab; 875 while ((sep = *sepp)) { 876 if (sep->se_checked) { 877 sepp = &sep->se_next; 878 continue; 879 } 880 *sepp = sep->se_next; 881 if (sep->se_fd >= 0) 882 close_sep(sep); 883 if (debug) 884 print_service("FREE", sep); 885 if (sep->se_rpc && sep->se_rpc_prog > 0) 886 unregisterrpc(sep); 887 freeconfig(sep); 888 free((char *)sep); 889 } 890 (void) sigsetmask(omask); 891} 892 893void 894unregisterrpc(sep) 895 struct servtab *sep; 896{ 897 int i; 898 struct servtab *sepp; 899 long omask; 900 901 omask = sigblock(SIGBLOCK); 902 for (sepp = servtab; sepp; sepp = sepp->se_next) { 903 if (sepp == sep) 904 continue; 905 if (sep->se_checked == 0 || 906 !sepp->se_rpc || 907 sep->se_rpc_prog != sepp->se_rpc_prog) 908 continue; 909 return; 910 } 911 if (debug) 912 print_service("UNREG", sep); 913 for (i = sep->se_rpc_lowvers; i <= sep->se_rpc_highvers; i++) 914 pmap_unset(sep->se_rpc_prog, i); 915 if (sep->se_fd != -1) 916 (void) close(sep->se_fd); 917 sep->se_fd = -1; 918 (void) sigsetmask(omask); 919} 920 921void 922flag_retry(signo) 923 int signo; 924{ 925 flag_signal('A'); 926} 927 928void 929retry() 930{ 931 struct servtab *sep; 932 933 timingout = 0; 934 for (sep = servtab; sep; sep = sep->se_next) 935 if (sep->se_fd == -1 && !ISMUX(sep)) 936 setup(sep); 937} 938 939void 940setup(sep) 941 struct servtab *sep; 942{ 943 int on = 1; 944 945 if ((sep->se_fd = socket(AF_INET, sep->se_socktype, 0)) < 0) { 946 if (debug) 947 warn("socket failed on %s/%s", 948 sep->se_service, sep->se_proto); 949 syslog(LOG_ERR, "%s/%s: socket: %m", 950 sep->se_service, sep->se_proto); 951 return; 952 } 953#define turnon(fd, opt) \ 954setsockopt(fd, SOL_SOCKET, opt, (char *)&on, sizeof (on)) 955 if (strcmp(sep->se_proto, "tcp") == 0 && (options & SO_DEBUG) && 956 turnon(sep->se_fd, SO_DEBUG) < 0) 957 syslog(LOG_ERR, "setsockopt (SO_DEBUG): %m"); 958 if (turnon(sep->se_fd, SO_REUSEADDR) < 0) 959 syslog(LOG_ERR, "setsockopt (SO_REUSEADDR): %m"); 960#ifdef SO_PRIVSTATE 961 if (turnon(sep->se_fd, SO_PRIVSTATE) < 0) 962 syslog(LOG_ERR, "setsockopt (SO_PRIVSTATE): %m"); 963#endif 964#undef turnon 965 if (sep->se_type == TTCP_TYPE) 966 if (setsockopt(sep->se_fd, IPPROTO_TCP, TCP_NOPUSH, 967 (char *)&on, sizeof (on)) < 0) 968 syslog(LOG_ERR, "setsockopt (TCP_NOPUSH): %m"); 969 if (bind(sep->se_fd, (struct sockaddr *)&sep->se_ctrladdr, 970 sizeof (sep->se_ctrladdr)) < 0) { 971 if (debug) 972 warn("bind failed on %s/%s", 973 sep->se_service, sep->se_proto); 974 syslog(LOG_ERR, "%s/%s: bind: %m", 975 sep->se_service, sep->se_proto); 976 (void) close(sep->se_fd); 977 sep->se_fd = -1; 978 if (!timingout) { 979 timingout = 1; 980 alarm(RETRYTIME); 981 } 982 return; 983 } 984 if (sep->se_rpc) { 985 int i, len = sizeof(struct sockaddr); 986 987 if (getsockname(sep->se_fd, 988 (struct sockaddr*)&sep->se_ctrladdr, &len) < 0){ 989 syslog(LOG_ERR, "%s/%s: getsockname: %m", 990 sep->se_service, sep->se_proto); 991 (void) close(sep->se_fd); 992 sep->se_fd = -1; 993 return; 994 } 995 if (debug) 996 print_service("REG ", sep); 997 for (i = sep->se_rpc_lowvers; i <= sep->se_rpc_highvers; i++) { 998 pmap_unset(sep->se_rpc_prog, i); 999 pmap_set(sep->se_rpc_prog, i, 1000 (sep->se_socktype == SOCK_DGRAM) 1001 ? IPPROTO_UDP : IPPROTO_TCP, 1002 ntohs(sep->se_ctrladdr.sin_port)); 1003 } 1004 1005 } 1006 if (sep->se_socktype == SOCK_STREAM) 1007 listen(sep->se_fd, 64); 1008 enable(sep); 1009 if (debug) { 1010 warnx("registered %s on %d", 1011 sep->se_server, sep->se_fd); 1012 } 1013} 1014 1015/* 1016 * Finish with a service and its socket. 1017 */ 1018void 1019close_sep(sep) 1020 struct servtab *sep; 1021{ 1022 if (sep->se_fd >= 0) { 1023 if (FD_ISSET(sep->se_fd, &allsock)) 1024 disable(sep); 1025 (void) close(sep->se_fd); 1026 sep->se_fd = -1; 1027 } 1028 sep->se_count = 0; 1029 sep->se_numchild = 0; /* forget about any existing children */ 1030} 1031 1032int 1033matchservent(name1, name2, proto) 1034 char *name1, *name2, *proto; 1035{ 1036 char **alias; 1037 struct servent *se; 1038 1039 if ((se = getservbyname(name1, proto)) != NULL) { 1040 if (strcmp(name2, se->s_name) == 0) 1041 return(1); 1042 for (alias = se->s_aliases; *alias; alias++) 1043 if (strcmp(name2, *alias) == 0) 1044 return(1); 1045 } 1046 return(0); 1047} 1048 1049struct servtab * 1050enter(cp) 1051 struct servtab *cp; 1052{ 1053 struct servtab *sep; 1054 long omask; 1055 1056 sep = (struct servtab *)malloc(sizeof (*sep)); 1057 if (sep == (struct servtab *)0) { 1058 syslog(LOG_ERR, "Out of memory."); 1059 exit(EX_OSERR); 1060 } 1061 *sep = *cp; 1062 sep->se_fd = -1; 1063 omask = sigblock(SIGBLOCK); 1064 sep->se_next = servtab; 1065 servtab = sep; 1066 sigsetmask(omask); 1067 return (sep); 1068} 1069 1070void 1071enable(struct servtab *sep) 1072{ 1073 if (debug) 1074 warnx( 1075 "enabling %s, fd %d", sep->se_service, sep->se_fd); 1076#ifdef SANITY_CHECK 1077 if (sep->se_fd < 0) { 1078 syslog(LOG_ERR, 1079 "%s: %s: bad fd", __FUNCTION__, sep->se_service); 1080 exit(EX_SOFTWARE); 1081 } 1082 if (ISMUX(sep)) { 1083 syslog(LOG_ERR, 1084 "%s: %s: is mux", __FUNCTION__, sep->se_service); 1085 exit(EX_SOFTWARE); 1086 } 1087 if (FD_ISSET(sep->se_fd, &allsock)) { 1088 syslog(LOG_ERR, 1089 "%s: %s: not off", __FUNCTION__, sep->se_service); 1090 exit(EX_SOFTWARE); 1091 } 1092 nsock++; 1093#endif 1094 FD_SET(sep->se_fd, &allsock); 1095 if (sep->se_fd > maxsock) 1096 maxsock = sep->se_fd; 1097} 1098 1099void 1100disable(struct servtab *sep) 1101{ 1102 if (debug) 1103 warnx( 1104 "disabling %s, fd %d", sep->se_service, sep->se_fd); 1105#ifdef SANITY_CHECK 1106 if (sep->se_fd < 0) { 1107 syslog(LOG_ERR, 1108 "%s: %s: bad fd", __FUNCTION__, sep->se_service); 1109 exit(EX_SOFTWARE); 1110 } 1111 if (ISMUX(sep)) { 1112 syslog(LOG_ERR, 1113 "%s: %s: is mux", __FUNCTION__, sep->se_service); 1114 exit(EX_SOFTWARE); 1115 } 1116 if (!FD_ISSET(sep->se_fd, &allsock)) { 1117 syslog(LOG_ERR, 1118 "%s: %s: not on", __FUNCTION__, sep->se_service); 1119 exit(EX_SOFTWARE); 1120 } 1121 if (nsock == 0) { 1122 syslog(LOG_ERR, "%s: nsock=0", __FUNCTION__); 1123 exit(EX_SOFTWARE); 1124 } 1125 nsock--; 1126#endif 1127 FD_CLR(sep->se_fd, &allsock); 1128 if (sep->se_fd == maxsock) 1129 maxsock--; 1130} 1131 1132FILE *fconfig = NULL; 1133struct servtab serv; 1134char line[LINE_MAX]; 1135 1136int 1137setconfig() 1138{ 1139 1140 if (fconfig != NULL) { 1141 fseek(fconfig, 0L, SEEK_SET); 1142 return (1); 1143 } 1144 fconfig = fopen(CONFIG, "r"); 1145 return (fconfig != NULL); 1146} 1147 1148void 1149endconfig() 1150{ 1151 if (fconfig) { 1152 (void) fclose(fconfig); 1153 fconfig = NULL; 1154 } 1155} 1156 1157struct servtab * 1158getconfigent() 1159{ 1160 struct servtab *sep = &serv; 1161 int argc; 1162 char *cp, *arg, *s; 1163 char *versp; 1164 static char TCPMUX_TOKEN[] = "tcpmux/"; 1165#define MUX_LEN (sizeof(TCPMUX_TOKEN)-1) 1166 1167more: 1168 while ((cp = nextline(fconfig)) && (*cp == '#' || *cp == '\0')) 1169 ; 1170 if (cp == NULL) 1171 return ((struct servtab *)0); 1172 /* 1173 * clear the static buffer, since some fields (se_ctrladdr, 1174 * for example) don't get initialized here. 1175 */ 1176 memset((caddr_t)sep, 0, sizeof *sep); 1177 arg = skip(&cp); 1178 if (cp == NULL) { 1179 /* got an empty line containing just blanks/tabs. */ 1180 goto more; 1181 } 1182 if (strncmp(arg, TCPMUX_TOKEN, MUX_LEN) == 0) { 1183 char *c = arg + MUX_LEN; 1184 if (*c == '+') { 1185 sep->se_type = MUXPLUS_TYPE; 1186 c++; 1187 } else 1188 sep->se_type = MUX_TYPE; 1189 sep->se_service = newstr(c); 1190 } else { 1191 sep->se_service = newstr(arg); 1192 sep->se_type = NORM_TYPE; 1193 } 1194 arg = sskip(&cp); 1195 if (strcmp(arg, "stream") == 0) 1196 sep->se_socktype = SOCK_STREAM; 1197 else if (strcmp(arg, "dgram") == 0) 1198 sep->se_socktype = SOCK_DGRAM; 1199 else if (strcmp(arg, "rdm") == 0) 1200 sep->se_socktype = SOCK_RDM; 1201 else if (strcmp(arg, "seqpacket") == 0) 1202 sep->se_socktype = SOCK_SEQPACKET; 1203 else if (strcmp(arg, "raw") == 0) 1204 sep->se_socktype = SOCK_RAW; 1205 else 1206 sep->se_socktype = -1; 1207 1208 arg = sskip(&cp); 1209 if (strcmp(arg, "tcp/ttcp") == 0) { 1210 sep->se_type = TTCP_TYPE; 1211 sep->se_proto = newstr("tcp"); 1212 } else { 1213 sep->se_proto = newstr(arg); 1214 } 1215 if (strncmp(sep->se_proto, "rpc/", 4) == 0) { 1216 memmove(sep->se_proto, sep->se_proto + 4, 1217 strlen(sep->se_proto) + 1 - 4); 1218 sep->se_rpc = 1; 1219 sep->se_rpc_prog = sep->se_rpc_lowvers = 1220 sep->se_rpc_lowvers = 0; 1221 sep->se_ctrladdr.sin_family = AF_INET; 1222 sep->se_ctrladdr.sin_port = 0; 1223 sep->se_ctrladdr.sin_addr = bind_address; 1224 if ((versp = rindex(sep->se_service, '/'))) { 1225 *versp++ = '\0'; 1226 switch (sscanf(versp, "%d-%d", 1227 &sep->se_rpc_lowvers, 1228 &sep->se_rpc_highvers)) { 1229 case 2: 1230 break; 1231 case 1: 1232 sep->se_rpc_highvers = 1233 sep->se_rpc_lowvers; 1234 break; 1235 default: 1236 syslog(LOG_ERR, 1237 "bad RPC version specifier; %s\n", 1238 sep->se_service); 1239 freeconfig(sep); 1240 goto more; 1241 } 1242 } 1243 else { 1244 sep->se_rpc_lowvers = 1245 sep->se_rpc_highvers = 1; 1246 } 1247 } 1248 arg = sskip(&cp); 1249 if (!strncmp(arg, "wait", 4)) 1250 sep->se_accept = 0; 1251 else if (!strncmp(arg, "nowait", 6)) 1252 sep->se_accept = 1; 1253 else { 1254 syslog(LOG_ERR, 1255 "%s: bad wait/nowait for service %s", 1256 CONFIG, sep->se_service); 1257 goto more; 1258 } 1259 sep->se_maxchild = -1; 1260 sep->se_maxcpm = -1; 1261 if ((s = strchr(arg, '/')) != NULL) { 1262 char *eptr; 1263 u_long val; 1264 1265 val = strtoul(s + 1, &eptr, 10); 1266 if (eptr == s + 1 || val > MAX_MAXCHLD) { 1267 syslog(LOG_ERR, 1268 "%s: bad max-child for service %s", 1269 CONFIG, sep->se_service); 1270 goto more; 1271 } 1272 if (debug) 1273 if (!sep->se_accept && val != 1) 1274 warnx("maxchild=%lu for wait service %s" 1275 " not recommended", val, sep->se_service); 1276 sep->se_maxchild = val; 1277 if (*eptr == '/') 1278 sep->se_maxcpm = strtol(eptr + 1, &eptr, 10); 1279 /* 1280 * explicitly do not check for \0 for future expansion / 1281 * backwards compatibility 1282 */ 1283 } 1284 if (ISMUX(sep)) { 1285 /* 1286 * Silently enforce "nowait" mode for TCPMUX services 1287 * since they don't have an assigned port to listen on. 1288 */ 1289 sep->se_accept = 1; 1290 if (strcmp(sep->se_proto, "tcp")) { 1291 syslog(LOG_ERR, 1292 "%s: bad protocol for tcpmux service %s", 1293 CONFIG, sep->se_service); 1294 goto more; 1295 } 1296 if (sep->se_socktype != SOCK_STREAM) { 1297 syslog(LOG_ERR, 1298 "%s: bad socket type for tcpmux service %s", 1299 CONFIG, sep->se_service); 1300 goto more; 1301 } 1302 } 1303 sep->se_user = newstr(sskip(&cp)); 1304#ifdef LOGIN_CAP 1305 if ((s = strrchr(sep->se_user, '/')) != NULL) { 1306 *s = '\0'; 1307 sep->se_class = newstr(s + 1); 1308 } else 1309 sep->se_class = newstr(RESOURCE_RC); 1310#endif 1311 if ((s = strrchr(sep->se_user, ':')) != NULL) { 1312 *s = '\0'; 1313 sep->se_group = newstr(s + 1); 1314 } else 1315 sep->se_group = NULL; 1316 sep->se_server = newstr(sskip(&cp)); 1317 if ((sep->se_server_name = rindex(sep->se_server, '/'))) 1318 sep->se_server_name++; 1319 if (strcmp(sep->se_server, "internal") == 0) { 1320 struct biltin *bi; 1321 1322 for (bi = biltins; bi->bi_service; bi++) 1323 if ((bi->bi_socktype == sep->se_socktype && 1324 strcmp(bi->bi_service, sep->se_service) == 0) || 1325 matchservent(bi->bi_service, sep->se_service, 1326 sep->se_proto)) 1327 break; 1328 if (bi->bi_service == 0) { 1329 syslog(LOG_ERR, "internal service %s unknown", 1330 sep->se_service); 1331 goto more; 1332 } 1333 sep->se_accept = 1; /* force accept mode for built-ins */ 1334 sep->se_bi = bi; 1335 } else 1336 sep->se_bi = NULL; 1337 if (sep->se_maxcpm < 0) 1338 sep->se_maxcpm = maxcpm; 1339 if (sep->se_maxchild < 0) { /* apply default max-children */ 1340 if (sep->se_bi && sep->se_bi->bi_maxchild >= 0) 1341 sep->se_maxchild = sep->se_bi->bi_maxchild; 1342 else if (sep->se_accept) 1343 sep->se_maxchild = maxchild > 0 ? maxchild : 0; 1344 else 1345 sep->se_maxchild = 1; 1346 } 1347 if (sep->se_maxchild) { 1348 sep->se_pids = malloc(sep->se_maxchild * sizeof(*sep->se_pids)); 1349 if (sep->se_pids == NULL) { 1350 syslog(LOG_ERR, "Out of memory."); 1351 exit(EX_OSERR); 1352 } 1353 } 1354 argc = 0; 1355 for (arg = skip(&cp); cp; arg = skip(&cp)) 1356 if (argc < MAXARGV) { 1357 sep->se_argv[argc++] = newstr(arg); 1358 } else { 1359 syslog(LOG_ERR, 1360 "%s: too many arguments for service %s", 1361 CONFIG, sep->se_service); 1362 goto more; 1363 } 1364 while (argc <= MAXARGV) 1365 sep->se_argv[argc++] = NULL; 1366 return (sep); 1367} 1368 1369void 1370freeconfig(cp) 1371 struct servtab *cp; 1372{ 1373 int i; 1374 1375 if (cp->se_service) 1376 free(cp->se_service); 1377 if (cp->se_proto) 1378 free(cp->se_proto); 1379 if (cp->se_user) 1380 free(cp->se_user); 1381 if (cp->se_group) 1382 free(cp->se_group); 1383#ifdef LOGIN_CAP 1384 if (cp->se_class) 1385 free(cp->se_class); 1386#endif 1387 if (cp->se_server) 1388 free(cp->se_server); 1389 if (cp->se_pids) 1390 free(cp->se_pids); 1391 for (i = 0; i < MAXARGV; i++) 1392 if (cp->se_argv[i]) 1393 free(cp->se_argv[i]); 1394} 1395 1396 1397/* 1398 * Safe skip - if skip returns null, log a syntax error in the 1399 * configuration file and exit. 1400 */ 1401char * 1402sskip(cpp) 1403 char **cpp; 1404{ 1405 char *cp; 1406 1407 cp = skip(cpp); 1408 if (cp == NULL) { 1409 syslog(LOG_ERR, "%s: syntax error", CONFIG); 1410 exit(EX_DATAERR); 1411 } 1412 return (cp); 1413} 1414 1415char * 1416skip(cpp) 1417 char **cpp; 1418{ 1419 char *cp = *cpp; 1420 char *start; 1421 char quote = '\0'; 1422 1423again: 1424 while (*cp == ' ' || *cp == '\t') 1425 cp++; 1426 if (*cp == '\0') { 1427 int c; 1428 1429 c = getc(fconfig); 1430 (void) ungetc(c, fconfig); 1431 if (c == ' ' || c == '\t') 1432 if ((cp = nextline(fconfig))) 1433 goto again; 1434 *cpp = (char *)0; 1435 return ((char *)0); 1436 } 1437 if (*cp == '"' || *cp == '\'') 1438 quote = *cp++; 1439 start = cp; 1440 if (quote) 1441 while (*cp && *cp != quote) 1442 cp++; 1443 else 1444 while (*cp && *cp != ' ' && *cp != '\t') 1445 cp++; 1446 if (*cp != '\0') 1447 *cp++ = '\0'; 1448 *cpp = cp; 1449 return (start); 1450} 1451 1452char * 1453nextline(fd) 1454 FILE *fd; 1455{ 1456 char *cp; 1457 1458 if (fgets(line, sizeof (line), fd) == NULL) 1459 return ((char *)0); 1460 cp = strchr(line, '\n'); 1461 if (cp) 1462 *cp = '\0'; 1463 return (line); 1464} 1465 1466char * 1467newstr(cp) 1468 char *cp; 1469{ 1470 if ((cp = strdup(cp ? cp : ""))) 1471 return (cp); 1472 syslog(LOG_ERR, "strdup: %m"); 1473 exit(EX_OSERR); 1474} 1475 1476#ifdef OLD_SETPROCTITLE 1477void 1478inetd_setproctitle(a, s) 1479 char *a; 1480 int s; 1481{ 1482 int size; 1483 char *cp; 1484 struct sockaddr_in sin; 1485 char buf[80]; 1486 1487 cp = Argv[0]; 1488 size = sizeof(sin); 1489 if (getpeername(s, (struct sockaddr *)&sin, &size) == 0) 1490 (void) sprintf(buf, "-%s [%s]", a, inet_ntoa(sin.sin_addr)); 1491 else 1492 (void) sprintf(buf, "-%s", a); 1493 strncpy(cp, buf, LastArg - cp); 1494 cp += strlen(cp); 1495 while (cp < LastArg) 1496 *cp++ = ' '; 1497} 1498#else 1499void 1500inetd_setproctitle(a, s) 1501 char *a; 1502 int s; 1503{ 1504 int size; 1505 struct sockaddr_in sin; 1506 char buf[80]; 1507 1508 size = sizeof(sin); 1509 if (getpeername(s, (struct sockaddr *)&sin, &size) == 0) 1510 (void) sprintf(buf, "%s [%s]", a, inet_ntoa(sin.sin_addr)); 1511 else 1512 (void) sprintf(buf, "%s", a); 1513 setproctitle("%s", buf); 1514} 1515#endif 1516 1517 1518/* 1519 * Internet services provided internally by inetd: 1520 */ 1521 1522int check_loop(sin, sep) 1523 struct sockaddr_in *sin; 1524 struct servtab *sep; 1525{ 1526 struct servtab *se2; 1527 1528 for (se2 = servtab; se2; se2 = se2->se_next) { 1529 if (!se2->se_bi || se2->se_socktype != SOCK_DGRAM) 1530 continue; 1531 1532 if (sin->sin_port == se2->se_ctrladdr.sin_port) { 1533 syslog(LOG_WARNING, 1534 "%s/%s:%s/%s loop request REFUSED from %s", 1535 sep->se_service, sep->se_proto, 1536 se2->se_service, se2->se_proto, 1537 inet_ntoa(sin->sin_addr)); 1538 return 1; 1539 } 1540 } 1541 return 0; 1542} 1543 1544/* 1545 * print_service: 1546 * Dump relevant information to stderr 1547 */ 1548void 1549print_service(action, sep) 1550 char *action; 1551 struct servtab *sep; 1552{ 1553 fprintf(stderr, 1554#ifdef LOGIN_CAP 1555 "%s: %s proto=%s accept=%d max=%d user=%s group=%s class=%s builtin=%p server=%s\n", 1556#else 1557 "%s: %s proto=%s accept=%d max=%d user=%s group=%s builtin=%p server=%s\n", 1558#endif 1559 action, sep->se_service, sep->se_proto, 1560 sep->se_accept, sep->se_maxchild, sep->se_user, sep->se_group, 1561#ifdef LOGIN_CAP 1562 sep->se_class, 1563#endif 1564 (void *) sep->se_bi, sep->se_server); 1565} 1566 1567#define CPMHSIZE 256 1568#define CPMHMASK (CPMHSIZE-1) 1569#define CHTGRAN 10 1570#define CHTSIZE 6 1571 1572typedef struct CTime { 1573 unsigned long ct_Ticks; 1574 int ct_Count; 1575} CTime; 1576 1577typedef struct CHash { 1578 struct in_addr ch_Addr; 1579 time_t ch_LTime; 1580 char *ch_Service; 1581 CTime ch_Times[CHTSIZE]; 1582} CHash; 1583 1584CHash CHashAry[CPMHSIZE]; 1585 1586int 1587cpmip(sep, ctrl) 1588 struct servtab *sep; 1589 int ctrl; 1590{ 1591 struct sockaddr_in rsin; 1592 int rsinLen = sizeof(rsin); 1593 int r = 0; 1594 1595 /* 1596 * If getpeername() fails, just let it through (if logging is 1597 * enabled the condition is caught elsewhere) 1598 */ 1599 1600 if (sep->se_maxcpm > 0 && 1601 getpeername(ctrl, (struct sockaddr *)&rsin, &rsinLen) == 0 ) { 1602 time_t t = time(NULL); 1603 int hv = 0xABC3D20F; 1604 int i; 1605 int cnt = 0; 1606 CHash *chBest = NULL; 1607 unsigned int ticks = t / CHTGRAN; 1608 1609 { 1610 char *p; 1611 int i; 1612 1613 for (i = 0, p = (char *)&rsin.sin_addr; 1614 i < sizeof(rsin.sin_addr); 1615 ++i, ++p) { 1616 hv = (hv << 5) ^ (hv >> 23) ^ *p; 1617 } 1618 hv = (hv ^ (hv >> 16)); 1619 } 1620 for (i = 0; i < 5; ++i) { 1621 CHash *ch = &CHashAry[(hv + i) & CPMHMASK]; 1622 1623 if (rsin.sin_addr.s_addr == ch->ch_Addr.s_addr && 1624 ch->ch_Service && strcmp(sep->se_service, 1625 ch->ch_Service) == 0) { 1626 chBest = ch; 1627 break; 1628 } 1629 if (chBest == NULL || ch->ch_LTime == 0 || 1630 ch->ch_LTime < chBest->ch_LTime) { 1631 chBest = ch; 1632 } 1633 } 1634 if (rsin.sin_addr.s_addr != chBest->ch_Addr.s_addr || 1635 chBest->ch_Service == NULL || 1636 strcmp(sep->se_service, chBest->ch_Service) != 0) { 1637 chBest->ch_Addr = rsin.sin_addr; 1638 if (chBest->ch_Service) 1639 free(chBest->ch_Service); 1640 chBest->ch_Service = strdup(sep->se_service); 1641 bzero(chBest->ch_Times, sizeof(chBest->ch_Times)); 1642 } 1643 chBest->ch_LTime = t; 1644 { 1645 CTime *ct = &chBest->ch_Times[ticks % CHTSIZE]; 1646 if (ct->ct_Ticks != ticks) { 1647 ct->ct_Ticks = ticks; 1648 ct->ct_Count = 0; 1649 } 1650 ++ct->ct_Count; 1651 } 1652 for (i = 0; i < CHTSIZE; ++i) { 1653 CTime *ct = &chBest->ch_Times[i]; 1654 if (ct->ct_Ticks <= ticks && 1655 ct->ct_Ticks >= ticks - CHTSIZE) { 1656 cnt += ct->ct_Count; 1657 } 1658 } 1659 if (cnt * (CHTSIZE * CHTGRAN) / 60 > sep->se_maxcpm) { 1660 r = -1; 1661 syslog(LOG_ERR, 1662 "%s from %s exceeded counts/min (limit %d/min)", 1663 sep->se_service, inet_ntoa(rsin.sin_addr), 1664 sep->se_maxcpm); 1665 } 1666 } 1667 return(r); 1668} 1669