5921 Want a netbios_enable parameter in sharectl_smb
[unleashed.git] / usr / src / lib / libshare / smb / libshare_smb.c
blob0d087329f7c8a439a3ca3e8e29d85a6d4b59062e
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 2013 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 "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>
56 #define SMB_CSC_BUFSZ 64
58 #define SMB_VALID_SUB_CHRS "UDhMLmIiSPu" /* substitution characters */
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);
75 static int smb_enable_service(void);
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 *);
88 static int smb_enable_resource(sa_resource_t);
89 static int smb_disable_resource(sa_resource_t);
90 static uint64_t smb_share_features(void);
91 static int smb_list_transient(sa_handle_t);
93 static int smb_build_shareinfo(sa_share_t, sa_resource_t, smb_share_t *);
94 static void smb_csc_option(const char *, smb_share_t *);
95 static char *smb_csc_name(const smb_share_t *);
96 static sa_group_t smb_get_defaultgrp(sa_handle_t);
97 static int interface_validator(int, char *);
98 static int smb_update_optionset_props(sa_handle_t, sa_resource_t, nvlist_t *);
100 static boolean_t smb_saprop_getbool(sa_optionset_t, char *);
101 static boolean_t smb_saprop_getstr(sa_optionset_t, char *, char *, size_t);
103 static struct {
104 char *value;
105 uint32_t flag;
106 } cscopt[] = {
107 { "disabled", SMB_SHRF_CSC_DISABLED },
108 { "manual", SMB_SHRF_CSC_MANUAL },
109 { "auto", SMB_SHRF_CSC_AUTO },
110 { "vdo", SMB_SHRF_CSC_VDO }
113 /* size of basic format allocation */
114 #define OPT_CHUNK 1024
116 /* size of string for types - big enough to hold "dependency" */
117 #define SCFTYPE_LEN 32
120 * Indexes of entries in smb_proto_options table.
121 * Changes to smb_proto_options table may require
122 * an update to these values.
124 #define PROTO_OPT_WINS1 6
125 #define PROTO_OPT_WINS_EXCLUDE 8
127 typedef struct smb_hostifs_walker {
128 const char *hiw_ifname;
129 boolean_t hiw_matchfound;
130 } smb_hostifs_walker_t;
134 * ops vector that provides the protocol specific info and operations
135 * for share management.
138 struct sa_plugin_ops sa_plugin_ops = {
139 SA_PLUGIN_VERSION,
140 SMB_PROTOCOL_NAME,
141 smb_share_init,
142 smb_share_fini,
143 smb_enable_share,
144 smb_disable_share,
145 smb_validate_property,
146 NULL, /* valid_space */
147 NULL, /* security_prop */
148 smb_parse_optstring,
149 smb_format_options,
150 smb_set_proto_prop,
151 smb_get_proto_set,
152 smb_get_status,
153 NULL, /* space_alias */
154 NULL, /* update_legacy */
155 NULL, /* delete_legacy */
156 smb_share_changed,
157 smb_enable_resource,
158 smb_disable_resource,
159 smb_share_features,
160 smb_list_transient,
161 smb_resource_changed,
162 smb_rename_resource,
163 NULL, /* run_command */
164 NULL, /* command_help */
165 NULL /* delete_proto_section */
168 struct option_defs optdefs[] = {
169 { SHOPT_AD_CONTAINER, OPT_TYPE_STRING },
170 { SHOPT_ABE, OPT_TYPE_BOOLEAN },
171 { SHOPT_NAME, OPT_TYPE_NAME },
172 { SHOPT_RO, OPT_TYPE_ACCLIST },
173 { SHOPT_RW, OPT_TYPE_ACCLIST },
174 { SHOPT_NONE, OPT_TYPE_ACCLIST },
175 { SHOPT_CATIA, OPT_TYPE_BOOLEAN },
176 { SHOPT_CSC, OPT_TYPE_CSC },
177 { SHOPT_GUEST, OPT_TYPE_BOOLEAN },
178 { SHOPT_DFSROOT, OPT_TYPE_BOOLEAN },
179 { SHOPT_DESCRIPTION, OPT_TYPE_STRING },
180 { NULL, NULL }
184 * findopt(name)
186 * Lookup option "name" in the option table and return the table
187 * index.
189 static int
190 findopt(char *name)
192 int i;
193 if (name != NULL) {
194 for (i = 0; optdefs[i].tag != NULL; i++) {
195 if (strcmp(optdefs[i].tag, name) == 0)
196 return (i);
199 return (-1);
203 * is_a_number(number)
205 * is the string a number in one of the forms we want to use?
207 static boolean_t
208 is_a_number(char *number)
210 boolean_t isnum = B_TRUE;
211 boolean_t ishex = B_FALSE;
213 if (number == NULL || *number == '\0')
214 return (B_FALSE);
216 if (strncasecmp(number, "0x", 2) == 0) {
217 number += 2;
218 ishex = B_TRUE;
219 } else if (*number == '-') {
220 number++;
223 while (isnum && (*number != '\0')) {
224 isnum = (ishex) ? isxdigit(*number) : isdigit(*number);
225 number++;
228 return (isnum);
232 * check ro vs rw values. Over time this may get beefed up.
233 * for now it just does simple checks.
236 static int
237 check_rorw(char *v1, char *v2)
239 int ret = SA_OK;
240 if (strcmp(v1, v2) == 0)
241 ret = SA_VALUE_CONFLICT;
242 return (ret);
246 * validresource(name)
248 * Check that name only has valid characters in it. The current valid
249 * set are the printable characters but not including:
250 * " / \ [ ] : | < > + ; , ? * = \t
251 * Note that space is included and there is a maximum length.
253 static boolean_t
254 validresource(const char *name)
256 const char *cp;
257 size_t len;
259 if (name == NULL)
260 return (B_FALSE);
262 len = strlen(name);
263 if (len == 0 || len > SA_MAX_RESOURCE_NAME)
264 return (B_FALSE);
266 if (strpbrk(name, "\"/\\[]:|<>+;,?*=\t") != NULL) {
267 return (B_FALSE);
270 for (cp = name; *cp != '\0'; cp++)
271 if (iscntrl(*cp))
272 return (B_FALSE);
274 return (B_TRUE);
278 * Check that the client-side caching (CSC) option value is valid.
280 static boolean_t
281 validcsc(const char *value)
283 int i;
285 for (i = 0; i < (sizeof (cscopt) / sizeof (cscopt[0])); ++i) {
286 if (strcasecmp(value, cscopt[i].value) == 0)
287 return (B_TRUE);
290 return (B_FALSE);
294 * smb_isonline()
296 * Determine if the SMF service instance is in the online state or
297 * not. A number of operations depend on this state.
299 static boolean_t
300 smb_isonline(void)
302 char *str;
303 boolean_t ret = B_FALSE;
305 if ((str = smf_get_state(SMBD_DEFAULT_INSTANCE_FMRI)) != NULL) {
306 ret = (strcmp(str, SCF_STATE_STRING_ONLINE) == 0);
307 free(str);
309 return (ret);
313 * smb_isdisabled()
315 * Determine if the SMF service instance is in the disabled state or
316 * not. A number of operations depend on this state.
318 static boolean_t
319 smb_isdisabled(void)
321 char *str;
322 boolean_t ret = B_FALSE;
324 if ((str = smf_get_state(SMBD_DEFAULT_INSTANCE_FMRI)) != NULL) {
325 ret = (strcmp(str, SCF_STATE_STRING_DISABLED) == 0);
326 free(str);
328 return (ret);
332 * smb_isautoenable()
334 * Determine if the SMF service instance auto_enabled set or not. A
335 * number of operations depend on this state. The property not being
336 * set or being set to true means autoenable. Only being set to false
337 * is not autoenabled.
339 static boolean_t
340 smb_isautoenable(void)
342 boolean_t ret = B_TRUE;
343 scf_simple_prop_t *prop;
344 uint8_t *retstr;
346 prop = scf_simple_prop_get(NULL, SMBD_DEFAULT_INSTANCE_FMRI,
347 "application", "auto_enable");
348 if (prop != NULL) {
349 retstr = scf_simple_prop_next_boolean(prop);
350 ret = *retstr != 0;
351 scf_simple_prop_free(prop);
353 return (ret);
357 * smb_ismaint()
359 * Determine if the SMF service instance is in the disabled state or
360 * not. A number of operations depend on this state.
362 static boolean_t
363 smb_ismaint(void)
365 char *str;
366 boolean_t ret = B_FALSE;
368 if ((str = smf_get_state(SMBD_DEFAULT_INSTANCE_FMRI)) != NULL) {
369 ret = (strcmp(str, SCF_STATE_STRING_MAINT) == 0);
370 free(str);
372 return (ret);
376 * smb_enable_share tells the implementation that it is to enable the share.
377 * This entails converting the path and options into the appropriate ioctl
378 * calls. It is assumed that all error checking of paths, etc. were
379 * done earlier.
381 static int
382 smb_enable_share(sa_share_t share)
384 char *path;
385 smb_share_t si;
386 sa_resource_t resource;
387 boolean_t iszfs;
388 boolean_t privileged;
389 int err = SA_OK;
390 priv_set_t *priv_effective;
391 boolean_t online;
394 * Don't support Trusted Extensions.
396 if (is_system_labeled()) {
397 (void) printf(dgettext(TEXT_DOMAIN,
398 "SMB: service not supported with Trusted Extensions\n"));
399 return (SA_NOT_SUPPORTED);
402 priv_effective = priv_allocset();
403 (void) getppriv(PRIV_EFFECTIVE, priv_effective);
404 privileged = (priv_isfullset(priv_effective) == B_TRUE);
405 priv_freeset(priv_effective);
407 /* get the path since it is important in several places */
408 path = sa_get_share_attr(share, "path");
409 if (path == NULL)
410 return (SA_NO_SUCH_PATH);
413 * If administratively disabled, don't try to start anything.
415 online = smb_isonline();
416 if (!online && !smb_isautoenable() && smb_isdisabled())
417 goto done;
419 iszfs = sa_path_is_zfs(path);
421 if (iszfs) {
423 if (privileged == B_FALSE && !online) {
425 if (!online) {
426 (void) printf(dgettext(TEXT_DOMAIN,
427 "SMB: Cannot share remove "
428 "file system: %s\n"), path);
429 (void) printf(dgettext(TEXT_DOMAIN,
430 "SMB: Service needs to be enabled "
431 "by a privileged user\n"));
432 err = SA_NO_PERMISSION;
433 errno = EPERM;
435 if (err) {
436 sa_free_attr_string(path);
437 return (err);
443 if (privileged == B_TRUE && !online) {
444 err = smb_enable_service();
445 if (err != SA_OK) {
446 (void) printf(dgettext(TEXT_DOMAIN,
447 "SMB: Unable to enable service\n"));
448 } else {
449 online = B_TRUE;
454 * Don't bother trying to start shares if the service isn't
455 * running.
457 if (!online)
458 goto done;
460 /* Each share can have multiple resources */
461 for (resource = sa_get_share_resource(share, NULL);
462 resource != NULL;
463 resource = sa_get_next_resource(resource)) {
464 err = smb_build_shareinfo(share, resource, &si);
465 if (err != SA_OK) {
466 sa_free_attr_string(path);
467 return (err);
470 if (!iszfs) {
471 err = smb_share_create(&si);
472 } else {
473 share_t sh;
475 (void) sa_sharetab_fill_zfs(share, &sh, "smb");
476 err = sa_share_zfs(share, resource, (char *)path, &sh,
477 &si, ZFS_SHARE_SMB);
478 if (err != SA_OK) {
479 errno = err;
480 err = -1;
482 sa_emptyshare(&sh);
485 if (!iszfs)
486 (void) sa_update_sharetab(share, "smb");
487 done:
488 sa_free_attr_string(path);
490 return (err == NERR_DuplicateShare ? 0 : err);
494 * This is the share for CIFS all shares have resource names.
495 * Enable tells the smb server to update its hash. If it fails
496 * because smb server is down, we just ignore as smb server loads
497 * the resources from sharemanager at startup.
500 static int
501 smb_enable_resource(sa_resource_t resource)
503 sa_share_t share;
504 smb_share_t si;
505 int ret = SA_OK;
506 int err;
507 boolean_t isonline;
509 share = sa_get_resource_parent(resource);
510 if (share == NULL)
511 return (SA_NO_SUCH_PATH);
514 * If administratively disabled, don't try to start anything.
516 isonline = smb_isonline();
517 if (!isonline && !smb_isautoenable() && smb_isdisabled())
518 return (SA_OK);
520 if (!isonline) {
521 (void) smb_enable_service();
523 if (!smb_isonline())
524 return (SA_OK);
527 if ((ret = smb_build_shareinfo(share, resource, &si)) != SA_OK)
528 return (ret);
531 * Attempt to add the share. Any error that occurs if it was
532 * online is an error but don't count NERR_DuplicateName if
533 * smb/server had to be brought online since bringing the
534 * service up will enable the share that was just added prior
535 * to the attempt to enable.
537 err = smb_share_create(&si);
538 if (err == NERR_Success || !(!isonline && err == NERR_DuplicateName))
539 (void) sa_update_sharetab(share, "smb");
540 else
541 return (SA_NOT_SHARED);
543 return (SA_OK);
547 * Remove it from smb server hash.
549 static int
550 smb_disable_resource(sa_resource_t resource)
552 char *rname;
553 uint32_t res;
554 sa_share_t share;
556 rname = sa_get_resource_attr(resource, "name");
557 if (rname == NULL)
558 return (SA_NO_SUCH_RESOURCE);
560 if (smb_isonline()) {
561 res = smb_share_delete(rname);
562 if (res != NERR_Success &&
563 res != NERR_NetNameNotFound) {
564 sa_free_attr_string(rname);
565 return (SA_CONFIG_ERR);
569 sa_free_attr_string(rname);
571 share = sa_get_resource_parent(resource);
572 if (share != NULL) {
573 rname = sa_get_share_attr(share, "path");
574 if (rname != NULL) {
575 sa_handle_t handle;
577 handle = sa_find_group_handle((sa_group_t)resource);
578 (void) sa_delete_sharetab(handle, rname, "smb");
579 sa_free_attr_string(rname);
583 * Always return OK as smb/server may be down and
584 * Shares will be picked up when loaded.
586 return (SA_OK);
590 * smb_share_changed(sa_share_t share)
592 * The specified share has changed.
594 static int
595 smb_share_changed(sa_share_t share)
597 char *path;
598 sa_resource_t resource;
600 if (!smb_isonline())
601 return (SA_OK);
603 /* get the path since it is important in several places */
604 path = sa_get_share_attr(share, "path");
605 if (path == NULL)
606 return (SA_NO_SUCH_PATH);
608 for (resource = sa_get_share_resource(share, NULL);
609 resource != NULL;
610 resource = sa_get_next_resource(resource))
611 (void) smb_resource_changed(resource);
613 sa_free_attr_string(path);
615 return (SA_OK);
619 * smb_resource_changed(sa_resource_t resource)
621 * The specified resource has changed.
623 static int
624 smb_resource_changed(sa_resource_t resource)
626 uint32_t res;
627 sa_share_t share;
628 smb_share_t si;
630 if (!smb_isonline())
631 return (SA_OK);
633 if ((share = sa_get_resource_parent(resource)) == NULL)
634 return (SA_CONFIG_ERR);
636 if ((res = smb_build_shareinfo(share, resource, &si)) != SA_OK)
637 return (res);
639 res = smb_share_modify(&si);
641 if (res != NERR_Success)
642 return (SA_CONFIG_ERR);
644 return (smb_enable_service());
648 * smb_disable_share(sa_share_t share, char *path)
650 * Unshare the specified share. Note that "path" is the same
651 * path as what is in the "share" object. It is passed in to avoid an
652 * additional lookup. A missing "path" value makes this a no-op
653 * function.
655 static int
656 smb_disable_share(sa_share_t share, char *path)
658 char *rname;
659 sa_resource_t resource;
660 sa_group_t parent;
661 boolean_t iszfs;
662 int err = SA_OK;
663 int ret = SA_OK;
664 sa_handle_t handle;
665 boolean_t first = B_TRUE; /* work around sharetab issue */
667 if (path == NULL)
668 return (ret);
671 * If the share is in a ZFS group we need to handle it
672 * differently. Just being on a ZFS file system isn't
673 * enough since we may be in a legacy share case.
675 parent = sa_get_parent_group(share);
676 iszfs = sa_group_is_zfs(parent);
678 if (!smb_isonline())
679 goto done;
681 for (resource = sa_get_share_resource(share, NULL);
682 resource != NULL;
683 resource = sa_get_next_resource(resource)) {
684 rname = sa_get_resource_attr(resource, "name");
685 if (rname == NULL) {
686 continue;
688 if (!iszfs) {
689 err = smb_share_delete(rname);
690 switch (err) {
691 case NERR_NetNameNotFound:
692 case NERR_Success:
693 err = SA_OK;
694 break;
695 default:
696 err = SA_CONFIG_ERR;
697 break;
699 } else {
700 share_t sh;
702 (void) sa_sharetab_fill_zfs(share, &sh, "smb");
703 err = sa_share_zfs(share, resource, (char *)path, &sh,
704 rname, ZFS_UNSHARE_SMB);
705 if (err != SA_OK) {
706 switch (err) {
707 case EINVAL:
708 case ENOENT:
709 err = SA_OK;
710 break;
711 default:
713 * If we are no longer the first case,
714 * we don't care about the sa_share_zfs
715 * err if it is -1. This works around
716 * a problem in sharefs and should be
717 * removed when sharefs supports
718 * multiple entries per path.
720 if (!first)
721 err = SA_OK;
722 else
723 err = SA_SYSTEM_ERR;
724 break;
728 first = B_FALSE;
730 sa_emptyshare(&sh);
733 if (err != SA_OK)
734 ret = err;
735 sa_free_attr_string(rname);
737 done:
738 if (!iszfs) {
739 handle = sa_find_group_handle((sa_group_t)share);
740 if (handle != NULL)
741 (void) sa_delete_sharetab(handle, path, "smb");
742 else
743 ret = SA_SYSTEM_ERR;
745 return (ret);
749 * smb_validate_property(handle, property, parent)
751 * Check that the property has a legitimate value for its type.
752 * Handle isn't currently used but may need to be in the future.
755 /*ARGSUSED*/
756 static int
757 smb_validate_property(sa_handle_t handle, sa_property_t property,
758 sa_optionset_t parent)
760 int ret = SA_OK;
761 char *propname;
762 int optindex;
763 sa_group_t parent_group;
764 char *value;
765 char *other;
767 propname = sa_get_property_attr(property, "type");
769 if ((optindex = findopt(propname)) < 0)
770 ret = SA_NO_SUCH_PROP;
772 /* need to validate value range here as well */
773 if (ret == SA_OK) {
774 parent_group = sa_get_parent_group((sa_share_t)parent);
775 if (optdefs[optindex].share && !sa_is_share(parent_group))
776 ret = SA_PROP_SHARE_ONLY;
778 if (ret != SA_OK) {
779 if (propname != NULL)
780 sa_free_attr_string(propname);
781 return (ret);
784 value = sa_get_property_attr(property, "value");
785 if (value != NULL) {
786 /* first basic type checking */
787 switch (optdefs[optindex].type) {
788 case OPT_TYPE_NUMBER:
789 /* check that the value is all digits */
790 if (!is_a_number(value))
791 ret = SA_BAD_VALUE;
792 break;
793 case OPT_TYPE_BOOLEAN:
794 ret = true_false_validator(0, value);
795 break;
796 case OPT_TYPE_NAME:
798 * Make sure no invalid characters
800 if (!validresource(value))
801 ret = SA_BAD_VALUE;
802 break;
803 case OPT_TYPE_STRING:
804 /* whatever is here should be ok */
805 break;
806 case OPT_TYPE_CSC:
807 if (!validcsc(value))
808 ret = SA_BAD_VALUE;
809 break;
810 case OPT_TYPE_ACCLIST: {
811 sa_property_t oprop;
812 char *ovalue;
814 * access list handling. Should eventually
815 * validate that all the values make sense.
816 * Also, ro and rw may have cross value
817 * conflicts.
819 if (parent == NULL)
820 break;
821 if (strcmp(propname, SHOPT_RO) == 0)
822 other = SHOPT_RW;
823 else if (strcmp(propname, SHOPT_RW) == 0)
824 other = SHOPT_RO;
825 else
826 other = NULL;
827 if (other == NULL)
828 break;
830 /* compare rw(ro) with ro(rw) */
831 oprop = sa_get_property(parent, other);
832 if (oprop == NULL)
833 break;
835 * only potential
836 * confusion if other
837 * exists
839 ovalue = sa_get_property_attr(oprop, "value");
840 if (ovalue != NULL) {
841 ret = check_rorw(value, ovalue);
842 sa_free_attr_string(ovalue);
844 break;
846 default:
847 break;
851 if (value != NULL)
852 sa_free_attr_string(value);
853 if (ret == SA_OK && optdefs[optindex].check != NULL)
854 /* do the property specific check */
855 ret = optdefs[optindex].check(property);
857 if (propname != NULL)
858 sa_free_attr_string(propname);
859 return (ret);
863 * Protocol management functions
865 * properties defined in the default files are defined in
866 * proto_option_defs for parsing and validation.
869 struct smb_proto_option_defs {
870 int smb_index;
871 int32_t minval;
872 int32_t maxval; /* In case of length of string this should be max */
873 int (*validator)(int, char *);
874 int32_t refresh;
875 } smb_proto_options[] = {
876 { SMB_CI_SYS_CMNT, 0, MAX_VALUE_BUFLEN,
877 string_length_check_validator, SMB_REFRESH_REFRESH },
878 { SMB_CI_MAX_WORKERS, 64, 1024, range_check_validator,
879 SMB_REFRESH_REFRESH },
880 { SMB_CI_NBSCOPE, 0, MAX_VALUE_BUFLEN,
881 string_length_check_validator, 0 },
882 { SMB_CI_LM_LEVEL, 2, 5, range_check_validator, 0 },
883 { SMB_CI_KEEPALIVE, 20, 5400, range_check_validator_zero_ok,
884 SMB_REFRESH_REFRESH },
885 { SMB_CI_WINS_SRV1, 0, MAX_VALUE_BUFLEN,
886 ipv4_validator, SMB_REFRESH_REFRESH },
887 { SMB_CI_WINS_SRV2, 0, MAX_VALUE_BUFLEN,
888 ipv4_validator, SMB_REFRESH_REFRESH },
889 { SMB_CI_WINS_EXCL, 0, MAX_VALUE_BUFLEN,
890 interface_validator, SMB_REFRESH_REFRESH },
891 { SMB_CI_SIGNING_ENABLE, 0, 0, true_false_validator,
892 SMB_REFRESH_REFRESH },
893 { SMB_CI_SIGNING_REQD, 0, 0, true_false_validator,
894 SMB_REFRESH_REFRESH },
895 { SMB_CI_RESTRICT_ANON, 0, 0, true_false_validator,
896 SMB_REFRESH_REFRESH },
897 { SMB_CI_DOMAIN_SRV, 0, MAX_VALUE_BUFLEN,
898 hostname_validator, SMB_REFRESH_REFRESH },
899 { SMB_CI_ADS_SITE, 0, MAX_VALUE_BUFLEN,
900 string_length_check_validator, SMB_REFRESH_REFRESH },
901 { SMB_CI_DYNDNS_ENABLE, 0, 0, true_false_validator, 0 },
902 { SMB_CI_AUTOHOME_MAP, 0, MAX_VALUE_BUFLEN, path_validator, 0 },
903 { SMB_CI_IPV6_ENABLE, 0, 0, true_false_validator,
904 SMB_REFRESH_REFRESH },
905 { SMB_CI_PRINT_ENABLE, 0, 0, print_enable_validator,
906 SMB_REFRESH_REFRESH },
907 { SMB_CI_TRAVERSE_MOUNTS, 0, 0, true_false_validator,
908 SMB_REFRESH_REFRESH },
909 { SMB_CI_MAP, 0, MAX_VALUE_BUFLEN, cmd_validator, SMB_REFRESH_REFRESH },
910 { SMB_CI_UNMAP, 0, MAX_VALUE_BUFLEN, cmd_validator,
911 SMB_REFRESH_REFRESH },
912 { SMB_CI_DISPOSITION, 0, MAX_VALUE_BUFLEN,
913 disposition_validator, SMB_REFRESH_REFRESH },
914 { SMB_CI_NETBIOS_ENABLE, 0, 0, true_false_validator,
915 SMB_REFRESH_REFRESH },
918 #define SMB_OPT_NUM \
919 (sizeof (smb_proto_options) / sizeof (smb_proto_options[0]))
922 * Check the range of value as int range.
924 static int
925 range_check_validator(int index, char *value)
927 int ret = SA_OK;
929 if (!is_a_number(value)) {
930 ret = SA_BAD_VALUE;
931 } else {
932 int val;
933 val = strtoul(value, NULL, 0);
934 if (val < smb_proto_options[index].minval ||
935 val > smb_proto_options[index].maxval)
936 ret = SA_BAD_VALUE;
938 return (ret);
942 * Check the range of value as int range.
944 static int
945 range_check_validator_zero_ok(int index, char *value)
947 int ret = SA_OK;
949 if (!is_a_number(value)) {
950 ret = SA_BAD_VALUE;
951 } else {
952 int val;
953 val = strtoul(value, NULL, 0);
954 if (val == 0)
955 ret = SA_OK;
956 else {
957 if (val < smb_proto_options[index].minval ||
958 val > smb_proto_options[index].maxval)
959 ret = SA_BAD_VALUE;
962 return (ret);
966 * Check the length of the string
968 static int
969 string_length_check_validator(int index, char *value)
971 int ret = SA_OK;
973 if (value == NULL)
974 return (SA_BAD_VALUE);
975 if (strlen(value) > smb_proto_options[index].maxval)
976 ret = SA_BAD_VALUE;
977 return (ret);
981 * Check yes/no
983 /*ARGSUSED*/
984 static int
985 true_false_validator(int index, char *value)
987 if (value == NULL)
988 return (SA_BAD_VALUE);
989 if ((strcasecmp(value, "true") == 0) ||
990 (strcasecmp(value, "false") == 0))
991 return (SA_OK);
992 return (SA_BAD_VALUE);
996 * If printing support is compiled in, this is the same as:
997 * true_false_validator. Otherwise, only allow false.
999 /*ARGSUSED*/
1000 static int
1001 print_enable_validator(int index, char *value)
1003 if (value == NULL)
1004 return (SA_BAD_VALUE);
1006 #ifdef HAVE_CUPS
1007 if (strcasecmp(value, "true") == 0)
1008 return (SA_OK);
1009 #endif
1010 if (strcasecmp(value, "false") == 0)
1011 return (SA_OK);
1013 return (SA_BAD_VALUE);
1017 * Check IP v4 address.
1019 /*ARGSUSED*/
1020 static int
1021 ipv4_validator(int index, char *value)
1023 char sbytes[16];
1025 if (value == NULL)
1026 return (SA_OK);
1028 if (strlen(value) == 0)
1029 return (SA_OK);
1031 if (inet_pton(AF_INET, value, (void *)sbytes) != 1)
1032 return (SA_BAD_VALUE);
1034 return (SA_OK);
1038 * Check that the specified name is an IP address (v4 or v6) or a hostname.
1039 * Per RFC 1035 and 1123, names may contain alphanumeric characters, hyphens
1040 * and dots. The first and last character of a label must be alphanumeric.
1041 * Interior characters may be alphanumeric or hypens.
1043 * Domain names should not contain underscores but we allow them because
1044 * Windows names are often in non-compliance with this rule.
1046 /*ARGSUSED*/
1047 static int
1048 hostname_validator(int index, char *value)
1050 char sbytes[INET6_ADDRSTRLEN];
1051 boolean_t new_label = B_TRUE;
1052 char *p;
1053 char label_terminator;
1054 int len;
1056 if (value == NULL)
1057 return (SA_OK);
1059 if ((len = strlen(value)) == 0)
1060 return (SA_OK);
1062 if (inet_pton(AF_INET, value, (void *)sbytes) == 1)
1063 return (SA_OK);
1065 if (inet_pton(AF_INET6, value, (void *)sbytes) == 1)
1066 return (SA_OK);
1068 if (len >= MAXHOSTNAMELEN)
1069 return (SA_BAD_VALUE);
1071 if (strspn(value, "0123456789.") == len)
1072 return (SA_BAD_VALUE);
1074 label_terminator = *value;
1076 for (p = value; *p != '\0'; ++p) {
1077 if (new_label) {
1078 if (!isalnum(*p))
1079 return (SA_BAD_VALUE);
1080 new_label = B_FALSE;
1081 label_terminator = *p;
1082 continue;
1085 if (*p == '.') {
1086 if (!isalnum(label_terminator))
1087 return (SA_BAD_VALUE);
1088 new_label = B_TRUE;
1089 label_terminator = *p;
1090 continue;
1093 label_terminator = *p;
1095 if (isalnum(*p) || *p == '-' || *p == '_')
1096 continue;
1098 return (SA_BAD_VALUE);
1101 if (!isalnum(label_terminator))
1102 return (SA_BAD_VALUE);
1104 return (SA_OK);
1108 * Call back function for dlpi_walk.
1109 * Returns TRUE if interface name exists on the host.
1111 static boolean_t
1112 smb_get_interface(const char *ifname, void *arg)
1114 smb_hostifs_walker_t *iterp = arg;
1116 iterp->hiw_matchfound = (strcmp(ifname, iterp->hiw_ifname) == 0);
1118 return (iterp->hiw_matchfound);
1122 * Checks to see if the input interface exists on the host.
1123 * Returns B_TRUE if the match is found, B_FALSE otherwise.
1125 static boolean_t
1126 smb_validate_interface(const char *ifname)
1128 smb_hostifs_walker_t iter;
1130 if ((ifname == NULL) || (*ifname == '\0'))
1131 return (B_FALSE);
1133 iter.hiw_ifname = ifname;
1134 iter.hiw_matchfound = B_FALSE;
1135 dlpi_walk(smb_get_interface, &iter, 0);
1137 return (iter.hiw_matchfound);
1141 * Check valid interfaces. Interface names value can be NULL or empty.
1142 * Returns SA_BAD_VALUE if interface cannot be found on the host.
1144 /*ARGSUSED*/
1145 static int
1146 interface_validator(int index, char *value)
1148 char buf[16];
1149 int ret = SA_OK;
1150 char *ifname, *tmp, *p;
1152 if (value == NULL || *value == '\0')
1153 return (ret);
1155 if (strlen(value) > MAX_VALUE_BUFLEN)
1156 return (SA_BAD_VALUE);
1158 if ((p = strdup(value)) == NULL)
1159 return (SA_NO_MEMORY);
1161 tmp = p;
1162 while ((ifname = strsep(&tmp, ",")) != NULL) {
1163 if (*ifname == '\0') {
1164 ret = SA_BAD_VALUE;
1165 break;
1168 if (!smb_validate_interface(ifname)) {
1169 if (inet_pton(AF_INET, ifname, (void *)buf) == 0) {
1170 ret = SA_BAD_VALUE;
1171 break;
1176 free(p);
1177 return (ret);
1181 * Check path
1183 /*ARGSUSED*/
1184 static int
1185 path_validator(int index, char *path)
1187 struct stat buffer;
1188 int fd, status;
1190 if (path == NULL)
1191 return (SA_BAD_VALUE);
1193 fd = open(path, O_RDONLY);
1194 if (fd < 0)
1195 return (SA_BAD_VALUE);
1197 status = fstat(fd, &buffer);
1198 (void) close(fd);
1200 if (status < 0)
1201 return (SA_BAD_VALUE);
1203 if (buffer.st_mode & S_IFDIR)
1204 return (SA_OK);
1205 return (SA_BAD_VALUE);
1209 * the protoset holds the defined options so we don't have to read
1210 * them multiple times
1212 static sa_protocol_properties_t protoset;
1214 static int
1215 findprotoopt(char *name)
1217 int i;
1218 char *sc_name;
1220 for (i = 0; i < SMB_OPT_NUM; i++) {
1221 sc_name = smb_config_getname(smb_proto_options[i].smb_index);
1222 if (strcasecmp(sc_name, name) == 0)
1223 return (i);
1226 return (-1);
1230 * smb_load_proto_properties()
1232 * read the smb config values from SMF.
1235 static int
1236 smb_load_proto_properties()
1238 sa_property_t prop;
1239 char value[MAX_VALUE_BUFLEN];
1240 char *name;
1241 int index;
1242 int ret = SA_OK;
1243 int rc;
1245 protoset = sa_create_protocol_properties(SMB_PROTOCOL_NAME);
1246 if (protoset == NULL)
1247 return (SA_NO_MEMORY);
1249 for (index = 0; index < SMB_OPT_NUM && ret == SA_OK; index++) {
1250 rc = smb_config_get(smb_proto_options[index].smb_index,
1251 value, sizeof (value));
1252 if (rc != SMBD_SMF_OK)
1253 continue;
1254 name = smb_config_getname(smb_proto_options[index].smb_index);
1255 prop = sa_create_property(name, value);
1256 if (prop != NULL)
1257 ret = sa_add_protocol_property(protoset, prop);
1258 else
1259 ret = SA_NO_MEMORY;
1261 return (ret);
1265 * smb_share_init()
1267 * Initialize the smb plugin.
1270 static int
1271 smb_share_init(void)
1273 if (sa_plugin_ops.sa_init != smb_share_init)
1274 return (SA_SYSTEM_ERR);
1276 smb_share_door_clnt_init();
1277 return (smb_load_proto_properties());
1281 * smb_share_fini()
1284 static void
1285 smb_share_fini(void)
1287 xmlFreeNode(protoset);
1288 protoset = NULL;
1290 smb_share_door_clnt_fini();
1294 * smb_get_proto_set()
1296 * Return an optionset with all the protocol specific properties in
1297 * it.
1299 static sa_protocol_properties_t
1300 smb_get_proto_set(void)
1302 return (protoset);
1306 * smb_enable_dependencies()
1308 * SMBD_DEFAULT_INSTANCE_FMRI may have some dependencies that aren't
1309 * enabled. This will attempt to enable all of them.
1311 static void
1312 smb_enable_dependencies(const char *fmri)
1314 scf_handle_t *handle;
1315 scf_service_t *service;
1316 scf_instance_t *inst = NULL;
1317 scf_iter_t *iter;
1318 scf_property_t *prop;
1319 scf_value_t *value;
1320 scf_propertygroup_t *pg;
1321 scf_scope_t *scope;
1322 char type[SCFTYPE_LEN];
1323 char *dependency;
1324 char *servname;
1325 int maxlen;
1328 * Get all required handles and storage.
1330 handle = scf_handle_create(SCF_VERSION);
1331 if (handle == NULL)
1332 return;
1334 if (scf_handle_bind(handle) != 0) {
1335 scf_handle_destroy(handle);
1336 return;
1339 maxlen = scf_limit(SCF_LIMIT_MAX_VALUE_LENGTH);
1340 if (maxlen == (ssize_t)-1)
1341 maxlen = MAXPATHLEN;
1343 dependency = malloc(maxlen);
1345 service = scf_service_create(handle);
1347 iter = scf_iter_create(handle);
1349 pg = scf_pg_create(handle);
1351 prop = scf_property_create(handle);
1353 value = scf_value_create(handle);
1355 scope = scf_scope_create(handle);
1357 if (service == NULL || iter == NULL || pg == NULL || prop == NULL ||
1358 value == NULL || scope == NULL || dependency == NULL)
1359 goto done;
1362 * We passed in the FMRI for the default instance but for
1363 * some things we need the simple form so construct it. Since
1364 * we reuse the storage that dependency points to, we need to
1365 * use the servname early.
1367 (void) snprintf(dependency, maxlen, "%s", fmri + sizeof ("svc:"));
1368 servname = strrchr(dependency, ':');
1369 if (servname == NULL)
1370 goto done;
1371 *servname = '\0';
1372 servname = dependency;
1375 * Setup to iterate over the service property groups, only
1376 * looking at those that are "dependency" types. The "entity"
1377 * property will have the FMRI of the service we are dependent
1378 * on.
1380 if (scf_handle_get_scope(handle, SCF_SCOPE_LOCAL, scope) != 0)
1381 goto done;
1383 if (scf_scope_get_service(scope, servname, service) != 0)
1384 goto done;
1386 if (scf_iter_service_pgs(iter, service) != 0)
1387 goto done;
1389 while (scf_iter_next_pg(iter, pg) > 0) {
1390 char *services[2];
1392 * Have a property group for the service. See if it is
1393 * a dependency pg and only do operations on those.
1395 if (scf_pg_get_type(pg, type, SCFTYPE_LEN) <= 0)
1396 continue;
1398 if (strncmp(type, SCF_GROUP_DEPENDENCY, SCFTYPE_LEN) != 0)
1399 continue;
1401 * Have a dependency. Attempt to enable it.
1403 if (scf_pg_get_property(pg, SCF_PROPERTY_ENTITIES, prop) != 0)
1404 continue;
1406 if (scf_property_get_value(prop, value) != 0)
1407 continue;
1409 services[1] = NULL;
1411 if (scf_value_get_as_string(value, dependency, maxlen) > 0) {
1412 services[0] = dependency;
1413 _check_services(services);
1417 done:
1418 if (dependency != NULL)
1419 free(dependency);
1420 if (value != NULL)
1421 scf_value_destroy(value);
1422 if (prop != NULL)
1423 scf_property_destroy(prop);
1424 if (pg != NULL)
1425 scf_pg_destroy(pg);
1426 if (iter != NULL)
1427 scf_iter_destroy(iter);
1428 if (scope != NULL)
1429 scf_scope_destroy(scope);
1430 if (inst != NULL)
1431 scf_instance_destroy(inst);
1432 if (service != NULL)
1433 scf_service_destroy(service);
1435 (void) scf_handle_unbind(handle);
1436 scf_handle_destroy(handle);
1440 * How long to wait for service to come online
1442 #define WAIT_FOR_SERVICE 15
1445 * smb_enable_service()
1448 static int
1449 smb_enable_service(void)
1451 int i;
1452 int ret = SA_OK;
1453 char *service[] = { SMBD_DEFAULT_INSTANCE_FMRI, NULL };
1455 if (!smb_isonline()) {
1457 * Attempt to start the idmap, and other dependent
1458 * services, first. If it fails, the SMB service will
1459 * ultimately fail so we use that as the error. If we
1460 * don't try to enable idmap, smb won't start the
1461 * first time unless the admin has done it
1462 * manually. The service could be administratively
1463 * disabled so we won't always get started.
1465 smb_enable_dependencies(SMBD_DEFAULT_INSTANCE_FMRI);
1466 _check_services(service);
1468 /* Wait for service to come online */
1469 for (i = 0; i < WAIT_FOR_SERVICE; i++) {
1470 if (smb_isonline()) {
1471 ret = SA_OK;
1472 break;
1473 } else if (smb_ismaint()) {
1474 /* maintenance requires help */
1475 ret = SA_SYSTEM_ERR;
1476 break;
1477 } else {
1478 /* try another time */
1479 ret = SA_BUSY;
1480 (void) sleep(1);
1484 return (ret);
1488 * smb_validate_proto_prop(index, name, value)
1490 * Verify that the property specified by name can take the new
1491 * value. This is a sanity check to prevent bad values getting into
1492 * the default files.
1494 static int
1495 smb_validate_proto_prop(int index, char *name, char *value)
1497 if ((name == NULL) || (index < 0))
1498 return (SA_BAD_VALUE);
1500 if (smb_proto_options[index].validator == NULL)
1501 return (SA_OK);
1503 if (smb_proto_options[index].validator(index, value) == SA_OK)
1504 return (SA_OK);
1505 return (SA_BAD_VALUE);
1509 * smb_set_proto_prop(prop)
1511 * check that prop is valid.
1513 /*ARGSUSED*/
1514 static int
1515 smb_set_proto_prop(sa_property_t prop)
1517 int ret = SA_OK;
1518 char *name;
1519 char *value;
1520 int index = -1;
1521 struct smb_proto_option_defs *opt;
1523 name = sa_get_property_attr(prop, "type");
1524 value = sa_get_property_attr(prop, "value");
1525 if (name != NULL && value != NULL) {
1526 index = findprotoopt(name);
1527 if (index >= 0) {
1528 /* should test for valid value */
1529 ret = smb_validate_proto_prop(index, name, value);
1530 if (ret == SA_OK) {
1531 opt = &smb_proto_options[index];
1533 /* Save to SMF */
1534 (void) smb_config_set(opt->smb_index, value);
1536 * Specialized refresh mechanisms can
1537 * be flagged in the proto_options and
1538 * processed here.
1540 if (opt->refresh & SMB_REFRESH_REFRESH)
1541 (void) smf_refresh_instance(
1542 SMBD_DEFAULT_INSTANCE_FMRI);
1543 else if (opt->refresh & SMB_REFRESH_RESTART)
1544 (void) smf_restart_instance(
1545 SMBD_DEFAULT_INSTANCE_FMRI);
1550 if (name != NULL)
1551 sa_free_attr_string(name);
1552 if (value != NULL)
1553 sa_free_attr_string(value);
1555 return (ret);
1559 * smb_get_status()
1561 * What is the current status of the smbd? We use the SMF state here.
1562 * Caller must free the returned value.
1565 static char *
1566 smb_get_status(void)
1568 return (smf_get_state(SMBD_DEFAULT_INSTANCE_FMRI));
1572 * This protocol plugin require resource names
1574 static uint64_t
1575 smb_share_features(void)
1577 return (SA_FEATURE_RESOURCE | SA_FEATURE_ALLOWSUBDIRS |
1578 SA_FEATURE_ALLOWPARDIRS | SA_FEATURE_SERVER);
1582 * This should be used to convert smb_share_t to sa_resource_t
1583 * Should only be needed to build transient shares/resources to be
1584 * supplied to sharemgr to display.
1586 static int
1587 smb_add_transient(sa_handle_t handle, smb_share_t *si)
1589 int err;
1590 sa_share_t share;
1591 sa_group_t group;
1592 sa_resource_t resource;
1593 nvlist_t *nvl;
1594 char *opt;
1596 if (si == NULL)
1597 return (SA_INVALID_NAME);
1599 if ((share = sa_find_share(handle, si->shr_path)) == NULL) {
1600 if ((group = smb_get_defaultgrp(handle)) == NULL)
1601 return (SA_NO_SUCH_GROUP);
1603 share = sa_get_share(group, si->shr_path);
1604 if (share == NULL) {
1605 share = sa_add_share(group, si->shr_path,
1606 SA_SHARE_TRANSIENT, &err);
1607 if (share == NULL)
1608 return (SA_NO_SUCH_PATH);
1613 * Now handle the resource. Make sure that the resource is
1614 * transient and added to the share.
1616 resource = sa_get_share_resource(share, si->shr_name);
1617 if (resource == NULL) {
1618 resource = sa_add_resource(share,
1619 si->shr_name, SA_SHARE_TRANSIENT, &err);
1620 if (resource == NULL)
1621 return (SA_NO_SUCH_RESOURCE);
1624 if (si->shr_cmnt[0] != '\0')
1625 (void) sa_set_resource_description(resource, si->shr_cmnt);
1627 if (si->shr_container[0] != '\0')
1628 (void) sa_set_resource_attr(resource, SHOPT_AD_CONTAINER,
1629 si->shr_container);
1631 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
1632 return (SA_NO_MEMORY);
1634 if ((opt = smb_csc_name(si)) != NULL)
1635 err |= nvlist_add_string(nvl, SHOPT_CSC, opt);
1637 opt = (si->shr_flags & SMB_SHRF_ABE) ? "true" : "false";
1638 err |= nvlist_add_string(nvl, SHOPT_ABE, opt);
1640 if ((si->shr_flags & SMB_SHRF_AUTOHOME) == 0) {
1641 opt = (si->shr_flags & SMB_SHRF_GUEST_OK) ? "true" : "false";
1642 err |= nvlist_add_string(nvl, SHOPT_GUEST, opt);
1645 if (si->shr_access_ro[0] != '\0')
1646 err |= nvlist_add_string(nvl, SHOPT_RO, si->shr_access_ro);
1648 if (si->shr_access_rw[0] != '\0')
1649 err |= nvlist_add_string(nvl, SHOPT_RW, si->shr_access_rw);
1651 if (si->shr_access_none[0] != '\0')
1652 err |= nvlist_add_string(nvl, SHOPT_NONE, si->shr_access_none);
1654 if (err) {
1655 nvlist_free(nvl);
1656 return (SA_CONFIG_ERR);
1659 err = smb_update_optionset_props(handle, resource, nvl);
1661 nvlist_free(nvl);
1662 return (err);
1666 * Return smb transient shares.
1668 static int
1669 smb_list_transient(sa_handle_t handle)
1671 int i, offset;
1672 smb_shrlist_t list;
1673 int res;
1675 if (smb_share_count() <= 0)
1676 return (SA_OK);
1678 offset = 0;
1679 while (smb_share_list(offset, &list) == NERR_Success) {
1680 if (list.sl_cnt == 0)
1681 break;
1683 for (i = 0; i < list.sl_cnt; i++) {
1684 res = smb_add_transient(handle, &(list.sl_shares[i]));
1685 if (res != SA_OK)
1686 return (res);
1688 offset += list.sl_cnt;
1691 return (SA_OK);
1695 * fix_resource_name(share, name, prefix)
1697 * Construct a name where the ZFS dataset has the prefix replaced with "name".
1699 static char *
1700 fix_resource_name(sa_share_t share, char *name, char *prefix)
1702 char buf[SA_MAX_RESOURCE_NAME + 1];
1703 char *dataset;
1704 size_t bufsz = SA_MAX_RESOURCE_NAME + 1;
1705 size_t prelen;
1707 if (prefix == NULL)
1708 return (strdup(name));
1710 dataset = sa_get_share_attr(share, "dataset");
1711 if (dataset == NULL)
1712 return (strdup(name));
1714 (void) strlcpy(buf, name, bufsz);
1715 prelen = strlen(prefix);
1717 if (strncmp(dataset, prefix, prelen) == 0)
1718 (void) strlcat(buf, dataset + prelen, bufsz);
1720 sa_free_attr_string(dataset);
1721 sa_fix_resource_name(buf);
1722 return (strdup(buf));
1726 * smb_parse_optstring(group, options)
1728 * parse a compact option string into individual options. This allows
1729 * ZFS sharesmb and sharemgr "share" command to work. group can be a
1730 * group, a share or a resource.
1732 static int
1733 smb_parse_optstring(sa_group_t group, char *options)
1735 char *dup;
1736 char *base;
1737 char *lasts;
1738 char *token;
1739 sa_optionset_t optionset;
1740 sa_group_t parent = NULL;
1741 sa_resource_t resource = NULL;
1742 int iszfs = 0;
1743 int persist = 0;
1744 int need_optionset = 0;
1745 int ret = SA_OK;
1746 sa_property_t prop;
1749 * In order to not attempt to change ZFS properties unless
1750 * absolutely necessary, we never do it in the legacy parsing
1751 * so we need to keep track of this.
1753 if (sa_is_share(group)) {
1754 char *zfs;
1756 parent = sa_get_parent_group(group);
1757 if (parent != NULL) {
1758 zfs = sa_get_group_attr(parent, "zfs");
1759 if (zfs != NULL) {
1760 sa_free_attr_string(zfs);
1761 iszfs = 1;
1764 } else {
1765 iszfs = sa_group_is_zfs(group);
1767 * If a ZFS group, then we need to see if a resource
1768 * name is being set. If so, bail with
1769 * SA_PROP_SHARE_ONLY, so we come back in with a share
1770 * instead of a group.
1772 if (iszfs ||
1773 strncmp(options, "name=", sizeof ("name=") - 1) == 0 ||
1774 strstr(options, ",name=") != NULL) {
1775 return (SA_PROP_SHARE_ONLY);
1779 /* do we have an existing optionset? */
1780 optionset = sa_get_optionset(group, "smb");
1781 if (optionset == NULL) {
1782 /* didn't find existing optionset so create one */
1783 optionset = sa_create_optionset(group, "smb");
1784 if (optionset == NULL)
1785 return (SA_NO_MEMORY);
1786 } else {
1788 * If an optionset already exists, we've come through
1789 * twice so ignore the second time.
1791 return (ret);
1794 /* We need a copy of options for the next part. */
1795 dup = strdup(options);
1796 if (dup == NULL)
1797 return (SA_NO_MEMORY);
1800 * SMB properties are straightforward and are strings,
1801 * integers or booleans. Properties are separated by
1802 * commas. It will be necessary to parse quotes due to some
1803 * strings not having a restricted characters set.
1805 * Note that names will create a resource. For now, if there
1806 * is a set of properties "before" the first name="", those
1807 * properties will be placed on the group.
1809 persist = sa_is_persistent(group);
1810 base = dup;
1811 token = dup;
1812 lasts = NULL;
1813 while (token != NULL && ret == SA_OK) {
1814 ret = SA_OK;
1815 token = strtok_r(base, ",", &lasts);
1816 base = NULL;
1817 if (token != NULL) {
1818 char *value;
1820 * All SMB properties have values so there
1821 * MUST be an '=' character. If it doesn't,
1822 * it is a syntax error.
1824 value = strchr(token, '=');
1825 if (value != NULL) {
1826 *value++ = '\0';
1827 } else {
1828 ret = SA_SYNTAX_ERR;
1829 break;
1832 * We may need to handle a "name" property
1833 * that is a ZFS imposed resource name. Each
1834 * name would trigger getting a new "resource"
1835 * to put properties on. For now, assume no
1836 * "name" property for special handling.
1839 if (strcmp(token, SHOPT_NAME) == 0) {
1840 char *prefix;
1841 char *name = NULL;
1843 * We have a name, so now work on the
1844 * resource level. We have a "share"
1845 * in "group" due to the caller having
1846 * added it. If we are called with a
1847 * group, the check for group/share
1848 * at the beginning of this function
1849 * will bail out the parse if there is a
1850 * "name" but no share.
1852 if (!iszfs) {
1853 ret = SA_SYNTAX_ERR;
1854 break;
1857 * Make sure the parent group has the
1858 * "prefix" property since we will
1859 * need to use this for constructing
1860 * inherited name= values.
1862 prefix = sa_get_group_attr(parent, "prefix");
1863 if (prefix == NULL) {
1864 prefix = sa_get_group_attr(parent,
1865 "name");
1866 if (prefix != NULL) {
1867 (void) sa_set_group_attr(parent,
1868 "prefix", prefix);
1871 name = fix_resource_name((sa_share_t)group,
1872 value, prefix);
1873 if (name != NULL) {
1874 resource = sa_add_resource(
1875 (sa_share_t)group, name,
1876 SA_SHARE_TRANSIENT, &ret);
1877 sa_free_attr_string(name);
1878 } else {
1879 ret = SA_NO_MEMORY;
1881 if (prefix != NULL)
1882 sa_free_attr_string(prefix);
1884 /* A resource level optionset is needed */
1886 need_optionset = 1;
1887 if (resource == NULL) {
1888 ret = SA_NO_MEMORY;
1889 break;
1891 continue;
1894 if (iszfs && strcmp(token, SHOPT_DESCRIPTION) == 0) {
1895 if (resource == NULL)
1896 (void) sa_set_share_description(
1897 (sa_share_t)group, value);
1898 else
1899 (void) sa_set_resource_description(
1900 resource, value);
1901 continue;
1904 if (need_optionset) {
1905 optionset = sa_create_optionset(resource,
1906 "smb");
1907 need_optionset = 0;
1910 prop = sa_create_property(token, value);
1911 if (prop == NULL)
1912 ret = SA_NO_MEMORY;
1913 else
1914 ret = sa_add_property(optionset, prop);
1915 if (ret != SA_OK)
1916 break;
1917 if (!iszfs)
1918 ret = sa_commit_properties(optionset, !persist);
1921 free(dup);
1922 return (ret);
1926 * smb_sprint_option(rbuff, rbuffsize, incr, prop, sep)
1928 * provides a mechanism to format SMB properties into legacy output
1929 * format. If the buffer would overflow, it is reallocated and grown
1930 * as appropriate. Special cases of converting internal form of values
1931 * to those used by "share" are done. this function does one property
1932 * at a time.
1935 static void
1936 smb_sprint_option(char **rbuff, size_t *rbuffsize, size_t incr,
1937 sa_property_t prop, int sep)
1939 char *name;
1940 char *value;
1941 int curlen;
1942 char *buff = *rbuff;
1943 size_t buffsize = *rbuffsize;
1945 name = sa_get_property_attr(prop, "type");
1946 value = sa_get_property_attr(prop, "value");
1947 if (buff != NULL)
1948 curlen = strlen(buff);
1949 else
1950 curlen = 0;
1951 if (name != NULL) {
1952 int len;
1953 len = strlen(name) + sep;
1956 * A future RFE would be to replace this with more
1957 * generic code and to possibly handle more types.
1959 * For now, everything else is treated as a string. If
1960 * we get any properties that aren't exactly
1961 * name/value pairs, we may need to
1962 * interpret/transform.
1964 if (value != NULL)
1965 len += 1 + strlen(value);
1967 while (buffsize <= (curlen + len)) {
1968 /* need more room */
1969 buffsize += incr;
1970 buff = realloc(buff, buffsize);
1971 *rbuff = buff;
1972 *rbuffsize = buffsize;
1973 if (buff == NULL) {
1974 /* realloc failed so free everything */
1975 if (*rbuff != NULL)
1976 free(*rbuff);
1977 goto err;
1980 if (buff == NULL)
1981 goto err;
1982 (void) snprintf(buff + curlen, buffsize - curlen,
1983 "%s%s=%s", sep ? "," : "",
1984 name, value != NULL ? value : "\"\"");
1987 err:
1988 if (name != NULL)
1989 sa_free_attr_string(name);
1990 if (value != NULL)
1991 sa_free_attr_string(value);
1995 * smb_format_resource_options(resource, hier)
1997 * format all the options on the group into a flattened option
1998 * string. If hier is non-zero, walk up the tree to get inherited
1999 * options.
2002 static char *
2003 smb_format_options(sa_group_t group, int hier)
2005 sa_optionset_t options = NULL;
2006 sa_property_t prop;
2007 int sep = 0;
2008 char *buff;
2009 size_t buffsize;
2012 buff = malloc(OPT_CHUNK);
2013 if (buff == NULL)
2014 return (NULL);
2016 buff[0] = '\0';
2017 buffsize = OPT_CHUNK;
2020 * We may have a an optionset relative to this item. format
2021 * these if we find them and then add any security definitions.
2024 options = sa_get_derived_optionset(group, "smb", hier);
2027 * do the default set first but skip any option that is also
2028 * in the protocol specific optionset.
2030 if (options != NULL) {
2031 for (prop = sa_get_property(options, NULL);
2032 prop != NULL; prop = sa_get_next_property(prop)) {
2034 * use this one since we skipped any
2035 * of these that were also in
2036 * optdefault
2038 smb_sprint_option(&buff, &buffsize, OPT_CHUNK,
2039 prop, sep);
2040 if (buff == NULL) {
2042 * buff could become NULL if there
2043 * isn't enough memory for
2044 * smb_sprint_option to realloc()
2045 * as necessary. We can't really
2046 * do anything about it at this
2047 * point so we return NULL. The
2048 * caller should handle the
2049 * failure.
2051 if (options != NULL)
2052 sa_free_derived_optionset(
2053 options);
2054 return (buff);
2056 sep = 1;
2060 if (options != NULL)
2061 sa_free_derived_optionset(options);
2062 return (buff);
2066 * smb_rename_resource(resource, newname)
2068 * Change the current exported name of the resource to newname.
2070 /*ARGSUSED*/
2072 smb_rename_resource(sa_handle_t handle, sa_resource_t resource, char *newname)
2074 int ret = SA_OK;
2075 int err;
2076 char *oldname;
2078 if (!smb_isonline())
2079 return (SA_OK);
2081 oldname = sa_get_resource_attr(resource, "name");
2082 if (oldname == NULL)
2083 return (SA_NO_SUCH_RESOURCE);
2085 err = smb_share_rename(oldname, newname);
2087 sa_free_attr_string(oldname);
2089 /* improve error values somewhat */
2090 switch (err) {
2091 case NERR_Success:
2092 break;
2093 case NERR_InternalError:
2094 ret = SA_SYSTEM_ERR;
2095 break;
2096 case NERR_DuplicateShare:
2097 ret = SA_DUPLICATE_NAME;
2098 break;
2099 default:
2100 ret = SA_CONFIG_ERR;
2101 break;
2104 return (ret);
2107 static int
2108 smb_build_shareinfo(sa_share_t share, sa_resource_t resource, smb_share_t *si)
2110 sa_optionset_t opts;
2111 char *path;
2112 char *rname;
2113 char *val = NULL;
2114 char csc_value[SMB_CSC_BUFSZ];
2116 bzero(si, sizeof (smb_share_t));
2118 if ((path = sa_get_share_attr(share, "path")) == NULL)
2119 return (SA_NO_SUCH_PATH);
2121 if ((rname = sa_get_resource_attr(resource, "name")) == NULL) {
2122 sa_free_attr_string(path);
2123 return (SA_NO_SUCH_RESOURCE);
2126 (void) strlcpy(si->shr_path, path, sizeof (si->shr_path));
2127 (void) strlcpy(si->shr_name, rname, sizeof (si->shr_name));
2128 sa_free_attr_string(path);
2129 sa_free_attr_string(rname);
2131 val = sa_get_resource_description(resource);
2132 if (val == NULL)
2133 val = sa_get_share_description(share);
2135 if (val != NULL) {
2136 (void) strlcpy(si->shr_cmnt, val, sizeof (si->shr_cmnt));
2137 sa_free_share_description(val);
2140 si->shr_flags = (sa_is_persistent(share))
2141 ? SMB_SHRF_PERM : SMB_SHRF_TRANS;
2143 opts = sa_get_derived_optionset(resource, SMB_PROTOCOL_NAME, 1);
2144 if (opts == NULL)
2145 return (SA_OK);
2147 if (smb_saprop_getbool(opts, SHOPT_CATIA))
2148 si->shr_flags |= SMB_SHRF_CATIA;
2150 if (smb_saprop_getbool(opts, SHOPT_ABE))
2151 si->shr_flags |= SMB_SHRF_ABE;
2153 if (smb_saprop_getbool(opts, SHOPT_GUEST))
2154 si->shr_flags |= SMB_SHRF_GUEST_OK;
2156 if (smb_saprop_getbool(opts, SHOPT_DFSROOT))
2157 si->shr_flags |= SMB_SHRF_DFSROOT;
2159 (void) smb_saprop_getstr(opts, SHOPT_AD_CONTAINER, si->shr_container,
2160 sizeof (si->shr_container));
2162 if (smb_saprop_getstr(opts, SHOPT_CSC, csc_value, sizeof (csc_value)))
2163 smb_csc_option(csc_value, si);
2165 if (smb_saprop_getstr(opts, SHOPT_RO, si->shr_access_ro,
2166 sizeof (si->shr_access_ro)))
2167 si->shr_flags |= SMB_SHRF_ACC_RO;
2169 if (smb_saprop_getstr(opts, SHOPT_RW, si->shr_access_rw,
2170 sizeof (si->shr_access_rw)))
2171 si->shr_flags |= SMB_SHRF_ACC_RW;
2173 if (smb_saprop_getstr(opts, SHOPT_NONE, si->shr_access_none,
2174 sizeof (si->shr_access_none)))
2175 si->shr_flags |= SMB_SHRF_ACC_NONE;
2177 sa_free_derived_optionset(opts);
2178 return (SA_OK);
2182 * Map a client-side caching (CSC) option to the appropriate share
2183 * flag. Only one option is allowed; an error will be logged if
2184 * multiple options have been specified. We don't need to do anything
2185 * about multiple values here because the SRVSVC will not recognize
2186 * a value containing multiple flags and will return the default value.
2188 * If the option value is not recognized, it will be ignored: invalid
2189 * values will typically be caught and rejected by sharemgr.
2191 static void
2192 smb_csc_option(const char *value, smb_share_t *si)
2194 char buf[SMB_CSC_BUFSZ];
2195 int i;
2197 for (i = 0; i < (sizeof (cscopt) / sizeof (cscopt[0])); ++i) {
2198 if (strcasecmp(value, cscopt[i].value) == 0) {
2199 si->shr_flags |= cscopt[i].flag;
2200 break;
2204 switch (si->shr_flags & SMB_SHRF_CSC_MASK) {
2205 case 0:
2206 case SMB_SHRF_CSC_DISABLED:
2207 case SMB_SHRF_CSC_MANUAL:
2208 case SMB_SHRF_CSC_AUTO:
2209 case SMB_SHRF_CSC_VDO:
2210 break;
2212 default:
2213 buf[0] = '\0';
2215 for (i = 0; i < (sizeof (cscopt) / sizeof (cscopt[0])); ++i) {
2216 if (si->shr_flags & cscopt[i].flag) {
2217 (void) strlcat(buf, " ", SMB_CSC_BUFSZ);
2218 (void) strlcat(buf, cscopt[i].value,
2219 SMB_CSC_BUFSZ);
2223 syslog(LOG_ERR, "csc option conflict:%s", buf);
2224 break;
2229 * Return the option name for the first CSC flag (there should be only
2230 * one) encountered in the share flags.
2232 static char *
2233 smb_csc_name(const smb_share_t *si)
2235 int i;
2237 for (i = 0; i < (sizeof (cscopt) / sizeof (cscopt[0])); ++i) {
2238 if (si->shr_flags & cscopt[i].flag)
2239 return (cscopt[i].value);
2242 return (NULL);
2246 * smb_get_defaultgrp
2248 * If default group for CIFS shares (i.e. "smb") exists
2249 * then it will return the group handle, otherwise it will
2250 * create the group and return the handle.
2252 * All the shares created by CIFS clients (this is only possible
2253 * via RPC) will be added to "smb" groups.
2255 static sa_group_t
2256 smb_get_defaultgrp(sa_handle_t handle)
2258 sa_group_t group = NULL;
2259 int err;
2261 group = sa_get_group(handle, SMB_DEFAULT_SHARE_GROUP);
2262 if (group != NULL)
2263 return (group);
2265 group = sa_create_group(handle, SMB_DEFAULT_SHARE_GROUP, &err);
2266 if (group == NULL)
2267 return (NULL);
2269 if (sa_create_optionset(group, SMB_DEFAULT_SHARE_GROUP) == NULL) {
2270 (void) sa_remove_group(group);
2271 group = NULL;
2274 return (group);
2278 * Checks to see if the command args are the supported substitution specifier.
2279 * i.e. <cmd> %U %S
2281 static int
2282 cmd_validator(int index, char *value)
2284 char cmd[MAXPATHLEN];
2285 char *ptr, *v;
2286 boolean_t skip_cmdname;
2288 if (string_length_check_validator(index, value) != SA_OK)
2289 return (SA_BAD_VALUE);
2291 if (*value == '\0')
2292 return (SA_OK);
2294 (void) strlcpy(cmd, value, sizeof (cmd));
2296 ptr = cmd;
2297 skip_cmdname = B_TRUE;
2298 do {
2299 if ((v = strsep(&ptr, " ")) == NULL)
2300 break;
2302 if (*v != '\0') {
2304 if (skip_cmdname) {
2305 skip_cmdname = B_FALSE;
2306 continue;
2309 if ((strlen(v) != 2) || *v != '%')
2310 return (SA_BAD_VALUE);
2312 if (strpbrk(v, SMB_VALID_SUB_CHRS) == NULL)
2313 return (SA_BAD_VALUE);
2316 } while (v != NULL);
2319 * If skip_cmdname is still true then the string contains
2320 * only spaces. Don't allow such a string.
2322 if (skip_cmdname)
2323 return (SA_BAD_VALUE);
2325 return (SA_OK);
2328 /*ARGSUSED*/
2329 static int
2330 disposition_validator(int index, char *value)
2332 if (value == NULL)
2333 return (SA_BAD_VALUE);
2335 if (*value == '\0')
2336 return (SA_OK);
2338 if ((strcasecmp(value, SMB_EXEC_DISP_CONTINUE) == 0) ||
2339 (strcasecmp(value, SMB_EXEC_DISP_TERMINATE) == 0))
2340 return (SA_OK);
2342 return (SA_BAD_VALUE);
2346 * Updates the optionset properties of the share resource.
2347 * The properties are given as a list of name-value pair.
2348 * The name argument should be the optionset property name and the value
2349 * should be a valid value for the specified property.
2351 * When calling this function for permanent shares, the caller must also
2352 * call sa_commit_properties() to commit the changes to SMF.
2354 static int
2355 smb_update_optionset_props(sa_handle_t handle, sa_resource_t resource,
2356 nvlist_t *nvl)
2358 sa_property_t prop;
2359 sa_optionset_t opts;
2360 int err = SA_OK;
2361 nvpair_t *cur;
2362 char *name, *val;
2364 if ((opts = sa_get_optionset(resource, SMB_PROTOCOL_NAME)) == NULL) {
2365 opts = sa_create_optionset(resource, SMB_PROTOCOL_NAME);
2366 if (opts == NULL)
2367 return (SA_CONFIG_ERR);
2370 cur = nvlist_next_nvpair(nvl, NULL);
2371 while (cur != NULL) {
2372 name = nvpair_name(cur);
2373 err = nvpair_value_string(cur, &val);
2374 if ((err != 0) || (name == NULL) || (val == NULL)) {
2375 err = SA_CONFIG_ERR;
2376 break;
2379 prop = NULL;
2380 if ((prop = sa_get_property(opts, name)) == NULL) {
2381 prop = sa_create_property(name, val);
2382 if (prop != NULL) {
2383 err = sa_valid_property(handle, opts,
2384 SMB_PROTOCOL_NAME, prop);
2385 if (err != SA_OK) {
2386 (void) sa_remove_property(prop);
2387 break;
2390 err = sa_add_property(opts, prop);
2391 if (err != SA_OK)
2392 break;
2393 } else {
2394 err = sa_update_property(prop, val);
2395 if (err != SA_OK)
2396 break;
2399 cur = nvlist_next_nvpair(nvl, cur);
2402 return (err);
2405 static boolean_t
2406 smb_saprop_getbool(sa_optionset_t opts, char *propname)
2408 sa_property_t prop;
2409 char *val;
2410 boolean_t propval = B_FALSE;
2412 prop = sa_get_property(opts, propname);
2413 if ((val = sa_get_property_attr(prop, "value")) != NULL) {
2414 if ((strcasecmp(val, "true") == 0) || (strcmp(val, "1") == 0))
2415 propval = B_TRUE;
2416 free(val);
2419 return (propval);
2422 static boolean_t
2423 smb_saprop_getstr(sa_optionset_t opts, char *propname, char *buf, size_t bufsz)
2425 sa_property_t prop;
2426 char *val;
2428 prop = sa_get_property(opts, propname);
2429 if ((val = sa_get_property_attr(prop, "value")) != NULL) {
2430 (void) strlcpy(buf, val, bufsz);
2431 free(val);
2432 return (B_TRUE);
2435 return (B_FALSE);