xref: /titanic_50/usr/src/cmd/cmd-inet/usr.sbin/in.rshd.c (revision 32885d593baf8bac788fa78885893a51b3ad0f28)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57243fb49Sjbeck  * Common Development and Distribution License (the "License").
67243fb49Sjbeck  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217243fb49Sjbeck 
227c478bd9Sstevel@tonic-gate /*
23*32885d59Sgtb  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*	Copyright (c) 1983-1989 AT&T	*/
287c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley 4.3 BSD
327c478bd9Sstevel@tonic-gate  * under license from the Regents of the University of California.
337c478bd9Sstevel@tonic-gate  */
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate #define	_FILE_OFFSET_BITS 64
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate /*
407c478bd9Sstevel@tonic-gate  * remote shell server:
417c478bd9Sstevel@tonic-gate  *	remuser\0
427c478bd9Sstevel@tonic-gate  *	locuser\0
437c478bd9Sstevel@tonic-gate  *	command\0
447c478bd9Sstevel@tonic-gate  *	data
457c478bd9Sstevel@tonic-gate  */
467c478bd9Sstevel@tonic-gate #include <sys/types.h>
477c478bd9Sstevel@tonic-gate #include <sys/ioctl.h>
487c478bd9Sstevel@tonic-gate #include <sys/telioctl.h>
497c478bd9Sstevel@tonic-gate #include <sys/param.h>
507c478bd9Sstevel@tonic-gate #include <sys/socket.h>
517c478bd9Sstevel@tonic-gate #include <sys/time.h>
527c478bd9Sstevel@tonic-gate #include <sys/stat.h>
537c478bd9Sstevel@tonic-gate #include <sys/file.h>
547c478bd9Sstevel@tonic-gate #include <sys/select.h>
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate #include <netinet/in.h>
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate #include <unistd.h>
617c478bd9Sstevel@tonic-gate #include <string.h>
627c478bd9Sstevel@tonic-gate #include <stdio.h>
637c478bd9Sstevel@tonic-gate #include <stdarg.h>
647c478bd9Sstevel@tonic-gate #include <errno.h>
657c478bd9Sstevel@tonic-gate #include <pwd.h>
667c478bd9Sstevel@tonic-gate #include <grp.h>
677c478bd9Sstevel@tonic-gate #include <signal.h>
687c478bd9Sstevel@tonic-gate #include <netdb.h>
697c478bd9Sstevel@tonic-gate #include <syslog.h>
707c478bd9Sstevel@tonic-gate #include <fcntl.h>
717c478bd9Sstevel@tonic-gate #include <ctype.h>
727c478bd9Sstevel@tonic-gate #include <locale.h>
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate #include <sys/resource.h>
757c478bd9Sstevel@tonic-gate #include <sys/filio.h>
767c478bd9Sstevel@tonic-gate #include <shadow.h>
777c478bd9Sstevel@tonic-gate #include <stdlib.h>
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate #include <security/pam_appl.h>
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate #include <k5-int.h>
827c478bd9Sstevel@tonic-gate #include <krb5_repository.h>
837c478bd9Sstevel@tonic-gate #include <com_err.h>
847c478bd9Sstevel@tonic-gate #include <kcmd.h>
857c478bd9Sstevel@tonic-gate 
867243fb49Sjbeck #include <addr_match.h>
87*32885d59Sgtb #include <store_forw_creds.h>
887243fb49Sjbeck 
897c478bd9Sstevel@tonic-gate #ifndef NCARGS
907c478bd9Sstevel@tonic-gate #define	NCARGS	5120
917c478bd9Sstevel@tonic-gate #endif /* !NCARGS */
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate static void error(char *, ...);
947c478bd9Sstevel@tonic-gate static void doit(int, struct sockaddr_storage *, char **);
957c478bd9Sstevel@tonic-gate static void getstr(int, char *, int, char *);
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate static int legalenvvar(char *);
987c478bd9Sstevel@tonic-gate static void add_to_envinit(char *);
997c478bd9Sstevel@tonic-gate static int locale_envmatch(char *, char *);
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate /* Function decls. for functions not in any header file.  (Grrrr.) */
1027c478bd9Sstevel@tonic-gate extern int audit_rshd_setup(void);
1037c478bd9Sstevel@tonic-gate extern int audit_rshd_success(char *, char *, char *, char *);
1047c478bd9Sstevel@tonic-gate extern int audit_rshd_fail(char *, char *, char *, char *, char *);
1057c478bd9Sstevel@tonic-gate extern int audit_settid(int);
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate static int do_encrypt = 0;
1087c478bd9Sstevel@tonic-gate static pam_handle_t *pamh;
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate /*
1117c478bd9Sstevel@tonic-gate  * This is the shell/kshell daemon. The very basic protocol for checking
1127c478bd9Sstevel@tonic-gate  * authentication and authorization is:
1137c478bd9Sstevel@tonic-gate  * 1) Check authentication.
1147c478bd9Sstevel@tonic-gate  * 2) Check authorization via the access-control files:
1157c478bd9Sstevel@tonic-gate  *    ~/.k5login (using krb5_kuserok) and/or
1167c478bd9Sstevel@tonic-gate  * Execute command if configured authoriztion checks pass, else deny
1177c478bd9Sstevel@tonic-gate  * permission.
1187c478bd9Sstevel@tonic-gate  *
1197c478bd9Sstevel@tonic-gate  * The configuration is done either by command-line arguments passed by inetd,
1207c478bd9Sstevel@tonic-gate  * or by the name of the daemon. If command-line arguments are present, they
1217c478bd9Sstevel@tonic-gate  * take priority. The options are:
1227c478bd9Sstevel@tonic-gate  * -k allow kerberos authentication (krb5 only; krb4 support is not provided)
1237c478bd9Sstevel@tonic-gate  * -5 same as `-k', mainly for compatability with MIT
1247c478bd9Sstevel@tonic-gate  * -e allow encrypted session
1257c478bd9Sstevel@tonic-gate  * -c demand authenticator checksum
1267c478bd9Sstevel@tonic-gate  * -i ignore authenticator checksum
1277c478bd9Sstevel@tonic-gate  * -U Refuse connections that cannot be mapped to a name via `gethostbyname'
1287c478bd9Sstevel@tonic-gate  * -s <tos>	Set the IP TOS option
1297c478bd9Sstevel@tonic-gate  * -S <keytab>	Set the keytab file to use
1307c478bd9Sstevel@tonic-gate  * -M <realm>	Set the Kerberos realm to use
1317c478bd9Sstevel@tonic-gate  */
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate #define	ARGSTR	"ek5ciUD:M:S:L:?:"
1347c478bd9Sstevel@tonic-gate #define	RSHD_BUFSIZ	(50 * 1024)
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate static krb5_context bsd_context;
1377c478bd9Sstevel@tonic-gate static krb5_keytab keytab = NULL;
1387c478bd9Sstevel@tonic-gate static krb5_ccache ccache = NULL;
1397c478bd9Sstevel@tonic-gate static krb5_keyblock *sessionkey = NULL;
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate static int require_encrypt = 0;
1427c478bd9Sstevel@tonic-gate static int resolve_hostname = 0;
1437c478bd9Sstevel@tonic-gate static int krb5auth_flag = 0;	/* Flag set, when KERBEROS is enabled */
1447c478bd9Sstevel@tonic-gate static enum kcmd_proto kcmd_protocol;
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate #ifdef DEBUG
1477c478bd9Sstevel@tonic-gate static int debug_port = 0;
1487c478bd9Sstevel@tonic-gate #endif /* DEBUG */
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate /*
1517c478bd9Sstevel@tonic-gate  * There are two authentication related masks:
1527c478bd9Sstevel@tonic-gate  * auth_ok and auth_sent.
1537c478bd9Sstevel@tonic-gate  * The auth_ok mask is the or'ing of authentication
1547c478bd9Sstevel@tonic-gate  * systems any one of which can be used.
1557c478bd9Sstevel@tonic-gate  * The auth_sent mask is the or'ing of one or more authentication/authorization
1567c478bd9Sstevel@tonic-gate  * systems that succeeded.  If the and'ing
1577c478bd9Sstevel@tonic-gate  * of these two masks is true, then authorization is successful.
1587c478bd9Sstevel@tonic-gate  */
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate #define	AUTH_KRB5	(0x2)
1617c478bd9Sstevel@tonic-gate static int auth_ok = 0;
1627c478bd9Sstevel@tonic-gate static int auth_sent = 0;
1637c478bd9Sstevel@tonic-gate static int checksum_required = 0;
1647c478bd9Sstevel@tonic-gate static int checksum_ignored = 0;
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate /*
1677c478bd9Sstevel@tonic-gate  * Leave room for 4 environment variables to be passed.
1687c478bd9Sstevel@tonic-gate  * The "-L env_var" option has been added primarily to
1697c478bd9Sstevel@tonic-gate  * maintain compatability with MIT.
1707c478bd9Sstevel@tonic-gate  */
1717c478bd9Sstevel@tonic-gate #define	MAXENV	4
1727c478bd9Sstevel@tonic-gate static char *save_env[MAXENV];
1737c478bd9Sstevel@tonic-gate static int num_env = 0;
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate static void usage(void);
1767c478bd9Sstevel@tonic-gate static krb5_error_code recvauth(int, int *);
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate /*ARGSUSED*/
179740638c8Sbw int
1807c478bd9Sstevel@tonic-gate main(int argc, char **argv, char **renvp)
1817c478bd9Sstevel@tonic-gate {
1827c478bd9Sstevel@tonic-gate 	struct linger linger;
1837c478bd9Sstevel@tonic-gate 	int on = 1, fromlen;
1847c478bd9Sstevel@tonic-gate 	struct sockaddr_storage from;
1857c478bd9Sstevel@tonic-gate 	int fd = 0;
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate 	extern int opterr, optind;
1887c478bd9Sstevel@tonic-gate 	extern char *optarg;
1897c478bd9Sstevel@tonic-gate 	int ch;
1907c478bd9Sstevel@tonic-gate 	int tos = -1;
1917c478bd9Sstevel@tonic-gate 	krb5_error_code status;
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate 	openlog("rsh", LOG_PID | LOG_ODELAY, LOG_DAEMON);
1947c478bd9Sstevel@tonic-gate 	(void) audit_rshd_setup();	/* BSM */
1957c478bd9Sstevel@tonic-gate 	fromlen = sizeof (from);
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate 	/*
2007c478bd9Sstevel@tonic-gate 	 * Analyze parameters.
2017c478bd9Sstevel@tonic-gate 	 */
2027c478bd9Sstevel@tonic-gate 	opterr = 0;
2037c478bd9Sstevel@tonic-gate 	while ((ch = getopt(argc, argv, ARGSTR)) != EOF)
2047c478bd9Sstevel@tonic-gate 		switch (ch) {
2057c478bd9Sstevel@tonic-gate 		case '5':
2067c478bd9Sstevel@tonic-gate 		case 'k':
2077c478bd9Sstevel@tonic-gate 			auth_ok |= AUTH_KRB5;
2087c478bd9Sstevel@tonic-gate 			krb5auth_flag++;
2097c478bd9Sstevel@tonic-gate 			break;
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate 		case 'c':
2127c478bd9Sstevel@tonic-gate 			checksum_required = 1;
2137c478bd9Sstevel@tonic-gate 			krb5auth_flag++;
2147c478bd9Sstevel@tonic-gate 			break;
2157c478bd9Sstevel@tonic-gate 		case 'i':
2167c478bd9Sstevel@tonic-gate 			checksum_ignored = 1;
2177c478bd9Sstevel@tonic-gate 			krb5auth_flag++;
2187c478bd9Sstevel@tonic-gate 			break;
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate 		case 'e':
2217c478bd9Sstevel@tonic-gate 			require_encrypt = 1;
2227c478bd9Sstevel@tonic-gate 			krb5auth_flag++;
2237c478bd9Sstevel@tonic-gate 			break;
2247c478bd9Sstevel@tonic-gate #ifdef DEBUG
2257c478bd9Sstevel@tonic-gate 		case 'D':
2267c478bd9Sstevel@tonic-gate 			debug_port = atoi(optarg);
2277c478bd9Sstevel@tonic-gate 			break;
2287c478bd9Sstevel@tonic-gate #endif /* DEBUG */
2297c478bd9Sstevel@tonic-gate 		case 'U':
2307c478bd9Sstevel@tonic-gate 			resolve_hostname = 1;
2317c478bd9Sstevel@tonic-gate 			break;
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate 		case 'M':
2347c478bd9Sstevel@tonic-gate 			krb5_set_default_realm(bsd_context, optarg);
2357c478bd9Sstevel@tonic-gate 			krb5auth_flag++;
2367c478bd9Sstevel@tonic-gate 			break;
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate 		case 'S':
2397c478bd9Sstevel@tonic-gate 			if ((status = krb5_kt_resolve(bsd_context, optarg,
2407c478bd9Sstevel@tonic-gate 				&keytab))) {
2417c478bd9Sstevel@tonic-gate 				com_err("rsh", status,
2427c478bd9Sstevel@tonic-gate 					gettext("while resolving "
2437c478bd9Sstevel@tonic-gate 						"srvtab file %s"), optarg);
2447c478bd9Sstevel@tonic-gate 				exit(2);
2457c478bd9Sstevel@tonic-gate 			}
2467c478bd9Sstevel@tonic-gate 			krb5auth_flag++;
2477c478bd9Sstevel@tonic-gate 			break;
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate 		case 's':
2507c478bd9Sstevel@tonic-gate 			if (optarg == NULL || ((tos = atoi(optarg)) < 0) ||
2517c478bd9Sstevel@tonic-gate 				(tos > 255)) {
2527c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR, "rshd: illegal tos value: "
2537c478bd9Sstevel@tonic-gate 				    "%s\n", optarg);
2547c478bd9Sstevel@tonic-gate 			}
2557c478bd9Sstevel@tonic-gate 			break;
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate 		case 'L':
2587c478bd9Sstevel@tonic-gate 			if (num_env < MAXENV) {
2597c478bd9Sstevel@tonic-gate 				save_env[num_env] = strdup(optarg);
2607c478bd9Sstevel@tonic-gate 				if (!save_env[num_env++]) {
2617c478bd9Sstevel@tonic-gate 					com_err("rsh", ENOMEM,
2627c478bd9Sstevel@tonic-gate 						gettext("in saving env"));
2637c478bd9Sstevel@tonic-gate 					exit(2);
2647c478bd9Sstevel@tonic-gate 				}
2657c478bd9Sstevel@tonic-gate 			} else {
2667c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext("rshd: Only %d"
2677c478bd9Sstevel@tonic-gate 						" -L arguments allowed\n"),
2687c478bd9Sstevel@tonic-gate 						MAXENV);
2697c478bd9Sstevel@tonic-gate 				exit(2);
2707c478bd9Sstevel@tonic-gate 			}
2717c478bd9Sstevel@tonic-gate 			break;
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate 		case '?':
2747c478bd9Sstevel@tonic-gate 		default:
2757c478bd9Sstevel@tonic-gate 			usage();
2767c478bd9Sstevel@tonic-gate 			exit(1);
2777c478bd9Sstevel@tonic-gate 			break;
2787c478bd9Sstevel@tonic-gate 		}
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate 	if (optind == 0) {
2817c478bd9Sstevel@tonic-gate 		usage();
2827c478bd9Sstevel@tonic-gate 		exit(1);
2837c478bd9Sstevel@tonic-gate 	}
2847c478bd9Sstevel@tonic-gate 	argc -= optind;
2857c478bd9Sstevel@tonic-gate 	argv += optind;
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate 	if (krb5auth_flag > 0) {
2887c478bd9Sstevel@tonic-gate 		status = krb5_init_context(&bsd_context);
2897c478bd9Sstevel@tonic-gate 		if (status) {
2907c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, "Error initializing krb5: %s",
2917c478bd9Sstevel@tonic-gate 			    error_message(status));
2927c478bd9Sstevel@tonic-gate 			exit(1);
2937c478bd9Sstevel@tonic-gate 		}
2947c478bd9Sstevel@tonic-gate 	}
2957c478bd9Sstevel@tonic-gate 
2967c478bd9Sstevel@tonic-gate 	if (!checksum_required && !checksum_ignored)
2977c478bd9Sstevel@tonic-gate 		checksum_ignored = 1;
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate 	if (checksum_required && checksum_ignored) {
3007c478bd9Sstevel@tonic-gate 		syslog(LOG_CRIT, gettext("Checksums are required and ignored."
3017c478bd9Sstevel@tonic-gate 		"These options are mutually exclusive"
3027c478bd9Sstevel@tonic-gate 		"--check the documentation."));
3037c478bd9Sstevel@tonic-gate 		error("Configuration error: mutually exclusive "
3047c478bd9Sstevel@tonic-gate 				"options specified.\n");
3057c478bd9Sstevel@tonic-gate 		exit(1);
3067c478bd9Sstevel@tonic-gate 	}
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate #ifdef DEBUG
3097c478bd9Sstevel@tonic-gate 	if (debug_port) {
3107c478bd9Sstevel@tonic-gate 		int s;
3117c478bd9Sstevel@tonic-gate 		struct sockaddr_in sin;
3127c478bd9Sstevel@tonic-gate 
3137c478bd9Sstevel@tonic-gate 		if ((s = socket(AF_INET, SOCK_STREAM, PF_UNSPEC)) < 0) {
3147c478bd9Sstevel@tonic-gate 			fprintf(stderr, gettext("Error in socket: %s\n"),
3157c478bd9Sstevel@tonic-gate 					strerror(errno));
3167c478bd9Sstevel@tonic-gate 			exit(2);
3177c478bd9Sstevel@tonic-gate 		}
3187c478bd9Sstevel@tonic-gate 		(void) memset((char *)&sin, 0, sizeof (sin));
3197c478bd9Sstevel@tonic-gate 		sin.sin_family = AF_INET;
3207c478bd9Sstevel@tonic-gate 		sin.sin_port = htons(debug_port);
3217c478bd9Sstevel@tonic-gate 		sin.sin_addr.s_addr = INADDR_ANY;
3227c478bd9Sstevel@tonic-gate 
3237c478bd9Sstevel@tonic-gate 		(void) setsockopt(s, SOL_SOCKET, SO_REUSEADDR,
3247c478bd9Sstevel@tonic-gate 			(char *)&on, sizeof (on));
3257c478bd9Sstevel@tonic-gate 
3267c478bd9Sstevel@tonic-gate 		if ((bind(s, (struct sockaddr *)&sin, sizeof (sin))) < 0) {
3277c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("Error in bind: %s\n"),
3287c478bd9Sstevel@tonic-gate 					strerror(errno));
3297c478bd9Sstevel@tonic-gate 			exit(2);
3307c478bd9Sstevel@tonic-gate 		}
3317c478bd9Sstevel@tonic-gate 		if ((listen(s, 5)) < 0) {
3327c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("Error in listen: %s\n"),
3337c478bd9Sstevel@tonic-gate 					strerror(errno));
3347c478bd9Sstevel@tonic-gate 			exit(2);
3357c478bd9Sstevel@tonic-gate 		}
3367c478bd9Sstevel@tonic-gate 		if ((fd = accept(s, (struct sockaddr *)&from,
3377c478bd9Sstevel@tonic-gate 					&fromlen)) < 0) {
3387c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("Error in accept: %s\n"),
3397c478bd9Sstevel@tonic-gate 					strerror(errno));
3407c478bd9Sstevel@tonic-gate 			exit(2);
3417c478bd9Sstevel@tonic-gate 		}
3427c478bd9Sstevel@tonic-gate 		(void) close(s);
3437c478bd9Sstevel@tonic-gate 	}
3447c478bd9Sstevel@tonic-gate 	else
3457c478bd9Sstevel@tonic-gate #endif /* DEBUG */
3467c478bd9Sstevel@tonic-gate 	{
3477c478bd9Sstevel@tonic-gate 		if (getpeername(STDIN_FILENO, (struct sockaddr *)&from,
3487c478bd9Sstevel@tonic-gate 				(socklen_t *)&fromlen) < 0) {
3497c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "rshd: ");
3507c478bd9Sstevel@tonic-gate 			perror("getpeername");
3517c478bd9Sstevel@tonic-gate 			_exit(1);
3527c478bd9Sstevel@tonic-gate 		}
3537c478bd9Sstevel@tonic-gate 		fd = STDIN_FILENO;
3547c478bd9Sstevel@tonic-gate 	}
3557c478bd9Sstevel@tonic-gate 
3567c478bd9Sstevel@tonic-gate 	if (audit_settid(fd) != 0) {
3577c478bd9Sstevel@tonic-gate 		perror("settid");
3587c478bd9Sstevel@tonic-gate 		exit(1);
3597c478bd9Sstevel@tonic-gate 	}
3607c478bd9Sstevel@tonic-gate 
3617c478bd9Sstevel@tonic-gate 	if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, (char *)&on,
3627c478bd9Sstevel@tonic-gate 	    sizeof (on)) < 0)
3637c478bd9Sstevel@tonic-gate 		syslog(LOG_WARNING, "setsockopt (SO_KEEPALIVE): %m");
3647c478bd9Sstevel@tonic-gate 	linger.l_onoff = 1;
3657c478bd9Sstevel@tonic-gate 	linger.l_linger = 60;			/* XXX */
3667c478bd9Sstevel@tonic-gate 	if (setsockopt(fd, SOL_SOCKET, SO_LINGER, (char *)&linger,
3677c478bd9Sstevel@tonic-gate 	    sizeof (linger)) < 0)
3687c478bd9Sstevel@tonic-gate 		syslog(LOG_WARNING, "setsockopt (SO_LINGER): %m");
3697c478bd9Sstevel@tonic-gate 
3707c478bd9Sstevel@tonic-gate 	if ((tos != -1) && (setsockopt(fd, IPPROTO_IP, IP_TOS, (char *)&tos,
3717c478bd9Sstevel@tonic-gate 				sizeof (tos)) < 0) &&
3727c478bd9Sstevel@tonic-gate 				(errno != ENOPROTOOPT)) {
3737c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "setsockopt (IP_TOS %d): %m");
3747c478bd9Sstevel@tonic-gate 	}
3757c478bd9Sstevel@tonic-gate 
3767c478bd9Sstevel@tonic-gate 	doit(dup(fd), &from, renvp);
377740638c8Sbw 	return (0);
3787c478bd9Sstevel@tonic-gate }
3797c478bd9Sstevel@tonic-gate 
3807c478bd9Sstevel@tonic-gate /*
3817c478bd9Sstevel@tonic-gate  * locale environments to be passed to shells.
3827c478bd9Sstevel@tonic-gate  */
3837c478bd9Sstevel@tonic-gate static char *localeenv[] = {
3847c478bd9Sstevel@tonic-gate 	"LANG",
3857c478bd9Sstevel@tonic-gate 	"LC_CTYPE", "LC_NUMERIC", "LC_TIME", "LC_COLLATE",
3867c478bd9Sstevel@tonic-gate 	"LC_MONETARY", "LC_MESSAGES", "LC_ALL", NULL};
3877c478bd9Sstevel@tonic-gate 
3887c478bd9Sstevel@tonic-gate /*
3897c478bd9Sstevel@tonic-gate  * The following is for the environment variable list
3907c478bd9Sstevel@tonic-gate  * used in the call to execle().  envinit is declared here,
3917c478bd9Sstevel@tonic-gate  * but populated after the call to getpwnam().
3927c478bd9Sstevel@tonic-gate  */
3937c478bd9Sstevel@tonic-gate static char	*homedir;	/* "HOME=" */
3947c478bd9Sstevel@tonic-gate static char	*shell;		/* "SHELL=" */
3957c478bd9Sstevel@tonic-gate static char	*username;	/* "USER=" */
3967c478bd9Sstevel@tonic-gate static char	*tz;		/* "TZ=" */
3977c478bd9Sstevel@tonic-gate 
3987c478bd9Sstevel@tonic-gate static char	homestr[] = "HOME=";
3997c478bd9Sstevel@tonic-gate static char	shellstr[] = "SHELL=";
4007c478bd9Sstevel@tonic-gate static char	userstr[] = "USER=";
4017c478bd9Sstevel@tonic-gate static char	tzstr[] = "TZ=";
4027c478bd9Sstevel@tonic-gate 
4037c478bd9Sstevel@tonic-gate static char	**envinit;
4047c478bd9Sstevel@tonic-gate #define	PAM_ENV_ELIM	16	/* allow 16 PAM environment variables */
4057c478bd9Sstevel@tonic-gate #define	USERNAME_LEN	16	/* maximum number of characters in user name */
4067c478bd9Sstevel@tonic-gate 
4077c478bd9Sstevel@tonic-gate /*
4087c478bd9Sstevel@tonic-gate  *	See PSARC opinion 1992/025
4097c478bd9Sstevel@tonic-gate  */
4107c478bd9Sstevel@tonic-gate static char	userpath[] = "PATH=/usr/bin:";
4117c478bd9Sstevel@tonic-gate static char	rootpath[] = "PATH=/usr/sbin:/usr/bin";
4127c478bd9Sstevel@tonic-gate 
4137c478bd9Sstevel@tonic-gate static char cmdbuf[NCARGS+1];
4147c478bd9Sstevel@tonic-gate static char hostname [MAXHOSTNAMELEN + 1];
4157c478bd9Sstevel@tonic-gate static char locuser[USERNAME_LEN + 1];
4167c478bd9Sstevel@tonic-gate static char remuser[USERNAME_LEN + 1];
4177c478bd9Sstevel@tonic-gate 
4187c478bd9Sstevel@tonic-gate #define	KRB5_RECVAUTH_V5	5
4197c478bd9Sstevel@tonic-gate #define	SIZEOF_INADDR sizeof	(struct in_addr)
4207c478bd9Sstevel@tonic-gate 
4217c478bd9Sstevel@tonic-gate #define	MAX_REPOSITORY_LEN	255
4227c478bd9Sstevel@tonic-gate static char repository[MAX_REPOSITORY_LEN];
4237c478bd9Sstevel@tonic-gate 
4247c478bd9Sstevel@tonic-gate static char *kremuser;
4257c478bd9Sstevel@tonic-gate static krb5_principal client = NULL;
4267c478bd9Sstevel@tonic-gate 
4277c478bd9Sstevel@tonic-gate static char	remote_addr[64];
4287c478bd9Sstevel@tonic-gate static char	local_addr[64];
4297c478bd9Sstevel@tonic-gate 
4307c478bd9Sstevel@tonic-gate static void
4317c478bd9Sstevel@tonic-gate doit(int f, struct sockaddr_storage *fromp, char **renvp)
4327c478bd9Sstevel@tonic-gate {
4337c478bd9Sstevel@tonic-gate 	char *cp;
4347c478bd9Sstevel@tonic-gate 
4357c478bd9Sstevel@tonic-gate 	struct passwd *pwd;
4367c478bd9Sstevel@tonic-gate 	char *path;
4377c478bd9Sstevel@tonic-gate 	char *tzenv;
4387c478bd9Sstevel@tonic-gate 	struct spwd *shpwd;
4397c478bd9Sstevel@tonic-gate 	struct stat statb;
4407c478bd9Sstevel@tonic-gate 	char **lenvp;
4417c478bd9Sstevel@tonic-gate 
4427c478bd9Sstevel@tonic-gate 	krb5_error_code status;
4437c478bd9Sstevel@tonic-gate 	int valid_checksum;
4447c478bd9Sstevel@tonic-gate 	int cnt;
4457c478bd9Sstevel@tonic-gate 	int sin_len;
4467c478bd9Sstevel@tonic-gate 	struct sockaddr_in localaddr;
4477c478bd9Sstevel@tonic-gate 
4487c478bd9Sstevel@tonic-gate 	int s;
4497c478bd9Sstevel@tonic-gate 	in_port_t port;
4507c478bd9Sstevel@tonic-gate 	pid_t pid;
4517c478bd9Sstevel@tonic-gate 	int pv[2], pw[2], px[2], cc;
4527c478bd9Sstevel@tonic-gate 	char buf[RSHD_BUFSIZ];
4537c478bd9Sstevel@tonic-gate 	char sig;
4547c478bd9Sstevel@tonic-gate 	int one = 1;
4557c478bd9Sstevel@tonic-gate 	int v = 0;
4567c478bd9Sstevel@tonic-gate 	int err = 0;
4577c478bd9Sstevel@tonic-gate 	int idx = 0;
4587c478bd9Sstevel@tonic-gate 	char **pam_env;
4597c478bd9Sstevel@tonic-gate 	char abuf[INET6_ADDRSTRLEN];
4607c478bd9Sstevel@tonic-gate 	struct sockaddr_in *sin;
4617c478bd9Sstevel@tonic-gate 	struct sockaddr_in6 *sin6;
4627c478bd9Sstevel@tonic-gate 	int fromplen;
4637c478bd9Sstevel@tonic-gate 	int homedir_len, shell_len, username_len, tz_len;
4647c478bd9Sstevel@tonic-gate 	int no_name;
4657243fb49Sjbeck 	boolean_t bad_port;
4667c478bd9Sstevel@tonic-gate 	int netf = 0;
4677c478bd9Sstevel@tonic-gate 
4687c478bd9Sstevel@tonic-gate 	(void) signal(SIGINT, SIG_DFL);
4697c478bd9Sstevel@tonic-gate 	(void) signal(SIGQUIT, SIG_DFL);
4707c478bd9Sstevel@tonic-gate 	(void) signal(SIGTERM, SIG_DFL);
4717c478bd9Sstevel@tonic-gate 	(void) signal(SIGXCPU, SIG_DFL);
4727c478bd9Sstevel@tonic-gate 	(void) signal(SIGXFSZ, SIG_DFL);
4737c478bd9Sstevel@tonic-gate 	(void) sigset(SIGCHLD, SIG_IGN);
4747c478bd9Sstevel@tonic-gate 	(void) signal(SIGPIPE, SIG_DFL);
4757c478bd9Sstevel@tonic-gate 	(void) signal(SIGHUP, SIG_DFL);
4767c478bd9Sstevel@tonic-gate 
4777c478bd9Sstevel@tonic-gate #ifdef DEBUG
4787c478bd9Sstevel@tonic-gate 	{ int t = open("/dev/tty", 2);
4797c478bd9Sstevel@tonic-gate 	    if (t >= 0) {
4807c478bd9Sstevel@tonic-gate 		(void) setsid();
4817c478bd9Sstevel@tonic-gate 		(void) close(t);
4827c478bd9Sstevel@tonic-gate 	    }
4837c478bd9Sstevel@tonic-gate 	}
4847c478bd9Sstevel@tonic-gate #endif
4857c478bd9Sstevel@tonic-gate 	if (fromp->ss_family == AF_INET) {
4867c478bd9Sstevel@tonic-gate 		sin = (struct sockaddr_in *)fromp;
4877c478bd9Sstevel@tonic-gate 		port = ntohs((ushort_t)sin->sin_port);
4887c478bd9Sstevel@tonic-gate 		fromplen = sizeof (struct sockaddr_in);
4897c478bd9Sstevel@tonic-gate 	} else if (fromp->ss_family == AF_INET6) {
4907c478bd9Sstevel@tonic-gate 		sin6 = (struct sockaddr_in6 *)fromp;
4917c478bd9Sstevel@tonic-gate 		port = ntohs((ushort_t)sin6->sin6_port);
4927c478bd9Sstevel@tonic-gate 		fromplen = sizeof (struct sockaddr_in6);
4937c478bd9Sstevel@tonic-gate 	} else {
4947c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "wrong address family\n");
4957c478bd9Sstevel@tonic-gate 		exit(1);
4967c478bd9Sstevel@tonic-gate 	}
4977c478bd9Sstevel@tonic-gate 
4987243fb49Sjbeck 	if (fromp->ss_family == AF_INET6) {
4997243fb49Sjbeck 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
5007243fb49Sjbeck 			struct in_addr ipv4_addr;
5017243fb49Sjbeck 
5027243fb49Sjbeck 			IN6_V4MAPPED_TO_INADDR(&sin6->sin6_addr, &ipv4_addr);
5037243fb49Sjbeck 			(void) inet_ntop(AF_INET, &ipv4_addr, abuf,
5047243fb49Sjbeck 			    sizeof (abuf));
5057243fb49Sjbeck 		} else {
5067243fb49Sjbeck 			(void) inet_ntop(AF_INET6, &sin6->sin6_addr, abuf,
5077243fb49Sjbeck 			    sizeof (abuf));
5087243fb49Sjbeck 		}
5097243fb49Sjbeck 	} else if (fromp->ss_family == AF_INET) {
5107243fb49Sjbeck 		(void) inet_ntop(AF_INET, &sin->sin_addr, abuf, sizeof (abuf));
5117243fb49Sjbeck 	}
5127243fb49Sjbeck 
5137c478bd9Sstevel@tonic-gate 	sin_len = sizeof (struct sockaddr_in);
5147243fb49Sjbeck 	if (getsockname(f, (struct sockaddr *)&localaddr, &sin_len) < 0) {
5157c478bd9Sstevel@tonic-gate 		perror("getsockname");
5167c478bd9Sstevel@tonic-gate 		exit(1);
5177c478bd9Sstevel@tonic-gate 	}
5187c478bd9Sstevel@tonic-gate 
5197c478bd9Sstevel@tonic-gate 	netf = f;
5207c478bd9Sstevel@tonic-gate 
5217c478bd9Sstevel@tonic-gate 	bad_port = (port >= IPPORT_RESERVED ||
5227c478bd9Sstevel@tonic-gate 		port < (uint_t)(IPPORT_RESERVED/2));
5237c478bd9Sstevel@tonic-gate 
5247243fb49Sjbeck 	/* Get the name of the client side host to use later */
5257c478bd9Sstevel@tonic-gate 	no_name = (getnameinfo((const struct sockaddr *) fromp, fromplen,
5267c478bd9Sstevel@tonic-gate 		hostname, sizeof (hostname), NULL, 0, 0) != 0);
5277c478bd9Sstevel@tonic-gate 
5287243fb49Sjbeck 	if (bad_port || no_name != 0) {
5297243fb49Sjbeck 		/*
5307243fb49Sjbeck 		 * If there is no host name available then use the
5317243fb49Sjbeck 		 * IP address to identify the host in the PAM call
5327243fb49Sjbeck 		 * below.  Do the same if a bad port was used, to
5337243fb49Sjbeck 		 * prevent untrustworthy authentication.
5347243fb49Sjbeck 		 */
5357243fb49Sjbeck 		(void) strlcpy(hostname, abuf, sizeof (hostname));
5367243fb49Sjbeck 	}
5377243fb49Sjbeck 
5387c478bd9Sstevel@tonic-gate 	if (no_name != 0) {
5397c478bd9Sstevel@tonic-gate 		/*
5407c478bd9Sstevel@tonic-gate 		 * If the '-U' option was given on the cmd line,
5417c478bd9Sstevel@tonic-gate 		 * we must be able to lookup the hostname
5427c478bd9Sstevel@tonic-gate 		 */
5437c478bd9Sstevel@tonic-gate 		if (resolve_hostname) {
5447c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, "rshd: Couldn't resolve your "
5457c478bd9Sstevel@tonic-gate 			    "address into a host name.\r\n Please "
5467c478bd9Sstevel@tonic-gate 			    "contact your net administrator");
5477c478bd9Sstevel@tonic-gate 			exit(1);
5487243fb49Sjbeck 		}
5497c478bd9Sstevel@tonic-gate 	} else {
5507c478bd9Sstevel@tonic-gate 		/*
5517243fb49Sjbeck 		 * Even if getnameinfo() succeeded, we still have to check
5527243fb49Sjbeck 		 * for spoofing.
5537c478bd9Sstevel@tonic-gate 		 */
5547243fb49Sjbeck 		check_address("rshd", fromp, sin, sin6, abuf, hostname,
5557c478bd9Sstevel@tonic-gate 		    sizeof (hostname));
5567c478bd9Sstevel@tonic-gate 	}
5577c478bd9Sstevel@tonic-gate 
5587c478bd9Sstevel@tonic-gate 	if (!krb5auth_flag && bad_port) {
5597c478bd9Sstevel@tonic-gate 		if (no_name)
5607c478bd9Sstevel@tonic-gate 			syslog(LOG_NOTICE, "connection from %s - "
5617c478bd9Sstevel@tonic-gate 			    "bad port\n", abuf);
5627c478bd9Sstevel@tonic-gate 		else
5637c478bd9Sstevel@tonic-gate 			syslog(LOG_NOTICE, "connection from %s (%s) - "
5647c478bd9Sstevel@tonic-gate 			    "bad port\n", hostname, abuf);
5657c478bd9Sstevel@tonic-gate 		exit(1);
5667c478bd9Sstevel@tonic-gate 	}
5677c478bd9Sstevel@tonic-gate 
5687c478bd9Sstevel@tonic-gate 	(void) alarm(60);
5697c478bd9Sstevel@tonic-gate 	port = 0;
5707c478bd9Sstevel@tonic-gate 	for (;;) {
5717c478bd9Sstevel@tonic-gate 		char c;
5727c478bd9Sstevel@tonic-gate 		if ((cc = read(f, &c, 1)) != 1) {
5737c478bd9Sstevel@tonic-gate 			if (cc < 0)
5747c478bd9Sstevel@tonic-gate 				syslog(LOG_NOTICE, "read: %m");
5757c478bd9Sstevel@tonic-gate 			(void) shutdown(f, 1+1);
5767c478bd9Sstevel@tonic-gate 			exit(1);
5777c478bd9Sstevel@tonic-gate 		}
5787c478bd9Sstevel@tonic-gate 		if (c == 0)
5797c478bd9Sstevel@tonic-gate 			break;
5807c478bd9Sstevel@tonic-gate 		port = port * 10 + c - '0';
5817c478bd9Sstevel@tonic-gate 	}
5827c478bd9Sstevel@tonic-gate 	(void) alarm(0);
5837c478bd9Sstevel@tonic-gate 	if (port != 0) {
5847c478bd9Sstevel@tonic-gate 		int lport = 0;
5857c478bd9Sstevel@tonic-gate 		struct sockaddr_storage ctl_addr;
5867c478bd9Sstevel@tonic-gate 		int addrlen;
5877c478bd9Sstevel@tonic-gate 
5887c478bd9Sstevel@tonic-gate 		(void) memset(&ctl_addr, 0, sizeof (ctl_addr));
5897c478bd9Sstevel@tonic-gate 		addrlen = sizeof (ctl_addr);
5907c478bd9Sstevel@tonic-gate 		if (getsockname(f, (struct sockaddr *)&ctl_addr,
5917c478bd9Sstevel@tonic-gate 			&addrlen) < 0) {
5927c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, "getsockname: %m");
5937c478bd9Sstevel@tonic-gate 			exit(1);
5947c478bd9Sstevel@tonic-gate 		}
5957c478bd9Sstevel@tonic-gate get_port:
5967c478bd9Sstevel@tonic-gate 		/*
5977c478bd9Sstevel@tonic-gate 		 * 0 means that rresvport_addr() will bind to a port in
5987c478bd9Sstevel@tonic-gate 		 * the anonymous priviledged port range.
5997c478bd9Sstevel@tonic-gate 		 */
6007c478bd9Sstevel@tonic-gate 		if (krb5auth_flag) {
6017c478bd9Sstevel@tonic-gate 			/*
6027c478bd9Sstevel@tonic-gate 			 * Kerberos does not support IPv6 yet.
6037c478bd9Sstevel@tonic-gate 			 */
6047c478bd9Sstevel@tonic-gate 			lport = IPPORT_RESERVED - 1;
6057c478bd9Sstevel@tonic-gate 		}
6067c478bd9Sstevel@tonic-gate 		s = rresvport_addr(&lport, &ctl_addr);
6077c478bd9Sstevel@tonic-gate 
6087c478bd9Sstevel@tonic-gate 		if (s < 0) {
6097c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, "can't get stderr port: %m");
6107c478bd9Sstevel@tonic-gate 			exit(1);
6117c478bd9Sstevel@tonic-gate 		}
6127c478bd9Sstevel@tonic-gate 		if (!krb5auth_flag && (port >= IPPORT_RESERVED)) {
6137c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, "2nd port not reserved\n");
6147c478bd9Sstevel@tonic-gate 			exit(1);
6157c478bd9Sstevel@tonic-gate 		}
6167c478bd9Sstevel@tonic-gate 		if (fromp->ss_family == AF_INET) {
6177c478bd9Sstevel@tonic-gate 			sin->sin_port = htons((ushort_t)port);
6187c478bd9Sstevel@tonic-gate 		} else if (fromp->ss_family == AF_INET6) {
6197c478bd9Sstevel@tonic-gate 			sin6->sin6_port = htons((ushort_t)port);
6207c478bd9Sstevel@tonic-gate 		}
6217c478bd9Sstevel@tonic-gate 		if (connect(s, (struct sockaddr *)fromp, fromplen) < 0) {
6227c478bd9Sstevel@tonic-gate 			if (errno == EADDRINUSE) {
6237c478bd9Sstevel@tonic-gate 				(void) close(s);
6247c478bd9Sstevel@tonic-gate 				goto get_port;
6257c478bd9Sstevel@tonic-gate 			}
6267c478bd9Sstevel@tonic-gate 			syslog(LOG_INFO, "connect second port: %m");
6277c478bd9Sstevel@tonic-gate 			exit(1);
6287c478bd9Sstevel@tonic-gate 		}
6297c478bd9Sstevel@tonic-gate 	}
6307c478bd9Sstevel@tonic-gate 	(void) dup2(f, 0);
6317c478bd9Sstevel@tonic-gate 	(void) dup2(f, 1);
6327c478bd9Sstevel@tonic-gate 	(void) dup2(f, 2);
6337c478bd9Sstevel@tonic-gate 
6347c478bd9Sstevel@tonic-gate #ifdef DEBUG
6357c478bd9Sstevel@tonic-gate 	syslog(LOG_NOTICE, "rshd: Client hostname = %s", hostname);
6367c478bd9Sstevel@tonic-gate 	if (debug_port)
6377c478bd9Sstevel@tonic-gate 		syslog(LOG_NOTICE, "rshd: Debug port is %d", debug_port);
6387c478bd9Sstevel@tonic-gate 	if (krb5auth_flag > 0)
6397c478bd9Sstevel@tonic-gate 		syslog(LOG_NOTICE, "rshd: Kerberos mode is ON");
6407c478bd9Sstevel@tonic-gate 	else
6417c478bd9Sstevel@tonic-gate 		syslog(LOG_NOTICE, "rshd: Kerberos mode is OFF");
6427c478bd9Sstevel@tonic-gate #endif /* DEBUG */
6437c478bd9Sstevel@tonic-gate 
6447c478bd9Sstevel@tonic-gate 	if (krb5auth_flag > 0) {
6457c478bd9Sstevel@tonic-gate 		if ((status = recvauth(f, &valid_checksum))) {
6467c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, gettext("Kerberos Authentication "
6477c478bd9Sstevel@tonic-gate 					"failed \n"));
6487c478bd9Sstevel@tonic-gate 			error("Authentication failed: %s\n",
6497c478bd9Sstevel@tonic-gate 					error_message(status));
6507c478bd9Sstevel@tonic-gate 			(void) audit_rshd_fail("Kerberos Authentication "
6517c478bd9Sstevel@tonic-gate 				"failed", hostname, remuser, locuser, cmdbuf);
6527c478bd9Sstevel@tonic-gate 			exit(1);
6537c478bd9Sstevel@tonic-gate 		}
6547c478bd9Sstevel@tonic-gate 
6557c478bd9Sstevel@tonic-gate 		if (checksum_required && !valid_checksum &&
6567c478bd9Sstevel@tonic-gate 			kcmd_protocol == KCMD_OLD_PROTOCOL) {
6577c478bd9Sstevel@tonic-gate 			syslog(LOG_WARNING, "Client did not supply required"
6587c478bd9Sstevel@tonic-gate 					" checksum--connection rejected.");
6597c478bd9Sstevel@tonic-gate 			error("Client did not supply required"
6607c478bd9Sstevel@tonic-gate 				"checksum--connection rejected.\n");
6617c478bd9Sstevel@tonic-gate 			(void) audit_rshd_fail("Client did not supply required"
6627c478bd9Sstevel@tonic-gate 				" checksum--connection rejected.", hostname,
6637c478bd9Sstevel@tonic-gate 				remuser, locuser, cmdbuf);	/* BSM */
6647c478bd9Sstevel@tonic-gate 			goto signout;
6657c478bd9Sstevel@tonic-gate 		}
6667c478bd9Sstevel@tonic-gate 
6677c478bd9Sstevel@tonic-gate 		/*
6687c478bd9Sstevel@tonic-gate 		 * Authentication has succeeded, we now need
6697c478bd9Sstevel@tonic-gate 		 * to check authorization.
6707c478bd9Sstevel@tonic-gate 		 *
6717c478bd9Sstevel@tonic-gate 		 * krb5_kuserok returns 1 if OK.
6727c478bd9Sstevel@tonic-gate 		 */
6737c478bd9Sstevel@tonic-gate 		if (client && krb5_kuserok(bsd_context, client, locuser)) {
6747c478bd9Sstevel@tonic-gate 			auth_sent |= AUTH_KRB5;
6757c478bd9Sstevel@tonic-gate 		} else {
6767c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, "Principal %s (%s@%s) for local user "
6777c478bd9Sstevel@tonic-gate 				"%s failed krb5_kuserok.\n",
6787c478bd9Sstevel@tonic-gate 				kremuser, remuser, hostname, locuser);
6797c478bd9Sstevel@tonic-gate 		}
6807c478bd9Sstevel@tonic-gate 	} else {
6817c478bd9Sstevel@tonic-gate 		getstr(netf, remuser, sizeof (remuser), "remuser");
6827c478bd9Sstevel@tonic-gate 		getstr(netf, locuser, sizeof (locuser), "locuser");
6837c478bd9Sstevel@tonic-gate 		getstr(netf, cmdbuf, sizeof (cmdbuf), "command");
6847c478bd9Sstevel@tonic-gate 	}
6857c478bd9Sstevel@tonic-gate 
6867c478bd9Sstevel@tonic-gate #ifdef DEBUG
6877c478bd9Sstevel@tonic-gate 	syslog(LOG_NOTICE, "rshd: locuser = %s, remuser = %s, cmdbuf = %s",
6887c478bd9Sstevel@tonic-gate 			locuser, remuser, cmdbuf);
6897c478bd9Sstevel@tonic-gate #endif /* DEBUG */
6907c478bd9Sstevel@tonic-gate 
6917c478bd9Sstevel@tonic-gate 	/*
6927c478bd9Sstevel@tonic-gate 	 * Note that there is no rsh conv functions at present.
6937c478bd9Sstevel@tonic-gate 	 */
6947c478bd9Sstevel@tonic-gate 	if (krb5auth_flag > 0) {
6957c478bd9Sstevel@tonic-gate 		if ((err = pam_start("krsh", locuser, NULL, &pamh))
6967c478bd9Sstevel@tonic-gate 				!= PAM_SUCCESS) {
6977c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, "pam_start() failed: %s\n",
6987c478bd9Sstevel@tonic-gate 				pam_strerror(0, err));
6997c478bd9Sstevel@tonic-gate 			exit(1);
7007c478bd9Sstevel@tonic-gate 		}
7017c478bd9Sstevel@tonic-gate 	}
7027c478bd9Sstevel@tonic-gate 	else
7037c478bd9Sstevel@tonic-gate 	{
7047c478bd9Sstevel@tonic-gate 		if ((err = pam_start("rsh", locuser, NULL, &pamh))
7057c478bd9Sstevel@tonic-gate 				!= PAM_SUCCESS) {
7067c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, "pam_start() failed: %s\n",
7077c478bd9Sstevel@tonic-gate 				pam_strerror(0, err));
7087c478bd9Sstevel@tonic-gate 			exit(1);
7097c478bd9Sstevel@tonic-gate 		}
7107c478bd9Sstevel@tonic-gate 	}
7117c478bd9Sstevel@tonic-gate 	if ((err = pam_set_item(pamh, PAM_RHOST, hostname)) != PAM_SUCCESS) {
7127c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "pam_set_item() failed: %s\n",
7137c478bd9Sstevel@tonic-gate 			pam_strerror(pamh, err));
7147c478bd9Sstevel@tonic-gate 		exit(1);
7157c478bd9Sstevel@tonic-gate 	}
7167c478bd9Sstevel@tonic-gate 	if ((err = pam_set_item(pamh, PAM_RUSER, remuser)) != PAM_SUCCESS) {
7177c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "pam_set_item() failed: %s\n",
7187c478bd9Sstevel@tonic-gate 			pam_strerror(pamh, err));
7197c478bd9Sstevel@tonic-gate 		exit(1);
7207c478bd9Sstevel@tonic-gate 	}
7217c478bd9Sstevel@tonic-gate 
7227c478bd9Sstevel@tonic-gate 	pwd = getpwnam(locuser);
7237c478bd9Sstevel@tonic-gate 	shpwd = getspnam(locuser);
7247c478bd9Sstevel@tonic-gate 	if ((pwd == NULL) || (shpwd == NULL)) {
7257c478bd9Sstevel@tonic-gate 		if (krb5auth_flag > 0)
7267c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, "Principal %s (%s@%s) for local user "
7277c478bd9Sstevel@tonic-gate 				"%s has no account.\n", kremuser, remuser,
7287c478bd9Sstevel@tonic-gate 							hostname, locuser);
7297c478bd9Sstevel@tonic-gate 		error("permission denied.\n");
7307c478bd9Sstevel@tonic-gate 		(void) audit_rshd_fail("Login incorrect", hostname,
7317c478bd9Sstevel@tonic-gate 			remuser, locuser, cmdbuf);	/* BSM */
7327c478bd9Sstevel@tonic-gate 		exit(1);
7337c478bd9Sstevel@tonic-gate 	}
7347c478bd9Sstevel@tonic-gate 
7357c478bd9Sstevel@tonic-gate 	if (krb5auth_flag > 0) {
7367c478bd9Sstevel@tonic-gate 		(void) snprintf(repository, sizeof (repository),
7377c478bd9Sstevel@tonic-gate 					KRB5_REPOSITORY_NAME);
7387c478bd9Sstevel@tonic-gate 		/*
7397c478bd9Sstevel@tonic-gate 		 * We currently only support special handling of the
7407c478bd9Sstevel@tonic-gate 		 * KRB5 PAM repository
7417c478bd9Sstevel@tonic-gate 		 */
7427c478bd9Sstevel@tonic-gate 		if (strlen(locuser) != 0) {
7437c478bd9Sstevel@tonic-gate 			krb5_repository_data_t krb5_data;
7447c478bd9Sstevel@tonic-gate 			pam_repository_t pam_rep_data;
7457c478bd9Sstevel@tonic-gate 
7467c478bd9Sstevel@tonic-gate 			krb5_data.principal = locuser;
7477c478bd9Sstevel@tonic-gate 			krb5_data.flags = SUNW_PAM_KRB5_ALREADY_AUTHENTICATED;
7487c478bd9Sstevel@tonic-gate 
7497c478bd9Sstevel@tonic-gate 			pam_rep_data.type = repository;
7507c478bd9Sstevel@tonic-gate 			pam_rep_data.scope = (void *)&krb5_data;
7517c478bd9Sstevel@tonic-gate 			pam_rep_data.scope_len = sizeof (krb5_data);
7527c478bd9Sstevel@tonic-gate 
7537c478bd9Sstevel@tonic-gate 			(void) pam_set_item(pamh, PAM_REPOSITORY,
7547c478bd9Sstevel@tonic-gate 					(void *)&pam_rep_data);
7557c478bd9Sstevel@tonic-gate 		}
7567c478bd9Sstevel@tonic-gate 	}
7577c478bd9Sstevel@tonic-gate 
7587c478bd9Sstevel@tonic-gate 	/*
7597c478bd9Sstevel@tonic-gate 	 * maintain 2.1 and 4.* and BSD semantics with anonymous rshd
7607c478bd9Sstevel@tonic-gate 	 */
7617c478bd9Sstevel@tonic-gate 	if (shpwd->sp_pwdp != 0 && *shpwd->sp_pwdp != '\0' &&
7627c478bd9Sstevel@tonic-gate 	    (v = pam_authenticate(pamh, 0)) != PAM_SUCCESS) {
7637c478bd9Sstevel@tonic-gate 		error("permission denied\n");
7647c478bd9Sstevel@tonic-gate 		(void) audit_rshd_fail("Permission denied", hostname,
7657c478bd9Sstevel@tonic-gate 			remuser, locuser, cmdbuf);	/* BSM */
7667c478bd9Sstevel@tonic-gate 		(void) pam_end(pamh, v);
7677c478bd9Sstevel@tonic-gate 		exit(1);
7687c478bd9Sstevel@tonic-gate 	}
7697c478bd9Sstevel@tonic-gate 
7707c478bd9Sstevel@tonic-gate 	if (krb5auth_flag > 0) {
7717c478bd9Sstevel@tonic-gate 		if (require_encrypt && (!do_encrypt)) {
7727c478bd9Sstevel@tonic-gate 			error("You must use encryption.\n");
7737c478bd9Sstevel@tonic-gate 			(void) audit_rshd_fail("You must use encryption.",
7747c478bd9Sstevel@tonic-gate 				hostname, remuser, locuser, cmdbuf); /* BSM */
7757c478bd9Sstevel@tonic-gate 			goto signout;
7767c478bd9Sstevel@tonic-gate 		}
7777c478bd9Sstevel@tonic-gate 
7787c478bd9Sstevel@tonic-gate 		if (!(auth_ok & auth_sent)) {
7797c478bd9Sstevel@tonic-gate 			if (auth_sent) {
7807c478bd9Sstevel@tonic-gate 				error("Another authentication mechanism "
7817c478bd9Sstevel@tonic-gate 				    "must be used to access this host.\n");
7827c478bd9Sstevel@tonic-gate 				(void) audit_rshd_fail("Another authentication"
7837c478bd9Sstevel@tonic-gate 					" mechanism must be used to access"
7847c478bd9Sstevel@tonic-gate 					" this host.\n", hostname, remuser,
7857c478bd9Sstevel@tonic-gate 					locuser, cmdbuf); /* BSM */
7867c478bd9Sstevel@tonic-gate 				goto signout;
7877c478bd9Sstevel@tonic-gate 			} else {
7887c478bd9Sstevel@tonic-gate 				error("Permission denied.\n");
7897c478bd9Sstevel@tonic-gate 				(void) audit_rshd_fail("Permission denied.",
7907c478bd9Sstevel@tonic-gate 					hostname, remuser, locuser, cmdbuf);
7917c478bd9Sstevel@tonic-gate 					/* BSM */
7927c478bd9Sstevel@tonic-gate 				goto signout;
7937c478bd9Sstevel@tonic-gate 			}
7947c478bd9Sstevel@tonic-gate 		}
7957c478bd9Sstevel@tonic-gate 
7967c478bd9Sstevel@tonic-gate 
7977c478bd9Sstevel@tonic-gate 		if (pwd->pw_uid && !access("/etc/nologin", F_OK)) {
7987c478bd9Sstevel@tonic-gate 			error("Logins currently disabled.\n");
7997c478bd9Sstevel@tonic-gate 			(void) audit_rshd_fail("Logins currently disabled.",
8007c478bd9Sstevel@tonic-gate 				hostname, remuser, locuser, cmdbuf);
8017c478bd9Sstevel@tonic-gate 			goto signout;
8027c478bd9Sstevel@tonic-gate 		}
8037c478bd9Sstevel@tonic-gate 
8047c478bd9Sstevel@tonic-gate 		/* Log access to account */
8057c478bd9Sstevel@tonic-gate 		if (pwd && (pwd->pw_uid == 0)) {
8067c478bd9Sstevel@tonic-gate 			syslog(LOG_NOTICE, "Executing %s for user %s (%s@%s)"
8077c478bd9Sstevel@tonic-gate 			    " as ROOT", cmdbuf,
8087c478bd9Sstevel@tonic-gate 			    kremuser, remuser, hostname);
8097c478bd9Sstevel@tonic-gate 		}
8107c478bd9Sstevel@tonic-gate 	}
8117c478bd9Sstevel@tonic-gate 
8127c478bd9Sstevel@tonic-gate 	if ((v = pam_acct_mgmt(pamh, 0)) != PAM_SUCCESS) {
8137c478bd9Sstevel@tonic-gate 		switch (v) {
8147c478bd9Sstevel@tonic-gate 		case PAM_NEW_AUTHTOK_REQD:
8157c478bd9Sstevel@tonic-gate 			error("password expired\n");
8167c478bd9Sstevel@tonic-gate 			(void) audit_rshd_fail("Password expired", hostname,
8177c478bd9Sstevel@tonic-gate 				remuser, locuser, cmdbuf); /* BSM */
8187c478bd9Sstevel@tonic-gate 			break;
8197c478bd9Sstevel@tonic-gate 		case PAM_PERM_DENIED:
8207c478bd9Sstevel@tonic-gate 			error("account expired\n");
8217c478bd9Sstevel@tonic-gate 			(void) audit_rshd_fail("Account expired", hostname,
8227c478bd9Sstevel@tonic-gate 				remuser, locuser, cmdbuf); /* BSM */
8237c478bd9Sstevel@tonic-gate 			break;
8247c478bd9Sstevel@tonic-gate 		case PAM_AUTHTOK_EXPIRED:
8257c478bd9Sstevel@tonic-gate 			error("password expired\n");
8267c478bd9Sstevel@tonic-gate 			(void) audit_rshd_fail("Password expired", hostname,
8277c478bd9Sstevel@tonic-gate 				remuser, locuser, cmdbuf); /* BSM */
8287c478bd9Sstevel@tonic-gate 			break;
8297c478bd9Sstevel@tonic-gate 		default:
8307c478bd9Sstevel@tonic-gate 			error("login incorrect\n");
8317c478bd9Sstevel@tonic-gate 			(void) audit_rshd_fail("Permission denied", hostname,
8327c478bd9Sstevel@tonic-gate 				remuser, locuser, cmdbuf); /* BSM */
8337c478bd9Sstevel@tonic-gate 			break;
8347c478bd9Sstevel@tonic-gate 		}
8357c478bd9Sstevel@tonic-gate 		(void) pam_end(pamh, PAM_ABORT);
8367c478bd9Sstevel@tonic-gate 		exit(1);
8377c478bd9Sstevel@tonic-gate 	}
8387c478bd9Sstevel@tonic-gate 
8397c478bd9Sstevel@tonic-gate 	if (chdir(pwd->pw_dir) < 0) {
8407c478bd9Sstevel@tonic-gate 		(void) chdir("/");
8417c478bd9Sstevel@tonic-gate #ifdef notdef
8427c478bd9Sstevel@tonic-gate 		error("No remote directory.\n");
8437c478bd9Sstevel@tonic-gate 
8447c478bd9Sstevel@tonic-gate 		exit(1);
8457c478bd9Sstevel@tonic-gate #endif
8467c478bd9Sstevel@tonic-gate 	}
8477c478bd9Sstevel@tonic-gate 
8487c478bd9Sstevel@tonic-gate 	/*
8497c478bd9Sstevel@tonic-gate 	 * XXX There is no session management currently being done
8507c478bd9Sstevel@tonic-gate 	 */
8517c478bd9Sstevel@tonic-gate 
8527c478bd9Sstevel@tonic-gate 	(void) write(STDERR_FILENO, "\0", 1);
8537c478bd9Sstevel@tonic-gate 	if (port || do_encrypt) {
8547c478bd9Sstevel@tonic-gate 		if ((pipe(pv) < 0)) {
8557c478bd9Sstevel@tonic-gate 			error("Can't make pipe.\n");
8567c478bd9Sstevel@tonic-gate 			(void) pam_end(pamh, PAM_ABORT);
8577c478bd9Sstevel@tonic-gate 			exit(1);
8587c478bd9Sstevel@tonic-gate 		}
8597c478bd9Sstevel@tonic-gate 		if (do_encrypt) {
8607c478bd9Sstevel@tonic-gate 			if (pipe(pw) < 0) {
8617c478bd9Sstevel@tonic-gate 				error("Can't make pipe 2.\n");
8627c478bd9Sstevel@tonic-gate 				(void) pam_end(pamh, PAM_ABORT);
8637c478bd9Sstevel@tonic-gate 				exit(1);
8647c478bd9Sstevel@tonic-gate 			}
8657c478bd9Sstevel@tonic-gate 			if (pipe(px) < 0) {
8667c478bd9Sstevel@tonic-gate 				error("Can't make pipe 3.\n");
8677c478bd9Sstevel@tonic-gate 				(void) pam_end(pamh, PAM_ABORT);
8687c478bd9Sstevel@tonic-gate 				exit(1);
8697c478bd9Sstevel@tonic-gate 			}
8707c478bd9Sstevel@tonic-gate 		}
8717c478bd9Sstevel@tonic-gate 		pid = fork();
8727c478bd9Sstevel@tonic-gate 		if (pid == (pid_t)-1)  {
8737c478bd9Sstevel@tonic-gate 			error("Fork (to start shell) failed on server.  "
8747c478bd9Sstevel@tonic-gate 				"Please try again later.\n");
8757c478bd9Sstevel@tonic-gate 			(void) pam_end(pamh, PAM_ABORT);
8767c478bd9Sstevel@tonic-gate 			exit(1);
8777c478bd9Sstevel@tonic-gate 		}
8787c478bd9Sstevel@tonic-gate 		if (pid) {
8797c478bd9Sstevel@tonic-gate 			fd_set ready;
8807c478bd9Sstevel@tonic-gate 			fd_set readfrom;
8817c478bd9Sstevel@tonic-gate 
8827c478bd9Sstevel@tonic-gate 			(void) close(STDIN_FILENO);
8837c478bd9Sstevel@tonic-gate 			(void) close(STDOUT_FILENO);
8847c478bd9Sstevel@tonic-gate 			(void) close(STDERR_FILENO);
8857c478bd9Sstevel@tonic-gate 			(void) close(pv[1]);
8867c478bd9Sstevel@tonic-gate 			if (do_encrypt) {
8877c478bd9Sstevel@tonic-gate 				(void) close(pw[1]);
8887c478bd9Sstevel@tonic-gate 				(void) close(px[0]);
8897c478bd9Sstevel@tonic-gate 			} else {
8907c478bd9Sstevel@tonic-gate 				(void) close(f);
8917c478bd9Sstevel@tonic-gate 			}
8927c478bd9Sstevel@tonic-gate 
8937c478bd9Sstevel@tonic-gate 			(void) FD_ZERO(&readfrom);
8947c478bd9Sstevel@tonic-gate 
8957c478bd9Sstevel@tonic-gate 			FD_SET(pv[0], &readfrom);
8967c478bd9Sstevel@tonic-gate 			if (do_encrypt) {
8977c478bd9Sstevel@tonic-gate 				FD_SET(pw[0], &readfrom);
8987c478bd9Sstevel@tonic-gate 				FD_SET(f, &readfrom);
8997c478bd9Sstevel@tonic-gate 			}
9007c478bd9Sstevel@tonic-gate 			if (port)
9017c478bd9Sstevel@tonic-gate 				FD_SET(s, &readfrom);
9027c478bd9Sstevel@tonic-gate 
9037c478bd9Sstevel@tonic-gate 			/* read f (net), write to px[1] (child stdin) */
9047c478bd9Sstevel@tonic-gate 			/* read pw[0] (child stdout), write to f (net) */
9057c478bd9Sstevel@tonic-gate 			/* read s (alt. channel), signal child */
9067c478bd9Sstevel@tonic-gate 			/* read pv[0] (child stderr), write to s */
9077c478bd9Sstevel@tonic-gate 			if (ioctl(pv[0], FIONBIO, (char *)&one) == -1)
9087c478bd9Sstevel@tonic-gate 				syslog(LOG_INFO, "ioctl FIONBIO: %m");
9097c478bd9Sstevel@tonic-gate 			if (do_encrypt &&
9107c478bd9Sstevel@tonic-gate 				ioctl(pw[0], FIONBIO, (char *)&one) == -1)
9117c478bd9Sstevel@tonic-gate 				syslog(LOG_INFO, "ioctl FIONBIO: %m");
9127c478bd9Sstevel@tonic-gate 			do {
9137c478bd9Sstevel@tonic-gate 				ready = readfrom;
9147c478bd9Sstevel@tonic-gate 				if (select(FD_SETSIZE, &ready, NULL,
9157c478bd9Sstevel@tonic-gate 					NULL, NULL) < 0) {
9167c478bd9Sstevel@tonic-gate 					if (errno == EINTR) {
9177c478bd9Sstevel@tonic-gate 						continue;
9187c478bd9Sstevel@tonic-gate 					} else {
9197c478bd9Sstevel@tonic-gate 						break;
9207c478bd9Sstevel@tonic-gate 					}
9217c478bd9Sstevel@tonic-gate 				}
9227c478bd9Sstevel@tonic-gate 				/*
9237c478bd9Sstevel@tonic-gate 				 * Read from child stderr, write to net
9247c478bd9Sstevel@tonic-gate 				 */
9257c478bd9Sstevel@tonic-gate 				if (port && FD_ISSET(pv[0], &ready)) {
9267c478bd9Sstevel@tonic-gate 					errno = 0;
9277c478bd9Sstevel@tonic-gate 					cc = read(pv[0], buf, sizeof (buf));
9287c478bd9Sstevel@tonic-gate 					if (cc <= 0) {
9297c478bd9Sstevel@tonic-gate 						(void) shutdown(s, 2);
9307c478bd9Sstevel@tonic-gate 						FD_CLR(pv[0], &readfrom);
9317c478bd9Sstevel@tonic-gate 					} else {
9327c478bd9Sstevel@tonic-gate 						(void) deswrite(s, buf, cc, 1);
9337c478bd9Sstevel@tonic-gate 					}
9347c478bd9Sstevel@tonic-gate 				}
9357c478bd9Sstevel@tonic-gate 				/*
9367c478bd9Sstevel@tonic-gate 				 * Read from alternate channel, signal child
9377c478bd9Sstevel@tonic-gate 				 */
9387c478bd9Sstevel@tonic-gate 				if (port && FD_ISSET(s, &ready)) {
9397c478bd9Sstevel@tonic-gate 					if ((int)desread(s, &sig, 1, 1) <= 0)
9407c478bd9Sstevel@tonic-gate 						FD_CLR(s, &readfrom);
9417c478bd9Sstevel@tonic-gate 					else
9427c478bd9Sstevel@tonic-gate 						(void) killpg(pid, sig);
9437c478bd9Sstevel@tonic-gate 				}
9447c478bd9Sstevel@tonic-gate 				/*
9457c478bd9Sstevel@tonic-gate 				 * Read from child stdout, write to net
9467c478bd9Sstevel@tonic-gate 				 */
9477c478bd9Sstevel@tonic-gate 				if (do_encrypt && FD_ISSET(pw[0], &ready)) {
9487c478bd9Sstevel@tonic-gate 					errno = 0;
9497c478bd9Sstevel@tonic-gate 					cc = read(pw[0], buf, sizeof (buf));
9507c478bd9Sstevel@tonic-gate 					if (cc <= 0) {
9517c478bd9Sstevel@tonic-gate 						(void) shutdown(f, 2);
9527c478bd9Sstevel@tonic-gate 						FD_CLR(pw[0], &readfrom);
9537c478bd9Sstevel@tonic-gate 					} else {
9547c478bd9Sstevel@tonic-gate 						(void) deswrite(f, buf, cc, 0);
9557c478bd9Sstevel@tonic-gate 					}
9567c478bd9Sstevel@tonic-gate 				}
9577c478bd9Sstevel@tonic-gate 				/*
9587c478bd9Sstevel@tonic-gate 				 * Read from the net, write to child stdin
9597c478bd9Sstevel@tonic-gate 				 */
9607c478bd9Sstevel@tonic-gate 				if (do_encrypt && FD_ISSET(f, &ready)) {
9617c478bd9Sstevel@tonic-gate 					errno = 0;
9627c478bd9Sstevel@tonic-gate 					cc = desread(f, buf, sizeof (buf), 0);
9637c478bd9Sstevel@tonic-gate 					if (cc <= 0) {
9647c478bd9Sstevel@tonic-gate 						(void) close(px[1]);
9657c478bd9Sstevel@tonic-gate 						FD_CLR(f, &readfrom);
9667c478bd9Sstevel@tonic-gate 					} else {
9677c478bd9Sstevel@tonic-gate 						int wcc;
9687c478bd9Sstevel@tonic-gate 						wcc = write(px[1], buf, cc);
9697c478bd9Sstevel@tonic-gate 						if (wcc == -1) {
9707c478bd9Sstevel@tonic-gate 							/*
9717c478bd9Sstevel@tonic-gate 							 * pipe closed,
9727c478bd9Sstevel@tonic-gate 							 * don't read any
9737c478bd9Sstevel@tonic-gate 							 * more
9747c478bd9Sstevel@tonic-gate 							 *
9757c478bd9Sstevel@tonic-gate 							 * might check for
9767c478bd9Sstevel@tonic-gate 							 * EPIPE
9777c478bd9Sstevel@tonic-gate 							 */
9787c478bd9Sstevel@tonic-gate 						    (void) close(px[1]);
9797c478bd9Sstevel@tonic-gate 						    FD_CLR(f, &readfrom);
9807c478bd9Sstevel@tonic-gate 						} else if (wcc != cc) {
9817c478bd9Sstevel@tonic-gate 						    /* CSTYLED */
9827c478bd9Sstevel@tonic-gate 						    syslog(LOG_INFO, gettext("only wrote %d/%d to child"),
9837c478bd9Sstevel@tonic-gate 						    wcc, cc);
9847c478bd9Sstevel@tonic-gate 						}
9857c478bd9Sstevel@tonic-gate 					}
9867c478bd9Sstevel@tonic-gate 				}
9877c478bd9Sstevel@tonic-gate 			} while ((port && FD_ISSET(s, &readfrom)) ||
9887c478bd9Sstevel@tonic-gate 				(port && FD_ISSET(pv[0], &readfrom)) ||
9897c478bd9Sstevel@tonic-gate 				(do_encrypt && FD_ISSET(f, &readfrom)) ||
9907c478bd9Sstevel@tonic-gate 				(do_encrypt && FD_ISSET(pw[0], &readfrom)));
9917c478bd9Sstevel@tonic-gate #ifdef DEBUG
9927c478bd9Sstevel@tonic-gate 			syslog(LOG_INFO, "Shell process completed.");
9937c478bd9Sstevel@tonic-gate #endif /* DEBUG */
9947c478bd9Sstevel@tonic-gate 			if (ccache)
9957c478bd9Sstevel@tonic-gate 				(void) pam_close_session(pamh, 0);
9967c478bd9Sstevel@tonic-gate 			(void) pam_end(pamh, PAM_SUCCESS);
9977c478bd9Sstevel@tonic-gate 
9987c478bd9Sstevel@tonic-gate 			exit(0);
9997c478bd9Sstevel@tonic-gate 		} /* End of Parent block */
10007c478bd9Sstevel@tonic-gate 
10017c478bd9Sstevel@tonic-gate 		(void) setsid();	/* Should be the same as above. */
10027c478bd9Sstevel@tonic-gate 		(void) close(pv[0]);
10037c478bd9Sstevel@tonic-gate 		(void) dup2(pv[1], 2);
10047c478bd9Sstevel@tonic-gate 		(void) close(pv[1]);
10057c478bd9Sstevel@tonic-gate 		if (port)
10067c478bd9Sstevel@tonic-gate 			(void) close(s);
10077c478bd9Sstevel@tonic-gate 		if (do_encrypt) {
10087c478bd9Sstevel@tonic-gate 			(void) close(f);
10097c478bd9Sstevel@tonic-gate 			(void) close(pw[0]);
10107c478bd9Sstevel@tonic-gate 			(void) close(px[1]);
10117c478bd9Sstevel@tonic-gate 
10127c478bd9Sstevel@tonic-gate 			(void) dup2(px[0], 0);
10137c478bd9Sstevel@tonic-gate 			(void) dup2(pw[1], 1);
10147c478bd9Sstevel@tonic-gate 
10157c478bd9Sstevel@tonic-gate 			(void) close(px[0]);
10167c478bd9Sstevel@tonic-gate 			(void) close(pw[1]);
10177c478bd9Sstevel@tonic-gate 		}
10187c478bd9Sstevel@tonic-gate 	}
10197c478bd9Sstevel@tonic-gate 
10207c478bd9Sstevel@tonic-gate 	if (*pwd->pw_shell == '\0')
10217c478bd9Sstevel@tonic-gate 		pwd->pw_shell = "/bin/sh";
10227c478bd9Sstevel@tonic-gate 	if (!do_encrypt)
10237c478bd9Sstevel@tonic-gate 		(void) close(f);
10247c478bd9Sstevel@tonic-gate 	/*
10257c478bd9Sstevel@tonic-gate 	 * write audit record before making uid switch
10267c478bd9Sstevel@tonic-gate 	 */
10277c478bd9Sstevel@tonic-gate 	(void) audit_rshd_success(hostname, remuser, locuser, cmdbuf); /* BSM */
10287c478bd9Sstevel@tonic-gate 
10297c478bd9Sstevel@tonic-gate 	/* set the real (and effective) GID */
10307c478bd9Sstevel@tonic-gate 	if (setgid(pwd->pw_gid) == -1) {
10317c478bd9Sstevel@tonic-gate 		error("Invalid gid.\n");
10327c478bd9Sstevel@tonic-gate 		(void) pam_end(pamh, PAM_ABORT);
10337c478bd9Sstevel@tonic-gate 		exit(1);
10347c478bd9Sstevel@tonic-gate 	}
10357c478bd9Sstevel@tonic-gate 
10367c478bd9Sstevel@tonic-gate 	/*
10377c478bd9Sstevel@tonic-gate 	 * Initialize the supplementary group access list.
10387c478bd9Sstevel@tonic-gate 	 */
10397c478bd9Sstevel@tonic-gate 	if (strlen(locuser) == 0) {
10407c478bd9Sstevel@tonic-gate 		error("No local user.\n");
10417c478bd9Sstevel@tonic-gate 		(void) pam_end(pamh, PAM_ABORT);
10427c478bd9Sstevel@tonic-gate 		exit(1);
10437c478bd9Sstevel@tonic-gate 	}
10447c478bd9Sstevel@tonic-gate 	if (initgroups(locuser, pwd->pw_gid) == -1) {
10457c478bd9Sstevel@tonic-gate 		error("Initgroup failed.\n");
10467c478bd9Sstevel@tonic-gate 		(void) pam_end(pamh, PAM_ABORT);
10477c478bd9Sstevel@tonic-gate 		exit(1);
10487c478bd9Sstevel@tonic-gate 	}
10497c478bd9Sstevel@tonic-gate 
10507c478bd9Sstevel@tonic-gate 	if ((v = pam_setcred(pamh, PAM_ESTABLISH_CRED)) != PAM_SUCCESS) {
10517c478bd9Sstevel@tonic-gate 		error("Insufficient credentials.\n");
10527c478bd9Sstevel@tonic-gate 		(void) pam_end(pamh, v);
10537c478bd9Sstevel@tonic-gate 		exit(1);
10547c478bd9Sstevel@tonic-gate 	}
10557c478bd9Sstevel@tonic-gate 
10567c478bd9Sstevel@tonic-gate 	/* set the real (and effective) UID */
10577c478bd9Sstevel@tonic-gate 	if (setuid(pwd->pw_uid) == -1) {
10587c478bd9Sstevel@tonic-gate 		error("Invalid uid.\n");
10597c478bd9Sstevel@tonic-gate 		(void) pam_end(pamh, PAM_ABORT);
10607c478bd9Sstevel@tonic-gate 		exit(1);
10617c478bd9Sstevel@tonic-gate 	}
10627c478bd9Sstevel@tonic-gate 
10637c478bd9Sstevel@tonic-gate 	/* Change directory only after becoming the appropriate user. */
10647c478bd9Sstevel@tonic-gate 	if (chdir(pwd->pw_dir) < 0) {
10657c478bd9Sstevel@tonic-gate 		(void) chdir("/");
10667c478bd9Sstevel@tonic-gate 		if (krb5auth_flag > 0) {
10677c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, "Principal %s  (%s@%s) for local user"
10687c478bd9Sstevel@tonic-gate 				" %s has no home directory.",
10697c478bd9Sstevel@tonic-gate 				kremuser, remuser, hostname, locuser);
10707c478bd9Sstevel@tonic-gate 			error("No remote directory.\n");
10717c478bd9Sstevel@tonic-gate 			goto signout;
10727c478bd9Sstevel@tonic-gate 		}
10737c478bd9Sstevel@tonic-gate #ifdef notdef
10747c478bd9Sstevel@tonic-gate 		error("No remote directory.\n");
10757c478bd9Sstevel@tonic-gate 		exit(1);
10767c478bd9Sstevel@tonic-gate #endif
10777c478bd9Sstevel@tonic-gate 	}
10787c478bd9Sstevel@tonic-gate 
10797c478bd9Sstevel@tonic-gate 	path = (pwd->pw_uid == 0) ? rootpath : userpath;
10807c478bd9Sstevel@tonic-gate 
10817c478bd9Sstevel@tonic-gate 	/*
10827c478bd9Sstevel@tonic-gate 	 * Space for the following environment variables are dynamically
10837c478bd9Sstevel@tonic-gate 	 * allocated because their lengths are not known before calling
10847c478bd9Sstevel@tonic-gate 	 * getpwnam().
10857c478bd9Sstevel@tonic-gate 	 */
10867c478bd9Sstevel@tonic-gate 	homedir_len = strlen(pwd->pw_dir) + strlen(homestr) + 1;
10877c478bd9Sstevel@tonic-gate 	shell_len = strlen(pwd->pw_shell) + strlen(shellstr) + 1;
10887c478bd9Sstevel@tonic-gate 	username_len = strlen(pwd->pw_name) + strlen(userstr) + 1;
10897c478bd9Sstevel@tonic-gate 	homedir = (char *)malloc(homedir_len);
10907c478bd9Sstevel@tonic-gate 	shell = (char *)malloc(shell_len);
10917c478bd9Sstevel@tonic-gate 	username = (char *)malloc(username_len);
10927c478bd9Sstevel@tonic-gate 	if (homedir == NULL || shell == NULL || username == NULL) {
10937c478bd9Sstevel@tonic-gate 		perror("malloc");
10947c478bd9Sstevel@tonic-gate 		exit(1);
10957c478bd9Sstevel@tonic-gate 	}
10967c478bd9Sstevel@tonic-gate 	(void) snprintf(homedir, homedir_len, "%s%s", homestr, pwd->pw_dir);
10977c478bd9Sstevel@tonic-gate 	(void) snprintf(shell, shell_len, "%s%s", shellstr, pwd->pw_shell);
10987c478bd9Sstevel@tonic-gate 	(void) snprintf(username, username_len, "%s%s", userstr, pwd->pw_name);
10997c478bd9Sstevel@tonic-gate 
11007c478bd9Sstevel@tonic-gate 	/* Pass timezone to executed command. */
11017c478bd9Sstevel@tonic-gate 	if (tzenv = getenv("TZ")) {
11027c478bd9Sstevel@tonic-gate 		tz_len = strlen(tzenv) + strlen(tzstr) + 1;
11037c478bd9Sstevel@tonic-gate 		tz = malloc(tz_len);
11047c478bd9Sstevel@tonic-gate 		if (tz != NULL)
11057c478bd9Sstevel@tonic-gate 			(void) snprintf(tz, tz_len, "%s%s", tzstr, tzenv);
11067c478bd9Sstevel@tonic-gate 	}
11077c478bd9Sstevel@tonic-gate 
11087c478bd9Sstevel@tonic-gate 	add_to_envinit(homedir);
11097c478bd9Sstevel@tonic-gate 	add_to_envinit(shell);
11107c478bd9Sstevel@tonic-gate 	add_to_envinit(path);
11117c478bd9Sstevel@tonic-gate 	add_to_envinit(username);
11127c478bd9Sstevel@tonic-gate 	add_to_envinit(tz);
11137c478bd9Sstevel@tonic-gate 
11147c478bd9Sstevel@tonic-gate 	if (krb5auth_flag > 0) {
11157c478bd9Sstevel@tonic-gate 		int length;
11167c478bd9Sstevel@tonic-gate 		char *buffer;
11177c478bd9Sstevel@tonic-gate 
11187c478bd9Sstevel@tonic-gate 		/*
11197c478bd9Sstevel@tonic-gate 		 * If we have KRB5CCNAME set, then copy into the child's
11207c478bd9Sstevel@tonic-gate 		 * environment.  This can't really have a fixed position
11217c478bd9Sstevel@tonic-gate 		 * because `tz' may or may not be set.
11227c478bd9Sstevel@tonic-gate 		 */
11237c478bd9Sstevel@tonic-gate 		if (getenv("KRB5CCNAME")) {
11247c478bd9Sstevel@tonic-gate 			length = (int)strlen(getenv("KRB5CCNAME")) +
11257c478bd9Sstevel@tonic-gate 					(int)strlen("KRB5CCNAME=") + 1;
11267c478bd9Sstevel@tonic-gate 			buffer = (char *)malloc(length);
11277c478bd9Sstevel@tonic-gate 
11287c478bd9Sstevel@tonic-gate 			if (buffer) {
11297c478bd9Sstevel@tonic-gate 				(void) snprintf(buffer, length, "KRB5CCNAME=%s",
11307c478bd9Sstevel@tonic-gate 						getenv("KRB5CCNAME"));
11317c478bd9Sstevel@tonic-gate 				add_to_envinit(buffer);
11327c478bd9Sstevel@tonic-gate 			}
11337c478bd9Sstevel@tonic-gate 		} {
11347c478bd9Sstevel@tonic-gate 			/* These two are covered by ADDRPAD */
11357c478bd9Sstevel@tonic-gate 			length = strlen(inet_ntoa(localaddr.sin_addr)) + 1 +
11367c478bd9Sstevel@tonic-gate 					strlen("KRB5LOCALADDR=");
11377c478bd9Sstevel@tonic-gate 			(void) snprintf(local_addr, length, "KRB5LOCALADDR=%s",
11387c478bd9Sstevel@tonic-gate 				inet_ntoa(localaddr.sin_addr));
11397c478bd9Sstevel@tonic-gate 			add_to_envinit(local_addr);
11407c478bd9Sstevel@tonic-gate 
11417c478bd9Sstevel@tonic-gate 			length = strlen(inet_ntoa(sin->sin_addr)) + 1 +
11427c478bd9Sstevel@tonic-gate 					strlen("KRB5REMOTEADDR=");
11437c478bd9Sstevel@tonic-gate 			(void) snprintf(remote_addr, length,
11447c478bd9Sstevel@tonic-gate 				"KRB5REMOTEADDR=%s", inet_ntoa(sin->sin_addr));
11457c478bd9Sstevel@tonic-gate 			add_to_envinit(remote_addr);
11467c478bd9Sstevel@tonic-gate 		}
11477c478bd9Sstevel@tonic-gate 
11487c478bd9Sstevel@tonic-gate 		/*
11497c478bd9Sstevel@tonic-gate 		 * If we do anything else, make sure there is
11507c478bd9Sstevel@tonic-gate 		 * space in the array.
11517c478bd9Sstevel@tonic-gate 		 */
11527c478bd9Sstevel@tonic-gate 		for (cnt = 0; cnt < num_env; cnt++) {
11537c478bd9Sstevel@tonic-gate 			char *buf;
11547c478bd9Sstevel@tonic-gate 
11557c478bd9Sstevel@tonic-gate 			if (getenv(save_env[cnt])) {
11567c478bd9Sstevel@tonic-gate 				length = (int)strlen(getenv(save_env[cnt])) +
11577c478bd9Sstevel@tonic-gate 					(int)strlen(save_env[cnt]) + 2;
11587c478bd9Sstevel@tonic-gate 
11597c478bd9Sstevel@tonic-gate 				buf = (char *)malloc(length);
11607c478bd9Sstevel@tonic-gate 				if (buf) {
11617c478bd9Sstevel@tonic-gate 					(void) snprintf(buf, length, "%s=%s",
11627c478bd9Sstevel@tonic-gate 						save_env[cnt],
11637c478bd9Sstevel@tonic-gate 						getenv(save_env[cnt]));
11647c478bd9Sstevel@tonic-gate 					add_to_envinit(buf);
11657c478bd9Sstevel@tonic-gate 				}
11667c478bd9Sstevel@tonic-gate 			}
11677c478bd9Sstevel@tonic-gate 		}
11687c478bd9Sstevel@tonic-gate 
11697c478bd9Sstevel@tonic-gate 	}
11707c478bd9Sstevel@tonic-gate 
11717c478bd9Sstevel@tonic-gate 	/*
11727c478bd9Sstevel@tonic-gate 	 * add PAM environment variables set by modules
11737c478bd9Sstevel@tonic-gate 	 * -- only allowed 16 (PAM_ENV_ELIM)
11747c478bd9Sstevel@tonic-gate 	 * -- check to see if the environment variable is legal
11757c478bd9Sstevel@tonic-gate 	 */
11767c478bd9Sstevel@tonic-gate 	if ((pam_env = pam_getenvlist(pamh)) != 0) {
11777c478bd9Sstevel@tonic-gate 		while (pam_env[idx] != 0) {
11787c478bd9Sstevel@tonic-gate 			if (idx < PAM_ENV_ELIM &&
11797c478bd9Sstevel@tonic-gate 			    legalenvvar(pam_env[idx])) {
11807c478bd9Sstevel@tonic-gate 				add_to_envinit(pam_env[idx]);
11817c478bd9Sstevel@tonic-gate 			}
11827c478bd9Sstevel@tonic-gate 			idx++;
11837c478bd9Sstevel@tonic-gate 		}
11847c478bd9Sstevel@tonic-gate 	}
11857c478bd9Sstevel@tonic-gate 
11867c478bd9Sstevel@tonic-gate 	(void) pam_end(pamh, PAM_SUCCESS);
11877c478bd9Sstevel@tonic-gate 
11887c478bd9Sstevel@tonic-gate 	/*
11897c478bd9Sstevel@tonic-gate 	 * Pick up locale environment variables, if any.
11907c478bd9Sstevel@tonic-gate 	 */
11917c478bd9Sstevel@tonic-gate 	lenvp = renvp;
11927c478bd9Sstevel@tonic-gate 	while (*lenvp != NULL) {
11937c478bd9Sstevel@tonic-gate 		int	index;
11947c478bd9Sstevel@tonic-gate 
11957c478bd9Sstevel@tonic-gate 		for (index = 0; localeenv[index] != NULL; index++)
11967c478bd9Sstevel@tonic-gate 			/*
11977c478bd9Sstevel@tonic-gate 			 * locale_envmatch() returns 1 if
11987c478bd9Sstevel@tonic-gate 			 * *lenvp is localenev[index] and valid.
11997c478bd9Sstevel@tonic-gate 			 */
12007c478bd9Sstevel@tonic-gate 			if (locale_envmatch(localeenv[index], *lenvp)) {
12017c478bd9Sstevel@tonic-gate 				add_to_envinit(*lenvp);
12027c478bd9Sstevel@tonic-gate 				break;
12037c478bd9Sstevel@tonic-gate 			}
12047c478bd9Sstevel@tonic-gate 
12057c478bd9Sstevel@tonic-gate 		lenvp++;
12067c478bd9Sstevel@tonic-gate 	}
12077c478bd9Sstevel@tonic-gate 
12087c478bd9Sstevel@tonic-gate 	cp = strrchr(pwd->pw_shell, '/');
12097c478bd9Sstevel@tonic-gate 	if (cp != NULL)
12107c478bd9Sstevel@tonic-gate 		cp++;
12117c478bd9Sstevel@tonic-gate 	else
12127c478bd9Sstevel@tonic-gate 		cp = pwd->pw_shell;
12137c478bd9Sstevel@tonic-gate 	/*
12147c478bd9Sstevel@tonic-gate 	 * rdist has been moved to /usr/bin, so /usr/ucb/rdist might not
12157c478bd9Sstevel@tonic-gate 	 * be present on a system.  So if it doesn't exist we fall back
12167c478bd9Sstevel@tonic-gate 	 * and try for it in /usr/bin.  We take care to match the space
12177c478bd9Sstevel@tonic-gate 	 * after the name because the only purpose of this is to protect
12187c478bd9Sstevel@tonic-gate 	 * the internal call from old rdist's, not humans who type
12197c478bd9Sstevel@tonic-gate 	 * "rsh foo /usr/ucb/rdist".
12207c478bd9Sstevel@tonic-gate 	 */
12217c478bd9Sstevel@tonic-gate #define	RDIST_PROG_NAME	"/usr/ucb/rdist -Server"
12227c478bd9Sstevel@tonic-gate 	if (strncmp(cmdbuf, RDIST_PROG_NAME, strlen(RDIST_PROG_NAME)) == 0) {
12237c478bd9Sstevel@tonic-gate 		if (stat("/usr/ucb/rdist", &statb) != 0) {
12247c478bd9Sstevel@tonic-gate 			(void) strncpy(cmdbuf + 5, "bin", 3);
12257c478bd9Sstevel@tonic-gate 		}
12267c478bd9Sstevel@tonic-gate 	}
12277c478bd9Sstevel@tonic-gate 
12287c478bd9Sstevel@tonic-gate #ifdef DEBUG
12297c478bd9Sstevel@tonic-gate 	syslog(LOG_NOTICE, "rshd: cmdbuf = %s", cmdbuf);
12307c478bd9Sstevel@tonic-gate 	if (do_encrypt)
12317c478bd9Sstevel@tonic-gate 		syslog(LOG_NOTICE, "rshd: cmd to be exec'ed = %s",
12327c478bd9Sstevel@tonic-gate 			((char *)cmdbuf + 3));
12337c478bd9Sstevel@tonic-gate #endif /* DEBUG */
12347c478bd9Sstevel@tonic-gate 
12357c478bd9Sstevel@tonic-gate 	if (do_encrypt && (strncmp(cmdbuf, "-x ", 3) == 0)) {
12367c478bd9Sstevel@tonic-gate 		(void) execle(pwd->pw_shell, cp, "-c", (char *)cmdbuf + 3,
12377c478bd9Sstevel@tonic-gate 				NULL, envinit);
12387c478bd9Sstevel@tonic-gate 	} else {
12397c478bd9Sstevel@tonic-gate 		(void) execle(pwd->pw_shell, cp, "-c", cmdbuf, NULL,
12407c478bd9Sstevel@tonic-gate 				envinit);
12417c478bd9Sstevel@tonic-gate 	}
12427c478bd9Sstevel@tonic-gate 
12437c478bd9Sstevel@tonic-gate 	perror(pwd->pw_shell);
12447c478bd9Sstevel@tonic-gate 	exit(1);
12457c478bd9Sstevel@tonic-gate 
12467c478bd9Sstevel@tonic-gate signout:
12477c478bd9Sstevel@tonic-gate 	if (ccache)
12487c478bd9Sstevel@tonic-gate 		(void) pam_close_session(pamh, 0);
12497c478bd9Sstevel@tonic-gate 	ccache = NULL;
12507c478bd9Sstevel@tonic-gate 	(void) pam_end(pamh, PAM_ABORT);
12517c478bd9Sstevel@tonic-gate 	exit(1);
12527c478bd9Sstevel@tonic-gate }
12537c478bd9Sstevel@tonic-gate 
12547c478bd9Sstevel@tonic-gate static void
12557c478bd9Sstevel@tonic-gate getstr(fd, buf, cnt, err)
12567c478bd9Sstevel@tonic-gate 	int fd;
12577c478bd9Sstevel@tonic-gate 	char *buf;
12587c478bd9Sstevel@tonic-gate 	int cnt;
12597c478bd9Sstevel@tonic-gate 	char *err;
12607c478bd9Sstevel@tonic-gate {
12617c478bd9Sstevel@tonic-gate 	char c;
12627c478bd9Sstevel@tonic-gate 
12637c478bd9Sstevel@tonic-gate 	do {
12647c478bd9Sstevel@tonic-gate 		if (read(fd, &c, 1) != 1)
12657c478bd9Sstevel@tonic-gate 			exit(1);
12667c478bd9Sstevel@tonic-gate 		if (cnt-- == 0) {
12677c478bd9Sstevel@tonic-gate 			error("%s too long\n", err);
12687c478bd9Sstevel@tonic-gate 			exit(1);
12697c478bd9Sstevel@tonic-gate 		}
12707c478bd9Sstevel@tonic-gate 		*buf++ = c;
12717c478bd9Sstevel@tonic-gate 	} while (c != 0);
12727c478bd9Sstevel@tonic-gate }
12737c478bd9Sstevel@tonic-gate 
12747c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/
12757c478bd9Sstevel@tonic-gate static void
12767c478bd9Sstevel@tonic-gate error(char *fmt, ...)
12777c478bd9Sstevel@tonic-gate {
12787c478bd9Sstevel@tonic-gate 	va_list ap;
12797c478bd9Sstevel@tonic-gate 	char buf[RSHD_BUFSIZ];
12807c478bd9Sstevel@tonic-gate 
12817c478bd9Sstevel@tonic-gate 	buf[0] = 1;
12827c478bd9Sstevel@tonic-gate 	va_start(ap, fmt);
12837c478bd9Sstevel@tonic-gate 	(void) vsnprintf(&buf[1], sizeof (buf) - 1, fmt, ap);
12847c478bd9Sstevel@tonic-gate 	va_end(ap);
12857c478bd9Sstevel@tonic-gate 	(void) write(STDERR_FILENO, buf, strlen(buf));
12867c478bd9Sstevel@tonic-gate }
12877c478bd9Sstevel@tonic-gate 
12887c478bd9Sstevel@tonic-gate static char *illegal[] = {
12897c478bd9Sstevel@tonic-gate 	"SHELL=",
12907c478bd9Sstevel@tonic-gate 	"HOME=",
12917c478bd9Sstevel@tonic-gate 	"LOGNAME=",
12927c478bd9Sstevel@tonic-gate #ifndef NO_MAIL
12937c478bd9Sstevel@tonic-gate 	"MAIL=",
12947c478bd9Sstevel@tonic-gate #endif
12957c478bd9Sstevel@tonic-gate 	"CDPATH=",
12967c478bd9Sstevel@tonic-gate 	"IFS=",
12977c478bd9Sstevel@tonic-gate 	"PATH=",
12987c478bd9Sstevel@tonic-gate 	"USER=",
12997c478bd9Sstevel@tonic-gate 	"TZ=",
13007c478bd9Sstevel@tonic-gate 	0
13017c478bd9Sstevel@tonic-gate };
13027c478bd9Sstevel@tonic-gate 
13037c478bd9Sstevel@tonic-gate /*
13047c478bd9Sstevel@tonic-gate  * legalenvvar - can PAM modules insert this environmental variable?
13057c478bd9Sstevel@tonic-gate  */
13067c478bd9Sstevel@tonic-gate 
13077c478bd9Sstevel@tonic-gate static int
13087c478bd9Sstevel@tonic-gate legalenvvar(char *s)
13097c478bd9Sstevel@tonic-gate {
13107c478bd9Sstevel@tonic-gate 	register char **p;
13117c478bd9Sstevel@tonic-gate 
13127c478bd9Sstevel@tonic-gate 	for (p = illegal; *p; p++)
13137c478bd9Sstevel@tonic-gate 		if (strncmp(s, *p, strlen(*p)) == 0)
13147c478bd9Sstevel@tonic-gate 			return (0);
13157c478bd9Sstevel@tonic-gate 
13167c478bd9Sstevel@tonic-gate 	if (s[0] == 'L' && s[1] == 'D' && s[2] == '_')
13177c478bd9Sstevel@tonic-gate 		return (0);
13187c478bd9Sstevel@tonic-gate 
13197c478bd9Sstevel@tonic-gate 	return (1);
13207c478bd9Sstevel@tonic-gate }
13217c478bd9Sstevel@tonic-gate 
13227c478bd9Sstevel@tonic-gate /*
13237c478bd9Sstevel@tonic-gate  * Add a string to the environment of the new process.
13247c478bd9Sstevel@tonic-gate  */
13257c478bd9Sstevel@tonic-gate 
13267c478bd9Sstevel@tonic-gate static void
13277c478bd9Sstevel@tonic-gate add_to_envinit(char *string)
13287c478bd9Sstevel@tonic-gate {
13297c478bd9Sstevel@tonic-gate 	/*
13307c478bd9Sstevel@tonic-gate 	 * Reserve space for 2 * 8 = 16 environment entries initially which
13317c478bd9Sstevel@tonic-gate 	 * should be enough to avoid reallocation of "envinit" in most cases.
13327c478bd9Sstevel@tonic-gate 	 */
13337c478bd9Sstevel@tonic-gate 	static int	size = 8;
13347c478bd9Sstevel@tonic-gate 	static int	index = 0;
13357c478bd9Sstevel@tonic-gate 
13367c478bd9Sstevel@tonic-gate 	if (string == NULL)
13377c478bd9Sstevel@tonic-gate 		return;
13387c478bd9Sstevel@tonic-gate 
13397c478bd9Sstevel@tonic-gate 	if ((envinit == NULL) || (index == size)) {
13407c478bd9Sstevel@tonic-gate 		size *= 2;
13417c478bd9Sstevel@tonic-gate 		envinit = realloc(envinit, (size + 1) * sizeof (char *));
13427c478bd9Sstevel@tonic-gate 		if (envinit == NULL) {
13437c478bd9Sstevel@tonic-gate 			perror("malloc");
13447c478bd9Sstevel@tonic-gate 			exit(1);
13457c478bd9Sstevel@tonic-gate 		}
13467c478bd9Sstevel@tonic-gate 	}
13477c478bd9Sstevel@tonic-gate 
13487c478bd9Sstevel@tonic-gate 	envinit[index++] = string;
13497c478bd9Sstevel@tonic-gate 	envinit[index] = NULL;
13507c478bd9Sstevel@tonic-gate }
13517c478bd9Sstevel@tonic-gate 
13527c478bd9Sstevel@tonic-gate /*
13537c478bd9Sstevel@tonic-gate  * Check if lenv and penv matches or not.
13547c478bd9Sstevel@tonic-gate  */
13557c478bd9Sstevel@tonic-gate static int
13567c478bd9Sstevel@tonic-gate locale_envmatch(char *lenv, char *penv)
13577c478bd9Sstevel@tonic-gate {
13587c478bd9Sstevel@tonic-gate 	while ((*lenv == *penv) && (*lenv != '\0') && (*penv != '=')) {
13597c478bd9Sstevel@tonic-gate 		lenv++;
13607c478bd9Sstevel@tonic-gate 		penv++;
13617c478bd9Sstevel@tonic-gate 	}
13627c478bd9Sstevel@tonic-gate 
13637c478bd9Sstevel@tonic-gate 	/*
13647c478bd9Sstevel@tonic-gate 	 * '/' is eliminated for security reason.
13657c478bd9Sstevel@tonic-gate 	 */
13667c478bd9Sstevel@tonic-gate 	return ((*lenv == '\0' && *penv == '=' && *(penv + 1) != '/'));
13677c478bd9Sstevel@tonic-gate }
13687c478bd9Sstevel@tonic-gate 
13697c478bd9Sstevel@tonic-gate #ifndef	KRB_SENDAUTH_VLEN
13707c478bd9Sstevel@tonic-gate #define	KRB_SENDAUTH_VLEN	8	/* length for version strings */
13717c478bd9Sstevel@tonic-gate #endif
13727c478bd9Sstevel@tonic-gate 
13737c478bd9Sstevel@tonic-gate /* MUST be KRB_SENDAUTH_VLEN chars */
13747c478bd9Sstevel@tonic-gate #define	KRB_SENDAUTH_VERS	"AUTHV0.1"
13757c478bd9Sstevel@tonic-gate #define	SIZEOF_INADDR sizeof (struct in_addr)
13767c478bd9Sstevel@tonic-gate 
13777c478bd9Sstevel@tonic-gate static krb5_error_code
13787c478bd9Sstevel@tonic-gate recvauth(int netf, int *valid_checksum)
13797c478bd9Sstevel@tonic-gate {
13807c478bd9Sstevel@tonic-gate 	krb5_auth_context auth_context = NULL;
13817c478bd9Sstevel@tonic-gate 	krb5_error_code status;
13827c478bd9Sstevel@tonic-gate 	struct sockaddr_in laddr;
13837c478bd9Sstevel@tonic-gate 	int len;
13847c478bd9Sstevel@tonic-gate 	krb5_data inbuf;
13857c478bd9Sstevel@tonic-gate 	krb5_authenticator *authenticator;
13867c478bd9Sstevel@tonic-gate 	krb5_ticket *ticket;
13877c478bd9Sstevel@tonic-gate 	krb5_rcache rcache;
13887c478bd9Sstevel@tonic-gate 	krb5_data version;
13897c478bd9Sstevel@tonic-gate 	krb5_encrypt_block eblock;	/* eblock for encrypt/decrypt */
13907c478bd9Sstevel@tonic-gate 	krb5_data desinbuf;
13917c478bd9Sstevel@tonic-gate 	krb5_data desoutbuf;
13927c478bd9Sstevel@tonic-gate 	char des_inbuf[2 * RSHD_BUFSIZ];
13937c478bd9Sstevel@tonic-gate 			/* needs to be > largest read size */
13947c478bd9Sstevel@tonic-gate 	char des_outbuf[2 * RSHD_BUFSIZ + 4];
13957c478bd9Sstevel@tonic-gate 			/* needs to be > largest write size */
13967c478bd9Sstevel@tonic-gate 
13977c478bd9Sstevel@tonic-gate 	*valid_checksum = 0;
13987c478bd9Sstevel@tonic-gate 	len = sizeof (laddr);
13997c478bd9Sstevel@tonic-gate 
14007c478bd9Sstevel@tonic-gate 	if (getsockname(netf, (struct sockaddr *)&laddr, &len)) {
14017c478bd9Sstevel@tonic-gate 		exit(1);
14027c478bd9Sstevel@tonic-gate 	}
14037c478bd9Sstevel@tonic-gate 
14047c478bd9Sstevel@tonic-gate 	if (status = krb5_auth_con_init(bsd_context, &auth_context))
14057c478bd9Sstevel@tonic-gate 		return (status);
14067c478bd9Sstevel@tonic-gate 
14077c478bd9Sstevel@tonic-gate 	if (status = krb5_auth_con_genaddrs(bsd_context, auth_context, netf,
14087c478bd9Sstevel@tonic-gate 		KRB5_AUTH_CONTEXT_GENERATE_REMOTE_FULL_ADDR))
14097c478bd9Sstevel@tonic-gate 		return (status);
14107c478bd9Sstevel@tonic-gate 
14117c478bd9Sstevel@tonic-gate 	status = krb5_auth_con_getrcache(bsd_context, auth_context, &rcache);
14127c478bd9Sstevel@tonic-gate 	if (status)
14137c478bd9Sstevel@tonic-gate 		return (status);
14147c478bd9Sstevel@tonic-gate 
14157c478bd9Sstevel@tonic-gate 	if (!rcache) {
14167c478bd9Sstevel@tonic-gate 		krb5_principal server;
14177c478bd9Sstevel@tonic-gate 
14187c478bd9Sstevel@tonic-gate 		status = krb5_sname_to_principal(bsd_context, 0, 0,
14197c478bd9Sstevel@tonic-gate 			KRB5_NT_SRV_HST, &server);
14207c478bd9Sstevel@tonic-gate 		if (status)
14217c478bd9Sstevel@tonic-gate 			return (status);
14227c478bd9Sstevel@tonic-gate 
14237c478bd9Sstevel@tonic-gate 		status = krb5_get_server_rcache(bsd_context,
14247c478bd9Sstevel@tonic-gate 			krb5_princ_component(bsd_context, server, 0),
14257c478bd9Sstevel@tonic-gate 			&rcache);
14267c478bd9Sstevel@tonic-gate 		krb5_free_principal(bsd_context, server);
14277c478bd9Sstevel@tonic-gate 		if (status)
14287c478bd9Sstevel@tonic-gate 			return (status);
14297c478bd9Sstevel@tonic-gate 
14307c478bd9Sstevel@tonic-gate 		status = krb5_auth_con_setrcache(bsd_context, auth_context,
14317c478bd9Sstevel@tonic-gate 							rcache);
14327c478bd9Sstevel@tonic-gate 		if (status)
14337c478bd9Sstevel@tonic-gate 			return (status);
14347c478bd9Sstevel@tonic-gate 	}
14357c478bd9Sstevel@tonic-gate 
14367c478bd9Sstevel@tonic-gate 	status = krb5_recvauth_version(bsd_context, &auth_context, &netf,
14377c478bd9Sstevel@tonic-gate 		NULL,		/* Specify daemon principal */
14387c478bd9Sstevel@tonic-gate 		0,		/* no flags */
14397c478bd9Sstevel@tonic-gate 		keytab,		/* normally NULL to use v5srvtab */
14407c478bd9Sstevel@tonic-gate 		&ticket,	/* return ticket */
14417c478bd9Sstevel@tonic-gate 		&version);	/* application version string */
14427c478bd9Sstevel@tonic-gate 
14437c478bd9Sstevel@tonic-gate 
14447c478bd9Sstevel@tonic-gate 	if (status) {
14457c478bd9Sstevel@tonic-gate 		getstr(netf, locuser, sizeof (locuser), "locuser");
14467c478bd9Sstevel@tonic-gate 		getstr(netf, cmdbuf, sizeof (cmdbuf), "command");
14477c478bd9Sstevel@tonic-gate 		getstr(netf, remuser, sizeof (locuser), "remuser");
14487c478bd9Sstevel@tonic-gate 		return (status);
14497c478bd9Sstevel@tonic-gate 	}
14507c478bd9Sstevel@tonic-gate 	getstr(netf, locuser, sizeof (locuser), "locuser");
14517c478bd9Sstevel@tonic-gate 	getstr(netf, cmdbuf, sizeof (cmdbuf), "command");
14527c478bd9Sstevel@tonic-gate 
14537c478bd9Sstevel@tonic-gate 	/* Must be V5  */
14547c478bd9Sstevel@tonic-gate 
14557c478bd9Sstevel@tonic-gate 	kcmd_protocol = KCMD_UNKNOWN_PROTOCOL;
14567c478bd9Sstevel@tonic-gate 	if (version.length != 9 || version.data == NULL) {
14577c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "bad application version length");
14587c478bd9Sstevel@tonic-gate 		error(gettext("bad application version length\n"));
14597c478bd9Sstevel@tonic-gate 		exit(1);
14607c478bd9Sstevel@tonic-gate 	}
14617c478bd9Sstevel@tonic-gate 	if (strncmp(version.data, "KCMDV0.1", 9) == 0) {
14627c478bd9Sstevel@tonic-gate 		kcmd_protocol = KCMD_OLD_PROTOCOL;
14637c478bd9Sstevel@tonic-gate 	} else if (strncmp(version.data, "KCMDV0.2", 9) == 0) {
14647c478bd9Sstevel@tonic-gate 		kcmd_protocol = KCMD_NEW_PROTOCOL;
14657c478bd9Sstevel@tonic-gate 	} else {
14667c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "Unrecognized KCMD protocol (%s)",
14677c478bd9Sstevel@tonic-gate 			(char *)version.data);
14687c478bd9Sstevel@tonic-gate 		error(gettext("Unrecognized KCMD protocol (%s)"),
14697c478bd9Sstevel@tonic-gate 			(char *)version.data);
14707c478bd9Sstevel@tonic-gate 		exit(1);
14717c478bd9Sstevel@tonic-gate 	}
14727c478bd9Sstevel@tonic-gate 	getstr(netf, remuser, sizeof (locuser), "remuser");
14737c478bd9Sstevel@tonic-gate 
14747c478bd9Sstevel@tonic-gate 	if ((status = krb5_unparse_name(bsd_context, ticket->enc_part2->client,
14757c478bd9Sstevel@tonic-gate 			&kremuser)))
14767c478bd9Sstevel@tonic-gate 		return (status);
14777c478bd9Sstevel@tonic-gate 
14787c478bd9Sstevel@tonic-gate 	if ((status = krb5_copy_principal(bsd_context,
14797c478bd9Sstevel@tonic-gate 				ticket->enc_part2->client, &client)))
14807c478bd9Sstevel@tonic-gate 		return (status);
14817c478bd9Sstevel@tonic-gate 
14827c478bd9Sstevel@tonic-gate 
14837c478bd9Sstevel@tonic-gate 	if (checksum_required && (kcmd_protocol == KCMD_OLD_PROTOCOL)) {
14847c478bd9Sstevel@tonic-gate 		if ((status = krb5_auth_con_getauthenticator(bsd_context,
14857c478bd9Sstevel@tonic-gate 			auth_context, &authenticator)))
14867c478bd9Sstevel@tonic-gate 			return (status);
14877c478bd9Sstevel@tonic-gate 
14887c478bd9Sstevel@tonic-gate 		if (authenticator->checksum && checksum_required) {
14897c478bd9Sstevel@tonic-gate 			struct sockaddr_in adr;
14907c478bd9Sstevel@tonic-gate 			int adr_length = sizeof (adr);
14917c478bd9Sstevel@tonic-gate 			int chksumsize = strlen(cmdbuf) + strlen(locuser) + 32;
14927c478bd9Sstevel@tonic-gate 			krb5_data input;
14937c478bd9Sstevel@tonic-gate 			krb5_keyblock key;
14947c478bd9Sstevel@tonic-gate 
14957c478bd9Sstevel@tonic-gate 			char *chksumbuf = (char *)malloc(chksumsize);
14967c478bd9Sstevel@tonic-gate 
14977c478bd9Sstevel@tonic-gate 			if (chksumbuf == 0)
14987c478bd9Sstevel@tonic-gate 				goto error_cleanup;
14997c478bd9Sstevel@tonic-gate 			if (getsockname(netf, (struct sockaddr *)&adr,
15007c478bd9Sstevel@tonic-gate 					&adr_length) != 0)
15017c478bd9Sstevel@tonic-gate 				goto error_cleanup;
15027c478bd9Sstevel@tonic-gate 
15037c478bd9Sstevel@tonic-gate 			(void) snprintf(chksumbuf, chksumsize, "%u:",
15047c478bd9Sstevel@tonic-gate 					ntohs(adr.sin_port));
15057c478bd9Sstevel@tonic-gate 			if (strlcat(chksumbuf, cmdbuf,
15067c478bd9Sstevel@tonic-gate 					chksumsize) >= chksumsize) {
15077c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR, "cmd buffer too long.");
15087c478bd9Sstevel@tonic-gate 				free(chksumbuf);
15097c478bd9Sstevel@tonic-gate 				return (-1);
15107c478bd9Sstevel@tonic-gate 			}
15117c478bd9Sstevel@tonic-gate 			if (strlcat(chksumbuf, locuser,
15127c478bd9Sstevel@tonic-gate 					chksumsize) >= chksumsize) {
15137c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR, "locuser too long.");
15147c478bd9Sstevel@tonic-gate 				free(chksumbuf);
15157c478bd9Sstevel@tonic-gate 				return (-1);
15167c478bd9Sstevel@tonic-gate 			}
15177c478bd9Sstevel@tonic-gate 
15187c478bd9Sstevel@tonic-gate 			input.data = chksumbuf;
15197c478bd9Sstevel@tonic-gate 			input.length = strlen(chksumbuf);
15207c478bd9Sstevel@tonic-gate 			key.magic = ticket->enc_part2->session->magic;
15217c478bd9Sstevel@tonic-gate 			key.enctype = ticket->enc_part2->session->enctype;
15227c478bd9Sstevel@tonic-gate 			key.contents = ticket->enc_part2->session->contents;
15237c478bd9Sstevel@tonic-gate 			key.length = ticket->enc_part2->session->length;
15247c478bd9Sstevel@tonic-gate 
15257c478bd9Sstevel@tonic-gate 			status = krb5_c_verify_checksum(bsd_context,
15267c478bd9Sstevel@tonic-gate 			    &key, 0, &input, authenticator->checksum,
15277c478bd9Sstevel@tonic-gate 			    (unsigned int *)valid_checksum);
15287c478bd9Sstevel@tonic-gate 
15297c478bd9Sstevel@tonic-gate 			if (status == 0 && *valid_checksum == 0)
15307c478bd9Sstevel@tonic-gate 			    status = KRB5KRB_AP_ERR_BAD_INTEGRITY;
15317c478bd9Sstevel@tonic-gate error_cleanup:
15327c478bd9Sstevel@tonic-gate 			if (chksumbuf)
15337c478bd9Sstevel@tonic-gate 				krb5_xfree(chksumbuf);
15347c478bd9Sstevel@tonic-gate 			if (status) {
15357c478bd9Sstevel@tonic-gate 				krb5_free_authenticator(bsd_context,
15367c478bd9Sstevel@tonic-gate 						authenticator);
15377c478bd9Sstevel@tonic-gate 				return (status);
15387c478bd9Sstevel@tonic-gate 			}
15397c478bd9Sstevel@tonic-gate 		}
15407c478bd9Sstevel@tonic-gate 		krb5_free_authenticator(bsd_context, authenticator);
15417c478bd9Sstevel@tonic-gate 	}
15427c478bd9Sstevel@tonic-gate 
15437c478bd9Sstevel@tonic-gate 
15447c478bd9Sstevel@tonic-gate 	if ((strncmp(cmdbuf, "-x ", 3) == 0)) {
15457c478bd9Sstevel@tonic-gate 		if (krb5_privacy_allowed()) {
15467c478bd9Sstevel@tonic-gate 			do_encrypt = 1;
15477c478bd9Sstevel@tonic-gate 		} else {
15487c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, "rshd: Encryption not supported");
15497c478bd9Sstevel@tonic-gate 			error("rshd: Encryption not supported. \n");
15507c478bd9Sstevel@tonic-gate 			exit(2);
15517c478bd9Sstevel@tonic-gate 		}
15527c478bd9Sstevel@tonic-gate 
15537c478bd9Sstevel@tonic-gate 		status = krb5_auth_con_getremotesubkey(bsd_context,
15547c478bd9Sstevel@tonic-gate 						    auth_context,
15557c478bd9Sstevel@tonic-gate 						    &sessionkey);
15567c478bd9Sstevel@tonic-gate 		if (status) {
15577c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, "Error getting KRB5 session subkey");
15587c478bd9Sstevel@tonic-gate 			error(gettext("Error getting KRB5 session subkey"));
15597c478bd9Sstevel@tonic-gate 			exit(1);
15607c478bd9Sstevel@tonic-gate 		}
15617c478bd9Sstevel@tonic-gate 		/*
15627c478bd9Sstevel@tonic-gate 		 * The "new" protocol requires that a subkey be sent.
15637c478bd9Sstevel@tonic-gate 		 */
15647c478bd9Sstevel@tonic-gate 		if (sessionkey == NULL && kcmd_protocol == KCMD_NEW_PROTOCOL) {
15657c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, "No KRB5 session subkey sent");
15667c478bd9Sstevel@tonic-gate 			error(gettext("No KRB5 session subkey sent"));
15677c478bd9Sstevel@tonic-gate 			exit(1);
15687c478bd9Sstevel@tonic-gate 		}
15697c478bd9Sstevel@tonic-gate 		/*
15707c478bd9Sstevel@tonic-gate 		 * The "old" protocol does not permit an authenticator subkey.
15717c478bd9Sstevel@tonic-gate 		 * The key is taken from the ticket instead (see below).
15727c478bd9Sstevel@tonic-gate 		 */
15737c478bd9Sstevel@tonic-gate 		if (sessionkey != NULL && kcmd_protocol == KCMD_OLD_PROTOCOL) {
15747c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, "KRB5 session subkey not permitted "
15757c478bd9Sstevel@tonic-gate 				"with old KCMD protocol");
15767c478bd9Sstevel@tonic-gate 			error(gettext("KRB5 session subkey not permitted "
15777c478bd9Sstevel@tonic-gate 				"with old KCMD protocol"));
15787c478bd9Sstevel@tonic-gate 			exit(1);
15797c478bd9Sstevel@tonic-gate 		}
15807c478bd9Sstevel@tonic-gate 		/*
15817c478bd9Sstevel@tonic-gate 		 * If no key at this point, use the session key from
15827c478bd9Sstevel@tonic-gate 		 * the ticket.
15837c478bd9Sstevel@tonic-gate 		 */
15847c478bd9Sstevel@tonic-gate 		if (sessionkey == NULL) {
15857c478bd9Sstevel@tonic-gate 			/*
15867c478bd9Sstevel@tonic-gate 			 * Save the session key so we can configure the crypto
15877c478bd9Sstevel@tonic-gate 			 * module later.
15887c478bd9Sstevel@tonic-gate 			 */
15897c478bd9Sstevel@tonic-gate 			status = krb5_copy_keyblock(bsd_context,
15907c478bd9Sstevel@tonic-gate 						ticket->enc_part2->session,
15917c478bd9Sstevel@tonic-gate 						&sessionkey);
15927c478bd9Sstevel@tonic-gate 			if (status) {
15937c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR, "krb5_copy_keyblock failed");
15947c478bd9Sstevel@tonic-gate 				error(gettext("krb5_copy_keyblock failed"));
15957c478bd9Sstevel@tonic-gate 				exit(1);
15967c478bd9Sstevel@tonic-gate 			}
15977c478bd9Sstevel@tonic-gate 		}
15987c478bd9Sstevel@tonic-gate 		/*
15997c478bd9Sstevel@tonic-gate 		 * If session key still cannot be found, we must
16007c478bd9Sstevel@tonic-gate 		 * exit because encryption is required here
16017c478bd9Sstevel@tonic-gate 		 * when encr_flag (-x) is set.
16027c478bd9Sstevel@tonic-gate 		 */
16037c478bd9Sstevel@tonic-gate 		if (sessionkey == NULL) {
16047c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, "Could not find an encryption key");
16057c478bd9Sstevel@tonic-gate 			error(gettext("Could not find an encryption key"));
16067c478bd9Sstevel@tonic-gate 			exit(1);
16077c478bd9Sstevel@tonic-gate 		}
16087c478bd9Sstevel@tonic-gate 
16097c478bd9Sstevel@tonic-gate 		/*
16107c478bd9Sstevel@tonic-gate 		 * Initialize parameters/buffers for desread & deswrite here.
16117c478bd9Sstevel@tonic-gate 		 */
16127c478bd9Sstevel@tonic-gate 		desinbuf.data = des_inbuf;
16137c478bd9Sstevel@tonic-gate 		desoutbuf.data = des_outbuf;
16147c478bd9Sstevel@tonic-gate 		desinbuf.length = sizeof (des_inbuf);
16157c478bd9Sstevel@tonic-gate 		desoutbuf.length = sizeof (des_outbuf);
16167c478bd9Sstevel@tonic-gate 
16177c478bd9Sstevel@tonic-gate 		eblock.crypto_entry = sessionkey->enctype;
16187c478bd9Sstevel@tonic-gate 		eblock.key = (krb5_keyblock *)sessionkey;
16197c478bd9Sstevel@tonic-gate 
16207c478bd9Sstevel@tonic-gate 		init_encrypt(do_encrypt, bsd_context, kcmd_protocol,
16217c478bd9Sstevel@tonic-gate 				&desinbuf, &desoutbuf, SERVER, &eblock);
16227c478bd9Sstevel@tonic-gate 	}
16237c478bd9Sstevel@tonic-gate 
16247c478bd9Sstevel@tonic-gate 	ticket->enc_part2->session = 0;
16257c478bd9Sstevel@tonic-gate 
16267c478bd9Sstevel@tonic-gate 	if ((status = krb5_read_message(bsd_context, (krb5_pointer) & netf,
16277c478bd9Sstevel@tonic-gate 				&inbuf))) {
16287c478bd9Sstevel@tonic-gate 		error(gettext("Error reading message: %s\n"),
16297c478bd9Sstevel@tonic-gate 				error_message(status));
16307c478bd9Sstevel@tonic-gate 		exit(1);
16317c478bd9Sstevel@tonic-gate 	}
16327c478bd9Sstevel@tonic-gate 
16337c478bd9Sstevel@tonic-gate 	if (inbuf.length) {
1634*32885d59Sgtb 		krb5_creds **creds = NULL;
1635*32885d59Sgtb 
16367c478bd9Sstevel@tonic-gate 		/* Forwarding being done, read creds */
1637*32885d59Sgtb 		if ((status = krb5_rd_cred(bsd_context,
1638*32885d59Sgtb 					    auth_context, &inbuf, &creds,
1639*32885d59Sgtb 					    NULL))) {
16407c478bd9Sstevel@tonic-gate 			error("Can't get forwarded credentials: %s\n",
16417c478bd9Sstevel@tonic-gate 				error_message(status));
16427c478bd9Sstevel@tonic-gate 			exit(1);
16437c478bd9Sstevel@tonic-gate 		}
16447c478bd9Sstevel@tonic-gate 
1645*32885d59Sgtb 		/* Store the forwarded creds in the ccache */
1646*32885d59Sgtb 		if ((status = store_forw_creds(bsd_context,
1647*32885d59Sgtb 					    creds, ticket, locuser,
1648*32885d59Sgtb 					    &ccache))) {
1649*32885d59Sgtb 			error("Can't store forwarded credentials: %s\n",
1650*32885d59Sgtb 				error_message(status));
1651*32885d59Sgtb 			exit(1);
16527c478bd9Sstevel@tonic-gate 		}
1653*32885d59Sgtb 		krb5_free_creds(bsd_context, *creds);
1654*32885d59Sgtb 	}
1655*32885d59Sgtb 
16567c478bd9Sstevel@tonic-gate 	krb5_free_ticket(bsd_context, ticket);
16577c478bd9Sstevel@tonic-gate 	return (0);
16587c478bd9Sstevel@tonic-gate }
16597c478bd9Sstevel@tonic-gate 
16607c478bd9Sstevel@tonic-gate static void
16617c478bd9Sstevel@tonic-gate usage(void)
16627c478bd9Sstevel@tonic-gate {
16637c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("%s: rshd [-k5eciU] "
16647c478bd9Sstevel@tonic-gate 			"[-P path] [-M realm] [-s tos] "
16657c478bd9Sstevel@tonic-gate #ifdef DEBUG
16667c478bd9Sstevel@tonic-gate 			"[-D port] "
16677c478bd9Sstevel@tonic-gate #endif /* DEBUG */
16687c478bd9Sstevel@tonic-gate 			"[-S keytab]"), gettext("usage"));
16697c478bd9Sstevel@tonic-gate 
16707c478bd9Sstevel@tonic-gate 	syslog(LOG_ERR, "%s: rshd [-k5eciU] [-P path] [-M realm] [-s tos] "
16717c478bd9Sstevel@tonic-gate #ifdef DEBUG
16727c478bd9Sstevel@tonic-gate 			"[-D port] "
16737c478bd9Sstevel@tonic-gate #endif /* DEBUG */
16747c478bd9Sstevel@tonic-gate 			"[-S keytab]", gettext("usage"));
16757c478bd9Sstevel@tonic-gate }
1676