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