inetd.c revision 331722
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 * 4. Neither the name of the University nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 30#ifndef lint 31static const char copyright[] = 32"@(#) Copyright (c) 1983, 1991, 1993, 1994\n\ 33 The Regents of the University of California. All rights reserved.\n"; 34#endif /* not lint */ 35 36#ifndef lint 37#if 0 38static char sccsid[] = "@(#)from: inetd.c 8.4 (Berkeley) 4/13/94"; 39#endif 40#endif /* not lint */ 41 42#include <sys/cdefs.h> 43__FBSDID("$FreeBSD: stable/11/usr.sbin/inetd/inetd.c 331722 2018-03-29 02:50:57Z eadler $"); 44 45/* 46 * Inetd - Internet super-server 47 * 48 * This program invokes all internet services as needed. Connection-oriented 49 * services are invoked each time a connection is made, by creating a process. 50 * This process is passed the connection as file descriptor 0 and is expected 51 * to do a getpeername to find out the source host and port. 52 * 53 * Datagram oriented services are invoked when a datagram 54 * arrives; a process is created and passed a pending message 55 * on file descriptor 0. Datagram servers may either connect 56 * to their peer, freeing up the original socket for inetd 57 * to receive further messages on, or ``take over the socket'', 58 * processing all arriving datagrams and, eventually, timing 59 * out. The first type of server is said to be ``multi-threaded''; 60 * the second type of server ``single-threaded''. 61 * 62 * Inetd uses a configuration file which is read at startup 63 * and, possibly, at some later time in response to a hangup signal. 64 * The configuration file is ``free format'' with fields given in the 65 * order shown below. Continuation lines for an entry must begin with 66 * a space or tab. All fields must be present in each entry. 67 * 68 * service name must be in /etc/services 69 * or name a tcpmux service 70 * or specify a unix domain socket 71 * socket type stream/dgram/raw/rdm/seqpacket 72 * protocol tcp[4][6], udp[4][6], unix 73 * wait/nowait single-threaded/multi-threaded 74 * user[:group][/login-class] user/group/login-class to run daemon as 75 * server program full path name 76 * server program arguments maximum of MAXARGS (20) 77 * 78 * TCP services without official port numbers are handled with the 79 * RFC1078-based tcpmux internal service. Tcpmux listens on port 1 for 80 * requests. When a connection is made from a foreign host, the service 81 * requested is passed to tcpmux, which looks it up in the servtab list 82 * and returns the proper entry for the service. Tcpmux returns a 83 * negative reply if the service doesn't exist, otherwise the invoked 84 * server is expected to return the positive reply if the service type in 85 * inetd.conf file has the prefix "tcpmux/". If the service type has the 86 * prefix "tcpmux/+", tcpmux will return the positive reply for the 87 * process; this is for compatibility with older server code, and also 88 * allows you to invoke programs that use stdin/stdout without putting any 89 * special server code in them. Services that use tcpmux are "nowait" 90 * because they do not have a well-known port and hence cannot listen 91 * for new requests. 92 * 93 * For RPC services 94 * service name/version must be in /etc/rpc 95 * socket type stream/dgram/raw/rdm/seqpacket 96 * protocol rpc/tcp[4][6], rpc/udp[4][6] 97 * wait/nowait single-threaded/multi-threaded 98 * user[:group][/login-class] user/group/login-class to run daemon as 99 * server program full path name 100 * server program arguments maximum of MAXARGS 101 * 102 * Comment lines are indicated by a `#' in column 1. 103 * 104 * #ifdef IPSEC 105 * Comment lines that start with "#@" denote IPsec policy string, as described 106 * in ipsec_set_policy(3). This will affect all the following items in 107 * inetd.conf(8). To reset the policy, just use "#@" line. By default, 108 * there's no IPsec policy. 109 * #endif 110 */ 111#include <sys/param.h> 112#include <sys/ioctl.h> 113#include <sys/mman.h> 114#include <sys/wait.h> 115#include <sys/time.h> 116#include <sys/resource.h> 117#include <sys/stat.h> 118#include <sys/un.h> 119 120#include <netinet/in.h> 121#include <netinet/tcp.h> 122#include <arpa/inet.h> 123#include <rpc/rpc.h> 124#include <rpc/pmap_clnt.h> 125 126#include <ctype.h> 127#include <errno.h> 128#include <err.h> 129#include <fcntl.h> 130#include <grp.h> 131#include <libutil.h> 132#include <limits.h> 133#include <netdb.h> 134#include <pwd.h> 135#include <signal.h> 136#include <stdio.h> 137#include <stdlib.h> 138#include <string.h> 139#include <sysexits.h> 140#include <syslog.h> 141#ifdef LIBWRAP 142#include <tcpd.h> 143#endif 144#include <unistd.h> 145 146#include "inetd.h" 147#include "pathnames.h" 148 149#ifdef IPSEC 150#include <netipsec/ipsec.h> 151#ifndef IPSEC_POLICY_IPSEC /* no ipsec support on old ipsec */ 152#undef IPSEC 153#endif 154#endif 155 156#ifndef LIBWRAP_ALLOW_FACILITY 157# define LIBWRAP_ALLOW_FACILITY LOG_AUTH 158#endif 159#ifndef LIBWRAP_ALLOW_SEVERITY 160# define LIBWRAP_ALLOW_SEVERITY LOG_INFO 161#endif 162#ifndef LIBWRAP_DENY_FACILITY 163# define LIBWRAP_DENY_FACILITY LOG_AUTH 164#endif 165#ifndef LIBWRAP_DENY_SEVERITY 166# define LIBWRAP_DENY_SEVERITY LOG_WARNING 167#endif 168 169#define ISWRAP(sep) \ 170 ( ((wrap_ex && !(sep)->se_bi) || (wrap_bi && (sep)->se_bi)) \ 171 && (sep->se_family == AF_INET || sep->se_family == AF_INET6) \ 172 && ( ((sep)->se_accept && (sep)->se_socktype == SOCK_STREAM) \ 173 || (sep)->se_socktype == SOCK_DGRAM)) 174 175#ifdef LOGIN_CAP 176#include <login_cap.h> 177 178/* see init.c */ 179#define RESOURCE_RC "daemon" 180 181#endif 182 183#ifndef MAXCHILD 184#define MAXCHILD -1 /* maximum number of this service 185 < 0 = no limit */ 186#endif 187 188#ifndef MAXCPM 189#define MAXCPM -1 /* rate limit invocations from a 190 single remote address, 191 < 0 = no limit */ 192#endif 193 194#ifndef MAXPERIP 195#define MAXPERIP -1 /* maximum number of this service 196 from a single remote address, 197 < 0 = no limit */ 198#endif 199 200#ifndef TOOMANY 201#define TOOMANY 256 /* don't start more than TOOMANY */ 202#endif 203#define CNT_INTVL 60 /* servers in CNT_INTVL sec. */ 204#define RETRYTIME (60*10) /* retry after bind or server fail */ 205#define MAX_MAXCHLD 32767 /* max allowable max children */ 206 207#define SIGBLOCK (sigmask(SIGCHLD)|sigmask(SIGHUP)|sigmask(SIGALRM)) 208 209void close_sep(struct servtab *); 210void flag_signal(int); 211void flag_config(int); 212void config(void); 213int cpmip(const struct servtab *, int); 214void endconfig(void); 215struct servtab *enter(struct servtab *); 216void freeconfig(struct servtab *); 217struct servtab *getconfigent(void); 218int matchservent(const char *, const char *, const char *); 219char *nextline(FILE *); 220void addchild(struct servtab *, int); 221void flag_reapchild(int); 222void reapchild(void); 223void enable(struct servtab *); 224void disable(struct servtab *); 225void flag_retry(int); 226void retry(void); 227int setconfig(void); 228void setup(struct servtab *); 229#ifdef IPSEC 230void ipsecsetup(struct servtab *); 231#endif 232void unregisterrpc(register struct servtab *sep); 233static struct conninfo *search_conn(struct servtab *sep, int ctrl); 234static int room_conn(struct servtab *sep, struct conninfo *conn); 235static void addchild_conn(struct conninfo *conn, pid_t pid); 236static void reapchild_conn(pid_t pid); 237static void free_conn(struct conninfo *conn); 238static void resize_conn(struct servtab *sep, int maxperip); 239static void free_connlist(struct servtab *sep); 240static void free_proc(struct procinfo *); 241static struct procinfo *search_proc(pid_t pid, int add); 242static int hashval(char *p, int len); 243 244int allow_severity; 245int deny_severity; 246int wrap_ex = 0; 247int wrap_bi = 0; 248int debug = 0; 249int dolog = 0; 250int maxsock; /* highest-numbered descriptor */ 251fd_set allsock; 252int options; 253int timingout; 254int toomany = TOOMANY; 255int maxchild = MAXCHILD; 256int maxcpm = MAXCPM; 257int maxperip = MAXPERIP; 258struct servent *sp; 259struct rpcent *rpc; 260char *hostname = NULL; 261struct sockaddr_in *bind_sa4; 262int v4bind_ok = 0; 263#ifdef INET6 264struct sockaddr_in6 *bind_sa6; 265int v6bind_ok = 0; 266#endif 267int signalpipe[2]; 268#ifdef SANITY_CHECK 269int nsock; 270#endif 271uid_t euid; 272gid_t egid; 273mode_t mask; 274 275struct servtab *servtab; 276 277extern struct biltin biltins[]; 278 279const char *CONFIG = _PATH_INETDCONF; 280const char *pid_file = _PATH_INETDPID; 281struct pidfh *pfh = NULL; 282 283struct netconfig *udpconf, *tcpconf, *udp6conf, *tcp6conf; 284 285static LIST_HEAD(, procinfo) proctable[PERIPSIZE]; 286 287int 288getvalue(const char *arg, int *value, const char *whine) 289{ 290 int tmp; 291 char *p; 292 293 tmp = strtol(arg, &p, 0); 294 if (tmp < 0 || *p) { 295 syslog(LOG_ERR, whine, arg); 296 return 1; /* failure */ 297 } 298 *value = tmp; 299 return 0; /* success */ 300} 301 302#ifdef LIBWRAP 303static sa_family_t 304whichaf(struct request_info *req) 305{ 306 struct sockaddr *sa; 307 308 sa = (struct sockaddr *)req->client->sin; 309 if (sa == NULL) 310 return AF_UNSPEC; 311 if (sa->sa_family == AF_INET6 && 312 IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)sa)->sin6_addr)) 313 return AF_INET; 314 return sa->sa_family; 315} 316#endif 317 318int 319main(int argc, char **argv) 320{ 321 struct servtab *sep; 322 struct passwd *pwd; 323 struct group *grp; 324 struct sigaction sa, saalrm, sachld, sahup, sapipe; 325 int ch, dofork; 326 pid_t pid; 327 char buf[50]; 328#ifdef LOGIN_CAP 329 login_cap_t *lc = NULL; 330#endif 331#ifdef LIBWRAP 332 struct request_info req; 333 int denied; 334 char *service = NULL; 335#endif 336 struct sockaddr_storage peer; 337 int i; 338 struct addrinfo hints, *res; 339 const char *servname; 340 int error; 341 struct conninfo *conn; 342 343 openlog("inetd", LOG_PID | LOG_NOWAIT | LOG_PERROR, LOG_DAEMON); 344 345 while ((ch = getopt(argc, argv, "dlwWR:a:c:C:p:s:")) != -1) 346 switch(ch) { 347 case 'd': 348 debug = 1; 349 options |= SO_DEBUG; 350 break; 351 case 'l': 352 dolog = 1; 353 break; 354 case 'R': 355 getvalue(optarg, &toomany, 356 "-R %s: bad value for service invocation rate"); 357 break; 358 case 'c': 359 getvalue(optarg, &maxchild, 360 "-c %s: bad value for maximum children"); 361 break; 362 case 'C': 363 getvalue(optarg, &maxcpm, 364 "-C %s: bad value for maximum children/minute"); 365 break; 366 case 'a': 367 hostname = optarg; 368 break; 369 case 'p': 370 pid_file = optarg; 371 break; 372 case 's': 373 getvalue(optarg, &maxperip, 374 "-s %s: bad value for maximum children per source address"); 375 break; 376 case 'w': 377 wrap_ex++; 378 break; 379 case 'W': 380 wrap_bi++; 381 break; 382 case '?': 383 default: 384 syslog(LOG_ERR, 385 "usage: inetd [-dlwW] [-a address] [-R rate]" 386 " [-c maximum] [-C rate]" 387 " [-p pidfile] [conf-file]"); 388 exit(EX_USAGE); 389 } 390 /* 391 * Initialize Bind Addrs. 392 * When hostname is NULL, wild card bind addrs are obtained from 393 * getaddrinfo(). But getaddrinfo() requires at least one of 394 * hostname or servname is non NULL. 395 * So when hostname is NULL, set dummy value to servname. 396 * Since getaddrinfo() doesn't accept numeric servname, and 397 * we doesn't use ai_socktype of struct addrinfo returned 398 * from getaddrinfo(), we set dummy value to ai_socktype. 399 */ 400 servname = (hostname == NULL) ? "0" /* dummy */ : NULL; 401 402 bzero(&hints, sizeof(struct addrinfo)); 403 hints.ai_flags = AI_PASSIVE; 404 hints.ai_family = AF_UNSPEC; 405 hints.ai_socktype = SOCK_STREAM; /* dummy */ 406 error = getaddrinfo(hostname, servname, &hints, &res); 407 if (error != 0) { 408 syslog(LOG_ERR, "-a %s: %s", hostname, gai_strerror(error)); 409 if (error == EAI_SYSTEM) 410 syslog(LOG_ERR, "%s", strerror(errno)); 411 exit(EX_USAGE); 412 } 413 do { 414 if (res->ai_addr == NULL) { 415 syslog(LOG_ERR, "-a %s: getaddrinfo failed", hostname); 416 exit(EX_USAGE); 417 } 418 switch (res->ai_addr->sa_family) { 419 case AF_INET: 420 if (v4bind_ok) 421 continue; 422 bind_sa4 = (struct sockaddr_in *)res->ai_addr; 423 /* init port num in case servname is dummy */ 424 bind_sa4->sin_port = 0; 425 v4bind_ok = 1; 426 continue; 427#ifdef INET6 428 case AF_INET6: 429 if (v6bind_ok) 430 continue; 431 bind_sa6 = (struct sockaddr_in6 *)res->ai_addr; 432 /* init port num in case servname is dummy */ 433 bind_sa6->sin6_port = 0; 434 v6bind_ok = 1; 435 continue; 436#endif 437 } 438 if (v4bind_ok 439#ifdef INET6 440 && v6bind_ok 441#endif 442 ) 443 break; 444 } while ((res = res->ai_next) != NULL); 445 if (!v4bind_ok 446#ifdef INET6 447 && !v6bind_ok 448#endif 449 ) { 450 syslog(LOG_ERR, "-a %s: unknown address family", hostname); 451 exit(EX_USAGE); 452 } 453 454 euid = geteuid(); 455 egid = getegid(); 456 umask(mask = umask(0777)); 457 458 argc -= optind; 459 argv += optind; 460 461 if (argc > 0) 462 CONFIG = argv[0]; 463 if (access(CONFIG, R_OK) < 0) 464 syslog(LOG_ERR, "Accessing %s: %m, continuing anyway.", CONFIG); 465 if (debug == 0) { 466 pid_t otherpid; 467 468 pfh = pidfile_open(pid_file, 0600, &otherpid); 469 if (pfh == NULL) { 470 if (errno == EEXIST) { 471 syslog(LOG_ERR, "%s already running, pid: %d", 472 getprogname(), otherpid); 473 exit(EX_OSERR); 474 } 475 syslog(LOG_WARNING, "pidfile_open() failed: %m"); 476 } 477 478 if (daemon(0, 0) < 0) { 479 syslog(LOG_WARNING, "daemon(0,0) failed: %m"); 480 } 481 /* From now on we don't want syslog messages going to stderr. */ 482 closelog(); 483 openlog("inetd", LOG_PID | LOG_NOWAIT, LOG_DAEMON); 484 /* 485 * In case somebody has started inetd manually, we need to 486 * clear the logname, so that old servers run as root do not 487 * get the user's logname.. 488 */ 489 if (setlogin("") < 0) { 490 syslog(LOG_WARNING, "cannot clear logname: %m"); 491 /* no big deal if it fails.. */ 492 } 493 if (pfh != NULL && pidfile_write(pfh) == -1) { 494 syslog(LOG_WARNING, "pidfile_write(): %m"); 495 } 496 } 497 498 if (madvise(NULL, 0, MADV_PROTECT) != 0) 499 syslog(LOG_WARNING, "madvise() failed: %s", strerror(errno)); 500 501 for (i = 0; i < PERIPSIZE; ++i) 502 LIST_INIT(&proctable[i]); 503 504 if (v4bind_ok) { 505 udpconf = getnetconfigent("udp"); 506 tcpconf = getnetconfigent("tcp"); 507 if (udpconf == NULL || tcpconf == NULL) { 508 syslog(LOG_ERR, "unknown rpc/udp or rpc/tcp"); 509 exit(EX_USAGE); 510 } 511 } 512#ifdef INET6 513 if (v6bind_ok) { 514 udp6conf = getnetconfigent("udp6"); 515 tcp6conf = getnetconfigent("tcp6"); 516 if (udp6conf == NULL || tcp6conf == NULL) { 517 syslog(LOG_ERR, "unknown rpc/udp6 or rpc/tcp6"); 518 exit(EX_USAGE); 519 } 520 } 521#endif 522 523 sa.sa_flags = 0; 524 sigemptyset(&sa.sa_mask); 525 sigaddset(&sa.sa_mask, SIGALRM); 526 sigaddset(&sa.sa_mask, SIGCHLD); 527 sigaddset(&sa.sa_mask, SIGHUP); 528 sa.sa_handler = flag_retry; 529 sigaction(SIGALRM, &sa, &saalrm); 530 config(); 531 sa.sa_handler = flag_config; 532 sigaction(SIGHUP, &sa, &sahup); 533 sa.sa_handler = flag_reapchild; 534 sigaction(SIGCHLD, &sa, &sachld); 535 sa.sa_handler = SIG_IGN; 536 sigaction(SIGPIPE, &sa, &sapipe); 537 538 { 539 /* space for daemons to overwrite environment for ps */ 540#define DUMMYSIZE 100 541 char dummy[DUMMYSIZE]; 542 543 (void)memset(dummy, 'x', DUMMYSIZE - 1); 544 dummy[DUMMYSIZE - 1] = '\0'; 545 (void)setenv("inetd_dummy", dummy, 1); 546 } 547 548 if (pipe2(signalpipe, O_CLOEXEC) != 0) { 549 syslog(LOG_ERR, "pipe: %m"); 550 exit(EX_OSERR); 551 } 552 FD_SET(signalpipe[0], &allsock); 553#ifdef SANITY_CHECK 554 nsock++; 555#endif 556 if (signalpipe[0] > maxsock) 557 maxsock = signalpipe[0]; 558 if (signalpipe[1] > maxsock) 559 maxsock = signalpipe[1]; 560 561 for (;;) { 562 int n, ctrl; 563 fd_set readable; 564 565#ifdef SANITY_CHECK 566 if (nsock == 0) { 567 syslog(LOG_ERR, "%s: nsock=0", __func__); 568 exit(EX_SOFTWARE); 569 } 570#endif 571 readable = allsock; 572 if ((n = select(maxsock + 1, &readable, (fd_set *)0, 573 (fd_set *)0, (struct timeval *)0)) <= 0) { 574 if (n < 0 && errno != EINTR) { 575 syslog(LOG_WARNING, "select: %m"); 576 sleep(1); 577 } 578 continue; 579 } 580 /* handle any queued signal flags */ 581 if (FD_ISSET(signalpipe[0], &readable)) { 582 int nsig; 583 if (ioctl(signalpipe[0], FIONREAD, &nsig) != 0) { 584 syslog(LOG_ERR, "ioctl: %m"); 585 exit(EX_OSERR); 586 } 587 while (--nsig >= 0) { 588 char c; 589 if (read(signalpipe[0], &c, 1) != 1) { 590 syslog(LOG_ERR, "read: %m"); 591 exit(EX_OSERR); 592 } 593 if (debug) 594 warnx("handling signal flag %c", c); 595 switch(c) { 596 case 'A': /* sigalrm */ 597 retry(); 598 break; 599 case 'C': /* sigchld */ 600 reapchild(); 601 break; 602 case 'H': /* sighup */ 603 config(); 604 break; 605 } 606 } 607 } 608 for (sep = servtab; n && sep; sep = sep->se_next) 609 if (sep->se_fd != -1 && FD_ISSET(sep->se_fd, &readable)) { 610 n--; 611 if (debug) 612 warnx("someone wants %s", sep->se_service); 613 dofork = !sep->se_bi || sep->se_bi->bi_fork || ISWRAP(sep); 614 conn = NULL; 615 if (sep->se_accept && sep->se_socktype == SOCK_STREAM) { 616 i = 1; 617 if (ioctl(sep->se_fd, FIONBIO, &i) < 0) 618 syslog(LOG_ERR, "ioctl (FIONBIO, 1): %m"); 619 ctrl = accept(sep->se_fd, (struct sockaddr *)0, 620 (socklen_t *)0); 621 if (debug) 622 warnx("accept, ctrl %d", ctrl); 623 if (ctrl < 0) { 624 if (errno != EINTR) 625 syslog(LOG_WARNING, 626 "accept (for %s): %m", 627 sep->se_service); 628 if (sep->se_accept && 629 sep->se_socktype == SOCK_STREAM) 630 close(ctrl); 631 continue; 632 } 633 i = 0; 634 if (ioctl(sep->se_fd, FIONBIO, &i) < 0) 635 syslog(LOG_ERR, "ioctl1(FIONBIO, 0): %m"); 636 if (ioctl(ctrl, FIONBIO, &i) < 0) 637 syslog(LOG_ERR, "ioctl2(FIONBIO, 0): %m"); 638 if (cpmip(sep, ctrl) < 0) { 639 close(ctrl); 640 continue; 641 } 642 if (dofork && 643 (conn = search_conn(sep, ctrl)) != NULL && 644 !room_conn(sep, conn)) { 645 close(ctrl); 646 continue; 647 } 648 } else 649 ctrl = sep->se_fd; 650 if (dolog && !ISWRAP(sep)) { 651 char pname[NI_MAXHOST] = "unknown"; 652 socklen_t sl; 653 sl = sizeof(peer); 654 if (getpeername(ctrl, (struct sockaddr *) 655 &peer, &sl)) { 656 sl = sizeof(peer); 657 if (recvfrom(ctrl, buf, sizeof(buf), 658 MSG_PEEK, 659 (struct sockaddr *)&peer, 660 &sl) >= 0) { 661 getnameinfo((struct sockaddr *)&peer, 662 peer.ss_len, 663 pname, sizeof(pname), 664 NULL, 0, NI_NUMERICHOST); 665 } 666 } else { 667 getnameinfo((struct sockaddr *)&peer, 668 peer.ss_len, 669 pname, sizeof(pname), 670 NULL, 0, NI_NUMERICHOST); 671 } 672 syslog(LOG_INFO,"%s from %s", sep->se_service, pname); 673 } 674 (void) sigblock(SIGBLOCK); 675 pid = 0; 676 /* 677 * Fork for all external services, builtins which need to 678 * fork and anything we're wrapping (as wrapping might 679 * block or use hosts_options(5) twist). 680 */ 681 if (dofork) { 682 if (sep->se_count++ == 0) 683 (void)clock_gettime(CLOCK_MONOTONIC_FAST, &sep->se_time); 684 else if (toomany > 0 && sep->se_count >= toomany) { 685 struct timespec now; 686 687 (void)clock_gettime(CLOCK_MONOTONIC_FAST, &now); 688 if (now.tv_sec - sep->se_time.tv_sec > 689 CNT_INTVL) { 690 sep->se_time = now; 691 sep->se_count = 1; 692 } else { 693 syslog(LOG_ERR, 694 "%s/%s server failing (looping), service terminated", 695 sep->se_service, sep->se_proto); 696 if (sep->se_accept && 697 sep->se_socktype == SOCK_STREAM) 698 close(ctrl); 699 close_sep(sep); 700 free_conn(conn); 701 sigsetmask(0L); 702 if (!timingout) { 703 timingout = 1; 704 alarm(RETRYTIME); 705 } 706 continue; 707 } 708 } 709 pid = fork(); 710 } 711 if (pid < 0) { 712 syslog(LOG_ERR, "fork: %m"); 713 if (sep->se_accept && 714 sep->se_socktype == SOCK_STREAM) 715 close(ctrl); 716 free_conn(conn); 717 sigsetmask(0L); 718 sleep(1); 719 continue; 720 } 721 if (pid) { 722 addchild_conn(conn, pid); 723 addchild(sep, pid); 724 } 725 sigsetmask(0L); 726 if (pid == 0) { 727 pidfile_close(pfh); 728 if (dofork) { 729 sigaction(SIGALRM, &saalrm, (struct sigaction *)0); 730 sigaction(SIGCHLD, &sachld, (struct sigaction *)0); 731 sigaction(SIGHUP, &sahup, (struct sigaction *)0); 732 /* SIGPIPE reset before exec */ 733 } 734 /* 735 * Call tcpmux to find the real service to exec. 736 */ 737 if (sep->se_bi && 738 sep->se_bi->bi_fn == (bi_fn_t *) tcpmux) { 739 sep = tcpmux(ctrl); 740 if (sep == NULL) { 741 close(ctrl); 742 _exit(0); 743 } 744 } 745#ifdef LIBWRAP 746 if (ISWRAP(sep)) { 747 inetd_setproctitle("wrapping", ctrl); 748 service = sep->se_server_name ? 749 sep->se_server_name : sep->se_service; 750 request_init(&req, RQ_DAEMON, service, RQ_FILE, ctrl, 0); 751 fromhost(&req); 752 deny_severity = LIBWRAP_DENY_FACILITY|LIBWRAP_DENY_SEVERITY; 753 allow_severity = LIBWRAP_ALLOW_FACILITY|LIBWRAP_ALLOW_SEVERITY; 754 denied = !hosts_access(&req); 755 if (denied) { 756 syslog(deny_severity, 757 "refused connection from %.500s, service %s (%s%s)", 758 eval_client(&req), service, sep->se_proto, 759 (whichaf(&req) == AF_INET6) ? "6" : ""); 760 if (sep->se_socktype != SOCK_STREAM) 761 recv(ctrl, buf, sizeof (buf), 0); 762 if (dofork) { 763 sleep(1); 764 _exit(0); 765 } 766 } 767 if (dolog) { 768 syslog(allow_severity, 769 "connection from %.500s, service %s (%s%s)", 770 eval_client(&req), service, sep->se_proto, 771 (whichaf(&req) == AF_INET6) ? "6" : ""); 772 } 773 } 774#endif 775 if (sep->se_bi) { 776 (*sep->se_bi->bi_fn)(ctrl, sep); 777 } else { 778 if (debug) 779 warnx("%d execl %s", 780 getpid(), sep->se_server); 781 /* Clear close-on-exec. */ 782 if (fcntl(ctrl, F_SETFD, 0) < 0) { 783 syslog(LOG_ERR, 784 "%s/%s: fcntl (F_SETFD, 0): %m", 785 sep->se_service, sep->se_proto); 786 _exit(EX_OSERR); 787 } 788 if (ctrl != 0) { 789 dup2(ctrl, 0); 790 close(ctrl); 791 } 792 dup2(0, 1); 793 dup2(0, 2); 794 if ((pwd = getpwnam(sep->se_user)) == NULL) { 795 syslog(LOG_ERR, 796 "%s/%s: %s: no such user", 797 sep->se_service, sep->se_proto, 798 sep->se_user); 799 if (sep->se_socktype != SOCK_STREAM) 800 recv(0, buf, sizeof (buf), 0); 801 _exit(EX_NOUSER); 802 } 803 grp = NULL; 804 if ( sep->se_group != NULL 805 && (grp = getgrnam(sep->se_group)) == NULL 806 ) { 807 syslog(LOG_ERR, 808 "%s/%s: %s: no such group", 809 sep->se_service, sep->se_proto, 810 sep->se_group); 811 if (sep->se_socktype != SOCK_STREAM) 812 recv(0, buf, sizeof (buf), 0); 813 _exit(EX_NOUSER); 814 } 815 if (grp != NULL) 816 pwd->pw_gid = grp->gr_gid; 817#ifdef LOGIN_CAP 818 if ((lc = login_getclass(sep->se_class)) == NULL) { 819 /* error syslogged by getclass */ 820 syslog(LOG_ERR, 821 "%s/%s: %s: login class error", 822 sep->se_service, sep->se_proto, 823 sep->se_class); 824 if (sep->se_socktype != SOCK_STREAM) 825 recv(0, buf, sizeof (buf), 0); 826 _exit(EX_NOUSER); 827 } 828#endif 829 if (setsid() < 0) { 830 syslog(LOG_ERR, 831 "%s: can't setsid(): %m", 832 sep->se_service); 833 /* _exit(EX_OSERR); not fatal yet */ 834 } 835#ifdef LOGIN_CAP 836 if (setusercontext(lc, pwd, pwd->pw_uid, 837 LOGIN_SETALL & ~LOGIN_SETMAC) 838 != 0) { 839 syslog(LOG_ERR, 840 "%s: can't setusercontext(..%s..): %m", 841 sep->se_service, sep->se_user); 842 _exit(EX_OSERR); 843 } 844 login_close(lc); 845#else 846 if (pwd->pw_uid) { 847 if (setlogin(sep->se_user) < 0) { 848 syslog(LOG_ERR, 849 "%s: can't setlogin(%s): %m", 850 sep->se_service, sep->se_user); 851 /* _exit(EX_OSERR); not yet */ 852 } 853 if (setgid(pwd->pw_gid) < 0) { 854 syslog(LOG_ERR, 855 "%s: can't set gid %d: %m", 856 sep->se_service, pwd->pw_gid); 857 _exit(EX_OSERR); 858 } 859 (void) initgroups(pwd->pw_name, 860 pwd->pw_gid); 861 if (setuid(pwd->pw_uid) < 0) { 862 syslog(LOG_ERR, 863 "%s: can't set uid %d: %m", 864 sep->se_service, pwd->pw_uid); 865 _exit(EX_OSERR); 866 } 867 } 868#endif 869 sigaction(SIGPIPE, &sapipe, 870 (struct sigaction *)0); 871 execv(sep->se_server, sep->se_argv); 872 syslog(LOG_ERR, 873 "cannot execute %s: %m", sep->se_server); 874 if (sep->se_socktype != SOCK_STREAM) 875 recv(0, buf, sizeof (buf), 0); 876 } 877 if (dofork) 878 _exit(0); 879 } 880 if (sep->se_accept && sep->se_socktype == SOCK_STREAM) 881 close(ctrl); 882 } 883 } 884} 885 886/* 887 * Add a signal flag to the signal flag queue for later handling 888 */ 889 890void 891flag_signal(int c) 892{ 893 char ch = c; 894 895 if (write(signalpipe[1], &ch, 1) != 1) { 896 syslog(LOG_ERR, "write: %m"); 897 _exit(EX_OSERR); 898 } 899} 900 901/* 902 * Record a new child pid for this service. If we've reached the 903 * limit on children, then stop accepting incoming requests. 904 */ 905 906void 907addchild(struct servtab *sep, pid_t pid) 908{ 909 if (sep->se_maxchild <= 0) 910 return; 911#ifdef SANITY_CHECK 912 if (sep->se_numchild >= sep->se_maxchild) { 913 syslog(LOG_ERR, "%s: %d >= %d", 914 __func__, sep->se_numchild, sep->se_maxchild); 915 exit(EX_SOFTWARE); 916 } 917#endif 918 sep->se_pids[sep->se_numchild++] = pid; 919 if (sep->se_numchild == sep->se_maxchild) 920 disable(sep); 921} 922 923/* 924 * Some child process has exited. See if it's on somebody's list. 925 */ 926 927void 928flag_reapchild(int signo __unused) 929{ 930 flag_signal('C'); 931} 932 933void 934reapchild(void) 935{ 936 int k, status; 937 pid_t pid; 938 struct servtab *sep; 939 940 for (;;) { 941 pid = wait3(&status, WNOHANG, (struct rusage *)0); 942 if (pid <= 0) 943 break; 944 if (debug) 945 warnx("%d reaped, %s %u", pid, 946 WIFEXITED(status) ? "status" : "signal", 947 WIFEXITED(status) ? WEXITSTATUS(status) 948 : WTERMSIG(status)); 949 for (sep = servtab; sep; sep = sep->se_next) { 950 for (k = 0; k < sep->se_numchild; k++) 951 if (sep->se_pids[k] == pid) 952 break; 953 if (k == sep->se_numchild) 954 continue; 955 if (sep->se_numchild == sep->se_maxchild) 956 enable(sep); 957 sep->se_pids[k] = sep->se_pids[--sep->se_numchild]; 958 if (WIFSIGNALED(status) || WEXITSTATUS(status)) 959 syslog(LOG_WARNING, 960 "%s[%d]: exited, %s %u", 961 sep->se_server, pid, 962 WIFEXITED(status) ? "status" : "signal", 963 WIFEXITED(status) ? WEXITSTATUS(status) 964 : WTERMSIG(status)); 965 break; 966 } 967 reapchild_conn(pid); 968 } 969} 970 971void 972flag_config(int signo __unused) 973{ 974 flag_signal('H'); 975} 976 977void 978config(void) 979{ 980 struct servtab *sep, *new, **sepp; 981 long omask; 982 int new_nomapped; 983#ifdef LOGIN_CAP 984 login_cap_t *lc = NULL; 985#endif 986 987 if (!setconfig()) { 988 syslog(LOG_ERR, "%s: %m", CONFIG); 989 return; 990 } 991 for (sep = servtab; sep; sep = sep->se_next) 992 sep->se_checked = 0; 993 while ((new = getconfigent())) { 994 if (getpwnam(new->se_user) == NULL) { 995 syslog(LOG_ERR, 996 "%s/%s: no such user '%s', service ignored", 997 new->se_service, new->se_proto, new->se_user); 998 continue; 999 } 1000 if (new->se_group && getgrnam(new->se_group) == NULL) { 1001 syslog(LOG_ERR, 1002 "%s/%s: no such group '%s', service ignored", 1003 new->se_service, new->se_proto, new->se_group); 1004 continue; 1005 } 1006#ifdef LOGIN_CAP 1007 if ((lc = login_getclass(new->se_class)) == NULL) { 1008 /* error syslogged by getclass */ 1009 syslog(LOG_ERR, 1010 "%s/%s: %s: login class error, service ignored", 1011 new->se_service, new->se_proto, new->se_class); 1012 continue; 1013 } 1014 login_close(lc); 1015#endif 1016 new_nomapped = new->se_nomapped; 1017 for (sep = servtab; sep; sep = sep->se_next) 1018 if (strcmp(sep->se_service, new->se_service) == 0 && 1019 strcmp(sep->se_proto, new->se_proto) == 0 && 1020 sep->se_rpc == new->se_rpc && 1021 sep->se_socktype == new->se_socktype && 1022 sep->se_family == new->se_family) 1023 break; 1024 if (sep != 0) { 1025 int i; 1026 1027#define SWAP(t,a, b) { t c = a; a = b; b = c; } 1028 omask = sigblock(SIGBLOCK); 1029 if (sep->se_nomapped != new->se_nomapped) { 1030 /* for rpc keep old nommaped till unregister */ 1031 if (!sep->se_rpc) 1032 sep->se_nomapped = new->se_nomapped; 1033 sep->se_reset = 1; 1034 } 1035 /* copy over outstanding child pids */ 1036 if (sep->se_maxchild > 0 && new->se_maxchild > 0) { 1037 new->se_numchild = sep->se_numchild; 1038 if (new->se_numchild > new->se_maxchild) 1039 new->se_numchild = new->se_maxchild; 1040 memcpy(new->se_pids, sep->se_pids, 1041 new->se_numchild * sizeof(*new->se_pids)); 1042 } 1043 SWAP(pid_t *, sep->se_pids, new->se_pids); 1044 sep->se_maxchild = new->se_maxchild; 1045 sep->se_numchild = new->se_numchild; 1046 sep->se_maxcpm = new->se_maxcpm; 1047 resize_conn(sep, new->se_maxperip); 1048 sep->se_maxperip = new->se_maxperip; 1049 sep->se_bi = new->se_bi; 1050 /* might need to turn on or off service now */ 1051 if (sep->se_fd >= 0) { 1052 if (sep->se_maxchild > 0 1053 && sep->se_numchild == sep->se_maxchild) { 1054 if (FD_ISSET(sep->se_fd, &allsock)) 1055 disable(sep); 1056 } else { 1057 if (!FD_ISSET(sep->se_fd, &allsock)) 1058 enable(sep); 1059 } 1060 } 1061 sep->se_accept = new->se_accept; 1062 SWAP(char *, sep->se_user, new->se_user); 1063 SWAP(char *, sep->se_group, new->se_group); 1064#ifdef LOGIN_CAP 1065 SWAP(char *, sep->se_class, new->se_class); 1066#endif 1067 SWAP(char *, sep->se_server, new->se_server); 1068 SWAP(char *, sep->se_server_name, new->se_server_name); 1069 for (i = 0; i < MAXARGV; i++) 1070 SWAP(char *, sep->se_argv[i], new->se_argv[i]); 1071#ifdef IPSEC 1072 SWAP(char *, sep->se_policy, new->se_policy); 1073 ipsecsetup(sep); 1074#endif 1075 sigsetmask(omask); 1076 freeconfig(new); 1077 if (debug) 1078 print_service("REDO", sep); 1079 } else { 1080 sep = enter(new); 1081 if (debug) 1082 print_service("ADD ", sep); 1083 } 1084 sep->se_checked = 1; 1085 if (ISMUX(sep)) { 1086 sep->se_fd = -1; 1087 continue; 1088 } 1089 switch (sep->se_family) { 1090 case AF_INET: 1091 if (!v4bind_ok) { 1092 sep->se_fd = -1; 1093 continue; 1094 } 1095 break; 1096#ifdef INET6 1097 case AF_INET6: 1098 if (!v6bind_ok) { 1099 sep->se_fd = -1; 1100 continue; 1101 } 1102 break; 1103#endif 1104 } 1105 if (!sep->se_rpc) { 1106 if (sep->se_family != AF_UNIX) { 1107 sp = getservbyname(sep->se_service, sep->se_proto); 1108 if (sp == 0) { 1109 syslog(LOG_ERR, "%s/%s: unknown service", 1110 sep->se_service, sep->se_proto); 1111 sep->se_checked = 0; 1112 continue; 1113 } 1114 } 1115 switch (sep->se_family) { 1116 case AF_INET: 1117 if (sp->s_port != sep->se_ctrladdr4.sin_port) { 1118 sep->se_ctrladdr4.sin_port = 1119 sp->s_port; 1120 sep->se_reset = 1; 1121 } 1122 break; 1123#ifdef INET6 1124 case AF_INET6: 1125 if (sp->s_port != 1126 sep->se_ctrladdr6.sin6_port) { 1127 sep->se_ctrladdr6.sin6_port = 1128 sp->s_port; 1129 sep->se_reset = 1; 1130 } 1131 break; 1132#endif 1133 } 1134 if (sep->se_reset != 0 && sep->se_fd >= 0) 1135 close_sep(sep); 1136 } else { 1137 rpc = getrpcbyname(sep->se_service); 1138 if (rpc == 0) { 1139 syslog(LOG_ERR, "%s/%s unknown RPC service", 1140 sep->se_service, sep->se_proto); 1141 if (sep->se_fd != -1) 1142 (void) close(sep->se_fd); 1143 sep->se_fd = -1; 1144 continue; 1145 } 1146 if (sep->se_reset != 0 || 1147 rpc->r_number != sep->se_rpc_prog) { 1148 if (sep->se_rpc_prog) 1149 unregisterrpc(sep); 1150 sep->se_rpc_prog = rpc->r_number; 1151 if (sep->se_fd != -1) 1152 (void) close(sep->se_fd); 1153 sep->se_fd = -1; 1154 } 1155 sep->se_nomapped = new_nomapped; 1156 } 1157 sep->se_reset = 0; 1158 if (sep->se_fd == -1) 1159 setup(sep); 1160 } 1161 endconfig(); 1162 /* 1163 * Purge anything not looked at above. 1164 */ 1165 omask = sigblock(SIGBLOCK); 1166 sepp = &servtab; 1167 while ((sep = *sepp)) { 1168 if (sep->se_checked) { 1169 sepp = &sep->se_next; 1170 continue; 1171 } 1172 *sepp = sep->se_next; 1173 if (sep->se_fd >= 0) 1174 close_sep(sep); 1175 if (debug) 1176 print_service("FREE", sep); 1177 if (sep->se_rpc && sep->se_rpc_prog > 0) 1178 unregisterrpc(sep); 1179 freeconfig(sep); 1180 free(sep); 1181 } 1182 (void) sigsetmask(omask); 1183} 1184 1185void 1186unregisterrpc(struct servtab *sep) 1187{ 1188 u_int i; 1189 struct servtab *sepp; 1190 long omask; 1191 struct netconfig *netid4, *netid6; 1192 1193 omask = sigblock(SIGBLOCK); 1194 netid4 = sep->se_socktype == SOCK_DGRAM ? udpconf : tcpconf; 1195 netid6 = sep->se_socktype == SOCK_DGRAM ? udp6conf : tcp6conf; 1196 if (sep->se_family == AF_INET) 1197 netid6 = NULL; 1198 else if (sep->se_nomapped) 1199 netid4 = NULL; 1200 /* 1201 * Conflict if same prog and protocol - In that case one should look 1202 * to versions, but it is not interesting: having separate servers for 1203 * different versions does not work well. 1204 * Therefore one do not unregister if there is a conflict. 1205 * There is also transport conflict if destroying INET when INET46 1206 * exists, or destroying INET46 when INET exists 1207 */ 1208 for (sepp = servtab; sepp; sepp = sepp->se_next) { 1209 if (sepp == sep) 1210 continue; 1211 if (sepp->se_checked == 0 || 1212 !sepp->se_rpc || 1213 strcmp(sep->se_proto, sepp->se_proto) != 0 || 1214 sep->se_rpc_prog != sepp->se_rpc_prog) 1215 continue; 1216 if (sepp->se_family == AF_INET) 1217 netid4 = NULL; 1218 if (sepp->se_family == AF_INET6) { 1219 netid6 = NULL; 1220 if (!sep->se_nomapped) 1221 netid4 = NULL; 1222 } 1223 if (netid4 == NULL && netid6 == NULL) 1224 return; 1225 } 1226 if (debug) 1227 print_service("UNREG", sep); 1228 for (i = sep->se_rpc_lowvers; i <= sep->se_rpc_highvers; i++) { 1229 if (netid4) 1230 rpcb_unset(sep->se_rpc_prog, i, netid4); 1231 if (netid6) 1232 rpcb_unset(sep->se_rpc_prog, i, netid6); 1233 } 1234 if (sep->se_fd != -1) 1235 (void) close(sep->se_fd); 1236 sep->se_fd = -1; 1237 (void) sigsetmask(omask); 1238} 1239 1240void 1241flag_retry(int signo __unused) 1242{ 1243 flag_signal('A'); 1244} 1245 1246void 1247retry(void) 1248{ 1249 struct servtab *sep; 1250 1251 timingout = 0; 1252 for (sep = servtab; sep; sep = sep->se_next) 1253 if (sep->se_fd == -1 && !ISMUX(sep)) 1254 setup(sep); 1255} 1256 1257void 1258setup(struct servtab *sep) 1259{ 1260 int on = 1; 1261 1262 /* Set all listening sockets to close-on-exec. */ 1263 if ((sep->se_fd = socket(sep->se_family, 1264 sep->se_socktype | SOCK_CLOEXEC, 0)) < 0) { 1265 if (debug) 1266 warn("socket failed on %s/%s", 1267 sep->se_service, sep->se_proto); 1268 syslog(LOG_ERR, "%s/%s: socket: %m", 1269 sep->se_service, sep->se_proto); 1270 return; 1271 } 1272#define turnon(fd, opt) \ 1273setsockopt(fd, SOL_SOCKET, opt, (char *)&on, sizeof (on)) 1274 if (strcmp(sep->se_proto, "tcp") == 0 && (options & SO_DEBUG) && 1275 turnon(sep->se_fd, SO_DEBUG) < 0) 1276 syslog(LOG_ERR, "setsockopt (SO_DEBUG): %m"); 1277 if (turnon(sep->se_fd, SO_REUSEADDR) < 0) 1278 syslog(LOG_ERR, "setsockopt (SO_REUSEADDR): %m"); 1279#ifdef SO_PRIVSTATE 1280 if (turnon(sep->se_fd, SO_PRIVSTATE) < 0) 1281 syslog(LOG_ERR, "setsockopt (SO_PRIVSTATE): %m"); 1282#endif 1283 /* tftpd opens a new connection then needs more infos */ 1284 if ((sep->se_family == AF_INET6) && 1285 (strcmp(sep->se_proto, "udp") == 0) && 1286 (sep->se_accept == 0) && 1287 (setsockopt(sep->se_fd, IPPROTO_IPV6, IPV6_RECVPKTINFO, 1288 (char *)&on, sizeof (on)) < 0)) 1289 syslog(LOG_ERR, "setsockopt (IPV6_RECVPKTINFO): %m"); 1290 if (sep->se_family == AF_INET6) { 1291 int flag = sep->se_nomapped ? 1 : 0; 1292 if (setsockopt(sep->se_fd, IPPROTO_IPV6, IPV6_V6ONLY, 1293 (char *)&flag, sizeof (flag)) < 0) 1294 syslog(LOG_ERR, "setsockopt (IPV6_V6ONLY): %m"); 1295 } 1296#undef turnon 1297#ifdef IPSEC 1298 ipsecsetup(sep); 1299#endif 1300 if (sep->se_family == AF_UNIX) { 1301 (void) unlink(sep->se_ctrladdr_un.sun_path); 1302 umask(0777); /* Make socket with conservative permissions */ 1303 } 1304 if (bind(sep->se_fd, (struct sockaddr *)&sep->se_ctrladdr, 1305 sep->se_ctrladdr_size) < 0) { 1306 if (debug) 1307 warn("bind failed on %s/%s", 1308 sep->se_service, sep->se_proto); 1309 syslog(LOG_ERR, "%s/%s: bind: %m", 1310 sep->se_service, sep->se_proto); 1311 (void) close(sep->se_fd); 1312 sep->se_fd = -1; 1313 if (!timingout) { 1314 timingout = 1; 1315 alarm(RETRYTIME); 1316 } 1317 if (sep->se_family == AF_UNIX) 1318 umask(mask); 1319 return; 1320 } 1321 if (sep->se_family == AF_UNIX) { 1322 /* Ick - fch{own,mod} don't work on Unix domain sockets */ 1323 if (chown(sep->se_service, sep->se_sockuid, sep->se_sockgid) < 0) 1324 syslog(LOG_ERR, "chown socket: %m"); 1325 if (chmod(sep->se_service, sep->se_sockmode) < 0) 1326 syslog(LOG_ERR, "chmod socket: %m"); 1327 umask(mask); 1328 } 1329 if (sep->se_rpc) { 1330 u_int i; 1331 socklen_t len = sep->se_ctrladdr_size; 1332 struct netconfig *netid, *netid2 = NULL; 1333 struct sockaddr_in sock; 1334 struct netbuf nbuf, nbuf2; 1335 1336 if (getsockname(sep->se_fd, 1337 (struct sockaddr*)&sep->se_ctrladdr, &len) < 0){ 1338 syslog(LOG_ERR, "%s/%s: getsockname: %m", 1339 sep->se_service, sep->se_proto); 1340 (void) close(sep->se_fd); 1341 sep->se_fd = -1; 1342 return; 1343 } 1344 nbuf.buf = &sep->se_ctrladdr; 1345 nbuf.len = sep->se_ctrladdr.sa_len; 1346 if (sep->se_family == AF_INET) 1347 netid = sep->se_socktype==SOCK_DGRAM? udpconf:tcpconf; 1348 else { 1349 netid = sep->se_socktype==SOCK_DGRAM? udp6conf:tcp6conf; 1350 if (!sep->se_nomapped) { /* INET and INET6 */ 1351 netid2 = netid==udp6conf? udpconf:tcpconf; 1352 memset(&sock, 0, sizeof sock); /* ADDR_ANY */ 1353 nbuf2.buf = &sock; 1354 nbuf2.len = sock.sin_len = sizeof sock; 1355 sock.sin_family = AF_INET; 1356 sock.sin_port = sep->se_ctrladdr6.sin6_port; 1357 } 1358 } 1359 if (debug) 1360 print_service("REG ", sep); 1361 for (i = sep->se_rpc_lowvers; i <= sep->se_rpc_highvers; i++) { 1362 rpcb_unset(sep->se_rpc_prog, i, netid); 1363 rpcb_set(sep->se_rpc_prog, i, netid, &nbuf); 1364 if (netid2) { 1365 rpcb_unset(sep->se_rpc_prog, i, netid2); 1366 rpcb_set(sep->se_rpc_prog, i, netid2, &nbuf2); 1367 } 1368 } 1369 } 1370 if (sep->se_socktype == SOCK_STREAM) 1371 listen(sep->se_fd, -1); 1372 enable(sep); 1373 if (debug) { 1374 warnx("registered %s on %d", 1375 sep->se_server, sep->se_fd); 1376 } 1377} 1378 1379#ifdef IPSEC 1380void 1381ipsecsetup(struct servtab *sep) 1382{ 1383 char *buf; 1384 char *policy_in = NULL; 1385 char *policy_out = NULL; 1386 int level; 1387 int opt; 1388 1389 switch (sep->se_family) { 1390 case AF_INET: 1391 level = IPPROTO_IP; 1392 opt = IP_IPSEC_POLICY; 1393 break; 1394#ifdef INET6 1395 case AF_INET6: 1396 level = IPPROTO_IPV6; 1397 opt = IPV6_IPSEC_POLICY; 1398 break; 1399#endif 1400 default: 1401 return; 1402 } 1403 1404 if (!sep->se_policy || sep->se_policy[0] == '\0') { 1405 static char def_in[] = "in entrust", def_out[] = "out entrust"; 1406 policy_in = def_in; 1407 policy_out = def_out; 1408 } else { 1409 if (!strncmp("in", sep->se_policy, 2)) 1410 policy_in = sep->se_policy; 1411 else if (!strncmp("out", sep->se_policy, 3)) 1412 policy_out = sep->se_policy; 1413 else { 1414 syslog(LOG_ERR, "invalid security policy \"%s\"", 1415 sep->se_policy); 1416 return; 1417 } 1418 } 1419 1420 if (policy_in != NULL) { 1421 buf = ipsec_set_policy(policy_in, strlen(policy_in)); 1422 if (buf != NULL) { 1423 if (setsockopt(sep->se_fd, level, opt, 1424 buf, ipsec_get_policylen(buf)) < 0 && 1425 debug != 0) 1426 warnx("%s/%s: ipsec initialization failed; %s", 1427 sep->se_service, sep->se_proto, 1428 policy_in); 1429 free(buf); 1430 } else 1431 syslog(LOG_ERR, "invalid security policy \"%s\"", 1432 policy_in); 1433 } 1434 if (policy_out != NULL) { 1435 buf = ipsec_set_policy(policy_out, strlen(policy_out)); 1436 if (buf != NULL) { 1437 if (setsockopt(sep->se_fd, level, opt, 1438 buf, ipsec_get_policylen(buf)) < 0 && 1439 debug != 0) 1440 warnx("%s/%s: ipsec initialization failed; %s", 1441 sep->se_service, sep->se_proto, 1442 policy_out); 1443 free(buf); 1444 } else 1445 syslog(LOG_ERR, "invalid security policy \"%s\"", 1446 policy_out); 1447 } 1448} 1449#endif 1450 1451/* 1452 * Finish with a service and its socket. 1453 */ 1454void 1455close_sep(struct servtab *sep) 1456{ 1457 if (sep->se_fd >= 0) { 1458 if (FD_ISSET(sep->se_fd, &allsock)) 1459 disable(sep); 1460 (void) close(sep->se_fd); 1461 sep->se_fd = -1; 1462 } 1463 sep->se_count = 0; 1464 sep->se_numchild = 0; /* forget about any existing children */ 1465} 1466 1467int 1468matchservent(const char *name1, const char *name2, const char *proto) 1469{ 1470 char **alias, *p; 1471 struct servent *se; 1472 1473 if (strcmp(proto, "unix") == 0) { 1474 if ((p = strrchr(name1, '/')) != NULL) 1475 name1 = p + 1; 1476 if ((p = strrchr(name2, '/')) != NULL) 1477 name2 = p + 1; 1478 } 1479 if (strcmp(name1, name2) == 0) 1480 return(1); 1481 if ((se = getservbyname(name1, proto)) != NULL) { 1482 if (strcmp(name2, se->s_name) == 0) 1483 return(1); 1484 for (alias = se->s_aliases; *alias; alias++) 1485 if (strcmp(name2, *alias) == 0) 1486 return(1); 1487 } 1488 return(0); 1489} 1490 1491struct servtab * 1492enter(struct servtab *cp) 1493{ 1494 struct servtab *sep; 1495 long omask; 1496 1497 sep = (struct servtab *)malloc(sizeof (*sep)); 1498 if (sep == (struct servtab *)0) { 1499 syslog(LOG_ERR, "malloc: %m"); 1500 exit(EX_OSERR); 1501 } 1502 *sep = *cp; 1503 sep->se_fd = -1; 1504 omask = sigblock(SIGBLOCK); 1505 sep->se_next = servtab; 1506 servtab = sep; 1507 sigsetmask(omask); 1508 return (sep); 1509} 1510 1511void 1512enable(struct servtab *sep) 1513{ 1514 if (debug) 1515 warnx( 1516 "enabling %s, fd %d", sep->se_service, sep->se_fd); 1517#ifdef SANITY_CHECK 1518 if (sep->se_fd < 0) { 1519 syslog(LOG_ERR, 1520 "%s: %s: bad fd", __func__, sep->se_service); 1521 exit(EX_SOFTWARE); 1522 } 1523 if (ISMUX(sep)) { 1524 syslog(LOG_ERR, 1525 "%s: %s: is mux", __func__, sep->se_service); 1526 exit(EX_SOFTWARE); 1527 } 1528 if (FD_ISSET(sep->se_fd, &allsock)) { 1529 syslog(LOG_ERR, 1530 "%s: %s: not off", __func__, sep->se_service); 1531 exit(EX_SOFTWARE); 1532 } 1533 nsock++; 1534#endif 1535 FD_SET(sep->se_fd, &allsock); 1536 if (sep->se_fd > maxsock) 1537 maxsock = sep->se_fd; 1538} 1539 1540void 1541disable(struct servtab *sep) 1542{ 1543 if (debug) 1544 warnx( 1545 "disabling %s, fd %d", sep->se_service, sep->se_fd); 1546#ifdef SANITY_CHECK 1547 if (sep->se_fd < 0) { 1548 syslog(LOG_ERR, 1549 "%s: %s: bad fd", __func__, sep->se_service); 1550 exit(EX_SOFTWARE); 1551 } 1552 if (ISMUX(sep)) { 1553 syslog(LOG_ERR, 1554 "%s: %s: is mux", __func__, sep->se_service); 1555 exit(EX_SOFTWARE); 1556 } 1557 if (!FD_ISSET(sep->se_fd, &allsock)) { 1558 syslog(LOG_ERR, 1559 "%s: %s: not on", __func__, sep->se_service); 1560 exit(EX_SOFTWARE); 1561 } 1562 if (nsock == 0) { 1563 syslog(LOG_ERR, "%s: nsock=0", __func__); 1564 exit(EX_SOFTWARE); 1565 } 1566 nsock--; 1567#endif 1568 FD_CLR(sep->se_fd, &allsock); 1569 if (sep->se_fd == maxsock) 1570 maxsock--; 1571} 1572 1573FILE *fconfig = NULL; 1574struct servtab serv; 1575char line[LINE_MAX]; 1576 1577int 1578setconfig(void) 1579{ 1580 1581 if (fconfig != NULL) { 1582 fseek(fconfig, 0L, SEEK_SET); 1583 return (1); 1584 } 1585 fconfig = fopen(CONFIG, "r"); 1586 return (fconfig != NULL); 1587} 1588 1589void 1590endconfig(void) 1591{ 1592 if (fconfig) { 1593 (void) fclose(fconfig); 1594 fconfig = NULL; 1595 } 1596} 1597 1598struct servtab * 1599getconfigent(void) 1600{ 1601 struct servtab *sep = &serv; 1602 int argc; 1603 char *cp, *arg, *s; 1604 char *versp; 1605 static char TCPMUX_TOKEN[] = "tcpmux/"; 1606#define MUX_LEN (sizeof(TCPMUX_TOKEN)-1) 1607#ifdef IPSEC 1608 char *policy; 1609#endif 1610 int v4bind; 1611#ifdef INET6 1612 int v6bind; 1613#endif 1614 int i; 1615 1616#ifdef IPSEC 1617 policy = NULL; 1618#endif 1619more: 1620 v4bind = 0; 1621#ifdef INET6 1622 v6bind = 0; 1623#endif 1624 while ((cp = nextline(fconfig)) != NULL) { 1625#ifdef IPSEC 1626 /* lines starting with #@ is not a comment, but the policy */ 1627 if (cp[0] == '#' && cp[1] == '@') { 1628 char *p; 1629 for (p = cp + 2; p && *p && isspace(*p); p++) 1630 ; 1631 if (*p == '\0') { 1632 if (policy) 1633 free(policy); 1634 policy = NULL; 1635 } else if (ipsec_get_policylen(p) >= 0) { 1636 if (policy) 1637 free(policy); 1638 policy = newstr(p); 1639 } else { 1640 syslog(LOG_ERR, 1641 "%s: invalid ipsec policy \"%s\"", 1642 CONFIG, p); 1643 exit(EX_CONFIG); 1644 } 1645 } 1646#endif 1647 if (*cp == '#' || *cp == '\0') 1648 continue; 1649 break; 1650 } 1651 if (cp == NULL) 1652 return ((struct servtab *)0); 1653 /* 1654 * clear the static buffer, since some fields (se_ctrladdr, 1655 * for example) don't get initialized here. 1656 */ 1657 memset(sep, 0, sizeof *sep); 1658 arg = skip(&cp); 1659 if (cp == NULL) { 1660 /* got an empty line containing just blanks/tabs. */ 1661 goto more; 1662 } 1663 if (arg[0] == ':') { /* :user:group:perm: */ 1664 char *user, *group, *perm; 1665 struct passwd *pw; 1666 struct group *gr; 1667 user = arg+1; 1668 if ((group = strchr(user, ':')) == NULL) { 1669 syslog(LOG_ERR, "no group after user '%s'", user); 1670 goto more; 1671 } 1672 *group++ = '\0'; 1673 if ((perm = strchr(group, ':')) == NULL) { 1674 syslog(LOG_ERR, "no mode after group '%s'", group); 1675 goto more; 1676 } 1677 *perm++ = '\0'; 1678 if ((pw = getpwnam(user)) == NULL) { 1679 syslog(LOG_ERR, "no such user '%s'", user); 1680 goto more; 1681 } 1682 sep->se_sockuid = pw->pw_uid; 1683 if ((gr = getgrnam(group)) == NULL) { 1684 syslog(LOG_ERR, "no such user '%s'", group); 1685 goto more; 1686 } 1687 sep->se_sockgid = gr->gr_gid; 1688 sep->se_sockmode = strtol(perm, &arg, 8); 1689 if (*arg != ':') { 1690 syslog(LOG_ERR, "bad mode '%s'", perm); 1691 goto more; 1692 } 1693 *arg++ = '\0'; 1694 } else { 1695 sep->se_sockuid = euid; 1696 sep->se_sockgid = egid; 1697 sep->se_sockmode = 0200; 1698 } 1699 if (strncmp(arg, TCPMUX_TOKEN, MUX_LEN) == 0) { 1700 char *c = arg + MUX_LEN; 1701 if (*c == '+') { 1702 sep->se_type = MUXPLUS_TYPE; 1703 c++; 1704 } else 1705 sep->se_type = MUX_TYPE; 1706 sep->se_service = newstr(c); 1707 } else { 1708 sep->se_service = newstr(arg); 1709 sep->se_type = NORM_TYPE; 1710 } 1711 arg = sskip(&cp); 1712 if (strcmp(arg, "stream") == 0) 1713 sep->se_socktype = SOCK_STREAM; 1714 else if (strcmp(arg, "dgram") == 0) 1715 sep->se_socktype = SOCK_DGRAM; 1716 else if (strcmp(arg, "rdm") == 0) 1717 sep->se_socktype = SOCK_RDM; 1718 else if (strcmp(arg, "seqpacket") == 0) 1719 sep->se_socktype = SOCK_SEQPACKET; 1720 else if (strcmp(arg, "raw") == 0) 1721 sep->se_socktype = SOCK_RAW; 1722 else 1723 sep->se_socktype = -1; 1724 1725 arg = sskip(&cp); 1726 if (strncmp(arg, "tcp", 3) == 0) { 1727 sep->se_proto = newstr(strsep(&arg, "/")); 1728 if (arg != NULL && (strcmp(arg, "faith") == 0)) { 1729 syslog(LOG_ERR, "faith has been deprecated"); 1730 goto more; 1731 } 1732 } else { 1733 if (sep->se_type == NORM_TYPE && 1734 strncmp(arg, "faith/", 6) == 0) { 1735 syslog(LOG_ERR, "faith has been deprecated"); 1736 goto more; 1737 } 1738 sep->se_proto = newstr(arg); 1739 } 1740 if (strncmp(sep->se_proto, "rpc/", 4) == 0) { 1741 memmove(sep->se_proto, sep->se_proto + 4, 1742 strlen(sep->se_proto) + 1 - 4); 1743 sep->se_rpc = 1; 1744 sep->se_rpc_prog = sep->se_rpc_lowvers = 1745 sep->se_rpc_highvers = 0; 1746 if ((versp = strrchr(sep->se_service, '/'))) { 1747 *versp++ = '\0'; 1748 switch (sscanf(versp, "%u-%u", 1749 &sep->se_rpc_lowvers, 1750 &sep->se_rpc_highvers)) { 1751 case 2: 1752 break; 1753 case 1: 1754 sep->se_rpc_highvers = 1755 sep->se_rpc_lowvers; 1756 break; 1757 default: 1758 syslog(LOG_ERR, 1759 "bad RPC version specifier; %s", 1760 sep->se_service); 1761 freeconfig(sep); 1762 goto more; 1763 } 1764 } 1765 else { 1766 sep->se_rpc_lowvers = 1767 sep->se_rpc_highvers = 1; 1768 } 1769 } 1770 sep->se_nomapped = 0; 1771 if (strcmp(sep->se_proto, "unix") == 0) { 1772 sep->se_family = AF_UNIX; 1773 } else { 1774 while (isdigit(sep->se_proto[strlen(sep->se_proto) - 1])) { 1775#ifdef INET6 1776 if (sep->se_proto[strlen(sep->se_proto) - 1] == '6') { 1777 sep->se_proto[strlen(sep->se_proto) - 1] = '\0'; 1778 v6bind = 1; 1779 continue; 1780 } 1781#endif 1782 if (sep->se_proto[strlen(sep->se_proto) - 1] == '4') { 1783 sep->se_proto[strlen(sep->se_proto) - 1] = '\0'; 1784 v4bind = 1; 1785 continue; 1786 } 1787 /* illegal version num */ 1788 syslog(LOG_ERR, "bad IP version for %s", sep->se_proto); 1789 freeconfig(sep); 1790 goto more; 1791 } 1792#ifdef INET6 1793 if (v6bind && !v6bind_ok) { 1794 syslog(LOG_INFO, "IPv6 bind is ignored for %s", 1795 sep->se_service); 1796 if (v4bind && v4bind_ok) 1797 v6bind = 0; 1798 else { 1799 freeconfig(sep); 1800 goto more; 1801 } 1802 } 1803 if (v6bind) { 1804 sep->se_family = AF_INET6; 1805 if (!v4bind || !v4bind_ok) 1806 sep->se_nomapped = 1; 1807 } else 1808#endif 1809 { /* default to v4 bind if not v6 bind */ 1810 if (!v4bind_ok) { 1811 syslog(LOG_NOTICE, "IPv4 bind is ignored for %s", 1812 sep->se_service); 1813 freeconfig(sep); 1814 goto more; 1815 } 1816 sep->se_family = AF_INET; 1817 } 1818 } 1819 /* init ctladdr */ 1820 switch(sep->se_family) { 1821 case AF_INET: 1822 memcpy(&sep->se_ctrladdr4, bind_sa4, 1823 sizeof(sep->se_ctrladdr4)); 1824 sep->se_ctrladdr_size = sizeof(sep->se_ctrladdr4); 1825 break; 1826#ifdef INET6 1827 case AF_INET6: 1828 memcpy(&sep->se_ctrladdr6, bind_sa6, 1829 sizeof(sep->se_ctrladdr6)); 1830 sep->se_ctrladdr_size = sizeof(sep->se_ctrladdr6); 1831 break; 1832#endif 1833 case AF_UNIX: 1834 if (strlen(sep->se_service) >= sizeof(sep->se_ctrladdr_un.sun_path)) { 1835 syslog(LOG_ERR, 1836 "domain socket pathname too long for service %s", 1837 sep->se_service); 1838 goto more; 1839 } 1840 memset(&sep->se_ctrladdr, 0, sizeof(sep->se_ctrladdr)); 1841 sep->se_ctrladdr_un.sun_family = sep->se_family; 1842 sep->se_ctrladdr_un.sun_len = strlen(sep->se_service); 1843 strcpy(sep->se_ctrladdr_un.sun_path, sep->se_service); 1844 sep->se_ctrladdr_size = SUN_LEN(&sep->se_ctrladdr_un); 1845 } 1846 arg = sskip(&cp); 1847 if (!strncmp(arg, "wait", 4)) 1848 sep->se_accept = 0; 1849 else if (!strncmp(arg, "nowait", 6)) 1850 sep->se_accept = 1; 1851 else { 1852 syslog(LOG_ERR, 1853 "%s: bad wait/nowait for service %s", 1854 CONFIG, sep->se_service); 1855 goto more; 1856 } 1857 sep->se_maxchild = -1; 1858 sep->se_maxcpm = -1; 1859 sep->se_maxperip = -1; 1860 if ((s = strchr(arg, '/')) != NULL) { 1861 char *eptr; 1862 u_long val; 1863 1864 val = strtoul(s + 1, &eptr, 10); 1865 if (eptr == s + 1 || val > MAX_MAXCHLD) { 1866 syslog(LOG_ERR, 1867 "%s: bad max-child for service %s", 1868 CONFIG, sep->se_service); 1869 goto more; 1870 } 1871 if (debug) 1872 if (!sep->se_accept && val != 1) 1873 warnx("maxchild=%lu for wait service %s" 1874 " not recommended", val, sep->se_service); 1875 sep->se_maxchild = val; 1876 if (*eptr == '/') 1877 sep->se_maxcpm = strtol(eptr + 1, &eptr, 10); 1878 if (*eptr == '/') 1879 sep->se_maxperip = strtol(eptr + 1, &eptr, 10); 1880 /* 1881 * explicitly do not check for \0 for future expansion / 1882 * backwards compatibility 1883 */ 1884 } 1885 if (ISMUX(sep)) { 1886 /* 1887 * Silently enforce "nowait" mode for TCPMUX services 1888 * since they don't have an assigned port to listen on. 1889 */ 1890 sep->se_accept = 1; 1891 if (strcmp(sep->se_proto, "tcp")) { 1892 syslog(LOG_ERR, 1893 "%s: bad protocol for tcpmux service %s", 1894 CONFIG, sep->se_service); 1895 goto more; 1896 } 1897 if (sep->se_socktype != SOCK_STREAM) { 1898 syslog(LOG_ERR, 1899 "%s: bad socket type for tcpmux service %s", 1900 CONFIG, sep->se_service); 1901 goto more; 1902 } 1903 } 1904 sep->se_user = newstr(sskip(&cp)); 1905#ifdef LOGIN_CAP 1906 if ((s = strrchr(sep->se_user, '/')) != NULL) { 1907 *s = '\0'; 1908 sep->se_class = newstr(s + 1); 1909 } else 1910 sep->se_class = newstr(RESOURCE_RC); 1911#endif 1912 if ((s = strrchr(sep->se_user, ':')) != NULL) { 1913 *s = '\0'; 1914 sep->se_group = newstr(s + 1); 1915 } else 1916 sep->se_group = NULL; 1917 sep->se_server = newstr(sskip(&cp)); 1918 if ((sep->se_server_name = strrchr(sep->se_server, '/'))) 1919 sep->se_server_name++; 1920 if (strcmp(sep->se_server, "internal") == 0) { 1921 struct biltin *bi; 1922 1923 for (bi = biltins; bi->bi_service; bi++) 1924 if (bi->bi_socktype == sep->se_socktype && 1925 matchservent(bi->bi_service, sep->se_service, 1926 sep->se_proto)) 1927 break; 1928 if (bi->bi_service == 0) { 1929 syslog(LOG_ERR, "internal service %s unknown", 1930 sep->se_service); 1931 goto more; 1932 } 1933 sep->se_accept = 1; /* force accept mode for built-ins */ 1934 sep->se_bi = bi; 1935 } else 1936 sep->se_bi = NULL; 1937 if (sep->se_maxperip < 0) 1938 sep->se_maxperip = maxperip; 1939 if (sep->se_maxcpm < 0) 1940 sep->se_maxcpm = maxcpm; 1941 if (sep->se_maxchild < 0) { /* apply default max-children */ 1942 if (sep->se_bi && sep->se_bi->bi_maxchild >= 0) 1943 sep->se_maxchild = sep->se_bi->bi_maxchild; 1944 else if (sep->se_accept) 1945 sep->se_maxchild = MAX(maxchild, 0); 1946 else 1947 sep->se_maxchild = 1; 1948 } 1949 if (sep->se_maxchild > 0) { 1950 sep->se_pids = malloc(sep->se_maxchild * sizeof(*sep->se_pids)); 1951 if (sep->se_pids == NULL) { 1952 syslog(LOG_ERR, "malloc: %m"); 1953 exit(EX_OSERR); 1954 } 1955 } 1956 argc = 0; 1957 for (arg = skip(&cp); cp; arg = skip(&cp)) 1958 if (argc < MAXARGV) { 1959 sep->se_argv[argc++] = newstr(arg); 1960 } else { 1961 syslog(LOG_ERR, 1962 "%s: too many arguments for service %s", 1963 CONFIG, sep->se_service); 1964 goto more; 1965 } 1966 while (argc <= MAXARGV) 1967 sep->se_argv[argc++] = NULL; 1968 for (i = 0; i < PERIPSIZE; ++i) 1969 LIST_INIT(&sep->se_conn[i]); 1970#ifdef IPSEC 1971 sep->se_policy = policy ? newstr(policy) : NULL; 1972#endif 1973 return (sep); 1974} 1975 1976void 1977freeconfig(struct servtab *cp) 1978{ 1979 int i; 1980 1981 if (cp->se_service) 1982 free(cp->se_service); 1983 if (cp->se_proto) 1984 free(cp->se_proto); 1985 if (cp->se_user) 1986 free(cp->se_user); 1987 if (cp->se_group) 1988 free(cp->se_group); 1989#ifdef LOGIN_CAP 1990 if (cp->se_class) 1991 free(cp->se_class); 1992#endif 1993 if (cp->se_server) 1994 free(cp->se_server); 1995 if (cp->se_pids) 1996 free(cp->se_pids); 1997 for (i = 0; i < MAXARGV; i++) 1998 if (cp->se_argv[i]) 1999 free(cp->se_argv[i]); 2000 free_connlist(cp); 2001#ifdef IPSEC 2002 if (cp->se_policy) 2003 free(cp->se_policy); 2004#endif 2005} 2006 2007 2008/* 2009 * Safe skip - if skip returns null, log a syntax error in the 2010 * configuration file and exit. 2011 */ 2012char * 2013sskip(char **cpp) 2014{ 2015 char *cp; 2016 2017 cp = skip(cpp); 2018 if (cp == NULL) { 2019 syslog(LOG_ERR, "%s: syntax error", CONFIG); 2020 exit(EX_DATAERR); 2021 } 2022 return (cp); 2023} 2024 2025char * 2026skip(char **cpp) 2027{ 2028 char *cp = *cpp; 2029 char *start; 2030 char quote = '\0'; 2031 2032again: 2033 while (*cp == ' ' || *cp == '\t') 2034 cp++; 2035 if (*cp == '\0') { 2036 int c; 2037 2038 c = getc(fconfig); 2039 (void) ungetc(c, fconfig); 2040 if (c == ' ' || c == '\t') 2041 if ((cp = nextline(fconfig))) 2042 goto again; 2043 *cpp = (char *)0; 2044 return ((char *)0); 2045 } 2046 if (*cp == '"' || *cp == '\'') 2047 quote = *cp++; 2048 start = cp; 2049 if (quote) 2050 while (*cp && *cp != quote) 2051 cp++; 2052 else 2053 while (*cp && *cp != ' ' && *cp != '\t') 2054 cp++; 2055 if (*cp != '\0') 2056 *cp++ = '\0'; 2057 *cpp = cp; 2058 return (start); 2059} 2060 2061char * 2062nextline(FILE *fd) 2063{ 2064 char *cp; 2065 2066 if (fgets(line, sizeof (line), fd) == NULL) 2067 return ((char *)0); 2068 cp = strchr(line, '\n'); 2069 if (cp) 2070 *cp = '\0'; 2071 return (line); 2072} 2073 2074char * 2075newstr(const char *cp) 2076{ 2077 char *cr; 2078 2079 if ((cr = strdup(cp != NULL ? cp : ""))) 2080 return (cr); 2081 syslog(LOG_ERR, "strdup: %m"); 2082 exit(EX_OSERR); 2083} 2084 2085void 2086inetd_setproctitle(const char *a, int s) 2087{ 2088 socklen_t size; 2089 struct sockaddr_storage ss; 2090 char buf[80], pbuf[NI_MAXHOST]; 2091 2092 size = sizeof(ss); 2093 if (getpeername(s, (struct sockaddr *)&ss, &size) == 0) { 2094 getnameinfo((struct sockaddr *)&ss, size, pbuf, sizeof(pbuf), 2095 NULL, 0, NI_NUMERICHOST); 2096 (void) sprintf(buf, "%s [%s]", a, pbuf); 2097 } else 2098 (void) sprintf(buf, "%s", a); 2099 setproctitle("%s", buf); 2100} 2101 2102int 2103check_loop(const struct sockaddr *sa, const struct servtab *sep) 2104{ 2105 struct servtab *se2; 2106 char pname[NI_MAXHOST]; 2107 2108 for (se2 = servtab; se2; se2 = se2->se_next) { 2109 if (!se2->se_bi || se2->se_socktype != SOCK_DGRAM) 2110 continue; 2111 2112 switch (se2->se_family) { 2113 case AF_INET: 2114 if (((const struct sockaddr_in *)sa)->sin_port == 2115 se2->se_ctrladdr4.sin_port) 2116 goto isloop; 2117 continue; 2118#ifdef INET6 2119 case AF_INET6: 2120 if (((const struct sockaddr_in6 *)sa)->sin6_port == 2121 se2->se_ctrladdr6.sin6_port) 2122 goto isloop; 2123 continue; 2124#endif 2125 default: 2126 continue; 2127 } 2128 isloop: 2129 getnameinfo(sa, sa->sa_len, pname, sizeof(pname), NULL, 0, 2130 NI_NUMERICHOST); 2131 syslog(LOG_WARNING, "%s/%s:%s/%s loop request REFUSED from %s", 2132 sep->se_service, sep->se_proto, 2133 se2->se_service, se2->se_proto, 2134 pname); 2135 return 1; 2136 } 2137 return 0; 2138} 2139 2140/* 2141 * print_service: 2142 * Dump relevant information to stderr 2143 */ 2144void 2145print_service(const char *action, const struct servtab *sep) 2146{ 2147 fprintf(stderr, 2148 "%s: %s proto=%s accept=%d max=%d user=%s group=%s" 2149#ifdef LOGIN_CAP 2150 "class=%s" 2151#endif 2152 " builtin=%p server=%s" 2153#ifdef IPSEC 2154 " policy=\"%s\"" 2155#endif 2156 "\n", 2157 action, sep->se_service, sep->se_proto, 2158 sep->se_accept, sep->se_maxchild, sep->se_user, sep->se_group, 2159#ifdef LOGIN_CAP 2160 sep->se_class, 2161#endif 2162 (void *) sep->se_bi, sep->se_server 2163#ifdef IPSEC 2164 , (sep->se_policy ? sep->se_policy : "") 2165#endif 2166 ); 2167} 2168 2169#define CPMHSIZE 256 2170#define CPMHMASK (CPMHSIZE-1) 2171#define CHTGRAN 10 2172#define CHTSIZE 6 2173 2174typedef struct CTime { 2175 unsigned long ct_Ticks; 2176 int ct_Count; 2177} CTime; 2178 2179typedef struct CHash { 2180 union { 2181 struct in_addr c4_Addr; 2182 struct in6_addr c6_Addr; 2183 } cu_Addr; 2184#define ch_Addr4 cu_Addr.c4_Addr 2185#define ch_Addr6 cu_Addr.c6_Addr 2186 int ch_Family; 2187 time_t ch_LTime; 2188 char *ch_Service; 2189 CTime ch_Times[CHTSIZE]; 2190} CHash; 2191 2192CHash CHashAry[CPMHSIZE]; 2193 2194int 2195cpmip(const struct servtab *sep, int ctrl) 2196{ 2197 struct sockaddr_storage rss; 2198 socklen_t rssLen = sizeof(rss); 2199 int r = 0; 2200 2201 /* 2202 * If getpeername() fails, just let it through (if logging is 2203 * enabled the condition is caught elsewhere) 2204 */ 2205 2206 if (sep->se_maxcpm > 0 && 2207 (sep->se_family == AF_INET || sep->se_family == AF_INET6) && 2208 getpeername(ctrl, (struct sockaddr *)&rss, &rssLen) == 0 ) { 2209 time_t t = time(NULL); 2210 int hv = 0xABC3D20F; 2211 int i; 2212 int cnt = 0; 2213 CHash *chBest = NULL; 2214 unsigned int ticks = t / CHTGRAN; 2215 struct sockaddr_in *sin4; 2216#ifdef INET6 2217 struct sockaddr_in6 *sin6; 2218#endif 2219 2220 sin4 = (struct sockaddr_in *)&rss; 2221#ifdef INET6 2222 sin6 = (struct sockaddr_in6 *)&rss; 2223#endif 2224 { 2225 char *p; 2226 int addrlen; 2227 2228 switch (rss.ss_family) { 2229 case AF_INET: 2230 p = (char *)&sin4->sin_addr; 2231 addrlen = sizeof(struct in_addr); 2232 break; 2233#ifdef INET6 2234 case AF_INET6: 2235 p = (char *)&sin6->sin6_addr; 2236 addrlen = sizeof(struct in6_addr); 2237 break; 2238#endif 2239 default: 2240 /* should not happen */ 2241 return -1; 2242 } 2243 2244 for (i = 0; i < addrlen; ++i, ++p) { 2245 hv = (hv << 5) ^ (hv >> 23) ^ *p; 2246 } 2247 hv = (hv ^ (hv >> 16)); 2248 } 2249 for (i = 0; i < 5; ++i) { 2250 CHash *ch = &CHashAry[(hv + i) & CPMHMASK]; 2251 2252 if (rss.ss_family == AF_INET && 2253 ch->ch_Family == AF_INET && 2254 sin4->sin_addr.s_addr == ch->ch_Addr4.s_addr && 2255 ch->ch_Service && strcmp(sep->se_service, 2256 ch->ch_Service) == 0) { 2257 chBest = ch; 2258 break; 2259 } 2260#ifdef INET6 2261 if (rss.ss_family == AF_INET6 && 2262 ch->ch_Family == AF_INET6 && 2263 IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr, 2264 &ch->ch_Addr6) != 0 && 2265 ch->ch_Service && strcmp(sep->se_service, 2266 ch->ch_Service) == 0) { 2267 chBest = ch; 2268 break; 2269 } 2270#endif 2271 if (chBest == NULL || ch->ch_LTime == 0 || 2272 ch->ch_LTime < chBest->ch_LTime) { 2273 chBest = ch; 2274 } 2275 } 2276 if ((rss.ss_family == AF_INET && 2277 (chBest->ch_Family != AF_INET || 2278 sin4->sin_addr.s_addr != chBest->ch_Addr4.s_addr)) || 2279 chBest->ch_Service == NULL || 2280 strcmp(sep->se_service, chBest->ch_Service) != 0) { 2281 chBest->ch_Family = sin4->sin_family; 2282 chBest->ch_Addr4 = sin4->sin_addr; 2283 if (chBest->ch_Service) 2284 free(chBest->ch_Service); 2285 chBest->ch_Service = strdup(sep->se_service); 2286 bzero(chBest->ch_Times, sizeof(chBest->ch_Times)); 2287 } 2288#ifdef INET6 2289 if ((rss.ss_family == AF_INET6 && 2290 (chBest->ch_Family != AF_INET6 || 2291 IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr, 2292 &chBest->ch_Addr6) == 0)) || 2293 chBest->ch_Service == NULL || 2294 strcmp(sep->se_service, chBest->ch_Service) != 0) { 2295 chBest->ch_Family = sin6->sin6_family; 2296 chBest->ch_Addr6 = sin6->sin6_addr; 2297 if (chBest->ch_Service) 2298 free(chBest->ch_Service); 2299 chBest->ch_Service = strdup(sep->se_service); 2300 bzero(chBest->ch_Times, sizeof(chBest->ch_Times)); 2301 } 2302#endif 2303 chBest->ch_LTime = t; 2304 { 2305 CTime *ct = &chBest->ch_Times[ticks % CHTSIZE]; 2306 if (ct->ct_Ticks != ticks) { 2307 ct->ct_Ticks = ticks; 2308 ct->ct_Count = 0; 2309 } 2310 ++ct->ct_Count; 2311 } 2312 for (i = 0; i < CHTSIZE; ++i) { 2313 CTime *ct = &chBest->ch_Times[i]; 2314 if (ct->ct_Ticks <= ticks && 2315 ct->ct_Ticks >= ticks - CHTSIZE) { 2316 cnt += ct->ct_Count; 2317 } 2318 } 2319 if ((cnt * 60) / (CHTSIZE * CHTGRAN) > sep->se_maxcpm) { 2320 char pname[NI_MAXHOST]; 2321 2322 getnameinfo((struct sockaddr *)&rss, 2323 ((struct sockaddr *)&rss)->sa_len, 2324 pname, sizeof(pname), NULL, 0, 2325 NI_NUMERICHOST); 2326 r = -1; 2327 syslog(LOG_ERR, 2328 "%s from %s exceeded counts/min (limit %d/min)", 2329 sep->se_service, pname, 2330 sep->se_maxcpm); 2331 } 2332 } 2333 return(r); 2334} 2335 2336static struct conninfo * 2337search_conn(struct servtab *sep, int ctrl) 2338{ 2339 struct sockaddr_storage ss; 2340 socklen_t sslen = sizeof(ss); 2341 struct conninfo *conn; 2342 int hv; 2343 char pname[NI_MAXHOST], pname2[NI_MAXHOST]; 2344 2345 if (sep->se_maxperip <= 0) 2346 return NULL; 2347 2348 /* 2349 * If getpeername() fails, just let it through (if logging is 2350 * enabled the condition is caught elsewhere) 2351 */ 2352 if (getpeername(ctrl, (struct sockaddr *)&ss, &sslen) != 0) 2353 return NULL; 2354 2355 switch (ss.ss_family) { 2356 case AF_INET: 2357 hv = hashval((char *)&((struct sockaddr_in *)&ss)->sin_addr, 2358 sizeof(struct in_addr)); 2359 break; 2360#ifdef INET6 2361 case AF_INET6: 2362 hv = hashval((char *)&((struct sockaddr_in6 *)&ss)->sin6_addr, 2363 sizeof(struct in6_addr)); 2364 break; 2365#endif 2366 default: 2367 /* 2368 * Since we only support AF_INET and AF_INET6, just 2369 * let other than AF_INET and AF_INET6 through. 2370 */ 2371 return NULL; 2372 } 2373 2374 if (getnameinfo((struct sockaddr *)&ss, sslen, pname, sizeof(pname), 2375 NULL, 0, NI_NUMERICHOST) != 0) 2376 return NULL; 2377 2378 LIST_FOREACH(conn, &sep->se_conn[hv], co_link) { 2379 if (getnameinfo((struct sockaddr *)&conn->co_addr, 2380 conn->co_addr.ss_len, pname2, sizeof(pname2), NULL, 0, 2381 NI_NUMERICHOST) == 0 && 2382 strcmp(pname, pname2) == 0) 2383 break; 2384 } 2385 2386 if (conn == NULL) { 2387 if ((conn = malloc(sizeof(struct conninfo))) == NULL) { 2388 syslog(LOG_ERR, "malloc: %m"); 2389 exit(EX_OSERR); 2390 } 2391 conn->co_proc = malloc(sep->se_maxperip * sizeof(*conn->co_proc)); 2392 if (conn->co_proc == NULL) { 2393 syslog(LOG_ERR, "malloc: %m"); 2394 exit(EX_OSERR); 2395 } 2396 memcpy(&conn->co_addr, (struct sockaddr *)&ss, sslen); 2397 conn->co_numchild = 0; 2398 LIST_INSERT_HEAD(&sep->se_conn[hv], conn, co_link); 2399 } 2400 2401 /* 2402 * Since a child process is not invoked yet, we cannot 2403 * determine a pid of a child. So, co_proc and co_numchild 2404 * should be filled leter. 2405 */ 2406 2407 return conn; 2408} 2409 2410static int 2411room_conn(struct servtab *sep, struct conninfo *conn) 2412{ 2413 char pname[NI_MAXHOST]; 2414 2415 if (conn->co_numchild >= sep->se_maxperip) { 2416 getnameinfo((struct sockaddr *)&conn->co_addr, 2417 conn->co_addr.ss_len, pname, sizeof(pname), NULL, 0, 2418 NI_NUMERICHOST); 2419 syslog(LOG_ERR, "%s from %s exceeded counts (limit %d)", 2420 sep->se_service, pname, sep->se_maxperip); 2421 return 0; 2422 } 2423 return 1; 2424} 2425 2426static void 2427addchild_conn(struct conninfo *conn, pid_t pid) 2428{ 2429 struct procinfo *proc; 2430 2431 if (conn == NULL) 2432 return; 2433 2434 if ((proc = search_proc(pid, 1)) != NULL) { 2435 if (proc->pr_conn != NULL) { 2436 syslog(LOG_ERR, 2437 "addchild_conn: child already on process list"); 2438 exit(EX_OSERR); 2439 } 2440 proc->pr_conn = conn; 2441 } 2442 2443 conn->co_proc[conn->co_numchild++] = proc; 2444} 2445 2446static void 2447reapchild_conn(pid_t pid) 2448{ 2449 struct procinfo *proc; 2450 struct conninfo *conn; 2451 int i; 2452 2453 if ((proc = search_proc(pid, 0)) == NULL) 2454 return; 2455 if ((conn = proc->pr_conn) == NULL) 2456 return; 2457 for (i = 0; i < conn->co_numchild; ++i) 2458 if (conn->co_proc[i] == proc) { 2459 conn->co_proc[i] = conn->co_proc[--conn->co_numchild]; 2460 break; 2461 } 2462 free_proc(proc); 2463 free_conn(conn); 2464} 2465 2466static void 2467resize_conn(struct servtab *sep, int maxpip) 2468{ 2469 struct conninfo *conn; 2470 int i, j; 2471 2472 if (sep->se_maxperip <= 0) 2473 return; 2474 if (maxpip <= 0) { 2475 free_connlist(sep); 2476 return; 2477 } 2478 for (i = 0; i < PERIPSIZE; ++i) { 2479 LIST_FOREACH(conn, &sep->se_conn[i], co_link) { 2480 for (j = maxpip; j < conn->co_numchild; ++j) 2481 free_proc(conn->co_proc[j]); 2482 conn->co_proc = realloc(conn->co_proc, 2483 maxpip * sizeof(*conn->co_proc)); 2484 if (conn->co_proc == NULL) { 2485 syslog(LOG_ERR, "realloc: %m"); 2486 exit(EX_OSERR); 2487 } 2488 if (conn->co_numchild > maxpip) 2489 conn->co_numchild = maxpip; 2490 } 2491 } 2492} 2493 2494static void 2495free_connlist(struct servtab *sep) 2496{ 2497 struct conninfo *conn; 2498 int i, j; 2499 2500 for (i = 0; i < PERIPSIZE; ++i) { 2501 while ((conn = LIST_FIRST(&sep->se_conn[i])) != NULL) { 2502 for (j = 0; j < conn->co_numchild; ++j) 2503 free_proc(conn->co_proc[j]); 2504 conn->co_numchild = 0; 2505 free_conn(conn); 2506 } 2507 } 2508} 2509 2510static void 2511free_conn(struct conninfo *conn) 2512{ 2513 if (conn == NULL) 2514 return; 2515 if (conn->co_numchild <= 0) { 2516 LIST_REMOVE(conn, co_link); 2517 free(conn->co_proc); 2518 free(conn); 2519 } 2520} 2521 2522static struct procinfo * 2523search_proc(pid_t pid, int add) 2524{ 2525 struct procinfo *proc; 2526 int hv; 2527 2528 hv = hashval((char *)&pid, sizeof(pid)); 2529 LIST_FOREACH(proc, &proctable[hv], pr_link) { 2530 if (proc->pr_pid == pid) 2531 break; 2532 } 2533 if (proc == NULL && add) { 2534 if ((proc = malloc(sizeof(struct procinfo))) == NULL) { 2535 syslog(LOG_ERR, "malloc: %m"); 2536 exit(EX_OSERR); 2537 } 2538 proc->pr_pid = pid; 2539 proc->pr_conn = NULL; 2540 LIST_INSERT_HEAD(&proctable[hv], proc, pr_link); 2541 } 2542 return proc; 2543} 2544 2545static void 2546free_proc(struct procinfo *proc) 2547{ 2548 if (proc == NULL) 2549 return; 2550 LIST_REMOVE(proc, pr_link); 2551 free(proc); 2552} 2553 2554static int 2555hashval(char *p, int len) 2556{ 2557 int i, hv = 0xABC3D20F; 2558 2559 for (i = 0; i < len; ++i, ++p) 2560 hv = (hv << 5) ^ (hv >> 23) ^ *p; 2561 hv = (hv ^ (hv >> 16)) & (PERIPSIZE - 1); 2562 return hv; 2563} 2564