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