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