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