builtins.c revision 72650
1/*- 2 * Copyright (c) 1983, 1991, 1993, 1994 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 * 26 * $FreeBSD: head/usr.sbin/inetd/builtins.c 72650 2001-02-18 13:30:20Z green $ 27 * 28 */ 29 30#include <sys/filio.h> 31#include <sys/ioccom.h> 32#include <sys/param.h> 33#include <sys/stat.h> 34#include <sys/socket.h> 35#include <sys/sysctl.h> 36#include <sys/ucred.h> 37#include <sys/uio.h> 38#include <sys/utsname.h> 39 40#include <ctype.h> 41#include <err.h> 42#include <errno.h> 43#include <fcntl.h> 44#include <limits.h> 45#include <pwd.h> 46#include <signal.h> 47#include <stdlib.h> 48#include <string.h> 49#include <sysexits.h> 50#include <syslog.h> 51#include <unistd.h> 52 53#include "inetd.h" 54 55extern int debug; 56extern struct servtab *servtab; 57 58char ring[128]; 59char *endring; 60 61 62struct biltin biltins[] = { 63 /* Echo received data */ 64 { "echo", SOCK_STREAM, 1, -1, echo_stream }, 65 { "echo", SOCK_DGRAM, 0, 1, echo_dg }, 66 67 /* Internet /dev/null */ 68 { "discard", SOCK_STREAM, 1, -1, discard_stream }, 69 { "discard", SOCK_DGRAM, 0, 1, discard_dg }, 70 71 /* Return 32 bit time since 1970 */ 72 { "time", SOCK_STREAM, 0, -1, machtime_stream }, 73 { "time", SOCK_DGRAM, 0, 1, machtime_dg }, 74 75 /* Return human-readable time */ 76 { "daytime", SOCK_STREAM, 0, -1, daytime_stream }, 77 { "daytime", SOCK_DGRAM, 0, 1, daytime_dg }, 78 79 /* Familiar character generator */ 80 { "chargen", SOCK_STREAM, 1, -1, chargen_stream }, 81 { "chargen", SOCK_DGRAM, 0, 1, chargen_dg }, 82 83 { "tcpmux", SOCK_STREAM, 1, -1, (void (*)())tcpmux }, 84 85 { "auth", SOCK_STREAM, 1, -1, ident_stream }, 86 87 { NULL } 88}; 89 90/* 91 * RFC864 Character Generator Protocol. Generates character data without 92 * any regard for input. 93 */ 94 95void 96initring() 97{ 98 int i; 99 100 endring = ring; 101 102 for (i = 0; i <= 128; ++i) 103 if (isprint(i)) 104 *endring++ = i; 105} 106 107/* ARGSUSED */ 108void 109chargen_dg(s, sep) /* Character generator */ 110 int s; 111 struct servtab *sep; 112{ 113 struct sockaddr_storage ss; 114 static char *rs; 115 int len; 116 socklen_t size; 117 char text[LINESIZ+2]; 118 119 if (endring == 0) { 120 initring(); 121 rs = ring; 122 } 123 124 size = sizeof(ss); 125 if (recvfrom(s, text, sizeof(text), 0, 126 (struct sockaddr *)&ss, &size) < 0) 127 return; 128 129 if (check_loop((struct sockaddr *)&ss, sep)) 130 return; 131 132 if ((len = endring - rs) >= LINESIZ) 133 memmove(text, rs, LINESIZ); 134 else { 135 memmove(text, rs, len); 136 memmove(text + len, ring, LINESIZ - len); 137 } 138 if (++rs == endring) 139 rs = ring; 140 text[LINESIZ] = '\r'; 141 text[LINESIZ + 1] = '\n'; 142 (void) sendto(s, text, sizeof(text), 0, (struct sockaddr *)&ss, size); 143} 144 145/* ARGSUSED */ 146void 147chargen_stream(s, sep) /* Character generator */ 148 int s; 149 struct servtab *sep; 150{ 151 int len; 152 char *rs, text[LINESIZ+2]; 153 154 inetd_setproctitle(sep->se_service, s); 155 156 if (!endring) { 157 initring(); 158 rs = ring; 159 } 160 161 text[LINESIZ] = '\r'; 162 text[LINESIZ + 1] = '\n'; 163 for (rs = ring;;) { 164 if ((len = endring - rs) >= LINESIZ) 165 memmove(text, rs, LINESIZ); 166 else { 167 memmove(text, rs, len); 168 memmove(text + len, ring, LINESIZ - len); 169 } 170 if (++rs == endring) 171 rs = ring; 172 if (write(s, text, sizeof(text)) != sizeof(text)) 173 break; 174 } 175 exit(0); 176} 177 178/* 179 * RFC867 Daytime Protocol. Sends the current date and time as an ascii 180 * character string without any regard for input. 181 */ 182 183/* ARGSUSED */ 184void 185daytime_dg(s, sep) /* Return human-readable time of day */ 186 int s; 187 struct servtab *sep; 188{ 189 char buffer[256]; 190 time_t clock; 191 struct sockaddr_storage ss; 192 socklen_t size; 193 194 clock = time((time_t *) 0); 195 196 size = sizeof(ss); 197 if (recvfrom(s, buffer, sizeof(buffer), 0, 198 (struct sockaddr *)&ss, &size) < 0) 199 return; 200 201 if (check_loop((struct sockaddr *)&ss, sep)) 202 return; 203 204 (void) sprintf(buffer, "%.24s\r\n", ctime(&clock)); 205 (void) sendto(s, buffer, strlen(buffer), 0, 206 (struct sockaddr *)&ss, size); 207} 208 209/* ARGSUSED */ 210void 211daytime_stream(s, sep) /* Return human-readable time of day */ 212 int s; 213 struct servtab *sep; 214{ 215 char buffer[256]; 216 time_t clock; 217 218 clock = time((time_t *) 0); 219 220 (void) sprintf(buffer, "%.24s\r\n", ctime(&clock)); 221 (void) send(s, buffer, strlen(buffer), MSG_EOF); 222} 223 224/* 225 * RFC863 Discard Protocol. Any data received is thrown away and no response 226 * is sent. 227 */ 228 229/* ARGSUSED */ 230void 231discard_dg(s, sep) /* Discard service -- ignore data */ 232 int s; 233 struct servtab *sep; 234{ 235 char buffer[BUFSIZE]; 236 237 (void) read(s, buffer, sizeof(buffer)); 238} 239 240/* ARGSUSED */ 241void 242discard_stream(s, sep) /* Discard service -- ignore data */ 243 int s; 244 struct servtab *sep; 245{ 246 int ret; 247 char buffer[BUFSIZE]; 248 249 inetd_setproctitle(sep->se_service, s); 250 while (1) { 251 while ((ret = read(s, buffer, sizeof(buffer))) > 0) 252 ; 253 if (ret == 0 || errno != EINTR) 254 break; 255 } 256 exit(0); 257} 258 259/* 260 * RFC862 Echo Protocol. Any data received is sent back to the sender as 261 * received. 262 */ 263 264/* ARGSUSED */ 265void 266echo_dg(s, sep) /* Echo service -- echo data back */ 267 int s; 268 struct servtab *sep; 269{ 270 char buffer[BUFSIZE]; 271 int i; 272 socklen_t size; 273 struct sockaddr_storage ss; 274 275 size = sizeof(ss); 276 if ((i = recvfrom(s, buffer, sizeof(buffer), 0, 277 (struct sockaddr *)&ss, &size)) < 0) 278 return; 279 280 if (check_loop((struct sockaddr *)&ss, sep)) 281 return; 282 283 (void) sendto(s, buffer, i, 0, (struct sockaddr *)&ss, size); 284} 285 286/* ARGSUSED */ 287void 288echo_stream(s, sep) /* Echo service -- echo data back */ 289 int s; 290 struct servtab *sep; 291{ 292 char buffer[BUFSIZE]; 293 int i; 294 295 inetd_setproctitle(sep->se_service, s); 296 while ((i = read(s, buffer, sizeof(buffer))) > 0 && 297 write(s, buffer, i) > 0) 298 ; 299 exit(0); 300} 301 302/* 303 * RFC1413 Identification Protocol. Given a TCP port number pair, return a 304 * character string which identifies the owner of that connection on the 305 * server's system. Extended to allow for ~/.fakeid support and ~/.noident 306 * support. 307 */ 308 309/* ARGSUSED */ 310void 311iderror(lport, fport, s, er) /* Generic ident_stream error-sending func */ 312 int lport, fport, s, er; 313{ 314 char *p; 315 316 asprintf(&p, "%d , %d : ERROR : %s\r\n", lport, fport, 317 er == -1 ? "HIDDEN-USER" : er ? strerror(er) : "UNKNOWN-ERROR"); 318 if (p == NULL) { 319 syslog(LOG_ERR, "asprintf: %m"); 320 exit(EX_OSERR); 321 } 322 send(s, p, strlen(p), MSG_EOF); 323 free(p); 324 325 exit(0); 326} 327 328/* ARGSUSED */ 329void 330ident_stream(s, sep) /* Ident service (AKA "auth") */ 331 int s; 332 struct servtab *sep; 333{ 334 struct utsname un; 335 struct stat sb; 336 struct sockaddr_in sin[2]; 337#ifdef INET6 338 struct sockaddr_in6 sin6[2]; 339#endif 340 struct sockaddr_storage ss[2]; 341 struct xucred uc; 342 struct timeval tv = { 343 10, 344 0 345 }, to; 346 struct passwd *pw = NULL; 347 fd_set fdset; 348 char buf[BUFSIZE], *cp = NULL, *p, **av, *osname = NULL, garbage[7], e; 349 char *fallback = NULL; 350 socklen_t socklen; 351 ssize_t ssize; 352 size_t size, bufsiz; 353 int c, fflag = 0, nflag = 0, rflag = 0, argc = 0, usedfallback = 0; 354 int gflag = 0, Fflag = 0, getcredfail = 0, onreadlen; 355 u_short lport, fport; 356 357 inetd_setproctitle(sep->se_service, s); 358 /* 359 * Reset getopt() since we are a fork() but not an exec() from 360 * a parent which used getopt() already. 361 */ 362 optind = 1; 363 optreset = 1; 364 /* 365 * Take the internal argument vector and count it out to make an 366 * argument count for getopt. This can be used for any internal 367 * service to read arguments and use getopt() easily. 368 */ 369 for (av = sep->se_argv; *av; av++) 370 argc++; 371 if (argc) { 372 int sec, usec; 373 size_t i; 374 u_int32_t random; 375 376 while ((c = getopt(argc, sep->se_argv, "d:fFgno:rt:")) != -1) 377 switch (c) { 378 case 'd': 379 fallback = optarg; 380 break; 381 case 'f': 382 fflag = 1; 383 break; 384 case 'F': 385 fflag = 1; 386 Fflag=1; 387 break; 388 case 'g': 389 gflag = 1; 390 random = 0; /* Shush, compiler. */ 391 /* 392 * The number of bits in "random" divided 393 * by the number of bits needed per iteration 394 * gives a more optimal way to reload the 395 * random number only when necessary. 396 * 397 * I'm using base-36, so I need at least 6 398 * bits; round it up to 8 bits to make it 399 * easier. 400 */ 401 for (i = 0; i < sizeof(garbage) - 1; i++) { 402 const char *const base36 = 403 "0123456789" 404 "abcdefghijklmnopqrstuvwxyz"; 405 if (i % (sizeof(random) * 8 / 8) == 0) 406 random = arc4random(); 407 garbage[i] = 408 base36[(random & 0xff) % 36]; 409 random >>= 8; 410 } 411 garbage[i] = '\0'; 412 break; 413 case 'n': 414 nflag = 1; 415 break; 416 case 'o': 417 osname = optarg; 418 break; 419 case 'r': 420 rflag = 1; 421 break; 422 case 't': 423 switch (sscanf(optarg, "%d.%d", &sec, &usec)) { 424 case 2: 425 tv.tv_usec = usec; 426 case 1: 427 tv.tv_sec = sec; 428 break; 429 default: 430 if (debug) 431 warnx("bad -t argument"); 432 break; 433 } 434 break; 435 default: 436 break; 437 } 438 } 439 if (osname == NULL) { 440 if (uname(&un) == -1) 441 iderror(0, 0, s, errno); 442 osname = un.sysname; 443 } 444 socklen = sizeof(ss[0]); 445 if (getsockname(s, (struct sockaddr *)&ss[0], &socklen) == -1) 446 iderror(0, 0, s, errno); 447 socklen = sizeof(ss[1]); 448 if (getpeername(s, (struct sockaddr *)&ss[1], &socklen) == -1) 449 iderror(0, 0, s, errno); 450 /* 451 * We're going to prepare for and execute reception of a 452 * packet of data from the user. The data is in the format 453 * "local_port , foreign_port\r\n" (with local being the 454 * server's port and foreign being the client's.) 455 */ 456 gettimeofday(&to, NULL); 457 to.tv_sec += tv.tv_sec; 458 to.tv_usec += tv.tv_usec; 459 if (to.tv_usec >= 1000000) { 460 to.tv_usec -= 1000000; 461 to.tv_sec++; 462 } 463 464 size = 0; 465 bufsiz = sizeof(buf) - 1; 466 FD_ZERO(&fdset); 467 while (bufsiz > 0 && (size == 0 || buf[size - 1] != '\n')) { 468 gettimeofday(&tv, NULL); 469 tv.tv_sec = to.tv_sec - tv.tv_sec; 470 tv.tv_usec = to.tv_usec - tv.tv_usec; 471 if (tv.tv_usec < 0) { 472 tv.tv_usec += 1000000; 473 tv.tv_sec--; 474 } 475 if (tv.tv_sec < 0) 476 break; 477 FD_SET(s, &fdset); 478 if (select(s + 1, &fdset, NULL, NULL, &tv) == -1) 479 iderror(0, 0, s, errno); 480 if (ioctl(s, FIONREAD, &onreadlen) == -1) 481 iderror(0, 0, s, errno); 482 if (onreadlen > bufsiz) 483 onreadlen = bufsiz; 484 ssize = read(s, &buf[size], (size_t)onreadlen); 485 if (ssize == -1) 486 iderror(0, 0, s, errno); 487 else if (ssize == 0) 488 break; 489 bufsiz -= ssize; 490 size += ssize; 491 } 492 buf[size] = '\0'; 493 /* Read two characters, and check for a delimiting character */ 494 if (sscanf(buf, "%hu , %hu%c", &lport, &fport, &e) != 3 || isdigit(e)) 495 iderror(0, 0, s, 0); 496 if (gflag) { 497 cp = garbage; 498 goto printit; 499 } 500 501 /* 502 * If not "real" (-r), send a HIDDEN-USER error for everything. 503 * If -d is used to set a fallback username, this is used to 504 * override it, and the fallback is returned instead. 505 */ 506 if (!rflag) { 507 if (fallback == NULL) 508 iderror(lport, fport, s, -1); 509 else { 510 cp = fallback; 511 goto printit; 512 } 513 } 514 515 /* 516 * We take the input and construct an array of two sockaddr_ins 517 * which contain the local address information and foreign 518 * address information, respectively, used to look up the 519 * credentials for the socket (which are returned by the 520 * sysctl "net.inet.tcp.getcred" when we call it.) The 521 * arrays have been filled in above via get{peer,sock}name(), 522 * so right here we are only setting the ports. 523 */ 524 if (ss[0].ss_family != ss[1].ss_family) 525 iderror(lport, fport, s, EINVAL); 526 size = sizeof(uc); 527 switch (ss[0].ss_family) { 528 case AF_INET: 529 sin[0] = *(struct sockaddr_in *)&ss[0]; 530 sin[0].sin_port = htons(lport); 531 sin[1] = *(struct sockaddr_in *)&ss[1]; 532 sin[1].sin_port = htons(fport); 533 if (sysctlbyname("net.inet.tcp.getcred", &uc, &size, sin, 534 sizeof(sin)) == -1) 535 getcredfail = errno; 536 break; 537#ifdef INET6 538 case AF_INET6: 539 sin6[0] = *(struct sockaddr_in6 *)&ss[0]; 540 sin6[0].sin6_port = htons(lport); 541 sin6[1] = *(struct sockaddr_in6 *)&ss[1]; 542 sin6[1].sin6_port = htons(fport); 543 if (sysctlbyname("net.inet6.tcp6.getcred", &uc, &size, sin6, 544 sizeof(sin6)) == -1) 545 getcredfail = errno; 546 break; 547#endif 548 default: /* should not reach here */ 549 getcredfail = EAFNOSUPPORT; 550 break; 551 } 552 if (getcredfail != 0) { 553 if (fallback == NULL) /* Use a default, if asked to */ 554 iderror(lport, fport, s, getcredfail); 555 usedfallback = 1; 556 } else { 557 /* Look up the pw to get the username */ 558 errno = 0; 559 pw = getpwuid(uc.cr_uid); 560 } 561 if (pw == NULL && !usedfallback) /* No such user... */ 562 iderror(lport, fport, s, errno != 0 ? errno : ENOENT); 563 /* 564 * If enabled, we check for a file named ".noident" in the user's 565 * home directory. If found, we return HIDDEN-USER. 566 */ 567 if (nflag && !usedfallback) { 568 if (asprintf(&p, "%s/.noident", pw->pw_dir) == -1) 569 iderror(lport, fport, s, errno); 570 if (lstat(p, &sb) == 0) { 571 free(p); 572 iderror(lport, fport, s, -1); 573 } 574 free(p); 575 } 576 /* 577 * Here, if enabled, we read a user's ".fakeid" file in their 578 * home directory. It consists of a line containing the name 579 * they want. 580 */ 581 if (fflag && !usedfallback) { 582 FILE *fakeid = NULL; 583 int fakeid_fd; 584 585 if (asprintf(&p, "%s/.fakeid", pw->pw_dir) == -1) 586 iderror(lport, fport, s, errno); 587 /* 588 * Here we set ourself to effectively be the user, so we don't 589 * open any files we have no permission to open, especially 590 * symbolic links to sensitive root-owned files or devices. 591 */ 592 if (initgroups(pw->pw_name, pw->pw_gid) == -1) 593 iderror(lport, fport, s, errno); 594 seteuid(pw->pw_uid); 595 /* 596 * We can't stat() here since that would be a race 597 * condition. 598 * Therefore, we open the file we have permissions to open 599 * and if it's not a regular file, we close it and end up 600 * returning the user's real username. 601 */ 602 fakeid_fd = open(p, O_RDONLY | O_NONBLOCK); 603 free(p); 604 if (fakeid_fd != -1 && fstat(fakeid_fd, &sb) != -1 && 605 S_ISREG(sb.st_mode) && 606 (fakeid = fdopen(fakeid_fd, "r")) != NULL) { 607 buf[sizeof(buf) - 1] = '\0'; 608 if (fgets(buf, sizeof(buf), fakeid) == NULL) { 609 cp = pw->pw_name; 610 fclose(fakeid); 611 goto printit; 612 } 613 /* 614 * Usually, the file will have the desired identity 615 * in the form "identity\n", so we use strcspn() to 616 * end the string (which fgets() doesn't do.) 617 */ 618 buf[strcspn(buf, "\r\n")] = '\0'; 619 cp = buf; 620 /* Allow for beginning white space... */ 621 while (isspace(*cp)) 622 cp++; 623 /* ...and ending white space. */ 624 cp[strcspn(cp, " \t")] = '\0'; 625 /* User names of >16 characters are invalid */ 626 if (strlen(cp) > 16) 627 cp[16] = '\0'; 628 /* 629 * If the name is a zero-length string or matches 630 * the name of another user, it's invalid, so 631 * we will return their real identity instead. 632 */ 633 634 if (!*cp || (!Fflag && getpwnam(cp))) { 635 errno = 0; 636 pw = getpwuid(uc.cr_uid); 637 if (pw == NULL) 638 iderror(lport, fport, s, 639 errno != 0 ? errno : ENOENT); 640 cp = pw->pw_name; 641 } 642 } else 643 cp = pw->pw_name; 644 if (fakeid != NULL) 645 fclose(fakeid); 646 else if (fakeid_fd != -1) 647 close(fakeid_fd); 648 } else if (!usedfallback) 649 cp = pw->pw_name; 650 else 651 cp = fallback; 652printit: 653 /* Finally, we make and send the reply. */ 654 if (asprintf(&p, "%d , %d : USERID : %s : %s\r\n", lport, fport, osname, 655 cp) == -1) { 656 syslog(LOG_ERR, "asprintf: %m"); 657 exit(EX_OSERR); 658 } 659 send(s, p, strlen(p), MSG_EOF); 660 free(p); 661 662 exit(0); 663} 664 665/* 666 * RFC738 Time Server. 667 * Return a machine readable date and time, in the form of the 668 * number of seconds since midnight, Jan 1, 1900. Since gettimeofday 669 * returns the number of seconds since midnight, Jan 1, 1970, 670 * we must add 2208988800 seconds to this figure to make up for 671 * some seventy years Bell Labs was asleep. 672 */ 673 674unsigned long 675machtime() 676{ 677 struct timeval tv; 678 679 if (gettimeofday(&tv, (struct timezone *)NULL) < 0) { 680 if (debug) 681 warnx("unable to get time of day"); 682 return (0L); 683 } 684#define OFFSET ((u_long)25567 * 24*60*60) 685 return (htonl((long)(tv.tv_sec + OFFSET))); 686#undef OFFSET 687} 688 689/* ARGSUSED */ 690void 691machtime_dg(s, sep) 692 int s; 693 struct servtab *sep; 694{ 695 unsigned long result; 696 struct sockaddr_storage ss; 697 socklen_t size; 698 699 size = sizeof(ss); 700 if (recvfrom(s, (char *)&result, sizeof(result), 0, 701 (struct sockaddr *)&ss, &size) < 0) 702 return; 703 704 if (check_loop((struct sockaddr *)&ss, sep)) 705 return; 706 707 result = machtime(); 708 (void) sendto(s, (char *) &result, sizeof(result), 0, 709 (struct sockaddr *)&ss, size); 710} 711 712/* ARGSUSED */ 713void 714machtime_stream(s, sep) 715 int s; 716 struct servtab *sep; 717{ 718 unsigned long result; 719 720 result = machtime(); 721 (void) send(s, (char *) &result, sizeof(result), MSG_EOF); 722} 723 724/* 725 * RFC1078 TCP Port Service Multiplexer (TCPMUX). Service connections to 726 * services based on the service name sent. 727 * 728 * Based on TCPMUX.C by Mark K. Lottor November 1988 729 * sri-nic::ps:<mkl>tcpmux.c 730 */ 731 732#define MAX_SERV_LEN (256+2) /* 2 bytes for \r\n */ 733#define strwrite(fd, buf) (void) write(fd, buf, sizeof(buf)-1) 734 735static int /* # of characters upto \r,\n or \0 */ 736getline(fd, buf, len) 737 int fd; 738 char *buf; 739 int len; 740{ 741 int count = 0, n; 742 struct sigaction sa; 743 744 sa.sa_flags = 0; 745 sigemptyset(&sa.sa_mask); 746 sa.sa_handler = SIG_DFL; 747 sigaction(SIGALRM, &sa, (struct sigaction *)0); 748 do { 749 alarm(10); 750 n = read(fd, buf, len-count); 751 alarm(0); 752 if (n == 0) 753 return (count); 754 if (n < 0) 755 return (-1); 756 while (--n >= 0) { 757 if (*buf == '\r' || *buf == '\n' || *buf == '\0') 758 return (count); 759 count++; 760 buf++; 761 } 762 } while (count < len); 763 return (count); 764} 765 766struct servtab * 767tcpmux(s) 768 int s; 769{ 770 struct servtab *sep; 771 char service[MAX_SERV_LEN+1]; 772 int len; 773 774 /* Get requested service name */ 775 if ((len = getline(s, service, MAX_SERV_LEN)) < 0) { 776 strwrite(s, "-Error reading service name\r\n"); 777 return (NULL); 778 } 779 service[len] = '\0'; 780 781 if (debug) 782 warnx("tcpmux: someone wants %s", service); 783 784 /* 785 * Help is a required command, and lists available services, 786 * one per line. 787 */ 788 if (!strcasecmp(service, "help")) { 789 for (sep = servtab; sep; sep = sep->se_next) { 790 if (!ISMUX(sep)) 791 continue; 792 (void)write(s,sep->se_service,strlen(sep->se_service)); 793 strwrite(s, "\r\n"); 794 } 795 return (NULL); 796 } 797 798 /* Try matching a service in inetd.conf with the request */ 799 for (sep = servtab; sep; sep = sep->se_next) { 800 if (!ISMUX(sep)) 801 continue; 802 if (!strcasecmp(service, sep->se_service)) { 803 if (ISMUXPLUS(sep)) { 804 strwrite(s, "+Go\r\n"); 805 } 806 return (sep); 807 } 808 } 809 strwrite(s, "-Service not available\r\n"); 810 return (NULL); 811} 812