xref: /csrg-svn/libexec/rlogind/rlogind.c (revision 36632)
1 /*
2  * Copyright (c) 1983, 1988 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that the above copyright notice and this paragraph are
7  * duplicated in all such forms and that any documentation,
8  * advertising materials, and other materials related to such
9  * distribution and use acknowledge that the software was developed
10  * by the University of California, Berkeley.  The name of the
11  * University may not be used to endorse or promote products derived
12  * from this software without specific prior written permission.
13  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16  */
17 
18 #ifndef lint
19 char copyright[] =
20 "@(#) Copyright (c) 1983, 1988 The Regents of the University of California.\n\
21  All rights reserved.\n";
22 #endif /* not lint */
23 
24 #ifndef lint
25 static char sccsid[] = "@(#)rlogind.c	5.22.1.4 (Berkeley) 01/25/89";
26 #endif /* not lint */
27 
28 /*
29  * remote login server:
30  *	\0
31  *	remuser\0
32  *	locuser\0
33  *	terminal_type/speed\0
34  *	data
35  *
36  * Automatic login protocol is done here, using login -f upon success,
37  * unless OLD_LOGIN is defined (then done in login, ala 4.2/4.3BSD).
38  */
39 
40 #include <stdio.h>
41 #include <sys/param.h>
42 #include <sys/stat.h>
43 #include <sys/socket.h>
44 #include <sys/wait.h>
45 #include <sys/file.h>
46 
47 #include <netinet/in.h>
48 
49 #include <errno.h>
50 #include <pwd.h>
51 #include <signal.h>
52 #include <sgtty.h>
53 #include <stdio.h>
54 #include <netdb.h>
55 #include <syslog.h>
56 #include <strings.h>
57 
58 #ifndef TIOCPKT_WINDOW
59 #define TIOCPKT_WINDOW 0x80
60 #endif
61 
62 char	*env[2];
63 #define	NMAX 30
64 char	lusername[NMAX+1], rusername[NMAX+1];
65 static	char term[64] = "TERM=";
66 #define	ENVSIZE	(sizeof("TERM=")-1)	/* skip null for concatenation */
67 int	keepalive = 1;
68 
69 #define	SUPERUSER(pwd)	((pwd)->pw_uid == 0)
70 
71 extern	int errno;
72 int	reapchild();
73 struct	passwd *getpwnam(), *pwd;
74 char	*malloc();
75 
76 main(argc, argv)
77 	int argc;
78 	char **argv;
79 {
80 	extern int opterr, optind, _check_rhosts_file;
81 	int ch;
82 	int on = 1, fromlen;
83 	struct sockaddr_in from;
84 
85 	openlog("rlogind", LOG_PID | LOG_CONS, LOG_AUTH);
86 
87 	opterr = 0;
88 	while ((ch = getopt(argc, argv, "ln")) != EOF)
89 		switch (ch) {
90 		case 'l':
91 			_check_rhosts_file = 0;
92 			break;
93 		case 'n':
94 			keepalive = 0;
95 			break;
96 		case '?':
97 		default:
98 			syslog(LOG_ERR, "usage: rlogind [-l] [-n]");
99 			break;
100 		}
101 	argc -= optind;
102 	argv += optind;
103 
104 	fromlen = sizeof (from);
105 	if (getpeername(0, &from, &fromlen) < 0) {
106 		syslog(LOG_ERR, "Couldn't get peer name of remote host: %m");
107 		fatalperror("Can't get peer name of host");
108 	}
109 	if (keepalive &&
110 	    setsockopt(0, SOL_SOCKET, SO_KEEPALIVE, &on, sizeof (on)) < 0)
111 		syslog(LOG_WARNING, "setsockopt (SO_KEEPALIVE): %m");
112 	doit(0, &from);
113 }
114 
115 int	child;
116 int	cleanup();
117 int	netf;
118 char	*line;
119 extern	char	*inet_ntoa();
120 
121 struct winsize win = { 0, 0, 0, 0 };
122 
123 
124 doit(f, fromp)
125 	int f;
126 	struct sockaddr_in *fromp;
127 {
128 	int i, p, t, pid, on = 1;
129 #ifndef OLD_LOGIN
130 	int authenticated = 0, hostok = 0;
131 	char remotehost[2 * MAXHOSTNAMELEN + 1];
132 #endif
133 	register struct hostent *hp;
134 	struct hostent hostent;
135 	char c;
136 
137 	alarm(60);
138 	read(f, &c, 1);
139 	if (c != 0)
140 		exit(1);
141 
142 	alarm(0);
143 	fromp->sin_port = ntohs((u_short)fromp->sin_port);
144 	hp = gethostbyaddr(&fromp->sin_addr, sizeof (struct in_addr),
145 		fromp->sin_family);
146 	if (hp == 0) {
147 		/*
148 		 * Only the name is used below.
149 		 */
150 		hp = &hostent;
151 		hp->h_name = inet_ntoa(fromp->sin_addr);
152 #ifndef OLD_LOGIN
153 	} else if (local_domain(hp->h_name)) {
154 		/*
155 		 * If name returned by gethostbyaddr is in our domain,
156 		 * attempt to verify that we haven't been fooled by someone
157 		 * in a remote net; look up the name and check that this
158 		 * address corresponds to the name.
159 		 */
160 		strncpy(remotehost, hp->h_name, sizeof(remotehost) - 1);
161 		remotehost[sizeof(remotehost) - 1] = 0;
162 		hp = gethostbyname(remotehost);
163 		if (hp)
164 		    for (; hp->h_addr_list[0]; hp->h_addr_list++) {
165 			if (!bcmp(hp->h_addr_list[0], (caddr_t)&fromp->sin_addr,
166 			    sizeof(fromp->sin_addr))) {
167 				hostok++;
168 				break;
169 			}
170 		}
171 #endif
172 	}
173 
174 	if (fromp->sin_family != AF_INET ||
175 	    fromp->sin_port >= IPPORT_RESERVED ||
176 	    fromp->sin_port < IPPORT_RESERVED/2) {
177 		syslog(LOG_NOTICE, "Connection from %s on illegal port",
178 			inet_ntoa(fromp->sin_addr));
179 		fatal(f, "Permission denied");
180 	}
181 	write(f, "", 1);
182 #ifndef OLD_LOGIN
183 	if (do_rlogin(hp->h_name) == 0) {
184 		if (hostok)
185 		    authenticated++;
186 		else
187 		    write(f, "rlogind: Host address mismatch.\r\n",
188 		     sizeof("rlogind: Host address mismatch.\r\n") - 1);
189 	}
190 #endif
191 
192 	for (c = 'p'; c <= 's'; c++) {
193 		struct stat stb;
194 		line = "/dev/ptyXX";
195 		line[strlen("/dev/pty")] = c;
196 		line[strlen("/dev/ptyp")] = '0';
197 		if (stat(line, &stb) < 0)
198 			break;
199 		for (i = 0; i < 16; i++) {
200 			line[sizeof("/dev/ptyp") - 1] = "0123456789abcdef"[i];
201 			p = open(line, O_RDWR);
202 			if (p > 0)
203 				goto gotpty;
204 		}
205 	}
206 	fatal(f, "Out of ptys");
207 	/*NOTREACHED*/
208 gotpty:
209 	(void) ioctl(p, TIOCSWINSZ, &win);
210 	netf = f;
211 	line[strlen("/dev/")] = 't';
212 	t = open(line, O_RDWR);
213 	if (t < 0)
214 		fatalperror(f, line);
215 	if (fchmod(t, 0))
216 		fatalperror(f, line);
217 	(void)signal(SIGHUP, SIG_IGN);
218 	vhangup();
219 	(void)signal(SIGHUP, SIG_DFL);
220 	t = open(line, O_RDWR);
221 	if (t < 0)
222 		fatalperror(f, line);
223 	setup_term(t);
224 #ifdef DEBUG
225 	{
226 		int tt = open("/dev/tty", O_RDWR);
227 		if (tt > 0) {
228 			(void)ioctl(tt, TIOCNOTTY, 0);
229 			(void)close(tt);
230 		}
231 	}
232 #endif
233 	pid = fork();
234 	if (pid < 0)
235 		fatalperror(f, "");
236 	if (pid == 0) {
237 		close(f), close(p);
238 		dup2(t, 0), dup2(t, 1), dup2(t, 2);
239 		close(t);
240 #ifdef OLD_LOGIN
241 		execl("/bin/login", "login", "-r", hp->h_name, 0);
242 #else /* OLD_LOGIN */
243 		if (authenticated)
244 			execl("/bin/login", "login", "-p", "-h", hp->h_name,
245 			    "-f", lusername, 0);
246 		else
247 			execl("/bin/login", "login", "-p", "-h", hp->h_name,
248 			    lusername, 0);
249 #endif /* OLD_LOGIN */
250 		fatalperror(2, "/bin/login");
251 		/*NOTREACHED*/
252 	}
253 	close(t);
254 
255 	ioctl(f, FIONBIO, &on);
256 	ioctl(p, FIONBIO, &on);
257 	ioctl(p, TIOCPKT, &on);
258 	signal(SIGTSTP, SIG_IGN);
259 	signal(SIGCHLD, cleanup);
260 	setpgrp(0, 0);
261 	protocol(f, p);
262 	signal(SIGCHLD, SIG_IGN);
263 	cleanup();
264 }
265 
266 char	magic[2] = { 0377, 0377 };
267 char	oobdata[] = {TIOCPKT_WINDOW};
268 
269 /*
270  * Handle a "control" request (signaled by magic being present)
271  * in the data stream.  For now, we are only willing to handle
272  * window size changes.
273  */
274 control(pty, cp, n)
275 	int pty;
276 	char *cp;
277 	int n;
278 {
279 	struct winsize w;
280 
281 	if (n < 4+sizeof (w) || cp[2] != 's' || cp[3] != 's')
282 		return (0);
283 	oobdata[0] &= ~TIOCPKT_WINDOW;	/* we know he heard */
284 	bcopy(cp+4, (char *)&w, sizeof(w));
285 	w.ws_row = ntohs(w.ws_row);
286 	w.ws_col = ntohs(w.ws_col);
287 	w.ws_xpixel = ntohs(w.ws_xpixel);
288 	w.ws_ypixel = ntohs(w.ws_ypixel);
289 	(void)ioctl(pty, TIOCSWINSZ, &w);
290 	return (4+sizeof (w));
291 }
292 
293 /*
294  * rlogin "protocol" machine.
295  */
296 protocol(f, p)
297 	int f, p;
298 {
299 	char pibuf[1024], fibuf[1024], *pbp, *fbp;
300 	register pcc = 0, fcc = 0;
301 	int cc, nfd, pmask, fmask;
302 	char cntl;
303 
304 	/*
305 	 * Must ignore SIGTTOU, otherwise we'll stop
306 	 * when we try and set slave pty's window shape
307 	 * (our controlling tty is the master pty).
308 	 */
309 	(void) signal(SIGTTOU, SIG_IGN);
310 	send(f, oobdata, 1, MSG_OOB);	/* indicate new rlogin */
311 	if (f > p)
312 		nfd = f + 1;
313 	else
314 		nfd = p + 1;
315 	fmask = 1 << f;
316 	pmask = 1 << p;
317 	for (;;) {
318 		int ibits, obits, ebits;
319 
320 		ibits = 0;
321 		obits = 0;
322 		if (fcc)
323 			obits |= pmask;
324 		else
325 			ibits |= fmask;
326 		if (pcc >= 0)
327 			if (pcc)
328 				obits |= fmask;
329 			else
330 				ibits |= pmask;
331 		ebits = pmask;
332 		if (select(nfd, &ibits, obits ? &obits : (int *)NULL,
333 		    &ebits, 0) < 0) {
334 			if (errno == EINTR)
335 				continue;
336 			fatalperror(f, "select");
337 		}
338 		if (ibits == 0 && obits == 0 && ebits == 0) {
339 			/* shouldn't happen... */
340 			sleep(5);
341 			continue;
342 		}
343 #define	pkcontrol(c)	((c)&(TIOCPKT_FLUSHWRITE|TIOCPKT_NOSTOP|TIOCPKT_DOSTOP))
344 		if (ebits & pmask) {
345 			cc = read(p, &cntl, 1);
346 			if (cc == 1 && pkcontrol(cntl)) {
347 				cntl |= oobdata[0];
348 				send(f, &cntl, 1, MSG_OOB);
349 				if (cntl & TIOCPKT_FLUSHWRITE) {
350 					pcc = 0;
351 					ibits &= ~pmask;
352 				}
353 			}
354 		}
355 		if (ibits & fmask) {
356 			fcc = read(f, fibuf, sizeof(fibuf));
357 			if (fcc < 0 && errno == EWOULDBLOCK)
358 				fcc = 0;
359 			else {
360 				register char *cp;
361 				int left, n;
362 
363 				if (fcc <= 0)
364 					break;
365 				fbp = fibuf;
366 
367 			top:
368 				for (cp = fibuf; cp < fibuf+fcc-1; cp++)
369 					if (cp[0] == magic[0] &&
370 					    cp[1] == magic[1]) {
371 						left = fcc - (cp-fibuf);
372 						n = control(p, cp, left);
373 						if (n) {
374 							left -= n;
375 							if (left > 0)
376 								bcopy(cp+n, cp, left);
377 							fcc -= n;
378 							goto top; /* n^2 */
379 						}
380 					}
381 				obits |= pmask;		/* try write */
382 			}
383 		}
384 
385 		if ((obits & pmask) && fcc > 0) {
386 			cc = write(p, fbp, fcc);
387 			if (cc > 0) {
388 				fcc -= cc;
389 				fbp += cc;
390 			}
391 		}
392 
393 		if (ibits & pmask) {
394 			pcc = read(p, pibuf, sizeof (pibuf));
395 			pbp = pibuf;
396 			if (pcc < 0 && errno == EWOULDBLOCK)
397 				pcc = 0;
398 			else if (pcc <= 0)
399 				break;
400 			else if (pibuf[0] == 0) {
401 				pbp++, pcc--;
402 				obits |= fmask;	/* try a write */
403 			} else {
404 				if (pkcontrol(pibuf[0])) {
405 					pibuf[0] |= oobdata[0];
406 					send(f, &pibuf[0], 1, MSG_OOB);
407 				}
408 				pcc = 0;
409 			}
410 		}
411 		if ((obits & fmask) && pcc > 0) {
412 			cc = write(f, pbp, pcc);
413 			if (cc < 0 && errno == EWOULDBLOCK) {
414 				/* also shouldn't happen */
415 				sleep(5);
416 				continue;
417 			}
418 			if (cc > 0) {
419 				pcc -= cc;
420 				pbp += cc;
421 			}
422 		}
423 	}
424 }
425 
426 cleanup()
427 {
428 	char *p;
429 
430 	p = line + sizeof("/dev/") - 1;
431 	if (logout(p))
432 		logwtmp(p, "", "");
433 	(void)chmod(line, 0666);
434 	(void)chown(line, 0, 0);
435 	*p = 'p';
436 	(void)chmod(line, 0666);
437 	(void)chown(line, 0, 0);
438 	shutdown(netf, 2);
439 	exit(1);
440 }
441 
442 fatal(f, msg)
443 	int f;
444 	char *msg;
445 {
446 	char buf[BUFSIZ];
447 
448 	buf[0] = '\01';		/* error indicator */
449 	(void) sprintf(buf + 1, "rlogind: %s.\r\n", msg);
450 	(void) write(f, buf, strlen(buf));
451 	exit(1);
452 }
453 
454 fatalperror(f, msg)
455 	int f;
456 	char *msg;
457 {
458 	char buf[BUFSIZ];
459 	extern int sys_nerr;
460 	extern char *sys_errlist[];
461 
462 	if ((unsigned)errno < sys_nerr)
463 		(void) sprintf(buf, "%s: %s", msg, sys_errlist[errno]);
464 	else
465 		(void) sprintf(buf, "%s: Error %d", msg, errno);
466 	fatal(f, buf);
467 }
468 
469 #ifndef OLD_LOGIN
470 do_rlogin(host)
471 	char *host;
472 {
473 
474 	getstr(rusername, sizeof(rusername), "remuser too long");
475 	getstr(lusername, sizeof(lusername), "locuser too long");
476 	getstr(term+ENVSIZE, sizeof(term)-ENVSIZE, "Terminal type too long");
477 
478 	if (getuid())
479 		return(-1);
480 	pwd = getpwnam(lusername);
481 	if (pwd == NULL)
482 		return(-1);
483 	return(ruserok(host, SUPERUSER(pwd), rusername, lusername));
484 }
485 
486 
487 getstr(buf, cnt, errmsg)
488 	char *buf;
489 	int cnt;
490 	char *errmsg;
491 {
492 	char c;
493 
494 	do {
495 		if (read(0, &c, 1) != 1)
496 			exit(1);
497 		if (--cnt < 0)
498 			fatal(1, errmsg);
499 		*buf++ = c;
500 	} while (c != 0);
501 }
502 
503 extern	char **environ;
504 
505 char *speeds[] = {
506 	"0", "50", "75", "110", "134", "150", "200", "300", "600",
507 	"1200", "1800", "2400", "4800", "9600", "19200", "38400",
508 };
509 #define	NSPEEDS	(sizeof(speeds) / sizeof(speeds[0]))
510 
511 setup_term(fd)
512 	int fd;
513 {
514 	register char *cp = index(term, '/'), **cpp;
515 	struct sgttyb sgttyb;
516 	char *speed;
517 
518 	(void)ioctl(fd, TIOCGETP, &sgttyb);
519 	if (cp) {
520 		*cp++ = '\0';
521 		speed = cp;
522 		cp = index(speed, '/');
523 		if (cp)
524 			*cp++ = '\0';
525 		for (cpp = speeds; cpp < &speeds[NSPEEDS]; cpp++)
526 		    if (strcmp(*cpp, speed) == 0) {
527 			sgttyb.sg_ispeed = sgttyb.sg_ospeed = cpp - speeds;
528 			break;
529 		    }
530 	}
531 	sgttyb.sg_flags = ECHO|CRMOD|ANYP|XTABS;
532 	(void)ioctl(fd, TIOCSETP, &sgttyb);
533 
534 	env[0] = term;
535 	env[1] = 0;
536 	environ = env;
537 }
538 
539 /*
540  * Check whether host h is in our local domain,
541  * as determined by the part of the name following
542  * the first '.' in its name and in ours.
543  * If either name is unqualified (contains no '.'),
544  * assume that the host is local, as it will be
545  * interpreted as such.
546  */
547 local_domain(h)
548 	char *h;
549 {
550 	char localhost[MAXHOSTNAMELEN];
551 	char *p1, *p2 = index(h, '.');
552 
553 	(void) gethostname(localhost, sizeof(localhost));
554 	p1 = index(localhost, '.');
555 	if (p1 == NULL || p2 == NULL || !strcasecmp(p1, p2))
556 		return(1);
557 	return(0);
558 }
559 #endif /* OLD_LOGIN */
560