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