8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / lib / smbsrv / libsmb / common / smb_cfg.c
bloba82e55f5fff83ef527faf2aec544b89efc6bb495
1 /*
2 * CDDL HEADER START
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]
19 * CDDL HEADER END
22 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright 2015 Nexenta Systems, Inc. All rights reserved.
27 * CIFS configuration management library
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>
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;
52 struct str_val {
53 char *str;
54 uint32_t val;
58 * config parameter flags
60 #define SMB_CF_PROTECTED 0x01
61 #define SMB_CF_EXEC 0x02
63 /* idmap SMF fmri and Property Group */
64 #define IDMAP_FMRI_PREFIX "system/idmap"
65 #define MACHINE_SID "machine_sid"
66 #define MACHINE_UUID "machine_uuid"
67 #define IDMAP_DOMAIN "domain_name"
68 #define IDMAP_PREF_DC "preferred_dc"
69 #define IDMAP_PG_NAME "config"
71 #define SMB_SECMODE_WORKGRP_STR "workgroup"
72 #define SMB_SECMODE_DOMAIN_STR "domain"
74 #define SMB_ENC_LEN 1024
75 #define SMB_DEC_LEN 256
77 static char *b64_data =
78 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
80 static smb_cfg_param_t smb_cfg_table[] =
82 {SMB_CI_VERSION, "sv_version", SCF_TYPE_ASTRING, 0},
84 /* Oplock configuration, Kernel Only */
85 {SMB_CI_OPLOCK_ENABLE, "oplock_enable", SCF_TYPE_BOOLEAN, 0},
87 /* Autohome configuration */
88 {SMB_CI_AUTOHOME_MAP, "autohome_map", SCF_TYPE_ASTRING, 0},
90 /* Domain/PDC configuration */
91 {SMB_CI_DOMAIN_SID, "domain_sid", SCF_TYPE_ASTRING, 0},
92 {SMB_CI_DOMAIN_MEMB, "domain_member", SCF_TYPE_BOOLEAN, 0},
93 {SMB_CI_DOMAIN_NAME, "domain_name", SCF_TYPE_ASTRING, 0},
94 {SMB_CI_DOMAIN_FQDN, "fqdn", SCF_TYPE_ASTRING, 0},
95 {SMB_CI_DOMAIN_FOREST, "forest", SCF_TYPE_ASTRING, 0},
96 {SMB_CI_DOMAIN_GUID, "domain_guid", SCF_TYPE_ASTRING, 0},
97 {SMB_CI_DOMAIN_SRV, "pdc", SCF_TYPE_ASTRING, 0},
99 /* WINS configuration */
100 {SMB_CI_WINS_SRV1, "wins_server_1", SCF_TYPE_ASTRING, 0},
101 {SMB_CI_WINS_SRV2, "wins_server_2", SCF_TYPE_ASTRING, 0},
102 {SMB_CI_WINS_EXCL, "wins_exclude", SCF_TYPE_ASTRING, 0},
104 /* Kmod specific configuration */
105 {SMB_CI_MAX_WORKERS, "max_workers", SCF_TYPE_INTEGER, 0},
106 {SMB_CI_MAX_CONNECTIONS, "max_connections", SCF_TYPE_INTEGER, 0},
107 {SMB_CI_KEEPALIVE, "keep_alive", SCF_TYPE_INTEGER, 0},
108 {SMB_CI_RESTRICT_ANON, "restrict_anonymous", SCF_TYPE_BOOLEAN, 0},
110 {SMB_CI_SIGNING_ENABLE, "signing_enabled", SCF_TYPE_BOOLEAN, 0},
111 {SMB_CI_SIGNING_REQD, "signing_required", SCF_TYPE_BOOLEAN, 0},
113 /* Kmod tuning configuration */
114 {SMB_CI_SYNC_ENABLE, "sync_enable", SCF_TYPE_BOOLEAN, 0},
116 /* SMBd configuration */
117 {SMB_CI_SECURITY, "security", SCF_TYPE_ASTRING, 0},
118 {SMB_CI_NETBIOS_ENABLE, "netbios_enable", SCF_TYPE_BOOLEAN, 0},
119 {SMB_CI_NBSCOPE, "netbios_scope", SCF_TYPE_ASTRING, 0},
120 {SMB_CI_SYS_CMNT, "system_comment", SCF_TYPE_ASTRING, 0},
121 {SMB_CI_LM_LEVEL, "lmauth_level", SCF_TYPE_INTEGER, 0},
123 /* ADS Configuration */
124 {SMB_CI_ADS_SITE, "ads_site", SCF_TYPE_ASTRING, 0},
126 /* Dynamic DNS */
127 {SMB_CI_DYNDNS_ENABLE, "ddns_enable", SCF_TYPE_BOOLEAN, 0},
129 {SMB_CI_MACHINE_PASSWD, "machine_passwd", SCF_TYPE_ASTRING,
130 SMB_CF_PROTECTED},
132 {SMB_CI_MACHINE_UUID, "machine_uuid", SCF_TYPE_ASTRING, 0},
133 {SMB_CI_KPASSWD_SRV, "kpasswd_server", SCF_TYPE_ASTRING, 0},
134 {SMB_CI_KPASSWD_DOMAIN, "kpasswd_domain", SCF_TYPE_ASTRING, 0},
135 {SMB_CI_KPASSWD_SEQNUM, "kpasswd_seqnum", SCF_TYPE_INTEGER, 0},
136 {SMB_CI_NETLOGON_SEQNUM, "netlogon_seqnum", SCF_TYPE_INTEGER, 0},
137 {SMB_CI_IPV6_ENABLE, "ipv6_enable", SCF_TYPE_BOOLEAN, 0},
138 {SMB_CI_PRINT_ENABLE, "print_enable", SCF_TYPE_BOOLEAN, 0},
139 {SMB_CI_MAP, "map", SCF_TYPE_ASTRING, SMB_CF_EXEC},
140 {SMB_CI_UNMAP, "unmap", SCF_TYPE_ASTRING, SMB_CF_EXEC},
141 {SMB_CI_DISPOSITION, "disposition", SCF_TYPE_ASTRING, SMB_CF_EXEC},
142 {SMB_CI_DFS_STDROOT_NUM, "dfs_stdroot_num", SCF_TYPE_INTEGER, 0},
143 {SMB_CI_TRAVERSE_MOUNTS, "traverse_mounts", SCF_TYPE_BOOLEAN, 0},
144 {SMB_CI_SMB2_ENABLE_OLD, "smb2_enable", SCF_TYPE_BOOLEAN, 0},
145 {SMB_CI_INITIAL_CREDITS, "initial_credits", SCF_TYPE_INTEGER, 0},
146 {SMB_CI_MAXIMUM_CREDITS, "maximum_credits", SCF_TYPE_INTEGER, 0},
147 {SMB_CI_MAX_PROTOCOL, "max_protocol", SCF_TYPE_ASTRING, 0},
149 /* SMB_CI_MAX */
152 static smb_cfg_param_t *smb_config_getent(smb_cfg_id_t);
154 static boolean_t smb_is_base64(unsigned char c);
155 static char *smb_base64_encode(char *str_to_encode);
156 static char *smb_base64_decode(char *encoded_str);
157 static int smb_config_get_idmap_preferred_dc(char *, int);
158 static int smb_config_set_idmap_preferred_dc(char *);
160 char *
161 smb_config_getname(smb_cfg_id_t id)
163 smb_cfg_param_t *cfg;
164 cfg = smb_config_getent(id);
165 return (cfg->sc_name);
168 static boolean_t
169 smb_is_base64(unsigned char c)
171 return (isalnum(c) || (c == '+') || (c == '/'));
175 * smb_base64_encode
177 * Encode a string using base64 algorithm.
178 * Caller should free the returned buffer when done.
180 static char *
181 smb_base64_encode(char *str_to_encode)
183 int ret_cnt = 0;
184 int i = 0, j = 0;
185 char arr_3[3], arr_4[4];
186 int len = strlen(str_to_encode);
187 char *ret = malloc(SMB_ENC_LEN);
189 if (ret == NULL) {
190 return (NULL);
193 while (len--) {
194 arr_3[i++] = *(str_to_encode++);
195 if (i == 3) {
196 arr_4[0] = (arr_3[0] & 0xfc) >> 2;
197 arr_4[1] = ((arr_3[0] & 0x03) << 4) +
198 ((arr_3[1] & 0xf0) >> 4);
199 arr_4[2] = ((arr_3[1] & 0x0f) << 2) +
200 ((arr_3[2] & 0xc0) >> 6);
201 arr_4[3] = arr_3[2] & 0x3f;
203 for (i = 0; i < 4; i++)
204 ret[ret_cnt++] = b64_data[arr_4[i]];
205 i = 0;
209 if (i) {
210 for (j = i; j < 3; j++)
211 arr_3[j] = '\0';
213 arr_4[0] = (arr_3[0] & 0xfc) >> 2;
214 arr_4[1] = ((arr_3[0] & 0x03) << 4) +
215 ((arr_3[1] & 0xf0) >> 4);
216 arr_4[2] = ((arr_3[1] & 0x0f) << 2) +
217 ((arr_3[2] & 0xc0) >> 6);
218 arr_4[3] = arr_3[2] & 0x3f;
220 for (j = 0; j < (i + 1); j++)
221 ret[ret_cnt++] = b64_data[arr_4[j]];
223 while (i++ < 3)
224 ret[ret_cnt++] = '=';
227 ret[ret_cnt++] = '\0';
228 return (ret);
232 * smb_base64_decode
234 * Decode using base64 algorithm.
235 * Caller should free the returned buffer when done.
237 static char *
238 smb_base64_decode(char *encoded_str)
240 int len = strlen(encoded_str);
241 int i = 0, j = 0;
242 int en_ind = 0;
243 char arr_4[4], arr_3[3];
244 int ret_cnt = 0;
245 char *ret = malloc(SMB_DEC_LEN);
246 char *p;
248 if (ret == NULL) {
249 return (NULL);
252 while (len-- && (encoded_str[en_ind] != '=') &&
253 smb_is_base64(encoded_str[en_ind])) {
254 arr_4[i++] = encoded_str[en_ind];
255 en_ind++;
256 if (i == 4) {
257 for (i = 0; i < 4; i++) {
258 if ((p = strchr(b64_data, arr_4[i])) == NULL)
259 return (NULL);
261 arr_4[i] = (int)(p - b64_data);
264 arr_3[0] = (arr_4[0] << 2) +
265 ((arr_4[1] & 0x30) >> 4);
266 arr_3[1] = ((arr_4[1] & 0xf) << 4) +
267 ((arr_4[2] & 0x3c) >> 2);
268 arr_3[2] = ((arr_4[2] & 0x3) << 6) +
269 arr_4[3];
271 for (i = 0; i < 3; i++)
272 ret[ret_cnt++] = arr_3[i];
274 i = 0;
278 if (i) {
279 for (j = i; j < 4; j++)
280 arr_4[j] = 0;
282 for (j = 0; j < 4; j++) {
283 if ((p = strchr(b64_data, arr_4[j])) == NULL)
284 return (NULL);
286 arr_4[j] = (int)(p - b64_data);
288 arr_3[0] = (arr_4[0] << 2) +
289 ((arr_4[1] & 0x30) >> 4);
290 arr_3[1] = ((arr_4[1] & 0xf) << 4) +
291 ((arr_4[2] & 0x3c) >> 2);
292 arr_3[2] = ((arr_4[2] & 0x3) << 6) +
293 arr_4[3];
294 for (j = 0; j < (i - 1); j++)
295 ret[ret_cnt++] = arr_3[j];
298 ret[ret_cnt++] = '\0';
299 return (ret);
302 static char *
303 smb_config_getenv_generic(char *name, char *svc_fmri_prefix, char *svc_propgrp)
305 smb_scfhandle_t *handle;
306 char *value;
308 if ((value = malloc(MAX_VALUE_BUFLEN * sizeof (char))) == NULL)
309 return (NULL);
311 handle = smb_smf_scf_init(svc_fmri_prefix);
312 if (handle == NULL) {
313 free(value);
314 return (NULL);
317 (void) smb_smf_create_service_pgroup(handle, svc_propgrp);
319 if (smb_smf_get_string_property(handle, name, value,
320 sizeof (char) * MAX_VALUE_BUFLEN) != 0) {
321 smb_smf_scf_fini(handle);
322 free(value);
323 return (NULL);
326 smb_smf_scf_fini(handle);
327 return (value);
331 static int
332 smb_config_setenv_generic(char *svc_fmri_prefix, char *svc_propgrp,
333 char *name, char *value)
335 smb_scfhandle_t *handle = NULL;
336 int rc = 0;
339 handle = smb_smf_scf_init(svc_fmri_prefix);
340 if (handle == NULL) {
341 return (1);
344 (void) smb_smf_create_service_pgroup(handle, svc_propgrp);
346 if (smb_smf_start_transaction(handle) != SMBD_SMF_OK) {
347 smb_smf_scf_fini(handle);
348 return (1);
351 if (smb_smf_set_string_property(handle, name, value) != SMBD_SMF_OK)
352 rc = 1;
354 if (smb_smf_end_transaction(handle) != SMBD_SMF_OK)
355 rc = 1;
357 smb_smf_scf_fini(handle);
358 return (rc);
362 * smb_config_getstr
364 * Fetch the specified string configuration item from SMF
367 smb_config_getstr(smb_cfg_id_t id, char *cbuf, int bufsz)
369 smb_scfhandle_t *handle;
370 smb_cfg_param_t *cfg;
371 int rc = SMBD_SMF_OK;
372 char *pg;
373 char protbuf[SMB_ENC_LEN];
374 char *tmp;
376 *cbuf = '\0';
377 cfg = smb_config_getent(id);
378 assert(cfg->sc_type == SCF_TYPE_ASTRING);
380 if (id == SMB_CI_DOMAIN_SRV)
381 return (smb_config_get_idmap_preferred_dc(cbuf, bufsz));
383 handle = smb_smf_scf_init(SMBD_FMRI_PREFIX);
384 if (handle == NULL)
385 return (SMBD_SMF_SYSTEM_ERR);
387 if (cfg->sc_flags & SMB_CF_PROTECTED) {
388 if ((rc = smb_smf_create_service_pgroup(handle,
389 SMBD_PROTECTED_PG_NAME)) != SMBD_SMF_OK)
390 goto error;
392 if ((rc = smb_smf_get_string_property(handle, cfg->sc_name,
393 protbuf, sizeof (protbuf))) != SMBD_SMF_OK)
394 goto error;
396 if (*protbuf != '\0') {
397 tmp = smb_base64_decode(protbuf);
398 (void) strlcpy(cbuf, tmp, bufsz);
399 free(tmp);
401 } else {
402 pg = (cfg->sc_flags & SMB_CF_EXEC) ? SMBD_EXEC_PG_NAME :
403 SMBD_PG_NAME;
404 rc = smb_smf_create_service_pgroup(handle, pg);
405 if (rc == SMBD_SMF_OK)
406 rc = smb_smf_get_string_property(handle, cfg->sc_name,
407 cbuf, bufsz);
410 error:
411 smb_smf_scf_fini(handle);
412 return (rc);
416 * Translate the value of an astring SMF property into a binary
417 * IP address. If the value is neither a valid IPv4 nor IPv6
418 * address, attempt to look it up as a hostname using the
419 * configured address type.
422 smb_config_getip(smb_cfg_id_t sc_id, smb_inaddr_t *ipaddr)
424 int rc, error;
425 int a_family;
426 char ipstr[MAXHOSTNAMELEN];
427 struct hostent *h;
428 smb_cfg_param_t *cfg;
430 if (ipaddr == NULL)
431 return (SMBD_SMF_INVALID_ARG);
433 bzero(ipaddr, sizeof (smb_inaddr_t));
434 rc = smb_config_getstr(sc_id, ipstr, sizeof (ipstr));
435 if (rc == SMBD_SMF_OK) {
436 if (*ipstr == '\0')
437 return (SMBD_SMF_INVALID_ARG);
439 if (inet_pton(AF_INET, ipstr, &ipaddr->a_ipv4) == 1) {
440 ipaddr->a_family = AF_INET;
441 return (SMBD_SMF_OK);
444 if (inet_pton(AF_INET6, ipstr, &ipaddr->a_ipv6) == 1) {
445 ipaddr->a_family = AF_INET6;
446 return (SMBD_SMF_OK);
450 * The value is neither an IPv4 nor IPv6 address;
451 * so check if it's a hostname.
453 a_family = smb_config_getbool(SMB_CI_IPV6_ENABLE) ?
454 AF_INET6 : AF_INET;
455 h = getipnodebyname(ipstr, a_family, AI_DEFAULT,
456 &error);
457 if (h != NULL) {
458 bcopy(*(h->h_addr_list), &ipaddr->a_ip,
459 h->h_length);
460 ipaddr->a_family = a_family;
461 freehostent(h);
462 rc = SMBD_SMF_OK;
463 } else {
464 cfg = smb_config_getent(sc_id);
465 syslog(LOG_ERR, "smbd/%s: %s unable to get %s "
466 "address: %d", cfg->sc_name, ipstr,
467 a_family == AF_INET ? "IPv4" : "IPv6", error);
468 rc = SMBD_SMF_INVALID_ARG;
472 return (rc);
476 * smb_config_getnum
478 * Returns the value of a numeric config param.
481 smb_config_getnum(smb_cfg_id_t id, int64_t *cint)
483 smb_scfhandle_t *handle;
484 smb_cfg_param_t *cfg;
485 int rc = SMBD_SMF_OK;
487 *cint = 0;
488 cfg = smb_config_getent(id);
489 assert(cfg->sc_type == SCF_TYPE_INTEGER);
491 handle = smb_smf_scf_init(SMBD_FMRI_PREFIX);
492 if (handle == NULL)
493 return (SMBD_SMF_SYSTEM_ERR);
495 rc = smb_smf_create_service_pgroup(handle, SMBD_PG_NAME);
496 if (rc == SMBD_SMF_OK)
497 rc = smb_smf_get_integer_property(handle, cfg->sc_name, cint);
498 smb_smf_scf_fini(handle);
500 return (rc);
504 * smb_config_getbool
506 * Returns the value of a boolean config param.
508 boolean_t
509 smb_config_getbool(smb_cfg_id_t id)
511 smb_scfhandle_t *handle;
512 smb_cfg_param_t *cfg;
513 int rc = SMBD_SMF_OK;
514 uint8_t vbool;
516 cfg = smb_config_getent(id);
517 assert(cfg->sc_type == SCF_TYPE_BOOLEAN);
519 handle = smb_smf_scf_init(SMBD_FMRI_PREFIX);
520 if (handle == NULL)
521 return (B_FALSE);
523 rc = smb_smf_create_service_pgroup(handle, SMBD_PG_NAME);
524 if (rc == SMBD_SMF_OK)
525 rc = smb_smf_get_boolean_property(handle, cfg->sc_name, &vbool);
526 smb_smf_scf_fini(handle);
528 return ((rc == SMBD_SMF_OK) ? (vbool == 1) : B_FALSE);
532 * smb_config_get
534 * This function returns the value of the requested config
535 * iterm regardless of its type in string format. This should
536 * be used when the config item type is not known by the caller.
539 smb_config_get(smb_cfg_id_t id, char *cbuf, int bufsz)
541 smb_cfg_param_t *cfg;
542 int64_t cint;
543 int rc;
545 cfg = smb_config_getent(id);
546 switch (cfg->sc_type) {
547 case SCF_TYPE_ASTRING:
548 return (smb_config_getstr(id, cbuf, bufsz));
550 case SCF_TYPE_INTEGER:
551 rc = smb_config_getnum(id, &cint);
552 if (rc == SMBD_SMF_OK)
553 (void) snprintf(cbuf, bufsz, "%lld", cint);
554 return (rc);
556 case SCF_TYPE_BOOLEAN:
557 if (smb_config_getbool(id))
558 (void) strlcpy(cbuf, "true", bufsz);
559 else
560 (void) strlcpy(cbuf, "false", bufsz);
561 return (SMBD_SMF_OK);
564 return (SMBD_SMF_INVALID_ARG);
568 * smb_config_setstr
570 * Set the specified config param with the given
571 * value.
574 smb_config_setstr(smb_cfg_id_t id, char *value)
576 smb_scfhandle_t *handle;
577 smb_cfg_param_t *cfg;
578 int rc = SMBD_SMF_OK;
579 boolean_t protected;
580 char *tmp = NULL;
581 char *pg;
583 cfg = smb_config_getent(id);
584 assert(cfg->sc_type == SCF_TYPE_ASTRING);
586 if (id == SMB_CI_DOMAIN_SRV)
587 return (smb_config_set_idmap_preferred_dc(value));
589 protected = B_FALSE;
591 switch (cfg->sc_flags) {
592 case SMB_CF_PROTECTED:
593 protected = B_TRUE;
594 pg = SMBD_PROTECTED_PG_NAME;
595 break;
596 case SMB_CF_EXEC:
597 pg = SMBD_EXEC_PG_NAME;
598 break;
599 default:
600 pg = SMBD_PG_NAME;
601 break;
604 handle = smb_smf_scf_init(SMBD_FMRI_PREFIX);
605 if (handle == NULL)
606 return (SMBD_SMF_SYSTEM_ERR);
608 rc = smb_smf_create_service_pgroup(handle, pg);
609 if (rc == SMBD_SMF_OK)
610 rc = smb_smf_start_transaction(handle);
612 if (rc != SMBD_SMF_OK) {
613 smb_smf_scf_fini(handle);
614 return (rc);
617 if (protected && value && (*value != '\0')) {
618 if ((tmp = smb_base64_encode(value)) == NULL) {
619 (void) smb_smf_end_transaction(handle);
620 smb_smf_scf_fini(handle);
621 return (SMBD_SMF_NO_MEMORY);
624 value = tmp;
627 rc = smb_smf_set_string_property(handle, cfg->sc_name, value);
629 free(tmp);
630 (void) smb_smf_end_transaction(handle);
631 smb_smf_scf_fini(handle);
632 return (rc);
636 * smb_config_setnum
638 * Sets a numeric configuration iterm
641 smb_config_setnum(smb_cfg_id_t id, int64_t value)
643 smb_scfhandle_t *handle;
644 smb_cfg_param_t *cfg;
645 int rc = SMBD_SMF_OK;
647 cfg = smb_config_getent(id);
648 assert(cfg->sc_type == SCF_TYPE_INTEGER);
650 handle = smb_smf_scf_init(SMBD_FMRI_PREFIX);
651 if (handle == NULL)
652 return (SMBD_SMF_SYSTEM_ERR);
654 rc = smb_smf_create_service_pgroup(handle, SMBD_PG_NAME);
655 if (rc == SMBD_SMF_OK)
656 rc = smb_smf_start_transaction(handle);
658 if (rc != SMBD_SMF_OK) {
659 smb_smf_scf_fini(handle);
660 return (rc);
663 rc = smb_smf_set_integer_property(handle, cfg->sc_name, value);
665 (void) smb_smf_end_transaction(handle);
666 smb_smf_scf_fini(handle);
667 return (rc);
671 * smb_config_setbool
673 * Sets a boolean configuration iterm
676 smb_config_setbool(smb_cfg_id_t id, boolean_t value)
678 smb_scfhandle_t *handle;
679 smb_cfg_param_t *cfg;
680 int rc = SMBD_SMF_OK;
682 cfg = smb_config_getent(id);
683 assert(cfg->sc_type == SCF_TYPE_BOOLEAN);
685 handle = smb_smf_scf_init(SMBD_FMRI_PREFIX);
686 if (handle == NULL)
687 return (SMBD_SMF_SYSTEM_ERR);
689 rc = smb_smf_create_service_pgroup(handle, SMBD_PG_NAME);
690 if (rc == SMBD_SMF_OK)
691 rc = smb_smf_start_transaction(handle);
693 if (rc != SMBD_SMF_OK) {
694 smb_smf_scf_fini(handle);
695 return (rc);
698 rc = smb_smf_set_boolean_property(handle, cfg->sc_name, value);
700 (void) smb_smf_end_transaction(handle);
701 smb_smf_scf_fini(handle);
702 return (rc);
706 * smb_config_set
708 * This function sets the value of the specified config
709 * iterm regardless of its type in string format. This should
710 * be used when the config item type is not known by the caller.
713 smb_config_set(smb_cfg_id_t id, char *value)
715 smb_cfg_param_t *cfg;
716 int64_t cint;
718 cfg = smb_config_getent(id);
719 switch (cfg->sc_type) {
720 case SCF_TYPE_ASTRING:
721 return (smb_config_setstr(id, value));
723 case SCF_TYPE_INTEGER:
724 cint = atoi(value);
725 return (smb_config_setnum(id, cint));
727 case SCF_TYPE_BOOLEAN:
728 return (smb_config_setbool(id, strcasecmp(value, "true") == 0));
731 return (SMBD_SMF_INVALID_ARG);
735 smb_config_get_debug()
737 int64_t val64;
738 int val = 0; /* default */
739 smb_scfhandle_t *handle = NULL;
741 handle = smb_smf_scf_init(SMBD_FMRI_PREFIX);
742 if (handle == NULL) {
743 return (val);
746 if (smb_smf_create_service_pgroup(handle,
747 SMBD_PG_NAME) != SMBD_SMF_OK) {
748 smb_smf_scf_fini(handle);
749 return (val);
752 if (smb_smf_get_integer_property(handle, "debug", &val64) != 0) {
753 smb_smf_scf_fini(handle);
754 return (val);
756 val = (int)val64;
758 smb_smf_scf_fini(handle);
760 return (val);
763 uint8_t
764 smb_config_get_fg_flag()
766 uint8_t run_fg = 0; /* Default is to run in daemon mode */
767 smb_scfhandle_t *handle = NULL;
769 handle = smb_smf_scf_init(SMBD_FMRI_PREFIX);
770 if (handle == NULL) {
771 return (run_fg);
774 if (smb_smf_create_service_pgroup(handle,
775 SMBD_PG_NAME) != SMBD_SMF_OK) {
776 smb_smf_scf_fini(handle);
777 return (run_fg);
780 if (smb_smf_get_boolean_property(handle, "run_fg", &run_fg) != 0) {
781 smb_smf_scf_fini(handle);
782 return (run_fg);
785 smb_smf_scf_fini(handle);
787 return (run_fg);
791 * smb_config_get_ads_enable
793 * Returns value of the "config/use_ads" parameter
794 * from the IDMAP SMF configuration repository.
797 boolean_t
798 smb_config_get_ads_enable(void)
800 smb_scfhandle_t *handle = NULL;
801 uint8_t vbool;
802 int rc = 0;
804 handle = smb_smf_scf_init(IDMAP_FMRI_PREFIX);
805 if (handle == NULL)
806 return (B_FALSE);
808 rc = smb_smf_create_service_pgroup(handle, IDMAP_PG_NAME);
809 if (rc == SMBD_SMF_OK)
810 rc = smb_smf_get_boolean_property(handle, "use_ads", &vbool);
811 smb_smf_scf_fini(handle);
813 return ((rc == SMBD_SMF_OK) ? (vbool == 1) : B_TRUE);
817 * smb_config_get_localsid
819 * Returns value of the "config/machine_sid" parameter
820 * from the IDMAP SMF configuration repository.
821 * Result is allocated; caller should free.
823 char *
824 smb_config_get_localsid(void)
826 return (smb_config_getenv_generic(MACHINE_SID, IDMAP_FMRI_PREFIX,
827 IDMAP_PG_NAME));
831 * smb_config_get_localuuid
833 * Returns value of the "config/machine_uuid" parameter
834 * from the IDMAP SMF configuration repository.
838 smb_config_get_localuuid(uuid_t uu)
840 char *s;
842 uuid_clear(uu);
843 s = smb_config_getenv_generic(MACHINE_UUID, IDMAP_FMRI_PREFIX,
844 IDMAP_PG_NAME);
845 if (s == NULL)
846 return (-1);
848 if (uuid_parse(s, uu) < 0) {
849 free(s);
850 return (-1);
853 return (0);
856 static int
857 smb_config_get_idmap_preferred_dc(char *cbuf, int bufsz)
859 char *s;
860 int len, rc = -1;
862 s = smb_config_getenv_generic(IDMAP_PREF_DC,
863 IDMAP_FMRI_PREFIX, IDMAP_PG_NAME);
864 if (s != NULL) {
865 len = strlcpy(cbuf, s, bufsz);
866 if (len < bufsz)
867 rc = 0;
868 free(s);
870 return (rc);
873 static int
874 smb_config_set_idmap_preferred_dc(char *value)
876 return (smb_config_setenv_generic(IDMAP_FMRI_PREFIX, IDMAP_PG_NAME,
877 IDMAP_PREF_DC, value));
881 * smb_config_set_idmap_domain
883 * Set the "config/domain_name" parameter from IDMAP SMF repository.
886 smb_config_set_idmap_domain(char *value)
888 return (smb_config_setenv_generic(IDMAP_FMRI_PREFIX, IDMAP_PG_NAME,
889 IDMAP_DOMAIN, value));
893 * smb_config_refresh_idmap
895 * Refresh IDMAP SMF service after making changes to its configuration.
898 smb_config_refresh_idmap(void)
900 char instance[32];
902 (void) snprintf(instance, sizeof (instance), "%s:default",
903 IDMAP_FMRI_PREFIX);
904 return (smf_refresh_instance(instance));
908 smb_config_secmode_fromstr(char *secmode)
910 if (secmode == NULL)
911 return (SMB_SECMODE_WORKGRP);
913 if (strcasecmp(secmode, SMB_SECMODE_DOMAIN_STR) == 0)
914 return (SMB_SECMODE_DOMAIN);
916 return (SMB_SECMODE_WORKGRP);
919 char *
920 smb_config_secmode_tostr(int secmode)
922 if (secmode == SMB_SECMODE_DOMAIN)
923 return (SMB_SECMODE_DOMAIN_STR);
925 return (SMB_SECMODE_WORKGRP_STR);
929 smb_config_get_secmode()
931 char p[16];
933 (void) smb_config_getstr(SMB_CI_SECURITY, p, sizeof (p));
934 return (smb_config_secmode_fromstr(p));
938 smb_config_set_secmode(int secmode)
940 char *p;
942 p = smb_config_secmode_tostr(secmode);
943 return (smb_config_setstr(SMB_CI_SECURITY, p));
946 void
947 smb_config_getdomaininfo(char *domain, char *fqdn, char *sid, char *forest,
948 char *guid)
950 if (domain)
951 (void) smb_config_getstr(SMB_CI_DOMAIN_NAME, domain,
952 NETBIOS_NAME_SZ);
954 if (fqdn)
955 (void) smb_config_getstr(SMB_CI_DOMAIN_FQDN, fqdn,
956 MAXHOSTNAMELEN);
958 if (sid)
959 (void) smb_config_getstr(SMB_CI_DOMAIN_SID, sid,
960 SMB_SID_STRSZ);
962 if (forest)
963 (void) smb_config_getstr(SMB_CI_DOMAIN_FOREST, forest,
964 MAXHOSTNAMELEN);
966 if (guid)
967 (void) smb_config_getstr(SMB_CI_DOMAIN_GUID, guid,
968 UUID_PRINTABLE_STRING_LENGTH);
971 void
972 smb_config_setdomaininfo(char *domain, char *fqdn, char *sid, char *forest,
973 char *guid)
975 if (domain)
976 (void) smb_config_setstr(SMB_CI_DOMAIN_NAME, domain);
977 if (fqdn)
978 (void) smb_config_setstr(SMB_CI_DOMAIN_FQDN, fqdn);
979 if (sid)
980 (void) smb_config_setstr(SMB_CI_DOMAIN_SID, sid);
981 if (forest)
982 (void) smb_config_setstr(SMB_CI_DOMAIN_FOREST, forest);
983 if (guid)
984 (void) smb_config_setstr(SMB_CI_DOMAIN_GUID, guid);
988 * The version stored in SMF in string format as N.N where
989 * N is a number defined by Microsoft. The first number represents
990 * the major version and the second number is the minor version.
991 * Current defined values can be found here in 'ver_table'.
993 * This function reads the SMF string value and converts it to
994 * two numbers returned in the given 'version' structure.
995 * Current default version number is 5.0 which is for Windows 2000.
997 void
998 smb_config_get_version(smb_version_t *version)
1000 smb_version_t tmpver;
1001 char verstr[SMB_VERSTR_LEN];
1002 char *p;
1003 int rc, i;
1004 static smb_version_t ver_table [] = {
1005 { 0, SMB_MAJOR_NT, SMB_MINOR_NT, 1381, 0 },
1006 { 0, SMB_MAJOR_2000, SMB_MINOR_2000, 2195, 0 },
1007 { 0, SMB_MAJOR_XP, SMB_MINOR_XP, 2196, 0 },
1008 { 0, SMB_MAJOR_2003, SMB_MINOR_2003, 2196, 0 },
1009 { 0, SMB_MAJOR_VISTA, SMB_MINOR_VISTA, 6000, 0 },
1010 { 0, SMB_MAJOR_2008, SMB_MINOR_2008, 6000, 0 },
1011 { 0, SMB_MAJOR_2008R2, SMB_MINOR_2008R2, 7007, 0 },
1012 { 0, SMB_MAJOR_7, SMB_MINOR_7, 7007, 0 }
1015 *version = ver_table[1];
1016 version->sv_size = sizeof (smb_version_t);
1018 rc = smb_config_getstr(SMB_CI_VERSION, verstr, sizeof (verstr));
1019 if (rc != SMBD_SMF_OK)
1020 return;
1022 if ((p = strchr(verstr, '.')) == NULL)
1023 return;
1025 *p = '\0';
1026 tmpver.sv_major = (uint8_t)atoi(verstr);
1027 tmpver.sv_minor = (uint8_t)atoi(p + 1);
1029 for (i = 0; i < sizeof (ver_table)/sizeof (ver_table[0]); ++i) {
1030 if ((tmpver.sv_major == ver_table[i].sv_major) &&
1031 (tmpver.sv_minor == ver_table[i].sv_minor)) {
1032 *version = ver_table[i];
1033 version->sv_size = sizeof (smb_version_t);
1034 break;
1040 * Reads share exec script properties
1042 uint32_t
1043 smb_config_get_execinfo(char *map, char *unmap, size_t bufsz)
1045 char buf[MAXPATHLEN];
1046 uint32_t flags = 0;
1048 if (map == NULL) {
1049 map = buf;
1050 bufsz = MAXPATHLEN;
1053 *map = '\0';
1054 (void) smb_config_getstr(SMB_CI_MAP, map, bufsz);
1055 if (*map != '\0')
1056 flags |= SMB_EXEC_MAP;
1058 if (unmap == NULL) {
1059 unmap = buf;
1060 bufsz = MAXPATHLEN;
1063 *unmap = '\0';
1064 (void) smb_config_getstr(SMB_CI_UNMAP, unmap, bufsz);
1065 if (*unmap != '\0')
1066 flags |= SMB_EXEC_UNMAP;
1068 *buf = '\0';
1069 (void) smb_config_getstr(SMB_CI_DISPOSITION, buf, sizeof (buf));
1070 if (*buf != '\0')
1071 if (strcasecmp(buf, SMB_EXEC_DISP_TERMINATE) == 0)
1072 flags |= SMB_EXEC_TERM;
1074 return (flags);
1077 static smb_cfg_param_t *
1078 smb_config_getent(smb_cfg_id_t id)
1080 int i;
1082 for (i = 0; i < SMB_CI_MAX; i++)
1083 if (smb_cfg_table[i].sc_id == id)
1084 return (&smb_cfg_table[id]);
1086 assert(0);
1087 return (NULL);
1092 * We store the max SMB protocol version in SMF as a string,
1093 * (for convenience of svccfg etc) but the programmatic get/set
1094 * interfaces use the numeric form.
1096 * The numeric values are as defined in the [MS-SMB2] spec.
1097 * except for how we represent "1" (for SMB1) which is an
1098 * arbitrary value below SMB2_VERS_BASE.
1100 static struct str_val
1101 smb_versions[] = {
1102 { "3.0", SMB_VERS_3_0 },
1103 { "2.1", SMB_VERS_2_1 },
1104 { "2.002", SMB_VERS_2_002 },
1105 { "1", SMB_VERS_1 },
1106 { NULL, 0 }
1110 * This really should be the latest (SMB_VERS_3_0)
1111 * but we're being cautious with SMB3 for a while.
1113 uint32_t max_protocol_default = SMB_VERS_2_1;
1115 uint32_t
1116 smb_config_get_max_protocol(void)
1118 char str[SMB_VERSTR_LEN];
1119 int i, rc;
1121 rc = smb_config_getstr(SMB_CI_MAX_PROTOCOL, str, sizeof (str));
1122 if (rc == SMBD_SMF_OK) {
1123 for (i = 0; smb_versions[i].str != NULL; i++) {
1124 if (strcmp(str, smb_versions[i].str) == 0)
1125 return (smb_versions[i].val);
1127 if (str[0] != '\0') {
1128 syslog(LOG_ERR, "smbd/max_protocol value invalid");
1132 return (max_protocol_default);
1136 smb_config_check_protocol(char *value)
1138 int i;
1140 for (i = 0; smb_versions[i].str != NULL; i++) {
1141 if (strcmp(value, smb_versions[i].str) == 0)
1142 return (0);
1145 return (-1);
1149 * If smb2_enable is present and max_protocol is empty,
1150 * set max_protocol. Delete smb2_enable.
1152 static void
1153 upgrade_smb2_enable()
1155 smb_scfhandle_t *handle;
1156 char *s2e_name = "smb2_enable";
1157 char *s2e_sval;
1158 uint8_t s2e_bval;
1159 char *maxp_name = "max_protocol";
1160 char *maxp_sval;
1161 char verstr[SMB_VERSTR_LEN];
1162 int rc;
1164 handle = smb_smf_scf_init(SMBD_FMRI_PREFIX);
1165 if (handle == NULL)
1166 return;
1167 rc = smb_smf_create_service_pgroup(handle, SMBD_PG_NAME);
1168 if (rc != SMBD_SMF_OK)
1169 goto out;
1171 /* Is there an "smb2_enable" property? */
1172 rc = smb_smf_get_boolean_property(handle, s2e_name, &s2e_bval);
1173 if (rc != SMBD_SMF_OK) {
1174 syslog(LOG_DEBUG, "upgrade: smb2_enable not found");
1175 goto out;
1179 * We will try to delete the smb2_enable property, so we need
1180 * the transaction to start now, before we modify max_protocol
1182 if ((rc = smb_smf_start_transaction(handle)) != 0) {
1183 syslog(LOG_DEBUG, "upgrade_smb2_enable: start trans (%d)", rc);
1184 goto out;
1188 * Old (smb2_enable) property exists.
1189 * Does the new one? (max_protocol)
1191 rc = smb_smf_get_string_property(handle, maxp_name,
1192 verstr, sizeof (verstr));
1193 if (rc == SMBD_SMF_OK && !smb_config_check_protocol(verstr)) {
1194 syslog(LOG_DEBUG, "upgrade: found %s = %s",
1195 maxp_name, verstr);
1196 /* Leave existing max_protocol as we found it. */
1197 } else {
1199 * New property missing or invalid.
1200 * Upgrade from "smb2_enable".
1202 if (s2e_bval == 0) {
1203 s2e_sval = "false";
1204 maxp_sval = "1";
1205 } else {
1206 s2e_sval = "true";
1207 maxp_sval = "2.1";
1210 * Note: Need this in the same transaction as the
1211 * delete of smb2_enable below.
1213 rc = smb_smf_set_string_property(handle, maxp_name, maxp_sval);
1214 if (rc != SMBD_SMF_OK) {
1215 syslog(LOG_ERR, "failed to set smbd/%d (%d)",
1216 maxp_name, rc);
1217 goto out;
1219 syslog(LOG_INFO, "upgrade smbd/smb2_enable=%s "
1220 "converted to smbd/max_protocol=%s",
1221 s2e_sval, maxp_sval);
1225 * Delete the old smb2_enable property.
1227 if ((rc = smb_smf_delete_property(handle, s2e_name)) != 0) {
1228 syslog(LOG_DEBUG, "upgrade_smb2_enable: delete prop (%d)", rc);
1229 } else if ((rc = smb_smf_end_transaction(handle)) != 0) {
1230 syslog(LOG_DEBUG, "upgrade_smb2_enable: end trans (%d)", rc);
1232 if (rc != 0) {
1233 syslog(LOG_ERR, "failed to delete property smbd/%d (%d)",
1234 s2e_name, rc);
1237 out:
1238 (void) smb_smf_end_transaction(handle);
1239 smb_smf_scf_fini(handle);
1244 * Run once at startup convert old SMF settings to current.
1246 void
1247 smb_config_upgrade(void)
1249 upgrade_smb2_enable();