xref: /illumos-gate/usr/src/lib/smbsrv/libsmb/common/smb_cfg.c (revision 29bd28862cfb8abbd3a0f0a4b17e08bbc3652836)
1 /*
2  * CDDL HEADER START
3  *
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.
7  *
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.
12  *
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]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"@(#)smb_cfg.c	1.5	08/07/08 SMI"
27 
28 /*
29  * CIFS configuration management library
30  */
31 
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <unistd.h>
35 #include <synch.h>
36 #include <string.h>
37 #include <strings.h>
38 #include <syslog.h>
39 #include <netdb.h>
40 #include <ctype.h>
41 #include <sys/types.h>
42 #include <libscf.h>
43 #include <assert.h>
44 #include <uuid/uuid.h>
45 #include <smbsrv/libsmb.h>
46 
47 typedef struct smb_cfg_param {
48 	smb_cfg_id_t sc_id;
49 	char *sc_name;
50 	int sc_type;
51 	uint32_t sc_flags;
52 } smb_cfg_param_t;
53 
54 /*
55  * config parameter flags
56  */
57 #define	SMB_CF_PROTECTED	0x01
58 #define	SMB_CF_EXEC		0x02
59 
60 /* idmap SMF fmri and Property Group */
61 #define	IDMAP_FMRI_PREFIX		"system/idmap"
62 #define	MACHINE_SID			"machine_sid"
63 #define	IDMAP_DOMAIN			"domain_name"
64 #define	IDMAP_PG_NAME			"config"
65 
66 #define	SMB_SECMODE_WORKGRP_STR 	"workgroup"
67 #define	SMB_SECMODE_DOMAIN_STR  	"domain"
68 
69 #define	SMB_ENC_LEN	1024
70 #define	SMB_DEC_LEN	256
71 
72 static char *b64_data =
73 	"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
74 
75 static smb_cfg_param_t smb_cfg_table[] =
76 {
77 	/* Oplock configuration, Kernel Only */
78 	{SMB_CI_OPLOCK_ENABLE, "oplock_enable", SCF_TYPE_BOOLEAN, 0},
79 
80 	/* Autohome configuration */
81 	{SMB_CI_AUTOHOME_MAP, "autohome_map", SCF_TYPE_ASTRING, 0},
82 
83 	/* Domain/PDC configuration */
84 	{SMB_CI_DOMAIN_SID, "domain_sid", SCF_TYPE_ASTRING, 0},
85 	{SMB_CI_DOMAIN_MEMB, "domain_member", SCF_TYPE_BOOLEAN, 0},
86 	{SMB_CI_DOMAIN_NAME, "domain_name", SCF_TYPE_ASTRING, 0},
87 	{SMB_CI_DOMAIN_FQDN, "fqdn", SCF_TYPE_ASTRING, 0},
88 	{SMB_CI_DOMAIN_FOREST, "forest", SCF_TYPE_ASTRING, 0},
89 	{SMB_CI_DOMAIN_GUID, "domain_guid", SCF_TYPE_ASTRING, 0},
90 	{SMB_CI_DOMAIN_SRV, "pdc", SCF_TYPE_ASTRING, 0},
91 
92 	/* WINS configuration */
93 	{SMB_CI_WINS_SRV1, "wins_server_1", SCF_TYPE_ASTRING, 0},
94 	{SMB_CI_WINS_SRV2, "wins_server_2", SCF_TYPE_ASTRING, 0},
95 	{SMB_CI_WINS_EXCL, "wins_exclude", SCF_TYPE_ASTRING, 0},
96 
97 	/* Kmod specific configuration */
98 	{SMB_CI_MAX_WORKERS, "max_workers", SCF_TYPE_INTEGER, 0},
99 	{SMB_CI_MAX_CONNECTIONS, "max_connections", SCF_TYPE_INTEGER, 0},
100 	{SMB_CI_KEEPALIVE, "keep_alive", SCF_TYPE_INTEGER, 0},
101 	{SMB_CI_RESTRICT_ANON, "restrict_anonymous", SCF_TYPE_BOOLEAN, 0},
102 
103 	{SMB_CI_SIGNING_ENABLE, "signing_enabled", SCF_TYPE_BOOLEAN, 0},
104 	{SMB_CI_SIGNING_REQD, "signing_required", SCF_TYPE_BOOLEAN, 0},
105 
106 	/* Kmod tuning configuration */
107 	{SMB_CI_SYNC_ENABLE, "sync_enable", SCF_TYPE_BOOLEAN, 0},
108 
109 	/* SMBd configuration */
110 	{SMB_CI_SECURITY, "security", SCF_TYPE_ASTRING, 0},
111 	{SMB_CI_NBSCOPE, "netbios_scope", SCF_TYPE_ASTRING, 0},
112 	{SMB_CI_SYS_CMNT, "system_comment", SCF_TYPE_ASTRING, 0},
113 	{SMB_CI_LM_LEVEL, "lmauth_level", SCF_TYPE_INTEGER, 0},
114 
115 	/* ADS Configuration */
116 	{SMB_CI_ADS_SITE, "ads_site", SCF_TYPE_ASTRING, 0},
117 
118 	/* Dynamic DNS */
119 	{SMB_CI_DYNDNS_ENABLE, "ddns_enable", SCF_TYPE_BOOLEAN, 0},
120 
121 	{SMB_CI_MACHINE_PASSWD, "machine_passwd", SCF_TYPE_ASTRING,
122 	    SMB_CF_PROTECTED},
123 	{SMB_CI_KPASSWD_SRV, "kpasswd_server", SCF_TYPE_ASTRING,
124 	    0},
125 	{SMB_CI_KPASSWD_DOMAIN, "kpasswd_domain", SCF_TYPE_ASTRING,
126 	    0},
127 	{SMB_CI_KPASSWD_SEQNUM, "kpasswd_seqnum", SCF_TYPE_INTEGER,
128 	    0},
129 	{SMB_CI_NETLOGON_SEQNUM, "netlogon_seqnum", SCF_TYPE_INTEGER,
130 	    0},
131 	{SMB_CI_IPV6_ENABLE, "ipv6_enable", SCF_TYPE_BOOLEAN, 0},
132 	{SMB_CI_MAP, "map", SCF_TYPE_ASTRING, SMB_CF_EXEC},
133 	{SMB_CI_UNMAP, "unmap", SCF_TYPE_ASTRING, SMB_CF_EXEC},
134 	{SMB_CI_DISPOSITION, "disposition", SCF_TYPE_ASTRING, SMB_CF_EXEC}
135 
136 	/* SMB_CI_MAX */
137 };
138 
139 static smb_cfg_param_t *smb_config_getent(smb_cfg_id_t);
140 
141 static boolean_t smb_is_base64(unsigned char c);
142 static char *smb_base64_encode(char *str_to_encode);
143 static char *smb_base64_decode(char *encoded_str);
144 
145 char *
146 smb_config_getname(smb_cfg_id_t id)
147 {
148 	smb_cfg_param_t *cfg;
149 	cfg = smb_config_getent(id);
150 	return (cfg->sc_name);
151 }
152 
153 static boolean_t
154 smb_is_base64(unsigned char c)
155 {
156 	return (isalnum(c) || (c == '+') || (c == '/'));
157 }
158 
159 /*
160  * smb_base64_encode
161  *
162  * Encode a string using base64 algorithm.
163  * Caller should free the returned buffer when done.
164  */
165 static char *
166 smb_base64_encode(char *str_to_encode)
167 {
168 	int ret_cnt = 0;
169 	int i = 0, j = 0;
170 	char arr_3[3], arr_4[4];
171 	int len = strlen(str_to_encode);
172 	char *ret = malloc(SMB_ENC_LEN);
173 
174 	if (ret == NULL) {
175 		return (NULL);
176 	}
177 
178 	while (len--) {
179 		arr_3[i++] = *(str_to_encode++);
180 		if (i == 3) {
181 			arr_4[0] = (arr_3[0] & 0xfc) >> 2;
182 			arr_4[1] = ((arr_3[0] & 0x03) << 4) +
183 			    ((arr_3[1] & 0xf0) >> 4);
184 			arr_4[2] = ((arr_3[1] & 0x0f) << 2) +
185 			    ((arr_3[2] & 0xc0) >> 6);
186 			arr_4[3] = arr_3[2] & 0x3f;
187 
188 			for (i = 0; i < 4; i++)
189 				ret[ret_cnt++] = b64_data[arr_4[i]];
190 			i = 0;
191 		}
192 	}
193 
194 	if (i) {
195 		for (j = i; j < 3; j++)
196 			arr_3[j] = '\0';
197 
198 		arr_4[0] = (arr_3[0] & 0xfc) >> 2;
199 		arr_4[1] = ((arr_3[0] & 0x03) << 4) +
200 		    ((arr_3[1] & 0xf0) >> 4);
201 		arr_4[2] = ((arr_3[1] & 0x0f) << 2) +
202 		    ((arr_3[2] & 0xc0) >> 6);
203 		arr_4[3] = arr_3[2] & 0x3f;
204 
205 		for (j = 0; j < (i + 1); j++)
206 			ret[ret_cnt++] = b64_data[arr_4[j]];
207 
208 		while (i++ < 3)
209 			ret[ret_cnt++] = '=';
210 	}
211 
212 	ret[ret_cnt++] = '\0';
213 	return (ret);
214 }
215 
216 /*
217  * smb_base64_decode
218  *
219  * Decode using base64 algorithm.
220  * Caller should free the returned buffer when done.
221  */
222 static char *
223 smb_base64_decode(char *encoded_str)
224 {
225 	int len = strlen(encoded_str);
226 	int i = 0, j = 0;
227 	int en_ind = 0;
228 	char arr_4[4], arr_3[3];
229 	int ret_cnt = 0;
230 	char *ret = malloc(SMB_DEC_LEN);
231 	char *p;
232 
233 	if (ret == NULL) {
234 		return (NULL);
235 	}
236 
237 	while (len-- && (encoded_str[en_ind] != '=') &&
238 	    smb_is_base64(encoded_str[en_ind])) {
239 		arr_4[i++] = encoded_str[en_ind];
240 		en_ind++;
241 		if (i == 4) {
242 			for (i = 0; i < 4; i++) {
243 				if ((p = strchr(b64_data, arr_4[i])) == NULL)
244 					return (NULL);
245 
246 				arr_4[i] = (int)(p - b64_data);
247 			}
248 
249 			arr_3[0] = (arr_4[0] << 2) +
250 			    ((arr_4[1] & 0x30) >> 4);
251 			arr_3[1] = ((arr_4[1] & 0xf) << 4) +
252 			    ((arr_4[2] & 0x3c) >> 2);
253 			arr_3[2] = ((arr_4[2] & 0x3) << 6) +
254 			    arr_4[3];
255 
256 			for (i = 0; i < 3; i++)
257 				ret[ret_cnt++] = arr_3[i];
258 
259 			i = 0;
260 		}
261 	}
262 
263 	if (i) {
264 		for (j = i; j < 4; j++)
265 			arr_4[j] = 0;
266 
267 		for (j = 0; j < 4; j++) {
268 			if ((p = strchr(b64_data, arr_4[j])) == NULL)
269 				return (NULL);
270 
271 			arr_4[j] = (int)(p - b64_data);
272 		}
273 		arr_3[0] = (arr_4[0] << 2) +
274 		    ((arr_4[1] & 0x30) >> 4);
275 		arr_3[1] = ((arr_4[1] & 0xf) << 4) +
276 		    ((arr_4[2] & 0x3c) >> 2);
277 		arr_3[2] = ((arr_4[2] & 0x3) << 6) +
278 		    arr_4[3];
279 		for (j = 0; j < (i - 1); j++)
280 			ret[ret_cnt++] = arr_3[j];
281 	}
282 
283 	ret[ret_cnt++] = '\0';
284 	return (ret);
285 }
286 
287 static char *
288 smb_config_getenv_generic(char *name, char *svc_fmri_prefix, char *svc_propgrp)
289 {
290 	smb_scfhandle_t *handle;
291 	char *value;
292 
293 	if ((value = malloc(MAX_VALUE_BUFLEN * sizeof (char))) == NULL)
294 		return (NULL);
295 
296 	handle = smb_smf_scf_init(svc_fmri_prefix);
297 	if (handle == NULL) {
298 		free(value);
299 		return (NULL);
300 	}
301 
302 	(void) smb_smf_create_service_pgroup(handle, svc_propgrp);
303 
304 	if (smb_smf_get_string_property(handle, name, value,
305 	    sizeof (char) * MAX_VALUE_BUFLEN) != 0) {
306 		smb_smf_scf_fini(handle);
307 		free(value);
308 		return (NULL);
309 	}
310 
311 	smb_smf_scf_fini(handle);
312 	return (value);
313 
314 }
315 
316 static int
317 smb_config_setenv_generic(char *svc_fmri_prefix, char *svc_propgrp,
318     char *name, char *value)
319 {
320 	smb_scfhandle_t *handle = NULL;
321 	int rc = 0;
322 
323 
324 	handle = smb_smf_scf_init(svc_fmri_prefix);
325 	if (handle == NULL) {
326 		return (1);
327 	}
328 
329 	(void) smb_smf_create_service_pgroup(handle, svc_propgrp);
330 
331 	if (smb_smf_start_transaction(handle) != SMBD_SMF_OK) {
332 		smb_smf_scf_fini(handle);
333 		return (1);
334 	}
335 
336 	if (smb_smf_set_string_property(handle, name, value) != SMBD_SMF_OK)
337 		rc = 1;
338 
339 	if (smb_smf_end_transaction(handle) != SMBD_SMF_OK)
340 		rc = 1;
341 
342 	smb_smf_scf_fini(handle);
343 	return (rc);
344 }
345 
346 /*
347  * smb_config_getstr
348  *
349  * Fetch the specified string configuration item from SMF
350  */
351 int
352 smb_config_getstr(smb_cfg_id_t id, char *cbuf, int bufsz)
353 {
354 	smb_scfhandle_t *handle;
355 	smb_cfg_param_t *cfg;
356 	int rc = SMBD_SMF_OK;
357 	char *pg;
358 
359 	*cbuf = '\0';
360 	cfg = smb_config_getent(id);
361 	assert(cfg->sc_type == SCF_TYPE_ASTRING);
362 
363 	handle = smb_smf_scf_init(SMBD_FMRI_PREFIX);
364 	if (handle == NULL)
365 		return (SMBD_SMF_SYSTEM_ERR);
366 
367 	if (cfg->sc_flags & SMB_CF_PROTECTED) {
368 		char protbuf[SMB_ENC_LEN];
369 		char *tmp;
370 
371 		if ((rc = smb_smf_create_service_pgroup(handle,
372 		    SMBD_PROTECTED_PG_NAME)) != SMBD_SMF_OK)
373 			goto error;
374 
375 		if ((rc = smb_smf_get_string_property(handle, cfg->sc_name,
376 		    protbuf, sizeof (protbuf))) != SMBD_SMF_OK)
377 			goto error;
378 
379 		if (*protbuf != '\0') {
380 			tmp = smb_base64_decode(protbuf);
381 			(void) strlcpy(cbuf, tmp, bufsz);
382 			free(tmp);
383 		}
384 	} else {
385 		pg = (cfg->sc_flags & SMB_CF_EXEC) ? SMBD_EXEC_PG_NAME :
386 		    SMBD_PG_NAME;
387 		rc = smb_smf_create_service_pgroup(handle, pg);
388 		if (rc == SMBD_SMF_OK)
389 			rc = smb_smf_get_string_property(handle, cfg->sc_name,
390 			    cbuf, bufsz);
391 	}
392 
393 error:
394 	smb_smf_scf_fini(handle);
395 	return (rc);
396 }
397 
398 int
399 smb_config_getip(smb_cfg_id_t sc_id, smb_inaddr_t *ipaddr)
400 {
401 	int rc;
402 	char ipstr[INET6_ADDRSTRLEN];
403 
404 	if (ipaddr == NULL)
405 		return (SMBD_SMF_INVALID_ARG);
406 
407 	bzero(ipaddr, sizeof (smb_inaddr_t));
408 	rc = smb_config_getstr(sc_id, ipstr, sizeof (ipstr));
409 	if (rc == SMBD_SMF_OK) {
410 		if (inet_pton(AF_INET, ipstr, &ipaddr->a_ipv4) == 1) {
411 			ipaddr->a_family = AF_INET;
412 			return (SMBD_SMF_OK);
413 		}
414 
415 		if (inet_pton(AF_INET6, ipstr, &ipaddr->a_ipv6) == 1) {
416 			ipaddr->a_family = AF_INET6;
417 			rc = SMBD_SMF_OK;
418 		} else {
419 			rc = SMBD_SMF_INVALID_ARG;
420 		}
421 	}
422 
423 	return (rc);
424 }
425 
426 /*
427  * smb_config_getnum
428  *
429  * Returns the value of a numeric config param.
430  */
431 int
432 smb_config_getnum(smb_cfg_id_t id, int64_t *cint)
433 {
434 	smb_scfhandle_t *handle;
435 	smb_cfg_param_t *cfg;
436 	int rc = SMBD_SMF_OK;
437 
438 	*cint = 0;
439 	cfg = smb_config_getent(id);
440 	assert(cfg->sc_type == SCF_TYPE_INTEGER);
441 
442 	handle = smb_smf_scf_init(SMBD_FMRI_PREFIX);
443 	if (handle == NULL)
444 		return (SMBD_SMF_SYSTEM_ERR);
445 
446 	rc = smb_smf_create_service_pgroup(handle, SMBD_PG_NAME);
447 	if (rc == SMBD_SMF_OK)
448 		rc = smb_smf_get_integer_property(handle, cfg->sc_name, cint);
449 	smb_smf_scf_fini(handle);
450 
451 	return (rc);
452 }
453 
454 /*
455  * smb_config_getbool
456  *
457  * Returns the value of a boolean config param.
458  */
459 boolean_t
460 smb_config_getbool(smb_cfg_id_t id)
461 {
462 	smb_scfhandle_t *handle;
463 	smb_cfg_param_t *cfg;
464 	int rc = SMBD_SMF_OK;
465 	uint8_t vbool;
466 
467 	cfg = smb_config_getent(id);
468 	assert(cfg->sc_type == SCF_TYPE_BOOLEAN);
469 
470 	handle = smb_smf_scf_init(SMBD_FMRI_PREFIX);
471 	if (handle == NULL)
472 		return (B_FALSE);
473 
474 	rc = smb_smf_create_service_pgroup(handle, SMBD_PG_NAME);
475 	if (rc == SMBD_SMF_OK)
476 		rc = smb_smf_get_boolean_property(handle, cfg->sc_name, &vbool);
477 	smb_smf_scf_fini(handle);
478 
479 	return ((rc == SMBD_SMF_OK) ? (vbool == 1) : B_FALSE);
480 }
481 
482 /*
483  * smb_config_get
484  *
485  * This function returns the value of the requested config
486  * iterm regardless of its type in string format. This should
487  * be used when the config item type is not known by the caller.
488  */
489 int
490 smb_config_get(smb_cfg_id_t id, char *cbuf, int bufsz)
491 {
492 	smb_cfg_param_t *cfg;
493 	int64_t cint;
494 	int rc;
495 
496 	cfg = smb_config_getent(id);
497 	switch (cfg->sc_type) {
498 	case SCF_TYPE_ASTRING:
499 		return (smb_config_getstr(id, cbuf, bufsz));
500 
501 	case SCF_TYPE_INTEGER:
502 		rc = smb_config_getnum(id, &cint);
503 		if (rc == SMBD_SMF_OK)
504 			(void) snprintf(cbuf, bufsz, "%lld", cint);
505 		return (rc);
506 
507 	case SCF_TYPE_BOOLEAN:
508 		if (smb_config_getbool(id))
509 			(void) strlcpy(cbuf, "true", bufsz);
510 		else
511 			(void) strlcpy(cbuf, "false", bufsz);
512 		return (SMBD_SMF_OK);
513 	}
514 
515 	return (SMBD_SMF_INVALID_ARG);
516 }
517 
518 /*
519  * smb_config_setstr
520  *
521  * Set the specified config param with the given
522  * value.
523  */
524 int
525 smb_config_setstr(smb_cfg_id_t id, char *value)
526 {
527 	smb_scfhandle_t *handle;
528 	smb_cfg_param_t *cfg;
529 	int rc = SMBD_SMF_OK;
530 	boolean_t protected;
531 	char *tmp = NULL;
532 	char *pg;
533 
534 	cfg = smb_config_getent(id);
535 	assert(cfg->sc_type == SCF_TYPE_ASTRING);
536 
537 	protected = B_FALSE;
538 
539 	switch (cfg->sc_flags) {
540 	case SMB_CF_PROTECTED:
541 		protected = B_TRUE;
542 		pg = SMBD_PROTECTED_PG_NAME;
543 		break;
544 	case SMB_CF_EXEC:
545 		pg = SMBD_EXEC_PG_NAME;
546 		break;
547 	default:
548 		pg = SMBD_PG_NAME;
549 		break;
550 	}
551 
552 	handle = smb_smf_scf_init(SMBD_FMRI_PREFIX);
553 	if (handle == NULL)
554 		return (SMBD_SMF_SYSTEM_ERR);
555 
556 	rc = smb_smf_create_service_pgroup(handle, pg);
557 	if (rc == SMBD_SMF_OK)
558 		rc = smb_smf_start_transaction(handle);
559 
560 	if (rc != SMBD_SMF_OK) {
561 		smb_smf_scf_fini(handle);
562 		return (rc);
563 	}
564 
565 	if (protected && value && (*value != '\0')) {
566 		if ((tmp = smb_base64_encode(value)) == NULL) {
567 			(void) smb_smf_end_transaction(handle);
568 			smb_smf_scf_fini(handle);
569 			return (SMBD_SMF_NO_MEMORY);
570 		}
571 
572 		value = tmp;
573 	}
574 
575 	rc = smb_smf_set_string_property(handle, cfg->sc_name, value);
576 
577 	free(tmp);
578 	(void) smb_smf_end_transaction(handle);
579 	smb_smf_scf_fini(handle);
580 	return (rc);
581 }
582 
583 /*
584  * smb_config_setnum
585  *
586  * Sets a numeric configuration iterm
587  */
588 int
589 smb_config_setnum(smb_cfg_id_t id, int64_t value)
590 {
591 	smb_scfhandle_t *handle;
592 	smb_cfg_param_t *cfg;
593 	int rc = SMBD_SMF_OK;
594 
595 	cfg = smb_config_getent(id);
596 	assert(cfg->sc_type == SCF_TYPE_INTEGER);
597 
598 	handle = smb_smf_scf_init(SMBD_FMRI_PREFIX);
599 	if (handle == NULL)
600 		return (SMBD_SMF_SYSTEM_ERR);
601 
602 	rc = smb_smf_create_service_pgroup(handle, SMBD_PG_NAME);
603 	if (rc == SMBD_SMF_OK)
604 		rc = smb_smf_start_transaction(handle);
605 
606 	if (rc != SMBD_SMF_OK) {
607 		smb_smf_scf_fini(handle);
608 		return (rc);
609 	}
610 
611 	rc = smb_smf_set_integer_property(handle, cfg->sc_name, value);
612 
613 	(void) smb_smf_end_transaction(handle);
614 	smb_smf_scf_fini(handle);
615 	return (rc);
616 }
617 
618 /*
619  * smb_config_setbool
620  *
621  * Sets a boolean configuration iterm
622  */
623 int
624 smb_config_setbool(smb_cfg_id_t id, boolean_t value)
625 {
626 	smb_scfhandle_t *handle;
627 	smb_cfg_param_t *cfg;
628 	int rc = SMBD_SMF_OK;
629 
630 	cfg = smb_config_getent(id);
631 	assert(cfg->sc_type == SCF_TYPE_BOOLEAN);
632 
633 	handle = smb_smf_scf_init(SMBD_FMRI_PREFIX);
634 	if (handle == NULL)
635 		return (SMBD_SMF_SYSTEM_ERR);
636 
637 	rc = smb_smf_create_service_pgroup(handle, SMBD_PG_NAME);
638 	if (rc == SMBD_SMF_OK)
639 		rc = smb_smf_start_transaction(handle);
640 
641 	if (rc != SMBD_SMF_OK) {
642 		smb_smf_scf_fini(handle);
643 		return (rc);
644 	}
645 
646 	rc = smb_smf_set_boolean_property(handle, cfg->sc_name, value);
647 
648 	(void) smb_smf_end_transaction(handle);
649 	smb_smf_scf_fini(handle);
650 	return (rc);
651 }
652 
653 /*
654  * smb_config_set
655  *
656  * This function sets the value of the specified config
657  * iterm regardless of its type in string format. This should
658  * be used when the config item type is not known by the caller.
659  */
660 int
661 smb_config_set(smb_cfg_id_t id, char *value)
662 {
663 	smb_cfg_param_t *cfg;
664 	int64_t cint;
665 
666 	cfg = smb_config_getent(id);
667 	switch (cfg->sc_type) {
668 	case SCF_TYPE_ASTRING:
669 		return (smb_config_setstr(id, value));
670 
671 	case SCF_TYPE_INTEGER:
672 		cint = atoi(value);
673 		return (smb_config_setnum(id, cint));
674 
675 	case SCF_TYPE_BOOLEAN:
676 		return (smb_config_setbool(id, strcasecmp(value, "true") == 0));
677 	}
678 
679 	return (SMBD_SMF_INVALID_ARG);
680 }
681 uint8_t
682 smb_config_get_fg_flag()
683 {
684 	uint8_t run_fg = 0; /* Default is to run in daemon mode */
685 	smb_scfhandle_t *handle = NULL;
686 
687 	handle = smb_smf_scf_init(SMBD_FMRI_PREFIX);
688 	if (handle == NULL) {
689 		return (run_fg);
690 	}
691 
692 	if (smb_smf_create_service_pgroup(handle,
693 	    SMBD_PG_NAME) != SMBD_SMF_OK) {
694 		smb_smf_scf_fini(handle);
695 		return (run_fg);
696 	}
697 
698 	if (smb_smf_get_boolean_property(handle, "run_fg", &run_fg) != 0) {
699 		smb_smf_scf_fini(handle);
700 		return (run_fg);
701 	}
702 
703 	smb_smf_scf_fini(handle);
704 
705 	return (run_fg);
706 }
707 
708 /*
709  * smb_config_get_localsid
710  *
711  * Returns value of the "config/machine_sid" parameter
712  * from the IDMAP SMF configuration repository.
713  *
714  */
715 char *
716 smb_config_get_localsid(void)
717 {
718 	return (smb_config_getenv_generic(MACHINE_SID, IDMAP_FMRI_PREFIX,
719 	    IDMAP_PG_NAME));
720 }
721 
722 /*
723  * smb_config_set_idmap_domain
724  *
725  * Set the "config/domain_name" parameter from IDMAP SMF repository.
726  */
727 int
728 smb_config_set_idmap_domain(char *value)
729 {
730 	return (smb_config_setenv_generic(IDMAP_FMRI_PREFIX, IDMAP_PG_NAME,
731 	    IDMAP_DOMAIN, value));
732 }
733 
734 /*
735  * smb_config_refresh_idmap
736  *
737  * Refresh IDMAP SMF service after making changes to its configuration.
738  */
739 int
740 smb_config_refresh_idmap(void)
741 {
742 	char instance[32];
743 
744 	(void) snprintf(instance, sizeof (instance), "%s:default",
745 	    IDMAP_FMRI_PREFIX);
746 	return (smf_refresh_instance(instance));
747 }
748 
749 int
750 smb_config_secmode_fromstr(char *secmode)
751 {
752 	if (secmode == NULL)
753 		return (SMB_SECMODE_WORKGRP);
754 
755 	if (strcasecmp(secmode, SMB_SECMODE_DOMAIN_STR) == 0)
756 		return (SMB_SECMODE_DOMAIN);
757 
758 	return (SMB_SECMODE_WORKGRP);
759 }
760 
761 char *
762 smb_config_secmode_tostr(int secmode)
763 {
764 	if (secmode == SMB_SECMODE_DOMAIN)
765 		return (SMB_SECMODE_DOMAIN_STR);
766 
767 	return (SMB_SECMODE_WORKGRP_STR);
768 }
769 
770 int
771 smb_config_get_secmode()
772 {
773 	char p[16];
774 
775 	(void) smb_config_getstr(SMB_CI_SECURITY, p, sizeof (p));
776 	return (smb_config_secmode_fromstr(p));
777 }
778 
779 int
780 smb_config_set_secmode(int secmode)
781 {
782 	char *p;
783 
784 	p = smb_config_secmode_tostr(secmode);
785 	return (smb_config_setstr(SMB_CI_SECURITY, p));
786 }
787 
788 static smb_cfg_param_t *
789 smb_config_getent(smb_cfg_id_t id)
790 {
791 	int i;
792 
793 	for (i = 0; i < SMB_CI_MAX; i++)
794 		if (smb_cfg_table[i].sc_id == id)
795 			return (&smb_cfg_table[id]);
796 
797 	assert(0);
798 	return (NULL);
799 }
800 
801 void
802 smb_config_getdomaininfo(char *domain, char *fqdn, char *sid, char *forest,
803     char *guid)
804 {
805 	if (domain)
806 		(void) smb_config_getstr(SMB_CI_DOMAIN_NAME, domain,
807 		    NETBIOS_NAME_SZ);
808 
809 	if (fqdn)
810 		(void) smb_config_getstr(SMB_CI_DOMAIN_FQDN, fqdn,
811 		    MAXHOSTNAMELEN);
812 
813 	if (sid)
814 		(void) smb_config_getstr(SMB_CI_DOMAIN_SID, sid,
815 		    SMB_SID_STRSZ);
816 
817 	if (forest)
818 		(void) smb_config_getstr(SMB_CI_DOMAIN_FOREST, forest,
819 		    MAXHOSTNAMELEN);
820 
821 	if (guid)
822 		(void) smb_config_getstr(SMB_CI_DOMAIN_GUID, guid,
823 		    UUID_PRINTABLE_STRING_LENGTH);
824 }
825 
826 void
827 smb_config_setdomaininfo(char *domain, char *fqdn, char *sid, char *forest,
828     char *guid)
829 {
830 	if (domain)
831 		(void) smb_config_setstr(SMB_CI_DOMAIN_NAME, domain);
832 	if (fqdn)
833 		(void) smb_config_setstr(SMB_CI_DOMAIN_FQDN, fqdn);
834 	if (sid)
835 		(void) smb_config_setstr(SMB_CI_DOMAIN_SID, sid);
836 	if (forest)
837 		(void) smb_config_setstr(SMB_CI_DOMAIN_FOREST, forest);
838 	if (guid)
839 		(void) smb_config_setstr(SMB_CI_DOMAIN_GUID, guid);
840 }
841