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