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