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(dgettext(TEXT_DOMAIN,
966 					"No share specified in dfstab: "
967 					"line %d: %s\n"),
968 			list->lineno, list->origline);
969 		add_syntax_comment(root, list->origline,
970 				    dgettext(TEXT_DOMAIN, "No share specified"),
971 				    1);
972 		continue;
973 	    }
974 	    if (list->group != NULL && strlen(list->group) > 0) {
975 		group = sa_get_group(list->group);
976 		defined_group = 1;
977 	    } else {
978 		group = defgroup;
979 	    }
980 	    if (defined_group && group == NULL) {
981 		(void) printf(dgettext(TEXT_DOMAIN,
982 					"Unknown group used in dfstab: "
983 					"line %d: %s\n"),
984 			list->lineno, list->origline);
985 		add_syntax_comment(root, list->origline,
986 				    dgettext(TEXT_DOMAIN,
987 						"Unknown group specified"), 1);
988 		continue;
989 	    }
990 	    if (group != NULL) {
991 		if (share == NULL) {
992 		    if (!defined_group && group == defgroup) {
993 			/* this is an OK add for legacy */
994 			share = sa_add_share(defgroup, list->path,
995 					SA_SHARE_PERMANENT | SA_SHARE_PARSER,
996 					&err);
997 			if (share != NULL) {
998 			    if (list->description != NULL &&
999 				strlen(list->description) > 0)
1000 				(void) sa_set_share_description(share,
1001 							    list->description);
1002 			    if (list->options != NULL &&
1003 				strlen(list->options) > 0) {
1004 				(void) sa_parse_legacy_options(share,
1005 								list->options,
1006 								list->fstype);
1007 			    }
1008 			    if (list->resource != NULL)
1009 				(void) sa_set_share_attr(share, "resource",
1010 						    list->resource);
1011 			} else {
1012 			    (void) printf(dgettext(TEXT_DOMAIN,
1013 					    "Error in dfstab: "
1014 					    "line %d: %s\n"),
1015 				    list->lineno, list->origline);
1016 			    if (err != SA_BAD_PATH)
1017 				add_syntax_comment(root, list->origline,
1018 						dgettext(TEXT_DOMAIN,
1019 							    "Syntax"), 1);
1020 			    else
1021 				add_syntax_comment(root, list->origline,
1022 						dgettext(TEXT_DOMAIN,
1023 							    "Path"), 1);
1024 			    continue;
1025 			}
1026 		    }
1027 		} else {
1028 		    if (group != sgroup) {
1029 			(void) printf(dgettext(TEXT_DOMAIN, "Attempt to change"
1030 						"configuration in"
1031 						"dfstab: line %d: %s\n"),
1032 				list->lineno, list->origline);
1033 			add_syntax_comment(root, list->origline,
1034 				dgettext(TEXT_DOMAIN,
1035 					    "Attempt to change configuration"),
1036 				1);
1037 			continue;
1038 		    }
1039 		    /* its the same group but could have changed options */
1040 		    oldprops = sa_proto_legacy_format(list->fstype, share, 0);
1041 		    if (oldprops != NULL) {
1042 			if (list->options != NULL &&
1043 				strcmp(oldprops, list->options) != 0) {
1044 			    sa_optionset_t opts;
1045 			    sa_security_t secs;
1046 			    /* possibly different values */
1047 			    opts = sa_get_optionset((sa_group_t)share,
1048 							list->fstype);
1049 			    (void) sa_destroy_optionset(opts);
1050 			    for (secs = sa_get_security((sa_group_t)share,
1051 							NULL, list->fstype);
1052 				secs != NULL;
1053 				secs = sa_get_security((sa_group_t)share,
1054 							NULL, list->fstype)) {
1055 				(void) sa_destroy_security(secs);
1056 			    }
1057 			    (void) sa_parse_legacy_options(share,
1058 							    list->options,
1059 							    list->fstype);
1060 			}
1061 		    }
1062 		}
1063 	    } else {
1064 		/* shouldn't happen */
1065 		err = SA_CONFIG_ERR;
1066 	    }
1067 
1068 	}
1069 	dfs_free_list(head);
1070 }
1071 
1072 /*
1073  * legacy_removes(group, file)
1074  *
1075  * Find any shares that are "missing" from the legacy file. These
1076  * should be removed from the configuration since they are likely from
1077  * a legacy app or the admin modified the dfstab file directly. We
1078  * have to support this even if it is not the recommended way to do
1079  * things.
1080  */
1081 
1082 static void
1083 legacy_removes(sa_group_t group, char *file)
1084 {
1085 	sa_share_t share;
1086 	char *path;
1087 	xfs_sharelist_t *list, *item;
1088 	FILE *dfstab;
1089 
1090 	dfstab = fopen(file, "r");
1091 	if (dfstab != NULL) {
1092 	    list = getdfstab(dfstab);
1093 	    (void) fclose(dfstab);
1094 retry:
1095 	    for (share = sa_get_share(group, NULL); share != NULL;
1096 		share = sa_get_next_share(share)) {
1097 		/* now see if the share is in the dfstab file */
1098 		path = sa_get_share_attr(share, "path");
1099 		if (path != NULL) {
1100 		    item = finddfsentry(list, path);
1101 		    sa_free_attr_string(path);
1102 		    if (item == NULL) {
1103 			/* the share was removed this way */
1104 			(void) sa_remove_share(share);
1105 
1106 			/* start over since the list was broken */
1107 			goto retry;
1108 		    }
1109 		}
1110 	    }
1111 	    if (list != NULL)
1112 		dfs_free_list(list);
1113 	}
1114 }
1115 
1116 /*
1117  * getlegacyconfig(path, root)
1118  *
1119  * Parse dfstab and build the legacy configuration. This only gets
1120  * called when a change was detected.
1121  */
1122 
1123 void
1124 getlegacyconfig(char *path, xmlNodePtr *root)
1125 {
1126 	sa_group_t defgroup;
1127 
1128 	if (root != NULL) {
1129 	    if (*root == NULL)
1130 		*root = xmlNewNode(NULL, (xmlChar *)"sharecfg");
1131 	    if (*root != NULL) {
1132 		if (strcmp(path, SA_LEGACY_DFSTAB) == 0) {
1133 			/*
1134 			 * walk the default shares and find anything
1135 			 * missing.  we do this first to make sure it
1136 			 * is cleaned up since there may be legacy
1137 			 * code add/del via dfstab and we need to
1138 			 * cleanup SMF.
1139 			 */
1140 		    defgroup = sa_get_group("default");
1141 		    if (defgroup != NULL) {
1142 			legacy_removes(defgroup, path);
1143 		    }
1144 		    /* parse the dfstab and add anything new */
1145 		    parse_dfstab(path, *root);
1146 		}
1147 	    }
1148 	}
1149 }
1150 
1151 /*
1152  * get_share_list(&err)
1153  *
1154  * Get a linked list of all the shares on the system from
1155  * /etc/dfs/sharetab. This is partially copied from libfsmgt which we
1156  * can't use due to package dependencies.
1157  */
1158 static xfs_sharelist_t *
1159 get_share_list(int *errp)
1160 {
1161 	xfs_sharelist_t	*newp;
1162 	xfs_sharelist_t	*headp;
1163 	xfs_sharelist_t	*tailp;
1164 	FILE		*fp;
1165 
1166 	headp = NULL;
1167 	tailp = NULL;
1168 
1169 	if ((fp = fopen(SHARETAB, "r")) != NULL) {
1170 		struct share	*sharetab_entry;
1171 
1172 		while (getshare(fp, &sharetab_entry) > 0) {
1173 		    newp = alloc_sharelist();
1174 		    if (newp == NULL) {
1175 			goto err;
1176 		    }
1177 
1178 			/*
1179 			 * link into the list here so we don't leak
1180 			 * memory on a failure from strdup().
1181 			 */
1182 		    if (headp == NULL) {
1183 			headp = newp;
1184 			tailp = newp;
1185 		    } else {
1186 			tailp->next = newp;
1187 			tailp = newp;
1188 		    }
1189 
1190 		    newp->path = strdup(sharetab_entry->sh_path);
1191 		    if (newp->path == NULL)
1192 			goto err;
1193 		    newp->resource = strdup(sharetab_entry->sh_res);
1194 		    if (newp->resource == NULL)
1195 			goto err;
1196 		    newp->fstype = strdup(sharetab_entry->sh_fstype);
1197 		    if (newp->fstype == NULL)
1198 			goto err;
1199 		    newp->options = strdup(sharetab_entry->sh_opts);
1200 		    if (newp->options == NULL)
1201 			goto err;
1202 		    newp->description = strdup(sharetab_entry->sh_descr);
1203 		    if (newp->description == NULL)
1204 			goto err;
1205 		}
1206 		(void) fclose(fp);
1207 	} else {
1208 	    *errp = errno;
1209 	}
1210 
1211 	/*
1212 	 * Caller must free the mount list
1213 	 */
1214 	return (headp);
1215 err:
1216 	/*
1217 	 * Out of memory so cleanup and leave.
1218 	 */
1219 	dfs_free_list(headp);
1220 	(void) fclose(fp);
1221 	return (NULL);
1222 }
1223 
1224 /*
1225  * parse_sharetab(void)
1226  *
1227  * Read the /etc/dfs/sharetab file and see which entries don't exist
1228  * in the repository. These shares are marked transient.  We also need
1229  * to see if they are ZFS shares since ZFS bypasses the SMF
1230  * repository.
1231  */
1232 
1233 int
1234 parse_sharetab(void)
1235 {
1236 	xfs_sharelist_t *list, *tmplist;
1237 	int err = 0;
1238 	sa_share_t share;
1239 	sa_group_t group;
1240 	sa_group_t lgroup;
1241 	char *groupname;
1242 	int legacy = 0;
1243 
1244 	list = get_share_list(&err);
1245 	if (list == NULL)
1246 	    return (legacy);
1247 
1248 	lgroup = sa_get_group("default");
1249 
1250 	for (tmplist = list; tmplist != NULL; tmplist = tmplist->next) {
1251 	    group = NULL;
1252 	    share = sa_find_share(tmplist->path);
1253 	    if (share == NULL) {
1254 		/*
1255 		 * this share is transient so needs to be
1256 		 * added. Initially, this will be under
1257 		 * default(legacy) unless it is a ZFS
1258 		 * share. If zfs, we need a zfs group.
1259 		 */
1260 		if (tmplist->resource != NULL &&
1261 		    (groupname = strchr(tmplist->resource, '@')) != NULL) {
1262 		    /* there is a defined group */
1263 		    *groupname++ = '\0';
1264 		    group = sa_get_group(groupname);
1265 		    if (group != NULL) {
1266 			share = _sa_add_share(group, tmplist->path,
1267 						SA_SHARE_TRANSIENT, &err);
1268 		    } else {
1269 			/*
1270 			 * While this case shouldn't occur very often,
1271 			 * it does occur out of a "zfs set
1272 			 * sharenfs=off" when the dataset is also set
1273 			 * to canmount=off. A warning will then cause
1274 			 * the zfs command to abort. Since we add it
1275 			 * to the default list, everything works
1276 			 * properly anyway and the library doesn't
1277 			 * need to give a warning.
1278 			 */
1279 			share = _sa_add_share(lgroup, tmplist->path,
1280 						SA_SHARE_TRANSIENT, &err);
1281 		    }
1282 		} else {
1283 		    if (sa_zfs_is_shared(tmplist->path)) {
1284 			group = sa_get_group("zfs");
1285 			if (group == NULL) {
1286 			    group = sa_create_group("zfs", &err);
1287 			    if (group == NULL && err == SA_NO_PERMISSION) {
1288 				group = _sa_create_group("zfs");
1289 			    }
1290 			    if (group != NULL) {
1291 				(void) sa_create_optionset(group,
1292 							    tmplist->fstype);
1293 				(void) sa_set_group_attr(group, "zfs", "true");
1294 			    }
1295 			}
1296 			if (group != NULL) {
1297 			    share = _sa_add_share(group, tmplist->path,
1298 						    SA_SHARE_TRANSIENT, &err);
1299 			}
1300 		    } else {
1301 			share = _sa_add_share(lgroup, tmplist->path,
1302 						SA_SHARE_TRANSIENT, &err);
1303 		    }
1304 		}
1305 		if (share == NULL)
1306 		    (void) printf(dgettext(TEXT_DOMAIN,
1307 					    "Problem with transient: %s\n"),
1308 				    sa_errorstr(err));
1309 		if (share != NULL)
1310 		    set_node_attr(share, "shared", "true");
1311 
1312 		if (err == SA_OK) {
1313 		    if (tmplist->options != NULL &&
1314 			strlen(tmplist->options) > 0) {
1315 			(void) sa_parse_legacy_options(share,
1316 							tmplist->options,
1317 							tmplist->fstype);
1318 		    }
1319 		    if (tmplist->resource != NULL &&
1320 			strcmp(tmplist->resource, "-") != 0)
1321 			set_node_attr(share, "resource", tmplist->resource);
1322 		    if (tmplist->description != NULL) {
1323 			xmlNodePtr node;
1324 			node = xmlNewChild((xmlNodePtr)share, NULL,
1325 						(xmlChar *)"description", NULL);
1326 			xmlNodeSetContent(node,
1327 					    (xmlChar *)tmplist->description);
1328 		    }
1329 		    legacy = 1;
1330 		}
1331 	    } else {
1332 		/*
1333 		 * if this is a legacy share, mark as shared so we
1334 		 * only update sharetab appropriately. We also keep
1335 		 * the sharetab options in order to display for legacy
1336 		 * share with no arguments.
1337 		 */
1338 		set_node_attr(share, "shared", "true");
1339 		set_node_attr(share, "shareopts", tmplist->options);
1340 	    }
1341 	}
1342 	dfs_free_list(list);
1343 	return (legacy);
1344 }
1345 
1346 /*
1347  * get the transient shares from the sharetab (or other) file.  since
1348  * these are transient, they only appear in the working file and not
1349  * in a repository.
1350  */
1351 int
1352 gettransients(xmlNodePtr *root)
1353 {
1354 	int legacy = 0;
1355 
1356 	if (root != NULL) {
1357 	    if (*root == NULL)
1358 		*root = xmlNewNode(NULL, (xmlChar *)"sharecfg");
1359 	    if (*root != NULL) {
1360 		legacy = parse_sharetab();
1361 	    }
1362 	}
1363 	return (legacy);
1364 }
1365 
1366 /*
1367  * sa_has_prop(optionset, prop)
1368  *
1369  * Is the specified property a member of the optionset?
1370  */
1371 
1372 int
1373 sa_has_prop(sa_optionset_t optionset, sa_property_t prop)
1374 {
1375 	char *name;
1376 	sa_property_t otherprop;
1377 	int result = 0;
1378 
1379 	if (optionset != NULL) {
1380 	    name = sa_get_property_attr(prop, "type");
1381 	    if (name != NULL) {
1382 		otherprop = sa_get_property(optionset, name);
1383 		if (otherprop != NULL)
1384 		    result = 1;
1385 		sa_free_attr_string(name);
1386 	    }
1387 	}
1388 	return (result);
1389 }
1390 
1391 /*
1392  * Update legacy files
1393  *
1394  * Provides functions to add/remove/modify individual entries
1395  * in dfstab and sharetab
1396  */
1397 
1398 void
1399 update_legacy_config(void)
1400 {
1401 	/*
1402 	 * no longer used -- this is a placeholder in case we need to
1403 	 * add it back later.
1404 	 */
1405 }
1406 
1407 /*
1408  * sa_valid_property(object, proto, property)
1409  *
1410  * check to see if the specified property is valid relative to the
1411  * specified protocol. The protocol plugin is called to do the work.
1412  */
1413 
1414 int
1415 sa_valid_property(void *object, char *proto, sa_property_t property)
1416 {
1417 	int ret = SA_OK;
1418 
1419 	if (proto != NULL && property != NULL) {
1420 	    ret = sa_proto_valid_prop(proto, property, object);
1421 	}
1422 
1423 	return (ret);
1424 }
1425 
1426 /*
1427  * sa_fstype(path)
1428  *
1429  * Given path, return the string representing the path's file system
1430  * type. This is used to discover ZFS shares.
1431  */
1432 
1433 char *
1434 sa_fstype(char *path)
1435 {
1436 	int err;
1437 	struct stat st;
1438 
1439 	err = stat(path, &st);
1440 	if (err < 0) {
1441 	    err = SA_NO_SUCH_PATH;
1442 	} else {
1443 	    err = SA_OK;
1444 	}
1445 	if (err == SA_OK) {
1446 		/* have a valid path at this point */
1447 	    return (strdup(st.st_fstype));
1448 	}
1449 	return (NULL);
1450 }
1451 
1452 void
1453 sa_free_fstype(char *type)
1454 {
1455 	free(type);
1456 }
1457 
1458 /*
1459  * sa_get_derived_optionset(object, proto, hier)
1460  *
1461  *	Work backward to the top of the share object tree and start
1462  *	copying protocol specific optionsets into a newly created
1463  *	optionset that doesn't have a parent (it will be freed
1464  *	later). This provides for the property inheritence model. That
1465  *	is, properties closer to the share take precedence over group
1466  *	level. This also provides for groups of groups in the future.
1467  */
1468 
1469 sa_optionset_t
1470 sa_get_derived_optionset(void *object, char *proto, int hier)
1471 {
1472 	sa_optionset_t newoptionset;
1473 	sa_optionset_t optionset;
1474 	sa_group_t group;
1475 
1476 	if (hier &&
1477 	    (group = sa_get_parent_group((sa_share_t)object)) != NULL) {
1478 	    newoptionset = sa_get_derived_optionset((void *)group, proto, hier);
1479 	} else {
1480 	    newoptionset = (sa_optionset_t)xmlNewNode(NULL,
1481 						    (xmlChar *)"optionset");
1482 	    if (newoptionset != NULL) {
1483 		sa_set_optionset_attr(newoptionset, "type", proto);
1484 	    }
1485 	}
1486 	/* dont' do anything if memory wasn't allocated */
1487 	if (newoptionset == NULL)
1488 	    return (NULL);
1489 
1490 	/* found the top so working back down the stack */
1491 	optionset = sa_get_optionset((sa_optionset_t)object, proto);
1492 	if (optionset != NULL) {
1493 	    sa_property_t prop;
1494 	    /* add optionset to the newoptionset */
1495 	    for (prop = sa_get_property(optionset, NULL);
1496 		prop != NULL; prop = sa_get_next_property(prop)) {
1497 		sa_property_t newprop;
1498 		char *name;
1499 		char *value;
1500 		name = sa_get_property_attr(prop, "type");
1501 		value = sa_get_property_attr(prop, "value");
1502 		if (name != NULL) {
1503 		    newprop = sa_get_property(newoptionset, name);
1504 		    /* replace the value with the new value */
1505 		    if (newprop != NULL) {
1506 			/*
1507 			 * only set if value is non NULL, old value ok
1508 			 * if it is NULL.
1509 			 */
1510 			if (value != NULL)
1511 			    set_node_attr(newprop, "value", value);
1512 		    } else {
1513 			/* an entirely new property */
1514 			if (value != NULL) {
1515 			    newprop = sa_create_property(name, value);
1516 			    if (newprop != NULL) {
1517 				newprop = (sa_property_t)
1518 				    xmlAddChild((xmlNodePtr)newoptionset,
1519 						(xmlNodePtr)newprop);
1520 			    }
1521 			}
1522 		    }
1523 		    sa_free_attr_string(name);
1524 		}
1525 		if (value != NULL)
1526 		    sa_free_attr_string(value);
1527 	    }
1528 	}
1529 	return (newoptionset);
1530 }
1531 
1532 void
1533 sa_free_derived_optionset(sa_optionset_t optionset)
1534 {
1535 	/* while it shouldn't be linked, it doesn't hurt */
1536 	if (optionset != NULL) {
1537 	    xmlUnlinkNode((xmlNodePtr) optionset);
1538 	    xmlFreeNode((xmlNodePtr) optionset);
1539 	}
1540 }
1541 
1542 /*
1543  *  sa_get_all_security_types(object, proto, hier)
1544  *
1545  *	find all the security types set for this object.  This is
1546  *	preliminary to getting a derived security set. The return value is an
1547  *	optionset containg properties which are the sectype values found by
1548  *	walking up the XML document struture. The returned optionset
1549  *	is a derived optionset.
1550  *
1551  *	If hier is 0, only look at object. If non-zero, walk up the tree.
1552  */
1553 sa_optionset_t
1554 sa_get_all_security_types(void *object, char *proto, int hier)
1555 {
1556 	sa_optionset_t options;
1557 	sa_security_t security;
1558 	sa_group_t group;
1559 	sa_property_t prop;
1560 
1561 	options = NULL;
1562 
1563 	if (hier &&
1564 	    (group = sa_get_parent_group((sa_share_t)object)) != NULL) {
1565 	    options = sa_get_all_security_types((void *)group, proto, hier);
1566 	} else {
1567 	    options = (sa_optionset_t)xmlNewNode(NULL,
1568 						    (xmlChar *)"optionset");
1569 	}
1570 	/* hit the top so collect the security types working back */
1571 	if (options != NULL) {
1572 	    for (security = sa_get_security((sa_group_t)object, NULL, NULL);
1573 		security != NULL; security = sa_get_next_security(security)) {
1574 		char *type;
1575 		char *sectype;
1576 
1577 		type = sa_get_security_attr(security, "type");
1578 		if (type != NULL) {
1579 		    if (strcmp(type, proto) != 0) {
1580 			sa_free_attr_string(type);
1581 			continue;
1582 		    }
1583 		    sectype = sa_get_security_attr(security, "sectype");
1584 		    if (sectype != NULL) {
1585 			/*
1586 			 * have a security type, check to see if
1587 			 * already present in optionset and add if it
1588 			 * isn't.
1589 			 */
1590 			if (sa_get_property(options, sectype) == NULL) {
1591 			    prop = sa_create_property(sectype, "true");
1592 			    if (prop != NULL)
1593 				prop = (sa_property_t)
1594 				    xmlAddChild((xmlNodePtr)options,
1595 						(xmlNodePtr)prop);
1596 			}
1597 			sa_free_attr_string(sectype);
1598 		    }
1599 		    sa_free_attr_string(type);
1600 		}
1601 	    }
1602 	}
1603 	return (options);
1604 }
1605 
1606 /*
1607  * sa_get_derived_security(object, sectype, proto, hier)
1608  *
1609  * Get the derived security(named optionset) for the object given the
1610  * sectype and proto. If hier is non-zero, walk up the tree to get all
1611  * properties defined for this object, otherwise just those on the
1612  * object.
1613  */
1614 
1615 sa_security_t
1616 sa_get_derived_security(void *object, char *sectype, char *proto, int hier)
1617 {
1618 	sa_security_t newsecurity;
1619 	sa_security_t security;
1620 	sa_group_t group;
1621 
1622 	if (hier &&
1623 	    (group = sa_get_parent_group((sa_share_t)object)) != NULL) {
1624 	    newsecurity = sa_get_derived_security((void *)group,
1625 						    sectype, proto, hier);
1626 	} else {
1627 	    newsecurity = (sa_security_t)xmlNewNode(NULL,
1628 						    (xmlChar *)"security");
1629 	    if (newsecurity != NULL) {
1630 		sa_set_security_attr(newsecurity, "type", proto);
1631 		sa_set_security_attr(newsecurity, "sectype", sectype);
1632 	    }
1633 	}
1634 	/* dont' do anything if memory wasn't allocated */
1635 	if (newsecurity == NULL)
1636 	    return (NULL);
1637 
1638 	/* found the top so working back down the stack */
1639 	security = sa_get_security((sa_security_t)object, sectype, proto);
1640 	if (security != NULL) {
1641 	    sa_property_t prop;
1642 	    /* add security to the newsecurity */
1643 	    for (prop = sa_get_property(security, NULL);
1644 		prop != NULL; prop = sa_get_next_property(prop)) {
1645 		sa_property_t newprop;
1646 		char *name;
1647 		char *value;
1648 		name = sa_get_property_attr(prop, "type");
1649 		value = sa_get_property_attr(prop, "value");
1650 		if (name != NULL) {
1651 		    newprop = sa_get_property(newsecurity, name);
1652 		    /* replace the value with the new value */
1653 		    if (newprop != NULL) {
1654 			/*
1655 			 * only set if value is non NULL, old value ok
1656 			 * if it is NULL.
1657 			 */
1658 			if (value != NULL)
1659 			    set_node_attr(newprop, name, value);
1660 		    } else {
1661 			/* an entirely new property */
1662 			if (value != NULL) {
1663 			    newprop = sa_create_property(name, value);
1664 			    newprop = (sa_property_t)
1665 				xmlAddChild((xmlNodePtr)newsecurity,
1666 					    (xmlNodePtr)newprop);
1667 			}
1668 		    }
1669 		    sa_free_attr_string(name);
1670 		}
1671 		if (value != NULL)
1672 		    sa_free_attr_string(value);
1673 	    }
1674 	}
1675 	return (newsecurity);
1676 }
1677 
1678 void
1679 sa_free_derived_security(sa_security_t security)
1680 {
1681 	/* while it shouldn't be linked, it doesn't hurt */
1682 	if (security != NULL) {
1683 	    xmlUnlinkNode((xmlNodePtr)security);
1684 	    xmlFreeNode((xmlNodePtr)security);
1685 	}
1686 }
1687 
1688 /*
1689  * sharetab utility functions
1690  *
1691  * makes use of the original sharetab.c from fs.d/nfs/lib
1692  */
1693 
1694 /*
1695  * fillshare(share, proto, sh)
1696  *
1697  * Fill the struct share with values obtained from the share object.
1698  */
1699 static void
1700 fillshare(sa_share_t share, char *proto, struct share *sh)
1701 {
1702 	char *groupname = NULL;
1703 	char *value;
1704 	sa_group_t group;
1705 	char *buff;
1706 	char *zfs;
1707 
1708 	group = sa_get_parent_group(share);
1709 	if (group != NULL) {
1710 	    zfs = sa_get_group_attr(group, "zfs");
1711 	    groupname = sa_get_group_attr(group, "name");
1712 
1713 	    if (groupname != NULL &&
1714 		(strcmp(groupname, "default") == 0 || zfs != NULL)) {
1715 		/*
1716 		 * since the groupname is either "default" or the
1717 		 * group is a ZFS group, we don't want to keep
1718 		 * groupname. We do want it if it is any other type of
1719 		 * group.
1720 		 */
1721 		sa_free_attr_string(groupname);
1722 		groupname = NULL;
1723 	    }
1724 	    if (zfs != NULL)
1725 		sa_free_attr_string(zfs);
1726 	}
1727 
1728 	value = sa_get_share_attr(share, "path");
1729 	if (value != NULL) {
1730 	    sh->sh_path = strdup(value);
1731 	    sa_free_attr_string(value);
1732 	}
1733 
1734 	value = sa_get_share_attr(share, "resource");
1735 	if (value != NULL || groupname != NULL) {
1736 	    int len = 0;
1737 
1738 	    if (value != NULL)
1739 		len += strlen(value);
1740 	    if (groupname != NULL)
1741 		len += strlen(groupname);
1742 	    len += 3; /* worst case */
1743 	    buff = malloc(len);
1744 	    (void) snprintf(buff, len, "%s%s%s",
1745 		    (value != NULL && strlen(value) > 0) ? value : "-",
1746 		    groupname != NULL ? "@" : "",
1747 		    groupname != NULL ? groupname : "");
1748 	    sh->sh_res = buff;
1749 	    if (value != NULL)
1750 		sa_free_attr_string(value);
1751 	    if (groupname != NULL) {
1752 		sa_free_attr_string(groupname);
1753 		groupname = NULL;
1754 	    }
1755 	} else {
1756 	    sh->sh_res = strdup("-");
1757 	}
1758 
1759 	sh->sh_fstype = strdup(proto);
1760 	value = sa_proto_legacy_format(proto, share, 1);
1761 	if (value != NULL) {
1762 	    if (strlen(value) > 0)
1763 		sh->sh_opts = strdup(value);
1764 	    else
1765 		sh->sh_opts = strdup("rw");
1766 	    free(value);
1767 	} else
1768 	    sh->sh_opts = strdup("rw");
1769 
1770 	value = sa_get_share_description(share);
1771 	if (value != NULL) {
1772 	    sh->sh_descr = strdup(value);
1773 	    sa_free_share_description(value);
1774 	} else
1775 	    sh->sh_descr = strdup("");
1776 }
1777 
1778 /*
1779  * emptyshare(sh)
1780  *
1781  * Free the strings in the non-NULL members of sh.
1782  */
1783 
1784 static void
1785 emptyshare(struct share *sh)
1786 {
1787 	if (sh->sh_path != NULL)
1788 	    free(sh->sh_path);
1789 	sh->sh_path = NULL;
1790 	if (sh->sh_res != NULL)
1791 	    free(sh->sh_res);
1792 	sh->sh_res = NULL;
1793 	if (sh->sh_fstype != NULL)
1794 	    free(sh->sh_fstype);
1795 	sh->sh_fstype = NULL;
1796 	if (sh->sh_opts != NULL)
1797 	    free(sh->sh_opts);
1798 	sh->sh_opts = NULL;
1799 	if (sh->sh_descr != NULL)
1800 	    free(sh->sh_descr);
1801 	sh->sh_descr = NULL;
1802 }
1803 
1804 /*
1805  * sa_update_sharetab(share, proto)
1806  *
1807  * Update the sharetab file with info from the specified share.
1808  * This could be an update or add.
1809  */
1810 
1811 int
1812 sa_update_sharetab(sa_share_t share, char *proto)
1813 {
1814 	int ret = SA_OK;
1815 	struct share shtab;
1816 	char *path;
1817 	int logging = 0;
1818 	FILE *sharetab;
1819 	sigset_t old, new;
1820 
1821 	path = sa_get_share_attr(share, "path");
1822 	if (path != NULL) {
1823 	    (void) memset(&shtab, '\0', sizeof (shtab));
1824 	    sharetab = fopen(SA_LEGACY_SHARETAB, "r+");
1825 	    if (sharetab == NULL) {
1826 		sharetab = fopen(SA_LEGACY_SHARETAB, "w+");
1827 	    }
1828 	    if (sharetab != NULL) {
1829 		(void) setvbuf(sharetab, NULL, _IOLBF, BUFSIZ * 8);
1830 		(void) sigprocmask(SIG_BLOCK, NULL, &new);
1831 		(void) sigaddset(&new, SIGHUP);
1832 		(void) sigaddset(&new, SIGINT);
1833 		(void) sigaddset(&new, SIGQUIT);
1834 		(void) sigaddset(&new, SIGTSTP);
1835 		(void) sigprocmask(SIG_SETMASK, &new, &old);
1836 		(void) lockf(fileno(sharetab), F_LOCK, 0);
1837 		(void) remshare(sharetab, path, &logging);
1838 		/* fill in share structure and write it out */
1839 		(void) fillshare(share, proto, &shtab);
1840 		(void) putshare(sharetab, &shtab);
1841 		emptyshare(&shtab);
1842 		(void) fflush(sharetab);
1843 		(void) lockf(fileno(sharetab), F_ULOCK, 0);
1844 		(void) fsync(fileno(sharetab));
1845 		(void) sigprocmask(SIG_SETMASK, &old, NULL);
1846 		(void) fclose(sharetab);
1847 	    } else {
1848 		if (errno == EACCES || errno == EPERM) {
1849 		    ret = SA_NO_PERMISSION;
1850 		} else {
1851 		    ret = SA_CONFIG_ERR;
1852 		}
1853 	    }
1854 	    sa_free_attr_string(path);
1855 	}
1856 	return (ret);
1857 }
1858 
1859 /*
1860  * sa_delete_sharetab(path, proto)
1861  *
1862  * remove the specified share from sharetab.
1863  */
1864 
1865 int
1866 sa_delete_sharetab(char *path, char *proto)
1867 {
1868 	int ret = SA_OK;
1869 	int logging = 0;
1870 	FILE *sharetab;
1871 	sigset_t old, new;
1872 #ifdef lint
1873 	proto = proto;
1874 #endif
1875 
1876 	if (path != NULL) {
1877 	    sharetab = fopen(SA_LEGACY_SHARETAB, "r+");
1878 	    if (sharetab == NULL) {
1879 		sharetab = fopen(SA_LEGACY_SHARETAB, "w+");
1880 	    }
1881 	    if (sharetab != NULL) {
1882 		/* should block keyboard level signals around the lock */
1883 		(void) sigprocmask(SIG_BLOCK, NULL, &new);
1884 		(void) sigaddset(&new, SIGHUP);
1885 		(void) sigaddset(&new, SIGINT);
1886 		(void) sigaddset(&new, SIGQUIT);
1887 		(void) sigaddset(&new, SIGTSTP);
1888 		(void) sigprocmask(SIG_SETMASK, &new, &old);
1889 		(void) lockf(fileno(sharetab), F_LOCK, 0);
1890 		ret = remshare(sharetab, path, &logging);
1891 		(void) fflush(sharetab);
1892 		(void) lockf(fileno(sharetab), F_ULOCK, 0);
1893 		(void) fsync(fileno(sharetab));
1894 		(void) sigprocmask(SIG_SETMASK, &old, NULL);
1895 		(void) fclose(sharetab);
1896 	    } else {
1897 		if (errno == EACCES || errno == EPERM) {
1898 		    ret = SA_NO_PERMISSION;
1899 		} else {
1900 		    ret = SA_CONFIG_ERR;
1901 		}
1902 	    }
1903 	}
1904 	return (ret);
1905 }
1906