xref: /csrg-svn/libexec/rlogind/rlogind.c (revision 36633)
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.27 (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 
37 #include <stdio.h>
38 #include <sys/types.h>
39 #include <sys/stat.h>
40 #include <sys/socket.h>
41 #include <sys/wait.h>
42 #include <sys/file.h>
43 #include <sys/param.h>
44 
45 #include <netinet/in.h>
46 
47 #include <errno.h>
48 #include <pwd.h>
49 #include <signal.h>
50 #include <sys/ioctl.h>
51 #include <sys/termios.h>
52 #include <stdio.h>
53 #include <netdb.h>
54 #include <syslog.h>
55 #include <strings.h>
56 
57 #ifndef TIOCPKT_WINDOW
58 #define TIOCPKT_WINDOW 0x80
59 #endif
60 
61 #ifdef	KERBEROS
62 #include <kerberos/krb.h>
63 #define	SECURE_MESSAGE "This rlogin session is using DES encryption for all transmissions.\r\n"
64 
65 AUTH_DAT	*kdata;
66 KTEXT		ticket;
67 u_char		auth_buf[sizeof(AUTH_DAT)];
68 u_char		tick_buf[sizeof(KTEXT_ST)];
69 Key_schedule	schedule;
70 int		encrypt, retval, use_kerberos = 0, vacuous = 0;
71 int		do_krb_login();
72 
73 #define		OLD_RCMD		0x00
74 #define		KERB_RCMD		0x00
75 #define		KERB_RCMD_MUTUAL	0x03
76 
77 #define		ARGSTR			"lnkv"
78 #else
79 #define		ARGSTR			"ln"
80 #endif	/* KERBEROS */
81 
82 char	*env[2];
83 #define	NMAX 30
84 char	lusername[NMAX+1], rusername[NMAX+1];
85 static	char term[64] = "TERM=";
86 #define	ENVSIZE	(sizeof("TERM=")-1)	/* skip null for concatenation */
87 int	keepalive = 1;
88 
89 #define	SUPERUSER(pwd)	((pwd)->pw_uid == 0)
90 
91 extern	int errno;
92 int	reapchild();
93 struct	passwd *getpwnam(), *pwd;
94 char	*malloc();
95 
96 main(argc, argv)
97 	int argc;
98 	char **argv;
99 {
100 	extern int opterr, optind, _check_rhosts_file;
101 	int ch;
102 	int on = 1, fromlen;
103 	struct sockaddr_in from;
104 
105 	openlog("rlogind", LOG_PID | LOG_CONS, LOG_AUTH);
106 
107 	opterr = 0;
108 	while ((ch = getopt(argc, argv, ARGSTR)) != EOF)
109 		switch (ch) {
110 		case 'l':
111 			_check_rhosts_file = 0;
112 			break;
113 		case 'n':
114 			keepalive = 0;
115 			break;
116 #ifdef KERBEROS
117 		case 'k':
118 			use_kerberos = 1;
119 			break;
120 		case 'v':
121 			vacuous = 1;
122 			break;
123 #endif
124 		case '?':
125 		default:
126 			usage();
127 			break;
128 		}
129 	argc -= optind;
130 	argv += optind;
131 
132 #ifdef	KERBEROS
133 	if (use_kerberos && vacuous) {
134 		usage();
135 		fatal("only one of -k and -v allowed\n");
136 	}
137 #endif
138 	fromlen = sizeof (from);
139 	if (getpeername(0, &from, &fromlen) < 0) {
140 		syslog(LOG_ERR,"Can't get peer name of remote host: %m");
141 		fatalperror("Can't get peer name of remote host");
142 	}
143 	if (keepalive &&
144 	    setsockopt(0, SOL_SOCKET, SO_KEEPALIVE, &on, sizeof (on)) < 0)
145 		syslog(LOG_WARNING, "setsockopt (SO_KEEPALIVE): %m");
146 	doit(0, &from);
147 }
148 
149 int	child;
150 int	cleanup();
151 int	netf;
152 char	*line;
153 extern	char	*inet_ntoa();
154 
155 struct winsize win = { 0, 0, 0, 0 };
156 
157 
158 doit(f, fromp)
159 	int f;
160 	struct sockaddr_in *fromp;
161 {
162 	int i, p, t, pid, on = 1;
163 	int authenticated = 0, hostok = 0;
164 	register struct hostent *hp;
165 	char remotehost[2 * MAXHOSTNAMELEN + 1];
166 	struct hostent hostent;
167 	char c;
168 
169 	alarm(60);
170 	read(f, &c, 1);
171 
172 #ifdef	KERBEROS
173 	/*
174 	 * XXX 1st char tells us which client we're talking to
175 	 */
176 	switch (c) {
177 
178 	case OLD_RCMD:		/* OLD_RCMD is same as KERB_RCMD */
179 		if (vacuous)
180 			fatal(f, "Remote host requires Kerberos authentication");
181 		break;
182 
183 	case KERB_RCMD_MUTUAL:
184 		encrypt = 1;
185 		break;
186 
187 	default:
188 		fatal(f, "Remote protocol error");
189 	}
190 #else
191 	if (c != 0)
192 		exit(1);
193 #endif
194 
195 	alarm(0);
196 	fromp->sin_port = ntohs((u_short)fromp->sin_port);
197 	hp = gethostbyaddr(&fromp->sin_addr, sizeof (struct in_addr),
198 		fromp->sin_family);
199 	if (hp == 0) {
200 		/*
201 		 * Only the name is used below.
202 		 */
203 		hp = &hostent;
204 		hp->h_name = inet_ntoa(fromp->sin_addr);
205 	} else if (local_domain(hp->h_name)) {
206 		/*
207 		 * If name returned by gethostbyaddr is in our domain,
208 		 * attempt to verify that we haven't been fooled by someone
209 		 * in a remote net; look up the name and check that this
210 		 * address corresponds to the name.
211 		 */
212 		strncpy(remotehost, hp->h_name, sizeof(remotehost) - 1);
213 		remotehost[sizeof(remotehost) - 1] = 0;
214 		hp = gethostbyname(remotehost);
215 		if (hp)
216 		    for (; hp->h_addr_list[0]; hp->h_addr_list++) {
217 			if (!bcmp(hp->h_addr_list[0], (caddr_t)&fromp->sin_addr,
218 			    sizeof(fromp->sin_addr))) {
219 				hostok++;
220 				break;
221 			}
222 		}
223 	}
224 
225 #ifdef	KERBEROS
226 	if (use_kerberos) {
227 		retval = do_krb_login(hp->h_name, fromp, encrypt);
228 		write(f, &c, 1);
229 		if (retval == 0)
230 			authenticated++;
231 		else if (retval > 0)
232 			fatal(f, krb_err_txt[retval]);
233 	} else
234 #endif
235 	if (fromp->sin_family != AF_INET ||
236 	    fromp->sin_port >= IPPORT_RESERVED ||
237 	    fromp->sin_port < IPPORT_RESERVED/2) {
238 		syslog(LOG_NOTICE, "Connection from %s on illegal port",
239 			inet_ntoa(fromp->sin_addr));
240 		fatal(f, "Permission denied");
241 	}
242 	write(f, "", 1);
243 
244 	if (do_rlogin(hp->h_name) == 0) {
245 		if (hostok)
246 			authenticated++;
247 		else
248 			write(f, "rlogind: Host address mismatch.\r\n",
249 			    sizeof("rlogind: Host address mismatch.\r\n") - 1);
250 	}
251 
252 	for (c = 'p'; c <= 's'; c++) {
253 		struct stat stb;
254 		line = "/dev/ptyXX";
255 		line[strlen("/dev/pty")] = c;
256 		line[strlen("/dev/ptyp")] = '0';
257 		if (stat(line, &stb) < 0)
258 			break;
259 		for (i = 0; i < 16; i++) {
260 			line[sizeof("/dev/ptyp") - 1] = "0123456789abcdef"[i];
261 			p = open(line, O_RDWR);
262 			if (p > 0)
263 				goto gotpty;
264 		}
265 	}
266 	fatal(f, "Out of ptys");
267 	/*NOTREACHED*/
268 gotpty:
269 	(void) ioctl(p, TIOCSWINSZ, &win);
270 	netf = f;
271 	line[strlen("/dev/")] = 't';
272 	t = open(line, O_RDWR);
273 	if (t < 0)
274 		fatalperror(f, line);
275 	if (fchmod(t, 0))
276 		fatalperror(f, line);
277 	(void)signal(SIGHUP, SIG_IGN);
278 	vhangup();
279 	(void)signal(SIGHUP, SIG_DFL);
280 	t = open(line, O_RDWR);
281 	if (t < 0)
282 		fatalperror(f, line);
283 	setup_term(t);
284 #ifdef DEBUG
285 	{
286 		int tt = open("/dev/tty", O_RDWR);
287 		if (tt > 0) {
288 			(void)ioctl(tt, TIOCNOTTY, 0);
289 			(void)close(tt);
290 		}
291 	}
292 #endif
293 	pid = fork();
294 	if (pid < 0)
295 		fatalperror(f, "");
296 	if (pid == 0) {
297 		if (setsid() < 0)
298 			fatalperror(f, "setsid");
299 		if (ioctl(t, TIOCSCTTY, 0) < 0)
300 			fatalperror(f, "ioctl(sctty)");
301 		close(f), close(p);
302 		dup2(t, 0), dup2(t, 1), dup2(t, 2);
303 		close(t);
304 		if (authenticated)
305 			execl("/bin/login", "login", "-p",
306 			    "-h", hp->h_name, "-f", lusername, 0);
307 		else
308 			execl("/bin/login", "login", "-p",
309 			    "-h", hp->h_name, lusername, 0);
310 		fatalperror(2, "/bin/login");
311 		/*NOTREACHED*/
312 	}
313 	close(t);
314 
315 #ifdef	KERBEROS
316 	/*
317 	 * If encrypted, don't turn on NBIO or the des read/write
318 	 * routines will croak.
319 	 */
320 
321 	if (encrypt)
322 		(void) des_write(f, SECURE_MESSAGE, sizeof(SECURE_MESSAGE));
323 	else
324 #endif
325 		ioctl(f, FIONBIO, &on);
326 	ioctl(p, FIONBIO, &on);
327 	ioctl(p, TIOCPKT, &on);
328 	signal(SIGTSTP, SIG_IGN);
329 	signal(SIGCHLD, cleanup);
330 	setpgrp(0, 0);
331 	protocol(f, p);
332 	signal(SIGCHLD, SIG_IGN);
333 	cleanup();
334 }
335 
336 char	magic[2] = { 0377, 0377 };
337 char	oobdata[] = {TIOCPKT_WINDOW};
338 
339 /*
340  * Handle a "control" request (signaled by magic being present)
341  * in the data stream.  For now, we are only willing to handle
342  * window size changes.
343  */
344 control(pty, cp, n)
345 	int pty;
346 	char *cp;
347 	int n;
348 {
349 	struct winsize w;
350 
351 	if (n < 4+sizeof (w) || cp[2] != 's' || cp[3] != 's')
352 		return (0);
353 	oobdata[0] &= ~TIOCPKT_WINDOW;	/* we know he heard */
354 	bcopy(cp+4, (char *)&w, sizeof(w));
355 	w.ws_row = ntohs(w.ws_row);
356 	w.ws_col = ntohs(w.ws_col);
357 	w.ws_xpixel = ntohs(w.ws_xpixel);
358 	w.ws_ypixel = ntohs(w.ws_ypixel);
359 	(void)ioctl(pty, TIOCSWINSZ, &w);
360 	return (4+sizeof (w));
361 }
362 
363 /*
364  * rlogin "protocol" machine.
365  */
366 protocol(f, p)
367 	int f, p;
368 {
369 	char pibuf[1024], fibuf[1024], *pbp, *fbp;
370 	register pcc = 0, fcc = 0;
371 	int cc, nfd, pmask, fmask;
372 	char cntl;
373 
374 	/*
375 	 * Must ignore SIGTTOU, otherwise we'll stop
376 	 * when we try and set slave pty's window shape
377 	 * (our controlling tty is the master pty).
378 	 */
379 	(void) signal(SIGTTOU, SIG_IGN);
380 	send(f, oobdata, 1, MSG_OOB);	/* indicate new rlogin */
381 	if (f > p)
382 		nfd = f + 1;
383 	else
384 		nfd = p + 1;
385 	fmask = 1 << f;
386 	pmask = 1 << p;
387 	for (;;) {
388 		int ibits, obits, ebits;
389 
390 		ibits = 0;
391 		obits = 0;
392 		if (fcc)
393 			obits |= pmask;
394 		else
395 			ibits |= fmask;
396 		if (pcc >= 0)
397 			if (pcc)
398 				obits |= fmask;
399 			else
400 				ibits |= pmask;
401 		ebits = pmask;
402 		if (select(nfd, &ibits, obits ? &obits : (int *)NULL,
403 		    &ebits, 0) < 0) {
404 			if (errno == EINTR)
405 				continue;
406 			fatalperror(f, "select");
407 		}
408 		if (ibits == 0 && obits == 0 && ebits == 0) {
409 			/* shouldn't happen... */
410 			sleep(5);
411 			continue;
412 		}
413 #define	pkcontrol(c)	((c)&(TIOCPKT_FLUSHWRITE|TIOCPKT_NOSTOP|TIOCPKT_DOSTOP))
414 		if (ebits & pmask) {
415 			cc = read(p, &cntl, 1);
416 			if (cc == 1 && pkcontrol(cntl)) {
417 				cntl |= oobdata[0];
418 				send(f, &cntl, 1, MSG_OOB);
419 				if (cntl & TIOCPKT_FLUSHWRITE) {
420 					pcc = 0;
421 					ibits &= ~pmask;
422 				}
423 			}
424 		}
425 		if (ibits & fmask) {
426 #ifdef	KERBEROS
427 			if (encrypt)
428 				fcc = des_read(f, fibuf, sizeof(fibuf));
429 			else
430 #endif
431 				fcc = read(f, fibuf, sizeof(fibuf));
432 			if (fcc < 0 && errno == EWOULDBLOCK)
433 				fcc = 0;
434 			else {
435 				register char *cp;
436 				int left, n;
437 
438 				if (fcc <= 0)
439 					break;
440 				fbp = fibuf;
441 
442 			top:
443 				for (cp = fibuf; cp < fibuf+fcc-1; cp++)
444 					if (cp[0] == magic[0] &&
445 					    cp[1] == magic[1]) {
446 						left = fcc - (cp-fibuf);
447 						n = control(p, cp, left);
448 						if (n) {
449 							left -= n;
450 							if (left > 0)
451 								bcopy(cp+n, cp, left);
452 							fcc -= n;
453 							goto top; /* n^2 */
454 						}
455 					}
456 				obits |= pmask;		/* try write */
457 			}
458 		}
459 
460 		if ((obits & pmask) && fcc > 0) {
461 			cc = write(p, fbp, fcc);
462 			if (cc > 0) {
463 				fcc -= cc;
464 				fbp += cc;
465 			}
466 		}
467 
468 		if (ibits & pmask) {
469 			pcc = read(p, pibuf, sizeof (pibuf));
470 			pbp = pibuf;
471 			if (pcc < 0 && errno == EWOULDBLOCK)
472 				pcc = 0;
473 			else if (pcc <= 0)
474 				break;
475 			else if (pibuf[0] == 0) {
476 				pbp++, pcc--;
477 #ifdef	KERBEROS
478 				if (!encrypt)
479 #endif
480 					obits |= fmask;	/* try a write */
481 			} else {
482 				if (pkcontrol(pibuf[0])) {
483 					pibuf[0] |= oobdata[0];
484 					send(f, &pibuf[0], 1, MSG_OOB);
485 				}
486 				pcc = 0;
487 			}
488 		}
489 		if ((obits & fmask) && pcc > 0) {
490 #ifdef	KERBEROS
491 			if (encrypt)
492 				cc = des_write(f, pbp, pcc);
493 			else
494 #endif
495 				cc = write(f, pbp, pcc);
496 			if (cc < 0 && errno == EWOULDBLOCK) {
497 				/* also shouldn't happen */
498 				sleep(5);
499 				continue;
500 			}
501 			if (cc > 0) {
502 				pcc -= cc;
503 				pbp += cc;
504 			}
505 		}
506 	}
507 }
508 
509 cleanup()
510 {
511 	char *p;
512 
513 	p = line + sizeof("/dev/") - 1;
514 	if (logout(p))
515 		logwtmp(p, "", "");
516 	(void)chmod(line, 0666);
517 	(void)chown(line, 0, 0);
518 	*p = 'p';
519 	(void)chmod(line, 0666);
520 	(void)chown(line, 0, 0);
521 	shutdown(netf, 2);
522 	exit(1);
523 }
524 
525 fatal(f, msg)
526 	int f;
527 	char *msg;
528 {
529 	char buf[BUFSIZ];
530 
531 	buf[0] = '\01';		/* error indicator */
532 	(void) sprintf(buf + 1, "rlogind: %s.\r\n", msg);
533 	(void) write(f, buf, strlen(buf));
534 	exit(1);
535 }
536 
537 fatalperror(f, msg)
538 	int f;
539 	char *msg;
540 {
541 	char buf[BUFSIZ];
542 	extern int sys_nerr;
543 	extern char *sys_errlist[];
544 
545 	if ((unsigned)errno < sys_nerr)
546 		(void) sprintf(buf, "%s: %s", msg, sys_errlist[errno]);
547 	else
548 		(void) sprintf(buf, "%s: Error %d", msg, errno);
549 	fatal(f, buf);
550 }
551 
552 do_rlogin(host)
553 	char *host;
554 {
555 
556 	getstr(rusername, sizeof(rusername), "remuser too long");
557 	getstr(lusername, sizeof(lusername), "locuser too long");
558 	getstr(term+ENVSIZE, sizeof(term)-ENVSIZE, "Terminal type too long");
559 
560 	if (getuid())
561 		return(-1);
562 	pwd = getpwnam(lusername);
563 	if (pwd == NULL)
564 		return(-1);
565 	return(ruserok(host, SUPERUSER(pwd), rusername, lusername));
566 }
567 
568 
569 getstr(buf, cnt, errmsg)
570 	char *buf;
571 	int cnt;
572 	char *errmsg;
573 {
574 	char c;
575 
576 	do {
577 		if (read(0, &c, 1) != 1)
578 			exit(1);
579 		if (--cnt < 0)
580 			fatal(1, errmsg);
581 		*buf++ = c;
582 	} while (c != 0);
583 }
584 
585 extern	char **environ;
586 
587 setup_term(fd)
588 	int fd;
589 {
590 	struct termios tt;
591 	register char *cp = index(term+ENVSIZE, '/');
592 	char *speed;
593 
594 	tcgetattr(fd, &tt);
595 	if (cp) {
596 		*cp++ = '\0';
597 		speed = cp;
598 		cp = index(speed, '/');
599 		if (cp)
600 			*cp++ = '\0';
601 		cfsetspeed(&tt, atoi(speed));
602 	}
603 	tt.c_iflag = BRKINT|ICRNL|IXON|ISTRIP|IEXTEN|IMAXBEL;
604 	tt.c_oflag = OPOST|ONLCR|OXTABS;
605 	tt.c_lflag = ISIG|ICANON|ECHO;
606 	tcsetattr(fd, TCSADFLUSH, &tt);
607 
608 	env[0] = term;
609 	env[1] = 0;
610 	environ = env;
611 }
612 
613 #ifdef	KERBEROS
614 #define	VERSION_SIZE	9
615 
616 /*
617  * Do the remote kerberos login to the named host with the
618  * given inet address
619  *
620  * Return 0 on valid authorization
621  * Return -1 on valid authentication, no authorization
622  * Return >0 for error conditions
623  */
624 do_krb_login(host, dest, encrypt)
625 	char *host;
626 	struct sockaddr_in *dest;
627 	int encrypt;
628 {
629 	int rc;
630 	char instance[INST_SZ], version[VERSION_SIZE];
631 	long authopts = 0L;	/* !mutual */
632 	struct sockaddr_in faddr;
633 
634 	if (getuid())
635 		return(KFAILURE);
636 
637 	kdata = (AUTH_DAT *) auth_buf;
638 	ticket = (KTEXT) tick_buf;
639 	strcpy(instance, "*");
640 
641 	if (encrypt) {
642 		rc = sizeof(faddr);
643 		if (getsockname(0, &faddr, &rc))
644 			return(-1);
645 		authopts = KOPT_DO_MUTUAL;
646 		rc = krb_recvauth(
647 			authopts, 0,
648 			ticket, "rcmd",
649 			instance, dest, &faddr,
650 			kdata, "", schedule, version);
651 		 des_set_key(kdata->session, schedule);
652 
653 	} else {
654 		rc = krb_recvauth(
655 			authopts, 0,
656 			ticket, "rcmd",
657 			instance, dest, (struct sockaddr_in *) 0,
658 			kdata, "", (bit_64 *) 0, version);
659 	}
660 
661 	if (rc != KSUCCESS)
662 		return(rc);
663 
664 	if ((rc = krb_kntoln(kdata, rusername)) != KSUCCESS)
665 		return(rc);
666 
667 	getstr(lusername, sizeof(lusername), "locuser");
668 	/* get the "cmd" in the rcmd protocol */
669 	getstr(term+ENVSIZE, sizeof(term)-ENVSIZE, "Terminal type");
670 
671 	pwd = getpwnam(lusername);
672 	if (pwd == NULL)
673 		return(-1);
674 
675 	/* returns nonzero for no access */
676 	/* return(ruserok(host, SUPERUSER(pwd), rusername, lusername)); */
677 	if (kuserok(kdata,lusername) != 0)
678 		return(-1);
679 
680 	return(0);
681 
682 }
683 
684 #endif /* KERBEROS */
685 
686 usage()
687 {
688 #ifdef	KERBEROS
689 	syslog(LOG_ERR, "usage: rlogind [-k | -v] [-l] [-n]");
690 #else
691 	syslog(LOG_ERR, "usage: rlogind [-l] [-n]");
692 #endif
693 }
694 
695 /*
696  * Check whether host h is in our local domain,
697  * as determined by the part of the name following
698  * the first '.' in its name and in ours.
699  * If either name is unqualified (contains no '.'),
700  * assume that the host is local, as it will be
701  * interpreted as such.
702  */
703 local_domain(h)
704 	char *h;
705 {
706 	char localhost[MAXHOSTNAMELEN];
707 	char *p1, *p2 = index(h, '.');
708 
709 	(void) gethostname(localhost, sizeof(localhost));
710 	p1 = index(localhost, '.');
711 	if (p1 == NULL || p2 == NULL || !strcasecmp(p1, p2))
712 		return(1);
713 	return(0);
714 }
715