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