xref: /titanic_52/usr/src/cmd/keyserv/chkey_common.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 /*
237c478bd9Sstevel@tonic-gate  * 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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #include <stdio.h>
307c478bd9Sstevel@tonic-gate #include <stdlib.h>
317c478bd9Sstevel@tonic-gate #include <ctype.h>
327c478bd9Sstevel@tonic-gate #include <strings.h>
337c478bd9Sstevel@tonic-gate #include <pwd.h>
347c478bd9Sstevel@tonic-gate #include <shadow.h>
357c478bd9Sstevel@tonic-gate #include <netdb.h>
367c478bd9Sstevel@tonic-gate #include <mp.h>
377c478bd9Sstevel@tonic-gate #include <rpcsvc/nis.h>
387c478bd9Sstevel@tonic-gate #include <rpc/key_prot.h>
397c478bd9Sstevel@tonic-gate #include <nsswitch.h>
407c478bd9Sstevel@tonic-gate #include <ns_sldap.h>
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate extern char *crypt();
437c478bd9Sstevel@tonic-gate extern long random();
447c478bd9Sstevel@tonic-gate extern char *getpassphrase();
457c478bd9Sstevel@tonic-gate extern char *program_name;
467c478bd9Sstevel@tonic-gate static const char *CRED_TABLE = "cred.org_dir";
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate #define	ROOTKEY_FILE	"/etc/.rootkey"
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate #ifndef MAXHOSTNAMELEN
517c478bd9Sstevel@tonic-gate #define	MAXHOSTNAMELEN 256
527c478bd9Sstevel@tonic-gate #endif
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate #define	PK_FILES	1
557c478bd9Sstevel@tonic-gate #define	PK_YP		2
567c478bd9Sstevel@tonic-gate #define	PK_NISPLUS	3
577c478bd9Sstevel@tonic-gate #define	PK_LDAP		4
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate #define	LDAP_BINDDN_DEFAULT	"cn=Directory Manager"
607c478bd9Sstevel@tonic-gate #define	PROMPTGET_SUCCESS	1
617c478bd9Sstevel@tonic-gate #define	PROMPTGET_FAIL		-1
627c478bd9Sstevel@tonic-gate #define	PROMPTGET_MEMORY_FAIL	-2
637c478bd9Sstevel@tonic-gate #define	PASSWD_UNMATCHED	-3
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate #define	FREE_CREDINFO(s) \
667c478bd9Sstevel@tonic-gate 	if ((s)) { (void) memset((s), 0, strlen((s))); }
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate /* ************************ switch functions *************************** */
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate /*	NSW_NOTSUCCESS  NSW_NOTFOUND   NSW_UNAVAIL    NSW_TRYAGAIN */
727c478bd9Sstevel@tonic-gate #define	DEF_ACTION {__NSW_RETURN, __NSW_RETURN, __NSW_CONTINUE, __NSW_CONTINUE}
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate static struct __nsw_lookup lookup_files = {"files", DEF_ACTION, NULL, NULL},
757c478bd9Sstevel@tonic-gate 		lookup_nis = {"nis", DEF_ACTION, NULL, &lookup_files};
767c478bd9Sstevel@tonic-gate static struct __nsw_switchconfig publickey_default =
777c478bd9Sstevel@tonic-gate 			{0, "publickey", 2, &lookup_nis};
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate static int get_ldap_bindDN(char **);
807c478bd9Sstevel@tonic-gate static int get_ldap_bindPassword(char **);
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate /*
837c478bd9Sstevel@tonic-gate  * Prompt the users for a ldap bind DN. If users do not enter a value but just
847c478bd9Sstevel@tonic-gate  * simply hit the return key, the default bindDN "cn=Directory Manager"
857c478bd9Sstevel@tonic-gate  * will be used.
867c478bd9Sstevel@tonic-gate  */
877c478bd9Sstevel@tonic-gate static int
887c478bd9Sstevel@tonic-gate get_ldap_bindDN(char **ret_bindDN) {
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate 	char	bindDN[BUFSIZ];
917c478bd9Sstevel@tonic-gate 	char	prompt[BUFSIZ];
927c478bd9Sstevel@tonic-gate 	int	blen, pos;
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate 	/* set the initial value for bindDN buffer */
957c478bd9Sstevel@tonic-gate 	(void) memset(bindDN, 0, BUFSIZ);
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate 	(void) snprintf(prompt, BUFSIZ,
987c478bd9Sstevel@tonic-gate 	"\nThe LDAP bind DN and password are required for this update.\n"
997c478bd9Sstevel@tonic-gate 	"If you are not sure what values to enter, please contact your\n"
1007c478bd9Sstevel@tonic-gate 	"LDAP administrator.\n\nPlease enter LDAP bind DN [%s]: ",
1017c478bd9Sstevel@tonic-gate 	LDAP_BINDDN_DEFAULT);
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate 	printf(prompt);
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate 	if (fgets(bindDN, sizeof (bindDN), stdin) == NULL) {
1067c478bd9Sstevel@tonic-gate 		(void) strlcpy(bindDN, LDAP_BINDDN_DEFAULT, BUFSIZ);
1077c478bd9Sstevel@tonic-gate 	}
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate 	blen = strlen(bindDN);
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate 	/* Check if the buffer ends with a newline */
1127c478bd9Sstevel@tonic-gate 	if ((blen > 0) && (bindDN[blen - 1] == '\n')) {
1137c478bd9Sstevel@tonic-gate 		bindDN[blen - 1] = '\0';
1147c478bd9Sstevel@tonic-gate 		blen -= 1;
1157c478bd9Sstevel@tonic-gate 	}
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate 	/* Remove the white spaces */
1187c478bd9Sstevel@tonic-gate 	if (blen > 0) {
1197c478bd9Sstevel@tonic-gate 		for (pos = blen - 1; pos >= 0; pos--) {
1207c478bd9Sstevel@tonic-gate 			if (isspace(bindDN[pos]))
1217c478bd9Sstevel@tonic-gate 				bindDN[pos] = '\0';
1227c478bd9Sstevel@tonic-gate 			else
1237c478bd9Sstevel@tonic-gate 				break;
1247c478bd9Sstevel@tonic-gate 		}
1257c478bd9Sstevel@tonic-gate 	}
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate 	/* Use the default bindDN, if the buffer contains no characters */
1287c478bd9Sstevel@tonic-gate 	if (strlen(bindDN) == 0)
1297c478bd9Sstevel@tonic-gate 		(void) strlcpy(bindDN, LDAP_BINDDN_DEFAULT, BUFSIZ);
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate 	if ((*ret_bindDN = (char *)malloc(strlen(bindDN)+1)) == NULL) {
1327c478bd9Sstevel@tonic-gate 		(void) memset(bindDN, 0, BUFSIZ);
1337c478bd9Sstevel@tonic-gate 		return (PROMPTGET_MEMORY_FAIL);
1347c478bd9Sstevel@tonic-gate 	}
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate 	(void) strlcpy(*ret_bindDN, bindDN, strlen(bindDN)+1);
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate 	/* Clean up and erase the credential info */
1397c478bd9Sstevel@tonic-gate 	(void) memset(bindDN, 0, BUFSIZ);
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate 	return (PROMPTGET_SUCCESS);
1427c478bd9Sstevel@tonic-gate }
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate /*
1467c478bd9Sstevel@tonic-gate  * Prompt the user for a ldap bind password.
1477c478bd9Sstevel@tonic-gate  */
1487c478bd9Sstevel@tonic-gate static int
1497c478bd9Sstevel@tonic-gate get_ldap_bindPassword(char **ret_bindPass) {
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate 	char 	bindPassword[BUFSIZ];
1527c478bd9Sstevel@tonic-gate 	char	prompt[BUFSIZ];
1537c478bd9Sstevel@tonic-gate 	char	*bindPass = NULL;
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate 	/* set the initial value for bindPassword buffer */
1567c478bd9Sstevel@tonic-gate 	(void) memset(bindPassword, 0, BUFSIZ);
1577c478bd9Sstevel@tonic-gate 	*ret_bindPass = NULL;
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate 	(void) snprintf(prompt, BUFSIZ,
1607c478bd9Sstevel@tonic-gate 		"Please enter LDAP bind password: ");
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate 	bindPass = getpassphrase(prompt);
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate 	if (bindPass == NULL)
1657c478bd9Sstevel@tonic-gate 		return (PROMPTGET_FAIL);
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate 	(void) strlcpy(bindPassword, bindPass, BUFSIZ);
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate 	/* clean the static buffer returned from getpassphrase call */
1707c478bd9Sstevel@tonic-gate 	(void) memset(bindPass, 0, strlen(bindPass));
1717c478bd9Sstevel@tonic-gate 	bindPass = NULL;
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate 	/*
1747c478bd9Sstevel@tonic-gate 	 * Re-enter the bind passowrd and compare it with the one
1757c478bd9Sstevel@tonic-gate 	 * from previous entered.
1767c478bd9Sstevel@tonic-gate 	 */
1777c478bd9Sstevel@tonic-gate 	(void) snprintf(prompt, BUFSIZ,
1787c478bd9Sstevel@tonic-gate 		"Re-enter LDAP bind password to confirm: ");
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate 	bindPass = getpassphrase(prompt);
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate 	if (bindPass == NULL) {
1837c478bd9Sstevel@tonic-gate 		(void) memset(bindPassword, 0, BUFSIZ);
1847c478bd9Sstevel@tonic-gate 		return (PASSWD_UNMATCHED);
1857c478bd9Sstevel@tonic-gate 	}
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate 	if (strcmp(bindPass, bindPassword) != 0) {
1887c478bd9Sstevel@tonic-gate 		(void) memset(bindPassword, 0, BUFSIZ);
1897c478bd9Sstevel@tonic-gate 		(void) memset(bindPass, 0, strlen(bindPass));
1907c478bd9Sstevel@tonic-gate 		return (PASSWD_UNMATCHED);
1917c478bd9Sstevel@tonic-gate 	} else {
1927c478bd9Sstevel@tonic-gate 		(void) memset(bindPass, 0, strlen(bindPass));
1937c478bd9Sstevel@tonic-gate 		if ((*ret_bindPass = (char *)malloc(strlen(bindPassword)+1))
1947c478bd9Sstevel@tonic-gate 			== NULL) {
1957c478bd9Sstevel@tonic-gate 			(void) memset(bindPassword, 0, BUFSIZ);
1967c478bd9Sstevel@tonic-gate 			return (PROMPTGET_MEMORY_FAIL);
1977c478bd9Sstevel@tonic-gate 		}
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate 		(void) strlcpy(*ret_bindPass, bindPassword,
2007c478bd9Sstevel@tonic-gate 			strlen(bindPassword)+1);
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate 		/* Clean up and erase the credential info */
2037c478bd9Sstevel@tonic-gate 		(void) memset(bindPassword, 0, BUFSIZ);
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate 		return (PROMPTGET_SUCCESS);
2067c478bd9Sstevel@tonic-gate 	}
2077c478bd9Sstevel@tonic-gate }
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate char *
2127c478bd9Sstevel@tonic-gate switch_policy_str(struct __nsw_switchconfig *conf)
2137c478bd9Sstevel@tonic-gate {
2147c478bd9Sstevel@tonic-gate 	struct __nsw_lookup *look;
2157c478bd9Sstevel@tonic-gate 	static char policy[256];  /* 256 is enough for (nis, files...etc) */
2167c478bd9Sstevel@tonic-gate 	int previous = 0;
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate 	memset((char *)policy, 0, 256);
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate 	for (look = conf->lookups; look; look = look->next) {
2217c478bd9Sstevel@tonic-gate 		if (previous)
2227c478bd9Sstevel@tonic-gate 			strcat(policy, " ");
2237c478bd9Sstevel@tonic-gate 		strcat(policy, look->service_name);
2247c478bd9Sstevel@tonic-gate 		previous = 1;
2257c478bd9Sstevel@tonic-gate 	}
2267c478bd9Sstevel@tonic-gate 	return (policy);
2277c478bd9Sstevel@tonic-gate }
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate int
2307c478bd9Sstevel@tonic-gate no_switch_policy(struct __nsw_switchconfig *conf)
2317c478bd9Sstevel@tonic-gate {
2327c478bd9Sstevel@tonic-gate 	return (conf == NULL || conf->lookups == NULL);
2337c478bd9Sstevel@tonic-gate }
2347c478bd9Sstevel@tonic-gate 
235*49e7ca49Speteh int
2367c478bd9Sstevel@tonic-gate is_switch_policy(struct __nsw_switchconfig *conf, char *target)
2377c478bd9Sstevel@tonic-gate {
2387c478bd9Sstevel@tonic-gate 	return (conf &&
2397c478bd9Sstevel@tonic-gate 		conf->lookups &&
2407c478bd9Sstevel@tonic-gate 		strcmp(conf->lookups->service_name, target) == 0 &&
2417c478bd9Sstevel@tonic-gate 		conf->lookups->next == NULL);
2427c478bd9Sstevel@tonic-gate }
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate char *
2457c478bd9Sstevel@tonic-gate first_and_only_switch_policy(char *policy,
2467c478bd9Sstevel@tonic-gate 		    struct __nsw_switchconfig *default_conf,
2477c478bd9Sstevel@tonic-gate 		    char *head_msg)
2487c478bd9Sstevel@tonic-gate {
2497c478bd9Sstevel@tonic-gate 	struct __nsw_switchconfig *conf;
2507c478bd9Sstevel@tonic-gate 	enum __nsw_parse_err perr;
2517c478bd9Sstevel@tonic-gate 	int policy_correct = 1;
2527c478bd9Sstevel@tonic-gate 	char *target_service = 0;
2537c478bd9Sstevel@tonic-gate 	int use_default = 0;
2547c478bd9Sstevel@tonic-gate 
2557c478bd9Sstevel@tonic-gate 	if (default_conf == 0)
2567c478bd9Sstevel@tonic-gate 		default_conf = &publickey_default;
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate 	conf = __nsw_getconfig(policy, &perr);
2597c478bd9Sstevel@tonic-gate 	if (no_switch_policy(conf)) {
2607c478bd9Sstevel@tonic-gate 		use_default = 1;
2617c478bd9Sstevel@tonic-gate 		conf = default_conf;
2627c478bd9Sstevel@tonic-gate 	}
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate 	target_service = conf->lookups->service_name;
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate 	if (conf->lookups->next != NULL) {
2677c478bd9Sstevel@tonic-gate 		policy_correct = 0;
2687c478bd9Sstevel@tonic-gate 		if (use_default) {
2697c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
2707c478bd9Sstevel@tonic-gate 			"\n%s\n There is no publickey entry in %s.\n",
2717c478bd9Sstevel@tonic-gate 				head_msg, __NSW_CONFIG_FILE);
2727c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
2737c478bd9Sstevel@tonic-gate 			"The default publickey policy is \"publickey: %s\".\n",
2747c478bd9Sstevel@tonic-gate 			switch_policy_str(default_conf));
2757c478bd9Sstevel@tonic-gate 		} else
2767c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
2777c478bd9Sstevel@tonic-gate 		"\n%s\nThe publickey entry in %s is \"publickey: %s\".\n",
2787c478bd9Sstevel@tonic-gate 			head_msg, __NSW_CONFIG_FILE,
2797c478bd9Sstevel@tonic-gate 			switch_policy_str(conf));
2807c478bd9Sstevel@tonic-gate 	}
2817c478bd9Sstevel@tonic-gate 
2827c478bd9Sstevel@tonic-gate 	if (policy_correct == 0)
2837c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
2847c478bd9Sstevel@tonic-gate 	"I cannot figure out which publickey database you want to update.\n");
2857c478bd9Sstevel@tonic-gate 	if (!use_default && conf)
2867c478bd9Sstevel@tonic-gate 		__nsw_freeconfig(conf);
2877c478bd9Sstevel@tonic-gate 
2887c478bd9Sstevel@tonic-gate 	if (policy_correct)
2897c478bd9Sstevel@tonic-gate 		return (target_service);
2907c478bd9Sstevel@tonic-gate 	else
2917c478bd9Sstevel@tonic-gate 		return (0);
2927c478bd9Sstevel@tonic-gate }
2937c478bd9Sstevel@tonic-gate 
2947c478bd9Sstevel@tonic-gate 
2957c478bd9Sstevel@tonic-gate 
2967c478bd9Sstevel@tonic-gate int
2977c478bd9Sstevel@tonic-gate check_switch_policy(char *policy, char *target_service,
2987c478bd9Sstevel@tonic-gate 		    struct __nsw_switchconfig *default_conf,
2997c478bd9Sstevel@tonic-gate 		    char *head_msg, char *tail_msg)
3007c478bd9Sstevel@tonic-gate {
3017c478bd9Sstevel@tonic-gate 	struct __nsw_switchconfig *conf;
3027c478bd9Sstevel@tonic-gate 	enum __nsw_parse_err perr;
3037c478bd9Sstevel@tonic-gate 	int policy_correct = 1;
3047c478bd9Sstevel@tonic-gate 
3057c478bd9Sstevel@tonic-gate 	if (default_conf == 0)
3067c478bd9Sstevel@tonic-gate 		default_conf = &publickey_default;
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate 	conf = __nsw_getconfig(policy, &perr);
3097c478bd9Sstevel@tonic-gate 	if (no_switch_policy(conf)) {
3107c478bd9Sstevel@tonic-gate 		if (!is_switch_policy(default_conf, target_service)) {
3117c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
3127c478bd9Sstevel@tonic-gate 				"\n%s\nThere is no publickey entry in %s.\n",
3137c478bd9Sstevel@tonic-gate 				head_msg, __NSW_CONFIG_FILE);
3147c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
3157c478bd9Sstevel@tonic-gate 			"The default publickey policy is \"publickey: %s\".\n",
3167c478bd9Sstevel@tonic-gate 			switch_policy_str(default_conf));
3177c478bd9Sstevel@tonic-gate 			policy_correct = 0;
3187c478bd9Sstevel@tonic-gate 		}
3197c478bd9Sstevel@tonic-gate 	} else if (!is_switch_policy(conf, target_service)) {
3207c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
3217c478bd9Sstevel@tonic-gate 		"\n%s\nThe publickey entry in %s is \"publickey: %s\".\n",
3227c478bd9Sstevel@tonic-gate 			head_msg, __NSW_CONFIG_FILE,
3237c478bd9Sstevel@tonic-gate 			switch_policy_str(conf));
3247c478bd9Sstevel@tonic-gate 		policy_correct = 0;
3257c478bd9Sstevel@tonic-gate 	}
3267c478bd9Sstevel@tonic-gate 	/* should we exit ? */
3277c478bd9Sstevel@tonic-gate 	if (policy_correct == 0)
3287c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
3297c478bd9Sstevel@tonic-gate 		"It should be \"publickey: %s\"%s\n\n",
3307c478bd9Sstevel@tonic-gate 			target_service, tail_msg);
3317c478bd9Sstevel@tonic-gate 	if (conf)
3327c478bd9Sstevel@tonic-gate 		__nsw_freeconfig(conf);
3337c478bd9Sstevel@tonic-gate 
3347c478bd9Sstevel@tonic-gate 	return (policy_correct);
3357c478bd9Sstevel@tonic-gate }
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate int
3387c478bd9Sstevel@tonic-gate get_pk_source(char *pk_service)
3397c478bd9Sstevel@tonic-gate {
3407c478bd9Sstevel@tonic-gate 	int db = 0, got_from_switch = 0;
3417c478bd9Sstevel@tonic-gate 
3427c478bd9Sstevel@tonic-gate 	/* No service specified, try to figure out from switch */
3437c478bd9Sstevel@tonic-gate 	if (pk_service == 0) {
3447c478bd9Sstevel@tonic-gate 		pk_service = first_and_only_switch_policy("publickey", 0,
3457c478bd9Sstevel@tonic-gate 				"ERROR:");
3467c478bd9Sstevel@tonic-gate 		if (pk_service == 0)
3477c478bd9Sstevel@tonic-gate 			return (0);
3487c478bd9Sstevel@tonic-gate 		(void) fprintf(stdout,
3497c478bd9Sstevel@tonic-gate 			"Updating %s publickey database.\n",
3507c478bd9Sstevel@tonic-gate 			pk_service);
3517c478bd9Sstevel@tonic-gate 		got_from_switch = 1;
3527c478bd9Sstevel@tonic-gate 	}
3537c478bd9Sstevel@tonic-gate 
3547c478bd9Sstevel@tonic-gate 	if (strcmp(pk_service, "ldap") == 0)
3557c478bd9Sstevel@tonic-gate 		db = PK_LDAP;
3567c478bd9Sstevel@tonic-gate 	else if (strcmp(pk_service, "nisplus") == 0)
3577c478bd9Sstevel@tonic-gate 		db = PK_NISPLUS;
3587c478bd9Sstevel@tonic-gate 	else if (strcmp(pk_service, "nis") == 0)
3597c478bd9Sstevel@tonic-gate 		db = PK_YP;
3607c478bd9Sstevel@tonic-gate 	else if (strcmp(pk_service, "files") == 0)
3617c478bd9Sstevel@tonic-gate 		db = PK_FILES;
3627c478bd9Sstevel@tonic-gate 	else return (0);
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate 	/*
3657c478bd9Sstevel@tonic-gate 	 * If we didn't get service name from switch, check switch
3667c478bd9Sstevel@tonic-gate 	 * and print warning about it source of publickeys if not unique
3677c478bd9Sstevel@tonic-gate 	 */
3687c478bd9Sstevel@tonic-gate 	if (got_from_switch == 0)
3697c478bd9Sstevel@tonic-gate 		check_switch_policy("publickey", pk_service, 0, "WARNING:",
3707c478bd9Sstevel@tonic-gate 				    db == PK_FILES ? "" :
3717c478bd9Sstevel@tonic-gate 			    "; add 'files' if you want the 'nobody' key.");
3727c478bd9Sstevel@tonic-gate 
3737c478bd9Sstevel@tonic-gate 
3747c478bd9Sstevel@tonic-gate 	return (db); /* all passed */
3757c478bd9Sstevel@tonic-gate }
3767c478bd9Sstevel@tonic-gate 
3777c478bd9Sstevel@tonic-gate 
3787c478bd9Sstevel@tonic-gate /* ***************************** keylogin stuff *************************** */
3797c478bd9Sstevel@tonic-gate int
3807c478bd9Sstevel@tonic-gate keylogin(char *netname, char *secret)
3817c478bd9Sstevel@tonic-gate {
3827c478bd9Sstevel@tonic-gate 	struct key_netstarg netst;
3837c478bd9Sstevel@tonic-gate 
3847c478bd9Sstevel@tonic-gate 	netst.st_pub_key[0] = 0;
3857c478bd9Sstevel@tonic-gate 	memcpy(netst.st_priv_key, secret, HEXKEYBYTES);
3867c478bd9Sstevel@tonic-gate 	netst.st_netname = netname;
3877c478bd9Sstevel@tonic-gate 
3887c478bd9Sstevel@tonic-gate #ifdef NFS_AUTH
3897c478bd9Sstevel@tonic-gate 	nra.authtype = AUTH_DES;	/* only revoke DES creds */
3907c478bd9Sstevel@tonic-gate 	nra.uid = getuid();		/* use the real uid */
3917c478bd9Sstevel@tonic-gate 	if (_nfssys(NFS_REVAUTH, &nra) < 0) {
3927c478bd9Sstevel@tonic-gate 		perror("Warning: NFS credentials not destroyed");
3937c478bd9Sstevel@tonic-gate 		err = 1;
3947c478bd9Sstevel@tonic-gate 	}
3957c478bd9Sstevel@tonic-gate #endif
3967c478bd9Sstevel@tonic-gate 
3977c478bd9Sstevel@tonic-gate 
3987c478bd9Sstevel@tonic-gate 	/* do actual key login */
3997c478bd9Sstevel@tonic-gate 	if (key_setnet(&netst) < 0) {
4007c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
4017c478bd9Sstevel@tonic-gate 			"Could not set %s's secret key\n", netname);
4027c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "May be the keyserv is down?\n");
4037c478bd9Sstevel@tonic-gate 		return (0);
4047c478bd9Sstevel@tonic-gate 	}
4057c478bd9Sstevel@tonic-gate 
4067c478bd9Sstevel@tonic-gate 	return (1);
4077c478bd9Sstevel@tonic-gate }
4087c478bd9Sstevel@tonic-gate 
4097c478bd9Sstevel@tonic-gate /*
4107c478bd9Sstevel@tonic-gate  * XXX unused routine.
4117c478bd9Sstevel@tonic-gate  * Can not locate any routine that is using this write_rootkey()
4127c478bd9Sstevel@tonic-gate  * function. There are 2 other write_rootkey() routines defined in chkey.c
4137c478bd9Sstevel@tonic-gate  * and in cmd/rpcsvc/nis/utils/nisaddcred/makedhextcred.c.
4147c478bd9Sstevel@tonic-gate  *
4157c478bd9Sstevel@tonic-gate  * write unencrypted secret key into root key file
4167c478bd9Sstevel@tonic-gate  */
417*49e7ca49Speteh void
4187c478bd9Sstevel@tonic-gate write_rootkey(char *secret)
4197c478bd9Sstevel@tonic-gate {
4207c478bd9Sstevel@tonic-gate 	char sbuf[HEXKEYBYTES+2];
4217c478bd9Sstevel@tonic-gate 	int fd, len;
4227c478bd9Sstevel@tonic-gate 
4237c478bd9Sstevel@tonic-gate 	strcpy(sbuf, secret);
4247c478bd9Sstevel@tonic-gate 	strcat(sbuf, "\n");
4257c478bd9Sstevel@tonic-gate 	len = strlen(sbuf);
4267c478bd9Sstevel@tonic-gate 	sbuf[len] = '\0';
4277c478bd9Sstevel@tonic-gate 	unlink(ROOTKEY_FILE);
4287c478bd9Sstevel@tonic-gate 	if ((fd = open(ROOTKEY_FILE, O_WRONLY+O_CREAT, 0600)) != -1) {
4297c478bd9Sstevel@tonic-gate 		write(fd, sbuf, len+1);
4307c478bd9Sstevel@tonic-gate 		close(fd);
4317c478bd9Sstevel@tonic-gate 		(void) fprintf(stdout, "Wrote secret key into %s\n",
4327c478bd9Sstevel@tonic-gate 			ROOTKEY_FILE);
4337c478bd9Sstevel@tonic-gate 	} else {
4347c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "Could not open %s for update\n",
4357c478bd9Sstevel@tonic-gate 			ROOTKEY_FILE);
4367c478bd9Sstevel@tonic-gate 		perror(ROOTKEY_FILE);
4377c478bd9Sstevel@tonic-gate 	}
4387c478bd9Sstevel@tonic-gate }
4397c478bd9Sstevel@tonic-gate 
4407c478bd9Sstevel@tonic-gate /* ****************************** nisplus stuff ************************* */
4417c478bd9Sstevel@tonic-gate /* most of it gotten from nisaddcred */
4427c478bd9Sstevel@tonic-gate 
4437c478bd9Sstevel@tonic-gate 
4447c478bd9Sstevel@tonic-gate /* Check that someone else don't have the same auth information already */
4457c478bd9Sstevel@tonic-gate static
4467c478bd9Sstevel@tonic-gate nis_error
4477c478bd9Sstevel@tonic-gate auth_exists(char *princname, char *auth_name, char *auth_type, char *domain)
4487c478bd9Sstevel@tonic-gate {
4497c478bd9Sstevel@tonic-gate 	char sname[NIS_MAXNAMELEN+MAXHOSTNAMELEN+64];
4507c478bd9Sstevel@tonic-gate 	nis_result	*res;
4517c478bd9Sstevel@tonic-gate 	nis_error status;
4527c478bd9Sstevel@tonic-gate 	char *foundprinc;
4537c478bd9Sstevel@tonic-gate 
4547c478bd9Sstevel@tonic-gate 	(void) sprintf(sname, "[auth_name=%s,auth_type=%s],%s.%s",
4557c478bd9Sstevel@tonic-gate 		auth_name, auth_type, CRED_TABLE, domain);
4567c478bd9Sstevel@tonic-gate 	if (sname[strlen(sname)-1] != '.')
4577c478bd9Sstevel@tonic-gate 		strcat(sname, ".");
4587c478bd9Sstevel@tonic-gate 	/* Don't want FOLLOW_PATH here */
4597c478bd9Sstevel@tonic-gate 	res = nis_list(sname,
4607c478bd9Sstevel@tonic-gate 		MASTER_ONLY+USE_DGRAM+NO_AUTHINFO+FOLLOW_LINKS,
4617c478bd9Sstevel@tonic-gate 		NULL, NULL);
4627c478bd9Sstevel@tonic-gate 
4637c478bd9Sstevel@tonic-gate 	status = res->status;
4647c478bd9Sstevel@tonic-gate 	switch (res->status) {
4657c478bd9Sstevel@tonic-gate 	case NIS_NOTFOUND:
4667c478bd9Sstevel@tonic-gate 		break;
4677c478bd9Sstevel@tonic-gate 	case NIS_TRYAGAIN :
4687c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
4697c478bd9Sstevel@tonic-gate 			"%s: NIS+ server busy, try again later.\n",
4707c478bd9Sstevel@tonic-gate 			program_name);
4717c478bd9Sstevel@tonic-gate 		exit(1);
4727c478bd9Sstevel@tonic-gate 	case NIS_PERMISSION :
4737c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
4747c478bd9Sstevel@tonic-gate 		"%s: insufficient permission to look up old credentials.\n",
4757c478bd9Sstevel@tonic-gate 			program_name);
4767c478bd9Sstevel@tonic-gate 		exit(1);
4777c478bd9Sstevel@tonic-gate 	case NIS_SUCCESS:
4787c478bd9Sstevel@tonic-gate 		foundprinc = ENTRY_VAL(res->objects.objects_val, 0);
4797c478bd9Sstevel@tonic-gate 		if (nis_dir_cmp(foundprinc, princname) != SAME_NAME) {
4807c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
4817c478bd9Sstevel@tonic-gate 	"%s: %s credentials with auth_name '%s' already belong to '%s'.\n",
4827c478bd9Sstevel@tonic-gate 			program_name, auth_type, auth_name, foundprinc);
4837c478bd9Sstevel@tonic-gate 			exit(1);
4847c478bd9Sstevel@tonic-gate 		}
4857c478bd9Sstevel@tonic-gate 		break;
4867c478bd9Sstevel@tonic-gate 	default:
4877c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
4887c478bd9Sstevel@tonic-gate 			"%s: error looking at cred table, NIS+ error: %s\n",
4897c478bd9Sstevel@tonic-gate 			program_name, nis_sperrno(res->status));
4907c478bd9Sstevel@tonic-gate 		exit(1);
4917c478bd9Sstevel@tonic-gate 	}
4927c478bd9Sstevel@tonic-gate 	nis_freeresult(res);
4937c478bd9Sstevel@tonic-gate 	return (status);
4947c478bd9Sstevel@tonic-gate }
4957c478bd9Sstevel@tonic-gate 
4967c478bd9Sstevel@tonic-gate /* Returns 0 if check fails; 1 if successful. */
4977c478bd9Sstevel@tonic-gate static int
4987c478bd9Sstevel@tonic-gate sanity_checks(nis_princ, netname, domain)
4997c478bd9Sstevel@tonic-gate char	*nis_princ,
5007c478bd9Sstevel@tonic-gate 	*netname,
5017c478bd9Sstevel@tonic-gate 	*domain;
5027c478bd9Sstevel@tonic-gate {
5037c478bd9Sstevel@tonic-gate 	char	netdomainaux[MAXHOSTNAMELEN+1];
5047c478bd9Sstevel@tonic-gate 	char	*princdomain, *netdomain;
5057c478bd9Sstevel@tonic-gate 	int	len;
5067c478bd9Sstevel@tonic-gate 
5077c478bd9Sstevel@tonic-gate 	/* Sanity check 0. Do we have a nis+ principal name to work with? */
5087c478bd9Sstevel@tonic-gate 	if (nis_princ == NULL) {
5097c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
5107c478bd9Sstevel@tonic-gate 		"%s: you must create a \"LOCAL\" credential for '%s' first.\n",
5117c478bd9Sstevel@tonic-gate 			program_name, netname);
5127c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "\tSee nisaddcred(1).\n");
5137c478bd9Sstevel@tonic-gate 		return (0);
5147c478bd9Sstevel@tonic-gate 	}
5157c478bd9Sstevel@tonic-gate 
5167c478bd9Sstevel@tonic-gate 	/* Sanity check 0.5.  NIS+ principal names must be dotted. */
5177c478bd9Sstevel@tonic-gate 	len = strlen(nis_princ);
5187c478bd9Sstevel@tonic-gate 	if (nis_princ[len-1] != '.') {
5197c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
5207c478bd9Sstevel@tonic-gate 		"%s: invalid principal name: '%s' (forgot ending dot?).\n",
5217c478bd9Sstevel@tonic-gate 			program_name, nis_princ);
5227c478bd9Sstevel@tonic-gate 		return (0);
5237c478bd9Sstevel@tonic-gate 	}
5247c478bd9Sstevel@tonic-gate 
5257c478bd9Sstevel@tonic-gate 	/* Sanity check 1.  We only deal with one type of netnames. */
5267c478bd9Sstevel@tonic-gate 	if (strncmp(netname, "unix", 4) != 0) {
5277c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
5287c478bd9Sstevel@tonic-gate 			"%s: unrecognized netname type: '%s'.\n",
5297c478bd9Sstevel@tonic-gate 			program_name, netname);
5307c478bd9Sstevel@tonic-gate 		return (0);
5317c478bd9Sstevel@tonic-gate 	}
5327c478bd9Sstevel@tonic-gate 
5337c478bd9Sstevel@tonic-gate 	/* Sanity check 2.  Should only add DES cred in home domain. */
5347c478bd9Sstevel@tonic-gate 	princdomain = nis_domain_of(nis_princ);
5357c478bd9Sstevel@tonic-gate 	if (strcasecmp(princdomain, domain) != 0) {
5367c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
5377c478bd9Sstevel@tonic-gate "%s: domain of principal '%s' does not match destination domain '%s'.\n",
5387c478bd9Sstevel@tonic-gate 			program_name, nis_princ, domain);
5397c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
5407c478bd9Sstevel@tonic-gate 	"Should only add DES credential of principal in its home domain\n");
5417c478bd9Sstevel@tonic-gate 		return (0);
5427c478bd9Sstevel@tonic-gate 	}
5437c478bd9Sstevel@tonic-gate 
5447c478bd9Sstevel@tonic-gate 	/*
5457c478bd9Sstevel@tonic-gate 	 * Sanity check 3:  Make sure netname's domain same as principal's
5467c478bd9Sstevel@tonic-gate 	 * and don't have extraneous dot at the end.
5477c478bd9Sstevel@tonic-gate 	 */
5487c478bd9Sstevel@tonic-gate 	netdomain = (char *)strchr(netname, '@');
5497c478bd9Sstevel@tonic-gate 	if (! netdomain || netname[strlen(netname)-1] == '.') {
5507c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: invalid netname: '%s'. \n",
5517c478bd9Sstevel@tonic-gate 			program_name, netname);
5527c478bd9Sstevel@tonic-gate 		return (0);
5537c478bd9Sstevel@tonic-gate 	}
5547c478bd9Sstevel@tonic-gate 	netdomain++; /* skip '@' */
5557c478bd9Sstevel@tonic-gate 	/* make sure we don't run into buffer overflow */
5567c478bd9Sstevel@tonic-gate 	if (strlen(netdomain) > sizeof (netdomainaux))
5577c478bd9Sstevel@tonic-gate 		return (0);
5587c478bd9Sstevel@tonic-gate 	strcpy(netdomainaux, netdomain);
5597c478bd9Sstevel@tonic-gate 	if (netdomainaux[strlen(netdomainaux)-1] != '.')
5607c478bd9Sstevel@tonic-gate 		strcat(netdomainaux, ".");
5617c478bd9Sstevel@tonic-gate 
5627c478bd9Sstevel@tonic-gate 	if (strcasecmp(princdomain, netdomainaux) != 0) {
5637c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
5647c478bd9Sstevel@tonic-gate 	"%s: domain of netname %s should be same as that of principal %s\n",
5657c478bd9Sstevel@tonic-gate 			program_name, netname, nis_princ);
5667c478bd9Sstevel@tonic-gate 		return (0);
5677c478bd9Sstevel@tonic-gate 	}
5687c478bd9Sstevel@tonic-gate 
5697c478bd9Sstevel@tonic-gate 	/* Another principal owns same credentials? (exits if that happens) */
5707c478bd9Sstevel@tonic-gate 	(void) auth_exists(nis_princ, netname, "DES", domain);
5717c478bd9Sstevel@tonic-gate 
5727c478bd9Sstevel@tonic-gate 	return (1); /* all passed */
5737c478bd9Sstevel@tonic-gate }
5747c478bd9Sstevel@tonic-gate 
5757c478bd9Sstevel@tonic-gate 
5767c478bd9Sstevel@tonic-gate /*
5777c478bd9Sstevel@tonic-gate  * Similar to nis_local_principal in "nis_subr.c" except
5787c478bd9Sstevel@tonic-gate  * this gets the results from the MASTER_ONLY and no FOLLOW_PATH.
5797c478bd9Sstevel@tonic-gate  * We only want the master because we'll be making updates there,
5807c478bd9Sstevel@tonic-gate  * and also the replicas may not have seen the 'nisaddacred local'
5817c478bd9Sstevel@tonic-gate  * that may have just occurred.
5827c478bd9Sstevel@tonic-gate  * Returns NULL if not found.
5837c478bd9Sstevel@tonic-gate  */
5847c478bd9Sstevel@tonic-gate char *
5857c478bd9Sstevel@tonic-gate get_nisplus_principal(directory, uid)
5867c478bd9Sstevel@tonic-gate char	*directory;
5877c478bd9Sstevel@tonic-gate uid_t	uid;
5887c478bd9Sstevel@tonic-gate {
5897c478bd9Sstevel@tonic-gate 	nis_result	*res;
5907c478bd9Sstevel@tonic-gate 	char		buf[NIS_MAXNAMELEN + 1];
5917c478bd9Sstevel@tonic-gate 	int		status;
5927c478bd9Sstevel@tonic-gate 	static	char	principal_name[NIS_MAXNAMELEN + 1];
5937c478bd9Sstevel@tonic-gate 
5947c478bd9Sstevel@tonic-gate 	if (uid == 0)
5957c478bd9Sstevel@tonic-gate 		return (nis_local_host());
5967c478bd9Sstevel@tonic-gate 
5977c478bd9Sstevel@tonic-gate 	/* buf is enough to hold the string, no buffer overflow */
5987c478bd9Sstevel@tonic-gate 	(void) sprintf(buf, "[auth_name=%d,auth_type=LOCAL],%s.%s",
5997c478bd9Sstevel@tonic-gate 		uid, CRED_TABLE, directory);
6007c478bd9Sstevel@tonic-gate 
6017c478bd9Sstevel@tonic-gate 	if (buf[strlen(buf)-1] != '.')
6027c478bd9Sstevel@tonic-gate 		strcat(buf, ".");
6037c478bd9Sstevel@tonic-gate 
6047c478bd9Sstevel@tonic-gate 	res = nis_list(buf,
6057c478bd9Sstevel@tonic-gate 		MASTER_ONLY+USE_DGRAM+NO_AUTHINFO+FOLLOW_LINKS,
6067c478bd9Sstevel@tonic-gate 		NULL, NULL);
6077c478bd9Sstevel@tonic-gate 
6087c478bd9Sstevel@tonic-gate 	if (res == NULL) {
6097c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
6107c478bd9Sstevel@tonic-gate 			"%s: unable to get result from NIS+ server.",
6117c478bd9Sstevel@tonic-gate 			program_name);
6127c478bd9Sstevel@tonic-gate 		exit(1);
6137c478bd9Sstevel@tonic-gate 	}
6147c478bd9Sstevel@tonic-gate 	switch (res->status) {
6157c478bd9Sstevel@tonic-gate 	case NIS_SUCCESS:
6167c478bd9Sstevel@tonic-gate 		if (res->objects.objects_len > 1) {
6177c478bd9Sstevel@tonic-gate 			/*
6187c478bd9Sstevel@tonic-gate 			 * More than one principal with same uid?
6197c478bd9Sstevel@tonic-gate 			 * something wrong with cred table. Should be unique
6207c478bd9Sstevel@tonic-gate 			 * Warn user and continue.
6217c478bd9Sstevel@tonic-gate 			 */
6227c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
6237c478bd9Sstevel@tonic-gate 			"%s: LOCAL entry for %d in directory %s not unique",
6247c478bd9Sstevel@tonic-gate 				program_name, uid, directory);
6257c478bd9Sstevel@tonic-gate 		}
6267c478bd9Sstevel@tonic-gate 		/* make sure we don't run into buffer overflow */
6277c478bd9Sstevel@tonic-gate 		if (strlen(ENTRY_VAL(res->objects.objects_val, 0)) >
6287c478bd9Sstevel@tonic-gate 			sizeof (principal_name))
6297c478bd9Sstevel@tonic-gate 			return (NULL);
6307c478bd9Sstevel@tonic-gate 		strcpy(principal_name,
6317c478bd9Sstevel@tonic-gate 			ENTRY_VAL(res->objects.objects_val, 0));
6327c478bd9Sstevel@tonic-gate 		nis_freeresult(res);
6337c478bd9Sstevel@tonic-gate 		return (principal_name);
6347c478bd9Sstevel@tonic-gate 
6357c478bd9Sstevel@tonic-gate 	case NIS_NOTFOUND:
6367c478bd9Sstevel@tonic-gate 		nis_freeresult(res);
6377c478bd9Sstevel@tonic-gate 		return (NULL);
6387c478bd9Sstevel@tonic-gate 
6397c478bd9Sstevel@tonic-gate 	case NIS_TRYAGAIN :
6407c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
6417c478bd9Sstevel@tonic-gate 			"%s: NIS+ server busy, try again later.\n",
6427c478bd9Sstevel@tonic-gate 			program_name);
6437c478bd9Sstevel@tonic-gate 		exit(1);
6447c478bd9Sstevel@tonic-gate 
6457c478bd9Sstevel@tonic-gate 	case NIS_PERMISSION :
6467c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
6477c478bd9Sstevel@tonic-gate 			"%s: insufficient permission to update credentials.\n",
6487c478bd9Sstevel@tonic-gate 			program_name);
6497c478bd9Sstevel@tonic-gate 		exit(1);
6507c478bd9Sstevel@tonic-gate 
6517c478bd9Sstevel@tonic-gate 	default:
6527c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
6537c478bd9Sstevel@tonic-gate 			"%s: error talking to server, NIS+ error: %s.\n",
6547c478bd9Sstevel@tonic-gate 			program_name, nis_sperrno(res->status));
6557c478bd9Sstevel@tonic-gate 		exit(1);
6567c478bd9Sstevel@tonic-gate 	}
6577c478bd9Sstevel@tonic-gate 	return (NULL);
6587c478bd9Sstevel@tonic-gate }
6597c478bd9Sstevel@tonic-gate 
6607c478bd9Sstevel@tonic-gate 
6617c478bd9Sstevel@tonic-gate 
6627c478bd9Sstevel@tonic-gate /* Check whether this principal already has this type of credentials */
6637c478bd9Sstevel@tonic-gate static nis_error
6647c478bd9Sstevel@tonic-gate cred_exists(char *nisprinc, char *flavor, char *domain)
6657c478bd9Sstevel@tonic-gate {
6667c478bd9Sstevel@tonic-gate 	char sname[NIS_MAXNAMELEN+MAXHOSTNAMELEN+64];
6677c478bd9Sstevel@tonic-gate 	nis_result	*res;
6687c478bd9Sstevel@tonic-gate 	nis_error status;
6697c478bd9Sstevel@tonic-gate 
6707c478bd9Sstevel@tonic-gate 	(void) sprintf(sname, "[cname=\"%s\",auth_type=%s],%s.%s",
6717c478bd9Sstevel@tonic-gate 		nisprinc, flavor, CRED_TABLE, domain);
6727c478bd9Sstevel@tonic-gate 	if (sname[strlen(sname)-1] != '.')
6737c478bd9Sstevel@tonic-gate 		strcat(sname, ".");
6747c478bd9Sstevel@tonic-gate 
6757c478bd9Sstevel@tonic-gate 	/* Don't want FOLLOW_PATH here */
6767c478bd9Sstevel@tonic-gate 	res = nis_list(sname,
6777c478bd9Sstevel@tonic-gate 		MASTER_ONLY+USE_DGRAM+NO_AUTHINFO+FOLLOW_LINKS,
6787c478bd9Sstevel@tonic-gate 		NULL, NULL);
6797c478bd9Sstevel@tonic-gate 
6807c478bd9Sstevel@tonic-gate 	status = res->status;
6817c478bd9Sstevel@tonic-gate 	switch (status) {
6827c478bd9Sstevel@tonic-gate 	case NIS_NOTFOUND:
6837c478bd9Sstevel@tonic-gate 		break;
6847c478bd9Sstevel@tonic-gate 	case NIS_TRYAGAIN:
6857c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
6867c478bd9Sstevel@tonic-gate 			"%s: NIS+ server busy, try again later.\n",
6877c478bd9Sstevel@tonic-gate 			program_name);
6887c478bd9Sstevel@tonic-gate 		exit(1);
6897c478bd9Sstevel@tonic-gate 	case NIS_PERMISSION:
6907c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
6917c478bd9Sstevel@tonic-gate 		"%s: insufficient permission to look at credentials table\n",
6927c478bd9Sstevel@tonic-gate 			program_name);
6937c478bd9Sstevel@tonic-gate 		exit(1);
6947c478bd9Sstevel@tonic-gate 	case NIS_SUCCESS:
6957c478bd9Sstevel@tonic-gate 	case NIS_S_SUCCESS:
6967c478bd9Sstevel@tonic-gate 		break;
6977c478bd9Sstevel@tonic-gate 	default:
6987c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
6997c478bd9Sstevel@tonic-gate 			"%s: error looking at cred table, NIS+ error: %s\n",
7007c478bd9Sstevel@tonic-gate 			program_name, nis_sperrno(res->status));
7017c478bd9Sstevel@tonic-gate 		exit(1);
7027c478bd9Sstevel@tonic-gate 	}
7037c478bd9Sstevel@tonic-gate 	nis_freeresult(res);
7047c478bd9Sstevel@tonic-gate 	return (status);
7057c478bd9Sstevel@tonic-gate }
7067c478bd9Sstevel@tonic-gate 
7077c478bd9Sstevel@tonic-gate 
7087c478bd9Sstevel@tonic-gate /* Returns 0 if operation fails; 1 if successful. */
7097c478bd9Sstevel@tonic-gate static
7107c478bd9Sstevel@tonic-gate int
7117c478bd9Sstevel@tonic-gate modify_cred_obj(obj, domain)
7127c478bd9Sstevel@tonic-gate 	char *domain;
7137c478bd9Sstevel@tonic-gate 	nis_object *obj;
7147c478bd9Sstevel@tonic-gate {
7157c478bd9Sstevel@tonic-gate 	int status = 0;
7167c478bd9Sstevel@tonic-gate 	char sname[NIS_MAXNAMELEN+1];
7177c478bd9Sstevel@tonic-gate 	nis_result	*res;
7187c478bd9Sstevel@tonic-gate 
7197c478bd9Sstevel@tonic-gate 	(void) sprintf(sname, "%s.%s", CRED_TABLE, domain);
7207c478bd9Sstevel@tonic-gate 	res = nis_modify_entry(sname, obj, 0);
7217c478bd9Sstevel@tonic-gate 	switch (res->status) {
7227c478bd9Sstevel@tonic-gate 	case NIS_TRYAGAIN :
7237c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
7247c478bd9Sstevel@tonic-gate 			"%s: NIS+ server busy, try again later.\n",
7257c478bd9Sstevel@tonic-gate 			program_name);
7267c478bd9Sstevel@tonic-gate 		exit(1);
7277c478bd9Sstevel@tonic-gate 	case NIS_PERMISSION :
7287c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
7297c478bd9Sstevel@tonic-gate 			"%s: insufficient permission to update credentials.\n",
7307c478bd9Sstevel@tonic-gate 			program_name);
7317c478bd9Sstevel@tonic-gate 		exit(1);
7327c478bd9Sstevel@tonic-gate 	case NIS_SUCCESS :
7337c478bd9Sstevel@tonic-gate 		status = 1;
7347c478bd9Sstevel@tonic-gate 		break;
7357c478bd9Sstevel@tonic-gate 	default:
7367c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
7377c478bd9Sstevel@tonic-gate 			"%s: error modifying credential, NIS+ error: %s.\n",
7387c478bd9Sstevel@tonic-gate 			program_name, nis_sperrno(res->status));
7397c478bd9Sstevel@tonic-gate 		exit(1);
7407c478bd9Sstevel@tonic-gate 	}
7417c478bd9Sstevel@tonic-gate 	nis_freeresult(res);
7427c478bd9Sstevel@tonic-gate 	return (status);
7437c478bd9Sstevel@tonic-gate }
7447c478bd9Sstevel@tonic-gate 
7457c478bd9Sstevel@tonic-gate 
7467c478bd9Sstevel@tonic-gate static
7477c478bd9Sstevel@tonic-gate int
7487c478bd9Sstevel@tonic-gate add_cred_obj(obj, domain)
7497c478bd9Sstevel@tonic-gate 	char *domain;
7507c478bd9Sstevel@tonic-gate 	nis_object *obj;
7517c478bd9Sstevel@tonic-gate {
7527c478bd9Sstevel@tonic-gate 	int status = 0;
7537c478bd9Sstevel@tonic-gate 	char sname[NIS_MAXNAMELEN+1];
7547c478bd9Sstevel@tonic-gate 	nis_result	*res;
7557c478bd9Sstevel@tonic-gate 
7567c478bd9Sstevel@tonic-gate 	/* Assume check for cred_exists performed already */
7577c478bd9Sstevel@tonic-gate 
7587c478bd9Sstevel@tonic-gate 	(void) sprintf(sname, "%s.%s", CRED_TABLE, domain);
7597c478bd9Sstevel@tonic-gate 	res = nis_add_entry(sname, obj, 0);
7607c478bd9Sstevel@tonic-gate 	switch (res->status) {
7617c478bd9Sstevel@tonic-gate 	case NIS_TRYAGAIN :
7627c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
7637c478bd9Sstevel@tonic-gate 			"%s: NIS+ server busy, try again later.\n",
7647c478bd9Sstevel@tonic-gate 			program_name);
7657c478bd9Sstevel@tonic-gate 		exit(1);
7667c478bd9Sstevel@tonic-gate 	case NIS_PERMISSION :
7677c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
7687c478bd9Sstevel@tonic-gate 			"%s: insufficient permission to update credentials.\n",
7697c478bd9Sstevel@tonic-gate 			program_name);
7707c478bd9Sstevel@tonic-gate 		exit(1);
7717c478bd9Sstevel@tonic-gate 	case NIS_SUCCESS :
7727c478bd9Sstevel@tonic-gate 		status = 1;
7737c478bd9Sstevel@tonic-gate 		break;
7747c478bd9Sstevel@tonic-gate 	default:
7757c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
7767c478bd9Sstevel@tonic-gate 			"%s: error creating credential, NIS+ error: %s.\n",
7777c478bd9Sstevel@tonic-gate 			program_name, nis_sperrno(res->status));
7787c478bd9Sstevel@tonic-gate 		exit(1);
7797c478bd9Sstevel@tonic-gate 	}
7807c478bd9Sstevel@tonic-gate 	nis_freeresult(res);
7817c478bd9Sstevel@tonic-gate 	return (status);
7827c478bd9Sstevel@tonic-gate }
7837c478bd9Sstevel@tonic-gate 
7847c478bd9Sstevel@tonic-gate nis_object *
7857c478bd9Sstevel@tonic-gate init_entry()
7867c478bd9Sstevel@tonic-gate {
7877c478bd9Sstevel@tonic-gate 	static nis_object	obj;
7887c478bd9Sstevel@tonic-gate 	static entry_col	cred_data[10];
7897c478bd9Sstevel@tonic-gate 	entry_obj		*eo;
7907c478bd9Sstevel@tonic-gate 
7917c478bd9Sstevel@tonic-gate 	memset((char *)(&obj), 0, sizeof (obj));
7927c478bd9Sstevel@tonic-gate 	memset((char *)(cred_data), 0, sizeof (entry_col) * 10);
7937c478bd9Sstevel@tonic-gate 
7947c478bd9Sstevel@tonic-gate 	obj.zo_name = "cred";
7957c478bd9Sstevel@tonic-gate 	obj.zo_group = "";
7967c478bd9Sstevel@tonic-gate 	obj.zo_ttl = 43200;
7977c478bd9Sstevel@tonic-gate 	obj.zo_data.zo_type = NIS_ENTRY_OBJ;
7987c478bd9Sstevel@tonic-gate 	eo = &(obj.EN_data);
7997c478bd9Sstevel@tonic-gate 	eo->en_type = "cred_tbl";
8007c478bd9Sstevel@tonic-gate 	eo->en_cols.en_cols_val = cred_data;
8017c478bd9Sstevel@tonic-gate 	eo->en_cols.en_cols_len = 5;
8027c478bd9Sstevel@tonic-gate 	cred_data[4].ec_flags |= EN_CRYPT;
8037c478bd9Sstevel@tonic-gate 	return (&obj);
8047c478bd9Sstevel@tonic-gate }
8057c478bd9Sstevel@tonic-gate 
8067c478bd9Sstevel@tonic-gate 
8077c478bd9Sstevel@tonic-gate static char	*attrFilter[] = {
8087c478bd9Sstevel@tonic-gate 	"objectclass",
8097c478bd9Sstevel@tonic-gate 	"nispublickey",
8107c478bd9Sstevel@tonic-gate 	"nissecretkey",
8117c478bd9Sstevel@tonic-gate 	(char *)NULL
8127c478bd9Sstevel@tonic-gate };
8137c478bd9Sstevel@tonic-gate 
8147c478bd9Sstevel@tonic-gate 
8157c478bd9Sstevel@tonic-gate /* Determines if there is a NisKeyObject objectclass in a given entry */
8167c478bd9Sstevel@tonic-gate static int
8177c478bd9Sstevel@tonic-gate ldap_keyobj_exist(ns_ldap_entry_t *entry)
8187c478bd9Sstevel@tonic-gate {
8197c478bd9Sstevel@tonic-gate 	char		**fattrs;
8207c478bd9Sstevel@tonic-gate 
8217c478bd9Sstevel@tonic-gate 	fattrs = __ns_ldap_getAttr(entry, "objectClass");
8227c478bd9Sstevel@tonic-gate 
8237c478bd9Sstevel@tonic-gate 	if (fattrs == NULL)
8247c478bd9Sstevel@tonic-gate 		return (1);
8257c478bd9Sstevel@tonic-gate 
8267c478bd9Sstevel@tonic-gate 	while (*fattrs) {
8277c478bd9Sstevel@tonic-gate 		if (strcasecmp("NisKeyObject", *fattrs) == 0)
8287c478bd9Sstevel@tonic-gate 			return (1);
8297c478bd9Sstevel@tonic-gate 		fattrs++;
8307c478bd9Sstevel@tonic-gate 	}
8317c478bd9Sstevel@tonic-gate 
8327c478bd9Sstevel@tonic-gate 	return (0);
8337c478bd9Sstevel@tonic-gate }
8347c478bd9Sstevel@tonic-gate 
8357c478bd9Sstevel@tonic-gate 
8367c478bd9Sstevel@tonic-gate static char *keyAttrs[] = {
8377c478bd9Sstevel@tonic-gate 	"nispublickey",
8387c478bd9Sstevel@tonic-gate 	"nissecretkey",
8397c478bd9Sstevel@tonic-gate 	NULL
8407c478bd9Sstevel@tonic-gate };
8417c478bd9Sstevel@tonic-gate 
8427c478bd9Sstevel@tonic-gate /*
8437c478bd9Sstevel@tonic-gate  * Replace or append new attribute value(s) to an attribute.
8447c478bd9Sstevel@tonic-gate  * Don't care about memory leaks, because program is short running.
8457c478bd9Sstevel@tonic-gate  */
8467c478bd9Sstevel@tonic-gate 
8477c478bd9Sstevel@tonic-gate static int
8487c478bd9Sstevel@tonic-gate ldap_attr_mod(ns_ldap_entry_t *entry,
8497c478bd9Sstevel@tonic-gate 	    char *mechname,
8507c478bd9Sstevel@tonic-gate 	    char *public,
8517c478bd9Sstevel@tonic-gate 	    ns_ldap_attr_t **pkeyattrs,
8527c478bd9Sstevel@tonic-gate 	    char *crypt,
8537c478bd9Sstevel@tonic-gate 	    ns_ldap_attr_t **ckeyattrs)
8547c478bd9Sstevel@tonic-gate {
8557c478bd9Sstevel@tonic-gate 	char		**alist[2];
8567c478bd9Sstevel@tonic-gate 	char		*keys[2];
8577c478bd9Sstevel@tonic-gate 
8587c478bd9Sstevel@tonic-gate 	char		*mechfilter;
8597c478bd9Sstevel@tonic-gate 	int		mechfilterlen;
8607c478bd9Sstevel@tonic-gate 	int		q = 0;
8617c478bd9Sstevel@tonic-gate 	int		i, j;
8627c478bd9Sstevel@tonic-gate 	int		keycount[] = {0, 0};
8637c478bd9Sstevel@tonic-gate 	ns_ldap_attr_t	*attrs;
8647c478bd9Sstevel@tonic-gate 
8657c478bd9Sstevel@tonic-gate 	keys[0] = public;
8667c478bd9Sstevel@tonic-gate 	keys[1] = crypt;
8677c478bd9Sstevel@tonic-gate 
8687c478bd9Sstevel@tonic-gate 	mechfilter = (char *)malloc(strlen(mechname) + 3);
8697c478bd9Sstevel@tonic-gate 	if (mechfilter == NULL)
8707c478bd9Sstevel@tonic-gate 		return (0);
8717c478bd9Sstevel@tonic-gate 	sprintf(mechfilter, "{%s}", mechname);
8727c478bd9Sstevel@tonic-gate 	mechfilterlen = strlen(mechfilter);
8737c478bd9Sstevel@tonic-gate 
8747c478bd9Sstevel@tonic-gate 	for (q = 0; keyAttrs[q] != NULL; q++) {
8757c478bd9Sstevel@tonic-gate 		int		found = 0;
8767c478bd9Sstevel@tonic-gate 
8777c478bd9Sstevel@tonic-gate 		for (i = 0; i < entry->attr_count; i++) {
8787c478bd9Sstevel@tonic-gate 			int		rep = 0;
8797c478bd9Sstevel@tonic-gate 			ns_ldap_attr_t	*attr = entry->attr_pair[i];
8807c478bd9Sstevel@tonic-gate 			char		*name = attr->attrname;
8817c478bd9Sstevel@tonic-gate 			int		count = 0;
8827c478bd9Sstevel@tonic-gate 
8837c478bd9Sstevel@tonic-gate 			if (strcasecmp(keyAttrs[q], name) == 0) {
8847c478bd9Sstevel@tonic-gate 				found++;
8857c478bd9Sstevel@tonic-gate 				count = attr->value_count;
8867c478bd9Sstevel@tonic-gate 		alist[q] = (char **)malloc(sizeof (char *) * (count + 1));
8877c478bd9Sstevel@tonic-gate 				if (alist[q] == NULL)
8887c478bd9Sstevel@tonic-gate 					return (0);
8897c478bd9Sstevel@tonic-gate 				alist[q][attr->value_count] = NULL;
8907c478bd9Sstevel@tonic-gate 				for (j = 0; j < attr->value_count; j++) {
8917c478bd9Sstevel@tonic-gate 					char	*val = attr->attrvalue[j];
8927c478bd9Sstevel@tonic-gate 					if (strncasecmp(val, mechfilter,
8937c478bd9Sstevel@tonic-gate 							mechfilterlen) == 0) {
8947c478bd9Sstevel@tonic-gate 						/* Replace entry */
8957c478bd9Sstevel@tonic-gate 						rep++;
8967c478bd9Sstevel@tonic-gate 						alist[q][j] = keys[q];
8977c478bd9Sstevel@tonic-gate 					} else
8987c478bd9Sstevel@tonic-gate 						alist[q][j] = val;
8997c478bd9Sstevel@tonic-gate 					++keycount[q];
9007c478bd9Sstevel@tonic-gate 				}
9017c478bd9Sstevel@tonic-gate 				if (!rep) {
9027c478bd9Sstevel@tonic-gate 					/* Add entry to list */
9037c478bd9Sstevel@tonic-gate 					alist[q] = (char **)realloc(alist[q],
9047c478bd9Sstevel@tonic-gate 					    sizeof (char *) * (count + 2));
9057c478bd9Sstevel@tonic-gate 					if (alist[q] == NULL)
9067c478bd9Sstevel@tonic-gate 						return (0);
9077c478bd9Sstevel@tonic-gate 					alist[q][attr->value_count + 1] = NULL;
9087c478bd9Sstevel@tonic-gate 					alist[q][attr->value_count] = keys[q];
9097c478bd9Sstevel@tonic-gate 					++keycount[q];
9107c478bd9Sstevel@tonic-gate 				}
9117c478bd9Sstevel@tonic-gate 			}
9127c478bd9Sstevel@tonic-gate 		}
9137c478bd9Sstevel@tonic-gate 		if (!found) {
9147c478bd9Sstevel@tonic-gate 			/* Attribute does not exist, add entry anyways */
9157c478bd9Sstevel@tonic-gate 			alist[q] = (char **)malloc(sizeof (char *) * 2);
9167c478bd9Sstevel@tonic-gate 			if (alist[q] == NULL)
9177c478bd9Sstevel@tonic-gate 				return (0);
9187c478bd9Sstevel@tonic-gate 			alist[q][0] = keys[q];
9197c478bd9Sstevel@tonic-gate 			alist[q][1] = NULL;
9207c478bd9Sstevel@tonic-gate 			++keycount[q];
9217c478bd9Sstevel@tonic-gate 		}
9227c478bd9Sstevel@tonic-gate 	}
9237c478bd9Sstevel@tonic-gate 	if ((attrs = (ns_ldap_attr_t *)calloc(1,
9247c478bd9Sstevel@tonic-gate 			sizeof (ns_ldap_attr_t))) == NULL)
9257c478bd9Sstevel@tonic-gate 		return (0);
9267c478bd9Sstevel@tonic-gate 	attrs->attrname = "nisPublicKey";
9277c478bd9Sstevel@tonic-gate 	attrs->attrvalue = alist[0];
9287c478bd9Sstevel@tonic-gate 	attrs->value_count = keycount[0];
9297c478bd9Sstevel@tonic-gate 	*pkeyattrs = attrs;
9307c478bd9Sstevel@tonic-gate 
9317c478bd9Sstevel@tonic-gate 	if ((attrs = (ns_ldap_attr_t *)calloc(1,
9327c478bd9Sstevel@tonic-gate 			sizeof (ns_ldap_attr_t))) == NULL)
9337c478bd9Sstevel@tonic-gate 		return (0);
9347c478bd9Sstevel@tonic-gate 	attrs->attrname = "nisSecretKey";
9357c478bd9Sstevel@tonic-gate 	attrs->attrvalue = alist[1];
9367c478bd9Sstevel@tonic-gate 	attrs->value_count = keycount[1];
9377c478bd9Sstevel@tonic-gate 	*ckeyattrs = attrs;
9387c478bd9Sstevel@tonic-gate 	return (1);
9397c478bd9Sstevel@tonic-gate }
9407c478bd9Sstevel@tonic-gate 
9417c478bd9Sstevel@tonic-gate 
9427c478bd9Sstevel@tonic-gate /*
9437c478bd9Sstevel@tonic-gate  * Do the actual Add or update of attributes in attrs.
9447c478bd9Sstevel@tonic-gate  * The parameter 'update4host' is a flag that tells the function which
9457c478bd9Sstevel@tonic-gate  * DN and password should be used to bind to ldap. If it is an update
9467c478bd9Sstevel@tonic-gate  * for a host (update4host > 0), the two parameters "bindDN" and
9477c478bd9Sstevel@tonic-gate  * "bindPasswd" would be used to bind as the directory manager,
9487c478bd9Sstevel@tonic-gate  * otherwise "dn" and "passwd" would be used to bind as an individual
9497c478bd9Sstevel@tonic-gate  * user.
9507c478bd9Sstevel@tonic-gate  */
9517c478bd9Sstevel@tonic-gate static void
9527c478bd9Sstevel@tonic-gate update_ldap_attr(const char *dn,
9537c478bd9Sstevel@tonic-gate 		ns_ldap_attr_t **attrs,
9547c478bd9Sstevel@tonic-gate 		const char *passwd,
9557c478bd9Sstevel@tonic-gate 		int add,
9567c478bd9Sstevel@tonic-gate 		int update4host,
9577c478bd9Sstevel@tonic-gate 		const char *bindDN,
9587c478bd9Sstevel@tonic-gate 		const char *bindPasswd)
9597c478bd9Sstevel@tonic-gate {
9607c478bd9Sstevel@tonic-gate 	int		ldaprc;
9617c478bd9Sstevel@tonic-gate 	int		authstried = 0;
9627c478bd9Sstevel@tonic-gate 	char		*msg;
9637c478bd9Sstevel@tonic-gate 	char		*ldap_pw;
9647c478bd9Sstevel@tonic-gate 	char		**certpath = NULL;
9657c478bd9Sstevel@tonic-gate 	ns_auth_t	**app;
9667c478bd9Sstevel@tonic-gate 	ns_auth_t	**authpp = NULL;
9677c478bd9Sstevel@tonic-gate 	ns_auth_t	*authp = NULL;
9687c478bd9Sstevel@tonic-gate 	ns_cred_t	*credp;
9697c478bd9Sstevel@tonic-gate 	ns_ldap_error_t	*errorp = NULL;
9707c478bd9Sstevel@tonic-gate 	int		status;
9717c478bd9Sstevel@tonic-gate 
9727c478bd9Sstevel@tonic-gate 	if ((credp = (ns_cred_t *)calloc(1, sizeof (ns_cred_t))) == NULL) {
9737c478bd9Sstevel@tonic-gate 		fprintf(stderr, "Can not allocate cred buffer.\n");
9747c478bd9Sstevel@tonic-gate 		goto out;
9757c478bd9Sstevel@tonic-gate 	}
9767c478bd9Sstevel@tonic-gate 
9777c478bd9Sstevel@tonic-gate 	/*
9787c478bd9Sstevel@tonic-gate 	 * if this is an update for host, use the bindDN from the
9797c478bd9Sstevel@tonic-gate 	 * command prompt, otherwise use user's DN directly.
9807c478bd9Sstevel@tonic-gate 	 */
9817c478bd9Sstevel@tonic-gate 	if (update4host)
9827c478bd9Sstevel@tonic-gate 		credp->cred.unix_cred.userID = strdup(bindDN);
9837c478bd9Sstevel@tonic-gate 	else
9847c478bd9Sstevel@tonic-gate 		credp->cred.unix_cred.userID = strdup(dn);
9857c478bd9Sstevel@tonic-gate 
9867c478bd9Sstevel@tonic-gate 	if (credp->cred.unix_cred.userID == NULL) {
9877c478bd9Sstevel@tonic-gate 		fprintf(stderr, "Memory allocation failure (userID)\n");
9887c478bd9Sstevel@tonic-gate 		goto out;
9897c478bd9Sstevel@tonic-gate 	}
9907c478bd9Sstevel@tonic-gate 
9917c478bd9Sstevel@tonic-gate 	if (update4host) {
9927c478bd9Sstevel@tonic-gate 		credp->cred.unix_cred.passwd = strdup(bindPasswd);
9937c478bd9Sstevel@tonic-gate 	} else {
9947c478bd9Sstevel@tonic-gate 		if (passwd)
9957c478bd9Sstevel@tonic-gate 			credp->cred.unix_cred.passwd = strdup(passwd);
9967c478bd9Sstevel@tonic-gate 		else {
9977c478bd9Sstevel@tonic-gate 			/* Make sure a valid password is received. */
9987c478bd9Sstevel@tonic-gate 			status = get_ldap_bindPassword(&ldap_pw);
9997c478bd9Sstevel@tonic-gate 
10007c478bd9Sstevel@tonic-gate 			if (status != PROMPTGET_SUCCESS) {
10017c478bd9Sstevel@tonic-gate 				if (!ldap_pw)
10027c478bd9Sstevel@tonic-gate 					free(ldap_pw);
10037c478bd9Sstevel@tonic-gate 				goto out;
10047c478bd9Sstevel@tonic-gate 			}
10057c478bd9Sstevel@tonic-gate 			credp->cred.unix_cred.passwd = ldap_pw;
10067c478bd9Sstevel@tonic-gate 		}
10077c478bd9Sstevel@tonic-gate 	}
10087c478bd9Sstevel@tonic-gate 
10097c478bd9Sstevel@tonic-gate 	if (credp->cred.unix_cred.passwd == NULL) {
10107c478bd9Sstevel@tonic-gate 		fprintf(stderr, "Memory allocation failure (passwd)\n");
10117c478bd9Sstevel@tonic-gate 		goto out;
10127c478bd9Sstevel@tonic-gate 	}
10137c478bd9Sstevel@tonic-gate 
10147c478bd9Sstevel@tonic-gate 	/* get host certificate path, if one is configured */
10157c478bd9Sstevel@tonic-gate 	if (__ns_ldap_getParam(NS_LDAP_HOST_CERTPATH_P,
10167c478bd9Sstevel@tonic-gate 		(void ***)&certpath, &errorp) != NS_LDAP_SUCCESS)
10177c478bd9Sstevel@tonic-gate 		goto out;
10187c478bd9Sstevel@tonic-gate 
10197c478bd9Sstevel@tonic-gate 	if (certpath && *certpath)
10207c478bd9Sstevel@tonic-gate 		credp->hostcertpath = *certpath;
10217c478bd9Sstevel@tonic-gate 
10227c478bd9Sstevel@tonic-gate 	/* Load the service specific authentication method */
10237c478bd9Sstevel@tonic-gate 	if (__ns_ldap_getServiceAuthMethods("keyserv", &authpp, &errorp) !=
10247c478bd9Sstevel@tonic-gate 		NS_LDAP_SUCCESS)
10257c478bd9Sstevel@tonic-gate 		goto out;
10267c478bd9Sstevel@tonic-gate 
10277c478bd9Sstevel@tonic-gate 	/*
10287c478bd9Sstevel@tonic-gate 	 * if authpp is null, there is no serviceAuthenticationMethod
10297c478bd9Sstevel@tonic-gate 	 * try default authenticationMethod
10307c478bd9Sstevel@tonic-gate 	 */
10317c478bd9Sstevel@tonic-gate 	if (authpp == NULL) {
10327c478bd9Sstevel@tonic-gate 		if (__ns_ldap_getParam(NS_LDAP_AUTH_P, (void ***)&authpp,
10337c478bd9Sstevel@tonic-gate 			&errorp) != NS_LDAP_SUCCESS)
10347c478bd9Sstevel@tonic-gate 			goto out;
10357c478bd9Sstevel@tonic-gate 	}
10367c478bd9Sstevel@tonic-gate 
10377c478bd9Sstevel@tonic-gate 	/*
10387c478bd9Sstevel@tonic-gate 	 * if authpp is still null, then can not authenticate, log
10397c478bd9Sstevel@tonic-gate 	 * error message and return error
10407c478bd9Sstevel@tonic-gate 	 */
10417c478bd9Sstevel@tonic-gate 	if (authpp == NULL) {
10427c478bd9Sstevel@tonic-gate 		fprintf(stderr, "No LDAP authentication method configured.\n"
10437c478bd9Sstevel@tonic-gate 			" configured.\n");
10447c478bd9Sstevel@tonic-gate 		goto out;
10457c478bd9Sstevel@tonic-gate 	}
10467c478bd9Sstevel@tonic-gate 
10477c478bd9Sstevel@tonic-gate 	/*
10487c478bd9Sstevel@tonic-gate 	 * Walk the array and try all authentication methods in order except
10497c478bd9Sstevel@tonic-gate 	 * for "none".
10507c478bd9Sstevel@tonic-gate 	 */
10517c478bd9Sstevel@tonic-gate 	for (app = authpp; *app; app++) {
10527c478bd9Sstevel@tonic-gate 		authp = *app;
10537c478bd9Sstevel@tonic-gate 		/* what about disabling other mechanisms? "tls:sasl/EXTERNAL" */
10547c478bd9Sstevel@tonic-gate 		if (authp->type == NS_LDAP_AUTH_NONE)
10557c478bd9Sstevel@tonic-gate 			continue;
10567c478bd9Sstevel@tonic-gate 		authstried++;
10577c478bd9Sstevel@tonic-gate 		credp->auth.type = authp->type;
10587c478bd9Sstevel@tonic-gate 		credp->auth.tlstype = authp->tlstype;
10597c478bd9Sstevel@tonic-gate 		credp->auth.saslmech = authp->saslmech;
10607c478bd9Sstevel@tonic-gate 		credp->auth.saslopt = authp->saslopt;
10617c478bd9Sstevel@tonic-gate 
10627c478bd9Sstevel@tonic-gate 		if (add == TRUE)
10637c478bd9Sstevel@tonic-gate 			ldaprc = __ns_ldap_addAttr("publickey", dn,
10647c478bd9Sstevel@tonic-gate 				(const ns_ldap_attr_t * const *)attrs,
10657c478bd9Sstevel@tonic-gate 				credp, NULL, &errorp);
10667c478bd9Sstevel@tonic-gate 		else
10677c478bd9Sstevel@tonic-gate 			ldaprc = __ns_ldap_repAttr("publickey", dn,
10687c478bd9Sstevel@tonic-gate 				(const ns_ldap_attr_t * const *)attrs,
10697c478bd9Sstevel@tonic-gate 				credp, NULL, &errorp);
10707c478bd9Sstevel@tonic-gate 		if (ldaprc == NS_LDAP_SUCCESS) {
10717c478bd9Sstevel@tonic-gate 			/* clean up ns_cred_t structure in memory */
10727c478bd9Sstevel@tonic-gate 			if (credp != NULL)
10737c478bd9Sstevel@tonic-gate 				(void) __ns_ldap_freeCred(&credp);
10747c478bd9Sstevel@tonic-gate 			return;
10757c478bd9Sstevel@tonic-gate 		}
10767c478bd9Sstevel@tonic-gate 
10777c478bd9Sstevel@tonic-gate 		/* XXX add checking for cases of authentication errors */
10787c478bd9Sstevel@tonic-gate 		if ((ldaprc == NS_LDAP_INTERNAL) &&
10797c478bd9Sstevel@tonic-gate 			((errorp->status == LDAP_INAPPROPRIATE_AUTH) ||
10807c478bd9Sstevel@tonic-gate 			(errorp->status == LDAP_INVALID_CREDENTIALS))) {
10817c478bd9Sstevel@tonic-gate 			fprintf(stderr, "LDAP authentication failed.\n");
10827c478bd9Sstevel@tonic-gate 			goto out;
10837c478bd9Sstevel@tonic-gate 		}
10847c478bd9Sstevel@tonic-gate 	}
10857c478bd9Sstevel@tonic-gate 	if (authstried == 0)
10867c478bd9Sstevel@tonic-gate 		fprintf(stderr, "No legal authentication method configured.\n");
10877c478bd9Sstevel@tonic-gate 
10887c478bd9Sstevel@tonic-gate out:
10897c478bd9Sstevel@tonic-gate 	/* clean up ns_cred_t structure in memory */
10907c478bd9Sstevel@tonic-gate 	if (credp != NULL) {
10917c478bd9Sstevel@tonic-gate 		(void) __ns_ldap_freeCred(&credp);
10927c478bd9Sstevel@tonic-gate 	}
10937c478bd9Sstevel@tonic-gate 
10947c478bd9Sstevel@tonic-gate 	if (errorp) {
10957c478bd9Sstevel@tonic-gate 		__ns_ldap_err2str(errorp->status, &msg);
10967c478bd9Sstevel@tonic-gate 		fprintf(stderr, "LDAP error: %s.\n", msg);
10977c478bd9Sstevel@tonic-gate 	}
10987c478bd9Sstevel@tonic-gate 	fprintf(stderr, "%s: key-pair(s) unchanged.\n", program_name);
10997c478bd9Sstevel@tonic-gate 	exit(1);
11007c478bd9Sstevel@tonic-gate }
11017c478bd9Sstevel@tonic-gate 
11027c478bd9Sstevel@tonic-gate 
11037c478bd9Sstevel@tonic-gate /*
11047c478bd9Sstevel@tonic-gate  * Update LDAP nisplublickey entry with new key information via SLDAP.
11057c478bd9Sstevel@tonic-gate  * Free and clean up memory that stores credential data soon after
11067c478bd9Sstevel@tonic-gate  * they are not used or an error comes up.
11077c478bd9Sstevel@tonic-gate  */
11087c478bd9Sstevel@tonic-gate int
11097c478bd9Sstevel@tonic-gate ldap_update(char *mechname,
11107c478bd9Sstevel@tonic-gate 	    char *netname,
11117c478bd9Sstevel@tonic-gate 	    char *public,
11127c478bd9Sstevel@tonic-gate 	    char *crypt,
11137c478bd9Sstevel@tonic-gate 	    char *passwd)
11147c478bd9Sstevel@tonic-gate {
11157c478bd9Sstevel@tonic-gate 	char		*netnamecpy;
11167c478bd9Sstevel@tonic-gate 	char		*id;
11177c478bd9Sstevel@tonic-gate 	char		*domain;
11187c478bd9Sstevel@tonic-gate 	char		*dn;
11197c478bd9Sstevel@tonic-gate 	char		*db;
11207c478bd9Sstevel@tonic-gate 	char		*filter;
11217c478bd9Sstevel@tonic-gate 	ns_ldap_error_t	*errorp;
11227c478bd9Sstevel@tonic-gate 	char		*pkeyatval, *ckeyatval;
11237c478bd9Sstevel@tonic-gate 	ns_ldap_result_t	*res;
11247c478bd9Sstevel@tonic-gate 	ns_ldap_attr_t	*pattrs, *cattrs;
11257c478bd9Sstevel@tonic-gate 	int		update4host = FALSE;
11267c478bd9Sstevel@tonic-gate 	char		*bindDN = NULL;
11277c478bd9Sstevel@tonic-gate 	char		*bindPasswd = NULL;
11287c478bd9Sstevel@tonic-gate 	int		status;
11297c478bd9Sstevel@tonic-gate 
11307c478bd9Sstevel@tonic-gate 	/* Generate DN */
11317c478bd9Sstevel@tonic-gate 	if ((netnamecpy = strdup(netname)) == NULL)
11327c478bd9Sstevel@tonic-gate 		return (0);
11337c478bd9Sstevel@tonic-gate 	if (((id = strchr(netnamecpy, '.')) == NULL) ||
11347c478bd9Sstevel@tonic-gate 	    ((domain = strchr(netnamecpy, '@')) == NULL))
11357c478bd9Sstevel@tonic-gate 		return (0);
11367c478bd9Sstevel@tonic-gate 	else {
11377c478bd9Sstevel@tonic-gate 		*domain++ = '\0';
11387c478bd9Sstevel@tonic-gate 		*id++ = '\0';
11397c478bd9Sstevel@tonic-gate 
11407c478bd9Sstevel@tonic-gate 		id = strdup(id);
11417c478bd9Sstevel@tonic-gate 		if (id == NULL) {
11427c478bd9Sstevel@tonic-gate 			free(netnamecpy);
11437c478bd9Sstevel@tonic-gate 			fprintf(stderr, "LDAP memory error (id)\n");
11447c478bd9Sstevel@tonic-gate 			return (0);
11457c478bd9Sstevel@tonic-gate 		}
11467c478bd9Sstevel@tonic-gate 		domain = strdup(domain);
11477c478bd9Sstevel@tonic-gate 		if (domain == NULL) {
11487c478bd9Sstevel@tonic-gate 			free(netnamecpy);
11497c478bd9Sstevel@tonic-gate 			free(id);
11507c478bd9Sstevel@tonic-gate 			fprintf(stderr, "LDAP memory error (domain)\n");
11517c478bd9Sstevel@tonic-gate 			return (0);
11527c478bd9Sstevel@tonic-gate 		}
11537c478bd9Sstevel@tonic-gate 		free(netnamecpy);
11547c478bd9Sstevel@tonic-gate 	}
11557c478bd9Sstevel@tonic-gate 
11567c478bd9Sstevel@tonic-gate 	if (isdigit(*id)) {
11577c478bd9Sstevel@tonic-gate 		/* We be user. */
11587c478bd9Sstevel@tonic-gate 		__ns_ldap_uid2dn(id, &dn, NULL, &errorp);
11597c478bd9Sstevel@tonic-gate 		if (dn == NULL) {
11607c478bd9Sstevel@tonic-gate 			fprintf(stderr, "Could not obtain LDAP dn\n");
11617c478bd9Sstevel@tonic-gate 			fprintf(stderr, "%s: key-pair(s) unchanged.\n",
11627c478bd9Sstevel@tonic-gate 				program_name);
11637c478bd9Sstevel@tonic-gate 			exit(1);
11647c478bd9Sstevel@tonic-gate 		}
11657c478bd9Sstevel@tonic-gate 		db = "passwd";
11667c478bd9Sstevel@tonic-gate 		filter = (char *)malloc(strlen(id) + 13);
11677c478bd9Sstevel@tonic-gate 		if (filter)
11687c478bd9Sstevel@tonic-gate 			sprintf(filter, "(uidnumber=%s)", id);
11697c478bd9Sstevel@tonic-gate 		else {
11707c478bd9Sstevel@tonic-gate 			fprintf(stderr, "Can not allocate filter buffer.\n");
11717c478bd9Sstevel@tonic-gate 			fprintf(stderr, "%s: key-pair(s) unchanged.\n",
11727c478bd9Sstevel@tonic-gate 				program_name);
11737c478bd9Sstevel@tonic-gate 			exit(1);
11747c478bd9Sstevel@tonic-gate 		}
11757c478bd9Sstevel@tonic-gate 	} else {
11767c478bd9Sstevel@tonic-gate 		/* We be host. */
11777c478bd9Sstevel@tonic-gate 		update4host = TRUE;
11787c478bd9Sstevel@tonic-gate 
11797c478bd9Sstevel@tonic-gate 		__ns_ldap_host2dn(id, NULL, &dn, NULL, &errorp);
11807c478bd9Sstevel@tonic-gate 		if (dn == NULL) {
11817c478bd9Sstevel@tonic-gate 			fprintf(stderr, "Could not obtain LDAP dn\n");
11827c478bd9Sstevel@tonic-gate 			fprintf(stderr, "%s: key-pair(s) unchanged.\n",
11837c478bd9Sstevel@tonic-gate 				program_name);
11847c478bd9Sstevel@tonic-gate 			exit(1);
11857c478bd9Sstevel@tonic-gate 		}
11867c478bd9Sstevel@tonic-gate 
11877c478bd9Sstevel@tonic-gate 		db = "hosts";
11887c478bd9Sstevel@tonic-gate 		filter = (char *)malloc(strlen(id) + 6);
11897c478bd9Sstevel@tonic-gate 		if (filter)
11907c478bd9Sstevel@tonic-gate 			sprintf(filter, "(cn=%s)", id);
11917c478bd9Sstevel@tonic-gate 		else {
11927c478bd9Sstevel@tonic-gate 			fprintf(stderr, "Can not allocate filter buffer.\n");
11937c478bd9Sstevel@tonic-gate 			fprintf(stderr, "%s: key-pair(s) unchanged.\n",
11947c478bd9Sstevel@tonic-gate 				program_name);
11957c478bd9Sstevel@tonic-gate 			exit(1);
11967c478bd9Sstevel@tonic-gate 		}
11977c478bd9Sstevel@tonic-gate 
11987c478bd9Sstevel@tonic-gate 		/* Prompt for ldap bind DN for entry udpates */
11997c478bd9Sstevel@tonic-gate 		status = get_ldap_bindDN(&bindDN);
12007c478bd9Sstevel@tonic-gate 
12017c478bd9Sstevel@tonic-gate 		if (status != PROMPTGET_SUCCESS) {
12027c478bd9Sstevel@tonic-gate 			FREE_CREDINFO(bindDN);
12037c478bd9Sstevel@tonic-gate 			fprintf(stderr,
12047c478bd9Sstevel@tonic-gate 				"Failed to get a valid LDAP bind DN.\n"
12057c478bd9Sstevel@tonic-gate 				"%s: key-pair(s) unchanged.\n",
12067c478bd9Sstevel@tonic-gate 				program_name);
12077c478bd9Sstevel@tonic-gate 			exit(1);
12087c478bd9Sstevel@tonic-gate 		}
12097c478bd9Sstevel@tonic-gate 
12107c478bd9Sstevel@tonic-gate 		/* Prompt for ldap bind password */
12117c478bd9Sstevel@tonic-gate 		status = get_ldap_bindPassword(&bindPasswd);
12127c478bd9Sstevel@tonic-gate 
12137c478bd9Sstevel@tonic-gate 		if (status != PROMPTGET_SUCCESS) {
12147c478bd9Sstevel@tonic-gate 			FREE_CREDINFO(bindPasswd);
12157c478bd9Sstevel@tonic-gate 			FREE_CREDINFO(bindDN);
12167c478bd9Sstevel@tonic-gate 
12177c478bd9Sstevel@tonic-gate 			fprintf(stderr,
12187c478bd9Sstevel@tonic-gate 				"Failed to get a valid LDAP bind password."
12197c478bd9Sstevel@tonic-gate 				"\n%s: key-pair(s) unchanged.\n",
12207c478bd9Sstevel@tonic-gate 				program_name);
12217c478bd9Sstevel@tonic-gate 			exit(1);
12227c478bd9Sstevel@tonic-gate 		}
12237c478bd9Sstevel@tonic-gate 	}
12247c478bd9Sstevel@tonic-gate 
12257c478bd9Sstevel@tonic-gate 	/* Construct attribute values */
12267c478bd9Sstevel@tonic-gate 	pkeyatval = (char *)malloc(strlen(mechname) + strlen(public) + 3);
12277c478bd9Sstevel@tonic-gate 	if (pkeyatval == NULL) {
12287c478bd9Sstevel@tonic-gate 		FREE_CREDINFO(bindPasswd);
12297c478bd9Sstevel@tonic-gate 		FREE_CREDINFO(bindDN);
12307c478bd9Sstevel@tonic-gate 		fprintf(stderr, "LDAP memory error (pkeyatval)\n");
12317c478bd9Sstevel@tonic-gate 		fprintf(stderr, "%s: key-pair(s) unchanged.\n", program_name);
12327c478bd9Sstevel@tonic-gate 		exit(1);
12337c478bd9Sstevel@tonic-gate 	}
12347c478bd9Sstevel@tonic-gate 	sprintf(pkeyatval, "{%s}%s", mechname, public);
12357c478bd9Sstevel@tonic-gate 	ckeyatval = (char *)malloc(strlen(mechname) + strlen(crypt) + 3);
12367c478bd9Sstevel@tonic-gate 	if (ckeyatval == NULL) {
12377c478bd9Sstevel@tonic-gate 		FREE_CREDINFO(pkeyatval);
12387c478bd9Sstevel@tonic-gate 		FREE_CREDINFO(bindPasswd);
12397c478bd9Sstevel@tonic-gate 		FREE_CREDINFO(bindDN);
12407c478bd9Sstevel@tonic-gate 		fprintf(stderr, "LDAP memory error (pkeyatval)\n");
12417c478bd9Sstevel@tonic-gate 		fprintf(stderr, "%s: key-pair(s) unchanged.\n", program_name);
12427c478bd9Sstevel@tonic-gate 		exit(1);
12437c478bd9Sstevel@tonic-gate 	}
12447c478bd9Sstevel@tonic-gate 	sprintf(ckeyatval, "{%s}%s", mechname, crypt);
12457c478bd9Sstevel@tonic-gate 
12467c478bd9Sstevel@tonic-gate 	/* Does entry exist? */
12477c478bd9Sstevel@tonic-gate 	if ((__ns_ldap_list(db, filter, NULL, (const char **)attrFilter,
12487c478bd9Sstevel@tonic-gate 			    NULL, 0, &res, &errorp,
12497c478bd9Sstevel@tonic-gate 			    NULL, NULL) == NS_LDAP_SUCCESS) && res == NULL) {
12507c478bd9Sstevel@tonic-gate 		FREE_CREDINFO(ckeyatval);
12517c478bd9Sstevel@tonic-gate 		FREE_CREDINFO(pkeyatval);
12527c478bd9Sstevel@tonic-gate 		FREE_CREDINFO(bindPasswd);
12537c478bd9Sstevel@tonic-gate 		FREE_CREDINFO(bindDN);
12547c478bd9Sstevel@tonic-gate 		fprintf(stderr, "LDAP entry does not exist.\n");
12557c478bd9Sstevel@tonic-gate 		fprintf(stderr, "%s: key-pair(s) unchanged.\n", program_name);
12567c478bd9Sstevel@tonic-gate 		exit(1);
12577c478bd9Sstevel@tonic-gate 	}
12587c478bd9Sstevel@tonic-gate 
12597c478bd9Sstevel@tonic-gate 	/* Entry exists, modify attributes for public and secret keys */
12607c478bd9Sstevel@tonic-gate 
12617c478bd9Sstevel@tonic-gate 	/* Is there a NisKeyObject in entry? */
12627c478bd9Sstevel@tonic-gate 	if (!ldap_keyobj_exist(&res->entry[0])) {
12637c478bd9Sstevel@tonic-gate 		/* Add NisKeyObject objectclass and the keys */
12647c478bd9Sstevel@tonic-gate 		char	**newattr;
12657c478bd9Sstevel@tonic-gate 		ns_ldap_attr_t	*attrs[4]; /* objectclass, pk, sk, NULL */
12667c478bd9Sstevel@tonic-gate 
12677c478bd9Sstevel@tonic-gate 		/* set objectclass */
12687c478bd9Sstevel@tonic-gate 		newattr = (char **)calloc(2, sizeof (char *));
12697c478bd9Sstevel@tonic-gate 		newattr[0] = "NisKeyObject";
12707c478bd9Sstevel@tonic-gate 		newattr[1] = NULL;
12717c478bd9Sstevel@tonic-gate 		if ((attrs[0] = (ns_ldap_attr_t *)calloc(1,
12727c478bd9Sstevel@tonic-gate 				sizeof (ns_ldap_attr_t))) == NULL) {
12737c478bd9Sstevel@tonic-gate 			FREE_CREDINFO(ckeyatval);
12747c478bd9Sstevel@tonic-gate 			FREE_CREDINFO(pkeyatval);
12757c478bd9Sstevel@tonic-gate 			FREE_CREDINFO(bindPasswd);
12767c478bd9Sstevel@tonic-gate 			FREE_CREDINFO(bindDN);
12777c478bd9Sstevel@tonic-gate 			fprintf(stderr, "Memory allocation failed\n");
12787c478bd9Sstevel@tonic-gate 			fprintf(stderr, "%s: key-pair(s) unchanged.\n",
12797c478bd9Sstevel@tonic-gate 				program_name);
12807c478bd9Sstevel@tonic-gate 			exit(1);
12817c478bd9Sstevel@tonic-gate 		}
12827c478bd9Sstevel@tonic-gate 		attrs[0]->attrname = "objectClass";
12837c478bd9Sstevel@tonic-gate 		attrs[0]->attrvalue = newattr;
12847c478bd9Sstevel@tonic-gate 		attrs[0]->value_count = 1;
12857c478bd9Sstevel@tonic-gate 
12867c478bd9Sstevel@tonic-gate 		/* set publickey */
12877c478bd9Sstevel@tonic-gate 		newattr = (char **)calloc(2, sizeof (char *));
12887c478bd9Sstevel@tonic-gate 		newattr[0] = pkeyatval;
12897c478bd9Sstevel@tonic-gate 		newattr[1] = NULL;
12907c478bd9Sstevel@tonic-gate 		if ((attrs[1] = (ns_ldap_attr_t *)calloc(1,
12917c478bd9Sstevel@tonic-gate 				sizeof (ns_ldap_attr_t))) == NULL) {
12927c478bd9Sstevel@tonic-gate 			FREE_CREDINFO(ckeyatval);
12937c478bd9Sstevel@tonic-gate 			FREE_CREDINFO(pkeyatval);
12947c478bd9Sstevel@tonic-gate 			FREE_CREDINFO(bindPasswd);
12957c478bd9Sstevel@tonic-gate 			FREE_CREDINFO(bindDN);
12967c478bd9Sstevel@tonic-gate 			fprintf(stderr, "Memory allocation failed\n");
12977c478bd9Sstevel@tonic-gate 			fprintf(stderr, "%s: key-pair(s) unchanged.\n",
12987c478bd9Sstevel@tonic-gate 				program_name);
12997c478bd9Sstevel@tonic-gate 			exit(1);
13007c478bd9Sstevel@tonic-gate 		}
13017c478bd9Sstevel@tonic-gate 		attrs[1]->attrname = "nisPublicKey";
13027c478bd9Sstevel@tonic-gate 		attrs[1]->attrvalue = newattr;
13037c478bd9Sstevel@tonic-gate 		attrs[1]->value_count = 1;
13047c478bd9Sstevel@tonic-gate 
13057c478bd9Sstevel@tonic-gate 		/* set privatekey */
13067c478bd9Sstevel@tonic-gate 		newattr = (char **)calloc(2, sizeof (char *));
13077c478bd9Sstevel@tonic-gate 		newattr[0] = ckeyatval;
13087c478bd9Sstevel@tonic-gate 		newattr[1] = NULL;
13097c478bd9Sstevel@tonic-gate 		if ((attrs[2] = (ns_ldap_attr_t *)calloc(1,
13107c478bd9Sstevel@tonic-gate 				sizeof (ns_ldap_attr_t))) == NULL) {
13117c478bd9Sstevel@tonic-gate 			FREE_CREDINFO(ckeyatval);
13127c478bd9Sstevel@tonic-gate 			FREE_CREDINFO(pkeyatval);
13137c478bd9Sstevel@tonic-gate 			FREE_CREDINFO(bindPasswd);
13147c478bd9Sstevel@tonic-gate 			FREE_CREDINFO(bindDN);
13157c478bd9Sstevel@tonic-gate 			fprintf(stderr, "Memory allocation failed\n");
13167c478bd9Sstevel@tonic-gate 			fprintf(stderr, "%s: key-pair(s) unchanged.\n",
13177c478bd9Sstevel@tonic-gate 				program_name);
13187c478bd9Sstevel@tonic-gate 			exit(1);
13197c478bd9Sstevel@tonic-gate 		}
13207c478bd9Sstevel@tonic-gate 		attrs[2]->attrname = "nisSecretKey";
13217c478bd9Sstevel@tonic-gate 		attrs[2]->attrvalue = newattr;
13227c478bd9Sstevel@tonic-gate 		attrs[2]->value_count = 1;
13237c478bd9Sstevel@tonic-gate 
13247c478bd9Sstevel@tonic-gate 		/* terminator */
13257c478bd9Sstevel@tonic-gate 		attrs[3] = NULL;
13267c478bd9Sstevel@tonic-gate 
13277c478bd9Sstevel@tonic-gate 		update_ldap_attr(dn, attrs, passwd, TRUE, update4host,
13287c478bd9Sstevel@tonic-gate 				bindDN, bindPasswd);
13297c478bd9Sstevel@tonic-gate 	} else {
13307c478bd9Sstevel@tonic-gate 		/* object class already exists, replace keys */
13317c478bd9Sstevel@tonic-gate 		ns_ldap_attr_t	*attrs[4]; /* objectclass, pk, sk, NULL */
13327c478bd9Sstevel@tonic-gate 
13337c478bd9Sstevel@tonic-gate 		if (!ldap_attr_mod(&res->entry[0], mechname,
13347c478bd9Sstevel@tonic-gate 				pkeyatval, &pattrs,
13357c478bd9Sstevel@tonic-gate 				ckeyatval, &cattrs)) {
13367c478bd9Sstevel@tonic-gate 			FREE_CREDINFO(ckeyatval);
13377c478bd9Sstevel@tonic-gate 			FREE_CREDINFO(pkeyatval);
13387c478bd9Sstevel@tonic-gate 			FREE_CREDINFO(bindPasswd);
13397c478bd9Sstevel@tonic-gate 			FREE_CREDINFO(bindDN);
13407c478bd9Sstevel@tonic-gate 			fprintf(stderr,
13417c478bd9Sstevel@tonic-gate 				"Could not generate LDAP attribute list.\n");
13427c478bd9Sstevel@tonic-gate 			fprintf(stderr,
13437c478bd9Sstevel@tonic-gate 				"%s: key-pair(s) unchanged.\n", program_name);
13447c478bd9Sstevel@tonic-gate 			exit(1);
13457c478bd9Sstevel@tonic-gate 		}
13467c478bd9Sstevel@tonic-gate 
13477c478bd9Sstevel@tonic-gate 		attrs[0] = pattrs;
13487c478bd9Sstevel@tonic-gate 		attrs[1] = cattrs;
13497c478bd9Sstevel@tonic-gate 		attrs[2] = NULL;
13507c478bd9Sstevel@tonic-gate 
13517c478bd9Sstevel@tonic-gate 		update_ldap_attr(dn, attrs, passwd, FALSE, update4host,
13527c478bd9Sstevel@tonic-gate 				bindDN, bindPasswd);
13537c478bd9Sstevel@tonic-gate 	}
13547c478bd9Sstevel@tonic-gate 
13557c478bd9Sstevel@tonic-gate 	FREE_CREDINFO(ckeyatval);
13567c478bd9Sstevel@tonic-gate 	FREE_CREDINFO(pkeyatval);
13577c478bd9Sstevel@tonic-gate 	FREE_CREDINFO(bindPasswd);
13587c478bd9Sstevel@tonic-gate 	FREE_CREDINFO(bindDN);
13597c478bd9Sstevel@tonic-gate 
13607c478bd9Sstevel@tonic-gate 	return (0);
13617c478bd9Sstevel@tonic-gate }
13627c478bd9Sstevel@tonic-gate 
13637c478bd9Sstevel@tonic-gate 
13647c478bd9Sstevel@tonic-gate /* Returns 0 if successful; -1 if failure. (expected by setpublicmap). */
13657c478bd9Sstevel@tonic-gate 
13667c478bd9Sstevel@tonic-gate int
13677c478bd9Sstevel@tonic-gate nisplus_update(char *netname, char *public, char *secret, nis_name nis_princ)
13687c478bd9Sstevel@tonic-gate {
13697c478bd9Sstevel@tonic-gate 	nis_object	*obj = init_entry();
13707c478bd9Sstevel@tonic-gate 	char	*domain, *netdomain;
13717c478bd9Sstevel@tonic-gate 	char	netdomainaux[MAXHOSTNAMELEN + 1];
13727c478bd9Sstevel@tonic-gate 	int status, addition;
13737c478bd9Sstevel@tonic-gate 
13747c478bd9Sstevel@tonic-gate 	/*
13757c478bd9Sstevel@tonic-gate 	 * we take the domain given in the netname & the principal
13767c478bd9Sstevel@tonic-gate 	 * name if they match otherwise the local domain.
13777c478bd9Sstevel@tonic-gate 	 */
13787c478bd9Sstevel@tonic-gate 
13797c478bd9Sstevel@tonic-gate 	netdomain = (char *)strchr(netname, '@');
13807c478bd9Sstevel@tonic-gate 	if (! netdomain) {
13817c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: invalid netname: '%s'. \n",
13827c478bd9Sstevel@tonic-gate 			program_name, netname);
13837c478bd9Sstevel@tonic-gate 		return (0);
13847c478bd9Sstevel@tonic-gate 	}
13857c478bd9Sstevel@tonic-gate 	netdomain++; /* skip '@' */
13867c478bd9Sstevel@tonic-gate 	/* make sure we don't run into buffer overflow */
13877c478bd9Sstevel@tonic-gate 	if (strlen(netdomain) > sizeof (netdomainaux))
13887c478bd9Sstevel@tonic-gate 		return (0);
13897c478bd9Sstevel@tonic-gate 	strcpy(netdomainaux, netdomain);
13907c478bd9Sstevel@tonic-gate 	if (netdomainaux[strlen(netdomainaux) - 1] != '.')
13917c478bd9Sstevel@tonic-gate 		strcat(netdomainaux, ".");
13927c478bd9Sstevel@tonic-gate 
13937c478bd9Sstevel@tonic-gate 	domain = nis_domain_of(nis_princ);
13947c478bd9Sstevel@tonic-gate 	if (strcasecmp(domain, netdomainaux) != 0)
13957c478bd9Sstevel@tonic-gate 		domain = nis_local_directory();
13967c478bd9Sstevel@tonic-gate 
13977c478bd9Sstevel@tonic-gate 	if (sanity_checks(nis_princ, netname, domain) == 0)
13987c478bd9Sstevel@tonic-gate 		return (-1);
13997c478bd9Sstevel@tonic-gate 
14007c478bd9Sstevel@tonic-gate 	addition = (cred_exists(nis_princ, "DES", domain) == NIS_NOTFOUND);
14017c478bd9Sstevel@tonic-gate 
14027c478bd9Sstevel@tonic-gate 	/* Now we have a key pair, build up the cred entry */
14037c478bd9Sstevel@tonic-gate 	ENTRY_VAL(obj, 0) = nis_princ;
14047c478bd9Sstevel@tonic-gate 	ENTRY_LEN(obj, 0) = strlen(nis_princ) + 1;
14057c478bd9Sstevel@tonic-gate 
14067c478bd9Sstevel@tonic-gate 	ENTRY_VAL(obj, 1) = "DES";
14077c478bd9Sstevel@tonic-gate 	ENTRY_LEN(obj, 1) = 4;
14087c478bd9Sstevel@tonic-gate 
14097c478bd9Sstevel@tonic-gate 	ENTRY_VAL(obj, 2) = netname;
14107c478bd9Sstevel@tonic-gate 	ENTRY_LEN(obj, 2) = strlen(netname) + 1;
14117c478bd9Sstevel@tonic-gate 
14127c478bd9Sstevel@tonic-gate 	ENTRY_VAL(obj, 3) = public;
14137c478bd9Sstevel@tonic-gate 	ENTRY_LEN(obj, 3) = strlen(public) + 1;
14147c478bd9Sstevel@tonic-gate 
14157c478bd9Sstevel@tonic-gate 	ENTRY_VAL(obj, 4) = secret;
14167c478bd9Sstevel@tonic-gate 	ENTRY_LEN(obj, 4) = strlen(secret) + 1;
14177c478bd9Sstevel@tonic-gate 
14187c478bd9Sstevel@tonic-gate 	if (addition) {
14197c478bd9Sstevel@tonic-gate 		obj->zo_owner = nis_princ;
14207c478bd9Sstevel@tonic-gate 		obj->zo_group = nis_local_group();
14217c478bd9Sstevel@tonic-gate 		obj->zo_domain = domain;
14227c478bd9Sstevel@tonic-gate 		/* owner: r, group: rmcd */
14237c478bd9Sstevel@tonic-gate 		obj->zo_access = ((NIS_READ_ACC<<16)|
14247c478bd9Sstevel@tonic-gate 				(NIS_READ_ACC|NIS_MODIFY_ACC|NIS_CREATE_ACC|
14257c478bd9Sstevel@tonic-gate 					NIS_DESTROY_ACC)<<8);
14267c478bd9Sstevel@tonic-gate 		status = add_cred_obj(obj, domain);
14277c478bd9Sstevel@tonic-gate 	} else {
14287c478bd9Sstevel@tonic-gate 		obj->EN_data.en_cols.en_cols_val[3].ec_flags |= EN_MODIFIED;
14297c478bd9Sstevel@tonic-gate 		obj->EN_data.en_cols.en_cols_val[4].ec_flags |= EN_MODIFIED;
14307c478bd9Sstevel@tonic-gate 		status = modify_cred_obj(obj, domain);
14317c478bd9Sstevel@tonic-gate 	}
14327c478bd9Sstevel@tonic-gate 
14337c478bd9Sstevel@tonic-gate 	return (status == 1 ? 0 : -1);
14347c478bd9Sstevel@tonic-gate }
1435