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