xref: /titanic_50/usr/src/cmd/keyserv/chkey.c (revision a0368f78728e5fb66c5c72ecc0b76905897ca79d)
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
5*a0368f78Speteh  * Common Development and Distribution License (the "License").
6*a0368f78Speteh  * 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 /*
22*a0368f78Speteh  * Copyright 2006 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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate #include <assert.h>
427c478bd9Sstevel@tonic-gate #include <stdio.h>
437c478bd9Sstevel@tonic-gate #include <stdlib.h>
447c478bd9Sstevel@tonic-gate #include <string.h>
457c478bd9Sstevel@tonic-gate #include <pwd.h>
467c478bd9Sstevel@tonic-gate #include <shadow.h>
477c478bd9Sstevel@tonic-gate #include <crypt.h>
487c478bd9Sstevel@tonic-gate #include <sys/types.h>
497c478bd9Sstevel@tonic-gate #include <unistd.h>
507c478bd9Sstevel@tonic-gate #include <rpc/rpc.h>
517c478bd9Sstevel@tonic-gate #include <rpc/key_prot.h>
527c478bd9Sstevel@tonic-gate #include <rpcsvc/nis.h>
537c478bd9Sstevel@tonic-gate #include <rpcsvc/nis_dhext.h>
547c478bd9Sstevel@tonic-gate #include <rpcsvc/ypclnt.h>
557c478bd9Sstevel@tonic-gate #include <nsswitch.h>
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate #define	PK_FILES	1
587c478bd9Sstevel@tonic-gate #define	PK_YP		2
597c478bd9Sstevel@tonic-gate #define	PK_NISPLUS	3
607c478bd9Sstevel@tonic-gate #define	PK_LDAP		4
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate #define	CURMECH		mechs[mcount]
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate static char	CRED_TABLE[] = "cred.org_dir";
657c478bd9Sstevel@tonic-gate static char	PKMAP[] = "publickey.byname";
667c478bd9Sstevel@tonic-gate static char	PKFILE[] = "/etc/publickey";
677c478bd9Sstevel@tonic-gate #define	MAXHOSTNAMELEN	256
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate #define	ROOTKEY_FILE		"/etc/.rootkey"
707c478bd9Sstevel@tonic-gate #define	ROOTKEY_FILE_BACKUP	"/etc/.rootkey.bak"
717c478bd9Sstevel@tonic-gate #define	MAXROOTKEY_LINE_LEN	4224	/* Good upto 16384-bit keys */
727c478bd9Sstevel@tonic-gate #define	MAXROOTKEY_LEN		4096
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate /* Should last up to 16384-bit keys */
757c478bd9Sstevel@tonic-gate #define	MAXPKENTLEN	8500
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate bool_t		makenew = TRUE;   /* Make new keys or reencrypt existing */
787c478bd9Sstevel@tonic-gate bool_t		specmech = FALSE; /* Specific mechs requested */
797c478bd9Sstevel@tonic-gate bool_t		force = FALSE;
807c478bd9Sstevel@tonic-gate int		dest_service = 0; /* To which nameservice do we store key(s) */
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate char		*program_name;
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate mechanism_t	**mechs = NULL;   /* List of DH mechanisms */
857c478bd9Sstevel@tonic-gate char		**plist = NULL;	  /* List of public key(s) */
867c478bd9Sstevel@tonic-gate char		**slist = NULL;	  /* List of secret key(s) */
877c478bd9Sstevel@tonic-gate char		**clist = NULL;   /* List of encrypted secret key(s) */
887c478bd9Sstevel@tonic-gate int		numspecmech = 0;  /* Number of mechanisms specified */
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate struct passwd	*pw = NULL;	  /* passwd entry of user */
917c478bd9Sstevel@tonic-gate struct spwd	*spw = NULL;	  /* shadow entry of user */
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate char		*netname = NULL;  /* RPC netname of user */
947c478bd9Sstevel@tonic-gate char		local_domain[MAXNETNAMELEN + 1];
957c478bd9Sstevel@tonic-gate char		*sec_domain = NULL;
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate char		**rpc_pws = NULL; /* List of S-RPC passwords */
987c478bd9Sstevel@tonic-gate int		rpc_pw_count = 0; /* Number of passwords entered by user */
997c478bd9Sstevel@tonic-gate char		*login_pw = NULL; /* Unencrypted login password */
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate static int add_cred_obj(nis_object *, char *);
1027c478bd9Sstevel@tonic-gate static nis_error auth_exists(char *, char *, char *, char *);
1037c478bd9Sstevel@tonic-gate static void cmp_passwd();
1047c478bd9Sstevel@tonic-gate static nis_error cred_exists(const char *, const char *, const char *);
1057c478bd9Sstevel@tonic-gate static void encryptkeys();
1067c478bd9Sstevel@tonic-gate static void error_msg();
1077c478bd9Sstevel@tonic-gate static char *fgets_ignorenul();
1087c478bd9Sstevel@tonic-gate static void getpublics();
1097c478bd9Sstevel@tonic-gate static void getrpcpws();
1107c478bd9Sstevel@tonic-gate static void getsecrets();
1117c478bd9Sstevel@tonic-gate static void initkeylist(bool_t);
1127c478bd9Sstevel@tonic-gate static void keylogin(keylen_t, algtype_t);
1137c478bd9Sstevel@tonic-gate static void keylogin_des();
1147c478bd9Sstevel@tonic-gate static void makenewkeys();
1157c478bd9Sstevel@tonic-gate static int modify_cred_obj(nis_object *, char *);
1167c478bd9Sstevel@tonic-gate static int nisplus_update(nis_name, char *, char *, char *);
1177c478bd9Sstevel@tonic-gate static int sanity_checks(char *, char *, char *);
1187c478bd9Sstevel@tonic-gate static void storekeys();
1197c478bd9Sstevel@tonic-gate static void usage();
1207c478bd9Sstevel@tonic-gate static void write_rootkey();
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate extern char *get_nisplus_principal(char *, uid_t);
1237c478bd9Sstevel@tonic-gate extern nis_object *init_entry();
1247c478bd9Sstevel@tonic-gate extern int get_pk_source(char *);
1257c478bd9Sstevel@tonic-gate extern int localupdate(char *, char *, uint_t, char *);
1267c478bd9Sstevel@tonic-gate extern int xencrypt();
1277c478bd9Sstevel@tonic-gate extern int xencrypt_g();
1287c478bd9Sstevel@tonic-gate extern int __gen_dhkeys();
1297c478bd9Sstevel@tonic-gate extern int key_setnet();
1307c478bd9Sstevel@tonic-gate extern int key_setnet_g();
1317c478bd9Sstevel@tonic-gate extern int key_secretkey_is_set_g();
1327c478bd9Sstevel@tonic-gate extern int __getnetnamebyuid();
1337c478bd9Sstevel@tonic-gate extern int getdomainname();
1347c478bd9Sstevel@tonic-gate extern int ldap_update(char *, char *, char *, char *, char *);
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate static void
1387c478bd9Sstevel@tonic-gate error_msg()
1397c478bd9Sstevel@tonic-gate {
1407c478bd9Sstevel@tonic-gate 	if (sec_domain && *sec_domain &&
1417c478bd9Sstevel@tonic-gate 	    strcasecmp(sec_domain, local_domain)) {
1427c478bd9Sstevel@tonic-gate 		fprintf(stderr,
1437c478bd9Sstevel@tonic-gate "The system default domain '%s' is different from the Secure RPC\n\
1447c478bd9Sstevel@tonic-gate domain %s where the key is stored.  The Secure RPC domainname is\n\
1457c478bd9Sstevel@tonic-gate defined by the directory object stored in the /var/nis/NIS_COLD_START file.\n\
1467c478bd9Sstevel@tonic-gate If you need to change this Secure RPC domainname, please use the nisinit(1M)\n\
1477c478bd9Sstevel@tonic-gate command with the `-k` option.\n", local_domain, sec_domain);
1487c478bd9Sstevel@tonic-gate 		exit(1);
1497c478bd9Sstevel@tonic-gate 	}
1507c478bd9Sstevel@tonic-gate }
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate static void
1547c478bd9Sstevel@tonic-gate usage()
1557c478bd9Sstevel@tonic-gate {
1567c478bd9Sstevel@tonic-gate 	fprintf(stderr, "usage: %s [-p] [-s ldap | nisplus | nis | files] \n",
1577c478bd9Sstevel@tonic-gate 		program_name);
1587c478bd9Sstevel@tonic-gate 	exit(1);
1597c478bd9Sstevel@tonic-gate }
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate /* Encrypt secret key(s) with login_pw */
1637c478bd9Sstevel@tonic-gate static void
1647c478bd9Sstevel@tonic-gate encryptkeys()
1657c478bd9Sstevel@tonic-gate {
1667c478bd9Sstevel@tonic-gate 	int	mcount, ccount = 0;
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate 	if (mechs) {
1697c478bd9Sstevel@tonic-gate 		for (mcount = 0; CURMECH; mcount++) {
1707c478bd9Sstevel@tonic-gate 			char		*crypt = NULL;
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate 			if (!xencrypt_g(slist[mcount], CURMECH->keylen,
1737c478bd9Sstevel@tonic-gate 					CURMECH->algtype, login_pw, netname,
1747c478bd9Sstevel@tonic-gate 					&crypt, TRUE)) {
1757c478bd9Sstevel@tonic-gate 				/* Could not crypt key */
1767c478bd9Sstevel@tonic-gate 				crypt = NULL;
1777c478bd9Sstevel@tonic-gate 			} else
1787c478bd9Sstevel@tonic-gate 				ccount++;
1797c478bd9Sstevel@tonic-gate 			clist[mcount] = crypt;
1807c478bd9Sstevel@tonic-gate 		}
1817c478bd9Sstevel@tonic-gate 	} else {
1827c478bd9Sstevel@tonic-gate 		char		*crypt = NULL;
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate 		if (!(crypt =
1857c478bd9Sstevel@tonic-gate 			(char *)malloc(HEXKEYBYTES + KEYCHECKSUMSIZE + 1))) {
1867c478bd9Sstevel@tonic-gate 			fprintf(stderr, "%s: Malloc failure.\n", program_name);
1877c478bd9Sstevel@tonic-gate 			exit(1);
1887c478bd9Sstevel@tonic-gate 		}
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate 		memcpy(crypt, slist[0], HEXKEYBYTES);
1917c478bd9Sstevel@tonic-gate 		memcpy(crypt + HEXKEYBYTES, slist[0], KEYCHECKSUMSIZE);
1927c478bd9Sstevel@tonic-gate 		crypt[HEXKEYBYTES + KEYCHECKSUMSIZE] = 0;
1937c478bd9Sstevel@tonic-gate 		xencrypt(crypt, login_pw);
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate 		clist[0] = crypt;
1967c478bd9Sstevel@tonic-gate 		ccount++;
1977c478bd9Sstevel@tonic-gate 	}
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate 	if (!ccount) {
2007c478bd9Sstevel@tonic-gate 		fprintf(stderr, "%s: Could not encrypt any secret keys.\n",
2017c478bd9Sstevel@tonic-gate 			program_name);
2027c478bd9Sstevel@tonic-gate 		exit(1);
2037c478bd9Sstevel@tonic-gate 	}
2047c478bd9Sstevel@tonic-gate }
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate /* Initialize the array of public, secret, and encrypted secret keys */
2087c478bd9Sstevel@tonic-gate static void
2097c478bd9Sstevel@tonic-gate initkeylist(bool_t nomech)
2107c478bd9Sstevel@tonic-gate {
2117c478bd9Sstevel@tonic-gate 	int		mcount;
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate 	if (!nomech) {
2147c478bd9Sstevel@tonic-gate 		assert(mechs && mechs[0]);
2157c478bd9Sstevel@tonic-gate 		for (mcount = 0; CURMECH; mcount++);
2167c478bd9Sstevel@tonic-gate 	} else
2177c478bd9Sstevel@tonic-gate 		mcount = 1;
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate 	if (!(plist = (char **)malloc(sizeof (char *) * mcount))) {
2207c478bd9Sstevel@tonic-gate 		fprintf(stderr, "%s: Malloc failure.\n", program_name);
2217c478bd9Sstevel@tonic-gate 		exit(1);
2227c478bd9Sstevel@tonic-gate 	}
2237c478bd9Sstevel@tonic-gate 	if (!(slist = (char **)malloc(sizeof (char *) * mcount))) {
2247c478bd9Sstevel@tonic-gate 		fprintf(stderr, "%s: Malloc failure.\n", program_name);
2257c478bd9Sstevel@tonic-gate 		exit(1);
2267c478bd9Sstevel@tonic-gate 	}
2277c478bd9Sstevel@tonic-gate 	if (!(clist = (char **)malloc(sizeof (char *) * mcount))) {
2287c478bd9Sstevel@tonic-gate 		fprintf(stderr, "%s: Malloc failure.\n", program_name);
2297c478bd9Sstevel@tonic-gate 		exit(1);
2307c478bd9Sstevel@tonic-gate 	}
2317c478bd9Sstevel@tonic-gate }
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate /* Retrieve public key(s) */
2357c478bd9Sstevel@tonic-gate static void
2367c478bd9Sstevel@tonic-gate getpublics()
2377c478bd9Sstevel@tonic-gate {
2387c478bd9Sstevel@tonic-gate 	int		mcount;
2397c478bd9Sstevel@tonic-gate 	int		pcount = 0;
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate 	if (mechs) {
2427c478bd9Sstevel@tonic-gate 		for (mcount = 0; CURMECH; mcount++) {
2437c478bd9Sstevel@tonic-gate 			char		*public;
2447c478bd9Sstevel@tonic-gate 			size_t		hexkeylen;
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate 			hexkeylen = ((CURMECH->keylen / 8) * 2) + 1;
2477c478bd9Sstevel@tonic-gate 			if (!(public = (char *)malloc(hexkeylen))) {
2487c478bd9Sstevel@tonic-gate 				fprintf(stderr, "%s: Malloc failure.\n",
2497c478bd9Sstevel@tonic-gate 					program_name);
2507c478bd9Sstevel@tonic-gate 				exit(1);
2517c478bd9Sstevel@tonic-gate 			}
2527c478bd9Sstevel@tonic-gate 			if (!getpublickey_g(netname, CURMECH->keylen,
2537c478bd9Sstevel@tonic-gate 					    CURMECH->algtype, public,
2547c478bd9Sstevel@tonic-gate 					    hexkeylen)) {
2557c478bd9Sstevel@tonic-gate 				/* Could not get public key */
2567c478bd9Sstevel@tonic-gate 				fprintf(stderr,
2577c478bd9Sstevel@tonic-gate 					"Could not get %s public key.\n",
2587c478bd9Sstevel@tonic-gate 					VALID_ALIAS(CURMECH->alias) ?
2597c478bd9Sstevel@tonic-gate 					CURMECH->alias : "");
2607c478bd9Sstevel@tonic-gate 				free(public);
2617c478bd9Sstevel@tonic-gate 				public = NULL;
2627c478bd9Sstevel@tonic-gate 			} else
2637c478bd9Sstevel@tonic-gate 				pcount++;
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate 			plist[mcount] = public;
2667c478bd9Sstevel@tonic-gate 		}
2677c478bd9Sstevel@tonic-gate 	} else {
2687c478bd9Sstevel@tonic-gate 		char		*public;
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate 		if (!(public = (char *)malloc(HEXKEYBYTES + 1))) {
2717c478bd9Sstevel@tonic-gate 			fprintf(stderr, "%s: Malloc failure.\n", program_name);
2727c478bd9Sstevel@tonic-gate 			exit(1);
2737c478bd9Sstevel@tonic-gate 		}
2747c478bd9Sstevel@tonic-gate 		if (!getpublickey(netname, public)) {
2757c478bd9Sstevel@tonic-gate 			free(public);
2767c478bd9Sstevel@tonic-gate 			public = NULL;
2777c478bd9Sstevel@tonic-gate 		} else
2787c478bd9Sstevel@tonic-gate 			pcount++;
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate 		plist[0] = public;
2817c478bd9Sstevel@tonic-gate 	}
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate 	if (!pcount) {
2847c478bd9Sstevel@tonic-gate 		fprintf(stderr, "%s: cannot get any public keys for %s.\n",
2857c478bd9Sstevel@tonic-gate 			program_name, pw->pw_name);
2867c478bd9Sstevel@tonic-gate 		error_msg();
2877c478bd9Sstevel@tonic-gate 		fprintf(stderr,
2887c478bd9Sstevel@tonic-gate 	"Make sure that the public keys are stored in the domain %s.\n",
2897c478bd9Sstevel@tonic-gate 			local_domain);
2907c478bd9Sstevel@tonic-gate 		exit(1);
2917c478bd9Sstevel@tonic-gate 	}
2927c478bd9Sstevel@tonic-gate }
2937c478bd9Sstevel@tonic-gate 
2947c478bd9Sstevel@tonic-gate 
2957c478bd9Sstevel@tonic-gate /* Generate a new set of public/secret key pair(s) */
2967c478bd9Sstevel@tonic-gate static void
2977c478bd9Sstevel@tonic-gate makenewkeys()
2987c478bd9Sstevel@tonic-gate {
2997c478bd9Sstevel@tonic-gate 	int		mcount;
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate 	if (mechs) {
3027c478bd9Sstevel@tonic-gate 		for (mcount = 0; CURMECH; mcount++) {
3037c478bd9Sstevel@tonic-gate 			char		*public, *secret;
3047c478bd9Sstevel@tonic-gate 			size_t		hexkeylen;
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate 			if (slist[mcount])
3077c478bd9Sstevel@tonic-gate 				free(slist[mcount]);
3087c478bd9Sstevel@tonic-gate 
3097c478bd9Sstevel@tonic-gate 			hexkeylen = ((CURMECH->keylen / 8) * 2) + 1;
3107c478bd9Sstevel@tonic-gate 
3117c478bd9Sstevel@tonic-gate 			if (!(public = malloc(hexkeylen))) {
3127c478bd9Sstevel@tonic-gate 				fprintf(stderr, "%s: Malloc failure.\n",
3137c478bd9Sstevel@tonic-gate 					program_name);
3147c478bd9Sstevel@tonic-gate 				exit(1);
3157c478bd9Sstevel@tonic-gate 			}
3167c478bd9Sstevel@tonic-gate 			if (!(secret = malloc(hexkeylen))) {
3177c478bd9Sstevel@tonic-gate 				fprintf(stderr, "%s: Malloc failure.\n",
3187c478bd9Sstevel@tonic-gate 					program_name);
3197c478bd9Sstevel@tonic-gate 				exit(1);
3207c478bd9Sstevel@tonic-gate 			}
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate 			if (!(__gen_dhkeys_g(public, secret, CURMECH->keylen,
3237c478bd9Sstevel@tonic-gate 					CURMECH->algtype, login_pw))) {
3247c478bd9Sstevel@tonic-gate 				/* Could not generate key pair */
3257c478bd9Sstevel@tonic-gate 				fprintf(stderr,
3267c478bd9Sstevel@tonic-gate 				"WARNING  Could not generate key pair %s\n",
3277c478bd9Sstevel@tonic-gate 					VALID_ALIAS(CURMECH->alias) ?
3287c478bd9Sstevel@tonic-gate 					CURMECH->alias : "");
3297c478bd9Sstevel@tonic-gate 				free(public);
3307c478bd9Sstevel@tonic-gate 				free(secret);
3317c478bd9Sstevel@tonic-gate 				public = NULL;
3327c478bd9Sstevel@tonic-gate 				secret = NULL;
3337c478bd9Sstevel@tonic-gate 			}
3347c478bd9Sstevel@tonic-gate 
3357c478bd9Sstevel@tonic-gate 			plist[mcount] = public;
3367c478bd9Sstevel@tonic-gate 			slist[mcount] = secret;
3377c478bd9Sstevel@tonic-gate 		}
3387c478bd9Sstevel@tonic-gate 	} else {
3397c478bd9Sstevel@tonic-gate 		char		*public, *secret;
3407c478bd9Sstevel@tonic-gate 		if (slist[0])
3417c478bd9Sstevel@tonic-gate 			free(slist[0]);
3427c478bd9Sstevel@tonic-gate 
3437c478bd9Sstevel@tonic-gate 		if (!(public = malloc(HEXKEYBYTES + 1))) {
3447c478bd9Sstevel@tonic-gate 			fprintf(stderr, "%s: Malloc failure.\n", program_name);
3457c478bd9Sstevel@tonic-gate 			exit(1);
3467c478bd9Sstevel@tonic-gate 		}
3477c478bd9Sstevel@tonic-gate 		if (!(secret = malloc(HEXKEYBYTES + 1))) {
3487c478bd9Sstevel@tonic-gate 			fprintf(stderr, "%s: Malloc failure.\n", program_name);
3497c478bd9Sstevel@tonic-gate 			exit(1);
3507c478bd9Sstevel@tonic-gate 		}
3517c478bd9Sstevel@tonic-gate 
3527c478bd9Sstevel@tonic-gate 		__gen_dhkeys(public, secret, login_pw);
3537c478bd9Sstevel@tonic-gate 
3547c478bd9Sstevel@tonic-gate 		plist[0] = public;
3557c478bd9Sstevel@tonic-gate 		slist[0] = secret;
3567c478bd9Sstevel@tonic-gate 	}
3577c478bd9Sstevel@tonic-gate }
3587c478bd9Sstevel@tonic-gate 
3597c478bd9Sstevel@tonic-gate 
3607c478bd9Sstevel@tonic-gate /*
3617c478bd9Sstevel@tonic-gate  * Make sure that the entered Secure-RPC password(s) match the login
3627c478bd9Sstevel@tonic-gate  * password
3637c478bd9Sstevel@tonic-gate  */
3647c478bd9Sstevel@tonic-gate static void
3657c478bd9Sstevel@tonic-gate cmp_passwd()
3667c478bd9Sstevel@tonic-gate {
3677c478bd9Sstevel@tonic-gate 	char	baseprompt[] = "Please enter the login password for";
3687c478bd9Sstevel@tonic-gate 	char	prompt[BUFSIZ];
3697c478bd9Sstevel@tonic-gate 	char	*en_login_pw = spw->sp_pwdp;
3707c478bd9Sstevel@tonic-gate 	char	*try_en_login_pw;
3717c478bd9Sstevel@tonic-gate 	bool_t	pwmatch = FALSE;
3727c478bd9Sstevel@tonic-gate 	int	done = 0, tries = 0, pcount;
3737c478bd9Sstevel@tonic-gate 
3747c478bd9Sstevel@tonic-gate 	snprintf(prompt, BUFSIZ, "%s %s:", baseprompt, pw->pw_name);
3757c478bd9Sstevel@tonic-gate 
3767c478bd9Sstevel@tonic-gate 	if (en_login_pw && (strlen(en_login_pw) != 0)) {
3777c478bd9Sstevel@tonic-gate 		for (pcount = 0; pcount < rpc_pw_count; pcount++) {
3787c478bd9Sstevel@tonic-gate 			char	*try_en_rpc_pw;
3797c478bd9Sstevel@tonic-gate 
3807c478bd9Sstevel@tonic-gate 			try_en_rpc_pw = crypt(rpc_pws[pcount], en_login_pw);
3817c478bd9Sstevel@tonic-gate 			if (strcmp(try_en_rpc_pw, en_login_pw) == 0) {
3827c478bd9Sstevel@tonic-gate 				login_pw = rpc_pws[pcount];
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 */
3917c478bd9Sstevel@tonic-gate 				login_pw = getpass(prompt);
3927c478bd9Sstevel@tonic-gate 				if (login_pw && strlen(login_pw)) {
3937c478bd9Sstevel@tonic-gate 					/* pw was not empty */
3947c478bd9Sstevel@tonic-gate 					try_en_login_pw = crypt(login_pw,
3957c478bd9Sstevel@tonic-gate 								en_login_pw);
3967c478bd9Sstevel@tonic-gate 					/* compare the pw's */
3977c478bd9Sstevel@tonic-gate 					if (!(strcmp(try_en_login_pw,
3987c478bd9Sstevel@tonic-gate 							en_login_pw))) {
3997c478bd9Sstevel@tonic-gate 						/* pw was correct */
4007c478bd9Sstevel@tonic-gate 						return;
4017c478bd9Sstevel@tonic-gate 					} else {
4027c478bd9Sstevel@tonic-gate 						/* pw was wrong */
4037c478bd9Sstevel@tonic-gate 						if (tries++) {
4047c478bd9Sstevel@tonic-gate 							/* Sorry */
4057c478bd9Sstevel@tonic-gate 							fprintf(stderr,
4067c478bd9Sstevel@tonic-gate 								"Sorry.\n");
4077c478bd9Sstevel@tonic-gate 							exit(1);
4087c478bd9Sstevel@tonic-gate 						} else {
4097c478bd9Sstevel@tonic-gate 							/* Try again */
4107c478bd9Sstevel@tonic-gate 							snprintf(prompt,
4117c478bd9Sstevel@tonic-gate 									BUFSIZ,
4127c478bd9Sstevel@tonic-gate 							"Try again. %s %s:",
4137c478bd9Sstevel@tonic-gate 								baseprompt,
4147c478bd9Sstevel@tonic-gate 								pw->pw_name);
4157c478bd9Sstevel@tonic-gate 						}
4167c478bd9Sstevel@tonic-gate 					}
4177c478bd9Sstevel@tonic-gate 				} else {
4187c478bd9Sstevel@tonic-gate 					/* pw was empty */
4197c478bd9Sstevel@tonic-gate 					if (tries++) {
4207c478bd9Sstevel@tonic-gate 						/* Unchanged */
4217c478bd9Sstevel@tonic-gate 						fprintf(stderr,
4227c478bd9Sstevel@tonic-gate 					"%s: key-pair(s) unchanged for %s.\n",
4237c478bd9Sstevel@tonic-gate 							program_name,
4247c478bd9Sstevel@tonic-gate 							pw->pw_name);
4257c478bd9Sstevel@tonic-gate 						exit(1);
4267c478bd9Sstevel@tonic-gate 					} else {
4277c478bd9Sstevel@tonic-gate 						/* Need a password */
4287c478bd9Sstevel@tonic-gate 						snprintf(prompt, BUFSIZ,
4297c478bd9Sstevel@tonic-gate 						"Need a password. %s %s:",
4307c478bd9Sstevel@tonic-gate 								baseprompt,
4317c478bd9Sstevel@tonic-gate 								pw->pw_name);
4327c478bd9Sstevel@tonic-gate 					}
4337c478bd9Sstevel@tonic-gate 				}
4347c478bd9Sstevel@tonic-gate 			}
4357c478bd9Sstevel@tonic-gate 		}
4367c478bd9Sstevel@tonic-gate 		/* pw match */
4377c478bd9Sstevel@tonic-gate 		return;
4387c478bd9Sstevel@tonic-gate 	} else {
4397c478bd9Sstevel@tonic-gate 		/* no pw found */
4407c478bd9Sstevel@tonic-gate 		fprintf(stderr,
4417c478bd9Sstevel@tonic-gate 		"%s: no passwd found for %s in the shadow passwd entry.\n",
4427c478bd9Sstevel@tonic-gate 			program_name, pw->pw_name);
4437c478bd9Sstevel@tonic-gate 		exit(1);
4447c478bd9Sstevel@tonic-gate 	}
4457c478bd9Sstevel@tonic-gate }
4467c478bd9Sstevel@tonic-gate 
4477c478bd9Sstevel@tonic-gate 
4487c478bd9Sstevel@tonic-gate /* Prompt the user for a Secure-RPC password and store it in a cache. */
4497c478bd9Sstevel@tonic-gate static void
4507c478bd9Sstevel@tonic-gate getrpcpws(char *flavor)
4517c478bd9Sstevel@tonic-gate {
4527c478bd9Sstevel@tonic-gate 	char		*cur_pw = NULL;
4537c478bd9Sstevel@tonic-gate 	char		prompt[BUFSIZ + 1];
4547c478bd9Sstevel@tonic-gate 
4557c478bd9Sstevel@tonic-gate 	if (flavor)
4567c478bd9Sstevel@tonic-gate 		snprintf(prompt, BUFSIZ,
4577c478bd9Sstevel@tonic-gate 			"Please enter the %s Secure-RPC password for %s:",
4587c478bd9Sstevel@tonic-gate 			flavor, pw->pw_name);
4597c478bd9Sstevel@tonic-gate 	else
4607c478bd9Sstevel@tonic-gate 		snprintf(prompt, BUFSIZ,
4617c478bd9Sstevel@tonic-gate 				"Please enter the Secure-RPC password for %s:",
4627c478bd9Sstevel@tonic-gate 				pw->pw_name);
4637c478bd9Sstevel@tonic-gate 
4647c478bd9Sstevel@tonic-gate 	cur_pw = getpass(prompt);
4657c478bd9Sstevel@tonic-gate 	if (!cur_pw) {
4667c478bd9Sstevel@tonic-gate 		/* No changes */
4677c478bd9Sstevel@tonic-gate 		fprintf(stderr, "%s: key-pair(s) unchanged for %s.\n",
4687c478bd9Sstevel@tonic-gate 			program_name, pw->pw_name);
4697c478bd9Sstevel@tonic-gate 		exit(1);
4707c478bd9Sstevel@tonic-gate 	}
4717c478bd9Sstevel@tonic-gate 
4727c478bd9Sstevel@tonic-gate 	rpc_pw_count++;
4737c478bd9Sstevel@tonic-gate 	if (!(rpc_pws =
4747c478bd9Sstevel@tonic-gate 		(char **)realloc(rpc_pws, sizeof (char *) * rpc_pw_count))) {
4757c478bd9Sstevel@tonic-gate 		fprintf(stderr, "%s: Realloc failure.\n", program_name);
4767c478bd9Sstevel@tonic-gate 		exit(1);
4777c478bd9Sstevel@tonic-gate 	}
4787c478bd9Sstevel@tonic-gate rpc_pws[rpc_pw_count - 1] = cur_pw;
4797c478bd9Sstevel@tonic-gate }
4807c478bd9Sstevel@tonic-gate 
4817c478bd9Sstevel@tonic-gate 
4827c478bd9Sstevel@tonic-gate /* Retrieve the secret key(s) for the user and attempt to decrypt them */
4837c478bd9Sstevel@tonic-gate static void
4847c478bd9Sstevel@tonic-gate getsecrets()
4857c478bd9Sstevel@tonic-gate {
4867c478bd9Sstevel@tonic-gate 	int		mcount, scount = 0;
4877c478bd9Sstevel@tonic-gate 	int		tries = 0;
4887c478bd9Sstevel@tonic-gate 
4897c478bd9Sstevel@tonic-gate 	getrpcpws(NULL);
4907c478bd9Sstevel@tonic-gate 
4917c478bd9Sstevel@tonic-gate 	if (mechs) {
4927c478bd9Sstevel@tonic-gate 		for (mcount = 0; CURMECH; mcount++) {
4937c478bd9Sstevel@tonic-gate 			char		*secret;
4947c478bd9Sstevel@tonic-gate 			int		pcount;
4957c478bd9Sstevel@tonic-gate 			size_t		hexkeylen;
4967c478bd9Sstevel@tonic-gate 
4977c478bd9Sstevel@tonic-gate 			hexkeylen = ((CURMECH->keylen / 8) * 2) + 1;
4987c478bd9Sstevel@tonic-gate 			if (!(secret = (char *)calloc(hexkeylen,
4997c478bd9Sstevel@tonic-gate 							sizeof (char)))) {
5007c478bd9Sstevel@tonic-gate 				fprintf(stderr, "%s: Malloc failure.\n",
5017c478bd9Sstevel@tonic-gate 					program_name);
5027c478bd9Sstevel@tonic-gate 				exit(1);
5037c478bd9Sstevel@tonic-gate 			}
5047c478bd9Sstevel@tonic-gate 
5057c478bd9Sstevel@tonic-gate 			for (pcount = 0; pcount < rpc_pw_count; pcount++) {
5067c478bd9Sstevel@tonic-gate 				if (!getsecretkey_g(netname, CURMECH->keylen,
5077c478bd9Sstevel@tonic-gate 						    CURMECH->algtype, secret,
5087c478bd9Sstevel@tonic-gate 						    hexkeylen,
5097c478bd9Sstevel@tonic-gate 						    rpc_pws[pcount]))
5107c478bd9Sstevel@tonic-gate 					continue;
5117c478bd9Sstevel@tonic-gate 
5127c478bd9Sstevel@tonic-gate 				if (secret[0] == 0)
5137c478bd9Sstevel@tonic-gate 					continue;
5147c478bd9Sstevel@tonic-gate 				else
5157c478bd9Sstevel@tonic-gate 					break;
5167c478bd9Sstevel@tonic-gate 			}
5177c478bd9Sstevel@tonic-gate 
5187c478bd9Sstevel@tonic-gate 			tries = 0;
5197c478bd9Sstevel@tonic-gate 		getsecrets_tryagain_g:
5207c478bd9Sstevel@tonic-gate 			if (secret[0] == 0) {
5217c478bd9Sstevel@tonic-gate 				if (!tries) {
5227c478bd9Sstevel@tonic-gate 					/*
5237c478bd9Sstevel@tonic-gate 					 * No existing pw can decrypt
5247c478bd9Sstevel@tonic-gate 					 * secret key
5257c478bd9Sstevel@tonic-gate 					 */
5267c478bd9Sstevel@tonic-gate 					getrpcpws(CURMECH->alias);
5277c478bd9Sstevel@tonic-gate 					if (!getsecretkey_g(netname,
5287c478bd9Sstevel@tonic-gate 							    CURMECH->keylen,
5297c478bd9Sstevel@tonic-gate 							    CURMECH->algtype,
5307c478bd9Sstevel@tonic-gate 							    secret,
5317c478bd9Sstevel@tonic-gate 							    hexkeylen,
5327c478bd9Sstevel@tonic-gate 							    rpc_pws[pcount])) {
5337c478bd9Sstevel@tonic-gate 						/*
5347c478bd9Sstevel@tonic-gate 						 * Could not retreive
5357c478bd9Sstevel@tonic-gate 						 * secret key, abort
5367c478bd9Sstevel@tonic-gate 						 */
5377c478bd9Sstevel@tonic-gate 						free(secret);
5387c478bd9Sstevel@tonic-gate 						secret = NULL;
5397c478bd9Sstevel@tonic-gate 						goto getsecrets_abort;
5407c478bd9Sstevel@tonic-gate 					}
5417c478bd9Sstevel@tonic-gate 
5427c478bd9Sstevel@tonic-gate 					if (secret[0] == 0) {
5437c478bd9Sstevel@tonic-gate 						/* Still no go, ask again */
5447c478bd9Sstevel@tonic-gate 						free(rpc_pws[pcount]);
5457c478bd9Sstevel@tonic-gate 						rpc_pw_count--;
5467c478bd9Sstevel@tonic-gate 						tries++;
5477c478bd9Sstevel@tonic-gate 						printf("Try again. ");
5487c478bd9Sstevel@tonic-gate 						fflush(stdout);
5497c478bd9Sstevel@tonic-gate 						goto getsecrets_tryagain_g;
5507c478bd9Sstevel@tonic-gate 					} else
5517c478bd9Sstevel@tonic-gate 						scount++;
5527c478bd9Sstevel@tonic-gate 				} else {
5537c478bd9Sstevel@tonic-gate 					fprintf(stderr,
5547c478bd9Sstevel@tonic-gate 					"%s: key-pair unchanged for %s.\n",
5557c478bd9Sstevel@tonic-gate 						program_name, pw->pw_name);
5567c478bd9Sstevel@tonic-gate 					exit(1);
5577c478bd9Sstevel@tonic-gate 				}
5587c478bd9Sstevel@tonic-gate 			} else
5597c478bd9Sstevel@tonic-gate 				scount++;
5607c478bd9Sstevel@tonic-gate 
5617c478bd9Sstevel@tonic-gate 		getsecrets_abort:
5627c478bd9Sstevel@tonic-gate 			slist[mcount] = secret;
5637c478bd9Sstevel@tonic-gate 		}
5647c478bd9Sstevel@tonic-gate 	} else {
5657c478bd9Sstevel@tonic-gate 		char		*secret = NULL;
5667c478bd9Sstevel@tonic-gate 
5677c478bd9Sstevel@tonic-gate 		if (!(secret = (char *)malloc(HEXKEYBYTES + 1))) {
5687c478bd9Sstevel@tonic-gate 			fprintf(stderr, "%s: Malloc failure.\n", program_name);
5697c478bd9Sstevel@tonic-gate 			exit(1);
5707c478bd9Sstevel@tonic-gate 		}
5717c478bd9Sstevel@tonic-gate 	getsecrets_tryagain:
5727c478bd9Sstevel@tonic-gate 		if (!getsecretkey(netname, secret, rpc_pws[0])) {
5737c478bd9Sstevel@tonic-gate 			fprintf(stderr,
5747c478bd9Sstevel@tonic-gate 				"%s: could not get secret key for '%s'\n",
5757c478bd9Sstevel@tonic-gate 				program_name, netname);
5767c478bd9Sstevel@tonic-gate 			exit(1);
5777c478bd9Sstevel@tonic-gate 		}
5787c478bd9Sstevel@tonic-gate 
5797c478bd9Sstevel@tonic-gate 		if (secret[0] == 0) {
5807c478bd9Sstevel@tonic-gate 			if (!tries) {
5817c478bd9Sstevel@tonic-gate 				free(rpc_pws[0]);
5827c478bd9Sstevel@tonic-gate 				rpc_pw_count = 0;
5837c478bd9Sstevel@tonic-gate 				tries++;
5847c478bd9Sstevel@tonic-gate 				printf("Try again. ");
5857c478bd9Sstevel@tonic-gate 				fflush(stdout);
5867c478bd9Sstevel@tonic-gate 				getrpcpws(NULL);
5877c478bd9Sstevel@tonic-gate 				goto getsecrets_tryagain;
5887c478bd9Sstevel@tonic-gate 			} else {
5897c478bd9Sstevel@tonic-gate 				fprintf(stderr,
5907c478bd9Sstevel@tonic-gate 					"%s: key-pair unchanged for %s.\n",
5917c478bd9Sstevel@tonic-gate 					program_name, pw->pw_name);
5927c478bd9Sstevel@tonic-gate 				exit(1);
5937c478bd9Sstevel@tonic-gate 			}
5947c478bd9Sstevel@tonic-gate 		}
5957c478bd9Sstevel@tonic-gate 
5967c478bd9Sstevel@tonic-gate 		slist[0] = secret;
5977c478bd9Sstevel@tonic-gate 		return;
5987c478bd9Sstevel@tonic-gate 	}
5997c478bd9Sstevel@tonic-gate 
6007c478bd9Sstevel@tonic-gate 	if (!scount) {
6017c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
6027c478bd9Sstevel@tonic-gate 		"%s: could not get nor decrypt any secret keys for '%s'\n",
6037c478bd9Sstevel@tonic-gate 					program_name, netname);
6047c478bd9Sstevel@tonic-gate 		error_msg();
6057c478bd9Sstevel@tonic-gate 		exit(1);
6067c478bd9Sstevel@tonic-gate 	}
6077c478bd9Sstevel@tonic-gate }
6087c478bd9Sstevel@tonic-gate 
6097c478bd9Sstevel@tonic-gate 
6107c478bd9Sstevel@tonic-gate /* Register AUTH_DES secret key with keyserv */
6117c478bd9Sstevel@tonic-gate static void
6127c478bd9Sstevel@tonic-gate keylogin_des()
6137c478bd9Sstevel@tonic-gate {
6147c478bd9Sstevel@tonic-gate 	char			*secret = slist[0];
6157c478bd9Sstevel@tonic-gate 	struct key_netstarg	netst;
6167c478bd9Sstevel@tonic-gate 
6177c478bd9Sstevel@tonic-gate 	/*
6187c478bd9Sstevel@tonic-gate 	 * try to revoke the existing key/credentials, assuming
6197c478bd9Sstevel@tonic-gate 	 * one exists.  this will effectively mark "stale" any
6207c478bd9Sstevel@tonic-gate 	 * cached credientials...
6217c478bd9Sstevel@tonic-gate 	 */
6227c478bd9Sstevel@tonic-gate 	if (key_setsecret(secret) < 0) {
6237c478bd9Sstevel@tonic-gate 		return;
6247c478bd9Sstevel@tonic-gate 	}
6257c478bd9Sstevel@tonic-gate 
6267c478bd9Sstevel@tonic-gate #ifdef NFS_AUTH
6277c478bd9Sstevel@tonic-gate 	/*
6287c478bd9Sstevel@tonic-gate 	 * it looks like a credential already existed, so try and
6297c478bd9Sstevel@tonic-gate 	 * revoke any lingering Secure-NFS privledges.
6307c478bd9Sstevel@tonic-gate 	 */
6317c478bd9Sstevel@tonic-gate 
6327c478bd9Sstevel@tonic-gate 	nra.authtype = AUTH_DES;
6337c478bd9Sstevel@tonic-gate 	nra.uid = getuid();
6347c478bd9Sstevel@tonic-gate 
6357c478bd9Sstevel@tonic-gate 	if (_nfssys(NFS_REVAUTH, &nra) < 0)
6367c478bd9Sstevel@tonic-gate 		perror("Warning: NFS credentials not destroyed");
6377c478bd9Sstevel@tonic-gate #endif /* NFS_AUTH */
6387c478bd9Sstevel@tonic-gate 
6397c478bd9Sstevel@tonic-gate 	memcpy(netst.st_priv_key, secret, HEXKEYBYTES);
6407c478bd9Sstevel@tonic-gate 
6417c478bd9Sstevel@tonic-gate 	netst.st_pub_key[0] = '\0';
6427c478bd9Sstevel@tonic-gate 	netst.st_netname = strdup(netname);
6437c478bd9Sstevel@tonic-gate 
6447c478bd9Sstevel@tonic-gate 	/* do actual key login */
6457c478bd9Sstevel@tonic-gate 	if (key_setnet(&netst) < 0) {
6467c478bd9Sstevel@tonic-gate 		fprintf(stderr, "Could not set %s's secret key\n", netname);
6477c478bd9Sstevel@tonic-gate 		fprintf(stderr, "May be the keyserv is down?\n");
6487c478bd9Sstevel@tonic-gate 	}
6497c478bd9Sstevel@tonic-gate }
6507c478bd9Sstevel@tonic-gate 
6517c478bd9Sstevel@tonic-gate 
6527c478bd9Sstevel@tonic-gate /* Register a secret key with the keyserv */
6537c478bd9Sstevel@tonic-gate static void
6547c478bd9Sstevel@tonic-gate keylogin(keylen_t keylen, algtype_t algtype)
6557c478bd9Sstevel@tonic-gate {
6567c478bd9Sstevel@tonic-gate 	int	mcount;
6577c478bd9Sstevel@tonic-gate 
6587c478bd9Sstevel@tonic-gate 	if (mechs) {
6597c478bd9Sstevel@tonic-gate 		for (mcount = 0; CURMECH; mcount++) {
6607c478bd9Sstevel@tonic-gate 			if (keylen == CURMECH->keylen &&
6617c478bd9Sstevel@tonic-gate 			    algtype == CURMECH->algtype) {
6627c478bd9Sstevel@tonic-gate 				if (key_setnet_g(netname, slist[mcount],
6637c478bd9Sstevel@tonic-gate 							CURMECH->keylen,
6647c478bd9Sstevel@tonic-gate 							NULL, 0,
6657c478bd9Sstevel@tonic-gate 							CURMECH->algtype)
6667c478bd9Sstevel@tonic-gate 				    < 0)
6677c478bd9Sstevel@tonic-gate 					fprintf(stderr,
6687c478bd9Sstevel@tonic-gate 					"Could not set %s's %s secret key\n",
6697c478bd9Sstevel@tonic-gate 						netname,
6707c478bd9Sstevel@tonic-gate 					VALID_ALIAS(CURMECH->alias) ?
6717c478bd9Sstevel@tonic-gate 						CURMECH->alias : "");
6727c478bd9Sstevel@tonic-gate 			}
6737c478bd9Sstevel@tonic-gate 		}
6747c478bd9Sstevel@tonic-gate 	} else {
6757c478bd9Sstevel@tonic-gate 		if (keylen == 192 && algtype == 0)
6767c478bd9Sstevel@tonic-gate 			keylogin_des();
6777c478bd9Sstevel@tonic-gate 	}
6787c478bd9Sstevel@tonic-gate }
6797c478bd9Sstevel@tonic-gate 
6807c478bd9Sstevel@tonic-gate 
6817c478bd9Sstevel@tonic-gate /*
6827c478bd9Sstevel@tonic-gate  * fgets is "broken" in that if it reads a NUL character it will
6837c478bd9Sstevel@tonic-gate  * always return EOF for all reads, even when there is data left in
6847c478bd9Sstevel@tonic-gate  * the file.  This replacement can deal with NUL's in a calm, rational
6857c478bd9Sstevel@tonic-gate  * manner.
6867c478bd9Sstevel@tonic-gate  */
6877c478bd9Sstevel@tonic-gate static char *
6887c478bd9Sstevel@tonic-gate fgets_ignorenul(char *s, int n, FILE *stream)
6897c478bd9Sstevel@tonic-gate {
6907c478bd9Sstevel@tonic-gate 	int fildes = fileno(stream);
6917c478bd9Sstevel@tonic-gate 	int i = 0;
6927c478bd9Sstevel@tonic-gate 	int rs = 0;
6937c478bd9Sstevel@tonic-gate 	char c;
6947c478bd9Sstevel@tonic-gate 
6957c478bd9Sstevel@tonic-gate 	if (fildes < 0)
6967c478bd9Sstevel@tonic-gate 		return (NULL);
6977c478bd9Sstevel@tonic-gate 
6987c478bd9Sstevel@tonic-gate 	while (i < n - 1) {
6997c478bd9Sstevel@tonic-gate 		rs = read(fildes, &c, 1);
7007c478bd9Sstevel@tonic-gate 		switch (rs) {
7017c478bd9Sstevel@tonic-gate 		case 1:
7027c478bd9Sstevel@tonic-gate 			break;
7037c478bd9Sstevel@tonic-gate 		case 0:
7047c478bd9Sstevel@tonic-gate 			/* EOF */
7057c478bd9Sstevel@tonic-gate 			if (i > 0)
7067c478bd9Sstevel@tonic-gate 				s[i] = '\0';
7077c478bd9Sstevel@tonic-gate 			return (NULL);
7087c478bd9Sstevel@tonic-gate 			break;
7097c478bd9Sstevel@tonic-gate 		default:
7107c478bd9Sstevel@tonic-gate 			return (NULL);
7117c478bd9Sstevel@tonic-gate 		}
7127c478bd9Sstevel@tonic-gate 		switch (c) {
7137c478bd9Sstevel@tonic-gate 		case '\0':
7147c478bd9Sstevel@tonic-gate 			break;
7157c478bd9Sstevel@tonic-gate 		case '\n':
7167c478bd9Sstevel@tonic-gate 			s[i] = c;
7177c478bd9Sstevel@tonic-gate 			s[++i] = '\0';
7187c478bd9Sstevel@tonic-gate 			return (s);
7197c478bd9Sstevel@tonic-gate 		default:
7207c478bd9Sstevel@tonic-gate 		if (c != '\0')
7217c478bd9Sstevel@tonic-gate 			s[i++] = c;
7227c478bd9Sstevel@tonic-gate 		}
7237c478bd9Sstevel@tonic-gate 	}
7247c478bd9Sstevel@tonic-gate 	s[i] = '\0';
7257c478bd9Sstevel@tonic-gate 	return (s);
7267c478bd9Sstevel@tonic-gate }
7277c478bd9Sstevel@tonic-gate 
7287c478bd9Sstevel@tonic-gate 
7297c478bd9Sstevel@tonic-gate /* Write unencrypted secret key into root key file */
7307c478bd9Sstevel@tonic-gate static void
7317c478bd9Sstevel@tonic-gate write_rootkey(char *secret, char *flavor, keylen_t keylen, algtype_t algtype)
7327c478bd9Sstevel@tonic-gate {
7337c478bd9Sstevel@tonic-gate 	char		line[MAXROOTKEY_LINE_LEN];
7347c478bd9Sstevel@tonic-gate 	char		keyent[MAXROOTKEY_LEN];
7357c478bd9Sstevel@tonic-gate 	algtype_t	atent;
7367c478bd9Sstevel@tonic-gate 	int		rootfd, bakfd, hexkeybytes;
7377c478bd9Sstevel@tonic-gate 	bool_t		lineone = TRUE;
7387c478bd9Sstevel@tonic-gate 	bool_t		gotit = FALSE;
7397c478bd9Sstevel@tonic-gate 	FILE		*rootfile, *bakfile;
7407c478bd9Sstevel@tonic-gate 
7417c478bd9Sstevel@tonic-gate 	unlink(ROOTKEY_FILE_BACKUP);
7427c478bd9Sstevel@tonic-gate 	if ((rename(ROOTKEY_FILE, ROOTKEY_FILE_BACKUP)) < 0) {
7437c478bd9Sstevel@tonic-gate 		if ((bakfd = creat(ROOTKEY_FILE_BACKUP, 0600)) < 0) {
7447c478bd9Sstevel@tonic-gate 			perror("Could not create /etc/.rootkey.bak");
7457c478bd9Sstevel@tonic-gate 			goto rootkey_err;
7467c478bd9Sstevel@tonic-gate 		}
7477c478bd9Sstevel@tonic-gate 		close(bakfd);
7487c478bd9Sstevel@tonic-gate 	}
7497c478bd9Sstevel@tonic-gate 
7507c478bd9Sstevel@tonic-gate 	if ((rootfd = open(ROOTKEY_FILE, O_WRONLY+O_CREAT, 0600)) < 0) {
7517c478bd9Sstevel@tonic-gate 		perror("Could not open /etc/.rootkey for writing");
7527c478bd9Sstevel@tonic-gate 		fprintf(stderr,
7537c478bd9Sstevel@tonic-gate 			"Attempting to restore original /etc/.rootkey\n");
7547c478bd9Sstevel@tonic-gate 		rename(ROOTKEY_FILE_BACKUP, ROOTKEY_FILE);
7557c478bd9Sstevel@tonic-gate 		goto rootkey_err;
7567c478bd9Sstevel@tonic-gate 	}
7577c478bd9Sstevel@tonic-gate 	if (!(rootfile = fdopen(rootfd, "w"))) {
7587c478bd9Sstevel@tonic-gate 		perror("Could not open /etc/.rootkey for writing");
7597c478bd9Sstevel@tonic-gate 		fprintf(stderr,
7607c478bd9Sstevel@tonic-gate 			"Attempting to restore original /etc/.rootkey\n");
7617c478bd9Sstevel@tonic-gate 		close(rootfd);
7627c478bd9Sstevel@tonic-gate 		unlink(ROOTKEY_FILE);
7637c478bd9Sstevel@tonic-gate 		rename(ROOTKEY_FILE_BACKUP, ROOTKEY_FILE);
7647c478bd9Sstevel@tonic-gate 		goto rootkey_err;
7657c478bd9Sstevel@tonic-gate 	}
7667c478bd9Sstevel@tonic-gate 	if (!(bakfile = fopen(ROOTKEY_FILE_BACKUP, "r"))) {
7677c478bd9Sstevel@tonic-gate 		perror("Could not open /etc/.rootkey.bak for reading");
7687c478bd9Sstevel@tonic-gate 		fprintf(stderr,
7697c478bd9Sstevel@tonic-gate 			"Attempting to restore original /etc/.rootkey\n");
7707c478bd9Sstevel@tonic-gate 		fclose(rootfile);
7717c478bd9Sstevel@tonic-gate 		unlink(ROOTKEY_FILE);
7727c478bd9Sstevel@tonic-gate 		rename(ROOTKEY_FILE_BACKUP, ROOTKEY_FILE);
7737c478bd9Sstevel@tonic-gate 		goto rootkey_err;
7747c478bd9Sstevel@tonic-gate 	}
7757c478bd9Sstevel@tonic-gate 
7767c478bd9Sstevel@tonic-gate 	hexkeybytes = ((keylen + 7) / 8) * 2;
7777c478bd9Sstevel@tonic-gate 
7787c478bd9Sstevel@tonic-gate 	while (fgets_ignorenul(line, MAXROOTKEY_LINE_LEN, bakfile)) {
779*a0368f78Speteh 		if (sscanf(line, "%s %d", keyent, &atent) < 2) {
780*a0368f78Speteh 			/*
781*a0368f78Speteh 			 * No encryption algorithm found in the file
782*a0368f78Speteh 			 * (atent) so default to DES.
783*a0368f78Speteh 			 */
784*a0368f78Speteh 			atent = AUTH_DES_ALGTYPE;
785*a0368f78Speteh 		}
7867c478bd9Sstevel@tonic-gate 		/*
7877c478bd9Sstevel@tonic-gate 		 * 192-bit keys always go on the first line
7887c478bd9Sstevel@tonic-gate 		 */
7897c478bd9Sstevel@tonic-gate 		if (lineone) {
7907c478bd9Sstevel@tonic-gate 			lineone = FALSE;
7917c478bd9Sstevel@tonic-gate 			if (keylen == 192) {
7927c478bd9Sstevel@tonic-gate 				gotit = TRUE;
7937c478bd9Sstevel@tonic-gate 				fprintf(rootfile, "%s\n", secret);
7947c478bd9Sstevel@tonic-gate 			} else
7957c478bd9Sstevel@tonic-gate 				fprintf(rootfile, "%s", line);
7967c478bd9Sstevel@tonic-gate 			fflush(rootfile);
7977c478bd9Sstevel@tonic-gate 		} else {
7987c478bd9Sstevel@tonic-gate 			if ((strlen(keyent) == hexkeybytes) &&
7997c478bd9Sstevel@tonic-gate 			    (atent == algtype)) {
8007c478bd9Sstevel@tonic-gate 				/*
8017c478bd9Sstevel@tonic-gate 				 * Silently remove lines with the same
8027c478bd9Sstevel@tonic-gate 				 * keylen/algtype
8037c478bd9Sstevel@tonic-gate 				 */
8047c478bd9Sstevel@tonic-gate 				if (gotit)
8057c478bd9Sstevel@tonic-gate 					continue;
8067c478bd9Sstevel@tonic-gate 				else
8077c478bd9Sstevel@tonic-gate 					gotit = TRUE;
8087c478bd9Sstevel@tonic-gate 
8097c478bd9Sstevel@tonic-gate 				fprintf(rootfile, "%s %d\n", secret, algtype);
8107c478bd9Sstevel@tonic-gate 			} else
8117c478bd9Sstevel@tonic-gate 				fprintf(rootfile, "%s", line);
8127c478bd9Sstevel@tonic-gate 			fflush(rootfile);
8137c478bd9Sstevel@tonic-gate 		}
8147c478bd9Sstevel@tonic-gate 	}
8157c478bd9Sstevel@tonic-gate 
8167c478bd9Sstevel@tonic-gate 	/* Append key to rootkey file */
8177c478bd9Sstevel@tonic-gate 	if (!gotit) {
8187c478bd9Sstevel@tonic-gate 		if (keylen == 192)
8197c478bd9Sstevel@tonic-gate 			fprintf(rootfile, "%s\n", secret);
8207c478bd9Sstevel@tonic-gate 		else {
8217c478bd9Sstevel@tonic-gate 			if (lineone)
8227c478bd9Sstevel@tonic-gate 				fprintf(rootfile, "\n");
8237c478bd9Sstevel@tonic-gate 			fprintf(rootfile, "%s %d\n", secret, algtype);
8247c478bd9Sstevel@tonic-gate 		}
8257c478bd9Sstevel@tonic-gate 	}
8267c478bd9Sstevel@tonic-gate 	fflush(rootfile);
8277c478bd9Sstevel@tonic-gate 	fclose(rootfile);
8287c478bd9Sstevel@tonic-gate 	fclose(bakfile);
8297c478bd9Sstevel@tonic-gate 	unlink(ROOTKEY_FILE_BACKUP);
8307c478bd9Sstevel@tonic-gate 	return;
8317c478bd9Sstevel@tonic-gate 
8327c478bd9Sstevel@tonic-gate rootkey_err:
8337c478bd9Sstevel@tonic-gate 	fprintf(stderr, "WARNING: Could not write %s key to /etc/.rootkey\n",
8347c478bd9Sstevel@tonic-gate 		flavor);
8357c478bd9Sstevel@tonic-gate }
8367c478bd9Sstevel@tonic-gate 
8377c478bd9Sstevel@tonic-gate 
8387c478bd9Sstevel@tonic-gate /* Returns 0 if check fails; 1 if successful. */
8397c478bd9Sstevel@tonic-gate static int
8407c478bd9Sstevel@tonic-gate sanity_checks(char *nis_princ, char *domain, char *authtype)
8417c478bd9Sstevel@tonic-gate {
8427c478bd9Sstevel@tonic-gate 	char	netdomainaux[MAXHOSTNAMELEN+1];
8437c478bd9Sstevel@tonic-gate 	char	*princdomain, *netdomain;
8447c478bd9Sstevel@tonic-gate 	int	len;
8457c478bd9Sstevel@tonic-gate 
8467c478bd9Sstevel@tonic-gate 	/* Sanity check 0. Do we have a nis+ principal name to work with? */
8477c478bd9Sstevel@tonic-gate 	if (nis_princ == NULL) {
8487c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
8497c478bd9Sstevel@tonic-gate 		"%s: you must create a \"LOCAL\" credential for '%s' first.\n",
8507c478bd9Sstevel@tonic-gate 			program_name, netname);
8517c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "\tSee nisaddcred(1).\n");
8527c478bd9Sstevel@tonic-gate 		return (0);
8537c478bd9Sstevel@tonic-gate 	}
8547c478bd9Sstevel@tonic-gate 
8557c478bd9Sstevel@tonic-gate 	/* Sanity check 0.5.  NIS+ principal names must be dotted. */
8567c478bd9Sstevel@tonic-gate 	len = strlen(nis_princ);
8577c478bd9Sstevel@tonic-gate 	if (nis_princ[len-1] != '.') {
8587c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
8597c478bd9Sstevel@tonic-gate 		"%s: invalid principal name: '%s' (forgot ending dot?).\n",
8607c478bd9Sstevel@tonic-gate 			program_name, nis_princ);
8617c478bd9Sstevel@tonic-gate 		return (0);
8627c478bd9Sstevel@tonic-gate 	}
8637c478bd9Sstevel@tonic-gate 
8647c478bd9Sstevel@tonic-gate 	/* Sanity check 1.  We only deal with one type of netnames. */
8657c478bd9Sstevel@tonic-gate 	if (strncmp(netname, "unix", 4) != 0) {
8667c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
8677c478bd9Sstevel@tonic-gate 			"%s: unrecognized netname type: '%s'.\n",
8687c478bd9Sstevel@tonic-gate 			program_name, netname);
8697c478bd9Sstevel@tonic-gate 		return (0);
8707c478bd9Sstevel@tonic-gate 	}
8717c478bd9Sstevel@tonic-gate 
8727c478bd9Sstevel@tonic-gate 	/* Sanity check 2.  Should only add DES cred in home domain. */
8737c478bd9Sstevel@tonic-gate 	princdomain = nis_domain_of(nis_princ);
8747c478bd9Sstevel@tonic-gate 	if (strcasecmp(princdomain, domain) != 0) {
8757c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
8767c478bd9Sstevel@tonic-gate "%s: domain of principal '%s' does not match destination domain '%s'.\n",
8777c478bd9Sstevel@tonic-gate 			program_name, nis_princ, domain);
8787c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
8797c478bd9Sstevel@tonic-gate 	"Should only add DES credential of principal in its home domain\n");
8807c478bd9Sstevel@tonic-gate 		return (0);
8817c478bd9Sstevel@tonic-gate 	}
8827c478bd9Sstevel@tonic-gate 
8837c478bd9Sstevel@tonic-gate 	/*
8847c478bd9Sstevel@tonic-gate 	 * Sanity check 3:  Make sure netname's domain same as principal's
8857c478bd9Sstevel@tonic-gate 	 * and don't have extraneous dot at the end.
8867c478bd9Sstevel@tonic-gate 	 */
8877c478bd9Sstevel@tonic-gate 	netdomain = (char *)strchr(netname, '@');
8887c478bd9Sstevel@tonic-gate 	if (! netdomain || netname[strlen(netname)-1] == '.') {
8897c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: invalid netname: '%s'. \n",
8907c478bd9Sstevel@tonic-gate 			program_name, netname);
8917c478bd9Sstevel@tonic-gate 		return (0);
8927c478bd9Sstevel@tonic-gate 	}
8937c478bd9Sstevel@tonic-gate 	netdomain++; /* skip '@' */
8947c478bd9Sstevel@tonic-gate 
8957c478bd9Sstevel@tonic-gate 	if (strlcpy(netdomainaux, netdomain, sizeof (netdomainaux)) >=
8967c478bd9Sstevel@tonic-gate 	    sizeof (netdomainaux)) {
8977c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: net domain name %s is too long\n",
8987c478bd9Sstevel@tonic-gate 			    program_name, netdomain);
8997c478bd9Sstevel@tonic-gate 		return (0);
9007c478bd9Sstevel@tonic-gate 	}
9017c478bd9Sstevel@tonic-gate 
9027c478bd9Sstevel@tonic-gate 	if (netdomainaux[strlen(netdomainaux) - 1] != '.') {
9037c478bd9Sstevel@tonic-gate 		if (strlcat(netdomainaux, ".", sizeof (netdomainaux)) >=
9047c478bd9Sstevel@tonic-gate 		    sizeof (netdomainaux)) {
9057c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
9067c478bd9Sstevel@tonic-gate 				    "%s: net domain name %s is too long\n",
9077c478bd9Sstevel@tonic-gate 				    program_name, netdomainaux);
9087c478bd9Sstevel@tonic-gate 			return (0);
9097c478bd9Sstevel@tonic-gate 		}
9107c478bd9Sstevel@tonic-gate 	}
9117c478bd9Sstevel@tonic-gate 
9127c478bd9Sstevel@tonic-gate 	if (strcasecmp(princdomain, netdomainaux) != 0) {
9137c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
9147c478bd9Sstevel@tonic-gate 	"%s: domain of netname %s should be same as that of principal %s\n",
9157c478bd9Sstevel@tonic-gate 			program_name, netname, nis_princ);
9167c478bd9Sstevel@tonic-gate 		return (0);
9177c478bd9Sstevel@tonic-gate 	}
9187c478bd9Sstevel@tonic-gate 
9197c478bd9Sstevel@tonic-gate 	/* Another principal owns same credentials? (exits if that happens) */
9207c478bd9Sstevel@tonic-gate 	(void) auth_exists(nis_princ, netname, authtype, domain);
9217c478bd9Sstevel@tonic-gate 
9227c478bd9Sstevel@tonic-gate 	return (1); /* all passed */
9237c478bd9Sstevel@tonic-gate }
9247c478bd9Sstevel@tonic-gate 
9257c478bd9Sstevel@tonic-gate 
9267c478bd9Sstevel@tonic-gate /* Store new key information in the specified name service */
9277c478bd9Sstevel@tonic-gate static void
9287c478bd9Sstevel@tonic-gate storekeys()
9297c478bd9Sstevel@tonic-gate {
9307c478bd9Sstevel@tonic-gate 	int		mcount, ucount = 0;
9317c478bd9Sstevel@tonic-gate 	char		*ypmaster, *ypdomain = NULL, pkent[MAXPKENTLEN];
9327c478bd9Sstevel@tonic-gate 	nis_name	nis_princ;
9337c478bd9Sstevel@tonic-gate 
9347c478bd9Sstevel@tonic-gate 
9357c478bd9Sstevel@tonic-gate 	/* Setup */
9367c478bd9Sstevel@tonic-gate 	switch (dest_service) {
9377c478bd9Sstevel@tonic-gate 	case PK_LDAP:
9387c478bd9Sstevel@tonic-gate 		break;
9397c478bd9Sstevel@tonic-gate 	case PK_NISPLUS:
9407c478bd9Sstevel@tonic-gate 		nis_princ = get_nisplus_principal(nis_local_directory(),
9417c478bd9Sstevel@tonic-gate 							geteuid());
9427c478bd9Sstevel@tonic-gate 		break;
9437c478bd9Sstevel@tonic-gate 	case PK_YP:
9447c478bd9Sstevel@tonic-gate 		yp_get_default_domain(&ypdomain);
9457c478bd9Sstevel@tonic-gate 		if (yp_master(ypdomain, PKMAP, &ypmaster) != 0) {
9467c478bd9Sstevel@tonic-gate 			fprintf(stderr,
9477c478bd9Sstevel@tonic-gate 			"%s: cannot find master of NIS publickey database\n",
9487c478bd9Sstevel@tonic-gate 				program_name);
9497c478bd9Sstevel@tonic-gate 			exit(1);
9507c478bd9Sstevel@tonic-gate 		}
9517c478bd9Sstevel@tonic-gate 		fprintf(stdout,
9527c478bd9Sstevel@tonic-gate 			"Sending key change request to %s ...\n", ypmaster);
9537c478bd9Sstevel@tonic-gate 		break;
9547c478bd9Sstevel@tonic-gate 	case PK_FILES:
9557c478bd9Sstevel@tonic-gate 		if (geteuid() != 0) {
9567c478bd9Sstevel@tonic-gate 			fprintf(stderr,
9577c478bd9Sstevel@tonic-gate 		"%s: non-root users cannot change their key-pair in %s\n",
9587c478bd9Sstevel@tonic-gate 				program_name, PKFILE);
9597c478bd9Sstevel@tonic-gate 			exit(1);
9607c478bd9Sstevel@tonic-gate 		}
9617c478bd9Sstevel@tonic-gate 		break;
9627c478bd9Sstevel@tonic-gate 	default:
9637c478bd9Sstevel@tonic-gate 		fprintf(stderr,
9647c478bd9Sstevel@tonic-gate 			"could not update; database %d unknown\n",
9657c478bd9Sstevel@tonic-gate 			dest_service);
9667c478bd9Sstevel@tonic-gate 		exit(1);
9677c478bd9Sstevel@tonic-gate 	}
9687c478bd9Sstevel@tonic-gate 
9697c478bd9Sstevel@tonic-gate 	if (mechs) {
9707c478bd9Sstevel@tonic-gate 		for (mcount = 0; CURMECH; mcount++) {
9717c478bd9Sstevel@tonic-gate 			char		authtype[MECH_MAXATNAME];
9727c478bd9Sstevel@tonic-gate 
9737c478bd9Sstevel@tonic-gate 			if (!plist[mcount] && !clist[mcount])
9747c478bd9Sstevel@tonic-gate 				continue;
9757c478bd9Sstevel@tonic-gate 
9767c478bd9Sstevel@tonic-gate 			__nis_mechalias2authtype(CURMECH->alias, authtype,
9777c478bd9Sstevel@tonic-gate 							MECH_MAXATNAME);
9787c478bd9Sstevel@tonic-gate 			if (!authtype) {
9797c478bd9Sstevel@tonic-gate 				fprintf(stderr,
9807c478bd9Sstevel@tonic-gate 				"Could not generate auth_type for %s.\n",
9817c478bd9Sstevel@tonic-gate 					CURMECH->alias);
9827c478bd9Sstevel@tonic-gate 				continue;
9837c478bd9Sstevel@tonic-gate 			}
9847c478bd9Sstevel@tonic-gate 
9857c478bd9Sstevel@tonic-gate 			snprintf(pkent, MAXPKENTLEN, "%s:%s:%d",
9867c478bd9Sstevel@tonic-gate 					plist[mcount], clist[mcount],
9877c478bd9Sstevel@tonic-gate 					CURMECH->algtype);
9887c478bd9Sstevel@tonic-gate 
9897c478bd9Sstevel@tonic-gate 			switch (dest_service) {
9907c478bd9Sstevel@tonic-gate 			case PK_LDAP:
9917c478bd9Sstevel@tonic-gate 				if (ldap_update(CURMECH->alias, netname,
9927c478bd9Sstevel@tonic-gate 						plist[mcount], clist[mcount],
9937c478bd9Sstevel@tonic-gate 						login_pw))
9947c478bd9Sstevel@tonic-gate 					fprintf(stderr,
9957c478bd9Sstevel@tonic-gate 			"%s: unable to update %s key in LDAP database\n",
9967c478bd9Sstevel@tonic-gate 						program_name, authtype);
9977c478bd9Sstevel@tonic-gate 				else
9987c478bd9Sstevel@tonic-gate 					ucount++;
9997c478bd9Sstevel@tonic-gate 				break;
10007c478bd9Sstevel@tonic-gate 
10017c478bd9Sstevel@tonic-gate 			case PK_NISPLUS:
10027c478bd9Sstevel@tonic-gate 				if (nisplus_update(nis_princ,
10037c478bd9Sstevel@tonic-gate 							authtype,
10047c478bd9Sstevel@tonic-gate 							plist[mcount],
10057c478bd9Sstevel@tonic-gate 							clist[mcount]))
10067c478bd9Sstevel@tonic-gate 					fprintf(stderr,
10077c478bd9Sstevel@tonic-gate 			"%s: unable to update %s key in nisplus database\n",
10087c478bd9Sstevel@tonic-gate 						program_name, authtype);
10097c478bd9Sstevel@tonic-gate 				else
10107c478bd9Sstevel@tonic-gate 					ucount++;
10117c478bd9Sstevel@tonic-gate 				break;
10127c478bd9Sstevel@tonic-gate 
10137c478bd9Sstevel@tonic-gate 			case PK_YP:
10147c478bd9Sstevel@tonic-gate 				/* Should never get here. */
10157c478bd9Sstevel@tonic-gate 				break;
10167c478bd9Sstevel@tonic-gate 
10177c478bd9Sstevel@tonic-gate 			case PK_FILES:
10187c478bd9Sstevel@tonic-gate 				/* Should never get here. */
10197c478bd9Sstevel@tonic-gate 				break;
10207c478bd9Sstevel@tonic-gate 			}
10217c478bd9Sstevel@tonic-gate 		}
10227c478bd9Sstevel@tonic-gate 	} else {
10237c478bd9Sstevel@tonic-gate 		int	status = 0;
10247c478bd9Sstevel@tonic-gate 
10257c478bd9Sstevel@tonic-gate 		assert(plist[0] && clist[0]);
10267c478bd9Sstevel@tonic-gate 		snprintf(pkent, MAXPKENTLEN, "%s:%s", plist[0], clist[0]);
10277c478bd9Sstevel@tonic-gate 
10287c478bd9Sstevel@tonic-gate 		switch (dest_service) {
10297c478bd9Sstevel@tonic-gate 		case PK_LDAP:
10307c478bd9Sstevel@tonic-gate 			if (ldap_update("dh192-0", netname,
10317c478bd9Sstevel@tonic-gate 					plist[0], clist[0],
10327c478bd9Sstevel@tonic-gate 					login_pw)) {
10337c478bd9Sstevel@tonic-gate 				fprintf(stderr,
10347c478bd9Sstevel@tonic-gate 			"%s: unable to update %s key in LDAP database\n",
10357c478bd9Sstevel@tonic-gate 					program_name);
10367c478bd9Sstevel@tonic-gate 				exit(1);
10377c478bd9Sstevel@tonic-gate 			}
10387c478bd9Sstevel@tonic-gate 			break;
10397c478bd9Sstevel@tonic-gate 
10407c478bd9Sstevel@tonic-gate 		case PK_NISPLUS:
10417c478bd9Sstevel@tonic-gate 			assert(plist[0] && clist[0]);
10427c478bd9Sstevel@tonic-gate 			if (nisplus_update(nis_princ,
10437c478bd9Sstevel@tonic-gate 						AUTH_DES_AUTH_TYPE,
10447c478bd9Sstevel@tonic-gate 						plist[0],
10457c478bd9Sstevel@tonic-gate 						clist[0])) {
10467c478bd9Sstevel@tonic-gate 					fprintf(stderr,
10477c478bd9Sstevel@tonic-gate 			"%s: unable to update nisplus database\n",
10487c478bd9Sstevel@tonic-gate 						program_name);
10497c478bd9Sstevel@tonic-gate 					exit(1);
10507c478bd9Sstevel@tonic-gate 			}
10517c478bd9Sstevel@tonic-gate 			break;
10527c478bd9Sstevel@tonic-gate 
10537c478bd9Sstevel@tonic-gate 		case PK_YP:
10547c478bd9Sstevel@tonic-gate 			if (status = yp_update(ypdomain, PKMAP,
10557c478bd9Sstevel@tonic-gate 						YPOP_STORE, netname,
10567c478bd9Sstevel@tonic-gate 						strlen(netname), pkent,
10577c478bd9Sstevel@tonic-gate 						strlen(pkent))) {
10587c478bd9Sstevel@tonic-gate 				fprintf(stderr,
10597c478bd9Sstevel@tonic-gate 				"%s: unable to update NIS database (%u): %s\n",
10607c478bd9Sstevel@tonic-gate 					program_name, status,
10617c478bd9Sstevel@tonic-gate 					yperr_string(status));
10627c478bd9Sstevel@tonic-gate 				exit(1);
10637c478bd9Sstevel@tonic-gate 			}
10647c478bd9Sstevel@tonic-gate 			break;
10657c478bd9Sstevel@tonic-gate 
10667c478bd9Sstevel@tonic-gate 		case PK_FILES:
10677c478bd9Sstevel@tonic-gate 			if (localupdate(netname, PKFILE, YPOP_STORE, pkent)) {
10687c478bd9Sstevel@tonic-gate 				fprintf(stderr,
10697c478bd9Sstevel@tonic-gate 			"%s: hence, unable to update publickey database\n",
10707c478bd9Sstevel@tonic-gate 					program_name);
10717c478bd9Sstevel@tonic-gate 				exit(1);
10727c478bd9Sstevel@tonic-gate 			}
10737c478bd9Sstevel@tonic-gate 			break;
10747c478bd9Sstevel@tonic-gate 
10757c478bd9Sstevel@tonic-gate 		default:
10767c478bd9Sstevel@tonic-gate 			/* Should never get here */
10777c478bd9Sstevel@tonic-gate 			assert(0);
10787c478bd9Sstevel@tonic-gate 		}
10797c478bd9Sstevel@tonic-gate 		return;
10807c478bd9Sstevel@tonic-gate 	}
10817c478bd9Sstevel@tonic-gate 	if (!ucount) {
10827c478bd9Sstevel@tonic-gate 		fprintf(stderr, "%s: unable to update any key-pairs for %s.\n",
10837c478bd9Sstevel@tonic-gate 			program_name, pw->pw_name);
10847c478bd9Sstevel@tonic-gate 		exit(1);
10857c478bd9Sstevel@tonic-gate 	}
10867c478bd9Sstevel@tonic-gate }
10877c478bd9Sstevel@tonic-gate 
10887c478bd9Sstevel@tonic-gate /* Check that someone else don't have the same auth information already */
10897c478bd9Sstevel@tonic-gate static
10907c478bd9Sstevel@tonic-gate nis_error
10917c478bd9Sstevel@tonic-gate auth_exists(char *princname, char *auth_name, char *auth_type, char *domain)
10927c478bd9Sstevel@tonic-gate {
10937c478bd9Sstevel@tonic-gate 	char sname[NIS_MAXNAMELEN+1];
10947c478bd9Sstevel@tonic-gate 	nis_result	*res;
10957c478bd9Sstevel@tonic-gate 	nis_error status;
10967c478bd9Sstevel@tonic-gate 	char *foundprinc;
10977c478bd9Sstevel@tonic-gate 
10987c478bd9Sstevel@tonic-gate 	(void) sprintf(sname, "[auth_name=%s,auth_type=%s],%s.%s",
10997c478bd9Sstevel@tonic-gate 		auth_name, auth_type, CRED_TABLE, domain);
11007c478bd9Sstevel@tonic-gate 	if (sname[strlen(sname)-1] != '.')
11017c478bd9Sstevel@tonic-gate 		strcat(sname, ".");
11027c478bd9Sstevel@tonic-gate 	/* Don't want FOLLOW_PATH here */
11037c478bd9Sstevel@tonic-gate 	res = nis_list(sname,
11047c478bd9Sstevel@tonic-gate 		MASTER_ONLY+USE_DGRAM+NO_AUTHINFO+FOLLOW_LINKS,
11057c478bd9Sstevel@tonic-gate 		NULL, NULL);
11067c478bd9Sstevel@tonic-gate 
11077c478bd9Sstevel@tonic-gate 	status = res->status;
11087c478bd9Sstevel@tonic-gate 	switch (res->status) {
11097c478bd9Sstevel@tonic-gate 	case NIS_NOTFOUND:
11107c478bd9Sstevel@tonic-gate 		break;
11117c478bd9Sstevel@tonic-gate 	case NIS_TRYAGAIN:
11127c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
11137c478bd9Sstevel@tonic-gate 			"%s: NIS+ server busy, try again later.\n",
11147c478bd9Sstevel@tonic-gate 			program_name);
11157c478bd9Sstevel@tonic-gate 		exit(1);
11167c478bd9Sstevel@tonic-gate 		break;
11177c478bd9Sstevel@tonic-gate 	case NIS_PERMISSION:
11187c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
11197c478bd9Sstevel@tonic-gate 		"%s: insufficient permission to look up old credentials.\n",
11207c478bd9Sstevel@tonic-gate 			program_name);
11217c478bd9Sstevel@tonic-gate 		exit(1);
11227c478bd9Sstevel@tonic-gate 		break;
11237c478bd9Sstevel@tonic-gate 	case NIS_SUCCESS:
11247c478bd9Sstevel@tonic-gate 		foundprinc = ENTRY_VAL(res->objects.objects_val, 0);
11257c478bd9Sstevel@tonic-gate 		if (nis_dir_cmp(foundprinc, princname) != SAME_NAME) {
11267c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
11277c478bd9Sstevel@tonic-gate 	"%s: %s credentials with auth_name '%s' already belong to '%s'.\n",
11287c478bd9Sstevel@tonic-gate 			program_name, auth_type, auth_name, foundprinc);
11297c478bd9Sstevel@tonic-gate 			exit(1);
11307c478bd9Sstevel@tonic-gate 		}
11317c478bd9Sstevel@tonic-gate 		break;
11327c478bd9Sstevel@tonic-gate 	default:
11337c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
11347c478bd9Sstevel@tonic-gate 			"%s: error looking at cred table, NIS+ error: %s\n",
11357c478bd9Sstevel@tonic-gate 			program_name, nis_sperrno(res->status));
11367c478bd9Sstevel@tonic-gate 		exit(1);
11377c478bd9Sstevel@tonic-gate 	}
11387c478bd9Sstevel@tonic-gate 	nis_freeresult(res);
11397c478bd9Sstevel@tonic-gate 	return (status);
11407c478bd9Sstevel@tonic-gate }
11417c478bd9Sstevel@tonic-gate 
11427c478bd9Sstevel@tonic-gate 
11437c478bd9Sstevel@tonic-gate /* Check whether this principal already has this type of credentials */
11447c478bd9Sstevel@tonic-gate static nis_error
11457c478bd9Sstevel@tonic-gate cred_exists(const char *nisprinc, const char *flavor, const char *domain)
11467c478bd9Sstevel@tonic-gate {
11477c478bd9Sstevel@tonic-gate 	char sname[NIS_MAXNAMELEN+1];
11487c478bd9Sstevel@tonic-gate 	nis_result	*res;
11497c478bd9Sstevel@tonic-gate 	nis_error status;
11507c478bd9Sstevel@tonic-gate 
11517c478bd9Sstevel@tonic-gate 	snprintf(sname, NIS_MAXNAMELEN,
11527c478bd9Sstevel@tonic-gate 			"[cname=\"%s\",auth_type=%s],%s.%s",
11537c478bd9Sstevel@tonic-gate 			nisprinc, flavor, CRED_TABLE, domain);
11547c478bd9Sstevel@tonic-gate 	if (sname[strlen(sname)-1] != '.')
11557c478bd9Sstevel@tonic-gate 		strcat(sname, ".");
11567c478bd9Sstevel@tonic-gate 
11577c478bd9Sstevel@tonic-gate 	/* Don't want FOLLOW_PATH here */
11587c478bd9Sstevel@tonic-gate 	res = nis_list(sname,
11597c478bd9Sstevel@tonic-gate 				MASTER_ONLY+USE_DGRAM+NO_AUTHINFO+FOLLOW_LINKS,
11607c478bd9Sstevel@tonic-gate 				NULL, NULL);
11617c478bd9Sstevel@tonic-gate 
11627c478bd9Sstevel@tonic-gate 	status = res->status;
11637c478bd9Sstevel@tonic-gate 	switch (status) {
11647c478bd9Sstevel@tonic-gate 	case NIS_NOTFOUND:
11657c478bd9Sstevel@tonic-gate 		break;
11667c478bd9Sstevel@tonic-gate 	case NIS_TRYAGAIN:
11677c478bd9Sstevel@tonic-gate 		fprintf(stderr,
11687c478bd9Sstevel@tonic-gate 			"%s: NIS+ server busy, try again later.\n",
11697c478bd9Sstevel@tonic-gate 			program_name);
11707c478bd9Sstevel@tonic-gate 		exit(1);
11717c478bd9Sstevel@tonic-gate 		break;
11727c478bd9Sstevel@tonic-gate 	case NIS_PERMISSION:
11737c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
11747c478bd9Sstevel@tonic-gate 		"%s: insufficient permission to look at credentials table\n",
11757c478bd9Sstevel@tonic-gate 			program_name);
11767c478bd9Sstevel@tonic-gate 		exit(1);
11777c478bd9Sstevel@tonic-gate 		break;
11787c478bd9Sstevel@tonic-gate 	case NIS_SUCCESS:
11797c478bd9Sstevel@tonic-gate 	case NIS_S_SUCCESS:
11807c478bd9Sstevel@tonic-gate 		break;
11817c478bd9Sstevel@tonic-gate 	default:
11827c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
11837c478bd9Sstevel@tonic-gate 			"%s: error looking at cred table, NIS+ error: %s\n",
11847c478bd9Sstevel@tonic-gate 			program_name, nis_sperrno(res->status));
11857c478bd9Sstevel@tonic-gate 		exit(1);
11867c478bd9Sstevel@tonic-gate 	}
11877c478bd9Sstevel@tonic-gate 	nis_freeresult(res);
11887c478bd9Sstevel@tonic-gate 	return (status);
11897c478bd9Sstevel@tonic-gate }
11907c478bd9Sstevel@tonic-gate 
11917c478bd9Sstevel@tonic-gate 
11927c478bd9Sstevel@tonic-gate static int
11937c478bd9Sstevel@tonic-gate modify_cred_obj(nis_object *obj, char *domain)
11947c478bd9Sstevel@tonic-gate {
11957c478bd9Sstevel@tonic-gate 	int status = 0;
11967c478bd9Sstevel@tonic-gate 	char sname[NIS_MAXNAMELEN+1];
11977c478bd9Sstevel@tonic-gate 	nis_result	*res;
11987c478bd9Sstevel@tonic-gate 
11997c478bd9Sstevel@tonic-gate 	(void) sprintf(sname, "%s.%s", CRED_TABLE, domain);
12007c478bd9Sstevel@tonic-gate 	res = nis_modify_entry(sname, obj, 0);
12017c478bd9Sstevel@tonic-gate 	switch (res->status) {
12027c478bd9Sstevel@tonic-gate 	case NIS_TRYAGAIN:
12037c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
12047c478bd9Sstevel@tonic-gate 			"%s: NIS+ server busy, try again later.\n",
12057c478bd9Sstevel@tonic-gate 			program_name);
12067c478bd9Sstevel@tonic-gate 		exit(1);
12077c478bd9Sstevel@tonic-gate 		break;
12087c478bd9Sstevel@tonic-gate 	case NIS_PERMISSION:
12097c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
12107c478bd9Sstevel@tonic-gate 			"%s: insufficient permission to update credentials.\n",
12117c478bd9Sstevel@tonic-gate 			program_name);
12127c478bd9Sstevel@tonic-gate 		exit(1);
12137c478bd9Sstevel@tonic-gate 		break;
12147c478bd9Sstevel@tonic-gate 	case NIS_SUCCESS:
12157c478bd9Sstevel@tonic-gate 		status = 1;
12167c478bd9Sstevel@tonic-gate 		break;
12177c478bd9Sstevel@tonic-gate 	default:
12187c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
12197c478bd9Sstevel@tonic-gate 			"%s: error modifying credential, NIS+ error: %s.\n",
12207c478bd9Sstevel@tonic-gate 			program_name, nis_sperrno(res->status));
12217c478bd9Sstevel@tonic-gate 		exit(1);
12227c478bd9Sstevel@tonic-gate 	}
12237c478bd9Sstevel@tonic-gate 	nis_freeresult(res);
12247c478bd9Sstevel@tonic-gate 	return (status);
12257c478bd9Sstevel@tonic-gate }
12267c478bd9Sstevel@tonic-gate 
12277c478bd9Sstevel@tonic-gate 
12287c478bd9Sstevel@tonic-gate static int
12297c478bd9Sstevel@tonic-gate add_cred_obj(nis_object *obj, char *domain)
12307c478bd9Sstevel@tonic-gate {
12317c478bd9Sstevel@tonic-gate 	int status = 0;
12327c478bd9Sstevel@tonic-gate 	char sname[NIS_MAXNAMELEN+1];
12337c478bd9Sstevel@tonic-gate 	nis_result	*res;
12347c478bd9Sstevel@tonic-gate 
12357c478bd9Sstevel@tonic-gate 	/* Assume check for cred_exists performed already */
12367c478bd9Sstevel@tonic-gate 
12377c478bd9Sstevel@tonic-gate 	(void) sprintf(sname, "%s.%s", CRED_TABLE, domain);
12387c478bd9Sstevel@tonic-gate 	res = nis_add_entry(sname, obj, 0);
12397c478bd9Sstevel@tonic-gate 	switch (res->status) {
12407c478bd9Sstevel@tonic-gate 	case NIS_TRYAGAIN:
12417c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
12427c478bd9Sstevel@tonic-gate 			"%s: NIS+ server busy, try again later.\n",
12437c478bd9Sstevel@tonic-gate 			program_name);
12447c478bd9Sstevel@tonic-gate 		exit(1);
12457c478bd9Sstevel@tonic-gate 		break;
12467c478bd9Sstevel@tonic-gate 	case NIS_PERMISSION:
12477c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
12487c478bd9Sstevel@tonic-gate 			"%s: insufficient permission to update credentials.\n",
12497c478bd9Sstevel@tonic-gate 			program_name);
12507c478bd9Sstevel@tonic-gate 		exit(1);
12517c478bd9Sstevel@tonic-gate 		break;
12527c478bd9Sstevel@tonic-gate 	case NIS_SUCCESS:
12537c478bd9Sstevel@tonic-gate 		status = 1;
12547c478bd9Sstevel@tonic-gate 		break;
12557c478bd9Sstevel@tonic-gate 	default:
12567c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
12577c478bd9Sstevel@tonic-gate 			"%s: error creating credential, NIS+ error: %s.\n",
12587c478bd9Sstevel@tonic-gate 			program_name, nis_sperrno(res->status));
12597c478bd9Sstevel@tonic-gate 		exit(1);
12607c478bd9Sstevel@tonic-gate 	}
12617c478bd9Sstevel@tonic-gate 	nis_freeresult(res);
12627c478bd9Sstevel@tonic-gate 	return (status);
12637c478bd9Sstevel@tonic-gate }
12647c478bd9Sstevel@tonic-gate 
12657c478bd9Sstevel@tonic-gate 
12667c478bd9Sstevel@tonic-gate /* Update NIS+ table with new key information */
12677c478bd9Sstevel@tonic-gate static int
12687c478bd9Sstevel@tonic-gate nisplus_update(nis_name nis_princ, char *authtype, char *public, char *crypt)
12697c478bd9Sstevel@tonic-gate {
12707c478bd9Sstevel@tonic-gate 	nis_object	*obj = init_entry();
12717c478bd9Sstevel@tonic-gate 	int		status;
12727c478bd9Sstevel@tonic-gate 	bool_t		addition;
12737c478bd9Sstevel@tonic-gate 	char		*userdomain, *cmpdomain, *domain;
12747c478bd9Sstevel@tonic-gate 
12757c478bd9Sstevel@tonic-gate 	if (!(userdomain = strchr(netname, '@'))) {
12767c478bd9Sstevel@tonic-gate 		fprintf(stderr, "%s: invalid netname: '%s'.\n",
12777c478bd9Sstevel@tonic-gate 			program_name, netname);
12787c478bd9Sstevel@tonic-gate 		exit(1);
12797c478bd9Sstevel@tonic-gate 	}
12807c478bd9Sstevel@tonic-gate 	userdomain++;
12817c478bd9Sstevel@tonic-gate 
12827c478bd9Sstevel@tonic-gate 	cmpdomain = strdup(userdomain);
12837c478bd9Sstevel@tonic-gate 	if (cmpdomain[strlen(cmpdomain) - 1] != '.')
12847c478bd9Sstevel@tonic-gate 		strcat(cmpdomain, ".");
12857c478bd9Sstevel@tonic-gate 
12867c478bd9Sstevel@tonic-gate 	domain = nis_domain_of(nis_princ);
12877c478bd9Sstevel@tonic-gate 	if (strcasecmp(domain, cmpdomain) != 0)
12887c478bd9Sstevel@tonic-gate 		domain = nis_local_directory();
12897c478bd9Sstevel@tonic-gate 
12907c478bd9Sstevel@tonic-gate 	if (!sanity_checks(nis_princ, domain, authtype))
12917c478bd9Sstevel@tonic-gate 		exit(1);
12927c478bd9Sstevel@tonic-gate 
12937c478bd9Sstevel@tonic-gate 	addition = (cred_exists(nis_princ, authtype, domain) == NIS_NOTFOUND);
12947c478bd9Sstevel@tonic-gate 
12957c478bd9Sstevel@tonic-gate 	ENTRY_VAL(obj, 0) = nis_princ;
12967c478bd9Sstevel@tonic-gate 	ENTRY_LEN(obj, 0) = strlen(nis_princ) + 1;
12977c478bd9Sstevel@tonic-gate 
12987c478bd9Sstevel@tonic-gate 	ENTRY_VAL(obj, 1) = authtype;
12997c478bd9Sstevel@tonic-gate 	ENTRY_LEN(obj, 1) = strlen(authtype) + 1;
13007c478bd9Sstevel@tonic-gate 
13017c478bd9Sstevel@tonic-gate 	ENTRY_VAL(obj, 2) = netname;
13027c478bd9Sstevel@tonic-gate 	ENTRY_LEN(obj, 2) = strlen(netname) + 1;
13037c478bd9Sstevel@tonic-gate 
13047c478bd9Sstevel@tonic-gate 	ENTRY_VAL(obj, 3) = public;
13057c478bd9Sstevel@tonic-gate 	ENTRY_LEN(obj, 3) = strlen(public) + 1;
13067c478bd9Sstevel@tonic-gate 
13077c478bd9Sstevel@tonic-gate 	ENTRY_VAL(obj, 4) = crypt;
13087c478bd9Sstevel@tonic-gate 	ENTRY_LEN(obj, 4) = strlen(crypt) + 1;
13097c478bd9Sstevel@tonic-gate 
13107c478bd9Sstevel@tonic-gate 	if (addition) {
13117c478bd9Sstevel@tonic-gate 		obj->zo_owner = nis_princ;
13127c478bd9Sstevel@tonic-gate 		obj->zo_group = nis_local_group();
13137c478bd9Sstevel@tonic-gate 		obj->zo_domain = domain;
13147c478bd9Sstevel@tonic-gate 		/* owner: r, group: rmcd */
13157c478bd9Sstevel@tonic-gate 		obj->zo_access = ((NIS_READ_ACC<<16)|
13167c478bd9Sstevel@tonic-gate 				(NIS_READ_ACC|NIS_MODIFY_ACC|NIS_CREATE_ACC|
13177c478bd9Sstevel@tonic-gate 				NIS_DESTROY_ACC)<<8);
13187c478bd9Sstevel@tonic-gate 		status = add_cred_obj(obj, domain);
13197c478bd9Sstevel@tonic-gate 	} else {
13207c478bd9Sstevel@tonic-gate 		obj->EN_data.en_cols.en_cols_val[3].ec_flags |= EN_MODIFIED;
13217c478bd9Sstevel@tonic-gate 		obj->EN_data.en_cols.en_cols_val[4].ec_flags |= EN_MODIFIED;
13227c478bd9Sstevel@tonic-gate 		status = modify_cred_obj(obj, domain);
13237c478bd9Sstevel@tonic-gate 	}
13247c478bd9Sstevel@tonic-gate 	return (status == 1 ? 0 : 1);
13257c478bd9Sstevel@tonic-gate }
13267c478bd9Sstevel@tonic-gate 
13277c478bd9Sstevel@tonic-gate 
13287c478bd9Sstevel@tonic-gate void
13297c478bd9Sstevel@tonic-gate addmechtolist(char *mechtype)
13307c478bd9Sstevel@tonic-gate {
13317c478bd9Sstevel@tonic-gate 	mechanism_t	**realmechlist;
13327c478bd9Sstevel@tonic-gate 	int		i;
13337c478bd9Sstevel@tonic-gate 
13347c478bd9Sstevel@tonic-gate 	if (realmechlist = __nis_get_mechanisms(FALSE)) {
13357c478bd9Sstevel@tonic-gate 		/* Match requested mech with list */
13367c478bd9Sstevel@tonic-gate 		for (i = 0; realmechlist[i]; i++) {
13377c478bd9Sstevel@tonic-gate 			if (realmechlist[i]->alias)
13387c478bd9Sstevel@tonic-gate 				if (strcmp(realmechlist[i]->alias, mechtype)
13397c478bd9Sstevel@tonic-gate 				    == 0) {
13407c478bd9Sstevel@tonic-gate 					/*
13417c478bd9Sstevel@tonic-gate 					 * Match, add it to the mechs.
13427c478bd9Sstevel@tonic-gate 					 * Don't worry about qop or
13437c478bd9Sstevel@tonic-gate 					 * secserv since they are not
13447c478bd9Sstevel@tonic-gate 					 * used by chkey.
13457c478bd9Sstevel@tonic-gate 					 */
13467c478bd9Sstevel@tonic-gate 					numspecmech++;
13477c478bd9Sstevel@tonic-gate 					if ((mechs =
13487c478bd9Sstevel@tonic-gate 						(mechanism_t **)realloc(mechs,
13497c478bd9Sstevel@tonic-gate 				sizeof (mechanism_t *) * (numspecmech + 1))) ==
13507c478bd9Sstevel@tonic-gate 					    NULL) {
13517c478bd9Sstevel@tonic-gate 						perror("Can not change keys");
13527c478bd9Sstevel@tonic-gate 						exit(1);
13537c478bd9Sstevel@tonic-gate 					}
13547c478bd9Sstevel@tonic-gate 
13557c478bd9Sstevel@tonic-gate 					if ((mechs[numspecmech - 1] =
13567c478bd9Sstevel@tonic-gate 		(mechanism_t *)malloc(sizeof (mechanism_t))) == NULL) {
13577c478bd9Sstevel@tonic-gate 						perror("Can not change keys");
13587c478bd9Sstevel@tonic-gate 						exit(1);
13597c478bd9Sstevel@tonic-gate 					}
13607c478bd9Sstevel@tonic-gate 					if (realmechlist[i]->mechname)
13617c478bd9Sstevel@tonic-gate 					mechs[numspecmech - 1]->mechname =
13627c478bd9Sstevel@tonic-gate 					strdup(realmechlist[i]->mechname);
13637c478bd9Sstevel@tonic-gate 					if (realmechlist[i]->alias)
13647c478bd9Sstevel@tonic-gate 					mechs[numspecmech - 1]->alias =
13657c478bd9Sstevel@tonic-gate 						strdup(realmechlist[i]->alias);
13667c478bd9Sstevel@tonic-gate 					mechs[numspecmech - 1]->keylen =
13677c478bd9Sstevel@tonic-gate 						realmechlist[i]->keylen;
13687c478bd9Sstevel@tonic-gate 					mechs[numspecmech - 1]->algtype =
13697c478bd9Sstevel@tonic-gate 						realmechlist[i]->algtype;
13707c478bd9Sstevel@tonic-gate 					mechs[numspecmech] = NULL;
13717c478bd9Sstevel@tonic-gate 					__nis_release_mechanisms(realmechlist);
13727c478bd9Sstevel@tonic-gate 					return;
13737c478bd9Sstevel@tonic-gate 				}
13747c478bd9Sstevel@tonic-gate 		}
13757c478bd9Sstevel@tonic-gate 
13767c478bd9Sstevel@tonic-gate 		fprintf(stderr,
13777c478bd9Sstevel@tonic-gate 		"WARNING: Mechanism '%s' not configured, skipping...\n",
13787c478bd9Sstevel@tonic-gate 			mechtype);
13797c478bd9Sstevel@tonic-gate 		__nis_release_mechanisms(realmechlist);
13807c478bd9Sstevel@tonic-gate 		return;
13817c478bd9Sstevel@tonic-gate 	}
13827c478bd9Sstevel@tonic-gate 	fprintf(stderr,
13837c478bd9Sstevel@tonic-gate 		"WARNING: Mechanism '%s' not configured, skipping...\n",
13847c478bd9Sstevel@tonic-gate 		mechtype);
13857c478bd9Sstevel@tonic-gate }
13867c478bd9Sstevel@tonic-gate 
13877c478bd9Sstevel@tonic-gate 
138849e7ca49Speteh int
13897c478bd9Sstevel@tonic-gate main(int argc, char **argv)
13907c478bd9Sstevel@tonic-gate {
13917c478bd9Sstevel@tonic-gate 	int		c, mcount;
13927c478bd9Sstevel@tonic-gate 	uid_t		uid;
13937c478bd9Sstevel@tonic-gate 	uid_t		orig_euid;
13947c478bd9Sstevel@tonic-gate 	char		*service = NULL;
13957c478bd9Sstevel@tonic-gate 	program_name = argv[0];
13967c478bd9Sstevel@tonic-gate 
13977c478bd9Sstevel@tonic-gate 	mechs = __nis_get_mechanisms(FALSE);
13987c478bd9Sstevel@tonic-gate 
13997c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "fps:m:")) != -1) {
14007c478bd9Sstevel@tonic-gate 		switch (c) {
14017c478bd9Sstevel@tonic-gate 		case 'f':
14027c478bd9Sstevel@tonic-gate 			/*
14037c478bd9Sstevel@tonic-gate 			 * Not documented as of on1093.
14047c478bd9Sstevel@tonic-gate 			 * Temporarily supported
14057c478bd9Sstevel@tonic-gate 			 */
14067c478bd9Sstevel@tonic-gate 			force++;
14077c478bd9Sstevel@tonic-gate 			break;
14087c478bd9Sstevel@tonic-gate 		case 'p':
14097c478bd9Sstevel@tonic-gate 			makenew = FALSE;
14107c478bd9Sstevel@tonic-gate 			break;
14117c478bd9Sstevel@tonic-gate 		case 's':
14127c478bd9Sstevel@tonic-gate 			if (!service)
14137c478bd9Sstevel@tonic-gate 				service = strdup(optarg);
14147c478bd9Sstevel@tonic-gate 			else
14157c478bd9Sstevel@tonic-gate 				usage();
14167c478bd9Sstevel@tonic-gate 			break;
14177c478bd9Sstevel@tonic-gate 		case 'm':
14187c478bd9Sstevel@tonic-gate 			if (mechs && specmech == FALSE) {
14197c478bd9Sstevel@tonic-gate 				__nis_release_mechanisms(mechs);
14207c478bd9Sstevel@tonic-gate 				mechs = NULL;
14217c478bd9Sstevel@tonic-gate 			}
14227c478bd9Sstevel@tonic-gate 			specmech = TRUE;
14237c478bd9Sstevel@tonic-gate 			addmechtolist(optarg);
14247c478bd9Sstevel@tonic-gate 			break;
14257c478bd9Sstevel@tonic-gate 		default:
14267c478bd9Sstevel@tonic-gate 			usage();
14277c478bd9Sstevel@tonic-gate 		}
14287c478bd9Sstevel@tonic-gate 	}
14297c478bd9Sstevel@tonic-gate 
14307c478bd9Sstevel@tonic-gate 	if (optind < argc)
14317c478bd9Sstevel@tonic-gate 		usage();
14327c478bd9Sstevel@tonic-gate 
14337c478bd9Sstevel@tonic-gate 	dest_service = get_pk_source(service);
14347c478bd9Sstevel@tonic-gate 
14357c478bd9Sstevel@tonic-gate 	if (!(netname = malloc(MAXNETNAMELEN + 1))) {
14367c478bd9Sstevel@tonic-gate 		fprintf(stderr, "%s: Malloc failure.\n", program_name);
14377c478bd9Sstevel@tonic-gate 		exit(1);
14387c478bd9Sstevel@tonic-gate 	}
14397c478bd9Sstevel@tonic-gate 	if (!__getnetnamebyuid(netname, uid = getuid())) {
14407c478bd9Sstevel@tonic-gate 		fprintf(stderr, "%s: cannot generate netname for uid %d\n",
14417c478bd9Sstevel@tonic-gate 			program_name, uid);
14427c478bd9Sstevel@tonic-gate 		exit(1);
14437c478bd9Sstevel@tonic-gate 	}
14447c478bd9Sstevel@tonic-gate 	sec_domain = strdup(strchr(netname, '@') + 1);
14457c478bd9Sstevel@tonic-gate 	getdomainname(local_domain, MAXNETNAMELEN);
14467c478bd9Sstevel@tonic-gate 
14477c478bd9Sstevel@tonic-gate 	if (makenew)
14487c478bd9Sstevel@tonic-gate 		fprintf(stdout, "Generating new key for '%s'.\n", netname);
14497c478bd9Sstevel@tonic-gate 	else
14507c478bd9Sstevel@tonic-gate 		fprintf(stdout, "Reencrypting key for '%s'.\n", netname);
14517c478bd9Sstevel@tonic-gate 
14527c478bd9Sstevel@tonic-gate 	if (mechs) {
14537c478bd9Sstevel@tonic-gate 		if (dest_service == PK_YP || dest_service == PK_FILES) {
14547c478bd9Sstevel@tonic-gate 			fprintf(stderr,
14557c478bd9Sstevel@tonic-gate 		"%s: can not add non-DES public keys to %s, skipping.\n",
14567c478bd9Sstevel@tonic-gate 				program_name, service);
14577c478bd9Sstevel@tonic-gate 			__nis_release_mechanisms(mechs);
14587c478bd9Sstevel@tonic-gate 			mechs = NULL;
14597c478bd9Sstevel@tonic-gate 			initkeylist(TRUE);
14607c478bd9Sstevel@tonic-gate 		} else
14617c478bd9Sstevel@tonic-gate 			initkeylist(FALSE);
14627c478bd9Sstevel@tonic-gate 	} else
14637c478bd9Sstevel@tonic-gate 		initkeylist(TRUE);
14647c478bd9Sstevel@tonic-gate 
14657c478bd9Sstevel@tonic-gate 	uid = getuid();
14667c478bd9Sstevel@tonic-gate 	orig_euid = geteuid();
14677c478bd9Sstevel@tonic-gate 
14687c478bd9Sstevel@tonic-gate 	/* Get password information */
14697c478bd9Sstevel@tonic-gate 	if ((pw = getpwuid(uid)) == NULL) {
14707c478bd9Sstevel@tonic-gate 		fprintf(stderr,
14717c478bd9Sstevel@tonic-gate 			"%s: Can not find passwd information for %d.\n",
14727c478bd9Sstevel@tonic-gate 			program_name, uid);
14737c478bd9Sstevel@tonic-gate 		exit(1);
14747c478bd9Sstevel@tonic-gate 	}
14757c478bd9Sstevel@tonic-gate 
14767c478bd9Sstevel@tonic-gate 	/* Set eUID to user */
14777c478bd9Sstevel@tonic-gate 	seteuid(uid);
14787c478bd9Sstevel@tonic-gate 
14797c478bd9Sstevel@tonic-gate 	/* Obtain a list of decrypted secret keys */
14807c478bd9Sstevel@tonic-gate 	getsecrets();
14817c478bd9Sstevel@tonic-gate 
14827c478bd9Sstevel@tonic-gate 	/* Keylogin user if not already done */
14837c478bd9Sstevel@tonic-gate 	if (mechs) {
14847c478bd9Sstevel@tonic-gate 		int mcount;
14857c478bd9Sstevel@tonic-gate 
14867c478bd9Sstevel@tonic-gate 		for (mcount = 0; CURMECH; mcount++) {
14877c478bd9Sstevel@tonic-gate 			keylen_t	keylen = CURMECH->keylen;
14887c478bd9Sstevel@tonic-gate 			algtype_t	algtype = CURMECH->algtype;
14897c478bd9Sstevel@tonic-gate 
14907c478bd9Sstevel@tonic-gate 			if (!key_secretkey_is_set_g(keylen, algtype) &&
14917c478bd9Sstevel@tonic-gate 			    slist[mcount]) {
14927c478bd9Sstevel@tonic-gate 				keylogin(CURMECH->keylen, CURMECH->algtype);
14937c478bd9Sstevel@tonic-gate 				if ((uid == 0) && (makenew == FALSE))
14947c478bd9Sstevel@tonic-gate 					write_rootkey(slist[mcount],
14957c478bd9Sstevel@tonic-gate 					VALID_ALIAS(CURMECH->alias) ?
14967c478bd9Sstevel@tonic-gate 							CURMECH->alias :
14977c478bd9Sstevel@tonic-gate 							"",
14987c478bd9Sstevel@tonic-gate 							keylen, algtype);
14997c478bd9Sstevel@tonic-gate 			}
15007c478bd9Sstevel@tonic-gate 		}
15017c478bd9Sstevel@tonic-gate 	} else {
15027c478bd9Sstevel@tonic-gate 		assert(slist[0]);
15037c478bd9Sstevel@tonic-gate 		if (!key_secretkey_is_set()) {
15047c478bd9Sstevel@tonic-gate 			keylogin_des();
15057c478bd9Sstevel@tonic-gate 			if ((uid == 0) && (makenew == FALSE))
15067c478bd9Sstevel@tonic-gate 				write_rootkey(slist[0], "des", 192, 0);
15077c478bd9Sstevel@tonic-gate 		}
15087c478bd9Sstevel@tonic-gate 	}
15097c478bd9Sstevel@tonic-gate 
15107c478bd9Sstevel@tonic-gate 	/* Set eUID back to root */
15117c478bd9Sstevel@tonic-gate 	(void) seteuid(orig_euid);
15127c478bd9Sstevel@tonic-gate 
15137c478bd9Sstevel@tonic-gate 	/*
15147c478bd9Sstevel@tonic-gate 	 * Call getspnam() after the keylogin has been done so we have
15157c478bd9Sstevel@tonic-gate 	 * the best chance of having read access to the encrypted pw.
15167c478bd9Sstevel@tonic-gate 	 *
15177c478bd9Sstevel@tonic-gate 	 * The eUID must be 0 for the getspnam() so the name service
15187c478bd9Sstevel@tonic-gate 	 * switch can handle the following eUID sensitive cases:
15197c478bd9Sstevel@tonic-gate 	 *
15207c478bd9Sstevel@tonic-gate 	 *	files/compat:	read /etc/shadow
15217c478bd9Sstevel@tonic-gate 	 *
15227c478bd9Sstevel@tonic-gate 	 *	nisplus:	try to read the encrypted pw as the root
15237c478bd9Sstevel@tonic-gate 	 *			principal and if that fails, and if the
15247c478bd9Sstevel@tonic-gate 	 *			user's secret key is set, seteuid(user)
15257c478bd9Sstevel@tonic-gate 	 *			and retry the read.
15267c478bd9Sstevel@tonic-gate 	 */
15277c478bd9Sstevel@tonic-gate 	if ((spw = getspnam(pw->pw_name)) == 0) {
15287c478bd9Sstevel@tonic-gate 
15297c478bd9Sstevel@tonic-gate 		/* Set eUID back to user */
15307c478bd9Sstevel@tonic-gate 		(void) seteuid(uid);
15317c478bd9Sstevel@tonic-gate 
15327c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
15337c478bd9Sstevel@tonic-gate 			"%s: cannot find shadow entry for %s.\n",
15347c478bd9Sstevel@tonic-gate 			program_name, pw->pw_name);
15357c478bd9Sstevel@tonic-gate 		exit(1);
15367c478bd9Sstevel@tonic-gate 	}
15377c478bd9Sstevel@tonic-gate 
15387c478bd9Sstevel@tonic-gate 	/* Set eUID back to user */
15397c478bd9Sstevel@tonic-gate 	(void) seteuid(uid);
15407c478bd9Sstevel@tonic-gate 
15417c478bd9Sstevel@tonic-gate 	if (strcmp(spw->sp_pwdp, "*NP*") == 0) {
15427c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
15437c478bd9Sstevel@tonic-gate 		"%s: do not have read access to the passwd field for %s\n",
15447c478bd9Sstevel@tonic-gate 				program_name, pw->pw_name);
15457c478bd9Sstevel@tonic-gate 		exit(1);
15467c478bd9Sstevel@tonic-gate 	}
15477c478bd9Sstevel@tonic-gate 
15487c478bd9Sstevel@tonic-gate 	/*
15497c478bd9Sstevel@tonic-gate 	 * force will be only supported for a while
15507c478bd9Sstevel@tonic-gate 	 * 	-- it is NOT documented as of s1093
15517c478bd9Sstevel@tonic-gate 	 */
15527c478bd9Sstevel@tonic-gate 	if (force) {
15537c478bd9Sstevel@tonic-gate 		char	*prompt = "Please enter New password:";
15547c478bd9Sstevel@tonic-gate 
15557c478bd9Sstevel@tonic-gate 		login_pw = getpass(prompt);
15567c478bd9Sstevel@tonic-gate 		if (!login_pw || !(strlen(login_pw))) {
15577c478bd9Sstevel@tonic-gate 			fprintf(stderr, "%s: key-pair(s) unchanged for %s.\n",
15587c478bd9Sstevel@tonic-gate 				program_name, pw->pw_name);
15597c478bd9Sstevel@tonic-gate 			exit(1);
15607c478bd9Sstevel@tonic-gate 		}
15617c478bd9Sstevel@tonic-gate 	} else {
15627c478bd9Sstevel@tonic-gate 		/*
15637c478bd9Sstevel@tonic-gate 		 * Reconsile rpc_pws and login_pw.
15647c478bd9Sstevel@tonic-gate 		 *
15657c478bd9Sstevel@tonic-gate 		 * This function will either return with login_pw == rpc_pw
15667c478bd9Sstevel@tonic-gate 		 * (and thus, the new pw to encrypt keys) or it will exit.
15677c478bd9Sstevel@tonic-gate 		 */
15687c478bd9Sstevel@tonic-gate 		cmp_passwd();
15697c478bd9Sstevel@tonic-gate 	}
15707c478bd9Sstevel@tonic-gate 
15717c478bd9Sstevel@tonic-gate 	if (makenew)
15727c478bd9Sstevel@tonic-gate 		makenewkeys();
15737c478bd9Sstevel@tonic-gate 	else
15747c478bd9Sstevel@tonic-gate 		getpublics();
15757c478bd9Sstevel@tonic-gate 
15767c478bd9Sstevel@tonic-gate 	encryptkeys();
15777c478bd9Sstevel@tonic-gate 
15787c478bd9Sstevel@tonic-gate 	storekeys();
15797c478bd9Sstevel@tonic-gate 
15807c478bd9Sstevel@tonic-gate 	if (makenew) {
15817c478bd9Sstevel@tonic-gate 		if (uid == 0) {
15827c478bd9Sstevel@tonic-gate 			if (mechs) {
15837c478bd9Sstevel@tonic-gate 				for (mcount = 0; CURMECH; mcount++) {
15847c478bd9Sstevel@tonic-gate 					if (!slist[mcount])
15857c478bd9Sstevel@tonic-gate 						continue;
15867c478bd9Sstevel@tonic-gate 					write_rootkey(slist[mcount],
15877c478bd9Sstevel@tonic-gate 							CURMECH->alias,
15887c478bd9Sstevel@tonic-gate 							CURMECH->keylen,
15897c478bd9Sstevel@tonic-gate 							CURMECH->algtype);
15907c478bd9Sstevel@tonic-gate 				}
15917c478bd9Sstevel@tonic-gate 			} else {
15927c478bd9Sstevel@tonic-gate 				assert(slist[0]);
15937c478bd9Sstevel@tonic-gate 				write_rootkey(slist[0], "des", 192, 0);
15947c478bd9Sstevel@tonic-gate 			}
15957c478bd9Sstevel@tonic-gate 		}
15967c478bd9Sstevel@tonic-gate 		if (mechs) {
15977c478bd9Sstevel@tonic-gate 			for (mcount = 0; CURMECH; mcount++)
15987c478bd9Sstevel@tonic-gate 				keylogin(CURMECH->keylen,
15997c478bd9Sstevel@tonic-gate 						CURMECH->algtype);
16007c478bd9Sstevel@tonic-gate 		} else
16017c478bd9Sstevel@tonic-gate 			keylogin_des();
16027c478bd9Sstevel@tonic-gate 	}
160349e7ca49Speteh 	return (0);
16047c478bd9Sstevel@tonic-gate }
1605