builtins.c revision 49004
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$
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:")) != -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			default:
334				break;
335			}
336	}
337	if (osname == NULL) {
338		struct utsname un;
339
340		if (uname(&un))
341			iderror(0, 0, s, errno);
342		osname = un.sysname;
343	}
344	len = sizeof(sin[0]);
345	if (getsockname(s, (struct sockaddr *)&sin[0], &len) == -1)
346		iderror(0, 0, s, errno);
347	len = sizeof(sin[1]);
348	if (getpeername(s, (struct sockaddr *)&sin[1], &len) == -1)
349		iderror(0, 0, s, errno);
350	FD_ZERO(&fdset);
351	FD_SET(s, &fdset);
352	if (select(s + 1, &fdset, NULL, NULL, &tv) == -1)
353		iderror(0, 0, s, errno);
354	if (ioctl(s, FIONREAD, &len) == -1)
355		iderror(0, 0, s, errno);
356	if (len >= sizeof(buf))
357		len = sizeof(buf) - 1;
358	len = read(s, buf, len);
359	if (len == -1)
360		iderror(0, 0, s, errno);
361	buf[len] = '\0';
362	if (sscanf(buf, "%hu,%hu", &lport, &fport) != 2)
363		iderror(0, 0, s, 0);
364	if (!rflag)
365		iderror(lport, fport, s, -1);
366	sin[0].sin_port = htons(lport);
367	sin[1].sin_port = htons(fport);
368	len = sizeof(uc);
369	if (sysctlbyname("net.inet.tcp.getcred", &uc, &len, sin,
370	    sizeof(sin)) == -1)
371		iderror(lport, fport, s, errno);
372	pw = getpwuid(uc.cr_uid);
373	if (pw == NULL)
374		iderror(lport, fport, s, errno);
375	if (fflag) {
376		FILE *fakeid = NULL;
377		char fakeid_path[PATH_MAX];
378		struct stat sb;
379		seteuid(pw->pw_uid);
380		setegid(pw->pw_gid);
381		snprintf(fakeid_path, sizeof(fakeid_path), "%s/.fakeid",
382		    pw->pw_dir);
383		if ((fakeid = fopen(fakeid_path, "r")) != NULL &&
384		    fstat(fileno(fakeid), &sb) != -1 && S_ISREG(sb.st_mode)) {
385			buf[sizeof(buf) - 1] = '\0';
386			if (fgets(buf, sizeof(buf), fakeid) == NULL) {
387				cp = pw->pw_name;
388				fclose(fakeid);
389				goto printit;
390			}
391			fclose(fakeid);
392			strtok(buf, "\r\n");
393			if (strlen(buf) > 16)
394				buf[16] = '\0';
395			cp = buf;
396			while (isspace(*cp))
397				cp++;
398			strtok(cp, " \t");
399			if (!*cp || getpwnam(cp))
400				cp = getpwuid(uc.cr_uid)->pw_name;
401		} else
402			cp = pw->pw_name;
403	} else
404		cp = pw->pw_name;
405printit:
406	if (asprintf(&p, "%d , %d : USERID : %s : %s\r\n", lport, fport, osname,
407	    cp) == -1)
408		iderror(0, 0, s, errno);
409	write(s, p, strlen(p));
410	free(p);
411
412	exit(0);
413}
414
415/*
416 * Return a machine readable date and time, in the form of the
417 * number of seconds since midnight, Jan 1, 1900.  Since gettimeofday
418 * returns the number of seconds since midnight, Jan 1, 1970,
419 * we must add 2208988800 seconds to this figure to make up for
420 * some seventy years Bell Labs was asleep.
421 */
422
423unsigned long
424machtime()
425{
426	struct timeval tv;
427
428	if (gettimeofday(&tv, (struct timezone *)NULL) < 0) {
429		if (debug)
430			warnx("unable to get time of day");
431		return (0L);
432	}
433#define	OFFSET ((u_long)25567 * 24*60*60)
434	return (htonl((long)(tv.tv_sec + OFFSET)));
435#undef OFFSET
436}
437
438/* ARGSUSED */
439void
440machtime_dg(s, sep)
441	int s;
442	struct servtab *sep;
443{
444	unsigned long result;
445	struct sockaddr_in sin;
446	int size;
447
448	size = sizeof(sin);
449	if (recvfrom(s, (char *)&result, sizeof(result), 0,
450		     (struct sockaddr *)&sin, &size) < 0)
451		return;
452
453	if (check_loop(&sin, sep))
454		return;
455
456	result = machtime();
457	(void) sendto(s, (char *) &result, sizeof(result), 0,
458		      (struct sockaddr *)&sin, sizeof(sin));
459}
460
461/* ARGSUSED */
462void
463machtime_stream(s, sep)
464	int s;
465	struct servtab *sep;
466{
467	unsigned long result;
468
469	result = machtime();
470	(void) write(s, (char *) &result, sizeof(result));
471}
472
473/*
474 *  Based on TCPMUX.C by Mark K. Lottor November 1988
475 *  sri-nic::ps:<mkl>tcpmux.c
476 */
477
478#define MAX_SERV_LEN	(256+2)		/* 2 bytes for \r\n */
479#define strwrite(fd, buf)	(void) write(fd, buf, sizeof(buf)-1)
480
481static int		/* # of characters upto \r,\n or \0 */
482getline(fd, buf, len)
483	int fd;
484	char *buf;
485	int len;
486{
487	int count = 0, n;
488	struct sigaction sa;
489
490	sa.sa_flags = 0;
491	sigemptyset(&sa.sa_mask);
492	sa.sa_handler = SIG_DFL;
493	sigaction(SIGALRM, &sa, (struct sigaction *)0);
494	do {
495		alarm(10);
496		n = read(fd, buf, len-count);
497		alarm(0);
498		if (n == 0)
499			return (count);
500		if (n < 0)
501			return (-1);
502		while (--n >= 0) {
503			if (*buf == '\r' || *buf == '\n' || *buf == '\0')
504				return (count);
505			count++;
506			buf++;
507		}
508	} while (count < len);
509	return (count);
510}
511
512struct servtab *
513tcpmux(s)
514	int s;
515{
516	struct servtab *sep;
517	char service[MAX_SERV_LEN+1];
518	int len;
519
520	/* Get requested service name */
521	if ((len = getline(s, service, MAX_SERV_LEN)) < 0) {
522		strwrite(s, "-Error reading service name\r\n");
523		return (NULL);
524	}
525	service[len] = '\0';
526
527	if (debug)
528		warnx("tcpmux: someone wants %s", service);
529
530	/*
531	 * Help is a required command, and lists available services,
532	 * one per line.
533	 */
534	if (!strcasecmp(service, "help")) {
535		for (sep = servtab; sep; sep = sep->se_next) {
536			if (!ISMUX(sep))
537				continue;
538			(void)write(s,sep->se_service,strlen(sep->se_service));
539			strwrite(s, "\r\n");
540		}
541		return (NULL);
542	}
543
544	/* Try matching a service in inetd.conf with the request */
545	for (sep = servtab; sep; sep = sep->se_next) {
546		if (!ISMUX(sep))
547			continue;
548		if (!strcasecmp(service, sep->se_service)) {
549			if (ISMUXPLUS(sep)) {
550				strwrite(s, "+Go\r\n");
551			}
552			return (sep);
553		}
554	}
555	strwrite(s, "-Service not available\r\n");
556	return (NULL);
557}
558
559