xref: /illumos-gate/usr/src/lib/smbsrv/libsmb/common/smb_cfg.c (revision b3700b074e637f8c6991b70754c88a2cfffb246b)
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 (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
23  * Copyright 2014 Nexenta Systems, Inc.  All rights reserved.
24  */
25 
26 /*
27  * CIFS configuration management library
28  */
29 
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <unistd.h>
33 #include <synch.h>
34 #include <string.h>
35 #include <strings.h>
36 #include <syslog.h>
37 #include <netdb.h>
38 #include <ctype.h>
39 #include <sys/types.h>
40 #include <libscf.h>
41 #include <assert.h>
42 #include <uuid/uuid.h>
43 #include <smbsrv/libsmb.h>
44 
45 typedef struct smb_cfg_param {
46 	smb_cfg_id_t sc_id;
47 	char *sc_name;
48 	int sc_type;
49 	uint32_t sc_flags;
50 } smb_cfg_param_t;
51 
52 /*
53  * config parameter flags
54  */
55 #define	SMB_CF_PROTECTED	0x01
56 #define	SMB_CF_EXEC		0x02
57 
58 /* idmap SMF fmri and Property Group */
59 #define	IDMAP_FMRI_PREFIX		"system/idmap"
60 #define	MACHINE_SID			"machine_sid"
61 #define	MACHINE_UUID			"machine_uuid"
62 #define	IDMAP_DOMAIN			"domain_name"
63 #define	IDMAP_PREF_DC			"preferred_dc"
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 	{SMB_CI_VERSION, "sv_version", SCF_TYPE_ASTRING, 0},
78 
79 	/* Oplock configuration, Kernel Only */
80 	{SMB_CI_OPLOCK_ENABLE, "oplock_enable", SCF_TYPE_BOOLEAN, 0},
81 
82 	/* Autohome configuration */
83 	{SMB_CI_AUTOHOME_MAP, "autohome_map", SCF_TYPE_ASTRING, 0},
84 
85 	/* Domain/PDC configuration */
86 	{SMB_CI_DOMAIN_SID, "domain_sid", SCF_TYPE_ASTRING, 0},
87 	{SMB_CI_DOMAIN_MEMB, "domain_member", SCF_TYPE_BOOLEAN, 0},
88 	{SMB_CI_DOMAIN_NAME, "domain_name", SCF_TYPE_ASTRING, 0},
89 	{SMB_CI_DOMAIN_FQDN, "fqdn", SCF_TYPE_ASTRING, 0},
90 	{SMB_CI_DOMAIN_FOREST, "forest", SCF_TYPE_ASTRING, 0},
91 	{SMB_CI_DOMAIN_GUID, "domain_guid", SCF_TYPE_ASTRING, 0},
92 	{SMB_CI_DOMAIN_SRV, "pdc", SCF_TYPE_ASTRING, 0},
93 
94 	/* WINS configuration */
95 	{SMB_CI_WINS_SRV1, "wins_server_1", SCF_TYPE_ASTRING, 0},
96 	{SMB_CI_WINS_SRV2, "wins_server_2", SCF_TYPE_ASTRING, 0},
97 	{SMB_CI_WINS_EXCL, "wins_exclude", SCF_TYPE_ASTRING, 0},
98 
99 	/* Kmod specific configuration */
100 	{SMB_CI_MAX_WORKERS, "max_workers", SCF_TYPE_INTEGER, 0},
101 	{SMB_CI_MAX_CONNECTIONS, "max_connections", SCF_TYPE_INTEGER, 0},
102 	{SMB_CI_KEEPALIVE, "keep_alive", SCF_TYPE_INTEGER, 0},
103 	{SMB_CI_RESTRICT_ANON, "restrict_anonymous", SCF_TYPE_BOOLEAN, 0},
104 
105 	{SMB_CI_SIGNING_ENABLE, "signing_enabled", SCF_TYPE_BOOLEAN, 0},
106 	{SMB_CI_SIGNING_REQD, "signing_required", SCF_TYPE_BOOLEAN, 0},
107 
108 	/* Kmod tuning configuration */
109 	{SMB_CI_SYNC_ENABLE, "sync_enable", SCF_TYPE_BOOLEAN, 0},
110 
111 	/* SMBd configuration */
112 	{SMB_CI_SECURITY, "security", SCF_TYPE_ASTRING, 0},
113 	{SMB_CI_NETBIOS_ENABLE, "netbios_enable", SCF_TYPE_BOOLEAN, 0},
114 	{SMB_CI_NBSCOPE, "netbios_scope", SCF_TYPE_ASTRING, 0},
115 	{SMB_CI_SYS_CMNT, "system_comment", SCF_TYPE_ASTRING, 0},
116 	{SMB_CI_LM_LEVEL, "lmauth_level", SCF_TYPE_INTEGER, 0},
117 
118 	/* ADS Configuration */
119 	{SMB_CI_ADS_SITE, "ads_site", SCF_TYPE_ASTRING, 0},
120 
121 	/* Dynamic DNS */
122 	{SMB_CI_DYNDNS_ENABLE, "ddns_enable", SCF_TYPE_BOOLEAN, 0},
123 
124 	{SMB_CI_MACHINE_PASSWD, "machine_passwd", SCF_TYPE_ASTRING,
125 	    SMB_CF_PROTECTED},
126 
127 	{SMB_CI_MACHINE_UUID, "machine_uuid", SCF_TYPE_ASTRING, 0},
128 	{SMB_CI_KPASSWD_SRV, "kpasswd_server", SCF_TYPE_ASTRING, 0},
129 	{SMB_CI_KPASSWD_DOMAIN, "kpasswd_domain", SCF_TYPE_ASTRING, 0},
130 	{SMB_CI_KPASSWD_SEQNUM, "kpasswd_seqnum", SCF_TYPE_INTEGER, 0},
131 	{SMB_CI_NETLOGON_SEQNUM, "netlogon_seqnum", SCF_TYPE_INTEGER, 0},
132 	{SMB_CI_IPV6_ENABLE, "ipv6_enable", SCF_TYPE_BOOLEAN, 0},
133 	{SMB_CI_PRINT_ENABLE, "print_enable", SCF_TYPE_BOOLEAN, 0},
134 	{SMB_CI_MAP, "map", SCF_TYPE_ASTRING, SMB_CF_EXEC},
135 	{SMB_CI_UNMAP, "unmap", SCF_TYPE_ASTRING, SMB_CF_EXEC},
136 	{SMB_CI_DISPOSITION, "disposition", SCF_TYPE_ASTRING, SMB_CF_EXEC},
137 	{SMB_CI_DFS_STDROOT_NUM, "dfs_stdroot_num", SCF_TYPE_INTEGER, 0},
138 	{SMB_CI_TRAVERSE_MOUNTS, "traverse_mounts", SCF_TYPE_BOOLEAN, 0},
139 
140 	/* SMB_CI_MAX */
141 };
142 
143 static smb_cfg_param_t *smb_config_getent(smb_cfg_id_t);
144 
145 static boolean_t smb_is_base64(unsigned char c);
146 static char *smb_base64_encode(char *str_to_encode);
147 static char *smb_base64_decode(char *encoded_str);
148 static int smb_config_get_idmap_preferred_dc(char *, int);
149 static int smb_config_set_idmap_preferred_dc(char *);
150 
151 char *
152 smb_config_getname(smb_cfg_id_t id)
153 {
154 	smb_cfg_param_t *cfg;
155 	cfg = smb_config_getent(id);
156 	return (cfg->sc_name);
157 }
158 
159 static boolean_t
160 smb_is_base64(unsigned char c)
161 {
162 	return (isalnum(c) || (c == '+') || (c == '/'));
163 }
164 
165 /*
166  * smb_base64_encode
167  *
168  * Encode a string using base64 algorithm.
169  * Caller should free the returned buffer when done.
170  */
171 static char *
172 smb_base64_encode(char *str_to_encode)
173 {
174 	int ret_cnt = 0;
175 	int i = 0, j = 0;
176 	char arr_3[3], arr_4[4];
177 	int len = strlen(str_to_encode);
178 	char *ret = malloc(SMB_ENC_LEN);
179 
180 	if (ret == NULL) {
181 		return (NULL);
182 	}
183 
184 	while (len--) {
185 		arr_3[i++] = *(str_to_encode++);
186 		if (i == 3) {
187 			arr_4[0] = (arr_3[0] & 0xfc) >> 2;
188 			arr_4[1] = ((arr_3[0] & 0x03) << 4) +
189 			    ((arr_3[1] & 0xf0) >> 4);
190 			arr_4[2] = ((arr_3[1] & 0x0f) << 2) +
191 			    ((arr_3[2] & 0xc0) >> 6);
192 			arr_4[3] = arr_3[2] & 0x3f;
193 
194 			for (i = 0; i < 4; i++)
195 				ret[ret_cnt++] = b64_data[arr_4[i]];
196 			i = 0;
197 		}
198 	}
199 
200 	if (i) {
201 		for (j = i; j < 3; j++)
202 			arr_3[j] = '\0';
203 
204 		arr_4[0] = (arr_3[0] & 0xfc) >> 2;
205 		arr_4[1] = ((arr_3[0] & 0x03) << 4) +
206 		    ((arr_3[1] & 0xf0) >> 4);
207 		arr_4[2] = ((arr_3[1] & 0x0f) << 2) +
208 		    ((arr_3[2] & 0xc0) >> 6);
209 		arr_4[3] = arr_3[2] & 0x3f;
210 
211 		for (j = 0; j < (i + 1); j++)
212 			ret[ret_cnt++] = b64_data[arr_4[j]];
213 
214 		while (i++ < 3)
215 			ret[ret_cnt++] = '=';
216 	}
217 
218 	ret[ret_cnt++] = '\0';
219 	return (ret);
220 }
221 
222 /*
223  * smb_base64_decode
224  *
225  * Decode using base64 algorithm.
226  * Caller should free the returned buffer when done.
227  */
228 static char *
229 smb_base64_decode(char *encoded_str)
230 {
231 	int len = strlen(encoded_str);
232 	int i = 0, j = 0;
233 	int en_ind = 0;
234 	char arr_4[4], arr_3[3];
235 	int ret_cnt = 0;
236 	char *ret = malloc(SMB_DEC_LEN);
237 	char *p;
238 
239 	if (ret == NULL) {
240 		return (NULL);
241 	}
242 
243 	while (len-- && (encoded_str[en_ind] != '=') &&
244 	    smb_is_base64(encoded_str[en_ind])) {
245 		arr_4[i++] = encoded_str[en_ind];
246 		en_ind++;
247 		if (i == 4) {
248 			for (i = 0; i < 4; i++) {
249 				if ((p = strchr(b64_data, arr_4[i])) == NULL)
250 					return (NULL);
251 
252 				arr_4[i] = (int)(p - b64_data);
253 			}
254 
255 			arr_3[0] = (arr_4[0] << 2) +
256 			    ((arr_4[1] & 0x30) >> 4);
257 			arr_3[1] = ((arr_4[1] & 0xf) << 4) +
258 			    ((arr_4[2] & 0x3c) >> 2);
259 			arr_3[2] = ((arr_4[2] & 0x3) << 6) +
260 			    arr_4[3];
261 
262 			for (i = 0; i < 3; i++)
263 				ret[ret_cnt++] = arr_3[i];
264 
265 			i = 0;
266 		}
267 	}
268 
269 	if (i) {
270 		for (j = i; j < 4; j++)
271 			arr_4[j] = 0;
272 
273 		for (j = 0; j < 4; j++) {
274 			if ((p = strchr(b64_data, arr_4[j])) == NULL)
275 				return (NULL);
276 
277 			arr_4[j] = (int)(p - b64_data);
278 		}
279 		arr_3[0] = (arr_4[0] << 2) +
280 		    ((arr_4[1] & 0x30) >> 4);
281 		arr_3[1] = ((arr_4[1] & 0xf) << 4) +
282 		    ((arr_4[2] & 0x3c) >> 2);
283 		arr_3[2] = ((arr_4[2] & 0x3) << 6) +
284 		    arr_4[3];
285 		for (j = 0; j < (i - 1); j++)
286 			ret[ret_cnt++] = arr_3[j];
287 	}
288 
289 	ret[ret_cnt++] = '\0';
290 	return (ret);
291 }
292 
293 static char *
294 smb_config_getenv_generic(char *name, char *svc_fmri_prefix, char *svc_propgrp)
295 {
296 	smb_scfhandle_t *handle;
297 	char *value;
298 
299 	if ((value = malloc(MAX_VALUE_BUFLEN * sizeof (char))) == NULL)
300 		return (NULL);
301 
302 	handle = smb_smf_scf_init(svc_fmri_prefix);
303 	if (handle == NULL) {
304 		free(value);
305 		return (NULL);
306 	}
307 
308 	(void) smb_smf_create_service_pgroup(handle, svc_propgrp);
309 
310 	if (smb_smf_get_string_property(handle, name, value,
311 	    sizeof (char) * MAX_VALUE_BUFLEN) != 0) {
312 		smb_smf_scf_fini(handle);
313 		free(value);
314 		return (NULL);
315 	}
316 
317 	smb_smf_scf_fini(handle);
318 	return (value);
319 
320 }
321 
322 static int
323 smb_config_setenv_generic(char *svc_fmri_prefix, char *svc_propgrp,
324     char *name, char *value)
325 {
326 	smb_scfhandle_t *handle = NULL;
327 	int rc = 0;
328 
329 
330 	handle = smb_smf_scf_init(svc_fmri_prefix);
331 	if (handle == NULL) {
332 		return (1);
333 	}
334 
335 	(void) smb_smf_create_service_pgroup(handle, svc_propgrp);
336 
337 	if (smb_smf_start_transaction(handle) != SMBD_SMF_OK) {
338 		smb_smf_scf_fini(handle);
339 		return (1);
340 	}
341 
342 	if (smb_smf_set_string_property(handle, name, value) != SMBD_SMF_OK)
343 		rc = 1;
344 
345 	if (smb_smf_end_transaction(handle) != SMBD_SMF_OK)
346 		rc = 1;
347 
348 	smb_smf_scf_fini(handle);
349 	return (rc);
350 }
351 
352 /*
353  * smb_config_getstr
354  *
355  * Fetch the specified string configuration item from SMF
356  */
357 int
358 smb_config_getstr(smb_cfg_id_t id, char *cbuf, int bufsz)
359 {
360 	smb_scfhandle_t *handle;
361 	smb_cfg_param_t *cfg;
362 	int rc = SMBD_SMF_OK;
363 	char *pg;
364 	char protbuf[SMB_ENC_LEN];
365 	char *tmp;
366 
367 	*cbuf = '\0';
368 	cfg = smb_config_getent(id);
369 	assert(cfg->sc_type == SCF_TYPE_ASTRING);
370 
371 	if (id == SMB_CI_DOMAIN_SRV)
372 		return (smb_config_get_idmap_preferred_dc(cbuf, bufsz));
373 
374 	handle = smb_smf_scf_init(SMBD_FMRI_PREFIX);
375 	if (handle == NULL)
376 		return (SMBD_SMF_SYSTEM_ERR);
377 
378 	if (cfg->sc_flags & SMB_CF_PROTECTED) {
379 		if ((rc = smb_smf_create_service_pgroup(handle,
380 		    SMBD_PROTECTED_PG_NAME)) != SMBD_SMF_OK)
381 			goto error;
382 
383 		if ((rc = smb_smf_get_string_property(handle, cfg->sc_name,
384 		    protbuf, sizeof (protbuf))) != SMBD_SMF_OK)
385 			goto error;
386 
387 		if (*protbuf != '\0') {
388 			tmp = smb_base64_decode(protbuf);
389 			(void) strlcpy(cbuf, tmp, bufsz);
390 			free(tmp);
391 		}
392 	} else {
393 		pg = (cfg->sc_flags & SMB_CF_EXEC) ? SMBD_EXEC_PG_NAME :
394 		    SMBD_PG_NAME;
395 		rc = smb_smf_create_service_pgroup(handle, pg);
396 		if (rc == SMBD_SMF_OK)
397 			rc = smb_smf_get_string_property(handle, cfg->sc_name,
398 			    cbuf, bufsz);
399 	}
400 
401 error:
402 	smb_smf_scf_fini(handle);
403 	return (rc);
404 }
405 
406 /*
407  * Translate the value of an astring SMF property into a binary
408  * IP address. If the value is neither a valid IPv4 nor IPv6
409  * address, attempt to look it up as a hostname using the
410  * configured address type.
411  */
412 int
413 smb_config_getip(smb_cfg_id_t sc_id, smb_inaddr_t *ipaddr)
414 {
415 	int rc, error;
416 	int a_family;
417 	char ipstr[MAXHOSTNAMELEN];
418 	struct hostent *h;
419 	smb_cfg_param_t *cfg;
420 
421 	if (ipaddr == NULL)
422 		return (SMBD_SMF_INVALID_ARG);
423 
424 	bzero(ipaddr, sizeof (smb_inaddr_t));
425 	rc = smb_config_getstr(sc_id, ipstr, sizeof (ipstr));
426 	if (rc == SMBD_SMF_OK) {
427 		if (*ipstr == '\0')
428 			return (SMBD_SMF_INVALID_ARG);
429 
430 		if (inet_pton(AF_INET, ipstr, &ipaddr->a_ipv4) == 1) {
431 			ipaddr->a_family = AF_INET;
432 			return (SMBD_SMF_OK);
433 		}
434 
435 		if (inet_pton(AF_INET6, ipstr, &ipaddr->a_ipv6) == 1) {
436 			ipaddr->a_family = AF_INET6;
437 			return (SMBD_SMF_OK);
438 		}
439 
440 		/*
441 		 * The value is neither an IPv4 nor IPv6 address;
442 		 * so check if it's a hostname.
443 		 */
444 		a_family = smb_config_getbool(SMB_CI_IPV6_ENABLE) ?
445 		    AF_INET6 : AF_INET;
446 		h = getipnodebyname(ipstr, a_family, AI_DEFAULT,
447 		    &error);
448 		if (h != NULL) {
449 			bcopy(*(h->h_addr_list), &ipaddr->a_ip,
450 			    h->h_length);
451 			ipaddr->a_family = a_family;
452 			freehostent(h);
453 			rc = SMBD_SMF_OK;
454 		} else {
455 			cfg = smb_config_getent(sc_id);
456 			syslog(LOG_ERR, "smbd/%s: %s unable to get %s "
457 			    "address: %d", cfg->sc_name, ipstr,
458 			    a_family == AF_INET ?  "IPv4" : "IPv6", error);
459 			rc = SMBD_SMF_INVALID_ARG;
460 		}
461 	}
462 
463 	return (rc);
464 }
465 
466 /*
467  * smb_config_getnum
468  *
469  * Returns the value of a numeric config param.
470  */
471 int
472 smb_config_getnum(smb_cfg_id_t id, int64_t *cint)
473 {
474 	smb_scfhandle_t *handle;
475 	smb_cfg_param_t *cfg;
476 	int rc = SMBD_SMF_OK;
477 
478 	*cint = 0;
479 	cfg = smb_config_getent(id);
480 	assert(cfg->sc_type == SCF_TYPE_INTEGER);
481 
482 	handle = smb_smf_scf_init(SMBD_FMRI_PREFIX);
483 	if (handle == NULL)
484 		return (SMBD_SMF_SYSTEM_ERR);
485 
486 	rc = smb_smf_create_service_pgroup(handle, SMBD_PG_NAME);
487 	if (rc == SMBD_SMF_OK)
488 		rc = smb_smf_get_integer_property(handle, cfg->sc_name, cint);
489 	smb_smf_scf_fini(handle);
490 
491 	return (rc);
492 }
493 
494 /*
495  * smb_config_getbool
496  *
497  * Returns the value of a boolean config param.
498  */
499 boolean_t
500 smb_config_getbool(smb_cfg_id_t id)
501 {
502 	smb_scfhandle_t *handle;
503 	smb_cfg_param_t *cfg;
504 	int rc = SMBD_SMF_OK;
505 	uint8_t vbool;
506 
507 	cfg = smb_config_getent(id);
508 	assert(cfg->sc_type == SCF_TYPE_BOOLEAN);
509 
510 	handle = smb_smf_scf_init(SMBD_FMRI_PREFIX);
511 	if (handle == NULL)
512 		return (B_FALSE);
513 
514 	rc = smb_smf_create_service_pgroup(handle, SMBD_PG_NAME);
515 	if (rc == SMBD_SMF_OK)
516 		rc = smb_smf_get_boolean_property(handle, cfg->sc_name, &vbool);
517 	smb_smf_scf_fini(handle);
518 
519 	return ((rc == SMBD_SMF_OK) ? (vbool == 1) : B_FALSE);
520 }
521 
522 /*
523  * smb_config_get
524  *
525  * This function returns the value of the requested config
526  * iterm regardless of its type in string format. This should
527  * be used when the config item type is not known by the caller.
528  */
529 int
530 smb_config_get(smb_cfg_id_t id, char *cbuf, int bufsz)
531 {
532 	smb_cfg_param_t *cfg;
533 	int64_t cint;
534 	int rc;
535 
536 	cfg = smb_config_getent(id);
537 	switch (cfg->sc_type) {
538 	case SCF_TYPE_ASTRING:
539 		return (smb_config_getstr(id, cbuf, bufsz));
540 
541 	case SCF_TYPE_INTEGER:
542 		rc = smb_config_getnum(id, &cint);
543 		if (rc == SMBD_SMF_OK)
544 			(void) snprintf(cbuf, bufsz, "%lld", cint);
545 		return (rc);
546 
547 	case SCF_TYPE_BOOLEAN:
548 		if (smb_config_getbool(id))
549 			(void) strlcpy(cbuf, "true", bufsz);
550 		else
551 			(void) strlcpy(cbuf, "false", bufsz);
552 		return (SMBD_SMF_OK);
553 	}
554 
555 	return (SMBD_SMF_INVALID_ARG);
556 }
557 
558 /*
559  * smb_config_setstr
560  *
561  * Set the specified config param with the given
562  * value.
563  */
564 int
565 smb_config_setstr(smb_cfg_id_t id, char *value)
566 {
567 	smb_scfhandle_t *handle;
568 	smb_cfg_param_t *cfg;
569 	int rc = SMBD_SMF_OK;
570 	boolean_t protected;
571 	char *tmp = NULL;
572 	char *pg;
573 
574 	cfg = smb_config_getent(id);
575 	assert(cfg->sc_type == SCF_TYPE_ASTRING);
576 
577 	if (id == SMB_CI_DOMAIN_SRV)
578 		return (smb_config_set_idmap_preferred_dc(value));
579 
580 	protected = B_FALSE;
581 
582 	switch (cfg->sc_flags) {
583 	case SMB_CF_PROTECTED:
584 		protected = B_TRUE;
585 		pg = SMBD_PROTECTED_PG_NAME;
586 		break;
587 	case SMB_CF_EXEC:
588 		pg = SMBD_EXEC_PG_NAME;
589 		break;
590 	default:
591 		pg = SMBD_PG_NAME;
592 		break;
593 	}
594 
595 	handle = smb_smf_scf_init(SMBD_FMRI_PREFIX);
596 	if (handle == NULL)
597 		return (SMBD_SMF_SYSTEM_ERR);
598 
599 	rc = smb_smf_create_service_pgroup(handle, pg);
600 	if (rc == SMBD_SMF_OK)
601 		rc = smb_smf_start_transaction(handle);
602 
603 	if (rc != SMBD_SMF_OK) {
604 		smb_smf_scf_fini(handle);
605 		return (rc);
606 	}
607 
608 	if (protected && value && (*value != '\0')) {
609 		if ((tmp = smb_base64_encode(value)) == NULL) {
610 			(void) smb_smf_end_transaction(handle);
611 			smb_smf_scf_fini(handle);
612 			return (SMBD_SMF_NO_MEMORY);
613 		}
614 
615 		value = tmp;
616 	}
617 
618 	rc = smb_smf_set_string_property(handle, cfg->sc_name, value);
619 
620 	free(tmp);
621 	(void) smb_smf_end_transaction(handle);
622 	smb_smf_scf_fini(handle);
623 	return (rc);
624 }
625 
626 /*
627  * smb_config_setnum
628  *
629  * Sets a numeric configuration iterm
630  */
631 int
632 smb_config_setnum(smb_cfg_id_t id, int64_t value)
633 {
634 	smb_scfhandle_t *handle;
635 	smb_cfg_param_t *cfg;
636 	int rc = SMBD_SMF_OK;
637 
638 	cfg = smb_config_getent(id);
639 	assert(cfg->sc_type == SCF_TYPE_INTEGER);
640 
641 	handle = smb_smf_scf_init(SMBD_FMRI_PREFIX);
642 	if (handle == NULL)
643 		return (SMBD_SMF_SYSTEM_ERR);
644 
645 	rc = smb_smf_create_service_pgroup(handle, SMBD_PG_NAME);
646 	if (rc == SMBD_SMF_OK)
647 		rc = smb_smf_start_transaction(handle);
648 
649 	if (rc != SMBD_SMF_OK) {
650 		smb_smf_scf_fini(handle);
651 		return (rc);
652 	}
653 
654 	rc = smb_smf_set_integer_property(handle, cfg->sc_name, value);
655 
656 	(void) smb_smf_end_transaction(handle);
657 	smb_smf_scf_fini(handle);
658 	return (rc);
659 }
660 
661 /*
662  * smb_config_setbool
663  *
664  * Sets a boolean configuration iterm
665  */
666 int
667 smb_config_setbool(smb_cfg_id_t id, boolean_t value)
668 {
669 	smb_scfhandle_t *handle;
670 	smb_cfg_param_t *cfg;
671 	int rc = SMBD_SMF_OK;
672 
673 	cfg = smb_config_getent(id);
674 	assert(cfg->sc_type == SCF_TYPE_BOOLEAN);
675 
676 	handle = smb_smf_scf_init(SMBD_FMRI_PREFIX);
677 	if (handle == NULL)
678 		return (SMBD_SMF_SYSTEM_ERR);
679 
680 	rc = smb_smf_create_service_pgroup(handle, SMBD_PG_NAME);
681 	if (rc == SMBD_SMF_OK)
682 		rc = smb_smf_start_transaction(handle);
683 
684 	if (rc != SMBD_SMF_OK) {
685 		smb_smf_scf_fini(handle);
686 		return (rc);
687 	}
688 
689 	rc = smb_smf_set_boolean_property(handle, cfg->sc_name, value);
690 
691 	(void) smb_smf_end_transaction(handle);
692 	smb_smf_scf_fini(handle);
693 	return (rc);
694 }
695 
696 /*
697  * smb_config_set
698  *
699  * This function sets the value of the specified config
700  * iterm regardless of its type in string format. This should
701  * be used when the config item type is not known by the caller.
702  */
703 int
704 smb_config_set(smb_cfg_id_t id, char *value)
705 {
706 	smb_cfg_param_t *cfg;
707 	int64_t cint;
708 
709 	cfg = smb_config_getent(id);
710 	switch (cfg->sc_type) {
711 	case SCF_TYPE_ASTRING:
712 		return (smb_config_setstr(id, value));
713 
714 	case SCF_TYPE_INTEGER:
715 		cint = atoi(value);
716 		return (smb_config_setnum(id, cint));
717 
718 	case SCF_TYPE_BOOLEAN:
719 		return (smb_config_setbool(id, strcasecmp(value, "true") == 0));
720 	}
721 
722 	return (SMBD_SMF_INVALID_ARG);
723 }
724 
725 int
726 smb_config_get_debug()
727 {
728 	int64_t val64;
729 	int val = 0;	/* default */
730 	smb_scfhandle_t *handle = NULL;
731 
732 	handle = smb_smf_scf_init(SMBD_FMRI_PREFIX);
733 	if (handle == NULL) {
734 		return (val);
735 	}
736 
737 	if (smb_smf_create_service_pgroup(handle,
738 	    SMBD_PG_NAME) != SMBD_SMF_OK) {
739 		smb_smf_scf_fini(handle);
740 		return (val);
741 	}
742 
743 	if (smb_smf_get_integer_property(handle, "debug", &val64) != 0) {
744 		smb_smf_scf_fini(handle);
745 		return (val);
746 	}
747 	val = (int)val64;
748 
749 	smb_smf_scf_fini(handle);
750 
751 	return (val);
752 }
753 
754 uint8_t
755 smb_config_get_fg_flag()
756 {
757 	uint8_t run_fg = 0; /* Default is to run in daemon mode */
758 	smb_scfhandle_t *handle = NULL;
759 
760 	handle = smb_smf_scf_init(SMBD_FMRI_PREFIX);
761 	if (handle == NULL) {
762 		return (run_fg);
763 	}
764 
765 	if (smb_smf_create_service_pgroup(handle,
766 	    SMBD_PG_NAME) != SMBD_SMF_OK) {
767 		smb_smf_scf_fini(handle);
768 		return (run_fg);
769 	}
770 
771 	if (smb_smf_get_boolean_property(handle, "run_fg", &run_fg) != 0) {
772 		smb_smf_scf_fini(handle);
773 		return (run_fg);
774 	}
775 
776 	smb_smf_scf_fini(handle);
777 
778 	return (run_fg);
779 }
780 
781 /*
782  * smb_config_get_ads_enable
783  *
784  * Returns value of the "config/use_ads" parameter
785  * from the IDMAP SMF configuration repository.
786  *
787  */
788 boolean_t
789 smb_config_get_ads_enable(void)
790 {
791 	smb_scfhandle_t *handle = NULL;
792 	uint8_t vbool;
793 	int rc = 0;
794 
795 	handle = smb_smf_scf_init(IDMAP_FMRI_PREFIX);
796 	if (handle == NULL)
797 		return (B_FALSE);
798 
799 	rc = smb_smf_create_service_pgroup(handle, IDMAP_PG_NAME);
800 	if (rc == SMBD_SMF_OK)
801 		rc = smb_smf_get_boolean_property(handle, "use_ads", &vbool);
802 	smb_smf_scf_fini(handle);
803 
804 	return ((rc == SMBD_SMF_OK) ? (vbool == 1) : B_TRUE);
805 }
806 
807 /*
808  * smb_config_get_localsid
809  *
810  * Returns value of the "config/machine_sid" parameter
811  * from the IDMAP SMF configuration repository.
812  * Result is allocated; caller should free.
813  */
814 char *
815 smb_config_get_localsid(void)
816 {
817 	return (smb_config_getenv_generic(MACHINE_SID, IDMAP_FMRI_PREFIX,
818 	    IDMAP_PG_NAME));
819 }
820 
821 /*
822  * smb_config_get_localuuid
823  *
824  * Returns value of the "config/machine_uuid" parameter
825  * from the IDMAP SMF configuration repository.
826  *
827  */
828 int
829 smb_config_get_localuuid(uuid_t uu)
830 {
831 	char *s;
832 
833 	uuid_clear(uu);
834 	s = smb_config_getenv_generic(MACHINE_UUID, IDMAP_FMRI_PREFIX,
835 	    IDMAP_PG_NAME);
836 	if (s == NULL)
837 		return (-1);
838 
839 	if (uuid_parse(s, uu) < 0) {
840 		free(s);
841 		return (-1);
842 	}
843 
844 	return (0);
845 }
846 
847 static int
848 smb_config_get_idmap_preferred_dc(char *cbuf, int bufsz)
849 {
850 	char *s;
851 	int len, rc = -1;
852 
853 	s = smb_config_getenv_generic(IDMAP_PREF_DC,
854 	    IDMAP_FMRI_PREFIX, IDMAP_PG_NAME);
855 	if (s != NULL) {
856 		len = strlcpy(cbuf, s, bufsz);
857 		if (len < bufsz)
858 			rc = 0;
859 		free(s);
860 	}
861 	return (rc);
862 }
863 
864 static int
865 smb_config_set_idmap_preferred_dc(char *value)
866 {
867 	return (smb_config_setenv_generic(IDMAP_FMRI_PREFIX, IDMAP_PG_NAME,
868 	    IDMAP_PREF_DC, value));
869 }
870 
871 /*
872  * smb_config_set_idmap_domain
873  *
874  * Set the "config/domain_name" parameter from IDMAP SMF repository.
875  */
876 int
877 smb_config_set_idmap_domain(char *value)
878 {
879 	return (smb_config_setenv_generic(IDMAP_FMRI_PREFIX, IDMAP_PG_NAME,
880 	    IDMAP_DOMAIN, value));
881 }
882 
883 /*
884  * smb_config_refresh_idmap
885  *
886  * Refresh IDMAP SMF service after making changes to its configuration.
887  */
888 int
889 smb_config_refresh_idmap(void)
890 {
891 	char instance[32];
892 
893 	(void) snprintf(instance, sizeof (instance), "%s:default",
894 	    IDMAP_FMRI_PREFIX);
895 	return (smf_refresh_instance(instance));
896 }
897 
898 int
899 smb_config_secmode_fromstr(char *secmode)
900 {
901 	if (secmode == NULL)
902 		return (SMB_SECMODE_WORKGRP);
903 
904 	if (strcasecmp(secmode, SMB_SECMODE_DOMAIN_STR) == 0)
905 		return (SMB_SECMODE_DOMAIN);
906 
907 	return (SMB_SECMODE_WORKGRP);
908 }
909 
910 char *
911 smb_config_secmode_tostr(int secmode)
912 {
913 	if (secmode == SMB_SECMODE_DOMAIN)
914 		return (SMB_SECMODE_DOMAIN_STR);
915 
916 	return (SMB_SECMODE_WORKGRP_STR);
917 }
918 
919 int
920 smb_config_get_secmode()
921 {
922 	char p[16];
923 
924 	(void) smb_config_getstr(SMB_CI_SECURITY, p, sizeof (p));
925 	return (smb_config_secmode_fromstr(p));
926 }
927 
928 int
929 smb_config_set_secmode(int secmode)
930 {
931 	char *p;
932 
933 	p = smb_config_secmode_tostr(secmode);
934 	return (smb_config_setstr(SMB_CI_SECURITY, p));
935 }
936 
937 void
938 smb_config_getdomaininfo(char *domain, char *fqdn, char *sid, char *forest,
939     char *guid)
940 {
941 	if (domain)
942 		(void) smb_config_getstr(SMB_CI_DOMAIN_NAME, domain,
943 		    NETBIOS_NAME_SZ);
944 
945 	if (fqdn)
946 		(void) smb_config_getstr(SMB_CI_DOMAIN_FQDN, fqdn,
947 		    MAXHOSTNAMELEN);
948 
949 	if (sid)
950 		(void) smb_config_getstr(SMB_CI_DOMAIN_SID, sid,
951 		    SMB_SID_STRSZ);
952 
953 	if (forest)
954 		(void) smb_config_getstr(SMB_CI_DOMAIN_FOREST, forest,
955 		    MAXHOSTNAMELEN);
956 
957 	if (guid)
958 		(void) smb_config_getstr(SMB_CI_DOMAIN_GUID, guid,
959 		    UUID_PRINTABLE_STRING_LENGTH);
960 }
961 
962 void
963 smb_config_setdomaininfo(char *domain, char *fqdn, char *sid, char *forest,
964     char *guid)
965 {
966 	if (domain)
967 		(void) smb_config_setstr(SMB_CI_DOMAIN_NAME, domain);
968 	if (fqdn)
969 		(void) smb_config_setstr(SMB_CI_DOMAIN_FQDN, fqdn);
970 	if (sid)
971 		(void) smb_config_setstr(SMB_CI_DOMAIN_SID, sid);
972 	if (forest)
973 		(void) smb_config_setstr(SMB_CI_DOMAIN_FOREST, forest);
974 	if (guid)
975 		(void) smb_config_setstr(SMB_CI_DOMAIN_GUID, guid);
976 }
977 
978 /*
979  * The version stored in SMF in string format as N.N where
980  * N is a number defined by Microsoft. The first number represents
981  * the major version and the second number is the minor version.
982  * Current defined values can be found here in 'ver_table'.
983  *
984  * This function reads the SMF string value and converts it to
985  * two numbers returned in the given 'version' structure.
986  * Current default version number is 5.0 which is for Windows 2000.
987  */
988 void
989 smb_config_get_version(smb_version_t *version)
990 {
991 	smb_version_t tmpver;
992 	char verstr[SMB_VERSTR_LEN];
993 	char *p;
994 	int rc, i;
995 	static smb_version_t ver_table [] = {
996 		{ 0, SMB_MAJOR_NT,	SMB_MINOR_NT,		1381,	0 },
997 		{ 0, SMB_MAJOR_2000,	SMB_MINOR_2000,		2195,	0 },
998 		{ 0, SMB_MAJOR_XP,	SMB_MINOR_XP,		2196,	0 },
999 		{ 0, SMB_MAJOR_2003,	SMB_MINOR_2003,		2196,	0 },
1000 		{ 0, SMB_MAJOR_VISTA,	SMB_MINOR_VISTA,	6000,	0 },
1001 		{ 0, SMB_MAJOR_2008,	SMB_MINOR_2008,		6000,	0 },
1002 		{ 0, SMB_MAJOR_2008R2,	SMB_MINOR_2008R2,	7007,	0 },
1003 		{ 0, SMB_MAJOR_7,	SMB_MINOR_7,		7007,	0 }
1004 	};
1005 
1006 	*version = ver_table[1];
1007 	version->sv_size = sizeof (smb_version_t);
1008 
1009 	rc = smb_config_getstr(SMB_CI_VERSION, verstr, sizeof (verstr));
1010 	if (rc != SMBD_SMF_OK)
1011 		return;
1012 
1013 	if ((p = strchr(verstr, '.')) == NULL)
1014 		return;
1015 
1016 	*p = '\0';
1017 	tmpver.sv_major = (uint8_t)atoi(verstr);
1018 	tmpver.sv_minor = (uint8_t)atoi(p + 1);
1019 
1020 	for (i = 0; i < sizeof (ver_table)/sizeof (ver_table[0]); ++i) {
1021 		if ((tmpver.sv_major == ver_table[i].sv_major) &&
1022 		    (tmpver.sv_minor == ver_table[i].sv_minor)) {
1023 			*version = ver_table[i];
1024 			version->sv_size = sizeof (smb_version_t);
1025 			break;
1026 		}
1027 	}
1028 }
1029 
1030 /*
1031  * Reads share exec script properties
1032  */
1033 uint32_t
1034 smb_config_get_execinfo(char *map, char *unmap, size_t bufsz)
1035 {
1036 	char buf[MAXPATHLEN];
1037 	uint32_t flags = 0;
1038 
1039 	if (map == NULL) {
1040 		map = buf;
1041 		bufsz = MAXPATHLEN;
1042 	}
1043 
1044 	*map = '\0';
1045 	(void) smb_config_getstr(SMB_CI_MAP, map, bufsz);
1046 	if (*map != '\0')
1047 		flags |= SMB_EXEC_MAP;
1048 
1049 	if (unmap == NULL) {
1050 		unmap = buf;
1051 		bufsz = MAXPATHLEN;
1052 	}
1053 
1054 	*unmap = '\0';
1055 	(void) smb_config_getstr(SMB_CI_UNMAP, unmap, bufsz);
1056 	if (*unmap != '\0')
1057 		flags |= SMB_EXEC_UNMAP;
1058 
1059 	*buf = '\0';
1060 	(void) smb_config_getstr(SMB_CI_DISPOSITION, buf, sizeof (buf));
1061 	if (*buf != '\0')
1062 		if (strcasecmp(buf, SMB_EXEC_DISP_TERMINATE) == 0)
1063 			flags |= SMB_EXEC_TERM;
1064 
1065 	return (flags);
1066 }
1067 
1068 static smb_cfg_param_t *
1069 smb_config_getent(smb_cfg_id_t id)
1070 {
1071 	int i;
1072 
1073 	for (i = 0; i < SMB_CI_MAX; i++)
1074 		if (smb_cfg_table[i].sc_id == id)
1075 			return (&smb_cfg_table[id]);
1076 
1077 	assert(0);
1078 	return (NULL);
1079 }
1080