dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / lib / libshare / common / libsharecore.c
blob8a63b24d6ebeb5a3692fa5efccf79c629d04f9ce
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
23 * Copyright (c) 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.
34 #include <stdio.h>
35 #include <string.h>
36 #include <ctype.h>
37 #include <unistd.h>
38 #include <limits.h>
39 #include <errno.h>
40 #include <sys/types.h>
41 #include <sys/stat.h>
42 #include <libxml/parser.h>
43 #include <libxml/tree.h>
44 #include "libshare.h"
45 #include "libshare_impl.h"
46 #include <fcntl.h>
47 #include <thread.h>
48 #include <grp.h>
49 #include <limits.h>
50 #include <sys/param.h>
51 #include <signal.h>
52 #include <libintl.h>
53 #include <dirent.h>
55 #include <sharefs/share.h>
56 #include "sharetab.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",
64 "#\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 */
76 typedef
77 struct sharelist {
78 struct sharelist *next;
79 int persist;
80 char *path;
81 char *resource;
82 char *fstype;
83 char *options;
84 char *description;
85 char *group;
86 char *origline;
87 int lineno;
88 } xfs_sharelist_t;
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 *);
92 /* prototypes */
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 *);
102 * sablocksigs(*sigs)
104 * block important signals for a critical region. Arg is a pointer to
105 * a sigset_t that is used later for the unblock.
107 void
108 sablocksigs(sigset_t *sigs)
110 sigset_t new;
112 if (sigs != NULL) {
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.
127 void
128 saunblocksigs(sigset_t *sigs)
130 if (sigs != NULL)
131 (void) sigprocmask(SIG_SETMASK, sigs, NULL);
135 * alloc_sharelist()
137 * allocator function to return an zfs_sharelist_t
140 static xfs_sharelist_t *
141 alloc_sharelist()
143 xfs_sharelist_t *item;
145 item = (xfs_sharelist_t *)malloc(sizeof (xfs_sharelist_t));
146 if (item != NULL)
147 (void) memset(item, '\0', sizeof (xfs_sharelist_t));
148 return (item);
152 * fix_notice(list)
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;
162 int i;
164 if (list == NULL) {
165 /* zero length dfstab */
166 list = alloc_sharelist();
167 if (list == NULL)
168 return (NULL);
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();
175 if (item != NULL) {
176 item->description = strdup(notice[i]);
177 if (prev == NULL) {
178 item->next = list;
179 prev = item;
180 list = item;
181 } else {
182 item->next = prev->next;
183 prev->next = item;
184 prev = item;
189 return (list);
193 * getdfstab(dfs)
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
198 * dfstab file.
201 static xfs_sharelist_t *
202 getdfstab(FILE *dfs)
204 char buff[_POSIX_ARG_MAX]; /* reasonable size given syntax of share */
205 char *bp;
206 char *token;
207 char *args[MAXARGSFORSHARE];
208 int argc;
209 int c;
210 static int line = 0;
211 xfs_sharelist_t *item = NULL, *first = NULL, *last;
213 if (dfs != NULL) {
214 first = NULL;
215 line = 0;
216 while (fgets(buff, sizeof (buff), dfs) != NULL) {
217 line++;
218 bp = buff;
219 if (buff[0] == '#') {
220 item = alloc_sharelist();
221 if (item != NULL) {
222 /* if no path, then comment */
223 item->lineno = line;
224 item->description = strdup(buff);
225 if (first == NULL) {
226 first = item;
227 last = item;
228 } else {
229 last->next = item;
230 last = item;
232 } else {
233 break;
235 continue;
236 } else if (buff[0] == '\n') {
237 continue;
239 optind = 1;
240 item = alloc_sharelist();
241 if (item == NULL) {
242 break;
243 } else if (first == NULL) {
244 first = item;
245 last = item;
246 } else {
247 last->next = item;
248 last = item;
250 item->lineno = line;
251 item->origline = strdup(buff);
252 (void) _sa_get_token(NULL); /* reset to new pointers */
253 argc = 0;
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) {
259 switch (c) {
260 case 'p':
261 item->persist = 1;
262 break;
263 case 'F':
264 item->fstype = strdup(optarg);
265 break;
266 case 'o':
267 item->options = strdup(optarg);
268 break;
269 case 'd':
270 item->description = strdup(optarg);
271 break;
272 case 'g':
273 item->group = strdup(optarg);
274 break;
275 default:
276 break;
279 if (optind < argc) {
280 item->path = strdup(args[optind]);
281 optind++;
282 if (optind < argc) {
283 char *resource;
284 char *optgroup;
285 /* resource and/or groupname */
286 resource = args[optind];
287 optgroup = strchr(resource, '@');
288 if (optgroup != NULL)
289 *optgroup++ = '\0';
290 if (optgroup != NULL)
291 item->group = strdup(optgroup);
292 if (resource != NULL &&
293 strlen(resource) > 0)
294 item->resource =
295 strdup(resource);
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);
304 return (first);
308 * finddfsentry(list, path)
310 * Look for path in the zfs_sharelist_t list and return the entry if it
311 * exists.
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)
321 return (item);
323 return (NULL);
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) {
342 prev = item;
343 continue;
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)))
348 break;
349 if (item->fstype == NULL &&
350 (proto == NULL || strcmp(proto, "nfs") == 0))
351 break;
352 prev = item;
354 if (item != NULL) {
355 if (item == prev)
356 list = item->next; /* this must be the first one */
357 else
358 prev->next = item->next;
359 item->next = NULL;
360 dfs_free_list(item);
362 return (list);
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) {
379 prev = item;
380 continue;
382 if (strcmp(item->origline, line) == 0)
383 break;
384 prev = item;
386 if (item != NULL) {
387 if (item == prev)
388 list = item->next; /* this must be the first one */
389 else
390 prev->next = item->next;
391 item->next = NULL;
392 dfs_free_list(item);
394 return (list);
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;
408 sa_group_t parent;
409 char *groupname;
411 item = alloc_sharelist();
412 if (item != NULL) {
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);
417 groupname = NULL;
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) {
425 free(item->options);
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;
434 if (list == NULL) {
435 list = item;
436 } else {
437 for (tmp = list; tmp->next != NULL; tmp = tmp->next)
438 /* do nothing */;
439 tmp->next = item;
442 return (list);
446 * outdfstab(dfstab, list)
448 * Output the list to dfstab making sure the file is truncated.
449 * Comments and errors are preserved.
452 static void
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) ?
468 item->options : "",
469 (item->description != NULL) ?
470 " -d \"" : "",
471 (item->description != NULL) ?
472 item->description : "",
473 (item->description != NULL) ? "\"" : "",
474 item->path,
475 ((item->resource != NULL) ||
476 (item->group != NULL)) ? " " : "",
477 (item->resource != NULL) ?
478 item->resource : "",
479 item->group != NULL ? "@" : "",
480 item->group != NULL ? item->group : "");
481 } else {
482 (void) fprintf(dfstab, "%s", item->origline);
484 } else {
485 if (item->description != NULL)
486 (void) fprintf(dfstab, "%s", item->description);
487 else
488 (void) fprintf(dfstab, "%s", item->origline);
494 * open_dfstab(file)
496 * Open the specified dfstab file. If the owner/group/perms are wrong,
497 * fix them.
500 static FILE *
501 open_dfstab(char *file)
503 struct group *grp;
504 struct group group;
505 char *buff;
506 int grsize;
507 FILE *dfstab;
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);
516 if (buff != NULL)
517 grp = getgrnam_r(SA_DEFAULT_FILE_GRP, &group, buff,
518 grsize);
519 else
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);
524 free(buff);
525 rewind(dfstab);
527 return (dfstab);
531 * sa_comment_line(line, err)
533 * Add a comment to the dfstab file with err as a prefix to the
534 * original line.
537 static void
538 sa_comment_line(char *line, char *err)
540 FILE *dfstab;
541 xfs_sharelist_t *list;
542 sigset_t old;
544 dfstab = open_dfstab(SA_LEGACY_DFSTAB);
545 if (dfstab != NULL) {
546 (void) setvbuf(dfstab, NULL, _IOLBF, BUFSIZ * 8);
547 sablocksigs(&old);
548 (void) lockf(fileno(dfstab), F_LOCK, 0);
549 (void) mutex_lock(&sa_dfstab_lock);
550 list = getdfstab(dfstab);
551 rewind(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);
563 saunblocksigs(&old);
564 if (list != NULL)
565 dfs_free_list(list);
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)
578 FILE *dfstab;
579 int err;
580 int ret = SA_OK;
581 xfs_sharelist_t *list;
582 char *path;
583 sa_optionset_t optionset;
584 sa_group_t parent;
585 sigset_t old;
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");
592 if (path == NULL)
593 return (ret);
595 dfstab = open_dfstab(SA_LEGACY_DFSTAB);
596 if (dfstab != NULL) {
597 (void) setvbuf(dfstab, NULL, _IOLBF, BUFSIZ * 8);
598 sablocksigs(&old);
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);
604 rewind(dfstab);
605 if (protocol != NULL) {
606 if (list != NULL)
607 list = remdfsentry(list, path,
608 protocol);
609 } else {
610 for (optionset = sa_get_optionset(parent, NULL);
611 optionset != NULL;
612 optionset =
613 sa_get_next_optionset(optionset)) {
614 char *proto = sa_get_optionset_attr(
615 optionset, "type");
617 if (list != NULL && proto != NULL)
618 list = remdfsentry(list, path,
619 proto);
620 if (proto == NULL)
621 ret = SA_NO_MEMORY;
623 * may want to only do the dfstab if
624 * this call returns NOT IMPLEMENTED
625 * but it shouldn't hurt.
627 if (ret == SA_OK) {
628 err = sa_proto_delete_legacy(
629 proto, share);
630 if (err != SA_NOT_IMPLEMENTED)
631 ret = err;
633 if (proto != NULL)
634 sa_free_attr_string(proto);
637 outdfstab(dfstab, list);
638 if (list != NULL)
639 dfs_free_list(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));
645 saunblocksigs(&old);
646 (void) fclose(dfstab);
647 } else {
648 if (errno == EACCES || errno == EPERM)
649 ret = SA_NO_PERMISSION;
650 else
651 ret = SA_CONFIG_ERR;
654 if (path != NULL)
655 sa_free_attr_string(path);
657 return (ret);
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)
676 FILE *dfstab;
677 int ret = SA_OK;
678 xfs_sharelist_t *list;
679 char *path;
680 sigset_t old;
681 char *persist;
682 uint64_t features;
684 ret = sa_proto_update_legacy(proto, share);
685 if (ret != SA_NOT_IMPLEMENTED)
686 return (ret);
688 features = sa_proto_get_featureset(proto);
689 if (!(features & SA_FEATURE_DFSTAB))
690 return (ret);
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");
700 if (path == NULL) {
701 ret = SA_NO_MEMORY;
702 goto out;
704 dfstab = open_dfstab(SA_LEGACY_DFSTAB);
705 if (dfstab != NULL) {
706 (void) setvbuf(dfstab, NULL, _IOLBF, BUFSIZ * 8);
707 sablocksigs(&old);
708 (void) lockf(fileno(dfstab), F_LOCK, 0);
709 (void) mutex_lock(&sa_dfstab_lock);
710 list = getdfstab(dfstab);
711 rewind(dfstab);
712 if (list != NULL)
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));
720 saunblocksigs(&old);
721 (void) fclose(dfstab);
722 if (list != NULL)
723 dfs_free_list(list);
724 } else {
725 if (errno == EACCES || errno == EPERM)
726 ret = SA_NO_PERMISSION;
727 else
728 ret = SA_CONFIG_ERR;
730 sa_free_attr_string(path);
732 out:
733 if (persist != NULL)
734 sa_free_attr_string(persist);
735 return (ret);
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)
748 int ret = 0;
749 if (proto != NULL)
750 ret = sa_proto_security_prop(proto, optname);
751 return (ret);
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.
761 static void
762 add_syntax_comment(xmlNodePtr root, char *line, char *err, int todfstab)
764 xmlNodePtr node;
766 node = xmlNewChild(root, NULL, (xmlChar *)"error", (xmlChar *)line);
767 if (node != NULL)
768 (void) xmlSetProp(node, (xmlChar *)"type", (xmlChar *)err);
769 if (todfstab)
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)
784 return (1);
786 return (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)
799 return (1);
801 return (0);
805 * _sa_remove_property(property)
807 * remove a property only from the document.
810 static void
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);
825 static sa_group_t
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",
834 NULL);
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
840 * this interface.
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");
846 } else {
847 xmlFreeNode(parent_node);
850 return (child_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.
860 static int
861 _sa_free_dummy_share(sa_share_t share)
863 xmlNodePtr node = (xmlNodePtr)share;
864 xmlNodePtr parent;
865 int ret = SA_OK;
866 char *name;
868 if (node != NULL) {
869 parent = node->parent;
870 name = (char *)xmlGetProp(node, (xmlChar *)"path");
871 if (name != NULL) {
872 /* Real shares always have a path but a dummy doesn't */
873 ret = SA_NOT_ALLOWED;
874 sa_free_attr_string(name);
875 } else {
877 * If there is a parent, do the free on that since
878 * xmlFreeNode is a recursive function and free's an
879 * child nodes.
881 if (parent != NULL) {
882 node = parent;
884 xmlUnlinkNode(node);
885 xmlFreeNode(node);
888 return (ret);
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
898 * share).
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;
910 sa_group_t parent;
911 int using_dummy = B_FALSE;
912 char *pvalue;
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
920 * handle this case.
922 if (group == NULL) {
923 group = (sa_group_t)_sa_create_dummy_share();
924 using_dummy = B_TRUE;
927 parent = sa_get_parent_group(group);
929 if (proto != NULL)
930 ret = sa_proto_legacy_opts(proto, group, options);
932 if (using_dummy) {
933 /* Since this is a dummy parse, cleanup and quit here */
934 (void) _sa_free_dummy_share(parent);
935 return (ret);
938 if (ret != SA_OK)
939 return (ret);
942 * If in a group, remove the inherited options and security
945 if (parent == NULL)
946 return (ret);
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);
953 popt != NULL;
954 popt = sa_get_next_property(popt)) {
955 char *tag;
956 char *value;
957 tag = sa_get_property_attr(popt, "type");
958 if (tag == NULL)
959 continue;
960 prop = sa_get_property(localoptions, tag);
961 if (prop != NULL) {
962 value = sa_get_property_attr(popt,
963 "value");
964 pvalue = sa_get_property_attr(prop,
965 "value");
966 if (value != NULL && pvalue != NULL &&
967 strcmp(value, pvalue) == 0) {
969 * Remove the property
970 * from the
971 * child. While we
972 * removed it, we
973 * don't need to reset
974 * as we do below
975 * since we always
976 * search from the
977 * beginning.
979 (void) _sa_remove_property(
980 prop);
982 if (value != NULL)
983 sa_free_attr_string(value);
984 if (pvalue != NULL)
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
1003 * affected.
1005 localoptions = sa_get_all_security_types(group, proto, 0);
1006 if (localoptions != NULL) {
1007 for (prop = sa_get_property(localoptions, NULL);
1008 prop != NULL;
1009 prop = sa_get_next_property(prop)) {
1010 char *tag;
1011 sa_security_t security;
1012 tag = sa_get_property_attr(prop, "type");
1013 if (tag != NULL) {
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);
1022 popt != NULL;
1023 popt = nextpopt) {
1024 char *value;
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,
1033 "value");
1034 if (value != NULL && pvalue != NULL &&
1035 strcmp(value, pvalue) == 0) {
1037 * remove the property
1038 * from the child
1040 (void) _sa_remove_property
1041 (popt);
1043 if (value != NULL)
1044 sa_free_attr_string(value);
1046 if (pvalue != NULL)
1047 sa_free_attr_string(pvalue);
1050 (void) sa_destroy_optionset(localoptions);
1052 return (ret);
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.
1063 static void
1064 dfs_free_list(xfs_sharelist_t *list)
1066 xfs_sharelist_t *entry;
1067 for (entry = list; entry != NULL; entry = list) {
1068 free(entry->path);
1069 free(entry->resource);
1070 free(entry->fstype);
1071 free(entry->options);
1072 free(entry->description);
1073 free(entry->origline);
1074 free(entry->group);
1075 list = list->next;
1076 free(entry);
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.
1088 static void
1089 parse_dfstab(sa_handle_t handle, char *dfstab, xmlNodePtr root)
1091 sa_share_t share;
1092 sa_group_t group;
1093 sa_group_t sgroup = NULL;
1094 sa_group_t defgroup;
1095 xfs_sharelist_t *head, *list;
1096 int err;
1097 int defined_group;
1098 FILE *dfs;
1099 char *oldprops;
1101 /* read the dfstab format file and fill in the doc tree */
1103 dfs = fopen(dfstab, "r");
1104 if (dfs == NULL)
1105 return;
1107 defgroup = sa_get_group(handle, "default");
1109 for (head = list = getdfstab(dfs);
1110 list != NULL;
1111 list = list->next) {
1112 share = NULL;
1113 group = NULL;
1114 defined_group = 0;
1115 err = 0;
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) {
1126 char *line;
1127 char *error;
1128 char *cmd;
1129 line = strdup(list->description);
1130 if (line != NULL) {
1131 error = line + 9;
1132 cmd = strchr(error, ':');
1133 if (cmd != NULL) {
1134 int len;
1135 *cmd = '\0';
1136 cmd += 2;
1137 len = strlen(cmd);
1138 cmd[len - 1] = '\0';
1139 add_syntax_comment(root, cmd,
1140 error, 0);
1142 free(line);
1145 continue;
1147 if (list->path != NULL && strlen(list->path) > 0 &&
1148 *list->path == '/') {
1149 share = sa_find_share(handle, list->path);
1150 if (share != NULL)
1151 sgroup = sa_get_parent_group(share);
1152 else
1153 sgroup = NULL;
1154 } else {
1155 (void) printf(dgettext(TEXT_DOMAIN,
1156 "No share specified in dfstab: "
1157 "line %d: %s\n"),
1158 list->lineno, list->origline);
1159 add_syntax_comment(root, list->origline,
1160 dgettext(TEXT_DOMAIN, "No share specified"), 1);
1161 continue;
1163 if (list->group != NULL && strlen(list->group) > 0) {
1164 group = sa_get_group(handle, list->group);
1165 defined_group = 1;
1166 } else {
1167 group = defgroup;
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"),
1176 continue;
1178 if (group == NULL) {
1179 /* Shouldn't happen unless an SMF error */
1180 err = SA_CONFIG_ERR;
1181 continue;
1183 if (share == NULL) {
1184 if (defined_group || group != defgroup)
1185 continue;
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,
1193 list->description);
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);
1202 } else {
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);
1209 else
1210 add_syntax_comment(root, list->origline,
1211 dgettext(TEXT_DOMAIN,
1212 "Path"), 1);
1213 continue;
1215 } else {
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);
1224 continue;
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,
1235 B_TRUE);
1237 if (oldprops == NULL)
1238 continue;
1240 if (list->options != NULL &&
1241 strcmp(oldprops, list->options) != 0) {
1242 sa_optionset_t opts;
1243 sa_security_t secs;
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);
1252 secs != NULL;
1253 secs = sa_get_security((sa_group_t)share,
1254 NULL, list->fstype)) {
1255 (void) sa_destroy_security(
1256 secs);
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
1274 * things.
1277 static void
1278 legacy_removes(sa_group_t group, char *file)
1280 sa_share_t share;
1281 char *path;
1282 xfs_sharelist_t *list, *item;
1283 FILE *dfstab;
1285 dfstab = fopen(file, "r");
1286 if (dfstab != NULL) {
1287 list = getdfstab(dfstab);
1288 (void) fclose(dfstab);
1289 retry:
1290 for (share = sa_get_share(group, NULL);
1291 share != 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");
1295 if (path != NULL) {
1296 item = finddfsentry(list, path);
1297 sa_free_attr_string(path);
1298 if (item == NULL) {
1299 /* The share was removed this way */
1300 (void) sa_remove_share(share);
1303 * Start over since the list was broken
1305 goto retry;
1309 if (list != NULL)
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.
1321 void
1322 getlegacyconfig(sa_handle_t handle, char *path, xmlNodePtr *root)
1324 sa_group_t defgroup;
1326 if (root != NULL) {
1327 if (*root == NULL)
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
1336 * cleanup SMF.
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;
1361 FILE *fp;
1363 headp = NULL;
1364 tailp = NULL;
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();
1374 if (newp == NULL) {
1375 (void) mutex_unlock(&sharetab_lock);
1376 (void) lockf(fileno(fp), F_ULOCK, 0);
1377 goto err;
1381 * Link into the list here so we don't leak
1382 * memory on a failure from strdup().
1384 if (headp == NULL) {
1385 headp = newp;
1386 tailp = newp;
1387 } else {
1388 tailp->next = newp;
1389 tailp = newp;
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);
1403 goto err;
1407 (void) mutex_unlock(&sharetab_lock);
1408 (void) lockf(fileno(fp), F_ULOCK, 0);
1409 (void) fclose(fp);
1410 } else {
1411 *errp = errno;
1415 * Caller must free the mount list
1417 return (headp);
1418 err:
1420 * Out of memory so cleanup and leave.
1422 dfs_free_list(headp);
1423 (void) fclose(fp);
1424 return (NULL);
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
1433 * repository.
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
1437 * could change.
1440 static int
1441 parse_sharetab_impl(sa_handle_t handle, xfs_sharelist_t *tmplist,
1442 sa_group_t lgroup)
1444 int err = 0;
1445 sa_share_t share;
1446 sa_group_t group;
1447 char *groupname;
1448 int legacy = 0;
1449 char shareopts[MAXNAMLEN];
1451 group = NULL;
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",
1461 tmplist->fstype);
1462 set_node_attr(share, shareopts, tmplist->options);
1463 return (1);
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
1469 * group.
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);
1480 } else {
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);
1493 } else {
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,
1498 "zfs", &err);
1499 if (group == NULL &&
1500 err == SA_NO_PERMISSION) {
1501 group = _sa_create_group(
1502 (sa_handle_impl_t)
1503 handle,
1504 "zfs");
1506 if (group != NULL) {
1507 (void) sa_create_optionset(
1508 group, tmplist->fstype);
1509 (void) sa_set_group_attr(group,
1510 "zfs", "true");
1513 if (group != NULL) {
1514 share = _sa_add_share(group,
1515 tmplist->path, SA_SHARE_TRANSIENT,
1516 &err, (uint64_t)SA_FEATURE_NONE);
1518 } else {
1519 share = _sa_add_share(lgroup, tmplist->path,
1520 SA_SHARE_TRANSIENT, &err,
1521 (uint64_t)SA_FEATURE_NONE);
1524 if (share == NULL)
1525 (void) printf(dgettext(TEXT_DOMAIN,
1526 "Problem with transient: %s\n"), sa_errorstr(err));
1527 if (share != NULL)
1528 set_node_attr(share, "shared", "true");
1529 if (err == SA_OK) {
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",
1538 tmplist->resource);
1539 if (tmplist->description != NULL) {
1540 xmlNodePtr node;
1541 node = xmlNewChild((xmlNodePtr)share, NULL,
1542 (xmlChar *)"description", NULL);
1543 xmlNodeSetContent(node,
1544 (xmlChar *)tmplist->description);
1546 legacy = 1;
1548 return (legacy);
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)
1560 int err = 0;
1561 int legacy = 0;
1562 sa_group_t lgroup;
1563 xfs_sharelist_t *list = get_share_list(&err);
1565 if (list == NULL)
1566 return (legacy);
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) {
1574 break;
1577 if (tmplist == NULL) {
1578 continue;
1581 legacy = parse_sharetab_impl(handle, tmplist, lgroup);
1584 dfs_free_list(list);
1585 return (legacy);
1589 * Runs parse_sharetab_impl on all the different share lists on the system.
1592 parse_sharetab(sa_handle_t handle)
1594 int err = 0;
1595 int legacy = 0;
1596 sa_group_t lgroup;
1597 xfs_sharelist_t *list = get_share_list(&err);
1599 list = get_share_list(&err);
1600 if (list == NULL)
1601 return (legacy);
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);
1610 return (legacy);
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,
1619 size_t paths_len)
1621 int legacy = 0;
1622 char **protocols = NULL;
1624 if (root != NULL) {
1625 if (*root == NULL)
1626 *root = xmlNewNode(NULL, (xmlChar *)"sharecfg");
1627 if (*root != NULL) {
1628 legacy = parse_sharetab_for_paths(ihandle, paths,
1629 paths_len);
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)
1635 free(protocols);
1638 return (legacy);
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
1644 * in a repository.
1647 gettransients(sa_handle_impl_t ihandle, xmlNodePtr *root)
1649 int legacy = 0;
1650 int numproto;
1651 char **protocols = NULL;
1652 int i;
1654 if (root != NULL) {
1655 if (*root == 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]);
1663 free(protocols);
1666 return (legacy);
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)
1678 char *name;
1679 sa_property_t otherprop;
1680 int result = 0;
1682 if (optionset != NULL) {
1683 name = sa_get_property_attr(prop, "type");
1684 if (name != NULL) {
1685 otherprop = sa_get_property(optionset, name);
1686 if (otherprop != NULL)
1687 result = 1;
1688 sa_free_attr_string(name);
1691 return (result);
1695 * Update legacy files
1697 * Provides functions to add/remove/modify individual entries
1698 * in dfstab and sharetab
1701 void
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)
1721 int ret = SA_OK;
1723 if (proto != NULL && property != NULL) {
1724 ret = sa_proto_valid_prop(handle, proto, property, object);
1727 return (ret);
1731 * sa_fstype(path)
1733 * Given path, return the string representing the path's file system
1734 * type. This is used to discover ZFS shares.
1737 char *
1738 sa_fstype(char *path)
1740 int err;
1741 struct stat st;
1743 err = stat(path, &st);
1744 if (err < 0)
1745 err = SA_NO_SUCH_PATH;
1746 else
1747 err = SA_OK;
1750 * If we have a valid path at this point ret, return the fstype.
1752 if (err == SA_OK)
1753 return (strdup(st.st_fstype));
1755 return (NULL);
1758 void
1759 sa_free_fstype(char *type)
1761 free(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.
1775 sa_optionset_t
1776 sa_get_derived_optionset(void *object, char *proto, int hier)
1778 sa_optionset_t newoptionset;
1779 sa_optionset_t optionset;
1780 sa_group_t group;
1782 if (hier &&
1783 (group = sa_get_parent_group((sa_share_t)object)) != NULL) {
1784 newoptionset = sa_get_derived_optionset((void *)group, proto,
1785 hier);
1786 } else {
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)
1795 return (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) {
1800 sa_property_t prop;
1801 /* add optionset to the newoptionset */
1802 for (prop = sa_get_property(optionset, NULL);
1803 prop != NULL;
1804 prop = sa_get_next_property(prop)) {
1805 sa_property_t newprop;
1806 char *name;
1807 char *value;
1808 name = sa_get_property_attr(prop, "type");
1809 value = sa_get_property_attr(prop, "value");
1810 if (name == NULL)
1811 continue;
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
1817 * if it is NULL.
1819 if (value != NULL)
1820 set_node_attr(newprop, "value", value);
1821 } else {
1822 /* an entirely new property */
1823 if (value != NULL) {
1824 newprop = sa_create_property(name,
1825 value);
1826 if (newprop != NULL) {
1827 newprop = (sa_property_t)
1828 xmlAddChild(
1829 (xmlNodePtr)newoptionset,
1830 (xmlNodePtr)newprop);
1834 sa_free_attr_string(name);
1836 if (value != NULL)
1837 sa_free_attr_string(value);
1840 return (newoptionset);
1843 void
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.
1864 sa_optionset_t
1865 sa_get_all_security_types(void *object, char *proto, int hier)
1867 sa_optionset_t options;
1868 sa_security_t security;
1869 sa_group_t group;
1870 sa_property_t prop;
1872 options = NULL;
1874 if (hier &&
1875 (group = sa_get_parent_group((sa_share_t)object)) != NULL)
1876 options = sa_get_all_security_types((void *)group, proto, hier);
1877 else
1878 options = (sa_optionset_t)xmlNewNode(NULL,
1879 (xmlChar *)"optionset");
1881 if (options == NULL)
1882 return (options);
1884 /* Hit the top so collect the security types working back. */
1885 for (security = sa_get_security((sa_group_t)object, NULL, NULL);
1886 security != NULL;
1887 security = sa_get_next_security(security)) {
1888 char *type;
1889 char *sectype;
1891 type = sa_get_security_attr(security, "type");
1892 if (type != NULL) {
1893 if (strcmp(type, proto) != 0) {
1894 sa_free_attr_string(type);
1895 continue;
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
1902 * isn't.
1904 if (sa_get_property(options, sectype) == NULL) {
1905 prop = sa_create_property(sectype,
1906 "true");
1907 if (prop != NULL)
1908 prop = (sa_property_t)
1909 xmlAddChild(
1910 (xmlNodePtr)options,
1911 (xmlNodePtr)prop);
1913 sa_free_attr_string(sectype);
1915 sa_free_attr_string(type);
1919 return (options);
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
1928 * object.
1931 sa_security_t
1932 sa_get_derived_security(void *object, char *sectype, char *proto, int hier)
1934 sa_security_t newsecurity;
1935 sa_security_t security;
1936 sa_group_t group;
1937 sa_property_t prop;
1939 if (hier &&
1940 (group = sa_get_parent_group((sa_share_t)object)) != NULL) {
1941 newsecurity = sa_get_derived_security((void *)group,
1942 sectype, proto, hier);
1943 } else {
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;
1964 char *name;
1965 char *value;
1966 name = sa_get_property_attr(prop, "type");
1967 value = sa_get_property_attr(prop, "value");
1968 if (name != NULL) {
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"
1976 * tag within XML.
1978 if (value != NULL)
1979 set_node_attr(newprop, "value", value);
1980 } else {
1981 /* An entirely new property */
1982 if (value != NULL) {
1983 newprop = sa_create_property(name,
1984 value);
1985 newprop = (sa_property_t)
1986 xmlAddChild((xmlNodePtr)newsecurity,
1987 (xmlNodePtr)newprop);
1990 sa_free_attr_string(name);
1992 if (value != NULL)
1993 sa_free_attr_string(value);
1995 return (newsecurity);
1998 void
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.
2019 void
2020 sa_fillshare(sa_share_t share, char *proto, struct share *sh)
2022 char *groupname = NULL;
2023 char *value;
2024 sa_group_t group;
2025 char *buff;
2026 char *zfs;
2027 sa_resource_t resource;
2028 char *rsrcname = NULL;
2029 char *defprop;
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
2052 * group.
2054 sa_free_attr_string(groupname);
2055 groupname = NULL;
2057 if (zfs != NULL)
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) {
2068 int len = 0;
2070 if (rsrcname != NULL)
2071 len += strlen(rsrcname);
2072 if (groupname != NULL)
2073 len += strlen(groupname);
2074 len += 3; /* worst case */
2075 buff = malloc(len);
2076 (void) snprintf(buff, len, "%s%s%s",
2077 (rsrcname != NULL &&
2078 strlen(rsrcname) > 0) ? rsrcname : "-",
2079 groupname != NULL ? "@" : "",
2080 groupname != NULL ? groupname : "");
2081 sh->sh_res = buff;
2082 if (rsrcname != NULL)
2083 sa_free_attr_string(rsrcname);
2084 if (groupname != NULL)
2085 sa_free_attr_string(groupname);
2086 } else {
2087 sh->sh_res = strdup("-");
2091 * Get correct default prop string. NFS uses "rw", others use
2092 * "".
2094 if (strcmp(proto, "nfs") != 0)
2095 defprop = "\"\"";
2096 else
2097 defprop = "rw";
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);
2104 else
2105 sh->sh_opts = strdup(defprop);
2106 free(value);
2107 } else {
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);
2115 } else {
2116 sh->sh_descr = strdup("");
2121 * sa_emptyshare(sh)
2123 * Free the strings in the non-NULL members of sh.
2126 void
2127 sa_emptyshare(struct share *sh)
2129 free(sh->sh_path);
2130 sh->sh_path = NULL;
2131 free(sh->sh_res);
2132 sh->sh_res = NULL;
2133 free(sh->sh_fstype);
2134 sh->sh_fstype = NULL;
2135 free(sh->sh_opts);
2136 sh->sh_opts = NULL;
2137 free(sh->sh_descr);
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.
2148 void
2149 sa_update_sharetab_ts(sa_handle_t handle)
2151 struct stat st;
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)
2168 int ret = SA_OK;
2169 share_t sh;
2170 char *path;
2171 sa_handle_t handle;
2173 path = sa_get_share_attr(share, "path");
2174 if (path != NULL) {
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);
2190 sa_emptyshare(&sh);
2191 } else {
2192 ret = SA_CONFIG_ERR;
2194 sa_free_attr_string(path);
2197 return (ret);
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)
2209 int ret = SA_OK;
2210 struct stat st;
2212 share_t sh;
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));
2219 sh.sh_path = path;
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);
2226 return (ret);
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.
2235 boolean_t
2236 sa_needs_refresh(sa_handle_t handle)
2238 sa_handle_impl_t implhandle = (sa_handle_impl_t)handle;
2239 struct stat st;
2240 char *str;
2241 uint64_t tstamp;
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))
2246 return (B_TRUE);
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
2252 * caught.
2254 if (stat(SA_LEGACY_SHARETAB, &st) == 0 &&
2255 TSTAMP(st.st_mtim) != implhandle->tssharetab)
2256 return (B_TRUE);
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",
2265 "lastupdate");
2266 if (prop != NULL) {
2267 str = scf_simple_prop_next_astring(prop);
2268 if (str != NULL)
2269 tstamp = strtoull(str, NULL, 0);
2270 else
2271 tstamp = 0;
2272 scf_simple_prop_free(prop);
2273 if (tstamp != implhandle->tstrans)
2274 return (B_TRUE);
2277 return (B_FALSE);
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.
2293 void
2294 sa_fix_resource_name(char *path)
2296 char *invalid = "\"/\\[]:|<>+;,?*=";
2297 char *p = path;
2298 char *q;
2299 size_t len;
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 == '/'))
2310 *q = '\0';
2313 if (*p == '\0') {
2314 (void) strcpy(path, "_");
2315 return;
2319 * Stride over path components until the remaining
2320 * path is no longer than SA_MAX_RESOURCE_NAME.
2322 q = p;
2323 while ((q != NULL) && (strlen(q) > SA_MAX_RESOURCE_NAME)) {
2324 if ((q = strchr(q, '/')) != NULL) {
2325 ++q;
2326 p = q;
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);
2340 path[len] = '\0';
2342 for (p = path; *p != '\0'; ++p) {
2343 if ((iscntrl(*p)) || strchr(invalid, *p))
2344 *p = '_';