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