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