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