inetd.c revision 1.24
1/* $NetBSD: inetd.c,v 1.24 1997/03/13 17:35:39 mycroft Exp $ */ 2 3/* 4 * Copyright (c) 1983, 1991, 1993, 1994 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by the University of 18 * California, Berkeley and its contributors. 19 * 4. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 36#ifndef lint 37static char copyright[] = 38"@(#) Copyright (c) 1983, 1991, 1993, 1994\n\ 39 The Regents of the University of California. All rights reserved.\n"; 40#endif /* not lint */ 41 42#ifndef lint 43#if 0 44static char sccsid[] = "@(#)inetd.c 8.4 (Berkeley) 4/13/94"; 45#else 46static char rcsid[] = "$NetBSD: inetd.c,v 1.24 1997/03/13 17:35:39 mycroft Exp $"; 47#endif 48#endif /* not lint */ 49 50/* 51 * Inetd - Internet super-server 52 * 53 * This program invokes all internet services as needed. Connection-oriented 54 * services are invoked each time a connection is made, by creating a process. 55 * This process is passed the connection as file descriptor 0 and is expected 56 * to do a getpeername to find out the source host and port. 57 * 58 * Datagram oriented services are invoked when a datagram 59 * arrives; a process is created and passed a pending message 60 * on file descriptor 0. Datagram servers may either connect 61 * to their peer, freeing up the original socket for inetd 62 * to receive further messages on, or ``take over the socket'', 63 * processing all arriving datagrams and, eventually, timing 64 * out. The first type of server is said to be ``multi-threaded''; 65 * the second type of server ``single-threaded''. 66 * 67 * Inetd uses a configuration file which is read at startup 68 * and, possibly, at some later time in response to a hangup signal. 69 * The configuration file is ``free format'' with fields given in the 70 * order shown below. Continuation lines for an entry must being with 71 * a space or tab. All fields must be present in each entry. 72 * 73 * service name must be in /etc/services or must 74 * name a tcpmux service 75 * socket type stream/dgram/raw/rdm/seqpacket 76 * protocol must be in /etc/protocols 77 * wait/nowait[.max] single-threaded/multi-threaded, max # 78 * user[.group] user/group to run daemon as 79 * server program full path name 80 * server program arguments maximum of MAXARGS (20) 81 * 82 * For RPC services 83 * service name/version must be in /etc/rpc 84 * socket type stream/dgram/raw/rdm/seqpacket 85 * protocol must be in /etc/protocols 86 * wait/nowait[.max] single-threaded/multi-threaded 87 * user[.group] user to run daemon as 88 * server program full path name 89 * server program arguments maximum of MAXARGS (20) 90 * 91 * For non-RPC services, the "service name" can be of the form 92 * hostaddress:servicename, in which case the hostaddress is used 93 * as the host portion of the address to listen on. If hostaddress 94 * consists of a single `*' character, INADDR_ANY is used. 95 * 96 * A line can also consist of just 97 * hostaddress: 98 * where hostaddress is as in the preceding paragraph. Such a line must 99 * have no further fields; the specified hostaddress is remembered and 100 * used for all further lines that have no hostaddress specified, 101 * until the next such line (or EOF). (This is why * is provided to 102 * allow explicit specification of INADDR_ANY.) A line 103 * *: 104 * is implicitly in effect at the beginning of the file. 105 * 106 * The hostaddress specifier may (and often will) contain dots; 107 * the service name must not. 108 * 109 * For RPC services, host-address specifiers are accepted and will 110 * work to some extent; however, because of limitations in the 111 * portmapper interface, it will not work to try to give more than 112 * one line for any given RPC service, even if the host-address 113 * specifiers are different. 114 * 115 * TCP services without official port numbers are handled with the 116 * RFC1078-based tcpmux internal service. Tcpmux listens on port 1 for 117 * requests. When a connection is made from a foreign host, the service 118 * requested is passed to tcpmux, which looks it up in the servtab list 119 * and returns the proper entry for the service. Tcpmux returns a 120 * negative reply if the service doesn't exist, otherwise the invoked 121 * server is expected to return the positive reply if the service type in 122 * inetd.conf file has the prefix "tcpmux/". If the service type has the 123 * prefix "tcpmux/+", tcpmux will return the positive reply for the 124 * process; this is for compatibility with older server code, and also 125 * allows you to invoke programs that use stdin/stdout without putting any 126 * special server code in them. Services that use tcpmux are "nowait" 127 * because they do not have a well-known port and hence cannot listen 128 * for new requests. 129 * 130 * Comment lines are indicated by a `#' in column 1. 131 */ 132 133/* 134 * Here's the scoop concerning the user.group feature: 135 * 136 * 1) set-group-option off. 137 * 138 * a) user = root: NO setuid() or setgid() is done 139 * 140 * b) other: setuid() 141 * setgid(primary group as found in passwd) 142 * initgroups(name, primary group) 143 * 144 * 2) set-group-option on. 145 * 146 * a) user = root: NO setuid() 147 * setgid(specified group) 148 * NO initgroups() 149 * 150 * b) other: setuid() 151 * setgid(specified group) 152 * initgroups(name, specified group) 153 * 154 */ 155 156#include <sys/param.h> 157#include <sys/stat.h> 158#include <sys/ioctl.h> 159#include <sys/socket.h> 160#include <sys/un.h> 161#include <sys/wait.h> 162#include <sys/time.h> 163#include <sys/resource.h> 164 165#ifndef RLIMIT_NOFILE 166#define RLIMIT_NOFILE RLIMIT_OFILE 167#endif 168 169#define RPC 170 171#include <netinet/in.h> 172#include <arpa/inet.h> 173#ifdef RPC 174#include <rpc/rpc.h> 175#endif 176 177#include <errno.h> 178#include <grp.h> 179#include <netdb.h> 180#include <pwd.h> 181#include <signal.h> 182#include <stdio.h> 183#include <stdlib.h> 184#include <string.h> 185#include <syslog.h> 186#include <unistd.h> 187 188#include "pathnames.h" 189 190#ifdef LIBWRAP 191# include <tcpd.h> 192#ifndef LIBWRAP_ALLOW_FACILITY 193# define LIBWRAP_ALLOW_FACILITY LOG_AUTH 194#endif 195#ifndef LIBWRAP_ALLOW_SEVERITY 196# define LIBWRAP_ALLOW_SEVERITY LOG_INFO 197#endif 198#ifndef LIBWRAP_DENY_FACILITY 199# define LIBWRAP_DENY_FACILITY LOG_AUTH 200#endif 201#ifndef LIBWRAP_DENY_SEVERITY 202# define LIBWRAP_DENY_SEVERITY LOG_WARNING 203#endif 204int allow_severity = LIBWRAP_ALLOW_FACILITY|LIBWRAP_ALLOW_SEVERITY; 205int deny_severity = LIBWRAP_DENY_FACILITY|LIBWRAP_DENY_SEVERITY; 206#endif 207 208#define TOOMANY 40 /* don't start more than TOOMANY */ 209#define CNT_INTVL 60 /* servers in CNT_INTVL sec. */ 210#define RETRYTIME (60*10) /* retry after bind or server fail */ 211 212#define SIGBLOCK (sigmask(SIGCHLD)|sigmask(SIGHUP)|sigmask(SIGALRM)) 213 214int debug; 215#ifdef LIBWRAP 216int lflag; 217#endif 218int nsock, maxsock; 219fd_set allsock; 220int options; 221int timingout; 222struct servent *sp; 223char *curdom; 224 225#ifndef OPEN_MAX 226#define OPEN_MAX 64 227#endif 228 229/* Reserve some descriptors, 3 stdio + at least: 1 log, 1 conf. file */ 230#define FD_MARGIN (8) 231typeof(((struct rlimit *)0)->rlim_cur) rlim_ofile_cur = OPEN_MAX; 232 233#ifdef RLIMIT_NOFILE 234struct rlimit rlim_ofile; 235#endif 236 237struct servtab { 238 char *se_hostaddr; /* host address to listen on */ 239 char *se_service; /* name of service */ 240 int se_socktype; /* type of socket to use */ 241 int se_family; /* address family */ 242 char *se_proto; /* protocol used */ 243 int se_rpcprog; /* rpc program number */ 244 int se_rpcversl; /* rpc program lowest version */ 245 int se_rpcversh; /* rpc program highest version */ 246#define isrpcservice(sep) ((sep)->se_rpcversl != 0) 247 short se_wait; /* single threaded server */ 248 short se_checked; /* looked at during merge */ 249 char *se_user; /* user name to run as */ 250 char *se_group; /* group name to run as */ 251 struct biltin *se_bi; /* if built-in, description */ 252 char *se_server; /* server program */ 253#define MAXARGV 20 254 char *se_argv[MAXARGV+1]; /* program arguments */ 255 int se_fd; /* open descriptor */ 256 int se_type; /* type */ 257 union { 258 struct sockaddr se_un_ctrladdr; 259 struct sockaddr_in se_un_ctrladdr_in; 260 struct sockaddr_un se_un_ctrladdr_un; 261 } se_un; /* bound address */ 262#define se_ctrladdr se_un.se_un_ctrladdr 263#define se_ctrladdr_in se_un.se_un_ctrladdr_in 264#define se_ctrladdr_un se_un.se_un_ctrladdr_un 265 int se_ctrladdr_size; 266 int se_max; /* max # of instances of this service */ 267 int se_count; /* number started since se_time */ 268 struct timeval se_time; /* start of se_count */ 269#ifdef MULOG 270 int se_log; 271#define MULOG_RFC931 0x40000000 272#endif 273 struct servtab *se_next; 274} *servtab; 275 276#define NORM_TYPE 0 277#define MUX_TYPE 1 278#define MUXPLUS_TYPE 2 279#define ISMUX(sep) (((sep)->se_type == MUX_TYPE) || \ 280 ((sep)->se_type == MUXPLUS_TYPE)) 281#define ISMUXPLUS(sep) ((sep)->se_type == MUXPLUS_TYPE) 282 283 284void chargen_dg __P((int, struct servtab *)); 285void chargen_stream __P((int, struct servtab *)); 286void close_sep __P((struct servtab *)); 287void config __P((int)); 288void daytime_dg __P((int, struct servtab *)); 289void daytime_stream __P((int, struct servtab *)); 290void discard_dg __P((int, struct servtab *)); 291void discard_stream __P((int, struct servtab *)); 292void echo_dg __P((int, struct servtab *)); 293void echo_stream __P((int, struct servtab *)); 294void endconfig __P((void)); 295struct servtab *enter __P((struct servtab *)); 296void freeconfig __P((struct servtab *)); 297struct servtab *getconfigent __P((void)); 298void goaway __P((int)); 299void machtime_dg __P((int, struct servtab *)); 300void machtime_stream __P((int, struct servtab *)); 301char *newstr __P((char *)); 302char *nextline __P((FILE *)); 303void print_service __P((char *, struct servtab *)); 304void reapchild __P((int)); 305void retry __P((int)); 306int setconfig __P((void)); 307void setup __P((struct servtab *)); 308char *sskip __P((char **)); 309char *skip __P((char **)); 310struct servtab *tcpmux __P((int)); 311void usage __P((void)); 312 313struct biltin { 314 char *bi_service; /* internally provided service name */ 315 int bi_socktype; /* type of socket supported */ 316 short bi_fork; /* 1 if should fork before call */ 317 short bi_wait; /* 1 if should wait for child */ 318 void (*bi_fn)(); /* function which performs it */ 319} biltins[] = { 320 /* Echo received data */ 321 { "echo", SOCK_STREAM, 1, 0, echo_stream }, 322 { "echo", SOCK_DGRAM, 0, 0, echo_dg }, 323 324 /* Internet /dev/null */ 325 { "discard", SOCK_STREAM, 1, 0, discard_stream }, 326 { "discard", SOCK_DGRAM, 0, 0, discard_dg }, 327 328 /* Return 32 bit time since 1900 */ 329 { "time", SOCK_STREAM, 0, 0, machtime_stream }, 330 { "time", SOCK_DGRAM, 0, 0, machtime_dg }, 331 332 /* Return human-readable time */ 333 { "daytime", SOCK_STREAM, 0, 0, daytime_stream }, 334 { "daytime", SOCK_DGRAM, 0, 0, daytime_dg }, 335 336 /* Familiar character generator */ 337 { "chargen", SOCK_STREAM, 1, 0, chargen_stream }, 338 { "chargen", SOCK_DGRAM, 0, 0, chargen_dg }, 339 340 { "tcpmux", SOCK_STREAM, 1, 0, (void (*)())tcpmux }, 341 342 { NULL } 343}; 344 345#define NUMINT (sizeof(intab) / sizeof(struct inent)) 346char *CONFIG = _PATH_INETDCONF; 347char **Argv; 348char *LastArg; 349extern char *__progname; 350 351#ifdef sun 352/* 353 * Sun's RPC library caches the result of `dtablesize()' 354 * This is incompatible with our "bumping" of file descriptors "on demand" 355 */ 356int 357_rpc_dtablesize() 358{ 359 return rlim_ofile_cur; 360} 361#endif 362 363main(argc, argv, envp) 364 int argc; 365 char *argv[], *envp[]; 366{ 367 struct servtab *sep, *nsep; 368 struct passwd *pwd; 369 struct group *grp; 370 struct sigvec sv; 371 int tmpint, ch, dofork; 372 pid_t pid; 373 char buf[50]; 374#ifdef LIBWRAP 375 struct request_info req; 376 int denied; 377 char *service; 378#endif 379 380 Argv = argv; 381 if (envp == 0 || *envp == 0) 382 envp = argv; 383 while (*envp) 384 envp++; 385 LastArg = envp[-1] + strlen(envp[-1]); 386 387 while ((ch = getopt(argc, argv, 388#ifdef LIBWRAP 389 "dl" 390#else 391 "d" 392#endif 393 )) != EOF) 394 switch(ch) { 395 case 'd': 396 debug = 1; 397 options |= SO_DEBUG; 398 break; 399#ifdef LIBWRAP 400 case 'l': 401 lflag = 1; 402 break; 403#endif 404 case '?': 405 default: 406 usage(); 407 } 408 argc -= optind; 409 argv += optind; 410 411 if (argc > 0) 412 CONFIG = argv[0]; 413 414 if (debug == 0) 415 daemon(0, 0); 416 openlog(__progname, LOG_PID | LOG_NOWAIT, LOG_DAEMON); 417 logpid(); 418 419#ifdef RLIMIT_NOFILE 420 if (getrlimit(RLIMIT_NOFILE, &rlim_ofile) < 0) { 421 syslog(LOG_ERR, "getrlimit: %m"); 422 } else { 423 rlim_ofile_cur = rlim_ofile.rlim_cur; 424 if (rlim_ofile_cur == RLIM_INFINITY) /* ! */ 425 rlim_ofile_cur = OPEN_MAX; 426 } 427#endif 428 429 memset(&sv, 0, sizeof(sv)); 430 sv.sv_mask = SIGBLOCK; 431 sv.sv_handler = retry; 432 sigvec(SIGALRM, &sv, (struct sigvec *)0); 433 config(SIGHUP); 434 sv.sv_handler = config; 435 sigvec(SIGHUP, &sv, (struct sigvec *)0); 436 sv.sv_handler = reapchild; 437 sigvec(SIGCHLD, &sv, (struct sigvec *)0); 438 sv.sv_handler = goaway; 439 sigvec(SIGTERM, &sv, (struct sigvec *)0); 440 sv.sv_handler = goaway; 441 sigvec(SIGINT, &sv, (struct sigvec *)0); 442 443 { 444 /* space for daemons to overwrite environment for ps */ 445#define DUMMYSIZE 100 446 char dummy[DUMMYSIZE]; 447 448 (void)memset(dummy, 'x', DUMMYSIZE - 1); 449 dummy[DUMMYSIZE - 1] = '\0'; 450 451 (void)setenv("inetd_dummy", dummy, 1); 452 } 453 454 for (;;) { 455 int n, ctrl; 456 fd_set readable; 457 458 if (nsock == 0) { 459 (void) sigblock(SIGBLOCK); 460 while (nsock == 0) 461 sigpause(0L); 462 (void) sigsetmask(0L); 463 } 464 readable = allsock; 465 if ((n = select(maxsock + 1, &readable, (fd_set *)0, 466 (fd_set *)0, (struct timeval *)0)) <= 0) { 467 if (n < 0 && errno != EINTR) 468 syslog(LOG_WARNING, "select: %m"); 469 sleep(1); 470 continue; 471 } 472 for (sep = servtab; n && sep; sep = nsep) { 473 nsep = sep->se_next; 474 if (sep->se_fd != -1 && FD_ISSET(sep->se_fd, &readable)) { 475 n--; 476 if (debug) 477 fprintf(stderr, "someone wants %s\n", sep->se_service); 478 if (!sep->se_wait && sep->se_socktype == SOCK_STREAM) { 479 /* XXX here do the libwrap check-before-accept */ 480 ctrl = accept(sep->se_fd, (struct sockaddr *)0, 481 (int *)0); 482 if (debug) 483 fprintf(stderr, "accept, ctrl %d\n", ctrl); 484 if (ctrl < 0) { 485 if (errno != EINTR) 486 syslog(LOG_WARNING, 487 "accept (for %s): %m", 488 sep->se_service); 489 continue; 490 } 491 /* 492 * Call tcpmux to find the real service to exec. 493 */ 494 if (sep->se_bi && 495 sep->se_bi->bi_fn == (void (*)()) tcpmux) { 496 sep = tcpmux(ctrl); 497 if (sep == NULL) { 498 close(ctrl); 499 continue; 500 } 501 } 502 } else 503 ctrl = sep->se_fd; 504 (void) sigblock(SIGBLOCK); 505 pid = 0; 506 dofork = (sep->se_bi == 0 || sep->se_bi->bi_fork); 507 if (dofork) { 508 if (sep->se_count++ == 0) 509 (void)gettimeofday(&sep->se_time, 510 (struct timezone *)0); 511 else if (sep->se_count >= sep->se_max) { 512 struct timeval now; 513 514 (void)gettimeofday(&now, (struct timezone *)0); 515 if (now.tv_sec - sep->se_time.tv_sec > 516 CNT_INTVL) { 517 sep->se_time = now; 518 sep->se_count = 1; 519 } else { 520 syslog(LOG_ERR, 521 "%s/%s server failing (looping), service terminated\n", 522 sep->se_service, sep->se_proto); 523 close_sep(sep); 524 sigsetmask(0L); 525 if (!timingout) { 526 timingout = 1; 527 alarm(RETRYTIME); 528 } 529 continue; 530 } 531 } 532 pid = fork(); 533 } 534 if (pid < 0) { 535 syslog(LOG_ERR, "fork: %m"); 536 if (!sep->se_wait && sep->se_socktype == SOCK_STREAM) 537 close(ctrl); 538 sigsetmask(0L); 539 sleep(1); 540 continue; 541 } 542 if (pid && sep->se_wait) { 543 sep->se_wait = pid; 544 FD_CLR(sep->se_fd, &allsock); 545 nsock--; 546 } 547 sigsetmask(0L); 548 if (pid == 0) { 549 if (debug && dofork) 550 setsid(); 551#ifdef LIBWRAP 552 request_init(&req, RQ_DAEMON, sep->se_argv[0] ? 553 sep->se_argv[0] : sep->se_service, RQ_FILE, ctrl, 554 NULL); 555 fromhost(&req); 556 denied = !hosts_access(&req); 557 if (denied || lflag) { 558 sp = getservbyport(sep->se_ctrladdr_in.sin_port, 559 sep->se_proto); 560 if (sp == NULL) { 561 (void)snprintf(buf, sizeof buf, "%d", 562 ntohs(sep->se_ctrladdr_in.sin_port)); 563 service = buf; 564 } else 565 service = sp->s_name; 566 } 567 if (denied) { 568 syslog(deny_severity, "refused " 569 "connection from %.500s, service %s (%s)", 570 eval_client(&req), service, sep->se_proto); 571 goto reject; 572 } 573 if (lflag) { 574 syslog(allow_severity, 575 "connection from %.500s, service %s (%s)", 576 eval_client(&req), service, sep->se_proto); 577 } 578#endif /* LIBWRAP */ 579 if (sep->se_bi) 580 (*sep->se_bi->bi_fn)(ctrl, sep); 581 else { 582 if ((pwd = getpwnam(sep->se_user)) == NULL) { 583 syslog(LOG_ERR, 584 "%s/%s: %s: No such user", 585 sep->se_service, sep->se_proto, 586 sep->se_user); 587 goto reject; 588 } 589 if (sep->se_group && 590 (grp = getgrnam(sep->se_group)) == NULL) { 591 syslog(LOG_ERR, 592 "%s/%s: %s: No such group", 593 sep->se_service, sep->se_proto, 594 sep->se_group); 595 goto reject; 596 } 597 if (pwd->pw_uid) { 598 if (sep->se_group) 599 pwd->pw_gid = grp->gr_gid; 600 if (setgid(pwd->pw_gid) < 0) { 601 syslog(LOG_ERR, 602 "%s/%s: can't set gid %d: %m", 603 sep->se_service, 604 sep->se_proto, pwd->pw_gid); 605 goto reject; 606 } 607 (void) initgroups(pwd->pw_name, 608 pwd->pw_gid); 609 if (setuid(pwd->pw_uid) < 0) { 610 syslog(LOG_ERR, 611 "%s/%s: can't set uid %d: %m", 612 sep->se_service, 613 sep->se_proto, pwd->pw_uid); 614 goto reject; 615 } 616 } else if (sep->se_group) { 617 (void) setgid((gid_t)grp->gr_gid); 618 } 619 if (debug) 620 fprintf(stderr, "%d execl %s\n", 621 getpid(), sep->se_server); 622#ifdef MULOG 623 if (sep->se_log) 624 dolog(sep, ctrl); 625#endif 626 if (ctrl != 0) { 627 dup2(ctrl, 0); 628 close(ctrl); 629 ctrl = 0; 630 } 631 dup2(0, 1); 632 dup2(0, 2); 633#ifdef RLIMIT_NOFILE 634 if (rlim_ofile.rlim_cur != rlim_ofile_cur) { 635 if (setrlimit(RLIMIT_NOFILE, 636 &rlim_ofile) < 0) 637 syslog(LOG_ERR, 638 "setrlimit: %m"); 639 } 640#endif 641 for (tmpint = rlim_ofile_cur-1; --tmpint > 2; ) 642 (void)close(tmpint); 643 execv(sep->se_server, sep->se_argv); 644 syslog(LOG_ERR, 645 "cannot execute %s: %m", sep->se_server); 646 reject: 647 if (sep->se_socktype != SOCK_STREAM) 648 recv(ctrl, buf, sizeof (buf), 0); 649 _exit(1); 650 } 651 } 652 if (!sep->se_wait && sep->se_socktype == SOCK_STREAM) 653 close(ctrl); 654 } 655 } 656 } 657} 658 659void 660reapchild(signo) 661 int signo; 662{ 663 int status; 664 pid_t pid; 665 struct servtab *sep; 666 667 for (;;) { 668 pid = wait3(&status, WNOHANG, (struct rusage *)0); 669 if (pid <= 0) 670 break; 671 if (debug) 672 fprintf(stderr, "%d reaped, status %#x\n", 673 pid, status); 674 for (sep = servtab; sep; sep = sep->se_next) 675 if (sep->se_wait == pid) { 676 if (WIFEXITED(status) && WEXITSTATUS(status)) 677 syslog(LOG_WARNING, 678 "%s: exit status 0x%x", 679 sep->se_server, WEXITSTATUS(status)); 680 else if (WIFSIGNALED(status)) 681 syslog(LOG_WARNING, 682 "%s: exit signal 0x%x", 683 sep->se_server, WTERMSIG(status)); 684 sep->se_wait = 1; 685 FD_SET(sep->se_fd, &allsock); 686 nsock++; 687 if (debug) 688 fprintf(stderr, "restored %s, fd %d\n", 689 sep->se_service, sep->se_fd); 690 } 691 } 692} 693 694void 695config(signo) 696 int signo; 697{ 698 struct servtab *sep, *cp, **sepp; 699 struct passwd *pwd; 700 long omask; 701 int n; 702 703 if (!setconfig()) { 704 syslog(LOG_ERR, "%s: %m", CONFIG); 705 return; 706 } 707 for (sep = servtab; sep; sep = sep->se_next) 708 sep->se_checked = 0; 709 while (cp = getconfigent()) { 710 for (sep = servtab; sep; sep = sep->se_next) 711 if (strcmp(sep->se_service, cp->se_service) == 0 && 712 strcmp(sep->se_hostaddr, cp->se_hostaddr) == 0 && 713 strcmp(sep->se_proto, cp->se_proto) == 0 && 714 ISMUX(sep) == ISMUX(cp)) 715 break; 716 if (sep != 0) { 717 int i; 718 719#define SWAP(type, a, b) {type c=(type)a; (type)a=(type)b; (type)b=(type)c;} 720 721 omask = sigblock(SIGBLOCK); 722 /* 723 * sep->se_wait may be holding the pid of a daemon 724 * that we're waiting for. If so, don't overwrite 725 * it unless the config file explicitly says don't 726 * wait. 727 */ 728 if (cp->se_bi == 0 && 729 (sep->se_wait == 1 || cp->se_wait == 0)) 730 sep->se_wait = cp->se_wait; 731 SWAP(char *, sep->se_user, cp->se_user); 732 SWAP(char *, sep->se_group, cp->se_group); 733 SWAP(char *, sep->se_server, cp->se_server); 734 for (i = 0; i < MAXARGV; i++) 735 SWAP(char *, sep->se_argv[i], cp->se_argv[i]); 736 SWAP(int, cp->se_type, sep->se_type); 737 SWAP(int, cp->se_max, sep->se_max); 738#undef SWAP 739 if (isrpcservice(sep)) 740 unregister_rpc(sep); 741 sep->se_rpcversl = cp->se_rpcversl; 742 sep->se_rpcversh = cp->se_rpcversh; 743 sigsetmask(omask); 744 freeconfig(cp); 745 if (debug) 746 print_service("REDO", sep); 747 } else { 748 sep = enter(cp); 749 if (debug) 750 print_service("ADD ", sep); 751 } 752 sep->se_checked = 1; 753 754 switch (sep->se_family) { 755 case AF_UNIX: 756 if (sep->se_fd != -1) 757 break; 758 (void)unlink(sep->se_service); 759 n = strlen(sep->se_service); 760 if (n > sizeof(sep->se_ctrladdr_un.sun_path) - 1) 761 n = sizeof(sep->se_ctrladdr_un.sun_path) - 1; 762 strncpy(sep->se_ctrladdr_un.sun_path, 763 sep->se_service, n); 764 sep->se_ctrladdr_un.sun_family = AF_UNIX; 765 sep->se_ctrladdr_size = n + 766 sizeof(sep->se_ctrladdr_un) - 767 sizeof(sep->se_ctrladdr_un.sun_path); 768 if (!ISMUX(sep)) 769 setup(sep); 770 break; 771 case AF_INET: 772 sep->se_ctrladdr_in.sin_family = AF_INET; 773 if (!strcmp(sep->se_hostaddr,"*")) 774 sep->se_ctrladdr_in.sin_addr.s_addr = 775 INADDR_ANY; 776 else if (!inet_aton(sep->se_hostaddr, 777 &sep->se_ctrladdr_in.sin_addr)) { 778 /* Do we really want to support hostname lookups here? */ 779 struct hostent *hp; 780 hp = gethostbyname(sep->se_hostaddr); 781 if (hp == 0) { 782 syslog(LOG_ERR, "%s: unknown host", 783 sep->se_hostaddr); 784 sep->se_checked = 0; 785 continue; 786 } else if (hp->h_addrtype != AF_INET) { 787 syslog(LOG_ERR, 788 "%s: address isn't an Internet address", 789 sep->se_hostaddr); 790 sep->se_checked = 0; 791 continue; 792 } else if (hp->h_length != sizeof(struct in_addr)) { 793 syslog(LOG_ERR, 794 "%s: address size wrong (under DNS corruption attack?)", 795 sep->se_hostaddr); 796 sep->se_checked = 0; 797 continue; 798 } else { 799 memcpy(&sep->se_ctrladdr_in.sin_addr, 800 hp->h_addr_list[0], 801 sizeof(struct in_addr)); 802 } 803 } 804 if (ISMUX(sep)) { 805 sep->se_fd = -1; 806 continue; 807 } 808 sep->se_ctrladdr_size = sizeof(sep->se_ctrladdr_in); 809 if (isrpcservice(sep)) { 810 struct rpcent *rp; 811 812 sep->se_rpcprog = atoi(sep->se_service); 813 if (sep->se_rpcprog == 0) { 814 rp = getrpcbyname(sep->se_service); 815 if (rp == 0) { 816 syslog(LOG_ERR, 817 "%s/%s: unknown service", 818 sep->se_service, 819 sep->se_proto); 820 sep->se_checked = 0; 821 continue; 822 } 823 sep->se_rpcprog = rp->r_number; 824 } 825 if (sep->se_fd == -1 && !ISMUX(sep)) 826 setup(sep); 827 if (sep->se_fd != -1) 828 register_rpc(sep); 829 } else { 830 u_short port = htons(atoi(sep->se_service)); 831 832 if (!port) { 833 sp = getservbyname(sep->se_service, 834 sep->se_proto); 835 if (sp == 0) { 836 syslog(LOG_ERR, 837 "%s/%s: unknown service", 838 sep->se_service, 839 sep->se_proto); 840 sep->se_checked = 0; 841 continue; 842 } 843 port = sp->s_port; 844 } 845 if (port != sep->se_ctrladdr_in.sin_port) { 846 sep->se_ctrladdr_in.sin_port = port; 847 if (sep->se_fd >= 0) 848 close_sep(sep); 849 } 850 if (sep->se_fd == -1 && !ISMUX(sep)) 851 setup(sep); 852 } 853 } 854 } 855 endconfig(); 856 /* 857 * Purge anything not looked at above. 858 */ 859 omask = sigblock(SIGBLOCK); 860 sepp = &servtab; 861 while (sep = *sepp) { 862 if (sep->se_checked) { 863 sepp = &sep->se_next; 864 continue; 865 } 866 *sepp = sep->se_next; 867 if (sep->se_fd >= 0) 868 close_sep(sep); 869 if (isrpcservice(sep)) 870 unregister_rpc(sep); 871 if (sep->se_family == AF_UNIX) 872 (void)unlink(sep->se_service); 873 if (debug) 874 print_service("FREE", sep); 875 freeconfig(sep); 876 free((char *)sep); 877 } 878 (void) sigsetmask(omask); 879} 880 881void 882retry(signo) 883 int signo; 884{ 885 struct servtab *sep; 886 887 timingout = 0; 888 for (sep = servtab; sep; sep = sep->se_next) { 889 if (sep->se_fd == -1 && !ISMUX(sep)) { 890 switch (sep->se_family) { 891 case AF_UNIX: 892 case AF_INET: 893 setup(sep); 894 if (sep->se_fd != -1 && isrpcservice(sep)) 895 register_rpc(sep); 896 break; 897 } 898 } 899 } 900} 901 902void 903goaway(signo) 904 int signo; 905{ 906 register struct servtab *sep; 907 908 for (sep = servtab; sep; sep = sep->se_next) { 909 if (sep->se_fd == -1) 910 continue; 911 912 switch (sep->se_family) { 913 case AF_UNIX: 914 (void)unlink(sep->se_service); 915 break; 916 case AF_INET: 917 if (sep->se_wait == 1 && isrpcservice(sep)) 918 unregister_rpc(sep); 919 break; 920 } 921 (void)close(sep->se_fd); 922 } 923 (void)unlink(_PATH_INETDPID); 924 exit(0); 925} 926 927void 928setup(sep) 929 struct servtab *sep; 930{ 931 int on = 1; 932 933 if ((sep->se_fd = socket(sep->se_family, sep->se_socktype, 0)) < 0) { 934 if (debug) 935 fprintf(stderr, "socket failed on %s/%s: %s\n", 936 sep->se_service, sep->se_proto, strerror(errno)); 937 syslog(LOG_ERR, "%s/%s: socket: %m", 938 sep->se_service, sep->se_proto); 939 return; 940 } 941#define turnon(fd, opt) \ 942setsockopt(fd, SOL_SOCKET, opt, (char *)&on, sizeof (on)) 943 if (strcmp(sep->se_proto, "tcp") == 0 && (options & SO_DEBUG) && 944 turnon(sep->se_fd, SO_DEBUG) < 0) 945 syslog(LOG_ERR, "setsockopt (SO_DEBUG): %m"); 946 if (turnon(sep->se_fd, SO_REUSEADDR) < 0) 947 syslog(LOG_ERR, "setsockopt (SO_REUSEADDR): %m"); 948#undef turnon 949 if (bind(sep->se_fd, &sep->se_ctrladdr, sep->se_ctrladdr_size) < 0) { 950 if (debug) 951 fprintf(stderr, "bind failed on %s/%s: %s\n", 952 sep->se_service, sep->se_proto, strerror(errno)); 953 syslog(LOG_ERR, "%s/%s: bind: %m", 954 sep->se_service, sep->se_proto); 955 (void) close(sep->se_fd); 956 sep->se_fd = -1; 957 if (!timingout) { 958 timingout = 1; 959 alarm(RETRYTIME); 960 } 961 return; 962 } 963 if (sep->se_socktype == SOCK_STREAM) 964 listen(sep->se_fd, 10); 965 966 FD_SET(sep->se_fd, &allsock); 967 nsock++; 968 if (sep->se_fd > maxsock) { 969 maxsock = sep->se_fd; 970 if (maxsock > rlim_ofile_cur - FD_MARGIN) 971 bump_nofile(); 972 } 973 if (debug) 974 fprintf(stderr, "registered %s on %d\n", 975 sep->se_server, sep->se_fd); 976} 977 978/* 979 * Finish with a service and its socket. 980 */ 981void 982close_sep(sep) 983 struct servtab *sep; 984{ 985 if (sep->se_fd >= 0) { 986 nsock--; 987 FD_CLR(sep->se_fd, &allsock); 988 (void) close(sep->se_fd); 989 sep->se_fd = -1; 990 } 991 sep->se_count = 0; 992 /* 993 * Don't keep the pid of this running deamon: when reapchild() 994 * reaps this pid, it would erroneously increment nsock. 995 */ 996 if (sep->se_wait > 1) 997 sep->se_wait = 1; 998} 999 1000register_rpc(sep) 1001 register struct servtab *sep; 1002{ 1003#ifdef RPC 1004 int n; 1005 struct sockaddr_in sin; 1006 struct protoent *pp; 1007 1008 if ((pp = getprotobyname(sep->se_proto+4)) == NULL) { 1009 syslog(LOG_ERR, "%s: getproto: %m", 1010 sep->se_proto); 1011 return; 1012 } 1013 n = sizeof sin; 1014 if (getsockname(sep->se_fd, (struct sockaddr *)&sin, &n) < 0) { 1015 syslog(LOG_ERR, "%s/%s: getsockname: %m", 1016 sep->se_service, sep->se_proto); 1017 return; 1018 } 1019 1020 for (n = sep->se_rpcversl; n <= sep->se_rpcversh; n++) { 1021 if (debug) 1022 fprintf(stderr, "pmap_set: %u %u %u %u\n", 1023 sep->se_rpcprog, n, pp->p_proto, 1024 ntohs(sin.sin_port)); 1025 (void)pmap_unset(sep->se_rpcprog, n); 1026 if (!pmap_set(sep->se_rpcprog, n, pp->p_proto, ntohs(sin.sin_port))) 1027 syslog(LOG_ERR, "pmap_set: %u %u %u %u: %m", 1028 sep->se_rpcprog, n, pp->p_proto, 1029 ntohs(sin.sin_port)); 1030 } 1031#endif /* RPC */ 1032} 1033 1034unregister_rpc(sep) 1035 register struct servtab *sep; 1036{ 1037#ifdef RPC 1038 int n; 1039 1040 for (n = sep->se_rpcversl; n <= sep->se_rpcversh; n++) { 1041 if (debug) 1042 fprintf(stderr, "pmap_unset(%u, %u)\n", 1043 sep->se_rpcprog, n); 1044 if (!pmap_unset(sep->se_rpcprog, n)) 1045 syslog(LOG_ERR, "pmap_unset(%u, %u)\n", 1046 sep->se_rpcprog, n); 1047 } 1048#endif /* RPC */ 1049} 1050 1051 1052struct servtab * 1053enter(cp) 1054 struct servtab *cp; 1055{ 1056 struct servtab *sep; 1057 long omask; 1058 1059 sep = (struct servtab *)malloc(sizeof (*sep)); 1060 if (sep == (struct servtab *)0) { 1061 syslog(LOG_ERR, "Out of memory."); 1062 exit(-1); 1063 } 1064 *sep = *cp; 1065 sep->se_fd = -1; 1066 sep->se_rpcprog = -1; 1067 omask = sigblock(SIGBLOCK); 1068 sep->se_next = servtab; 1069 servtab = sep; 1070 sigsetmask(omask); 1071 return (sep); 1072} 1073 1074FILE *fconfig = NULL; 1075struct servtab serv; 1076char line[LINE_MAX]; 1077char *defhost; 1078 1079int 1080setconfig() 1081{ 1082 if (defhost) free(defhost); 1083 defhost = newstr("*"); 1084 if (fconfig != NULL) { 1085 fseek(fconfig, 0L, SEEK_SET); 1086 return (1); 1087 } 1088 fconfig = fopen(CONFIG, "r"); 1089 return (fconfig != NULL); 1090} 1091 1092void 1093endconfig() 1094{ 1095 if (fconfig) { 1096 (void) fclose(fconfig); 1097 fconfig = NULL; 1098 } 1099 if (defhost) { 1100 free(defhost); 1101 defhost = 0; 1102 } 1103} 1104 1105struct servtab * 1106getconfigent() 1107{ 1108 struct servtab *sep = &serv; 1109 int argc; 1110 char *cp, *arg; 1111 static char TCPMUX_TOKEN[] = "tcpmux/"; 1112#define MUX_LEN (sizeof(TCPMUX_TOKEN)-1) 1113 char *hostdelim; 1114 1115more: 1116#ifdef MULOG 1117 while ((cp = nextline(fconfig)) && (*cp == '#' || *cp == '\0')) { 1118 /* Avoid use of `skip' if there is a danger of it looking 1119 * at continuation lines. 1120 */ 1121 do { 1122 cp++; 1123 } while (*cp == ' ' || *cp == '\t'); 1124 if (*cp == '\0') 1125 continue; 1126 if ((arg = skip(&cp)) == NULL) 1127 continue; 1128 if (strcmp(arg, "DOMAIN")) 1129 continue; 1130 if (curdom) 1131 free(curdom); 1132 curdom = NULL; 1133 while (*cp == ' ' || *cp == '\t') 1134 cp++; 1135 if (*cp == '\0') 1136 continue; 1137 arg = cp; 1138 while (*cp && *cp != ' ' && *cp != '\t') 1139 cp++; 1140 if (*cp != '\0') 1141 *cp++ = '\0'; 1142 curdom = newstr(arg); 1143 } 1144#else 1145 while ((cp = nextline(fconfig)) && (*cp == '#' || *cp == '\0')) 1146 ; 1147#endif 1148 if (cp == NULL) 1149 return ((struct servtab *)0); 1150 /* 1151 * clear the static buffer, since some fields (se_ctrladdr, 1152 * for example) don't get initialized here. 1153 */ 1154 memset((caddr_t)sep, 0, sizeof *sep); 1155 arg = skip(&cp); 1156 if (cp == NULL) { 1157 /* got an empty line containing just blanks/tabs. */ 1158 goto more; 1159 } 1160 /* Check for a host name. */ 1161 hostdelim = strrchr(arg, ':'); 1162 if (hostdelim) { 1163 *hostdelim = '\0'; 1164 sep->se_hostaddr = newstr(arg); 1165 arg = hostdelim + 1; 1166 /* 1167 * If the line is of the form `host:', then just change the 1168 * default host for the following lines. 1169 */ 1170 if (*arg == '\0') { 1171 arg = skip(&cp); 1172 if (cp == NULL) { 1173 free(defhost); 1174 defhost = sep->se_hostaddr; 1175 goto more; 1176 } 1177 } 1178 } else 1179 sep->se_hostaddr = newstr(defhost); 1180 if (strncmp(arg, TCPMUX_TOKEN, MUX_LEN) == 0) { 1181 char *c = arg + MUX_LEN; 1182 if (*c == '+') { 1183 sep->se_type = MUXPLUS_TYPE; 1184 c++; 1185 } else 1186 sep->se_type = MUX_TYPE; 1187 sep->se_service = newstr(c); 1188 } else { 1189 sep->se_service = newstr(arg); 1190 sep->se_type = NORM_TYPE; 1191 } 1192 1193 arg = sskip(&cp); 1194 if (strcmp(arg, "stream") == 0) 1195 sep->se_socktype = SOCK_STREAM; 1196 else if (strcmp(arg, "dgram") == 0) 1197 sep->se_socktype = SOCK_DGRAM; 1198 else if (strcmp(arg, "rdm") == 0) 1199 sep->se_socktype = SOCK_RDM; 1200 else if (strcmp(arg, "seqpacket") == 0) 1201 sep->se_socktype = SOCK_SEQPACKET; 1202 else if (strcmp(arg, "raw") == 0) 1203 sep->se_socktype = SOCK_RAW; 1204 else 1205 sep->se_socktype = -1; 1206 1207 sep->se_proto = newstr(sskip(&cp)); 1208 if (strcmp(sep->se_proto, "unix") == 0) { 1209 sep->se_family = AF_UNIX; 1210 } else { 1211 sep->se_family = AF_INET; 1212 if (strncmp(sep->se_proto, "rpc/", 4) == 0) { 1213#ifdef RPC 1214 char *cp, *ccp; 1215 cp = strchr(sep->se_service, '/'); 1216 if (cp == 0) { 1217 syslog(LOG_ERR, "%s: no rpc version", 1218 sep->se_service); 1219 goto more; 1220 } 1221 *cp++ = '\0'; 1222 sep->se_rpcversl = sep->se_rpcversh = 1223 strtol(cp, &ccp, 0); 1224 if (ccp == cp) { 1225 badafterall: 1226 syslog(LOG_ERR, "%s/%s: bad rpc version", 1227 sep->se_service, cp); 1228 goto more; 1229 } 1230 if (*ccp == '-') { 1231 cp = ccp + 1; 1232 sep->se_rpcversh = strtol(cp, &ccp, 0); 1233 if (ccp == cp) 1234 goto badafterall; 1235 } 1236#else 1237 syslog(LOG_ERR, "%s: rpc services not suported", 1238 sep->se_service); 1239 goto more; 1240#endif /* RPC */ 1241 } 1242 } 1243 arg = sskip(&cp); 1244 { 1245 char *cp; 1246 cp = strchr(arg, '.'); 1247 if (cp) { 1248 *cp++ = '\0'; 1249 sep->se_max = atoi(cp); 1250 } else 1251 sep->se_max = TOOMANY; 1252 } 1253 sep->se_wait = strcmp(arg, "wait") == 0; 1254 if (ISMUX(sep)) { 1255 /* 1256 * Silently enforce "nowait" for TCPMUX services since 1257 * they don't have an assigned port to listen on. 1258 */ 1259 sep->se_wait = 0; 1260 1261 if (strcmp(sep->se_proto, "tcp")) { 1262 syslog(LOG_ERR, 1263 "%s: bad protocol for tcpmux service %s", 1264 CONFIG, sep->se_service); 1265 goto more; 1266 } 1267 if (sep->se_socktype != SOCK_STREAM) { 1268 syslog(LOG_ERR, 1269 "%s: bad socket type for tcpmux service %s", 1270 CONFIG, sep->se_service); 1271 goto more; 1272 } 1273 } 1274 sep->se_user = newstr(sskip(&cp)); 1275 if (sep->se_group = strchr(sep->se_user, '.')) 1276 *sep->se_group++ = '\0'; 1277 sep->se_server = newstr(sskip(&cp)); 1278 if (strcmp(sep->se_server, "internal") == 0) { 1279 struct biltin *bi; 1280 1281 for (bi = biltins; bi->bi_service; bi++) 1282 if (bi->bi_socktype == sep->se_socktype && 1283 strcmp(bi->bi_service, sep->se_service) == 0) 1284 break; 1285 if (bi->bi_service == 0) { 1286 syslog(LOG_ERR, "internal service %s unknown", 1287 sep->se_service); 1288 goto more; 1289 } 1290 sep->se_bi = bi; 1291 sep->se_wait = bi->bi_wait; 1292 } else 1293 sep->se_bi = NULL; 1294 argc = 0; 1295 for (arg = skip(&cp); cp; arg = skip(&cp)) { 1296#if MULOG 1297 char *colon; 1298 1299 if (argc == 0 && (colon = strrchr(arg, ':'))) { 1300 while (arg < colon) { 1301 int x; 1302 char *ccp; 1303 1304 switch (*arg++) { 1305 case 'l': 1306 x = 1; 1307 if (isdigit(*arg)) { 1308 x = strtol(arg, &ccp, 0); 1309 if (ccp == arg) 1310 break; 1311 arg = ccp; 1312 } 1313 sep->se_log &= ~MULOG_RFC931; 1314 sep->se_log |= x; 1315 break; 1316 case 'a': 1317 sep->se_log |= MULOG_RFC931; 1318 break; 1319 default: 1320 break; 1321 } 1322 } 1323 arg = colon + 1; 1324 } 1325#endif 1326 if (argc < MAXARGV) 1327 sep->se_argv[argc++] = newstr(arg); 1328 } 1329 while (argc <= MAXARGV) 1330 sep->se_argv[argc++] = NULL; 1331 return (sep); 1332} 1333 1334void 1335freeconfig(cp) 1336 struct servtab *cp; 1337{ 1338 int i; 1339 1340 if (cp->se_hostaddr) 1341 free(cp->se_hostaddr); 1342 if (cp->se_service) 1343 free(cp->se_service); 1344 if (cp->se_proto) 1345 free(cp->se_proto); 1346 if (cp->se_user) 1347 free(cp->se_user); 1348 /* Note: se_group is part of the newstr'ed se_user */ 1349 if (cp->se_server) 1350 free(cp->se_server); 1351 for (i = 0; i < MAXARGV; i++) 1352 if (cp->se_argv[i]) 1353 free(cp->se_argv[i]); 1354} 1355 1356 1357/* 1358 * Safe skip - if skip returns null, log a syntax error in the 1359 * configuration file and exit. 1360 */ 1361char * 1362sskip(cpp) 1363 char **cpp; 1364{ 1365 char *cp; 1366 1367 cp = skip(cpp); 1368 if (cp == NULL) { 1369 syslog(LOG_ERR, "%s: syntax error", CONFIG); 1370 exit(-1); 1371 } 1372 return (cp); 1373} 1374 1375char * 1376skip(cpp) 1377 char **cpp; 1378{ 1379 char *cp = *cpp; 1380 char *start; 1381 1382 if (*cpp == NULL) 1383 return ((char *)0); 1384 1385again: 1386 while (*cp == ' ' || *cp == '\t') 1387 cp++; 1388 if (*cp == '\0') { 1389 int c; 1390 1391 c = getc(fconfig); 1392 (void) ungetc(c, fconfig); 1393 if (c == ' ' || c == '\t') 1394 if (cp = nextline(fconfig)) 1395 goto again; 1396 *cpp = (char *)0; 1397 return ((char *)0); 1398 } 1399 start = cp; 1400 while (*cp && *cp != ' ' && *cp != '\t') 1401 cp++; 1402 if (*cp != '\0') 1403 *cp++ = '\0'; 1404 *cpp = cp; 1405 return (start); 1406} 1407 1408char * 1409nextline(fd) 1410 FILE *fd; 1411{ 1412 char *cp; 1413 1414 if (fgets(line, sizeof (line), fd) == NULL) 1415 return ((char *)0); 1416 cp = strchr(line, '\n'); 1417 if (cp) 1418 *cp = '\0'; 1419 return (line); 1420} 1421 1422char * 1423newstr(cp) 1424 char *cp; 1425{ 1426 if (cp = strdup(cp ? cp : "")) 1427 return (cp); 1428 syslog(LOG_ERR, "strdup: %m"); 1429 exit(-1); 1430} 1431 1432void 1433inetd_setproctitle(a, s) 1434 char *a; 1435 int s; 1436{ 1437 int size; 1438 char *cp; 1439 struct sockaddr_in sin; 1440 char buf[80]; 1441 1442 cp = Argv[0]; 1443 size = sizeof(sin); 1444 if (getpeername(s, (struct sockaddr *)&sin, &size) == 0) 1445 (void)snprintf(buf, sizeof buf, "-%s [%s]", a, 1446 inet_ntoa(sin.sin_addr)); 1447 else 1448 (void)snprintf(buf, sizeof buf, "-%s", a); 1449 strncpy(cp, buf, LastArg - cp); 1450 cp += strlen(cp); 1451 while (cp < LastArg) 1452 *cp++ = ' '; 1453} 1454 1455logpid() 1456{ 1457 FILE *fp; 1458 1459 if ((fp = fopen(_PATH_INETDPID, "w")) != NULL) { 1460 fprintf(fp, "%u\n", getpid()); 1461 (void)fclose(fp); 1462 } 1463} 1464 1465bump_nofile() 1466{ 1467#ifdef RLIMIT_NOFILE 1468 1469#define FD_CHUNK 32 1470 1471 struct rlimit rl; 1472 1473 if (getrlimit(RLIMIT_NOFILE, &rl) < 0) { 1474 syslog(LOG_ERR, "getrlimit: %m"); 1475 return -1; 1476 } 1477 rl.rlim_cur = MIN(rl.rlim_max, rl.rlim_cur + FD_CHUNK); 1478 if (rl.rlim_cur <= rlim_ofile_cur) { 1479 syslog(LOG_ERR, 1480 "bump_nofile: cannot extend file limit, max = %d", 1481 rl.rlim_cur); 1482 return -1; 1483 } 1484 1485 if (setrlimit(RLIMIT_NOFILE, &rl) < 0) { 1486 syslog(LOG_ERR, "setrlimit: %m"); 1487 return -1; 1488 } 1489 1490 rlim_ofile_cur = rl.rlim_cur; 1491 return 0; 1492 1493#else 1494 syslog(LOG_ERR, "bump_nofile: cannot extend file limit"); 1495 return -1; 1496#endif 1497} 1498 1499/* 1500 * Internet services provided internally by inetd: 1501 */ 1502#define BUFSIZE 4096 1503 1504/* ARGSUSED */ 1505void 1506echo_stream(s, sep) /* Echo service -- echo data back */ 1507 int s; 1508 struct servtab *sep; 1509{ 1510 char buffer[BUFSIZE]; 1511 int i; 1512 1513 inetd_setproctitle(sep->se_service, s); 1514 while ((i = read(s, buffer, sizeof(buffer))) > 0 && 1515 write(s, buffer, i) > 0) 1516 ; 1517 exit(0); 1518} 1519 1520/* ARGSUSED */ 1521void 1522echo_dg(s, sep) /* Echo service -- echo data back */ 1523 int s; 1524 struct servtab *sep; 1525{ 1526 char buffer[BUFSIZE]; 1527 int i, size; 1528 struct sockaddr sa; 1529 1530 size = sizeof(sa); 1531 if ((i = recvfrom(s, buffer, sizeof(buffer), 0, &sa, &size)) < 0) 1532 return; 1533 (void) sendto(s, buffer, i, 0, &sa, sizeof(sa)); 1534} 1535 1536/* ARGSUSED */ 1537void 1538discard_stream(s, sep) /* Discard service -- ignore data */ 1539 int s; 1540 struct servtab *sep; 1541{ 1542 char buffer[BUFSIZE]; 1543 1544 inetd_setproctitle(sep->se_service, s); 1545 while ((errno = 0, read(s, buffer, sizeof(buffer)) > 0) || 1546 errno == EINTR) 1547 ; 1548 exit(0); 1549} 1550 1551/* ARGSUSED */ 1552void 1553discard_dg(s, sep) /* Discard service -- ignore data */ 1554 int s; 1555 struct servtab *sep; 1556{ 1557 char buffer[BUFSIZE]; 1558 1559 (void) read(s, buffer, sizeof(buffer)); 1560} 1561 1562#include <ctype.h> 1563#define LINESIZ 72 1564char ring[128]; 1565char *endring; 1566 1567void 1568initring() 1569{ 1570 int i; 1571 1572 endring = ring; 1573 1574 for (i = 0; i <= 128; ++i) 1575 if (isprint(i)) 1576 *endring++ = i; 1577} 1578 1579/* ARGSUSED */ 1580void 1581chargen_stream(s, sep) /* Character generator */ 1582 int s; 1583 struct servtab *sep; 1584{ 1585 int len; 1586 char *rs, text[LINESIZ+2]; 1587 1588 inetd_setproctitle(sep->se_service, s); 1589 1590 if (!endring) { 1591 initring(); 1592 rs = ring; 1593 } 1594 1595 text[LINESIZ] = '\r'; 1596 text[LINESIZ + 1] = '\n'; 1597 for (rs = ring;;) { 1598 if ((len = endring - rs) >= LINESIZ) 1599 memmove(text, rs, LINESIZ); 1600 else { 1601 memmove(text, rs, len); 1602 memmove(text + len, ring, LINESIZ - len); 1603 } 1604 if (++rs == endring) 1605 rs = ring; 1606 if (write(s, text, sizeof(text)) != sizeof(text)) 1607 break; 1608 } 1609 exit(0); 1610} 1611 1612/* ARGSUSED */ 1613void 1614chargen_dg(s, sep) /* Character generator */ 1615 int s; 1616 struct servtab *sep; 1617{ 1618 struct sockaddr sa; 1619 static char *rs; 1620 int len, size; 1621 char text[LINESIZ+2]; 1622 1623 if (endring == 0) { 1624 initring(); 1625 rs = ring; 1626 } 1627 1628 size = sizeof(sa); 1629 if (recvfrom(s, text, sizeof(text), 0, &sa, &size) < 0) 1630 return; 1631 1632 if ((len = endring - rs) >= LINESIZ) 1633 memmove(text, rs, LINESIZ); 1634 else { 1635 memmove(text, rs, len); 1636 memmove(text + len, ring, LINESIZ - len); 1637 } 1638 if (++rs == endring) 1639 rs = ring; 1640 text[LINESIZ] = '\r'; 1641 text[LINESIZ + 1] = '\n'; 1642 (void) sendto(s, text, sizeof(text), 0, &sa, sizeof(sa)); 1643} 1644 1645/* 1646 * Return a machine readable date and time, in the form of the 1647 * number of seconds since midnight, Jan 1, 1900. Since gettimeofday 1648 * returns the number of seconds since midnight, Jan 1, 1970, 1649 * we must add 2208988800 seconds to this figure to make up for 1650 * some seventy years Bell Labs was asleep. 1651 */ 1652 1653long 1654machtime() 1655{ 1656 struct timeval tv; 1657 1658 if (gettimeofday(&tv, (struct timezone *)0) < 0) { 1659 if (debug) 1660 fprintf(stderr, "Unable to get time of day\n"); 1661 return (0L); 1662 } 1663#define OFFSET ((u_long)25567 * 24*60*60) 1664 return (htonl((long)(tv.tv_sec + OFFSET))); 1665#undef OFFSET 1666} 1667 1668/* ARGSUSED */ 1669void 1670machtime_stream(s, sep) 1671 int s; 1672 struct servtab *sep; 1673{ 1674 long result; 1675 1676 result = machtime(); 1677 (void) write(s, (char *) &result, sizeof(result)); 1678} 1679 1680/* ARGSUSED */ 1681void 1682machtime_dg(s, sep) 1683 int s; 1684 struct servtab *sep; 1685{ 1686 long result; 1687 struct sockaddr sa; 1688 int size; 1689 1690 size = sizeof(sa); 1691 if (recvfrom(s, (char *)&result, sizeof(result), 0, &sa, &size) < 0) 1692 return; 1693 result = machtime(); 1694 (void) sendto(s, (char *) &result, sizeof(result), 0, &sa, sizeof(sa)); 1695} 1696 1697/* ARGSUSED */ 1698void 1699daytime_stream(s, sep) /* Return human-readable time of day */ 1700 int s; 1701 struct servtab *sep; 1702{ 1703 char buffer[256]; 1704 time_t clock; 1705 int len; 1706 1707 clock = time((time_t *) 0); 1708 1709 len = snprintf(buffer, sizeof buffer, "%.24s\r\n", ctime(&clock)); 1710 (void) write(s, buffer, len); 1711} 1712 1713/* ARGSUSED */ 1714void 1715daytime_dg(s, sep) /* Return human-readable time of day */ 1716 int s; 1717 struct servtab *sep; 1718{ 1719 char buffer[256]; 1720 time_t clock; 1721 struct sockaddr sa; 1722 int size, len; 1723 1724 clock = time((time_t *) 0); 1725 1726 size = sizeof(sa); 1727 if (recvfrom(s, buffer, sizeof(buffer), 0, &sa, &size) < 0) 1728 return; 1729 len = snprintf(buffer, sizeof buffer, "%.24s\r\n", ctime(&clock)); 1730 (void) sendto(s, buffer, len, 0, &sa, sizeof(sa)); 1731} 1732 1733/* 1734 * print_service: 1735 * Dump relevant information to stderr 1736 */ 1737void 1738print_service(action, sep) 1739 char *action; 1740 struct servtab *sep; 1741{ 1742 if (isrpcservice(sep)) 1743 fprintf(stderr, 1744 "%s: %s rpcprog=%d, rpcvers = %d/%d, proto=%s, wait.max=%d.%d, user.group=%s.%s builtin=%lx server=%s\n", 1745 action, sep->se_service, 1746 sep->se_rpcprog, sep->se_rpcversh, sep->se_rpcversl, sep->se_proto, 1747 sep->se_wait, sep->se_max, sep->se_user, sep->se_group, 1748 (long)sep->se_bi, sep->se_server); 1749 else 1750 fprintf(stderr, 1751 "%s: %s proto=%s, wait.max=%d.%d, user.group=%s.%s builtin=%lx server=%s\n", 1752 action, sep->se_service, sep->se_proto, 1753 sep->se_wait, sep->se_max, sep->se_user, sep->se_group, 1754 (long)sep->se_bi, sep->se_server); 1755} 1756 1757void 1758usage() 1759{ 1760 1761#ifdef LIBWRAP 1762 (void)fprintf(stderr, "usage: %s [-dl] [conf]\n", __progname); 1763#else 1764 (void)fprintf(stderr, "usage: %s [-d] [conf]\n", __progname); 1765#endif 1766 exit(1); 1767} 1768 1769 1770/* 1771 * Based on TCPMUX.C by Mark K. Lottor November 1988 1772 * sri-nic::ps:<mkl>tcpmux.c 1773 */ 1774 1775static int /* # of characters upto \r,\n or \0 */ 1776getline(fd, buf, len) 1777 int fd; 1778 char *buf; 1779 int len; 1780{ 1781 int count = 0, n; 1782 1783 do { 1784 n = read(fd, buf, len-count); 1785 if (n == 0) 1786 return (count); 1787 if (n < 0) 1788 return (-1); 1789 while (--n >= 0) { 1790 if (*buf == '\r' || *buf == '\n' || *buf == '\0') 1791 return (count); 1792 count++; 1793 buf++; 1794 } 1795 } while (count < len); 1796 return (count); 1797} 1798 1799#define MAX_SERV_LEN (256+2) /* 2 bytes for \r\n */ 1800 1801#define strwrite(fd, buf) (void) write(fd, buf, sizeof(buf)-1) 1802 1803struct servtab * 1804tcpmux(s) 1805 int s; 1806{ 1807 struct servtab *sep; 1808 char service[MAX_SERV_LEN+1]; 1809 int len; 1810 1811 /* Get requested service name */ 1812 if ((len = getline(s, service, MAX_SERV_LEN)) < 0) { 1813 strwrite(s, "-Error reading service name\r\n"); 1814 return (NULL); 1815 } 1816 service[len] = '\0'; 1817 1818 if (debug) 1819 fprintf(stderr, "tcpmux: someone wants %s\n", service); 1820 1821 /* 1822 * Help is a required command, and lists available services, 1823 * one per line. 1824 */ 1825 if (!strcasecmp(service, "help")) { 1826 for (sep = servtab; sep; sep = sep->se_next) { 1827 if (!ISMUX(sep)) 1828 continue; 1829 (void)write(s,sep->se_service,strlen(sep->se_service)); 1830 strwrite(s, "\r\n"); 1831 } 1832 return (NULL); 1833 } 1834 1835 /* Try matching a service in inetd.conf with the request */ 1836 for (sep = servtab; sep; sep = sep->se_next) { 1837 if (!ISMUX(sep)) 1838 continue; 1839 if (!strcasecmp(service, sep->se_service)) { 1840 if (ISMUXPLUS(sep)) { 1841 strwrite(s, "+Go\r\n"); 1842 } 1843 return (sep); 1844 } 1845 } 1846 strwrite(s, "-Service not available\r\n"); 1847 return (NULL); 1848} 1849 1850 1851#ifdef MULOG 1852dolog(sep, ctrl) 1853 struct servtab *sep; 1854 int ctrl; 1855{ 1856 struct sockaddr sa; 1857 struct sockaddr_in *sin = (struct sockaddr_in *)&sa; 1858 int len = sizeof(sa); 1859 struct hostent *hp; 1860 char *host, *dp, buf[BUFSIZ], *rfc931_name(); 1861 int connected = 1; 1862 1863 if (sep->se_family != AF_INET) 1864 return; 1865 1866 if (getpeername(ctrl, &sa, &len) < 0) { 1867 if (errno != ENOTCONN) { 1868 syslog(LOG_ERR, "getpeername: %m"); 1869 return; 1870 } 1871 if (recvfrom(ctrl, buf, sizeof(buf), MSG_PEEK, &sa, &len) < 0) { 1872 syslog(LOG_ERR, "recvfrom: %m"); 1873 return; 1874 } 1875 connected = 0; 1876 } 1877 if (sa.sa_family != AF_INET) { 1878 syslog(LOG_ERR, "unexpected address family %u", sa.sa_family); 1879 return; 1880 } 1881 1882 hp = gethostbyaddr((char *) &sin->sin_addr.s_addr, 1883 sizeof (sin->sin_addr.s_addr), AF_INET); 1884 1885 host = hp?hp->h_name:inet_ntoa(sin->sin_addr); 1886 1887 switch (sep->se_log & ~MULOG_RFC931) { 1888 case 0: 1889 return; 1890 case 1: 1891 if (curdom == NULL || *curdom == '\0') 1892 break; 1893 dp = host + strlen(host) - strlen(curdom); 1894 if (dp < host) 1895 break; 1896 if (debug) 1897 fprintf(stderr, "check \"%s\" against curdom \"%s\"\n", 1898 host, curdom); 1899 if (strcasecmp(dp, curdom) == 0) 1900 return; 1901 break; 1902 case 2: 1903 default: 1904 break; 1905 } 1906 1907 openlog("", LOG_NOWAIT, MULOG); 1908 1909 if (connected && (sep->se_log & MULOG_RFC931)) 1910 syslog(LOG_INFO, "%s@%s wants %s", 1911 rfc931_name(sin, ctrl), host, sep->se_service); 1912 else 1913 syslog(LOG_INFO, "%s wants %s", 1914 host, sep->se_service); 1915} 1916 1917/* 1918 * From tcp_log by 1919 * Wietse Venema, Eindhoven University of Technology, The Netherlands. 1920 */ 1921#if 0 1922static char sccsid[] = "@(#) rfc931.c 1.3 92/08/31 22:54:46"; 1923#endif 1924 1925#include <setjmp.h> 1926 1927#define RFC931_PORT 113 /* Semi-well-known port */ 1928#define TIMEOUT 4 1929#define TIMEOUT2 10 1930 1931static jmp_buf timebuf; 1932 1933/* timeout - handle timeouts */ 1934 1935static void timeout(sig) 1936int sig; 1937{ 1938 longjmp(timebuf, sig); 1939} 1940 1941/* rfc931_name - return remote user name */ 1942 1943char * 1944rfc931_name(there, ctrl) 1945struct sockaddr_in *there; /* remote link information */ 1946int ctrl; 1947{ 1948 struct sockaddr_in here; /* local link information */ 1949 struct sockaddr_in sin; /* for talking to RFC931 daemon */ 1950 int length; 1951 int s; 1952 unsigned remote; 1953 unsigned local; 1954 static char user[256]; /* XXX */ 1955 char buf[256]; 1956 char *cp; 1957 char *result = "USER_UNKNOWN"; 1958 int len; 1959 1960 /* Find out local port number of our stdin. */ 1961 1962 length = sizeof(here); 1963 if (getsockname(ctrl, (struct sockaddr *) &here, &length) == -1) { 1964 syslog(LOG_ERR, "getsockname: %m"); 1965 return (result); 1966 } 1967 /* Set up timer so we won't get stuck. */ 1968 1969 if ((s = socket(AF_INET, SOCK_STREAM, 0)) == -1) { 1970 syslog(LOG_ERR, "socket: %m"); 1971 return (result); 1972 } 1973 1974 sin = here; 1975 sin.sin_port = htons(0); 1976 if (bind(s, (struct sockaddr *) &sin, sizeof(sin)) == -1) { 1977 syslog(LOG_ERR, "bind: %m"); 1978 return (result); 1979 } 1980 1981 signal(SIGALRM, timeout); 1982 if (setjmp(timebuf)) { 1983 close(s); /* not: fclose(fp) */ 1984 return (result); 1985 } 1986 alarm(TIMEOUT); 1987 1988 /* Connect to the RFC931 daemon. */ 1989 1990 sin = *there; 1991 sin.sin_port = htons(RFC931_PORT); 1992 if (connect(s, (struct sockaddr *) &sin, sizeof(sin)) == -1) { 1993 close(s); 1994 alarm(0); 1995 return (result); 1996 } 1997 1998 /* Query the RFC 931 server. Would 13-byte writes ever be broken up? */ 1999 (void)snprintf(buf, sizeof buf, "%u,%u\r\n", ntohs(there->sin_port), 2000 ntohs(here.sin_port)); 2001 2002 2003 for (len = 0, cp = buf; len < strlen(buf); ) { 2004 int n; 2005 2006 if ((n = write(s, cp, strlen(buf) - len)) == -1) { 2007 close(s); 2008 alarm(0); 2009 return (result); 2010 } 2011 cp += n; 2012 len += n; 2013 } 2014 2015 /* Read response */ 2016 for (cp = buf; cp < buf + sizeof(buf) - 1; ) { 2017 char c; 2018 if (read(s, &c, 1) != 1) { 2019 close(s); 2020 alarm(0); 2021 return (result); 2022 } 2023 if (c == '\n') 2024 break; 2025 *cp++ = c; 2026 } 2027 *cp = '\0'; 2028 2029 if (sscanf(buf, "%u , %u : USERID :%*[^:]:%255s", &remote, &local, user) == 3 2030 && ntohs(there->sin_port) == remote 2031 && ntohs(here.sin_port) == local) { 2032 2033 /* Strip trailing carriage return. */ 2034 if (cp = strchr(user, '\r')) 2035 *cp = 0; 2036 result = user; 2037 } 2038 2039 alarm(0); 2040 close(s); 2041 return (result); 2042} 2043#endif 2044