xref: /titanic_50/usr/src/cmd/cmd-inet/usr.sbin/in.rlogind.c (revision 32885d593baf8bac788fa78885893a51b3ad0f28)
17c478bd9Sstevel@tonic-gate /*
2*32885d59Sgtb  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
37c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
47c478bd9Sstevel@tonic-gate  */
57c478bd9Sstevel@tonic-gate 
67c478bd9Sstevel@tonic-gate /*	Copyright(c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
77c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
87c478bd9Sstevel@tonic-gate 
97c478bd9Sstevel@tonic-gate /*
107c478bd9Sstevel@tonic-gate  * Copyright (c) 1983 The Regents of the University of California.
117c478bd9Sstevel@tonic-gate  * All rights reserved.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * Redistribution and use in source and binary forms are permitted
147c478bd9Sstevel@tonic-gate  * provided that the above copyright notice and this paragraph are
157c478bd9Sstevel@tonic-gate  * duplicated in all such forms and that any documentation,
167c478bd9Sstevel@tonic-gate  * advertising materials, and other materials related to such
177c478bd9Sstevel@tonic-gate  * distribution and use acknowledge that the software was developed
187c478bd9Sstevel@tonic-gate  * by the University of California, Berkeley.  The name of the
197c478bd9Sstevel@tonic-gate  * University may not be used to endorse or promote products derived
207c478bd9Sstevel@tonic-gate  * from this software without specific prior written permission.
217c478bd9Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
227c478bd9Sstevel@tonic-gate  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
237c478bd9Sstevel@tonic-gate  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate /*
297c478bd9Sstevel@tonic-gate  * remote login server:
307c478bd9Sstevel@tonic-gate  *	remuser\0
317c478bd9Sstevel@tonic-gate  *	locuser\0
327c478bd9Sstevel@tonic-gate  *	terminal info\0
337c478bd9Sstevel@tonic-gate  *	data
347c478bd9Sstevel@tonic-gate  */
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate #include <time.h>
377c478bd9Sstevel@tonic-gate #include <sys/types.h>
387c478bd9Sstevel@tonic-gate #include <sys/stat.h>
397c478bd9Sstevel@tonic-gate #include <sys/socket.h>
407c478bd9Sstevel@tonic-gate #include <sys/wait.h>
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate #include <netinet/in.h>
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate #include <errno.h>
457c478bd9Sstevel@tonic-gate #include <signal.h>
467c478bd9Sstevel@tonic-gate #include <fcntl.h>
477c478bd9Sstevel@tonic-gate #include <stdio.h>
487c478bd9Sstevel@tonic-gate #include <netdb.h>
497c478bd9Sstevel@tonic-gate #include <syslog.h>
507c478bd9Sstevel@tonic-gate #include <string.h>
517c478bd9Sstevel@tonic-gate #include <unistd.h>
527c478bd9Sstevel@tonic-gate #include <stdlib.h>
537c478bd9Sstevel@tonic-gate #include <alloca.h>
547c478bd9Sstevel@tonic-gate #include <stropts.h>
557c478bd9Sstevel@tonic-gate #include <sac.h>	/* for SC_WILDC */
567c478bd9Sstevel@tonic-gate #include <utmpx.h>
577c478bd9Sstevel@tonic-gate #include <sys/filio.h>
587c478bd9Sstevel@tonic-gate #include <sys/logindmux.h>
597c478bd9Sstevel@tonic-gate #include <sys/rlioctl.h>
607c478bd9Sstevel@tonic-gate #include <sys/termios.h>
617c478bd9Sstevel@tonic-gate #include <sys/tihdr.h>
627c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
637c478bd9Sstevel@tonic-gate #include <security/pam_appl.h>
647c478bd9Sstevel@tonic-gate #include <strings.h>
657c478bd9Sstevel@tonic-gate #include <com_err.h>
667c478bd9Sstevel@tonic-gate #include <k5-int.h>
677c478bd9Sstevel@tonic-gate #include <kcmd.h>
687c478bd9Sstevel@tonic-gate #include <krb5_repository.h>
697c478bd9Sstevel@tonic-gate #include <sys/cryptmod.h>
707c478bd9Sstevel@tonic-gate #include <bsm/adt.h>
717243fb49Sjbeck #include <addr_match.h>
72*32885d59Sgtb #include <store_forw_creds.h>
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate #define	KRB5_RECVAUTH_V5 5
757c478bd9Sstevel@tonic-gate #define	UT_NAMESIZE	sizeof (((struct utmpx *)0)->ut_name)
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate static char lusername[UT_NAMESIZE+1];
787c478bd9Sstevel@tonic-gate static char rusername[UT_NAMESIZE+1];
797c478bd9Sstevel@tonic-gate static char *krusername = NULL;
807c478bd9Sstevel@tonic-gate static char term[64];
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate static krb5_ccache ccache = NULL;
837c478bd9Sstevel@tonic-gate static krb5_keyblock *session_key = NULL;
847c478bd9Sstevel@tonic-gate static int chksum_flag = 0;
857c478bd9Sstevel@tonic-gate static int use_auth = 0;
867c478bd9Sstevel@tonic-gate static enum kcmd_proto kcmd_protocol;
877c478bd9Sstevel@tonic-gate #ifdef ALLOW_KCMD_V2
887c478bd9Sstevel@tonic-gate static krb5_data encr_iv = { NULL, 0 };
897c478bd9Sstevel@tonic-gate static krb5_data decr_iv = { NULL, 0 };
907c478bd9Sstevel@tonic-gate #endif /* ALLOW_KCMD_V2 */
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate #define	CHKSUM_REQUIRED 0x01
937c478bd9Sstevel@tonic-gate #define	CHKSUM_IGNORED  0x02
947c478bd9Sstevel@tonic-gate #define	VALID_CHKSUM(x) ((x) == 0 || (x) == CHKSUM_REQUIRED ||\
957c478bd9Sstevel@tonic-gate 			(x) == CHKSUM_IGNORED)
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate #define	PWD_IF_FAIL  0x01
987c478bd9Sstevel@tonic-gate #define	PWD_REQUIRED 0x02
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate #define	AUTH_NONE 0x00
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate #define	ARGSTR "k5exEXciM:s:S:D:"
1037c478bd9Sstevel@tonic-gate #define	DEFAULT_TOS 16
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate #define	KRB5_PROG_NAME "krlogin"
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate #define	SECURE_MSG "This rlogin session is using encryption " \
1087c478bd9Sstevel@tonic-gate 	"for all data transmissions.\r\n"
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate #define	KRB_V5_SENDAUTH_VERS	"KRB5_SENDAUTH_V1.0"
1117c478bd9Sstevel@tonic-gate #define	KRB5_RECVAUTH_V5	5
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate static krb5_error_code krb5_compat_recvauth(krb5_context context,
1147c478bd9Sstevel@tonic-gate 					    krb5_auth_context *auth_context,
1157c478bd9Sstevel@tonic-gate 					    krb5_pointer fdp,
1167c478bd9Sstevel@tonic-gate 					    krb5_principal server,
1177c478bd9Sstevel@tonic-gate 					    krb5_int32 flags,
1187c478bd9Sstevel@tonic-gate 					    krb5_keytab keytab,
1197c478bd9Sstevel@tonic-gate 					    krb5_ticket **ticket,
1207c478bd9Sstevel@tonic-gate 					    krb5_int32 *auth_sys,
1217c478bd9Sstevel@tonic-gate 					    krb5_data *version);
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate static void do_krb_login(int, char *, char *, krb5_context, int, krb5_keytab);
1247c478bd9Sstevel@tonic-gate static int configure_stream(int, krb5_keyblock *, int, krb5_data *, uint_t);
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate extern krb5_error_code krb5_read_message(krb5_context, krb5_pointer,
1277c478bd9Sstevel@tonic-gate 					krb5_data *);
1287c478bd9Sstevel@tonic-gate extern krb5_error_code krb5_net_read(krb5_context, int, char *, int);
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate #define	LOGIN_PROGRAM "/bin/login"
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate #define	DEFAULT_PROG_NAME	"rlogin"
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate static const char *pam_prog_name = DEFAULT_PROG_NAME;
1357c478bd9Sstevel@tonic-gate static void	rmut(void);
1367c478bd9Sstevel@tonic-gate static void	doit(int,  struct sockaddr_storage *, krb5_context, int,
1377c478bd9Sstevel@tonic-gate 		    krb5_keytab);
1387c478bd9Sstevel@tonic-gate static void	protocol(int, int, int);
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate static int	readstream(int, char *, int);
1417c478bd9Sstevel@tonic-gate static void	fatal(int, const char *);
1427c478bd9Sstevel@tonic-gate static void	fatalperror(int, const char *);
1437c478bd9Sstevel@tonic-gate static int	send_oob(int fd, void *ptr, size_t count);
1447c478bd9Sstevel@tonic-gate static int	removemod(int f, char *modname);
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate static int
1477c478bd9Sstevel@tonic-gate issock(int fd)
1487c478bd9Sstevel@tonic-gate {
1497c478bd9Sstevel@tonic-gate 	struct stat stats;
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate 	if (fstat(fd, &stats) == -1)
1527c478bd9Sstevel@tonic-gate 		return (0);
1537c478bd9Sstevel@tonic-gate 	return (S_ISSOCK(stats.st_mode));
1547c478bd9Sstevel@tonic-gate }
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate /*
1577c478bd9Sstevel@tonic-gate  * audit_rlogin_settid stores the terminal id while it is still
1587c478bd9Sstevel@tonic-gate  * available.  Subsequent calls to adt_load_hostname() return
1597c478bd9Sstevel@tonic-gate  * the id which is stored here.
1607c478bd9Sstevel@tonic-gate  */
1617c478bd9Sstevel@tonic-gate static int
1627c478bd9Sstevel@tonic-gate audit_rlogin_settid(int fd) {
1637c478bd9Sstevel@tonic-gate 	adt_session_data_t	*ah;
1647c478bd9Sstevel@tonic-gate 	adt_termid_t		*termid;
1657c478bd9Sstevel@tonic-gate 	int			rc;
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate 	if ((rc = adt_start_session(&ah, NULL, 0)) == 0) {
1687c478bd9Sstevel@tonic-gate 		if ((rc = adt_load_termid(fd, &termid)) == 0) {
1697c478bd9Sstevel@tonic-gate 			if ((rc = adt_set_user(ah, ADT_NO_AUDIT,
1707c478bd9Sstevel@tonic-gate 			    ADT_NO_AUDIT, 0, ADT_NO_AUDIT,
1717c478bd9Sstevel@tonic-gate 			    termid, ADT_SETTID)) == 0)
1727c478bd9Sstevel@tonic-gate 				(void) adt_set_proc(ah);
1737c478bd9Sstevel@tonic-gate 			free(termid);
1747c478bd9Sstevel@tonic-gate 		}
1757c478bd9Sstevel@tonic-gate 		(void) adt_end_session(ah);
1767c478bd9Sstevel@tonic-gate 	}
1777c478bd9Sstevel@tonic-gate 	return (rc);
1787c478bd9Sstevel@tonic-gate }
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate /* ARGSUSED */
182740638c8Sbw int
1837c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
1847c478bd9Sstevel@tonic-gate {
1857c478bd9Sstevel@tonic-gate 	int on = 1;
1867c478bd9Sstevel@tonic-gate 	socklen_t fromlen;
1877c478bd9Sstevel@tonic-gate 	struct sockaddr_storage from;
1887c478bd9Sstevel@tonic-gate 	int fd = -1;
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate 	extern char *optarg;
1917c478bd9Sstevel@tonic-gate 	char c;
1927c478bd9Sstevel@tonic-gate 	int tos = -1;
1937c478bd9Sstevel@tonic-gate 	krb5_context krb_context;
1947c478bd9Sstevel@tonic-gate 	krb5_keytab keytab = NULL;
1957c478bd9Sstevel@tonic-gate 	krb5_error_code status;
1967c478bd9Sstevel@tonic-gate 	char *realm = NULL;
1977c478bd9Sstevel@tonic-gate 	char *keytab_file = NULL;
1987c478bd9Sstevel@tonic-gate 	int encr_flag = 0;
1997c478bd9Sstevel@tonic-gate 	struct sockaddr_storage ouraddr;
2007c478bd9Sstevel@tonic-gate 	socklen_t ourlen;
2017c478bd9Sstevel@tonic-gate #ifdef DEBUG
2027c478bd9Sstevel@tonic-gate 	int debug_port = 0;
2037c478bd9Sstevel@tonic-gate #endif /* DEBUG */
2047c478bd9Sstevel@tonic-gate 	openlog("rlogind", LOG_PID | LOG_ODELAY, LOG_DAEMON);
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, ARGSTR)) != -1) {
2077c478bd9Sstevel@tonic-gate 		switch (c) {
2087c478bd9Sstevel@tonic-gate 		case 'k':
2097c478bd9Sstevel@tonic-gate 		case '5':
2107c478bd9Sstevel@tonic-gate 			use_auth = KRB5_RECVAUTH_V5;
2117c478bd9Sstevel@tonic-gate 			break;
2127c478bd9Sstevel@tonic-gate 		case 'e':
2137c478bd9Sstevel@tonic-gate 		case 'E':
2147c478bd9Sstevel@tonic-gate 		case 'x':
2157c478bd9Sstevel@tonic-gate 		case 'X':
2167c478bd9Sstevel@tonic-gate 			encr_flag = 1;
2177c478bd9Sstevel@tonic-gate 			break;
2187c478bd9Sstevel@tonic-gate 		case 'M':
2197c478bd9Sstevel@tonic-gate 			realm = (char *)strdup(optarg);
2207c478bd9Sstevel@tonic-gate 			break;
2217c478bd9Sstevel@tonic-gate 		case 'S':
2227c478bd9Sstevel@tonic-gate 			keytab_file = (char *)strdup(optarg);
2237c478bd9Sstevel@tonic-gate 			break;
2247c478bd9Sstevel@tonic-gate 		case 'c':
2257c478bd9Sstevel@tonic-gate 			chksum_flag |= CHKSUM_REQUIRED;
2267c478bd9Sstevel@tonic-gate 			break;
2277c478bd9Sstevel@tonic-gate 		case 'i':
2287c478bd9Sstevel@tonic-gate 			chksum_flag |= CHKSUM_IGNORED;
2297c478bd9Sstevel@tonic-gate 			break;
2307c478bd9Sstevel@tonic-gate 		case 's':
2317c478bd9Sstevel@tonic-gate 			if (optarg == NULL || (tos = atoi(optarg)) < 0 ||
2327c478bd9Sstevel@tonic-gate 			    tos > 255) {
2337c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR, "%s: illegal tos value: "
2347c478bd9Sstevel@tonic-gate 				    "%s\n", argv[0], optarg);
2357c478bd9Sstevel@tonic-gate 			} else {
2367c478bd9Sstevel@tonic-gate 				if (tos < 0)
2377c478bd9Sstevel@tonic-gate 					tos = DEFAULT_TOS;
2387c478bd9Sstevel@tonic-gate 			}
2397c478bd9Sstevel@tonic-gate 			break;
2407c478bd9Sstevel@tonic-gate #ifdef DEBUG
2417c478bd9Sstevel@tonic-gate 		case 'D':
2427c478bd9Sstevel@tonic-gate 			debug_port = atoi(optarg);
2437c478bd9Sstevel@tonic-gate 			break;
2447c478bd9Sstevel@tonic-gate #endif /* DEBUG */
2457c478bd9Sstevel@tonic-gate 		default:
2467c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, "Unrecognized command line option "
2477c478bd9Sstevel@tonic-gate 			    "(%s), exiting", argv[optind]);
2487c478bd9Sstevel@tonic-gate 			exit(EXIT_FAILURE);
2497c478bd9Sstevel@tonic-gate 		}
2507c478bd9Sstevel@tonic-gate 	}
2517c478bd9Sstevel@tonic-gate 	if (use_auth == KRB5_RECVAUTH_V5) {
2527c478bd9Sstevel@tonic-gate 		status = krb5_init_context(&krb_context);
2537c478bd9Sstevel@tonic-gate 		if (status) {
2547c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, "Error initializing krb5: %s",
2557c478bd9Sstevel@tonic-gate 			    error_message(status));
2567c478bd9Sstevel@tonic-gate 			exit(EXIT_FAILURE);
2577c478bd9Sstevel@tonic-gate 		}
2587c478bd9Sstevel@tonic-gate 		if (realm != NULL)
2597c478bd9Sstevel@tonic-gate 			krb5_set_default_realm(krb_context, realm);
2607c478bd9Sstevel@tonic-gate 		if (keytab_file != NULL) {
2617c478bd9Sstevel@tonic-gate 			if ((status = krb5_kt_resolve(krb_context,
2627c478bd9Sstevel@tonic-gate 						    keytab_file,
2637c478bd9Sstevel@tonic-gate 						    &keytab))) {
2647c478bd9Sstevel@tonic-gate 				com_err(argv[0],
2657c478bd9Sstevel@tonic-gate 					status,
2667c478bd9Sstevel@tonic-gate 					"while resolving srvtab file %s",
2677c478bd9Sstevel@tonic-gate 					keytab_file);
2687c478bd9Sstevel@tonic-gate 				exit(EXIT_FAILURE);
2697c478bd9Sstevel@tonic-gate 			}
2707c478bd9Sstevel@tonic-gate 		}
2717c478bd9Sstevel@tonic-gate 	}
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate #ifdef DEBUG
2747c478bd9Sstevel@tonic-gate 	if (debug_port) {
2757c478bd9Sstevel@tonic-gate 		int s;
2767c478bd9Sstevel@tonic-gate 		struct sockaddr_in sin;
2777c478bd9Sstevel@tonic-gate 
2787c478bd9Sstevel@tonic-gate 		if ((s = socket(AF_INET, SOCK_STREAM, PF_UNSPEC)) < 0) {
2797c478bd9Sstevel@tonic-gate 			fatalperror(STDERR_FILENO, "Error in socket");
2807c478bd9Sstevel@tonic-gate 		}
2817c478bd9Sstevel@tonic-gate 
2827c478bd9Sstevel@tonic-gate 		(void) memset((char *)&sin, 0, sizeof (sin));
2837c478bd9Sstevel@tonic-gate 		sin.sin_family = AF_INET;
2847c478bd9Sstevel@tonic-gate 		sin.sin_port = htons(debug_port);
2857c478bd9Sstevel@tonic-gate 		sin.sin_addr.s_addr = INADDR_ANY;
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate 		(void) setsockopt(s, SOL_SOCKET, SO_REUSEADDR,
2887c478bd9Sstevel@tonic-gate 					(char *)&on, sizeof (on));
2897c478bd9Sstevel@tonic-gate 
2907c478bd9Sstevel@tonic-gate 		if ((bind(s, (struct sockaddr *)&sin, sizeof (sin))) < 0) {
2917c478bd9Sstevel@tonic-gate 			fatalperror(STDERR_FILENO, "bind error");
2927c478bd9Sstevel@tonic-gate 		}
2937c478bd9Sstevel@tonic-gate 
2947c478bd9Sstevel@tonic-gate 		if ((listen(s, 5)) < 0) {
2957c478bd9Sstevel@tonic-gate 			fatalperror(STDERR_FILENO, "listen error");
2967c478bd9Sstevel@tonic-gate 		}
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate 		fromlen = sizeof (from);
2997c478bd9Sstevel@tonic-gate 		if ((fd = accept(s, (struct sockaddr *)&from, &fromlen)) < 0) {
3007c478bd9Sstevel@tonic-gate 			fatalperror(STDERR_FILENO, "accept error");
3017c478bd9Sstevel@tonic-gate 		}
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate 		(void) close(s);
3047c478bd9Sstevel@tonic-gate 	} else
3057c478bd9Sstevel@tonic-gate #endif /* DEBUG */
3067c478bd9Sstevel@tonic-gate 	{
3077c478bd9Sstevel@tonic-gate 		if (!issock(STDIN_FILENO))
3087c478bd9Sstevel@tonic-gate 			fatal(STDIN_FILENO,
3097c478bd9Sstevel@tonic-gate 				"stdin is not a socket file descriptor");
3107c478bd9Sstevel@tonic-gate 		fd = STDIN_FILENO;
3117c478bd9Sstevel@tonic-gate 	}
3127c478bd9Sstevel@tonic-gate 
3137c478bd9Sstevel@tonic-gate 	fromlen = sizeof (from);
3147c478bd9Sstevel@tonic-gate 	if (getpeername(fd, (struct sockaddr *)&from, &fromlen) < 0)
3157c478bd9Sstevel@tonic-gate 		fatalperror(STDERR_FILENO, "getpeername");
3167c478bd9Sstevel@tonic-gate 
3177c478bd9Sstevel@tonic-gate 	if (audit_rlogin_settid(fd))	/* set terminal ID */
3187c478bd9Sstevel@tonic-gate 		fatalperror(STDERR_FILENO, "audit");
3197c478bd9Sstevel@tonic-gate 
3207c478bd9Sstevel@tonic-gate 	if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, (char *)&on,
3217c478bd9Sstevel@tonic-gate 	    sizeof (on)) < 0)
3227c478bd9Sstevel@tonic-gate 		syslog(LOG_WARNING, "setsockopt(SO_KEEPALIVE): %m");
3237c478bd9Sstevel@tonic-gate 
3247c478bd9Sstevel@tonic-gate 	if (!VALID_CHKSUM(chksum_flag)) {
3257c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "Configuration error: mutually exclusive "
3267c478bd9Sstevel@tonic-gate 		    "options specified (-c and -i)");
3277c478bd9Sstevel@tonic-gate 		fatal(fd, "Checksums are required and ignored (-c and -i);"
3287c478bd9Sstevel@tonic-gate 		    "these options are mutually exclusive - check "
3297c478bd9Sstevel@tonic-gate 		    "the documentation.");
3307c478bd9Sstevel@tonic-gate 	}
3317c478bd9Sstevel@tonic-gate 	ourlen = sizeof (ouraddr);
3327c478bd9Sstevel@tonic-gate 	if (getsockname(fd, (struct sockaddr *)&ouraddr, &ourlen) == -1) {
3337c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "getsockname error: %m");
3347c478bd9Sstevel@tonic-gate 		exit(EXIT_FAILURE);
3357c478bd9Sstevel@tonic-gate 	}
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate 	if (tos != -1 &&
3387c478bd9Sstevel@tonic-gate 	    ouraddr.ss_family != AF_INET6 &&
3397c478bd9Sstevel@tonic-gate 	    setsockopt(fd, IPPROTO_IP, IP_TOS, (char *)&tos,
3407c478bd9Sstevel@tonic-gate 					sizeof (tos)) < 0 &&
3417c478bd9Sstevel@tonic-gate 					errno != ENOPROTOOPT) {
3427c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "setsockopt(IP_TOS %d): %m", tos);
3437c478bd9Sstevel@tonic-gate 	}
3447c478bd9Sstevel@tonic-gate 	doit(fd, &from, krb_context, encr_flag, keytab);
345740638c8Sbw 	return (0);
3467c478bd9Sstevel@tonic-gate }
3477c478bd9Sstevel@tonic-gate 
3487c478bd9Sstevel@tonic-gate static void	cleanup(int);
3497c478bd9Sstevel@tonic-gate static int	nsize = 0;	/* bytes read prior to pushing rlmod */
3507c478bd9Sstevel@tonic-gate static char	*rlbuf;		/* buffer where nbytes are read to */
3517c478bd9Sstevel@tonic-gate static char	*line;
3527c478bd9Sstevel@tonic-gate 
3537c478bd9Sstevel@tonic-gate static struct winsize win = { 0, 0, 0, 0 };
3547c478bd9Sstevel@tonic-gate static pid_t pid;
3557c478bd9Sstevel@tonic-gate static char hostname[MAXHOSTNAMELEN + 1];
3567c478bd9Sstevel@tonic-gate 
3577c478bd9Sstevel@tonic-gate static void
3587c478bd9Sstevel@tonic-gate getstr(int f, char *buf, int cnt, char *err)
3597c478bd9Sstevel@tonic-gate {
3607c478bd9Sstevel@tonic-gate 	char c;
3617c478bd9Sstevel@tonic-gate 	do {
3627c478bd9Sstevel@tonic-gate 		if (read(f, &c, 1) != 1 || (--cnt < 0)) {
3637c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, "Error reading \'%s\' field", err);
3647c478bd9Sstevel@tonic-gate 			exit(EXIT_FAILURE);
3657c478bd9Sstevel@tonic-gate 		}
3667c478bd9Sstevel@tonic-gate 		*buf++ = c;
3677c478bd9Sstevel@tonic-gate 	} while (c != '\0');
3687c478bd9Sstevel@tonic-gate }
3697c478bd9Sstevel@tonic-gate 
3707c478bd9Sstevel@tonic-gate static krb5_error_code
3717c478bd9Sstevel@tonic-gate recvauth(int f,
3727c478bd9Sstevel@tonic-gate 	krb5_context krb_context,
3737c478bd9Sstevel@tonic-gate 	unsigned int *valid_checksum,
3747c478bd9Sstevel@tonic-gate 	krb5_ticket **ticket,
3757c478bd9Sstevel@tonic-gate 	int *auth_type,
3767c478bd9Sstevel@tonic-gate 	krb5_principal *client,
3777c478bd9Sstevel@tonic-gate 	int encr_flag,
3787c478bd9Sstevel@tonic-gate 	krb5_keytab keytab)
3797c478bd9Sstevel@tonic-gate {
3807c478bd9Sstevel@tonic-gate 	krb5_error_code status = 0;
3817c478bd9Sstevel@tonic-gate 	krb5_auth_context auth_context = NULL;
3827c478bd9Sstevel@tonic-gate 	krb5_rcache rcache;
3837c478bd9Sstevel@tonic-gate 	krb5_authenticator *authenticator;
3847c478bd9Sstevel@tonic-gate 	krb5_data inbuf;
3857c478bd9Sstevel@tonic-gate 	krb5_data auth_version;
3867c478bd9Sstevel@tonic-gate 
3877c478bd9Sstevel@tonic-gate 	*valid_checksum = 0;
3887c478bd9Sstevel@tonic-gate 
3897c478bd9Sstevel@tonic-gate 	if ((status = krb5_auth_con_init(krb_context, &auth_context)))
3907c478bd9Sstevel@tonic-gate 		return (status);
3917c478bd9Sstevel@tonic-gate 
3927c478bd9Sstevel@tonic-gate 	/* Only need remote address for rd_cred() to verify client */
3937c478bd9Sstevel@tonic-gate 	if ((status = krb5_auth_con_genaddrs(krb_context, auth_context, f,
3947c478bd9Sstevel@tonic-gate 			KRB5_AUTH_CONTEXT_GENERATE_REMOTE_FULL_ADDR)))
3957c478bd9Sstevel@tonic-gate 		return (status);
3967c478bd9Sstevel@tonic-gate 
3977c478bd9Sstevel@tonic-gate 	status = krb5_auth_con_getrcache(krb_context, auth_context, &rcache);
3987c478bd9Sstevel@tonic-gate 	if (status)
3997c478bd9Sstevel@tonic-gate 		return (status);
4007c478bd9Sstevel@tonic-gate 
4017c478bd9Sstevel@tonic-gate 	if (!rcache) {
4027c478bd9Sstevel@tonic-gate 		krb5_principal server;
4037c478bd9Sstevel@tonic-gate 
4047c478bd9Sstevel@tonic-gate 		status = krb5_sname_to_principal(krb_context, 0, 0,
4057c478bd9Sstevel@tonic-gate 						KRB5_NT_SRV_HST, &server);
4067c478bd9Sstevel@tonic-gate 		if (status)
4077c478bd9Sstevel@tonic-gate 			return (status);
4087c478bd9Sstevel@tonic-gate 
4097c478bd9Sstevel@tonic-gate 		status = krb5_get_server_rcache(krb_context,
4107c478bd9Sstevel@tonic-gate 				krb5_princ_component(krb_context, server, 0),
4117c478bd9Sstevel@tonic-gate 				&rcache);
4127c478bd9Sstevel@tonic-gate 		krb5_free_principal(krb_context, server);
4137c478bd9Sstevel@tonic-gate 		if (status)
4147c478bd9Sstevel@tonic-gate 			return (status);
4157c478bd9Sstevel@tonic-gate 
4167c478bd9Sstevel@tonic-gate 		status = krb5_auth_con_setrcache(krb_context, auth_context,
4177c478bd9Sstevel@tonic-gate 						rcache);
4187c478bd9Sstevel@tonic-gate 		if (status)
4197c478bd9Sstevel@tonic-gate 			return (status);
4207c478bd9Sstevel@tonic-gate 	}
4217c478bd9Sstevel@tonic-gate 	if ((status = krb5_compat_recvauth(krb_context,
4227c478bd9Sstevel@tonic-gate 					&auth_context,
4237c478bd9Sstevel@tonic-gate 					&f,
4247c478bd9Sstevel@tonic-gate 					NULL,	/* Specify daemon principal */
4257c478bd9Sstevel@tonic-gate 					0,	/* no flags */
4267c478bd9Sstevel@tonic-gate 					keytab,	/* NULL to use v5srvtab */
4277c478bd9Sstevel@tonic-gate 					ticket,	/* return ticket */
4287c478bd9Sstevel@tonic-gate 					auth_type, /* authentication system */
4297c478bd9Sstevel@tonic-gate 					&auth_version))) {
4307c478bd9Sstevel@tonic-gate 		if (*auth_type == KRB5_RECVAUTH_V5) {
4317c478bd9Sstevel@tonic-gate 			/*
4327c478bd9Sstevel@tonic-gate 			 * clean up before exiting
4337c478bd9Sstevel@tonic-gate 			 */
4347c478bd9Sstevel@tonic-gate 			getstr(f, rusername, sizeof (rusername), "remuser");
4357c478bd9Sstevel@tonic-gate 			getstr(f, lusername, sizeof (lusername), "locuser");
4367c478bd9Sstevel@tonic-gate 			getstr(f, term, sizeof (term), "Terminal type");
4377c478bd9Sstevel@tonic-gate 		}
4387c478bd9Sstevel@tonic-gate 		return (status);
4397c478bd9Sstevel@tonic-gate 	}
4407c478bd9Sstevel@tonic-gate 
4417c478bd9Sstevel@tonic-gate 	getstr(f, lusername, sizeof (lusername), "locuser");
4427c478bd9Sstevel@tonic-gate 	getstr(f, term, sizeof (term), "Terminal type");
4437c478bd9Sstevel@tonic-gate 
4447c478bd9Sstevel@tonic-gate 	kcmd_protocol = KCMD_UNKNOWN_PROTOCOL;
4457c478bd9Sstevel@tonic-gate 	if (auth_version.length != 9 || auth_version.data == NULL) {
4467c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "Bad application protocol version length in "
4477c478bd9Sstevel@tonic-gate 		    "KRB5 exchange, exiting");
4487c478bd9Sstevel@tonic-gate 		fatal(f, "Bad application version length, exiting.");
4497c478bd9Sstevel@tonic-gate 	}
4507c478bd9Sstevel@tonic-gate 	/*
4517c478bd9Sstevel@tonic-gate 	 * Determine which Kerberos CMD protocol was used.
4527c478bd9Sstevel@tonic-gate 	 */
4537c478bd9Sstevel@tonic-gate 	if (strncmp(auth_version.data, "KCMDV0.1", 9) == 0) {
4547c478bd9Sstevel@tonic-gate 		kcmd_protocol = KCMD_OLD_PROTOCOL;
4557c478bd9Sstevel@tonic-gate 	} else if (strncmp(auth_version.data, "KCMDV0.2", 9) == 0) {
4567c478bd9Sstevel@tonic-gate 		kcmd_protocol = KCMD_NEW_PROTOCOL;
4577c478bd9Sstevel@tonic-gate 	} else {
4587c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "Unrecognized KCMD protocol (%s), exiting",
4597c478bd9Sstevel@tonic-gate 			(char *)auth_version.data);
4607c478bd9Sstevel@tonic-gate 		fatal(f, "Unrecognized KCMD protocol, exiting");
4617c478bd9Sstevel@tonic-gate 	}
4627c478bd9Sstevel@tonic-gate 
4637c478bd9Sstevel@tonic-gate 	if ((*auth_type == KRB5_RECVAUTH_V5) && chksum_flag &&
4647c478bd9Sstevel@tonic-gate 		kcmd_protocol == KCMD_OLD_PROTOCOL) {
4657c478bd9Sstevel@tonic-gate 		if ((status = krb5_auth_con_getauthenticator(krb_context,
4667c478bd9Sstevel@tonic-gate 							    auth_context,
4677c478bd9Sstevel@tonic-gate 							    &authenticator)))
4687c478bd9Sstevel@tonic-gate 			return (status);
4697c478bd9Sstevel@tonic-gate 		if (authenticator->checksum) {
4707c478bd9Sstevel@tonic-gate 			struct sockaddr_storage adr;
4717c478bd9Sstevel@tonic-gate 			int adr_length = sizeof (adr);
4727c478bd9Sstevel@tonic-gate 			int buflen;
4737c478bd9Sstevel@tonic-gate 			krb5_data input;
4747c478bd9Sstevel@tonic-gate 			krb5_keyblock key;
4757c478bd9Sstevel@tonic-gate 			char *chksumbuf;
4767c478bd9Sstevel@tonic-gate 
4777c478bd9Sstevel@tonic-gate 			/*
4787c478bd9Sstevel@tonic-gate 			 * Define the lenght of the chksum buffer.
4797c478bd9Sstevel@tonic-gate 			 * chksum string = "[portnum]:termstr:username"
4807c478bd9Sstevel@tonic-gate 			 * The extra 32 is to hold a integer string for
4817c478bd9Sstevel@tonic-gate 			 * the portnumber.
4827c478bd9Sstevel@tonic-gate 			 */
4837c478bd9Sstevel@tonic-gate 			buflen = strlen(term) + strlen(lusername) + 32;
4847c478bd9Sstevel@tonic-gate 			chksumbuf = (char *)malloc(buflen);
4857c478bd9Sstevel@tonic-gate 			if (chksumbuf == 0) {
4867c478bd9Sstevel@tonic-gate 				krb5_free_authenticator(krb_context,
4877c478bd9Sstevel@tonic-gate 							authenticator);
4887c478bd9Sstevel@tonic-gate 				fatal(f, "Out of memory error");
4897c478bd9Sstevel@tonic-gate 			}
4907c478bd9Sstevel@tonic-gate 
4917c478bd9Sstevel@tonic-gate 			if (getsockname(f, (struct sockaddr *)&adr,
4927c478bd9Sstevel@tonic-gate 							&adr_length) != 0) {
4937c478bd9Sstevel@tonic-gate 				krb5_free_authenticator(krb_context,
4947c478bd9Sstevel@tonic-gate 							authenticator);
4957c478bd9Sstevel@tonic-gate 				fatal(f, "getsockname error");
4967c478bd9Sstevel@tonic-gate 			}
4977c478bd9Sstevel@tonic-gate 
4987c478bd9Sstevel@tonic-gate 			(void) snprintf(chksumbuf, buflen,
4997c478bd9Sstevel@tonic-gate 					"%u:%s%s",
5007c478bd9Sstevel@tonic-gate 					ntohs(SOCK_PORT(adr)),
5017c478bd9Sstevel@tonic-gate 					term, lusername);
5027c478bd9Sstevel@tonic-gate 
5037c478bd9Sstevel@tonic-gate 			input.data = chksumbuf;
5047c478bd9Sstevel@tonic-gate 			input.length = strlen(chksumbuf);
5057c478bd9Sstevel@tonic-gate 			key.contents = (*ticket)->enc_part2->session->contents;
5067c478bd9Sstevel@tonic-gate 			key.length = (*ticket)->enc_part2->session->length;
5077c478bd9Sstevel@tonic-gate 			status = krb5_c_verify_checksum(krb_context,
5087c478bd9Sstevel@tonic-gate 						&key, 0,
5097c478bd9Sstevel@tonic-gate 						&input,
5107c478bd9Sstevel@tonic-gate 						authenticator->checksum,
5117c478bd9Sstevel@tonic-gate 						valid_checksum);
5127c478bd9Sstevel@tonic-gate 
5137c478bd9Sstevel@tonic-gate 			if (status == 0 && *valid_checksum == 0)
5147c478bd9Sstevel@tonic-gate 				status = KRB5KRB_AP_ERR_BAD_INTEGRITY;
5157c478bd9Sstevel@tonic-gate 
5167c478bd9Sstevel@tonic-gate 			if (chksumbuf)
5177c478bd9Sstevel@tonic-gate 				krb5_xfree(chksumbuf);
5187c478bd9Sstevel@tonic-gate 			if (status) {
5197c478bd9Sstevel@tonic-gate 				krb5_free_authenticator(krb_context,
5207c478bd9Sstevel@tonic-gate 							authenticator);
5217c478bd9Sstevel@tonic-gate 				return (status);
5227c478bd9Sstevel@tonic-gate 			}
5237c478bd9Sstevel@tonic-gate 		}
5247c478bd9Sstevel@tonic-gate 		krb5_free_authenticator(krb_context, authenticator);
5257c478bd9Sstevel@tonic-gate 	}
5267c478bd9Sstevel@tonic-gate 
5277c478bd9Sstevel@tonic-gate 	if ((status = krb5_copy_principal(krb_context,
5287c478bd9Sstevel@tonic-gate 					(*ticket)->enc_part2->client,
5297c478bd9Sstevel@tonic-gate 					client)))
5307c478bd9Sstevel@tonic-gate 		return (status);
5317c478bd9Sstevel@tonic-gate 
5327c478bd9Sstevel@tonic-gate 	/* Get the Unix username of the remote user */
5337c478bd9Sstevel@tonic-gate 	getstr(f, rusername, sizeof (rusername), "remuser");
5347c478bd9Sstevel@tonic-gate 
5357c478bd9Sstevel@tonic-gate 	/* Get the Kerberos principal name string of the remote user */
5367c478bd9Sstevel@tonic-gate 	if ((status = krb5_unparse_name(krb_context, *client, &krusername)))
5377c478bd9Sstevel@tonic-gate 		return (status);
5387c478bd9Sstevel@tonic-gate 
5397c478bd9Sstevel@tonic-gate #ifdef DEBUG
5407c478bd9Sstevel@tonic-gate 	syslog(LOG_DEBUG | LOG_AUTH, "rlogind: got krb5 credentials for %s",
5417c478bd9Sstevel@tonic-gate 	    (krusername != NULL ? krusername : "<unknown>"));
5427c478bd9Sstevel@tonic-gate #endif
5437c478bd9Sstevel@tonic-gate 
5447c478bd9Sstevel@tonic-gate 	if (encr_flag) {
5457c478bd9Sstevel@tonic-gate 		status = krb5_auth_con_getremotesubkey(krb_context,
5467c478bd9Sstevel@tonic-gate 						    auth_context,
5477c478bd9Sstevel@tonic-gate 						    &session_key);
5487c478bd9Sstevel@tonic-gate 		if (status) {
5497c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, "Error getting KRB5 session "
5507c478bd9Sstevel@tonic-gate 			    "subkey, exiting");
5517c478bd9Sstevel@tonic-gate 			fatal(f, "Error getting KRB5 session subkey, exiting");
5527c478bd9Sstevel@tonic-gate 		}
5537c478bd9Sstevel@tonic-gate 		/*
5547c478bd9Sstevel@tonic-gate 		 * The "new" protocol requires that a subkey be sent.
5557c478bd9Sstevel@tonic-gate 		 */
5567c478bd9Sstevel@tonic-gate 		if (session_key == NULL &&
5577c478bd9Sstevel@tonic-gate 		    kcmd_protocol == KCMD_NEW_PROTOCOL) {
5587c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, "No KRB5 session subkey sent, exiting");
5597c478bd9Sstevel@tonic-gate 			fatal(f, "No KRB5 session subkey sent, exiting");
5607c478bd9Sstevel@tonic-gate 		}
5617c478bd9Sstevel@tonic-gate 		/*
5627c478bd9Sstevel@tonic-gate 		 * The "old" protocol does not permit an authenticator subkey.
5637c478bd9Sstevel@tonic-gate 		 * The key is taken from the ticket instead (see below).
5647c478bd9Sstevel@tonic-gate 		 */
5657c478bd9Sstevel@tonic-gate 		if (session_key != NULL &&
5667c478bd9Sstevel@tonic-gate 		    kcmd_protocol == KCMD_OLD_PROTOCOL) {
5677c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, "KRB5 session subkey not permitted "
5687c478bd9Sstevel@tonic-gate 			    "with old KCMD protocol, exiting");
5697c478bd9Sstevel@tonic-gate 
5707c478bd9Sstevel@tonic-gate 			fatal(f, "KRB5 session subkey not permitted "
5717c478bd9Sstevel@tonic-gate 			    "with old KCMD protocol, exiting");
5727c478bd9Sstevel@tonic-gate 		}
5737c478bd9Sstevel@tonic-gate 		/*
5747c478bd9Sstevel@tonic-gate 		 * If no key at this point, use the session key from
5757c478bd9Sstevel@tonic-gate 		 * the ticket.
5767c478bd9Sstevel@tonic-gate 		 */
5777c478bd9Sstevel@tonic-gate 		if (session_key == NULL) {
5787c478bd9Sstevel@tonic-gate 			/*
5797c478bd9Sstevel@tonic-gate 			 * Save the session key so we can configure the crypto
5807c478bd9Sstevel@tonic-gate 			 * module later.
5817c478bd9Sstevel@tonic-gate 			 */
5827c478bd9Sstevel@tonic-gate 			status = krb5_copy_keyblock(krb_context,
5837c478bd9Sstevel@tonic-gate 					    (*ticket)->enc_part2->session,
5847c478bd9Sstevel@tonic-gate 					    &session_key);
5857c478bd9Sstevel@tonic-gate 			if (status) {
5867c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR, "krb5_copy_keyblock failed");
5877c478bd9Sstevel@tonic-gate 				fatal(f, "krb5_copy_keyblock failed");
5887c478bd9Sstevel@tonic-gate 			}
5897c478bd9Sstevel@tonic-gate 		}
5907c478bd9Sstevel@tonic-gate 		/*
5917c478bd9Sstevel@tonic-gate 		 * If session key still cannot be found, we must
5927c478bd9Sstevel@tonic-gate 		 * exit because encryption is required here
5937c478bd9Sstevel@tonic-gate 		 * when encr_flag (-x) is set.
5947c478bd9Sstevel@tonic-gate 		 */
5957c478bd9Sstevel@tonic-gate 		if (session_key == NULL) {
5967c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, "Could not find an encryption key,"
5977c478bd9Sstevel@tonic-gate 				    "exiting");
5987c478bd9Sstevel@tonic-gate 			fatal(f, "Encryption required but key not found, "
5997c478bd9Sstevel@tonic-gate 			    "exiting");
6007c478bd9Sstevel@tonic-gate 		}
6017c478bd9Sstevel@tonic-gate 	}
6027c478bd9Sstevel@tonic-gate 	/*
6037c478bd9Sstevel@tonic-gate 	 * Use krb5_read_message to read the principal stuff.
6047c478bd9Sstevel@tonic-gate 	 */
6057c478bd9Sstevel@tonic-gate 	if ((status = krb5_read_message(krb_context, (krb5_pointer)&f,
6067c478bd9Sstevel@tonic-gate 					&inbuf)))
6077c478bd9Sstevel@tonic-gate 		fatal(f, "Error reading krb5 message");
6087c478bd9Sstevel@tonic-gate 
609*32885d59Sgtb 	if (inbuf.length) { /* Forwarding being done, read creds */
610*32885d59Sgtb 		krb5_creds **creds = NULL;
611*32885d59Sgtb 
612*32885d59Sgtb 		if (status = krb5_rd_cred(krb_context, auth_context, &inbuf,
613*32885d59Sgtb 					    &creds, NULL)) {
6147c478bd9Sstevel@tonic-gate 			if (rcache)
6157c478bd9Sstevel@tonic-gate 				(void) krb5_rc_close(krb_context, rcache);
616*32885d59Sgtb 			krb5_free_creds(krb_context, *creds);
6177c478bd9Sstevel@tonic-gate 			fatal(f, "Can't get forwarded credentials");
6187c478bd9Sstevel@tonic-gate 		}
619*32885d59Sgtb 
620*32885d59Sgtb 		/* Store the forwarded creds in the ccache */
621*32885d59Sgtb 		if (status = store_forw_creds(krb_context,
622*32885d59Sgtb 					    creds, *ticket, lusername,
623*32885d59Sgtb 					    &ccache)) {
624*32885d59Sgtb 			if (rcache)
625*32885d59Sgtb 				(void) krb5_rc_close(krb_context, rcache);
626*32885d59Sgtb 			krb5_free_creds(krb_context, *creds);
627*32885d59Sgtb 			fatal(f, "Can't store forwarded credentials");
628*32885d59Sgtb 		}
629*32885d59Sgtb 		krb5_free_creds(krb_context, *creds);
630*32885d59Sgtb 	}
631*32885d59Sgtb 
6327c478bd9Sstevel@tonic-gate 	if (rcache)
6337c478bd9Sstevel@tonic-gate 		(void) krb5_rc_close(krb_context, rcache);
6347c478bd9Sstevel@tonic-gate 
6357c478bd9Sstevel@tonic-gate 	return (status);
6367c478bd9Sstevel@tonic-gate }
6377c478bd9Sstevel@tonic-gate 
6387c478bd9Sstevel@tonic-gate static void
6397c478bd9Sstevel@tonic-gate do_krb_login(int f, char *host_addr, char *hostname,
6407c478bd9Sstevel@tonic-gate 	    krb5_context krb_context, int encr_flag,
6417c478bd9Sstevel@tonic-gate 	    krb5_keytab keytab)
6427c478bd9Sstevel@tonic-gate {
6437c478bd9Sstevel@tonic-gate 	krb5_error_code status;
6447c478bd9Sstevel@tonic-gate 	uint_t valid_checksum;
6457c478bd9Sstevel@tonic-gate 	krb5_ticket	*ticket = NULL;
6467c478bd9Sstevel@tonic-gate 	int auth_sys = 0;
6477c478bd9Sstevel@tonic-gate 	int auth_sent = 0;
6487c478bd9Sstevel@tonic-gate 	krb5_principal client = NULL;
6497c478bd9Sstevel@tonic-gate 
6507c478bd9Sstevel@tonic-gate 	if (getuid())
6517c478bd9Sstevel@tonic-gate 		fatal(f, "Error authorizing KRB5 connection, "
6527c478bd9Sstevel@tonic-gate 			"server lacks privilege");
6537c478bd9Sstevel@tonic-gate 
6547c478bd9Sstevel@tonic-gate 	status = recvauth(f, krb_context, &valid_checksum, &ticket,
6557c478bd9Sstevel@tonic-gate 			&auth_sys, &client, encr_flag, keytab);
6567c478bd9Sstevel@tonic-gate 	if (status) {
6577c478bd9Sstevel@tonic-gate 		if (ticket)
6587c478bd9Sstevel@tonic-gate 			krb5_free_ticket(krb_context, ticket);
6597c478bd9Sstevel@tonic-gate 		if (status != 255)
6607c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR,
6617c478bd9Sstevel@tonic-gate 			    "Authentication failed from %s(%s): %s\n",
6627c478bd9Sstevel@tonic-gate 			    host_addr, hostname, error_message(status));
6637c478bd9Sstevel@tonic-gate 		fatal(f, "Kerberos authentication failed, exiting");
6647c478bd9Sstevel@tonic-gate 	}
6657c478bd9Sstevel@tonic-gate 
6667c478bd9Sstevel@tonic-gate 	if (auth_sys != KRB5_RECVAUTH_V5) {
6677c478bd9Sstevel@tonic-gate 		fatal(f, "This server only supports Kerberos V5");
6687c478bd9Sstevel@tonic-gate 	} else {
6697c478bd9Sstevel@tonic-gate 		/*
6707c478bd9Sstevel@tonic-gate 		 * Authenticated OK, now check authorization.
6717c478bd9Sstevel@tonic-gate 		 */
6727c478bd9Sstevel@tonic-gate 		if (client && krb5_kuserok(krb_context, client, lusername))
6737c478bd9Sstevel@tonic-gate 		    auth_sent = KRB5_RECVAUTH_V5;
6747c478bd9Sstevel@tonic-gate 	}
6757c478bd9Sstevel@tonic-gate 
6767c478bd9Sstevel@tonic-gate 	if (auth_sent == KRB5_RECVAUTH_V5 &&
6777c478bd9Sstevel@tonic-gate 	    kcmd_protocol == KCMD_OLD_PROTOCOL &&
6787c478bd9Sstevel@tonic-gate 	    chksum_flag == CHKSUM_REQUIRED && !valid_checksum) {
6797c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "Client did not supply required checksum, "
6807c478bd9Sstevel@tonic-gate 		    "connection rejected.");
6817c478bd9Sstevel@tonic-gate 		fatal(f, "Client did not supply required checksum, "
6827c478bd9Sstevel@tonic-gate 		    "connection rejected.");
6837c478bd9Sstevel@tonic-gate 	}
6847c478bd9Sstevel@tonic-gate 
6857c478bd9Sstevel@tonic-gate 	if (auth_sys != auth_sent) {
6867c478bd9Sstevel@tonic-gate 		char *msg_fail = NULL;
6877c478bd9Sstevel@tonic-gate 		int msgsize = 0;
6887c478bd9Sstevel@tonic-gate 
6897c478bd9Sstevel@tonic-gate 		if (ticket)
6907c478bd9Sstevel@tonic-gate 			krb5_free_ticket(krb_context, ticket);
6917c478bd9Sstevel@tonic-gate 
6927c478bd9Sstevel@tonic-gate 		if (krusername != NULL) {
6937c478bd9Sstevel@tonic-gate 			/*
6947c478bd9Sstevel@tonic-gate 			 * msgsize must be enough to hold
6957c478bd9Sstevel@tonic-gate 			 * krusername, lusername and a brief
6967c478bd9Sstevel@tonic-gate 			 * message describing the failure.
6977c478bd9Sstevel@tonic-gate 			 */
6987c478bd9Sstevel@tonic-gate 			msgsize = strlen(krusername) +
6997c478bd9Sstevel@tonic-gate 				strlen(lusername) + 80;
7007c478bd9Sstevel@tonic-gate 			msg_fail = (char *)malloc(msgsize);
7017c478bd9Sstevel@tonic-gate 		}
7027c478bd9Sstevel@tonic-gate 		if (msg_fail == NULL) {
7037c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, "User is not authorized to login to "
7047c478bd9Sstevel@tonic-gate 			    "specified account");
7057c478bd9Sstevel@tonic-gate 
7067c478bd9Sstevel@tonic-gate 			fatal(f, "User is not authorized to login to "
7077c478bd9Sstevel@tonic-gate 			    "specified account");
7087c478bd9Sstevel@tonic-gate 		}
7097c478bd9Sstevel@tonic-gate 		if (auth_sent != 0)
7107c478bd9Sstevel@tonic-gate 			(void) snprintf(msg_fail, msgsize,
7117c478bd9Sstevel@tonic-gate 					"Access denied because of improper "
7127c478bd9Sstevel@tonic-gate 					"KRB5 credentials");
7137c478bd9Sstevel@tonic-gate 		else
7147c478bd9Sstevel@tonic-gate 			(void) snprintf(msg_fail, msgsize,
7157c478bd9Sstevel@tonic-gate 					"User %s is not authorized to login "
7167c478bd9Sstevel@tonic-gate 					"to account %s",
7177c478bd9Sstevel@tonic-gate 					krusername, lusername);
7187c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "%s", msg_fail);
7197c478bd9Sstevel@tonic-gate 		fatal(f, msg_fail);
7207c478bd9Sstevel@tonic-gate 	}
7217c478bd9Sstevel@tonic-gate }
7227c478bd9Sstevel@tonic-gate 
7237c478bd9Sstevel@tonic-gate /*
7247c478bd9Sstevel@tonic-gate  * stop_stream
7257c478bd9Sstevel@tonic-gate  *
7267c478bd9Sstevel@tonic-gate  * Utility routine to send a CRYPTIOCSTOP ioctl to the
7277c478bd9Sstevel@tonic-gate  * crypto module(cryptmod).
7287c478bd9Sstevel@tonic-gate  */
7297c478bd9Sstevel@tonic-gate static void
7307c478bd9Sstevel@tonic-gate stop_stream(int fd, int dir)
7317c478bd9Sstevel@tonic-gate {
7327c478bd9Sstevel@tonic-gate 	struct strioctl  crioc;
7337c478bd9Sstevel@tonic-gate 	uint32_t stopdir = dir;
7347c478bd9Sstevel@tonic-gate 
7357c478bd9Sstevel@tonic-gate 	crioc.ic_cmd = CRYPTIOCSTOP;
7367c478bd9Sstevel@tonic-gate 	crioc.ic_timout = -1;
7377c478bd9Sstevel@tonic-gate 	crioc.ic_len = sizeof (stopdir);
7387c478bd9Sstevel@tonic-gate 	crioc.ic_dp = (char *)&stopdir;
7397c478bd9Sstevel@tonic-gate 
7407c478bd9Sstevel@tonic-gate 	if (ioctl(fd, I_STR, &crioc))
7417c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "Error sending CRYPTIOCSTOP ioctl: %m");
7427c478bd9Sstevel@tonic-gate }
7437c478bd9Sstevel@tonic-gate 
7447c478bd9Sstevel@tonic-gate /*
7457c478bd9Sstevel@tonic-gate  * start_stream
7467c478bd9Sstevel@tonic-gate  *
7477c478bd9Sstevel@tonic-gate  * Utility routine to send a CRYPTIOCSTART ioctl to the
7487c478bd9Sstevel@tonic-gate  * crypto module(cryptmod).  This routine may contain optional
7497c478bd9Sstevel@tonic-gate  * payload data that the cryptmod will interpret as bytes that
7507c478bd9Sstevel@tonic-gate  * need to be decrypted and sent back up to the application
7517c478bd9Sstevel@tonic-gate  * via the data stream.
7527c478bd9Sstevel@tonic-gate  */
7537c478bd9Sstevel@tonic-gate static void
7547c478bd9Sstevel@tonic-gate start_stream(int fd, int dir)
7557c478bd9Sstevel@tonic-gate {
7567c478bd9Sstevel@tonic-gate 	struct strioctl crioc;
7577c478bd9Sstevel@tonic-gate 	uint32_t iocval;
7587c478bd9Sstevel@tonic-gate 	size_t datalen = 0;
7597c478bd9Sstevel@tonic-gate 	char *data = NULL;
7607c478bd9Sstevel@tonic-gate 
7617c478bd9Sstevel@tonic-gate 	if (dir == CRYPT_DECRYPT) {
7627c478bd9Sstevel@tonic-gate 		iocval = CRYPTIOCSTARTDEC;
7637c478bd9Sstevel@tonic-gate 
7647c478bd9Sstevel@tonic-gate 		/* Look for data not yet processed */
7657c478bd9Sstevel@tonic-gate 		if (ioctl(fd, I_NREAD, &datalen) < 0) {
7667c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, "I_NREAD returned error %m");
7677c478bd9Sstevel@tonic-gate 			datalen = 0;
7687c478bd9Sstevel@tonic-gate 		} else {
7697c478bd9Sstevel@tonic-gate 			if (datalen > 0) {
7707c478bd9Sstevel@tonic-gate 				data = malloc(datalen);
7717c478bd9Sstevel@tonic-gate 				if (data != NULL) {
7727c478bd9Sstevel@tonic-gate 					int nbytes = read(fd, data, datalen);
7737c478bd9Sstevel@tonic-gate 					datalen = nbytes;
7747c478bd9Sstevel@tonic-gate 				} else {
7757c478bd9Sstevel@tonic-gate 					syslog(LOG_ERR,
7767c478bd9Sstevel@tonic-gate 						"malloc error (%d bytes)",
7777c478bd9Sstevel@tonic-gate 						datalen);
7787c478bd9Sstevel@tonic-gate 					datalen = 0;
7797c478bd9Sstevel@tonic-gate 				}
7807c478bd9Sstevel@tonic-gate 			} else {
7817c478bd9Sstevel@tonic-gate 				datalen = 0;
7827c478bd9Sstevel@tonic-gate 			}
7837c478bd9Sstevel@tonic-gate 		}
7847c478bd9Sstevel@tonic-gate 	} else {
7857c478bd9Sstevel@tonic-gate 		iocval = CRYPTIOCSTARTENC;
7867c478bd9Sstevel@tonic-gate 	}
7877c478bd9Sstevel@tonic-gate 
7887c478bd9Sstevel@tonic-gate 	crioc.ic_cmd = iocval;
7897c478bd9Sstevel@tonic-gate 	crioc.ic_timout = -1;
7907c478bd9Sstevel@tonic-gate 	crioc.ic_len = datalen;
7917c478bd9Sstevel@tonic-gate 	crioc.ic_dp = data;
7927c478bd9Sstevel@tonic-gate 
7937c478bd9Sstevel@tonic-gate 	if (ioctl(fd, I_STR, &crioc))
7947c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "Error sending CRYPTIOCSTART ioctl: %m");
7957c478bd9Sstevel@tonic-gate 
7967c478bd9Sstevel@tonic-gate 	if (data != NULL)
7977c478bd9Sstevel@tonic-gate 		free(data);
7987c478bd9Sstevel@tonic-gate }
7997c478bd9Sstevel@tonic-gate 
8007c478bd9Sstevel@tonic-gate static int
8017c478bd9Sstevel@tonic-gate configure_stream(int fd, krb5_keyblock *skey, int dir, krb5_data *ivec,
8027c478bd9Sstevel@tonic-gate 		uint_t iv_usage)
8037c478bd9Sstevel@tonic-gate {
8047c478bd9Sstevel@tonic-gate 	struct cr_info_t setup_info;
8057c478bd9Sstevel@tonic-gate 	struct strioctl crioc;
8067c478bd9Sstevel@tonic-gate 	int retval = 0;
8077c478bd9Sstevel@tonic-gate 
8087c478bd9Sstevel@tonic-gate 	switch (skey->enctype) {
8097c478bd9Sstevel@tonic-gate 	case ENCTYPE_DES_CBC_CRC:
8107c478bd9Sstevel@tonic-gate 		setup_info.crypto_method = CRYPT_METHOD_DES_CBC_CRC;
8117c478bd9Sstevel@tonic-gate 		break;
8127c478bd9Sstevel@tonic-gate 	case ENCTYPE_DES_CBC_MD5:
8137c478bd9Sstevel@tonic-gate 		setup_info.crypto_method = CRYPT_METHOD_DES_CBC_MD5;
8147c478bd9Sstevel@tonic-gate 		break;
8157c478bd9Sstevel@tonic-gate 	case ENCTYPE_DES_CBC_RAW:
8167c478bd9Sstevel@tonic-gate 		setup_info.crypto_method = CRYPT_METHOD_DES_CBC_NULL;
8177c478bd9Sstevel@tonic-gate 		break;
8187c478bd9Sstevel@tonic-gate 	case ENCTYPE_DES3_CBC_SHA1:
8197c478bd9Sstevel@tonic-gate 		setup_info.crypto_method = CRYPT_METHOD_DES3_CBC_SHA1;
8207c478bd9Sstevel@tonic-gate 		break;
8217c478bd9Sstevel@tonic-gate 	case ENCTYPE_ARCFOUR_HMAC:
8227c478bd9Sstevel@tonic-gate 		setup_info.crypto_method = CRYPT_METHOD_ARCFOUR_HMAC_MD5;
8237c478bd9Sstevel@tonic-gate 		break;
8247c478bd9Sstevel@tonic-gate 	case ENCTYPE_ARCFOUR_HMAC_EXP:
8257c478bd9Sstevel@tonic-gate 		setup_info.crypto_method = CRYPT_METHOD_ARCFOUR_HMAC_MD5_EXP;
8267c478bd9Sstevel@tonic-gate 		break;
8277c478bd9Sstevel@tonic-gate 	case ENCTYPE_AES128_CTS_HMAC_SHA1_96:
8287c478bd9Sstevel@tonic-gate 		setup_info.crypto_method = CRYPT_METHOD_AES128;
8297c478bd9Sstevel@tonic-gate 		break;
8307c478bd9Sstevel@tonic-gate 	case ENCTYPE_AES256_CTS_HMAC_SHA1_96:
8317c478bd9Sstevel@tonic-gate 		setup_info.crypto_method = CRYPT_METHOD_AES256;
8327c478bd9Sstevel@tonic-gate 		break;
8337c478bd9Sstevel@tonic-gate 	default:
8347c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "Enctype in kerberos session key "
8357c478bd9Sstevel@tonic-gate 		    "is not supported by crypto module(%d)",
8367c478bd9Sstevel@tonic-gate 		    skey->enctype);
8377c478bd9Sstevel@tonic-gate 		return (-1);
8387c478bd9Sstevel@tonic-gate 	}
8397c478bd9Sstevel@tonic-gate 	if (ivec == NULL || ivec->length == 0) {
8407c478bd9Sstevel@tonic-gate 		(void) memset(&setup_info.ivec, 0, sizeof (setup_info.ivec));
8417c478bd9Sstevel@tonic-gate 
8427c478bd9Sstevel@tonic-gate 		if (skey->enctype != ENCTYPE_ARCFOUR_HMAC &&
8437c478bd9Sstevel@tonic-gate 		    skey->enctype != ENCTYPE_ARCFOUR_HMAC_EXP)
8447c478bd9Sstevel@tonic-gate 			/* Kerberos IVs are 8 bytes long for DES keys */
8457c478bd9Sstevel@tonic-gate 			setup_info.iveclen = KRB5_MIT_DES_KEYSIZE;
8467c478bd9Sstevel@tonic-gate 		else
8477c478bd9Sstevel@tonic-gate 			setup_info.iveclen = 0;
8487c478bd9Sstevel@tonic-gate 	} else {
8497c478bd9Sstevel@tonic-gate 		(void) memcpy(&setup_info.ivec, ivec->data, ivec->length);
8507c478bd9Sstevel@tonic-gate 		setup_info.iveclen = ivec->length;
8517c478bd9Sstevel@tonic-gate 	}
8527c478bd9Sstevel@tonic-gate 
8537c478bd9Sstevel@tonic-gate 	setup_info.ivec_usage = iv_usage;
8547c478bd9Sstevel@tonic-gate 	(void) memcpy(&setup_info.key, skey->contents, skey->length);
8557c478bd9Sstevel@tonic-gate 
8567c478bd9Sstevel@tonic-gate 	setup_info.keylen = skey->length;
8577c478bd9Sstevel@tonic-gate 	setup_info.direction_mask = dir;
8587c478bd9Sstevel@tonic-gate 	/*
8597c478bd9Sstevel@tonic-gate 	 * R* commands get special handling by crypto module -
8607c478bd9Sstevel@tonic-gate 	 * 4 byte length field is used before each encrypted block
8617c478bd9Sstevel@tonic-gate 	 * of data.
8627c478bd9Sstevel@tonic-gate 	 */
8637c478bd9Sstevel@tonic-gate 	setup_info.option_mask = (kcmd_protocol == KCMD_OLD_PROTOCOL ?
8647c478bd9Sstevel@tonic-gate 				CRYPTOPT_RCMD_MODE_V1 :
8657c478bd9Sstevel@tonic-gate 				CRYPTOPT_RCMD_MODE_V2);
8667c478bd9Sstevel@tonic-gate 
8677c478bd9Sstevel@tonic-gate 	crioc.ic_cmd = CRYPTIOCSETUP;
8687c478bd9Sstevel@tonic-gate 	crioc.ic_timout = -1;
8697c478bd9Sstevel@tonic-gate 	crioc.ic_len = sizeof (setup_info);
8707c478bd9Sstevel@tonic-gate 	crioc.ic_dp = (char *)&setup_info;
8717c478bd9Sstevel@tonic-gate 
8727c478bd9Sstevel@tonic-gate 	if (ioctl(fd, I_STR, &crioc)) {
8737c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "Error sending CRYPTIOCSETUP ioctl: %m");
8747c478bd9Sstevel@tonic-gate 		retval = -1;
8757c478bd9Sstevel@tonic-gate 	}
8767c478bd9Sstevel@tonic-gate 	return (retval);
8777c478bd9Sstevel@tonic-gate }
8787c478bd9Sstevel@tonic-gate 
8797c478bd9Sstevel@tonic-gate static krb5_error_code
8807c478bd9Sstevel@tonic-gate krb5_compat_recvauth(krb5_context context,
8817c478bd9Sstevel@tonic-gate 		    krb5_auth_context *auth_context,
8827c478bd9Sstevel@tonic-gate 		    krb5_pointer fdp,	/* IN */
8837c478bd9Sstevel@tonic-gate 		    krb5_principal server,	/* IN */
8847c478bd9Sstevel@tonic-gate 		    krb5_int32 flags,	/* IN */
8857c478bd9Sstevel@tonic-gate 		    krb5_keytab keytab,	/* IN */
8867c478bd9Sstevel@tonic-gate 		    krb5_ticket **ticket, /* OUT */
8877c478bd9Sstevel@tonic-gate 		    krb5_int32 *auth_sys, /* OUT */
8887c478bd9Sstevel@tonic-gate 		    krb5_data *version)   /* OUT */
8897c478bd9Sstevel@tonic-gate {
8907c478bd9Sstevel@tonic-gate 	krb5_int32 vlen;
8917c478bd9Sstevel@tonic-gate 	char	*buf;
8927c478bd9Sstevel@tonic-gate 	int	len, length;
8937c478bd9Sstevel@tonic-gate 	krb5_int32	retval;
8947c478bd9Sstevel@tonic-gate 	int		fd = *((int *)fdp);
8957c478bd9Sstevel@tonic-gate 
8967c478bd9Sstevel@tonic-gate 	if ((retval = krb5_net_read(context, fd, (char *)&vlen, 4)) != 4)
8977c478bd9Sstevel@tonic-gate 		return ((retval < 0) ? errno : ECONNABORTED);
8987c478bd9Sstevel@tonic-gate 
8997c478bd9Sstevel@tonic-gate 	/*
9007c478bd9Sstevel@tonic-gate 	 * Assume that we're talking to a V5 recvauth; read in the
9017c478bd9Sstevel@tonic-gate 	 * the version string, and make sure it matches.
9027c478bd9Sstevel@tonic-gate 	 */
9037c478bd9Sstevel@tonic-gate 	len = (int)ntohl(vlen);
9047c478bd9Sstevel@tonic-gate 
9057c478bd9Sstevel@tonic-gate 	if (len < 0 || len > 255)
9067c478bd9Sstevel@tonic-gate 		return (KRB5_SENDAUTH_BADAUTHVERS);
9077c478bd9Sstevel@tonic-gate 
9087c478bd9Sstevel@tonic-gate 	buf = malloc(len);
9097c478bd9Sstevel@tonic-gate 	if (buf == NULL)
9107c478bd9Sstevel@tonic-gate 		return (ENOMEM);
9117c478bd9Sstevel@tonic-gate 
9127c478bd9Sstevel@tonic-gate 	length = krb5_net_read(context, fd, buf, len);
9137c478bd9Sstevel@tonic-gate 	if (len != length) {
9147c478bd9Sstevel@tonic-gate 		krb5_xfree(buf);
9157c478bd9Sstevel@tonic-gate 		return ((len < 0) ? errno : ECONNABORTED);
9167c478bd9Sstevel@tonic-gate 	}
9177c478bd9Sstevel@tonic-gate 
9187c478bd9Sstevel@tonic-gate 	if (strcmp(buf, KRB_V5_SENDAUTH_VERS) != 0) {
9197c478bd9Sstevel@tonic-gate 		krb5_xfree(buf);
9207c478bd9Sstevel@tonic-gate 		return (KRB5_SENDAUTH_BADAUTHVERS);
9217c478bd9Sstevel@tonic-gate 	}
9227c478bd9Sstevel@tonic-gate 	krb5_xfree(buf);
9237c478bd9Sstevel@tonic-gate 
9247c478bd9Sstevel@tonic-gate 	*auth_sys = KRB5_RECVAUTH_V5;
9257c478bd9Sstevel@tonic-gate 
9267c478bd9Sstevel@tonic-gate 	retval = krb5_recvauth_version(context, auth_context, fdp,
9277c478bd9Sstevel@tonic-gate 				    server, flags | KRB5_RECVAUTH_SKIP_VERSION,
9287c478bd9Sstevel@tonic-gate 				    keytab, ticket, version);
9297c478bd9Sstevel@tonic-gate 
9307c478bd9Sstevel@tonic-gate 	return (retval);
9317c478bd9Sstevel@tonic-gate }
9327c478bd9Sstevel@tonic-gate 
9337c478bd9Sstevel@tonic-gate 
9347c478bd9Sstevel@tonic-gate static void
9357c478bd9Sstevel@tonic-gate doit(int f,
9367c478bd9Sstevel@tonic-gate 	struct sockaddr_storage *fromp,
9377c478bd9Sstevel@tonic-gate 	krb5_context krb_context,
9387c478bd9Sstevel@tonic-gate 	int encr_flag,
9397c478bd9Sstevel@tonic-gate 	krb5_keytab keytab)
9407c478bd9Sstevel@tonic-gate {
9417c478bd9Sstevel@tonic-gate 	int p, t, on = 1;
9427c478bd9Sstevel@tonic-gate 	char c;
9437c478bd9Sstevel@tonic-gate 	char abuf[INET6_ADDRSTRLEN];
9447c478bd9Sstevel@tonic-gate 	struct sockaddr_in *sin;
9457c478bd9Sstevel@tonic-gate 	struct sockaddr_in6 *sin6;
9467c478bd9Sstevel@tonic-gate 	int fromplen;
9477c478bd9Sstevel@tonic-gate 	in_port_t port;
9487c478bd9Sstevel@tonic-gate 	struct termios tp;
9497c478bd9Sstevel@tonic-gate 	boolean_t bad_port;
9507c478bd9Sstevel@tonic-gate 	boolean_t no_name;
9517c478bd9Sstevel@tonic-gate 	char rhost_addra[INET6_ADDRSTRLEN];
9527c478bd9Sstevel@tonic-gate 
9537c478bd9Sstevel@tonic-gate 	if (!(rlbuf = malloc(BUFSIZ))) {
9547c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "rlbuf malloc failed\n");
9557c478bd9Sstevel@tonic-gate 		exit(EXIT_FAILURE);
9567c478bd9Sstevel@tonic-gate 	}
9577c478bd9Sstevel@tonic-gate 	(void) alarm(60);
9587c478bd9Sstevel@tonic-gate 	if (read(f, &c, 1) != 1 || c != 0) {
9597c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "failed to receive protocol zero byte\n");
9607c478bd9Sstevel@tonic-gate 		exit(EXIT_FAILURE);
9617c478bd9Sstevel@tonic-gate 	}
9627c478bd9Sstevel@tonic-gate 	(void) alarm(0);
9637c478bd9Sstevel@tonic-gate 	if (fromp->ss_family == AF_INET) {
9647c478bd9Sstevel@tonic-gate 		sin = (struct sockaddr_in *)fromp;
9657c478bd9Sstevel@tonic-gate 		port = sin->sin_port = ntohs((ushort_t)sin->sin_port);
9667c478bd9Sstevel@tonic-gate 		fromplen = sizeof (struct sockaddr_in);
9677c478bd9Sstevel@tonic-gate 
9687c478bd9Sstevel@tonic-gate 		if (!inet_ntop(AF_INET, &sin->sin_addr,
9697c478bd9Sstevel@tonic-gate 			    rhost_addra, sizeof (rhost_addra)))
9707c478bd9Sstevel@tonic-gate 			goto badconversion;
9717c478bd9Sstevel@tonic-gate 	} else if (fromp->ss_family == AF_INET6) {
9727c478bd9Sstevel@tonic-gate 		sin6 = (struct sockaddr_in6 *)fromp;
9737c478bd9Sstevel@tonic-gate 		port = sin6->sin6_port = ntohs((ushort_t)sin6->sin6_port);
9747c478bd9Sstevel@tonic-gate 		fromplen = sizeof (struct sockaddr_in6);
9757c478bd9Sstevel@tonic-gate 
9767c478bd9Sstevel@tonic-gate 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
9777c478bd9Sstevel@tonic-gate 			struct in_addr ipv4_addr;
9787c478bd9Sstevel@tonic-gate 
9797c478bd9Sstevel@tonic-gate 			IN6_V4MAPPED_TO_INADDR(&sin6->sin6_addr,
9807c478bd9Sstevel@tonic-gate 					    &ipv4_addr);
9817c478bd9Sstevel@tonic-gate 			if (!inet_ntop(AF_INET, &ipv4_addr, rhost_addra,
9827c478bd9Sstevel@tonic-gate 				    sizeof (rhost_addra)))
9837c478bd9Sstevel@tonic-gate 				goto badconversion;
9847c478bd9Sstevel@tonic-gate 		} else {
9857c478bd9Sstevel@tonic-gate 			if (!inet_ntop(AF_INET6, &sin6->sin6_addr,
9867c478bd9Sstevel@tonic-gate 				    rhost_addra, sizeof (rhost_addra)))
9877c478bd9Sstevel@tonic-gate 				goto badconversion;
9887c478bd9Sstevel@tonic-gate 		}
9897c478bd9Sstevel@tonic-gate 	} else {
9907c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "unknown address family %d\n",
9917c478bd9Sstevel@tonic-gate 		    fromp->ss_family);
9927c478bd9Sstevel@tonic-gate 		fatal(f, "Permission denied");
9937c478bd9Sstevel@tonic-gate 	}
9947c478bd9Sstevel@tonic-gate 
9957c478bd9Sstevel@tonic-gate 	/*
9967c478bd9Sstevel@tonic-gate 	 * Allow connections only from the "ephemeral" reserved
9977c478bd9Sstevel@tonic-gate 	 * ports(ports 512 - 1023) by checking the remote port
9987c478bd9Sstevel@tonic-gate 	 * because other utilities(e.g. in.ftpd) can be used to
9997c478bd9Sstevel@tonic-gate 	 * allow a unprivileged user to originate a connection
10007c478bd9Sstevel@tonic-gate 	 * from a privileged port and provide untrustworthy
10017c478bd9Sstevel@tonic-gate 	 * authentication.
10027c478bd9Sstevel@tonic-gate 	 */
10037c478bd9Sstevel@tonic-gate 	bad_port = (use_auth != KRB5_RECVAUTH_V5 &&
10047c478bd9Sstevel@tonic-gate 		    (port >= (in_port_t)IPPORT_RESERVED) ||
10057c478bd9Sstevel@tonic-gate 		    (port < (in_port_t)(IPPORT_RESERVED/2)));
10067c478bd9Sstevel@tonic-gate 	no_name = getnameinfo((const struct sockaddr *) fromp,
10077c478bd9Sstevel@tonic-gate 			    fromplen, hostname, sizeof (hostname),
10087c478bd9Sstevel@tonic-gate 			    NULL, 0, 0) != 0;
10097c478bd9Sstevel@tonic-gate 
10107c478bd9Sstevel@tonic-gate 	if (no_name || bad_port) {
10117c478bd9Sstevel@tonic-gate 		(void) strlcpy(abuf, rhost_addra, sizeof (abuf));
10127c478bd9Sstevel@tonic-gate 		/* If no host name, use IP address for name later on. */
10137c478bd9Sstevel@tonic-gate 		if (no_name)
10147c478bd9Sstevel@tonic-gate 			(void) strlcpy(hostname, abuf, sizeof (hostname));
10157c478bd9Sstevel@tonic-gate 	}
10167c478bd9Sstevel@tonic-gate 
10177243fb49Sjbeck 	if (!no_name) {
10187243fb49Sjbeck 		/*
10197243fb49Sjbeck 		 * Even if getnameinfo() succeeded, we still have to check
10207243fb49Sjbeck 		 * for spoofing.
10217243fb49Sjbeck 		 */
10227243fb49Sjbeck 		check_address("rlogind", fromp, sin, sin6, rhost_addra,
10237243fb49Sjbeck 		    hostname, sizeof (hostname));
10247243fb49Sjbeck 	}
10257243fb49Sjbeck 
10267c478bd9Sstevel@tonic-gate 	if (bad_port) {
10277c478bd9Sstevel@tonic-gate 		if (no_name)
10287c478bd9Sstevel@tonic-gate 			syslog(LOG_NOTICE,
10297c478bd9Sstevel@tonic-gate 			    "connection from %s - bad port\n",
10307c478bd9Sstevel@tonic-gate 			    abuf);
10317c478bd9Sstevel@tonic-gate 		else
10327c478bd9Sstevel@tonic-gate 			syslog(LOG_NOTICE,
10337c478bd9Sstevel@tonic-gate 			    "connection from %s(%s) - bad port\n",
10347c478bd9Sstevel@tonic-gate 			    hostname, abuf);
10357c478bd9Sstevel@tonic-gate 		fatal(f, "Permission denied");
10367c478bd9Sstevel@tonic-gate 	}
10377c478bd9Sstevel@tonic-gate 
10387c478bd9Sstevel@tonic-gate 	if (use_auth == KRB5_RECVAUTH_V5) {
10397c478bd9Sstevel@tonic-gate 		do_krb_login(f, rhost_addra, hostname,
10407c478bd9Sstevel@tonic-gate 			    krb_context, encr_flag, keytab);
10417c478bd9Sstevel@tonic-gate 		if (krusername != NULL && strlen(krusername)) {
10427c478bd9Sstevel@tonic-gate 			/*
10437c478bd9Sstevel@tonic-gate 			 * Kerberos Authentication succeeded,
10447c478bd9Sstevel@tonic-gate 			 * so set the proper program name to use
10457c478bd9Sstevel@tonic-gate 			 * with pam (important during 'cleanup'
10467c478bd9Sstevel@tonic-gate 			 * routine later).
10477c478bd9Sstevel@tonic-gate 			 */
10487c478bd9Sstevel@tonic-gate 			pam_prog_name = KRB5_PROG_NAME;
10497c478bd9Sstevel@tonic-gate 		}
10507c478bd9Sstevel@tonic-gate 	}
10517c478bd9Sstevel@tonic-gate 
10527c478bd9Sstevel@tonic-gate 	if (write(f, "", 1) != 1) {
10537c478bd9Sstevel@tonic-gate 		syslog(LOG_NOTICE,
10547c478bd9Sstevel@tonic-gate 		    "send of the zero byte(to %s) failed:"
10557c478bd9Sstevel@tonic-gate 		    " cannot start data transfer mode\n",
10567c478bd9Sstevel@tonic-gate 		    (no_name ? abuf : hostname));
10577c478bd9Sstevel@tonic-gate 		exit(EXIT_FAILURE);
10587c478bd9Sstevel@tonic-gate 	}
10597c478bd9Sstevel@tonic-gate 	if ((p = open("/dev/ptmx", O_RDWR)) == -1)
10607c478bd9Sstevel@tonic-gate 		fatalperror(f, "cannot open /dev/ptmx");
10617c478bd9Sstevel@tonic-gate 	if (grantpt(p) == -1)
10627c478bd9Sstevel@tonic-gate 		fatal(f, "could not grant slave pty");
10637c478bd9Sstevel@tonic-gate 	if (unlockpt(p) == -1)
10647c478bd9Sstevel@tonic-gate 		fatal(f, "could not unlock slave pty");
10657c478bd9Sstevel@tonic-gate 	if ((line = ptsname(p)) == NULL)
10667c478bd9Sstevel@tonic-gate 		fatal(f, "could not enable slave pty");
10677c478bd9Sstevel@tonic-gate 	if ((t = open(line, O_RDWR)) == -1)
10687c478bd9Sstevel@tonic-gate 		fatal(f, "could not open slave pty");
10697c478bd9Sstevel@tonic-gate 	if (ioctl(t, I_PUSH, "ptem") == -1)
10707c478bd9Sstevel@tonic-gate 		fatalperror(f, "ioctl I_PUSH ptem");
10717c478bd9Sstevel@tonic-gate 	if (ioctl(t, I_PUSH, "ldterm") == -1)
10727c478bd9Sstevel@tonic-gate 		fatalperror(f, "ioctl I_PUSH ldterm");
10737c478bd9Sstevel@tonic-gate 	if (ioctl(t, I_PUSH, "ttcompat") == -1)
10747c478bd9Sstevel@tonic-gate 		fatalperror(f, "ioctl I_PUSH ttcompat");
10757c478bd9Sstevel@tonic-gate 	/*
10767c478bd9Sstevel@tonic-gate 	 * POP the sockmod and push the rlmod module.
10777c478bd9Sstevel@tonic-gate 	 *
10787c478bd9Sstevel@tonic-gate 	 * Note that sockmod has to be removed since readstream assumes
10797c478bd9Sstevel@tonic-gate 	 * a "raw" TPI endpoint(e.g. it uses getmsg).
10807c478bd9Sstevel@tonic-gate 	 */
10817c478bd9Sstevel@tonic-gate 	if (removemod(f, "sockmod") < 0)
10827c478bd9Sstevel@tonic-gate 		fatalperror(f, "couldn't remove sockmod");
10837c478bd9Sstevel@tonic-gate 
10847c478bd9Sstevel@tonic-gate 	if (encr_flag) {
10857c478bd9Sstevel@tonic-gate 		if (ioctl(f, I_PUSH, "cryptmod") < 0)
10867c478bd9Sstevel@tonic-gate 		    fatalperror(f, "ioctl I_PUSH rlmod");
10877c478bd9Sstevel@tonic-gate 
10887c478bd9Sstevel@tonic-gate 	}
10897c478bd9Sstevel@tonic-gate 
10907c478bd9Sstevel@tonic-gate 	if (ioctl(f, I_PUSH, "rlmod") < 0)
10917c478bd9Sstevel@tonic-gate 		fatalperror(f, "ioctl I_PUSH rlmod");
10927c478bd9Sstevel@tonic-gate 
10937c478bd9Sstevel@tonic-gate 	if (encr_flag) {
10947c478bd9Sstevel@tonic-gate 		/*
10957c478bd9Sstevel@tonic-gate 		 * Make sure rlmod will pass unrecognized IOCTLs to cryptmod
10967c478bd9Sstevel@tonic-gate 		 */
10977c478bd9Sstevel@tonic-gate 		uchar_t passthru = 1;
10987c478bd9Sstevel@tonic-gate 		struct strioctl rlmodctl;
10997c478bd9Sstevel@tonic-gate 
11007c478bd9Sstevel@tonic-gate 		rlmodctl.ic_cmd = CRYPTPASSTHRU;
11017c478bd9Sstevel@tonic-gate 		rlmodctl.ic_timout = -1;
11027c478bd9Sstevel@tonic-gate 		rlmodctl.ic_len = sizeof (uchar_t);
11037c478bd9Sstevel@tonic-gate 		rlmodctl.ic_dp = (char *)&passthru;
11047c478bd9Sstevel@tonic-gate 
11057c478bd9Sstevel@tonic-gate 		if (ioctl(f, I_STR, &rlmodctl) < 0)
11067c478bd9Sstevel@tonic-gate 			fatal(f, "ioctl CRYPTPASSTHRU failed\n");
11077c478bd9Sstevel@tonic-gate 	}
11087c478bd9Sstevel@tonic-gate 
11097c478bd9Sstevel@tonic-gate 	/*
11107c478bd9Sstevel@tonic-gate 	 * readstream will do a getmsg till it receives
11117c478bd9Sstevel@tonic-gate 	 * M_PROTO type T_DATA_REQ from rloginmodopen()
11127c478bd9Sstevel@tonic-gate 	 * indicating all data on the stream prior to pushing rlmod has
11137c478bd9Sstevel@tonic-gate 	 * been drained at the stream head.
11147c478bd9Sstevel@tonic-gate 	 */
11157c478bd9Sstevel@tonic-gate 	if ((nsize = readstream(f, rlbuf, BUFSIZ)) < 0)
11167c478bd9Sstevel@tonic-gate 		fatalperror(f, "readstream failed");
11177c478bd9Sstevel@tonic-gate 	/*
11187c478bd9Sstevel@tonic-gate 	 * Make sure the pty doesn't modify the strings passed
11197c478bd9Sstevel@tonic-gate 	 * to login as part of the "rlogin protocol."  The login
11207c478bd9Sstevel@tonic-gate 	 * program should set these flags to apropriate values
11217c478bd9Sstevel@tonic-gate 	 * after it has read the strings.
11227c478bd9Sstevel@tonic-gate 	 */
11237c478bd9Sstevel@tonic-gate 	if (ioctl(t, TCGETS, &tp) == -1)
11247c478bd9Sstevel@tonic-gate 		fatalperror(f, "ioctl TCGETS");
11257c478bd9Sstevel@tonic-gate 	tp.c_lflag &= ~(ECHO|ICANON);
11267c478bd9Sstevel@tonic-gate 	tp.c_oflag &= ~(XTABS|OCRNL);
11277c478bd9Sstevel@tonic-gate 	tp.c_iflag &= ~(IGNPAR|ICRNL);
11287c478bd9Sstevel@tonic-gate 	if (ioctl(t, TCSETS, &tp) == -1)
11297c478bd9Sstevel@tonic-gate 		fatalperror(f, "ioctl TCSETS");
11307c478bd9Sstevel@tonic-gate 
11317c478bd9Sstevel@tonic-gate 	/*
11327c478bd9Sstevel@tonic-gate 	 * System V ptys allow the TIOC{SG}WINSZ ioctl to be
11337c478bd9Sstevel@tonic-gate 	 * issued on the master side of the pty.  Luckily, that's
11347c478bd9Sstevel@tonic-gate 	 * the only tty ioctl we need to do do, so we can close the
11357c478bd9Sstevel@tonic-gate 	 * slave side in the parent process after the fork.
11367c478bd9Sstevel@tonic-gate 	 */
11377c478bd9Sstevel@tonic-gate 	(void) ioctl(p, TIOCSWINSZ, &win);
11387c478bd9Sstevel@tonic-gate 
11397c478bd9Sstevel@tonic-gate 	pid = fork();
11407c478bd9Sstevel@tonic-gate 	if (pid < 0)
11417c478bd9Sstevel@tonic-gate 		fatalperror(f, "fork");
11427c478bd9Sstevel@tonic-gate 	if (pid == 0) {
11437c478bd9Sstevel@tonic-gate 		int tt;
11447c478bd9Sstevel@tonic-gate 		struct utmpx ut;
11457c478bd9Sstevel@tonic-gate 
11467c478bd9Sstevel@tonic-gate 		/* System V login expects a utmp entry to already be there */
11477c478bd9Sstevel@tonic-gate 		(void) memset(&ut, 0, sizeof (ut));
11487c478bd9Sstevel@tonic-gate 		(void) strncpy(ut.ut_user, ".rlogin", sizeof (ut.ut_user));
11497c478bd9Sstevel@tonic-gate 		(void) strncpy(ut.ut_line, line, sizeof (ut.ut_line));
11507c478bd9Sstevel@tonic-gate 		ut.ut_pid = getpid();
11517c478bd9Sstevel@tonic-gate 		ut.ut_id[0] = 'r';
11527c478bd9Sstevel@tonic-gate 		ut.ut_id[1] = (char)SC_WILDC;
11537c478bd9Sstevel@tonic-gate 		ut.ut_id[2] = (char)SC_WILDC;
11547c478bd9Sstevel@tonic-gate 		ut.ut_id[3] = (char)SC_WILDC;
11557c478bd9Sstevel@tonic-gate 		ut.ut_type = LOGIN_PROCESS;
11567c478bd9Sstevel@tonic-gate 		ut.ut_exit.e_termination = 0;
11577c478bd9Sstevel@tonic-gate 		ut.ut_exit.e_exit = 0;
11587c478bd9Sstevel@tonic-gate 		(void) time(&ut.ut_tv.tv_sec);
11597c478bd9Sstevel@tonic-gate 		if (makeutx(&ut) == NULL)
11607c478bd9Sstevel@tonic-gate 			syslog(LOG_INFO, "in.rlogind:\tmakeutx failed");
11617c478bd9Sstevel@tonic-gate 
11627c478bd9Sstevel@tonic-gate 		/* controlling tty */
11637c478bd9Sstevel@tonic-gate 		if (setsid() == -1)
11647c478bd9Sstevel@tonic-gate 			fatalperror(f, "setsid");
11657c478bd9Sstevel@tonic-gate 		if ((tt = open(line, O_RDWR)) == -1)
11667c478bd9Sstevel@tonic-gate 			fatalperror(f, "could not re-open slave pty");
11677c478bd9Sstevel@tonic-gate 
11687c478bd9Sstevel@tonic-gate 		if (close(p) == -1)
11697c478bd9Sstevel@tonic-gate 			fatalperror(f, "error closing pty master");
11707c478bd9Sstevel@tonic-gate 		if (close(t) == -1)
11717c478bd9Sstevel@tonic-gate 			fatalperror(f, "error closing pty slave"
11727c478bd9Sstevel@tonic-gate 				    " opened before session established");
11737c478bd9Sstevel@tonic-gate 		/*
11747c478bd9Sstevel@tonic-gate 		 * If this fails we may or may not be able to output an
11757c478bd9Sstevel@tonic-gate 		 * error message.
11767c478bd9Sstevel@tonic-gate 		 */
11777c478bd9Sstevel@tonic-gate 		if (close(f) == -1)
11787c478bd9Sstevel@tonic-gate 			fatalperror(f, "error closing deamon stdout");
11797c478bd9Sstevel@tonic-gate 		if (dup2(tt, STDIN_FILENO) == -1 ||
11807c478bd9Sstevel@tonic-gate 		    dup2(tt, STDOUT_FILENO) == -1 ||
11817c478bd9Sstevel@tonic-gate 		    dup2(tt, STDERR_FILENO) == -1)
11827c478bd9Sstevel@tonic-gate 			exit(EXIT_FAILURE);	/* Disaster! No stderr! */
11837c478bd9Sstevel@tonic-gate 
11847c478bd9Sstevel@tonic-gate 		(void) close(tt);
11857c478bd9Sstevel@tonic-gate 
11867c478bd9Sstevel@tonic-gate 		if (use_auth == KRB5_RECVAUTH_V5 &&
11877c478bd9Sstevel@tonic-gate 		    krusername != NULL && strlen(krusername)) {
11887c478bd9Sstevel@tonic-gate 			(void) execl(LOGIN_PROGRAM, "login",
11897c478bd9Sstevel@tonic-gate 				    "-d", line,
11907c478bd9Sstevel@tonic-gate 				    "-r", hostname,
11917c478bd9Sstevel@tonic-gate 				    "-u", krusername, /* KRB5 principal name */
11927c478bd9Sstevel@tonic-gate 				    "-s", pam_prog_name,
11937c478bd9Sstevel@tonic-gate 				    "-t", term,	/* Remote Terminal */
11947c478bd9Sstevel@tonic-gate 				    "-U", rusername,	/* Remote User */
11957c478bd9Sstevel@tonic-gate 				    "-R", KRB5_REPOSITORY_NAME,
11967c478bd9Sstevel@tonic-gate 				    lusername,  /* local user */
11977c478bd9Sstevel@tonic-gate 				    NULL);
11987c478bd9Sstevel@tonic-gate 		} else {
11997c478bd9Sstevel@tonic-gate 			(void) execl(LOGIN_PROGRAM, "login",
12007c478bd9Sstevel@tonic-gate 				"-d", line,
12017c478bd9Sstevel@tonic-gate 				"-r", hostname,
12027c478bd9Sstevel@tonic-gate 				NULL);
12037c478bd9Sstevel@tonic-gate 		}
12047c478bd9Sstevel@tonic-gate 
12057c478bd9Sstevel@tonic-gate 		fatalperror(STDERR_FILENO, "/bin/login");
12067c478bd9Sstevel@tonic-gate 		/*NOTREACHED*/
12077c478bd9Sstevel@tonic-gate 	}
12087c478bd9Sstevel@tonic-gate 	(void) close(t);
12097c478bd9Sstevel@tonic-gate 	(void) ioctl(f, FIONBIO, &on);
12107c478bd9Sstevel@tonic-gate 	(void) ioctl(p, FIONBIO, &on);
12117c478bd9Sstevel@tonic-gate 
12127c478bd9Sstevel@tonic-gate 	/*
12137c478bd9Sstevel@tonic-gate 	 * Must ignore SIGTTOU, otherwise we'll stop
12147c478bd9Sstevel@tonic-gate 	 * when we try and set slave pty's window shape
12157c478bd9Sstevel@tonic-gate 	 * (our controlling tty is the master pty).
12167c478bd9Sstevel@tonic-gate 	 * Likewise, we don't want any of the tty-generated
12177c478bd9Sstevel@tonic-gate 	 * signals from chars passing through.
12187c478bd9Sstevel@tonic-gate 	 */
12197c478bd9Sstevel@tonic-gate 	(void) sigset(SIGTSTP, SIG_IGN);
12207c478bd9Sstevel@tonic-gate 	(void) sigset(SIGINT, SIG_IGN);
12217c478bd9Sstevel@tonic-gate 	(void) sigset(SIGQUIT, SIG_IGN);
12227c478bd9Sstevel@tonic-gate 	(void) sigset(SIGTTOU, SIG_IGN);
12237c478bd9Sstevel@tonic-gate 	(void) sigset(SIGTTIN, SIG_IGN);
12247c478bd9Sstevel@tonic-gate 	(void) sigset(SIGCHLD, cleanup);
12257c478bd9Sstevel@tonic-gate 	(void) setpgrp();
12267c478bd9Sstevel@tonic-gate 
12277c478bd9Sstevel@tonic-gate 	if (encr_flag) {
12287c478bd9Sstevel@tonic-gate 		krb5_data ivec, *ivptr;
12297c478bd9Sstevel@tonic-gate 		uint_t ivec_usage;
12307c478bd9Sstevel@tonic-gate 		stop_stream(f, CRYPT_ENCRYPT|CRYPT_DECRYPT);
12317c478bd9Sstevel@tonic-gate 
12327c478bd9Sstevel@tonic-gate 		/*
12337c478bd9Sstevel@tonic-gate 		 * Configure the STREAMS crypto module.  For now,
12347c478bd9Sstevel@tonic-gate 		 * don't use any IV parameter.  KCMDV0.2 support
12357c478bd9Sstevel@tonic-gate 		 * will require the use of Initialization Vectors
12367c478bd9Sstevel@tonic-gate 		 * for both encrypt and decrypt modes.
12377c478bd9Sstevel@tonic-gate 		 */
12387c478bd9Sstevel@tonic-gate 		if (kcmd_protocol == KCMD_OLD_PROTOCOL) {
12397c478bd9Sstevel@tonic-gate 			if (session_key->enctype == ENCTYPE_DES_CBC_CRC) {
12407c478bd9Sstevel@tonic-gate 				/*
12417c478bd9Sstevel@tonic-gate 				 * This is gross but necessary for MIT compat.
12427c478bd9Sstevel@tonic-gate 				 */
12437c478bd9Sstevel@tonic-gate 				ivec.length = session_key->length;
12447c478bd9Sstevel@tonic-gate 				ivec.data = (char *)session_key->contents;
12457c478bd9Sstevel@tonic-gate 				ivec_usage = IVEC_REUSE;
12467c478bd9Sstevel@tonic-gate 				ivptr = &ivec;
12477c478bd9Sstevel@tonic-gate 			} else {
12487c478bd9Sstevel@tonic-gate 				ivptr = NULL; /* defaults to all 0's */
12497c478bd9Sstevel@tonic-gate 				ivec_usage = IVEC_NEVER;
12507c478bd9Sstevel@tonic-gate 			}
12517c478bd9Sstevel@tonic-gate 			/*
12527c478bd9Sstevel@tonic-gate 			 * configure both sides of stream together
12537c478bd9Sstevel@tonic-gate 			 * since they share the same IV.
12547c478bd9Sstevel@tonic-gate 			 * This is what makes the OLD KCMD protocol
12557c478bd9Sstevel@tonic-gate 			 * less secure than the newer one - Bad ivecs.
12567c478bd9Sstevel@tonic-gate 			 */
12577c478bd9Sstevel@tonic-gate 			if (configure_stream(f, session_key,
12587c478bd9Sstevel@tonic-gate 				CRYPT_ENCRYPT|CRYPT_DECRYPT,
12597c478bd9Sstevel@tonic-gate 				ivptr, ivec_usage) != 0)
12607c478bd9Sstevel@tonic-gate 				fatal(f, "Cannot initialize encryption -"
12617c478bd9Sstevel@tonic-gate 					" exiting.\n");
12627c478bd9Sstevel@tonic-gate 		} else {
12637c478bd9Sstevel@tonic-gate 			size_t blocksize;
12647c478bd9Sstevel@tonic-gate 			if (session_key->enctype == ENCTYPE_ARCFOUR_HMAC ||
12657c478bd9Sstevel@tonic-gate 			    session_key->enctype == ENCTYPE_ARCFOUR_HMAC_EXP) {
12667c478bd9Sstevel@tonic-gate 				if (configure_stream(f, session_key,
12677c478bd9Sstevel@tonic-gate 					CRYPT_ENCRYPT|CRYPT_DECRYPT,
12687c478bd9Sstevel@tonic-gate 					NULL, IVEC_NEVER) != 0)
12697c478bd9Sstevel@tonic-gate 					fatal(f,
12707c478bd9Sstevel@tonic-gate 					"Cannot initialize encryption -"
12717c478bd9Sstevel@tonic-gate 					" exiting.\n");
12727c478bd9Sstevel@tonic-gate 				goto startcrypto;
12737c478bd9Sstevel@tonic-gate 			}
12747c478bd9Sstevel@tonic-gate 			if (krb5_c_block_size(krb_context,
12757c478bd9Sstevel@tonic-gate 					    session_key->enctype,
12767c478bd9Sstevel@tonic-gate 					    &blocksize)) {
12777c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR, "Cannot determine blocksize "
12787c478bd9Sstevel@tonic-gate 				    "for encryption type %d",
12797c478bd9Sstevel@tonic-gate 				    session_key->enctype);
12807c478bd9Sstevel@tonic-gate 				fatal(f, "Cannot determine blocksize "
12817c478bd9Sstevel@tonic-gate 				    "for encryption - exiting.\n");
12827c478bd9Sstevel@tonic-gate 			}
12837c478bd9Sstevel@tonic-gate 			ivec.data = (char *)malloc(blocksize);
12847c478bd9Sstevel@tonic-gate 			ivec.length = blocksize;
12857c478bd9Sstevel@tonic-gate 			if (ivec.data == NULL)
12867c478bd9Sstevel@tonic-gate 				fatal(f, "memory error - exiting\n");
12877c478bd9Sstevel@tonic-gate 			/*
12887c478bd9Sstevel@tonic-gate 			 * Following MIT convention -
12897c478bd9Sstevel@tonic-gate 			 *   encrypt IV = 0x01 x blocksize
12907c478bd9Sstevel@tonic-gate 			 *   decrypt IV = 0x00 x blocksize
12917c478bd9Sstevel@tonic-gate 			 *   ivec_usage = IVEC_ONETIME
12927c478bd9Sstevel@tonic-gate 			 *
12937c478bd9Sstevel@tonic-gate 			 * configure_stream separately for encrypt and
12947c478bd9Sstevel@tonic-gate 			 * decrypt because there are 2 different IVs.
12957c478bd9Sstevel@tonic-gate 			 *
12967c478bd9Sstevel@tonic-gate 			 * AES uses 0's for IV.
12977c478bd9Sstevel@tonic-gate 			 */
12987c478bd9Sstevel@tonic-gate 			if (session_key->enctype ==
12997c478bd9Sstevel@tonic-gate 				ENCTYPE_AES128_CTS_HMAC_SHA1_96 ||
13007c478bd9Sstevel@tonic-gate 			    session_key->enctype ==
13017c478bd9Sstevel@tonic-gate 				ENCTYPE_AES256_CTS_HMAC_SHA1_96)
13027c478bd9Sstevel@tonic-gate 				(void) memset(ivec.data, 0x00, blocksize);
13037c478bd9Sstevel@tonic-gate 			else
13047c478bd9Sstevel@tonic-gate 				(void) memset(ivec.data, 0x01, blocksize);
13057c478bd9Sstevel@tonic-gate 			if (configure_stream(f, session_key, CRYPT_ENCRYPT,
13067c478bd9Sstevel@tonic-gate 				&ivec, IVEC_ONETIME) != 0)
13077c478bd9Sstevel@tonic-gate 				fatal(f, "Cannot initialize encryption -"
13087c478bd9Sstevel@tonic-gate 					" exiting.\n");
13097c478bd9Sstevel@tonic-gate 			(void) memset(ivec.data, 0x00, blocksize);
13107c478bd9Sstevel@tonic-gate 			if (configure_stream(f, session_key, CRYPT_DECRYPT,
13117c478bd9Sstevel@tonic-gate 				&ivec, IVEC_ONETIME) != 0)
13127c478bd9Sstevel@tonic-gate 				fatal(f, "Cannot initialize encryption -"
13137c478bd9Sstevel@tonic-gate 					" exiting.\n");
13147c478bd9Sstevel@tonic-gate 
13157c478bd9Sstevel@tonic-gate 			(void) free(ivec.data);
13167c478bd9Sstevel@tonic-gate 		}
13177c478bd9Sstevel@tonic-gate startcrypto:
13187c478bd9Sstevel@tonic-gate 		start_stream(f, CRYPT_ENCRYPT);
13197c478bd9Sstevel@tonic-gate 		start_stream(f, CRYPT_DECRYPT);
13207c478bd9Sstevel@tonic-gate 	}
13217c478bd9Sstevel@tonic-gate 	protocol(f, p, encr_flag);
13227c478bd9Sstevel@tonic-gate 	cleanup(0);
13237c478bd9Sstevel@tonic-gate 	/*NOTREACHED*/
13247c478bd9Sstevel@tonic-gate 
13257c478bd9Sstevel@tonic-gate badconversion:
13267c478bd9Sstevel@tonic-gate 	fatalperror(f, "address conversion");
13277c478bd9Sstevel@tonic-gate 	/*NOTREACHED*/
13287c478bd9Sstevel@tonic-gate }
13297c478bd9Sstevel@tonic-gate 
13307c478bd9Sstevel@tonic-gate /*
13317c478bd9Sstevel@tonic-gate  * rlogin "protocol" machine.
13327c478bd9Sstevel@tonic-gate  */
13337c478bd9Sstevel@tonic-gate static void
13347c478bd9Sstevel@tonic-gate protocol(int f, int p, int encr_flag)
13357c478bd9Sstevel@tonic-gate {
13367c478bd9Sstevel@tonic-gate 	struct	stat	buf;
13377c478bd9Sstevel@tonic-gate 	struct 	protocol_arg	rloginp;
13387c478bd9Sstevel@tonic-gate 	struct	strioctl	rloginmod;
13397c478bd9Sstevel@tonic-gate 	int	ptmfd;	/* fd of logindmux coneected to ptmx */
13407c478bd9Sstevel@tonic-gate 	int	netfd;	/* fd of logindmux connected to netf */
13417c478bd9Sstevel@tonic-gate 	static uchar_t	oobdata[] = {TIOCPKT_WINDOW};
13427c478bd9Sstevel@tonic-gate 
13437c478bd9Sstevel@tonic-gate 	/* indicate new rlogin */
13447c478bd9Sstevel@tonic-gate 	if (send_oob(f, oobdata, 1) < 0)
13457c478bd9Sstevel@tonic-gate 		fatalperror(f, "send_oob");
13467c478bd9Sstevel@tonic-gate 	/*
13477c478bd9Sstevel@tonic-gate 	 * We cannot send the SECURE_MSG until after the
13487c478bd9Sstevel@tonic-gate 	 * client has been signaled with the oobdata (above).
13497c478bd9Sstevel@tonic-gate 	 */
13507c478bd9Sstevel@tonic-gate 	if (encr_flag) {
13517c478bd9Sstevel@tonic-gate 		if (write(f, SECURE_MSG, strlen(SECURE_MSG)) < 0)
13527c478bd9Sstevel@tonic-gate 			fatalperror(f, "Error writing SECURE message");
13537c478bd9Sstevel@tonic-gate 	}
13547c478bd9Sstevel@tonic-gate 
13557c478bd9Sstevel@tonic-gate 	/*
13567c478bd9Sstevel@tonic-gate 	 * Open logindmux driver and link netf and ptmx
13577c478bd9Sstevel@tonic-gate 	 * underneath logindmux.
13587c478bd9Sstevel@tonic-gate 	 */
13597c478bd9Sstevel@tonic-gate 	if ((ptmfd = open("/dev/logindmux", O_RDWR)) == -1)
13607c478bd9Sstevel@tonic-gate 		fatalperror(f, "open /dev/logindmux");
13617c478bd9Sstevel@tonic-gate 
13627c478bd9Sstevel@tonic-gate 	if ((netfd = open("/dev/logindmux", O_RDWR)) == -1)
13637c478bd9Sstevel@tonic-gate 		fatalperror(f, "open /dev/logindmux");
13647c478bd9Sstevel@tonic-gate 
13657c478bd9Sstevel@tonic-gate 	if (ioctl(ptmfd, I_LINK, p) < 0)
13667c478bd9Sstevel@tonic-gate 		fatal(f, "ioctl I_LINK of /dev/ptmx failed\n");
13677c478bd9Sstevel@tonic-gate 
13687c478bd9Sstevel@tonic-gate 	if (ioctl(netfd, I_LINK, f) < 0)
13697c478bd9Sstevel@tonic-gate 		fatal(f, "ioctl I_LINK of tcp connection failed\n");
13707c478bd9Sstevel@tonic-gate 
13717c478bd9Sstevel@tonic-gate 	/*
13727c478bd9Sstevel@tonic-gate 	 * Figure out the device number of the ptm's mux fd, and pass that
13737c478bd9Sstevel@tonic-gate 	 * to the net's mux.
13747c478bd9Sstevel@tonic-gate 	 */
13757c478bd9Sstevel@tonic-gate 	if (fstat(ptmfd, &buf) < 0)
13767c478bd9Sstevel@tonic-gate 		fatalperror(f, "cannot determine device number"
13777c478bd9Sstevel@tonic-gate 		    " of pty side of /dev/logindmux");
13787c478bd9Sstevel@tonic-gate 	rloginp.dev = buf.st_rdev;
13797c478bd9Sstevel@tonic-gate 	rloginp.flag = 0;
13807c478bd9Sstevel@tonic-gate 
13817c478bd9Sstevel@tonic-gate 	rloginmod.ic_cmd = LOGDMX_IOC_QEXCHANGE;
13827c478bd9Sstevel@tonic-gate 	rloginmod.ic_timout = -1;
13837c478bd9Sstevel@tonic-gate 	rloginmod.ic_len = sizeof (struct protocol_arg);
13847c478bd9Sstevel@tonic-gate 	rloginmod.ic_dp = (char *)&rloginp;
13857c478bd9Sstevel@tonic-gate 
13867c478bd9Sstevel@tonic-gate 	if (ioctl(netfd, I_STR, &rloginmod) < 0)
13877c478bd9Sstevel@tonic-gate 		fatal(netfd, "ioctl LOGDMX_IOC_QEXCHANGE of netfd failed\n");
13887c478bd9Sstevel@tonic-gate 
13897c478bd9Sstevel@tonic-gate 	/*
13907c478bd9Sstevel@tonic-gate 	 * Figure out the device number of the net's mux fd, and pass that
13917c478bd9Sstevel@tonic-gate 	 * to the ptm's mux.
13927c478bd9Sstevel@tonic-gate 	 */
13937c478bd9Sstevel@tonic-gate 	if (fstat(netfd, &buf))
13947c478bd9Sstevel@tonic-gate 		fatalperror(f, "cannot determine device number"
13957c478bd9Sstevel@tonic-gate 		    " of network side of /dev/logindmux");
13967c478bd9Sstevel@tonic-gate 	rloginp.dev = buf.st_rdev;
13977c478bd9Sstevel@tonic-gate 	rloginp.flag = 1;
13987c478bd9Sstevel@tonic-gate 
13997c478bd9Sstevel@tonic-gate 	rloginmod.ic_cmd = LOGDMX_IOC_QEXCHANGE;
14007c478bd9Sstevel@tonic-gate 	rloginmod.ic_timout = -1;
14017c478bd9Sstevel@tonic-gate 	rloginmod.ic_len = sizeof (struct protocol_arg);
14027c478bd9Sstevel@tonic-gate 	rloginmod.ic_dp = (char *)&rloginp;
14037c478bd9Sstevel@tonic-gate 
14047c478bd9Sstevel@tonic-gate 	if (ioctl(ptmfd, I_STR, &rloginmod) < 0)
14057c478bd9Sstevel@tonic-gate 		fatal(netfd, "ioctl LOGDMXZ_IOC_QEXCHANGE of ptmfd failed\n");
14067c478bd9Sstevel@tonic-gate 	/*
14077c478bd9Sstevel@tonic-gate 	 * Send an ioctl type RL_IOC_ENABLE to reenable the
14087c478bd9Sstevel@tonic-gate 	 * message queue and reinsert the data read from streamhead
14097c478bd9Sstevel@tonic-gate 	 * at the time of pushing rloginmod module.
14107c478bd9Sstevel@tonic-gate 	 * We need to send this ioctl even if no data was read earlier
14117c478bd9Sstevel@tonic-gate 	 * since we need to reenable the message queue of rloginmod module.
14127c478bd9Sstevel@tonic-gate 	 */
14137c478bd9Sstevel@tonic-gate 	rloginmod.ic_cmd = RL_IOC_ENABLE;
14147c478bd9Sstevel@tonic-gate 	rloginmod.ic_timout = -1;
14157c478bd9Sstevel@tonic-gate 	if (nsize) {
14167c478bd9Sstevel@tonic-gate 		rloginmod.ic_len = nsize;
14177c478bd9Sstevel@tonic-gate 		rloginmod.ic_dp = rlbuf;
14187c478bd9Sstevel@tonic-gate 	} else {
14197c478bd9Sstevel@tonic-gate 		rloginmod.ic_len = 0;
14207c478bd9Sstevel@tonic-gate 		rloginmod.ic_dp = NULL;
14217c478bd9Sstevel@tonic-gate 	}
14227c478bd9Sstevel@tonic-gate 
14237c478bd9Sstevel@tonic-gate 	if (ioctl(netfd, I_STR, &rloginmod) < 0)
14247c478bd9Sstevel@tonic-gate 		fatal(netfd, "ioctl RL_IOC_ENABLE of netfd failed\n");
14257c478bd9Sstevel@tonic-gate 
14267c478bd9Sstevel@tonic-gate 	/*
14277c478bd9Sstevel@tonic-gate 	 * User level daemon now pauses till the shell exits.
14287c478bd9Sstevel@tonic-gate 	 */
14297c478bd9Sstevel@tonic-gate 	(void) pause();
14307c478bd9Sstevel@tonic-gate }
14317c478bd9Sstevel@tonic-gate 
14327c478bd9Sstevel@tonic-gate /* This is a signal handler, hence the dummy argument */
14337c478bd9Sstevel@tonic-gate /*ARGSUSED*/
14347c478bd9Sstevel@tonic-gate static void
14357c478bd9Sstevel@tonic-gate cleanup(int dummy)
14367c478bd9Sstevel@tonic-gate {
14377c478bd9Sstevel@tonic-gate 	rmut();
14387c478bd9Sstevel@tonic-gate 	exit(EXIT_FAILURE);
14397c478bd9Sstevel@tonic-gate 	/*NOTREACHED*/
14407c478bd9Sstevel@tonic-gate }
14417c478bd9Sstevel@tonic-gate 
14427c478bd9Sstevel@tonic-gate /*
14437c478bd9Sstevel@tonic-gate  * TPI style replacement for socket send() primitive, so we don't require
14447c478bd9Sstevel@tonic-gate  * sockmod to be on the stream.
14457c478bd9Sstevel@tonic-gate  */
14467c478bd9Sstevel@tonic-gate static int
14477c478bd9Sstevel@tonic-gate send_oob(int fd, void *ptr, size_t count)
14487c478bd9Sstevel@tonic-gate {
14497c478bd9Sstevel@tonic-gate 	struct T_exdata_req exd_req;
14507c478bd9Sstevel@tonic-gate 	struct strbuf hdr, dat;
14517c478bd9Sstevel@tonic-gate 	int ret;
14527c478bd9Sstevel@tonic-gate 
14537c478bd9Sstevel@tonic-gate 	exd_req.PRIM_type = T_EXDATA_REQ;
14547c478bd9Sstevel@tonic-gate 	exd_req.MORE_flag = 0;
14557c478bd9Sstevel@tonic-gate 
14567c478bd9Sstevel@tonic-gate 	hdr.buf = (char *)&exd_req;
14577c478bd9Sstevel@tonic-gate 	hdr.len = sizeof (exd_req);
14587c478bd9Sstevel@tonic-gate 
14597c478bd9Sstevel@tonic-gate 	dat.buf = ptr;
14607c478bd9Sstevel@tonic-gate 	dat.len = count;
14617c478bd9Sstevel@tonic-gate 
14627c478bd9Sstevel@tonic-gate 	ret = putmsg(fd, &hdr, &dat, 0);
14637c478bd9Sstevel@tonic-gate 	if (ret == 0)
14647c478bd9Sstevel@tonic-gate 		ret = count;
14657c478bd9Sstevel@tonic-gate 	return (ret);
14667c478bd9Sstevel@tonic-gate }
14677c478bd9Sstevel@tonic-gate 
14687c478bd9Sstevel@tonic-gate static void
14697c478bd9Sstevel@tonic-gate fatal(int fd, const char *msg)
14707c478bd9Sstevel@tonic-gate {
14717c478bd9Sstevel@tonic-gate 	char *bufp;
14727c478bd9Sstevel@tonic-gate 	size_t len = strlen(msg) + 16;		/* enough for our wrapper */
14737c478bd9Sstevel@tonic-gate 
14747c478bd9Sstevel@tonic-gate 	bufp = alloca(len);
14757c478bd9Sstevel@tonic-gate 	/* ASCII 001 is the error indicator */
14767c478bd9Sstevel@tonic-gate 	len = snprintf(bufp, len, "\01rlogind: %s.\r\n", msg);
14777c478bd9Sstevel@tonic-gate 	(void) write(fd, bufp, len);
14787c478bd9Sstevel@tonic-gate 	exit(EXIT_FAILURE);
14797c478bd9Sstevel@tonic-gate 	/*NOTREACHED*/
14807c478bd9Sstevel@tonic-gate }
14817c478bd9Sstevel@tonic-gate 
14827c478bd9Sstevel@tonic-gate /*PRINTFLIKE2*/
14837c478bd9Sstevel@tonic-gate static void
14847c478bd9Sstevel@tonic-gate fatalperror(int fd, const char *msg)
14857c478bd9Sstevel@tonic-gate {
14867c478bd9Sstevel@tonic-gate 	char *bufp;
14877c478bd9Sstevel@tonic-gate 	const char *errstr;
14887c478bd9Sstevel@tonic-gate 	int save_errno = errno;
14897c478bd9Sstevel@tonic-gate 	size_t len = strlen(msg);
14907c478bd9Sstevel@tonic-gate 
14917c478bd9Sstevel@tonic-gate 	if ((errstr = strerror(save_errno))) {
14927c478bd9Sstevel@tonic-gate 		len += strlen(errstr) + 3;	/* 3 for ": " and \0 below */
14937c478bd9Sstevel@tonic-gate 		bufp = alloca(len);
14947c478bd9Sstevel@tonic-gate 		(void) snprintf(bufp, len, "%s: %s", msg, errstr);
14957c478bd9Sstevel@tonic-gate 	} else {
14967c478bd9Sstevel@tonic-gate 		const char fmt[] = "%s: Error %d";
14977c478bd9Sstevel@tonic-gate 
14987c478bd9Sstevel@tonic-gate 		/* -4 for %s & %d. "*8/3" is bytes->decimal, pessimistically */
14997c478bd9Sstevel@tonic-gate 		len += sizeof (fmt) -4 + (sizeof (save_errno) *8 /3);
15007c478bd9Sstevel@tonic-gate 		bufp = alloca(len);
15017c478bd9Sstevel@tonic-gate 		(void) snprintf(bufp, len, fmt, msg, save_errno);
15027c478bd9Sstevel@tonic-gate 	}
15037c478bd9Sstevel@tonic-gate 	fatal(fd, bufp);
15047c478bd9Sstevel@tonic-gate 	/*NOTREACHED*/
15057c478bd9Sstevel@tonic-gate }
15067c478bd9Sstevel@tonic-gate 
15077c478bd9Sstevel@tonic-gate static void
15087c478bd9Sstevel@tonic-gate rmut(void)
15097c478bd9Sstevel@tonic-gate {
15107c478bd9Sstevel@tonic-gate 	pam_handle_t *pamh;
15117c478bd9Sstevel@tonic-gate 	struct utmpx *up;
15127c478bd9Sstevel@tonic-gate 	char user[sizeof (up->ut_user) + 1];
15137c478bd9Sstevel@tonic-gate 	char ttyn[sizeof (up->ut_line) + 1];
15147c478bd9Sstevel@tonic-gate 	char rhost[sizeof (up->ut_host) + 1];
15157c478bd9Sstevel@tonic-gate 
15167c478bd9Sstevel@tonic-gate 	/* while cleaning up dont allow disruption */
15177c478bd9Sstevel@tonic-gate 	(void) sigset(SIGCHLD, SIG_IGN);
15187c478bd9Sstevel@tonic-gate 
15197c478bd9Sstevel@tonic-gate 	setutxent();
15207c478bd9Sstevel@tonic-gate 	while (up = getutxent()) {
15217c478bd9Sstevel@tonic-gate 		if (up->ut_pid == pid) {
15227c478bd9Sstevel@tonic-gate 			if (up->ut_type == DEAD_PROCESS)
15237c478bd9Sstevel@tonic-gate 				break;		/* Cleaned up elsewhere. */
15247c478bd9Sstevel@tonic-gate 
15257c478bd9Sstevel@tonic-gate 			/*
15267c478bd9Sstevel@tonic-gate 			 * call pam_close_session if login changed
15277c478bd9Sstevel@tonic-gate 			 * the utmpx user entry from type LOGIN_PROCESS
15287c478bd9Sstevel@tonic-gate 			 * to type USER_PROCESS, which happens
15297c478bd9Sstevel@tonic-gate 			 * after pam_open_session is called.
15307c478bd9Sstevel@tonic-gate 			 */
15317c478bd9Sstevel@tonic-gate 			if (up->ut_type == USER_PROCESS) {
15327c478bd9Sstevel@tonic-gate 				(void) strlcpy(user, up->ut_user,
15337c478bd9Sstevel@tonic-gate 					    sizeof (user));
15347c478bd9Sstevel@tonic-gate 				(void) strlcpy(ttyn, up->ut_line,
15357c478bd9Sstevel@tonic-gate 					    sizeof (ttyn));
15367c478bd9Sstevel@tonic-gate 				(void) strlcpy(rhost, up->ut_host,
15377c478bd9Sstevel@tonic-gate 					    sizeof (rhost));
15387c478bd9Sstevel@tonic-gate 
15397c478bd9Sstevel@tonic-gate 				/*
15407c478bd9Sstevel@tonic-gate 				 * Use the same pam_prog_name that
15417c478bd9Sstevel@tonic-gate 				 * 'login' used.
15427c478bd9Sstevel@tonic-gate 				 */
15437c478bd9Sstevel@tonic-gate 				if ((pam_start(pam_prog_name, user,  NULL,
15447c478bd9Sstevel@tonic-gate 					    &pamh))
15457c478bd9Sstevel@tonic-gate 				    == PAM_SUCCESS) {
15467c478bd9Sstevel@tonic-gate 					(void) pam_set_item(pamh, PAM_TTY,
15477c478bd9Sstevel@tonic-gate 							    ttyn);
15487c478bd9Sstevel@tonic-gate 					(void) pam_set_item(pamh, PAM_RHOST,
15497c478bd9Sstevel@tonic-gate 							    rhost);
15507c478bd9Sstevel@tonic-gate 					(void) pam_close_session(pamh, 0);
15517c478bd9Sstevel@tonic-gate 					(void) pam_end(pamh, PAM_SUCCESS);
15527c478bd9Sstevel@tonic-gate 				}
15537c478bd9Sstevel@tonic-gate 			}
15547c478bd9Sstevel@tonic-gate 
15557c478bd9Sstevel@tonic-gate 			up->ut_type = DEAD_PROCESS;
15567c478bd9Sstevel@tonic-gate 			up->ut_exit.e_termination = WTERMSIG(0);
15577c478bd9Sstevel@tonic-gate 			up->ut_exit.e_exit = WEXITSTATUS(0);
15587c478bd9Sstevel@tonic-gate 			(void) time(&up->ut_tv.tv_sec);
15597c478bd9Sstevel@tonic-gate 
15607c478bd9Sstevel@tonic-gate 			if (modutx(up) == NULL) {
15617c478bd9Sstevel@tonic-gate 				/*
15627c478bd9Sstevel@tonic-gate 				 * Since modutx failed we'll
15637c478bd9Sstevel@tonic-gate 				 * write out the new entry
15647c478bd9Sstevel@tonic-gate 				 * ourselves.
15657c478bd9Sstevel@tonic-gate 				 */
15667c478bd9Sstevel@tonic-gate 				(void) pututxline(up);
15677c478bd9Sstevel@tonic-gate 				updwtmpx("wtmpx", up);
15687c478bd9Sstevel@tonic-gate 			}
15697c478bd9Sstevel@tonic-gate 			break;
15707c478bd9Sstevel@tonic-gate 		}
15717c478bd9Sstevel@tonic-gate 	}
15727c478bd9Sstevel@tonic-gate 
15737c478bd9Sstevel@tonic-gate 	endutxent();
15747c478bd9Sstevel@tonic-gate 
15757c478bd9Sstevel@tonic-gate 	(void) sigset(SIGCHLD, cleanup);
15767c478bd9Sstevel@tonic-gate }
15777c478bd9Sstevel@tonic-gate 
15787c478bd9Sstevel@tonic-gate static int
15797c478bd9Sstevel@tonic-gate readstream(int fd, char *buf, int size)
15807c478bd9Sstevel@tonic-gate {
15817c478bd9Sstevel@tonic-gate 	struct strbuf ctlbuf, datbuf;
15827c478bd9Sstevel@tonic-gate 	union T_primitives tpi;
15837c478bd9Sstevel@tonic-gate 	int	nbytes = 0;
15847c478bd9Sstevel@tonic-gate 	int	ret = 0;
15857c478bd9Sstevel@tonic-gate 	int	flags = 0;
15867c478bd9Sstevel@tonic-gate 	int	bufsize = size;
15877c478bd9Sstevel@tonic-gate 	int	nread;
15887c478bd9Sstevel@tonic-gate 
15897c478bd9Sstevel@tonic-gate 	(void) memset(&ctlbuf, 0, sizeof (ctlbuf));
15907c478bd9Sstevel@tonic-gate 	(void) memset(&datbuf, 0, sizeof (datbuf));
15917c478bd9Sstevel@tonic-gate 
15927c478bd9Sstevel@tonic-gate 	ctlbuf.buf = (char *)&tpi;
15937c478bd9Sstevel@tonic-gate 	ctlbuf.maxlen = sizeof (tpi);
15947c478bd9Sstevel@tonic-gate 	datbuf.buf = buf;
15957c478bd9Sstevel@tonic-gate 	datbuf.maxlen = size;
15967c478bd9Sstevel@tonic-gate 
15977c478bd9Sstevel@tonic-gate 	for (;;) {
15987c478bd9Sstevel@tonic-gate 		if (ioctl(fd, I_NREAD, &nread) < 0) {
15997c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, "I_NREAD returned error %m");
16007c478bd9Sstevel@tonic-gate 			return (-1);
16017c478bd9Sstevel@tonic-gate 		}
16027c478bd9Sstevel@tonic-gate 		if (nread + nbytes > bufsize) {
16037c478bd9Sstevel@tonic-gate 			buf = (char *)realloc(buf, (unsigned)(bufsize + nread));
16047c478bd9Sstevel@tonic-gate 			if (buf == NULL) {
16057c478bd9Sstevel@tonic-gate 				syslog(LOG_WARNING,
16067c478bd9Sstevel@tonic-gate 				    "buffer allocation failed\n");
16077c478bd9Sstevel@tonic-gate 				return (-1);
16087c478bd9Sstevel@tonic-gate 			}
16097c478bd9Sstevel@tonic-gate 			bufsize += nread;
16107c478bd9Sstevel@tonic-gate 			rlbuf = buf;
16117c478bd9Sstevel@tonic-gate 			datbuf.buf = buf + nbytes;
16127c478bd9Sstevel@tonic-gate 		}
16137c478bd9Sstevel@tonic-gate 		datbuf.maxlen = bufsize - nbytes;
16147c478bd9Sstevel@tonic-gate 		ret = getmsg(fd, &ctlbuf, &datbuf, &flags);
16157c478bd9Sstevel@tonic-gate 		if (ret < 0) {
16167c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, "getmsg failed error %m");
16177c478bd9Sstevel@tonic-gate 			return (-1);
16187c478bd9Sstevel@tonic-gate 		}
16197c478bd9Sstevel@tonic-gate 		if ((ctlbuf.len == 0) && (datbuf.len == 0)) {
16207c478bd9Sstevel@tonic-gate 			/*
16217c478bd9Sstevel@tonic-gate 			 * getmsg() returned no data - this indicates
16227c478bd9Sstevel@tonic-gate 			 * that the connection is closing down.
16237c478bd9Sstevel@tonic-gate 			 */
16247c478bd9Sstevel@tonic-gate 			cleanup(0);
16257c478bd9Sstevel@tonic-gate 		}
16267c478bd9Sstevel@tonic-gate 		if (ctlbuf.len <= 0) {
16277c478bd9Sstevel@tonic-gate 			nbytes += datbuf.len;
16287c478bd9Sstevel@tonic-gate 			datbuf.buf += datbuf.len;
16297c478bd9Sstevel@tonic-gate 			continue;
16307c478bd9Sstevel@tonic-gate 		}
16317c478bd9Sstevel@tonic-gate 		if (tpi.type == T_DATA_REQ) {
16327c478bd9Sstevel@tonic-gate 			return (nbytes);
16337c478bd9Sstevel@tonic-gate 		}
16347c478bd9Sstevel@tonic-gate 		if ((tpi.type == T_ORDREL_IND) || (tpi.type == T_DISCON_IND))
16357c478bd9Sstevel@tonic-gate 			cleanup(0);
16367c478bd9Sstevel@tonic-gate 	}
16377c478bd9Sstevel@tonic-gate }
16387c478bd9Sstevel@tonic-gate 
16397c478bd9Sstevel@tonic-gate /*
16407c478bd9Sstevel@tonic-gate  * Verify that the named module is at the top of the stream
16417c478bd9Sstevel@tonic-gate  * and then pop it off.
16427c478bd9Sstevel@tonic-gate  */
16437c478bd9Sstevel@tonic-gate static int
16447c478bd9Sstevel@tonic-gate removemod(int f, char *modname)
16457c478bd9Sstevel@tonic-gate {
16467c478bd9Sstevel@tonic-gate 	char topmodname[BUFSIZ];
16477c478bd9Sstevel@tonic-gate 
16487c478bd9Sstevel@tonic-gate 	if (ioctl(f, I_LOOK, topmodname) < 0)
16497c478bd9Sstevel@tonic-gate 		return (-1);
16507c478bd9Sstevel@tonic-gate 	if (strcmp(modname, topmodname) != 0) {
16517c478bd9Sstevel@tonic-gate 		errno = ENXIO;
16527c478bd9Sstevel@tonic-gate 		return (-1);
16537c478bd9Sstevel@tonic-gate 	}
16547c478bd9Sstevel@tonic-gate 	if (ioctl(f, I_POP, 0) < 0)
16557c478bd9Sstevel@tonic-gate 		return (-1);
16567c478bd9Sstevel@tonic-gate 	return (0);
16577c478bd9Sstevel@tonic-gate }
1658