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