4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
24 * Copyright 2012 Milan Jurik. All rights reserved.
25 * Copyright (c) 2016 by Delphix. All rights reserved.
29 * ldapclient command. To make (initiailize) or uninitialize a machines as
30 * and LDAP client. This command MUST be run as root (or it will simply exit).
32 * -I Install. No file_backup/recover for installing only (no doc).
34 * init Initialze (create) an LDAP client from a profile stored
35 * in a directory-server.
36 * manual Initialze (create) an LDAP client by hand (-file option
38 * mod Modify the LDAP client configuration on this machine by hand.
39 * list List the contents of the LDAP client cache files.
40 * uninit Uninitialize this machine.
43 * -q Quiet flag (mutually exclusive with -v).
46 * <attrName> can be one of the following:
49 * Attribute map. Can be multiple instances of this option.
51 * authenticationMethod
52 * Authentication method (formerly -a)
54 * Bind time limit. (no former option)
56 * Path to certificates used for secure bind (no former option)
58 * Client credential level (no former option)
60 * Default server (no former option) Refer to DUA Config
63 * Search Base DN. e.g. dc=eng,dc=sun,dc=com (formerly -b)
65 * Search scope. (formerly -s)
67 * Hosts lookup domain (DNS) Ex. eng.sun.com (formerly -d)
69 * Search dereference. followref or noref (default followref)
72 * Objectclass map. Can be multiple instances of this option.
75 * Server preference list. Comma ',' seperated list of IPaddr.
78 * Profile name to use for init (ldapclient) or
79 * generate (gen_profile). (formerly -P)
81 * Client info TTL. If set to 0 this information will not be
82 * automatically updated by the ldap_cachemgr(1M).
85 * Binding DN. Ex. cn=client,ou=people,cd=eng,dc=sun,dc=com
88 * Client password not needed for authentication "none".
91 * Administrator DN for updating naming data.
93 * Administrator password
95 * Allow Administrator to change shadow data in LDAP
97 * Timeout value. (formerly -o)
98 * serviceSearchDescriptor
99 * Service search scope. (no former option)
100 * serviceAuthenticationMethod
101 * Service authenticaion method (no former option)
102 * serviceCredentialLevel
103 * Service credential level (no former option)
111 #include <sys/types.h>
113 #include <sys/param.h>
114 #include <sys/stat.h>
115 #include <sys/systeminfo.h>
125 #include "standalone.h"
127 #if !defined(TEXT_DOMAIN)
128 #define TEXT_DOMAIN "SUNW_OST_OSCMD"
132 /* The manpage doc only allows for SUCCESS(0), FAIL(1) and CRED(2) on exit */
133 #define CLIENT_SUCCESS 0
134 #define CLIENT_ERR_PARSE -1
135 #define CLIENT_ERR_FAIL 1
136 #define CLIENT_ERR_CREDENTIAL 2
137 #define CLIENT_ERR_MEMORY 3
138 #define CLIENT_ERR_RESTORE 4
139 #define CLIENT_ERR_RENAME 5
140 #define CLIENT_ERR_RECOVER 6
141 #define CLIENT_ERR_TIMEDOUT 7
142 #define CLIENT_ERR_MAINTENANCE 8
144 /* Reset flag for start_services() */
146 #define START_RESET 2
147 #define START_UNINIT 3
149 /* Reset flag for stop_services() */
150 #define STATE_NOSAVE 0
153 /* files to (possibiliy) restore */
154 #define LDAP_RESTORE_DIR "/var/ldap/restore"
156 #define DOMAINNAME_DIR "/etc"
157 #define DOMAINNAME_FILE "defaultdomain"
158 #define DOMAINNAME DOMAINNAME_DIR "/" DOMAINNAME_FILE
159 #define DOMAINNAME_BACK LDAP_RESTORE_DIR "/" DOMAINNAME_FILE
161 #define NSSWITCH_DIR "/etc"
162 #define NSSWITCH_FILE "nsswitch.conf"
163 #define NSSWITCH_CONF NSSWITCH_DIR "/" NSSWITCH_FILE
164 #define NSSWITCH_BACK LDAP_RESTORE_DIR "/" NSSWITCH_FILE
165 #define NSSWITCH_LDAP "/etc/nsswitch.ldap"
167 #define YP_BIND_DIR "/var/yp/binding"
169 /* Define the service FMRIs */
170 #define SENDMAIL_FMRI "network/smtp:sendmail"
171 #define NSCD_FMRI "system/name-service-cache:default"
172 #define AUTOFS_FMRI "system/filesystem/autofs:default"
173 #define LDAP_FMRI "network/ldap/client:default"
174 #define YP_FMRI "network/nis/client:default"
175 #define NS_MILESTONE_FMRI "milestone/name-services:default"
177 /* Define flags for checking if services were enabled */
178 #define SENDMAIL_ON 0x1
180 #define AUTOFS_ON 0x100
182 #define CMD_DOMAIN_START "/usr/bin/domainname"
184 /* Command to copy files */
185 #define CMD_CP "/bin/cp -f"
186 #define CMD_MV "/bin/mv -f"
187 #define CMD_RM "/bin/rm -f"
189 #define TO_DEV_NULL " >/dev/null 2>&1"
191 /* Files that need to be just removed */
192 #define LDAP_CACHE_LOG "/var/ldap/cachemgr.log"
194 /* Output defines to supress if quiet mode set */
195 #define CLIENT_FPUTS if (!mode_quiet) (void) fputs
196 #define CLIENT_FPRINTF if (!mode_quiet) (void) fprintf
197 #define CLIENT_FPUTC if (!mode_quiet) (void) fputc
199 #define restart_service(fmri, waitflag)\
200 do_service(fmri, waitflag, RESTART_SERVICE,\
201 SCF_STATE_STRING_ONLINE)
202 #define start_service(fmri, waitflag) \
203 do_service(fmri, waitflag, START_SERVICE,\
204 SCF_STATE_STRING_ONLINE)
205 #define disable_service(fmri, waitflag) \
206 do_service(fmri, waitflag, STOP_SERVICE,\
207 SCF_STATE_STRING_DISABLED)
210 * There isn't a domainName defined as a param, so we set a value here
211 * (1001) should be big enough
213 #define LOCAL_DOMAIN_P 1001
215 #define START_SERVICE 1
216 #define STOP_SERVICE 2
217 #define RESTART_SERVICE 3
219 #define DEFAULT_TIMEOUT 60000000
221 #define INIT_WAIT_USECS 50000
223 /* Used to turn off profile checking */
224 #define CACHETTL_OFF "0"
229 static char *dname
= NULL
;
230 static char dname_buf
[BUFSIZ
];
232 static boolean_t sysid_install
= B_FALSE
;
234 static int mode_verbose
= 0;
235 static int mode_quiet
= 0;
238 static int gStartLdap
= 0;
239 static int gStartYp
= 0;
241 static int enableFlag
= 0;
243 /* multival_t is used to hold params that can have more than one value */
249 static multival_t
*multival_new();
250 static int multival_add(multival_t
*list
, char *opt
);
251 static void multival_free(multival_t
*list
);
254 * clientopts_t is used to hold and pass around the param values from
258 multival_t
*attributeMap
;
259 char *authenticationMethod
;
261 char *certificatePath
;
262 char *credentialLevel
;
263 char *defaultSearchBase
;
264 char *defaultServerList
;
266 char *followReferrals
;
267 multival_t
*objectclassMap
;
268 char *preferredServerList
;
273 char *enableShadowUpdate
;
278 char *defaultSearchScope
;
279 char *searchTimeLimit
;
280 multival_t
*serviceAuthenticationMethod
;
281 multival_t
*serviceCredentialLevel
;
282 multival_t
*serviceSearchDescriptor
;
285 static clientopts_t
*clientopts_new();
286 static void clientopts_free(clientopts_t
*list
);
288 extern ns_ldap_error_t
*__ns_ldap_print_config(int);
289 extern void __ns_ldap_default_config();
290 extern int __ns_ldap_download(const char *, char *, char *, ns_ldap_error_t
**);
292 /* Function prototypes (these could be static) */
293 static void usage(void);
295 static int credCheck(clientopts_t
*arglist
);
296 static int adminCredCheck(clientopts_t
*arglist
);
297 static int clientSetParam(clientopts_t
*optlist
, int paramFlag
, char *attrVal
);
298 static int parseParam(char *param
, char **paramVal
);
299 static void dumpargs(clientopts_t
*arglist
);
300 static int num_args(clientopts_t
*arglist
);
302 static int file_backup(void);
303 static int recover(int saveState
);
304 static int mod_backup(void);
305 static int mod_recover(void);
306 static void mod_cleanup(void);
308 static int client_list(clientopts_t
*arglist
);
309 static int client_manual(clientopts_t
*arglist
);
310 static int client_mod(clientopts_t
*arglist
);
311 static int client_uninit(clientopts_t
*arglist
);
312 static int client_genProfile(clientopts_t
*arglist
);
313 static int client_init(clientopts_t
*arglist
);
314 static int file_move(const char *from
, const char *to
);
316 static int start_services(int flag
);
317 static int stop_services(int saveState
);
318 static boolean_t
is_service(const char *fmri
, const char *state
);
319 static int wait_till(const char *fmri
, const char *state
, useconds_t max
,
320 const char *what
, boolean_t check_maint
);
321 static int do_service(const char *fmri
, boolean_t waitflag
, int dowhat
,
323 static useconds_t
get_timeout_value(int dowhat
, const char *fmri
,
324 useconds_t default_val
);
327 main(int argc
, char **argv
)
329 char *ret_locale
, *ret_textdomain
;
334 clientopts_t
*optlist
= NULL
;
335 int op_manual
= 0, op_mod
= 0, op_uninit
= 0;
336 int op_list
= 0, op_init
= 0, op_genprofile
= 0;
341 ret_locale
= setlocale(LC_ALL
, "");
342 if (ret_locale
== NULL
) {
343 CLIENT_FPUTS(gettext("Unable to set locale.\n"), stderr
);
345 ret_textdomain
= textdomain(TEXT_DOMAIN
);
346 if (ret_textdomain
== NULL
) {
347 CLIENT_FPUTS(gettext("Unable to set textdomain.\n"), stderr
);
350 openlog("ldapclient", LOG_PID
, LOG_USER
);
352 /* get name that invoked us */
353 if (cmd
= strrchr(argv
[0], '/'))
358 sysinfostatus
= sysinfo(SI_SRPC_DOMAIN
, dname_buf
, BUFSIZ
);
359 if (0 < sysinfostatus
)
360 dname
= &dname_buf
[0];
362 optlist
= clientopts_new();
363 if (optlist
== NULL
) {
365 gettext("Error getting optlist (malloc fail)\n"),
367 exit(CLIENT_ERR_FAIL
);
371 while (optind
< argc
) {
372 option
= getopt(argc
, argv
, "vqa:ID:w:j:y:z:");
383 paramFlag
= parseParam(optarg
, &attrVal
);
384 if (paramFlag
== CLIENT_ERR_PARSE
) {
385 CLIENT_FPRINTF(stderr
,
386 gettext("Unrecognized "
387 "parameter \"%s\"\n"),
390 exit(CLIENT_ERR_FAIL
);
392 if (paramFlag
== NS_LDAP_BINDPASSWD_P
&&
393 optlist
->proxyPassword
!= NULL
) {
394 (void) fprintf(stderr
,
395 gettext("The -a proxyPassword option is "
396 "mutually exclusive of -y. "
397 "-a proxyPassword is ignored.\n"));
400 if (paramFlag
== NS_LDAP_ADMIN_BINDPASSWD_P
&&
401 optlist
->adminPassword
!= NULL
) {
402 (void) fprintf(stderr
,
403 gettext("The -a adminPassword option is "
404 "mutually exclusive of -z. "
405 "-a adminPassword is ignored.\n"));
408 retcode
= clientSetParam(optlist
, paramFlag
, attrVal
);
409 if (retcode
!= CLIENT_SUCCESS
) {
412 gettext("Error (%d) setting "
416 exit(CLIENT_ERR_FAIL
);
420 optlist
->bindDN
= strdup(optarg
);
423 if (optlist
->bindPasswd
!= NULL
) {
424 CLIENT_FPRINTF(stderr
,
425 gettext("The -w option is mutually "
426 "exclusive of -j. -w is ignored."));
430 if (optarg
[0] == '-' && optarg
[1] == '\0') {
431 /* Ask for a password later */
435 optlist
->bindPasswd
= strdup(optarg
);
438 if (optlist
->bindPasswd
!= NULL
) {
439 (void) fprintf(stderr
,
440 gettext("The -w option is mutually "
441 "exclusive of -j. -w is ignored.\n"));
442 free(optlist
->bindPasswd
);
444 optlist
->bindPasswd
= readPwd(optarg
);
445 if (optlist
->bindPasswd
== NULL
) {
446 exit(CLIENT_ERR_FAIL
);
450 if (optlist
->proxyPassword
!= NULL
) {
451 (void) fprintf(stderr
,
452 gettext("The -a proxyPassword option is "
453 "mutually exclusive of -y. "
454 "-a proxyPassword is ignored.\n"));
456 optlist
->proxyPassword
= readPwd(optarg
);
457 if (optlist
->proxyPassword
== NULL
) {
458 exit(CLIENT_ERR_FAIL
);
462 if (optlist
->adminPassword
!= NULL
) {
463 (void) fprintf(stderr
,
464 gettext("The -a adminPassword option is "
465 "mutually exclusive of -z. "
466 "-a adminPassword is ignored.\n"));
468 optlist
->adminPassword
= readPwd(optarg
);
469 if (optlist
->adminPassword
== NULL
) {
470 exit(CLIENT_ERR_FAIL
);
474 if (strcmp(argv
[optind
], "init") == 0) {
476 } else if (strcmp(argv
[optind
], "manual") == 0) {
478 } else if (strcmp(argv
[optind
], "mod") == 0) {
480 } else if (strcmp(argv
[optind
], "list") == 0) {
482 } else if (strcmp(argv
[optind
], "uninit") == 0) {
484 } else if (strcmp(argv
[optind
], "genprofile") == 0) {
487 } else if (optind
== argc
-1) {
488 retcode
= clientSetParam(
491 argv
[optind
]); /* ipAddr */
492 if (retcode
!= CLIENT_SUCCESS
) {
495 gettext("Error (%d) setting "
496 "serverList param.\n"),
499 exit(CLIENT_ERR_FAIL
);
503 gettext("Error parsing "
507 exit(CLIENT_ERR_FAIL
);
509 optind
++; /* get past the verb and keep trying */
511 /* Backwards compatibility to support system install */
513 sysid_install
= B_TRUE
;
519 CLIENT_FPUTS(gettext("\nOr\n\n"), stderr
);
522 exit(CLIENT_ERR_FAIL
);
528 if ((getuid() != 0) && (!op_genprofile
)) {
530 "You must be root (SuperUser) to run this command.");
532 exit(CLIENT_ERR_FAIL
);
536 * All command line arguments are finished being parsed now
539 /* *** Do semantic checking here *** */
541 /* if gen and no no searchBase then err */
542 if (gen
&& !optlist
->defaultSearchBase
) {
544 gettext("ldapclient: Missing required attrName "
545 "defaultSearchBase\n"),
548 clientopts_free(optlist
);
549 exit(CLIENT_ERR_FAIL
);
553 * if init or manual, and if adminDN is specified then enableShadowUpdate
554 * must be set to TRUE.
556 if ((op_init
|| op_manual
) &&
557 (!optlist
->enableShadowUpdate
||
558 strcasecmp(optlist
->enableShadowUpdate
, "TRUE") != 0) &&
559 (optlist
->adminDN
|| optlist
->adminPassword
)) {
561 gettext("ldapclient: adminDN and adminPassword must not "
562 "be specified if enableShadowUpdate is not set to TRUE \n"),
565 clientopts_free(optlist
);
566 exit(CLIENT_ERR_FAIL
);
569 /* Only one verb can be specified */
570 if ((op_init
+ op_manual
+ op_mod
+ op_uninit
+
571 op_list
+ op_genprofile
) != 1) {
573 clientopts_free(optlist
);
574 exit(CLIENT_ERR_FAIL
);
577 /* *** We passed semantic checking, so now do the operation *** */
580 CLIENT_FPUTS(gettext("Arguments parsed:\n"), stderr
);
585 /* handle "ldapclient list" here. err checking done in func */
589 gettext("Handling list option\n"),
591 retcode
= client_list(optlist
);
594 /* handle "ldapclient uninit" here */
598 gettext("Handling uninit option\n"),
600 retcode
= client_uninit(optlist
);
603 /* handle "ldapclient init" (profile) */
607 gettext("Handling init option\n"),
609 retcode
= client_init(optlist
);
612 /* handle "genprofile" here */
616 gettext("Handling genProfile\n"),
618 retcode
= client_genProfile(optlist
);
621 /* handle "ldapclient manual" here */
625 gettext("Handling manual option\n"),
627 retcode
= client_manual(optlist
);
630 /* handle "ldapclient mod" here */
634 gettext("Handling mod option\n"),
636 retcode
= client_mod(optlist
);
639 clientopts_free(optlist
);
640 if ((retcode
== CLIENT_SUCCESS
) ||
641 (retcode
== CLIENT_ERR_FAIL
) ||
642 (retcode
== CLIENT_ERR_CREDENTIAL
))
645 return (CLIENT_ERR_FAIL
);
649 client_list(clientopts_t
*arglist
)
651 ns_ldap_error_t
*errorp
;
652 int retcode
= CLIENT_SUCCESS
;
654 if (num_args(arglist
) > 0) {
656 gettext("No args supported with \"list\" option\n"),
659 return (CLIENT_ERR_FAIL
); /* exit code here ? */
661 if ((errorp
= __ns_ldap_print_config(mode_verbose
)) != NULL
) {
662 retcode
= CLIENT_ERR_FAIL
;
664 gettext("Cannot get print configuration\n"),
666 CLIENT_FPUTS(errorp
->message
, stderr
);
667 (void) __ns_ldap_freeError(&errorp
);
668 CLIENT_FPUTC('\n', stderr
);
675 client_uninit(clientopts_t
*arglist
)
677 int retcode
= CLIENT_SUCCESS
;
678 ns_ldap_self_gssapi_config_t config
= NS_LDAP_SELF_GSSAPI_CONFIG_NONE
;
682 gettext("Restoring machine to previous "
683 "configuration state\n"),
687 if (num_args(arglist
) > 0) {
689 gettext("No args supported with \"uninit\" option\n"),
692 return (CLIENT_ERR_FAIL
);
695 (void) __ns_ldap_self_gssapi_config(&config
);
697 retcode
= stop_services(STATE_SAVE
);
699 if (config
!= NS_LDAP_SELF_GSSAPI_CONFIG_NONE
)
700 (void) system("/usr/sbin/cryptoadm enable metaslot");
702 if (retcode
!= CLIENT_SUCCESS
) {
704 gettext("Errors stopping network services.\n"), stderr
);
705 /* restart whatever services we can */
706 (void) start_services(START_RESET
);
707 return (CLIENT_ERR_FAIL
);
710 retcode
= recover(STATE_SAVE
);
711 if (retcode
!= CLIENT_SUCCESS
) {
713 gettext("Cannot recover the configuration on "
716 (void) start_services(START_RESET
);
718 retcode
= start_services(START_UNINIT
);
719 if (retcode
!= CLIENT_SUCCESS
) {
721 gettext("Config restored but problems "
722 "encountered resetting network "
728 if (retcode
== CLIENT_SUCCESS
) {
730 gettext("System successfully recovered\n"),
738 * The following macro is used to do a __ns_ldap_setParam().
739 * On every call, the return code is checked, and if there was
740 * a problem then the error message is printed, the ldaperr
741 * is freed and we return from the function with the offending
742 * error return code. This macro keeps us from having to
743 * repeat this code for every call to setParam as was done
744 * in the previous incarnation of ldapclient.
746 * assumes a "retcode" variable is available for status
748 #define LDAP_SET_PARAM(argval, argdef) \
750 if (NULL != argval) { \
751 ns_ldap_error_t *ldaperr; \
752 retcode = __ns_ldap_setParam(argdef, (void *)argval, &ldaperr); \
753 if (retcode != NS_LDAP_SUCCESS) { \
754 if (NULL != ldaperr) { \
755 CLIENT_FPUTS(ldaperr->message, stderr); \
756 CLIENT_FPUTC('\n', stderr); \
757 (void) __ns_ldap_freeError(&ldaperr); \
759 return (retcode ? CLIENT_ERR_FAIL : CLIENT_SUCCESS); \
764 * The following macro is used to check if an arg has already been set
765 * and issues an error message, a usage message and then returns an error.
766 * This was made into a macro to avoid the duplication of this code many
767 * times in the function below.
769 #define LDAP_CHECK_INVALID(arg, param) \
771 CLIENT_FPRINTF(stderr, gettext("Invalid parameter (%s) " \
772 "specified\n"), param); \
774 return (CLIENT_ERR_FAIL); \
778 client_manual(clientopts_t
*arglist
)
782 ns_ldap_error_t
*errorp
;
785 int retcode
= CLIENT_SUCCESS
;
789 gettext("Manual failed: System domain not set and "
790 "no domainName specified.\n"),
792 return (CLIENT_ERR_FAIL
);
795 if (arglist
->defaultSearchBase
== NULL
) {
797 gettext("Manual failed: Missing required "
798 "defaultSearchBase attribute.\n"),
800 return (CLIENT_ERR_FAIL
);
803 if ((arglist
->defaultServerList
== NULL
) &&
804 (arglist
->preferredServerList
== NULL
)) {
806 gettext("Manual failed: Missing required "
807 "defaultServerList or preferredServerList "
810 return (CLIENT_ERR_FAIL
);
813 if (arglist
->profileTTL
!= NULL
) {
815 gettext("Manual aborted: profileTTL is not supported "
816 "in manual mode.\n"),
818 return (CLIENT_ERR_FAIL
);
821 if (arglist
->profileName
!= NULL
) {
823 gettext("Manual aborted: profileName is not supported "
824 "in manual mode.\n"),
826 return (CLIENT_ERR_FAIL
);
829 LDAP_CHECK_INVALID(arglist
->bindDN
, "bind DN");
830 LDAP_CHECK_INVALID(arglist
->bindPasswd
, "bind password");
832 __ns_ldap_setServer(TRUE
); /* Need this for _ns_setParam() */
833 __ns_ldap_default_config();
835 /* Set version to latest (not version 1) */
836 LDAP_SET_PARAM(NS_LDAP_VERSION
, NS_LDAP_FILE_VERSION_P
);
838 /* Set profileTTL to 0 since NO profile on manual */
839 LDAP_SET_PARAM(CACHETTL_OFF
, NS_LDAP_CACHETTL_P
);
841 /* Set additional valid params from command line */
842 LDAP_SET_PARAM(arglist
->authenticationMethod
, NS_LDAP_AUTH_P
);
843 LDAP_SET_PARAM(arglist
->defaultSearchBase
, NS_LDAP_SEARCH_BASEDN_P
);
844 LDAP_SET_PARAM(arglist
->credentialLevel
, NS_LDAP_CREDENTIAL_LEVEL_P
);
845 LDAP_SET_PARAM(arglist
->proxyDN
, NS_LDAP_BINDDN_P
);
846 LDAP_SET_PARAM(arglist
->enableShadowUpdate
,
847 NS_LDAP_ENABLE_SHADOW_UPDATE_P
);
848 LDAP_SET_PARAM(arglist
->adminDN
, NS_LDAP_ADMIN_BINDDN_P
);
849 LDAP_SET_PARAM(arglist
->searchTimeLimit
, NS_LDAP_SEARCH_TIME_P
);
850 LDAP_SET_PARAM(arglist
->preferredServerList
, NS_LDAP_SERVER_PREF_P
);
851 LDAP_SET_PARAM(arglist
->profileName
, NS_LDAP_PROFILE_P
);
852 LDAP_SET_PARAM(arglist
->followReferrals
, NS_LDAP_SEARCH_REF_P
);
853 LDAP_SET_PARAM(arglist
->defaultSearchScope
, NS_LDAP_SEARCH_SCOPE_P
);
854 LDAP_SET_PARAM(arglist
->bindTimeLimit
, NS_LDAP_BIND_TIME_P
);
855 LDAP_SET_PARAM(arglist
->proxyPassword
, NS_LDAP_BINDPASSWD_P
);
856 LDAP_SET_PARAM(arglist
->adminPassword
, NS_LDAP_ADMIN_BINDPASSWD_P
);
857 LDAP_SET_PARAM(arglist
->defaultServerList
, NS_LDAP_SERVERS_P
);
858 LDAP_SET_PARAM(arglist
->certificatePath
, NS_LDAP_HOST_CERTPATH_P
);
861 counter
< arglist
->serviceAuthenticationMethod
->count
;
865 arglist
->serviceAuthenticationMethod
->optlist
[counter
],
866 NS_LDAP_SERVICE_AUTH_METHOD_P
);
869 counter
< arglist
->serviceCredentialLevel
->count
;
873 arglist
->serviceCredentialLevel
->optlist
[counter
],
874 NS_LDAP_SERVICE_CRED_LEVEL_P
);
877 counter
< arglist
->objectclassMap
->count
;
880 LDAP_SET_PARAM(arglist
->objectclassMap
->optlist
[counter
],
881 NS_LDAP_OBJECTCLASSMAP_P
);
883 for (counter
= 0; counter
< arglist
->attributeMap
->count
; counter
++) {
884 LDAP_SET_PARAM(arglist
->attributeMap
->optlist
[counter
],
885 NS_LDAP_ATTRIBUTEMAP_P
);
888 counter
< arglist
->serviceSearchDescriptor
->count
;
892 arglist
->serviceSearchDescriptor
->optlist
[counter
],
893 NS_LDAP_SERVICE_SEARCH_DESC_P
);
896 retcode
= credCheck(arglist
);
897 if (retcode
== CLIENT_SUCCESS
)
898 retcode
= adminCredCheck(arglist
);
899 if (retcode
!= CLIENT_SUCCESS
) {
901 gettext("Error in setting up credentials\n"),
908 gettext("About to modify this machines "
909 "configuration by writing the files\n"),
912 /* get ready to start playing with files */
913 retcode
= stop_services(STATE_SAVE
);
914 if (retcode
!= CLIENT_SUCCESS
) {
916 gettext("Errors stopping network services.\n"), stderr
);
917 return (CLIENT_ERR_FAIL
);
920 /* Save orig versions of files */
921 retcode
= file_backup();
922 if (retcode
== CLIENT_ERR_RESTORE
) {
924 gettext("System not in state to enable ldap client.\n"),
927 reset_ret
= start_services(START_RESET
);
928 if (reset_ret
!= CLIENT_SUCCESS
) {
929 CLIENT_FPRINTF(stderr
, gettext("Error (%d) while "
930 "starting services during reset\n"),
934 } else if (retcode
!= CLIENT_SUCCESS
) {
936 gettext("Save of system configuration failed! "
937 "Attempting recovery.\n"),
939 retcode
= recover(STATE_NOSAVE
);
940 if (retcode
!= CLIENT_SUCCESS
) {
942 gettext("Recovery of systems configuration "
943 "failed. Manual intervention of "
944 "config files is required.\n"),
949 reset_ret
= start_services(START_RESET
);
950 if (reset_ret
!= CLIENT_SUCCESS
) {
951 CLIENT_FPRINTF(stderr
, gettext("Error (%d) while "
952 "starting services during reset\n"),
960 errorp
= __ns_ldap_DumpConfiguration(NSCONFIGFILE
);
961 if (errorp
!= NULL
) {
962 CLIENT_FPRINTF(stderr
,
963 gettext("%s manual: errorp is not NULL; %s\n"),
964 cmd
, errorp
->message
);
965 retcode
= recover(STATE_NOSAVE
);
966 if (retcode
!= CLIENT_SUCCESS
) {
968 gettext("Recovery of systems configuration "
969 "failed. Manual intervention of "
970 "config files is required.\n"),
974 reset_ret
= start_services(START_RESET
);
975 if (reset_ret
!= CLIENT_SUCCESS
) {
976 CLIENT_FPRINTF(stderr
, gettext("Error (%d) while "
977 "starting services during reset\n"),
980 (void) __ns_ldap_freeError(&errorp
);
981 return (CLIENT_ERR_FAIL
);
984 /* if (credargs(arglist)) */
985 errorp
= __ns_ldap_DumpConfiguration(NSCREDFILE
);
986 if (errorp
!= NULL
) {
987 CLIENT_FPRINTF(stderr
,
988 gettext("%s init: errorp is not NULL; %s\n"),
989 cmd
, errorp
->message
);
990 retcode
= recover(STATE_NOSAVE
);
991 if (retcode
!= CLIENT_SUCCESS
) {
993 gettext("Recovery of systems configuration "
994 "failed. Manual intervention of "
995 "config files is required.\n"),
999 reset_ret
= start_services(START_RESET
);
1000 if (reset_ret
!= CLIENT_SUCCESS
) {
1001 CLIENT_FPRINTF(stderr
, gettext("Error (%d) while "
1002 "starting services during reset\n"),
1005 (void) __ns_ldap_freeError(&errorp
);
1006 return (CLIENT_ERR_FAIL
);
1009 ret_copy
= system(CMD_CP
" " NSSWITCH_LDAP
" " NSSWITCH_CONF
);
1010 if (ret_copy
!= 0) {
1011 CLIENT_FPRINTF(stderr
,
1012 gettext("Error %d copying (%s) -> (%s)\n"),
1013 ret_copy
, NSSWITCH_LDAP
, NSSWITCH_CONF
);
1014 retcode
= recover(STATE_NOSAVE
);
1015 if (retcode
!= CLIENT_SUCCESS
) {
1017 gettext("Recovery of systems configuration "
1018 "failed. Manual intervention of "
1019 "config files is required.\n"),
1022 reset_ret
= start_services(START_RESET
);
1023 if (reset_ret
!= CLIENT_SUCCESS
) {
1024 CLIENT_FPRINTF(stderr
, gettext("Error (%d) while "
1025 "starting services during reset\n"),
1028 return (CLIENT_ERR_FAIL
);
1031 if ((domain_fp
= open(DOMAINNAME
, O_WRONLY
|O_CREAT
|O_TRUNC
,
1032 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IROTH
)) == -1) { /* 0644 */
1033 CLIENT_FPRINTF(stderr
, gettext("Cannot open %s\n"), DOMAINNAME
);
1034 retcode
= recover(STATE_NOSAVE
);
1035 if (retcode
!= CLIENT_SUCCESS
) {
1037 gettext("Recovery of systems configuration "
1038 "failed. Manual intervention of "
1039 "config files is required.\n"),
1041 return (CLIENT_ERR_FAIL
);
1043 reset_ret
= start_services(START_RESET
);
1044 if (reset_ret
!= CLIENT_SUCCESS
) {
1045 CLIENT_FPRINTF(stderr
, gettext("Error (%d) while "
1046 "starting services during reset\n"),
1049 return (CLIENT_ERR_FAIL
);
1051 (void) write(domain_fp
, dname
, strlen(dname
));
1052 (void) write(domain_fp
, "\n", 1);
1053 (void) close(domain_fp
);
1055 retcode
= start_services(START_INIT
);
1057 if (retcode
== CLIENT_SUCCESS
) {
1058 CLIENT_FPUTS(gettext("System successfully configured\n"),
1061 CLIENT_FPUTS(gettext("Error resetting system.\n"
1062 "Recovering old system settings.\n"), stderr
),
1064 /* stop any started services for recover */
1065 /* don't stomp on history of saved services state */
1066 reset_ret
= stop_services(STATE_NOSAVE
);
1067 if (reset_ret
!= CLIENT_SUCCESS
) {
1068 CLIENT_FPRINTF(stderr
, gettext("Error (%d) while "
1069 "stopping services during reset\n"),
1071 /* Coninue and try to recover what we can */
1073 reset_ret
= recover(STATE_NOSAVE
);
1074 if (reset_ret
!= CLIENT_SUCCESS
) {
1075 CLIENT_FPRINTF(stderr
, gettext("Error (%d) while "
1076 "recovering service files during "
1077 "reset\n"), reset_ret
);
1078 /* Continue and start what we can */
1080 reset_ret
= start_services(START_RESET
);
1081 if (reset_ret
!= CLIENT_SUCCESS
) {
1082 CLIENT_FPRINTF(stderr
, gettext("Error (%d) while "
1083 "starting services during reset\n"),
1092 client_mod(clientopts_t
*arglist
)
1096 ns_ldap_error_t
*errorp
;
1098 int retcode
= CLIENT_SUCCESS
;
1100 __ns_ldap_setServer(TRUE
); /* Need this for _ns_setParam() */
1101 if ((errorp
= __ns_ldap_LoadConfiguration()) != NULL
) {
1102 CLIENT_FPUTS(gettext("Cannot get load configuration\n"),
1104 CLIENT_FPUTS(errorp
->message
, stderr
);
1105 CLIENT_FPUTC('\n', stderr
);
1106 (void) __ns_ldap_freeError(&errorp
);
1107 return (CLIENT_ERR_FAIL
);
1110 if (arglist
->profileTTL
!= NULL
) {
1112 gettext("Mod aborted: profileTTL modification is "
1113 "not allowed in mod mode.\n"),
1115 return (CLIENT_ERR_FAIL
);
1118 if (arglist
->profileName
!= NULL
) {
1120 gettext("Mod aborted: profileName modification is "
1121 "not allowed. If you want to use profiles "
1122 "generate one with genProfile and load it "
1123 "on the server with ldapadd.\n"),
1125 return (CLIENT_ERR_FAIL
);
1128 LDAP_CHECK_INVALID(arglist
->bindDN
, "bind DN");
1129 LDAP_CHECK_INVALID(arglist
->bindPasswd
, "bind password");
1131 /* Set additional valid params from command line */
1132 LDAP_SET_PARAM(arglist
->authenticationMethod
, NS_LDAP_AUTH_P
);
1133 LDAP_SET_PARAM(arglist
->defaultSearchBase
, NS_LDAP_SEARCH_BASEDN_P
);
1134 LDAP_SET_PARAM(arglist
->credentialLevel
, NS_LDAP_CREDENTIAL_LEVEL_P
);
1135 LDAP_SET_PARAM(arglist
->proxyDN
, NS_LDAP_BINDDN_P
);
1136 LDAP_SET_PARAM(arglist
->adminDN
, NS_LDAP_ADMIN_BINDDN_P
);
1137 LDAP_SET_PARAM(arglist
->profileTTL
, NS_LDAP_CACHETTL_P
);
1138 LDAP_SET_PARAM(arglist
->searchTimeLimit
, NS_LDAP_SEARCH_TIME_P
);
1139 LDAP_SET_PARAM(arglist
->preferredServerList
, NS_LDAP_SERVER_PREF_P
);
1140 LDAP_SET_PARAM(arglist
->profileName
, NS_LDAP_PROFILE_P
);
1141 LDAP_SET_PARAM(arglist
->followReferrals
, NS_LDAP_SEARCH_REF_P
);
1142 LDAP_SET_PARAM(arglist
->defaultSearchScope
, NS_LDAP_SEARCH_SCOPE_P
);
1143 LDAP_SET_PARAM(arglist
->bindTimeLimit
, NS_LDAP_BIND_TIME_P
);
1144 LDAP_SET_PARAM(arglist
->proxyPassword
, NS_LDAP_BINDPASSWD_P
);
1145 LDAP_SET_PARAM(arglist
->adminPassword
, NS_LDAP_ADMIN_BINDPASSWD_P
);
1146 LDAP_SET_PARAM(arglist
->defaultServerList
, NS_LDAP_SERVERS_P
);
1147 LDAP_SET_PARAM(arglist
->enableShadowUpdate
,
1148 NS_LDAP_ENABLE_SHADOW_UPDATE_P
);
1149 LDAP_SET_PARAM(arglist
->certificatePath
, NS_LDAP_HOST_CERTPATH_P
);
1152 counter
< arglist
->serviceAuthenticationMethod
->count
;
1156 arglist
->serviceAuthenticationMethod
->optlist
[counter
],
1157 NS_LDAP_SERVICE_AUTH_METHOD_P
);
1160 counter
< arglist
->serviceCredentialLevel
->count
;
1164 arglist
->serviceCredentialLevel
->optlist
[counter
],
1165 NS_LDAP_SERVICE_CRED_LEVEL_P
);
1168 counter
< arglist
->objectclassMap
->count
;
1172 arglist
->objectclassMap
->optlist
[counter
],
1173 NS_LDAP_OBJECTCLASSMAP_P
);
1176 counter
< arglist
->attributeMap
->count
;
1180 arglist
->attributeMap
->optlist
[counter
],
1181 NS_LDAP_ATTRIBUTEMAP_P
);
1184 counter
< arglist
->serviceSearchDescriptor
->count
;
1188 arglist
->serviceSearchDescriptor
->optlist
[counter
],
1189 NS_LDAP_SERVICE_SEARCH_DESC_P
);
1192 retcode
= credCheck(arglist
);
1193 if (retcode
== CLIENT_SUCCESS
)
1194 retcode
= adminCredCheck(arglist
);
1195 if (retcode
!= CLIENT_SUCCESS
) {
1197 gettext("Error in setting up credentials\n"),
1204 gettext("About to modify this machines configuration "
1205 "by writing the files\n"),
1208 /* get ready to start playing with files */
1209 retcode
= stop_services(STATE_SAVE
);
1210 if (retcode
!= CLIENT_SUCCESS
) {
1212 gettext("Errors stopping network services.\n"), stderr
);
1213 return (CLIENT_ERR_FAIL
);
1216 /* Temporarily save orig versions of files */
1217 retcode
= mod_backup();
1218 if (retcode
!= CLIENT_SUCCESS
) {
1220 gettext("Unable to backup the ldap client files!\n"),
1227 /* Dump new files */
1228 errorp
= __ns_ldap_DumpConfiguration(NSCONFIGFILE
);
1229 if (errorp
!= NULL
) {
1230 CLIENT_FPRINTF(stderr
,
1231 gettext("%s mod: errorp is not NULL; %s\n"),
1232 cmd
, errorp
->message
);
1233 retcode
= mod_recover();
1234 if (retcode
!= CLIENT_SUCCESS
) {
1236 gettext("Recovery of systems configuration "
1237 "failed. Manual intervention of "
1238 "config files is required.\n"),
1241 (void) __ns_ldap_freeError(&errorp
);
1242 reset_ret
= start_services(START_RESET
);
1243 if (reset_ret
!= CLIENT_SUCCESS
) {
1244 CLIENT_FPRINTF(stderr
, gettext("Error (%d) while "
1245 "starting services during reset\n"),
1248 return (CLIENT_ERR_FAIL
);
1251 /* if (credargs(arglist)) */
1252 errorp
= __ns_ldap_DumpConfiguration(NSCREDFILE
);
1253 if (errorp
!= NULL
) {
1254 CLIENT_FPRINTF(stderr
,
1255 gettext("%s mod: errorp is not NULL; %s\n"),
1256 cmd
, errorp
->message
);
1257 retcode
= mod_recover();
1258 if (retcode
!= CLIENT_SUCCESS
) {
1260 gettext("Recovery of systems configuration "
1261 "failed. Manual intervention of "
1262 "config files is required.\n"),
1265 (void) __ns_ldap_freeError(&errorp
);
1266 reset_ret
= start_services(START_RESET
);
1267 if (reset_ret
!= CLIENT_SUCCESS
) {
1268 CLIENT_FPRINTF(stderr
, gettext("Error (%d) while "
1269 "starting services during reset\n"),
1272 return (CLIENT_ERR_FAIL
);
1275 if ((domain_fp
= open(DOMAINNAME
, O_WRONLY
|O_CREAT
|O_TRUNC
,
1276 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IROTH
)) == -1) { /* 0644 */
1277 CLIENT_FPRINTF(stderr
, gettext("Cannot open %s\n"), DOMAINNAME
);
1278 retcode
= mod_recover();
1279 if (retcode
!= CLIENT_SUCCESS
) {
1281 gettext("Recovery of systems configuration "
1282 "failed! Machine needs to be "
1286 reset_ret
= start_services(START_RESET
);
1287 if (reset_ret
!= CLIENT_SUCCESS
) {
1288 CLIENT_FPRINTF(stderr
, gettext("Error (%d) while "
1289 "starting services during reset\n"),
1292 return (CLIENT_ERR_FAIL
);
1294 (void) write(domain_fp
, dname
, strlen(dname
));
1295 (void) write(domain_fp
, "\n", 1);
1296 (void) close(domain_fp
);
1298 retcode
= start_services(START_INIT
);
1300 if (retcode
== CLIENT_SUCCESS
) {
1301 CLIENT_FPUTS(gettext("System successfully configured\n"),
1304 CLIENT_FPUTS(gettext("Error resetting system.\n"
1305 "Recovering old system settings.\n"), stderr
),
1307 /* stop any started services for recover */
1308 /* don't stomp on history of saved services state */
1309 reset_ret
= stop_services(STATE_NOSAVE
);
1310 if (reset_ret
!= CLIENT_SUCCESS
) {
1311 CLIENT_FPRINTF(stderr
, gettext("Error (%d) while "
1312 "stopping services during reset\n"),
1314 /* Coninue and try to recover what we can */
1316 reset_ret
= mod_recover();
1317 if (reset_ret
!= CLIENT_SUCCESS
) {
1318 CLIENT_FPRINTF(stderr
, gettext("Error (%d) while "
1319 "recovering service files during "
1320 "reset\n"), reset_ret
);
1321 /* Continue and start what we can */
1323 reset_ret
= start_services(START_RESET
);
1324 if (reset_ret
!= CLIENT_SUCCESS
) {
1325 CLIENT_FPRINTF(stderr
, gettext("Error (%d) while "
1326 "starting services during reset\n"),
1331 /* Cleanup temporary files created by mod_backup() */
1339 client_genProfile(clientopts_t
*arglist
)
1342 int retcode
; /* required for LDAP_SET_PARAM macro */
1343 ns_ldap_error_t
*errorp
;
1346 CLIENT_FPUTS(gettext("About to generate a profile\n"), stderr
);
1348 /* *** Check for invalid args *** */
1349 LDAP_CHECK_INVALID(arglist
->proxyDN
, "proxyDN");
1350 LDAP_CHECK_INVALID(arglist
->proxyPassword
, "proxyPassword");
1351 LDAP_CHECK_INVALID(arglist
->enableShadowUpdate
,
1352 "enableShadowUpdate");
1353 LDAP_CHECK_INVALID(arglist
->adminDN
, "adminDN");
1354 LDAP_CHECK_INVALID(arglist
->adminPassword
, "adminPassword");
1355 LDAP_CHECK_INVALID(arglist
->certificatePath
, "certificatePath");
1356 LDAP_CHECK_INVALID(arglist
->domainName
, "domainName");
1357 LDAP_CHECK_INVALID(arglist
->bindDN
, "bind DN");
1358 LDAP_CHECK_INVALID(arglist
->bindPasswd
, "bind password");
1359 /* *** End check for invalid args *** */
1361 if (arglist
->profileName
== NULL
) {
1364 gettext("No profile specified. "
1365 "Using \"default\"\n"),
1367 arglist
->profileName
= "default";
1370 __ns_ldap_setServer(TRUE
);
1371 __ns_ldap_default_config();
1373 /* Set version to latest (not version 1) */
1374 LDAP_SET_PARAM(NS_LDAP_VERSION
, NS_LDAP_FILE_VERSION_P
);
1376 /* Set additional valid params from command line */
1377 LDAP_SET_PARAM(arglist
->authenticationMethod
, NS_LDAP_AUTH_P
);
1378 LDAP_SET_PARAM(arglist
->defaultSearchBase
, NS_LDAP_SEARCH_BASEDN_P
);
1379 LDAP_SET_PARAM(arglist
->credentialLevel
, NS_LDAP_CREDENTIAL_LEVEL_P
);
1380 LDAP_SET_PARAM(arglist
->profileTTL
, NS_LDAP_CACHETTL_P
);
1381 LDAP_SET_PARAM(arglist
->searchTimeLimit
, NS_LDAP_SEARCH_TIME_P
);
1382 LDAP_SET_PARAM(arglist
->preferredServerList
, NS_LDAP_SERVER_PREF_P
);
1383 LDAP_SET_PARAM(arglist
->profileName
, NS_LDAP_PROFILE_P
);
1384 LDAP_SET_PARAM(arglist
->followReferrals
, NS_LDAP_SEARCH_REF_P
);
1385 LDAP_SET_PARAM(arglist
->defaultSearchScope
, NS_LDAP_SEARCH_SCOPE_P
);
1386 LDAP_SET_PARAM(arglist
->bindTimeLimit
, NS_LDAP_BIND_TIME_P
);
1387 LDAP_SET_PARAM(arglist
->defaultServerList
, NS_LDAP_SERVERS_P
);
1390 counter
< arglist
->serviceAuthenticationMethod
->count
;
1394 arglist
->serviceAuthenticationMethod
->optlist
[counter
],
1395 NS_LDAP_SERVICE_AUTH_METHOD_P
);
1398 counter
< arglist
->serviceCredentialLevel
->count
;
1402 arglist
->serviceCredentialLevel
->optlist
[counter
],
1403 NS_LDAP_SERVICE_CRED_LEVEL_P
);
1406 counter
< arglist
->objectclassMap
->count
;
1410 arglist
->objectclassMap
->optlist
[counter
],
1411 NS_LDAP_OBJECTCLASSMAP_P
);
1414 counter
< arglist
->attributeMap
->count
;
1418 arglist
->attributeMap
->optlist
[counter
],
1419 NS_LDAP_ATTRIBUTEMAP_P
);
1422 counter
< arglist
->serviceSearchDescriptor
->count
;
1426 arglist
->serviceSearchDescriptor
->optlist
[counter
],
1427 NS_LDAP_SERVICE_SEARCH_DESC_P
);
1430 errorp
= __ns_ldap_DumpLdif(NULL
);
1431 if (errorp
!= NULL
) {
1432 CLIENT_FPUTS(errorp
->message
, stderr
);
1433 CLIENT_FPUTC('\n', stderr
);
1434 (void) __ns_ldap_freeError(&errorp
);
1435 return (CLIENT_ERR_FAIL
);
1438 return (CLIENT_SUCCESS
);
1441 /* INET6_ADDRSTRLEN + ":" + <5-digit port> + some round-up */
1442 #define MAX_HOSTADDR_LEN (INET6_ADDRSTRLEN + 6 + 12)
1445 client_init(clientopts_t
*arglist
)
1448 int retcode
= CLIENT_SUCCESS
;
1449 ns_ldap_error_t
*errorp
;
1452 ns_standalone_conf_t cfg
= standaloneDefaults
;
1453 ns_auth_t auth
= {NS_LDAP_AUTH_NONE
,
1456 NS_LDAP_SASLOPT_NONE
};
1457 char peer
[MAX_HOSTADDR_LEN
];
1458 ns_auth_t
**authMethod
;
1464 gettext("About to configure machine by downloading "
1468 if (dname
== NULL
) {
1470 gettext("Init failed: System domain not set and "
1471 "no domainName specified.\n"),
1473 return (CLIENT_ERR_FAIL
);
1476 if (!arglist
->defaultServerList
) {
1477 CLIENT_FPUTS(gettext("Missing LDAP server address\n"), stderr
);
1478 return (CLIENT_ERR_FAIL
);
1481 /* *** Check for invalid args *** */
1482 LDAP_CHECK_INVALID(arglist
->defaultSearchBase
,
1483 "defaultSearchBase");
1484 LDAP_CHECK_INVALID(arglist
->profileTTL
,
1486 LDAP_CHECK_INVALID(arglist
->searchTimeLimit
,
1488 LDAP_CHECK_INVALID(arglist
->preferredServerList
,
1489 "preferredServerList");
1490 LDAP_CHECK_INVALID(arglist
->followReferrals
,
1492 LDAP_CHECK_INVALID(arglist
->defaultSearchScope
,
1493 "defaultSearchScope");
1494 LDAP_CHECK_INVALID(arglist
->bindTimeLimit
,
1497 LDAP_CHECK_INVALID(arglist
->objectclassMap
->count
,
1499 LDAP_CHECK_INVALID(arglist
->attributeMap
->count
,
1501 LDAP_CHECK_INVALID(arglist
->serviceAuthenticationMethod
->count
,
1502 "serviceAuthenticationMethod");
1503 LDAP_CHECK_INVALID(arglist
->serviceCredentialLevel
->count
,
1504 "serviceCredentialLevel");
1505 LDAP_CHECK_INVALID(arglist
->serviceSearchDescriptor
->count
,
1506 "serviceSearchDescriptor");
1507 /* *** End check for invalid args *** */
1509 if (arglist
->profileName
== NULL
) {
1512 gettext("No profile specified. "
1513 "Using \"default\"\n"),
1515 arglist
->profileName
= "default";
1518 (void) strncpy(peer
, arglist
->defaultServerList
, MAX_HOSTADDR_LEN
- 1);
1519 if (separatePort(peer
, &cfg
.SA_SERVER
, &cfg
.SA_PORT
) > 0) {
1520 return (CLIENT_ERR_FAIL
);
1523 if (arglist
->bindDN
!= NULL
) {
1524 cfg
.SA_CRED
= "proxy";
1526 * We don't want to force users to always specify authentication
1527 * method when we can infer it. If users want SSL, they would
1528 * have to specify appropriate -a though.
1530 auth
.type
= NS_LDAP_AUTH_SIMPLE
;
1531 if (arglist
->bindPasswd
== NULL
) {
1532 arglist
->bindPasswd
=
1533 getpassphrase("Bind Password:");
1534 if (arglist
->bindPasswd
== NULL
) {
1535 CLIENT_FPUTS(gettext("Get password failed\n"),
1538 if (gStartLdap
== START_RESET
)
1539 (void) start_service(LDAP_FMRI
, B_TRUE
);
1541 return (CLIENT_ERR_CREDENTIAL
);
1545 cfg
.SA_BIND_DN
= arglist
->bindDN
;
1546 cfg
.SA_BIND_PWD
= arglist
->bindPasswd
;
1548 if (arglist
->authenticationMethod
!= NULL
) {
1549 if (__ns_ldap_initAuth(arglist
->authenticationMethod
,
1550 &auth
, &errorp
) != NS_LDAP_SUCCESS
) {
1551 if (errorp
!= NULL
) {
1552 CLIENT_FPRINTF(stderr
, "%s", errorp
->message
);
1553 (void) __ns_ldap_freeError(&errorp
);
1556 if (gStartLdap
== START_RESET
)
1557 (void) start_service(LDAP_FMRI
, B_TRUE
);
1559 return (CLIENT_ERR_FAIL
);
1561 cfg
.SA_AUTH
= &auth
;
1563 cfg
.SA_CRED
= arglist
->credentialLevel
;
1565 cfg
.SA_DOMAIN
= arglist
->domainName
;
1566 cfg
.SA_PROFILE_NAME
= arglist
->profileName
;
1567 cfg
.SA_CERT_PATH
= arglist
->certificatePath
;
1569 cfg
.type
= NS_LDAP_SERVER
;
1571 if (__ns_ldap_initStandalone(&cfg
, &errorp
) != NS_LDAP_SUCCESS
) {
1572 if (errorp
!= NULL
) {
1573 CLIENT_FPRINTF(stderr
, "%s", errorp
->message
);
1574 (void) __ns_ldap_freeError(&errorp
);
1577 if (gStartLdap
== START_RESET
)
1578 (void) start_service(LDAP_FMRI
, B_TRUE
);
1580 return (CLIENT_ERR_FAIL
);
1583 if (arglist
->proxyDN
!= NULL
&& arglist
->proxyPassword
== NULL
) {
1584 arglist
->proxyPassword
= getpassphrase("Proxy Bind Password:");
1585 if (arglist
->proxyPassword
== NULL
) {
1586 CLIENT_FPUTS(gettext("Get password failed\n"), stderr
);
1588 if (gStartLdap
== START_RESET
)
1589 (void) start_service(LDAP_FMRI
, B_TRUE
);
1591 return (CLIENT_ERR_CREDENTIAL
);
1594 if (arglist
->proxyDN
!= NULL
&& arglist
->proxyPassword
!= NULL
) {
1595 if (__ns_ldap_setParam(NS_LDAP_BINDDN_P
,
1596 arglist
->proxyDN
, &errorp
) != NS_LDAP_SUCCESS
) {
1597 if (errorp
!= NULL
) {
1598 CLIENT_FPRINTF(stderr
, "%s", errorp
->message
);
1599 (void) __ns_ldap_freeError(&errorp
);
1601 return (CLIENT_ERR_CREDENTIAL
);
1603 if (__ns_ldap_setParam(NS_LDAP_BINDPASSWD_P
,
1604 arglist
->proxyPassword
, &errorp
) != NS_LDAP_SUCCESS
) {
1605 if (errorp
!= NULL
) {
1606 CLIENT_FPRINTF(stderr
, "%s", errorp
->message
);
1607 (void) __ns_ldap_freeError(&errorp
);
1609 return (CLIENT_ERR_CREDENTIAL
);
1613 if (arglist
->enableShadowUpdate
!= NULL
) {
1614 LDAP_SET_PARAM(arglist
->enableShadowUpdate
,
1615 NS_LDAP_ENABLE_SHADOW_UPDATE_P
);
1618 if (arglist
->enableShadowUpdate
&&
1619 strcasecmp(arglist
->enableShadowUpdate
, "TRUE") == 0 &&
1620 arglist
->adminDN
!= NULL
&& arglist
->adminPassword
== NULL
) {
1621 arglist
->adminPassword
= getpassphrase("admin Bind Password:");
1622 if (arglist
->adminPassword
== NULL
) {
1623 CLIENT_FPUTS(gettext("Get password failed\n"), stderr
);
1625 if (gStartLdap
== START_RESET
)
1626 (void) start_service(LDAP_FMRI
, B_TRUE
);
1628 return (CLIENT_ERR_CREDENTIAL
);
1631 if (arglist
->adminDN
!= NULL
&& arglist
->adminPassword
!= NULL
) {
1632 if (__ns_ldap_setParam(NS_LDAP_ADMIN_BINDDN_P
,
1633 arglist
->adminDN
, &errorp
) != NS_LDAP_SUCCESS
) {
1634 if (errorp
!= NULL
) {
1635 CLIENT_FPRINTF(stderr
, "%s\n", errorp
->message
);
1636 (void) __ns_ldap_freeError(&errorp
);
1638 return (CLIENT_ERR_CREDENTIAL
);
1640 if (__ns_ldap_setParam(NS_LDAP_ADMIN_BINDPASSWD_P
,
1641 arglist
->adminPassword
, &errorp
) != NS_LDAP_SUCCESS
) {
1642 if (errorp
!= NULL
) {
1643 CLIENT_FPRINTF(stderr
, "%s\n", errorp
->message
);
1644 (void) __ns_ldap_freeError(&errorp
);
1646 return (CLIENT_ERR_CREDENTIAL
);
1650 if (arglist
->authenticationMethod
!= NULL
) {
1651 if (__ns_ldap_getParam(NS_LDAP_AUTH_P
,
1652 (void ***)&authMethod
, &errorp
) != NS_LDAP_SUCCESS
) {
1653 if (errorp
!= NULL
) {
1654 CLIENT_FPRINTF(stderr
, "%s", errorp
->message
);
1655 (void) __ns_ldap_freeError(&errorp
);
1657 return (CLIENT_ERR_CREDENTIAL
);
1660 if (authMethod
!= NULL
) {
1661 for (i
= 0; authMethod
[i
] != NULL
; ++i
) {
1662 if (authMethod
[i
]->type
== auth
.type
) {
1667 if (authMethod
[i
] == NULL
) {
1668 CLIENT_FPRINTF(stderr
, gettext(
1669 "Warning: init authentication method "
1670 "not found in DUAConfigProfile.\n"));
1673 CLIENT_FPRINTF(stderr
,
1675 "Warning: init authentication"
1676 "method using secondary "
1677 "authentication method from "
1678 "DUAConfigProfile.\n"));
1681 (void) __ns_ldap_freeParam((void ***) &authMethod
);
1685 if (arglist
->credentialLevel
!= NULL
) {
1686 if (__ns_ldap_getParam(NS_LDAP_CREDENTIAL_LEVEL_P
,
1687 (void ***)&credLevel
, &errorp
) != NS_LDAP_SUCCESS
) {
1688 if (errorp
!= NULL
) {
1689 CLIENT_FPRINTF(stderr
, "%s", errorp
->message
);
1690 (void) __ns_ldap_freeError(&errorp
);
1692 return (CLIENT_ERR_CREDENTIAL
);
1694 if (credLevel
!= NULL
) {
1695 for (i
= 0; credLevel
[i
] != NULL
; ++i
) {
1696 switch (*credLevel
[i
]) {
1697 case NS_LDAP_CRED_ANON
:
1700 case NS_LDAP_CRED_PROXY
:
1703 case NS_LDAP_CRED_SELF
:
1710 arglist
->credentialLevel
) == 0) {
1714 if (credLevel
[i
] == NULL
) {
1715 CLIENT_FPRINTF(stderr
, gettext(
1716 "Warning: init credential level not found "
1717 "in DUAConfigProfile.\n"));
1720 CLIENT_FPRINTF(stderr
,
1722 "init credential level using "
1723 "secondary credential level from "
1724 "DUAConfigProfile.\n"));
1727 (void) __ns_ldap_freeParam((void ***) &credLevel
);
1731 retcode
= credCheck(arglist
);
1732 if (retcode
== CLIENT_SUCCESS
)
1733 retcode
= adminCredCheck(arglist
);
1734 if (retcode
!= CLIENT_SUCCESS
) {
1736 gettext("Error in setting up credentials\n"), stderr
);
1738 if (gStartLdap
== START_RESET
)
1739 (void) start_service(LDAP_FMRI
, B_TRUE
);
1746 gettext("About to modify this machines configuration "
1747 "by writing the files\n"),
1750 /* get ready to start playing with files */
1751 retcode
= stop_services(STATE_SAVE
);
1752 if (retcode
!= CLIENT_SUCCESS
) {
1754 gettext("Errors stopping network services.\n"), stderr
);
1756 if (gStartLdap
== START_RESET
)
1757 (void) start_service(LDAP_FMRI
, B_TRUE
);
1759 return (CLIENT_ERR_FAIL
);
1762 /* Save orig versions of files */
1763 retcode
= file_backup();
1764 if (retcode
== CLIENT_ERR_RESTORE
) {
1766 gettext("System not in state to enable ldap client.\n"),
1771 } else if (retcode
!= CLIENT_SUCCESS
) {
1773 gettext("Save of system configuration failed. "
1774 "Attempting recovery.\n"),
1776 retcode
= recover(STATE_NOSAVE
);
1777 if (retcode
!= CLIENT_SUCCESS
) {
1779 gettext("Recovery of systems configuration "
1780 "failed. Manual intervention of "
1781 "config files is required.\n"),
1785 reset_ret
= start_services(START_RESET
);
1786 if (reset_ret
!= CLIENT_SUCCESS
) {
1787 CLIENT_FPRINTF(stderr
, gettext("Error (%d) while "
1788 "starting services during reset\n"),
1795 /* Dump new files */
1796 errorp
= __ns_ldap_DumpConfiguration(NSCONFIGFILE
);
1797 if (NULL
!= errorp
) {
1798 CLIENT_FPRINTF(stderr
,
1799 gettext("%s init: errorp is not NULL; %s\n"),
1800 cmd
, errorp
->message
);
1801 retcode
= recover(STATE_NOSAVE
);
1802 if (retcode
!= CLIENT_SUCCESS
) {
1804 gettext("Recovery of systems configuration "
1805 "failed. Manual intervention of "
1806 "config files is required.\n"),
1808 return (CLIENT_ERR_FAIL
);
1810 (void) __ns_ldap_freeError(&errorp
);
1811 reset_ret
= start_services(START_RESET
);
1812 if (reset_ret
!= CLIENT_SUCCESS
) {
1813 CLIENT_FPRINTF(stderr
, gettext("Error (%d) while "
1814 "starting services during reset\n"),
1817 return (CLIENT_ERR_FAIL
);
1820 /* if (credargs(arglist)) */
1821 errorp
= __ns_ldap_DumpConfiguration(NSCREDFILE
);
1822 if (NULL
!= errorp
) {
1823 CLIENT_FPRINTF(stderr
,
1824 gettext("%s init: errorp is not NULL; %s\n"),
1825 cmd
, errorp
->message
);
1826 retcode
= recover(STATE_NOSAVE
);
1827 if (retcode
!= CLIENT_SUCCESS
) {
1829 gettext("Recovery of systems configuration "
1830 "failed. Manual intervention of "
1831 "config files is required.\n"),
1833 return (CLIENT_ERR_FAIL
);
1835 (void) __ns_ldap_freeError(&errorp
);
1836 reset_ret
= start_services(START_RESET
);
1837 if (reset_ret
!= CLIENT_SUCCESS
) {
1838 CLIENT_FPRINTF(stderr
, gettext("Error (%d) while "
1839 "starting services during reset\n"),
1842 return (CLIENT_ERR_FAIL
);
1845 ret_copy
= system(CMD_CP
" " NSSWITCH_LDAP
" " NSSWITCH_CONF
);
1846 if (ret_copy
!= 0) {
1847 CLIENT_FPRINTF(stderr
,
1848 gettext("Error %d copying (%s) -> (%s)\n"),
1849 ret_copy
, NSSWITCH_LDAP
, NSSWITCH_CONF
);
1850 retcode
= recover(STATE_NOSAVE
);
1851 if (retcode
!= CLIENT_SUCCESS
) {
1853 gettext("Recovery of systems configuration "
1854 "failed. Manual intervention of "
1855 "config files is required.\n"),
1858 reset_ret
= start_services(START_RESET
);
1859 if (reset_ret
!= CLIENT_SUCCESS
) {
1860 CLIENT_FPRINTF(stderr
, gettext("Error (%d) while "
1861 "starting services during reset\n"),
1864 return (CLIENT_ERR_FAIL
);
1867 if ((profile_fp
= open(DOMAINNAME
, O_WRONLY
|O_CREAT
|O_TRUNC
,
1868 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IROTH
)) == -1) { /* 0644 */
1869 CLIENT_FPRINTF(stderr
, gettext("Cannot open %s\n"), DOMAINNAME
);
1870 retcode
= recover(STATE_NOSAVE
);
1871 if (retcode
!= CLIENT_SUCCESS
) {
1873 gettext("Recovery of systems configuration "
1874 "failed. Manual intervention of "
1875 "config files is required.\n"),
1877 return (CLIENT_ERR_FAIL
);
1879 reset_ret
= start_services(START_RESET
);
1880 if (reset_ret
!= CLIENT_SUCCESS
) {
1881 CLIENT_FPRINTF(stderr
, gettext("Error (%d) while "
1882 "starting services during reset\n"),
1885 return (CLIENT_ERR_FAIL
);
1887 (void) write(profile_fp
, dname
, strlen(dname
));
1888 (void) write(profile_fp
, "\n", 1);
1889 (void) close(profile_fp
);
1891 retcode
= start_services(START_INIT
);
1893 if (retcode
== CLIENT_SUCCESS
) {
1894 CLIENT_FPUTS(gettext("System successfully configured\n"),
1897 CLIENT_FPUTS(gettext("Error resetting system.\n"
1898 "Recovering old system settings.\n"), stderr
),
1900 /* stop any started services for recover */
1901 /* don't stomp on history of saved services state */
1902 reset_ret
= stop_services(STATE_NOSAVE
);
1903 if (reset_ret
!= CLIENT_SUCCESS
) {
1904 CLIENT_FPRINTF(stderr
, gettext("Error (%d) while "
1905 "stopping services during reset\n"),
1907 /* Coninue and try to recover what we can */
1909 reset_ret
= recover(STATE_NOSAVE
);
1910 if (reset_ret
!= CLIENT_SUCCESS
) {
1911 CLIENT_FPRINTF(stderr
, gettext("Error (%d) while "
1912 "recovering service files during "
1913 "reset\n"), reset_ret
);
1914 /* Continue and start what we can */
1916 reset_ret
= start_services(START_RESET
);
1917 if (reset_ret
!= CLIENT_SUCCESS
) {
1918 CLIENT_FPRINTF(stderr
, gettext("Error (%d) while "
1919 "starting services during reset\n"),
1935 CLIENT_FPRINTF(stderr
,
1936 gettext("Usage: %s [-v | -q] init | manual | mod | "
1937 "list | uninit [<args>]\n"),
1940 CLIENT_FPRINTF(stderr
,
1941 gettext("\n %s [-v | -q] [-a authenticationMethod]"
1942 " [-D bindDN]\n\t[-w bindPassword] [-j passswdFile]"
1943 " [-y proxyPasswordFile]\n\t"
1944 "[-z adminPasswordFile] init [<args>]\n"),
1948 gettext("\nSet up a server or workstation as a "
1949 "client of an LDAP namespace.\n"),
1951 } else { /* genprofile */
1952 CLIENT_FPRINTF(stderr
,
1953 gettext("Usage: %s [-v | -q] genprofile "
1954 "-a profileName=<name> "
1955 "-a defaultSearchBase=<base> <args>\n"),
1959 gettext("\nGenerate a profile used to set up clients "
1960 "of an LDAP namespace.\n"),
1964 gettext("<args> take the form of \'-a attrName=attrVal\' as "
1965 "described in the\n"),
1967 CLIENT_FPUTS(gettext("man page: ldapclient(1M)\n"), stderr
);
1972 * stop_services is called to stop network services prior to their
1973 * config files being moved/changed. In case a later recovery is needed
1974 * (an error occurs during config), we detect whether the service is
1975 * running and store that info so that a reset will only start services
1976 * that were stopped here.
1978 * In terms of SMF, this translates to disabling the services. So we
1979 * try to disable them if they are in any other state
1982 * sendmail, nscd, autofs, ldap.client, nisd (rpc), inetinit(domainname)
1985 stop_services(int saveState
)
1990 CLIENT_FPUTS(gettext("Stopping network services\n"), stderr
);
1993 if (!is_service(SENDMAIL_FMRI
, SCF_STATE_STRING_DISABLED
)) {
1995 CLIENT_FPUTS(gettext("Stopping sendmail\n"), stderr
);
1996 ret
= disable_service(SENDMAIL_FMRI
, B_TRUE
);
1997 if (ret
!= CLIENT_SUCCESS
) {
1998 /* Not serious, but tell user what to do */
1999 CLIENT_FPRINTF(stderr
, gettext("Stopping sendmail "
2000 "failed with (%d). You may need to restart "
2001 "it manually for changes to take effect.\n"),
2003 } else enableFlag
|= SENDMAIL_ON
;
2006 CLIENT_FPUTS(gettext("sendmail not running\n"), stderr
);
2009 if (!is_service(NSCD_FMRI
, SCF_STATE_STRING_DISABLED
)) {
2011 CLIENT_FPUTS(gettext("Stopping nscd\n"), stderr
);
2012 ret
= disable_service(NSCD_FMRI
, B_TRUE
);
2013 if (ret
!= CLIENT_SUCCESS
) {
2014 CLIENT_FPRINTF(stderr
, gettext("Stopping nscd "
2015 "failed with (%d)\n"), ret
);
2016 return (CLIENT_ERR_FAIL
);
2017 } else enableFlag
|= NSCD_ON
;
2020 CLIENT_FPUTS(gettext("nscd not running\n"), stderr
);
2023 if (!is_service(AUTOFS_FMRI
, SCF_STATE_STRING_DISABLED
)) {
2025 CLIENT_FPUTS(gettext("Stopping autofs\n"), stderr
);
2026 ret
= disable_service(AUTOFS_FMRI
, B_TRUE
);
2027 if (ret
!= CLIENT_SUCCESS
) {
2028 /* Not serious, but tell user what to do */
2029 CLIENT_FPRINTF(stderr
, gettext("Stopping autofs "
2030 "failed with (%d). You may need to restart "
2031 "it manually for changes to take effect.\n"),
2033 } else enableFlag
|= AUTOFS_ON
;
2036 CLIENT_FPUTS(gettext("autofs not running\n"), stderr
);
2039 if (!is_service(LDAP_FMRI
, SCF_STATE_STRING_DISABLED
)) {
2041 gStartLdap
= START_RESET
;
2043 CLIENT_FPUTS(gettext("Stopping ldap\n"), stderr
);
2044 ret
= disable_service(LDAP_FMRI
, B_TRUE
);
2045 if (ret
!= CLIENT_SUCCESS
) {
2046 CLIENT_FPRINTF(stderr
, gettext("Stopping ldap "
2047 "failed with (%d)\n"), ret
);
2048 return (CLIENT_ERR_FAIL
);
2052 CLIENT_FPUTS(gettext("ldap not running\n"),
2056 if (!is_service(YP_FMRI
, SCF_STATE_STRING_DISABLED
)) {
2058 gStartYp
= START_RESET
;
2060 CLIENT_FPUTS(gettext("Stopping nis(yp)\n"), stderr
);
2061 ret
= disable_service(YP_FMRI
, B_TRUE
);
2063 CLIENT_FPRINTF(stderr
, gettext("Stopping nis(yp) "
2064 "failed with (%d)\n"), ret
);
2065 return (CLIENT_ERR_FAIL
);
2069 CLIENT_FPUTS(gettext("nis(yp) not running\n"),
2073 return (CLIENT_SUCCESS
);
2077 * start_services is called to start up network services after config
2078 * files have all been setup or recovered. In the case of an error, the
2079 * files will be recovered and start_services will be called with the
2080 * "reset" flag set so that only those services that were earlier stopped
2081 * will be started. If it is not a reset, then the services associated
2082 * with files "recovered" will attempt to be started.
2085 start_services(int flag
)
2087 int sysret
, retcode
= CLIENT_SUCCESS
, rc
= NS_LDAP_SUCCESS
;
2089 char domainname
[BUFSIZ
];
2090 char cmd_domain_start
[BUFSIZ
];
2092 ns_ldap_self_gssapi_config_t config
= NS_LDAP_SELF_GSSAPI_CONFIG_NONE
;
2093 ns_ldap_error_t
*errorp
= NULL
;
2096 CLIENT_FPUTS(gettext("Starting network services\n"), stderr
);
2099 /* Read in current defaultdomain so we can set it */
2100 domain_fp
= fopen(DOMAINNAME
, "r");
2101 if (domain_fp
== NULL
) {
2102 CLIENT_FPRINTF(stderr
, gettext("Error opening defaultdomain "
2104 /* if we did an ldap init, we must have domain */
2105 if (flag
== START_INIT
)
2106 return (CLIENT_ERR_FAIL
);
2108 if (fgets(domainname
, BUFSIZ
, domain_fp
) == NULL
) {
2109 CLIENT_FPUTS(gettext("Error reading defaultdomain\n"),
2111 return (CLIENT_ERR_FAIL
);
2114 if (fclose(domain_fp
) != 0) {
2115 CLIENT_FPRINTF(stderr
,
2116 gettext("Error closing defaultdomain (%d)\n"),
2118 return (CLIENT_ERR_FAIL
);
2120 domainlen
= strlen(domainname
);
2121 /* sanity check to make sure sprintf will fit */
2122 if (domainlen
> (BUFSIZE
- sizeof (CMD_DOMAIN_START
) -
2123 sizeof (TO_DEV_NULL
) - 3)) {
2124 CLIENT_FPUTS(gettext("Specified domainname is "
2125 "too large\n"), stderr
);
2126 return (CLIENT_ERR_FAIL
);
2128 if (domainname
[domainlen
-1] == '\n')
2129 domainname
[domainlen
-1] = 0;
2130 /* buffer size is checked above */
2131 (void) snprintf(cmd_domain_start
, BUFSIZ
, "%s %s %s",
2132 CMD_DOMAIN_START
, domainname
, TO_DEV_NULL
);
2136 * We can be starting services after an init in which case
2137 * we want to start ldap and not start yp.
2139 if (flag
== START_INIT
) {
2140 sysret
= system(cmd_domain_start
);
2142 CLIENT_FPRINTF(stderr
, "start: %s %s... %s\n",
2143 CMD_DOMAIN_START
, domainname
,
2144 (sysret
== 0) ? gettext("success") :
2147 CLIENT_FPRINTF(stderr
, gettext("\"%s\" returned: %d\n"),
2148 CMD_DOMAIN_START
, sysret
);
2150 retcode
= CLIENT_ERR_FAIL
;
2153 if ((rc
= __ns_ldap_self_gssapi_config(&config
)) !=
2155 CLIENT_FPRINTF(stderr
, gettext("Error (%d) while "
2156 "checking sasl/GSSAPI configuration\n"),
2158 retcode
= CLIENT_ERR_FAIL
;
2161 if (config
!= NS_LDAP_SELF_GSSAPI_CONFIG_NONE
) {
2163 rc
= __ns_ldap_check_dns_preq(
2164 1, mode_verbose
, mode_quiet
,
2165 NSSWITCH_LDAP
, config
, &errorp
);
2167 (void) __ns_ldap_freeError(&errorp
);
2169 if (rc
!= NS_LDAP_SUCCESS
)
2170 retcode
= CLIENT_ERR_FAIL
;
2173 if (rc
== NS_LDAP_SUCCESS
&&
2174 start_service(LDAP_FMRI
, B_TRUE
) != CLIENT_SUCCESS
)
2175 retcode
= CLIENT_ERR_FAIL
;
2177 if (config
!= NS_LDAP_SELF_GSSAPI_CONFIG_NONE
&&
2178 rc
== NS_LDAP_SUCCESS
&& retcode
== CLIENT_SUCCESS
) {
2179 rc
= __ns_ldap_check_gssapi_preq(
2180 1, mode_verbose
, mode_quiet
, config
,
2183 (void) __ns_ldap_freeError(&errorp
);
2185 if (rc
!= NS_LDAP_SUCCESS
)
2186 retcode
= CLIENT_ERR_FAIL
;
2189 /* No YP after init */
2191 * Or we can be starting services after an uninit or error
2192 * recovery. We want to start whatever services were running
2193 * before. In the case of error recovery, it is the services
2194 * that were running before we stopped them (flags set in
2195 * stop_services). If it is an uninit then we determine
2196 * which services to start based on the files we recovered
2197 * (flags set in recover).
2200 /* uninit and recover should set flags of what to start */
2202 sysret
= system(cmd_domain_start
);
2204 CLIENT_FPRINTF(stderr
, "start: %s %s... %s\n",
2205 CMD_DOMAIN_START
, domainname
,
2206 (sysret
== 0) ? gettext("success") :
2209 CLIENT_FPRINTF(stderr
, gettext("\"%s\" "
2211 CMD_DOMAIN_START
, sysret
);
2213 retcode
= CLIENT_ERR_FAIL
;
2217 if (gStartLdap
== flag
) {
2218 if (!(is_service(LDAP_FMRI
, SCF_STATE_STRING_ONLINE
)))
2219 if (start_service(LDAP_FMRI
, B_TRUE
)
2221 retcode
= CLIENT_ERR_FAIL
;
2224 if (gStartYp
== flag
) {
2225 if (!(is_service(YP_FMRI
, SCF_STATE_STRING_ONLINE
)))
2226 (void) start_service(YP_FMRI
, B_TRUE
);
2229 if ((enableFlag
& AUTOFS_ON
) &&
2230 !(is_service(AUTOFS_FMRI
, SCF_STATE_STRING_ONLINE
)))
2231 (void) start_service(AUTOFS_FMRI
, B_TRUE
);
2233 if ((enableFlag
& NSCD_ON
) &&
2234 !(is_service(NSCD_FMRI
, SCF_STATE_STRING_ONLINE
)))
2235 (void) start_service(NSCD_FMRI
, B_TRUE
);
2238 if (flag
== START_INIT
&& config
!= NS_LDAP_SELF_GSSAPI_CONFIG_NONE
&&
2239 retcode
== CLIENT_SUCCESS
&&
2240 !(is_service(NSCD_FMRI
, SCF_STATE_STRING_ONLINE
))) {
2241 CLIENT_FPRINTF(stderr
, "start: %s\n",
2242 gettext("self/sasl/GSSAPI is configured"
2243 " but nscd is not online"));
2244 retcode
= CLIENT_ERR_FAIL
;
2248 if ((enableFlag
& SENDMAIL_ON
) &&
2249 !(is_service(SENDMAIL_FMRI
, SCF_STATE_STRING_ONLINE
)))
2250 (void) start_service(SENDMAIL_FMRI
, B_TRUE
);
2253 * Restart name-service milestone so that any consumer
2254 * which depends on it will be restarted.
2256 (void) restart_service(NS_MILESTONE_FMRI
, B_TRUE
);
2261 * credCheck is called to check if credentials are required for this
2262 * configuration. Currently, this means that if any credentialLevel is
2263 * proxy and any authenticationMethod is something other than none, then
2264 * credential info is required (proxyDN and proxyPassword).
2267 credCheck(clientopts_t
*arglist
)
2271 ns_auth_t
**authMethod
;
2272 char **proxyDN
, **proxyPassword
;
2273 ns_ldap_error_t
*errorp
;
2274 int credProxy
, authNotNone
;
2277 /* If credentialLevel is proxy, make sure we have proxyDN and proxyPassword */
2278 retcode
= __ns_ldap_getParam(NS_LDAP_CREDENTIAL_LEVEL_P
,
2279 (void ***)&credLevel
, &errorp
);
2281 CLIENT_FPRINTF(stderr
,
2282 gettext("Error %d while trying to retrieve "
2285 return (CLIENT_ERR_FAIL
);
2287 retcode
= __ns_ldap_getParam(NS_LDAP_AUTH_P
,
2288 (void ***)&authMethod
, &errorp
);
2290 CLIENT_FPRINTF(stderr
,
2291 gettext("Error %d while trying to retrieve "
2292 "authMethod\n"), retcode
);
2293 return (CLIENT_ERR_FAIL
);
2295 retcode
= __ns_ldap_getParam(NS_LDAP_BINDDN_P
,
2296 (void ***)&proxyDN
, &errorp
);
2298 CLIENT_FPRINTF(stderr
,
2299 gettext("Error %d while trying to retrieve proxyDN\n"),
2301 return (CLIENT_ERR_FAIL
);
2303 retcode
= __ns_ldap_getParam(NS_LDAP_BINDPASSWD_P
,
2304 (void ***)&proxyPassword
, &errorp
);
2306 CLIENT_FPRINTF(stderr
,
2307 gettext("Error %d while trying to retrieve "
2308 "proxyPassword\n"), retcode
);
2309 return (CLIENT_ERR_FAIL
);
2313 CLIENT_FPRINTF(stderr
,
2314 gettext("Proxy DN: %s\n"),
2315 (proxyDN
&& proxyDN
[0]) ? proxyDN
[0] : "NULL");
2316 CLIENT_FPRINTF(stderr
,
2317 gettext("Proxy password: %s\n"),
2318 (proxyPassword
&& proxyPassword
[0]) ?
2319 proxyPassword
[0] : "NULL");
2322 credProxy
= 0; /* flag to indicate if we have a credLevel of proxy */
2323 for (counter
= 0; credLevel
&& credLevel
[counter
] != NULL
; counter
++) {
2325 CLIENT_FPRINTF(stderr
,
2326 gettext("Credential level: %d\n"),
2327 *credLevel
[counter
]);
2328 if (*credLevel
[counter
] == NS_LDAP_CRED_PROXY
) {
2334 authNotNone
= 0; /* flag for authMethod other than none */
2336 authMethod
&& authMethod
[counter
] != NULL
;
2340 CLIENT_FPRINTF(stderr
,
2341 gettext("Authentication method: %d\n"),
2342 authMethod
[counter
]->type
);
2343 if (authMethod
[counter
]->type
!= NS_LDAP_AUTH_NONE
&&
2344 !(authMethod
[counter
]->type
== NS_LDAP_AUTH_TLS
&&
2345 authMethod
[counter
]->tlstype
== NS_LDAP_TLS_NONE
)) {
2351 /* First, if we don't need proxyDN/Password then just return ok */
2352 if (!(credProxy
&& authNotNone
)) {
2355 gettext("No proxyDN/proxyPassword required\n"),
2357 return (CLIENT_SUCCESS
);
2360 /* Now let's check if we have the cred stuff we need */
2361 if (!proxyDN
|| !proxyDN
[0]) {
2363 gettext("credentialLevel is proxy and no proxyDN "
2366 return (CLIENT_ERR_CREDENTIAL
);
2369 /* If we need proxyPassword (prompt) */
2370 if (!proxyPassword
|| !proxyPassword
[0]) {
2372 gettext("credentialLevel requires proxyPassword\n"),
2374 arglist
->proxyPassword
= getpassphrase("Proxy Bind Password:");
2375 if (arglist
->proxyPassword
== NULL
) {
2376 CLIENT_FPUTS(gettext("Get password failed\n"), stderr
);
2377 return (CLIENT_ERR_CREDENTIAL
);
2379 LDAP_SET_PARAM(arglist
->proxyPassword
, NS_LDAP_BINDPASSWD_P
);
2382 gettext("setParam proxyPassword failed.\n"),
2384 return (CLIENT_ERR_CREDENTIAL
);
2388 return (CLIENT_SUCCESS
);
2392 * adminCredCheck is called to check if the admin credential is required
2393 * for this configuration. This means that if enableShadowUpdate is set
2394 * to TRUE then credential info is required (adminDN and adminPassword).
2395 * One exception is that if there is a 'self' credentialLevel and
2396 * 'sasl/GSSAPI' authenticationMethod (i.e., possibly using Kerberos
2397 * host credential) then adminDN and adminPassword are not required.
2400 adminCredCheck(clientopts_t
*arglist
)
2403 int **enabled
= NULL
;
2404 int **credLevel
= NULL
;
2405 char **adminDN
= NULL
;
2406 char **adminPassword
= NULL
;
2407 ns_auth_t
**authMethod
= NULL
;
2408 ns_ldap_error_t
*errorp
= NULL
;
2409 int credSelf
, authSASLgss
;
2412 /* If shadow update not enabled, then no need to check */
2413 retcode
= __ns_ldap_getParam(NS_LDAP_ENABLE_SHADOW_UPDATE_P
,
2414 (void ***)&enabled
, &errorp
);
2416 CLIENT_FPRINTF(stderr
,
2417 gettext("Error %d while trying to retrieve "
2418 "enableShadowUpdate\n"), retcode
);
2419 rc
= CLIENT_ERR_FAIL
;
2422 if (enabled
== NULL
||
2423 *enabled
[0] != NS_LDAP_ENABLE_SHADOW_UPDATE_TRUE
) {
2426 gettext("Shadow Update is not enabled, "
2427 "no adminDN/adminPassword is required.\n"), stderr
);
2428 rc
= CLIENT_SUCCESS
;
2432 /* get credentialLevel */
2433 retcode
= __ns_ldap_getParam(NS_LDAP_CREDENTIAL_LEVEL_P
,
2434 (void ***)&credLevel
, &errorp
);
2436 CLIENT_FPRINTF(stderr
,
2437 gettext("Error %d while trying to retrieve credLevel\n"),
2439 rc
= CLIENT_ERR_FAIL
;
2443 /* get AuthenticationMethod */
2444 retcode
= __ns_ldap_getParam(NS_LDAP_AUTH_P
,
2445 (void ***)&authMethod
, &errorp
);
2447 CLIENT_FPRINTF(stderr
,
2448 gettext("Error %d while trying to retrieve authMethod\n"),
2450 rc
= CLIENT_ERR_FAIL
;
2455 retcode
= __ns_ldap_getParam(NS_LDAP_ADMIN_BINDDN_P
,
2456 (void ***)&adminDN
, &errorp
);
2458 CLIENT_FPRINTF(stderr
,
2459 gettext("Error %d while trying to retrieve adminDN\n"),
2461 rc
= CLIENT_ERR_FAIL
;
2465 /* get adminPassword */
2466 retcode
= __ns_ldap_getParam(NS_LDAP_ADMIN_BINDPASSWD_P
,
2467 (void ***)&adminPassword
, &errorp
);
2469 CLIENT_FPRINTF(stderr
,
2470 gettext("Error %d while trying to retrieve "
2471 "adminPassword\n"), retcode
);
2472 rc
= CLIENT_ERR_FAIL
;
2477 CLIENT_FPRINTF(stderr
,
2478 gettext("admin DN: %s\n"),
2479 (adminDN
&& adminDN
[0]) ? adminDN
[0] : "NULL");
2480 CLIENT_FPRINTF(stderr
,
2481 gettext("admin password: %s\n"),
2482 (adminPassword
&& adminPassword
[0]) ?
2483 adminPassword
[0] : "NULL");
2486 credSelf
= 0; /* flag to indicate if we have a credLevel of self */
2487 for (counter
= 0; credLevel
&& credLevel
[counter
] != NULL
; counter
++) {
2489 CLIENT_FPRINTF(stderr
,
2490 gettext("Credential level: %d\n"),
2491 *credLevel
[counter
]);
2492 if (*credLevel
[counter
] == NS_LDAP_CRED_SELF
) {
2498 authSASLgss
= 0; /* flag for authMethod of SASL/gssapi */
2500 authMethod
&& authMethod
[counter
] != NULL
;
2504 CLIENT_FPRINTF(stderr
,
2505 gettext("Authentication sasl mechanism: %d\n"),
2506 authMethod
[counter
]->saslmech
);
2507 if (authMethod
[counter
]->saslmech
== NS_LDAP_SASL_GSSAPI
) {
2513 /* First, if we don't need adminDN/adminPassword then just return ok */
2514 if (credSelf
&& authSASLgss
) {
2517 gettext("A credential Level of self and an "
2518 "authentication method of sasl/GSSAPI is "
2519 "configured, no adminDN/adminPassword "
2520 "is required.\n"), stderr
);
2521 rc
= CLIENT_SUCCESS
;
2525 /* Now let's check if we have the cred stuff we need */
2526 if (adminDN
== NULL
|| adminDN
[0] == '\0') {
2528 gettext("Shadow Update is enabled, but "
2529 "no adminDN is configured.\n"), stderr
);
2530 rc
= CLIENT_ERR_CREDENTIAL
;
2534 /* If we need adminPassword (prompt) */
2535 if (adminPassword
== NULL
|| adminPassword
[0] == '\0') {
2537 gettext("Shadow Update requires adminPassword\n"),
2539 arglist
->adminPassword
= getpassphrase("admin Password:");
2540 if (arglist
->adminPassword
== NULL
) {
2541 CLIENT_FPUTS(gettext("Unable to get admin password\n"),
2543 rc
= CLIENT_ERR_CREDENTIAL
;
2546 LDAP_SET_PARAM(arglist
->adminPassword
,
2547 NS_LDAP_ADMIN_BINDPASSWD_P
);
2550 gettext("setParam adminPassword failed.\n"),
2552 rc
= CLIENT_ERR_CREDENTIAL
;
2557 rc
= CLIENT_SUCCESS
;
2560 if (enabled
!= NULL
)
2561 (void) __ns_ldap_freeParam((void ***)&enabled
);
2562 if (credLevel
!= NULL
)
2563 (void) __ns_ldap_freeParam((void ***)&credLevel
);
2564 if (authMethod
!= NULL
)
2565 (void) __ns_ldap_freeParam((void ***)&authMethod
);
2566 if (adminDN
!= NULL
)
2567 (void) __ns_ldap_freeParam((void ***)&adminDN
);
2568 if (adminPassword
!= NULL
)
2569 (void) __ns_ldap_freeParam((void ***)&adminPassword
);
2575 * try to restore the previous name space on this machine
2578 recover(int saveState
)
2581 int stat_ret
, retcode
, fd
;
2582 int domain
= 0, domainlen
;
2583 char yp_dir
[BUFSIZE
], yp_dir_back
[BUFSIZE
];
2585 char *ldap_conf_file
, *ldap_cred_file
;
2586 char ldap_file_back
[BUFSIZE
], ldap_cred_back
[BUFSIZE
];
2588 /* If running as Sysid Install become a no-op */
2589 if (sysid_install
== B_TRUE
)
2590 return (CLIENT_SUCCESS
);
2592 stat_ret
= stat(LDAP_RESTORE_DIR
, &buf
);
2593 if (stat_ret
!= 0) {
2595 gettext("Cannot recover. No backup files "
2599 gettext("\t Either this machine was not initialized\n"),
2602 gettext("\t by ldapclient or the backup files "
2606 gettext("\t removed manually or with an \"uninit\"\n"),
2608 return (CLIENT_ERR_RESTORE
); /* invalid backup */
2612 * Get domainname. Allow no domainname for the case where "files"
2613 * config was backed up.
2615 stat_ret
= stat(DOMAINNAME_BACK
, &buf
);
2617 CLIENT_FPRINTF(stderr
,
2618 gettext("recover: stat(%s)=%d\n"),
2619 DOMAINNAME_BACK
, stat_ret
);
2620 if (stat_ret
== 0) {
2622 CLIENT_FPRINTF(stderr
,
2623 gettext("recover: open(%s)\n"),
2625 fd
= open(DOMAINNAME_BACK
, O_RDONLY
);
2627 CLIENT_FPRINTF(stderr
,
2628 gettext("recover: read(%s)\n"),
2630 domainlen
= read(fd
, &(name
[0]), BUFSIZ
-1);
2632 if (domainlen
< 0) {
2634 gettext("Cannot recover. Cannot determine "
2635 "previous domain name.\n"),
2637 return (CLIENT_ERR_RESTORE
); /* invalid backup */
2641 ptr
= strchr(&(name
[0]), '\n');
2645 name
[domainlen
] = '\0';
2648 CLIENT_FPRINTF(stderr
,
2649 gettext("recover: old domainname "
2652 if (strlen(name
) == 0)
2655 domain
= 1; /* flag that we have domain */
2662 * we can recover at this point
2663 * remove LDAP config files before restore
2665 (void) unlink(NSCONFIGFILE
);
2666 (void) unlink(NSCREDFILE
);
2668 ldap_conf_file
= strrchr(NSCONFIGFILE
, '/') + 1;
2669 ldap_cred_file
= strrchr(NSCREDFILE
, '/') + 1;
2671 (void) strlcpy(ldap_file_back
, LDAP_RESTORE_DIR
"/", BUFSIZE
);
2672 (void) strlcat(ldap_file_back
, ldap_conf_file
, BUFSIZE
);
2674 stat_ret
= stat(ldap_file_back
, &buf
);
2676 CLIENT_FPRINTF(stderr
,
2677 gettext("recover: stat(%s)=%d\n"),
2678 ldap_file_back
, stat_ret
);
2679 if (stat_ret
== 0) {
2681 gStartLdap
= START_UNINIT
;
2682 retcode
= file_move(ldap_file_back
, NSCONFIGFILE
);
2684 CLIENT_FPRINTF(stderr
,
2685 gettext("recover: file_move(%s, %s)=%d\n"),
2686 ldap_file_back
, NSCONFIGFILE
, retcode
);
2688 CLIENT_FPRINTF(stderr
,
2689 gettext("recover: file_move(%s, %s) failed\n"),
2690 ldap_file_back
, NSCONFIGFILE
);
2693 (void) strlcpy(ldap_cred_back
, LDAP_RESTORE_DIR
"/", BUFSIZE
);
2694 (void) strlcat(ldap_cred_back
, ldap_cred_file
, BUFSIZE
);
2696 stat_ret
= stat(ldap_cred_back
, &buf
);
2698 CLIENT_FPRINTF(stderr
,
2699 gettext("recover: stat(%s)=%d\n"),
2700 ldap_cred_back
, stat_ret
);
2701 if (stat_ret
== 0) {
2702 retcode
= file_move(ldap_cred_back
, NSCREDFILE
);
2704 CLIENT_FPRINTF(stderr
,
2705 gettext("recover: file_move(%s, %s)=%d\n"),
2706 ldap_cred_back
, NSCREDFILE
, retcode
);
2708 CLIENT_FPRINTF(stderr
,
2709 gettext("recover: file_move(%s, %s) failed\n"),
2710 ldap_cred_back
, NSCREDFILE
);
2713 /* Check for recovery of NIS(YP) if we have a domainname */
2715 /* "name" would have to be huge for this, but just in case */
2716 if (strlen(name
) >= (BUFSIZE
- strlen(LDAP_RESTORE_DIR
)))
2717 return (CLIENT_ERR_FAIL
);
2718 if (strlen(name
) >= (BUFSIZE
- strlen(YP_BIND_DIR
)))
2719 return (CLIENT_ERR_FAIL
);
2721 (void) strlcpy(yp_dir_back
, LDAP_RESTORE_DIR
"/", BUFSIZE
);
2722 (void) strlcat(yp_dir_back
, name
, BUFSIZE
);
2723 stat_ret
= stat(yp_dir_back
, &buf
);
2725 CLIENT_FPRINTF(stderr
,
2726 gettext("recover: stat(%s)=%d\n"),
2727 yp_dir_back
, stat_ret
);
2728 if (stat_ret
== 0) {
2729 (void) strlcpy(yp_dir
, YP_BIND_DIR
"/", BUFSIZE
);
2730 (void) strlcat(yp_dir
, name
, BUFSIZE
);
2731 retcode
= file_move(yp_dir_back
, yp_dir
);
2733 CLIENT_FPRINTF(stderr
,
2734 gettext("recover: file_move(%s, "
2736 yp_dir_back
, yp_dir
, retcode
);
2738 CLIENT_FPRINTF(stderr
,
2739 gettext("recover: file_move(%s, "
2741 yp_dir_back
, yp_dir
);
2744 gStartYp
= START_UNINIT
;
2749 /* restore machine configuration */
2750 stat_ret
= stat(NSSWITCH_BACK
, &buf
);
2752 CLIENT_FPRINTF(stderr
,
2753 gettext("recover: stat(%s)=%d\n"),
2754 NSSWITCH_BACK
, stat_ret
);
2755 if (stat_ret
== 0) {
2756 retcode
= file_move(NSSWITCH_BACK
, NSSWITCH_CONF
);
2758 CLIENT_FPRINTF(stderr
,
2759 gettext("recover: file_move(%s, %s)=%d\n"),
2760 NSSWITCH_BACK
, NSSWITCH_CONF
, retcode
);
2762 CLIENT_FPRINTF(stderr
,
2763 gettext("recover: file_move(%s, %s) failed\n"),
2764 NSSWITCH_BACK
, NSSWITCH_CONF
);
2767 stat_ret
= stat(DOMAINNAME_BACK
, &buf
);
2769 CLIENT_FPRINTF(stderr
,
2770 gettext("recover: stat(%s)=%d\n"),
2771 DOMAINNAME_BACK
, stat_ret
);
2772 if (stat_ret
== 0) {
2773 retcode
= file_move(DOMAINNAME_BACK
, DOMAINNAME
);
2775 CLIENT_FPRINTF(stderr
,
2776 gettext("recover: file_move(%s, %s)=%d\n"),
2777 DOMAINNAME_BACK
, DOMAINNAME
, retcode
);
2779 CLIENT_FPRINTF(stderr
,
2780 gettext("recover: file_move(%s, %s) failed\n"),
2781 DOMAINNAME_BACK
, DOMAINNAME
);
2784 retcode
= rmdir(LDAP_RESTORE_DIR
);
2786 CLIENT_FPRINTF(stderr
,
2787 gettext("Error removing \"%s\" directory.\n"),
2791 return (CLIENT_SUCCESS
);
2795 * try to save the current state of this machine.
2796 * this just overwrites any old saved configration files.
2798 * This function should only be called after network services have been stopped.
2800 * Returns 0 on successful save
2801 * Otherwise returns -1
2807 int domain_stat
, conf_stat
, ldap_stat
;
2808 int yp_stat
, restore_stat
;
2809 int retcode
, namelen
, ret
;
2810 char yp_dir
[BUFSIZ
], yp_dir_back
[BUFSIZ
];
2812 char *ldap_conf_file
, *ldap_cred_file
;
2813 char ldap_file_back
[BUFSIZE
], ldap_cred_back
[BUFSIZE
];
2815 ret
= CLIENT_SUCCESS
;
2816 /* If running as Sysid Install become a no-op */
2817 if (sysid_install
== B_TRUE
)
2818 return (CLIENT_SUCCESS
);
2820 /* If existing backup files, clear for this run */
2821 restore_stat
= stat(LDAP_RESTORE_DIR
, &buf
);
2822 if (restore_stat
== 0) {
2825 gettext("Removing existing restore "
2829 (void) system("/bin/rm -fr " LDAP_RESTORE_DIR
);
2830 restore_stat
= stat(LDAP_RESTORE_DIR
, &buf
);
2831 if (restore_stat
== 0) {
2832 CLIENT_FPRINTF(stderr
,
2833 gettext("Unable to remove backup "
2834 "directory (%s)\n"),
2836 return (CLIENT_ERR_RESTORE
);
2840 retcode
= mkdir(LDAP_RESTORE_DIR
, 0755);
2842 CLIENT_FPRINTF(stderr
,
2843 gettext("file_backup: Failed to make %s backup "
2844 "directory. mkdir=%d\n"),
2845 LDAP_RESTORE_DIR
, retcode
);
2846 return (CLIENT_ERR_FAIL
);
2849 conf_stat
= stat(NSSWITCH_CONF
, &buf
);
2851 CLIENT_FPRINTF(stderr
,
2852 gettext("file_backup: stat(%s)=%d\n"),
2853 NSSWITCH_CONF
, conf_stat
);
2854 if (conf_stat
== 0) {
2856 CLIENT_FPRINTF(stderr
,
2857 gettext("file_backup: (%s -> %s)\n"),
2858 NSSWITCH_CONF
, NSSWITCH_BACK
);
2859 retcode
= file_move(NSSWITCH_CONF
, NSSWITCH_BACK
);
2861 CLIENT_FPRINTF(stderr
,
2862 gettext("file_backup: file_move(%s, %s) failed "
2864 NSSWITCH_CONF
, NSSWITCH_BACK
, retcode
);
2865 ret
= CLIENT_ERR_RENAME
;
2869 CLIENT_FPRINTF(stderr
,
2870 gettext("file_backup: No %s file.\n"),
2874 domain_stat
= stat(DOMAINNAME
, &buf
);
2876 CLIENT_FPRINTF(stderr
,
2877 gettext("file_backup: stat(%s)=%d\n"),
2878 DOMAINNAME
, domain_stat
);
2879 if ((domain_stat
== 0) && (buf
.st_size
> 0)) {
2881 CLIENT_FPRINTF(stderr
,
2882 gettext("file_backup: (%s -> %s)\n"),
2883 DOMAINNAME
, DOMAINNAME_BACK
);
2884 retcode
= file_move(DOMAINNAME
, DOMAINNAME_BACK
);
2886 CLIENT_FPRINTF(stderr
,
2887 gettext("file_backup: file_move(%s, %s) failed "
2889 DOMAINNAME
, DOMAINNAME_BACK
, retcode
);
2890 ret
= CLIENT_ERR_RENAME
;
2894 if (domain_stat
!= 0) {
2895 CLIENT_FPRINTF(stderr
,
2896 gettext("file_backup: No %s file.\n"),
2899 CLIENT_FPRINTF(stderr
,
2900 gettext("file_backup: Empty %s "
2907 (void) sysinfo(SI_SRPC_DOMAIN
, &(name
[0]), namelen
);
2908 namelen
= strlen(name
);
2911 CLIENT_FPRINTF(stderr
,
2912 gettext("file_backup: nis domain is \"%s\"\n"),
2913 (namelen
> 0) ? name
: "EMPTY");
2914 /* check for domain name if not set cannot save NIS(YP) state */
2916 /* moving /var/yp/binding will cause ypbind to core dump */
2917 (void) strlcpy(yp_dir
, YP_BIND_DIR
"/", BUFSIZE
);
2918 (void) strlcat(yp_dir
, name
, BUFSIZE
);
2919 yp_stat
= stat(yp_dir
, &buf
);
2921 CLIENT_FPRINTF(stderr
,
2922 gettext("file_backup: stat(%s)=%d\n"),
2925 (void) strlcpy(yp_dir_back
, LDAP_RESTORE_DIR
"/",
2927 (void) strlcat(yp_dir_back
, name
, BUFSIZE
);
2929 CLIENT_FPRINTF(stderr
,
2930 gettext("file_backup: (%s -> %s)\n"),
2931 yp_dir
, yp_dir_back
);
2932 retcode
= file_move(yp_dir
, yp_dir_back
);
2934 CLIENT_FPRINTF(stderr
,
2935 gettext("file_backup: file_move(%s, %s)"
2936 " failed with %d\n"),
2937 yp_dir
, yp_dir_back
, retcode
);
2938 ret
= CLIENT_ERR_RENAME
;
2942 CLIENT_FPRINTF(stderr
,
2943 gettext("file_backup: No %s "
2944 "directory.\n"), yp_dir
);
2949 /* point to file name, not path delim (/) */
2950 ldap_conf_file
= strrchr(NSCONFIGFILE
, '/') + 1;
2951 ldap_cred_file
= strrchr(NSCREDFILE
, '/') + 1;
2953 ldap_stat
= stat(NSCONFIGFILE
, &buf
);
2955 CLIENT_FPRINTF(stderr
,
2956 gettext("file_backup: stat(%s)=%d\n"),
2957 NSCONFIGFILE
, ldap_stat
);
2958 if (ldap_stat
== 0) {
2959 (void) strlcpy(ldap_file_back
, LDAP_RESTORE_DIR
"/", BUFSIZE
);
2960 (void) strlcat(ldap_file_back
, ldap_conf_file
, BUFSIZE
);
2962 CLIENT_FPRINTF(stderr
,
2963 gettext("file_backup: (%s -> %s)\n"),
2964 NSCONFIGFILE
, ldap_file_back
);
2965 retcode
= file_move(NSCONFIGFILE
, ldap_file_back
);
2967 CLIENT_FPRINTF(stderr
,
2968 gettext("file_backup: file_move(%s, %s) failed "
2970 NSCONFIGFILE
, ldap_file_back
, retcode
);
2971 ret
= CLIENT_ERR_RENAME
;
2974 (void) strlcpy(ldap_cred_back
, LDAP_RESTORE_DIR
"/", BUFSIZE
);
2975 (void) strlcat(ldap_cred_back
, ldap_cred_file
, BUFSIZE
);
2977 CLIENT_FPRINTF(stderr
,
2978 gettext("file_backup: (%s -> %s)\n"),
2979 NSCREDFILE
, ldap_cred_back
);
2980 retcode
= file_move(NSCREDFILE
, ldap_cred_back
);
2982 CLIENT_FPRINTF(stderr
,
2983 gettext("file_backup: file_move(%s, %s) failed "
2985 NSCREDFILE
, ldap_cred_back
, retcode
);
2986 ret
= CLIENT_ERR_RENAME
;
2990 CLIENT_FPRINTF(stderr
,
2991 gettext("file_backup: No %s file.\n"),
3001 * This function is used to temporily backup the LDAP client files in /var/ldap
3002 * that the "mod" operation needs to update. If an error occurs then the
3003 * function mod_recover() can be invoke to recover the unmodified files.
3009 int retcode
= CLIENT_SUCCESS
;
3011 rc
= system(CMD_CP
" " NSCONFIGFILE
" " NSCONFIGFILE
".mod");
3014 CLIENT_FPRINTF(stderr
,
3015 gettext("mod_backup: backup %s for %s\n"),
3016 rc
? "failed" : "successful", NSCONFIGFILE
);
3018 rc
= system(CMD_CP
" " NSCREDFILE
" " NSCREDFILE
".mod");
3021 CLIENT_FPRINTF(stderr
,
3022 gettext("mod_backup: backup %s for %s\n"),
3023 rc
? "failed" : "successful", NSCREDFILE
);
3025 rc
= system(CMD_CP
" " DOMAINNAME
" " DOMAINNAME
".mod");
3028 CLIENT_FPRINTF(stderr
,
3029 gettext("mod_backup: backup %s for %s\n"),
3030 rc
? "failed" : "successful", DOMAINNAME
);
3032 if (retcode
!= CLIENT_SUCCESS
)
3033 retcode
= CLIENT_ERR_RENAME
;
3040 * This function is used to recover the temporily backed up files by
3041 * the mod_backup() function if an error occurs during the "mod"
3048 int retcode
= CLIENT_SUCCESS
;
3050 rc
= system(CMD_MV
" " NSCONFIGFILE
".mod " NSCONFIGFILE
);
3053 CLIENT_FPRINTF(stderr
,
3054 gettext("mod_recover: recovery %s for %s\n"),
3055 rc
? "failed" : "successful", NSCONFIGFILE
);
3057 rc
= system(CMD_MV
" " NSCREDFILE
".mod " NSCREDFILE
);
3060 CLIENT_FPRINTF(stderr
,
3061 gettext("mod_recover: recovery %s for %s\n"),
3062 rc
? "failed" : "successful", NSCREDFILE
);
3064 rc
= system(CMD_MV
" " DOMAINNAME
".mod " DOMAINNAME
);
3067 CLIENT_FPRINTF(stderr
,
3068 gettext("mod_recover: recovery %s for %s\n"),
3069 rc
? "failed" : "successful", DOMAINNAME
);
3071 if (retcode
!= CLIENT_SUCCESS
)
3072 retcode
= CLIENT_ERR_RENAME
;
3079 * This function removes the .mod files in /var/ldap.
3084 (void) system(CMD_RM
" " NSCONFIGFILE
".mod " TO_DEV_NULL
);
3085 (void) system(CMD_RM
" " NSCREDFILE
".mod " TO_DEV_NULL
);
3086 (void) system(CMD_RM
" " DOMAINNAME
".mod " TO_DEV_NULL
);
3089 #define MAX_DN_ARRAY 100
3090 #define LDAP_NAMINGCONTEXTS "namingcontexts"
3097 hold
= calloc(1, sizeof (multival_t
));
3100 gettext("multival_new: Memory allocation error\n"),
3103 return (hold
); /* NULL -> error */
3107 multival_add(multival_t
*list
, char *opt
)
3111 gettext("Empty value passed to multival_add\n"),
3113 return (CLIENT_ERR_FAIL
);
3116 if (list
->count
== 0) {
3117 list
->optlist
= (char **)malloc(sizeof (char **));
3119 list
->optlist
= (char **)realloc(list
->optlist
,
3120 (list
->count
+ 1) * sizeof (char **));
3123 if (list
->optlist
== NULL
) {
3124 CLIENT_FPUTS(gettext("Error allocating memory\n"), stderr
);
3125 return (CLIENT_ERR_MEMORY
); /* 0 is success */
3128 list
->optlist
[list
->count
] = opt
;
3131 return (CLIENT_SUCCESS
);
3135 multival_free(multival_t
*list
)
3140 if (list
->optlist
!= NULL
)
3141 free(list
->optlist
);
3145 static clientopts_t
*
3150 hold
= calloc(1, sizeof (clientopts_t
));
3152 CLIENT_FPUTS(gettext("Error allocating memory for "
3153 "clientopts structure\n"), stderr
);
3154 return (hold
); /* NULL -> error */
3157 hold
->serviceAuthenticationMethod
= multival_new();
3158 if (NULL
== hold
->serviceAuthenticationMethod
) {
3159 CLIENT_FPUTS(gettext("Error allocating memory for "
3160 "serviceAuthenticationMethod\n"), stderr
);
3162 return (NULL
); /* NULL -> error */
3165 hold
->serviceCredentialLevel
= multival_new();
3166 if (NULL
== hold
->serviceCredentialLevel
) {
3167 CLIENT_FPUTS(gettext("Error allocating memory for "
3168 "serviceCredentialLevel\n"), stderr
);
3169 multival_free(hold
->serviceAuthenticationMethod
);
3171 return (NULL
); /* NULL -> error */
3174 hold
->objectclassMap
= multival_new();
3175 if (NULL
== hold
->objectclassMap
) {
3176 CLIENT_FPUTS(gettext("Error allocating memory for "
3177 "objectclassMap\n"), stderr
);
3178 multival_free(hold
->serviceAuthenticationMethod
);
3179 multival_free(hold
->serviceCredentialLevel
);
3181 return (NULL
); /* NULL -> error */
3184 hold
->attributeMap
= multival_new();
3185 if (NULL
== hold
->attributeMap
) {
3186 CLIENT_FPUTS(gettext("Error allocating memory for "
3187 "attributeMap\n"), stderr
);
3188 multival_free(hold
->serviceAuthenticationMethod
);
3189 multival_free(hold
->serviceCredentialLevel
);
3190 multival_free(hold
->objectclassMap
);
3192 return (NULL
); /* NULL -> error */
3195 hold
->serviceSearchDescriptor
= multival_new();
3196 if (NULL
== hold
->serviceSearchDescriptor
) {
3197 CLIENT_FPUTS(gettext("Error allocating memory for "
3198 "serviceSearchDescriptor\n"), stderr
);
3199 multival_free(hold
->serviceAuthenticationMethod
);
3200 multival_free(hold
->serviceCredentialLevel
);
3201 multival_free(hold
->objectclassMap
);
3202 multival_free(hold
->attributeMap
);
3204 return (NULL
); /* NULL -> error */
3211 clientopts_free(clientopts_t
*list
)
3216 multival_free(list
->serviceAuthenticationMethod
);
3217 multival_free(list
->serviceCredentialLevel
);
3218 multival_free(list
->objectclassMap
);
3219 multival_free(list
->attributeMap
);
3220 multival_free(list
->serviceSearchDescriptor
);
3227 multival_list(char *opt
, multival_t
*list
)
3231 if (list
->count
== 0)
3235 for (i
= 0; i
< list
->count
; i
++) {
3236 (void) printf("\t\targ[%d]: %s\n", i
, list
->optlist
[i
]);
3240 /* return the number of arguments specified in the command line */
3242 num_args(clientopts_t
*list
)
3246 arg_count
+= list
->authenticationMethod
? 1 : 0;
3247 arg_count
+= list
->serviceAuthenticationMethod
->count
;
3248 arg_count
+= list
->defaultSearchBase
? 1 : 0;
3249 arg_count
+= list
->credentialLevel
? 1 : 0;
3250 arg_count
+= list
->serviceCredentialLevel
->count
;
3251 arg_count
+= list
->domainName
? 1 : 0;
3252 arg_count
+= list
->proxyDN
? 1 : 0;
3253 arg_count
+= list
->enableShadowUpdate
? 1 : 0;
3254 arg_count
+= list
->adminDN
? 1 : 0;
3255 arg_count
+= list
->profileTTL
? 1 : 0;
3256 arg_count
+= list
->objectclassMap
->count
;
3257 arg_count
+= list
->searchTimeLimit
? 1 : 0;
3258 arg_count
+= list
->preferredServerList
? 1 : 0;
3259 arg_count
+= list
->profileName
? 1 : 0;
3260 arg_count
+= list
->followReferrals
? 1 : 0;
3261 arg_count
+= list
->attributeMap
->count
;
3262 arg_count
+= list
->defaultSearchScope
? 1 : 0;
3263 arg_count
+= list
->serviceSearchDescriptor
->count
;
3264 arg_count
+= list
->bindTimeLimit
? 1 : 0;
3265 arg_count
+= list
->proxyPassword
? 1 : 0;
3266 arg_count
+= list
->adminPassword
? 1 : 0;
3267 arg_count
+= list
->defaultServerList
? 1 : 0;
3268 arg_count
+= list
->certificatePath
? 1 : 0;
3273 #define CLIENT_PRINT(opt, str) if (str) \
3274 (void) printf("%s%s\n", (opt), (str))
3277 dumpargs(clientopts_t
*list
)
3279 CLIENT_PRINT("\tauthenticationMethod: ", list
->authenticationMethod
);
3280 multival_list("\tserviceAuthenticationMethod: ",
3281 list
->serviceAuthenticationMethod
);
3282 CLIENT_PRINT("\tdefaultSearchBase: ", list
->defaultSearchBase
);
3283 CLIENT_PRINT("\tcredentialLevel: ", list
->credentialLevel
);
3284 multival_list("\tserviceCredentialLevel: ",
3285 list
->serviceCredentialLevel
);
3286 CLIENT_PRINT("\tdomainName: ", list
->domainName
);
3287 CLIENT_PRINT("\tproxyDN: ", list
->proxyDN
);
3288 CLIENT_PRINT("\tadminDN: ", list
->adminDN
);
3289 CLIENT_PRINT("\tenableShadowUpdate: ", list
->enableShadowUpdate
);
3290 CLIENT_PRINT("\tprofileTTL: ", list
->profileTTL
);
3291 multival_list("\tobjectclassMap: ", list
->objectclassMap
);
3292 CLIENT_PRINT("\tsearchTimeLimit: ", list
->searchTimeLimit
);
3293 CLIENT_PRINT("\tpreferredServerList: ", list
->preferredServerList
);
3294 CLIENT_PRINT("\tprofileName: ", list
->profileName
);
3295 CLIENT_PRINT("\tfollowReferrals: ", list
->followReferrals
);
3296 multival_list("\tattributeMap: ", list
->attributeMap
);
3297 CLIENT_PRINT("\tdefaultSearchScope: ", list
->defaultSearchScope
);
3298 multival_list("\tserviceSearchDescriptor: ",
3299 list
->serviceSearchDescriptor
);
3300 CLIENT_PRINT("\tbindTimeLimit: ", list
->bindTimeLimit
);
3301 CLIENT_PRINT("\tproxyPassword: ", list
->proxyPassword
);
3302 CLIENT_PRINT("\tadminPassword: ", list
->adminPassword
);
3303 CLIENT_PRINT("\tdefaultServerList: ", list
->defaultServerList
);
3304 CLIENT_PRINT("\tcertificatePath: ", list
->certificatePath
);
3308 /* These definitions are only used in parseParam() below. */
3314 static struct param paramArray
[] = {
3315 {"proxyDN", NS_LDAP_BINDDN_P
},
3316 {"proxyPassword", NS_LDAP_BINDPASSWD_P
},
3317 {"defaultServerList", NS_LDAP_SERVERS_P
},
3318 {"defaultSearchBase", NS_LDAP_SEARCH_BASEDN_P
},
3319 {"authenticationMethod", NS_LDAP_AUTH_P
},
3320 {"followReferrals", NS_LDAP_SEARCH_REF_P
},
3321 {"profileTTL", NS_LDAP_CACHETTL_P
},
3322 {"certificatePath", NS_LDAP_HOST_CERTPATH_P
},
3323 {"defaultSearchScope", NS_LDAP_SEARCH_SCOPE_P
},
3324 {"bindTimeLimit", NS_LDAP_BIND_TIME_P
},
3325 {"searchTimeLimit", NS_LDAP_SEARCH_TIME_P
},
3326 {"preferredServerList", NS_LDAP_SERVER_PREF_P
},
3327 {"profileName", NS_LDAP_PROFILE_P
},
3328 {"credentialLevel", NS_LDAP_CREDENTIAL_LEVEL_P
},
3329 {"serviceSearchDescriptor", NS_LDAP_SERVICE_SEARCH_DESC_P
},
3330 {"attributeMap", NS_LDAP_ATTRIBUTEMAP_P
},
3331 {"objectclassMap", NS_LDAP_OBJECTCLASSMAP_P
},
3332 {"serviceAuthenticationMethod", NS_LDAP_SERVICE_AUTH_METHOD_P
},
3333 {"serviceCredentialLevel", NS_LDAP_SERVICE_CRED_LEVEL_P
},
3334 {"domainName", LOCAL_DOMAIN_P
},
3335 {"enableShadowUpdate", NS_LDAP_ENABLE_SHADOW_UPDATE_P
},
3336 {"adminDN", NS_LDAP_ADMIN_BINDDN_P
},
3337 {"adminPassword", NS_LDAP_ADMIN_BINDPASSWD_P
},
3342 parseParam(char *param
, char **paramVal
)
3348 CLIENT_FPRINTF(stderr
, gettext("Parsing %s\n"), param
);
3351 val
= strchr(param
, '=');
3354 gettext("Didn\'t find \'=\' character in string\n"),
3357 return (CLIENT_ERR_PARSE
);
3362 for (counter
= 0; paramArray
[counter
].name
!= NULL
; counter
++) {
3363 if (strcasecmp(paramArray
[counter
].name
, param
) == 0) {
3365 *val
= '='; /* restore original param */
3366 return (paramArray
[counter
].index
);
3371 *val
= '='; /* restore original param */
3373 return (CLIENT_ERR_PARSE
);
3377 * The following macro checks if an option has already been specified
3378 * and errs out with usage if so
3380 #define CLIENT_OPT_CHECK(opt, optarg) \
3382 CLIENT_FPUTS(gettext("Invalid use of option\n"), stderr); \
3384 clientopts_free(optlist); \
3385 return (CLIENT_ERR_FAIL); \
3389 clientSetParam(clientopts_t
*optlist
, int paramFlag
, char *attrVal
)
3395 switch (paramFlag
) {
3396 case NS_LDAP_AUTH_P
:
3397 CLIENT_OPT_CHECK(paramFlag
, optlist
->authenticationMethod
);
3398 optlist
->authenticationMethod
= attrVal
;
3401 case NS_LDAP_SERVICE_AUTH_METHOD_P
: /* multiple allowed */
3402 retcode
= multival_add(optlist
->serviceAuthenticationMethod
,
3404 if (retcode
!= CLIENT_SUCCESS
) {
3405 CLIENT_FPRINTF(stderr
,
3406 gettext("Error processing attrVal %s\n"),
3407 attrVal
?attrVal
:"NULL");
3409 clientopts_free(optlist
);
3410 return (CLIENT_ERR_FAIL
);
3414 case NS_LDAP_SEARCH_BASEDN_P
:
3415 CLIENT_OPT_CHECK(paramFlag
, optlist
->defaultSearchBase
);
3416 optlist
->defaultSearchBase
= attrVal
;
3419 case NS_LDAP_CREDENTIAL_LEVEL_P
:
3420 CLIENT_OPT_CHECK(paramFlag
, optlist
->credentialLevel
);
3421 optlist
->credentialLevel
= attrVal
;
3424 case NS_LDAP_SERVICE_CRED_LEVEL_P
: /* multiple allowed */
3425 retcode
= multival_add(optlist
->serviceCredentialLevel
,
3427 if (retcode
!= CLIENT_SUCCESS
) {
3428 CLIENT_FPRINTF(stderr
,
3429 gettext("Error processing attrVal %s\n"),
3430 attrVal
?attrVal
:"NULL");
3432 clientopts_free(optlist
);
3433 return (CLIENT_ERR_FAIL
);
3437 case LOCAL_DOMAIN_P
:
3438 CLIENT_OPT_CHECK(paramFlag
, optlist
->domainName
);
3439 optlist
->domainName
= attrVal
;
3440 dname
= optlist
->domainName
;
3443 case NS_LDAP_BINDDN_P
:
3444 CLIENT_OPT_CHECK(paramFlag
, optlist
->proxyDN
);
3445 optlist
->proxyDN
= attrVal
;
3448 case NS_LDAP_ENABLE_SHADOW_UPDATE_P
:
3449 CLIENT_OPT_CHECK(paramFlag
, optlist
->enableShadowUpdate
);
3450 optlist
->enableShadowUpdate
= attrVal
;
3453 case NS_LDAP_ADMIN_BINDDN_P
:
3454 CLIENT_OPT_CHECK(paramFlag
, optlist
->adminDN
);
3455 optlist
->adminDN
= attrVal
;
3458 case NS_LDAP_CACHETTL_P
:
3459 CLIENT_OPT_CHECK(paramFlag
, optlist
->profileTTL
);
3460 optlist
->profileTTL
= attrVal
;
3463 case NS_LDAP_OBJECTCLASSMAP_P
: /* multiple allowed */
3464 retcode
= multival_add(optlist
->objectclassMap
, attrVal
);
3465 if (retcode
!= CLIENT_SUCCESS
) {
3466 CLIENT_FPRINTF(stderr
,
3467 gettext("Error processing attrVal %s\n"),
3468 attrVal
?attrVal
:"NULL");
3470 clientopts_free(optlist
);
3471 return (CLIENT_ERR_FAIL
);
3475 case NS_LDAP_SEARCH_TIME_P
:
3476 CLIENT_OPT_CHECK(paramFlag
, optlist
->searchTimeLimit
);
3477 optlist
->searchTimeLimit
= attrVal
;
3480 case NS_LDAP_SERVER_PREF_P
:
3481 CLIENT_OPT_CHECK(paramFlag
, optlist
->preferredServerList
);
3482 optlist
->preferredServerList
= attrVal
;
3483 /* replace ',' chars with ' ' for proper syntax */
3485 counter
< strlen(optlist
->preferredServerList
);
3488 if (optlist
->preferredServerList
[counter
] == ',')
3489 optlist
->preferredServerList
[counter
] = ' ';
3493 case NS_LDAP_PROFILE_P
:
3494 CLIENT_OPT_CHECK(paramFlag
, optlist
->profileName
);
3495 optlist
->profileName
= attrVal
;
3498 case NS_LDAP_SEARCH_REF_P
:
3499 CLIENT_OPT_CHECK(paramFlag
, optlist
->followReferrals
);
3500 if (0 == strcasecmp(attrVal
, "followref"))
3501 optlist
->followReferrals
= "TRUE";
3502 else if (0 == strcasecmp(attrVal
, "noref"))
3503 optlist
->followReferrals
= "FALSE";
3505 optlist
->followReferrals
= attrVal
;
3508 case NS_LDAP_ATTRIBUTEMAP_P
: /* multiple allowed */
3509 retcode
= multival_add(optlist
->attributeMap
, attrVal
);
3510 if (retcode
!= CLIENT_SUCCESS
) {
3511 CLIENT_FPRINTF(stderr
,
3512 gettext("Error processing attrVal %s\n"),
3513 attrVal
?attrVal
:"NULL");
3515 clientopts_free(optlist
);
3516 return (CLIENT_ERR_FAIL
);
3520 case NS_LDAP_SEARCH_SCOPE_P
:
3521 CLIENT_OPT_CHECK(paramFlag
, optlist
->defaultSearchScope
);
3522 optlist
->defaultSearchScope
= attrVal
;
3525 case NS_LDAP_SERVICE_SEARCH_DESC_P
: /* multiple allowed */
3526 retcode
= multival_add(optlist
->serviceSearchDescriptor
,
3528 if (retcode
!= CLIENT_SUCCESS
) {
3529 CLIENT_FPRINTF(stderr
,
3530 gettext("Error processing attrVal %s\n"),
3531 attrVal
?attrVal
:"NULL");
3533 clientopts_free(optlist
);
3534 return (CLIENT_ERR_FAIL
);
3538 case NS_LDAP_BIND_TIME_P
:
3539 CLIENT_OPT_CHECK(paramFlag
, optlist
->bindTimeLimit
);
3540 optlist
->bindTimeLimit
= attrVal
;
3543 case NS_LDAP_BINDPASSWD_P
:
3544 CLIENT_OPT_CHECK(paramFlag
, optlist
->proxyPassword
);
3545 optlist
->proxyPassword
= attrVal
;
3548 case NS_LDAP_ADMIN_BINDPASSWD_P
:
3549 CLIENT_OPT_CHECK(paramFlag
, optlist
->adminPassword
);
3550 optlist
->adminPassword
= attrVal
;
3553 case NS_LDAP_HOST_CERTPATH_P
:
3554 CLIENT_OPT_CHECK(paramFlag
, optlist
->certificatePath
);
3555 optlist
->certificatePath
= attrVal
;
3558 case NS_LDAP_SERVERS_P
:
3559 CLIENT_OPT_CHECK(paramFlag
, optlist
->defaultServerList
);
3560 optlist
->defaultServerList
= attrVal
;
3565 return (CLIENT_ERR_FAIL
);
3566 /* break; lint doesn't like break before end of switch */
3573 * file_move() - Used to move a config file (backup/restore).
3575 * This function uses a system() call with /bin/mv to handle the
3576 * case where the backup directory (/var) is on a different file
3577 * system than the config file (typically /etc).
3580 file_move(const char *from
, const char *to
)
3583 char mvCommand
[] = CMD_MV
;
3584 char cmd_buffer
[(2 * MAXPATHLEN
) + sizeof (mvCommand
) + 3];
3586 (void) snprintf(cmd_buffer
, sizeof (cmd_buffer
), "%s %s %s",
3587 mvCommand
, from
, to
);
3590 * This function should only be used internally to move
3591 * system files to/from the backup directory. For security
3592 * reasons (this is run as root), don't use this function
3593 * with arguments passed into the program.
3595 retcode
= system(cmd_buffer
);
3602 * Manipulate the service as instructed by "dowhat"
3605 do_service(const char *fmri
, boolean_t waitflag
, int dowhat
,
3606 const char *state
) {
3610 const char *what
= gettext("not set");
3613 /* Check if we are in maintenance */
3614 is_maint
= is_service(fmri
, SCF_STATE_STRING_MAINT
);
3618 what
= gettext("start");
3619 status
= smf_enable_instance(fmri
,
3620 (sysid_install
== B_TRUE
)?SMF_TEMPORARY
:0);
3623 what
= gettext("stop");
3624 status
= smf_disable_instance(fmri
,
3625 (sysid_install
== B_TRUE
)?SMF_TEMPORARY
:0);
3627 case RESTART_SERVICE
:
3628 what
= gettext("restart");
3629 status
= smf_restart_instance(fmri
);
3632 /* coding error; will not happen */
3637 * If the service was previously in maintenance then we need to
3638 * clear it immediately. The "dowhat" action will set the
3639 * enabled property of the service as intended by the caller while
3640 * clear will actually cause it to be enabled/disabled.
3641 * We assume that the caller has called us after taking some
3642 * recovery action. Even if it's not the case, we don't lose
3645 if (status
== 0 && is_maint
== B_TRUE
) {
3647 CLIENT_FPRINTF(stderr
,
3651 gettext("restoring from maintenance state"));
3652 status
= smf_restore_instance(fmri
);
3656 /* Check if we need to wait ? */
3657 if (waitflag
== B_FALSE
) {
3659 CLIENT_FPRINTF(stderr
,
3663 gettext("success"));
3664 return (CLIENT_SUCCESS
);
3667 /* Otherwise wait for max seconds (from the manifest) */
3668 max
= get_timeout_value(dowhat
, fmri
, DEFAULT_TIMEOUT
);
3669 status
= wait_till(fmri
, state
, max
, what
, !is_maint
);
3670 if (status
== CLIENT_SUCCESS
)
3671 return (CLIENT_SUCCESS
);
3672 /* For error fall through for corrective action */
3674 /* Well, service failed ... */
3676 CLIENT_FPRINTF(stderr
, "%s: %s... %s: %s\n",
3680 scf_strerror(scf_error()));
3681 status
= CLIENT_ERR_FAIL
;
3682 /* For error fall through for corrective action */
3686 * If service is still offline after start/restart, then transitioning
3687 * failed and guess is restarter failed to apply the timeout as well.
3688 * So instead of leaving it offline, let's just disable it until we have
3689 * some other mechanism available from smf to handle such situation.
3691 if (dowhat
!= STOP_SERVICE
)
3692 if (is_service(fmri
, SCF_STATE_STRING_OFFLINE
)) {
3694 CLIENT_FPRINTF(stderr
,
3698 gettext("offline to disable"));
3699 (void) disable_service(fmri
, waitflag
);
3707 * Wait for "max" usecs for the service described by "fmri" to change
3708 * to "state". If check_maint is true then return immediately if
3709 * service goes into maintenance
3712 wait_till(const char *fmri
, const char *state
, useconds_t max
,
3713 const char *what
, boolean_t check_maint
) {
3715 useconds_t usecs
= INIT_WAIT_USECS
;
3717 for (; max
> 0; max
-= usecs
) {
3718 /* incremental wait */
3720 usecs
= (usecs
> max
)?max
:usecs
;
3722 CLIENT_FPRINTF(stderr
,
3724 what
, gettext("sleep"), usecs
,
3725 gettext("microseconds"));
3726 (void) usleep(usecs
);
3728 /* Check state after the wait */
3729 if ((st
= smf_get_state(fmri
)) != NULL
) {
3730 if (strcmp(st
, state
) == 0) {
3732 CLIENT_FPRINTF(stderr
,
3736 gettext("success"));
3738 return (CLIENT_SUCCESS
);
3742 * If service has gone into maintenance then
3743 * we will time out anyway, so we are better
3747 strcmp(st
, SCF_STATE_STRING_MAINT
) == 0) {
3749 CLIENT_FPRINTF(stderr
,
3753 gettext("maintenance"));
3755 return (CLIENT_ERR_MAINTENANCE
);
3760 CLIENT_FPRINTF(stderr
,
3761 "%s: %s... %s: %s\n",
3765 scf_strerror(scf_error()));
3766 return (CLIENT_ERR_FAIL
);
3770 /* Timed out waiting */
3772 CLIENT_FPRINTF(stderr
,
3776 gettext("timed out"));
3777 return (CLIENT_ERR_TIMEDOUT
);
3782 is_service(const char *fmri
, const char *state
) {
3784 boolean_t result
= B_FALSE
;
3786 if ((st
= smf_get_state(fmri
)) != NULL
) {
3787 if (strcmp(st
, state
) == 0)
3797 * get_timeout_val : returns the timeout value set in fmri manifest
3798 * inputs : action(start/stop)
3799 * fmri(defined fmri string)
3800 * Returns default if error, the timeout val otherwise
3805 get_timeout_value(int dowhat
, const char *fmri
, useconds_t default_val
)
3807 scf_simple_prop_t
*sp
= NULL
;
3808 uint64_t *cp
= NULL
;
3809 int timeout
= default_val
/1000000;
3810 char *action
= NULL
;
3811 const char *actionstr
= NULL
;
3815 case RESTART_SERVICE
:
3817 actionstr
= gettext("start");
3821 actionstr
= gettext("stop");
3828 sp
= scf_simple_prop_get(NULL
, fmri
, action
, SCF_PROPERTY_TIMEOUT
);
3831 CLIENT_FPRINTF(stderr
, "%s: %s... %s: %s\n",
3834 gettext("failed to retrieve timeout property"),
3835 scf_strerror(scf_error()));
3836 return (default_val
);
3839 cp
= scf_simple_prop_next_count(sp
);
3842 CLIENT_FPRINTF(stderr
, "%s: %s... %s: %s\n",
3845 gettext("failed to retrieve timeout value"),
3846 scf_strerror(scf_error()));
3847 scf_simple_prop_free(sp
);
3848 return (default_val
);
3853 scf_simple_prop_free(sp
);
3854 return (timeout
* 1000000);