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