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);
532 * sa_comment_line(line, err)
534 * Add a comment to the dfstab file with err as a prefix to the
539 sa_comment_line(char *line
, char *err
)
542 xfs_sharelist_t
*list
;
545 dfstab
= open_dfstab(SA_LEGACY_DFSTAB
);
546 if (dfstab
!= NULL
) {
547 (void) setvbuf(dfstab
, NULL
, _IOLBF
, BUFSIZ
* 8);
549 (void) lockf(fileno(dfstab
), F_LOCK
, 0);
550 (void) mutex_lock(&sa_dfstab_lock
);
551 list
= getdfstab(dfstab
);
554 * don't ignore the return since the list could have
555 * gone to NULL if the file only had one line in it.
557 list
= remdfsline(list
, line
);
558 outdfstab(dfstab
, list
);
559 (void) fprintf(dfstab
, "# Error: %s: %s", err
, line
);
560 (void) fsync(fileno(dfstab
));
561 (void) mutex_unlock(&sa_dfstab_lock
);
562 (void) lockf(fileno(dfstab
), F_ULOCK
, 0);
563 (void) fclose(dfstab
);
571 * sa_delete_legacy(share, protocol)
573 * Delete the specified share from the legacy config file.
577 sa_delete_legacy(sa_share_t share
, char *protocol
)
582 xfs_sharelist_t
*list
;
584 sa_optionset_t optionset
;
589 * Protect against shares that don't have paths. This is not
590 * really an error at this point.
592 path
= sa_get_share_attr(share
, "path");
596 dfstab
= open_dfstab(SA_LEGACY_DFSTAB
);
597 if (dfstab
!= NULL
) {
598 (void) setvbuf(dfstab
, NULL
, _IOLBF
, BUFSIZ
* 8);
600 parent
= sa_get_parent_group(share
);
601 if (parent
!= NULL
) {
602 (void) lockf(fileno(dfstab
), F_LOCK
, 0);
603 (void) mutex_lock(&sa_dfstab_lock
);
604 list
= getdfstab(dfstab
);
606 if (protocol
!= NULL
) {
608 list
= remdfsentry(list
, path
,
611 for (optionset
= sa_get_optionset(parent
, NULL
);
614 sa_get_next_optionset(optionset
)) {
615 char *proto
= sa_get_optionset_attr(
618 if (list
!= NULL
&& proto
!= NULL
)
619 list
= remdfsentry(list
, path
,
624 * may want to only do the dfstab if
625 * this call returns NOT IMPLEMENTED
626 * but it shouldn't hurt.
629 err
= sa_proto_delete_legacy(
631 if (err
!= SA_NOT_IMPLEMENTED
)
635 sa_free_attr_string(proto
);
638 outdfstab(dfstab
, list
);
641 (void) fflush(dfstab
);
642 (void) mutex_unlock(&sa_dfstab_lock
);
643 (void) lockf(fileno(dfstab
), F_ULOCK
, 0);
645 (void) fsync(fileno(dfstab
));
647 (void) fclose(dfstab
);
649 if (errno
== EACCES
|| errno
== EPERM
)
650 ret
= SA_NO_PERMISSION
;
656 sa_free_attr_string(path
);
662 * sa_update_legacy(share, proto)
664 * There is an assumption that dfstab will be the most common form of
665 * legacy configuration file for shares, but not the only one. Because
666 * of that, dfstab handling is done in the main code with calls to
667 * this function and protocol specific calls to deal with formatting
668 * options into dfstab/share compatible syntax. Since not everything
669 * will be dfstab, there is a provision for calling a protocol
670 * specific plugin interface that allows the protocol plugin to do its
671 * own legacy files and skip the dfstab update.
675 sa_update_legacy(sa_share_t share
, char *proto
)
679 xfs_sharelist_t
*list
;
685 ret
= sa_proto_update_legacy(proto
, share
);
686 if (ret
!= SA_NOT_IMPLEMENTED
)
689 features
= sa_proto_get_featureset(proto
);
690 if (!(features
& SA_FEATURE_DFSTAB
))
693 /* do the dfstab format */
694 persist
= sa_get_share_attr(share
, "type");
696 * only update if the share is not transient -- no share type
697 * set or the type is not "transient".
699 if (persist
== NULL
|| strcmp(persist
, "transient") != 0) {
700 path
= sa_get_share_attr(share
, "path");
705 dfstab
= open_dfstab(SA_LEGACY_DFSTAB
);
706 if (dfstab
!= NULL
) {
707 (void) setvbuf(dfstab
, NULL
, _IOLBF
, BUFSIZ
* 8);
709 (void) lockf(fileno(dfstab
), F_LOCK
, 0);
710 (void) mutex_lock(&sa_dfstab_lock
);
711 list
= getdfstab(dfstab
);
714 list
= remdfsentry(list
, path
, proto
);
715 list
= adddfsentry(list
, share
, proto
);
716 outdfstab(dfstab
, list
);
717 (void) fflush(dfstab
);
718 (void) mutex_unlock(&sa_dfstab_lock
);
719 (void) lockf(fileno(dfstab
), F_ULOCK
, 0);
720 (void) fsync(fileno(dfstab
));
722 (void) fclose(dfstab
);
726 if (errno
== EACCES
|| errno
== EPERM
)
727 ret
= SA_NO_PERMISSION
;
731 sa_free_attr_string(path
);
735 sa_free_attr_string(persist
);
740 * sa_is_security(optname, proto)
742 * Check to see if optname is a security (named optionset) specific
743 * property for the specified protocol.
747 sa_is_security(char *optname
, char *proto
)
751 ret
= sa_proto_security_prop(proto
, optname
);
756 * add_syntax_comment(root, line, err, todfstab)
758 * Add a comment to the document indicating a syntax error. If
759 * todfstab is set, write it back to the dfstab file as well.
763 add_syntax_comment(xmlNodePtr root
, char *line
, char *err
, int todfstab
)
767 node
= xmlNewChild(root
, NULL
, (xmlChar
*)"error", (xmlChar
*)line
);
769 (void) xmlSetProp(node
, (xmlChar
*)"type", (xmlChar
*)err
);
771 sa_comment_line(line
, err
);
775 * sa_is_share(object)
777 * returns true if the object is of type "share".
781 sa_is_share(void *object
)
783 if (object
!= NULL
) {
784 if (strcmp((char *)((xmlNodePtr
)object
)->name
, "share") == 0)
790 * sa_is_resource(object)
792 * returns true if the object is of type "resource".
796 sa_is_resource(void *object
)
798 if (object
!= NULL
) {
799 if (strcmp((char *)((xmlNodePtr
)object
)->name
, "resource") == 0)
806 * _sa_remove_property(property)
808 * remove a property only from the document.
812 _sa_remove_property(sa_property_t property
)
814 xmlUnlinkNode((xmlNodePtr
)property
);
815 xmlFreeNode((xmlNodePtr
)property
);
819 * _sa_create_dummy_share()
821 * Create a share entry suitable for parsing but not tied to any real
822 * config tree. Need to have a parent as well as the node to parse
823 * on. Free using _sa_free_dummy_share(share);
827 _sa_create_dummy_share()
829 xmlNodePtr parent_node
= NULL
;
830 xmlNodePtr child_node
= NULL
;
832 parent_node
= xmlNewNode(NULL
, (xmlChar
*)"group");
833 if (parent_node
!= NULL
) {
834 child_node
= xmlNewChild(parent_node
, NULL
, (xmlChar
*)"share",
836 if (child_node
!= NULL
) {
838 * Use a "zfs" tag since that will make sure nothing
839 * really attempts to put values into the
840 * repository. Also ZFS is currently the only user of
843 set_node_attr(parent_node
, "type", "transient");
844 set_node_attr(parent_node
, "zfs", "true");
845 set_node_attr(child_node
, "type", "transient");
846 set_node_attr(child_node
, "zfs", "true");
848 xmlFreeNode(parent_node
);
855 * _sa_free_dummy_share(share)
857 * Free the dummy share and its parent. It is an error to try and
858 * free something that isn't a dummy.
862 _sa_free_dummy_share(sa_share_t share
)
864 xmlNodePtr node
= (xmlNodePtr
)share
;
870 parent
= node
->parent
;
871 name
= (char *)xmlGetProp(node
, (xmlChar
*)"path");
873 /* Real shares always have a path but a dummy doesn't */
874 ret
= SA_NOT_ALLOWED
;
875 sa_free_attr_string(name
);
878 * If there is a parent, do the free on that since
879 * xmlFreeNode is a recursive function and free's an
882 if (parent
!= NULL
) {
894 * sa_parse_legacy_options(group, options, proto)
896 * In order to support legacy configurations, we allow the protocol
897 * specific plugin to parse legacy syntax options (like those in
898 * /etc/dfs/dfstab). This adds a new optionset to the group (or
901 * Once the optionset has been created, we then get the derived
902 * optionset of the parent (options from the optionset of the parent
903 * and any parent it might have) and remove those from the created
904 * optionset. This avoids duplication of options.
908 sa_parse_legacy_options(sa_group_t group
, char *options
, char *proto
)
910 int ret
= SA_INVALID_PROTOCOL
;
912 int using_dummy
= B_FALSE
;
914 sa_optionset_t optionset
;
915 sa_property_t popt
, prop
;
916 sa_optionset_t localoptions
;
919 * If "group" is NULL, this is just a parse without saving
920 * anything in either SMF or ZFS. Create a dummy group to
924 group
= (sa_group_t
)_sa_create_dummy_share();
925 using_dummy
= B_TRUE
;
928 parent
= sa_get_parent_group(group
);
931 ret
= sa_proto_legacy_opts(proto
, group
, options
);
934 /* Since this is a dummy parse, cleanup and quit here */
935 (void) _sa_free_dummy_share(parent
);
943 * If in a group, remove the inherited options and security
949 /* Find parent options to remove from child */
950 optionset
= sa_get_derived_optionset(parent
, proto
, 1);
951 localoptions
= sa_get_optionset(group
, proto
);
952 if (optionset
!= NULL
) {
953 for (popt
= sa_get_property(optionset
, NULL
);
955 popt
= sa_get_next_property(popt
)) {
958 tag
= sa_get_property_attr(popt
, "type");
961 prop
= sa_get_property(localoptions
, tag
);
963 value
= sa_get_property_attr(popt
,
965 pvalue
= sa_get_property_attr(prop
,
967 if (value
!= NULL
&& pvalue
!= NULL
&&
968 strcmp(value
, pvalue
) == 0) {
970 * Remove the property
974 * don't need to reset
980 (void) _sa_remove_property(
984 sa_free_attr_string(value
);
986 sa_free_attr_string(pvalue
);
988 sa_free_attr_string(tag
);
990 prop
= sa_get_property(localoptions
, NULL
);
991 if (prop
== NULL
&& sa_is_share(group
)) {
993 * All properties removed so remove the
994 * optionset if it is on a share
996 (void) _sa_remove_optionset(localoptions
);
998 sa_free_derived_optionset(optionset
);
1001 * Need to remove security here. If there are no
1002 * security options on the local group/share, don't
1003 * bother since those are the only ones that would be
1006 localoptions
= sa_get_all_security_types(group
, proto
, 0);
1007 if (localoptions
!= NULL
) {
1008 for (prop
= sa_get_property(localoptions
, NULL
);
1010 prop
= sa_get_next_property(prop
)) {
1012 sa_security_t security
;
1013 tag
= sa_get_property_attr(prop
, "type");
1015 sa_property_t nextpopt
= NULL
;
1016 security
= sa_get_security(group
, tag
, proto
);
1017 sa_free_attr_string(tag
);
1019 * prop's value only changes outside this loop
1021 pvalue
= sa_get_property_attr(prop
, "value");
1022 for (popt
= sa_get_property(security
, NULL
);
1027 * Need to get the next prop
1028 * now since we could break
1029 * the list during removal.
1031 nextpopt
= sa_get_next_property(popt
);
1032 /* remove Duplicates from this level */
1033 value
= sa_get_property_attr(popt
,
1035 if (value
!= NULL
&& pvalue
!= NULL
&&
1036 strcmp(value
, pvalue
) == 0) {
1038 * remove the property
1041 (void) _sa_remove_property
1045 sa_free_attr_string(value
);
1048 sa_free_attr_string(pvalue
);
1051 (void) sa_destroy_optionset(localoptions
);
1057 * dfs_free_list(list)
1059 * Free the data in each list entry of the list as well as freeing the
1060 * entries themselves. We need to avoid memory leaks and don't want to
1061 * dereference any NULL members.
1065 dfs_free_list(xfs_sharelist_t
*list
)
1067 xfs_sharelist_t
*entry
;
1068 for (entry
= list
; entry
!= NULL
; entry
= list
) {
1069 if (entry
->path
!= NULL
)
1071 if (entry
->resource
!= NULL
)
1072 free(entry
->resource
);
1073 if (entry
->fstype
!= NULL
)
1074 free(entry
->fstype
);
1075 if (entry
->options
!= NULL
)
1076 free(entry
->options
);
1077 if (entry
->description
!= NULL
)
1078 free(entry
->description
);
1079 if (entry
->origline
!= NULL
)
1080 free(entry
->origline
);
1081 if (entry
->group
!= NULL
)
1089 * parse_dfstab(dfstab, root)
1091 * Open and read the existing dfstab, parsing each line and adding it
1092 * to the internal configuration. Make sure syntax errors, etc are
1093 * preserved as comments.
1097 parse_dfstab(sa_handle_t handle
, char *dfstab
, xmlNodePtr root
)
1101 sa_group_t sgroup
= NULL
;
1102 sa_group_t defgroup
;
1103 xfs_sharelist_t
*head
, *list
;
1109 /* read the dfstab format file and fill in the doc tree */
1111 dfs
= fopen(dfstab
, "r");
1115 defgroup
= sa_get_group(handle
, "default");
1117 for (head
= list
= getdfstab(dfs
);
1119 list
= list
->next
) {
1125 if (list
->origline
== NULL
) {
1127 * Comment line that we will likely skip.
1128 * If the line has the syntax:
1129 * # error: string: string
1130 * It should be preserved until manually deleted.
1132 if (list
->description
!= NULL
&&
1133 strncmp(list
->description
, "# Error: ", 9) == 0) {
1137 line
= strdup(list
->description
);
1140 cmd
= strchr(error
, ':');
1146 cmd
[len
- 1] = '\0';
1147 add_syntax_comment(root
, cmd
,
1155 if (list
->path
!= NULL
&& strlen(list
->path
) > 0 &&
1156 *list
->path
== '/') {
1157 share
= sa_find_share(handle
, list
->path
);
1159 sgroup
= sa_get_parent_group(share
);
1163 (void) printf(dgettext(TEXT_DOMAIN
,
1164 "No share specified in dfstab: "
1166 list
->lineno
, list
->origline
);
1167 add_syntax_comment(root
, list
->origline
,
1168 dgettext(TEXT_DOMAIN
, "No share specified"), 1);
1171 if (list
->group
!= NULL
&& strlen(list
->group
) > 0) {
1172 group
= sa_get_group(handle
, list
->group
);
1177 if (defined_group
&& group
== NULL
) {
1178 (void) printf(dgettext(TEXT_DOMAIN
,
1179 "Unknown group used in dfstab: line %d: %s\n"),
1180 list
->lineno
, list
->origline
);
1181 add_syntax_comment(root
, list
->origline
,
1182 dgettext(TEXT_DOMAIN
, "Unknown group specified"),
1186 if (group
== NULL
) {
1187 /* Shouldn't happen unless an SMF error */
1188 err
= SA_CONFIG_ERR
;
1191 if (share
== NULL
) {
1192 if (defined_group
|| group
!= defgroup
)
1194 /* This is an OK add for legacy */
1195 share
= sa_add_share(defgroup
, list
->path
,
1196 SA_SHARE_PERMANENT
| SA_SHARE_PARSER
, &err
);
1197 if (share
!= NULL
) {
1198 if (list
->description
!= NULL
&&
1199 strlen(list
->description
) > 0)
1200 (void) sa_set_share_description(share
,
1202 if (list
->options
!= NULL
&&
1203 strlen(list
->options
) > 0) {
1204 (void) sa_parse_legacy_options(share
,
1205 list
->options
, list
->fstype
);
1207 if (list
->resource
!= NULL
)
1208 (void) sa_set_share_attr(share
,
1209 "resource", list
->resource
);
1211 (void) printf(dgettext(TEXT_DOMAIN
,
1212 "Error in dfstab: line %d: %s\n"),
1213 list
->lineno
, list
->origline
);
1214 if (err
!= SA_BAD_PATH
)
1215 add_syntax_comment(root
, list
->origline
,
1216 dgettext(TEXT_DOMAIN
, "Syntax"), 1);
1218 add_syntax_comment(root
, list
->origline
,
1219 dgettext(TEXT_DOMAIN
,
1224 if (group
!= sgroup
) {
1225 (void) printf(dgettext(TEXT_DOMAIN
,
1226 "Attempt to change configuration in "
1227 "dfstab: line %d: %s\n"),
1228 list
->lineno
, list
->origline
);
1229 add_syntax_comment(root
, list
->origline
,
1230 dgettext(TEXT_DOMAIN
,
1231 "Attempt to change configuration"), 1);
1235 * It is the same group but could have changed
1236 * options. Make sure we include the group's
1237 * properties so we don't end up moving them to
1238 * the share inadvertantly. The last arg being
1239 * true says to get the inherited properties as well
1240 * as the local properties.
1242 oldprops
= sa_proto_legacy_format(list
->fstype
, share
,
1245 if (oldprops
== NULL
)
1248 if (list
->options
!= NULL
&&
1249 strcmp(oldprops
, list
->options
) != 0) {
1250 sa_optionset_t opts
;
1253 /* possibly different values */
1254 opts
= sa_get_optionset((sa_group_t
)
1255 share
, list
->fstype
);
1256 (void) sa_destroy_optionset(opts
);
1258 for (secs
= sa_get_security(
1259 (sa_group_t
)share
, NULL
, list
->fstype
);
1261 secs
= sa_get_security((sa_group_t
)share
,
1262 NULL
, list
->fstype
)) {
1263 (void) sa_destroy_security(
1266 (void) sa_parse_legacy_options(share
,
1267 list
->options
, list
->fstype
);
1269 sa_format_free(oldprops
);
1272 dfs_free_list(head
);
1276 * legacy_removes(group, file)
1278 * Find any shares that are "missing" from the legacy file. These
1279 * should be removed from the configuration since they are likely from
1280 * a legacy app or the admin modified the dfstab file directly. We
1281 * have to support this even if it is not the recommended way to do
1286 legacy_removes(sa_group_t group
, char *file
)
1290 xfs_sharelist_t
*list
, *item
;
1293 dfstab
= fopen(file
, "r");
1294 if (dfstab
!= NULL
) {
1295 list
= getdfstab(dfstab
);
1296 (void) fclose(dfstab
);
1298 for (share
= sa_get_share(group
, NULL
);
1300 share
= sa_get_next_share(share
)) {
1301 /* now see if the share is in the dfstab file */
1302 path
= sa_get_share_attr(share
, "path");
1304 item
= finddfsentry(list
, path
);
1305 sa_free_attr_string(path
);
1307 /* The share was removed this way */
1308 (void) sa_remove_share(share
);
1311 * Start over since the list was broken
1318 dfs_free_list(list
);
1323 * getlegacyconfig(path, root)
1325 * Parse dfstab and build the legacy configuration. This only gets
1326 * called when a change was detected.
1330 getlegacyconfig(sa_handle_t handle
, char *path
, xmlNodePtr
*root
)
1332 sa_group_t defgroup
;
1336 *root
= xmlNewNode(NULL
, (xmlChar
*)"sharecfg");
1337 if (*root
!= NULL
) {
1338 if (strcmp(path
, SA_LEGACY_DFSTAB
) == 0) {
1340 * Walk the default shares and find anything
1341 * missing. we do this first to make sure it
1342 * is cleaned up since there may be legacy
1343 * code add/del via dfstab and we need to
1346 defgroup
= sa_get_group(handle
, "default");
1347 if (defgroup
!= NULL
)
1348 legacy_removes(defgroup
, path
);
1349 /* Parse the dfstab and add anything new */
1350 parse_dfstab(handle
, path
, *root
);
1357 * get_share_list(&err)
1359 * Get a linked list of all the shares on the system from
1360 * /etc/dfs/sharetab. This is partially copied from libfsmgt which we
1361 * can't use due to package dependencies.
1363 static xfs_sharelist_t
*
1364 get_share_list(int *errp
)
1366 xfs_sharelist_t
*newp
;
1367 xfs_sharelist_t
*headp
;
1368 xfs_sharelist_t
*tailp
;
1374 if ((fp
= fopen(SHARETAB
, "r")) != NULL
) {
1375 struct share
*sharetab_entry
;
1377 (void) lockf(fileno(fp
), F_LOCK
, 0);
1378 (void) mutex_lock(&sharetab_lock
);
1380 while (getshare(fp
, &sharetab_entry
) > 0) {
1381 newp
= alloc_sharelist();
1383 (void) mutex_unlock(&sharetab_lock
);
1384 (void) lockf(fileno(fp
), F_ULOCK
, 0);
1389 * Link into the list here so we don't leak
1390 * memory on a failure from strdup().
1392 if (headp
== NULL
) {
1400 newp
->path
= strdup(sharetab_entry
->sh_path
);
1401 newp
->resource
= strdup(sharetab_entry
->sh_res
);
1402 newp
->fstype
= strdup(sharetab_entry
->sh_fstype
);
1403 newp
->options
= strdup(sharetab_entry
->sh_opts
);
1404 newp
->description
= strdup(sharetab_entry
->sh_descr
);
1406 if (newp
->path
== NULL
|| newp
->resource
== NULL
||
1407 newp
->fstype
== NULL
|| newp
->options
== NULL
||
1408 newp
->description
== NULL
) {
1409 (void) mutex_unlock(&sharetab_lock
);
1410 (void) lockf(fileno(fp
), F_ULOCK
, 0);
1415 (void) mutex_unlock(&sharetab_lock
);
1416 (void) lockf(fileno(fp
), F_ULOCK
, 0);
1423 * Caller must free the mount list
1428 * Out of memory so cleanup and leave.
1430 dfs_free_list(headp
);
1436 * parse_sharetab_impl(handle, tmplist, lgroup)
1438 * Read the /etc/dfs/sharetab file and see which entries don't exist
1439 * in the repository. These shares are marked transient. We also need
1440 * to see if they are ZFS shares since ZFS bypasses the SMF
1443 * The return value is used to contribute to values indicating if it is a legacy
1444 * share. Right now this is 0 or 1, but as multiple systems become legacy that
1449 parse_sharetab_impl(sa_handle_t handle
, xfs_sharelist_t
*tmplist
,
1457 char shareopts
[MAXNAMLEN
];
1460 share
= sa_find_share(handle
, tmplist
->path
);
1461 if (share
!= NULL
) {
1463 * If this is a legacy share, mark as shared so we only update
1464 * sharetab appropriately. We also keep the sharetab options in
1465 * order to display for legacy share with no arguments.
1467 set_node_attr(share
, "shared", "true");
1468 (void) snprintf(shareopts
, MAXNAMLEN
, "shareopts-%s",
1470 set_node_attr(share
, shareopts
, tmplist
->options
);
1475 * This share is transient so needs to be added. Initially, this will be
1476 * under default(legacy) unless it is a ZFS share. If zfs, we need a zfs
1479 if (tmplist
->resource
!= NULL
&&
1480 (groupname
= strchr(tmplist
->resource
, '@')) != NULL
) {
1481 /* There is a defined group */
1482 *groupname
++ = '\0';
1483 group
= sa_get_group(handle
, groupname
);
1484 if (group
!= NULL
) {
1485 share
= _sa_add_share(group
, tmplist
->path
,
1486 SA_SHARE_TRANSIENT
, &err
,
1487 (uint64_t)SA_FEATURE_NONE
);
1490 * While this case shouldn't occur very often, it does
1491 * occur out of a "zfs set sharenfs=off" when the
1492 * dataset is also set to canmount=off. A warning will
1493 * then cause the zfs command to abort. Since we add it
1494 * to the default list, everything works properly anyway
1495 * and the library doesn't need to give a warning.
1497 share
= _sa_add_share(lgroup
,
1498 tmplist
->path
, SA_SHARE_TRANSIENT
,
1499 &err
, (uint64_t)SA_FEATURE_NONE
);
1502 if (sa_zfs_is_shared(handle
, tmplist
->path
)) {
1503 group
= sa_get_group(handle
, "zfs");
1504 if (group
== NULL
) {
1505 group
= sa_create_group(handle
,
1507 if (group
== NULL
&&
1508 err
== SA_NO_PERMISSION
) {
1509 group
= _sa_create_group(
1514 if (group
!= NULL
) {
1515 (void) sa_create_optionset(
1516 group
, tmplist
->fstype
);
1517 (void) sa_set_group_attr(group
,
1521 if (group
!= NULL
) {
1522 share
= _sa_add_share(group
,
1523 tmplist
->path
, SA_SHARE_TRANSIENT
,
1524 &err
, (uint64_t)SA_FEATURE_NONE
);
1527 share
= _sa_add_share(lgroup
, tmplist
->path
,
1528 SA_SHARE_TRANSIENT
, &err
,
1529 (uint64_t)SA_FEATURE_NONE
);
1533 (void) printf(dgettext(TEXT_DOMAIN
,
1534 "Problem with transient: %s\n"), sa_errorstr(err
));
1536 set_node_attr(share
, "shared", "true");
1538 if (tmplist
->options
!= NULL
&&
1539 strlen(tmplist
->options
) > 0) {
1540 (void) sa_parse_legacy_options(share
,
1541 tmplist
->options
, tmplist
->fstype
);
1543 if (tmplist
->resource
!= NULL
&&
1544 strcmp(tmplist
->resource
, "-") != 0)
1545 set_node_attr(share
, "resource",
1547 if (tmplist
->description
!= NULL
) {
1549 node
= xmlNewChild((xmlNodePtr
)share
, NULL
,
1550 (xmlChar
*)"description", NULL
);
1551 xmlNodeSetContent(node
,
1552 (xmlChar
*)tmplist
->description
);
1560 * Given an array of paths, parses the sharetab in /etc/dfs/sharetab looking for
1561 * matches to each path that could be transients from that file.
1563 * parse_sharetab_impl has more information on what exactly it is looking for.
1566 parse_sharetab_for_paths(sa_handle_t handle
, char **paths
, size_t paths_len
)
1571 xfs_sharelist_t
*list
= get_share_list(&err
);
1576 lgroup
= sa_get_group(handle
, "default");
1577 for (int i
= 0; i
< paths_len
; ++i
) {
1578 xfs_sharelist_t
*tmplist
;
1580 for (tmplist
= list
; tmplist
!= NULL
; tmplist
= tmplist
->next
) {
1581 if (strcmp(tmplist
->path
, paths
[i
]) == 0) {
1585 if (tmplist
== NULL
) {
1589 legacy
= parse_sharetab_impl(handle
, tmplist
, lgroup
);
1592 dfs_free_list(list
);
1597 * Runs parse_sharetab_impl on all the different share lists on the system.
1600 parse_sharetab(sa_handle_t handle
)
1605 xfs_sharelist_t
*list
= get_share_list(&err
);
1607 list
= get_share_list(&err
);
1611 lgroup
= sa_get_group(handle
, "default");
1613 for (xfs_sharelist_t
*tmplist
= list
; tmplist
!= NULL
;
1614 tmplist
= tmplist
->next
) {
1615 legacy
|= parse_sharetab_impl(handle
, tmplist
, lgroup
);
1617 dfs_free_list(list
);
1622 * This is the same as gettransients except when parsing sharetab the entries
1623 * are only processed if the path matches an element of paths.
1626 get_one_transient(sa_handle_impl_t ihandle
, xmlNodePtr
*root
, char **paths
,
1630 char **protocols
= NULL
;
1634 *root
= xmlNewNode(NULL
, (xmlChar
*)"sharecfg");
1635 if (*root
!= NULL
) {
1636 legacy
= parse_sharetab_for_paths(ihandle
, paths
,
1638 int numproto
= sa_get_protocols(&protocols
);
1639 for (int i
= 0; i
< numproto
; i
++)
1640 legacy
|= sa_proto_get_transients(
1641 (sa_handle_t
)ihandle
, protocols
[i
]);
1642 if (protocols
!= NULL
)
1650 * Get the transient shares from the sharetab (or other) file. Since
1651 * these are transient, they only appear in the working file and not
1655 gettransients(sa_handle_impl_t ihandle
, xmlNodePtr
*root
)
1659 char **protocols
= NULL
;
1664 *root
= xmlNewNode(NULL
, (xmlChar
*)"sharecfg");
1665 if (*root
!= NULL
) {
1666 legacy
= parse_sharetab(ihandle
);
1667 numproto
= sa_get_protocols(&protocols
);
1668 for (i
= 0; i
< numproto
; i
++)
1669 legacy
|= sa_proto_get_transients(
1670 (sa_handle_t
)ihandle
, protocols
[i
]);
1671 if (protocols
!= NULL
)
1679 * sa_has_prop(optionset, prop)
1681 * Is the specified property a member of the optionset?
1685 sa_has_prop(sa_optionset_t optionset
, sa_property_t prop
)
1688 sa_property_t otherprop
;
1691 if (optionset
!= NULL
) {
1692 name
= sa_get_property_attr(prop
, "type");
1694 otherprop
= sa_get_property(optionset
, name
);
1695 if (otherprop
!= NULL
)
1697 sa_free_attr_string(name
);
1704 * Update legacy files
1706 * Provides functions to add/remove/modify individual entries
1707 * in dfstab and sharetab
1711 update_legacy_config(sa_handle_t handle
)
1714 * no longer used -- this is a placeholder in case we need to
1715 * add it back later.
1723 * sa_valid_property(handle, object, proto, property)
1725 * check to see if the specified property is valid relative to the
1726 * specified protocol. The protocol plugin is called to do the work.
1730 sa_valid_property(sa_handle_t handle
, void *object
, char *proto
,
1731 sa_property_t property
)
1735 if (proto
!= NULL
&& property
!= NULL
) {
1736 ret
= sa_proto_valid_prop(handle
, proto
, property
, object
);
1745 * Given path, return the string representing the path's file system
1746 * type. This is used to discover ZFS shares.
1750 sa_fstype(char *path
)
1755 err
= stat(path
, &st
);
1757 err
= SA_NO_SUCH_PATH
;
1762 * If we have a valid path at this point ret, return the fstype.
1765 return (strdup(st
.st_fstype
));
1771 sa_free_fstype(char *type
)
1777 * sa_get_derived_optionset(object, proto, hier)
1779 * Work backward to the top of the share object tree and start
1780 * copying protocol specific optionsets into a newly created
1781 * optionset that doesn't have a parent (it will be freed
1782 * later). This provides for the property inheritance model. That
1783 * is, properties closer to the share take precedence over group
1784 * level. This also provides for groups of groups in the future.
1788 sa_get_derived_optionset(void *object
, char *proto
, int hier
)
1790 sa_optionset_t newoptionset
;
1791 sa_optionset_t optionset
;
1795 (group
= sa_get_parent_group((sa_share_t
)object
)) != NULL
) {
1796 newoptionset
= sa_get_derived_optionset((void *)group
, proto
,
1799 newoptionset
= (sa_optionset_t
)xmlNewNode(NULL
,
1800 (xmlChar
*)"optionset");
1801 if (newoptionset
!= NULL
) {
1802 sa_set_optionset_attr(newoptionset
, "type", proto
);
1805 /* Dont' do anything if memory wasn't allocated */
1806 if (newoptionset
== NULL
)
1809 /* Found the top so working back down the stack */
1810 optionset
= sa_get_optionset((sa_optionset_t
)object
, proto
);
1811 if (optionset
!= NULL
) {
1813 /* add optionset to the newoptionset */
1814 for (prop
= sa_get_property(optionset
, NULL
);
1816 prop
= sa_get_next_property(prop
)) {
1817 sa_property_t newprop
;
1820 name
= sa_get_property_attr(prop
, "type");
1821 value
= sa_get_property_attr(prop
, "value");
1824 newprop
= sa_get_property(newoptionset
, name
);
1825 /* Replace the value with the new value */
1826 if (newprop
!= NULL
) {
1828 * Only set if value is non NULL, old value ok
1832 set_node_attr(newprop
, "value", value
);
1834 /* an entirely new property */
1835 if (value
!= NULL
) {
1836 newprop
= sa_create_property(name
,
1838 if (newprop
!= NULL
) {
1839 newprop
= (sa_property_t
)
1841 (xmlNodePtr
)newoptionset
,
1842 (xmlNodePtr
)newprop
);
1846 sa_free_attr_string(name
);
1849 sa_free_attr_string(value
);
1852 return (newoptionset
);
1856 sa_free_derived_optionset(sa_optionset_t optionset
)
1858 /* While it shouldn't be linked, it doesn't hurt */
1859 if (optionset
!= NULL
) {
1860 xmlUnlinkNode((xmlNodePtr
) optionset
);
1861 xmlFreeNode((xmlNodePtr
) optionset
);
1866 * sa_get_all_security_types(object, proto, hier)
1868 * Find all the security types set for this object. This is
1869 * preliminary to getting a derived security set. The return value is an
1870 * optionset containg properties which are the sectype values found by
1871 * walking up the XML document structure. The returned optionset
1872 * is a derived optionset.
1874 * If hier is 0, only look at object. If non-zero, walk up the tree.
1877 sa_get_all_security_types(void *object
, char *proto
, int hier
)
1879 sa_optionset_t options
;
1880 sa_security_t security
;
1887 (group
= sa_get_parent_group((sa_share_t
)object
)) != NULL
)
1888 options
= sa_get_all_security_types((void *)group
, proto
, hier
);
1890 options
= (sa_optionset_t
)xmlNewNode(NULL
,
1891 (xmlChar
*)"optionset");
1893 if (options
== NULL
)
1896 /* Hit the top so collect the security types working back. */
1897 for (security
= sa_get_security((sa_group_t
)object
, NULL
, NULL
);
1899 security
= sa_get_next_security(security
)) {
1903 type
= sa_get_security_attr(security
, "type");
1905 if (strcmp(type
, proto
) != 0) {
1906 sa_free_attr_string(type
);
1909 sectype
= sa_get_security_attr(security
, "sectype");
1910 if (sectype
!= NULL
) {
1912 * Have a security type, check to see if
1913 * already present in optionset and add if it
1916 if (sa_get_property(options
, sectype
) == NULL
) {
1917 prop
= sa_create_property(sectype
,
1920 prop
= (sa_property_t
)
1922 (xmlNodePtr
)options
,
1925 sa_free_attr_string(sectype
);
1927 sa_free_attr_string(type
);
1935 * sa_get_derived_security(object, sectype, proto, hier)
1937 * Get the derived security(named optionset) for the object given the
1938 * sectype and proto. If hier is non-zero, walk up the tree to get all
1939 * properties defined for this object, otherwise just those on the
1944 sa_get_derived_security(void *object
, char *sectype
, char *proto
, int hier
)
1946 sa_security_t newsecurity
;
1947 sa_security_t security
;
1952 (group
= sa_get_parent_group((sa_share_t
)object
)) != NULL
) {
1953 newsecurity
= sa_get_derived_security((void *)group
,
1954 sectype
, proto
, hier
);
1956 newsecurity
= (sa_security_t
)xmlNewNode(NULL
,
1957 (xmlChar
*)"security");
1958 if (newsecurity
!= NULL
) {
1959 sa_set_security_attr(newsecurity
, "type", proto
);
1960 sa_set_security_attr(newsecurity
, "sectype", sectype
);
1963 /* Don't do anything if memory wasn't allocated */
1964 if (newsecurity
== NULL
)
1965 return (newsecurity
);
1967 /* Found the top so working back down the stack. */
1968 security
= sa_get_security((sa_security_t
)object
, sectype
, proto
);
1969 if (security
== NULL
)
1970 return (newsecurity
);
1972 /* add security to the newsecurity */
1973 for (prop
= sa_get_property(security
, NULL
);
1974 prop
!= NULL
; prop
= sa_get_next_property(prop
)) {
1975 sa_property_t newprop
;
1978 name
= sa_get_property_attr(prop
, "type");
1979 value
= sa_get_property_attr(prop
, "value");
1981 newprop
= sa_get_property(newsecurity
, name
);
1982 /* Replace the value with the new value */
1983 if (newprop
!= NULL
) {
1985 * Only set if value is non NULL, old
1986 * value ok if it is NULL. The value
1987 * must be associated with the "value"
1991 set_node_attr(newprop
, "value", value
);
1993 /* An entirely new property */
1994 if (value
!= NULL
) {
1995 newprop
= sa_create_property(name
,
1997 newprop
= (sa_property_t
)
1998 xmlAddChild((xmlNodePtr
)newsecurity
,
1999 (xmlNodePtr
)newprop
);
2002 sa_free_attr_string(name
);
2005 sa_free_attr_string(value
);
2007 return (newsecurity
);
2011 sa_free_derived_security(sa_security_t security
)
2013 /* while it shouldn't be linked, it doesn't hurt */
2014 if (security
!= NULL
) {
2015 xmlUnlinkNode((xmlNodePtr
)security
);
2016 xmlFreeNode((xmlNodePtr
)security
);
2021 * sharetab utility functions
2023 * Makes use of the original sharetab.c from fs.d/nfs/lib
2027 * sa_fillshare(share, proto, sh)
2029 * Fill the struct share with values obtained from the share object.
2032 sa_fillshare(sa_share_t share
, char *proto
, struct share
*sh
)
2034 char *groupname
= NULL
;
2039 sa_resource_t resource
;
2040 char *rsrcname
= NULL
;
2044 * We only want to deal with the path level shares for the
2045 * sharetab file. If a resource, get the parent.
2047 if (sa_is_resource(share
)) {
2048 resource
= (sa_resource_t
)share
;
2049 share
= sa_get_resource_parent(resource
);
2050 rsrcname
= sa_get_resource_attr(resource
, "name");
2053 group
= sa_get_parent_group(share
);
2054 if (group
!= NULL
) {
2055 zfs
= sa_get_group_attr(group
, "zfs");
2056 groupname
= sa_get_group_attr(group
, "name");
2058 if (groupname
!= NULL
&&
2059 (strcmp(groupname
, "default") == 0 || zfs
!= NULL
)) {
2061 * since the groupname is either "default" or the
2062 * group is a ZFS group, we don't want to keep
2063 * groupname. We do want it if it is any other type of
2066 sa_free_attr_string(groupname
);
2070 sa_free_attr_string(zfs
);
2073 value
= sa_get_share_attr(share
, "path");
2074 if (value
!= NULL
) {
2075 sh
->sh_path
= strdup(value
);
2076 sa_free_attr_string(value
);
2079 if (rsrcname
!= NULL
|| groupname
!= NULL
) {
2082 if (rsrcname
!= NULL
)
2083 len
+= strlen(rsrcname
);
2084 if (groupname
!= NULL
)
2085 len
+= strlen(groupname
);
2086 len
+= 3; /* worst case */
2088 (void) snprintf(buff
, len
, "%s%s%s",
2089 (rsrcname
!= NULL
&&
2090 strlen(rsrcname
) > 0) ? rsrcname
: "-",
2091 groupname
!= NULL
? "@" : "",
2092 groupname
!= NULL
? groupname
: "");
2094 if (rsrcname
!= NULL
)
2095 sa_free_attr_string(rsrcname
);
2096 if (groupname
!= NULL
)
2097 sa_free_attr_string(groupname
);
2099 sh
->sh_res
= strdup("-");
2103 * Get correct default prop string. NFS uses "rw", others use
2106 if (strcmp(proto
, "nfs") != 0)
2111 sh
->sh_fstype
= strdup(proto
);
2112 value
= sa_proto_legacy_format(proto
, share
, 1);
2113 if (value
!= NULL
) {
2114 if (strlen(value
) > 0)
2115 sh
->sh_opts
= strdup(value
);
2117 sh
->sh_opts
= strdup(defprop
);
2120 sh
->sh_opts
= strdup(defprop
);
2123 value
= sa_get_share_description(share
);
2124 if (value
!= NULL
) {
2125 sh
->sh_descr
= strdup(value
);
2126 sa_free_share_description(value
);
2128 sh
->sh_descr
= strdup("");
2135 * Free the strings in the non-NULL members of sh.
2139 sa_emptyshare(struct share
*sh
)
2141 if (sh
->sh_path
!= NULL
)
2144 if (sh
->sh_res
!= NULL
)
2147 if (sh
->sh_fstype
!= NULL
)
2148 free(sh
->sh_fstype
);
2149 sh
->sh_fstype
= NULL
;
2150 if (sh
->sh_opts
!= NULL
)
2153 if (sh
->sh_descr
!= NULL
)
2155 sh
->sh_descr
= NULL
;
2159 * sa_update_sharetab_ts(handle)
2161 * Update the internal timestamp of when sharetab was last
2162 * changed. This needs to be public for ZFS to get at it.
2166 sa_update_sharetab_ts(sa_handle_t handle
)
2169 sa_handle_impl_t implhandle
= (sa_handle_impl_t
)handle
;
2171 if (implhandle
!= NULL
&& stat(SA_LEGACY_SHARETAB
, &st
) == 0)
2172 implhandle
->tssharetab
= TSTAMP(st
.st_mtim
);
2176 * sa_update_sharetab(share, proto)
2178 * Update the sharetab file with info from the specified share.
2179 * This could be an update or add.
2183 sa_update_sharetab(sa_share_t share
, char *proto
)
2190 path
= sa_get_share_attr(share
, "path");
2192 (void) memset(&sh
, '\0', sizeof (sh
));
2194 handle
= sa_find_group_handle((sa_group_t
)share
);
2195 if (handle
!= NULL
) {
2197 * Fill in share structure and send it to the kernel.
2199 (void) sa_fillshare(share
, proto
, &sh
);
2200 (void) _sharefs(SHAREFS_ADD
, &sh
);
2202 * We need the timestamp of the sharetab file right
2203 * after the update was done. This lets us detect a
2204 * change that made by a different process.
2206 sa_update_sharetab_ts(handle
);
2209 ret
= SA_CONFIG_ERR
;
2211 sa_free_attr_string(path
);
2218 * sa_delete_sharetab(handle, path, proto)
2220 * remove the specified share from sharetab.
2224 sa_delete_sharetab(sa_handle_t handle
, char *path
, char *proto
)
2231 * Both the path and the proto are
2232 * keys into the sharetab.
2234 if (path
!= NULL
&& proto
!= NULL
) {
2235 (void) memset(&sh
, '\0', sizeof (sh
));
2237 sh
.sh_fstype
= proto
;
2239 ret
= _sharefs(SHAREFS_REMOVE
, &sh
);
2240 if (handle
!= NULL
&& stat(SA_LEGACY_SHARETAB
, &st
) == 0)
2241 sa_update_sharetab_ts(handle
);
2247 * sa_needs_refresh(handle)
2249 * Returns B_TRUE if the internal cache needs to be refreshed due to a
2250 * change by another process. B_FALSE returned otherwise.
2253 sa_needs_refresh(sa_handle_t handle
)
2255 sa_handle_impl_t implhandle
= (sa_handle_impl_t
)handle
;
2259 scf_simple_prop_t
*prop
;
2261 if (handle
== NULL
|| implhandle
->sa_service
&
2262 (SA_INIT_ONE_SHARE_FROM_NAME
| SA_INIT_ONE_SHARE_FROM_HANDLE
))
2266 * If sharetab has changed, then there was an external
2267 * change. Check sharetab first since it is updated by ZFS as
2268 * well as sharemgr. This is where external ZFS changes are
2271 if (stat(SA_LEGACY_SHARETAB
, &st
) == 0 &&
2272 TSTAMP(st
.st_mtim
) != implhandle
->tssharetab
)
2276 * If sharetab wasn't changed, check whether there were any
2277 * SMF transactions that modified the config but didn't
2278 * initiate a share. This is less common but does happen.
2280 prop
= scf_simple_prop_get(implhandle
->scfhandle
->handle
,
2281 (const char *)SA_SVC_FMRI_BASE
":default", "state",
2284 str
= scf_simple_prop_next_astring(prop
);
2286 tstamp
= strtoull(str
, NULL
, 0);
2289 scf_simple_prop_free(prop
);
2290 if (tstamp
!= implhandle
->tstrans
)
2298 * sa_fix_resource_name(path)
2300 * Convert invalid characters in a resource name (SMB share name)
2301 * to underscores ('_'). The list of invalid characters includes
2302 * control characters and the following:
2304 * " / \ [ ] : | < > + ; , ? * =
2306 * The caller must pass a valid path. Leading and trailing slashes
2307 * are stripped from the path before converting invalid characters.
2308 * Resource names are restricted to SA_MAX_RESOURCE_NAME characters.
2311 sa_fix_resource_name(char *path
)
2313 char *invalid
= "\"/\\[]:|<>+;,?*=";
2318 assert(path
!= NULL
);
2321 * Strip leading and trailing /'s.
2323 p
+= strspn(p
, "/");
2324 q
= strchr(p
, '\0');
2325 if (q
!= NULL
&& q
!= path
) {
2326 while ((--q
, *q
== '/'))
2331 (void) strcpy(path
, "_");
2336 * Stride over path components until the remaining
2337 * path is no longer than SA_MAX_RESOURCE_NAME.
2340 while ((q
!= NULL
) && (strlen(q
) > SA_MAX_RESOURCE_NAME
)) {
2341 if ((q
= strchr(q
, '/')) != NULL
) {
2348 * If the path is still longer than SA_MAX_RESOURCE_NAME,
2349 * take the trailing SA_MAX_RESOURCE_NAME characters.
2351 if ((len
= strlen(p
)) > SA_MAX_RESOURCE_NAME
) {
2352 len
= SA_MAX_RESOURCE_NAME
;
2353 p
= strchr(p
, '\0') - (SA_MAX_RESOURCE_NAME
- 1);
2356 (void) memmove(path
, p
, len
);
2359 for (p
= path
; *p
!= '\0'; ++p
) {
2360 if ((iscntrl(*p
)) || strchr(invalid
, *p
))