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