16185db85Sdougm /*
26185db85Sdougm  * CDDL HEADER START
36185db85Sdougm  *
46185db85Sdougm  * The contents of this file are subject to the terms of the
56185db85Sdougm  * Common Development and Distribution License (the "License").
66185db85Sdougm  * You may not use this file except in compliance with the License.
76185db85Sdougm  *
86185db85Sdougm  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
96185db85Sdougm  * or http://www.opensolaris.org/os/licensing.
106185db85Sdougm  * See the License for the specific language governing permissions
116185db85Sdougm  * and limitations under the License.
126185db85Sdougm  *
136185db85Sdougm  * When distributing Covered Code, include this CDDL HEADER in each
146185db85Sdougm  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
156185db85Sdougm  * If applicable, add the following below this CDDL HEADER, with the
166185db85Sdougm  * fields enclosed by brackets "[]" replaced with your own identifying
176185db85Sdougm  * information: Portions Copyright [yyyy] [name of copyright owner]
186185db85Sdougm  *
196185db85Sdougm  * CDDL HEADER END
206185db85Sdougm  */
216185db85Sdougm 
226185db85Sdougm /*
23f345c0beSdougm  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
246185db85Sdougm  * Use is subject to license terms.
256185db85Sdougm  */
266185db85Sdougm 
276185db85Sdougm #pragma ident	"%Z%%M%	%I%	%E% SMI"
286185db85Sdougm 
296185db85Sdougm /*
306185db85Sdougm  * core library for common functions across all config store types
316185db85Sdougm  * and file systems to be exported. This includes legacy dfstab/sharetab
326185db85Sdougm  * parsing. Need to eliminate XML where possible.
336185db85Sdougm  */
346185db85Sdougm 
356185db85Sdougm #include <stdio.h>
366185db85Sdougm #include <string.h>
376185db85Sdougm #include <ctype.h>
386185db85Sdougm #include <unistd.h>
396185db85Sdougm #include <limits.h>
406185db85Sdougm #include <errno.h>
416185db85Sdougm #include <sys/types.h>
426185db85Sdougm #include <sys/stat.h>
436185db85Sdougm #include <libxml/parser.h>
446185db85Sdougm #include <libxml/tree.h>
456185db85Sdougm #include "libshare.h"
466185db85Sdougm #include "libshare_impl.h"
476185db85Sdougm #include <fcntl.h>
486185db85Sdougm #include <sys/stat.h>
496185db85Sdougm #include <grp.h>
506185db85Sdougm #include <limits.h>
516185db85Sdougm #include <sys/param.h>
526185db85Sdougm #include <signal.h>
536185db85Sdougm #include <libintl.h>
546185db85Sdougm 
55a237e38eSth #include <sharefs/share.h>
566185db85Sdougm #include "sharetab.h"
576185db85Sdougm 
586185db85Sdougm #define	DFSTAB_NOTICE_LINES	5
596185db85Sdougm static char *notice[DFSTAB_NOTICE_LINES] =	{
606185db85Sdougm 	"# Do not modify this file directly.\n",
616185db85Sdougm 	"# Use the sharemgr(1m) command for all share management\n",
626185db85Sdougm 	"# This file is reconstructed and only maintained for backward\n",
636185db85Sdougm 	"# compatibility. Configuration lines could be lost.\n",
646185db85Sdougm 	"#\n"
656185db85Sdougm };
666185db85Sdougm 
676185db85Sdougm #define	STRNCAT(x, y, z)	(xmlChar *)strncat((char *)x, (char *)y, z)
686185db85Sdougm 
696185db85Sdougm /* will be much smaller, but this handles bad syntax in the file */
706185db85Sdougm #define	MAXARGSFORSHARE	256
716185db85Sdougm 
726185db85Sdougm /* used internally only */
736185db85Sdougm typedef
746185db85Sdougm struct sharelist {
756185db85Sdougm     struct sharelist *next;
766185db85Sdougm     int   persist;
776185db85Sdougm     char *path;
786185db85Sdougm     char *resource;
796185db85Sdougm     char *fstype;
806185db85Sdougm     char *options;
816185db85Sdougm     char *description;
826185db85Sdougm     char *group;
836185db85Sdougm     char *origline;
846185db85Sdougm     int lineno;
856185db85Sdougm } xfs_sharelist_t;
86549ec3ffSdougm static void parse_dfstab(sa_handle_t, char *, xmlNodePtr);
876185db85Sdougm extern char *get_token(char *);
886185db85Sdougm static void dfs_free_list(xfs_sharelist_t *);
896185db85Sdougm /* prototypes */
90549ec3ffSdougm void getlegacyconfig(sa_handle_t, char *, xmlNodePtr *);
916185db85Sdougm extern sa_share_t _sa_add_share(sa_group_t, char *, int, int *);
92549ec3ffSdougm extern sa_group_t _sa_create_group(sa_handle_impl_t, char *);
936185db85Sdougm static void outdfstab(FILE *, xfs_sharelist_t *);
946185db85Sdougm extern int _sa_remove_optionset(sa_optionset_t);
956185db85Sdougm extern int set_node_share(void *, char *, char *);
966185db85Sdougm extern void set_node_attr(void *, char *, char *);
976185db85Sdougm 
98a99982a7Sdougm /*
99a99982a7Sdougm  * sablocksigs(*sigs)
100a99982a7Sdougm  *
101a99982a7Sdougm  * block important signals for a critical region. Arg is a pointer to
102a99982a7Sdougm  * a sigset_t that is used later for the unblock.
103a99982a7Sdougm  */
104a99982a7Sdougm void
105a99982a7Sdougm sablocksigs(sigset_t *sigs)
106a99982a7Sdougm {
107a99982a7Sdougm 	sigset_t new;
108a99982a7Sdougm 
109a99982a7Sdougm 	if (sigs != NULL) {
11025a68471Sdougm 		(void) sigprocmask(SIG_BLOCK, NULL, &new);
11125a68471Sdougm 		(void) sigaddset(&new, SIGHUP);
11225a68471Sdougm 		(void) sigaddset(&new, SIGINT);
11325a68471Sdougm 		(void) sigaddset(&new, SIGQUIT);
11425a68471Sdougm 		(void) sigaddset(&new, SIGTSTP);
11525a68471Sdougm 		(void) sigprocmask(SIG_SETMASK, &new, sigs);
116a99982a7Sdougm 	}
117a99982a7Sdougm }
118a99982a7Sdougm 
119a99982a7Sdougm /*
120a99982a7Sdougm  * saunblocksigs(*sigs)
121a99982a7Sdougm  *
122a99982a7Sdougm  * unblock previously blocked signals from the sigs arg.
123a99982a7Sdougm  */
124a99982a7Sdougm void
125a99982a7Sdougm saunblocksigs(sigset_t *sigs)
126a99982a7Sdougm {
127a99982a7Sdougm 	if (sigs != NULL)
12825a68471Sdougm 		(void) sigprocmask(SIG_SETMASK, sigs, NULL);
129a99982a7Sdougm }
130a99982a7Sdougm 
1316185db85Sdougm /*
1326185db85Sdougm  * alloc_sharelist()
1336185db85Sdougm  *
1346185db85Sdougm  * allocator function to return an zfs_sharelist_t
1356185db85Sdougm  */
1366185db85Sdougm 
1376185db85Sdougm static xfs_sharelist_t *
1386185db85Sdougm alloc_sharelist()
1396185db85Sdougm {
1406185db85Sdougm 	xfs_sharelist_t *item;
1416185db85Sdougm 
1426185db85Sdougm 	item = (xfs_sharelist_t *)malloc(sizeof (xfs_sharelist_t));
1436185db85Sdougm 	if (item != NULL)
14425a68471Sdougm 		(void) memset(item, '\0', sizeof (xfs_sharelist_t));
1456185db85Sdougm 	return (item);
1466185db85Sdougm }
1476185db85Sdougm 
1486185db85Sdougm /*
1496185db85Sdougm  * fix_notice(list)
1506185db85Sdougm  *
1516185db85Sdougm  * Look at the beginning of the current /etc/dfs/dfstab file and add
1526185db85Sdougm  * the do not modify notice if it doesn't exist.
1536185db85Sdougm  */
1546185db85Sdougm 
1556185db85Sdougm static xfs_sharelist_t *
1566185db85Sdougm fix_notice(xfs_sharelist_t *list)
1576185db85Sdougm {
1586185db85Sdougm 	xfs_sharelist_t *item, *prev;
1596185db85Sdougm 	int i;
1606185db85Sdougm 
1617d968cb8Sdougm 	if (list == NULL) {
16225a68471Sdougm 		/* zero length dfstab */
16325a68471Sdougm 		list = alloc_sharelist();
16425a68471Sdougm 		if (list == NULL)
16525a68471Sdougm 			return (NULL);
16625a68471Sdougm 		list->description = strdup("#\n");
1677d968cb8Sdougm 	}
1686185db85Sdougm 	if (list->path == NULL && list->description != NULL &&
1696185db85Sdougm 	    strcmp(list->description, notice[0]) != 0) {
17025a68471Sdougm 		for (prev = NULL, i = 0; i < DFSTAB_NOTICE_LINES; i++) {
17125a68471Sdougm 			item = alloc_sharelist();
17225a68471Sdougm 			if (item != NULL) {
17325a68471Sdougm 				item->description = strdup(notice[i]);
17425a68471Sdougm 				if (prev == NULL) {
17525a68471Sdougm 					item->next = list;
17625a68471Sdougm 					prev = item;
17725a68471Sdougm 					list = item;
17825a68471Sdougm 				} else {
17925a68471Sdougm 					item->next = prev->next;
18025a68471Sdougm 					prev->next = item;
18125a68471Sdougm 					prev = item;
18225a68471Sdougm 				}
18325a68471Sdougm 			}
1846185db85Sdougm 		}
1856185db85Sdougm 	}
1866185db85Sdougm 	return (list);
1876185db85Sdougm }
1886185db85Sdougm 
1896185db85Sdougm /*
1906185db85Sdougm  * getdfstab(dfs)
1916185db85Sdougm  *
1926185db85Sdougm  * Returns an zfs_sharelist_t list of lines from the dfstab file
1936185db85Sdougm  * pointed to by the FILE pointer dfs. Each entry is parsed and the
1946185db85Sdougm  * original line is also preserved. Used in parsing and updating the
1956185db85Sdougm  * dfstab file.
1966185db85Sdougm  */
1976185db85Sdougm 
1986185db85Sdougm static xfs_sharelist_t *
1996185db85Sdougm getdfstab(FILE *dfs)
2006185db85Sdougm {
2016185db85Sdougm 	char buff[_POSIX_ARG_MAX]; /* reasonable size given syntax of share */
2026185db85Sdougm 	char *bp;
2036185db85Sdougm 	char *token;
2046185db85Sdougm 	char *args[MAXARGSFORSHARE];
2056185db85Sdougm 	int argc;
2066185db85Sdougm 	int c;
2076185db85Sdougm 	static int line = 0;
2087d968cb8Sdougm 	xfs_sharelist_t *item = NULL, *first = NULL, *last;
2096185db85Sdougm 
2106185db85Sdougm 	if (dfs != NULL) {
21125a68471Sdougm 		first = NULL;
21225a68471Sdougm 		line = 0;
21325a68471Sdougm 		while (fgets(buff, sizeof (buff), dfs) != NULL) {
21425a68471Sdougm 			line++;
21525a68471Sdougm 			bp = buff;
21625a68471Sdougm 			if (buff[0] == '#') {
21725a68471Sdougm 				item = alloc_sharelist();
21825a68471Sdougm 				if (item != NULL) {
21925a68471Sdougm 					/* if no path, then comment */
22025a68471Sdougm 					item->lineno = line;
22125a68471Sdougm 					item->description = strdup(buff);
22225a68471Sdougm 					if (first == NULL) {
22325a68471Sdougm 						first = item;
22425a68471Sdougm 						last = item;
22525a68471Sdougm 					} else {
22625a68471Sdougm 						last->next = item;
22725a68471Sdougm 						last = item;
22825a68471Sdougm 					}
22925a68471Sdougm 				} else {
23025a68471Sdougm 					break;
23125a68471Sdougm 				}
23225a68471Sdougm 				continue;
23325a68471Sdougm 			} else if (buff[0] == '\n') {
23425a68471Sdougm 				continue;
23525a68471Sdougm 			}
23625a68471Sdougm 			optind = 1;
23725a68471Sdougm 			item = alloc_sharelist();
23825a68471Sdougm 			if (item == NULL) {
23925a68471Sdougm 				break;
24025a68471Sdougm 			} else if (first == NULL) {
24125a68471Sdougm 				first = item;
24225a68471Sdougm 				last = item;
2436185db85Sdougm 			} else {
24425a68471Sdougm 				last->next = item;
24525a68471Sdougm 				last = item;
2466185db85Sdougm 			}
24725a68471Sdougm 			item->lineno = line;
24825a68471Sdougm 			item->origline = strdup(buff);
24925a68471Sdougm 			(void) get_token(NULL); /* reset to new pointers */
25025a68471Sdougm 			argc = 0;
25125a68471Sdougm 			while ((token = get_token(bp)) != NULL) {
25225a68471Sdougm 				if (argc < MAXARGSFORSHARE)
25325a68471Sdougm 					args[argc++] = token;
2546185db85Sdougm 			}
25525a68471Sdougm 			while ((c = getopt(argc, args, "F:o:d:pg:")) != -1) {
25625a68471Sdougm 				switch (c) {
25725a68471Sdougm 				case 'p':
25825a68471Sdougm 					item->persist = 1;
25925a68471Sdougm 					break;
26025a68471Sdougm 				case 'F':
26125a68471Sdougm 					item->fstype = strdup(optarg);
26225a68471Sdougm 					break;
26325a68471Sdougm 				case 'o':
26425a68471Sdougm 					item->options = strdup(optarg);
26525a68471Sdougm 					break;
26625a68471Sdougm 				case 'd':
26725a68471Sdougm 					item->description = strdup(optarg);
26825a68471Sdougm 					break;
26925a68471Sdougm 				case 'g':
27025a68471Sdougm 					item->group = strdup(optarg);
27125a68471Sdougm 					break;
27225a68471Sdougm 				default:
27325a68471Sdougm 					break;
27425a68471Sdougm 				}
27525a68471Sdougm 			}
27625a68471Sdougm 			if (optind < argc) {
27725a68471Sdougm 				item->path = strdup(args[optind]);
27825a68471Sdougm 				optind++;
27925a68471Sdougm 				if (optind < argc) {
28025a68471Sdougm 					char *resource;
28125a68471Sdougm 					char *optgroup;
28225a68471Sdougm 					/* resource and/or groupname */
28325a68471Sdougm 					resource = args[optind];
28425a68471Sdougm 					optgroup = strchr(resource, '@');
28525a68471Sdougm 					if (optgroup != NULL)
28625a68471Sdougm 						*optgroup++ = '\0';
28725a68471Sdougm 					if (optgroup != NULL)
28825a68471Sdougm 						item->group = strdup(optgroup);
28925a68471Sdougm 					if (resource != NULL &&
29025a68471Sdougm 					    strlen(resource) > 0)
29125a68471Sdougm 						item->resource =
29225a68471Sdougm 						    strdup(resource);
29325a68471Sdougm 				}
29425a68471Sdougm 			}
29525a68471Sdougm 			/* NFS is the default if none defined */
29625a68471Sdougm 			if (item != NULL && item->fstype == NULL)
29725a68471Sdougm 				item->fstype = strdup("nfs");
298a99982a7Sdougm 		}
2996185db85Sdougm 	}
3006185db85Sdougm 	first = fix_notice(first);
3016185db85Sdougm 	return (first);
3026185db85Sdougm }
3036185db85Sdougm 
3046185db85Sdougm /*
3056185db85Sdougm  * finddfsentry(list, path)
3066185db85Sdougm  *
3077d968cb8Sdougm  * Look for path in the zfs_sharelist_t list and return the entry if it
3086185db85Sdougm  * exists.
3096185db85Sdougm  */
3106185db85Sdougm 
3116185db85Sdougm static xfs_sharelist_t *
3126185db85Sdougm finddfsentry(xfs_sharelist_t *list, char *path)
3136185db85Sdougm {
3146185db85Sdougm 	xfs_sharelist_t *item;
3156185db85Sdougm 
3166185db85Sdougm 	for (item = list; item != NULL; item = item->next) {
31725a68471Sdougm 		if (item->path != NULL && strcmp(item->path, path) == 0)
3186185db85Sdougm 		return (item);
3196185db85Sdougm 	}
3206185db85Sdougm 	return (NULL);
3216185db85Sdougm }
3226185db85Sdougm 
3236185db85Sdougm /*
3246185db85Sdougm  * remdfsentry(list, path, proto)
3256185db85Sdougm  *
3266185db85Sdougm  * Remove the specified path (with protocol) from the list. This will
3276185db85Sdougm  * remove it from dfstab when the file is rewritten.
3286185db85Sdougm  */
3296185db85Sdougm 
3306185db85Sdougm static xfs_sharelist_t *
3316185db85Sdougm remdfsentry(xfs_sharelist_t *list, char *path, char *proto)
3326185db85Sdougm {
3336185db85Sdougm 	xfs_sharelist_t *item, *prev = NULL;
3346185db85Sdougm 
3356185db85Sdougm 
3366185db85Sdougm 	for (item = prev = list; item != NULL; item = item->next) {
3376185db85Sdougm 	    /* skip comment entry but don't lose it */
33825a68471Sdougm 		if (item->path == NULL) {
33925a68471Sdougm 			prev = item;
34025a68471Sdougm 			continue;
34125a68471Sdougm 		}
34225a68471Sdougm 		/* if proto is NULL, remove all protocols */
34325a68471Sdougm 		if (proto == NULL || (strcmp(item->path, path) == 0 &&
34425a68471Sdougm 		    (item->fstype != NULL && strcmp(item->fstype, proto) == 0)))
34525a68471Sdougm 			break;
34625a68471Sdougm 		if (item->fstype == NULL &&
34725a68471Sdougm 		    (proto == NULL || strcmp(proto, "nfs") == 0))
34825a68471Sdougm 			break;
3496185db85Sdougm 		prev = item;
3506185db85Sdougm 	}
3516185db85Sdougm 	if (item != NULL) {
35225a68471Sdougm 		if (item == prev)
35325a68471Sdougm 			list = item->next; /* this must be the first one */
35425a68471Sdougm 		else
35525a68471Sdougm 			prev->next = item->next;
35625a68471Sdougm 		item->next = NULL;
35725a68471Sdougm 		dfs_free_list(item);
3586185db85Sdougm 	}
3596185db85Sdougm 	return (list);
3606185db85Sdougm }
3616185db85Sdougm 
3626185db85Sdougm /*
3636185db85Sdougm  * remdfsline(list, line)
3646185db85Sdougm  *
3656185db85Sdougm  * Remove the line specified from the list.
3666185db85Sdougm  */
3676185db85Sdougm 
3686185db85Sdougm static xfs_sharelist_t *
3696185db85Sdougm remdfsline(xfs_sharelist_t *list, char *line)
3706185db85Sdougm {
3716185db85Sdougm 	xfs_sharelist_t *item, *prev = NULL;
3726185db85Sdougm 
3736185db85Sdougm 	for (item = prev = list; item != NULL; item = item->next) {
37425a68471Sdougm 		/* skip comment entry but don't lose it */
37525a68471Sdougm 		if (item->path == NULL) {
3766185db85Sdougm 		prev = item;
3776185db85Sdougm 		continue;
37825a68471Sdougm 		}
37925a68471Sdougm 		if (strcmp(item->origline, line) == 0)
38025a68471Sdougm 			break;
38125a68471Sdougm 		prev = item;
3826185db85Sdougm 	}
3836185db85Sdougm 	if (item != NULL) {
38425a68471Sdougm 		if (item == prev)
38525a68471Sdougm 			list = item->next; /* this must be the first one */
38625a68471Sdougm 		else
38725a68471Sdougm 			prev->next = item->next;
38825a68471Sdougm 		item->next = NULL;
38925a68471Sdougm 		dfs_free_list(item);
3906185db85Sdougm 	}
3916185db85Sdougm 	return (list);
3926185db85Sdougm }
3936185db85Sdougm 
3946185db85Sdougm /*
3956185db85Sdougm  * adddfsentry(list, share, proto)
3966185db85Sdougm  *
3976185db85Sdougm  * Add an entry to the dfstab list for share (relative to proto). This
3986185db85Sdougm  * is used to update dfstab for legacy purposes.
3996185db85Sdougm  */
4006185db85Sdougm 
4016185db85Sdougm static xfs_sharelist_t *
4026185db85Sdougm adddfsentry(xfs_sharelist_t *list, sa_share_t share, char *proto)
4036185db85Sdougm {
4046185db85Sdougm 	xfs_sharelist_t *item, *tmp;
4056185db85Sdougm 	sa_group_t parent;
4066185db85Sdougm 	char *groupname;
4076185db85Sdougm 
4086185db85Sdougm 	item = alloc_sharelist();
4096185db85Sdougm 	if (item != NULL) {
41025a68471Sdougm 		parent = sa_get_parent_group(share);
41125a68471Sdougm 		groupname = sa_get_group_attr(parent, "name");
41225a68471Sdougm 		if (strcmp(groupname, "default") == 0) {
41325a68471Sdougm 			sa_free_attr_string(groupname);
41425a68471Sdougm 			groupname = NULL;
41525a68471Sdougm 		}
41625a68471Sdougm 		item->path = sa_get_share_attr(share, "path");
41725a68471Sdougm 		item->resource = sa_get_share_attr(share, "resource");
41825a68471Sdougm 		item->group = groupname;
41925a68471Sdougm 		item->fstype = strdup(proto);
42025a68471Sdougm 		item->options = sa_proto_legacy_format(proto, share, 1);
42125a68471Sdougm 		if (item->options != NULL && strlen(item->options) == 0) {
42225a68471Sdougm 			free(item->options);
42325a68471Sdougm 			item->options = NULL;
42425a68471Sdougm 		}
42525a68471Sdougm 		item->description = sa_get_share_description(share);
42625a68471Sdougm 		if (item->description != NULL &&
42725a68471Sdougm 		    strlen(item->description) == 0) {
42825a68471Sdougm 			sa_free_share_description(item->description);
42925a68471Sdougm 			item->description = NULL;
43025a68471Sdougm 		}
43125a68471Sdougm 		if (list == NULL) {
43225a68471Sdougm 			list = item;
43325a68471Sdougm 		} else {
43425a68471Sdougm 			for (tmp = list; tmp->next != NULL; tmp = tmp->next)
43525a68471Sdougm 				/* do nothing */;
43625a68471Sdougm 				tmp->next = item;
43725a68471Sdougm 		}
4386185db85Sdougm 	}
4396185db85Sdougm 	return (list);
4406185db85Sdougm }
4416185db85Sdougm 
4426185db85Sdougm /*
4436185db85Sdougm  * outdfstab(dfstab, list)
4446185db85Sdougm  *
4456185db85Sdougm  * Output the list to dfstab making sure the file is truncated.
4466185db85Sdougm  * Comments and errors are preserved.
4476185db85Sdougm  */
4486185db85Sdougm 
4496185db85Sdougm static void
4506185db85Sdougm outdfstab(FILE *dfstab, xfs_sharelist_t *list)
4516185db85Sdougm {
4526185db85Sdougm 	xfs_sharelist_t *item;
4536185db85Sdougm 
4546185db85Sdougm 	(void) ftruncate(fileno(dfstab), 0);
4556185db85Sdougm 
4566185db85Sdougm 	for (item = list; item != NULL; item = item->next) {
45725a68471Sdougm 		if (item->path != NULL) {
45825a68471Sdougm 			if (*item->path == '/') {
45925a68471Sdougm 				(void) fprintf(dfstab,
46025a68471Sdougm 				    "share %s%s%s%s%s%s%s %s%s%s%s%s\n",
46125a68471Sdougm 				    (item->fstype != NULL) ? "-F " : "",
46225a68471Sdougm 				    (item->fstype != NULL) ? item->fstype : "",
46325a68471Sdougm 				    (item->options != NULL) ? " -o " : "",
46425a68471Sdougm 				    (item->options != NULL) ?
46525a68471Sdougm 				    item->options : "",
46625a68471Sdougm 				    (item->description != NULL) ?
46725a68471Sdougm 				    " -d \"" : "",
46825a68471Sdougm 				    (item->description != NULL) ?
46925a68471Sdougm 				    item->description : "",
47025a68471Sdougm 				    (item->description != NULL) ? "\"" : "",
47125a68471Sdougm 				    item->path,
47225a68471Sdougm 				    ((item->resource != NULL) ||
47325a68471Sdougm 				    (item->group != NULL)) ? " " : "",
47425a68471Sdougm 				    (item->resource != NULL) ?
47525a68471Sdougm 				    item->resource : "",
47625a68471Sdougm 				    item->group != NULL ? "@" : "",
47725a68471Sdougm 				    item->group != NULL ? item->group : "");
47825a68471Sdougm 			} else {
47925a68471Sdougm 				(void) fprintf(dfstab, "%s", item->origline);
48025a68471Sdougm 			}
4816185db85Sdougm 		} else {
48225a68471Sdougm 			if (item->description != NULL)
48325a68471Sdougm 				(void) fprintf(dfstab, "%s", item->description);
48425a68471Sdougm 			else
48525a68471Sdougm 				(void) fprintf(dfstab, "%s", item->origline);
4866185db85Sdougm 		}
4876185db85Sdougm 	}
4886185db85Sdougm }
4896185db85Sdougm 
4906185db85Sdougm /*
4916185db85Sdougm  * open_dfstab(file)
4926185db85Sdougm  *
4936185db85Sdougm  * Open the specified dfstab file. If the owner/group/perms are wrong,
4946185db85Sdougm  * fix them.
4956185db85Sdougm  */
4966185db85Sdougm 
4976185db85Sdougm static FILE *
4986185db85Sdougm open_dfstab(char *file)
4996185db85Sdougm {
5006185db85Sdougm 	struct group *grp;
5016185db85Sdougm 	struct group group;
5026185db85Sdougm 	char *buff;
5036185db85Sdougm 	int grsize;
5046185db85Sdougm 	FILE *dfstab;
5056185db85Sdougm 
5066185db85Sdougm 	dfstab = fopen(file, "r+");
5076185db85Sdougm 	if (dfstab == NULL) {
50825a68471Sdougm 		dfstab = fopen(file, "w+");
5096185db85Sdougm 	}
5106185db85Sdougm 	if (dfstab != NULL) {
5116185db85Sdougm 		grsize = sysconf(_SC_GETGR_R_SIZE_MAX);
5126185db85Sdougm 		buff = malloc(grsize);
5136185db85Sdougm 		if (buff != NULL)
51425a68471Sdougm 			grp = getgrnam_r(SA_DEFAULT_FILE_GRP, &group, buff,
51525a68471Sdougm 			    grsize);
5166185db85Sdougm 		else
51725a68471Sdougm 			grp = getgrnam(SA_DEFAULT_FILE_GRP);
5186185db85Sdougm 		(void) fchmod(fileno(dfstab), 0644);
5196185db85Sdougm 		(void) fchown(fileno(dfstab), 0,
52025a68471Sdougm 		    grp != NULL ? grp->gr_gid : 3);
5216185db85Sdougm 		if (buff != NULL)
52225a68471Sdougm 			free(buff);
5236185db85Sdougm 		rewind(dfstab);
5246185db85Sdougm 	}
5256185db85Sdougm 	return (dfstab);
5266185db85Sdougm }
5276185db85Sdougm 
5286185db85Sdougm /*
5296185db85Sdougm  * sa_comment_line(line, err)
5306185db85Sdougm  *
5316185db85Sdougm  * Add a comment to the dfstab file with err as a prefix to the
5326185db85Sdougm  * original line.
5336185db85Sdougm  */
5346185db85Sdougm 
5356185db85Sdougm static void
5366185db85Sdougm sa_comment_line(char *line, char *err)
5376185db85Sdougm {
5386185db85Sdougm 	FILE *dfstab;
5396185db85Sdougm 	xfs_sharelist_t *list;
540a99982a7Sdougm 	sigset_t old;
5416185db85Sdougm 
5426185db85Sdougm 	dfstab = open_dfstab(SA_LEGACY_DFSTAB);
5436185db85Sdougm 	if (dfstab != NULL) {
5446185db85Sdougm 		(void) setvbuf(dfstab, NULL, _IOLBF, BUFSIZ * 8);
545a99982a7Sdougm 		sablocksigs(&old);
5466185db85Sdougm 		(void) lockf(fileno(dfstab), F_LOCK, 0);
5476185db85Sdougm 		list = getdfstab(dfstab);
5486185db85Sdougm 		rewind(dfstab);
549f345c0beSdougm 		/*
550f345c0beSdougm 		 * don't ignore the return since the list could have
551f345c0beSdougm 		 * gone to NULL if the file only had one line in it.
552f345c0beSdougm 		 */
553f345c0beSdougm 		list = remdfsline(list, line);
5546185db85Sdougm 		outdfstab(dfstab, list);
5556185db85Sdougm 		(void) fprintf(dfstab, "# Error: %s: %s", err, line);
5566185db85Sdougm 		(void) fsync(fileno(dfstab));
5576185db85Sdougm 		(void) lockf(fileno(dfstab), F_ULOCK, 0);
5586185db85Sdougm 		(void) fclose(dfstab);
559a99982a7Sdougm 		saunblocksigs(&old);
5606185db85Sdougm 		if (list != NULL)
56125a68471Sdougm 			dfs_free_list(list);
5626185db85Sdougm 	}
5636185db85Sdougm }
5646185db85Sdougm 
5656185db85Sdougm /*
5666185db85Sdougm  * sa_delete_legacy(share)
5676185db85Sdougm  *
5686185db85Sdougm  * Delete the specified share from the legacy config file.
5696185db85Sdougm  */
5706185db85Sdougm 
5716185db85Sdougm int
5726185db85Sdougm sa_delete_legacy(sa_share_t share)
5736185db85Sdougm {
5746185db85Sdougm 	FILE *dfstab;
5756185db85Sdougm 	int err;
5766185db85Sdougm 	int ret = SA_OK;
5776185db85Sdougm 	xfs_sharelist_t *list;
5786185db85Sdougm 	char *path;
5796185db85Sdougm 	sa_optionset_t optionset;
5806185db85Sdougm 	sa_group_t parent;
581a99982a7Sdougm 	sigset_t old;
5826185db85Sdougm 
5836185db85Sdougm 	dfstab = open_dfstab(SA_LEGACY_DFSTAB);
5846185db85Sdougm 	if (dfstab != NULL) {
5856185db85Sdougm 		(void) setvbuf(dfstab, NULL, _IOLBF, BUFSIZ * 8);
586a99982a7Sdougm 		sablocksigs(&old);
5876185db85Sdougm 		path = sa_get_share_attr(share, "path");
5886185db85Sdougm 		parent = sa_get_parent_group(share);
5896185db85Sdougm 		if (parent != NULL) {
59025a68471Sdougm 			(void) lockf(fileno(dfstab), F_LOCK, 0);
59125a68471Sdougm 			list = getdfstab(dfstab);
59225a68471Sdougm 			rewind(dfstab);
59325a68471Sdougm 			for (optionset = sa_get_optionset(parent, NULL);
59425a68471Sdougm 			    optionset != NULL;
59525a68471Sdougm 			    optionset = sa_get_next_optionset(optionset)) {
59625a68471Sdougm 				char *proto = sa_get_optionset_attr(optionset,
59725a68471Sdougm 				    "type");
59825a68471Sdougm 				if (list != NULL && proto != NULL)
59925a68471Sdougm 					list = remdfsentry(list, path,
60025a68471Sdougm 					    proto);
60125a68471Sdougm 				if (proto == NULL)
60225a68471Sdougm 					ret = SA_NO_MEMORY;
60325a68471Sdougm 				/*
60425a68471Sdougm 				 * May want to only do the dfstab if
60525a68471Sdougm 				 * this call returns NOT IMPLEMENTED
60625a68471Sdougm 				 * but it shouldn't hurt.
60725a68471Sdougm 				 */
60825a68471Sdougm 				if (ret == SA_OK) {
60925a68471Sdougm 					err = sa_proto_delete_legacy(proto,
61025a68471Sdougm 					    share);
61125a68471Sdougm 					if (err != SA_NOT_IMPLEMENTED)
61225a68471Sdougm 						ret = err;
61325a68471Sdougm 				}
61425a68471Sdougm 				if (proto != NULL)
61525a68471Sdougm 					sa_free_attr_string(proto);
6166185db85Sdougm 			}
61725a68471Sdougm 			outdfstab(dfstab, list);
61825a68471Sdougm 			if (list != NULL)
61925a68471Sdougm 				dfs_free_list(list);
62025a68471Sdougm 			(void) fflush(dfstab);
62125a68471Sdougm 			(void) lockf(fileno(dfstab), F_ULOCK, 0);
6226185db85Sdougm 		}
6236185db85Sdougm 		(void) fsync(fileno(dfstab));
624a99982a7Sdougm 		saunblocksigs(&old);
6256185db85Sdougm 		(void) fclose(dfstab);
6266185db85Sdougm 		sa_free_attr_string(path);
6276185db85Sdougm 	} else {
62825a68471Sdougm 		if (errno == EACCES || errno == EPERM)
62925a68471Sdougm 			ret = SA_NO_PERMISSION;
63025a68471Sdougm 		else
63125a68471Sdougm 			ret = SA_CONFIG_ERR;
6326185db85Sdougm 	}
6336185db85Sdougm 	return (ret);
6346185db85Sdougm }
6356185db85Sdougm 
6366185db85Sdougm /*
6376185db85Sdougm  * sa_update_legacy(share, proto)
6386185db85Sdougm  *
6396185db85Sdougm  * There is an assumption that dfstab will be the most common form of
6406185db85Sdougm  * legacy configuration file for shares, but not the only one. Because
6416185db85Sdougm  * of that, dfstab handling is done in the main code with calls to
6426185db85Sdougm  * this function and protocol specific calls to deal with formating
6436185db85Sdougm  * options into dfstab/share compatible syntax. Since not everything
6446185db85Sdougm  * will be dfstab, there is a provision for calling a protocol
6456185db85Sdougm  * specific plugin interface that allows the protocol plugin to do its
6466185db85Sdougm  * own legacy files and skip the dfstab update.
6476185db85Sdougm  */
6486185db85Sdougm 
6496185db85Sdougm int
6506185db85Sdougm sa_update_legacy(sa_share_t share, char *proto)
6516185db85Sdougm {
6526185db85Sdougm 	FILE *dfstab;
6536185db85Sdougm 	int ret = SA_OK;
6546185db85Sdougm 	xfs_sharelist_t *list;
6556185db85Sdougm 	char *path;
656a99982a7Sdougm 	sigset_t old;
6576185db85Sdougm 	char *persist;
6586185db85Sdougm 
6596185db85Sdougm 	ret = sa_proto_update_legacy(proto, share);
6606185db85Sdougm 	if (ret != SA_NOT_IMPLEMENTED)
66125a68471Sdougm 		return (ret);
6626185db85Sdougm 	/* do the dfstab format */
6636185db85Sdougm 	persist = sa_get_share_attr(share, "type");
6646185db85Sdougm 	/*
6656185db85Sdougm 	 * only update if the share is not transient -- no share type
6666185db85Sdougm 	 * set or the type is not "transient".
6676185db85Sdougm 	 */
6686185db85Sdougm 	if (persist == NULL || strcmp(persist, "transient") != 0) {
66925a68471Sdougm 		dfstab = open_dfstab(SA_LEGACY_DFSTAB);
67025a68471Sdougm 		if (dfstab != NULL) {
67125a68471Sdougm 			(void) setvbuf(dfstab, NULL, _IOLBF, BUFSIZ * 8);
67225a68471Sdougm 			sablocksigs(&old);
67325a68471Sdougm 			path = sa_get_share_attr(share, "path");
67425a68471Sdougm 			(void) lockf(fileno(dfstab), F_LOCK, 0);
67525a68471Sdougm 			list = getdfstab(dfstab);
67625a68471Sdougm 			rewind(dfstab);
67725a68471Sdougm 			if (list != NULL)
67825a68471Sdougm 				list = remdfsentry(list, path, proto);
67925a68471Sdougm 			list = adddfsentry(list, share, proto);
68025a68471Sdougm 			outdfstab(dfstab, list);
68125a68471Sdougm 			(void) fflush(dfstab);
68225a68471Sdougm 			(void) lockf(fileno(dfstab), F_ULOCK, 0);
68325a68471Sdougm 			(void) fsync(fileno(dfstab));
68425a68471Sdougm 			saunblocksigs(&old);
68525a68471Sdougm 			(void) fclose(dfstab);
68625a68471Sdougm 			sa_free_attr_string(path);
68725a68471Sdougm 			if (list != NULL)
68825a68471Sdougm 				dfs_free_list(list);
6896185db85Sdougm 		} else {
69025a68471Sdougm 			if (errno == EACCES || errno == EPERM)
69125a68471Sdougm 				ret = SA_NO_PERMISSION;
69225a68471Sdougm 			else
69325a68471Sdougm 				ret = SA_CONFIG_ERR;
6946185db85Sdougm 		}
6956185db85Sdougm 	}
6966185db85Sdougm 	if (persist != NULL)
69725a68471Sdougm 		sa_free_attr_string(persist);
6986185db85Sdougm 	return (ret);
6996185db85Sdougm }
7006185db85Sdougm 
7016185db85Sdougm /*
7026185db85Sdougm  * sa_is_security(optname, proto)
7036185db85Sdougm  *
7046185db85Sdougm  * Check to see if optname is a security (named optionset) specific
7056185db85Sdougm  * property for the specified protocol.
7066185db85Sdougm  */
7076185db85Sdougm 
7086185db85Sdougm int
7096185db85Sdougm sa_is_security(char *optname, char *proto)
7106185db85Sdougm {
7116185db85Sdougm 	int ret = 0;
7126185db85Sdougm 	if (proto != NULL)
71325a68471Sdougm 		ret = sa_proto_security_prop(proto, optname);
7146185db85Sdougm 	return (ret);
7156185db85Sdougm }
7166185db85Sdougm 
7176185db85Sdougm /*
7186185db85Sdougm  * add_syntax_comment(root, line, err, todfstab)
7196185db85Sdougm  *
72025a68471Sdougm  * Add a comment to the document indicating a syntax error. If
7216185db85Sdougm  * todfstab is set, write it back to the dfstab file as well.
7226185db85Sdougm  */
7236185db85Sdougm 
7246185db85Sdougm static void
7256185db85Sdougm add_syntax_comment(xmlNodePtr root, char *line, char *err, int todfstab)
7266185db85Sdougm {
7276185db85Sdougm 	xmlNodePtr node;
7286185db85Sdougm 
7296185db85Sdougm 	node = xmlNewChild(root, NULL, (xmlChar *)"error", (xmlChar *)line);
73025a68471Sdougm 	if (node != NULL)
73125a68471Sdougm 		xmlSetProp(node, (xmlChar *)"type", (xmlChar *)err);
7326185db85Sdougm 	if (todfstab)
73325a68471Sdougm 		sa_comment_line(line, err);
7346185db85Sdougm }
7356185db85Sdougm 
7366185db85Sdougm /*
7376185db85Sdougm  * sa_is_share(object)
7386185db85Sdougm  *
7396185db85Sdougm  * returns true of the object is of type "share".
7406185db85Sdougm  */
7416185db85Sdougm 
7426185db85Sdougm int
7436185db85Sdougm sa_is_share(void *object)
7446185db85Sdougm {
7456185db85Sdougm 	if (object != NULL) {
74625a68471Sdougm 		if (strcmp((char *)((xmlNodePtr)object)->name, "share") == 0)
7476185db85Sdougm 		return (1);
7486185db85Sdougm 	}
7496185db85Sdougm 	return (0);
7506185db85Sdougm }
7516185db85Sdougm 
7526185db85Sdougm /*
7536185db85Sdougm  * _sa_remove_property(property)
7546185db85Sdougm  *
7556185db85Sdougm  * remove a property only from the document.
7566185db85Sdougm  */
7576185db85Sdougm 
7586185db85Sdougm static void
7596185db85Sdougm _sa_remove_property(sa_property_t property)
7606185db85Sdougm {
7616185db85Sdougm 	xmlUnlinkNode((xmlNodePtr)property);
7626185db85Sdougm 	xmlFreeNode((xmlNodePtr)property);
7636185db85Sdougm }
7646185db85Sdougm 
76567331909Sdougm /*
76667331909Sdougm  * _sa_create_dummy_share()
76767331909Sdougm  *
76867331909Sdougm  * Create a share entry suitable for parsing but not tied to any real
76967331909Sdougm  * config tree.  Need to have a parent as well as the node to parse
77067331909Sdougm  * on.  Free using _sa_free_dummy_share(share);
77167331909Sdougm  */
77267331909Sdougm 
77367331909Sdougm static sa_group_t
77467331909Sdougm _sa_create_dummy_share()
77567331909Sdougm {
77667331909Sdougm 	xmlNodePtr parent_node = NULL;
77767331909Sdougm 	xmlNodePtr child_node = NULL;
77867331909Sdougm 
77967331909Sdougm 	parent_node = xmlNewNode(NULL, (xmlChar *)"group");
78067331909Sdougm 	if (parent_node != NULL) {
78125a68471Sdougm 		child_node = xmlNewChild(parent_node, NULL, (xmlChar *)"share",
78225a68471Sdougm 		    NULL);
78325a68471Sdougm 		if (child_node != NULL) {
78425a68471Sdougm 			/*
78525a68471Sdougm 			 * Use a "zfs" tag since that will make sure nothing
78625a68471Sdougm 			 * really attempts to put values into the
78725a68471Sdougm 			 * repository. Also ZFS is currently the only user of
78825a68471Sdougm 			 * this interface.
78925a68471Sdougm 			 */
79025a68471Sdougm 			set_node_attr(parent_node, "type", "transient");
79125a68471Sdougm 			set_node_attr(parent_node, "zfs", "true");
79225a68471Sdougm 			set_node_attr(child_node, "type", "transient");
79325a68471Sdougm 			set_node_attr(child_node, "zfs", "true");
79425a68471Sdougm 		} else {
79525a68471Sdougm 			xmlFreeNode(parent_node);
79625a68471Sdougm 		}
79767331909Sdougm 	}
79867331909Sdougm 	return (child_node);
79967331909Sdougm }
80067331909Sdougm 
80167331909Sdougm /*
80267331909Sdougm  * _sa_free_dummy_share(share)
80367331909Sdougm  *
80467331909Sdougm  * Free the dummy share and its parent.  It is an error to try and
80567331909Sdougm  * free something that isn't a dummy.
80667331909Sdougm  */
80767331909Sdougm 
80867331909Sdougm static int
80967331909Sdougm _sa_free_dummy_share(sa_share_t share)
81067331909Sdougm {
81167331909Sdougm 	xmlNodePtr node = (xmlNodePtr)share;
81267331909Sdougm 	xmlNodePtr parent;
81367331909Sdougm 	int ret = SA_OK;
81467331909Sdougm 	char *name;
81567331909Sdougm 
81667331909Sdougm 	if (node != NULL) {
81725a68471Sdougm 		parent = node->parent;
81825a68471Sdougm 		name = (char *)xmlGetProp(node, (xmlChar *)"path");
81925a68471Sdougm 		if (name != NULL) {
82025a68471Sdougm 			/* Real shares always have a path but a dummy doesn't */
82125a68471Sdougm 			ret = SA_NOT_ALLOWED;
82225a68471Sdougm 			sa_free_attr_string(name);
82325a68471Sdougm 		} else {
82425a68471Sdougm 			/*
82525a68471Sdougm 			 * If there is a parent, do the free on that since
82625a68471Sdougm 			 * xmlFreeNode is a recursive function and free's an
82725a68471Sdougm 			 * child nodes.
82825a68471Sdougm 			 */
82925a68471Sdougm 			if (parent != NULL) {
83025a68471Sdougm 				node = parent;
83125a68471Sdougm 			}
83225a68471Sdougm 			xmlUnlinkNode(node);
83325a68471Sdougm 			xmlFreeNode(node);
83467331909Sdougm 		}
83567331909Sdougm 	}
83667331909Sdougm 	return (ret);
83767331909Sdougm }
83867331909Sdougm 
83967331909Sdougm 
8406185db85Sdougm /*
8416185db85Sdougm  * sa_parse_legacy_options(group, options, proto)
8426185db85Sdougm  *
8436185db85Sdougm  * In order to support legacy configurations, we allow the protocol
8446185db85Sdougm  * specific plugin to parse legacy syntax options (like those in
8456185db85Sdougm  * /etc/dfs/dfstab). This adds a new optionset to the group (or
8466185db85Sdougm  * share).
8476185db85Sdougm  *
8486185db85Sdougm  * Once the optionset has been created, we then get the derived
8496185db85Sdougm  * optionset of the parent (options from the optionset of the parent
8506185db85Sdougm  * and any parent it might have) and remove those from the created
8516185db85Sdougm  * optionset. This avoids duplication of options.
8526185db85Sdougm  */
8536185db85Sdougm 
8546185db85Sdougm int
8556185db85Sdougm sa_parse_legacy_options(sa_group_t group, char *options, char *proto)
8566185db85Sdougm {
8576185db85Sdougm 	int ret = SA_INVALID_PROTOCOL;
8586185db85Sdougm 	sa_group_t parent;
85967331909Sdougm 	int using_dummy = B_FALSE;
860717a41ebSdougm 	char *pvalue;
86125a68471Sdougm 	sa_optionset_t optionset;
86225a68471Sdougm 	sa_property_t popt, prop;
86325a68471Sdougm 	sa_optionset_t localoptions;
86467331909Sdougm 
86567331909Sdougm 	/*
86625a68471Sdougm 	 * If "group" is NULL, this is just a parse without saving
86767331909Sdougm 	 * anything in either SMF or ZFS.  Create a dummy group to
86867331909Sdougm 	 * handle this case.
86967331909Sdougm 	 */
87067331909Sdougm 	if (group == NULL) {
87125a68471Sdougm 		group = (sa_group_t)_sa_create_dummy_share();
87225a68471Sdougm 		using_dummy = B_TRUE;
87367331909Sdougm 	}
87467331909Sdougm 
8756185db85Sdougm 	parent = sa_get_parent_group(group);
8766185db85Sdougm 
8776185db85Sdougm 	if (proto != NULL)
87825a68471Sdougm 		ret = sa_proto_legacy_opts(proto, group, options);
87967331909Sdougm 
88067331909Sdougm 	if (using_dummy) {
88125a68471Sdougm 		/* Since this is a dummy parse, cleanup and quit here */
88225a68471Sdougm 		(void) _sa_free_dummy_share(parent);
88325a68471Sdougm 		return (ret);
88467331909Sdougm 	}
88525a68471Sdougm 
88625a68471Sdougm 	if (ret != SA_OK)
88725a68471Sdougm 		return (ret);
88825a68471Sdougm 
8896185db85Sdougm 	/*
89025a68471Sdougm 	 * If in a group, remove the inherited options and security
8916185db85Sdougm 	 */
89225a68471Sdougm 
89325a68471Sdougm 	if (parent == NULL)
89425a68471Sdougm 		return (ret);
89525a68471Sdougm 
89625a68471Sdougm 	/* Find parent options to remove from child */
89725a68471Sdougm 	optionset = sa_get_derived_optionset(parent, proto, 1);
89825a68471Sdougm 	localoptions = sa_get_optionset(group, proto);
89925a68471Sdougm 	if (optionset != NULL) {
90025a68471Sdougm 		for (popt = sa_get_property(optionset, NULL);
90125a68471Sdougm 		    popt != NULL;
90225a68471Sdougm 		    popt = sa_get_next_property(popt)) {
9036185db85Sdougm 			char *tag;
904717a41ebSdougm 			char *value;
9056185db85Sdougm 			tag = sa_get_property_attr(popt, "type");
90625a68471Sdougm 			if (tag == NULL)
90725a68471Sdougm 				continue;
90825a68471Sdougm 			prop = sa_get_property(localoptions, tag);
90925a68471Sdougm 			if (prop != NULL) {
91025a68471Sdougm 				value = sa_get_property_attr(popt,
91125a68471Sdougm 				    "value");
91225a68471Sdougm 				pvalue = sa_get_property_attr(prop,
91325a68471Sdougm 				    "value");
914717a41ebSdougm 				if (value != NULL && pvalue != NULL &&
915717a41ebSdougm 				    strcmp(value, pvalue) == 0) {
916717a41ebSdougm 					/*
91725a68471Sdougm 					 * Remove the property
91825a68471Sdougm 					 * from the
91925a68471Sdougm 					 * child. While we
92025a68471Sdougm 					 * removed it, we
92125a68471Sdougm 					 * don't need to reset
92225a68471Sdougm 					 * as we do below
92325a68471Sdougm 					 * since we always
92425a68471Sdougm 					 * search from the
92525a68471Sdougm 					 * beginning.
926717a41ebSdougm 					 */
92725a68471Sdougm 					(void) _sa_remove_property(
92825a68471Sdougm 					    prop);
9296185db85Sdougm 				}
930717a41ebSdougm 				if (value != NULL)
93125a68471Sdougm 					sa_free_attr_string(value);
932717a41ebSdougm 				if (pvalue != NULL)
93325a68471Sdougm 					sa_free_attr_string(pvalue);
9346185db85Sdougm 			}
93525a68471Sdougm 			sa_free_attr_string(tag);
93625a68471Sdougm 		}
93725a68471Sdougm 		prop = sa_get_property(localoptions, NULL);
93825a68471Sdougm 		if (prop == NULL && sa_is_share(group)) {
9396185db85Sdougm 			/*
94025a68471Sdougm 			 * All properties removed so remove the
9416185db85Sdougm 			 * optionset if it is on a share
9426185db85Sdougm 			 */
9436185db85Sdougm 			(void) _sa_remove_optionset(localoptions);
9446185db85Sdougm 		}
94525a68471Sdougm 		sa_free_derived_optionset(optionset);
94625a68471Sdougm 	}
94725a68471Sdougm 	/*
94825a68471Sdougm 	 * Need to remove security here. If there are no
94925a68471Sdougm 	 * security options on the local group/share, don't
95025a68471Sdougm 	 * bother since those are the only ones that would be
95125a68471Sdougm 	 * affected.
95225a68471Sdougm 	 */
95325a68471Sdougm 	localoptions = sa_get_all_security_types(group, proto, 0);
95425a68471Sdougm 	if (localoptions != NULL) {
95525a68471Sdougm 		for (prop = sa_get_property(localoptions, NULL);
95625a68471Sdougm 		    prop != NULL;
95725a68471Sdougm 		    prop = sa_get_next_property(prop)) {
9586185db85Sdougm 			char *tag;
9596185db85Sdougm 			sa_security_t security;
9606185db85Sdougm 			tag = sa_get_property_attr(prop, "type");
9616185db85Sdougm 			if (tag != NULL) {
96225a68471Sdougm 				sa_property_t nextpopt = NULL;
96325a68471Sdougm 				security = sa_get_security(group, tag, proto);
96425a68471Sdougm 				sa_free_attr_string(tag);
965717a41ebSdougm 				/*
96625a68471Sdougm 				 * prop's value only changes outside this loop
967717a41ebSdougm 				 */
96825a68471Sdougm 				pvalue = sa_get_property_attr(prop, "value");
96925a68471Sdougm 				for (popt = sa_get_property(security, NULL);
97025a68471Sdougm 				    popt != NULL;
97125a68471Sdougm 				    popt = nextpopt) {
97225a68471Sdougm 					char *value;
97325a68471Sdougm 					/*
97425a68471Sdougm 					 * Need to get the next prop
97525a68471Sdougm 					 * now since we could break
97625a68471Sdougm 					 * the list during removal.
97725a68471Sdougm 					 */
97825a68471Sdougm 					nextpopt = sa_get_next_property(popt);
97925a68471Sdougm 					/* remove Duplicates from this level */
98025a68471Sdougm 					value = sa_get_property_attr(popt,
98125a68471Sdougm 					    "value");
98225a68471Sdougm 					if (value != NULL && pvalue != NULL &&
98325a68471Sdougm 					    strcmp(value, pvalue) == 0) {
98425a68471Sdougm 						/*
98525a68471Sdougm 						 * remove the property
98625a68471Sdougm 						 * from the child
98725a68471Sdougm 						 */
98825a68471Sdougm 						(void) _sa_remove_property
98925a68471Sdougm 						    (popt);
99025a68471Sdougm 					}
99125a68471Sdougm 					if (value != NULL)
99225a68471Sdougm 						sa_free_attr_string(value);
9936185db85Sdougm 				}
99425a68471Sdougm 				if (pvalue != NULL)
99525a68471Sdougm 					sa_free_attr_string(pvalue);
9966185db85Sdougm 			}
9976185db85Sdougm 		}
99825a68471Sdougm 		(void) sa_destroy_optionset(localoptions);
9996185db85Sdougm 	}
10006185db85Sdougm 	return (ret);
10016185db85Sdougm }
10026185db85Sdougm 
10036185db85Sdougm /*
10046185db85Sdougm  * dfs_free_list(list)
10056185db85Sdougm  *
10066185db85Sdougm  * Free the data in each list entry of the list as well as freeing the
10076185db85Sdougm  * entries themselves. We need to avoid memory leaks and don't want to
10086185db85Sdougm  * dereference any NULL members.
10096185db85Sdougm  */
10106185db85Sdougm 
10116185db85Sdougm static void
10126185db85Sdougm dfs_free_list(xfs_sharelist_t *list)
10136185db85Sdougm {
10146185db85Sdougm 	xfs_sharelist_t *entry;
10156185db85Sdougm 	for (entry = list; entry != NULL; entry = list) {
101625a68471Sdougm 		if (entry->path != NULL)
101725a68471Sdougm 			free(entry->path);
101825a68471Sdougm 		if (entry->resource != NULL)
101925a68471Sdougm 			free(entry->resource);
102025a68471Sdougm 		if (entry->fstype != NULL)
102125a68471Sdougm 			free(entry->fstype);
102225a68471Sdougm 		if (entry->options != NULL)
102325a68471Sdougm 			free(entry->options);
102425a68471Sdougm 		if (entry->description != NULL)
102525a68471Sdougm 			free(entry->description);
102625a68471Sdougm 		if (entry->origline != NULL)
102725a68471Sdougm 			free(entry->origline);
102825a68471Sdougm 		if (entry->group != NULL)
102925a68471Sdougm 			free(entry->group);
103025a68471Sdougm 		list = list->next;
103125a68471Sdougm 			free(entry);
10326185db85Sdougm 	}
10336185db85Sdougm }
10346185db85Sdougm 
10356185db85Sdougm /*
10366185db85Sdougm  * parse_dfstab(dfstab, root)
10376185db85Sdougm  *
10386185db85Sdougm  * Open and read the existing dfstab, parsing each line and adding it
10396185db85Sdougm  * to the internal configuration. Make sure syntax errors, etc are
10406185db85Sdougm  * preserved as comments.
10416185db85Sdougm  */
10426185db85Sdougm 
10436185db85Sdougm static void
1044549ec3ffSdougm parse_dfstab(sa_handle_t handle, char *dfstab, xmlNodePtr root)
10456185db85Sdougm {
10466185db85Sdougm 	sa_share_t share;
10476185db85Sdougm 	sa_group_t group;
10486185db85Sdougm 	sa_group_t sgroup = NULL;
10496185db85Sdougm 	sa_group_t defgroup;
10506185db85Sdougm 	xfs_sharelist_t *head, *list;
10516185db85Sdougm 	int err;
10526185db85Sdougm 	int defined_group;
10536185db85Sdougm 	FILE *dfs;
10546185db85Sdougm 	char *oldprops;
10556185db85Sdougm 
10566185db85Sdougm 	/* read the dfstab format file and fill in the doc tree */
10576185db85Sdougm 
10586185db85Sdougm 	dfs = fopen(dfstab, "r");
105925a68471Sdougm 	if (dfs == NULL)
106025a68471Sdougm 		return;
10616185db85Sdougm 
1062549ec3ffSdougm 	defgroup = sa_get_group(handle, "default");
10636185db85Sdougm 
10646185db85Sdougm 	for (head = list = getdfstab(dfs);
106525a68471Sdougm 	    list != NULL;
106625a68471Sdougm 	    list = list->next) {
106725a68471Sdougm 		share = NULL;
106825a68471Sdougm 		group = NULL;
106925a68471Sdougm 		defined_group = 0;
107025a68471Sdougm 		err = 0;
107125a68471Sdougm 
107225a68471Sdougm 		if (list->origline == NULL) {
107325a68471Sdougm 			/*
107425a68471Sdougm 			 * Comment line that we will likely skip.
107525a68471Sdougm 			 * If the line has the syntax:
107625a68471Sdougm 			 *	# error: string: string
107725a68471Sdougm 			 * It should be preserved until manually deleted.
107825a68471Sdougm 			 */
107925a68471Sdougm 			if (list->description != NULL &&
108025a68471Sdougm 			    strncmp(list->description, "# Error: ", 9) == 0) {
108125a68471Sdougm 				char *line;
108225a68471Sdougm 				char *error;
108325a68471Sdougm 				char *cmd;
108425a68471Sdougm 				line = strdup(list->description);
108525a68471Sdougm 				if (line != NULL) {
108625a68471Sdougm 					error = line + 9;
108725a68471Sdougm 					cmd = strchr(error, ':');
108825a68471Sdougm 					if (cmd != NULL) {
108925a68471Sdougm 						int len;
109025a68471Sdougm 						*cmd = '\0';
109125a68471Sdougm 						cmd += 2;
109225a68471Sdougm 						len = strlen(cmd);
109325a68471Sdougm 						cmd[len - 1] = '\0';
109425a68471Sdougm 						add_syntax_comment(root, cmd,
109525a68471Sdougm 						    error, 0);
109625a68471Sdougm 					}
109725a68471Sdougm 					free(line);
109825a68471Sdougm 				}
10996185db85Sdougm 			}
110025a68471Sdougm 			continue;
11016185db85Sdougm 		}
110225a68471Sdougm 		if (list->path != NULL && strlen(list->path) > 0 &&
1103549ec3ffSdougm 		    *list->path == '/') {
110425a68471Sdougm 			share = sa_find_share(handle, list->path);
110525a68471Sdougm 			if (share != NULL)
110625a68471Sdougm 				sgroup = sa_get_parent_group(share);
110725a68471Sdougm 			else
110825a68471Sdougm 				sgroup = NULL;
110925a68471Sdougm 		} else {
111025a68471Sdougm 			(void) printf(dgettext(TEXT_DOMAIN,
111125a68471Sdougm 			    "No share specified in dfstab: "
111225a68471Sdougm 			    "line %d: %s\n"),
111325a68471Sdougm 			    list->lineno, list->origline);
111425a68471Sdougm 			add_syntax_comment(root, list->origline,
111525a68471Sdougm 			    dgettext(TEXT_DOMAIN, "No share specified"), 1);
111625a68471Sdougm 			continue;
111725a68471Sdougm 		}
111825a68471Sdougm 		if (list->group != NULL && strlen(list->group) > 0) {
111925a68471Sdougm 			group = sa_get_group(handle, list->group);
112025a68471Sdougm 			defined_group = 1;
112125a68471Sdougm 		} else {
112225a68471Sdougm 			group = defgroup;
112325a68471Sdougm 		}
112425a68471Sdougm 		if (defined_group && group == NULL) {
112525a68471Sdougm 			(void) printf(dgettext(TEXT_DOMAIN,
112625a68471Sdougm 			    "Unknown group used in dfstab: line %d: %s\n"),
112725a68471Sdougm 			    list->lineno, list->origline);
112825a68471Sdougm 			add_syntax_comment(root, list->origline,
112925a68471Sdougm 			    dgettext(TEXT_DOMAIN, "Unknown group specified"),
113025a68471Sdougm 			    1);
113125a68471Sdougm 			continue;
113225a68471Sdougm 		}
113325a68471Sdougm 		if (group == NULL) {
113425a68471Sdougm 			/* Shouldn't happen unless an SMF error */
113525a68471Sdougm 			err = SA_CONFIG_ERR;
113625a68471Sdougm 			continue;
113725a68471Sdougm 		}
11386185db85Sdougm 		if (share == NULL) {
113925a68471Sdougm 			if (defined_group || group != defgroup)
114025a68471Sdougm 				continue;
114125a68471Sdougm 			/* This is an OK add for legacy */
11426185db85Sdougm 			share = sa_add_share(defgroup, list->path,
114325a68471Sdougm 			    SA_SHARE_PERMANENT | SA_SHARE_PARSER, &err);
11446185db85Sdougm 			if (share != NULL) {
114525a68471Sdougm 				if (list->description != NULL &&
114625a68471Sdougm 				    strlen(list->description) > 0)
114725a68471Sdougm 					(void) sa_set_share_description(share,
114825a68471Sdougm 					    list->description);
114925a68471Sdougm 				if (list->options != NULL &&
115025a68471Sdougm 				    strlen(list->options) > 0) {
115125a68471Sdougm 					(void) sa_parse_legacy_options(share,
115225a68471Sdougm 					    list->options, list->fstype);
115325a68471Sdougm 				}
115425a68471Sdougm 				if (list->resource != NULL)
115525a68471Sdougm 					(void) sa_set_share_attr(share,
115625a68471Sdougm 					    "resource", list->resource);
11576185db85Sdougm 			} else {
115825a68471Sdougm 				(void) printf(dgettext(TEXT_DOMAIN,
115925a68471Sdougm 				    "Error in dfstab: line %d: %s\n"),
11606185db85Sdougm 				    list->lineno, list->origline);
116125a68471Sdougm 				if (err != SA_BAD_PATH)
116225a68471Sdougm 					add_syntax_comment(root, list->origline,
116325a68471Sdougm 					    dgettext(TEXT_DOMAIN, "Syntax"), 1);
116425a68471Sdougm 				else
116525a68471Sdougm 					add_syntax_comment(root, list->origline,
116625a68471Sdougm 					    dgettext(TEXT_DOMAIN,
116725a68471Sdougm 					    "Path"), 1);
116825a68471Sdougm 				continue;
11696185db85Sdougm 			}
11706185db85Sdougm 		} else {
117125a68471Sdougm 			if (group != sgroup) {
117225a68471Sdougm 				(void) printf(dgettext(TEXT_DOMAIN,
117325a68471Sdougm 				    "Attempt to change configuration in "
117425a68471Sdougm 				    "dfstab: line %d: %s\n"),
117525a68471Sdougm 				    list->lineno, list->origline);
117625a68471Sdougm 				add_syntax_comment(root, list->origline,
117725a68471Sdougm 				    dgettext(TEXT_DOMAIN,
117825a68471Sdougm 				    "Attempt to change configuration"), 1);
117925a68471Sdougm 				continue;
118025a68471Sdougm 			}
1181546405c3Sdougm 			/*
1182546405c3Sdougm 			 * It is the same group but could have changed
1183546405c3Sdougm 			 * options. Make sure we include the group's
1184546405c3Sdougm 			 * properties so we don't end up moving them to
1185546405c3Sdougm 			 * the share inadvertantly. The last arg being
1186546405c3Sdougm 			 * true says to get the inherited properties as well
1187546405c3Sdougm 			 * as the local properties.
1188546405c3Sdougm 			 */
118925a68471Sdougm 			oldprops = sa_proto_legacy_format(list->fstype, share,
119025a68471Sdougm 			    B_TRUE);
119125a68471Sdougm 
119225a68471Sdougm 			if (oldprops == NULL)
119325a68471Sdougm 				continue;
119425a68471Sdougm 
11956185db85Sdougm 			if (list->options != NULL &&
119625a68471Sdougm 			    strcmp(oldprops, list->options) != 0) {
119725a68471Sdougm 				sa_optionset_t opts;
119825a68471Sdougm 				sa_security_t secs;
119925a68471Sdougm 
120025a68471Sdougm 				/* possibly different values */
120125a68471Sdougm 				opts = sa_get_optionset((sa_group_t)
120225a68471Sdougm 				    share, list->fstype);
120325a68471Sdougm 				(void) sa_destroy_optionset(opts);
120425a68471Sdougm 
120525a68471Sdougm 				for (secs = sa_get_security(
120625a68471Sdougm 				    (sa_group_t)share, NULL, list->fstype);
120725a68471Sdougm 				    secs != NULL;
120825a68471Sdougm 				    secs = sa_get_security((sa_group_t)share,
120925a68471Sdougm 				    NULL, list->fstype)) {
121025a68471Sdougm 					(void) sa_destroy_security(
121125a68471Sdougm 					    secs);
121225a68471Sdougm 				}
121325a68471Sdougm 				(void) sa_parse_legacy_options(share,
121425a68471Sdougm 				    list->options, list->fstype);
12156185db85Sdougm 			}
1216a99982a7Sdougm 			sa_format_free(oldprops);
12176185db85Sdougm 		}
12186185db85Sdougm 	}
12196185db85Sdougm 	dfs_free_list(head);
12206185db85Sdougm }
12216185db85Sdougm 
12226185db85Sdougm /*
12236185db85Sdougm  * legacy_removes(group, file)
12246185db85Sdougm  *
12256185db85Sdougm  * Find any shares that are "missing" from the legacy file. These
12266185db85Sdougm  * should be removed from the configuration since they are likely from
12276185db85Sdougm  * a legacy app or the admin modified the dfstab file directly. We
12286185db85Sdougm  * have to support this even if it is not the recommended way to do
12296185db85Sdougm  * things.
12306185db85Sdougm  */
12316185db85Sdougm 
12326185db85Sdougm static void
12336185db85Sdougm legacy_removes(sa_group_t group, char *file)
12346185db85Sdougm {
12356185db85Sdougm 	sa_share_t share;
12366185db85Sdougm 	char *path;
12376185db85Sdougm 	xfs_sharelist_t *list, *item;
12386185db85Sdougm 	FILE *dfstab;
12396185db85Sdougm 
12406185db85Sdougm 	dfstab = fopen(file, "r");
12416185db85Sdougm 	if (dfstab != NULL) {
124225a68471Sdougm 		list = getdfstab(dfstab);
124325a68471Sdougm 		(void) fclose(dfstab);
1244f345c0beSdougm retry:
124525a68471Sdougm 		for (share = sa_get_share(group, NULL);
124625a68471Sdougm 		    share != NULL;
124725a68471Sdougm 		    share = sa_get_next_share(share)) {
124825a68471Sdougm 			/* now see if the share is in the dfstab file */
124925a68471Sdougm 			path = sa_get_share_attr(share, "path");
125025a68471Sdougm 			if (path != NULL) {
125125a68471Sdougm 				item = finddfsentry(list, path);
125225a68471Sdougm 				sa_free_attr_string(path);
125325a68471Sdougm 				if (item == NULL) {
125425a68471Sdougm 					/* The share was removed this way */
125525a68471Sdougm 					(void) sa_remove_share(share);
125625a68471Sdougm 
125725a68471Sdougm 					/*
125825a68471Sdougm 					 * Start over since the list was broken
125925a68471Sdougm 					 */
126025a68471Sdougm 					goto retry;
126125a68471Sdougm 				}
126225a68471Sdougm 			}
12636185db85Sdougm 		}
126425a68471Sdougm 		if (list != NULL)
126525a68471Sdougm 			dfs_free_list(list);
12666185db85Sdougm 	}
12676185db85Sdougm }
12686185db85Sdougm 
12696185db85Sdougm /*
12706185db85Sdougm  * getlegacyconfig(path, root)
12716185db85Sdougm  *
12726185db85Sdougm  * Parse dfstab and build the legacy configuration. This only gets
12736185db85Sdougm  * called when a change was detected.
12746185db85Sdougm  */
12756185db85Sdougm 
12766185db85Sdougm void
1277549ec3ffSdougm getlegacyconfig(sa_handle_t handle, char *path, xmlNodePtr *root)
12786185db85Sdougm {
12796185db85Sdougm 	sa_group_t defgroup;
12806185db85Sdougm 
12816185db85Sdougm 	if (root != NULL) {
128225a68471Sdougm 		if (*root == NULL)
128325a68471Sdougm 			*root = xmlNewNode(NULL, (xmlChar *)"sharecfg");
128425a68471Sdougm 		if (*root != NULL) {
128525a68471Sdougm 			if (strcmp(path, SA_LEGACY_DFSTAB) == 0) {
128625a68471Sdougm 				/*
128725a68471Sdougm 				 * Walk the default shares and find anything
128825a68471Sdougm 				 * missing.  we do this first to make sure it
128925a68471Sdougm 				 * is cleaned up since there may be legacy
129025a68471Sdougm 				 * code add/del via dfstab and we need to
129125a68471Sdougm 				 * cleanup SMF.
129225a68471Sdougm 				 */
129325a68471Sdougm 				defgroup = sa_get_group(handle, "default");
129425a68471Sdougm 				if (defgroup != NULL)
129525a68471Sdougm 					legacy_removes(defgroup, path);
129625a68471Sdougm 				/* Parse the dfstab and add anything new */
129725a68471Sdougm 				parse_dfstab(handle, path, *root);
129825a68471Sdougm 			}
12996185db85Sdougm 		}
13006185db85Sdougm 	}
13016185db85Sdougm }
13026185db85Sdougm 
13031cea05afSdougm /*
13041cea05afSdougm  * get_share_list(&err)
13051cea05afSdougm  *
13061cea05afSdougm  * Get a linked list of all the shares on the system from
13071cea05afSdougm  * /etc/dfs/sharetab. This is partially copied from libfsmgt which we
13081cea05afSdougm  * can't use due to package dependencies.
13091cea05afSdougm  */
13101cea05afSdougm static xfs_sharelist_t *
13111cea05afSdougm get_share_list(int *errp)
13121cea05afSdougm {
13131cea05afSdougm 	xfs_sharelist_t	*newp;
13141cea05afSdougm 	xfs_sharelist_t	*headp;
13151cea05afSdougm 	xfs_sharelist_t	*tailp;
13161cea05afSdougm 	FILE		*fp;
13171cea05afSdougm 
13181cea05afSdougm 	headp = NULL;
13191cea05afSdougm 	tailp = NULL;
13201cea05afSdougm 
13211cea05afSdougm 	if ((fp = fopen(SHARETAB, "r")) != NULL) {
13221cea05afSdougm 		struct share	*sharetab_entry;
13231cea05afSdougm 
13241cea05afSdougm 		while (getshare(fp, &sharetab_entry) > 0) {
132525a68471Sdougm 			newp = alloc_sharelist();
132625a68471Sdougm 			if (newp == NULL)
132725a68471Sdougm 				goto err;
13281cea05afSdougm 
13291cea05afSdougm 			/*
133025a68471Sdougm 			 * Link into the list here so we don't leak
13311cea05afSdougm 			 * memory on a failure from strdup().
13321cea05afSdougm 			 */
133325a68471Sdougm 			if (headp == NULL) {
133425a68471Sdougm 				headp = newp;
133525a68471Sdougm 				tailp = newp;
133625a68471Sdougm 			} else {
133725a68471Sdougm 				tailp->next = newp;
133825a68471Sdougm 				tailp = newp;
133925a68471Sdougm 			}
134025a68471Sdougm 
134125a68471Sdougm 			newp->path = strdup(sharetab_entry->sh_path);
134225a68471Sdougm 			if (newp->path == NULL)
134325a68471Sdougm 				goto err;
134425a68471Sdougm 			newp->resource = strdup(sharetab_entry->sh_res);
134525a68471Sdougm 			if (newp->resource == NULL)
134625a68471Sdougm 				goto err;
134725a68471Sdougm 			newp->fstype = strdup(sharetab_entry->sh_fstype);
134825a68471Sdougm 			if (newp->fstype == NULL)
134925a68471Sdougm 				goto err;
135025a68471Sdougm 			newp->options = strdup(sharetab_entry->sh_opts);
135125a68471Sdougm 			if (newp->options == NULL)
135225a68471Sdougm 				goto err;
135325a68471Sdougm 			newp->description = strdup(sharetab_entry->sh_descr);
135425a68471Sdougm 			if (newp->description == NULL)
135525a68471Sdougm 				goto err;
13561cea05afSdougm 		}
1357a99982a7Sdougm 		(void) lockf(fileno(fp), F_ULOCK, 0);
13581cea05afSdougm 		(void) fclose(fp);
13591cea05afSdougm 	} else {
136025a68471Sdougm 		*errp = errno;
13611cea05afSdougm 	}
13621cea05afSdougm 
13631cea05afSdougm 	/*
13641cea05afSdougm 	 * Caller must free the mount list
13651cea05afSdougm 	 */
13661cea05afSdougm 	return (headp);
13671cea05afSdougm err:
13681cea05afSdougm 	/*
13691cea05afSdougm 	 * Out of memory so cleanup and leave.
13701cea05afSdougm 	 */
13711cea05afSdougm 	dfs_free_list(headp);
13721cea05afSdougm 	(void) fclose(fp);
13731cea05afSdougm 	return (NULL);
13741cea05afSdougm }
13751cea05afSdougm 
13766185db85Sdougm /*
1377549ec3ffSdougm  * parse_sharetab(handle)
13786185db85Sdougm  *
13791cea05afSdougm  * Read the /etc/dfs/sharetab file and see which entries don't exist
13801cea05afSdougm  * in the repository. These shares are marked transient.  We also need
13811cea05afSdougm  * to see if they are ZFS shares since ZFS bypasses the SMF
13821cea05afSdougm  * repository.
13836185db85Sdougm  */
13846185db85Sdougm 
13856185db85Sdougm int
1386549ec3ffSdougm parse_sharetab(sa_handle_t handle)
13876185db85Sdougm {
13881cea05afSdougm 	xfs_sharelist_t *list, *tmplist;
13896185db85Sdougm 	int err = 0;
13906185db85Sdougm 	sa_share_t share;
13916185db85Sdougm 	sa_group_t group;
13926185db85Sdougm 	sa_group_t lgroup;
13936185db85Sdougm 	char *groupname;
13946185db85Sdougm 	int legacy = 0;
13956185db85Sdougm 
13961cea05afSdougm 	list = get_share_list(&err);
13976185db85Sdougm 	if (list == NULL)
139825a68471Sdougm 		return (legacy);
13996185db85Sdougm 
1400549ec3ffSdougm 	lgroup = sa_get_group(handle, "default");
14016185db85Sdougm 
14026185db85Sdougm 	for (tmplist = list; tmplist != NULL; tmplist = tmplist->next) {
140325a68471Sdougm 		group = NULL;
140425a68471Sdougm 		share = sa_find_share(handle, tmplist->path);
140525a68471Sdougm 		if (share != NULL) {
140625a68471Sdougm 			/*
140725a68471Sdougm 			 * If this is a legacy share, mark as shared so we
140825a68471Sdougm 			 * only update sharetab appropriately. We also keep
140925a68471Sdougm 			 * the sharetab options in order to display for legacy
141025a68471Sdougm 			 * share with no arguments.
141125a68471Sdougm 			 */
141225a68471Sdougm 			set_node_attr(share, "shared", "true");
141325a68471Sdougm 			set_node_attr(share, "shareopts", tmplist->options);
141425a68471Sdougm 			continue;
141525a68471Sdougm 		}
141625a68471Sdougm 
14176185db85Sdougm 		/*
141825a68471Sdougm 		 * This share is transient so needs to be
14196185db85Sdougm 		 * added. Initially, this will be under
14206185db85Sdougm 		 * default(legacy) unless it is a ZFS
14216185db85Sdougm 		 * share. If zfs, we need a zfs group.
14226185db85Sdougm 		 */
14236185db85Sdougm 		if (tmplist->resource != NULL &&
14246185db85Sdougm 		    (groupname = strchr(tmplist->resource, '@')) != NULL) {
142525a68471Sdougm 			/* There is a defined group */
142625a68471Sdougm 			*groupname++ = '\0';
142725a68471Sdougm 			group = sa_get_group(handle, groupname);
14286185db85Sdougm 			if (group != NULL) {
142925a68471Sdougm 				share = _sa_add_share(group,
143025a68471Sdougm 				    tmplist->path,
143125a68471Sdougm 				    SA_SHARE_TRANSIENT, &err);
143225a68471Sdougm 			} else {
143325a68471Sdougm 				/*
143425a68471Sdougm 				 * While this case shouldn't
143525a68471Sdougm 				 * occur very often, it does
143625a68471Sdougm 				 * occur out of a "zfs set
143725a68471Sdougm 				 * sharenfs=off" when the
143825a68471Sdougm 				 * dataset is also set to
143925a68471Sdougm 				 * canmount=off. A warning
144025a68471Sdougm 				 * will then cause the zfs
144125a68471Sdougm 				 * command to abort. Since we
144225a68471Sdougm 				 * add it to the default list,
144325a68471Sdougm 				 * everything works properly
144425a68471Sdougm 				 * anyway and the library
144525a68471Sdougm 				 * doesn't need to give a
144625a68471Sdougm 				 * warning.
144725a68471Sdougm 				 */
144825a68471Sdougm 				share = _sa_add_share(lgroup,
144925a68471Sdougm 				    tmplist->path, SA_SHARE_TRANSIENT,
145025a68471Sdougm 				    &err);
145125a68471Sdougm 			}
145225a68471Sdougm 		} else {
145325a68471Sdougm 			if (sa_zfs_is_shared(handle, tmplist->path)) {
145425a68471Sdougm 				group = sa_get_group(handle, "zfs");
145525a68471Sdougm 				if (group == NULL) {
145625a68471Sdougm 					group = sa_create_group(handle,
145725a68471Sdougm 					    "zfs", &err);
145825a68471Sdougm 					if (group == NULL &&
145925a68471Sdougm 					    err == SA_NO_PERMISSION) {
146025a68471Sdougm 						group = _sa_create_group(
146125a68471Sdougm 						    (sa_handle_impl_t)
146225a68471Sdougm 						    handle,
146325a68471Sdougm 						    "zfs");
146425a68471Sdougm 					}
146525a68471Sdougm 					if (group != NULL) {
146625a68471Sdougm 						(void) sa_create_optionset(
146725a68471Sdougm 						    group, tmplist->fstype);
146825a68471Sdougm 						(void) sa_set_group_attr(group,
146925a68471Sdougm 						    "zfs", "true");
147025a68471Sdougm 					}
147125a68471Sdougm 				}
147225a68471Sdougm 				if (group != NULL) {
147325a68471Sdougm 					share = _sa_add_share(group,
147425a68471Sdougm 					    tmplist->path, SA_SHARE_TRANSIENT,
147525a68471Sdougm 					    &err);
147625a68471Sdougm 				}
147725a68471Sdougm 			} else {
147825a68471Sdougm 				share = _sa_add_share(lgroup, tmplist->path,
147925a68471Sdougm 				    SA_SHARE_TRANSIENT, &err);
14806185db85Sdougm 			}
14816185db85Sdougm 		}
14826185db85Sdougm 		if (share == NULL)
148325a68471Sdougm 			(void) printf(dgettext(TEXT_DOMAIN,
148425a68471Sdougm 			    "Problem with transient: %s\n"), sa_errorstr(err));
14856185db85Sdougm 		if (share != NULL)
148625a68471Sdougm 			set_node_attr(share, "shared", "true");
14876185db85Sdougm 		if (err == SA_OK) {
148825a68471Sdougm 			if (tmplist->options != NULL &&
148925a68471Sdougm 			    strlen(tmplist->options) > 0) {
149025a68471Sdougm 				(void) sa_parse_legacy_options(share,
149125a68471Sdougm 				    tmplist->options, tmplist->fstype);
149225a68471Sdougm 			}
149325a68471Sdougm 			if (tmplist->resource != NULL &&
149425a68471Sdougm 			    strcmp(tmplist->resource, "-") != 0)
149525a68471Sdougm 				set_node_attr(share, "resource",
149625a68471Sdougm 				    tmplist->resource);
149725a68471Sdougm 			if (tmplist->description != NULL) {
149825a68471Sdougm 				xmlNodePtr node;
149925a68471Sdougm 				node = xmlNewChild((xmlNodePtr)share, NULL,
150025a68471Sdougm 				    (xmlChar *)"description", NULL);
150125a68471Sdougm 				xmlNodeSetContent(node,
150225a68471Sdougm 				    (xmlChar *)tmplist->description);
150325a68471Sdougm 			}
150425a68471Sdougm 			legacy = 1;
15056185db85Sdougm 		}
15066185db85Sdougm 	}
15071cea05afSdougm 	dfs_free_list(list);
15086185db85Sdougm 	return (legacy);
15096185db85Sdougm }
15106185db85Sdougm 
15116185db85Sdougm /*
151225a68471Sdougm  * Get the transient shares from the sharetab (or other) file.  since
15136185db85Sdougm  * these are transient, they only appear in the working file and not
15146185db85Sdougm  * in a repository.
15156185db85Sdougm  */
15166185db85Sdougm int
1517549ec3ffSdougm gettransients(sa_handle_impl_t ihandle, xmlNodePtr *root)
15186185db85Sdougm {
15196185db85Sdougm 	int legacy = 0;
15206185db85Sdougm 
15216185db85Sdougm 	if (root != NULL) {
152225a68471Sdougm 		if (*root == NULL)
152325a68471Sdougm 			*root = xmlNewNode(NULL, (xmlChar *)"sharecfg");
152425a68471Sdougm 		if (*root != NULL)
152525a68471Sdougm 			legacy = parse_sharetab(ihandle);
15266185db85Sdougm 	}
15276185db85Sdougm 	return (legacy);
15286185db85Sdougm }
15296185db85Sdougm 
15306185db85Sdougm /*
15316185db85Sdougm  * sa_has_prop(optionset, prop)
15326185db85Sdougm  *
15336185db85Sdougm  * Is the specified property a member of the optionset?
15346185db85Sdougm  */
15356185db85Sdougm 
15366185db85Sdougm int
15376185db85Sdougm sa_has_prop(sa_optionset_t optionset, sa_property_t prop)
15386185db85Sdougm {
15396185db85Sdougm 	char *name;
15406185db85Sdougm 	sa_property_t otherprop;
15416185db85Sdougm 	int result = 0;
15426185db85Sdougm 
15436185db85Sdougm 	if (optionset != NULL) {
154425a68471Sdougm 		name = sa_get_property_attr(prop, "type");
154525a68471Sdougm 		if (name != NULL) {
154625a68471Sdougm 			otherprop = sa_get_property(optionset, name);
154725a68471Sdougm 			if (otherprop != NULL)
154825a68471Sdougm 				result = 1;
154925a68471Sdougm 			sa_free_attr_string(name);
155025a68471Sdougm 		}
15516185db85Sdougm 	}
15526185db85Sdougm 	return (result);
15536185db85Sdougm }
15546185db85Sdougm 
15556185db85Sdougm /*
15566185db85Sdougm  * Update legacy files
15576185db85Sdougm  *
15586185db85Sdougm  * Provides functions to add/remove/modify individual entries
15596185db85Sdougm  * in dfstab and sharetab
15606185db85Sdougm  */
15616185db85Sdougm 
15626185db85Sdougm void
1563549ec3ffSdougm update_legacy_config(sa_handle_t handle)
15646185db85Sdougm {
15656185db85Sdougm 	/*
15666185db85Sdougm 	 * no longer used -- this is a placeholder in case we need to
15676185db85Sdougm 	 * add it back later.
15686185db85Sdougm 	 */
1569549ec3ffSdougm #ifdef lint
1570549ec3ffSdougm 	handle = handle;
1571549ec3ffSdougm #endif
15726185db85Sdougm }
15736185db85Sdougm 
15746185db85Sdougm /*
15756185db85Sdougm  * sa_valid_property(object, proto, property)
15766185db85Sdougm  *
15776185db85Sdougm  * check to see if the specified property is valid relative to the
15786185db85Sdougm  * specified protocol. The protocol plugin is called to do the work.
15796185db85Sdougm  */
15806185db85Sdougm 
15816185db85Sdougm int
15826185db85Sdougm sa_valid_property(void *object, char *proto, sa_property_t property)
15836185db85Sdougm {
15846185db85Sdougm 	int ret = SA_OK;
15856185db85Sdougm 
15866185db85Sdougm 	if (proto != NULL && property != NULL) {
158725a68471Sdougm 		ret = sa_proto_valid_prop(proto, property, object);
15886185db85Sdougm 	}
15896185db85Sdougm 
15906185db85Sdougm 	return (ret);
15916185db85Sdougm }
15926185db85Sdougm 
15936185db85Sdougm /*
15946185db85Sdougm  * sa_fstype(path)
15956185db85Sdougm  *
15966185db85Sdougm  * Given path, return the string representing the path's file system
15976185db85Sdougm  * type. This is used to discover ZFS shares.
15986185db85Sdougm  */
15996185db85Sdougm 
16006185db85Sdougm char *
16016185db85Sdougm sa_fstype(char *path)
16026185db85Sdougm {
16036185db85Sdougm 	int err;
16046185db85Sdougm 	struct stat st;
16056185db85Sdougm 
16066185db85Sdougm 	err = stat(path, &st);
160725a68471Sdougm 	if (err < 0)
160825a68471Sdougm 		err = SA_NO_SUCH_PATH;
160925a68471Sdougm 	else
161025a68471Sdougm 		err = SA_OK;
161125a68471Sdougm 
161225a68471Sdougm 	/*
161325a68471Sdougm 	 * If we have a valid path at this point ret, return the fstype.
161425a68471Sdougm 	 */
161525a68471Sdougm 	if (err == SA_OK)
161625a68471Sdougm 		return (strdup(st.st_fstype));
161725a68471Sdougm 
16186185db85Sdougm 	return (NULL);
16196185db85Sdougm }
16206185db85Sdougm 
16216185db85Sdougm void
16226185db85Sdougm sa_free_fstype(char *type)
16236185db85Sdougm {
16246185db85Sdougm 	free(type);
16256185db85Sdougm }
16266185db85Sdougm 
16276185db85Sdougm /*
16286185db85Sdougm  * sa_get_derived_optionset(object, proto, hier)
16296185db85Sdougm  *
16306185db85Sdougm  *	Work backward to the top of the share object tree and start
16316185db85Sdougm  *	copying protocol specific optionsets into a newly created
16326185db85Sdougm  *	optionset that doesn't have a parent (it will be freed
16336185db85Sdougm  *	later). This provides for the property inheritence model. That
16346185db85Sdougm  *	is, properties closer to the share take precedence over group
16356185db85Sdougm  *	level. This also provides for groups of groups in the future.
16366185db85Sdougm  */
16376185db85Sdougm 
16386185db85Sdougm sa_optionset_t
16396185db85Sdougm sa_get_derived_optionset(void *object, char *proto, int hier)
16406185db85Sdougm {
16416185db85Sdougm 	sa_optionset_t newoptionset;
16426185db85Sdougm 	sa_optionset_t optionset;
16436185db85Sdougm 	sa_group_t group;
16446185db85Sdougm 
16456185db85Sdougm 	if (hier &&
16466185db85Sdougm 	    (group = sa_get_parent_group((sa_share_t)object)) != NULL) {
164725a68471Sdougm 		newoptionset = sa_get_derived_optionset((void *)group, proto,
164825a68471Sdougm 		    hier);
16496185db85Sdougm 	} else {
165025a68471Sdougm 		newoptionset = (sa_optionset_t)xmlNewNode(NULL,
165125a68471Sdougm 		    (xmlChar *)"optionset");
165225a68471Sdougm 		if (newoptionset != NULL) {
165325a68471Sdougm 			sa_set_optionset_attr(newoptionset, "type", proto);
165425a68471Sdougm 		}
16556185db85Sdougm 	}
165625a68471Sdougm 	/* Dont' do anything if memory wasn't allocated */
16576185db85Sdougm 	if (newoptionset == NULL)
165825a68471Sdougm 		return (NULL);
16596185db85Sdougm 
166025a68471Sdougm 	/* Found the top so working back down the stack */
16616185db85Sdougm 	optionset = sa_get_optionset((sa_optionset_t)object, proto);
16626185db85Sdougm 	if (optionset != NULL) {
166325a68471Sdougm 		sa_property_t prop;
166425a68471Sdougm 		/* add optionset to the newoptionset */
166525a68471Sdougm 		for (prop = sa_get_property(optionset, NULL);
166625a68471Sdougm 		    prop != NULL;
166725a68471Sdougm 		    prop = sa_get_next_property(prop)) {
166825a68471Sdougm 			sa_property_t newprop;
166925a68471Sdougm 			char *name;
167025a68471Sdougm 			char *value;
167125a68471Sdougm 			name = sa_get_property_attr(prop, "type");
167225a68471Sdougm 			value = sa_get_property_attr(prop, "value");
167325a68471Sdougm 			if (name == NULL)
167425a68471Sdougm 				continue;
167525a68471Sdougm 			newprop = sa_get_property(newoptionset, name);
167625a68471Sdougm 			/* Replace the value with the new value */
167725a68471Sdougm 			if (newprop != NULL) {
167825a68471Sdougm 				/*
167925a68471Sdougm 				 * Only set if value is non NULL, old value ok
168025a68471Sdougm 				 * if it is NULL.
168125a68471Sdougm 				 */
168225a68471Sdougm 				if (value != NULL)
168325a68471Sdougm 					set_node_attr(newprop, "value", value);
168425a68471Sdougm 			} else {
168525a68471Sdougm 				/* an entirely new property */
168625a68471Sdougm 				if (value != NULL) {
168725a68471Sdougm 					newprop = sa_create_property(name,
168825a68471Sdougm 					    value);
168925a68471Sdougm 					if (newprop != NULL) {
169025a68471Sdougm 						newprop = (sa_property_t)
169125a68471Sdougm 						    xmlAddChild(
169225a68471Sdougm 						    (xmlNodePtr)newoptionset,
169325a68471Sdougm 						    (xmlNodePtr)newprop);
169425a68471Sdougm 					}
169525a68471Sdougm 				}
16966185db85Sdougm 			}
169725a68471Sdougm 			sa_free_attr_string(name);
169825a68471Sdougm 
169925a68471Sdougm 			if (value != NULL)
170025a68471Sdougm 				sa_free_attr_string(value);
17016185db85Sdougm 		}
17026185db85Sdougm 	}
17036185db85Sdougm 	return (newoptionset);
17046185db85Sdougm }
17056185db85Sdougm 
17066185db85Sdougm void
17076185db85Sdougm sa_free_derived_optionset(sa_optionset_t optionset)
17086185db85Sdougm {
170925a68471Sdougm 	/* While it shouldn't be linked, it doesn't hurt */
17106185db85Sdougm 	if (optionset != NULL) {
171125a68471Sdougm 		xmlUnlinkNode((xmlNodePtr) optionset);
171225a68471Sdougm 		xmlFreeNode((xmlNodePtr) optionset);
17136185db85Sdougm 	}
17146185db85Sdougm }
17156185db85Sdougm 
17166185db85Sdougm /*
17176185db85Sdougm  *  sa_get_all_security_types(object, proto, hier)
17186185db85Sdougm  *
171925a68471Sdougm  *	Find all the security types set for this object.  This is
17206185db85Sdougm  *	preliminary to getting a derived security set. The return value is an
17216185db85Sdougm  *	optionset containg properties which are the sectype values found by
17226185db85Sdougm  *	walking up the XML document struture. The returned optionset
17236185db85Sdougm  *	is a derived optionset.
17246185db85Sdougm  *
17256185db85Sdougm  *	If hier is 0, only look at object. If non-zero, walk up the tree.
17266185db85Sdougm  */
17276185db85Sdougm sa_optionset_t
17286185db85Sdougm sa_get_all_security_types(void *object, char *proto, int hier)
17296185db85Sdougm {
17306185db85Sdougm 	sa_optionset_t options;
17316185db85Sdougm 	sa_security_t security;
17326185db85Sdougm 	sa_group_t group;
17336185db85Sdougm 	sa_property_t prop;
17346185db85Sdougm 
17356185db85Sdougm 	options = NULL;
17366185db85Sdougm 
17376185db85Sdougm 	if (hier &&
173825a68471Sdougm 	    (group = sa_get_parent_group((sa_share_t)object)) != NULL)
173925a68471Sdougm 		options = sa_get_all_security_types((void *)group, proto, hier);
174025a68471Sdougm 	else
174125a68471Sdougm 		options = (sa_optionset_t)xmlNewNode(NULL,
174225a68471Sdougm 		    (xmlChar *)"optionset");
174325a68471Sdougm 
174425a68471Sdougm 	if (options == NULL)
174525a68471Sdougm 		return (options);
174625a68471Sdougm 
174725a68471Sdougm 	/* Hit the top so collect the security types working back. */
174825a68471Sdougm 	for (security = sa_get_security((sa_group_t)object, NULL, NULL);
174925a68471Sdougm 	    security != NULL;
175025a68471Sdougm 	    security = sa_get_next_security(security)) {
17516185db85Sdougm 		char *type;
17526185db85Sdougm 		char *sectype;
17536185db85Sdougm 
17546185db85Sdougm 		type = sa_get_security_attr(security, "type");
17556185db85Sdougm 		if (type != NULL) {
175625a68471Sdougm 			if (strcmp(type, proto) != 0) {
175725a68471Sdougm 				sa_free_attr_string(type);
175825a68471Sdougm 				continue;
175925a68471Sdougm 			}
176025a68471Sdougm 			sectype = sa_get_security_attr(security, "sectype");
176125a68471Sdougm 			if (sectype != NULL) {
176225a68471Sdougm 				/*
176325a68471Sdougm 				 * Have a security type, check to see if
176425a68471Sdougm 				 * already present in optionset and add if it
176525a68471Sdougm 				 * isn't.
176625a68471Sdougm 				 */
176725a68471Sdougm 				if (sa_get_property(options, sectype) == NULL) {
176825a68471Sdougm 					prop = sa_create_property(sectype,
176925a68471Sdougm 					    "true");
177025a68471Sdougm 					if (prop != NULL)
177125a68471Sdougm 						prop = (sa_property_t)
177225a68471Sdougm 						    xmlAddChild(
177325a68471Sdougm 						    (xmlNodePtr)options,
177425a68471Sdougm 						    (xmlNodePtr)prop);
177525a68471Sdougm 				}
177625a68471Sdougm 				sa_free_attr_string(sectype);
17776185db85Sdougm 			}
177825a68471Sdougm 			sa_free_attr_string(type);
17796185db85Sdougm 		}
17806185db85Sdougm 	}
178125a68471Sdougm 
17826185db85Sdougm 	return (options);
17836185db85Sdougm }
17846185db85Sdougm 
17856185db85Sdougm /*
17866185db85Sdougm  * sa_get_derived_security(object, sectype, proto, hier)
17876185db85Sdougm  *
17886185db85Sdougm  * Get the derived security(named optionset) for the object given the
17896185db85Sdougm  * sectype and proto. If hier is non-zero, walk up the tree to get all
17906185db85Sdougm  * properties defined for this object, otherwise just those on the
17916185db85Sdougm  * object.
17926185db85Sdougm  */
17936185db85Sdougm 
17946185db85Sdougm sa_security_t
17956185db85Sdougm sa_get_derived_security(void *object, char *sectype, char *proto, int hier)
17966185db85Sdougm {
17976185db85Sdougm 	sa_security_t newsecurity;
17986185db85Sdougm 	sa_security_t security;
17996185db85Sdougm 	sa_group_t group;
180025a68471Sdougm 	sa_property_t prop;
18016185db85Sdougm 
18026185db85Sdougm 	if (hier &&
18036185db85Sdougm 	    (group = sa_get_parent_group((sa_share_t)object)) != NULL) {
180425a68471Sdougm 		newsecurity = sa_get_derived_security((void *)group,
180525a68471Sdougm 		    sectype, proto, hier);
18066185db85Sdougm 	} else {
180725a68471Sdougm 		newsecurity = (sa_security_t)xmlNewNode(NULL,
180825a68471Sdougm 		    (xmlChar *)"security");
180925a68471Sdougm 		if (newsecurity != NULL) {
181025a68471Sdougm 			sa_set_security_attr(newsecurity, "type", proto);
181125a68471Sdougm 			sa_set_security_attr(newsecurity, "sectype", sectype);
181225a68471Sdougm 		}
18136185db85Sdougm 	}
181425a68471Sdougm 	/* Don't do anything if memory wasn't allocated */
18156185db85Sdougm 	if (newsecurity == NULL)
181625a68471Sdougm 		return (newsecurity);
18176185db85Sdougm 
181825a68471Sdougm 	/* Found the top so working back down the stack. */
18196185db85Sdougm 	security = sa_get_security((sa_security_t)object, sectype, proto);
182025a68471Sdougm 	if (security == NULL)
182125a68471Sdougm 		return (newsecurity);
182225a68471Sdougm 
182325a68471Sdougm 	/* add security to the newsecurity */
182425a68471Sdougm 	for (prop = sa_get_property(security, NULL);
182525a68471Sdougm 	    prop != NULL; prop = sa_get_next_property(prop)) {
18266185db85Sdougm 		sa_property_t newprop;
18276185db85Sdougm 		char *name;
18286185db85Sdougm 		char *value;
18296185db85Sdougm 		name = sa_get_property_attr(prop, "type");
18306185db85Sdougm 		value = sa_get_property_attr(prop, "value");
18316185db85Sdougm 		if (name != NULL) {
183225a68471Sdougm 			newprop = sa_get_property(newsecurity, name);
183325a68471Sdougm 			/* Replace the value with the new value */
183425a68471Sdougm 			if (newprop != NULL) {
183525a68471Sdougm 				/*
183625a68471Sdougm 				 * Only set if value is non NULL, old value ok
183725a68471Sdougm 				 * if it is NULL.
183825a68471Sdougm 				 */
183925a68471Sdougm 				if (value != NULL)
184025a68471Sdougm 					set_node_attr(newprop, name, value);
184125a68471Sdougm 			} else {
184225a68471Sdougm 				/* An entirely new property */
184325a68471Sdougm 				if (value != NULL) {
184425a68471Sdougm 					newprop = sa_create_property(name,
184525a68471Sdougm 					    value);
184625a68471Sdougm 					newprop = (sa_property_t)
184725a68471Sdougm 					    xmlAddChild((xmlNodePtr)newsecurity,
18486185db85Sdougm 					    (xmlNodePtr)newprop);
184925a68471Sdougm 				}
18506185db85Sdougm 			}
185125a68471Sdougm 			sa_free_attr_string(name);
18526185db85Sdougm 		}
18536185db85Sdougm 		if (value != NULL)
185425a68471Sdougm 			sa_free_attr_string(value);
18556185db85Sdougm 	}
18566185db85Sdougm 	return (newsecurity);
18576185db85Sdougm }
18586185db85Sdougm 
18596185db85Sdougm void
18606185db85Sdougm sa_free_derived_security(sa_security_t security)
18616185db85Sdougm {
18626185db85Sdougm 	/* while it shouldn't be linked, it doesn't hurt */
18636185db85Sdougm 	if (security != NULL) {
186425a68471Sdougm 		xmlUnlinkNode((xmlNodePtr)security);
186525a68471Sdougm 		xmlFreeNode((xmlNodePtr)security);
18666185db85Sdougm 	}
18676185db85Sdougm }
18686185db85Sdougm 
18696185db85Sdougm /*
18706185db85Sdougm  * sharetab utility functions
18716185db85Sdougm  *
187225a68471Sdougm  * Makes use of the original sharetab.c from fs.d/nfs/lib
18736185db85Sdougm  */
18746185db85Sdougm 
18756185db85Sdougm /*
1876ecd6cf80Smarks  * sa_fillshare(share, proto, sh)
18776185db85Sdougm  *
18786185db85Sdougm  * Fill the struct share with values obtained from the share object.
18796185db85Sdougm  */
1880ecd6cf80Smarks void
1881ecd6cf80Smarks sa_fillshare(sa_share_t share, char *proto, struct share *sh)
18826185db85Sdougm {
18836185db85Sdougm 	char *groupname = NULL;
18846185db85Sdougm 	char *value;
18856185db85Sdougm 	sa_group_t group;
18866185db85Sdougm 	char *buff;
18876185db85Sdougm 	char *zfs;
18886185db85Sdougm 
18896185db85Sdougm 	group = sa_get_parent_group(share);
18906185db85Sdougm 	if (group != NULL) {
189125a68471Sdougm 		zfs = sa_get_group_attr(group, "zfs");
189225a68471Sdougm 		groupname = sa_get_group_attr(group, "name");
18936185db85Sdougm 
189425a68471Sdougm 		if (groupname != NULL &&
189525a68471Sdougm 		    (strcmp(groupname, "default") == 0 || zfs != NULL)) {
189625a68471Sdougm 			/*
189725a68471Sdougm 			 * since the groupname is either "default" or the
189825a68471Sdougm 			 * group is a ZFS group, we don't want to keep
189925a68471Sdougm 			 * groupname. We do want it if it is any other type of
190025a68471Sdougm 			 * group.
190125a68471Sdougm 			 */
190225a68471Sdougm 			sa_free_attr_string(groupname);
190325a68471Sdougm 			groupname = NULL;
190425a68471Sdougm 		}
190525a68471Sdougm 		if (zfs != NULL)
190625a68471Sdougm 			sa_free_attr_string(zfs);
19076185db85Sdougm 	}
19086185db85Sdougm 
19096185db85Sdougm 	value = sa_get_share_attr(share, "path");
19106185db85Sdougm 	if (value != NULL) {
191125a68471Sdougm 		sh->sh_path = strdup(value);
191225a68471Sdougm 		sa_free_attr_string(value);
19136185db85Sdougm 	}
19146185db85Sdougm 
19156185db85Sdougm 	value = sa_get_share_attr(share, "resource");
19166185db85Sdougm 	if (value != NULL || groupname != NULL) {
191725a68471Sdougm 		int len = 0;
191825a68471Sdougm 
191925a68471Sdougm 		if (value != NULL)
192025a68471Sdougm 			len += strlen(value);
192125a68471Sdougm 		if (groupname != NULL)
192225a68471Sdougm 			len += strlen(groupname);
192325a68471Sdougm 		len += 3; /* worst case */
192425a68471Sdougm 		buff = malloc(len);
192525a68471Sdougm 		(void) snprintf(buff, len, "%s%s%s",
19266185db85Sdougm 		    (value != NULL && strlen(value) > 0) ? value : "-",
19276185db85Sdougm 		    groupname != NULL ? "@" : "",
19286185db85Sdougm 		    groupname != NULL ? groupname : "");
192925a68471Sdougm 		sh->sh_res = buff;
193025a68471Sdougm 		if (value != NULL)
193125a68471Sdougm 			sa_free_attr_string(value);
193225a68471Sdougm 		if (groupname != NULL) {
193325a68471Sdougm 			sa_free_attr_string(groupname);
193425a68471Sdougm 			groupname = NULL;
193525a68471Sdougm 		}
19366185db85Sdougm 	} else {
193725a68471Sdougm 		sh->sh_res = strdup("-");
19386185db85Sdougm 	}
19396185db85Sdougm 
19406185db85Sdougm 	sh->sh_fstype = strdup(proto);
19416185db85Sdougm 	value = sa_proto_legacy_format(proto, share, 1);
19426185db85Sdougm 	if (value != NULL) {
194325a68471Sdougm 		if (strlen(value) > 0)
194425a68471Sdougm 			sh->sh_opts = strdup(value);
194525a68471Sdougm 		else
194625a68471Sdougm 			sh->sh_opts = strdup("rw");
194725a68471Sdougm 		free(value);
194825a68471Sdougm 	} else {
19496185db85Sdougm 		sh->sh_opts = strdup("rw");
195025a68471Sdougm 	}
19516185db85Sdougm 
19526185db85Sdougm 	value = sa_get_share_description(share);
19536185db85Sdougm 	if (value != NULL) {
195425a68471Sdougm 		sh->sh_descr = strdup(value);
195525a68471Sdougm 		sa_free_share_description(value);
195625a68471Sdougm 	} else {
195725a68471Sdougm 		sh->sh_descr = strdup("");
195825a68471Sdougm 	}
19596185db85Sdougm }
19606185db85Sdougm 
19616185db85Sdougm /*
1962ecd6cf80Smarks  * sa_emptyshare(sh)
19636185db85Sdougm  *
19646185db85Sdougm  * Free the strings in the non-NULL members of sh.
19656185db85Sdougm  */
19666185db85Sdougm 
1967ecd6cf80Smarks void
1968ecd6cf80Smarks sa_emptyshare(struct share *sh)
19696185db85Sdougm {
19706185db85Sdougm 	if (sh->sh_path != NULL)
197125a68471Sdougm 		free(sh->sh_path);
19726185db85Sdougm 	sh->sh_path = NULL;
19736185db85Sdougm 	if (sh->sh_res != NULL)
197425a68471Sdougm 		free(sh->sh_res);
19756185db85Sdougm 	sh->sh_res = NULL;
19766185db85Sdougm 	if (sh->sh_fstype != NULL)
197725a68471Sdougm 		free(sh->sh_fstype);
19786185db85Sdougm 	sh->sh_fstype = NULL;
19796185db85Sdougm 	if (sh->sh_opts != NULL)
198025a68471Sdougm 		free(sh->sh_opts);
19816185db85Sdougm 	sh->sh_opts = NULL;
19826185db85Sdougm 	if (sh->sh_descr != NULL)
198325a68471Sdougm 		free(sh->sh_descr);
19846185db85Sdougm 	sh->sh_descr = NULL;
19856185db85Sdougm }
19866185db85Sdougm 
19876185db85Sdougm /*
19886185db85Sdougm  * sa_update_sharetab(share, proto)
19896185db85Sdougm  *
19906185db85Sdougm  * Update the sharetab file with info from the specified share.
19916185db85Sdougm  * This could be an update or add.
19926185db85Sdougm  */
19936185db85Sdougm 
19946185db85Sdougm int
19956185db85Sdougm sa_update_sharetab(sa_share_t share, char *proto)
19966185db85Sdougm {
1997a237e38eSth 	int	ret = SA_OK;
1998a237e38eSth 	share_t	sh;
1999a237e38eSth 	char	*path;
20006185db85Sdougm 
20016185db85Sdougm 	path = sa_get_share_attr(share, "path");
20026185db85Sdougm 	if (path != NULL) {
2003a237e38eSth 		(void) memset(&sh, '\0', sizeof (sh));
2004a237e38eSth 
2005a99982a7Sdougm 		/*
2006a237e38eSth 		 * Fill in share structure and send it to the kernel.
2007a99982a7Sdougm 		 */
2008ecd6cf80Smarks 		(void) sa_fillshare(share, proto, &sh);
2009*a3175730Sth 		(void) _sharefs(SHAREFS_ADD, &sh);
2010ecd6cf80Smarks 		sa_emptyshare(&sh);
2011a237e38eSth 		sa_free_attr_string(path);
20126185db85Sdougm 	}
2013a237e38eSth 
20146185db85Sdougm 	return (ret);
20156185db85Sdougm }
20166185db85Sdougm 
20176185db85Sdougm /*
20186185db85Sdougm  * sa_delete_sharetab(path, proto)
20196185db85Sdougm  *
20206185db85Sdougm  * remove the specified share from sharetab.
20216185db85Sdougm  */
20226185db85Sdougm 
20236185db85Sdougm int
20246185db85Sdougm sa_delete_sharetab(char *path, char *proto)
20256185db85Sdougm {
2026a237e38eSth 	int	ret = SA_OK;
20276185db85Sdougm 
2028a237e38eSth 	share_t	sh;
2029a237e38eSth 
2030a237e38eSth 	/*
2031a237e38eSth 	 * Both the path and the proto are
2032a237e38eSth 	 * keys into the sharetab.
2033a237e38eSth 	 */
2034a237e38eSth 	if (path != NULL && proto != NULL) {
2035a237e38eSth 		(void) memset(&sh, '\0', sizeof (sh));
2036a237e38eSth 		sh.sh_path = path;
2037a237e38eSth 		sh.sh_fstype = proto;
2038a237e38eSth 
2039*a3175730Sth 		ret = _sharefs(SHAREFS_REMOVE, &sh);
20406185db85Sdougm 	}
2041a237e38eSth 
20426185db85Sdougm 	return (ret);
20436185db85Sdougm }
2044