inetd.c revision 1.76
1/* $NetBSD: inetd.c,v 1.76 2002/01/21 14:42:28 wiz Exp $ */ 2 3/*- 4 * Copyright (c) 1998 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, 9 * NASA Ames Research Center. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 3. All advertising materials mentioning features or use of this software 20 * must display the following acknowledgement: 21 * This product includes software developed by the NetBSD 22 * Foundation, Inc. and its contributors. 23 * 4. Neither the name of The NetBSD Foundation nor the names of its 24 * contributors may be used to endorse or promote products derived 25 * from this software without specific prior written permission. 26 * 27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 37 * POSSIBILITY OF SUCH DAMAGE. 38 */ 39 40/* 41 * Copyright (c) 1983, 1991, 1993, 1994 42 * The Regents of the University of California. All rights reserved. 43 * 44 * Redistribution and use in source and binary forms, with or without 45 * modification, are permitted provided that the following conditions 46 * are met: 47 * 1. Redistributions of source code must retain the above copyright 48 * notice, this list of conditions and the following disclaimer. 49 * 2. Redistributions in binary form must reproduce the above copyright 50 * notice, this list of conditions and the following disclaimer in the 51 * documentation and/or other materials provided with the distribution. 52 * 3. All advertising materials mentioning features or use of this software 53 * must display the following acknowledgement: 54 * This product includes software developed by the University of 55 * California, Berkeley and its contributors. 56 * 4. Neither the name of the University nor the names of its contributors 57 * may be used to endorse or promote products derived from this software 58 * without specific prior written permission. 59 * 60 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 61 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 62 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 63 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 64 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 65 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 66 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 67 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 68 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 69 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 70 * SUCH DAMAGE. 71 */ 72 73#include <sys/cdefs.h> 74#ifndef lint 75__COPYRIGHT("@(#) Copyright (c) 1983, 1991, 1993, 1994\n\ 76 The Regents of the University of California. All rights reserved.\n"); 77#if 0 78static char sccsid[] = "@(#)inetd.c 8.4 (Berkeley) 4/13/94"; 79#else 80__RCSID("$NetBSD: inetd.c,v 1.76 2002/01/21 14:42:28 wiz Exp $"); 81#endif 82#endif /* not lint */ 83 84/* 85 * Inetd - Internet super-server 86 * 87 * This program invokes all internet services as needed. Connection-oriented 88 * services are invoked each time a connection is made, by creating a process. 89 * This process is passed the connection as file descriptor 0 and is expected 90 * to do a getpeername to find out the source host and port. 91 * 92 * Datagram oriented services are invoked when a datagram 93 * arrives; a process is created and passed a pending message 94 * on file descriptor 0. Datagram servers may either connect 95 * to their peer, freeing up the original socket for inetd 96 * to receive further messages on, or ``take over the socket'', 97 * processing all arriving datagrams and, eventually, timing 98 * out. The first type of server is said to be ``multi-threaded''; 99 * the second type of server ``single-threaded''. 100 * 101 * Inetd uses a configuration file which is read at startup 102 * and, possibly, at some later time in response to a hangup signal. 103 * The configuration file is ``free format'' with fields given in the 104 * order shown below. Continuation lines for an entry must being with 105 * a space or tab. All fields must be present in each entry. 106 * 107 * service name must be in /etc/services or must 108 * name a tcpmux service 109 * socket type stream/dgram/raw/rdm/seqpacket 110 * protocol must be in /etc/protocols 111 * wait/nowait[:max] single-threaded/multi-threaded, max # 112 * user[:group] user/group to run daemon as 113 * server program full path name 114 * server program arguments maximum of MAXARGS (20) 115 * 116 * For RPC services 117 * service name/version must be in /etc/rpc 118 * socket type stream/dgram/raw/rdm/seqpacket 119 * protocol must be in /etc/protocols 120 * wait/nowait[:max] single-threaded/multi-threaded 121 * user[:group] user to run daemon as 122 * server program full path name 123 * server program arguments maximum of MAXARGS (20) 124 * 125 * For non-RPC services, the "service name" can be of the form 126 * hostaddress:servicename, in which case the hostaddress is used 127 * as the host portion of the address to listen on. If hostaddress 128 * consists of a single `*' character, INADDR_ANY is used. 129 * 130 * A line can also consist of just 131 * hostaddress: 132 * where hostaddress is as in the preceding paragraph. Such a line must 133 * have no further fields; the specified hostaddress is remembered and 134 * used for all further lines that have no hostaddress specified, 135 * until the next such line (or EOF). (This is why * is provided to 136 * allow explicit specification of INADDR_ANY.) A line 137 * *: 138 * is implicitly in effect at the beginning of the file. 139 * 140 * The hostaddress specifier may (and often will) contain dots; 141 * the service name must not. 142 * 143 * For RPC services, host-address specifiers are accepted and will 144 * work to some extent; however, because of limitations in the 145 * portmapper interface, it will not work to try to give more than 146 * one line for any given RPC service, even if the host-address 147 * specifiers are different. 148 * 149 * TCP services without official port numbers are handled with the 150 * RFC1078-based tcpmux internal service. Tcpmux listens on port 1 for 151 * requests. When a connection is made from a foreign host, the service 152 * requested is passed to tcpmux, which looks it up in the servtab list 153 * and returns the proper entry for the service. Tcpmux returns a 154 * negative reply if the service doesn't exist, otherwise the invoked 155 * server is expected to return the positive reply if the service type in 156 * inetd.conf file has the prefix "tcpmux/". If the service type has the 157 * prefix "tcpmux/+", tcpmux will return the positive reply for the 158 * process; this is for compatibility with older server code, and also 159 * allows you to invoke programs that use stdin/stdout without putting any 160 * special server code in them. Services that use tcpmux are "nowait" 161 * because they do not have a well-known port and hence cannot listen 162 * for new requests. 163 * 164 * Comment lines are indicated by a `#' in column 1. 165 * 166 * #ifdef IPSEC 167 * Comment lines that start with "#@" denote IPsec policy string, as described 168 * in ipsec_set_policy(3). This will affect all the following items in 169 * inetd.conf(8). To reset the policy, just use "#@" line. By default, 170 * there's no IPsec policy. 171 * #endif 172 */ 173 174/* 175 * Here's the scoop concerning the user:group feature: 176 * 177 * 1) set-group-option off. 178 * 179 * a) user = root: NO setuid() or setgid() is done 180 * 181 * b) other: setuid() 182 * setgid(primary group as found in passwd) 183 * initgroups(name, primary group) 184 * 185 * 2) set-group-option on. 186 * 187 * a) user = root: NO setuid() 188 * setgid(specified group) 189 * NO initgroups() 190 * 191 * b) other: setuid() 192 * setgid(specified group) 193 * initgroups(name, specified group) 194 * 195 */ 196 197#include <sys/param.h> 198#include <sys/stat.h> 199#include <sys/ioctl.h> 200#include <sys/socket.h> 201#include <sys/un.h> 202#include <sys/wait.h> 203#include <sys/time.h> 204#include <sys/resource.h> 205 206#ifndef RLIMIT_NOFILE 207#define RLIMIT_NOFILE RLIMIT_OFILE 208#endif 209 210#ifndef NO_RPC 211#define RPC 212#endif 213 214#include <netinet/in.h> 215#include <arpa/inet.h> 216#ifdef RPC 217#include <rpc/rpc.h> 218#include <rpc/rpcb_clnt.h> 219#include <netconfig.h> 220#endif 221 222#include <ctype.h> 223#include <errno.h> 224#include <fcntl.h> 225#include <grp.h> 226#include <netdb.h> 227#include <pwd.h> 228#include <signal.h> 229#include <stdio.h> 230#include <stdlib.h> 231#include <string.h> 232#include <syslog.h> 233#include <unistd.h> 234#include <util.h> 235 236#include "pathnames.h" 237 238#ifdef IPSEC 239#include <netinet6/ipsec.h> 240#ifndef IPSEC_POLICY_IPSEC /* no ipsec support on old ipsec */ 241#undef IPSEC 242#endif 243#include "ipsec.h" 244#endif 245 246#ifdef LIBWRAP 247# include <tcpd.h> 248#ifndef LIBWRAP_ALLOW_FACILITY 249# define LIBWRAP_ALLOW_FACILITY LOG_AUTH 250#endif 251#ifndef LIBWRAP_ALLOW_SEVERITY 252# define LIBWRAP_ALLOW_SEVERITY LOG_INFO 253#endif 254#ifndef LIBWRAP_DENY_FACILITY 255# define LIBWRAP_DENY_FACILITY LOG_AUTH 256#endif 257#ifndef LIBWRAP_DENY_SEVERITY 258# define LIBWRAP_DENY_SEVERITY LOG_WARNING 259#endif 260int allow_severity = LIBWRAP_ALLOW_FACILITY|LIBWRAP_ALLOW_SEVERITY; 261int deny_severity = LIBWRAP_DENY_FACILITY|LIBWRAP_DENY_SEVERITY; 262#endif 263 264#define TOOMANY 40 /* don't start more than TOOMANY */ 265#define CNT_INTVL 60 /* servers in CNT_INTVL sec. */ 266#define RETRYTIME (60*10) /* retry after bind or server fail */ 267 268#define SIGBLOCK (sigmask(SIGCHLD)|sigmask(SIGHUP)|sigmask(SIGALRM)) 269 270int debug; 271#ifdef LIBWRAP 272int lflag; 273#endif 274int nsock, maxsock; 275fd_set allsock; 276int options; 277int timingout; 278struct servent *sp; 279char *curdom; 280#ifdef NI_WITHSCOPEID 281const int niflags = NI_NUMERICHOST | NI_NUMERICSERV | NI_WITHSCOPEID; 282#else 283const int niflags = NI_NUMERICHOST | NI_NUMERICSERV; 284#endif 285 286#ifndef OPEN_MAX 287#define OPEN_MAX 64 288#endif 289 290/* Reserve some descriptors, 3 stdio + at least: 1 log, 1 conf. file */ 291#define FD_MARGIN (8) 292rlim_t rlim_ofile_cur = OPEN_MAX; 293 294#ifdef RLIMIT_NOFILE 295struct rlimit rlim_ofile; 296#endif 297 298struct servtab { 299 char *se_hostaddr; /* host address to listen on */ 300 char *se_service; /* name of service */ 301 int se_socktype; /* type of socket to use */ 302 int se_family; /* address family */ 303 char *se_proto; /* protocol used */ 304 int se_sndbuf; /* sndbuf size */ 305 int se_rcvbuf; /* rcvbuf size */ 306 int se_rpcprog; /* rpc program number */ 307 int se_rpcversl; /* rpc program lowest version */ 308 int se_rpcversh; /* rpc program highest version */ 309#define isrpcservice(sep) ((sep)->se_rpcversl != 0) 310 pid_t se_wait; /* single threaded server */ 311 short se_checked; /* looked at during merge */ 312 char *se_user; /* user name to run as */ 313 char *se_group; /* group name to run as */ 314 struct biltin *se_bi; /* if built-in, description */ 315 char *se_server; /* server program */ 316#define MAXARGV 20 317 char *se_argv[MAXARGV+1]; /* program arguments */ 318#ifdef IPSEC 319 char *se_policy; /* IPsec poilcy string */ 320#endif 321 int se_fd; /* open descriptor */ 322 int se_type; /* type */ 323 union { 324 struct sockaddr se_un_ctrladdr; 325 struct sockaddr_in se_un_ctrladdr_in; 326 struct sockaddr_in6 se_un_ctrladdr_in6; 327 struct sockaddr_un se_un_ctrladdr_un; 328 } se_un; /* bound address */ 329#define se_ctrladdr se_un.se_un_ctrladdr 330#define se_ctrladdr_in se_un.se_un_ctrladdr_in 331#define se_ctrladdr_un se_un.se_un_ctrladdr_un 332 int se_ctrladdr_size; 333 int se_max; /* max # of instances of this service */ 334 int se_count; /* number started since se_time */ 335 struct timeval se_time; /* start of se_count */ 336#ifdef MULOG 337 int se_log; 338#define MULOG_RFC931 0x40000000 339#endif 340 struct servtab *se_next; 341} *servtab; 342 343#define NORM_TYPE 0 344#define MUX_TYPE 1 345#define MUXPLUS_TYPE 2 346#define FAITH_TYPE 3 347#define ISMUX(sep) (((sep)->se_type == MUX_TYPE) || \ 348 ((sep)->se_type == MUXPLUS_TYPE)) 349#define ISMUXPLUS(sep) ((sep)->se_type == MUXPLUS_TYPE) 350 351 352void chargen_dg __P((int, struct servtab *)); 353void chargen_stream __P((int, struct servtab *)); 354void close_sep __P((struct servtab *)); 355void config __P((int)); 356void daytime_dg __P((int, struct servtab *)); 357void daytime_stream __P((int, struct servtab *)); 358void discard_dg __P((int, struct servtab *)); 359void discard_stream __P((int, struct servtab *)); 360void echo_dg __P((int, struct servtab *)); 361void echo_stream __P((int, struct servtab *)); 362void endconfig __P((void)); 363struct servtab *enter __P((struct servtab *)); 364void freeconfig __P((struct servtab *)); 365struct servtab *getconfigent __P((void)); 366void goaway __P((int)); 367void machtime_dg __P((int, struct servtab *)); 368void machtime_stream __P((int, struct servtab *)); 369char *newstr __P((char *)); 370char *nextline __P((FILE *)); 371void print_service __P((char *, struct servtab *)); 372void reapchild __P((int)); 373void retry __P((int)); 374void run_service __P((int, struct servtab *)); 375int setconfig __P((void)); 376void setup __P((struct servtab *)); 377char *sskip __P((char **)); 378char *skip __P((char **)); 379void tcpmux __P((int, struct servtab *)); 380void usage __P((void)); 381void register_rpc __P((struct servtab *sep)); 382void unregister_rpc __P((struct servtab *sep)); 383void bump_nofile __P((void)); 384void inetd_setproctitle __P((char *, int)); 385void initring __P((void)); 386uint32_t machtime __P((void)); 387int port_good_dg __P((struct sockaddr *sa)); 388static int getline __P((int, char *, int)); 389int main __P((int, char *[], char *[])); 390 391struct biltin { 392 char *bi_service; /* internally provided service name */ 393 int bi_socktype; /* type of socket supported */ 394 short bi_fork; /* 1 if should fork before call */ 395 short bi_wait; /* 1 if should wait for child */ 396 void (*bi_fn) __P((int, struct servtab *)); 397 /* function which performs it */ 398} biltins[] = { 399 /* Echo received data */ 400 { "echo", SOCK_STREAM, 1, 0, echo_stream }, 401 { "echo", SOCK_DGRAM, 0, 0, echo_dg }, 402 403 /* Internet /dev/null */ 404 { "discard", SOCK_STREAM, 1, 0, discard_stream }, 405 { "discard", SOCK_DGRAM, 0, 0, discard_dg }, 406 407 /* Return 32 bit time since 1970 */ 408 { "time", SOCK_STREAM, 0, 0, machtime_stream }, 409 { "time", SOCK_DGRAM, 0, 0, machtime_dg }, 410 411 /* Return human-readable time */ 412 { "daytime", SOCK_STREAM, 0, 0, daytime_stream }, 413 { "daytime", SOCK_DGRAM, 0, 0, daytime_dg }, 414 415 /* Familiar character generator */ 416 { "chargen", SOCK_STREAM, 1, 0, chargen_stream }, 417 { "chargen", SOCK_DGRAM, 0, 0, chargen_dg }, 418 419 { "tcpmux", SOCK_STREAM, 1, 0, tcpmux }, 420 421 { NULL } 422}; 423 424/* list of "bad" ports. I.e. ports that are most obviously used for 425 * "cycling packets" denial of service attacks. See /etc/services. 426 * List must end with port number "0". 427 */ 428 429u_int16_t bad_ports[] = { 7, 9, 13, 19, 37, 0}; 430 431 432#define NUMINT (sizeof(intab) / sizeof(struct inent)) 433char *CONFIG = _PATH_INETDCONF; 434char **Argv; 435char *LastArg; 436 437int 438main(argc, argv, envp) 439 int argc; 440 char *argv[], *envp[]; 441{ 442 struct servtab *sep, *nsep; 443 struct sigvec sv; 444 int ch, dofork; 445 pid_t pid; 446 447 Argv = argv; 448 if (envp == 0 || *envp == 0) 449 envp = argv; 450 while (*envp) 451 envp++; 452 LastArg = envp[-1] + strlen(envp[-1]); 453 454 while ((ch = getopt(argc, argv, 455#ifdef LIBWRAP 456 "dl" 457#else 458 "d" 459#endif 460 )) != -1) 461 switch(ch) { 462 case 'd': 463 debug = 1; 464 options |= SO_DEBUG; 465 break; 466#ifdef LIBWRAP 467 case 'l': 468 lflag = 1; 469 break; 470#endif 471 case '?': 472 default: 473 usage(); 474 } 475 argc -= optind; 476 argv += optind; 477 478 if (argc > 0) 479 CONFIG = argv[0]; 480 481 if (debug == 0) 482 daemon(0, 0); 483 openlog("inetd", LOG_PID | LOG_NOWAIT, LOG_DAEMON); 484 pidfile(NULL); 485 486#ifdef RLIMIT_NOFILE 487 if (getrlimit(RLIMIT_NOFILE, &rlim_ofile) < 0) { 488 syslog(LOG_ERR, "getrlimit: %m"); 489 } else { 490 rlim_ofile_cur = rlim_ofile.rlim_cur; 491 if (rlim_ofile_cur == RLIM_INFINITY) /* ! */ 492 rlim_ofile_cur = OPEN_MAX; 493 } 494#endif 495 496 memset(&sv, 0, sizeof(sv)); 497 sv.sv_mask = SIGBLOCK; 498 sv.sv_handler = retry; 499 sigvec(SIGALRM, &sv, (struct sigvec *)0); 500 config(SIGHUP); 501 sv.sv_handler = config; 502 sigvec(SIGHUP, &sv, (struct sigvec *)0); 503 sv.sv_handler = reapchild; 504 sigvec(SIGCHLD, &sv, (struct sigvec *)0); 505 sv.sv_handler = goaway; 506 sigvec(SIGTERM, &sv, (struct sigvec *)0); 507 sv.sv_handler = goaway; 508 sigvec(SIGINT, &sv, (struct sigvec *)0); 509 sv.sv_mask = 0L; 510 sv.sv_handler = SIG_IGN; 511 sigvec(SIGPIPE, &sv, (struct sigvec *)0); 512 513 { 514 /* space for daemons to overwrite environment for ps */ 515#define DUMMYSIZE 100 516 char dummy[DUMMYSIZE]; 517 518 (void)memset(dummy, 'x', DUMMYSIZE - 1); 519 dummy[DUMMYSIZE - 1] = '\0'; 520 521 (void)setenv("inetd_dummy", dummy, 1); 522 } 523 524 for (;;) { 525 int n, ctrl; 526 fd_set readable; 527 528 if (nsock == 0) { 529 (void) sigblock(SIGBLOCK); 530 while (nsock == 0) 531 sigpause(0L); 532 (void) sigsetmask(0L); 533 } 534 readable = allsock; 535 if ((n = select(maxsock + 1, &readable, (fd_set *)0, 536 (fd_set *)0, (struct timeval *)0)) <= 0) { 537 if (n == -1 && errno != EINTR) { 538 syslog(LOG_WARNING, "select: %m"); 539 sleep(1); 540 } 541 continue; 542 } 543 for (sep = servtab; n && sep; sep = nsep) { 544 nsep = sep->se_next; 545 if (sep->se_fd != -1 && FD_ISSET(sep->se_fd, &readable)) { 546 n--; 547 if (debug) 548 fprintf(stderr, "someone wants %s\n", sep->se_service); 549 if (!sep->se_wait && sep->se_socktype == SOCK_STREAM) { 550 /* XXX here do the libwrap check-before-accept */ 551 ctrl = accept(sep->se_fd, (struct sockaddr *)0, 552 (int *)0); 553 if (debug) 554 fprintf(stderr, "accept, ctrl %d\n", ctrl); 555 if (ctrl < 0) { 556 if (errno != EINTR) 557 syslog(LOG_WARNING, 558 "accept (for %s): %m", 559 sep->se_service); 560 continue; 561 } 562 } else 563 ctrl = sep->se_fd; 564 (void) sigblock(SIGBLOCK); 565 pid = 0; 566#ifdef LIBWRAP_INTERNAL 567 dofork = 1; 568#else 569 dofork = (sep->se_bi == 0 || sep->se_bi->bi_fork); 570#endif 571 if (dofork) { 572 if (sep->se_count++ == 0) 573 (void)gettimeofday(&sep->se_time, 574 (struct timezone *)0); 575 else if (sep->se_count >= sep->se_max) { 576 struct timeval now; 577 578 (void)gettimeofday(&now, (struct timezone *)0); 579 if (now.tv_sec - sep->se_time.tv_sec > 580 CNT_INTVL) { 581 sep->se_time = now; 582 sep->se_count = 1; 583 } else { 584 syslog(LOG_ERR, 585 "%s/%s server failing (looping), service terminated\n", 586 sep->se_service, sep->se_proto); 587 if (!sep->se_wait && sep->se_socktype == SOCK_STREAM) 588 close(ctrl); 589 close_sep(sep); 590 sigsetmask(0L); 591 if (!timingout) { 592 timingout = 1; 593 alarm(RETRYTIME); 594 } 595 continue; 596 } 597 } 598 pid = fork(); 599 if (pid < 0) { 600 syslog(LOG_ERR, "fork: %m"); 601 if (!sep->se_wait && sep->se_socktype == SOCK_STREAM) 602 close(ctrl); 603 sigsetmask(0L); 604 sleep(1); 605 continue; 606 } 607 if (pid != 0 && sep->se_wait) { 608 sep->se_wait = pid; 609 FD_CLR(sep->se_fd, &allsock); 610 nsock--; 611 } 612 if (pid == 0) { 613 sv.sv_mask = 0L; 614 sv.sv_handler = SIG_DFL; 615 sigvec(SIGPIPE, &sv, (struct sigvec *)0); 616 if (debug) 617 setsid(); 618 } 619 } 620 sigsetmask(0L); 621 if (pid == 0) { 622 run_service(ctrl, sep); 623 if (dofork) 624 exit(0); 625 } 626 if (!sep->se_wait && sep->se_socktype == SOCK_STREAM) 627 close(ctrl); 628 } 629 } 630 } 631} 632 633void 634run_service(ctrl, sep) 635 int ctrl; 636 struct servtab *sep; 637{ 638 struct passwd *pwd; 639 struct group *grp = NULL; /* XXX gcc */ 640 char buf[NI_MAXSERV]; 641#ifdef LIBWRAP 642 struct request_info req; 643 int denied; 644 char *service = NULL; /* XXX gcc */ 645#endif 646 647#ifdef LIBWRAP 648#ifndef LIBWRAP_INTERNAL 649 if (sep->se_bi == 0) 650#endif 651 if (!sep->se_wait && sep->se_socktype == SOCK_STREAM) { 652 request_init(&req, RQ_DAEMON, sep->se_argv[0] ? 653 sep->se_argv[0] : sep->se_service, RQ_FILE, ctrl, NULL); 654 fromhost(&req); 655 denied = !hosts_access(&req); 656 if (denied || lflag) { 657 if (getnameinfo(&sep->se_ctrladdr, 658 sep->se_ctrladdr.sa_len, NULL, 0, 659 buf, sizeof(buf), 0) != 0) { 660 /* shouldn't happen */ 661 (void)snprintf(buf, sizeof buf, "%d", 662 ntohs(sep->se_ctrladdr_in.sin_port)); 663 } 664 service = buf; 665 } 666 if (denied) { 667 syslog(deny_severity, 668 "refused connection from %.500s, service %s (%s)", 669 eval_client(&req), service, sep->se_proto); 670 goto reject; 671 } 672 if (lflag) { 673 syslog(allow_severity, 674 "connection from %.500s, service %s (%s)", 675 eval_client(&req), service, sep->se_proto); 676 } 677 } 678#endif /* LIBWRAP */ 679 680 if (sep->se_bi) { 681 (*sep->se_bi->bi_fn)(ctrl, sep); 682 } else { 683 if ((pwd = getpwnam(sep->se_user)) == NULL) { 684 syslog(LOG_ERR, "%s/%s: %s: No such user", 685 sep->se_service, sep->se_proto, sep->se_user); 686 goto reject; 687 } 688 if (sep->se_group && 689 (grp = getgrnam(sep->se_group)) == NULL) { 690 syslog(LOG_ERR, "%s/%s: %s: No such group", 691 sep->se_service, sep->se_proto, sep->se_group); 692 goto reject; 693 } 694 if (pwd->pw_uid) { 695 if (sep->se_group) 696 pwd->pw_gid = grp->gr_gid; 697 if (setgid(pwd->pw_gid) < 0) { 698 syslog(LOG_ERR, 699 "%s/%s: can't set gid %d: %m", sep->se_service, 700 sep->se_proto, pwd->pw_gid); 701 goto reject; 702 } 703 (void) initgroups(pwd->pw_name, 704 pwd->pw_gid); 705 if (setuid(pwd->pw_uid) < 0) { 706 syslog(LOG_ERR, 707 "%s/%s: can't set uid %d: %m", sep->se_service, 708 sep->se_proto, pwd->pw_uid); 709 goto reject; 710 } 711 } else if (sep->se_group) { 712 (void) setgid((gid_t)grp->gr_gid); 713 } 714 if (debug) 715 fprintf(stderr, "%d execl %s\n", 716 getpid(), sep->se_server); 717#ifdef MULOG 718 if (sep->se_log) 719 dolog(sep, ctrl); 720#endif 721 /* Set our control descriptor to not close-on-exec... */ 722 if (fcntl(ctrl, F_SETFD, 0) < 0) 723 syslog(LOG_ERR, "fcntl (F_SETFD, 0): %m"); 724 /* ...and dup it to stdin, stdout, and stderr. */ 725 if (ctrl != 0) { 726 dup2(ctrl, 0); 727 close(ctrl); 728 ctrl = 0; 729 } 730 dup2(0, 1); 731 dup2(0, 2); 732#ifdef RLIMIT_NOFILE 733 if (rlim_ofile.rlim_cur != rlim_ofile_cur && 734 setrlimit(RLIMIT_NOFILE, &rlim_ofile) < 0) 735 syslog(LOG_ERR, "setrlimit: %m"); 736#endif 737 execv(sep->se_server, sep->se_argv); 738 syslog(LOG_ERR, "cannot execute %s: %m", sep->se_server); 739 reject: 740 if (sep->se_socktype != SOCK_STREAM) 741 recv(ctrl, buf, sizeof (buf), 0); 742 _exit(1); 743 } 744} 745 746void 747reapchild(signo) 748 int signo; 749{ 750 int status; 751 pid_t pid; 752 struct servtab *sep; 753 754 for (;;) { 755 pid = wait3(&status, WNOHANG, (struct rusage *)0); 756 if (pid <= 0) 757 break; 758 if (debug) 759 fprintf(stderr, "%d reaped, status %#x\n", 760 pid, status); 761 for (sep = servtab; sep; sep = sep->se_next) 762 if (sep->se_wait == pid) { 763 if (WIFEXITED(status) && WEXITSTATUS(status)) 764 syslog(LOG_WARNING, 765 "%s: exit status 0x%x", 766 sep->se_server, WEXITSTATUS(status)); 767 else if (WIFSIGNALED(status)) 768 syslog(LOG_WARNING, 769 "%s: exit signal 0x%x", 770 sep->se_server, WTERMSIG(status)); 771 sep->se_wait = 1; 772 FD_SET(sep->se_fd, &allsock); 773 nsock++; 774 if (debug) 775 fprintf(stderr, "restored %s, fd %d\n", 776 sep->se_service, sep->se_fd); 777 } 778 } 779} 780 781void 782config(signo) 783 int signo; 784{ 785 struct servtab *sep, *cp, **sepp; 786 long omask; 787 int n; 788 789 if (!setconfig()) { 790 syslog(LOG_ERR, "%s: %m", CONFIG); 791 return; 792 } 793 for (sep = servtab; sep; sep = sep->se_next) 794 sep->se_checked = 0; 795 while ((cp = getconfigent())) { 796 for (sep = servtab; sep; sep = sep->se_next) 797 if (strcmp(sep->se_service, cp->se_service) == 0 && 798 strcmp(sep->se_hostaddr, cp->se_hostaddr) == 0 && 799 strcmp(sep->se_proto, cp->se_proto) == 0 && 800 ISMUX(sep) == ISMUX(cp)) 801 break; 802 if (sep != 0) { 803 int i; 804 805#define SWAP(type, a, b) {type c=(type)a; (type)a=(type)b; (type)b=(type)c;} 806 807 omask = sigblock(SIGBLOCK); 808 /* 809 * sep->se_wait may be holding the pid of a daemon 810 * that we're waiting for. If so, don't overwrite 811 * it unless the config file explicitly says don't 812 * wait. 813 */ 814 if (cp->se_bi == 0 && 815 (sep->se_wait == 1 || cp->se_wait == 0)) 816 sep->se_wait = cp->se_wait; 817 SWAP(char *, sep->se_user, cp->se_user); 818 SWAP(char *, sep->se_group, cp->se_group); 819 SWAP(char *, sep->se_server, cp->se_server); 820 for (i = 0; i < MAXARGV; i++) 821 SWAP(char *, sep->se_argv[i], cp->se_argv[i]); 822#ifdef IPSEC 823 SWAP(char *, sep->se_policy, cp->se_policy); 824#endif 825 SWAP(int, cp->se_type, sep->se_type); 826 SWAP(int, cp->se_max, sep->se_max); 827#undef SWAP 828 if (isrpcservice(sep)) 829 unregister_rpc(sep); 830 sep->se_rpcversl = cp->se_rpcversl; 831 sep->se_rpcversh = cp->se_rpcversh; 832 sigsetmask(omask); 833 freeconfig(cp); 834 if (debug) 835 print_service("REDO", sep); 836 } else { 837 sep = enter(cp); 838 if (debug) 839 print_service("ADD ", sep); 840 } 841 sep->se_checked = 1; 842 843 switch (sep->se_family) { 844 case AF_LOCAL: 845 if (sep->se_fd != -1) 846 break; 847 n = strlen(sep->se_service); 848 if (n > sizeof(sep->se_ctrladdr_un.sun_path)) { 849 syslog(LOG_ERR, "%s: address too long", 850 sep->se_service); 851 sep->se_checked = 0; 852 continue; 853 } 854 (void)unlink(sep->se_service); 855 strncpy(sep->se_ctrladdr_un.sun_path, 856 sep->se_service, n); 857 sep->se_ctrladdr_un.sun_family = AF_LOCAL; 858 sep->se_ctrladdr_size = n + 859 sizeof(sep->se_ctrladdr_un) - 860 sizeof(sep->se_ctrladdr_un.sun_path); 861 if (!ISMUX(sep)) 862 setup(sep); 863 break; 864 case AF_INET: 865#ifdef INET6 866 case AF_INET6: 867#endif 868 { 869 struct addrinfo hints, *res; 870 char *host, *port; 871 int error; 872 int s; 873 874 /* check if the family is supported */ 875 s = socket(sep->se_family, SOCK_DGRAM, 0); 876 if (s < 0) { 877 syslog(LOG_WARNING, 878"%s/%s: %s: the address family is not supported by the kernel", 879 sep->se_service, sep->se_proto, 880 sep->se_hostaddr); 881 sep->se_checked = 0; 882 continue; 883 } 884 close(s); 885 886 memset(&hints, 0, sizeof(hints)); 887 hints.ai_family = sep->se_family; 888 hints.ai_socktype = sep->se_socktype; 889 hints.ai_flags = AI_PASSIVE; 890 if (!strcmp(sep->se_hostaddr, "*")) 891 host = NULL; 892 else 893 host = sep->se_hostaddr; 894 if (isrpcservice(sep) || ISMUX(sep)) 895 port = "0"; 896 else 897 port = sep->se_service; 898 error = getaddrinfo(host, port, &hints, &res); 899 if (error) { 900 if (error == EAI_SERVICE) { 901 /* gai_strerror not friendly enough */ 902 syslog(LOG_WARNING, "%s/%s: " 903 "unknown service", 904 sep->se_service, sep->se_proto); 905 } else { 906 syslog(LOG_ERR, "%s/%s: %s: %s", 907 sep->se_service, sep->se_proto, 908 sep->se_hostaddr, 909 gai_strerror(error)); 910 } 911 sep->se_checked = 0; 912 continue; 913 } 914 if (res->ai_next) { 915 syslog(LOG_ERR, 916 "%s/%s: %s: resolved to multiple addr", 917 sep->se_service, sep->se_proto, 918 sep->se_hostaddr); 919 sep->se_checked = 0; 920 freeaddrinfo(res); 921 continue; 922 } 923 memcpy(&sep->se_ctrladdr, res->ai_addr, 924 res->ai_addrlen); 925 if (ISMUX(sep)) { 926 sep->se_fd = -1; 927 freeaddrinfo(res); 928 continue; 929 } 930 sep->se_ctrladdr_size = res->ai_addrlen; 931 freeaddrinfo(res); 932#ifdef RPC 933 if (isrpcservice(sep)) { 934 struct rpcent *rp; 935 936 sep->se_rpcprog = atoi(sep->se_service); 937 if (sep->se_rpcprog == 0) { 938 rp = getrpcbyname(sep->se_service); 939 if (rp == 0) { 940 syslog(LOG_ERR, 941 "%s/%s: unknown service", 942 sep->se_service, 943 sep->se_proto); 944 sep->se_checked = 0; 945 continue; 946 } 947 sep->se_rpcprog = rp->r_number; 948 } 949 if (sep->se_fd == -1 && !ISMUX(sep)) 950 setup(sep); 951 if (sep->se_fd != -1) 952 register_rpc(sep); 953 } else 954#endif 955 { 956 if (sep->se_fd >= 0) 957 close_sep(sep); 958 if (sep->se_fd == -1 && !ISMUX(sep)) 959 setup(sep); 960 } 961 } 962 } 963 } 964 endconfig(); 965 /* 966 * Purge anything not looked at above. 967 */ 968 omask = sigblock(SIGBLOCK); 969 sepp = &servtab; 970 while ((sep = *sepp)) { 971 if (sep->se_checked) { 972 sepp = &sep->se_next; 973 continue; 974 } 975 *sepp = sep->se_next; 976 if (sep->se_fd >= 0) 977 close_sep(sep); 978 if (isrpcservice(sep)) 979 unregister_rpc(sep); 980 if (sep->se_family == AF_LOCAL) 981 (void)unlink(sep->se_service); 982 if (debug) 983 print_service("FREE", sep); 984 freeconfig(sep); 985 free((char *)sep); 986 } 987 (void) sigsetmask(omask); 988} 989 990void 991retry(signo) 992 int signo; 993{ 994 struct servtab *sep; 995 996 timingout = 0; 997 for (sep = servtab; sep; sep = sep->se_next) { 998 if (sep->se_fd == -1 && !ISMUX(sep)) { 999 switch (sep->se_family) { 1000 case AF_LOCAL: 1001 case AF_INET: 1002#ifdef INET6 1003 case AF_INET6: 1004#endif 1005 setup(sep); 1006 if (sep->se_fd != -1 && isrpcservice(sep)) 1007 register_rpc(sep); 1008 break; 1009 } 1010 } 1011 } 1012} 1013 1014void 1015goaway(signo) 1016 int signo; 1017{ 1018 struct servtab *sep; 1019 1020 for (sep = servtab; sep; sep = sep->se_next) { 1021 if (sep->se_fd == -1) 1022 continue; 1023 1024 switch (sep->se_family) { 1025 case AF_LOCAL: 1026 (void)unlink(sep->se_service); 1027 break; 1028 case AF_INET: 1029#ifdef INET6 1030 case AF_INET6: 1031#endif 1032 if (sep->se_wait == 1 && isrpcservice(sep)) 1033 unregister_rpc(sep); 1034 break; 1035 } 1036 (void)close(sep->se_fd); 1037 } 1038 exit(0); 1039} 1040 1041void 1042setup(sep) 1043 struct servtab *sep; 1044{ 1045 int on = 1; 1046 1047 if ((sep->se_fd = socket(sep->se_family, sep->se_socktype, 0)) < 0) { 1048 if (debug) 1049 fprintf(stderr, "socket failed on %s/%s: %s\n", 1050 sep->se_service, sep->se_proto, strerror(errno)); 1051 syslog(LOG_ERR, "%s/%s: socket: %m", 1052 sep->se_service, sep->se_proto); 1053 return; 1054 } 1055 /* Set all listening sockets to close-on-exec. */ 1056 if (fcntl(sep->se_fd, F_SETFD, FD_CLOEXEC) < 0) 1057 syslog(LOG_ERR, "fcntl (F_SETFD, FD_CLOEXEC): %m"); 1058 1059#define turnon(fd, opt) \ 1060setsockopt(fd, SOL_SOCKET, opt, (char *)&on, sizeof (on)) 1061 if (strcmp(sep->se_proto, "tcp") == 0 && (options & SO_DEBUG) && 1062 turnon(sep->se_fd, SO_DEBUG) < 0) 1063 syslog(LOG_ERR, "setsockopt (SO_DEBUG): %m"); 1064 if (turnon(sep->se_fd, SO_REUSEADDR) < 0) 1065 syslog(LOG_ERR, "setsockopt (SO_REUSEADDR): %m"); 1066#undef turnon 1067 1068 /* Set the socket buffer sizes, if specified. */ 1069 if (sep->se_sndbuf != 0 && setsockopt(sep->se_fd, SOL_SOCKET, 1070 SO_SNDBUF, (char *)&sep->se_sndbuf, sizeof(sep->se_sndbuf)) < 0) 1071 syslog(LOG_ERR, "setsockopt (SO_SNDBUF %d): %m", 1072 sep->se_sndbuf); 1073 if (sep->se_rcvbuf != 0 && setsockopt(sep->se_fd, SOL_SOCKET, 1074 SO_RCVBUF, (char *)&sep->se_rcvbuf, sizeof(sep->se_rcvbuf)) < 0) 1075 syslog(LOG_ERR, "setsockopt (SO_RCVBUF %d): %m", 1076 sep->se_rcvbuf); 1077 if (sep->se_type == FAITH_TYPE && setsockopt(sep->se_fd, IPPROTO_IPV6, 1078 IPV6_FAITH, (char *)&on, sizeof(on)) < 0) 1079 syslog(LOG_ERR, "setsockopt (IPV6_FAITH): %m"); 1080#ifdef IPSEC 1081 if (ipsecsetup(sep->se_family, sep->se_fd, sep->se_policy) < 0 && 1082 sep->se_policy) { 1083 syslog(LOG_ERR, "%s/%s: ipsec setup failed", 1084 sep->se_service, sep->se_proto); 1085 (void)close(sep->se_fd); 1086 sep->se_fd = -1; 1087 return; 1088 } 1089#endif 1090 1091 if (bind(sep->se_fd, &sep->se_ctrladdr, sep->se_ctrladdr_size) < 0) { 1092 if (debug) 1093 fprintf(stderr, "bind failed on %s/%s: %s\n", 1094 sep->se_service, sep->se_proto, strerror(errno)); 1095 syslog(LOG_ERR, "%s/%s: bind: %m", 1096 sep->se_service, sep->se_proto); 1097 (void) close(sep->se_fd); 1098 sep->se_fd = -1; 1099 if (!timingout) { 1100 timingout = 1; 1101 alarm(RETRYTIME); 1102 } 1103 return; 1104 } 1105 if (sep->se_socktype == SOCK_STREAM) 1106 listen(sep->se_fd, 10); 1107 1108 FD_SET(sep->se_fd, &allsock); 1109 nsock++; 1110 if (sep->se_fd > maxsock) { 1111 maxsock = sep->se_fd; 1112 if (maxsock > rlim_ofile_cur - FD_MARGIN) 1113 bump_nofile(); 1114 } 1115 if (debug) 1116 fprintf(stderr, "registered %s on %d\n", 1117 sep->se_server, sep->se_fd); 1118} 1119 1120/* 1121 * Finish with a service and its socket. 1122 */ 1123void 1124close_sep(sep) 1125 struct servtab *sep; 1126{ 1127 if (sep->se_fd >= 0) { 1128 nsock--; 1129 FD_CLR(sep->se_fd, &allsock); 1130 (void) close(sep->se_fd); 1131 sep->se_fd = -1; 1132 } 1133 sep->se_count = 0; 1134 /* 1135 * Don't keep the pid of this running daemon: when reapchild() 1136 * reaps this pid, it would erroneously increment nsock. 1137 */ 1138 if (sep->se_wait > 1) 1139 sep->se_wait = 1; 1140} 1141 1142void 1143register_rpc(sep) 1144 struct servtab *sep; 1145{ 1146#ifdef RPC 1147 int n; 1148 struct netbuf nbuf; 1149 struct sockaddr_storage ss; 1150 struct netconfig *nconf; 1151 1152 if ((nconf = getnetconfigent(sep->se_proto+4)) == NULL) { 1153 syslog(LOG_ERR, "%s: getnetconfigent failed", 1154 sep->se_proto); 1155 return; 1156 } 1157 n = sizeof ss; 1158 if (getsockname(sep->se_fd, (struct sockaddr *)&ss, &n) < 0) { 1159 syslog(LOG_ERR, "%s/%s: getsockname: %m", 1160 sep->se_service, sep->se_proto); 1161 return; 1162 } 1163 1164 nbuf.buf = &ss; 1165 nbuf.len = ss.ss_len; 1166 nbuf.maxlen = sizeof (struct sockaddr_storage); 1167 for (n = sep->se_rpcversl; n <= sep->se_rpcversh; n++) { 1168 if (debug) 1169 fprintf(stderr, "rpcb_set: %u %d %s %s\n", 1170 sep->se_rpcprog, n, nconf->nc_netid, 1171 taddr2uaddr(nconf, &nbuf)); 1172 (void)rpcb_unset(sep->se_rpcprog, n, nconf); 1173 if (!rpcb_set(sep->se_rpcprog, n, nconf, &nbuf)) 1174 syslog(LOG_ERR, "rpcb_set: %u %d %s %s%s", 1175 sep->se_rpcprog, n, nconf->nc_netid, 1176 taddr2uaddr(nconf, &nbuf), clnt_spcreateerror("")); 1177 } 1178#endif /* RPC */ 1179} 1180 1181void 1182unregister_rpc(sep) 1183 struct servtab *sep; 1184{ 1185#ifdef RPC 1186 int n; 1187 struct netconfig *nconf; 1188 1189 if ((nconf = getnetconfigent(sep->se_proto+4)) == NULL) { 1190 syslog(LOG_ERR, "%s: getnetconfigent failed", 1191 sep->se_proto); 1192 return; 1193 } 1194 1195 for (n = sep->se_rpcversl; n <= sep->se_rpcversh; n++) { 1196 if (debug) 1197 fprintf(stderr, "rpcb_unset(%u, %d, %s)\n", 1198 sep->se_rpcprog, n, nconf->nc_netid); 1199 if (!rpcb_unset(sep->se_rpcprog, n, nconf)) 1200 syslog(LOG_ERR, "rpcb_unset(%u, %d, %s) failed\n", 1201 sep->se_rpcprog, n, nconf->nc_netid); 1202 } 1203#endif /* RPC */ 1204} 1205 1206 1207struct servtab * 1208enter(cp) 1209 struct servtab *cp; 1210{ 1211 struct servtab *sep; 1212 long omask; 1213 1214 sep = (struct servtab *)malloc(sizeof (*sep)); 1215 if (sep == (struct servtab *)0) { 1216 syslog(LOG_ERR, "Out of memory."); 1217 exit(1); 1218 } 1219 *sep = *cp; 1220 sep->se_fd = -1; 1221 sep->se_rpcprog = -1; 1222 omask = sigblock(SIGBLOCK); 1223 sep->se_next = servtab; 1224 servtab = sep; 1225 sigsetmask(omask); 1226 return (sep); 1227} 1228 1229FILE *fconfig = NULL; 1230struct servtab serv; 1231char line[LINE_MAX]; 1232char *defhost; 1233#ifdef IPSEC 1234static char *policy = NULL; 1235#endif 1236 1237int 1238setconfig() 1239{ 1240 if (defhost) 1241 free(defhost); 1242 defhost = newstr("*"); 1243#ifdef IPSEC 1244 if (policy) 1245 free(policy); 1246 policy = NULL; 1247#endif 1248 if (fconfig != NULL) { 1249 fseek(fconfig, 0L, SEEK_SET); 1250 return (1); 1251 } 1252 fconfig = fopen(CONFIG, "r"); 1253 return (fconfig != NULL); 1254} 1255 1256void 1257endconfig() 1258{ 1259 if (fconfig) { 1260 (void) fclose(fconfig); 1261 fconfig = NULL; 1262 } 1263 if (defhost) { 1264 free(defhost); 1265 defhost = 0; 1266 } 1267} 1268 1269struct servtab * 1270getconfigent() 1271{ 1272 struct servtab *sep = &serv; 1273 int argc, val; 1274 char *cp, *cp0, *arg, *buf0, *buf1, *sz0, *sz1; 1275 static char TCPMUX_TOKEN[] = "tcpmux/"; 1276#define MUX_LEN (sizeof(TCPMUX_TOKEN)-1) 1277 char *hostdelim; 1278 1279more: 1280 while ((cp = nextline(fconfig))) { 1281#ifdef IPSEC 1282 /* lines starting with #@ is not a comment, but the policy */ 1283 if (cp[0] == '#' && cp[1] == '@') { 1284 char *p; 1285 for (p = cp + 2; p && *p && isspace(*p); p++) 1286 ; 1287 if (*p == '\0') { 1288 if (policy) 1289 free(policy); 1290 policy = NULL; 1291 } else { 1292 if (ipsecsetup_test(p) < 0) { 1293 syslog(LOG_ERR, 1294 "%s: invalid ipsec policy \"%s\"", 1295 CONFIG, p); 1296 exit(1); 1297 } else { 1298 if (policy) 1299 free(policy); 1300 policy = newstr(p); 1301 } 1302 } 1303 } 1304#endif 1305 if (*cp == '#' || *cp == '\0') 1306 continue; 1307#ifdef MULOG 1308 /* Avoid use of `skip' if there is a danger of it looking 1309 * at continuation lines. 1310 */ 1311 do { 1312 cp++; 1313 } while (*cp == ' ' || *cp == '\t'); 1314 if (*cp == '\0') 1315 continue; 1316 if ((arg = skip(&cp)) == NULL) 1317 continue; 1318 if (strcmp(arg, "DOMAIN")) 1319 continue; 1320 if (curdom) 1321 free(curdom); 1322 curdom = NULL; 1323 while (*cp == ' ' || *cp == '\t') 1324 cp++; 1325 if (*cp == '\0') 1326 continue; 1327 arg = cp; 1328 while (*cp && *cp != ' ' && *cp != '\t') 1329 cp++; 1330 if (*cp != '\0') 1331 *cp++ = '\0'; 1332 curdom = newstr(arg); 1333#endif 1334 break; 1335 } 1336 if (cp == NULL) 1337 return ((struct servtab *)0); 1338 /* 1339 * clear the static buffer, since some fields (se_ctrladdr, 1340 * for example) don't get initialized here. 1341 */ 1342 memset((caddr_t)sep, 0, sizeof *sep); 1343 arg = skip(&cp); 1344 if (cp == NULL) { 1345 /* got an empty line containing just blanks/tabs. */ 1346 goto more; 1347 } 1348 /* Check for a host name. */ 1349 hostdelim = strrchr(arg, ':'); 1350 if (hostdelim) { 1351 *hostdelim = '\0'; 1352 if (arg[0] == '[' && hostdelim > arg && hostdelim[-1] == ']') { 1353 hostdelim[-1] = '\0'; 1354 sep->se_hostaddr = newstr(arg + 1); 1355 } else 1356 sep->se_hostaddr = newstr(arg); 1357 arg = hostdelim + 1; 1358 /* 1359 * If the line is of the form `host:', then just change the 1360 * default host for the following lines. 1361 */ 1362 if (*arg == '\0') { 1363 arg = skip(&cp); 1364 if (cp == NULL) { 1365 free(defhost); 1366 defhost = sep->se_hostaddr; 1367 goto more; 1368 } 1369 } 1370 } else 1371 sep->se_hostaddr = newstr(defhost); 1372 if (strncmp(arg, TCPMUX_TOKEN, MUX_LEN) == 0) { 1373 char *c = arg + MUX_LEN; 1374 if (*c == '+') { 1375 sep->se_type = MUXPLUS_TYPE; 1376 c++; 1377 } else 1378 sep->se_type = MUX_TYPE; 1379 sep->se_service = newstr(c); 1380 } else { 1381 sep->se_service = newstr(arg); 1382 sep->se_type = NORM_TYPE; 1383 } 1384 1385 arg = sskip(&cp); 1386 if (strcmp(arg, "stream") == 0) 1387 sep->se_socktype = SOCK_STREAM; 1388 else if (strcmp(arg, "dgram") == 0) 1389 sep->se_socktype = SOCK_DGRAM; 1390 else if (strcmp(arg, "rdm") == 0) 1391 sep->se_socktype = SOCK_RDM; 1392 else if (strcmp(arg, "seqpacket") == 0) 1393 sep->se_socktype = SOCK_SEQPACKET; 1394 else if (strcmp(arg, "raw") == 0) 1395 sep->se_socktype = SOCK_RAW; 1396 else 1397 sep->se_socktype = -1; 1398 1399 arg = sskip(&cp); 1400 if (sep->se_type == NORM_TYPE && 1401 strncmp(arg, "faith/", strlen("faith/")) == 0) { 1402 arg += strlen("faith/"); 1403 sep->se_type = FAITH_TYPE; 1404 } 1405 sep->se_proto = newstr(arg); 1406 1407#define MALFORMED(arg) \ 1408do { \ 1409 syslog(LOG_ERR, "%s: malformed buffer size option `%s'", \ 1410 sep->se_service, (arg)); \ 1411 goto more; \ 1412} while (0) 1413 1414#define GETVAL(arg) \ 1415do { \ 1416 if (!isdigit(*(arg))) \ 1417 MALFORMED(arg); \ 1418 val = strtol((arg), &cp0, 10); \ 1419 if (cp0 != NULL) { \ 1420 if (cp0[1] != '\0') \ 1421 MALFORMED((arg)); \ 1422 if (cp0[0] == 'k') \ 1423 val *= 1024; \ 1424 if (cp0[0] == 'm') \ 1425 val *= 1024 * 1024; \ 1426 } \ 1427 if (val < 1) { \ 1428 syslog(LOG_ERR, "%s: invalid buffer size `%s'", \ 1429 sep->se_service, (arg)); \ 1430 goto more; \ 1431 } \ 1432} while (0) 1433 1434#define ASSIGN(arg) \ 1435do { \ 1436 if (strcmp((arg), "sndbuf") == 0) \ 1437 sep->se_sndbuf = val; \ 1438 else if (strcmp((arg), "rcvbuf") == 0) \ 1439 sep->se_rcvbuf = val; \ 1440 else \ 1441 MALFORMED((arg)); \ 1442} while (0) 1443 1444 /* 1445 * Extract the send and receive buffer sizes before parsing 1446 * the protocol. 1447 */ 1448 sep->se_sndbuf = sep->se_rcvbuf = 0; 1449 buf0 = buf1 = sz0 = sz1 = NULL; 1450 if ((buf0 = strchr(sep->se_proto, ',')) != NULL) { 1451 /* Not meaningful for Tcpmux services. */ 1452 if (ISMUX(sep)) { 1453 syslog(LOG_ERR, "%s: can't specify buffer sizes for " 1454 "tcpmux services", sep->se_service); 1455 goto more; 1456 } 1457 1458 /* Skip the , */ 1459 *buf0++ = '\0'; 1460 1461 /* Check to see if another socket buffer size was specified. */ 1462 if ((buf1 = strchr(buf0, ',')) != NULL) { 1463 /* Skip the , */ 1464 *buf1++ = '\0'; 1465 1466 /* Make sure a 3rd one wasn't specified. */ 1467 if (strchr(buf1, ',') != NULL) { 1468 syslog(LOG_ERR, "%s: too many buffer sizes", 1469 sep->se_service); 1470 goto more; 1471 } 1472 1473 /* Locate the size. */ 1474 if ((sz1 = strchr(buf1, '=')) == NULL) 1475 MALFORMED(buf1); 1476 1477 /* Skip the = */ 1478 *sz1++ = '\0'; 1479 } 1480 1481 /* Locate the size. */ 1482 if ((sz0 = strchr(buf0, '=')) == NULL) 1483 MALFORMED(buf0); 1484 1485 /* Skip the = */ 1486 *sz0++ = '\0'; 1487 1488 GETVAL(sz0); 1489 ASSIGN(buf0); 1490 1491 if (buf1 != NULL) { 1492 GETVAL(sz1); 1493 ASSIGN(buf1); 1494 } 1495 } 1496 1497#undef ASSIGN 1498#undef GETVAL 1499#undef MALFORMED 1500 1501 if (strcmp(sep->se_proto, "unix") == 0) { 1502 sep->se_family = AF_LOCAL; 1503 } else { 1504 val = strlen(sep->se_proto); 1505 if (!val) { 1506 syslog(LOG_ERR, "%s: invalid protocol specified", 1507 sep->se_service); 1508 goto more; 1509 } 1510 val = sep->se_proto[val - 1]; 1511 switch (val) { 1512 case '4': /*tcp4 or udp4*/ 1513 sep->se_family = AF_INET; 1514 break; 1515#ifdef INET6 1516 case '6': /*tcp6 or udp6*/ 1517 sep->se_family = AF_INET6; 1518 break; 1519#endif 1520 default: 1521 sep->se_family = AF_INET; /*will become AF_INET6*/ 1522 break; 1523 } 1524 if (strncmp(sep->se_proto, "rpc/", 4) == 0) { 1525#ifdef RPC 1526 char *cp, *ccp; 1527 cp = strchr(sep->se_service, '/'); 1528 if (cp == 0) { 1529 syslog(LOG_ERR, "%s: no rpc version", 1530 sep->se_service); 1531 goto more; 1532 } 1533 *cp++ = '\0'; 1534 sep->se_rpcversl = sep->se_rpcversh = 1535 strtol(cp, &ccp, 0); 1536 if (ccp == cp) { 1537 badafterall: 1538 syslog(LOG_ERR, "%s/%s: bad rpc version", 1539 sep->se_service, cp); 1540 goto more; 1541 } 1542 if (*ccp == '-') { 1543 cp = ccp + 1; 1544 sep->se_rpcversh = strtol(cp, &ccp, 0); 1545 if (ccp == cp) 1546 goto badafterall; 1547 } 1548#else 1549 syslog(LOG_ERR, "%s: rpc services not suported", 1550 sep->se_service); 1551 goto more; 1552#endif /* RPC */ 1553 } 1554 } 1555 arg = sskip(&cp); 1556 { 1557 char *cp; 1558 if ((cp = strchr(arg, ':')) == NULL) 1559 cp = strchr(arg, '.'); 1560 if (cp != NULL) { 1561 *cp++ = '\0'; 1562 sep->se_max = atoi(cp); 1563 } else 1564 sep->se_max = TOOMANY; 1565 } 1566 sep->se_wait = strcmp(arg, "wait") == 0; 1567 if (ISMUX(sep)) { 1568 /* 1569 * Silently enforce "nowait" for TCPMUX services since 1570 * they don't have an assigned port to listen on. 1571 */ 1572 sep->se_wait = 0; 1573 1574 if (strncmp(sep->se_proto, "tcp", 3)) { 1575 syslog(LOG_ERR, 1576 "%s: bad protocol for tcpmux service %s", 1577 CONFIG, sep->se_service); 1578 goto more; 1579 } 1580 if (sep->se_socktype != SOCK_STREAM) { 1581 syslog(LOG_ERR, 1582 "%s: bad socket type for tcpmux service %s", 1583 CONFIG, sep->se_service); 1584 goto more; 1585 } 1586 } 1587 sep->se_user = newstr(sskip(&cp)); 1588 if ((sep->se_group = strchr(sep->se_user, ':')) != NULL) 1589 *sep->se_group++ = '\0'; 1590 else if ((sep->se_group = strchr(sep->se_user, '.')) != NULL) 1591 *sep->se_group++ = '\0'; 1592 1593 sep->se_server = newstr(sskip(&cp)); 1594 if (strcmp(sep->se_server, "internal") == 0) { 1595 struct biltin *bi; 1596 1597 for (bi = biltins; bi->bi_service; bi++) 1598 if (bi->bi_socktype == sep->se_socktype && 1599 strcmp(bi->bi_service, sep->se_service) == 0) 1600 break; 1601 if (bi->bi_service == 0) { 1602 syslog(LOG_ERR, "internal service %s unknown", 1603 sep->se_service); 1604 goto more; 1605 } 1606 sep->se_bi = bi; 1607 sep->se_wait = bi->bi_wait; 1608 } else 1609 sep->se_bi = NULL; 1610 argc = 0; 1611 for (arg = skip(&cp); cp; arg = skip(&cp)) { 1612#if MULOG 1613 char *colon; 1614 1615 if (argc == 0 && (colon = strrchr(arg, ':'))) { 1616 while (arg < colon) { 1617 int x; 1618 char *ccp; 1619 1620 switch (*arg++) { 1621 case 'l': 1622 x = 1; 1623 if (isdigit(*arg)) { 1624 x = strtol(arg, &ccp, 0); 1625 if (ccp == arg) 1626 break; 1627 arg = ccp; 1628 } 1629 sep->se_log &= ~MULOG_RFC931; 1630 sep->se_log |= x; 1631 break; 1632 case 'a': 1633 sep->se_log |= MULOG_RFC931; 1634 break; 1635 default: 1636 break; 1637 } 1638 } 1639 arg = colon + 1; 1640 } 1641#endif 1642 if (argc < MAXARGV) 1643 sep->se_argv[argc++] = newstr(arg); 1644 } 1645 while (argc <= MAXARGV) 1646 sep->se_argv[argc++] = NULL; 1647#ifdef IPSEC 1648 sep->se_policy = policy ? newstr(policy) : NULL; 1649#endif 1650 return (sep); 1651} 1652 1653void 1654freeconfig(cp) 1655 struct servtab *cp; 1656{ 1657 int i; 1658 1659 if (cp->se_hostaddr) 1660 free(cp->se_hostaddr); 1661 if (cp->se_service) 1662 free(cp->se_service); 1663 if (cp->se_proto) 1664 free(cp->se_proto); 1665 if (cp->se_user) 1666 free(cp->se_user); 1667 /* Note: se_group is part of the newstr'ed se_user */ 1668 if (cp->se_server) 1669 free(cp->se_server); 1670 for (i = 0; i < MAXARGV; i++) 1671 if (cp->se_argv[i]) 1672 free(cp->se_argv[i]); 1673#ifdef IPSEC 1674 if (cp->se_policy) 1675 free(cp->se_policy); 1676#endif 1677} 1678 1679 1680/* 1681 * Safe skip - if skip returns null, log a syntax error in the 1682 * configuration file and exit. 1683 */ 1684char * 1685sskip(cpp) 1686 char **cpp; 1687{ 1688 char *cp; 1689 1690 cp = skip(cpp); 1691 if (cp == NULL) { 1692 syslog(LOG_ERR, "%s: syntax error", CONFIG); 1693 exit(1); 1694 } 1695 return (cp); 1696} 1697 1698char * 1699skip(cpp) 1700 char **cpp; 1701{ 1702 char *cp = *cpp; 1703 char *start; 1704 1705 if (*cpp == NULL) 1706 return ((char *)0); 1707 1708again: 1709 while (*cp == ' ' || *cp == '\t') 1710 cp++; 1711 if (*cp == '\0') { 1712 int c; 1713 1714 c = getc(fconfig); 1715 (void) ungetc(c, fconfig); 1716 if (c == ' ' || c == '\t') 1717 if ((cp = nextline(fconfig))) 1718 goto again; 1719 *cpp = (char *)0; 1720 return ((char *)0); 1721 } 1722 start = cp; 1723 while (*cp && *cp != ' ' && *cp != '\t') 1724 cp++; 1725 if (*cp != '\0') 1726 *cp++ = '\0'; 1727 *cpp = cp; 1728 return (start); 1729} 1730 1731char * 1732nextline(fd) 1733 FILE *fd; 1734{ 1735 char *cp; 1736 1737 if (fgets(line, sizeof (line), fd) == NULL) 1738 return ((char *)0); 1739 cp = strchr(line, '\n'); 1740 if (cp) 1741 *cp = '\0'; 1742 return (line); 1743} 1744 1745char * 1746newstr(cp) 1747 char *cp; 1748{ 1749 if ((cp = strdup(cp ? cp : ""))) 1750 return (cp); 1751 syslog(LOG_ERR, "strdup: %m"); 1752 exit(1); 1753} 1754 1755void 1756inetd_setproctitle(a, s) 1757 char *a; 1758 int s; 1759{ 1760 int size; 1761 char *cp; 1762 struct sockaddr_storage ss; 1763 char buf[80]; 1764 char hbuf[NI_MAXHOST]; 1765 1766 cp = Argv[0]; 1767 size = sizeof(ss); 1768 if (getpeername(s, (struct sockaddr *)&ss, &size) == 0) { 1769 if (getnameinfo((struct sockaddr *)&ss, ss.ss_len, 1770 hbuf, sizeof(hbuf), NULL, 0, niflags) != 0) 1771 strcpy(hbuf, "?"); 1772 (void)snprintf(buf, sizeof buf, "-%s [%s]", a, hbuf); 1773 } else 1774 (void)snprintf(buf, sizeof buf, "-%s", a); 1775 strncpy(cp, buf, LastArg - cp); 1776 cp += strlen(cp); 1777 while (cp < LastArg) 1778 *cp++ = ' '; 1779} 1780 1781void 1782bump_nofile() 1783{ 1784#ifdef RLIMIT_NOFILE 1785 1786#define FD_CHUNK 32 1787 1788 struct rlimit rl; 1789 1790 if (getrlimit(RLIMIT_NOFILE, &rl) < 0) { 1791 syslog(LOG_ERR, "getrlimit: %m"); 1792 return; 1793 } 1794 rl.rlim_cur = MIN(rl.rlim_max, rl.rlim_cur + FD_CHUNK); 1795 if (rl.rlim_cur <= rlim_ofile_cur) { 1796 syslog(LOG_ERR, 1797 "bump_nofile: cannot extend file limit, max = %d", 1798 (int)rl.rlim_cur); 1799 return; 1800 } 1801 1802 if (setrlimit(RLIMIT_NOFILE, &rl) < 0) { 1803 syslog(LOG_ERR, "setrlimit: %m"); 1804 return; 1805 } 1806 1807 rlim_ofile_cur = rl.rlim_cur; 1808 return; 1809 1810#else 1811 syslog(LOG_ERR, "bump_nofile: cannot extend file limit"); 1812 return; 1813#endif 1814} 1815 1816/* 1817 * Internet services provided internally by inetd: 1818 */ 1819#define BUFSIZE 4096 1820 1821/* ARGSUSED */ 1822void 1823echo_stream(s, sep) /* Echo service -- echo data back */ 1824 int s; 1825 struct servtab *sep; 1826{ 1827 char buffer[BUFSIZE]; 1828 int i; 1829 1830 inetd_setproctitle(sep->se_service, s); 1831 while ((i = read(s, buffer, sizeof(buffer))) > 0 && 1832 write(s, buffer, i) > 0) 1833 ; 1834} 1835 1836/* ARGSUSED */ 1837void 1838echo_dg(s, sep) /* Echo service -- echo data back */ 1839 int s; 1840 struct servtab *sep; 1841{ 1842 char buffer[BUFSIZE]; 1843 int i, size; 1844 struct sockaddr_storage ss; 1845 struct sockaddr *sa; 1846 1847 sa = (struct sockaddr *)&ss; 1848 size = sizeof(ss); 1849 if ((i = recvfrom(s, buffer, sizeof(buffer), 0, sa, &size)) < 0) 1850 return; 1851 if (port_good_dg(sa)) 1852 (void) sendto(s, buffer, i, 0, sa, size); 1853} 1854 1855/* ARGSUSED */ 1856void 1857discard_stream(s, sep) /* Discard service -- ignore data */ 1858 int s; 1859 struct servtab *sep; 1860{ 1861 char buffer[BUFSIZE]; 1862 1863 inetd_setproctitle(sep->se_service, s); 1864 while ((errno = 0, read(s, buffer, sizeof(buffer)) > 0) || 1865 errno == EINTR) 1866 ; 1867} 1868 1869/* ARGSUSED */ 1870void 1871discard_dg(s, sep) /* Discard service -- ignore data */ 1872 int s; 1873 struct servtab *sep; 1874{ 1875 char buffer[BUFSIZE]; 1876 1877 (void) read(s, buffer, sizeof(buffer)); 1878} 1879 1880#include <ctype.h> 1881#define LINESIZ 72 1882char ring[128]; 1883char *endring; 1884 1885void 1886initring() 1887{ 1888 int i; 1889 1890 endring = ring; 1891 1892 for (i = 0; i <= 128; ++i) 1893 if (isprint(i)) 1894 *endring++ = i; 1895} 1896 1897/* ARGSUSED */ 1898void 1899chargen_stream(s, sep) /* Character generator */ 1900 int s; 1901 struct servtab *sep; 1902{ 1903 int len; 1904 char *rs, text[LINESIZ+2]; 1905 1906 inetd_setproctitle(sep->se_service, s); 1907 1908 if (!endring) { 1909 initring(); 1910 rs = ring; 1911 } 1912 1913 text[LINESIZ] = '\r'; 1914 text[LINESIZ + 1] = '\n'; 1915 for (rs = ring;;) { 1916 if ((len = endring - rs) >= LINESIZ) 1917 memmove(text, rs, LINESIZ); 1918 else { 1919 memmove(text, rs, len); 1920 memmove(text + len, ring, LINESIZ - len); 1921 } 1922 if (++rs == endring) 1923 rs = ring; 1924 if (write(s, text, sizeof(text)) != sizeof(text)) 1925 break; 1926 } 1927} 1928 1929/* ARGSUSED */ 1930void 1931chargen_dg(s, sep) /* Character generator */ 1932 int s; 1933 struct servtab *sep; 1934{ 1935 struct sockaddr_storage ss; 1936 struct sockaddr *sa; 1937 static char *rs; 1938 int len, size; 1939 char text[LINESIZ+2]; 1940 1941 if (endring == 0) { 1942 initring(); 1943 rs = ring; 1944 } 1945 1946 sa = (struct sockaddr *)&ss; 1947 size = sizeof(ss); 1948 if (recvfrom(s, text, sizeof(text), 0, sa, &size) < 0) 1949 return; 1950 1951 if (!port_good_dg(sa)) 1952 return; 1953 1954 if ((len = endring - rs) >= LINESIZ) 1955 memmove(text, rs, LINESIZ); 1956 else { 1957 memmove(text, rs, len); 1958 memmove(text + len, ring, LINESIZ - len); 1959 } 1960 if (++rs == endring) 1961 rs = ring; 1962 text[LINESIZ] = '\r'; 1963 text[LINESIZ + 1] = '\n'; 1964 (void) sendto(s, text, sizeof(text), 0, sa, size); 1965} 1966 1967/* 1968 * Return a machine readable date and time, in the form of the 1969 * number of seconds since midnight, Jan 1, 1900. Since gettimeofday 1970 * returns the number of seconds since midnight, Jan 1, 1970, 1971 * we must add 2208988800 seconds to this figure to make up for 1972 * some seventy years Bell Labs was asleep. 1973 */ 1974 1975uint32_t 1976machtime() 1977{ 1978 struct timeval tv; 1979 1980 if (gettimeofday(&tv, (struct timezone *)0) < 0) { 1981 if (debug) 1982 fprintf(stderr, "Unable to get time of day\n"); 1983 return (0); 1984 } 1985#define OFFSET ((uint32_t)25567 * 24*60*60) 1986 return (htonl((uint32_t)(tv.tv_sec + OFFSET))); 1987#undef OFFSET 1988} 1989 1990/* ARGSUSED */ 1991void 1992machtime_stream(s, sep) 1993 int s; 1994 struct servtab *sep; 1995{ 1996 uint32_t result; 1997 1998 result = machtime(); 1999 (void) write(s, (char *) &result, sizeof(result)); 2000} 2001 2002/* ARGSUSED */ 2003void 2004machtime_dg(s, sep) 2005 int s; 2006 struct servtab *sep; 2007{ 2008 uint32_t result; 2009 struct sockaddr_storage ss; 2010 struct sockaddr *sa; 2011 int size; 2012 2013 sa = (struct sockaddr *)&ss; 2014 size = sizeof(ss); 2015 if (recvfrom(s, (char *)&result, sizeof(result), 0, sa, &size) < 0) 2016 return; 2017 if (!port_good_dg(sa)) 2018 return; 2019 result = machtime(); 2020 (void) sendto(s, (char *) &result, sizeof(result), 0, sa, size); 2021} 2022 2023/* ARGSUSED */ 2024void 2025daytime_stream(s, sep) /* Return human-readable time of day */ 2026 int s; 2027 struct servtab *sep; 2028{ 2029 char buffer[256]; 2030 time_t clock; 2031 int len; 2032 2033 clock = time((time_t *) 0); 2034 2035 len = snprintf(buffer, sizeof buffer, "%.24s\r\n", ctime(&clock)); 2036 (void) write(s, buffer, len); 2037} 2038 2039/* ARGSUSED */ 2040void 2041daytime_dg(s, sep) /* Return human-readable time of day */ 2042 int s; 2043 struct servtab *sep; 2044{ 2045 char buffer[256]; 2046 time_t clock; 2047 struct sockaddr_storage ss; 2048 struct sockaddr *sa; 2049 int size, len; 2050 2051 clock = time((time_t *) 0); 2052 2053 sa = (struct sockaddr *)&ss; 2054 size = sizeof(ss); 2055 if (recvfrom(s, buffer, sizeof(buffer), 0, sa, &size) < 0) 2056 return; 2057 if (!port_good_dg(sa)) 2058 return; 2059 len = snprintf(buffer, sizeof buffer, "%.24s\r\n", ctime(&clock)); 2060 (void) sendto(s, buffer, len, 0, sa, size); 2061} 2062 2063/* 2064 * print_service: 2065 * Dump relevant information to stderr 2066 */ 2067void 2068print_service(action, sep) 2069 char *action; 2070 struct servtab *sep; 2071{ 2072 if (isrpcservice(sep)) 2073 fprintf(stderr, 2074 "%s: %s rpcprog=%d, rpcvers = %d/%d, proto=%s, wait:max=%d.%d, user:group=%s.%s builtin=%lx server=%s" 2075#ifdef IPSEC 2076 " policy=\"%s\"" 2077#endif 2078 "\n", 2079 action, sep->se_service, 2080 sep->se_rpcprog, sep->se_rpcversh, sep->se_rpcversl, sep->se_proto, 2081 sep->se_wait, sep->se_max, sep->se_user, sep->se_group, 2082 (long)sep->se_bi, sep->se_server 2083#ifdef IPSEC 2084 , (sep->se_policy ? sep->se_policy : "") 2085#endif 2086 ); 2087 else 2088 fprintf(stderr, 2089 "%s: %s proto=%s%s, wait:max=%d.%d, user:group=%s.%s builtin=%lx server=%s" 2090#ifdef IPSEC 2091 " policy=%s" 2092#endif 2093 "\n", 2094 action, sep->se_service, 2095 sep->se_type == FAITH_TYPE ? "faith/" : "", 2096 sep->se_proto, 2097 sep->se_wait, sep->se_max, sep->se_user, sep->se_group, 2098 (long)sep->se_bi, sep->se_server 2099#ifdef IPSEC 2100 , (sep->se_policy ? sep->se_policy : "") 2101#endif 2102 ); 2103} 2104 2105void 2106usage() 2107{ 2108 2109#ifdef LIBWRAP 2110 (void)fprintf(stderr, "usage: %s [-dl] [conf]\n", getprogname()); 2111#else 2112 (void)fprintf(stderr, "usage: %s [-d] [conf]\n", getprogname()); 2113#endif 2114 exit(1); 2115} 2116 2117 2118/* 2119 * Based on TCPMUX.C by Mark K. Lottor November 1988 2120 * sri-nic::ps:<mkl>tcpmux.c 2121 */ 2122 2123static int /* # of characters upto \r,\n or \0 */ 2124getline(fd, buf, len) 2125 int fd; 2126 char *buf; 2127 int len; 2128{ 2129 int count = 0, n; 2130 2131 do { 2132 n = read(fd, buf, len-count); 2133 if (n == 0) 2134 return (count); 2135 if (n < 0) 2136 return (-1); 2137 while (--n >= 0) { 2138 if (*buf == '\r' || *buf == '\n' || *buf == '\0') 2139 return (count); 2140 count++; 2141 buf++; 2142 } 2143 } while (count < len); 2144 return (count); 2145} 2146 2147#define MAX_SERV_LEN (256+2) /* 2 bytes for \r\n */ 2148 2149#define strwrite(fd, buf) (void) write(fd, buf, sizeof(buf)-1) 2150 2151void 2152tcpmux(ctrl, sep) 2153 int ctrl; 2154 struct servtab *sep; 2155{ 2156 char service[MAX_SERV_LEN+1]; 2157 int len; 2158 2159 /* Get requested service name */ 2160 if ((len = getline(ctrl, service, MAX_SERV_LEN)) < 0) { 2161 strwrite(ctrl, "-Error reading service name\r\n"); 2162 goto reject; 2163 } 2164 service[len] = '\0'; 2165 2166 if (debug) 2167 fprintf(stderr, "tcpmux: someone wants %s\n", service); 2168 2169 /* 2170 * Help is a required command, and lists available services, 2171 * one per line. 2172 */ 2173 if (!strcasecmp(service, "help")) { 2174 strwrite(ctrl, "+Available services:\r\n"); 2175 strwrite(ctrl, "help\r\n"); 2176 for (sep = servtab; sep; sep = sep->se_next) { 2177 if (!ISMUX(sep)) 2178 continue; 2179 (void)write(ctrl, sep->se_service, 2180 strlen(sep->se_service)); 2181 strwrite(ctrl, "\r\n"); 2182 } 2183 goto reject; 2184 } 2185 2186 /* Try matching a service in inetd.conf with the request */ 2187 for (sep = servtab; sep; sep = sep->se_next) { 2188 if (!ISMUX(sep)) 2189 continue; 2190 if (!strcasecmp(service, sep->se_service)) { 2191 if (ISMUXPLUS(sep)) 2192 strwrite(ctrl, "+Go\r\n"); 2193 run_service(ctrl, sep); 2194 return; 2195 } 2196 } 2197 strwrite(ctrl, "-Service not available\r\n"); 2198reject: 2199 _exit(1); 2200} 2201 2202 2203#ifdef MULOG 2204dolog(sep, ctrl) 2205 struct servtab *sep; 2206 int ctrl; 2207{ 2208 struct sockaddr_storage ss; 2209 struct sockaddr *sa = (struct sockaddr *)&ss; 2210 struct sockaddr_in *sin = (struct sockaddr_in *)&ss; 2211 int len = sizeof(ss); 2212 struct hostent *hp; 2213 char *host, *dp, buf[BUFSIZ], *rfc931_name(); 2214 int connected = 1; 2215 2216 switch (sep->se_family) { 2217 case AF_INET: 2218#ifdef INET6 2219 case AF_INET6: 2220#endif 2221 break; 2222 default; 2223 return; 2224 } 2225 2226 if (getpeername(ctrl, sa, &len) < 0) { 2227 if (errno != ENOTCONN) { 2228 syslog(LOG_ERR, "getpeername: %m"); 2229 return; 2230 } 2231 if (recvfrom(ctrl, buf, sizeof(buf), MSG_PEEK, sa, &len) < 0) { 2232 syslog(LOG_ERR, "recvfrom: %m"); 2233 return; 2234 } 2235 connected = 0; 2236 } 2237 switch (sa->sa_family) { 2238 case AF_INET: 2239#ifdef INET6 2240 case AF_INET6: 2241#endif 2242 break; 2243 default; 2244 syslog(LOG_ERR, "unexpected address family %u", sa->sa_family); 2245 return; 2246 } 2247 2248 if (getnameinfo(sa, sa->sa_len, buf, sizeof(buf), NULL, 0, 0) != 0) 2249 strcpy(buf, "?"); 2250 host = buf; 2251 2252 switch (sep->se_log & ~MULOG_RFC931) { 2253 case 0: 2254 return; 2255 case 1: 2256 if (curdom == NULL || *curdom == '\0') 2257 break; 2258 dp = host + strlen(host) - strlen(curdom); 2259 if (dp < host) 2260 break; 2261 if (debug) 2262 fprintf(stderr, "check \"%s\" against curdom \"%s\"\n", 2263 host, curdom); 2264 if (strcasecmp(dp, curdom) == 0) 2265 return; 2266 break; 2267 case 2: 2268 default: 2269 break; 2270 } 2271 2272 openlog("", LOG_NOWAIT, MULOG); 2273 2274 if (connected && (sep->se_log & MULOG_RFC931)) 2275 syslog(LOG_INFO, "%s@%s wants %s", 2276 rfc931_name(sa, ctrl), host, sep->se_service); 2277 else 2278 syslog(LOG_INFO, "%s wants %s", 2279 host, sep->se_service); 2280} 2281 2282/* 2283 * From tcp_log by 2284 * Wietse Venema, Eindhoven University of Technology, The Netherlands. 2285 */ 2286#if 0 2287static char sccsid[] = "@(#) rfc931.c 1.3 92/08/31 22:54:46"; 2288#endif 2289 2290#include <setjmp.h> 2291 2292#define RFC931_PORT 113 /* Semi-well-known port */ 2293#define TIMEOUT 4 2294#define TIMEOUT2 10 2295 2296static jmp_buf timebuf; 2297 2298/* timeout - handle timeouts */ 2299 2300static void timeout(sig) 2301int sig; 2302{ 2303 longjmp(timebuf, sig); 2304} 2305 2306/* rfc931_name - return remote user name */ 2307 2308char * 2309rfc931_name(there, ctrl) 2310struct sockaddr *there; /* remote link information */ 2311int ctrl; 2312{ 2313 struct sockaddr_storage here; /* local link information */ 2314 struct sockaddr_storage sin; /* for talking to RFC931 daemon */ 2315 int length; 2316 int s; 2317 unsigned remote; 2318 unsigned local; 2319 static char user[256]; /* XXX */ 2320 char buf[256]; 2321 char *cp; 2322 char *result = "USER_UNKNOWN"; 2323 int len; 2324 u_int16_t myport, hisport; 2325 2326 /* Find out local port number of our stdin. */ 2327 2328 length = sizeof(here); 2329 if (getsockname(ctrl, (struct sockaddr *) &here, &length) == -1) { 2330 syslog(LOG_ERR, "getsockname: %m"); 2331 return (result); 2332 } 2333 switch (here.ss_family) { 2334 case AF_INET: 2335 myport = ((struct sockaddr_in *)&here)->sin_port; 2336 break; 2337#ifdef INET6 2338 case AF_INET6: 2339 myport = ((struct sockaddr_in6 *)&here)->sin6_port; 2340 break; 2341#endif 2342 } 2343 switch (there->sa_family) { 2344 case AF_INET: 2345 hisport = ((struct sockaddr_in *)sa)->sin_port; 2346 break; 2347#ifdef INET6 2348 case AF_INET6: 2349 hisport = ((struct sockaddr_in6 *)sa)->sin6_port; 2350 break; 2351#endif 2352 } 2353 /* Set up timer so we won't get stuck. */ 2354 2355 if ((s = socket(here.ss_family, SOCK_STREAM, 0)) == -1) { 2356 syslog(LOG_ERR, "socket: %m"); 2357 return (result); 2358 } 2359 2360 sin = here; 2361 switch (sin.ss_family) { 2362 case AF_INET: 2363 ((struct sockaddr_in *)&sin)->sin_port = htons(0); 2364 break; 2365#ifdef INET6 2366 case AF_INET6: 2367 ((struct sockaddr_in6 *)&sin)->sin6_port = htons(0); 2368 break; 2369#endif 2370 } 2371 if (bind(s, (struct sockaddr *) &sin, sin.ss_len) == -1) { 2372 syslog(LOG_ERR, "bind: %m"); 2373 return (result); 2374 } 2375 2376 signal(SIGALRM, timeout); 2377 if (setjmp(timebuf)) { 2378 close(s); /* not: fclose(fp) */ 2379 return (result); 2380 } 2381 alarm(TIMEOUT); 2382 2383 /* Connect to the RFC931 daemon. */ 2384 2385 memcpy(&sin, there, there->sa_len); 2386 switch (sin.ss_family) { 2387 case AF_INET: 2388 ((struct sockaddr_in *)&sin)->sin_port = htons(RFC931_PORT); 2389 break; 2390#ifdef INET6 2391 case AF_INET6: 2392 ((struct sockaddr_in6 *)&sin)->sin6_port = htons(RFC931_PORT); 2393 break; 2394#endif 2395 } 2396 if (connect(s, (struct sockaddr *) &sin, sin.ss_len) == -1) { 2397 close(s); 2398 alarm(0); 2399 return (result); 2400 } 2401 2402 /* Query the RFC 931 server. Would 13-byte writes ever be broken up? */ 2403 (void)snprintf(buf, sizeof buf, "%u,%u\r\n", ntohs(hisport), 2404 ntohs(myport)); 2405 2406 2407 for (len = 0, cp = buf; len < strlen(buf); ) { 2408 int n; 2409 2410 if ((n = write(s, cp, strlen(buf) - len)) == -1) { 2411 close(s); 2412 alarm(0); 2413 return (result); 2414 } 2415 cp += n; 2416 len += n; 2417 } 2418 2419 /* Read response */ 2420 for (cp = buf; cp < buf + sizeof(buf) - 1; ) { 2421 char c; 2422 if (read(s, &c, 1) != 1) { 2423 close(s); 2424 alarm(0); 2425 return (result); 2426 } 2427 if (c == '\n') 2428 break; 2429 *cp++ = c; 2430 } 2431 *cp = '\0'; 2432 2433 if (sscanf(buf, "%u , %u : USERID :%*[^:]:%255s", &remote, &local, user) == 3 2434 && ntohs(hisport) == remote 2435 && ntohs(myport) == local) { 2436 2437 /* Strip trailing carriage return. */ 2438 if (cp = strchr(user, '\r')) 2439 *cp = 0; 2440 result = user; 2441 } 2442 2443 alarm(0); 2444 close(s); 2445 return (result); 2446} 2447#endif 2448 2449/* 2450 * check if the address/port where send data to is one of the obvious ports 2451 * that are used for denial of service attacks like two echo ports 2452 * just echoing data between them 2453 */ 2454int 2455port_good_dg(sa) 2456 struct sockaddr *sa; 2457{ 2458 struct in_addr in; 2459#ifdef INET6 2460 struct in6_addr *in6; 2461#endif 2462 u_int16_t port; 2463 int i, bad; 2464 char hbuf[NI_MAXHOST]; 2465 2466 bad = 0; 2467 2468 switch (sa->sa_family) { 2469 case AF_INET: 2470 in.s_addr = ntohl(((struct sockaddr_in *)sa)->sin_addr.s_addr); 2471 port = ntohs(((struct sockaddr_in *)sa)->sin_port); 2472 v4chk: 2473 if (IN_MULTICAST(in.s_addr)) 2474 goto bad; 2475 switch ((in.s_addr & 0xff000000) >> 24) { 2476 case 0: case 127: case 255: 2477 goto bad; 2478 } 2479 /* XXX check for subnet broadcast using getifaddrs(3) */ 2480 break; 2481#ifdef INET6 2482 case AF_INET6: 2483 in6 = &((struct sockaddr_in6 *)sa)->sin6_addr; 2484 port = ntohs(((struct sockaddr_in6 *)sa)->sin6_port); 2485 if (IN6_IS_ADDR_MULTICAST(in6) || IN6_IS_ADDR_UNSPECIFIED(in6)) 2486 goto bad; 2487 if (IN6_IS_ADDR_V4MAPPED(in6) || IN6_IS_ADDR_V4COMPAT(in6)) { 2488 memcpy(&in, &in6->s6_addr[12], sizeof(in)); 2489 in.s_addr = ntohl(in.s_addr); 2490 goto v4chk; 2491 } 2492 break; 2493#endif 2494 default: 2495 /* XXX unsupported af, is it safe to assume it to be safe? */ 2496 return 1; 2497 } 2498 2499 for (i = 0; bad_ports[i] != 0; i++) { 2500 if (port == bad_ports[i]) 2501 goto bad; 2502 } 2503 2504 return (1); 2505 2506bad: 2507 if (getnameinfo(sa, sa->sa_len, hbuf, sizeof(hbuf), 2508 NULL, 0, niflags) != 0) 2509 strcpy(hbuf, "?"); 2510 syslog(LOG_WARNING,"Possible DoS attack from %s, Port %d", 2511 hbuf, port); 2512 return (0); 2513} 2514