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