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