xref: /illumos-gate/usr/src/lib/libshare/common/libsharecore.c (revision 5600110389005ea5ace473e10a15c3e80a3edf97)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  * core library for common functions across all config store types
31  * and file systems to be exported. This includes legacy dfstab/sharetab
32  * parsing. Need to eliminate XML where possible.
33  */
34 
35 #include <stdio.h>
36 #include <string.h>
37 #include <ctype.h>
38 #include <unistd.h>
39 #include <limits.h>
40 #include <errno.h>
41 #include <sys/types.h>
42 #include <sys/stat.h>
43 #include <libxml/parser.h>
44 #include <libxml/tree.h>
45 #include "libshare.h"
46 #include "libshare_impl.h"
47 #include <fcntl.h>
48 #include <sys/stat.h>
49 #include <grp.h>
50 #include <limits.h>
51 #include <sys/param.h>
52 #include <signal.h>
53 #include <libintl.h>
54 
55 #include "sharetab.h"
56 
57 #define	DFSTAB_NOTICE_LINES	5
58 static char *notice[DFSTAB_NOTICE_LINES] =	{
59 	"# Do not modify this file directly.\n",
60 	"# Use the sharemgr(1m) command for all share management\n",
61 	"# This file is reconstructed and only maintained for backward\n",
62 	"# compatibility. Configuration lines could be lost.\n",
63 	"#\n"
64 };
65 
66 #define	STRNCAT(x, y, z)	(xmlChar *)strncat((char *)x, (char *)y, z)
67 
68 /* will be much smaller, but this handles bad syntax in the file */
69 #define	MAXARGSFORSHARE	256
70 
71 /* used internally only */
72 typedef
73 struct sharelist {
74     struct sharelist *next;
75     int   persist;
76     char *path;
77     char *resource;
78     char *fstype;
79     char *options;
80     char *description;
81     char *group;
82     char *origline;
83     int lineno;
84 } xfs_sharelist_t;
85 static void parse_dfstab(char *, xmlNodePtr);
86 extern char *get_token(char *);
87 static void dfs_free_list(xfs_sharelist_t *);
88 /* prototypes */
89 void getlegacyconfig(char *, xmlNodePtr *);
90 extern sa_share_t _sa_add_share(sa_group_t, char *, int, int *);
91 extern xmlNodePtr _sa_get_next_error(xmlNodePtr);
92 extern sa_group_t _sa_create_group(char *);
93 static void outdfstab(FILE *, xfs_sharelist_t *);
94 extern int _sa_remove_optionset(sa_optionset_t);
95 extern int set_node_share(void *, char *, char *);
96 extern void set_node_attr(void *, char *, char *);
97 
98 /*
99  * alloc_sharelist()
100  *
101  * allocator function to return an zfs_sharelist_t
102  */
103 
104 static xfs_sharelist_t *
105 alloc_sharelist()
106 {
107 	xfs_sharelist_t *item;
108 
109 	item = (xfs_sharelist_t *)malloc(sizeof (xfs_sharelist_t));
110 	if (item != NULL)
111 	    (void) memset(item, '\0', sizeof (xfs_sharelist_t));
112 	return (item);
113 }
114 
115 /*
116  * fix_notice(list)
117  *
118  * Look at the beginning of the current /etc/dfs/dfstab file and add
119  * the do not modify notice if it doesn't exist.
120  */
121 
122 static xfs_sharelist_t *
123 fix_notice(xfs_sharelist_t *list)
124 {
125 	xfs_sharelist_t *item, *prev;
126 	int i;
127 
128 	if (list == NULL) {
129 	    /* zero length dfstab */
130 	    list = alloc_sharelist();
131 	    if (list == NULL)
132 		return (NULL);
133 	    list->description = strdup("#\n");
134 	}
135 	if (list->path == NULL && list->description != NULL &&
136 	    strcmp(list->description, notice[0]) != 0) {
137 	    for (prev = NULL, i = 0; i < DFSTAB_NOTICE_LINES; i++) {
138 		item = alloc_sharelist();
139 		if (item != NULL) {
140 		    item->description = strdup(notice[i]);
141 		    if (prev == NULL) {
142 			item->next = list;
143 			prev = item;
144 			list = item;
145 		    } else {
146 			item->next = prev->next;
147 			prev->next = item;
148 			prev = item;
149 		    }
150 		}
151 	    }
152 	}
153 	return (list);
154 }
155 
156 /*
157  * getdfstab(dfs)
158  *
159  * Returns an zfs_sharelist_t list of lines from the dfstab file
160  * pointed to by the FILE pointer dfs. Each entry is parsed and the
161  * original line is also preserved. Used in parsing and updating the
162  * dfstab file.
163  */
164 
165 static xfs_sharelist_t *
166 getdfstab(FILE *dfs)
167 {
168 	char buff[_POSIX_ARG_MAX]; /* reasonable size given syntax of share */
169 	char *bp;
170 	char *token;
171 	char *args[MAXARGSFORSHARE];
172 	int argc;
173 	int c;
174 	static int line = 0;
175 	xfs_sharelist_t *item = NULL, *first = NULL, *last;
176 
177 	if (dfs != NULL) {
178 	    first = NULL;
179 	    line = 0;
180 	    while (fgets(buff, sizeof (buff), dfs) != NULL) {
181 		line++;
182 		bp = buff;
183 		if (buff[0] == '#') {
184 		    item = alloc_sharelist();
185 		    if (item != NULL) {
186 			/* if no path, then comment */
187 			item->lineno = line;
188 			item->description = strdup(buff);
189 			if (first == NULL) {
190 			    first = item;
191 			    last = item;
192 			} else {
193 			    last->next = item;
194 			    last = item;
195 			}
196 		    } else {
197 			break;
198 		    }
199 		    continue;
200 		} else if (buff[0] == '\n') {
201 		    continue;
202 		}
203 		optind = 1;
204 		item = alloc_sharelist();
205 		if (item == NULL) {
206 		    break;
207 		} else if (first == NULL) {
208 		    first = item;
209 		    last = item;
210 		} else {
211 		    last->next = item;
212 		    last = item;
213 		}
214 		item->lineno = line;
215 		item->origline = strdup(buff);
216 		(void) get_token(NULL); /* reset to new pointers */
217 		argc = 0;
218 		while ((token = get_token(bp)) != NULL) {
219 		    if (argc < MAXARGSFORSHARE) {
220 			args[argc++] = token;
221 		    }
222 		}
223 		while ((c = getopt(argc, args, "F:o:d:pg:")) != -1) {
224 		    switch (c) {
225 		    case 'p':
226 			item->persist = 1;
227 			break;
228 		    case 'F':
229 			item->fstype = strdup(optarg);
230 			break;
231 		    case 'o':
232 			item->options = strdup(optarg);
233 			break;
234 		    case 'd':
235 			item->description = strdup(optarg);
236 			break;
237 		    case 'g':
238 			item->group = strdup(optarg);
239 			break;
240 		    default:
241 			break;
242 		    }
243 		}
244 		if (optind < argc) {
245 		    item->path = strdup(args[optind]);
246 		    optind++;
247 		    if (optind < argc) {
248 			char *resource;
249 			char *optgroup;
250 			/* resource and/or groupname */
251 			resource = args[optind];
252 			optgroup = strchr(resource, '@');
253 			if (optgroup != NULL) {
254 			    *optgroup++ = '\0';
255 			}
256 			if (optgroup != NULL)
257 			    item->group = strdup(optgroup);
258 			if (resource != NULL && strlen(resource) > 0)
259 			    item->resource = strdup(resource);
260 		    }
261 		}
262 	    }
263 	    if (item != NULL && item->fstype == NULL)
264 		item->fstype = strdup("nfs"); /* this is the default */
265 	}
266 	first = fix_notice(first);
267 	return (first);
268 }
269 
270 /*
271  * finddfsentry(list, path)
272  *
273  * Look for path in the zfs_sharelist_t list and return the entry if it
274  * exists.
275  */
276 
277 static xfs_sharelist_t *
278 finddfsentry(xfs_sharelist_t *list, char *path)
279 {
280 	xfs_sharelist_t *item;
281 
282 	for (item = list; item != NULL; item = item->next) {
283 	    if (item->path != NULL && strcmp(item->path, path) == 0)
284 		return (item);
285 	}
286 	return (NULL);
287 }
288 
289 /*
290  * remdfsentry(list, path, proto)
291  *
292  * Remove the specified path (with protocol) from the list. This will
293  * remove it from dfstab when the file is rewritten.
294  */
295 
296 static xfs_sharelist_t *
297 remdfsentry(xfs_sharelist_t *list, char *path, char *proto)
298 {
299 	xfs_sharelist_t *item, *prev = NULL;
300 
301 
302 	for (item = prev = list; item != NULL; item = item->next) {
303 	    /* skip comment entry but don't lose it */
304 	    if (item->path == NULL) {
305 		prev = item;
306 		continue;
307 	    }
308 	    /* if proto is NULL, remove all protocols */
309 	    if (proto == NULL || (strcmp(item->path, path) == 0 &&
310 		(item->fstype != NULL && strcmp(item->fstype, proto) == 0)))
311 		break;
312 	    if (item->fstype == NULL &&
313 		(proto == NULL || strcmp(proto, "nfs") == 0))
314 		break;
315 	    prev = item;
316 	}
317 	if (item != NULL) {
318 	    if (item == prev) {
319 		list = item->next; /* this must be the first one */
320 	    } else {
321 		prev->next = item->next;
322 	    }
323 	    item->next = NULL;
324 	    dfs_free_list(item);
325 	}
326 	return (list);
327 }
328 
329 /*
330  * remdfsline(list, line)
331  *
332  * Remove the line specified from the list.
333  */
334 
335 static xfs_sharelist_t *
336 remdfsline(xfs_sharelist_t *list, char *line)
337 {
338 	xfs_sharelist_t *item, *prev = NULL;
339 
340 	for (item = prev = list; item != NULL; item = item->next) {
341 	    /* skip comment entry but don't lose it */
342 	    if (item->path == NULL) {
343 		prev = item;
344 		continue;
345 	    }
346 	    if (strcmp(item->origline, line) == 0) {
347 		break;
348 	    }
349 	    prev = item;
350 	}
351 	if (item != NULL) {
352 	    if (item == prev) {
353 		list = item->next; /* this must be the first one */
354 	    } else {
355 		prev->next = item->next;
356 	    }
357 	    item->next = NULL;
358 	    dfs_free_list(item);
359 	}
360 	return (list);
361 }
362 
363 /*
364  * adddfsentry(list, share, proto)
365  *
366  * Add an entry to the dfstab list for share (relative to proto). This
367  * is used to update dfstab for legacy purposes.
368  */
369 
370 static xfs_sharelist_t *
371 adddfsentry(xfs_sharelist_t *list, sa_share_t share, char *proto)
372 {
373 	xfs_sharelist_t *item, *tmp;
374 	sa_group_t parent;
375 	char *groupname;
376 
377 	item = alloc_sharelist();
378 	if (item != NULL) {
379 	    parent = sa_get_parent_group(share);
380 	    groupname = sa_get_group_attr(parent, "name");
381 	    if (strcmp(groupname, "default") == 0) {
382 		sa_free_attr_string(groupname);
383 		groupname = NULL;
384 	    }
385 	    item->path = sa_get_share_attr(share, "path");
386 	    item->resource = sa_get_share_attr(share, "resource");
387 	    item->group = groupname;
388 	    item->fstype = strdup(proto);
389 	    item->options = sa_proto_legacy_format(proto, share, 1);
390 	    if (item->options != NULL && strlen(item->options) == 0) {
391 		free(item->options);
392 		item->options = NULL;
393 	    }
394 	    item->description = sa_get_share_description(share);
395 	    if (item->description != NULL && strlen(item->description) == 0) {
396 		sa_free_share_description(item->description);
397 		item->description = NULL;
398 	    }
399 	    if (list == NULL) {
400 		list = item;
401 	    } else {
402 		for (tmp = list; tmp->next != NULL; tmp = tmp->next)
403 		    /* do nothing */;
404 		tmp->next = item;
405 	    }
406 	}
407 	return (list);
408 }
409 
410 /*
411  * outdfstab(dfstab, list)
412  *
413  * Output the list to dfstab making sure the file is truncated.
414  * Comments and errors are preserved.
415  */
416 
417 static void
418 outdfstab(FILE *dfstab, xfs_sharelist_t *list)
419 {
420 	xfs_sharelist_t *item;
421 
422 	(void) ftruncate(fileno(dfstab), 0);
423 
424 	for (item = list; item != NULL; item = item->next) {
425 	    if (item->path != NULL) {
426 		if (*item->path == '/')
427 		    (void) fprintf(dfstab, "share %s%s%s%s%s%s%s %s%s%s%s%s\n",
428 			    (item->fstype != NULL) ? "-F " : "",
429 			    (item->fstype != NULL) ? item->fstype : "",
430 			    (item->options != NULL) ? " -o " : "",
431 			    (item->options != NULL) ? item->options : "",
432 			    (item->description != NULL) ? " -d \"" : "",
433 			    (item->description != NULL) ?
434 				item->description : "",
435 			    (item->description != NULL) ? "\"" : "",
436 			    item->path,
437 			    ((item->resource != NULL) ||
438 				(item->group != NULL)) ? " " : "",
439 			    (item->resource != NULL) ? item->resource : "",
440 			    item->group != NULL ? "@" : "",
441 			    item->group != NULL ? item->group : "");
442 		else
443 		    (void) fprintf(dfstab, "%s", item->origline);
444 	    } else {
445 		if (item->description != NULL) {
446 		    (void) fprintf(dfstab, "%s", item->description);
447 		} else {
448 		    (void) fprintf(dfstab, "%s", item->origline);
449 		}
450 	    }
451 	}
452 }
453 
454 /*
455  * open_dfstab(file)
456  *
457  * Open the specified dfstab file. If the owner/group/perms are wrong,
458  * fix them.
459  */
460 
461 static FILE *
462 open_dfstab(char *file)
463 {
464 	struct group *grp;
465 	struct group group;
466 	char *buff;
467 	int grsize;
468 	FILE *dfstab;
469 
470 	dfstab = fopen(file, "r+");
471 	if (dfstab == NULL) {
472 	    dfstab = fopen(file, "w+");
473 	}
474 	if (dfstab != NULL) {
475 		grsize = sysconf(_SC_GETGR_R_SIZE_MAX);
476 		buff = malloc(grsize);
477 		if (buff != NULL)
478 		    grp = getgrnam_r(SA_DEFAULT_FILE_GRP, &group, buff, grsize);
479 		else
480 		    grp = getgrnam(SA_DEFAULT_FILE_GRP); /* take the risk */
481 		(void) fchmod(fileno(dfstab), 0644);
482 		(void) fchown(fileno(dfstab), 0,
483 				grp != NULL ? grp->gr_gid : 3);
484 		if (buff != NULL)
485 		    free(buff);
486 		rewind(dfstab);
487 	}
488 	return (dfstab);
489 }
490 
491 /*
492  * sa_comment_line(line, err)
493  *
494  * Add a comment to the dfstab file with err as a prefix to the
495  * original line.
496  */
497 
498 static void
499 sa_comment_line(char *line, char *err)
500 {
501 	FILE *dfstab;
502 	xfs_sharelist_t *list;
503 	sigset_t old, new;
504 
505 	dfstab = open_dfstab(SA_LEGACY_DFSTAB);
506 	if (dfstab != NULL) {
507 		(void) setvbuf(dfstab, NULL, _IOLBF, BUFSIZ * 8);
508 		(void) sigprocmask(SIG_BLOCK, NULL, &new);
509 		(void) sigaddset(&new, SIGHUP);
510 		(void) sigaddset(&new, SIGINT);
511 		(void) sigaddset(&new, SIGQUIT);
512 		(void) sigaddset(&new, SIGTSTP);
513 		(void) sigprocmask(SIG_SETMASK, &new, &old);
514 		(void) lockf(fileno(dfstab), F_LOCK, 0);
515 		list = getdfstab(dfstab);
516 		rewind(dfstab);
517 		/*
518 		 * don't ignore the return since the list could have
519 		 * gone to NULL if the file only had one line in it.
520 		 */
521 		list = remdfsline(list, line);
522 		outdfstab(dfstab, list);
523 		(void) fprintf(dfstab, "# Error: %s: %s", err, line);
524 		(void) fsync(fileno(dfstab));
525 		(void) lockf(fileno(dfstab), F_ULOCK, 0);
526 		(void) fclose(dfstab);
527 		(void) sigprocmask(SIG_SETMASK, &old, NULL);
528 		if (list != NULL)
529 		    dfs_free_list(list);
530 	}
531 }
532 
533 /*
534  * sa_delete_legacy(share)
535  *
536  * Delete the specified share from the legacy config file.
537  */
538 
539 int
540 sa_delete_legacy(sa_share_t share)
541 {
542 	FILE *dfstab;
543 	int err;
544 	int ret = SA_OK;
545 	xfs_sharelist_t *list;
546 	char *path;
547 	sa_optionset_t optionset;
548 	sa_group_t parent;
549 	sigset_t old, new;
550 
551 	dfstab = open_dfstab(SA_LEGACY_DFSTAB);
552 	if (dfstab != NULL) {
553 		(void) setvbuf(dfstab, NULL, _IOLBF, BUFSIZ * 8);
554 		(void) sigprocmask(SIG_BLOCK, NULL, &new);
555 		(void) sigaddset(&new, SIGHUP);
556 		(void) sigaddset(&new, SIGINT);
557 		(void) sigaddset(&new, SIGQUIT);
558 		(void) sigaddset(&new, SIGTSTP);
559 		(void) sigprocmask(SIG_SETMASK, &new, &old);
560 		path = sa_get_share_attr(share, "path");
561 		parent = sa_get_parent_group(share);
562 		if (parent != NULL) {
563 		    (void) lockf(fileno(dfstab), F_LOCK, 0);
564 		    list = getdfstab(dfstab);
565 		    rewind(dfstab);
566 		    for (optionset = sa_get_optionset(parent, NULL);
567 			optionset != NULL;
568 			optionset = sa_get_next_optionset(optionset)) {
569 			char *proto = sa_get_optionset_attr(optionset, "type");
570 			if (list != NULL && proto != NULL)
571 			    list = remdfsentry(list, path, proto);
572 			if (proto == NULL)
573 			    ret = SA_NO_MEMORY;
574 			/*
575 			 * may want to only do the dfstab if this call
576 			 * returns NOT IMPLEMENTED but it shouldn't
577 			 * hurt.
578 			 */
579 			if (ret == SA_OK) {
580 			    err = sa_proto_delete_legacy(proto, share);
581 			    if (err != SA_NOT_IMPLEMENTED)
582 				ret = err;
583 			}
584 			if (proto != NULL)
585 			    sa_free_attr_string(proto);
586 		    }
587 		    outdfstab(dfstab, list);
588 		    if (list != NULL)
589 			dfs_free_list(list);
590 		    (void) fflush(dfstab);
591 		    (void) lockf(fileno(dfstab), F_ULOCK, 0);
592 		}
593 		(void) fsync(fileno(dfstab));
594 		(void) sigprocmask(SIG_SETMASK, &old, NULL);
595 		(void) fclose(dfstab);
596 		sa_free_attr_string(path);
597 	} else {
598 		if (errno == EACCES || errno == EPERM) {
599 		    ret = SA_NO_PERMISSION;
600 		} else {
601 		    ret = SA_CONFIG_ERR;
602 		}
603 	}
604 	return (ret);
605 }
606 
607 /*
608  * sa_update_legacy(share, proto)
609  *
610  * There is an assumption that dfstab will be the most common form of
611  * legacy configuration file for shares, but not the only one. Because
612  * of that, dfstab handling is done in the main code with calls to
613  * this function and protocol specific calls to deal with formating
614  * options into dfstab/share compatible syntax. Since not everything
615  * will be dfstab, there is a provision for calling a protocol
616  * specific plugin interface that allows the protocol plugin to do its
617  * own legacy files and skip the dfstab update.
618  */
619 
620 int
621 sa_update_legacy(sa_share_t share, char *proto)
622 {
623 	FILE *dfstab;
624 	int ret = SA_OK;
625 	xfs_sharelist_t *list;
626 	char *path;
627 	sigset_t old, new;
628 	char *persist;
629 
630 	ret = sa_proto_update_legacy(proto, share);
631 	if (ret != SA_NOT_IMPLEMENTED)
632 	    return (ret);
633 	/* do the dfstab format */
634 	persist = sa_get_share_attr(share, "type");
635 	/*
636 	 * only update if the share is not transient -- no share type
637 	 * set or the type is not "transient".
638 	 */
639 	if (persist == NULL || strcmp(persist, "transient") != 0) {
640 	    dfstab = open_dfstab(SA_LEGACY_DFSTAB);
641 	    if (dfstab != NULL) {
642 		(void) setvbuf(dfstab, NULL, _IOLBF, BUFSIZ * 8);
643 		(void) sigprocmask(SIG_BLOCK, NULL, &new);
644 		(void) sigaddset(&new, SIGHUP);
645 		(void) sigaddset(&new, SIGINT);
646 		(void) sigaddset(&new, SIGQUIT);
647 		(void) sigaddset(&new, SIGTSTP);
648 		(void) sigprocmask(SIG_SETMASK, &new, &old);
649 		path = sa_get_share_attr(share, "path");
650 		(void) lockf(fileno(dfstab), F_LOCK, 0);
651 		list = getdfstab(dfstab);
652 		rewind(dfstab);
653 		if (list != NULL)
654 		    list = remdfsentry(list, path, proto);
655 		list = adddfsentry(list, share, proto);
656 		outdfstab(dfstab, list);
657 		(void) fflush(dfstab);
658 		(void) lockf(fileno(dfstab), F_ULOCK, 0);
659 		(void) fsync(fileno(dfstab));
660 		(void) sigprocmask(SIG_SETMASK, &old, NULL);
661 		(void) fclose(dfstab);
662 		sa_free_attr_string(path);
663 		if (list != NULL)
664 		    dfs_free_list(list);
665 	    } else {
666 		if (errno == EACCES || errno == EPERM) {
667 		    ret = SA_NO_PERMISSION;
668 		} else {
669 		    ret = SA_CONFIG_ERR;
670 		}
671 	    }
672 	}
673 	if (persist != NULL)
674 	    sa_free_attr_string(persist);
675 	return (ret);
676 }
677 
678 /*
679  * sa_is_security(optname, proto)
680  *
681  * Check to see if optname is a security (named optionset) specific
682  * property for the specified protocol.
683  */
684 
685 int
686 sa_is_security(char *optname, char *proto)
687 {
688 	int ret = 0;
689 	if (proto != NULL)
690 	    ret = sa_proto_security_prop(proto, optname);
691 	return (ret);
692 }
693 
694 /*
695  * add_syntax_comment(root, line, err, todfstab)
696  *
697  * add a comment to the document indicating a syntax error. If
698  * todfstab is set, write it back to the dfstab file as well.
699  */
700 
701 static void
702 add_syntax_comment(xmlNodePtr root, char *line, char *err, int todfstab)
703 {
704 	xmlNodePtr node;
705 
706 	node = xmlNewChild(root, NULL, (xmlChar *)"error", (xmlChar *)line);
707 	if (node != NULL) {
708 	    xmlSetProp(node, (xmlChar *)"type", (xmlChar *)err);
709 	}
710 	if (todfstab)
711 	    sa_comment_line(line, err);
712 }
713 
714 /*
715  * sa_is_share(object)
716  *
717  * returns true of the object is of type "share".
718  */
719 
720 int
721 sa_is_share(void *object)
722 {
723 	if (object != NULL) {
724 	    if (strcmp((char *)((xmlNodePtr)object)->name, "share") == 0)
725 		return (1);
726 	}
727 	return (0);
728 }
729 
730 /*
731  * _sa_remove_property(property)
732  *
733  * remove a property only from the document.
734  */
735 
736 static void
737 _sa_remove_property(sa_property_t property)
738 {
739 	xmlUnlinkNode((xmlNodePtr)property);
740 	xmlFreeNode((xmlNodePtr)property);
741 }
742 
743 /*
744  * sa_parse_legacy_options(group, options, proto)
745  *
746  * In order to support legacy configurations, we allow the protocol
747  * specific plugin to parse legacy syntax options (like those in
748  * /etc/dfs/dfstab). This adds a new optionset to the group (or
749  * share).
750  *
751  * Once the optionset has been created, we then get the derived
752  * optionset of the parent (options from the optionset of the parent
753  * and any parent it might have) and remove those from the created
754  * optionset. This avoids duplication of options.
755  */
756 
757 int
758 sa_parse_legacy_options(sa_group_t group, char *options, char *proto)
759 {
760 	int ret = SA_INVALID_PROTOCOL;
761 	sa_group_t parent;
762 	parent = sa_get_parent_group(group);
763 
764 	if (proto != NULL)
765 	    ret = sa_proto_legacy_opts(proto, group, options);
766 	/*
767 	 * if in a group, remove the inherited options and security
768 	 */
769 	if (ret == SA_OK) {
770 	    if (parent != NULL) {
771 		sa_optionset_t optionset;
772 		sa_property_t popt, prop;
773 		sa_optionset_t localoptions;
774 		/* find parent options to remove from child */
775 		optionset = sa_get_derived_optionset(parent, proto, 1);
776 		localoptions = sa_get_optionset(group, proto);
777 		if (optionset != NULL) {
778 		    for (popt = sa_get_property(optionset, NULL);
779 			popt != NULL;
780 			popt = sa_get_next_property(popt)) {
781 			char *tag;
782 			char *value1;
783 			char *value2;
784 
785 			tag = sa_get_property_attr(popt, "type");
786 			if (tag != NULL) {
787 			    prop = sa_get_property(localoptions, tag);
788 			    if (prop != NULL) {
789 				value1 = sa_get_property_attr(popt, "value");
790 				value2 = sa_get_property_attr(prop, "value");
791 				if (value1 != NULL && value2 != NULL &&
792 				    strcmp(value1, value2) == 0) {
793 				    /* remove the property from the child */
794 				    (void) _sa_remove_property(prop);
795 				}
796 				if (value1 != NULL)
797 				    sa_free_attr_string(value1);
798 				if (value2 != NULL)
799 				    sa_free_attr_string(value2);
800 			    }
801 			    sa_free_attr_string(tag);
802 			}
803 		    }
804 		    prop = sa_get_property(localoptions, NULL);
805 		    if (prop == NULL && sa_is_share(group)) {
806 			/*
807 			 * all properties removed so remove the
808 			 * optionset if it is on a share
809 			 */
810 			(void) _sa_remove_optionset(localoptions);
811 		    }
812 		    sa_free_derived_optionset(optionset);
813 		}
814 		/*
815 		 * need to remove security here. If there are no
816 		 * security options on the local group/share, don't
817 		 * bother since those are the only ones that would be
818 		 * affected.
819 		 */
820 		localoptions = sa_get_all_security_types(group, proto, 0);
821 		if (localoptions != NULL) {
822 		    for (prop = sa_get_property(localoptions, NULL);
823 			prop != NULL; prop = sa_get_next_property(prop)) {
824 			char *tag;
825 			sa_security_t security;
826 			tag = sa_get_property_attr(prop, "type");
827 			if (tag != NULL) {
828 			    security = sa_get_security(group, tag, proto);
829 			    sa_free_attr_string(tag);
830 			    for (popt = sa_get_property(security, NULL);
831 				popt != NULL;
832 				popt = sa_get_next_property(popt)) {
833 				char *value1;
834 				char *value2;
835 
836 				/* remove duplicates from this level */
837 				value1 = sa_get_property_attr(popt, "value");
838 				value2 = sa_get_property_attr(prop, "value");
839 				if (value1 != NULL && value2 != NULL &&
840 				    strcmp(value1, value2) == 0) {
841 				    /* remove the property from the child */
842 				    (void) _sa_remove_property(prop);
843 				}
844 				if (value1 != NULL)
845 				    sa_free_attr_string(value1);
846 				if (value2 != NULL)
847 				    sa_free_attr_string(value2);
848 			    }
849 			}
850 		    }
851 		    (void) sa_destroy_optionset(localoptions);
852 		}
853 	    }
854 	}
855 	return (ret);
856 }
857 
858 /*
859  * dfs_free_list(list)
860  *
861  * Free the data in each list entry of the list as well as freeing the
862  * entries themselves. We need to avoid memory leaks and don't want to
863  * dereference any NULL members.
864  */
865 
866 static void
867 dfs_free_list(xfs_sharelist_t *list)
868 {
869 	xfs_sharelist_t *entry;
870 	for (entry = list; entry != NULL; entry = list) {
871 	    if (entry->path != NULL)
872 		free(entry->path);
873 	    if (entry->resource != NULL)
874 		free(entry->resource);
875 	    if (entry->fstype != NULL)
876 		free(entry->fstype);
877 	    if (entry->options != NULL)
878 		free(entry->options);
879 	    if (entry->description != NULL)
880 		free(entry->description);
881 	    if (entry->origline != NULL)
882 		free(entry->origline);
883 	    if (entry->group != NULL)
884 		free(entry->group);
885 	    list = list->next;
886 	    free(entry);
887 	}
888 }
889 
890 /*
891  * parse_dfstab(dfstab, root)
892  *
893  * Open and read the existing dfstab, parsing each line and adding it
894  * to the internal configuration. Make sure syntax errors, etc are
895  * preserved as comments.
896  */
897 
898 static void
899 parse_dfstab(char *dfstab, xmlNodePtr root)
900 {
901 	sa_share_t share;
902 	sa_group_t group;
903 	sa_group_t sgroup = NULL;
904 	sa_group_t defgroup;
905 	xfs_sharelist_t *head, *list;
906 	int err;
907 	int defined_group;
908 	FILE *dfs;
909 	char *oldprops;
910 
911 	/* read the dfstab format file and fill in the doc tree */
912 
913 	dfs = fopen(dfstab, "r");
914 	if (dfs == NULL) {
915 	    return;
916 	}
917 
918 	defgroup = sa_get_group("default");
919 
920 	for (head = list = getdfstab(dfs);
921 		list != NULL;
922 		list = list->next) {
923 	    share = NULL;
924 	    group = NULL;
925 	    defined_group = 0;
926 	    err = 0;
927 
928 	    if (list->origline == NULL) {
929 		/*
930 		 * Comment line that we will likely skip.
931 		 * If the line has the syntax:
932 		 *	# error: string: string
933 		 * It should be preserved until manually deleted.
934 		 */
935 		if (list->description != NULL &&
936 		    strncmp(list->description, "# Error: ", 9) == 0) {
937 		    char *line;
938 		    char *error;
939 		    char *cmd;
940 		    line = strdup(list->description);
941 		    if (line != NULL) {
942 			error = line + 9;
943 			cmd = strchr(error, ':');
944 			if (cmd != NULL) {
945 			    int len;
946 			    *cmd = '\0';
947 			    cmd += 2;
948 			    len = strlen(cmd);
949 			    cmd[len - 1] = '\0';
950 			    add_syntax_comment(root, cmd, error, 0);
951 			}
952 			free(line);
953 		    }
954 		}
955 		continue;
956 	    }
957 	    if (list->path != NULL && strlen(list->path) > 0 &&
958 		*list->path == '/') {
959 		share = sa_find_share(list->path);
960 		if (share != NULL)
961 		    sgroup = sa_get_parent_group(share);
962 		else
963 		    sgroup = NULL;
964 	    } else {
965 		(void) printf(gettext("No share specified in dfstab: "
966 					"line %d: %s\n"),
967 			list->lineno, list->origline);
968 		add_syntax_comment(root, list->origline,
969 				    gettext("No share specified"),
970 				    1);
971 		continue;
972 	    }
973 	    if (list->group != NULL && strlen(list->group) > 0) {
974 		group = sa_get_group(list->group);
975 		defined_group = 1;
976 	    } else {
977 		group = defgroup;
978 	    }
979 	    if (defined_group && group == NULL) {
980 		(void) printf(gettext("Unknown group used in dfstab: "
981 					"line %d: %s\n"),
982 			list->lineno, list->origline);
983 		add_syntax_comment(root, list->origline,
984 				    gettext("Unknown group specified"), 1);
985 		continue;
986 	    }
987 	    if (group != NULL) {
988 		if (share == NULL) {
989 		    if (!defined_group && group == defgroup) {
990 			/* this is an OK add for legacy */
991 			share = sa_add_share(defgroup, list->path,
992 					SA_SHARE_PERMANENT | SA_SHARE_PARSER,
993 					&err);
994 			if (share != NULL) {
995 			    if (list->description != NULL &&
996 				strlen(list->description) > 0)
997 				(void) sa_set_share_description(share,
998 							    list->description);
999 			    if (list->options != NULL &&
1000 				strlen(list->options) > 0) {
1001 				(void) sa_parse_legacy_options(share,
1002 								list->options,
1003 								list->fstype);
1004 			    }
1005 			    if (list->resource != NULL)
1006 				(void) sa_set_share_attr(share, "resource",
1007 						    list->resource);
1008 			} else {
1009 			    (void) printf(gettext("Error in dfstab: "
1010 					    "line %d: %s\n"),
1011 				    list->lineno, list->origline);
1012 			    if (err != SA_BAD_PATH)
1013 				add_syntax_comment(root, list->origline,
1014 						gettext("Syntax"), 1);
1015 			    else
1016 				add_syntax_comment(root, list->origline,
1017 						gettext("Path"), 1);
1018 			    continue;
1019 			}
1020 		    }
1021 		} else {
1022 		    if (group != sgroup) {
1023 			(void) printf(gettext("Attempt to change"
1024 						"configuration in"
1025 						"dfstab: line %d: %s\n"),
1026 				list->lineno, list->origline);
1027 			add_syntax_comment(root, list->origline,
1028 				gettext("Attempt to change configuration"), 1);
1029 			continue;
1030 		    }
1031 		    /* its the same group but could have changed options */
1032 		    oldprops = sa_proto_legacy_format(list->fstype, share, 0);
1033 		    if (oldprops != NULL) {
1034 			if (list->options != NULL &&
1035 				strcmp(oldprops, list->options) != 0) {
1036 			    sa_optionset_t opts;
1037 			    sa_security_t secs;
1038 			    /* possibly different values */
1039 			    opts = sa_get_optionset((sa_group_t)share,
1040 							list->fstype);
1041 			    (void) sa_destroy_optionset(opts);
1042 			    for (secs = sa_get_security((sa_group_t)share,
1043 							NULL, list->fstype);
1044 				secs != NULL;
1045 				secs = sa_get_security((sa_group_t)share,
1046 							NULL, list->fstype)) {
1047 				(void) sa_destroy_security(secs);
1048 			    }
1049 			    (void) sa_parse_legacy_options(share,
1050 							    list->options,
1051 							    list->fstype);
1052 			}
1053 		    }
1054 		}
1055 	    } else {
1056 		/* shouldn't happen */
1057 		err = SA_CONFIG_ERR;
1058 	    }
1059 
1060 	}
1061 	dfs_free_list(head);
1062 }
1063 
1064 /*
1065  * legacy_removes(group, file)
1066  *
1067  * Find any shares that are "missing" from the legacy file. These
1068  * should be removed from the configuration since they are likely from
1069  * a legacy app or the admin modified the dfstab file directly. We
1070  * have to support this even if it is not the recommended way to do
1071  * things.
1072  */
1073 
1074 static void
1075 legacy_removes(sa_group_t group, char *file)
1076 {
1077 	sa_share_t share;
1078 	char *path;
1079 	xfs_sharelist_t *list, *item;
1080 	FILE *dfstab;
1081 
1082 	dfstab = fopen(file, "r");
1083 	if (dfstab != NULL) {
1084 	    list = getdfstab(dfstab);
1085 	    (void) fclose(dfstab);
1086 retry:
1087 	    for (share = sa_get_share(group, NULL); share != NULL;
1088 		share = sa_get_next_share(share)) {
1089 		/* now see if the share is in the dfstab file */
1090 		path = sa_get_share_attr(share, "path");
1091 		if (path != NULL) {
1092 		    item = finddfsentry(list, path);
1093 		    sa_free_attr_string(path);
1094 		    if (item == NULL) {
1095 			/* the share was removed this way */
1096 			(void) sa_remove_share(share);
1097 
1098 			/* start over since the list was broken */
1099 			goto retry;
1100 		    }
1101 		}
1102 	    }
1103 	    if (list != NULL)
1104 		dfs_free_list(list);
1105 	}
1106 }
1107 
1108 /*
1109  * getlegacyconfig(path, root)
1110  *
1111  * Parse dfstab and build the legacy configuration. This only gets
1112  * called when a change was detected.
1113  */
1114 
1115 void
1116 getlegacyconfig(char *path, xmlNodePtr *root)
1117 {
1118 	sa_group_t defgroup;
1119 
1120 	if (root != NULL) {
1121 	    if (*root == NULL)
1122 		*root = xmlNewNode(NULL, (xmlChar *)"sharecfg");
1123 	    if (*root != NULL) {
1124 		if (strcmp(path, SA_LEGACY_DFSTAB) == 0) {
1125 			/*
1126 			 * walk the default shares and find anything
1127 			 * missing.  we do this first to make sure it
1128 			 * is cleaned up since there may be legacy
1129 			 * code add/del via dfstab and we need to
1130 			 * cleanup SMF.
1131 			 */
1132 		    defgroup = sa_get_group("default");
1133 		    if (defgroup != NULL) {
1134 			legacy_removes(defgroup, path);
1135 		    }
1136 		    /* parse the dfstab and add anything new */
1137 		    parse_dfstab(path, *root);
1138 		}
1139 	    }
1140 	}
1141 }
1142 
1143 /*
1144  * get_share_list(&err)
1145  *
1146  * Get a linked list of all the shares on the system from
1147  * /etc/dfs/sharetab. This is partially copied from libfsmgt which we
1148  * can't use due to package dependencies.
1149  */
1150 static xfs_sharelist_t *
1151 get_share_list(int *errp)
1152 {
1153 	xfs_sharelist_t	*newp;
1154 	xfs_sharelist_t	*headp;
1155 	xfs_sharelist_t	*tailp;
1156 	FILE		*fp;
1157 
1158 	headp = NULL;
1159 	tailp = NULL;
1160 
1161 	if ((fp = fopen(SHARETAB, "r")) != NULL) {
1162 		struct share	*sharetab_entry;
1163 
1164 		while (getshare(fp, &sharetab_entry) > 0) {
1165 		    newp = alloc_sharelist();
1166 		    if (newp == NULL) {
1167 			goto err;
1168 		    }
1169 
1170 			/*
1171 			 * link into the list here so we don't leak
1172 			 * memory on a failure from strdup().
1173 			 */
1174 		    if (headp == NULL) {
1175 			headp = newp;
1176 			tailp = newp;
1177 		    } else {
1178 			tailp->next = newp;
1179 			tailp = newp;
1180 		    }
1181 
1182 		    newp->path = strdup(sharetab_entry->sh_path);
1183 		    if (newp->path == NULL)
1184 			goto err;
1185 		    newp->resource = strdup(sharetab_entry->sh_res);
1186 		    if (newp->resource == NULL)
1187 			goto err;
1188 		    newp->fstype = strdup(sharetab_entry->sh_fstype);
1189 		    if (newp->fstype == NULL)
1190 			goto err;
1191 		    newp->options = strdup(sharetab_entry->sh_opts);
1192 		    if (newp->options == NULL)
1193 			goto err;
1194 		    newp->description = strdup(sharetab_entry->sh_descr);
1195 		    if (newp->description == NULL)
1196 			goto err;
1197 		}
1198 		(void) fclose(fp);
1199 	} else {
1200 	    *errp = errno;
1201 	}
1202 
1203 	/*
1204 	 * Caller must free the mount list
1205 	 */
1206 	return (headp);
1207 err:
1208 	/*
1209 	 * Out of memory so cleanup and leave.
1210 	 */
1211 	dfs_free_list(headp);
1212 	(void) fclose(fp);
1213 	return (NULL);
1214 }
1215 
1216 /*
1217  * parse_sharetab(void)
1218  *
1219  * Read the /etc/dfs/sharetab file and see which entries don't exist
1220  * in the repository. These shares are marked transient.  We also need
1221  * to see if they are ZFS shares since ZFS bypasses the SMF
1222  * repository.
1223  */
1224 
1225 int
1226 parse_sharetab(void)
1227 {
1228 	xfs_sharelist_t *list, *tmplist;
1229 	int err = 0;
1230 	sa_share_t share;
1231 	sa_group_t group;
1232 	sa_group_t lgroup;
1233 	char *groupname;
1234 	int legacy = 0;
1235 
1236 	list = get_share_list(&err);
1237 	if (list == NULL)
1238 	    return (legacy);
1239 
1240 	lgroup = sa_get_group("default");
1241 
1242 	for (tmplist = list; tmplist != NULL; tmplist = tmplist->next) {
1243 	    group = NULL;
1244 	    share = sa_find_share(tmplist->path);
1245 	    if (share == NULL) {
1246 		/*
1247 		 * this share is transient so needs to be
1248 		 * added. Initially, this will be under
1249 		 * default(legacy) unless it is a ZFS
1250 		 * share. If zfs, we need a zfs group.
1251 		 */
1252 		if (tmplist->resource != NULL &&
1253 		    (groupname = strchr(tmplist->resource, '@')) != NULL) {
1254 		    /* there is a defined group */
1255 		    *groupname++ = '\0';
1256 		    group = sa_get_group(groupname);
1257 		    if (group != NULL) {
1258 			share = _sa_add_share(group, tmplist->path,
1259 						SA_SHARE_TRANSIENT, &err);
1260 		    } else {
1261 			/*
1262 			 * While this case shouldn't occur very often,
1263 			 * it does occur out of a "zfs set
1264 			 * sharenfs=off" when the dataset is also set
1265 			 * to canmount=off. A warning will then cause
1266 			 * the zfs command to abort. Since we add it
1267 			 * to the default list, everything works
1268 			 * properly anyway and the library doesn't
1269 			 * need to give a warning.
1270 			 */
1271 			share = _sa_add_share(lgroup, tmplist->path,
1272 						SA_SHARE_TRANSIENT, &err);
1273 		    }
1274 		} else {
1275 		    if (sa_zfs_is_shared(tmplist->path)) {
1276 			group = sa_get_group("zfs");
1277 			if (group == NULL) {
1278 			    group = sa_create_group("zfs", &err);
1279 			    if (group == NULL && err == SA_NO_PERMISSION) {
1280 				group = _sa_create_group("zfs");
1281 			    }
1282 			    if (group != NULL) {
1283 				(void) sa_create_optionset(group,
1284 							    tmplist->fstype);
1285 				(void) sa_set_group_attr(group, "zfs", "true");
1286 			    }
1287 			}
1288 			if (group != NULL) {
1289 			    share = _sa_add_share(group, tmplist->path,
1290 						    SA_SHARE_TRANSIENT, &err);
1291 			}
1292 		    } else {
1293 			share = _sa_add_share(lgroup, tmplist->path,
1294 						SA_SHARE_TRANSIENT, &err);
1295 		    }
1296 		}
1297 		if (share == NULL)
1298 		    (void) printf(gettext("Problem with transient: %s\n"),
1299 				    sa_errorstr(err));
1300 		if (share != NULL)
1301 		    set_node_attr(share, "shared", "true");
1302 
1303 		if (err == SA_OK) {
1304 		    if (tmplist->options != NULL &&
1305 			strlen(tmplist->options) > 0) {
1306 			(void) sa_parse_legacy_options(share,
1307 							tmplist->options,
1308 							tmplist->fstype);
1309 		    }
1310 		    if (tmplist->resource != NULL &&
1311 			strcmp(tmplist->resource, "-") != 0)
1312 			set_node_attr(share, "resource", tmplist->resource);
1313 		    if (tmplist->description != NULL) {
1314 			xmlNodePtr node;
1315 			node = xmlNewChild((xmlNodePtr)share, NULL,
1316 						(xmlChar *)"description", NULL);
1317 			xmlNodeSetContent(node,
1318 					    (xmlChar *)tmplist->description);
1319 		    }
1320 		    legacy = 1;
1321 		}
1322 	    } else {
1323 		/*
1324 		 * if this is a legacy share, mark as shared so we
1325 		 * only update sharetab appropriately. We also keep
1326 		 * the sharetab options in order to display for legacy
1327 		 * share with no arguments.
1328 		 */
1329 		set_node_attr(share, "shared", "true");
1330 		set_node_attr(share, "shareopts", tmplist->options);
1331 	    }
1332 	}
1333 	dfs_free_list(list);
1334 	return (legacy);
1335 }
1336 
1337 /*
1338  * get the transient shares from the sharetab (or other) file.  since
1339  * these are transient, they only appear in the working file and not
1340  * in a repository.
1341  */
1342 int
1343 gettransients(xmlNodePtr *root)
1344 {
1345 	int legacy = 0;
1346 
1347 	if (root != NULL) {
1348 	    if (*root == NULL)
1349 		*root = xmlNewNode(NULL, (xmlChar *)"sharecfg");
1350 	    if (*root != NULL) {
1351 		legacy = parse_sharetab();
1352 	    }
1353 	}
1354 	return (legacy);
1355 }
1356 
1357 /*
1358  * sa_has_prop(optionset, prop)
1359  *
1360  * Is the specified property a member of the optionset?
1361  */
1362 
1363 int
1364 sa_has_prop(sa_optionset_t optionset, sa_property_t prop)
1365 {
1366 	char *name;
1367 	sa_property_t otherprop;
1368 	int result = 0;
1369 
1370 	if (optionset != NULL) {
1371 	    name = sa_get_property_attr(prop, "type");
1372 	    if (name != NULL) {
1373 		otherprop = sa_get_property(optionset, name);
1374 		if (otherprop != NULL)
1375 		    result = 1;
1376 		sa_free_attr_string(name);
1377 	    }
1378 	}
1379 	return (result);
1380 }
1381 
1382 /*
1383  * Update legacy files
1384  *
1385  * Provides functions to add/remove/modify individual entries
1386  * in dfstab and sharetab
1387  */
1388 
1389 void
1390 update_legacy_config(void)
1391 {
1392 	/*
1393 	 * no longer used -- this is a placeholder in case we need to
1394 	 * add it back later.
1395 	 */
1396 }
1397 
1398 /*
1399  * sa_valid_property(object, proto, property)
1400  *
1401  * check to see if the specified property is valid relative to the
1402  * specified protocol. The protocol plugin is called to do the work.
1403  */
1404 
1405 int
1406 sa_valid_property(void *object, char *proto, sa_property_t property)
1407 {
1408 	int ret = SA_OK;
1409 
1410 	if (proto != NULL && property != NULL) {
1411 	    ret = sa_proto_valid_prop(proto, property, object);
1412 	}
1413 
1414 	return (ret);
1415 }
1416 
1417 /*
1418  * sa_fstype(path)
1419  *
1420  * Given path, return the string representing the path's file system
1421  * type. This is used to discover ZFS shares.
1422  */
1423 
1424 char *
1425 sa_fstype(char *path)
1426 {
1427 	int err;
1428 	struct stat st;
1429 
1430 	err = stat(path, &st);
1431 	if (err < 0) {
1432 	    err = SA_NO_SUCH_PATH;
1433 	} else {
1434 	    err = SA_OK;
1435 	}
1436 	if (err == SA_OK) {
1437 		/* have a valid path at this point */
1438 	    return (strdup(st.st_fstype));
1439 	}
1440 	return (NULL);
1441 }
1442 
1443 void
1444 sa_free_fstype(char *type)
1445 {
1446 	free(type);
1447 }
1448 
1449 /*
1450  * sa_get_derived_optionset(object, proto, hier)
1451  *
1452  *	Work backward to the top of the share object tree and start
1453  *	copying protocol specific optionsets into a newly created
1454  *	optionset that doesn't have a parent (it will be freed
1455  *	later). This provides for the property inheritence model. That
1456  *	is, properties closer to the share take precedence over group
1457  *	level. This also provides for groups of groups in the future.
1458  */
1459 
1460 sa_optionset_t
1461 sa_get_derived_optionset(void *object, char *proto, int hier)
1462 {
1463 	sa_optionset_t newoptionset;
1464 	sa_optionset_t optionset;
1465 	sa_group_t group;
1466 
1467 	if (hier &&
1468 	    (group = sa_get_parent_group((sa_share_t)object)) != NULL) {
1469 	    newoptionset = sa_get_derived_optionset((void *)group, proto, hier);
1470 	} else {
1471 	    newoptionset = (sa_optionset_t)xmlNewNode(NULL,
1472 						    (xmlChar *)"optionset");
1473 	    if (newoptionset != NULL) {
1474 		sa_set_optionset_attr(newoptionset, "type", proto);
1475 	    }
1476 	}
1477 	/* dont' do anything if memory wasn't allocated */
1478 	if (newoptionset == NULL)
1479 	    return (NULL);
1480 
1481 	/* found the top so working back down the stack */
1482 	optionset = sa_get_optionset((sa_optionset_t)object, proto);
1483 	if (optionset != NULL) {
1484 	    sa_property_t prop;
1485 	    /* add optionset to the newoptionset */
1486 	    for (prop = sa_get_property(optionset, NULL);
1487 		prop != NULL; prop = sa_get_next_property(prop)) {
1488 		sa_property_t newprop;
1489 		char *name;
1490 		char *value;
1491 		name = sa_get_property_attr(prop, "type");
1492 		value = sa_get_property_attr(prop, "value");
1493 		if (name != NULL) {
1494 		    newprop = sa_get_property(newoptionset, name);
1495 		    /* replace the value with the new value */
1496 		    if (newprop != NULL) {
1497 			/*
1498 			 * only set if value is non NULL, old value ok
1499 			 * if it is NULL.
1500 			 */
1501 			if (value != NULL)
1502 			    set_node_attr(newprop, "value", value);
1503 		    } else {
1504 			/* an entirely new property */
1505 			if (value != NULL) {
1506 			    newprop = sa_create_property(name, value);
1507 			    if (newprop != NULL) {
1508 				newprop = (sa_property_t)
1509 				    xmlAddChild((xmlNodePtr)newoptionset,
1510 						(xmlNodePtr)newprop);
1511 			    }
1512 			}
1513 		    }
1514 		    sa_free_attr_string(name);
1515 		}
1516 		if (value != NULL)
1517 		    sa_free_attr_string(value);
1518 	    }
1519 	}
1520 	return (newoptionset);
1521 }
1522 
1523 void
1524 sa_free_derived_optionset(sa_optionset_t optionset)
1525 {
1526 	/* while it shouldn't be linked, it doesn't hurt */
1527 	if (optionset != NULL) {
1528 	    xmlUnlinkNode((xmlNodePtr) optionset);
1529 	    xmlFreeNode((xmlNodePtr) optionset);
1530 	}
1531 }
1532 
1533 /*
1534  *  sa_get_all_security_types(object, proto, hier)
1535  *
1536  *	find all the security types set for this object.  This is
1537  *	preliminary to getting a derived security set. The return value is an
1538  *	optionset containg properties which are the sectype values found by
1539  *	walking up the XML document struture. The returned optionset
1540  *	is a derived optionset.
1541  *
1542  *	If hier is 0, only look at object. If non-zero, walk up the tree.
1543  */
1544 sa_optionset_t
1545 sa_get_all_security_types(void *object, char *proto, int hier)
1546 {
1547 	sa_optionset_t options;
1548 	sa_security_t security;
1549 	sa_group_t group;
1550 	sa_property_t prop;
1551 
1552 	options = NULL;
1553 
1554 	if (hier &&
1555 	    (group = sa_get_parent_group((sa_share_t)object)) != NULL) {
1556 	    options = sa_get_all_security_types((void *)group, proto, hier);
1557 	} else {
1558 	    options = (sa_optionset_t)xmlNewNode(NULL,
1559 						    (xmlChar *)"optionset");
1560 	}
1561 	/* hit the top so collect the security types working back */
1562 	if (options != NULL) {
1563 	    for (security = sa_get_security((sa_group_t)object, NULL, NULL);
1564 		security != NULL; security = sa_get_next_security(security)) {
1565 		char *type;
1566 		char *sectype;
1567 
1568 		type = sa_get_security_attr(security, "type");
1569 		if (type != NULL) {
1570 		    if (strcmp(type, proto) != 0) {
1571 			sa_free_attr_string(type);
1572 			continue;
1573 		    }
1574 		    sectype = sa_get_security_attr(security, "sectype");
1575 		    if (sectype != NULL) {
1576 			/*
1577 			 * have a security type, check to see if
1578 			 * already present in optionset and add if it
1579 			 * isn't.
1580 			 */
1581 			if (sa_get_property(options, sectype) == NULL) {
1582 			    prop = sa_create_property(sectype, "true");
1583 			    if (prop != NULL)
1584 				prop = (sa_property_t)
1585 				    xmlAddChild((xmlNodePtr)options,
1586 						(xmlNodePtr)prop);
1587 			}
1588 			sa_free_attr_string(sectype);
1589 		    }
1590 		    sa_free_attr_string(type);
1591 		}
1592 	    }
1593 	}
1594 	return (options);
1595 }
1596 
1597 /*
1598  * sa_get_derived_security(object, sectype, proto, hier)
1599  *
1600  * Get the derived security(named optionset) for the object given the
1601  * sectype and proto. If hier is non-zero, walk up the tree to get all
1602  * properties defined for this object, otherwise just those on the
1603  * object.
1604  */
1605 
1606 sa_security_t
1607 sa_get_derived_security(void *object, char *sectype, char *proto, int hier)
1608 {
1609 	sa_security_t newsecurity;
1610 	sa_security_t security;
1611 	sa_group_t group;
1612 
1613 	if (hier &&
1614 	    (group = sa_get_parent_group((sa_share_t)object)) != NULL) {
1615 	    newsecurity = sa_get_derived_security((void *)group,
1616 						    sectype, proto, hier);
1617 	} else {
1618 	    newsecurity = (sa_security_t)xmlNewNode(NULL,
1619 						    (xmlChar *)"security");
1620 	    if (newsecurity != NULL) {
1621 		sa_set_security_attr(newsecurity, "type", proto);
1622 		sa_set_security_attr(newsecurity, "sectype", sectype);
1623 	    }
1624 	}
1625 	/* dont' do anything if memory wasn't allocated */
1626 	if (newsecurity == NULL)
1627 	    return (NULL);
1628 
1629 	/* found the top so working back down the stack */
1630 	security = sa_get_security((sa_security_t)object, sectype, proto);
1631 	if (security != NULL) {
1632 	    sa_property_t prop;
1633 	    /* add security to the newsecurity */
1634 	    for (prop = sa_get_property(security, NULL);
1635 		prop != NULL; prop = sa_get_next_property(prop)) {
1636 		sa_property_t newprop;
1637 		char *name;
1638 		char *value;
1639 		name = sa_get_property_attr(prop, "type");
1640 		value = sa_get_property_attr(prop, "value");
1641 		if (name != NULL) {
1642 		    newprop = sa_get_property(newsecurity, name);
1643 		    /* replace the value with the new value */
1644 		    if (newprop != NULL) {
1645 			/*
1646 			 * only set if value is non NULL, old value ok
1647 			 * if it is NULL.
1648 			 */
1649 			if (value != NULL)
1650 			    set_node_attr(newprop, name, value);
1651 		    } else {
1652 			/* an entirely new property */
1653 			if (value != NULL) {
1654 			    newprop = sa_create_property(name, value);
1655 			    newprop = (sa_property_t)
1656 				xmlAddChild((xmlNodePtr)newsecurity,
1657 					    (xmlNodePtr)newprop);
1658 			}
1659 		    }
1660 		    sa_free_attr_string(name);
1661 		}
1662 		if (value != NULL)
1663 		    sa_free_attr_string(value);
1664 	    }
1665 	}
1666 	return (newsecurity);
1667 }
1668 
1669 void
1670 sa_free_derived_security(sa_security_t security)
1671 {
1672 	/* while it shouldn't be linked, it doesn't hurt */
1673 	if (security != NULL) {
1674 	    xmlUnlinkNode((xmlNodePtr)security);
1675 	    xmlFreeNode((xmlNodePtr)security);
1676 	}
1677 }
1678 
1679 /*
1680  * sharetab utility functions
1681  *
1682  * makes use of the original sharetab.c from fs.d/nfs/lib
1683  */
1684 
1685 /*
1686  * fillshare(share, proto, sh)
1687  *
1688  * Fill the struct share with values obtained from the share object.
1689  */
1690 static void
1691 fillshare(sa_share_t share, char *proto, struct share *sh)
1692 {
1693 	char *groupname = NULL;
1694 	char *value;
1695 	sa_group_t group;
1696 	char *buff;
1697 	char *zfs;
1698 
1699 	group = sa_get_parent_group(share);
1700 	if (group != NULL) {
1701 	    zfs = sa_get_group_attr(group, "zfs");
1702 	    groupname = sa_get_group_attr(group, "name");
1703 
1704 	    if (groupname != NULL &&
1705 		(strcmp(groupname, "default") == 0 || zfs != NULL)) {
1706 		/*
1707 		 * since the groupname is either "default" or the
1708 		 * group is a ZFS group, we don't want to keep
1709 		 * groupname. We do want it if it is any other type of
1710 		 * group.
1711 		 */
1712 		sa_free_attr_string(groupname);
1713 		groupname = NULL;
1714 	    }
1715 	    if (zfs != NULL)
1716 		sa_free_attr_string(zfs);
1717 	}
1718 
1719 	value = sa_get_share_attr(share, "path");
1720 	if (value != NULL) {
1721 	    sh->sh_path = strdup(value);
1722 	    sa_free_attr_string(value);
1723 	}
1724 
1725 	value = sa_get_share_attr(share, "resource");
1726 	if (value != NULL || groupname != NULL) {
1727 	    int len = 0;
1728 
1729 	    if (value != NULL)
1730 		len += strlen(value);
1731 	    if (groupname != NULL)
1732 		len += strlen(groupname);
1733 	    len += 3; /* worst case */
1734 	    buff = malloc(len);
1735 	    (void) snprintf(buff, len, "%s%s%s",
1736 		    (value != NULL && strlen(value) > 0) ? value : "-",
1737 		    groupname != NULL ? "@" : "",
1738 		    groupname != NULL ? groupname : "");
1739 	    sh->sh_res = buff;
1740 	    if (value != NULL)
1741 		sa_free_attr_string(value);
1742 	    if (groupname != NULL) {
1743 		sa_free_attr_string(groupname);
1744 		groupname = NULL;
1745 	    }
1746 	} else {
1747 	    sh->sh_res = strdup("-");
1748 	}
1749 
1750 	sh->sh_fstype = strdup(proto);
1751 	value = sa_proto_legacy_format(proto, share, 1);
1752 	if (value != NULL) {
1753 	    if (strlen(value) > 0)
1754 		sh->sh_opts = strdup(value);
1755 	    else
1756 		sh->sh_opts = strdup("rw");
1757 	    free(value);
1758 	} else
1759 	    sh->sh_opts = strdup("rw");
1760 
1761 	value = sa_get_share_description(share);
1762 	if (value != NULL) {
1763 	    sh->sh_descr = strdup(value);
1764 	    sa_free_share_description(value);
1765 	} else
1766 	    sh->sh_descr = strdup("");
1767 }
1768 
1769 /*
1770  * emptyshare(sh)
1771  *
1772  * Free the strings in the non-NULL members of sh.
1773  */
1774 
1775 static void
1776 emptyshare(struct share *sh)
1777 {
1778 	if (sh->sh_path != NULL)
1779 	    free(sh->sh_path);
1780 	sh->sh_path = NULL;
1781 	if (sh->sh_res != NULL)
1782 	    free(sh->sh_res);
1783 	sh->sh_res = NULL;
1784 	if (sh->sh_fstype != NULL)
1785 	    free(sh->sh_fstype);
1786 	sh->sh_fstype = NULL;
1787 	if (sh->sh_opts != NULL)
1788 	    free(sh->sh_opts);
1789 	sh->sh_opts = NULL;
1790 	if (sh->sh_descr != NULL)
1791 	    free(sh->sh_descr);
1792 	sh->sh_descr = NULL;
1793 }
1794 
1795 /*
1796  * sa_update_sharetab(share, proto)
1797  *
1798  * Update the sharetab file with info from the specified share.
1799  * This could be an update or add.
1800  */
1801 
1802 int
1803 sa_update_sharetab(sa_share_t share, char *proto)
1804 {
1805 	int ret = SA_OK;
1806 	struct share shtab;
1807 	char *path;
1808 	int logging = 0;
1809 	FILE *sharetab;
1810 	sigset_t old, new;
1811 
1812 	path = sa_get_share_attr(share, "path");
1813 	if (path != NULL) {
1814 	    (void) memset(&shtab, '\0', sizeof (shtab));
1815 	    sharetab = fopen(SA_LEGACY_SHARETAB, "r+");
1816 	    if (sharetab == NULL) {
1817 		sharetab = fopen(SA_LEGACY_SHARETAB, "w+");
1818 	    }
1819 	    if (sharetab != NULL) {
1820 		(void) setvbuf(sharetab, NULL, _IOLBF, BUFSIZ * 8);
1821 		(void) sigprocmask(SIG_BLOCK, NULL, &new);
1822 		(void) sigaddset(&new, SIGHUP);
1823 		(void) sigaddset(&new, SIGINT);
1824 		(void) sigaddset(&new, SIGQUIT);
1825 		(void) sigaddset(&new, SIGTSTP);
1826 		(void) sigprocmask(SIG_SETMASK, &new, &old);
1827 		(void) lockf(fileno(sharetab), F_LOCK, 0);
1828 		(void) remshare(sharetab, path, &logging);
1829 		/* fill in share structure and write it out */
1830 		(void) fillshare(share, proto, &shtab);
1831 		(void) putshare(sharetab, &shtab);
1832 		emptyshare(&shtab);
1833 		(void) fflush(sharetab);
1834 		(void) lockf(fileno(sharetab), F_ULOCK, 0);
1835 		(void) fsync(fileno(sharetab));
1836 		(void) sigprocmask(SIG_SETMASK, &old, NULL);
1837 		(void) fclose(sharetab);
1838 	    } else {
1839 		if (errno == EACCES || errno == EPERM) {
1840 		    ret = SA_NO_PERMISSION;
1841 		} else {
1842 		    ret = SA_CONFIG_ERR;
1843 		}
1844 	    }
1845 	    sa_free_attr_string(path);
1846 	}
1847 	return (ret);
1848 }
1849 
1850 /*
1851  * sa_delete_sharetab(path, proto)
1852  *
1853  * remove the specified share from sharetab.
1854  */
1855 
1856 int
1857 sa_delete_sharetab(char *path, char *proto)
1858 {
1859 	int ret = SA_OK;
1860 	int logging = 0;
1861 	FILE *sharetab;
1862 	sigset_t old, new;
1863 #ifdef lint
1864 	proto = proto;
1865 #endif
1866 
1867 	if (path != NULL) {
1868 	    sharetab = fopen(SA_LEGACY_SHARETAB, "r+");
1869 	    if (sharetab == NULL) {
1870 		sharetab = fopen(SA_LEGACY_SHARETAB, "w+");
1871 	    }
1872 	    if (sharetab != NULL) {
1873 		/* should block keyboard level signals around the lock */
1874 		(void) sigprocmask(SIG_BLOCK, NULL, &new);
1875 		(void) sigaddset(&new, SIGHUP);
1876 		(void) sigaddset(&new, SIGINT);
1877 		(void) sigaddset(&new, SIGQUIT);
1878 		(void) sigaddset(&new, SIGTSTP);
1879 		(void) sigprocmask(SIG_SETMASK, &new, &old);
1880 		(void) lockf(fileno(sharetab), F_LOCK, 0);
1881 		ret = remshare(sharetab, path, &logging);
1882 		(void) fflush(sharetab);
1883 		(void) lockf(fileno(sharetab), F_ULOCK, 0);
1884 		(void) fsync(fileno(sharetab));
1885 		(void) sigprocmask(SIG_SETMASK, &old, NULL);
1886 		(void) fclose(sharetab);
1887 	    } else {
1888 		if (errno == EACCES || errno == EPERM) {
1889 		    ret = SA_NO_PERMISSION;
1890 		} else {
1891 		    ret = SA_CONFIG_ERR;
1892 		}
1893 	    }
1894 	}
1895 	return (ret);
1896 }
1897