builtins.c revision 49029
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 * $Id: builtins.c,v 1.4 1999/07/23 03:51:52 green Exp $
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 <limits.h>
44#include <pwd.h>
45#include <signal.h>
46#include <stdlib.h>
47#include <string.h>
48#include <unistd.h>
49
50#include "inetd.h"
51
52extern int	 debug;
53extern struct servtab *servtab;
54
55char ring[128];
56char *endring;
57
58int check_loop __P((struct sockaddr_in *, struct servtab *sep));
59void inetd_setproctitle __P((char *, int));
60
61struct biltin biltins[] = {
62	/* Echo received data */
63	{ "echo",	SOCK_STREAM,	1, -1,	echo_stream },
64	{ "echo",	SOCK_DGRAM,	0, 1,	echo_dg },
65
66	/* Internet /dev/null */
67	{ "discard",	SOCK_STREAM,	1, -1,	discard_stream },
68	{ "discard",	SOCK_DGRAM,	0, 1,	discard_dg },
69
70	/* Return 32 bit time since 1970 */
71	{ "time",	SOCK_STREAM,	0, -1,	machtime_stream },
72	{ "time",	SOCK_DGRAM,	0, 1,	machtime_dg },
73
74	/* Return human-readable time */
75	{ "daytime",	SOCK_STREAM,	0, -1,	daytime_stream },
76	{ "daytime",	SOCK_DGRAM,	0, 1,	daytime_dg },
77
78	/* Familiar character generator */
79	{ "chargen",	SOCK_STREAM,	1, -1,	chargen_stream },
80	{ "chargen",	SOCK_DGRAM,	0, 1,	chargen_dg },
81
82	{ "tcpmux",	SOCK_STREAM,	1, -1,	(void (*)())tcpmux },
83
84	{ "auth",	SOCK_STREAM,	1, -1,	ident_stream },
85
86	{ NULL }
87};
88
89void
90initring()
91{
92	int i;
93
94	endring = ring;
95
96	for (i = 0; i <= 128; ++i)
97		if (isprint(i))
98			*endring++ = i;
99}
100
101/* ARGSUSED */
102void
103chargen_dg(s, sep)		/* Character generator */
104	int s;
105	struct servtab *sep;
106{
107	struct sockaddr_in sin;
108	static char *rs;
109	int len, size;
110	char text[LINESIZ+2];
111
112	if (endring == 0) {
113		initring();
114		rs = ring;
115	}
116
117	size = sizeof(sin);
118	if (recvfrom(s, text, sizeof(text), 0,
119		     (struct sockaddr *)&sin, &size) < 0)
120		return;
121
122	if (check_loop(&sin, sep))
123		return;
124
125	if ((len = endring - rs) >= LINESIZ)
126		memmove(text, rs, LINESIZ);
127	else {
128		memmove(text, rs, len);
129		memmove(text + len, ring, LINESIZ - len);
130	}
131	if (++rs == endring)
132		rs = ring;
133	text[LINESIZ] = '\r';
134	text[LINESIZ + 1] = '\n';
135	(void) sendto(s, text, sizeof(text), 0,
136		      (struct sockaddr *)&sin, sizeof(sin));
137}
138
139/* ARGSUSED */
140void
141chargen_stream(s, sep)		/* Character generator */
142	int s;
143	struct servtab *sep;
144{
145	int len;
146	char *rs, text[LINESIZ+2];
147
148	inetd_setproctitle(sep->se_service, s);
149
150	if (!endring) {
151		initring();
152		rs = ring;
153	}
154
155	text[LINESIZ] = '\r';
156	text[LINESIZ + 1] = '\n';
157	for (rs = ring;;) {
158		if ((len = endring - rs) >= LINESIZ)
159			memmove(text, rs, LINESIZ);
160		else {
161			memmove(text, rs, len);
162			memmove(text + len, ring, LINESIZ - len);
163		}
164		if (++rs == endring)
165			rs = ring;
166		if (write(s, text, sizeof(text)) != sizeof(text))
167			break;
168	}
169	exit(0);
170}
171
172/* ARGSUSED */
173void
174daytime_dg(s, sep)		/* Return human-readable time of day */
175	int s;
176	struct servtab *sep;
177{
178	char buffer[256];
179	time_t clock;
180	struct sockaddr_in sin;
181	int size;
182
183	clock = time((time_t *) 0);
184
185	size = sizeof(sin);
186	if (recvfrom(s, buffer, sizeof(buffer), 0,
187		     (struct sockaddr *)&sin, &size) < 0)
188		return;
189
190	if (check_loop(&sin, sep))
191		return;
192
193	(void) sprintf(buffer, "%.24s\r\n", ctime(&clock));
194	(void) sendto(s, buffer, strlen(buffer), 0,
195		      (struct sockaddr *)&sin, sizeof(sin));
196}
197
198/* ARGSUSED */
199void
200daytime_stream(s, sep)		/* Return human-readable time of day */
201	int s;
202	struct servtab *sep;
203{
204	char buffer[256];
205	time_t clock;
206
207	clock = time((time_t *) 0);
208
209	(void) sprintf(buffer, "%.24s\r\n", ctime(&clock));
210	(void) write(s, buffer, strlen(buffer));
211}
212
213/* ARGSUSED */
214void
215discard_dg(s, sep)		/* Discard service -- ignore data */
216	int s;
217	struct servtab *sep;
218{
219	char buffer[BUFSIZE];
220
221	(void) read(s, buffer, sizeof(buffer));
222}
223
224/* ARGSUSED */
225void
226discard_stream(s, sep)		/* Discard service -- ignore data */
227	int s;
228	struct servtab *sep;
229{
230	int ret;
231	char buffer[BUFSIZE];
232
233	inetd_setproctitle(sep->se_service, s);
234	while (1) {
235		while ((ret = read(s, buffer, sizeof(buffer))) > 0)
236			;
237		if (ret == 0 || errno != EINTR)
238			break;
239	}
240	exit(0);
241}
242
243/* ARGSUSED */
244void
245echo_dg(s, sep)			/* Echo service -- echo data back */
246	int s;
247	struct servtab *sep;
248{
249	char buffer[BUFSIZE];
250	int i, size;
251	struct sockaddr_in sin;
252
253	size = sizeof(sin);
254	if ((i = recvfrom(s, buffer, sizeof(buffer), 0,
255			  (struct sockaddr *)&sin, &size)) < 0)
256		return;
257
258	if (check_loop(&sin, sep))
259		return;
260
261	(void) sendto(s, buffer, i, 0, (struct sockaddr *)&sin,
262		      sizeof(sin));
263}
264
265/* ARGSUSED */
266void
267echo_stream(s, sep)		/* Echo service -- echo data back */
268	int s;
269	struct servtab *sep;
270{
271	char buffer[BUFSIZE];
272	int i;
273
274	inetd_setproctitle(sep->se_service, s);
275	while ((i = read(s, buffer, sizeof(buffer))) > 0 &&
276	    write(s, buffer, i) > 0)
277		;
278	exit(0);
279}
280
281/* ARGSUSED */
282void
283iderror(lport, fport, s, er)
284	int lport, fport, s, er;
285{
286	char *p;
287
288	er = asprintf(&p, "%d , %d : ERROR : %s\r\n", lport, fport,
289	    er == -1 ? "HIDDEN-USER" : er ? strerror(er) : "UNKNOWN-ERROR");
290	if (er == -1)
291		exit(0);
292	write(s, p, strlen(p));
293	free(p);
294
295	exit(0);
296}
297
298/* ARGSUSED */
299void
300ident_stream(s, sep)		/* Ident service */
301	int s;
302	struct servtab *sep;
303{
304	struct sockaddr_in sin[2];
305	struct ucred uc;
306	struct passwd *pw;
307	struct timeval tv = {
308		10,
309		0
310	};
311	fd_set fdset;
312	char buf[BUFSIZE], *cp = NULL, *p, **av, *osname = NULL;
313	int len, c, rflag = 0, fflag = 0, argc = 0;
314	u_short lport, fport;
315
316	inetd_setproctitle(sep->se_service, s);
317	optind = 1;
318	optreset = 1;
319	for (av = sep->se_argv; *av; av++)
320		argc++;
321	if (argc) {
322		while ((c = getopt(argc, sep->se_argv, "fro:t:")) != -1)
323			switch (c) {
324			case 'f':
325				fflag = 1;
326				break;
327			case 'r':
328				rflag = 1;
329				break;
330			case 'o':
331				osname = optarg;
332				break;
333			case 't':
334				do {
335					int sec, usec;
336
337					switch (sscanf(optarg, "%d.%d", &sec,
338					    &usec)) {
339					case 2:
340						tv.tv_usec = usec;
341					case 1:
342						tv.tv_sec = sec;
343						break;
344					default:
345						if (debug)
346							warnx("bad argument to -t option");
347						break;
348					}
349				} while (0);
350			default:
351				break;
352			}
353	}
354	if (osname == NULL) {
355		struct utsname un;
356
357		if (uname(&un))
358			iderror(0, 0, s, errno);
359		osname = un.sysname;
360	}
361	len = sizeof(sin[0]);
362	if (getsockname(s, (struct sockaddr *)&sin[0], &len) == -1)
363		iderror(0, 0, s, errno);
364	len = sizeof(sin[1]);
365	if (getpeername(s, (struct sockaddr *)&sin[1], &len) == -1)
366		iderror(0, 0, s, errno);
367	FD_ZERO(&fdset);
368	FD_SET(s, &fdset);
369	if (select(s + 1, &fdset, NULL, NULL, &tv) == -1)
370		iderror(0, 0, s, errno);
371	if (ioctl(s, FIONREAD, &len) == -1)
372		iderror(0, 0, s, errno);
373	if (len >= sizeof(buf))
374		len = sizeof(buf) - 1;
375	len = read(s, buf, len);
376	if (len == -1)
377		iderror(0, 0, s, errno);
378	buf[len] = '\0';
379	if (sscanf(buf, "%hu , %hu", &lport, &fport) != 2)
380		iderror(0, 0, s, 0);
381	if (!rflag)
382		iderror(lport, fport, s, -1);
383	sin[0].sin_port = htons(lport);
384	sin[1].sin_port = htons(fport);
385	len = sizeof(uc);
386	if (sysctlbyname("net.inet.tcp.getcred", &uc, &len, sin,
387	    sizeof(sin)) == -1)
388		iderror(lport, fport, s, errno);
389	pw = getpwuid(uc.cr_uid);
390	if (pw == NULL)
391		iderror(lport, fport, s, errno);
392	if (fflag) {
393		FILE *fakeid = NULL;
394		char fakeid_path[PATH_MAX];
395		struct stat sb;
396		seteuid(pw->pw_uid);
397		setegid(pw->pw_gid);
398		snprintf(fakeid_path, sizeof(fakeid_path), "%s/.fakeid",
399		    pw->pw_dir);
400		if ((fakeid = fopen(fakeid_path, "r")) != NULL &&
401		    fstat(fileno(fakeid), &sb) != -1 && S_ISREG(sb.st_mode)) {
402			buf[sizeof(buf) - 1] = '\0';
403			if (fgets(buf, sizeof(buf), fakeid) == NULL) {
404				cp = pw->pw_name;
405				fclose(fakeid);
406				goto printit;
407			}
408			fclose(fakeid);
409			strtok(buf, "\r\n");
410			if (strlen(buf) > 16)
411				buf[16] = '\0';
412			cp = buf;
413			while (isspace(*cp))
414				cp++;
415			strtok(cp, " \t");
416			if (!*cp || getpwnam(cp))
417				cp = getpwuid(uc.cr_uid)->pw_name;
418		} else
419			cp = pw->pw_name;
420	} else
421		cp = pw->pw_name;
422printit:
423	if (asprintf(&p, "%d , %d : USERID : %s : %s\r\n", lport, fport, osname,
424	    cp) == -1)
425		iderror(0, 0, s, errno);
426	write(s, p, strlen(p));
427	free(p);
428
429	exit(0);
430}
431
432/*
433 * Return a machine readable date and time, in the form of the
434 * number of seconds since midnight, Jan 1, 1900.  Since gettimeofday
435 * returns the number of seconds since midnight, Jan 1, 1970,
436 * we must add 2208988800 seconds to this figure to make up for
437 * some seventy years Bell Labs was asleep.
438 */
439
440unsigned long
441machtime()
442{
443	struct timeval tv;
444
445	if (gettimeofday(&tv, (struct timezone *)NULL) < 0) {
446		if (debug)
447			warnx("unable to get time of day");
448		return (0L);
449	}
450#define	OFFSET ((u_long)25567 * 24*60*60)
451	return (htonl((long)(tv.tv_sec + OFFSET)));
452#undef OFFSET
453}
454
455/* ARGSUSED */
456void
457machtime_dg(s, sep)
458	int s;
459	struct servtab *sep;
460{
461	unsigned long result;
462	struct sockaddr_in sin;
463	int size;
464
465	size = sizeof(sin);
466	if (recvfrom(s, (char *)&result, sizeof(result), 0,
467		     (struct sockaddr *)&sin, &size) < 0)
468		return;
469
470	if (check_loop(&sin, sep))
471		return;
472
473	result = machtime();
474	(void) sendto(s, (char *) &result, sizeof(result), 0,
475		      (struct sockaddr *)&sin, sizeof(sin));
476}
477
478/* ARGSUSED */
479void
480machtime_stream(s, sep)
481	int s;
482	struct servtab *sep;
483{
484	unsigned long result;
485
486	result = machtime();
487	(void) write(s, (char *) &result, sizeof(result));
488}
489
490/*
491 *  Based on TCPMUX.C by Mark K. Lottor November 1988
492 *  sri-nic::ps:<mkl>tcpmux.c
493 */
494
495#define MAX_SERV_LEN	(256+2)		/* 2 bytes for \r\n */
496#define strwrite(fd, buf)	(void) write(fd, buf, sizeof(buf)-1)
497
498static int		/* # of characters upto \r,\n or \0 */
499getline(fd, buf, len)
500	int fd;
501	char *buf;
502	int len;
503{
504	int count = 0, n;
505	struct sigaction sa;
506
507	sa.sa_flags = 0;
508	sigemptyset(&sa.sa_mask);
509	sa.sa_handler = SIG_DFL;
510	sigaction(SIGALRM, &sa, (struct sigaction *)0);
511	do {
512		alarm(10);
513		n = read(fd, buf, len-count);
514		alarm(0);
515		if (n == 0)
516			return (count);
517		if (n < 0)
518			return (-1);
519		while (--n >= 0) {
520			if (*buf == '\r' || *buf == '\n' || *buf == '\0')
521				return (count);
522			count++;
523			buf++;
524		}
525	} while (count < len);
526	return (count);
527}
528
529struct servtab *
530tcpmux(s)
531	int s;
532{
533	struct servtab *sep;
534	char service[MAX_SERV_LEN+1];
535	int len;
536
537	/* Get requested service name */
538	if ((len = getline(s, service, MAX_SERV_LEN)) < 0) {
539		strwrite(s, "-Error reading service name\r\n");
540		return (NULL);
541	}
542	service[len] = '\0';
543
544	if (debug)
545		warnx("tcpmux: someone wants %s", service);
546
547	/*
548	 * Help is a required command, and lists available services,
549	 * one per line.
550	 */
551	if (!strcasecmp(service, "help")) {
552		for (sep = servtab; sep; sep = sep->se_next) {
553			if (!ISMUX(sep))
554				continue;
555			(void)write(s,sep->se_service,strlen(sep->se_service));
556			strwrite(s, "\r\n");
557		}
558		return (NULL);
559	}
560
561	/* Try matching a service in inetd.conf with the request */
562	for (sep = servtab; sep; sep = sep->se_next) {
563		if (!ISMUX(sep))
564			continue;
565		if (!strcasecmp(service, sep->se_service)) {
566			if (ISMUXPLUS(sep)) {
567				strwrite(s, "+Go\r\n");
568			}
569			return (sep);
570		}
571	}
572	strwrite(s, "-Service not available\r\n");
573	return (NULL);
574}
575
576