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: src/usr.sbin/inetd/builtins.c,v 1.19.2.7 2002/07/22 14:05:56 fanf Exp $ 27 * $DragonFly: src/usr.sbin/inetd/builtins.c,v 1.5 2004/12/18 22:48:03 swildner Exp $ 28 * 29 */ 30 31 #include <sys/filio.h> 32 #include <sys/ioccom.h> 33 #include <sys/param.h> 34 #include <sys/stat.h> 35 #include <sys/socket.h> 36 #include <sys/sysctl.h> 37 #include <sys/ucred.h> 38 #include <sys/uio.h> 39 #include <sys/utsname.h> 40 41 #include <ctype.h> 42 #include <err.h> 43 #include <errno.h> 44 #include <fcntl.h> 45 #include <limits.h> 46 #include <pwd.h> 47 #include <signal.h> 48 #include <stdlib.h> 49 #include <string.h> 50 #include <sysexits.h> 51 #include <syslog.h> 52 #include <unistd.h> 53 54 #include "inetd.h" 55 56 void chargen_dg(int, struct servtab *); 57 void chargen_stream(int, struct servtab *); 58 void daytime_dg(int, struct servtab *); 59 void daytime_stream(int, struct servtab *); 60 void discard_dg(int, struct servtab *); 61 void discard_stream(int, struct servtab *); 62 void echo_dg(int, struct servtab *); 63 void echo_stream(int, struct servtab *); 64 static int getline(int, char *, int); 65 void iderror(int, int, int, const char *); 66 void ident_stream(int, struct servtab *); 67 void initring(void); 68 unsigned long machtime(void); 69 void machtime_dg(int, struct servtab *); 70 void machtime_stream(int, struct servtab *); 71 72 char ring[128]; 73 char *endring; 74 75 76 struct biltin biltins[] = { 77 /* Echo received data */ 78 { "echo", SOCK_STREAM, 1, -1, echo_stream }, 79 { "echo", SOCK_DGRAM, 0, 1, echo_dg }, 80 81 /* Internet /dev/null */ 82 { "discard", SOCK_STREAM, 1, -1, discard_stream }, 83 { "discard", SOCK_DGRAM, 0, 1, discard_dg }, 84 85 /* Return 32 bit time since 1900 */ 86 { "time", SOCK_STREAM, 0, -1, machtime_stream }, 87 { "time", SOCK_DGRAM, 0, 1, machtime_dg }, 88 89 /* Return human-readable time */ 90 { "daytime", SOCK_STREAM, 0, -1, daytime_stream }, 91 { "daytime", SOCK_DGRAM, 0, 1, daytime_dg }, 92 93 /* Familiar character generator */ 94 { "chargen", SOCK_STREAM, 1, -1, chargen_stream }, 95 { "chargen", SOCK_DGRAM, 0, 1, chargen_dg }, 96 97 { "tcpmux", SOCK_STREAM, 1, -1, (bi_fn_t *)tcpmux }, 98 99 { "auth", SOCK_STREAM, 1, -1, ident_stream }, 100 101 { NULL, 0, 0, 0, NULL } 102 }; 103 104 /* 105 * RFC864 Character Generator Protocol. Generates character data without 106 * any regard for input. 107 */ 108 109 void 110 initring(void) 111 { 112 int i; 113 114 endring = ring; 115 116 for (i = 0; i <= 128; ++i) 117 if (isprint(i)) 118 *endring++ = i; 119 } 120 121 /* ARGSUSED */ 122 void 123 chargen_dg(int s, struct servtab *sep) /* Character generator */ 124 { 125 struct sockaddr_storage ss; 126 static char *rs; 127 int len; 128 socklen_t size; 129 char text[LINESIZ+2]; 130 131 if (endring == 0) { 132 initring(); 133 rs = ring; 134 } 135 136 size = sizeof(ss); 137 if (recvfrom(s, text, sizeof(text), 0, 138 (struct sockaddr *)&ss, &size) < 0) 139 return; 140 141 if (check_loop((struct sockaddr *)&ss, sep)) 142 return; 143 144 if ((len = endring - rs) >= LINESIZ) 145 memmove(text, rs, LINESIZ); 146 else { 147 memmove(text, rs, len); 148 memmove(text + len, ring, LINESIZ - len); 149 } 150 if (++rs == endring) 151 rs = ring; 152 text[LINESIZ] = '\r'; 153 text[LINESIZ + 1] = '\n'; 154 sendto(s, text, sizeof(text), 0, (struct sockaddr *)&ss, size); 155 } 156 157 /* ARGSUSED */ 158 void 159 chargen_stream(int s, struct servtab *sep) /* Character generator */ 160 { 161 int len; 162 char *rs, text[LINESIZ+2]; 163 164 inetd_setproctitle(sep->se_service, s); 165 166 if (!endring) { 167 initring(); 168 rs = ring; 169 } 170 171 text[LINESIZ] = '\r'; 172 text[LINESIZ + 1] = '\n'; 173 for (rs = ring;;) { 174 if ((len = endring - rs) >= LINESIZ) 175 memmove(text, rs, LINESIZ); 176 else { 177 memmove(text, rs, len); 178 memmove(text + len, ring, LINESIZ - len); 179 } 180 if (++rs == endring) 181 rs = ring; 182 if (write(s, text, sizeof(text)) != sizeof(text)) 183 break; 184 } 185 exit(0); 186 } 187 188 /* 189 * RFC867 Daytime Protocol. Sends the current date and time as an ascii 190 * character string without any regard for input. 191 */ 192 193 /* ARGSUSED */ 194 void 195 daytime_dg(int s, struct servtab *sep) /* Return human-readable time of day */ 196 { 197 char buffer[256]; 198 time_t now; 199 struct sockaddr_storage ss; 200 socklen_t size; 201 202 now = time((time_t *) 0); 203 204 size = sizeof(ss); 205 if (recvfrom(s, buffer, sizeof(buffer), 0, 206 (struct sockaddr *)&ss, &size) < 0) 207 return; 208 209 if (check_loop((struct sockaddr *)&ss, sep)) 210 return; 211 212 sprintf(buffer, "%.24s\r\n", ctime(&now)); 213 sendto(s, buffer, strlen(buffer), 0, (struct sockaddr *)&ss, size); 214 } 215 216 /* ARGSUSED */ 217 void 218 daytime_stream(int s, struct servtab *sep __unused) /* Return human-readable time of day */ 219 { 220 char buffer[256]; 221 time_t now; 222 223 now = time((time_t *) 0); 224 225 sprintf(buffer, "%.24s\r\n", ctime(&now)); 226 send(s, buffer, strlen(buffer), MSG_EOF); 227 } 228 229 /* 230 * RFC863 Discard Protocol. Any data received is thrown away and no response 231 * is sent. 232 */ 233 234 /* ARGSUSED */ 235 void 236 discard_dg(int s, struct servtab *sep __unused) /* Discard service -- ignore data */ 237 { 238 char buffer[BUFSIZE]; 239 240 read(s, buffer, sizeof(buffer)); 241 } 242 243 /* ARGSUSED */ 244 void 245 discard_stream(int s, struct servtab *sep) /* Discard service -- ignore data */ 246 { 247 int ret; 248 char buffer[BUFSIZE]; 249 250 inetd_setproctitle(sep->se_service, s); 251 while (1) { 252 while ((ret = read(s, buffer, sizeof(buffer))) > 0) 253 ; 254 if (ret == 0 || errno != EINTR) 255 break; 256 } 257 exit(0); 258 } 259 260 /* 261 * RFC862 Echo Protocol. Any data received is sent back to the sender as 262 * received. 263 */ 264 265 /* ARGSUSED */ 266 void 267 echo_dg(int s, struct servtab *sep) /* Echo service -- echo data back */ 268 { 269 char buffer[65536]; /* Should be sizeof(max datagram). */ 270 int i; 271 socklen_t size; 272 struct sockaddr_storage ss; 273 274 size = sizeof(ss); 275 if ((i = recvfrom(s, buffer, sizeof(buffer), 0, 276 (struct sockaddr *)&ss, &size)) < 0) 277 return; 278 279 if (check_loop((struct sockaddr *)&ss, sep)) 280 return; 281 282 sendto(s, buffer, i, 0, (struct sockaddr *)&ss, size); 283 } 284 285 /* ARGSUSED */ 286 void 287 echo_stream(int s, struct servtab *sep) /* Echo service -- echo data back */ 288 { 289 char buffer[BUFSIZE]; 290 int i; 291 292 inetd_setproctitle(sep->se_service, s); 293 while ((i = read(s, buffer, sizeof(buffer))) > 0 && 294 write(s, buffer, i) > 0) 295 ; 296 exit(0); 297 } 298 299 /* 300 * RFC1413 Identification Protocol. Given a TCP port number pair, return a 301 * character string which identifies the owner of that connection on the 302 * server's system. Extended to allow for ~/.fakeid support and ~/.noident 303 * support. 304 */ 305 306 /* RFC 1413 says the following are the only errors you can return. */ 307 #define ID_INVALID "INVALID-PORT" /* Port number improperly specified. */ 308 #define ID_NOUSER "NO-USER" /* Port not in use/not identifable. */ 309 #define ID_HIDDEN "HIDDEN-USER" /* Hiden at user's request. */ 310 #define ID_UNKNOWN "UNKNOWN-ERROR" /* Everything else. */ 311 312 /* ARGSUSED */ 313 void 314 iderror(int lport, int fport, int s, const char *er) /* Generic ident_stream error-sending func */ 315 { 316 char *p; 317 318 asprintf(&p, "%d , %d : ERROR : %s\r\n", lport, fport, er); 319 if (p == NULL) { 320 syslog(LOG_ERR, "asprintf: %m"); 321 exit(EX_OSERR); 322 } 323 send(s, p, strlen(p), MSG_EOF); 324 free(p); 325 326 exit(0); 327 } 328 329 /* ARGSUSED */ 330 void 331 ident_stream(int s, struct servtab *sep) /* Ident service (AKA "auth") */ 332 { 333 struct utsname un; 334 struct stat sb; 335 struct sockaddr_in sin4[2]; 336 #ifdef INET6 337 struct sockaddr_in6 sin6[2]; 338 #endif 339 struct sockaddr_storage ss[2]; 340 struct ucred uc; 341 struct timeval tv = { 342 10, 343 0 344 }, to; 345 struct passwd *pw = NULL; 346 fd_set fdset; 347 char buf[BUFSIZE], *p, **av, *osname = NULL, e; 348 char idbuf[MAXLOGNAME] = ""; /* Big enough to hold uid in decimal. */ 349 socklen_t socklen; 350 ssize_t ssize; 351 size_t size, bufsiz; 352 int c, fflag = 0, nflag = 0, rflag = 0, argc = 0; 353 int gflag = 0, iflag = 0, Fflag = 0, getcredfail = 0, onreadlen; 354 u_short lport, fport; 355 356 inetd_setproctitle(sep->se_service, s); 357 /* 358 * Reset getopt() since we are a fork() but not an exec() from 359 * a parent which used getopt() already. 360 */ 361 optind = 1; 362 optreset = 1; 363 /* 364 * Take the internal argument vector and count it out to make an 365 * argument count for getopt. This can be used for any internal 366 * service to read arguments and use getopt() easily. 367 */ 368 for (av = sep->se_argv; *av; av++) 369 argc++; 370 if (argc) { 371 int sec, usec; 372 size_t i; 373 u_int32_t rnd32; 374 375 while ((c = getopt(argc, sep->se_argv, "d:fFgino:rt:")) != -1) 376 switch (c) { 377 case 'd': 378 if (!gflag) 379 strlcpy(idbuf, optarg, sizeof(idbuf)); 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 rnd32 = 0; /* Shush, compiler. */ 391 /* 392 * The number of bits in "rnd32" 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 * 32 bits from arc4random corresponds to 398 * about 6 base-36 digits, so we reseed evey 6. 399 */ 400 for (i = 0; i < sizeof(idbuf) - 1; i++) { 401 static const char *const base36 = 402 "0123456789" 403 "abcdefghijklmnopqrstuvwxyz"; 404 if (i % 6 == 0) 405 rnd32 = arc4random(); 406 idbuf[i] = base36[rnd32 % 36]; 407 rnd32 /= 36; 408 } 409 idbuf[i] = '\0'; 410 break; 411 case 'i': 412 iflag = 1; 413 break; 414 case 'n': 415 nflag = 1; 416 break; 417 case 'o': 418 osname = optarg; 419 break; 420 case 'r': 421 rflag = 1; 422 break; 423 case 't': 424 switch (sscanf(optarg, "%d.%d", &sec, &usec)) { 425 case 2: 426 tv.tv_usec = usec; 427 /* FALLTHROUGH */ 428 case 1: 429 tv.tv_sec = sec; 430 break; 431 default: 432 if (debug) 433 warnx("bad -t argument"); 434 break; 435 } 436 break; 437 default: 438 break; 439 } 440 } 441 if (osname == NULL) { 442 if (uname(&un) == -1) 443 iderror(0, 0, s, ID_UNKNOWN); 444 osname = un.sysname; 445 } 446 447 /* 448 * We're going to prepare for and execute reception of a 449 * packet of data from the user. The data is in the format 450 * "local_port , foreign_port\r\n" (with local being the 451 * server's port and foreign being the client's.) 452 */ 453 gettimeofday(&to, NULL); 454 to.tv_sec += tv.tv_sec; 455 to.tv_usec += tv.tv_usec; 456 if (to.tv_usec >= 1000000) { 457 to.tv_usec -= 1000000; 458 to.tv_sec++; 459 } 460 461 size = 0; 462 bufsiz = sizeof(buf) - 1; 463 FD_ZERO(&fdset); 464 while (bufsiz > 0) { 465 gettimeofday(&tv, NULL); 466 tv.tv_sec = to.tv_sec - tv.tv_sec; 467 tv.tv_usec = to.tv_usec - tv.tv_usec; 468 if (tv.tv_usec < 0) { 469 tv.tv_usec += 1000000; 470 tv.tv_sec--; 471 } 472 if (tv.tv_sec < 0) 473 break; 474 FD_SET(s, &fdset); 475 if (select(s + 1, &fdset, NULL, NULL, &tv) == -1) 476 iderror(0, 0, s, ID_UNKNOWN); 477 if (ioctl(s, FIONREAD, &onreadlen) == -1) 478 iderror(0, 0, s, ID_UNKNOWN); 479 if ((size_t)onreadlen > bufsiz) 480 onreadlen = bufsiz; 481 ssize = read(s, &buf[size], (size_t)onreadlen); 482 if (ssize == -1) 483 iderror(0, 0, s, ID_UNKNOWN); 484 else if (ssize == 0) 485 break; 486 bufsiz -= ssize; 487 size += ssize; 488 if (memchr(&buf[size - ssize], '\n', ssize) != NULL) 489 break; 490 } 491 buf[size] = '\0'; 492 /* Read two characters, and check for a delimiting character */ 493 if (sscanf(buf, "%hu , %hu%c", &lport, &fport, &e) != 3 || isdigit(e)) 494 iderror(0, 0, s, ID_INVALID); 495 496 /* Send garbage? */ 497 if (gflag) 498 goto printit; 499 500 /* 501 * If not "real" (-r), send a HIDDEN-USER error for everything. 502 * If -d is used to set a fallback username, this is used to 503 * override it, and the fallback is returned instead. 504 */ 505 if (!rflag) { 506 if (*idbuf == '\0') 507 iderror(lport, fport, s, ID_HIDDEN); 508 goto printit; 509 } 510 511 /* 512 * We take the input and construct an array of two sockaddr_ins 513 * which contain the local address information and foreign 514 * address information, respectively, used to look up the 515 * credentials for the socket (which are returned by the 516 * sysctl "net.inet.tcp.getcred" when we call it.) 517 */ 518 socklen = sizeof(ss[0]); 519 if (getsockname(s, (struct sockaddr *)&ss[0], &socklen) == -1) 520 iderror(lport, fport, s, ID_UNKNOWN); 521 socklen = sizeof(ss[1]); 522 if (getpeername(s, (struct sockaddr *)&ss[1], &socklen) == -1) 523 iderror(lport, fport, s, ID_UNKNOWN); 524 if (ss[0].ss_family != ss[1].ss_family) 525 iderror(lport, fport, s, ID_UNKNOWN); 526 size = sizeof(uc); 527 switch (ss[0].ss_family) { 528 case AF_INET: 529 sin4[0] = *(struct sockaddr_in *)&ss[0]; 530 sin4[0].sin_port = htons(lport); 531 sin4[1] = *(struct sockaddr_in *)&ss[1]; 532 sin4[1].sin_port = htons(fport); 533 if (sysctlbyname("net.inet.tcp.getcred", &uc, &size, sin4, 534 sizeof(sin4)) == -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 (*idbuf == '\0') 554 iderror(lport, fport, s, 555 getcredfail == ENOENT ? ID_NOUSER : ID_UNKNOWN); 556 goto printit; 557 } 558 559 /* Look up the pw to get the username and home directory*/ 560 errno = 0; 561 pw = getpwuid(uc.cr_uid); 562 if (pw == NULL) 563 iderror(lport, fport, s, errno == 0 ? ID_NOUSER : ID_UNKNOWN); 564 565 if (iflag) 566 snprintf(idbuf, sizeof(idbuf), "%u", (unsigned)pw->pw_uid); 567 else 568 strlcpy(idbuf, pw->pw_name, sizeof(idbuf)); 569 570 /* 571 * If enabled, we check for a file named ".noident" in the user's 572 * home directory. If found, we return HIDDEN-USER. 573 */ 574 if (nflag) { 575 if (asprintf(&p, "%s/.noident", pw->pw_dir) == -1) 576 iderror(lport, fport, s, ID_UNKNOWN); 577 if (lstat(p, &sb) == 0) { 578 free(p); 579 iderror(lport, fport, s, ID_HIDDEN); 580 } 581 free(p); 582 } 583 584 /* 585 * Here, if enabled, we read a user's ".fakeid" file in their 586 * home directory. It consists of a line containing the name 587 * they want. 588 */ 589 if (fflag) { 590 int fakeid_fd; 591 592 /* 593 * Here we set ourself to effectively be the user, so we don't 594 * open any files we have no permission to open, especially 595 * symbolic links to sensitive root-owned files or devices. 596 */ 597 if (initgroups(pw->pw_name, pw->pw_gid) == -1) 598 iderror(lport, fport, s, ID_UNKNOWN); 599 if (seteuid(pw->pw_uid) == -1) 600 iderror(lport, fport, s, ID_UNKNOWN); 601 /* 602 * We can't stat() here since that would be a race 603 * condition. 604 * Therefore, we open the file we have permissions to open 605 * and if it's not a regular file, we close it and end up 606 * returning the user's real username. 607 */ 608 if (asprintf(&p, "%s/.fakeid", pw->pw_dir) == -1) 609 iderror(lport, fport, s, ID_UNKNOWN); 610 fakeid_fd = open(p, O_RDONLY | O_NONBLOCK); 611 free(p); 612 if (fakeid_fd == -1 || fstat(fakeid_fd, &sb) == -1 || 613 !S_ISREG(sb.st_mode)) 614 goto fakeid_fail; 615 616 if ((ssize = read(fakeid_fd, buf, sizeof(buf) - 1)) < 0) 617 goto fakeid_fail; 618 buf[ssize] = '\0'; 619 620 /* 621 * Usually, the file will have the desired identity 622 * in the form "identity\n". Allow for leading white 623 * space and trailing white space/end of line. 624 */ 625 p = buf; 626 p += strspn(p, " \t"); 627 p[strcspn(p, " \t\r\n")] = '\0'; 628 if (strlen(p) > MAXLOGNAME - 1) /* Too long (including nul)? */ 629 p[MAXLOGNAME - 1] = '\0'; 630 631 /* 632 * If the name is a zero-length string or matches it 633 * the id or name of another user (unless permitted by -F) 634 * then it is invalid. 635 */ 636 if (*p == '\0') 637 goto fakeid_fail; 638 if (!Fflag) { 639 if (iflag) { 640 if (p[strspn(p, "0123456789")] == '\0' && 641 getpwuid(atoi(p)) != NULL) 642 goto fakeid_fail; 643 } else { 644 if (getpwnam(p) != NULL) 645 goto fakeid_fail; 646 } 647 } 648 649 strlcpy(idbuf, p, sizeof(idbuf)); 650 651 fakeid_fail: 652 if (fakeid_fd != -1) 653 close(fakeid_fd); 654 } 655 656 printit: 657 /* Finally, we make and send the reply. */ 658 if (asprintf(&p, "%d , %d : USERID : %s : %s\r\n", lport, fport, osname, 659 idbuf) == -1) { 660 syslog(LOG_ERR, "asprintf: %m"); 661 exit(EX_OSERR); 662 } 663 send(s, p, strlen(p), MSG_EOF); 664 free(p); 665 666 exit(0); 667 } 668 669 /* 670 * RFC738 Time Server. 671 * Return a machine readable date and time, in the form of the 672 * number of seconds since midnight, Jan 1, 1900. Since gettimeofday 673 * returns the number of seconds since midnight, Jan 1, 1970, 674 * we must add 2208988800 seconds to this figure to make up for 675 * some seventy years Bell Labs was asleep. 676 */ 677 678 unsigned long 679 machtime(void) 680 { 681 struct timeval tv; 682 683 if (gettimeofday(&tv, (struct timezone *)NULL) < 0) { 684 if (debug) 685 warnx("unable to get time of day"); 686 return (0L); 687 } 688 #define OFFSET ((u_long)25567 * 24*60*60) 689 return (htonl((long)(tv.tv_sec + OFFSET))); 690 #undef OFFSET 691 } 692 693 /* ARGSUSED */ 694 void 695 machtime_dg(int s, struct servtab *sep) 696 { 697 unsigned long result; 698 struct sockaddr_storage ss; 699 socklen_t size; 700 701 size = sizeof(ss); 702 if (recvfrom(s, (char *)&result, sizeof(result), 0, 703 (struct sockaddr *)&ss, &size) < 0) 704 return; 705 706 if (check_loop((struct sockaddr *)&ss, sep)) 707 return; 708 709 result = machtime(); 710 sendto(s, (char *) &result, sizeof(result), 0, 711 (struct sockaddr *)&ss, size); 712 } 713 714 /* ARGSUSED */ 715 void 716 machtime_stream(int s, struct servtab *sep __unused) 717 { 718 unsigned long result; 719 720 result = machtime(); 721 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) write(fd, buf, sizeof(buf)-1) 734 735 static int /* # of characters upto \r,\n or \0 */ 736 getline(int fd, char *buf, int len) 737 { 738 int count = 0, n; 739 struct sigaction sa; 740 741 sa.sa_flags = 0; 742 sigemptyset(&sa.sa_mask); 743 sa.sa_handler = SIG_DFL; 744 sigaction(SIGALRM, &sa, (struct sigaction *)0); 745 do { 746 alarm(10); 747 n = read(fd, buf, len-count); 748 alarm(0); 749 if (n == 0) 750 return (count); 751 if (n < 0) 752 return (-1); 753 while (--n >= 0) { 754 if (*buf == '\r' || *buf == '\n' || *buf == '\0') 755 return (count); 756 count++; 757 buf++; 758 } 759 } while (count < len); 760 return (count); 761 } 762 763 struct servtab * 764 tcpmux(int s) 765 { 766 struct servtab *sep; 767 char service[MAX_SERV_LEN+1]; 768 int len; 769 770 /* Get requested service name */ 771 if ((len = getline(s, service, MAX_SERV_LEN)) < 0) { 772 strwrite(s, "-Error reading service name\r\n"); 773 return (NULL); 774 } 775 service[len] = '\0'; 776 777 if (debug) 778 warnx("tcpmux: someone wants %s", service); 779 780 /* 781 * Help is a required command, and lists available services, 782 * one per line. 783 */ 784 if (!strcasecmp(service, "help")) { 785 for (sep = servtab; sep; sep = sep->se_next) { 786 if (!ISMUX(sep)) 787 continue; 788 write(s,sep->se_service,strlen(sep->se_service)); 789 strwrite(s, "\r\n"); 790 } 791 return (NULL); 792 } 793 794 /* Try matching a service in inetd.conf with the request */ 795 for (sep = servtab; sep; sep = sep->se_next) { 796 if (!ISMUX(sep)) 797 continue; 798 if (!strcasecmp(service, sep->se_service)) { 799 if (ISMUXPLUS(sep)) { 800 strwrite(s, "+Go\r\n"); 801 } 802 return (sep); 803 } 804 } 805 strwrite(s, "-Service not available\r\n"); 806 return (NULL); 807 } 808