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