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