xref: /titanic_51/usr/src/cmd/keyserv/chkey.c (revision 36e852a172cba914383d7341c988128b2c667fbd)
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
5a0368f78Speteh  * Common Development and Distribution License (the "License").
6a0368f78Speteh  * 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  */
217c478bd9Sstevel@tonic-gate /*
227d1e8394SAshok Kumar T  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
277c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
317c478bd9Sstevel@tonic-gate  * The Regents of the University of California
327c478bd9Sstevel@tonic-gate  * All Rights Reserved
337c478bd9Sstevel@tonic-gate  *
347c478bd9Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
357c478bd9Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
367c478bd9Sstevel@tonic-gate  * contributors.
377c478bd9Sstevel@tonic-gate  */
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate #include <assert.h>
417c478bd9Sstevel@tonic-gate #include <stdio.h>
427c478bd9Sstevel@tonic-gate #include <stdlib.h>
437c478bd9Sstevel@tonic-gate #include <string.h>
447c478bd9Sstevel@tonic-gate #include <pwd.h>
457c478bd9Sstevel@tonic-gate #include <shadow.h>
467c478bd9Sstevel@tonic-gate #include <crypt.h>
477c478bd9Sstevel@tonic-gate #include <sys/types.h>
487c478bd9Sstevel@tonic-gate #include <unistd.h>
497c478bd9Sstevel@tonic-gate #include <rpc/rpc.h>
507c478bd9Sstevel@tonic-gate #include <rpc/key_prot.h>
517c478bd9Sstevel@tonic-gate #include <rpcsvc/nis.h>
527c478bd9Sstevel@tonic-gate #include <rpcsvc/nis_dhext.h>
537c478bd9Sstevel@tonic-gate #include <rpcsvc/ypclnt.h>
547c478bd9Sstevel@tonic-gate #include <nsswitch.h>
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate #define	PK_FILES	1
577c478bd9Sstevel@tonic-gate #define	PK_YP		2
587c478bd9Sstevel@tonic-gate #define	PK_LDAP		4
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate #define	CURMECH		mechs[mcount]
61*36e852a1SRaja Andra #define	DESCREDPASSLEN	sizeof (des_block)
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate static char	CRED_TABLE[] = "cred.org_dir";
647c478bd9Sstevel@tonic-gate static char	PKMAP[] = "publickey.byname";
657c478bd9Sstevel@tonic-gate static char	PKFILE[] = "/etc/publickey";
667c478bd9Sstevel@tonic-gate #define	MAXHOSTNAMELEN	256
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate #define	ROOTKEY_FILE		"/etc/.rootkey"
697c478bd9Sstevel@tonic-gate #define	ROOTKEY_FILE_BACKUP	"/etc/.rootkey.bak"
707c478bd9Sstevel@tonic-gate #define	MAXROOTKEY_LINE_LEN	4224	/* Good upto 16384-bit keys */
717c478bd9Sstevel@tonic-gate #define	MAXROOTKEY_LEN		4096
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate /* Should last up to 16384-bit keys */
747c478bd9Sstevel@tonic-gate #define	MAXPKENTLEN	8500
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate bool_t		makenew = TRUE;   /* Make new keys or reencrypt existing */
777c478bd9Sstevel@tonic-gate bool_t		specmech = FALSE; /* Specific mechs requested */
787c478bd9Sstevel@tonic-gate bool_t		force = FALSE;
797c478bd9Sstevel@tonic-gate int		dest_service = 0; /* To which nameservice do we store key(s) */
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate char		*program_name;
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate mechanism_t	**mechs = NULL;   /* List of DH mechanisms */
847c478bd9Sstevel@tonic-gate char		**plist = NULL;	  /* List of public key(s) */
857c478bd9Sstevel@tonic-gate char		**slist = NULL;	  /* List of secret key(s) */
867c478bd9Sstevel@tonic-gate char		**clist = NULL;   /* List of encrypted secret key(s) */
877c478bd9Sstevel@tonic-gate int		numspecmech = 0;  /* Number of mechanisms specified */
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate struct passwd	*pw = NULL;	  /* passwd entry of user */
907c478bd9Sstevel@tonic-gate struct spwd	*spw = NULL;	  /* shadow entry of user */
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate char		*netname = NULL;  /* RPC netname of user */
937c478bd9Sstevel@tonic-gate char		local_domain[MAXNETNAMELEN + 1];
947c478bd9Sstevel@tonic-gate char		*sec_domain = NULL;
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate char		**rpc_pws = NULL; /* List of S-RPC passwords */
977c478bd9Sstevel@tonic-gate int		rpc_pw_count = 0; /* Number of passwords entered by user */
987c478bd9Sstevel@tonic-gate char		*login_pw = NULL; /* Unencrypted login password */
997d1e8394SAshok Kumar T char		short_login_pw[DESCREDPASSLEN + 1];
1007d1e8394SAshok Kumar T /* Short S-RPC password, which has first 8 chars of login_pw */
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate static int add_cred_obj(nis_object *, char *);
1037c478bd9Sstevel@tonic-gate static void cmp_passwd();
1047c478bd9Sstevel@tonic-gate static void encryptkeys();
1057c478bd9Sstevel@tonic-gate static void error_msg();
1067c478bd9Sstevel@tonic-gate static char *fgets_ignorenul();
1077c478bd9Sstevel@tonic-gate static void getpublics();
1087c478bd9Sstevel@tonic-gate static void getrpcpws();
1097c478bd9Sstevel@tonic-gate static void getsecrets();
1107c478bd9Sstevel@tonic-gate static void initkeylist(bool_t);
1117c478bd9Sstevel@tonic-gate static void keylogin(keylen_t, algtype_t);
1127c478bd9Sstevel@tonic-gate static void keylogin_des();
1137c478bd9Sstevel@tonic-gate static void makenewkeys();
1147c478bd9Sstevel@tonic-gate static int modify_cred_obj(nis_object *, char *);
1157c478bd9Sstevel@tonic-gate static void storekeys();
1167c478bd9Sstevel@tonic-gate static void usage();
1177c478bd9Sstevel@tonic-gate static void write_rootkey();
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate extern nis_object *init_entry();
1207c478bd9Sstevel@tonic-gate extern int get_pk_source(char *);
1217c478bd9Sstevel@tonic-gate extern int localupdate(char *, char *, uint_t, char *);
1227c478bd9Sstevel@tonic-gate extern int xencrypt();
1237c478bd9Sstevel@tonic-gate extern int xencrypt_g();
1247c478bd9Sstevel@tonic-gate extern int __gen_dhkeys();
1257c478bd9Sstevel@tonic-gate extern int key_setnet();
1267c478bd9Sstevel@tonic-gate extern int key_setnet_g();
1277c478bd9Sstevel@tonic-gate extern int key_secretkey_is_set_g();
1287c478bd9Sstevel@tonic-gate extern int __getnetnamebyuid();
1297c478bd9Sstevel@tonic-gate extern int getdomainname();
1307c478bd9Sstevel@tonic-gate extern int ldap_update(char *, char *, char *, char *, char *);
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate static void
1347c478bd9Sstevel@tonic-gate error_msg()
1357c478bd9Sstevel@tonic-gate {
1367c478bd9Sstevel@tonic-gate 	if (sec_domain && *sec_domain &&
1377c478bd9Sstevel@tonic-gate 	    strcasecmp(sec_domain, local_domain)) {
1387c478bd9Sstevel@tonic-gate 		fprintf(stderr,
1397c478bd9Sstevel@tonic-gate "The system default domain '%s' is different from the Secure RPC\n\
140*36e852a1SRaja Andra domain %s where the key is stored. \n", local_domain, sec_domain);
1417c478bd9Sstevel@tonic-gate 		exit(1);
1427c478bd9Sstevel@tonic-gate 	}
1437c478bd9Sstevel@tonic-gate }
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate static void
1477c478bd9Sstevel@tonic-gate usage()
1487c478bd9Sstevel@tonic-gate {
149*36e852a1SRaja Andra 	fprintf(stderr, "usage: %s [-p] [-s ldap | nis | files] \n",
1507c478bd9Sstevel@tonic-gate 	    program_name);
1517c478bd9Sstevel@tonic-gate 	exit(1);
1527c478bd9Sstevel@tonic-gate }
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate /* Encrypt secret key(s) with login_pw */
1567c478bd9Sstevel@tonic-gate static void
1577c478bd9Sstevel@tonic-gate encryptkeys()
1587c478bd9Sstevel@tonic-gate {
1597c478bd9Sstevel@tonic-gate 	int	mcount, ccount = 0;
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate 	if (mechs) {
1627c478bd9Sstevel@tonic-gate 		for (mcount = 0; CURMECH; mcount++) {
1637c478bd9Sstevel@tonic-gate 			char		*crypt = NULL;
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate 			if (!xencrypt_g(slist[mcount], CURMECH->keylen,
1667d1e8394SAshok Kumar T 			    CURMECH->algtype, short_login_pw, netname,
1677c478bd9Sstevel@tonic-gate 			    &crypt, TRUE)) {
1687c478bd9Sstevel@tonic-gate 				/* Could not crypt key */
1697c478bd9Sstevel@tonic-gate 				crypt = NULL;
1707c478bd9Sstevel@tonic-gate 			} else
1717c478bd9Sstevel@tonic-gate 				ccount++;
1727c478bd9Sstevel@tonic-gate 			clist[mcount] = crypt;
1737c478bd9Sstevel@tonic-gate 		}
1747c478bd9Sstevel@tonic-gate 	} else {
1757c478bd9Sstevel@tonic-gate 		char		*crypt = NULL;
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate 		if (!(crypt =
1787c478bd9Sstevel@tonic-gate 		    (char *)malloc(HEXKEYBYTES + KEYCHECKSUMSIZE + 1))) {
1797c478bd9Sstevel@tonic-gate 			fprintf(stderr, "%s: Malloc failure.\n", program_name);
1807c478bd9Sstevel@tonic-gate 			exit(1);
1817c478bd9Sstevel@tonic-gate 		}
1827c478bd9Sstevel@tonic-gate 
1837d1e8394SAshok Kumar T 		(void) memcpy(crypt, slist[0], HEXKEYBYTES);
1847d1e8394SAshok Kumar T 		(void) memcpy(crypt + HEXKEYBYTES, slist[0], KEYCHECKSUMSIZE);
1857c478bd9Sstevel@tonic-gate 		crypt[HEXKEYBYTES + KEYCHECKSUMSIZE] = 0;
1867d1e8394SAshok Kumar T 		xencrypt(crypt, short_login_pw);
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate 		clist[0] = crypt;
1897c478bd9Sstevel@tonic-gate 		ccount++;
1907c478bd9Sstevel@tonic-gate 	}
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate 	if (!ccount) {
1937c478bd9Sstevel@tonic-gate 		fprintf(stderr, "%s: Could not encrypt any secret keys.\n",
1947c478bd9Sstevel@tonic-gate 		    program_name);
1957c478bd9Sstevel@tonic-gate 		exit(1);
1967c478bd9Sstevel@tonic-gate 	}
1977c478bd9Sstevel@tonic-gate }
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate /* Initialize the array of public, secret, and encrypted secret keys */
2017c478bd9Sstevel@tonic-gate static void
2027c478bd9Sstevel@tonic-gate initkeylist(bool_t nomech)
2037c478bd9Sstevel@tonic-gate {
2047c478bd9Sstevel@tonic-gate 	int		mcount;
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate 	if (!nomech) {
2077c478bd9Sstevel@tonic-gate 		assert(mechs && mechs[0]);
2087d1e8394SAshok Kumar T 		for (mcount = 0; CURMECH; mcount++)
2097d1e8394SAshok Kumar T 			;
2107c478bd9Sstevel@tonic-gate 	} else
2117c478bd9Sstevel@tonic-gate 		mcount = 1;
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate 	if (!(plist = (char **)malloc(sizeof (char *) * mcount))) {
2147c478bd9Sstevel@tonic-gate 		fprintf(stderr, "%s: Malloc failure.\n", program_name);
2157c478bd9Sstevel@tonic-gate 		exit(1);
2167c478bd9Sstevel@tonic-gate 	}
2177c478bd9Sstevel@tonic-gate 	if (!(slist = (char **)malloc(sizeof (char *) * mcount))) {
2187c478bd9Sstevel@tonic-gate 		fprintf(stderr, "%s: Malloc failure.\n", program_name);
2197c478bd9Sstevel@tonic-gate 		exit(1);
2207c478bd9Sstevel@tonic-gate 	}
2217c478bd9Sstevel@tonic-gate 	if (!(clist = (char **)malloc(sizeof (char *) * mcount))) {
2227c478bd9Sstevel@tonic-gate 		fprintf(stderr, "%s: Malloc failure.\n", program_name);
2237c478bd9Sstevel@tonic-gate 		exit(1);
2247c478bd9Sstevel@tonic-gate 	}
2257c478bd9Sstevel@tonic-gate }
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate /* Retrieve public key(s) */
2297c478bd9Sstevel@tonic-gate static void
2307c478bd9Sstevel@tonic-gate getpublics()
2317c478bd9Sstevel@tonic-gate {
2327c478bd9Sstevel@tonic-gate 	int		mcount;
2337c478bd9Sstevel@tonic-gate 	int		pcount = 0;
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate 	if (mechs) {
2367c478bd9Sstevel@tonic-gate 		for (mcount = 0; CURMECH; mcount++) {
2377c478bd9Sstevel@tonic-gate 			char		*public;
2387c478bd9Sstevel@tonic-gate 			size_t		hexkeylen;
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate 			hexkeylen = ((CURMECH->keylen / 8) * 2) + 1;
2417c478bd9Sstevel@tonic-gate 			if (!(public = (char *)malloc(hexkeylen))) {
2427c478bd9Sstevel@tonic-gate 				fprintf(stderr, "%s: Malloc failure.\n",
2437c478bd9Sstevel@tonic-gate 				    program_name);
2447c478bd9Sstevel@tonic-gate 				exit(1);
2457c478bd9Sstevel@tonic-gate 			}
2467c478bd9Sstevel@tonic-gate 			if (!getpublickey_g(netname, CURMECH->keylen,
2477c478bd9Sstevel@tonic-gate 			    CURMECH->algtype, public,
2487c478bd9Sstevel@tonic-gate 			    hexkeylen)) {
2497c478bd9Sstevel@tonic-gate 				/* Could not get public key */
2507c478bd9Sstevel@tonic-gate 				fprintf(stderr,
2517c478bd9Sstevel@tonic-gate 				    "Could not get %s public key.\n",
2527c478bd9Sstevel@tonic-gate 				    VALID_ALIAS(CURMECH->alias) ?
2537c478bd9Sstevel@tonic-gate 				    CURMECH->alias : "");
2547c478bd9Sstevel@tonic-gate 				free(public);
2557c478bd9Sstevel@tonic-gate 				public = NULL;
2567c478bd9Sstevel@tonic-gate 			} else
2577c478bd9Sstevel@tonic-gate 				pcount++;
2587c478bd9Sstevel@tonic-gate 
2597c478bd9Sstevel@tonic-gate 			plist[mcount] = public;
2607c478bd9Sstevel@tonic-gate 		}
2617c478bd9Sstevel@tonic-gate 	} else {
2627c478bd9Sstevel@tonic-gate 		char		*public;
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate 		if (!(public = (char *)malloc(HEXKEYBYTES + 1))) {
2657c478bd9Sstevel@tonic-gate 			fprintf(stderr, "%s: Malloc failure.\n", program_name);
2667c478bd9Sstevel@tonic-gate 			exit(1);
2677c478bd9Sstevel@tonic-gate 		}
2687c478bd9Sstevel@tonic-gate 		if (!getpublickey(netname, public)) {
2697c478bd9Sstevel@tonic-gate 			free(public);
2707c478bd9Sstevel@tonic-gate 			public = NULL;
2717c478bd9Sstevel@tonic-gate 		} else
2727c478bd9Sstevel@tonic-gate 			pcount++;
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate 		plist[0] = public;
2757c478bd9Sstevel@tonic-gate 	}
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate 	if (!pcount) {
2787c478bd9Sstevel@tonic-gate 		fprintf(stderr, "%s: cannot get any public keys for %s.\n",
2797c478bd9Sstevel@tonic-gate 		    program_name, pw->pw_name);
2807c478bd9Sstevel@tonic-gate 		error_msg();
2817c478bd9Sstevel@tonic-gate 		fprintf(stderr,
2827c478bd9Sstevel@tonic-gate 	"Make sure that the public keys are stored in the domain %s.\n",
2837c478bd9Sstevel@tonic-gate 		    local_domain);
2847c478bd9Sstevel@tonic-gate 		exit(1);
2857c478bd9Sstevel@tonic-gate 	}
2867c478bd9Sstevel@tonic-gate }
2877c478bd9Sstevel@tonic-gate 
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate /* Generate a new set of public/secret key pair(s) */
2907c478bd9Sstevel@tonic-gate static void
2917c478bd9Sstevel@tonic-gate makenewkeys()
2927c478bd9Sstevel@tonic-gate {
2937c478bd9Sstevel@tonic-gate 	int		mcount;
2947c478bd9Sstevel@tonic-gate 
2957c478bd9Sstevel@tonic-gate 	if (mechs) {
2967c478bd9Sstevel@tonic-gate 		for (mcount = 0; CURMECH; mcount++) {
2977c478bd9Sstevel@tonic-gate 			char		*public, *secret;
2987c478bd9Sstevel@tonic-gate 			size_t		hexkeylen;
2997c478bd9Sstevel@tonic-gate 
3007c478bd9Sstevel@tonic-gate 			if (slist[mcount])
3017c478bd9Sstevel@tonic-gate 				free(slist[mcount]);
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate 			hexkeylen = ((CURMECH->keylen / 8) * 2) + 1;
3047c478bd9Sstevel@tonic-gate 
3057c478bd9Sstevel@tonic-gate 			if (!(public = malloc(hexkeylen))) {
3067c478bd9Sstevel@tonic-gate 				fprintf(stderr, "%s: Malloc failure.\n",
3077c478bd9Sstevel@tonic-gate 				    program_name);
3087c478bd9Sstevel@tonic-gate 				exit(1);
3097c478bd9Sstevel@tonic-gate 			}
3107c478bd9Sstevel@tonic-gate 			if (!(secret = malloc(hexkeylen))) {
3117c478bd9Sstevel@tonic-gate 				fprintf(stderr, "%s: Malloc failure.\n",
3127c478bd9Sstevel@tonic-gate 				    program_name);
3137c478bd9Sstevel@tonic-gate 				exit(1);
3147c478bd9Sstevel@tonic-gate 			}
3157c478bd9Sstevel@tonic-gate 
3167c478bd9Sstevel@tonic-gate 			if (!(__gen_dhkeys_g(public, secret, CURMECH->keylen,
3177d1e8394SAshok Kumar T 			    CURMECH->algtype, short_login_pw))) {
3187c478bd9Sstevel@tonic-gate 				/* Could not generate key pair */
3197c478bd9Sstevel@tonic-gate 				fprintf(stderr,
3207c478bd9Sstevel@tonic-gate 				"WARNING  Could not generate key pair %s\n",
3217c478bd9Sstevel@tonic-gate 				    VALID_ALIAS(CURMECH->alias) ?
3227c478bd9Sstevel@tonic-gate 				    CURMECH->alias : "");
3237c478bd9Sstevel@tonic-gate 				free(public);
3247c478bd9Sstevel@tonic-gate 				free(secret);
3257c478bd9Sstevel@tonic-gate 				public = NULL;
3267c478bd9Sstevel@tonic-gate 				secret = NULL;
3277c478bd9Sstevel@tonic-gate 			}
3287c478bd9Sstevel@tonic-gate 
3297c478bd9Sstevel@tonic-gate 			plist[mcount] = public;
3307c478bd9Sstevel@tonic-gate 			slist[mcount] = secret;
3317c478bd9Sstevel@tonic-gate 		}
3327c478bd9Sstevel@tonic-gate 	} else {
3337c478bd9Sstevel@tonic-gate 		char		*public, *secret;
3347c478bd9Sstevel@tonic-gate 		if (slist[0])
3357c478bd9Sstevel@tonic-gate 			free(slist[0]);
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate 		if (!(public = malloc(HEXKEYBYTES + 1))) {
3387c478bd9Sstevel@tonic-gate 			fprintf(stderr, "%s: Malloc failure.\n", program_name);
3397c478bd9Sstevel@tonic-gate 			exit(1);
3407c478bd9Sstevel@tonic-gate 		}
3417c478bd9Sstevel@tonic-gate 		if (!(secret = malloc(HEXKEYBYTES + 1))) {
3427c478bd9Sstevel@tonic-gate 			fprintf(stderr, "%s: Malloc failure.\n", program_name);
3437c478bd9Sstevel@tonic-gate 			exit(1);
3447c478bd9Sstevel@tonic-gate 		}
3457c478bd9Sstevel@tonic-gate 
3467d1e8394SAshok Kumar T 		__gen_dhkeys(public, secret, short_login_pw);
3477c478bd9Sstevel@tonic-gate 
3487c478bd9Sstevel@tonic-gate 		plist[0] = public;
3497c478bd9Sstevel@tonic-gate 		slist[0] = secret;
3507c478bd9Sstevel@tonic-gate 	}
3517c478bd9Sstevel@tonic-gate }
3527c478bd9Sstevel@tonic-gate 
3537c478bd9Sstevel@tonic-gate 
3547c478bd9Sstevel@tonic-gate /*
3557c478bd9Sstevel@tonic-gate  * Make sure that the entered Secure-RPC password(s) match the login
3567c478bd9Sstevel@tonic-gate  * password
3577c478bd9Sstevel@tonic-gate  */
3587c478bd9Sstevel@tonic-gate static void
3597c478bd9Sstevel@tonic-gate cmp_passwd()
3607c478bd9Sstevel@tonic-gate {
3617c478bd9Sstevel@tonic-gate 	char	baseprompt[] = "Please enter the login password for";
3627c478bd9Sstevel@tonic-gate 	char	prompt[BUFSIZ];
3637c478bd9Sstevel@tonic-gate 	char	*en_login_pw = spw->sp_pwdp;
3647d1e8394SAshok Kumar T 	char	short_en_login_pw[DESCREDPASSLEN + 1];
3657c478bd9Sstevel@tonic-gate 	char	*try_en_login_pw;
3667c478bd9Sstevel@tonic-gate 	bool_t	pwmatch = FALSE;
3677c478bd9Sstevel@tonic-gate 	int	done = 0, tries = 0, pcount;
3687c478bd9Sstevel@tonic-gate 
3697c478bd9Sstevel@tonic-gate 	snprintf(prompt, BUFSIZ, "%s %s:", baseprompt, pw->pw_name);
3707c478bd9Sstevel@tonic-gate 
3717d1e8394SAshok Kumar T 	(void) strlcpy(short_en_login_pw, en_login_pw,
3727d1e8394SAshok Kumar T 	    sizeof (short_en_login_pw));
3737d1e8394SAshok Kumar T 
3747c478bd9Sstevel@tonic-gate 	if (en_login_pw && (strlen(en_login_pw) != 0)) {
3757c478bd9Sstevel@tonic-gate 		for (pcount = 0; pcount < rpc_pw_count; pcount++) {
3767c478bd9Sstevel@tonic-gate 			char	*try_en_rpc_pw;
3777c478bd9Sstevel@tonic-gate 
3787d1e8394SAshok Kumar T 		try_en_rpc_pw = crypt(rpc_pws[pcount], short_en_login_pw);
3797d1e8394SAshok Kumar T 			if (strcmp(try_en_rpc_pw, short_en_login_pw) == 0) {
3807c478bd9Sstevel@tonic-gate 				login_pw = rpc_pws[pcount];
3817d1e8394SAshok Kumar T 				(void) strlcpy(short_login_pw, login_pw,
3827d1e8394SAshok Kumar T 				    sizeof (short_login_pw));
3837c478bd9Sstevel@tonic-gate 				pwmatch = TRUE;
3847c478bd9Sstevel@tonic-gate 				break;
3857c478bd9Sstevel@tonic-gate 			}
3867c478bd9Sstevel@tonic-gate 		}
3877c478bd9Sstevel@tonic-gate 		if (!pwmatch) {
3887c478bd9Sstevel@tonic-gate 			/* pw don't match */
3897c478bd9Sstevel@tonic-gate 			while (!done) {
3907c478bd9Sstevel@tonic-gate 				/* ask for the pw */
3917d1e8394SAshok Kumar T 				login_pw = getpassphrase(prompt);
3927d1e8394SAshok Kumar T 				(void) strlcpy(short_login_pw, login_pw,
3937d1e8394SAshok Kumar T 				    sizeof (short_login_pw));
3947c478bd9Sstevel@tonic-gate 				if (login_pw && strlen(login_pw)) {
3957c478bd9Sstevel@tonic-gate 					/* pw was not empty */
3967c478bd9Sstevel@tonic-gate 					try_en_login_pw = crypt(login_pw,
3977c478bd9Sstevel@tonic-gate 					    en_login_pw);
3987c478bd9Sstevel@tonic-gate 					/* compare the pw's */
3997c478bd9Sstevel@tonic-gate 					if (!(strcmp(try_en_login_pw,
4007c478bd9Sstevel@tonic-gate 					    en_login_pw))) {
4017c478bd9Sstevel@tonic-gate 						/* pw was correct */
4027c478bd9Sstevel@tonic-gate 						return;
4037c478bd9Sstevel@tonic-gate 					} else {
4047c478bd9Sstevel@tonic-gate 						/* pw was wrong */
4057c478bd9Sstevel@tonic-gate 						if (tries++) {
4067c478bd9Sstevel@tonic-gate 							/* Sorry */
4077c478bd9Sstevel@tonic-gate 							fprintf(stderr,
4087c478bd9Sstevel@tonic-gate 							    "Sorry.\n");
4097c478bd9Sstevel@tonic-gate 							exit(1);
4107c478bd9Sstevel@tonic-gate 						} else {
4117c478bd9Sstevel@tonic-gate 							/* Try again */
4127c478bd9Sstevel@tonic-gate 							snprintf(prompt,
4137c478bd9Sstevel@tonic-gate 							    BUFSIZ,
4147c478bd9Sstevel@tonic-gate 							"Try again. %s %s:",
4157c478bd9Sstevel@tonic-gate 							    baseprompt,
4167c478bd9Sstevel@tonic-gate 							    pw->pw_name);
4177c478bd9Sstevel@tonic-gate 						}
4187c478bd9Sstevel@tonic-gate 					}
4197c478bd9Sstevel@tonic-gate 				} else {
4207c478bd9Sstevel@tonic-gate 					/* pw was empty */
4217c478bd9Sstevel@tonic-gate 					if (tries++) {
4227c478bd9Sstevel@tonic-gate 						/* Unchanged */
4237c478bd9Sstevel@tonic-gate 						fprintf(stderr,
4247c478bd9Sstevel@tonic-gate 					"%s: key-pair(s) unchanged for %s.\n",
4257c478bd9Sstevel@tonic-gate 						    program_name,
4267c478bd9Sstevel@tonic-gate 						    pw->pw_name);
4277c478bd9Sstevel@tonic-gate 						exit(1);
4287c478bd9Sstevel@tonic-gate 					} else {
4297c478bd9Sstevel@tonic-gate 						/* Need a password */
4307c478bd9Sstevel@tonic-gate 						snprintf(prompt, BUFSIZ,
4317c478bd9Sstevel@tonic-gate 						"Need a password. %s %s:",
4327c478bd9Sstevel@tonic-gate 						    baseprompt,
4337c478bd9Sstevel@tonic-gate 						    pw->pw_name);
4347c478bd9Sstevel@tonic-gate 					}
4357c478bd9Sstevel@tonic-gate 				}
4367c478bd9Sstevel@tonic-gate 			}
4377c478bd9Sstevel@tonic-gate 		}
4387c478bd9Sstevel@tonic-gate 		/* pw match */
4397c478bd9Sstevel@tonic-gate 		return;
4407c478bd9Sstevel@tonic-gate 	} else {
4417c478bd9Sstevel@tonic-gate 		/* no pw found */
4427c478bd9Sstevel@tonic-gate 		fprintf(stderr,
4437c478bd9Sstevel@tonic-gate 		"%s: no passwd found for %s in the shadow passwd entry.\n",
4447c478bd9Sstevel@tonic-gate 		    program_name, pw->pw_name);
4457c478bd9Sstevel@tonic-gate 		exit(1);
4467c478bd9Sstevel@tonic-gate 	}
4477c478bd9Sstevel@tonic-gate }
4487c478bd9Sstevel@tonic-gate 
4497c478bd9Sstevel@tonic-gate 
4507c478bd9Sstevel@tonic-gate /* Prompt the user for a Secure-RPC password and store it in a cache. */
4517c478bd9Sstevel@tonic-gate static void
4527c478bd9Sstevel@tonic-gate getrpcpws(char *flavor)
4537c478bd9Sstevel@tonic-gate {
4547c478bd9Sstevel@tonic-gate 	char		*cur_pw = NULL;
4557c478bd9Sstevel@tonic-gate 	char		prompt[BUFSIZ + 1];
4567c478bd9Sstevel@tonic-gate 
4577c478bd9Sstevel@tonic-gate 	if (flavor)
4587c478bd9Sstevel@tonic-gate 		snprintf(prompt, BUFSIZ,
4597c478bd9Sstevel@tonic-gate 		    "Please enter the %s Secure-RPC password for %s:",
4607c478bd9Sstevel@tonic-gate 		    flavor, pw->pw_name);
4617c478bd9Sstevel@tonic-gate 	else
4627c478bd9Sstevel@tonic-gate 		snprintf(prompt, BUFSIZ,
4637c478bd9Sstevel@tonic-gate 		    "Please enter the Secure-RPC password for %s:",
4647c478bd9Sstevel@tonic-gate 		    pw->pw_name);
4657c478bd9Sstevel@tonic-gate 
4667c478bd9Sstevel@tonic-gate 	cur_pw = getpass(prompt);
4677c478bd9Sstevel@tonic-gate 	if (!cur_pw) {
4687c478bd9Sstevel@tonic-gate 		/* No changes */
4697c478bd9Sstevel@tonic-gate 		fprintf(stderr, "%s: key-pair(s) unchanged for %s.\n",
4707c478bd9Sstevel@tonic-gate 		    program_name, pw->pw_name);
4717c478bd9Sstevel@tonic-gate 		exit(1);
4727c478bd9Sstevel@tonic-gate 	}
4737c478bd9Sstevel@tonic-gate 
4747c478bd9Sstevel@tonic-gate 	rpc_pw_count++;
4757c478bd9Sstevel@tonic-gate 	if (!(rpc_pws =
4767c478bd9Sstevel@tonic-gate 	    (char **)realloc(rpc_pws, sizeof (char *) * rpc_pw_count))) {
4777c478bd9Sstevel@tonic-gate 		fprintf(stderr, "%s: Realloc failure.\n", program_name);
4787c478bd9Sstevel@tonic-gate 		exit(1);
4797c478bd9Sstevel@tonic-gate 	}
4807c478bd9Sstevel@tonic-gate rpc_pws[rpc_pw_count - 1] = cur_pw;
4817c478bd9Sstevel@tonic-gate }
4827c478bd9Sstevel@tonic-gate 
4837c478bd9Sstevel@tonic-gate 
4847c478bd9Sstevel@tonic-gate /* Retrieve the secret key(s) for the user and attempt to decrypt them */
4857c478bd9Sstevel@tonic-gate static void
4867c478bd9Sstevel@tonic-gate getsecrets()
4877c478bd9Sstevel@tonic-gate {
4887c478bd9Sstevel@tonic-gate 	int		mcount, scount = 0;
4897c478bd9Sstevel@tonic-gate 	int		tries = 0;
4907c478bd9Sstevel@tonic-gate 
4917c478bd9Sstevel@tonic-gate 	getrpcpws(NULL);
4927c478bd9Sstevel@tonic-gate 
4937c478bd9Sstevel@tonic-gate 	if (mechs) {
4947c478bd9Sstevel@tonic-gate 		for (mcount = 0; CURMECH; mcount++) {
4957c478bd9Sstevel@tonic-gate 			char		*secret;
4967c478bd9Sstevel@tonic-gate 			int		pcount;
4977c478bd9Sstevel@tonic-gate 			size_t		hexkeylen;
4987c478bd9Sstevel@tonic-gate 
4997c478bd9Sstevel@tonic-gate 			hexkeylen = ((CURMECH->keylen / 8) * 2) + 1;
5007c478bd9Sstevel@tonic-gate 			if (!(secret = (char *)calloc(hexkeylen,
5017c478bd9Sstevel@tonic-gate 			    sizeof (char)))) {
5027c478bd9Sstevel@tonic-gate 				fprintf(stderr, "%s: Malloc failure.\n",
5037c478bd9Sstevel@tonic-gate 				    program_name);
5047c478bd9Sstevel@tonic-gate 				exit(1);
5057c478bd9Sstevel@tonic-gate 			}
5067c478bd9Sstevel@tonic-gate 
5077c478bd9Sstevel@tonic-gate 			for (pcount = 0; pcount < rpc_pw_count; pcount++) {
5087c478bd9Sstevel@tonic-gate 				if (!getsecretkey_g(netname, CURMECH->keylen,
5097c478bd9Sstevel@tonic-gate 				    CURMECH->algtype, secret,
5107c478bd9Sstevel@tonic-gate 				    hexkeylen,
5117c478bd9Sstevel@tonic-gate 				    rpc_pws[pcount]))
5127c478bd9Sstevel@tonic-gate 					continue;
5137c478bd9Sstevel@tonic-gate 
5147c478bd9Sstevel@tonic-gate 				if (secret[0] == 0)
5157c478bd9Sstevel@tonic-gate 					continue;
5167c478bd9Sstevel@tonic-gate 				else
5177c478bd9Sstevel@tonic-gate 					break;
5187c478bd9Sstevel@tonic-gate 			}
5197c478bd9Sstevel@tonic-gate 
5207c478bd9Sstevel@tonic-gate 			tries = 0;
5217c478bd9Sstevel@tonic-gate 		getsecrets_tryagain_g:
5227c478bd9Sstevel@tonic-gate 			if (secret[0] == 0) {
5237c478bd9Sstevel@tonic-gate 				if (!tries) {
5247c478bd9Sstevel@tonic-gate 					/*
5257c478bd9Sstevel@tonic-gate 					 * No existing pw can decrypt
5267c478bd9Sstevel@tonic-gate 					 * secret key
5277c478bd9Sstevel@tonic-gate 					 */
5287c478bd9Sstevel@tonic-gate 					getrpcpws(CURMECH->alias);
5297c478bd9Sstevel@tonic-gate 					if (!getsecretkey_g(netname,
5307c478bd9Sstevel@tonic-gate 					    CURMECH->keylen,
5317c478bd9Sstevel@tonic-gate 					    CURMECH->algtype,
5327c478bd9Sstevel@tonic-gate 					    secret,
5337c478bd9Sstevel@tonic-gate 					    hexkeylen,
5347c478bd9Sstevel@tonic-gate 					    rpc_pws[pcount])) {
5357c478bd9Sstevel@tonic-gate 						/*
5367c478bd9Sstevel@tonic-gate 						 * Could not retreive
5377c478bd9Sstevel@tonic-gate 						 * secret key, abort
5387c478bd9Sstevel@tonic-gate 						 */
5397c478bd9Sstevel@tonic-gate 						free(secret);
5407c478bd9Sstevel@tonic-gate 						secret = NULL;
5417c478bd9Sstevel@tonic-gate 						goto getsecrets_abort;
5427c478bd9Sstevel@tonic-gate 					}
5437c478bd9Sstevel@tonic-gate 
5447c478bd9Sstevel@tonic-gate 					if (secret[0] == 0) {
5457c478bd9Sstevel@tonic-gate 						/* Still no go, ask again */
5467c478bd9Sstevel@tonic-gate 						free(rpc_pws[pcount]);
5477c478bd9Sstevel@tonic-gate 						rpc_pw_count--;
5487c478bd9Sstevel@tonic-gate 						tries++;
5497c478bd9Sstevel@tonic-gate 						printf("Try again. ");
5507c478bd9Sstevel@tonic-gate 						fflush(stdout);
5517c478bd9Sstevel@tonic-gate 						goto getsecrets_tryagain_g;
5527c478bd9Sstevel@tonic-gate 					} else
5537c478bd9Sstevel@tonic-gate 						scount++;
5547c478bd9Sstevel@tonic-gate 				} else {
5557c478bd9Sstevel@tonic-gate 					fprintf(stderr,
5567c478bd9Sstevel@tonic-gate 					"%s: key-pair unchanged for %s.\n",
5577c478bd9Sstevel@tonic-gate 					    program_name, pw->pw_name);
5587c478bd9Sstevel@tonic-gate 					exit(1);
5597c478bd9Sstevel@tonic-gate 				}
5607c478bd9Sstevel@tonic-gate 			} else
5617c478bd9Sstevel@tonic-gate 				scount++;
5627c478bd9Sstevel@tonic-gate 
5637c478bd9Sstevel@tonic-gate 		getsecrets_abort:
5647c478bd9Sstevel@tonic-gate 			slist[mcount] = secret;
5657c478bd9Sstevel@tonic-gate 		}
5667c478bd9Sstevel@tonic-gate 	} else {
5677c478bd9Sstevel@tonic-gate 		char		*secret = NULL;
5687c478bd9Sstevel@tonic-gate 
5697c478bd9Sstevel@tonic-gate 		if (!(secret = (char *)malloc(HEXKEYBYTES + 1))) {
5707c478bd9Sstevel@tonic-gate 			fprintf(stderr, "%s: Malloc failure.\n", program_name);
5717c478bd9Sstevel@tonic-gate 			exit(1);
5727c478bd9Sstevel@tonic-gate 		}
5737c478bd9Sstevel@tonic-gate 	getsecrets_tryagain:
5747c478bd9Sstevel@tonic-gate 		if (!getsecretkey(netname, secret, rpc_pws[0])) {
5757c478bd9Sstevel@tonic-gate 			fprintf(stderr,
5767c478bd9Sstevel@tonic-gate 			    "%s: could not get secret key for '%s'\n",
5777c478bd9Sstevel@tonic-gate 			    program_name, netname);
5787c478bd9Sstevel@tonic-gate 			exit(1);
5797c478bd9Sstevel@tonic-gate 		}
5807c478bd9Sstevel@tonic-gate 
5817c478bd9Sstevel@tonic-gate 		if (secret[0] == 0) {
5827c478bd9Sstevel@tonic-gate 			if (!tries) {
5837c478bd9Sstevel@tonic-gate 				free(rpc_pws[0]);
5847c478bd9Sstevel@tonic-gate 				rpc_pw_count = 0;
5857c478bd9Sstevel@tonic-gate 				tries++;
5867c478bd9Sstevel@tonic-gate 				printf("Try again. ");
5877c478bd9Sstevel@tonic-gate 				fflush(stdout);
5887c478bd9Sstevel@tonic-gate 				getrpcpws(NULL);
5897c478bd9Sstevel@tonic-gate 				goto getsecrets_tryagain;
5907c478bd9Sstevel@tonic-gate 			} else {
5917c478bd9Sstevel@tonic-gate 				fprintf(stderr,
5927c478bd9Sstevel@tonic-gate 				    "%s: key-pair unchanged for %s.\n",
5937c478bd9Sstevel@tonic-gate 				    program_name, pw->pw_name);
5947c478bd9Sstevel@tonic-gate 				exit(1);
5957c478bd9Sstevel@tonic-gate 			}
5967c478bd9Sstevel@tonic-gate 		}
5977c478bd9Sstevel@tonic-gate 
5987c478bd9Sstevel@tonic-gate 		slist[0] = secret;
5997c478bd9Sstevel@tonic-gate 		return;
6007c478bd9Sstevel@tonic-gate 	}
6017c478bd9Sstevel@tonic-gate 
6027c478bd9Sstevel@tonic-gate 	if (!scount) {
6037c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
6047c478bd9Sstevel@tonic-gate 		"%s: could not get nor decrypt any secret keys for '%s'\n",
6057c478bd9Sstevel@tonic-gate 		    program_name, netname);
6067c478bd9Sstevel@tonic-gate 		error_msg();
6077c478bd9Sstevel@tonic-gate 		exit(1);
6087c478bd9Sstevel@tonic-gate 	}
6097c478bd9Sstevel@tonic-gate }
6107c478bd9Sstevel@tonic-gate 
6117c478bd9Sstevel@tonic-gate 
6127c478bd9Sstevel@tonic-gate /* Register AUTH_DES secret key with keyserv */
6137c478bd9Sstevel@tonic-gate static void
6147c478bd9Sstevel@tonic-gate keylogin_des()
6157c478bd9Sstevel@tonic-gate {
6167c478bd9Sstevel@tonic-gate 	char			*secret = slist[0];
6177c478bd9Sstevel@tonic-gate 	struct key_netstarg	netst;
6187c478bd9Sstevel@tonic-gate 
6197c478bd9Sstevel@tonic-gate 	/*
6207c478bd9Sstevel@tonic-gate 	 * try to revoke the existing key/credentials, assuming
6217c478bd9Sstevel@tonic-gate 	 * one exists.  this will effectively mark "stale" any
6227c478bd9Sstevel@tonic-gate 	 * cached credientials...
6237c478bd9Sstevel@tonic-gate 	 */
6247c478bd9Sstevel@tonic-gate 	if (key_setsecret(secret) < 0) {
6257c478bd9Sstevel@tonic-gate 		return;
6267c478bd9Sstevel@tonic-gate 	}
6277c478bd9Sstevel@tonic-gate 
6287c478bd9Sstevel@tonic-gate #ifdef NFS_AUTH
6297c478bd9Sstevel@tonic-gate 	/*
6307c478bd9Sstevel@tonic-gate 	 * it looks like a credential already existed, so try and
6317c478bd9Sstevel@tonic-gate 	 * revoke any lingering Secure-NFS privledges.
6327c478bd9Sstevel@tonic-gate 	 */
6337c478bd9Sstevel@tonic-gate 
6347c478bd9Sstevel@tonic-gate 	nra.authtype = AUTH_DES;
6357c478bd9Sstevel@tonic-gate 	nra.uid = getuid();
6367c478bd9Sstevel@tonic-gate 
6377c478bd9Sstevel@tonic-gate 	if (_nfssys(NFS_REVAUTH, &nra) < 0)
6387c478bd9Sstevel@tonic-gate 		perror("Warning: NFS credentials not destroyed");
6397c478bd9Sstevel@tonic-gate #endif /* NFS_AUTH */
6407c478bd9Sstevel@tonic-gate 
6417d1e8394SAshok Kumar T 	(void) memcpy(netst.st_priv_key, secret, HEXKEYBYTES);
6427c478bd9Sstevel@tonic-gate 
6437c478bd9Sstevel@tonic-gate 	netst.st_pub_key[0] = '\0';
6447c478bd9Sstevel@tonic-gate 	netst.st_netname = strdup(netname);
6457c478bd9Sstevel@tonic-gate 
6467c478bd9Sstevel@tonic-gate 	/* do actual key login */
6477c478bd9Sstevel@tonic-gate 	if (key_setnet(&netst) < 0) {
6487c478bd9Sstevel@tonic-gate 		fprintf(stderr, "Could not set %s's secret key\n", netname);
6497c478bd9Sstevel@tonic-gate 		fprintf(stderr, "May be the keyserv is down?\n");
6507c478bd9Sstevel@tonic-gate 	}
6517c478bd9Sstevel@tonic-gate }
6527c478bd9Sstevel@tonic-gate 
6537c478bd9Sstevel@tonic-gate 
6547c478bd9Sstevel@tonic-gate /* Register a secret key with the keyserv */
6557c478bd9Sstevel@tonic-gate static void
6567c478bd9Sstevel@tonic-gate keylogin(keylen_t keylen, algtype_t algtype)
6577c478bd9Sstevel@tonic-gate {
6587c478bd9Sstevel@tonic-gate 	int	mcount;
6597c478bd9Sstevel@tonic-gate 
6607c478bd9Sstevel@tonic-gate 	if (mechs) {
6617c478bd9Sstevel@tonic-gate 		for (mcount = 0; CURMECH; mcount++) {
6627c478bd9Sstevel@tonic-gate 			if (keylen == CURMECH->keylen &&
6637c478bd9Sstevel@tonic-gate 			    algtype == CURMECH->algtype) {
6647c478bd9Sstevel@tonic-gate 				if (key_setnet_g(netname, slist[mcount],
6657c478bd9Sstevel@tonic-gate 				    CURMECH->keylen,
6667c478bd9Sstevel@tonic-gate 				    NULL, 0,
6677c478bd9Sstevel@tonic-gate 				    CURMECH->algtype)
6687c478bd9Sstevel@tonic-gate 				    < 0)
6697c478bd9Sstevel@tonic-gate 					fprintf(stderr,
6707c478bd9Sstevel@tonic-gate 					"Could not set %s's %s secret key\n",
6717c478bd9Sstevel@tonic-gate 					    netname,
6727c478bd9Sstevel@tonic-gate 					    VALID_ALIAS(CURMECH->alias) ?
6737c478bd9Sstevel@tonic-gate 					    CURMECH->alias : "");
6747c478bd9Sstevel@tonic-gate 			}
6757c478bd9Sstevel@tonic-gate 		}
6767c478bd9Sstevel@tonic-gate 	} else {
6777c478bd9Sstevel@tonic-gate 		if (keylen == 192 && algtype == 0)
6787c478bd9Sstevel@tonic-gate 			keylogin_des();
6797c478bd9Sstevel@tonic-gate 	}
6807c478bd9Sstevel@tonic-gate }
6817c478bd9Sstevel@tonic-gate 
6827c478bd9Sstevel@tonic-gate 
6837c478bd9Sstevel@tonic-gate /*
6847c478bd9Sstevel@tonic-gate  * fgets is "broken" in that if it reads a NUL character it will
6857c478bd9Sstevel@tonic-gate  * always return EOF for all reads, even when there is data left in
6867c478bd9Sstevel@tonic-gate  * the file.  This replacement can deal with NUL's in a calm, rational
6877c478bd9Sstevel@tonic-gate  * manner.
6887c478bd9Sstevel@tonic-gate  */
6897c478bd9Sstevel@tonic-gate static char *
6907c478bd9Sstevel@tonic-gate fgets_ignorenul(char *s, int n, FILE *stream)
6917c478bd9Sstevel@tonic-gate {
6927c478bd9Sstevel@tonic-gate 	int fildes = fileno(stream);
6937c478bd9Sstevel@tonic-gate 	int i = 0;
6947c478bd9Sstevel@tonic-gate 	int rs = 0;
6957c478bd9Sstevel@tonic-gate 	char c;
6967c478bd9Sstevel@tonic-gate 
6977c478bd9Sstevel@tonic-gate 	if (fildes < 0)
6987c478bd9Sstevel@tonic-gate 		return (NULL);
6997c478bd9Sstevel@tonic-gate 
7007c478bd9Sstevel@tonic-gate 	while (i < n - 1) {
7017c478bd9Sstevel@tonic-gate 		rs = read(fildes, &c, 1);
7027c478bd9Sstevel@tonic-gate 		switch (rs) {
7037c478bd9Sstevel@tonic-gate 		case 1:
7047c478bd9Sstevel@tonic-gate 			break;
7057c478bd9Sstevel@tonic-gate 		case 0:
7067c478bd9Sstevel@tonic-gate 			/* EOF */
7077c478bd9Sstevel@tonic-gate 			if (i > 0)
7087c478bd9Sstevel@tonic-gate 				s[i] = '\0';
7097c478bd9Sstevel@tonic-gate 			return (NULL);
7107c478bd9Sstevel@tonic-gate 			break;
7117c478bd9Sstevel@tonic-gate 		default:
7127c478bd9Sstevel@tonic-gate 			return (NULL);
7137c478bd9Sstevel@tonic-gate 		}
7147c478bd9Sstevel@tonic-gate 		switch (c) {
7157c478bd9Sstevel@tonic-gate 		case '\0':
7167c478bd9Sstevel@tonic-gate 			break;
7177c478bd9Sstevel@tonic-gate 		case '\n':
7187c478bd9Sstevel@tonic-gate 			s[i] = c;
7197c478bd9Sstevel@tonic-gate 			s[++i] = '\0';
7207c478bd9Sstevel@tonic-gate 			return (s);
7217c478bd9Sstevel@tonic-gate 		default:
7227c478bd9Sstevel@tonic-gate 		if (c != '\0')
7237c478bd9Sstevel@tonic-gate 			s[i++] = c;
7247c478bd9Sstevel@tonic-gate 		}
7257c478bd9Sstevel@tonic-gate 	}
7267c478bd9Sstevel@tonic-gate 	s[i] = '\0';
7277c478bd9Sstevel@tonic-gate 	return (s);
7287c478bd9Sstevel@tonic-gate }
7297c478bd9Sstevel@tonic-gate 
7307c478bd9Sstevel@tonic-gate 
7317c478bd9Sstevel@tonic-gate /* Write unencrypted secret key into root key file */
7327c478bd9Sstevel@tonic-gate static void
7337c478bd9Sstevel@tonic-gate write_rootkey(char *secret, char *flavor, keylen_t keylen, algtype_t algtype)
7347c478bd9Sstevel@tonic-gate {
7357c478bd9Sstevel@tonic-gate 	char		line[MAXROOTKEY_LINE_LEN];
7367c478bd9Sstevel@tonic-gate 	char		keyent[MAXROOTKEY_LEN];
7377c478bd9Sstevel@tonic-gate 	algtype_t	atent;
7387c478bd9Sstevel@tonic-gate 	int		rootfd, bakfd, hexkeybytes;
7397c478bd9Sstevel@tonic-gate 	bool_t		lineone = TRUE;
7407c478bd9Sstevel@tonic-gate 	bool_t		gotit = FALSE;
7417c478bd9Sstevel@tonic-gate 	FILE		*rootfile, *bakfile;
7427c478bd9Sstevel@tonic-gate 
7437c478bd9Sstevel@tonic-gate 	unlink(ROOTKEY_FILE_BACKUP);
7447c478bd9Sstevel@tonic-gate 	if ((rename(ROOTKEY_FILE, ROOTKEY_FILE_BACKUP)) < 0) {
7457c478bd9Sstevel@tonic-gate 		if ((bakfd = creat(ROOTKEY_FILE_BACKUP, 0600)) < 0) {
7467c478bd9Sstevel@tonic-gate 			perror("Could not create /etc/.rootkey.bak");
7477c478bd9Sstevel@tonic-gate 			goto rootkey_err;
7487c478bd9Sstevel@tonic-gate 		}
7497c478bd9Sstevel@tonic-gate 		close(bakfd);
7507c478bd9Sstevel@tonic-gate 	}
7517c478bd9Sstevel@tonic-gate 
7527c478bd9Sstevel@tonic-gate 	if ((rootfd = open(ROOTKEY_FILE, O_WRONLY+O_CREAT, 0600)) < 0) {
7537c478bd9Sstevel@tonic-gate 		perror("Could not open /etc/.rootkey for writing");
7547c478bd9Sstevel@tonic-gate 		fprintf(stderr,
7557c478bd9Sstevel@tonic-gate 		    "Attempting to restore original /etc/.rootkey\n");
7567c478bd9Sstevel@tonic-gate 		rename(ROOTKEY_FILE_BACKUP, ROOTKEY_FILE);
7577c478bd9Sstevel@tonic-gate 		goto rootkey_err;
7587c478bd9Sstevel@tonic-gate 	}
7597c478bd9Sstevel@tonic-gate 	if (!(rootfile = fdopen(rootfd, "w"))) {
7607c478bd9Sstevel@tonic-gate 		perror("Could not open /etc/.rootkey for writing");
7617c478bd9Sstevel@tonic-gate 		fprintf(stderr,
7627c478bd9Sstevel@tonic-gate 		    "Attempting to restore original /etc/.rootkey\n");
7637c478bd9Sstevel@tonic-gate 		close(rootfd);
7647c478bd9Sstevel@tonic-gate 		unlink(ROOTKEY_FILE);
7657c478bd9Sstevel@tonic-gate 		rename(ROOTKEY_FILE_BACKUP, ROOTKEY_FILE);
7667c478bd9Sstevel@tonic-gate 		goto rootkey_err;
7677c478bd9Sstevel@tonic-gate 	}
7687c478bd9Sstevel@tonic-gate 	if (!(bakfile = fopen(ROOTKEY_FILE_BACKUP, "r"))) {
7697c478bd9Sstevel@tonic-gate 		perror("Could not open /etc/.rootkey.bak for reading");
7707c478bd9Sstevel@tonic-gate 		fprintf(stderr,
7717c478bd9Sstevel@tonic-gate 		    "Attempting to restore original /etc/.rootkey\n");
7727c478bd9Sstevel@tonic-gate 		fclose(rootfile);
7737c478bd9Sstevel@tonic-gate 		unlink(ROOTKEY_FILE);
7747c478bd9Sstevel@tonic-gate 		rename(ROOTKEY_FILE_BACKUP, ROOTKEY_FILE);
7757c478bd9Sstevel@tonic-gate 		goto rootkey_err;
7767c478bd9Sstevel@tonic-gate 	}
7777c478bd9Sstevel@tonic-gate 
7787c478bd9Sstevel@tonic-gate 	hexkeybytes = ((keylen + 7) / 8) * 2;
7797c478bd9Sstevel@tonic-gate 
7807c478bd9Sstevel@tonic-gate 	while (fgets_ignorenul(line, MAXROOTKEY_LINE_LEN, bakfile)) {
781a0368f78Speteh 		if (sscanf(line, "%s %d", keyent, &atent) < 2) {
782a0368f78Speteh 			/*
783a0368f78Speteh 			 * No encryption algorithm found in the file
784a0368f78Speteh 			 * (atent) so default to DES.
785a0368f78Speteh 			 */
786a0368f78Speteh 			atent = AUTH_DES_ALGTYPE;
787a0368f78Speteh 		}
7887c478bd9Sstevel@tonic-gate 		/*
7897c478bd9Sstevel@tonic-gate 		 * 192-bit keys always go on the first line
7907c478bd9Sstevel@tonic-gate 		 */
7917c478bd9Sstevel@tonic-gate 		if (lineone) {
7927c478bd9Sstevel@tonic-gate 			lineone = FALSE;
7937c478bd9Sstevel@tonic-gate 			if (keylen == 192) {
7947c478bd9Sstevel@tonic-gate 				gotit = TRUE;
7957c478bd9Sstevel@tonic-gate 				fprintf(rootfile, "%s\n", secret);
7967c478bd9Sstevel@tonic-gate 			} else
7977c478bd9Sstevel@tonic-gate 				fprintf(rootfile, "%s", line);
7987c478bd9Sstevel@tonic-gate 			fflush(rootfile);
7997c478bd9Sstevel@tonic-gate 		} else {
8007c478bd9Sstevel@tonic-gate 			if ((strlen(keyent) == hexkeybytes) &&
8017c478bd9Sstevel@tonic-gate 			    (atent == algtype)) {
8027c478bd9Sstevel@tonic-gate 				/*
8037c478bd9Sstevel@tonic-gate 				 * Silently remove lines with the same
8047c478bd9Sstevel@tonic-gate 				 * keylen/algtype
8057c478bd9Sstevel@tonic-gate 				 */
8067c478bd9Sstevel@tonic-gate 				if (gotit)
8077c478bd9Sstevel@tonic-gate 					continue;
8087c478bd9Sstevel@tonic-gate 				else
8097c478bd9Sstevel@tonic-gate 					gotit = TRUE;
8107c478bd9Sstevel@tonic-gate 
8117c478bd9Sstevel@tonic-gate 				fprintf(rootfile, "%s %d\n", secret, algtype);
8127c478bd9Sstevel@tonic-gate 			} else
8137c478bd9Sstevel@tonic-gate 				fprintf(rootfile, "%s", line);
8147c478bd9Sstevel@tonic-gate 			fflush(rootfile);
8157c478bd9Sstevel@tonic-gate 		}
8167c478bd9Sstevel@tonic-gate 	}
8177c478bd9Sstevel@tonic-gate 
8187c478bd9Sstevel@tonic-gate 	/* Append key to rootkey file */
8197c478bd9Sstevel@tonic-gate 	if (!gotit) {
8207c478bd9Sstevel@tonic-gate 		if (keylen == 192)
8217c478bd9Sstevel@tonic-gate 			fprintf(rootfile, "%s\n", secret);
8227c478bd9Sstevel@tonic-gate 		else {
8237c478bd9Sstevel@tonic-gate 			if (lineone)
8247c478bd9Sstevel@tonic-gate 				fprintf(rootfile, "\n");
8257c478bd9Sstevel@tonic-gate 			fprintf(rootfile, "%s %d\n", secret, algtype);
8267c478bd9Sstevel@tonic-gate 		}
8277c478bd9Sstevel@tonic-gate 	}
8287c478bd9Sstevel@tonic-gate 	fflush(rootfile);
8297c478bd9Sstevel@tonic-gate 	fclose(rootfile);
8307c478bd9Sstevel@tonic-gate 	fclose(bakfile);
8317c478bd9Sstevel@tonic-gate 	unlink(ROOTKEY_FILE_BACKUP);
8327c478bd9Sstevel@tonic-gate 	return;
8337c478bd9Sstevel@tonic-gate 
8347c478bd9Sstevel@tonic-gate rootkey_err:
8357c478bd9Sstevel@tonic-gate 	fprintf(stderr, "WARNING: Could not write %s key to /etc/.rootkey\n",
8367c478bd9Sstevel@tonic-gate 	    flavor);
8377c478bd9Sstevel@tonic-gate }
8387c478bd9Sstevel@tonic-gate 
8397c478bd9Sstevel@tonic-gate /* Store new key information in the specified name service */
8407c478bd9Sstevel@tonic-gate static void
8417c478bd9Sstevel@tonic-gate storekeys()
8427c478bd9Sstevel@tonic-gate {
8437c478bd9Sstevel@tonic-gate 	int		mcount, ucount = 0;
8447c478bd9Sstevel@tonic-gate 	char		*ypmaster, *ypdomain = NULL, pkent[MAXPKENTLEN];
8457c478bd9Sstevel@tonic-gate 	nis_name	nis_princ;
8467c478bd9Sstevel@tonic-gate 
8477c478bd9Sstevel@tonic-gate 
8487c478bd9Sstevel@tonic-gate 	/* Setup */
8497c478bd9Sstevel@tonic-gate 	switch (dest_service) {
8507c478bd9Sstevel@tonic-gate 	case PK_LDAP:
8517c478bd9Sstevel@tonic-gate 		break;
8527c478bd9Sstevel@tonic-gate 	case PK_YP:
8537c478bd9Sstevel@tonic-gate 		yp_get_default_domain(&ypdomain);
8547c478bd9Sstevel@tonic-gate 		if (yp_master(ypdomain, PKMAP, &ypmaster) != 0) {
8557c478bd9Sstevel@tonic-gate 			fprintf(stderr,
8567c478bd9Sstevel@tonic-gate 			"%s: cannot find master of NIS publickey database\n",
8577c478bd9Sstevel@tonic-gate 			    program_name);
8587c478bd9Sstevel@tonic-gate 			exit(1);
8597c478bd9Sstevel@tonic-gate 		}
8607c478bd9Sstevel@tonic-gate 		fprintf(stdout,
8617c478bd9Sstevel@tonic-gate 		    "Sending key change request to %s ...\n", ypmaster);
8627c478bd9Sstevel@tonic-gate 		break;
8637c478bd9Sstevel@tonic-gate 	case PK_FILES:
8647c478bd9Sstevel@tonic-gate 		if (geteuid() != 0) {
8657c478bd9Sstevel@tonic-gate 			fprintf(stderr,
8667c478bd9Sstevel@tonic-gate 		"%s: non-root users cannot change their key-pair in %s\n",
8677c478bd9Sstevel@tonic-gate 			    program_name, PKFILE);
8687c478bd9Sstevel@tonic-gate 			exit(1);
8697c478bd9Sstevel@tonic-gate 		}
8707c478bd9Sstevel@tonic-gate 		break;
8717c478bd9Sstevel@tonic-gate 	default:
8727c478bd9Sstevel@tonic-gate 		fprintf(stderr,
8737c478bd9Sstevel@tonic-gate 		    "could not update; database %d unknown\n",
8747c478bd9Sstevel@tonic-gate 		    dest_service);
8757c478bd9Sstevel@tonic-gate 		exit(1);
8767c478bd9Sstevel@tonic-gate 	}
8777c478bd9Sstevel@tonic-gate 
8787c478bd9Sstevel@tonic-gate 	if (mechs) {
8797c478bd9Sstevel@tonic-gate 		for (mcount = 0; CURMECH; mcount++) {
8807c478bd9Sstevel@tonic-gate 			char		authtype[MECH_MAXATNAME];
8817c478bd9Sstevel@tonic-gate 
8827c478bd9Sstevel@tonic-gate 			if (!plist[mcount] && !clist[mcount])
8837c478bd9Sstevel@tonic-gate 				continue;
8847c478bd9Sstevel@tonic-gate 
8857c478bd9Sstevel@tonic-gate 			__nis_mechalias2authtype(CURMECH->alias, authtype,
8867c478bd9Sstevel@tonic-gate 			    MECH_MAXATNAME);
8877c478bd9Sstevel@tonic-gate 			if (!authtype) {
8887c478bd9Sstevel@tonic-gate 				fprintf(stderr,
8897c478bd9Sstevel@tonic-gate 				"Could not generate auth_type for %s.\n",
8907c478bd9Sstevel@tonic-gate 				    CURMECH->alias);
8917c478bd9Sstevel@tonic-gate 				continue;
8927c478bd9Sstevel@tonic-gate 			}
8937c478bd9Sstevel@tonic-gate 
8947c478bd9Sstevel@tonic-gate 			snprintf(pkent, MAXPKENTLEN, "%s:%s:%d",
8957c478bd9Sstevel@tonic-gate 			    plist[mcount], clist[mcount],
8967c478bd9Sstevel@tonic-gate 			    CURMECH->algtype);
8977c478bd9Sstevel@tonic-gate 
8987c478bd9Sstevel@tonic-gate 			switch (dest_service) {
8997c478bd9Sstevel@tonic-gate 			case PK_LDAP:
9007c478bd9Sstevel@tonic-gate 				if (ldap_update(CURMECH->alias, netname,
9017c478bd9Sstevel@tonic-gate 				    plist[mcount], clist[mcount],
9027c478bd9Sstevel@tonic-gate 				    login_pw))
9037c478bd9Sstevel@tonic-gate 					fprintf(stderr,
9047c478bd9Sstevel@tonic-gate 			"%s: unable to update %s key in LDAP database\n",
9057c478bd9Sstevel@tonic-gate 					    program_name, authtype);
9067c478bd9Sstevel@tonic-gate 				else
9077c478bd9Sstevel@tonic-gate 					ucount++;
9087c478bd9Sstevel@tonic-gate 				break;
9097c478bd9Sstevel@tonic-gate 
9107c478bd9Sstevel@tonic-gate 			case PK_YP:
9117c478bd9Sstevel@tonic-gate 				/* Should never get here. */
9127c478bd9Sstevel@tonic-gate 				break;
9137c478bd9Sstevel@tonic-gate 
9147c478bd9Sstevel@tonic-gate 			case PK_FILES:
9157c478bd9Sstevel@tonic-gate 				/* Should never get here. */
9167c478bd9Sstevel@tonic-gate 				break;
9177c478bd9Sstevel@tonic-gate 			}
9187c478bd9Sstevel@tonic-gate 		}
9197c478bd9Sstevel@tonic-gate 	} else {
9207c478bd9Sstevel@tonic-gate 		int	status = 0;
9217c478bd9Sstevel@tonic-gate 
9227c478bd9Sstevel@tonic-gate 		assert(plist[0] && clist[0]);
9237c478bd9Sstevel@tonic-gate 		snprintf(pkent, MAXPKENTLEN, "%s:%s", plist[0], clist[0]);
9247c478bd9Sstevel@tonic-gate 
9257c478bd9Sstevel@tonic-gate 		switch (dest_service) {
9267c478bd9Sstevel@tonic-gate 		case PK_LDAP:
9277c478bd9Sstevel@tonic-gate 			if (ldap_update("dh192-0", netname,
9287c478bd9Sstevel@tonic-gate 			    plist[0], clist[0],
9297c478bd9Sstevel@tonic-gate 			    login_pw)) {
9307c478bd9Sstevel@tonic-gate 				fprintf(stderr,
9317c478bd9Sstevel@tonic-gate 			"%s: unable to update %s key in LDAP database\n",
9327c478bd9Sstevel@tonic-gate 				    program_name);
9337c478bd9Sstevel@tonic-gate 				exit(1);
9347c478bd9Sstevel@tonic-gate 			}
9357c478bd9Sstevel@tonic-gate 			break;
9367c478bd9Sstevel@tonic-gate 
9377c478bd9Sstevel@tonic-gate 		case PK_YP:
9387c478bd9Sstevel@tonic-gate 			if (status = yp_update(ypdomain, PKMAP,
9397c478bd9Sstevel@tonic-gate 			    YPOP_STORE, netname,
9407c478bd9Sstevel@tonic-gate 			    strlen(netname), pkent,
9417c478bd9Sstevel@tonic-gate 			    strlen(pkent))) {
9427c478bd9Sstevel@tonic-gate 				fprintf(stderr,
9437c478bd9Sstevel@tonic-gate 				"%s: unable to update NIS database (%u): %s\n",
9447c478bd9Sstevel@tonic-gate 				    program_name, status,
9457c478bd9Sstevel@tonic-gate 				    yperr_string(status));
9467c478bd9Sstevel@tonic-gate 				exit(1);
9477c478bd9Sstevel@tonic-gate 			}
9487c478bd9Sstevel@tonic-gate 			break;
9497c478bd9Sstevel@tonic-gate 
9507c478bd9Sstevel@tonic-gate 		case PK_FILES:
9517c478bd9Sstevel@tonic-gate 			if (localupdate(netname, PKFILE, YPOP_STORE, pkent)) {
9527c478bd9Sstevel@tonic-gate 				fprintf(stderr,
9537c478bd9Sstevel@tonic-gate 			"%s: hence, unable to update publickey database\n",
9547c478bd9Sstevel@tonic-gate 				    program_name);
9557c478bd9Sstevel@tonic-gate 				exit(1);
9567c478bd9Sstevel@tonic-gate 			}
9577c478bd9Sstevel@tonic-gate 			break;
9587c478bd9Sstevel@tonic-gate 
9597c478bd9Sstevel@tonic-gate 		default:
9607c478bd9Sstevel@tonic-gate 			/* Should never get here */
9617c478bd9Sstevel@tonic-gate 			assert(0);
9627c478bd9Sstevel@tonic-gate 		}
9637c478bd9Sstevel@tonic-gate 		return;
9647c478bd9Sstevel@tonic-gate 	}
9657c478bd9Sstevel@tonic-gate 	if (!ucount) {
9667c478bd9Sstevel@tonic-gate 		fprintf(stderr, "%s: unable to update any key-pairs for %s.\n",
9677c478bd9Sstevel@tonic-gate 		    program_name, pw->pw_name);
9687c478bd9Sstevel@tonic-gate 		exit(1);
9697c478bd9Sstevel@tonic-gate 	}
9707c478bd9Sstevel@tonic-gate }
9717c478bd9Sstevel@tonic-gate 
9727c478bd9Sstevel@tonic-gate void
9737c478bd9Sstevel@tonic-gate addmechtolist(char *mechtype)
9747c478bd9Sstevel@tonic-gate {
9757c478bd9Sstevel@tonic-gate 	mechanism_t	**realmechlist;
9767c478bd9Sstevel@tonic-gate 	int		i;
9777c478bd9Sstevel@tonic-gate 
9787c478bd9Sstevel@tonic-gate 	if (realmechlist = __nis_get_mechanisms(FALSE)) {
9797c478bd9Sstevel@tonic-gate 		/* Match requested mech with list */
9807c478bd9Sstevel@tonic-gate 		for (i = 0; realmechlist[i]; i++) {
9817c478bd9Sstevel@tonic-gate 			if (realmechlist[i]->alias)
9827c478bd9Sstevel@tonic-gate 				if (strcmp(realmechlist[i]->alias, mechtype)
9837c478bd9Sstevel@tonic-gate 				    == 0) {
9847c478bd9Sstevel@tonic-gate 					/*
9857c478bd9Sstevel@tonic-gate 					 * Match, add it to the mechs.
9867c478bd9Sstevel@tonic-gate 					 * Don't worry about qop or
9877c478bd9Sstevel@tonic-gate 					 * secserv since they are not
9887c478bd9Sstevel@tonic-gate 					 * used by chkey.
9897c478bd9Sstevel@tonic-gate 					 */
9907c478bd9Sstevel@tonic-gate 					numspecmech++;
9917c478bd9Sstevel@tonic-gate 					if ((mechs =
9927c478bd9Sstevel@tonic-gate 					    (mechanism_t **)realloc(mechs,
9937d1e8394SAshok Kumar T 					    sizeof (mechanism_t *) *
9947d1e8394SAshok Kumar T 					    (numspecmech + 1))) == NULL) {
9957c478bd9Sstevel@tonic-gate 						perror("Can not change keys");
9967c478bd9Sstevel@tonic-gate 						exit(1);
9977c478bd9Sstevel@tonic-gate 					}
9987c478bd9Sstevel@tonic-gate 
9997c478bd9Sstevel@tonic-gate 					if ((mechs[numspecmech - 1] =
10007d1e8394SAshok Kumar T 					    (mechanism_t *)malloc(
10017d1e8394SAshok Kumar T 					    sizeof (mechanism_t))) == NULL) {
10027c478bd9Sstevel@tonic-gate 						perror("Can not change keys");
10037c478bd9Sstevel@tonic-gate 						exit(1);
10047c478bd9Sstevel@tonic-gate 					}
10057c478bd9Sstevel@tonic-gate 					if (realmechlist[i]->mechname)
10067c478bd9Sstevel@tonic-gate 					mechs[numspecmech - 1]->mechname =
10077c478bd9Sstevel@tonic-gate 					    strdup(realmechlist[i]->mechname);
10087c478bd9Sstevel@tonic-gate 					if (realmechlist[i]->alias)
10097c478bd9Sstevel@tonic-gate 					mechs[numspecmech - 1]->alias =
10107c478bd9Sstevel@tonic-gate 					    strdup(realmechlist[i]->alias);
10117c478bd9Sstevel@tonic-gate 					mechs[numspecmech - 1]->keylen =
10127c478bd9Sstevel@tonic-gate 					    realmechlist[i]->keylen;
10137c478bd9Sstevel@tonic-gate 					mechs[numspecmech - 1]->algtype =
10147c478bd9Sstevel@tonic-gate 					    realmechlist[i]->algtype;
10157c478bd9Sstevel@tonic-gate 					mechs[numspecmech] = NULL;
10167c478bd9Sstevel@tonic-gate 					__nis_release_mechanisms(realmechlist);
10177c478bd9Sstevel@tonic-gate 					return;
10187c478bd9Sstevel@tonic-gate 				}
10197c478bd9Sstevel@tonic-gate 		}
10207c478bd9Sstevel@tonic-gate 
10217c478bd9Sstevel@tonic-gate 		fprintf(stderr,
10227c478bd9Sstevel@tonic-gate 		"WARNING: Mechanism '%s' not configured, skipping...\n",
10237c478bd9Sstevel@tonic-gate 		    mechtype);
10247c478bd9Sstevel@tonic-gate 		__nis_release_mechanisms(realmechlist);
10257c478bd9Sstevel@tonic-gate 		return;
10267c478bd9Sstevel@tonic-gate 	}
10277c478bd9Sstevel@tonic-gate 	fprintf(stderr,
10287c478bd9Sstevel@tonic-gate 	"WARNING: Mechanism '%s' not configured, skipping...\n",
10297c478bd9Sstevel@tonic-gate 	    mechtype);
10307c478bd9Sstevel@tonic-gate }
10317c478bd9Sstevel@tonic-gate 
10327c478bd9Sstevel@tonic-gate 
103349e7ca49Speteh int
10347c478bd9Sstevel@tonic-gate main(int argc, char **argv)
10357c478bd9Sstevel@tonic-gate {
10367c478bd9Sstevel@tonic-gate 	int		c, mcount;
10377c478bd9Sstevel@tonic-gate 	uid_t		uid;
10387c478bd9Sstevel@tonic-gate 	uid_t		orig_euid;
10397c478bd9Sstevel@tonic-gate 	char		*service = NULL;
10407c478bd9Sstevel@tonic-gate 	program_name = argv[0];
10417c478bd9Sstevel@tonic-gate 
10427c478bd9Sstevel@tonic-gate 	mechs = __nis_get_mechanisms(FALSE);
10437c478bd9Sstevel@tonic-gate 
10447c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "fps:m:")) != -1) {
10457c478bd9Sstevel@tonic-gate 		switch (c) {
10467c478bd9Sstevel@tonic-gate 		case 'f':
10477c478bd9Sstevel@tonic-gate 			/*
10487c478bd9Sstevel@tonic-gate 			 * Not documented as of on1093.
10497c478bd9Sstevel@tonic-gate 			 * Temporarily supported
10507c478bd9Sstevel@tonic-gate 			 */
10517c478bd9Sstevel@tonic-gate 			force++;
10527c478bd9Sstevel@tonic-gate 			break;
10537c478bd9Sstevel@tonic-gate 		case 'p':
10547c478bd9Sstevel@tonic-gate 			makenew = FALSE;
10557c478bd9Sstevel@tonic-gate 			break;
10567c478bd9Sstevel@tonic-gate 		case 's':
10577c478bd9Sstevel@tonic-gate 			if (!service)
10587c478bd9Sstevel@tonic-gate 				service = strdup(optarg);
10597c478bd9Sstevel@tonic-gate 			else
10607c478bd9Sstevel@tonic-gate 				usage();
10617c478bd9Sstevel@tonic-gate 			break;
10627c478bd9Sstevel@tonic-gate 		case 'm':
10637c478bd9Sstevel@tonic-gate 			if (mechs && specmech == FALSE) {
10647c478bd9Sstevel@tonic-gate 				__nis_release_mechanisms(mechs);
10657c478bd9Sstevel@tonic-gate 				mechs = NULL;
10667c478bd9Sstevel@tonic-gate 			}
10677c478bd9Sstevel@tonic-gate 			specmech = TRUE;
10687c478bd9Sstevel@tonic-gate 			addmechtolist(optarg);
10697c478bd9Sstevel@tonic-gate 			break;
10707c478bd9Sstevel@tonic-gate 		default:
10717c478bd9Sstevel@tonic-gate 			usage();
10727c478bd9Sstevel@tonic-gate 		}
10737c478bd9Sstevel@tonic-gate 	}
10747c478bd9Sstevel@tonic-gate 
10757c478bd9Sstevel@tonic-gate 	if (optind < argc)
10767c478bd9Sstevel@tonic-gate 		usage();
10777c478bd9Sstevel@tonic-gate 
10787c478bd9Sstevel@tonic-gate 	dest_service = get_pk_source(service);
10797c478bd9Sstevel@tonic-gate 
10807c478bd9Sstevel@tonic-gate 	if (!(netname = malloc(MAXNETNAMELEN + 1))) {
10817c478bd9Sstevel@tonic-gate 		fprintf(stderr, "%s: Malloc failure.\n", program_name);
10827c478bd9Sstevel@tonic-gate 		exit(1);
10837c478bd9Sstevel@tonic-gate 	}
10847c478bd9Sstevel@tonic-gate 	if (!__getnetnamebyuid(netname, uid = getuid())) {
10857c478bd9Sstevel@tonic-gate 		fprintf(stderr, "%s: cannot generate netname for uid %d\n",
10867c478bd9Sstevel@tonic-gate 		    program_name, uid);
10877c478bd9Sstevel@tonic-gate 		exit(1);
10887c478bd9Sstevel@tonic-gate 	}
10897c478bd9Sstevel@tonic-gate 	sec_domain = strdup(strchr(netname, '@') + 1);
10907c478bd9Sstevel@tonic-gate 	getdomainname(local_domain, MAXNETNAMELEN);
10917c478bd9Sstevel@tonic-gate 
10927c478bd9Sstevel@tonic-gate 	if (makenew)
10937c478bd9Sstevel@tonic-gate 		fprintf(stdout, "Generating new key for '%s'.\n", netname);
10947c478bd9Sstevel@tonic-gate 	else
10957c478bd9Sstevel@tonic-gate 		fprintf(stdout, "Reencrypting key for '%s'.\n", netname);
10967c478bd9Sstevel@tonic-gate 
10977c478bd9Sstevel@tonic-gate 	if (mechs) {
10987c478bd9Sstevel@tonic-gate 		if (dest_service == PK_YP || dest_service == PK_FILES) {
10997c478bd9Sstevel@tonic-gate 			fprintf(stderr,
11007c478bd9Sstevel@tonic-gate 		"%s: can not add non-DES public keys to %s, skipping.\n",
11017c478bd9Sstevel@tonic-gate 			    program_name, service);
11027c478bd9Sstevel@tonic-gate 			__nis_release_mechanisms(mechs);
11037c478bd9Sstevel@tonic-gate 			mechs = NULL;
11047c478bd9Sstevel@tonic-gate 			initkeylist(TRUE);
11057c478bd9Sstevel@tonic-gate 		} else
11067c478bd9Sstevel@tonic-gate 			initkeylist(FALSE);
11077c478bd9Sstevel@tonic-gate 	} else
11087c478bd9Sstevel@tonic-gate 		initkeylist(TRUE);
11097c478bd9Sstevel@tonic-gate 
11107c478bd9Sstevel@tonic-gate 	uid = getuid();
11117c478bd9Sstevel@tonic-gate 	orig_euid = geteuid();
11127c478bd9Sstevel@tonic-gate 
11137c478bd9Sstevel@tonic-gate 	/* Get password information */
11147c478bd9Sstevel@tonic-gate 	if ((pw = getpwuid(uid)) == NULL) {
11157c478bd9Sstevel@tonic-gate 		fprintf(stderr,
11167c478bd9Sstevel@tonic-gate 		"%s: Can not find passwd information for %d.\n",
11177c478bd9Sstevel@tonic-gate 		    program_name, uid);
11187c478bd9Sstevel@tonic-gate 		exit(1);
11197c478bd9Sstevel@tonic-gate 	}
11207c478bd9Sstevel@tonic-gate 
11217c478bd9Sstevel@tonic-gate 	/* Set eUID to user */
11227c478bd9Sstevel@tonic-gate 	seteuid(uid);
11237c478bd9Sstevel@tonic-gate 
11247c478bd9Sstevel@tonic-gate 	/* Obtain a list of decrypted secret keys */
11257c478bd9Sstevel@tonic-gate 	getsecrets();
11267c478bd9Sstevel@tonic-gate 
11277c478bd9Sstevel@tonic-gate 	/* Keylogin user if not already done */
11287c478bd9Sstevel@tonic-gate 	if (mechs) {
11297c478bd9Sstevel@tonic-gate 		int mcount;
11307c478bd9Sstevel@tonic-gate 
11317c478bd9Sstevel@tonic-gate 		for (mcount = 0; CURMECH; mcount++) {
11327c478bd9Sstevel@tonic-gate 			keylen_t	keylen = CURMECH->keylen;
11337c478bd9Sstevel@tonic-gate 			algtype_t	algtype = CURMECH->algtype;
11347c478bd9Sstevel@tonic-gate 
11357c478bd9Sstevel@tonic-gate 			if (!key_secretkey_is_set_g(keylen, algtype) &&
11367c478bd9Sstevel@tonic-gate 			    slist[mcount]) {
11377c478bd9Sstevel@tonic-gate 				keylogin(CURMECH->keylen, CURMECH->algtype);
11387c478bd9Sstevel@tonic-gate 				if ((uid == 0) && (makenew == FALSE))
11397c478bd9Sstevel@tonic-gate 					write_rootkey(slist[mcount],
11407c478bd9Sstevel@tonic-gate 					    VALID_ALIAS(CURMECH->alias) ?
11417c478bd9Sstevel@tonic-gate 					    CURMECH->alias :
11427c478bd9Sstevel@tonic-gate 					    "",
11437c478bd9Sstevel@tonic-gate 					    keylen, algtype);
11447c478bd9Sstevel@tonic-gate 			}
11457c478bd9Sstevel@tonic-gate 		}
11467c478bd9Sstevel@tonic-gate 	} else {
11477c478bd9Sstevel@tonic-gate 		assert(slist[0]);
11487c478bd9Sstevel@tonic-gate 		if (!key_secretkey_is_set()) {
11497c478bd9Sstevel@tonic-gate 			keylogin_des();
11507c478bd9Sstevel@tonic-gate 			if ((uid == 0) && (makenew == FALSE))
11517c478bd9Sstevel@tonic-gate 				write_rootkey(slist[0], "des", 192, 0);
11527c478bd9Sstevel@tonic-gate 		}
11537c478bd9Sstevel@tonic-gate 	}
11547c478bd9Sstevel@tonic-gate 
11557c478bd9Sstevel@tonic-gate 	/* Set eUID back to root */
11567c478bd9Sstevel@tonic-gate 	(void) seteuid(orig_euid);
11577c478bd9Sstevel@tonic-gate 
11587c478bd9Sstevel@tonic-gate 	/*
11597c478bd9Sstevel@tonic-gate 	 * Call getspnam() after the keylogin has been done so we have
11607c478bd9Sstevel@tonic-gate 	 * the best chance of having read access to the encrypted pw.
11617c478bd9Sstevel@tonic-gate 	 *
11627c478bd9Sstevel@tonic-gate 	 * The eUID must be 0 for the getspnam() so the name service
11637c478bd9Sstevel@tonic-gate 	 * switch can handle the following eUID sensitive cases:
11647c478bd9Sstevel@tonic-gate 	 *
11657c478bd9Sstevel@tonic-gate 	 *	files/compat:	read /etc/shadow
11667c478bd9Sstevel@tonic-gate 	 *
11677c478bd9Sstevel@tonic-gate 	 */
11687c478bd9Sstevel@tonic-gate 	if ((spw = getspnam(pw->pw_name)) == 0) {
11697c478bd9Sstevel@tonic-gate 
11707c478bd9Sstevel@tonic-gate 		/* Set eUID back to user */
11717c478bd9Sstevel@tonic-gate 		(void) seteuid(uid);
11727c478bd9Sstevel@tonic-gate 
11737c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
11747c478bd9Sstevel@tonic-gate 		"%s: cannot find shadow entry for %s.\n",
11757c478bd9Sstevel@tonic-gate 		    program_name, pw->pw_name);
11767c478bd9Sstevel@tonic-gate 		exit(1);
11777c478bd9Sstevel@tonic-gate 	}
11787c478bd9Sstevel@tonic-gate 
11797c478bd9Sstevel@tonic-gate 	/* Set eUID back to user */
11807c478bd9Sstevel@tonic-gate 	(void) seteuid(uid);
11817c478bd9Sstevel@tonic-gate 
118266e150d7SJohn Sonnenschein 	if (strcmp(spw->sp_pwdp, NOPWDRTR) == 0) {
11837c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
11847c478bd9Sstevel@tonic-gate 		"%s: do not have read access to the passwd field for %s\n",
11857c478bd9Sstevel@tonic-gate 		    program_name, pw->pw_name);
11867c478bd9Sstevel@tonic-gate 		exit(1);
11877c478bd9Sstevel@tonic-gate 	}
11887c478bd9Sstevel@tonic-gate 
11897c478bd9Sstevel@tonic-gate 	/*
11907c478bd9Sstevel@tonic-gate 	 * force will be only supported for a while
11917c478bd9Sstevel@tonic-gate 	 * 	-- it is NOT documented as of s1093
11927c478bd9Sstevel@tonic-gate 	 */
11937c478bd9Sstevel@tonic-gate 	if (force) {
11947c478bd9Sstevel@tonic-gate 		char	*prompt = "Please enter New password:";
11957c478bd9Sstevel@tonic-gate 
11967d1e8394SAshok Kumar T 		login_pw = getpassphrase(prompt);
11977d1e8394SAshok Kumar T 		(void) strlcpy(short_login_pw, login_pw,
11987d1e8394SAshok Kumar T 		    sizeof (short_login_pw));
11997c478bd9Sstevel@tonic-gate 		if (!login_pw || !(strlen(login_pw))) {
12007c478bd9Sstevel@tonic-gate 			fprintf(stderr, "%s: key-pair(s) unchanged for %s.\n",
12017c478bd9Sstevel@tonic-gate 			    program_name, pw->pw_name);
12027c478bd9Sstevel@tonic-gate 			exit(1);
12037c478bd9Sstevel@tonic-gate 		}
12047c478bd9Sstevel@tonic-gate 	} else {
12057c478bd9Sstevel@tonic-gate 		/*
12067c478bd9Sstevel@tonic-gate 		 * Reconsile rpc_pws and login_pw.
12077c478bd9Sstevel@tonic-gate 		 *
12087c478bd9Sstevel@tonic-gate 		 * This function will either return with login_pw == rpc_pw
12097c478bd9Sstevel@tonic-gate 		 * (and thus, the new pw to encrypt keys) or it will exit.
12107c478bd9Sstevel@tonic-gate 		 */
12117c478bd9Sstevel@tonic-gate 		cmp_passwd();
12127c478bd9Sstevel@tonic-gate 	}
12137c478bd9Sstevel@tonic-gate 
12147c478bd9Sstevel@tonic-gate 	if (makenew)
12157c478bd9Sstevel@tonic-gate 		makenewkeys();
12167c478bd9Sstevel@tonic-gate 	else
12177c478bd9Sstevel@tonic-gate 		getpublics();
12187c478bd9Sstevel@tonic-gate 
12197c478bd9Sstevel@tonic-gate 	encryptkeys();
12207c478bd9Sstevel@tonic-gate 
12217c478bd9Sstevel@tonic-gate 	storekeys();
12227c478bd9Sstevel@tonic-gate 
12237c478bd9Sstevel@tonic-gate 	if (makenew) {
12247c478bd9Sstevel@tonic-gate 		if (uid == 0) {
12257c478bd9Sstevel@tonic-gate 			if (mechs) {
12267c478bd9Sstevel@tonic-gate 				for (mcount = 0; CURMECH; mcount++) {
12277c478bd9Sstevel@tonic-gate 					if (!slist[mcount])
12287c478bd9Sstevel@tonic-gate 						continue;
12297c478bd9Sstevel@tonic-gate 					write_rootkey(slist[mcount],
12307c478bd9Sstevel@tonic-gate 					    CURMECH->alias,
12317c478bd9Sstevel@tonic-gate 					    CURMECH->keylen,
12327c478bd9Sstevel@tonic-gate 					    CURMECH->algtype);
12337c478bd9Sstevel@tonic-gate 				}
12347c478bd9Sstevel@tonic-gate 			} else {
12357c478bd9Sstevel@tonic-gate 				assert(slist[0]);
12367c478bd9Sstevel@tonic-gate 				write_rootkey(slist[0], "des", 192, 0);
12377c478bd9Sstevel@tonic-gate 			}
12387c478bd9Sstevel@tonic-gate 		}
12397c478bd9Sstevel@tonic-gate 		if (mechs) {
12407c478bd9Sstevel@tonic-gate 			for (mcount = 0; CURMECH; mcount++)
12417c478bd9Sstevel@tonic-gate 				keylogin(CURMECH->keylen,
12427c478bd9Sstevel@tonic-gate 				    CURMECH->algtype);
12437c478bd9Sstevel@tonic-gate 		} else
12447c478bd9Sstevel@tonic-gate 			keylogin_des();
12457c478bd9Sstevel@tonic-gate 	}
124649e7ca49Speteh 	return (0);
12477c478bd9Sstevel@tonic-gate }
1248