kill tsol ("Trusted Solaris") aka TX ("Trusted Extensions")
[unleashed.git] / usr / src / lib / libshare / smb / libshare_smb.c
blobd3293056be02850a6c99fc0883ae0d772af0cda5
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
23 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright 2015 Nexenta Systems, Inc. All rights reserved.
28 * SMB specific functions
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 <priv.h>
43 #include "libshare.h"
44 #include "libshare_impl.h"
45 #include <pwd.h>
46 #include <limits.h>
47 #include <libscf.h>
48 #include <libscf_priv.h>
49 #include <strings.h>
50 #include <sys/cfgparam.h>
51 #include "libshare_smb.h"
52 #include <rpcsvc/daemon_utils.h>
53 #include <smbsrv/smb_share.h>
54 #include <smbsrv/smbinfo.h>
55 #include <smbsrv/libsmb.h>
56 #include <libdlpi.h>
58 #define SMB_CSC_BUFSZ 64
60 #define SMB_VALID_SUB_CHRS "UDhMLmIiSPu" /* substitution characters */
62 /* internal functions */
63 static int smb_share_init(void);
64 static void smb_share_fini(void);
65 static int smb_enable_share(sa_share_t);
66 static int smb_share_changed(sa_share_t);
67 static int smb_resource_changed(sa_resource_t);
68 static int smb_rename_resource(sa_handle_t, sa_resource_t, char *);
69 static int smb_disable_share(sa_share_t share, char *);
70 static int smb_validate_property(sa_handle_t, sa_property_t, sa_optionset_t);
71 static int smb_set_proto_prop(sa_property_t);
72 static sa_protocol_properties_t smb_get_proto_set(void);
73 static char *smb_get_status(void);
74 static int smb_parse_optstring(sa_group_t, char *);
75 static char *smb_format_options(sa_group_t, int);
77 static int smb_enable_service(void);
79 static int range_check_validator(int, char *);
80 static int range_check_validator_zero_ok(int, char *);
81 static int string_length_check_validator(int, char *);
82 static int print_enable_validator(int, char *);
83 static int true_false_validator(int, char *);
84 static int ipv4_validator(int, char *);
85 static int hostname_validator(int, char *);
86 static int path_validator(int, char *);
87 static int cmd_validator(int, char *);
88 static int disposition_validator(int, char *);
89 static int max_protocol_validator(int, char *);
91 static int smb_enable_resource(sa_resource_t);
92 static int smb_disable_resource(sa_resource_t);
93 static uint64_t smb_share_features(void);
94 static int smb_list_transient(sa_handle_t);
96 static int smb_build_shareinfo(sa_share_t, sa_resource_t, smb_share_t *);
97 static void smb_csc_option(const char *, smb_share_t *);
98 static char *smb_csc_name(const smb_share_t *);
99 static sa_group_t smb_get_defaultgrp(sa_handle_t);
100 static int interface_validator(int, char *);
101 static int smb_update_optionset_props(sa_handle_t, sa_resource_t, nvlist_t *);
103 static boolean_t smb_saprop_getbool(sa_optionset_t, char *);
104 static boolean_t smb_saprop_getstr(sa_optionset_t, char *, char *, size_t);
106 static struct {
107 char *value;
108 uint32_t flag;
109 } cscopt[] = {
110 { "disabled", SMB_SHRF_CSC_DISABLED },
111 { "manual", SMB_SHRF_CSC_MANUAL },
112 { "auto", SMB_SHRF_CSC_AUTO },
113 { "vdo", SMB_SHRF_CSC_VDO }
116 /* size of basic format allocation */
117 #define OPT_CHUNK 1024
119 /* size of string for types - big enough to hold "dependency" */
120 #define SCFTYPE_LEN 32
123 * Indexes of entries in smb_proto_options table.
124 * Changes to smb_proto_options table may require
125 * an update to these values.
127 #define PROTO_OPT_WINS1 6
128 #define PROTO_OPT_WINS_EXCLUDE 8
130 typedef struct smb_hostifs_walker {
131 const char *hiw_ifname;
132 boolean_t hiw_matchfound;
133 } smb_hostifs_walker_t;
137 * ops vector that provides the protocol specific info and operations
138 * for share management.
141 struct sa_plugin_ops sa_plugin_ops = {
142 SA_PLUGIN_VERSION,
143 SMB_PROTOCOL_NAME,
144 smb_share_init,
145 smb_share_fini,
146 smb_enable_share,
147 smb_disable_share,
148 smb_validate_property,
149 NULL, /* valid_space */
150 NULL, /* security_prop */
151 smb_parse_optstring,
152 smb_format_options,
153 smb_set_proto_prop,
154 smb_get_proto_set,
155 smb_get_status,
156 NULL, /* space_alias */
157 NULL, /* update_legacy */
158 NULL, /* delete_legacy */
159 smb_share_changed,
160 smb_enable_resource,
161 smb_disable_resource,
162 smb_share_features,
163 smb_list_transient,
164 smb_resource_changed,
165 smb_rename_resource,
166 NULL, /* run_command */
167 NULL, /* command_help */
168 NULL /* delete_proto_section */
171 struct option_defs optdefs[] = {
172 { SHOPT_AD_CONTAINER, OPT_TYPE_STRING },
173 { SHOPT_ABE, OPT_TYPE_BOOLEAN },
174 { SHOPT_NAME, OPT_TYPE_NAME },
175 { SHOPT_RO, OPT_TYPE_ACCLIST },
176 { SHOPT_RW, OPT_TYPE_ACCLIST },
177 { SHOPT_NONE, OPT_TYPE_ACCLIST },
178 { SHOPT_CATIA, OPT_TYPE_BOOLEAN },
179 { SHOPT_CSC, OPT_TYPE_CSC },
180 { SHOPT_GUEST, OPT_TYPE_BOOLEAN },
181 { SHOPT_DFSROOT, OPT_TYPE_BOOLEAN },
182 { SHOPT_DESCRIPTION, OPT_TYPE_STRING },
183 { NULL, NULL }
187 * findopt(name)
189 * Lookup option "name" in the option table and return the table
190 * index.
192 static int
193 findopt(char *name)
195 int i;
196 if (name != NULL) {
197 for (i = 0; optdefs[i].tag != NULL; i++) {
198 if (strcmp(optdefs[i].tag, name) == 0)
199 return (i);
202 return (-1);
206 * is_a_number(number)
208 * is the string a number in one of the forms we want to use?
210 static boolean_t
211 is_a_number(char *number)
213 boolean_t isnum = B_TRUE;
214 boolean_t ishex = B_FALSE;
216 if (number == NULL || *number == '\0')
217 return (B_FALSE);
219 if (strncasecmp(number, "0x", 2) == 0) {
220 number += 2;
221 ishex = B_TRUE;
222 } else if (*number == '-') {
223 number++;
226 while (isnum && (*number != '\0')) {
227 isnum = (ishex) ? isxdigit(*number) : isdigit(*number);
228 number++;
231 return (isnum);
235 * check ro vs rw values. Over time this may get beefed up.
236 * for now it just does simple checks.
239 static int
240 check_rorw(char *v1, char *v2)
242 int ret = SA_OK;
243 if (strcmp(v1, v2) == 0)
244 ret = SA_VALUE_CONFLICT;
245 return (ret);
249 * validresource(name)
251 * Check that name only has valid characters in it. The current valid
252 * set are the printable characters but not including:
253 * " / \ [ ] : | < > + ; , ? * = \t
254 * Note that space is included and there is a maximum length.
256 static boolean_t
257 validresource(const char *name)
259 const char *cp;
260 size_t len;
262 if (name == NULL)
263 return (B_FALSE);
265 len = strlen(name);
266 if (len == 0 || len > SA_MAX_RESOURCE_NAME)
267 return (B_FALSE);
269 if (strpbrk(name, "\"/\\[]:|<>+;,?*=\t") != NULL) {
270 return (B_FALSE);
273 for (cp = name; *cp != '\0'; cp++)
274 if (iscntrl(*cp))
275 return (B_FALSE);
277 return (B_TRUE);
281 * Check that the client-side caching (CSC) option value is valid.
283 static boolean_t
284 validcsc(const char *value)
286 int i;
288 for (i = 0; i < (sizeof (cscopt) / sizeof (cscopt[0])); ++i) {
289 if (strcasecmp(value, cscopt[i].value) == 0)
290 return (B_TRUE);
293 return (B_FALSE);
297 * smb_isonline()
299 * Determine if the SMF service instance is in the online state or
300 * not. A number of operations depend on this state.
302 static boolean_t
303 smb_isonline(void)
305 char *str;
306 boolean_t ret = B_FALSE;
308 if ((str = smf_get_state(SMBD_DEFAULT_INSTANCE_FMRI)) != NULL) {
309 ret = (strcmp(str, SCF_STATE_STRING_ONLINE) == 0);
310 free(str);
312 return (ret);
316 * smb_isdisabled()
318 * Determine if the SMF service instance is in the disabled state or
319 * not. A number of operations depend on this state.
321 static boolean_t
322 smb_isdisabled(void)
324 char *str;
325 boolean_t ret = B_FALSE;
327 if ((str = smf_get_state(SMBD_DEFAULT_INSTANCE_FMRI)) != NULL) {
328 ret = (strcmp(str, SCF_STATE_STRING_DISABLED) == 0);
329 free(str);
331 return (ret);
335 * smb_isautoenable()
337 * Determine if the SMF service instance auto_enabled set or not. A
338 * number of operations depend on this state. The property not being
339 * set or being set to true means autoenable. Only being set to false
340 * is not autoenabled.
342 static boolean_t
343 smb_isautoenable(void)
345 boolean_t ret = B_TRUE;
346 scf_simple_prop_t *prop;
347 uint8_t *retstr;
349 prop = scf_simple_prop_get(NULL, SMBD_DEFAULT_INSTANCE_FMRI,
350 "application", "auto_enable");
351 if (prop != NULL) {
352 retstr = scf_simple_prop_next_boolean(prop);
353 ret = *retstr != 0;
354 scf_simple_prop_free(prop);
356 return (ret);
360 * smb_ismaint()
362 * Determine if the SMF service instance is in the disabled state or
363 * not. A number of operations depend on this state.
365 static boolean_t
366 smb_ismaint(void)
368 char *str;
369 boolean_t ret = B_FALSE;
371 if ((str = smf_get_state(SMBD_DEFAULT_INSTANCE_FMRI)) != NULL) {
372 ret = (strcmp(str, SCF_STATE_STRING_MAINT) == 0);
373 free(str);
375 return (ret);
379 * smb_enable_share tells the implementation that it is to enable the share.
380 * This entails converting the path and options into the appropriate ioctl
381 * calls. It is assumed that all error checking of paths, etc. were
382 * done earlier.
384 static int
385 smb_enable_share(sa_share_t share)
387 char *path;
388 smb_share_t si;
389 sa_resource_t resource;
390 boolean_t iszfs;
391 boolean_t privileged;
392 int err = SA_OK;
393 priv_set_t *priv_effective;
394 boolean_t online;
396 priv_effective = priv_allocset();
397 (void) getppriv(PRIV_EFFECTIVE, priv_effective);
398 privileged = (priv_isfullset(priv_effective) == B_TRUE);
399 priv_freeset(priv_effective);
401 /* get the path since it is important in several places */
402 path = sa_get_share_attr(share, "path");
403 if (path == NULL)
404 return (SA_NO_SUCH_PATH);
407 * If administratively disabled, don't try to start anything.
409 online = smb_isonline();
410 if (!online && !smb_isautoenable() && smb_isdisabled())
411 goto done;
413 iszfs = sa_path_is_zfs(path);
415 if (iszfs) {
417 if (privileged == B_FALSE && !online) {
419 if (!online) {
420 (void) printf(dgettext(TEXT_DOMAIN,
421 "SMB: Cannot share remove "
422 "file system: %s\n"), path);
423 (void) printf(dgettext(TEXT_DOMAIN,
424 "SMB: Service needs to be enabled "
425 "by a privileged user\n"));
426 err = SA_NO_PERMISSION;
427 errno = EPERM;
429 if (err) {
430 sa_free_attr_string(path);
431 return (err);
437 if (privileged == B_TRUE && !online) {
438 err = smb_enable_service();
439 if (err != SA_OK) {
440 (void) printf(dgettext(TEXT_DOMAIN,
441 "SMB: Unable to enable service\n"));
442 } else {
443 online = B_TRUE;
448 * Don't bother trying to start shares if the service isn't
449 * running.
451 if (!online)
452 goto done;
454 /* Each share can have multiple resources */
455 for (resource = sa_get_share_resource(share, NULL);
456 resource != NULL;
457 resource = sa_get_next_resource(resource)) {
458 err = smb_build_shareinfo(share, resource, &si);
459 if (err != SA_OK) {
460 sa_free_attr_string(path);
461 return (err);
464 if (!iszfs) {
465 err = smb_share_create(&si);
466 } else {
467 share_t sh;
469 (void) sa_sharetab_fill_zfs(share, &sh, "smb");
470 err = sa_share_zfs(share, resource, (char *)path, &sh,
471 &si, ZFS_SHARE_SMB);
472 if (err != SA_OK) {
473 errno = err;
474 err = -1;
476 sa_emptyshare(&sh);
479 if (!iszfs)
480 (void) sa_update_sharetab(share, "smb");
481 done:
482 sa_free_attr_string(path);
484 return (err == NERR_DuplicateShare ? 0 : err);
488 * This is the share for CIFS all shares have resource names.
489 * Enable tells the smb server to update its hash. If it fails
490 * because smb server is down, we just ignore as smb server loads
491 * the resources from sharemanager at startup.
494 static int
495 smb_enable_resource(sa_resource_t resource)
497 sa_share_t share;
498 smb_share_t si;
499 int ret = SA_OK;
500 int err;
501 boolean_t isonline;
503 share = sa_get_resource_parent(resource);
504 if (share == NULL)
505 return (SA_NO_SUCH_PATH);
508 * If administratively disabled, don't try to start anything.
510 isonline = smb_isonline();
511 if (!isonline && !smb_isautoenable() && smb_isdisabled())
512 return (SA_OK);
514 if (!isonline) {
515 (void) smb_enable_service();
517 if (!smb_isonline())
518 return (SA_OK);
521 if ((ret = smb_build_shareinfo(share, resource, &si)) != SA_OK)
522 return (ret);
525 * Attempt to add the share. Any error that occurs if it was
526 * online is an error but don't count NERR_DuplicateName if
527 * smb/server had to be brought online since bringing the
528 * service up will enable the share that was just added prior
529 * to the attempt to enable.
531 err = smb_share_create(&si);
532 if (err == NERR_Success || !(!isonline && err == NERR_DuplicateName))
533 (void) sa_update_sharetab(share, "smb");
534 else
535 return (SA_NOT_SHARED);
537 return (SA_OK);
541 * Remove it from smb server hash.
543 static int
544 smb_disable_resource(sa_resource_t resource)
546 char *rname;
547 uint32_t res;
548 sa_share_t share;
550 rname = sa_get_resource_attr(resource, "name");
551 if (rname == NULL)
552 return (SA_NO_SUCH_RESOURCE);
554 if (smb_isonline()) {
555 res = smb_share_delete(rname);
556 if (res != NERR_Success &&
557 res != NERR_NetNameNotFound) {
558 sa_free_attr_string(rname);
559 return (SA_CONFIG_ERR);
563 sa_free_attr_string(rname);
565 share = sa_get_resource_parent(resource);
566 if (share != NULL) {
567 rname = sa_get_share_attr(share, "path");
568 if (rname != NULL) {
569 sa_handle_t handle;
571 handle = sa_find_group_handle((sa_group_t)resource);
572 (void) sa_delete_sharetab(handle, rname, "smb");
573 sa_free_attr_string(rname);
577 * Always return OK as smb/server may be down and
578 * Shares will be picked up when loaded.
580 return (SA_OK);
584 * smb_share_changed(sa_share_t share)
586 * The specified share has changed.
588 static int
589 smb_share_changed(sa_share_t share)
591 char *path;
592 sa_resource_t resource;
594 if (!smb_isonline())
595 return (SA_OK);
597 /* get the path since it is important in several places */
598 path = sa_get_share_attr(share, "path");
599 if (path == NULL)
600 return (SA_NO_SUCH_PATH);
602 for (resource = sa_get_share_resource(share, NULL);
603 resource != NULL;
604 resource = sa_get_next_resource(resource))
605 (void) smb_resource_changed(resource);
607 sa_free_attr_string(path);
609 return (SA_OK);
613 * smb_resource_changed(sa_resource_t resource)
615 * The specified resource has changed.
617 static int
618 smb_resource_changed(sa_resource_t resource)
620 uint32_t res;
621 sa_share_t share;
622 smb_share_t si;
624 if (!smb_isonline())
625 return (SA_OK);
627 if ((share = sa_get_resource_parent(resource)) == NULL)
628 return (SA_CONFIG_ERR);
630 if ((res = smb_build_shareinfo(share, resource, &si)) != SA_OK)
631 return (res);
633 res = smb_share_modify(&si);
635 if (res != NERR_Success)
636 return (SA_CONFIG_ERR);
638 return (smb_enable_service());
642 * smb_disable_share(sa_share_t share, char *path)
644 * Unshare the specified share. Note that "path" is the same
645 * path as what is in the "share" object. It is passed in to avoid an
646 * additional lookup. A missing "path" value makes this a no-op
647 * function.
649 static int
650 smb_disable_share(sa_share_t share, char *path)
652 char *rname;
653 sa_resource_t resource;
654 sa_group_t parent;
655 boolean_t iszfs;
656 int err = SA_OK;
657 int ret = SA_OK;
658 sa_handle_t handle;
659 boolean_t first = B_TRUE; /* work around sharetab issue */
661 if (path == NULL)
662 return (ret);
665 * If the share is in a ZFS group we need to handle it
666 * differently. Just being on a ZFS file system isn't
667 * enough since we may be in a legacy share case.
669 parent = sa_get_parent_group(share);
670 iszfs = sa_group_is_zfs(parent);
672 if (!smb_isonline())
673 goto done;
675 for (resource = sa_get_share_resource(share, NULL);
676 resource != NULL;
677 resource = sa_get_next_resource(resource)) {
678 rname = sa_get_resource_attr(resource, "name");
679 if (rname == NULL) {
680 continue;
682 if (!iszfs) {
683 err = smb_share_delete(rname);
684 switch (err) {
685 case NERR_NetNameNotFound:
686 case NERR_Success:
687 err = SA_OK;
688 break;
689 default:
690 err = SA_CONFIG_ERR;
691 break;
693 } else {
694 share_t sh;
696 (void) sa_sharetab_fill_zfs(share, &sh, "smb");
697 err = sa_share_zfs(share, resource, (char *)path, &sh,
698 rname, ZFS_UNSHARE_SMB);
699 if (err != SA_OK) {
700 switch (err) {
701 case EINVAL:
702 case ENOENT:
703 err = SA_OK;
704 break;
705 default:
707 * If we are no longer the first case,
708 * we don't care about the sa_share_zfs
709 * err if it is -1. This works around
710 * a problem in sharefs and should be
711 * removed when sharefs supports
712 * multiple entries per path.
714 if (!first)
715 err = SA_OK;
716 else
717 err = SA_SYSTEM_ERR;
718 break;
722 first = B_FALSE;
724 sa_emptyshare(&sh);
727 if (err != SA_OK)
728 ret = err;
729 sa_free_attr_string(rname);
731 done:
732 if (!iszfs) {
733 handle = sa_find_group_handle((sa_group_t)share);
734 if (handle != NULL)
735 (void) sa_delete_sharetab(handle, path, "smb");
736 else
737 ret = SA_SYSTEM_ERR;
739 return (ret);
743 * smb_validate_property(handle, property, parent)
745 * Check that the property has a legitimate value for its type.
746 * Handle isn't currently used but may need to be in the future.
749 /*ARGSUSED*/
750 static int
751 smb_validate_property(sa_handle_t handle, sa_property_t property,
752 sa_optionset_t parent)
754 int ret = SA_OK;
755 char *propname;
756 int optindex;
757 sa_group_t parent_group;
758 char *value;
759 char *other;
761 propname = sa_get_property_attr(property, "type");
763 if ((optindex = findopt(propname)) < 0)
764 ret = SA_NO_SUCH_PROP;
766 /* need to validate value range here as well */
767 if (ret == SA_OK) {
768 parent_group = sa_get_parent_group((sa_share_t)parent);
769 if (optdefs[optindex].share && !sa_is_share(parent_group))
770 ret = SA_PROP_SHARE_ONLY;
772 if (ret != SA_OK) {
773 if (propname != NULL)
774 sa_free_attr_string(propname);
775 return (ret);
778 value = sa_get_property_attr(property, "value");
779 if (value != NULL) {
780 /* first basic type checking */
781 switch (optdefs[optindex].type) {
782 case OPT_TYPE_NUMBER:
783 /* check that the value is all digits */
784 if (!is_a_number(value))
785 ret = SA_BAD_VALUE;
786 break;
787 case OPT_TYPE_BOOLEAN:
788 ret = true_false_validator(0, value);
789 break;
790 case OPT_TYPE_NAME:
792 * Make sure no invalid characters
794 if (!validresource(value))
795 ret = SA_BAD_VALUE;
796 break;
797 case OPT_TYPE_STRING:
798 /* whatever is here should be ok */
799 break;
800 case OPT_TYPE_CSC:
801 if (!validcsc(value))
802 ret = SA_BAD_VALUE;
803 break;
804 case OPT_TYPE_ACCLIST: {
805 sa_property_t oprop;
806 char *ovalue;
808 * access list handling. Should eventually
809 * validate that all the values make sense.
810 * Also, ro and rw may have cross value
811 * conflicts.
813 if (parent == NULL)
814 break;
815 if (strcmp(propname, SHOPT_RO) == 0)
816 other = SHOPT_RW;
817 else if (strcmp(propname, SHOPT_RW) == 0)
818 other = SHOPT_RO;
819 else
820 other = NULL;
821 if (other == NULL)
822 break;
824 /* compare rw(ro) with ro(rw) */
825 oprop = sa_get_property(parent, other);
826 if (oprop == NULL)
827 break;
829 * only potential
830 * confusion if other
831 * exists
833 ovalue = sa_get_property_attr(oprop, "value");
834 if (ovalue != NULL) {
835 ret = check_rorw(value, ovalue);
836 sa_free_attr_string(ovalue);
838 break;
840 default:
841 break;
845 if (value != NULL)
846 sa_free_attr_string(value);
847 if (ret == SA_OK && optdefs[optindex].check != NULL)
848 /* do the property specific check */
849 ret = optdefs[optindex].check(property);
851 if (propname != NULL)
852 sa_free_attr_string(propname);
853 return (ret);
857 * Protocol management functions
859 * properties defined in the default files are defined in
860 * proto_option_defs for parsing and validation.
863 struct smb_proto_option_defs {
864 int smb_index;
865 int32_t minval;
866 int32_t maxval; /* In case of length of string this should be max */
867 int (*validator)(int, char *);
868 int32_t refresh;
869 } smb_proto_options[] = {
870 { SMB_CI_SYS_CMNT, 0, MAX_VALUE_BUFLEN,
871 string_length_check_validator, SMB_REFRESH_REFRESH },
872 { SMB_CI_MAX_WORKERS, SMB_PI_MAX_WORKERS_MIN, SMB_PI_MAX_WORKERS_MAX,
873 range_check_validator, SMB_REFRESH_REFRESH },
874 { SMB_CI_NETBIOS_ENABLE, 0, 0, true_false_validator,
875 SMB_REFRESH_REFRESH },
876 { SMB_CI_NBSCOPE, 0, MAX_VALUE_BUFLEN,
877 string_length_check_validator, 0 },
878 { SMB_CI_LM_LEVEL, 2, 5, range_check_validator, 0 },
879 { SMB_CI_KEEPALIVE, 20, 5400, range_check_validator_zero_ok,
880 SMB_REFRESH_REFRESH },
881 { SMB_CI_WINS_SRV1, 0, MAX_VALUE_BUFLEN,
882 ipv4_validator, SMB_REFRESH_REFRESH },
883 { SMB_CI_WINS_SRV2, 0, MAX_VALUE_BUFLEN,
884 ipv4_validator, SMB_REFRESH_REFRESH },
885 { SMB_CI_WINS_EXCL, 0, MAX_VALUE_BUFLEN,
886 interface_validator, SMB_REFRESH_REFRESH },
887 { SMB_CI_SIGNING_ENABLE, 0, 0, true_false_validator,
888 SMB_REFRESH_REFRESH },
889 { SMB_CI_SIGNING_REQD, 0, 0, true_false_validator,
890 SMB_REFRESH_REFRESH },
891 { SMB_CI_RESTRICT_ANON, 0, 0, true_false_validator,
892 SMB_REFRESH_REFRESH },
893 { SMB_CI_DOMAIN_SRV, 0, MAX_VALUE_BUFLEN,
894 hostname_validator, SMB_REFRESH_REFRESH },
895 { SMB_CI_ADS_SITE, 0, MAX_VALUE_BUFLEN,
896 string_length_check_validator, SMB_REFRESH_REFRESH },
897 { SMB_CI_DYNDNS_ENABLE, 0, 0, true_false_validator, 0 },
898 { SMB_CI_AUTOHOME_MAP, 0, MAX_VALUE_BUFLEN, path_validator, 0 },
899 { SMB_CI_IPV6_ENABLE, 0, 0, true_false_validator,
900 SMB_REFRESH_REFRESH },
901 { SMB_CI_PRINT_ENABLE, 0, 0, print_enable_validator,
902 SMB_REFRESH_REFRESH },
903 { SMB_CI_TRAVERSE_MOUNTS, 0, 0, true_false_validator,
904 SMB_REFRESH_REFRESH },
905 { SMB_CI_MAP, 0, MAX_VALUE_BUFLEN, cmd_validator, SMB_REFRESH_REFRESH },
906 { SMB_CI_UNMAP, 0, MAX_VALUE_BUFLEN, cmd_validator,
907 SMB_REFRESH_REFRESH },
908 { SMB_CI_DISPOSITION, 0, MAX_VALUE_BUFLEN,
909 disposition_validator, SMB_REFRESH_REFRESH },
910 { SMB_CI_MAX_PROTOCOL, 0, MAX_VALUE_BUFLEN, max_protocol_validator,
911 SMB_REFRESH_REFRESH },
914 #define SMB_OPT_NUM \
915 (sizeof (smb_proto_options) / sizeof (smb_proto_options[0]))
918 * Check the range of value as int range.
920 static int
921 range_check_validator(int index, char *value)
923 int ret = SA_OK;
925 if (!is_a_number(value)) {
926 ret = SA_BAD_VALUE;
927 } else {
928 int val;
929 val = strtoul(value, NULL, 0);
930 if (val < smb_proto_options[index].minval ||
931 val > smb_proto_options[index].maxval)
932 ret = SA_BAD_VALUE;
934 return (ret);
938 * Check the range of value as int range.
940 static int
941 range_check_validator_zero_ok(int index, char *value)
943 int ret = SA_OK;
945 if (!is_a_number(value)) {
946 ret = SA_BAD_VALUE;
947 } else {
948 int val;
949 val = strtoul(value, NULL, 0);
950 if (val == 0)
951 ret = SA_OK;
952 else {
953 if (val < smb_proto_options[index].minval ||
954 val > smb_proto_options[index].maxval)
955 ret = SA_BAD_VALUE;
958 return (ret);
962 * Check the length of the string
964 static int
965 string_length_check_validator(int index, char *value)
967 int ret = SA_OK;
969 if (value == NULL)
970 return (SA_BAD_VALUE);
971 if (strlen(value) > smb_proto_options[index].maxval)
972 ret = SA_BAD_VALUE;
973 return (ret);
977 * Check yes/no
979 /*ARGSUSED*/
980 static int
981 true_false_validator(int index, char *value)
983 if (value == NULL)
984 return (SA_BAD_VALUE);
985 if ((strcasecmp(value, "true") == 0) ||
986 (strcasecmp(value, "false") == 0))
987 return (SA_OK);
988 return (SA_BAD_VALUE);
992 * If printing support is compiled in, this is the same as:
993 * true_false_validator. Otherwise, only allow false.
995 /*ARGSUSED*/
996 static int
997 print_enable_validator(int index, char *value)
999 if (value == NULL)
1000 return (SA_BAD_VALUE);
1002 #ifdef CONFIG_SMB_PRINTING
1003 if (strcasecmp(value, "true") == 0)
1004 return (SA_OK);
1005 #endif
1006 if (strcasecmp(value, "false") == 0)
1007 return (SA_OK);
1009 return (SA_BAD_VALUE);
1013 * Check IP v4 address.
1015 /*ARGSUSED*/
1016 static int
1017 ipv4_validator(int index, char *value)
1019 char sbytes[16];
1021 if (value == NULL)
1022 return (SA_OK);
1024 if (strlen(value) == 0)
1025 return (SA_OK);
1027 if (inet_pton(AF_INET, value, (void *)sbytes) != 1)
1028 return (SA_BAD_VALUE);
1030 return (SA_OK);
1034 * Check that the specified name is an IP address (v4 or v6) or a hostname.
1035 * Per RFC 1035 and 1123, names may contain alphanumeric characters, hyphens
1036 * and dots. The first and last character of a label must be alphanumeric.
1037 * Interior characters may be alphanumeric or hypens.
1039 * Domain names should not contain underscores but we allow them because
1040 * Windows names are often in non-compliance with this rule.
1042 /*ARGSUSED*/
1043 static int
1044 hostname_validator(int index, char *value)
1046 char sbytes[INET6_ADDRSTRLEN];
1047 boolean_t new_label = B_TRUE;
1048 char *p;
1049 char label_terminator;
1050 int len;
1052 if (value == NULL)
1053 return (SA_OK);
1055 if ((len = strlen(value)) == 0)
1056 return (SA_OK);
1058 if (inet_pton(AF_INET, value, (void *)sbytes) == 1)
1059 return (SA_OK);
1061 if (inet_pton(AF_INET6, value, (void *)sbytes) == 1)
1062 return (SA_OK);
1064 if (len >= MAXHOSTNAMELEN)
1065 return (SA_BAD_VALUE);
1067 if (strspn(value, "0123456789.") == len)
1068 return (SA_BAD_VALUE);
1070 label_terminator = *value;
1072 for (p = value; *p != '\0'; ++p) {
1073 if (new_label) {
1074 if (!isalnum(*p))
1075 return (SA_BAD_VALUE);
1076 new_label = B_FALSE;
1077 label_terminator = *p;
1078 continue;
1081 if (*p == '.') {
1082 if (!isalnum(label_terminator))
1083 return (SA_BAD_VALUE);
1084 new_label = B_TRUE;
1085 label_terminator = *p;
1086 continue;
1089 label_terminator = *p;
1091 if (isalnum(*p) || *p == '-' || *p == '_')
1092 continue;
1094 return (SA_BAD_VALUE);
1097 if (!isalnum(label_terminator))
1098 return (SA_BAD_VALUE);
1100 return (SA_OK);
1104 * Call back function for dlpi_walk.
1105 * Returns TRUE if interface name exists on the host.
1107 static boolean_t
1108 smb_get_interface(const char *ifname, void *arg)
1110 smb_hostifs_walker_t *iterp = arg;
1112 iterp->hiw_matchfound = (strcmp(ifname, iterp->hiw_ifname) == 0);
1114 return (iterp->hiw_matchfound);
1118 * Checks to see if the input interface exists on the host.
1119 * Returns B_TRUE if the match is found, B_FALSE otherwise.
1121 static boolean_t
1122 smb_validate_interface(const char *ifname)
1124 smb_hostifs_walker_t iter;
1126 if ((ifname == NULL) || (*ifname == '\0'))
1127 return (B_FALSE);
1129 iter.hiw_ifname = ifname;
1130 iter.hiw_matchfound = B_FALSE;
1131 dlpi_walk(smb_get_interface, &iter, 0);
1133 return (iter.hiw_matchfound);
1137 * Check valid interfaces. Interface names value can be NULL or empty.
1138 * Returns SA_BAD_VALUE if interface cannot be found on the host.
1140 /*ARGSUSED*/
1141 static int
1142 interface_validator(int index, char *value)
1144 char buf[16];
1145 int ret = SA_OK;
1146 char *ifname, *tmp, *p;
1148 if (value == NULL || *value == '\0')
1149 return (ret);
1151 if (strlen(value) > MAX_VALUE_BUFLEN)
1152 return (SA_BAD_VALUE);
1154 if ((p = strdup(value)) == NULL)
1155 return (SA_NO_MEMORY);
1157 tmp = p;
1158 while ((ifname = strsep(&tmp, ",")) != NULL) {
1159 if (*ifname == '\0') {
1160 ret = SA_BAD_VALUE;
1161 break;
1164 if (!smb_validate_interface(ifname)) {
1165 if (inet_pton(AF_INET, ifname, (void *)buf) == 0) {
1166 ret = SA_BAD_VALUE;
1167 break;
1172 free(p);
1173 return (ret);
1177 * Check path
1179 /*ARGSUSED*/
1180 static int
1181 path_validator(int index, char *path)
1183 struct stat buffer;
1184 int fd, status;
1186 if (path == NULL)
1187 return (SA_BAD_VALUE);
1189 fd = open(path, O_RDONLY);
1190 if (fd < 0)
1191 return (SA_BAD_VALUE);
1193 status = fstat(fd, &buffer);
1194 (void) close(fd);
1196 if (status < 0)
1197 return (SA_BAD_VALUE);
1199 if (buffer.st_mode & S_IFDIR)
1200 return (SA_OK);
1201 return (SA_BAD_VALUE);
1205 * the protoset holds the defined options so we don't have to read
1206 * them multiple times
1208 static sa_protocol_properties_t protoset;
1210 static int
1211 findprotoopt(char *name)
1213 int i;
1214 char *sc_name;
1216 for (i = 0; i < SMB_OPT_NUM; i++) {
1217 sc_name = smb_config_getname(smb_proto_options[i].smb_index);
1218 if (strcasecmp(sc_name, name) == 0)
1219 return (i);
1222 return (-1);
1226 * smb_load_proto_properties()
1228 * read the smb config values from SMF.
1231 static int
1232 smb_load_proto_properties()
1234 sa_property_t prop;
1235 char value[MAX_VALUE_BUFLEN];
1236 char *name;
1237 int index;
1238 int ret = SA_OK;
1239 int rc;
1241 protoset = sa_create_protocol_properties(SMB_PROTOCOL_NAME);
1242 if (protoset == NULL)
1243 return (SA_NO_MEMORY);
1245 for (index = 0; index < SMB_OPT_NUM && ret == SA_OK; index++) {
1246 rc = smb_config_get(smb_proto_options[index].smb_index,
1247 value, sizeof (value));
1248 if (rc != SMBD_SMF_OK)
1249 continue;
1250 name = smb_config_getname(smb_proto_options[index].smb_index);
1251 prop = sa_create_property(name, value);
1252 if (prop != NULL)
1253 ret = sa_add_protocol_property(protoset, prop);
1254 else
1255 ret = SA_NO_MEMORY;
1257 return (ret);
1261 * smb_share_init()
1263 * Initialize the smb plugin.
1266 static int
1267 smb_share_init(void)
1269 if (sa_plugin_ops.sa_init != smb_share_init)
1270 return (SA_SYSTEM_ERR);
1272 smb_share_door_clnt_init();
1273 return (smb_load_proto_properties());
1277 * smb_share_fini()
1280 static void
1281 smb_share_fini(void)
1283 xmlFreeNode(protoset);
1284 protoset = NULL;
1286 smb_share_door_clnt_fini();
1290 * smb_get_proto_set()
1292 * Return an optionset with all the protocol specific properties in
1293 * it.
1295 static sa_protocol_properties_t
1296 smb_get_proto_set(void)
1298 return (protoset);
1302 * smb_enable_dependencies()
1304 * SMBD_DEFAULT_INSTANCE_FMRI may have some dependencies that aren't
1305 * enabled. This will attempt to enable all of them.
1307 static void
1308 smb_enable_dependencies(const char *fmri)
1310 scf_handle_t *handle;
1311 scf_service_t *service;
1312 scf_instance_t *inst = NULL;
1313 scf_iter_t *iter;
1314 scf_property_t *prop;
1315 scf_value_t *value;
1316 scf_propertygroup_t *pg;
1317 scf_scope_t *scope;
1318 char type[SCFTYPE_LEN];
1319 char *dependency;
1320 char *servname;
1321 int maxlen;
1324 * Get all required handles and storage.
1326 handle = scf_handle_create(SCF_VERSION);
1327 if (handle == NULL)
1328 return;
1330 if (scf_handle_bind(handle) != 0) {
1331 scf_handle_destroy(handle);
1332 return;
1335 maxlen = scf_limit(SCF_LIMIT_MAX_VALUE_LENGTH);
1336 if (maxlen == (ssize_t)-1)
1337 maxlen = MAXPATHLEN;
1339 dependency = malloc(maxlen);
1341 service = scf_service_create(handle);
1343 iter = scf_iter_create(handle);
1345 pg = scf_pg_create(handle);
1347 prop = scf_property_create(handle);
1349 value = scf_value_create(handle);
1351 scope = scf_scope_create(handle);
1353 if (service == NULL || iter == NULL || pg == NULL || prop == NULL ||
1354 value == NULL || scope == NULL || dependency == NULL)
1355 goto done;
1358 * We passed in the FMRI for the default instance but for
1359 * some things we need the simple form so construct it. Since
1360 * we reuse the storage that dependency points to, we need to
1361 * use the servname early.
1363 (void) snprintf(dependency, maxlen, "%s", fmri + sizeof ("svc:"));
1364 servname = strrchr(dependency, ':');
1365 if (servname == NULL)
1366 goto done;
1367 *servname = '\0';
1368 servname = dependency;
1371 * Setup to iterate over the service property groups, only
1372 * looking at those that are "dependency" types. The "entity"
1373 * property will have the FMRI of the service we are dependent
1374 * on.
1376 if (scf_handle_get_scope(handle, SCF_SCOPE_LOCAL, scope) != 0)
1377 goto done;
1379 if (scf_scope_get_service(scope, servname, service) != 0)
1380 goto done;
1382 if (scf_iter_service_pgs(iter, service) != 0)
1383 goto done;
1385 while (scf_iter_next_pg(iter, pg) > 0) {
1386 char *services[2];
1388 * Have a property group for the service. See if it is
1389 * a dependency pg and only do operations on those.
1391 if (scf_pg_get_type(pg, type, SCFTYPE_LEN) <= 0)
1392 continue;
1394 if (strncmp(type, SCF_GROUP_DEPENDENCY, SCFTYPE_LEN) != 0)
1395 continue;
1397 * Have a dependency. Attempt to enable it.
1399 if (scf_pg_get_property(pg, SCF_PROPERTY_ENTITIES, prop) != 0)
1400 continue;
1402 if (scf_property_get_value(prop, value) != 0)
1403 continue;
1405 services[1] = NULL;
1407 if (scf_value_get_as_string(value, dependency, maxlen) > 0) {
1408 services[0] = dependency;
1409 _check_services(services);
1413 done:
1414 free(dependency);
1415 if (value != NULL)
1416 scf_value_destroy(value);
1417 if (prop != NULL)
1418 scf_property_destroy(prop);
1419 if (pg != NULL)
1420 scf_pg_destroy(pg);
1421 if (iter != NULL)
1422 scf_iter_destroy(iter);
1423 if (scope != NULL)
1424 scf_scope_destroy(scope);
1425 if (inst != NULL)
1426 scf_instance_destroy(inst);
1427 if (service != NULL)
1428 scf_service_destroy(service);
1430 (void) scf_handle_unbind(handle);
1431 scf_handle_destroy(handle);
1435 * How long to wait for service to come online
1437 #define WAIT_FOR_SERVICE 15
1440 * smb_enable_service()
1443 static int
1444 smb_enable_service(void)
1446 int i;
1447 int ret = SA_OK;
1448 char *service[] = { SMBD_DEFAULT_INSTANCE_FMRI, NULL };
1450 if (!smb_isonline()) {
1452 * Attempt to start the idmap, and other dependent
1453 * services, first. If it fails, the SMB service will
1454 * ultimately fail so we use that as the error. If we
1455 * don't try to enable idmap, smb won't start the
1456 * first time unless the admin has done it
1457 * manually. The service could be administratively
1458 * disabled so we won't always get started.
1460 smb_enable_dependencies(SMBD_DEFAULT_INSTANCE_FMRI);
1461 _check_services(service);
1463 /* Wait for service to come online */
1464 for (i = 0; i < WAIT_FOR_SERVICE; i++) {
1465 if (smb_isonline()) {
1466 ret = SA_OK;
1467 break;
1468 } else if (smb_ismaint()) {
1469 /* maintenance requires help */
1470 ret = SA_SYSTEM_ERR;
1471 break;
1472 } else {
1473 /* try another time */
1474 ret = SA_BUSY;
1475 (void) sleep(1);
1479 return (ret);
1483 * smb_validate_proto_prop(index, name, value)
1485 * Verify that the property specified by name can take the new
1486 * value. This is a sanity check to prevent bad values getting into
1487 * the default files.
1489 static int
1490 smb_validate_proto_prop(int index, char *name, char *value)
1492 if ((name == NULL) || (index < 0))
1493 return (SA_BAD_VALUE);
1495 if (smb_proto_options[index].validator == NULL)
1496 return (SA_OK);
1498 if (smb_proto_options[index].validator(index, value) == SA_OK)
1499 return (SA_OK);
1500 return (SA_BAD_VALUE);
1504 * smb_set_proto_prop(prop)
1506 * check that prop is valid.
1508 /*ARGSUSED*/
1509 static int
1510 smb_set_proto_prop(sa_property_t prop)
1512 int ret = SA_OK;
1513 char *name;
1514 char *value;
1515 int index = -1;
1516 struct smb_proto_option_defs *opt;
1518 name = sa_get_property_attr(prop, "type");
1519 value = sa_get_property_attr(prop, "value");
1520 if (name != NULL && value != NULL) {
1521 index = findprotoopt(name);
1522 if (index >= 0) {
1523 /* should test for valid value */
1524 ret = smb_validate_proto_prop(index, name, value);
1525 if (ret == SA_OK) {
1526 opt = &smb_proto_options[index];
1528 /* Save to SMF */
1529 (void) smb_config_set(opt->smb_index, value);
1531 * Specialized refresh mechanisms can
1532 * be flagged in the proto_options and
1533 * processed here.
1535 if (opt->refresh & SMB_REFRESH_REFRESH)
1536 (void) smf_refresh_instance(
1537 SMBD_DEFAULT_INSTANCE_FMRI);
1538 else if (opt->refresh & SMB_REFRESH_RESTART)
1539 (void) smf_restart_instance(
1540 SMBD_DEFAULT_INSTANCE_FMRI);
1545 if (name != NULL)
1546 sa_free_attr_string(name);
1547 if (value != NULL)
1548 sa_free_attr_string(value);
1550 return (ret);
1554 * smb_get_status()
1556 * What is the current status of the smbd? We use the SMF state here.
1557 * Caller must free the returned value.
1560 static char *
1561 smb_get_status(void)
1563 return (smf_get_state(SMBD_DEFAULT_INSTANCE_FMRI));
1567 * This protocol plugin require resource names
1569 static uint64_t
1570 smb_share_features(void)
1572 return (SA_FEATURE_RESOURCE | SA_FEATURE_ALLOWSUBDIRS |
1573 SA_FEATURE_ALLOWPARDIRS | SA_FEATURE_SERVER);
1577 * This should be used to convert smb_share_t to sa_resource_t
1578 * Should only be needed to build transient shares/resources to be
1579 * supplied to sharemgr to display.
1581 static int
1582 smb_add_transient(sa_handle_t handle, smb_share_t *si)
1584 int err;
1585 sa_share_t share;
1586 sa_group_t group;
1587 sa_resource_t resource;
1588 nvlist_t *nvl;
1589 char *opt;
1591 if (si == NULL)
1592 return (SA_INVALID_NAME);
1594 if ((share = sa_find_share(handle, si->shr_path)) == NULL) {
1595 if ((group = smb_get_defaultgrp(handle)) == NULL)
1596 return (SA_NO_SUCH_GROUP);
1598 share = sa_get_share(group, si->shr_path);
1599 if (share == NULL) {
1600 share = sa_add_share(group, si->shr_path,
1601 SA_SHARE_TRANSIENT, &err);
1602 if (share == NULL)
1603 return (SA_NO_SUCH_PATH);
1608 * Now handle the resource. Make sure that the resource is
1609 * transient and added to the share.
1611 resource = sa_get_share_resource(share, si->shr_name);
1612 if (resource == NULL) {
1613 resource = sa_add_resource(share,
1614 si->shr_name, SA_SHARE_TRANSIENT, &err);
1615 if (resource == NULL)
1616 return (SA_NO_SUCH_RESOURCE);
1619 if (si->shr_cmnt[0] != '\0')
1620 (void) sa_set_resource_description(resource, si->shr_cmnt);
1622 if (si->shr_container[0] != '\0')
1623 (void) sa_set_resource_attr(resource, SHOPT_AD_CONTAINER,
1624 si->shr_container);
1626 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
1627 return (SA_NO_MEMORY);
1629 if ((opt = smb_csc_name(si)) != NULL)
1630 err |= nvlist_add_string(nvl, SHOPT_CSC, opt);
1632 opt = (si->shr_flags & SMB_SHRF_ABE) ? "true" : "false";
1633 err |= nvlist_add_string(nvl, SHOPT_ABE, opt);
1635 if ((si->shr_flags & SMB_SHRF_AUTOHOME) == 0) {
1636 opt = (si->shr_flags & SMB_SHRF_GUEST_OK) ? "true" : "false";
1637 err |= nvlist_add_string(nvl, SHOPT_GUEST, opt);
1640 if (si->shr_access_ro[0] != '\0')
1641 err |= nvlist_add_string(nvl, SHOPT_RO, si->shr_access_ro);
1643 if (si->shr_access_rw[0] != '\0')
1644 err |= nvlist_add_string(nvl, SHOPT_RW, si->shr_access_rw);
1646 if (si->shr_access_none[0] != '\0')
1647 err |= nvlist_add_string(nvl, SHOPT_NONE, si->shr_access_none);
1649 if (err) {
1650 nvlist_free(nvl);
1651 return (SA_CONFIG_ERR);
1654 err = smb_update_optionset_props(handle, resource, nvl);
1656 nvlist_free(nvl);
1657 return (err);
1661 * Return smb transient shares.
1663 static int
1664 smb_list_transient(sa_handle_t handle)
1666 int i, offset;
1667 smb_shrlist_t list;
1668 int res;
1670 if (smb_share_count() <= 0)
1671 return (SA_OK);
1673 offset = 0;
1674 while (smb_share_list(offset, &list) == NERR_Success) {
1675 if (list.sl_cnt == 0)
1676 break;
1678 for (i = 0; i < list.sl_cnt; i++) {
1679 res = smb_add_transient(handle, &(list.sl_shares[i]));
1680 if (res != SA_OK)
1681 return (res);
1683 offset += list.sl_cnt;
1686 return (SA_OK);
1690 * fix_resource_name(share, name, prefix)
1692 * Construct a name where the ZFS dataset has the prefix replaced with "name".
1694 static char *
1695 fix_resource_name(sa_share_t share, char *name, char *prefix)
1697 char buf[SA_MAX_RESOURCE_NAME + 1];
1698 char *dataset;
1699 size_t bufsz = SA_MAX_RESOURCE_NAME + 1;
1700 size_t prelen;
1702 if (prefix == NULL)
1703 return (strdup(name));
1705 dataset = sa_get_share_attr(share, "dataset");
1706 if (dataset == NULL)
1707 return (strdup(name));
1709 (void) strlcpy(buf, name, bufsz);
1710 prelen = strlen(prefix);
1712 if (strncmp(dataset, prefix, prelen) == 0)
1713 (void) strlcat(buf, dataset + prelen, bufsz);
1715 sa_free_attr_string(dataset);
1716 sa_fix_resource_name(buf);
1717 return (strdup(buf));
1721 * smb_parse_optstring(group, options)
1723 * parse a compact option string into individual options. This allows
1724 * ZFS sharesmb and sharemgr "share" command to work. group can be a
1725 * group, a share or a resource.
1727 static int
1728 smb_parse_optstring(sa_group_t group, char *options)
1730 char *dup;
1731 char *base;
1732 char *lasts;
1733 char *token;
1734 sa_optionset_t optionset;
1735 sa_group_t parent = NULL;
1736 sa_resource_t resource = NULL;
1737 int iszfs = 0;
1738 int persist = 0;
1739 int need_optionset = 0;
1740 int ret = SA_OK;
1741 sa_property_t prop;
1744 * In order to not attempt to change ZFS properties unless
1745 * absolutely necessary, we never do it in the legacy parsing
1746 * so we need to keep track of this.
1748 if (sa_is_share(group)) {
1749 char *zfs;
1751 parent = sa_get_parent_group(group);
1752 if (parent != NULL) {
1753 zfs = sa_get_group_attr(parent, "zfs");
1754 if (zfs != NULL) {
1755 sa_free_attr_string(zfs);
1756 iszfs = 1;
1759 } else {
1760 iszfs = sa_group_is_zfs(group);
1762 * If a ZFS group, then we need to see if a resource
1763 * name is being set. If so, bail with
1764 * SA_PROP_SHARE_ONLY, so we come back in with a share
1765 * instead of a group.
1767 if (iszfs ||
1768 strncmp(options, "name=", sizeof ("name=") - 1) == 0 ||
1769 strstr(options, ",name=") != NULL) {
1770 return (SA_PROP_SHARE_ONLY);
1774 /* do we have an existing optionset? */
1775 optionset = sa_get_optionset(group, "smb");
1776 if (optionset == NULL) {
1777 /* didn't find existing optionset so create one */
1778 optionset = sa_create_optionset(group, "smb");
1779 if (optionset == NULL)
1780 return (SA_NO_MEMORY);
1781 } else {
1783 * If an optionset already exists, we've come through
1784 * twice so ignore the second time.
1786 return (ret);
1789 /* We need a copy of options for the next part. */
1790 dup = strdup(options);
1791 if (dup == NULL)
1792 return (SA_NO_MEMORY);
1795 * SMB properties are straightforward and are strings,
1796 * integers or booleans. Properties are separated by
1797 * commas. It will be necessary to parse quotes due to some
1798 * strings not having a restricted characters set.
1800 * Note that names will create a resource. For now, if there
1801 * is a set of properties "before" the first name="", those
1802 * properties will be placed on the group.
1804 persist = sa_is_persistent(group);
1805 base = dup;
1806 token = dup;
1807 lasts = NULL;
1808 while (token != NULL && ret == SA_OK) {
1809 ret = SA_OK;
1810 token = strtok_r(base, ",", &lasts);
1811 base = NULL;
1812 if (token != NULL) {
1813 char *value;
1815 * All SMB properties have values so there
1816 * MUST be an '=' character. If it doesn't,
1817 * it is a syntax error.
1819 value = strchr(token, '=');
1820 if (value != NULL) {
1821 *value++ = '\0';
1822 } else {
1823 ret = SA_SYNTAX_ERR;
1824 break;
1827 * We may need to handle a "name" property
1828 * that is a ZFS imposed resource name. Each
1829 * name would trigger getting a new "resource"
1830 * to put properties on. For now, assume no
1831 * "name" property for special handling.
1834 if (strcmp(token, SHOPT_NAME) == 0) {
1835 char *prefix;
1836 char *name = NULL;
1838 * We have a name, so now work on the
1839 * resource level. We have a "share"
1840 * in "group" due to the caller having
1841 * added it. If we are called with a
1842 * group, the check for group/share
1843 * at the beginning of this function
1844 * will bail out the parse if there is a
1845 * "name" but no share.
1847 if (!iszfs) {
1848 ret = SA_SYNTAX_ERR;
1849 break;
1852 * Make sure the parent group has the
1853 * "prefix" property since we will
1854 * need to use this for constructing
1855 * inherited name= values.
1857 prefix = sa_get_group_attr(parent, "prefix");
1858 if (prefix == NULL) {
1859 prefix = sa_get_group_attr(parent,
1860 "name");
1861 if (prefix != NULL) {
1862 (void) sa_set_group_attr(parent,
1863 "prefix", prefix);
1866 name = fix_resource_name((sa_share_t)group,
1867 value, prefix);
1868 if (name != NULL) {
1869 resource = sa_add_resource(
1870 (sa_share_t)group, name,
1871 SA_SHARE_TRANSIENT, &ret);
1872 sa_free_attr_string(name);
1873 } else {
1874 ret = SA_NO_MEMORY;
1876 if (prefix != NULL)
1877 sa_free_attr_string(prefix);
1879 /* A resource level optionset is needed */
1881 need_optionset = 1;
1882 if (resource == NULL) {
1883 ret = SA_NO_MEMORY;
1884 break;
1886 continue;
1889 if (iszfs && strcmp(token, SHOPT_DESCRIPTION) == 0) {
1890 if (resource == NULL)
1891 (void) sa_set_share_description(
1892 (sa_share_t)group, value);
1893 else
1894 (void) sa_set_resource_description(
1895 resource, value);
1896 continue;
1899 if (need_optionset) {
1900 optionset = sa_create_optionset(resource,
1901 "smb");
1902 need_optionset = 0;
1905 prop = sa_create_property(token, value);
1906 if (prop == NULL)
1907 ret = SA_NO_MEMORY;
1908 else
1909 ret = sa_add_property(optionset, prop);
1910 if (ret != SA_OK)
1911 break;
1912 if (!iszfs)
1913 ret = sa_commit_properties(optionset, !persist);
1916 free(dup);
1917 return (ret);
1921 * smb_sprint_option(rbuff, rbuffsize, incr, prop, sep)
1923 * provides a mechanism to format SMB properties into legacy output
1924 * format. If the buffer would overflow, it is reallocated and grown
1925 * as appropriate. Special cases of converting internal form of values
1926 * to those used by "share" are done. this function does one property
1927 * at a time.
1930 static void
1931 smb_sprint_option(char **rbuff, size_t *rbuffsize, size_t incr,
1932 sa_property_t prop, int sep)
1934 char *name;
1935 char *value;
1936 int curlen;
1937 char *buff = *rbuff;
1938 size_t buffsize = *rbuffsize;
1940 name = sa_get_property_attr(prop, "type");
1941 value = sa_get_property_attr(prop, "value");
1942 if (buff != NULL)
1943 curlen = strlen(buff);
1944 else
1945 curlen = 0;
1946 if (name != NULL) {
1947 int len;
1948 len = strlen(name) + sep;
1951 * A future RFE would be to replace this with more
1952 * generic code and to possibly handle more types.
1954 * For now, everything else is treated as a string. If
1955 * we get any properties that aren't exactly
1956 * name/value pairs, we may need to
1957 * interpret/transform.
1959 if (value != NULL)
1960 len += 1 + strlen(value);
1962 while (buffsize <= (curlen + len)) {
1963 /* need more room */
1964 buffsize += incr;
1965 buff = realloc(buff, buffsize);
1966 *rbuff = buff;
1967 *rbuffsize = buffsize;
1968 if (buff == NULL) {
1969 /* realloc failed so free everything */
1970 free(*rbuff);
1971 goto err;
1974 if (buff == NULL)
1975 goto err;
1976 (void) snprintf(buff + curlen, buffsize - curlen,
1977 "%s%s=%s", sep ? "," : "",
1978 name, value != NULL ? value : "\"\"");
1981 err:
1982 if (name != NULL)
1983 sa_free_attr_string(name);
1984 if (value != NULL)
1985 sa_free_attr_string(value);
1989 * smb_format_resource_options(resource, hier)
1991 * format all the options on the group into a flattened option
1992 * string. If hier is non-zero, walk up the tree to get inherited
1993 * options.
1996 static char *
1997 smb_format_options(sa_group_t group, int hier)
1999 sa_optionset_t options = NULL;
2000 sa_property_t prop;
2001 int sep = 0;
2002 char *buff;
2003 size_t buffsize;
2006 buff = malloc(OPT_CHUNK);
2007 if (buff == NULL)
2008 return (NULL);
2010 buff[0] = '\0';
2011 buffsize = OPT_CHUNK;
2014 * We may have a an optionset relative to this item. format
2015 * these if we find them and then add any security definitions.
2018 options = sa_get_derived_optionset(group, "smb", hier);
2021 * do the default set first but skip any option that is also
2022 * in the protocol specific optionset.
2024 if (options != NULL) {
2025 for (prop = sa_get_property(options, NULL);
2026 prop != NULL; prop = sa_get_next_property(prop)) {
2028 * use this one since we skipped any
2029 * of these that were also in
2030 * optdefault
2032 smb_sprint_option(&buff, &buffsize, OPT_CHUNK,
2033 prop, sep);
2034 if (buff == NULL) {
2036 * buff could become NULL if there
2037 * isn't enough memory for
2038 * smb_sprint_option to realloc()
2039 * as necessary. We can't really
2040 * do anything about it at this
2041 * point so we return NULL. The
2042 * caller should handle the
2043 * failure.
2045 if (options != NULL)
2046 sa_free_derived_optionset(
2047 options);
2048 return (buff);
2050 sep = 1;
2054 if (options != NULL)
2055 sa_free_derived_optionset(options);
2056 return (buff);
2060 * smb_rename_resource(resource, newname)
2062 * Change the current exported name of the resource to newname.
2064 /*ARGSUSED*/
2066 smb_rename_resource(sa_handle_t handle, sa_resource_t resource, char *newname)
2068 int ret = SA_OK;
2069 int err;
2070 char *oldname;
2072 if (!smb_isonline())
2073 return (SA_OK);
2075 oldname = sa_get_resource_attr(resource, "name");
2076 if (oldname == NULL)
2077 return (SA_NO_SUCH_RESOURCE);
2079 err = smb_share_rename(oldname, newname);
2081 sa_free_attr_string(oldname);
2083 /* improve error values somewhat */
2084 switch (err) {
2085 case NERR_Success:
2086 break;
2087 case NERR_InternalError:
2088 ret = SA_SYSTEM_ERR;
2089 break;
2090 case NERR_DuplicateShare:
2091 ret = SA_DUPLICATE_NAME;
2092 break;
2093 default:
2094 ret = SA_CONFIG_ERR;
2095 break;
2098 return (ret);
2101 static int
2102 smb_build_shareinfo(sa_share_t share, sa_resource_t resource, smb_share_t *si)
2104 sa_optionset_t opts;
2105 char *path;
2106 char *rname;
2107 char *val = NULL;
2108 char csc_value[SMB_CSC_BUFSZ];
2110 bzero(si, sizeof (smb_share_t));
2112 if ((path = sa_get_share_attr(share, "path")) == NULL)
2113 return (SA_NO_SUCH_PATH);
2115 if ((rname = sa_get_resource_attr(resource, "name")) == NULL) {
2116 sa_free_attr_string(path);
2117 return (SA_NO_SUCH_RESOURCE);
2120 (void) strlcpy(si->shr_path, path, sizeof (si->shr_path));
2121 (void) strlcpy(si->shr_name, rname, sizeof (si->shr_name));
2122 sa_free_attr_string(path);
2123 sa_free_attr_string(rname);
2125 val = sa_get_resource_description(resource);
2126 if (val == NULL)
2127 val = sa_get_share_description(share);
2129 if (val != NULL) {
2130 (void) strlcpy(si->shr_cmnt, val, sizeof (si->shr_cmnt));
2131 sa_free_share_description(val);
2134 si->shr_flags = (sa_is_persistent(share))
2135 ? SMB_SHRF_PERM : SMB_SHRF_TRANS;
2137 opts = sa_get_derived_optionset(resource, SMB_PROTOCOL_NAME, 1);
2138 if (opts == NULL)
2139 return (SA_OK);
2141 if (smb_saprop_getbool(opts, SHOPT_CATIA))
2142 si->shr_flags |= SMB_SHRF_CATIA;
2144 if (smb_saprop_getbool(opts, SHOPT_ABE))
2145 si->shr_flags |= SMB_SHRF_ABE;
2147 if (smb_saprop_getbool(opts, SHOPT_GUEST))
2148 si->shr_flags |= SMB_SHRF_GUEST_OK;
2150 if (smb_saprop_getbool(opts, SHOPT_DFSROOT))
2151 si->shr_flags |= SMB_SHRF_DFSROOT;
2153 (void) smb_saprop_getstr(opts, SHOPT_AD_CONTAINER, si->shr_container,
2154 sizeof (si->shr_container));
2156 if (smb_saprop_getstr(opts, SHOPT_CSC, csc_value, sizeof (csc_value)))
2157 smb_csc_option(csc_value, si);
2159 if (smb_saprop_getstr(opts, SHOPT_RO, si->shr_access_ro,
2160 sizeof (si->shr_access_ro)))
2161 si->shr_flags |= SMB_SHRF_ACC_RO;
2163 if (smb_saprop_getstr(opts, SHOPT_RW, si->shr_access_rw,
2164 sizeof (si->shr_access_rw)))
2165 si->shr_flags |= SMB_SHRF_ACC_RW;
2167 if (smb_saprop_getstr(opts, SHOPT_NONE, si->shr_access_none,
2168 sizeof (si->shr_access_none)))
2169 si->shr_flags |= SMB_SHRF_ACC_NONE;
2171 sa_free_derived_optionset(opts);
2172 return (SA_OK);
2176 * Map a client-side caching (CSC) option to the appropriate share
2177 * flag. Only one option is allowed; an error will be logged if
2178 * multiple options have been specified. We don't need to do anything
2179 * about multiple values here because the SRVSVC will not recognize
2180 * a value containing multiple flags and will return the default value.
2182 * If the option value is not recognized, it will be ignored: invalid
2183 * values will typically be caught and rejected by sharemgr.
2185 static void
2186 smb_csc_option(const char *value, smb_share_t *si)
2188 char buf[SMB_CSC_BUFSZ];
2189 int i;
2191 for (i = 0; i < (sizeof (cscopt) / sizeof (cscopt[0])); ++i) {
2192 if (strcasecmp(value, cscopt[i].value) == 0) {
2193 si->shr_flags |= cscopt[i].flag;
2194 break;
2198 switch (si->shr_flags & SMB_SHRF_CSC_MASK) {
2199 case 0:
2200 case SMB_SHRF_CSC_DISABLED:
2201 case SMB_SHRF_CSC_MANUAL:
2202 case SMB_SHRF_CSC_AUTO:
2203 case SMB_SHRF_CSC_VDO:
2204 break;
2206 default:
2207 buf[0] = '\0';
2209 for (i = 0; i < (sizeof (cscopt) / sizeof (cscopt[0])); ++i) {
2210 if (si->shr_flags & cscopt[i].flag) {
2211 (void) strlcat(buf, " ", SMB_CSC_BUFSZ);
2212 (void) strlcat(buf, cscopt[i].value,
2213 SMB_CSC_BUFSZ);
2217 syslog(LOG_ERR, "csc option conflict:%s", buf);
2218 break;
2223 * Return the option name for the first CSC flag (there should be only
2224 * one) encountered in the share flags.
2226 static char *
2227 smb_csc_name(const smb_share_t *si)
2229 int i;
2231 for (i = 0; i < (sizeof (cscopt) / sizeof (cscopt[0])); ++i) {
2232 if (si->shr_flags & cscopt[i].flag)
2233 return (cscopt[i].value);
2236 return (NULL);
2240 * smb_get_defaultgrp
2242 * If default group for CIFS shares (i.e. "smb") exists
2243 * then it will return the group handle, otherwise it will
2244 * create the group and return the handle.
2246 * All the shares created by CIFS clients (this is only possible
2247 * via RPC) will be added to "smb" groups.
2249 static sa_group_t
2250 smb_get_defaultgrp(sa_handle_t handle)
2252 sa_group_t group = NULL;
2253 int err;
2255 group = sa_get_group(handle, SMB_DEFAULT_SHARE_GROUP);
2256 if (group != NULL)
2257 return (group);
2259 group = sa_create_group(handle, SMB_DEFAULT_SHARE_GROUP, &err);
2260 if (group == NULL)
2261 return (NULL);
2263 if (sa_create_optionset(group, SMB_DEFAULT_SHARE_GROUP) == NULL) {
2264 (void) sa_remove_group(group);
2265 group = NULL;
2268 return (group);
2272 * Checks to see if the command args are the supported substitution specifier.
2273 * i.e. <cmd> %U %S
2275 static int
2276 cmd_validator(int index, char *value)
2278 char cmd[MAXPATHLEN];
2279 char *ptr, *v;
2280 boolean_t skip_cmdname;
2282 if (string_length_check_validator(index, value) != SA_OK)
2283 return (SA_BAD_VALUE);
2285 if (*value == '\0')
2286 return (SA_OK);
2288 (void) strlcpy(cmd, value, sizeof (cmd));
2290 ptr = cmd;
2291 skip_cmdname = B_TRUE;
2292 do {
2293 if ((v = strsep(&ptr, " ")) == NULL)
2294 break;
2296 if (*v != '\0') {
2298 if (skip_cmdname) {
2299 skip_cmdname = B_FALSE;
2300 continue;
2303 if ((strlen(v) != 2) || *v != '%')
2304 return (SA_BAD_VALUE);
2306 if (strpbrk(v, SMB_VALID_SUB_CHRS) == NULL)
2307 return (SA_BAD_VALUE);
2310 } while (v != NULL);
2313 * If skip_cmdname is still true then the string contains
2314 * only spaces. Don't allow such a string.
2316 if (skip_cmdname)
2317 return (SA_BAD_VALUE);
2319 return (SA_OK);
2322 /*ARGSUSED*/
2323 static int
2324 disposition_validator(int index, char *value)
2326 if (value == NULL)
2327 return (SA_BAD_VALUE);
2329 if (*value == '\0')
2330 return (SA_OK);
2332 if ((strcasecmp(value, SMB_EXEC_DISP_CONTINUE) == 0) ||
2333 (strcasecmp(value, SMB_EXEC_DISP_TERMINATE) == 0))
2334 return (SA_OK);
2336 return (SA_BAD_VALUE);
2339 /*ARGSUSED*/
2340 static int
2341 max_protocol_validator(int index, char *value)
2343 if (value == NULL)
2344 return (SA_BAD_VALUE);
2346 if (*value == '\0')
2347 return (SA_OK);
2349 if (smb_config_check_protocol(value) == 0)
2350 return (SA_OK);
2352 return (SA_BAD_VALUE);
2357 * Updates the optionset properties of the share resource.
2358 * The properties are given as a list of name-value pair.
2359 * The name argument should be the optionset property name and the value
2360 * should be a valid value for the specified property.
2362 * When calling this function for permanent shares, the caller must also
2363 * call sa_commit_properties() to commit the changes to SMF.
2365 static int
2366 smb_update_optionset_props(sa_handle_t handle, sa_resource_t resource,
2367 nvlist_t *nvl)
2369 sa_property_t prop;
2370 sa_optionset_t opts;
2371 int err = SA_OK;
2372 nvpair_t *cur;
2373 char *name, *val;
2375 if ((opts = sa_get_optionset(resource, SMB_PROTOCOL_NAME)) == NULL) {
2376 opts = sa_create_optionset(resource, SMB_PROTOCOL_NAME);
2377 if (opts == NULL)
2378 return (SA_CONFIG_ERR);
2381 cur = nvlist_next_nvpair(nvl, NULL);
2382 while (cur != NULL) {
2383 name = nvpair_name(cur);
2384 err = nvpair_value_string(cur, &val);
2385 if ((err != 0) || (name == NULL) || (val == NULL)) {
2386 err = SA_CONFIG_ERR;
2387 break;
2390 prop = NULL;
2391 if ((prop = sa_get_property(opts, name)) == NULL) {
2392 prop = sa_create_property(name, val);
2393 if (prop != NULL) {
2394 err = sa_valid_property(handle, opts,
2395 SMB_PROTOCOL_NAME, prop);
2396 if (err != SA_OK) {
2397 (void) sa_remove_property(prop);
2398 break;
2401 err = sa_add_property(opts, prop);
2402 if (err != SA_OK)
2403 break;
2404 } else {
2405 err = sa_update_property(prop, val);
2406 if (err != SA_OK)
2407 break;
2410 cur = nvlist_next_nvpair(nvl, cur);
2413 return (err);
2416 static boolean_t
2417 smb_saprop_getbool(sa_optionset_t opts, char *propname)
2419 sa_property_t prop;
2420 char *val;
2421 boolean_t propval = B_FALSE;
2423 prop = sa_get_property(opts, propname);
2424 if ((val = sa_get_property_attr(prop, "value")) != NULL) {
2425 if ((strcasecmp(val, "true") == 0) || (strcmp(val, "1") == 0))
2426 propval = B_TRUE;
2427 free(val);
2430 return (propval);
2433 static boolean_t
2434 smb_saprop_getstr(sa_optionset_t opts, char *propname, char *buf, size_t bufsz)
2436 sa_property_t prop;
2437 char *val;
2439 prop = sa_get_property(opts, propname);
2440 if ((val = sa_get_property_attr(prop, "value")) != NULL) {
2441 (void) strlcpy(buf, val, bufsz);
2442 free(val);
2443 return (B_TRUE);
2446 return (B_FALSE);