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