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