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]
23 * Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright 2014 Nexenta Systems, Inc. All rights reserved.
25 * Copyright (c) 2016 by Delphix. All rights reserved.
29 * core library for common functions across all config store types
30 * and file systems to be exported. This includes legacy dfstab/sharetab
31 * parsing. Need to eliminate XML where possible.
40 #include <sys/types.h>
42 #include <libxml/parser.h>
43 #include <libxml/tree.h>
45 #include "libshare_impl.h"
50 #include <sys/param.h>
55 #include <sharefs/share.h>
58 #define DFSTAB_NOTICE_LINES 5
59 static char *notice
[DFSTAB_NOTICE_LINES
] = {
60 "# Do not modify this file directly.\n",
61 "# Use the sharemgr(1m) command for all share management\n",
62 "# This file is reconstructed and only maintained for backward\n",
63 "# compatibility. Configuration lines could be lost.\n",
67 #define STRNCAT(x, y, z) (xmlChar *)strncat((char *)x, (char *)y, z)
69 /* will be much smaller, but this handles bad syntax in the file */
70 #define MAXARGSFORSHARE 256
72 static mutex_t sharetab_lock
= DEFAULTMUTEX
;
73 extern mutex_t sa_dfstab_lock
;
75 /* used internally only */
78 struct sharelist
*next
;
89 static void parse_dfstab(sa_handle_t
, char *, xmlNodePtr
);
90 extern char *_sa_get_token(char *);
91 static void dfs_free_list(xfs_sharelist_t
*);
93 void getlegacyconfig(sa_handle_t
, char *, xmlNodePtr
*);
94 extern sa_share_t
_sa_add_share(sa_group_t
, char *, int, int *, uint64_t);
95 extern sa_group_t
_sa_create_group(sa_handle_impl_t
, char *);
96 static void outdfstab(FILE *, xfs_sharelist_t
*);
97 extern int _sa_remove_optionset(sa_optionset_t
);
98 extern int set_node_share(void *, char *, char *);
99 extern void set_node_attr(void *, char *, char *);
104 * block important signals for a critical region. Arg is a pointer to
105 * a sigset_t that is used later for the unblock.
108 sablocksigs(sigset_t
*sigs
)
113 (void) sigprocmask(SIG_BLOCK
, NULL
, &new);
114 (void) sigaddset(&new, SIGHUP
);
115 (void) sigaddset(&new, SIGINT
);
116 (void) sigaddset(&new, SIGQUIT
);
117 (void) sigaddset(&new, SIGTSTP
);
118 (void) sigprocmask(SIG_SETMASK
, &new, sigs
);
123 * saunblocksigs(*sigs)
125 * unblock previously blocked signals from the sigs arg.
128 saunblocksigs(sigset_t
*sigs
)
131 (void) sigprocmask(SIG_SETMASK
, sigs
, NULL
);
137 * allocator function to return an zfs_sharelist_t
140 static xfs_sharelist_t
*
143 xfs_sharelist_t
*item
;
145 item
= (xfs_sharelist_t
*)malloc(sizeof (xfs_sharelist_t
));
147 (void) memset(item
, '\0', sizeof (xfs_sharelist_t
));
154 * Look at the beginning of the current /etc/dfs/dfstab file and add
155 * the do not modify notice if it doesn't exist.
158 static xfs_sharelist_t
*
159 fix_notice(xfs_sharelist_t
*list
)
161 xfs_sharelist_t
*item
, *prev
;
165 /* zero length dfstab */
166 list
= alloc_sharelist();
169 list
->description
= strdup("#\n");
171 if (list
->path
== NULL
&& list
->description
!= NULL
&&
172 strcmp(list
->description
, notice
[0]) != 0) {
173 for (prev
= NULL
, i
= 0; i
< DFSTAB_NOTICE_LINES
; i
++) {
174 item
= alloc_sharelist();
176 item
->description
= strdup(notice
[i
]);
182 item
->next
= prev
->next
;
195 * Returns an zfs_sharelist_t list of lines from the dfstab file
196 * pointed to by the FILE pointer dfs. Each entry is parsed and the
197 * original line is also preserved. Used in parsing and updating the
201 static xfs_sharelist_t
*
204 char buff
[_POSIX_ARG_MAX
]; /* reasonable size given syntax of share */
207 char *args
[MAXARGSFORSHARE
];
211 xfs_sharelist_t
*item
= NULL
, *first
= NULL
, *last
;
216 while (fgets(buff
, sizeof (buff
), dfs
) != NULL
) {
219 if (buff
[0] == '#') {
220 item
= alloc_sharelist();
222 /* if no path, then comment */
224 item
->description
= strdup(buff
);
236 } else if (buff
[0] == '\n') {
240 item
= alloc_sharelist();
243 } else if (first
== NULL
) {
251 item
->origline
= strdup(buff
);
252 (void) _sa_get_token(NULL
); /* reset to new pointers */
254 while ((token
= _sa_get_token(bp
)) != NULL
) {
255 if (argc
< MAXARGSFORSHARE
)
256 args
[argc
++] = token
;
258 while ((c
= getopt(argc
, args
, "F:o:d:pg:")) != -1) {
264 item
->fstype
= strdup(optarg
);
267 item
->options
= strdup(optarg
);
270 item
->description
= strdup(optarg
);
273 item
->group
= strdup(optarg
);
280 item
->path
= strdup(args
[optind
]);
285 /* resource and/or groupname */
286 resource
= args
[optind
];
287 optgroup
= strchr(resource
, '@');
288 if (optgroup
!= NULL
)
290 if (optgroup
!= NULL
)
291 item
->group
= strdup(optgroup
);
292 if (resource
!= NULL
&&
293 strlen(resource
) > 0)
298 /* NFS is the default if none defined */
299 if (item
!= NULL
&& item
->fstype
== NULL
)
300 item
->fstype
= strdup("nfs");
303 first
= fix_notice(first
);
308 * finddfsentry(list, path)
310 * Look for path in the zfs_sharelist_t list and return the entry if it
314 static xfs_sharelist_t
*
315 finddfsentry(xfs_sharelist_t
*list
, char *path
)
317 xfs_sharelist_t
*item
;
319 for (item
= list
; item
!= NULL
; item
= item
->next
) {
320 if (item
->path
!= NULL
&& strcmp(item
->path
, path
) == 0)
327 * remdfsentry(list, path, proto)
329 * Remove the specified path (with protocol) from the list. This will
330 * remove it from dfstab when the file is rewritten.
333 static xfs_sharelist_t
*
334 remdfsentry(xfs_sharelist_t
*list
, char *path
, char *proto
)
336 xfs_sharelist_t
*item
, *prev
= NULL
;
339 for (item
= prev
= list
; item
!= NULL
; item
= item
->next
) {
340 /* skip comment entry but don't lose it */
341 if (item
->path
== NULL
) {
345 /* if proto is NULL, remove all protocols */
346 if (proto
== NULL
|| (strcmp(item
->path
, path
) == 0 &&
347 (item
->fstype
!= NULL
&& strcmp(item
->fstype
, proto
) == 0)))
349 if (item
->fstype
== NULL
&&
350 (proto
== NULL
|| strcmp(proto
, "nfs") == 0))
356 list
= item
->next
; /* this must be the first one */
358 prev
->next
= item
->next
;
366 * remdfsline(list, line)
368 * Remove the line specified from the list.
371 static xfs_sharelist_t
*
372 remdfsline(xfs_sharelist_t
*list
, char *line
)
374 xfs_sharelist_t
*item
, *prev
= NULL
;
376 for (item
= prev
= list
; item
!= NULL
; item
= item
->next
) {
377 /* skip comment entry but don't lose it */
378 if (item
->path
== NULL
) {
382 if (strcmp(item
->origline
, line
) == 0)
388 list
= item
->next
; /* this must be the first one */
390 prev
->next
= item
->next
;
398 * adddfsentry(list, share, proto)
400 * Add an entry to the dfstab list for share (relative to proto). This
401 * is used to update dfstab for legacy purposes.
404 static xfs_sharelist_t
*
405 adddfsentry(xfs_sharelist_t
*list
, sa_share_t share
, char *proto
)
407 xfs_sharelist_t
*item
, *tmp
;
411 item
= alloc_sharelist();
413 parent
= sa_get_parent_group(share
);
414 groupname
= sa_get_group_attr(parent
, "name");
415 if (groupname
!= NULL
&& strcmp(groupname
, "default") == 0) {
416 sa_free_attr_string(groupname
);
419 item
->path
= sa_get_share_attr(share
, "path");
420 item
->resource
= sa_get_share_attr(share
, "resource");
421 item
->group
= groupname
;
422 item
->fstype
= strdup(proto
);
423 item
->options
= sa_proto_legacy_format(proto
, share
, 1);
424 if (item
->options
!= NULL
&& strlen(item
->options
) == 0) {
426 item
->options
= NULL
;
428 item
->description
= sa_get_share_description(share
);
429 if (item
->description
!= NULL
&&
430 strlen(item
->description
) == 0) {
431 sa_free_share_description(item
->description
);
432 item
->description
= NULL
;
437 for (tmp
= list
; tmp
->next
!= NULL
; tmp
= tmp
->next
)
446 * outdfstab(dfstab, list)
448 * Output the list to dfstab making sure the file is truncated.
449 * Comments and errors are preserved.
453 outdfstab(FILE *dfstab
, xfs_sharelist_t
*list
)
455 xfs_sharelist_t
*item
;
457 (void) ftruncate(fileno(dfstab
), 0);
459 for (item
= list
; item
!= NULL
; item
= item
->next
) {
460 if (item
->path
!= NULL
) {
461 if (*item
->path
== '/') {
462 (void) fprintf(dfstab
,
463 "share %s%s%s%s%s%s%s %s%s%s%s%s\n",
464 (item
->fstype
!= NULL
) ? "-F " : "",
465 (item
->fstype
!= NULL
) ? item
->fstype
: "",
466 (item
->options
!= NULL
) ? " -o " : "",
467 (item
->options
!= NULL
) ?
469 (item
->description
!= NULL
) ?
471 (item
->description
!= NULL
) ?
472 item
->description
: "",
473 (item
->description
!= NULL
) ? "\"" : "",
475 ((item
->resource
!= NULL
) ||
476 (item
->group
!= NULL
)) ? " " : "",
477 (item
->resource
!= NULL
) ?
479 item
->group
!= NULL
? "@" : "",
480 item
->group
!= NULL
? item
->group
: "");
482 (void) fprintf(dfstab
, "%s", item
->origline
);
485 if (item
->description
!= NULL
)
486 (void) fprintf(dfstab
, "%s", item
->description
);
488 (void) fprintf(dfstab
, "%s", item
->origline
);
496 * Open the specified dfstab file. If the owner/group/perms are wrong,
501 open_dfstab(char *file
)
509 dfstab
= fopen(file
, "r+");
510 if (dfstab
== NULL
) {
511 dfstab
= fopen(file
, "w+");
513 if (dfstab
!= NULL
) {
514 grsize
= sysconf(_SC_GETGR_R_SIZE_MAX
);
515 buff
= malloc(grsize
);
517 grp
= getgrnam_r(SA_DEFAULT_FILE_GRP
, &group
, buff
,
520 grp
= getgrnam(SA_DEFAULT_FILE_GRP
);
521 (void) fchmod(fileno(dfstab
), 0644);
522 (void) fchown(fileno(dfstab
), 0,
523 grp
!= NULL
? grp
->gr_gid
: 3);
531 * sa_comment_line(line, err)
533 * Add a comment to the dfstab file with err as a prefix to the
538 sa_comment_line(char *line
, char *err
)
541 xfs_sharelist_t
*list
;
544 dfstab
= open_dfstab(SA_LEGACY_DFSTAB
);
545 if (dfstab
!= NULL
) {
546 (void) setvbuf(dfstab
, NULL
, _IOLBF
, BUFSIZ
* 8);
548 (void) lockf(fileno(dfstab
), F_LOCK
, 0);
549 (void) mutex_lock(&sa_dfstab_lock
);
550 list
= getdfstab(dfstab
);
553 * don't ignore the return since the list could have
554 * gone to NULL if the file only had one line in it.
556 list
= remdfsline(list
, line
);
557 outdfstab(dfstab
, list
);
558 (void) fprintf(dfstab
, "# Error: %s: %s", err
, line
);
559 (void) fsync(fileno(dfstab
));
560 (void) mutex_unlock(&sa_dfstab_lock
);
561 (void) lockf(fileno(dfstab
), F_ULOCK
, 0);
562 (void) fclose(dfstab
);
570 * sa_delete_legacy(share, protocol)
572 * Delete the specified share from the legacy config file.
576 sa_delete_legacy(sa_share_t share
, char *protocol
)
581 xfs_sharelist_t
*list
;
583 sa_optionset_t optionset
;
588 * Protect against shares that don't have paths. This is not
589 * really an error at this point.
591 path
= sa_get_share_attr(share
, "path");
595 dfstab
= open_dfstab(SA_LEGACY_DFSTAB
);
596 if (dfstab
!= NULL
) {
597 (void) setvbuf(dfstab
, NULL
, _IOLBF
, BUFSIZ
* 8);
599 parent
= sa_get_parent_group(share
);
600 if (parent
!= NULL
) {
601 (void) lockf(fileno(dfstab
), F_LOCK
, 0);
602 (void) mutex_lock(&sa_dfstab_lock
);
603 list
= getdfstab(dfstab
);
605 if (protocol
!= NULL
) {
607 list
= remdfsentry(list
, path
,
610 for (optionset
= sa_get_optionset(parent
, NULL
);
613 sa_get_next_optionset(optionset
)) {
614 char *proto
= sa_get_optionset_attr(
617 if (list
!= NULL
&& proto
!= NULL
)
618 list
= remdfsentry(list
, path
,
623 * may want to only do the dfstab if
624 * this call returns NOT IMPLEMENTED
625 * but it shouldn't hurt.
628 err
= sa_proto_delete_legacy(
630 if (err
!= SA_NOT_IMPLEMENTED
)
634 sa_free_attr_string(proto
);
637 outdfstab(dfstab
, list
);
640 (void) fflush(dfstab
);
641 (void) mutex_unlock(&sa_dfstab_lock
);
642 (void) lockf(fileno(dfstab
), F_ULOCK
, 0);
644 (void) fsync(fileno(dfstab
));
646 (void) fclose(dfstab
);
648 if (errno
== EACCES
|| errno
== EPERM
)
649 ret
= SA_NO_PERMISSION
;
655 sa_free_attr_string(path
);
661 * sa_update_legacy(share, proto)
663 * There is an assumption that dfstab will be the most common form of
664 * legacy configuration file for shares, but not the only one. Because
665 * of that, dfstab handling is done in the main code with calls to
666 * this function and protocol specific calls to deal with formatting
667 * options into dfstab/share compatible syntax. Since not everything
668 * will be dfstab, there is a provision for calling a protocol
669 * specific plugin interface that allows the protocol plugin to do its
670 * own legacy files and skip the dfstab update.
674 sa_update_legacy(sa_share_t share
, char *proto
)
678 xfs_sharelist_t
*list
;
684 ret
= sa_proto_update_legacy(proto
, share
);
685 if (ret
!= SA_NOT_IMPLEMENTED
)
688 features
= sa_proto_get_featureset(proto
);
689 if (!(features
& SA_FEATURE_DFSTAB
))
692 /* do the dfstab format */
693 persist
= sa_get_share_attr(share
, "type");
695 * only update if the share is not transient -- no share type
696 * set or the type is not "transient".
698 if (persist
== NULL
|| strcmp(persist
, "transient") != 0) {
699 path
= sa_get_share_attr(share
, "path");
704 dfstab
= open_dfstab(SA_LEGACY_DFSTAB
);
705 if (dfstab
!= NULL
) {
706 (void) setvbuf(dfstab
, NULL
, _IOLBF
, BUFSIZ
* 8);
708 (void) lockf(fileno(dfstab
), F_LOCK
, 0);
709 (void) mutex_lock(&sa_dfstab_lock
);
710 list
= getdfstab(dfstab
);
713 list
= remdfsentry(list
, path
, proto
);
714 list
= adddfsentry(list
, share
, proto
);
715 outdfstab(dfstab
, list
);
716 (void) fflush(dfstab
);
717 (void) mutex_unlock(&sa_dfstab_lock
);
718 (void) lockf(fileno(dfstab
), F_ULOCK
, 0);
719 (void) fsync(fileno(dfstab
));
721 (void) fclose(dfstab
);
725 if (errno
== EACCES
|| errno
== EPERM
)
726 ret
= SA_NO_PERMISSION
;
730 sa_free_attr_string(path
);
734 sa_free_attr_string(persist
);
739 * sa_is_security(optname, proto)
741 * Check to see if optname is a security (named optionset) specific
742 * property for the specified protocol.
746 sa_is_security(char *optname
, char *proto
)
750 ret
= sa_proto_security_prop(proto
, optname
);
755 * add_syntax_comment(root, line, err, todfstab)
757 * Add a comment to the document indicating a syntax error. If
758 * todfstab is set, write it back to the dfstab file as well.
762 add_syntax_comment(xmlNodePtr root
, char *line
, char *err
, int todfstab
)
766 node
= xmlNewChild(root
, NULL
, (xmlChar
*)"error", (xmlChar
*)line
);
768 (void) xmlSetProp(node
, (xmlChar
*)"type", (xmlChar
*)err
);
770 sa_comment_line(line
, err
);
774 * sa_is_share(object)
776 * returns true if the object is of type "share".
780 sa_is_share(void *object
)
782 if (object
!= NULL
) {
783 if (strcmp((char *)((xmlNodePtr
)object
)->name
, "share") == 0)
789 * sa_is_resource(object)
791 * returns true if the object is of type "resource".
795 sa_is_resource(void *object
)
797 if (object
!= NULL
) {
798 if (strcmp((char *)((xmlNodePtr
)object
)->name
, "resource") == 0)
805 * _sa_remove_property(property)
807 * remove a property only from the document.
811 _sa_remove_property(sa_property_t property
)
813 xmlUnlinkNode((xmlNodePtr
)property
);
814 xmlFreeNode((xmlNodePtr
)property
);
818 * _sa_create_dummy_share()
820 * Create a share entry suitable for parsing but not tied to any real
821 * config tree. Need to have a parent as well as the node to parse
822 * on. Free using _sa_free_dummy_share(share);
826 _sa_create_dummy_share()
828 xmlNodePtr parent_node
= NULL
;
829 xmlNodePtr child_node
= NULL
;
831 parent_node
= xmlNewNode(NULL
, (xmlChar
*)"group");
832 if (parent_node
!= NULL
) {
833 child_node
= xmlNewChild(parent_node
, NULL
, (xmlChar
*)"share",
835 if (child_node
!= NULL
) {
837 * Use a "zfs" tag since that will make sure nothing
838 * really attempts to put values into the
839 * repository. Also ZFS is currently the only user of
842 set_node_attr(parent_node
, "type", "transient");
843 set_node_attr(parent_node
, "zfs", "true");
844 set_node_attr(child_node
, "type", "transient");
845 set_node_attr(child_node
, "zfs", "true");
847 xmlFreeNode(parent_node
);
854 * _sa_free_dummy_share(share)
856 * Free the dummy share and its parent. It is an error to try and
857 * free something that isn't a dummy.
861 _sa_free_dummy_share(sa_share_t share
)
863 xmlNodePtr node
= (xmlNodePtr
)share
;
869 parent
= node
->parent
;
870 name
= (char *)xmlGetProp(node
, (xmlChar
*)"path");
872 /* Real shares always have a path but a dummy doesn't */
873 ret
= SA_NOT_ALLOWED
;
874 sa_free_attr_string(name
);
877 * If there is a parent, do the free on that since
878 * xmlFreeNode is a recursive function and free's an
881 if (parent
!= NULL
) {
893 * sa_parse_legacy_options(group, options, proto)
895 * In order to support legacy configurations, we allow the protocol
896 * specific plugin to parse legacy syntax options (like those in
897 * /etc/dfs/dfstab). This adds a new optionset to the group (or
900 * Once the optionset has been created, we then get the derived
901 * optionset of the parent (options from the optionset of the parent
902 * and any parent it might have) and remove those from the created
903 * optionset. This avoids duplication of options.
907 sa_parse_legacy_options(sa_group_t group
, char *options
, char *proto
)
909 int ret
= SA_INVALID_PROTOCOL
;
911 int using_dummy
= B_FALSE
;
913 sa_optionset_t optionset
;
914 sa_property_t popt
, prop
;
915 sa_optionset_t localoptions
;
918 * If "group" is NULL, this is just a parse without saving
919 * anything in either SMF or ZFS. Create a dummy group to
923 group
= (sa_group_t
)_sa_create_dummy_share();
924 using_dummy
= B_TRUE
;
927 parent
= sa_get_parent_group(group
);
930 ret
= sa_proto_legacy_opts(proto
, group
, options
);
933 /* Since this is a dummy parse, cleanup and quit here */
934 (void) _sa_free_dummy_share(parent
);
942 * If in a group, remove the inherited options and security
948 /* Find parent options to remove from child */
949 optionset
= sa_get_derived_optionset(parent
, proto
, 1);
950 localoptions
= sa_get_optionset(group
, proto
);
951 if (optionset
!= NULL
) {
952 for (popt
= sa_get_property(optionset
, NULL
);
954 popt
= sa_get_next_property(popt
)) {
957 tag
= sa_get_property_attr(popt
, "type");
960 prop
= sa_get_property(localoptions
, tag
);
962 value
= sa_get_property_attr(popt
,
964 pvalue
= sa_get_property_attr(prop
,
966 if (value
!= NULL
&& pvalue
!= NULL
&&
967 strcmp(value
, pvalue
) == 0) {
969 * Remove the property
973 * don't need to reset
979 (void) _sa_remove_property(
983 sa_free_attr_string(value
);
985 sa_free_attr_string(pvalue
);
987 sa_free_attr_string(tag
);
989 prop
= sa_get_property(localoptions
, NULL
);
990 if (prop
== NULL
&& sa_is_share(group
)) {
992 * All properties removed so remove the
993 * optionset if it is on a share
995 (void) _sa_remove_optionset(localoptions
);
997 sa_free_derived_optionset(optionset
);
1000 * Need to remove security here. If there are no
1001 * security options on the local group/share, don't
1002 * bother since those are the only ones that would be
1005 localoptions
= sa_get_all_security_types(group
, proto
, 0);
1006 if (localoptions
!= NULL
) {
1007 for (prop
= sa_get_property(localoptions
, NULL
);
1009 prop
= sa_get_next_property(prop
)) {
1011 sa_security_t security
;
1012 tag
= sa_get_property_attr(prop
, "type");
1014 sa_property_t nextpopt
= NULL
;
1015 security
= sa_get_security(group
, tag
, proto
);
1016 sa_free_attr_string(tag
);
1018 * prop's value only changes outside this loop
1020 pvalue
= sa_get_property_attr(prop
, "value");
1021 for (popt
= sa_get_property(security
, NULL
);
1026 * Need to get the next prop
1027 * now since we could break
1028 * the list during removal.
1030 nextpopt
= sa_get_next_property(popt
);
1031 /* remove Duplicates from this level */
1032 value
= sa_get_property_attr(popt
,
1034 if (value
!= NULL
&& pvalue
!= NULL
&&
1035 strcmp(value
, pvalue
) == 0) {
1037 * remove the property
1040 (void) _sa_remove_property
1044 sa_free_attr_string(value
);
1047 sa_free_attr_string(pvalue
);
1050 (void) sa_destroy_optionset(localoptions
);
1056 * dfs_free_list(list)
1058 * Free the data in each list entry of the list as well as freeing the
1059 * entries themselves. We need to avoid memory leaks and don't want to
1060 * dereference any NULL members.
1064 dfs_free_list(xfs_sharelist_t
*list
)
1066 xfs_sharelist_t
*entry
;
1067 for (entry
= list
; entry
!= NULL
; entry
= list
) {
1069 free(entry
->resource
);
1070 free(entry
->fstype
);
1071 free(entry
->options
);
1072 free(entry
->description
);
1073 free(entry
->origline
);
1081 * parse_dfstab(dfstab, root)
1083 * Open and read the existing dfstab, parsing each line and adding it
1084 * to the internal configuration. Make sure syntax errors, etc are
1085 * preserved as comments.
1089 parse_dfstab(sa_handle_t handle
, char *dfstab
, xmlNodePtr root
)
1093 sa_group_t sgroup
= NULL
;
1094 sa_group_t defgroup
;
1095 xfs_sharelist_t
*head
, *list
;
1101 /* read the dfstab format file and fill in the doc tree */
1103 dfs
= fopen(dfstab
, "r");
1107 defgroup
= sa_get_group(handle
, "default");
1109 for (head
= list
= getdfstab(dfs
);
1111 list
= list
->next
) {
1117 if (list
->origline
== NULL
) {
1119 * Comment line that we will likely skip.
1120 * If the line has the syntax:
1121 * # error: string: string
1122 * It should be preserved until manually deleted.
1124 if (list
->description
!= NULL
&&
1125 strncmp(list
->description
, "# Error: ", 9) == 0) {
1129 line
= strdup(list
->description
);
1132 cmd
= strchr(error
, ':');
1138 cmd
[len
- 1] = '\0';
1139 add_syntax_comment(root
, cmd
,
1147 if (list
->path
!= NULL
&& strlen(list
->path
) > 0 &&
1148 *list
->path
== '/') {
1149 share
= sa_find_share(handle
, list
->path
);
1151 sgroup
= sa_get_parent_group(share
);
1155 (void) printf(dgettext(TEXT_DOMAIN
,
1156 "No share specified in dfstab: "
1158 list
->lineno
, list
->origline
);
1159 add_syntax_comment(root
, list
->origline
,
1160 dgettext(TEXT_DOMAIN
, "No share specified"), 1);
1163 if (list
->group
!= NULL
&& strlen(list
->group
) > 0) {
1164 group
= sa_get_group(handle
, list
->group
);
1169 if (defined_group
&& group
== NULL
) {
1170 (void) printf(dgettext(TEXT_DOMAIN
,
1171 "Unknown group used in dfstab: line %d: %s\n"),
1172 list
->lineno
, list
->origline
);
1173 add_syntax_comment(root
, list
->origline
,
1174 dgettext(TEXT_DOMAIN
, "Unknown group specified"),
1178 if (group
== NULL
) {
1179 /* Shouldn't happen unless an SMF error */
1180 err
= SA_CONFIG_ERR
;
1183 if (share
== NULL
) {
1184 if (defined_group
|| group
!= defgroup
)
1186 /* This is an OK add for legacy */
1187 share
= sa_add_share(defgroup
, list
->path
,
1188 SA_SHARE_PERMANENT
| SA_SHARE_PARSER
, &err
);
1189 if (share
!= NULL
) {
1190 if (list
->description
!= NULL
&&
1191 strlen(list
->description
) > 0)
1192 (void) sa_set_share_description(share
,
1194 if (list
->options
!= NULL
&&
1195 strlen(list
->options
) > 0) {
1196 (void) sa_parse_legacy_options(share
,
1197 list
->options
, list
->fstype
);
1199 if (list
->resource
!= NULL
)
1200 (void) sa_set_share_attr(share
,
1201 "resource", list
->resource
);
1203 (void) printf(dgettext(TEXT_DOMAIN
,
1204 "Error in dfstab: line %d: %s\n"),
1205 list
->lineno
, list
->origline
);
1206 if (err
!= SA_BAD_PATH
)
1207 add_syntax_comment(root
, list
->origline
,
1208 dgettext(TEXT_DOMAIN
, "Syntax"), 1);
1210 add_syntax_comment(root
, list
->origline
,
1211 dgettext(TEXT_DOMAIN
,
1216 if (group
!= sgroup
) {
1217 (void) printf(dgettext(TEXT_DOMAIN
,
1218 "Attempt to change configuration in "
1219 "dfstab: line %d: %s\n"),
1220 list
->lineno
, list
->origline
);
1221 add_syntax_comment(root
, list
->origline
,
1222 dgettext(TEXT_DOMAIN
,
1223 "Attempt to change configuration"), 1);
1227 * It is the same group but could have changed
1228 * options. Make sure we include the group's
1229 * properties so we don't end up moving them to
1230 * the share inadvertantly. The last arg being
1231 * true says to get the inherited properties as well
1232 * as the local properties.
1234 oldprops
= sa_proto_legacy_format(list
->fstype
, share
,
1237 if (oldprops
== NULL
)
1240 if (list
->options
!= NULL
&&
1241 strcmp(oldprops
, list
->options
) != 0) {
1242 sa_optionset_t opts
;
1245 /* possibly different values */
1246 opts
= sa_get_optionset((sa_group_t
)
1247 share
, list
->fstype
);
1248 (void) sa_destroy_optionset(opts
);
1250 for (secs
= sa_get_security(
1251 (sa_group_t
)share
, NULL
, list
->fstype
);
1253 secs
= sa_get_security((sa_group_t
)share
,
1254 NULL
, list
->fstype
)) {
1255 (void) sa_destroy_security(
1258 (void) sa_parse_legacy_options(share
,
1259 list
->options
, list
->fstype
);
1261 sa_format_free(oldprops
);
1264 dfs_free_list(head
);
1268 * legacy_removes(group, file)
1270 * Find any shares that are "missing" from the legacy file. These
1271 * should be removed from the configuration since they are likely from
1272 * a legacy app or the admin modified the dfstab file directly. We
1273 * have to support this even if it is not the recommended way to do
1278 legacy_removes(sa_group_t group
, char *file
)
1282 xfs_sharelist_t
*list
, *item
;
1285 dfstab
= fopen(file
, "r");
1286 if (dfstab
!= NULL
) {
1287 list
= getdfstab(dfstab
);
1288 (void) fclose(dfstab
);
1290 for (share
= sa_get_share(group
, NULL
);
1292 share
= sa_get_next_share(share
)) {
1293 /* now see if the share is in the dfstab file */
1294 path
= sa_get_share_attr(share
, "path");
1296 item
= finddfsentry(list
, path
);
1297 sa_free_attr_string(path
);
1299 /* The share was removed this way */
1300 (void) sa_remove_share(share
);
1303 * Start over since the list was broken
1310 dfs_free_list(list
);
1315 * getlegacyconfig(path, root)
1317 * Parse dfstab and build the legacy configuration. This only gets
1318 * called when a change was detected.
1322 getlegacyconfig(sa_handle_t handle
, char *path
, xmlNodePtr
*root
)
1324 sa_group_t defgroup
;
1328 *root
= xmlNewNode(NULL
, (xmlChar
*)"sharecfg");
1329 if (*root
!= NULL
) {
1330 if (strcmp(path
, SA_LEGACY_DFSTAB
) == 0) {
1332 * Walk the default shares and find anything
1333 * missing. we do this first to make sure it
1334 * is cleaned up since there may be legacy
1335 * code add/del via dfstab and we need to
1338 defgroup
= sa_get_group(handle
, "default");
1339 if (defgroup
!= NULL
)
1340 legacy_removes(defgroup
, path
);
1341 /* Parse the dfstab and add anything new */
1342 parse_dfstab(handle
, path
, *root
);
1349 * get_share_list(&err)
1351 * Get a linked list of all the shares on the system from
1352 * /etc/dfs/sharetab. This is partially copied from libfsmgt which we
1353 * can't use due to package dependencies.
1355 static xfs_sharelist_t
*
1356 get_share_list(int *errp
)
1358 xfs_sharelist_t
*newp
;
1359 xfs_sharelist_t
*headp
;
1360 xfs_sharelist_t
*tailp
;
1366 if ((fp
= fopen(SHARETAB
, "r")) != NULL
) {
1367 struct share
*sharetab_entry
;
1369 (void) lockf(fileno(fp
), F_LOCK
, 0);
1370 (void) mutex_lock(&sharetab_lock
);
1372 while (getshare(fp
, &sharetab_entry
) > 0) {
1373 newp
= alloc_sharelist();
1375 (void) mutex_unlock(&sharetab_lock
);
1376 (void) lockf(fileno(fp
), F_ULOCK
, 0);
1381 * Link into the list here so we don't leak
1382 * memory on a failure from strdup().
1384 if (headp
== NULL
) {
1392 newp
->path
= strdup(sharetab_entry
->sh_path
);
1393 newp
->resource
= strdup(sharetab_entry
->sh_res
);
1394 newp
->fstype
= strdup(sharetab_entry
->sh_fstype
);
1395 newp
->options
= strdup(sharetab_entry
->sh_opts
);
1396 newp
->description
= strdup(sharetab_entry
->sh_descr
);
1398 if (newp
->path
== NULL
|| newp
->resource
== NULL
||
1399 newp
->fstype
== NULL
|| newp
->options
== NULL
||
1400 newp
->description
== NULL
) {
1401 (void) mutex_unlock(&sharetab_lock
);
1402 (void) lockf(fileno(fp
), F_ULOCK
, 0);
1407 (void) mutex_unlock(&sharetab_lock
);
1408 (void) lockf(fileno(fp
), F_ULOCK
, 0);
1415 * Caller must free the mount list
1420 * Out of memory so cleanup and leave.
1422 dfs_free_list(headp
);
1428 * parse_sharetab_impl(handle, tmplist, lgroup)
1430 * Read the /etc/dfs/sharetab file and see which entries don't exist
1431 * in the repository. These shares are marked transient. We also need
1432 * to see if they are ZFS shares since ZFS bypasses the SMF
1435 * The return value is used to contribute to values indicating if it is a legacy
1436 * share. Right now this is 0 or 1, but as multiple systems become legacy that
1441 parse_sharetab_impl(sa_handle_t handle
, xfs_sharelist_t
*tmplist
,
1449 char shareopts
[MAXNAMLEN
];
1452 share
= sa_find_share(handle
, tmplist
->path
);
1453 if (share
!= NULL
) {
1455 * If this is a legacy share, mark as shared so we only update
1456 * sharetab appropriately. We also keep the sharetab options in
1457 * order to display for legacy share with no arguments.
1459 set_node_attr(share
, "shared", "true");
1460 (void) snprintf(shareopts
, MAXNAMLEN
, "shareopts-%s",
1462 set_node_attr(share
, shareopts
, tmplist
->options
);
1467 * This share is transient so needs to be added. Initially, this will be
1468 * under default(legacy) unless it is a ZFS share. If zfs, we need a zfs
1471 if (tmplist
->resource
!= NULL
&&
1472 (groupname
= strchr(tmplist
->resource
, '@')) != NULL
) {
1473 /* There is a defined group */
1474 *groupname
++ = '\0';
1475 group
= sa_get_group(handle
, groupname
);
1476 if (group
!= NULL
) {
1477 share
= _sa_add_share(group
, tmplist
->path
,
1478 SA_SHARE_TRANSIENT
, &err
,
1479 (uint64_t)SA_FEATURE_NONE
);
1482 * While this case shouldn't occur very often, it does
1483 * occur out of a "zfs set sharenfs=off" when the
1484 * dataset is also set to canmount=off. A warning will
1485 * then cause the zfs command to abort. Since we add it
1486 * to the default list, everything works properly anyway
1487 * and the library doesn't need to give a warning.
1489 share
= _sa_add_share(lgroup
,
1490 tmplist
->path
, SA_SHARE_TRANSIENT
,
1491 &err
, (uint64_t)SA_FEATURE_NONE
);
1494 if (sa_zfs_is_shared(handle
, tmplist
->path
)) {
1495 group
= sa_get_group(handle
, "zfs");
1496 if (group
== NULL
) {
1497 group
= sa_create_group(handle
,
1499 if (group
== NULL
&&
1500 err
== SA_NO_PERMISSION
) {
1501 group
= _sa_create_group(
1506 if (group
!= NULL
) {
1507 (void) sa_create_optionset(
1508 group
, tmplist
->fstype
);
1509 (void) sa_set_group_attr(group
,
1513 if (group
!= NULL
) {
1514 share
= _sa_add_share(group
,
1515 tmplist
->path
, SA_SHARE_TRANSIENT
,
1516 &err
, (uint64_t)SA_FEATURE_NONE
);
1519 share
= _sa_add_share(lgroup
, tmplist
->path
,
1520 SA_SHARE_TRANSIENT
, &err
,
1521 (uint64_t)SA_FEATURE_NONE
);
1525 (void) printf(dgettext(TEXT_DOMAIN
,
1526 "Problem with transient: %s\n"), sa_errorstr(err
));
1528 set_node_attr(share
, "shared", "true");
1530 if (tmplist
->options
!= NULL
&&
1531 strlen(tmplist
->options
) > 0) {
1532 (void) sa_parse_legacy_options(share
,
1533 tmplist
->options
, tmplist
->fstype
);
1535 if (tmplist
->resource
!= NULL
&&
1536 strcmp(tmplist
->resource
, "-") != 0)
1537 set_node_attr(share
, "resource",
1539 if (tmplist
->description
!= NULL
) {
1541 node
= xmlNewChild((xmlNodePtr
)share
, NULL
,
1542 (xmlChar
*)"description", NULL
);
1543 xmlNodeSetContent(node
,
1544 (xmlChar
*)tmplist
->description
);
1552 * Given an array of paths, parses the sharetab in /etc/dfs/sharetab looking for
1553 * matches to each path that could be transients from that file.
1555 * parse_sharetab_impl has more information on what exactly it is looking for.
1558 parse_sharetab_for_paths(sa_handle_t handle
, char **paths
, size_t paths_len
)
1563 xfs_sharelist_t
*list
= get_share_list(&err
);
1568 lgroup
= sa_get_group(handle
, "default");
1569 for (int i
= 0; i
< paths_len
; ++i
) {
1570 xfs_sharelist_t
*tmplist
;
1572 for (tmplist
= list
; tmplist
!= NULL
; tmplist
= tmplist
->next
) {
1573 if (strcmp(tmplist
->path
, paths
[i
]) == 0) {
1577 if (tmplist
== NULL
) {
1581 legacy
= parse_sharetab_impl(handle
, tmplist
, lgroup
);
1584 dfs_free_list(list
);
1589 * Runs parse_sharetab_impl on all the different share lists on the system.
1592 parse_sharetab(sa_handle_t handle
)
1597 xfs_sharelist_t
*list
= get_share_list(&err
);
1599 list
= get_share_list(&err
);
1603 lgroup
= sa_get_group(handle
, "default");
1605 for (xfs_sharelist_t
*tmplist
= list
; tmplist
!= NULL
;
1606 tmplist
= tmplist
->next
) {
1607 legacy
|= parse_sharetab_impl(handle
, tmplist
, lgroup
);
1609 dfs_free_list(list
);
1614 * This is the same as gettransients except when parsing sharetab the entries
1615 * are only processed if the path matches an element of paths.
1618 get_one_transient(sa_handle_impl_t ihandle
, xmlNodePtr
*root
, char **paths
,
1622 char **protocols
= NULL
;
1626 *root
= xmlNewNode(NULL
, (xmlChar
*)"sharecfg");
1627 if (*root
!= NULL
) {
1628 legacy
= parse_sharetab_for_paths(ihandle
, paths
,
1630 int numproto
= sa_get_protocols(&protocols
);
1631 for (int i
= 0; i
< numproto
; i
++)
1632 legacy
|= sa_proto_get_transients(
1633 (sa_handle_t
)ihandle
, protocols
[i
]);
1634 if (protocols
!= NULL
)
1642 * Get the transient shares from the sharetab (or other) file. Since
1643 * these are transient, they only appear in the working file and not
1647 gettransients(sa_handle_impl_t ihandle
, xmlNodePtr
*root
)
1651 char **protocols
= NULL
;
1656 *root
= xmlNewNode(NULL
, (xmlChar
*)"sharecfg");
1657 if (*root
!= NULL
) {
1658 legacy
= parse_sharetab(ihandle
);
1659 numproto
= sa_get_protocols(&protocols
);
1660 for (i
= 0; i
< numproto
; i
++)
1661 legacy
|= sa_proto_get_transients(
1662 (sa_handle_t
)ihandle
, protocols
[i
]);
1670 * sa_has_prop(optionset, prop)
1672 * Is the specified property a member of the optionset?
1676 sa_has_prop(sa_optionset_t optionset
, sa_property_t prop
)
1679 sa_property_t otherprop
;
1682 if (optionset
!= NULL
) {
1683 name
= sa_get_property_attr(prop
, "type");
1685 otherprop
= sa_get_property(optionset
, name
);
1686 if (otherprop
!= NULL
)
1688 sa_free_attr_string(name
);
1695 * Update legacy files
1697 * Provides functions to add/remove/modify individual entries
1698 * in dfstab and sharetab
1702 update_legacy_config(sa_handle_t handle
)
1705 * no longer used -- this is a placeholder in case we need to
1706 * add it back later.
1711 * sa_valid_property(handle, object, proto, property)
1713 * check to see if the specified property is valid relative to the
1714 * specified protocol. The protocol plugin is called to do the work.
1718 sa_valid_property(sa_handle_t handle
, void *object
, char *proto
,
1719 sa_property_t property
)
1723 if (proto
!= NULL
&& property
!= NULL
) {
1724 ret
= sa_proto_valid_prop(handle
, proto
, property
, object
);
1733 * Given path, return the string representing the path's file system
1734 * type. This is used to discover ZFS shares.
1738 sa_fstype(char *path
)
1743 err
= stat(path
, &st
);
1745 err
= SA_NO_SUCH_PATH
;
1750 * If we have a valid path at this point ret, return the fstype.
1753 return (strdup(st
.st_fstype
));
1759 sa_free_fstype(char *type
)
1765 * sa_get_derived_optionset(object, proto, hier)
1767 * Work backward to the top of the share object tree and start
1768 * copying protocol specific optionsets into a newly created
1769 * optionset that doesn't have a parent (it will be freed
1770 * later). This provides for the property inheritance model. That
1771 * is, properties closer to the share take precedence over group
1772 * level. This also provides for groups of groups in the future.
1776 sa_get_derived_optionset(void *object
, char *proto
, int hier
)
1778 sa_optionset_t newoptionset
;
1779 sa_optionset_t optionset
;
1783 (group
= sa_get_parent_group((sa_share_t
)object
)) != NULL
) {
1784 newoptionset
= sa_get_derived_optionset((void *)group
, proto
,
1787 newoptionset
= (sa_optionset_t
)xmlNewNode(NULL
,
1788 (xmlChar
*)"optionset");
1789 if (newoptionset
!= NULL
) {
1790 sa_set_optionset_attr(newoptionset
, "type", proto
);
1793 /* Dont' do anything if memory wasn't allocated */
1794 if (newoptionset
== NULL
)
1797 /* Found the top so working back down the stack */
1798 optionset
= sa_get_optionset((sa_optionset_t
)object
, proto
);
1799 if (optionset
!= NULL
) {
1801 /* add optionset to the newoptionset */
1802 for (prop
= sa_get_property(optionset
, NULL
);
1804 prop
= sa_get_next_property(prop
)) {
1805 sa_property_t newprop
;
1808 name
= sa_get_property_attr(prop
, "type");
1809 value
= sa_get_property_attr(prop
, "value");
1812 newprop
= sa_get_property(newoptionset
, name
);
1813 /* Replace the value with the new value */
1814 if (newprop
!= NULL
) {
1816 * Only set if value is non NULL, old value ok
1820 set_node_attr(newprop
, "value", value
);
1822 /* an entirely new property */
1823 if (value
!= NULL
) {
1824 newprop
= sa_create_property(name
,
1826 if (newprop
!= NULL
) {
1827 newprop
= (sa_property_t
)
1829 (xmlNodePtr
)newoptionset
,
1830 (xmlNodePtr
)newprop
);
1834 sa_free_attr_string(name
);
1837 sa_free_attr_string(value
);
1840 return (newoptionset
);
1844 sa_free_derived_optionset(sa_optionset_t optionset
)
1846 /* While it shouldn't be linked, it doesn't hurt */
1847 if (optionset
!= NULL
) {
1848 xmlUnlinkNode((xmlNodePtr
) optionset
);
1849 xmlFreeNode((xmlNodePtr
) optionset
);
1854 * sa_get_all_security_types(object, proto, hier)
1856 * Find all the security types set for this object. This is
1857 * preliminary to getting a derived security set. The return value is an
1858 * optionset containg properties which are the sectype values found by
1859 * walking up the XML document structure. The returned optionset
1860 * is a derived optionset.
1862 * If hier is 0, only look at object. If non-zero, walk up the tree.
1865 sa_get_all_security_types(void *object
, char *proto
, int hier
)
1867 sa_optionset_t options
;
1868 sa_security_t security
;
1875 (group
= sa_get_parent_group((sa_share_t
)object
)) != NULL
)
1876 options
= sa_get_all_security_types((void *)group
, proto
, hier
);
1878 options
= (sa_optionset_t
)xmlNewNode(NULL
,
1879 (xmlChar
*)"optionset");
1881 if (options
== NULL
)
1884 /* Hit the top so collect the security types working back. */
1885 for (security
= sa_get_security((sa_group_t
)object
, NULL
, NULL
);
1887 security
= sa_get_next_security(security
)) {
1891 type
= sa_get_security_attr(security
, "type");
1893 if (strcmp(type
, proto
) != 0) {
1894 sa_free_attr_string(type
);
1897 sectype
= sa_get_security_attr(security
, "sectype");
1898 if (sectype
!= NULL
) {
1900 * Have a security type, check to see if
1901 * already present in optionset and add if it
1904 if (sa_get_property(options
, sectype
) == NULL
) {
1905 prop
= sa_create_property(sectype
,
1908 prop
= (sa_property_t
)
1910 (xmlNodePtr
)options
,
1913 sa_free_attr_string(sectype
);
1915 sa_free_attr_string(type
);
1923 * sa_get_derived_security(object, sectype, proto, hier)
1925 * Get the derived security(named optionset) for the object given the
1926 * sectype and proto. If hier is non-zero, walk up the tree to get all
1927 * properties defined for this object, otherwise just those on the
1932 sa_get_derived_security(void *object
, char *sectype
, char *proto
, int hier
)
1934 sa_security_t newsecurity
;
1935 sa_security_t security
;
1940 (group
= sa_get_parent_group((sa_share_t
)object
)) != NULL
) {
1941 newsecurity
= sa_get_derived_security((void *)group
,
1942 sectype
, proto
, hier
);
1944 newsecurity
= (sa_security_t
)xmlNewNode(NULL
,
1945 (xmlChar
*)"security");
1946 if (newsecurity
!= NULL
) {
1947 sa_set_security_attr(newsecurity
, "type", proto
);
1948 sa_set_security_attr(newsecurity
, "sectype", sectype
);
1951 /* Don't do anything if memory wasn't allocated */
1952 if (newsecurity
== NULL
)
1953 return (newsecurity
);
1955 /* Found the top so working back down the stack. */
1956 security
= sa_get_security((sa_security_t
)object
, sectype
, proto
);
1957 if (security
== NULL
)
1958 return (newsecurity
);
1960 /* add security to the newsecurity */
1961 for (prop
= sa_get_property(security
, NULL
);
1962 prop
!= NULL
; prop
= sa_get_next_property(prop
)) {
1963 sa_property_t newprop
;
1966 name
= sa_get_property_attr(prop
, "type");
1967 value
= sa_get_property_attr(prop
, "value");
1969 newprop
= sa_get_property(newsecurity
, name
);
1970 /* Replace the value with the new value */
1971 if (newprop
!= NULL
) {
1973 * Only set if value is non NULL, old
1974 * value ok if it is NULL. The value
1975 * must be associated with the "value"
1979 set_node_attr(newprop
, "value", value
);
1981 /* An entirely new property */
1982 if (value
!= NULL
) {
1983 newprop
= sa_create_property(name
,
1985 newprop
= (sa_property_t
)
1986 xmlAddChild((xmlNodePtr
)newsecurity
,
1987 (xmlNodePtr
)newprop
);
1990 sa_free_attr_string(name
);
1993 sa_free_attr_string(value
);
1995 return (newsecurity
);
1999 sa_free_derived_security(sa_security_t security
)
2001 /* while it shouldn't be linked, it doesn't hurt */
2002 if (security
!= NULL
) {
2003 xmlUnlinkNode((xmlNodePtr
)security
);
2004 xmlFreeNode((xmlNodePtr
)security
);
2009 * sharetab utility functions
2011 * Makes use of the original sharetab.c from fs.d/nfs/lib
2015 * sa_fillshare(share, proto, sh)
2017 * Fill the struct share with values obtained from the share object.
2020 sa_fillshare(sa_share_t share
, char *proto
, struct share
*sh
)
2022 char *groupname
= NULL
;
2027 sa_resource_t resource
;
2028 char *rsrcname
= NULL
;
2032 * We only want to deal with the path level shares for the
2033 * sharetab file. If a resource, get the parent.
2035 if (sa_is_resource(share
)) {
2036 resource
= (sa_resource_t
)share
;
2037 share
= sa_get_resource_parent(resource
);
2038 rsrcname
= sa_get_resource_attr(resource
, "name");
2041 group
= sa_get_parent_group(share
);
2042 if (group
!= NULL
) {
2043 zfs
= sa_get_group_attr(group
, "zfs");
2044 groupname
= sa_get_group_attr(group
, "name");
2046 if (groupname
!= NULL
&&
2047 (strcmp(groupname
, "default") == 0 || zfs
!= NULL
)) {
2049 * since the groupname is either "default" or the
2050 * group is a ZFS group, we don't want to keep
2051 * groupname. We do want it if it is any other type of
2054 sa_free_attr_string(groupname
);
2058 sa_free_attr_string(zfs
);
2061 value
= sa_get_share_attr(share
, "path");
2062 if (value
!= NULL
) {
2063 sh
->sh_path
= strdup(value
);
2064 sa_free_attr_string(value
);
2067 if (rsrcname
!= NULL
|| groupname
!= NULL
) {
2070 if (rsrcname
!= NULL
)
2071 len
+= strlen(rsrcname
);
2072 if (groupname
!= NULL
)
2073 len
+= strlen(groupname
);
2074 len
+= 3; /* worst case */
2076 (void) snprintf(buff
, len
, "%s%s%s",
2077 (rsrcname
!= NULL
&&
2078 strlen(rsrcname
) > 0) ? rsrcname
: "-",
2079 groupname
!= NULL
? "@" : "",
2080 groupname
!= NULL
? groupname
: "");
2082 if (rsrcname
!= NULL
)
2083 sa_free_attr_string(rsrcname
);
2084 if (groupname
!= NULL
)
2085 sa_free_attr_string(groupname
);
2087 sh
->sh_res
= strdup("-");
2091 * Get correct default prop string. NFS uses "rw", others use
2094 if (strcmp(proto
, "nfs") != 0)
2099 sh
->sh_fstype
= strdup(proto
);
2100 value
= sa_proto_legacy_format(proto
, share
, 1);
2101 if (value
!= NULL
) {
2102 if (strlen(value
) > 0)
2103 sh
->sh_opts
= strdup(value
);
2105 sh
->sh_opts
= strdup(defprop
);
2108 sh
->sh_opts
= strdup(defprop
);
2111 value
= sa_get_share_description(share
);
2112 if (value
!= NULL
) {
2113 sh
->sh_descr
= strdup(value
);
2114 sa_free_share_description(value
);
2116 sh
->sh_descr
= strdup("");
2123 * Free the strings in the non-NULL members of sh.
2127 sa_emptyshare(struct share
*sh
)
2133 free(sh
->sh_fstype
);
2134 sh
->sh_fstype
= NULL
;
2138 sh
->sh_descr
= NULL
;
2142 * sa_update_sharetab_ts(handle)
2144 * Update the internal timestamp of when sharetab was last
2145 * changed. This needs to be public for ZFS to get at it.
2149 sa_update_sharetab_ts(sa_handle_t handle
)
2152 sa_handle_impl_t implhandle
= (sa_handle_impl_t
)handle
;
2154 if (implhandle
!= NULL
&& stat(SA_LEGACY_SHARETAB
, &st
) == 0)
2155 implhandle
->tssharetab
= TSTAMP(st
.st_mtim
);
2159 * sa_update_sharetab(share, proto)
2161 * Update the sharetab file with info from the specified share.
2162 * This could be an update or add.
2166 sa_update_sharetab(sa_share_t share
, char *proto
)
2173 path
= sa_get_share_attr(share
, "path");
2175 (void) memset(&sh
, '\0', sizeof (sh
));
2177 handle
= sa_find_group_handle((sa_group_t
)share
);
2178 if (handle
!= NULL
) {
2180 * Fill in share structure and send it to the kernel.
2182 (void) sa_fillshare(share
, proto
, &sh
);
2183 (void) _sharefs(SHAREFS_ADD
, &sh
);
2185 * We need the timestamp of the sharetab file right
2186 * after the update was done. This lets us detect a
2187 * change that made by a different process.
2189 sa_update_sharetab_ts(handle
);
2192 ret
= SA_CONFIG_ERR
;
2194 sa_free_attr_string(path
);
2201 * sa_delete_sharetab(handle, path, proto)
2203 * remove the specified share from sharetab.
2207 sa_delete_sharetab(sa_handle_t handle
, char *path
, char *proto
)
2214 * Both the path and the proto are
2215 * keys into the sharetab.
2217 if (path
!= NULL
&& proto
!= NULL
) {
2218 (void) memset(&sh
, '\0', sizeof (sh
));
2220 sh
.sh_fstype
= proto
;
2222 ret
= _sharefs(SHAREFS_REMOVE
, &sh
);
2223 if (handle
!= NULL
&& stat(SA_LEGACY_SHARETAB
, &st
) == 0)
2224 sa_update_sharetab_ts(handle
);
2230 * sa_needs_refresh(handle)
2232 * Returns B_TRUE if the internal cache needs to be refreshed due to a
2233 * change by another process. B_FALSE returned otherwise.
2236 sa_needs_refresh(sa_handle_t handle
)
2238 sa_handle_impl_t implhandle
= (sa_handle_impl_t
)handle
;
2242 scf_simple_prop_t
*prop
;
2244 if (handle
== NULL
|| implhandle
->sa_service
&
2245 (SA_INIT_ONE_SHARE_FROM_NAME
| SA_INIT_ONE_SHARE_FROM_HANDLE
))
2249 * If sharetab has changed, then there was an external
2250 * change. Check sharetab first since it is updated by ZFS as
2251 * well as sharemgr. This is where external ZFS changes are
2254 if (stat(SA_LEGACY_SHARETAB
, &st
) == 0 &&
2255 TSTAMP(st
.st_mtim
) != implhandle
->tssharetab
)
2259 * If sharetab wasn't changed, check whether there were any
2260 * SMF transactions that modified the config but didn't
2261 * initiate a share. This is less common but does happen.
2263 prop
= scf_simple_prop_get(implhandle
->scfhandle
->handle
,
2264 (const char *)SA_SVC_FMRI_BASE
":default", "state",
2267 str
= scf_simple_prop_next_astring(prop
);
2269 tstamp
= strtoull(str
, NULL
, 0);
2272 scf_simple_prop_free(prop
);
2273 if (tstamp
!= implhandle
->tstrans
)
2281 * sa_fix_resource_name(path)
2283 * Convert invalid characters in a resource name (SMB share name)
2284 * to underscores ('_'). The list of invalid characters includes
2285 * control characters and the following:
2287 * " / \ [ ] : | < > + ; , ? * =
2289 * The caller must pass a valid path. Leading and trailing slashes
2290 * are stripped from the path before converting invalid characters.
2291 * Resource names are restricted to SA_MAX_RESOURCE_NAME characters.
2294 sa_fix_resource_name(char *path
)
2296 char *invalid
= "\"/\\[]:|<>+;,?*=";
2301 assert(path
!= NULL
);
2304 * Strip leading and trailing /'s.
2306 p
+= strspn(p
, "/");
2307 q
= strchr(p
, '\0');
2308 if (q
!= NULL
&& q
!= path
) {
2309 while ((--q
, *q
== '/'))
2314 (void) strcpy(path
, "_");
2319 * Stride over path components until the remaining
2320 * path is no longer than SA_MAX_RESOURCE_NAME.
2323 while ((q
!= NULL
) && (strlen(q
) > SA_MAX_RESOURCE_NAME
)) {
2324 if ((q
= strchr(q
, '/')) != NULL
) {
2331 * If the path is still longer than SA_MAX_RESOURCE_NAME,
2332 * take the trailing SA_MAX_RESOURCE_NAME characters.
2334 if ((len
= strlen(p
)) > SA_MAX_RESOURCE_NAME
) {
2335 len
= SA_MAX_RESOURCE_NAME
;
2336 p
= strchr(p
, '\0') - (SA_MAX_RESOURCE_NAME
- 1);
2339 (void) memmove(path
, p
, len
);
2342 for (p
= path
; *p
!= '\0'; ++p
) {
2343 if ((iscntrl(*p
)) || strchr(invalid
, *p
))