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