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
536e852a1SRaja Andra  * Common Development and Distribution License (the "License").
636e852a1SRaja Andra  * 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 /*
2236e852a1SRaja Andra  * 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 #include <stdio.h>
277c478bd9Sstevel@tonic-gate #include <stdlib.h>
287c478bd9Sstevel@tonic-gate #include <ctype.h>
297c478bd9Sstevel@tonic-gate #include <strings.h>
307c478bd9Sstevel@tonic-gate #include <pwd.h>
317c478bd9Sstevel@tonic-gate #include <shadow.h>
327c478bd9Sstevel@tonic-gate #include <netdb.h>
337c478bd9Sstevel@tonic-gate #include <mp.h>
347c478bd9Sstevel@tonic-gate #include <rpcsvc/nis.h>
357c478bd9Sstevel@tonic-gate #include <rpc/key_prot.h>
367c478bd9Sstevel@tonic-gate #include <nsswitch.h>
377c478bd9Sstevel@tonic-gate #include <ns_sldap.h>
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate extern char *crypt();
407c478bd9Sstevel@tonic-gate extern long random();
417c478bd9Sstevel@tonic-gate extern char *getpassphrase();
427c478bd9Sstevel@tonic-gate extern char *program_name;
437c478bd9Sstevel@tonic-gate static const char *CRED_TABLE = "cred.org_dir";
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate #define	ROOTKEY_FILE	"/etc/.rootkey"
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate #ifndef MAXHOSTNAMELEN
487c478bd9Sstevel@tonic-gate #define	MAXHOSTNAMELEN 256
497c478bd9Sstevel@tonic-gate #endif
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate #define	PK_FILES	1
527c478bd9Sstevel@tonic-gate #define	PK_YP		2
537c478bd9Sstevel@tonic-gate #define	PK_LDAP		4
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate #define	LDAP_BINDDN_DEFAULT	"cn=Directory Manager"
567c478bd9Sstevel@tonic-gate #define	PROMPTGET_SUCCESS	1
577c478bd9Sstevel@tonic-gate #define	PROMPTGET_FAIL		-1
587c478bd9Sstevel@tonic-gate #define	PROMPTGET_MEMORY_FAIL	-2
597c478bd9Sstevel@tonic-gate #define	PASSWD_UNMATCHED	-3
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate #define	FREE_CREDINFO(s) \
627c478bd9Sstevel@tonic-gate 	if ((s)) { (void) memset((s), 0, strlen((s))); }
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate /* ************************ switch functions *************************** */
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate /*	NSW_NOTSUCCESS  NSW_NOTFOUND   NSW_UNAVAIL    NSW_TRYAGAIN */
687c478bd9Sstevel@tonic-gate #define	DEF_ACTION {__NSW_RETURN, __NSW_RETURN, __NSW_CONTINUE, __NSW_CONTINUE}
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate static struct __nsw_lookup lookup_files = {"files", DEF_ACTION, NULL, NULL},
717c478bd9Sstevel@tonic-gate 		lookup_nis = {"nis", DEF_ACTION, NULL, &lookup_files};
727c478bd9Sstevel@tonic-gate static struct __nsw_switchconfig publickey_default =
737c478bd9Sstevel@tonic-gate 			{0, "publickey", 2, &lookup_nis};
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate static int get_ldap_bindDN(char **);
767c478bd9Sstevel@tonic-gate static int get_ldap_bindPassword(char **);
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate /*
797c478bd9Sstevel@tonic-gate  * Prompt the users for a ldap bind DN. If users do not enter a value but just
807c478bd9Sstevel@tonic-gate  * simply hit the return key, the default bindDN "cn=Directory Manager"
817c478bd9Sstevel@tonic-gate  * will be used.
827c478bd9Sstevel@tonic-gate  */
837c478bd9Sstevel@tonic-gate static int
get_ldap_bindDN(char ** ret_bindDN)84*c77bf777SToomas Soome get_ldap_bindDN(char **ret_bindDN)
85*c77bf777SToomas Soome {
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate 	char	bindDN[BUFSIZ];
887c478bd9Sstevel@tonic-gate 	char	prompt[BUFSIZ];
897c478bd9Sstevel@tonic-gate 	int	blen, pos;
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate 	/* set the initial value for bindDN buffer */
927c478bd9Sstevel@tonic-gate 	(void) memset(bindDN, 0, BUFSIZ);
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate 	(void) snprintf(prompt, BUFSIZ,
957c478bd9Sstevel@tonic-gate 	"\nThe LDAP bind DN and password are required for this update.\n"
967c478bd9Sstevel@tonic-gate 	"If you are not sure what values to enter, please contact your\n"
977c478bd9Sstevel@tonic-gate 	"LDAP administrator.\n\nPlease enter LDAP bind DN [%s]: ",
98*c77bf777SToomas Soome 	    LDAP_BINDDN_DEFAULT);
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate 	printf(prompt);
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate 	if (fgets(bindDN, sizeof (bindDN), stdin) == NULL) {
1037c478bd9Sstevel@tonic-gate 		(void) strlcpy(bindDN, LDAP_BINDDN_DEFAULT, BUFSIZ);
1047c478bd9Sstevel@tonic-gate 	}
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate 	blen = strlen(bindDN);
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate 	/* Check if the buffer ends with a newline */
1097c478bd9Sstevel@tonic-gate 	if ((blen > 0) && (bindDN[blen - 1] == '\n')) {
1107c478bd9Sstevel@tonic-gate 		bindDN[blen - 1] = '\0';
1117c478bd9Sstevel@tonic-gate 		blen -= 1;
1127c478bd9Sstevel@tonic-gate 	}
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate 	/* Remove the white spaces */
1157c478bd9Sstevel@tonic-gate 	if (blen > 0) {
1167c478bd9Sstevel@tonic-gate 		for (pos = blen - 1; pos >= 0; pos--) {
1177c478bd9Sstevel@tonic-gate 			if (isspace(bindDN[pos]))
1187c478bd9Sstevel@tonic-gate 				bindDN[pos] = '\0';
1197c478bd9Sstevel@tonic-gate 			else
1207c478bd9Sstevel@tonic-gate 				break;
1217c478bd9Sstevel@tonic-gate 		}
1227c478bd9Sstevel@tonic-gate 	}
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate 	/* Use the default bindDN, if the buffer contains no characters */
1257c478bd9Sstevel@tonic-gate 	if (strlen(bindDN) == 0)
1267c478bd9Sstevel@tonic-gate 		(void) strlcpy(bindDN, LDAP_BINDDN_DEFAULT, BUFSIZ);
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate 	if ((*ret_bindDN = (char *)malloc(strlen(bindDN)+1)) == NULL) {
1297c478bd9Sstevel@tonic-gate 		(void) memset(bindDN, 0, BUFSIZ);
1307c478bd9Sstevel@tonic-gate 		return (PROMPTGET_MEMORY_FAIL);
1317c478bd9Sstevel@tonic-gate 	}
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate 	(void) strlcpy(*ret_bindDN, bindDN, strlen(bindDN)+1);
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate 	/* Clean up and erase the credential info */
1367c478bd9Sstevel@tonic-gate 	(void) memset(bindDN, 0, BUFSIZ);
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate 	return (PROMPTGET_SUCCESS);
1397c478bd9Sstevel@tonic-gate }
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate /*
1437c478bd9Sstevel@tonic-gate  * Prompt the user for a ldap bind password.
1447c478bd9Sstevel@tonic-gate  */
1457c478bd9Sstevel@tonic-gate static int
get_ldap_bindPassword(char ** ret_bindPass)146*c77bf777SToomas Soome get_ldap_bindPassword(char **ret_bindPass)
147*c77bf777SToomas Soome {
1487c478bd9Sstevel@tonic-gate 
149*c77bf777SToomas Soome 	char	bindPassword[BUFSIZ];
1507c478bd9Sstevel@tonic-gate 	char	prompt[BUFSIZ];
1517c478bd9Sstevel@tonic-gate 	char	*bindPass = NULL;
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate 	/* set the initial value for bindPassword buffer */
1547c478bd9Sstevel@tonic-gate 	(void) memset(bindPassword, 0, BUFSIZ);
1557c478bd9Sstevel@tonic-gate 	*ret_bindPass = NULL;
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate 	(void) snprintf(prompt, BUFSIZ,
158*c77bf777SToomas Soome 	    "Please enter LDAP bind password: ");
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate 	bindPass = getpassphrase(prompt);
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate 	if (bindPass == NULL)
1637c478bd9Sstevel@tonic-gate 		return (PROMPTGET_FAIL);
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate 	(void) strlcpy(bindPassword, bindPass, BUFSIZ);
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate 	/* clean the static buffer returned from getpassphrase call */
1687c478bd9Sstevel@tonic-gate 	(void) memset(bindPass, 0, strlen(bindPass));
1697c478bd9Sstevel@tonic-gate 	bindPass = NULL;
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate 	/*
1727c478bd9Sstevel@tonic-gate 	 * Re-enter the bind passowrd and compare it with the one
1737c478bd9Sstevel@tonic-gate 	 * from previous entered.
1747c478bd9Sstevel@tonic-gate 	 */
1757c478bd9Sstevel@tonic-gate 	(void) snprintf(prompt, BUFSIZ,
176*c77bf777SToomas Soome 	    "Re-enter LDAP bind password to confirm: ");
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate 	bindPass = getpassphrase(prompt);
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate 	if (bindPass == NULL) {
1817c478bd9Sstevel@tonic-gate 		(void) memset(bindPassword, 0, BUFSIZ);
1827c478bd9Sstevel@tonic-gate 		return (PASSWD_UNMATCHED);
1837c478bd9Sstevel@tonic-gate 	}
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate 	if (strcmp(bindPass, bindPassword) != 0) {
1867c478bd9Sstevel@tonic-gate 		(void) memset(bindPassword, 0, BUFSIZ);
1877c478bd9Sstevel@tonic-gate 		(void) memset(bindPass, 0, strlen(bindPass));
1887c478bd9Sstevel@tonic-gate 		return (PASSWD_UNMATCHED);
1897c478bd9Sstevel@tonic-gate 	} else {
1907c478bd9Sstevel@tonic-gate 		(void) memset(bindPass, 0, strlen(bindPass));
1917c478bd9Sstevel@tonic-gate 		if ((*ret_bindPass = (char *)malloc(strlen(bindPassword)+1))
192*c77bf777SToomas Soome 		    == NULL) {
1937c478bd9Sstevel@tonic-gate 			(void) memset(bindPassword, 0, BUFSIZ);
1947c478bd9Sstevel@tonic-gate 			return (PROMPTGET_MEMORY_FAIL);
1957c478bd9Sstevel@tonic-gate 		}
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 		(void) strlcpy(*ret_bindPass, bindPassword,
198*c77bf777SToomas Soome 		    strlen(bindPassword)+1);
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate 		/* Clean up and erase the credential info */
2017c478bd9Sstevel@tonic-gate 		(void) memset(bindPassword, 0, BUFSIZ);
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate 		return (PROMPTGET_SUCCESS);
2047c478bd9Sstevel@tonic-gate 	}
2057c478bd9Sstevel@tonic-gate }
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate char *
switch_policy_str(struct __nsw_switchconfig * conf)2107c478bd9Sstevel@tonic-gate switch_policy_str(struct __nsw_switchconfig *conf)
2117c478bd9Sstevel@tonic-gate {
2127c478bd9Sstevel@tonic-gate 	struct __nsw_lookup *look;
2137c478bd9Sstevel@tonic-gate 	static char policy[256];  /* 256 is enough for (nis, files...etc) */
2147c478bd9Sstevel@tonic-gate 	int previous = 0;
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate 	memset((char *)policy, 0, 256);
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate 	for (look = conf->lookups; look; look = look->next) {
2197c478bd9Sstevel@tonic-gate 		if (previous)
2207c478bd9Sstevel@tonic-gate 			strcat(policy, " ");
2217c478bd9Sstevel@tonic-gate 		strcat(policy, look->service_name);
2227c478bd9Sstevel@tonic-gate 		previous = 1;
2237c478bd9Sstevel@tonic-gate 	}
2247c478bd9Sstevel@tonic-gate 	return (policy);
2257c478bd9Sstevel@tonic-gate }
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate int
no_switch_policy(struct __nsw_switchconfig * conf)2287c478bd9Sstevel@tonic-gate no_switch_policy(struct __nsw_switchconfig *conf)
2297c478bd9Sstevel@tonic-gate {
2307c478bd9Sstevel@tonic-gate 	return (conf == NULL || conf->lookups == NULL);
2317c478bd9Sstevel@tonic-gate }
2327c478bd9Sstevel@tonic-gate 
23349e7ca49Speteh int
is_switch_policy(struct __nsw_switchconfig * conf,char * target)2347c478bd9Sstevel@tonic-gate is_switch_policy(struct __nsw_switchconfig *conf, char *target)
2357c478bd9Sstevel@tonic-gate {
2367c478bd9Sstevel@tonic-gate 	return (conf &&
23736e852a1SRaja Andra 	    conf->lookups &&
23836e852a1SRaja Andra 	    strcmp(conf->lookups->service_name, target) == 0 &&
23936e852a1SRaja Andra 	    conf->lookups->next == NULL);
2407c478bd9Sstevel@tonic-gate }
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate char *
first_and_only_switch_policy(char * policy,struct __nsw_switchconfig * default_conf,char * head_msg)2437c478bd9Sstevel@tonic-gate first_and_only_switch_policy(char *policy,
244*c77bf777SToomas Soome     struct __nsw_switchconfig *default_conf, char *head_msg)
2457c478bd9Sstevel@tonic-gate {
2467c478bd9Sstevel@tonic-gate 	struct __nsw_switchconfig *conf;
2477c478bd9Sstevel@tonic-gate 	enum __nsw_parse_err perr;
2487c478bd9Sstevel@tonic-gate 	int policy_correct = 1;
2497c478bd9Sstevel@tonic-gate 	char *target_service = 0;
2507c478bd9Sstevel@tonic-gate 	int use_default = 0;
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate 	if (default_conf == 0)
2537c478bd9Sstevel@tonic-gate 		default_conf = &publickey_default;
2547c478bd9Sstevel@tonic-gate 
2557c478bd9Sstevel@tonic-gate 	conf = __nsw_getconfig(policy, &perr);
2567c478bd9Sstevel@tonic-gate 	if (no_switch_policy(conf)) {
2577c478bd9Sstevel@tonic-gate 		use_default = 1;
2587c478bd9Sstevel@tonic-gate 		conf = default_conf;
2597c478bd9Sstevel@tonic-gate 	}
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate 	target_service = conf->lookups->service_name;
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate 	if (conf->lookups->next != NULL) {
2647c478bd9Sstevel@tonic-gate 		policy_correct = 0;
2657c478bd9Sstevel@tonic-gate 		if (use_default) {
2667c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
2677c478bd9Sstevel@tonic-gate 			"\n%s\n There is no publickey entry in %s.\n",
26836e852a1SRaja Andra 			    head_msg, __NSW_CONFIG_FILE);
2697c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
2707c478bd9Sstevel@tonic-gate 			"The default publickey policy is \"publickey: %s\".\n",
27136e852a1SRaja Andra 			    switch_policy_str(default_conf));
2727c478bd9Sstevel@tonic-gate 		} else
2737c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
2747c478bd9Sstevel@tonic-gate 		"\n%s\nThe publickey entry in %s is \"publickey: %s\".\n",
27536e852a1SRaja Andra 			    head_msg, __NSW_CONFIG_FILE,
27636e852a1SRaja Andra 			    switch_policy_str(conf));
2777c478bd9Sstevel@tonic-gate 	}
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate 	if (policy_correct == 0)
2807c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
2817c478bd9Sstevel@tonic-gate 	"I cannot figure out which publickey database you want to update.\n");
2827c478bd9Sstevel@tonic-gate 	if (!use_default && conf)
2837c478bd9Sstevel@tonic-gate 		__nsw_freeconfig(conf);
2847c478bd9Sstevel@tonic-gate 
2857c478bd9Sstevel@tonic-gate 	if (policy_correct)
2867c478bd9Sstevel@tonic-gate 		return (target_service);
2877c478bd9Sstevel@tonic-gate 	else
2887c478bd9Sstevel@tonic-gate 		return (0);
2897c478bd9Sstevel@tonic-gate }
2907c478bd9Sstevel@tonic-gate 
2917c478bd9Sstevel@tonic-gate 
2927c478bd9Sstevel@tonic-gate 
2937c478bd9Sstevel@tonic-gate int
check_switch_policy(char * policy,char * target_service,struct __nsw_switchconfig * default_conf,char * head_msg,char * tail_msg)2947c478bd9Sstevel@tonic-gate check_switch_policy(char *policy, char *target_service,
295*c77bf777SToomas Soome     struct __nsw_switchconfig *default_conf, char *head_msg, char *tail_msg)
2967c478bd9Sstevel@tonic-gate {
2977c478bd9Sstevel@tonic-gate 	struct __nsw_switchconfig *conf;
2987c478bd9Sstevel@tonic-gate 	enum __nsw_parse_err perr;
2997c478bd9Sstevel@tonic-gate 	int policy_correct = 1;
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate 	if (default_conf == 0)
3027c478bd9Sstevel@tonic-gate 		default_conf = &publickey_default;
3037c478bd9Sstevel@tonic-gate 
3047c478bd9Sstevel@tonic-gate 	conf = __nsw_getconfig(policy, &perr);
3057c478bd9Sstevel@tonic-gate 	if (no_switch_policy(conf)) {
3067c478bd9Sstevel@tonic-gate 		if (!is_switch_policy(default_conf, target_service)) {
3077c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
30836e852a1SRaja Andra 			    "\n%s\nThere is no publickey entry in %s.\n",
30936e852a1SRaja Andra 			    head_msg, __NSW_CONFIG_FILE);
3107c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
3117c478bd9Sstevel@tonic-gate 			"The default publickey policy is \"publickey: %s\".\n",
31236e852a1SRaja Andra 			    switch_policy_str(default_conf));
3137c478bd9Sstevel@tonic-gate 			policy_correct = 0;
3147c478bd9Sstevel@tonic-gate 		}
3157c478bd9Sstevel@tonic-gate 	} else if (!is_switch_policy(conf, target_service)) {
3167c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
3177c478bd9Sstevel@tonic-gate 		"\n%s\nThe publickey entry in %s is \"publickey: %s\".\n",
31836e852a1SRaja Andra 		    head_msg, __NSW_CONFIG_FILE,
31936e852a1SRaja Andra 		    switch_policy_str(conf));
3207c478bd9Sstevel@tonic-gate 		policy_correct = 0;
3217c478bd9Sstevel@tonic-gate 	}
3227c478bd9Sstevel@tonic-gate 	/* should we exit ? */
3237c478bd9Sstevel@tonic-gate 	if (policy_correct == 0)
3247c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
3257c478bd9Sstevel@tonic-gate 		"It should be \"publickey: %s\"%s\n\n",
32636e852a1SRaja Andra 		    target_service, tail_msg);
3277c478bd9Sstevel@tonic-gate 	if (conf)
3287c478bd9Sstevel@tonic-gate 		__nsw_freeconfig(conf);
3297c478bd9Sstevel@tonic-gate 
3307c478bd9Sstevel@tonic-gate 	return (policy_correct);
3317c478bd9Sstevel@tonic-gate }
3327c478bd9Sstevel@tonic-gate 
3337c478bd9Sstevel@tonic-gate int
get_pk_source(char * pk_service)3347c478bd9Sstevel@tonic-gate get_pk_source(char *pk_service)
3357c478bd9Sstevel@tonic-gate {
3367c478bd9Sstevel@tonic-gate 	int db = 0, got_from_switch = 0;
3377c478bd9Sstevel@tonic-gate 
3387c478bd9Sstevel@tonic-gate 	/* No service specified, try to figure out from switch */
3397c478bd9Sstevel@tonic-gate 	if (pk_service == 0) {
3407c478bd9Sstevel@tonic-gate 		pk_service = first_and_only_switch_policy("publickey", 0,
34136e852a1SRaja Andra 		    "ERROR:");
3427c478bd9Sstevel@tonic-gate 		if (pk_service == 0)
3437c478bd9Sstevel@tonic-gate 			return (0);
3447c478bd9Sstevel@tonic-gate 		(void) fprintf(stdout,
34536e852a1SRaja Andra 		    "Updating %s publickey database.\n",
34636e852a1SRaja Andra 		    pk_service);
3477c478bd9Sstevel@tonic-gate 		got_from_switch = 1;
3487c478bd9Sstevel@tonic-gate 	}
3497c478bd9Sstevel@tonic-gate 
3507c478bd9Sstevel@tonic-gate 	if (strcmp(pk_service, "ldap") == 0)
3517c478bd9Sstevel@tonic-gate 		db = PK_LDAP;
3527c478bd9Sstevel@tonic-gate 	else if (strcmp(pk_service, "nis") == 0)
3537c478bd9Sstevel@tonic-gate 		db = PK_YP;
3547c478bd9Sstevel@tonic-gate 	else if (strcmp(pk_service, "files") == 0)
3557c478bd9Sstevel@tonic-gate 		db = PK_FILES;
3567c478bd9Sstevel@tonic-gate 	else return (0);
3577c478bd9Sstevel@tonic-gate 
3587c478bd9Sstevel@tonic-gate 	/*
3597c478bd9Sstevel@tonic-gate 	 * If we didn't get service name from switch, check switch
3607c478bd9Sstevel@tonic-gate 	 * and print warning about it source of publickeys if not unique
3617c478bd9Sstevel@tonic-gate 	 */
3627c478bd9Sstevel@tonic-gate 	if (got_from_switch == 0)
3637c478bd9Sstevel@tonic-gate 		check_switch_policy("publickey", pk_service, 0, "WARNING:",
36436e852a1SRaja Andra 		    db == PK_FILES ? "" :
36536e852a1SRaja Andra 		    "; add 'files' if you want the 'nobody' key.");
3667c478bd9Sstevel@tonic-gate 
3677c478bd9Sstevel@tonic-gate 
3687c478bd9Sstevel@tonic-gate 	return (db); /* all passed */
3697c478bd9Sstevel@tonic-gate }
3707c478bd9Sstevel@tonic-gate 
3717c478bd9Sstevel@tonic-gate 
3727c478bd9Sstevel@tonic-gate /* ***************************** keylogin stuff *************************** */
3737c478bd9Sstevel@tonic-gate int
keylogin(char * netname,char * secret)3747c478bd9Sstevel@tonic-gate keylogin(char *netname, char *secret)
3757c478bd9Sstevel@tonic-gate {
3767c478bd9Sstevel@tonic-gate 	struct key_netstarg netst;
3777c478bd9Sstevel@tonic-gate 
3787c478bd9Sstevel@tonic-gate 	netst.st_pub_key[0] = 0;
3797c478bd9Sstevel@tonic-gate 	memcpy(netst.st_priv_key, secret, HEXKEYBYTES);
3807c478bd9Sstevel@tonic-gate 	netst.st_netname = netname;
3817c478bd9Sstevel@tonic-gate 
3827c478bd9Sstevel@tonic-gate #ifdef NFS_AUTH
3837c478bd9Sstevel@tonic-gate 	nra.authtype = AUTH_DES;	/* only revoke DES creds */
3847c478bd9Sstevel@tonic-gate 	nra.uid = getuid();		/* use the real uid */
3857c478bd9Sstevel@tonic-gate 	if (_nfssys(NFS_REVAUTH, &nra) < 0) {
3867c478bd9Sstevel@tonic-gate 		perror("Warning: NFS credentials not destroyed");
3877c478bd9Sstevel@tonic-gate 		err = 1;
3887c478bd9Sstevel@tonic-gate 	}
3897c478bd9Sstevel@tonic-gate #endif
3907c478bd9Sstevel@tonic-gate 
3917c478bd9Sstevel@tonic-gate 
3927c478bd9Sstevel@tonic-gate 	/* do actual key login */
3937c478bd9Sstevel@tonic-gate 	if (key_setnet(&netst) < 0) {
3947c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
39536e852a1SRaja Andra 		    "Could not set %s's secret key\n", netname);
3967c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "May be the keyserv is down?\n");
3977c478bd9Sstevel@tonic-gate 		return (0);
3987c478bd9Sstevel@tonic-gate 	}
3997c478bd9Sstevel@tonic-gate 
4007c478bd9Sstevel@tonic-gate 	return (1);
4017c478bd9Sstevel@tonic-gate }
4027c478bd9Sstevel@tonic-gate 
4037c478bd9Sstevel@tonic-gate nis_object *
init_entry()4047c478bd9Sstevel@tonic-gate init_entry()
4057c478bd9Sstevel@tonic-gate {
4067c478bd9Sstevel@tonic-gate 	static nis_object	obj;
4077c478bd9Sstevel@tonic-gate 	static entry_col	cred_data[10];
4087c478bd9Sstevel@tonic-gate 	entry_obj		*eo;
4097c478bd9Sstevel@tonic-gate 
4107c478bd9Sstevel@tonic-gate 	memset((char *)(&obj), 0, sizeof (obj));
4117c478bd9Sstevel@tonic-gate 	memset((char *)(cred_data), 0, sizeof (entry_col) * 10);
4127c478bd9Sstevel@tonic-gate 
4137c478bd9Sstevel@tonic-gate 	obj.zo_name = "cred";
4147c478bd9Sstevel@tonic-gate 	obj.zo_group = "";
4157c478bd9Sstevel@tonic-gate 	obj.zo_ttl = 43200;
4167c478bd9Sstevel@tonic-gate 	obj.zo_data.zo_type = NIS_ENTRY_OBJ;
4177c478bd9Sstevel@tonic-gate 	eo = &(obj.EN_data);
4187c478bd9Sstevel@tonic-gate 	eo->en_type = "cred_tbl";
4197c478bd9Sstevel@tonic-gate 	eo->en_cols.en_cols_val = cred_data;
4207c478bd9Sstevel@tonic-gate 	eo->en_cols.en_cols_len = 5;
4217c478bd9Sstevel@tonic-gate 	cred_data[4].ec_flags |= EN_CRYPT;
4227c478bd9Sstevel@tonic-gate 	return (&obj);
4237c478bd9Sstevel@tonic-gate }
4247c478bd9Sstevel@tonic-gate 
4257c478bd9Sstevel@tonic-gate 
4267c478bd9Sstevel@tonic-gate static char	*attrFilter[] = {
4277c478bd9Sstevel@tonic-gate 	"objectclass",
4287c478bd9Sstevel@tonic-gate 	"nispublickey",
4297c478bd9Sstevel@tonic-gate 	"nissecretkey",
4307c478bd9Sstevel@tonic-gate 	(char *)NULL
4317c478bd9Sstevel@tonic-gate };
4327c478bd9Sstevel@tonic-gate 
4337c478bd9Sstevel@tonic-gate 
4347c478bd9Sstevel@tonic-gate /* Determines if there is a NisKeyObject objectclass in a given entry */
4357c478bd9Sstevel@tonic-gate static int
ldap_keyobj_exist(ns_ldap_entry_t * entry)4367c478bd9Sstevel@tonic-gate ldap_keyobj_exist(ns_ldap_entry_t *entry)
4377c478bd9Sstevel@tonic-gate {
4387c478bd9Sstevel@tonic-gate 	char		**fattrs;
4397c478bd9Sstevel@tonic-gate 
4407c478bd9Sstevel@tonic-gate 	fattrs = __ns_ldap_getAttr(entry, "objectClass");
4417c478bd9Sstevel@tonic-gate 
4427c478bd9Sstevel@tonic-gate 	if (fattrs == NULL)
4437c478bd9Sstevel@tonic-gate 		return (1);
4447c478bd9Sstevel@tonic-gate 
4457c478bd9Sstevel@tonic-gate 	while (*fattrs) {
4467c478bd9Sstevel@tonic-gate 		if (strcasecmp("NisKeyObject", *fattrs) == 0)
4477c478bd9Sstevel@tonic-gate 			return (1);
4487c478bd9Sstevel@tonic-gate 		fattrs++;
4497c478bd9Sstevel@tonic-gate 	}
4507c478bd9Sstevel@tonic-gate 
4517c478bd9Sstevel@tonic-gate 	return (0);
4527c478bd9Sstevel@tonic-gate }
4537c478bd9Sstevel@tonic-gate 
4547c478bd9Sstevel@tonic-gate 
4557c478bd9Sstevel@tonic-gate static char *keyAttrs[] = {
4567c478bd9Sstevel@tonic-gate 	"nispublickey",
4577c478bd9Sstevel@tonic-gate 	"nissecretkey",
4587c478bd9Sstevel@tonic-gate 	NULL
4597c478bd9Sstevel@tonic-gate };
4607c478bd9Sstevel@tonic-gate 
4617c478bd9Sstevel@tonic-gate /*
4627c478bd9Sstevel@tonic-gate  * Replace or append new attribute value(s) to an attribute.
4637c478bd9Sstevel@tonic-gate  * Don't care about memory leaks, because program is short running.
4647c478bd9Sstevel@tonic-gate  */
4657c478bd9Sstevel@tonic-gate 
4667c478bd9Sstevel@tonic-gate static int
ldap_attr_mod(ns_ldap_entry_t * entry,char * mechname,char * public,ns_ldap_attr_t ** pkeyattrs,char * crypt,ns_ldap_attr_t ** ckeyattrs)467*c77bf777SToomas Soome ldap_attr_mod(ns_ldap_entry_t *entry, char *mechname, char *public,
468*c77bf777SToomas Soome     ns_ldap_attr_t **pkeyattrs, char *crypt, ns_ldap_attr_t **ckeyattrs)
4697c478bd9Sstevel@tonic-gate {
4707c478bd9Sstevel@tonic-gate 	char		**alist[2];
4717c478bd9Sstevel@tonic-gate 	char		*keys[2];
4727c478bd9Sstevel@tonic-gate 
4737c478bd9Sstevel@tonic-gate 	char		*mechfilter;
4747c478bd9Sstevel@tonic-gate 	int		mechfilterlen;
4757c478bd9Sstevel@tonic-gate 	int		q = 0;
4767c478bd9Sstevel@tonic-gate 	int		i, j;
4777c478bd9Sstevel@tonic-gate 	int		keycount[] = {0, 0};
4787c478bd9Sstevel@tonic-gate 	ns_ldap_attr_t	*attrs;
4797c478bd9Sstevel@tonic-gate 
4807c478bd9Sstevel@tonic-gate 	keys[0] = public;
4817c478bd9Sstevel@tonic-gate 	keys[1] = crypt;
4827c478bd9Sstevel@tonic-gate 
4837c478bd9Sstevel@tonic-gate 	mechfilter = (char *)malloc(strlen(mechname) + 3);
4847c478bd9Sstevel@tonic-gate 	if (mechfilter == NULL)
4857c478bd9Sstevel@tonic-gate 		return (0);
4867c478bd9Sstevel@tonic-gate 	sprintf(mechfilter, "{%s}", mechname);
4877c478bd9Sstevel@tonic-gate 	mechfilterlen = strlen(mechfilter);
4887c478bd9Sstevel@tonic-gate 
4897c478bd9Sstevel@tonic-gate 	for (q = 0; keyAttrs[q] != NULL; q++) {
4907c478bd9Sstevel@tonic-gate 		int		found = 0;
4917c478bd9Sstevel@tonic-gate 
4927c478bd9Sstevel@tonic-gate 		for (i = 0; i < entry->attr_count; i++) {
4937c478bd9Sstevel@tonic-gate 			int		rep = 0;
4947c478bd9Sstevel@tonic-gate 			ns_ldap_attr_t	*attr = entry->attr_pair[i];
4957c478bd9Sstevel@tonic-gate 			char		*name = attr->attrname;
4967c478bd9Sstevel@tonic-gate 			int		count = 0;
4977c478bd9Sstevel@tonic-gate 
4987c478bd9Sstevel@tonic-gate 			if (strcasecmp(keyAttrs[q], name) == 0) {
4997c478bd9Sstevel@tonic-gate 				found++;
5007c478bd9Sstevel@tonic-gate 				count = attr->value_count;
5017c478bd9Sstevel@tonic-gate 		alist[q] = (char **)malloc(sizeof (char *) * (count + 1));
5027c478bd9Sstevel@tonic-gate 				if (alist[q] == NULL)
5037c478bd9Sstevel@tonic-gate 					return (0);
5047c478bd9Sstevel@tonic-gate 				alist[q][attr->value_count] = NULL;
5057c478bd9Sstevel@tonic-gate 				for (j = 0; j < attr->value_count; j++) {
5067c478bd9Sstevel@tonic-gate 					char	*val = attr->attrvalue[j];
5077c478bd9Sstevel@tonic-gate 					if (strncasecmp(val, mechfilter,
50836e852a1SRaja Andra 					    mechfilterlen) == 0) {
5097c478bd9Sstevel@tonic-gate 						/* Replace entry */
5107c478bd9Sstevel@tonic-gate 						rep++;
5117c478bd9Sstevel@tonic-gate 						alist[q][j] = keys[q];
5127c478bd9Sstevel@tonic-gate 					} else
5137c478bd9Sstevel@tonic-gate 						alist[q][j] = val;
5147c478bd9Sstevel@tonic-gate 					++keycount[q];
5157c478bd9Sstevel@tonic-gate 				}
5167c478bd9Sstevel@tonic-gate 				if (!rep) {
5177c478bd9Sstevel@tonic-gate 					/* Add entry to list */
5187c478bd9Sstevel@tonic-gate 					alist[q] = (char **)realloc(alist[q],
5197c478bd9Sstevel@tonic-gate 					    sizeof (char *) * (count + 2));
5207c478bd9Sstevel@tonic-gate 					if (alist[q] == NULL)
5217c478bd9Sstevel@tonic-gate 						return (0);
5227c478bd9Sstevel@tonic-gate 					alist[q][attr->value_count + 1] = NULL;
5237c478bd9Sstevel@tonic-gate 					alist[q][attr->value_count] = keys[q];
5247c478bd9Sstevel@tonic-gate 					++keycount[q];
5257c478bd9Sstevel@tonic-gate 				}
5267c478bd9Sstevel@tonic-gate 			}
5277c478bd9Sstevel@tonic-gate 		}
5287c478bd9Sstevel@tonic-gate 		if (!found) {
5297c478bd9Sstevel@tonic-gate 			/* Attribute does not exist, add entry anyways */
5307c478bd9Sstevel@tonic-gate 			alist[q] = (char **)malloc(sizeof (char *) * 2);
5317c478bd9Sstevel@tonic-gate 			if (alist[q] == NULL)
5327c478bd9Sstevel@tonic-gate 				return (0);
5337c478bd9Sstevel@tonic-gate 			alist[q][0] = keys[q];
5347c478bd9Sstevel@tonic-gate 			alist[q][1] = NULL;
5357c478bd9Sstevel@tonic-gate 			++keycount[q];
5367c478bd9Sstevel@tonic-gate 		}
5377c478bd9Sstevel@tonic-gate 	}
5387c478bd9Sstevel@tonic-gate 	if ((attrs = (ns_ldap_attr_t *)calloc(1,
53936e852a1SRaja Andra 	    sizeof (ns_ldap_attr_t))) == NULL)
5407c478bd9Sstevel@tonic-gate 		return (0);
5417c478bd9Sstevel@tonic-gate 	attrs->attrname = "nisPublicKey";
5427c478bd9Sstevel@tonic-gate 	attrs->attrvalue = alist[0];
5437c478bd9Sstevel@tonic-gate 	attrs->value_count = keycount[0];
5447c478bd9Sstevel@tonic-gate 	*pkeyattrs = attrs;
5457c478bd9Sstevel@tonic-gate 
5467c478bd9Sstevel@tonic-gate 	if ((attrs = (ns_ldap_attr_t *)calloc(1,
54736e852a1SRaja Andra 	    sizeof (ns_ldap_attr_t))) == NULL)
5487c478bd9Sstevel@tonic-gate 		return (0);
5497c478bd9Sstevel@tonic-gate 	attrs->attrname = "nisSecretKey";
5507c478bd9Sstevel@tonic-gate 	attrs->attrvalue = alist[1];
5517c478bd9Sstevel@tonic-gate 	attrs->value_count = keycount[1];
5527c478bd9Sstevel@tonic-gate 	*ckeyattrs = attrs;
5537c478bd9Sstevel@tonic-gate 	return (1);
5547c478bd9Sstevel@tonic-gate }
5557c478bd9Sstevel@tonic-gate 
5567c478bd9Sstevel@tonic-gate 
5577c478bd9Sstevel@tonic-gate /*
5587c478bd9Sstevel@tonic-gate  * Do the actual Add or update of attributes in attrs.
5597c478bd9Sstevel@tonic-gate  * The parameter 'update4host' is a flag that tells the function which
5607c478bd9Sstevel@tonic-gate  * DN and password should be used to bind to ldap. If it is an update
5617c478bd9Sstevel@tonic-gate  * for a host (update4host > 0), the two parameters "bindDN" and
5627c478bd9Sstevel@tonic-gate  * "bindPasswd" would be used to bind as the directory manager,
5637c478bd9Sstevel@tonic-gate  * otherwise "dn" and "passwd" would be used to bind as an individual
5647c478bd9Sstevel@tonic-gate  * user.
5657c478bd9Sstevel@tonic-gate  */
5667c478bd9Sstevel@tonic-gate static void
update_ldap_attr(const char * dn,ns_ldap_attr_t ** attrs,const char * passwd,int add,int update4host,const char * bindDN,const char * bindPasswd)567*c77bf777SToomas Soome update_ldap_attr(const char *dn, ns_ldap_attr_t **attrs, const char *passwd,
568*c77bf777SToomas Soome     int add, int update4host, const char *bindDN, const char *bindPasswd)
5697c478bd9Sstevel@tonic-gate {
5707c478bd9Sstevel@tonic-gate 	int		ldaprc;
5717c478bd9Sstevel@tonic-gate 	int		authstried = 0;
5727c478bd9Sstevel@tonic-gate 	char		*msg;
5737c478bd9Sstevel@tonic-gate 	char		*ldap_pw;
5747c478bd9Sstevel@tonic-gate 	char		**certpath = NULL;
5757c478bd9Sstevel@tonic-gate 	ns_auth_t	**app;
5767c478bd9Sstevel@tonic-gate 	ns_auth_t	**authpp = NULL;
5777c478bd9Sstevel@tonic-gate 	ns_auth_t	*authp = NULL;
5787c478bd9Sstevel@tonic-gate 	ns_cred_t	*credp;
5797c478bd9Sstevel@tonic-gate 	ns_ldap_error_t	*errorp = NULL;
5807c478bd9Sstevel@tonic-gate 	int		status;
5817c478bd9Sstevel@tonic-gate 
5827c478bd9Sstevel@tonic-gate 	if ((credp = (ns_cred_t *)calloc(1, sizeof (ns_cred_t))) == NULL) {
5837c478bd9Sstevel@tonic-gate 		fprintf(stderr, "Can not allocate cred buffer.\n");
5847c478bd9Sstevel@tonic-gate 		goto out;
5857c478bd9Sstevel@tonic-gate 	}
5867c478bd9Sstevel@tonic-gate 
5877c478bd9Sstevel@tonic-gate 	/*
5887c478bd9Sstevel@tonic-gate 	 * if this is an update for host, use the bindDN from the
5897c478bd9Sstevel@tonic-gate 	 * command prompt, otherwise use user's DN directly.
5907c478bd9Sstevel@tonic-gate 	 */
5917c478bd9Sstevel@tonic-gate 	if (update4host)
5927c478bd9Sstevel@tonic-gate 		credp->cred.unix_cred.userID = strdup(bindDN);
5937c478bd9Sstevel@tonic-gate 	else
5947c478bd9Sstevel@tonic-gate 		credp->cred.unix_cred.userID = strdup(dn);
5957c478bd9Sstevel@tonic-gate 
5967c478bd9Sstevel@tonic-gate 	if (credp->cred.unix_cred.userID == NULL) {
5977c478bd9Sstevel@tonic-gate 		fprintf(stderr, "Memory allocation failure (userID)\n");
5987c478bd9Sstevel@tonic-gate 		goto out;
5997c478bd9Sstevel@tonic-gate 	}
6007c478bd9Sstevel@tonic-gate 
6017c478bd9Sstevel@tonic-gate 	if (update4host) {
6027c478bd9Sstevel@tonic-gate 		credp->cred.unix_cred.passwd = strdup(bindPasswd);
6037c478bd9Sstevel@tonic-gate 	} else {
6047c478bd9Sstevel@tonic-gate 		if (passwd)
6057c478bd9Sstevel@tonic-gate 			credp->cred.unix_cred.passwd = strdup(passwd);
6067c478bd9Sstevel@tonic-gate 		else {
6077c478bd9Sstevel@tonic-gate 			/* Make sure a valid password is received. */
6087c478bd9Sstevel@tonic-gate 			status = get_ldap_bindPassword(&ldap_pw);
6097c478bd9Sstevel@tonic-gate 
6107c478bd9Sstevel@tonic-gate 			if (status != PROMPTGET_SUCCESS) {
6117c478bd9Sstevel@tonic-gate 				if (!ldap_pw)
6127c478bd9Sstevel@tonic-gate 					free(ldap_pw);
6137c478bd9Sstevel@tonic-gate 				goto out;
6147c478bd9Sstevel@tonic-gate 			}
6157c478bd9Sstevel@tonic-gate 			credp->cred.unix_cred.passwd = ldap_pw;
6167c478bd9Sstevel@tonic-gate 		}
6177c478bd9Sstevel@tonic-gate 	}
6187c478bd9Sstevel@tonic-gate 
6197c478bd9Sstevel@tonic-gate 	if (credp->cred.unix_cred.passwd == NULL) {
6207c478bd9Sstevel@tonic-gate 		fprintf(stderr, "Memory allocation failure (passwd)\n");
6217c478bd9Sstevel@tonic-gate 		goto out;
6227c478bd9Sstevel@tonic-gate 	}
6237c478bd9Sstevel@tonic-gate 
6247c478bd9Sstevel@tonic-gate 	/* get host certificate path, if one is configured */
6257c478bd9Sstevel@tonic-gate 	if (__ns_ldap_getParam(NS_LDAP_HOST_CERTPATH_P,
62636e852a1SRaja Andra 	    (void ***)&certpath, &errorp) != NS_LDAP_SUCCESS)
6277c478bd9Sstevel@tonic-gate 		goto out;
6287c478bd9Sstevel@tonic-gate 
6297c478bd9Sstevel@tonic-gate 	if (certpath && *certpath)
6307c478bd9Sstevel@tonic-gate 		credp->hostcertpath = *certpath;
6317c478bd9Sstevel@tonic-gate 
6327c478bd9Sstevel@tonic-gate 	/* Load the service specific authentication method */
6337c478bd9Sstevel@tonic-gate 	if (__ns_ldap_getServiceAuthMethods("keyserv", &authpp, &errorp) !=
63436e852a1SRaja Andra 	    NS_LDAP_SUCCESS)
6357c478bd9Sstevel@tonic-gate 		goto out;
6367c478bd9Sstevel@tonic-gate 
6377c478bd9Sstevel@tonic-gate 	/*
6387c478bd9Sstevel@tonic-gate 	 * if authpp is null, there is no serviceAuthenticationMethod
6397c478bd9Sstevel@tonic-gate 	 * try default authenticationMethod
6407c478bd9Sstevel@tonic-gate 	 */
6417c478bd9Sstevel@tonic-gate 	if (authpp == NULL) {
6427c478bd9Sstevel@tonic-gate 		if (__ns_ldap_getParam(NS_LDAP_AUTH_P, (void ***)&authpp,
64336e852a1SRaja Andra 		    &errorp) != NS_LDAP_SUCCESS)
6447c478bd9Sstevel@tonic-gate 			goto out;
6457c478bd9Sstevel@tonic-gate 	}
6467c478bd9Sstevel@tonic-gate 
6477c478bd9Sstevel@tonic-gate 	/*
6487c478bd9Sstevel@tonic-gate 	 * if authpp is still null, then can not authenticate, log
6497c478bd9Sstevel@tonic-gate 	 * error message and return error
6507c478bd9Sstevel@tonic-gate 	 */
6517c478bd9Sstevel@tonic-gate 	if (authpp == NULL) {
6527c478bd9Sstevel@tonic-gate 		fprintf(stderr, "No LDAP authentication method configured.\n"
65336e852a1SRaja Andra 		    " configured.\n");
6547c478bd9Sstevel@tonic-gate 		goto out;
6557c478bd9Sstevel@tonic-gate 	}
6567c478bd9Sstevel@tonic-gate 
6577c478bd9Sstevel@tonic-gate 	/*
6587c478bd9Sstevel@tonic-gate 	 * Walk the array and try all authentication methods in order except
6597c478bd9Sstevel@tonic-gate 	 * for "none".
6607c478bd9Sstevel@tonic-gate 	 */
6617c478bd9Sstevel@tonic-gate 	for (app = authpp; *app; app++) {
6627c478bd9Sstevel@tonic-gate 		authp = *app;
6637c478bd9Sstevel@tonic-gate 		/* what about disabling other mechanisms? "tls:sasl/EXTERNAL" */
6647c478bd9Sstevel@tonic-gate 		if (authp->type == NS_LDAP_AUTH_NONE)
6657c478bd9Sstevel@tonic-gate 			continue;
6667c478bd9Sstevel@tonic-gate 		authstried++;
6677c478bd9Sstevel@tonic-gate 		credp->auth.type = authp->type;
6687c478bd9Sstevel@tonic-gate 		credp->auth.tlstype = authp->tlstype;
6697c478bd9Sstevel@tonic-gate 		credp->auth.saslmech = authp->saslmech;
6707c478bd9Sstevel@tonic-gate 		credp->auth.saslopt = authp->saslopt;
6717c478bd9Sstevel@tonic-gate 
6727c478bd9Sstevel@tonic-gate 		if (add == TRUE)
6737c478bd9Sstevel@tonic-gate 			ldaprc = __ns_ldap_addAttr("publickey", dn,
67436e852a1SRaja Andra 			    (const ns_ldap_attr_t * const *)attrs,
675*c77bf777SToomas Soome 			    credp, 0, &errorp);
6767c478bd9Sstevel@tonic-gate 		else
6777c478bd9Sstevel@tonic-gate 			ldaprc = __ns_ldap_repAttr("publickey", dn,
67836e852a1SRaja Andra 			    (const ns_ldap_attr_t * const *)attrs,
679*c77bf777SToomas Soome 			    credp, 0, &errorp);
6807c478bd9Sstevel@tonic-gate 		if (ldaprc == NS_LDAP_SUCCESS) {
6817c478bd9Sstevel@tonic-gate 			/* clean up ns_cred_t structure in memory */
6827c478bd9Sstevel@tonic-gate 			if (credp != NULL)
6837c478bd9Sstevel@tonic-gate 				(void) __ns_ldap_freeCred(&credp);
6847c478bd9Sstevel@tonic-gate 			return;
6857c478bd9Sstevel@tonic-gate 		}
6867c478bd9Sstevel@tonic-gate 
6877c478bd9Sstevel@tonic-gate 		/* XXX add checking for cases of authentication errors */
6887c478bd9Sstevel@tonic-gate 		if ((ldaprc == NS_LDAP_INTERNAL) &&
68936e852a1SRaja Andra 		    ((errorp->status == LDAP_INAPPROPRIATE_AUTH) ||
69036e852a1SRaja Andra 		    (errorp->status == LDAP_INVALID_CREDENTIALS))) {
6917c478bd9Sstevel@tonic-gate 			fprintf(stderr, "LDAP authentication failed.\n");
6927c478bd9Sstevel@tonic-gate 			goto out;
6937c478bd9Sstevel@tonic-gate 		}
6947c478bd9Sstevel@tonic-gate 	}
6957c478bd9Sstevel@tonic-gate 	if (authstried == 0)
6967c478bd9Sstevel@tonic-gate 		fprintf(stderr, "No legal authentication method configured.\n");
6977c478bd9Sstevel@tonic-gate 
6987c478bd9Sstevel@tonic-gate out:
6997c478bd9Sstevel@tonic-gate 	/* clean up ns_cred_t structure in memory */
7007c478bd9Sstevel@tonic-gate 	if (credp != NULL) {
7017c478bd9Sstevel@tonic-gate 		(void) __ns_ldap_freeCred(&credp);
7027c478bd9Sstevel@tonic-gate 	}
7037c478bd9Sstevel@tonic-gate 
7047c478bd9Sstevel@tonic-gate 	if (errorp) {
7057c478bd9Sstevel@tonic-gate 		__ns_ldap_err2str(errorp->status, &msg);
7067c478bd9Sstevel@tonic-gate 		fprintf(stderr, "LDAP error: %s.\n", msg);
7077c478bd9Sstevel@tonic-gate 	}
7087c478bd9Sstevel@tonic-gate 	fprintf(stderr, "%s: key-pair(s) unchanged.\n", program_name);
7097c478bd9Sstevel@tonic-gate 	exit(1);
7107c478bd9Sstevel@tonic-gate }
7117c478bd9Sstevel@tonic-gate 
7127c478bd9Sstevel@tonic-gate 
7137c478bd9Sstevel@tonic-gate /*
7147c478bd9Sstevel@tonic-gate  * Update LDAP nisplublickey entry with new key information via SLDAP.
7157c478bd9Sstevel@tonic-gate  * Free and clean up memory that stores credential data soon after
7167c478bd9Sstevel@tonic-gate  * they are not used or an error comes up.
7177c478bd9Sstevel@tonic-gate  */
7187c478bd9Sstevel@tonic-gate int
ldap_update(char * mechname,char * netname,char * public,char * crypt,char * passwd)719*c77bf777SToomas Soome ldap_update(char *mechname, char *netname, char *public, char *crypt,
720*c77bf777SToomas Soome     char *passwd)
7217c478bd9Sstevel@tonic-gate {
7227c478bd9Sstevel@tonic-gate 	char		*netnamecpy;
7237c478bd9Sstevel@tonic-gate 	char		*id;
7247c478bd9Sstevel@tonic-gate 	char		*domain;
7257c478bd9Sstevel@tonic-gate 	char		*dn;
7267c478bd9Sstevel@tonic-gate 	char		*db;
7277c478bd9Sstevel@tonic-gate 	char		*filter;
7287c478bd9Sstevel@tonic-gate 	ns_ldap_error_t	*errorp;
7297c478bd9Sstevel@tonic-gate 	char		*pkeyatval, *ckeyatval;
7307c478bd9Sstevel@tonic-gate 	ns_ldap_result_t	*res;
7317c478bd9Sstevel@tonic-gate 	ns_ldap_attr_t	*pattrs, *cattrs;
7327c478bd9Sstevel@tonic-gate 	int		update4host = FALSE;
7337c478bd9Sstevel@tonic-gate 	char		*bindDN = NULL;
7347c478bd9Sstevel@tonic-gate 	char		*bindPasswd = NULL;
7357c478bd9Sstevel@tonic-gate 	int		status;
7367c478bd9Sstevel@tonic-gate 
7377c478bd9Sstevel@tonic-gate 	/* Generate DN */
7387c478bd9Sstevel@tonic-gate 	if ((netnamecpy = strdup(netname)) == NULL)
7397c478bd9Sstevel@tonic-gate 		return (0);
7407c478bd9Sstevel@tonic-gate 	if (((id = strchr(netnamecpy, '.')) == NULL) ||
7417c478bd9Sstevel@tonic-gate 	    ((domain = strchr(netnamecpy, '@')) == NULL))
7427c478bd9Sstevel@tonic-gate 		return (0);
7437c478bd9Sstevel@tonic-gate 	else {
7447c478bd9Sstevel@tonic-gate 		*domain++ = '\0';
7457c478bd9Sstevel@tonic-gate 		*id++ = '\0';
7467c478bd9Sstevel@tonic-gate 
7477c478bd9Sstevel@tonic-gate 		id = strdup(id);
7487c478bd9Sstevel@tonic-gate 		if (id == NULL) {
7497c478bd9Sstevel@tonic-gate 			free(netnamecpy);
7507c478bd9Sstevel@tonic-gate 			fprintf(stderr, "LDAP memory error (id)\n");
7517c478bd9Sstevel@tonic-gate 			return (0);
7527c478bd9Sstevel@tonic-gate 		}
7537c478bd9Sstevel@tonic-gate 		domain = strdup(domain);
7547c478bd9Sstevel@tonic-gate 		if (domain == NULL) {
7557c478bd9Sstevel@tonic-gate 			free(netnamecpy);
7567c478bd9Sstevel@tonic-gate 			free(id);
7577c478bd9Sstevel@tonic-gate 			fprintf(stderr, "LDAP memory error (domain)\n");
7587c478bd9Sstevel@tonic-gate 			return (0);
7597c478bd9Sstevel@tonic-gate 		}
7607c478bd9Sstevel@tonic-gate 		free(netnamecpy);
7617c478bd9Sstevel@tonic-gate 	}
7627c478bd9Sstevel@tonic-gate 
7637c478bd9Sstevel@tonic-gate 	if (isdigit(*id)) {
7647c478bd9Sstevel@tonic-gate 		/* We be user. */
7657c478bd9Sstevel@tonic-gate 		__ns_ldap_uid2dn(id, &dn, NULL, &errorp);
7667c478bd9Sstevel@tonic-gate 		if (dn == NULL) {
7677c478bd9Sstevel@tonic-gate 			fprintf(stderr, "Could not obtain LDAP dn\n");
7687c478bd9Sstevel@tonic-gate 			fprintf(stderr, "%s: key-pair(s) unchanged.\n",
76936e852a1SRaja Andra 			    program_name);
7707c478bd9Sstevel@tonic-gate 			exit(1);
7717c478bd9Sstevel@tonic-gate 		}
7727c478bd9Sstevel@tonic-gate 		db = "passwd";
7737c478bd9Sstevel@tonic-gate 		filter = (char *)malloc(strlen(id) + 13);
7747c478bd9Sstevel@tonic-gate 		if (filter)
7757c478bd9Sstevel@tonic-gate 			sprintf(filter, "(uidnumber=%s)", id);
7767c478bd9Sstevel@tonic-gate 		else {
7777c478bd9Sstevel@tonic-gate 			fprintf(stderr, "Can not allocate filter buffer.\n");
7787c478bd9Sstevel@tonic-gate 			fprintf(stderr, "%s: key-pair(s) unchanged.\n",
77936e852a1SRaja Andra 			    program_name);
7807c478bd9Sstevel@tonic-gate 			exit(1);
7817c478bd9Sstevel@tonic-gate 		}
7827c478bd9Sstevel@tonic-gate 	} else {
7837c478bd9Sstevel@tonic-gate 		/* We be host. */
7847c478bd9Sstevel@tonic-gate 		update4host = TRUE;
7857c478bd9Sstevel@tonic-gate 
7867c478bd9Sstevel@tonic-gate 		__ns_ldap_host2dn(id, NULL, &dn, NULL, &errorp);
7877c478bd9Sstevel@tonic-gate 		if (dn == NULL) {
7887c478bd9Sstevel@tonic-gate 			fprintf(stderr, "Could not obtain LDAP dn\n");
7897c478bd9Sstevel@tonic-gate 			fprintf(stderr, "%s: key-pair(s) unchanged.\n",
79036e852a1SRaja Andra 			    program_name);
7917c478bd9Sstevel@tonic-gate 			exit(1);
7927c478bd9Sstevel@tonic-gate 		}
7937c478bd9Sstevel@tonic-gate 
7947c478bd9Sstevel@tonic-gate 		db = "hosts";
7957c478bd9Sstevel@tonic-gate 		filter = (char *)malloc(strlen(id) + 6);
7967c478bd9Sstevel@tonic-gate 		if (filter)
7977c478bd9Sstevel@tonic-gate 			sprintf(filter, "(cn=%s)", id);
7987c478bd9Sstevel@tonic-gate 		else {
7997c478bd9Sstevel@tonic-gate 			fprintf(stderr, "Can not allocate filter buffer.\n");
8007c478bd9Sstevel@tonic-gate 			fprintf(stderr, "%s: key-pair(s) unchanged.\n",
80136e852a1SRaja Andra 			    program_name);
8027c478bd9Sstevel@tonic-gate 			exit(1);
8037c478bd9Sstevel@tonic-gate 		}
8047c478bd9Sstevel@tonic-gate 
8057c478bd9Sstevel@tonic-gate 		/* Prompt for ldap bind DN for entry udpates */
8067c478bd9Sstevel@tonic-gate 		status = get_ldap_bindDN(&bindDN);
8077c478bd9Sstevel@tonic-gate 
8087c478bd9Sstevel@tonic-gate 		if (status != PROMPTGET_SUCCESS) {
8097c478bd9Sstevel@tonic-gate 			FREE_CREDINFO(bindDN);
8107c478bd9Sstevel@tonic-gate 			fprintf(stderr,
81136e852a1SRaja Andra 			    "Failed to get a valid LDAP bind DN.\n"
81236e852a1SRaja Andra 			    "%s: key-pair(s) unchanged.\n",
81336e852a1SRaja Andra 			    program_name);
8147c478bd9Sstevel@tonic-gate 			exit(1);
8157c478bd9Sstevel@tonic-gate 		}
8167c478bd9Sstevel@tonic-gate 
8177c478bd9Sstevel@tonic-gate 		/* Prompt for ldap bind password */
8187c478bd9Sstevel@tonic-gate 		status = get_ldap_bindPassword(&bindPasswd);
8197c478bd9Sstevel@tonic-gate 
8207c478bd9Sstevel@tonic-gate 		if (status != PROMPTGET_SUCCESS) {
8217c478bd9Sstevel@tonic-gate 			FREE_CREDINFO(bindPasswd);
8227c478bd9Sstevel@tonic-gate 			FREE_CREDINFO(bindDN);
8237c478bd9Sstevel@tonic-gate 
8247c478bd9Sstevel@tonic-gate 			fprintf(stderr,
82536e852a1SRaja Andra 			    "Failed to get a valid LDAP bind password."
82636e852a1SRaja Andra 			    "\n%s: key-pair(s) unchanged.\n",
82736e852a1SRaja Andra 			    program_name);
8287c478bd9Sstevel@tonic-gate 			exit(1);
8297c478bd9Sstevel@tonic-gate 		}
8307c478bd9Sstevel@tonic-gate 	}
8317c478bd9Sstevel@tonic-gate 
8327c478bd9Sstevel@tonic-gate 	/* Construct attribute values */
8337c478bd9Sstevel@tonic-gate 	pkeyatval = (char *)malloc(strlen(mechname) + strlen(public) + 3);
8347c478bd9Sstevel@tonic-gate 	if (pkeyatval == NULL) {
8357c478bd9Sstevel@tonic-gate 		FREE_CREDINFO(bindPasswd);
8367c478bd9Sstevel@tonic-gate 		FREE_CREDINFO(bindDN);
8377c478bd9Sstevel@tonic-gate 		fprintf(stderr, "LDAP memory error (pkeyatval)\n");
8387c478bd9Sstevel@tonic-gate 		fprintf(stderr, "%s: key-pair(s) unchanged.\n", program_name);
8397c478bd9Sstevel@tonic-gate 		exit(1);
8407c478bd9Sstevel@tonic-gate 	}
8417c478bd9Sstevel@tonic-gate 	sprintf(pkeyatval, "{%s}%s", mechname, public);
8427c478bd9Sstevel@tonic-gate 	ckeyatval = (char *)malloc(strlen(mechname) + strlen(crypt) + 3);
8437c478bd9Sstevel@tonic-gate 	if (ckeyatval == NULL) {
8447c478bd9Sstevel@tonic-gate 		FREE_CREDINFO(pkeyatval);
8457c478bd9Sstevel@tonic-gate 		FREE_CREDINFO(bindPasswd);
8467c478bd9Sstevel@tonic-gate 		FREE_CREDINFO(bindDN);
8477c478bd9Sstevel@tonic-gate 		fprintf(stderr, "LDAP memory error (pkeyatval)\n");
8487c478bd9Sstevel@tonic-gate 		fprintf(stderr, "%s: key-pair(s) unchanged.\n", program_name);
8497c478bd9Sstevel@tonic-gate 		exit(1);
8507c478bd9Sstevel@tonic-gate 	}
8517c478bd9Sstevel@tonic-gate 	sprintf(ckeyatval, "{%s}%s", mechname, crypt);
8527c478bd9Sstevel@tonic-gate 
8537c478bd9Sstevel@tonic-gate 	/* Does entry exist? */
8547c478bd9Sstevel@tonic-gate 	if ((__ns_ldap_list(db, filter, NULL, (const char **)attrFilter,
85536e852a1SRaja Andra 	    NULL, 0, &res, &errorp,
85636e852a1SRaja Andra 	    NULL, NULL) == NS_LDAP_SUCCESS) && res == NULL) {
8577c478bd9Sstevel@tonic-gate 		FREE_CREDINFO(ckeyatval);
8587c478bd9Sstevel@tonic-gate 		FREE_CREDINFO(pkeyatval);
8597c478bd9Sstevel@tonic-gate 		FREE_CREDINFO(bindPasswd);
8607c478bd9Sstevel@tonic-gate 		FREE_CREDINFO(bindDN);
8617c478bd9Sstevel@tonic-gate 		fprintf(stderr, "LDAP entry does not exist.\n");
8627c478bd9Sstevel@tonic-gate 		fprintf(stderr, "%s: key-pair(s) unchanged.\n", program_name);
8637c478bd9Sstevel@tonic-gate 		exit(1);
8647c478bd9Sstevel@tonic-gate 	}
8657c478bd9Sstevel@tonic-gate 
8667c478bd9Sstevel@tonic-gate 	/* Entry exists, modify attributes for public and secret keys */
8677c478bd9Sstevel@tonic-gate 
8687c478bd9Sstevel@tonic-gate 	/* Is there a NisKeyObject in entry? */
8697c478bd9Sstevel@tonic-gate 	if (!ldap_keyobj_exist(&res->entry[0])) {
8707c478bd9Sstevel@tonic-gate 		/* Add NisKeyObject objectclass and the keys */
8717c478bd9Sstevel@tonic-gate 		char	**newattr;
8727c478bd9Sstevel@tonic-gate 		ns_ldap_attr_t	*attrs[4]; /* objectclass, pk, sk, NULL */
8737c478bd9Sstevel@tonic-gate 
8747c478bd9Sstevel@tonic-gate 		/* set objectclass */
8757c478bd9Sstevel@tonic-gate 		newattr = (char **)calloc(2, sizeof (char *));
8767c478bd9Sstevel@tonic-gate 		newattr[0] = "NisKeyObject";
8777c478bd9Sstevel@tonic-gate 		newattr[1] = NULL;
8787c478bd9Sstevel@tonic-gate 		if ((attrs[0] = (ns_ldap_attr_t *)calloc(1,
87936e852a1SRaja Andra 		    sizeof (ns_ldap_attr_t))) == NULL) {
8807c478bd9Sstevel@tonic-gate 			FREE_CREDINFO(ckeyatval);
8817c478bd9Sstevel@tonic-gate 			FREE_CREDINFO(pkeyatval);
8827c478bd9Sstevel@tonic-gate 			FREE_CREDINFO(bindPasswd);
8837c478bd9Sstevel@tonic-gate 			FREE_CREDINFO(bindDN);
8847c478bd9Sstevel@tonic-gate 			fprintf(stderr, "Memory allocation failed\n");
8857c478bd9Sstevel@tonic-gate 			fprintf(stderr, "%s: key-pair(s) unchanged.\n",
88636e852a1SRaja Andra 			    program_name);
8877c478bd9Sstevel@tonic-gate 			exit(1);
8887c478bd9Sstevel@tonic-gate 		}
8897c478bd9Sstevel@tonic-gate 		attrs[0]->attrname = "objectClass";
8907c478bd9Sstevel@tonic-gate 		attrs[0]->attrvalue = newattr;
8917c478bd9Sstevel@tonic-gate 		attrs[0]->value_count = 1;
8927c478bd9Sstevel@tonic-gate 
8937c478bd9Sstevel@tonic-gate 		/* set publickey */
8947c478bd9Sstevel@tonic-gate 		newattr = (char **)calloc(2, sizeof (char *));
8957c478bd9Sstevel@tonic-gate 		newattr[0] = pkeyatval;
8967c478bd9Sstevel@tonic-gate 		newattr[1] = NULL;
8977c478bd9Sstevel@tonic-gate 		if ((attrs[1] = (ns_ldap_attr_t *)calloc(1,
89836e852a1SRaja Andra 		    sizeof (ns_ldap_attr_t))) == NULL) {
8997c478bd9Sstevel@tonic-gate 			FREE_CREDINFO(ckeyatval);
9007c478bd9Sstevel@tonic-gate 			FREE_CREDINFO(pkeyatval);
9017c478bd9Sstevel@tonic-gate 			FREE_CREDINFO(bindPasswd);
9027c478bd9Sstevel@tonic-gate 			FREE_CREDINFO(bindDN);
9037c478bd9Sstevel@tonic-gate 			fprintf(stderr, "Memory allocation failed\n");
9047c478bd9Sstevel@tonic-gate 			fprintf(stderr, "%s: key-pair(s) unchanged.\n",
90536e852a1SRaja Andra 			    program_name);
9067c478bd9Sstevel@tonic-gate 			exit(1);
9077c478bd9Sstevel@tonic-gate 		}
9087c478bd9Sstevel@tonic-gate 		attrs[1]->attrname = "nisPublicKey";
9097c478bd9Sstevel@tonic-gate 		attrs[1]->attrvalue = newattr;
9107c478bd9Sstevel@tonic-gate 		attrs[1]->value_count = 1;
9117c478bd9Sstevel@tonic-gate 
9127c478bd9Sstevel@tonic-gate 		/* set privatekey */
9137c478bd9Sstevel@tonic-gate 		newattr = (char **)calloc(2, sizeof (char *));
9147c478bd9Sstevel@tonic-gate 		newattr[0] = ckeyatval;
9157c478bd9Sstevel@tonic-gate 		newattr[1] = NULL;
9167c478bd9Sstevel@tonic-gate 		if ((attrs[2] = (ns_ldap_attr_t *)calloc(1,
91736e852a1SRaja Andra 		    sizeof (ns_ldap_attr_t))) == NULL) {
9187c478bd9Sstevel@tonic-gate 			FREE_CREDINFO(ckeyatval);
9197c478bd9Sstevel@tonic-gate 			FREE_CREDINFO(pkeyatval);
9207c478bd9Sstevel@tonic-gate 			FREE_CREDINFO(bindPasswd);
9217c478bd9Sstevel@tonic-gate 			FREE_CREDINFO(bindDN);
9227c478bd9Sstevel@tonic-gate 			fprintf(stderr, "Memory allocation failed\n");
9237c478bd9Sstevel@tonic-gate 			fprintf(stderr, "%s: key-pair(s) unchanged.\n",
92436e852a1SRaja Andra 			    program_name);
9257c478bd9Sstevel@tonic-gate 			exit(1);
9267c478bd9Sstevel@tonic-gate 		}
9277c478bd9Sstevel@tonic-gate 		attrs[2]->attrname = "nisSecretKey";
9287c478bd9Sstevel@tonic-gate 		attrs[2]->attrvalue = newattr;
9297c478bd9Sstevel@tonic-gate 		attrs[2]->value_count = 1;
9307c478bd9Sstevel@tonic-gate 
9317c478bd9Sstevel@tonic-gate 		/* terminator */
9327c478bd9Sstevel@tonic-gate 		attrs[3] = NULL;
9337c478bd9Sstevel@tonic-gate 
9347c478bd9Sstevel@tonic-gate 		update_ldap_attr(dn, attrs, passwd, TRUE, update4host,
93536e852a1SRaja Andra 		    bindDN, bindPasswd);
9367c478bd9Sstevel@tonic-gate 	} else {
9377c478bd9Sstevel@tonic-gate 		/* object class already exists, replace keys */
9387c478bd9Sstevel@tonic-gate 		ns_ldap_attr_t	*attrs[4]; /* objectclass, pk, sk, NULL */
9397c478bd9Sstevel@tonic-gate 
9407c478bd9Sstevel@tonic-gate 		if (!ldap_attr_mod(&res->entry[0], mechname,
94136e852a1SRaja Andra 		    pkeyatval, &pattrs,
94236e852a1SRaja Andra 		    ckeyatval, &cattrs)) {
9437c478bd9Sstevel@tonic-gate 			FREE_CREDINFO(ckeyatval);
9447c478bd9Sstevel@tonic-gate 			FREE_CREDINFO(pkeyatval);
9457c478bd9Sstevel@tonic-gate 			FREE_CREDINFO(bindPasswd);
9467c478bd9Sstevel@tonic-gate 			FREE_CREDINFO(bindDN);
9477c478bd9Sstevel@tonic-gate 			fprintf(stderr,
94836e852a1SRaja Andra 			    "Could not generate LDAP attribute list.\n");
9497c478bd9Sstevel@tonic-gate 			fprintf(stderr,
95036e852a1SRaja Andra 			    "%s: key-pair(s) unchanged.\n", program_name);
9517c478bd9Sstevel@tonic-gate 			exit(1);
9527c478bd9Sstevel@tonic-gate 		}
9537c478bd9Sstevel@tonic-gate 
9547c478bd9Sstevel@tonic-gate 		attrs[0] = pattrs;
9557c478bd9Sstevel@tonic-gate 		attrs[1] = cattrs;
9567c478bd9Sstevel@tonic-gate 		attrs[2] = NULL;
9577c478bd9Sstevel@tonic-gate 
9587c478bd9Sstevel@tonic-gate 		update_ldap_attr(dn, attrs, passwd, FALSE, update4host,
95936e852a1SRaja Andra 		    bindDN, bindPasswd);
9607c478bd9Sstevel@tonic-gate 	}
9617c478bd9Sstevel@tonic-gate 
9627c478bd9Sstevel@tonic-gate 	FREE_CREDINFO(ckeyatval);
9637c478bd9Sstevel@tonic-gate 	FREE_CREDINFO(pkeyatval);
9647c478bd9Sstevel@tonic-gate 	FREE_CREDINFO(bindPasswd);
9657c478bd9Sstevel@tonic-gate 	FREE_CREDINFO(bindDN);
9667c478bd9Sstevel@tonic-gate 
9677c478bd9Sstevel@tonic-gate 	return (0);
9687c478bd9Sstevel@tonic-gate }
969