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