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