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 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, protocol_validator,
922 	    SMB_REFRESH_REFRESH },
923 	{ SMB_CI_ENCRYPT, 0, MAX_VALUE_BUFLEN, require_validator,
924 	    SMB_REFRESH_REFRESH },
925 	{ SMB_CI_MIN_PROTOCOL, 0, MAX_VALUE_BUFLEN, protocol_validator,
926 	    SMB_REFRESH_REFRESH },
927 	{ SMB_CI_BYPASS_TRAVERSE_CHECKING, 0, 0, true_false_validator,
928 	    SMB_REFRESH_REFRESH },
929 	{ SMB_CI_OPLOCK_ENABLE, 0, 0, true_false_validator,
930 	    SMB_REFRESH_REFRESH },
931 };
932 
933 #define	SMB_OPT_NUM \
934 	(sizeof (smb_proto_options) / sizeof (smb_proto_options[0]))
935 
936 static int
937 require_validator(int index, char *value)
938 {
939 	if (string_length_check_validator(index, value) != SA_OK)
940 		return (SA_BAD_VALUE);
941 
942 	if (strcmp(value, "required") == 0)
943 		return (SA_OK);
944 
945 	if (strcmp(value, "disabled") == 0)
946 		return (SA_OK);
947 
948 	if (strcmp(value, "enabled") == 0)
949 		return (SA_OK);
950 
951 	return (SA_BAD_VALUE);
952 }
953 
954 /*
955  * Check the range of value as int range.
956  */
957 static int
958 range_check_validator(int index, char *value)
959 {
960 	int ret = SA_OK;
961 
962 	if (!is_a_number(value)) {
963 		ret = SA_BAD_VALUE;
964 	} else {
965 		int val;
966 		val = strtoul(value, NULL, 0);
967 		if (val < smb_proto_options[index].minval ||
968 		    val > smb_proto_options[index].maxval)
969 			ret = SA_BAD_VALUE;
970 	}
971 	return (ret);
972 }
973 
974 /*
975  * Check the range of value as int range.
976  */
977 static int
978 range_check_validator_zero_ok(int index, char *value)
979 {
980 	int ret = SA_OK;
981 
982 	if (!is_a_number(value)) {
983 		ret = SA_BAD_VALUE;
984 	} else {
985 		int val;
986 		val = strtoul(value, NULL, 0);
987 		if (val == 0)
988 			ret = SA_OK;
989 		else {
990 			if (val < smb_proto_options[index].minval ||
991 			    val > smb_proto_options[index].maxval)
992 			ret = SA_BAD_VALUE;
993 		}
994 	}
995 	return (ret);
996 }
997 
998 /*
999  * Check the length of the string
1000  */
1001 static int
1002 string_length_check_validator(int index, char *value)
1003 {
1004 	int ret = SA_OK;
1005 
1006 	if (value == NULL)
1007 		return (SA_BAD_VALUE);
1008 	if (strlen(value) > smb_proto_options[index].maxval)
1009 		ret = SA_BAD_VALUE;
1010 	return (ret);
1011 }
1012 
1013 /*
1014  * Check yes/no
1015  */
1016 /*ARGSUSED*/
1017 static int
1018 true_false_validator(int index, char *value)
1019 {
1020 	if (value == NULL)
1021 		return (SA_BAD_VALUE);
1022 	if ((strcasecmp(value, "true") == 0) ||
1023 	    (strcasecmp(value, "false") == 0))
1024 		return (SA_OK);
1025 	return (SA_BAD_VALUE);
1026 }
1027 
1028 /*
1029  * If printing support is compiled in, this is the same as:
1030  * true_false_validator.  Otherwise, only allow false.
1031  */
1032 /*ARGSUSED*/
1033 static int
1034 print_enable_validator(int index, char *value)
1035 {
1036 	if (value == NULL)
1037 		return (SA_BAD_VALUE);
1038 
1039 #ifdef	HAVE_CUPS
1040 	if (strcasecmp(value, "true") == 0)
1041 		return (SA_OK);
1042 #endif
1043 	if (strcasecmp(value, "false") == 0)
1044 		return (SA_OK);
1045 
1046 	return (SA_BAD_VALUE);
1047 }
1048 
1049 /*
1050  * Check IP v4 address.
1051  */
1052 /*ARGSUSED*/
1053 static int
1054 ipv4_validator(int index, char *value)
1055 {
1056 	char sbytes[16];
1057 
1058 	if (value == NULL)
1059 		return (SA_OK);
1060 
1061 	if (strlen(value) == 0)
1062 		return (SA_OK);
1063 
1064 	if (inet_pton(AF_INET, value, (void *)sbytes) != 1)
1065 		return (SA_BAD_VALUE);
1066 
1067 	return (SA_OK);
1068 }
1069 
1070 /*
1071  * Check that the specified name is an IP address (v4 or v6) or a hostname.
1072  * Per RFC 1035 and 1123, names may contain alphanumeric characters, hyphens
1073  * and dots.  The first and last character of a label must be alphanumeric.
1074  * Interior characters may be alphanumeric or hypens.
1075  *
1076  * Domain names should not contain underscores but we allow them because
1077  * Windows names are often in non-compliance with this rule.
1078  */
1079 /*ARGSUSED*/
1080 static int
1081 hostname_validator(int index, char *value)
1082 {
1083 	char		sbytes[INET6_ADDRSTRLEN];
1084 	boolean_t	new_label = B_TRUE;
1085 	char		*p;
1086 	char		label_terminator;
1087 	int		len;
1088 
1089 	if (value == NULL)
1090 		return (SA_OK);
1091 
1092 	if ((len = strlen(value)) == 0)
1093 		return (SA_OK);
1094 
1095 	if (inet_pton(AF_INET, value, (void *)sbytes) == 1)
1096 		return (SA_OK);
1097 
1098 	if (inet_pton(AF_INET6, value, (void *)sbytes) == 1)
1099 		return (SA_OK);
1100 
1101 	if (len >= MAXHOSTNAMELEN)
1102 		return (SA_BAD_VALUE);
1103 
1104 	if (strspn(value, "0123456789.") == len)
1105 		return (SA_BAD_VALUE);
1106 
1107 	label_terminator = *value;
1108 
1109 	for (p = value; *p != '\0'; ++p) {
1110 		if (new_label) {
1111 			if (!isalnum(*p))
1112 				return (SA_BAD_VALUE);
1113 			new_label = B_FALSE;
1114 			label_terminator = *p;
1115 			continue;
1116 		}
1117 
1118 		if (*p == '.') {
1119 			if (!isalnum(label_terminator))
1120 				return (SA_BAD_VALUE);
1121 			new_label = B_TRUE;
1122 			label_terminator = *p;
1123 			continue;
1124 		}
1125 
1126 		label_terminator = *p;
1127 
1128 		if (isalnum(*p) || *p == '-' || *p == '_')
1129 			continue;
1130 
1131 		return (SA_BAD_VALUE);
1132 	}
1133 
1134 	if (!isalnum(label_terminator))
1135 		return (SA_BAD_VALUE);
1136 
1137 	return (SA_OK);
1138 }
1139 
1140 /*
1141  * Call back function for dlpi_walk.
1142  * Returns TRUE if interface name exists on the host.
1143  */
1144 static boolean_t
1145 smb_get_interface(const char *ifname, void *arg)
1146 {
1147 	smb_hostifs_walker_t *iterp = arg;
1148 
1149 	iterp->hiw_matchfound = (strcmp(ifname, iterp->hiw_ifname) == 0);
1150 
1151 	return (iterp->hiw_matchfound);
1152 }
1153 
1154 /*
1155  * Checks to see if the input interface exists on the host.
1156  * Returns B_TRUE if the match is found, B_FALSE otherwise.
1157  */
1158 static boolean_t
1159 smb_validate_interface(const char *ifname)
1160 {
1161 	smb_hostifs_walker_t	iter;
1162 
1163 	if ((ifname == NULL) || (*ifname == '\0'))
1164 		return (B_FALSE);
1165 
1166 	iter.hiw_ifname = ifname;
1167 	iter.hiw_matchfound = B_FALSE;
1168 	dlpi_walk(smb_get_interface, &iter, 0);
1169 
1170 	return (iter.hiw_matchfound);
1171 }
1172 
1173 /*
1174  * Check valid interfaces. Interface names value can be NULL or empty.
1175  * Returns SA_BAD_VALUE if interface cannot be found on the host.
1176  */
1177 /*ARGSUSED*/
1178 static int
1179 interface_validator(int index, char *value)
1180 {
1181 	char buf[16];
1182 	int ret = SA_OK;
1183 	char *ifname, *tmp, *p;
1184 
1185 	if (value == NULL || *value == '\0')
1186 		return (ret);
1187 
1188 	if (strlen(value) > MAX_VALUE_BUFLEN)
1189 		return (SA_BAD_VALUE);
1190 
1191 	if ((p = strdup(value)) == NULL)
1192 		return (SA_NO_MEMORY);
1193 
1194 	tmp = p;
1195 	while ((ifname = strsep(&tmp, ",")) != NULL) {
1196 		if (*ifname == '\0') {
1197 			ret = SA_BAD_VALUE;
1198 			break;
1199 		}
1200 
1201 		if (!smb_validate_interface(ifname)) {
1202 			if (inet_pton(AF_INET, ifname, (void *)buf) == 0) {
1203 				ret = SA_BAD_VALUE;
1204 				break;
1205 			}
1206 		}
1207 	}
1208 
1209 	free(p);
1210 	return (ret);
1211 }
1212 
1213 /*
1214  * Check path
1215  */
1216 /*ARGSUSED*/
1217 static int
1218 path_validator(int index, char *path)
1219 {
1220 	struct stat buffer;
1221 	int fd, status;
1222 
1223 	if (path == NULL)
1224 		return (SA_BAD_VALUE);
1225 
1226 	fd = open(path, O_RDONLY);
1227 	if (fd < 0)
1228 		return (SA_BAD_VALUE);
1229 
1230 	status = fstat(fd, &buffer);
1231 	(void) close(fd);
1232 
1233 	if (status < 0)
1234 		return (SA_BAD_VALUE);
1235 
1236 	if (buffer.st_mode & S_IFDIR)
1237 		return (SA_OK);
1238 	return (SA_BAD_VALUE);
1239 }
1240 
1241 /*
1242  * the protoset holds the defined options so we don't have to read
1243  * them multiple times
1244  */
1245 static sa_protocol_properties_t protoset;
1246 
1247 static int
1248 findprotoopt(char *name)
1249 {
1250 	int i;
1251 	char *sc_name;
1252 
1253 	for (i = 0; i < SMB_OPT_NUM; i++) {
1254 		sc_name = smb_config_getname(smb_proto_options[i].smb_index);
1255 		if (strcasecmp(sc_name, name) == 0)
1256 			return (i);
1257 	}
1258 
1259 	return (-1);
1260 }
1261 
1262 /*
1263  * smb_load_proto_properties()
1264  *
1265  * read the smb config values from SMF.
1266  */
1267 
1268 static int
1269 smb_load_proto_properties()
1270 {
1271 	sa_property_t prop;
1272 	char value[MAX_VALUE_BUFLEN];
1273 	char *name;
1274 	int index;
1275 	int ret = SA_OK;
1276 	int rc;
1277 
1278 	protoset = sa_create_protocol_properties(SMB_PROTOCOL_NAME);
1279 	if (protoset == NULL)
1280 		return (SA_NO_MEMORY);
1281 
1282 	for (index = 0; index < SMB_OPT_NUM && ret == SA_OK; index++) {
1283 		rc = smb_config_get(smb_proto_options[index].smb_index,
1284 		    value, sizeof (value));
1285 		if (rc != SMBD_SMF_OK)
1286 			continue;
1287 		name = smb_config_getname(smb_proto_options[index].smb_index);
1288 		prop = sa_create_property(name, value);
1289 		if (prop != NULL)
1290 			ret = sa_add_protocol_property(protoset, prop);
1291 		else
1292 			ret = SA_NO_MEMORY;
1293 	}
1294 	return (ret);
1295 }
1296 
1297 /*
1298  * smb_share_init()
1299  *
1300  * Initialize the smb plugin.
1301  */
1302 
1303 static int
1304 smb_share_init(void)
1305 {
1306 	if (sa_plugin_ops.sa_init != smb_share_init)
1307 		return (SA_SYSTEM_ERR);
1308 
1309 	smb_share_door_clnt_init();
1310 	return (smb_load_proto_properties());
1311 }
1312 
1313 /*
1314  * smb_share_fini()
1315  *
1316  */
1317 static void
1318 smb_share_fini(void)
1319 {
1320 	xmlFreeNode(protoset);
1321 	protoset = NULL;
1322 
1323 	smb_share_door_clnt_fini();
1324 }
1325 
1326 /*
1327  * smb_get_proto_set()
1328  *
1329  * Return an optionset with all the protocol specific properties in
1330  * it.
1331  */
1332 static sa_protocol_properties_t
1333 smb_get_proto_set(void)
1334 {
1335 	return (protoset);
1336 }
1337 
1338 /*
1339  * smb_enable_dependencies()
1340  *
1341  * SMBD_DEFAULT_INSTANCE_FMRI may have some dependencies that aren't
1342  * enabled. This will attempt to enable all of them.
1343  */
1344 static void
1345 smb_enable_dependencies(const char *fmri)
1346 {
1347 	scf_handle_t *handle;
1348 	scf_service_t *service;
1349 	scf_instance_t *inst = NULL;
1350 	scf_iter_t *iter;
1351 	scf_property_t *prop;
1352 	scf_value_t *value;
1353 	scf_propertygroup_t *pg;
1354 	scf_scope_t *scope;
1355 	char type[SCFTYPE_LEN];
1356 	char *dependency;
1357 	char *servname;
1358 	int maxlen;
1359 
1360 	/*
1361 	 * Get all required handles and storage.
1362 	 */
1363 	handle = scf_handle_create(SCF_VERSION);
1364 	if (handle == NULL)
1365 		return;
1366 
1367 	if (scf_handle_bind(handle) != 0) {
1368 		scf_handle_destroy(handle);
1369 		return;
1370 	}
1371 
1372 	maxlen = scf_limit(SCF_LIMIT_MAX_VALUE_LENGTH);
1373 	if (maxlen == (ssize_t)-1)
1374 		maxlen = MAXPATHLEN;
1375 
1376 	dependency = malloc(maxlen);
1377 
1378 	service = scf_service_create(handle);
1379 
1380 	iter = scf_iter_create(handle);
1381 
1382 	pg = scf_pg_create(handle);
1383 
1384 	prop = scf_property_create(handle);
1385 
1386 	value = scf_value_create(handle);
1387 
1388 	scope = scf_scope_create(handle);
1389 
1390 	if (service == NULL || iter == NULL || pg == NULL || prop == NULL ||
1391 	    value == NULL || scope == NULL || dependency == NULL)
1392 		goto done;
1393 
1394 	/*
1395 	 *  We passed in the FMRI for the default instance but for
1396 	 *  some things we need the simple form so construct it. Since
1397 	 *  we reuse the storage that dependency points to, we need to
1398 	 *  use the servname early.
1399 	 */
1400 	(void) snprintf(dependency, maxlen, "%s", fmri + sizeof ("svc:"));
1401 	servname = strrchr(dependency, ':');
1402 	if (servname == NULL)
1403 		goto done;
1404 	*servname = '\0';
1405 	servname = dependency;
1406 
1407 	/*
1408 	 * Setup to iterate over the service property groups, only
1409 	 * looking at those that are "dependency" types. The "entity"
1410 	 * property will have the FMRI of the service we are dependent
1411 	 * on.
1412 	 */
1413 	if (scf_handle_get_scope(handle, SCF_SCOPE_LOCAL, scope) != 0)
1414 		goto done;
1415 
1416 	if (scf_scope_get_service(scope, servname, service) != 0)
1417 		goto done;
1418 
1419 	if (scf_iter_service_pgs(iter, service) != 0)
1420 		goto done;
1421 
1422 	while (scf_iter_next_pg(iter, pg) > 0) {
1423 		char *services[2];
1424 		/*
1425 		 * Have a property group for the service. See if it is
1426 		 * a dependency pg and only do operations on those.
1427 		 */
1428 		if (scf_pg_get_type(pg, type, SCFTYPE_LEN) <= 0)
1429 			continue;
1430 
1431 		if (strncmp(type, SCF_GROUP_DEPENDENCY, SCFTYPE_LEN) != 0)
1432 			continue;
1433 		/*
1434 		 * Have a dependency.  Attempt to enable it.
1435 		 */
1436 		if (scf_pg_get_property(pg, SCF_PROPERTY_ENTITIES, prop) != 0)
1437 			continue;
1438 
1439 		if (scf_property_get_value(prop, value) != 0)
1440 			continue;
1441 
1442 		services[1] = NULL;
1443 
1444 		if (scf_value_get_as_string(value, dependency, maxlen) > 0) {
1445 			services[0] = dependency;
1446 			_check_services(services);
1447 		}
1448 	}
1449 
1450 done:
1451 	if (dependency != NULL)
1452 		free(dependency);
1453 	if (value != NULL)
1454 		scf_value_destroy(value);
1455 	if (prop != NULL)
1456 		scf_property_destroy(prop);
1457 	if (pg != NULL)
1458 		scf_pg_destroy(pg);
1459 	if (iter != NULL)
1460 		scf_iter_destroy(iter);
1461 	if (scope != NULL)
1462 		scf_scope_destroy(scope);
1463 	if (inst != NULL)
1464 		scf_instance_destroy(inst);
1465 	if (service != NULL)
1466 		scf_service_destroy(service);
1467 
1468 	(void) scf_handle_unbind(handle);
1469 	scf_handle_destroy(handle);
1470 }
1471 
1472 /*
1473  * How long to wait for service to come online
1474  */
1475 #define	WAIT_FOR_SERVICE	15
1476 
1477 /*
1478  * smb_enable_service()
1479  *
1480  */
1481 static int
1482 smb_enable_service(void)
1483 {
1484 	int i;
1485 	int ret = SA_OK;
1486 	char *service[] = { SMBD_DEFAULT_INSTANCE_FMRI, NULL };
1487 
1488 	if (!smb_isonline()) {
1489 		/*
1490 		 * Attempt to start the idmap, and other dependent
1491 		 * services, first.  If it fails, the SMB service will
1492 		 * ultimately fail so we use that as the error.  If we
1493 		 * don't try to enable idmap, smb won't start the
1494 		 * first time unless the admin has done it
1495 		 * manually. The service could be administratively
1496 		 * disabled so we won't always get started.
1497 		 */
1498 		smb_enable_dependencies(SMBD_DEFAULT_INSTANCE_FMRI);
1499 		_check_services(service);
1500 
1501 		/* Wait for service to come online */
1502 		for (i = 0; i < WAIT_FOR_SERVICE; i++) {
1503 			if (smb_isonline()) {
1504 				ret =  SA_OK;
1505 				break;
1506 			} else if (smb_ismaint()) {
1507 				/* maintenance requires help */
1508 				ret = SA_SYSTEM_ERR;
1509 				break;
1510 			} else {
1511 				/* try another time */
1512 				ret = SA_BUSY;
1513 				(void) sleep(1);
1514 			}
1515 		}
1516 	}
1517 	return (ret);
1518 }
1519 
1520 /*
1521  * smb_validate_proto_prop(index, name, value)
1522  *
1523  * Verify that the property specified by name can take the new
1524  * value. This is a sanity check to prevent bad values getting into
1525  * the default files.
1526  */
1527 static int
1528 smb_validate_proto_prop(int index, char *name, char *value)
1529 {
1530 	if ((name == NULL) || (index < 0))
1531 		return (SA_BAD_VALUE);
1532 
1533 	if (smb_proto_options[index].validator == NULL)
1534 		return (SA_OK);
1535 
1536 	if (smb_proto_options[index].validator(index, value) == SA_OK)
1537 		return (SA_OK);
1538 	return (SA_BAD_VALUE);
1539 }
1540 
1541 /*
1542  * smb_set_proto_prop(prop)
1543  *
1544  * check that prop is valid.
1545  */
1546 /*ARGSUSED*/
1547 static int
1548 smb_set_proto_prop(sa_property_t prop)
1549 {
1550 	int ret = SA_OK;
1551 	char *name;
1552 	char *value;
1553 	int index = -1;
1554 	struct smb_proto_option_defs *opt;
1555 
1556 	name = sa_get_property_attr(prop, "type");
1557 	value = sa_get_property_attr(prop, "value");
1558 	if (name != NULL && value != NULL) {
1559 		index = findprotoopt(name);
1560 		if (index >= 0) {
1561 			/* should test for valid value */
1562 			ret = smb_validate_proto_prop(index, name, value);
1563 			if (ret == SA_OK) {
1564 				opt = &smb_proto_options[index];
1565 
1566 				/* Save to SMF */
1567 				if (smb_config_set(opt->smb_index,
1568 				    value) != 0) {
1569 					ret = SA_BAD_VALUE;
1570 					goto out;
1571 				}
1572 				/*
1573 				 * Specialized refresh mechanisms can
1574 				 * be flagged in the proto_options and
1575 				 * processed here.
1576 				 */
1577 				if (opt->refresh & SMB_REFRESH_REFRESH)
1578 					(void) smf_refresh_instance(
1579 					    SMBD_DEFAULT_INSTANCE_FMRI);
1580 				else if (opt->refresh & SMB_REFRESH_RESTART)
1581 					(void) smf_restart_instance(
1582 					    SMBD_DEFAULT_INSTANCE_FMRI);
1583 			}
1584 		}
1585 	}
1586 
1587 out:
1588 	if (name != NULL)
1589 		sa_free_attr_string(name);
1590 	if (value != NULL)
1591 		sa_free_attr_string(value);
1592 
1593 	return (ret);
1594 }
1595 
1596 /*
1597  * smb_get_status()
1598  *
1599  * What is the current status of the smbd? We use the SMF state here.
1600  * Caller must free the returned value.
1601  */
1602 
1603 static char *
1604 smb_get_status(void)
1605 {
1606 	return (smf_get_state(SMBD_DEFAULT_INSTANCE_FMRI));
1607 }
1608 
1609 /*
1610  * This protocol plugin require resource names
1611  */
1612 static uint64_t
1613 smb_share_features(void)
1614 {
1615 	return (SA_FEATURE_RESOURCE | SA_FEATURE_ALLOWSUBDIRS |
1616 	    SA_FEATURE_ALLOWPARDIRS | SA_FEATURE_SERVER);
1617 }
1618 
1619 /*
1620  * This should be used to convert smb_share_t to sa_resource_t
1621  * Should only be needed to build transient shares/resources to be
1622  * supplied to sharemgr to display.
1623  */
1624 static int
1625 smb_add_transient(sa_handle_t handle, smb_share_t *si)
1626 {
1627 	int err;
1628 	sa_share_t share;
1629 	sa_group_t group;
1630 	sa_resource_t resource;
1631 	nvlist_t *nvl;
1632 	char *opt;
1633 
1634 	if (si == NULL)
1635 		return (SA_INVALID_NAME);
1636 
1637 	if ((share = sa_find_share(handle, si->shr_path)) == NULL) {
1638 		if ((group = smb_get_defaultgrp(handle)) == NULL)
1639 			return (SA_NO_SUCH_GROUP);
1640 
1641 		share = sa_get_share(group, si->shr_path);
1642 		if (share == NULL) {
1643 			share = sa_add_share(group, si->shr_path,
1644 			    SA_SHARE_TRANSIENT, &err);
1645 			if (share == NULL)
1646 				return (SA_NO_SUCH_PATH);
1647 		}
1648 	}
1649 
1650 	/*
1651 	 * Now handle the resource. Make sure that the resource is
1652 	 * transient and added to the share.
1653 	 */
1654 	resource = sa_get_share_resource(share, si->shr_name);
1655 	if (resource == NULL) {
1656 		resource = sa_add_resource(share,
1657 		    si->shr_name, SA_SHARE_TRANSIENT, &err);
1658 		if (resource == NULL)
1659 			return (SA_NO_SUCH_RESOURCE);
1660 	}
1661 
1662 	if (si->shr_cmnt[0] != '\0')
1663 		(void) sa_set_resource_description(resource, si->shr_cmnt);
1664 
1665 	if (si->shr_container[0] != '\0')
1666 		(void) sa_set_resource_attr(resource, SHOPT_AD_CONTAINER,
1667 		    si->shr_container);
1668 
1669 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
1670 		return (SA_NO_MEMORY);
1671 
1672 	if ((opt = smb_csc_name(si)) != NULL)
1673 		err |= nvlist_add_string(nvl, SHOPT_CSC, opt);
1674 
1675 	opt = (si->shr_flags & SMB_SHRF_ABE) ? "true" : "false";
1676 	err |= nvlist_add_string(nvl, SHOPT_ABE, opt);
1677 
1678 	if ((si->shr_flags & SMB_SHRF_AUTOHOME) == 0) {
1679 		opt = (si->shr_flags & SMB_SHRF_GUEST_OK) ? "true" : "false";
1680 		err |= nvlist_add_string(nvl, SHOPT_GUEST, opt);
1681 	}
1682 
1683 	if (si->shr_access_ro[0] != '\0')
1684 		err |= nvlist_add_string(nvl, SHOPT_RO, si->shr_access_ro);
1685 
1686 	if (si->shr_access_rw[0] != '\0')
1687 		err |= nvlist_add_string(nvl, SHOPT_RW, si->shr_access_rw);
1688 
1689 	if (si->shr_access_none[0] != '\0')
1690 		err |= nvlist_add_string(nvl, SHOPT_NONE, si->shr_access_none);
1691 
1692 	if (err) {
1693 		nvlist_free(nvl);
1694 		return (SA_CONFIG_ERR);
1695 	}
1696 
1697 	err = smb_update_optionset_props(handle, resource, nvl);
1698 
1699 	nvlist_free(nvl);
1700 	return (err);
1701 }
1702 
1703 /*
1704  * Return smb transient shares.
1705  */
1706 static int
1707 smb_list_transient(sa_handle_t handle)
1708 {
1709 	int i, offset;
1710 	smb_shrlist_t list;
1711 	int res;
1712 
1713 	if (smb_share_count() <= 0)
1714 		return (SA_OK);
1715 
1716 	offset = 0;
1717 	while (smb_share_list(offset, &list) == NERR_Success) {
1718 		if (list.sl_cnt == 0)
1719 			break;
1720 
1721 		for (i = 0; i < list.sl_cnt; i++) {
1722 			res = smb_add_transient(handle, &(list.sl_shares[i]));
1723 			if (res != SA_OK)
1724 				return (res);
1725 		}
1726 		offset += list.sl_cnt;
1727 	}
1728 
1729 	return (SA_OK);
1730 }
1731 
1732 /*
1733  * fix_resource_name(share, name,  prefix)
1734  *
1735  * Construct a name where the ZFS dataset has the prefix replaced with "name".
1736  */
1737 static char *
1738 fix_resource_name(sa_share_t share, char *name, char *prefix)
1739 {
1740 	char buf[SA_MAX_RESOURCE_NAME + 1];
1741 	char *dataset;
1742 	size_t bufsz = SA_MAX_RESOURCE_NAME + 1;
1743 	size_t prelen;
1744 
1745 	if (prefix == NULL)
1746 		return (strdup(name));
1747 
1748 	dataset = sa_get_share_attr(share, "dataset");
1749 	if (dataset == NULL)
1750 		return (strdup(name));
1751 
1752 	(void) strlcpy(buf, name, bufsz);
1753 	prelen = strlen(prefix);
1754 
1755 	if (strncmp(dataset, prefix, prelen) == 0)
1756 		(void) strlcat(buf, dataset + prelen, bufsz);
1757 
1758 	sa_free_attr_string(dataset);
1759 	sa_fix_resource_name(buf);
1760 	return (strdup(buf));
1761 }
1762 
1763 /*
1764  * smb_parse_optstring(group, options)
1765  *
1766  * parse a compact option string into individual options. This allows
1767  * ZFS sharesmb and sharemgr "share" command to work.  group can be a
1768  * group, a share or a resource.
1769  */
1770 static int
1771 smb_parse_optstring(sa_group_t group, char *options)
1772 {
1773 	char *dup;
1774 	char *base;
1775 	char *lasts;
1776 	char *token;
1777 	sa_optionset_t optionset;
1778 	sa_group_t parent = NULL;
1779 	sa_resource_t resource = NULL;
1780 	int iszfs = 0;
1781 	int persist = 0;
1782 	int need_optionset = 0;
1783 	int ret = SA_OK;
1784 	sa_property_t prop;
1785 
1786 	/*
1787 	 * In order to not attempt to change ZFS properties unless
1788 	 * absolutely necessary, we never do it in the legacy parsing
1789 	 * so we need to keep track of this.
1790 	 */
1791 	if (sa_is_share(group)) {
1792 		char *zfs;
1793 
1794 		parent = sa_get_parent_group(group);
1795 		if (parent != NULL) {
1796 			zfs = sa_get_group_attr(parent, "zfs");
1797 			if (zfs != NULL) {
1798 				sa_free_attr_string(zfs);
1799 				iszfs = 1;
1800 			}
1801 		}
1802 	} else {
1803 		iszfs = sa_group_is_zfs(group);
1804 		/*
1805 		 * If a ZFS group, then we need to see if a resource
1806 		 * name is being set. If so, bail with
1807 		 * SA_PROP_SHARE_ONLY, so we come back in with a share
1808 		 * instead of a group.
1809 		 */
1810 		if (iszfs ||
1811 		    strncmp(options, "name=", sizeof ("name=") - 1) == 0 ||
1812 		    strstr(options, ",name=") != NULL) {
1813 			return (SA_PROP_SHARE_ONLY);
1814 		}
1815 	}
1816 
1817 	/* do we have an existing optionset? */
1818 	optionset = sa_get_optionset(group, "smb");
1819 	if (optionset == NULL) {
1820 		/* didn't find existing optionset so create one */
1821 		optionset = sa_create_optionset(group, "smb");
1822 		if (optionset == NULL)
1823 			return (SA_NO_MEMORY);
1824 	} else {
1825 		/*
1826 		 * If an optionset already exists, we've come through
1827 		 * twice so ignore the second time.
1828 		 */
1829 		return (ret);
1830 	}
1831 
1832 	/* We need a copy of options for the next part. */
1833 	dup = strdup(options);
1834 	if (dup == NULL)
1835 		return (SA_NO_MEMORY);
1836 
1837 	/*
1838 	 * SMB properties are straightforward and are strings,
1839 	 * integers or booleans.  Properties are separated by
1840 	 * commas. It will be necessary to parse quotes due to some
1841 	 * strings not having a restricted characters set.
1842 	 *
1843 	 * Note that names will create a resource. For now, if there
1844 	 * is a set of properties "before" the first name="", those
1845 	 * properties will be placed on the group.
1846 	 */
1847 	persist = sa_is_persistent(group);
1848 	base = dup;
1849 	token = dup;
1850 	lasts = NULL;
1851 	while (token != NULL && ret == SA_OK) {
1852 		ret = SA_OK;
1853 		token = strtok_r(base, ",", &lasts);
1854 		base = NULL;
1855 		if (token != NULL) {
1856 			char *value;
1857 			/*
1858 			 * All SMB properties have values so there
1859 			 * MUST be an '=' character.  If it doesn't,
1860 			 * it is a syntax error.
1861 			 */
1862 			value = strchr(token, '=');
1863 			if (value != NULL) {
1864 				*value++ = '\0';
1865 			} else {
1866 				ret = SA_SYNTAX_ERR;
1867 				break;
1868 			}
1869 			/*
1870 			 * We may need to handle a "name" property
1871 			 * that is a ZFS imposed resource name. Each
1872 			 * name would trigger getting a new "resource"
1873 			 * to put properties on. For now, assume no
1874 			 * "name" property for special handling.
1875 			 */
1876 
1877 			if (strcmp(token, SHOPT_NAME) == 0) {
1878 				char *prefix;
1879 				char *name = NULL;
1880 				/*
1881 				 * We have a name, so now work on the
1882 				 * resource level. We have a "share"
1883 				 * in "group" due to the caller having
1884 				 * added it. If we are called with a
1885 				 * group, the check for group/share
1886 				 * at the beginning of this function
1887 				 * will bail out the parse if there is a
1888 				 * "name" but no share.
1889 				 */
1890 				if (!iszfs) {
1891 					ret = SA_SYNTAX_ERR;
1892 					break;
1893 				}
1894 				/*
1895 				 * Make sure the parent group has the
1896 				 * "prefix" property since we will
1897 				 * need to use this for constructing
1898 				 * inherited name= values.
1899 				 */
1900 				prefix = sa_get_group_attr(parent, "prefix");
1901 				if (prefix == NULL) {
1902 					prefix = sa_get_group_attr(parent,
1903 					    "name");
1904 					if (prefix != NULL) {
1905 						(void) sa_set_group_attr(parent,
1906 						    "prefix", prefix);
1907 					}
1908 				}
1909 				name = fix_resource_name((sa_share_t)group,
1910 				    value, prefix);
1911 				if (name != NULL) {
1912 					resource = sa_add_resource(
1913 					    (sa_share_t)group, name,
1914 					    SA_SHARE_TRANSIENT, &ret);
1915 					sa_free_attr_string(name);
1916 				} else {
1917 					ret = SA_NO_MEMORY;
1918 				}
1919 				if (prefix != NULL)
1920 					sa_free_attr_string(prefix);
1921 
1922 				/* A resource level optionset is needed */
1923 
1924 				need_optionset = 1;
1925 				if (resource == NULL) {
1926 					ret = SA_NO_MEMORY;
1927 					break;
1928 				}
1929 				continue;
1930 			}
1931 
1932 			if (iszfs && strcmp(token, SHOPT_DESCRIPTION) == 0) {
1933 				if (resource == NULL)
1934 					(void) sa_set_share_description(
1935 					    (sa_share_t)group, value);
1936 				else
1937 					(void) sa_set_resource_description(
1938 					    resource, value);
1939 				continue;
1940 			}
1941 
1942 			if (need_optionset) {
1943 				optionset = sa_create_optionset(resource,
1944 				    "smb");
1945 				need_optionset = 0;
1946 			}
1947 
1948 			prop = sa_create_property(token, value);
1949 			if (prop == NULL)
1950 				ret = SA_NO_MEMORY;
1951 			else
1952 				ret = sa_add_property(optionset, prop);
1953 			if (ret != SA_OK)
1954 				break;
1955 			if (!iszfs)
1956 				ret = sa_commit_properties(optionset, !persist);
1957 		}
1958 	}
1959 	free(dup);
1960 	return (ret);
1961 }
1962 
1963 /*
1964  * smb_sprint_option(rbuff, rbuffsize, incr, prop, sep)
1965  *
1966  * provides a mechanism to format SMB properties into legacy output
1967  * format. If the buffer would overflow, it is reallocated and grown
1968  * as appropriate. Special cases of converting internal form of values
1969  * to those used by "share" are done. this function does one property
1970  * at a time.
1971  */
1972 
1973 static void
1974 smb_sprint_option(char **rbuff, size_t *rbuffsize, size_t incr,
1975     sa_property_t prop, int sep)
1976 {
1977 	char *name;
1978 	char *value;
1979 	int curlen;
1980 	char *buff = *rbuff;
1981 	size_t buffsize = *rbuffsize;
1982 
1983 	name = sa_get_property_attr(prop, "type");
1984 	value = sa_get_property_attr(prop, "value");
1985 	if (buff != NULL)
1986 		curlen = strlen(buff);
1987 	else
1988 		curlen = 0;
1989 	if (name != NULL) {
1990 		int len;
1991 		len = strlen(name) + sep;
1992 
1993 		/*
1994 		 * A future RFE would be to replace this with more
1995 		 * generic code and to possibly handle more types.
1996 		 *
1997 		 * For now, everything else is treated as a string. If
1998 		 * we get any properties that aren't exactly
1999 		 * name/value pairs, we may need to
2000 		 * interpret/transform.
2001 		 */
2002 		if (value != NULL)
2003 			len += 1 + strlen(value);
2004 
2005 		while (buffsize <= (curlen + len)) {
2006 			/* need more room */
2007 			buffsize += incr;
2008 			buff = realloc(buff, buffsize);
2009 			*rbuff = buff;
2010 			*rbuffsize = buffsize;
2011 			if (buff == NULL) {
2012 				/* realloc failed so free everything */
2013 				if (*rbuff != NULL)
2014 					free(*rbuff);
2015 				goto err;
2016 			}
2017 		}
2018 		if (buff == NULL)
2019 			goto err;
2020 		(void) snprintf(buff + curlen, buffsize - curlen,
2021 		    "%s%s=%s", sep ? "," : "",
2022 		    name, value != NULL ? value : "\"\"");
2023 
2024 	}
2025 err:
2026 	if (name != NULL)
2027 		sa_free_attr_string(name);
2028 	if (value != NULL)
2029 		sa_free_attr_string(value);
2030 }
2031 
2032 /*
2033  * smb_format_resource_options(resource, hier)
2034  *
2035  * format all the options on the group into a flattened option
2036  * string. If hier is non-zero, walk up the tree to get inherited
2037  * options.
2038  */
2039 
2040 static char *
2041 smb_format_options(sa_group_t group, int hier)
2042 {
2043 	sa_optionset_t options = NULL;
2044 	sa_property_t prop;
2045 	int sep = 0;
2046 	char *buff;
2047 	size_t buffsize;
2048 
2049 
2050 	buff = malloc(OPT_CHUNK);
2051 	if (buff == NULL)
2052 		return (NULL);
2053 
2054 	buff[0] = '\0';
2055 	buffsize = OPT_CHUNK;
2056 
2057 	/*
2058 	 * We may have a an optionset relative to this item. format
2059 	 * these if we find them and then add any security definitions.
2060 	 */
2061 
2062 	options = sa_get_derived_optionset(group, "smb", hier);
2063 
2064 	/*
2065 	 * do the default set first but skip any option that is also
2066 	 * in the protocol specific optionset.
2067 	 */
2068 	if (options != NULL) {
2069 		for (prop = sa_get_property(options, NULL);
2070 		    prop != NULL; prop = sa_get_next_property(prop)) {
2071 			/*
2072 			 * use this one since we skipped any
2073 			 * of these that were also in
2074 			 * optdefault
2075 			 */
2076 			smb_sprint_option(&buff, &buffsize, OPT_CHUNK,
2077 			    prop, sep);
2078 			if (buff == NULL) {
2079 				/*
2080 				 * buff could become NULL if there
2081 				 * isn't enough memory for
2082 				 * smb_sprint_option to realloc()
2083 				 * as necessary. We can't really
2084 				 * do anything about it at this
2085 				 * point so we return NULL.  The
2086 				 * caller should handle the
2087 				 * failure.
2088 				 */
2089 				if (options != NULL)
2090 					sa_free_derived_optionset(
2091 					    options);
2092 				return (buff);
2093 			}
2094 			sep = 1;
2095 		}
2096 	}
2097 
2098 	if (options != NULL)
2099 		sa_free_derived_optionset(options);
2100 	return (buff);
2101 }
2102 
2103 /*
2104  * smb_rename_resource(resource, newname)
2105  *
2106  * Change the current exported name of the resource to newname.
2107  */
2108 /*ARGSUSED*/
2109 int
2110 smb_rename_resource(sa_handle_t handle, sa_resource_t resource, char *newname)
2111 {
2112 	int ret = SA_OK;
2113 	int err;
2114 	char *oldname;
2115 
2116 	if (!smb_isonline())
2117 		return (SA_OK);
2118 
2119 	oldname = sa_get_resource_attr(resource, "name");
2120 	if (oldname == NULL)
2121 		return (SA_NO_SUCH_RESOURCE);
2122 
2123 	err = smb_share_rename(oldname, newname);
2124 
2125 	sa_free_attr_string(oldname);
2126 
2127 	/* improve error values somewhat */
2128 	switch (err) {
2129 	case NERR_Success:
2130 		break;
2131 	case NERR_InternalError:
2132 		ret = SA_SYSTEM_ERR;
2133 		break;
2134 	case NERR_DuplicateShare:
2135 		ret = SA_DUPLICATE_NAME;
2136 		break;
2137 	default:
2138 		ret = SA_CONFIG_ERR;
2139 		break;
2140 	}
2141 
2142 	return (ret);
2143 }
2144 
2145 static int
2146 smb_build_shareinfo(sa_share_t share, sa_resource_t resource, smb_share_t *si)
2147 {
2148 	sa_optionset_t opts;
2149 	char *path;
2150 	char *rname;
2151 	char *val = NULL;
2152 	char csc_value[SMB_CSC_BUFSZ];
2153 	char strbuf[sizeof ("required")];
2154 
2155 	bzero(si, sizeof (smb_share_t));
2156 
2157 	if ((path = sa_get_share_attr(share, "path")) == NULL)
2158 		return (SA_NO_SUCH_PATH);
2159 
2160 	if ((rname = sa_get_resource_attr(resource, "name")) == NULL) {
2161 		sa_free_attr_string(path);
2162 		return (SA_NO_SUCH_RESOURCE);
2163 	}
2164 
2165 	(void) strlcpy(si->shr_path, path, sizeof (si->shr_path));
2166 	(void) strlcpy(si->shr_name, rname, sizeof (si->shr_name));
2167 	sa_free_attr_string(path);
2168 	sa_free_attr_string(rname);
2169 
2170 	val = sa_get_resource_description(resource);
2171 	if (val == NULL)
2172 		val = sa_get_share_description(share);
2173 
2174 	if (val != NULL) {
2175 		(void) strlcpy(si->shr_cmnt, val, sizeof (si->shr_cmnt));
2176 		sa_free_share_description(val);
2177 	}
2178 
2179 	si->shr_flags = (sa_is_persistent(share))
2180 	    ? SMB_SHRF_PERM : SMB_SHRF_TRANS;
2181 
2182 	opts = sa_get_derived_optionset(resource, SMB_PROTOCOL_NAME, 1);
2183 	if (opts == NULL)
2184 		return (SA_OK);
2185 
2186 	if (smb_saprop_getbool(opts, SHOPT_CATIA, B_FALSE))
2187 		si->shr_flags |= SMB_SHRF_CATIA;
2188 
2189 	if (smb_saprop_getbool(opts, SHOPT_ABE, B_FALSE))
2190 		si->shr_flags |= SMB_SHRF_ABE;
2191 
2192 	if (smb_saprop_getbool(opts, SHOPT_GUEST, B_FALSE))
2193 		si->shr_flags |= SMB_SHRF_GUEST_OK;
2194 
2195 	if (smb_saprop_getbool(opts, SHOPT_DFSROOT, B_FALSE))
2196 		si->shr_flags |= SMB_SHRF_DFSROOT;
2197 
2198 	if (smb_saprop_getbool(opts, SHOPT_FSO, B_FALSE))
2199 		si->shr_flags |= SMB_SHRF_FSO;
2200 
2201 	/* Quotas are enabled by default. */
2202 	if (smb_saprop_getbool(opts, SHOPT_QUOTAS, B_TRUE))
2203 		si->shr_flags |= SMB_SHRF_QUOTAS;
2204 
2205 	if (smb_saprop_getstr(opts, SHOPT_ENCRYPT, strbuf, sizeof (strbuf)))
2206 		smb_cfg_set_require(strbuf, &si->shr_encrypt);
2207 
2208 	(void) smb_saprop_getstr(opts, SHOPT_AD_CONTAINER, si->shr_container,
2209 	    sizeof (si->shr_container));
2210 
2211 	if (smb_saprop_getstr(opts, SHOPT_CSC, csc_value, sizeof (csc_value)))
2212 		smb_csc_option(csc_value, si);
2213 
2214 	if (smb_saprop_getstr(opts, SHOPT_RO, si->shr_access_ro,
2215 	    sizeof (si->shr_access_ro)))
2216 		si->shr_flags |= SMB_SHRF_ACC_RO;
2217 
2218 	if (smb_saprop_getstr(opts, SHOPT_RW, si->shr_access_rw,
2219 	    sizeof (si->shr_access_rw)))
2220 		si->shr_flags |= SMB_SHRF_ACC_RW;
2221 
2222 	if (smb_saprop_getstr(opts, SHOPT_NONE, si->shr_access_none,
2223 	    sizeof (si->shr_access_none)))
2224 		si->shr_flags |= SMB_SHRF_ACC_NONE;
2225 
2226 	sa_free_derived_optionset(opts);
2227 	return (SA_OK);
2228 }
2229 
2230 /*
2231  * Map a client-side caching (CSC) option to the appropriate share
2232  * flag.  Only one option is allowed; an error will be logged if
2233  * multiple options have been specified.  We don't need to do anything
2234  * about multiple values here because the SRVSVC will not recognize
2235  * a value containing multiple flags and will return the default value.
2236  *
2237  * If the option value is not recognized, it will be ignored: invalid
2238  * values will typically be caught and rejected by sharemgr.
2239  */
2240 static void
2241 smb_csc_option(const char *value, smb_share_t *si)
2242 {
2243 	char buf[SMB_CSC_BUFSZ];
2244 	int i;
2245 
2246 	for (i = 0; i < (sizeof (cscopt) / sizeof (cscopt[0])); ++i) {
2247 		if (strcasecmp(value, cscopt[i].value) == 0) {
2248 			si->shr_flags |= cscopt[i].flag;
2249 			break;
2250 		}
2251 	}
2252 
2253 	switch (si->shr_flags & SMB_SHRF_CSC_MASK) {
2254 	case 0:
2255 	case SMB_SHRF_CSC_DISABLED:
2256 	case SMB_SHRF_CSC_MANUAL:
2257 	case SMB_SHRF_CSC_AUTO:
2258 	case SMB_SHRF_CSC_VDO:
2259 		break;
2260 
2261 	default:
2262 		buf[0] = '\0';
2263 
2264 		for (i = 0; i < (sizeof (cscopt) / sizeof (cscopt[0])); ++i) {
2265 			if (si->shr_flags & cscopt[i].flag) {
2266 				(void) strlcat(buf, " ", SMB_CSC_BUFSZ);
2267 				(void) strlcat(buf, cscopt[i].value,
2268 				    SMB_CSC_BUFSZ);
2269 			}
2270 		}
2271 
2272 		syslog(LOG_ERR, "csc option conflict:%s", buf);
2273 		break;
2274 	}
2275 }
2276 
2277 /*
2278  * Return the option name for the first CSC flag (there should be only
2279  * one) encountered in the share flags.
2280  */
2281 static char *
2282 smb_csc_name(const smb_share_t *si)
2283 {
2284 	int i;
2285 
2286 	for (i = 0; i < (sizeof (cscopt) / sizeof (cscopt[0])); ++i) {
2287 		if (si->shr_flags & cscopt[i].flag)
2288 			return (cscopt[i].value);
2289 	}
2290 
2291 	return (NULL);
2292 }
2293 
2294 /*
2295  * smb_get_defaultgrp
2296  *
2297  * If default group for CIFS shares (i.e. "smb") exists
2298  * then it will return the group handle, otherwise it will
2299  * create the group and return the handle.
2300  *
2301  * All the shares created by CIFS clients (this is only possible
2302  * via RPC) will be added to "smb" groups.
2303  */
2304 static sa_group_t
2305 smb_get_defaultgrp(sa_handle_t handle)
2306 {
2307 	sa_group_t group = NULL;
2308 	int err;
2309 
2310 	group = sa_get_group(handle, SMB_DEFAULT_SHARE_GROUP);
2311 	if (group != NULL)
2312 		return (group);
2313 
2314 	group = sa_create_group(handle, SMB_DEFAULT_SHARE_GROUP, &err);
2315 	if (group == NULL)
2316 		return (NULL);
2317 
2318 	if (sa_create_optionset(group, SMB_DEFAULT_SHARE_GROUP) == NULL) {
2319 		(void) sa_remove_group(group);
2320 		group = NULL;
2321 	}
2322 
2323 	return (group);
2324 }
2325 
2326 /*
2327  * Checks to see if the command args are the supported substitution specifier.
2328  * i.e. <cmd> %U %S
2329  */
2330 static int
2331 cmd_validator(int index, char *value)
2332 {
2333 	char cmd[MAXPATHLEN];
2334 	char *ptr, *v;
2335 	boolean_t skip_cmdname;
2336 
2337 	if (string_length_check_validator(index, value) != SA_OK)
2338 		return (SA_BAD_VALUE);
2339 
2340 	if (*value == '\0')
2341 		return (SA_OK);
2342 
2343 	(void) strlcpy(cmd, value, sizeof (cmd));
2344 
2345 	ptr = cmd;
2346 	skip_cmdname = B_TRUE;
2347 	do {
2348 		if ((v = strsep(&ptr, " ")) == NULL)
2349 			break;
2350 
2351 		if (*v != '\0') {
2352 
2353 			if (skip_cmdname) {
2354 				skip_cmdname = B_FALSE;
2355 				continue;
2356 			}
2357 
2358 			if ((strlen(v) != 2) || *v != '%')
2359 				return (SA_BAD_VALUE);
2360 
2361 			if (strpbrk(v, SMB_VALID_SUB_CHRS) == NULL)
2362 				return (SA_BAD_VALUE);
2363 		}
2364 
2365 	} while (v != NULL);
2366 
2367 	/*
2368 	 * If skip_cmdname is still true then the string contains
2369 	 * only spaces.  Don't allow such a string.
2370 	 */
2371 	if (skip_cmdname)
2372 		return (SA_BAD_VALUE);
2373 
2374 	return (SA_OK);
2375 }
2376 
2377 /*ARGSUSED*/
2378 static int
2379 disposition_validator(int index, char *value)
2380 {
2381 	if (value == NULL)
2382 		return (SA_BAD_VALUE);
2383 
2384 	if (*value == '\0')
2385 		return (SA_OK);
2386 
2387 	if ((strcasecmp(value, SMB_EXEC_DISP_CONTINUE) == 0) ||
2388 	    (strcasecmp(value, SMB_EXEC_DISP_TERMINATE) == 0))
2389 		return (SA_OK);
2390 
2391 	return (SA_BAD_VALUE);
2392 }
2393 
2394 /*ARGSUSED*/
2395 static int
2396 protocol_validator(int index, char *value)
2397 {
2398 	if (value == NULL)
2399 		return (SA_BAD_VALUE);
2400 
2401 	if (*value == '\0')
2402 		return (SA_OK);
2403 
2404 	if (smb_config_check_protocol(value) == 0)
2405 		return (SA_OK);
2406 
2407 	return (SA_BAD_VALUE);
2408 
2409 }
2410 
2411 /*
2412  * Updates the optionset properties of the share resource.
2413  * The properties are given as a list of name-value pair.
2414  * The name argument should be the optionset property name and the value
2415  * should be a valid value for the specified property.
2416  *
2417  * When calling this function for permanent shares, the caller must also
2418  * call sa_commit_properties() to commit the changes to SMF.
2419  */
2420 static int
2421 smb_update_optionset_props(sa_handle_t handle, sa_resource_t resource,
2422     nvlist_t *nvl)
2423 {
2424 	sa_property_t prop;
2425 	sa_optionset_t opts;
2426 	int err = SA_OK;
2427 	nvpair_t *cur;
2428 	char *name, *val;
2429 
2430 	if ((opts = sa_get_optionset(resource, SMB_PROTOCOL_NAME)) == NULL) {
2431 		opts = sa_create_optionset(resource, SMB_PROTOCOL_NAME);
2432 		if (opts == NULL)
2433 			return (SA_CONFIG_ERR);
2434 	}
2435 
2436 	cur = nvlist_next_nvpair(nvl, NULL);
2437 	while (cur != NULL) {
2438 		name = nvpair_name(cur);
2439 		err = nvpair_value_string(cur, &val);
2440 		if ((err != 0) || (name == NULL) || (val == NULL)) {
2441 			err = SA_CONFIG_ERR;
2442 			break;
2443 		}
2444 
2445 		prop = NULL;
2446 		if ((prop = sa_get_property(opts, name)) == NULL) {
2447 			prop = sa_create_property(name, val);
2448 			if (prop != NULL) {
2449 				err = sa_valid_property(handle, opts,
2450 				    SMB_PROTOCOL_NAME, prop);
2451 				if (err != SA_OK) {
2452 					(void) sa_remove_property(prop);
2453 					break;
2454 				}
2455 			}
2456 			err = sa_add_property(opts, prop);
2457 			if (err != SA_OK)
2458 				break;
2459 		} else {
2460 			err = sa_update_property(prop, val);
2461 			if (err != SA_OK)
2462 				break;
2463 		}
2464 
2465 		cur = nvlist_next_nvpair(nvl, cur);
2466 	}
2467 
2468 	return (err);
2469 }
2470 
2471 static boolean_t
2472 smb_saprop_getbool(sa_optionset_t opts, char *propname, boolean_t def)
2473 {
2474 	sa_property_t prop;
2475 	char *val;
2476 	boolean_t ret = def;
2477 
2478 	prop = sa_get_property(opts, propname);
2479 	if ((val = sa_get_property_attr(prop, "value")) != NULL) {
2480 		if (def) {
2481 			/* Default is true, ret false if... */
2482 			if ((strcasecmp(val, "false") == 0) ||
2483 			    (strcmp(val, "0") == 0))
2484 				ret = B_FALSE;
2485 		} else {
2486 			/* Default is false, ret true if... */
2487 			if ((strcasecmp(val, "true") == 0) ||
2488 			    (strcmp(val, "1") == 0))
2489 				ret = B_TRUE;
2490 		}
2491 		free(val);
2492 	}
2493 
2494 	return (ret);
2495 }
2496 
2497 static boolean_t
2498 smb_saprop_getstr(sa_optionset_t opts, char *propname, char *buf, size_t bufsz)
2499 {
2500 	sa_property_t prop;
2501 	char *val;
2502 
2503 	prop = sa_get_property(opts, propname);
2504 	if ((val = sa_get_property_attr(prop, "value")) != NULL) {
2505 		(void) strlcpy(buf, val, bufsz);
2506 		free(val);
2507 		return (B_TRUE);
2508 	}
2509 
2510 	return (B_FALSE);
2511 }
2512