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