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