xref: /illumos-gate/usr/src/lib/libshare/smb/libshare_smb.c (revision d8a7fe16f62711cdc5c4267da8b34ff24a6b668c)
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 2009 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 	dataset = sa_get_share_attr(share, "dataset");
1620 	if (dataset == NULL)
1621 		return (strdup(name));
1622 
1623 	(void) strlcpy(buf, name, bufsz);
1624 	prelen = strlen(prefix);
1625 
1626 	if (strncmp(dataset, prefix, prelen) == 0)
1627 		(void) strlcat(buf, dataset + prelen, bufsz);
1628 
1629 	sa_free_attr_string(dataset);
1630 	sa_fix_resource_name(buf);
1631 	return (strdup(buf));
1632 }
1633 
1634 /*
1635  * smb_parse_optstring(group, options)
1636  *
1637  * parse a compact option string into individual options. This allows
1638  * ZFS sharesmb and sharemgr "share" command to work.  group can be a
1639  * group, a share or a resource.
1640  */
1641 static int
1642 smb_parse_optstring(sa_group_t group, char *options)
1643 {
1644 	char *dup;
1645 	char *base;
1646 	char *lasts;
1647 	char *token;
1648 	sa_optionset_t optionset;
1649 	sa_group_t parent = NULL;
1650 	sa_resource_t resource = NULL;
1651 	int iszfs = 0;
1652 	int persist = 0;
1653 	int need_optionset = 0;
1654 	int ret = SA_OK;
1655 	sa_property_t prop;
1656 
1657 	/*
1658 	 * In order to not attempt to change ZFS properties unless
1659 	 * absolutely necessary, we never do it in the legacy parsing
1660 	 * so we need to keep track of this.
1661 	 */
1662 	if (sa_is_share(group)) {
1663 		char *zfs;
1664 
1665 		parent = sa_get_parent_group(group);
1666 		if (parent != NULL) {
1667 			zfs = sa_get_group_attr(parent, "zfs");
1668 			if (zfs != NULL) {
1669 				sa_free_attr_string(zfs);
1670 				iszfs = 1;
1671 			}
1672 		}
1673 	} else {
1674 		iszfs = sa_group_is_zfs(group);
1675 		/*
1676 		 * If a ZFS group, then we need to see if a resource
1677 		 * name is being set. If so, bail with
1678 		 * SA_PROP_SHARE_ONLY, so we come back in with a share
1679 		 * instead of a group.
1680 		 */
1681 		if (strncmp(options, "name=", sizeof ("name=") - 1) == 0 ||
1682 		    strstr(options, ",name=") != NULL) {
1683 			return (SA_PROP_SHARE_ONLY);
1684 		}
1685 	}
1686 
1687 	/* do we have an existing optionset? */
1688 	optionset = sa_get_optionset(group, "smb");
1689 	if (optionset == NULL) {
1690 		/* didn't find existing optionset so create one */
1691 		optionset = sa_create_optionset(group, "smb");
1692 		if (optionset == NULL)
1693 			return (SA_NO_MEMORY);
1694 	} else {
1695 		/*
1696 		 * If an optionset already exists, we've come through
1697 		 * twice so ignore the second time.
1698 		 */
1699 		return (ret);
1700 	}
1701 
1702 	/* We need a copy of options for the next part. */
1703 	dup = strdup(options);
1704 	if (dup == NULL)
1705 		return (SA_NO_MEMORY);
1706 
1707 	/*
1708 	 * SMB properties are straightforward and are strings,
1709 	 * integers or booleans.  Properties are separated by
1710 	 * commas. It will be necessary to parse quotes due to some
1711 	 * strings not having a restricted characters set.
1712 	 *
1713 	 * Note that names will create a resource. For now, if there
1714 	 * is a set of properties "before" the first name="", those
1715 	 * properties will be placed on the group.
1716 	 */
1717 	persist = sa_is_persistent(group);
1718 	base = dup;
1719 	token = dup;
1720 	lasts = NULL;
1721 	while (token != NULL && ret == SA_OK) {
1722 		ret = SA_OK;
1723 		token = strtok_r(base, ",", &lasts);
1724 		base = NULL;
1725 		if (token != NULL) {
1726 			char *value;
1727 			/*
1728 			 * All SMB properties have values so there
1729 			 * MUST be an '=' character.  If it doesn't,
1730 			 * it is a syntax error.
1731 			 */
1732 			value = strchr(token, '=');
1733 			if (value != NULL) {
1734 				*value++ = '\0';
1735 			} else {
1736 				ret = SA_SYNTAX_ERR;
1737 				break;
1738 			}
1739 			/*
1740 			 * We may need to handle a "name" property
1741 			 * that is a ZFS imposed resource name. Each
1742 			 * name would trigger getting a new "resource"
1743 			 * to put properties on. For now, assume no
1744 			 * "name" property for special handling.
1745 			 */
1746 
1747 			if (strcmp(token, "name") == 0) {
1748 				char *prefix;
1749 				char *name = NULL;
1750 				/*
1751 				 * We have a name, so now work on the
1752 				 * resource level. We have a "share"
1753 				 * in "group" due to the caller having
1754 				 * added it. If we are called with a
1755 				 * group, the check for group/share
1756 				 * at the beginning of this function
1757 				 * will bail out the parse if there is a
1758 				 * "name" but no share.
1759 				 */
1760 				if (!iszfs) {
1761 					ret = SA_SYNTAX_ERR;
1762 					break;
1763 				}
1764 				/*
1765 				 * Make sure the parent group has the
1766 				 * "prefix" property since we will
1767 				 * need to use this for constructing
1768 				 * inherited name= values.
1769 				 */
1770 				prefix = sa_get_group_attr(parent, "prefix");
1771 				if (prefix == NULL) {
1772 					prefix = sa_get_group_attr(parent,
1773 					    "name");
1774 					if (prefix != NULL) {
1775 						(void) sa_set_group_attr(parent,
1776 						    "prefix", prefix);
1777 					}
1778 				}
1779 				if (prefix != NULL)
1780 					name = fix_resource_name(
1781 					    (sa_share_t)group, value, prefix);
1782 				if (name != NULL) {
1783 					resource = sa_add_resource(
1784 					    (sa_share_t)group, name,
1785 					    SA_SHARE_TRANSIENT, &ret);
1786 					sa_free_attr_string(name);
1787 				} else {
1788 					ret = SA_NO_MEMORY;
1789 				}
1790 				if (prefix != NULL)
1791 					sa_free_attr_string(prefix);
1792 
1793 				/* A resource level optionset is needed */
1794 
1795 				need_optionset = 1;
1796 				if (resource == NULL) {
1797 					ret = SA_NO_MEMORY;
1798 					break;
1799 				}
1800 				continue;
1801 			}
1802 
1803 			if (need_optionset) {
1804 				optionset = sa_create_optionset(resource,
1805 				    "smb");
1806 				need_optionset = 0;
1807 			}
1808 
1809 			prop = sa_create_property(token, value);
1810 			if (prop == NULL)
1811 				ret = SA_NO_MEMORY;
1812 			else
1813 				ret = sa_add_property(optionset, prop);
1814 			if (ret != SA_OK)
1815 				break;
1816 			if (!iszfs)
1817 				ret = sa_commit_properties(optionset, !persist);
1818 		}
1819 	}
1820 	free(dup);
1821 	return (ret);
1822 }
1823 
1824 /*
1825  * smb_sprint_option(rbuff, rbuffsize, incr, prop, sep)
1826  *
1827  * provides a mechanism to format SMB properties into legacy output
1828  * format. If the buffer would overflow, it is reallocated and grown
1829  * as appropriate. Special cases of converting internal form of values
1830  * to those used by "share" are done. this function does one property
1831  * at a time.
1832  */
1833 
1834 static void
1835 smb_sprint_option(char **rbuff, size_t *rbuffsize, size_t incr,
1836 			sa_property_t prop, int sep)
1837 {
1838 	char *name;
1839 	char *value;
1840 	int curlen;
1841 	char *buff = *rbuff;
1842 	size_t buffsize = *rbuffsize;
1843 
1844 	name = sa_get_property_attr(prop, "type");
1845 	value = sa_get_property_attr(prop, "value");
1846 	if (buff != NULL)
1847 		curlen = strlen(buff);
1848 	else
1849 		curlen = 0;
1850 	if (name != NULL) {
1851 		int len;
1852 		len = strlen(name) + sep;
1853 
1854 		/*
1855 		 * A future RFE would be to replace this with more
1856 		 * generic code and to possibly handle more types.
1857 		 *
1858 		 * For now, everything else is treated as a string. If
1859 		 * we get any properties that aren't exactly
1860 		 * name/value pairs, we may need to
1861 		 * interpret/transform.
1862 		 */
1863 		if (value != NULL)
1864 			len += 1 + strlen(value);
1865 
1866 		while (buffsize <= (curlen + len)) {
1867 			/* need more room */
1868 			buffsize += incr;
1869 			buff = realloc(buff, buffsize);
1870 			*rbuff = buff;
1871 			*rbuffsize = buffsize;
1872 			if (buff == NULL) {
1873 				/* realloc failed so free everything */
1874 				if (*rbuff != NULL)
1875 					free(*rbuff);
1876 				goto err;
1877 			}
1878 		}
1879 		if (buff == NULL)
1880 			goto err;
1881 		(void) snprintf(buff + curlen, buffsize - curlen,
1882 		    "%s%s=%s", sep ? "," : "",
1883 		    name, value != NULL ? value : "\"\"");
1884 
1885 	}
1886 err:
1887 	if (name != NULL)
1888 		sa_free_attr_string(name);
1889 	if (value != NULL)
1890 		sa_free_attr_string(value);
1891 }
1892 
1893 /*
1894  * smb_format_resource_options(resource, hier)
1895  *
1896  * format all the options on the group into a flattened option
1897  * string. If hier is non-zero, walk up the tree to get inherited
1898  * options.
1899  */
1900 
1901 static char *
1902 smb_format_options(sa_group_t group, int hier)
1903 {
1904 	sa_optionset_t options = NULL;
1905 	sa_property_t prop;
1906 	int sep = 0;
1907 	char *buff;
1908 	size_t buffsize;
1909 
1910 
1911 	buff = malloc(OPT_CHUNK);
1912 	if (buff == NULL)
1913 		return (NULL);
1914 
1915 	buff[0] = '\0';
1916 	buffsize = OPT_CHUNK;
1917 
1918 	/*
1919 	 * We may have a an optionset relative to this item. format
1920 	 * these if we find them and then add any security definitions.
1921 	 */
1922 
1923 	options = sa_get_derived_optionset(group, "smb", hier);
1924 
1925 	/*
1926 	 * do the default set first but skip any option that is also
1927 	 * in the protocol specific optionset.
1928 	 */
1929 	if (options != NULL) {
1930 		for (prop = sa_get_property(options, NULL);
1931 		    prop != NULL; prop = sa_get_next_property(prop)) {
1932 			/*
1933 			 * use this one since we skipped any
1934 			 * of these that were also in
1935 			 * optdefault
1936 			 */
1937 			smb_sprint_option(&buff, &buffsize, OPT_CHUNK,
1938 			    prop, sep);
1939 			if (buff == NULL) {
1940 				/*
1941 				 * buff could become NULL if there
1942 				 * isn't enough memory for
1943 				 * smb_sprint_option to realloc()
1944 				 * as necessary. We can't really
1945 				 * do anything about it at this
1946 				 * point so we return NULL.  The
1947 				 * caller should handle the
1948 				 * failure.
1949 				 */
1950 				if (options != NULL)
1951 					sa_free_derived_optionset(
1952 					    options);
1953 				return (buff);
1954 			}
1955 			sep = 1;
1956 		}
1957 	}
1958 
1959 	if (options != NULL)
1960 		sa_free_derived_optionset(options);
1961 	return (buff);
1962 }
1963 
1964 /*
1965  * smb_rename_resource(resource, newname)
1966  *
1967  * Change the current exported name of the resource to newname.
1968  */
1969 /*ARGSUSED*/
1970 int
1971 smb_rename_resource(sa_handle_t handle, sa_resource_t resource, char *newname)
1972 {
1973 	int ret = SA_OK;
1974 	int err;
1975 	char *oldname;
1976 
1977 	if (!smb_isonline())
1978 		return (SA_OK);
1979 
1980 	oldname = sa_get_resource_attr(resource, "name");
1981 	if (oldname == NULL)
1982 		return (SA_NO_SUCH_RESOURCE);
1983 
1984 	err = smb_share_rename(oldname, newname);
1985 
1986 	sa_free_attr_string(oldname);
1987 
1988 	/* improve error values somewhat */
1989 	switch (err) {
1990 	case NERR_Success:
1991 		break;
1992 	case NERR_InternalError:
1993 		ret = SA_SYSTEM_ERR;
1994 		break;
1995 	case NERR_DuplicateShare:
1996 		ret = SA_DUPLICATE_NAME;
1997 		break;
1998 	default:
1999 		ret = SA_CONFIG_ERR;
2000 		break;
2001 	}
2002 
2003 	return (ret);
2004 }
2005 
2006 static int
2007 smb_build_shareinfo(sa_share_t share, sa_resource_t resource, smb_share_t *si)
2008 {
2009 	sa_property_t prop;
2010 	sa_optionset_t opts;
2011 	char *path;
2012 	char *rname;
2013 	char *val = NULL;
2014 
2015 	bzero(si, sizeof (smb_share_t));
2016 
2017 	if ((path = sa_get_share_attr(share, "path")) == NULL)
2018 		return (SA_NO_SUCH_PATH);
2019 
2020 	if ((rname = sa_get_resource_attr(resource, "name")) == NULL) {
2021 		sa_free_attr_string(path);
2022 		return (SA_NO_SUCH_RESOURCE);
2023 	}
2024 
2025 	si->shr_flags = (sa_is_persistent(share))
2026 	    ? SMB_SHRF_PERM : SMB_SHRF_TRANS;
2027 
2028 	(void) strlcpy(si->shr_path, path, sizeof (si->shr_path));
2029 	(void) strlcpy(si->shr_name, rname, sizeof (si->shr_name));
2030 	sa_free_attr_string(path);
2031 	sa_free_attr_string(rname);
2032 
2033 	val = sa_get_resource_description(resource);
2034 	if (val == NULL)
2035 		val = sa_get_share_description(share);
2036 
2037 	if (val != NULL) {
2038 		(void) strlcpy(si->shr_cmnt, val, sizeof (si->shr_cmnt));
2039 		sa_free_share_description(val);
2040 	}
2041 
2042 	opts = sa_get_derived_optionset(resource, SMB_PROTOCOL_NAME, 1);
2043 	if (opts == NULL)
2044 		return (SA_OK);
2045 
2046 	prop = sa_get_property(opts, SHOPT_AD_CONTAINER);
2047 	if (prop != NULL) {
2048 		if ((val = sa_get_property_attr(prop, "value")) != NULL) {
2049 			(void) strlcpy(si->shr_container, val,
2050 			    sizeof (si->shr_container));
2051 			free(val);
2052 		}
2053 	}
2054 
2055 	prop = sa_get_property(opts, SHOPT_CATIA);
2056 	if (prop != NULL) {
2057 		if ((val = sa_get_property_attr(prop, "value")) != NULL) {
2058 			if ((strcasecmp(val, "true") == 0) ||
2059 			    (strcmp(val, "1") == 0)) {
2060 				si->shr_flags |= SMB_SHRF_CATIA;
2061 			} else {
2062 				si->shr_flags &= ~SMB_SHRF_CATIA;
2063 			}
2064 			free(val);
2065 		}
2066 	}
2067 
2068 	prop = sa_get_property(opts, SHOPT_ABE);
2069 	if (prop != NULL) {
2070 		if ((val = sa_get_property_attr(prop, "value")) != NULL) {
2071 			if ((strcasecmp(val, "true") == 0) ||
2072 			    (strcmp(val, "1") == 0)) {
2073 				si->shr_flags |= SMB_SHRF_ABE;
2074 			} else {
2075 				si->shr_flags &= ~SMB_SHRF_ABE;
2076 			}
2077 			free(val);
2078 		}
2079 	}
2080 
2081 	prop = sa_get_property(opts, SHOPT_CSC);
2082 	if (prop != NULL) {
2083 		if ((val = sa_get_property_attr(prop, "value")) != NULL) {
2084 			smb_csc_option(val, si);
2085 			free(val);
2086 		}
2087 	}
2088 
2089 	prop = sa_get_property(opts, SHOPT_GUEST);
2090 	if (prop != NULL) {
2091 		if ((val = sa_get_property_attr(prop, "value")) != NULL) {
2092 			if ((strcasecmp(val, "true") == 0) ||
2093 			    (strcmp(val, "1") == 0))
2094 				si->shr_flags |= SMB_SHRF_GUEST_OK;
2095 			else if (strcasecmp(val, "false") == 0)
2096 				si->shr_flags &= ~SMB_SHRF_GUEST_OK;
2097 			free(val);
2098 		}
2099 	}
2100 
2101 	prop = sa_get_property(opts, SHOPT_RO);
2102 	if (prop != NULL) {
2103 		if ((val = sa_get_property_attr(prop, "value")) != NULL) {
2104 			(void) strlcpy(si->shr_access_ro, val,
2105 			    sizeof (si->shr_access_ro));
2106 			free(val);
2107 			si->shr_flags |= SMB_SHRF_ACC_RO;
2108 		}
2109 	}
2110 
2111 	prop = sa_get_property(opts, SHOPT_RW);
2112 	if (prop != NULL) {
2113 		if ((val = sa_get_property_attr(prop, "value")) != NULL) {
2114 			(void) strlcpy(si->shr_access_rw, val,
2115 			    sizeof (si->shr_access_rw));
2116 			free(val);
2117 			si->shr_flags |= SMB_SHRF_ACC_RW;
2118 		}
2119 	}
2120 
2121 	prop = sa_get_property(opts, SHOPT_NONE);
2122 	if (prop != NULL) {
2123 		if ((val = sa_get_property_attr(prop, "value")) != NULL) {
2124 			(void) strlcpy(si->shr_access_none, val,
2125 			    sizeof (si->shr_access_none));
2126 			free(val);
2127 			si->shr_flags |= SMB_SHRF_ACC_NONE;
2128 		}
2129 	}
2130 
2131 	sa_free_derived_optionset(opts);
2132 	return (SA_OK);
2133 }
2134 
2135 /*
2136  * Map a client-side caching (CSC) option to the appropriate share
2137  * flag.  Only one option is allowed; an error will be logged if
2138  * multiple options have been specified.  We don't need to do anything
2139  * about multiple values here because the SRVSVC will not recognize
2140  * a value containing multiple flags and will return the default value.
2141  *
2142  * If the option value is not recognized, it will be ignored: invalid
2143  * values will typically be caught and rejected by sharemgr.
2144  */
2145 static void
2146 smb_csc_option(const char *value, smb_share_t *si)
2147 {
2148 	char buf[SMB_CSC_BUFSZ];
2149 	int i;
2150 
2151 	for (i = 0; i < (sizeof (cscopt) / sizeof (cscopt[0])); ++i) {
2152 		if (strcasecmp(value, cscopt[i].value) == 0) {
2153 			si->shr_flags |= cscopt[i].flag;
2154 			break;
2155 		}
2156 	}
2157 
2158 	switch (si->shr_flags & SMB_SHRF_CSC_MASK) {
2159 	case 0:
2160 	case SMB_SHRF_CSC_DISABLED:
2161 	case SMB_SHRF_CSC_MANUAL:
2162 	case SMB_SHRF_CSC_AUTO:
2163 	case SMB_SHRF_CSC_VDO:
2164 		break;
2165 
2166 	default:
2167 		buf[0] = '\0';
2168 
2169 		for (i = 0; i < (sizeof (cscopt) / sizeof (cscopt[0])); ++i) {
2170 			if (si->shr_flags & cscopt[i].flag) {
2171 				(void) strlcat(buf, " ", SMB_CSC_BUFSZ);
2172 				(void) strlcat(buf, cscopt[i].value,
2173 				    SMB_CSC_BUFSZ);
2174 			}
2175 		}
2176 
2177 		syslog(LOG_ERR, "csc option conflict:%s", buf);
2178 		break;
2179 	}
2180 }
2181 
2182 /*
2183  * Return the option name for the first CSC flag (there should be only
2184  * one) encountered in the share flags.
2185  */
2186 static char *
2187 smb_csc_name(const smb_share_t *si)
2188 {
2189 	int i;
2190 
2191 	for (i = 0; i < (sizeof (cscopt) / sizeof (cscopt[0])); ++i) {
2192 		if (si->shr_flags & cscopt[i].flag)
2193 			return (cscopt[i].value);
2194 	}
2195 
2196 	return (NULL);
2197 }
2198 
2199 /*
2200  * smb_get_defaultgrp
2201  *
2202  * If default group for CIFS shares (i.e. "smb") exists
2203  * then it will return the group handle, otherwise it will
2204  * create the group and return the handle.
2205  *
2206  * All the shares created by CIFS clients (this is only possible
2207  * via RPC) will be added to "smb" groups.
2208  */
2209 static sa_group_t
2210 smb_get_defaultgrp(sa_handle_t handle)
2211 {
2212 	sa_group_t group = NULL;
2213 	int err;
2214 
2215 	group = sa_get_group(handle, SMB_DEFAULT_SHARE_GROUP);
2216 	if (group != NULL)
2217 		return (group);
2218 
2219 	group = sa_create_group(handle, SMB_DEFAULT_SHARE_GROUP, &err);
2220 	if (group == NULL)
2221 		return (NULL);
2222 
2223 	if (sa_create_optionset(group, SMB_DEFAULT_SHARE_GROUP) == NULL) {
2224 		(void) sa_remove_group(group);
2225 		group = NULL;
2226 	}
2227 
2228 	return (group);
2229 }
2230 
2231 /*
2232  * Checks to see if the command args are the supported substitution specifier.
2233  * i.e. <cmd> %U %S
2234  */
2235 static int
2236 cmd_validator(int index, char *value)
2237 {
2238 	char cmd[MAXPATHLEN];
2239 	char *ptr, *v;
2240 	boolean_t skip_cmdname;
2241 
2242 	if (string_length_check_validator(index, value) != SA_OK)
2243 		return (SA_BAD_VALUE);
2244 
2245 	if (*value == '\0')
2246 		return (SA_OK);
2247 
2248 	(void) strlcpy(cmd, value, sizeof (cmd));
2249 
2250 	ptr = cmd;
2251 	skip_cmdname = B_TRUE;
2252 	do {
2253 		if ((v = strsep(&ptr, " ")) == NULL)
2254 			break;
2255 
2256 		if (*v != '\0') {
2257 
2258 			if (skip_cmdname) {
2259 				skip_cmdname = B_FALSE;
2260 				continue;
2261 			}
2262 
2263 			if ((strlen(v) != 2) || *v != '%')
2264 				return (SA_BAD_VALUE);
2265 
2266 			if (strpbrk(v, SMB_VALID_SUB_CHRS) == NULL)
2267 				return (SA_BAD_VALUE);
2268 		}
2269 
2270 	} while (v != NULL);
2271 
2272 	/*
2273 	 * If skip_cmdname is still true then the string contains
2274 	 * only spaces.  Don't allow such a string.
2275 	 */
2276 	if (skip_cmdname)
2277 		return (SA_BAD_VALUE);
2278 
2279 	return (SA_OK);
2280 }
2281 
2282 /*ARGSUSED*/
2283 static int
2284 disposition_validator(int index, char *value)
2285 {
2286 	if (value == NULL)
2287 		return (SA_BAD_VALUE);
2288 
2289 	if (*value == '\0')
2290 		return (SA_OK);
2291 
2292 	if ((strcasecmp(value, SMB_SHR_DISP_CONT_STR) == 0) ||
2293 	    (strcasecmp(value, SMB_SHR_DISP_TERM_STR) == 0))
2294 		return (SA_OK);
2295 
2296 	return (SA_BAD_VALUE);
2297 }
2298