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 /*
23148c5f43SAlan Wright  * Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
24*5cb0d679SMarcel Telka  * Copyright 2014 Nexenta Systems, Inc.  All rights reserved.
256185db85Sdougm  */
266185db85Sdougm 
276185db85Sdougm /*
286185db85Sdougm  * core library for common functions across all config store types
296185db85Sdougm  * and file systems to be exported. This includes legacy dfstab/sharetab
306185db85Sdougm  * parsing. Need to eliminate XML where possible.
316185db85Sdougm  */
326185db85Sdougm 
336185db85Sdougm #include <stdio.h>
346185db85Sdougm #include <string.h>
356185db85Sdougm #include <ctype.h>
366185db85Sdougm #include <unistd.h>
376185db85Sdougm #include <limits.h>
386185db85Sdougm #include <errno.h>
396185db85Sdougm #include <sys/types.h>
406185db85Sdougm #include <sys/stat.h>
416185db85Sdougm #include <libxml/parser.h>
426185db85Sdougm #include <libxml/tree.h>
436185db85Sdougm #include "libshare.h"
446185db85Sdougm #include "libshare_impl.h"
456185db85Sdougm #include <fcntl.h>
468d7e4166Sjose borrego #include <thread.h>
476185db85Sdougm #include <grp.h>
486185db85Sdougm #include <limits.h>
496185db85Sdougm #include <sys/param.h>
506185db85Sdougm #include <signal.h>
516185db85Sdougm #include <libintl.h>
52da6c28aaSamw #include <dirent.h>
536185db85Sdougm 
54a237e38eSth #include <sharefs/share.h>
556185db85Sdougm #include "sharetab.h"
566185db85Sdougm 
576185db85Sdougm #define	DFSTAB_NOTICE_LINES	5
586185db85Sdougm static char *notice[DFSTAB_NOTICE_LINES] =	{
596185db85Sdougm 	"# Do not modify this file directly.\n",
606185db85Sdougm 	"# Use the sharemgr(1m) command for all share management\n",
616185db85Sdougm 	"# This file is reconstructed and only maintained for backward\n",
626185db85Sdougm 	"# compatibility. Configuration lines could be lost.\n",
636185db85Sdougm 	"#\n"
646185db85Sdougm };
656185db85Sdougm 
666185db85Sdougm #define	STRNCAT(x, y, z)	(xmlChar *)strncat((char *)x, (char *)y, z)
676185db85Sdougm 
686185db85Sdougm /* will be much smaller, but this handles bad syntax in the file */
696185db85Sdougm #define	MAXARGSFORSHARE	256
706185db85Sdougm 
718d7e4166Sjose borrego static mutex_t sharetab_lock = DEFAULTMUTEX;
72148c5f43SAlan Wright extern mutex_t sa_dfstab_lock;
738d7e4166Sjose borrego 
746185db85Sdougm /* used internally only */
756185db85Sdougm typedef
766185db85Sdougm struct sharelist {
776185db85Sdougm     struct sharelist *next;
786185db85Sdougm     int   persist;
796185db85Sdougm     char *path;
806185db85Sdougm     char *resource;
816185db85Sdougm     char *fstype;
826185db85Sdougm     char *options;
836185db85Sdougm     char *description;
846185db85Sdougm     char *group;
856185db85Sdougm     char *origline;
866185db85Sdougm     int lineno;
876185db85Sdougm } xfs_sharelist_t;
88549ec3ffSdougm static void parse_dfstab(sa_handle_t, char *, xmlNodePtr);
898d7e4166Sjose borrego extern char *_sa_get_token(char *);
906185db85Sdougm static void dfs_free_list(xfs_sharelist_t *);
916185db85Sdougm /* prototypes */
92549ec3ffSdougm void getlegacyconfig(sa_handle_t, char *, xmlNodePtr *);
93da6c28aaSamw extern sa_share_t _sa_add_share(sa_group_t, char *, int, int *, uint64_t);
94549ec3ffSdougm extern sa_group_t _sa_create_group(sa_handle_impl_t, char *);
956185db85Sdougm static void outdfstab(FILE *, xfs_sharelist_t *);
966185db85Sdougm extern int _sa_remove_optionset(sa_optionset_t);
976185db85Sdougm extern int set_node_share(void *, char *, char *);
986185db85Sdougm extern void set_node_attr(void *, char *, char *);
996185db85Sdougm 
100a99982a7Sdougm /*
101a99982a7Sdougm  * sablocksigs(*sigs)
102a99982a7Sdougm  *
103a99982a7Sdougm  * block important signals for a critical region. Arg is a pointer to
104a99982a7Sdougm  * a sigset_t that is used later for the unblock.
105a99982a7Sdougm  */
106a99982a7Sdougm void
107a99982a7Sdougm sablocksigs(sigset_t *sigs)
108a99982a7Sdougm {
109a99982a7Sdougm 	sigset_t new;
110a99982a7Sdougm 
111a99982a7Sdougm 	if (sigs != NULL) {
11225a68471Sdougm 		(void) sigprocmask(SIG_BLOCK, NULL, &new);
11325a68471Sdougm 		(void) sigaddset(&new, SIGHUP);
11425a68471Sdougm 		(void) sigaddset(&new, SIGINT);
11525a68471Sdougm 		(void) sigaddset(&new, SIGQUIT);
11625a68471Sdougm 		(void) sigaddset(&new, SIGTSTP);
11725a68471Sdougm 		(void) sigprocmask(SIG_SETMASK, &new, sigs);
118a99982a7Sdougm 	}
119a99982a7Sdougm }
120a99982a7Sdougm 
121a99982a7Sdougm /*
122a99982a7Sdougm  * saunblocksigs(*sigs)
123a99982a7Sdougm  *
124a99982a7Sdougm  * unblock previously blocked signals from the sigs arg.
125a99982a7Sdougm  */
126a99982a7Sdougm void
127a99982a7Sdougm saunblocksigs(sigset_t *sigs)
128a99982a7Sdougm {
129a99982a7Sdougm 	if (sigs != NULL)
13025a68471Sdougm 		(void) sigprocmask(SIG_SETMASK, sigs, NULL);
131a99982a7Sdougm }
132a99982a7Sdougm 
1336185db85Sdougm /*
1346185db85Sdougm  * alloc_sharelist()
1356185db85Sdougm  *
1366185db85Sdougm  * allocator function to return an zfs_sharelist_t
1376185db85Sdougm  */
1386185db85Sdougm 
1396185db85Sdougm static xfs_sharelist_t *
1406185db85Sdougm alloc_sharelist()
1416185db85Sdougm {
1426185db85Sdougm 	xfs_sharelist_t *item;
1436185db85Sdougm 
1446185db85Sdougm 	item = (xfs_sharelist_t *)malloc(sizeof (xfs_sharelist_t));
1456185db85Sdougm 	if (item != NULL)
14625a68471Sdougm 		(void) memset(item, '\0', sizeof (xfs_sharelist_t));
1476185db85Sdougm 	return (item);
1486185db85Sdougm }
1496185db85Sdougm 
1506185db85Sdougm /*
1516185db85Sdougm  * fix_notice(list)
1526185db85Sdougm  *
1536185db85Sdougm  * Look at the beginning of the current /etc/dfs/dfstab file and add
1546185db85Sdougm  * the do not modify notice if it doesn't exist.
1556185db85Sdougm  */
1566185db85Sdougm 
1576185db85Sdougm static xfs_sharelist_t *
1586185db85Sdougm fix_notice(xfs_sharelist_t *list)
1596185db85Sdougm {
1606185db85Sdougm 	xfs_sharelist_t *item, *prev;
1616185db85Sdougm 	int i;
1626185db85Sdougm 
1637d968cb8Sdougm 	if (list == NULL) {
16425a68471Sdougm 		/* zero length dfstab */
16525a68471Sdougm 		list = alloc_sharelist();
16625a68471Sdougm 		if (list == NULL)
16725a68471Sdougm 			return (NULL);
16825a68471Sdougm 		list->description = strdup("#\n");
1697d968cb8Sdougm 	}
1706185db85Sdougm 	if (list->path == NULL && list->description != NULL &&
1716185db85Sdougm 	    strcmp(list->description, notice[0]) != 0) {
17225a68471Sdougm 		for (prev = NULL, i = 0; i < DFSTAB_NOTICE_LINES; i++) {
17325a68471Sdougm 			item = alloc_sharelist();
17425a68471Sdougm 			if (item != NULL) {
17525a68471Sdougm 				item->description = strdup(notice[i]);
17625a68471Sdougm 				if (prev == NULL) {
17725a68471Sdougm 					item->next = list;
17825a68471Sdougm 					prev = item;
17925a68471Sdougm 					list = item;
18025a68471Sdougm 				} else {
18125a68471Sdougm 					item->next = prev->next;
18225a68471Sdougm 					prev->next = item;
18325a68471Sdougm 					prev = item;
18425a68471Sdougm 				}
18525a68471Sdougm 			}
1866185db85Sdougm 		}
1876185db85Sdougm 	}
1886185db85Sdougm 	return (list);
1896185db85Sdougm }
1906185db85Sdougm 
1916185db85Sdougm /*
1926185db85Sdougm  * getdfstab(dfs)
1936185db85Sdougm  *
1946185db85Sdougm  * Returns an zfs_sharelist_t list of lines from the dfstab file
1956185db85Sdougm  * pointed to by the FILE pointer dfs. Each entry is parsed and the
1966185db85Sdougm  * original line is also preserved. Used in parsing and updating the
1976185db85Sdougm  * dfstab file.
1986185db85Sdougm  */
1996185db85Sdougm 
2006185db85Sdougm static xfs_sharelist_t *
2016185db85Sdougm getdfstab(FILE *dfs)
2026185db85Sdougm {
2036185db85Sdougm 	char buff[_POSIX_ARG_MAX]; /* reasonable size given syntax of share */
2046185db85Sdougm 	char *bp;
2056185db85Sdougm 	char *token;
2066185db85Sdougm 	char *args[MAXARGSFORSHARE];
2076185db85Sdougm 	int argc;
2086185db85Sdougm 	int c;
2096185db85Sdougm 	static int line = 0;
2107d968cb8Sdougm 	xfs_sharelist_t *item = NULL, *first = NULL, *last;
2116185db85Sdougm 
2126185db85Sdougm 	if (dfs != NULL) {
21325a68471Sdougm 		first = NULL;
21425a68471Sdougm 		line = 0;
21525a68471Sdougm 		while (fgets(buff, sizeof (buff), dfs) != NULL) {
21625a68471Sdougm 			line++;
21725a68471Sdougm 			bp = buff;
21825a68471Sdougm 			if (buff[0] == '#') {
21925a68471Sdougm 				item = alloc_sharelist();
22025a68471Sdougm 				if (item != NULL) {
22125a68471Sdougm 					/* if no path, then comment */
22225a68471Sdougm 					item->lineno = line;
22325a68471Sdougm 					item->description = strdup(buff);
22425a68471Sdougm 					if (first == NULL) {
22525a68471Sdougm 						first = item;
22625a68471Sdougm 						last = item;
22725a68471Sdougm 					} else {
22825a68471Sdougm 						last->next = item;
22925a68471Sdougm 						last = item;
23025a68471Sdougm 					}
23125a68471Sdougm 				} else {
23225a68471Sdougm 					break;
23325a68471Sdougm 				}
23425a68471Sdougm 				continue;
23525a68471Sdougm 			} else if (buff[0] == '\n') {
23625a68471Sdougm 				continue;
23725a68471Sdougm 			}
23825a68471Sdougm 			optind = 1;
23925a68471Sdougm 			item = alloc_sharelist();
24025a68471Sdougm 			if (item == NULL) {
24125a68471Sdougm 				break;
24225a68471Sdougm 			} else if (first == NULL) {
24325a68471Sdougm 				first = item;
24425a68471Sdougm 				last = item;
2456185db85Sdougm 			} else {
24625a68471Sdougm 				last->next = item;
24725a68471Sdougm 				last = item;
2486185db85Sdougm 			}
24925a68471Sdougm 			item->lineno = line;
25025a68471Sdougm 			item->origline = strdup(buff);
2518d7e4166Sjose borrego 			(void) _sa_get_token(NULL); /* reset to new pointers */
25225a68471Sdougm 			argc = 0;
2538d7e4166Sjose borrego 			while ((token = _sa_get_token(bp)) != NULL) {
25425a68471Sdougm 				if (argc < MAXARGSFORSHARE)
25525a68471Sdougm 					args[argc++] = token;
2566185db85Sdougm 			}
25725a68471Sdougm 			while ((c = getopt(argc, args, "F:o:d:pg:")) != -1) {
25825a68471Sdougm 				switch (c) {
25925a68471Sdougm 				case 'p':
26025a68471Sdougm 					item->persist = 1;
26125a68471Sdougm 					break;
26225a68471Sdougm 				case 'F':
26325a68471Sdougm 					item->fstype = strdup(optarg);
26425a68471Sdougm 					break;
26525a68471Sdougm 				case 'o':
26625a68471Sdougm 					item->options = strdup(optarg);
26725a68471Sdougm 					break;
26825a68471Sdougm 				case 'd':
26925a68471Sdougm 					item->description = strdup(optarg);
27025a68471Sdougm 					break;
27125a68471Sdougm 				case 'g':
27225a68471Sdougm 					item->group = strdup(optarg);
27325a68471Sdougm 					break;
27425a68471Sdougm 				default:
27525a68471Sdougm 					break;
27625a68471Sdougm 				}
27725a68471Sdougm 			}
27825a68471Sdougm 			if (optind < argc) {
27925a68471Sdougm 				item->path = strdup(args[optind]);
28025a68471Sdougm 				optind++;
28125a68471Sdougm 				if (optind < argc) {
28225a68471Sdougm 					char *resource;
28325a68471Sdougm 					char *optgroup;
28425a68471Sdougm 					/* resource and/or groupname */
28525a68471Sdougm 					resource = args[optind];
28625a68471Sdougm 					optgroup = strchr(resource, '@');
28725a68471Sdougm 					if (optgroup != NULL)
28825a68471Sdougm 						*optgroup++ = '\0';
28925a68471Sdougm 					if (optgroup != NULL)
29025a68471Sdougm 						item->group = strdup(optgroup);
29125a68471Sdougm 					if (resource != NULL &&
29225a68471Sdougm 					    strlen(resource) > 0)
29325a68471Sdougm 						item->resource =
29425a68471Sdougm 						    strdup(resource);
29525a68471Sdougm 				}
29625a68471Sdougm 			}
29725a68471Sdougm 			/* NFS is the default if none defined */
29825a68471Sdougm 			if (item != NULL && item->fstype == NULL)
29925a68471Sdougm 				item->fstype = strdup("nfs");
300a99982a7Sdougm 		}
3016185db85Sdougm 	}
3026185db85Sdougm 	first = fix_notice(first);
3036185db85Sdougm 	return (first);
3046185db85Sdougm }
3056185db85Sdougm 
3066185db85Sdougm /*
3076185db85Sdougm  * finddfsentry(list, path)
3086185db85Sdougm  *
3097d968cb8Sdougm  * Look for path in the zfs_sharelist_t list and return the entry if it
3106185db85Sdougm  * exists.
3116185db85Sdougm  */
3126185db85Sdougm 
3136185db85Sdougm static xfs_sharelist_t *
3146185db85Sdougm finddfsentry(xfs_sharelist_t *list, char *path)
3156185db85Sdougm {
3166185db85Sdougm 	xfs_sharelist_t *item;
3176185db85Sdougm 
3186185db85Sdougm 	for (item = list; item != NULL; item = item->next) {
31925a68471Sdougm 		if (item->path != NULL && strcmp(item->path, path) == 0)
3206185db85Sdougm 		return (item);
3216185db85Sdougm 	}
3226185db85Sdougm 	return (NULL);
3236185db85Sdougm }
3246185db85Sdougm 
3256185db85Sdougm /*
3266185db85Sdougm  * remdfsentry(list, path, proto)
3276185db85Sdougm  *
3286185db85Sdougm  * Remove the specified path (with protocol) from the list. This will
3296185db85Sdougm  * remove it from dfstab when the file is rewritten.
3306185db85Sdougm  */
3316185db85Sdougm 
3326185db85Sdougm static xfs_sharelist_t *
3336185db85Sdougm remdfsentry(xfs_sharelist_t *list, char *path, char *proto)
3346185db85Sdougm {
3356185db85Sdougm 	xfs_sharelist_t *item, *prev = NULL;
3366185db85Sdougm 
3376185db85Sdougm 
3386185db85Sdougm 	for (item = prev = list; item != NULL; item = item->next) {
3396185db85Sdougm 	    /* skip comment entry but don't lose it */
34025a68471Sdougm 		if (item->path == NULL) {
34125a68471Sdougm 			prev = item;
34225a68471Sdougm 			continue;
34325a68471Sdougm 		}
34425a68471Sdougm 		/* if proto is NULL, remove all protocols */
34525a68471Sdougm 		if (proto == NULL || (strcmp(item->path, path) == 0 &&
34625a68471Sdougm 		    (item->fstype != NULL && strcmp(item->fstype, proto) == 0)))
34725a68471Sdougm 			break;
34825a68471Sdougm 		if (item->fstype == NULL &&
34925a68471Sdougm 		    (proto == NULL || strcmp(proto, "nfs") == 0))
35025a68471Sdougm 			break;
3516185db85Sdougm 		prev = item;
3526185db85Sdougm 	}
3536185db85Sdougm 	if (item != NULL) {
35425a68471Sdougm 		if (item == prev)
35525a68471Sdougm 			list = item->next; /* this must be the first one */
35625a68471Sdougm 		else
35725a68471Sdougm 			prev->next = item->next;
35825a68471Sdougm 		item->next = NULL;
35925a68471Sdougm 		dfs_free_list(item);
3606185db85Sdougm 	}
3616185db85Sdougm 	return (list);
3626185db85Sdougm }
3636185db85Sdougm 
3646185db85Sdougm /*
3656185db85Sdougm  * remdfsline(list, line)
3666185db85Sdougm  *
3676185db85Sdougm  * Remove the line specified from the list.
3686185db85Sdougm  */
3696185db85Sdougm 
3706185db85Sdougm static xfs_sharelist_t *
3716185db85Sdougm remdfsline(xfs_sharelist_t *list, char *line)
3726185db85Sdougm {
3736185db85Sdougm 	xfs_sharelist_t *item, *prev = NULL;
3746185db85Sdougm 
3756185db85Sdougm 	for (item = prev = list; item != NULL; item = item->next) {
37625a68471Sdougm 		/* skip comment entry but don't lose it */
37725a68471Sdougm 		if (item->path == NULL) {
3786185db85Sdougm 		prev = item;
3796185db85Sdougm 		continue;
38025a68471Sdougm 		}
38125a68471Sdougm 		if (strcmp(item->origline, line) == 0)
38225a68471Sdougm 			break;
38325a68471Sdougm 		prev = item;
3846185db85Sdougm 	}
3856185db85Sdougm 	if (item != NULL) {
38625a68471Sdougm 		if (item == prev)
38725a68471Sdougm 			list = item->next; /* this must be the first one */
38825a68471Sdougm 		else
38925a68471Sdougm 			prev->next = item->next;
39025a68471Sdougm 		item->next = NULL;
39125a68471Sdougm 		dfs_free_list(item);
3926185db85Sdougm 	}
3936185db85Sdougm 	return (list);
3946185db85Sdougm }
3956185db85Sdougm 
3966185db85Sdougm /*
3976185db85Sdougm  * adddfsentry(list, share, proto)
3986185db85Sdougm  *
3996185db85Sdougm  * Add an entry to the dfstab list for share (relative to proto). This
4006185db85Sdougm  * is used to update dfstab for legacy purposes.
4016185db85Sdougm  */
4026185db85Sdougm 
4036185db85Sdougm static xfs_sharelist_t *
4046185db85Sdougm adddfsentry(xfs_sharelist_t *list, sa_share_t share, char *proto)
4056185db85Sdougm {
4066185db85Sdougm 	xfs_sharelist_t *item, *tmp;
4076185db85Sdougm 	sa_group_t parent;
4086185db85Sdougm 	char *groupname;
4096185db85Sdougm 
4106185db85Sdougm 	item = alloc_sharelist();
4116185db85Sdougm 	if (item != NULL) {
41225a68471Sdougm 		parent = sa_get_parent_group(share);
41325a68471Sdougm 		groupname = sa_get_group_attr(parent, "name");
414fe1c642dSBill Krier 		if (groupname != NULL && strcmp(groupname, "default") == 0) {
41525a68471Sdougm 			sa_free_attr_string(groupname);
41625a68471Sdougm 			groupname = NULL;
41725a68471Sdougm 		}
41825a68471Sdougm 		item->path = sa_get_share_attr(share, "path");
41925a68471Sdougm 		item->resource = sa_get_share_attr(share, "resource");
42025a68471Sdougm 		item->group = groupname;
42125a68471Sdougm 		item->fstype = strdup(proto);
42225a68471Sdougm 		item->options = sa_proto_legacy_format(proto, share, 1);
42325a68471Sdougm 		if (item->options != NULL && strlen(item->options) == 0) {
42425a68471Sdougm 			free(item->options);
42525a68471Sdougm 			item->options = NULL;
42625a68471Sdougm 		}
42725a68471Sdougm 		item->description = sa_get_share_description(share);
42825a68471Sdougm 		if (item->description != NULL &&
42925a68471Sdougm 		    strlen(item->description) == 0) {
43025a68471Sdougm 			sa_free_share_description(item->description);
43125a68471Sdougm 			item->description = NULL;
43225a68471Sdougm 		}
43325a68471Sdougm 		if (list == NULL) {
43425a68471Sdougm 			list = item;
43525a68471Sdougm 		} else {
43625a68471Sdougm 			for (tmp = list; tmp->next != NULL; tmp = tmp->next)
43725a68471Sdougm 				/* do nothing */;
43825a68471Sdougm 				tmp->next = item;
43925a68471Sdougm 		}
4406185db85Sdougm 	}
4416185db85Sdougm 	return (list);
4426185db85Sdougm }
4436185db85Sdougm 
4446185db85Sdougm /*
4456185db85Sdougm  * outdfstab(dfstab, list)
4466185db85Sdougm  *
4476185db85Sdougm  * Output the list to dfstab making sure the file is truncated.
4486185db85Sdougm  * Comments and errors are preserved.
4496185db85Sdougm  */
4506185db85Sdougm 
4516185db85Sdougm static void
4526185db85Sdougm outdfstab(FILE *dfstab, xfs_sharelist_t *list)
4536185db85Sdougm {
4546185db85Sdougm 	xfs_sharelist_t *item;
4556185db85Sdougm 
4566185db85Sdougm 	(void) ftruncate(fileno(dfstab), 0);
4576185db85Sdougm 
4586185db85Sdougm 	for (item = list; item != NULL; item = item->next) {
45925a68471Sdougm 		if (item->path != NULL) {
46025a68471Sdougm 			if (*item->path == '/') {
46125a68471Sdougm 				(void) fprintf(dfstab,
46225a68471Sdougm 				    "share %s%s%s%s%s%s%s %s%s%s%s%s\n",
46325a68471Sdougm 				    (item->fstype != NULL) ? "-F " : "",
46425a68471Sdougm 				    (item->fstype != NULL) ? item->fstype : "",
46525a68471Sdougm 				    (item->options != NULL) ? " -o " : "",
46625a68471Sdougm 				    (item->options != NULL) ?
46725a68471Sdougm 				    item->options : "",
46825a68471Sdougm 				    (item->description != NULL) ?
46925a68471Sdougm 				    " -d \"" : "",
47025a68471Sdougm 				    (item->description != NULL) ?
47125a68471Sdougm 				    item->description : "",
47225a68471Sdougm 				    (item->description != NULL) ? "\"" : "",
47325a68471Sdougm 				    item->path,
47425a68471Sdougm 				    ((item->resource != NULL) ||
47525a68471Sdougm 				    (item->group != NULL)) ? " " : "",
47625a68471Sdougm 				    (item->resource != NULL) ?
47725a68471Sdougm 				    item->resource : "",
47825a68471Sdougm 				    item->group != NULL ? "@" : "",
47925a68471Sdougm 				    item->group != NULL ? item->group : "");
48025a68471Sdougm 			} else {
48125a68471Sdougm 				(void) fprintf(dfstab, "%s", item->origline);
48225a68471Sdougm 			}
4836185db85Sdougm 		} else {
48425a68471Sdougm 			if (item->description != NULL)
48525a68471Sdougm 				(void) fprintf(dfstab, "%s", item->description);
48625a68471Sdougm 			else
48725a68471Sdougm 				(void) fprintf(dfstab, "%s", item->origline);
4886185db85Sdougm 		}
4896185db85Sdougm 	}
4906185db85Sdougm }
4916185db85Sdougm 
4926185db85Sdougm /*
4936185db85Sdougm  * open_dfstab(file)
4946185db85Sdougm  *
4956185db85Sdougm  * Open the specified dfstab file. If the owner/group/perms are wrong,
4966185db85Sdougm  * fix them.
4976185db85Sdougm  */
4986185db85Sdougm 
4996185db85Sdougm static FILE *
5006185db85Sdougm open_dfstab(char *file)
5016185db85Sdougm {
5026185db85Sdougm 	struct group *grp;
5036185db85Sdougm 	struct group group;
5046185db85Sdougm 	char *buff;
5056185db85Sdougm 	int grsize;
5066185db85Sdougm 	FILE *dfstab;
5076185db85Sdougm 
5086185db85Sdougm 	dfstab = fopen(file, "r+");
5096185db85Sdougm 	if (dfstab == NULL) {
51025a68471Sdougm 		dfstab = fopen(file, "w+");
5116185db85Sdougm 	}
5126185db85Sdougm 	if (dfstab != NULL) {
5136185db85Sdougm 		grsize = sysconf(_SC_GETGR_R_SIZE_MAX);
5146185db85Sdougm 		buff = malloc(grsize);
5156185db85Sdougm 		if (buff != NULL)
51625a68471Sdougm 			grp = getgrnam_r(SA_DEFAULT_FILE_GRP, &group, buff,
51725a68471Sdougm 			    grsize);
5186185db85Sdougm 		else
51925a68471Sdougm 			grp = getgrnam(SA_DEFAULT_FILE_GRP);
5206185db85Sdougm 		(void) fchmod(fileno(dfstab), 0644);
5216185db85Sdougm 		(void) fchown(fileno(dfstab), 0,
52225a68471Sdougm 		    grp != NULL ? grp->gr_gid : 3);
5236185db85Sdougm 		if (buff != NULL)
52425a68471Sdougm 			free(buff);
5256185db85Sdougm 		rewind(dfstab);
5266185db85Sdougm 	}
5276185db85Sdougm 	return (dfstab);
5286185db85Sdougm }
5296185db85Sdougm 
5306185db85Sdougm /*
5316185db85Sdougm  * sa_comment_line(line, err)
5326185db85Sdougm  *
5336185db85Sdougm  * Add a comment to the dfstab file with err as a prefix to the
5346185db85Sdougm  * original line.
5356185db85Sdougm  */
5366185db85Sdougm 
5376185db85Sdougm static void
5386185db85Sdougm sa_comment_line(char *line, char *err)
5396185db85Sdougm {
5406185db85Sdougm 	FILE *dfstab;
5416185db85Sdougm 	xfs_sharelist_t *list;
542a99982a7Sdougm 	sigset_t old;
5436185db85Sdougm 
5446185db85Sdougm 	dfstab = open_dfstab(SA_LEGACY_DFSTAB);
5456185db85Sdougm 	if (dfstab != NULL) {
5466185db85Sdougm 		(void) setvbuf(dfstab, NULL, _IOLBF, BUFSIZ * 8);
547a99982a7Sdougm 		sablocksigs(&old);
5486185db85Sdougm 		(void) lockf(fileno(dfstab), F_LOCK, 0);
549148c5f43SAlan Wright 		(void) mutex_lock(&sa_dfstab_lock);
5506185db85Sdougm 		list = getdfstab(dfstab);
5516185db85Sdougm 		rewind(dfstab);
552f345c0beSdougm 		/*
553f345c0beSdougm 		 * don't ignore the return since the list could have
554f345c0beSdougm 		 * gone to NULL if the file only had one line in it.
555f345c0beSdougm 		 */
556f345c0beSdougm 		list = remdfsline(list, line);
5576185db85Sdougm 		outdfstab(dfstab, list);
5586185db85Sdougm 		(void) fprintf(dfstab, "# Error: %s: %s", err, line);
5596185db85Sdougm 		(void) fsync(fileno(dfstab));
560148c5f43SAlan Wright 		(void) mutex_unlock(&sa_dfstab_lock);
5616185db85Sdougm 		(void) lockf(fileno(dfstab), F_ULOCK, 0);
5626185db85Sdougm 		(void) fclose(dfstab);
563a99982a7Sdougm 		saunblocksigs(&old);
5646185db85Sdougm 		if (list != NULL)
56525a68471Sdougm 			dfs_free_list(list);
5666185db85Sdougm 	}
5676185db85Sdougm }
5686185db85Sdougm 
5696185db85Sdougm /*
570da6c28aaSamw  * sa_delete_legacy(share, protocol)
5716185db85Sdougm  *
5726185db85Sdougm  * Delete the specified share from the legacy config file.
5736185db85Sdougm  */
5746185db85Sdougm 
5756185db85Sdougm int
576da6c28aaSamw sa_delete_legacy(sa_share_t share, char *protocol)
5776185db85Sdougm {
5786185db85Sdougm 	FILE *dfstab;
5796185db85Sdougm 	int err;
5806185db85Sdougm 	int ret = SA_OK;
5816185db85Sdougm 	xfs_sharelist_t *list;
5826185db85Sdougm 	char *path;
5836185db85Sdougm 	sa_optionset_t optionset;
5846185db85Sdougm 	sa_group_t parent;
585a99982a7Sdougm 	sigset_t old;
5866185db85Sdougm 
587da6c28aaSamw 	/*
588da6c28aaSamw 	 * Protect against shares that don't have paths. This is not
589da6c28aaSamw 	 * really an error at this point.
590da6c28aaSamw 	 */
591da6c28aaSamw 	path = sa_get_share_attr(share, "path");
592da6c28aaSamw 	if (path == NULL)
593da6c28aaSamw 		return (ret);
594da6c28aaSamw 
5956185db85Sdougm 	dfstab = open_dfstab(SA_LEGACY_DFSTAB);
5966185db85Sdougm 	if (dfstab != NULL) {
5976185db85Sdougm 		(void) setvbuf(dfstab, NULL, _IOLBF, BUFSIZ * 8);
598a99982a7Sdougm 		sablocksigs(&old);
5996185db85Sdougm 		parent = sa_get_parent_group(share);
6006185db85Sdougm 		if (parent != NULL) {
60125a68471Sdougm 			(void) lockf(fileno(dfstab), F_LOCK, 0);
602148c5f43SAlan Wright 			(void) mutex_lock(&sa_dfstab_lock);
60325a68471Sdougm 			list = getdfstab(dfstab);
60425a68471Sdougm 			rewind(dfstab);
605da6c28aaSamw 			if (protocol != NULL) {
606da6c28aaSamw 				if (list != NULL)
60725a68471Sdougm 					list = remdfsentry(list, path,
608da6c28aaSamw 					    protocol);
609da6c28aaSamw 			} else {
610da6c28aaSamw 				for (optionset = sa_get_optionset(parent, NULL);
611da6c28aaSamw 				    optionset != NULL;
612da6c28aaSamw 				    optionset =
613da6c28aaSamw 				    sa_get_next_optionset(optionset)) {
614da6c28aaSamw 					char *proto = sa_get_optionset_attr(
615da6c28aaSamw 					    optionset, "type");
616da6c28aaSamw 
617da6c28aaSamw 					if (list != NULL && proto != NULL)
618da6c28aaSamw 						list = remdfsentry(list, path,
619da6c28aaSamw 						    proto);
620da6c28aaSamw 					if (proto == NULL)
621da6c28aaSamw 						ret = SA_NO_MEMORY;
622da6c28aaSamw 					/*
623da6c28aaSamw 					 * may want to only do the dfstab if
624da6c28aaSamw 					 * this call returns NOT IMPLEMENTED
625da6c28aaSamw 					 * but it shouldn't hurt.
626da6c28aaSamw 					 */
627da6c28aaSamw 					if (ret == SA_OK) {
628da6c28aaSamw 						err = sa_proto_delete_legacy(
629da6c28aaSamw 						    proto, share);
630da6c28aaSamw 						if (err != SA_NOT_IMPLEMENTED)
631da6c28aaSamw 							ret = err;
632da6c28aaSamw 					}
633da6c28aaSamw 					if (proto != NULL)
634da6c28aaSamw 						sa_free_attr_string(proto);
63525a68471Sdougm 				}
6366185db85Sdougm 			}
63725a68471Sdougm 			outdfstab(dfstab, list);
63825a68471Sdougm 			if (list != NULL)
63925a68471Sdougm 				dfs_free_list(list);
64025a68471Sdougm 			(void) fflush(dfstab);
641148c5f43SAlan Wright 			(void) mutex_unlock(&sa_dfstab_lock);
64225a68471Sdougm 			(void) lockf(fileno(dfstab), F_ULOCK, 0);
6436185db85Sdougm 		}
6446185db85Sdougm 		(void) fsync(fileno(dfstab));
645a99982a7Sdougm 		saunblocksigs(&old);
6466185db85Sdougm 		(void) fclose(dfstab);
6476185db85Sdougm 	} else {
64825a68471Sdougm 		if (errno == EACCES || errno == EPERM)
64925a68471Sdougm 			ret = SA_NO_PERMISSION;
65025a68471Sdougm 		else
65125a68471Sdougm 			ret = SA_CONFIG_ERR;
6526185db85Sdougm 	}
653da6c28aaSamw 
654da6c28aaSamw 	if (path != NULL)
655da6c28aaSamw 		sa_free_attr_string(path);
656da6c28aaSamw 
6576185db85Sdougm 	return (ret);
6586185db85Sdougm }
6596185db85Sdougm 
6606185db85Sdougm /*
6616185db85Sdougm  * sa_update_legacy(share, proto)
6626185db85Sdougm  *
6636185db85Sdougm  * There is an assumption that dfstab will be the most common form of
6646185db85Sdougm  * legacy configuration file for shares, but not the only one. Because
6656185db85Sdougm  * of that, dfstab handling is done in the main code with calls to
666da6c28aaSamw  * this function and protocol specific calls to deal with formatting
6676185db85Sdougm  * options into dfstab/share compatible syntax. Since not everything
6686185db85Sdougm  * will be dfstab, there is a provision for calling a protocol
6696185db85Sdougm  * specific plugin interface that allows the protocol plugin to do its
6706185db85Sdougm  * own legacy files and skip the dfstab update.
6716185db85Sdougm  */
6726185db85Sdougm 
6736185db85Sdougm int
6746185db85Sdougm sa_update_legacy(sa_share_t share, char *proto)
6756185db85Sdougm {
6766185db85Sdougm 	FILE *dfstab;
6776185db85Sdougm 	int ret = SA_OK;
6786185db85Sdougm 	xfs_sharelist_t *list;
6796185db85Sdougm 	char *path;
680a99982a7Sdougm 	sigset_t old;
6816185db85Sdougm 	char *persist;
682da6c28aaSamw 	uint64_t features;
6836185db85Sdougm 
6846185db85Sdougm 	ret = sa_proto_update_legacy(proto, share);
6856185db85Sdougm 	if (ret != SA_NOT_IMPLEMENTED)
68625a68471Sdougm 		return (ret);
687da6c28aaSamw 
688da6c28aaSamw 	features = sa_proto_get_featureset(proto);
689da6c28aaSamw 	if (!(features & SA_FEATURE_DFSTAB))
690da6c28aaSamw 		return (ret);
691da6c28aaSamw 
6926185db85Sdougm 	/* do the dfstab format */
6936185db85Sdougm 	persist = sa_get_share_attr(share, "type");
6946185db85Sdougm 	/*
6956185db85Sdougm 	 * only update if the share is not transient -- no share type
6966185db85Sdougm 	 * set or the type is not "transient".
6976185db85Sdougm 	 */
6986185db85Sdougm 	if (persist == NULL || strcmp(persist, "transient") != 0) {
699fe1c642dSBill Krier 		path = sa_get_share_attr(share, "path");
700fe1c642dSBill Krier 		if (path == NULL) {
701fe1c642dSBill Krier 			ret = SA_NO_MEMORY;
702fe1c642dSBill Krier 			goto out;
703fe1c642dSBill Krier 		}
70425a68471Sdougm 		dfstab = open_dfstab(SA_LEGACY_DFSTAB);
70525a68471Sdougm 		if (dfstab != NULL) {
70625a68471Sdougm 			(void) setvbuf(dfstab, NULL, _IOLBF, BUFSIZ * 8);
70725a68471Sdougm 			sablocksigs(&old);
70825a68471Sdougm 			(void) lockf(fileno(dfstab), F_LOCK, 0);
709148c5f43SAlan Wright 			(void) mutex_lock(&sa_dfstab_lock);
71025a68471Sdougm 			list = getdfstab(dfstab);
71125a68471Sdougm 			rewind(dfstab);
71225a68471Sdougm 			if (list != NULL)
71325a68471Sdougm 				list = remdfsentry(list, path, proto);
71425a68471Sdougm 			list = adddfsentry(list, share, proto);
71525a68471Sdougm 			outdfstab(dfstab, list);
71625a68471Sdougm 			(void) fflush(dfstab);
717148c5f43SAlan Wright 			(void) mutex_unlock(&sa_dfstab_lock);
71825a68471Sdougm 			(void) lockf(fileno(dfstab), F_ULOCK, 0);
71925a68471Sdougm 			(void) fsync(fileno(dfstab));
72025a68471Sdougm 			saunblocksigs(&old);
72125a68471Sdougm 			(void) fclose(dfstab);
72225a68471Sdougm 			if (list != NULL)
72325a68471Sdougm 				dfs_free_list(list);
7246185db85Sdougm 		} else {
72525a68471Sdougm 			if (errno == EACCES || errno == EPERM)
72625a68471Sdougm 				ret = SA_NO_PERMISSION;
72725a68471Sdougm 			else
72825a68471Sdougm 				ret = SA_CONFIG_ERR;
7296185db85Sdougm 		}
730fe1c642dSBill Krier 		sa_free_attr_string(path);
7316185db85Sdougm 	}
732fe1c642dSBill Krier out:
7336185db85Sdougm 	if (persist != NULL)
73425a68471Sdougm 		sa_free_attr_string(persist);
7356185db85Sdougm 	return (ret);
7366185db85Sdougm }
7376185db85Sdougm 
7386185db85Sdougm /*
7396185db85Sdougm  * sa_is_security(optname, proto)
7406185db85Sdougm  *
7416185db85Sdougm  * Check to see if optname is a security (named optionset) specific
7426185db85Sdougm  * property for the specified protocol.
7436185db85Sdougm  */
7446185db85Sdougm 
7456185db85Sdougm int
7466185db85Sdougm sa_is_security(char *optname, char *proto)
7476185db85Sdougm {
7486185db85Sdougm 	int ret = 0;
7496185db85Sdougm 	if (proto != NULL)
75025a68471Sdougm 		ret = sa_proto_security_prop(proto, optname);
7516185db85Sdougm 	return (ret);
7526185db85Sdougm }
7536185db85Sdougm 
7546185db85Sdougm /*
7556185db85Sdougm  * add_syntax_comment(root, line, err, todfstab)
7566185db85Sdougm  *
75725a68471Sdougm  * Add a comment to the document indicating a syntax error. If
7586185db85Sdougm  * todfstab is set, write it back to the dfstab file as well.
7596185db85Sdougm  */
7606185db85Sdougm 
7616185db85Sdougm static void
7626185db85Sdougm add_syntax_comment(xmlNodePtr root, char *line, char *err, int todfstab)
7636185db85Sdougm {
7646185db85Sdougm 	xmlNodePtr node;
7656185db85Sdougm 
7666185db85Sdougm 	node = xmlNewChild(root, NULL, (xmlChar *)"error", (xmlChar *)line);
76725a68471Sdougm 	if (node != NULL)
7687a9d7716Sthurlow 		(void) xmlSetProp(node, (xmlChar *)"type", (xmlChar *)err);
7696185db85Sdougm 	if (todfstab)
77025a68471Sdougm 		sa_comment_line(line, err);
7716185db85Sdougm }
7726185db85Sdougm 
7736185db85Sdougm /*
7746185db85Sdougm  * sa_is_share(object)
7756185db85Sdougm  *
776*5cb0d679SMarcel Telka  * returns true if the object is of type "share".
7776185db85Sdougm  */
7786185db85Sdougm 
7796185db85Sdougm int
7806185db85Sdougm sa_is_share(void *object)
7816185db85Sdougm {
7826185db85Sdougm 	if (object != NULL) {
78325a68471Sdougm 		if (strcmp((char *)((xmlNodePtr)object)->name, "share") == 0)
7846185db85Sdougm 		return (1);
7856185db85Sdougm 	}
7866185db85Sdougm 	return (0);
7876185db85Sdougm }
788da6c28aaSamw /*
789da6c28aaSamw  * sa_is_resource(object)
790da6c28aaSamw  *
791*5cb0d679SMarcel Telka  * returns true if the object is of type "resource".
792da6c28aaSamw  */
793da6c28aaSamw 
794da6c28aaSamw int
795da6c28aaSamw sa_is_resource(void *object)
796da6c28aaSamw {
797da6c28aaSamw 	if (object != NULL) {
798da6c28aaSamw 		if (strcmp((char *)((xmlNodePtr)object)->name, "resource") == 0)
799da6c28aaSamw 			return (1);
800da6c28aaSamw 	}
801da6c28aaSamw 	return (0);
802da6c28aaSamw }
8036185db85Sdougm 
8046185db85Sdougm /*
8056185db85Sdougm  * _sa_remove_property(property)
8066185db85Sdougm  *
8076185db85Sdougm  * remove a property only from the document.
8086185db85Sdougm  */
8096185db85Sdougm 
8106185db85Sdougm static void
8116185db85Sdougm _sa_remove_property(sa_property_t property)
8126185db85Sdougm {
8136185db85Sdougm 	xmlUnlinkNode((xmlNodePtr)property);
8146185db85Sdougm 	xmlFreeNode((xmlNodePtr)property);
8156185db85Sdougm }
8166185db85Sdougm 
81767331909Sdougm /*
81867331909Sdougm  * _sa_create_dummy_share()
81967331909Sdougm  *
82067331909Sdougm  * Create a share entry suitable for parsing but not tied to any real
82167331909Sdougm  * config tree.  Need to have a parent as well as the node to parse
82267331909Sdougm  * on.  Free using _sa_free_dummy_share(share);
82367331909Sdougm  */
82467331909Sdougm 
82567331909Sdougm static sa_group_t
82667331909Sdougm _sa_create_dummy_share()
82767331909Sdougm {
82867331909Sdougm 	xmlNodePtr parent_node = NULL;
82967331909Sdougm 	xmlNodePtr child_node = NULL;
83067331909Sdougm 
83167331909Sdougm 	parent_node = xmlNewNode(NULL, (xmlChar *)"group");
83267331909Sdougm 	if (parent_node != NULL) {
83325a68471Sdougm 		child_node = xmlNewChild(parent_node, NULL, (xmlChar *)"share",
83425a68471Sdougm 		    NULL);
83525a68471Sdougm 		if (child_node != NULL) {
83625a68471Sdougm 			/*
83725a68471Sdougm 			 * Use a "zfs" tag since that will make sure nothing
83825a68471Sdougm 			 * really attempts to put values into the
83925a68471Sdougm 			 * repository. Also ZFS is currently the only user of
84025a68471Sdougm 			 * this interface.
84125a68471Sdougm 			 */
84225a68471Sdougm 			set_node_attr(parent_node, "type", "transient");
84325a68471Sdougm 			set_node_attr(parent_node, "zfs", "true");
84425a68471Sdougm 			set_node_attr(child_node, "type", "transient");
84525a68471Sdougm 			set_node_attr(child_node, "zfs", "true");
84625a68471Sdougm 		} else {
84725a68471Sdougm 			xmlFreeNode(parent_node);
84825a68471Sdougm 		}
84967331909Sdougm 	}
85067331909Sdougm 	return (child_node);
85167331909Sdougm }
85267331909Sdougm 
85367331909Sdougm /*
85467331909Sdougm  * _sa_free_dummy_share(share)
85567331909Sdougm  *
85667331909Sdougm  * Free the dummy share and its parent.  It is an error to try and
85767331909Sdougm  * free something that isn't a dummy.
85867331909Sdougm  */
85967331909Sdougm 
86067331909Sdougm static int
86167331909Sdougm _sa_free_dummy_share(sa_share_t share)
86267331909Sdougm {
86367331909Sdougm 	xmlNodePtr node = (xmlNodePtr)share;
86467331909Sdougm 	xmlNodePtr parent;
86567331909Sdougm 	int ret = SA_OK;
86667331909Sdougm 	char *name;
86767331909Sdougm 
86867331909Sdougm 	if (node != NULL) {
86925a68471Sdougm 		parent = node->parent;
87025a68471Sdougm 		name = (char *)xmlGetProp(node, (xmlChar *)"path");
87125a68471Sdougm 		if (name != NULL) {
87225a68471Sdougm 			/* Real shares always have a path but a dummy doesn't */
87325a68471Sdougm 			ret = SA_NOT_ALLOWED;
87425a68471Sdougm 			sa_free_attr_string(name);
87525a68471Sdougm 		} else {
87625a68471Sdougm 			/*
87725a68471Sdougm 			 * If there is a parent, do the free on that since
87825a68471Sdougm 			 * xmlFreeNode is a recursive function and free's an
87925a68471Sdougm 			 * child nodes.
88025a68471Sdougm 			 */
88125a68471Sdougm 			if (parent != NULL) {
88225a68471Sdougm 				node = parent;
88325a68471Sdougm 			}
88425a68471Sdougm 			xmlUnlinkNode(node);
88525a68471Sdougm 			xmlFreeNode(node);
88667331909Sdougm 		}
88767331909Sdougm 	}
88867331909Sdougm 	return (ret);
88967331909Sdougm }
89067331909Sdougm 
89167331909Sdougm 
8926185db85Sdougm /*
8936185db85Sdougm  * sa_parse_legacy_options(group, options, proto)
8946185db85Sdougm  *
8956185db85Sdougm  * In order to support legacy configurations, we allow the protocol
8966185db85Sdougm  * specific plugin to parse legacy syntax options (like those in
8976185db85Sdougm  * /etc/dfs/dfstab). This adds a new optionset to the group (or
8986185db85Sdougm  * share).
8996185db85Sdougm  *
9006185db85Sdougm  * Once the optionset has been created, we then get the derived
9016185db85Sdougm  * optionset of the parent (options from the optionset of the parent
9026185db85Sdougm  * and any parent it might have) and remove those from the created
9036185db85Sdougm  * optionset. This avoids duplication of options.
9046185db85Sdougm  */
9056185db85Sdougm 
9066185db85Sdougm int
9076185db85Sdougm sa_parse_legacy_options(sa_group_t group, char *options, char *proto)
9086185db85Sdougm {
9096185db85Sdougm 	int ret = SA_INVALID_PROTOCOL;
9106185db85Sdougm 	sa_group_t parent;
91167331909Sdougm 	int using_dummy = B_FALSE;
912717a41ebSdougm 	char *pvalue;
91325a68471Sdougm 	sa_optionset_t optionset;
91425a68471Sdougm 	sa_property_t popt, prop;
91525a68471Sdougm 	sa_optionset_t localoptions;
91667331909Sdougm 
91767331909Sdougm 	/*
91825a68471Sdougm 	 * If "group" is NULL, this is just a parse without saving
91967331909Sdougm 	 * anything in either SMF or ZFS.  Create a dummy group to
92067331909Sdougm 	 * handle this case.
92167331909Sdougm 	 */
92267331909Sdougm 	if (group == NULL) {
92325a68471Sdougm 		group = (sa_group_t)_sa_create_dummy_share();
92425a68471Sdougm 		using_dummy = B_TRUE;
92567331909Sdougm 	}
92667331909Sdougm 
9276185db85Sdougm 	parent = sa_get_parent_group(group);
9286185db85Sdougm 
9296185db85Sdougm 	if (proto != NULL)
93025a68471Sdougm 		ret = sa_proto_legacy_opts(proto, group, options);
93167331909Sdougm 
93267331909Sdougm 	if (using_dummy) {
93325a68471Sdougm 		/* Since this is a dummy parse, cleanup and quit here */
93425a68471Sdougm 		(void) _sa_free_dummy_share(parent);
93525a68471Sdougm 		return (ret);
93667331909Sdougm 	}
93725a68471Sdougm 
93825a68471Sdougm 	if (ret != SA_OK)
93925a68471Sdougm 		return (ret);
94025a68471Sdougm 
9416185db85Sdougm 	/*
94225a68471Sdougm 	 * If in a group, remove the inherited options and security
9436185db85Sdougm 	 */
94425a68471Sdougm 
94525a68471Sdougm 	if (parent == NULL)
94625a68471Sdougm 		return (ret);
94725a68471Sdougm 
94825a68471Sdougm 	/* Find parent options to remove from child */
94925a68471Sdougm 	optionset = sa_get_derived_optionset(parent, proto, 1);
95025a68471Sdougm 	localoptions = sa_get_optionset(group, proto);
95125a68471Sdougm 	if (optionset != NULL) {
95225a68471Sdougm 		for (popt = sa_get_property(optionset, NULL);
95325a68471Sdougm 		    popt != NULL;
95425a68471Sdougm 		    popt = sa_get_next_property(popt)) {
9556185db85Sdougm 			char *tag;
956717a41ebSdougm 			char *value;
9576185db85Sdougm 			tag = sa_get_property_attr(popt, "type");
95825a68471Sdougm 			if (tag == NULL)
95925a68471Sdougm 				continue;
96025a68471Sdougm 			prop = sa_get_property(localoptions, tag);
96125a68471Sdougm 			if (prop != NULL) {
96225a68471Sdougm 				value = sa_get_property_attr(popt,
96325a68471Sdougm 				    "value");
96425a68471Sdougm 				pvalue = sa_get_property_attr(prop,
96525a68471Sdougm 				    "value");
966717a41ebSdougm 				if (value != NULL && pvalue != NULL &&
967717a41ebSdougm 				    strcmp(value, pvalue) == 0) {
968717a41ebSdougm 					/*
96925a68471Sdougm 					 * Remove the property
97025a68471Sdougm 					 * from the
97125a68471Sdougm 					 * child. While we
97225a68471Sdougm 					 * removed it, we
97325a68471Sdougm 					 * don't need to reset
97425a68471Sdougm 					 * as we do below
97525a68471Sdougm 					 * since we always
97625a68471Sdougm 					 * search from the
97725a68471Sdougm 					 * beginning.
978717a41ebSdougm 					 */
97925a68471Sdougm 					(void) _sa_remove_property(
98025a68471Sdougm 					    prop);
9816185db85Sdougm 				}
982717a41ebSdougm 				if (value != NULL)
98325a68471Sdougm 					sa_free_attr_string(value);
984717a41ebSdougm 				if (pvalue != NULL)
98525a68471Sdougm 					sa_free_attr_string(pvalue);
9866185db85Sdougm 			}
98725a68471Sdougm 			sa_free_attr_string(tag);
98825a68471Sdougm 		}
98925a68471Sdougm 		prop = sa_get_property(localoptions, NULL);
99025a68471Sdougm 		if (prop == NULL && sa_is_share(group)) {
9916185db85Sdougm 			/*
99225a68471Sdougm 			 * All properties removed so remove the
9936185db85Sdougm 			 * optionset if it is on a share
9946185db85Sdougm 			 */
9956185db85Sdougm 			(void) _sa_remove_optionset(localoptions);
9966185db85Sdougm 		}
99725a68471Sdougm 		sa_free_derived_optionset(optionset);
99825a68471Sdougm 	}
99925a68471Sdougm 	/*
100025a68471Sdougm 	 * Need to remove security here. If there are no
100125a68471Sdougm 	 * security options on the local group/share, don't
100225a68471Sdougm 	 * bother since those are the only ones that would be
100325a68471Sdougm 	 * affected.
100425a68471Sdougm 	 */
100525a68471Sdougm 	localoptions = sa_get_all_security_types(group, proto, 0);
100625a68471Sdougm 	if (localoptions != NULL) {
100725a68471Sdougm 		for (prop = sa_get_property(localoptions, NULL);
100825a68471Sdougm 		    prop != NULL;
100925a68471Sdougm 		    prop = sa_get_next_property(prop)) {
10106185db85Sdougm 			char *tag;
10116185db85Sdougm 			sa_security_t security;
10126185db85Sdougm 			tag = sa_get_property_attr(prop, "type");
10136185db85Sdougm 			if (tag != NULL) {
101425a68471Sdougm 				sa_property_t nextpopt = NULL;
101525a68471Sdougm 				security = sa_get_security(group, tag, proto);
101625a68471Sdougm 				sa_free_attr_string(tag);
1017717a41ebSdougm 				/*
101825a68471Sdougm 				 * prop's value only changes outside this loop
1019717a41ebSdougm 				 */
102025a68471Sdougm 				pvalue = sa_get_property_attr(prop, "value");
102125a68471Sdougm 				for (popt = sa_get_property(security, NULL);
102225a68471Sdougm 				    popt != NULL;
102325a68471Sdougm 				    popt = nextpopt) {
102425a68471Sdougm 					char *value;
102525a68471Sdougm 					/*
102625a68471Sdougm 					 * Need to get the next prop
102725a68471Sdougm 					 * now since we could break
102825a68471Sdougm 					 * the list during removal.
102925a68471Sdougm 					 */
103025a68471Sdougm 					nextpopt = sa_get_next_property(popt);
103125a68471Sdougm 					/* remove Duplicates from this level */
103225a68471Sdougm 					value = sa_get_property_attr(popt,
103325a68471Sdougm 					    "value");
103425a68471Sdougm 					if (value != NULL && pvalue != NULL &&
103525a68471Sdougm 					    strcmp(value, pvalue) == 0) {
103625a68471Sdougm 						/*
103725a68471Sdougm 						 * remove the property
103825a68471Sdougm 						 * from the child
103925a68471Sdougm 						 */
104025a68471Sdougm 						(void) _sa_remove_property
104125a68471Sdougm 						    (popt);
104225a68471Sdougm 					}
104325a68471Sdougm 					if (value != NULL)
104425a68471Sdougm 						sa_free_attr_string(value);
10456185db85Sdougm 				}
104625a68471Sdougm 				if (pvalue != NULL)
104725a68471Sdougm 					sa_free_attr_string(pvalue);
10486185db85Sdougm 			}
10496185db85Sdougm 		}
105025a68471Sdougm 		(void) sa_destroy_optionset(localoptions);
10516185db85Sdougm 	}
10526185db85Sdougm 	return (ret);
10536185db85Sdougm }
10546185db85Sdougm 
10556185db85Sdougm /*
10566185db85Sdougm  * dfs_free_list(list)
10576185db85Sdougm  *
10586185db85Sdougm  * Free the data in each list entry of the list as well as freeing the
10596185db85Sdougm  * entries themselves. We need to avoid memory leaks and don't want to
10606185db85Sdougm  * dereference any NULL members.
10616185db85Sdougm  */
10626185db85Sdougm 
10636185db85Sdougm static void
10646185db85Sdougm dfs_free_list(xfs_sharelist_t *list)
10656185db85Sdougm {
10666185db85Sdougm 	xfs_sharelist_t *entry;
10676185db85Sdougm 	for (entry = list; entry != NULL; entry = list) {
106825a68471Sdougm 		if (entry->path != NULL)
106925a68471Sdougm 			free(entry->path);
107025a68471Sdougm 		if (entry->resource != NULL)
107125a68471Sdougm 			free(entry->resource);
107225a68471Sdougm 		if (entry->fstype != NULL)
107325a68471Sdougm 			free(entry->fstype);
107425a68471Sdougm 		if (entry->options != NULL)
107525a68471Sdougm 			free(entry->options);
107625a68471Sdougm 		if (entry->description != NULL)
107725a68471Sdougm 			free(entry->description);
107825a68471Sdougm 		if (entry->origline != NULL)
107925a68471Sdougm 			free(entry->origline);
108025a68471Sdougm 		if (entry->group != NULL)
108125a68471Sdougm 			free(entry->group);
108225a68471Sdougm 		list = list->next;
108325a68471Sdougm 			free(entry);
10846185db85Sdougm 	}
10856185db85Sdougm }
10866185db85Sdougm 
10876185db85Sdougm /*
10886185db85Sdougm  * parse_dfstab(dfstab, root)
10896185db85Sdougm  *
10906185db85Sdougm  * Open and read the existing dfstab, parsing each line and adding it
10916185db85Sdougm  * to the internal configuration. Make sure syntax errors, etc are
10926185db85Sdougm  * preserved as comments.
10936185db85Sdougm  */
10946185db85Sdougm 
10956185db85Sdougm static void
1096549ec3ffSdougm parse_dfstab(sa_handle_t handle, char *dfstab, xmlNodePtr root)
10976185db85Sdougm {
10986185db85Sdougm 	sa_share_t share;
10996185db85Sdougm 	sa_group_t group;
11006185db85Sdougm 	sa_group_t sgroup = NULL;
11016185db85Sdougm 	sa_group_t defgroup;
11026185db85Sdougm 	xfs_sharelist_t *head, *list;
11036185db85Sdougm 	int err;
11046185db85Sdougm 	int defined_group;
11056185db85Sdougm 	FILE *dfs;
11066185db85Sdougm 	char *oldprops;
11076185db85Sdougm 
11086185db85Sdougm 	/* read the dfstab format file and fill in the doc tree */
11096185db85Sdougm 
11106185db85Sdougm 	dfs = fopen(dfstab, "r");
111125a68471Sdougm 	if (dfs == NULL)
111225a68471Sdougm 		return;
11136185db85Sdougm 
1114549ec3ffSdougm 	defgroup = sa_get_group(handle, "default");
11156185db85Sdougm 
11166185db85Sdougm 	for (head = list = getdfstab(dfs);
111725a68471Sdougm 	    list != NULL;
111825a68471Sdougm 	    list = list->next) {
111925a68471Sdougm 		share = NULL;
112025a68471Sdougm 		group = NULL;
112125a68471Sdougm 		defined_group = 0;
112225a68471Sdougm 		err = 0;
112325a68471Sdougm 
112425a68471Sdougm 		if (list->origline == NULL) {
112525a68471Sdougm 			/*
112625a68471Sdougm 			 * Comment line that we will likely skip.
112725a68471Sdougm 			 * If the line has the syntax:
112825a68471Sdougm 			 *	# error: string: string
112925a68471Sdougm 			 * It should be preserved until manually deleted.
113025a68471Sdougm 			 */
113125a68471Sdougm 			if (list->description != NULL &&
113225a68471Sdougm 			    strncmp(list->description, "# Error: ", 9) == 0) {
113325a68471Sdougm 				char *line;
113425a68471Sdougm 				char *error;
113525a68471Sdougm 				char *cmd;
113625a68471Sdougm 				line = strdup(list->description);
113725a68471Sdougm 				if (line != NULL) {
113825a68471Sdougm 					error = line + 9;
113925a68471Sdougm 					cmd = strchr(error, ':');
114025a68471Sdougm 					if (cmd != NULL) {
114125a68471Sdougm 						int len;
114225a68471Sdougm 						*cmd = '\0';
114325a68471Sdougm 						cmd += 2;
114425a68471Sdougm 						len = strlen(cmd);
114525a68471Sdougm 						cmd[len - 1] = '\0';
114625a68471Sdougm 						add_syntax_comment(root, cmd,
114725a68471Sdougm 						    error, 0);
114825a68471Sdougm 					}
114925a68471Sdougm 					free(line);
115025a68471Sdougm 				}
11516185db85Sdougm 			}
115225a68471Sdougm 			continue;
11536185db85Sdougm 		}
115425a68471Sdougm 		if (list->path != NULL && strlen(list->path) > 0 &&
1155549ec3ffSdougm 		    *list->path == '/') {
115625a68471Sdougm 			share = sa_find_share(handle, list->path);
115725a68471Sdougm 			if (share != NULL)
115825a68471Sdougm 				sgroup = sa_get_parent_group(share);
115925a68471Sdougm 			else
116025a68471Sdougm 				sgroup = NULL;
116125a68471Sdougm 		} else {
116225a68471Sdougm 			(void) printf(dgettext(TEXT_DOMAIN,
116325a68471Sdougm 			    "No share specified in dfstab: "
116425a68471Sdougm 			    "line %d: %s\n"),
116525a68471Sdougm 			    list->lineno, list->origline);
116625a68471Sdougm 			add_syntax_comment(root, list->origline,
116725a68471Sdougm 			    dgettext(TEXT_DOMAIN, "No share specified"), 1);
116825a68471Sdougm 			continue;
116925a68471Sdougm 		}
117025a68471Sdougm 		if (list->group != NULL && strlen(list->group) > 0) {
117125a68471Sdougm 			group = sa_get_group(handle, list->group);
117225a68471Sdougm 			defined_group = 1;
117325a68471Sdougm 		} else {
117425a68471Sdougm 			group = defgroup;
117525a68471Sdougm 		}
117625a68471Sdougm 		if (defined_group && group == NULL) {
117725a68471Sdougm 			(void) printf(dgettext(TEXT_DOMAIN,
117825a68471Sdougm 			    "Unknown group used in dfstab: line %d: %s\n"),
117925a68471Sdougm 			    list->lineno, list->origline);
118025a68471Sdougm 			add_syntax_comment(root, list->origline,
118125a68471Sdougm 			    dgettext(TEXT_DOMAIN, "Unknown group specified"),
118225a68471Sdougm 			    1);
118325a68471Sdougm 			continue;
118425a68471Sdougm 		}
118525a68471Sdougm 		if (group == NULL) {
118625a68471Sdougm 			/* Shouldn't happen unless an SMF error */
118725a68471Sdougm 			err = SA_CONFIG_ERR;
118825a68471Sdougm 			continue;
118925a68471Sdougm 		}
11906185db85Sdougm 		if (share == NULL) {
119125a68471Sdougm 			if (defined_group || group != defgroup)
119225a68471Sdougm 				continue;
119325a68471Sdougm 			/* This is an OK add for legacy */
11946185db85Sdougm 			share = sa_add_share(defgroup, list->path,
119525a68471Sdougm 			    SA_SHARE_PERMANENT | SA_SHARE_PARSER, &err);
11966185db85Sdougm 			if (share != NULL) {
119725a68471Sdougm 				if (list->description != NULL &&
119825a68471Sdougm 				    strlen(list->description) > 0)
119925a68471Sdougm 					(void) sa_set_share_description(share,
120025a68471Sdougm 					    list->description);
120125a68471Sdougm 				if (list->options != NULL &&
120225a68471Sdougm 				    strlen(list->options) > 0) {
120325a68471Sdougm 					(void) sa_parse_legacy_options(share,
120425a68471Sdougm 					    list->options, list->fstype);
120525a68471Sdougm 				}
120625a68471Sdougm 				if (list->resource != NULL)
120725a68471Sdougm 					(void) sa_set_share_attr(share,
120825a68471Sdougm 					    "resource", list->resource);
12096185db85Sdougm 			} else {
121025a68471Sdougm 				(void) printf(dgettext(TEXT_DOMAIN,
121125a68471Sdougm 				    "Error in dfstab: line %d: %s\n"),
12126185db85Sdougm 				    list->lineno, list->origline);
121325a68471Sdougm 				if (err != SA_BAD_PATH)
121425a68471Sdougm 					add_syntax_comment(root, list->origline,
121525a68471Sdougm 					    dgettext(TEXT_DOMAIN, "Syntax"), 1);
121625a68471Sdougm 				else
121725a68471Sdougm 					add_syntax_comment(root, list->origline,
121825a68471Sdougm 					    dgettext(TEXT_DOMAIN,
121925a68471Sdougm 					    "Path"), 1);
122025a68471Sdougm 				continue;
12216185db85Sdougm 			}
12226185db85Sdougm 		} else {
122325a68471Sdougm 			if (group != sgroup) {
122425a68471Sdougm 				(void) printf(dgettext(TEXT_DOMAIN,
122525a68471Sdougm 				    "Attempt to change configuration in "
122625a68471Sdougm 				    "dfstab: line %d: %s\n"),
122725a68471Sdougm 				    list->lineno, list->origline);
122825a68471Sdougm 				add_syntax_comment(root, list->origline,
122925a68471Sdougm 				    dgettext(TEXT_DOMAIN,
123025a68471Sdougm 				    "Attempt to change configuration"), 1);
123125a68471Sdougm 				continue;
123225a68471Sdougm 			}
1233546405c3Sdougm 			/*
1234546405c3Sdougm 			 * It is the same group but could have changed
1235546405c3Sdougm 			 * options. Make sure we include the group's
1236546405c3Sdougm 			 * properties so we don't end up moving them to
1237546405c3Sdougm 			 * the share inadvertantly. The last arg being
1238546405c3Sdougm 			 * true says to get the inherited properties as well
1239546405c3Sdougm 			 * as the local properties.
1240546405c3Sdougm 			 */
124125a68471Sdougm 			oldprops = sa_proto_legacy_format(list->fstype, share,
124225a68471Sdougm 			    B_TRUE);
124325a68471Sdougm 
124425a68471Sdougm 			if (oldprops == NULL)
124525a68471Sdougm 				continue;
124625a68471Sdougm 
12476185db85Sdougm 			if (list->options != NULL &&
124825a68471Sdougm 			    strcmp(oldprops, list->options) != 0) {
124925a68471Sdougm 				sa_optionset_t opts;
125025a68471Sdougm 				sa_security_t secs;
125125a68471Sdougm 
125225a68471Sdougm 				/* possibly different values */
125325a68471Sdougm 				opts = sa_get_optionset((sa_group_t)
125425a68471Sdougm 				    share, list->fstype);
125525a68471Sdougm 				(void) sa_destroy_optionset(opts);
125625a68471Sdougm 
125725a68471Sdougm 				for (secs = sa_get_security(
125825a68471Sdougm 				    (sa_group_t)share, NULL, list->fstype);
125925a68471Sdougm 				    secs != NULL;
126025a68471Sdougm 				    secs = sa_get_security((sa_group_t)share,
126125a68471Sdougm 				    NULL, list->fstype)) {
126225a68471Sdougm 					(void) sa_destroy_security(
126325a68471Sdougm 					    secs);
126425a68471Sdougm 				}
126525a68471Sdougm 				(void) sa_parse_legacy_options(share,
126625a68471Sdougm 				    list->options, list->fstype);
12676185db85Sdougm 			}
1268a99982a7Sdougm 			sa_format_free(oldprops);
12696185db85Sdougm 		}
12706185db85Sdougm 	}
12716185db85Sdougm 	dfs_free_list(head);
12726185db85Sdougm }
12736185db85Sdougm 
12746185db85Sdougm /*
12756185db85Sdougm  * legacy_removes(group, file)
12766185db85Sdougm  *
12776185db85Sdougm  * Find any shares that are "missing" from the legacy file. These
12786185db85Sdougm  * should be removed from the configuration since they are likely from
12796185db85Sdougm  * a legacy app or the admin modified the dfstab file directly. We
12806185db85Sdougm  * have to support this even if it is not the recommended way to do
12816185db85Sdougm  * things.
12826185db85Sdougm  */
12836185db85Sdougm 
12846185db85Sdougm static void
12856185db85Sdougm legacy_removes(sa_group_t group, char *file)
12866185db85Sdougm {
12876185db85Sdougm 	sa_share_t share;
12886185db85Sdougm 	char *path;
12896185db85Sdougm 	xfs_sharelist_t *list, *item;
12906185db85Sdougm 	FILE *dfstab;
12916185db85Sdougm 
12926185db85Sdougm 	dfstab = fopen(file, "r");
12936185db85Sdougm 	if (dfstab != NULL) {
129425a68471Sdougm 		list = getdfstab(dfstab);
129525a68471Sdougm 		(void) fclose(dfstab);
1296f345c0beSdougm retry:
129725a68471Sdougm 		for (share = sa_get_share(group, NULL);
129825a68471Sdougm 		    share != NULL;
129925a68471Sdougm 		    share = sa_get_next_share(share)) {
130025a68471Sdougm 			/* now see if the share is in the dfstab file */
130125a68471Sdougm 			path = sa_get_share_attr(share, "path");
130225a68471Sdougm 			if (path != NULL) {
130325a68471Sdougm 				item = finddfsentry(list, path);
130425a68471Sdougm 				sa_free_attr_string(path);
130525a68471Sdougm 				if (item == NULL) {
130625a68471Sdougm 					/* The share was removed this way */
130725a68471Sdougm 					(void) sa_remove_share(share);
130825a68471Sdougm 
130925a68471Sdougm 					/*
131025a68471Sdougm 					 * Start over since the list was broken
131125a68471Sdougm 					 */
131225a68471Sdougm 					goto retry;
131325a68471Sdougm 				}
131425a68471Sdougm 			}
13156185db85Sdougm 		}
131625a68471Sdougm 		if (list != NULL)
131725a68471Sdougm 			dfs_free_list(list);
13186185db85Sdougm 	}
13196185db85Sdougm }
13206185db85Sdougm 
13216185db85Sdougm /*
13226185db85Sdougm  * getlegacyconfig(path, root)
13236185db85Sdougm  *
13246185db85Sdougm  * Parse dfstab and build the legacy configuration. This only gets
13256185db85Sdougm  * called when a change was detected.
13266185db85Sdougm  */
13276185db85Sdougm 
13286185db85Sdougm void
1329549ec3ffSdougm getlegacyconfig(sa_handle_t handle, char *path, xmlNodePtr *root)
13306185db85Sdougm {
13316185db85Sdougm 	sa_group_t defgroup;
13326185db85Sdougm 
13336185db85Sdougm 	if (root != NULL) {
133425a68471Sdougm 		if (*root == NULL)
133525a68471Sdougm 			*root = xmlNewNode(NULL, (xmlChar *)"sharecfg");
133625a68471Sdougm 		if (*root != NULL) {
133725a68471Sdougm 			if (strcmp(path, SA_LEGACY_DFSTAB) == 0) {
133825a68471Sdougm 				/*
133925a68471Sdougm 				 * Walk the default shares and find anything
134025a68471Sdougm 				 * missing.  we do this first to make sure it
134125a68471Sdougm 				 * is cleaned up since there may be legacy
134225a68471Sdougm 				 * code add/del via dfstab and we need to
134325a68471Sdougm 				 * cleanup SMF.
134425a68471Sdougm 				 */
134525a68471Sdougm 				defgroup = sa_get_group(handle, "default");
134625a68471Sdougm 				if (defgroup != NULL)
134725a68471Sdougm 					legacy_removes(defgroup, path);
134825a68471Sdougm 				/* Parse the dfstab and add anything new */
134925a68471Sdougm 				parse_dfstab(handle, path, *root);
135025a68471Sdougm 			}
13516185db85Sdougm 		}
13526185db85Sdougm 	}
13536185db85Sdougm }
13546185db85Sdougm 
13551cea05afSdougm /*
13561cea05afSdougm  * get_share_list(&err)
13571cea05afSdougm  *
13581cea05afSdougm  * Get a linked list of all the shares on the system from
13591cea05afSdougm  * /etc/dfs/sharetab. This is partially copied from libfsmgt which we
13601cea05afSdougm  * can't use due to package dependencies.
13611cea05afSdougm  */
13621cea05afSdougm static xfs_sharelist_t *
13631cea05afSdougm get_share_list(int *errp)
13641cea05afSdougm {
13651cea05afSdougm 	xfs_sharelist_t	*newp;
13661cea05afSdougm 	xfs_sharelist_t	*headp;
13671cea05afSdougm 	xfs_sharelist_t	*tailp;
13681cea05afSdougm 	FILE		*fp;
13691cea05afSdougm 
13701cea05afSdougm 	headp = NULL;
13711cea05afSdougm 	tailp = NULL;
13721cea05afSdougm 
13731cea05afSdougm 	if ((fp = fopen(SHARETAB, "r")) != NULL) {
13741cea05afSdougm 		struct share	*sharetab_entry;
13751cea05afSdougm 
13768d7e4166Sjose borrego 		(void) lockf(fileno(fp), F_LOCK, 0);
13778d7e4166Sjose borrego 		(void) mutex_lock(&sharetab_lock);
13788d7e4166Sjose borrego 
13791cea05afSdougm 		while (getshare(fp, &sharetab_entry) > 0) {
138025a68471Sdougm 			newp = alloc_sharelist();
13818d7e4166Sjose borrego 			if (newp == NULL) {
13828d7e4166Sjose borrego 				(void) mutex_unlock(&sharetab_lock);
13838d7e4166Sjose borrego 				(void) lockf(fileno(fp), F_ULOCK, 0);
138425a68471Sdougm 				goto err;
13858d7e4166Sjose borrego 			}
13861cea05afSdougm 
13871cea05afSdougm 			/*
138825a68471Sdougm 			 * Link into the list here so we don't leak
13891cea05afSdougm 			 * memory on a failure from strdup().
13901cea05afSdougm 			 */
139125a68471Sdougm 			if (headp == NULL) {
139225a68471Sdougm 				headp = newp;
139325a68471Sdougm 				tailp = newp;
139425a68471Sdougm 			} else {
139525a68471Sdougm 				tailp->next = newp;
139625a68471Sdougm 				tailp = newp;
139725a68471Sdougm 			}
139825a68471Sdougm 
139925a68471Sdougm 			newp->path = strdup(sharetab_entry->sh_path);
140025a68471Sdougm 			newp->resource = strdup(sharetab_entry->sh_res);
140125a68471Sdougm 			newp->fstype = strdup(sharetab_entry->sh_fstype);
140225a68471Sdougm 			newp->options = strdup(sharetab_entry->sh_opts);
140325a68471Sdougm 			newp->description = strdup(sharetab_entry->sh_descr);
14048d7e4166Sjose borrego 
14058d7e4166Sjose borrego 			if (newp->path == NULL || newp->resource == NULL ||
14068d7e4166Sjose borrego 			    newp->fstype == NULL || newp->options == NULL ||
14078d7e4166Sjose borrego 			    newp->description == NULL) {
14088d7e4166Sjose borrego 				(void) mutex_unlock(&sharetab_lock);
14098d7e4166Sjose borrego 				(void) lockf(fileno(fp), F_ULOCK, 0);
141025a68471Sdougm 				goto err;
14118d7e4166Sjose borrego 			}
14121cea05afSdougm 		}
14138d7e4166Sjose borrego 
14148d7e4166Sjose borrego 		(void) mutex_unlock(&sharetab_lock);
1415a99982a7Sdougm 		(void) lockf(fileno(fp), F_ULOCK, 0);
14161cea05afSdougm 		(void) fclose(fp);
14171cea05afSdougm 	} else {
141825a68471Sdougm 		*errp = errno;
14191cea05afSdougm 	}
14201cea05afSdougm 
14211cea05afSdougm 	/*
14221cea05afSdougm 	 * Caller must free the mount list
14231cea05afSdougm 	 */
14241cea05afSdougm 	return (headp);
14251cea05afSdougm err:
14261cea05afSdougm 	/*
14271cea05afSdougm 	 * Out of memory so cleanup and leave.
14281cea05afSdougm 	 */
14291cea05afSdougm 	dfs_free_list(headp);
14301cea05afSdougm 	(void) fclose(fp);
14311cea05afSdougm 	return (NULL);
14321cea05afSdougm }
14331cea05afSdougm 
14346185db85Sdougm /*
1435549ec3ffSdougm  * parse_sharetab(handle)
14366185db85Sdougm  *
14371cea05afSdougm  * Read the /etc/dfs/sharetab file and see which entries don't exist
14381cea05afSdougm  * in the repository. These shares are marked transient.  We also need
14391cea05afSdougm  * to see if they are ZFS shares since ZFS bypasses the SMF
14401cea05afSdougm  * repository.
14416185db85Sdougm  */
14426185db85Sdougm 
14436185db85Sdougm int
1444549ec3ffSdougm parse_sharetab(sa_handle_t handle)
14456185db85Sdougm {
14461cea05afSdougm 	xfs_sharelist_t *list, *tmplist;
14476185db85Sdougm 	int err = 0;
14486185db85Sdougm 	sa_share_t share;
14496185db85Sdougm 	sa_group_t group;
14506185db85Sdougm 	sa_group_t lgroup;
14516185db85Sdougm 	char *groupname;
14526185db85Sdougm 	int legacy = 0;
1453da6c28aaSamw 	char shareopts[MAXNAMLEN];
14546185db85Sdougm 
14551cea05afSdougm 	list = get_share_list(&err);
14566185db85Sdougm 	if (list == NULL)
145725a68471Sdougm 		return (legacy);
14586185db85Sdougm 
1459549ec3ffSdougm 	lgroup = sa_get_group(handle, "default");
14606185db85Sdougm 
14616185db85Sdougm 	for (tmplist = list; tmplist != NULL; tmplist = tmplist->next) {
146225a68471Sdougm 		group = NULL;
146325a68471Sdougm 		share = sa_find_share(handle, tmplist->path);
146425a68471Sdougm 		if (share != NULL) {
146525a68471Sdougm 			/*
146625a68471Sdougm 			 * If this is a legacy share, mark as shared so we
146725a68471Sdougm 			 * only update sharetab appropriately. We also keep
146825a68471Sdougm 			 * the sharetab options in order to display for legacy
146925a68471Sdougm 			 * share with no arguments.
147025a68471Sdougm 			 */
147125a68471Sdougm 			set_node_attr(share, "shared", "true");
1472da6c28aaSamw 			(void) snprintf(shareopts, MAXNAMLEN, "shareopts-%s",
1473da6c28aaSamw 			    tmplist->fstype);
1474da6c28aaSamw 			set_node_attr(share, shareopts, tmplist->options);
147525a68471Sdougm 			continue;
147625a68471Sdougm 		}
147725a68471Sdougm 
14786185db85Sdougm 		/*
147925a68471Sdougm 		 * This share is transient so needs to be
14806185db85Sdougm 		 * added. Initially, this will be under
14816185db85Sdougm 		 * default(legacy) unless it is a ZFS
14826185db85Sdougm 		 * share. If zfs, we need a zfs group.
14836185db85Sdougm 		 */
14846185db85Sdougm 		if (tmplist->resource != NULL &&
14856185db85Sdougm 		    (groupname = strchr(tmplist->resource, '@')) != NULL) {
148625a68471Sdougm 			/* There is a defined group */
148725a68471Sdougm 			*groupname++ = '\0';
148825a68471Sdougm 			group = sa_get_group(handle, groupname);
14896185db85Sdougm 			if (group != NULL) {
1490da6c28aaSamw 				share = _sa_add_share(group, tmplist->path,
1491da6c28aaSamw 				    SA_SHARE_TRANSIENT, &err,
1492da6c28aaSamw 				    (uint64_t)SA_FEATURE_NONE);
149325a68471Sdougm 			} else {
149425a68471Sdougm 				/*
149525a68471Sdougm 				 * While this case shouldn't
149625a68471Sdougm 				 * occur very often, it does
149725a68471Sdougm 				 * occur out of a "zfs set
149825a68471Sdougm 				 * sharenfs=off" when the
149925a68471Sdougm 				 * dataset is also set to
150025a68471Sdougm 				 * canmount=off. A warning
150125a68471Sdougm 				 * will then cause the zfs
150225a68471Sdougm 				 * command to abort. Since we
150325a68471Sdougm 				 * add it to the default list,
150425a68471Sdougm 				 * everything works properly
150525a68471Sdougm 				 * anyway and the library
150625a68471Sdougm 				 * doesn't need to give a
150725a68471Sdougm 				 * warning.
150825a68471Sdougm 				 */
150925a68471Sdougm 				share = _sa_add_share(lgroup,
151025a68471Sdougm 				    tmplist->path, SA_SHARE_TRANSIENT,
1511da6c28aaSamw 				    &err, (uint64_t)SA_FEATURE_NONE);
151225a68471Sdougm 			}
151325a68471Sdougm 		} else {
151425a68471Sdougm 			if (sa_zfs_is_shared(handle, tmplist->path)) {
151525a68471Sdougm 				group = sa_get_group(handle, "zfs");
151625a68471Sdougm 				if (group == NULL) {
151725a68471Sdougm 					group = sa_create_group(handle,
151825a68471Sdougm 					    "zfs", &err);
151925a68471Sdougm 					if (group == NULL &&
152025a68471Sdougm 					    err == SA_NO_PERMISSION) {
152125a68471Sdougm 						group = _sa_create_group(
152225a68471Sdougm 						    (sa_handle_impl_t)
152325a68471Sdougm 						    handle,
152425a68471Sdougm 						    "zfs");
152525a68471Sdougm 					}
152625a68471Sdougm 					if (group != NULL) {
152725a68471Sdougm 						(void) sa_create_optionset(
152825a68471Sdougm 						    group, tmplist->fstype);
152925a68471Sdougm 						(void) sa_set_group_attr(group,
153025a68471Sdougm 						    "zfs", "true");
153125a68471Sdougm 					}
153225a68471Sdougm 				}
153325a68471Sdougm 				if (group != NULL) {
153425a68471Sdougm 					share = _sa_add_share(group,
153525a68471Sdougm 					    tmplist->path, SA_SHARE_TRANSIENT,
1536da6c28aaSamw 					    &err, (uint64_t)SA_FEATURE_NONE);
153725a68471Sdougm 				}
153825a68471Sdougm 			} else {
153925a68471Sdougm 				share = _sa_add_share(lgroup, tmplist->path,
1540da6c28aaSamw 				    SA_SHARE_TRANSIENT, &err,
1541da6c28aaSamw 				    (uint64_t)SA_FEATURE_NONE);
15426185db85Sdougm 			}
15436185db85Sdougm 		}
15446185db85Sdougm 		if (share == NULL)
154525a68471Sdougm 			(void) printf(dgettext(TEXT_DOMAIN,
154625a68471Sdougm 			    "Problem with transient: %s\n"), sa_errorstr(err));
15476185db85Sdougm 		if (share != NULL)
154825a68471Sdougm 			set_node_attr(share, "shared", "true");
15496185db85Sdougm 		if (err == SA_OK) {
155025a68471Sdougm 			if (tmplist->options != NULL &&
155125a68471Sdougm 			    strlen(tmplist->options) > 0) {
155225a68471Sdougm 				(void) sa_parse_legacy_options(share,
155325a68471Sdougm 				    tmplist->options, tmplist->fstype);
155425a68471Sdougm 			}
155525a68471Sdougm 			if (tmplist->resource != NULL &&
155625a68471Sdougm 			    strcmp(tmplist->resource, "-") != 0)
155725a68471Sdougm 				set_node_attr(share, "resource",
155825a68471Sdougm 				    tmplist->resource);
155925a68471Sdougm 			if (tmplist->description != NULL) {
156025a68471Sdougm 				xmlNodePtr node;
156125a68471Sdougm 				node = xmlNewChild((xmlNodePtr)share, NULL,
156225a68471Sdougm 				    (xmlChar *)"description", NULL);
156325a68471Sdougm 				xmlNodeSetContent(node,
156425a68471Sdougm 				    (xmlChar *)tmplist->description);
156525a68471Sdougm 			}
156625a68471Sdougm 			legacy = 1;
15676185db85Sdougm 		}
15686185db85Sdougm 	}
15691cea05afSdougm 	dfs_free_list(list);
15706185db85Sdougm 	return (legacy);
15716185db85Sdougm }
15726185db85Sdougm 
15736185db85Sdougm /*
157425a68471Sdougm  * Get the transient shares from the sharetab (or other) file.  since
15756185db85Sdougm  * these are transient, they only appear in the working file and not
15766185db85Sdougm  * in a repository.
15776185db85Sdougm  */
15786185db85Sdougm int
1579549ec3ffSdougm gettransients(sa_handle_impl_t ihandle, xmlNodePtr *root)
15806185db85Sdougm {
15816185db85Sdougm 	int legacy = 0;
1582da6c28aaSamw 	int numproto;
1583da6c28aaSamw 	char **protocols = NULL;
1584da6c28aaSamw 	int i;
15856185db85Sdougm 
15866185db85Sdougm 	if (root != NULL) {
158725a68471Sdougm 		if (*root == NULL)
158825a68471Sdougm 			*root = xmlNewNode(NULL, (xmlChar *)"sharecfg");
1589da6c28aaSamw 		if (*root != NULL) {
159025a68471Sdougm 			legacy = parse_sharetab(ihandle);
1591da6c28aaSamw 			numproto = sa_get_protocols(&protocols);
1592da6c28aaSamw 			for (i = 0; i < numproto; i++)
1593da6c28aaSamw 				legacy |= sa_proto_get_transients(
1594da6c28aaSamw 				    (sa_handle_t)ihandle, protocols[i]);
1595da6c28aaSamw 			if (protocols != NULL)
1596da6c28aaSamw 				free(protocols);
1597da6c28aaSamw 		}
15986185db85Sdougm 	}
15996185db85Sdougm 	return (legacy);
16006185db85Sdougm }
16016185db85Sdougm 
16026185db85Sdougm /*
16036185db85Sdougm  * sa_has_prop(optionset, prop)
16046185db85Sdougm  *
16056185db85Sdougm  * Is the specified property a member of the optionset?
16066185db85Sdougm  */
16076185db85Sdougm 
16086185db85Sdougm int
16096185db85Sdougm sa_has_prop(sa_optionset_t optionset, sa_property_t prop)
16106185db85Sdougm {
16116185db85Sdougm 	char *name;
16126185db85Sdougm 	sa_property_t otherprop;
16136185db85Sdougm 	int result = 0;
16146185db85Sdougm 
16156185db85Sdougm 	if (optionset != NULL) {
161625a68471Sdougm 		name = sa_get_property_attr(prop, "type");
161725a68471Sdougm 		if (name != NULL) {
161825a68471Sdougm 			otherprop = sa_get_property(optionset, name);
161925a68471Sdougm 			if (otherprop != NULL)
162025a68471Sdougm 				result = 1;
162125a68471Sdougm 			sa_free_attr_string(name);
162225a68471Sdougm 		}
16236185db85Sdougm 	}
16246185db85Sdougm 	return (result);
16256185db85Sdougm }
16266185db85Sdougm 
16276185db85Sdougm /*
16286185db85Sdougm  * Update legacy files
16296185db85Sdougm  *
16306185db85Sdougm  * Provides functions to add/remove/modify individual entries
16316185db85Sdougm  * in dfstab and sharetab
16326185db85Sdougm  */
16336185db85Sdougm 
16346185db85Sdougm void
1635549ec3ffSdougm update_legacy_config(sa_handle_t handle)
16366185db85Sdougm {
16376185db85Sdougm 	/*
16386185db85Sdougm 	 * no longer used -- this is a placeholder in case we need to
16396185db85Sdougm 	 * add it back later.
16406185db85Sdougm 	 */
1641549ec3ffSdougm #ifdef lint
1642549ec3ffSdougm 	handle = handle;
1643549ec3ffSdougm #endif
16446185db85Sdougm }
16456185db85Sdougm 
16466185db85Sdougm /*
1647687915e9Sdougm  * sa_valid_property(handle, object, proto, property)
16486185db85Sdougm  *
16496185db85Sdougm  * check to see if the specified property is valid relative to the
16506185db85Sdougm  * specified protocol. The protocol plugin is called to do the work.
16516185db85Sdougm  */
16526185db85Sdougm 
16536185db85Sdougm int
1654687915e9Sdougm sa_valid_property(sa_handle_t handle, void *object, char *proto,
1655687915e9Sdougm     sa_property_t property)
16566185db85Sdougm {
16576185db85Sdougm 	int ret = SA_OK;
16586185db85Sdougm 
16596185db85Sdougm 	if (proto != NULL && property != NULL) {
1660687915e9Sdougm 		ret = sa_proto_valid_prop(handle, proto, property, object);
16616185db85Sdougm 	}
16626185db85Sdougm 
16636185db85Sdougm 	return (ret);
16646185db85Sdougm }
16656185db85Sdougm 
16666185db85Sdougm /*
16676185db85Sdougm  * sa_fstype(path)
16686185db85Sdougm  *
16696185db85Sdougm  * Given path, return the string representing the path's file system
16706185db85Sdougm  * type. This is used to discover ZFS shares.
16716185db85Sdougm  */
16726185db85Sdougm 
16736185db85Sdougm char *
16746185db85Sdougm sa_fstype(char *path)
16756185db85Sdougm {
16766185db85Sdougm 	int err;
16776185db85Sdougm 	struct stat st;
16786185db85Sdougm 
16796185db85Sdougm 	err = stat(path, &st);
168025a68471Sdougm 	if (err < 0)
168125a68471Sdougm 		err = SA_NO_SUCH_PATH;
168225a68471Sdougm 	else
168325a68471Sdougm 		err = SA_OK;
168425a68471Sdougm 
168525a68471Sdougm 	/*
168625a68471Sdougm 	 * If we have a valid path at this point ret, return the fstype.
168725a68471Sdougm 	 */
168825a68471Sdougm 	if (err == SA_OK)
168925a68471Sdougm 		return (strdup(st.st_fstype));
169025a68471Sdougm 
16916185db85Sdougm 	return (NULL);
16926185db85Sdougm }
16936185db85Sdougm 
16946185db85Sdougm void
16956185db85Sdougm sa_free_fstype(char *type)
16966185db85Sdougm {
16976185db85Sdougm 	free(type);
16986185db85Sdougm }
16996185db85Sdougm 
17006185db85Sdougm /*
17016185db85Sdougm  * sa_get_derived_optionset(object, proto, hier)
17026185db85Sdougm  *
17036185db85Sdougm  *	Work backward to the top of the share object tree and start
17046185db85Sdougm  *	copying protocol specific optionsets into a newly created
17056185db85Sdougm  *	optionset that doesn't have a parent (it will be freed
1706da6c28aaSamw  *	later). This provides for the property inheritance model. That
17076185db85Sdougm  *	is, properties closer to the share take precedence over group
17086185db85Sdougm  *	level. This also provides for groups of groups in the future.
17096185db85Sdougm  */
17106185db85Sdougm 
17116185db85Sdougm sa_optionset_t
17126185db85Sdougm sa_get_derived_optionset(void *object, char *proto, int hier)
17136185db85Sdougm {
17146185db85Sdougm 	sa_optionset_t newoptionset;
17156185db85Sdougm 	sa_optionset_t optionset;
17166185db85Sdougm 	sa_group_t group;
17176185db85Sdougm 
17186185db85Sdougm 	if (hier &&
17196185db85Sdougm 	    (group = sa_get_parent_group((sa_share_t)object)) != NULL) {
172025a68471Sdougm 		newoptionset = sa_get_derived_optionset((void *)group, proto,
172125a68471Sdougm 		    hier);
17226185db85Sdougm 	} else {
172325a68471Sdougm 		newoptionset = (sa_optionset_t)xmlNewNode(NULL,
172425a68471Sdougm 		    (xmlChar *)"optionset");
172525a68471Sdougm 		if (newoptionset != NULL) {
172625a68471Sdougm 			sa_set_optionset_attr(newoptionset, "type", proto);
172725a68471Sdougm 		}
17286185db85Sdougm 	}
172925a68471Sdougm 	/* Dont' do anything if memory wasn't allocated */
17306185db85Sdougm 	if (newoptionset == NULL)
173125a68471Sdougm 		return (NULL);
17326185db85Sdougm 
173325a68471Sdougm 	/* Found the top so working back down the stack */
17346185db85Sdougm 	optionset = sa_get_optionset((sa_optionset_t)object, proto);
17356185db85Sdougm 	if (optionset != NULL) {
173625a68471Sdougm 		sa_property_t prop;
173725a68471Sdougm 		/* add optionset to the newoptionset */
173825a68471Sdougm 		for (prop = sa_get_property(optionset, NULL);
173925a68471Sdougm 		    prop != NULL;
174025a68471Sdougm 		    prop = sa_get_next_property(prop)) {
174125a68471Sdougm 			sa_property_t newprop;
174225a68471Sdougm 			char *name;
174325a68471Sdougm 			char *value;
174425a68471Sdougm 			name = sa_get_property_attr(prop, "type");
174525a68471Sdougm 			value = sa_get_property_attr(prop, "value");
174625a68471Sdougm 			if (name == NULL)
174725a68471Sdougm 				continue;
174825a68471Sdougm 			newprop = sa_get_property(newoptionset, name);
174925a68471Sdougm 			/* Replace the value with the new value */
175025a68471Sdougm 			if (newprop != NULL) {
175125a68471Sdougm 				/*
175225a68471Sdougm 				 * Only set if value is non NULL, old value ok
175325a68471Sdougm 				 * if it is NULL.
175425a68471Sdougm 				 */
175525a68471Sdougm 				if (value != NULL)
175625a68471Sdougm 					set_node_attr(newprop, "value", value);
175725a68471Sdougm 			} else {
175825a68471Sdougm 				/* an entirely new property */
175925a68471Sdougm 				if (value != NULL) {
176025a68471Sdougm 					newprop = sa_create_property(name,
176125a68471Sdougm 					    value);
176225a68471Sdougm 					if (newprop != NULL) {
176325a68471Sdougm 						newprop = (sa_property_t)
176425a68471Sdougm 						    xmlAddChild(
176525a68471Sdougm 						    (xmlNodePtr)newoptionset,
176625a68471Sdougm 						    (xmlNodePtr)newprop);
176725a68471Sdougm 					}
176825a68471Sdougm 				}
17696185db85Sdougm 			}
177025a68471Sdougm 			sa_free_attr_string(name);
177125a68471Sdougm 
177225a68471Sdougm 			if (value != NULL)
177325a68471Sdougm 				sa_free_attr_string(value);
17746185db85Sdougm 		}
17756185db85Sdougm 	}
17766185db85Sdougm 	return (newoptionset);
17776185db85Sdougm }
17786185db85Sdougm 
17796185db85Sdougm void
17806185db85Sdougm sa_free_derived_optionset(sa_optionset_t optionset)
17816185db85Sdougm {
178225a68471Sdougm 	/* While it shouldn't be linked, it doesn't hurt */
17836185db85Sdougm 	if (optionset != NULL) {
178425a68471Sdougm 		xmlUnlinkNode((xmlNodePtr) optionset);
178525a68471Sdougm 		xmlFreeNode((xmlNodePtr) optionset);
17866185db85Sdougm 	}
17876185db85Sdougm }
17886185db85Sdougm 
17896185db85Sdougm /*
17906185db85Sdougm  *  sa_get_all_security_types(object, proto, hier)
17916185db85Sdougm  *
179225a68471Sdougm  *	Find all the security types set for this object.  This is
17936185db85Sdougm  *	preliminary to getting a derived security set. The return value is an
17946185db85Sdougm  *	optionset containg properties which are the sectype values found by
1795da6c28aaSamw  *	walking up the XML document structure. The returned optionset
17966185db85Sdougm  *	is a derived optionset.
17976185db85Sdougm  *
17986185db85Sdougm  *	If hier is 0, only look at object. If non-zero, walk up the tree.
17996185db85Sdougm  */
18006185db85Sdougm sa_optionset_t
18016185db85Sdougm sa_get_all_security_types(void *object, char *proto, int hier)
18026185db85Sdougm {
18036185db85Sdougm 	sa_optionset_t options;
18046185db85Sdougm 	sa_security_t security;
18056185db85Sdougm 	sa_group_t group;
18066185db85Sdougm 	sa_property_t prop;
18076185db85Sdougm 
18086185db85Sdougm 	options = NULL;
18096185db85Sdougm 
18106185db85Sdougm 	if (hier &&
181125a68471Sdougm 	    (group = sa_get_parent_group((sa_share_t)object)) != NULL)
181225a68471Sdougm 		options = sa_get_all_security_types((void *)group, proto, hier);
181325a68471Sdougm 	else
181425a68471Sdougm 		options = (sa_optionset_t)xmlNewNode(NULL,
181525a68471Sdougm 		    (xmlChar *)"optionset");
181625a68471Sdougm 
181725a68471Sdougm 	if (options == NULL)
181825a68471Sdougm 		return (options);
181925a68471Sdougm 
182025a68471Sdougm 	/* Hit the top so collect the security types working back. */
182125a68471Sdougm 	for (security = sa_get_security((sa_group_t)object, NULL, NULL);
182225a68471Sdougm 	    security != NULL;
182325a68471Sdougm 	    security = sa_get_next_security(security)) {
18246185db85Sdougm 		char *type;
18256185db85Sdougm 		char *sectype;
18266185db85Sdougm 
18276185db85Sdougm 		type = sa_get_security_attr(security, "type");
18286185db85Sdougm 		if (type != NULL) {
182925a68471Sdougm 			if (strcmp(type, proto) != 0) {
183025a68471Sdougm 				sa_free_attr_string(type);
183125a68471Sdougm 				continue;
183225a68471Sdougm 			}
183325a68471Sdougm 			sectype = sa_get_security_attr(security, "sectype");
183425a68471Sdougm 			if (sectype != NULL) {
183525a68471Sdougm 				/*
183625a68471Sdougm 				 * Have a security type, check to see if
183725a68471Sdougm 				 * already present in optionset and add if it
183825a68471Sdougm 				 * isn't.
183925a68471Sdougm 				 */
184025a68471Sdougm 				if (sa_get_property(options, sectype) == NULL) {
184125a68471Sdougm 					prop = sa_create_property(sectype,
184225a68471Sdougm 					    "true");
184325a68471Sdougm 					if (prop != NULL)
184425a68471Sdougm 						prop = (sa_property_t)
184525a68471Sdougm 						    xmlAddChild(
184625a68471Sdougm 						    (xmlNodePtr)options,
184725a68471Sdougm 						    (xmlNodePtr)prop);
184825a68471Sdougm 				}
184925a68471Sdougm 				sa_free_attr_string(sectype);
18506185db85Sdougm 			}
185125a68471Sdougm 			sa_free_attr_string(type);
18526185db85Sdougm 		}
18536185db85Sdougm 	}
185425a68471Sdougm 
18556185db85Sdougm 	return (options);
18566185db85Sdougm }
18576185db85Sdougm 
18586185db85Sdougm /*
18596185db85Sdougm  * sa_get_derived_security(object, sectype, proto, hier)
18606185db85Sdougm  *
18616185db85Sdougm  * Get the derived security(named optionset) for the object given the
18626185db85Sdougm  * sectype and proto. If hier is non-zero, walk up the tree to get all
18636185db85Sdougm  * properties defined for this object, otherwise just those on the
18646185db85Sdougm  * object.
18656185db85Sdougm  */
18666185db85Sdougm 
18676185db85Sdougm sa_security_t
18686185db85Sdougm sa_get_derived_security(void *object, char *sectype, char *proto, int hier)
18696185db85Sdougm {
18706185db85Sdougm 	sa_security_t newsecurity;
18716185db85Sdougm 	sa_security_t security;
18726185db85Sdougm 	sa_group_t group;
187325a68471Sdougm 	sa_property_t prop;
18746185db85Sdougm 
18756185db85Sdougm 	if (hier &&
18766185db85Sdougm 	    (group = sa_get_parent_group((sa_share_t)object)) != NULL) {
187725a68471Sdougm 		newsecurity = sa_get_derived_security((void *)group,
187825a68471Sdougm 		    sectype, proto, hier);
18796185db85Sdougm 	} else {
188025a68471Sdougm 		newsecurity = (sa_security_t)xmlNewNode(NULL,
188125a68471Sdougm 		    (xmlChar *)"security");
188225a68471Sdougm 		if (newsecurity != NULL) {
188325a68471Sdougm 			sa_set_security_attr(newsecurity, "type", proto);
188425a68471Sdougm 			sa_set_security_attr(newsecurity, "sectype", sectype);
188525a68471Sdougm 		}
18866185db85Sdougm 	}
188725a68471Sdougm 	/* Don't do anything if memory wasn't allocated */
18886185db85Sdougm 	if (newsecurity == NULL)
188925a68471Sdougm 		return (newsecurity);
18906185db85Sdougm 
189125a68471Sdougm 	/* Found the top so working back down the stack. */
18926185db85Sdougm 	security = sa_get_security((sa_security_t)object, sectype, proto);
189325a68471Sdougm 	if (security == NULL)
189425a68471Sdougm 		return (newsecurity);
189525a68471Sdougm 
189625a68471Sdougm 	/* add security to the newsecurity */
189725a68471Sdougm 	for (prop = sa_get_property(security, NULL);
189825a68471Sdougm 	    prop != NULL; prop = sa_get_next_property(prop)) {
18996185db85Sdougm 		sa_property_t newprop;
19006185db85Sdougm 		char *name;
19016185db85Sdougm 		char *value;
19026185db85Sdougm 		name = sa_get_property_attr(prop, "type");
19036185db85Sdougm 		value = sa_get_property_attr(prop, "value");
19046185db85Sdougm 		if (name != NULL) {
190525a68471Sdougm 			newprop = sa_get_property(newsecurity, name);
190625a68471Sdougm 			/* Replace the value with the new value */
190725a68471Sdougm 			if (newprop != NULL) {
190825a68471Sdougm 				/*
190997df5ac9Sdougm 				 * Only set if value is non NULL, old
191097df5ac9Sdougm 				 * value ok if it is NULL. The value
191197df5ac9Sdougm 				 * must be associated with the "value"
191297df5ac9Sdougm 				 * tag within XML.
191325a68471Sdougm 				 */
191425a68471Sdougm 				if (value != NULL)
191597df5ac9Sdougm 					set_node_attr(newprop, "value", value);
191625a68471Sdougm 			} else {
191725a68471Sdougm 				/* An entirely new property */
191825a68471Sdougm 				if (value != NULL) {
191925a68471Sdougm 					newprop = sa_create_property(name,
192025a68471Sdougm 					    value);
192125a68471Sdougm 					newprop = (sa_property_t)
192225a68471Sdougm 					    xmlAddChild((xmlNodePtr)newsecurity,
19236185db85Sdougm 					    (xmlNodePtr)newprop);
192425a68471Sdougm 				}
19256185db85Sdougm 			}
192625a68471Sdougm 			sa_free_attr_string(name);
19276185db85Sdougm 		}
19286185db85Sdougm 		if (value != NULL)
192925a68471Sdougm 			sa_free_attr_string(value);
19306185db85Sdougm 	}
19316185db85Sdougm 	return (newsecurity);
19326185db85Sdougm }
19336185db85Sdougm 
19346185db85Sdougm void
19356185db85Sdougm sa_free_derived_security(sa_security_t security)
19366185db85Sdougm {
19376185db85Sdougm 	/* while it shouldn't be linked, it doesn't hurt */
19386185db85Sdougm 	if (security != NULL) {
193925a68471Sdougm 		xmlUnlinkNode((xmlNodePtr)security);
194025a68471Sdougm 		xmlFreeNode((xmlNodePtr)security);
19416185db85Sdougm 	}
19426185db85Sdougm }
19436185db85Sdougm 
19446185db85Sdougm /*
19456185db85Sdougm  * sharetab utility functions
19466185db85Sdougm  *
194725a68471Sdougm  * Makes use of the original sharetab.c from fs.d/nfs/lib
19486185db85Sdougm  */
19496185db85Sdougm 
19506185db85Sdougm /*
1951ecd6cf80Smarks  * sa_fillshare(share, proto, sh)
19526185db85Sdougm  *
19536185db85Sdougm  * Fill the struct share with values obtained from the share object.
19546185db85Sdougm  */
1955ecd6cf80Smarks void
1956ecd6cf80Smarks sa_fillshare(sa_share_t share, char *proto, struct share *sh)
19576185db85Sdougm {
19586185db85Sdougm 	char *groupname = NULL;
19596185db85Sdougm 	char *value;
19606185db85Sdougm 	sa_group_t group;
19616185db85Sdougm 	char *buff;
19626185db85Sdougm 	char *zfs;
1963da6c28aaSamw 	sa_resource_t resource;
1964da6c28aaSamw 	char *rsrcname = NULL;
1965da6c28aaSamw 	char *defprop;
1966da6c28aaSamw 
1967da6c28aaSamw 	/*
1968da6c28aaSamw 	 * We only want to deal with the path level shares for the
1969da6c28aaSamw 	 * sharetab file. If a resource, get the parent.
1970da6c28aaSamw 	 */
1971da6c28aaSamw 	if (sa_is_resource(share)) {
1972da6c28aaSamw 		resource = (sa_resource_t)share;
1973da6c28aaSamw 		share = sa_get_resource_parent(resource);
1974da6c28aaSamw 		rsrcname = sa_get_resource_attr(resource, "name");
1975da6c28aaSamw 	}
19766185db85Sdougm 
19776185db85Sdougm 	group = sa_get_parent_group(share);
19786185db85Sdougm 	if (group != NULL) {
197925a68471Sdougm 		zfs = sa_get_group_attr(group, "zfs");
198025a68471Sdougm 		groupname = sa_get_group_attr(group, "name");
19816185db85Sdougm 
198225a68471Sdougm 		if (groupname != NULL &&
198325a68471Sdougm 		    (strcmp(groupname, "default") == 0 || zfs != NULL)) {
198425a68471Sdougm 			/*
198525a68471Sdougm 			 * since the groupname is either "default" or the
198625a68471Sdougm 			 * group is a ZFS group, we don't want to keep
198725a68471Sdougm 			 * groupname. We do want it if it is any other type of
198825a68471Sdougm 			 * group.
198925a68471Sdougm 			 */
199025a68471Sdougm 			sa_free_attr_string(groupname);
199125a68471Sdougm 			groupname = NULL;
199225a68471Sdougm 		}
199325a68471Sdougm 		if (zfs != NULL)
199425a68471Sdougm 			sa_free_attr_string(zfs);
19956185db85Sdougm 	}
19966185db85Sdougm 
19976185db85Sdougm 	value = sa_get_share_attr(share, "path");
19986185db85Sdougm 	if (value != NULL) {
199925a68471Sdougm 		sh->sh_path = strdup(value);
200025a68471Sdougm 		sa_free_attr_string(value);
20016185db85Sdougm 	}
20026185db85Sdougm 
2003da6c28aaSamw 	if (rsrcname != NULL || groupname != NULL) {
200425a68471Sdougm 		int len = 0;
200525a68471Sdougm 
2006da6c28aaSamw 		if (rsrcname != NULL)
2007da6c28aaSamw 			len += strlen(rsrcname);
200825a68471Sdougm 		if (groupname != NULL)
200925a68471Sdougm 			len += strlen(groupname);
201025a68471Sdougm 		len += 3; /* worst case */
201125a68471Sdougm 		buff = malloc(len);
201225a68471Sdougm 		(void) snprintf(buff, len, "%s%s%s",
2013da6c28aaSamw 		    (rsrcname != NULL &&
2014da6c28aaSamw 		    strlen(rsrcname) > 0) ? rsrcname : "-",
20156185db85Sdougm 		    groupname != NULL ? "@" : "",
20166185db85Sdougm 		    groupname != NULL ? groupname : "");
201725a68471Sdougm 		sh->sh_res = buff;
2018da6c28aaSamw 		if (rsrcname != NULL)
2019da6c28aaSamw 			sa_free_attr_string(rsrcname);
2020da6c28aaSamw 		if (groupname != NULL)
202125a68471Sdougm 			sa_free_attr_string(groupname);
20226185db85Sdougm 	} else {
202325a68471Sdougm 		sh->sh_res = strdup("-");
20246185db85Sdougm 	}
20256185db85Sdougm 
2026da6c28aaSamw 	/*
2027da6c28aaSamw 	 * Get correct default prop string. NFS uses "rw", others use
2028da6c28aaSamw 	 * "".
2029da6c28aaSamw 	 */
2030da6c28aaSamw 	if (strcmp(proto, "nfs") != 0)
2031da6c28aaSamw 		defprop = "\"\"";
2032da6c28aaSamw 	else
2033da6c28aaSamw 		defprop = "rw";
2034da6c28aaSamw 
20356185db85Sdougm 	sh->sh_fstype = strdup(proto);
20366185db85Sdougm 	value = sa_proto_legacy_format(proto, share, 1);
20376185db85Sdougm 	if (value != NULL) {
203825a68471Sdougm 		if (strlen(value) > 0)
203925a68471Sdougm 			sh->sh_opts = strdup(value);
204025a68471Sdougm 		else
2041da6c28aaSamw 			sh->sh_opts = strdup(defprop);
204225a68471Sdougm 		free(value);
204325a68471Sdougm 	} else {
2044da6c28aaSamw 		sh->sh_opts = strdup(defprop);
204525a68471Sdougm 	}
20466185db85Sdougm 
20476185db85Sdougm 	value = sa_get_share_description(share);
20486185db85Sdougm 	if (value != NULL) {
204925a68471Sdougm 		sh->sh_descr = strdup(value);
205025a68471Sdougm 		sa_free_share_description(value);
205125a68471Sdougm 	} else {
205225a68471Sdougm 		sh->sh_descr = strdup("");
205325a68471Sdougm 	}
20546185db85Sdougm }
20556185db85Sdougm 
20566185db85Sdougm /*
2057ecd6cf80Smarks  * sa_emptyshare(sh)
20586185db85Sdougm  *
20596185db85Sdougm  * Free the strings in the non-NULL members of sh.
20606185db85Sdougm  */
20616185db85Sdougm 
2062ecd6cf80Smarks void
2063ecd6cf80Smarks sa_emptyshare(struct share *sh)
20646185db85Sdougm {
20656185db85Sdougm 	if (sh->sh_path != NULL)
206625a68471Sdougm 		free(sh->sh_path);
20676185db85Sdougm 	sh->sh_path = NULL;
20686185db85Sdougm 	if (sh->sh_res != NULL)
206925a68471Sdougm 		free(sh->sh_res);
20706185db85Sdougm 	sh->sh_res = NULL;
20716185db85Sdougm 	if (sh->sh_fstype != NULL)
207225a68471Sdougm 		free(sh->sh_fstype);
20736185db85Sdougm 	sh->sh_fstype = NULL;
20746185db85Sdougm 	if (sh->sh_opts != NULL)
207525a68471Sdougm 		free(sh->sh_opts);
20766185db85Sdougm 	sh->sh_opts = NULL;
20776185db85Sdougm 	if (sh->sh_descr != NULL)
207825a68471Sdougm 		free(sh->sh_descr);
20796185db85Sdougm 	sh->sh_descr = NULL;
20806185db85Sdougm }
20816185db85Sdougm 
20825b6e0c46Sdougm /*
20835b6e0c46Sdougm  * sa_update_sharetab_ts(handle)
20845b6e0c46Sdougm  *
20855b6e0c46Sdougm  * Update the internal timestamp of when sharetab was last
20865b6e0c46Sdougm  * changed. This needs to be public for ZFS to get at it.
20875b6e0c46Sdougm  */
20885b6e0c46Sdougm 
20895b6e0c46Sdougm void
20905b6e0c46Sdougm sa_update_sharetab_ts(sa_handle_t handle)
20915b6e0c46Sdougm {
20925b6e0c46Sdougm 	struct stat st;
20935b6e0c46Sdougm 	sa_handle_impl_t implhandle = (sa_handle_impl_t)handle;
20945b6e0c46Sdougm 
20955b6e0c46Sdougm 	if (implhandle != NULL && stat(SA_LEGACY_SHARETAB, &st) == 0)
20965b6e0c46Sdougm 		implhandle->tssharetab = TSTAMP(st.st_mtim);
20975b6e0c46Sdougm }
20985b6e0c46Sdougm 
20996185db85Sdougm /*
21006185db85Sdougm  * sa_update_sharetab(share, proto)
21016185db85Sdougm  *
21026185db85Sdougm  * Update the sharetab file with info from the specified share.
21036185db85Sdougm  * This could be an update or add.
21046185db85Sdougm  */
21056185db85Sdougm 
21066185db85Sdougm int
21076185db85Sdougm sa_update_sharetab(sa_share_t share, char *proto)
21086185db85Sdougm {
2109a237e38eSth 	int	ret = SA_OK;
2110a237e38eSth 	share_t	sh;
2111a237e38eSth 	char	*path;
21125b6e0c46Sdougm 	sa_handle_t handle;
21136185db85Sdougm 
21146185db85Sdougm 	path = sa_get_share_attr(share, "path");
21156185db85Sdougm 	if (path != NULL) {
2116a237e38eSth 		(void) memset(&sh, '\0', sizeof (sh));
2117a237e38eSth 
21185b6e0c46Sdougm 		handle = sa_find_group_handle((sa_group_t)share);
21195b6e0c46Sdougm 		if (handle != NULL) {
21205b6e0c46Sdougm 			/*
21215b6e0c46Sdougm 			 * Fill in share structure and send it to the kernel.
21225b6e0c46Sdougm 			 */
21235b6e0c46Sdougm 			(void) sa_fillshare(share, proto, &sh);
21245b6e0c46Sdougm 			(void) _sharefs(SHAREFS_ADD, &sh);
21255b6e0c46Sdougm 			/*
21265b6e0c46Sdougm 			 * We need the timestamp of the sharetab file right
21275b6e0c46Sdougm 			 * after the update was done. This lets us detect a
21285b6e0c46Sdougm 			 * change that made by a different process.
21295b6e0c46Sdougm 			 */
21305b6e0c46Sdougm 			sa_update_sharetab_ts(handle);
21315b6e0c46Sdougm 			sa_emptyshare(&sh);
21325b6e0c46Sdougm 		} else {
21335b6e0c46Sdougm 			ret = SA_CONFIG_ERR;
21345b6e0c46Sdougm 		}
2135a237e38eSth 		sa_free_attr_string(path);
21366185db85Sdougm 	}
2137a237e38eSth 
21386185db85Sdougm 	return (ret);
21396185db85Sdougm }
21406185db85Sdougm 
21416185db85Sdougm /*
21425b6e0c46Sdougm  * sa_delete_sharetab(handle, path, proto)
21436185db85Sdougm  *
21446185db85Sdougm  * remove the specified share from sharetab.
21456185db85Sdougm  */
21466185db85Sdougm 
21476185db85Sdougm int
21485b6e0c46Sdougm sa_delete_sharetab(sa_handle_t handle, char *path, char *proto)
21496185db85Sdougm {
2150a237e38eSth 	int	ret = SA_OK;
21515b6e0c46Sdougm 	struct stat st;
21526185db85Sdougm 
2153a237e38eSth 	share_t	sh;
2154a237e38eSth 	/*
2155a237e38eSth 	 * Both the path and the proto are
2156a237e38eSth 	 * keys into the sharetab.
2157a237e38eSth 	 */
2158a237e38eSth 	if (path != NULL && proto != NULL) {
2159a237e38eSth 		(void) memset(&sh, '\0', sizeof (sh));
2160a237e38eSth 		sh.sh_path = path;
2161a237e38eSth 		sh.sh_fstype = proto;
2162a237e38eSth 
2163a3175730Sth 		ret = _sharefs(SHAREFS_REMOVE, &sh);
21645b6e0c46Sdougm 		if (handle != NULL && stat(SA_LEGACY_SHARETAB, &st) == 0)
21655b6e0c46Sdougm 			sa_update_sharetab_ts(handle);
21666185db85Sdougm 	}
21676185db85Sdougm 	return (ret);
21686185db85Sdougm }
21695b6e0c46Sdougm 
21705b6e0c46Sdougm /*
21715b6e0c46Sdougm  * sa_needs_refresh(handle)
21725b6e0c46Sdougm  *
21735b6e0c46Sdougm  * Returns B_TRUE if the internal cache needs to be refreshed do to a
21745b6e0c46Sdougm  * change by another process.  B_FALSE returned otherwise.
21755b6e0c46Sdougm  */
21765b6e0c46Sdougm boolean_t
2177b83b3cacSdougm sa_needs_refresh(sa_handle_t handle)
21785b6e0c46Sdougm {
21795b6e0c46Sdougm 	sa_handle_impl_t implhandle = (sa_handle_impl_t)handle;
21805b6e0c46Sdougm 	struct stat st;
21815b6e0c46Sdougm 	char *str;
21825b6e0c46Sdougm 	uint64_t tstamp;
21835b6e0c46Sdougm 	scf_simple_prop_t *prop;
21845b6e0c46Sdougm 
21855b6e0c46Sdougm 	if (handle == NULL)
21865b6e0c46Sdougm 		return (B_TRUE);
21875b6e0c46Sdougm 
21885b6e0c46Sdougm 	/*
21895b6e0c46Sdougm 	 * If sharetab has changed, then there was an external
21905b6e0c46Sdougm 	 * change. Check sharetab first since it is updated by ZFS as
21915b6e0c46Sdougm 	 * well as sharemgr.  This is where external ZFS changes are
21925b6e0c46Sdougm 	 * caught.
21935b6e0c46Sdougm 	 */
21945b6e0c46Sdougm 	if (stat(SA_LEGACY_SHARETAB, &st) == 0 &&
21955b6e0c46Sdougm 	    TSTAMP(st.st_mtim) != implhandle->tssharetab)
21965b6e0c46Sdougm 		return (B_TRUE);
21975b6e0c46Sdougm 
21985b6e0c46Sdougm 	/*
21995b6e0c46Sdougm 	 * If sharetab wasn't changed, check whether there were any
22005b6e0c46Sdougm 	 * SMF transactions that modified the config but didn't
22015b6e0c46Sdougm 	 * initiate a share.  This is less common but does happen.
22025b6e0c46Sdougm 	 */
22035b6e0c46Sdougm 	prop = scf_simple_prop_get(implhandle->scfhandle->handle,
22045b6e0c46Sdougm 	    (const char *)SA_SVC_FMRI_BASE ":default", "state",
22055b6e0c46Sdougm 	    "lastupdate");
22065b6e0c46Sdougm 	if (prop != NULL) {
22075b6e0c46Sdougm 		str = scf_simple_prop_next_astring(prop);
22085b6e0c46Sdougm 		if (str != NULL)
22095b6e0c46Sdougm 			tstamp = strtoull(str, NULL, 0);
22105b6e0c46Sdougm 		else
22115b6e0c46Sdougm 			tstamp = 0;
22125b6e0c46Sdougm 		scf_simple_prop_free(prop);
22135b6e0c46Sdougm 		if (tstamp != implhandle->tstrans)
22145b6e0c46Sdougm 			return (B_TRUE);
22155b6e0c46Sdougm 	}
22165b6e0c46Sdougm 
22175b6e0c46Sdougm 	return (B_FALSE);
22185b6e0c46Sdougm }
22195b6e0c46Sdougm 
2220da6c28aaSamw /*
2221da6c28aaSamw  * sa_fix_resource_name(path)
2222da6c28aaSamw  *
222389dc44ceSjose borrego  * Convert invalid characters in a resource name (SMB share name)
222489dc44ceSjose borrego  * to underscores ('_').  The list of invalid characters includes
222589dc44ceSjose borrego  * control characters and the following:
222689dc44ceSjose borrego  *
222789dc44ceSjose borrego  *	" / \ [ ] : | < > + ; , ? * =
222889dc44ceSjose borrego  *
222989dc44ceSjose borrego  * The caller must pass a valid path.  Leading and trailing slashes
223089dc44ceSjose borrego  * are stripped from the path before converting invalid characters.
223189dc44ceSjose borrego  * Resource names are restricted to SA_MAX_RESOURCE_NAME characters.
2232da6c28aaSamw  */
2233da6c28aaSamw void
2234da6c28aaSamw sa_fix_resource_name(char *path)
2235da6c28aaSamw {
223689dc44ceSjose borrego 	char *invalid = "\"/\\[]:|<>+;,?*=";
223789dc44ceSjose borrego 	char *p = path;
223889dc44ceSjose borrego 	char *q;
2239da6c28aaSamw 	size_t len;
2240da6c28aaSamw 
2241da6c28aaSamw 	assert(path != NULL);
2242da6c28aaSamw 
224389dc44ceSjose borrego 	/*
224489dc44ceSjose borrego 	 * Strip leading and trailing /'s.
224589dc44ceSjose borrego 	 */
224689dc44ceSjose borrego 	p += strspn(p, "/");
224789dc44ceSjose borrego 	q = strchr(p, '\0');
224889dc44ceSjose borrego 	if (q != NULL && q != path) {
224989dc44ceSjose borrego 		while ((--q, *q == '/'))
225089dc44ceSjose borrego 			*q = '\0';
2251da6c28aaSamw 	}
225289dc44ceSjose borrego 
225389dc44ceSjose borrego 	if (*p == '\0') {
225489dc44ceSjose borrego 		(void) strcpy(path, "_");
225589dc44ceSjose borrego 		return;
2256da6c28aaSamw 	}
2257da6c28aaSamw 
2258da6c28aaSamw 	/*
225989dc44ceSjose borrego 	 * Stride over path components until the remaining
226089dc44ceSjose borrego 	 * path is no longer than SA_MAX_RESOURCE_NAME.
2261da6c28aaSamw 	 */
226289dc44ceSjose borrego 	q = p;
226389dc44ceSjose borrego 	while ((q != NULL) && (strlen(q) > SA_MAX_RESOURCE_NAME)) {
226489dc44ceSjose borrego 		if ((q = strchr(q, '/')) != NULL) {
226589dc44ceSjose borrego 			++q;
226689dc44ceSjose borrego 			p = q;
2267da6c28aaSamw 		}
226889dc44ceSjose borrego 	}
226989dc44ceSjose borrego 
227089dc44ceSjose borrego 	/*
227189dc44ceSjose borrego 	 * If the path is still longer than SA_MAX_RESOURCE_NAME,
227289dc44ceSjose borrego 	 * take the trailing SA_MAX_RESOURCE_NAME characters.
227389dc44ceSjose borrego 	 */
227489dc44ceSjose borrego 	if ((len = strlen(p)) > SA_MAX_RESOURCE_NAME) {
227589dc44ceSjose borrego 		len = SA_MAX_RESOURCE_NAME;
227689dc44ceSjose borrego 		p = strchr(p, '\0') - (SA_MAX_RESOURCE_NAME - 1);
227789dc44ceSjose borrego 	}
227889dc44ceSjose borrego 
227989dc44ceSjose borrego 	(void) memmove(path, p, len);
228089dc44ceSjose borrego 	path[len] = '\0';
228189dc44ceSjose borrego 
228289dc44ceSjose borrego 	for (p = path; *p != '\0'; ++p) {
228389dc44ceSjose borrego 		if ((iscntrl(*p)) || strchr(invalid, *p))
228489dc44ceSjose borrego 			*p = '_';
2285da6c28aaSamw 	}
2286da6c28aaSamw }
2287