xref: /illumos-gate/usr/src/lib/libshare/smb/libshare_smb.c (revision e4fb8a5fd5bb5a5d316430c6ac8f6d33704fcf5d)
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 /*
23  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Copyright 2017 Nexenta Systems, Inc.  All rights reserved.
25  */
26 
27 /*
28  * SMB specific functions
29  */
30 #include <stdio.h>
31 #include <string.h>
32 #include <ctype.h>
33 #include <stdlib.h>
34 #include <unistd.h>
35 #include <zone.h>
36 #include <errno.h>
37 #include <locale.h>
38 #include <fcntl.h>
39 #include <sys/types.h>
40 #include <sys/stat.h>
41 #include <syslog.h>
42 #include "libshare.h"
43 #include "libshare_impl.h"
44 #include <pwd.h>
45 #include <limits.h>
46 #include <libscf.h>
47 #include <libscf_priv.h>
48 #include <strings.h>
49 #include "libshare_smb.h"
50 #include <rpcsvc/daemon_utils.h>
51 #include <smbsrv/smb_share.h>
52 #include <smbsrv/smbinfo.h>
53 #include <smbsrv/libsmb.h>
54 #include <libdlpi.h>
55 
56 #define	SMB_CSC_BUFSZ		64
57 
58 #define	SMB_VALID_SUB_CHRS	"UDhMLmIiSPu"	/* substitution characters */
59 
60 /* internal functions */
61 static int smb_share_init(void);
62 static void smb_share_fini(void);
63 static int smb_enable_share(sa_share_t);
64 static int smb_share_changed(sa_share_t);
65 static int smb_resource_changed(sa_resource_t);
66 static int smb_rename_resource(sa_handle_t, sa_resource_t, char *);
67 static int smb_disable_share(sa_share_t share, char *);
68 static int smb_validate_property(sa_handle_t, sa_property_t, sa_optionset_t);
69 static int smb_set_proto_prop(sa_property_t);
70 static sa_protocol_properties_t smb_get_proto_set(void);
71 static char *smb_get_status(void);
72 static int smb_parse_optstring(sa_group_t, char *);
73 static char *smb_format_options(sa_group_t, int);
74 
75 static int smb_enable_service(void);
76 
77 static int range_check_validator(int, char *);
78 static int range_check_validator_zero_ok(int, char *);
79 static int string_length_check_validator(int, char *);
80 static int print_enable_validator(int, char *);
81 static int true_false_validator(int, char *);
82 static int ipv4_validator(int, char *);
83 static int hostname_validator(int, char *);
84 static int path_validator(int, char *);
85 static int cmd_validator(int, char *);
86 static int disposition_validator(int, char *);
87 static int max_protocol_validator(int, char *);
88 static int require_validator(int, char *);
89 
90 static int smb_enable_resource(sa_resource_t);
91 static int smb_disable_resource(sa_resource_t);
92 static uint64_t smb_share_features(void);
93 static int smb_list_transient(sa_handle_t);
94 
95 static int smb_build_shareinfo(sa_share_t, sa_resource_t, smb_share_t *);
96 static void smb_csc_option(const char *, smb_share_t *);
97 static char *smb_csc_name(const smb_share_t *);
98 static sa_group_t smb_get_defaultgrp(sa_handle_t);
99 static int interface_validator(int, char *);
100 static int smb_update_optionset_props(sa_handle_t, sa_resource_t, nvlist_t *);
101 
102 static boolean_t smb_saprop_getbool(sa_optionset_t, char *, boolean_t);
103 static boolean_t smb_saprop_getstr(sa_optionset_t, char *, char *, size_t);
104 
105 static struct {
106 	char *value;
107 	uint32_t flag;
108 } cscopt[] = {
109 	{ "disabled",	SMB_SHRF_CSC_DISABLED },
110 	{ "manual",	SMB_SHRF_CSC_MANUAL },
111 	{ "auto",	SMB_SHRF_CSC_AUTO },
112 	{ "vdo",	SMB_SHRF_CSC_VDO }
113 };
114 
115 /* size of basic format allocation */
116 #define	OPT_CHUNK	1024
117 
118 /* size of string for types - big enough to hold "dependency" */
119 #define	SCFTYPE_LEN	32
120 
121 /*
122  * Indexes of entries in smb_proto_options table.
123  * Changes to smb_proto_options table may require
124  * an update to these values.
125  */
126 #define	PROTO_OPT_WINS1			6
127 #define	PROTO_OPT_WINS_EXCLUDE		8
128 
129 typedef struct smb_hostifs_walker {
130 	const char	*hiw_ifname;
131 	boolean_t	hiw_matchfound;
132 } smb_hostifs_walker_t;
133 
134 
135 /*
136  * ops vector that provides the protocol specific info and operations
137  * for share management.
138  */
139 
140 struct sa_plugin_ops sa_plugin_ops = {
141 	SA_PLUGIN_VERSION,
142 	SMB_PROTOCOL_NAME,
143 	smb_share_init,
144 	smb_share_fini,
145 	smb_enable_share,
146 	smb_disable_share,
147 	smb_validate_property,
148 	NULL,	/* valid_space */
149 	NULL,	/* security_prop */
150 	smb_parse_optstring,
151 	smb_format_options,
152 	smb_set_proto_prop,
153 	smb_get_proto_set,
154 	smb_get_status,
155 	NULL,	/* space_alias */
156 	NULL,	/* update_legacy */
157 	NULL,	/* delete_legacy */
158 	smb_share_changed,
159 	smb_enable_resource,
160 	smb_disable_resource,
161 	smb_share_features,
162 	smb_list_transient,
163 	smb_resource_changed,
164 	smb_rename_resource,
165 	NULL,	/* run_command */
166 	NULL,	/* command_help */
167 	NULL	/* delete_proto_section */
168 };
169 
170 struct option_defs optdefs[] = {
171 	{ SHOPT_AD_CONTAINER,	OPT_TYPE_STRING },
172 	{ SHOPT_ABE,		OPT_TYPE_BOOLEAN },
173 	{ SHOPT_NAME,		OPT_TYPE_NAME },
174 	{ SHOPT_RO,		OPT_TYPE_ACCLIST },
175 	{ SHOPT_RW,		OPT_TYPE_ACCLIST },
176 	{ SHOPT_NONE,		OPT_TYPE_ACCLIST },
177 	{ SHOPT_CATIA,		OPT_TYPE_BOOLEAN },
178 	{ SHOPT_CSC,		OPT_TYPE_CSC },
179 	{ SHOPT_GUEST,		OPT_TYPE_BOOLEAN },
180 	{ SHOPT_DFSROOT,	OPT_TYPE_BOOLEAN },
181 	{ SHOPT_DESCRIPTION,	OPT_TYPE_STRING },
182 	{ SHOPT_FSO,		OPT_TYPE_BOOLEAN },
183 	{ SHOPT_QUOTAS,		OPT_TYPE_BOOLEAN },
184 	{ SHOPT_ENCRYPT,	OPT_TYPE_STRING },
185 	{ NULL, 0 }
186 };
187 
188 /*
189  * findopt(name)
190  *
191  * Lookup option "name" in the option table and return the table
192  * index.
193  */
194 static int
195 findopt(char *name)
196 {
197 	int i;
198 	if (name != NULL) {
199 		for (i = 0; optdefs[i].tag != NULL; i++) {
200 			if (strcmp(optdefs[i].tag, name) == 0)
201 				return (i);
202 		}
203 	}
204 	return (-1);
205 }
206 
207 /*
208  * is_a_number(number)
209  *
210  * is the string a number in one of the forms we want to use?
211  */
212 static boolean_t
213 is_a_number(char *number)
214 {
215 	boolean_t isnum = B_TRUE;
216 	boolean_t ishex = B_FALSE;
217 
218 	if (number == NULL || *number == '\0')
219 		return (B_FALSE);
220 
221 	if (strncasecmp(number, "0x", 2) == 0) {
222 		number += 2;
223 		ishex = B_TRUE;
224 	} else if (*number == '-') {
225 		number++;
226 	}
227 
228 	while (isnum && (*number != '\0')) {
229 		isnum = (ishex) ? isxdigit(*number) : isdigit(*number);
230 		number++;
231 	}
232 
233 	return (isnum);
234 }
235 
236 /*
237  * check ro vs rw values.  Over time this may get beefed up.
238  * for now it just does simple checks.
239  */
240 
241 static int
242 check_rorw(char *v1, char *v2)
243 {
244 	int ret = SA_OK;
245 	if (strcmp(v1, v2) == 0)
246 		ret = SA_VALUE_CONFLICT;
247 	return (ret);
248 }
249 
250 /*
251  * validresource(name)
252  *
253  * Check that name only has valid characters in it. The current valid
254  * set are the printable characters but not including:
255  *	" / \ [ ] : | < > + ; , ? * = \t
256  * Note that space is included and there is a maximum length.
257  */
258 static boolean_t
259 validresource(const char *name)
260 {
261 	const char *cp;
262 	size_t len;
263 
264 	if (name == NULL)
265 		return (B_FALSE);
266 
267 	len = strlen(name);
268 	if (len == 0 || len > SA_MAX_RESOURCE_NAME)
269 		return (B_FALSE);
270 
271 	if (strpbrk(name, "\"/\\[]:|<>+;,?*=\t") != NULL) {
272 		return (B_FALSE);
273 	}
274 
275 	for (cp = name; *cp != '\0'; cp++)
276 		if (iscntrl(*cp))
277 			return (B_FALSE);
278 
279 	return (B_TRUE);
280 }
281 
282 /*
283  * Check that the client-side caching (CSC) option value is valid.
284  */
285 static boolean_t
286 validcsc(const char *value)
287 {
288 	int i;
289 
290 	for (i = 0; i < (sizeof (cscopt) / sizeof (cscopt[0])); ++i) {
291 		if (strcasecmp(value, cscopt[i].value) == 0)
292 			return (B_TRUE);
293 	}
294 
295 	return (B_FALSE);
296 }
297 
298 /*
299  * smb_isonline()
300  *
301  * Determine if the SMF service instance is in the online state or
302  * not. A number of operations depend on this state.
303  */
304 static boolean_t
305 smb_isonline(void)
306 {
307 	char *str;
308 	boolean_t ret = B_FALSE;
309 
310 	if ((str = smf_get_state(SMBD_DEFAULT_INSTANCE_FMRI)) != NULL) {
311 		ret = (strcmp(str, SCF_STATE_STRING_ONLINE) == 0);
312 		free(str);
313 	}
314 	return (ret);
315 }
316 
317 /*
318  * smb_isdisabled()
319  *
320  * Determine if the SMF service instance is in the disabled state or
321  * not. A number of operations depend on this state.
322  */
323 static boolean_t
324 smb_isdisabled(void)
325 {
326 	char *str;
327 	boolean_t ret = B_FALSE;
328 
329 	if ((str = smf_get_state(SMBD_DEFAULT_INSTANCE_FMRI)) != NULL) {
330 		ret = (strcmp(str, SCF_STATE_STRING_DISABLED) == 0);
331 		free(str);
332 	}
333 	return (ret);
334 }
335 
336 /*
337  * smb_isautoenable()
338  *
339  * Determine if the SMF service instance auto_enabled set or not. A
340  * number of operations depend on this state.  The property not being
341  * set or being set to true means autoenable.  Only being set to false
342  * is not autoenabled.
343  */
344 static boolean_t
345 smb_isautoenable(void)
346 {
347 	boolean_t ret = B_TRUE;
348 	scf_simple_prop_t *prop;
349 	uint8_t *retstr;
350 
351 	prop = scf_simple_prop_get(NULL, SMBD_DEFAULT_INSTANCE_FMRI,
352 	    "application", "auto_enable");
353 	if (prop != NULL) {
354 		retstr = scf_simple_prop_next_boolean(prop);
355 		ret = *retstr != 0;
356 		scf_simple_prop_free(prop);
357 	}
358 	return (ret);
359 }
360 
361 /*
362  * smb_ismaint()
363  *
364  * Determine if the SMF service instance is in the disabled state or
365  * not. A number of operations depend on this state.
366  */
367 static boolean_t
368 smb_ismaint(void)
369 {
370 	char *str;
371 	boolean_t ret = B_FALSE;
372 
373 	if ((str = smf_get_state(SMBD_DEFAULT_INSTANCE_FMRI)) != NULL) {
374 		ret = (strcmp(str, SCF_STATE_STRING_MAINT) == 0);
375 		free(str);
376 	}
377 	return (ret);
378 }
379 
380 /*
381  * smb_enable_share tells the implementation that it is to enable the share.
382  * This entails converting the path and options into the appropriate ioctl
383  * calls. It is assumed that all error checking of paths, etc. were
384  * done earlier.
385  */
386 static int
387 smb_enable_share(sa_share_t share)
388 {
389 	char *path;
390 	smb_share_t si;
391 	sa_resource_t resource;
392 	boolean_t iszfs;
393 	boolean_t privileged;
394 	int err = SA_OK;
395 	priv_set_t *priv_effective;
396 	boolean_t online;
397 
398 	/*
399 	 * Don't support Trusted Extensions.
400 	 */
401 	if (is_system_labeled()) {
402 		(void) printf(dgettext(TEXT_DOMAIN,
403 		    "SMB: service not supported with Trusted Extensions\n"));
404 		return (SA_NOT_SUPPORTED);
405 	}
406 
407 	priv_effective = priv_allocset();
408 	(void) getppriv(PRIV_EFFECTIVE, priv_effective);
409 	privileged = (priv_isfullset(priv_effective) == B_TRUE);
410 	priv_freeset(priv_effective);
411 
412 	/* get the path since it is important in several places */
413 	path = sa_get_share_attr(share, "path");
414 	if (path == NULL)
415 		return (SA_NO_SUCH_PATH);
416 
417 	/*
418 	 * If administratively disabled, don't try to start anything.
419 	 */
420 	online = smb_isonline();
421 	if (!online && !smb_isautoenable() && smb_isdisabled())
422 		goto done;
423 
424 	iszfs = sa_path_is_zfs(path);
425 
426 	if (iszfs) {
427 
428 		if (privileged == B_FALSE && !online) {
429 
430 			if (!online) {
431 				(void) printf(dgettext(TEXT_DOMAIN,
432 				    "SMB: Cannot share remove "
433 				    "file system: %s\n"), path);
434 				(void) printf(dgettext(TEXT_DOMAIN,
435 				    "SMB: Service needs to be enabled "
436 				    "by a privileged user\n"));
437 				err = SA_NO_PERMISSION;
438 				errno = EPERM;
439 			}
440 			if (err) {
441 				sa_free_attr_string(path);
442 				return (err);
443 			}
444 
445 		}
446 	}
447 
448 	if (privileged == B_TRUE && !online) {
449 		err = smb_enable_service();
450 		if (err != SA_OK) {
451 			(void) printf(dgettext(TEXT_DOMAIN,
452 			    "SMB: Unable to enable service\n"));
453 		} else {
454 			online = B_TRUE;
455 		}
456 	}
457 
458 	/*
459 	 * Don't bother trying to start shares if the service isn't
460 	 * running.
461 	 */
462 	if (!online)
463 		goto done;
464 
465 	/* Each share can have multiple resources */
466 	for (resource = sa_get_share_resource(share, NULL);
467 	    resource != NULL;
468 	    resource = sa_get_next_resource(resource)) {
469 		err = smb_build_shareinfo(share, resource, &si);
470 		if (err != SA_OK) {
471 			sa_free_attr_string(path);
472 			return (err);
473 		}
474 
475 		if (!iszfs) {
476 			err = smb_share_create(&si);
477 		} else {
478 			share_t sh;
479 
480 			(void) sa_sharetab_fill_zfs(share, &sh, "smb");
481 			err = sa_share_zfs(share, resource, (char *)path, &sh,
482 			    &si, ZFS_SHARE_SMB);
483 			if (err != SA_OK) {
484 				errno = err;
485 				err = -1;
486 			}
487 			sa_emptyshare(&sh);
488 		}
489 	}
490 	if (!iszfs)
491 		(void) sa_update_sharetab(share, "smb");
492 done:
493 	sa_free_attr_string(path);
494 
495 	return (err == NERR_DuplicateShare ? 0 : err);
496 }
497 
498 /*
499  * This is the share for CIFS all shares have resource names.
500  * Enable tells the smb server to update its hash. If it fails
501  * because smb server is down, we just ignore as smb server loads
502  * the resources from sharemanager at startup.
503  */
504 
505 static int
506 smb_enable_resource(sa_resource_t resource)
507 {
508 	sa_share_t share;
509 	smb_share_t si;
510 	int ret = SA_OK;
511 	int err;
512 	boolean_t isonline;
513 
514 	share = sa_get_resource_parent(resource);
515 	if (share == NULL)
516 		return (SA_NO_SUCH_PATH);
517 
518 	/*
519 	 * If administratively disabled, don't try to start anything.
520 	 */
521 	isonline = smb_isonline();
522 	if (!isonline && !smb_isautoenable() && smb_isdisabled())
523 		return (SA_OK);
524 
525 	if (!isonline) {
526 		(void) smb_enable_service();
527 
528 		if (!smb_isonline())
529 			return (SA_OK);
530 	}
531 
532 	if ((ret = smb_build_shareinfo(share, resource, &si)) != SA_OK)
533 		return (ret);
534 
535 	/*
536 	 * Attempt to add the share. Any error that occurs if it was
537 	 * online is an error but don't count NERR_DuplicateName if
538 	 * smb/server had to be brought online since bringing the
539 	 * service up will enable the share that was just added prior
540 	 * to the attempt to enable.
541 	 */
542 	err = smb_share_create(&si);
543 	if (err == NERR_Success || !(!isonline && err == NERR_DuplicateName))
544 		(void) sa_update_sharetab(share, "smb");
545 	else
546 		return (SA_NOT_SHARED);
547 
548 	return (SA_OK);
549 }
550 
551 /*
552  * Remove it from smb server hash.
553  */
554 static int
555 smb_disable_resource(sa_resource_t resource)
556 {
557 	char *rname;
558 	uint32_t res;
559 	sa_share_t share;
560 
561 	rname = sa_get_resource_attr(resource, "name");
562 	if (rname == NULL)
563 		return (SA_NO_SUCH_RESOURCE);
564 
565 	if (smb_isonline()) {
566 		res = smb_share_delete(rname);
567 		if (res != NERR_Success &&
568 		    res != NERR_NetNameNotFound) {
569 			sa_free_attr_string(rname);
570 			return (SA_CONFIG_ERR);
571 		}
572 	}
573 
574 	sa_free_attr_string(rname);
575 
576 	share = sa_get_resource_parent(resource);
577 	if (share != NULL) {
578 		rname = sa_get_share_attr(share, "path");
579 		if (rname != NULL) {
580 			sa_handle_t handle;
581 
582 			handle = sa_find_group_handle((sa_group_t)resource);
583 			(void) sa_delete_sharetab(handle, rname, "smb");
584 			sa_free_attr_string(rname);
585 		}
586 	}
587 	/*
588 	 * Always return OK as smb/server may be down and
589 	 * Shares will be picked up when loaded.
590 	 */
591 	return (SA_OK);
592 }
593 
594 /*
595  * smb_share_changed(sa_share_t share)
596  *
597  * The specified share has changed.
598  */
599 static int
600 smb_share_changed(sa_share_t share)
601 {
602 	char *path;
603 	sa_resource_t resource;
604 
605 	if (!smb_isonline())
606 		return (SA_OK);
607 
608 	/* get the path since it is important in several places */
609 	path = sa_get_share_attr(share, "path");
610 	if (path == NULL)
611 		return (SA_NO_SUCH_PATH);
612 
613 	for (resource = sa_get_share_resource(share, NULL);
614 	    resource != NULL;
615 	    resource = sa_get_next_resource(resource))
616 		(void) smb_resource_changed(resource);
617 
618 	sa_free_attr_string(path);
619 
620 	return (SA_OK);
621 }
622 
623 /*
624  * smb_resource_changed(sa_resource_t resource)
625  *
626  * The specified resource has changed.
627  */
628 static int
629 smb_resource_changed(sa_resource_t resource)
630 {
631 	uint32_t res;
632 	sa_share_t share;
633 	smb_share_t si;
634 
635 	if (!smb_isonline())
636 		return (SA_OK);
637 
638 	if ((share = sa_get_resource_parent(resource)) == NULL)
639 		return (SA_CONFIG_ERR);
640 
641 	if ((res = smb_build_shareinfo(share, resource, &si)) != SA_OK)
642 		return (res);
643 
644 	res = smb_share_modify(&si);
645 
646 	if (res != NERR_Success)
647 		return (SA_CONFIG_ERR);
648 
649 	return (smb_enable_service());
650 }
651 
652 /*
653  * smb_disable_share(sa_share_t share, char *path)
654  *
655  * Unshare the specified share. Note that "path" is the same
656  * path as what is in the "share" object. It is passed in to avoid an
657  * additional lookup. A missing "path" value makes this a no-op
658  * function.
659  */
660 static int
661 smb_disable_share(sa_share_t share, char *path)
662 {
663 	char *rname;
664 	sa_resource_t resource;
665 	sa_group_t parent;
666 	boolean_t iszfs;
667 	int err = SA_OK;
668 	int ret = SA_OK;
669 	sa_handle_t handle;
670 	boolean_t first = B_TRUE; /* work around sharetab issue */
671 
672 	if (path == NULL)
673 		return (ret);
674 
675 	/*
676 	 * If the share is in a ZFS group we need to handle it
677 	 * differently.  Just being on a ZFS file system isn't
678 	 * enough since we may be in a legacy share case.
679 	 */
680 	parent = sa_get_parent_group(share);
681 	iszfs = sa_group_is_zfs(parent);
682 
683 	if (!smb_isonline())
684 		goto done;
685 
686 	for (resource = sa_get_share_resource(share, NULL);
687 	    resource != NULL;
688 	    resource = sa_get_next_resource(resource)) {
689 		rname = sa_get_resource_attr(resource, "name");
690 		if (rname == NULL) {
691 			continue;
692 		}
693 		if (!iszfs) {
694 			err = smb_share_delete(rname);
695 			switch (err) {
696 			case NERR_NetNameNotFound:
697 			case NERR_Success:
698 				err = SA_OK;
699 				break;
700 			default:
701 				err = SA_CONFIG_ERR;
702 				break;
703 			}
704 		} else {
705 			share_t sh;
706 
707 			(void) sa_sharetab_fill_zfs(share, &sh, "smb");
708 			err = sa_share_zfs(share, resource, (char *)path, &sh,
709 			    rname, ZFS_UNSHARE_SMB);
710 			if (err != SA_OK) {
711 				switch (err) {
712 				case EINVAL:
713 				case ENOENT:
714 					err = SA_OK;
715 					break;
716 				default:
717 					/*
718 					 * If we are no longer the first case,
719 					 * we don't care about the sa_share_zfs
720 					 * err if it is -1. This works around
721 					 * a problem in sharefs and should be
722 					 * removed when sharefs supports
723 					 * multiple entries per path.
724 					 */
725 					if (!first)
726 						err = SA_OK;
727 					else
728 						err = SA_SYSTEM_ERR;
729 					break;
730 				}
731 			}
732 
733 			first = B_FALSE;
734 
735 			sa_emptyshare(&sh);
736 		}
737 
738 		if (err != SA_OK)
739 			ret = err;
740 		sa_free_attr_string(rname);
741 	}
742 done:
743 	if (!iszfs) {
744 		handle = sa_find_group_handle((sa_group_t)share);
745 		if (handle != NULL)
746 			(void) sa_delete_sharetab(handle, path, "smb");
747 		else
748 			ret = SA_SYSTEM_ERR;
749 	}
750 	return (ret);
751 }
752 
753 /*
754  * smb_validate_property(handle, property, parent)
755  *
756  * Check that the property has a legitimate value for its type.
757  * Handle isn't currently used but may need to be in the future.
758  */
759 
760 /*ARGSUSED*/
761 static int
762 smb_validate_property(sa_handle_t handle, sa_property_t property,
763     sa_optionset_t parent)
764 {
765 	int ret = SA_OK;
766 	char *propname;
767 	int optindex;
768 	sa_group_t parent_group;
769 	char *value;
770 	char *other;
771 
772 	propname = sa_get_property_attr(property, "type");
773 
774 	if ((optindex = findopt(propname)) < 0)
775 		ret = SA_NO_SUCH_PROP;
776 
777 	/* need to validate value range here as well */
778 	if (ret == SA_OK) {
779 		parent_group = sa_get_parent_group((sa_share_t)parent);
780 		if (optdefs[optindex].share && !sa_is_share(parent_group))
781 			ret = SA_PROP_SHARE_ONLY;
782 	}
783 	if (ret != SA_OK) {
784 		if (propname != NULL)
785 			sa_free_attr_string(propname);
786 		return (ret);
787 	}
788 
789 	value = sa_get_property_attr(property, "value");
790 	if (value != NULL) {
791 		/* first basic type checking */
792 		switch (optdefs[optindex].type) {
793 		case OPT_TYPE_NUMBER:
794 			/* check that the value is all digits */
795 			if (!is_a_number(value))
796 				ret = SA_BAD_VALUE;
797 			break;
798 		case OPT_TYPE_BOOLEAN:
799 			ret = true_false_validator(0, value);
800 			break;
801 		case OPT_TYPE_NAME:
802 			/*
803 			 * Make sure no invalid characters
804 			 */
805 			if (!validresource(value))
806 				ret = SA_BAD_VALUE;
807 			break;
808 		case OPT_TYPE_STRING:
809 			/* whatever is here should be ok */
810 			break;
811 		case OPT_TYPE_CSC:
812 			if (!validcsc(value))
813 				ret = SA_BAD_VALUE;
814 			break;
815 		case OPT_TYPE_ACCLIST: {
816 			sa_property_t oprop;
817 			char *ovalue;
818 			/*
819 			 * access list handling. Should eventually
820 			 * validate that all the values make sense.
821 			 * Also, ro and rw may have cross value
822 			 * conflicts.
823 			 */
824 			if (parent == NULL)
825 				break;
826 			if (strcmp(propname, SHOPT_RO) == 0)
827 				other = SHOPT_RW;
828 			else if (strcmp(propname, SHOPT_RW) == 0)
829 				other = SHOPT_RO;
830 			else
831 				other = NULL;
832 			if (other == NULL)
833 				break;
834 
835 			/* compare rw(ro) with ro(rw) */
836 			oprop = sa_get_property(parent, other);
837 			if (oprop == NULL)
838 				break;
839 			/*
840 			 * only potential
841 			 * confusion if other
842 			 * exists
843 			 */
844 			ovalue = sa_get_property_attr(oprop, "value");
845 			if (ovalue != NULL) {
846 				ret = check_rorw(value, ovalue);
847 				sa_free_attr_string(ovalue);
848 			}
849 			break;
850 		}
851 		default:
852 			break;
853 		}
854 	}
855 
856 	if (value != NULL)
857 		sa_free_attr_string(value);
858 	if (ret == SA_OK && optdefs[optindex].check != NULL)
859 		/* do the property specific check */
860 		ret = optdefs[optindex].check(property);
861 
862 	if (propname != NULL)
863 		sa_free_attr_string(propname);
864 	return (ret);
865 }
866 
867 /*
868  * Protocol management functions
869  *
870  * properties defined in the default files are defined in
871  * proto_option_defs for parsing and validation.
872  */
873 
874 struct smb_proto_option_defs {
875 	int smb_index;
876 	int32_t minval;
877 	int32_t maxval; /* In case of length of string this should be max */
878 	int (*validator)(int, char *);
879 	int32_t	refresh;
880 } smb_proto_options[] = {
881 	{ SMB_CI_SYS_CMNT, 0, MAX_VALUE_BUFLEN,
882 	    string_length_check_validator, SMB_REFRESH_REFRESH },
883 	{ SMB_CI_MAX_WORKERS, SMB_PI_MAX_WORKERS_MIN, SMB_PI_MAX_WORKERS_MAX,
884 	    range_check_validator, SMB_REFRESH_REFRESH },
885 	{ SMB_CI_NETBIOS_ENABLE, 0, 0, true_false_validator,
886 	    SMB_REFRESH_REFRESH },
887 	{ SMB_CI_NBSCOPE, 0, MAX_VALUE_BUFLEN,
888 	    string_length_check_validator, 0 },
889 	{ SMB_CI_LM_LEVEL, 2, 5, range_check_validator, 0 },
890 	{ SMB_CI_KEEPALIVE, 20, 5400, range_check_validator_zero_ok,
891 	    SMB_REFRESH_REFRESH },
892 	{ SMB_CI_WINS_SRV1, 0, MAX_VALUE_BUFLEN,
893 	    ipv4_validator, SMB_REFRESH_REFRESH },
894 	{ SMB_CI_WINS_SRV2, 0, MAX_VALUE_BUFLEN,
895 	    ipv4_validator, SMB_REFRESH_REFRESH },
896 	{ SMB_CI_WINS_EXCL, 0, MAX_VALUE_BUFLEN,
897 	    interface_validator, SMB_REFRESH_REFRESH },
898 	{ SMB_CI_SIGNING_ENABLE, 0, 0, true_false_validator,
899 	    SMB_REFRESH_REFRESH },
900 	{ SMB_CI_SIGNING_REQD, 0, 0, true_false_validator,
901 	    SMB_REFRESH_REFRESH },
902 	{ SMB_CI_RESTRICT_ANON, 0, 0, true_false_validator,
903 	    SMB_REFRESH_REFRESH },
904 	{ SMB_CI_DOMAIN_SRV, 0, MAX_VALUE_BUFLEN,
905 	    hostname_validator, SMB_REFRESH_REFRESH },
906 	{ SMB_CI_ADS_SITE, 0, MAX_VALUE_BUFLEN,
907 	    string_length_check_validator, SMB_REFRESH_REFRESH },
908 	{ SMB_CI_DYNDNS_ENABLE, 0, 0, true_false_validator, 0 },
909 	{ SMB_CI_AUTOHOME_MAP, 0, MAX_VALUE_BUFLEN, path_validator, 0 },
910 	{ SMB_CI_IPV6_ENABLE, 0, 0, true_false_validator,
911 	    SMB_REFRESH_REFRESH },
912 	{ SMB_CI_PRINT_ENABLE, 0, 0, print_enable_validator,
913 	    SMB_REFRESH_REFRESH },
914 	{ SMB_CI_TRAVERSE_MOUNTS, 0, 0, true_false_validator,
915 	    SMB_REFRESH_REFRESH },
916 	{ SMB_CI_MAP, 0, MAX_VALUE_BUFLEN, cmd_validator, SMB_REFRESH_REFRESH },
917 	{ SMB_CI_UNMAP, 0, MAX_VALUE_BUFLEN, cmd_validator,
918 	    SMB_REFRESH_REFRESH },
919 	{ SMB_CI_DISPOSITION, 0, MAX_VALUE_BUFLEN,
920 	    disposition_validator, SMB_REFRESH_REFRESH },
921 	{ SMB_CI_MAX_PROTOCOL, 0, MAX_VALUE_BUFLEN, max_protocol_validator,
922 	    SMB_REFRESH_REFRESH },
923 	{ SMB_CI_ENCRYPT, 0, MAX_VALUE_BUFLEN, require_validator,
924 	    SMB_REFRESH_REFRESH },
925 	{ SMB_CI_OPLOCK_ENABLE, 0, 0, true_false_validator,
926 	    SMB_REFRESH_REFRESH },
927 };
928 
929 #define	SMB_OPT_NUM \
930 	(sizeof (smb_proto_options) / sizeof (smb_proto_options[0]))
931 
932 static int
933 require_validator(int index, char *value)
934 {
935 	if (string_length_check_validator(index, value) != SA_OK)
936 		return (SA_BAD_VALUE);
937 
938 	if (strcmp(value, "required") == 0)
939 		return (SA_OK);
940 
941 	if (strcmp(value, "disabled") == 0)
942 		return (SA_OK);
943 
944 	if (strcmp(value, "enabled") == 0)
945 		return (SA_OK);
946 
947 	return (SA_BAD_VALUE);
948 }
949 
950 /*
951  * Check the range of value as int range.
952  */
953 static int
954 range_check_validator(int index, char *value)
955 {
956 	int ret = SA_OK;
957 
958 	if (!is_a_number(value)) {
959 		ret = SA_BAD_VALUE;
960 	} else {
961 		int val;
962 		val = strtoul(value, NULL, 0);
963 		if (val < smb_proto_options[index].minval ||
964 		    val > smb_proto_options[index].maxval)
965 			ret = SA_BAD_VALUE;
966 	}
967 	return (ret);
968 }
969 
970 /*
971  * Check the range of value as int range.
972  */
973 static int
974 range_check_validator_zero_ok(int index, char *value)
975 {
976 	int ret = SA_OK;
977 
978 	if (!is_a_number(value)) {
979 		ret = SA_BAD_VALUE;
980 	} else {
981 		int val;
982 		val = strtoul(value, NULL, 0);
983 		if (val == 0)
984 			ret = SA_OK;
985 		else {
986 			if (val < smb_proto_options[index].minval ||
987 			    val > smb_proto_options[index].maxval)
988 			ret = SA_BAD_VALUE;
989 		}
990 	}
991 	return (ret);
992 }
993 
994 /*
995  * Check the length of the string
996  */
997 static int
998 string_length_check_validator(int index, char *value)
999 {
1000 	int ret = SA_OK;
1001 
1002 	if (value == NULL)
1003 		return (SA_BAD_VALUE);
1004 	if (strlen(value) > smb_proto_options[index].maxval)
1005 		ret = SA_BAD_VALUE;
1006 	return (ret);
1007 }
1008 
1009 /*
1010  * Check yes/no
1011  */
1012 /*ARGSUSED*/
1013 static int
1014 true_false_validator(int index, char *value)
1015 {
1016 	if (value == NULL)
1017 		return (SA_BAD_VALUE);
1018 	if ((strcasecmp(value, "true") == 0) ||
1019 	    (strcasecmp(value, "false") == 0))
1020 		return (SA_OK);
1021 	return (SA_BAD_VALUE);
1022 }
1023 
1024 /*
1025  * If printing support is compiled in, this is the same as:
1026  * true_false_validator.  Otherwise, only allow false.
1027  */
1028 /*ARGSUSED*/
1029 static int
1030 print_enable_validator(int index, char *value)
1031 {
1032 	if (value == NULL)
1033 		return (SA_BAD_VALUE);
1034 
1035 #ifdef	HAVE_CUPS
1036 	if (strcasecmp(value, "true") == 0)
1037 		return (SA_OK);
1038 #endif
1039 	if (strcasecmp(value, "false") == 0)
1040 		return (SA_OK);
1041 
1042 	return (SA_BAD_VALUE);
1043 }
1044 
1045 /*
1046  * Check IP v4 address.
1047  */
1048 /*ARGSUSED*/
1049 static int
1050 ipv4_validator(int index, char *value)
1051 {
1052 	char sbytes[16];
1053 
1054 	if (value == NULL)
1055 		return (SA_OK);
1056 
1057 	if (strlen(value) == 0)
1058 		return (SA_OK);
1059 
1060 	if (inet_pton(AF_INET, value, (void *)sbytes) != 1)
1061 		return (SA_BAD_VALUE);
1062 
1063 	return (SA_OK);
1064 }
1065 
1066 /*
1067  * Check that the specified name is an IP address (v4 or v6) or a hostname.
1068  * Per RFC 1035 and 1123, names may contain alphanumeric characters, hyphens
1069  * and dots.  The first and last character of a label must be alphanumeric.
1070  * Interior characters may be alphanumeric or hypens.
1071  *
1072  * Domain names should not contain underscores but we allow them because
1073  * Windows names are often in non-compliance with this rule.
1074  */
1075 /*ARGSUSED*/
1076 static int
1077 hostname_validator(int index, char *value)
1078 {
1079 	char		sbytes[INET6_ADDRSTRLEN];
1080 	boolean_t	new_label = B_TRUE;
1081 	char		*p;
1082 	char		label_terminator;
1083 	int		len;
1084 
1085 	if (value == NULL)
1086 		return (SA_OK);
1087 
1088 	if ((len = strlen(value)) == 0)
1089 		return (SA_OK);
1090 
1091 	if (inet_pton(AF_INET, value, (void *)sbytes) == 1)
1092 		return (SA_OK);
1093 
1094 	if (inet_pton(AF_INET6, value, (void *)sbytes) == 1)
1095 		return (SA_OK);
1096 
1097 	if (len >= MAXHOSTNAMELEN)
1098 		return (SA_BAD_VALUE);
1099 
1100 	if (strspn(value, "0123456789.") == len)
1101 		return (SA_BAD_VALUE);
1102 
1103 	label_terminator = *value;
1104 
1105 	for (p = value; *p != '\0'; ++p) {
1106 		if (new_label) {
1107 			if (!isalnum(*p))
1108 				return (SA_BAD_VALUE);
1109 			new_label = B_FALSE;
1110 			label_terminator = *p;
1111 			continue;
1112 		}
1113 
1114 		if (*p == '.') {
1115 			if (!isalnum(label_terminator))
1116 				return (SA_BAD_VALUE);
1117 			new_label = B_TRUE;
1118 			label_terminator = *p;
1119 			continue;
1120 		}
1121 
1122 		label_terminator = *p;
1123 
1124 		if (isalnum(*p) || *p == '-' || *p == '_')
1125 			continue;
1126 
1127 		return (SA_BAD_VALUE);
1128 	}
1129 
1130 	if (!isalnum(label_terminator))
1131 		return (SA_BAD_VALUE);
1132 
1133 	return (SA_OK);
1134 }
1135 
1136 /*
1137  * Call back function for dlpi_walk.
1138  * Returns TRUE if interface name exists on the host.
1139  */
1140 static boolean_t
1141 smb_get_interface(const char *ifname, void *arg)
1142 {
1143 	smb_hostifs_walker_t *iterp = arg;
1144 
1145 	iterp->hiw_matchfound = (strcmp(ifname, iterp->hiw_ifname) == 0);
1146 
1147 	return (iterp->hiw_matchfound);
1148 }
1149 
1150 /*
1151  * Checks to see if the input interface exists on the host.
1152  * Returns B_TRUE if the match is found, B_FALSE otherwise.
1153  */
1154 static boolean_t
1155 smb_validate_interface(const char *ifname)
1156 {
1157 	smb_hostifs_walker_t	iter;
1158 
1159 	if ((ifname == NULL) || (*ifname == '\0'))
1160 		return (B_FALSE);
1161 
1162 	iter.hiw_ifname = ifname;
1163 	iter.hiw_matchfound = B_FALSE;
1164 	dlpi_walk(smb_get_interface, &iter, 0);
1165 
1166 	return (iter.hiw_matchfound);
1167 }
1168 
1169 /*
1170  * Check valid interfaces. Interface names value can be NULL or empty.
1171  * Returns SA_BAD_VALUE if interface cannot be found on the host.
1172  */
1173 /*ARGSUSED*/
1174 static int
1175 interface_validator(int index, char *value)
1176 {
1177 	char buf[16];
1178 	int ret = SA_OK;
1179 	char *ifname, *tmp, *p;
1180 
1181 	if (value == NULL || *value == '\0')
1182 		return (ret);
1183 
1184 	if (strlen(value) > MAX_VALUE_BUFLEN)
1185 		return (SA_BAD_VALUE);
1186 
1187 	if ((p = strdup(value)) == NULL)
1188 		return (SA_NO_MEMORY);
1189 
1190 	tmp = p;
1191 	while ((ifname = strsep(&tmp, ",")) != NULL) {
1192 		if (*ifname == '\0') {
1193 			ret = SA_BAD_VALUE;
1194 			break;
1195 		}
1196 
1197 		if (!smb_validate_interface(ifname)) {
1198 			if (inet_pton(AF_INET, ifname, (void *)buf) == 0) {
1199 				ret = SA_BAD_VALUE;
1200 				break;
1201 			}
1202 		}
1203 	}
1204 
1205 	free(p);
1206 	return (ret);
1207 }
1208 
1209 /*
1210  * Check path
1211  */
1212 /*ARGSUSED*/
1213 static int
1214 path_validator(int index, char *path)
1215 {
1216 	struct stat buffer;
1217 	int fd, status;
1218 
1219 	if (path == NULL)
1220 		return (SA_BAD_VALUE);
1221 
1222 	fd = open(path, O_RDONLY);
1223 	if (fd < 0)
1224 		return (SA_BAD_VALUE);
1225 
1226 	status = fstat(fd, &buffer);
1227 	(void) close(fd);
1228 
1229 	if (status < 0)
1230 		return (SA_BAD_VALUE);
1231 
1232 	if (buffer.st_mode & S_IFDIR)
1233 		return (SA_OK);
1234 	return (SA_BAD_VALUE);
1235 }
1236 
1237 /*
1238  * the protoset holds the defined options so we don't have to read
1239  * them multiple times
1240  */
1241 static sa_protocol_properties_t protoset;
1242 
1243 static int
1244 findprotoopt(char *name)
1245 {
1246 	int i;
1247 	char *sc_name;
1248 
1249 	for (i = 0; i < SMB_OPT_NUM; i++) {
1250 		sc_name = smb_config_getname(smb_proto_options[i].smb_index);
1251 		if (strcasecmp(sc_name, name) == 0)
1252 			return (i);
1253 	}
1254 
1255 	return (-1);
1256 }
1257 
1258 /*
1259  * smb_load_proto_properties()
1260  *
1261  * read the smb config values from SMF.
1262  */
1263 
1264 static int
1265 smb_load_proto_properties()
1266 {
1267 	sa_property_t prop;
1268 	char value[MAX_VALUE_BUFLEN];
1269 	char *name;
1270 	int index;
1271 	int ret = SA_OK;
1272 	int rc;
1273 
1274 	protoset = sa_create_protocol_properties(SMB_PROTOCOL_NAME);
1275 	if (protoset == NULL)
1276 		return (SA_NO_MEMORY);
1277 
1278 	for (index = 0; index < SMB_OPT_NUM && ret == SA_OK; index++) {
1279 		rc = smb_config_get(smb_proto_options[index].smb_index,
1280 		    value, sizeof (value));
1281 		if (rc != SMBD_SMF_OK)
1282 			continue;
1283 		name = smb_config_getname(smb_proto_options[index].smb_index);
1284 		prop = sa_create_property(name, value);
1285 		if (prop != NULL)
1286 			ret = sa_add_protocol_property(protoset, prop);
1287 		else
1288 			ret = SA_NO_MEMORY;
1289 	}
1290 	return (ret);
1291 }
1292 
1293 /*
1294  * smb_share_init()
1295  *
1296  * Initialize the smb plugin.
1297  */
1298 
1299 static int
1300 smb_share_init(void)
1301 {
1302 	if (sa_plugin_ops.sa_init != smb_share_init)
1303 		return (SA_SYSTEM_ERR);
1304 
1305 	smb_share_door_clnt_init();
1306 	return (smb_load_proto_properties());
1307 }
1308 
1309 /*
1310  * smb_share_fini()
1311  *
1312  */
1313 static void
1314 smb_share_fini(void)
1315 {
1316 	xmlFreeNode(protoset);
1317 	protoset = NULL;
1318 
1319 	smb_share_door_clnt_fini();
1320 }
1321 
1322 /*
1323  * smb_get_proto_set()
1324  *
1325  * Return an optionset with all the protocol specific properties in
1326  * it.
1327  */
1328 static sa_protocol_properties_t
1329 smb_get_proto_set(void)
1330 {
1331 	return (protoset);
1332 }
1333 
1334 /*
1335  * smb_enable_dependencies()
1336  *
1337  * SMBD_DEFAULT_INSTANCE_FMRI may have some dependencies that aren't
1338  * enabled. This will attempt to enable all of them.
1339  */
1340 static void
1341 smb_enable_dependencies(const char *fmri)
1342 {
1343 	scf_handle_t *handle;
1344 	scf_service_t *service;
1345 	scf_instance_t *inst = NULL;
1346 	scf_iter_t *iter;
1347 	scf_property_t *prop;
1348 	scf_value_t *value;
1349 	scf_propertygroup_t *pg;
1350 	scf_scope_t *scope;
1351 	char type[SCFTYPE_LEN];
1352 	char *dependency;
1353 	char *servname;
1354 	int maxlen;
1355 
1356 	/*
1357 	 * Get all required handles and storage.
1358 	 */
1359 	handle = scf_handle_create(SCF_VERSION);
1360 	if (handle == NULL)
1361 		return;
1362 
1363 	if (scf_handle_bind(handle) != 0) {
1364 		scf_handle_destroy(handle);
1365 		return;
1366 	}
1367 
1368 	maxlen = scf_limit(SCF_LIMIT_MAX_VALUE_LENGTH);
1369 	if (maxlen == (ssize_t)-1)
1370 		maxlen = MAXPATHLEN;
1371 
1372 	dependency = malloc(maxlen);
1373 
1374 	service = scf_service_create(handle);
1375 
1376 	iter = scf_iter_create(handle);
1377 
1378 	pg = scf_pg_create(handle);
1379 
1380 	prop = scf_property_create(handle);
1381 
1382 	value = scf_value_create(handle);
1383 
1384 	scope = scf_scope_create(handle);
1385 
1386 	if (service == NULL || iter == NULL || pg == NULL || prop == NULL ||
1387 	    value == NULL || scope == NULL || dependency == NULL)
1388 		goto done;
1389 
1390 	/*
1391 	 *  We passed in the FMRI for the default instance but for
1392 	 *  some things we need the simple form so construct it. Since
1393 	 *  we reuse the storage that dependency points to, we need to
1394 	 *  use the servname early.
1395 	 */
1396 	(void) snprintf(dependency, maxlen, "%s", fmri + sizeof ("svc:"));
1397 	servname = strrchr(dependency, ':');
1398 	if (servname == NULL)
1399 		goto done;
1400 	*servname = '\0';
1401 	servname = dependency;
1402 
1403 	/*
1404 	 * Setup to iterate over the service property groups, only
1405 	 * looking at those that are "dependency" types. The "entity"
1406 	 * property will have the FMRI of the service we are dependent
1407 	 * on.
1408 	 */
1409 	if (scf_handle_get_scope(handle, SCF_SCOPE_LOCAL, scope) != 0)
1410 		goto done;
1411 
1412 	if (scf_scope_get_service(scope, servname, service) != 0)
1413 		goto done;
1414 
1415 	if (scf_iter_service_pgs(iter, service) != 0)
1416 		goto done;
1417 
1418 	while (scf_iter_next_pg(iter, pg) > 0) {
1419 		char *services[2];
1420 		/*
1421 		 * Have a property group for the service. See if it is
1422 		 * a dependency pg and only do operations on those.
1423 		 */
1424 		if (scf_pg_get_type(pg, type, SCFTYPE_LEN) <= 0)
1425 			continue;
1426 
1427 		if (strncmp(type, SCF_GROUP_DEPENDENCY, SCFTYPE_LEN) != 0)
1428 			continue;
1429 		/*
1430 		 * Have a dependency.  Attempt to enable it.
1431 		 */
1432 		if (scf_pg_get_property(pg, SCF_PROPERTY_ENTITIES, prop) != 0)
1433 			continue;
1434 
1435 		if (scf_property_get_value(prop, value) != 0)
1436 			continue;
1437 
1438 		services[1] = NULL;
1439 
1440 		if (scf_value_get_as_string(value, dependency, maxlen) > 0) {
1441 			services[0] = dependency;
1442 			_check_services(services);
1443 		}
1444 	}
1445 
1446 done:
1447 	if (dependency != NULL)
1448 		free(dependency);
1449 	if (value != NULL)
1450 		scf_value_destroy(value);
1451 	if (prop != NULL)
1452 		scf_property_destroy(prop);
1453 	if (pg != NULL)
1454 		scf_pg_destroy(pg);
1455 	if (iter != NULL)
1456 		scf_iter_destroy(iter);
1457 	if (scope != NULL)
1458 		scf_scope_destroy(scope);
1459 	if (inst != NULL)
1460 		scf_instance_destroy(inst);
1461 	if (service != NULL)
1462 		scf_service_destroy(service);
1463 
1464 	(void) scf_handle_unbind(handle);
1465 	scf_handle_destroy(handle);
1466 }
1467 
1468 /*
1469  * How long to wait for service to come online
1470  */
1471 #define	WAIT_FOR_SERVICE	15
1472 
1473 /*
1474  * smb_enable_service()
1475  *
1476  */
1477 static int
1478 smb_enable_service(void)
1479 {
1480 	int i;
1481 	int ret = SA_OK;
1482 	char *service[] = { SMBD_DEFAULT_INSTANCE_FMRI, NULL };
1483 
1484 	if (!smb_isonline()) {
1485 		/*
1486 		 * Attempt to start the idmap, and other dependent
1487 		 * services, first.  If it fails, the SMB service will
1488 		 * ultimately fail so we use that as the error.  If we
1489 		 * don't try to enable idmap, smb won't start the
1490 		 * first time unless the admin has done it
1491 		 * manually. The service could be administratively
1492 		 * disabled so we won't always get started.
1493 		 */
1494 		smb_enable_dependencies(SMBD_DEFAULT_INSTANCE_FMRI);
1495 		_check_services(service);
1496 
1497 		/* Wait for service to come online */
1498 		for (i = 0; i < WAIT_FOR_SERVICE; i++) {
1499 			if (smb_isonline()) {
1500 				ret =  SA_OK;
1501 				break;
1502 			} else if (smb_ismaint()) {
1503 				/* maintenance requires help */
1504 				ret = SA_SYSTEM_ERR;
1505 				break;
1506 			} else {
1507 				/* try another time */
1508 				ret = SA_BUSY;
1509 				(void) sleep(1);
1510 			}
1511 		}
1512 	}
1513 	return (ret);
1514 }
1515 
1516 /*
1517  * smb_validate_proto_prop(index, name, value)
1518  *
1519  * Verify that the property specified by name can take the new
1520  * value. This is a sanity check to prevent bad values getting into
1521  * the default files.
1522  */
1523 static int
1524 smb_validate_proto_prop(int index, char *name, char *value)
1525 {
1526 	if ((name == NULL) || (index < 0))
1527 		return (SA_BAD_VALUE);
1528 
1529 	if (smb_proto_options[index].validator == NULL)
1530 		return (SA_OK);
1531 
1532 	if (smb_proto_options[index].validator(index, value) == SA_OK)
1533 		return (SA_OK);
1534 	return (SA_BAD_VALUE);
1535 }
1536 
1537 /*
1538  * smb_set_proto_prop(prop)
1539  *
1540  * check that prop is valid.
1541  */
1542 /*ARGSUSED*/
1543 static int
1544 smb_set_proto_prop(sa_property_t prop)
1545 {
1546 	int ret = SA_OK;
1547 	char *name;
1548 	char *value;
1549 	int index = -1;
1550 	struct smb_proto_option_defs *opt;
1551 
1552 	name = sa_get_property_attr(prop, "type");
1553 	value = sa_get_property_attr(prop, "value");
1554 	if (name != NULL && value != NULL) {
1555 		index = findprotoopt(name);
1556 		if (index >= 0) {
1557 			/* should test for valid value */
1558 			ret = smb_validate_proto_prop(index, name, value);
1559 			if (ret == SA_OK) {
1560 				opt = &smb_proto_options[index];
1561 
1562 				/* Save to SMF */
1563 				if (smb_config_set(opt->smb_index,
1564 				    value) != 0) {
1565 					ret = SA_BAD_VALUE;
1566 					goto out;
1567 				}
1568 				/*
1569 				 * Specialized refresh mechanisms can
1570 				 * be flagged in the proto_options and
1571 				 * processed here.
1572 				 */
1573 				if (opt->refresh & SMB_REFRESH_REFRESH)
1574 					(void) smf_refresh_instance(
1575 					    SMBD_DEFAULT_INSTANCE_FMRI);
1576 				else if (opt->refresh & SMB_REFRESH_RESTART)
1577 					(void) smf_restart_instance(
1578 					    SMBD_DEFAULT_INSTANCE_FMRI);
1579 			}
1580 		}
1581 	}
1582 
1583 out:
1584 	if (name != NULL)
1585 		sa_free_attr_string(name);
1586 	if (value != NULL)
1587 		sa_free_attr_string(value);
1588 
1589 	return (ret);
1590 }
1591 
1592 /*
1593  * smb_get_status()
1594  *
1595  * What is the current status of the smbd? We use the SMF state here.
1596  * Caller must free the returned value.
1597  */
1598 
1599 static char *
1600 smb_get_status(void)
1601 {
1602 	return (smf_get_state(SMBD_DEFAULT_INSTANCE_FMRI));
1603 }
1604 
1605 /*
1606  * This protocol plugin require resource names
1607  */
1608 static uint64_t
1609 smb_share_features(void)
1610 {
1611 	return (SA_FEATURE_RESOURCE | SA_FEATURE_ALLOWSUBDIRS |
1612 	    SA_FEATURE_ALLOWPARDIRS | SA_FEATURE_SERVER);
1613 }
1614 
1615 /*
1616  * This should be used to convert smb_share_t to sa_resource_t
1617  * Should only be needed to build transient shares/resources to be
1618  * supplied to sharemgr to display.
1619  */
1620 static int
1621 smb_add_transient(sa_handle_t handle, smb_share_t *si)
1622 {
1623 	int err;
1624 	sa_share_t share;
1625 	sa_group_t group;
1626 	sa_resource_t resource;
1627 	nvlist_t *nvl;
1628 	char *opt;
1629 
1630 	if (si == NULL)
1631 		return (SA_INVALID_NAME);
1632 
1633 	if ((share = sa_find_share(handle, si->shr_path)) == NULL) {
1634 		if ((group = smb_get_defaultgrp(handle)) == NULL)
1635 			return (SA_NO_SUCH_GROUP);
1636 
1637 		share = sa_get_share(group, si->shr_path);
1638 		if (share == NULL) {
1639 			share = sa_add_share(group, si->shr_path,
1640 			    SA_SHARE_TRANSIENT, &err);
1641 			if (share == NULL)
1642 				return (SA_NO_SUCH_PATH);
1643 		}
1644 	}
1645 
1646 	/*
1647 	 * Now handle the resource. Make sure that the resource is
1648 	 * transient and added to the share.
1649 	 */
1650 	resource = sa_get_share_resource(share, si->shr_name);
1651 	if (resource == NULL) {
1652 		resource = sa_add_resource(share,
1653 		    si->shr_name, SA_SHARE_TRANSIENT, &err);
1654 		if (resource == NULL)
1655 			return (SA_NO_SUCH_RESOURCE);
1656 	}
1657 
1658 	if (si->shr_cmnt[0] != '\0')
1659 		(void) sa_set_resource_description(resource, si->shr_cmnt);
1660 
1661 	if (si->shr_container[0] != '\0')
1662 		(void) sa_set_resource_attr(resource, SHOPT_AD_CONTAINER,
1663 		    si->shr_container);
1664 
1665 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
1666 		return (SA_NO_MEMORY);
1667 
1668 	if ((opt = smb_csc_name(si)) != NULL)
1669 		err |= nvlist_add_string(nvl, SHOPT_CSC, opt);
1670 
1671 	opt = (si->shr_flags & SMB_SHRF_ABE) ? "true" : "false";
1672 	err |= nvlist_add_string(nvl, SHOPT_ABE, opt);
1673 
1674 	if ((si->shr_flags & SMB_SHRF_AUTOHOME) == 0) {
1675 		opt = (si->shr_flags & SMB_SHRF_GUEST_OK) ? "true" : "false";
1676 		err |= nvlist_add_string(nvl, SHOPT_GUEST, opt);
1677 	}
1678 
1679 	if (si->shr_access_ro[0] != '\0')
1680 		err |= nvlist_add_string(nvl, SHOPT_RO, si->shr_access_ro);
1681 
1682 	if (si->shr_access_rw[0] != '\0')
1683 		err |= nvlist_add_string(nvl, SHOPT_RW, si->shr_access_rw);
1684 
1685 	if (si->shr_access_none[0] != '\0')
1686 		err |= nvlist_add_string(nvl, SHOPT_NONE, si->shr_access_none);
1687 
1688 	if (err) {
1689 		nvlist_free(nvl);
1690 		return (SA_CONFIG_ERR);
1691 	}
1692 
1693 	err = smb_update_optionset_props(handle, resource, nvl);
1694 
1695 	nvlist_free(nvl);
1696 	return (err);
1697 }
1698 
1699 /*
1700  * Return smb transient shares.
1701  */
1702 static int
1703 smb_list_transient(sa_handle_t handle)
1704 {
1705 	int i, offset;
1706 	smb_shrlist_t list;
1707 	int res;
1708 
1709 	if (smb_share_count() <= 0)
1710 		return (SA_OK);
1711 
1712 	offset = 0;
1713 	while (smb_share_list(offset, &list) == NERR_Success) {
1714 		if (list.sl_cnt == 0)
1715 			break;
1716 
1717 		for (i = 0; i < list.sl_cnt; i++) {
1718 			res = smb_add_transient(handle, &(list.sl_shares[i]));
1719 			if (res != SA_OK)
1720 				return (res);
1721 		}
1722 		offset += list.sl_cnt;
1723 	}
1724 
1725 	return (SA_OK);
1726 }
1727 
1728 /*
1729  * fix_resource_name(share, name,  prefix)
1730  *
1731  * Construct a name where the ZFS dataset has the prefix replaced with "name".
1732  */
1733 static char *
1734 fix_resource_name(sa_share_t share, char *name, char *prefix)
1735 {
1736 	char buf[SA_MAX_RESOURCE_NAME + 1];
1737 	char *dataset;
1738 	size_t bufsz = SA_MAX_RESOURCE_NAME + 1;
1739 	size_t prelen;
1740 
1741 	if (prefix == NULL)
1742 		return (strdup(name));
1743 
1744 	dataset = sa_get_share_attr(share, "dataset");
1745 	if (dataset == NULL)
1746 		return (strdup(name));
1747 
1748 	(void) strlcpy(buf, name, bufsz);
1749 	prelen = strlen(prefix);
1750 
1751 	if (strncmp(dataset, prefix, prelen) == 0)
1752 		(void) strlcat(buf, dataset + prelen, bufsz);
1753 
1754 	sa_free_attr_string(dataset);
1755 	sa_fix_resource_name(buf);
1756 	return (strdup(buf));
1757 }
1758 
1759 /*
1760  * smb_parse_optstring(group, options)
1761  *
1762  * parse a compact option string into individual options. This allows
1763  * ZFS sharesmb and sharemgr "share" command to work.  group can be a
1764  * group, a share or a resource.
1765  */
1766 static int
1767 smb_parse_optstring(sa_group_t group, char *options)
1768 {
1769 	char *dup;
1770 	char *base;
1771 	char *lasts;
1772 	char *token;
1773 	sa_optionset_t optionset;
1774 	sa_group_t parent = NULL;
1775 	sa_resource_t resource = NULL;
1776 	int iszfs = 0;
1777 	int persist = 0;
1778 	int need_optionset = 0;
1779 	int ret = SA_OK;
1780 	sa_property_t prop;
1781 
1782 	/*
1783 	 * In order to not attempt to change ZFS properties unless
1784 	 * absolutely necessary, we never do it in the legacy parsing
1785 	 * so we need to keep track of this.
1786 	 */
1787 	if (sa_is_share(group)) {
1788 		char *zfs;
1789 
1790 		parent = sa_get_parent_group(group);
1791 		if (parent != NULL) {
1792 			zfs = sa_get_group_attr(parent, "zfs");
1793 			if (zfs != NULL) {
1794 				sa_free_attr_string(zfs);
1795 				iszfs = 1;
1796 			}
1797 		}
1798 	} else {
1799 		iszfs = sa_group_is_zfs(group);
1800 		/*
1801 		 * If a ZFS group, then we need to see if a resource
1802 		 * name is being set. If so, bail with
1803 		 * SA_PROP_SHARE_ONLY, so we come back in with a share
1804 		 * instead of a group.
1805 		 */
1806 		if (iszfs ||
1807 		    strncmp(options, "name=", sizeof ("name=") - 1) == 0 ||
1808 		    strstr(options, ",name=") != NULL) {
1809 			return (SA_PROP_SHARE_ONLY);
1810 		}
1811 	}
1812 
1813 	/* do we have an existing optionset? */
1814 	optionset = sa_get_optionset(group, "smb");
1815 	if (optionset == NULL) {
1816 		/* didn't find existing optionset so create one */
1817 		optionset = sa_create_optionset(group, "smb");
1818 		if (optionset == NULL)
1819 			return (SA_NO_MEMORY);
1820 	} else {
1821 		/*
1822 		 * If an optionset already exists, we've come through
1823 		 * twice so ignore the second time.
1824 		 */
1825 		return (ret);
1826 	}
1827 
1828 	/* We need a copy of options for the next part. */
1829 	dup = strdup(options);
1830 	if (dup == NULL)
1831 		return (SA_NO_MEMORY);
1832 
1833 	/*
1834 	 * SMB properties are straightforward and are strings,
1835 	 * integers or booleans.  Properties are separated by
1836 	 * commas. It will be necessary to parse quotes due to some
1837 	 * strings not having a restricted characters set.
1838 	 *
1839 	 * Note that names will create a resource. For now, if there
1840 	 * is a set of properties "before" the first name="", those
1841 	 * properties will be placed on the group.
1842 	 */
1843 	persist = sa_is_persistent(group);
1844 	base = dup;
1845 	token = dup;
1846 	lasts = NULL;
1847 	while (token != NULL && ret == SA_OK) {
1848 		ret = SA_OK;
1849 		token = strtok_r(base, ",", &lasts);
1850 		base = NULL;
1851 		if (token != NULL) {
1852 			char *value;
1853 			/*
1854 			 * All SMB properties have values so there
1855 			 * MUST be an '=' character.  If it doesn't,
1856 			 * it is a syntax error.
1857 			 */
1858 			value = strchr(token, '=');
1859 			if (value != NULL) {
1860 				*value++ = '\0';
1861 			} else {
1862 				ret = SA_SYNTAX_ERR;
1863 				break;
1864 			}
1865 			/*
1866 			 * We may need to handle a "name" property
1867 			 * that is a ZFS imposed resource name. Each
1868 			 * name would trigger getting a new "resource"
1869 			 * to put properties on. For now, assume no
1870 			 * "name" property for special handling.
1871 			 */
1872 
1873 			if (strcmp(token, SHOPT_NAME) == 0) {
1874 				char *prefix;
1875 				char *name = NULL;
1876 				/*
1877 				 * We have a name, so now work on the
1878 				 * resource level. We have a "share"
1879 				 * in "group" due to the caller having
1880 				 * added it. If we are called with a
1881 				 * group, the check for group/share
1882 				 * at the beginning of this function
1883 				 * will bail out the parse if there is a
1884 				 * "name" but no share.
1885 				 */
1886 				if (!iszfs) {
1887 					ret = SA_SYNTAX_ERR;
1888 					break;
1889 				}
1890 				/*
1891 				 * Make sure the parent group has the
1892 				 * "prefix" property since we will
1893 				 * need to use this for constructing
1894 				 * inherited name= values.
1895 				 */
1896 				prefix = sa_get_group_attr(parent, "prefix");
1897 				if (prefix == NULL) {
1898 					prefix = sa_get_group_attr(parent,
1899 					    "name");
1900 					if (prefix != NULL) {
1901 						(void) sa_set_group_attr(parent,
1902 						    "prefix", prefix);
1903 					}
1904 				}
1905 				name = fix_resource_name((sa_share_t)group,
1906 				    value, prefix);
1907 				if (name != NULL) {
1908 					resource = sa_add_resource(
1909 					    (sa_share_t)group, name,
1910 					    SA_SHARE_TRANSIENT, &ret);
1911 					sa_free_attr_string(name);
1912 				} else {
1913 					ret = SA_NO_MEMORY;
1914 				}
1915 				if (prefix != NULL)
1916 					sa_free_attr_string(prefix);
1917 
1918 				/* A resource level optionset is needed */
1919 
1920 				need_optionset = 1;
1921 				if (resource == NULL) {
1922 					ret = SA_NO_MEMORY;
1923 					break;
1924 				}
1925 				continue;
1926 			}
1927 
1928 			if (iszfs && strcmp(token, SHOPT_DESCRIPTION) == 0) {
1929 				if (resource == NULL)
1930 					(void) sa_set_share_description(
1931 					    (sa_share_t)group, value);
1932 				else
1933 					(void) sa_set_resource_description(
1934 					    resource, value);
1935 				continue;
1936 			}
1937 
1938 			if (need_optionset) {
1939 				optionset = sa_create_optionset(resource,
1940 				    "smb");
1941 				need_optionset = 0;
1942 			}
1943 
1944 			prop = sa_create_property(token, value);
1945 			if (prop == NULL)
1946 				ret = SA_NO_MEMORY;
1947 			else
1948 				ret = sa_add_property(optionset, prop);
1949 			if (ret != SA_OK)
1950 				break;
1951 			if (!iszfs)
1952 				ret = sa_commit_properties(optionset, !persist);
1953 		}
1954 	}
1955 	free(dup);
1956 	return (ret);
1957 }
1958 
1959 /*
1960  * smb_sprint_option(rbuff, rbuffsize, incr, prop, sep)
1961  *
1962  * provides a mechanism to format SMB properties into legacy output
1963  * format. If the buffer would overflow, it is reallocated and grown
1964  * as appropriate. Special cases of converting internal form of values
1965  * to those used by "share" are done. this function does one property
1966  * at a time.
1967  */
1968 
1969 static void
1970 smb_sprint_option(char **rbuff, size_t *rbuffsize, size_t incr,
1971     sa_property_t prop, int sep)
1972 {
1973 	char *name;
1974 	char *value;
1975 	int curlen;
1976 	char *buff = *rbuff;
1977 	size_t buffsize = *rbuffsize;
1978 
1979 	name = sa_get_property_attr(prop, "type");
1980 	value = sa_get_property_attr(prop, "value");
1981 	if (buff != NULL)
1982 		curlen = strlen(buff);
1983 	else
1984 		curlen = 0;
1985 	if (name != NULL) {
1986 		int len;
1987 		len = strlen(name) + sep;
1988 
1989 		/*
1990 		 * A future RFE would be to replace this with more
1991 		 * generic code and to possibly handle more types.
1992 		 *
1993 		 * For now, everything else is treated as a string. If
1994 		 * we get any properties that aren't exactly
1995 		 * name/value pairs, we may need to
1996 		 * interpret/transform.
1997 		 */
1998 		if (value != NULL)
1999 			len += 1 + strlen(value);
2000 
2001 		while (buffsize <= (curlen + len)) {
2002 			/* need more room */
2003 			buffsize += incr;
2004 			buff = realloc(buff, buffsize);
2005 			*rbuff = buff;
2006 			*rbuffsize = buffsize;
2007 			if (buff == NULL) {
2008 				/* realloc failed so free everything */
2009 				if (*rbuff != NULL)
2010 					free(*rbuff);
2011 				goto err;
2012 			}
2013 		}
2014 		if (buff == NULL)
2015 			goto err;
2016 		(void) snprintf(buff + curlen, buffsize - curlen,
2017 		    "%s%s=%s", sep ? "," : "",
2018 		    name, value != NULL ? value : "\"\"");
2019 
2020 	}
2021 err:
2022 	if (name != NULL)
2023 		sa_free_attr_string(name);
2024 	if (value != NULL)
2025 		sa_free_attr_string(value);
2026 }
2027 
2028 /*
2029  * smb_format_resource_options(resource, hier)
2030  *
2031  * format all the options on the group into a flattened option
2032  * string. If hier is non-zero, walk up the tree to get inherited
2033  * options.
2034  */
2035 
2036 static char *
2037 smb_format_options(sa_group_t group, int hier)
2038 {
2039 	sa_optionset_t options = NULL;
2040 	sa_property_t prop;
2041 	int sep = 0;
2042 	char *buff;
2043 	size_t buffsize;
2044 
2045 
2046 	buff = malloc(OPT_CHUNK);
2047 	if (buff == NULL)
2048 		return (NULL);
2049 
2050 	buff[0] = '\0';
2051 	buffsize = OPT_CHUNK;
2052 
2053 	/*
2054 	 * We may have a an optionset relative to this item. format
2055 	 * these if we find them and then add any security definitions.
2056 	 */
2057 
2058 	options = sa_get_derived_optionset(group, "smb", hier);
2059 
2060 	/*
2061 	 * do the default set first but skip any option that is also
2062 	 * in the protocol specific optionset.
2063 	 */
2064 	if (options != NULL) {
2065 		for (prop = sa_get_property(options, NULL);
2066 		    prop != NULL; prop = sa_get_next_property(prop)) {
2067 			/*
2068 			 * use this one since we skipped any
2069 			 * of these that were also in
2070 			 * optdefault
2071 			 */
2072 			smb_sprint_option(&buff, &buffsize, OPT_CHUNK,
2073 			    prop, sep);
2074 			if (buff == NULL) {
2075 				/*
2076 				 * buff could become NULL if there
2077 				 * isn't enough memory for
2078 				 * smb_sprint_option to realloc()
2079 				 * as necessary. We can't really
2080 				 * do anything about it at this
2081 				 * point so we return NULL.  The
2082 				 * caller should handle the
2083 				 * failure.
2084 				 */
2085 				if (options != NULL)
2086 					sa_free_derived_optionset(
2087 					    options);
2088 				return (buff);
2089 			}
2090 			sep = 1;
2091 		}
2092 	}
2093 
2094 	if (options != NULL)
2095 		sa_free_derived_optionset(options);
2096 	return (buff);
2097 }
2098 
2099 /*
2100  * smb_rename_resource(resource, newname)
2101  *
2102  * Change the current exported name of the resource to newname.
2103  */
2104 /*ARGSUSED*/
2105 int
2106 smb_rename_resource(sa_handle_t handle, sa_resource_t resource, char *newname)
2107 {
2108 	int ret = SA_OK;
2109 	int err;
2110 	char *oldname;
2111 
2112 	if (!smb_isonline())
2113 		return (SA_OK);
2114 
2115 	oldname = sa_get_resource_attr(resource, "name");
2116 	if (oldname == NULL)
2117 		return (SA_NO_SUCH_RESOURCE);
2118 
2119 	err = smb_share_rename(oldname, newname);
2120 
2121 	sa_free_attr_string(oldname);
2122 
2123 	/* improve error values somewhat */
2124 	switch (err) {
2125 	case NERR_Success:
2126 		break;
2127 	case NERR_InternalError:
2128 		ret = SA_SYSTEM_ERR;
2129 		break;
2130 	case NERR_DuplicateShare:
2131 		ret = SA_DUPLICATE_NAME;
2132 		break;
2133 	default:
2134 		ret = SA_CONFIG_ERR;
2135 		break;
2136 	}
2137 
2138 	return (ret);
2139 }
2140 
2141 static int
2142 smb_build_shareinfo(sa_share_t share, sa_resource_t resource, smb_share_t *si)
2143 {
2144 	sa_optionset_t opts;
2145 	char *path;
2146 	char *rname;
2147 	char *val = NULL;
2148 	char csc_value[SMB_CSC_BUFSZ];
2149 	char strbuf[sizeof ("required")];
2150 
2151 	bzero(si, sizeof (smb_share_t));
2152 
2153 	if ((path = sa_get_share_attr(share, "path")) == NULL)
2154 		return (SA_NO_SUCH_PATH);
2155 
2156 	if ((rname = sa_get_resource_attr(resource, "name")) == NULL) {
2157 		sa_free_attr_string(path);
2158 		return (SA_NO_SUCH_RESOURCE);
2159 	}
2160 
2161 	(void) strlcpy(si->shr_path, path, sizeof (si->shr_path));
2162 	(void) strlcpy(si->shr_name, rname, sizeof (si->shr_name));
2163 	sa_free_attr_string(path);
2164 	sa_free_attr_string(rname);
2165 
2166 	val = sa_get_resource_description(resource);
2167 	if (val == NULL)
2168 		val = sa_get_share_description(share);
2169 
2170 	if (val != NULL) {
2171 		(void) strlcpy(si->shr_cmnt, val, sizeof (si->shr_cmnt));
2172 		sa_free_share_description(val);
2173 	}
2174 
2175 	si->shr_flags = (sa_is_persistent(share))
2176 	    ? SMB_SHRF_PERM : SMB_SHRF_TRANS;
2177 
2178 	opts = sa_get_derived_optionset(resource, SMB_PROTOCOL_NAME, 1);
2179 	if (opts == NULL)
2180 		return (SA_OK);
2181 
2182 	if (smb_saprop_getbool(opts, SHOPT_CATIA, B_FALSE))
2183 		si->shr_flags |= SMB_SHRF_CATIA;
2184 
2185 	if (smb_saprop_getbool(opts, SHOPT_ABE, B_FALSE))
2186 		si->shr_flags |= SMB_SHRF_ABE;
2187 
2188 	if (smb_saprop_getbool(opts, SHOPT_GUEST, B_FALSE))
2189 		si->shr_flags |= SMB_SHRF_GUEST_OK;
2190 
2191 	if (smb_saprop_getbool(opts, SHOPT_DFSROOT, B_FALSE))
2192 		si->shr_flags |= SMB_SHRF_DFSROOT;
2193 
2194 	if (smb_saprop_getbool(opts, SHOPT_FSO, B_FALSE))
2195 		si->shr_flags |= SMB_SHRF_FSO;
2196 
2197 	/* Quotas are enabled by default. */
2198 	if (smb_saprop_getbool(opts, SHOPT_QUOTAS, B_TRUE))
2199 		si->shr_flags |= SMB_SHRF_QUOTAS;
2200 
2201 	if (smb_saprop_getstr(opts, SHOPT_ENCRYPT, strbuf, sizeof (strbuf)))
2202 		smb_cfg_set_require(strbuf, &si->shr_encrypt);
2203 
2204 	(void) smb_saprop_getstr(opts, SHOPT_AD_CONTAINER, si->shr_container,
2205 	    sizeof (si->shr_container));
2206 
2207 	if (smb_saprop_getstr(opts, SHOPT_CSC, csc_value, sizeof (csc_value)))
2208 		smb_csc_option(csc_value, si);
2209 
2210 	if (smb_saprop_getstr(opts, SHOPT_RO, si->shr_access_ro,
2211 	    sizeof (si->shr_access_ro)))
2212 		si->shr_flags |= SMB_SHRF_ACC_RO;
2213 
2214 	if (smb_saprop_getstr(opts, SHOPT_RW, si->shr_access_rw,
2215 	    sizeof (si->shr_access_rw)))
2216 		si->shr_flags |= SMB_SHRF_ACC_RW;
2217 
2218 	if (smb_saprop_getstr(opts, SHOPT_NONE, si->shr_access_none,
2219 	    sizeof (si->shr_access_none)))
2220 		si->shr_flags |= SMB_SHRF_ACC_NONE;
2221 
2222 	sa_free_derived_optionset(opts);
2223 	return (SA_OK);
2224 }
2225 
2226 /*
2227  * Map a client-side caching (CSC) option to the appropriate share
2228  * flag.  Only one option is allowed; an error will be logged if
2229  * multiple options have been specified.  We don't need to do anything
2230  * about multiple values here because the SRVSVC will not recognize
2231  * a value containing multiple flags and will return the default value.
2232  *
2233  * If the option value is not recognized, it will be ignored: invalid
2234  * values will typically be caught and rejected by sharemgr.
2235  */
2236 static void
2237 smb_csc_option(const char *value, smb_share_t *si)
2238 {
2239 	char buf[SMB_CSC_BUFSZ];
2240 	int i;
2241 
2242 	for (i = 0; i < (sizeof (cscopt) / sizeof (cscopt[0])); ++i) {
2243 		if (strcasecmp(value, cscopt[i].value) == 0) {
2244 			si->shr_flags |= cscopt[i].flag;
2245 			break;
2246 		}
2247 	}
2248 
2249 	switch (si->shr_flags & SMB_SHRF_CSC_MASK) {
2250 	case 0:
2251 	case SMB_SHRF_CSC_DISABLED:
2252 	case SMB_SHRF_CSC_MANUAL:
2253 	case SMB_SHRF_CSC_AUTO:
2254 	case SMB_SHRF_CSC_VDO:
2255 		break;
2256 
2257 	default:
2258 		buf[0] = '\0';
2259 
2260 		for (i = 0; i < (sizeof (cscopt) / sizeof (cscopt[0])); ++i) {
2261 			if (si->shr_flags & cscopt[i].flag) {
2262 				(void) strlcat(buf, " ", SMB_CSC_BUFSZ);
2263 				(void) strlcat(buf, cscopt[i].value,
2264 				    SMB_CSC_BUFSZ);
2265 			}
2266 		}
2267 
2268 		syslog(LOG_ERR, "csc option conflict:%s", buf);
2269 		break;
2270 	}
2271 }
2272 
2273 /*
2274  * Return the option name for the first CSC flag (there should be only
2275  * one) encountered in the share flags.
2276  */
2277 static char *
2278 smb_csc_name(const smb_share_t *si)
2279 {
2280 	int i;
2281 
2282 	for (i = 0; i < (sizeof (cscopt) / sizeof (cscopt[0])); ++i) {
2283 		if (si->shr_flags & cscopt[i].flag)
2284 			return (cscopt[i].value);
2285 	}
2286 
2287 	return (NULL);
2288 }
2289 
2290 /*
2291  * smb_get_defaultgrp
2292  *
2293  * If default group for CIFS shares (i.e. "smb") exists
2294  * then it will return the group handle, otherwise it will
2295  * create the group and return the handle.
2296  *
2297  * All the shares created by CIFS clients (this is only possible
2298  * via RPC) will be added to "smb" groups.
2299  */
2300 static sa_group_t
2301 smb_get_defaultgrp(sa_handle_t handle)
2302 {
2303 	sa_group_t group = NULL;
2304 	int err;
2305 
2306 	group = sa_get_group(handle, SMB_DEFAULT_SHARE_GROUP);
2307 	if (group != NULL)
2308 		return (group);
2309 
2310 	group = sa_create_group(handle, SMB_DEFAULT_SHARE_GROUP, &err);
2311 	if (group == NULL)
2312 		return (NULL);
2313 
2314 	if (sa_create_optionset(group, SMB_DEFAULT_SHARE_GROUP) == NULL) {
2315 		(void) sa_remove_group(group);
2316 		group = NULL;
2317 	}
2318 
2319 	return (group);
2320 }
2321 
2322 /*
2323  * Checks to see if the command args are the supported substitution specifier.
2324  * i.e. <cmd> %U %S
2325  */
2326 static int
2327 cmd_validator(int index, char *value)
2328 {
2329 	char cmd[MAXPATHLEN];
2330 	char *ptr, *v;
2331 	boolean_t skip_cmdname;
2332 
2333 	if (string_length_check_validator(index, value) != SA_OK)
2334 		return (SA_BAD_VALUE);
2335 
2336 	if (*value == '\0')
2337 		return (SA_OK);
2338 
2339 	(void) strlcpy(cmd, value, sizeof (cmd));
2340 
2341 	ptr = cmd;
2342 	skip_cmdname = B_TRUE;
2343 	do {
2344 		if ((v = strsep(&ptr, " ")) == NULL)
2345 			break;
2346 
2347 		if (*v != '\0') {
2348 
2349 			if (skip_cmdname) {
2350 				skip_cmdname = B_FALSE;
2351 				continue;
2352 			}
2353 
2354 			if ((strlen(v) != 2) || *v != '%')
2355 				return (SA_BAD_VALUE);
2356 
2357 			if (strpbrk(v, SMB_VALID_SUB_CHRS) == NULL)
2358 				return (SA_BAD_VALUE);
2359 		}
2360 
2361 	} while (v != NULL);
2362 
2363 	/*
2364 	 * If skip_cmdname is still true then the string contains
2365 	 * only spaces.  Don't allow such a string.
2366 	 */
2367 	if (skip_cmdname)
2368 		return (SA_BAD_VALUE);
2369 
2370 	return (SA_OK);
2371 }
2372 
2373 /*ARGSUSED*/
2374 static int
2375 disposition_validator(int index, char *value)
2376 {
2377 	if (value == NULL)
2378 		return (SA_BAD_VALUE);
2379 
2380 	if (*value == '\0')
2381 		return (SA_OK);
2382 
2383 	if ((strcasecmp(value, SMB_EXEC_DISP_CONTINUE) == 0) ||
2384 	    (strcasecmp(value, SMB_EXEC_DISP_TERMINATE) == 0))
2385 		return (SA_OK);
2386 
2387 	return (SA_BAD_VALUE);
2388 }
2389 
2390 /*ARGSUSED*/
2391 static int
2392 max_protocol_validator(int index, char *value)
2393 {
2394 	if (value == NULL)
2395 		return (SA_BAD_VALUE);
2396 
2397 	if (*value == '\0')
2398 		return (SA_OK);
2399 
2400 	if (smb_config_check_protocol(value) == 0)
2401 		return (SA_OK);
2402 
2403 	return (SA_BAD_VALUE);
2404 
2405 }
2406 
2407 /*
2408  * Updates the optionset properties of the share resource.
2409  * The properties are given as a list of name-value pair.
2410  * The name argument should be the optionset property name and the value
2411  * should be a valid value for the specified property.
2412  *
2413  * When calling this function for permanent shares, the caller must also
2414  * call sa_commit_properties() to commit the changes to SMF.
2415  */
2416 static int
2417 smb_update_optionset_props(sa_handle_t handle, sa_resource_t resource,
2418     nvlist_t *nvl)
2419 {
2420 	sa_property_t prop;
2421 	sa_optionset_t opts;
2422 	int err = SA_OK;
2423 	nvpair_t *cur;
2424 	char *name, *val;
2425 
2426 	if ((opts = sa_get_optionset(resource, SMB_PROTOCOL_NAME)) == NULL) {
2427 		opts = sa_create_optionset(resource, SMB_PROTOCOL_NAME);
2428 		if (opts == NULL)
2429 			return (SA_CONFIG_ERR);
2430 	}
2431 
2432 	cur = nvlist_next_nvpair(nvl, NULL);
2433 	while (cur != NULL) {
2434 		name = nvpair_name(cur);
2435 		err = nvpair_value_string(cur, &val);
2436 		if ((err != 0) || (name == NULL) || (val == NULL)) {
2437 			err = SA_CONFIG_ERR;
2438 			break;
2439 		}
2440 
2441 		prop = NULL;
2442 		if ((prop = sa_get_property(opts, name)) == NULL) {
2443 			prop = sa_create_property(name, val);
2444 			if (prop != NULL) {
2445 				err = sa_valid_property(handle, opts,
2446 				    SMB_PROTOCOL_NAME, prop);
2447 				if (err != SA_OK) {
2448 					(void) sa_remove_property(prop);
2449 					break;
2450 				}
2451 			}
2452 			err = sa_add_property(opts, prop);
2453 			if (err != SA_OK)
2454 				break;
2455 		} else {
2456 			err = sa_update_property(prop, val);
2457 			if (err != SA_OK)
2458 				break;
2459 		}
2460 
2461 		cur = nvlist_next_nvpair(nvl, cur);
2462 	}
2463 
2464 	return (err);
2465 }
2466 
2467 static boolean_t
2468 smb_saprop_getbool(sa_optionset_t opts, char *propname, boolean_t def)
2469 {
2470 	sa_property_t prop;
2471 	char *val;
2472 	boolean_t ret = def;
2473 
2474 	prop = sa_get_property(opts, propname);
2475 	if ((val = sa_get_property_attr(prop, "value")) != NULL) {
2476 		if (def) {
2477 			/* Default is true, ret false if... */
2478 			if ((strcasecmp(val, "false") == 0) ||
2479 			    (strcmp(val, "0") == 0))
2480 				ret = B_FALSE;
2481 		} else {
2482 			/* Default is false, ret true if... */
2483 			if ((strcasecmp(val, "true") == 0) ||
2484 			    (strcmp(val, "1") == 0))
2485 				ret = B_TRUE;
2486 		}
2487 		free(val);
2488 	}
2489 
2490 	return (ret);
2491 }
2492 
2493 static boolean_t
2494 smb_saprop_getstr(sa_optionset_t opts, char *propname, char *buf, size_t bufsz)
2495 {
2496 	sa_property_t prop;
2497 	char *val;
2498 
2499 	prop = sa_get_property(opts, propname);
2500 	if ((val = sa_get_property_attr(prop, "value")) != NULL) {
2501 		(void) strlcpy(buf, val, bufsz);
2502 		free(val);
2503 		return (B_TRUE);
2504 	}
2505 
2506 	return (B_FALSE);
2507 }
2508