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