xref: /titanic_41/usr/src/lib/smbsrv/libsmb/common/smb_cfg.c (revision 7206bf49b1fe641544165ee97f63856da95e0868)
1da6c28aaSamw /*
2da6c28aaSamw  * CDDL HEADER START
3da6c28aaSamw  *
4da6c28aaSamw  * The contents of this file are subject to the terms of the
5da6c28aaSamw  * Common Development and Distribution License (the "License").
6da6c28aaSamw  * You may not use this file except in compliance with the License.
7da6c28aaSamw  *
8da6c28aaSamw  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9da6c28aaSamw  * or http://www.opensolaris.org/os/licensing.
10da6c28aaSamw  * See the License for the specific language governing permissions
11da6c28aaSamw  * and limitations under the License.
12da6c28aaSamw  *
13da6c28aaSamw  * When distributing Covered Code, include this CDDL HEADER in each
14da6c28aaSamw  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15da6c28aaSamw  * If applicable, add the following below this CDDL HEADER, with the
16da6c28aaSamw  * fields enclosed by brackets "[]" replaced with your own identifying
17da6c28aaSamw  * information: Portions Copyright [yyyy] [name of copyright owner]
18da6c28aaSamw  *
19da6c28aaSamw  * CDDL HEADER END
20da6c28aaSamw  */
21da6c28aaSamw /*
22148c5f43SAlan Wright  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
234c0e320cSGordon Ross  * Copyright 2013 Nexenta Systems, Inc.  All rights reserved.
24da6c28aaSamw  */
25da6c28aaSamw 
26da6c28aaSamw /*
27da6c28aaSamw  * CIFS configuration management library
28da6c28aaSamw  */
29da6c28aaSamw 
30da6c28aaSamw #include <stdio.h>
31da6c28aaSamw #include <stdlib.h>
32da6c28aaSamw #include <unistd.h>
33da6c28aaSamw #include <synch.h>
34da6c28aaSamw #include <string.h>
35da6c28aaSamw #include <strings.h>
36da6c28aaSamw #include <syslog.h>
37da6c28aaSamw #include <netdb.h>
38da6c28aaSamw #include <ctype.h>
39da6c28aaSamw #include <sys/types.h>
40da6c28aaSamw #include <libscf.h>
41dc20a302Sas200622 #include <assert.h>
428d7e4166Sjose borrego #include <uuid/uuid.h>
43da6c28aaSamw #include <smbsrv/libsmb.h>
44da6c28aaSamw 
45da6c28aaSamw typedef struct smb_cfg_param {
46dc20a302Sas200622 	smb_cfg_id_t sc_id;
47da6c28aaSamw 	char *sc_name;
48da6c28aaSamw 	int sc_type;
49da6c28aaSamw 	uint32_t sc_flags;
50da6c28aaSamw } smb_cfg_param_t;
51da6c28aaSamw 
52da6c28aaSamw /*
53da6c28aaSamw  * config parameter flags
54da6c28aaSamw  */
55dc20a302Sas200622 #define	SMB_CF_PROTECTED	0x01
5629bd2886SAlan Wright #define	SMB_CF_EXEC		0x02
57da6c28aaSamw 
58da6c28aaSamw /* idmap SMF fmri and Property Group */
59da6c28aaSamw #define	IDMAP_FMRI_PREFIX		"system/idmap"
60da6c28aaSamw #define	MACHINE_SID			"machine_sid"
6155bf511dSas200622 #define	IDMAP_DOMAIN			"domain_name"
62da6c28aaSamw #define	IDMAP_PG_NAME			"config"
63da6c28aaSamw 
64da6c28aaSamw #define	SMB_SECMODE_WORKGRP_STR 	"workgroup"
65da6c28aaSamw #define	SMB_SECMODE_DOMAIN_STR  	"domain"
66da6c28aaSamw 
67da6c28aaSamw #define	SMB_ENC_LEN	1024
68da6c28aaSamw #define	SMB_DEC_LEN	256
69da6c28aaSamw 
70da6c28aaSamw static char *b64_data =
71da6c28aaSamw 	"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
72da6c28aaSamw 
73da6c28aaSamw static smb_cfg_param_t smb_cfg_table[] =
74da6c28aaSamw {
759fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	{SMB_CI_VERSION, "sv_version", SCF_TYPE_ASTRING, 0},
769fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 
77da6c28aaSamw 	/* Oplock configuration, Kernel Only */
78dc20a302Sas200622 	{SMB_CI_OPLOCK_ENABLE, "oplock_enable", SCF_TYPE_BOOLEAN, 0},
79da6c28aaSamw 
80da6c28aaSamw 	/* Autohome configuration */
81dc20a302Sas200622 	{SMB_CI_AUTOHOME_MAP, "autohome_map", SCF_TYPE_ASTRING, 0},
82da6c28aaSamw 
83da6c28aaSamw 	/* Domain/PDC configuration */
84dc20a302Sas200622 	{SMB_CI_DOMAIN_SID, "domain_sid", SCF_TYPE_ASTRING, 0},
85dc20a302Sas200622 	{SMB_CI_DOMAIN_MEMB, "domain_member", SCF_TYPE_BOOLEAN, 0},
86dc20a302Sas200622 	{SMB_CI_DOMAIN_NAME, "domain_name", SCF_TYPE_ASTRING, 0},
878d7e4166Sjose borrego 	{SMB_CI_DOMAIN_FQDN, "fqdn", SCF_TYPE_ASTRING, 0},
888d7e4166Sjose borrego 	{SMB_CI_DOMAIN_FOREST, "forest", SCF_TYPE_ASTRING, 0},
898d7e4166Sjose borrego 	{SMB_CI_DOMAIN_GUID, "domain_guid", SCF_TYPE_ASTRING, 0},
90dc20a302Sas200622 	{SMB_CI_DOMAIN_SRV, "pdc", SCF_TYPE_ASTRING, 0},
91da6c28aaSamw 
92da6c28aaSamw 	/* WINS configuration */
93dc20a302Sas200622 	{SMB_CI_WINS_SRV1, "wins_server_1", SCF_TYPE_ASTRING, 0},
94dc20a302Sas200622 	{SMB_CI_WINS_SRV2, "wins_server_2", SCF_TYPE_ASTRING, 0},
95dc20a302Sas200622 	{SMB_CI_WINS_EXCL, "wins_exclude", SCF_TYPE_ASTRING, 0},
96da6c28aaSamw 
97da6c28aaSamw 	/* Kmod specific configuration */
98dc20a302Sas200622 	{SMB_CI_MAX_WORKERS, "max_workers", SCF_TYPE_INTEGER, 0},
99dc20a302Sas200622 	{SMB_CI_MAX_CONNECTIONS, "max_connections", SCF_TYPE_INTEGER, 0},
100dc20a302Sas200622 	{SMB_CI_KEEPALIVE, "keep_alive", SCF_TYPE_INTEGER, 0},
101dc20a302Sas200622 	{SMB_CI_RESTRICT_ANON, "restrict_anonymous", SCF_TYPE_BOOLEAN, 0},
102da6c28aaSamw 
103dc20a302Sas200622 	{SMB_CI_SIGNING_ENABLE, "signing_enabled", SCF_TYPE_BOOLEAN, 0},
104dc20a302Sas200622 	{SMB_CI_SIGNING_REQD, "signing_required", SCF_TYPE_BOOLEAN, 0},
105da6c28aaSamw 
106da6c28aaSamw 	/* Kmod tuning configuration */
107dc20a302Sas200622 	{SMB_CI_SYNC_ENABLE, "sync_enable", SCF_TYPE_BOOLEAN, 0},
108da6c28aaSamw 
109da6c28aaSamw 	/* SMBd configuration */
110dc20a302Sas200622 	{SMB_CI_SECURITY, "security", SCF_TYPE_ASTRING, 0},
1114c0e320cSGordon Ross 	{SMB_CI_NETBIOS_ENABLE, "netbios_enable", SCF_TYPE_BOOLEAN, 0},
112dc20a302Sas200622 	{SMB_CI_NBSCOPE, "netbios_scope", SCF_TYPE_ASTRING, 0},
113dc20a302Sas200622 	{SMB_CI_SYS_CMNT, "system_comment", SCF_TYPE_ASTRING, 0},
114dc20a302Sas200622 	{SMB_CI_LM_LEVEL, "lmauth_level", SCF_TYPE_INTEGER, 0},
115da6c28aaSamw 
116da6c28aaSamw 	/* ADS Configuration */
117dc20a302Sas200622 	{SMB_CI_ADS_SITE, "ads_site", SCF_TYPE_ASTRING, 0},
118da6c28aaSamw 
119da6c28aaSamw 	/* Dynamic DNS */
120dc20a302Sas200622 	{SMB_CI_DYNDNS_ENABLE, "ddns_enable", SCF_TYPE_BOOLEAN, 0},
121da6c28aaSamw 
122dc20a302Sas200622 	{SMB_CI_MACHINE_PASSWD, "machine_passwd", SCF_TYPE_ASTRING,
123faa1795aSjb150015 	    SMB_CF_PROTECTED},
124faa1795aSjb150015 	{SMB_CI_KPASSWD_SRV, "kpasswd_server", SCF_TYPE_ASTRING,
125faa1795aSjb150015 	    0},
126faa1795aSjb150015 	{SMB_CI_KPASSWD_DOMAIN, "kpasswd_domain", SCF_TYPE_ASTRING,
127faa1795aSjb150015 	    0},
128faa1795aSjb150015 	{SMB_CI_KPASSWD_SEQNUM, "kpasswd_seqnum", SCF_TYPE_INTEGER,
129faa1795aSjb150015 	    0},
130faa1795aSjb150015 	{SMB_CI_NETLOGON_SEQNUM, "netlogon_seqnum", SCF_TYPE_INTEGER,
1317f667e74Sjose borrego 	    0},
13229bd2886SAlan Wright 	{SMB_CI_IPV6_ENABLE, "ipv6_enable", SCF_TYPE_BOOLEAN, 0},
133cb174861Sjoyce mcintosh 	{SMB_CI_PRINT_ENABLE, "print_enable", SCF_TYPE_BOOLEAN, 0},
13429bd2886SAlan Wright 	{SMB_CI_MAP, "map", SCF_TYPE_ASTRING, SMB_CF_EXEC},
13529bd2886SAlan Wright 	{SMB_CI_UNMAP, "unmap", SCF_TYPE_ASTRING, SMB_CF_EXEC},
136148c5f43SAlan Wright 	{SMB_CI_DISPOSITION, "disposition", SCF_TYPE_ASTRING, SMB_CF_EXEC},
1375f1ef25cSAram Hăvărneanu 	{SMB_CI_DFS_STDROOT_NUM, "dfs_stdroot_num", SCF_TYPE_INTEGER, 0},
138*7206bf49SGordon Ross 	{SMB_CI_TRAVERSE_MOUNTS, "traverse_mounts", SCF_TYPE_BOOLEAN, 0},
139da6c28aaSamw 	/* SMB_CI_MAX */
140da6c28aaSamw };
141da6c28aaSamw 
142dc20a302Sas200622 static smb_cfg_param_t *smb_config_getent(smb_cfg_id_t);
143dc20a302Sas200622 
144da6c28aaSamw static boolean_t smb_is_base64(unsigned char c);
145da6c28aaSamw static char *smb_base64_encode(char *str_to_encode);
146da6c28aaSamw static char *smb_base64_decode(char *encoded_str);
147dc20a302Sas200622 
148dc20a302Sas200622 char *
smb_config_getname(smb_cfg_id_t id)149dc20a302Sas200622 smb_config_getname(smb_cfg_id_t id)
150dc20a302Sas200622 {
151dc20a302Sas200622 	smb_cfg_param_t *cfg;
152dc20a302Sas200622 	cfg = smb_config_getent(id);
153dc20a302Sas200622 	return (cfg->sc_name);
154dc20a302Sas200622 }
155da6c28aaSamw 
156da6c28aaSamw static boolean_t
smb_is_base64(unsigned char c)157da6c28aaSamw smb_is_base64(unsigned char c)
158da6c28aaSamw {
159da6c28aaSamw 	return (isalnum(c) || (c == '+') || (c == '/'));
160da6c28aaSamw }
161da6c28aaSamw 
162da6c28aaSamw /*
163da6c28aaSamw  * smb_base64_encode
164da6c28aaSamw  *
165da6c28aaSamw  * Encode a string using base64 algorithm.
166da6c28aaSamw  * Caller should free the returned buffer when done.
167da6c28aaSamw  */
168da6c28aaSamw static char *
smb_base64_encode(char * str_to_encode)169da6c28aaSamw smb_base64_encode(char *str_to_encode)
170da6c28aaSamw {
171da6c28aaSamw 	int ret_cnt = 0;
172da6c28aaSamw 	int i = 0, j = 0;
173da6c28aaSamw 	char arr_3[3], arr_4[4];
174da6c28aaSamw 	int len = strlen(str_to_encode);
175da6c28aaSamw 	char *ret = malloc(SMB_ENC_LEN);
176da6c28aaSamw 
177da6c28aaSamw 	if (ret == NULL) {
178da6c28aaSamw 		return (NULL);
179da6c28aaSamw 	}
180da6c28aaSamw 
181da6c28aaSamw 	while (len--) {
182da6c28aaSamw 		arr_3[i++] = *(str_to_encode++);
183da6c28aaSamw 		if (i == 3) {
184da6c28aaSamw 			arr_4[0] = (arr_3[0] & 0xfc) >> 2;
185da6c28aaSamw 			arr_4[1] = ((arr_3[0] & 0x03) << 4) +
186da6c28aaSamw 			    ((arr_3[1] & 0xf0) >> 4);
187da6c28aaSamw 			arr_4[2] = ((arr_3[1] & 0x0f) << 2) +
188da6c28aaSamw 			    ((arr_3[2] & 0xc0) >> 6);
189da6c28aaSamw 			arr_4[3] = arr_3[2] & 0x3f;
190da6c28aaSamw 
191da6c28aaSamw 			for (i = 0; i < 4; i++)
192da6c28aaSamw 				ret[ret_cnt++] = b64_data[arr_4[i]];
193da6c28aaSamw 			i = 0;
194da6c28aaSamw 		}
195da6c28aaSamw 	}
196da6c28aaSamw 
197da6c28aaSamw 	if (i) {
198da6c28aaSamw 		for (j = i; j < 3; j++)
199da6c28aaSamw 			arr_3[j] = '\0';
200da6c28aaSamw 
201da6c28aaSamw 		arr_4[0] = (arr_3[0] & 0xfc) >> 2;
202da6c28aaSamw 		arr_4[1] = ((arr_3[0] & 0x03) << 4) +
203da6c28aaSamw 		    ((arr_3[1] & 0xf0) >> 4);
204da6c28aaSamw 		arr_4[2] = ((arr_3[1] & 0x0f) << 2) +
205da6c28aaSamw 		    ((arr_3[2] & 0xc0) >> 6);
206da6c28aaSamw 		arr_4[3] = arr_3[2] & 0x3f;
207da6c28aaSamw 
208da6c28aaSamw 		for (j = 0; j < (i + 1); j++)
209da6c28aaSamw 			ret[ret_cnt++] = b64_data[arr_4[j]];
210da6c28aaSamw 
211da6c28aaSamw 		while (i++ < 3)
212da6c28aaSamw 			ret[ret_cnt++] = '=';
213da6c28aaSamw 	}
214da6c28aaSamw 
215da6c28aaSamw 	ret[ret_cnt++] = '\0';
216da6c28aaSamw 	return (ret);
217da6c28aaSamw }
218da6c28aaSamw 
219da6c28aaSamw /*
220da6c28aaSamw  * smb_base64_decode
221da6c28aaSamw  *
222da6c28aaSamw  * Decode using base64 algorithm.
223da6c28aaSamw  * Caller should free the returned buffer when done.
224da6c28aaSamw  */
225da6c28aaSamw static char *
smb_base64_decode(char * encoded_str)226da6c28aaSamw smb_base64_decode(char *encoded_str)
227da6c28aaSamw {
228da6c28aaSamw 	int len = strlen(encoded_str);
229da6c28aaSamw 	int i = 0, j = 0;
230da6c28aaSamw 	int en_ind = 0;
231da6c28aaSamw 	char arr_4[4], arr_3[3];
232da6c28aaSamw 	int ret_cnt = 0;
233da6c28aaSamw 	char *ret = malloc(SMB_DEC_LEN);
234da6c28aaSamw 	char *p;
235da6c28aaSamw 
236da6c28aaSamw 	if (ret == NULL) {
237da6c28aaSamw 		return (NULL);
238da6c28aaSamw 	}
239da6c28aaSamw 
240da6c28aaSamw 	while (len-- && (encoded_str[en_ind] != '=') &&
241da6c28aaSamw 	    smb_is_base64(encoded_str[en_ind])) {
242da6c28aaSamw 		arr_4[i++] = encoded_str[en_ind];
243da6c28aaSamw 		en_ind++;
244da6c28aaSamw 		if (i == 4) {
245da6c28aaSamw 			for (i = 0; i < 4; i++) {
246da6c28aaSamw 				if ((p = strchr(b64_data, arr_4[i])) == NULL)
247da6c28aaSamw 					return (NULL);
248da6c28aaSamw 
249da6c28aaSamw 				arr_4[i] = (int)(p - b64_data);
250da6c28aaSamw 			}
251da6c28aaSamw 
252da6c28aaSamw 			arr_3[0] = (arr_4[0] << 2) +
253da6c28aaSamw 			    ((arr_4[1] & 0x30) >> 4);
254da6c28aaSamw 			arr_3[1] = ((arr_4[1] & 0xf) << 4) +
255da6c28aaSamw 			    ((arr_4[2] & 0x3c) >> 2);
256da6c28aaSamw 			arr_3[2] = ((arr_4[2] & 0x3) << 6) +
257da6c28aaSamw 			    arr_4[3];
258da6c28aaSamw 
259da6c28aaSamw 			for (i = 0; i < 3; i++)
260da6c28aaSamw 				ret[ret_cnt++] = arr_3[i];
261da6c28aaSamw 
262da6c28aaSamw 			i = 0;
263da6c28aaSamw 		}
264da6c28aaSamw 	}
265da6c28aaSamw 
266da6c28aaSamw 	if (i) {
267da6c28aaSamw 		for (j = i; j < 4; j++)
268da6c28aaSamw 			arr_4[j] = 0;
269da6c28aaSamw 
270da6c28aaSamw 		for (j = 0; j < 4; j++) {
271da6c28aaSamw 			if ((p = strchr(b64_data, arr_4[j])) == NULL)
272da6c28aaSamw 				return (NULL);
273da6c28aaSamw 
274da6c28aaSamw 			arr_4[j] = (int)(p - b64_data);
275da6c28aaSamw 		}
276da6c28aaSamw 		arr_3[0] = (arr_4[0] << 2) +
277da6c28aaSamw 		    ((arr_4[1] & 0x30) >> 4);
278da6c28aaSamw 		arr_3[1] = ((arr_4[1] & 0xf) << 4) +
279da6c28aaSamw 		    ((arr_4[2] & 0x3c) >> 2);
280da6c28aaSamw 		arr_3[2] = ((arr_4[2] & 0x3) << 6) +
281da6c28aaSamw 		    arr_4[3];
282da6c28aaSamw 		for (j = 0; j < (i - 1); j++)
283da6c28aaSamw 			ret[ret_cnt++] = arr_3[j];
284da6c28aaSamw 	}
285da6c28aaSamw 
286da6c28aaSamw 	ret[ret_cnt++] = '\0';
287da6c28aaSamw 	return (ret);
288da6c28aaSamw }
289da6c28aaSamw 
290da6c28aaSamw static char *
smb_config_getenv_generic(char * name,char * svc_fmri_prefix,char * svc_propgrp)291da6c28aaSamw smb_config_getenv_generic(char *name, char *svc_fmri_prefix, char *svc_propgrp)
292da6c28aaSamw {
293da6c28aaSamw 	smb_scfhandle_t *handle;
294da6c28aaSamw 	char *value;
295da6c28aaSamw 
296da6c28aaSamw 	if ((value = malloc(MAX_VALUE_BUFLEN * sizeof (char))) == NULL)
297da6c28aaSamw 		return (NULL);
298da6c28aaSamw 
299da6c28aaSamw 	handle = smb_smf_scf_init(svc_fmri_prefix);
300da6c28aaSamw 	if (handle == NULL) {
301da6c28aaSamw 		free(value);
302da6c28aaSamw 		return (NULL);
303da6c28aaSamw 	}
304da6c28aaSamw 
305da6c28aaSamw 	(void) smb_smf_create_service_pgroup(handle, svc_propgrp);
306da6c28aaSamw 
307da6c28aaSamw 	if (smb_smf_get_string_property(handle, name, value,
308da6c28aaSamw 	    sizeof (char) * MAX_VALUE_BUFLEN) != 0) {
309da6c28aaSamw 		smb_smf_scf_fini(handle);
310da6c28aaSamw 		free(value);
311da6c28aaSamw 		return (NULL);
312da6c28aaSamw 	}
313da6c28aaSamw 
314da6c28aaSamw 	smb_smf_scf_fini(handle);
315da6c28aaSamw 	return (value);
316da6c28aaSamw 
317da6c28aaSamw }
318da6c28aaSamw 
319dc20a302Sas200622 static int
smb_config_setenv_generic(char * svc_fmri_prefix,char * svc_propgrp,char * name,char * value)320da6c28aaSamw smb_config_setenv_generic(char *svc_fmri_prefix, char *svc_propgrp,
321da6c28aaSamw     char *name, char *value)
322da6c28aaSamw {
323da6c28aaSamw 	smb_scfhandle_t *handle = NULL;
324da6c28aaSamw 	int rc = 0;
325da6c28aaSamw 
326da6c28aaSamw 
327da6c28aaSamw 	handle = smb_smf_scf_init(svc_fmri_prefix);
328da6c28aaSamw 	if (handle == NULL) {
329da6c28aaSamw 		return (1);
330da6c28aaSamw 	}
331da6c28aaSamw 
332da6c28aaSamw 	(void) smb_smf_create_service_pgroup(handle, svc_propgrp);
333da6c28aaSamw 
334da6c28aaSamw 	if (smb_smf_start_transaction(handle) != SMBD_SMF_OK) {
335da6c28aaSamw 		smb_smf_scf_fini(handle);
336da6c28aaSamw 		return (1);
337da6c28aaSamw 	}
338da6c28aaSamw 
339da6c28aaSamw 	if (smb_smf_set_string_property(handle, name, value) != SMBD_SMF_OK)
340da6c28aaSamw 		rc = 1;
341da6c28aaSamw 
342da6c28aaSamw 	if (smb_smf_end_transaction(handle) != SMBD_SMF_OK)
343da6c28aaSamw 		rc = 1;
344da6c28aaSamw 
345da6c28aaSamw 	smb_smf_scf_fini(handle);
346da6c28aaSamw 	return (rc);
347da6c28aaSamw }
348da6c28aaSamw 
349da6c28aaSamw /*
350da6c28aaSamw  * smb_config_getstr
351da6c28aaSamw  *
352dc20a302Sas200622  * Fetch the specified string configuration item from SMF
353da6c28aaSamw  */
354dc20a302Sas200622 int
smb_config_getstr(smb_cfg_id_t id,char * cbuf,int bufsz)355dc20a302Sas200622 smb_config_getstr(smb_cfg_id_t id, char *cbuf, int bufsz)
356da6c28aaSamw {
357dc20a302Sas200622 	smb_scfhandle_t *handle;
358da6c28aaSamw 	smb_cfg_param_t *cfg;
359dc20a302Sas200622 	int rc = SMBD_SMF_OK;
36029bd2886SAlan Wright 	char *pg;
36196a62adaSjoyce mcintosh 	char protbuf[SMB_ENC_LEN];
36296a62adaSjoyce mcintosh 	char *tmp;
363da6c28aaSamw 
364dc20a302Sas200622 	*cbuf = '\0';
365dc20a302Sas200622 	cfg = smb_config_getent(id);
366dc20a302Sas200622 	assert(cfg->sc_type == SCF_TYPE_ASTRING);
367dc20a302Sas200622 
368dc20a302Sas200622 	handle = smb_smf_scf_init(SMBD_FMRI_PREFIX);
369dc20a302Sas200622 	if (handle == NULL)
370dc20a302Sas200622 		return (SMBD_SMF_SYSTEM_ERR);
371dc20a302Sas200622 
372dc20a302Sas200622 	if (cfg->sc_flags & SMB_CF_PROTECTED) {
373dc20a302Sas200622 		if ((rc = smb_smf_create_service_pgroup(handle,
374dc20a302Sas200622 		    SMBD_PROTECTED_PG_NAME)) != SMBD_SMF_OK)
375dc20a302Sas200622 			goto error;
376dc20a302Sas200622 
377dc20a302Sas200622 		if ((rc = smb_smf_get_string_property(handle, cfg->sc_name,
378dc20a302Sas200622 		    protbuf, sizeof (protbuf))) != SMBD_SMF_OK)
379dc20a302Sas200622 			goto error;
380dc20a302Sas200622 
381dc20a302Sas200622 		if (*protbuf != '\0') {
382dc20a302Sas200622 			tmp = smb_base64_decode(protbuf);
383dc20a302Sas200622 			(void) strlcpy(cbuf, tmp, bufsz);
384dc20a302Sas200622 			free(tmp);
385dc20a302Sas200622 		}
386dc20a302Sas200622 	} else {
38729bd2886SAlan Wright 		pg = (cfg->sc_flags & SMB_CF_EXEC) ? SMBD_EXEC_PG_NAME :
38829bd2886SAlan Wright 		    SMBD_PG_NAME;
38929bd2886SAlan Wright 		rc = smb_smf_create_service_pgroup(handle, pg);
390dc20a302Sas200622 		if (rc == SMBD_SMF_OK)
391dc20a302Sas200622 			rc = smb_smf_get_string_property(handle, cfg->sc_name,
392dc20a302Sas200622 			    cbuf, bufsz);
393da6c28aaSamw 	}
394da6c28aaSamw 
395dc20a302Sas200622 error:
396dc20a302Sas200622 	smb_smf_scf_fini(handle);
397dc20a302Sas200622 	return (rc);
398da6c28aaSamw }
399da6c28aaSamw 
40096a62adaSjoyce mcintosh /*
40196a62adaSjoyce mcintosh  * Translate the value of an astring SMF property into a binary
40296a62adaSjoyce mcintosh  * IP address. If the value is neither a valid IPv4 nor IPv6
40396a62adaSjoyce mcintosh  * address, attempt to look it up as a hostname using the
40496a62adaSjoyce mcintosh  * configured address type.
40596a62adaSjoyce mcintosh  */
4067f667e74Sjose borrego int
smb_config_getip(smb_cfg_id_t sc_id,smb_inaddr_t * ipaddr)4077f667e74Sjose borrego smb_config_getip(smb_cfg_id_t sc_id, smb_inaddr_t *ipaddr)
4087f667e74Sjose borrego {
40996a62adaSjoyce mcintosh 	int rc, error;
41096a62adaSjoyce mcintosh 	int a_family;
41196a62adaSjoyce mcintosh 	char ipstr[MAXHOSTNAMELEN];
41296a62adaSjoyce mcintosh 	struct hostent *h;
41396a62adaSjoyce mcintosh 	smb_cfg_param_t *cfg;
4147f667e74Sjose borrego 
41529bd2886SAlan Wright 	if (ipaddr == NULL)
41629bd2886SAlan Wright 		return (SMBD_SMF_INVALID_ARG);
41729bd2886SAlan Wright 
41829bd2886SAlan Wright 	bzero(ipaddr, sizeof (smb_inaddr_t));
4197f667e74Sjose borrego 	rc = smb_config_getstr(sc_id, ipstr, sizeof (ipstr));
4207f667e74Sjose borrego 	if (rc == SMBD_SMF_OK) {
42196a62adaSjoyce mcintosh 		if (*ipstr == '\0')
42296a62adaSjoyce mcintosh 			return (SMBD_SMF_INVALID_ARG);
42396a62adaSjoyce mcintosh 
42429bd2886SAlan Wright 		if (inet_pton(AF_INET, ipstr, &ipaddr->a_ipv4) == 1) {
42529bd2886SAlan Wright 			ipaddr->a_family = AF_INET;
42629bd2886SAlan Wright 			return (SMBD_SMF_OK);
42729bd2886SAlan Wright 		}
42829bd2886SAlan Wright 
42929bd2886SAlan Wright 		if (inet_pton(AF_INET6, ipstr, &ipaddr->a_ipv6) == 1) {
43029bd2886SAlan Wright 			ipaddr->a_family = AF_INET6;
43196a62adaSjoyce mcintosh 			return (SMBD_SMF_OK);
43296a62adaSjoyce mcintosh 		}
43396a62adaSjoyce mcintosh 
43496a62adaSjoyce mcintosh 		/*
43596a62adaSjoyce mcintosh 		 * The value is neither an IPv4 nor IPv6 address;
43696a62adaSjoyce mcintosh 		 * so check if it's a hostname.
43796a62adaSjoyce mcintosh 		 */
43896a62adaSjoyce mcintosh 		a_family = smb_config_getbool(SMB_CI_IPV6_ENABLE) ?
43996a62adaSjoyce mcintosh 		    AF_INET6 : AF_INET;
44096a62adaSjoyce mcintosh 		h = getipnodebyname(ipstr, a_family, AI_DEFAULT,
44196a62adaSjoyce mcintosh 		    &error);
44296a62adaSjoyce mcintosh 		if (h != NULL) {
44396a62adaSjoyce mcintosh 			bcopy(*(h->h_addr_list), &ipaddr->a_ip,
44496a62adaSjoyce mcintosh 			    h->h_length);
44596a62adaSjoyce mcintosh 			ipaddr->a_family = a_family;
44696a62adaSjoyce mcintosh 			freehostent(h);
44729bd2886SAlan Wright 			rc = SMBD_SMF_OK;
44829bd2886SAlan Wright 		} else {
44996a62adaSjoyce mcintosh 			cfg = smb_config_getent(sc_id);
45096a62adaSjoyce mcintosh 			syslog(LOG_ERR, "smbd/%s: %s unable to get %s "
45196a62adaSjoyce mcintosh 			    "address: %d", cfg->sc_name, ipstr,
45296a62adaSjoyce mcintosh 			    a_family == AF_INET ?  "IPv4" : "IPv6", error);
45329bd2886SAlan Wright 			rc = SMBD_SMF_INVALID_ARG;
4547f667e74Sjose borrego 		}
4557f667e74Sjose borrego 	}
45629bd2886SAlan Wright 
4577f667e74Sjose borrego 	return (rc);
4587f667e74Sjose borrego }
4597f667e74Sjose borrego 
460da6c28aaSamw /*
461da6c28aaSamw  * smb_config_getnum
462da6c28aaSamw  *
463da6c28aaSamw  * Returns the value of a numeric config param.
464da6c28aaSamw  */
465da6c28aaSamw int
smb_config_getnum(smb_cfg_id_t id,int64_t * cint)466dc20a302Sas200622 smb_config_getnum(smb_cfg_id_t id, int64_t *cint)
467da6c28aaSamw {
468dc20a302Sas200622 	smb_scfhandle_t *handle;
469da6c28aaSamw 	smb_cfg_param_t *cfg;
470dc20a302Sas200622 	int rc = SMBD_SMF_OK;
471da6c28aaSamw 
472dc20a302Sas200622 	*cint = 0;
473dc20a302Sas200622 	cfg = smb_config_getent(id);
474dc20a302Sas200622 	assert(cfg->sc_type == SCF_TYPE_INTEGER);
475dc20a302Sas200622 
476dc20a302Sas200622 	handle = smb_smf_scf_init(SMBD_FMRI_PREFIX);
477dc20a302Sas200622 	if (handle == NULL)
478dc20a302Sas200622 		return (SMBD_SMF_SYSTEM_ERR);
479dc20a302Sas200622 
480dc20a302Sas200622 	rc = smb_smf_create_service_pgroup(handle, SMBD_PG_NAME);
481dc20a302Sas200622 	if (rc == SMBD_SMF_OK)
482dc20a302Sas200622 		rc = smb_smf_get_integer_property(handle, cfg->sc_name, cint);
483dc20a302Sas200622 	smb_smf_scf_fini(handle);
484dc20a302Sas200622 
485da6c28aaSamw 	return (rc);
486da6c28aaSamw }
487da6c28aaSamw 
488dc20a302Sas200622 /*
489dc20a302Sas200622  * smb_config_getbool
490dc20a302Sas200622  *
491dc20a302Sas200622  * Returns the value of a boolean config param.
492dc20a302Sas200622  */
493dc20a302Sas200622 boolean_t
smb_config_getbool(smb_cfg_id_t id)494dc20a302Sas200622 smb_config_getbool(smb_cfg_id_t id)
495dc20a302Sas200622 {
496dc20a302Sas200622 	smb_scfhandle_t *handle;
497dc20a302Sas200622 	smb_cfg_param_t *cfg;
498dc20a302Sas200622 	int rc = SMBD_SMF_OK;
499dc20a302Sas200622 	uint8_t vbool;
500dc20a302Sas200622 
501dc20a302Sas200622 	cfg = smb_config_getent(id);
502dc20a302Sas200622 	assert(cfg->sc_type == SCF_TYPE_BOOLEAN);
503dc20a302Sas200622 
504dc20a302Sas200622 	handle = smb_smf_scf_init(SMBD_FMRI_PREFIX);
505dc20a302Sas200622 	if (handle == NULL)
506dc20a302Sas200622 		return (B_FALSE);
507dc20a302Sas200622 
508dc20a302Sas200622 	rc = smb_smf_create_service_pgroup(handle, SMBD_PG_NAME);
509dc20a302Sas200622 	if (rc == SMBD_SMF_OK)
510dc20a302Sas200622 		rc = smb_smf_get_boolean_property(handle, cfg->sc_name, &vbool);
511dc20a302Sas200622 	smb_smf_scf_fini(handle);
512dc20a302Sas200622 
513dc20a302Sas200622 	return ((rc == SMBD_SMF_OK) ? (vbool == 1) : B_FALSE);
514dc20a302Sas200622 }
515dc20a302Sas200622 
516dc20a302Sas200622 /*
517dc20a302Sas200622  * smb_config_get
518dc20a302Sas200622  *
519dc20a302Sas200622  * This function returns the value of the requested config
520dc20a302Sas200622  * iterm regardless of its type in string format. This should
521dc20a302Sas200622  * be used when the config item type is not known by the caller.
522dc20a302Sas200622  */
523dc20a302Sas200622 int
smb_config_get(smb_cfg_id_t id,char * cbuf,int bufsz)524dc20a302Sas200622 smb_config_get(smb_cfg_id_t id, char *cbuf, int bufsz)
525dc20a302Sas200622 {
526dc20a302Sas200622 	smb_cfg_param_t *cfg;
527dc20a302Sas200622 	int64_t cint;
528dc20a302Sas200622 	int rc;
529dc20a302Sas200622 
530dc20a302Sas200622 	cfg = smb_config_getent(id);
531dc20a302Sas200622 	switch (cfg->sc_type) {
532dc20a302Sas200622 	case SCF_TYPE_ASTRING:
533dc20a302Sas200622 		return (smb_config_getstr(id, cbuf, bufsz));
534dc20a302Sas200622 
535dc20a302Sas200622 	case SCF_TYPE_INTEGER:
536dc20a302Sas200622 		rc = smb_config_getnum(id, &cint);
537dc20a302Sas200622 		if (rc == SMBD_SMF_OK)
538dc20a302Sas200622 			(void) snprintf(cbuf, bufsz, "%lld", cint);
539dc20a302Sas200622 		return (rc);
540dc20a302Sas200622 
541dc20a302Sas200622 	case SCF_TYPE_BOOLEAN:
542dc20a302Sas200622 		if (smb_config_getbool(id))
543dc20a302Sas200622 			(void) strlcpy(cbuf, "true", bufsz);
544dc20a302Sas200622 		else
545dc20a302Sas200622 			(void) strlcpy(cbuf, "false", bufsz);
546dc20a302Sas200622 		return (SMBD_SMF_OK);
547dc20a302Sas200622 	}
548dc20a302Sas200622 
549dc20a302Sas200622 	return (SMBD_SMF_INVALID_ARG);
550dc20a302Sas200622 }
551dc20a302Sas200622 
552dc20a302Sas200622 /*
553dc20a302Sas200622  * smb_config_setstr
554dc20a302Sas200622  *
555dc20a302Sas200622  * Set the specified config param with the given
556dc20a302Sas200622  * value.
557dc20a302Sas200622  */
558dc20a302Sas200622 int
smb_config_setstr(smb_cfg_id_t id,char * value)559dc20a302Sas200622 smb_config_setstr(smb_cfg_id_t id, char *value)
560dc20a302Sas200622 {
561dc20a302Sas200622 	smb_scfhandle_t *handle;
562dc20a302Sas200622 	smb_cfg_param_t *cfg;
563dc20a302Sas200622 	int rc = SMBD_SMF_OK;
564dc20a302Sas200622 	boolean_t protected;
565dc20a302Sas200622 	char *tmp = NULL;
566dc20a302Sas200622 	char *pg;
567dc20a302Sas200622 
568dc20a302Sas200622 	cfg = smb_config_getent(id);
569dc20a302Sas200622 	assert(cfg->sc_type == SCF_TYPE_ASTRING);
570dc20a302Sas200622 
571dc20a302Sas200622 	protected = B_FALSE;
57229bd2886SAlan Wright 
57329bd2886SAlan Wright 	switch (cfg->sc_flags) {
57429bd2886SAlan Wright 	case SMB_CF_PROTECTED:
57529bd2886SAlan Wright 		protected = B_TRUE;
57629bd2886SAlan Wright 		pg = SMBD_PROTECTED_PG_NAME;
57729bd2886SAlan Wright 		break;
57829bd2886SAlan Wright 	case SMB_CF_EXEC:
57929bd2886SAlan Wright 		pg = SMBD_EXEC_PG_NAME;
58029bd2886SAlan Wright 		break;
58129bd2886SAlan Wright 	default:
58229bd2886SAlan Wright 		pg = SMBD_PG_NAME;
58329bd2886SAlan Wright 		break;
584dc20a302Sas200622 	}
585dc20a302Sas200622 
586dc20a302Sas200622 	handle = smb_smf_scf_init(SMBD_FMRI_PREFIX);
587dc20a302Sas200622 	if (handle == NULL)
588dc20a302Sas200622 		return (SMBD_SMF_SYSTEM_ERR);
589dc20a302Sas200622 
590dc20a302Sas200622 	rc = smb_smf_create_service_pgroup(handle, pg);
591dc20a302Sas200622 	if (rc == SMBD_SMF_OK)
592dc20a302Sas200622 		rc = smb_smf_start_transaction(handle);
593dc20a302Sas200622 
594dc20a302Sas200622 	if (rc != SMBD_SMF_OK) {
595dc20a302Sas200622 		smb_smf_scf_fini(handle);
596dc20a302Sas200622 		return (rc);
597dc20a302Sas200622 	}
598dc20a302Sas200622 
599dc20a302Sas200622 	if (protected && value && (*value != '\0')) {
600dc20a302Sas200622 		if ((tmp = smb_base64_encode(value)) == NULL) {
601dc20a302Sas200622 			(void) smb_smf_end_transaction(handle);
602dc20a302Sas200622 			smb_smf_scf_fini(handle);
603dc20a302Sas200622 			return (SMBD_SMF_NO_MEMORY);
604dc20a302Sas200622 		}
605dc20a302Sas200622 
606dc20a302Sas200622 		value = tmp;
607dc20a302Sas200622 	}
608dc20a302Sas200622 
609dc20a302Sas200622 	rc = smb_smf_set_string_property(handle, cfg->sc_name, value);
610dc20a302Sas200622 
611dc20a302Sas200622 	free(tmp);
612dc20a302Sas200622 	(void) smb_smf_end_transaction(handle);
613dc20a302Sas200622 	smb_smf_scf_fini(handle);
614dc20a302Sas200622 	return (rc);
615da6c28aaSamw }
616da6c28aaSamw 
617da6c28aaSamw /*
618da6c28aaSamw  * smb_config_setnum
619da6c28aaSamw  *
620dc20a302Sas200622  * Sets a numeric configuration iterm
621da6c28aaSamw  */
622da6c28aaSamw int
smb_config_setnum(smb_cfg_id_t id,int64_t value)623dc20a302Sas200622 smb_config_setnum(smb_cfg_id_t id, int64_t value)
624da6c28aaSamw {
625dc20a302Sas200622 	smb_scfhandle_t *handle;
626da6c28aaSamw 	smb_cfg_param_t *cfg;
627dc20a302Sas200622 	int rc = SMBD_SMF_OK;
628da6c28aaSamw 
629dc20a302Sas200622 	cfg = smb_config_getent(id);
630dc20a302Sas200622 	assert(cfg->sc_type == SCF_TYPE_INTEGER);
631da6c28aaSamw 
632da6c28aaSamw 	handle = smb_smf_scf_init(SMBD_FMRI_PREFIX);
633dc20a302Sas200622 	if (handle == NULL)
634dc20a302Sas200622 		return (SMBD_SMF_SYSTEM_ERR);
635da6c28aaSamw 
636dc20a302Sas200622 	rc = smb_smf_create_service_pgroup(handle, SMBD_PG_NAME);
637dc20a302Sas200622 	if (rc == SMBD_SMF_OK)
638dc20a302Sas200622 		rc = smb_smf_start_transaction(handle);
639da6c28aaSamw 
640dc20a302Sas200622 	if (rc != SMBD_SMF_OK) {
641dc20a302Sas200622 		smb_smf_scf_fini(handle);
642da6c28aaSamw 		return (rc);
643da6c28aaSamw 	}
644da6c28aaSamw 
645dc20a302Sas200622 	rc = smb_smf_set_integer_property(handle, cfg->sc_name, value);
646dc20a302Sas200622 
647dc20a302Sas200622 	(void) smb_smf_end_transaction(handle);
648dc20a302Sas200622 	smb_smf_scf_fini(handle);
649dc20a302Sas200622 	return (rc);
650dc20a302Sas200622 }
651dc20a302Sas200622 
652dc20a302Sas200622 /*
653dc20a302Sas200622  * smb_config_setbool
654dc20a302Sas200622  *
655dc20a302Sas200622  * Sets a boolean configuration iterm
656dc20a302Sas200622  */
657dc20a302Sas200622 int
smb_config_setbool(smb_cfg_id_t id,boolean_t value)658dc20a302Sas200622 smb_config_setbool(smb_cfg_id_t id, boolean_t value)
659dc20a302Sas200622 {
660dc20a302Sas200622 	smb_scfhandle_t *handle;
661dc20a302Sas200622 	smb_cfg_param_t *cfg;
662dc20a302Sas200622 	int rc = SMBD_SMF_OK;
663dc20a302Sas200622 
664dc20a302Sas200622 	cfg = smb_config_getent(id);
665dc20a302Sas200622 	assert(cfg->sc_type == SCF_TYPE_BOOLEAN);
666dc20a302Sas200622 
667dc20a302Sas200622 	handle = smb_smf_scf_init(SMBD_FMRI_PREFIX);
668dc20a302Sas200622 	if (handle == NULL)
669dc20a302Sas200622 		return (SMBD_SMF_SYSTEM_ERR);
670dc20a302Sas200622 
671dc20a302Sas200622 	rc = smb_smf_create_service_pgroup(handle, SMBD_PG_NAME);
672dc20a302Sas200622 	if (rc == SMBD_SMF_OK)
673dc20a302Sas200622 		rc = smb_smf_start_transaction(handle);
674dc20a302Sas200622 
675dc20a302Sas200622 	if (rc != SMBD_SMF_OK) {
676dc20a302Sas200622 		smb_smf_scf_fini(handle);
677dc20a302Sas200622 		return (rc);
678dc20a302Sas200622 	}
679dc20a302Sas200622 
680dc20a302Sas200622 	rc = smb_smf_set_boolean_property(handle, cfg->sc_name, value);
681dc20a302Sas200622 
682dc20a302Sas200622 	(void) smb_smf_end_transaction(handle);
683dc20a302Sas200622 	smb_smf_scf_fini(handle);
684dc20a302Sas200622 	return (rc);
685dc20a302Sas200622 }
686dc20a302Sas200622 
687dc20a302Sas200622 /*
688dc20a302Sas200622  * smb_config_set
689dc20a302Sas200622  *
690dc20a302Sas200622  * This function sets the value of the specified config
691dc20a302Sas200622  * iterm regardless of its type in string format. This should
692dc20a302Sas200622  * be used when the config item type is not known by the caller.
693dc20a302Sas200622  */
694dc20a302Sas200622 int
smb_config_set(smb_cfg_id_t id,char * value)695dc20a302Sas200622 smb_config_set(smb_cfg_id_t id, char *value)
696dc20a302Sas200622 {
697dc20a302Sas200622 	smb_cfg_param_t *cfg;
698dc20a302Sas200622 	int64_t cint;
699dc20a302Sas200622 
700dc20a302Sas200622 	cfg = smb_config_getent(id);
701dc20a302Sas200622 	switch (cfg->sc_type) {
702dc20a302Sas200622 	case SCF_TYPE_ASTRING:
703dc20a302Sas200622 		return (smb_config_setstr(id, value));
704dc20a302Sas200622 
705dc20a302Sas200622 	case SCF_TYPE_INTEGER:
706dc20a302Sas200622 		cint = atoi(value);
707dc20a302Sas200622 		return (smb_config_setnum(id, cint));
708dc20a302Sas200622 
709dc20a302Sas200622 	case SCF_TYPE_BOOLEAN:
710dc20a302Sas200622 		return (smb_config_setbool(id, strcasecmp(value, "true") == 0));
711dc20a302Sas200622 	}
712dc20a302Sas200622 
713dc20a302Sas200622 	return (SMBD_SMF_INVALID_ARG);
714dc20a302Sas200622 }
715*7206bf49SGordon Ross 
716*7206bf49SGordon Ross int
smb_config_get_debug()717*7206bf49SGordon Ross smb_config_get_debug()
718*7206bf49SGordon Ross {
719*7206bf49SGordon Ross 	int64_t val64;
720*7206bf49SGordon Ross 	int val = 0;	/* default */
721*7206bf49SGordon Ross 	smb_scfhandle_t *handle = NULL;
722*7206bf49SGordon Ross 
723*7206bf49SGordon Ross 	handle = smb_smf_scf_init(SMBD_FMRI_PREFIX);
724*7206bf49SGordon Ross 	if (handle == NULL) {
725*7206bf49SGordon Ross 		return (val);
726*7206bf49SGordon Ross 	}
727*7206bf49SGordon Ross 
728*7206bf49SGordon Ross 	if (smb_smf_create_service_pgroup(handle,
729*7206bf49SGordon Ross 	    SMBD_PG_NAME) != SMBD_SMF_OK) {
730*7206bf49SGordon Ross 		smb_smf_scf_fini(handle);
731*7206bf49SGordon Ross 		return (val);
732*7206bf49SGordon Ross 	}
733*7206bf49SGordon Ross 
734*7206bf49SGordon Ross 	if (smb_smf_get_integer_property(handle, "debug", &val64) != 0) {
735*7206bf49SGordon Ross 		smb_smf_scf_fini(handle);
736*7206bf49SGordon Ross 		return (val);
737*7206bf49SGordon Ross 	}
738*7206bf49SGordon Ross 	val = (int)val64;
739*7206bf49SGordon Ross 
740*7206bf49SGordon Ross 	smb_smf_scf_fini(handle);
741*7206bf49SGordon Ross 
742*7206bf49SGordon Ross 	return (val);
743*7206bf49SGordon Ross }
744*7206bf49SGordon Ross 
745da6c28aaSamw uint8_t
smb_config_get_fg_flag()746da6c28aaSamw smb_config_get_fg_flag()
747da6c28aaSamw {
748da6c28aaSamw 	uint8_t run_fg = 0; /* Default is to run in daemon mode */
749da6c28aaSamw 	smb_scfhandle_t *handle = NULL;
750da6c28aaSamw 
751da6c28aaSamw 	handle = smb_smf_scf_init(SMBD_FMRI_PREFIX);
752da6c28aaSamw 	if (handle == NULL) {
753da6c28aaSamw 		return (run_fg);
754da6c28aaSamw 	}
755da6c28aaSamw 
756da6c28aaSamw 	if (smb_smf_create_service_pgroup(handle,
757da6c28aaSamw 	    SMBD_PG_NAME) != SMBD_SMF_OK) {
758da6c28aaSamw 		smb_smf_scf_fini(handle);
759da6c28aaSamw 		return (run_fg);
760da6c28aaSamw 	}
761da6c28aaSamw 
762da6c28aaSamw 	if (smb_smf_get_boolean_property(handle, "run_fg", &run_fg) != 0) {
763da6c28aaSamw 		smb_smf_scf_fini(handle);
764da6c28aaSamw 		return (run_fg);
765da6c28aaSamw 	}
766da6c28aaSamw 
767da6c28aaSamw 	smb_smf_scf_fini(handle);
768da6c28aaSamw 
769da6c28aaSamw 	return (run_fg);
770da6c28aaSamw }
771da6c28aaSamw 
772da6c28aaSamw /*
7734f449947SGordon Ross  * smb_config_get_ads_enable
7744f449947SGordon Ross  *
7754f449947SGordon Ross  * Returns value of the "config/use_ads" parameter
7764f449947SGordon Ross  * from the IDMAP SMF configuration repository.
7774f449947SGordon Ross  *
7784f449947SGordon Ross  */
7794f449947SGordon Ross boolean_t
smb_config_get_ads_enable(void)7804f449947SGordon Ross smb_config_get_ads_enable(void)
7814f449947SGordon Ross {
7824f449947SGordon Ross 	smb_scfhandle_t *handle = NULL;
7834f449947SGordon Ross 	uint8_t vbool;
7844f449947SGordon Ross 	int rc = 0;
7854f449947SGordon Ross 
7864f449947SGordon Ross 	handle = smb_smf_scf_init(IDMAP_FMRI_PREFIX);
7874f449947SGordon Ross 	if (handle == NULL)
7884f449947SGordon Ross 		return (B_FALSE);
7894f449947SGordon Ross 
7904f449947SGordon Ross 	rc = smb_smf_create_service_pgroup(handle, IDMAP_PG_NAME);
7914f449947SGordon Ross 	if (rc == SMBD_SMF_OK)
7924f449947SGordon Ross 		rc = smb_smf_get_boolean_property(handle, "use_ads", &vbool);
7934f449947SGordon Ross 	smb_smf_scf_fini(handle);
7944f449947SGordon Ross 
7954f449947SGordon Ross 	return ((rc == SMBD_SMF_OK) ? (vbool == 1) : B_FALSE);
7964f449947SGordon Ross }
7974f449947SGordon Ross 
7984f449947SGordon Ross /*
799da6c28aaSamw  * smb_config_get_localsid
800da6c28aaSamw  *
801da6c28aaSamw  * Returns value of the "config/machine_sid" parameter
802da6c28aaSamw  * from the IDMAP SMF configuration repository.
803da6c28aaSamw  *
804da6c28aaSamw  */
805da6c28aaSamw char *
smb_config_get_localsid(void)806da6c28aaSamw smb_config_get_localsid(void)
807da6c28aaSamw {
808da6c28aaSamw 	return (smb_config_getenv_generic(MACHINE_SID, IDMAP_FMRI_PREFIX,
809da6c28aaSamw 	    IDMAP_PG_NAME));
810da6c28aaSamw }
811da6c28aaSamw 
812da6c28aaSamw /*
813da6c28aaSamw  * smb_config_set_idmap_domain
814da6c28aaSamw  *
81555bf511dSas200622  * Set the "config/domain_name" parameter from IDMAP SMF repository.
816da6c28aaSamw  */
817da6c28aaSamw int
smb_config_set_idmap_domain(char * value)818da6c28aaSamw smb_config_set_idmap_domain(char *value)
819da6c28aaSamw {
820da6c28aaSamw 	return (smb_config_setenv_generic(IDMAP_FMRI_PREFIX, IDMAP_PG_NAME,
82155bf511dSas200622 	    IDMAP_DOMAIN, value));
822da6c28aaSamw }
823da6c28aaSamw 
824da6c28aaSamw /*
825da6c28aaSamw  * smb_config_refresh_idmap
826da6c28aaSamw  *
827da6c28aaSamw  * Refresh IDMAP SMF service after making changes to its configuration.
828da6c28aaSamw  */
829da6c28aaSamw int
smb_config_refresh_idmap(void)830da6c28aaSamw smb_config_refresh_idmap(void)
831da6c28aaSamw {
832da6c28aaSamw 	char instance[32];
833da6c28aaSamw 
834da6c28aaSamw 	(void) snprintf(instance, sizeof (instance), "%s:default",
835da6c28aaSamw 	    IDMAP_FMRI_PREFIX);
836da6c28aaSamw 	return (smf_refresh_instance(instance));
837da6c28aaSamw }
838da6c28aaSamw 
839da6c28aaSamw int
smb_config_secmode_fromstr(char * secmode)840da6c28aaSamw smb_config_secmode_fromstr(char *secmode)
841da6c28aaSamw {
842da6c28aaSamw 	if (secmode == NULL)
843da6c28aaSamw 		return (SMB_SECMODE_WORKGRP);
844da6c28aaSamw 
845da6c28aaSamw 	if (strcasecmp(secmode, SMB_SECMODE_DOMAIN_STR) == 0)
846da6c28aaSamw 		return (SMB_SECMODE_DOMAIN);
847da6c28aaSamw 
848da6c28aaSamw 	return (SMB_SECMODE_WORKGRP);
849da6c28aaSamw }
850da6c28aaSamw 
851da6c28aaSamw char *
smb_config_secmode_tostr(int secmode)852da6c28aaSamw smb_config_secmode_tostr(int secmode)
853da6c28aaSamw {
854da6c28aaSamw 	if (secmode == SMB_SECMODE_DOMAIN)
855da6c28aaSamw 		return (SMB_SECMODE_DOMAIN_STR);
856da6c28aaSamw 
857da6c28aaSamw 	return (SMB_SECMODE_WORKGRP_STR);
858da6c28aaSamw }
859da6c28aaSamw 
860da6c28aaSamw int
smb_config_get_secmode()861da6c28aaSamw smb_config_get_secmode()
862da6c28aaSamw {
863dc20a302Sas200622 	char p[16];
864da6c28aaSamw 
865dc20a302Sas200622 	(void) smb_config_getstr(SMB_CI_SECURITY, p, sizeof (p));
866da6c28aaSamw 	return (smb_config_secmode_fromstr(p));
867da6c28aaSamw }
868da6c28aaSamw 
869da6c28aaSamw int
smb_config_set_secmode(int secmode)870da6c28aaSamw smb_config_set_secmode(int secmode)
871da6c28aaSamw {
872da6c28aaSamw 	char *p;
873da6c28aaSamw 
874da6c28aaSamw 	p = smb_config_secmode_tostr(secmode);
875dc20a302Sas200622 	return (smb_config_setstr(SMB_CI_SECURITY, p));
876dc20a302Sas200622 }
877dc20a302Sas200622 
8788d7e4166Sjose borrego void
smb_config_getdomaininfo(char * domain,char * fqdn,char * sid,char * forest,char * guid)87929bd2886SAlan Wright smb_config_getdomaininfo(char *domain, char *fqdn, char *sid, char *forest,
88029bd2886SAlan Wright     char *guid)
8818d7e4166Sjose borrego {
88229bd2886SAlan Wright 	if (domain)
88329bd2886SAlan Wright 		(void) smb_config_getstr(SMB_CI_DOMAIN_NAME, domain,
88429bd2886SAlan Wright 		    NETBIOS_NAME_SZ);
88529bd2886SAlan Wright 
88629bd2886SAlan Wright 	if (fqdn)
88729bd2886SAlan Wright 		(void) smb_config_getstr(SMB_CI_DOMAIN_FQDN, fqdn,
88829bd2886SAlan Wright 		    MAXHOSTNAMELEN);
88929bd2886SAlan Wright 
89029bd2886SAlan Wright 	if (sid)
89129bd2886SAlan Wright 		(void) smb_config_getstr(SMB_CI_DOMAIN_SID, sid,
89229bd2886SAlan Wright 		    SMB_SID_STRSZ);
89329bd2886SAlan Wright 
89429bd2886SAlan Wright 	if (forest)
89529bd2886SAlan Wright 		(void) smb_config_getstr(SMB_CI_DOMAIN_FOREST, forest,
89629bd2886SAlan Wright 		    MAXHOSTNAMELEN);
89729bd2886SAlan Wright 
89829bd2886SAlan Wright 	if (guid)
8998d7e4166Sjose borrego 		(void) smb_config_getstr(SMB_CI_DOMAIN_GUID, guid,
9008d7e4166Sjose borrego 		    UUID_PRINTABLE_STRING_LENGTH);
9018d7e4166Sjose borrego }
9028d7e4166Sjose borrego 
9038d7e4166Sjose borrego void
smb_config_setdomaininfo(char * domain,char * fqdn,char * sid,char * forest,char * guid)90429bd2886SAlan Wright smb_config_setdomaininfo(char *domain, char *fqdn, char *sid, char *forest,
90529bd2886SAlan Wright     char *guid)
9068d7e4166Sjose borrego {
90729bd2886SAlan Wright 	if (domain)
9088d7e4166Sjose borrego 		(void) smb_config_setstr(SMB_CI_DOMAIN_NAME, domain);
90929bd2886SAlan Wright 	if (fqdn)
9108d7e4166Sjose borrego 		(void) smb_config_setstr(SMB_CI_DOMAIN_FQDN, fqdn);
91129bd2886SAlan Wright 	if (sid)
91229bd2886SAlan Wright 		(void) smb_config_setstr(SMB_CI_DOMAIN_SID, sid);
91329bd2886SAlan Wright 	if (forest)
9148d7e4166Sjose borrego 		(void) smb_config_setstr(SMB_CI_DOMAIN_FOREST, forest);
91529bd2886SAlan Wright 	if (guid)
9168d7e4166Sjose borrego 		(void) smb_config_setstr(SMB_CI_DOMAIN_GUID, guid);
9178d7e4166Sjose borrego }
9189fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 
9199fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States /*
9209fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States  * The version stored in SMF in string format as N.N where
9219fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States  * N is a number defined by Microsoft. The first number represents
9229fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States  * the major version and the second number is the minor version.
9239fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States  * Current defined values can be found here in 'ver_table'.
9249fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States  *
9259fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States  * This function reads the SMF string value and converts it to
9269fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States  * two numbers returned in the given 'version' structure.
9279fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States  * Current default version number is 5.0 which is for Windows 2000.
9289fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States  */
9299fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States void
smb_config_get_version(smb_version_t * version)9309fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States smb_config_get_version(smb_version_t *version)
9319fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States {
9329fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	smb_version_t tmpver;
9339fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	char verstr[SMB_VERSTR_LEN];
9349fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	char *p;
9359fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	int rc, i;
9369fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	static smb_version_t ver_table [] = {
937fd9ee8b5Sjoyce mcintosh 		{ 0, SMB_MAJOR_NT,	SMB_MINOR_NT,		1381,	0 },
938fd9ee8b5Sjoyce mcintosh 		{ 0, SMB_MAJOR_2000,	SMB_MINOR_2000,		2195,	0 },
939fd9ee8b5Sjoyce mcintosh 		{ 0, SMB_MAJOR_XP,	SMB_MINOR_XP,		2196,	0 },
940fd9ee8b5Sjoyce mcintosh 		{ 0, SMB_MAJOR_2003,	SMB_MINOR_2003,		2196,	0 },
941fd9ee8b5Sjoyce mcintosh 		{ 0, SMB_MAJOR_VISTA,	SMB_MINOR_VISTA,	6000,	0 },
942fd9ee8b5Sjoyce mcintosh 		{ 0, SMB_MAJOR_2008,	SMB_MINOR_2008,		6000,	0 },
943fd9ee8b5Sjoyce mcintosh 		{ 0, SMB_MAJOR_2008R2,	SMB_MINOR_2008R2,	7007,	0 },
944fd9ee8b5Sjoyce mcintosh 		{ 0, SMB_MAJOR_7,	SMB_MINOR_7,		7007,	0 }
9459fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	};
9469fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 
9479fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	*version = ver_table[1];
948fd9ee8b5Sjoyce mcintosh 	version->sv_size = sizeof (smb_version_t);
9499fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 
9509fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	rc = smb_config_getstr(SMB_CI_VERSION, verstr, sizeof (verstr));
9519fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	if (rc != SMBD_SMF_OK)
9529fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 		return;
9539fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 
9549fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	if ((p = strchr(verstr, '.')) == NULL)
9559fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 		return;
9569fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 
9579fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	*p = '\0';
9589fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	tmpver.sv_major = (uint8_t)atoi(verstr);
9599fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	tmpver.sv_minor = (uint8_t)atoi(p + 1);
9609fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 
9619fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	for (i = 0; i < sizeof (ver_table)/sizeof (ver_table[0]); ++i) {
9629fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 		if ((tmpver.sv_major == ver_table[i].sv_major) &&
9639fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 		    (tmpver.sv_minor == ver_table[i].sv_minor)) {
9649fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 			*version = ver_table[i];
965fd9ee8b5Sjoyce mcintosh 			version->sv_size = sizeof (smb_version_t);
9669fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 			break;
9679fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 		}
9689fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	}
9699fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States }
9709fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 
971148c5f43SAlan Wright /*
972148c5f43SAlan Wright  * Reads share exec script properties
973148c5f43SAlan Wright  */
974148c5f43SAlan Wright uint32_t
smb_config_get_execinfo(char * map,char * unmap,size_t bufsz)975148c5f43SAlan Wright smb_config_get_execinfo(char *map, char *unmap, size_t bufsz)
976148c5f43SAlan Wright {
977148c5f43SAlan Wright 	char buf[MAXPATHLEN];
978148c5f43SAlan Wright 	uint32_t flags = 0;
979148c5f43SAlan Wright 
980148c5f43SAlan Wright 	if (map == NULL) {
981148c5f43SAlan Wright 		map = buf;
982148c5f43SAlan Wright 		bufsz = MAXPATHLEN;
983148c5f43SAlan Wright 	}
984148c5f43SAlan Wright 
985148c5f43SAlan Wright 	*map = '\0';
986148c5f43SAlan Wright 	(void) smb_config_getstr(SMB_CI_MAP, map, bufsz);
987148c5f43SAlan Wright 	if (*map != '\0')
988148c5f43SAlan Wright 		flags |= SMB_EXEC_MAP;
989148c5f43SAlan Wright 
990148c5f43SAlan Wright 	if (unmap == NULL) {
991148c5f43SAlan Wright 		unmap = buf;
992148c5f43SAlan Wright 		bufsz = MAXPATHLEN;
993148c5f43SAlan Wright 	}
994148c5f43SAlan Wright 
995148c5f43SAlan Wright 	*unmap = '\0';
996148c5f43SAlan Wright 	(void) smb_config_getstr(SMB_CI_UNMAP, unmap, bufsz);
997148c5f43SAlan Wright 	if (*unmap != '\0')
998148c5f43SAlan Wright 		flags |= SMB_EXEC_UNMAP;
999148c5f43SAlan Wright 
1000148c5f43SAlan Wright 	*buf = '\0';
1001148c5f43SAlan Wright 	(void) smb_config_getstr(SMB_CI_DISPOSITION, buf, sizeof (buf));
1002148c5f43SAlan Wright 	if (*buf != '\0')
1003148c5f43SAlan Wright 		if (strcasecmp(buf, SMB_EXEC_DISP_TERMINATE) == 0)
1004148c5f43SAlan Wright 			flags |= SMB_EXEC_TERM;
1005148c5f43SAlan Wright 
1006148c5f43SAlan Wright 	return (flags);
1007148c5f43SAlan Wright }
1008148c5f43SAlan Wright 
10099fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States static smb_cfg_param_t *
smb_config_getent(smb_cfg_id_t id)10109fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States smb_config_getent(smb_cfg_id_t id)
10119fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States {
10129fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	int i;
10139fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 
10149fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	for (i = 0; i < SMB_CI_MAX; i++)
10159fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 		if (smb_cfg_table[i].sc_id == id)
10169fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 			return (&smb_cfg_table[id]);
10179fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 
10189fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	assert(0);
10199fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	return (NULL);
10209fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States }
1021