2 * Copyright (c) 1983, 1991, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
30 #include <sys/filio.h>
31 #include <sys/ioccom.h>
32 #include <sys/param.h>
34 #include <sys/socket.h>
35 #include <sys/sysctl.h>
36 #include <sys/ucred.h>
38 #include <sys/utsname.h>
55 void chargen_dg __P((int, struct servtab *));
56 void chargen_stream __P((int, struct servtab *));
57 void daytime_dg __P((int, struct servtab *));
58 void daytime_stream __P((int, struct servtab *));
59 void discard_dg __P((int, struct servtab *));
60 void discard_stream __P((int, struct servtab *));
61 void echo_dg __P((int, struct servtab *));
62 void echo_stream __P((int, struct servtab *));
63 static int getline __P((int, char *, int));
64 void iderror __P((int, int, int, const char *));
65 void ident_stream __P((int, struct servtab *));
66 void initring __P((void));
67 unsigned long machtime __P((void));
68 void machtime_dg __P((int, struct servtab *));
69 void machtime_stream __P((int, struct servtab *));
75 struct biltin biltins[] = {
76 /* Echo received data */
77 { "echo", SOCK_STREAM, 1, -1, echo_stream },
78 { "echo", SOCK_DGRAM, 0, 1, echo_dg },
80 /* Internet /dev/null */
81 { "discard", SOCK_STREAM, 1, -1, discard_stream },
82 { "discard", SOCK_DGRAM, 0, 1, discard_dg },
84 /* Return 32 bit time since 1900 */
85 { "time", SOCK_STREAM, 0, -1, machtime_stream },
86 { "time", SOCK_DGRAM, 0, 1, machtime_dg },
88 /* Return human-readable time */
89 { "daytime", SOCK_STREAM, 0, -1, daytime_stream },
90 { "daytime", SOCK_DGRAM, 0, 1, daytime_dg },
92 /* Familiar character generator */
93 { "chargen", SOCK_STREAM, 1, -1, chargen_stream },
94 { "chargen", SOCK_DGRAM, 0, 1, chargen_dg },
96 { "tcpmux", SOCK_STREAM, 1, -1, (bi_fn_t *)tcpmux },
98 { "auth", SOCK_STREAM, 1, -1, ident_stream },
100 { NULL, 0, 0, 0, NULL }
104 * RFC864 Character Generator Protocol. Generates character data without
105 * any regard for input.
115 for (i = 0; i <= 128; ++i)
122 chargen_dg(s, sep) /* Character generator */
126 struct sockaddr_storage ss;
130 char text[LINESIZ+2];
138 if (recvfrom(s, text, sizeof(text), 0,
139 (struct sockaddr *)&ss, &size) < 0)
142 if (check_loop((struct sockaddr *)&ss, sep))
145 if ((len = endring - rs) >= LINESIZ)
146 memmove(text, rs, LINESIZ);
148 memmove(text, rs, len);
149 memmove(text + len, ring, LINESIZ - len);
153 text[LINESIZ] = '\r';
154 text[LINESIZ + 1] = '\n';
155 (void) sendto(s, text, sizeof(text), 0, (struct sockaddr *)&ss, size);
160 chargen_stream(s, sep) /* Character generator */
165 char *rs, text[LINESIZ+2];
167 inetd_setproctitle(sep->se_service, s);
174 text[LINESIZ] = '\r';
175 text[LINESIZ + 1] = '\n';
177 if ((len = endring - rs) >= LINESIZ)
178 memmove(text, rs, LINESIZ);
180 memmove(text, rs, len);
181 memmove(text + len, ring, LINESIZ - len);
185 if (write(s, text, sizeof(text)) != sizeof(text))
192 * RFC867 Daytime Protocol. Sends the current date and time as an ascii
193 * character string without any regard for input.
198 daytime_dg(s, sep) /* Return human-readable time of day */
204 struct sockaddr_storage ss;
207 now = time((time_t *) 0);
210 if (recvfrom(s, buffer, sizeof(buffer), 0,
211 (struct sockaddr *)&ss, &size) < 0)
214 if (check_loop((struct sockaddr *)&ss, sep))
217 (void) sprintf(buffer, "%.24s\r\n", ctime(&now));
218 (void) sendto(s, buffer, strlen(buffer), 0,
219 (struct sockaddr *)&ss, size);
224 daytime_stream(s, sep) /* Return human-readable time of day */
226 struct servtab *sep __unused;
231 now = time((time_t *) 0);
233 (void) sprintf(buffer, "%.24s\r\n", ctime(&now));
234 (void) send(s, buffer, strlen(buffer), MSG_EOF);
238 * RFC863 Discard Protocol. Any data received is thrown away and no response
244 discard_dg(s, sep) /* Discard service -- ignore data */
246 struct servtab *sep __unused;
248 char buffer[BUFSIZE];
250 (void) read(s, buffer, sizeof(buffer));
255 discard_stream(s, sep) /* Discard service -- ignore data */
260 char buffer[BUFSIZE];
262 inetd_setproctitle(sep->se_service, s);
264 while ((ret = read(s, buffer, sizeof(buffer))) > 0)
266 if (ret == 0 || errno != EINTR)
273 * RFC862 Echo Protocol. Any data received is sent back to the sender as
279 echo_dg(s, sep) /* Echo service -- echo data back */
283 char buffer[65536]; /* Should be sizeof(max datagram). */
286 struct sockaddr_storage ss;
289 if ((i = recvfrom(s, buffer, sizeof(buffer), 0,
290 (struct sockaddr *)&ss, &size)) < 0)
293 if (check_loop((struct sockaddr *)&ss, sep))
296 (void) sendto(s, buffer, i, 0, (struct sockaddr *)&ss, size);
301 echo_stream(s, sep) /* Echo service -- echo data back */
305 char buffer[BUFSIZE];
308 inetd_setproctitle(sep->se_service, s);
309 while ((i = read(s, buffer, sizeof(buffer))) > 0 &&
310 write(s, buffer, i) > 0)
316 * RFC1413 Identification Protocol. Given a TCP port number pair, return a
317 * character string which identifies the owner of that connection on the
318 * server's system. Extended to allow for ~/.fakeid support and ~/.noident
322 /* RFC 1413 says the following are the only errors you can return. */
323 #define ID_INVALID "INVALID-PORT" /* Port number improperly specified. */
324 #define ID_NOUSER "NO-USER" /* Port not in use/not identifable. */
325 #define ID_HIDDEN "HIDDEN-USER" /* Hiden at user's request. */
326 #define ID_UNKNOWN "UNKNOWN-ERROR" /* Everything else. */
330 iderror(lport, fport, s, er) /* Generic ident_stream error-sending func */
336 asprintf(&p, "%d , %d : ERROR : %s\r\n", lport, fport, er);
338 syslog(LOG_ERR, "asprintf: %m");
341 send(s, p, strlen(p), MSG_EOF);
349 ident_stream(s, sep) /* Ident service (AKA "auth") */
355 struct sockaddr_in sin4[2];
357 struct sockaddr_in6 sin6[2];
359 struct sockaddr_storage ss[2];
361 struct timeval tv = {
365 struct passwd *pw = NULL;
367 char buf[BUFSIZE], *p, **av, *osname = NULL, e;
368 char idbuf[MAXLOGNAME] = ""; /* Big enough to hold uid in decimal. */
372 int c, fflag = 0, nflag = 0, rflag = 0, argc = 0;
373 int gflag = 0, iflag = 0, Fflag = 0, getcredfail = 0, onreadlen;
374 u_short lport, fport;
376 inetd_setproctitle(sep->se_service, s);
378 * Reset getopt() since we are a fork() but not an exec() from
379 * a parent which used getopt() already.
384 * Take the internal argument vector and count it out to make an
385 * argument count for getopt. This can be used for any internal
386 * service to read arguments and use getopt() easily.
388 for (av = sep->se_argv; *av; av++)
395 while ((c = getopt(argc, sep->se_argv, "d:fFgino:rt:")) != -1)
399 strlcpy(idbuf, optarg, sizeof(idbuf));
410 rnd32 = 0; /* Shush, compiler. */
412 * The number of bits in "rnd32" divided
413 * by the number of bits needed per iteration
414 * gives a more optimal way to reload the
415 * random number only when necessary.
417 * 32 bits from arc4random corrisponds to
418 * about 6 base-36 digits, so we reseed evey 6.
420 for (i = 0; i < sizeof(idbuf) - 1; i++) {
421 static const char *const base36 =
423 "abcdefghijklmnopqrstuvwxyz";
425 rnd32 = arc4random();
426 idbuf[i] = base36[rnd32 % 36];
444 switch (sscanf(optarg, "%d.%d", &sec, &usec)) {
453 warnx("bad -t argument");
461 if (osname == NULL) {
462 if (uname(&un) == -1)
463 iderror(0, 0, s, ID_UNKNOWN);
468 * We're going to prepare for and execute reception of a
469 * packet of data from the user. The data is in the format
470 * "local_port , foreign_port\r\n" (with local being the
471 * server's port and foreign being the client's.)
473 gettimeofday(&to, NULL);
474 to.tv_sec += tv.tv_sec;
475 to.tv_usec += tv.tv_usec;
476 if (to.tv_usec >= 1000000) {
477 to.tv_usec -= 1000000;
482 bufsiz = sizeof(buf) - 1;
485 gettimeofday(&tv, NULL);
486 tv.tv_sec = to.tv_sec - tv.tv_sec;
487 tv.tv_usec = to.tv_usec - tv.tv_usec;
488 if (tv.tv_usec < 0) {
489 tv.tv_usec += 1000000;
495 if (select(s + 1, &fdset, NULL, NULL, &tv) == -1)
496 iderror(0, 0, s, ID_UNKNOWN);
497 if (ioctl(s, FIONREAD, &onreadlen) == -1)
498 iderror(0, 0, s, ID_UNKNOWN);
499 if ((size_t)onreadlen > bufsiz)
501 ssize = read(s, &buf[size], (size_t)onreadlen);
503 iderror(0, 0, s, ID_UNKNOWN);
508 if (memchr(&buf[size - ssize], '\n', ssize) != NULL)
512 /* Read two characters, and check for a delimiting character */
513 if (sscanf(buf, "%hu , %hu%c", &lport, &fport, &e) != 3 || isdigit(e))
514 iderror(0, 0, s, ID_INVALID);
521 * If not "real" (-r), send a HIDDEN-USER error for everything.
522 * If -d is used to set a fallback username, this is used to
523 * override it, and the fallback is returned instead.
527 iderror(lport, fport, s, ID_HIDDEN);
532 * We take the input and construct an array of two sockaddr_ins
533 * which contain the local address information and foreign
534 * address information, respectively, used to look up the
535 * credentials for the socket (which are returned by the
536 * sysctl "net.inet.tcp.getcred" when we call it.)
538 socklen = sizeof(ss[0]);
539 if (getsockname(s, (struct sockaddr *)&ss[0], &socklen) == -1)
540 iderror(lport, fport, s, ID_UNKNOWN);
541 socklen = sizeof(ss[1]);
542 if (getpeername(s, (struct sockaddr *)&ss[1], &socklen) == -1)
543 iderror(lport, fport, s, ID_UNKNOWN);
544 if (ss[0].ss_family != ss[1].ss_family)
545 iderror(lport, fport, s, ID_UNKNOWN);
547 switch (ss[0].ss_family) {
549 sin4[0] = *(struct sockaddr_in *)&ss[0];
550 sin4[0].sin_port = htons(lport);
551 sin4[1] = *(struct sockaddr_in *)&ss[1];
552 sin4[1].sin_port = htons(fport);
553 if (sysctlbyname("net.inet.tcp.getcred", &uc, &size, sin4,
559 sin6[0] = *(struct sockaddr_in6 *)&ss[0];
560 sin6[0].sin6_port = htons(lport);
561 sin6[1] = *(struct sockaddr_in6 *)&ss[1];
562 sin6[1].sin6_port = htons(fport);
563 if (sysctlbyname("net.inet6.tcp6.getcred", &uc, &size, sin6,
568 default: /* should not reach here */
569 getcredfail = EAFNOSUPPORT;
572 if (getcredfail != 0 || uc.cr_version != XUCRED_VERSION) {
574 iderror(lport, fport, s,
575 getcredfail == ENOENT ? ID_NOUSER : ID_UNKNOWN);
579 /* Look up the pw to get the username and home directory*/
581 pw = getpwuid(uc.cr_uid);
583 iderror(lport, fport, s, errno == 0 ? ID_NOUSER : ID_UNKNOWN);
586 snprintf(idbuf, sizeof(idbuf), "%u", (unsigned)pw->pw_uid);
588 strlcpy(idbuf, pw->pw_name, sizeof(idbuf));
591 * If enabled, we check for a file named ".noident" in the user's
592 * home directory. If found, we return HIDDEN-USER.
595 if (asprintf(&p, "%s/.noident", pw->pw_dir) == -1)
596 iderror(lport, fport, s, ID_UNKNOWN);
597 if (lstat(p, &sb) == 0) {
599 iderror(lport, fport, s, ID_HIDDEN);
605 * Here, if enabled, we read a user's ".fakeid" file in their
606 * home directory. It consists of a line containing the name
613 * Here we set ourself to effectively be the user, so we don't
614 * open any files we have no permission to open, especially
615 * symbolic links to sensitive root-owned files or devices.
617 if (initgroups(pw->pw_name, pw->pw_gid) == -1)
618 iderror(lport, fport, s, ID_UNKNOWN);
619 if (seteuid(pw->pw_uid) == -1)
620 iderror(lport, fport, s, ID_UNKNOWN);
622 * We can't stat() here since that would be a race
624 * Therefore, we open the file we have permissions to open
625 * and if it's not a regular file, we close it and end up
626 * returning the user's real username.
628 if (asprintf(&p, "%s/.fakeid", pw->pw_dir) == -1)
629 iderror(lport, fport, s, ID_UNKNOWN);
630 fakeid_fd = open(p, O_RDONLY | O_NONBLOCK);
632 if (fakeid_fd == -1 || fstat(fakeid_fd, &sb) == -1 ||
633 !S_ISREG(sb.st_mode))
636 if ((ssize = read(fakeid_fd, buf, sizeof(buf) - 1)) < 0)
641 * Usually, the file will have the desired identity
642 * in the form "identity\n". Allow for leading white
643 * space and trailing white space/end of line.
646 p += strspn(p, " \t");
647 p[strcspn(p, " \t\r\n")] = '\0';
648 if (strlen(p) > MAXLOGNAME - 1) /* Too long (including nul)? */
649 p[MAXLOGNAME - 1] = '\0';
652 * If the name is a zero-length string or matches it
653 * the id or name of another user (unless permitted by -F)
654 * then it is invalid.
660 if (p[strspn(p, "0123456789")] == '\0' &&
661 getpwuid(atoi(p)) != NULL)
664 if (getpwnam(p) != NULL)
669 strlcpy(idbuf, p, sizeof(idbuf));
677 /* Finally, we make and send the reply. */
678 if (asprintf(&p, "%d , %d : USERID : %s : %s\r\n", lport, fport, osname,
680 syslog(LOG_ERR, "asprintf: %m");
683 send(s, p, strlen(p), MSG_EOF);
690 * RFC738 Time Server.
691 * Return a machine readable date and time, in the form of the
692 * number of seconds since midnight, Jan 1, 1900. Since gettimeofday
693 * returns the number of seconds since midnight, Jan 1, 1970,
694 * we must add 2208988800 seconds to this figure to make up for
695 * some seventy years Bell Labs was asleep.
703 if (gettimeofday(&tv, (struct timezone *)NULL) < 0) {
705 warnx("unable to get time of day");
708 #define OFFSET ((u_long)25567 * 24*60*60)
709 return (htonl((long)(tv.tv_sec + OFFSET)));
719 unsigned long result;
720 struct sockaddr_storage ss;
724 if (recvfrom(s, (char *)&result, sizeof(result), 0,
725 (struct sockaddr *)&ss, &size) < 0)
728 if (check_loop((struct sockaddr *)&ss, sep))
732 (void) sendto(s, (char *) &result, sizeof(result), 0,
733 (struct sockaddr *)&ss, size);
738 machtime_stream(s, sep)
740 struct servtab *sep __unused;
742 unsigned long result;
745 (void) send(s, (char *) &result, sizeof(result), MSG_EOF);
749 * RFC1078 TCP Port Service Multiplexer (TCPMUX). Service connections to
750 * services based on the service name sent.
752 * Based on TCPMUX.C by Mark K. Lottor November 1988
753 * sri-nic::ps:<mkl>tcpmux.c
756 #define MAX_SERV_LEN (256+2) /* 2 bytes for \r\n */
757 #define strwrite(fd, buf) (void) write(fd, buf, sizeof(buf)-1)
759 static int /* # of characters upto \r,\n or \0 */
760 getline(fd, buf, len)
769 sigemptyset(&sa.sa_mask);
770 sa.sa_handler = SIG_DFL;
771 sigaction(SIGALRM, &sa, (struct sigaction *)0);
774 n = read(fd, buf, len-count);
781 if (*buf == '\r' || *buf == '\n' || *buf == '\0')
786 } while (count < len);
795 char service[MAX_SERV_LEN+1];
798 /* Get requested service name */
799 if ((len = getline(s, service, MAX_SERV_LEN)) < 0) {
800 strwrite(s, "-Error reading service name\r\n");
806 warnx("tcpmux: someone wants %s", service);
809 * Help is a required command, and lists available services,
812 if (!strcasecmp(service, "help")) {
813 for (sep = servtab; sep; sep = sep->se_next) {
816 (void)write(s,sep->se_service,strlen(sep->se_service));
822 /* Try matching a service in inetd.conf with the request */
823 for (sep = servtab; sep; sep = sep->se_next) {
826 if (!strcasecmp(service, sep->se_service)) {
827 if (ISMUXPLUS(sep)) {
828 strwrite(s, "+Go\r\n");
833 strwrite(s, "-Service not available\r\n");