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