xref: /illumos-gate/usr/src/lib/libshare/smb/libshare_smb.c (revision 7014882c6a3672fd0e5d60200af8643ae53c5928)
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_MAP, 0, MAX_VALUE_BUFLEN, cmd_validator, SMB_REFRESH_REFRESH },
920 	{ SMB_CI_UNMAP, 0, MAX_VALUE_BUFLEN, cmd_validator,
921 	    SMB_REFRESH_REFRESH },
922 	{ SMB_CI_DISPOSITION, 0, MAX_VALUE_BUFLEN,
923 	    disposition_validator, SMB_REFRESH_REFRESH },
924 };
925 
926 #define	SMB_OPT_NUM \
927 	(sizeof (smb_proto_options) / sizeof (smb_proto_options[0]))
928 
929 /*
930  * Check the range of value as int range.
931  */
932 static int
933 range_check_validator(int index, char *value)
934 {
935 	int ret = SA_OK;
936 
937 	if (!is_a_number(value)) {
938 		ret = SA_BAD_VALUE;
939 	} else {
940 		int val;
941 		val = strtoul(value, NULL, 0);
942 		if (val < smb_proto_options[index].minval ||
943 		    val > smb_proto_options[index].maxval)
944 			ret = SA_BAD_VALUE;
945 	}
946 	return (ret);
947 }
948 
949 /*
950  * Check the range of value as int range.
951  */
952 static int
953 range_check_validator_zero_ok(int index, char *value)
954 {
955 	int ret = SA_OK;
956 
957 	if (!is_a_number(value)) {
958 		ret = SA_BAD_VALUE;
959 	} else {
960 		int val;
961 		val = strtoul(value, NULL, 0);
962 		if (val == 0)
963 			ret = SA_OK;
964 		else {
965 			if (val < smb_proto_options[index].minval ||
966 			    val > smb_proto_options[index].maxval)
967 			ret = SA_BAD_VALUE;
968 		}
969 	}
970 	return (ret);
971 }
972 
973 /*
974  * Check the length of the string
975  */
976 static int
977 string_length_check_validator(int index, char *value)
978 {
979 	int ret = SA_OK;
980 
981 	if (value == NULL)
982 		return (SA_BAD_VALUE);
983 	if (strlen(value) > smb_proto_options[index].maxval)
984 		ret = SA_BAD_VALUE;
985 	return (ret);
986 }
987 
988 /*
989  * Check yes/no
990  */
991 /*ARGSUSED*/
992 static int
993 true_false_validator(int index, char *value)
994 {
995 	if (value == NULL)
996 		return (SA_BAD_VALUE);
997 	if ((strcasecmp(value, "true") == 0) ||
998 	    (strcasecmp(value, "false") == 0))
999 		return (SA_OK);
1000 	return (SA_BAD_VALUE);
1001 }
1002 
1003 /*
1004  * If printing support is compiled in, this is the same as:
1005  * true_false_validator.  Otherwise, only allow false.
1006  */
1007 /*ARGSUSED*/
1008 static int
1009 print_enable_validator(int index, char *value)
1010 {
1011 	if (value == NULL)
1012 		return (SA_BAD_VALUE);
1013 
1014 #ifdef	HAVE_CUPS
1015 	if (strcasecmp(value, "true") == 0)
1016 		return (SA_OK);
1017 #endif
1018 	if (strcasecmp(value, "false") == 0)
1019 		return (SA_OK);
1020 
1021 	return (SA_BAD_VALUE);
1022 }
1023 
1024 /*
1025  * Check IP v4 address.
1026  */
1027 /*ARGSUSED*/
1028 static int
1029 ipv4_validator(int index, char *value)
1030 {
1031 	char sbytes[16];
1032 
1033 	if (value == NULL)
1034 		return (SA_OK);
1035 
1036 	if (strlen(value) == 0)
1037 		return (SA_OK);
1038 
1039 	if (inet_pton(AF_INET, value, (void *)sbytes) != 1)
1040 		return (SA_BAD_VALUE);
1041 
1042 	return (SA_OK);
1043 }
1044 
1045 /*
1046  * Check that the specified name is an IP address (v4 or v6) or a hostname.
1047  * Per RFC 1035 and 1123, names may contain alphanumeric characters, hyphens
1048  * and dots.  The first and last character of a label must be alphanumeric.
1049  * Interior characters may be alphanumeric or hypens.
1050  *
1051  * Domain names should not contain underscores but we allow them because
1052  * Windows names are often in non-compliance with this rule.
1053  */
1054 /*ARGSUSED*/
1055 static int
1056 hostname_validator(int index, char *value)
1057 {
1058 	char		sbytes[INET6_ADDRSTRLEN];
1059 	boolean_t	new_label = B_TRUE;
1060 	char		*p;
1061 	char		label_terminator;
1062 	int		len;
1063 
1064 	if (value == NULL)
1065 		return (SA_OK);
1066 
1067 	if ((len = strlen(value)) == 0)
1068 		return (SA_OK);
1069 
1070 	if (inet_pton(AF_INET, value, (void *)sbytes) == 1)
1071 		return (SA_OK);
1072 
1073 	if (inet_pton(AF_INET6, value, (void *)sbytes) == 1)
1074 		return (SA_OK);
1075 
1076 	if (len >= MAXHOSTNAMELEN)
1077 		return (SA_BAD_VALUE);
1078 
1079 	if (strspn(value, "0123456789.") == len)
1080 		return (SA_BAD_VALUE);
1081 
1082 	label_terminator = *value;
1083 
1084 	for (p = value; *p != '\0'; ++p) {
1085 		if (new_label) {
1086 			if (!isalnum(*p))
1087 				return (SA_BAD_VALUE);
1088 			new_label = B_FALSE;
1089 			label_terminator = *p;
1090 			continue;
1091 		}
1092 
1093 		if (*p == '.') {
1094 			if (!isalnum(label_terminator))
1095 				return (SA_BAD_VALUE);
1096 			new_label = B_TRUE;
1097 			label_terminator = *p;
1098 			continue;
1099 		}
1100 
1101 		label_terminator = *p;
1102 
1103 		if (isalnum(*p) || *p == '-' || *p == '_')
1104 			continue;
1105 
1106 		return (SA_BAD_VALUE);
1107 	}
1108 
1109 	if (!isalnum(label_terminator))
1110 		return (SA_BAD_VALUE);
1111 
1112 	return (SA_OK);
1113 }
1114 
1115 /*
1116  * Call back function for dlpi_walk.
1117  * Returns TRUE if interface name exists on the host.
1118  */
1119 static boolean_t
1120 smb_get_interface(const char *ifname, void *arg)
1121 {
1122 	smb_hostifs_walker_t *iterp = arg;
1123 
1124 	iterp->hiw_matchfound = (strcmp(ifname, iterp->hiw_ifname) == 0);
1125 
1126 	return (iterp->hiw_matchfound);
1127 }
1128 
1129 /*
1130  * Checks to see if the input interface exists on the host.
1131  * Returns B_TRUE if the match is found, B_FALSE otherwise.
1132  */
1133 static boolean_t
1134 smb_validate_interface(const char *ifname)
1135 {
1136 	smb_hostifs_walker_t	iter;
1137 
1138 	if ((ifname == NULL) || (*ifname == '\0'))
1139 		return (B_FALSE);
1140 
1141 	iter.hiw_ifname = ifname;
1142 	iter.hiw_matchfound = B_FALSE;
1143 	dlpi_walk(smb_get_interface, &iter, 0);
1144 
1145 	return (iter.hiw_matchfound);
1146 }
1147 
1148 /*
1149  * Check valid interfaces. Interface names value can be NULL or empty.
1150  * Returns SA_BAD_VALUE if interface cannot be found on the host.
1151  */
1152 /*ARGSUSED*/
1153 static int
1154 interface_validator(int index, char *value)
1155 {
1156 	char buf[16];
1157 	int ret = SA_OK;
1158 	char *ifname, *tmp, *p;
1159 
1160 	if (value == NULL || *value == '\0')
1161 		return (ret);
1162 
1163 	if (strlen(value) > MAX_VALUE_BUFLEN)
1164 		return (SA_BAD_VALUE);
1165 
1166 	if ((p = strdup(value)) == NULL)
1167 		return (SA_NO_MEMORY);
1168 
1169 	tmp = p;
1170 	while ((ifname = strsep(&tmp, ",")) != NULL) {
1171 		if (*ifname == '\0') {
1172 			ret = SA_BAD_VALUE;
1173 			break;
1174 		}
1175 
1176 		if (!smb_validate_interface(ifname)) {
1177 			if (inet_pton(AF_INET, ifname, (void *)buf) == 0) {
1178 				ret = SA_BAD_VALUE;
1179 				break;
1180 			}
1181 		}
1182 	}
1183 
1184 	free(p);
1185 	return (ret);
1186 }
1187 
1188 /*
1189  * Check path
1190  */
1191 /*ARGSUSED*/
1192 static int
1193 path_validator(int index, char *path)
1194 {
1195 	struct stat buffer;
1196 	int fd, status;
1197 
1198 	if (path == NULL)
1199 		return (SA_BAD_VALUE);
1200 
1201 	fd = open(path, O_RDONLY);
1202 	if (fd < 0)
1203 		return (SA_BAD_VALUE);
1204 
1205 	status = fstat(fd, &buffer);
1206 	(void) close(fd);
1207 
1208 	if (status < 0)
1209 		return (SA_BAD_VALUE);
1210 
1211 	if (buffer.st_mode & S_IFDIR)
1212 		return (SA_OK);
1213 	return (SA_BAD_VALUE);
1214 }
1215 
1216 /*
1217  * the protoset holds the defined options so we don't have to read
1218  * them multiple times
1219  */
1220 static sa_protocol_properties_t protoset;
1221 
1222 static int
1223 findprotoopt(char *name)
1224 {
1225 	int i;
1226 	char *sc_name;
1227 
1228 	for (i = 0; i < SMB_OPT_NUM; i++) {
1229 		sc_name = smb_config_getname(smb_proto_options[i].smb_index);
1230 		if (strcasecmp(sc_name, name) == 0)
1231 			return (i);
1232 	}
1233 
1234 	return (-1);
1235 }
1236 
1237 /*
1238  * smb_load_proto_properties()
1239  *
1240  * read the smb config values from SMF.
1241  */
1242 
1243 static int
1244 smb_load_proto_properties()
1245 {
1246 	sa_property_t prop;
1247 	char value[MAX_VALUE_BUFLEN];
1248 	char *name;
1249 	int index;
1250 	int ret = SA_OK;
1251 	int rc;
1252 
1253 	protoset = sa_create_protocol_properties(SMB_PROTOCOL_NAME);
1254 	if (protoset == NULL)
1255 		return (SA_NO_MEMORY);
1256 
1257 	for (index = 0; index < SMB_OPT_NUM && ret == SA_OK; index++) {
1258 		rc = smb_config_get(smb_proto_options[index].smb_index,
1259 		    value, sizeof (value));
1260 		if (rc != SMBD_SMF_OK)
1261 			continue;
1262 		name = smb_config_getname(smb_proto_options[index].smb_index);
1263 		prop = sa_create_property(name, value);
1264 		if (prop != NULL)
1265 			ret = sa_add_protocol_property(protoset, prop);
1266 		else
1267 			ret = SA_NO_MEMORY;
1268 	}
1269 	return (ret);
1270 }
1271 
1272 /*
1273  * smb_share_init()
1274  *
1275  * Initialize the smb plugin.
1276  */
1277 
1278 static int
1279 smb_share_init(void)
1280 {
1281 	if (sa_plugin_ops.sa_init != smb_share_init)
1282 		return (SA_SYSTEM_ERR);
1283 
1284 	smb_share_door_clnt_init();
1285 	return (smb_load_proto_properties());
1286 }
1287 
1288 /*
1289  * smb_share_fini()
1290  *
1291  */
1292 static void
1293 smb_share_fini(void)
1294 {
1295 	xmlFreeNode(protoset);
1296 	protoset = NULL;
1297 
1298 	smb_share_door_clnt_fini();
1299 }
1300 
1301 /*
1302  * smb_get_proto_set()
1303  *
1304  * Return an optionset with all the protocol specific properties in
1305  * it.
1306  */
1307 static sa_protocol_properties_t
1308 smb_get_proto_set(void)
1309 {
1310 	return (protoset);
1311 }
1312 
1313 /*
1314  * smb_enable_dependencies()
1315  *
1316  * SMBD_DEFAULT_INSTANCE_FMRI may have some dependencies that aren't
1317  * enabled. This will attempt to enable all of them.
1318  */
1319 static void
1320 smb_enable_dependencies(const char *fmri)
1321 {
1322 	scf_handle_t *handle;
1323 	scf_service_t *service;
1324 	scf_instance_t *inst = NULL;
1325 	scf_iter_t *iter;
1326 	scf_property_t *prop;
1327 	scf_value_t *value;
1328 	scf_propertygroup_t *pg;
1329 	scf_scope_t *scope;
1330 	char type[SCFTYPE_LEN];
1331 	char *dependency;
1332 	char *servname;
1333 	int maxlen;
1334 
1335 	/*
1336 	 * Get all required handles and storage.
1337 	 */
1338 	handle = scf_handle_create(SCF_VERSION);
1339 	if (handle == NULL)
1340 		return;
1341 
1342 	if (scf_handle_bind(handle) != 0) {
1343 		scf_handle_destroy(handle);
1344 		return;
1345 	}
1346 
1347 	maxlen = scf_limit(SCF_LIMIT_MAX_VALUE_LENGTH);
1348 	if (maxlen == (ssize_t)-1)
1349 		maxlen = MAXPATHLEN;
1350 
1351 	dependency = malloc(maxlen);
1352 
1353 	service = scf_service_create(handle);
1354 
1355 	iter = scf_iter_create(handle);
1356 
1357 	pg = scf_pg_create(handle);
1358 
1359 	prop = scf_property_create(handle);
1360 
1361 	value = scf_value_create(handle);
1362 
1363 	scope = scf_scope_create(handle);
1364 
1365 	if (service == NULL || iter == NULL || pg == NULL || prop == NULL ||
1366 	    value == NULL || scope == NULL || dependency == NULL)
1367 		goto done;
1368 
1369 	/*
1370 	 *  We passed in the FMRI for the default instance but for
1371 	 *  some things we need the simple form so construct it. Since
1372 	 *  we reuse the storage that dependency points to, we need to
1373 	 *  use the servname early.
1374 	 */
1375 	(void) snprintf(dependency, maxlen, "%s", fmri + sizeof ("svc:"));
1376 	servname = strrchr(dependency, ':');
1377 	if (servname == NULL)
1378 		goto done;
1379 	*servname = '\0';
1380 	servname = dependency;
1381 
1382 	/*
1383 	 * Setup to iterate over the service property groups, only
1384 	 * looking at those that are "dependency" types. The "entity"
1385 	 * property will have the FMRI of the service we are dependent
1386 	 * on.
1387 	 */
1388 	if (scf_handle_get_scope(handle, SCF_SCOPE_LOCAL, scope) != 0)
1389 		goto done;
1390 
1391 	if (scf_scope_get_service(scope, servname, service) != 0)
1392 		goto done;
1393 
1394 	if (scf_iter_service_pgs(iter, service) != 0)
1395 		goto done;
1396 
1397 	while (scf_iter_next_pg(iter, pg) > 0) {
1398 		char *services[2];
1399 		/*
1400 		 * Have a property group for the service. See if it is
1401 		 * a dependency pg and only do operations on those.
1402 		 */
1403 		if (scf_pg_get_type(pg, type, SCFTYPE_LEN) <= 0)
1404 			continue;
1405 
1406 		if (strncmp(type, SCF_GROUP_DEPENDENCY, SCFTYPE_LEN) != 0)
1407 			continue;
1408 		/*
1409 		 * Have a dependency.  Attempt to enable it.
1410 		 */
1411 		if (scf_pg_get_property(pg, SCF_PROPERTY_ENTITIES, prop) != 0)
1412 			continue;
1413 
1414 		if (scf_property_get_value(prop, value) != 0)
1415 			continue;
1416 
1417 		services[1] = NULL;
1418 
1419 		if (scf_value_get_as_string(value, dependency, maxlen) > 0) {
1420 			services[0] = dependency;
1421 			_check_services(services);
1422 		}
1423 	}
1424 
1425 done:
1426 	if (dependency != NULL)
1427 		free(dependency);
1428 	if (value != NULL)
1429 		scf_value_destroy(value);
1430 	if (prop != NULL)
1431 		scf_property_destroy(prop);
1432 	if (pg != NULL)
1433 		scf_pg_destroy(pg);
1434 	if (iter != NULL)
1435 		scf_iter_destroy(iter);
1436 	if (scope != NULL)
1437 		scf_scope_destroy(scope);
1438 	if (inst != NULL)
1439 		scf_instance_destroy(inst);
1440 	if (service != NULL)
1441 		scf_service_destroy(service);
1442 
1443 	(void) scf_handle_unbind(handle);
1444 	scf_handle_destroy(handle);
1445 }
1446 
1447 /*
1448  * How long to wait for service to come online
1449  */
1450 #define	WAIT_FOR_SERVICE	15
1451 
1452 /*
1453  * smb_enable_service()
1454  *
1455  */
1456 static int
1457 smb_enable_service(void)
1458 {
1459 	int i;
1460 	int ret = SA_OK;
1461 	char *service[] = { SMBD_DEFAULT_INSTANCE_FMRI, NULL };
1462 
1463 	if (!smb_isonline()) {
1464 		/*
1465 		 * Attempt to start the idmap, and other dependent
1466 		 * services, first.  If it fails, the SMB service will
1467 		 * ultimately fail so we use that as the error.  If we
1468 		 * don't try to enable idmap, smb won't start the
1469 		 * first time unless the admin has done it
1470 		 * manually. The service could be administratively
1471 		 * disabled so we won't always get started.
1472 		 */
1473 		smb_enable_dependencies(SMBD_DEFAULT_INSTANCE_FMRI);
1474 		_check_services(service);
1475 
1476 		/* Wait for service to come online */
1477 		for (i = 0; i < WAIT_FOR_SERVICE; i++) {
1478 			if (smb_isonline()) {
1479 				ret =  SA_OK;
1480 				break;
1481 			} else if (smb_ismaint()) {
1482 				/* maintenance requires help */
1483 				ret = SA_SYSTEM_ERR;
1484 				break;
1485 			} else if (smb_isdisabled()) {
1486 				/* disabled is ok */
1487 				ret = SA_OK;
1488 				break;
1489 			} else {
1490 				/* try another time */
1491 				ret = SA_BUSY;
1492 				(void) sleep(1);
1493 			}
1494 		}
1495 	}
1496 	return (ret);
1497 }
1498 
1499 /*
1500  * smb_validate_proto_prop(index, name, value)
1501  *
1502  * Verify that the property specified by name can take the new
1503  * value. This is a sanity check to prevent bad values getting into
1504  * the default files.
1505  */
1506 static int
1507 smb_validate_proto_prop(int index, char *name, char *value)
1508 {
1509 	if ((name == NULL) || (index < 0))
1510 		return (SA_BAD_VALUE);
1511 
1512 	if (smb_proto_options[index].validator == NULL)
1513 		return (SA_OK);
1514 
1515 	if (smb_proto_options[index].validator(index, value) == SA_OK)
1516 		return (SA_OK);
1517 	return (SA_BAD_VALUE);
1518 }
1519 
1520 /*
1521  * smb_set_proto_prop(prop)
1522  *
1523  * check that prop is valid.
1524  */
1525 /*ARGSUSED*/
1526 static int
1527 smb_set_proto_prop(sa_property_t prop)
1528 {
1529 	int ret = SA_OK;
1530 	char *name;
1531 	char *value;
1532 	int index = -1;
1533 	struct smb_proto_option_defs *opt;
1534 
1535 	name = sa_get_property_attr(prop, "type");
1536 	value = sa_get_property_attr(prop, "value");
1537 	if (name != NULL && value != NULL) {
1538 		index = findprotoopt(name);
1539 		if (index >= 0) {
1540 			/* should test for valid value */
1541 			ret = smb_validate_proto_prop(index, name, value);
1542 			if (ret == SA_OK) {
1543 				opt = &smb_proto_options[index];
1544 
1545 				/* Save to SMF */
1546 				(void) smb_config_set(opt->smb_index, value);
1547 				/*
1548 				 * Specialized refresh mechanisms can
1549 				 * be flagged in the proto_options and
1550 				 * processed here.
1551 				 */
1552 				if (opt->refresh & SMB_REFRESH_REFRESH)
1553 					(void) smf_refresh_instance(
1554 					    SMBD_DEFAULT_INSTANCE_FMRI);
1555 				else if (opt->refresh & SMB_REFRESH_RESTART)
1556 					(void) smf_restart_instance(
1557 					    SMBD_DEFAULT_INSTANCE_FMRI);
1558 			}
1559 		}
1560 	}
1561 
1562 	if (name != NULL)
1563 		sa_free_attr_string(name);
1564 	if (value != NULL)
1565 		sa_free_attr_string(value);
1566 
1567 	return (ret);
1568 }
1569 
1570 /*
1571  * smb_get_status()
1572  *
1573  * What is the current status of the smbd? We use the SMF state here.
1574  * Caller must free the returned value.
1575  */
1576 
1577 static char *
1578 smb_get_status(void)
1579 {
1580 	char *state = NULL;
1581 	state = smf_get_state(SMBD_DEFAULT_INSTANCE_FMRI);
1582 	return (state != NULL ? state : "-");
1583 }
1584 
1585 /*
1586  * This protocol plugin require resource names
1587  */
1588 static uint64_t
1589 smb_share_features(void)
1590 {
1591 	return (SA_FEATURE_RESOURCE | SA_FEATURE_ALLOWSUBDIRS |
1592 	    SA_FEATURE_ALLOWPARDIRS | SA_FEATURE_SERVER);
1593 }
1594 
1595 /*
1596  * This should be used to convert smb_share_t to sa_resource_t
1597  * Should only be needed to build transient shares/resources to be
1598  * supplied to sharemgr to display.
1599  */
1600 static int
1601 smb_add_transient(sa_handle_t handle, smb_share_t *si)
1602 {
1603 	int err;
1604 	sa_share_t share;
1605 	sa_group_t group;
1606 	sa_resource_t resource;
1607 	nvlist_t *nvl;
1608 	char *opt;
1609 
1610 	if (si == NULL)
1611 		return (SA_INVALID_NAME);
1612 
1613 	if ((share = sa_find_share(handle, si->shr_path)) == NULL) {
1614 		if ((group = smb_get_defaultgrp(handle)) == NULL)
1615 			return (SA_NO_SUCH_GROUP);
1616 
1617 		share = sa_get_share(group, si->shr_path);
1618 		if (share == NULL) {
1619 			share = sa_add_share(group, si->shr_path,
1620 			    SA_SHARE_TRANSIENT, &err);
1621 			if (share == NULL)
1622 				return (SA_NO_SUCH_PATH);
1623 		}
1624 	}
1625 
1626 	/*
1627 	 * Now handle the resource. Make sure that the resource is
1628 	 * transient and added to the share.
1629 	 */
1630 	resource = sa_get_share_resource(share, si->shr_name);
1631 	if (resource == NULL) {
1632 		resource = sa_add_resource(share,
1633 		    si->shr_name, SA_SHARE_TRANSIENT, &err);
1634 		if (resource == NULL)
1635 			return (SA_NO_SUCH_RESOURCE);
1636 	}
1637 
1638 	if (si->shr_cmnt[0] != '\0')
1639 		(void) sa_set_resource_description(resource, si->shr_cmnt);
1640 
1641 	if (si->shr_container[0] != '\0')
1642 		(void) sa_set_resource_attr(resource, SHOPT_AD_CONTAINER,
1643 		    si->shr_container);
1644 
1645 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
1646 		return (SA_NO_MEMORY);
1647 
1648 	if ((opt = smb_csc_name(si)) != NULL)
1649 		err |= nvlist_add_string(nvl, SHOPT_CSC, opt);
1650 
1651 	opt = (si->shr_flags & SMB_SHRF_ABE) ? "true" : "false";
1652 	err |= nvlist_add_string(nvl, SHOPT_ABE, opt);
1653 
1654 	if ((si->shr_flags & SMB_SHRF_AUTOHOME) == 0) {
1655 		opt = (si->shr_flags & SMB_SHRF_GUEST_OK) ? "true" : "false";
1656 		err |= nvlist_add_string(nvl, SHOPT_GUEST, opt);
1657 	}
1658 
1659 	if (si->shr_access_ro[0] != '\0')
1660 		err |= nvlist_add_string(nvl, SHOPT_RO, si->shr_access_ro);
1661 
1662 	if (si->shr_access_rw[0] != '\0')
1663 		err |= nvlist_add_string(nvl, SHOPT_RW, si->shr_access_rw);
1664 
1665 	if (si->shr_access_none[0] != '\0')
1666 		err |= nvlist_add_string(nvl, SHOPT_NONE, si->shr_access_none);
1667 
1668 	if (err) {
1669 		nvlist_free(nvl);
1670 		return (SA_CONFIG_ERR);
1671 	}
1672 
1673 	err = smb_update_optionset_props(handle, resource, nvl);
1674 
1675 	nvlist_free(nvl);
1676 	return (err);
1677 }
1678 
1679 /*
1680  * Return smb transient shares.
1681  */
1682 static int
1683 smb_list_transient(sa_handle_t handle)
1684 {
1685 	int i, offset;
1686 	smb_shrlist_t list;
1687 	int res;
1688 
1689 	if (smb_share_count() <= 0)
1690 		return (SA_OK);
1691 
1692 	offset = 0;
1693 	while (smb_share_list(offset, &list) == NERR_Success) {
1694 		if (list.sl_cnt == 0)
1695 			break;
1696 
1697 		for (i = 0; i < list.sl_cnt; i++) {
1698 			res = smb_add_transient(handle, &(list.sl_shares[i]));
1699 			if (res != SA_OK)
1700 				return (res);
1701 		}
1702 		offset += list.sl_cnt;
1703 	}
1704 
1705 	return (SA_OK);
1706 }
1707 
1708 /*
1709  * fix_resource_name(share, name,  prefix)
1710  *
1711  * Construct a name where the ZFS dataset has the prefix replaced with "name".
1712  */
1713 static char *
1714 fix_resource_name(sa_share_t share, char *name, char *prefix)
1715 {
1716 	char buf[SA_MAX_RESOURCE_NAME + 1];
1717 	char *dataset;
1718 	size_t bufsz = SA_MAX_RESOURCE_NAME + 1;
1719 	size_t prelen;
1720 
1721 	if (prefix == NULL)
1722 		return (strdup(name));
1723 
1724 	dataset = sa_get_share_attr(share, "dataset");
1725 	if (dataset == NULL)
1726 		return (strdup(name));
1727 
1728 	(void) strlcpy(buf, name, bufsz);
1729 	prelen = strlen(prefix);
1730 
1731 	if (strncmp(dataset, prefix, prelen) == 0)
1732 		(void) strlcat(buf, dataset + prelen, bufsz);
1733 
1734 	sa_free_attr_string(dataset);
1735 	sa_fix_resource_name(buf);
1736 	return (strdup(buf));
1737 }
1738 
1739 /*
1740  * smb_parse_optstring(group, options)
1741  *
1742  * parse a compact option string into individual options. This allows
1743  * ZFS sharesmb and sharemgr "share" command to work.  group can be a
1744  * group, a share or a resource.
1745  */
1746 static int
1747 smb_parse_optstring(sa_group_t group, char *options)
1748 {
1749 	char *dup;
1750 	char *base;
1751 	char *lasts;
1752 	char *token;
1753 	sa_optionset_t optionset;
1754 	sa_group_t parent = NULL;
1755 	sa_resource_t resource = NULL;
1756 	int iszfs = 0;
1757 	int persist = 0;
1758 	int need_optionset = 0;
1759 	int ret = SA_OK;
1760 	sa_property_t prop;
1761 
1762 	/*
1763 	 * In order to not attempt to change ZFS properties unless
1764 	 * absolutely necessary, we never do it in the legacy parsing
1765 	 * so we need to keep track of this.
1766 	 */
1767 	if (sa_is_share(group)) {
1768 		char *zfs;
1769 
1770 		parent = sa_get_parent_group(group);
1771 		if (parent != NULL) {
1772 			zfs = sa_get_group_attr(parent, "zfs");
1773 			if (zfs != NULL) {
1774 				sa_free_attr_string(zfs);
1775 				iszfs = 1;
1776 			}
1777 		}
1778 	} else {
1779 		iszfs = sa_group_is_zfs(group);
1780 		/*
1781 		 * If a ZFS group, then we need to see if a resource
1782 		 * name is being set. If so, bail with
1783 		 * SA_PROP_SHARE_ONLY, so we come back in with a share
1784 		 * instead of a group.
1785 		 */
1786 		if (iszfs ||
1787 		    strncmp(options, "name=", sizeof ("name=") - 1) == 0 ||
1788 		    strstr(options, ",name=") != NULL) {
1789 			return (SA_PROP_SHARE_ONLY);
1790 		}
1791 	}
1792 
1793 	/* do we have an existing optionset? */
1794 	optionset = sa_get_optionset(group, "smb");
1795 	if (optionset == NULL) {
1796 		/* didn't find existing optionset so create one */
1797 		optionset = sa_create_optionset(group, "smb");
1798 		if (optionset == NULL)
1799 			return (SA_NO_MEMORY);
1800 	} else {
1801 		/*
1802 		 * If an optionset already exists, we've come through
1803 		 * twice so ignore the second time.
1804 		 */
1805 		return (ret);
1806 	}
1807 
1808 	/* We need a copy of options for the next part. */
1809 	dup = strdup(options);
1810 	if (dup == NULL)
1811 		return (SA_NO_MEMORY);
1812 
1813 	/*
1814 	 * SMB properties are straightforward and are strings,
1815 	 * integers or booleans.  Properties are separated by
1816 	 * commas. It will be necessary to parse quotes due to some
1817 	 * strings not having a restricted characters set.
1818 	 *
1819 	 * Note that names will create a resource. For now, if there
1820 	 * is a set of properties "before" the first name="", those
1821 	 * properties will be placed on the group.
1822 	 */
1823 	persist = sa_is_persistent(group);
1824 	base = dup;
1825 	token = dup;
1826 	lasts = NULL;
1827 	while (token != NULL && ret == SA_OK) {
1828 		ret = SA_OK;
1829 		token = strtok_r(base, ",", &lasts);
1830 		base = NULL;
1831 		if (token != NULL) {
1832 			char *value;
1833 			/*
1834 			 * All SMB properties have values so there
1835 			 * MUST be an '=' character.  If it doesn't,
1836 			 * it is a syntax error.
1837 			 */
1838 			value = strchr(token, '=');
1839 			if (value != NULL) {
1840 				*value++ = '\0';
1841 			} else {
1842 				ret = SA_SYNTAX_ERR;
1843 				break;
1844 			}
1845 			/*
1846 			 * We may need to handle a "name" property
1847 			 * that is a ZFS imposed resource name. Each
1848 			 * name would trigger getting a new "resource"
1849 			 * to put properties on. For now, assume no
1850 			 * "name" property for special handling.
1851 			 */
1852 
1853 			if (strcmp(token, SHOPT_NAME) == 0) {
1854 				char *prefix;
1855 				char *name = NULL;
1856 				/*
1857 				 * We have a name, so now work on the
1858 				 * resource level. We have a "share"
1859 				 * in "group" due to the caller having
1860 				 * added it. If we are called with a
1861 				 * group, the check for group/share
1862 				 * at the beginning of this function
1863 				 * will bail out the parse if there is a
1864 				 * "name" but no share.
1865 				 */
1866 				if (!iszfs) {
1867 					ret = SA_SYNTAX_ERR;
1868 					break;
1869 				}
1870 				/*
1871 				 * Make sure the parent group has the
1872 				 * "prefix" property since we will
1873 				 * need to use this for constructing
1874 				 * inherited name= values.
1875 				 */
1876 				prefix = sa_get_group_attr(parent, "prefix");
1877 				if (prefix == NULL) {
1878 					prefix = sa_get_group_attr(parent,
1879 					    "name");
1880 					if (prefix != NULL) {
1881 						(void) sa_set_group_attr(parent,
1882 						    "prefix", prefix);
1883 					}
1884 				}
1885 				name = fix_resource_name((sa_share_t)group,
1886 				    value, prefix);
1887 				if (name != NULL) {
1888 					resource = sa_add_resource(
1889 					    (sa_share_t)group, name,
1890 					    SA_SHARE_TRANSIENT, &ret);
1891 					sa_free_attr_string(name);
1892 				} else {
1893 					ret = SA_NO_MEMORY;
1894 				}
1895 				if (prefix != NULL)
1896 					sa_free_attr_string(prefix);
1897 
1898 				/* A resource level optionset is needed */
1899 
1900 				need_optionset = 1;
1901 				if (resource == NULL) {
1902 					ret = SA_NO_MEMORY;
1903 					break;
1904 				}
1905 				continue;
1906 			}
1907 
1908 			if (iszfs && strcmp(token, SHOPT_DESCRIPTION) == 0) {
1909 				if (resource == NULL)
1910 					(void) sa_set_share_description(
1911 					    (sa_share_t)group, value);
1912 				else
1913 					(void) sa_set_resource_description(
1914 					    resource, value);
1915 				continue;
1916 			}
1917 
1918 			if (need_optionset) {
1919 				optionset = sa_create_optionset(resource,
1920 				    "smb");
1921 				need_optionset = 0;
1922 			}
1923 
1924 			prop = sa_create_property(token, value);
1925 			if (prop == NULL)
1926 				ret = SA_NO_MEMORY;
1927 			else
1928 				ret = sa_add_property(optionset, prop);
1929 			if (ret != SA_OK)
1930 				break;
1931 			if (!iszfs)
1932 				ret = sa_commit_properties(optionset, !persist);
1933 		}
1934 	}
1935 	free(dup);
1936 	return (ret);
1937 }
1938 
1939 /*
1940  * smb_sprint_option(rbuff, rbuffsize, incr, prop, sep)
1941  *
1942  * provides a mechanism to format SMB properties into legacy output
1943  * format. If the buffer would overflow, it is reallocated and grown
1944  * as appropriate. Special cases of converting internal form of values
1945  * to those used by "share" are done. this function does one property
1946  * at a time.
1947  */
1948 
1949 static void
1950 smb_sprint_option(char **rbuff, size_t *rbuffsize, size_t incr,
1951 			sa_property_t prop, int sep)
1952 {
1953 	char *name;
1954 	char *value;
1955 	int curlen;
1956 	char *buff = *rbuff;
1957 	size_t buffsize = *rbuffsize;
1958 
1959 	name = sa_get_property_attr(prop, "type");
1960 	value = sa_get_property_attr(prop, "value");
1961 	if (buff != NULL)
1962 		curlen = strlen(buff);
1963 	else
1964 		curlen = 0;
1965 	if (name != NULL) {
1966 		int len;
1967 		len = strlen(name) + sep;
1968 
1969 		/*
1970 		 * A future RFE would be to replace this with more
1971 		 * generic code and to possibly handle more types.
1972 		 *
1973 		 * For now, everything else is treated as a string. If
1974 		 * we get any properties that aren't exactly
1975 		 * name/value pairs, we may need to
1976 		 * interpret/transform.
1977 		 */
1978 		if (value != NULL)
1979 			len += 1 + strlen(value);
1980 
1981 		while (buffsize <= (curlen + len)) {
1982 			/* need more room */
1983 			buffsize += incr;
1984 			buff = realloc(buff, buffsize);
1985 			*rbuff = buff;
1986 			*rbuffsize = buffsize;
1987 			if (buff == NULL) {
1988 				/* realloc failed so free everything */
1989 				if (*rbuff != NULL)
1990 					free(*rbuff);
1991 				goto err;
1992 			}
1993 		}
1994 		if (buff == NULL)
1995 			goto err;
1996 		(void) snprintf(buff + curlen, buffsize - curlen,
1997 		    "%s%s=%s", sep ? "," : "",
1998 		    name, value != NULL ? value : "\"\"");
1999 
2000 	}
2001 err:
2002 	if (name != NULL)
2003 		sa_free_attr_string(name);
2004 	if (value != NULL)
2005 		sa_free_attr_string(value);
2006 }
2007 
2008 /*
2009  * smb_format_resource_options(resource, hier)
2010  *
2011  * format all the options on the group into a flattened option
2012  * string. If hier is non-zero, walk up the tree to get inherited
2013  * options.
2014  */
2015 
2016 static char *
2017 smb_format_options(sa_group_t group, int hier)
2018 {
2019 	sa_optionset_t options = NULL;
2020 	sa_property_t prop;
2021 	int sep = 0;
2022 	char *buff;
2023 	size_t buffsize;
2024 
2025 
2026 	buff = malloc(OPT_CHUNK);
2027 	if (buff == NULL)
2028 		return (NULL);
2029 
2030 	buff[0] = '\0';
2031 	buffsize = OPT_CHUNK;
2032 
2033 	/*
2034 	 * We may have a an optionset relative to this item. format
2035 	 * these if we find them and then add any security definitions.
2036 	 */
2037 
2038 	options = sa_get_derived_optionset(group, "smb", hier);
2039 
2040 	/*
2041 	 * do the default set first but skip any option that is also
2042 	 * in the protocol specific optionset.
2043 	 */
2044 	if (options != NULL) {
2045 		for (prop = sa_get_property(options, NULL);
2046 		    prop != NULL; prop = sa_get_next_property(prop)) {
2047 			/*
2048 			 * use this one since we skipped any
2049 			 * of these that were also in
2050 			 * optdefault
2051 			 */
2052 			smb_sprint_option(&buff, &buffsize, OPT_CHUNK,
2053 			    prop, sep);
2054 			if (buff == NULL) {
2055 				/*
2056 				 * buff could become NULL if there
2057 				 * isn't enough memory for
2058 				 * smb_sprint_option to realloc()
2059 				 * as necessary. We can't really
2060 				 * do anything about it at this
2061 				 * point so we return NULL.  The
2062 				 * caller should handle the
2063 				 * failure.
2064 				 */
2065 				if (options != NULL)
2066 					sa_free_derived_optionset(
2067 					    options);
2068 				return (buff);
2069 			}
2070 			sep = 1;
2071 		}
2072 	}
2073 
2074 	if (options != NULL)
2075 		sa_free_derived_optionset(options);
2076 	return (buff);
2077 }
2078 
2079 /*
2080  * smb_rename_resource(resource, newname)
2081  *
2082  * Change the current exported name of the resource to newname.
2083  */
2084 /*ARGSUSED*/
2085 int
2086 smb_rename_resource(sa_handle_t handle, sa_resource_t resource, char *newname)
2087 {
2088 	int ret = SA_OK;
2089 	int err;
2090 	char *oldname;
2091 
2092 	if (!smb_isonline())
2093 		return (SA_OK);
2094 
2095 	oldname = sa_get_resource_attr(resource, "name");
2096 	if (oldname == NULL)
2097 		return (SA_NO_SUCH_RESOURCE);
2098 
2099 	err = smb_share_rename(oldname, newname);
2100 
2101 	sa_free_attr_string(oldname);
2102 
2103 	/* improve error values somewhat */
2104 	switch (err) {
2105 	case NERR_Success:
2106 		break;
2107 	case NERR_InternalError:
2108 		ret = SA_SYSTEM_ERR;
2109 		break;
2110 	case NERR_DuplicateShare:
2111 		ret = SA_DUPLICATE_NAME;
2112 		break;
2113 	default:
2114 		ret = SA_CONFIG_ERR;
2115 		break;
2116 	}
2117 
2118 	return (ret);
2119 }
2120 
2121 static int
2122 smb_build_shareinfo(sa_share_t share, sa_resource_t resource, smb_share_t *si)
2123 {
2124 	sa_optionset_t opts;
2125 	char *path;
2126 	char *rname;
2127 	char *val = NULL;
2128 	char csc_value[SMB_CSC_BUFSZ];
2129 
2130 	bzero(si, sizeof (smb_share_t));
2131 
2132 	if ((path = sa_get_share_attr(share, "path")) == NULL)
2133 		return (SA_NO_SUCH_PATH);
2134 
2135 	if ((rname = sa_get_resource_attr(resource, "name")) == NULL) {
2136 		sa_free_attr_string(path);
2137 		return (SA_NO_SUCH_RESOURCE);
2138 	}
2139 
2140 	(void) strlcpy(si->shr_path, path, sizeof (si->shr_path));
2141 	(void) strlcpy(si->shr_name, rname, sizeof (si->shr_name));
2142 	sa_free_attr_string(path);
2143 	sa_free_attr_string(rname);
2144 
2145 	val = sa_get_resource_description(resource);
2146 	if (val == NULL)
2147 		val = sa_get_share_description(share);
2148 
2149 	if (val != NULL) {
2150 		(void) strlcpy(si->shr_cmnt, val, sizeof (si->shr_cmnt));
2151 		sa_free_share_description(val);
2152 	}
2153 
2154 	si->shr_flags = (sa_is_persistent(share))
2155 	    ? SMB_SHRF_PERM : SMB_SHRF_TRANS;
2156 
2157 	opts = sa_get_derived_optionset(resource, SMB_PROTOCOL_NAME, 1);
2158 	if (opts == NULL)
2159 		return (SA_OK);
2160 
2161 	if (smb_saprop_getbool(opts, SHOPT_CATIA))
2162 		si->shr_flags |= SMB_SHRF_CATIA;
2163 
2164 	if (smb_saprop_getbool(opts, SHOPT_ABE))
2165 		si->shr_flags |= SMB_SHRF_ABE;
2166 
2167 	if (smb_saprop_getbool(opts, SHOPT_GUEST))
2168 		si->shr_flags |= SMB_SHRF_GUEST_OK;
2169 
2170 	if (smb_saprop_getbool(opts, SHOPT_DFSROOT))
2171 		si->shr_flags |= SMB_SHRF_DFSROOT;
2172 
2173 	(void) smb_saprop_getstr(opts, SHOPT_AD_CONTAINER, si->shr_container,
2174 	    sizeof (si->shr_container));
2175 
2176 	if (smb_saprop_getstr(opts, SHOPT_CSC, csc_value, sizeof (csc_value)))
2177 		smb_csc_option(csc_value, si);
2178 
2179 	if (smb_saprop_getstr(opts, SHOPT_RO, si->shr_access_ro,
2180 	    sizeof (si->shr_access_ro)))
2181 		si->shr_flags |= SMB_SHRF_ACC_RO;
2182 
2183 	if (smb_saprop_getstr(opts, SHOPT_RW, si->shr_access_rw,
2184 	    sizeof (si->shr_access_rw)))
2185 		si->shr_flags |= SMB_SHRF_ACC_RW;
2186 
2187 	if (smb_saprop_getstr(opts, SHOPT_NONE, si->shr_access_none,
2188 	    sizeof (si->shr_access_none)))
2189 		si->shr_flags |= SMB_SHRF_ACC_NONE;
2190 
2191 	sa_free_derived_optionset(opts);
2192 	return (SA_OK);
2193 }
2194 
2195 /*
2196  * Map a client-side caching (CSC) option to the appropriate share
2197  * flag.  Only one option is allowed; an error will be logged if
2198  * multiple options have been specified.  We don't need to do anything
2199  * about multiple values here because the SRVSVC will not recognize
2200  * a value containing multiple flags and will return the default value.
2201  *
2202  * If the option value is not recognized, it will be ignored: invalid
2203  * values will typically be caught and rejected by sharemgr.
2204  */
2205 static void
2206 smb_csc_option(const char *value, smb_share_t *si)
2207 {
2208 	char buf[SMB_CSC_BUFSZ];
2209 	int i;
2210 
2211 	for (i = 0; i < (sizeof (cscopt) / sizeof (cscopt[0])); ++i) {
2212 		if (strcasecmp(value, cscopt[i].value) == 0) {
2213 			si->shr_flags |= cscopt[i].flag;
2214 			break;
2215 		}
2216 	}
2217 
2218 	switch (si->shr_flags & SMB_SHRF_CSC_MASK) {
2219 	case 0:
2220 	case SMB_SHRF_CSC_DISABLED:
2221 	case SMB_SHRF_CSC_MANUAL:
2222 	case SMB_SHRF_CSC_AUTO:
2223 	case SMB_SHRF_CSC_VDO:
2224 		break;
2225 
2226 	default:
2227 		buf[0] = '\0';
2228 
2229 		for (i = 0; i < (sizeof (cscopt) / sizeof (cscopt[0])); ++i) {
2230 			if (si->shr_flags & cscopt[i].flag) {
2231 				(void) strlcat(buf, " ", SMB_CSC_BUFSZ);
2232 				(void) strlcat(buf, cscopt[i].value,
2233 				    SMB_CSC_BUFSZ);
2234 			}
2235 		}
2236 
2237 		syslog(LOG_ERR, "csc option conflict:%s", buf);
2238 		break;
2239 	}
2240 }
2241 
2242 /*
2243  * Return the option name for the first CSC flag (there should be only
2244  * one) encountered in the share flags.
2245  */
2246 static char *
2247 smb_csc_name(const smb_share_t *si)
2248 {
2249 	int i;
2250 
2251 	for (i = 0; i < (sizeof (cscopt) / sizeof (cscopt[0])); ++i) {
2252 		if (si->shr_flags & cscopt[i].flag)
2253 			return (cscopt[i].value);
2254 	}
2255 
2256 	return (NULL);
2257 }
2258 
2259 /*
2260  * smb_get_defaultgrp
2261  *
2262  * If default group for CIFS shares (i.e. "smb") exists
2263  * then it will return the group handle, otherwise it will
2264  * create the group and return the handle.
2265  *
2266  * All the shares created by CIFS clients (this is only possible
2267  * via RPC) will be added to "smb" groups.
2268  */
2269 static sa_group_t
2270 smb_get_defaultgrp(sa_handle_t handle)
2271 {
2272 	sa_group_t group = NULL;
2273 	int err;
2274 
2275 	group = sa_get_group(handle, SMB_DEFAULT_SHARE_GROUP);
2276 	if (group != NULL)
2277 		return (group);
2278 
2279 	group = sa_create_group(handle, SMB_DEFAULT_SHARE_GROUP, &err);
2280 	if (group == NULL)
2281 		return (NULL);
2282 
2283 	if (sa_create_optionset(group, SMB_DEFAULT_SHARE_GROUP) == NULL) {
2284 		(void) sa_remove_group(group);
2285 		group = NULL;
2286 	}
2287 
2288 	return (group);
2289 }
2290 
2291 /*
2292  * Checks to see if the command args are the supported substitution specifier.
2293  * i.e. <cmd> %U %S
2294  */
2295 static int
2296 cmd_validator(int index, char *value)
2297 {
2298 	char cmd[MAXPATHLEN];
2299 	char *ptr, *v;
2300 	boolean_t skip_cmdname;
2301 
2302 	if (string_length_check_validator(index, value) != SA_OK)
2303 		return (SA_BAD_VALUE);
2304 
2305 	if (*value == '\0')
2306 		return (SA_OK);
2307 
2308 	(void) strlcpy(cmd, value, sizeof (cmd));
2309 
2310 	ptr = cmd;
2311 	skip_cmdname = B_TRUE;
2312 	do {
2313 		if ((v = strsep(&ptr, " ")) == NULL)
2314 			break;
2315 
2316 		if (*v != '\0') {
2317 
2318 			if (skip_cmdname) {
2319 				skip_cmdname = B_FALSE;
2320 				continue;
2321 			}
2322 
2323 			if ((strlen(v) != 2) || *v != '%')
2324 				return (SA_BAD_VALUE);
2325 
2326 			if (strpbrk(v, SMB_VALID_SUB_CHRS) == NULL)
2327 				return (SA_BAD_VALUE);
2328 		}
2329 
2330 	} while (v != NULL);
2331 
2332 	/*
2333 	 * If skip_cmdname is still true then the string contains
2334 	 * only spaces.  Don't allow such a string.
2335 	 */
2336 	if (skip_cmdname)
2337 		return (SA_BAD_VALUE);
2338 
2339 	return (SA_OK);
2340 }
2341 
2342 /*ARGSUSED*/
2343 static int
2344 disposition_validator(int index, char *value)
2345 {
2346 	if (value == NULL)
2347 		return (SA_BAD_VALUE);
2348 
2349 	if (*value == '\0')
2350 		return (SA_OK);
2351 
2352 	if ((strcasecmp(value, SMB_EXEC_DISP_CONTINUE) == 0) ||
2353 	    (strcasecmp(value, SMB_EXEC_DISP_TERMINATE) == 0))
2354 		return (SA_OK);
2355 
2356 	return (SA_BAD_VALUE);
2357 }
2358 
2359 /*
2360  * Updates the optionset properties of the share resource.
2361  * The properties are given as a list of name-value pair.
2362  * The name argument should be the optionset property name and the value
2363  * should be a valid value for the specified property.
2364  *
2365  * When calling this function for permanent shares, the caller must also
2366  * call sa_commit_properties() to commit the changes to SMF.
2367  */
2368 static int
2369 smb_update_optionset_props(sa_handle_t handle, sa_resource_t resource,
2370     nvlist_t *nvl)
2371 {
2372 	sa_property_t prop;
2373 	sa_optionset_t opts;
2374 	int err = SA_OK;
2375 	nvpair_t *cur;
2376 	char *name, *val;
2377 
2378 	if ((opts = sa_get_optionset(resource, SMB_PROTOCOL_NAME)) == NULL) {
2379 		opts = sa_create_optionset(resource, SMB_PROTOCOL_NAME);
2380 		if (opts == NULL)
2381 			return (SA_CONFIG_ERR);
2382 	}
2383 
2384 	cur = nvlist_next_nvpair(nvl, NULL);
2385 	while (cur != NULL) {
2386 		name = nvpair_name(cur);
2387 		err = nvpair_value_string(cur, &val);
2388 		if ((err != 0) || (name == NULL) || (val == NULL)) {
2389 			err = SA_CONFIG_ERR;
2390 			break;
2391 		}
2392 
2393 		prop = NULL;
2394 		if ((prop = sa_get_property(opts, name)) == NULL) {
2395 			prop = sa_create_property(name, val);
2396 			if (prop != NULL) {
2397 				err = sa_valid_property(handle, opts,
2398 				    SMB_PROTOCOL_NAME, prop);
2399 				if (err != SA_OK) {
2400 					(void) sa_remove_property(prop);
2401 					break;
2402 				}
2403 			}
2404 			err = sa_add_property(opts, prop);
2405 			if (err != SA_OK)
2406 				break;
2407 		} else {
2408 			err = sa_update_property(prop, val);
2409 			if (err != SA_OK)
2410 				break;
2411 		}
2412 
2413 		cur = nvlist_next_nvpair(nvl, cur);
2414 	}
2415 
2416 	return (err);
2417 }
2418 
2419 static boolean_t
2420 smb_saprop_getbool(sa_optionset_t opts, char *propname)
2421 {
2422 	sa_property_t prop;
2423 	char *val;
2424 	boolean_t propval = B_FALSE;
2425 
2426 	prop = sa_get_property(opts, propname);
2427 	if ((val = sa_get_property_attr(prop, "value")) != NULL) {
2428 		if ((strcasecmp(val, "true") == 0) || (strcmp(val, "1") == 0))
2429 			propval = B_TRUE;
2430 		free(val);
2431 	}
2432 
2433 	return (propval);
2434 }
2435 
2436 static boolean_t
2437 smb_saprop_getstr(sa_optionset_t opts, char *propname, char *buf, size_t bufsz)
2438 {
2439 	sa_property_t prop;
2440 	char *val;
2441 
2442 	prop = sa_get_property(opts, propname);
2443 	if ((val = sa_get_property_attr(prop, "value")) != NULL) {
2444 		(void) strlcpy(buf, val, bufsz);
2445 		free(val);
2446 		return (B_TRUE);
2447 	}
2448 
2449 	return (B_FALSE);
2450 }
2451