xref: /onnv-gate/usr/src/cmd/ldap/ns_ldap/ldapclient.c (revision 11262:b7ebfbf2359e)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
52830Sdjl  * Common Development and Distribution License (the "License").
62830Sdjl  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
228821SMichen.Chang@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate /*
270Sstevel@tonic-gate  * ldapclient command. To make (initiailize) or uninitialize a machines as
280Sstevel@tonic-gate  * and LDAP client.  This command MUST be run as root (or it will simply exit).
290Sstevel@tonic-gate  *
300Sstevel@tonic-gate  *	-I	Install. No file_backup/recover for installing only (no doc).
310Sstevel@tonic-gate  *
320Sstevel@tonic-gate  *	init	Initialze (create) an LDAP client from a profile stored
330Sstevel@tonic-gate  *		in a directory-server.
340Sstevel@tonic-gate  *	manual	Initialze (create) an LDAP client by hand (-file option
350Sstevel@tonic-gate  *		reads from file).
360Sstevel@tonic-gate  *	mod	Modify the LDAP client configuration on this machine by hand.
370Sstevel@tonic-gate  *	list	List the contents of the LDAP client cache files.
380Sstevel@tonic-gate  *	uninit	Uninitialize this machine.
390Sstevel@tonic-gate  *
400Sstevel@tonic-gate  *	-v	Verbose flag.
410Sstevel@tonic-gate  *	-q	Quiet flag (mutually exclusive with -v).
420Sstevel@tonic-gate  *
430Sstevel@tonic-gate  *	-a attrName=attrVal
440Sstevel@tonic-gate  *	<attrName> can be one of the following:
450Sstevel@tonic-gate  *
460Sstevel@tonic-gate  *	attributeMap
470Sstevel@tonic-gate  *		Attribute map.  Can be multiple instances of this option.
480Sstevel@tonic-gate  *		(no former option)
490Sstevel@tonic-gate  *	authenticationMethod
500Sstevel@tonic-gate  *		Authentication method (formerly -a)
510Sstevel@tonic-gate  *	bindTimeLimit
520Sstevel@tonic-gate  *		Bind time limit. (no former option)
530Sstevel@tonic-gate  *	certificatePath
540Sstevel@tonic-gate  *		Path to certificates used for secure bind (no former option)
550Sstevel@tonic-gate  *	credentialLevel
560Sstevel@tonic-gate  *		Client credential level (no former option)
570Sstevel@tonic-gate  *	defaultServerList
580Sstevel@tonic-gate  *		Default server (no former option) Refer to DUA Config
590Sstevel@tonic-gate  *		Schema draft.
600Sstevel@tonic-gate  *	defaultSearchBase
610Sstevel@tonic-gate  *		Search Base DN. e.g. dc=eng,dc=sun,dc=com (formerly -b)
620Sstevel@tonic-gate  *	defaultSearchScope
630Sstevel@tonic-gate  *		Search scope. (formerly -s)
640Sstevel@tonic-gate  *	domainName
650Sstevel@tonic-gate  *		Hosts lookup domain (DNS)  Ex. eng.sun.com (formerly -d)
660Sstevel@tonic-gate  *	followReferrals
670Sstevel@tonic-gate  *		Search dereference. followref or noref (default followref)
680Sstevel@tonic-gate  *		(formerly -r)
690Sstevel@tonic-gate  *	objectclassMap
700Sstevel@tonic-gate  *		Objectclass map.  Can be multiple instances of this option.
710Sstevel@tonic-gate  *		(no former option)
720Sstevel@tonic-gate  *	preferredServerList
730Sstevel@tonic-gate  *		Server preference list. Comma ',' seperated list of IPaddr.
740Sstevel@tonic-gate  *		(formerly -p)
750Sstevel@tonic-gate  *	profileName
760Sstevel@tonic-gate  *		Profile name to use for init (ldapclient) or
770Sstevel@tonic-gate  *		generate (gen_profile). (formerly -P)
780Sstevel@tonic-gate  *	profileTTL
790Sstevel@tonic-gate  *		Client info TTL.  If set to 0 this information will not be
800Sstevel@tonic-gate  *		automatically updated by the ldap_cachemgr(1M).
810Sstevel@tonic-gate  *		(formerly -e)
820Sstevel@tonic-gate  *	proxyDN
830Sstevel@tonic-gate  *		Binding DN.  Ex. cn=client,ou=people,cd=eng,dc=sun,dc=com
840Sstevel@tonic-gate  *		(formerly -D)
850Sstevel@tonic-gate  *	proxyPassword
860Sstevel@tonic-gate  *		Client password not needed for authentication "none".
870Sstevel@tonic-gate  *		(formerly -w)
888821SMichen.Chang@Sun.COM  *	adminDN
898821SMichen.Chang@Sun.COM  *		Administrator DN for updating naming data.
908821SMichen.Chang@Sun.COM  *	adminPassword
918821SMichen.Chang@Sun.COM  *		Administrator password
928821SMichen.Chang@Sun.COM  *	enableShadowUpdate
938821SMichen.Chang@Sun.COM  *		Allow Administrator to change shadow data in LDAP
940Sstevel@tonic-gate  *	searchTimeLimit
950Sstevel@tonic-gate  *		Timeout value. (formerly -o)
960Sstevel@tonic-gate  *	serviceSearchDescriptor
970Sstevel@tonic-gate  *		Service search scope. (no former option)
980Sstevel@tonic-gate  *	serviceAuthenticationMethod
990Sstevel@tonic-gate  *		Service authenticaion method (no former option)
1000Sstevel@tonic-gate  *	serviceCredentialLevel
1010Sstevel@tonic-gate  *		Service credential level (no former option)
1020Sstevel@tonic-gate  *
1030Sstevel@tonic-gate  */
1040Sstevel@tonic-gate 
1050Sstevel@tonic-gate #include <stdlib.h>
1060Sstevel@tonic-gate #include <stdio.h>
1070Sstevel@tonic-gate #include <unistd.h>
1080Sstevel@tonic-gate #include <errno.h>
1090Sstevel@tonic-gate #include <sys/types.h>
1100Sstevel@tonic-gate #include <time.h>
1110Sstevel@tonic-gate #include <sys/param.h>
1120Sstevel@tonic-gate #include <sys/stat.h>
1130Sstevel@tonic-gate #include <sys/systeminfo.h>
1140Sstevel@tonic-gate #include <fcntl.h>
1150Sstevel@tonic-gate #include <xti.h>
1160Sstevel@tonic-gate #include <strings.h>
1170Sstevel@tonic-gate #include <limits.h>
1180Sstevel@tonic-gate #include <locale.h>
1190Sstevel@tonic-gate #include <syslog.h>
1200Sstevel@tonic-gate #include <libscf.h>
1210Sstevel@tonic-gate #include <assert.h>
1226842Sth160488 
1236842Sth160488 #include "standalone.h"
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
1260Sstevel@tonic-gate #define	TEXT_DOMAIN "SUNW_OST_OSCMD"
1270Sstevel@tonic-gate #endif
1280Sstevel@tonic-gate 
1290Sstevel@tonic-gate /* error codes */
1300Sstevel@tonic-gate /* The manpage doc only allows for SUCCESS(0), FAIL(1) and CRED(2) on exit */
1310Sstevel@tonic-gate #define	CLIENT_SUCCESS		0
1320Sstevel@tonic-gate #define	CLIENT_ERR_PARSE	-1
1330Sstevel@tonic-gate #define	CLIENT_ERR_FAIL		1
1340Sstevel@tonic-gate #define	CLIENT_ERR_CREDENTIAL	2
1350Sstevel@tonic-gate #define	CLIENT_ERR_MEMORY	3
1360Sstevel@tonic-gate #define	CLIENT_ERR_RESTORE	4
1370Sstevel@tonic-gate #define	CLIENT_ERR_RENAME	5
1380Sstevel@tonic-gate #define	CLIENT_ERR_RECOVER	6
1390Sstevel@tonic-gate #define	CLIENT_ERR_TIMEDOUT	7
1400Sstevel@tonic-gate #define	CLIENT_ERR_MAINTENANCE	8
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate /* Reset flag for start_services() */
1430Sstevel@tonic-gate #define	START_INIT	1
1440Sstevel@tonic-gate #define	START_RESET	2
1450Sstevel@tonic-gate #define	START_UNINIT	3
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate /* Reset flag for stop_services() */
1480Sstevel@tonic-gate #define	STATE_NOSAVE	0
1490Sstevel@tonic-gate #define	STATE_SAVE	1
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate /* files to (possibiliy) restore */
1520Sstevel@tonic-gate #define	LDAP_RESTORE_DIR	"/var/ldap/restore"
1530Sstevel@tonic-gate 
1540Sstevel@tonic-gate #define	DOMAINNAME_DIR		"/etc"
1550Sstevel@tonic-gate #define	DOMAINNAME_FILE		"defaultdomain"
1560Sstevel@tonic-gate #define	DOMAINNAME		DOMAINNAME_DIR "/" DOMAINNAME_FILE
1570Sstevel@tonic-gate #define	DOMAINNAME_BACK		LDAP_RESTORE_DIR "/" DOMAINNAME_FILE
1580Sstevel@tonic-gate 
1590Sstevel@tonic-gate #define	NSSWITCH_DIR		"/etc"
1600Sstevel@tonic-gate #define	NSSWITCH_FILE		"nsswitch.conf"
1610Sstevel@tonic-gate #define	NSSWITCH_CONF		NSSWITCH_DIR "/" NSSWITCH_FILE
1620Sstevel@tonic-gate #define	NSSWITCH_BACK		LDAP_RESTORE_DIR "/" NSSWITCH_FILE
1630Sstevel@tonic-gate #define	NSSWITCH_LDAP		"/etc/nsswitch.ldap"
1640Sstevel@tonic-gate 
1650Sstevel@tonic-gate #define	YP_BIND_DIR		"/var/yp/binding"
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate /* Define the service FMRIs */
1680Sstevel@tonic-gate #define	SENDMAIL_FMRI		"network/smtp:sendmail"
1690Sstevel@tonic-gate #define	NSCD_FMRI		"system/name-service-cache:default"
1700Sstevel@tonic-gate #define	AUTOFS_FMRI		"system/filesystem/autofs:default"
1710Sstevel@tonic-gate #define	LDAP_FMRI		"network/ldap/client:default"
1720Sstevel@tonic-gate #define	YP_FMRI			"network/nis/client:default"
1730Sstevel@tonic-gate #define	NS_MILESTONE_FMRI	"milestone/name-services:default"
1740Sstevel@tonic-gate 
1750Sstevel@tonic-gate /* Define flags for checking if services were enabled */
1760Sstevel@tonic-gate #define	SENDMAIL_ON	0x1
1770Sstevel@tonic-gate #define	NSCD_ON		0x10
1780Sstevel@tonic-gate #define	AUTOFS_ON	0x100
1790Sstevel@tonic-gate 
1800Sstevel@tonic-gate #define	CMD_DOMAIN_START	"/usr/bin/domainname"
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate /* Command to copy files */
1830Sstevel@tonic-gate #define	CMD_CP			"/bin/cp -f"
1840Sstevel@tonic-gate #define	CMD_MV			"/bin/mv -f"
1850Sstevel@tonic-gate #define	CMD_RM			"/bin/rm -f"
1860Sstevel@tonic-gate 
1870Sstevel@tonic-gate #define	TO_DEV_NULL		" >/dev/null 2>&1"
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate /* Files that need to be just removed */
1900Sstevel@tonic-gate #define	LDAP_CACHE_LOG		"/var/ldap/cachemgr.log"
1910Sstevel@tonic-gate 
1920Sstevel@tonic-gate /* Output defines to supress if quiet mode set */
1930Sstevel@tonic-gate #define	CLIENT_FPUTS if (!mode_quiet) (void) fputs
1940Sstevel@tonic-gate #define	CLIENT_FPRINTF if (!mode_quiet) (void) fprintf
1950Sstevel@tonic-gate #define	CLIENT_FPUTC if (!mode_quiet) (void) fputc
1960Sstevel@tonic-gate 
1970Sstevel@tonic-gate #define	restart_service(fmri, waitflag)\
1980Sstevel@tonic-gate 		do_service(fmri, waitflag, RESTART_SERVICE,\
1990Sstevel@tonic-gate 		SCF_STATE_STRING_ONLINE)
2000Sstevel@tonic-gate #define	start_service(fmri, waitflag)	\
2010Sstevel@tonic-gate 		do_service(fmri, waitflag, START_SERVICE,\
2020Sstevel@tonic-gate 		SCF_STATE_STRING_ONLINE)
2030Sstevel@tonic-gate #define	disable_service(fmri, waitflag)	\
2040Sstevel@tonic-gate 		do_service(fmri, waitflag, STOP_SERVICE,\
2050Sstevel@tonic-gate 		SCF_STATE_STRING_DISABLED)
2060Sstevel@tonic-gate 
2070Sstevel@tonic-gate /*
2080Sstevel@tonic-gate  * There isn't a domainName defined as a param, so we set a value here
2090Sstevel@tonic-gate  * (1001) should be big enough
2100Sstevel@tonic-gate  */
2110Sstevel@tonic-gate #define	LOCAL_DOMAIN_P 1001
2120Sstevel@tonic-gate 
2130Sstevel@tonic-gate #define	START_SERVICE	1
2140Sstevel@tonic-gate #define	STOP_SERVICE	2
2150Sstevel@tonic-gate #define	RESTART_SERVICE	3
2160Sstevel@tonic-gate 
2170Sstevel@tonic-gate #define	DEFAULT_TIMEOUT	60000000
2180Sstevel@tonic-gate 
2190Sstevel@tonic-gate #define	INIT_WAIT_USECS	50000
2200Sstevel@tonic-gate 
2210Sstevel@tonic-gate /* Used to turn off profile checking */
2220Sstevel@tonic-gate #define	CACHETTL_OFF "0"
2230Sstevel@tonic-gate 
2240Sstevel@tonic-gate /* Globals */
2250Sstevel@tonic-gate static char *cmd;
2260Sstevel@tonic-gate 
2270Sstevel@tonic-gate static char *dname = NULL;
2280Sstevel@tonic-gate static char dname_buf[BUFSIZ];
2290Sstevel@tonic-gate 
2300Sstevel@tonic-gate static boolean_t sysid_install = B_FALSE;
2310Sstevel@tonic-gate 
2320Sstevel@tonic-gate static int mode_verbose = 0;
2330Sstevel@tonic-gate static int mode_quiet = 0;
2340Sstevel@tonic-gate static int gen = 0;
2350Sstevel@tonic-gate 
2360Sstevel@tonic-gate static int gStartLdap = 0;
2370Sstevel@tonic-gate static int gStartYp = 0;
2380Sstevel@tonic-gate 
2390Sstevel@tonic-gate static int enableFlag = 0;
2400Sstevel@tonic-gate 
2410Sstevel@tonic-gate /* multival_t is used to hold params that can have more than one value */
2420Sstevel@tonic-gate typedef struct {
2430Sstevel@tonic-gate 	int count;
2440Sstevel@tonic-gate 	char **optlist;
2450Sstevel@tonic-gate } multival_t;
2460Sstevel@tonic-gate 
2470Sstevel@tonic-gate static multival_t *multival_new();
2480Sstevel@tonic-gate static int multival_add(multival_t *list, char *opt);
2490Sstevel@tonic-gate static void multival_free(multival_t *list);
2500Sstevel@tonic-gate 
2510Sstevel@tonic-gate /*
2520Sstevel@tonic-gate  * clientopts_t is used to hold and pass around the param values from
2530Sstevel@tonic-gate  * the cmd line
2540Sstevel@tonic-gate  */
2550Sstevel@tonic-gate typedef struct {
2560Sstevel@tonic-gate 	multival_t	*attributeMap;
2570Sstevel@tonic-gate 	char		*authenticationMethod;
2580Sstevel@tonic-gate 	char		*bindTimeLimit;
2590Sstevel@tonic-gate 	char		*certificatePath;
2600Sstevel@tonic-gate 	char		*credentialLevel;
2610Sstevel@tonic-gate 	char		*defaultSearchBase;
2620Sstevel@tonic-gate 	char		*defaultServerList;
2630Sstevel@tonic-gate 	char		*domainName;
2640Sstevel@tonic-gate 	char		*followReferrals;
2650Sstevel@tonic-gate 	multival_t	*objectclassMap;
2660Sstevel@tonic-gate 	char		*preferredServerList;
2670Sstevel@tonic-gate 	char		*profileName;
2680Sstevel@tonic-gate 	char		*profileTTL;
2690Sstevel@tonic-gate 	char		*proxyDN;
2700Sstevel@tonic-gate 	char		*proxyPassword;
2718821SMichen.Chang@Sun.COM 	char		*enableShadowUpdate;
2728821SMichen.Chang@Sun.COM 	char		*adminDN;
2738821SMichen.Chang@Sun.COM 	char		*adminPassword;
2746842Sth160488 	char		*bindDN;
2756842Sth160488 	char		*bindPasswd;
2760Sstevel@tonic-gate 	char		*defaultSearchScope;
2770Sstevel@tonic-gate 	char		*searchTimeLimit;
2780Sstevel@tonic-gate 	multival_t	*serviceAuthenticationMethod;
2790Sstevel@tonic-gate 	multival_t	*serviceCredentialLevel;
2800Sstevel@tonic-gate 	multival_t	*serviceSearchDescriptor;
2810Sstevel@tonic-gate } clientopts_t;
2820Sstevel@tonic-gate 
2830Sstevel@tonic-gate static clientopts_t *clientopts_new();
2840Sstevel@tonic-gate static void clientopts_free(clientopts_t *list);
2850Sstevel@tonic-gate 
2860Sstevel@tonic-gate extern ns_ldap_error_t *__ns_ldap_print_config(int);
2870Sstevel@tonic-gate extern void __ns_ldap_default_config();
2882830Sdjl extern int __ns_ldap_download(const char *, char *, char *, ns_ldap_error_t **);
2890Sstevel@tonic-gate 
2900Sstevel@tonic-gate /* Function prototypes (these could be static) */
2910Sstevel@tonic-gate static void usage(void);
2920Sstevel@tonic-gate 
2930Sstevel@tonic-gate static int credCheck(clientopts_t *arglist);
2948821SMichen.Chang@Sun.COM static int adminCredCheck(clientopts_t *arglist);
2950Sstevel@tonic-gate static int clientSetParam(clientopts_t *optlist, int paramFlag, char *attrVal);
2960Sstevel@tonic-gate static int parseParam(char *param, char **paramVal);
2970Sstevel@tonic-gate static void dumpargs(clientopts_t *arglist);
2980Sstevel@tonic-gate static int num_args(clientopts_t *arglist);
2990Sstevel@tonic-gate 
3000Sstevel@tonic-gate static int file_backup(void);
3010Sstevel@tonic-gate static int recover(int saveState);
3020Sstevel@tonic-gate static int mod_backup(void);
3030Sstevel@tonic-gate static int mod_recover(void);
3040Sstevel@tonic-gate static void mod_cleanup(void);
3050Sstevel@tonic-gate 
3060Sstevel@tonic-gate static int client_list(clientopts_t *arglist);
3070Sstevel@tonic-gate static int client_manual(clientopts_t *arglist);
3080Sstevel@tonic-gate static int client_mod(clientopts_t *arglist);
3090Sstevel@tonic-gate static int client_uninit(clientopts_t *arglist);
3100Sstevel@tonic-gate static int client_genProfile(clientopts_t *arglist);
3110Sstevel@tonic-gate static int client_init(clientopts_t *arglist);
3120Sstevel@tonic-gate static int file_move(const char *from, const char *to);
3130Sstevel@tonic-gate 
3140Sstevel@tonic-gate static int start_services(int flag);
3150Sstevel@tonic-gate static int stop_services(int saveState);
3160Sstevel@tonic-gate static boolean_t is_service(const char *fmri, const char *state);
3170Sstevel@tonic-gate static int wait_till(const char *fmri, const char *state, useconds_t max,
3180Sstevel@tonic-gate 		const char *what, boolean_t check_maint);
3190Sstevel@tonic-gate static int do_service(const char *fmri, boolean_t waitflag, int dowhat,
3200Sstevel@tonic-gate 		const char *state);
3210Sstevel@tonic-gate static useconds_t get_timeout_value(int dowhat, const char *fmri,
3220Sstevel@tonic-gate 		useconds_t default_val);
3230Sstevel@tonic-gate 
324702Sth160488 int
main(int argc,char ** argv)325702Sth160488 main(int argc, char **argv)
3260Sstevel@tonic-gate {
3276842Sth160488 	char		*ret_locale, *ret_textdomain;
3286842Sth160488 	int		retcode;
3296842Sth160488 	int		paramFlag;
3306842Sth160488 	char		*attrVal;
3316842Sth160488 	int		sysinfostatus;
3326842Sth160488 	clientopts_t	*optlist = NULL;
3336842Sth160488 	int		op_manual = 0, op_mod = 0, op_uninit = 0;
3346842Sth160488 	int		op_list = 0, op_init = 0, op_genprofile = 0;
3356842Sth160488 	extern char	*optarg;
3366842Sth160488 	extern int	optind;
3376842Sth160488 	int		option;
3380Sstevel@tonic-gate 
3390Sstevel@tonic-gate 	ret_locale = setlocale(LC_ALL, "");
3400Sstevel@tonic-gate 	if (ret_locale == NULL) {
3410Sstevel@tonic-gate 		CLIENT_FPUTS(gettext("Unable to set locale.\n"), stderr);
3420Sstevel@tonic-gate 	}
3430Sstevel@tonic-gate 	ret_textdomain = textdomain(TEXT_DOMAIN);
3440Sstevel@tonic-gate 	if (ret_textdomain == NULL) {
3450Sstevel@tonic-gate 		CLIENT_FPUTS(gettext("Unable to set textdomain.\n"), stderr);
3460Sstevel@tonic-gate 	}
3470Sstevel@tonic-gate 
3480Sstevel@tonic-gate 	openlog("ldapclient", LOG_PID, LOG_USER);
3490Sstevel@tonic-gate 
3500Sstevel@tonic-gate 	/* get name that invoked us */
3510Sstevel@tonic-gate 	if (cmd = strrchr(argv[0], '/'))
3520Sstevel@tonic-gate 		++cmd;
3530Sstevel@tonic-gate 	else
3540Sstevel@tonic-gate 		cmd = argv[0];
3550Sstevel@tonic-gate 
3560Sstevel@tonic-gate 	sysinfostatus = sysinfo(SI_SRPC_DOMAIN, dname_buf, BUFSIZ);
3570Sstevel@tonic-gate 	if (0 < sysinfostatus)
3580Sstevel@tonic-gate 		dname = &dname_buf[0];
3590Sstevel@tonic-gate 
3600Sstevel@tonic-gate 	optlist = clientopts_new();
3610Sstevel@tonic-gate 	if (optlist == NULL) {
3620Sstevel@tonic-gate 		CLIENT_FPUTS(
3636842Sth160488 		    gettext("Error getting optlist (malloc fail)\n"),
3646842Sth160488 		    stderr);
3650Sstevel@tonic-gate 		exit(CLIENT_ERR_FAIL);
3660Sstevel@tonic-gate 	}
3670Sstevel@tonic-gate 
3680Sstevel@tonic-gate 	optind = 1;
3690Sstevel@tonic-gate 	while (optind < argc) {
3708821SMichen.Chang@Sun.COM 		option = getopt(argc, argv, "vqa:ID:w:j:y:z:");
3710Sstevel@tonic-gate 
3720Sstevel@tonic-gate 		switch (option) {
3730Sstevel@tonic-gate 		case 'v':
3740Sstevel@tonic-gate 			mode_verbose = 1;
3750Sstevel@tonic-gate 			break;
3760Sstevel@tonic-gate 		case 'q':
3770Sstevel@tonic-gate 			mode_quiet = 1;
3780Sstevel@tonic-gate 			break;
3790Sstevel@tonic-gate 		case 'a':
3800Sstevel@tonic-gate 			attrVal = NULL;
3810Sstevel@tonic-gate 			paramFlag = parseParam(optarg, &attrVal);
3820Sstevel@tonic-gate 			if (paramFlag == CLIENT_ERR_PARSE) {
3830Sstevel@tonic-gate 				CLIENT_FPRINTF(stderr,
3846842Sth160488 				    gettext("Unrecognized "
3856842Sth160488 				    "parameter \"%s\"\n"),
3866842Sth160488 				    optarg);
3870Sstevel@tonic-gate 				usage();
3880Sstevel@tonic-gate 				exit(CLIENT_ERR_FAIL);
3890Sstevel@tonic-gate 			}
3906842Sth160488 			if (paramFlag == NS_LDAP_BINDPASSWD_P &&
3916842Sth160488 			    optlist->proxyPassword != NULL) {
3926842Sth160488 				(void) fprintf(stderr,
3936842Sth160488 				    gettext("The -a proxyPassword option is "
3946842Sth160488 				    "mutually exclusive of -y. "
3956842Sth160488 				    "-a proxyPassword is ignored.\n"));
3966842Sth160488 				break;
3976842Sth160488 			}
3988821SMichen.Chang@Sun.COM 			if (paramFlag == NS_LDAP_ADMIN_BINDPASSWD_P &&
3998821SMichen.Chang@Sun.COM 			    optlist->adminPassword != NULL) {
4008821SMichen.Chang@Sun.COM 				(void) fprintf(stderr,
4018821SMichen.Chang@Sun.COM 				    gettext("The -a adminPassword option is "
4028821SMichen.Chang@Sun.COM 				    "mutually exclusive of -z. "
4038821SMichen.Chang@Sun.COM 				    "-a adminPassword is ignored.\n"));
4048821SMichen.Chang@Sun.COM 				break;
4058821SMichen.Chang@Sun.COM 			}
4060Sstevel@tonic-gate 			retcode = clientSetParam(optlist, paramFlag, attrVal);
4070Sstevel@tonic-gate 			if (retcode != CLIENT_SUCCESS) {
4080Sstevel@tonic-gate 				CLIENT_FPRINTF(
4096842Sth160488 				    stderr,
4106842Sth160488 				    gettext("Error (%d) setting "
4116842Sth160488 				    "param \"%s\"\n"),
4126842Sth160488 				    retcode, optarg);
4130Sstevel@tonic-gate 				usage();
4140Sstevel@tonic-gate 				exit(CLIENT_ERR_FAIL);
4150Sstevel@tonic-gate 			}
4160Sstevel@tonic-gate 			break;
4176842Sth160488 		case 'D':
4186842Sth160488 			optlist->bindDN = strdup(optarg);
4196842Sth160488 			break;
4206842Sth160488 		case 'w':
4216842Sth160488 			if (optlist->bindPasswd != NULL) {
4226842Sth160488 				CLIENT_FPRINTF(stderr,
4236842Sth160488 				    gettext("The -w option is mutually "
4246842Sth160488 				    "exclusive of -j. -w is ignored."));
4256842Sth160488 				break;
4266842Sth160488 			}
4276842Sth160488 
4286842Sth160488 			if (optarg[0] == '-' && optarg[1] == '\0') {
4296842Sth160488 				/* Ask for a password later */
4306842Sth160488 				break;
4316842Sth160488 			}
4326842Sth160488 
4336842Sth160488 			optlist->bindPasswd = strdup(optarg);
4346842Sth160488 			break;
4356842Sth160488 		case 'j':
4366842Sth160488 			if (optlist->bindPasswd != NULL) {
4376842Sth160488 				(void) fprintf(stderr,
4386842Sth160488 				    gettext("The -w option is mutually "
4396842Sth160488 				    "exclusive of -j. -w is ignored.\n"));
4406842Sth160488 				free(optlist->bindPasswd);
4416842Sth160488 			}
4426842Sth160488 			optlist->bindPasswd = readPwd(optarg);
4436842Sth160488 			if (optlist->bindPasswd == NULL) {
4446842Sth160488 				exit(CLIENT_ERR_FAIL);
4456842Sth160488 			}
4466842Sth160488 			break;
4476842Sth160488 		case 'y':
4486842Sth160488 			if (optlist->proxyPassword != NULL) {
4496842Sth160488 				(void) fprintf(stderr,
4506842Sth160488 				    gettext("The -a proxyPassword option is "
4516842Sth160488 				    "mutually exclusive of -y. "
4526842Sth160488 				    "-a proxyPassword is ignored.\n"));
4536842Sth160488 			}
4546842Sth160488 			optlist->proxyPassword = readPwd(optarg);
4556842Sth160488 			if (optlist->proxyPassword == NULL) {
4566842Sth160488 				exit(CLIENT_ERR_FAIL);
4576842Sth160488 			}
4586842Sth160488 			break;
4598821SMichen.Chang@Sun.COM 		case 'z':
4608821SMichen.Chang@Sun.COM 			if (optlist->adminPassword != NULL) {
4618821SMichen.Chang@Sun.COM 				(void) fprintf(stderr,
4628821SMichen.Chang@Sun.COM 				    gettext("The -a adminPassword option is "
4638821SMichen.Chang@Sun.COM 				    "mutually exclusive of -z. "
4648821SMichen.Chang@Sun.COM 				    "-a adminPassword is ignored.\n"));
4658821SMichen.Chang@Sun.COM 			}
4668821SMichen.Chang@Sun.COM 			optlist->adminPassword = readPwd(optarg);
4678821SMichen.Chang@Sun.COM 			if (optlist->adminPassword == NULL) {
4688821SMichen.Chang@Sun.COM 				exit(CLIENT_ERR_FAIL);
4698821SMichen.Chang@Sun.COM 			}
4708821SMichen.Chang@Sun.COM 			break;
4710Sstevel@tonic-gate 		case EOF:
4720Sstevel@tonic-gate 			if (strcmp(argv[optind], "init") == 0) {
4730Sstevel@tonic-gate 				op_init = 1;
4740Sstevel@tonic-gate 			} else if (strcmp(argv[optind], "manual") == 0) {
4750Sstevel@tonic-gate 				op_manual = 1;
4760Sstevel@tonic-gate 			} else if (strcmp(argv[optind], "mod") == 0) {
4770Sstevel@tonic-gate 				op_mod = 1;
4780Sstevel@tonic-gate 			} else if (strcmp(argv[optind], "list") == 0) {
4790Sstevel@tonic-gate 				op_list = 1;
4800Sstevel@tonic-gate 			} else if (strcmp(argv[optind], "uninit") == 0) {
4810Sstevel@tonic-gate 				op_uninit = 1;
4820Sstevel@tonic-gate 			} else if (strcmp(argv[optind], "genprofile") == 0) {
4830Sstevel@tonic-gate 				gen = 1;
4840Sstevel@tonic-gate 				op_genprofile = 1;
4850Sstevel@tonic-gate 			} else if (optind == argc-1) {
4860Sstevel@tonic-gate 				retcode = clientSetParam(
4876842Sth160488 				    optlist,
4886842Sth160488 				    NS_LDAP_SERVERS_P,
4896842Sth160488 				    argv[optind]);	/* ipAddr */
4900Sstevel@tonic-gate 				if (retcode != CLIENT_SUCCESS) {
4910Sstevel@tonic-gate 					CLIENT_FPRINTF(
4926842Sth160488 					    stderr,
4936842Sth160488 					    gettext("Error (%d) setting "
4946842Sth160488 					    "serverList param.\n"),
4956842Sth160488 					    retcode);
4960Sstevel@tonic-gate 					usage();
4970Sstevel@tonic-gate 					exit(CLIENT_ERR_FAIL);
4980Sstevel@tonic-gate 				}
4990Sstevel@tonic-gate 			} else {
5000Sstevel@tonic-gate 				CLIENT_FPUTS(
5016842Sth160488 				    gettext("Error parsing "
5026842Sth160488 				    "command line\n"),
5036842Sth160488 				    stderr);
5040Sstevel@tonic-gate 				usage();
5050Sstevel@tonic-gate 				exit(CLIENT_ERR_FAIL);
5060Sstevel@tonic-gate 			}
5070Sstevel@tonic-gate 			optind++;	/* get past the verb and keep trying */
5080Sstevel@tonic-gate 			break;
5090Sstevel@tonic-gate 		/* Backwards compatibility to support system install */
5100Sstevel@tonic-gate 		case 'I':
5110Sstevel@tonic-gate 			sysid_install = B_TRUE;
5120Sstevel@tonic-gate 			op_init = 1;
5130Sstevel@tonic-gate 			mode_quiet = 1;
5140Sstevel@tonic-gate 			break;
5150Sstevel@tonic-gate 		case '?':
5160Sstevel@tonic-gate 			usage();
5170Sstevel@tonic-gate 			CLIENT_FPUTS(gettext("\nOr\n\n"), stderr);
5180Sstevel@tonic-gate 			gen = 1;
5190Sstevel@tonic-gate 			usage();
5200Sstevel@tonic-gate 			exit(CLIENT_ERR_FAIL);
5210Sstevel@tonic-gate 			break;
5220Sstevel@tonic-gate 		}
5230Sstevel@tonic-gate 
5240Sstevel@tonic-gate 	}
5250Sstevel@tonic-gate 
5260Sstevel@tonic-gate 	if ((getuid() != 0) && (!op_genprofile)) {
5270Sstevel@tonic-gate 		(void) puts(
5286842Sth160488 		    "You must be root (SuperUser) to run this command.");
5290Sstevel@tonic-gate 		usage();
5300Sstevel@tonic-gate 		exit(CLIENT_ERR_FAIL);
5310Sstevel@tonic-gate 	}
5320Sstevel@tonic-gate 
5330Sstevel@tonic-gate /*
5340Sstevel@tonic-gate  *	All command line arguments are finished being parsed now
5350Sstevel@tonic-gate  */
5360Sstevel@tonic-gate 
5370Sstevel@tonic-gate /* *** Do semantic checking here *** */
5380Sstevel@tonic-gate 
5390Sstevel@tonic-gate /* if gen and no no searchBase then err */
5400Sstevel@tonic-gate 	if (gen && !optlist->defaultSearchBase) {
5410Sstevel@tonic-gate 		CLIENT_FPUTS(
5426842Sth160488 		    gettext("ldapclient: Missing required attrName "
5436842Sth160488 		    "defaultSearchBase\n"),
5446842Sth160488 		    stderr);
5450Sstevel@tonic-gate 		usage();
5460Sstevel@tonic-gate 		clientopts_free(optlist);
5470Sstevel@tonic-gate 		exit(CLIENT_ERR_FAIL);
5480Sstevel@tonic-gate 	}
5490Sstevel@tonic-gate 
5508821SMichen.Chang@Sun.COM /*
5518821SMichen.Chang@Sun.COM  * if init or manual, and if adminDN is specified then enableShadowUpdate
5528821SMichen.Chang@Sun.COM  * must be set to TRUE.
5538821SMichen.Chang@Sun.COM  */
5548821SMichen.Chang@Sun.COM 	if ((op_init || op_manual) &&
5558821SMichen.Chang@Sun.COM 	    (!optlist->enableShadowUpdate ||
5568821SMichen.Chang@Sun.COM 	    strcasecmp(optlist->enableShadowUpdate, "TRUE") != 0) &&
5578821SMichen.Chang@Sun.COM 	    (optlist->adminDN || optlist->adminPassword)) {
5588821SMichen.Chang@Sun.COM 		CLIENT_FPUTS(
5598821SMichen.Chang@Sun.COM 		    gettext("ldapclient: adminDN and adminPassword must not "
5608821SMichen.Chang@Sun.COM 		    "be specified if enableShadowUpdate is not set to TRUE \n"),
5618821SMichen.Chang@Sun.COM 		    stderr);
5628821SMichen.Chang@Sun.COM 		usage();
5638821SMichen.Chang@Sun.COM 		clientopts_free(optlist);
5648821SMichen.Chang@Sun.COM 		exit(CLIENT_ERR_FAIL);
5658821SMichen.Chang@Sun.COM 	}
5668821SMichen.Chang@Sun.COM 
5670Sstevel@tonic-gate /* Only one verb can be specified */
5680Sstevel@tonic-gate 	if ((op_init + op_manual + op_mod + op_uninit +
5696842Sth160488 	    op_list + op_genprofile) != 1) {
5700Sstevel@tonic-gate 		usage();
5710Sstevel@tonic-gate 		clientopts_free(optlist);
5720Sstevel@tonic-gate 		exit(CLIENT_ERR_FAIL);
5730Sstevel@tonic-gate 	}
5740Sstevel@tonic-gate 
5750Sstevel@tonic-gate /* *** We passed semantic checking, so now do the operation *** */
5760Sstevel@tonic-gate 
5770Sstevel@tonic-gate 	if (mode_verbose) {
5780Sstevel@tonic-gate 		CLIENT_FPUTS(gettext("Arguments parsed:\n"), stderr);
5790Sstevel@tonic-gate 		dumpargs(optlist);
5800Sstevel@tonic-gate 	}
5810Sstevel@tonic-gate 
5820Sstevel@tonic-gate 
5830Sstevel@tonic-gate /* handle "ldapclient list" here.  err checking done in func */
5840Sstevel@tonic-gate 	if (op_list) {
5850Sstevel@tonic-gate 		if (mode_verbose)
5860Sstevel@tonic-gate 			CLIENT_FPUTS(
5876842Sth160488 			    gettext("Handling list option\n"),
5886842Sth160488 			    stderr);
5890Sstevel@tonic-gate 		retcode = client_list(optlist);
5900Sstevel@tonic-gate 	}
5910Sstevel@tonic-gate 
5920Sstevel@tonic-gate /* handle "ldapclient uninit" here */
5930Sstevel@tonic-gate 	if (op_uninit) {
5940Sstevel@tonic-gate 		if (mode_verbose)
5950Sstevel@tonic-gate 			CLIENT_FPUTS(
5966842Sth160488 			    gettext("Handling uninit option\n"),
5976842Sth160488 			    stderr);
5980Sstevel@tonic-gate 		retcode = client_uninit(optlist);
5990Sstevel@tonic-gate 	}
6000Sstevel@tonic-gate 
6010Sstevel@tonic-gate /* handle "ldapclient init" (profile) */
6020Sstevel@tonic-gate 	if (op_init) {
6030Sstevel@tonic-gate 		if (mode_verbose)
6040Sstevel@tonic-gate 			CLIENT_FPUTS(
6056842Sth160488 			    gettext("Handling init option\n"),
6066842Sth160488 			    stderr);
6070Sstevel@tonic-gate 		retcode = client_init(optlist);
6080Sstevel@tonic-gate 	}
6090Sstevel@tonic-gate 
6100Sstevel@tonic-gate /* handle "genprofile" here */
6110Sstevel@tonic-gate 	if (op_genprofile) {
6120Sstevel@tonic-gate 		if (mode_verbose)
6130Sstevel@tonic-gate 			CLIENT_FPUTS(
6146842Sth160488 			    gettext("Handling genProfile\n"),
6156842Sth160488 			    stderr);
6160Sstevel@tonic-gate 		retcode = client_genProfile(optlist);
6170Sstevel@tonic-gate 	}
6180Sstevel@tonic-gate 
6190Sstevel@tonic-gate /* handle "ldapclient manual" here */
6200Sstevel@tonic-gate 	if (op_manual) {
6210Sstevel@tonic-gate 		if (mode_verbose)
6220Sstevel@tonic-gate 			CLIENT_FPUTS(
6236842Sth160488 			    gettext("Handling manual option\n"),
6246842Sth160488 			    stderr);
6250Sstevel@tonic-gate 		retcode = client_manual(optlist);
6260Sstevel@tonic-gate 	}
6270Sstevel@tonic-gate 
6280Sstevel@tonic-gate /* handle "ldapclient mod" here */
6290Sstevel@tonic-gate 	if (op_mod) {
6300Sstevel@tonic-gate 		if (mode_verbose)
6310Sstevel@tonic-gate 			CLIENT_FPUTS(
6326842Sth160488 			    gettext("Handling mod option\n"),
6336842Sth160488 			    stderr);
6340Sstevel@tonic-gate 		retcode = client_mod(optlist);
6350Sstevel@tonic-gate 	}
6360Sstevel@tonic-gate 
6370Sstevel@tonic-gate 	clientopts_free(optlist);
6380Sstevel@tonic-gate 	if ((retcode == CLIENT_SUCCESS) ||
6396842Sth160488 	    (retcode == CLIENT_ERR_FAIL) ||
6406842Sth160488 	    (retcode == CLIENT_ERR_CREDENTIAL))
6412830Sdjl 		return (retcode);
6420Sstevel@tonic-gate 	else
6432830Sdjl 		return (CLIENT_ERR_FAIL);
6440Sstevel@tonic-gate }
6450Sstevel@tonic-gate 
6460Sstevel@tonic-gate static int
client_list(clientopts_t * arglist)6470Sstevel@tonic-gate client_list(clientopts_t *arglist)
6480Sstevel@tonic-gate {
6490Sstevel@tonic-gate 	ns_ldap_error_t *errorp;
6500Sstevel@tonic-gate 	int retcode = CLIENT_SUCCESS;
6510Sstevel@tonic-gate 
6520Sstevel@tonic-gate 	if (num_args(arglist) > 0) {
6530Sstevel@tonic-gate 		CLIENT_FPUTS(
6546842Sth160488 		    gettext("No args supported with \"list\" option\n"),
6556842Sth160488 		    stderr);
6560Sstevel@tonic-gate 		usage();
6570Sstevel@tonic-gate 		return (CLIENT_ERR_FAIL);	/* exit code here ? */
6580Sstevel@tonic-gate 	}
6590Sstevel@tonic-gate 	if ((errorp = __ns_ldap_print_config(mode_verbose)) != NULL) {
6600Sstevel@tonic-gate 		retcode = CLIENT_ERR_FAIL;
6610Sstevel@tonic-gate 		CLIENT_FPUTS(
6626842Sth160488 		    gettext("Cannot get print configuration\n"),
6636842Sth160488 		    stderr);
6640Sstevel@tonic-gate 		CLIENT_FPUTS(errorp->message, stderr);
6650Sstevel@tonic-gate 		(void) __ns_ldap_freeError(&errorp);
6660Sstevel@tonic-gate 		CLIENT_FPUTC('\n', stderr);
6670Sstevel@tonic-gate 	}
6680Sstevel@tonic-gate 
6690Sstevel@tonic-gate 	return (retcode);
6700Sstevel@tonic-gate }
6710Sstevel@tonic-gate 
6720Sstevel@tonic-gate static int
client_uninit(clientopts_t * arglist)6730Sstevel@tonic-gate client_uninit(clientopts_t *arglist)
6740Sstevel@tonic-gate {
6750Sstevel@tonic-gate 	int retcode = CLIENT_SUCCESS;
6762830Sdjl 	ns_ldap_self_gssapi_config_t config = NS_LDAP_SELF_GSSAPI_CONFIG_NONE;
6770Sstevel@tonic-gate 
6780Sstevel@tonic-gate 	if (mode_verbose) {
6790Sstevel@tonic-gate 		CLIENT_FPUTS(
6806842Sth160488 		    gettext("Restoring machine to previous "
6816842Sth160488 		    "configuration state\n"),
6826842Sth160488 		    stderr);
6830Sstevel@tonic-gate 	}
6840Sstevel@tonic-gate 
6850Sstevel@tonic-gate 	if (num_args(arglist) > 0) {
6860Sstevel@tonic-gate 		CLIENT_FPUTS(
6876842Sth160488 		    gettext("No args supported with \"uninit\" option\n"),
6886842Sth160488 		    stderr);
6890Sstevel@tonic-gate 		usage();
6900Sstevel@tonic-gate 		return (CLIENT_ERR_FAIL);
6910Sstevel@tonic-gate 	}
6920Sstevel@tonic-gate 
6932830Sdjl 	(void) __ns_ldap_self_gssapi_config(&config);
6942830Sdjl 
6950Sstevel@tonic-gate 	retcode = stop_services(STATE_SAVE);
6962830Sdjl 
6972830Sdjl 	if (config != NS_LDAP_SELF_GSSAPI_CONFIG_NONE)
6982830Sdjl 		(void) system("/usr/sbin/cryptoadm enable metaslot");
6992830Sdjl 
7000Sstevel@tonic-gate 	if (retcode != CLIENT_SUCCESS) {
7010Sstevel@tonic-gate 		CLIENT_FPUTS(
7026842Sth160488 		    gettext("Errors stopping network services.\n"), stderr);
7030Sstevel@tonic-gate 		/* restart whatever services we can */
7040Sstevel@tonic-gate 		(void) start_services(START_RESET);
7050Sstevel@tonic-gate 		return (CLIENT_ERR_FAIL);
7060Sstevel@tonic-gate 	}
7070Sstevel@tonic-gate 
7080Sstevel@tonic-gate 	retcode = recover(STATE_SAVE);
7090Sstevel@tonic-gate 	if (retcode != CLIENT_SUCCESS) {
7100Sstevel@tonic-gate 		CLIENT_FPUTS(
7116842Sth160488 		    gettext("Cannot recover the configuration on "
7126842Sth160488 		    "this machine.\n"),
7136842Sth160488 		    stderr);
7140Sstevel@tonic-gate 		(void) start_services(START_RESET);
7150Sstevel@tonic-gate 	} else {
7160Sstevel@tonic-gate 		retcode = start_services(START_UNINIT);
7170Sstevel@tonic-gate 		if (retcode != CLIENT_SUCCESS) {
7180Sstevel@tonic-gate 			CLIENT_FPUTS(
7196842Sth160488 			    gettext("Config restored but problems "
7206842Sth160488 			    "encountered resetting network "
7216842Sth160488 			    "services.\n"),
7226842Sth160488 			    stderr);
7230Sstevel@tonic-gate 		}
7240Sstevel@tonic-gate 	}
7250Sstevel@tonic-gate 
7260Sstevel@tonic-gate 	if (retcode == CLIENT_SUCCESS) {
7270Sstevel@tonic-gate 		CLIENT_FPUTS(
7286842Sth160488 		    gettext("System successfully recovered\n"),
7296842Sth160488 		    stderr);
7300Sstevel@tonic-gate 	}
7310Sstevel@tonic-gate 
7320Sstevel@tonic-gate 	return (retcode);
7330Sstevel@tonic-gate }
7340Sstevel@tonic-gate 
7350Sstevel@tonic-gate /*
7360Sstevel@tonic-gate  * The following macro is used to do a __ns_ldap_setParam().
7370Sstevel@tonic-gate  * On every call, the return code is checked, and if there was
7380Sstevel@tonic-gate  * a problem then the error message is printed, the ldaperr
7390Sstevel@tonic-gate  * is freed and we return from the function with the offending
7400Sstevel@tonic-gate  * error return code.  This macro keeps us from having to
7410Sstevel@tonic-gate  * repeat this code for every call to setParam as was done
7420Sstevel@tonic-gate  * in the previous incarnation of ldapclient.
7430Sstevel@tonic-gate  *
7440Sstevel@tonic-gate  * assumes a "retcode" variable is available for status
7450Sstevel@tonic-gate  */
7460Sstevel@tonic-gate #define	LDAP_SET_PARAM(argval, argdef)	\
7470Sstevel@tonic-gate retcode = 0;	\
7480Sstevel@tonic-gate if (NULL != argval) {	\
7490Sstevel@tonic-gate 	ns_ldap_error_t *ldaperr;	\
7500Sstevel@tonic-gate 	retcode = __ns_ldap_setParam(argdef, (void *)argval, &ldaperr);	\
7510Sstevel@tonic-gate 	if (retcode != NS_LDAP_SUCCESS) {	\
7520Sstevel@tonic-gate 		if (NULL != ldaperr) {	\
7530Sstevel@tonic-gate 			CLIENT_FPUTS(ldaperr->message, stderr);	\
7540Sstevel@tonic-gate 			CLIENT_FPUTC('\n', stderr);	\
7550Sstevel@tonic-gate 			(void) __ns_ldap_freeError(&ldaperr);	\
7560Sstevel@tonic-gate 		}	\
7570Sstevel@tonic-gate 		return (retcode ? CLIENT_ERR_FAIL : CLIENT_SUCCESS);	\
7580Sstevel@tonic-gate 	}	\
7590Sstevel@tonic-gate }
7600Sstevel@tonic-gate 
7616842Sth160488 /*
7626842Sth160488  * The following macro is used to check if an arg has already been set
7636842Sth160488  * and issues an error message, a usage message and then returns an error.
7646842Sth160488  * This was made into a macro to avoid the duplication of this code many
7656842Sth160488  * times in the function below.
7666842Sth160488  */
7676842Sth160488 #define	LDAP_CHECK_INVALID(arg, param)	\
7686842Sth160488 if (arg) {	\
7696842Sth160488 	CLIENT_FPRINTF(stderr, gettext("Invalid parameter (%s) " \
7706842Sth160488 	    "specified\n"), param);	\
7716842Sth160488 	usage();	\
7726842Sth160488 	return (CLIENT_ERR_FAIL);	\
7736842Sth160488 }
7746842Sth160488 
7750Sstevel@tonic-gate static int
client_manual(clientopts_t * arglist)7760Sstevel@tonic-gate client_manual(clientopts_t *arglist)
7770Sstevel@tonic-gate {
7780Sstevel@tonic-gate 	int counter;
7790Sstevel@tonic-gate 	int domain_fp;
7800Sstevel@tonic-gate 	ns_ldap_error_t *errorp;
7810Sstevel@tonic-gate 	int ret_copy;
7820Sstevel@tonic-gate 	int reset_ret;
7830Sstevel@tonic-gate 	int retcode = CLIENT_SUCCESS;
7840Sstevel@tonic-gate 
7850Sstevel@tonic-gate 	if (dname == NULL) {
7860Sstevel@tonic-gate 		CLIENT_FPUTS(
7876842Sth160488 		    gettext("Manual failed: System domain not set and "
7886842Sth160488 		    "no domainName specified.\n"),
7896842Sth160488 		    stderr);
7900Sstevel@tonic-gate 		return (CLIENT_ERR_FAIL);
7910Sstevel@tonic-gate 	}
7920Sstevel@tonic-gate 
7930Sstevel@tonic-gate 	if (arglist->defaultSearchBase == NULL) {
7940Sstevel@tonic-gate 		CLIENT_FPUTS(
7956842Sth160488 		    gettext("Manual failed: Missing required "
7966842Sth160488 		    "defaultSearchBase attribute.\n"),
7976842Sth160488 		    stderr);
7980Sstevel@tonic-gate 		return (CLIENT_ERR_FAIL);
7990Sstevel@tonic-gate 	}
8000Sstevel@tonic-gate 
8010Sstevel@tonic-gate 	if ((arglist->defaultServerList == NULL) &&
8026842Sth160488 	    (arglist->preferredServerList == NULL)) {
8030Sstevel@tonic-gate 		CLIENT_FPUTS(
8046842Sth160488 		    gettext("Manual failed: Missing required "
8056842Sth160488 		    "defaultServerList or preferredServerList "
8066842Sth160488 		    "attribute.\n"),
8076842Sth160488 		    stderr);
8080Sstevel@tonic-gate 		return (CLIENT_ERR_FAIL);
8090Sstevel@tonic-gate 	}
8100Sstevel@tonic-gate 
8110Sstevel@tonic-gate 	if (arglist->profileTTL != NULL) {
8120Sstevel@tonic-gate 		CLIENT_FPUTS(
8136842Sth160488 		    gettext("Manual aborted: profileTTL is not supported "
8146842Sth160488 		    "in manual mode.\n"),
8156842Sth160488 		    stderr);
8160Sstevel@tonic-gate 		return (CLIENT_ERR_FAIL);
8170Sstevel@tonic-gate 	}
8180Sstevel@tonic-gate 
8190Sstevel@tonic-gate 	if (arglist->profileName != NULL) {
8200Sstevel@tonic-gate 		CLIENT_FPUTS(
8216842Sth160488 		    gettext("Manual aborted: profileName is not supported "
8226842Sth160488 		    "in manual mode.\n"),
8236842Sth160488 		    stderr);
8240Sstevel@tonic-gate 		return (CLIENT_ERR_FAIL);
8250Sstevel@tonic-gate 	}
8260Sstevel@tonic-gate 
8276842Sth160488 	LDAP_CHECK_INVALID(arglist->bindDN, "bind DN");
8286842Sth160488 	LDAP_CHECK_INVALID(arglist->bindPasswd, "bind password");
8290Sstevel@tonic-gate 
8300Sstevel@tonic-gate 	__ns_ldap_setServer(TRUE);	/* Need this for _ns_setParam() */
8310Sstevel@tonic-gate 	__ns_ldap_default_config();
8320Sstevel@tonic-gate 
8330Sstevel@tonic-gate 	/* Set version to latest (not version 1) */
8340Sstevel@tonic-gate 	LDAP_SET_PARAM(NS_LDAP_VERSION, NS_LDAP_FILE_VERSION_P);
8350Sstevel@tonic-gate 
8360Sstevel@tonic-gate 	/* Set profileTTL to 0 since NO profile on manual */
8370Sstevel@tonic-gate 	LDAP_SET_PARAM(CACHETTL_OFF, NS_LDAP_CACHETTL_P);
8380Sstevel@tonic-gate 
8390Sstevel@tonic-gate 	/* Set additional valid params from command line */
8400Sstevel@tonic-gate 	LDAP_SET_PARAM(arglist->authenticationMethod, NS_LDAP_AUTH_P);
8410Sstevel@tonic-gate 	LDAP_SET_PARAM(arglist->defaultSearchBase, NS_LDAP_SEARCH_BASEDN_P);
8420Sstevel@tonic-gate 	LDAP_SET_PARAM(arglist->credentialLevel, NS_LDAP_CREDENTIAL_LEVEL_P);
8430Sstevel@tonic-gate 	LDAP_SET_PARAM(arglist->proxyDN, NS_LDAP_BINDDN_P);
8448821SMichen.Chang@Sun.COM 	LDAP_SET_PARAM(arglist->enableShadowUpdate,
8458821SMichen.Chang@Sun.COM 	    NS_LDAP_ENABLE_SHADOW_UPDATE_P);
8468821SMichen.Chang@Sun.COM 	LDAP_SET_PARAM(arglist->adminDN, NS_LDAP_ADMIN_BINDDN_P);
8470Sstevel@tonic-gate 	LDAP_SET_PARAM(arglist->searchTimeLimit, NS_LDAP_SEARCH_TIME_P);
8480Sstevel@tonic-gate 	LDAP_SET_PARAM(arglist->preferredServerList, NS_LDAP_SERVER_PREF_P);
8490Sstevel@tonic-gate 	LDAP_SET_PARAM(arglist->profileName, NS_LDAP_PROFILE_P);
8500Sstevel@tonic-gate 	LDAP_SET_PARAM(arglist->followReferrals, NS_LDAP_SEARCH_REF_P);
8510Sstevel@tonic-gate 	LDAP_SET_PARAM(arglist->defaultSearchScope, NS_LDAP_SEARCH_SCOPE_P);
8520Sstevel@tonic-gate 	LDAP_SET_PARAM(arglist->bindTimeLimit, NS_LDAP_BIND_TIME_P);
8530Sstevel@tonic-gate 	LDAP_SET_PARAM(arglist->proxyPassword, NS_LDAP_BINDPASSWD_P);
8548821SMichen.Chang@Sun.COM 	LDAP_SET_PARAM(arglist->adminPassword, NS_LDAP_ADMIN_BINDPASSWD_P);
8550Sstevel@tonic-gate 	LDAP_SET_PARAM(arglist->defaultServerList, NS_LDAP_SERVERS_P);
8560Sstevel@tonic-gate 	LDAP_SET_PARAM(arglist->certificatePath, NS_LDAP_HOST_CERTPATH_P);
8570Sstevel@tonic-gate 
8580Sstevel@tonic-gate 	for (counter = 0;
8596842Sth160488 	    counter < arglist->serviceAuthenticationMethod->count;
8606842Sth160488 	    counter++) {
8610Sstevel@tonic-gate 
8620Sstevel@tonic-gate 		LDAP_SET_PARAM(
8636842Sth160488 		    arglist->serviceAuthenticationMethod->optlist[counter],
8646842Sth160488 		    NS_LDAP_SERVICE_AUTH_METHOD_P);
8650Sstevel@tonic-gate 	}
8660Sstevel@tonic-gate 	for (counter = 0;
8676842Sth160488 	    counter < arglist->serviceCredentialLevel->count;
8686842Sth160488 	    counter++) {
8690Sstevel@tonic-gate 
8700Sstevel@tonic-gate 		LDAP_SET_PARAM(
8716842Sth160488 		    arglist->serviceCredentialLevel->optlist[counter],
8726842Sth160488 		    NS_LDAP_SERVICE_CRED_LEVEL_P);
8730Sstevel@tonic-gate 	}
8740Sstevel@tonic-gate 	for (counter = 0;
8756842Sth160488 	    counter < arglist->objectclassMap->count;
8766842Sth160488 	    counter++) {
8770Sstevel@tonic-gate 
8780Sstevel@tonic-gate 		LDAP_SET_PARAM(arglist->objectclassMap->optlist[counter],
8796842Sth160488 		    NS_LDAP_OBJECTCLASSMAP_P);
8800Sstevel@tonic-gate 	}
8810Sstevel@tonic-gate 	for (counter = 0; counter < arglist->attributeMap->count; counter++) {
8820Sstevel@tonic-gate 		LDAP_SET_PARAM(arglist->attributeMap->optlist[counter],
8836842Sth160488 		    NS_LDAP_ATTRIBUTEMAP_P);
8840Sstevel@tonic-gate 	}
8850Sstevel@tonic-gate 	for (counter = 0;
8866842Sth160488 	    counter < arglist->serviceSearchDescriptor->count;
8876842Sth160488 	    counter++) {
8880Sstevel@tonic-gate 
8890Sstevel@tonic-gate 		LDAP_SET_PARAM(
8906842Sth160488 		    arglist->serviceSearchDescriptor->optlist[counter],
8916842Sth160488 		    NS_LDAP_SERVICE_SEARCH_DESC_P);
8920Sstevel@tonic-gate 	}
8930Sstevel@tonic-gate 
8940Sstevel@tonic-gate 	retcode = credCheck(arglist);
8958821SMichen.Chang@Sun.COM 	if (retcode == CLIENT_SUCCESS)
8968821SMichen.Chang@Sun.COM 		retcode = adminCredCheck(arglist);
8970Sstevel@tonic-gate 	if (retcode != CLIENT_SUCCESS) {
8980Sstevel@tonic-gate 		CLIENT_FPUTS(
8996842Sth160488 		    gettext("Error in setting up credentials\n"),
9006842Sth160488 		    stderr);
9010Sstevel@tonic-gate 		return (retcode);
9020Sstevel@tonic-gate 	}
9030Sstevel@tonic-gate 
9040Sstevel@tonic-gate 	if (mode_verbose)
9050Sstevel@tonic-gate 		CLIENT_FPUTS(
9066842Sth160488 		    gettext("About to modify this machines "
9076842Sth160488 		    "configuration by writing the files\n"),
9086842Sth160488 		    stderr);
9090Sstevel@tonic-gate 
9100Sstevel@tonic-gate 	/* get ready to start playing with files */
9110Sstevel@tonic-gate 	retcode = stop_services(STATE_SAVE);
9120Sstevel@tonic-gate 	if (retcode != CLIENT_SUCCESS) {
9130Sstevel@tonic-gate 		CLIENT_FPUTS(
9146842Sth160488 		    gettext("Errors stopping network services.\n"), stderr);
9150Sstevel@tonic-gate 		return (CLIENT_ERR_FAIL);
9160Sstevel@tonic-gate 	}
9170Sstevel@tonic-gate 
9180Sstevel@tonic-gate 	/* Save orig versions of files */
9190Sstevel@tonic-gate 	retcode = file_backup();
9200Sstevel@tonic-gate 	if (retcode == CLIENT_ERR_RESTORE) {
9210Sstevel@tonic-gate 		CLIENT_FPUTS(
9226842Sth160488 		    gettext("System not in state to enable ldap client.\n"),
9236842Sth160488 		    stderr);
9240Sstevel@tonic-gate 
9250Sstevel@tonic-gate 		reset_ret = start_services(START_RESET);
9260Sstevel@tonic-gate 		if (reset_ret != CLIENT_SUCCESS) {
9270Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr, gettext("Error (%d) while "
9286842Sth160488 			    "starting services during reset\n"),
9296842Sth160488 			    reset_ret);
9300Sstevel@tonic-gate 		}
9310Sstevel@tonic-gate 		return (retcode);
9320Sstevel@tonic-gate 	} else if (retcode != CLIENT_SUCCESS) {
9330Sstevel@tonic-gate 		CLIENT_FPUTS(
9346842Sth160488 		    gettext("Save of system configuration failed!  "
9356842Sth160488 		    "Attempting recovery.\n"),
9366842Sth160488 		    stderr);
9370Sstevel@tonic-gate 		retcode = recover(STATE_NOSAVE);
9380Sstevel@tonic-gate 		if (retcode != CLIENT_SUCCESS) {
9390Sstevel@tonic-gate 			CLIENT_FPUTS(
9406842Sth160488 			    gettext("Recovery of systems configuration "
9416842Sth160488 			    "failed.  Manual intervention of "
9426842Sth160488 			    "config files is required.\n"),
9436842Sth160488 			    stderr);
9440Sstevel@tonic-gate 			return (retcode);
9450Sstevel@tonic-gate 		}
9460Sstevel@tonic-gate 
9470Sstevel@tonic-gate 		reset_ret = start_services(START_RESET);
9480Sstevel@tonic-gate 		if (reset_ret != CLIENT_SUCCESS) {
9490Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr, gettext("Error (%d) while "
9506842Sth160488 			    "starting services during reset\n"),
9516842Sth160488 			    reset_ret);
9520Sstevel@tonic-gate 		}
9530Sstevel@tonic-gate 
9540Sstevel@tonic-gate 		return (retcode);
9550Sstevel@tonic-gate 	}
9560Sstevel@tonic-gate 
9570Sstevel@tonic-gate 	/* Dump new files */
9580Sstevel@tonic-gate 	errorp = __ns_ldap_DumpConfiguration(NSCONFIGFILE);
9590Sstevel@tonic-gate 	if (errorp != NULL) {
9600Sstevel@tonic-gate 		CLIENT_FPRINTF(stderr,
9616842Sth160488 		    gettext("%s manual: errorp is not NULL; %s\n"),
9626842Sth160488 		    cmd, errorp->message);
9630Sstevel@tonic-gate 		retcode = recover(STATE_NOSAVE);
9640Sstevel@tonic-gate 		if (retcode != CLIENT_SUCCESS) {
9650Sstevel@tonic-gate 			CLIENT_FPUTS(
9666842Sth160488 			    gettext("Recovery of systems configuration "
9676842Sth160488 			    "failed.  Manual intervention of "
9686842Sth160488 			    "config files is required.\n"),
9696842Sth160488 			    stderr);
9700Sstevel@tonic-gate 			return (retcode);
9710Sstevel@tonic-gate 		}
9720Sstevel@tonic-gate 		reset_ret = start_services(START_RESET);
9730Sstevel@tonic-gate 		if (reset_ret != CLIENT_SUCCESS) {
9740Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr, gettext("Error (%d) while "
9756842Sth160488 			    "starting services during reset\n"),
9766842Sth160488 			    reset_ret);
9770Sstevel@tonic-gate 		}
9780Sstevel@tonic-gate 		(void) __ns_ldap_freeError(&errorp);
9790Sstevel@tonic-gate 		return (CLIENT_ERR_FAIL);
9800Sstevel@tonic-gate 	}
9810Sstevel@tonic-gate 
9820Sstevel@tonic-gate 	/* if (credargs(arglist)) */
9830Sstevel@tonic-gate 	errorp = __ns_ldap_DumpConfiguration(NSCREDFILE);
9840Sstevel@tonic-gate 	if (errorp != NULL) {
9850Sstevel@tonic-gate 		CLIENT_FPRINTF(stderr,
9866842Sth160488 		    gettext("%s init: errorp is not NULL; %s\n"),
9876842Sth160488 		    cmd, errorp->message);
9880Sstevel@tonic-gate 		retcode = recover(STATE_NOSAVE);
9890Sstevel@tonic-gate 		if (retcode != CLIENT_SUCCESS) {
9900Sstevel@tonic-gate 			CLIENT_FPUTS(
9916842Sth160488 			    gettext("Recovery of systems configuration "
9926842Sth160488 			    "failed.  Manual intervention of "
9936842Sth160488 			    "config files is required.\n"),
9946842Sth160488 			    stderr);
9950Sstevel@tonic-gate 			return (retcode);
9960Sstevel@tonic-gate 		}
9970Sstevel@tonic-gate 		reset_ret = start_services(START_RESET);
9980Sstevel@tonic-gate 		if (reset_ret != CLIENT_SUCCESS) {
9990Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr, gettext("Error (%d) while "
10006842Sth160488 			    "starting services during reset\n"),
10016842Sth160488 			    reset_ret);
10020Sstevel@tonic-gate 		}
10030Sstevel@tonic-gate 		(void) __ns_ldap_freeError(&errorp);
10040Sstevel@tonic-gate 		return (CLIENT_ERR_FAIL);
10050Sstevel@tonic-gate 	}
10060Sstevel@tonic-gate 
10070Sstevel@tonic-gate 	ret_copy = system(CMD_CP " " NSSWITCH_LDAP " " NSSWITCH_CONF);
10080Sstevel@tonic-gate 	if (ret_copy != 0) {
10090Sstevel@tonic-gate 		CLIENT_FPRINTF(stderr,
10106842Sth160488 		    gettext("Error %d copying (%s) -> (%s)\n"),
10116842Sth160488 		    ret_copy, NSSWITCH_LDAP, NSSWITCH_CONF);
10120Sstevel@tonic-gate 		retcode = recover(STATE_NOSAVE);
10130Sstevel@tonic-gate 		if (retcode != CLIENT_SUCCESS) {
10140Sstevel@tonic-gate 			CLIENT_FPUTS(
10156842Sth160488 			    gettext("Recovery of systems configuration "
10166842Sth160488 			    "failed.  Manual intervention of "
10176842Sth160488 			    "config files is required.\n"),
10186842Sth160488 			    stderr);
10190Sstevel@tonic-gate 		}
10200Sstevel@tonic-gate 		reset_ret = start_services(START_RESET);
10210Sstevel@tonic-gate 		if (reset_ret != CLIENT_SUCCESS) {
10220Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr, gettext("Error (%d) while "
10236842Sth160488 			    "starting services during reset\n"),
10246842Sth160488 			    reset_ret);
10250Sstevel@tonic-gate 		}
10260Sstevel@tonic-gate 		return (CLIENT_ERR_FAIL);
10270Sstevel@tonic-gate 	}
10280Sstevel@tonic-gate 
10290Sstevel@tonic-gate 	if ((domain_fp = open(DOMAINNAME, O_WRONLY|O_CREAT|O_TRUNC,
10306842Sth160488 	    S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) == -1) { /* 0644 */
10310Sstevel@tonic-gate 		CLIENT_FPRINTF(stderr, gettext("Cannot open %s\n"), DOMAINNAME);
10320Sstevel@tonic-gate 		retcode = recover(STATE_NOSAVE);
10330Sstevel@tonic-gate 		if (retcode != CLIENT_SUCCESS) {
10340Sstevel@tonic-gate 			CLIENT_FPUTS(
10356842Sth160488 			    gettext("Recovery of systems configuration "
10366842Sth160488 			    "failed.  Manual intervention of "
10376842Sth160488 			    "config files is required.\n"),
10386842Sth160488 			    stderr);
10390Sstevel@tonic-gate 			return (CLIENT_ERR_FAIL);
10400Sstevel@tonic-gate 		}
10410Sstevel@tonic-gate 		reset_ret = start_services(START_RESET);
10420Sstevel@tonic-gate 		if (reset_ret != CLIENT_SUCCESS) {
10430Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr, gettext("Error (%d) while "
10446842Sth160488 			    "starting services during reset\n"),
10456842Sth160488 			    reset_ret);
10460Sstevel@tonic-gate 		}
10470Sstevel@tonic-gate 		return (CLIENT_ERR_FAIL);
10480Sstevel@tonic-gate 	}
10490Sstevel@tonic-gate 	(void) write(domain_fp, dname, strlen(dname));
10500Sstevel@tonic-gate 	(void) write(domain_fp, "\n", 1);
10510Sstevel@tonic-gate 	(void) close(domain_fp);
10520Sstevel@tonic-gate 
10530Sstevel@tonic-gate 	retcode = start_services(START_INIT);
10540Sstevel@tonic-gate 
10550Sstevel@tonic-gate 	if (retcode == CLIENT_SUCCESS) {
10560Sstevel@tonic-gate 		CLIENT_FPUTS(gettext("System successfully configured\n"),
10576842Sth160488 		    stderr);
10580Sstevel@tonic-gate 	} else {
10590Sstevel@tonic-gate 		CLIENT_FPUTS(gettext("Error resetting system.\n"
10606842Sth160488 		    "Recovering old system settings.\n"), stderr),
10616842Sth160488 
10626842Sth160488 		    /* stop any started services for recover */
10636842Sth160488 		    /* don't stomp on history of saved services state */
10646842Sth160488 		    reset_ret = stop_services(STATE_NOSAVE);
10650Sstevel@tonic-gate 		if (reset_ret != CLIENT_SUCCESS) {
10660Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr, gettext("Error (%d) while "
10676842Sth160488 			    "stopping services during reset\n"),
10686842Sth160488 			    reset_ret);
10690Sstevel@tonic-gate 			/* Coninue and try to recover what we can */
10700Sstevel@tonic-gate 		}
10710Sstevel@tonic-gate 		reset_ret = recover(STATE_NOSAVE);
10720Sstevel@tonic-gate 		if (reset_ret != CLIENT_SUCCESS) {
10730Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr, gettext("Error (%d) while "
10746842Sth160488 			    "recovering service files during "
10756842Sth160488 			    "reset\n"), reset_ret);
10760Sstevel@tonic-gate 			/* Continue and start what we can */
10770Sstevel@tonic-gate 		}
10780Sstevel@tonic-gate 		reset_ret = start_services(START_RESET);
10790Sstevel@tonic-gate 		if (reset_ret != CLIENT_SUCCESS) {
10800Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr, gettext("Error (%d) while "
10816842Sth160488 			    "starting services during reset\n"),
10826842Sth160488 			    reset_ret);
10830Sstevel@tonic-gate 		}
10840Sstevel@tonic-gate 	}
10850Sstevel@tonic-gate 
10860Sstevel@tonic-gate 	return (retcode);
10870Sstevel@tonic-gate }
10880Sstevel@tonic-gate 
10890Sstevel@tonic-gate static int
client_mod(clientopts_t * arglist)10900Sstevel@tonic-gate client_mod(clientopts_t *arglist)
10910Sstevel@tonic-gate {
10920Sstevel@tonic-gate 	int counter;
10930Sstevel@tonic-gate 	int domain_fp;
10940Sstevel@tonic-gate 	ns_ldap_error_t *errorp;
10950Sstevel@tonic-gate 	int reset_ret;
10960Sstevel@tonic-gate 	int retcode = CLIENT_SUCCESS;
10970Sstevel@tonic-gate 
10980Sstevel@tonic-gate 	__ns_ldap_setServer(TRUE);	/* Need this for _ns_setParam() */
10990Sstevel@tonic-gate 	if ((errorp = __ns_ldap_LoadConfiguration()) != NULL) {
11000Sstevel@tonic-gate 		CLIENT_FPUTS(gettext("Cannot get load configuration\n"),
11016842Sth160488 		    stderr);
11020Sstevel@tonic-gate 		CLIENT_FPUTS(errorp->message, stderr);
11030Sstevel@tonic-gate 		CLIENT_FPUTC('\n', stderr);
11040Sstevel@tonic-gate 		(void) __ns_ldap_freeError(&errorp);
11050Sstevel@tonic-gate 		return (CLIENT_ERR_FAIL);
11060Sstevel@tonic-gate 	}
11070Sstevel@tonic-gate 
11080Sstevel@tonic-gate 	if (arglist->profileTTL != NULL) {
11090Sstevel@tonic-gate 		CLIENT_FPUTS(
11106842Sth160488 		    gettext("Mod aborted: profileTTL modification is "
11116842Sth160488 		    "not allowed in mod mode.\n"),
11126842Sth160488 		    stderr);
11130Sstevel@tonic-gate 		return (CLIENT_ERR_FAIL);
11140Sstevel@tonic-gate 	}
11150Sstevel@tonic-gate 
11160Sstevel@tonic-gate 	if (arglist->profileName != NULL) {
11170Sstevel@tonic-gate 		CLIENT_FPUTS(
11186842Sth160488 		    gettext("Mod aborted: profileName modification is "
11196842Sth160488 		    "not allowed.  If you want to use profiles "
11206842Sth160488 		    "generate one with genProfile and load it "
11216842Sth160488 		    "on the server with ldapadd.\n"),
11226842Sth160488 		    stderr);
11230Sstevel@tonic-gate 		return (CLIENT_ERR_FAIL);
11240Sstevel@tonic-gate 	}
11250Sstevel@tonic-gate 
11266842Sth160488 	LDAP_CHECK_INVALID(arglist->bindDN, "bind DN");
11276842Sth160488 	LDAP_CHECK_INVALID(arglist->bindPasswd, "bind password");
11280Sstevel@tonic-gate 
11290Sstevel@tonic-gate 	/* Set additional valid params from command line */
11300Sstevel@tonic-gate 	LDAP_SET_PARAM(arglist->authenticationMethod, NS_LDAP_AUTH_P);
11310Sstevel@tonic-gate 	LDAP_SET_PARAM(arglist->defaultSearchBase, NS_LDAP_SEARCH_BASEDN_P);
11320Sstevel@tonic-gate 	LDAP_SET_PARAM(arglist->credentialLevel, NS_LDAP_CREDENTIAL_LEVEL_P);
11330Sstevel@tonic-gate 	LDAP_SET_PARAM(arglist->proxyDN, NS_LDAP_BINDDN_P);
11348821SMichen.Chang@Sun.COM 	LDAP_SET_PARAM(arglist->adminDN, NS_LDAP_ADMIN_BINDDN_P);
11350Sstevel@tonic-gate 	LDAP_SET_PARAM(arglist->profileTTL, NS_LDAP_CACHETTL_P);
11360Sstevel@tonic-gate 	LDAP_SET_PARAM(arglist->searchTimeLimit, NS_LDAP_SEARCH_TIME_P);
11370Sstevel@tonic-gate 	LDAP_SET_PARAM(arglist->preferredServerList, NS_LDAP_SERVER_PREF_P);
11380Sstevel@tonic-gate 	LDAP_SET_PARAM(arglist->profileName, NS_LDAP_PROFILE_P);
11390Sstevel@tonic-gate 	LDAP_SET_PARAM(arglist->followReferrals, NS_LDAP_SEARCH_REF_P);
11400Sstevel@tonic-gate 	LDAP_SET_PARAM(arglist->defaultSearchScope, NS_LDAP_SEARCH_SCOPE_P);
11410Sstevel@tonic-gate 	LDAP_SET_PARAM(arglist->bindTimeLimit, NS_LDAP_BIND_TIME_P);
11420Sstevel@tonic-gate 	LDAP_SET_PARAM(arglist->proxyPassword, NS_LDAP_BINDPASSWD_P);
11438821SMichen.Chang@Sun.COM 	LDAP_SET_PARAM(arglist->adminPassword, NS_LDAP_ADMIN_BINDPASSWD_P);
11440Sstevel@tonic-gate 	LDAP_SET_PARAM(arglist->defaultServerList, NS_LDAP_SERVERS_P);
11458821SMichen.Chang@Sun.COM 	LDAP_SET_PARAM(arglist->enableShadowUpdate,
11468821SMichen.Chang@Sun.COM 	    NS_LDAP_ENABLE_SHADOW_UPDATE_P);
11470Sstevel@tonic-gate 	LDAP_SET_PARAM(arglist->certificatePath, NS_LDAP_HOST_CERTPATH_P);
11480Sstevel@tonic-gate 
11490Sstevel@tonic-gate 	for (counter = 0;
11506842Sth160488 	    counter < arglist->serviceAuthenticationMethod->count;
11516842Sth160488 	    counter++) {
11520Sstevel@tonic-gate 
11530Sstevel@tonic-gate 		LDAP_SET_PARAM(
11546842Sth160488 		    arglist->serviceAuthenticationMethod->optlist[counter],
11556842Sth160488 		    NS_LDAP_SERVICE_AUTH_METHOD_P);
11560Sstevel@tonic-gate 	}
11570Sstevel@tonic-gate 	for (counter = 0;
11586842Sth160488 	    counter < arglist->serviceCredentialLevel->count;
11596842Sth160488 	    counter++) {
11600Sstevel@tonic-gate 
11610Sstevel@tonic-gate 		LDAP_SET_PARAM(
11626842Sth160488 		    arglist->serviceCredentialLevel->optlist[counter],
11636842Sth160488 		    NS_LDAP_SERVICE_CRED_LEVEL_P);
11640Sstevel@tonic-gate 	}
11650Sstevel@tonic-gate 	for (counter = 0;
11666842Sth160488 	    counter < arglist->objectclassMap->count;
11676842Sth160488 	    counter++) {
11680Sstevel@tonic-gate 
11690Sstevel@tonic-gate 		LDAP_SET_PARAM(
11706842Sth160488 		    arglist->objectclassMap->optlist[counter],
11716842Sth160488 		    NS_LDAP_OBJECTCLASSMAP_P);
11720Sstevel@tonic-gate 	}
11730Sstevel@tonic-gate 	for (counter = 0;
11746842Sth160488 	    counter < arglist->attributeMap->count;
11756842Sth160488 	    counter++) {
11760Sstevel@tonic-gate 
11770Sstevel@tonic-gate 		LDAP_SET_PARAM(
11786842Sth160488 		    arglist->attributeMap->optlist[counter],
11796842Sth160488 		    NS_LDAP_ATTRIBUTEMAP_P);
11800Sstevel@tonic-gate 	}
11810Sstevel@tonic-gate 	for (counter = 0;
11826842Sth160488 	    counter < arglist->serviceSearchDescriptor->count;
11836842Sth160488 	    counter++) {
11840Sstevel@tonic-gate 
11850Sstevel@tonic-gate 		LDAP_SET_PARAM(
11866842Sth160488 		    arglist->serviceSearchDescriptor->optlist[counter],
11876842Sth160488 		    NS_LDAP_SERVICE_SEARCH_DESC_P);
11880Sstevel@tonic-gate 	}
11890Sstevel@tonic-gate 
11900Sstevel@tonic-gate 	retcode = credCheck(arglist);
11918821SMichen.Chang@Sun.COM 	if (retcode == CLIENT_SUCCESS)
11928821SMichen.Chang@Sun.COM 		retcode = adminCredCheck(arglist);
11930Sstevel@tonic-gate 	if (retcode != CLIENT_SUCCESS) {
11940Sstevel@tonic-gate 		CLIENT_FPUTS(
11956842Sth160488 		    gettext("Error in setting up credentials\n"),
11966842Sth160488 		    stderr);
11970Sstevel@tonic-gate 		return (retcode);
11980Sstevel@tonic-gate 	}
11990Sstevel@tonic-gate 
12000Sstevel@tonic-gate 	if (mode_verbose)
12010Sstevel@tonic-gate 		CLIENT_FPUTS(
12026842Sth160488 		    gettext("About to modify this machines configuration "
12036842Sth160488 		    "by writing the files\n"),
12046842Sth160488 		    stderr);
12050Sstevel@tonic-gate 
12060Sstevel@tonic-gate 	/* get ready to start playing with files */
12070Sstevel@tonic-gate 	retcode = stop_services(STATE_SAVE);
12080Sstevel@tonic-gate 	if (retcode != CLIENT_SUCCESS) {
12090Sstevel@tonic-gate 		CLIENT_FPUTS(
12106842Sth160488 		    gettext("Errors stopping network services.\n"), stderr);
12110Sstevel@tonic-gate 		return (CLIENT_ERR_FAIL);
12120Sstevel@tonic-gate 	}
12130Sstevel@tonic-gate 
12140Sstevel@tonic-gate 	/* Temporarily save orig versions of files */
12150Sstevel@tonic-gate 	retcode = mod_backup();
12160Sstevel@tonic-gate 	if (retcode != CLIENT_SUCCESS) {
12170Sstevel@tonic-gate 		CLIENT_FPUTS(
12186842Sth160488 		    gettext("Unable to backup the ldap client files!\n"),
12196842Sth160488 		    stderr);
12200Sstevel@tonic-gate 
12210Sstevel@tonic-gate 		return (retcode);
12220Sstevel@tonic-gate 
12230Sstevel@tonic-gate 	}
12240Sstevel@tonic-gate 
12250Sstevel@tonic-gate 	/* Dump new files */
12260Sstevel@tonic-gate 	errorp = __ns_ldap_DumpConfiguration(NSCONFIGFILE);
12270Sstevel@tonic-gate 	if (errorp != NULL) {
12280Sstevel@tonic-gate 		CLIENT_FPRINTF(stderr,
12296842Sth160488 		    gettext("%s mod: errorp is not NULL; %s\n"),
12306842Sth160488 		    cmd, errorp->message);
12310Sstevel@tonic-gate 		retcode = mod_recover();
12320Sstevel@tonic-gate 		if (retcode != CLIENT_SUCCESS) {
12330Sstevel@tonic-gate 			CLIENT_FPUTS(
12346842Sth160488 			    gettext("Recovery of systems configuration "
12356842Sth160488 			    "failed.  Manual intervention of "
12366842Sth160488 			    "config files is required.\n"),
12376842Sth160488 			    stderr);
12380Sstevel@tonic-gate 		}
12390Sstevel@tonic-gate 		(void) __ns_ldap_freeError(&errorp);
12400Sstevel@tonic-gate 		reset_ret = start_services(START_RESET);
12410Sstevel@tonic-gate 		if (reset_ret != CLIENT_SUCCESS) {
12420Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr, gettext("Error (%d) while "
12436842Sth160488 			    "starting services during reset\n"),
12446842Sth160488 			    reset_ret);
12450Sstevel@tonic-gate 		}
12460Sstevel@tonic-gate 		return (CLIENT_ERR_FAIL);
12470Sstevel@tonic-gate 	}
12480Sstevel@tonic-gate 
12490Sstevel@tonic-gate 	/* if (credargs(arglist)) */
12500Sstevel@tonic-gate 	errorp = __ns_ldap_DumpConfiguration(NSCREDFILE);
12510Sstevel@tonic-gate 	if (errorp != NULL) {
12520Sstevel@tonic-gate 		CLIENT_FPRINTF(stderr,
12536842Sth160488 		    gettext("%s mod: errorp is not NULL; %s\n"),
12546842Sth160488 		    cmd, errorp->message);
12550Sstevel@tonic-gate 		retcode = mod_recover();
12560Sstevel@tonic-gate 		if (retcode != CLIENT_SUCCESS) {
12570Sstevel@tonic-gate 			CLIENT_FPUTS(
12586842Sth160488 			    gettext("Recovery of systems configuration "
12596842Sth160488 			    "failed.  Manual intervention of "
12606842Sth160488 			    "config files is required.\n"),
12616842Sth160488 			    stderr);
12620Sstevel@tonic-gate 		}
12630Sstevel@tonic-gate 		(void) __ns_ldap_freeError(&errorp);
12640Sstevel@tonic-gate 		reset_ret = start_services(START_RESET);
12650Sstevel@tonic-gate 		if (reset_ret != CLIENT_SUCCESS) {
12660Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr, gettext("Error (%d) while "
12676842Sth160488 			    "starting services during reset\n"),
12686842Sth160488 			    reset_ret);
12690Sstevel@tonic-gate 		}
12700Sstevel@tonic-gate 		return (CLIENT_ERR_FAIL);
12710Sstevel@tonic-gate 	}
12720Sstevel@tonic-gate 
12730Sstevel@tonic-gate 	if ((domain_fp = open(DOMAINNAME, O_WRONLY|O_CREAT|O_TRUNC,
12746842Sth160488 	    S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) == -1) { /* 0644 */
12750Sstevel@tonic-gate 		CLIENT_FPRINTF(stderr, gettext("Cannot open %s\n"), DOMAINNAME);
12760Sstevel@tonic-gate 		retcode = mod_recover();
12770Sstevel@tonic-gate 		if (retcode != CLIENT_SUCCESS) {
12780Sstevel@tonic-gate 			CLIENT_FPUTS(
12796842Sth160488 			    gettext("Recovery of systems configuration "
12806842Sth160488 			    "failed!  Machine needs to be "
12816842Sth160488 			    "fixed!\n"),
12826842Sth160488 			    stderr);
12830Sstevel@tonic-gate 		}
12840Sstevel@tonic-gate 		reset_ret = start_services(START_RESET);
12850Sstevel@tonic-gate 		if (reset_ret != CLIENT_SUCCESS) {
12860Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr, gettext("Error (%d) while "
12876842Sth160488 			    "starting services during reset\n"),
12886842Sth160488 			    reset_ret);
12890Sstevel@tonic-gate 		}
12900Sstevel@tonic-gate 		return (CLIENT_ERR_FAIL);
12910Sstevel@tonic-gate 	}
12920Sstevel@tonic-gate 	(void) write(domain_fp, dname, strlen(dname));
12930Sstevel@tonic-gate 	(void) write(domain_fp, "\n", 1);
12940Sstevel@tonic-gate 	(void) close(domain_fp);
12950Sstevel@tonic-gate 
12960Sstevel@tonic-gate 	retcode = start_services(START_INIT);
12970Sstevel@tonic-gate 
12980Sstevel@tonic-gate 	if (retcode == CLIENT_SUCCESS) {
12990Sstevel@tonic-gate 		CLIENT_FPUTS(gettext("System successfully configured\n"),
13006842Sth160488 		    stderr);
13010Sstevel@tonic-gate 	} else {
13020Sstevel@tonic-gate 		CLIENT_FPUTS(gettext("Error resetting system.\n"
13036842Sth160488 		    "Recovering old system settings.\n"), stderr),
13046842Sth160488 
13056842Sth160488 		    /* stop any started services for recover */
13066842Sth160488 		    /* don't stomp on history of saved services state */
13076842Sth160488 		    reset_ret = stop_services(STATE_NOSAVE);
13080Sstevel@tonic-gate 		if (reset_ret != CLIENT_SUCCESS) {
13090Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr, gettext("Error (%d) while "
13106842Sth160488 			    "stopping services during reset\n"),
13116842Sth160488 			    reset_ret);
13120Sstevel@tonic-gate 			/* Coninue and try to recover what we can */
13130Sstevel@tonic-gate 		}
13140Sstevel@tonic-gate 		reset_ret = mod_recover();
13150Sstevel@tonic-gate 		if (reset_ret != CLIENT_SUCCESS) {
13160Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr, gettext("Error (%d) while "
13176842Sth160488 			    "recovering service files during "
13186842Sth160488 			    "reset\n"), reset_ret);
13190Sstevel@tonic-gate 			/* Continue and start what we can */
13200Sstevel@tonic-gate 		}
13210Sstevel@tonic-gate 		reset_ret = start_services(START_RESET);
13220Sstevel@tonic-gate 		if (reset_ret != CLIENT_SUCCESS) {
13230Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr, gettext("Error (%d) while "
13246842Sth160488 			    "starting services during reset\n"),
13256842Sth160488 			    reset_ret);
13260Sstevel@tonic-gate 		}
13270Sstevel@tonic-gate 	}
13280Sstevel@tonic-gate 
13290Sstevel@tonic-gate 	/* Cleanup temporary files created by mod_backup() */
13300Sstevel@tonic-gate 	mod_cleanup();
13310Sstevel@tonic-gate 
13320Sstevel@tonic-gate 	return (retcode);
13330Sstevel@tonic-gate }
13340Sstevel@tonic-gate 
13350Sstevel@tonic-gate 
13360Sstevel@tonic-gate static int
client_genProfile(clientopts_t * arglist)13370Sstevel@tonic-gate client_genProfile(clientopts_t *arglist)
13380Sstevel@tonic-gate {
13390Sstevel@tonic-gate 	int counter;
13400Sstevel@tonic-gate 	int retcode;	/* required for LDAP_SET_PARAM macro */
13410Sstevel@tonic-gate 	ns_ldap_error_t *errorp;
13420Sstevel@tonic-gate 
13430Sstevel@tonic-gate 	if (mode_verbose)
13440Sstevel@tonic-gate 		CLIENT_FPUTS(gettext("About to generate a profile\n"), stderr);
13450Sstevel@tonic-gate 
13460Sstevel@tonic-gate 	/* *** Check for invalid args *** */
13470Sstevel@tonic-gate 	LDAP_CHECK_INVALID(arglist->proxyDN, "proxyDN");
13480Sstevel@tonic-gate 	LDAP_CHECK_INVALID(arglist->proxyPassword, "proxyPassword");
13498821SMichen.Chang@Sun.COM 	LDAP_CHECK_INVALID(arglist->enableShadowUpdate,
13508821SMichen.Chang@Sun.COM 	    "enableShadowUpdate");
13518821SMichen.Chang@Sun.COM 	LDAP_CHECK_INVALID(arglist->adminDN, "adminDN");
13528821SMichen.Chang@Sun.COM 	LDAP_CHECK_INVALID(arglist->adminPassword, "adminPassword");
13530Sstevel@tonic-gate 	LDAP_CHECK_INVALID(arglist->certificatePath, "certificatePath");
13540Sstevel@tonic-gate 	LDAP_CHECK_INVALID(arglist->domainName, "domainName");
13556842Sth160488 	LDAP_CHECK_INVALID(arglist->bindDN, "bind DN");
13566842Sth160488 	LDAP_CHECK_INVALID(arglist->bindPasswd, "bind password");
13570Sstevel@tonic-gate 	/* *** End check for invalid args *** */
13580Sstevel@tonic-gate 
13590Sstevel@tonic-gate 	if (arglist->profileName == NULL) {
13600Sstevel@tonic-gate 		if (mode_verbose)
13610Sstevel@tonic-gate 			CLIENT_FPUTS(
13626842Sth160488 			    gettext("No profile specified. "
13636842Sth160488 			    "Using \"default\"\n"),
13646842Sth160488 			    stderr);
13650Sstevel@tonic-gate 		arglist->profileName = "default";
13660Sstevel@tonic-gate 	}
13670Sstevel@tonic-gate 
13680Sstevel@tonic-gate 	__ns_ldap_setServer(TRUE);
13690Sstevel@tonic-gate 	__ns_ldap_default_config();
13700Sstevel@tonic-gate 
13710Sstevel@tonic-gate 	/* Set version to latest (not version 1) */
13720Sstevel@tonic-gate 	LDAP_SET_PARAM(NS_LDAP_VERSION, NS_LDAP_FILE_VERSION_P);
13730Sstevel@tonic-gate 
13740Sstevel@tonic-gate 	/* Set additional valid params from command line */
13750Sstevel@tonic-gate 	LDAP_SET_PARAM(arglist->authenticationMethod, NS_LDAP_AUTH_P);
13760Sstevel@tonic-gate 	LDAP_SET_PARAM(arglist->defaultSearchBase, NS_LDAP_SEARCH_BASEDN_P);
13770Sstevel@tonic-gate 	LDAP_SET_PARAM(arglist->credentialLevel, NS_LDAP_CREDENTIAL_LEVEL_P);
13780Sstevel@tonic-gate 	LDAP_SET_PARAM(arglist->profileTTL, NS_LDAP_CACHETTL_P);
13790Sstevel@tonic-gate 	LDAP_SET_PARAM(arglist->searchTimeLimit, NS_LDAP_SEARCH_TIME_P);
13800Sstevel@tonic-gate 	LDAP_SET_PARAM(arglist->preferredServerList, NS_LDAP_SERVER_PREF_P);
13810Sstevel@tonic-gate 	LDAP_SET_PARAM(arglist->profileName, NS_LDAP_PROFILE_P);
13820Sstevel@tonic-gate 	LDAP_SET_PARAM(arglist->followReferrals, NS_LDAP_SEARCH_REF_P);
13830Sstevel@tonic-gate 	LDAP_SET_PARAM(arglist->defaultSearchScope, NS_LDAP_SEARCH_SCOPE_P);
13840Sstevel@tonic-gate 	LDAP_SET_PARAM(arglist->bindTimeLimit, NS_LDAP_BIND_TIME_P);
13850Sstevel@tonic-gate 	LDAP_SET_PARAM(arglist->defaultServerList, NS_LDAP_SERVERS_P);
13860Sstevel@tonic-gate 
13870Sstevel@tonic-gate 	for (counter = 0;
13886842Sth160488 	    counter < arglist->serviceAuthenticationMethod->count;
13896842Sth160488 	    counter++) {
13900Sstevel@tonic-gate 
13910Sstevel@tonic-gate 		LDAP_SET_PARAM(
13926842Sth160488 		    arglist->serviceAuthenticationMethod->optlist[counter],
13936842Sth160488 		    NS_LDAP_SERVICE_AUTH_METHOD_P);
13940Sstevel@tonic-gate 	}
13950Sstevel@tonic-gate 	for (counter = 0;
13966842Sth160488 	    counter < arglist->serviceCredentialLevel->count;
13976842Sth160488 	    counter++) {
13980Sstevel@tonic-gate 
13990Sstevel@tonic-gate 		LDAP_SET_PARAM(
14006842Sth160488 		    arglist->serviceCredentialLevel->optlist[counter],
14016842Sth160488 		    NS_LDAP_SERVICE_CRED_LEVEL_P);
14020Sstevel@tonic-gate 	}
14030Sstevel@tonic-gate 	for (counter = 0;
14046842Sth160488 	    counter < arglist->objectclassMap->count;
14056842Sth160488 	    counter++) {
14060Sstevel@tonic-gate 
14070Sstevel@tonic-gate 		LDAP_SET_PARAM(
14086842Sth160488 		    arglist->objectclassMap->optlist[counter],
14096842Sth160488 		    NS_LDAP_OBJECTCLASSMAP_P);
14100Sstevel@tonic-gate 	}
14110Sstevel@tonic-gate 	for (counter = 0;
14126842Sth160488 	    counter < arglist->attributeMap->count;
14136842Sth160488 	    counter++) {
14140Sstevel@tonic-gate 
14150Sstevel@tonic-gate 		LDAP_SET_PARAM(
14166842Sth160488 		    arglist->attributeMap->optlist[counter],
14176842Sth160488 		    NS_LDAP_ATTRIBUTEMAP_P);
14180Sstevel@tonic-gate 	}
14196842Sth160488 	for (counter = 0;
14206842Sth160488 	    counter < arglist->serviceSearchDescriptor->count;
14216842Sth160488 	    counter++) {
14226842Sth160488 
14236842Sth160488 		LDAP_SET_PARAM(
14246842Sth160488 		    arglist->serviceSearchDescriptor->optlist[counter],
14256842Sth160488 		    NS_LDAP_SERVICE_SEARCH_DESC_P);
14260Sstevel@tonic-gate 	}
14270Sstevel@tonic-gate 
14280Sstevel@tonic-gate 	errorp = __ns_ldap_DumpLdif(NULL);
14290Sstevel@tonic-gate 	if (errorp != NULL) {
14300Sstevel@tonic-gate 		CLIENT_FPUTS(errorp->message, stderr);
14310Sstevel@tonic-gate 		CLIENT_FPUTC('\n', stderr);
14320Sstevel@tonic-gate 		(void) __ns_ldap_freeError(&errorp);
14330Sstevel@tonic-gate 		return (CLIENT_ERR_FAIL);
14340Sstevel@tonic-gate 	}
14350Sstevel@tonic-gate 
14360Sstevel@tonic-gate 	return (CLIENT_SUCCESS);
14370Sstevel@tonic-gate }
14380Sstevel@tonic-gate 
14396842Sth160488 /* INET6_ADDRSTRLEN + ":" + <5-digit port> + some round-up */
14406842Sth160488 #define	MAX_HOSTADDR_LEN (INET6_ADDRSTRLEN + 6 + 12)
14416842Sth160488 
14420Sstevel@tonic-gate static int
client_init(clientopts_t * arglist)14430Sstevel@tonic-gate client_init(clientopts_t *arglist)
14440Sstevel@tonic-gate {
14456842Sth160488 	int			profile_fp;
14466842Sth160488 	int			retcode = CLIENT_SUCCESS;
14476842Sth160488 	ns_ldap_error_t		*errorp;
14486842Sth160488 	int			reset_ret;
14496842Sth160488 	int			ret_copy;
14506842Sth160488 	ns_standalone_conf_t	cfg = standaloneDefaults;
14516842Sth160488 	ns_auth_t		auth = {NS_LDAP_AUTH_NONE,
14526842Sth160488 					NS_LDAP_TLS_NONE,
14536842Sth160488 					NS_LDAP_SASL_NONE,
14546842Sth160488 					NS_LDAP_SASLOPT_NONE};
14556842Sth160488 	char			peer[MAX_HOSTADDR_LEN];
14566842Sth160488 	ns_auth_t		**authMethod;
14576842Sth160488 	int			**credLevel, i;
14586842Sth160488 	char			*cred;
14590Sstevel@tonic-gate 
14600Sstevel@tonic-gate 	if (mode_verbose)
14610Sstevel@tonic-gate 		CLIENT_FPUTS(
14626842Sth160488 		    gettext("About to configure machine by downloading "
14636842Sth160488 		    "a profile\n"),
14646842Sth160488 		    stderr);
14650Sstevel@tonic-gate 
14660Sstevel@tonic-gate 	if (dname == NULL) {
14670Sstevel@tonic-gate 		CLIENT_FPUTS(
14686842Sth160488 		    gettext("Init failed: System domain not set and "
14696842Sth160488 		    "no domainName specified.\n"),
14706842Sth160488 		    stderr);
14710Sstevel@tonic-gate 		return (CLIENT_ERR_FAIL);
14720Sstevel@tonic-gate 	}
14730Sstevel@tonic-gate 
14740Sstevel@tonic-gate 	if (!arglist->defaultServerList) {
14750Sstevel@tonic-gate 		CLIENT_FPUTS(gettext("Missing LDAP server address\n"), stderr);
14760Sstevel@tonic-gate 		return (CLIENT_ERR_FAIL);
14770Sstevel@tonic-gate 	}
14780Sstevel@tonic-gate 
14790Sstevel@tonic-gate 	/* *** Check for invalid args *** */
14800Sstevel@tonic-gate 	LDAP_CHECK_INVALID(arglist->defaultSearchBase,
14816842Sth160488 	    "defaultSearchBase");
14820Sstevel@tonic-gate 	LDAP_CHECK_INVALID(arglist->profileTTL,
14836842Sth160488 	    "profileTTL");
14840Sstevel@tonic-gate 	LDAP_CHECK_INVALID(arglist->searchTimeLimit,
14856842Sth160488 	    "searchTimeLimit");
14860Sstevel@tonic-gate 	LDAP_CHECK_INVALID(arglist->preferredServerList,
14876842Sth160488 	    "preferredServerList");
14880Sstevel@tonic-gate 	LDAP_CHECK_INVALID(arglist->followReferrals,
14896842Sth160488 	    "followReferrals");
14900Sstevel@tonic-gate 	LDAP_CHECK_INVALID(arglist->defaultSearchScope,
14916842Sth160488 	    "defaultSearchScope");
14920Sstevel@tonic-gate 	LDAP_CHECK_INVALID(arglist->bindTimeLimit,
14936842Sth160488 	    "bindTimeLimit");
14940Sstevel@tonic-gate 
14950Sstevel@tonic-gate 	LDAP_CHECK_INVALID(arglist->objectclassMap->count,
14966842Sth160488 	    "objectclassMap");
14970Sstevel@tonic-gate 	LDAP_CHECK_INVALID(arglist->attributeMap->count,
14986842Sth160488 	    "attributeMap");
14990Sstevel@tonic-gate 	LDAP_CHECK_INVALID(arglist->serviceAuthenticationMethod->count,
15006842Sth160488 	    "serviceAuthenticationMethod");
15010Sstevel@tonic-gate 	LDAP_CHECK_INVALID(arglist->serviceCredentialLevel->count,
15026842Sth160488 	    "serviceCredentialLevel");
15030Sstevel@tonic-gate 	LDAP_CHECK_INVALID(arglist->serviceSearchDescriptor->count,
15046842Sth160488 	    "serviceSearchDescriptor");
15050Sstevel@tonic-gate 	/* *** End check for invalid args *** */
15060Sstevel@tonic-gate 
15070Sstevel@tonic-gate 	if (arglist->profileName == NULL) {
15080Sstevel@tonic-gate 		if (mode_verbose)
15090Sstevel@tonic-gate 			CLIENT_FPUTS(
15106842Sth160488 			    gettext("No profile specified. "
15116842Sth160488 			    "Using \"default\"\n"),
15126842Sth160488 			    stderr);
15130Sstevel@tonic-gate 		arglist->profileName = "default";
15140Sstevel@tonic-gate 	}
15150Sstevel@tonic-gate 
15166842Sth160488 	(void) strncpy(peer, arglist->defaultServerList, MAX_HOSTADDR_LEN - 1);
15176842Sth160488 	if (separatePort(peer, &cfg.SA_SERVER, &cfg.SA_PORT) > 0) {
15180Sstevel@tonic-gate 		return (CLIENT_ERR_FAIL);
15190Sstevel@tonic-gate 	}
15206842Sth160488 
15216842Sth160488 	if (arglist->bindDN != NULL) {
15226842Sth160488 		cfg.SA_CRED = "proxy";
15236842Sth160488 		/*
15246842Sth160488 		 * We don't want to force users to always specify authentication
15258821SMichen.Chang@Sun.COM 		 * method when we can infer it. If users want SSL, he/she would
15266842Sth160488 		 * have to specify appropriate -a though.
15276842Sth160488 		 */
15286842Sth160488 		auth.type = NS_LDAP_AUTH_SIMPLE;
15296842Sth160488 		if (arglist->bindPasswd == NULL) {
15306842Sth160488 			arglist->bindPasswd =
15316842Sth160488 			    getpassphrase("Bind Password:");
15326842Sth160488 			if (arglist->bindPasswd == NULL) {
15336842Sth160488 				CLIENT_FPUTS(gettext("Get password failed\n"),
15346842Sth160488 				    stderr);
15356842Sth160488 
15366842Sth160488 				if (gStartLdap == START_RESET)
15376842Sth160488 					(void) start_service(LDAP_FMRI, B_TRUE);
15386842Sth160488 
15396842Sth160488 				return (CLIENT_ERR_CREDENTIAL);
15406842Sth160488 			}
15416842Sth160488 		}
15420Sstevel@tonic-gate 	}
15436842Sth160488 	cfg.SA_BIND_DN = arglist->bindDN;
15446842Sth160488 	cfg.SA_BIND_PWD = arglist->bindPasswd;
15456842Sth160488 
15466842Sth160488 	if (arglist->authenticationMethod != NULL) {
15476842Sth160488 		if (__ns_ldap_initAuth(arglist->authenticationMethod,
15486842Sth160488 		    &auth, &errorp) != NS_LDAP_SUCCESS) {
15496842Sth160488 			if (errorp != NULL) {
15506842Sth160488 				CLIENT_FPRINTF(stderr, "%s", errorp->message);
15516842Sth160488 				(void) __ns_ldap_freeError(&errorp);
15526842Sth160488 			}
15536842Sth160488 
15546842Sth160488 			if (gStartLdap == START_RESET)
15556842Sth160488 				(void) start_service(LDAP_FMRI, B_TRUE);
15566842Sth160488 
15576842Sth160488 			return (CLIENT_ERR_FAIL);
15586842Sth160488 		}
15596842Sth160488 		cfg.SA_AUTH = &auth;
15606842Sth160488 	}
15616842Sth160488 	cfg.SA_CRED = arglist->credentialLevel;
15626842Sth160488 
15636842Sth160488 	cfg.SA_DOMAIN = arglist->domainName;
15646842Sth160488 	cfg.SA_PROFILE_NAME = arglist->profileName;
15656842Sth160488 	cfg.SA_CERT_PATH = arglist->certificatePath;
15666842Sth160488 
15676842Sth160488 	cfg.type = NS_LDAP_SERVER;
15686842Sth160488 
15696842Sth160488 	if (__ns_ldap_initStandalone(&cfg, &errorp) != NS_LDAP_SUCCESS) {
15700Sstevel@tonic-gate 		if (errorp != NULL) {
15716842Sth160488 			CLIENT_FPRINTF(stderr, "%s", errorp->message);
15720Sstevel@tonic-gate 			(void) __ns_ldap_freeError(&errorp);
15730Sstevel@tonic-gate 		}
15740Sstevel@tonic-gate 
15750Sstevel@tonic-gate 		if (gStartLdap == START_RESET)
15760Sstevel@tonic-gate 			(void) start_service(LDAP_FMRI, B_TRUE);
15770Sstevel@tonic-gate 
15780Sstevel@tonic-gate 		return (CLIENT_ERR_FAIL);
15790Sstevel@tonic-gate 	}
15800Sstevel@tonic-gate 
15816842Sth160488 	if (arglist->proxyDN != NULL && arglist->proxyPassword == NULL) {
15826842Sth160488 		arglist->proxyPassword = getpassphrase("Proxy Bind Password:");
15836842Sth160488 		if (arglist->proxyPassword == NULL) {
15846842Sth160488 			CLIENT_FPUTS(gettext("Get password failed\n"), stderr);
15856842Sth160488 
15866842Sth160488 			if (gStartLdap == START_RESET)
15876842Sth160488 				(void) start_service(LDAP_FMRI, B_TRUE);
15886842Sth160488 
15896842Sth160488 			return (CLIENT_ERR_CREDENTIAL);
15906842Sth160488 		}
15916842Sth160488 	}
15926842Sth160488 	if (arglist->proxyDN != NULL && arglist->proxyPassword != NULL) {
15936842Sth160488 		if (__ns_ldap_setParam(NS_LDAP_BINDDN_P,
15946842Sth160488 		    arglist->proxyDN, &errorp) != NS_LDAP_SUCCESS) {
15956842Sth160488 			if (errorp != NULL) {
15966842Sth160488 				CLIENT_FPRINTF(stderr, "%s", errorp->message);
15976842Sth160488 				(void) __ns_ldap_freeError(&errorp);
15986842Sth160488 			}
15996842Sth160488 			return (CLIENT_ERR_CREDENTIAL);
16006842Sth160488 		}
16016842Sth160488 		if (__ns_ldap_setParam(NS_LDAP_BINDPASSWD_P,
16026842Sth160488 		    arglist->proxyPassword, &errorp) != NS_LDAP_SUCCESS) {
16036842Sth160488 			if (errorp != NULL) {
16046842Sth160488 				CLIENT_FPRINTF(stderr, "%s", errorp->message);
16056842Sth160488 				(void) __ns_ldap_freeError(&errorp);
16066842Sth160488 			}
16076842Sth160488 			return (CLIENT_ERR_CREDENTIAL);
16086842Sth160488 		}
16090Sstevel@tonic-gate 	}
16106842Sth160488 
16118821SMichen.Chang@Sun.COM 	if (arglist->enableShadowUpdate != NULL) {
16128821SMichen.Chang@Sun.COM 		LDAP_SET_PARAM(arglist->enableShadowUpdate,
16138821SMichen.Chang@Sun.COM 		    NS_LDAP_ENABLE_SHADOW_UPDATE_P);
16148821SMichen.Chang@Sun.COM 	}
16158821SMichen.Chang@Sun.COM 
16168821SMichen.Chang@Sun.COM 	if (arglist->enableShadowUpdate &&
16178821SMichen.Chang@Sun.COM 	    strcasecmp(arglist->enableShadowUpdate, "TRUE") == 0 &&
16188821SMichen.Chang@Sun.COM 	    arglist->adminDN != NULL && arglist->adminPassword == NULL) {
16198821SMichen.Chang@Sun.COM 		arglist->adminPassword = getpassphrase("admin Bind Password:");
16208821SMichen.Chang@Sun.COM 		if (arglist->adminPassword == NULL) {
16218821SMichen.Chang@Sun.COM 			CLIENT_FPUTS(gettext("Get password failed\n"), stderr);
16228821SMichen.Chang@Sun.COM 
16238821SMichen.Chang@Sun.COM 			if (gStartLdap == START_RESET)
16248821SMichen.Chang@Sun.COM 				(void) start_service(LDAP_FMRI, B_TRUE);
16258821SMichen.Chang@Sun.COM 
16268821SMichen.Chang@Sun.COM 			return (CLIENT_ERR_CREDENTIAL);
16278821SMichen.Chang@Sun.COM 		}
16288821SMichen.Chang@Sun.COM 	}
16298821SMichen.Chang@Sun.COM 	if (arglist->adminDN != NULL && arglist->adminPassword != NULL) {
16308821SMichen.Chang@Sun.COM 		if (__ns_ldap_setParam(NS_LDAP_ADMIN_BINDDN_P,
16318821SMichen.Chang@Sun.COM 		    arglist->adminDN, &errorp) != NS_LDAP_SUCCESS) {
16328821SMichen.Chang@Sun.COM 			if (errorp != NULL) {
16338821SMichen.Chang@Sun.COM 				CLIENT_FPRINTF(stderr, "%s\n", errorp->message);
16348821SMichen.Chang@Sun.COM 				(void) __ns_ldap_freeError(&errorp);
16358821SMichen.Chang@Sun.COM 			}
16368821SMichen.Chang@Sun.COM 			return (CLIENT_ERR_CREDENTIAL);
16378821SMichen.Chang@Sun.COM 		}
16388821SMichen.Chang@Sun.COM 		if (__ns_ldap_setParam(NS_LDAP_ADMIN_BINDPASSWD_P,
16398821SMichen.Chang@Sun.COM 		    arglist->adminPassword, &errorp) != NS_LDAP_SUCCESS) {
16408821SMichen.Chang@Sun.COM 			if (errorp != NULL) {
16418821SMichen.Chang@Sun.COM 				CLIENT_FPRINTF(stderr, "%s\n", errorp->message);
16428821SMichen.Chang@Sun.COM 				(void) __ns_ldap_freeError(&errorp);
16438821SMichen.Chang@Sun.COM 			}
16448821SMichen.Chang@Sun.COM 			return (CLIENT_ERR_CREDENTIAL);
16458821SMichen.Chang@Sun.COM 		}
16468821SMichen.Chang@Sun.COM 	}
16478821SMichen.Chang@Sun.COM 
16486842Sth160488 	if (arglist->authenticationMethod != NULL) {
16496842Sth160488 		if (__ns_ldap_getParam(NS_LDAP_AUTH_P,
16506842Sth160488 		    (void ***)&authMethod, &errorp) != NS_LDAP_SUCCESS) {
16516842Sth160488 			if (errorp != NULL) {
16526842Sth160488 				CLIENT_FPRINTF(stderr, "%s", errorp->message);
16536842Sth160488 				(void) __ns_ldap_freeError(&errorp);
16546842Sth160488 			}
16556842Sth160488 			return (CLIENT_ERR_CREDENTIAL);
16566842Sth160488 		}
16576842Sth160488 
16586842Sth160488 		if (authMethod != NULL) {
16596842Sth160488 			for (i = 0; authMethod[i] != NULL; ++i) {
16606842Sth160488 				if (authMethod[i]->type == auth.type) {
16616842Sth160488 					break;
16626842Sth160488 				}
16636842Sth160488 			}
16646842Sth160488 
16656842Sth160488 			if (authMethod[i] == NULL) {
16666842Sth160488 				CLIENT_FPRINTF(stderr, gettext(
16676842Sth160488 				    "Warning: init authentication method "
16686842Sth160488 				    "not found in DUAConfigProfile.\n"));
16696842Sth160488 			} else {
16706842Sth160488 				if (i != 0) {
16716842Sth160488 					CLIENT_FPRINTF(stderr,
16726842Sth160488 					    gettext(
16736842Sth160488 					    "Warning: init authentication"
16746842Sth160488 					    "method using secondary "
16756842Sth160488 					    "authentication method from "
16766842Sth160488 					    "DUAConfigProfile.\n"));
16776842Sth160488 				}
16786842Sth160488 			}
16796842Sth160488 			(void) __ns_ldap_freeParam((void ***) &authMethod);
16806842Sth160488 		}
16810Sstevel@tonic-gate 	}
16826842Sth160488 
16836842Sth160488 	if (arglist->credentialLevel != NULL) {
16846842Sth160488 		if (__ns_ldap_getParam(NS_LDAP_CREDENTIAL_LEVEL_P,
16856842Sth160488 		    (void ***)&credLevel, &errorp) != NS_LDAP_SUCCESS) {
16866842Sth160488 			if (errorp != NULL) {
16876842Sth160488 				CLIENT_FPRINTF(stderr, "%s", errorp->message);
16886842Sth160488 				(void) __ns_ldap_freeError(&errorp);
16896842Sth160488 			}
16906842Sth160488 			return (CLIENT_ERR_CREDENTIAL);
16916842Sth160488 		}
16926842Sth160488 		if (credLevel != NULL) {
16936842Sth160488 			for (i = 0; credLevel[i] != NULL; ++i) {
16946842Sth160488 				switch (*credLevel[i]) {
16956842Sth160488 				case NS_LDAP_CRED_ANON :
16966842Sth160488 					cred = "none";
16976842Sth160488 					break;
16986842Sth160488 				case NS_LDAP_CRED_PROXY :
16996842Sth160488 					cred = "proxy";
17006842Sth160488 					break;
17016842Sth160488 				case NS_LDAP_CRED_SELF :
17026842Sth160488 					cred = "self";
17036842Sth160488 					break;
17046842Sth160488 				default:
17056842Sth160488 					continue;
17066842Sth160488 					break;
17076842Sth160488 				}
17086842Sth160488 				if (strcmp(cred,
17096842Sth160488 				    arglist->credentialLevel) == 0) {
17106842Sth160488 					break;
17116842Sth160488 				}
17126842Sth160488 			}
17136842Sth160488 			if (credLevel[i] == NULL) {
17146842Sth160488 				CLIENT_FPRINTF(stderr, gettext(
17156842Sth160488 				    "Warning: init credential level not found "
17166842Sth160488 				    "in DUAConfigProfile.\n"));
17176842Sth160488 			} else {
17186842Sth160488 				if (i != 0) {
17196842Sth160488 					CLIENT_FPRINTF(stderr,
17206842Sth160488 					    gettext("Warning: "
17216842Sth160488 					    "init credential level using "
17226842Sth160488 					    "secondary credential level from "
17236842Sth160488 					    "DUAConfigProfile.\n"));
17246842Sth160488 				}
17256842Sth160488 			}
17266842Sth160488 			(void) __ns_ldap_freeParam((void ***) &credLevel);
17276842Sth160488 		}
17286842Sth160488 	}
17290Sstevel@tonic-gate 
17300Sstevel@tonic-gate 	retcode = credCheck(arglist);
17318821SMichen.Chang@Sun.COM 	if (retcode == CLIENT_SUCCESS)
17328821SMichen.Chang@Sun.COM 		retcode = adminCredCheck(arglist);
17330Sstevel@tonic-gate 	if (retcode != CLIENT_SUCCESS) {
17340Sstevel@tonic-gate 		CLIENT_FPUTS(
17356842Sth160488 		    gettext("Error in setting up credentials\n"), stderr);
17360Sstevel@tonic-gate 
17370Sstevel@tonic-gate 		if (gStartLdap == START_RESET)
17380Sstevel@tonic-gate 			(void) start_service(LDAP_FMRI, B_TRUE);
17390Sstevel@tonic-gate 
17400Sstevel@tonic-gate 		return (retcode);
17410Sstevel@tonic-gate 	}
17420Sstevel@tonic-gate 
17430Sstevel@tonic-gate 	if (mode_verbose)
17440Sstevel@tonic-gate 		CLIENT_FPUTS(
17456842Sth160488 		    gettext("About to modify this machines configuration "
17466842Sth160488 		    "by writing the files\n"),
17476842Sth160488 		    stderr);
17480Sstevel@tonic-gate 
17490Sstevel@tonic-gate 	/* get ready to start playing with files */
17500Sstevel@tonic-gate 	retcode = stop_services(STATE_SAVE);
17510Sstevel@tonic-gate 	if (retcode != CLIENT_SUCCESS) {
17520Sstevel@tonic-gate 		CLIENT_FPUTS(
17536842Sth160488 		    gettext("Errors stopping network services.\n"), stderr);
17540Sstevel@tonic-gate 
17550Sstevel@tonic-gate 		if (gStartLdap == START_RESET)
17560Sstevel@tonic-gate 			(void) start_service(LDAP_FMRI, B_TRUE);
17570Sstevel@tonic-gate 
17580Sstevel@tonic-gate 		return (CLIENT_ERR_FAIL);
17590Sstevel@tonic-gate 	}
17600Sstevel@tonic-gate 
17610Sstevel@tonic-gate 	/* Save orig versions of files */
17620Sstevel@tonic-gate 	retcode = file_backup();
17630Sstevel@tonic-gate 	if (retcode == CLIENT_ERR_RESTORE) {
17640Sstevel@tonic-gate 		CLIENT_FPUTS(
17656842Sth160488 		    gettext("System not in state to enable ldap client.\n"),
17666842Sth160488 		    stderr);
17670Sstevel@tonic-gate 
17680Sstevel@tonic-gate 		return (retcode);
17690Sstevel@tonic-gate 
17700Sstevel@tonic-gate 	} else if (retcode != CLIENT_SUCCESS) {
17710Sstevel@tonic-gate 		CLIENT_FPUTS(
17726842Sth160488 		    gettext("Save of system configuration failed.  "
17736842Sth160488 		    "Attempting recovery.\n"),
17746842Sth160488 		    stderr);
17750Sstevel@tonic-gate 		retcode = recover(STATE_NOSAVE);
17760Sstevel@tonic-gate 		if (retcode != CLIENT_SUCCESS) {
17770Sstevel@tonic-gate 			CLIENT_FPUTS(
17786842Sth160488 			    gettext("Recovery of systems configuration "
17796842Sth160488 			    "failed.  Manual intervention of "
17806842Sth160488 			    "config files is required.\n"),
17816842Sth160488 			    stderr);
17820Sstevel@tonic-gate 		}
17830Sstevel@tonic-gate 
17840Sstevel@tonic-gate 		reset_ret = start_services(START_RESET);
17850Sstevel@tonic-gate 		if (reset_ret != CLIENT_SUCCESS) {
17860Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr, gettext("Error (%d) while "
17876842Sth160488 			    "starting services during reset\n"),
17886842Sth160488 			    reset_ret);
17890Sstevel@tonic-gate 		}
17900Sstevel@tonic-gate 
17910Sstevel@tonic-gate 		return (retcode);
17920Sstevel@tonic-gate 	}
17930Sstevel@tonic-gate 
17940Sstevel@tonic-gate 	/* Dump new files */
17950Sstevel@tonic-gate 	errorp = __ns_ldap_DumpConfiguration(NSCONFIGFILE);
17960Sstevel@tonic-gate 	if (NULL != errorp) {
17970Sstevel@tonic-gate 		CLIENT_FPRINTF(stderr,
17986842Sth160488 		    gettext("%s init: errorp is not NULL; %s\n"),
17996842Sth160488 		    cmd, errorp->message);
18000Sstevel@tonic-gate 		retcode = recover(STATE_NOSAVE);
18010Sstevel@tonic-gate 		if (retcode != CLIENT_SUCCESS) {
18020Sstevel@tonic-gate 			CLIENT_FPUTS(
18036842Sth160488 			    gettext("Recovery of systems configuration "
18046842Sth160488 			    "failed.  Manual intervention of "
18056842Sth160488 			    "config files is required.\n"),
18066842Sth160488 			    stderr);
18070Sstevel@tonic-gate 			return (CLIENT_ERR_FAIL);
18080Sstevel@tonic-gate 		}
18090Sstevel@tonic-gate 		(void) __ns_ldap_freeError(&errorp);
18100Sstevel@tonic-gate 		reset_ret = start_services(START_RESET);
18110Sstevel@tonic-gate 		if (reset_ret != CLIENT_SUCCESS) {
18120Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr, gettext("Error (%d) while "
18136842Sth160488 			    "starting services during reset\n"),
18146842Sth160488 			    reset_ret);
18150Sstevel@tonic-gate 		}
18160Sstevel@tonic-gate 		return (CLIENT_ERR_FAIL);
18170Sstevel@tonic-gate 	}
18180Sstevel@tonic-gate 
18190Sstevel@tonic-gate 	/* if (credargs(arglist)) */
18200Sstevel@tonic-gate 	errorp = __ns_ldap_DumpConfiguration(NSCREDFILE);
18210Sstevel@tonic-gate 	if (NULL != errorp) {
18220Sstevel@tonic-gate 		CLIENT_FPRINTF(stderr,
18236842Sth160488 		    gettext("%s init: errorp is not NULL; %s\n"),
18246842Sth160488 		    cmd, errorp->message);
18250Sstevel@tonic-gate 		retcode = recover(STATE_NOSAVE);
18260Sstevel@tonic-gate 		if (retcode != CLIENT_SUCCESS) {
18270Sstevel@tonic-gate 			CLIENT_FPUTS(
18286842Sth160488 			    gettext("Recovery of systems configuration "
18296842Sth160488 			    "failed.  Manual intervention of "
18306842Sth160488 			    "config files is required.\n"),
18316842Sth160488 			    stderr);
18320Sstevel@tonic-gate 			return (CLIENT_ERR_FAIL);
18330Sstevel@tonic-gate 		}
18340Sstevel@tonic-gate 		(void) __ns_ldap_freeError(&errorp);
18350Sstevel@tonic-gate 		reset_ret = start_services(START_RESET);
18360Sstevel@tonic-gate 		if (reset_ret != CLIENT_SUCCESS) {
18370Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr, gettext("Error (%d) while "
18386842Sth160488 			    "starting services during reset\n"),
18396842Sth160488 			    reset_ret);
18400Sstevel@tonic-gate 		}
18410Sstevel@tonic-gate 		return (CLIENT_ERR_FAIL);
18420Sstevel@tonic-gate 	}
18430Sstevel@tonic-gate 
18440Sstevel@tonic-gate 	ret_copy = system(CMD_CP " " NSSWITCH_LDAP " " NSSWITCH_CONF);
18450Sstevel@tonic-gate 	if (ret_copy != 0) {
18460Sstevel@tonic-gate 		CLIENT_FPRINTF(stderr,
18476842Sth160488 		    gettext("Error %d copying (%s) -> (%s)\n"),
18486842Sth160488 		    ret_copy, NSSWITCH_LDAP, NSSWITCH_CONF);
18490Sstevel@tonic-gate 		retcode = recover(STATE_NOSAVE);
18500Sstevel@tonic-gate 		if (retcode != CLIENT_SUCCESS) {
18510Sstevel@tonic-gate 			CLIENT_FPUTS(
18526842Sth160488 			    gettext("Recovery of systems configuration "
18536842Sth160488 			    "failed.  Manual intervention of "
18546842Sth160488 			    "config files is required.\n"),
18556842Sth160488 			    stderr);
18560Sstevel@tonic-gate 		}
18570Sstevel@tonic-gate 		reset_ret = start_services(START_RESET);
18580Sstevel@tonic-gate 		if (reset_ret != CLIENT_SUCCESS) {
18590Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr, gettext("Error (%d) while "
18606842Sth160488 			    "starting services during reset\n"),
18616842Sth160488 			    reset_ret);
18620Sstevel@tonic-gate 		}
18630Sstevel@tonic-gate 		return (CLIENT_ERR_FAIL);
18640Sstevel@tonic-gate 	}
18650Sstevel@tonic-gate 
18660Sstevel@tonic-gate 	if ((profile_fp = open(DOMAINNAME, O_WRONLY|O_CREAT|O_TRUNC,
18676842Sth160488 	    S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) == -1) { /* 0644 */
18680Sstevel@tonic-gate 		CLIENT_FPRINTF(stderr, gettext("Cannot open %s\n"), DOMAINNAME);
18690Sstevel@tonic-gate 		retcode = recover(STATE_NOSAVE);
18700Sstevel@tonic-gate 		if (retcode != CLIENT_SUCCESS) {
18710Sstevel@tonic-gate 			CLIENT_FPUTS(
18726842Sth160488 			    gettext("Recovery of systems configuration "
18736842Sth160488 			    "failed.  Manual intervention of "
18746842Sth160488 			    "config files is required.\n"),
18756842Sth160488 			    stderr);
18760Sstevel@tonic-gate 			return (CLIENT_ERR_FAIL);
18770Sstevel@tonic-gate 		}
18780Sstevel@tonic-gate 		reset_ret = start_services(START_RESET);
18790Sstevel@tonic-gate 		if (reset_ret != CLIENT_SUCCESS) {
18800Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr, gettext("Error (%d) while "
18816842Sth160488 			    "starting services during reset\n"),
18826842Sth160488 			    reset_ret);
18830Sstevel@tonic-gate 		}
18840Sstevel@tonic-gate 		return (CLIENT_ERR_FAIL);
18850Sstevel@tonic-gate 	}
18860Sstevel@tonic-gate 	(void) write(profile_fp, dname, strlen(dname));
18870Sstevel@tonic-gate 	(void) write(profile_fp, "\n", 1);
18880Sstevel@tonic-gate 	(void) close(profile_fp);
18890Sstevel@tonic-gate 
18900Sstevel@tonic-gate 	retcode = start_services(START_INIT);
18910Sstevel@tonic-gate 
18920Sstevel@tonic-gate 	if (retcode == CLIENT_SUCCESS) {
18930Sstevel@tonic-gate 		CLIENT_FPUTS(gettext("System successfully configured\n"),
18946842Sth160488 		    stderr);
18950Sstevel@tonic-gate 	} else {
18960Sstevel@tonic-gate 		CLIENT_FPUTS(gettext("Error resetting system.\n"
18976842Sth160488 		    "Recovering old system settings.\n"), stderr),
18986842Sth160488 
18996842Sth160488 		    /* stop any started services for recover */
19006842Sth160488 		    /* don't stomp on history of saved services state */
19016842Sth160488 		    reset_ret = stop_services(STATE_NOSAVE);
19020Sstevel@tonic-gate 		if (reset_ret != CLIENT_SUCCESS) {
19030Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr, gettext("Error (%d) while "
19046842Sth160488 			    "stopping services during reset\n"),
19056842Sth160488 			    reset_ret);
19060Sstevel@tonic-gate 			/* Coninue and try to recover what we can */
19070Sstevel@tonic-gate 		}
19080Sstevel@tonic-gate 		reset_ret = recover(STATE_NOSAVE);
19090Sstevel@tonic-gate 		if (reset_ret != CLIENT_SUCCESS) {
19100Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr, gettext("Error (%d) while "
19116842Sth160488 			    "recovering service files during "
19126842Sth160488 			    "reset\n"), reset_ret);
19130Sstevel@tonic-gate 			/* Continue and start what we can */
19140Sstevel@tonic-gate 		}
19150Sstevel@tonic-gate 		reset_ret = start_services(START_RESET);
19160Sstevel@tonic-gate 		if (reset_ret != CLIENT_SUCCESS) {
19170Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr, gettext("Error (%d) while "
19186842Sth160488 			    "starting services during reset\n"),
19196842Sth160488 			    reset_ret);
19200Sstevel@tonic-gate 		}
19210Sstevel@tonic-gate 	}
19220Sstevel@tonic-gate 
19230Sstevel@tonic-gate 	return (retcode);
19240Sstevel@tonic-gate }
19250Sstevel@tonic-gate 
19260Sstevel@tonic-gate 
19270Sstevel@tonic-gate static void
usage(void)19280Sstevel@tonic-gate usage(void)
19290Sstevel@tonic-gate {
19300Sstevel@tonic-gate 	if (mode_quiet)
19310Sstevel@tonic-gate 		return;
19320Sstevel@tonic-gate 
19330Sstevel@tonic-gate 	if (gen == 0) {
19340Sstevel@tonic-gate 		CLIENT_FPRINTF(stderr,
19356842Sth160488 		    gettext("Usage: %s [-v | -q] init | manual | mod | "
19366842Sth160488 		    "list | uninit [<args>]\n"),
19376842Sth160488 		    cmd);
19386842Sth160488 
19396842Sth160488 		CLIENT_FPRINTF(stderr,
19406842Sth160488 		    gettext("\n       %s [-v | -q] [-a authenticationMethod]"
19416842Sth160488 		    " [-D bindDN]\n\t[-w bindPassword] [-j passswdFile]"
19428821SMichen.Chang@Sun.COM 		    " [-y proxyPasswordFile]\n\t"
19438821SMichen.Chang@Sun.COM 		    "[-z adminPasswordFile] init [<args>]\n"),
19446842Sth160488 		    cmd);
19450Sstevel@tonic-gate 
19460Sstevel@tonic-gate 		CLIENT_FPUTS(
19476842Sth160488 		    gettext("\nSet up a server or workstation as a "
19486842Sth160488 		    "client of an LDAP namespace.\n"),
19496842Sth160488 		    stderr);
19500Sstevel@tonic-gate 	} else {	/* genprofile */
19510Sstevel@tonic-gate 		CLIENT_FPRINTF(stderr,
19526842Sth160488 		    gettext("Usage: %s [-v | -q] genprofile "
19536842Sth160488 		    "-a profileName=<name> "
19546842Sth160488 		    "-a defaultSearchBase=<base> <args>\n"),
19556842Sth160488 		    cmd);
19560Sstevel@tonic-gate 
19570Sstevel@tonic-gate 		CLIENT_FPUTS(
19586842Sth160488 		    gettext("\nGenerate a profile used to set up clients "
19596842Sth160488 		    "of an LDAP namespace.\n"),
19606842Sth160488 		    stderr);
19610Sstevel@tonic-gate 	}
19620Sstevel@tonic-gate 	CLIENT_FPUTS(
19636842Sth160488 	    gettext("<args> take the form of \'-a attrName=attrVal\' as "
19646842Sth160488 	    "described in the\n"),
19656842Sth160488 	    stderr);
19660Sstevel@tonic-gate 	CLIENT_FPUTS(gettext("man page: ldapclient(1M)\n"), stderr);
19670Sstevel@tonic-gate }
19680Sstevel@tonic-gate 
19690Sstevel@tonic-gate 
19700Sstevel@tonic-gate /*
19710Sstevel@tonic-gate  * stop_services is called to stop network services prior to their
19720Sstevel@tonic-gate  * config files being moved/changed.  In case a later recovery is needed
19730Sstevel@tonic-gate  * (an error occurs during config), we detect whether the service is
19740Sstevel@tonic-gate  * running and store that info so that a reset will only start services
19750Sstevel@tonic-gate  * that were stopped here.
19760Sstevel@tonic-gate  *
19770Sstevel@tonic-gate  * In terms of SMF, this translates to disabling the services. So we
19780Sstevel@tonic-gate  * try to disable them if they are in any other state
19790Sstevel@tonic-gate  *
19800Sstevel@tonic-gate  * Stop order :
19810Sstevel@tonic-gate  * sendmail, nscd, autofs, ldap.client, nisd (rpc), inetinit(domainname)
19820Sstevel@tonic-gate  */
19830Sstevel@tonic-gate static int
stop_services(int saveState)19840Sstevel@tonic-gate stop_services(int saveState)
19850Sstevel@tonic-gate {
19860Sstevel@tonic-gate 	int ret;
19870Sstevel@tonic-gate 
19880Sstevel@tonic-gate 	if (mode_verbose) {
19890Sstevel@tonic-gate 		CLIENT_FPUTS(gettext("Stopping network services\n"), stderr);
19900Sstevel@tonic-gate 	}
19910Sstevel@tonic-gate 
19920Sstevel@tonic-gate 	if (!is_service(SENDMAIL_FMRI, SCF_STATE_STRING_DISABLED)) {
19930Sstevel@tonic-gate 		if (mode_verbose)
19940Sstevel@tonic-gate 			CLIENT_FPUTS(gettext("Stopping sendmail\n"), stderr);
19950Sstevel@tonic-gate 		ret = disable_service(SENDMAIL_FMRI, B_TRUE);
19960Sstevel@tonic-gate 		if (ret != CLIENT_SUCCESS) {
19970Sstevel@tonic-gate 			/* Not serious, but tell user what to do */
19980Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr, gettext("Stopping sendmail "
19996842Sth160488 			    "failed with (%d). You may need to restart "
20006842Sth160488 			    "it manually for changes to take effect.\n"),
20016842Sth160488 			    ret);
20020Sstevel@tonic-gate 		} else enableFlag |= SENDMAIL_ON;
20030Sstevel@tonic-gate 	} else {
20040Sstevel@tonic-gate 		if (mode_verbose)
20050Sstevel@tonic-gate 			CLIENT_FPUTS(gettext("sendmail not running\n"), stderr);
20060Sstevel@tonic-gate 	}
20070Sstevel@tonic-gate 
20080Sstevel@tonic-gate 	if (!is_service(NSCD_FMRI, SCF_STATE_STRING_DISABLED)) {
20090Sstevel@tonic-gate 		if (mode_verbose)
20100Sstevel@tonic-gate 			CLIENT_FPUTS(gettext("Stopping nscd\n"), stderr);
20110Sstevel@tonic-gate 		ret = disable_service(NSCD_FMRI, B_TRUE);
20120Sstevel@tonic-gate 		if (ret != CLIENT_SUCCESS) {
20130Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr, gettext("Stopping nscd "
20140Sstevel@tonic-gate 			    "failed with (%d)\n"), ret);
20150Sstevel@tonic-gate 			return (CLIENT_ERR_FAIL);
20160Sstevel@tonic-gate 		} else enableFlag |= NSCD_ON;
20170Sstevel@tonic-gate 	} else {
20180Sstevel@tonic-gate 		if (mode_verbose)
20190Sstevel@tonic-gate 			CLIENT_FPUTS(gettext("nscd not running\n"), stderr);
20200Sstevel@tonic-gate 	}
20210Sstevel@tonic-gate 
20220Sstevel@tonic-gate 	if (!is_service(AUTOFS_FMRI, SCF_STATE_STRING_DISABLED)) {
20230Sstevel@tonic-gate 		if (mode_verbose)
20240Sstevel@tonic-gate 			CLIENT_FPUTS(gettext("Stopping autofs\n"), stderr);
20250Sstevel@tonic-gate 		ret = disable_service(AUTOFS_FMRI, B_TRUE);
20260Sstevel@tonic-gate 		if (ret != CLIENT_SUCCESS) {
20270Sstevel@tonic-gate 			/* Not serious, but tell user what to do */
20280Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr, gettext("Stopping autofs "
20296842Sth160488 			    "failed with (%d). You may need to restart "
20306842Sth160488 			    "it manually for changes to take effect.\n"),
20316842Sth160488 			    ret);
20320Sstevel@tonic-gate 		} else enableFlag |= AUTOFS_ON;
20330Sstevel@tonic-gate 	} else {
20340Sstevel@tonic-gate 		if (mode_verbose)
20350Sstevel@tonic-gate 			CLIENT_FPUTS(gettext("autofs not running\n"), stderr);
20360Sstevel@tonic-gate 	}
20370Sstevel@tonic-gate 
20380Sstevel@tonic-gate 	if (!is_service(LDAP_FMRI, SCF_STATE_STRING_DISABLED)) {
20390Sstevel@tonic-gate 		if (saveState)
20400Sstevel@tonic-gate 			gStartLdap = START_RESET;
20410Sstevel@tonic-gate 		if (mode_verbose)
20420Sstevel@tonic-gate 			CLIENT_FPUTS(gettext("Stopping ldap\n"), stderr);
20430Sstevel@tonic-gate 		ret = disable_service(LDAP_FMRI, B_TRUE);
20440Sstevel@tonic-gate 		if (ret != CLIENT_SUCCESS) {
20450Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr, gettext("Stopping ldap "
20460Sstevel@tonic-gate 			    "failed with (%d)\n"), ret);
20470Sstevel@tonic-gate 			return (CLIENT_ERR_FAIL);
20480Sstevel@tonic-gate 		}
20490Sstevel@tonic-gate 	} else {
20500Sstevel@tonic-gate 		if (mode_verbose)
20510Sstevel@tonic-gate 			CLIENT_FPUTS(gettext("ldap not running\n"),
20526842Sth160488 			    stderr);
20530Sstevel@tonic-gate 	}
20540Sstevel@tonic-gate 
20550Sstevel@tonic-gate 	if (!is_service(YP_FMRI, SCF_STATE_STRING_DISABLED)) {
20560Sstevel@tonic-gate 		if (saveState)
20570Sstevel@tonic-gate 			gStartYp = START_RESET;
20580Sstevel@tonic-gate 		if (mode_verbose)
20590Sstevel@tonic-gate 			CLIENT_FPUTS(gettext("Stopping nis(yp)\n"), stderr);
20600Sstevel@tonic-gate 		ret = disable_service(YP_FMRI, B_TRUE);
20610Sstevel@tonic-gate 		if (ret != 0) {
20620Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr, gettext("Stopping nis(yp) "
20630Sstevel@tonic-gate 			    "failed with (%d)\n"), ret);
20640Sstevel@tonic-gate 			return (CLIENT_ERR_FAIL);
20650Sstevel@tonic-gate 		}
20660Sstevel@tonic-gate 	} else {
20670Sstevel@tonic-gate 		if (mode_verbose)
20680Sstevel@tonic-gate 			CLIENT_FPUTS(gettext("nis(yp) not running\n"),
20696842Sth160488 			    stderr);
20700Sstevel@tonic-gate 	}
20710Sstevel@tonic-gate 
20720Sstevel@tonic-gate 	return (CLIENT_SUCCESS);
20730Sstevel@tonic-gate }
20740Sstevel@tonic-gate 
20750Sstevel@tonic-gate /*
20760Sstevel@tonic-gate  * start_services is called to start up network services after config
20770Sstevel@tonic-gate  * files have all been setup or recovered.  In the case of an error, the
20780Sstevel@tonic-gate  * files will be recovered and start_services will be called with the
20790Sstevel@tonic-gate  * "reset" flag set so that only those services that were earlier stopped
20800Sstevel@tonic-gate  * will be started.  If it is not a reset, then the services associated
20810Sstevel@tonic-gate  * with files "recovered" will attempt to be started.
20820Sstevel@tonic-gate  */
20830Sstevel@tonic-gate static int
start_services(int flag)20840Sstevel@tonic-gate start_services(int flag)
20850Sstevel@tonic-gate {
20862830Sdjl 	int sysret, retcode = CLIENT_SUCCESS, rc = NS_LDAP_SUCCESS;
20870Sstevel@tonic-gate 	FILE *domain_fp;
20880Sstevel@tonic-gate 	char domainname[BUFSIZ];
20890Sstevel@tonic-gate 	char cmd_domain_start[BUFSIZ];
20900Sstevel@tonic-gate 	int domainlen;
20912830Sdjl 	ns_ldap_self_gssapi_config_t config = NS_LDAP_SELF_GSSAPI_CONFIG_NONE;
20922830Sdjl 	ns_ldap_error_t		*errorp = NULL;
20930Sstevel@tonic-gate 
20940Sstevel@tonic-gate 	if (mode_verbose) {
20950Sstevel@tonic-gate 		CLIENT_FPUTS(gettext("Starting network services\n"), stderr);
20960Sstevel@tonic-gate 	}
20970Sstevel@tonic-gate 
20980Sstevel@tonic-gate 	/* Read in current defaultdomain so we can set it */
20990Sstevel@tonic-gate 	domain_fp = fopen(DOMAINNAME, "r");
21000Sstevel@tonic-gate 	if (domain_fp == NULL) {
21010Sstevel@tonic-gate 		CLIENT_FPRINTF(stderr, gettext("Error opening defaultdomain "
21026842Sth160488 		    "(%d)\n"), errno);
21030Sstevel@tonic-gate 		/* if we did an ldap init, we must have domain */
21040Sstevel@tonic-gate 		if (flag == START_INIT)
21050Sstevel@tonic-gate 			return (CLIENT_ERR_FAIL);
21060Sstevel@tonic-gate 	} else {
21070Sstevel@tonic-gate 		if (fgets(domainname, BUFSIZ, domain_fp) == NULL) {
21080Sstevel@tonic-gate 			CLIENT_FPUTS(gettext("Error reading defaultdomain\n"),
21096842Sth160488 			    stderr);
21100Sstevel@tonic-gate 			return (CLIENT_ERR_FAIL);
21110Sstevel@tonic-gate 		}
21120Sstevel@tonic-gate 
21130Sstevel@tonic-gate 		if (fclose(domain_fp) != 0) {
21140Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr,
21156842Sth160488 			    gettext("Error closing defaultdomain (%d)\n"),
21166842Sth160488 			    errno);
21170Sstevel@tonic-gate 			return (CLIENT_ERR_FAIL);
21180Sstevel@tonic-gate 		}
21190Sstevel@tonic-gate 		domainlen = strlen(domainname);
21200Sstevel@tonic-gate 		/* sanity check to make sure sprintf will fit */
21210Sstevel@tonic-gate 		if (domainlen > (BUFSIZE - sizeof (CMD_DOMAIN_START) -
21226842Sth160488 		    sizeof (TO_DEV_NULL) - 3)) {
21230Sstevel@tonic-gate 			CLIENT_FPUTS(gettext("Specified domainname is "
21246842Sth160488 			    "too large\n"), stderr);
21250Sstevel@tonic-gate 			return (CLIENT_ERR_FAIL);
21260Sstevel@tonic-gate 		}
21270Sstevel@tonic-gate 		if (domainname[domainlen-1] == '\n')
21280Sstevel@tonic-gate 			domainname[domainlen-1] = 0;
21290Sstevel@tonic-gate 		/* buffer size is checked above */
21302830Sdjl 		(void) snprintf(cmd_domain_start, BUFSIZ, "%s %s %s",
21316842Sth160488 		    CMD_DOMAIN_START, domainname, TO_DEV_NULL);
21320Sstevel@tonic-gate 	}
21330Sstevel@tonic-gate 
21340Sstevel@tonic-gate 	/*
21350Sstevel@tonic-gate 	 * We can be starting services after an init in which case
2136*11262SRajagopal.Andra@Sun.COM 	 * we want to start ldap and not start yp.
21370Sstevel@tonic-gate 	 */
21380Sstevel@tonic-gate 	if (flag == START_INIT) {
21390Sstevel@tonic-gate 		sysret = system(cmd_domain_start);
21400Sstevel@tonic-gate 		if (mode_verbose)
21410Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr, "start: %s %s... %s\n",
21426842Sth160488 			    CMD_DOMAIN_START, domainname,
21436842Sth160488 			    (sysret == 0) ? gettext("success") :
21446842Sth160488 			    gettext("failed"));
21450Sstevel@tonic-gate 		if (sysret != 0) {
21460Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr, gettext("\"%s\" returned: %d\n"),
21476842Sth160488 			    CMD_DOMAIN_START, sysret);
21480Sstevel@tonic-gate 
21490Sstevel@tonic-gate 			retcode = CLIENT_ERR_FAIL;
21500Sstevel@tonic-gate 		}
21510Sstevel@tonic-gate 
21522830Sdjl 		if ((rc = __ns_ldap_self_gssapi_config(&config)) !=
21536842Sth160488 		    NS_LDAP_SUCCESS) {
21542830Sdjl 			CLIENT_FPRINTF(stderr, gettext("Error (%d) while "
21556842Sth160488 			    "checking sasl/GSSAPI configuration\n"),
21566842Sth160488 			    rc);
21570Sstevel@tonic-gate 			retcode = CLIENT_ERR_FAIL;
21582830Sdjl 		}
21592830Sdjl 
21602830Sdjl 		if (config != NS_LDAP_SELF_GSSAPI_CONFIG_NONE) {
21612830Sdjl 
21622830Sdjl 			rc = __ns_ldap_check_dns_preq(
21636842Sth160488 			    1, mode_verbose, mode_quiet,
21646842Sth160488 			    NSSWITCH_LDAP, config, &errorp);
21652830Sdjl 			if (errorp)
21662830Sdjl 				(void) __ns_ldap_freeError(&errorp);
21672830Sdjl 
21682830Sdjl 			if (rc != NS_LDAP_SUCCESS)
21692830Sdjl 				retcode = CLIENT_ERR_FAIL;
21702830Sdjl 		}
21712830Sdjl 
21722830Sdjl 		if (rc == NS_LDAP_SUCCESS &&
21736842Sth160488 		    start_service(LDAP_FMRI, B_TRUE) != CLIENT_SUCCESS)
21742830Sdjl 			retcode = CLIENT_ERR_FAIL;
21752830Sdjl 
21762830Sdjl 		if (config != NS_LDAP_SELF_GSSAPI_CONFIG_NONE &&
21776842Sth160488 		    rc == NS_LDAP_SUCCESS && retcode == CLIENT_SUCCESS) {
21782830Sdjl 			rc = __ns_ldap_check_gssapi_preq(
21796842Sth160488 			    1, mode_verbose, mode_quiet, config,
21806842Sth160488 			    &errorp);
21812830Sdjl 			if (errorp)
21822830Sdjl 				(void) __ns_ldap_freeError(&errorp);
21832830Sdjl 
21842830Sdjl 			if (rc != NS_LDAP_SUCCESS)
21852830Sdjl 				retcode = CLIENT_ERR_FAIL;
21862830Sdjl 
21872830Sdjl 		}
2188*11262SRajagopal.Andra@Sun.COM 		/* No YP after init */
21890Sstevel@tonic-gate 	/*
21900Sstevel@tonic-gate 	 * Or we can be starting services after an uninit or error
21910Sstevel@tonic-gate 	 * recovery.  We want to start whatever services were running
21920Sstevel@tonic-gate 	 * before.  In the case of error recovery, it is the services
21930Sstevel@tonic-gate 	 * that were running before we stopped them (flags set in
21940Sstevel@tonic-gate 	 * stop_services).  If it is an uninit then we determine
21950Sstevel@tonic-gate 	 * which services to start based on the files we recovered
21960Sstevel@tonic-gate 	 * (flags set in recover).
21970Sstevel@tonic-gate 	 */
21980Sstevel@tonic-gate 	} else {
21990Sstevel@tonic-gate 		/* uninit and recover should set flags of what to start */
22000Sstevel@tonic-gate 		if (domain_fp) {
22010Sstevel@tonic-gate 			sysret = system(cmd_domain_start);
22020Sstevel@tonic-gate 			if (mode_verbose)
22030Sstevel@tonic-gate 				CLIENT_FPRINTF(stderr, "start: %s %s... %s\n",
22046842Sth160488 				    CMD_DOMAIN_START, domainname,
22056842Sth160488 				    (sysret == 0) ? gettext("success") :
22066842Sth160488 				    gettext("failed"));
22070Sstevel@tonic-gate 			if (sysret != 0) {
22080Sstevel@tonic-gate 				CLIENT_FPRINTF(stderr, gettext("\"%s\" "
22096842Sth160488 				    "returned: %d\n"),
22106842Sth160488 				    CMD_DOMAIN_START, sysret);
22110Sstevel@tonic-gate 
22120Sstevel@tonic-gate 				retcode = CLIENT_ERR_FAIL;
22130Sstevel@tonic-gate 			}
22140Sstevel@tonic-gate 		}
22150Sstevel@tonic-gate 
22160Sstevel@tonic-gate 		if (gStartLdap == flag) {
22170Sstevel@tonic-gate 			if (!(is_service(LDAP_FMRI, SCF_STATE_STRING_ONLINE)))
22180Sstevel@tonic-gate 				if (start_service(LDAP_FMRI, B_TRUE)
22196842Sth160488 				    != CLIENT_SUCCESS)
22200Sstevel@tonic-gate 					retcode = CLIENT_ERR_FAIL;
22210Sstevel@tonic-gate 		}
22220Sstevel@tonic-gate 
22230Sstevel@tonic-gate 		if (gStartYp == flag) {
22240Sstevel@tonic-gate 			if (!(is_service(YP_FMRI, SCF_STATE_STRING_ONLINE)))
22250Sstevel@tonic-gate 				(void) start_service(YP_FMRI, B_TRUE);
22260Sstevel@tonic-gate 		}
22270Sstevel@tonic-gate 	}
22280Sstevel@tonic-gate 	if ((enableFlag & AUTOFS_ON) &&
22290Sstevel@tonic-gate 	    !(is_service(AUTOFS_FMRI, SCF_STATE_STRING_ONLINE)))
22300Sstevel@tonic-gate 		(void) start_service(AUTOFS_FMRI, B_TRUE);
22310Sstevel@tonic-gate 
22320Sstevel@tonic-gate 	if ((enableFlag & NSCD_ON) &&
22330Sstevel@tonic-gate 	    !(is_service(NSCD_FMRI, SCF_STATE_STRING_ONLINE)))
22340Sstevel@tonic-gate 		(void) start_service(NSCD_FMRI, B_TRUE);
22350Sstevel@tonic-gate 
22362830Sdjl #if 0
22372830Sdjl 	if (flag == START_INIT && config != NS_LDAP_SELF_GSSAPI_CONFIG_NONE &&
22382830Sdjl 	    retcode == CLIENT_SUCCESS &&
22392830Sdjl 	    !(is_service(NSCD_FMRI, SCF_STATE_STRING_ONLINE))) {
22402830Sdjl 		CLIENT_FPRINTF(stderr, "start: %s\n",
22416842Sth160488 		    gettext("self/sasl/GSSAPI is configured"
22426842Sth160488 		    " but nscd is not online"));
22432830Sdjl 		retcode = CLIENT_ERR_FAIL;
22442830Sdjl 	}
22452830Sdjl #endif
22462830Sdjl 
22470Sstevel@tonic-gate 	if ((enableFlag & SENDMAIL_ON) &&
22480Sstevel@tonic-gate 	    !(is_service(SENDMAIL_FMRI, SCF_STATE_STRING_ONLINE)))
22490Sstevel@tonic-gate 		(void) start_service(SENDMAIL_FMRI, B_TRUE);
22500Sstevel@tonic-gate 
22510Sstevel@tonic-gate 	/*
22520Sstevel@tonic-gate 	 * Restart name-service milestone so that any consumer
22530Sstevel@tonic-gate 	 * which depends on it will be restarted.
22540Sstevel@tonic-gate 	 */
22550Sstevel@tonic-gate 	(void) restart_service(NS_MILESTONE_FMRI, B_TRUE);
22560Sstevel@tonic-gate 	return (retcode);
22570Sstevel@tonic-gate }
22580Sstevel@tonic-gate 
22590Sstevel@tonic-gate /*
22600Sstevel@tonic-gate  * credCheck is called to check if credentials are required for this
22610Sstevel@tonic-gate  * configuration.  Currently, this means that if any credentialLevel is
22620Sstevel@tonic-gate  * proxy and any authenticationMethod is something other than none, then
22630Sstevel@tonic-gate  * credential info is required (proxyDN and proxyPassword).
22640Sstevel@tonic-gate  */
22650Sstevel@tonic-gate static int
credCheck(clientopts_t * arglist)22660Sstevel@tonic-gate credCheck(clientopts_t *arglist)
22670Sstevel@tonic-gate {
22680Sstevel@tonic-gate 	int counter;
22690Sstevel@tonic-gate 	int **credLevel;
22700Sstevel@tonic-gate 	ns_auth_t **authMethod;
22710Sstevel@tonic-gate 	char **proxyDN, **proxyPassword;
22720Sstevel@tonic-gate 	ns_ldap_error_t *errorp;
22730Sstevel@tonic-gate 	int credProxy, authNotNone;
22740Sstevel@tonic-gate 	int retcode;
22750Sstevel@tonic-gate 
22760Sstevel@tonic-gate /* If credentialLevel is proxy, make sure we have proxyDN and proxyPassword */
22770Sstevel@tonic-gate 	retcode = __ns_ldap_getParam(NS_LDAP_CREDENTIAL_LEVEL_P,
22786842Sth160488 	    (void ***)&credLevel, &errorp);
22790Sstevel@tonic-gate 	if (retcode != 0) {
22800Sstevel@tonic-gate 		CLIENT_FPRINTF(stderr,
22816842Sth160488 		    gettext("Error %d while trying to retrieve "
22826842Sth160488 		    "credLevel\n"),
22836842Sth160488 		    retcode);
22840Sstevel@tonic-gate 		return (CLIENT_ERR_FAIL);
22850Sstevel@tonic-gate 	}
22860Sstevel@tonic-gate 	retcode = __ns_ldap_getParam(NS_LDAP_AUTH_P,
22876842Sth160488 	    (void ***)&authMethod, &errorp);
22880Sstevel@tonic-gate 	if (retcode != 0) {
22890Sstevel@tonic-gate 		CLIENT_FPRINTF(stderr,
22906842Sth160488 		    gettext("Error %d while trying to retrieve "
22916842Sth160488 		    "authMethod\n"), retcode);
22920Sstevel@tonic-gate 		return (CLIENT_ERR_FAIL);
22930Sstevel@tonic-gate 	}
22940Sstevel@tonic-gate 	retcode = __ns_ldap_getParam(NS_LDAP_BINDDN_P,
22956842Sth160488 	    (void ***)&proxyDN, &errorp);
22960Sstevel@tonic-gate 	if (retcode != 0) {
22970Sstevel@tonic-gate 		CLIENT_FPRINTF(stderr,
22986842Sth160488 		    gettext("Error %d while trying to retrieve proxyDN\n"),
22996842Sth160488 		    retcode);
23000Sstevel@tonic-gate 		return (CLIENT_ERR_FAIL);
23010Sstevel@tonic-gate 	}
23020Sstevel@tonic-gate 	retcode = __ns_ldap_getParam(NS_LDAP_BINDPASSWD_P,
23036842Sth160488 	    (void ***)&proxyPassword, &errorp);
23040Sstevel@tonic-gate 	if (retcode != 0) {
23050Sstevel@tonic-gate 		CLIENT_FPRINTF(stderr,
23066842Sth160488 		    gettext("Error %d while trying to retrieve "
23076842Sth160488 		    "proxyPassword\n"), retcode);
23080Sstevel@tonic-gate 		return (CLIENT_ERR_FAIL);
23090Sstevel@tonic-gate 	}
23100Sstevel@tonic-gate 
23110Sstevel@tonic-gate 	if (mode_verbose) {
23120Sstevel@tonic-gate 		CLIENT_FPRINTF(stderr,
23136842Sth160488 		    gettext("Proxy DN: %s\n"),
23146842Sth160488 		    (proxyDN && proxyDN[0]) ? proxyDN[0] : "NULL");
23150Sstevel@tonic-gate 		CLIENT_FPRINTF(stderr,
23166842Sth160488 		    gettext("Proxy password: %s\n"),
23176842Sth160488 		    (proxyPassword && proxyPassword[0]) ?
23186842Sth160488 		    proxyPassword[0] : "NULL");
23190Sstevel@tonic-gate 	}
23200Sstevel@tonic-gate 
23210Sstevel@tonic-gate 	credProxy = 0;	/* flag to indicate if we have a credLevel of proxy */
23220Sstevel@tonic-gate 	for (counter = 0; credLevel && credLevel[counter] != NULL; counter++) {
23230Sstevel@tonic-gate 		if (mode_verbose)
23240Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr,
23256842Sth160488 			    gettext("Credential level: %d\n"),
23266842Sth160488 			    *credLevel[counter]);
23270Sstevel@tonic-gate 		if (*credLevel[counter] == NS_LDAP_CRED_PROXY) {
23280Sstevel@tonic-gate 			credProxy = 1;
23290Sstevel@tonic-gate 			break;
23300Sstevel@tonic-gate 		}
23310Sstevel@tonic-gate 	}
23320Sstevel@tonic-gate 
23330Sstevel@tonic-gate 	authNotNone = 0;	/* flag for authMethod other than none */
23340Sstevel@tonic-gate 	for (counter = 0;
23356842Sth160488 	    authMethod && authMethod[counter] != NULL;
23366842Sth160488 	    counter++) {
23370Sstevel@tonic-gate 
23380Sstevel@tonic-gate 		if (mode_verbose)
23390Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr,
23406842Sth160488 			    gettext("Authentication method: %d\n"),
23416842Sth160488 			    authMethod[counter]->type);
23420Sstevel@tonic-gate 		if (authMethod[counter]->type != NS_LDAP_AUTH_NONE &&
23430Sstevel@tonic-gate 		    !(authMethod[counter]->type == NS_LDAP_AUTH_TLS &&
23440Sstevel@tonic-gate 		    authMethod[counter]->tlstype == NS_LDAP_TLS_NONE)) {
23450Sstevel@tonic-gate 			authNotNone = 1;
23460Sstevel@tonic-gate 			break;
23470Sstevel@tonic-gate 		}
23480Sstevel@tonic-gate 	}
23490Sstevel@tonic-gate 
23500Sstevel@tonic-gate 	/* First, if we don't need proxyDN/Password then just return ok */
23510Sstevel@tonic-gate 	if (!(credProxy && authNotNone)) {
23520Sstevel@tonic-gate 		if (mode_verbose)
23530Sstevel@tonic-gate 			CLIENT_FPUTS(
23546842Sth160488 			    gettext("No proxyDN/proxyPassword required\n"),
23556842Sth160488 			    stderr);
23560Sstevel@tonic-gate 		return (CLIENT_SUCCESS);
23570Sstevel@tonic-gate 	}
23580Sstevel@tonic-gate 
23590Sstevel@tonic-gate 	/* Now let's check if we have the cred stuff we need */
23600Sstevel@tonic-gate 	if (!proxyDN || !proxyDN[0]) {
23610Sstevel@tonic-gate 		CLIENT_FPUTS(
23626842Sth160488 		    gettext("credentialLevel is proxy and no proxyDN "
23636842Sth160488 		    "specified\n"),
23646842Sth160488 		    stderr);
23650Sstevel@tonic-gate 		return (CLIENT_ERR_CREDENTIAL);
23660Sstevel@tonic-gate 	}
23670Sstevel@tonic-gate 
23680Sstevel@tonic-gate 	/* If we need proxyPassword (prompt) */
23690Sstevel@tonic-gate 	if (!proxyPassword || !proxyPassword[0]) {
23700Sstevel@tonic-gate 		CLIENT_FPUTS(
23716842Sth160488 		    gettext("credentialLevel requires proxyPassword\n"),
23726842Sth160488 		    stderr);
23730Sstevel@tonic-gate 		arglist->proxyPassword = getpassphrase("Proxy Bind Password:");
23740Sstevel@tonic-gate 		if (arglist->proxyPassword == NULL) {
23750Sstevel@tonic-gate 			CLIENT_FPUTS(gettext("Get password failed\n"), stderr);
23760Sstevel@tonic-gate 			return (CLIENT_ERR_CREDENTIAL);
23770Sstevel@tonic-gate 		}
23780Sstevel@tonic-gate 		LDAP_SET_PARAM(arglist->proxyPassword, NS_LDAP_BINDPASSWD_P);
23790Sstevel@tonic-gate 		if (retcode != 0) {
23800Sstevel@tonic-gate 			CLIENT_FPUTS(
23816842Sth160488 			    gettext("setParam proxyPassword failed.\n"),
23826842Sth160488 			    stderr);
23830Sstevel@tonic-gate 			return (CLIENT_ERR_CREDENTIAL);
23840Sstevel@tonic-gate 		}
23850Sstevel@tonic-gate 	}
23860Sstevel@tonic-gate 
23870Sstevel@tonic-gate 	return (CLIENT_SUCCESS);
23880Sstevel@tonic-gate }
23890Sstevel@tonic-gate 
23900Sstevel@tonic-gate /*
23918821SMichen.Chang@Sun.COM  * adminCredCheck is called to check if the admin credential is required
23928821SMichen.Chang@Sun.COM  * for this configuration. This means that if enableShadowUpdate is set
23938821SMichen.Chang@Sun.COM  * to TRUE then credential info is required (adminDN and adminPassword).
23948821SMichen.Chang@Sun.COM  * One exception is that if there is a 'self' credentialLevel and
23958821SMichen.Chang@Sun.COM  * 'sasl/GSSAPI' authenticationMethod (i.e., possibly using Kerberos
23968821SMichen.Chang@Sun.COM  * host credential) then adminDN and adminPassword are not required.
23978821SMichen.Chang@Sun.COM  */
23988821SMichen.Chang@Sun.COM static int
adminCredCheck(clientopts_t * arglist)23998821SMichen.Chang@Sun.COM adminCredCheck(clientopts_t *arglist)
24008821SMichen.Chang@Sun.COM {
24018821SMichen.Chang@Sun.COM 	int counter;
24028821SMichen.Chang@Sun.COM 	int **enabled = NULL;
24038821SMichen.Chang@Sun.COM 	int **credLevel = NULL;
24048821SMichen.Chang@Sun.COM 	char **adminDN = NULL;
24058821SMichen.Chang@Sun.COM 	char **adminPassword = NULL;
24068821SMichen.Chang@Sun.COM 	ns_auth_t **authMethod = NULL;
24078821SMichen.Chang@Sun.COM 	ns_ldap_error_t *errorp = NULL;
24088821SMichen.Chang@Sun.COM 	int credSelf, authSASLgss;
24098821SMichen.Chang@Sun.COM 	int retcode, rc;
24108821SMichen.Chang@Sun.COM 
24118821SMichen.Chang@Sun.COM 	/* If shadow update not enabled, then no need to check */
24128821SMichen.Chang@Sun.COM 	retcode = __ns_ldap_getParam(NS_LDAP_ENABLE_SHADOW_UPDATE_P,
24138821SMichen.Chang@Sun.COM 	    (void ***)&enabled, &errorp);
24148821SMichen.Chang@Sun.COM 	if (retcode != 0) {
24158821SMichen.Chang@Sun.COM 		CLIENT_FPRINTF(stderr,
24168821SMichen.Chang@Sun.COM 		    gettext("Error %d while trying to retrieve "
24178821SMichen.Chang@Sun.COM 		    "enableShadowUpdate\n"), retcode);
24188821SMichen.Chang@Sun.COM 		rc = CLIENT_ERR_FAIL;
24198821SMichen.Chang@Sun.COM 		goto out;
24208821SMichen.Chang@Sun.COM 	}
24218821SMichen.Chang@Sun.COM 	if (enabled == NULL ||
24228821SMichen.Chang@Sun.COM 	    *enabled[0] != NS_LDAP_ENABLE_SHADOW_UPDATE_TRUE) {
24238821SMichen.Chang@Sun.COM 		if (mode_verbose)
24248821SMichen.Chang@Sun.COM 			CLIENT_FPUTS(
24258821SMichen.Chang@Sun.COM 			    gettext("Shadow Update is not enabled, "
24268821SMichen.Chang@Sun.COM 			    "no adminDN/adminPassword is required.\n"), stderr);
24278821SMichen.Chang@Sun.COM 		rc = CLIENT_SUCCESS;
24288821SMichen.Chang@Sun.COM 		goto out;
24298821SMichen.Chang@Sun.COM 	}
24308821SMichen.Chang@Sun.COM 
24318821SMichen.Chang@Sun.COM 	/* get credentialLevel */
24328821SMichen.Chang@Sun.COM 	retcode = __ns_ldap_getParam(NS_LDAP_CREDENTIAL_LEVEL_P,
24338821SMichen.Chang@Sun.COM 	    (void ***)&credLevel, &errorp);
24348821SMichen.Chang@Sun.COM 	if (retcode != 0) {
24358821SMichen.Chang@Sun.COM 		CLIENT_FPRINTF(stderr,
24368821SMichen.Chang@Sun.COM 		    gettext("Error %d while trying to retrieve credLevel\n"),
24378821SMichen.Chang@Sun.COM 		    retcode);
24388821SMichen.Chang@Sun.COM 		rc = CLIENT_ERR_FAIL;
24398821SMichen.Chang@Sun.COM 		goto out;
24408821SMichen.Chang@Sun.COM 	}
24418821SMichen.Chang@Sun.COM 
24428821SMichen.Chang@Sun.COM 	/* get AuthenticationMethod */
24438821SMichen.Chang@Sun.COM 	retcode = __ns_ldap_getParam(NS_LDAP_AUTH_P,
24448821SMichen.Chang@Sun.COM 	    (void ***)&authMethod, &errorp);
24458821SMichen.Chang@Sun.COM 	if (retcode != 0) {
24468821SMichen.Chang@Sun.COM 		CLIENT_FPRINTF(stderr,
24478821SMichen.Chang@Sun.COM 		    gettext("Error %d while trying to retrieve authMethod\n"),
24488821SMichen.Chang@Sun.COM 		    retcode);
24498821SMichen.Chang@Sun.COM 		rc = CLIENT_ERR_FAIL;
24508821SMichen.Chang@Sun.COM 		goto out;
24518821SMichen.Chang@Sun.COM 	}
24528821SMichen.Chang@Sun.COM 
24538821SMichen.Chang@Sun.COM 	/* get adminDN */
24548821SMichen.Chang@Sun.COM 	retcode = __ns_ldap_getParam(NS_LDAP_ADMIN_BINDDN_P,
24558821SMichen.Chang@Sun.COM 	    (void ***)&adminDN, &errorp);
24568821SMichen.Chang@Sun.COM 	if (retcode != 0) {
24578821SMichen.Chang@Sun.COM 		CLIENT_FPRINTF(stderr,
24588821SMichen.Chang@Sun.COM 		    gettext("Error %d while trying to retrieve adminDN\n"),
24598821SMichen.Chang@Sun.COM 		    retcode);
24608821SMichen.Chang@Sun.COM 		rc = CLIENT_ERR_FAIL;
24618821SMichen.Chang@Sun.COM 		goto out;
24628821SMichen.Chang@Sun.COM 	}
24638821SMichen.Chang@Sun.COM 
24648821SMichen.Chang@Sun.COM 	/* get adminPassword */
24658821SMichen.Chang@Sun.COM 	retcode = __ns_ldap_getParam(NS_LDAP_ADMIN_BINDPASSWD_P,
24668821SMichen.Chang@Sun.COM 	    (void ***)&adminPassword, &errorp);
24678821SMichen.Chang@Sun.COM 	if (retcode != 0) {
24688821SMichen.Chang@Sun.COM 		CLIENT_FPRINTF(stderr,
24698821SMichen.Chang@Sun.COM 		    gettext("Error %d while trying to retrieve "
24708821SMichen.Chang@Sun.COM 		    "adminPassword\n"), retcode);
24718821SMichen.Chang@Sun.COM 		rc = CLIENT_ERR_FAIL;
24728821SMichen.Chang@Sun.COM 		goto out;
24738821SMichen.Chang@Sun.COM 	}
24748821SMichen.Chang@Sun.COM 
24758821SMichen.Chang@Sun.COM 	if (mode_verbose) {
24768821SMichen.Chang@Sun.COM 		CLIENT_FPRINTF(stderr,
24778821SMichen.Chang@Sun.COM 		    gettext("admin DN: %s\n"),
24788821SMichen.Chang@Sun.COM 		    (adminDN && adminDN[0]) ? adminDN[0] : "NULL");
24798821SMichen.Chang@Sun.COM 		CLIENT_FPRINTF(stderr,
24808821SMichen.Chang@Sun.COM 		    gettext("admin password: %s\n"),
24818821SMichen.Chang@Sun.COM 		    (adminPassword && adminPassword[0]) ?
24828821SMichen.Chang@Sun.COM 		    adminPassword[0] : "NULL");
24838821SMichen.Chang@Sun.COM 	}
24848821SMichen.Chang@Sun.COM 
24858821SMichen.Chang@Sun.COM 	credSelf = 0;	/* flag to indicate if we have a credLevel of self */
24868821SMichen.Chang@Sun.COM 	for (counter = 0; credLevel && credLevel[counter] != NULL; counter++) {
24878821SMichen.Chang@Sun.COM 		if (mode_verbose)
24888821SMichen.Chang@Sun.COM 			CLIENT_FPRINTF(stderr,
24898821SMichen.Chang@Sun.COM 			    gettext("Credential level: %d\n"),
24908821SMichen.Chang@Sun.COM 			    *credLevel[counter]);
24918821SMichen.Chang@Sun.COM 		if (*credLevel[counter] == NS_LDAP_CRED_SELF) {
24928821SMichen.Chang@Sun.COM 			credSelf = 1;
24938821SMichen.Chang@Sun.COM 			break;
24948821SMichen.Chang@Sun.COM 		}
24958821SMichen.Chang@Sun.COM 	}
24968821SMichen.Chang@Sun.COM 
24978821SMichen.Chang@Sun.COM 	authSASLgss = 0;	/* flag for authMethod of SASL/gssapi */
24988821SMichen.Chang@Sun.COM 	for (counter = 0;
24998821SMichen.Chang@Sun.COM 	    authMethod && authMethod[counter] != NULL;
25008821SMichen.Chang@Sun.COM 	    counter++) {
25018821SMichen.Chang@Sun.COM 
25028821SMichen.Chang@Sun.COM 		if (mode_verbose)
25038821SMichen.Chang@Sun.COM 			CLIENT_FPRINTF(stderr,
25048821SMichen.Chang@Sun.COM 			    gettext("Authentication sasl mechanism: %d\n"),
25058821SMichen.Chang@Sun.COM 			    authMethod[counter]->saslmech);
25068821SMichen.Chang@Sun.COM 		if (authMethod[counter]->saslmech == NS_LDAP_SASL_GSSAPI) {
25078821SMichen.Chang@Sun.COM 			authSASLgss = 1;
25088821SMichen.Chang@Sun.COM 			break;
25098821SMichen.Chang@Sun.COM 		}
25108821SMichen.Chang@Sun.COM 	}
25118821SMichen.Chang@Sun.COM 
25128821SMichen.Chang@Sun.COM 	/* First, if we don't need adminDN/adminPassword then just return ok */
25138821SMichen.Chang@Sun.COM 	if (credSelf && authSASLgss) {
25148821SMichen.Chang@Sun.COM 		if (mode_verbose)
25158821SMichen.Chang@Sun.COM 			CLIENT_FPUTS(
25168821SMichen.Chang@Sun.COM 			    gettext("A credential Level of self and an "
25178821SMichen.Chang@Sun.COM 			    "authentication method of sasl/GSSAPI is "
25188821SMichen.Chang@Sun.COM 			    "configured, no adminDN/adminPassword "
25198821SMichen.Chang@Sun.COM 			    "is required.\n"), stderr);
25208821SMichen.Chang@Sun.COM 		rc = CLIENT_SUCCESS;
25218821SMichen.Chang@Sun.COM 		goto out;
25228821SMichen.Chang@Sun.COM 	}
25238821SMichen.Chang@Sun.COM 
25248821SMichen.Chang@Sun.COM 	/* Now let's check if we have the cred stuff we need */
25258821SMichen.Chang@Sun.COM 	if (adminDN == NULL || adminDN[0] == '\0') {
25268821SMichen.Chang@Sun.COM 		CLIENT_FPUTS(
25278821SMichen.Chang@Sun.COM 		    gettext("Shadow Update is enabled, but "
25288821SMichen.Chang@Sun.COM 		    "no adminDN is configured.\n"), stderr);
25298821SMichen.Chang@Sun.COM 		rc = CLIENT_ERR_CREDENTIAL;
25308821SMichen.Chang@Sun.COM 		goto out;
25318821SMichen.Chang@Sun.COM 	}
25328821SMichen.Chang@Sun.COM 
25338821SMichen.Chang@Sun.COM 	/* If we need adminPassword (prompt) */
25348821SMichen.Chang@Sun.COM 	if (adminPassword == NULL || adminPassword[0] == '\0') {
25358821SMichen.Chang@Sun.COM 		CLIENT_FPUTS(
25368821SMichen.Chang@Sun.COM 		    gettext("Shadow Update requires adminPassword\n"),
25378821SMichen.Chang@Sun.COM 		    stderr);
25388821SMichen.Chang@Sun.COM 		arglist->adminPassword = getpassphrase("admin Password:");
25398821SMichen.Chang@Sun.COM 		if (arglist->adminPassword == NULL) {
25408821SMichen.Chang@Sun.COM 			CLIENT_FPUTS(gettext("Unable to get admin password\n"),
25418821SMichen.Chang@Sun.COM 			    stderr);
25428821SMichen.Chang@Sun.COM 			rc = CLIENT_ERR_CREDENTIAL;
25438821SMichen.Chang@Sun.COM 			goto out;
25448821SMichen.Chang@Sun.COM 		}
25458821SMichen.Chang@Sun.COM 		LDAP_SET_PARAM(arglist->adminPassword,
25468821SMichen.Chang@Sun.COM 		    NS_LDAP_ADMIN_BINDPASSWD_P);
25478821SMichen.Chang@Sun.COM 		if (retcode != 0) {
25488821SMichen.Chang@Sun.COM 			CLIENT_FPUTS(
25498821SMichen.Chang@Sun.COM 			    gettext("setParam adminPassword failed.\n"),
25508821SMichen.Chang@Sun.COM 			    stderr);
25518821SMichen.Chang@Sun.COM 			rc = CLIENT_ERR_CREDENTIAL;
25528821SMichen.Chang@Sun.COM 			goto out;
25538821SMichen.Chang@Sun.COM 		}
25548821SMichen.Chang@Sun.COM 	}
25558821SMichen.Chang@Sun.COM 
25568821SMichen.Chang@Sun.COM 	rc = CLIENT_SUCCESS;
25578821SMichen.Chang@Sun.COM 
25588821SMichen.Chang@Sun.COM 	out:
25598821SMichen.Chang@Sun.COM 	if (enabled != NULL)
25608821SMichen.Chang@Sun.COM 		(void) __ns_ldap_freeParam((void ***)&enabled);
25618821SMichen.Chang@Sun.COM 	if (credLevel != NULL)
25628821SMichen.Chang@Sun.COM 		(void) __ns_ldap_freeParam((void ***)&credLevel);
25638821SMichen.Chang@Sun.COM 	if (authMethod != NULL)
25648821SMichen.Chang@Sun.COM 		(void) __ns_ldap_freeParam((void ***)&authMethod);
25658821SMichen.Chang@Sun.COM 	if (adminDN != NULL)
25668821SMichen.Chang@Sun.COM 		(void) __ns_ldap_freeParam((void ***)&adminDN);
25678821SMichen.Chang@Sun.COM 	if (adminPassword != NULL)
25688821SMichen.Chang@Sun.COM 		(void) __ns_ldap_freeParam((void ***)&adminPassword);
25698821SMichen.Chang@Sun.COM 
25708821SMichen.Chang@Sun.COM 	return (rc);
25718821SMichen.Chang@Sun.COM }
25728821SMichen.Chang@Sun.COM 
25738821SMichen.Chang@Sun.COM /*
25740Sstevel@tonic-gate  * try to restore the previous name space on this machine
25750Sstevel@tonic-gate  */
25760Sstevel@tonic-gate static int
recover(int saveState)25770Sstevel@tonic-gate recover(int saveState)
25780Sstevel@tonic-gate {
25790Sstevel@tonic-gate 	struct stat buf;
25800Sstevel@tonic-gate 	int stat_ret, retcode, fd;
25810Sstevel@tonic-gate 	int domain = 0, domainlen;
25820Sstevel@tonic-gate 	char yp_dir[BUFSIZE], yp_dir_back[BUFSIZE];
25830Sstevel@tonic-gate 	char name[BUFSIZ];
25840Sstevel@tonic-gate 	char *ldap_conf_file, *ldap_cred_file;
25850Sstevel@tonic-gate 	char ldap_file_back[BUFSIZE], ldap_cred_back[BUFSIZE];
25860Sstevel@tonic-gate 
25870Sstevel@tonic-gate 	/* If running as Sysid Install become a no-op */
25880Sstevel@tonic-gate 	if (sysid_install == B_TRUE)
25890Sstevel@tonic-gate 		return (CLIENT_SUCCESS);
25900Sstevel@tonic-gate 
25910Sstevel@tonic-gate 	stat_ret = stat(LDAP_RESTORE_DIR, &buf);
25920Sstevel@tonic-gate 	if (stat_ret != 0) {
25930Sstevel@tonic-gate 		CLIENT_FPUTS(
25946842Sth160488 		    gettext("Cannot recover.  No backup files "
25956842Sth160488 		    "found.\n"),
25966842Sth160488 		    stderr);
25970Sstevel@tonic-gate 		CLIENT_FPUTS(
25986842Sth160488 		    gettext("\t Either this machine was not initialized\n"),
25996842Sth160488 		    stderr);
26000Sstevel@tonic-gate 		CLIENT_FPUTS(
26016842Sth160488 		    gettext("\t by ldapclient or the backup files "
26026842Sth160488 		    "have been\n"),
26036842Sth160488 		    stderr);
26040Sstevel@tonic-gate 		CLIENT_FPUTS(
26056842Sth160488 		    gettext("\t removed manually or with an \"uninit\"\n"),
26066842Sth160488 		    stderr);
26070Sstevel@tonic-gate 		return (CLIENT_ERR_RESTORE);	/* invalid backup */
26080Sstevel@tonic-gate 	}
26090Sstevel@tonic-gate 
26100Sstevel@tonic-gate 	/*
26110Sstevel@tonic-gate 	 * Get domainname.  Allow no domainname for the case where "files"
26120Sstevel@tonic-gate 	 * config was backed up.
26130Sstevel@tonic-gate 	 */
26140Sstevel@tonic-gate 	stat_ret = stat(DOMAINNAME_BACK, &buf);
26150Sstevel@tonic-gate 	if (mode_verbose)
26160Sstevel@tonic-gate 		CLIENT_FPRINTF(stderr,
26176842Sth160488 		    gettext("recover: stat(%s)=%d\n"),
26186842Sth160488 		    DOMAINNAME_BACK, stat_ret);
26190Sstevel@tonic-gate 	if (stat_ret == 0) {
26200Sstevel@tonic-gate 		if (mode_verbose)
26210Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr,
26226842Sth160488 			    gettext("recover: open(%s)\n"),
26236842Sth160488 			    DOMAINNAME_BACK);
26240Sstevel@tonic-gate 		fd = open(DOMAINNAME_BACK, O_RDONLY);
26250Sstevel@tonic-gate 		if (mode_verbose)
26260Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr,
26276842Sth160488 			    gettext("recover: read(%s)\n"),
26286842Sth160488 			    DOMAINNAME_BACK);
26290Sstevel@tonic-gate 		domainlen = read(fd, &(name[0]), BUFSIZ-1);
26300Sstevel@tonic-gate 		(void) close(fd);
26310Sstevel@tonic-gate 		if (domainlen < 0) {
26320Sstevel@tonic-gate 			CLIENT_FPUTS(
26336842Sth160488 			    gettext("Cannot recover.  Cannot determine "
26346842Sth160488 			    "previous domain name.\n"),
26356842Sth160488 			    stderr);
26360Sstevel@tonic-gate 			return (CLIENT_ERR_RESTORE);	/* invalid backup */
26370Sstevel@tonic-gate 		} else 	{
26380Sstevel@tonic-gate 			char *ptr;
26390Sstevel@tonic-gate 
26400Sstevel@tonic-gate 			ptr = strchr(&(name[0]), '\n');
26410Sstevel@tonic-gate 			if (ptr != NULL)
26420Sstevel@tonic-gate 				*ptr = '\0';
26430Sstevel@tonic-gate 			else
26440Sstevel@tonic-gate 				name[domainlen] = '\0';
26450Sstevel@tonic-gate 
26460Sstevel@tonic-gate 			if (mode_verbose)
26470Sstevel@tonic-gate 				CLIENT_FPRINTF(stderr,
26486842Sth160488 				    gettext("recover: old domainname "
26496842Sth160488 				    "\"%s\"\n"), name);
26500Sstevel@tonic-gate 
26510Sstevel@tonic-gate 			if (strlen(name) == 0)
26520Sstevel@tonic-gate 				domain = 0;
26530Sstevel@tonic-gate 			else
26540Sstevel@tonic-gate 				domain = 1;	/* flag that we have domain */
26550Sstevel@tonic-gate 
26560Sstevel@tonic-gate 		}
26570Sstevel@tonic-gate 	}
26580Sstevel@tonic-gate 
26590Sstevel@tonic-gate 
26600Sstevel@tonic-gate 	/*
26610Sstevel@tonic-gate 	 * we can recover at this point
26620Sstevel@tonic-gate 	 * remove LDAP config files before restore
26630Sstevel@tonic-gate 	 */
26640Sstevel@tonic-gate 	(void) unlink(NSCONFIGFILE);
26650Sstevel@tonic-gate 	(void) unlink(NSCREDFILE);
26660Sstevel@tonic-gate 
26670Sstevel@tonic-gate 	ldap_conf_file = strrchr(NSCONFIGFILE, '/') + 1;
26680Sstevel@tonic-gate 	ldap_cred_file = strrchr(NSCREDFILE, '/') + 1;
26690Sstevel@tonic-gate 
26700Sstevel@tonic-gate 	(void) strlcpy(ldap_file_back, LDAP_RESTORE_DIR "/", BUFSIZE);
26710Sstevel@tonic-gate 	(void) strlcat(ldap_file_back, ldap_conf_file, BUFSIZE);
26720Sstevel@tonic-gate 
26730Sstevel@tonic-gate 	stat_ret = stat(ldap_file_back, &buf);
26740Sstevel@tonic-gate 	if (mode_verbose)
26750Sstevel@tonic-gate 		CLIENT_FPRINTF(stderr,
26766842Sth160488 		    gettext("recover: stat(%s)=%d\n"),
26776842Sth160488 		    ldap_file_back, stat_ret);
26780Sstevel@tonic-gate 	if (stat_ret == 0) {
26790Sstevel@tonic-gate 		if (saveState)
26800Sstevel@tonic-gate 			gStartLdap = START_UNINIT;
26810Sstevel@tonic-gate 		retcode = file_move(ldap_file_back, NSCONFIGFILE);
26820Sstevel@tonic-gate 		if (mode_verbose)
26830Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr,
26846842Sth160488 			    gettext("recover: file_move(%s, %s)=%d\n"),
26856842Sth160488 			    ldap_file_back, NSCONFIGFILE, retcode);
26860Sstevel@tonic-gate 		if (retcode != 0)
26870Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr,
26886842Sth160488 			    gettext("recover: file_move(%s, %s) failed\n"),
26896842Sth160488 			    ldap_file_back, NSCONFIGFILE);
26900Sstevel@tonic-gate 	}
26910Sstevel@tonic-gate 
26920Sstevel@tonic-gate 	(void) strlcpy(ldap_cred_back, LDAP_RESTORE_DIR "/", BUFSIZE);
26930Sstevel@tonic-gate 	(void) strlcat(ldap_cred_back, ldap_cred_file, BUFSIZE);
26940Sstevel@tonic-gate 
26950Sstevel@tonic-gate 	stat_ret = stat(ldap_cred_back, &buf);
26960Sstevel@tonic-gate 	if (mode_verbose)
26970Sstevel@tonic-gate 		CLIENT_FPRINTF(stderr,
26986842Sth160488 		    gettext("recover: stat(%s)=%d\n"),
26996842Sth160488 		    ldap_cred_back, stat_ret);
27000Sstevel@tonic-gate 	if (stat_ret == 0) {
27010Sstevel@tonic-gate 		retcode = file_move(ldap_cred_back, NSCREDFILE);
27020Sstevel@tonic-gate 		if (mode_verbose)
27030Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr,
27046842Sth160488 			    gettext("recover: file_move(%s, %s)=%d\n"),
27056842Sth160488 			    ldap_cred_back, NSCREDFILE, retcode);
27060Sstevel@tonic-gate 		if (retcode != 0)
27070Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr,
27086842Sth160488 			    gettext("recover: file_move(%s, %s) failed\n"),
27096842Sth160488 			    ldap_cred_back, NSCREDFILE);
27100Sstevel@tonic-gate 	}
27110Sstevel@tonic-gate 
27120Sstevel@tonic-gate 	/* Check for recovery of NIS(YP) if we have a domainname */
27130Sstevel@tonic-gate 	if (domain) {
27140Sstevel@tonic-gate 		/* "name" would have to be huge for this, but just in case */
27150Sstevel@tonic-gate 		if (strlen(name) >= (BUFSIZE - strlen(LDAP_RESTORE_DIR)))
27160Sstevel@tonic-gate 			return (CLIENT_ERR_FAIL);
27170Sstevel@tonic-gate 		if (strlen(name) >= (BUFSIZE - strlen(YP_BIND_DIR)))
27180Sstevel@tonic-gate 			return (CLIENT_ERR_FAIL);
27190Sstevel@tonic-gate 
27200Sstevel@tonic-gate 		(void) strlcpy(yp_dir_back, LDAP_RESTORE_DIR "/", BUFSIZE);
27210Sstevel@tonic-gate 		(void) strlcat(yp_dir_back, name, BUFSIZE);
27220Sstevel@tonic-gate 		stat_ret = stat(yp_dir_back, &buf);
27230Sstevel@tonic-gate 		if (mode_verbose)
27240Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr,
27256842Sth160488 			    gettext("recover: stat(%s)=%d\n"),
27266842Sth160488 			    yp_dir_back, stat_ret);
27270Sstevel@tonic-gate 		if (stat_ret == 0) {
27280Sstevel@tonic-gate 			(void) strlcpy(yp_dir, YP_BIND_DIR "/", BUFSIZE);
27290Sstevel@tonic-gate 			(void) strlcat(yp_dir, name, BUFSIZE);
27300Sstevel@tonic-gate 			retcode = file_move(yp_dir_back, yp_dir);
27310Sstevel@tonic-gate 			if (mode_verbose)
27320Sstevel@tonic-gate 				CLIENT_FPRINTF(stderr,
27336842Sth160488 				    gettext("recover: file_move(%s, "
27346842Sth160488 				    "%s)=%d\n"),
27356842Sth160488 				    yp_dir_back, yp_dir, retcode);
27360Sstevel@tonic-gate 			if (retcode != 0) {
27370Sstevel@tonic-gate 				CLIENT_FPRINTF(stderr,
27386842Sth160488 				    gettext("recover: file_move(%s, "
27396842Sth160488 				    "%s) failed!\n"),
27406842Sth160488 				    yp_dir_back, yp_dir);
27410Sstevel@tonic-gate 			} else {
27420Sstevel@tonic-gate 				if (saveState)
27430Sstevel@tonic-gate 					gStartYp = START_UNINIT;
27440Sstevel@tonic-gate 			}
27450Sstevel@tonic-gate 		}
27460Sstevel@tonic-gate 	}
27470Sstevel@tonic-gate 
27480Sstevel@tonic-gate 	/* restore machine configuration */
27490Sstevel@tonic-gate 	stat_ret = stat(NSSWITCH_BACK, &buf);
27500Sstevel@tonic-gate 	if (mode_verbose)
27510Sstevel@tonic-gate 		CLIENT_FPRINTF(stderr,
27526842Sth160488 		    gettext("recover: stat(%s)=%d\n"),
27536842Sth160488 		    NSSWITCH_BACK, stat_ret);
27540Sstevel@tonic-gate 	if (stat_ret == 0) {
27550Sstevel@tonic-gate 		retcode = file_move(NSSWITCH_BACK, NSSWITCH_CONF);
27560Sstevel@tonic-gate 		if (mode_verbose)
27570Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr,
27586842Sth160488 			    gettext("recover: file_move(%s, %s)=%d\n"),
27596842Sth160488 			    NSSWITCH_BACK, NSSWITCH_CONF, retcode);
27600Sstevel@tonic-gate 		if (retcode != 0)
27610Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr,
27626842Sth160488 			    gettext("recover: file_move(%s, %s) failed\n"),
27636842Sth160488 			    NSSWITCH_BACK, NSSWITCH_CONF);
27640Sstevel@tonic-gate 	}
27650Sstevel@tonic-gate 
27660Sstevel@tonic-gate 	stat_ret = stat(DOMAINNAME_BACK, &buf);
27670Sstevel@tonic-gate 	if (mode_verbose)
27680Sstevel@tonic-gate 		CLIENT_FPRINTF(stderr,
27696842Sth160488 		    gettext("recover: stat(%s)=%d\n"),
27706842Sth160488 		    DOMAINNAME_BACK, stat_ret);
27710Sstevel@tonic-gate 	if (stat_ret == 0) {
27720Sstevel@tonic-gate 		retcode = file_move(DOMAINNAME_BACK, DOMAINNAME);
27730Sstevel@tonic-gate 		if (mode_verbose)
27740Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr,
27756842Sth160488 			    gettext("recover: file_move(%s, %s)=%d\n"),
27766842Sth160488 			    DOMAINNAME_BACK, DOMAINNAME, retcode);
27770Sstevel@tonic-gate 		if (retcode != 0)
27780Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr,
27796842Sth160488 			    gettext("recover: file_move(%s, %s) failed\n"),
27806842Sth160488 			    DOMAINNAME_BACK, DOMAINNAME);
27810Sstevel@tonic-gate 	}
27820Sstevel@tonic-gate 
27830Sstevel@tonic-gate 	retcode = rmdir(LDAP_RESTORE_DIR);
27840Sstevel@tonic-gate 	if (retcode != 0) {
27850Sstevel@tonic-gate 		CLIENT_FPRINTF(stderr,
27866842Sth160488 		    gettext("Error removing \"%s\" directory.\n"),
27876842Sth160488 		    LDAP_RESTORE_DIR);
27880Sstevel@tonic-gate 	}
27890Sstevel@tonic-gate 
27900Sstevel@tonic-gate 	return (CLIENT_SUCCESS);
27910Sstevel@tonic-gate }
27920Sstevel@tonic-gate 
27930Sstevel@tonic-gate /*
27940Sstevel@tonic-gate  * try to save the current state of this machine.
27950Sstevel@tonic-gate  * this just overwrites any old saved configration files.
27960Sstevel@tonic-gate  *
27970Sstevel@tonic-gate  * This function should only be called after network services have been stopped.
27980Sstevel@tonic-gate  *
27990Sstevel@tonic-gate  * Returns 0 on successful save
28000Sstevel@tonic-gate  * Otherwise returns -1
28010Sstevel@tonic-gate  */
28020Sstevel@tonic-gate static int
file_backup(void)28030Sstevel@tonic-gate file_backup(void)
28040Sstevel@tonic-gate {
28050Sstevel@tonic-gate 	struct stat buf;
28060Sstevel@tonic-gate 	int domain_stat, conf_stat, ldap_stat;
2807*11262SRajagopal.Andra@Sun.COM 	int yp_stat, restore_stat;
28080Sstevel@tonic-gate 	int retcode, namelen, ret;
28090Sstevel@tonic-gate 	char yp_dir[BUFSIZ], yp_dir_back[BUFSIZ];
28100Sstevel@tonic-gate 	char name[BUFSIZ];
28110Sstevel@tonic-gate 	char *ldap_conf_file, *ldap_cred_file;
28120Sstevel@tonic-gate 	char ldap_file_back[BUFSIZE], ldap_cred_back[BUFSIZE];
28130Sstevel@tonic-gate 
28140Sstevel@tonic-gate 	ret = CLIENT_SUCCESS;
28150Sstevel@tonic-gate 	/* If running as Sysid Install become a no-op */
28160Sstevel@tonic-gate 	if (sysid_install == B_TRUE)
28170Sstevel@tonic-gate 		return (CLIENT_SUCCESS);
28180Sstevel@tonic-gate 
28190Sstevel@tonic-gate 	/* If existing backup files, clear for this run */
28200Sstevel@tonic-gate 	restore_stat = stat(LDAP_RESTORE_DIR, &buf);
28210Sstevel@tonic-gate 	if (restore_stat == 0) {
28220Sstevel@tonic-gate 		if (mode_verbose) {
28230Sstevel@tonic-gate 			CLIENT_FPUTS(
28246842Sth160488 			    gettext("Removing existing restore "
28256842Sth160488 			    "directory\n"),
28266842Sth160488 			    stderr);
28270Sstevel@tonic-gate 		}
28280Sstevel@tonic-gate 		(void) system("/bin/rm -fr " LDAP_RESTORE_DIR);
28290Sstevel@tonic-gate 		restore_stat = stat(LDAP_RESTORE_DIR, &buf);
28300Sstevel@tonic-gate 		if (restore_stat == 0) {
28310Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr,
28326842Sth160488 			    gettext("Unable to remove backup "
28336842Sth160488 			    "directory (%s)\n"),
28346842Sth160488 			    LDAP_RESTORE_DIR);
28350Sstevel@tonic-gate 			return (CLIENT_ERR_RESTORE);
28360Sstevel@tonic-gate 		}
28370Sstevel@tonic-gate 	}
28380Sstevel@tonic-gate 
28390Sstevel@tonic-gate 	retcode = mkdir(LDAP_RESTORE_DIR, 0755);
28400Sstevel@tonic-gate 	if (retcode != 0) {
28410Sstevel@tonic-gate 		CLIENT_FPRINTF(stderr,
28426842Sth160488 		    gettext("file_backup: Failed to make %s backup "
28436842Sth160488 		    "directory. mkdir=%d\n"),
28446842Sth160488 		    LDAP_RESTORE_DIR, retcode);
28450Sstevel@tonic-gate 		return (CLIENT_ERR_FAIL);
28460Sstevel@tonic-gate 	}
28470Sstevel@tonic-gate 
28480Sstevel@tonic-gate 	conf_stat = stat(NSSWITCH_CONF, &buf);
28490Sstevel@tonic-gate 	if (mode_verbose)
28500Sstevel@tonic-gate 		CLIENT_FPRINTF(stderr,
28516842Sth160488 		    gettext("file_backup: stat(%s)=%d\n"),
28526842Sth160488 		    NSSWITCH_CONF, conf_stat);
28530Sstevel@tonic-gate 	if (conf_stat == 0) {
28540Sstevel@tonic-gate 		if (mode_verbose)
28550Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr,
28566842Sth160488 			    gettext("file_backup: (%s -> %s)\n"),
28576842Sth160488 			    NSSWITCH_CONF, NSSWITCH_BACK);
28580Sstevel@tonic-gate 		retcode = file_move(NSSWITCH_CONF, NSSWITCH_BACK);
28590Sstevel@tonic-gate 		if (retcode != 0) {
28600Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr,
28616842Sth160488 			    gettext("file_backup: file_move(%s, %s) failed "
28626842Sth160488 			    "with %d\n"),
28636842Sth160488 			    NSSWITCH_CONF, NSSWITCH_BACK, retcode);
28640Sstevel@tonic-gate 			ret = CLIENT_ERR_RENAME;
28650Sstevel@tonic-gate 		}
28660Sstevel@tonic-gate 	} else {
28670Sstevel@tonic-gate 		if (mode_verbose)
28680Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr,
28696842Sth160488 			    gettext("file_backup: No %s file.\n"),
28706842Sth160488 			    NSSWITCH_CONF);
28710Sstevel@tonic-gate 	}
28720Sstevel@tonic-gate 
28730Sstevel@tonic-gate 	domain_stat = stat(DOMAINNAME, &buf);
28740Sstevel@tonic-gate 	if (mode_verbose)
28750Sstevel@tonic-gate 		CLIENT_FPRINTF(stderr,
28766842Sth160488 		    gettext("file_backup: stat(%s)=%d\n"),
28776842Sth160488 		    DOMAINNAME, domain_stat);
28780Sstevel@tonic-gate 	if ((domain_stat == 0) && (buf.st_size > 0)) {
28790Sstevel@tonic-gate 		if (mode_verbose)
28800Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr,
28816842Sth160488 			    gettext("file_backup: (%s -> %s)\n"),
28826842Sth160488 			    DOMAINNAME, DOMAINNAME_BACK);
28830Sstevel@tonic-gate 		retcode = file_move(DOMAINNAME, DOMAINNAME_BACK);
28840Sstevel@tonic-gate 		if (retcode != 0) {
28850Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr,
28866842Sth160488 			    gettext("file_backup: file_move(%s, %s) failed "
28876842Sth160488 			    "with %d\n"),
28886842Sth160488 			    DOMAINNAME, DOMAINNAME_BACK, retcode);
28890Sstevel@tonic-gate 			ret = CLIENT_ERR_RENAME;
28900Sstevel@tonic-gate 		}
28910Sstevel@tonic-gate 	} else {
28920Sstevel@tonic-gate 		if (mode_verbose)
28930Sstevel@tonic-gate 			if (domain_stat != 0) {
28940Sstevel@tonic-gate 				CLIENT_FPRINTF(stderr,
28956842Sth160488 				    gettext("file_backup: No %s file.\n"),
28966842Sth160488 				    DOMAINNAME);
28970Sstevel@tonic-gate 			} else {
28980Sstevel@tonic-gate 				CLIENT_FPRINTF(stderr,
28996842Sth160488 				    gettext("file_backup: Empty %s "
29006842Sth160488 				    "file.\n"),
29016842Sth160488 				    DOMAINNAME);
29020Sstevel@tonic-gate 			}
29030Sstevel@tonic-gate 	}
29040Sstevel@tonic-gate 
29050Sstevel@tonic-gate 	namelen = BUFSIZ;
29060Sstevel@tonic-gate 	(void) sysinfo(SI_SRPC_DOMAIN, &(name[0]), namelen);
29070Sstevel@tonic-gate 	namelen = strlen(name);
29080Sstevel@tonic-gate 
29090Sstevel@tonic-gate 	if (mode_verbose)
29100Sstevel@tonic-gate 		CLIENT_FPRINTF(stderr,
29116842Sth160488 		    gettext("file_backup: nis domain is \"%s\"\n"),
29126842Sth160488 		    (namelen > 0) ? name : "EMPTY");
29130Sstevel@tonic-gate 	/* check for domain name if not set cannot save NIS(YP) state */
29140Sstevel@tonic-gate 	if (namelen > 0) {
29150Sstevel@tonic-gate 		/* moving /var/yp/binding will cause ypbind to core dump */
29160Sstevel@tonic-gate 		(void) strlcpy(yp_dir, YP_BIND_DIR "/", BUFSIZE);
29170Sstevel@tonic-gate 		(void) strlcat(yp_dir, name, BUFSIZE);
29180Sstevel@tonic-gate 		yp_stat = stat(yp_dir, &buf);
29190Sstevel@tonic-gate 		if (mode_verbose)
29200Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr,
29216842Sth160488 			    gettext("file_backup: stat(%s)=%d\n"),
29226842Sth160488 			    yp_dir, yp_stat);
29230Sstevel@tonic-gate 		if (yp_stat == 0) {
29240Sstevel@tonic-gate 			(void) strlcpy(yp_dir_back, LDAP_RESTORE_DIR "/",
29256842Sth160488 			    BUFSIZE);
29260Sstevel@tonic-gate 			(void) strlcat(yp_dir_back, name, BUFSIZE);
29270Sstevel@tonic-gate 			if (mode_verbose)
29280Sstevel@tonic-gate 				CLIENT_FPRINTF(stderr,
29296842Sth160488 				    gettext("file_backup: (%s -> %s)\n"),
29306842Sth160488 				    yp_dir, yp_dir_back);
29310Sstevel@tonic-gate 			retcode = file_move(yp_dir, yp_dir_back);
29320Sstevel@tonic-gate 			if (retcode != 0) {
29330Sstevel@tonic-gate 				CLIENT_FPRINTF(stderr,
29346842Sth160488 				    gettext("file_backup: file_move(%s, %s)"
29356842Sth160488 				    " failed with %d\n"),
29366842Sth160488 				    yp_dir, yp_dir_back, retcode);
29370Sstevel@tonic-gate 				ret = CLIENT_ERR_RENAME;
29380Sstevel@tonic-gate 			}
29390Sstevel@tonic-gate 		} else {
29400Sstevel@tonic-gate 			if (mode_verbose)
29410Sstevel@tonic-gate 				CLIENT_FPRINTF(stderr,
29426842Sth160488 				    gettext("file_backup: No %s "
29436842Sth160488 				    "directory.\n"), yp_dir);
29440Sstevel@tonic-gate 		}
29450Sstevel@tonic-gate 	}
29460Sstevel@tonic-gate 
29470Sstevel@tonic-gate 
29480Sstevel@tonic-gate 	/* point to file name, not path delim (/) */
29490Sstevel@tonic-gate 	ldap_conf_file = strrchr(NSCONFIGFILE, '/') + 1;
29500Sstevel@tonic-gate 	ldap_cred_file = strrchr(NSCREDFILE, '/') + 1;
29510Sstevel@tonic-gate 
29520Sstevel@tonic-gate 	ldap_stat = stat(NSCONFIGFILE, &buf);
29530Sstevel@tonic-gate 	if (mode_verbose)
29540Sstevel@tonic-gate 		CLIENT_FPRINTF(stderr,
29556842Sth160488 		    gettext("file_backup: stat(%s)=%d\n"),
29566842Sth160488 		    NSCONFIGFILE, ldap_stat);
29570Sstevel@tonic-gate 	if (ldap_stat == 0) {
29580Sstevel@tonic-gate 		(void) strlcpy(ldap_file_back, LDAP_RESTORE_DIR "/", BUFSIZE);
29590Sstevel@tonic-gate 		(void) strlcat(ldap_file_back, ldap_conf_file, BUFSIZE);
29600Sstevel@tonic-gate 		if (mode_verbose)
29610Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr,
29626842Sth160488 			    gettext("file_backup: (%s -> %s)\n"),
29636842Sth160488 			    NSCONFIGFILE, ldap_file_back);
29640Sstevel@tonic-gate 		retcode = file_move(NSCONFIGFILE, ldap_file_back);
29650Sstevel@tonic-gate 		if (retcode != 0) {
29660Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr,
29676842Sth160488 			    gettext("file_backup: file_move(%s, %s) failed "
29686842Sth160488 			    "with %d\n"),
29696842Sth160488 			    NSCONFIGFILE, ldap_file_back, retcode);
29700Sstevel@tonic-gate 			ret = CLIENT_ERR_RENAME;
29710Sstevel@tonic-gate 		}
29720Sstevel@tonic-gate 
29730Sstevel@tonic-gate 		(void) strlcpy(ldap_cred_back, LDAP_RESTORE_DIR "/", BUFSIZE);
29740Sstevel@tonic-gate 		(void) strlcat(ldap_cred_back, ldap_cred_file, BUFSIZE);
29750Sstevel@tonic-gate 		if (mode_verbose)
29760Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr,
29776842Sth160488 			    gettext("file_backup: (%s -> %s)\n"),
29786842Sth160488 			    NSCREDFILE, ldap_cred_back);
29790Sstevel@tonic-gate 		retcode = file_move(NSCREDFILE, ldap_cred_back);
29800Sstevel@tonic-gate 		if (retcode != 0) {
29810Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr,
29826842Sth160488 			    gettext("file_backup: file_move(%s, %s) failed "
29836842Sth160488 			    "with %d\n"),
29846842Sth160488 			    NSCREDFILE, ldap_cred_back, retcode);
29850Sstevel@tonic-gate 			ret = CLIENT_ERR_RENAME;
29860Sstevel@tonic-gate 		}
29870Sstevel@tonic-gate 	} else {
29880Sstevel@tonic-gate 		if (mode_verbose)
29890Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr,
29906842Sth160488 			    gettext("file_backup: No %s file.\n"),
29916842Sth160488 			    NSCONFIGFILE);
29920Sstevel@tonic-gate 	}
29930Sstevel@tonic-gate 
29940Sstevel@tonic-gate 	return (ret);
29950Sstevel@tonic-gate }
29960Sstevel@tonic-gate 
29970Sstevel@tonic-gate /*
29980Sstevel@tonic-gate  * mod_backup()
29990Sstevel@tonic-gate  *
30000Sstevel@tonic-gate  * This function is used to temporily backup the LDAP client files in /var/ldap
30010Sstevel@tonic-gate  * that the "mod" operation needs to update.  If an error occurs then the
30020Sstevel@tonic-gate  * function mod_recover() can be invoke to recover the unmodified files.
30030Sstevel@tonic-gate  */
30040Sstevel@tonic-gate static int
mod_backup(void)30050Sstevel@tonic-gate mod_backup(void)
30060Sstevel@tonic-gate {
30070Sstevel@tonic-gate 	int rc;
30080Sstevel@tonic-gate 	int retcode = CLIENT_SUCCESS;
30090Sstevel@tonic-gate 
30100Sstevel@tonic-gate 	rc = system(CMD_CP " " NSCONFIGFILE " " NSCONFIGFILE ".mod");
30110Sstevel@tonic-gate 	retcode += rc;
30120Sstevel@tonic-gate 	if (mode_verbose)
30130Sstevel@tonic-gate 		CLIENT_FPRINTF(stderr,
30140Sstevel@tonic-gate 		    gettext("mod_backup: backup %s for %s\n"),
30150Sstevel@tonic-gate 		    rc ? "failed" : "successful", NSCONFIGFILE);
30160Sstevel@tonic-gate 
30170Sstevel@tonic-gate 	rc = system(CMD_CP " " NSCREDFILE " " NSCREDFILE ".mod");
30180Sstevel@tonic-gate 	retcode += rc;
30190Sstevel@tonic-gate 	if (mode_verbose)
30200Sstevel@tonic-gate 		CLIENT_FPRINTF(stderr,
30210Sstevel@tonic-gate 		    gettext("mod_backup: backup %s for %s\n"),
30220Sstevel@tonic-gate 		    rc ? "failed" : "successful", NSCREDFILE);
30230Sstevel@tonic-gate 
30240Sstevel@tonic-gate 	rc = system(CMD_CP " " DOMAINNAME " " DOMAINNAME ".mod");
30250Sstevel@tonic-gate 	retcode += rc;
30260Sstevel@tonic-gate 	if (mode_verbose)
30270Sstevel@tonic-gate 		CLIENT_FPRINTF(stderr,
30280Sstevel@tonic-gate 		    gettext("mod_backup: backup %s for %s\n"),
30290Sstevel@tonic-gate 		    rc ? "failed" : "successful", DOMAINNAME);
30300Sstevel@tonic-gate 
30310Sstevel@tonic-gate 	if (retcode != CLIENT_SUCCESS)
30320Sstevel@tonic-gate 		retcode = CLIENT_ERR_RENAME;
30330Sstevel@tonic-gate 	return (retcode);
30340Sstevel@tonic-gate }
30350Sstevel@tonic-gate 
30360Sstevel@tonic-gate /*
30370Sstevel@tonic-gate  * mod_recover()
30380Sstevel@tonic-gate  *
30390Sstevel@tonic-gate  * This function is used to recover the temporily backed up files by
30400Sstevel@tonic-gate  * the mod_backup() function if an error occurs during the "mod"
30410Sstevel@tonic-gate  * operation.
30420Sstevel@tonic-gate  */
30430Sstevel@tonic-gate static int
mod_recover(void)30440Sstevel@tonic-gate mod_recover(void)
30450Sstevel@tonic-gate {
30460Sstevel@tonic-gate 	int rc;
30470Sstevel@tonic-gate 	int retcode = CLIENT_SUCCESS;
30480Sstevel@tonic-gate 
30490Sstevel@tonic-gate 	rc = system(CMD_MV " " NSCONFIGFILE ".mod " NSCONFIGFILE);
30500Sstevel@tonic-gate 	retcode += rc;
30510Sstevel@tonic-gate 	if (mode_verbose)
30520Sstevel@tonic-gate 		CLIENT_FPRINTF(stderr,
30530Sstevel@tonic-gate 		    gettext("mod_recover: recovery %s for %s\n"),
30540Sstevel@tonic-gate 		    rc ? "failed" : "successful", NSCONFIGFILE);
30550Sstevel@tonic-gate 
30560Sstevel@tonic-gate 	rc = system(CMD_MV " " NSCREDFILE ".mod " NSCREDFILE);
30570Sstevel@tonic-gate 	retcode += rc;
30580Sstevel@tonic-gate 	if (mode_verbose)
30590Sstevel@tonic-gate 		CLIENT_FPRINTF(stderr,
30600Sstevel@tonic-gate 		    gettext("mod_recover: recovery %s for %s\n"),
30610Sstevel@tonic-gate 		    rc ? "failed" : "successful", NSCREDFILE);
30620Sstevel@tonic-gate 
30630Sstevel@tonic-gate 	rc = system(CMD_MV " " DOMAINNAME ".mod " DOMAINNAME);
30640Sstevel@tonic-gate 	retcode += rc;
30650Sstevel@tonic-gate 	if (mode_verbose)
30660Sstevel@tonic-gate 		CLIENT_FPRINTF(stderr,
30670Sstevel@tonic-gate 		    gettext("mod_recover: recovery %s for %s\n"),
30680Sstevel@tonic-gate 		    rc ? "failed" : "successful", DOMAINNAME);
30690Sstevel@tonic-gate 
30700Sstevel@tonic-gate 	if (retcode != CLIENT_SUCCESS)
30710Sstevel@tonic-gate 		retcode = CLIENT_ERR_RENAME;
30720Sstevel@tonic-gate 	return (retcode);
30730Sstevel@tonic-gate }
30740Sstevel@tonic-gate 
30750Sstevel@tonic-gate /*
30760Sstevel@tonic-gate  * mod_cleanup()
30770Sstevel@tonic-gate  *
30780Sstevel@tonic-gate  * This function removes the .mod files in /var/ldap.
30790Sstevel@tonic-gate  */
30800Sstevel@tonic-gate static void
mod_cleanup(void)30810Sstevel@tonic-gate mod_cleanup(void)
30820Sstevel@tonic-gate {
30830Sstevel@tonic-gate 	(void) system(CMD_RM " " NSCONFIGFILE ".mod " TO_DEV_NULL);
30840Sstevel@tonic-gate 	(void) system(CMD_RM " " NSCREDFILE ".mod " TO_DEV_NULL);
30850Sstevel@tonic-gate 	(void) system(CMD_RM " " DOMAINNAME ".mod " TO_DEV_NULL);
30860Sstevel@tonic-gate }
30870Sstevel@tonic-gate 
30880Sstevel@tonic-gate #define	MAX_DN_ARRAY 100
30890Sstevel@tonic-gate #define	LDAP_NAMINGCONTEXTS	"namingcontexts"
30900Sstevel@tonic-gate 
30910Sstevel@tonic-gate static multival_t *
multival_new()30920Sstevel@tonic-gate multival_new()
30930Sstevel@tonic-gate {
30940Sstevel@tonic-gate 	multival_t *hold;
30950Sstevel@tonic-gate 
30960Sstevel@tonic-gate 	hold = calloc(1, sizeof (multival_t));
30970Sstevel@tonic-gate 	if (hold == NULL) {
30980Sstevel@tonic-gate 		CLIENT_FPUTS(
30996842Sth160488 		    gettext("multival_new: Memory allocation error\n"),
31006842Sth160488 		    stderr);
31010Sstevel@tonic-gate 	}
31020Sstevel@tonic-gate 	return (hold);	/* NULL -> error */
31030Sstevel@tonic-gate }
31040Sstevel@tonic-gate 
31050Sstevel@tonic-gate static int
multival_add(multival_t * list,char * opt)31060Sstevel@tonic-gate multival_add(multival_t *list, char *opt)
31070Sstevel@tonic-gate {
31080Sstevel@tonic-gate 	if (opt == NULL) {
31090Sstevel@tonic-gate 		CLIENT_FPUTS(
31106842Sth160488 		    gettext("Empty value passed to multival_add\n"),
31116842Sth160488 		    stderr);
31120Sstevel@tonic-gate 		return (CLIENT_ERR_FAIL);
31130Sstevel@tonic-gate 	}
31140Sstevel@tonic-gate 
31150Sstevel@tonic-gate 	if (list->count == 0) {
31160Sstevel@tonic-gate 		list->optlist = (char **)malloc(sizeof (char **));
31170Sstevel@tonic-gate 	} else {
31180Sstevel@tonic-gate 		list->optlist = (char **)realloc(list->optlist,
31196842Sth160488 		    (list->count + 1) * sizeof (char **));
31200Sstevel@tonic-gate 	}
31210Sstevel@tonic-gate 
31220Sstevel@tonic-gate 	if (list->optlist == NULL) {
31230Sstevel@tonic-gate 		CLIENT_FPUTS(gettext("Error allocating memory\n"), stderr);
31240Sstevel@tonic-gate 		return (CLIENT_ERR_MEMORY);	/* 0 is success */
31250Sstevel@tonic-gate 	}
31260Sstevel@tonic-gate 
31270Sstevel@tonic-gate 	list->optlist[list->count] = opt;
31280Sstevel@tonic-gate 	list->count++;
31290Sstevel@tonic-gate 
31300Sstevel@tonic-gate 	return (CLIENT_SUCCESS);
31310Sstevel@tonic-gate }
31320Sstevel@tonic-gate 
31330Sstevel@tonic-gate static void
multival_free(multival_t * list)31340Sstevel@tonic-gate multival_free(multival_t *list)
31350Sstevel@tonic-gate {
31360Sstevel@tonic-gate 	if (list == NULL)
31370Sstevel@tonic-gate 		return;
31380Sstevel@tonic-gate 
31390Sstevel@tonic-gate 	if (list->optlist != NULL)
31400Sstevel@tonic-gate 		free(list->optlist);
31410Sstevel@tonic-gate 	free(list);
31420Sstevel@tonic-gate }
31430Sstevel@tonic-gate 
31440Sstevel@tonic-gate static clientopts_t *
clientopts_new()31450Sstevel@tonic-gate clientopts_new()
31460Sstevel@tonic-gate {
31470Sstevel@tonic-gate 	clientopts_t *hold;
31480Sstevel@tonic-gate 
31490Sstevel@tonic-gate 	hold = calloc(1, sizeof (clientopts_t));
31500Sstevel@tonic-gate 	if (NULL == hold) {
31510Sstevel@tonic-gate 		CLIENT_FPUTS(gettext("Error allocating memory for "
31526842Sth160488 		    "clientopts structure\n"), stderr);
31530Sstevel@tonic-gate 		return (hold);	/* NULL -> error */
31540Sstevel@tonic-gate 	}
31550Sstevel@tonic-gate 
31560Sstevel@tonic-gate 	hold->serviceAuthenticationMethod = multival_new();
31570Sstevel@tonic-gate 	if (NULL == hold->serviceAuthenticationMethod) {
31580Sstevel@tonic-gate 		CLIENT_FPUTS(gettext("Error allocating memory for "
31596842Sth160488 		    "serviceAuthenticationMethod\n"), stderr);
31600Sstevel@tonic-gate 		free(hold);
31610Sstevel@tonic-gate 		return (NULL);	/* NULL -> error */
31620Sstevel@tonic-gate 	}
31630Sstevel@tonic-gate 
31640Sstevel@tonic-gate 	hold->serviceCredentialLevel = multival_new();
31650Sstevel@tonic-gate 	if (NULL == hold->serviceCredentialLevel) {
31660Sstevel@tonic-gate 		CLIENT_FPUTS(gettext("Error allocating memory for "
31676842Sth160488 		    "serviceCredentialLevel\n"), stderr);
31680Sstevel@tonic-gate 		multival_free(hold->serviceAuthenticationMethod);
31690Sstevel@tonic-gate 		free(hold);
31700Sstevel@tonic-gate 		return (NULL);	/* NULL -> error */
31710Sstevel@tonic-gate 	}
31720Sstevel@tonic-gate 
31730Sstevel@tonic-gate 	hold->objectclassMap = multival_new();
31740Sstevel@tonic-gate 	if (NULL == hold->objectclassMap) {
31750Sstevel@tonic-gate 		CLIENT_FPUTS(gettext("Error allocating memory for "
31766842Sth160488 		    "objectclassMap\n"), stderr);
31770Sstevel@tonic-gate 		multival_free(hold->serviceAuthenticationMethod);
31780Sstevel@tonic-gate 		multival_free(hold->serviceCredentialLevel);
31790Sstevel@tonic-gate 		free(hold);
31800Sstevel@tonic-gate 		return (NULL);	/* NULL -> error */
31810Sstevel@tonic-gate 	}
31820Sstevel@tonic-gate 
31830Sstevel@tonic-gate 	hold->attributeMap = multival_new();
31840Sstevel@tonic-gate 	if (NULL == hold->attributeMap) {
31850Sstevel@tonic-gate 		CLIENT_FPUTS(gettext("Error allocating memory for "
31866842Sth160488 		    "attributeMap\n"), stderr);
31870Sstevel@tonic-gate 		multival_free(hold->serviceAuthenticationMethod);
31880Sstevel@tonic-gate 		multival_free(hold->serviceCredentialLevel);
31890Sstevel@tonic-gate 		multival_free(hold->objectclassMap);
31900Sstevel@tonic-gate 		free(hold);
31910Sstevel@tonic-gate 		return (NULL);	/* NULL -> error */
31920Sstevel@tonic-gate 	}
31930Sstevel@tonic-gate 
31940Sstevel@tonic-gate 	hold->serviceSearchDescriptor = multival_new();
31950Sstevel@tonic-gate 	if (NULL == hold->serviceSearchDescriptor) {
31960Sstevel@tonic-gate 		CLIENT_FPUTS(gettext("Error allocating memory for "
31976842Sth160488 		    "serviceSearchDescriptor\n"), stderr);
31980Sstevel@tonic-gate 		multival_free(hold->serviceAuthenticationMethod);
31990Sstevel@tonic-gate 		multival_free(hold->serviceCredentialLevel);
32000Sstevel@tonic-gate 		multival_free(hold->objectclassMap);
32010Sstevel@tonic-gate 		multival_free(hold->attributeMap);
32020Sstevel@tonic-gate 		free(hold);
32030Sstevel@tonic-gate 		return (NULL);	/* NULL -> error */
32040Sstevel@tonic-gate 	}
32050Sstevel@tonic-gate 
32060Sstevel@tonic-gate 	return (hold);
32070Sstevel@tonic-gate }
32080Sstevel@tonic-gate 
32090Sstevel@tonic-gate static void
clientopts_free(clientopts_t * list)32100Sstevel@tonic-gate clientopts_free(clientopts_t *list)
32110Sstevel@tonic-gate {
32120Sstevel@tonic-gate 	if (NULL == list)
32130Sstevel@tonic-gate 		return;
32140Sstevel@tonic-gate 
32150Sstevel@tonic-gate 	multival_free(list->serviceAuthenticationMethod);
32160Sstevel@tonic-gate 	multival_free(list->serviceCredentialLevel);
32170Sstevel@tonic-gate 	multival_free(list->objectclassMap);
32180Sstevel@tonic-gate 	multival_free(list->attributeMap);
32190Sstevel@tonic-gate 	multival_free(list->serviceSearchDescriptor);
32200Sstevel@tonic-gate 
32210Sstevel@tonic-gate 	free(list);
32220Sstevel@tonic-gate 
32230Sstevel@tonic-gate }
32240Sstevel@tonic-gate 
32250Sstevel@tonic-gate static void
multival_list(char * opt,multival_t * list)32260Sstevel@tonic-gate multival_list(char *opt, multival_t *list)
32270Sstevel@tonic-gate {
32280Sstevel@tonic-gate 	int i;
32290Sstevel@tonic-gate 
32300Sstevel@tonic-gate 	if (list->count == 0)
32310Sstevel@tonic-gate 		return;
32320Sstevel@tonic-gate 
32330Sstevel@tonic-gate 	(void) puts(opt);
32340Sstevel@tonic-gate 	for (i = 0; i < list->count; i++) {
32350Sstevel@tonic-gate 		(void) printf("\t\targ[%d]: %s\n", i, list->optlist[i]);
32360Sstevel@tonic-gate 	}
32370Sstevel@tonic-gate }
32380Sstevel@tonic-gate 
32390Sstevel@tonic-gate /* return the number of arguments specified in the command line */
32400Sstevel@tonic-gate static int
num_args(clientopts_t * list)32410Sstevel@tonic-gate num_args(clientopts_t *list)
32420Sstevel@tonic-gate {
32430Sstevel@tonic-gate 	int arg_count = 0;
32440Sstevel@tonic-gate 
32450Sstevel@tonic-gate 	arg_count += list->authenticationMethod ? 1 : 0;
32460Sstevel@tonic-gate 	arg_count += list->serviceAuthenticationMethod->count;
32470Sstevel@tonic-gate 	arg_count += list->defaultSearchBase ? 1 : 0;
32480Sstevel@tonic-gate 	arg_count += list->credentialLevel ? 1 : 0;
32490Sstevel@tonic-gate 	arg_count += list->serviceCredentialLevel->count;
32500Sstevel@tonic-gate 	arg_count += list->domainName ? 1 : 0;
32510Sstevel@tonic-gate 	arg_count += list->proxyDN ? 1 : 0;
32528821SMichen.Chang@Sun.COM 	arg_count += list->enableShadowUpdate ? 1 : 0;
32538821SMichen.Chang@Sun.COM 	arg_count += list->adminDN ? 1 : 0;
32540Sstevel@tonic-gate 	arg_count += list->profileTTL ? 1 : 0;
32550Sstevel@tonic-gate 	arg_count += list->objectclassMap->count;
32560Sstevel@tonic-gate 	arg_count += list->searchTimeLimit ? 1 : 0;
32570Sstevel@tonic-gate 	arg_count += list->preferredServerList ? 1 : 0;
32580Sstevel@tonic-gate 	arg_count += list->profileName ? 1 : 0;
32590Sstevel@tonic-gate 	arg_count += list->followReferrals ? 1 : 0;
32600Sstevel@tonic-gate 	arg_count += list->attributeMap->count;
32610Sstevel@tonic-gate 	arg_count += list->defaultSearchScope ? 1 : 0;
32620Sstevel@tonic-gate 	arg_count += list->serviceSearchDescriptor->count;
32630Sstevel@tonic-gate 	arg_count += list->bindTimeLimit ? 1 : 0;
32640Sstevel@tonic-gate 	arg_count += list->proxyPassword ? 1 : 0;
32658821SMichen.Chang@Sun.COM 	arg_count += list->adminPassword ? 1 : 0;
32660Sstevel@tonic-gate 	arg_count += list->defaultServerList ? 1 : 0;
32670Sstevel@tonic-gate 	arg_count += list->certificatePath ? 1 : 0;
32680Sstevel@tonic-gate 
32690Sstevel@tonic-gate 	return (arg_count);
32700Sstevel@tonic-gate }
32710Sstevel@tonic-gate 
32720Sstevel@tonic-gate #define	CLIENT_PRINT(opt, str) if (str) \
32730Sstevel@tonic-gate 		(void) printf("%s%s\n", (opt), (str))
32740Sstevel@tonic-gate 
32750Sstevel@tonic-gate static void
dumpargs(clientopts_t * list)32760Sstevel@tonic-gate dumpargs(clientopts_t *list)
32770Sstevel@tonic-gate {
32780Sstevel@tonic-gate 	CLIENT_PRINT("\tauthenticationMethod: ", list->authenticationMethod);
32790Sstevel@tonic-gate 	multival_list("\tserviceAuthenticationMethod: ",
32806842Sth160488 	    list->serviceAuthenticationMethod);
32810Sstevel@tonic-gate 	CLIENT_PRINT("\tdefaultSearchBase: ", list->defaultSearchBase);
32820Sstevel@tonic-gate 	CLIENT_PRINT("\tcredentialLevel: ", list->credentialLevel);
32830Sstevel@tonic-gate 	multival_list("\tserviceCredentialLevel: ",
32846842Sth160488 	    list->serviceCredentialLevel);
32850Sstevel@tonic-gate 	CLIENT_PRINT("\tdomainName: ", list->domainName);
32860Sstevel@tonic-gate 	CLIENT_PRINT("\tproxyDN: ", list->proxyDN);
32878821SMichen.Chang@Sun.COM 	CLIENT_PRINT("\tadminDN: ", list->adminDN);
32888821SMichen.Chang@Sun.COM 	CLIENT_PRINT("\tenableShadowUpdate: ", list->enableShadowUpdate);
32890Sstevel@tonic-gate 	CLIENT_PRINT("\tprofileTTL: ", list->profileTTL);
32900Sstevel@tonic-gate 	multival_list("\tobjectclassMap: ", list->objectclassMap);
32910Sstevel@tonic-gate 	CLIENT_PRINT("\tsearchTimeLimit: ", list->searchTimeLimit);
32920Sstevel@tonic-gate 	CLIENT_PRINT("\tpreferredServerList: ", list->preferredServerList);
32930Sstevel@tonic-gate 	CLIENT_PRINT("\tprofileName: ", list->profileName);
32940Sstevel@tonic-gate 	CLIENT_PRINT("\tfollowReferrals: ", list->followReferrals);
32950Sstevel@tonic-gate 	multival_list("\tattributeMap: ", list->attributeMap);
32960Sstevel@tonic-gate 	CLIENT_PRINT("\tdefaultSearchScope: ", list->defaultSearchScope);
32970Sstevel@tonic-gate 	multival_list("\tserviceSearchDescriptor: ",
32986842Sth160488 	    list->serviceSearchDescriptor);
32990Sstevel@tonic-gate 	CLIENT_PRINT("\tbindTimeLimit: ", list->bindTimeLimit);
33000Sstevel@tonic-gate 	CLIENT_PRINT("\tproxyPassword: ", list->proxyPassword);
33018821SMichen.Chang@Sun.COM 	CLIENT_PRINT("\tadminPassword: ", list->adminPassword);
33020Sstevel@tonic-gate 	CLIENT_PRINT("\tdefaultServerList: ", list->defaultServerList);
33030Sstevel@tonic-gate 	CLIENT_PRINT("\tcertificatePath: ", list->certificatePath);
33040Sstevel@tonic-gate }
33050Sstevel@tonic-gate 
33060Sstevel@tonic-gate 
33070Sstevel@tonic-gate /* These definitions are only used in parseParam() below. */
33080Sstevel@tonic-gate struct param {
33090Sstevel@tonic-gate 	char	*name;
33100Sstevel@tonic-gate 	int	index;
33110Sstevel@tonic-gate };
33120Sstevel@tonic-gate 
33130Sstevel@tonic-gate static struct param paramArray[] = {
33140Sstevel@tonic-gate 	{"proxyDN", NS_LDAP_BINDDN_P},
33150Sstevel@tonic-gate 	{"proxyPassword", NS_LDAP_BINDPASSWD_P},
33160Sstevel@tonic-gate 	{"defaultServerList", NS_LDAP_SERVERS_P},
33170Sstevel@tonic-gate 	{"defaultSearchBase", NS_LDAP_SEARCH_BASEDN_P},
33180Sstevel@tonic-gate 	{"authenticationMethod", NS_LDAP_AUTH_P},
33190Sstevel@tonic-gate 	{"followReferrals", NS_LDAP_SEARCH_REF_P},
33200Sstevel@tonic-gate 	{"profileTTL", NS_LDAP_CACHETTL_P},
33210Sstevel@tonic-gate 	{"certificatePath", NS_LDAP_HOST_CERTPATH_P},
33220Sstevel@tonic-gate 	{"defaultSearchScope", NS_LDAP_SEARCH_SCOPE_P},
33230Sstevel@tonic-gate 	{"bindTimeLimit", NS_LDAP_BIND_TIME_P},
33240Sstevel@tonic-gate 	{"searchTimeLimit", NS_LDAP_SEARCH_TIME_P},
33250Sstevel@tonic-gate 	{"preferredServerList", NS_LDAP_SERVER_PREF_P},
33260Sstevel@tonic-gate 	{"profileName", NS_LDAP_PROFILE_P},
33270Sstevel@tonic-gate 	{"credentialLevel", NS_LDAP_CREDENTIAL_LEVEL_P},
33280Sstevel@tonic-gate 	{"serviceSearchDescriptor", NS_LDAP_SERVICE_SEARCH_DESC_P},
33290Sstevel@tonic-gate 	{"attributeMap", NS_LDAP_ATTRIBUTEMAP_P},
33300Sstevel@tonic-gate 	{"objectclassMap", NS_LDAP_OBJECTCLASSMAP_P},
33310Sstevel@tonic-gate 	{"serviceAuthenticationMethod", NS_LDAP_SERVICE_AUTH_METHOD_P},
33320Sstevel@tonic-gate 	{"serviceCredentialLevel", NS_LDAP_SERVICE_CRED_LEVEL_P},
33330Sstevel@tonic-gate 	{"domainName", LOCAL_DOMAIN_P},
33348821SMichen.Chang@Sun.COM 	{"enableShadowUpdate", NS_LDAP_ENABLE_SHADOW_UPDATE_P},
33358821SMichen.Chang@Sun.COM 	{"adminDN", NS_LDAP_ADMIN_BINDDN_P},
33368821SMichen.Chang@Sun.COM 	{"adminPassword", NS_LDAP_ADMIN_BINDPASSWD_P},
33370Sstevel@tonic-gate 	{NULL, 0}
33380Sstevel@tonic-gate };
33390Sstevel@tonic-gate 
33400Sstevel@tonic-gate static int
parseParam(char * param,char ** paramVal)33410Sstevel@tonic-gate parseParam(char *param, char **paramVal)
33420Sstevel@tonic-gate {
33430Sstevel@tonic-gate 	char *val = NULL;
33440Sstevel@tonic-gate 	int counter;
33450Sstevel@tonic-gate 
33460Sstevel@tonic-gate 	if (mode_verbose) {
33470Sstevel@tonic-gate 		CLIENT_FPRINTF(stderr, gettext("Parsing %s\n"), param);
33480Sstevel@tonic-gate 	}
33490Sstevel@tonic-gate 
33500Sstevel@tonic-gate 	val = strchr(param, '=');
33510Sstevel@tonic-gate 	if (val == NULL) {
33520Sstevel@tonic-gate 		CLIENT_FPUTS(
33536842Sth160488 		    gettext("Didn\'t find \'=\' character in string\n"),
33546842Sth160488 		    stderr);
33550Sstevel@tonic-gate 		paramVal = NULL;
33560Sstevel@tonic-gate 		return (CLIENT_ERR_PARSE);
33570Sstevel@tonic-gate 	}
33580Sstevel@tonic-gate 
33590Sstevel@tonic-gate 	*val = '\0';
33600Sstevel@tonic-gate 
33610Sstevel@tonic-gate 	for (counter = 0; paramArray[counter].name != NULL; counter++) {
33620Sstevel@tonic-gate 		if (strcasecmp(paramArray[counter].name, param) == 0) {
33630Sstevel@tonic-gate 			*paramVal = val+1;
33640Sstevel@tonic-gate 			*val = '=';	/* restore original param */
33650Sstevel@tonic-gate 			return (paramArray[counter].index);
33660Sstevel@tonic-gate 		}
33670Sstevel@tonic-gate 	}
33680Sstevel@tonic-gate 
33690Sstevel@tonic-gate 	/* Not found */
33700Sstevel@tonic-gate 	*val = '=';	/* restore original param */
33710Sstevel@tonic-gate 	*paramVal = NULL;
33720Sstevel@tonic-gate 	return (CLIENT_ERR_PARSE);
33730Sstevel@tonic-gate }
33740Sstevel@tonic-gate 
33750Sstevel@tonic-gate /*
33760Sstevel@tonic-gate  * The following macro checks if an option has already been specified
33770Sstevel@tonic-gate  * and errs out with usage if so
33780Sstevel@tonic-gate  */
33790Sstevel@tonic-gate #define	CLIENT_OPT_CHECK(opt, optarg)	\
33800Sstevel@tonic-gate if (optarg) {			\
33810Sstevel@tonic-gate 	CLIENT_FPUTS(gettext("Invalid use of option\n"), stderr);	\
33820Sstevel@tonic-gate 	usage();		\
33830Sstevel@tonic-gate 	clientopts_free(optlist); \
33840Sstevel@tonic-gate 	return (CLIENT_ERR_FAIL);		\
33850Sstevel@tonic-gate }
33860Sstevel@tonic-gate 
33870Sstevel@tonic-gate static int
clientSetParam(clientopts_t * optlist,int paramFlag,char * attrVal)33880Sstevel@tonic-gate clientSetParam(clientopts_t *optlist, int paramFlag, char *attrVal)
33890Sstevel@tonic-gate {
33900Sstevel@tonic-gate 	int retcode = 0;
33910Sstevel@tonic-gate 	int counter;
33920Sstevel@tonic-gate 
33930Sstevel@tonic-gate 
33940Sstevel@tonic-gate 	switch (paramFlag) {
33950Sstevel@tonic-gate 	case NS_LDAP_AUTH_P:
33960Sstevel@tonic-gate 		CLIENT_OPT_CHECK(paramFlag, optlist->authenticationMethod);
33970Sstevel@tonic-gate 		optlist->authenticationMethod = attrVal;
33980Sstevel@tonic-gate 		break;
33990Sstevel@tonic-gate 
34000Sstevel@tonic-gate 	case NS_LDAP_SERVICE_AUTH_METHOD_P:	/* multiple allowed */
34010Sstevel@tonic-gate 		retcode = multival_add(optlist->serviceAuthenticationMethod,
34026842Sth160488 		    attrVal);
34030Sstevel@tonic-gate 		if (retcode != CLIENT_SUCCESS) {
34040Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr,
34056842Sth160488 			    gettext("Error processing attrVal %s\n"),
34066842Sth160488 			    attrVal?attrVal:"NULL");
34070Sstevel@tonic-gate 			usage();
34080Sstevel@tonic-gate 			clientopts_free(optlist);
34090Sstevel@tonic-gate 			return (CLIENT_ERR_FAIL);
34100Sstevel@tonic-gate 		}
34110Sstevel@tonic-gate 		break;
34120Sstevel@tonic-gate 
34130Sstevel@tonic-gate 	case NS_LDAP_SEARCH_BASEDN_P:
34140Sstevel@tonic-gate 		CLIENT_OPT_CHECK(paramFlag, optlist->defaultSearchBase);
34150Sstevel@tonic-gate 		optlist->defaultSearchBase = attrVal;
34160Sstevel@tonic-gate 		break;
34170Sstevel@tonic-gate 
34180Sstevel@tonic-gate 	case NS_LDAP_CREDENTIAL_LEVEL_P:
34190Sstevel@tonic-gate 		CLIENT_OPT_CHECK(paramFlag, optlist->credentialLevel);
34200Sstevel@tonic-gate 		optlist->credentialLevel = attrVal;
34210Sstevel@tonic-gate 		break;
34220Sstevel@tonic-gate 
34230Sstevel@tonic-gate 	case NS_LDAP_SERVICE_CRED_LEVEL_P:	/* multiple allowed */
34240Sstevel@tonic-gate 		retcode = multival_add(optlist->serviceCredentialLevel,
34256842Sth160488 		    attrVal);
34260Sstevel@tonic-gate 		if (retcode != CLIENT_SUCCESS) {
34270Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr,
34286842Sth160488 			    gettext("Error processing attrVal %s\n"),
34296842Sth160488 			    attrVal?attrVal:"NULL");
34300Sstevel@tonic-gate 			usage();
34310Sstevel@tonic-gate 			clientopts_free(optlist);
34320Sstevel@tonic-gate 			return (CLIENT_ERR_FAIL);
34330Sstevel@tonic-gate 		}
34340Sstevel@tonic-gate 		break;
34350Sstevel@tonic-gate 
34360Sstevel@tonic-gate 	case LOCAL_DOMAIN_P:
34370Sstevel@tonic-gate 		CLIENT_OPT_CHECK(paramFlag, optlist->domainName);
34380Sstevel@tonic-gate 		optlist->domainName = attrVal;
34390Sstevel@tonic-gate 		dname = optlist->domainName;
34400Sstevel@tonic-gate 		break;
34410Sstevel@tonic-gate 
34420Sstevel@tonic-gate 	case NS_LDAP_BINDDN_P:
34430Sstevel@tonic-gate 		CLIENT_OPT_CHECK(paramFlag, optlist->proxyDN);
34440Sstevel@tonic-gate 		optlist->proxyDN = attrVal;
34450Sstevel@tonic-gate 		break;
34460Sstevel@tonic-gate 
34478821SMichen.Chang@Sun.COM 	case NS_LDAP_ENABLE_SHADOW_UPDATE_P:
34488821SMichen.Chang@Sun.COM 		CLIENT_OPT_CHECK(paramFlag, optlist->enableShadowUpdate);
34498821SMichen.Chang@Sun.COM 		optlist->enableShadowUpdate = attrVal;
34508821SMichen.Chang@Sun.COM 		break;
34518821SMichen.Chang@Sun.COM 
34528821SMichen.Chang@Sun.COM 	case NS_LDAP_ADMIN_BINDDN_P:
34538821SMichen.Chang@Sun.COM 		CLIENT_OPT_CHECK(paramFlag, optlist->adminDN);
34548821SMichen.Chang@Sun.COM 		optlist->adminDN = attrVal;
34558821SMichen.Chang@Sun.COM 		break;
34568821SMichen.Chang@Sun.COM 
34570Sstevel@tonic-gate 	case NS_LDAP_CACHETTL_P:
34580Sstevel@tonic-gate 		CLIENT_OPT_CHECK(paramFlag, optlist->profileTTL);
34590Sstevel@tonic-gate 		optlist->profileTTL = attrVal;
34600Sstevel@tonic-gate 		break;
34610Sstevel@tonic-gate 
34620Sstevel@tonic-gate 	case NS_LDAP_OBJECTCLASSMAP_P:	/* multiple allowed */
34630Sstevel@tonic-gate 		retcode = multival_add(optlist->objectclassMap, attrVal);
34640Sstevel@tonic-gate 		if (retcode != CLIENT_SUCCESS) {
34650Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr,
34666842Sth160488 			    gettext("Error processing attrVal %s\n"),
34676842Sth160488 			    attrVal?attrVal:"NULL");
34680Sstevel@tonic-gate 			usage();
34690Sstevel@tonic-gate 			clientopts_free(optlist);
34700Sstevel@tonic-gate 			return (CLIENT_ERR_FAIL);
34710Sstevel@tonic-gate 		}
34720Sstevel@tonic-gate 		break;
34730Sstevel@tonic-gate 
34740Sstevel@tonic-gate 	case NS_LDAP_SEARCH_TIME_P:
34750Sstevel@tonic-gate 		CLIENT_OPT_CHECK(paramFlag, optlist->searchTimeLimit);
34760Sstevel@tonic-gate 		optlist->searchTimeLimit = attrVal;
34770Sstevel@tonic-gate 		break;
34780Sstevel@tonic-gate 
34790Sstevel@tonic-gate 	case NS_LDAP_SERVER_PREF_P:
34800Sstevel@tonic-gate 		CLIENT_OPT_CHECK(paramFlag, optlist->preferredServerList);
34810Sstevel@tonic-gate 		optlist->preferredServerList = attrVal;
34820Sstevel@tonic-gate 		/* replace ',' chars with ' ' for proper syntax */
34830Sstevel@tonic-gate 		for (counter = 0;
34846842Sth160488 		    counter < strlen(optlist->preferredServerList);
34856842Sth160488 		    counter++) {
34860Sstevel@tonic-gate 
34870Sstevel@tonic-gate 			if (optlist->preferredServerList[counter] == ',')
34880Sstevel@tonic-gate 				optlist->preferredServerList[counter] = ' ';
34890Sstevel@tonic-gate 		}
34900Sstevel@tonic-gate 		break;
34910Sstevel@tonic-gate 
34920Sstevel@tonic-gate 	case NS_LDAP_PROFILE_P:
34930Sstevel@tonic-gate 		CLIENT_OPT_CHECK(paramFlag, optlist->profileName);
34940Sstevel@tonic-gate 		optlist->profileName = attrVal;
34950Sstevel@tonic-gate 		break;
34960Sstevel@tonic-gate 
34970Sstevel@tonic-gate 	case NS_LDAP_SEARCH_REF_P:
34980Sstevel@tonic-gate 		CLIENT_OPT_CHECK(paramFlag, optlist->followReferrals);
34990Sstevel@tonic-gate 		if (0 == strcasecmp(attrVal, "followref"))
35000Sstevel@tonic-gate 			optlist->followReferrals = "TRUE";
35010Sstevel@tonic-gate 		else if (0 == strcasecmp(attrVal, "noref"))
35020Sstevel@tonic-gate 			optlist->followReferrals = "FALSE";
35030Sstevel@tonic-gate 		else
35040Sstevel@tonic-gate 			optlist->followReferrals = attrVal;
35050Sstevel@tonic-gate 		break;
35060Sstevel@tonic-gate 
35070Sstevel@tonic-gate 	case NS_LDAP_ATTRIBUTEMAP_P:	/* multiple allowed */
35080Sstevel@tonic-gate 		retcode = multival_add(optlist->attributeMap, attrVal);
35090Sstevel@tonic-gate 		if (retcode != CLIENT_SUCCESS) {
35100Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr,
35116842Sth160488 			    gettext("Error processing attrVal %s\n"),
35126842Sth160488 			    attrVal?attrVal:"NULL");
35130Sstevel@tonic-gate 			usage();
35140Sstevel@tonic-gate 			clientopts_free(optlist);
35150Sstevel@tonic-gate 			return (CLIENT_ERR_FAIL);
35160Sstevel@tonic-gate 		}
35170Sstevel@tonic-gate 		break;
35180Sstevel@tonic-gate 
35190Sstevel@tonic-gate 	case NS_LDAP_SEARCH_SCOPE_P:
35200Sstevel@tonic-gate 		CLIENT_OPT_CHECK(paramFlag, optlist->defaultSearchScope);
35210Sstevel@tonic-gate 		optlist->defaultSearchScope = attrVal;
35220Sstevel@tonic-gate 		break;
35230Sstevel@tonic-gate 
35240Sstevel@tonic-gate 	case NS_LDAP_SERVICE_SEARCH_DESC_P:	/* multiple allowed */
35250Sstevel@tonic-gate 		retcode = multival_add(optlist->serviceSearchDescriptor,
35266842Sth160488 		    attrVal);
35270Sstevel@tonic-gate 		if (retcode != CLIENT_SUCCESS) {
35280Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr,
35296842Sth160488 			    gettext("Error processing attrVal %s\n"),
35306842Sth160488 			    attrVal?attrVal:"NULL");
35310Sstevel@tonic-gate 			usage();
35320Sstevel@tonic-gate 			clientopts_free(optlist);
35330Sstevel@tonic-gate 			return (CLIENT_ERR_FAIL);
35340Sstevel@tonic-gate 		}
35350Sstevel@tonic-gate 		break;
35360Sstevel@tonic-gate 
35370Sstevel@tonic-gate 	case NS_LDAP_BIND_TIME_P:
35380Sstevel@tonic-gate 		CLIENT_OPT_CHECK(paramFlag, optlist->bindTimeLimit);
35390Sstevel@tonic-gate 		optlist->bindTimeLimit = attrVal;
35400Sstevel@tonic-gate 		break;
35410Sstevel@tonic-gate 
35420Sstevel@tonic-gate 	case NS_LDAP_BINDPASSWD_P:
35430Sstevel@tonic-gate 		CLIENT_OPT_CHECK(paramFlag, optlist->proxyPassword);
35440Sstevel@tonic-gate 		optlist->proxyPassword = attrVal;
35450Sstevel@tonic-gate 		break;
35460Sstevel@tonic-gate 
35478821SMichen.Chang@Sun.COM 	case NS_LDAP_ADMIN_BINDPASSWD_P:
35488821SMichen.Chang@Sun.COM 		CLIENT_OPT_CHECK(paramFlag, optlist->adminPassword);
35498821SMichen.Chang@Sun.COM 		optlist->adminPassword = attrVal;
35508821SMichen.Chang@Sun.COM 		break;
35518821SMichen.Chang@Sun.COM 
35520Sstevel@tonic-gate 	case NS_LDAP_HOST_CERTPATH_P:
35530Sstevel@tonic-gate 		CLIENT_OPT_CHECK(paramFlag, optlist->certificatePath);
35540Sstevel@tonic-gate 		optlist->certificatePath = attrVal;
35550Sstevel@tonic-gate 		break;
35560Sstevel@tonic-gate 
35570Sstevel@tonic-gate 	case NS_LDAP_SERVERS_P:
35580Sstevel@tonic-gate 		CLIENT_OPT_CHECK(paramFlag, optlist->defaultServerList);
35590Sstevel@tonic-gate 		optlist->defaultServerList = attrVal;
35600Sstevel@tonic-gate 		break;
35610Sstevel@tonic-gate 
35620Sstevel@tonic-gate 	default:
35630Sstevel@tonic-gate 		usage();
35640Sstevel@tonic-gate 		return (CLIENT_ERR_FAIL);
35650Sstevel@tonic-gate 		/* break;  lint doesn't like break before end of switch */
35660Sstevel@tonic-gate 	}
35670Sstevel@tonic-gate 
35680Sstevel@tonic-gate 	return (retcode);
35690Sstevel@tonic-gate }
35700Sstevel@tonic-gate 
35710Sstevel@tonic-gate /*
35720Sstevel@tonic-gate  * file_move() - Used to move a config file (backup/restore).
35730Sstevel@tonic-gate  *
35740Sstevel@tonic-gate  * This function uses a system() call with /bin/mv to handle the
35750Sstevel@tonic-gate  * case where the backup directory (/var) is on a different file
35760Sstevel@tonic-gate  * system than the config file (typically /etc).
35770Sstevel@tonic-gate  */
35780Sstevel@tonic-gate static int
file_move(const char * from,const char * to)35790Sstevel@tonic-gate file_move(const char *from, const char *to)
35800Sstevel@tonic-gate {
35810Sstevel@tonic-gate 	int retcode;
35820Sstevel@tonic-gate 	char mvCommand[] = CMD_MV;
35830Sstevel@tonic-gate 	char cmd_buffer[(2 * MAXPATHLEN) + sizeof (mvCommand) + 3];
35840Sstevel@tonic-gate 
35850Sstevel@tonic-gate 	(void) snprintf(cmd_buffer, sizeof (cmd_buffer), "%s %s %s",
35866842Sth160488 	    mvCommand, from, to);
35870Sstevel@tonic-gate 
35880Sstevel@tonic-gate 	/*
35890Sstevel@tonic-gate 	 * This function should only be used internally to move
35900Sstevel@tonic-gate 	 * system files to/from the backup directory.  For security
35910Sstevel@tonic-gate 	 * reasons (this is run as root), don't use this function
35920Sstevel@tonic-gate 	 * with arguments passed into the program.
35930Sstevel@tonic-gate 	 */
35940Sstevel@tonic-gate 	retcode = system(cmd_buffer);
35950Sstevel@tonic-gate 
35960Sstevel@tonic-gate 	return (retcode);
35970Sstevel@tonic-gate }
35980Sstevel@tonic-gate 
35990Sstevel@tonic-gate 
36000Sstevel@tonic-gate /*
36010Sstevel@tonic-gate  * Manipulate the service as instructed by "dowhat"
36020Sstevel@tonic-gate  */
36030Sstevel@tonic-gate static int
do_service(const char * fmri,boolean_t waitflag,int dowhat,const char * state)36040Sstevel@tonic-gate do_service(const char *fmri, boolean_t waitflag, int dowhat,
36050Sstevel@tonic-gate 		const char *state) {
36060Sstevel@tonic-gate 
36070Sstevel@tonic-gate 	int		status;
36080Sstevel@tonic-gate 	boolean_t	is_maint;
36090Sstevel@tonic-gate 	const char	*what = gettext("not set");
36100Sstevel@tonic-gate 	useconds_t	max;
36110Sstevel@tonic-gate 
36120Sstevel@tonic-gate 	/* Check if we are in maintenance */
36130Sstevel@tonic-gate 	is_maint = is_service(fmri, SCF_STATE_STRING_MAINT);
36140Sstevel@tonic-gate 
36150Sstevel@tonic-gate 	switch (dowhat) {
36160Sstevel@tonic-gate 	case START_SERVICE:
36170Sstevel@tonic-gate 		what = gettext("start");
36180Sstevel@tonic-gate 		status = smf_enable_instance(fmri,
36190Sstevel@tonic-gate 			(sysid_install == B_TRUE)?SMF_TEMPORARY:0);
36200Sstevel@tonic-gate 		break;
36210Sstevel@tonic-gate 	case STOP_SERVICE:
36220Sstevel@tonic-gate 		what = gettext("stop");
36230Sstevel@tonic-gate 		status = smf_disable_instance(fmri,
36240Sstevel@tonic-gate 			(sysid_install == B_TRUE)?SMF_TEMPORARY:0);
36250Sstevel@tonic-gate 		break;
36260Sstevel@tonic-gate 	case RESTART_SERVICE:
36270Sstevel@tonic-gate 		what = gettext("restart");
36280Sstevel@tonic-gate 		status = smf_restart_instance(fmri);
36290Sstevel@tonic-gate 		break;
36300Sstevel@tonic-gate 	default:
36310Sstevel@tonic-gate 		/* coding error; will not happen */
36320Sstevel@tonic-gate 		assert(0);
36330Sstevel@tonic-gate 	}
36340Sstevel@tonic-gate 
36350Sstevel@tonic-gate 	/*
36360Sstevel@tonic-gate 	 * If the service was previously in maintenance then we need to
36370Sstevel@tonic-gate 	 * clear it immediately.  The "dowhat" action will set the
36380Sstevel@tonic-gate 	 * enabled property of the service as intended by the caller while
36390Sstevel@tonic-gate 	 * clear will actually cause it to be enabled/disabled.
36400Sstevel@tonic-gate 	 * We assume that the caller has called us after taking some
36410Sstevel@tonic-gate 	 * recovery action. Even if it's not the case, we don't lose
36420Sstevel@tonic-gate 	 * anything.
36430Sstevel@tonic-gate 	 */
36440Sstevel@tonic-gate 	if (status == 0 && is_maint == B_TRUE) {
36450Sstevel@tonic-gate 		if (mode_verbose)
36460Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr,
36470Sstevel@tonic-gate 				"%s: %s... %s\n",
36480Sstevel@tonic-gate 				what,
36490Sstevel@tonic-gate 				fmri,
36500Sstevel@tonic-gate 				gettext("restoring from maintenance state"));
36510Sstevel@tonic-gate 		status = smf_restore_instance(fmri);
36520Sstevel@tonic-gate 	}
36530Sstevel@tonic-gate 
36540Sstevel@tonic-gate 	if (status == 0) {
36550Sstevel@tonic-gate 		/* Check if we need to wait ? */
36560Sstevel@tonic-gate 		if (waitflag == B_FALSE) {
36570Sstevel@tonic-gate 			if (mode_verbose)
36580Sstevel@tonic-gate 				CLIENT_FPRINTF(stderr,
36590Sstevel@tonic-gate 					"%s: %s... %s\n",
36600Sstevel@tonic-gate 					what,
36610Sstevel@tonic-gate 					fmri,
36620Sstevel@tonic-gate 					gettext("success"));
36630Sstevel@tonic-gate 			return (CLIENT_SUCCESS);
36640Sstevel@tonic-gate 		}
36650Sstevel@tonic-gate 
36660Sstevel@tonic-gate 		/* Otherwise wait for max seconds (from the manifest) */
36670Sstevel@tonic-gate 		max = get_timeout_value(dowhat, fmri, DEFAULT_TIMEOUT);
36680Sstevel@tonic-gate 		status = wait_till(fmri, state, max, what, !is_maint);
36690Sstevel@tonic-gate 		if (status == CLIENT_SUCCESS)
36700Sstevel@tonic-gate 			return (CLIENT_SUCCESS);
36710Sstevel@tonic-gate 		/* For error fall through for corrective action */
36720Sstevel@tonic-gate 	} else {
36730Sstevel@tonic-gate 		/* Well, service failed ... */
36740Sstevel@tonic-gate 		if (mode_verbose)
36750Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr, "%s: %s... %s: %s\n",
36760Sstevel@tonic-gate 				what,
36770Sstevel@tonic-gate 				fmri,
36780Sstevel@tonic-gate 				gettext("failed"),
36790Sstevel@tonic-gate 				scf_strerror(scf_error()));
36800Sstevel@tonic-gate 		status = CLIENT_ERR_FAIL;
36810Sstevel@tonic-gate 		/* For error fall through for corrective action */
36820Sstevel@tonic-gate 	}
36830Sstevel@tonic-gate 
36840Sstevel@tonic-gate 	/*
36850Sstevel@tonic-gate 	 * If service is still offline after start/restart, then transitioning
36860Sstevel@tonic-gate 	 * failed and guess is restarter failed to apply the timeout as well.
36870Sstevel@tonic-gate 	 * So instead of leaving it offline, let's just disable it until we have
36880Sstevel@tonic-gate 	 * some other mechanism available from smf to handle such situation.
36890Sstevel@tonic-gate 	 */
36900Sstevel@tonic-gate 	if (dowhat != STOP_SERVICE)
36910Sstevel@tonic-gate 		if (is_service(fmri, SCF_STATE_STRING_OFFLINE)) {
36920Sstevel@tonic-gate 			if (mode_verbose)
36930Sstevel@tonic-gate 				CLIENT_FPRINTF(stderr,
36940Sstevel@tonic-gate 					"%s: %s... %s\n",
36950Sstevel@tonic-gate 					what,
36960Sstevel@tonic-gate 					fmri,
36970Sstevel@tonic-gate 					gettext("offline to disable"));
36980Sstevel@tonic-gate 			(void) disable_service(fmri, waitflag);
36990Sstevel@tonic-gate 		}
37000Sstevel@tonic-gate 
37010Sstevel@tonic-gate 	return (status);
37020Sstevel@tonic-gate }
37030Sstevel@tonic-gate 
37040Sstevel@tonic-gate 
37050Sstevel@tonic-gate /*
37060Sstevel@tonic-gate  * Wait for "max" usecs for the service described by "fmri" to change
37070Sstevel@tonic-gate  * to "state". If check_maint is true then return immediately if
37080Sstevel@tonic-gate  * service goes into maintenance
37090Sstevel@tonic-gate  */
37100Sstevel@tonic-gate static int
wait_till(const char * fmri,const char * state,useconds_t max,const char * what,boolean_t check_maint)37110Sstevel@tonic-gate wait_till(const char *fmri, const char *state, useconds_t max,
37120Sstevel@tonic-gate 		const char *what, boolean_t check_maint) {
37130Sstevel@tonic-gate 	char *st;
37140Sstevel@tonic-gate 	useconds_t usecs = INIT_WAIT_USECS;
37150Sstevel@tonic-gate 
37160Sstevel@tonic-gate 	for (; max > 0; max -= usecs) {
37170Sstevel@tonic-gate 		/* incremental wait */
37180Sstevel@tonic-gate 		usecs *= 2;
37190Sstevel@tonic-gate 		usecs = (usecs > max)?max:usecs;
37200Sstevel@tonic-gate 		if (mode_verbose)
37210Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr,
37220Sstevel@tonic-gate 				"%s: %s %u %s\n",
37230Sstevel@tonic-gate 				what, gettext("sleep"), usecs,
37240Sstevel@tonic-gate 				gettext("microseconds"));
37250Sstevel@tonic-gate 		(void) usleep(usecs);
37260Sstevel@tonic-gate 
37270Sstevel@tonic-gate 		/* Check state after the wait */
37280Sstevel@tonic-gate 		if ((st = smf_get_state(fmri)) != NULL) {
37290Sstevel@tonic-gate 			if (strcmp(st, state) == 0) {
37300Sstevel@tonic-gate 				if (mode_verbose)
37310Sstevel@tonic-gate 					CLIENT_FPRINTF(stderr,
37320Sstevel@tonic-gate 						"%s: %s... %s\n",
37330Sstevel@tonic-gate 						what,
37340Sstevel@tonic-gate 						fmri,
37350Sstevel@tonic-gate 						gettext("success"));
37360Sstevel@tonic-gate 				free(st);
37370Sstevel@tonic-gate 				return (CLIENT_SUCCESS);
37380Sstevel@tonic-gate 			}
37390Sstevel@tonic-gate 
37400Sstevel@tonic-gate 			/*
37410Sstevel@tonic-gate 			 * If service has gone into maintenance then
37420Sstevel@tonic-gate 			 * we will time out anyway, so we are better
37430Sstevel@tonic-gate 			 * off returning now
37440Sstevel@tonic-gate 			 */
37450Sstevel@tonic-gate 			if (check_maint &&
37460Sstevel@tonic-gate 				strcmp(st, SCF_STATE_STRING_MAINT) == 0) {
37470Sstevel@tonic-gate 				if (mode_verbose)
37480Sstevel@tonic-gate 					CLIENT_FPRINTF(stderr,
37490Sstevel@tonic-gate 						"%s: %s... %s\n",
37500Sstevel@tonic-gate 						what,
37510Sstevel@tonic-gate 						fmri,
37520Sstevel@tonic-gate 						gettext("maintenance"));
37530Sstevel@tonic-gate 				free(st);
37540Sstevel@tonic-gate 				return (CLIENT_ERR_MAINTENANCE);
37550Sstevel@tonic-gate 			}
37560Sstevel@tonic-gate 			free(st);
37570Sstevel@tonic-gate 		} else {
37580Sstevel@tonic-gate 			if (mode_verbose)
37590Sstevel@tonic-gate 				CLIENT_FPRINTF(stderr,
37600Sstevel@tonic-gate 						"%s: %s... %s: %s\n",
37610Sstevel@tonic-gate 						what,
37620Sstevel@tonic-gate 						fmri,
37630Sstevel@tonic-gate 						gettext("failed"),
37640Sstevel@tonic-gate 						scf_strerror(scf_error()));
37650Sstevel@tonic-gate 			return (CLIENT_ERR_FAIL);
37660Sstevel@tonic-gate 		}
37670Sstevel@tonic-gate 	}
37680Sstevel@tonic-gate 
37690Sstevel@tonic-gate 	/* Timed out waiting */
37700Sstevel@tonic-gate 	if (mode_verbose)
37710Sstevel@tonic-gate 		CLIENT_FPRINTF(stderr,
37720Sstevel@tonic-gate 			"%s: %s... %s\n",
37730Sstevel@tonic-gate 			what,
37740Sstevel@tonic-gate 			fmri,
37750Sstevel@tonic-gate 			gettext("timed out"));
37760Sstevel@tonic-gate 	return (CLIENT_ERR_TIMEDOUT);
37770Sstevel@tonic-gate }
37780Sstevel@tonic-gate 
37790Sstevel@tonic-gate 
37800Sstevel@tonic-gate static boolean_t
is_service(const char * fmri,const char * state)37810Sstevel@tonic-gate is_service(const char *fmri, const char *state) {
37820Sstevel@tonic-gate 	char		*st;
37830Sstevel@tonic-gate 	boolean_t	result = B_FALSE;
37840Sstevel@tonic-gate 
37850Sstevel@tonic-gate 	if ((st = smf_get_state(fmri)) != NULL) {
37860Sstevel@tonic-gate 		if (strcmp(st, state) == 0)
37870Sstevel@tonic-gate 			result = B_TRUE;
37880Sstevel@tonic-gate 		free(st);
37890Sstevel@tonic-gate 	}
37900Sstevel@tonic-gate 	return (result);
37910Sstevel@tonic-gate }
37920Sstevel@tonic-gate 
37930Sstevel@tonic-gate 
37940Sstevel@tonic-gate /*
37950Sstevel@tonic-gate  *
37960Sstevel@tonic-gate  * get_timeout_val : returns the timeout value set in fmri manifest
37970Sstevel@tonic-gate  * 	inputs	: action(start/stop)
37980Sstevel@tonic-gate  *	fmri(defined fmri string)
37990Sstevel@tonic-gate  *	Returns default if error, the timeout val otherwise
38000Sstevel@tonic-gate  *
38010Sstevel@tonic-gate  */
38020Sstevel@tonic-gate 
38030Sstevel@tonic-gate static useconds_t
get_timeout_value(int dowhat,const char * fmri,useconds_t default_val)38040Sstevel@tonic-gate get_timeout_value(int dowhat, const char *fmri, useconds_t default_val)
38050Sstevel@tonic-gate {
38060Sstevel@tonic-gate 	scf_simple_prop_t	*sp = NULL;
38070Sstevel@tonic-gate 	uint64_t		*cp = NULL;
38080Sstevel@tonic-gate 	int			timeout = default_val/1000000;
38090Sstevel@tonic-gate 	char			*action = NULL;
38100Sstevel@tonic-gate 	const char		*actionstr = NULL;
38110Sstevel@tonic-gate 
38120Sstevel@tonic-gate 	switch (dowhat)  {
38130Sstevel@tonic-gate 		case START_SERVICE:
38140Sstevel@tonic-gate 		case RESTART_SERVICE:
38150Sstevel@tonic-gate 				action = "start";
38160Sstevel@tonic-gate 				actionstr = gettext("start");
38170Sstevel@tonic-gate 				break;
38180Sstevel@tonic-gate 		case STOP_SERVICE:
38190Sstevel@tonic-gate 				action = "stop";
38200Sstevel@tonic-gate 				actionstr = gettext("stop");
38210Sstevel@tonic-gate 				break;
38220Sstevel@tonic-gate 		default:
38230Sstevel@tonic-gate 			assert(0);
38240Sstevel@tonic-gate 	}
38250Sstevel@tonic-gate 
38260Sstevel@tonic-gate 
38270Sstevel@tonic-gate 	sp = scf_simple_prop_get(NULL, fmri, action, SCF_PROPERTY_TIMEOUT);
38280Sstevel@tonic-gate 	if (sp == NULL) {
38290Sstevel@tonic-gate 		if (mode_verbose)
38300Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr, "%s: %s... %s: %s\n",
38316842Sth160488 			    actionstr,
38326842Sth160488 			    fmri,
38336842Sth160488 			    gettext("failed to retrieve timeout property"),
38346842Sth160488 			    scf_strerror(scf_error()));
38350Sstevel@tonic-gate 		return (default_val);
38360Sstevel@tonic-gate 	}
38370Sstevel@tonic-gate 
38380Sstevel@tonic-gate 	cp = scf_simple_prop_next_count(sp);
38390Sstevel@tonic-gate 	if (cp == NULL) {
38400Sstevel@tonic-gate 		if (mode_verbose)
38410Sstevel@tonic-gate 			CLIENT_FPRINTF(stderr, "%s: %s... %s: %s\n",
38426842Sth160488 			    actionstr,
38436842Sth160488 			    fmri,
38446842Sth160488 			    gettext("failed to retrieve timeout value"),
38456842Sth160488 			    scf_strerror(scf_error()));
38460Sstevel@tonic-gate 		scf_simple_prop_free(sp);
38470Sstevel@tonic-gate 		return (default_val);
38480Sstevel@tonic-gate 	}
38490Sstevel@tonic-gate 
38500Sstevel@tonic-gate 	if (*cp != 0)
38510Sstevel@tonic-gate 		timeout = *cp;
38520Sstevel@tonic-gate 	scf_simple_prop_free(sp);
38530Sstevel@tonic-gate 	return (timeout * 1000000);
38540Sstevel@tonic-gate }
3855