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 /*
23*5b6e0c46Sdougm  * Copyright 2008 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>
54da6c28aaSamw #include <dirent.h>
556185db85Sdougm 
56a237e38eSth #include <sharefs/share.h>
576185db85Sdougm #include "sharetab.h"
586185db85Sdougm 
596185db85Sdougm #define	DFSTAB_NOTICE_LINES	5
606185db85Sdougm static char *notice[DFSTAB_NOTICE_LINES] =	{
616185db85Sdougm 	"# Do not modify this file directly.\n",
626185db85Sdougm 	"# Use the sharemgr(1m) command for all share management\n",
636185db85Sdougm 	"# This file is reconstructed and only maintained for backward\n",
646185db85Sdougm 	"# compatibility. Configuration lines could be lost.\n",
656185db85Sdougm 	"#\n"
666185db85Sdougm };
676185db85Sdougm 
686185db85Sdougm #define	STRNCAT(x, y, z)	(xmlChar *)strncat((char *)x, (char *)y, z)
696185db85Sdougm 
706185db85Sdougm /* will be much smaller, but this handles bad syntax in the file */
716185db85Sdougm #define	MAXARGSFORSHARE	256
726185db85Sdougm 
736185db85Sdougm /* used internally only */
746185db85Sdougm typedef
756185db85Sdougm struct sharelist {
766185db85Sdougm     struct sharelist *next;
776185db85Sdougm     int   persist;
786185db85Sdougm     char *path;
796185db85Sdougm     char *resource;
806185db85Sdougm     char *fstype;
816185db85Sdougm     char *options;
826185db85Sdougm     char *description;
836185db85Sdougm     char *group;
846185db85Sdougm     char *origline;
856185db85Sdougm     int lineno;
866185db85Sdougm } xfs_sharelist_t;
87549ec3ffSdougm static void parse_dfstab(sa_handle_t, char *, xmlNodePtr);
886185db85Sdougm extern char *get_token(char *);
896185db85Sdougm static void dfs_free_list(xfs_sharelist_t *);
906185db85Sdougm /* prototypes */
91549ec3ffSdougm void getlegacyconfig(sa_handle_t, char *, xmlNodePtr *);
92da6c28aaSamw extern sa_share_t _sa_add_share(sa_group_t, char *, int, int *, uint64_t);
93549ec3ffSdougm extern sa_group_t _sa_create_group(sa_handle_impl_t, char *);
946185db85Sdougm static void outdfstab(FILE *, xfs_sharelist_t *);
956185db85Sdougm extern int _sa_remove_optionset(sa_optionset_t);
966185db85Sdougm extern int set_node_share(void *, char *, char *);
976185db85Sdougm extern void set_node_attr(void *, char *, char *);
986185db85Sdougm 
99a99982a7Sdougm /*
100a99982a7Sdougm  * sablocksigs(*sigs)
101a99982a7Sdougm  *
102a99982a7Sdougm  * block important signals for a critical region. Arg is a pointer to
103a99982a7Sdougm  * a sigset_t that is used later for the unblock.
104a99982a7Sdougm  */
105a99982a7Sdougm void
106a99982a7Sdougm sablocksigs(sigset_t *sigs)
107a99982a7Sdougm {
108a99982a7Sdougm 	sigset_t new;
109a99982a7Sdougm 
110a99982a7Sdougm 	if (sigs != NULL) {
11125a68471Sdougm 		(void) sigprocmask(SIG_BLOCK, NULL, &new);
11225a68471Sdougm 		(void) sigaddset(&new, SIGHUP);
11325a68471Sdougm 		(void) sigaddset(&new, SIGINT);
11425a68471Sdougm 		(void) sigaddset(&new, SIGQUIT);
11525a68471Sdougm 		(void) sigaddset(&new, SIGTSTP);
11625a68471Sdougm 		(void) sigprocmask(SIG_SETMASK, &new, sigs);
117a99982a7Sdougm 	}
118a99982a7Sdougm }
119a99982a7Sdougm 
120a99982a7Sdougm /*
121a99982a7Sdougm  * saunblocksigs(*sigs)
122a99982a7Sdougm  *
123a99982a7Sdougm  * unblock previously blocked signals from the sigs arg.
124a99982a7Sdougm  */
125a99982a7Sdougm void
126a99982a7Sdougm saunblocksigs(sigset_t *sigs)
127a99982a7Sdougm {
128a99982a7Sdougm 	if (sigs != NULL)
12925a68471Sdougm 		(void) sigprocmask(SIG_SETMASK, sigs, NULL);
130a99982a7Sdougm }
131a99982a7Sdougm 
1326185db85Sdougm /*
1336185db85Sdougm  * alloc_sharelist()
1346185db85Sdougm  *
1356185db85Sdougm  * allocator function to return an zfs_sharelist_t
1366185db85Sdougm  */
1376185db85Sdougm 
1386185db85Sdougm static xfs_sharelist_t *
1396185db85Sdougm alloc_sharelist()
1406185db85Sdougm {
1416185db85Sdougm 	xfs_sharelist_t *item;
1426185db85Sdougm 
1436185db85Sdougm 	item = (xfs_sharelist_t *)malloc(sizeof (xfs_sharelist_t));
1446185db85Sdougm 	if (item != NULL)
14525a68471Sdougm 		(void) memset(item, '\0', sizeof (xfs_sharelist_t));
1466185db85Sdougm 	return (item);
1476185db85Sdougm }
1486185db85Sdougm 
1496185db85Sdougm /*
1506185db85Sdougm  * fix_notice(list)
1516185db85Sdougm  *
1526185db85Sdougm  * Look at the beginning of the current /etc/dfs/dfstab file and add
1536185db85Sdougm  * the do not modify notice if it doesn't exist.
1546185db85Sdougm  */
1556185db85Sdougm 
1566185db85Sdougm static xfs_sharelist_t *
1576185db85Sdougm fix_notice(xfs_sharelist_t *list)
1586185db85Sdougm {
1596185db85Sdougm 	xfs_sharelist_t *item, *prev;
1606185db85Sdougm 	int i;
1616185db85Sdougm 
1627d968cb8Sdougm 	if (list == NULL) {
16325a68471Sdougm 		/* zero length dfstab */
16425a68471Sdougm 		list = alloc_sharelist();
16525a68471Sdougm 		if (list == NULL)
16625a68471Sdougm 			return (NULL);
16725a68471Sdougm 		list->description = strdup("#\n");
1687d968cb8Sdougm 	}
1696185db85Sdougm 	if (list->path == NULL && list->description != NULL &&
1706185db85Sdougm 	    strcmp(list->description, notice[0]) != 0) {
17125a68471Sdougm 		for (prev = NULL, i = 0; i < DFSTAB_NOTICE_LINES; i++) {
17225a68471Sdougm 			item = alloc_sharelist();
17325a68471Sdougm 			if (item != NULL) {
17425a68471Sdougm 				item->description = strdup(notice[i]);
17525a68471Sdougm 				if (prev == NULL) {
17625a68471Sdougm 					item->next = list;
17725a68471Sdougm 					prev = item;
17825a68471Sdougm 					list = item;
17925a68471Sdougm 				} else {
18025a68471Sdougm 					item->next = prev->next;
18125a68471Sdougm 					prev->next = item;
18225a68471Sdougm 					prev = item;
18325a68471Sdougm 				}
18425a68471Sdougm 			}
1856185db85Sdougm 		}
1866185db85Sdougm 	}
1876185db85Sdougm 	return (list);
1886185db85Sdougm }
1896185db85Sdougm 
1906185db85Sdougm /*
1916185db85Sdougm  * getdfstab(dfs)
1926185db85Sdougm  *
1936185db85Sdougm  * Returns an zfs_sharelist_t list of lines from the dfstab file
1946185db85Sdougm  * pointed to by the FILE pointer dfs. Each entry is parsed and the
1956185db85Sdougm  * original line is also preserved. Used in parsing and updating the
1966185db85Sdougm  * dfstab file.
1976185db85Sdougm  */
1986185db85Sdougm 
1996185db85Sdougm static xfs_sharelist_t *
2006185db85Sdougm getdfstab(FILE *dfs)
2016185db85Sdougm {
2026185db85Sdougm 	char buff[_POSIX_ARG_MAX]; /* reasonable size given syntax of share */
2036185db85Sdougm 	char *bp;
2046185db85Sdougm 	char *token;
2056185db85Sdougm 	char *args[MAXARGSFORSHARE];
2066185db85Sdougm 	int argc;
2076185db85Sdougm 	int c;
2086185db85Sdougm 	static int line = 0;
2097d968cb8Sdougm 	xfs_sharelist_t *item = NULL, *first = NULL, *last;
2106185db85Sdougm 
2116185db85Sdougm 	if (dfs != NULL) {
21225a68471Sdougm 		first = NULL;
21325a68471Sdougm 		line = 0;
21425a68471Sdougm 		while (fgets(buff, sizeof (buff), dfs) != NULL) {
21525a68471Sdougm 			line++;
21625a68471Sdougm 			bp = buff;
21725a68471Sdougm 			if (buff[0] == '#') {
21825a68471Sdougm 				item = alloc_sharelist();
21925a68471Sdougm 				if (item != NULL) {
22025a68471Sdougm 					/* if no path, then comment */
22125a68471Sdougm 					item->lineno = line;
22225a68471Sdougm 					item->description = strdup(buff);
22325a68471Sdougm 					if (first == NULL) {
22425a68471Sdougm 						first = item;
22525a68471Sdougm 						last = item;
22625a68471Sdougm 					} else {
22725a68471Sdougm 						last->next = item;
22825a68471Sdougm 						last = item;
22925a68471Sdougm 					}
23025a68471Sdougm 				} else {
23125a68471Sdougm 					break;
23225a68471Sdougm 				}
23325a68471Sdougm 				continue;
23425a68471Sdougm 			} else if (buff[0] == '\n') {
23525a68471Sdougm 				continue;
23625a68471Sdougm 			}
23725a68471Sdougm 			optind = 1;
23825a68471Sdougm 			item = alloc_sharelist();
23925a68471Sdougm 			if (item == NULL) {
24025a68471Sdougm 				break;
24125a68471Sdougm 			} else if (first == NULL) {
24225a68471Sdougm 				first = item;
24325a68471Sdougm 				last = item;
2446185db85Sdougm 			} else {
24525a68471Sdougm 				last->next = item;
24625a68471Sdougm 				last = item;
2476185db85Sdougm 			}
24825a68471Sdougm 			item->lineno = line;
24925a68471Sdougm 			item->origline = strdup(buff);
25025a68471Sdougm 			(void) get_token(NULL); /* reset to new pointers */
25125a68471Sdougm 			argc = 0;
25225a68471Sdougm 			while ((token = get_token(bp)) != NULL) {
25325a68471Sdougm 				if (argc < MAXARGSFORSHARE)
25425a68471Sdougm 					args[argc++] = token;
2556185db85Sdougm 			}
25625a68471Sdougm 			while ((c = getopt(argc, args, "F:o:d:pg:")) != -1) {
25725a68471Sdougm 				switch (c) {
25825a68471Sdougm 				case 'p':
25925a68471Sdougm 					item->persist = 1;
26025a68471Sdougm 					break;
26125a68471Sdougm 				case 'F':
26225a68471Sdougm 					item->fstype = strdup(optarg);
26325a68471Sdougm 					break;
26425a68471Sdougm 				case 'o':
26525a68471Sdougm 					item->options = strdup(optarg);
26625a68471Sdougm 					break;
26725a68471Sdougm 				case 'd':
26825a68471Sdougm 					item->description = strdup(optarg);
26925a68471Sdougm 					break;
27025a68471Sdougm 				case 'g':
27125a68471Sdougm 					item->group = strdup(optarg);
27225a68471Sdougm 					break;
27325a68471Sdougm 				default:
27425a68471Sdougm 					break;
27525a68471Sdougm 				}
27625a68471Sdougm 			}
27725a68471Sdougm 			if (optind < argc) {
27825a68471Sdougm 				item->path = strdup(args[optind]);
27925a68471Sdougm 				optind++;
28025a68471Sdougm 				if (optind < argc) {
28125a68471Sdougm 					char *resource;
28225a68471Sdougm 					char *optgroup;
28325a68471Sdougm 					/* resource and/or groupname */
28425a68471Sdougm 					resource = args[optind];
28525a68471Sdougm 					optgroup = strchr(resource, '@');
28625a68471Sdougm 					if (optgroup != NULL)
28725a68471Sdougm 						*optgroup++ = '\0';
28825a68471Sdougm 					if (optgroup != NULL)
28925a68471Sdougm 						item->group = strdup(optgroup);
29025a68471Sdougm 					if (resource != NULL &&
29125a68471Sdougm 					    strlen(resource) > 0)
29225a68471Sdougm 						item->resource =
29325a68471Sdougm 						    strdup(resource);
29425a68471Sdougm 				}
29525a68471Sdougm 			}
29625a68471Sdougm 			/* NFS is the default if none defined */
29725a68471Sdougm 			if (item != NULL && item->fstype == NULL)
29825a68471Sdougm 				item->fstype = strdup("nfs");
299a99982a7Sdougm 		}
3006185db85Sdougm 	}
3016185db85Sdougm 	first = fix_notice(first);
3026185db85Sdougm 	return (first);
3036185db85Sdougm }
3046185db85Sdougm 
3056185db85Sdougm /*
3066185db85Sdougm  * finddfsentry(list, path)
3076185db85Sdougm  *
3087d968cb8Sdougm  * Look for path in the zfs_sharelist_t list and return the entry if it
3096185db85Sdougm  * exists.
3106185db85Sdougm  */
3116185db85Sdougm 
3126185db85Sdougm static xfs_sharelist_t *
3136185db85Sdougm finddfsentry(xfs_sharelist_t *list, char *path)
3146185db85Sdougm {
3156185db85Sdougm 	xfs_sharelist_t *item;
3166185db85Sdougm 
3176185db85Sdougm 	for (item = list; item != NULL; item = item->next) {
31825a68471Sdougm 		if (item->path != NULL && strcmp(item->path, path) == 0)
3196185db85Sdougm 		return (item);
3206185db85Sdougm 	}
3216185db85Sdougm 	return (NULL);
3226185db85Sdougm }
3236185db85Sdougm 
3246185db85Sdougm /*
3256185db85Sdougm  * remdfsentry(list, path, proto)
3266185db85Sdougm  *
3276185db85Sdougm  * Remove the specified path (with protocol) from the list. This will
3286185db85Sdougm  * remove it from dfstab when the file is rewritten.
3296185db85Sdougm  */
3306185db85Sdougm 
3316185db85Sdougm static xfs_sharelist_t *
3326185db85Sdougm remdfsentry(xfs_sharelist_t *list, char *path, char *proto)
3336185db85Sdougm {
3346185db85Sdougm 	xfs_sharelist_t *item, *prev = NULL;
3356185db85Sdougm 
3366185db85Sdougm 
3376185db85Sdougm 	for (item = prev = list; item != NULL; item = item->next) {
3386185db85Sdougm 	    /* skip comment entry but don't lose it */
33925a68471Sdougm 		if (item->path == NULL) {
34025a68471Sdougm 			prev = item;
34125a68471Sdougm 			continue;
34225a68471Sdougm 		}
34325a68471Sdougm 		/* if proto is NULL, remove all protocols */
34425a68471Sdougm 		if (proto == NULL || (strcmp(item->path, path) == 0 &&
34525a68471Sdougm 		    (item->fstype != NULL && strcmp(item->fstype, proto) == 0)))
34625a68471Sdougm 			break;
34725a68471Sdougm 		if (item->fstype == NULL &&
34825a68471Sdougm 		    (proto == NULL || strcmp(proto, "nfs") == 0))
34925a68471Sdougm 			break;
3506185db85Sdougm 		prev = item;
3516185db85Sdougm 	}
3526185db85Sdougm 	if (item != NULL) {
35325a68471Sdougm 		if (item == prev)
35425a68471Sdougm 			list = item->next; /* this must be the first one */
35525a68471Sdougm 		else
35625a68471Sdougm 			prev->next = item->next;
35725a68471Sdougm 		item->next = NULL;
35825a68471Sdougm 		dfs_free_list(item);
3596185db85Sdougm 	}
3606185db85Sdougm 	return (list);
3616185db85Sdougm }
3626185db85Sdougm 
3636185db85Sdougm /*
3646185db85Sdougm  * remdfsline(list, line)
3656185db85Sdougm  *
3666185db85Sdougm  * Remove the line specified from the list.
3676185db85Sdougm  */
3686185db85Sdougm 
3696185db85Sdougm static xfs_sharelist_t *
3706185db85Sdougm remdfsline(xfs_sharelist_t *list, char *line)
3716185db85Sdougm {
3726185db85Sdougm 	xfs_sharelist_t *item, *prev = NULL;
3736185db85Sdougm 
3746185db85Sdougm 	for (item = prev = list; item != NULL; item = item->next) {
37525a68471Sdougm 		/* skip comment entry but don't lose it */
37625a68471Sdougm 		if (item->path == NULL) {
3776185db85Sdougm 		prev = item;
3786185db85Sdougm 		continue;
37925a68471Sdougm 		}
38025a68471Sdougm 		if (strcmp(item->origline, line) == 0)
38125a68471Sdougm 			break;
38225a68471Sdougm 		prev = item;
3836185db85Sdougm 	}
3846185db85Sdougm 	if (item != NULL) {
38525a68471Sdougm 		if (item == prev)
38625a68471Sdougm 			list = item->next; /* this must be the first one */
38725a68471Sdougm 		else
38825a68471Sdougm 			prev->next = item->next;
38925a68471Sdougm 		item->next = NULL;
39025a68471Sdougm 		dfs_free_list(item);
3916185db85Sdougm 	}
3926185db85Sdougm 	return (list);
3936185db85Sdougm }
3946185db85Sdougm 
3956185db85Sdougm /*
3966185db85Sdougm  * adddfsentry(list, share, proto)
3976185db85Sdougm  *
3986185db85Sdougm  * Add an entry to the dfstab list for share (relative to proto). This
3996185db85Sdougm  * is used to update dfstab for legacy purposes.
4006185db85Sdougm  */
4016185db85Sdougm 
4026185db85Sdougm static xfs_sharelist_t *
4036185db85Sdougm adddfsentry(xfs_sharelist_t *list, sa_share_t share, char *proto)
4046185db85Sdougm {
4056185db85Sdougm 	xfs_sharelist_t *item, *tmp;
4066185db85Sdougm 	sa_group_t parent;
4076185db85Sdougm 	char *groupname;
4086185db85Sdougm 
4096185db85Sdougm 	item = alloc_sharelist();
4106185db85Sdougm 	if (item != NULL) {
41125a68471Sdougm 		parent = sa_get_parent_group(share);
41225a68471Sdougm 		groupname = sa_get_group_attr(parent, "name");
41325a68471Sdougm 		if (strcmp(groupname, "default") == 0) {
41425a68471Sdougm 			sa_free_attr_string(groupname);
41525a68471Sdougm 			groupname = NULL;
41625a68471Sdougm 		}
41725a68471Sdougm 		item->path = sa_get_share_attr(share, "path");
41825a68471Sdougm 		item->resource = sa_get_share_attr(share, "resource");
41925a68471Sdougm 		item->group = groupname;
42025a68471Sdougm 		item->fstype = strdup(proto);
42125a68471Sdougm 		item->options = sa_proto_legacy_format(proto, share, 1);
42225a68471Sdougm 		if (item->options != NULL && strlen(item->options) == 0) {
42325a68471Sdougm 			free(item->options);
42425a68471Sdougm 			item->options = NULL;
42525a68471Sdougm 		}
42625a68471Sdougm 		item->description = sa_get_share_description(share);
42725a68471Sdougm 		if (item->description != NULL &&
42825a68471Sdougm 		    strlen(item->description) == 0) {
42925a68471Sdougm 			sa_free_share_description(item->description);
43025a68471Sdougm 			item->description = NULL;
43125a68471Sdougm 		}
43225a68471Sdougm 		if (list == NULL) {
43325a68471Sdougm 			list = item;
43425a68471Sdougm 		} else {
43525a68471Sdougm 			for (tmp = list; tmp->next != NULL; tmp = tmp->next)
43625a68471Sdougm 				/* do nothing */;
43725a68471Sdougm 				tmp->next = item;
43825a68471Sdougm 		}
4396185db85Sdougm 	}
4406185db85Sdougm 	return (list);
4416185db85Sdougm }
4426185db85Sdougm 
4436185db85Sdougm /*
4446185db85Sdougm  * outdfstab(dfstab, list)
4456185db85Sdougm  *
4466185db85Sdougm  * Output the list to dfstab making sure the file is truncated.
4476185db85Sdougm  * Comments and errors are preserved.
4486185db85Sdougm  */
4496185db85Sdougm 
4506185db85Sdougm static void
4516185db85Sdougm outdfstab(FILE *dfstab, xfs_sharelist_t *list)
4526185db85Sdougm {
4536185db85Sdougm 	xfs_sharelist_t *item;
4546185db85Sdougm 
4556185db85Sdougm 	(void) ftruncate(fileno(dfstab), 0);
4566185db85Sdougm 
4576185db85Sdougm 	for (item = list; item != NULL; item = item->next) {
45825a68471Sdougm 		if (item->path != NULL) {
45925a68471Sdougm 			if (*item->path == '/') {
46025a68471Sdougm 				(void) fprintf(dfstab,
46125a68471Sdougm 				    "share %s%s%s%s%s%s%s %s%s%s%s%s\n",
46225a68471Sdougm 				    (item->fstype != NULL) ? "-F " : "",
46325a68471Sdougm 				    (item->fstype != NULL) ? item->fstype : "",
46425a68471Sdougm 				    (item->options != NULL) ? " -o " : "",
46525a68471Sdougm 				    (item->options != NULL) ?
46625a68471Sdougm 				    item->options : "",
46725a68471Sdougm 				    (item->description != NULL) ?
46825a68471Sdougm 				    " -d \"" : "",
46925a68471Sdougm 				    (item->description != NULL) ?
47025a68471Sdougm 				    item->description : "",
47125a68471Sdougm 				    (item->description != NULL) ? "\"" : "",
47225a68471Sdougm 				    item->path,
47325a68471Sdougm 				    ((item->resource != NULL) ||
47425a68471Sdougm 				    (item->group != NULL)) ? " " : "",
47525a68471Sdougm 				    (item->resource != NULL) ?
47625a68471Sdougm 				    item->resource : "",
47725a68471Sdougm 				    item->group != NULL ? "@" : "",
47825a68471Sdougm 				    item->group != NULL ? item->group : "");
47925a68471Sdougm 			} else {
48025a68471Sdougm 				(void) fprintf(dfstab, "%s", item->origline);
48125a68471Sdougm 			}
4826185db85Sdougm 		} else {
48325a68471Sdougm 			if (item->description != NULL)
48425a68471Sdougm 				(void) fprintf(dfstab, "%s", item->description);
48525a68471Sdougm 			else
48625a68471Sdougm 				(void) fprintf(dfstab, "%s", item->origline);
4876185db85Sdougm 		}
4886185db85Sdougm 	}
4896185db85Sdougm }
4906185db85Sdougm 
4916185db85Sdougm /*
4926185db85Sdougm  * open_dfstab(file)
4936185db85Sdougm  *
4946185db85Sdougm  * Open the specified dfstab file. If the owner/group/perms are wrong,
4956185db85Sdougm  * fix them.
4966185db85Sdougm  */
4976185db85Sdougm 
4986185db85Sdougm static FILE *
4996185db85Sdougm open_dfstab(char *file)
5006185db85Sdougm {
5016185db85Sdougm 	struct group *grp;
5026185db85Sdougm 	struct group group;
5036185db85Sdougm 	char *buff;
5046185db85Sdougm 	int grsize;
5056185db85Sdougm 	FILE *dfstab;
5066185db85Sdougm 
5076185db85Sdougm 	dfstab = fopen(file, "r+");
5086185db85Sdougm 	if (dfstab == NULL) {
50925a68471Sdougm 		dfstab = fopen(file, "w+");
5106185db85Sdougm 	}
5116185db85Sdougm 	if (dfstab != NULL) {
5126185db85Sdougm 		grsize = sysconf(_SC_GETGR_R_SIZE_MAX);
5136185db85Sdougm 		buff = malloc(grsize);
5146185db85Sdougm 		if (buff != NULL)
51525a68471Sdougm 			grp = getgrnam_r(SA_DEFAULT_FILE_GRP, &group, buff,
51625a68471Sdougm 			    grsize);
5176185db85Sdougm 		else
51825a68471Sdougm 			grp = getgrnam(SA_DEFAULT_FILE_GRP);
5196185db85Sdougm 		(void) fchmod(fileno(dfstab), 0644);
5206185db85Sdougm 		(void) fchown(fileno(dfstab), 0,
52125a68471Sdougm 		    grp != NULL ? grp->gr_gid : 3);
5226185db85Sdougm 		if (buff != NULL)
52325a68471Sdougm 			free(buff);
5246185db85Sdougm 		rewind(dfstab);
5256185db85Sdougm 	}
5266185db85Sdougm 	return (dfstab);
5276185db85Sdougm }
5286185db85Sdougm 
5296185db85Sdougm /*
5306185db85Sdougm  * sa_comment_line(line, err)
5316185db85Sdougm  *
5326185db85Sdougm  * Add a comment to the dfstab file with err as a prefix to the
5336185db85Sdougm  * original line.
5346185db85Sdougm  */
5356185db85Sdougm 
5366185db85Sdougm static void
5376185db85Sdougm sa_comment_line(char *line, char *err)
5386185db85Sdougm {
5396185db85Sdougm 	FILE *dfstab;
5406185db85Sdougm 	xfs_sharelist_t *list;
541a99982a7Sdougm 	sigset_t old;
5426185db85Sdougm 
5436185db85Sdougm 	dfstab = open_dfstab(SA_LEGACY_DFSTAB);
5446185db85Sdougm 	if (dfstab != NULL) {
5456185db85Sdougm 		(void) setvbuf(dfstab, NULL, _IOLBF, BUFSIZ * 8);
546a99982a7Sdougm 		sablocksigs(&old);
5476185db85Sdougm 		(void) lockf(fileno(dfstab), F_LOCK, 0);
5486185db85Sdougm 		list = getdfstab(dfstab);
5496185db85Sdougm 		rewind(dfstab);
550f345c0beSdougm 		/*
551f345c0beSdougm 		 * don't ignore the return since the list could have
552f345c0beSdougm 		 * gone to NULL if the file only had one line in it.
553f345c0beSdougm 		 */
554f345c0beSdougm 		list = remdfsline(list, line);
5556185db85Sdougm 		outdfstab(dfstab, list);
5566185db85Sdougm 		(void) fprintf(dfstab, "# Error: %s: %s", err, line);
5576185db85Sdougm 		(void) fsync(fileno(dfstab));
5586185db85Sdougm 		(void) lockf(fileno(dfstab), F_ULOCK, 0);
5596185db85Sdougm 		(void) fclose(dfstab);
560a99982a7Sdougm 		saunblocksigs(&old);
5616185db85Sdougm 		if (list != NULL)
56225a68471Sdougm 			dfs_free_list(list);
5636185db85Sdougm 	}
5646185db85Sdougm }
5656185db85Sdougm 
5666185db85Sdougm /*
567da6c28aaSamw  * sa_delete_legacy(share, protocol)
5686185db85Sdougm  *
5696185db85Sdougm  * Delete the specified share from the legacy config file.
5706185db85Sdougm  */
5716185db85Sdougm 
5726185db85Sdougm int
573da6c28aaSamw sa_delete_legacy(sa_share_t share, char *protocol)
5746185db85Sdougm {
5756185db85Sdougm 	FILE *dfstab;
5766185db85Sdougm 	int err;
5776185db85Sdougm 	int ret = SA_OK;
5786185db85Sdougm 	xfs_sharelist_t *list;
5796185db85Sdougm 	char *path;
5806185db85Sdougm 	sa_optionset_t optionset;
5816185db85Sdougm 	sa_group_t parent;
582a99982a7Sdougm 	sigset_t old;
5836185db85Sdougm 
584da6c28aaSamw 	/*
585da6c28aaSamw 	 * Protect against shares that don't have paths. This is not
586da6c28aaSamw 	 * really an error at this point.
587da6c28aaSamw 	 */
588da6c28aaSamw 	path = sa_get_share_attr(share, "path");
589da6c28aaSamw 	if (path == NULL)
590da6c28aaSamw 		return (ret);
591da6c28aaSamw 
5926185db85Sdougm 	dfstab = open_dfstab(SA_LEGACY_DFSTAB);
5936185db85Sdougm 	if (dfstab != NULL) {
5946185db85Sdougm 		(void) setvbuf(dfstab, NULL, _IOLBF, BUFSIZ * 8);
595a99982a7Sdougm 		sablocksigs(&old);
5966185db85Sdougm 		parent = sa_get_parent_group(share);
5976185db85Sdougm 		if (parent != NULL) {
59825a68471Sdougm 			(void) lockf(fileno(dfstab), F_LOCK, 0);
59925a68471Sdougm 			list = getdfstab(dfstab);
60025a68471Sdougm 			rewind(dfstab);
601da6c28aaSamw 			if (protocol != NULL) {
602da6c28aaSamw 				if (list != NULL)
60325a68471Sdougm 					list = remdfsentry(list, path,
604da6c28aaSamw 					    protocol);
605da6c28aaSamw 			} else {
606da6c28aaSamw 				for (optionset = sa_get_optionset(parent, NULL);
607da6c28aaSamw 				    optionset != NULL;
608da6c28aaSamw 				    optionset =
609da6c28aaSamw 				    sa_get_next_optionset(optionset)) {
610da6c28aaSamw 					char *proto = sa_get_optionset_attr(
611da6c28aaSamw 					    optionset, "type");
612da6c28aaSamw 
613da6c28aaSamw 					if (list != NULL && proto != NULL)
614da6c28aaSamw 						list = remdfsentry(list, path,
615da6c28aaSamw 						    proto);
616da6c28aaSamw 					if (proto == NULL)
617da6c28aaSamw 						ret = SA_NO_MEMORY;
618da6c28aaSamw 					/*
619da6c28aaSamw 					 * may want to only do the dfstab if
620da6c28aaSamw 					 * this call returns NOT IMPLEMENTED
621da6c28aaSamw 					 * but it shouldn't hurt.
622da6c28aaSamw 					 */
623da6c28aaSamw 					if (ret == SA_OK) {
624da6c28aaSamw 						err = sa_proto_delete_legacy(
625da6c28aaSamw 						    proto, share);
626da6c28aaSamw 						if (err != SA_NOT_IMPLEMENTED)
627da6c28aaSamw 							ret = err;
628da6c28aaSamw 					}
629da6c28aaSamw 					if (proto != NULL)
630da6c28aaSamw 						sa_free_attr_string(proto);
63125a68471Sdougm 				}
6326185db85Sdougm 			}
63325a68471Sdougm 			outdfstab(dfstab, list);
63425a68471Sdougm 			if (list != NULL)
63525a68471Sdougm 				dfs_free_list(list);
63625a68471Sdougm 			(void) fflush(dfstab);
63725a68471Sdougm 			(void) lockf(fileno(dfstab), F_ULOCK, 0);
6386185db85Sdougm 		}
6396185db85Sdougm 		(void) fsync(fileno(dfstab));
640a99982a7Sdougm 		saunblocksigs(&old);
6416185db85Sdougm 		(void) fclose(dfstab);
6426185db85Sdougm 	} else {
64325a68471Sdougm 		if (errno == EACCES || errno == EPERM)
64425a68471Sdougm 			ret = SA_NO_PERMISSION;
64525a68471Sdougm 		else
64625a68471Sdougm 			ret = SA_CONFIG_ERR;
6476185db85Sdougm 	}
648da6c28aaSamw 
649da6c28aaSamw 	if (path != NULL)
650da6c28aaSamw 		sa_free_attr_string(path);
651da6c28aaSamw 
6526185db85Sdougm 	return (ret);
6536185db85Sdougm }
6546185db85Sdougm 
6556185db85Sdougm /*
6566185db85Sdougm  * sa_update_legacy(share, proto)
6576185db85Sdougm  *
6586185db85Sdougm  * There is an assumption that dfstab will be the most common form of
6596185db85Sdougm  * legacy configuration file for shares, but not the only one. Because
6606185db85Sdougm  * of that, dfstab handling is done in the main code with calls to
661da6c28aaSamw  * this function and protocol specific calls to deal with formatting
6626185db85Sdougm  * options into dfstab/share compatible syntax. Since not everything
6636185db85Sdougm  * will be dfstab, there is a provision for calling a protocol
6646185db85Sdougm  * specific plugin interface that allows the protocol plugin to do its
6656185db85Sdougm  * own legacy files and skip the dfstab update.
6666185db85Sdougm  */
6676185db85Sdougm 
6686185db85Sdougm int
6696185db85Sdougm sa_update_legacy(sa_share_t share, char *proto)
6706185db85Sdougm {
6716185db85Sdougm 	FILE *dfstab;
6726185db85Sdougm 	int ret = SA_OK;
6736185db85Sdougm 	xfs_sharelist_t *list;
6746185db85Sdougm 	char *path;
675a99982a7Sdougm 	sigset_t old;
6766185db85Sdougm 	char *persist;
677da6c28aaSamw 	uint64_t features;
6786185db85Sdougm 
6796185db85Sdougm 	ret = sa_proto_update_legacy(proto, share);
6806185db85Sdougm 	if (ret != SA_NOT_IMPLEMENTED)
68125a68471Sdougm 		return (ret);
682da6c28aaSamw 
683da6c28aaSamw 	features = sa_proto_get_featureset(proto);
684da6c28aaSamw 	if (!(features & SA_FEATURE_DFSTAB))
685da6c28aaSamw 		return (ret);
686da6c28aaSamw 
6876185db85Sdougm 	/* do the dfstab format */
6886185db85Sdougm 	persist = sa_get_share_attr(share, "type");
6896185db85Sdougm 	/*
6906185db85Sdougm 	 * only update if the share is not transient -- no share type
6916185db85Sdougm 	 * set or the type is not "transient".
6926185db85Sdougm 	 */
6936185db85Sdougm 	if (persist == NULL || strcmp(persist, "transient") != 0) {
69425a68471Sdougm 		dfstab = open_dfstab(SA_LEGACY_DFSTAB);
69525a68471Sdougm 		if (dfstab != NULL) {
69625a68471Sdougm 			(void) setvbuf(dfstab, NULL, _IOLBF, BUFSIZ * 8);
69725a68471Sdougm 			sablocksigs(&old);
69825a68471Sdougm 			path = sa_get_share_attr(share, "path");
69925a68471Sdougm 			(void) lockf(fileno(dfstab), F_LOCK, 0);
70025a68471Sdougm 			list = getdfstab(dfstab);
70125a68471Sdougm 			rewind(dfstab);
70225a68471Sdougm 			if (list != NULL)
70325a68471Sdougm 				list = remdfsentry(list, path, proto);
70425a68471Sdougm 			list = adddfsentry(list, share, proto);
70525a68471Sdougm 			outdfstab(dfstab, list);
70625a68471Sdougm 			(void) fflush(dfstab);
70725a68471Sdougm 			(void) lockf(fileno(dfstab), F_ULOCK, 0);
70825a68471Sdougm 			(void) fsync(fileno(dfstab));
70925a68471Sdougm 			saunblocksigs(&old);
71025a68471Sdougm 			(void) fclose(dfstab);
71125a68471Sdougm 			sa_free_attr_string(path);
71225a68471Sdougm 			if (list != NULL)
71325a68471Sdougm 				dfs_free_list(list);
7146185db85Sdougm 		} else {
71525a68471Sdougm 			if (errno == EACCES || errno == EPERM)
71625a68471Sdougm 				ret = SA_NO_PERMISSION;
71725a68471Sdougm 			else
71825a68471Sdougm 				ret = SA_CONFIG_ERR;
7196185db85Sdougm 		}
7206185db85Sdougm 	}
7216185db85Sdougm 	if (persist != NULL)
72225a68471Sdougm 		sa_free_attr_string(persist);
7236185db85Sdougm 	return (ret);
7246185db85Sdougm }
7256185db85Sdougm 
7266185db85Sdougm /*
7276185db85Sdougm  * sa_is_security(optname, proto)
7286185db85Sdougm  *
7296185db85Sdougm  * Check to see if optname is a security (named optionset) specific
7306185db85Sdougm  * property for the specified protocol.
7316185db85Sdougm  */
7326185db85Sdougm 
7336185db85Sdougm int
7346185db85Sdougm sa_is_security(char *optname, char *proto)
7356185db85Sdougm {
7366185db85Sdougm 	int ret = 0;
7376185db85Sdougm 	if (proto != NULL)
73825a68471Sdougm 		ret = sa_proto_security_prop(proto, optname);
7396185db85Sdougm 	return (ret);
7406185db85Sdougm }
7416185db85Sdougm 
7426185db85Sdougm /*
7436185db85Sdougm  * add_syntax_comment(root, line, err, todfstab)
7446185db85Sdougm  *
74525a68471Sdougm  * Add a comment to the document indicating a syntax error. If
7466185db85Sdougm  * todfstab is set, write it back to the dfstab file as well.
7476185db85Sdougm  */
7486185db85Sdougm 
7496185db85Sdougm static void
7506185db85Sdougm add_syntax_comment(xmlNodePtr root, char *line, char *err, int todfstab)
7516185db85Sdougm {
7526185db85Sdougm 	xmlNodePtr node;
7536185db85Sdougm 
7546185db85Sdougm 	node = xmlNewChild(root, NULL, (xmlChar *)"error", (xmlChar *)line);
75525a68471Sdougm 	if (node != NULL)
75625a68471Sdougm 		xmlSetProp(node, (xmlChar *)"type", (xmlChar *)err);
7576185db85Sdougm 	if (todfstab)
75825a68471Sdougm 		sa_comment_line(line, err);
7596185db85Sdougm }
7606185db85Sdougm 
7616185db85Sdougm /*
7626185db85Sdougm  * sa_is_share(object)
7636185db85Sdougm  *
7646185db85Sdougm  * returns true of the object is of type "share".
7656185db85Sdougm  */
7666185db85Sdougm 
7676185db85Sdougm int
7686185db85Sdougm sa_is_share(void *object)
7696185db85Sdougm {
7706185db85Sdougm 	if (object != NULL) {
77125a68471Sdougm 		if (strcmp((char *)((xmlNodePtr)object)->name, "share") == 0)
7726185db85Sdougm 		return (1);
7736185db85Sdougm 	}
7746185db85Sdougm 	return (0);
7756185db85Sdougm }
776da6c28aaSamw /*
777da6c28aaSamw  * sa_is_resource(object)
778da6c28aaSamw  *
779da6c28aaSamw  * returns true of the object is of type "share".
780da6c28aaSamw  */
781da6c28aaSamw 
782da6c28aaSamw int
783da6c28aaSamw sa_is_resource(void *object)
784da6c28aaSamw {
785da6c28aaSamw 	if (object != NULL) {
786da6c28aaSamw 		if (strcmp((char *)((xmlNodePtr)object)->name, "resource") == 0)
787da6c28aaSamw 			return (1);
788da6c28aaSamw 	}
789da6c28aaSamw 	return (0);
790da6c28aaSamw }
7916185db85Sdougm 
7926185db85Sdougm /*
7936185db85Sdougm  * _sa_remove_property(property)
7946185db85Sdougm  *
7956185db85Sdougm  * remove a property only from the document.
7966185db85Sdougm  */
7976185db85Sdougm 
7986185db85Sdougm static void
7996185db85Sdougm _sa_remove_property(sa_property_t property)
8006185db85Sdougm {
8016185db85Sdougm 	xmlUnlinkNode((xmlNodePtr)property);
8026185db85Sdougm 	xmlFreeNode((xmlNodePtr)property);
8036185db85Sdougm }
8046185db85Sdougm 
80567331909Sdougm /*
80667331909Sdougm  * _sa_create_dummy_share()
80767331909Sdougm  *
80867331909Sdougm  * Create a share entry suitable for parsing but not tied to any real
80967331909Sdougm  * config tree.  Need to have a parent as well as the node to parse
81067331909Sdougm  * on.  Free using _sa_free_dummy_share(share);
81167331909Sdougm  */
81267331909Sdougm 
81367331909Sdougm static sa_group_t
81467331909Sdougm _sa_create_dummy_share()
81567331909Sdougm {
81667331909Sdougm 	xmlNodePtr parent_node = NULL;
81767331909Sdougm 	xmlNodePtr child_node = NULL;
81867331909Sdougm 
81967331909Sdougm 	parent_node = xmlNewNode(NULL, (xmlChar *)"group");
82067331909Sdougm 	if (parent_node != NULL) {
82125a68471Sdougm 		child_node = xmlNewChild(parent_node, NULL, (xmlChar *)"share",
82225a68471Sdougm 		    NULL);
82325a68471Sdougm 		if (child_node != NULL) {
82425a68471Sdougm 			/*
82525a68471Sdougm 			 * Use a "zfs" tag since that will make sure nothing
82625a68471Sdougm 			 * really attempts to put values into the
82725a68471Sdougm 			 * repository. Also ZFS is currently the only user of
82825a68471Sdougm 			 * this interface.
82925a68471Sdougm 			 */
83025a68471Sdougm 			set_node_attr(parent_node, "type", "transient");
83125a68471Sdougm 			set_node_attr(parent_node, "zfs", "true");
83225a68471Sdougm 			set_node_attr(child_node, "type", "transient");
83325a68471Sdougm 			set_node_attr(child_node, "zfs", "true");
83425a68471Sdougm 		} else {
83525a68471Sdougm 			xmlFreeNode(parent_node);
83625a68471Sdougm 		}
83767331909Sdougm 	}
83867331909Sdougm 	return (child_node);
83967331909Sdougm }
84067331909Sdougm 
84167331909Sdougm /*
84267331909Sdougm  * _sa_free_dummy_share(share)
84367331909Sdougm  *
84467331909Sdougm  * Free the dummy share and its parent.  It is an error to try and
84567331909Sdougm  * free something that isn't a dummy.
84667331909Sdougm  */
84767331909Sdougm 
84867331909Sdougm static int
84967331909Sdougm _sa_free_dummy_share(sa_share_t share)
85067331909Sdougm {
85167331909Sdougm 	xmlNodePtr node = (xmlNodePtr)share;
85267331909Sdougm 	xmlNodePtr parent;
85367331909Sdougm 	int ret = SA_OK;
85467331909Sdougm 	char *name;
85567331909Sdougm 
85667331909Sdougm 	if (node != NULL) {
85725a68471Sdougm 		parent = node->parent;
85825a68471Sdougm 		name = (char *)xmlGetProp(node, (xmlChar *)"path");
85925a68471Sdougm 		if (name != NULL) {
86025a68471Sdougm 			/* Real shares always have a path but a dummy doesn't */
86125a68471Sdougm 			ret = SA_NOT_ALLOWED;
86225a68471Sdougm 			sa_free_attr_string(name);
86325a68471Sdougm 		} else {
86425a68471Sdougm 			/*
86525a68471Sdougm 			 * If there is a parent, do the free on that since
86625a68471Sdougm 			 * xmlFreeNode is a recursive function and free's an
86725a68471Sdougm 			 * child nodes.
86825a68471Sdougm 			 */
86925a68471Sdougm 			if (parent != NULL) {
87025a68471Sdougm 				node = parent;
87125a68471Sdougm 			}
87225a68471Sdougm 			xmlUnlinkNode(node);
87325a68471Sdougm 			xmlFreeNode(node);
87467331909Sdougm 		}
87567331909Sdougm 	}
87667331909Sdougm 	return (ret);
87767331909Sdougm }
87867331909Sdougm 
87967331909Sdougm 
8806185db85Sdougm /*
8816185db85Sdougm  * sa_parse_legacy_options(group, options, proto)
8826185db85Sdougm  *
8836185db85Sdougm  * In order to support legacy configurations, we allow the protocol
8846185db85Sdougm  * specific plugin to parse legacy syntax options (like those in
8856185db85Sdougm  * /etc/dfs/dfstab). This adds a new optionset to the group (or
8866185db85Sdougm  * share).
8876185db85Sdougm  *
8886185db85Sdougm  * Once the optionset has been created, we then get the derived
8896185db85Sdougm  * optionset of the parent (options from the optionset of the parent
8906185db85Sdougm  * and any parent it might have) and remove those from the created
8916185db85Sdougm  * optionset. This avoids duplication of options.
8926185db85Sdougm  */
8936185db85Sdougm 
8946185db85Sdougm int
8956185db85Sdougm sa_parse_legacy_options(sa_group_t group, char *options, char *proto)
8966185db85Sdougm {
8976185db85Sdougm 	int ret = SA_INVALID_PROTOCOL;
8986185db85Sdougm 	sa_group_t parent;
89967331909Sdougm 	int using_dummy = B_FALSE;
900717a41ebSdougm 	char *pvalue;
90125a68471Sdougm 	sa_optionset_t optionset;
90225a68471Sdougm 	sa_property_t popt, prop;
90325a68471Sdougm 	sa_optionset_t localoptions;
90467331909Sdougm 
90567331909Sdougm 	/*
90625a68471Sdougm 	 * If "group" is NULL, this is just a parse without saving
90767331909Sdougm 	 * anything in either SMF or ZFS.  Create a dummy group to
90867331909Sdougm 	 * handle this case.
90967331909Sdougm 	 */
91067331909Sdougm 	if (group == NULL) {
91125a68471Sdougm 		group = (sa_group_t)_sa_create_dummy_share();
91225a68471Sdougm 		using_dummy = B_TRUE;
91367331909Sdougm 	}
91467331909Sdougm 
9156185db85Sdougm 	parent = sa_get_parent_group(group);
9166185db85Sdougm 
9176185db85Sdougm 	if (proto != NULL)
91825a68471Sdougm 		ret = sa_proto_legacy_opts(proto, group, options);
91967331909Sdougm 
92067331909Sdougm 	if (using_dummy) {
92125a68471Sdougm 		/* Since this is a dummy parse, cleanup and quit here */
92225a68471Sdougm 		(void) _sa_free_dummy_share(parent);
92325a68471Sdougm 		return (ret);
92467331909Sdougm 	}
92525a68471Sdougm 
92625a68471Sdougm 	if (ret != SA_OK)
92725a68471Sdougm 		return (ret);
92825a68471Sdougm 
9296185db85Sdougm 	/*
93025a68471Sdougm 	 * If in a group, remove the inherited options and security
9316185db85Sdougm 	 */
93225a68471Sdougm 
93325a68471Sdougm 	if (parent == NULL)
93425a68471Sdougm 		return (ret);
93525a68471Sdougm 
93625a68471Sdougm 	/* Find parent options to remove from child */
93725a68471Sdougm 	optionset = sa_get_derived_optionset(parent, proto, 1);
93825a68471Sdougm 	localoptions = sa_get_optionset(group, proto);
93925a68471Sdougm 	if (optionset != NULL) {
94025a68471Sdougm 		for (popt = sa_get_property(optionset, NULL);
94125a68471Sdougm 		    popt != NULL;
94225a68471Sdougm 		    popt = sa_get_next_property(popt)) {
9436185db85Sdougm 			char *tag;
944717a41ebSdougm 			char *value;
9456185db85Sdougm 			tag = sa_get_property_attr(popt, "type");
94625a68471Sdougm 			if (tag == NULL)
94725a68471Sdougm 				continue;
94825a68471Sdougm 			prop = sa_get_property(localoptions, tag);
94925a68471Sdougm 			if (prop != NULL) {
95025a68471Sdougm 				value = sa_get_property_attr(popt,
95125a68471Sdougm 				    "value");
95225a68471Sdougm 				pvalue = sa_get_property_attr(prop,
95325a68471Sdougm 				    "value");
954717a41ebSdougm 				if (value != NULL && pvalue != NULL &&
955717a41ebSdougm 				    strcmp(value, pvalue) == 0) {
956717a41ebSdougm 					/*
95725a68471Sdougm 					 * Remove the property
95825a68471Sdougm 					 * from the
95925a68471Sdougm 					 * child. While we
96025a68471Sdougm 					 * removed it, we
96125a68471Sdougm 					 * don't need to reset
96225a68471Sdougm 					 * as we do below
96325a68471Sdougm 					 * since we always
96425a68471Sdougm 					 * search from the
96525a68471Sdougm 					 * beginning.
966717a41ebSdougm 					 */
96725a68471Sdougm 					(void) _sa_remove_property(
96825a68471Sdougm 					    prop);
9696185db85Sdougm 				}
970717a41ebSdougm 				if (value != NULL)
97125a68471Sdougm 					sa_free_attr_string(value);
972717a41ebSdougm 				if (pvalue != NULL)
97325a68471Sdougm 					sa_free_attr_string(pvalue);
9746185db85Sdougm 			}
97525a68471Sdougm 			sa_free_attr_string(tag);
97625a68471Sdougm 		}
97725a68471Sdougm 		prop = sa_get_property(localoptions, NULL);
97825a68471Sdougm 		if (prop == NULL && sa_is_share(group)) {
9796185db85Sdougm 			/*
98025a68471Sdougm 			 * All properties removed so remove the
9816185db85Sdougm 			 * optionset if it is on a share
9826185db85Sdougm 			 */
9836185db85Sdougm 			(void) _sa_remove_optionset(localoptions);
9846185db85Sdougm 		}
98525a68471Sdougm 		sa_free_derived_optionset(optionset);
98625a68471Sdougm 	}
98725a68471Sdougm 	/*
98825a68471Sdougm 	 * Need to remove security here. If there are no
98925a68471Sdougm 	 * security options on the local group/share, don't
99025a68471Sdougm 	 * bother since those are the only ones that would be
99125a68471Sdougm 	 * affected.
99225a68471Sdougm 	 */
99325a68471Sdougm 	localoptions = sa_get_all_security_types(group, proto, 0);
99425a68471Sdougm 	if (localoptions != NULL) {
99525a68471Sdougm 		for (prop = sa_get_property(localoptions, NULL);
99625a68471Sdougm 		    prop != NULL;
99725a68471Sdougm 		    prop = sa_get_next_property(prop)) {
9986185db85Sdougm 			char *tag;
9996185db85Sdougm 			sa_security_t security;
10006185db85Sdougm 			tag = sa_get_property_attr(prop, "type");
10016185db85Sdougm 			if (tag != NULL) {
100225a68471Sdougm 				sa_property_t nextpopt = NULL;
100325a68471Sdougm 				security = sa_get_security(group, tag, proto);
100425a68471Sdougm 				sa_free_attr_string(tag);
1005717a41ebSdougm 				/*
100625a68471Sdougm 				 * prop's value only changes outside this loop
1007717a41ebSdougm 				 */
100825a68471Sdougm 				pvalue = sa_get_property_attr(prop, "value");
100925a68471Sdougm 				for (popt = sa_get_property(security, NULL);
101025a68471Sdougm 				    popt != NULL;
101125a68471Sdougm 				    popt = nextpopt) {
101225a68471Sdougm 					char *value;
101325a68471Sdougm 					/*
101425a68471Sdougm 					 * Need to get the next prop
101525a68471Sdougm 					 * now since we could break
101625a68471Sdougm 					 * the list during removal.
101725a68471Sdougm 					 */
101825a68471Sdougm 					nextpopt = sa_get_next_property(popt);
101925a68471Sdougm 					/* remove Duplicates from this level */
102025a68471Sdougm 					value = sa_get_property_attr(popt,
102125a68471Sdougm 					    "value");
102225a68471Sdougm 					if (value != NULL && pvalue != NULL &&
102325a68471Sdougm 					    strcmp(value, pvalue) == 0) {
102425a68471Sdougm 						/*
102525a68471Sdougm 						 * remove the property
102625a68471Sdougm 						 * from the child
102725a68471Sdougm 						 */
102825a68471Sdougm 						(void) _sa_remove_property
102925a68471Sdougm 						    (popt);
103025a68471Sdougm 					}
103125a68471Sdougm 					if (value != NULL)
103225a68471Sdougm 						sa_free_attr_string(value);
10336185db85Sdougm 				}
103425a68471Sdougm 				if (pvalue != NULL)
103525a68471Sdougm 					sa_free_attr_string(pvalue);
10366185db85Sdougm 			}
10376185db85Sdougm 		}
103825a68471Sdougm 		(void) sa_destroy_optionset(localoptions);
10396185db85Sdougm 	}
10406185db85Sdougm 	return (ret);
10416185db85Sdougm }
10426185db85Sdougm 
10436185db85Sdougm /*
10446185db85Sdougm  * dfs_free_list(list)
10456185db85Sdougm  *
10466185db85Sdougm  * Free the data in each list entry of the list as well as freeing the
10476185db85Sdougm  * entries themselves. We need to avoid memory leaks and don't want to
10486185db85Sdougm  * dereference any NULL members.
10496185db85Sdougm  */
10506185db85Sdougm 
10516185db85Sdougm static void
10526185db85Sdougm dfs_free_list(xfs_sharelist_t *list)
10536185db85Sdougm {
10546185db85Sdougm 	xfs_sharelist_t *entry;
10556185db85Sdougm 	for (entry = list; entry != NULL; entry = list) {
105625a68471Sdougm 		if (entry->path != NULL)
105725a68471Sdougm 			free(entry->path);
105825a68471Sdougm 		if (entry->resource != NULL)
105925a68471Sdougm 			free(entry->resource);
106025a68471Sdougm 		if (entry->fstype != NULL)
106125a68471Sdougm 			free(entry->fstype);
106225a68471Sdougm 		if (entry->options != NULL)
106325a68471Sdougm 			free(entry->options);
106425a68471Sdougm 		if (entry->description != NULL)
106525a68471Sdougm 			free(entry->description);
106625a68471Sdougm 		if (entry->origline != NULL)
106725a68471Sdougm 			free(entry->origline);
106825a68471Sdougm 		if (entry->group != NULL)
106925a68471Sdougm 			free(entry->group);
107025a68471Sdougm 		list = list->next;
107125a68471Sdougm 			free(entry);
10726185db85Sdougm 	}
10736185db85Sdougm }
10746185db85Sdougm 
10756185db85Sdougm /*
10766185db85Sdougm  * parse_dfstab(dfstab, root)
10776185db85Sdougm  *
10786185db85Sdougm  * Open and read the existing dfstab, parsing each line and adding it
10796185db85Sdougm  * to the internal configuration. Make sure syntax errors, etc are
10806185db85Sdougm  * preserved as comments.
10816185db85Sdougm  */
10826185db85Sdougm 
10836185db85Sdougm static void
1084549ec3ffSdougm parse_dfstab(sa_handle_t handle, char *dfstab, xmlNodePtr root)
10856185db85Sdougm {
10866185db85Sdougm 	sa_share_t share;
10876185db85Sdougm 	sa_group_t group;
10886185db85Sdougm 	sa_group_t sgroup = NULL;
10896185db85Sdougm 	sa_group_t defgroup;
10906185db85Sdougm 	xfs_sharelist_t *head, *list;
10916185db85Sdougm 	int err;
10926185db85Sdougm 	int defined_group;
10936185db85Sdougm 	FILE *dfs;
10946185db85Sdougm 	char *oldprops;
10956185db85Sdougm 
10966185db85Sdougm 	/* read the dfstab format file and fill in the doc tree */
10976185db85Sdougm 
10986185db85Sdougm 	dfs = fopen(dfstab, "r");
109925a68471Sdougm 	if (dfs == NULL)
110025a68471Sdougm 		return;
11016185db85Sdougm 
1102549ec3ffSdougm 	defgroup = sa_get_group(handle, "default");
11036185db85Sdougm 
11046185db85Sdougm 	for (head = list = getdfstab(dfs);
110525a68471Sdougm 	    list != NULL;
110625a68471Sdougm 	    list = list->next) {
110725a68471Sdougm 		share = NULL;
110825a68471Sdougm 		group = NULL;
110925a68471Sdougm 		defined_group = 0;
111025a68471Sdougm 		err = 0;
111125a68471Sdougm 
111225a68471Sdougm 		if (list->origline == NULL) {
111325a68471Sdougm 			/*
111425a68471Sdougm 			 * Comment line that we will likely skip.
111525a68471Sdougm 			 * If the line has the syntax:
111625a68471Sdougm 			 *	# error: string: string
111725a68471Sdougm 			 * It should be preserved until manually deleted.
111825a68471Sdougm 			 */
111925a68471Sdougm 			if (list->description != NULL &&
112025a68471Sdougm 			    strncmp(list->description, "# Error: ", 9) == 0) {
112125a68471Sdougm 				char *line;
112225a68471Sdougm 				char *error;
112325a68471Sdougm 				char *cmd;
112425a68471Sdougm 				line = strdup(list->description);
112525a68471Sdougm 				if (line != NULL) {
112625a68471Sdougm 					error = line + 9;
112725a68471Sdougm 					cmd = strchr(error, ':');
112825a68471Sdougm 					if (cmd != NULL) {
112925a68471Sdougm 						int len;
113025a68471Sdougm 						*cmd = '\0';
113125a68471Sdougm 						cmd += 2;
113225a68471Sdougm 						len = strlen(cmd);
113325a68471Sdougm 						cmd[len - 1] = '\0';
113425a68471Sdougm 						add_syntax_comment(root, cmd,
113525a68471Sdougm 						    error, 0);
113625a68471Sdougm 					}
113725a68471Sdougm 					free(line);
113825a68471Sdougm 				}
11396185db85Sdougm 			}
114025a68471Sdougm 			continue;
11416185db85Sdougm 		}
114225a68471Sdougm 		if (list->path != NULL && strlen(list->path) > 0 &&
1143549ec3ffSdougm 		    *list->path == '/') {
114425a68471Sdougm 			share = sa_find_share(handle, list->path);
114525a68471Sdougm 			if (share != NULL)
114625a68471Sdougm 				sgroup = sa_get_parent_group(share);
114725a68471Sdougm 			else
114825a68471Sdougm 				sgroup = NULL;
114925a68471Sdougm 		} else {
115025a68471Sdougm 			(void) printf(dgettext(TEXT_DOMAIN,
115125a68471Sdougm 			    "No share specified in dfstab: "
115225a68471Sdougm 			    "line %d: %s\n"),
115325a68471Sdougm 			    list->lineno, list->origline);
115425a68471Sdougm 			add_syntax_comment(root, list->origline,
115525a68471Sdougm 			    dgettext(TEXT_DOMAIN, "No share specified"), 1);
115625a68471Sdougm 			continue;
115725a68471Sdougm 		}
115825a68471Sdougm 		if (list->group != NULL && strlen(list->group) > 0) {
115925a68471Sdougm 			group = sa_get_group(handle, list->group);
116025a68471Sdougm 			defined_group = 1;
116125a68471Sdougm 		} else {
116225a68471Sdougm 			group = defgroup;
116325a68471Sdougm 		}
116425a68471Sdougm 		if (defined_group && group == NULL) {
116525a68471Sdougm 			(void) printf(dgettext(TEXT_DOMAIN,
116625a68471Sdougm 			    "Unknown group used in dfstab: line %d: %s\n"),
116725a68471Sdougm 			    list->lineno, list->origline);
116825a68471Sdougm 			add_syntax_comment(root, list->origline,
116925a68471Sdougm 			    dgettext(TEXT_DOMAIN, "Unknown group specified"),
117025a68471Sdougm 			    1);
117125a68471Sdougm 			continue;
117225a68471Sdougm 		}
117325a68471Sdougm 		if (group == NULL) {
117425a68471Sdougm 			/* Shouldn't happen unless an SMF error */
117525a68471Sdougm 			err = SA_CONFIG_ERR;
117625a68471Sdougm 			continue;
117725a68471Sdougm 		}
11786185db85Sdougm 		if (share == NULL) {
117925a68471Sdougm 			if (defined_group || group != defgroup)
118025a68471Sdougm 				continue;
118125a68471Sdougm 			/* This is an OK add for legacy */
11826185db85Sdougm 			share = sa_add_share(defgroup, list->path,
118325a68471Sdougm 			    SA_SHARE_PERMANENT | SA_SHARE_PARSER, &err);
11846185db85Sdougm 			if (share != NULL) {
118525a68471Sdougm 				if (list->description != NULL &&
118625a68471Sdougm 				    strlen(list->description) > 0)
118725a68471Sdougm 					(void) sa_set_share_description(share,
118825a68471Sdougm 					    list->description);
118925a68471Sdougm 				if (list->options != NULL &&
119025a68471Sdougm 				    strlen(list->options) > 0) {
119125a68471Sdougm 					(void) sa_parse_legacy_options(share,
119225a68471Sdougm 					    list->options, list->fstype);
119325a68471Sdougm 				}
119425a68471Sdougm 				if (list->resource != NULL)
119525a68471Sdougm 					(void) sa_set_share_attr(share,
119625a68471Sdougm 					    "resource", list->resource);
11976185db85Sdougm 			} else {
119825a68471Sdougm 				(void) printf(dgettext(TEXT_DOMAIN,
119925a68471Sdougm 				    "Error in dfstab: line %d: %s\n"),
12006185db85Sdougm 				    list->lineno, list->origline);
120125a68471Sdougm 				if (err != SA_BAD_PATH)
120225a68471Sdougm 					add_syntax_comment(root, list->origline,
120325a68471Sdougm 					    dgettext(TEXT_DOMAIN, "Syntax"), 1);
120425a68471Sdougm 				else
120525a68471Sdougm 					add_syntax_comment(root, list->origline,
120625a68471Sdougm 					    dgettext(TEXT_DOMAIN,
120725a68471Sdougm 					    "Path"), 1);
120825a68471Sdougm 				continue;
12096185db85Sdougm 			}
12106185db85Sdougm 		} else {
121125a68471Sdougm 			if (group != sgroup) {
121225a68471Sdougm 				(void) printf(dgettext(TEXT_DOMAIN,
121325a68471Sdougm 				    "Attempt to change configuration in "
121425a68471Sdougm 				    "dfstab: line %d: %s\n"),
121525a68471Sdougm 				    list->lineno, list->origline);
121625a68471Sdougm 				add_syntax_comment(root, list->origline,
121725a68471Sdougm 				    dgettext(TEXT_DOMAIN,
121825a68471Sdougm 				    "Attempt to change configuration"), 1);
121925a68471Sdougm 				continue;
122025a68471Sdougm 			}
1221546405c3Sdougm 			/*
1222546405c3Sdougm 			 * It is the same group but could have changed
1223546405c3Sdougm 			 * options. Make sure we include the group's
1224546405c3Sdougm 			 * properties so we don't end up moving them to
1225546405c3Sdougm 			 * the share inadvertantly. The last arg being
1226546405c3Sdougm 			 * true says to get the inherited properties as well
1227546405c3Sdougm 			 * as the local properties.
1228546405c3Sdougm 			 */
122925a68471Sdougm 			oldprops = sa_proto_legacy_format(list->fstype, share,
123025a68471Sdougm 			    B_TRUE);
123125a68471Sdougm 
123225a68471Sdougm 			if (oldprops == NULL)
123325a68471Sdougm 				continue;
123425a68471Sdougm 
12356185db85Sdougm 			if (list->options != NULL &&
123625a68471Sdougm 			    strcmp(oldprops, list->options) != 0) {
123725a68471Sdougm 				sa_optionset_t opts;
123825a68471Sdougm 				sa_security_t secs;
123925a68471Sdougm 
124025a68471Sdougm 				/* possibly different values */
124125a68471Sdougm 				opts = sa_get_optionset((sa_group_t)
124225a68471Sdougm 				    share, list->fstype);
124325a68471Sdougm 				(void) sa_destroy_optionset(opts);
124425a68471Sdougm 
124525a68471Sdougm 				for (secs = sa_get_security(
124625a68471Sdougm 				    (sa_group_t)share, NULL, list->fstype);
124725a68471Sdougm 				    secs != NULL;
124825a68471Sdougm 				    secs = sa_get_security((sa_group_t)share,
124925a68471Sdougm 				    NULL, list->fstype)) {
125025a68471Sdougm 					(void) sa_destroy_security(
125125a68471Sdougm 					    secs);
125225a68471Sdougm 				}
125325a68471Sdougm 				(void) sa_parse_legacy_options(share,
125425a68471Sdougm 				    list->options, list->fstype);
12556185db85Sdougm 			}
1256a99982a7Sdougm 			sa_format_free(oldprops);
12576185db85Sdougm 		}
12586185db85Sdougm 	}
12596185db85Sdougm 	dfs_free_list(head);
12606185db85Sdougm }
12616185db85Sdougm 
12626185db85Sdougm /*
12636185db85Sdougm  * legacy_removes(group, file)
12646185db85Sdougm  *
12656185db85Sdougm  * Find any shares that are "missing" from the legacy file. These
12666185db85Sdougm  * should be removed from the configuration since they are likely from
12676185db85Sdougm  * a legacy app or the admin modified the dfstab file directly. We
12686185db85Sdougm  * have to support this even if it is not the recommended way to do
12696185db85Sdougm  * things.
12706185db85Sdougm  */
12716185db85Sdougm 
12726185db85Sdougm static void
12736185db85Sdougm legacy_removes(sa_group_t group, char *file)
12746185db85Sdougm {
12756185db85Sdougm 	sa_share_t share;
12766185db85Sdougm 	char *path;
12776185db85Sdougm 	xfs_sharelist_t *list, *item;
12786185db85Sdougm 	FILE *dfstab;
12796185db85Sdougm 
12806185db85Sdougm 	dfstab = fopen(file, "r");
12816185db85Sdougm 	if (dfstab != NULL) {
128225a68471Sdougm 		list = getdfstab(dfstab);
128325a68471Sdougm 		(void) fclose(dfstab);
1284f345c0beSdougm retry:
128525a68471Sdougm 		for (share = sa_get_share(group, NULL);
128625a68471Sdougm 		    share != NULL;
128725a68471Sdougm 		    share = sa_get_next_share(share)) {
128825a68471Sdougm 			/* now see if the share is in the dfstab file */
128925a68471Sdougm 			path = sa_get_share_attr(share, "path");
129025a68471Sdougm 			if (path != NULL) {
129125a68471Sdougm 				item = finddfsentry(list, path);
129225a68471Sdougm 				sa_free_attr_string(path);
129325a68471Sdougm 				if (item == NULL) {
129425a68471Sdougm 					/* The share was removed this way */
129525a68471Sdougm 					(void) sa_remove_share(share);
129625a68471Sdougm 
129725a68471Sdougm 					/*
129825a68471Sdougm 					 * Start over since the list was broken
129925a68471Sdougm 					 */
130025a68471Sdougm 					goto retry;
130125a68471Sdougm 				}
130225a68471Sdougm 			}
13036185db85Sdougm 		}
130425a68471Sdougm 		if (list != NULL)
130525a68471Sdougm 			dfs_free_list(list);
13066185db85Sdougm 	}
13076185db85Sdougm }
13086185db85Sdougm 
13096185db85Sdougm /*
13106185db85Sdougm  * getlegacyconfig(path, root)
13116185db85Sdougm  *
13126185db85Sdougm  * Parse dfstab and build the legacy configuration. This only gets
13136185db85Sdougm  * called when a change was detected.
13146185db85Sdougm  */
13156185db85Sdougm 
13166185db85Sdougm void
1317549ec3ffSdougm getlegacyconfig(sa_handle_t handle, char *path, xmlNodePtr *root)
13186185db85Sdougm {
13196185db85Sdougm 	sa_group_t defgroup;
13206185db85Sdougm 
13216185db85Sdougm 	if (root != NULL) {
132225a68471Sdougm 		if (*root == NULL)
132325a68471Sdougm 			*root = xmlNewNode(NULL, (xmlChar *)"sharecfg");
132425a68471Sdougm 		if (*root != NULL) {
132525a68471Sdougm 			if (strcmp(path, SA_LEGACY_DFSTAB) == 0) {
132625a68471Sdougm 				/*
132725a68471Sdougm 				 * Walk the default shares and find anything
132825a68471Sdougm 				 * missing.  we do this first to make sure it
132925a68471Sdougm 				 * is cleaned up since there may be legacy
133025a68471Sdougm 				 * code add/del via dfstab and we need to
133125a68471Sdougm 				 * cleanup SMF.
133225a68471Sdougm 				 */
133325a68471Sdougm 				defgroup = sa_get_group(handle, "default");
133425a68471Sdougm 				if (defgroup != NULL)
133525a68471Sdougm 					legacy_removes(defgroup, path);
133625a68471Sdougm 				/* Parse the dfstab and add anything new */
133725a68471Sdougm 				parse_dfstab(handle, path, *root);
133825a68471Sdougm 			}
13396185db85Sdougm 		}
13406185db85Sdougm 	}
13416185db85Sdougm }
13426185db85Sdougm 
13431cea05afSdougm /*
13441cea05afSdougm  * get_share_list(&err)
13451cea05afSdougm  *
13461cea05afSdougm  * Get a linked list of all the shares on the system from
13471cea05afSdougm  * /etc/dfs/sharetab. This is partially copied from libfsmgt which we
13481cea05afSdougm  * can't use due to package dependencies.
13491cea05afSdougm  */
13501cea05afSdougm static xfs_sharelist_t *
13511cea05afSdougm get_share_list(int *errp)
13521cea05afSdougm {
13531cea05afSdougm 	xfs_sharelist_t	*newp;
13541cea05afSdougm 	xfs_sharelist_t	*headp;
13551cea05afSdougm 	xfs_sharelist_t	*tailp;
13561cea05afSdougm 	FILE		*fp;
13571cea05afSdougm 
13581cea05afSdougm 	headp = NULL;
13591cea05afSdougm 	tailp = NULL;
13601cea05afSdougm 
13611cea05afSdougm 	if ((fp = fopen(SHARETAB, "r")) != NULL) {
13621cea05afSdougm 		struct share	*sharetab_entry;
13631cea05afSdougm 
13641cea05afSdougm 		while (getshare(fp, &sharetab_entry) > 0) {
136525a68471Sdougm 			newp = alloc_sharelist();
136625a68471Sdougm 			if (newp == NULL)
136725a68471Sdougm 				goto err;
13681cea05afSdougm 
13691cea05afSdougm 			/*
137025a68471Sdougm 			 * Link into the list here so we don't leak
13711cea05afSdougm 			 * memory on a failure from strdup().
13721cea05afSdougm 			 */
137325a68471Sdougm 			if (headp == NULL) {
137425a68471Sdougm 				headp = newp;
137525a68471Sdougm 				tailp = newp;
137625a68471Sdougm 			} else {
137725a68471Sdougm 				tailp->next = newp;
137825a68471Sdougm 				tailp = newp;
137925a68471Sdougm 			}
138025a68471Sdougm 
138125a68471Sdougm 			newp->path = strdup(sharetab_entry->sh_path);
138225a68471Sdougm 			if (newp->path == NULL)
138325a68471Sdougm 				goto err;
138425a68471Sdougm 			newp->resource = strdup(sharetab_entry->sh_res);
138525a68471Sdougm 			if (newp->resource == NULL)
138625a68471Sdougm 				goto err;
138725a68471Sdougm 			newp->fstype = strdup(sharetab_entry->sh_fstype);
138825a68471Sdougm 			if (newp->fstype == NULL)
138925a68471Sdougm 				goto err;
139025a68471Sdougm 			newp->options = strdup(sharetab_entry->sh_opts);
139125a68471Sdougm 			if (newp->options == NULL)
139225a68471Sdougm 				goto err;
139325a68471Sdougm 			newp->description = strdup(sharetab_entry->sh_descr);
139425a68471Sdougm 			if (newp->description == NULL)
139525a68471Sdougm 				goto err;
13961cea05afSdougm 		}
1397a99982a7Sdougm 		(void) lockf(fileno(fp), F_ULOCK, 0);
13981cea05afSdougm 		(void) fclose(fp);
13991cea05afSdougm 	} else {
140025a68471Sdougm 		*errp = errno;
14011cea05afSdougm 	}
14021cea05afSdougm 
14031cea05afSdougm 	/*
14041cea05afSdougm 	 * Caller must free the mount list
14051cea05afSdougm 	 */
14061cea05afSdougm 	return (headp);
14071cea05afSdougm err:
14081cea05afSdougm 	/*
14091cea05afSdougm 	 * Out of memory so cleanup and leave.
14101cea05afSdougm 	 */
14111cea05afSdougm 	dfs_free_list(headp);
14121cea05afSdougm 	(void) fclose(fp);
14131cea05afSdougm 	return (NULL);
14141cea05afSdougm }
14151cea05afSdougm 
14166185db85Sdougm /*
1417549ec3ffSdougm  * parse_sharetab(handle)
14186185db85Sdougm  *
14191cea05afSdougm  * Read the /etc/dfs/sharetab file and see which entries don't exist
14201cea05afSdougm  * in the repository. These shares are marked transient.  We also need
14211cea05afSdougm  * to see if they are ZFS shares since ZFS bypasses the SMF
14221cea05afSdougm  * repository.
14236185db85Sdougm  */
14246185db85Sdougm 
14256185db85Sdougm int
1426549ec3ffSdougm parse_sharetab(sa_handle_t handle)
14276185db85Sdougm {
14281cea05afSdougm 	xfs_sharelist_t *list, *tmplist;
14296185db85Sdougm 	int err = 0;
14306185db85Sdougm 	sa_share_t share;
14316185db85Sdougm 	sa_group_t group;
14326185db85Sdougm 	sa_group_t lgroup;
14336185db85Sdougm 	char *groupname;
14346185db85Sdougm 	int legacy = 0;
1435da6c28aaSamw 	char shareopts[MAXNAMLEN];
14366185db85Sdougm 
14371cea05afSdougm 	list = get_share_list(&err);
14386185db85Sdougm 	if (list == NULL)
143925a68471Sdougm 		return (legacy);
14406185db85Sdougm 
1441549ec3ffSdougm 	lgroup = sa_get_group(handle, "default");
14426185db85Sdougm 
14436185db85Sdougm 	for (tmplist = list; tmplist != NULL; tmplist = tmplist->next) {
144425a68471Sdougm 		group = NULL;
144525a68471Sdougm 		share = sa_find_share(handle, tmplist->path);
144625a68471Sdougm 		if (share != NULL) {
144725a68471Sdougm 			/*
144825a68471Sdougm 			 * If this is a legacy share, mark as shared so we
144925a68471Sdougm 			 * only update sharetab appropriately. We also keep
145025a68471Sdougm 			 * the sharetab options in order to display for legacy
145125a68471Sdougm 			 * share with no arguments.
145225a68471Sdougm 			 */
145325a68471Sdougm 			set_node_attr(share, "shared", "true");
1454da6c28aaSamw 			(void) snprintf(shareopts, MAXNAMLEN, "shareopts-%s",
1455da6c28aaSamw 			    tmplist->fstype);
1456da6c28aaSamw 			set_node_attr(share, shareopts, tmplist->options);
145725a68471Sdougm 			continue;
145825a68471Sdougm 		}
145925a68471Sdougm 
14606185db85Sdougm 		/*
146125a68471Sdougm 		 * This share is transient so needs to be
14626185db85Sdougm 		 * added. Initially, this will be under
14636185db85Sdougm 		 * default(legacy) unless it is a ZFS
14646185db85Sdougm 		 * share. If zfs, we need a zfs group.
14656185db85Sdougm 		 */
14666185db85Sdougm 		if (tmplist->resource != NULL &&
14676185db85Sdougm 		    (groupname = strchr(tmplist->resource, '@')) != NULL) {
146825a68471Sdougm 			/* There is a defined group */
146925a68471Sdougm 			*groupname++ = '\0';
147025a68471Sdougm 			group = sa_get_group(handle, groupname);
14716185db85Sdougm 			if (group != NULL) {
1472da6c28aaSamw 				share = _sa_add_share(group, tmplist->path,
1473da6c28aaSamw 				    SA_SHARE_TRANSIENT, &err,
1474da6c28aaSamw 				    (uint64_t)SA_FEATURE_NONE);
147525a68471Sdougm 			} else {
147625a68471Sdougm 				/*
147725a68471Sdougm 				 * While this case shouldn't
147825a68471Sdougm 				 * occur very often, it does
147925a68471Sdougm 				 * occur out of a "zfs set
148025a68471Sdougm 				 * sharenfs=off" when the
148125a68471Sdougm 				 * dataset is also set to
148225a68471Sdougm 				 * canmount=off. A warning
148325a68471Sdougm 				 * will then cause the zfs
148425a68471Sdougm 				 * command to abort. Since we
148525a68471Sdougm 				 * add it to the default list,
148625a68471Sdougm 				 * everything works properly
148725a68471Sdougm 				 * anyway and the library
148825a68471Sdougm 				 * doesn't need to give a
148925a68471Sdougm 				 * warning.
149025a68471Sdougm 				 */
149125a68471Sdougm 				share = _sa_add_share(lgroup,
149225a68471Sdougm 				    tmplist->path, SA_SHARE_TRANSIENT,
1493da6c28aaSamw 				    &err, (uint64_t)SA_FEATURE_NONE);
149425a68471Sdougm 			}
149525a68471Sdougm 		} else {
149625a68471Sdougm 			if (sa_zfs_is_shared(handle, tmplist->path)) {
149725a68471Sdougm 				group = sa_get_group(handle, "zfs");
149825a68471Sdougm 				if (group == NULL) {
149925a68471Sdougm 					group = sa_create_group(handle,
150025a68471Sdougm 					    "zfs", &err);
150125a68471Sdougm 					if (group == NULL &&
150225a68471Sdougm 					    err == SA_NO_PERMISSION) {
150325a68471Sdougm 						group = _sa_create_group(
150425a68471Sdougm 						    (sa_handle_impl_t)
150525a68471Sdougm 						    handle,
150625a68471Sdougm 						    "zfs");
150725a68471Sdougm 					}
150825a68471Sdougm 					if (group != NULL) {
150925a68471Sdougm 						(void) sa_create_optionset(
151025a68471Sdougm 						    group, tmplist->fstype);
151125a68471Sdougm 						(void) sa_set_group_attr(group,
151225a68471Sdougm 						    "zfs", "true");
151325a68471Sdougm 					}
151425a68471Sdougm 				}
151525a68471Sdougm 				if (group != NULL) {
151625a68471Sdougm 					share = _sa_add_share(group,
151725a68471Sdougm 					    tmplist->path, SA_SHARE_TRANSIENT,
1518da6c28aaSamw 					    &err, (uint64_t)SA_FEATURE_NONE);
151925a68471Sdougm 				}
152025a68471Sdougm 			} else {
152125a68471Sdougm 				share = _sa_add_share(lgroup, tmplist->path,
1522da6c28aaSamw 				    SA_SHARE_TRANSIENT, &err,
1523da6c28aaSamw 				    (uint64_t)SA_FEATURE_NONE);
15246185db85Sdougm 			}
15256185db85Sdougm 		}
15266185db85Sdougm 		if (share == NULL)
152725a68471Sdougm 			(void) printf(dgettext(TEXT_DOMAIN,
152825a68471Sdougm 			    "Problem with transient: %s\n"), sa_errorstr(err));
15296185db85Sdougm 		if (share != NULL)
153025a68471Sdougm 			set_node_attr(share, "shared", "true");
15316185db85Sdougm 		if (err == SA_OK) {
153225a68471Sdougm 			if (tmplist->options != NULL &&
153325a68471Sdougm 			    strlen(tmplist->options) > 0) {
153425a68471Sdougm 				(void) sa_parse_legacy_options(share,
153525a68471Sdougm 				    tmplist->options, tmplist->fstype);
153625a68471Sdougm 			}
153725a68471Sdougm 			if (tmplist->resource != NULL &&
153825a68471Sdougm 			    strcmp(tmplist->resource, "-") != 0)
153925a68471Sdougm 				set_node_attr(share, "resource",
154025a68471Sdougm 				    tmplist->resource);
154125a68471Sdougm 			if (tmplist->description != NULL) {
154225a68471Sdougm 				xmlNodePtr node;
154325a68471Sdougm 				node = xmlNewChild((xmlNodePtr)share, NULL,
154425a68471Sdougm 				    (xmlChar *)"description", NULL);
154525a68471Sdougm 				xmlNodeSetContent(node,
154625a68471Sdougm 				    (xmlChar *)tmplist->description);
154725a68471Sdougm 			}
154825a68471Sdougm 			legacy = 1;
15496185db85Sdougm 		}
15506185db85Sdougm 	}
15511cea05afSdougm 	dfs_free_list(list);
15526185db85Sdougm 	return (legacy);
15536185db85Sdougm }
15546185db85Sdougm 
15556185db85Sdougm /*
155625a68471Sdougm  * Get the transient shares from the sharetab (or other) file.  since
15576185db85Sdougm  * these are transient, they only appear in the working file and not
15586185db85Sdougm  * in a repository.
15596185db85Sdougm  */
15606185db85Sdougm int
1561549ec3ffSdougm gettransients(sa_handle_impl_t ihandle, xmlNodePtr *root)
15626185db85Sdougm {
15636185db85Sdougm 	int legacy = 0;
1564da6c28aaSamw 	int numproto;
1565da6c28aaSamw 	char **protocols = NULL;
1566da6c28aaSamw 	int i;
15676185db85Sdougm 
15686185db85Sdougm 	if (root != NULL) {
156925a68471Sdougm 		if (*root == NULL)
157025a68471Sdougm 			*root = xmlNewNode(NULL, (xmlChar *)"sharecfg");
1571da6c28aaSamw 		if (*root != NULL) {
157225a68471Sdougm 			legacy = parse_sharetab(ihandle);
1573da6c28aaSamw 			numproto = sa_get_protocols(&protocols);
1574da6c28aaSamw 			for (i = 0; i < numproto; i++)
1575da6c28aaSamw 				legacy |= sa_proto_get_transients(
1576da6c28aaSamw 				    (sa_handle_t)ihandle, protocols[i]);
1577da6c28aaSamw 			if (protocols != NULL)
1578da6c28aaSamw 				free(protocols);
1579da6c28aaSamw 		}
15806185db85Sdougm 	}
15816185db85Sdougm 	return (legacy);
15826185db85Sdougm }
15836185db85Sdougm 
15846185db85Sdougm /*
15856185db85Sdougm  * sa_has_prop(optionset, prop)
15866185db85Sdougm  *
15876185db85Sdougm  * Is the specified property a member of the optionset?
15886185db85Sdougm  */
15896185db85Sdougm 
15906185db85Sdougm int
15916185db85Sdougm sa_has_prop(sa_optionset_t optionset, sa_property_t prop)
15926185db85Sdougm {
15936185db85Sdougm 	char *name;
15946185db85Sdougm 	sa_property_t otherprop;
15956185db85Sdougm 	int result = 0;
15966185db85Sdougm 
15976185db85Sdougm 	if (optionset != NULL) {
159825a68471Sdougm 		name = sa_get_property_attr(prop, "type");
159925a68471Sdougm 		if (name != NULL) {
160025a68471Sdougm 			otherprop = sa_get_property(optionset, name);
160125a68471Sdougm 			if (otherprop != NULL)
160225a68471Sdougm 				result = 1;
160325a68471Sdougm 			sa_free_attr_string(name);
160425a68471Sdougm 		}
16056185db85Sdougm 	}
16066185db85Sdougm 	return (result);
16076185db85Sdougm }
16086185db85Sdougm 
16096185db85Sdougm /*
16106185db85Sdougm  * Update legacy files
16116185db85Sdougm  *
16126185db85Sdougm  * Provides functions to add/remove/modify individual entries
16136185db85Sdougm  * in dfstab and sharetab
16146185db85Sdougm  */
16156185db85Sdougm 
16166185db85Sdougm void
1617549ec3ffSdougm update_legacy_config(sa_handle_t handle)
16186185db85Sdougm {
16196185db85Sdougm 	/*
16206185db85Sdougm 	 * no longer used -- this is a placeholder in case we need to
16216185db85Sdougm 	 * add it back later.
16226185db85Sdougm 	 */
1623549ec3ffSdougm #ifdef lint
1624549ec3ffSdougm 	handle = handle;
1625549ec3ffSdougm #endif
16266185db85Sdougm }
16276185db85Sdougm 
16286185db85Sdougm /*
16296185db85Sdougm  * sa_valid_property(object, proto, property)
16306185db85Sdougm  *
16316185db85Sdougm  * check to see if the specified property is valid relative to the
16326185db85Sdougm  * specified protocol. The protocol plugin is called to do the work.
16336185db85Sdougm  */
16346185db85Sdougm 
16356185db85Sdougm int
16366185db85Sdougm sa_valid_property(void *object, char *proto, sa_property_t property)
16376185db85Sdougm {
16386185db85Sdougm 	int ret = SA_OK;
16396185db85Sdougm 
16406185db85Sdougm 	if (proto != NULL && property != NULL) {
164125a68471Sdougm 		ret = sa_proto_valid_prop(proto, property, object);
16426185db85Sdougm 	}
16436185db85Sdougm 
16446185db85Sdougm 	return (ret);
16456185db85Sdougm }
16466185db85Sdougm 
16476185db85Sdougm /*
16486185db85Sdougm  * sa_fstype(path)
16496185db85Sdougm  *
16506185db85Sdougm  * Given path, return the string representing the path's file system
16516185db85Sdougm  * type. This is used to discover ZFS shares.
16526185db85Sdougm  */
16536185db85Sdougm 
16546185db85Sdougm char *
16556185db85Sdougm sa_fstype(char *path)
16566185db85Sdougm {
16576185db85Sdougm 	int err;
16586185db85Sdougm 	struct stat st;
16596185db85Sdougm 
16606185db85Sdougm 	err = stat(path, &st);
166125a68471Sdougm 	if (err < 0)
166225a68471Sdougm 		err = SA_NO_SUCH_PATH;
166325a68471Sdougm 	else
166425a68471Sdougm 		err = SA_OK;
166525a68471Sdougm 
166625a68471Sdougm 	/*
166725a68471Sdougm 	 * If we have a valid path at this point ret, return the fstype.
166825a68471Sdougm 	 */
166925a68471Sdougm 	if (err == SA_OK)
167025a68471Sdougm 		return (strdup(st.st_fstype));
167125a68471Sdougm 
16726185db85Sdougm 	return (NULL);
16736185db85Sdougm }
16746185db85Sdougm 
16756185db85Sdougm void
16766185db85Sdougm sa_free_fstype(char *type)
16776185db85Sdougm {
16786185db85Sdougm 	free(type);
16796185db85Sdougm }
16806185db85Sdougm 
16816185db85Sdougm /*
16826185db85Sdougm  * sa_get_derived_optionset(object, proto, hier)
16836185db85Sdougm  *
16846185db85Sdougm  *	Work backward to the top of the share object tree and start
16856185db85Sdougm  *	copying protocol specific optionsets into a newly created
16866185db85Sdougm  *	optionset that doesn't have a parent (it will be freed
1687da6c28aaSamw  *	later). This provides for the property inheritance model. That
16886185db85Sdougm  *	is, properties closer to the share take precedence over group
16896185db85Sdougm  *	level. This also provides for groups of groups in the future.
16906185db85Sdougm  */
16916185db85Sdougm 
16926185db85Sdougm sa_optionset_t
16936185db85Sdougm sa_get_derived_optionset(void *object, char *proto, int hier)
16946185db85Sdougm {
16956185db85Sdougm 	sa_optionset_t newoptionset;
16966185db85Sdougm 	sa_optionset_t optionset;
16976185db85Sdougm 	sa_group_t group;
16986185db85Sdougm 
16996185db85Sdougm 	if (hier &&
17006185db85Sdougm 	    (group = sa_get_parent_group((sa_share_t)object)) != NULL) {
170125a68471Sdougm 		newoptionset = sa_get_derived_optionset((void *)group, proto,
170225a68471Sdougm 		    hier);
17036185db85Sdougm 	} else {
170425a68471Sdougm 		newoptionset = (sa_optionset_t)xmlNewNode(NULL,
170525a68471Sdougm 		    (xmlChar *)"optionset");
170625a68471Sdougm 		if (newoptionset != NULL) {
170725a68471Sdougm 			sa_set_optionset_attr(newoptionset, "type", proto);
170825a68471Sdougm 		}
17096185db85Sdougm 	}
171025a68471Sdougm 	/* Dont' do anything if memory wasn't allocated */
17116185db85Sdougm 	if (newoptionset == NULL)
171225a68471Sdougm 		return (NULL);
17136185db85Sdougm 
171425a68471Sdougm 	/* Found the top so working back down the stack */
17156185db85Sdougm 	optionset = sa_get_optionset((sa_optionset_t)object, proto);
17166185db85Sdougm 	if (optionset != NULL) {
171725a68471Sdougm 		sa_property_t prop;
171825a68471Sdougm 		/* add optionset to the newoptionset */
171925a68471Sdougm 		for (prop = sa_get_property(optionset, NULL);
172025a68471Sdougm 		    prop != NULL;
172125a68471Sdougm 		    prop = sa_get_next_property(prop)) {
172225a68471Sdougm 			sa_property_t newprop;
172325a68471Sdougm 			char *name;
172425a68471Sdougm 			char *value;
172525a68471Sdougm 			name = sa_get_property_attr(prop, "type");
172625a68471Sdougm 			value = sa_get_property_attr(prop, "value");
172725a68471Sdougm 			if (name == NULL)
172825a68471Sdougm 				continue;
172925a68471Sdougm 			newprop = sa_get_property(newoptionset, name);
173025a68471Sdougm 			/* Replace the value with the new value */
173125a68471Sdougm 			if (newprop != NULL) {
173225a68471Sdougm 				/*
173325a68471Sdougm 				 * Only set if value is non NULL, old value ok
173425a68471Sdougm 				 * if it is NULL.
173525a68471Sdougm 				 */
173625a68471Sdougm 				if (value != NULL)
173725a68471Sdougm 					set_node_attr(newprop, "value", value);
173825a68471Sdougm 			} else {
173925a68471Sdougm 				/* an entirely new property */
174025a68471Sdougm 				if (value != NULL) {
174125a68471Sdougm 					newprop = sa_create_property(name,
174225a68471Sdougm 					    value);
174325a68471Sdougm 					if (newprop != NULL) {
174425a68471Sdougm 						newprop = (sa_property_t)
174525a68471Sdougm 						    xmlAddChild(
174625a68471Sdougm 						    (xmlNodePtr)newoptionset,
174725a68471Sdougm 						    (xmlNodePtr)newprop);
174825a68471Sdougm 					}
174925a68471Sdougm 				}
17506185db85Sdougm 			}
175125a68471Sdougm 			sa_free_attr_string(name);
175225a68471Sdougm 
175325a68471Sdougm 			if (value != NULL)
175425a68471Sdougm 				sa_free_attr_string(value);
17556185db85Sdougm 		}
17566185db85Sdougm 	}
17576185db85Sdougm 	return (newoptionset);
17586185db85Sdougm }
17596185db85Sdougm 
17606185db85Sdougm void
17616185db85Sdougm sa_free_derived_optionset(sa_optionset_t optionset)
17626185db85Sdougm {
176325a68471Sdougm 	/* While it shouldn't be linked, it doesn't hurt */
17646185db85Sdougm 	if (optionset != NULL) {
176525a68471Sdougm 		xmlUnlinkNode((xmlNodePtr) optionset);
176625a68471Sdougm 		xmlFreeNode((xmlNodePtr) optionset);
17676185db85Sdougm 	}
17686185db85Sdougm }
17696185db85Sdougm 
17706185db85Sdougm /*
17716185db85Sdougm  *  sa_get_all_security_types(object, proto, hier)
17726185db85Sdougm  *
177325a68471Sdougm  *	Find all the security types set for this object.  This is
17746185db85Sdougm  *	preliminary to getting a derived security set. The return value is an
17756185db85Sdougm  *	optionset containg properties which are the sectype values found by
1776da6c28aaSamw  *	walking up the XML document structure. The returned optionset
17776185db85Sdougm  *	is a derived optionset.
17786185db85Sdougm  *
17796185db85Sdougm  *	If hier is 0, only look at object. If non-zero, walk up the tree.
17806185db85Sdougm  */
17816185db85Sdougm sa_optionset_t
17826185db85Sdougm sa_get_all_security_types(void *object, char *proto, int hier)
17836185db85Sdougm {
17846185db85Sdougm 	sa_optionset_t options;
17856185db85Sdougm 	sa_security_t security;
17866185db85Sdougm 	sa_group_t group;
17876185db85Sdougm 	sa_property_t prop;
17886185db85Sdougm 
17896185db85Sdougm 	options = NULL;
17906185db85Sdougm 
17916185db85Sdougm 	if (hier &&
179225a68471Sdougm 	    (group = sa_get_parent_group((sa_share_t)object)) != NULL)
179325a68471Sdougm 		options = sa_get_all_security_types((void *)group, proto, hier);
179425a68471Sdougm 	else
179525a68471Sdougm 		options = (sa_optionset_t)xmlNewNode(NULL,
179625a68471Sdougm 		    (xmlChar *)"optionset");
179725a68471Sdougm 
179825a68471Sdougm 	if (options == NULL)
179925a68471Sdougm 		return (options);
180025a68471Sdougm 
180125a68471Sdougm 	/* Hit the top so collect the security types working back. */
180225a68471Sdougm 	for (security = sa_get_security((sa_group_t)object, NULL, NULL);
180325a68471Sdougm 	    security != NULL;
180425a68471Sdougm 	    security = sa_get_next_security(security)) {
18056185db85Sdougm 		char *type;
18066185db85Sdougm 		char *sectype;
18076185db85Sdougm 
18086185db85Sdougm 		type = sa_get_security_attr(security, "type");
18096185db85Sdougm 		if (type != NULL) {
181025a68471Sdougm 			if (strcmp(type, proto) != 0) {
181125a68471Sdougm 				sa_free_attr_string(type);
181225a68471Sdougm 				continue;
181325a68471Sdougm 			}
181425a68471Sdougm 			sectype = sa_get_security_attr(security, "sectype");
181525a68471Sdougm 			if (sectype != NULL) {
181625a68471Sdougm 				/*
181725a68471Sdougm 				 * Have a security type, check to see if
181825a68471Sdougm 				 * already present in optionset and add if it
181925a68471Sdougm 				 * isn't.
182025a68471Sdougm 				 */
182125a68471Sdougm 				if (sa_get_property(options, sectype) == NULL) {
182225a68471Sdougm 					prop = sa_create_property(sectype,
182325a68471Sdougm 					    "true");
182425a68471Sdougm 					if (prop != NULL)
182525a68471Sdougm 						prop = (sa_property_t)
182625a68471Sdougm 						    xmlAddChild(
182725a68471Sdougm 						    (xmlNodePtr)options,
182825a68471Sdougm 						    (xmlNodePtr)prop);
182925a68471Sdougm 				}
183025a68471Sdougm 				sa_free_attr_string(sectype);
18316185db85Sdougm 			}
183225a68471Sdougm 			sa_free_attr_string(type);
18336185db85Sdougm 		}
18346185db85Sdougm 	}
183525a68471Sdougm 
18366185db85Sdougm 	return (options);
18376185db85Sdougm }
18386185db85Sdougm 
18396185db85Sdougm /*
18406185db85Sdougm  * sa_get_derived_security(object, sectype, proto, hier)
18416185db85Sdougm  *
18426185db85Sdougm  * Get the derived security(named optionset) for the object given the
18436185db85Sdougm  * sectype and proto. If hier is non-zero, walk up the tree to get all
18446185db85Sdougm  * properties defined for this object, otherwise just those on the
18456185db85Sdougm  * object.
18466185db85Sdougm  */
18476185db85Sdougm 
18486185db85Sdougm sa_security_t
18496185db85Sdougm sa_get_derived_security(void *object, char *sectype, char *proto, int hier)
18506185db85Sdougm {
18516185db85Sdougm 	sa_security_t newsecurity;
18526185db85Sdougm 	sa_security_t security;
18536185db85Sdougm 	sa_group_t group;
185425a68471Sdougm 	sa_property_t prop;
18556185db85Sdougm 
18566185db85Sdougm 	if (hier &&
18576185db85Sdougm 	    (group = sa_get_parent_group((sa_share_t)object)) != NULL) {
185825a68471Sdougm 		newsecurity = sa_get_derived_security((void *)group,
185925a68471Sdougm 		    sectype, proto, hier);
18606185db85Sdougm 	} else {
186125a68471Sdougm 		newsecurity = (sa_security_t)xmlNewNode(NULL,
186225a68471Sdougm 		    (xmlChar *)"security");
186325a68471Sdougm 		if (newsecurity != NULL) {
186425a68471Sdougm 			sa_set_security_attr(newsecurity, "type", proto);
186525a68471Sdougm 			sa_set_security_attr(newsecurity, "sectype", sectype);
186625a68471Sdougm 		}
18676185db85Sdougm 	}
186825a68471Sdougm 	/* Don't do anything if memory wasn't allocated */
18696185db85Sdougm 	if (newsecurity == NULL)
187025a68471Sdougm 		return (newsecurity);
18716185db85Sdougm 
187225a68471Sdougm 	/* Found the top so working back down the stack. */
18736185db85Sdougm 	security = sa_get_security((sa_security_t)object, sectype, proto);
187425a68471Sdougm 	if (security == NULL)
187525a68471Sdougm 		return (newsecurity);
187625a68471Sdougm 
187725a68471Sdougm 	/* add security to the newsecurity */
187825a68471Sdougm 	for (prop = sa_get_property(security, NULL);
187925a68471Sdougm 	    prop != NULL; prop = sa_get_next_property(prop)) {
18806185db85Sdougm 		sa_property_t newprop;
18816185db85Sdougm 		char *name;
18826185db85Sdougm 		char *value;
18836185db85Sdougm 		name = sa_get_property_attr(prop, "type");
18846185db85Sdougm 		value = sa_get_property_attr(prop, "value");
18856185db85Sdougm 		if (name != NULL) {
188625a68471Sdougm 			newprop = sa_get_property(newsecurity, name);
188725a68471Sdougm 			/* Replace the value with the new value */
188825a68471Sdougm 			if (newprop != NULL) {
188925a68471Sdougm 				/*
189097df5ac9Sdougm 				 * Only set if value is non NULL, old
189197df5ac9Sdougm 				 * value ok if it is NULL. The value
189297df5ac9Sdougm 				 * must be associated with the "value"
189397df5ac9Sdougm 				 * tag within XML.
189425a68471Sdougm 				 */
189525a68471Sdougm 				if (value != NULL)
189697df5ac9Sdougm 					set_node_attr(newprop, "value", value);
189725a68471Sdougm 			} else {
189825a68471Sdougm 				/* An entirely new property */
189925a68471Sdougm 				if (value != NULL) {
190025a68471Sdougm 					newprop = sa_create_property(name,
190125a68471Sdougm 					    value);
190225a68471Sdougm 					newprop = (sa_property_t)
190325a68471Sdougm 					    xmlAddChild((xmlNodePtr)newsecurity,
19046185db85Sdougm 					    (xmlNodePtr)newprop);
190525a68471Sdougm 				}
19066185db85Sdougm 			}
190725a68471Sdougm 			sa_free_attr_string(name);
19086185db85Sdougm 		}
19096185db85Sdougm 		if (value != NULL)
191025a68471Sdougm 			sa_free_attr_string(value);
19116185db85Sdougm 	}
19126185db85Sdougm 	return (newsecurity);
19136185db85Sdougm }
19146185db85Sdougm 
19156185db85Sdougm void
19166185db85Sdougm sa_free_derived_security(sa_security_t security)
19176185db85Sdougm {
19186185db85Sdougm 	/* while it shouldn't be linked, it doesn't hurt */
19196185db85Sdougm 	if (security != NULL) {
192025a68471Sdougm 		xmlUnlinkNode((xmlNodePtr)security);
192125a68471Sdougm 		xmlFreeNode((xmlNodePtr)security);
19226185db85Sdougm 	}
19236185db85Sdougm }
19246185db85Sdougm 
19256185db85Sdougm /*
19266185db85Sdougm  * sharetab utility functions
19276185db85Sdougm  *
192825a68471Sdougm  * Makes use of the original sharetab.c from fs.d/nfs/lib
19296185db85Sdougm  */
19306185db85Sdougm 
19316185db85Sdougm /*
1932ecd6cf80Smarks  * sa_fillshare(share, proto, sh)
19336185db85Sdougm  *
19346185db85Sdougm  * Fill the struct share with values obtained from the share object.
19356185db85Sdougm  */
1936ecd6cf80Smarks void
1937ecd6cf80Smarks sa_fillshare(sa_share_t share, char *proto, struct share *sh)
19386185db85Sdougm {
19396185db85Sdougm 	char *groupname = NULL;
19406185db85Sdougm 	char *value;
19416185db85Sdougm 	sa_group_t group;
19426185db85Sdougm 	char *buff;
19436185db85Sdougm 	char *zfs;
1944da6c28aaSamw 	sa_resource_t resource;
1945da6c28aaSamw 	char *rsrcname = NULL;
1946da6c28aaSamw 	char *defprop;
1947da6c28aaSamw 
1948da6c28aaSamw 	/*
1949da6c28aaSamw 	 * We only want to deal with the path level shares for the
1950da6c28aaSamw 	 * sharetab file. If a resource, get the parent.
1951da6c28aaSamw 	 */
1952da6c28aaSamw 	if (sa_is_resource(share)) {
1953da6c28aaSamw 		resource = (sa_resource_t)share;
1954da6c28aaSamw 		share = sa_get_resource_parent(resource);
1955da6c28aaSamw 		rsrcname = sa_get_resource_attr(resource, "name");
1956da6c28aaSamw 	}
19576185db85Sdougm 
19586185db85Sdougm 	group = sa_get_parent_group(share);
19596185db85Sdougm 	if (group != NULL) {
196025a68471Sdougm 		zfs = sa_get_group_attr(group, "zfs");
196125a68471Sdougm 		groupname = sa_get_group_attr(group, "name");
19626185db85Sdougm 
196325a68471Sdougm 		if (groupname != NULL &&
196425a68471Sdougm 		    (strcmp(groupname, "default") == 0 || zfs != NULL)) {
196525a68471Sdougm 			/*
196625a68471Sdougm 			 * since the groupname is either "default" or the
196725a68471Sdougm 			 * group is a ZFS group, we don't want to keep
196825a68471Sdougm 			 * groupname. We do want it if it is any other type of
196925a68471Sdougm 			 * group.
197025a68471Sdougm 			 */
197125a68471Sdougm 			sa_free_attr_string(groupname);
197225a68471Sdougm 			groupname = NULL;
197325a68471Sdougm 		}
197425a68471Sdougm 		if (zfs != NULL)
197525a68471Sdougm 			sa_free_attr_string(zfs);
19766185db85Sdougm 	}
19776185db85Sdougm 
19786185db85Sdougm 	value = sa_get_share_attr(share, "path");
19796185db85Sdougm 	if (value != NULL) {
198025a68471Sdougm 		sh->sh_path = strdup(value);
198125a68471Sdougm 		sa_free_attr_string(value);
19826185db85Sdougm 	}
19836185db85Sdougm 
1984da6c28aaSamw 	if (rsrcname != NULL || groupname != NULL) {
198525a68471Sdougm 		int len = 0;
198625a68471Sdougm 
1987da6c28aaSamw 		if (rsrcname != NULL)
1988da6c28aaSamw 			len += strlen(rsrcname);
198925a68471Sdougm 		if (groupname != NULL)
199025a68471Sdougm 			len += strlen(groupname);
199125a68471Sdougm 		len += 3; /* worst case */
199225a68471Sdougm 		buff = malloc(len);
199325a68471Sdougm 		(void) snprintf(buff, len, "%s%s%s",
1994da6c28aaSamw 		    (rsrcname != NULL &&
1995da6c28aaSamw 		    strlen(rsrcname) > 0) ? rsrcname : "-",
19966185db85Sdougm 		    groupname != NULL ? "@" : "",
19976185db85Sdougm 		    groupname != NULL ? groupname : "");
199825a68471Sdougm 		sh->sh_res = buff;
1999da6c28aaSamw 		if (rsrcname != NULL)
2000da6c28aaSamw 			sa_free_attr_string(rsrcname);
2001da6c28aaSamw 		if (groupname != NULL)
200225a68471Sdougm 			sa_free_attr_string(groupname);
20036185db85Sdougm 	} else {
200425a68471Sdougm 		sh->sh_res = strdup("-");
20056185db85Sdougm 	}
20066185db85Sdougm 
2007da6c28aaSamw 	/*
2008da6c28aaSamw 	 * Get correct default prop string. NFS uses "rw", others use
2009da6c28aaSamw 	 * "".
2010da6c28aaSamw 	 */
2011da6c28aaSamw 	if (strcmp(proto, "nfs") != 0)
2012da6c28aaSamw 		defprop = "\"\"";
2013da6c28aaSamw 	else
2014da6c28aaSamw 		defprop = "rw";
2015da6c28aaSamw 
20166185db85Sdougm 	sh->sh_fstype = strdup(proto);
20176185db85Sdougm 	value = sa_proto_legacy_format(proto, share, 1);
20186185db85Sdougm 	if (value != NULL) {
201925a68471Sdougm 		if (strlen(value) > 0)
202025a68471Sdougm 			sh->sh_opts = strdup(value);
202125a68471Sdougm 		else
2022da6c28aaSamw 			sh->sh_opts = strdup(defprop);
202325a68471Sdougm 		free(value);
202425a68471Sdougm 	} else {
2025da6c28aaSamw 		sh->sh_opts = strdup(defprop);
202625a68471Sdougm 	}
20276185db85Sdougm 
20286185db85Sdougm 	value = sa_get_share_description(share);
20296185db85Sdougm 	if (value != NULL) {
203025a68471Sdougm 		sh->sh_descr = strdup(value);
203125a68471Sdougm 		sa_free_share_description(value);
203225a68471Sdougm 	} else {
203325a68471Sdougm 		sh->sh_descr = strdup("");
203425a68471Sdougm 	}
20356185db85Sdougm }
20366185db85Sdougm 
20376185db85Sdougm /*
2038ecd6cf80Smarks  * sa_emptyshare(sh)
20396185db85Sdougm  *
20406185db85Sdougm  * Free the strings in the non-NULL members of sh.
20416185db85Sdougm  */
20426185db85Sdougm 
2043ecd6cf80Smarks void
2044ecd6cf80Smarks sa_emptyshare(struct share *sh)
20456185db85Sdougm {
20466185db85Sdougm 	if (sh->sh_path != NULL)
204725a68471Sdougm 		free(sh->sh_path);
20486185db85Sdougm 	sh->sh_path = NULL;
20496185db85Sdougm 	if (sh->sh_res != NULL)
205025a68471Sdougm 		free(sh->sh_res);
20516185db85Sdougm 	sh->sh_res = NULL;
20526185db85Sdougm 	if (sh->sh_fstype != NULL)
205325a68471Sdougm 		free(sh->sh_fstype);
20546185db85Sdougm 	sh->sh_fstype = NULL;
20556185db85Sdougm 	if (sh->sh_opts != NULL)
205625a68471Sdougm 		free(sh->sh_opts);
20576185db85Sdougm 	sh->sh_opts = NULL;
20586185db85Sdougm 	if (sh->sh_descr != NULL)
205925a68471Sdougm 		free(sh->sh_descr);
20606185db85Sdougm 	sh->sh_descr = NULL;
20616185db85Sdougm }
20626185db85Sdougm 
2063*5b6e0c46Sdougm /*
2064*5b6e0c46Sdougm  * sa_update_sharetab_ts(handle)
2065*5b6e0c46Sdougm  *
2066*5b6e0c46Sdougm  * Update the internal timestamp of when sharetab was last
2067*5b6e0c46Sdougm  * changed. This needs to be public for ZFS to get at it.
2068*5b6e0c46Sdougm  */
2069*5b6e0c46Sdougm 
2070*5b6e0c46Sdougm void
2071*5b6e0c46Sdougm sa_update_sharetab_ts(sa_handle_t handle)
2072*5b6e0c46Sdougm {
2073*5b6e0c46Sdougm 	struct stat st;
2074*5b6e0c46Sdougm 	sa_handle_impl_t implhandle = (sa_handle_impl_t)handle;
2075*5b6e0c46Sdougm 
2076*5b6e0c46Sdougm 	if (implhandle != NULL && stat(SA_LEGACY_SHARETAB, &st) == 0)
2077*5b6e0c46Sdougm 		implhandle->tssharetab = TSTAMP(st.st_mtim);
2078*5b6e0c46Sdougm }
2079*5b6e0c46Sdougm 
20806185db85Sdougm /*
20816185db85Sdougm  * sa_update_sharetab(share, proto)
20826185db85Sdougm  *
20836185db85Sdougm  * Update the sharetab file with info from the specified share.
20846185db85Sdougm  * This could be an update or add.
20856185db85Sdougm  */
20866185db85Sdougm 
20876185db85Sdougm int
20886185db85Sdougm sa_update_sharetab(sa_share_t share, char *proto)
20896185db85Sdougm {
2090a237e38eSth 	int	ret = SA_OK;
2091a237e38eSth 	share_t	sh;
2092a237e38eSth 	char	*path;
2093*5b6e0c46Sdougm 	sa_handle_t handle;
20946185db85Sdougm 
20956185db85Sdougm 	path = sa_get_share_attr(share, "path");
20966185db85Sdougm 	if (path != NULL) {
2097a237e38eSth 		(void) memset(&sh, '\0', sizeof (sh));
2098a237e38eSth 
2099*5b6e0c46Sdougm 		handle = sa_find_group_handle((sa_group_t)share);
2100*5b6e0c46Sdougm 		if (handle != NULL) {
2101*5b6e0c46Sdougm 			/*
2102*5b6e0c46Sdougm 			 * Fill in share structure and send it to the kernel.
2103*5b6e0c46Sdougm 			 */
2104*5b6e0c46Sdougm 			(void) sa_fillshare(share, proto, &sh);
2105*5b6e0c46Sdougm 			(void) _sharefs(SHAREFS_ADD, &sh);
2106*5b6e0c46Sdougm 			/*
2107*5b6e0c46Sdougm 			 * We need the timestamp of the sharetab file right
2108*5b6e0c46Sdougm 			 * after the update was done. This lets us detect a
2109*5b6e0c46Sdougm 			 * change that made by a different process.
2110*5b6e0c46Sdougm 			 */
2111*5b6e0c46Sdougm 			sa_update_sharetab_ts(handle);
2112*5b6e0c46Sdougm 			sa_emptyshare(&sh);
2113*5b6e0c46Sdougm 		} else {
2114*5b6e0c46Sdougm 			ret = SA_CONFIG_ERR;
2115*5b6e0c46Sdougm 		}
2116a237e38eSth 		sa_free_attr_string(path);
21176185db85Sdougm 	}
2118a237e38eSth 
21196185db85Sdougm 	return (ret);
21206185db85Sdougm }
21216185db85Sdougm 
21226185db85Sdougm /*
2123*5b6e0c46Sdougm  * sa_delete_sharetab(handle, path, proto)
21246185db85Sdougm  *
21256185db85Sdougm  * remove the specified share from sharetab.
21266185db85Sdougm  */
21276185db85Sdougm 
21286185db85Sdougm int
2129*5b6e0c46Sdougm sa_delete_sharetab(sa_handle_t handle, char *path, char *proto)
21306185db85Sdougm {
2131a237e38eSth 	int	ret = SA_OK;
2132*5b6e0c46Sdougm 	struct stat st;
21336185db85Sdougm 
2134a237e38eSth 	share_t	sh;
2135a237e38eSth 	/*
2136a237e38eSth 	 * Both the path and the proto are
2137a237e38eSth 	 * keys into the sharetab.
2138a237e38eSth 	 */
2139a237e38eSth 	if (path != NULL && proto != NULL) {
2140a237e38eSth 		(void) memset(&sh, '\0', sizeof (sh));
2141a237e38eSth 		sh.sh_path = path;
2142a237e38eSth 		sh.sh_fstype = proto;
2143a237e38eSth 
2144a3175730Sth 		ret = _sharefs(SHAREFS_REMOVE, &sh);
2145*5b6e0c46Sdougm 		if (handle != NULL && stat(SA_LEGACY_SHARETAB, &st) == 0)
2146*5b6e0c46Sdougm 			sa_update_sharetab_ts(handle);
21476185db85Sdougm 	}
21486185db85Sdougm 	return (ret);
21496185db85Sdougm }
2150*5b6e0c46Sdougm 
2151*5b6e0c46Sdougm /*
2152*5b6e0c46Sdougm  * sa_needs_refresh(handle)
2153*5b6e0c46Sdougm  *
2154*5b6e0c46Sdougm  * Returns B_TRUE if the internal cache needs to be refreshed do to a
2155*5b6e0c46Sdougm  * change by another process.  B_FALSE returned otherwise.
2156*5b6e0c46Sdougm  */
2157*5b6e0c46Sdougm boolean_t
2158*5b6e0c46Sdougm sa_needs_refresh(sa_handle_t *handle)
2159*5b6e0c46Sdougm {
2160*5b6e0c46Sdougm 	sa_handle_impl_t implhandle = (sa_handle_impl_t)handle;
2161*5b6e0c46Sdougm 	struct stat st;
2162*5b6e0c46Sdougm 	char *str;
2163*5b6e0c46Sdougm 	uint64_t tstamp;
2164*5b6e0c46Sdougm 	scf_simple_prop_t *prop;
2165*5b6e0c46Sdougm 
2166*5b6e0c46Sdougm 	if (handle == NULL)
2167*5b6e0c46Sdougm 		return (B_TRUE);
2168*5b6e0c46Sdougm 
2169*5b6e0c46Sdougm 	/*
2170*5b6e0c46Sdougm 	 * If sharetab has changed, then there was an external
2171*5b6e0c46Sdougm 	 * change. Check sharetab first since it is updated by ZFS as
2172*5b6e0c46Sdougm 	 * well as sharemgr.  This is where external ZFS changes are
2173*5b6e0c46Sdougm 	 * caught.
2174*5b6e0c46Sdougm 	 */
2175*5b6e0c46Sdougm 	if (stat(SA_LEGACY_SHARETAB, &st) == 0 &&
2176*5b6e0c46Sdougm 	    TSTAMP(st.st_mtim) != implhandle->tssharetab)
2177*5b6e0c46Sdougm 		return (B_TRUE);
2178*5b6e0c46Sdougm 
2179*5b6e0c46Sdougm 	/*
2180*5b6e0c46Sdougm 	 * If sharetab wasn't changed, check whether there were any
2181*5b6e0c46Sdougm 	 * SMF transactions that modified the config but didn't
2182*5b6e0c46Sdougm 	 * initiate a share.  This is less common but does happen.
2183*5b6e0c46Sdougm 	 */
2184*5b6e0c46Sdougm 	prop = scf_simple_prop_get(implhandle->scfhandle->handle,
2185*5b6e0c46Sdougm 	    (const char *)SA_SVC_FMRI_BASE ":default", "state",
2186*5b6e0c46Sdougm 	    "lastupdate");
2187*5b6e0c46Sdougm 	if (prop != NULL) {
2188*5b6e0c46Sdougm 		str = scf_simple_prop_next_astring(prop);
2189*5b6e0c46Sdougm 		if (str != NULL)
2190*5b6e0c46Sdougm 			tstamp = strtoull(str, NULL, 0);
2191*5b6e0c46Sdougm 		else
2192*5b6e0c46Sdougm 			tstamp = 0;
2193*5b6e0c46Sdougm 		scf_simple_prop_free(prop);
2194*5b6e0c46Sdougm 		if (tstamp != implhandle->tstrans)
2195*5b6e0c46Sdougm 			return (B_TRUE);
2196*5b6e0c46Sdougm 	}
2197*5b6e0c46Sdougm 
2198*5b6e0c46Sdougm 	return (B_FALSE);
2199*5b6e0c46Sdougm }
2200*5b6e0c46Sdougm 
2201da6c28aaSamw /*
2202da6c28aaSamw  * sa_fix_resource_name(path)
2203da6c28aaSamw  *
2204da6c28aaSamw  * change all illegal characters to something else.  For now, all get
2205da6c28aaSamw  * converted to '_' and the leading '/' is stripped off. This is used
2206da6c28aaSamw  * to construct an resource name (SMB share name) that is valid.
2207da6c28aaSamw  * Caller must pass a valid path.
2208da6c28aaSamw  */
2209da6c28aaSamw void
2210da6c28aaSamw sa_fix_resource_name(char *path)
2211da6c28aaSamw {
2212da6c28aaSamw 	char *cp;
2213da6c28aaSamw 	size_t len;
2214da6c28aaSamw 
2215da6c28aaSamw 	assert(path != NULL);
2216da6c28aaSamw 
2217da6c28aaSamw 	/* make sure we are appropriate length */
2218da6c28aaSamw 	cp = path;
2219da6c28aaSamw 	if (*cp == '/')
2220da6c28aaSamw 		cp++; /* skip leading slash */
2221da6c28aaSamw 	while (cp != NULL && strlen(cp) > SA_MAX_RESOURCE_NAME) {
2222da6c28aaSamw 		cp = strchr(cp, '/');
2223da6c28aaSamw 		if (cp != NULL)
2224da6c28aaSamw 			cp++;
2225da6c28aaSamw 	}
2226da6c28aaSamw 	/* two cases - cp == NULL and cp is substring of path */
2227da6c28aaSamw 	if (cp == NULL) {
2228da6c28aaSamw 		/* just take last SA_MAX_RESOURCE_NAME chars */
2229da6c28aaSamw 		len = 1 + strlen(path) - SA_MAX_RESOURCE_NAME;
2230da6c28aaSamw 		(void) memmove(path, path + len, SA_MAX_RESOURCE_NAME);
2231da6c28aaSamw 		path[SA_MAX_RESOURCE_NAME] = '\0';
2232da6c28aaSamw 	} else {
2233da6c28aaSamw 		len = strlen(cp) + 1;
2234da6c28aaSamw 		(void) memmove(path, cp, len);
2235da6c28aaSamw 	}
2236da6c28aaSamw 
2237da6c28aaSamw 	/*
2238da6c28aaSamw 	 * Don't want any of the characters that are not allowed
2239da6c28aaSamw 	 * in an SMB share name. Replace them with '_'.
2240da6c28aaSamw 	 */
2241da6c28aaSamw 	while (*path) {
2242da6c28aaSamw 		switch (*path) {
2243da6c28aaSamw 		case '/':
2244da6c28aaSamw 		case '"':
2245da6c28aaSamw 		case '\\':
2246da6c28aaSamw 		case '[':
2247da6c28aaSamw 		case ']':
2248da6c28aaSamw 		case ':':
2249da6c28aaSamw 		case '|':
2250da6c28aaSamw 		case '<':
2251da6c28aaSamw 		case '>':
2252da6c28aaSamw 		case '+':
2253da6c28aaSamw 		case ';':
2254da6c28aaSamw 		case ',':
2255da6c28aaSamw 		case '?':
2256da6c28aaSamw 		case '*':
2257da6c28aaSamw 		case '=':
2258da6c28aaSamw 		case '\t':
2259da6c28aaSamw 			*path = '_';
2260da6c28aaSamw 			break;
2261da6c28aaSamw 		}
2262da6c28aaSamw 		path++;
2263da6c28aaSamw 	}
2264da6c28aaSamw }
2265