xref: /titanic_44/usr/src/cmd/ldap/ns_ldap/ldapclient.c (revision 33f5ff17089e3a43e6e730bf80384c233123dbd9)
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>
123e1dd0a2fSth160488 
124e1dd0a2fSth160488 #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;
275e1dd0a2fSth160488 	char		*bindDN;
276e1dd0a2fSth160488 	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 
325a506a34cSth160488 int
main(int argc,char ** argv)326a506a34cSth160488 main(int argc, char **argv)
3277c478bd9Sstevel@tonic-gate {
3287c478bd9Sstevel@tonic-gate 	char		*ret_locale, *ret_textdomain;
3297c478bd9Sstevel@tonic-gate 	int		retcode;
3307c478bd9Sstevel@tonic-gate 	int		paramFlag;
3317c478bd9Sstevel@tonic-gate 	char		*attrVal;
3327c478bd9Sstevel@tonic-gate 	int		sysinfostatus;
3337c478bd9Sstevel@tonic-gate 	clientopts_t	*optlist = NULL;
3347c478bd9Sstevel@tonic-gate 	int		op_manual = 0, op_mod = 0, op_uninit = 0;
3357c478bd9Sstevel@tonic-gate 	int		op_list = 0, op_init = 0, op_genprofile = 0;
3367c478bd9Sstevel@tonic-gate 	extern char	*optarg;
3377c478bd9Sstevel@tonic-gate 	extern int	optind;
3387c478bd9Sstevel@tonic-gate 	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(
3647c478bd9Sstevel@tonic-gate 		    gettext("Error getting optlist (malloc fail)\n"),
3657c478bd9Sstevel@tonic-gate 		    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,
3857c478bd9Sstevel@tonic-gate 				    gettext("Unrecognized "
3867c478bd9Sstevel@tonic-gate 				    "parameter \"%s\"\n"),
3877c478bd9Sstevel@tonic-gate 				    optarg);
3887c478bd9Sstevel@tonic-gate 				usage();
3897c478bd9Sstevel@tonic-gate 				exit(CLIENT_ERR_FAIL);
3907c478bd9Sstevel@tonic-gate 			}
391e1dd0a2fSth160488 			if (paramFlag == NS_LDAP_BINDPASSWD_P &&
392e1dd0a2fSth160488 			    optlist->proxyPassword != NULL) {
393e1dd0a2fSth160488 				(void) fprintf(stderr,
394e1dd0a2fSth160488 				    gettext("The -a proxyPassword option is "
395e1dd0a2fSth160488 				    "mutually exclusive of -y. "
396e1dd0a2fSth160488 				    "-a proxyPassword is ignored.\n"));
397e1dd0a2fSth160488 				break;
398e1dd0a2fSth160488 			}
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(
4107c478bd9Sstevel@tonic-gate 				    stderr,
4117c478bd9Sstevel@tonic-gate 				    gettext("Error (%d) setting "
4127c478bd9Sstevel@tonic-gate 				    "param \"%s\"\n"),
4137c478bd9Sstevel@tonic-gate 				    retcode, optarg);
4147c478bd9Sstevel@tonic-gate 				usage();
4157c478bd9Sstevel@tonic-gate 				exit(CLIENT_ERR_FAIL);
4167c478bd9Sstevel@tonic-gate 			}
4177c478bd9Sstevel@tonic-gate 			break;
418e1dd0a2fSth160488 		case 'D':
419e1dd0a2fSth160488 			optlist->bindDN = strdup(optarg);
420e1dd0a2fSth160488 			break;
421e1dd0a2fSth160488 		case 'w':
422e1dd0a2fSth160488 			if (optlist->bindPasswd != NULL) {
423e1dd0a2fSth160488 				CLIENT_FPRINTF(stderr,
424e1dd0a2fSth160488 				    gettext("The -w option is mutually "
425e1dd0a2fSth160488 				    "exclusive of -j. -w is ignored."));
426e1dd0a2fSth160488 				break;
427e1dd0a2fSth160488 			}
428e1dd0a2fSth160488 
429e1dd0a2fSth160488 			if (optarg[0] == '-' && optarg[1] == '\0') {
430e1dd0a2fSth160488 				/* Ask for a password later */
431e1dd0a2fSth160488 				break;
432e1dd0a2fSth160488 			}
433e1dd0a2fSth160488 
434e1dd0a2fSth160488 			optlist->bindPasswd = strdup(optarg);
435e1dd0a2fSth160488 			break;
436e1dd0a2fSth160488 		case 'j':
437e1dd0a2fSth160488 			if (optlist->bindPasswd != NULL) {
438e1dd0a2fSth160488 				(void) fprintf(stderr,
439e1dd0a2fSth160488 				    gettext("The -w option is mutually "
440e1dd0a2fSth160488 				    "exclusive of -j. -w is ignored.\n"));
441e1dd0a2fSth160488 				free(optlist->bindPasswd);
442e1dd0a2fSth160488 			}
443e1dd0a2fSth160488 			optlist->bindPasswd = readPwd(optarg);
444e1dd0a2fSth160488 			if (optlist->bindPasswd == NULL) {
445e1dd0a2fSth160488 				exit(CLIENT_ERR_FAIL);
446e1dd0a2fSth160488 			}
447e1dd0a2fSth160488 			break;
448e1dd0a2fSth160488 		case 'y':
449e1dd0a2fSth160488 			if (optlist->proxyPassword != NULL) {
450e1dd0a2fSth160488 				(void) fprintf(stderr,
451e1dd0a2fSth160488 				    gettext("The -a proxyPassword option is "
452e1dd0a2fSth160488 				    "mutually exclusive of -y. "
453e1dd0a2fSth160488 				    "-a proxyPassword is ignored.\n"));
454e1dd0a2fSth160488 			}
455e1dd0a2fSth160488 			optlist->proxyPassword = readPwd(optarg);
456e1dd0a2fSth160488 			if (optlist->proxyPassword == NULL) {
457e1dd0a2fSth160488 				exit(CLIENT_ERR_FAIL);
458e1dd0a2fSth160488 			}
459e1dd0a2fSth160488 			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(
4887c478bd9Sstevel@tonic-gate 				    optlist,
4897c478bd9Sstevel@tonic-gate 				    NS_LDAP_SERVERS_P,
4907c478bd9Sstevel@tonic-gate 				    argv[optind]);	/* ipAddr */
4917c478bd9Sstevel@tonic-gate 				if (retcode != CLIENT_SUCCESS) {
4927c478bd9Sstevel@tonic-gate 					CLIENT_FPRINTF(
4937c478bd9Sstevel@tonic-gate 					    stderr,
4947c478bd9Sstevel@tonic-gate 					    gettext("Error (%d) setting "
4957c478bd9Sstevel@tonic-gate 					    "serverList param.\n"),
4967c478bd9Sstevel@tonic-gate 					    retcode);
4977c478bd9Sstevel@tonic-gate 					usage();
4987c478bd9Sstevel@tonic-gate 					exit(CLIENT_ERR_FAIL);
4997c478bd9Sstevel@tonic-gate 				}
5007c478bd9Sstevel@tonic-gate 			} else {
5017c478bd9Sstevel@tonic-gate 				CLIENT_FPUTS(
5027c478bd9Sstevel@tonic-gate 				    gettext("Error parsing "
5037c478bd9Sstevel@tonic-gate 				    "command line\n"),
5047c478bd9Sstevel@tonic-gate 				    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(
5297c478bd9Sstevel@tonic-gate 		    "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(
5437c478bd9Sstevel@tonic-gate 		    gettext("ldapclient: Missing required attrName "
5447c478bd9Sstevel@tonic-gate 		    "defaultSearchBase\n"),
5457c478bd9Sstevel@tonic-gate 		    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 +
5707c478bd9Sstevel@tonic-gate 	    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(
5887c478bd9Sstevel@tonic-gate 			    gettext("Handling list option\n"),
5897c478bd9Sstevel@tonic-gate 			    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(
5977c478bd9Sstevel@tonic-gate 			    gettext("Handling uninit option\n"),
5987c478bd9Sstevel@tonic-gate 			    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(
6067c478bd9Sstevel@tonic-gate 			    gettext("Handling init option\n"),
6077c478bd9Sstevel@tonic-gate 			    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(
6157c478bd9Sstevel@tonic-gate 			    gettext("Handling genProfile\n"),
6167c478bd9Sstevel@tonic-gate 			    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(
6247c478bd9Sstevel@tonic-gate 			    gettext("Handling manual option\n"),
6257c478bd9Sstevel@tonic-gate 			    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(
6337c478bd9Sstevel@tonic-gate 			    gettext("Handling mod option\n"),
6347c478bd9Sstevel@tonic-gate 			    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) ||
6407c478bd9Sstevel@tonic-gate 	    (retcode == CLIENT_ERR_FAIL) ||
6417c478bd9Sstevel@tonic-gate 	    (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
client_list(clientopts_t * arglist)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(
6557c478bd9Sstevel@tonic-gate 		    gettext("No args supported with \"list\" option\n"),
6567c478bd9Sstevel@tonic-gate 		    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(
6637c478bd9Sstevel@tonic-gate 		    gettext("Cannot get print configuration\n"),
6647c478bd9Sstevel@tonic-gate 		    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
client_uninit(clientopts_t * arglist)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(
6817c478bd9Sstevel@tonic-gate 		    gettext("Restoring machine to previous "
6827c478bd9Sstevel@tonic-gate 		    "configuration state\n"),
6837c478bd9Sstevel@tonic-gate 		    stderr);
6847c478bd9Sstevel@tonic-gate 	}
6857c478bd9Sstevel@tonic-gate 
6867c478bd9Sstevel@tonic-gate 	if (num_args(arglist) > 0) {
6877c478bd9Sstevel@tonic-gate 		CLIENT_FPUTS(
6887c478bd9Sstevel@tonic-gate 		    gettext("No args supported with \"uninit\" option\n"),
6897c478bd9Sstevel@tonic-gate 		    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(
7037c478bd9Sstevel@tonic-gate 		    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(
7127c478bd9Sstevel@tonic-gate 		    gettext("Cannot recover the configuration on "
7137c478bd9Sstevel@tonic-gate 		    "this machine.\n"),
7147c478bd9Sstevel@tonic-gate 		    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(
7207c478bd9Sstevel@tonic-gate 			    gettext("Config restored but problems "
7217c478bd9Sstevel@tonic-gate 			    "encountered resetting network "
7227c478bd9Sstevel@tonic-gate 			    "services.\n"),
7237c478bd9Sstevel@tonic-gate 			    stderr);
7247c478bd9Sstevel@tonic-gate 		}
7257c478bd9Sstevel@tonic-gate 	}
7267c478bd9Sstevel@tonic-gate 
7277c478bd9Sstevel@tonic-gate 	if (retcode == CLIENT_SUCCESS) {
7287c478bd9Sstevel@tonic-gate 		CLIENT_FPUTS(
7297c478bd9Sstevel@tonic-gate 		    gettext("System successfully recovered\n"),
7307c478bd9Sstevel@tonic-gate 		    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 
762e1dd0a2fSth160488 /*
763e1dd0a2fSth160488  * The following macro is used to check if an arg has already been set
764e1dd0a2fSth160488  * and issues an error message, a usage message and then returns an error.
765e1dd0a2fSth160488  * This was made into a macro to avoid the duplication of this code many
766e1dd0a2fSth160488  * times in the function below.
767e1dd0a2fSth160488  */
768e1dd0a2fSth160488 #define	LDAP_CHECK_INVALID(arg, param)	\
769e1dd0a2fSth160488 if (arg) {	\
770e1dd0a2fSth160488 	CLIENT_FPRINTF(stderr, gettext("Invalid parameter (%s) " \
771e1dd0a2fSth160488 	    "specified\n"), param);	\
772e1dd0a2fSth160488 	usage();	\
773e1dd0a2fSth160488 	return (CLIENT_ERR_FAIL);	\
774e1dd0a2fSth160488 }
775e1dd0a2fSth160488 
7767c478bd9Sstevel@tonic-gate static int
client_manual(clientopts_t * arglist)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(
7887c478bd9Sstevel@tonic-gate 		    gettext("Manual failed: System domain not set and "
7897c478bd9Sstevel@tonic-gate 		    "no domainName specified.\n"),
7907c478bd9Sstevel@tonic-gate 		    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(
7967c478bd9Sstevel@tonic-gate 		    gettext("Manual failed: Missing required "
7977c478bd9Sstevel@tonic-gate 		    "defaultSearchBase attribute.\n"),
7987c478bd9Sstevel@tonic-gate 		    stderr);
7997c478bd9Sstevel@tonic-gate 		return (CLIENT_ERR_FAIL);
8007c478bd9Sstevel@tonic-gate 	}
8017c478bd9Sstevel@tonic-gate 
8027c478bd9Sstevel@tonic-gate 	if ((arglist->defaultServerList == NULL) &&
8037c478bd9Sstevel@tonic-gate 	    (arglist->preferredServerList == NULL)) {
8047c478bd9Sstevel@tonic-gate 		CLIENT_FPUTS(
8057c478bd9Sstevel@tonic-gate 		    gettext("Manual failed: Missing required "
8067c478bd9Sstevel@tonic-gate 		    "defaultServerList or preferredServerList "
8077c478bd9Sstevel@tonic-gate 		    "attribute.\n"),
8087c478bd9Sstevel@tonic-gate 		    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(
8147c478bd9Sstevel@tonic-gate 		    gettext("Manual aborted: profileTTL is not supported "
8157c478bd9Sstevel@tonic-gate 		    "in manual mode.\n"),
8167c478bd9Sstevel@tonic-gate 		    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(
8227c478bd9Sstevel@tonic-gate 		    gettext("Manual aborted: profileName is not supported "
8237c478bd9Sstevel@tonic-gate 		    "in manual mode.\n"),
8247c478bd9Sstevel@tonic-gate 		    stderr);
8257c478bd9Sstevel@tonic-gate 		return (CLIENT_ERR_FAIL);
8267c478bd9Sstevel@tonic-gate 	}
8277c478bd9Sstevel@tonic-gate 
828e1dd0a2fSth160488 	LDAP_CHECK_INVALID(arglist->bindDN, "bind DN");
829e1dd0a2fSth160488 	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;
8607c478bd9Sstevel@tonic-gate 	    counter < arglist->serviceAuthenticationMethod->count;
8617c478bd9Sstevel@tonic-gate 	    counter++) {
8627c478bd9Sstevel@tonic-gate 
8637c478bd9Sstevel@tonic-gate 		LDAP_SET_PARAM(
8647c478bd9Sstevel@tonic-gate 		    arglist->serviceAuthenticationMethod->optlist[counter],
8657c478bd9Sstevel@tonic-gate 		    NS_LDAP_SERVICE_AUTH_METHOD_P);
8667c478bd9Sstevel@tonic-gate 	}
8677c478bd9Sstevel@tonic-gate 	for (counter = 0;
8687c478bd9Sstevel@tonic-gate 	    counter < arglist->serviceCredentialLevel->count;
8697c478bd9Sstevel@tonic-gate 	    counter++) {
8707c478bd9Sstevel@tonic-gate 
8717c478bd9Sstevel@tonic-gate 		LDAP_SET_PARAM(
8727c478bd9Sstevel@tonic-gate 		    arglist->serviceCredentialLevel->optlist[counter],
8737c478bd9Sstevel@tonic-gate 		    NS_LDAP_SERVICE_CRED_LEVEL_P);
8747c478bd9Sstevel@tonic-gate 	}
8757c478bd9Sstevel@tonic-gate 	for (counter = 0;
8767c478bd9Sstevel@tonic-gate 	    counter < arglist->objectclassMap->count;
8777c478bd9Sstevel@tonic-gate 	    counter++) {
8787c478bd9Sstevel@tonic-gate 
8797c478bd9Sstevel@tonic-gate 		LDAP_SET_PARAM(arglist->objectclassMap->optlist[counter],
8807c478bd9Sstevel@tonic-gate 		    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],
8847c478bd9Sstevel@tonic-gate 		    NS_LDAP_ATTRIBUTEMAP_P);
8857c478bd9Sstevel@tonic-gate 	}
8867c478bd9Sstevel@tonic-gate 	for (counter = 0;
8877c478bd9Sstevel@tonic-gate 	    counter < arglist->serviceSearchDescriptor->count;
8887c478bd9Sstevel@tonic-gate 	    counter++) {
8897c478bd9Sstevel@tonic-gate 
8907c478bd9Sstevel@tonic-gate 		LDAP_SET_PARAM(
8917c478bd9Sstevel@tonic-gate 		    arglist->serviceSearchDescriptor->optlist[counter],
8927c478bd9Sstevel@tonic-gate 		    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(
9007c478bd9Sstevel@tonic-gate 		    gettext("Error in setting up credentials\n"),
9017c478bd9Sstevel@tonic-gate 		    stderr);
9027c478bd9Sstevel@tonic-gate 		return (retcode);
9037c478bd9Sstevel@tonic-gate 	}
9047c478bd9Sstevel@tonic-gate 
9057c478bd9Sstevel@tonic-gate 	if (mode_verbose)
9067c478bd9Sstevel@tonic-gate 		CLIENT_FPUTS(
9077c478bd9Sstevel@tonic-gate 		    gettext("About to modify this machines "
9087c478bd9Sstevel@tonic-gate 		    "configuration by writing the files\n"),
9097c478bd9Sstevel@tonic-gate 		    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(
9157c478bd9Sstevel@tonic-gate 		    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(
9237c478bd9Sstevel@tonic-gate 		    gettext("System not in state to enable ldap client.\n"),
9247c478bd9Sstevel@tonic-gate 		    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 "
9297c478bd9Sstevel@tonic-gate 			    "starting services during reset\n"),
9307c478bd9Sstevel@tonic-gate 			    reset_ret);
9317c478bd9Sstevel@tonic-gate 		}
9327c478bd9Sstevel@tonic-gate 		return (retcode);
9337c478bd9Sstevel@tonic-gate 	} else if (retcode != CLIENT_SUCCESS) {
9347c478bd9Sstevel@tonic-gate 		CLIENT_FPUTS(
9357c478bd9Sstevel@tonic-gate 		    gettext("Save of system configuration failed!  "
9367c478bd9Sstevel@tonic-gate 		    "Attempting recovery.\n"),
9377c478bd9Sstevel@tonic-gate 		    stderr);
9387c478bd9Sstevel@tonic-gate 		retcode = recover(STATE_NOSAVE);
9397c478bd9Sstevel@tonic-gate 		if (retcode != CLIENT_SUCCESS) {
9407c478bd9Sstevel@tonic-gate 			CLIENT_FPUTS(
9417c478bd9Sstevel@tonic-gate 			    gettext("Recovery of systems configuration "
9427c478bd9Sstevel@tonic-gate 			    "failed.  Manual intervention of "
9437c478bd9Sstevel@tonic-gate 			    "config files is required.\n"),
9447c478bd9Sstevel@tonic-gate 			    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 "
9517c478bd9Sstevel@tonic-gate 			    "starting services during reset\n"),
9527c478bd9Sstevel@tonic-gate 			    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,
9627c478bd9Sstevel@tonic-gate 		    gettext("%s manual: errorp is not NULL; %s\n"),
9637c478bd9Sstevel@tonic-gate 		    cmd, errorp->message);
9647c478bd9Sstevel@tonic-gate 		retcode = recover(STATE_NOSAVE);
9657c478bd9Sstevel@tonic-gate 		if (retcode != CLIENT_SUCCESS) {
9667c478bd9Sstevel@tonic-gate 			CLIENT_FPUTS(
9677c478bd9Sstevel@tonic-gate 			    gettext("Recovery of systems configuration "
9687c478bd9Sstevel@tonic-gate 			    "failed.  Manual intervention of "
9697c478bd9Sstevel@tonic-gate 			    "config files is required.\n"),
9707c478bd9Sstevel@tonic-gate 			    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 "
9767c478bd9Sstevel@tonic-gate 			    "starting services during reset\n"),
9777c478bd9Sstevel@tonic-gate 			    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,
9877c478bd9Sstevel@tonic-gate 		    gettext("%s init: errorp is not NULL; %s\n"),
9887c478bd9Sstevel@tonic-gate 		    cmd, errorp->message);
9897c478bd9Sstevel@tonic-gate 		retcode = recover(STATE_NOSAVE);
9907c478bd9Sstevel@tonic-gate 		if (retcode != CLIENT_SUCCESS) {
9917c478bd9Sstevel@tonic-gate 			CLIENT_FPUTS(
9927c478bd9Sstevel@tonic-gate 			    gettext("Recovery of systems configuration "
9937c478bd9Sstevel@tonic-gate 			    "failed.  Manual intervention of "
9947c478bd9Sstevel@tonic-gate 			    "config files is required.\n"),
9957c478bd9Sstevel@tonic-gate 			    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 "
10017c478bd9Sstevel@tonic-gate 			    "starting services during reset\n"),
10027c478bd9Sstevel@tonic-gate 			    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,
10117c478bd9Sstevel@tonic-gate 		    gettext("Error %d copying (%s) -> (%s)\n"),
10127c478bd9Sstevel@tonic-gate 		    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(
10167c478bd9Sstevel@tonic-gate 			    gettext("Recovery of systems configuration "
10177c478bd9Sstevel@tonic-gate 			    "failed.  Manual intervention of "
10187c478bd9Sstevel@tonic-gate 			    "config files is required.\n"),
10197c478bd9Sstevel@tonic-gate 			    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 "
10247c478bd9Sstevel@tonic-gate 			    "starting services during reset\n"),
10257c478bd9Sstevel@tonic-gate 			    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,
10317c478bd9Sstevel@tonic-gate 	    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(
10367c478bd9Sstevel@tonic-gate 			    gettext("Recovery of systems configuration "
10377c478bd9Sstevel@tonic-gate 			    "failed.  Manual intervention of "
10387c478bd9Sstevel@tonic-gate 			    "config files is required.\n"),
10397c478bd9Sstevel@tonic-gate 			    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 "
10457c478bd9Sstevel@tonic-gate 			    "starting services during reset\n"),
10467c478bd9Sstevel@tonic-gate 			    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"),
10587c478bd9Sstevel@tonic-gate 		    stderr);
10597c478bd9Sstevel@tonic-gate 	} else {
10607c478bd9Sstevel@tonic-gate 		CLIENT_FPUTS(gettext("Error resetting system.\n"
10617c478bd9Sstevel@tonic-gate 		    "Recovering old system settings.\n"), stderr),
10627c478bd9Sstevel@tonic-gate 
10637c478bd9Sstevel@tonic-gate 		    /* stop any started services for recover */
10647c478bd9Sstevel@tonic-gate 		    /* don't stomp on history of saved services state */
10657c478bd9Sstevel@tonic-gate 		    reset_ret = stop_services(STATE_NOSAVE);
10667c478bd9Sstevel@tonic-gate 		if (reset_ret != CLIENT_SUCCESS) {
10677c478bd9Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr, gettext("Error (%d) while "
10687c478bd9Sstevel@tonic-gate 			    "stopping services during reset\n"),
10697c478bd9Sstevel@tonic-gate 			    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 "
10757c478bd9Sstevel@tonic-gate 			    "recovering service files during "
10767c478bd9Sstevel@tonic-gate 			    "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 "
10827c478bd9Sstevel@tonic-gate 			    "starting services during reset\n"),
10837c478bd9Sstevel@tonic-gate 			    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
client_mod(clientopts_t * arglist)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"),
11027c478bd9Sstevel@tonic-gate 		    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(
11117c478bd9Sstevel@tonic-gate 		    gettext("Mod aborted: profileTTL modification is "
11127c478bd9Sstevel@tonic-gate 		    "not allowed in mod mode.\n"),
11137c478bd9Sstevel@tonic-gate 		    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(
11197c478bd9Sstevel@tonic-gate 		    gettext("Mod aborted: profileName modification is "
11207c478bd9Sstevel@tonic-gate 		    "not allowed.  If you want to use profiles "
11217c478bd9Sstevel@tonic-gate 		    "generate one with genProfile and load it "
11227c478bd9Sstevel@tonic-gate 		    "on the server with ldapadd.\n"),
11237c478bd9Sstevel@tonic-gate 		    stderr);
11247c478bd9Sstevel@tonic-gate 		return (CLIENT_ERR_FAIL);
11257c478bd9Sstevel@tonic-gate 	}
11267c478bd9Sstevel@tonic-gate 
1127e1dd0a2fSth160488 	LDAP_CHECK_INVALID(arglist->bindDN, "bind DN");
1128e1dd0a2fSth160488 	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;
11517c478bd9Sstevel@tonic-gate 	    counter < arglist->serviceAuthenticationMethod->count;
11527c478bd9Sstevel@tonic-gate 	    counter++) {
11537c478bd9Sstevel@tonic-gate 
11547c478bd9Sstevel@tonic-gate 		LDAP_SET_PARAM(
11557c478bd9Sstevel@tonic-gate 		    arglist->serviceAuthenticationMethod->optlist[counter],
11567c478bd9Sstevel@tonic-gate 		    NS_LDAP_SERVICE_AUTH_METHOD_P);
11577c478bd9Sstevel@tonic-gate 	}
11587c478bd9Sstevel@tonic-gate 	for (counter = 0;
11597c478bd9Sstevel@tonic-gate 	    counter < arglist->serviceCredentialLevel->count;
11607c478bd9Sstevel@tonic-gate 	    counter++) {
11617c478bd9Sstevel@tonic-gate 
11627c478bd9Sstevel@tonic-gate 		LDAP_SET_PARAM(
11637c478bd9Sstevel@tonic-gate 		    arglist->serviceCredentialLevel->optlist[counter],
11647c478bd9Sstevel@tonic-gate 		    NS_LDAP_SERVICE_CRED_LEVEL_P);
11657c478bd9Sstevel@tonic-gate 	}
11667c478bd9Sstevel@tonic-gate 	for (counter = 0;
11677c478bd9Sstevel@tonic-gate 	    counter < arglist->objectclassMap->count;
11687c478bd9Sstevel@tonic-gate 	    counter++) {
11697c478bd9Sstevel@tonic-gate 
11707c478bd9Sstevel@tonic-gate 		LDAP_SET_PARAM(
11717c478bd9Sstevel@tonic-gate 		    arglist->objectclassMap->optlist[counter],
11727c478bd9Sstevel@tonic-gate 		    NS_LDAP_OBJECTCLASSMAP_P);
11737c478bd9Sstevel@tonic-gate 	}
11747c478bd9Sstevel@tonic-gate 	for (counter = 0;
11757c478bd9Sstevel@tonic-gate 	    counter < arglist->attributeMap->count;
11767c478bd9Sstevel@tonic-gate 	    counter++) {
11777c478bd9Sstevel@tonic-gate 
11787c478bd9Sstevel@tonic-gate 		LDAP_SET_PARAM(
11797c478bd9Sstevel@tonic-gate 		    arglist->attributeMap->optlist[counter],
11807c478bd9Sstevel@tonic-gate 		    NS_LDAP_ATTRIBUTEMAP_P);
11817c478bd9Sstevel@tonic-gate 	}
11827c478bd9Sstevel@tonic-gate 	for (counter = 0;
11837c478bd9Sstevel@tonic-gate 	    counter < arglist->serviceSearchDescriptor->count;
11847c478bd9Sstevel@tonic-gate 	    counter++) {
11857c478bd9Sstevel@tonic-gate 
11867c478bd9Sstevel@tonic-gate 		LDAP_SET_PARAM(
11877c478bd9Sstevel@tonic-gate 		    arglist->serviceSearchDescriptor->optlist[counter],
11887c478bd9Sstevel@tonic-gate 		    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(
11967c478bd9Sstevel@tonic-gate 		    gettext("Error in setting up credentials\n"),
11977c478bd9Sstevel@tonic-gate 		    stderr);
11987c478bd9Sstevel@tonic-gate 		return (retcode);
11997c478bd9Sstevel@tonic-gate 	}
12007c478bd9Sstevel@tonic-gate 
12017c478bd9Sstevel@tonic-gate 	if (mode_verbose)
12027c478bd9Sstevel@tonic-gate 		CLIENT_FPUTS(
12037c478bd9Sstevel@tonic-gate 		    gettext("About to modify this machines configuration "
12047c478bd9Sstevel@tonic-gate 		    "by writing the files\n"),
12057c478bd9Sstevel@tonic-gate 		    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(
12117c478bd9Sstevel@tonic-gate 		    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(
12197c478bd9Sstevel@tonic-gate 		    gettext("Unable to backup the ldap client files!\n"),
12207c478bd9Sstevel@tonic-gate 		    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,
12307c478bd9Sstevel@tonic-gate 		    gettext("%s mod: errorp is not NULL; %s\n"),
12317c478bd9Sstevel@tonic-gate 		    cmd, errorp->message);
12327c478bd9Sstevel@tonic-gate 		retcode = mod_recover();
12337c478bd9Sstevel@tonic-gate 		if (retcode != CLIENT_SUCCESS) {
12347c478bd9Sstevel@tonic-gate 			CLIENT_FPUTS(
12357c478bd9Sstevel@tonic-gate 			    gettext("Recovery of systems configuration "
12367c478bd9Sstevel@tonic-gate 			    "failed.  Manual intervention of "
12377c478bd9Sstevel@tonic-gate 			    "config files is required.\n"),
12387c478bd9Sstevel@tonic-gate 			    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 "
12447c478bd9Sstevel@tonic-gate 			    "starting services during reset\n"),
12457c478bd9Sstevel@tonic-gate 			    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,
12547c478bd9Sstevel@tonic-gate 		    gettext("%s mod: errorp is not NULL; %s\n"),
12557c478bd9Sstevel@tonic-gate 		    cmd, errorp->message);
12567c478bd9Sstevel@tonic-gate 		retcode = mod_recover();
12577c478bd9Sstevel@tonic-gate 		if (retcode != CLIENT_SUCCESS) {
12587c478bd9Sstevel@tonic-gate 			CLIENT_FPUTS(
12597c478bd9Sstevel@tonic-gate 			    gettext("Recovery of systems configuration "
12607c478bd9Sstevel@tonic-gate 			    "failed.  Manual intervention of "
12617c478bd9Sstevel@tonic-gate 			    "config files is required.\n"),
12627c478bd9Sstevel@tonic-gate 			    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 "
12687c478bd9Sstevel@tonic-gate 			    "starting services during reset\n"),
12697c478bd9Sstevel@tonic-gate 			    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,
12757c478bd9Sstevel@tonic-gate 	    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(
12807c478bd9Sstevel@tonic-gate 			    gettext("Recovery of systems configuration "
12817c478bd9Sstevel@tonic-gate 			    "failed!  Machine needs to be "
12827c478bd9Sstevel@tonic-gate 			    "fixed!\n"),
12837c478bd9Sstevel@tonic-gate 			    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 "
12887c478bd9Sstevel@tonic-gate 			    "starting services during reset\n"),
12897c478bd9Sstevel@tonic-gate 			    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"),
13017c478bd9Sstevel@tonic-gate 		    stderr);
13027c478bd9Sstevel@tonic-gate 	} else {
13037c478bd9Sstevel@tonic-gate 		CLIENT_FPUTS(gettext("Error resetting system.\n"
13047c478bd9Sstevel@tonic-gate 		    "Recovering old system settings.\n"), stderr),
13057c478bd9Sstevel@tonic-gate 
13067c478bd9Sstevel@tonic-gate 		    /* stop any started services for recover */
13077c478bd9Sstevel@tonic-gate 		    /* don't stomp on history of saved services state */
13087c478bd9Sstevel@tonic-gate 		    reset_ret = stop_services(STATE_NOSAVE);
13097c478bd9Sstevel@tonic-gate 		if (reset_ret != CLIENT_SUCCESS) {
13107c478bd9Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr, gettext("Error (%d) while "
13117c478bd9Sstevel@tonic-gate 			    "stopping services during reset\n"),
13127c478bd9Sstevel@tonic-gate 			    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 "
13187c478bd9Sstevel@tonic-gate 			    "recovering service files during "
13197c478bd9Sstevel@tonic-gate 			    "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 "
13257c478bd9Sstevel@tonic-gate 			    "starting services during reset\n"),
13267c478bd9Sstevel@tonic-gate 			    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
client_genProfile(clientopts_t * arglist)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");
1356e1dd0a2fSth160488 	LDAP_CHECK_INVALID(arglist->bindDN, "bind DN");
1357e1dd0a2fSth160488 	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(
13637c478bd9Sstevel@tonic-gate 			    gettext("No profile specified. "
13647c478bd9Sstevel@tonic-gate 			    "Using \"default\"\n"),
13657c478bd9Sstevel@tonic-gate 			    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;
13897c478bd9Sstevel@tonic-gate 	    counter < arglist->serviceAuthenticationMethod->count;
13907c478bd9Sstevel@tonic-gate 	    counter++) {
13917c478bd9Sstevel@tonic-gate 
13927c478bd9Sstevel@tonic-gate 		LDAP_SET_PARAM(
13937c478bd9Sstevel@tonic-gate 		    arglist->serviceAuthenticationMethod->optlist[counter],
13947c478bd9Sstevel@tonic-gate 		    NS_LDAP_SERVICE_AUTH_METHOD_P);
13957c478bd9Sstevel@tonic-gate 	}
13967c478bd9Sstevel@tonic-gate 	for (counter = 0;
13977c478bd9Sstevel@tonic-gate 	    counter < arglist->serviceCredentialLevel->count;
13987c478bd9Sstevel@tonic-gate 	    counter++) {
13997c478bd9Sstevel@tonic-gate 
14007c478bd9Sstevel@tonic-gate 		LDAP_SET_PARAM(
14017c478bd9Sstevel@tonic-gate 		    arglist->serviceCredentialLevel->optlist[counter],
14027c478bd9Sstevel@tonic-gate 		    NS_LDAP_SERVICE_CRED_LEVEL_P);
14037c478bd9Sstevel@tonic-gate 	}
14047c478bd9Sstevel@tonic-gate 	for (counter = 0;
14057c478bd9Sstevel@tonic-gate 	    counter < arglist->objectclassMap->count;
14067c478bd9Sstevel@tonic-gate 	    counter++) {
14077c478bd9Sstevel@tonic-gate 
14087c478bd9Sstevel@tonic-gate 		LDAP_SET_PARAM(
14097c478bd9Sstevel@tonic-gate 		    arglist->objectclassMap->optlist[counter],
14107c478bd9Sstevel@tonic-gate 		    NS_LDAP_OBJECTCLASSMAP_P);
14117c478bd9Sstevel@tonic-gate 	}
14127c478bd9Sstevel@tonic-gate 	for (counter = 0;
14137c478bd9Sstevel@tonic-gate 	    counter < arglist->attributeMap->count;
14147c478bd9Sstevel@tonic-gate 	    counter++) {
14157c478bd9Sstevel@tonic-gate 
14167c478bd9Sstevel@tonic-gate 		LDAP_SET_PARAM(
14177c478bd9Sstevel@tonic-gate 		    arglist->attributeMap->optlist[counter],
14187c478bd9Sstevel@tonic-gate 		    NS_LDAP_ATTRIBUTEMAP_P);
14197c478bd9Sstevel@tonic-gate 	}
14207c478bd9Sstevel@tonic-gate 	for (counter = 0;
14217c478bd9Sstevel@tonic-gate 	    counter < arglist->serviceSearchDescriptor->count;
14227c478bd9Sstevel@tonic-gate 	    counter++) {
14237c478bd9Sstevel@tonic-gate 
14247c478bd9Sstevel@tonic-gate 		LDAP_SET_PARAM(
14257c478bd9Sstevel@tonic-gate 		    arglist->serviceSearchDescriptor->optlist[counter],
14267c478bd9Sstevel@tonic-gate 		    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 
1440e1dd0a2fSth160488 /* INET6_ADDRSTRLEN + ":" + <5-digit port> + some round-up */
1441e1dd0a2fSth160488 #define	MAX_HOSTADDR_LEN (INET6_ADDRSTRLEN + 6 + 12)
1442e1dd0a2fSth160488 
14437c478bd9Sstevel@tonic-gate static int
client_init(clientopts_t * arglist)14447c478bd9Sstevel@tonic-gate client_init(clientopts_t *arglist)
14457c478bd9Sstevel@tonic-gate {
14467c478bd9Sstevel@tonic-gate 	int			profile_fp;
14477c478bd9Sstevel@tonic-gate 	int			retcode = CLIENT_SUCCESS;
14487c478bd9Sstevel@tonic-gate 	ns_ldap_error_t		*errorp;
14497c478bd9Sstevel@tonic-gate 	int			reset_ret;
14507c478bd9Sstevel@tonic-gate 	int			ret_copy;
1451e1dd0a2fSth160488 	ns_standalone_conf_t	cfg = standaloneDefaults;
1452e1dd0a2fSth160488 	ns_auth_t		auth = {NS_LDAP_AUTH_NONE,
1453e1dd0a2fSth160488 					NS_LDAP_TLS_NONE,
1454e1dd0a2fSth160488 					NS_LDAP_SASL_NONE,
1455e1dd0a2fSth160488 					NS_LDAP_SASLOPT_NONE};
1456e1dd0a2fSth160488 	char			peer[MAX_HOSTADDR_LEN];
1457e1dd0a2fSth160488 	ns_auth_t		**authMethod;
1458e1dd0a2fSth160488 	int			**credLevel, i;
1459e1dd0a2fSth160488 	char			*cred;
14607c478bd9Sstevel@tonic-gate 
14617c478bd9Sstevel@tonic-gate 	if (mode_verbose)
14627c478bd9Sstevel@tonic-gate 		CLIENT_FPUTS(
14637c478bd9Sstevel@tonic-gate 		    gettext("About to configure machine by downloading "
14647c478bd9Sstevel@tonic-gate 		    "a profile\n"),
14657c478bd9Sstevel@tonic-gate 		    stderr);
14667c478bd9Sstevel@tonic-gate 
14677c478bd9Sstevel@tonic-gate 	if (dname == NULL) {
14687c478bd9Sstevel@tonic-gate 		CLIENT_FPUTS(
14697c478bd9Sstevel@tonic-gate 		    gettext("Init failed: System domain not set and "
14707c478bd9Sstevel@tonic-gate 		    "no domainName specified.\n"),
14717c478bd9Sstevel@tonic-gate 		    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,
14827c478bd9Sstevel@tonic-gate 	    "defaultSearchBase");
14837c478bd9Sstevel@tonic-gate 	LDAP_CHECK_INVALID(arglist->profileTTL,
14847c478bd9Sstevel@tonic-gate 	    "profileTTL");
14857c478bd9Sstevel@tonic-gate 	LDAP_CHECK_INVALID(arglist->searchTimeLimit,
14867c478bd9Sstevel@tonic-gate 	    "searchTimeLimit");
14877c478bd9Sstevel@tonic-gate 	LDAP_CHECK_INVALID(arglist->preferredServerList,
14887c478bd9Sstevel@tonic-gate 	    "preferredServerList");
14897c478bd9Sstevel@tonic-gate 	LDAP_CHECK_INVALID(arglist->followReferrals,
14907c478bd9Sstevel@tonic-gate 	    "followReferrals");
14917c478bd9Sstevel@tonic-gate 	LDAP_CHECK_INVALID(arglist->defaultSearchScope,
14927c478bd9Sstevel@tonic-gate 	    "defaultSearchScope");
14937c478bd9Sstevel@tonic-gate 	LDAP_CHECK_INVALID(arglist->bindTimeLimit,
14947c478bd9Sstevel@tonic-gate 	    "bindTimeLimit");
14957c478bd9Sstevel@tonic-gate 
14967c478bd9Sstevel@tonic-gate 	LDAP_CHECK_INVALID(arglist->objectclassMap->count,
14977c478bd9Sstevel@tonic-gate 	    "objectclassMap");
14987c478bd9Sstevel@tonic-gate 	LDAP_CHECK_INVALID(arglist->attributeMap->count,
14997c478bd9Sstevel@tonic-gate 	    "attributeMap");
15007c478bd9Sstevel@tonic-gate 	LDAP_CHECK_INVALID(arglist->serviceAuthenticationMethod->count,
15017c478bd9Sstevel@tonic-gate 	    "serviceAuthenticationMethod");
15027c478bd9Sstevel@tonic-gate 	LDAP_CHECK_INVALID(arglist->serviceCredentialLevel->count,
15037c478bd9Sstevel@tonic-gate 	    "serviceCredentialLevel");
15047c478bd9Sstevel@tonic-gate 	LDAP_CHECK_INVALID(arglist->serviceSearchDescriptor->count,
15057c478bd9Sstevel@tonic-gate 	    "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(
15117c478bd9Sstevel@tonic-gate 			    gettext("No profile specified. "
15127c478bd9Sstevel@tonic-gate 			    "Using \"default\"\n"),
15137c478bd9Sstevel@tonic-gate 			    stderr);
15147c478bd9Sstevel@tonic-gate 		arglist->profileName = "default";
15157c478bd9Sstevel@tonic-gate 	}
15167c478bd9Sstevel@tonic-gate 
1517e1dd0a2fSth160488 	(void) strncpy(peer, arglist->defaultServerList, MAX_HOSTADDR_LEN - 1);
1518e1dd0a2fSth160488 	if (separatePort(peer, &cfg.SA_SERVER, &cfg.SA_PORT) > 0) {
1519e1dd0a2fSth160488 		return (CLIENT_ERR_FAIL);
1520e1dd0a2fSth160488 	}
1521e1dd0a2fSth160488 
1522e1dd0a2fSth160488 	if (arglist->bindDN != NULL) {
1523e1dd0a2fSth160488 		cfg.SA_CRED = "proxy";
1524e1dd0a2fSth160488 		/*
1525e1dd0a2fSth160488 		 * 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
1527e1dd0a2fSth160488 		 * have to specify appropriate -a though.
1528e1dd0a2fSth160488 		 */
1529e1dd0a2fSth160488 		auth.type = NS_LDAP_AUTH_SIMPLE;
1530e1dd0a2fSth160488 		if (arglist->bindPasswd == NULL) {
1531e1dd0a2fSth160488 			arglist->bindPasswd =
1532e1dd0a2fSth160488 			    getpassphrase("Bind Password:");
1533e1dd0a2fSth160488 			if (arglist->bindPasswd == NULL) {
1534e1dd0a2fSth160488 				CLIENT_FPUTS(gettext("Get password failed\n"),
1535e1dd0a2fSth160488 				    stderr);
15367c478bd9Sstevel@tonic-gate 
15377c478bd9Sstevel@tonic-gate 				if (gStartLdap == START_RESET)
15387c478bd9Sstevel@tonic-gate 					(void) start_service(LDAP_FMRI, B_TRUE);
15397c478bd9Sstevel@tonic-gate 
1540e1dd0a2fSth160488 				return (CLIENT_ERR_CREDENTIAL);
15417c478bd9Sstevel@tonic-gate 			}
15427c478bd9Sstevel@tonic-gate 		}
1543e1dd0a2fSth160488 	}
1544e1dd0a2fSth160488 	cfg.SA_BIND_DN = arglist->bindDN;
1545e1dd0a2fSth160488 	cfg.SA_BIND_PWD = arglist->bindPasswd;
15467c478bd9Sstevel@tonic-gate 
1547e1dd0a2fSth160488 	if (arglist->authenticationMethod != NULL) {
1548e1dd0a2fSth160488 		if (__ns_ldap_initAuth(arglist->authenticationMethod,
1549e1dd0a2fSth160488 		    &auth, &errorp) != NS_LDAP_SUCCESS) {
15507c478bd9Sstevel@tonic-gate 			if (errorp != NULL) {
1551e1dd0a2fSth160488 				CLIENT_FPRINTF(stderr, "%s", errorp->message);
15527c478bd9Sstevel@tonic-gate 				(void) __ns_ldap_freeError(&errorp);
1553e1dd0a2fSth160488 			}
1554e1dd0a2fSth160488 
1555e1dd0a2fSth160488 			if (gStartLdap == START_RESET)
1556e1dd0a2fSth160488 				(void) start_service(LDAP_FMRI, B_TRUE);
1557e1dd0a2fSth160488 
1558e1dd0a2fSth160488 			return (CLIENT_ERR_FAIL);
1559e1dd0a2fSth160488 		}
1560e1dd0a2fSth160488 		cfg.SA_AUTH = &auth;
1561e1dd0a2fSth160488 	}
1562e1dd0a2fSth160488 	cfg.SA_CRED = arglist->credentialLevel;
1563e1dd0a2fSth160488 
1564e1dd0a2fSth160488 	cfg.SA_DOMAIN = arglist->domainName;
1565e1dd0a2fSth160488 	cfg.SA_PROFILE_NAME = arglist->profileName;
1566e1dd0a2fSth160488 	cfg.SA_CERT_PATH = arglist->certificatePath;
1567e1dd0a2fSth160488 
1568e1dd0a2fSth160488 	cfg.type = NS_LDAP_SERVER;
1569e1dd0a2fSth160488 
1570e1dd0a2fSth160488 	if (__ns_ldap_initStandalone(&cfg, &errorp) != NS_LDAP_SUCCESS) {
1571e1dd0a2fSth160488 		if (errorp != NULL) {
1572e1dd0a2fSth160488 			CLIENT_FPRINTF(stderr, "%s", errorp->message);
1573e1dd0a2fSth160488 			(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 
1582e1dd0a2fSth160488 	if (arglist->proxyDN != NULL && arglist->proxyPassword == NULL) {
1583e1dd0a2fSth160488 		arglist->proxyPassword = getpassphrase("Proxy Bind Password:");
1584e1dd0a2fSth160488 		if (arglist->proxyPassword == NULL) {
1585e1dd0a2fSth160488 			CLIENT_FPUTS(gettext("Get password failed\n"), stderr);
1586e1dd0a2fSth160488 
1587e1dd0a2fSth160488 			if (gStartLdap == START_RESET)
1588e1dd0a2fSth160488 				(void) start_service(LDAP_FMRI, B_TRUE);
1589e1dd0a2fSth160488 
1590e1dd0a2fSth160488 			return (CLIENT_ERR_CREDENTIAL);
15917c478bd9Sstevel@tonic-gate 		}
15927c478bd9Sstevel@tonic-gate 	}
1593e1dd0a2fSth160488 	if (arglist->proxyDN != NULL && arglist->proxyPassword != NULL) {
1594e1dd0a2fSth160488 		if (__ns_ldap_setParam(NS_LDAP_BINDDN_P,
1595e1dd0a2fSth160488 		    arglist->proxyDN, &errorp) != NS_LDAP_SUCCESS) {
1596e1dd0a2fSth160488 			if (errorp != NULL) {
1597e1dd0a2fSth160488 				CLIENT_FPRINTF(stderr, "%s", errorp->message);
1598e1dd0a2fSth160488 				(void) __ns_ldap_freeError(&errorp);
1599e1dd0a2fSth160488 			}
1600e1dd0a2fSth160488 			return (CLIENT_ERR_CREDENTIAL);
1601e1dd0a2fSth160488 		}
1602e1dd0a2fSth160488 		if (__ns_ldap_setParam(NS_LDAP_BINDPASSWD_P,
1603e1dd0a2fSth160488 		    arglist->proxyPassword, &errorp) != NS_LDAP_SUCCESS) {
1604e1dd0a2fSth160488 			if (errorp != NULL) {
1605e1dd0a2fSth160488 				CLIENT_FPRINTF(stderr, "%s", errorp->message);
1606e1dd0a2fSth160488 				(void) __ns_ldap_freeError(&errorp);
1607e1dd0a2fSth160488 			}
1608e1dd0a2fSth160488 			return (CLIENT_ERR_CREDENTIAL);
1609e1dd0a2fSth160488 		}
1610e1dd0a2fSth160488 	}
1611e1dd0a2fSth160488 
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 
1649e1dd0a2fSth160488 	if (arglist->authenticationMethod != NULL) {
1650e1dd0a2fSth160488 		if (__ns_ldap_getParam(NS_LDAP_AUTH_P,
1651e1dd0a2fSth160488 		    (void ***)&authMethod, &errorp) != NS_LDAP_SUCCESS) {
1652e1dd0a2fSth160488 			if (errorp != NULL) {
1653e1dd0a2fSth160488 				CLIENT_FPRINTF(stderr, "%s", errorp->message);
1654e1dd0a2fSth160488 				(void) __ns_ldap_freeError(&errorp);
1655e1dd0a2fSth160488 			}
1656e1dd0a2fSth160488 			return (CLIENT_ERR_CREDENTIAL);
1657e1dd0a2fSth160488 		}
1658e1dd0a2fSth160488 
1659e1dd0a2fSth160488 		if (authMethod != NULL) {
1660e1dd0a2fSth160488 			for (i = 0; authMethod[i] != NULL; ++i) {
1661e1dd0a2fSth160488 				if (authMethod[i]->type == auth.type) {
1662e1dd0a2fSth160488 					break;
1663e1dd0a2fSth160488 				}
1664e1dd0a2fSth160488 			}
1665e1dd0a2fSth160488 
1666e1dd0a2fSth160488 			if (authMethod[i] == NULL) {
1667e1dd0a2fSth160488 				CLIENT_FPRINTF(stderr, gettext(
1668e1dd0a2fSth160488 				    "Warning: init authentication method "
1669e1dd0a2fSth160488 				    "not found in DUAConfigProfile.\n"));
1670e1dd0a2fSth160488 			} else {
1671e1dd0a2fSth160488 				if (i != 0) {
1672e1dd0a2fSth160488 					CLIENT_FPRINTF(stderr,
1673e1dd0a2fSth160488 					    gettext(
1674e1dd0a2fSth160488 					    "Warning: init authentication"
1675e1dd0a2fSth160488 					    "method using secondary "
1676e1dd0a2fSth160488 					    "authentication method from "
1677e1dd0a2fSth160488 					    "DUAConfigProfile.\n"));
1678e1dd0a2fSth160488 				}
1679e1dd0a2fSth160488 			}
1680e1dd0a2fSth160488 			(void) __ns_ldap_freeParam((void ***) &authMethod);
1681e1dd0a2fSth160488 		}
1682e1dd0a2fSth160488 	}
1683e1dd0a2fSth160488 
1684e1dd0a2fSth160488 	if (arglist->credentialLevel != NULL) {
1685e1dd0a2fSth160488 		if (__ns_ldap_getParam(NS_LDAP_CREDENTIAL_LEVEL_P,
1686e1dd0a2fSth160488 		    (void ***)&credLevel, &errorp) != NS_LDAP_SUCCESS) {
1687e1dd0a2fSth160488 			if (errorp != NULL) {
1688e1dd0a2fSth160488 				CLIENT_FPRINTF(stderr, "%s", errorp->message);
1689e1dd0a2fSth160488 				(void) __ns_ldap_freeError(&errorp);
1690e1dd0a2fSth160488 			}
1691e1dd0a2fSth160488 			return (CLIENT_ERR_CREDENTIAL);
1692e1dd0a2fSth160488 		}
1693e1dd0a2fSth160488 		if (credLevel != NULL) {
1694e1dd0a2fSth160488 			for (i = 0; credLevel[i] != NULL; ++i) {
1695e1dd0a2fSth160488 				switch (*credLevel[i]) {
1696e1dd0a2fSth160488 				case NS_LDAP_CRED_ANON :
1697e1dd0a2fSth160488 					cred = "none";
1698e1dd0a2fSth160488 					break;
1699e1dd0a2fSth160488 				case NS_LDAP_CRED_PROXY :
1700e1dd0a2fSth160488 					cred = "proxy";
1701e1dd0a2fSth160488 					break;
1702e1dd0a2fSth160488 				case NS_LDAP_CRED_SELF :
1703e1dd0a2fSth160488 					cred = "self";
1704e1dd0a2fSth160488 					break;
1705e1dd0a2fSth160488 				default:
1706e1dd0a2fSth160488 					continue;
1707e1dd0a2fSth160488 				}
1708e1dd0a2fSth160488 				if (strcmp(cred,
1709e1dd0a2fSth160488 				    arglist->credentialLevel) == 0) {
1710e1dd0a2fSth160488 					break;
1711e1dd0a2fSth160488 				}
1712e1dd0a2fSth160488 			}
1713e1dd0a2fSth160488 			if (credLevel[i] == NULL) {
1714e1dd0a2fSth160488 				CLIENT_FPRINTF(stderr, gettext(
1715e1dd0a2fSth160488 				    "Warning: init credential level not found "
1716e1dd0a2fSth160488 				    "in DUAConfigProfile.\n"));
1717e1dd0a2fSth160488 			} else {
1718e1dd0a2fSth160488 				if (i != 0) {
1719e1dd0a2fSth160488 					CLIENT_FPRINTF(stderr,
1720e1dd0a2fSth160488 					    gettext("Warning: "
1721e1dd0a2fSth160488 					    "init credential level using "
1722e1dd0a2fSth160488 					    "secondary credential level from "
1723e1dd0a2fSth160488 					    "DUAConfigProfile.\n"));
1724e1dd0a2fSth160488 				}
1725e1dd0a2fSth160488 			}
1726e1dd0a2fSth160488 			(void) __ns_ldap_freeParam((void ***) &credLevel);
1727e1dd0a2fSth160488 		}
1728e1dd0a2fSth160488 	}
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(
17357c478bd9Sstevel@tonic-gate 		    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(
17457c478bd9Sstevel@tonic-gate 		    gettext("About to modify this machines configuration "
17467c478bd9Sstevel@tonic-gate 		    "by writing the files\n"),
17477c478bd9Sstevel@tonic-gate 		    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(
17537c478bd9Sstevel@tonic-gate 		    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(
17657c478bd9Sstevel@tonic-gate 		    gettext("System not in state to enable ldap client.\n"),
17667c478bd9Sstevel@tonic-gate 		    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(
17727c478bd9Sstevel@tonic-gate 		    gettext("Save of system configuration failed.  "
17737c478bd9Sstevel@tonic-gate 		    "Attempting recovery.\n"),
17747c478bd9Sstevel@tonic-gate 		    stderr);
17757c478bd9Sstevel@tonic-gate 		retcode = recover(STATE_NOSAVE);
17767c478bd9Sstevel@tonic-gate 		if (retcode != CLIENT_SUCCESS) {
17777c478bd9Sstevel@tonic-gate 			CLIENT_FPUTS(
17787c478bd9Sstevel@tonic-gate 			    gettext("Recovery of systems configuration "
17797c478bd9Sstevel@tonic-gate 			    "failed.  Manual intervention of "
17807c478bd9Sstevel@tonic-gate 			    "config files is required.\n"),
17817c478bd9Sstevel@tonic-gate 			    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 "
17877c478bd9Sstevel@tonic-gate 			    "starting services during reset\n"),
17887c478bd9Sstevel@tonic-gate 			    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,
17987c478bd9Sstevel@tonic-gate 		    gettext("%s init: errorp is not NULL; %s\n"),
17997c478bd9Sstevel@tonic-gate 		    cmd, errorp->message);
18007c478bd9Sstevel@tonic-gate 		retcode = recover(STATE_NOSAVE);
18017c478bd9Sstevel@tonic-gate 		if (retcode != CLIENT_SUCCESS) {
18027c478bd9Sstevel@tonic-gate 			CLIENT_FPUTS(
18037c478bd9Sstevel@tonic-gate 			    gettext("Recovery of systems configuration "
18047c478bd9Sstevel@tonic-gate 			    "failed.  Manual intervention of "
18057c478bd9Sstevel@tonic-gate 			    "config files is required.\n"),
18067c478bd9Sstevel@tonic-gate 			    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 "
18137c478bd9Sstevel@tonic-gate 			    "starting services during reset\n"),
18147c478bd9Sstevel@tonic-gate 			    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,
18237c478bd9Sstevel@tonic-gate 		    gettext("%s init: errorp is not NULL; %s\n"),
18247c478bd9Sstevel@tonic-gate 		    cmd, errorp->message);
18257c478bd9Sstevel@tonic-gate 		retcode = recover(STATE_NOSAVE);
18267c478bd9Sstevel@tonic-gate 		if (retcode != CLIENT_SUCCESS) {
18277c478bd9Sstevel@tonic-gate 			CLIENT_FPUTS(
18287c478bd9Sstevel@tonic-gate 			    gettext("Recovery of systems configuration "
18297c478bd9Sstevel@tonic-gate 			    "failed.  Manual intervention of "
18307c478bd9Sstevel@tonic-gate 			    "config files is required.\n"),
18317c478bd9Sstevel@tonic-gate 			    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 "
18387c478bd9Sstevel@tonic-gate 			    "starting services during reset\n"),
18397c478bd9Sstevel@tonic-gate 			    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,
18477c478bd9Sstevel@tonic-gate 		    gettext("Error %d copying (%s) -> (%s)\n"),
18487c478bd9Sstevel@tonic-gate 		    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(
18527c478bd9Sstevel@tonic-gate 			    gettext("Recovery of systems configuration "
18537c478bd9Sstevel@tonic-gate 			    "failed.  Manual intervention of "
18547c478bd9Sstevel@tonic-gate 			    "config files is required.\n"),
18557c478bd9Sstevel@tonic-gate 			    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 "
18607c478bd9Sstevel@tonic-gate 			    "starting services during reset\n"),
18617c478bd9Sstevel@tonic-gate 			    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,
18677c478bd9Sstevel@tonic-gate 	    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(
18727c478bd9Sstevel@tonic-gate 			    gettext("Recovery of systems configuration "
18737c478bd9Sstevel@tonic-gate 			    "failed.  Manual intervention of "
18747c478bd9Sstevel@tonic-gate 			    "config files is required.\n"),
18757c478bd9Sstevel@tonic-gate 			    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 "
18817c478bd9Sstevel@tonic-gate 			    "starting services during reset\n"),
18827c478bd9Sstevel@tonic-gate 			    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"),
18947c478bd9Sstevel@tonic-gate 		    stderr);
18957c478bd9Sstevel@tonic-gate 	} else {
18967c478bd9Sstevel@tonic-gate 		CLIENT_FPUTS(gettext("Error resetting system.\n"
18977c478bd9Sstevel@tonic-gate 		    "Recovering old system settings.\n"), stderr),
18987c478bd9Sstevel@tonic-gate 
18997c478bd9Sstevel@tonic-gate 		    /* stop any started services for recover */
19007c478bd9Sstevel@tonic-gate 		    /* don't stomp on history of saved services state */
19017c478bd9Sstevel@tonic-gate 		    reset_ret = stop_services(STATE_NOSAVE);
19027c478bd9Sstevel@tonic-gate 		if (reset_ret != CLIENT_SUCCESS) {
19037c478bd9Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr, gettext("Error (%d) while "
19047c478bd9Sstevel@tonic-gate 			    "stopping services during reset\n"),
19057c478bd9Sstevel@tonic-gate 			    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 "
19117c478bd9Sstevel@tonic-gate 			    "recovering service files during "
19127c478bd9Sstevel@tonic-gate 			    "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 "
19187c478bd9Sstevel@tonic-gate 			    "starting services during reset\n"),
19197c478bd9Sstevel@tonic-gate 			    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
usage(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,
19357c478bd9Sstevel@tonic-gate 		    gettext("Usage: %s [-v | -q] init | manual | mod | "
19367c478bd9Sstevel@tonic-gate 		    "list | uninit [<args>]\n"),
19377c478bd9Sstevel@tonic-gate 		    cmd);
19387c478bd9Sstevel@tonic-gate 
1939e1dd0a2fSth160488 		CLIENT_FPRINTF(stderr,
1940e1dd0a2fSth160488 		    gettext("\n       %s [-v | -q] [-a authenticationMethod]"
1941e1dd0a2fSth160488 		    " [-D bindDN]\n\t[-w bindPassword] [-j passswdFile]"
1942dd1104fbSMichen Chang 		    " [-y proxyPasswordFile]\n\t"
1943dd1104fbSMichen Chang 		    "[-z adminPasswordFile] init [<args>]\n"),
1944e1dd0a2fSth160488 		    cmd);
1945e1dd0a2fSth160488 
19467c478bd9Sstevel@tonic-gate 		CLIENT_FPUTS(
19477c478bd9Sstevel@tonic-gate 		    gettext("\nSet up a server or workstation as a "
19487c478bd9Sstevel@tonic-gate 		    "client of an LDAP namespace.\n"),
19497c478bd9Sstevel@tonic-gate 		    stderr);
19507c478bd9Sstevel@tonic-gate 	} else {	/* genprofile */
19517c478bd9Sstevel@tonic-gate 		CLIENT_FPRINTF(stderr,
19527c478bd9Sstevel@tonic-gate 		    gettext("Usage: %s [-v | -q] genprofile "
19537c478bd9Sstevel@tonic-gate 		    "-a profileName=<name> "
19547c478bd9Sstevel@tonic-gate 		    "-a defaultSearchBase=<base> <args>\n"),
19557c478bd9Sstevel@tonic-gate 		    cmd);
19567c478bd9Sstevel@tonic-gate 
19577c478bd9Sstevel@tonic-gate 		CLIENT_FPUTS(
19587c478bd9Sstevel@tonic-gate 		    gettext("\nGenerate a profile used to set up clients "
19597c478bd9Sstevel@tonic-gate 		    "of an LDAP namespace.\n"),
19607c478bd9Sstevel@tonic-gate 		    stderr);
19617c478bd9Sstevel@tonic-gate 	}
19627c478bd9Sstevel@tonic-gate 	CLIENT_FPUTS(
19637c478bd9Sstevel@tonic-gate 	    gettext("<args> take the form of \'-a attrName=attrVal\' as "
19647c478bd9Sstevel@tonic-gate 	    "described in the\n"),
19657c478bd9Sstevel@tonic-gate 	    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
stop_services(int saveState)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 "
19997c478bd9Sstevel@tonic-gate 			    "failed with (%d). You may need to restart "
20007c478bd9Sstevel@tonic-gate 			    "it manually for changes to take effect.\n"),
20017c478bd9Sstevel@tonic-gate 			    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 "
20297c478bd9Sstevel@tonic-gate 			    "failed with (%d). You may need to restart "
20307c478bd9Sstevel@tonic-gate 			    "it manually for changes to take effect.\n"),
20317c478bd9Sstevel@tonic-gate 			    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"),
20527c478bd9Sstevel@tonic-gate 			    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"),
20697c478bd9Sstevel@tonic-gate 			    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
start_services(int flag)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 "
21027c478bd9Sstevel@tonic-gate 		    "(%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"),
21097c478bd9Sstevel@tonic-gate 			    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,
21157c478bd9Sstevel@tonic-gate 			    gettext("Error closing defaultdomain (%d)\n"),
21167c478bd9Sstevel@tonic-gate 			    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) -
21227c478bd9Sstevel@tonic-gate 		    sizeof (TO_DEV_NULL) - 3)) {
21237c478bd9Sstevel@tonic-gate 			CLIENT_FPUTS(gettext("Specified domainname is "
21247c478bd9Sstevel@tonic-gate 			    "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",
2131cb5caa98Sdjl 		    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",
21427c478bd9Sstevel@tonic-gate 			    CMD_DOMAIN_START, domainname,
21437c478bd9Sstevel@tonic-gate 			    (sysret == 0) ? gettext("success") :
21447c478bd9Sstevel@tonic-gate 			    gettext("failed"));
21457c478bd9Sstevel@tonic-gate 		if (sysret != 0) {
21467c478bd9Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr, gettext("\"%s\" returned: %d\n"),
21477c478bd9Sstevel@tonic-gate 			    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)) !=
2153cb5caa98Sdjl 		    NS_LDAP_SUCCESS) {
2154cb5caa98Sdjl 			CLIENT_FPRINTF(stderr, gettext("Error (%d) while "
2155cb5caa98Sdjl 			    "checking sasl/GSSAPI configuration\n"),
2156cb5caa98Sdjl 			    rc);
2157cb5caa98Sdjl 			retcode = CLIENT_ERR_FAIL;
2158cb5caa98Sdjl 		}
2159cb5caa98Sdjl 
2160cb5caa98Sdjl 		if (config != NS_LDAP_SELF_GSSAPI_CONFIG_NONE) {
2161cb5caa98Sdjl 
2162cb5caa98Sdjl 			rc = __ns_ldap_check_dns_preq(
2163cb5caa98Sdjl 			    1, mode_verbose, mode_quiet,
2164cb5caa98Sdjl 			    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 &&
2173cb5caa98Sdjl 		    start_service(LDAP_FMRI, B_TRUE) != CLIENT_SUCCESS)
21747c478bd9Sstevel@tonic-gate 			retcode = CLIENT_ERR_FAIL;
21757c478bd9Sstevel@tonic-gate 
2176cb5caa98Sdjl 		if (config != NS_LDAP_SELF_GSSAPI_CONFIG_NONE &&
2177cb5caa98Sdjl 		    rc == NS_LDAP_SUCCESS && retcode == CLIENT_SUCCESS) {
2178cb5caa98Sdjl 			rc = __ns_ldap_check_gssapi_preq(
2179cb5caa98Sdjl 			    1, mode_verbose, mode_quiet, config,
2180cb5caa98Sdjl 			    &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",
22047c478bd9Sstevel@tonic-gate 				    CMD_DOMAIN_START, domainname,
22057c478bd9Sstevel@tonic-gate 				    (sysret == 0) ? gettext("success") :
22067c478bd9Sstevel@tonic-gate 				    gettext("failed"));
22077c478bd9Sstevel@tonic-gate 			if (sysret != 0) {
22087c478bd9Sstevel@tonic-gate 				CLIENT_FPRINTF(stderr, gettext("\"%s\" "
22097c478bd9Sstevel@tonic-gate 				    "returned: %d\n"),
22107c478bd9Sstevel@tonic-gate 				    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)
22197c478bd9Sstevel@tonic-gate 				    != 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",
2241cb5caa98Sdjl 		    gettext("self/sasl/GSSAPI is configured"
2242cb5caa98Sdjl 		    " 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
credCheck(clientopts_t * arglist)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,
22787c478bd9Sstevel@tonic-gate 	    (void ***)&credLevel, &errorp);
22797c478bd9Sstevel@tonic-gate 	if (retcode != 0) {
22807c478bd9Sstevel@tonic-gate 		CLIENT_FPRINTF(stderr,
22817c478bd9Sstevel@tonic-gate 		    gettext("Error %d while trying to retrieve "
22827c478bd9Sstevel@tonic-gate 		    "credLevel\n"),
22837c478bd9Sstevel@tonic-gate 		    retcode);
22847c478bd9Sstevel@tonic-gate 		return (CLIENT_ERR_FAIL);
22857c478bd9Sstevel@tonic-gate 	}
22867c478bd9Sstevel@tonic-gate 	retcode = __ns_ldap_getParam(NS_LDAP_AUTH_P,
22877c478bd9Sstevel@tonic-gate 	    (void ***)&authMethod, &errorp);
22887c478bd9Sstevel@tonic-gate 	if (retcode != 0) {
22897c478bd9Sstevel@tonic-gate 		CLIENT_FPRINTF(stderr,
22907c478bd9Sstevel@tonic-gate 		    gettext("Error %d while trying to retrieve "
22917c478bd9Sstevel@tonic-gate 		    "authMethod\n"), retcode);
22927c478bd9Sstevel@tonic-gate 		return (CLIENT_ERR_FAIL);
22937c478bd9Sstevel@tonic-gate 	}
22947c478bd9Sstevel@tonic-gate 	retcode = __ns_ldap_getParam(NS_LDAP_BINDDN_P,
22957c478bd9Sstevel@tonic-gate 	    (void ***)&proxyDN, &errorp);
22967c478bd9Sstevel@tonic-gate 	if (retcode != 0) {
22977c478bd9Sstevel@tonic-gate 		CLIENT_FPRINTF(stderr,
22987c478bd9Sstevel@tonic-gate 		    gettext("Error %d while trying to retrieve proxyDN\n"),
22997c478bd9Sstevel@tonic-gate 		    retcode);
23007c478bd9Sstevel@tonic-gate 		return (CLIENT_ERR_FAIL);
23017c478bd9Sstevel@tonic-gate 	}
23027c478bd9Sstevel@tonic-gate 	retcode = __ns_ldap_getParam(NS_LDAP_BINDPASSWD_P,
23037c478bd9Sstevel@tonic-gate 	    (void ***)&proxyPassword, &errorp);
23047c478bd9Sstevel@tonic-gate 	if (retcode != 0) {
23057c478bd9Sstevel@tonic-gate 		CLIENT_FPRINTF(stderr,
23067c478bd9Sstevel@tonic-gate 		    gettext("Error %d while trying to retrieve "
23077c478bd9Sstevel@tonic-gate 		    "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,
23137c478bd9Sstevel@tonic-gate 		    gettext("Proxy DN: %s\n"),
23147c478bd9Sstevel@tonic-gate 		    (proxyDN && proxyDN[0]) ? proxyDN[0] : "NULL");
23157c478bd9Sstevel@tonic-gate 		CLIENT_FPRINTF(stderr,
23167c478bd9Sstevel@tonic-gate 		    gettext("Proxy password: %s\n"),
23177c478bd9Sstevel@tonic-gate 		    (proxyPassword && proxyPassword[0]) ?
23187c478bd9Sstevel@tonic-gate 		    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,
23257c478bd9Sstevel@tonic-gate 			    gettext("Credential level: %d\n"),
23267c478bd9Sstevel@tonic-gate 			    *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;
23357c478bd9Sstevel@tonic-gate 	    authMethod && authMethod[counter] != NULL;
23367c478bd9Sstevel@tonic-gate 	    counter++) {
23377c478bd9Sstevel@tonic-gate 
23387c478bd9Sstevel@tonic-gate 		if (mode_verbose)
23397c478bd9Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr,
23407c478bd9Sstevel@tonic-gate 			    gettext("Authentication method: %d\n"),
23417c478bd9Sstevel@tonic-gate 			    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(
23547c478bd9Sstevel@tonic-gate 			    gettext("No proxyDN/proxyPassword required\n"),
23557c478bd9Sstevel@tonic-gate 			    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(
23627c478bd9Sstevel@tonic-gate 		    gettext("credentialLevel is proxy and no proxyDN "
23637c478bd9Sstevel@tonic-gate 		    "specified\n"),
23647c478bd9Sstevel@tonic-gate 		    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(
23717c478bd9Sstevel@tonic-gate 		    gettext("credentialLevel requires proxyPassword\n"),
23727c478bd9Sstevel@tonic-gate 		    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(
23817c478bd9Sstevel@tonic-gate 			    gettext("setParam proxyPassword failed.\n"),
23827c478bd9Sstevel@tonic-gate 			    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 
23907c478bd9Sstevel@tonic-gate /*
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
adminCredCheck(clientopts_t * arglist)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 
2573dd1104fbSMichen Chang /*
25747c478bd9Sstevel@tonic-gate  * try to restore the previous name space on this machine
25757c478bd9Sstevel@tonic-gate  */
25767c478bd9Sstevel@tonic-gate static int
recover(int saveState)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(
25947c478bd9Sstevel@tonic-gate 		    gettext("Cannot recover.  No backup files "
25957c478bd9Sstevel@tonic-gate 		    "found.\n"),
25967c478bd9Sstevel@tonic-gate 		    stderr);
25977c478bd9Sstevel@tonic-gate 		CLIENT_FPUTS(
25987c478bd9Sstevel@tonic-gate 		    gettext("\t Either this machine was not initialized\n"),
25997c478bd9Sstevel@tonic-gate 		    stderr);
26007c478bd9Sstevel@tonic-gate 		CLIENT_FPUTS(
26017c478bd9Sstevel@tonic-gate 		    gettext("\t by ldapclient or the backup files "
26027c478bd9Sstevel@tonic-gate 		    "have been\n"),
26037c478bd9Sstevel@tonic-gate 		    stderr);
26047c478bd9Sstevel@tonic-gate 		CLIENT_FPUTS(
26057c478bd9Sstevel@tonic-gate 		    gettext("\t removed manually or with an \"uninit\"\n"),
26067c478bd9Sstevel@tonic-gate 		    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,
26177c478bd9Sstevel@tonic-gate 		    gettext("recover: stat(%s)=%d\n"),
26187c478bd9Sstevel@tonic-gate 		    DOMAINNAME_BACK, stat_ret);
26197c478bd9Sstevel@tonic-gate 	if (stat_ret == 0) {
26207c478bd9Sstevel@tonic-gate 		if (mode_verbose)
26217c478bd9Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr,
26227c478bd9Sstevel@tonic-gate 			    gettext("recover: open(%s)\n"),
26237c478bd9Sstevel@tonic-gate 			    DOMAINNAME_BACK);
26247c478bd9Sstevel@tonic-gate 		fd = open(DOMAINNAME_BACK, O_RDONLY);
26257c478bd9Sstevel@tonic-gate 		if (mode_verbose)
26267c478bd9Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr,
26277c478bd9Sstevel@tonic-gate 			    gettext("recover: read(%s)\n"),
26287c478bd9Sstevel@tonic-gate 			    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(
26337c478bd9Sstevel@tonic-gate 			    gettext("Cannot recover.  Cannot determine "
26347c478bd9Sstevel@tonic-gate 			    "previous domain name.\n"),
26357c478bd9Sstevel@tonic-gate 			    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,
26487c478bd9Sstevel@tonic-gate 				    gettext("recover: old domainname "
26497c478bd9Sstevel@tonic-gate 				    "\"%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,
26767c478bd9Sstevel@tonic-gate 		    gettext("recover: stat(%s)=%d\n"),
26777c478bd9Sstevel@tonic-gate 		    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,
26847c478bd9Sstevel@tonic-gate 			    gettext("recover: file_move(%s, %s)=%d\n"),
26857c478bd9Sstevel@tonic-gate 			    ldap_file_back, NSCONFIGFILE, retcode);
26867c478bd9Sstevel@tonic-gate 		if (retcode != 0)
26877c478bd9Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr,
26887c478bd9Sstevel@tonic-gate 			    gettext("recover: file_move(%s, %s) failed\n"),
26897c478bd9Sstevel@tonic-gate 			    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,
26987c478bd9Sstevel@tonic-gate 		    gettext("recover: stat(%s)=%d\n"),
26997c478bd9Sstevel@tonic-gate 		    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,
27047c478bd9Sstevel@tonic-gate 			    gettext("recover: file_move(%s, %s)=%d\n"),
27057c478bd9Sstevel@tonic-gate 			    ldap_cred_back, NSCREDFILE, retcode);
27067c478bd9Sstevel@tonic-gate 		if (retcode != 0)
27077c478bd9Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr,
27087c478bd9Sstevel@tonic-gate 			    gettext("recover: file_move(%s, %s) failed\n"),
27097c478bd9Sstevel@tonic-gate 			    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,
27257c478bd9Sstevel@tonic-gate 			    gettext("recover: stat(%s)=%d\n"),
27267c478bd9Sstevel@tonic-gate 			    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,
27337c478bd9Sstevel@tonic-gate 				    gettext("recover: file_move(%s, "
27347c478bd9Sstevel@tonic-gate 				    "%s)=%d\n"),
27357c478bd9Sstevel@tonic-gate 				    yp_dir_back, yp_dir, retcode);
27367c478bd9Sstevel@tonic-gate 			if (retcode != 0) {
27377c478bd9Sstevel@tonic-gate 				CLIENT_FPRINTF(stderr,
27387c478bd9Sstevel@tonic-gate 				    gettext("recover: file_move(%s, "
27397c478bd9Sstevel@tonic-gate 				    "%s) failed!\n"),
27407c478bd9Sstevel@tonic-gate 				    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,
27527c478bd9Sstevel@tonic-gate 		    gettext("recover: stat(%s)=%d\n"),
27537c478bd9Sstevel@tonic-gate 		    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,
27587c478bd9Sstevel@tonic-gate 			    gettext("recover: file_move(%s, %s)=%d\n"),
27597c478bd9Sstevel@tonic-gate 			    NSSWITCH_BACK, NSSWITCH_CONF, retcode);
27607c478bd9Sstevel@tonic-gate 		if (retcode != 0)
27617c478bd9Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr,
27627c478bd9Sstevel@tonic-gate 			    gettext("recover: file_move(%s, %s) failed\n"),
27637c478bd9Sstevel@tonic-gate 			    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,
27697c478bd9Sstevel@tonic-gate 		    gettext("recover: stat(%s)=%d\n"),
27707c478bd9Sstevel@tonic-gate 		    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,
27757c478bd9Sstevel@tonic-gate 			    gettext("recover: file_move(%s, %s)=%d\n"),
27767c478bd9Sstevel@tonic-gate 			    DOMAINNAME_BACK, DOMAINNAME, retcode);
27777c478bd9Sstevel@tonic-gate 		if (retcode != 0)
27787c478bd9Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr,
27797c478bd9Sstevel@tonic-gate 			    gettext("recover: file_move(%s, %s) failed\n"),
27807c478bd9Sstevel@tonic-gate 			    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,
27867c478bd9Sstevel@tonic-gate 		    gettext("Error removing \"%s\" directory.\n"),
27877c478bd9Sstevel@tonic-gate 		    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
file_backup(void)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(
28247c478bd9Sstevel@tonic-gate 			    gettext("Removing existing restore "
28257c478bd9Sstevel@tonic-gate 			    "directory\n"),
28267c478bd9Sstevel@tonic-gate 			    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,
28327c478bd9Sstevel@tonic-gate 			    gettext("Unable to remove backup "
28337c478bd9Sstevel@tonic-gate 			    "directory (%s)\n"),
28347c478bd9Sstevel@tonic-gate 			    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,
28427c478bd9Sstevel@tonic-gate 		    gettext("file_backup: Failed to make %s backup "
28437c478bd9Sstevel@tonic-gate 		    "directory. mkdir=%d\n"),
28447c478bd9Sstevel@tonic-gate 		    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,
28517c478bd9Sstevel@tonic-gate 		    gettext("file_backup: stat(%s)=%d\n"),
28527c478bd9Sstevel@tonic-gate 		    NSSWITCH_CONF, conf_stat);
28537c478bd9Sstevel@tonic-gate 	if (conf_stat == 0) {
28547c478bd9Sstevel@tonic-gate 		if (mode_verbose)
28557c478bd9Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr,
28567c478bd9Sstevel@tonic-gate 			    gettext("file_backup: (%s -> %s)\n"),
28577c478bd9Sstevel@tonic-gate 			    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,
28617c478bd9Sstevel@tonic-gate 			    gettext("file_backup: file_move(%s, %s) failed "
28627c478bd9Sstevel@tonic-gate 			    "with %d\n"),
28637c478bd9Sstevel@tonic-gate 			    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,
28697c478bd9Sstevel@tonic-gate 			    gettext("file_backup: No %s file.\n"),
28707c478bd9Sstevel@tonic-gate 			    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,
28767c478bd9Sstevel@tonic-gate 		    gettext("file_backup: stat(%s)=%d\n"),
28777c478bd9Sstevel@tonic-gate 		    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,
28817c478bd9Sstevel@tonic-gate 			    gettext("file_backup: (%s -> %s)\n"),
28827c478bd9Sstevel@tonic-gate 			    DOMAINNAME, DOMAINNAME_BACK);
28837c478bd9Sstevel@tonic-gate 		retcode = file_move(DOMAINNAME, DOMAINNAME_BACK);
28847c478bd9Sstevel@tonic-gate 		if (retcode != 0) {
28857c478bd9Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr,
28867c478bd9Sstevel@tonic-gate 			    gettext("file_backup: file_move(%s, %s) failed "
28877c478bd9Sstevel@tonic-gate 			    "with %d\n"),
28887c478bd9Sstevel@tonic-gate 			    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,
28957c478bd9Sstevel@tonic-gate 				    gettext("file_backup: No %s file.\n"),
28967c478bd9Sstevel@tonic-gate 				    DOMAINNAME);
28977c478bd9Sstevel@tonic-gate 			} else {
28987c478bd9Sstevel@tonic-gate 				CLIENT_FPRINTF(stderr,
28997c478bd9Sstevel@tonic-gate 				    gettext("file_backup: Empty %s "
29007c478bd9Sstevel@tonic-gate 				    "file.\n"),
29017c478bd9Sstevel@tonic-gate 				    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,
29117c478bd9Sstevel@tonic-gate 		    gettext("file_backup: nis domain is \"%s\"\n"),
29127c478bd9Sstevel@tonic-gate 		    (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,
29217c478bd9Sstevel@tonic-gate 			    gettext("file_backup: stat(%s)=%d\n"),
29227c478bd9Sstevel@tonic-gate 			    yp_dir, yp_stat);
29237c478bd9Sstevel@tonic-gate 		if (yp_stat == 0) {
29247c478bd9Sstevel@tonic-gate 			(void) strlcpy(yp_dir_back, LDAP_RESTORE_DIR "/",
29257c478bd9Sstevel@tonic-gate 			    BUFSIZE);
29267c478bd9Sstevel@tonic-gate 			(void) strlcat(yp_dir_back, name, BUFSIZE);
29277c478bd9Sstevel@tonic-gate 			if (mode_verbose)
29287c478bd9Sstevel@tonic-gate 				CLIENT_FPRINTF(stderr,
29297c478bd9Sstevel@tonic-gate 				    gettext("file_backup: (%s -> %s)\n"),
29307c478bd9Sstevel@tonic-gate 				    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,
29347c478bd9Sstevel@tonic-gate 				    gettext("file_backup: file_move(%s, %s)"
29357c478bd9Sstevel@tonic-gate 				    " failed with %d\n"),
29367c478bd9Sstevel@tonic-gate 				    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,
29427c478bd9Sstevel@tonic-gate 				    gettext("file_backup: No %s "
29437c478bd9Sstevel@tonic-gate 				    "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,
29557c478bd9Sstevel@tonic-gate 		    gettext("file_backup: stat(%s)=%d\n"),
29567c478bd9Sstevel@tonic-gate 		    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,
29627c478bd9Sstevel@tonic-gate 			    gettext("file_backup: (%s -> %s)\n"),
29637c478bd9Sstevel@tonic-gate 			    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,
29677c478bd9Sstevel@tonic-gate 			    gettext("file_backup: file_move(%s, %s) failed "
29687c478bd9Sstevel@tonic-gate 			    "with %d\n"),
29697c478bd9Sstevel@tonic-gate 			    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,
29777c478bd9Sstevel@tonic-gate 			    gettext("file_backup: (%s -> %s)\n"),
29787c478bd9Sstevel@tonic-gate 			    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,
29827c478bd9Sstevel@tonic-gate 			    gettext("file_backup: file_move(%s, %s) failed "
29837c478bd9Sstevel@tonic-gate 			    "with %d\n"),
29847c478bd9Sstevel@tonic-gate 			    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,
29907c478bd9Sstevel@tonic-gate 			    gettext("file_backup: No %s file.\n"),
29917c478bd9Sstevel@tonic-gate 			    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
mod_backup(void)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
mod_recover(void)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
mod_cleanup(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 *
multival_new()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(
30997c478bd9Sstevel@tonic-gate 		    gettext("multival_new: Memory allocation error\n"),
31007c478bd9Sstevel@tonic-gate 		    stderr);
31017c478bd9Sstevel@tonic-gate 	}
31027c478bd9Sstevel@tonic-gate 	return (hold);	/* NULL -> error */
31037c478bd9Sstevel@tonic-gate }
31047c478bd9Sstevel@tonic-gate 
31057c478bd9Sstevel@tonic-gate static int
multival_add(multival_t * list,char * opt)31067c478bd9Sstevel@tonic-gate multival_add(multival_t *list, char *opt)
31077c478bd9Sstevel@tonic-gate {
31087c478bd9Sstevel@tonic-gate 	if (opt == NULL) {
31097c478bd9Sstevel@tonic-gate 		CLIENT_FPUTS(
31107c478bd9Sstevel@tonic-gate 		    gettext("Empty value passed to multival_add\n"),
31117c478bd9Sstevel@tonic-gate 		    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,
31197c478bd9Sstevel@tonic-gate 		    (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
multival_free(multival_t * list)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 *
clientopts_new()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 "
31527c478bd9Sstevel@tonic-gate 		    "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 "
31597c478bd9Sstevel@tonic-gate 		    "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 "
31677c478bd9Sstevel@tonic-gate 		    "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 "
31767c478bd9Sstevel@tonic-gate 		    "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 "
31867c478bd9Sstevel@tonic-gate 		    "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 "
31977c478bd9Sstevel@tonic-gate 		    "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
clientopts_free(clientopts_t * list)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
multival_list(char * opt,multival_t * list)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
num_args(clientopts_t * list)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
dumpargs(clientopts_t * list)32767c478bd9Sstevel@tonic-gate dumpargs(clientopts_t *list)
32777c478bd9Sstevel@tonic-gate {
32787c478bd9Sstevel@tonic-gate 	CLIENT_PRINT("\tauthenticationMethod: ", list->authenticationMethod);
32797c478bd9Sstevel@tonic-gate 	multival_list("\tserviceAuthenticationMethod: ",
32807c478bd9Sstevel@tonic-gate 	    list->serviceAuthenticationMethod);
32817c478bd9Sstevel@tonic-gate 	CLIENT_PRINT("\tdefaultSearchBase: ", list->defaultSearchBase);
32827c478bd9Sstevel@tonic-gate 	CLIENT_PRINT("\tcredentialLevel: ", list->credentialLevel);
32837c478bd9Sstevel@tonic-gate 	multival_list("\tserviceCredentialLevel: ",
32847c478bd9Sstevel@tonic-gate 	    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: ",
32987c478bd9Sstevel@tonic-gate 	    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
parseParam(char * param,char ** paramVal)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(
33537c478bd9Sstevel@tonic-gate 		    gettext("Didn\'t find \'=\' character in string\n"),
33547c478bd9Sstevel@tonic-gate 		    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
clientSetParam(clientopts_t * optlist,int paramFlag,char * attrVal)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,
34027c478bd9Sstevel@tonic-gate 		    attrVal);
34037c478bd9Sstevel@tonic-gate 		if (retcode != CLIENT_SUCCESS) {
34047c478bd9Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr,
34057c478bd9Sstevel@tonic-gate 			    gettext("Error processing attrVal %s\n"),
34067c478bd9Sstevel@tonic-gate 			    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,
34257c478bd9Sstevel@tonic-gate 		    attrVal);
34267c478bd9Sstevel@tonic-gate 		if (retcode != CLIENT_SUCCESS) {
34277c478bd9Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr,
34287c478bd9Sstevel@tonic-gate 			    gettext("Error processing attrVal %s\n"),
34297c478bd9Sstevel@tonic-gate 			    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,
34667c478bd9Sstevel@tonic-gate 			    gettext("Error processing attrVal %s\n"),
34677c478bd9Sstevel@tonic-gate 			    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;
34847c478bd9Sstevel@tonic-gate 		    counter < strlen(optlist->preferredServerList);
34857c478bd9Sstevel@tonic-gate 		    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,
35117c478bd9Sstevel@tonic-gate 			    gettext("Error processing attrVal %s\n"),
35127c478bd9Sstevel@tonic-gate 			    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,
35267c478bd9Sstevel@tonic-gate 		    attrVal);
35277c478bd9Sstevel@tonic-gate 		if (retcode != CLIENT_SUCCESS) {
35287c478bd9Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr,
35297c478bd9Sstevel@tonic-gate 			    gettext("Error processing attrVal %s\n"),
35307c478bd9Sstevel@tonic-gate 			    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
file_move(const char * from,const char * to)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",
35867c478bd9Sstevel@tonic-gate 	    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
do_service(const char * fmri,boolean_t waitflag,int dowhat,const char * state)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
wait_till(const char * fmri,const char * state,useconds_t max,const char * what,boolean_t check_maint)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
is_service(const char * fmri,const char * state)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
get_timeout_value(int dowhat,const char * fmri,useconds_t default_val)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",
38317c478bd9Sstevel@tonic-gate 			    actionstr,
38327c478bd9Sstevel@tonic-gate 			    fmri,
38337c478bd9Sstevel@tonic-gate 			    gettext("failed to retrieve timeout property"),
38347c478bd9Sstevel@tonic-gate 			    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",
38427c478bd9Sstevel@tonic-gate 			    actionstr,
38437c478bd9Sstevel@tonic-gate 			    fmri,
38447c478bd9Sstevel@tonic-gate 			    gettext("failed to retrieve timeout value"),
38457c478bd9Sstevel@tonic-gate 			    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