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