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