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