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