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