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