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
5cb5caa98Sdjl  * Common Development and Distribution License (the "License").
6cb5caa98Sdjl  * 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 /*
22dd1104fbSMichen Chang  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
24*33f5ff17SMilan Jurik  * Copyright 2012 Milan Jurik. All rights reserved.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*
287c478bd9Sstevel@tonic-gate  * ldapclient command. To make (initiailize) or uninitialize a machines as
297c478bd9Sstevel@tonic-gate  * and LDAP client.  This command MUST be run as root (or it will simply exit).
307c478bd9Sstevel@tonic-gate  *
317c478bd9Sstevel@tonic-gate  *	-I	Install. No file_backup/recover for installing only (no doc).
327c478bd9Sstevel@tonic-gate  *
337c478bd9Sstevel@tonic-gate  *	init	Initialze (create) an LDAP client from a profile stored
347c478bd9Sstevel@tonic-gate  *		in a directory-server.
357c478bd9Sstevel@tonic-gate  *	manual	Initialze (create) an LDAP client by hand (-file option
367c478bd9Sstevel@tonic-gate  *		reads from file).
377c478bd9Sstevel@tonic-gate  *	mod	Modify the LDAP client configuration on this machine by hand.
387c478bd9Sstevel@tonic-gate  *	list	List the contents of the LDAP client cache files.
397c478bd9Sstevel@tonic-gate  *	uninit	Uninitialize this machine.
407c478bd9Sstevel@tonic-gate  *
417c478bd9Sstevel@tonic-gate  *	-v	Verbose flag.
427c478bd9Sstevel@tonic-gate  *	-q	Quiet flag (mutually exclusive with -v).
437c478bd9Sstevel@tonic-gate  *
447c478bd9Sstevel@tonic-gate  *	-a attrName=attrVal
457c478bd9Sstevel@tonic-gate  *	<attrName> can be one of the following:
467c478bd9Sstevel@tonic-gate  *
477c478bd9Sstevel@tonic-gate  *	attributeMap
487c478bd9Sstevel@tonic-gate  *		Attribute map.  Can be multiple instances of this option.
497c478bd9Sstevel@tonic-gate  *		(no former option)
507c478bd9Sstevel@tonic-gate  *	authenticationMethod
517c478bd9Sstevel@tonic-gate  *		Authentication method (formerly -a)
527c478bd9Sstevel@tonic-gate  *	bindTimeLimit
537c478bd9Sstevel@tonic-gate  *		Bind time limit. (no former option)
547c478bd9Sstevel@tonic-gate  *	certificatePath
557c478bd9Sstevel@tonic-gate  *		Path to certificates used for secure bind (no former option)
567c478bd9Sstevel@tonic-gate  *	credentialLevel
577c478bd9Sstevel@tonic-gate  *		Client credential level (no former option)
587c478bd9Sstevel@tonic-gate  *	defaultServerList
597c478bd9Sstevel@tonic-gate  *		Default server (no former option) Refer to DUA Config
607c478bd9Sstevel@tonic-gate  *		Schema draft.
617c478bd9Sstevel@tonic-gate  *	defaultSearchBase
627c478bd9Sstevel@tonic-gate  *		Search Base DN. e.g. dc=eng,dc=sun,dc=com (formerly -b)
637c478bd9Sstevel@tonic-gate  *	defaultSearchScope
647c478bd9Sstevel@tonic-gate  *		Search scope. (formerly -s)
657c478bd9Sstevel@tonic-gate  *	domainName
667c478bd9Sstevel@tonic-gate  *		Hosts lookup domain (DNS)  Ex. eng.sun.com (formerly -d)
677c478bd9Sstevel@tonic-gate  *	followReferrals
687c478bd9Sstevel@tonic-gate  *		Search dereference. followref or noref (default followref)
697c478bd9Sstevel@tonic-gate  *		(formerly -r)
707c478bd9Sstevel@tonic-gate  *	objectclassMap
717c478bd9Sstevel@tonic-gate  *		Objectclass map.  Can be multiple instances of this option.
727c478bd9Sstevel@tonic-gate  *		(no former option)
737c478bd9Sstevel@tonic-gate  *	preferredServerList
747c478bd9Sstevel@tonic-gate  *		Server preference list. Comma ',' seperated list of IPaddr.
757c478bd9Sstevel@tonic-gate  *		(formerly -p)
767c478bd9Sstevel@tonic-gate  *	profileName
777c478bd9Sstevel@tonic-gate  *		Profile name to use for init (ldapclient) or
787c478bd9Sstevel@tonic-gate  *		generate (gen_profile). (formerly -P)
797c478bd9Sstevel@tonic-gate  *	profileTTL
807c478bd9Sstevel@tonic-gate  *		Client info TTL.  If set to 0 this information will not be
817c478bd9Sstevel@tonic-gate  *		automatically updated by the ldap_cachemgr(1M).
827c478bd9Sstevel@tonic-gate  *		(formerly -e)
837c478bd9Sstevel@tonic-gate  *	proxyDN
847c478bd9Sstevel@tonic-gate  *		Binding DN.  Ex. cn=client,ou=people,cd=eng,dc=sun,dc=com
857c478bd9Sstevel@tonic-gate  *		(formerly -D)
867c478bd9Sstevel@tonic-gate  *	proxyPassword
877c478bd9Sstevel@tonic-gate  *		Client password not needed for authentication "none".
887c478bd9Sstevel@tonic-gate  *		(formerly -w)
89dd1104fbSMichen Chang  *	adminDN
90dd1104fbSMichen Chang  *		Administrator DN for updating naming data.
91dd1104fbSMichen Chang  *	adminPassword
92dd1104fbSMichen Chang  *		Administrator password
93dd1104fbSMichen Chang  *	enableShadowUpdate
94dd1104fbSMichen Chang  *		Allow Administrator to change shadow data in LDAP
957c478bd9Sstevel@tonic-gate  *	searchTimeLimit
967c478bd9Sstevel@tonic-gate  *		Timeout value. (formerly -o)
977c478bd9Sstevel@tonic-gate  *	serviceSearchDescriptor
987c478bd9Sstevel@tonic-gate  *		Service search scope. (no former option)
997c478bd9Sstevel@tonic-gate  *	serviceAuthenticationMethod
1007c478bd9Sstevel@tonic-gate  *		Service authenticaion method (no former option)
1017c478bd9Sstevel@tonic-gate  *	serviceCredentialLevel
1027c478bd9Sstevel@tonic-gate  *		Service credential level (no former option)
1037c478bd9Sstevel@tonic-gate  *
1047c478bd9Sstevel@tonic-gate  */
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate #include <stdlib.h>
1077c478bd9Sstevel@tonic-gate #include <stdio.h>
1087c478bd9Sstevel@tonic-gate #include <unistd.h>
1097c478bd9Sstevel@tonic-gate #include <errno.h>
1107c478bd9Sstevel@tonic-gate #include <sys/types.h>
1117c478bd9Sstevel@tonic-gate #include <time.h>
1127c478bd9Sstevel@tonic-gate #include <sys/param.h>
1137c478bd9Sstevel@tonic-gate #include <sys/stat.h>
1147c478bd9Sstevel@tonic-gate #include <sys/systeminfo.h>
1157c478bd9Sstevel@tonic-gate #include <fcntl.h>
1167c478bd9Sstevel@tonic-gate #include <xti.h>
1177c478bd9Sstevel@tonic-gate #include <strings.h>
1187c478bd9Sstevel@tonic-gate #include <limits.h>
1197c478bd9Sstevel@tonic-gate #include <locale.h>
1207c478bd9Sstevel@tonic-gate #include <syslog.h>
1217c478bd9Sstevel@tonic-gate #include <libscf.h>
1227c478bd9Sstevel@tonic-gate #include <assert.h>
123e1dd0a2fSth 
124e1dd0a2fSth #include "standalone.h"
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
1277c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SUNW_OST_OSCMD"
1287c478bd9Sstevel@tonic-gate #endif
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate /* error codes */
1317c478bd9Sstevel@tonic-gate /* The manpage doc only allows for SUCCESS(0), FAIL(1) and CRED(2) on exit */
1327c478bd9Sstevel@tonic-gate #define	CLIENT_SUCCESS		0
1337c478bd9Sstevel@tonic-gate #define	CLIENT_ERR_PARSE	-1
1347c478bd9Sstevel@tonic-gate #define	CLIENT_ERR_FAIL		1
1357c478bd9Sstevel@tonic-gate #define	CLIENT_ERR_CREDENTIAL	2
1367c478bd9Sstevel@tonic-gate #define	CLIENT_ERR_MEMORY	3
1377c478bd9Sstevel@tonic-gate #define	CLIENT_ERR_RESTORE	4
1387c478bd9Sstevel@tonic-gate #define	CLIENT_ERR_RENAME	5
1397c478bd9Sstevel@tonic-gate #define	CLIENT_ERR_RECOVER	6
1407c478bd9Sstevel@tonic-gate #define	CLIENT_ERR_TIMEDOUT	7
1417c478bd9Sstevel@tonic-gate #define	CLIENT_ERR_MAINTENANCE	8
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate /* Reset flag for start_services() */
1447c478bd9Sstevel@tonic-gate #define	START_INIT	1
1457c478bd9Sstevel@tonic-gate #define	START_RESET	2
1467c478bd9Sstevel@tonic-gate #define	START_UNINIT	3
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate /* Reset flag for stop_services() */
1497c478bd9Sstevel@tonic-gate #define	STATE_NOSAVE	0
1507c478bd9Sstevel@tonic-gate #define	STATE_SAVE	1
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate /* files to (possibiliy) restore */
1537c478bd9Sstevel@tonic-gate #define	LDAP_RESTORE_DIR	"/var/ldap/restore"
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate #define	DOMAINNAME_DIR		"/etc"
1567c478bd9Sstevel@tonic-gate #define	DOMAINNAME_FILE		"defaultdomain"
1577c478bd9Sstevel@tonic-gate #define	DOMAINNAME		DOMAINNAME_DIR "/" DOMAINNAME_FILE
1587c478bd9Sstevel@tonic-gate #define	DOMAINNAME_BACK		LDAP_RESTORE_DIR "/" DOMAINNAME_FILE
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate #define	NSSWITCH_DIR		"/etc"
1617c478bd9Sstevel@tonic-gate #define	NSSWITCH_FILE		"nsswitch.conf"
1627c478bd9Sstevel@tonic-gate #define	NSSWITCH_CONF		NSSWITCH_DIR "/" NSSWITCH_FILE
1637c478bd9Sstevel@tonic-gate #define	NSSWITCH_BACK		LDAP_RESTORE_DIR "/" NSSWITCH_FILE
1647c478bd9Sstevel@tonic-gate #define	NSSWITCH_LDAP		"/etc/nsswitch.ldap"
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate #define	YP_BIND_DIR		"/var/yp/binding"
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate /* Define the service FMRIs */
1697c478bd9Sstevel@tonic-gate #define	SENDMAIL_FMRI		"network/smtp:sendmail"
1707c478bd9Sstevel@tonic-gate #define	NSCD_FMRI		"system/name-service-cache:default"
1717c478bd9Sstevel@tonic-gate #define	AUTOFS_FMRI		"system/filesystem/autofs:default"
1727c478bd9Sstevel@tonic-gate #define	LDAP_FMRI		"network/ldap/client:default"
1737c478bd9Sstevel@tonic-gate #define	YP_FMRI			"network/nis/client:default"
1747c478bd9Sstevel@tonic-gate #define	NS_MILESTONE_FMRI	"milestone/name-services:default"
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate /* Define flags for checking if services were enabled */
1777c478bd9Sstevel@tonic-gate #define	SENDMAIL_ON	0x1
1787c478bd9Sstevel@tonic-gate #define	NSCD_ON		0x10
1797c478bd9Sstevel@tonic-gate #define	AUTOFS_ON	0x100
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate #define	CMD_DOMAIN_START	"/usr/bin/domainname"
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate /* Command to copy files */
1847c478bd9Sstevel@tonic-gate #define	CMD_CP			"/bin/cp -f"
1857c478bd9Sstevel@tonic-gate #define	CMD_MV			"/bin/mv -f"
1867c478bd9Sstevel@tonic-gate #define	CMD_RM			"/bin/rm -f"
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate #define	TO_DEV_NULL		" >/dev/null 2>&1"
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate /* Files that need to be just removed */
1917c478bd9Sstevel@tonic-gate #define	LDAP_CACHE_LOG		"/var/ldap/cachemgr.log"
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate /* Output defines to supress if quiet mode set */
1947c478bd9Sstevel@tonic-gate #define	CLIENT_FPUTS if (!mode_quiet) (void) fputs
1957c478bd9Sstevel@tonic-gate #define	CLIENT_FPRINTF if (!mode_quiet) (void) fprintf
1967c478bd9Sstevel@tonic-gate #define	CLIENT_FPUTC if (!mode_quiet) (void) fputc
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate #define	restart_service(fmri, waitflag)\
1997c478bd9Sstevel@tonic-gate 		do_service(fmri, waitflag, RESTART_SERVICE,\
2007c478bd9Sstevel@tonic-gate 		SCF_STATE_STRING_ONLINE)
2017c478bd9Sstevel@tonic-gate #define	start_service(fmri, waitflag)	\
2027c478bd9Sstevel@tonic-gate 		do_service(fmri, waitflag, START_SERVICE,\
2037c478bd9Sstevel@tonic-gate 		SCF_STATE_STRING_ONLINE)
2047c478bd9Sstevel@tonic-gate #define	disable_service(fmri, waitflag)	\
2057c478bd9Sstevel@tonic-gate 		do_service(fmri, waitflag, STOP_SERVICE,\
2067c478bd9Sstevel@tonic-gate 		SCF_STATE_STRING_DISABLED)
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate /*
2097c478bd9Sstevel@tonic-gate  * There isn't a domainName defined as a param, so we set a value here
2107c478bd9Sstevel@tonic-gate  * (1001) should be big enough
2117c478bd9Sstevel@tonic-gate  */
2127c478bd9Sstevel@tonic-gate #define	LOCAL_DOMAIN_P 1001
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate #define	START_SERVICE	1
2157c478bd9Sstevel@tonic-gate #define	STOP_SERVICE	2
2167c478bd9Sstevel@tonic-gate #define	RESTART_SERVICE	3
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate #define	DEFAULT_TIMEOUT	60000000
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate #define	INIT_WAIT_USECS	50000
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate /* Used to turn off profile checking */
2237c478bd9Sstevel@tonic-gate #define	CACHETTL_OFF "0"
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate /* Globals */
2267c478bd9Sstevel@tonic-gate static char *cmd;
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate static char *dname = NULL;
2297c478bd9Sstevel@tonic-gate static char dname_buf[BUFSIZ];
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate static boolean_t sysid_install = B_FALSE;
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate static int mode_verbose = 0;
2347c478bd9Sstevel@tonic-gate static int mode_quiet = 0;
2357c478bd9Sstevel@tonic-gate static int gen = 0;
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate static int gStartLdap = 0;
2387c478bd9Sstevel@tonic-gate static int gStartYp = 0;
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate static int enableFlag = 0;
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate /* multival_t is used to hold params that can have more than one value */
2437c478bd9Sstevel@tonic-gate typedef struct {
2447c478bd9Sstevel@tonic-gate 	int count;
2457c478bd9Sstevel@tonic-gate 	char **optlist;
2467c478bd9Sstevel@tonic-gate } multival_t;
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate static multival_t *multival_new();
2497c478bd9Sstevel@tonic-gate static int multival_add(multival_t *list, char *opt);
2507c478bd9Sstevel@tonic-gate static void multival_free(multival_t *list);
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate /*
2537c478bd9Sstevel@tonic-gate  * clientopts_t is used to hold and pass around the param values from
2547c478bd9Sstevel@tonic-gate  * the cmd line
2557c478bd9Sstevel@tonic-gate  */
2567c478bd9Sstevel@tonic-gate typedef struct {
2577c478bd9Sstevel@tonic-gate 	multival_t	*attributeMap;
2587c478bd9Sstevel@tonic-gate 	char		*authenticationMethod;
2597c478bd9Sstevel@tonic-gate 	char		*bindTimeLimit;
2607c478bd9Sstevel@tonic-gate 	char		*certificatePath;
2617c478bd9Sstevel@tonic-gate 	char		*credentialLevel;
2627c478bd9Sstevel@tonic-gate 	char		*defaultSearchBase;
2637c478bd9Sstevel@tonic-gate 	char		*defaultServerList;
2647c478bd9Sstevel@tonic-gate 	char		*domainName;
2657c478bd9Sstevel@tonic-gate 	char		*followReferrals;
2667c478bd9Sstevel@tonic-gate 	multival_t	*objectclassMap;
2677c478bd9Sstevel@tonic-gate 	char		*preferredServerList;
2687c478bd9Sstevel@tonic-gate 	char		*profileName;
2697c478bd9Sstevel@tonic-gate 	char		*profileTTL;
2707c478bd9Sstevel@tonic-gate 	char		*proxyDN;
2717c478bd9Sstevel@tonic-gate 	char		*proxyPassword;
272dd1104fbSMichen Chang 	char		*enableShadowUpdate;
273dd1104fbSMichen Chang 	char		*adminDN;
274dd1104fbSMichen Chang 	char		*adminPassword;
275e1dd0a2fSth 	char		*bindDN;
276e1dd0a2fSth 	char		*bindPasswd;
2777c478bd9Sstevel@tonic-gate 	char		*defaultSearchScope;
2787c478bd9Sstevel@tonic-gate 	char		*searchTimeLimit;
2797c478bd9Sstevel@tonic-gate 	multival_t	*serviceAuthenticationMethod;
2807c478bd9Sstevel@tonic-gate 	multival_t	*serviceCredentialLevel;
2817c478bd9Sstevel@tonic-gate 	multival_t	*serviceSearchDescriptor;
2827c478bd9Sstevel@tonic-gate } clientopts_t;
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate static clientopts_t *clientopts_new();
2857c478bd9Sstevel@tonic-gate static void clientopts_free(clientopts_t *list);
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate extern ns_ldap_error_t *__ns_ldap_print_config(int);
2887c478bd9Sstevel@tonic-gate extern void __ns_ldap_default_config();
289cb5caa98Sdjl extern int __ns_ldap_download(const char *, char *, char *, ns_ldap_error_t **);
2907c478bd9Sstevel@tonic-gate 
2917c478bd9Sstevel@tonic-gate /* Function prototypes (these could be static) */
2927c478bd9Sstevel@tonic-gate static void usage(void);
2937c478bd9Sstevel@tonic-gate 
2947c478bd9Sstevel@tonic-gate static int credCheck(clientopts_t *arglist);
295dd1104fbSMichen Chang static int adminCredCheck(clientopts_t *arglist);
2967c478bd9Sstevel@tonic-gate static int clientSetParam(clientopts_t *optlist, int paramFlag, char *attrVal);
2977c478bd9Sstevel@tonic-gate static int parseParam(char *param, char **paramVal);
2987c478bd9Sstevel@tonic-gate static void dumpargs(clientopts_t *arglist);
2997c478bd9Sstevel@tonic-gate static int num_args(clientopts_t *arglist);
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate static int file_backup(void);
3027c478bd9Sstevel@tonic-gate static int recover(int saveState);
3037c478bd9Sstevel@tonic-gate static int mod_backup(void);
3047c478bd9Sstevel@tonic-gate static int mod_recover(void);
3057c478bd9Sstevel@tonic-gate static void mod_cleanup(void);
3067c478bd9Sstevel@tonic-gate 
3077c478bd9Sstevel@tonic-gate static int client_list(clientopts_t *arglist);
3087c478bd9Sstevel@tonic-gate static int client_manual(clientopts_t *arglist);
3097c478bd9Sstevel@tonic-gate static int client_mod(clientopts_t *arglist);
3107c478bd9Sstevel@tonic-gate static int client_uninit(clientopts_t *arglist);
3117c478bd9Sstevel@tonic-gate static int client_genProfile(clientopts_t *arglist);
3127c478bd9Sstevel@tonic-gate static int client_init(clientopts_t *arglist);
3137c478bd9Sstevel@tonic-gate static int file_move(const char *from, const char *to);
3147c478bd9Sstevel@tonic-gate 
3157c478bd9Sstevel@tonic-gate static int start_services(int flag);
3167c478bd9Sstevel@tonic-gate static int stop_services(int saveState);
3177c478bd9Sstevel@tonic-gate static boolean_t is_service(const char *fmri, const char *state);
3187c478bd9Sstevel@tonic-gate static int wait_till(const char *fmri, const char *state, useconds_t max,
3197c478bd9Sstevel@tonic-gate 		const char *what, boolean_t check_maint);
3207c478bd9Sstevel@tonic-gate static int do_service(const char *fmri, boolean_t waitflag, int dowhat,
3217c478bd9Sstevel@tonic-gate 		const char *state);
3227c478bd9Sstevel@tonic-gate static useconds_t get_timeout_value(int dowhat, const char *fmri,
3237c478bd9Sstevel@tonic-gate 		useconds_t default_val);
3247c478bd9Sstevel@tonic-gate 
325a506a34cSth int
326a506a34cSth main(int argc, char **argv)
3277c478bd9Sstevel@tonic-gate {
328e1dd0a2fSth 	char		*ret_locale, *ret_textdomain;
329e1dd0a2fSth 	int		retcode;
330e1dd0a2fSth 	int		paramFlag;
331e1dd0a2fSth 	char		*attrVal;
332e1dd0a2fSth 	int		sysinfostatus;
333e1dd0a2fSth 	clientopts_t	*optlist = NULL;
334e1dd0a2fSth 	int		op_manual = 0, op_mod = 0, op_uninit = 0;
335e1dd0a2fSth 	int		op_list = 0, op_init = 0, op_genprofile = 0;
336e1dd0a2fSth 	extern char	*optarg;
337e1dd0a2fSth 	extern int	optind;
338e1dd0a2fSth 	int		option;
3397c478bd9Sstevel@tonic-gate 
3407c478bd9Sstevel@tonic-gate 	ret_locale = setlocale(LC_ALL, "");
3417c478bd9Sstevel@tonic-gate 	if (ret_locale == NULL) {
3427c478bd9Sstevel@tonic-gate 		CLIENT_FPUTS(gettext("Unable to set locale.\n"), stderr);
3437c478bd9Sstevel@tonic-gate 	}
3447c478bd9Sstevel@tonic-gate 	ret_textdomain = textdomain(TEXT_DOMAIN);
3457c478bd9Sstevel@tonic-gate 	if (ret_textdomain == NULL) {
3467c478bd9Sstevel@tonic-gate 		CLIENT_FPUTS(gettext("Unable to set textdomain.\n"), stderr);
3477c478bd9Sstevel@tonic-gate 	}
3487c478bd9Sstevel@tonic-gate 
3497c478bd9Sstevel@tonic-gate 	openlog("ldapclient", LOG_PID, LOG_USER);
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate 	/* get name that invoked us */
3527c478bd9Sstevel@tonic-gate 	if (cmd = strrchr(argv[0], '/'))
3537c478bd9Sstevel@tonic-gate 		++cmd;
3547c478bd9Sstevel@tonic-gate 	else
3557c478bd9Sstevel@tonic-gate 		cmd = argv[0];
3567c478bd9Sstevel@tonic-gate 
3577c478bd9Sstevel@tonic-gate 	sysinfostatus = sysinfo(SI_SRPC_DOMAIN, dname_buf, BUFSIZ);
3587c478bd9Sstevel@tonic-gate 	if (0 < sysinfostatus)
3597c478bd9Sstevel@tonic-gate 		dname = &dname_buf[0];
3607c478bd9Sstevel@tonic-gate 
3617c478bd9Sstevel@tonic-gate 	optlist = clientopts_new();
3627c478bd9Sstevel@tonic-gate 	if (optlist == NULL) {
3637c478bd9Sstevel@tonic-gate 		CLIENT_FPUTS(
364e1dd0a2fSth 		    gettext("Error getting optlist (malloc fail)\n"),
365e1dd0a2fSth 		    stderr);
3667c478bd9Sstevel@tonic-gate 		exit(CLIENT_ERR_FAIL);
3677c478bd9Sstevel@tonic-gate 	}
3687c478bd9Sstevel@tonic-gate 
3697c478bd9Sstevel@tonic-gate 	optind = 1;
3707c478bd9Sstevel@tonic-gate 	while (optind < argc) {
371dd1104fbSMichen Chang 		option = getopt(argc, argv, "vqa:ID:w:j:y:z:");
3727c478bd9Sstevel@tonic-gate 
3737c478bd9Sstevel@tonic-gate 		switch (option) {
3747c478bd9Sstevel@tonic-gate 		case 'v':
3757c478bd9Sstevel@tonic-gate 			mode_verbose = 1;
3767c478bd9Sstevel@tonic-gate 			break;
3777c478bd9Sstevel@tonic-gate 		case 'q':
3787c478bd9Sstevel@tonic-gate 			mode_quiet = 1;
3797c478bd9Sstevel@tonic-gate 			break;
3807c478bd9Sstevel@tonic-gate 		case 'a':
3817c478bd9Sstevel@tonic-gate 			attrVal = NULL;
3827c478bd9Sstevel@tonic-gate 			paramFlag = parseParam(optarg, &attrVal);
3837c478bd9Sstevel@tonic-gate 			if (paramFlag == CLIENT_ERR_PARSE) {
3847c478bd9Sstevel@tonic-gate 				CLIENT_FPRINTF(stderr,
385e1dd0a2fSth 				    gettext("Unrecognized "
386e1dd0a2fSth 				    "parameter \"%s\"\n"),
387e1dd0a2fSth 				    optarg);
3887c478bd9Sstevel@tonic-gate 				usage();
3897c478bd9Sstevel@tonic-gate 				exit(CLIENT_ERR_FAIL);
3907c478bd9Sstevel@tonic-gate 			}
391e1dd0a2fSth 			if (paramFlag == NS_LDAP_BINDPASSWD_P &&
392e1dd0a2fSth 			    optlist->proxyPassword != NULL) {
393e1dd0a2fSth 				(void) fprintf(stderr,
394e1dd0a2fSth 				    gettext("The -a proxyPassword option is "
395e1dd0a2fSth 				    "mutually exclusive of -y. "
396e1dd0a2fSth 				    "-a proxyPassword is ignored.\n"));
397e1dd0a2fSth 				break;
398e1dd0a2fSth 			}
399dd1104fbSMichen Chang 			if (paramFlag == NS_LDAP_ADMIN_BINDPASSWD_P &&
400dd1104fbSMichen Chang 			    optlist->adminPassword != NULL) {
401dd1104fbSMichen Chang 				(void) fprintf(stderr,
402dd1104fbSMichen Chang 				    gettext("The -a adminPassword option is "
403dd1104fbSMichen Chang 				    "mutually exclusive of -z. "
404dd1104fbSMichen Chang 				    "-a adminPassword is ignored.\n"));
405dd1104fbSMichen Chang 				break;
406dd1104fbSMichen Chang 			}
4077c478bd9Sstevel@tonic-gate 			retcode = clientSetParam(optlist, paramFlag, attrVal);
4087c478bd9Sstevel@tonic-gate 			if (retcode != CLIENT_SUCCESS) {
4097c478bd9Sstevel@tonic-gate 				CLIENT_FPRINTF(
410e1dd0a2fSth 				    stderr,
411e1dd0a2fSth 				    gettext("Error (%d) setting "
412e1dd0a2fSth 				    "param \"%s\"\n"),
413e1dd0a2fSth 				    retcode, optarg);
4147c478bd9Sstevel@tonic-gate 				usage();
4157c478bd9Sstevel@tonic-gate 				exit(CLIENT_ERR_FAIL);
4167c478bd9Sstevel@tonic-gate 			}
4177c478bd9Sstevel@tonic-gate 			break;
418e1dd0a2fSth 		case 'D':
419e1dd0a2fSth 			optlist->bindDN = strdup(optarg);
420e1dd0a2fSth 			break;
421e1dd0a2fSth 		case 'w':
422e1dd0a2fSth 			if (optlist->bindPasswd != NULL) {
423e1dd0a2fSth 				CLIENT_FPRINTF(stderr,
424e1dd0a2fSth 				    gettext("The -w option is mutually "
425e1dd0a2fSth 				    "exclusive of -j. -w is ignored."));
426e1dd0a2fSth 				break;
427e1dd0a2fSth 			}
428e1dd0a2fSth 
429e1dd0a2fSth 			if (optarg[0] == '-' && optarg[1] == '\0') {
430e1dd0a2fSth 				/* Ask for a password later */
431e1dd0a2fSth 				break;
432e1dd0a2fSth 			}
433e1dd0a2fSth 
434e1dd0a2fSth 			optlist->bindPasswd = strdup(optarg);
435e1dd0a2fSth 			break;
436e1dd0a2fSth 		case 'j':
437e1dd0a2fSth 			if (optlist->bindPasswd != NULL) {
438e1dd0a2fSth 				(void) fprintf(stderr,
439e1dd0a2fSth 				    gettext("The -w option is mutually "
440e1dd0a2fSth 				    "exclusive of -j. -w is ignored.\n"));
441e1dd0a2fSth 				free(optlist->bindPasswd);
442e1dd0a2fSth 			}
443e1dd0a2fSth 			optlist->bindPasswd = readPwd(optarg);
444e1dd0a2fSth 			if (optlist->bindPasswd == NULL) {
445e1dd0a2fSth 				exit(CLIENT_ERR_FAIL);
446e1dd0a2fSth 			}
447e1dd0a2fSth 			break;
448e1dd0a2fSth 		case 'y':
449e1dd0a2fSth 			if (optlist->proxyPassword != NULL) {
450e1dd0a2fSth 				(void) fprintf(stderr,
451e1dd0a2fSth 				    gettext("The -a proxyPassword option is "
452e1dd0a2fSth 				    "mutually exclusive of -y. "
453e1dd0a2fSth 				    "-a proxyPassword is ignored.\n"));
454e1dd0a2fSth 			}
455e1dd0a2fSth 			optlist->proxyPassword = readPwd(optarg);
456e1dd0a2fSth 			if (optlist->proxyPassword == NULL) {
457e1dd0a2fSth 				exit(CLIENT_ERR_FAIL);
458e1dd0a2fSth 			}
459e1dd0a2fSth 			break;
460dd1104fbSMichen Chang 		case 'z':
461dd1104fbSMichen Chang 			if (optlist->adminPassword != NULL) {
462dd1104fbSMichen Chang 				(void) fprintf(stderr,
463dd1104fbSMichen Chang 				    gettext("The -a adminPassword option is "
464dd1104fbSMichen Chang 				    "mutually exclusive of -z. "
465dd1104fbSMichen Chang 				    "-a adminPassword is ignored.\n"));
466dd1104fbSMichen Chang 			}
467dd1104fbSMichen Chang 			optlist->adminPassword = readPwd(optarg);
468dd1104fbSMichen Chang 			if (optlist->adminPassword == NULL) {
469dd1104fbSMichen Chang 				exit(CLIENT_ERR_FAIL);
470dd1104fbSMichen Chang 			}
471dd1104fbSMichen Chang 			break;
4727c478bd9Sstevel@tonic-gate 		case EOF:
4737c478bd9Sstevel@tonic-gate 			if (strcmp(argv[optind], "init") == 0) {
4747c478bd9Sstevel@tonic-gate 				op_init = 1;
4757c478bd9Sstevel@tonic-gate 			} else if (strcmp(argv[optind], "manual") == 0) {
4767c478bd9Sstevel@tonic-gate 				op_manual = 1;
4777c478bd9Sstevel@tonic-gate 			} else if (strcmp(argv[optind], "mod") == 0) {
4787c478bd9Sstevel@tonic-gate 				op_mod = 1;
4797c478bd9Sstevel@tonic-gate 			} else if (strcmp(argv[optind], "list") == 0) {
4807c478bd9Sstevel@tonic-gate 				op_list = 1;
4817c478bd9Sstevel@tonic-gate 			} else if (strcmp(argv[optind], "uninit") == 0) {
4827c478bd9Sstevel@tonic-gate 				op_uninit = 1;
4837c478bd9Sstevel@tonic-gate 			} else if (strcmp(argv[optind], "genprofile") == 0) {
4847c478bd9Sstevel@tonic-gate 				gen = 1;
4857c478bd9Sstevel@tonic-gate 				op_genprofile = 1;
4867c478bd9Sstevel@tonic-gate 			} else if (optind == argc-1) {
4877c478bd9Sstevel@tonic-gate 				retcode = clientSetParam(
488e1dd0a2fSth 				    optlist,
489e1dd0a2fSth 				    NS_LDAP_SERVERS_P,
490e1dd0a2fSth 				    argv[optind]);	/* ipAddr */
4917c478bd9Sstevel@tonic-gate 				if (retcode != CLIENT_SUCCESS) {
4927c478bd9Sstevel@tonic-gate 					CLIENT_FPRINTF(
493e1dd0a2fSth 					    stderr,
494e1dd0a2fSth 					    gettext("Error (%d) setting "
495e1dd0a2fSth 					    "serverList param.\n"),
496e1dd0a2fSth 					    retcode);
4977c478bd9Sstevel@tonic-gate 					usage();
4987c478bd9Sstevel@tonic-gate 					exit(CLIENT_ERR_FAIL);
4997c478bd9Sstevel@tonic-gate 				}
5007c478bd9Sstevel@tonic-gate 			} else {
5017c478bd9Sstevel@tonic-gate 				CLIENT_FPUTS(
502e1dd0a2fSth 				    gettext("Error parsing "
503e1dd0a2fSth 				    "command line\n"),
504e1dd0a2fSth 				    stderr);
5057c478bd9Sstevel@tonic-gate 				usage();
5067c478bd9Sstevel@tonic-gate 				exit(CLIENT_ERR_FAIL);
5077c478bd9Sstevel@tonic-gate 			}
5087c478bd9Sstevel@tonic-gate 			optind++;	/* get past the verb and keep trying */
5097c478bd9Sstevel@tonic-gate 			break;
5107c478bd9Sstevel@tonic-gate 		/* Backwards compatibility to support system install */
5117c478bd9Sstevel@tonic-gate 		case 'I':
5127c478bd9Sstevel@tonic-gate 			sysid_install = B_TRUE;
5137c478bd9Sstevel@tonic-gate 			op_init = 1;
5147c478bd9Sstevel@tonic-gate 			mode_quiet = 1;
5157c478bd9Sstevel@tonic-gate 			break;
5167c478bd9Sstevel@tonic-gate 		case '?':
5177c478bd9Sstevel@tonic-gate 			usage();
5187c478bd9Sstevel@tonic-gate 			CLIENT_FPUTS(gettext("\nOr\n\n"), stderr);
5197c478bd9Sstevel@tonic-gate 			gen = 1;
5207c478bd9Sstevel@tonic-gate 			usage();
5217c478bd9Sstevel@tonic-gate 			exit(CLIENT_ERR_FAIL);
5227c478bd9Sstevel@tonic-gate 			break;
5237c478bd9Sstevel@tonic-gate 		}
5247c478bd9Sstevel@tonic-gate 
5257c478bd9Sstevel@tonic-gate 	}
5267c478bd9Sstevel@tonic-gate 
5277c478bd9Sstevel@tonic-gate 	if ((getuid() != 0) && (!op_genprofile)) {
5287c478bd9Sstevel@tonic-gate 		(void) puts(
529e1dd0a2fSth 		    "You must be root (SuperUser) to run this command.");
5307c478bd9Sstevel@tonic-gate 		usage();
5317c478bd9Sstevel@tonic-gate 		exit(CLIENT_ERR_FAIL);
5327c478bd9Sstevel@tonic-gate 	}
5337c478bd9Sstevel@tonic-gate 
5347c478bd9Sstevel@tonic-gate /*
5357c478bd9Sstevel@tonic-gate  *	All command line arguments are finished being parsed now
5367c478bd9Sstevel@tonic-gate  */
5377c478bd9Sstevel@tonic-gate 
5387c478bd9Sstevel@tonic-gate /* *** Do semantic checking here *** */
5397c478bd9Sstevel@tonic-gate 
5407c478bd9Sstevel@tonic-gate /* if gen and no no searchBase then err */
5417c478bd9Sstevel@tonic-gate 	if (gen && !optlist->defaultSearchBase) {
5427c478bd9Sstevel@tonic-gate 		CLIENT_FPUTS(
543e1dd0a2fSth 		    gettext("ldapclient: Missing required attrName "
544e1dd0a2fSth 		    "defaultSearchBase\n"),
545e1dd0a2fSth 		    stderr);
5467c478bd9Sstevel@tonic-gate 		usage();
5477c478bd9Sstevel@tonic-gate 		clientopts_free(optlist);
5487c478bd9Sstevel@tonic-gate 		exit(CLIENT_ERR_FAIL);
5497c478bd9Sstevel@tonic-gate 	}
5507c478bd9Sstevel@tonic-gate 
551dd1104fbSMichen Chang /*
552dd1104fbSMichen Chang  * if init or manual, and if adminDN is specified then enableShadowUpdate
553dd1104fbSMichen Chang  * must be set to TRUE.
554dd1104fbSMichen Chang  */
555dd1104fbSMichen Chang 	if ((op_init || op_manual) &&
556dd1104fbSMichen Chang 	    (!optlist->enableShadowUpdate ||
557dd1104fbSMichen Chang 	    strcasecmp(optlist->enableShadowUpdate, "TRUE") != 0) &&
558dd1104fbSMichen Chang 	    (optlist->adminDN || optlist->adminPassword)) {
559dd1104fbSMichen Chang 		CLIENT_FPUTS(
560dd1104fbSMichen Chang 		    gettext("ldapclient: adminDN and adminPassword must not "
561dd1104fbSMichen Chang 		    "be specified if enableShadowUpdate is not set to TRUE \n"),
562dd1104fbSMichen Chang 		    stderr);
563dd1104fbSMichen Chang 		usage();
564dd1104fbSMichen Chang 		clientopts_free(optlist);
565dd1104fbSMichen Chang 		exit(CLIENT_ERR_FAIL);
566dd1104fbSMichen Chang 	}
567dd1104fbSMichen Chang 
5687c478bd9Sstevel@tonic-gate /* Only one verb can be specified */
5697c478bd9Sstevel@tonic-gate 	if ((op_init + op_manual + op_mod + op_uninit +
570e1dd0a2fSth 	    op_list + op_genprofile) != 1) {
5717c478bd9Sstevel@tonic-gate 		usage();
5727c478bd9Sstevel@tonic-gate 		clientopts_free(optlist);
5737c478bd9Sstevel@tonic-gate 		exit(CLIENT_ERR_FAIL);
5747c478bd9Sstevel@tonic-gate 	}
5757c478bd9Sstevel@tonic-gate 
5767c478bd9Sstevel@tonic-gate /* *** We passed semantic checking, so now do the operation *** */
5777c478bd9Sstevel@tonic-gate 
5787c478bd9Sstevel@tonic-gate 	if (mode_verbose) {
5797c478bd9Sstevel@tonic-gate 		CLIENT_FPUTS(gettext("Arguments parsed:\n"), stderr);
5807c478bd9Sstevel@tonic-gate 		dumpargs(optlist);
5817c478bd9Sstevel@tonic-gate 	}
5827c478bd9Sstevel@tonic-gate 
5837c478bd9Sstevel@tonic-gate 
5847c478bd9Sstevel@tonic-gate /* handle "ldapclient list" here.  err checking done in func */
5857c478bd9Sstevel@tonic-gate 	if (op_list) {
5867c478bd9Sstevel@tonic-gate 		if (mode_verbose)
5877c478bd9Sstevel@tonic-gate 			CLIENT_FPUTS(
588e1dd0a2fSth 			    gettext("Handling list option\n"),
589e1dd0a2fSth 			    stderr);
5907c478bd9Sstevel@tonic-gate 		retcode = client_list(optlist);
5917c478bd9Sstevel@tonic-gate 	}
5927c478bd9Sstevel@tonic-gate 
5937c478bd9Sstevel@tonic-gate /* handle "ldapclient uninit" here */
5947c478bd9Sstevel@tonic-gate 	if (op_uninit) {
5957c478bd9Sstevel@tonic-gate 		if (mode_verbose)
5967c478bd9Sstevel@tonic-gate 			CLIENT_FPUTS(
597e1dd0a2fSth 			    gettext("Handling uninit option\n"),
598e1dd0a2fSth 			    stderr);
5997c478bd9Sstevel@tonic-gate 		retcode = client_uninit(optlist);
6007c478bd9Sstevel@tonic-gate 	}
6017c478bd9Sstevel@tonic-gate 
6027c478bd9Sstevel@tonic-gate /* handle "ldapclient init" (profile) */
6037c478bd9Sstevel@tonic-gate 	if (op_init) {
6047c478bd9Sstevel@tonic-gate 		if (mode_verbose)
6057c478bd9Sstevel@tonic-gate 			CLIENT_FPUTS(
606e1dd0a2fSth 			    gettext("Handling init option\n"),
607e1dd0a2fSth 			    stderr);
6087c478bd9Sstevel@tonic-gate 		retcode = client_init(optlist);
6097c478bd9Sstevel@tonic-gate 	}
6107c478bd9Sstevel@tonic-gate 
6117c478bd9Sstevel@tonic-gate /* handle "genprofile" here */
6127c478bd9Sstevel@tonic-gate 	if (op_genprofile) {
6137c478bd9Sstevel@tonic-gate 		if (mode_verbose)
6147c478bd9Sstevel@tonic-gate 			CLIENT_FPUTS(
615e1dd0a2fSth 			    gettext("Handling genProfile\n"),
616e1dd0a2fSth 			    stderr);
6177c478bd9Sstevel@tonic-gate 		retcode = client_genProfile(optlist);
6187c478bd9Sstevel@tonic-gate 	}
6197c478bd9Sstevel@tonic-gate 
6207c478bd9Sstevel@tonic-gate /* handle "ldapclient manual" here */
6217c478bd9Sstevel@tonic-gate 	if (op_manual) {
6227c478bd9Sstevel@tonic-gate 		if (mode_verbose)
6237c478bd9Sstevel@tonic-gate 			CLIENT_FPUTS(
624e1dd0a2fSth 			    gettext("Handling manual option\n"),
625e1dd0a2fSth 			    stderr);
6267c478bd9Sstevel@tonic-gate 		retcode = client_manual(optlist);
6277c478bd9Sstevel@tonic-gate 	}
6287c478bd9Sstevel@tonic-gate 
6297c478bd9Sstevel@tonic-gate /* handle "ldapclient mod" here */
6307c478bd9Sstevel@tonic-gate 	if (op_mod) {
6317c478bd9Sstevel@tonic-gate 		if (mode_verbose)
6327c478bd9Sstevel@tonic-gate 			CLIENT_FPUTS(
633e1dd0a2fSth 			    gettext("Handling mod option\n"),
634e1dd0a2fSth 			    stderr);
6357c478bd9Sstevel@tonic-gate 		retcode = client_mod(optlist);
6367c478bd9Sstevel@tonic-gate 	}
6377c478bd9Sstevel@tonic-gate 
6387c478bd9Sstevel@tonic-gate 	clientopts_free(optlist);
6397c478bd9Sstevel@tonic-gate 	if ((retcode == CLIENT_SUCCESS) ||
640e1dd0a2fSth 	    (retcode == CLIENT_ERR_FAIL) ||
641e1dd0a2fSth 	    (retcode == CLIENT_ERR_CREDENTIAL))
642cb5caa98Sdjl 		return (retcode);
6437c478bd9Sstevel@tonic-gate 	else
644cb5caa98Sdjl 		return (CLIENT_ERR_FAIL);
6457c478bd9Sstevel@tonic-gate }
6467c478bd9Sstevel@tonic-gate 
6477c478bd9Sstevel@tonic-gate static int
6487c478bd9Sstevel@tonic-gate client_list(clientopts_t *arglist)
6497c478bd9Sstevel@tonic-gate {
6507c478bd9Sstevel@tonic-gate 	ns_ldap_error_t *errorp;
6517c478bd9Sstevel@tonic-gate 	int retcode = CLIENT_SUCCESS;
6527c478bd9Sstevel@tonic-gate 
6537c478bd9Sstevel@tonic-gate 	if (num_args(arglist) > 0) {
6547c478bd9Sstevel@tonic-gate 		CLIENT_FPUTS(
655e1dd0a2fSth 		    gettext("No args supported with \"list\" option\n"),
656e1dd0a2fSth 		    stderr);
6577c478bd9Sstevel@tonic-gate 		usage();
6587c478bd9Sstevel@tonic-gate 		return (CLIENT_ERR_FAIL);	/* exit code here ? */
6597c478bd9Sstevel@tonic-gate 	}
6607c478bd9Sstevel@tonic-gate 	if ((errorp = __ns_ldap_print_config(mode_verbose)) != NULL) {
6617c478bd9Sstevel@tonic-gate 		retcode = CLIENT_ERR_FAIL;
6627c478bd9Sstevel@tonic-gate 		CLIENT_FPUTS(
663e1dd0a2fSth 		    gettext("Cannot get print configuration\n"),
664e1dd0a2fSth 		    stderr);
6657c478bd9Sstevel@tonic-gate 		CLIENT_FPUTS(errorp->message, stderr);
6667c478bd9Sstevel@tonic-gate 		(void) __ns_ldap_freeError(&errorp);
6677c478bd9Sstevel@tonic-gate 		CLIENT_FPUTC('\n', stderr);
6687c478bd9Sstevel@tonic-gate 	}
6697c478bd9Sstevel@tonic-gate 
6707c478bd9Sstevel@tonic-gate 	return (retcode);
6717c478bd9Sstevel@tonic-gate }
6727c478bd9Sstevel@tonic-gate 
6737c478bd9Sstevel@tonic-gate static int
6747c478bd9Sstevel@tonic-gate client_uninit(clientopts_t *arglist)
6757c478bd9Sstevel@tonic-gate {
6767c478bd9Sstevel@tonic-gate 	int retcode = CLIENT_SUCCESS;
677cb5caa98Sdjl 	ns_ldap_self_gssapi_config_t config = NS_LDAP_SELF_GSSAPI_CONFIG_NONE;
6787c478bd9Sstevel@tonic-gate 
6797c478bd9Sstevel@tonic-gate 	if (mode_verbose) {
6807c478bd9Sstevel@tonic-gate 		CLIENT_FPUTS(
681e1dd0a2fSth 		    gettext("Restoring machine to previous "
682e1dd0a2fSth 		    "configuration state\n"),
683e1dd0a2fSth 		    stderr);
6847c478bd9Sstevel@tonic-gate 	}
6857c478bd9Sstevel@tonic-gate 
6867c478bd9Sstevel@tonic-gate 	if (num_args(arglist) > 0) {
6877c478bd9Sstevel@tonic-gate 		CLIENT_FPUTS(
688e1dd0a2fSth 		    gettext("No args supported with \"uninit\" option\n"),
689e1dd0a2fSth 		    stderr);
6907c478bd9Sstevel@tonic-gate 		usage();
6917c478bd9Sstevel@tonic-gate 		return (CLIENT_ERR_FAIL);
6927c478bd9Sstevel@tonic-gate 	}
6937c478bd9Sstevel@tonic-gate 
694cb5caa98Sdjl 	(void) __ns_ldap_self_gssapi_config(&config);
695cb5caa98Sdjl 
6967c478bd9Sstevel@tonic-gate 	retcode = stop_services(STATE_SAVE);
697cb5caa98Sdjl 
698cb5caa98Sdjl 	if (config != NS_LDAP_SELF_GSSAPI_CONFIG_NONE)
699cb5caa98Sdjl 		(void) system("/usr/sbin/cryptoadm enable metaslot");
700cb5caa98Sdjl 
7017c478bd9Sstevel@tonic-gate 	if (retcode != CLIENT_SUCCESS) {
7027c478bd9Sstevel@tonic-gate 		CLIENT_FPUTS(
703e1dd0a2fSth 		    gettext("Errors stopping network services.\n"), stderr);
7047c478bd9Sstevel@tonic-gate 		/* restart whatever services we can */
7057c478bd9Sstevel@tonic-gate 		(void) start_services(START_RESET);
7067c478bd9Sstevel@tonic-gate 		return (CLIENT_ERR_FAIL);
7077c478bd9Sstevel@tonic-gate 	}
7087c478bd9Sstevel@tonic-gate 
7097c478bd9Sstevel@tonic-gate 	retcode = recover(STATE_SAVE);
7107c478bd9Sstevel@tonic-gate 	if (retcode != CLIENT_SUCCESS) {
7117c478bd9Sstevel@tonic-gate 		CLIENT_FPUTS(
712e1dd0a2fSth 		    gettext("Cannot recover the configuration on "
713e1dd0a2fSth 		    "this machine.\n"),
714e1dd0a2fSth 		    stderr);
7157c478bd9Sstevel@tonic-gate 		(void) start_services(START_RESET);
7167c478bd9Sstevel@tonic-gate 	} else {
7177c478bd9Sstevel@tonic-gate 		retcode = start_services(START_UNINIT);
7187c478bd9Sstevel@tonic-gate 		if (retcode != CLIENT_SUCCESS) {
7197c478bd9Sstevel@tonic-gate 			CLIENT_FPUTS(
720e1dd0a2fSth 			    gettext("Config restored but problems "
721e1dd0a2fSth 			    "encountered resetting network "
722e1dd0a2fSth 			    "services.\n"),
723e1dd0a2fSth 			    stderr);
7247c478bd9Sstevel@tonic-gate 		}
7257c478bd9Sstevel@tonic-gate 	}
7267c478bd9Sstevel@tonic-gate 
7277c478bd9Sstevel@tonic-gate 	if (retcode == CLIENT_SUCCESS) {
7287c478bd9Sstevel@tonic-gate 		CLIENT_FPUTS(
729e1dd0a2fSth 		    gettext("System successfully recovered\n"),
730e1dd0a2fSth 		    stderr);
7317c478bd9Sstevel@tonic-gate 	}
7327c478bd9Sstevel@tonic-gate 
7337c478bd9Sstevel@tonic-gate 	return (retcode);
7347c478bd9Sstevel@tonic-gate }
7357c478bd9Sstevel@tonic-gate 
7367c478bd9Sstevel@tonic-gate /*
7377c478bd9Sstevel@tonic-gate  * The following macro is used to do a __ns_ldap_setParam().
7387c478bd9Sstevel@tonic-gate  * On every call, the return code is checked, and if there was
7397c478bd9Sstevel@tonic-gate  * a problem then the error message is printed, the ldaperr
7407c478bd9Sstevel@tonic-gate  * is freed and we return from the function with the offending
7417c478bd9Sstevel@tonic-gate  * error return code.  This macro keeps us from having to
7427c478bd9Sstevel@tonic-gate  * repeat this code for every call to setParam as was done
7437c478bd9Sstevel@tonic-gate  * in the previous incarnation of ldapclient.
7447c478bd9Sstevel@tonic-gate  *
7457c478bd9Sstevel@tonic-gate  * assumes a "retcode" variable is available for status
7467c478bd9Sstevel@tonic-gate  */
7477c478bd9Sstevel@tonic-gate #define	LDAP_SET_PARAM(argval, argdef)	\
7487c478bd9Sstevel@tonic-gate retcode = 0;	\
7497c478bd9Sstevel@tonic-gate if (NULL != argval) {	\
7507c478bd9Sstevel@tonic-gate 	ns_ldap_error_t *ldaperr;	\
7517c478bd9Sstevel@tonic-gate 	retcode = __ns_ldap_setParam(argdef, (void *)argval, &ldaperr);	\
7527c478bd9Sstevel@tonic-gate 	if (retcode != NS_LDAP_SUCCESS) {	\
7537c478bd9Sstevel@tonic-gate 		if (NULL != ldaperr) {	\
7547c478bd9Sstevel@tonic-gate 			CLIENT_FPUTS(ldaperr->message, stderr);	\
7557c478bd9Sstevel@tonic-gate 			CLIENT_FPUTC('\n', stderr);	\
7567c478bd9Sstevel@tonic-gate 			(void) __ns_ldap_freeError(&ldaperr);	\
7577c478bd9Sstevel@tonic-gate 		}	\
7587c478bd9Sstevel@tonic-gate 		return (retcode ? CLIENT_ERR_FAIL : CLIENT_SUCCESS);	\
7597c478bd9Sstevel@tonic-gate 	}	\
7607c478bd9Sstevel@tonic-gate }
7617c478bd9Sstevel@tonic-gate 
762e1dd0a2fSth /*
763e1dd0a2fSth  * The following macro is used to check if an arg has already been set
764e1dd0a2fSth  * and issues an error message, a usage message and then returns an error.
765e1dd0a2fSth  * This was made into a macro to avoid the duplication of this code many
766e1dd0a2fSth  * times in the function below.
767e1dd0a2fSth  */
768e1dd0a2fSth #define	LDAP_CHECK_INVALID(arg, param)	\
769e1dd0a2fSth if (arg) {	\
770e1dd0a2fSth 	CLIENT_FPRINTF(stderr, gettext("Invalid parameter (%s) " \
771e1dd0a2fSth 	    "specified\n"), param);	\
772e1dd0a2fSth 	usage();	\
773e1dd0a2fSth 	return (CLIENT_ERR_FAIL);	\
774e1dd0a2fSth }
775e1dd0a2fSth 
7767c478bd9Sstevel@tonic-gate static int
7777c478bd9Sstevel@tonic-gate client_manual(clientopts_t *arglist)
7787c478bd9Sstevel@tonic-gate {
7797c478bd9Sstevel@tonic-gate 	int counter;
7807c478bd9Sstevel@tonic-gate 	int domain_fp;
7817c478bd9Sstevel@tonic-gate 	ns_ldap_error_t *errorp;
7827c478bd9Sstevel@tonic-gate 	int ret_copy;
7837c478bd9Sstevel@tonic-gate 	int reset_ret;
7847c478bd9Sstevel@tonic-gate 	int retcode = CLIENT_SUCCESS;
7857c478bd9Sstevel@tonic-gate 
7867c478bd9Sstevel@tonic-gate 	if (dname == NULL) {
7877c478bd9Sstevel@tonic-gate 		CLIENT_FPUTS(
788e1dd0a2fSth 		    gettext("Manual failed: System domain not set and "
789e1dd0a2fSth 		    "no domainName specified.\n"),
790e1dd0a2fSth 		    stderr);
7917c478bd9Sstevel@tonic-gate 		return (CLIENT_ERR_FAIL);
7927c478bd9Sstevel@tonic-gate 	}
7937c478bd9Sstevel@tonic-gate 
7947c478bd9Sstevel@tonic-gate 	if (arglist->defaultSearchBase == NULL) {
7957c478bd9Sstevel@tonic-gate 		CLIENT_FPUTS(
796e1dd0a2fSth 		    gettext("Manual failed: Missing required "
797e1dd0a2fSth 		    "defaultSearchBase attribute.\n"),
798e1dd0a2fSth 		    stderr);
7997c478bd9Sstevel@tonic-gate 		return (CLIENT_ERR_FAIL);
8007c478bd9Sstevel@tonic-gate 	}
8017c478bd9Sstevel@tonic-gate 
8027c478bd9Sstevel@tonic-gate 	if ((arglist->defaultServerList == NULL) &&
803e1dd0a2fSth 	    (arglist->preferredServerList == NULL)) {
8047c478bd9Sstevel@tonic-gate 		CLIENT_FPUTS(
805e1dd0a2fSth 		    gettext("Manual failed: Missing required "
806e1dd0a2fSth 		    "defaultServerList or preferredServerList "
807e1dd0a2fSth 		    "attribute.\n"),
808e1dd0a2fSth 		    stderr);
8097c478bd9Sstevel@tonic-gate 		return (CLIENT_ERR_FAIL);
8107c478bd9Sstevel@tonic-gate 	}
8117c478bd9Sstevel@tonic-gate 
8127c478bd9Sstevel@tonic-gate 	if (arglist->profileTTL != NULL) {
8137c478bd9Sstevel@tonic-gate 		CLIENT_FPUTS(
814e1dd0a2fSth 		    gettext("Manual aborted: profileTTL is not supported "
815e1dd0a2fSth 		    "in manual mode.\n"),
816e1dd0a2fSth 		    stderr);
8177c478bd9Sstevel@tonic-gate 		return (CLIENT_ERR_FAIL);
8187c478bd9Sstevel@tonic-gate 	}
8197c478bd9Sstevel@tonic-gate 
8207c478bd9Sstevel@tonic-gate 	if (arglist->profileName != NULL) {
8217c478bd9Sstevel@tonic-gate 		CLIENT_FPUTS(
822e1dd0a2fSth 		    gettext("Manual aborted: profileName is not supported "
823e1dd0a2fSth 		    "in manual mode.\n"),
824e1dd0a2fSth 		    stderr);
8257c478bd9Sstevel@tonic-gate 		return (CLIENT_ERR_FAIL);
8267c478bd9Sstevel@tonic-gate 	}
8277c478bd9Sstevel@tonic-gate 
828e1dd0a2fSth 	LDAP_CHECK_INVALID(arglist->bindDN, "bind DN");
829e1dd0a2fSth 	LDAP_CHECK_INVALID(arglist->bindPasswd, "bind password");
8307c478bd9Sstevel@tonic-gate 
8317c478bd9Sstevel@tonic-gate 	__ns_ldap_setServer(TRUE);	/* Need this for _ns_setParam() */
8327c478bd9Sstevel@tonic-gate 	__ns_ldap_default_config();
8337c478bd9Sstevel@tonic-gate 
8347c478bd9Sstevel@tonic-gate 	/* Set version to latest (not version 1) */
8357c478bd9Sstevel@tonic-gate 	LDAP_SET_PARAM(NS_LDAP_VERSION, NS_LDAP_FILE_VERSION_P);
8367c478bd9Sstevel@tonic-gate 
8377c478bd9Sstevel@tonic-gate 	/* Set profileTTL to 0 since NO profile on manual */
8387c478bd9Sstevel@tonic-gate 	LDAP_SET_PARAM(CACHETTL_OFF, NS_LDAP_CACHETTL_P);
8397c478bd9Sstevel@tonic-gate 
8407c478bd9Sstevel@tonic-gate 	/* Set additional valid params from command line */
8417c478bd9Sstevel@tonic-gate 	LDAP_SET_PARAM(arglist->authenticationMethod, NS_LDAP_AUTH_P);
8427c478bd9Sstevel@tonic-gate 	LDAP_SET_PARAM(arglist->defaultSearchBase, NS_LDAP_SEARCH_BASEDN_P);
8437c478bd9Sstevel@tonic-gate 	LDAP_SET_PARAM(arglist->credentialLevel, NS_LDAP_CREDENTIAL_LEVEL_P);
8447c478bd9Sstevel@tonic-gate 	LDAP_SET_PARAM(arglist->proxyDN, NS_LDAP_BINDDN_P);
845dd1104fbSMichen Chang 	LDAP_SET_PARAM(arglist->enableShadowUpdate,
846dd1104fbSMichen Chang 	    NS_LDAP_ENABLE_SHADOW_UPDATE_P);
847dd1104fbSMichen Chang 	LDAP_SET_PARAM(arglist->adminDN, NS_LDAP_ADMIN_BINDDN_P);
8487c478bd9Sstevel@tonic-gate 	LDAP_SET_PARAM(arglist->searchTimeLimit, NS_LDAP_SEARCH_TIME_P);
8497c478bd9Sstevel@tonic-gate 	LDAP_SET_PARAM(arglist->preferredServerList, NS_LDAP_SERVER_PREF_P);
8507c478bd9Sstevel@tonic-gate 	LDAP_SET_PARAM(arglist->profileName, NS_LDAP_PROFILE_P);
8517c478bd9Sstevel@tonic-gate 	LDAP_SET_PARAM(arglist->followReferrals, NS_LDAP_SEARCH_REF_P);
8527c478bd9Sstevel@tonic-gate 	LDAP_SET_PARAM(arglist->defaultSearchScope, NS_LDAP_SEARCH_SCOPE_P);
8537c478bd9Sstevel@tonic-gate 	LDAP_SET_PARAM(arglist->bindTimeLimit, NS_LDAP_BIND_TIME_P);
8547c478bd9Sstevel@tonic-gate 	LDAP_SET_PARAM(arglist->proxyPassword, NS_LDAP_BINDPASSWD_P);
855dd1104fbSMichen Chang 	LDAP_SET_PARAM(arglist->adminPassword, NS_LDAP_ADMIN_BINDPASSWD_P);
8567c478bd9Sstevel@tonic-gate 	LDAP_SET_PARAM(arglist->defaultServerList, NS_LDAP_SERVERS_P);
8577c478bd9Sstevel@tonic-gate 	LDAP_SET_PARAM(arglist->certificatePath, NS_LDAP_HOST_CERTPATH_P);
8587c478bd9Sstevel@tonic-gate 
8597c478bd9Sstevel@tonic-gate 	for (counter = 0;
860e1dd0a2fSth 	    counter < arglist->serviceAuthenticationMethod->count;
861e1dd0a2fSth 	    counter++) {
8627c478bd9Sstevel@tonic-gate 
8637c478bd9Sstevel@tonic-gate 		LDAP_SET_PARAM(
864e1dd0a2fSth 		    arglist->serviceAuthenticationMethod->optlist[counter],
865e1dd0a2fSth 		    NS_LDAP_SERVICE_AUTH_METHOD_P);
8667c478bd9Sstevel@tonic-gate 	}
8677c478bd9Sstevel@tonic-gate 	for (counter = 0;
868e1dd0a2fSth 	    counter < arglist->serviceCredentialLevel->count;
869e1dd0a2fSth 	    counter++) {
8707c478bd9Sstevel@tonic-gate 
8717c478bd9Sstevel@tonic-gate 		LDAP_SET_PARAM(
872e1dd0a2fSth 		    arglist->serviceCredentialLevel->optlist[counter],
873e1dd0a2fSth 		    NS_LDAP_SERVICE_CRED_LEVEL_P);
8747c478bd9Sstevel@tonic-gate 	}
8757c478bd9Sstevel@tonic-gate 	for (counter = 0;
876e1dd0a2fSth 	    counter < arglist->objectclassMap->count;
877e1dd0a2fSth 	    counter++) {
8787c478bd9Sstevel@tonic-gate 
8797c478bd9Sstevel@tonic-gate 		LDAP_SET_PARAM(arglist->objectclassMap->optlist[counter],
880e1dd0a2fSth 		    NS_LDAP_OBJECTCLASSMAP_P);
8817c478bd9Sstevel@tonic-gate 	}
8827c478bd9Sstevel@tonic-gate 	for (counter = 0; counter < arglist->attributeMap->count; counter++) {
8837c478bd9Sstevel@tonic-gate 		LDAP_SET_PARAM(arglist->attributeMap->optlist[counter],
884e1dd0a2fSth 		    NS_LDAP_ATTRIBUTEMAP_P);
8857c478bd9Sstevel@tonic-gate 	}
8867c478bd9Sstevel@tonic-gate 	for (counter = 0;
887e1dd0a2fSth 	    counter < arglist->serviceSearchDescriptor->count;
888e1dd0a2fSth 	    counter++) {
8897c478bd9Sstevel@tonic-gate 
8907c478bd9Sstevel@tonic-gate 		LDAP_SET_PARAM(
891e1dd0a2fSth 		    arglist->serviceSearchDescriptor->optlist[counter],
892e1dd0a2fSth 		    NS_LDAP_SERVICE_SEARCH_DESC_P);
8937c478bd9Sstevel@tonic-gate 	}
8947c478bd9Sstevel@tonic-gate 
8957c478bd9Sstevel@tonic-gate 	retcode = credCheck(arglist);
896dd1104fbSMichen Chang 	if (retcode == CLIENT_SUCCESS)
897dd1104fbSMichen Chang 		retcode = adminCredCheck(arglist);
8987c478bd9Sstevel@tonic-gate 	if (retcode != CLIENT_SUCCESS) {
8997c478bd9Sstevel@tonic-gate 		CLIENT_FPUTS(
900e1dd0a2fSth 		    gettext("Error in setting up credentials\n"),
901e1dd0a2fSth 		    stderr);
9027c478bd9Sstevel@tonic-gate 		return (retcode);
9037c478bd9Sstevel@tonic-gate 	}
9047c478bd9Sstevel@tonic-gate 
9057c478bd9Sstevel@tonic-gate 	if (mode_verbose)
9067c478bd9Sstevel@tonic-gate 		CLIENT_FPUTS(
907e1dd0a2fSth 		    gettext("About to modify this machines "
908e1dd0a2fSth 		    "configuration by writing the files\n"),
909e1dd0a2fSth 		    stderr);
9107c478bd9Sstevel@tonic-gate 
9117c478bd9Sstevel@tonic-gate 	/* get ready to start playing with files */
9127c478bd9Sstevel@tonic-gate 	retcode = stop_services(STATE_SAVE);
9137c478bd9Sstevel@tonic-gate 	if (retcode != CLIENT_SUCCESS) {
9147c478bd9Sstevel@tonic-gate 		CLIENT_FPUTS(
915e1dd0a2fSth 		    gettext("Errors stopping network services.\n"), stderr);
9167c478bd9Sstevel@tonic-gate 		return (CLIENT_ERR_FAIL);
9177c478bd9Sstevel@tonic-gate 	}
9187c478bd9Sstevel@tonic-gate 
9197c478bd9Sstevel@tonic-gate 	/* Save orig versions of files */
9207c478bd9Sstevel@tonic-gate 	retcode = file_backup();
9217c478bd9Sstevel@tonic-gate 	if (retcode == CLIENT_ERR_RESTORE) {
9227c478bd9Sstevel@tonic-gate 		CLIENT_FPUTS(
923e1dd0a2fSth 		    gettext("System not in state to enable ldap client.\n"),
924e1dd0a2fSth 		    stderr);
9257c478bd9Sstevel@tonic-gate 
9267c478bd9Sstevel@tonic-gate 		reset_ret = start_services(START_RESET);
9277c478bd9Sstevel@tonic-gate 		if (reset_ret != CLIENT_SUCCESS) {
9287c478bd9Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr, gettext("Error (%d) while "
929e1dd0a2fSth 			    "starting services during reset\n"),
930e1dd0a2fSth 			    reset_ret);
9317c478bd9Sstevel@tonic-gate 		}
9327c478bd9Sstevel@tonic-gate 		return (retcode);
9337c478bd9Sstevel@tonic-gate 	} else if (retcode != CLIENT_SUCCESS) {
9347c478bd9Sstevel@tonic-gate 		CLIENT_FPUTS(
935e1dd0a2fSth 		    gettext("Save of system configuration failed!  "
936e1dd0a2fSth 		    "Attempting recovery.\n"),
937e1dd0a2fSth 		    stderr);
9387c478bd9Sstevel@tonic-gate 		retcode = recover(STATE_NOSAVE);
9397c478bd9Sstevel@tonic-gate 		if (retcode != CLIENT_SUCCESS) {
9407c478bd9Sstevel@tonic-gate 			CLIENT_FPUTS(
941e1dd0a2fSth 			    gettext("Recovery of systems configuration "
942e1dd0a2fSth 			    "failed.  Manual intervention of "
943e1dd0a2fSth 			    "config files is required.\n"),
944e1dd0a2fSth 			    stderr);
9457c478bd9Sstevel@tonic-gate 			return (retcode);
9467c478bd9Sstevel@tonic-gate 		}
9477c478bd9Sstevel@tonic-gate 
9487c478bd9Sstevel@tonic-gate 		reset_ret = start_services(START_RESET);
9497c478bd9Sstevel@tonic-gate 		if (reset_ret != CLIENT_SUCCESS) {
9507c478bd9Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr, gettext("Error (%d) while "
951e1dd0a2fSth 			    "starting services during reset\n"),
952e1dd0a2fSth 			    reset_ret);
9537c478bd9Sstevel@tonic-gate 		}
9547c478bd9Sstevel@tonic-gate 
9557c478bd9Sstevel@tonic-gate 		return (retcode);
9567c478bd9Sstevel@tonic-gate 	}
9577c478bd9Sstevel@tonic-gate 
9587c478bd9Sstevel@tonic-gate 	/* Dump new files */
9597c478bd9Sstevel@tonic-gate 	errorp = __ns_ldap_DumpConfiguration(NSCONFIGFILE);
9607c478bd9Sstevel@tonic-gate 	if (errorp != NULL) {
9617c478bd9Sstevel@tonic-gate 		CLIENT_FPRINTF(stderr,
962e1dd0a2fSth 		    gettext("%s manual: errorp is not NULL; %s\n"),
963e1dd0a2fSth 		    cmd, errorp->message);
9647c478bd9Sstevel@tonic-gate 		retcode = recover(STATE_NOSAVE);
9657c478bd9Sstevel@tonic-gate 		if (retcode != CLIENT_SUCCESS) {
9667c478bd9Sstevel@tonic-gate 			CLIENT_FPUTS(
967e1dd0a2fSth 			    gettext("Recovery of systems configuration "
968e1dd0a2fSth 			    "failed.  Manual intervention of "
969e1dd0a2fSth 			    "config files is required.\n"),
970e1dd0a2fSth 			    stderr);
9717c478bd9Sstevel@tonic-gate 			return (retcode);
9727c478bd9Sstevel@tonic-gate 		}
9737c478bd9Sstevel@tonic-gate 		reset_ret = start_services(START_RESET);
9747c478bd9Sstevel@tonic-gate 		if (reset_ret != CLIENT_SUCCESS) {
9757c478bd9Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr, gettext("Error (%d) while "
976e1dd0a2fSth 			    "starting services during reset\n"),
977e1dd0a2fSth 			    reset_ret);
9787c478bd9Sstevel@tonic-gate 		}
9797c478bd9Sstevel@tonic-gate 		(void) __ns_ldap_freeError(&errorp);
9807c478bd9Sstevel@tonic-gate 		return (CLIENT_ERR_FAIL);
9817c478bd9Sstevel@tonic-gate 	}
9827c478bd9Sstevel@tonic-gate 
9837c478bd9Sstevel@tonic-gate 	/* if (credargs(arglist)) */
9847c478bd9Sstevel@tonic-gate 	errorp = __ns_ldap_DumpConfiguration(NSCREDFILE);
9857c478bd9Sstevel@tonic-gate 	if (errorp != NULL) {
9867c478bd9Sstevel@tonic-gate 		CLIENT_FPRINTF(stderr,
987e1dd0a2fSth 		    gettext("%s init: errorp is not NULL; %s\n"),
988e1dd0a2fSth 		    cmd, errorp->message);
9897c478bd9Sstevel@tonic-gate 		retcode = recover(STATE_NOSAVE);
9907c478bd9Sstevel@tonic-gate 		if (retcode != CLIENT_SUCCESS) {
9917c478bd9Sstevel@tonic-gate 			CLIENT_FPUTS(
992e1dd0a2fSth 			    gettext("Recovery of systems configuration "
993e1dd0a2fSth 			    "failed.  Manual intervention of "
994e1dd0a2fSth 			    "config files is required.\n"),
995e1dd0a2fSth 			    stderr);
9967c478bd9Sstevel@tonic-gate 			return (retcode);
9977c478bd9Sstevel@tonic-gate 		}
9987c478bd9Sstevel@tonic-gate 		reset_ret = start_services(START_RESET);
9997c478bd9Sstevel@tonic-gate 		if (reset_ret != CLIENT_SUCCESS) {
10007c478bd9Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr, gettext("Error (%d) while "
1001e1dd0a2fSth 			    "starting services during reset\n"),
1002e1dd0a2fSth 			    reset_ret);
10037c478bd9Sstevel@tonic-gate 		}
10047c478bd9Sstevel@tonic-gate 		(void) __ns_ldap_freeError(&errorp);
10057c478bd9Sstevel@tonic-gate 		return (CLIENT_ERR_FAIL);
10067c478bd9Sstevel@tonic-gate 	}
10077c478bd9Sstevel@tonic-gate 
10087c478bd9Sstevel@tonic-gate 	ret_copy = system(CMD_CP " " NSSWITCH_LDAP " " NSSWITCH_CONF);
10097c478bd9Sstevel@tonic-gate 	if (ret_copy != 0) {
10107c478bd9Sstevel@tonic-gate 		CLIENT_FPRINTF(stderr,
1011e1dd0a2fSth 		    gettext("Error %d copying (%s) -> (%s)\n"),
1012e1dd0a2fSth 		    ret_copy, NSSWITCH_LDAP, NSSWITCH_CONF);
10137c478bd9Sstevel@tonic-gate 		retcode = recover(STATE_NOSAVE);
10147c478bd9Sstevel@tonic-gate 		if (retcode != CLIENT_SUCCESS) {
10157c478bd9Sstevel@tonic-gate 			CLIENT_FPUTS(
1016e1dd0a2fSth 			    gettext("Recovery of systems configuration "
1017e1dd0a2fSth 			    "failed.  Manual intervention of "
1018e1dd0a2fSth 			    "config files is required.\n"),
1019e1dd0a2fSth 			    stderr);
10207c478bd9Sstevel@tonic-gate 		}
10217c478bd9Sstevel@tonic-gate 		reset_ret = start_services(START_RESET);
10227c478bd9Sstevel@tonic-gate 		if (reset_ret != CLIENT_SUCCESS) {
10237c478bd9Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr, gettext("Error (%d) while "
1024e1dd0a2fSth 			    "starting services during reset\n"),
1025e1dd0a2fSth 			    reset_ret);
10267c478bd9Sstevel@tonic-gate 		}
10277c478bd9Sstevel@tonic-gate 		return (CLIENT_ERR_FAIL);
10287c478bd9Sstevel@tonic-gate 	}
10297c478bd9Sstevel@tonic-gate 
10307c478bd9Sstevel@tonic-gate 	if ((domain_fp = open(DOMAINNAME, O_WRONLY|O_CREAT|O_TRUNC,
1031e1dd0a2fSth 	    S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) == -1) { /* 0644 */
10327c478bd9Sstevel@tonic-gate 		CLIENT_FPRINTF(stderr, gettext("Cannot open %s\n"), DOMAINNAME);
10337c478bd9Sstevel@tonic-gate 		retcode = recover(STATE_NOSAVE);
10347c478bd9Sstevel@tonic-gate 		if (retcode != CLIENT_SUCCESS) {
10357c478bd9Sstevel@tonic-gate 			CLIENT_FPUTS(
1036e1dd0a2fSth 			    gettext("Recovery of systems configuration "
1037e1dd0a2fSth 			    "failed.  Manual intervention of "
1038e1dd0a2fSth 			    "config files is required.\n"),
1039e1dd0a2fSth 			    stderr);
10407c478bd9Sstevel@tonic-gate 			return (CLIENT_ERR_FAIL);
10417c478bd9Sstevel@tonic-gate 		}
10427c478bd9Sstevel@tonic-gate 		reset_ret = start_services(START_RESET);
10437c478bd9Sstevel@tonic-gate 		if (reset_ret != CLIENT_SUCCESS) {
10447c478bd9Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr, gettext("Error (%d) while "
1045e1dd0a2fSth 			    "starting services during reset\n"),
1046e1dd0a2fSth 			    reset_ret);
10477c478bd9Sstevel@tonic-gate 		}
10487c478bd9Sstevel@tonic-gate 		return (CLIENT_ERR_FAIL);
10497c478bd9Sstevel@tonic-gate 	}
10507c478bd9Sstevel@tonic-gate 	(void) write(domain_fp, dname, strlen(dname));
10517c478bd9Sstevel@tonic-gate 	(void) write(domain_fp, "\n", 1);
10527c478bd9Sstevel@tonic-gate 	(void) close(domain_fp);
10537c478bd9Sstevel@tonic-gate 
10547c478bd9Sstevel@tonic-gate 	retcode = start_services(START_INIT);
10557c478bd9Sstevel@tonic-gate 
10567c478bd9Sstevel@tonic-gate 	if (retcode == CLIENT_SUCCESS) {
10577c478bd9Sstevel@tonic-gate 		CLIENT_FPUTS(gettext("System successfully configured\n"),
1058e1dd0a2fSth 		    stderr);
10597c478bd9Sstevel@tonic-gate 	} else {
10607c478bd9Sstevel@tonic-gate 		CLIENT_FPUTS(gettext("Error resetting system.\n"
1061e1dd0a2fSth 		    "Recovering old system settings.\n"), stderr),
10627c478bd9Sstevel@tonic-gate 
1063e1dd0a2fSth 		    /* stop any started services for recover */
1064e1dd0a2fSth 		    /* don't stomp on history of saved services state */
1065e1dd0a2fSth 		    reset_ret = stop_services(STATE_NOSAVE);
10667c478bd9Sstevel@tonic-gate 		if (reset_ret != CLIENT_SUCCESS) {
10677c478bd9Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr, gettext("Error (%d) while "
1068e1dd0a2fSth 			    "stopping services during reset\n"),
1069e1dd0a2fSth 			    reset_ret);
10707c478bd9Sstevel@tonic-gate 			/* Coninue and try to recover what we can */
10717c478bd9Sstevel@tonic-gate 		}
10727c478bd9Sstevel@tonic-gate 		reset_ret = recover(STATE_NOSAVE);
10737c478bd9Sstevel@tonic-gate 		if (reset_ret != CLIENT_SUCCESS) {
10747c478bd9Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr, gettext("Error (%d) while "
1075e1dd0a2fSth 			    "recovering service files during "
1076e1dd0a2fSth 			    "reset\n"), reset_ret);
10777c478bd9Sstevel@tonic-gate 			/* Continue and start what we can */
10787c478bd9Sstevel@tonic-gate 		}
10797c478bd9Sstevel@tonic-gate 		reset_ret = start_services(START_RESET);
10807c478bd9Sstevel@tonic-gate 		if (reset_ret != CLIENT_SUCCESS) {
10817c478bd9Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr, gettext("Error (%d) while "
1082e1dd0a2fSth 			    "starting services during reset\n"),
1083e1dd0a2fSth 			    reset_ret);
10847c478bd9Sstevel@tonic-gate 		}
10857c478bd9Sstevel@tonic-gate 	}
10867c478bd9Sstevel@tonic-gate 
10877c478bd9Sstevel@tonic-gate 	return (retcode);
10887c478bd9Sstevel@tonic-gate }
10897c478bd9Sstevel@tonic-gate 
10907c478bd9Sstevel@tonic-gate static int
10917c478bd9Sstevel@tonic-gate client_mod(clientopts_t *arglist)
10927c478bd9Sstevel@tonic-gate {
10937c478bd9Sstevel@tonic-gate 	int counter;
10947c478bd9Sstevel@tonic-gate 	int domain_fp;
10957c478bd9Sstevel@tonic-gate 	ns_ldap_error_t *errorp;
10967c478bd9Sstevel@tonic-gate 	int reset_ret;
10977c478bd9Sstevel@tonic-gate 	int retcode = CLIENT_SUCCESS;
10987c478bd9Sstevel@tonic-gate 
10997c478bd9Sstevel@tonic-gate 	__ns_ldap_setServer(TRUE);	/* Need this for _ns_setParam() */
11007c478bd9Sstevel@tonic-gate 	if ((errorp = __ns_ldap_LoadConfiguration()) != NULL) {
11017c478bd9Sstevel@tonic-gate 		CLIENT_FPUTS(gettext("Cannot get load configuration\n"),
1102e1dd0a2fSth 		    stderr);
11037c478bd9Sstevel@tonic-gate 		CLIENT_FPUTS(errorp->message, stderr);
11047c478bd9Sstevel@tonic-gate 		CLIENT_FPUTC('\n', stderr);
11057c478bd9Sstevel@tonic-gate 		(void) __ns_ldap_freeError(&errorp);
11067c478bd9Sstevel@tonic-gate 		return (CLIENT_ERR_FAIL);
11077c478bd9Sstevel@tonic-gate 	}
11087c478bd9Sstevel@tonic-gate 
11097c478bd9Sstevel@tonic-gate 	if (arglist->profileTTL != NULL) {
11107c478bd9Sstevel@tonic-gate 		CLIENT_FPUTS(
1111e1dd0a2fSth 		    gettext("Mod aborted: profileTTL modification is "
1112e1dd0a2fSth 		    "not allowed in mod mode.\n"),
1113e1dd0a2fSth 		    stderr);
11147c478bd9Sstevel@tonic-gate 		return (CLIENT_ERR_FAIL);
11157c478bd9Sstevel@tonic-gate 	}
11167c478bd9Sstevel@tonic-gate 
11177c478bd9Sstevel@tonic-gate 	if (arglist->profileName != NULL) {
11187c478bd9Sstevel@tonic-gate 		CLIENT_FPUTS(
1119e1dd0a2fSth 		    gettext("Mod aborted: profileName modification is "
1120e1dd0a2fSth 		    "not allowed.  If you want to use profiles "
1121e1dd0a2fSth 		    "generate one with genProfile and load it "
1122e1dd0a2fSth 		    "on the server with ldapadd.\n"),
1123e1dd0a2fSth 		    stderr);
11247c478bd9Sstevel@tonic-gate 		return (CLIENT_ERR_FAIL);
11257c478bd9Sstevel@tonic-gate 	}
11267c478bd9Sstevel@tonic-gate 
1127e1dd0a2fSth 	LDAP_CHECK_INVALID(arglist->bindDN, "bind DN");
1128e1dd0a2fSth 	LDAP_CHECK_INVALID(arglist->bindPasswd, "bind password");
11297c478bd9Sstevel@tonic-gate 
11307c478bd9Sstevel@tonic-gate 	/* Set additional valid params from command line */
11317c478bd9Sstevel@tonic-gate 	LDAP_SET_PARAM(arglist->authenticationMethod, NS_LDAP_AUTH_P);
11327c478bd9Sstevel@tonic-gate 	LDAP_SET_PARAM(arglist->defaultSearchBase, NS_LDAP_SEARCH_BASEDN_P);
11337c478bd9Sstevel@tonic-gate 	LDAP_SET_PARAM(arglist->credentialLevel, NS_LDAP_CREDENTIAL_LEVEL_P);
11347c478bd9Sstevel@tonic-gate 	LDAP_SET_PARAM(arglist->proxyDN, NS_LDAP_BINDDN_P);
1135dd1104fbSMichen Chang 	LDAP_SET_PARAM(arglist->adminDN, NS_LDAP_ADMIN_BINDDN_P);
11367c478bd9Sstevel@tonic-gate 	LDAP_SET_PARAM(arglist->profileTTL, NS_LDAP_CACHETTL_P);
11377c478bd9Sstevel@tonic-gate 	LDAP_SET_PARAM(arglist->searchTimeLimit, NS_LDAP_SEARCH_TIME_P);
11387c478bd9Sstevel@tonic-gate 	LDAP_SET_PARAM(arglist->preferredServerList, NS_LDAP_SERVER_PREF_P);
11397c478bd9Sstevel@tonic-gate 	LDAP_SET_PARAM(arglist->profileName, NS_LDAP_PROFILE_P);
11407c478bd9Sstevel@tonic-gate 	LDAP_SET_PARAM(arglist->followReferrals, NS_LDAP_SEARCH_REF_P);
11417c478bd9Sstevel@tonic-gate 	LDAP_SET_PARAM(arglist->defaultSearchScope, NS_LDAP_SEARCH_SCOPE_P);
11427c478bd9Sstevel@tonic-gate 	LDAP_SET_PARAM(arglist->bindTimeLimit, NS_LDAP_BIND_TIME_P);
11437c478bd9Sstevel@tonic-gate 	LDAP_SET_PARAM(arglist->proxyPassword, NS_LDAP_BINDPASSWD_P);
1144dd1104fbSMichen Chang 	LDAP_SET_PARAM(arglist->adminPassword, NS_LDAP_ADMIN_BINDPASSWD_P);
11457c478bd9Sstevel@tonic-gate 	LDAP_SET_PARAM(arglist->defaultServerList, NS_LDAP_SERVERS_P);
1146dd1104fbSMichen Chang 	LDAP_SET_PARAM(arglist->enableShadowUpdate,
1147dd1104fbSMichen Chang 	    NS_LDAP_ENABLE_SHADOW_UPDATE_P);
11487c478bd9Sstevel@tonic-gate 	LDAP_SET_PARAM(arglist->certificatePath, NS_LDAP_HOST_CERTPATH_P);
11497c478bd9Sstevel@tonic-gate 
11507c478bd9Sstevel@tonic-gate 	for (counter = 0;
1151e1dd0a2fSth 	    counter < arglist->serviceAuthenticationMethod->count;
1152e1dd0a2fSth 	    counter++) {
11537c478bd9Sstevel@tonic-gate 
11547c478bd9Sstevel@tonic-gate 		LDAP_SET_PARAM(
1155e1dd0a2fSth 		    arglist->serviceAuthenticationMethod->optlist[counter],
1156e1dd0a2fSth 		    NS_LDAP_SERVICE_AUTH_METHOD_P);
11577c478bd9Sstevel@tonic-gate 	}
11587c478bd9Sstevel@tonic-gate 	for (counter = 0;
1159e1dd0a2fSth 	    counter < arglist->serviceCredentialLevel->count;
1160e1dd0a2fSth 	    counter++) {
11617c478bd9Sstevel@tonic-gate 
11627c478bd9Sstevel@tonic-gate 		LDAP_SET_PARAM(
1163e1dd0a2fSth 		    arglist->serviceCredentialLevel->optlist[counter],
1164e1dd0a2fSth 		    NS_LDAP_SERVICE_CRED_LEVEL_P);
11657c478bd9Sstevel@tonic-gate 	}
11667c478bd9Sstevel@tonic-gate 	for (counter = 0;
1167e1dd0a2fSth 	    counter < arglist->objectclassMap->count;
1168e1dd0a2fSth 	    counter++) {
11697c478bd9Sstevel@tonic-gate 
11707c478bd9Sstevel@tonic-gate 		LDAP_SET_PARAM(
1171e1dd0a2fSth 		    arglist->objectclassMap->optlist[counter],
1172e1dd0a2fSth 		    NS_LDAP_OBJECTCLASSMAP_P);
11737c478bd9Sstevel@tonic-gate 	}
11747c478bd9Sstevel@tonic-gate 	for (counter = 0;
1175e1dd0a2fSth 	    counter < arglist->attributeMap->count;
1176e1dd0a2fSth 	    counter++) {
11777c478bd9Sstevel@tonic-gate 
11787c478bd9Sstevel@tonic-gate 		LDAP_SET_PARAM(
1179e1dd0a2fSth 		    arglist->attributeMap->optlist[counter],
1180e1dd0a2fSth 		    NS_LDAP_ATTRIBUTEMAP_P);
11817c478bd9Sstevel@tonic-gate 	}
11827c478bd9Sstevel@tonic-gate 	for (counter = 0;
1183e1dd0a2fSth 	    counter < arglist->serviceSearchDescriptor->count;
1184e1dd0a2fSth 	    counter++) {
11857c478bd9Sstevel@tonic-gate 
11867c478bd9Sstevel@tonic-gate 		LDAP_SET_PARAM(
1187e1dd0a2fSth 		    arglist->serviceSearchDescriptor->optlist[counter],
1188e1dd0a2fSth 		    NS_LDAP_SERVICE_SEARCH_DESC_P);
11897c478bd9Sstevel@tonic-gate 	}
11907c478bd9Sstevel@tonic-gate 
11917c478bd9Sstevel@tonic-gate 	retcode = credCheck(arglist);
1192dd1104fbSMichen Chang 	if (retcode == CLIENT_SUCCESS)
1193dd1104fbSMichen Chang 		retcode = adminCredCheck(arglist);
11947c478bd9Sstevel@tonic-gate 	if (retcode != CLIENT_SUCCESS) {
11957c478bd9Sstevel@tonic-gate 		CLIENT_FPUTS(
1196e1dd0a2fSth 		    gettext("Error in setting up credentials\n"),
1197e1dd0a2fSth 		    stderr);
11987c478bd9Sstevel@tonic-gate 		return (retcode);
11997c478bd9Sstevel@tonic-gate 	}
12007c478bd9Sstevel@tonic-gate 
12017c478bd9Sstevel@tonic-gate 	if (mode_verbose)
12027c478bd9Sstevel@tonic-gate 		CLIENT_FPUTS(
1203e1dd0a2fSth 		    gettext("About to modify this machines configuration "
1204e1dd0a2fSth 		    "by writing the files\n"),
1205e1dd0a2fSth 		    stderr);
12067c478bd9Sstevel@tonic-gate 
12077c478bd9Sstevel@tonic-gate 	/* get ready to start playing with files */
12087c478bd9Sstevel@tonic-gate 	retcode = stop_services(STATE_SAVE);
12097c478bd9Sstevel@tonic-gate 	if (retcode != CLIENT_SUCCESS) {
12107c478bd9Sstevel@tonic-gate 		CLIENT_FPUTS(
1211e1dd0a2fSth 		    gettext("Errors stopping network services.\n"), stderr);
12127c478bd9Sstevel@tonic-gate 		return (CLIENT_ERR_FAIL);
12137c478bd9Sstevel@tonic-gate 	}
12147c478bd9Sstevel@tonic-gate 
12157c478bd9Sstevel@tonic-gate 	/* Temporarily save orig versions of files */
12167c478bd9Sstevel@tonic-gate 	retcode = mod_backup();
12177c478bd9Sstevel@tonic-gate 	if (retcode != CLIENT_SUCCESS) {
12187c478bd9Sstevel@tonic-gate 		CLIENT_FPUTS(
1219e1dd0a2fSth 		    gettext("Unable to backup the ldap client files!\n"),
1220e1dd0a2fSth 		    stderr);
12217c478bd9Sstevel@tonic-gate 
12227c478bd9Sstevel@tonic-gate 		return (retcode);
12237c478bd9Sstevel@tonic-gate 
12247c478bd9Sstevel@tonic-gate 	}
12257c478bd9Sstevel@tonic-gate 
12267c478bd9Sstevel@tonic-gate 	/* Dump new files */
12277c478bd9Sstevel@tonic-gate 	errorp = __ns_ldap_DumpConfiguration(NSCONFIGFILE);
12287c478bd9Sstevel@tonic-gate 	if (errorp != NULL) {
12297c478bd9Sstevel@tonic-gate 		CLIENT_FPRINTF(stderr,
1230e1dd0a2fSth 		    gettext("%s mod: errorp is not NULL; %s\n"),
1231e1dd0a2fSth 		    cmd, errorp->message);
12327c478bd9Sstevel@tonic-gate 		retcode = mod_recover();
12337c478bd9Sstevel@tonic-gate 		if (retcode != CLIENT_SUCCESS) {
12347c478bd9Sstevel@tonic-gate 			CLIENT_FPUTS(
1235e1dd0a2fSth 			    gettext("Recovery of systems configuration "
1236e1dd0a2fSth 			    "failed.  Manual intervention of "
1237e1dd0a2fSth 			    "config files is required.\n"),
1238e1dd0a2fSth 			    stderr);
12397c478bd9Sstevel@tonic-gate 		}
12407c478bd9Sstevel@tonic-gate 		(void) __ns_ldap_freeError(&errorp);
12417c478bd9Sstevel@tonic-gate 		reset_ret = start_services(START_RESET);
12427c478bd9Sstevel@tonic-gate 		if (reset_ret != CLIENT_SUCCESS) {
12437c478bd9Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr, gettext("Error (%d) while "
1244e1dd0a2fSth 			    "starting services during reset\n"),
1245e1dd0a2fSth 			    reset_ret);
12467c478bd9Sstevel@tonic-gate 		}
12477c478bd9Sstevel@tonic-gate 		return (CLIENT_ERR_FAIL);
12487c478bd9Sstevel@tonic-gate 	}
12497c478bd9Sstevel@tonic-gate 
12507c478bd9Sstevel@tonic-gate 	/* if (credargs(arglist)) */
12517c478bd9Sstevel@tonic-gate 	errorp = __ns_ldap_DumpConfiguration(NSCREDFILE);
12527c478bd9Sstevel@tonic-gate 	if (errorp != NULL) {
12537c478bd9Sstevel@tonic-gate 		CLIENT_FPRINTF(stderr,
1254e1dd0a2fSth 		    gettext("%s mod: errorp is not NULL; %s\n"),
1255e1dd0a2fSth 		    cmd, errorp->message);
12567c478bd9Sstevel@tonic-gate 		retcode = mod_recover();
12577c478bd9Sstevel@tonic-gate 		if (retcode != CLIENT_SUCCESS) {
12587c478bd9Sstevel@tonic-gate 			CLIENT_FPUTS(
1259e1dd0a2fSth 			    gettext("Recovery of systems configuration "
1260e1dd0a2fSth 			    "failed.  Manual intervention of "
1261e1dd0a2fSth 			    "config files is required.\n"),
1262e1dd0a2fSth 			    stderr);
12637c478bd9Sstevel@tonic-gate 		}
12647c478bd9Sstevel@tonic-gate 		(void) __ns_ldap_freeError(&errorp);
12657c478bd9Sstevel@tonic-gate 		reset_ret = start_services(START_RESET);
12667c478bd9Sstevel@tonic-gate 		if (reset_ret != CLIENT_SUCCESS) {
12677c478bd9Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr, gettext("Error (%d) while "
1268e1dd0a2fSth 			    "starting services during reset\n"),
1269e1dd0a2fSth 			    reset_ret);
12707c478bd9Sstevel@tonic-gate 		}
12717c478bd9Sstevel@tonic-gate 		return (CLIENT_ERR_FAIL);
12727c478bd9Sstevel@tonic-gate 	}
12737c478bd9Sstevel@tonic-gate 
12747c478bd9Sstevel@tonic-gate 	if ((domain_fp = open(DOMAINNAME, O_WRONLY|O_CREAT|O_TRUNC,
1275e1dd0a2fSth 	    S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) == -1) { /* 0644 */
12767c478bd9Sstevel@tonic-gate 		CLIENT_FPRINTF(stderr, gettext("Cannot open %s\n"), DOMAINNAME);
12777c478bd9Sstevel@tonic-gate 		retcode = mod_recover();
12787c478bd9Sstevel@tonic-gate 		if (retcode != CLIENT_SUCCESS) {
12797c478bd9Sstevel@tonic-gate 			CLIENT_FPUTS(
1280e1dd0a2fSth 			    gettext("Recovery of systems configuration "
1281e1dd0a2fSth 			    "failed!  Machine needs to be "
1282e1dd0a2fSth 			    "fixed!\n"),
1283e1dd0a2fSth 			    stderr);
12847c478bd9Sstevel@tonic-gate 		}
12857c478bd9Sstevel@tonic-gate 		reset_ret = start_services(START_RESET);
12867c478bd9Sstevel@tonic-gate 		if (reset_ret != CLIENT_SUCCESS) {
12877c478bd9Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr, gettext("Error (%d) while "
1288e1dd0a2fSth 			    "starting services during reset\n"),
1289e1dd0a2fSth 			    reset_ret);
12907c478bd9Sstevel@tonic-gate 		}
12917c478bd9Sstevel@tonic-gate 		return (CLIENT_ERR_FAIL);
12927c478bd9Sstevel@tonic-gate 	}
12937c478bd9Sstevel@tonic-gate 	(void) write(domain_fp, dname, strlen(dname));
12947c478bd9Sstevel@tonic-gate 	(void) write(domain_fp, "\n", 1);
12957c478bd9Sstevel@tonic-gate 	(void) close(domain_fp);
12967c478bd9Sstevel@tonic-gate 
12977c478bd9Sstevel@tonic-gate 	retcode = start_services(START_INIT);
12987c478bd9Sstevel@tonic-gate 
12997c478bd9Sstevel@tonic-gate 	if (retcode == CLIENT_SUCCESS) {
13007c478bd9Sstevel@tonic-gate 		CLIENT_FPUTS(gettext("System successfully configured\n"),
1301e1dd0a2fSth 		    stderr);
13027c478bd9Sstevel@tonic-gate 	} else {
13037c478bd9Sstevel@tonic-gate 		CLIENT_FPUTS(gettext("Error resetting system.\n"
1304e1dd0a2fSth 		    "Recovering old system settings.\n"), stderr),
13057c478bd9Sstevel@tonic-gate 
1306e1dd0a2fSth 		    /* stop any started services for recover */
1307e1dd0a2fSth 		    /* don't stomp on history of saved services state */
1308e1dd0a2fSth 		    reset_ret = stop_services(STATE_NOSAVE);
13097c478bd9Sstevel@tonic-gate 		if (reset_ret != CLIENT_SUCCESS) {
13107c478bd9Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr, gettext("Error (%d) while "
1311e1dd0a2fSth 			    "stopping services during reset\n"),
1312e1dd0a2fSth 			    reset_ret);
13137c478bd9Sstevel@tonic-gate 			/* Coninue and try to recover what we can */
13147c478bd9Sstevel@tonic-gate 		}
13157c478bd9Sstevel@tonic-gate 		reset_ret = mod_recover();
13167c478bd9Sstevel@tonic-gate 		if (reset_ret != CLIENT_SUCCESS) {
13177c478bd9Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr, gettext("Error (%d) while "
1318e1dd0a2fSth 			    "recovering service files during "
1319e1dd0a2fSth 			    "reset\n"), reset_ret);
13207c478bd9Sstevel@tonic-gate 			/* Continue and start what we can */
13217c478bd9Sstevel@tonic-gate 		}
13227c478bd9Sstevel@tonic-gate 		reset_ret = start_services(START_RESET);
13237c478bd9Sstevel@tonic-gate 		if (reset_ret != CLIENT_SUCCESS) {
13247c478bd9Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr, gettext("Error (%d) while "
1325e1dd0a2fSth 			    "starting services during reset\n"),
1326e1dd0a2fSth 			    reset_ret);
13277c478bd9Sstevel@tonic-gate 		}
13287c478bd9Sstevel@tonic-gate 	}
13297c478bd9Sstevel@tonic-gate 
13307c478bd9Sstevel@tonic-gate 	/* Cleanup temporary files created by mod_backup() */
13317c478bd9Sstevel@tonic-gate 	mod_cleanup();
13327c478bd9Sstevel@tonic-gate 
13337c478bd9Sstevel@tonic-gate 	return (retcode);
13347c478bd9Sstevel@tonic-gate }
13357c478bd9Sstevel@tonic-gate 
13367c478bd9Sstevel@tonic-gate 
13377c478bd9Sstevel@tonic-gate static int
13387c478bd9Sstevel@tonic-gate client_genProfile(clientopts_t *arglist)
13397c478bd9Sstevel@tonic-gate {
13407c478bd9Sstevel@tonic-gate 	int counter;
13417c478bd9Sstevel@tonic-gate 	int retcode;	/* required for LDAP_SET_PARAM macro */
13427c478bd9Sstevel@tonic-gate 	ns_ldap_error_t *errorp;
13437c478bd9Sstevel@tonic-gate 
13447c478bd9Sstevel@tonic-gate 	if (mode_verbose)
13457c478bd9Sstevel@tonic-gate 		CLIENT_FPUTS(gettext("About to generate a profile\n"), stderr);
13467c478bd9Sstevel@tonic-gate 
13477c478bd9Sstevel@tonic-gate 	/* *** Check for invalid args *** */
13487c478bd9Sstevel@tonic-gate 	LDAP_CHECK_INVALID(arglist->proxyDN, "proxyDN");
13497c478bd9Sstevel@tonic-gate 	LDAP_CHECK_INVALID(arglist->proxyPassword, "proxyPassword");
1350dd1104fbSMichen Chang 	LDAP_CHECK_INVALID(arglist->enableShadowUpdate,
1351dd1104fbSMichen Chang 	    "enableShadowUpdate");
1352dd1104fbSMichen Chang 	LDAP_CHECK_INVALID(arglist->adminDN, "adminDN");
1353dd1104fbSMichen Chang 	LDAP_CHECK_INVALID(arglist->adminPassword, "adminPassword");
13547c478bd9Sstevel@tonic-gate 	LDAP_CHECK_INVALID(arglist->certificatePath, "certificatePath");
13557c478bd9Sstevel@tonic-gate 	LDAP_CHECK_INVALID(arglist->domainName, "domainName");
1356e1dd0a2fSth 	LDAP_CHECK_INVALID(arglist->bindDN, "bind DN");
1357e1dd0a2fSth 	LDAP_CHECK_INVALID(arglist->bindPasswd, "bind password");
13587c478bd9Sstevel@tonic-gate 	/* *** End check for invalid args *** */
13597c478bd9Sstevel@tonic-gate 
13607c478bd9Sstevel@tonic-gate 	if (arglist->profileName == NULL) {
13617c478bd9Sstevel@tonic-gate 		if (mode_verbose)
13627c478bd9Sstevel@tonic-gate 			CLIENT_FPUTS(
1363e1dd0a2fSth 			    gettext("No profile specified. "
1364e1dd0a2fSth 			    "Using \"default\"\n"),
1365e1dd0a2fSth 			    stderr);
13667c478bd9Sstevel@tonic-gate 		arglist->profileName = "default";
13677c478bd9Sstevel@tonic-gate 	}
13687c478bd9Sstevel@tonic-gate 
13697c478bd9Sstevel@tonic-gate 	__ns_ldap_setServer(TRUE);
13707c478bd9Sstevel@tonic-gate 	__ns_ldap_default_config();
13717c478bd9Sstevel@tonic-gate 
13727c478bd9Sstevel@tonic-gate 	/* Set version to latest (not version 1) */
13737c478bd9Sstevel@tonic-gate 	LDAP_SET_PARAM(NS_LDAP_VERSION, NS_LDAP_FILE_VERSION_P);
13747c478bd9Sstevel@tonic-gate 
13757c478bd9Sstevel@tonic-gate 	/* Set additional valid params from command line */
13767c478bd9Sstevel@tonic-gate 	LDAP_SET_PARAM(arglist->authenticationMethod, NS_LDAP_AUTH_P);
13777c478bd9Sstevel@tonic-gate 	LDAP_SET_PARAM(arglist->defaultSearchBase, NS_LDAP_SEARCH_BASEDN_P);
13787c478bd9Sstevel@tonic-gate 	LDAP_SET_PARAM(arglist->credentialLevel, NS_LDAP_CREDENTIAL_LEVEL_P);
13797c478bd9Sstevel@tonic-gate 	LDAP_SET_PARAM(arglist->profileTTL, NS_LDAP_CACHETTL_P);
13807c478bd9Sstevel@tonic-gate 	LDAP_SET_PARAM(arglist->searchTimeLimit, NS_LDAP_SEARCH_TIME_P);
13817c478bd9Sstevel@tonic-gate 	LDAP_SET_PARAM(arglist->preferredServerList, NS_LDAP_SERVER_PREF_P);
13827c478bd9Sstevel@tonic-gate 	LDAP_SET_PARAM(arglist->profileName, NS_LDAP_PROFILE_P);
13837c478bd9Sstevel@tonic-gate 	LDAP_SET_PARAM(arglist->followReferrals, NS_LDAP_SEARCH_REF_P);
13847c478bd9Sstevel@tonic-gate 	LDAP_SET_PARAM(arglist->defaultSearchScope, NS_LDAP_SEARCH_SCOPE_P);
13857c478bd9Sstevel@tonic-gate 	LDAP_SET_PARAM(arglist->bindTimeLimit, NS_LDAP_BIND_TIME_P);
13867c478bd9Sstevel@tonic-gate 	LDAP_SET_PARAM(arglist->defaultServerList, NS_LDAP_SERVERS_P);
13877c478bd9Sstevel@tonic-gate 
13887c478bd9Sstevel@tonic-gate 	for (counter = 0;
1389e1dd0a2fSth 	    counter < arglist->serviceAuthenticationMethod->count;
1390e1dd0a2fSth 	    counter++) {
13917c478bd9Sstevel@tonic-gate 
13927c478bd9Sstevel@tonic-gate 		LDAP_SET_PARAM(
1393e1dd0a2fSth 		    arglist->serviceAuthenticationMethod->optlist[counter],
1394e1dd0a2fSth 		    NS_LDAP_SERVICE_AUTH_METHOD_P);
13957c478bd9Sstevel@tonic-gate 	}
13967c478bd9Sstevel@tonic-gate 	for (counter = 0;
1397e1dd0a2fSth 	    counter < arglist->serviceCredentialLevel->count;
1398e1dd0a2fSth 	    counter++) {
13997c478bd9Sstevel@tonic-gate 
14007c478bd9Sstevel@tonic-gate 		LDAP_SET_PARAM(
1401e1dd0a2fSth 		    arglist->serviceCredentialLevel->optlist[counter],
1402e1dd0a2fSth 		    NS_LDAP_SERVICE_CRED_LEVEL_P);
14037c478bd9Sstevel@tonic-gate 	}
14047c478bd9Sstevel@tonic-gate 	for (counter = 0;
1405e1dd0a2fSth 	    counter < arglist->objectclassMap->count;
1406e1dd0a2fSth 	    counter++) {
14077c478bd9Sstevel@tonic-gate 
14087c478bd9Sstevel@tonic-gate 		LDAP_SET_PARAM(
1409e1dd0a2fSth 		    arglist->objectclassMap->optlist[counter],
1410e1dd0a2fSth 		    NS_LDAP_OBJECTCLASSMAP_P);
14117c478bd9Sstevel@tonic-gate 	}
14127c478bd9Sstevel@tonic-gate 	for (counter = 0;
1413e1dd0a2fSth 	    counter < arglist->attributeMap->count;
1414e1dd0a2fSth 	    counter++) {
14157c478bd9Sstevel@tonic-gate 
14167c478bd9Sstevel@tonic-gate 		LDAP_SET_PARAM(
1417e1dd0a2fSth 		    arglist->attributeMap->optlist[counter],
1418e1dd0a2fSth 		    NS_LDAP_ATTRIBUTEMAP_P);
14197c478bd9Sstevel@tonic-gate 	}
14207c478bd9Sstevel@tonic-gate 	for (counter = 0;
1421e1dd0a2fSth 	    counter < arglist->serviceSearchDescriptor->count;
1422e1dd0a2fSth 	    counter++) {
14237c478bd9Sstevel@tonic-gate 
14247c478bd9Sstevel@tonic-gate 		LDAP_SET_PARAM(
1425e1dd0a2fSth 		    arglist->serviceSearchDescriptor->optlist[counter],
1426e1dd0a2fSth 		    NS_LDAP_SERVICE_SEARCH_DESC_P);
14277c478bd9Sstevel@tonic-gate 	}
14287c478bd9Sstevel@tonic-gate 
14297c478bd9Sstevel@tonic-gate 	errorp = __ns_ldap_DumpLdif(NULL);
14307c478bd9Sstevel@tonic-gate 	if (errorp != NULL) {
14317c478bd9Sstevel@tonic-gate 		CLIENT_FPUTS(errorp->message, stderr);
14327c478bd9Sstevel@tonic-gate 		CLIENT_FPUTC('\n', stderr);
14337c478bd9Sstevel@tonic-gate 		(void) __ns_ldap_freeError(&errorp);
14347c478bd9Sstevel@tonic-gate 		return (CLIENT_ERR_FAIL);
14357c478bd9Sstevel@tonic-gate 	}
14367c478bd9Sstevel@tonic-gate 
14377c478bd9Sstevel@tonic-gate 	return (CLIENT_SUCCESS);
14387c478bd9Sstevel@tonic-gate }
14397c478bd9Sstevel@tonic-gate 
1440e1dd0a2fSth /* INET6_ADDRSTRLEN + ":" + <5-digit port> + some round-up */
1441e1dd0a2fSth #define	MAX_HOSTADDR_LEN (INET6_ADDRSTRLEN + 6 + 12)
1442e1dd0a2fSth 
14437c478bd9Sstevel@tonic-gate static int
14447c478bd9Sstevel@tonic-gate client_init(clientopts_t *arglist)
14457c478bd9Sstevel@tonic-gate {
1446e1dd0a2fSth 	int			profile_fp;
1447e1dd0a2fSth 	int			retcode = CLIENT_SUCCESS;
1448e1dd0a2fSth 	ns_ldap_error_t		*errorp;
1449e1dd0a2fSth 	int			reset_ret;
1450e1dd0a2fSth 	int			ret_copy;
1451e1dd0a2fSth 	ns_standalone_conf_t	cfg = standaloneDefaults;
1452e1dd0a2fSth 	ns_auth_t		auth = {NS_LDAP_AUTH_NONE,
1453e1dd0a2fSth 					NS_LDAP_TLS_NONE,
1454e1dd0a2fSth 					NS_LDAP_SASL_NONE,
1455e1dd0a2fSth 					NS_LDAP_SASLOPT_NONE};
1456e1dd0a2fSth 	char			peer[MAX_HOSTADDR_LEN];
1457e1dd0a2fSth 	ns_auth_t		**authMethod;
1458e1dd0a2fSth 	int			**credLevel, i;
1459e1dd0a2fSth 	char			*cred;
14607c478bd9Sstevel@tonic-gate 
14617c478bd9Sstevel@tonic-gate 	if (mode_verbose)
14627c478bd9Sstevel@tonic-gate 		CLIENT_FPUTS(
1463e1dd0a2fSth 		    gettext("About to configure machine by downloading "
1464e1dd0a2fSth 		    "a profile\n"),
1465e1dd0a2fSth 		    stderr);
14667c478bd9Sstevel@tonic-gate 
14677c478bd9Sstevel@tonic-gate 	if (dname == NULL) {
14687c478bd9Sstevel@tonic-gate 		CLIENT_FPUTS(
1469e1dd0a2fSth 		    gettext("Init failed: System domain not set and "
1470e1dd0a2fSth 		    "no domainName specified.\n"),
1471e1dd0a2fSth 		    stderr);
14727c478bd9Sstevel@tonic-gate 		return (CLIENT_ERR_FAIL);
14737c478bd9Sstevel@tonic-gate 	}
14747c478bd9Sstevel@tonic-gate 
14757c478bd9Sstevel@tonic-gate 	if (!arglist->defaultServerList) {
14767c478bd9Sstevel@tonic-gate 		CLIENT_FPUTS(gettext("Missing LDAP server address\n"), stderr);
14777c478bd9Sstevel@tonic-gate 		return (CLIENT_ERR_FAIL);
14787c478bd9Sstevel@tonic-gate 	}
14797c478bd9Sstevel@tonic-gate 
14807c478bd9Sstevel@tonic-gate 	/* *** Check for invalid args *** */
14817c478bd9Sstevel@tonic-gate 	LDAP_CHECK_INVALID(arglist->defaultSearchBase,
1482e1dd0a2fSth 	    "defaultSearchBase");
14837c478bd9Sstevel@tonic-gate 	LDAP_CHECK_INVALID(arglist->profileTTL,
1484e1dd0a2fSth 	    "profileTTL");
14857c478bd9Sstevel@tonic-gate 	LDAP_CHECK_INVALID(arglist->searchTimeLimit,
1486e1dd0a2fSth 	    "searchTimeLimit");
14877c478bd9Sstevel@tonic-gate 	LDAP_CHECK_INVALID(arglist->preferredServerList,
1488e1dd0a2fSth 	    "preferredServerList");
14897c478bd9Sstevel@tonic-gate 	LDAP_CHECK_INVALID(arglist->followReferrals,
1490e1dd0a2fSth 	    "followReferrals");
14917c478bd9Sstevel@tonic-gate 	LDAP_CHECK_INVALID(arglist->defaultSearchScope,
1492e1dd0a2fSth 	    "defaultSearchScope");
14937c478bd9Sstevel@tonic-gate 	LDAP_CHECK_INVALID(arglist->bindTimeLimit,
1494e1dd0a2fSth 	    "bindTimeLimit");
14957c478bd9Sstevel@tonic-gate 
14967c478bd9Sstevel@tonic-gate 	LDAP_CHECK_INVALID(arglist->objectclassMap->count,
1497e1dd0a2fSth 	    "objectclassMap");
14987c478bd9Sstevel@tonic-gate 	LDAP_CHECK_INVALID(arglist->attributeMap->count,
1499e1dd0a2fSth 	    "attributeMap");
15007c478bd9Sstevel@tonic-gate 	LDAP_CHECK_INVALID(arglist->serviceAuthenticationMethod->count,
1501e1dd0a2fSth 	    "serviceAuthenticationMethod");
15027c478bd9Sstevel@tonic-gate 	LDAP_CHECK_INVALID(arglist->serviceCredentialLevel->count,
1503e1dd0a2fSth 	    "serviceCredentialLevel");
15047c478bd9Sstevel@tonic-gate 	LDAP_CHECK_INVALID(arglist->serviceSearchDescriptor->count,
1505e1dd0a2fSth 	    "serviceSearchDescriptor");
15067c478bd9Sstevel@tonic-gate 	/* *** End check for invalid args *** */
15077c478bd9Sstevel@tonic-gate 
15087c478bd9Sstevel@tonic-gate 	if (arglist->profileName == NULL) {
15097c478bd9Sstevel@tonic-gate 		if (mode_verbose)
15107c478bd9Sstevel@tonic-gate 			CLIENT_FPUTS(
1511e1dd0a2fSth 			    gettext("No profile specified. "
1512e1dd0a2fSth 			    "Using \"default\"\n"),
1513e1dd0a2fSth 			    stderr);
15147c478bd9Sstevel@tonic-gate 		arglist->profileName = "default";
15157c478bd9Sstevel@tonic-gate 	}
15167c478bd9Sstevel@tonic-gate 
1517e1dd0a2fSth 	(void) strncpy(peer, arglist->defaultServerList, MAX_HOSTADDR_LEN - 1);
1518e1dd0a2fSth 	if (separatePort(peer, &cfg.SA_SERVER, &cfg.SA_PORT) > 0) {
15197c478bd9Sstevel@tonic-gate 		return (CLIENT_ERR_FAIL);
15207c478bd9Sstevel@tonic-gate 	}
1521e1dd0a2fSth 
1522e1dd0a2fSth 	if (arglist->bindDN != NULL) {
1523e1dd0a2fSth 		cfg.SA_CRED = "proxy";
1524e1dd0a2fSth 		/*
1525e1dd0a2fSth 		 * We don't want to force users to always specify authentication
1526dd1104fbSMichen Chang 		 * method when we can infer it. If users want SSL, he/she would
1527e1dd0a2fSth 		 * have to specify appropriate -a though.
1528e1dd0a2fSth 		 */
1529e1dd0a2fSth 		auth.type = NS_LDAP_AUTH_SIMPLE;
1530e1dd0a2fSth 		if (arglist->bindPasswd == NULL) {
1531e1dd0a2fSth 			arglist->bindPasswd =
1532e1dd0a2fSth 			    getpassphrase("Bind Password:");
1533e1dd0a2fSth 			if (arglist->bindPasswd == NULL) {
1534e1dd0a2fSth 				CLIENT_FPUTS(gettext("Get password failed\n"),
1535e1dd0a2fSth 				    stderr);
1536e1dd0a2fSth 
1537e1dd0a2fSth 				if (gStartLdap == START_RESET)
1538e1dd0a2fSth 					(void) start_service(LDAP_FMRI, B_TRUE);
1539e1dd0a2fSth 
1540e1dd0a2fSth 				return (CLIENT_ERR_CREDENTIAL);
1541e1dd0a2fSth 			}
1542e1dd0a2fSth 		}
15437c478bd9Sstevel@tonic-gate 	}
1544e1dd0a2fSth 	cfg.SA_BIND_DN = arglist->bindDN;
1545e1dd0a2fSth 	cfg.SA_BIND_PWD = arglist->bindPasswd;
15467c478bd9Sstevel@tonic-gate 
1547e1dd0a2fSth 	if (arglist->authenticationMethod != NULL) {
1548e1dd0a2fSth 		if (__ns_ldap_initAuth(arglist->authenticationMethod,
1549e1dd0a2fSth 		    &auth, &errorp) != NS_LDAP_SUCCESS) {
1550e1dd0a2fSth 			if (errorp != NULL) {
1551e1dd0a2fSth 				CLIENT_FPRINTF(stderr, "%s", errorp->message);
1552e1dd0a2fSth 				(void) __ns_ldap_freeError(&errorp);
1553e1dd0a2fSth 			}
1554e1dd0a2fSth 
1555e1dd0a2fSth 			if (gStartLdap == START_RESET)
1556e1dd0a2fSth 				(void) start_service(LDAP_FMRI, B_TRUE);
1557e1dd0a2fSth 
1558e1dd0a2fSth 			return (CLIENT_ERR_FAIL);
1559e1dd0a2fSth 		}
1560e1dd0a2fSth 		cfg.SA_AUTH = &auth;
15617c478bd9Sstevel@tonic-gate 	}
1562e1dd0a2fSth 	cfg.SA_CRED = arglist->credentialLevel;
15637c478bd9Sstevel@tonic-gate 
1564e1dd0a2fSth 	cfg.SA_DOMAIN = arglist->domainName;
1565e1dd0a2fSth 	cfg.SA_PROFILE_NAME = arglist->profileName;
1566e1dd0a2fSth 	cfg.SA_CERT_PATH = arglist->certificatePath;
1567e1dd0a2fSth 
1568e1dd0a2fSth 	cfg.type = NS_LDAP_SERVER;
1569e1dd0a2fSth 
1570e1dd0a2fSth 	if (__ns_ldap_initStandalone(&cfg, &errorp) != NS_LDAP_SUCCESS) {
15717c478bd9Sstevel@tonic-gate 		if (errorp != NULL) {
1572e1dd0a2fSth 			CLIENT_FPRINTF(stderr, "%s", errorp->message);
15737c478bd9Sstevel@tonic-gate 			(void) __ns_ldap_freeError(&errorp);
15747c478bd9Sstevel@tonic-gate 		}
15757c478bd9Sstevel@tonic-gate 
15767c478bd9Sstevel@tonic-gate 		if (gStartLdap == START_RESET)
15777c478bd9Sstevel@tonic-gate 			(void) start_service(LDAP_FMRI, B_TRUE);
15787c478bd9Sstevel@tonic-gate 
15797c478bd9Sstevel@tonic-gate 		return (CLIENT_ERR_FAIL);
15807c478bd9Sstevel@tonic-gate 	}
15817c478bd9Sstevel@tonic-gate 
1582e1dd0a2fSth 	if (arglist->proxyDN != NULL && arglist->proxyPassword == NULL) {
1583e1dd0a2fSth 		arglist->proxyPassword = getpassphrase("Proxy Bind Password:");
1584e1dd0a2fSth 		if (arglist->proxyPassword == NULL) {
1585e1dd0a2fSth 			CLIENT_FPUTS(gettext("Get password failed\n"), stderr);
1586e1dd0a2fSth 
1587e1dd0a2fSth 			if (gStartLdap == START_RESET)
1588e1dd0a2fSth 				(void) start_service(LDAP_FMRI, B_TRUE);
1589e1dd0a2fSth 
1590e1dd0a2fSth 			return (CLIENT_ERR_CREDENTIAL);
1591e1dd0a2fSth 		}
15927c478bd9Sstevel@tonic-gate 	}
1593e1dd0a2fSth 	if (arglist->proxyDN != NULL && arglist->proxyPassword != NULL) {
1594e1dd0a2fSth 		if (__ns_ldap_setParam(NS_LDAP_BINDDN_P,
1595e1dd0a2fSth 		    arglist->proxyDN, &errorp) != NS_LDAP_SUCCESS) {
1596e1dd0a2fSth 			if (errorp != NULL) {
1597e1dd0a2fSth 				CLIENT_FPRINTF(stderr, "%s", errorp->message);
1598e1dd0a2fSth 				(void) __ns_ldap_freeError(&errorp);
1599e1dd0a2fSth 			}
1600e1dd0a2fSth 			return (CLIENT_ERR_CREDENTIAL);
1601e1dd0a2fSth 		}
1602e1dd0a2fSth 		if (__ns_ldap_setParam(NS_LDAP_BINDPASSWD_P,
1603e1dd0a2fSth 		    arglist->proxyPassword, &errorp) != NS_LDAP_SUCCESS) {
1604e1dd0a2fSth 			if (errorp != NULL) {
1605e1dd0a2fSth 				CLIENT_FPRINTF(stderr, "%s", errorp->message);
1606e1dd0a2fSth 				(void) __ns_ldap_freeError(&errorp);
1607e1dd0a2fSth 			}
1608e1dd0a2fSth 			return (CLIENT_ERR_CREDENTIAL);
1609e1dd0a2fSth 		}
1610e1dd0a2fSth 	}
1611e1dd0a2fSth 
1612dd1104fbSMichen Chang 	if (arglist->enableShadowUpdate != NULL) {
1613dd1104fbSMichen Chang 		LDAP_SET_PARAM(arglist->enableShadowUpdate,
1614dd1104fbSMichen Chang 		    NS_LDAP_ENABLE_SHADOW_UPDATE_P);
1615dd1104fbSMichen Chang 	}
1616dd1104fbSMichen Chang 
1617dd1104fbSMichen Chang 	if (arglist->enableShadowUpdate &&
1618dd1104fbSMichen Chang 	    strcasecmp(arglist->enableShadowUpdate, "TRUE") == 0 &&
1619dd1104fbSMichen Chang 	    arglist->adminDN != NULL && arglist->adminPassword == NULL) {
1620dd1104fbSMichen Chang 		arglist->adminPassword = getpassphrase("admin Bind Password:");
1621dd1104fbSMichen Chang 		if (arglist->adminPassword == NULL) {
1622dd1104fbSMichen Chang 			CLIENT_FPUTS(gettext("Get password failed\n"), stderr);
1623dd1104fbSMichen Chang 
1624dd1104fbSMichen Chang 			if (gStartLdap == START_RESET)
1625dd1104fbSMichen Chang 				(void) start_service(LDAP_FMRI, B_TRUE);
1626dd1104fbSMichen Chang 
1627dd1104fbSMichen Chang 			return (CLIENT_ERR_CREDENTIAL);
1628dd1104fbSMichen Chang 		}
1629dd1104fbSMichen Chang 	}
1630dd1104fbSMichen Chang 	if (arglist->adminDN != NULL && arglist->adminPassword != NULL) {
1631dd1104fbSMichen Chang 		if (__ns_ldap_setParam(NS_LDAP_ADMIN_BINDDN_P,
1632dd1104fbSMichen Chang 		    arglist->adminDN, &errorp) != NS_LDAP_SUCCESS) {
1633dd1104fbSMichen Chang 			if (errorp != NULL) {
1634dd1104fbSMichen Chang 				CLIENT_FPRINTF(stderr, "%s\n", errorp->message);
1635dd1104fbSMichen Chang 				(void) __ns_ldap_freeError(&errorp);
1636dd1104fbSMichen Chang 			}
1637dd1104fbSMichen Chang 			return (CLIENT_ERR_CREDENTIAL);
1638dd1104fbSMichen Chang 		}
1639dd1104fbSMichen Chang 		if (__ns_ldap_setParam(NS_LDAP_ADMIN_BINDPASSWD_P,
1640dd1104fbSMichen Chang 		    arglist->adminPassword, &errorp) != NS_LDAP_SUCCESS) {
1641dd1104fbSMichen Chang 			if (errorp != NULL) {
1642dd1104fbSMichen Chang 				CLIENT_FPRINTF(stderr, "%s\n", errorp->message);
1643dd1104fbSMichen Chang 				(void) __ns_ldap_freeError(&errorp);
1644dd1104fbSMichen Chang 			}
1645dd1104fbSMichen Chang 			return (CLIENT_ERR_CREDENTIAL);
1646dd1104fbSMichen Chang 		}
1647dd1104fbSMichen Chang 	}
1648dd1104fbSMichen Chang 
1649e1dd0a2fSth 	if (arglist->authenticationMethod != NULL) {
1650e1dd0a2fSth 		if (__ns_ldap_getParam(NS_LDAP_AUTH_P,
1651e1dd0a2fSth 		    (void ***)&authMethod, &errorp) != NS_LDAP_SUCCESS) {
1652e1dd0a2fSth 			if (errorp != NULL) {
1653e1dd0a2fSth 				CLIENT_FPRINTF(stderr, "%s", errorp->message);
1654e1dd0a2fSth 				(void) __ns_ldap_freeError(&errorp);
1655e1dd0a2fSth 			}
1656e1dd0a2fSth 			return (CLIENT_ERR_CREDENTIAL);
1657e1dd0a2fSth 		}
1658e1dd0a2fSth 
1659e1dd0a2fSth 		if (authMethod != NULL) {
1660e1dd0a2fSth 			for (i = 0; authMethod[i] != NULL; ++i) {
1661e1dd0a2fSth 				if (authMethod[i]->type == auth.type) {
1662e1dd0a2fSth 					break;
1663e1dd0a2fSth 				}
1664e1dd0a2fSth 			}
1665e1dd0a2fSth 
1666e1dd0a2fSth 			if (authMethod[i] == NULL) {
1667e1dd0a2fSth 				CLIENT_FPRINTF(stderr, gettext(
1668e1dd0a2fSth 				    "Warning: init authentication method "
1669e1dd0a2fSth 				    "not found in DUAConfigProfile.\n"));
1670e1dd0a2fSth 			} else {
1671e1dd0a2fSth 				if (i != 0) {
1672e1dd0a2fSth 					CLIENT_FPRINTF(stderr,
1673e1dd0a2fSth 					    gettext(
1674e1dd0a2fSth 					    "Warning: init authentication"
1675e1dd0a2fSth 					    "method using secondary "
1676e1dd0a2fSth 					    "authentication method from "
1677e1dd0a2fSth 					    "DUAConfigProfile.\n"));
1678e1dd0a2fSth 				}
1679e1dd0a2fSth 			}
1680e1dd0a2fSth 			(void) __ns_ldap_freeParam((void ***) &authMethod);
1681e1dd0a2fSth 		}
1682e1dd0a2fSth 	}
1683e1dd0a2fSth 
1684e1dd0a2fSth 	if (arglist->credentialLevel != NULL) {
1685e1dd0a2fSth 		if (__ns_ldap_getParam(NS_LDAP_CREDENTIAL_LEVEL_P,
1686e1dd0a2fSth 		    (void ***)&credLevel, &errorp) != NS_LDAP_SUCCESS) {
1687e1dd0a2fSth 			if (errorp != NULL) {
1688e1dd0a2fSth 				CLIENT_FPRINTF(stderr, "%s", errorp->message);
1689e1dd0a2fSth 				(void) __ns_ldap_freeError(&errorp);
1690e1dd0a2fSth 			}
1691e1dd0a2fSth 			return (CLIENT_ERR_CREDENTIAL);
1692e1dd0a2fSth 		}
1693e1dd0a2fSth 		if (credLevel != NULL) {
1694e1dd0a2fSth 			for (i = 0; credLevel[i] != NULL; ++i) {
1695e1dd0a2fSth 				switch (*credLevel[i]) {
1696e1dd0a2fSth 				case NS_LDAP_CRED_ANON :
1697e1dd0a2fSth 					cred = "none";
1698e1dd0a2fSth 					break;
1699e1dd0a2fSth 				case NS_LDAP_CRED_PROXY :
1700e1dd0a2fSth 					cred = "proxy";
1701e1dd0a2fSth 					break;
1702e1dd0a2fSth 				case NS_LDAP_CRED_SELF :
1703e1dd0a2fSth 					cred = "self";
1704e1dd0a2fSth 					break;
1705e1dd0a2fSth 				default:
1706e1dd0a2fSth 					continue;
1707e1dd0a2fSth 				}
1708e1dd0a2fSth 				if (strcmp(cred,
1709e1dd0a2fSth 				    arglist->credentialLevel) == 0) {
1710e1dd0a2fSth 					break;
1711e1dd0a2fSth 				}
1712e1dd0a2fSth 			}
1713e1dd0a2fSth 			if (credLevel[i] == NULL) {
1714e1dd0a2fSth 				CLIENT_FPRINTF(stderr, gettext(
1715e1dd0a2fSth 				    "Warning: init credential level not found "
1716e1dd0a2fSth 				    "in DUAConfigProfile.\n"));
1717e1dd0a2fSth 			} else {
1718e1dd0a2fSth 				if (i != 0) {
1719e1dd0a2fSth 					CLIENT_FPRINTF(stderr,
1720e1dd0a2fSth 					    gettext("Warning: "
1721e1dd0a2fSth 					    "init credential level using "
1722e1dd0a2fSth 					    "secondary credential level from "
1723e1dd0a2fSth 					    "DUAConfigProfile.\n"));
1724e1dd0a2fSth 				}
1725e1dd0a2fSth 			}
1726e1dd0a2fSth 			(void) __ns_ldap_freeParam((void ***) &credLevel);
1727e1dd0a2fSth 		}
17287c478bd9Sstevel@tonic-gate 	}
17297c478bd9Sstevel@tonic-gate 
17307c478bd9Sstevel@tonic-gate 	retcode = credCheck(arglist);
1731dd1104fbSMichen Chang 	if (retcode == CLIENT_SUCCESS)
1732dd1104fbSMichen Chang 		retcode = adminCredCheck(arglist);
17337c478bd9Sstevel@tonic-gate 	if (retcode != CLIENT_SUCCESS) {
17347c478bd9Sstevel@tonic-gate 		CLIENT_FPUTS(
1735e1dd0a2fSth 		    gettext("Error in setting up credentials\n"), stderr);
17367c478bd9Sstevel@tonic-gate 
17377c478bd9Sstevel@tonic-gate 		if (gStartLdap == START_RESET)
17387c478bd9Sstevel@tonic-gate 			(void) start_service(LDAP_FMRI, B_TRUE);
17397c478bd9Sstevel@tonic-gate 
17407c478bd9Sstevel@tonic-gate 		return (retcode);
17417c478bd9Sstevel@tonic-gate 	}
17427c478bd9Sstevel@tonic-gate 
17437c478bd9Sstevel@tonic-gate 	if (mode_verbose)
17447c478bd9Sstevel@tonic-gate 		CLIENT_FPUTS(
1745e1dd0a2fSth 		    gettext("About to modify this machines configuration "
1746e1dd0a2fSth 		    "by writing the files\n"),
1747e1dd0a2fSth 		    stderr);
17487c478bd9Sstevel@tonic-gate 
17497c478bd9Sstevel@tonic-gate 	/* get ready to start playing with files */
17507c478bd9Sstevel@tonic-gate 	retcode = stop_services(STATE_SAVE);
17517c478bd9Sstevel@tonic-gate 	if (retcode != CLIENT_SUCCESS) {
17527c478bd9Sstevel@tonic-gate 		CLIENT_FPUTS(
1753e1dd0a2fSth 		    gettext("Errors stopping network services.\n"), stderr);
17547c478bd9Sstevel@tonic-gate 
17557c478bd9Sstevel@tonic-gate 		if (gStartLdap == START_RESET)
17567c478bd9Sstevel@tonic-gate 			(void) start_service(LDAP_FMRI, B_TRUE);
17577c478bd9Sstevel@tonic-gate 
17587c478bd9Sstevel@tonic-gate 		return (CLIENT_ERR_FAIL);
17597c478bd9Sstevel@tonic-gate 	}
17607c478bd9Sstevel@tonic-gate 
17617c478bd9Sstevel@tonic-gate 	/* Save orig versions of files */
17627c478bd9Sstevel@tonic-gate 	retcode = file_backup();
17637c478bd9Sstevel@tonic-gate 	if (retcode == CLIENT_ERR_RESTORE) {
17647c478bd9Sstevel@tonic-gate 		CLIENT_FPUTS(
1765e1dd0a2fSth 		    gettext("System not in state to enable ldap client.\n"),
1766e1dd0a2fSth 		    stderr);
17677c478bd9Sstevel@tonic-gate 
17687c478bd9Sstevel@tonic-gate 		return (retcode);
17697c478bd9Sstevel@tonic-gate 
17707c478bd9Sstevel@tonic-gate 	} else if (retcode != CLIENT_SUCCESS) {
17717c478bd9Sstevel@tonic-gate 		CLIENT_FPUTS(
1772e1dd0a2fSth 		    gettext("Save of system configuration failed.  "
1773e1dd0a2fSth 		    "Attempting recovery.\n"),
1774e1dd0a2fSth 		    stderr);
17757c478bd9Sstevel@tonic-gate 		retcode = recover(STATE_NOSAVE);
17767c478bd9Sstevel@tonic-gate 		if (retcode != CLIENT_SUCCESS) {
17777c478bd9Sstevel@tonic-gate 			CLIENT_FPUTS(
1778e1dd0a2fSth 			    gettext("Recovery of systems configuration "
1779e1dd0a2fSth 			    "failed.  Manual intervention of "
1780e1dd0a2fSth 			    "config files is required.\n"),
1781e1dd0a2fSth 			    stderr);
17827c478bd9Sstevel@tonic-gate 		}
17837c478bd9Sstevel@tonic-gate 
17847c478bd9Sstevel@tonic-gate 		reset_ret = start_services(START_RESET);
17857c478bd9Sstevel@tonic-gate 		if (reset_ret != CLIENT_SUCCESS) {
17867c478bd9Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr, gettext("Error (%d) while "
1787e1dd0a2fSth 			    "starting services during reset\n"),
1788e1dd0a2fSth 			    reset_ret);
17897c478bd9Sstevel@tonic-gate 		}
17907c478bd9Sstevel@tonic-gate 
17917c478bd9Sstevel@tonic-gate 		return (retcode);
17927c478bd9Sstevel@tonic-gate 	}
17937c478bd9Sstevel@tonic-gate 
17947c478bd9Sstevel@tonic-gate 	/* Dump new files */
17957c478bd9Sstevel@tonic-gate 	errorp = __ns_ldap_DumpConfiguration(NSCONFIGFILE);
17967c478bd9Sstevel@tonic-gate 	if (NULL != errorp) {
17977c478bd9Sstevel@tonic-gate 		CLIENT_FPRINTF(stderr,
1798e1dd0a2fSth 		    gettext("%s init: errorp is not NULL; %s\n"),
1799e1dd0a2fSth 		    cmd, errorp->message);
18007c478bd9Sstevel@tonic-gate 		retcode = recover(STATE_NOSAVE);
18017c478bd9Sstevel@tonic-gate 		if (retcode != CLIENT_SUCCESS) {
18027c478bd9Sstevel@tonic-gate 			CLIENT_FPUTS(
1803e1dd0a2fSth 			    gettext("Recovery of systems configuration "
1804e1dd0a2fSth 			    "failed.  Manual intervention of "
1805e1dd0a2fSth 			    "config files is required.\n"),
1806e1dd0a2fSth 			    stderr);
18077c478bd9Sstevel@tonic-gate 			return (CLIENT_ERR_FAIL);
18087c478bd9Sstevel@tonic-gate 		}
18097c478bd9Sstevel@tonic-gate 		(void) __ns_ldap_freeError(&errorp);
18107c478bd9Sstevel@tonic-gate 		reset_ret = start_services(START_RESET);
18117c478bd9Sstevel@tonic-gate 		if (reset_ret != CLIENT_SUCCESS) {
18127c478bd9Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr, gettext("Error (%d) while "
1813e1dd0a2fSth 			    "starting services during reset\n"),
1814e1dd0a2fSth 			    reset_ret);
18157c478bd9Sstevel@tonic-gate 		}
18167c478bd9Sstevel@tonic-gate 		return (CLIENT_ERR_FAIL);
18177c478bd9Sstevel@tonic-gate 	}
18187c478bd9Sstevel@tonic-gate 
18197c478bd9Sstevel@tonic-gate 	/* if (credargs(arglist)) */
18207c478bd9Sstevel@tonic-gate 	errorp = __ns_ldap_DumpConfiguration(NSCREDFILE);
18217c478bd9Sstevel@tonic-gate 	if (NULL != errorp) {
18227c478bd9Sstevel@tonic-gate 		CLIENT_FPRINTF(stderr,
1823e1dd0a2fSth 		    gettext("%s init: errorp is not NULL; %s\n"),
1824e1dd0a2fSth 		    cmd, errorp->message);
18257c478bd9Sstevel@tonic-gate 		retcode = recover(STATE_NOSAVE);
18267c478bd9Sstevel@tonic-gate 		if (retcode != CLIENT_SUCCESS) {
18277c478bd9Sstevel@tonic-gate 			CLIENT_FPUTS(
1828e1dd0a2fSth 			    gettext("Recovery of systems configuration "
1829e1dd0a2fSth 			    "failed.  Manual intervention of "
1830e1dd0a2fSth 			    "config files is required.\n"),
1831e1dd0a2fSth 			    stderr);
18327c478bd9Sstevel@tonic-gate 			return (CLIENT_ERR_FAIL);
18337c478bd9Sstevel@tonic-gate 		}
18347c478bd9Sstevel@tonic-gate 		(void) __ns_ldap_freeError(&errorp);
18357c478bd9Sstevel@tonic-gate 		reset_ret = start_services(START_RESET);
18367c478bd9Sstevel@tonic-gate 		if (reset_ret != CLIENT_SUCCESS) {
18377c478bd9Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr, gettext("Error (%d) while "
1838e1dd0a2fSth 			    "starting services during reset\n"),
1839e1dd0a2fSth 			    reset_ret);
18407c478bd9Sstevel@tonic-gate 		}
18417c478bd9Sstevel@tonic-gate 		return (CLIENT_ERR_FAIL);
18427c478bd9Sstevel@tonic-gate 	}
18437c478bd9Sstevel@tonic-gate 
18447c478bd9Sstevel@tonic-gate 	ret_copy = system(CMD_CP " " NSSWITCH_LDAP " " NSSWITCH_CONF);
18457c478bd9Sstevel@tonic-gate 	if (ret_copy != 0) {
18467c478bd9Sstevel@tonic-gate 		CLIENT_FPRINTF(stderr,
1847e1dd0a2fSth 		    gettext("Error %d copying (%s) -> (%s)\n"),
1848e1dd0a2fSth 		    ret_copy, NSSWITCH_LDAP, NSSWITCH_CONF);
18497c478bd9Sstevel@tonic-gate 		retcode = recover(STATE_NOSAVE);
18507c478bd9Sstevel@tonic-gate 		if (retcode != CLIENT_SUCCESS) {
18517c478bd9Sstevel@tonic-gate 			CLIENT_FPUTS(
1852e1dd0a2fSth 			    gettext("Recovery of systems configuration "
1853e1dd0a2fSth 			    "failed.  Manual intervention of "
1854e1dd0a2fSth 			    "config files is required.\n"),
1855e1dd0a2fSth 			    stderr);
18567c478bd9Sstevel@tonic-gate 		}
18577c478bd9Sstevel@tonic-gate 		reset_ret = start_services(START_RESET);
18587c478bd9Sstevel@tonic-gate 		if (reset_ret != CLIENT_SUCCESS) {
18597c478bd9Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr, gettext("Error (%d) while "
1860e1dd0a2fSth 			    "starting services during reset\n"),
1861e1dd0a2fSth 			    reset_ret);
18627c478bd9Sstevel@tonic-gate 		}
18637c478bd9Sstevel@tonic-gate 		return (CLIENT_ERR_FAIL);
18647c478bd9Sstevel@tonic-gate 	}
18657c478bd9Sstevel@tonic-gate 
18667c478bd9Sstevel@tonic-gate 	if ((profile_fp = open(DOMAINNAME, O_WRONLY|O_CREAT|O_TRUNC,
1867e1dd0a2fSth 	    S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) == -1) { /* 0644 */
18687c478bd9Sstevel@tonic-gate 		CLIENT_FPRINTF(stderr, gettext("Cannot open %s\n"), DOMAINNAME);
18697c478bd9Sstevel@tonic-gate 		retcode = recover(STATE_NOSAVE);
18707c478bd9Sstevel@tonic-gate 		if (retcode != CLIENT_SUCCESS) {
18717c478bd9Sstevel@tonic-gate 			CLIENT_FPUTS(
1872e1dd0a2fSth 			    gettext("Recovery of systems configuration "
1873e1dd0a2fSth 			    "failed.  Manual intervention of "
1874e1dd0a2fSth 			    "config files is required.\n"),
1875e1dd0a2fSth 			    stderr);
18767c478bd9Sstevel@tonic-gate 			return (CLIENT_ERR_FAIL);
18777c478bd9Sstevel@tonic-gate 		}
18787c478bd9Sstevel@tonic-gate 		reset_ret = start_services(START_RESET);
18797c478bd9Sstevel@tonic-gate 		if (reset_ret != CLIENT_SUCCESS) {
18807c478bd9Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr, gettext("Error (%d) while "
1881e1dd0a2fSth 			    "starting services during reset\n"),
1882e1dd0a2fSth 			    reset_ret);
18837c478bd9Sstevel@tonic-gate 		}
18847c478bd9Sstevel@tonic-gate 		return (CLIENT_ERR_FAIL);
18857c478bd9Sstevel@tonic-gate 	}
18867c478bd9Sstevel@tonic-gate 	(void) write(profile_fp, dname, strlen(dname));
18877c478bd9Sstevel@tonic-gate 	(void) write(profile_fp, "\n", 1);
18887c478bd9Sstevel@tonic-gate 	(void) close(profile_fp);
18897c478bd9Sstevel@tonic-gate 
18907c478bd9Sstevel@tonic-gate 	retcode = start_services(START_INIT);
18917c478bd9Sstevel@tonic-gate 
18927c478bd9Sstevel@tonic-gate 	if (retcode == CLIENT_SUCCESS) {
18937c478bd9Sstevel@tonic-gate 		CLIENT_FPUTS(gettext("System successfully configured\n"),
1894e1dd0a2fSth 		    stderr);
18957c478bd9Sstevel@tonic-gate 	} else {
18967c478bd9Sstevel@tonic-gate 		CLIENT_FPUTS(gettext("Error resetting system.\n"
1897e1dd0a2fSth 		    "Recovering old system settings.\n"), stderr),
18987c478bd9Sstevel@tonic-gate 
1899e1dd0a2fSth 		    /* stop any started services for recover */
1900e1dd0a2fSth 		    /* don't stomp on history of saved services state */
1901e1dd0a2fSth 		    reset_ret = stop_services(STATE_NOSAVE);
19027c478bd9Sstevel@tonic-gate 		if (reset_ret != CLIENT_SUCCESS) {
19037c478bd9Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr, gettext("Error (%d) while "
1904e1dd0a2fSth 			    "stopping services during reset\n"),
1905e1dd0a2fSth 			    reset_ret);
19067c478bd9Sstevel@tonic-gate 			/* Coninue and try to recover what we can */
19077c478bd9Sstevel@tonic-gate 		}
19087c478bd9Sstevel@tonic-gate 		reset_ret = recover(STATE_NOSAVE);
19097c478bd9Sstevel@tonic-gate 		if (reset_ret != CLIENT_SUCCESS) {
19107c478bd9Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr, gettext("Error (%d) while "
1911e1dd0a2fSth 			    "recovering service files during "
1912e1dd0a2fSth 			    "reset\n"), reset_ret);
19137c478bd9Sstevel@tonic-gate 			/* Continue and start what we can */
19147c478bd9Sstevel@tonic-gate 		}
19157c478bd9Sstevel@tonic-gate 		reset_ret = start_services(START_RESET);
19167c478bd9Sstevel@tonic-gate 		if (reset_ret != CLIENT_SUCCESS) {
19177c478bd9Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr, gettext("Error (%d) while "
1918e1dd0a2fSth 			    "starting services during reset\n"),
1919e1dd0a2fSth 			    reset_ret);
19207c478bd9Sstevel@tonic-gate 		}
19217c478bd9Sstevel@tonic-gate 	}
19227c478bd9Sstevel@tonic-gate 
19237c478bd9Sstevel@tonic-gate 	return (retcode);
19247c478bd9Sstevel@tonic-gate }
19257c478bd9Sstevel@tonic-gate 
19267c478bd9Sstevel@tonic-gate 
19277c478bd9Sstevel@tonic-gate static void
19287c478bd9Sstevel@tonic-gate usage(void)
19297c478bd9Sstevel@tonic-gate {
19307c478bd9Sstevel@tonic-gate 	if (mode_quiet)
19317c478bd9Sstevel@tonic-gate 		return;
19327c478bd9Sstevel@tonic-gate 
19337c478bd9Sstevel@tonic-gate 	if (gen == 0) {
19347c478bd9Sstevel@tonic-gate 		CLIENT_FPRINTF(stderr,
1935e1dd0a2fSth 		    gettext("Usage: %s [-v | -q] init | manual | mod | "
1936e1dd0a2fSth 		    "list | uninit [<args>]\n"),
1937e1dd0a2fSth 		    cmd);
1938e1dd0a2fSth 
1939e1dd0a2fSth 		CLIENT_FPRINTF(stderr,
1940e1dd0a2fSth 		    gettext("\n       %s [-v | -q] [-a authenticationMethod]"
1941e1dd0a2fSth 		    " [-D bindDN]\n\t[-w bindPassword] [-j passswdFile]"
1942dd1104fbSMichen Chang 		    " [-y proxyPasswordFile]\n\t"
1943dd1104fbSMichen Chang 		    "[-z adminPasswordFile] init [<args>]\n"),
1944e1dd0a2fSth 		    cmd);
19457c478bd9Sstevel@tonic-gate 
19467c478bd9Sstevel@tonic-gate 		CLIENT_FPUTS(
1947e1dd0a2fSth 		    gettext("\nSet up a server or workstation as a "
1948e1dd0a2fSth 		    "client of an LDAP namespace.\n"),
1949e1dd0a2fSth 		    stderr);
19507c478bd9Sstevel@tonic-gate 	} else {	/* genprofile */
19517c478bd9Sstevel@tonic-gate 		CLIENT_FPRINTF(stderr,
1952e1dd0a2fSth 		    gettext("Usage: %s [-v | -q] genprofile "
1953e1dd0a2fSth 		    "-a profileName=<name> "
1954e1dd0a2fSth 		    "-a defaultSearchBase=<base> <args>\n"),
1955e1dd0a2fSth 		    cmd);
19567c478bd9Sstevel@tonic-gate 
19577c478bd9Sstevel@tonic-gate 		CLIENT_FPUTS(
1958e1dd0a2fSth 		    gettext("\nGenerate a profile used to set up clients "
1959e1dd0a2fSth 		    "of an LDAP namespace.\n"),
1960e1dd0a2fSth 		    stderr);
19617c478bd9Sstevel@tonic-gate 	}
19627c478bd9Sstevel@tonic-gate 	CLIENT_FPUTS(
1963e1dd0a2fSth 	    gettext("<args> take the form of \'-a attrName=attrVal\' as "
1964e1dd0a2fSth 	    "described in the\n"),
1965e1dd0a2fSth 	    stderr);
19667c478bd9Sstevel@tonic-gate 	CLIENT_FPUTS(gettext("man page: ldapclient(1M)\n"), stderr);
19677c478bd9Sstevel@tonic-gate }
19687c478bd9Sstevel@tonic-gate 
19697c478bd9Sstevel@tonic-gate 
19707c478bd9Sstevel@tonic-gate /*
19717c478bd9Sstevel@tonic-gate  * stop_services is called to stop network services prior to their
19727c478bd9Sstevel@tonic-gate  * config files being moved/changed.  In case a later recovery is needed
19737c478bd9Sstevel@tonic-gate  * (an error occurs during config), we detect whether the service is
19747c478bd9Sstevel@tonic-gate  * running and store that info so that a reset will only start services
19757c478bd9Sstevel@tonic-gate  * that were stopped here.
19767c478bd9Sstevel@tonic-gate  *
19777c478bd9Sstevel@tonic-gate  * In terms of SMF, this translates to disabling the services. So we
19787c478bd9Sstevel@tonic-gate  * try to disable them if they are in any other state
19797c478bd9Sstevel@tonic-gate  *
19807c478bd9Sstevel@tonic-gate  * Stop order :
19817c478bd9Sstevel@tonic-gate  * sendmail, nscd, autofs, ldap.client, nisd (rpc), inetinit(domainname)
19827c478bd9Sstevel@tonic-gate  */
19837c478bd9Sstevel@tonic-gate static int
19847c478bd9Sstevel@tonic-gate stop_services(int saveState)
19857c478bd9Sstevel@tonic-gate {
19867c478bd9Sstevel@tonic-gate 	int ret;
19877c478bd9Sstevel@tonic-gate 
19887c478bd9Sstevel@tonic-gate 	if (mode_verbose) {
19897c478bd9Sstevel@tonic-gate 		CLIENT_FPUTS(gettext("Stopping network services\n"), stderr);
19907c478bd9Sstevel@tonic-gate 	}
19917c478bd9Sstevel@tonic-gate 
19927c478bd9Sstevel@tonic-gate 	if (!is_service(SENDMAIL_FMRI, SCF_STATE_STRING_DISABLED)) {
19937c478bd9Sstevel@tonic-gate 		if (mode_verbose)
19947c478bd9Sstevel@tonic-gate 			CLIENT_FPUTS(gettext("Stopping sendmail\n"), stderr);
19957c478bd9Sstevel@tonic-gate 		ret = disable_service(SENDMAIL_FMRI, B_TRUE);
19967c478bd9Sstevel@tonic-gate 		if (ret != CLIENT_SUCCESS) {
19977c478bd9Sstevel@tonic-gate 			/* Not serious, but tell user what to do */
19987c478bd9Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr, gettext("Stopping sendmail "
1999e1dd0a2fSth 			    "failed with (%d). You may need to restart "
2000e1dd0a2fSth 			    "it manually for changes to take effect.\n"),
2001e1dd0a2fSth 			    ret);
20027c478bd9Sstevel@tonic-gate 		} else enableFlag |= SENDMAIL_ON;
20037c478bd9Sstevel@tonic-gate 	} else {
20047c478bd9Sstevel@tonic-gate 		if (mode_verbose)
20057c478bd9Sstevel@tonic-gate 			CLIENT_FPUTS(gettext("sendmail not running\n"), stderr);
20067c478bd9Sstevel@tonic-gate 	}
20077c478bd9Sstevel@tonic-gate 
20087c478bd9Sstevel@tonic-gate 	if (!is_service(NSCD_FMRI, SCF_STATE_STRING_DISABLED)) {
20097c478bd9Sstevel@tonic-gate 		if (mode_verbose)
20107c478bd9Sstevel@tonic-gate 			CLIENT_FPUTS(gettext("Stopping nscd\n"), stderr);
20117c478bd9Sstevel@tonic-gate 		ret = disable_service(NSCD_FMRI, B_TRUE);
20127c478bd9Sstevel@tonic-gate 		if (ret != CLIENT_SUCCESS) {
20137c478bd9Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr, gettext("Stopping nscd "
20147c478bd9Sstevel@tonic-gate 			    "failed with (%d)\n"), ret);
20157c478bd9Sstevel@tonic-gate 			return (CLIENT_ERR_FAIL);
20167c478bd9Sstevel@tonic-gate 		} else enableFlag |= NSCD_ON;
20177c478bd9Sstevel@tonic-gate 	} else {
20187c478bd9Sstevel@tonic-gate 		if (mode_verbose)
20197c478bd9Sstevel@tonic-gate 			CLIENT_FPUTS(gettext("nscd not running\n"), stderr);
20207c478bd9Sstevel@tonic-gate 	}
20217c478bd9Sstevel@tonic-gate 
20227c478bd9Sstevel@tonic-gate 	if (!is_service(AUTOFS_FMRI, SCF_STATE_STRING_DISABLED)) {
20237c478bd9Sstevel@tonic-gate 		if (mode_verbose)
20247c478bd9Sstevel@tonic-gate 			CLIENT_FPUTS(gettext("Stopping autofs\n"), stderr);
20257c478bd9Sstevel@tonic-gate 		ret = disable_service(AUTOFS_FMRI, B_TRUE);
20267c478bd9Sstevel@tonic-gate 		if (ret != CLIENT_SUCCESS) {
20277c478bd9Sstevel@tonic-gate 			/* Not serious, but tell user what to do */
20287c478bd9Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr, gettext("Stopping autofs "
2029e1dd0a2fSth 			    "failed with (%d). You may need to restart "
2030e1dd0a2fSth 			    "it manually for changes to take effect.\n"),
2031e1dd0a2fSth 			    ret);
20327c478bd9Sstevel@tonic-gate 		} else enableFlag |= AUTOFS_ON;
20337c478bd9Sstevel@tonic-gate 	} else {
20347c478bd9Sstevel@tonic-gate 		if (mode_verbose)
20357c478bd9Sstevel@tonic-gate 			CLIENT_FPUTS(gettext("autofs not running\n"), stderr);
20367c478bd9Sstevel@tonic-gate 	}
20377c478bd9Sstevel@tonic-gate 
20387c478bd9Sstevel@tonic-gate 	if (!is_service(LDAP_FMRI, SCF_STATE_STRING_DISABLED)) {
20397c478bd9Sstevel@tonic-gate 		if (saveState)
20407c478bd9Sstevel@tonic-gate 			gStartLdap = START_RESET;
20417c478bd9Sstevel@tonic-gate 		if (mode_verbose)
20427c478bd9Sstevel@tonic-gate 			CLIENT_FPUTS(gettext("Stopping ldap\n"), stderr);
20437c478bd9Sstevel@tonic-gate 		ret = disable_service(LDAP_FMRI, B_TRUE);
20447c478bd9Sstevel@tonic-gate 		if (ret != CLIENT_SUCCESS) {
20457c478bd9Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr, gettext("Stopping ldap "
20467c478bd9Sstevel@tonic-gate 			    "failed with (%d)\n"), ret);
20477c478bd9Sstevel@tonic-gate 			return (CLIENT_ERR_FAIL);
20487c478bd9Sstevel@tonic-gate 		}
20497c478bd9Sstevel@tonic-gate 	} else {
20507c478bd9Sstevel@tonic-gate 		if (mode_verbose)
20517c478bd9Sstevel@tonic-gate 			CLIENT_FPUTS(gettext("ldap not running\n"),
2052e1dd0a2fSth 			    stderr);
20537c478bd9Sstevel@tonic-gate 	}
20547c478bd9Sstevel@tonic-gate 
20557c478bd9Sstevel@tonic-gate 	if (!is_service(YP_FMRI, SCF_STATE_STRING_DISABLED)) {
20567c478bd9Sstevel@tonic-gate 		if (saveState)
20577c478bd9Sstevel@tonic-gate 			gStartYp = START_RESET;
20587c478bd9Sstevel@tonic-gate 		if (mode_verbose)
20597c478bd9Sstevel@tonic-gate 			CLIENT_FPUTS(gettext("Stopping nis(yp)\n"), stderr);
20607c478bd9Sstevel@tonic-gate 		ret = disable_service(YP_FMRI, B_TRUE);
20617c478bd9Sstevel@tonic-gate 		if (ret != 0) {
20627c478bd9Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr, gettext("Stopping nis(yp) "
20637c478bd9Sstevel@tonic-gate 			    "failed with (%d)\n"), ret);
20647c478bd9Sstevel@tonic-gate 			return (CLIENT_ERR_FAIL);
20657c478bd9Sstevel@tonic-gate 		}
20667c478bd9Sstevel@tonic-gate 	} else {
20677c478bd9Sstevel@tonic-gate 		if (mode_verbose)
20687c478bd9Sstevel@tonic-gate 			CLIENT_FPUTS(gettext("nis(yp) not running\n"),
2069e1dd0a2fSth 			    stderr);
20707c478bd9Sstevel@tonic-gate 	}
20717c478bd9Sstevel@tonic-gate 
20727c478bd9Sstevel@tonic-gate 	return (CLIENT_SUCCESS);
20737c478bd9Sstevel@tonic-gate }
20747c478bd9Sstevel@tonic-gate 
20757c478bd9Sstevel@tonic-gate /*
20767c478bd9Sstevel@tonic-gate  * start_services is called to start up network services after config
20777c478bd9Sstevel@tonic-gate  * files have all been setup or recovered.  In the case of an error, the
20787c478bd9Sstevel@tonic-gate  * files will be recovered and start_services will be called with the
20797c478bd9Sstevel@tonic-gate  * "reset" flag set so that only those services that were earlier stopped
20807c478bd9Sstevel@tonic-gate  * will be started.  If it is not a reset, then the services associated
20817c478bd9Sstevel@tonic-gate  * with files "recovered" will attempt to be started.
20827c478bd9Sstevel@tonic-gate  */
20837c478bd9Sstevel@tonic-gate static int
20847c478bd9Sstevel@tonic-gate start_services(int flag)
20857c478bd9Sstevel@tonic-gate {
2086cb5caa98Sdjl 	int sysret, retcode = CLIENT_SUCCESS, rc = NS_LDAP_SUCCESS;
20877c478bd9Sstevel@tonic-gate 	FILE *domain_fp;
20887c478bd9Sstevel@tonic-gate 	char domainname[BUFSIZ];
20897c478bd9Sstevel@tonic-gate 	char cmd_domain_start[BUFSIZ];
20907c478bd9Sstevel@tonic-gate 	int domainlen;
2091cb5caa98Sdjl 	ns_ldap_self_gssapi_config_t config = NS_LDAP_SELF_GSSAPI_CONFIG_NONE;
2092cb5caa98Sdjl 	ns_ldap_error_t		*errorp = NULL;
20937c478bd9Sstevel@tonic-gate 
20947c478bd9Sstevel@tonic-gate 	if (mode_verbose) {
20957c478bd9Sstevel@tonic-gate 		CLIENT_FPUTS(gettext("Starting network services\n"), stderr);
20967c478bd9Sstevel@tonic-gate 	}
20977c478bd9Sstevel@tonic-gate 
20987c478bd9Sstevel@tonic-gate 	/* Read in current defaultdomain so we can set it */
20997c478bd9Sstevel@tonic-gate 	domain_fp = fopen(DOMAINNAME, "r");
21007c478bd9Sstevel@tonic-gate 	if (domain_fp == NULL) {
21017c478bd9Sstevel@tonic-gate 		CLIENT_FPRINTF(stderr, gettext("Error opening defaultdomain "
2102e1dd0a2fSth 		    "(%d)\n"), errno);
21037c478bd9Sstevel@tonic-gate 		/* if we did an ldap init, we must have domain */
21047c478bd9Sstevel@tonic-gate 		if (flag == START_INIT)
21057c478bd9Sstevel@tonic-gate 			return (CLIENT_ERR_FAIL);
21067c478bd9Sstevel@tonic-gate 	} else {
21077c478bd9Sstevel@tonic-gate 		if (fgets(domainname, BUFSIZ, domain_fp) == NULL) {
21087c478bd9Sstevel@tonic-gate 			CLIENT_FPUTS(gettext("Error reading defaultdomain\n"),
2109e1dd0a2fSth 			    stderr);
21107c478bd9Sstevel@tonic-gate 			return (CLIENT_ERR_FAIL);
21117c478bd9Sstevel@tonic-gate 		}
21127c478bd9Sstevel@tonic-gate 
21137c478bd9Sstevel@tonic-gate 		if (fclose(domain_fp) != 0) {
21147c478bd9Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr,
2115e1dd0a2fSth 			    gettext("Error closing defaultdomain (%d)\n"),
2116e1dd0a2fSth 			    errno);
21177c478bd9Sstevel@tonic-gate 			return (CLIENT_ERR_FAIL);
21187c478bd9Sstevel@tonic-gate 		}
21197c478bd9Sstevel@tonic-gate 		domainlen = strlen(domainname);
21207c478bd9Sstevel@tonic-gate 		/* sanity check to make sure sprintf will fit */
21217c478bd9Sstevel@tonic-gate 		if (domainlen > (BUFSIZE - sizeof (CMD_DOMAIN_START) -
2122e1dd0a2fSth 		    sizeof (TO_DEV_NULL) - 3)) {
21237c478bd9Sstevel@tonic-gate 			CLIENT_FPUTS(gettext("Specified domainname is "
2124e1dd0a2fSth 			    "too large\n"), stderr);
21257c478bd9Sstevel@tonic-gate 			return (CLIENT_ERR_FAIL);
21267c478bd9Sstevel@tonic-gate 		}
21277c478bd9Sstevel@tonic-gate 		if (domainname[domainlen-1] == '\n')
21287c478bd9Sstevel@tonic-gate 			domainname[domainlen-1] = 0;
21297c478bd9Sstevel@tonic-gate 		/* buffer size is checked above */
2130cb5caa98Sdjl 		(void) snprintf(cmd_domain_start, BUFSIZ, "%s %s %s",
2131e1dd0a2fSth 		    CMD_DOMAIN_START, domainname, TO_DEV_NULL);
21327c478bd9Sstevel@tonic-gate 	}
21337c478bd9Sstevel@tonic-gate 
21347c478bd9Sstevel@tonic-gate 	/*
21357c478bd9Sstevel@tonic-gate 	 * We can be starting services after an init in which case
213636e852a1SRaja Andra 	 * we want to start ldap and not start yp.
21377c478bd9Sstevel@tonic-gate 	 */
21387c478bd9Sstevel@tonic-gate 	if (flag == START_INIT) {
21397c478bd9Sstevel@tonic-gate 		sysret = system(cmd_domain_start);
21407c478bd9Sstevel@tonic-gate 		if (mode_verbose)
21417c478bd9Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr, "start: %s %s... %s\n",
2142e1dd0a2fSth 			    CMD_DOMAIN_START, domainname,
2143e1dd0a2fSth 			    (sysret == 0) ? gettext("success") :
2144e1dd0a2fSth 			    gettext("failed"));
21457c478bd9Sstevel@tonic-gate 		if (sysret != 0) {
21467c478bd9Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr, gettext("\"%s\" returned: %d\n"),
2147e1dd0a2fSth 			    CMD_DOMAIN_START, sysret);
21487c478bd9Sstevel@tonic-gate 
21497c478bd9Sstevel@tonic-gate 			retcode = CLIENT_ERR_FAIL;
21507c478bd9Sstevel@tonic-gate 		}
21517c478bd9Sstevel@tonic-gate 
2152cb5caa98Sdjl 		if ((rc = __ns_ldap_self_gssapi_config(&config)) !=
2153e1dd0a2fSth 		    NS_LDAP_SUCCESS) {
2154cb5caa98Sdjl 			CLIENT_FPRINTF(stderr, gettext("Error (%d) while "
2155e1dd0a2fSth 			    "checking sasl/GSSAPI configuration\n"),
2156e1dd0a2fSth 			    rc);
21577c478bd9Sstevel@tonic-gate 			retcode = CLIENT_ERR_FAIL;
2158cb5caa98Sdjl 		}
21597c478bd9Sstevel@tonic-gate 
2160cb5caa98Sdjl 		if (config != NS_LDAP_SELF_GSSAPI_CONFIG_NONE) {
2161cb5caa98Sdjl 
2162cb5caa98Sdjl 			rc = __ns_ldap_check_dns_preq(
2163e1dd0a2fSth 			    1, mode_verbose, mode_quiet,
2164e1dd0a2fSth 			    NSSWITCH_LDAP, config, &errorp);
2165cb5caa98Sdjl 			if (errorp)
2166cb5caa98Sdjl 				(void) __ns_ldap_freeError(&errorp);
2167cb5caa98Sdjl 
2168cb5caa98Sdjl 			if (rc != NS_LDAP_SUCCESS)
2169cb5caa98Sdjl 				retcode = CLIENT_ERR_FAIL;
2170cb5caa98Sdjl 		}
2171cb5caa98Sdjl 
2172cb5caa98Sdjl 		if (rc == NS_LDAP_SUCCESS &&
2173e1dd0a2fSth 		    start_service(LDAP_FMRI, B_TRUE) != CLIENT_SUCCESS)
2174cb5caa98Sdjl 			retcode = CLIENT_ERR_FAIL;
2175cb5caa98Sdjl 
2176cb5caa98Sdjl 		if (config != NS_LDAP_SELF_GSSAPI_CONFIG_NONE &&
2177e1dd0a2fSth 		    rc == NS_LDAP_SUCCESS && retcode == CLIENT_SUCCESS) {
2178cb5caa98Sdjl 			rc = __ns_ldap_check_gssapi_preq(
2179e1dd0a2fSth 			    1, mode_verbose, mode_quiet, config,
2180e1dd0a2fSth 			    &errorp);
2181cb5caa98Sdjl 			if (errorp)
2182cb5caa98Sdjl 				(void) __ns_ldap_freeError(&errorp);
2183cb5caa98Sdjl 
2184cb5caa98Sdjl 			if (rc != NS_LDAP_SUCCESS)
2185cb5caa98Sdjl 				retcode = CLIENT_ERR_FAIL;
2186cb5caa98Sdjl 
2187cb5caa98Sdjl 		}
218836e852a1SRaja Andra 		/* No YP after init */
21897c478bd9Sstevel@tonic-gate 	/*
21907c478bd9Sstevel@tonic-gate 	 * Or we can be starting services after an uninit or error
21917c478bd9Sstevel@tonic-gate 	 * recovery.  We want to start whatever services were running
21927c478bd9Sstevel@tonic-gate 	 * before.  In the case of error recovery, it is the services
21937c478bd9Sstevel@tonic-gate 	 * that were running before we stopped them (flags set in
21947c478bd9Sstevel@tonic-gate 	 * stop_services).  If it is an uninit then we determine
21957c478bd9Sstevel@tonic-gate 	 * which services to start based on the files we recovered
21967c478bd9Sstevel@tonic-gate 	 * (flags set in recover).
21977c478bd9Sstevel@tonic-gate 	 */
21987c478bd9Sstevel@tonic-gate 	} else {
21997c478bd9Sstevel@tonic-gate 		/* uninit and recover should set flags of what to start */
22007c478bd9Sstevel@tonic-gate 		if (domain_fp) {
22017c478bd9Sstevel@tonic-gate 			sysret = system(cmd_domain_start);
22027c478bd9Sstevel@tonic-gate 			if (mode_verbose)
22037c478bd9Sstevel@tonic-gate 				CLIENT_FPRINTF(stderr, "start: %s %s... %s\n",
2204e1dd0a2fSth 				    CMD_DOMAIN_START, domainname,
2205e1dd0a2fSth 				    (sysret == 0) ? gettext("success") :
2206e1dd0a2fSth 				    gettext("failed"));
22077c478bd9Sstevel@tonic-gate 			if (sysret != 0) {
22087c478bd9Sstevel@tonic-gate 				CLIENT_FPRINTF(stderr, gettext("\"%s\" "
2209e1dd0a2fSth 				    "returned: %d\n"),
2210e1dd0a2fSth 				    CMD_DOMAIN_START, sysret);
22117c478bd9Sstevel@tonic-gate 
22127c478bd9Sstevel@tonic-gate 				retcode = CLIENT_ERR_FAIL;
22137c478bd9Sstevel@tonic-gate 			}
22147c478bd9Sstevel@tonic-gate 		}
22157c478bd9Sstevel@tonic-gate 
22167c478bd9Sstevel@tonic-gate 		if (gStartLdap == flag) {
22177c478bd9Sstevel@tonic-gate 			if (!(is_service(LDAP_FMRI, SCF_STATE_STRING_ONLINE)))
22187c478bd9Sstevel@tonic-gate 				if (start_service(LDAP_FMRI, B_TRUE)
2219e1dd0a2fSth 				    != CLIENT_SUCCESS)
22207c478bd9Sstevel@tonic-gate 					retcode = CLIENT_ERR_FAIL;
22217c478bd9Sstevel@tonic-gate 		}
22227c478bd9Sstevel@tonic-gate 
22237c478bd9Sstevel@tonic-gate 		if (gStartYp == flag) {
22247c478bd9Sstevel@tonic-gate 			if (!(is_service(YP_FMRI, SCF_STATE_STRING_ONLINE)))
22257c478bd9Sstevel@tonic-gate 				(void) start_service(YP_FMRI, B_TRUE);
22267c478bd9Sstevel@tonic-gate 		}
22277c478bd9Sstevel@tonic-gate 	}
22287c478bd9Sstevel@tonic-gate 	if ((enableFlag & AUTOFS_ON) &&
22297c478bd9Sstevel@tonic-gate 	    !(is_service(AUTOFS_FMRI, SCF_STATE_STRING_ONLINE)))
22307c478bd9Sstevel@tonic-gate 		(void) start_service(AUTOFS_FMRI, B_TRUE);
22317c478bd9Sstevel@tonic-gate 
22327c478bd9Sstevel@tonic-gate 	if ((enableFlag & NSCD_ON) &&
22337c478bd9Sstevel@tonic-gate 	    !(is_service(NSCD_FMRI, SCF_STATE_STRING_ONLINE)))
22347c478bd9Sstevel@tonic-gate 		(void) start_service(NSCD_FMRI, B_TRUE);
22357c478bd9Sstevel@tonic-gate 
2236cb5caa98Sdjl #if 0
2237cb5caa98Sdjl 	if (flag == START_INIT && config != NS_LDAP_SELF_GSSAPI_CONFIG_NONE &&
2238cb5caa98Sdjl 	    retcode == CLIENT_SUCCESS &&
2239cb5caa98Sdjl 	    !(is_service(NSCD_FMRI, SCF_STATE_STRING_ONLINE))) {
2240cb5caa98Sdjl 		CLIENT_FPRINTF(stderr, "start: %s\n",
2241e1dd0a2fSth 		    gettext("self/sasl/GSSAPI is configured"
2242e1dd0a2fSth 		    " but nscd is not online"));
2243cb5caa98Sdjl 		retcode = CLIENT_ERR_FAIL;
2244cb5caa98Sdjl 	}
2245cb5caa98Sdjl #endif
2246cb5caa98Sdjl 
22477c478bd9Sstevel@tonic-gate 	if ((enableFlag & SENDMAIL_ON) &&
22487c478bd9Sstevel@tonic-gate 	    !(is_service(SENDMAIL_FMRI, SCF_STATE_STRING_ONLINE)))
22497c478bd9Sstevel@tonic-gate 		(void) start_service(SENDMAIL_FMRI, B_TRUE);
22507c478bd9Sstevel@tonic-gate 
22517c478bd9Sstevel@tonic-gate 	/*
22527c478bd9Sstevel@tonic-gate 	 * Restart name-service milestone so that any consumer
22537c478bd9Sstevel@tonic-gate 	 * which depends on it will be restarted.
22547c478bd9Sstevel@tonic-gate 	 */
22557c478bd9Sstevel@tonic-gate 	(void) restart_service(NS_MILESTONE_FMRI, B_TRUE);
22567c478bd9Sstevel@tonic-gate 	return (retcode);
22577c478bd9Sstevel@tonic-gate }
22587c478bd9Sstevel@tonic-gate 
22597c478bd9Sstevel@tonic-gate /*
22607c478bd9Sstevel@tonic-gate  * credCheck is called to check if credentials are required for this
22617c478bd9Sstevel@tonic-gate  * configuration.  Currently, this means that if any credentialLevel is
22627c478bd9Sstevel@tonic-gate  * proxy and any authenticationMethod is something other than none, then
22637c478bd9Sstevel@tonic-gate  * credential info is required (proxyDN and proxyPassword).
22647c478bd9Sstevel@tonic-gate  */
22657c478bd9Sstevel@tonic-gate static int
22667c478bd9Sstevel@tonic-gate credCheck(clientopts_t *arglist)
22677c478bd9Sstevel@tonic-gate {
22687c478bd9Sstevel@tonic-gate 	int counter;
22697c478bd9Sstevel@tonic-gate 	int **credLevel;
22707c478bd9Sstevel@tonic-gate 	ns_auth_t **authMethod;
22717c478bd9Sstevel@tonic-gate 	char **proxyDN, **proxyPassword;
22727c478bd9Sstevel@tonic-gate 	ns_ldap_error_t *errorp;
22737c478bd9Sstevel@tonic-gate 	int credProxy, authNotNone;
22747c478bd9Sstevel@tonic-gate 	int retcode;
22757c478bd9Sstevel@tonic-gate 
22767c478bd9Sstevel@tonic-gate /* If credentialLevel is proxy, make sure we have proxyDN and proxyPassword */
22777c478bd9Sstevel@tonic-gate 	retcode = __ns_ldap_getParam(NS_LDAP_CREDENTIAL_LEVEL_P,
2278e1dd0a2fSth 	    (void ***)&credLevel, &errorp);
22797c478bd9Sstevel@tonic-gate 	if (retcode != 0) {
22807c478bd9Sstevel@tonic-gate 		CLIENT_FPRINTF(stderr,
2281e1dd0a2fSth 		    gettext("Error %d while trying to retrieve "
2282e1dd0a2fSth 		    "credLevel\n"),
2283e1dd0a2fSth 		    retcode);
22847c478bd9Sstevel@tonic-gate 		return (CLIENT_ERR_FAIL);
22857c478bd9Sstevel@tonic-gate 	}
22867c478bd9Sstevel@tonic-gate 	retcode = __ns_ldap_getParam(NS_LDAP_AUTH_P,
2287e1dd0a2fSth 	    (void ***)&authMethod, &errorp);
22887c478bd9Sstevel@tonic-gate 	if (retcode != 0) {
22897c478bd9Sstevel@tonic-gate 		CLIENT_FPRINTF(stderr,
2290e1dd0a2fSth 		    gettext("Error %d while trying to retrieve "
2291e1dd0a2fSth 		    "authMethod\n"), retcode);
22927c478bd9Sstevel@tonic-gate 		return (CLIENT_ERR_FAIL);
22937c478bd9Sstevel@tonic-gate 	}
22947c478bd9Sstevel@tonic-gate 	retcode = __ns_ldap_getParam(NS_LDAP_BINDDN_P,
2295e1dd0a2fSth 	    (void ***)&proxyDN, &errorp);
22967c478bd9Sstevel@tonic-gate 	if (retcode != 0) {
22977c478bd9Sstevel@tonic-gate 		CLIENT_FPRINTF(stderr,
2298e1dd0a2fSth 		    gettext("Error %d while trying to retrieve proxyDN\n"),
2299e1dd0a2fSth 		    retcode);
23007c478bd9Sstevel@tonic-gate 		return (CLIENT_ERR_FAIL);
23017c478bd9Sstevel@tonic-gate 	}
23027c478bd9Sstevel@tonic-gate 	retcode = __ns_ldap_getParam(NS_LDAP_BINDPASSWD_P,
2303e1dd0a2fSth 	    (void ***)&proxyPassword, &errorp);
23047c478bd9Sstevel@tonic-gate 	if (retcode != 0) {
23057c478bd9Sstevel@tonic-gate 		CLIENT_FPRINTF(stderr,
2306e1dd0a2fSth 		    gettext("Error %d while trying to retrieve "
2307e1dd0a2fSth 		    "proxyPassword\n"), retcode);
23087c478bd9Sstevel@tonic-gate 		return (CLIENT_ERR_FAIL);
23097c478bd9Sstevel@tonic-gate 	}
23107c478bd9Sstevel@tonic-gate 
23117c478bd9Sstevel@tonic-gate 	if (mode_verbose) {
23127c478bd9Sstevel@tonic-gate 		CLIENT_FPRINTF(stderr,
2313e1dd0a2fSth 		    gettext("Proxy DN: %s\n"),
2314e1dd0a2fSth 		    (proxyDN && proxyDN[0]) ? proxyDN[0] : "NULL");
23157c478bd9Sstevel@tonic-gate 		CLIENT_FPRINTF(stderr,
2316e1dd0a2fSth 		    gettext("Proxy password: %s\n"),
2317e1dd0a2fSth 		    (proxyPassword && proxyPassword[0]) ?
2318e1dd0a2fSth 		    proxyPassword[0] : "NULL");
23197c478bd9Sstevel@tonic-gate 	}
23207c478bd9Sstevel@tonic-gate 
23217c478bd9Sstevel@tonic-gate 	credProxy = 0;	/* flag to indicate if we have a credLevel of proxy */
23227c478bd9Sstevel@tonic-gate 	for (counter = 0; credLevel && credLevel[counter] != NULL; counter++) {
23237c478bd9Sstevel@tonic-gate 		if (mode_verbose)
23247c478bd9Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr,
2325e1dd0a2fSth 			    gettext("Credential level: %d\n"),
2326e1dd0a2fSth 			    *credLevel[counter]);
23277c478bd9Sstevel@tonic-gate 		if (*credLevel[counter] == NS_LDAP_CRED_PROXY) {
23287c478bd9Sstevel@tonic-gate 			credProxy = 1;
23297c478bd9Sstevel@tonic-gate 			break;
23307c478bd9Sstevel@tonic-gate 		}
23317c478bd9Sstevel@tonic-gate 	}
23327c478bd9Sstevel@tonic-gate 
23337c478bd9Sstevel@tonic-gate 	authNotNone = 0;	/* flag for authMethod other than none */
23347c478bd9Sstevel@tonic-gate 	for (counter = 0;
2335e1dd0a2fSth 	    authMethod && authMethod[counter] != NULL;
2336e1dd0a2fSth 	    counter++) {
23377c478bd9Sstevel@tonic-gate 
23387c478bd9Sstevel@tonic-gate 		if (mode_verbose)
23397c478bd9Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr,
2340e1dd0a2fSth 			    gettext("Authentication method: %d\n"),
2341e1dd0a2fSth 			    authMethod[counter]->type);
23427c478bd9Sstevel@tonic-gate 		if (authMethod[counter]->type != NS_LDAP_AUTH_NONE &&
23437c478bd9Sstevel@tonic-gate 		    !(authMethod[counter]->type == NS_LDAP_AUTH_TLS &&
23447c478bd9Sstevel@tonic-gate 		    authMethod[counter]->tlstype == NS_LDAP_TLS_NONE)) {
23457c478bd9Sstevel@tonic-gate 			authNotNone = 1;
23467c478bd9Sstevel@tonic-gate 			break;
23477c478bd9Sstevel@tonic-gate 		}
23487c478bd9Sstevel@tonic-gate 	}
23497c478bd9Sstevel@tonic-gate 
23507c478bd9Sstevel@tonic-gate 	/* First, if we don't need proxyDN/Password then just return ok */
23517c478bd9Sstevel@tonic-gate 	if (!(credProxy && authNotNone)) {
23527c478bd9Sstevel@tonic-gate 		if (mode_verbose)
23537c478bd9Sstevel@tonic-gate 			CLIENT_FPUTS(
2354e1dd0a2fSth 			    gettext("No proxyDN/proxyPassword required\n"),
2355e1dd0a2fSth 			    stderr);
23567c478bd9Sstevel@tonic-gate 		return (CLIENT_SUCCESS);
23577c478bd9Sstevel@tonic-gate 	}
23587c478bd9Sstevel@tonic-gate 
23597c478bd9Sstevel@tonic-gate 	/* Now let's check if we have the cred stuff we need */
23607c478bd9Sstevel@tonic-gate 	if (!proxyDN || !proxyDN[0]) {
23617c478bd9Sstevel@tonic-gate 		CLIENT_FPUTS(
2362e1dd0a2fSth 		    gettext("credentialLevel is proxy and no proxyDN "
2363e1dd0a2fSth 		    "specified\n"),
2364e1dd0a2fSth 		    stderr);
23657c478bd9Sstevel@tonic-gate 		return (CLIENT_ERR_CREDENTIAL);
23667c478bd9Sstevel@tonic-gate 	}
23677c478bd9Sstevel@tonic-gate 
23687c478bd9Sstevel@tonic-gate 	/* If we need proxyPassword (prompt) */
23697c478bd9Sstevel@tonic-gate 	if (!proxyPassword || !proxyPassword[0]) {
23707c478bd9Sstevel@tonic-gate 		CLIENT_FPUTS(
2371e1dd0a2fSth 		    gettext("credentialLevel requires proxyPassword\n"),
2372e1dd0a2fSth 		    stderr);
23737c478bd9Sstevel@tonic-gate 		arglist->proxyPassword = getpassphrase("Proxy Bind Password:");
23747c478bd9Sstevel@tonic-gate 		if (arglist->proxyPassword == NULL) {
23757c478bd9Sstevel@tonic-gate 			CLIENT_FPUTS(gettext("Get password failed\n"), stderr);
23767c478bd9Sstevel@tonic-gate 			return (CLIENT_ERR_CREDENTIAL);
23777c478bd9Sstevel@tonic-gate 		}
23787c478bd9Sstevel@tonic-gate 		LDAP_SET_PARAM(arglist->proxyPassword, NS_LDAP_BINDPASSWD_P);
23797c478bd9Sstevel@tonic-gate 		if (retcode != 0) {
23807c478bd9Sstevel@tonic-gate 			CLIENT_FPUTS(
2381e1dd0a2fSth 			    gettext("setParam proxyPassword failed.\n"),
2382e1dd0a2fSth 			    stderr);
23837c478bd9Sstevel@tonic-gate 			return (CLIENT_ERR_CREDENTIAL);
23847c478bd9Sstevel@tonic-gate 		}
23857c478bd9Sstevel@tonic-gate 	}
23867c478bd9Sstevel@tonic-gate 
23877c478bd9Sstevel@tonic-gate 	return (CLIENT_SUCCESS);
23887c478bd9Sstevel@tonic-gate }
23897c478bd9Sstevel@tonic-gate 
2390dd1104fbSMichen Chang /*
2391dd1104fbSMichen Chang  * adminCredCheck is called to check if the admin credential is required
2392dd1104fbSMichen Chang  * for this configuration. This means that if enableShadowUpdate is set
2393dd1104fbSMichen Chang  * to TRUE then credential info is required (adminDN and adminPassword).
2394dd1104fbSMichen Chang  * One exception is that if there is a 'self' credentialLevel and
2395dd1104fbSMichen Chang  * 'sasl/GSSAPI' authenticationMethod (i.e., possibly using Kerberos
2396dd1104fbSMichen Chang  * host credential) then adminDN and adminPassword are not required.
2397dd1104fbSMichen Chang  */
2398dd1104fbSMichen Chang static int
2399dd1104fbSMichen Chang adminCredCheck(clientopts_t *arglist)
2400dd1104fbSMichen Chang {
2401dd1104fbSMichen Chang 	int counter;
2402dd1104fbSMichen Chang 	int **enabled = NULL;
2403dd1104fbSMichen Chang 	int **credLevel = NULL;
2404dd1104fbSMichen Chang 	char **adminDN = NULL;
2405dd1104fbSMichen Chang 	char **adminPassword = NULL;
2406dd1104fbSMichen Chang 	ns_auth_t **authMethod = NULL;
2407dd1104fbSMichen Chang 	ns_ldap_error_t *errorp = NULL;
2408dd1104fbSMichen Chang 	int credSelf, authSASLgss;
2409dd1104fbSMichen Chang 	int retcode, rc;
2410dd1104fbSMichen Chang 
2411dd1104fbSMichen Chang 	/* If shadow update not enabled, then no need to check */
2412dd1104fbSMichen Chang 	retcode = __ns_ldap_getParam(NS_LDAP_ENABLE_SHADOW_UPDATE_P,
2413dd1104fbSMichen Chang 	    (void ***)&enabled, &errorp);
2414dd1104fbSMichen Chang 	if (retcode != 0) {
2415dd1104fbSMichen Chang 		CLIENT_FPRINTF(stderr,
2416dd1104fbSMichen Chang 		    gettext("Error %d while trying to retrieve "
2417dd1104fbSMichen Chang 		    "enableShadowUpdate\n"), retcode);
2418dd1104fbSMichen Chang 		rc = CLIENT_ERR_FAIL;
2419dd1104fbSMichen Chang 		goto out;
2420dd1104fbSMichen Chang 	}
2421dd1104fbSMichen Chang 	if (enabled == NULL ||
2422dd1104fbSMichen Chang 	    *enabled[0] != NS_LDAP_ENABLE_SHADOW_UPDATE_TRUE) {
2423dd1104fbSMichen Chang 		if (mode_verbose)
2424dd1104fbSMichen Chang 			CLIENT_FPUTS(
2425dd1104fbSMichen Chang 			    gettext("Shadow Update is not enabled, "
2426dd1104fbSMichen Chang 			    "no adminDN/adminPassword is required.\n"), stderr);
2427dd1104fbSMichen Chang 		rc = CLIENT_SUCCESS;
2428dd1104fbSMichen Chang 		goto out;
2429dd1104fbSMichen Chang 	}
2430dd1104fbSMichen Chang 
2431dd1104fbSMichen Chang 	/* get credentialLevel */
2432dd1104fbSMichen Chang 	retcode = __ns_ldap_getParam(NS_LDAP_CREDENTIAL_LEVEL_P,
2433dd1104fbSMichen Chang 	    (void ***)&credLevel, &errorp);
2434dd1104fbSMichen Chang 	if (retcode != 0) {
2435dd1104fbSMichen Chang 		CLIENT_FPRINTF(stderr,
2436dd1104fbSMichen Chang 		    gettext("Error %d while trying to retrieve credLevel\n"),
2437dd1104fbSMichen Chang 		    retcode);
2438dd1104fbSMichen Chang 		rc = CLIENT_ERR_FAIL;
2439dd1104fbSMichen Chang 		goto out;
2440dd1104fbSMichen Chang 	}
2441dd1104fbSMichen Chang 
2442dd1104fbSMichen Chang 	/* get AuthenticationMethod */
2443dd1104fbSMichen Chang 	retcode = __ns_ldap_getParam(NS_LDAP_AUTH_P,
2444dd1104fbSMichen Chang 	    (void ***)&authMethod, &errorp);
2445dd1104fbSMichen Chang 	if (retcode != 0) {
2446dd1104fbSMichen Chang 		CLIENT_FPRINTF(stderr,
2447dd1104fbSMichen Chang 		    gettext("Error %d while trying to retrieve authMethod\n"),
2448dd1104fbSMichen Chang 		    retcode);
2449dd1104fbSMichen Chang 		rc = CLIENT_ERR_FAIL;
2450dd1104fbSMichen Chang 		goto out;
2451dd1104fbSMichen Chang 	}
2452dd1104fbSMichen Chang 
2453dd1104fbSMichen Chang 	/* get adminDN */
2454dd1104fbSMichen Chang 	retcode = __ns_ldap_getParam(NS_LDAP_ADMIN_BINDDN_P,
2455dd1104fbSMichen Chang 	    (void ***)&adminDN, &errorp);
2456dd1104fbSMichen Chang 	if (retcode != 0) {
2457dd1104fbSMichen Chang 		CLIENT_FPRINTF(stderr,
2458dd1104fbSMichen Chang 		    gettext("Error %d while trying to retrieve adminDN\n"),
2459dd1104fbSMichen Chang 		    retcode);
2460dd1104fbSMichen Chang 		rc = CLIENT_ERR_FAIL;
2461dd1104fbSMichen Chang 		goto out;
2462dd1104fbSMichen Chang 	}
2463dd1104fbSMichen Chang 
2464dd1104fbSMichen Chang 	/* get adminPassword */
2465dd1104fbSMichen Chang 	retcode = __ns_ldap_getParam(NS_LDAP_ADMIN_BINDPASSWD_P,
2466dd1104fbSMichen Chang 	    (void ***)&adminPassword, &errorp);
2467dd1104fbSMichen Chang 	if (retcode != 0) {
2468dd1104fbSMichen Chang 		CLIENT_FPRINTF(stderr,
2469dd1104fbSMichen Chang 		    gettext("Error %d while trying to retrieve "
2470dd1104fbSMichen Chang 		    "adminPassword\n"), retcode);
2471dd1104fbSMichen Chang 		rc = CLIENT_ERR_FAIL;
2472dd1104fbSMichen Chang 		goto out;
2473dd1104fbSMichen Chang 	}
2474dd1104fbSMichen Chang 
2475dd1104fbSMichen Chang 	if (mode_verbose) {
2476dd1104fbSMichen Chang 		CLIENT_FPRINTF(stderr,
2477dd1104fbSMichen Chang 		    gettext("admin DN: %s\n"),
2478dd1104fbSMichen Chang 		    (adminDN && adminDN[0]) ? adminDN[0] : "NULL");
2479dd1104fbSMichen Chang 		CLIENT_FPRINTF(stderr,
2480dd1104fbSMichen Chang 		    gettext("admin password: %s\n"),
2481dd1104fbSMichen Chang 		    (adminPassword && adminPassword[0]) ?
2482dd1104fbSMichen Chang 		    adminPassword[0] : "NULL");
2483dd1104fbSMichen Chang 	}
2484dd1104fbSMichen Chang 
2485dd1104fbSMichen Chang 	credSelf = 0;	/* flag to indicate if we have a credLevel of self */
2486dd1104fbSMichen Chang 	for (counter = 0; credLevel && credLevel[counter] != NULL; counter++) {
2487dd1104fbSMichen Chang 		if (mode_verbose)
2488dd1104fbSMichen Chang 			CLIENT_FPRINTF(stderr,
2489dd1104fbSMichen Chang 			    gettext("Credential level: %d\n"),
2490dd1104fbSMichen Chang 			    *credLevel[counter]);
2491dd1104fbSMichen Chang 		if (*credLevel[counter] == NS_LDAP_CRED_SELF) {
2492dd1104fbSMichen Chang 			credSelf = 1;
2493dd1104fbSMichen Chang 			break;
2494dd1104fbSMichen Chang 		}
2495dd1104fbSMichen Chang 	}
2496dd1104fbSMichen Chang 
2497dd1104fbSMichen Chang 	authSASLgss = 0;	/* flag for authMethod of SASL/gssapi */
2498dd1104fbSMichen Chang 	for (counter = 0;
2499dd1104fbSMichen Chang 	    authMethod && authMethod[counter] != NULL;
2500dd1104fbSMichen Chang 	    counter++) {
2501dd1104fbSMichen Chang 
2502dd1104fbSMichen Chang 		if (mode_verbose)
2503dd1104fbSMichen Chang 			CLIENT_FPRINTF(stderr,
2504dd1104fbSMichen Chang 			    gettext("Authentication sasl mechanism: %d\n"),
2505dd1104fbSMichen Chang 			    authMethod[counter]->saslmech);
2506dd1104fbSMichen Chang 		if (authMethod[counter]->saslmech == NS_LDAP_SASL_GSSAPI) {
2507dd1104fbSMichen Chang 			authSASLgss = 1;
2508dd1104fbSMichen Chang 			break;
2509dd1104fbSMichen Chang 		}
2510dd1104fbSMichen Chang 	}
2511dd1104fbSMichen Chang 
2512dd1104fbSMichen Chang 	/* First, if we don't need adminDN/adminPassword then just return ok */
2513dd1104fbSMichen Chang 	if (credSelf && authSASLgss) {
2514dd1104fbSMichen Chang 		if (mode_verbose)
2515dd1104fbSMichen Chang 			CLIENT_FPUTS(
2516dd1104fbSMichen Chang 			    gettext("A credential Level of self and an "
2517dd1104fbSMichen Chang 			    "authentication method of sasl/GSSAPI is "
2518dd1104fbSMichen Chang 			    "configured, no adminDN/adminPassword "
2519dd1104fbSMichen Chang 			    "is required.\n"), stderr);
2520dd1104fbSMichen Chang 		rc = CLIENT_SUCCESS;
2521dd1104fbSMichen Chang 		goto out;
2522dd1104fbSMichen Chang 	}
2523dd1104fbSMichen Chang 
2524dd1104fbSMichen Chang 	/* Now let's check if we have the cred stuff we need */
2525dd1104fbSMichen Chang 	if (adminDN == NULL || adminDN[0] == '\0') {
2526dd1104fbSMichen Chang 		CLIENT_FPUTS(
2527dd1104fbSMichen Chang 		    gettext("Shadow Update is enabled, but "
2528dd1104fbSMichen Chang 		    "no adminDN is configured.\n"), stderr);
2529dd1104fbSMichen Chang 		rc = CLIENT_ERR_CREDENTIAL;
2530dd1104fbSMichen Chang 		goto out;
2531dd1104fbSMichen Chang 	}
2532dd1104fbSMichen Chang 
2533dd1104fbSMichen Chang 	/* If we need adminPassword (prompt) */
2534dd1104fbSMichen Chang 	if (adminPassword == NULL || adminPassword[0] == '\0') {
2535dd1104fbSMichen Chang 		CLIENT_FPUTS(
2536dd1104fbSMichen Chang 		    gettext("Shadow Update requires adminPassword\n"),
2537dd1104fbSMichen Chang 		    stderr);
2538dd1104fbSMichen Chang 		arglist->adminPassword = getpassphrase("admin Password:");
2539dd1104fbSMichen Chang 		if (arglist->adminPassword == NULL) {
2540dd1104fbSMichen Chang 			CLIENT_FPUTS(gettext("Unable to get admin password\n"),
2541dd1104fbSMichen Chang 			    stderr);
2542dd1104fbSMichen Chang 			rc = CLIENT_ERR_CREDENTIAL;
2543dd1104fbSMichen Chang 			goto out;
2544dd1104fbSMichen Chang 		}
2545dd1104fbSMichen Chang 		LDAP_SET_PARAM(arglist->adminPassword,
2546dd1104fbSMichen Chang 		    NS_LDAP_ADMIN_BINDPASSWD_P);
2547dd1104fbSMichen Chang 		if (retcode != 0) {
2548dd1104fbSMichen Chang 			CLIENT_FPUTS(
2549dd1104fbSMichen Chang 			    gettext("setParam adminPassword failed.\n"),
2550dd1104fbSMichen Chang 			    stderr);
2551dd1104fbSMichen Chang 			rc = CLIENT_ERR_CREDENTIAL;
2552dd1104fbSMichen Chang 			goto out;
2553dd1104fbSMichen Chang 		}
2554dd1104fbSMichen Chang 	}
2555dd1104fbSMichen Chang 
2556dd1104fbSMichen Chang 	rc = CLIENT_SUCCESS;
2557dd1104fbSMichen Chang 
2558dd1104fbSMichen Chang 	out:
2559dd1104fbSMichen Chang 	if (enabled != NULL)
2560dd1104fbSMichen Chang 		(void) __ns_ldap_freeParam((void ***)&enabled);
2561dd1104fbSMichen Chang 	if (credLevel != NULL)
2562dd1104fbSMichen Chang 		(void) __ns_ldap_freeParam((void ***)&credLevel);
2563dd1104fbSMichen Chang 	if (authMethod != NULL)
2564dd1104fbSMichen Chang 		(void) __ns_ldap_freeParam((void ***)&authMethod);
2565dd1104fbSMichen Chang 	if (adminDN != NULL)
2566dd1104fbSMichen Chang 		(void) __ns_ldap_freeParam((void ***)&adminDN);
2567dd1104fbSMichen Chang 	if (adminPassword != NULL)
2568dd1104fbSMichen Chang 		(void) __ns_ldap_freeParam((void ***)&adminPassword);
2569dd1104fbSMichen Chang 
2570dd1104fbSMichen Chang 	return (rc);
2571dd1104fbSMichen Chang }
2572dd1104fbSMichen Chang 
25737c478bd9Sstevel@tonic-gate /*
25747c478bd9Sstevel@tonic-gate  * try to restore the previous name space on this machine
25757c478bd9Sstevel@tonic-gate  */
25767c478bd9Sstevel@tonic-gate static int
25777c478bd9Sstevel@tonic-gate recover(int saveState)
25787c478bd9Sstevel@tonic-gate {
25797c478bd9Sstevel@tonic-gate 	struct stat buf;
25807c478bd9Sstevel@tonic-gate 	int stat_ret, retcode, fd;
25817c478bd9Sstevel@tonic-gate 	int domain = 0, domainlen;
25827c478bd9Sstevel@tonic-gate 	char yp_dir[BUFSIZE], yp_dir_back[BUFSIZE];
25837c478bd9Sstevel@tonic-gate 	char name[BUFSIZ];
25847c478bd9Sstevel@tonic-gate 	char *ldap_conf_file, *ldap_cred_file;
25857c478bd9Sstevel@tonic-gate 	char ldap_file_back[BUFSIZE], ldap_cred_back[BUFSIZE];
25867c478bd9Sstevel@tonic-gate 
25877c478bd9Sstevel@tonic-gate 	/* If running as Sysid Install become a no-op */
25887c478bd9Sstevel@tonic-gate 	if (sysid_install == B_TRUE)
25897c478bd9Sstevel@tonic-gate 		return (CLIENT_SUCCESS);
25907c478bd9Sstevel@tonic-gate 
25917c478bd9Sstevel@tonic-gate 	stat_ret = stat(LDAP_RESTORE_DIR, &buf);
25927c478bd9Sstevel@tonic-gate 	if (stat_ret != 0) {
25937c478bd9Sstevel@tonic-gate 		CLIENT_FPUTS(
2594e1dd0a2fSth 		    gettext("Cannot recover.  No backup files "
2595e1dd0a2fSth 		    "found.\n"),
2596e1dd0a2fSth 		    stderr);
25977c478bd9Sstevel@tonic-gate 		CLIENT_FPUTS(
2598e1dd0a2fSth 		    gettext("\t Either this machine was not initialized\n"),
2599e1dd0a2fSth 		    stderr);
26007c478bd9Sstevel@tonic-gate 		CLIENT_FPUTS(
2601e1dd0a2fSth 		    gettext("\t by ldapclient or the backup files "
2602e1dd0a2fSth 		    "have been\n"),
2603e1dd0a2fSth 		    stderr);
26047c478bd9Sstevel@tonic-gate 		CLIENT_FPUTS(
2605e1dd0a2fSth 		    gettext("\t removed manually or with an \"uninit\"\n"),
2606e1dd0a2fSth 		    stderr);
26077c478bd9Sstevel@tonic-gate 		return (CLIENT_ERR_RESTORE);	/* invalid backup */
26087c478bd9Sstevel@tonic-gate 	}
26097c478bd9Sstevel@tonic-gate 
26107c478bd9Sstevel@tonic-gate 	/*
26117c478bd9Sstevel@tonic-gate 	 * Get domainname.  Allow no domainname for the case where "files"
26127c478bd9Sstevel@tonic-gate 	 * config was backed up.
26137c478bd9Sstevel@tonic-gate 	 */
26147c478bd9Sstevel@tonic-gate 	stat_ret = stat(DOMAINNAME_BACK, &buf);
26157c478bd9Sstevel@tonic-gate 	if (mode_verbose)
26167c478bd9Sstevel@tonic-gate 		CLIENT_FPRINTF(stderr,
2617e1dd0a2fSth 		    gettext("recover: stat(%s)=%d\n"),
2618e1dd0a2fSth 		    DOMAINNAME_BACK, stat_ret);
26197c478bd9Sstevel@tonic-gate 	if (stat_ret == 0) {
26207c478bd9Sstevel@tonic-gate 		if (mode_verbose)
26217c478bd9Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr,
2622e1dd0a2fSth 			    gettext("recover: open(%s)\n"),
2623e1dd0a2fSth 			    DOMAINNAME_BACK);
26247c478bd9Sstevel@tonic-gate 		fd = open(DOMAINNAME_BACK, O_RDONLY);
26257c478bd9Sstevel@tonic-gate 		if (mode_verbose)
26267c478bd9Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr,
2627e1dd0a2fSth 			    gettext("recover: read(%s)\n"),
2628e1dd0a2fSth 			    DOMAINNAME_BACK);
26297c478bd9Sstevel@tonic-gate 		domainlen = read(fd, &(name[0]), BUFSIZ-1);
26307c478bd9Sstevel@tonic-gate 		(void) close(fd);
26317c478bd9Sstevel@tonic-gate 		if (domainlen < 0) {
26327c478bd9Sstevel@tonic-gate 			CLIENT_FPUTS(
2633e1dd0a2fSth 			    gettext("Cannot recover.  Cannot determine "
2634e1dd0a2fSth 			    "previous domain name.\n"),
2635e1dd0a2fSth 			    stderr);
26367c478bd9Sstevel@tonic-gate 			return (CLIENT_ERR_RESTORE);	/* invalid backup */
26377c478bd9Sstevel@tonic-gate 		} else 	{
26387c478bd9Sstevel@tonic-gate 			char *ptr;
26397c478bd9Sstevel@tonic-gate 
26407c478bd9Sstevel@tonic-gate 			ptr = strchr(&(name[0]), '\n');
26417c478bd9Sstevel@tonic-gate 			if (ptr != NULL)
26427c478bd9Sstevel@tonic-gate 				*ptr = '\0';
26437c478bd9Sstevel@tonic-gate 			else
26447c478bd9Sstevel@tonic-gate 				name[domainlen] = '\0';
26457c478bd9Sstevel@tonic-gate 
26467c478bd9Sstevel@tonic-gate 			if (mode_verbose)
26477c478bd9Sstevel@tonic-gate 				CLIENT_FPRINTF(stderr,
2648e1dd0a2fSth 				    gettext("recover: old domainname "
2649e1dd0a2fSth 				    "\"%s\"\n"), name);
26507c478bd9Sstevel@tonic-gate 
26517c478bd9Sstevel@tonic-gate 			if (strlen(name) == 0)
26527c478bd9Sstevel@tonic-gate 				domain = 0;
26537c478bd9Sstevel@tonic-gate 			else
26547c478bd9Sstevel@tonic-gate 				domain = 1;	/* flag that we have domain */
26557c478bd9Sstevel@tonic-gate 
26567c478bd9Sstevel@tonic-gate 		}
26577c478bd9Sstevel@tonic-gate 	}
26587c478bd9Sstevel@tonic-gate 
26597c478bd9Sstevel@tonic-gate 
26607c478bd9Sstevel@tonic-gate 	/*
26617c478bd9Sstevel@tonic-gate 	 * we can recover at this point
26627c478bd9Sstevel@tonic-gate 	 * remove LDAP config files before restore
26637c478bd9Sstevel@tonic-gate 	 */
26647c478bd9Sstevel@tonic-gate 	(void) unlink(NSCONFIGFILE);
26657c478bd9Sstevel@tonic-gate 	(void) unlink(NSCREDFILE);
26667c478bd9Sstevel@tonic-gate 
26677c478bd9Sstevel@tonic-gate 	ldap_conf_file = strrchr(NSCONFIGFILE, '/') + 1;
26687c478bd9Sstevel@tonic-gate 	ldap_cred_file = strrchr(NSCREDFILE, '/') + 1;
26697c478bd9Sstevel@tonic-gate 
26707c478bd9Sstevel@tonic-gate 	(void) strlcpy(ldap_file_back, LDAP_RESTORE_DIR "/", BUFSIZE);
26717c478bd9Sstevel@tonic-gate 	(void) strlcat(ldap_file_back, ldap_conf_file, BUFSIZE);
26727c478bd9Sstevel@tonic-gate 
26737c478bd9Sstevel@tonic-gate 	stat_ret = stat(ldap_file_back, &buf);
26747c478bd9Sstevel@tonic-gate 	if (mode_verbose)
26757c478bd9Sstevel@tonic-gate 		CLIENT_FPRINTF(stderr,
2676e1dd0a2fSth 		    gettext("recover: stat(%s)=%d\n"),
2677e1dd0a2fSth 		    ldap_file_back, stat_ret);
26787c478bd9Sstevel@tonic-gate 	if (stat_ret == 0) {
26797c478bd9Sstevel@tonic-gate 		if (saveState)
26807c478bd9Sstevel@tonic-gate 			gStartLdap = START_UNINIT;
26817c478bd9Sstevel@tonic-gate 		retcode = file_move(ldap_file_back, NSCONFIGFILE);
26827c478bd9Sstevel@tonic-gate 		if (mode_verbose)
26837c478bd9Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr,
2684e1dd0a2fSth 			    gettext("recover: file_move(%s, %s)=%d\n"),
2685e1dd0a2fSth 			    ldap_file_back, NSCONFIGFILE, retcode);
26867c478bd9Sstevel@tonic-gate 		if (retcode != 0)
26877c478bd9Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr,
2688e1dd0a2fSth 			    gettext("recover: file_move(%s, %s) failed\n"),
2689e1dd0a2fSth 			    ldap_file_back, NSCONFIGFILE);
26907c478bd9Sstevel@tonic-gate 	}
26917c478bd9Sstevel@tonic-gate 
26927c478bd9Sstevel@tonic-gate 	(void) strlcpy(ldap_cred_back, LDAP_RESTORE_DIR "/", BUFSIZE);
26937c478bd9Sstevel@tonic-gate 	(void) strlcat(ldap_cred_back, ldap_cred_file, BUFSIZE);
26947c478bd9Sstevel@tonic-gate 
26957c478bd9Sstevel@tonic-gate 	stat_ret = stat(ldap_cred_back, &buf);
26967c478bd9Sstevel@tonic-gate 	if (mode_verbose)
26977c478bd9Sstevel@tonic-gate 		CLIENT_FPRINTF(stderr,
2698e1dd0a2fSth 		    gettext("recover: stat(%s)=%d\n"),
2699e1dd0a2fSth 		    ldap_cred_back, stat_ret);
27007c478bd9Sstevel@tonic-gate 	if (stat_ret == 0) {
27017c478bd9Sstevel@tonic-gate 		retcode = file_move(ldap_cred_back, NSCREDFILE);
27027c478bd9Sstevel@tonic-gate 		if (mode_verbose)
27037c478bd9Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr,
2704e1dd0a2fSth 			    gettext("recover: file_move(%s, %s)=%d\n"),
2705e1dd0a2fSth 			    ldap_cred_back, NSCREDFILE, retcode);
27067c478bd9Sstevel@tonic-gate 		if (retcode != 0)
27077c478bd9Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr,
2708e1dd0a2fSth 			    gettext("recover: file_move(%s, %s) failed\n"),
2709e1dd0a2fSth 			    ldap_cred_back, NSCREDFILE);
27107c478bd9Sstevel@tonic-gate 	}
27117c478bd9Sstevel@tonic-gate 
27127c478bd9Sstevel@tonic-gate 	/* Check for recovery of NIS(YP) if we have a domainname */
27137c478bd9Sstevel@tonic-gate 	if (domain) {
27147c478bd9Sstevel@tonic-gate 		/* "name" would have to be huge for this, but just in case */
27157c478bd9Sstevel@tonic-gate 		if (strlen(name) >= (BUFSIZE - strlen(LDAP_RESTORE_DIR)))
27167c478bd9Sstevel@tonic-gate 			return (CLIENT_ERR_FAIL);
27177c478bd9Sstevel@tonic-gate 		if (strlen(name) >= (BUFSIZE - strlen(YP_BIND_DIR)))
27187c478bd9Sstevel@tonic-gate 			return (CLIENT_ERR_FAIL);
27197c478bd9Sstevel@tonic-gate 
27207c478bd9Sstevel@tonic-gate 		(void) strlcpy(yp_dir_back, LDAP_RESTORE_DIR "/", BUFSIZE);
27217c478bd9Sstevel@tonic-gate 		(void) strlcat(yp_dir_back, name, BUFSIZE);
27227c478bd9Sstevel@tonic-gate 		stat_ret = stat(yp_dir_back, &buf);
27237c478bd9Sstevel@tonic-gate 		if (mode_verbose)
27247c478bd9Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr,
2725e1dd0a2fSth 			    gettext("recover: stat(%s)=%d\n"),
2726e1dd0a2fSth 			    yp_dir_back, stat_ret);
27277c478bd9Sstevel@tonic-gate 		if (stat_ret == 0) {
27287c478bd9Sstevel@tonic-gate 			(void) strlcpy(yp_dir, YP_BIND_DIR "/", BUFSIZE);
27297c478bd9Sstevel@tonic-gate 			(void) strlcat(yp_dir, name, BUFSIZE);
27307c478bd9Sstevel@tonic-gate 			retcode = file_move(yp_dir_back, yp_dir);
27317c478bd9Sstevel@tonic-gate 			if (mode_verbose)
27327c478bd9Sstevel@tonic-gate 				CLIENT_FPRINTF(stderr,
2733e1dd0a2fSth 				    gettext("recover: file_move(%s, "
2734e1dd0a2fSth 				    "%s)=%d\n"),
2735e1dd0a2fSth 				    yp_dir_back, yp_dir, retcode);
27367c478bd9Sstevel@tonic-gate 			if (retcode != 0) {
27377c478bd9Sstevel@tonic-gate 				CLIENT_FPRINTF(stderr,
2738e1dd0a2fSth 				    gettext("recover: file_move(%s, "
2739e1dd0a2fSth 				    "%s) failed!\n"),
2740e1dd0a2fSth 				    yp_dir_back, yp_dir);
27417c478bd9Sstevel@tonic-gate 			} else {
27427c478bd9Sstevel@tonic-gate 				if (saveState)
27437c478bd9Sstevel@tonic-gate 					gStartYp = START_UNINIT;
27447c478bd9Sstevel@tonic-gate 			}
27457c478bd9Sstevel@tonic-gate 		}
27467c478bd9Sstevel@tonic-gate 	}
27477c478bd9Sstevel@tonic-gate 
27487c478bd9Sstevel@tonic-gate 	/* restore machine configuration */
27497c478bd9Sstevel@tonic-gate 	stat_ret = stat(NSSWITCH_BACK, &buf);
27507c478bd9Sstevel@tonic-gate 	if (mode_verbose)
27517c478bd9Sstevel@tonic-gate 		CLIENT_FPRINTF(stderr,
2752e1dd0a2fSth 		    gettext("recover: stat(%s)=%d\n"),
2753e1dd0a2fSth 		    NSSWITCH_BACK, stat_ret);
27547c478bd9Sstevel@tonic-gate 	if (stat_ret == 0) {
27557c478bd9Sstevel@tonic-gate 		retcode = file_move(NSSWITCH_BACK, NSSWITCH_CONF);
27567c478bd9Sstevel@tonic-gate 		if (mode_verbose)
27577c478bd9Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr,
2758e1dd0a2fSth 			    gettext("recover: file_move(%s, %s)=%d\n"),
2759e1dd0a2fSth 			    NSSWITCH_BACK, NSSWITCH_CONF, retcode);
27607c478bd9Sstevel@tonic-gate 		if (retcode != 0)
27617c478bd9Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr,
2762e1dd0a2fSth 			    gettext("recover: file_move(%s, %s) failed\n"),
2763e1dd0a2fSth 			    NSSWITCH_BACK, NSSWITCH_CONF);
27647c478bd9Sstevel@tonic-gate 	}
27657c478bd9Sstevel@tonic-gate 
27667c478bd9Sstevel@tonic-gate 	stat_ret = stat(DOMAINNAME_BACK, &buf);
27677c478bd9Sstevel@tonic-gate 	if (mode_verbose)
27687c478bd9Sstevel@tonic-gate 		CLIENT_FPRINTF(stderr,
2769e1dd0a2fSth 		    gettext("recover: stat(%s)=%d\n"),
2770e1dd0a2fSth 		    DOMAINNAME_BACK, stat_ret);
27717c478bd9Sstevel@tonic-gate 	if (stat_ret == 0) {
27727c478bd9Sstevel@tonic-gate 		retcode = file_move(DOMAINNAME_BACK, DOMAINNAME);
27737c478bd9Sstevel@tonic-gate 		if (mode_verbose)
27747c478bd9Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr,
2775e1dd0a2fSth 			    gettext("recover: file_move(%s, %s)=%d\n"),
2776e1dd0a2fSth 			    DOMAINNAME_BACK, DOMAINNAME, retcode);
27777c478bd9Sstevel@tonic-gate 		if (retcode != 0)
27787c478bd9Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr,
2779e1dd0a2fSth 			    gettext("recover: file_move(%s, %s) failed\n"),
2780e1dd0a2fSth 			    DOMAINNAME_BACK, DOMAINNAME);
27817c478bd9Sstevel@tonic-gate 	}
27827c478bd9Sstevel@tonic-gate 
27837c478bd9Sstevel@tonic-gate 	retcode = rmdir(LDAP_RESTORE_DIR);
27847c478bd9Sstevel@tonic-gate 	if (retcode != 0) {
27857c478bd9Sstevel@tonic-gate 		CLIENT_FPRINTF(stderr,
2786e1dd0a2fSth 		    gettext("Error removing \"%s\" directory.\n"),
2787e1dd0a2fSth 		    LDAP_RESTORE_DIR);
27887c478bd9Sstevel@tonic-gate 	}
27897c478bd9Sstevel@tonic-gate 
27907c478bd9Sstevel@tonic-gate 	return (CLIENT_SUCCESS);
27917c478bd9Sstevel@tonic-gate }
27927c478bd9Sstevel@tonic-gate 
27937c478bd9Sstevel@tonic-gate /*
27947c478bd9Sstevel@tonic-gate  * try to save the current state of this machine.
27957c478bd9Sstevel@tonic-gate  * this just overwrites any old saved configration files.
27967c478bd9Sstevel@tonic-gate  *
27977c478bd9Sstevel@tonic-gate  * This function should only be called after network services have been stopped.
27987c478bd9Sstevel@tonic-gate  *
27997c478bd9Sstevel@tonic-gate  * Returns 0 on successful save
28007c478bd9Sstevel@tonic-gate  * Otherwise returns -1
28017c478bd9Sstevel@tonic-gate  */
28027c478bd9Sstevel@tonic-gate static int
28037c478bd9Sstevel@tonic-gate file_backup(void)
28047c478bd9Sstevel@tonic-gate {
28057c478bd9Sstevel@tonic-gate 	struct stat buf;
28067c478bd9Sstevel@tonic-gate 	int domain_stat, conf_stat, ldap_stat;
280736e852a1SRaja Andra 	int yp_stat, restore_stat;
28087c478bd9Sstevel@tonic-gate 	int retcode, namelen, ret;
28097c478bd9Sstevel@tonic-gate 	char yp_dir[BUFSIZ], yp_dir_back[BUFSIZ];
28107c478bd9Sstevel@tonic-gate 	char name[BUFSIZ];
28117c478bd9Sstevel@tonic-gate 	char *ldap_conf_file, *ldap_cred_file;
28127c478bd9Sstevel@tonic-gate 	char ldap_file_back[BUFSIZE], ldap_cred_back[BUFSIZE];
28137c478bd9Sstevel@tonic-gate 
28147c478bd9Sstevel@tonic-gate 	ret = CLIENT_SUCCESS;
28157c478bd9Sstevel@tonic-gate 	/* If running as Sysid Install become a no-op */
28167c478bd9Sstevel@tonic-gate 	if (sysid_install == B_TRUE)
28177c478bd9Sstevel@tonic-gate 		return (CLIENT_SUCCESS);
28187c478bd9Sstevel@tonic-gate 
28197c478bd9Sstevel@tonic-gate 	/* If existing backup files, clear for this run */
28207c478bd9Sstevel@tonic-gate 	restore_stat = stat(LDAP_RESTORE_DIR, &buf);
28217c478bd9Sstevel@tonic-gate 	if (restore_stat == 0) {
28227c478bd9Sstevel@tonic-gate 		if (mode_verbose) {
28237c478bd9Sstevel@tonic-gate 			CLIENT_FPUTS(
2824e1dd0a2fSth 			    gettext("Removing existing restore "
2825e1dd0a2fSth 			    "directory\n"),
2826e1dd0a2fSth 			    stderr);
28277c478bd9Sstevel@tonic-gate 		}
28287c478bd9Sstevel@tonic-gate 		(void) system("/bin/rm -fr " LDAP_RESTORE_DIR);
28297c478bd9Sstevel@tonic-gate 		restore_stat = stat(LDAP_RESTORE_DIR, &buf);
28307c478bd9Sstevel@tonic-gate 		if (restore_stat == 0) {
28317c478bd9Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr,
2832e1dd0a2fSth 			    gettext("Unable to remove backup "
2833e1dd0a2fSth 			    "directory (%s)\n"),
2834e1dd0a2fSth 			    LDAP_RESTORE_DIR);
28357c478bd9Sstevel@tonic-gate 			return (CLIENT_ERR_RESTORE);
28367c478bd9Sstevel@tonic-gate 		}
28377c478bd9Sstevel@tonic-gate 	}
28387c478bd9Sstevel@tonic-gate 
28397c478bd9Sstevel@tonic-gate 	retcode = mkdir(LDAP_RESTORE_DIR, 0755);
28407c478bd9Sstevel@tonic-gate 	if (retcode != 0) {
28417c478bd9Sstevel@tonic-gate 		CLIENT_FPRINTF(stderr,
2842e1dd0a2fSth 		    gettext("file_backup: Failed to make %s backup "
2843e1dd0a2fSth 		    "directory. mkdir=%d\n"),
2844e1dd0a2fSth 		    LDAP_RESTORE_DIR, retcode);
28457c478bd9Sstevel@tonic-gate 		return (CLIENT_ERR_FAIL);
28467c478bd9Sstevel@tonic-gate 	}
28477c478bd9Sstevel@tonic-gate 
28487c478bd9Sstevel@tonic-gate 	conf_stat = stat(NSSWITCH_CONF, &buf);
28497c478bd9Sstevel@tonic-gate 	if (mode_verbose)
28507c478bd9Sstevel@tonic-gate 		CLIENT_FPRINTF(stderr,
2851e1dd0a2fSth 		    gettext("file_backup: stat(%s)=%d\n"),
2852e1dd0a2fSth 		    NSSWITCH_CONF, conf_stat);
28537c478bd9Sstevel@tonic-gate 	if (conf_stat == 0) {
28547c478bd9Sstevel@tonic-gate 		if (mode_verbose)
28557c478bd9Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr,
2856e1dd0a2fSth 			    gettext("file_backup: (%s -> %s)\n"),
2857e1dd0a2fSth 			    NSSWITCH_CONF, NSSWITCH_BACK);
28587c478bd9Sstevel@tonic-gate 		retcode = file_move(NSSWITCH_CONF, NSSWITCH_BACK);
28597c478bd9Sstevel@tonic-gate 		if (retcode != 0) {
28607c478bd9Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr,
2861e1dd0a2fSth 			    gettext("file_backup: file_move(%s, %s) failed "
2862e1dd0a2fSth 			    "with %d\n"),
2863e1dd0a2fSth 			    NSSWITCH_CONF, NSSWITCH_BACK, retcode);
28647c478bd9Sstevel@tonic-gate 			ret = CLIENT_ERR_RENAME;
28657c478bd9Sstevel@tonic-gate 		}
28667c478bd9Sstevel@tonic-gate 	} else {
28677c478bd9Sstevel@tonic-gate 		if (mode_verbose)
28687c478bd9Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr,
2869e1dd0a2fSth 			    gettext("file_backup: No %s file.\n"),
2870e1dd0a2fSth 			    NSSWITCH_CONF);
28717c478bd9Sstevel@tonic-gate 	}
28727c478bd9Sstevel@tonic-gate 
28737c478bd9Sstevel@tonic-gate 	domain_stat = stat(DOMAINNAME, &buf);
28747c478bd9Sstevel@tonic-gate 	if (mode_verbose)
28757c478bd9Sstevel@tonic-gate 		CLIENT_FPRINTF(stderr,
2876e1dd0a2fSth 		    gettext("file_backup: stat(%s)=%d\n"),
2877e1dd0a2fSth 		    DOMAINNAME, domain_stat);
28787c478bd9Sstevel@tonic-gate 	if ((domain_stat == 0) && (buf.st_size > 0)) {
28797c478bd9Sstevel@tonic-gate 		if (mode_verbose)
28807c478bd9Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr,
2881e1dd0a2fSth 			    gettext("file_backup: (%s -> %s)\n"),
2882e1dd0a2fSth 			    DOMAINNAME, DOMAINNAME_BACK);
28837c478bd9Sstevel@tonic-gate 		retcode = file_move(DOMAINNAME, DOMAINNAME_BACK);
28847c478bd9Sstevel@tonic-gate 		if (retcode != 0) {
28857c478bd9Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr,
2886e1dd0a2fSth 			    gettext("file_backup: file_move(%s, %s) failed "
2887e1dd0a2fSth 			    "with %d\n"),
2888e1dd0a2fSth 			    DOMAINNAME, DOMAINNAME_BACK, retcode);
28897c478bd9Sstevel@tonic-gate 			ret = CLIENT_ERR_RENAME;
28907c478bd9Sstevel@tonic-gate 		}
28917c478bd9Sstevel@tonic-gate 	} else {
28927c478bd9Sstevel@tonic-gate 		if (mode_verbose)
28937c478bd9Sstevel@tonic-gate 			if (domain_stat != 0) {
28947c478bd9Sstevel@tonic-gate 				CLIENT_FPRINTF(stderr,
2895e1dd0a2fSth 				    gettext("file_backup: No %s file.\n"),
2896e1dd0a2fSth 				    DOMAINNAME);
28977c478bd9Sstevel@tonic-gate 			} else {
28987c478bd9Sstevel@tonic-gate 				CLIENT_FPRINTF(stderr,
2899e1dd0a2fSth 				    gettext("file_backup: Empty %s "
2900e1dd0a2fSth 				    "file.\n"),
2901e1dd0a2fSth 				    DOMAINNAME);
29027c478bd9Sstevel@tonic-gate 			}
29037c478bd9Sstevel@tonic-gate 	}
29047c478bd9Sstevel@tonic-gate 
29057c478bd9Sstevel@tonic-gate 	namelen = BUFSIZ;
29067c478bd9Sstevel@tonic-gate 	(void) sysinfo(SI_SRPC_DOMAIN, &(name[0]), namelen);
29077c478bd9Sstevel@tonic-gate 	namelen = strlen(name);
29087c478bd9Sstevel@tonic-gate 
29097c478bd9Sstevel@tonic-gate 	if (mode_verbose)
29107c478bd9Sstevel@tonic-gate 		CLIENT_FPRINTF(stderr,
2911e1dd0a2fSth 		    gettext("file_backup: nis domain is \"%s\"\n"),
2912e1dd0a2fSth 		    (namelen > 0) ? name : "EMPTY");
29137c478bd9Sstevel@tonic-gate 	/* check for domain name if not set cannot save NIS(YP) state */
29147c478bd9Sstevel@tonic-gate 	if (namelen > 0) {
29157c478bd9Sstevel@tonic-gate 		/* moving /var/yp/binding will cause ypbind to core dump */
29167c478bd9Sstevel@tonic-gate 		(void) strlcpy(yp_dir, YP_BIND_DIR "/", BUFSIZE);
29177c478bd9Sstevel@tonic-gate 		(void) strlcat(yp_dir, name, BUFSIZE);
29187c478bd9Sstevel@tonic-gate 		yp_stat = stat(yp_dir, &buf);
29197c478bd9Sstevel@tonic-gate 		if (mode_verbose)
29207c478bd9Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr,
2921e1dd0a2fSth 			    gettext("file_backup: stat(%s)=%d\n"),
2922e1dd0a2fSth 			    yp_dir, yp_stat);
29237c478bd9Sstevel@tonic-gate 		if (yp_stat == 0) {
29247c478bd9Sstevel@tonic-gate 			(void) strlcpy(yp_dir_back, LDAP_RESTORE_DIR "/",
2925e1dd0a2fSth 			    BUFSIZE);
29267c478bd9Sstevel@tonic-gate 			(void) strlcat(yp_dir_back, name, BUFSIZE);
29277c478bd9Sstevel@tonic-gate 			if (mode_verbose)
29287c478bd9Sstevel@tonic-gate 				CLIENT_FPRINTF(stderr,
2929e1dd0a2fSth 				    gettext("file_backup: (%s -> %s)\n"),
2930e1dd0a2fSth 				    yp_dir, yp_dir_back);
29317c478bd9Sstevel@tonic-gate 			retcode = file_move(yp_dir, yp_dir_back);
29327c478bd9Sstevel@tonic-gate 			if (retcode != 0) {
29337c478bd9Sstevel@tonic-gate 				CLIENT_FPRINTF(stderr,
2934e1dd0a2fSth 				    gettext("file_backup: file_move(%s, %s)"
2935e1dd0a2fSth 				    " failed with %d\n"),
2936e1dd0a2fSth 				    yp_dir, yp_dir_back, retcode);
29377c478bd9Sstevel@tonic-gate 				ret = CLIENT_ERR_RENAME;
29387c478bd9Sstevel@tonic-gate 			}
29397c478bd9Sstevel@tonic-gate 		} else {
29407c478bd9Sstevel@tonic-gate 			if (mode_verbose)
29417c478bd9Sstevel@tonic-gate 				CLIENT_FPRINTF(stderr,
2942e1dd0a2fSth 				    gettext("file_backup: No %s "
2943e1dd0a2fSth 				    "directory.\n"), yp_dir);
29447c478bd9Sstevel@tonic-gate 		}
29457c478bd9Sstevel@tonic-gate 	}
29467c478bd9Sstevel@tonic-gate 
29477c478bd9Sstevel@tonic-gate 
29487c478bd9Sstevel@tonic-gate 	/* point to file name, not path delim (/) */
29497c478bd9Sstevel@tonic-gate 	ldap_conf_file = strrchr(NSCONFIGFILE, '/') + 1;
29507c478bd9Sstevel@tonic-gate 	ldap_cred_file = strrchr(NSCREDFILE, '/') + 1;
29517c478bd9Sstevel@tonic-gate 
29527c478bd9Sstevel@tonic-gate 	ldap_stat = stat(NSCONFIGFILE, &buf);
29537c478bd9Sstevel@tonic-gate 	if (mode_verbose)
29547c478bd9Sstevel@tonic-gate 		CLIENT_FPRINTF(stderr,
2955e1dd0a2fSth 		    gettext("file_backup: stat(%s)=%d\n"),
2956e1dd0a2fSth 		    NSCONFIGFILE, ldap_stat);
29577c478bd9Sstevel@tonic-gate 	if (ldap_stat == 0) {
29587c478bd9Sstevel@tonic-gate 		(void) strlcpy(ldap_file_back, LDAP_RESTORE_DIR "/", BUFSIZE);
29597c478bd9Sstevel@tonic-gate 		(void) strlcat(ldap_file_back, ldap_conf_file, BUFSIZE);
29607c478bd9Sstevel@tonic-gate 		if (mode_verbose)
29617c478bd9Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr,
2962e1dd0a2fSth 			    gettext("file_backup: (%s -> %s)\n"),
2963e1dd0a2fSth 			    NSCONFIGFILE, ldap_file_back);
29647c478bd9Sstevel@tonic-gate 		retcode = file_move(NSCONFIGFILE, ldap_file_back);
29657c478bd9Sstevel@tonic-gate 		if (retcode != 0) {
29667c478bd9Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr,
2967e1dd0a2fSth 			    gettext("file_backup: file_move(%s, %s) failed "
2968e1dd0a2fSth 			    "with %d\n"),
2969e1dd0a2fSth 			    NSCONFIGFILE, ldap_file_back, retcode);
29707c478bd9Sstevel@tonic-gate 			ret = CLIENT_ERR_RENAME;
29717c478bd9Sstevel@tonic-gate 		}
29727c478bd9Sstevel@tonic-gate 
29737c478bd9Sstevel@tonic-gate 		(void) strlcpy(ldap_cred_back, LDAP_RESTORE_DIR "/", BUFSIZE);
29747c478bd9Sstevel@tonic-gate 		(void) strlcat(ldap_cred_back, ldap_cred_file, BUFSIZE);
29757c478bd9Sstevel@tonic-gate 		if (mode_verbose)
29767c478bd9Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr,
2977e1dd0a2fSth 			    gettext("file_backup: (%s -> %s)\n"),
2978e1dd0a2fSth 			    NSCREDFILE, ldap_cred_back);
29797c478bd9Sstevel@tonic-gate 		retcode = file_move(NSCREDFILE, ldap_cred_back);
29807c478bd9Sstevel@tonic-gate 		if (retcode != 0) {
29817c478bd9Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr,
2982e1dd0a2fSth 			    gettext("file_backup: file_move(%s, %s) failed "
2983e1dd0a2fSth 			    "with %d\n"),
2984e1dd0a2fSth 			    NSCREDFILE, ldap_cred_back, retcode);
29857c478bd9Sstevel@tonic-gate 			ret = CLIENT_ERR_RENAME;
29867c478bd9Sstevel@tonic-gate 		}
29877c478bd9Sstevel@tonic-gate 	} else {
29887c478bd9Sstevel@tonic-gate 		if (mode_verbose)
29897c478bd9Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr,
2990e1dd0a2fSth 			    gettext("file_backup: No %s file.\n"),
2991e1dd0a2fSth 			    NSCONFIGFILE);
29927c478bd9Sstevel@tonic-gate 	}
29937c478bd9Sstevel@tonic-gate 
29947c478bd9Sstevel@tonic-gate 	return (ret);
29957c478bd9Sstevel@tonic-gate }
29967c478bd9Sstevel@tonic-gate 
29977c478bd9Sstevel@tonic-gate /*
29987c478bd9Sstevel@tonic-gate  * mod_backup()
29997c478bd9Sstevel@tonic-gate  *
30007c478bd9Sstevel@tonic-gate  * This function is used to temporily backup the LDAP client files in /var/ldap
30017c478bd9Sstevel@tonic-gate  * that the "mod" operation needs to update.  If an error occurs then the
30027c478bd9Sstevel@tonic-gate  * function mod_recover() can be invoke to recover the unmodified files.
30037c478bd9Sstevel@tonic-gate  */
30047c478bd9Sstevel@tonic-gate static int
30057c478bd9Sstevel@tonic-gate mod_backup(void)
30067c478bd9Sstevel@tonic-gate {
30077c478bd9Sstevel@tonic-gate 	int rc;
30087c478bd9Sstevel@tonic-gate 	int retcode = CLIENT_SUCCESS;
30097c478bd9Sstevel@tonic-gate 
30107c478bd9Sstevel@tonic-gate 	rc = system(CMD_CP " " NSCONFIGFILE " " NSCONFIGFILE ".mod");
30117c478bd9Sstevel@tonic-gate 	retcode += rc;
30127c478bd9Sstevel@tonic-gate 	if (mode_verbose)
30137c478bd9Sstevel@tonic-gate 		CLIENT_FPRINTF(stderr,
30147c478bd9Sstevel@tonic-gate 		    gettext("mod_backup: backup %s for %s\n"),
30157c478bd9Sstevel@tonic-gate 		    rc ? "failed" : "successful", NSCONFIGFILE);
30167c478bd9Sstevel@tonic-gate 
30177c478bd9Sstevel@tonic-gate 	rc = system(CMD_CP " " NSCREDFILE " " NSCREDFILE ".mod");
30187c478bd9Sstevel@tonic-gate 	retcode += rc;
30197c478bd9Sstevel@tonic-gate 	if (mode_verbose)
30207c478bd9Sstevel@tonic-gate 		CLIENT_FPRINTF(stderr,
30217c478bd9Sstevel@tonic-gate 		    gettext("mod_backup: backup %s for %s\n"),
30227c478bd9Sstevel@tonic-gate 		    rc ? "failed" : "successful", NSCREDFILE);
30237c478bd9Sstevel@tonic-gate 
30247c478bd9Sstevel@tonic-gate 	rc = system(CMD_CP " " DOMAINNAME " " DOMAINNAME ".mod");
30257c478bd9Sstevel@tonic-gate 	retcode += rc;
30267c478bd9Sstevel@tonic-gate 	if (mode_verbose)
30277c478bd9Sstevel@tonic-gate 		CLIENT_FPRINTF(stderr,
30287c478bd9Sstevel@tonic-gate 		    gettext("mod_backup: backup %s for %s\n"),
30297c478bd9Sstevel@tonic-gate 		    rc ? "failed" : "successful", DOMAINNAME);
30307c478bd9Sstevel@tonic-gate 
30317c478bd9Sstevel@tonic-gate 	if (retcode != CLIENT_SUCCESS)
30327c478bd9Sstevel@tonic-gate 		retcode = CLIENT_ERR_RENAME;
30337c478bd9Sstevel@tonic-gate 	return (retcode);
30347c478bd9Sstevel@tonic-gate }
30357c478bd9Sstevel@tonic-gate 
30367c478bd9Sstevel@tonic-gate /*
30377c478bd9Sstevel@tonic-gate  * mod_recover()
30387c478bd9Sstevel@tonic-gate  *
30397c478bd9Sstevel@tonic-gate  * This function is used to recover the temporily backed up files by
30407c478bd9Sstevel@tonic-gate  * the mod_backup() function if an error occurs during the "mod"
30417c478bd9Sstevel@tonic-gate  * operation.
30427c478bd9Sstevel@tonic-gate  */
30437c478bd9Sstevel@tonic-gate static int
30447c478bd9Sstevel@tonic-gate mod_recover(void)
30457c478bd9Sstevel@tonic-gate {
30467c478bd9Sstevel@tonic-gate 	int rc;
30477c478bd9Sstevel@tonic-gate 	int retcode = CLIENT_SUCCESS;
30487c478bd9Sstevel@tonic-gate 
30497c478bd9Sstevel@tonic-gate 	rc = system(CMD_MV " " NSCONFIGFILE ".mod " NSCONFIGFILE);
30507c478bd9Sstevel@tonic-gate 	retcode += rc;
30517c478bd9Sstevel@tonic-gate 	if (mode_verbose)
30527c478bd9Sstevel@tonic-gate 		CLIENT_FPRINTF(stderr,
30537c478bd9Sstevel@tonic-gate 		    gettext("mod_recover: recovery %s for %s\n"),
30547c478bd9Sstevel@tonic-gate 		    rc ? "failed" : "successful", NSCONFIGFILE);
30557c478bd9Sstevel@tonic-gate 
30567c478bd9Sstevel@tonic-gate 	rc = system(CMD_MV " " NSCREDFILE ".mod " NSCREDFILE);
30577c478bd9Sstevel@tonic-gate 	retcode += rc;
30587c478bd9Sstevel@tonic-gate 	if (mode_verbose)
30597c478bd9Sstevel@tonic-gate 		CLIENT_FPRINTF(stderr,
30607c478bd9Sstevel@tonic-gate 		    gettext("mod_recover: recovery %s for %s\n"),
30617c478bd9Sstevel@tonic-gate 		    rc ? "failed" : "successful", NSCREDFILE);
30627c478bd9Sstevel@tonic-gate 
30637c478bd9Sstevel@tonic-gate 	rc = system(CMD_MV " " DOMAINNAME ".mod " DOMAINNAME);
30647c478bd9Sstevel@tonic-gate 	retcode += rc;
30657c478bd9Sstevel@tonic-gate 	if (mode_verbose)
30667c478bd9Sstevel@tonic-gate 		CLIENT_FPRINTF(stderr,
30677c478bd9Sstevel@tonic-gate 		    gettext("mod_recover: recovery %s for %s\n"),
30687c478bd9Sstevel@tonic-gate 		    rc ? "failed" : "successful", DOMAINNAME);
30697c478bd9Sstevel@tonic-gate 
30707c478bd9Sstevel@tonic-gate 	if (retcode != CLIENT_SUCCESS)
30717c478bd9Sstevel@tonic-gate 		retcode = CLIENT_ERR_RENAME;
30727c478bd9Sstevel@tonic-gate 	return (retcode);
30737c478bd9Sstevel@tonic-gate }
30747c478bd9Sstevel@tonic-gate 
30757c478bd9Sstevel@tonic-gate /*
30767c478bd9Sstevel@tonic-gate  * mod_cleanup()
30777c478bd9Sstevel@tonic-gate  *
30787c478bd9Sstevel@tonic-gate  * This function removes the .mod files in /var/ldap.
30797c478bd9Sstevel@tonic-gate  */
30807c478bd9Sstevel@tonic-gate static void
30817c478bd9Sstevel@tonic-gate mod_cleanup(void)
30827c478bd9Sstevel@tonic-gate {
30837c478bd9Sstevel@tonic-gate 	(void) system(CMD_RM " " NSCONFIGFILE ".mod " TO_DEV_NULL);
30847c478bd9Sstevel@tonic-gate 	(void) system(CMD_RM " " NSCREDFILE ".mod " TO_DEV_NULL);
30857c478bd9Sstevel@tonic-gate 	(void) system(CMD_RM " " DOMAINNAME ".mod " TO_DEV_NULL);
30867c478bd9Sstevel@tonic-gate }
30877c478bd9Sstevel@tonic-gate 
30887c478bd9Sstevel@tonic-gate #define	MAX_DN_ARRAY 100
30897c478bd9Sstevel@tonic-gate #define	LDAP_NAMINGCONTEXTS	"namingcontexts"
30907c478bd9Sstevel@tonic-gate 
30917c478bd9Sstevel@tonic-gate static multival_t *
30927c478bd9Sstevel@tonic-gate multival_new()
30937c478bd9Sstevel@tonic-gate {
30947c478bd9Sstevel@tonic-gate 	multival_t *hold;
30957c478bd9Sstevel@tonic-gate 
30967c478bd9Sstevel@tonic-gate 	hold = calloc(1, sizeof (multival_t));
30977c478bd9Sstevel@tonic-gate 	if (hold == NULL) {
30987c478bd9Sstevel@tonic-gate 		CLIENT_FPUTS(
3099e1dd0a2fSth 		    gettext("multival_new: Memory allocation error\n"),
3100e1dd0a2fSth 		    stderr);
31017c478bd9Sstevel@tonic-gate 	}
31027c478bd9Sstevel@tonic-gate 	return (hold);	/* NULL -> error */
31037c478bd9Sstevel@tonic-gate }
31047c478bd9Sstevel@tonic-gate 
31057c478bd9Sstevel@tonic-gate static int
31067c478bd9Sstevel@tonic-gate multival_add(multival_t *list, char *opt)
31077c478bd9Sstevel@tonic-gate {
31087c478bd9Sstevel@tonic-gate 	if (opt == NULL) {
31097c478bd9Sstevel@tonic-gate 		CLIENT_FPUTS(
3110e1dd0a2fSth 		    gettext("Empty value passed to multival_add\n"),
3111e1dd0a2fSth 		    stderr);
31127c478bd9Sstevel@tonic-gate 		return (CLIENT_ERR_FAIL);
31137c478bd9Sstevel@tonic-gate 	}
31147c478bd9Sstevel@tonic-gate 
31157c478bd9Sstevel@tonic-gate 	if (list->count == 0) {
31167c478bd9Sstevel@tonic-gate 		list->optlist = (char **)malloc(sizeof (char **));
31177c478bd9Sstevel@tonic-gate 	} else {
31187c478bd9Sstevel@tonic-gate 		list->optlist = (char **)realloc(list->optlist,
3119e1dd0a2fSth 		    (list->count + 1) * sizeof (char **));
31207c478bd9Sstevel@tonic-gate 	}
31217c478bd9Sstevel@tonic-gate 
31227c478bd9Sstevel@tonic-gate 	if (list->optlist == NULL) {
31237c478bd9Sstevel@tonic-gate 		CLIENT_FPUTS(gettext("Error allocating memory\n"), stderr);
31247c478bd9Sstevel@tonic-gate 		return (CLIENT_ERR_MEMORY);	/* 0 is success */
31257c478bd9Sstevel@tonic-gate 	}
31267c478bd9Sstevel@tonic-gate 
31277c478bd9Sstevel@tonic-gate 	list->optlist[list->count] = opt;
31287c478bd9Sstevel@tonic-gate 	list->count++;
31297c478bd9Sstevel@tonic-gate 
31307c478bd9Sstevel@tonic-gate 	return (CLIENT_SUCCESS);
31317c478bd9Sstevel@tonic-gate }
31327c478bd9Sstevel@tonic-gate 
31337c478bd9Sstevel@tonic-gate static void
31347c478bd9Sstevel@tonic-gate multival_free(multival_t *list)
31357c478bd9Sstevel@tonic-gate {
31367c478bd9Sstevel@tonic-gate 	if (list == NULL)
31377c478bd9Sstevel@tonic-gate 		return;
31387c478bd9Sstevel@tonic-gate 
31397c478bd9Sstevel@tonic-gate 	if (list->optlist != NULL)
31407c478bd9Sstevel@tonic-gate 		free(list->optlist);
31417c478bd9Sstevel@tonic-gate 	free(list);
31427c478bd9Sstevel@tonic-gate }
31437c478bd9Sstevel@tonic-gate 
31447c478bd9Sstevel@tonic-gate static clientopts_t *
31457c478bd9Sstevel@tonic-gate clientopts_new()
31467c478bd9Sstevel@tonic-gate {
31477c478bd9Sstevel@tonic-gate 	clientopts_t *hold;
31487c478bd9Sstevel@tonic-gate 
31497c478bd9Sstevel@tonic-gate 	hold = calloc(1, sizeof (clientopts_t));
31507c478bd9Sstevel@tonic-gate 	if (NULL == hold) {
31517c478bd9Sstevel@tonic-gate 		CLIENT_FPUTS(gettext("Error allocating memory for "
3152e1dd0a2fSth 		    "clientopts structure\n"), stderr);
31537c478bd9Sstevel@tonic-gate 		return (hold);	/* NULL -> error */
31547c478bd9Sstevel@tonic-gate 	}
31557c478bd9Sstevel@tonic-gate 
31567c478bd9Sstevel@tonic-gate 	hold->serviceAuthenticationMethod = multival_new();
31577c478bd9Sstevel@tonic-gate 	if (NULL == hold->serviceAuthenticationMethod) {
31587c478bd9Sstevel@tonic-gate 		CLIENT_FPUTS(gettext("Error allocating memory for "
3159e1dd0a2fSth 		    "serviceAuthenticationMethod\n"), stderr);
31607c478bd9Sstevel@tonic-gate 		free(hold);
31617c478bd9Sstevel@tonic-gate 		return (NULL);	/* NULL -> error */
31627c478bd9Sstevel@tonic-gate 	}
31637c478bd9Sstevel@tonic-gate 
31647c478bd9Sstevel@tonic-gate 	hold->serviceCredentialLevel = multival_new();
31657c478bd9Sstevel@tonic-gate 	if (NULL == hold->serviceCredentialLevel) {
31667c478bd9Sstevel@tonic-gate 		CLIENT_FPUTS(gettext("Error allocating memory for "
3167e1dd0a2fSth 		    "serviceCredentialLevel\n"), stderr);
31687c478bd9Sstevel@tonic-gate 		multival_free(hold->serviceAuthenticationMethod);
31697c478bd9Sstevel@tonic-gate 		free(hold);
31707c478bd9Sstevel@tonic-gate 		return (NULL);	/* NULL -> error */
31717c478bd9Sstevel@tonic-gate 	}
31727c478bd9Sstevel@tonic-gate 
31737c478bd9Sstevel@tonic-gate 	hold->objectclassMap = multival_new();
31747c478bd9Sstevel@tonic-gate 	if (NULL == hold->objectclassMap) {
31757c478bd9Sstevel@tonic-gate 		CLIENT_FPUTS(gettext("Error allocating memory for "
3176e1dd0a2fSth 		    "objectclassMap\n"), stderr);
31777c478bd9Sstevel@tonic-gate 		multival_free(hold->serviceAuthenticationMethod);
31787c478bd9Sstevel@tonic-gate 		multival_free(hold->serviceCredentialLevel);
31797c478bd9Sstevel@tonic-gate 		free(hold);
31807c478bd9Sstevel@tonic-gate 		return (NULL);	/* NULL -> error */
31817c478bd9Sstevel@tonic-gate 	}
31827c478bd9Sstevel@tonic-gate 
31837c478bd9Sstevel@tonic-gate 	hold->attributeMap = multival_new();
31847c478bd9Sstevel@tonic-gate 	if (NULL == hold->attributeMap) {
31857c478bd9Sstevel@tonic-gate 		CLIENT_FPUTS(gettext("Error allocating memory for "
3186e1dd0a2fSth 		    "attributeMap\n"), stderr);
31877c478bd9Sstevel@tonic-gate 		multival_free(hold->serviceAuthenticationMethod);
31887c478bd9Sstevel@tonic-gate 		multival_free(hold->serviceCredentialLevel);
31897c478bd9Sstevel@tonic-gate 		multival_free(hold->objectclassMap);
31907c478bd9Sstevel@tonic-gate 		free(hold);
31917c478bd9Sstevel@tonic-gate 		return (NULL);	/* NULL -> error */
31927c478bd9Sstevel@tonic-gate 	}
31937c478bd9Sstevel@tonic-gate 
31947c478bd9Sstevel@tonic-gate 	hold->serviceSearchDescriptor = multival_new();
31957c478bd9Sstevel@tonic-gate 	if (NULL == hold->serviceSearchDescriptor) {
31967c478bd9Sstevel@tonic-gate 		CLIENT_FPUTS(gettext("Error allocating memory for "
3197e1dd0a2fSth 		    "serviceSearchDescriptor\n"), stderr);
31987c478bd9Sstevel@tonic-gate 		multival_free(hold->serviceAuthenticationMethod);
31997c478bd9Sstevel@tonic-gate 		multival_free(hold->serviceCredentialLevel);
32007c478bd9Sstevel@tonic-gate 		multival_free(hold->objectclassMap);
32017c478bd9Sstevel@tonic-gate 		multival_free(hold->attributeMap);
32027c478bd9Sstevel@tonic-gate 		free(hold);
32037c478bd9Sstevel@tonic-gate 		return (NULL);	/* NULL -> error */
32047c478bd9Sstevel@tonic-gate 	}
32057c478bd9Sstevel@tonic-gate 
32067c478bd9Sstevel@tonic-gate 	return (hold);
32077c478bd9Sstevel@tonic-gate }
32087c478bd9Sstevel@tonic-gate 
32097c478bd9Sstevel@tonic-gate static void
32107c478bd9Sstevel@tonic-gate clientopts_free(clientopts_t *list)
32117c478bd9Sstevel@tonic-gate {
32127c478bd9Sstevel@tonic-gate 	if (NULL == list)
32137c478bd9Sstevel@tonic-gate 		return;
32147c478bd9Sstevel@tonic-gate 
32157c478bd9Sstevel@tonic-gate 	multival_free(list->serviceAuthenticationMethod);
32167c478bd9Sstevel@tonic-gate 	multival_free(list->serviceCredentialLevel);
32177c478bd9Sstevel@tonic-gate 	multival_free(list->objectclassMap);
32187c478bd9Sstevel@tonic-gate 	multival_free(list->attributeMap);
32197c478bd9Sstevel@tonic-gate 	multival_free(list->serviceSearchDescriptor);
32207c478bd9Sstevel@tonic-gate 
32217c478bd9Sstevel@tonic-gate 	free(list);
32227c478bd9Sstevel@tonic-gate 
32237c478bd9Sstevel@tonic-gate }
32247c478bd9Sstevel@tonic-gate 
32257c478bd9Sstevel@tonic-gate static void
32267c478bd9Sstevel@tonic-gate multival_list(char *opt, multival_t *list)
32277c478bd9Sstevel@tonic-gate {
32287c478bd9Sstevel@tonic-gate 	int i;
32297c478bd9Sstevel@tonic-gate 
32307c478bd9Sstevel@tonic-gate 	if (list->count == 0)
32317c478bd9Sstevel@tonic-gate 		return;
32327c478bd9Sstevel@tonic-gate 
32337c478bd9Sstevel@tonic-gate 	(void) puts(opt);
32347c478bd9Sstevel@tonic-gate 	for (i = 0; i < list->count; i++) {
32357c478bd9Sstevel@tonic-gate 		(void) printf("\t\targ[%d]: %s\n", i, list->optlist[i]);
32367c478bd9Sstevel@tonic-gate 	}
32377c478bd9Sstevel@tonic-gate }
32387c478bd9Sstevel@tonic-gate 
32397c478bd9Sstevel@tonic-gate /* return the number of arguments specified in the command line */
32407c478bd9Sstevel@tonic-gate static int
32417c478bd9Sstevel@tonic-gate num_args(clientopts_t *list)
32427c478bd9Sstevel@tonic-gate {
32437c478bd9Sstevel@tonic-gate 	int arg_count = 0;
32447c478bd9Sstevel@tonic-gate 
32457c478bd9Sstevel@tonic-gate 	arg_count += list->authenticationMethod ? 1 : 0;
32467c478bd9Sstevel@tonic-gate 	arg_count += list->serviceAuthenticationMethod->count;
32477c478bd9Sstevel@tonic-gate 	arg_count += list->defaultSearchBase ? 1 : 0;
32487c478bd9Sstevel@tonic-gate 	arg_count += list->credentialLevel ? 1 : 0;
32497c478bd9Sstevel@tonic-gate 	arg_count += list->serviceCredentialLevel->count;
32507c478bd9Sstevel@tonic-gate 	arg_count += list->domainName ? 1 : 0;
32517c478bd9Sstevel@tonic-gate 	arg_count += list->proxyDN ? 1 : 0;
3252dd1104fbSMichen Chang 	arg_count += list->enableShadowUpdate ? 1 : 0;
3253dd1104fbSMichen Chang 	arg_count += list->adminDN ? 1 : 0;
32547c478bd9Sstevel@tonic-gate 	arg_count += list->profileTTL ? 1 : 0;
32557c478bd9Sstevel@tonic-gate 	arg_count += list->objectclassMap->count;
32567c478bd9Sstevel@tonic-gate 	arg_count += list->searchTimeLimit ? 1 : 0;
32577c478bd9Sstevel@tonic-gate 	arg_count += list->preferredServerList ? 1 : 0;
32587c478bd9Sstevel@tonic-gate 	arg_count += list->profileName ? 1 : 0;
32597c478bd9Sstevel@tonic-gate 	arg_count += list->followReferrals ? 1 : 0;
32607c478bd9Sstevel@tonic-gate 	arg_count += list->attributeMap->count;
32617c478bd9Sstevel@tonic-gate 	arg_count += list->defaultSearchScope ? 1 : 0;
32627c478bd9Sstevel@tonic-gate 	arg_count += list->serviceSearchDescriptor->count;
32637c478bd9Sstevel@tonic-gate 	arg_count += list->bindTimeLimit ? 1 : 0;
32647c478bd9Sstevel@tonic-gate 	arg_count += list->proxyPassword ? 1 : 0;
3265dd1104fbSMichen Chang 	arg_count += list->adminPassword ? 1 : 0;
32667c478bd9Sstevel@tonic-gate 	arg_count += list->defaultServerList ? 1 : 0;
32677c478bd9Sstevel@tonic-gate 	arg_count += list->certificatePath ? 1 : 0;
32687c478bd9Sstevel@tonic-gate 
32697c478bd9Sstevel@tonic-gate 	return (arg_count);
32707c478bd9Sstevel@tonic-gate }
32717c478bd9Sstevel@tonic-gate 
32727c478bd9Sstevel@tonic-gate #define	CLIENT_PRINT(opt, str) if (str) \
32737c478bd9Sstevel@tonic-gate 		(void) printf("%s%s\n", (opt), (str))
32747c478bd9Sstevel@tonic-gate 
32757c478bd9Sstevel@tonic-gate static void
32767c478bd9Sstevel@tonic-gate dumpargs(clientopts_t *list)
32777c478bd9Sstevel@tonic-gate {
32787c478bd9Sstevel@tonic-gate 	CLIENT_PRINT("\tauthenticationMethod: ", list->authenticationMethod);
32797c478bd9Sstevel@tonic-gate 	multival_list("\tserviceAuthenticationMethod: ",
3280e1dd0a2fSth 	    list->serviceAuthenticationMethod);
32817c478bd9Sstevel@tonic-gate 	CLIENT_PRINT("\tdefaultSearchBase: ", list->defaultSearchBase);
32827c478bd9Sstevel@tonic-gate 	CLIENT_PRINT("\tcredentialLevel: ", list->credentialLevel);
32837c478bd9Sstevel@tonic-gate 	multival_list("\tserviceCredentialLevel: ",
3284e1dd0a2fSth 	    list->serviceCredentialLevel);
32857c478bd9Sstevel@tonic-gate 	CLIENT_PRINT("\tdomainName: ", list->domainName);
32867c478bd9Sstevel@tonic-gate 	CLIENT_PRINT("\tproxyDN: ", list->proxyDN);
3287dd1104fbSMichen Chang 	CLIENT_PRINT("\tadminDN: ", list->adminDN);
3288dd1104fbSMichen Chang 	CLIENT_PRINT("\tenableShadowUpdate: ", list->enableShadowUpdate);
32897c478bd9Sstevel@tonic-gate 	CLIENT_PRINT("\tprofileTTL: ", list->profileTTL);
32907c478bd9Sstevel@tonic-gate 	multival_list("\tobjectclassMap: ", list->objectclassMap);
32917c478bd9Sstevel@tonic-gate 	CLIENT_PRINT("\tsearchTimeLimit: ", list->searchTimeLimit);
32927c478bd9Sstevel@tonic-gate 	CLIENT_PRINT("\tpreferredServerList: ", list->preferredServerList);
32937c478bd9Sstevel@tonic-gate 	CLIENT_PRINT("\tprofileName: ", list->profileName);
32947c478bd9Sstevel@tonic-gate 	CLIENT_PRINT("\tfollowReferrals: ", list->followReferrals);
32957c478bd9Sstevel@tonic-gate 	multival_list("\tattributeMap: ", list->attributeMap);
32967c478bd9Sstevel@tonic-gate 	CLIENT_PRINT("\tdefaultSearchScope: ", list->defaultSearchScope);
32977c478bd9Sstevel@tonic-gate 	multival_list("\tserviceSearchDescriptor: ",
3298e1dd0a2fSth 	    list->serviceSearchDescriptor);
32997c478bd9Sstevel@tonic-gate 	CLIENT_PRINT("\tbindTimeLimit: ", list->bindTimeLimit);
33007c478bd9Sstevel@tonic-gate 	CLIENT_PRINT("\tproxyPassword: ", list->proxyPassword);
3301dd1104fbSMichen Chang 	CLIENT_PRINT("\tadminPassword: ", list->adminPassword);
33027c478bd9Sstevel@tonic-gate 	CLIENT_PRINT("\tdefaultServerList: ", list->defaultServerList);
33037c478bd9Sstevel@tonic-gate 	CLIENT_PRINT("\tcertificatePath: ", list->certificatePath);
33047c478bd9Sstevel@tonic-gate }
33057c478bd9Sstevel@tonic-gate 
33067c478bd9Sstevel@tonic-gate 
33077c478bd9Sstevel@tonic-gate /* These definitions are only used in parseParam() below. */
33087c478bd9Sstevel@tonic-gate struct param {
33097c478bd9Sstevel@tonic-gate 	char	*name;
33107c478bd9Sstevel@tonic-gate 	int	index;
33117c478bd9Sstevel@tonic-gate };
33127c478bd9Sstevel@tonic-gate 
33137c478bd9Sstevel@tonic-gate static struct param paramArray[] = {
33147c478bd9Sstevel@tonic-gate 	{"proxyDN", NS_LDAP_BINDDN_P},
33157c478bd9Sstevel@tonic-gate 	{"proxyPassword", NS_LDAP_BINDPASSWD_P},
33167c478bd9Sstevel@tonic-gate 	{"defaultServerList", NS_LDAP_SERVERS_P},
33177c478bd9Sstevel@tonic-gate 	{"defaultSearchBase", NS_LDAP_SEARCH_BASEDN_P},
33187c478bd9Sstevel@tonic-gate 	{"authenticationMethod", NS_LDAP_AUTH_P},
33197c478bd9Sstevel@tonic-gate 	{"followReferrals", NS_LDAP_SEARCH_REF_P},
33207c478bd9Sstevel@tonic-gate 	{"profileTTL", NS_LDAP_CACHETTL_P},
33217c478bd9Sstevel@tonic-gate 	{"certificatePath", NS_LDAP_HOST_CERTPATH_P},
33227c478bd9Sstevel@tonic-gate 	{"defaultSearchScope", NS_LDAP_SEARCH_SCOPE_P},
33237c478bd9Sstevel@tonic-gate 	{"bindTimeLimit", NS_LDAP_BIND_TIME_P},
33247c478bd9Sstevel@tonic-gate 	{"searchTimeLimit", NS_LDAP_SEARCH_TIME_P},
33257c478bd9Sstevel@tonic-gate 	{"preferredServerList", NS_LDAP_SERVER_PREF_P},
33267c478bd9Sstevel@tonic-gate 	{"profileName", NS_LDAP_PROFILE_P},
33277c478bd9Sstevel@tonic-gate 	{"credentialLevel", NS_LDAP_CREDENTIAL_LEVEL_P},
33287c478bd9Sstevel@tonic-gate 	{"serviceSearchDescriptor", NS_LDAP_SERVICE_SEARCH_DESC_P},
33297c478bd9Sstevel@tonic-gate 	{"attributeMap", NS_LDAP_ATTRIBUTEMAP_P},
33307c478bd9Sstevel@tonic-gate 	{"objectclassMap", NS_LDAP_OBJECTCLASSMAP_P},
33317c478bd9Sstevel@tonic-gate 	{"serviceAuthenticationMethod", NS_LDAP_SERVICE_AUTH_METHOD_P},
33327c478bd9Sstevel@tonic-gate 	{"serviceCredentialLevel", NS_LDAP_SERVICE_CRED_LEVEL_P},
33337c478bd9Sstevel@tonic-gate 	{"domainName", LOCAL_DOMAIN_P},
3334dd1104fbSMichen Chang 	{"enableShadowUpdate", NS_LDAP_ENABLE_SHADOW_UPDATE_P},
3335dd1104fbSMichen Chang 	{"adminDN", NS_LDAP_ADMIN_BINDDN_P},
3336dd1104fbSMichen Chang 	{"adminPassword", NS_LDAP_ADMIN_BINDPASSWD_P},
33377c478bd9Sstevel@tonic-gate 	{NULL, 0}
33387c478bd9Sstevel@tonic-gate };
33397c478bd9Sstevel@tonic-gate 
33407c478bd9Sstevel@tonic-gate static int
33417c478bd9Sstevel@tonic-gate parseParam(char *param, char **paramVal)
33427c478bd9Sstevel@tonic-gate {
33437c478bd9Sstevel@tonic-gate 	char *val = NULL;
33447c478bd9Sstevel@tonic-gate 	int counter;
33457c478bd9Sstevel@tonic-gate 
33467c478bd9Sstevel@tonic-gate 	if (mode_verbose) {
33477c478bd9Sstevel@tonic-gate 		CLIENT_FPRINTF(stderr, gettext("Parsing %s\n"), param);
33487c478bd9Sstevel@tonic-gate 	}
33497c478bd9Sstevel@tonic-gate 
33507c478bd9Sstevel@tonic-gate 	val = strchr(param, '=');
33517c478bd9Sstevel@tonic-gate 	if (val == NULL) {
33527c478bd9Sstevel@tonic-gate 		CLIENT_FPUTS(
3353e1dd0a2fSth 		    gettext("Didn\'t find \'=\' character in string\n"),
3354e1dd0a2fSth 		    stderr);
33557c478bd9Sstevel@tonic-gate 		paramVal = NULL;
33567c478bd9Sstevel@tonic-gate 		return (CLIENT_ERR_PARSE);
33577c478bd9Sstevel@tonic-gate 	}
33587c478bd9Sstevel@tonic-gate 
33597c478bd9Sstevel@tonic-gate 	*val = '\0';
33607c478bd9Sstevel@tonic-gate 
33617c478bd9Sstevel@tonic-gate 	for (counter = 0; paramArray[counter].name != NULL; counter++) {
33627c478bd9Sstevel@tonic-gate 		if (strcasecmp(paramArray[counter].name, param) == 0) {
33637c478bd9Sstevel@tonic-gate 			*paramVal = val+1;
33647c478bd9Sstevel@tonic-gate 			*val = '=';	/* restore original param */
33657c478bd9Sstevel@tonic-gate 			return (paramArray[counter].index);
33667c478bd9Sstevel@tonic-gate 		}
33677c478bd9Sstevel@tonic-gate 	}
33687c478bd9Sstevel@tonic-gate 
33697c478bd9Sstevel@tonic-gate 	/* Not found */
33707c478bd9Sstevel@tonic-gate 	*val = '=';	/* restore original param */
33717c478bd9Sstevel@tonic-gate 	*paramVal = NULL;
33727c478bd9Sstevel@tonic-gate 	return (CLIENT_ERR_PARSE);
33737c478bd9Sstevel@tonic-gate }
33747c478bd9Sstevel@tonic-gate 
33757c478bd9Sstevel@tonic-gate /*
33767c478bd9Sstevel@tonic-gate  * The following macro checks if an option has already been specified
33777c478bd9Sstevel@tonic-gate  * and errs out with usage if so
33787c478bd9Sstevel@tonic-gate  */
33797c478bd9Sstevel@tonic-gate #define	CLIENT_OPT_CHECK(opt, optarg)	\
33807c478bd9Sstevel@tonic-gate if (optarg) {			\
33817c478bd9Sstevel@tonic-gate 	CLIENT_FPUTS(gettext("Invalid use of option\n"), stderr);	\
33827c478bd9Sstevel@tonic-gate 	usage();		\
33837c478bd9Sstevel@tonic-gate 	clientopts_free(optlist); \
33847c478bd9Sstevel@tonic-gate 	return (CLIENT_ERR_FAIL);		\
33857c478bd9Sstevel@tonic-gate }
33867c478bd9Sstevel@tonic-gate 
33877c478bd9Sstevel@tonic-gate static int
33887c478bd9Sstevel@tonic-gate clientSetParam(clientopts_t *optlist, int paramFlag, char *attrVal)
33897c478bd9Sstevel@tonic-gate {
33907c478bd9Sstevel@tonic-gate 	int retcode = 0;
33917c478bd9Sstevel@tonic-gate 	int counter;
33927c478bd9Sstevel@tonic-gate 
33937c478bd9Sstevel@tonic-gate 
33947c478bd9Sstevel@tonic-gate 	switch (paramFlag) {
33957c478bd9Sstevel@tonic-gate 	case NS_LDAP_AUTH_P:
33967c478bd9Sstevel@tonic-gate 		CLIENT_OPT_CHECK(paramFlag, optlist->authenticationMethod);
33977c478bd9Sstevel@tonic-gate 		optlist->authenticationMethod = attrVal;
33987c478bd9Sstevel@tonic-gate 		break;
33997c478bd9Sstevel@tonic-gate 
34007c478bd9Sstevel@tonic-gate 	case NS_LDAP_SERVICE_AUTH_METHOD_P:	/* multiple allowed */
34017c478bd9Sstevel@tonic-gate 		retcode = multival_add(optlist->serviceAuthenticationMethod,
3402e1dd0a2fSth 		    attrVal);
34037c478bd9Sstevel@tonic-gate 		if (retcode != CLIENT_SUCCESS) {
34047c478bd9Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr,
3405e1dd0a2fSth 			    gettext("Error processing attrVal %s\n"),
3406e1dd0a2fSth 			    attrVal?attrVal:"NULL");
34077c478bd9Sstevel@tonic-gate 			usage();
34087c478bd9Sstevel@tonic-gate 			clientopts_free(optlist);
34097c478bd9Sstevel@tonic-gate 			return (CLIENT_ERR_FAIL);
34107c478bd9Sstevel@tonic-gate 		}
34117c478bd9Sstevel@tonic-gate 		break;
34127c478bd9Sstevel@tonic-gate 
34137c478bd9Sstevel@tonic-gate 	case NS_LDAP_SEARCH_BASEDN_P:
34147c478bd9Sstevel@tonic-gate 		CLIENT_OPT_CHECK(paramFlag, optlist->defaultSearchBase);
34157c478bd9Sstevel@tonic-gate 		optlist->defaultSearchBase = attrVal;
34167c478bd9Sstevel@tonic-gate 		break;
34177c478bd9Sstevel@tonic-gate 
34187c478bd9Sstevel@tonic-gate 	case NS_LDAP_CREDENTIAL_LEVEL_P:
34197c478bd9Sstevel@tonic-gate 		CLIENT_OPT_CHECK(paramFlag, optlist->credentialLevel);
34207c478bd9Sstevel@tonic-gate 		optlist->credentialLevel = attrVal;
34217c478bd9Sstevel@tonic-gate 		break;
34227c478bd9Sstevel@tonic-gate 
34237c478bd9Sstevel@tonic-gate 	case NS_LDAP_SERVICE_CRED_LEVEL_P:	/* multiple allowed */
34247c478bd9Sstevel@tonic-gate 		retcode = multival_add(optlist->serviceCredentialLevel,
3425e1dd0a2fSth 		    attrVal);
34267c478bd9Sstevel@tonic-gate 		if (retcode != CLIENT_SUCCESS) {
34277c478bd9Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr,
3428e1dd0a2fSth 			    gettext("Error processing attrVal %s\n"),
3429e1dd0a2fSth 			    attrVal?attrVal:"NULL");
34307c478bd9Sstevel@tonic-gate 			usage();
34317c478bd9Sstevel@tonic-gate 			clientopts_free(optlist);
34327c478bd9Sstevel@tonic-gate 			return (CLIENT_ERR_FAIL);
34337c478bd9Sstevel@tonic-gate 		}
34347c478bd9Sstevel@tonic-gate 		break;
34357c478bd9Sstevel@tonic-gate 
34367c478bd9Sstevel@tonic-gate 	case LOCAL_DOMAIN_P:
34377c478bd9Sstevel@tonic-gate 		CLIENT_OPT_CHECK(paramFlag, optlist->domainName);
34387c478bd9Sstevel@tonic-gate 		optlist->domainName = attrVal;
34397c478bd9Sstevel@tonic-gate 		dname = optlist->domainName;
34407c478bd9Sstevel@tonic-gate 		break;
34417c478bd9Sstevel@tonic-gate 
34427c478bd9Sstevel@tonic-gate 	case NS_LDAP_BINDDN_P:
34437c478bd9Sstevel@tonic-gate 		CLIENT_OPT_CHECK(paramFlag, optlist->proxyDN);
34447c478bd9Sstevel@tonic-gate 		optlist->proxyDN = attrVal;
34457c478bd9Sstevel@tonic-gate 		break;
34467c478bd9Sstevel@tonic-gate 
3447dd1104fbSMichen Chang 	case NS_LDAP_ENABLE_SHADOW_UPDATE_P:
3448dd1104fbSMichen Chang 		CLIENT_OPT_CHECK(paramFlag, optlist->enableShadowUpdate);
3449dd1104fbSMichen Chang 		optlist->enableShadowUpdate = attrVal;
3450dd1104fbSMichen Chang 		break;
3451dd1104fbSMichen Chang 
3452dd1104fbSMichen Chang 	case NS_LDAP_ADMIN_BINDDN_P:
3453dd1104fbSMichen Chang 		CLIENT_OPT_CHECK(paramFlag, optlist->adminDN);
3454dd1104fbSMichen Chang 		optlist->adminDN = attrVal;
3455dd1104fbSMichen Chang 		break;
3456dd1104fbSMichen Chang 
34577c478bd9Sstevel@tonic-gate 	case NS_LDAP_CACHETTL_P:
34587c478bd9Sstevel@tonic-gate 		CLIENT_OPT_CHECK(paramFlag, optlist->profileTTL);
34597c478bd9Sstevel@tonic-gate 		optlist->profileTTL = attrVal;
34607c478bd9Sstevel@tonic-gate 		break;
34617c478bd9Sstevel@tonic-gate 
34627c478bd9Sstevel@tonic-gate 	case NS_LDAP_OBJECTCLASSMAP_P:	/* multiple allowed */
34637c478bd9Sstevel@tonic-gate 		retcode = multival_add(optlist->objectclassMap, attrVal);
34647c478bd9Sstevel@tonic-gate 		if (retcode != CLIENT_SUCCESS) {
34657c478bd9Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr,
3466e1dd0a2fSth 			    gettext("Error processing attrVal %s\n"),
3467e1dd0a2fSth 			    attrVal?attrVal:"NULL");
34687c478bd9Sstevel@tonic-gate 			usage();
34697c478bd9Sstevel@tonic-gate 			clientopts_free(optlist);
34707c478bd9Sstevel@tonic-gate 			return (CLIENT_ERR_FAIL);
34717c478bd9Sstevel@tonic-gate 		}
34727c478bd9Sstevel@tonic-gate 		break;
34737c478bd9Sstevel@tonic-gate 
34747c478bd9Sstevel@tonic-gate 	case NS_LDAP_SEARCH_TIME_P:
34757c478bd9Sstevel@tonic-gate 		CLIENT_OPT_CHECK(paramFlag, optlist->searchTimeLimit);
34767c478bd9Sstevel@tonic-gate 		optlist->searchTimeLimit = attrVal;
34777c478bd9Sstevel@tonic-gate 		break;
34787c478bd9Sstevel@tonic-gate 
34797c478bd9Sstevel@tonic-gate 	case NS_LDAP_SERVER_PREF_P:
34807c478bd9Sstevel@tonic-gate 		CLIENT_OPT_CHECK(paramFlag, optlist->preferredServerList);
34817c478bd9Sstevel@tonic-gate 		optlist->preferredServerList = attrVal;
34827c478bd9Sstevel@tonic-gate 		/* replace ',' chars with ' ' for proper syntax */
34837c478bd9Sstevel@tonic-gate 		for (counter = 0;
3484e1dd0a2fSth 		    counter < strlen(optlist->preferredServerList);
3485e1dd0a2fSth 		    counter++) {
34867c478bd9Sstevel@tonic-gate 
34877c478bd9Sstevel@tonic-gate 			if (optlist->preferredServerList[counter] == ',')
34887c478bd9Sstevel@tonic-gate 				optlist->preferredServerList[counter] = ' ';
34897c478bd9Sstevel@tonic-gate 		}
34907c478bd9Sstevel@tonic-gate 		break;
34917c478bd9Sstevel@tonic-gate 
34927c478bd9Sstevel@tonic-gate 	case NS_LDAP_PROFILE_P:
34937c478bd9Sstevel@tonic-gate 		CLIENT_OPT_CHECK(paramFlag, optlist->profileName);
34947c478bd9Sstevel@tonic-gate 		optlist->profileName = attrVal;
34957c478bd9Sstevel@tonic-gate 		break;
34967c478bd9Sstevel@tonic-gate 
34977c478bd9Sstevel@tonic-gate 	case NS_LDAP_SEARCH_REF_P:
34987c478bd9Sstevel@tonic-gate 		CLIENT_OPT_CHECK(paramFlag, optlist->followReferrals);
34997c478bd9Sstevel@tonic-gate 		if (0 == strcasecmp(attrVal, "followref"))
35007c478bd9Sstevel@tonic-gate 			optlist->followReferrals = "TRUE";
35017c478bd9Sstevel@tonic-gate 		else if (0 == strcasecmp(attrVal, "noref"))
35027c478bd9Sstevel@tonic-gate 			optlist->followReferrals = "FALSE";
35037c478bd9Sstevel@tonic-gate 		else
35047c478bd9Sstevel@tonic-gate 			optlist->followReferrals = attrVal;
35057c478bd9Sstevel@tonic-gate 		break;
35067c478bd9Sstevel@tonic-gate 
35077c478bd9Sstevel@tonic-gate 	case NS_LDAP_ATTRIBUTEMAP_P:	/* multiple allowed */
35087c478bd9Sstevel@tonic-gate 		retcode = multival_add(optlist->attributeMap, attrVal);
35097c478bd9Sstevel@tonic-gate 		if (retcode != CLIENT_SUCCESS) {
35107c478bd9Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr,
3511e1dd0a2fSth 			    gettext("Error processing attrVal %s\n"),
3512e1dd0a2fSth 			    attrVal?attrVal:"NULL");
35137c478bd9Sstevel@tonic-gate 			usage();
35147c478bd9Sstevel@tonic-gate 			clientopts_free(optlist);
35157c478bd9Sstevel@tonic-gate 			return (CLIENT_ERR_FAIL);
35167c478bd9Sstevel@tonic-gate 		}
35177c478bd9Sstevel@tonic-gate 		break;
35187c478bd9Sstevel@tonic-gate 
35197c478bd9Sstevel@tonic-gate 	case NS_LDAP_SEARCH_SCOPE_P:
35207c478bd9Sstevel@tonic-gate 		CLIENT_OPT_CHECK(paramFlag, optlist->defaultSearchScope);
35217c478bd9Sstevel@tonic-gate 		optlist->defaultSearchScope = attrVal;
35227c478bd9Sstevel@tonic-gate 		break;
35237c478bd9Sstevel@tonic-gate 
35247c478bd9Sstevel@tonic-gate 	case NS_LDAP_SERVICE_SEARCH_DESC_P:	/* multiple allowed */
35257c478bd9Sstevel@tonic-gate 		retcode = multival_add(optlist->serviceSearchDescriptor,
3526e1dd0a2fSth 		    attrVal);
35277c478bd9Sstevel@tonic-gate 		if (retcode != CLIENT_SUCCESS) {
35287c478bd9Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr,
3529e1dd0a2fSth 			    gettext("Error processing attrVal %s\n"),
3530e1dd0a2fSth 			    attrVal?attrVal:"NULL");
35317c478bd9Sstevel@tonic-gate 			usage();
35327c478bd9Sstevel@tonic-gate 			clientopts_free(optlist);
35337c478bd9Sstevel@tonic-gate 			return (CLIENT_ERR_FAIL);
35347c478bd9Sstevel@tonic-gate 		}
35357c478bd9Sstevel@tonic-gate 		break;
35367c478bd9Sstevel@tonic-gate 
35377c478bd9Sstevel@tonic-gate 	case NS_LDAP_BIND_TIME_P:
35387c478bd9Sstevel@tonic-gate 		CLIENT_OPT_CHECK(paramFlag, optlist->bindTimeLimit);
35397c478bd9Sstevel@tonic-gate 		optlist->bindTimeLimit = attrVal;
35407c478bd9Sstevel@tonic-gate 		break;
35417c478bd9Sstevel@tonic-gate 
35427c478bd9Sstevel@tonic-gate 	case NS_LDAP_BINDPASSWD_P:
35437c478bd9Sstevel@tonic-gate 		CLIENT_OPT_CHECK(paramFlag, optlist->proxyPassword);
35447c478bd9Sstevel@tonic-gate 		optlist->proxyPassword = attrVal;
35457c478bd9Sstevel@tonic-gate 		break;
35467c478bd9Sstevel@tonic-gate 
3547dd1104fbSMichen Chang 	case NS_LDAP_ADMIN_BINDPASSWD_P:
3548dd1104fbSMichen Chang 		CLIENT_OPT_CHECK(paramFlag, optlist->adminPassword);
3549dd1104fbSMichen Chang 		optlist->adminPassword = attrVal;
3550dd1104fbSMichen Chang 		break;
3551dd1104fbSMichen Chang 
35527c478bd9Sstevel@tonic-gate 	case NS_LDAP_HOST_CERTPATH_P:
35537c478bd9Sstevel@tonic-gate 		CLIENT_OPT_CHECK(paramFlag, optlist->certificatePath);
35547c478bd9Sstevel@tonic-gate 		optlist->certificatePath = attrVal;
35557c478bd9Sstevel@tonic-gate 		break;
35567c478bd9Sstevel@tonic-gate 
35577c478bd9Sstevel@tonic-gate 	case NS_LDAP_SERVERS_P:
35587c478bd9Sstevel@tonic-gate 		CLIENT_OPT_CHECK(paramFlag, optlist->defaultServerList);
35597c478bd9Sstevel@tonic-gate 		optlist->defaultServerList = attrVal;
35607c478bd9Sstevel@tonic-gate 		break;
35617c478bd9Sstevel@tonic-gate 
35627c478bd9Sstevel@tonic-gate 	default:
35637c478bd9Sstevel@tonic-gate 		usage();
35647c478bd9Sstevel@tonic-gate 		return (CLIENT_ERR_FAIL);
35657c478bd9Sstevel@tonic-gate 		/* break;  lint doesn't like break before end of switch */
35667c478bd9Sstevel@tonic-gate 	}
35677c478bd9Sstevel@tonic-gate 
35687c478bd9Sstevel@tonic-gate 	return (retcode);
35697c478bd9Sstevel@tonic-gate }
35707c478bd9Sstevel@tonic-gate 
35717c478bd9Sstevel@tonic-gate /*
35727c478bd9Sstevel@tonic-gate  * file_move() - Used to move a config file (backup/restore).
35737c478bd9Sstevel@tonic-gate  *
35747c478bd9Sstevel@tonic-gate  * This function uses a system() call with /bin/mv to handle the
35757c478bd9Sstevel@tonic-gate  * case where the backup directory (/var) is on a different file
35767c478bd9Sstevel@tonic-gate  * system than the config file (typically /etc).
35777c478bd9Sstevel@tonic-gate  */
35787c478bd9Sstevel@tonic-gate static int
35797c478bd9Sstevel@tonic-gate file_move(const char *from, const char *to)
35807c478bd9Sstevel@tonic-gate {
35817c478bd9Sstevel@tonic-gate 	int retcode;
35827c478bd9Sstevel@tonic-gate 	char mvCommand[] = CMD_MV;
35837c478bd9Sstevel@tonic-gate 	char cmd_buffer[(2 * MAXPATHLEN) + sizeof (mvCommand) + 3];
35847c478bd9Sstevel@tonic-gate 
35857c478bd9Sstevel@tonic-gate 	(void) snprintf(cmd_buffer, sizeof (cmd_buffer), "%s %s %s",
3586e1dd0a2fSth 	    mvCommand, from, to);
35877c478bd9Sstevel@tonic-gate 
35887c478bd9Sstevel@tonic-gate 	/*
35897c478bd9Sstevel@tonic-gate 	 * This function should only be used internally to move
35907c478bd9Sstevel@tonic-gate 	 * system files to/from the backup directory.  For security
35917c478bd9Sstevel@tonic-gate 	 * reasons (this is run as root), don't use this function
35927c478bd9Sstevel@tonic-gate 	 * with arguments passed into the program.
35937c478bd9Sstevel@tonic-gate 	 */
35947c478bd9Sstevel@tonic-gate 	retcode = system(cmd_buffer);
35957c478bd9Sstevel@tonic-gate 
35967c478bd9Sstevel@tonic-gate 	return (retcode);
35977c478bd9Sstevel@tonic-gate }
35987c478bd9Sstevel@tonic-gate 
35997c478bd9Sstevel@tonic-gate 
36007c478bd9Sstevel@tonic-gate /*
36017c478bd9Sstevel@tonic-gate  * Manipulate the service as instructed by "dowhat"
36027c478bd9Sstevel@tonic-gate  */
36037c478bd9Sstevel@tonic-gate static int
36047c478bd9Sstevel@tonic-gate do_service(const char *fmri, boolean_t waitflag, int dowhat,
36057c478bd9Sstevel@tonic-gate 		const char *state) {
36067c478bd9Sstevel@tonic-gate 
36077c478bd9Sstevel@tonic-gate 	int		status;
36087c478bd9Sstevel@tonic-gate 	boolean_t	is_maint;
36097c478bd9Sstevel@tonic-gate 	const char	*what = gettext("not set");
36107c478bd9Sstevel@tonic-gate 	useconds_t	max;
36117c478bd9Sstevel@tonic-gate 
36127c478bd9Sstevel@tonic-gate 	/* Check if we are in maintenance */
36137c478bd9Sstevel@tonic-gate 	is_maint = is_service(fmri, SCF_STATE_STRING_MAINT);
36147c478bd9Sstevel@tonic-gate 
36157c478bd9Sstevel@tonic-gate 	switch (dowhat) {
36167c478bd9Sstevel@tonic-gate 	case START_SERVICE:
36177c478bd9Sstevel@tonic-gate 		what = gettext("start");
36187c478bd9Sstevel@tonic-gate 		status = smf_enable_instance(fmri,
36197c478bd9Sstevel@tonic-gate 			(sysid_install == B_TRUE)?SMF_TEMPORARY:0);
36207c478bd9Sstevel@tonic-gate 		break;
36217c478bd9Sstevel@tonic-gate 	case STOP_SERVICE:
36227c478bd9Sstevel@tonic-gate 		what = gettext("stop");
36237c478bd9Sstevel@tonic-gate 		status = smf_disable_instance(fmri,
36247c478bd9Sstevel@tonic-gate 			(sysid_install == B_TRUE)?SMF_TEMPORARY:0);
36257c478bd9Sstevel@tonic-gate 		break;
36267c478bd9Sstevel@tonic-gate 	case RESTART_SERVICE:
36277c478bd9Sstevel@tonic-gate 		what = gettext("restart");
36287c478bd9Sstevel@tonic-gate 		status = smf_restart_instance(fmri);
36297c478bd9Sstevel@tonic-gate 		break;
36307c478bd9Sstevel@tonic-gate 	default:
36317c478bd9Sstevel@tonic-gate 		/* coding error; will not happen */
36327c478bd9Sstevel@tonic-gate 		assert(0);
36337c478bd9Sstevel@tonic-gate 	}
36347c478bd9Sstevel@tonic-gate 
36357c478bd9Sstevel@tonic-gate 	/*
36367c478bd9Sstevel@tonic-gate 	 * If the service was previously in maintenance then we need to
36377c478bd9Sstevel@tonic-gate 	 * clear it immediately.  The "dowhat" action will set the
36387c478bd9Sstevel@tonic-gate 	 * enabled property of the service as intended by the caller while
36397c478bd9Sstevel@tonic-gate 	 * clear will actually cause it to be enabled/disabled.
36407c478bd9Sstevel@tonic-gate 	 * We assume that the caller has called us after taking some
36417c478bd9Sstevel@tonic-gate 	 * recovery action. Even if it's not the case, we don't lose
36427c478bd9Sstevel@tonic-gate 	 * anything.
36437c478bd9Sstevel@tonic-gate 	 */
36447c478bd9Sstevel@tonic-gate 	if (status == 0 && is_maint == B_TRUE) {
36457c478bd9Sstevel@tonic-gate 		if (mode_verbose)
36467c478bd9Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr,
36477c478bd9Sstevel@tonic-gate 				"%s: %s... %s\n",
36487c478bd9Sstevel@tonic-gate 				what,
36497c478bd9Sstevel@tonic-gate 				fmri,
36507c478bd9Sstevel@tonic-gate 				gettext("restoring from maintenance state"));
36517c478bd9Sstevel@tonic-gate 		status = smf_restore_instance(fmri);
36527c478bd9Sstevel@tonic-gate 	}
36537c478bd9Sstevel@tonic-gate 
36547c478bd9Sstevel@tonic-gate 	if (status == 0) {
36557c478bd9Sstevel@tonic-gate 		/* Check if we need to wait ? */
36567c478bd9Sstevel@tonic-gate 		if (waitflag == B_FALSE) {
36577c478bd9Sstevel@tonic-gate 			if (mode_verbose)
36587c478bd9Sstevel@tonic-gate 				CLIENT_FPRINTF(stderr,
36597c478bd9Sstevel@tonic-gate 					"%s: %s... %s\n",
36607c478bd9Sstevel@tonic-gate 					what,
36617c478bd9Sstevel@tonic-gate 					fmri,
36627c478bd9Sstevel@tonic-gate 					gettext("success"));
36637c478bd9Sstevel@tonic-gate 			return (CLIENT_SUCCESS);
36647c478bd9Sstevel@tonic-gate 		}
36657c478bd9Sstevel@tonic-gate 
36667c478bd9Sstevel@tonic-gate 		/* Otherwise wait for max seconds (from the manifest) */
36677c478bd9Sstevel@tonic-gate 		max = get_timeout_value(dowhat, fmri, DEFAULT_TIMEOUT);
36687c478bd9Sstevel@tonic-gate 		status = wait_till(fmri, state, max, what, !is_maint);
36697c478bd9Sstevel@tonic-gate 		if (status == CLIENT_SUCCESS)
36707c478bd9Sstevel@tonic-gate 			return (CLIENT_SUCCESS);
36717c478bd9Sstevel@tonic-gate 		/* For error fall through for corrective action */
36727c478bd9Sstevel@tonic-gate 	} else {
36737c478bd9Sstevel@tonic-gate 		/* Well, service failed ... */
36747c478bd9Sstevel@tonic-gate 		if (mode_verbose)
36757c478bd9Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr, "%s: %s... %s: %s\n",
36767c478bd9Sstevel@tonic-gate 				what,
36777c478bd9Sstevel@tonic-gate 				fmri,
36787c478bd9Sstevel@tonic-gate 				gettext("failed"),
36797c478bd9Sstevel@tonic-gate 				scf_strerror(scf_error()));
36807c478bd9Sstevel@tonic-gate 		status = CLIENT_ERR_FAIL;
36817c478bd9Sstevel@tonic-gate 		/* For error fall through for corrective action */
36827c478bd9Sstevel@tonic-gate 	}
36837c478bd9Sstevel@tonic-gate 
36847c478bd9Sstevel@tonic-gate 	/*
36857c478bd9Sstevel@tonic-gate 	 * If service is still offline after start/restart, then transitioning
36867c478bd9Sstevel@tonic-gate 	 * failed and guess is restarter failed to apply the timeout as well.
36877c478bd9Sstevel@tonic-gate 	 * So instead of leaving it offline, let's just disable it until we have
36887c478bd9Sstevel@tonic-gate 	 * some other mechanism available from smf to handle such situation.
36897c478bd9Sstevel@tonic-gate 	 */
36907c478bd9Sstevel@tonic-gate 	if (dowhat != STOP_SERVICE)
36917c478bd9Sstevel@tonic-gate 		if (is_service(fmri, SCF_STATE_STRING_OFFLINE)) {
36927c478bd9Sstevel@tonic-gate 			if (mode_verbose)
36937c478bd9Sstevel@tonic-gate 				CLIENT_FPRINTF(stderr,
36947c478bd9Sstevel@tonic-gate 					"%s: %s... %s\n",
36957c478bd9Sstevel@tonic-gate 					what,
36967c478bd9Sstevel@tonic-gate 					fmri,
36977c478bd9Sstevel@tonic-gate 					gettext("offline to disable"));
36987c478bd9Sstevel@tonic-gate 			(void) disable_service(fmri, waitflag);
36997c478bd9Sstevel@tonic-gate 		}
37007c478bd9Sstevel@tonic-gate 
37017c478bd9Sstevel@tonic-gate 	return (status);
37027c478bd9Sstevel@tonic-gate }
37037c478bd9Sstevel@tonic-gate 
37047c478bd9Sstevel@tonic-gate 
37057c478bd9Sstevel@tonic-gate /*
37067c478bd9Sstevel@tonic-gate  * Wait for "max" usecs for the service described by "fmri" to change
37077c478bd9Sstevel@tonic-gate  * to "state". If check_maint is true then return immediately if
37087c478bd9Sstevel@tonic-gate  * service goes into maintenance
37097c478bd9Sstevel@tonic-gate  */
37107c478bd9Sstevel@tonic-gate static int
37117c478bd9Sstevel@tonic-gate wait_till(const char *fmri, const char *state, useconds_t max,
37127c478bd9Sstevel@tonic-gate 		const char *what, boolean_t check_maint) {
37137c478bd9Sstevel@tonic-gate 	char *st;
37147c478bd9Sstevel@tonic-gate 	useconds_t usecs = INIT_WAIT_USECS;
37157c478bd9Sstevel@tonic-gate 
37167c478bd9Sstevel@tonic-gate 	for (; max > 0; max -= usecs) {
37177c478bd9Sstevel@tonic-gate 		/* incremental wait */
37187c478bd9Sstevel@tonic-gate 		usecs *= 2;
37197c478bd9Sstevel@tonic-gate 		usecs = (usecs > max)?max:usecs;
37207c478bd9Sstevel@tonic-gate 		if (mode_verbose)
37217c478bd9Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr,
37227c478bd9Sstevel@tonic-gate 				"%s: %s %u %s\n",
37237c478bd9Sstevel@tonic-gate 				what, gettext("sleep"), usecs,
37247c478bd9Sstevel@tonic-gate 				gettext("microseconds"));
37257c478bd9Sstevel@tonic-gate 		(void) usleep(usecs);
37267c478bd9Sstevel@tonic-gate 
37277c478bd9Sstevel@tonic-gate 		/* Check state after the wait */
37287c478bd9Sstevel@tonic-gate 		if ((st = smf_get_state(fmri)) != NULL) {
37297c478bd9Sstevel@tonic-gate 			if (strcmp(st, state) == 0) {
37307c478bd9Sstevel@tonic-gate 				if (mode_verbose)
37317c478bd9Sstevel@tonic-gate 					CLIENT_FPRINTF(stderr,
37327c478bd9Sstevel@tonic-gate 						"%s: %s... %s\n",
37337c478bd9Sstevel@tonic-gate 						what,
37347c478bd9Sstevel@tonic-gate 						fmri,
37357c478bd9Sstevel@tonic-gate 						gettext("success"));
37367c478bd9Sstevel@tonic-gate 				free(st);
37377c478bd9Sstevel@tonic-gate 				return (CLIENT_SUCCESS);
37387c478bd9Sstevel@tonic-gate 			}
37397c478bd9Sstevel@tonic-gate 
37407c478bd9Sstevel@tonic-gate 			/*
37417c478bd9Sstevel@tonic-gate 			 * If service has gone into maintenance then
37427c478bd9Sstevel@tonic-gate 			 * we will time out anyway, so we are better
37437c478bd9Sstevel@tonic-gate 			 * off returning now
37447c478bd9Sstevel@tonic-gate 			 */
37457c478bd9Sstevel@tonic-gate 			if (check_maint &&
37467c478bd9Sstevel@tonic-gate 				strcmp(st, SCF_STATE_STRING_MAINT) == 0) {
37477c478bd9Sstevel@tonic-gate 				if (mode_verbose)
37487c478bd9Sstevel@tonic-gate 					CLIENT_FPRINTF(stderr,
37497c478bd9Sstevel@tonic-gate 						"%s: %s... %s\n",
37507c478bd9Sstevel@tonic-gate 						what,
37517c478bd9Sstevel@tonic-gate 						fmri,
37527c478bd9Sstevel@tonic-gate 						gettext("maintenance"));
37537c478bd9Sstevel@tonic-gate 				free(st);
37547c478bd9Sstevel@tonic-gate 				return (CLIENT_ERR_MAINTENANCE);
37557c478bd9Sstevel@tonic-gate 			}
37567c478bd9Sstevel@tonic-gate 			free(st);
37577c478bd9Sstevel@tonic-gate 		} else {
37587c478bd9Sstevel@tonic-gate 			if (mode_verbose)
37597c478bd9Sstevel@tonic-gate 				CLIENT_FPRINTF(stderr,
37607c478bd9Sstevel@tonic-gate 						"%s: %s... %s: %s\n",
37617c478bd9Sstevel@tonic-gate 						what,
37627c478bd9Sstevel@tonic-gate 						fmri,
37637c478bd9Sstevel@tonic-gate 						gettext("failed"),
37647c478bd9Sstevel@tonic-gate 						scf_strerror(scf_error()));
37657c478bd9Sstevel@tonic-gate 			return (CLIENT_ERR_FAIL);
37667c478bd9Sstevel@tonic-gate 		}
37677c478bd9Sstevel@tonic-gate 	}
37687c478bd9Sstevel@tonic-gate 
37697c478bd9Sstevel@tonic-gate 	/* Timed out waiting */
37707c478bd9Sstevel@tonic-gate 	if (mode_verbose)
37717c478bd9Sstevel@tonic-gate 		CLIENT_FPRINTF(stderr,
37727c478bd9Sstevel@tonic-gate 			"%s: %s... %s\n",
37737c478bd9Sstevel@tonic-gate 			what,
37747c478bd9Sstevel@tonic-gate 			fmri,
37757c478bd9Sstevel@tonic-gate 			gettext("timed out"));
37767c478bd9Sstevel@tonic-gate 	return (CLIENT_ERR_TIMEDOUT);
37777c478bd9Sstevel@tonic-gate }
37787c478bd9Sstevel@tonic-gate 
37797c478bd9Sstevel@tonic-gate 
37807c478bd9Sstevel@tonic-gate static boolean_t
37817c478bd9Sstevel@tonic-gate is_service(const char *fmri, const char *state) {
37827c478bd9Sstevel@tonic-gate 	char		*st;
37837c478bd9Sstevel@tonic-gate 	boolean_t	result = B_FALSE;
37847c478bd9Sstevel@tonic-gate 
37857c478bd9Sstevel@tonic-gate 	if ((st = smf_get_state(fmri)) != NULL) {
37867c478bd9Sstevel@tonic-gate 		if (strcmp(st, state) == 0)
37877c478bd9Sstevel@tonic-gate 			result = B_TRUE;
37887c478bd9Sstevel@tonic-gate 		free(st);
37897c478bd9Sstevel@tonic-gate 	}
37907c478bd9Sstevel@tonic-gate 	return (result);
37917c478bd9Sstevel@tonic-gate }
37927c478bd9Sstevel@tonic-gate 
37937c478bd9Sstevel@tonic-gate 
37947c478bd9Sstevel@tonic-gate /*
37957c478bd9Sstevel@tonic-gate  *
37967c478bd9Sstevel@tonic-gate  * get_timeout_val : returns the timeout value set in fmri manifest
37977c478bd9Sstevel@tonic-gate  * 	inputs	: action(start/stop)
37987c478bd9Sstevel@tonic-gate  *	fmri(defined fmri string)
37997c478bd9Sstevel@tonic-gate  *	Returns default if error, the timeout val otherwise
38007c478bd9Sstevel@tonic-gate  *
38017c478bd9Sstevel@tonic-gate  */
38027c478bd9Sstevel@tonic-gate 
38037c478bd9Sstevel@tonic-gate static useconds_t
38047c478bd9Sstevel@tonic-gate get_timeout_value(int dowhat, const char *fmri, useconds_t default_val)
38057c478bd9Sstevel@tonic-gate {
38067c478bd9Sstevel@tonic-gate 	scf_simple_prop_t	*sp = NULL;
38077c478bd9Sstevel@tonic-gate 	uint64_t		*cp = NULL;
38087c478bd9Sstevel@tonic-gate 	int			timeout = default_val/1000000;
38097c478bd9Sstevel@tonic-gate 	char			*action = NULL;
38107c478bd9Sstevel@tonic-gate 	const char		*actionstr = NULL;
38117c478bd9Sstevel@tonic-gate 
38127c478bd9Sstevel@tonic-gate 	switch (dowhat)  {
38137c478bd9Sstevel@tonic-gate 		case START_SERVICE:
38147c478bd9Sstevel@tonic-gate 		case RESTART_SERVICE:
38157c478bd9Sstevel@tonic-gate 				action = "start";
38167c478bd9Sstevel@tonic-gate 				actionstr = gettext("start");
38177c478bd9Sstevel@tonic-gate 				break;
38187c478bd9Sstevel@tonic-gate 		case STOP_SERVICE:
38197c478bd9Sstevel@tonic-gate 				action = "stop";
38207c478bd9Sstevel@tonic-gate 				actionstr = gettext("stop");
38217c478bd9Sstevel@tonic-gate 				break;
38227c478bd9Sstevel@tonic-gate 		default:
38237c478bd9Sstevel@tonic-gate 			assert(0);
38247c478bd9Sstevel@tonic-gate 	}
38257c478bd9Sstevel@tonic-gate 
38267c478bd9Sstevel@tonic-gate 
38277c478bd9Sstevel@tonic-gate 	sp = scf_simple_prop_get(NULL, fmri, action, SCF_PROPERTY_TIMEOUT);
38287c478bd9Sstevel@tonic-gate 	if (sp == NULL) {
38297c478bd9Sstevel@tonic-gate 		if (mode_verbose)
38307c478bd9Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr, "%s: %s... %s: %s\n",
3831e1dd0a2fSth 			    actionstr,
3832e1dd0a2fSth 			    fmri,
3833e1dd0a2fSth 			    gettext("failed to retrieve timeout property"),
3834e1dd0a2fSth 			    scf_strerror(scf_error()));
38357c478bd9Sstevel@tonic-gate 		return (default_val);
38367c478bd9Sstevel@tonic-gate 	}
38377c478bd9Sstevel@tonic-gate 
38387c478bd9Sstevel@tonic-gate 	cp = scf_simple_prop_next_count(sp);
38397c478bd9Sstevel@tonic-gate 	if (cp == NULL) {
38407c478bd9Sstevel@tonic-gate 		if (mode_verbose)
38417c478bd9Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr, "%s: %s... %s: %s\n",
3842e1dd0a2fSth 			    actionstr,
3843e1dd0a2fSth 			    fmri,
3844e1dd0a2fSth 			    gettext("failed to retrieve timeout value"),
3845e1dd0a2fSth 			    scf_strerror(scf_error()));
38467c478bd9Sstevel@tonic-gate 		scf_simple_prop_free(sp);
38477c478bd9Sstevel@tonic-gate 		return (default_val);
38487c478bd9Sstevel@tonic-gate 	}
38497c478bd9Sstevel@tonic-gate 
38507c478bd9Sstevel@tonic-gate 	if (*cp != 0)
38517c478bd9Sstevel@tonic-gate 		timeout = *cp;
38527c478bd9Sstevel@tonic-gate 	scf_simple_prop_free(sp);
38537c478bd9Sstevel@tonic-gate 	return (timeout * 1000000);
38547c478bd9Sstevel@tonic-gate }
3855