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