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