xref: /illumos-gate/usr/src/lib/libshare/common/libsharecore.c (revision f48205be61a214698b763ff550ab9e657525104c)
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_create_dummy_share()
764  *
765  * Create a share entry suitable for parsing but not tied to any real
766  * config tree.  Need to have a parent as well as the node to parse
767  * on.  Free using _sa_free_dummy_share(share);
768  */
769 
770 static sa_group_t
771 _sa_create_dummy_share()
772 {
773 	xmlNodePtr parent_node = NULL;
774 	xmlNodePtr child_node = NULL;
775 
776 	parent_node = xmlNewNode(NULL, (xmlChar *)"group");
777 	if (parent_node != NULL) {
778 	    child_node = xmlNewChild(parent_node, NULL, (xmlChar *)"share",
779 					NULL);
780 	    if (child_node != NULL) {
781 		/*
782 		 * Use a "zfs" tag since that will make sure nothing
783 		 * really attempts to put values into the
784 		 * repository. Also ZFS is currently the only user of
785 		 * this interface.
786 		 */
787 		set_node_attr(parent_node, "type", "transient");
788 		set_node_attr(parent_node, "zfs", "true");
789 		set_node_attr(child_node, "type", "transient");
790 		set_node_attr(child_node, "zfs", "true");
791 	    } else {
792 		xmlFreeNode(parent_node);
793 	    }
794 	}
795 	return (child_node);
796 }
797 
798 /*
799  * _sa_free_dummy_share(share)
800  *
801  * Free the dummy share and its parent.  It is an error to try and
802  * free something that isn't a dummy.
803  */
804 
805 static int
806 _sa_free_dummy_share(sa_share_t share)
807 {
808 	xmlNodePtr node = (xmlNodePtr)share;
809 	xmlNodePtr parent;
810 	int ret = SA_OK;
811 	char *name;
812 
813 	if (node != NULL) {
814 	    parent = node->parent;
815 	    name = (char *)xmlGetProp(node, (xmlChar *)"path");
816 	    if (name != NULL) {
817 		/* Real shares always have a path but a dummy doesn't */
818 		ret = SA_NOT_ALLOWED;
819 		sa_free_attr_string(name);
820 	    } else {
821 		/*
822 		 * If there is a parent, do the free on that since
823 		 * xmlFreeNode is a recursive function and free's an
824 		 * child nodes.
825 		 */
826 		if (parent != NULL) {
827 		    node = parent;
828 		}
829 		xmlUnlinkNode(node);
830 		xmlFreeNode(node);
831 	    }
832 	}
833 	return (ret);
834 }
835 
836 
837 /*
838  * sa_parse_legacy_options(group, options, proto)
839  *
840  * In order to support legacy configurations, we allow the protocol
841  * specific plugin to parse legacy syntax options (like those in
842  * /etc/dfs/dfstab). This adds a new optionset to the group (or
843  * share).
844  *
845  * Once the optionset has been created, we then get the derived
846  * optionset of the parent (options from the optionset of the parent
847  * and any parent it might have) and remove those from the created
848  * optionset. This avoids duplication of options.
849  */
850 
851 int
852 sa_parse_legacy_options(sa_group_t group, char *options, char *proto)
853 {
854 	int ret = SA_INVALID_PROTOCOL;
855 	sa_group_t parent;
856 	int using_dummy = B_FALSE;
857 
858 	/*
859 	 * if "group" is NULL, this is just a parse without saving
860 	 * anything in either SMF or ZFS.  Create a dummy group to
861 	 * handle this case.
862 	 */
863 	if (group == NULL) {
864 	    group = (sa_group_t)_sa_create_dummy_share();
865 	    using_dummy = B_TRUE;
866 	}
867 
868 	parent = sa_get_parent_group(group);
869 
870 	if (proto != NULL)
871 	    ret = sa_proto_legacy_opts(proto, group, options);
872 
873 	if (using_dummy) {
874 	    /* Since this is a dummy parse, cleanup and quit here */
875 	    (void) _sa_free_dummy_share(group);
876 	    return (ret);
877 	}
878 	/*
879 	 * if in a group, remove the inherited options and security
880 	 */
881 	if (ret == SA_OK) {
882 	    if (parent != NULL) {
883 		sa_optionset_t optionset;
884 		sa_property_t popt, prop;
885 		sa_optionset_t localoptions;
886 		/* find parent options to remove from child */
887 		optionset = sa_get_derived_optionset(parent, proto, 1);
888 		localoptions = sa_get_optionset(group, proto);
889 		if (optionset != NULL) {
890 		    for (popt = sa_get_property(optionset, NULL);
891 			popt != NULL;
892 			popt = sa_get_next_property(popt)) {
893 			char *tag;
894 			char *value1;
895 			char *value2;
896 
897 			tag = sa_get_property_attr(popt, "type");
898 			if (tag != NULL) {
899 			    prop = sa_get_property(localoptions, tag);
900 			    if (prop != NULL) {
901 				value1 = sa_get_property_attr(popt, "value");
902 				value2 = sa_get_property_attr(prop, "value");
903 				if (value1 != NULL && value2 != NULL &&
904 				    strcmp(value1, value2) == 0) {
905 				    /* remove the property from the child */
906 				    (void) _sa_remove_property(prop);
907 				}
908 				if (value1 != NULL)
909 				    sa_free_attr_string(value1);
910 				if (value2 != NULL)
911 				    sa_free_attr_string(value2);
912 			    }
913 			    sa_free_attr_string(tag);
914 			}
915 		    }
916 		    prop = sa_get_property(localoptions, NULL);
917 		    if (prop == NULL && sa_is_share(group)) {
918 			/*
919 			 * all properties removed so remove the
920 			 * optionset if it is on a share
921 			 */
922 			(void) _sa_remove_optionset(localoptions);
923 		    }
924 		    sa_free_derived_optionset(optionset);
925 		}
926 		/*
927 		 * need to remove security here. If there are no
928 		 * security options on the local group/share, don't
929 		 * bother since those are the only ones that would be
930 		 * affected.
931 		 */
932 		localoptions = sa_get_all_security_types(group, proto, 0);
933 		if (localoptions != NULL) {
934 		    for (prop = sa_get_property(localoptions, NULL);
935 			prop != NULL; prop = sa_get_next_property(prop)) {
936 			char *tag;
937 			sa_security_t security;
938 			tag = sa_get_property_attr(prop, "type");
939 			if (tag != NULL) {
940 			    security = sa_get_security(group, tag, proto);
941 			    sa_free_attr_string(tag);
942 			    for (popt = sa_get_property(security, NULL);
943 				popt != NULL;
944 				popt = sa_get_next_property(popt)) {
945 				char *value1;
946 				char *value2;
947 
948 				/* remove duplicates from this level */
949 				value1 = sa_get_property_attr(popt, "value");
950 				value2 = sa_get_property_attr(prop, "value");
951 				if (value1 != NULL && value2 != NULL &&
952 				    strcmp(value1, value2) == 0) {
953 				    /* remove the property from the child */
954 				    (void) _sa_remove_property(prop);
955 				}
956 				if (value1 != NULL)
957 				    sa_free_attr_string(value1);
958 				if (value2 != NULL)
959 				    sa_free_attr_string(value2);
960 			    }
961 			}
962 		    }
963 		    (void) sa_destroy_optionset(localoptions);
964 		}
965 	    }
966 	}
967 	return (ret);
968 }
969 
970 /*
971  * dfs_free_list(list)
972  *
973  * Free the data in each list entry of the list as well as freeing the
974  * entries themselves. We need to avoid memory leaks and don't want to
975  * dereference any NULL members.
976  */
977 
978 static void
979 dfs_free_list(xfs_sharelist_t *list)
980 {
981 	xfs_sharelist_t *entry;
982 	for (entry = list; entry != NULL; entry = list) {
983 	    if (entry->path != NULL)
984 		free(entry->path);
985 	    if (entry->resource != NULL)
986 		free(entry->resource);
987 	    if (entry->fstype != NULL)
988 		free(entry->fstype);
989 	    if (entry->options != NULL)
990 		free(entry->options);
991 	    if (entry->description != NULL)
992 		free(entry->description);
993 	    if (entry->origline != NULL)
994 		free(entry->origline);
995 	    if (entry->group != NULL)
996 		free(entry->group);
997 	    list = list->next;
998 	    free(entry);
999 	}
1000 }
1001 
1002 /*
1003  * parse_dfstab(dfstab, root)
1004  *
1005  * Open and read the existing dfstab, parsing each line and adding it
1006  * to the internal configuration. Make sure syntax errors, etc are
1007  * preserved as comments.
1008  */
1009 
1010 static void
1011 parse_dfstab(sa_handle_t handle, char *dfstab, xmlNodePtr root)
1012 {
1013 	sa_share_t share;
1014 	sa_group_t group;
1015 	sa_group_t sgroup = NULL;
1016 	sa_group_t defgroup;
1017 	xfs_sharelist_t *head, *list;
1018 	int err;
1019 	int defined_group;
1020 	FILE *dfs;
1021 	char *oldprops;
1022 
1023 	/* read the dfstab format file and fill in the doc tree */
1024 
1025 	dfs = fopen(dfstab, "r");
1026 	if (dfs == NULL) {
1027 	    return;
1028 	}
1029 
1030 	defgroup = sa_get_group(handle, "default");
1031 
1032 	for (head = list = getdfstab(dfs);
1033 		list != NULL;
1034 		list = list->next) {
1035 	    share = NULL;
1036 	    group = NULL;
1037 	    defined_group = 0;
1038 	    err = 0;
1039 
1040 	    if (list->origline == NULL) {
1041 		/*
1042 		 * Comment line that we will likely skip.
1043 		 * If the line has the syntax:
1044 		 *	# error: string: string
1045 		 * It should be preserved until manually deleted.
1046 		 */
1047 		if (list->description != NULL &&
1048 		    strncmp(list->description, "# Error: ", 9) == 0) {
1049 		    char *line;
1050 		    char *error;
1051 		    char *cmd;
1052 		    line = strdup(list->description);
1053 		    if (line != NULL) {
1054 			error = line + 9;
1055 			cmd = strchr(error, ':');
1056 			if (cmd != NULL) {
1057 			    int len;
1058 			    *cmd = '\0';
1059 			    cmd += 2;
1060 			    len = strlen(cmd);
1061 			    cmd[len - 1] = '\0';
1062 			    add_syntax_comment(root, cmd, error, 0);
1063 			}
1064 			free(line);
1065 		    }
1066 		}
1067 		continue;
1068 	    }
1069 	    if (list->path != NULL && strlen(list->path) > 0 &&
1070 		    *list->path == '/') {
1071 		share = sa_find_share(handle, list->path);
1072 		if (share != NULL)
1073 		    sgroup = sa_get_parent_group(share);
1074 		else
1075 		    sgroup = NULL;
1076 	    } else {
1077 		(void) printf(dgettext(TEXT_DOMAIN,
1078 					"No share specified in dfstab: "
1079 					"line %d: %s\n"),
1080 			list->lineno, list->origline);
1081 		add_syntax_comment(root, list->origline,
1082 				    dgettext(TEXT_DOMAIN, "No share specified"),
1083 				    1);
1084 		continue;
1085 	    }
1086 	    if (list->group != NULL && strlen(list->group) > 0) {
1087 		group = sa_get_group(handle, list->group);
1088 		defined_group = 1;
1089 	    } else {
1090 		group = defgroup;
1091 	    }
1092 	    if (defined_group && group == NULL) {
1093 		(void) printf(dgettext(TEXT_DOMAIN,
1094 					"Unknown group used in dfstab: "
1095 					"line %d: %s\n"),
1096 			list->lineno, list->origline);
1097 		add_syntax_comment(root, list->origline,
1098 				    dgettext(TEXT_DOMAIN,
1099 						"Unknown group specified"), 1);
1100 		continue;
1101 	    }
1102 	    if (group != NULL) {
1103 		if (share == NULL) {
1104 		    if (!defined_group && group == defgroup) {
1105 			/* this is an OK add for legacy */
1106 			share = sa_add_share(defgroup, list->path,
1107 					SA_SHARE_PERMANENT | SA_SHARE_PARSER,
1108 					&err);
1109 			if (share != NULL) {
1110 			    if (list->description != NULL &&
1111 				strlen(list->description) > 0)
1112 				(void) sa_set_share_description(share,
1113 							    list->description);
1114 			    if (list->options != NULL &&
1115 				strlen(list->options) > 0) {
1116 				(void) sa_parse_legacy_options(share,
1117 								list->options,
1118 								list->fstype);
1119 			    }
1120 			    if (list->resource != NULL)
1121 				(void) sa_set_share_attr(share, "resource",
1122 						    list->resource);
1123 			} else {
1124 			    (void) printf(dgettext(TEXT_DOMAIN,
1125 					    "Error in dfstab: "
1126 					    "line %d: %s\n"),
1127 				    list->lineno, list->origline);
1128 			    if (err != SA_BAD_PATH)
1129 				add_syntax_comment(root, list->origline,
1130 						dgettext(TEXT_DOMAIN,
1131 							    "Syntax"), 1);
1132 			    else
1133 				add_syntax_comment(root, list->origline,
1134 						dgettext(TEXT_DOMAIN,
1135 							    "Path"), 1);
1136 			    continue;
1137 			}
1138 		    }
1139 		} else {
1140 		    if (group != sgroup) {
1141 			(void) printf(dgettext(TEXT_DOMAIN, "Attempt to change"
1142 						"configuration in"
1143 						"dfstab: line %d: %s\n"),
1144 				list->lineno, list->origline);
1145 			add_syntax_comment(root, list->origline,
1146 				dgettext(TEXT_DOMAIN,
1147 					    "Attempt to change configuration"),
1148 				1);
1149 			continue;
1150 		    }
1151 		    /* its the same group but could have changed options */
1152 		    oldprops = sa_proto_legacy_format(list->fstype, share, 0);
1153 		    if (oldprops != NULL) {
1154 			if (list->options != NULL &&
1155 				strcmp(oldprops, list->options) != 0) {
1156 			    sa_optionset_t opts;
1157 			    sa_security_t secs;
1158 			    /* possibly different values */
1159 			    opts = sa_get_optionset((sa_group_t)share,
1160 							list->fstype);
1161 			    (void) sa_destroy_optionset(opts);
1162 			    for (secs = sa_get_security((sa_group_t)share,
1163 							NULL, list->fstype);
1164 				secs != NULL;
1165 				secs = sa_get_security((sa_group_t)share,
1166 							NULL, list->fstype)) {
1167 				(void) sa_destroy_security(secs);
1168 			    }
1169 			    (void) sa_parse_legacy_options(share,
1170 							    list->options,
1171 							    list->fstype);
1172 			}
1173 			sa_format_free(oldprops);
1174 		    }
1175 		}
1176 	    } else {
1177 		/* shouldn't happen */
1178 		err = SA_CONFIG_ERR;
1179 	    }
1180 
1181 	}
1182 	dfs_free_list(head);
1183 }
1184 
1185 /*
1186  * legacy_removes(group, file)
1187  *
1188  * Find any shares that are "missing" from the legacy file. These
1189  * should be removed from the configuration since they are likely from
1190  * a legacy app or the admin modified the dfstab file directly. We
1191  * have to support this even if it is not the recommended way to do
1192  * things.
1193  */
1194 
1195 static void
1196 legacy_removes(sa_group_t group, char *file)
1197 {
1198 	sa_share_t share;
1199 	char *path;
1200 	xfs_sharelist_t *list, *item;
1201 	FILE *dfstab;
1202 
1203 	dfstab = fopen(file, "r");
1204 	if (dfstab != NULL) {
1205 	    list = getdfstab(dfstab);
1206 	    (void) fclose(dfstab);
1207 retry:
1208 	    for (share = sa_get_share(group, NULL); share != NULL;
1209 		share = sa_get_next_share(share)) {
1210 		/* now see if the share is in the dfstab file */
1211 		path = sa_get_share_attr(share, "path");
1212 		if (path != NULL) {
1213 		    item = finddfsentry(list, path);
1214 		    sa_free_attr_string(path);
1215 		    if (item == NULL) {
1216 			/* the share was removed this way */
1217 			(void) sa_remove_share(share);
1218 
1219 			/* start over since the list was broken */
1220 			goto retry;
1221 		    }
1222 		}
1223 	    }
1224 	    if (list != NULL)
1225 		dfs_free_list(list);
1226 	}
1227 }
1228 
1229 /*
1230  * getlegacyconfig(path, root)
1231  *
1232  * Parse dfstab and build the legacy configuration. This only gets
1233  * called when a change was detected.
1234  */
1235 
1236 void
1237 getlegacyconfig(sa_handle_t handle, char *path, xmlNodePtr *root)
1238 {
1239 	sa_group_t defgroup;
1240 
1241 	if (root != NULL) {
1242 	    if (*root == NULL)
1243 		*root = xmlNewNode(NULL, (xmlChar *)"sharecfg");
1244 	    if (*root != NULL) {
1245 		if (strcmp(path, SA_LEGACY_DFSTAB) == 0) {
1246 			/*
1247 			 * walk the default shares and find anything
1248 			 * missing.  we do this first to make sure it
1249 			 * is cleaned up since there may be legacy
1250 			 * code add/del via dfstab and we need to
1251 			 * cleanup SMF.
1252 			 */
1253 		    defgroup = sa_get_group(handle, "default");
1254 		    if (defgroup != NULL) {
1255 			legacy_removes(defgroup, path);
1256 		    }
1257 		    /* parse the dfstab and add anything new */
1258 		    parse_dfstab(handle, path, *root);
1259 		}
1260 	    }
1261 	}
1262 }
1263 
1264 /*
1265  * get_share_list(&err)
1266  *
1267  * Get a linked list of all the shares on the system from
1268  * /etc/dfs/sharetab. This is partially copied from libfsmgt which we
1269  * can't use due to package dependencies.
1270  */
1271 static xfs_sharelist_t *
1272 get_share_list(int *errp)
1273 {
1274 	xfs_sharelist_t	*newp;
1275 	xfs_sharelist_t	*headp;
1276 	xfs_sharelist_t	*tailp;
1277 	FILE		*fp;
1278 
1279 	headp = NULL;
1280 	tailp = NULL;
1281 
1282 	if ((fp = fopen(SHARETAB, "r")) != NULL) {
1283 		struct share	*sharetab_entry;
1284 
1285 		while (getshare(fp, &sharetab_entry) > 0) {
1286 		    newp = alloc_sharelist();
1287 		    if (newp == NULL) {
1288 			goto err;
1289 		    }
1290 
1291 			/*
1292 			 * link into the list here so we don't leak
1293 			 * memory on a failure from strdup().
1294 			 */
1295 		    if (headp == NULL) {
1296 			headp = newp;
1297 			tailp = newp;
1298 		    } else {
1299 			tailp->next = newp;
1300 			tailp = newp;
1301 		    }
1302 
1303 		    newp->path = strdup(sharetab_entry->sh_path);
1304 		    if (newp->path == NULL)
1305 			goto err;
1306 		    newp->resource = strdup(sharetab_entry->sh_res);
1307 		    if (newp->resource == NULL)
1308 			goto err;
1309 		    newp->fstype = strdup(sharetab_entry->sh_fstype);
1310 		    if (newp->fstype == NULL)
1311 			goto err;
1312 		    newp->options = strdup(sharetab_entry->sh_opts);
1313 		    if (newp->options == NULL)
1314 			goto err;
1315 		    newp->description = strdup(sharetab_entry->sh_descr);
1316 		    if (newp->description == NULL)
1317 			goto err;
1318 		}
1319 		(void) lockf(fileno(fp), F_ULOCK, 0);
1320 		(void) fclose(fp);
1321 	} else {
1322 	    *errp = errno;
1323 	}
1324 
1325 	/*
1326 	 * Caller must free the mount list
1327 	 */
1328 	return (headp);
1329 err:
1330 	/*
1331 	 * Out of memory so cleanup and leave.
1332 	 */
1333 	dfs_free_list(headp);
1334 	(void) fclose(fp);
1335 	return (NULL);
1336 }
1337 
1338 /*
1339  * parse_sharetab(handle)
1340  *
1341  * Read the /etc/dfs/sharetab file and see which entries don't exist
1342  * in the repository. These shares are marked transient.  We also need
1343  * to see if they are ZFS shares since ZFS bypasses the SMF
1344  * repository.
1345  */
1346 
1347 int
1348 parse_sharetab(sa_handle_t handle)
1349 {
1350 	xfs_sharelist_t *list, *tmplist;
1351 	int err = 0;
1352 	sa_share_t share;
1353 	sa_group_t group;
1354 	sa_group_t lgroup;
1355 	char *groupname;
1356 	int legacy = 0;
1357 
1358 	list = get_share_list(&err);
1359 	if (list == NULL)
1360 	    return (legacy);
1361 
1362 	lgroup = sa_get_group(handle, "default");
1363 
1364 	for (tmplist = list; tmplist != NULL; tmplist = tmplist->next) {
1365 	    group = NULL;
1366 	    share = sa_find_share(handle, tmplist->path);
1367 	    if (share == NULL) {
1368 		/*
1369 		 * this share is transient so needs to be
1370 		 * added. Initially, this will be under
1371 		 * default(legacy) unless it is a ZFS
1372 		 * share. If zfs, we need a zfs group.
1373 		 */
1374 		if (tmplist->resource != NULL &&
1375 		    (groupname = strchr(tmplist->resource, '@')) != NULL) {
1376 		    /* there is a defined group */
1377 		    *groupname++ = '\0';
1378 		    group = sa_get_group(handle, groupname);
1379 		    if (group != NULL) {
1380 			share = _sa_add_share(group, tmplist->path,
1381 						SA_SHARE_TRANSIENT, &err);
1382 		    } else {
1383 			/*
1384 			 * While this case shouldn't occur very often,
1385 			 * it does occur out of a "zfs set
1386 			 * sharenfs=off" when the dataset is also set
1387 			 * to canmount=off. A warning will then cause
1388 			 * the zfs command to abort. Since we add it
1389 			 * to the default list, everything works
1390 			 * properly anyway and the library doesn't
1391 			 * need to give a warning.
1392 			 */
1393 			share = _sa_add_share(lgroup, tmplist->path,
1394 						SA_SHARE_TRANSIENT, &err);
1395 		    }
1396 		} else {
1397 		    if (sa_zfs_is_shared(handle, tmplist->path)) {
1398 			group = sa_get_group(handle, "zfs");
1399 			if (group == NULL) {
1400 			    group = sa_create_group(handle, "zfs", &err);
1401 			    if (group == NULL && err == SA_NO_PERMISSION) {
1402 				group = _sa_create_group(
1403 						(sa_handle_impl_t)handle,
1404 						"zfs");
1405 			    }
1406 			    if (group != NULL) {
1407 				(void) sa_create_optionset(group,
1408 							    tmplist->fstype);
1409 				(void) sa_set_group_attr(group, "zfs", "true");
1410 			    }
1411 			}
1412 			if (group != NULL) {
1413 			    share = _sa_add_share(group, tmplist->path,
1414 						    SA_SHARE_TRANSIENT, &err);
1415 			}
1416 		    } else {
1417 			share = _sa_add_share(lgroup, tmplist->path,
1418 						SA_SHARE_TRANSIENT, &err);
1419 		    }
1420 		}
1421 		if (share == NULL)
1422 		    (void) printf(dgettext(TEXT_DOMAIN,
1423 					    "Problem with transient: %s\n"),
1424 				    sa_errorstr(err));
1425 		if (share != NULL)
1426 		    set_node_attr(share, "shared", "true");
1427 
1428 		if (err == SA_OK) {
1429 		    if (tmplist->options != NULL &&
1430 			strlen(tmplist->options) > 0) {
1431 			(void) sa_parse_legacy_options(share,
1432 							tmplist->options,
1433 							tmplist->fstype);
1434 		    }
1435 		    if (tmplist->resource != NULL &&
1436 			strcmp(tmplist->resource, "-") != 0)
1437 			set_node_attr(share, "resource", tmplist->resource);
1438 		    if (tmplist->description != NULL) {
1439 			xmlNodePtr node;
1440 			node = xmlNewChild((xmlNodePtr)share, NULL,
1441 						(xmlChar *)"description", NULL);
1442 			xmlNodeSetContent(node,
1443 					    (xmlChar *)tmplist->description);
1444 		    }
1445 		    legacy = 1;
1446 		}
1447 	    } else {
1448 		/*
1449 		 * if this is a legacy share, mark as shared so we
1450 		 * only update sharetab appropriately. We also keep
1451 		 * the sharetab options in order to display for legacy
1452 		 * share with no arguments.
1453 		 */
1454 		set_node_attr(share, "shared", "true");
1455 		set_node_attr(share, "shareopts", tmplist->options);
1456 	    }
1457 	}
1458 	dfs_free_list(list);
1459 	return (legacy);
1460 }
1461 
1462 /*
1463  * get the transient shares from the sharetab (or other) file.  since
1464  * these are transient, they only appear in the working file and not
1465  * in a repository.
1466  */
1467 int
1468 gettransients(sa_handle_impl_t ihandle, xmlNodePtr *root)
1469 {
1470 	int legacy = 0;
1471 
1472 	if (root != NULL) {
1473 	    if (*root == NULL)
1474 		*root = xmlNewNode(NULL, (xmlChar *)"sharecfg");
1475 	    if (*root != NULL) {
1476 		legacy = parse_sharetab(ihandle);
1477 	    }
1478 	}
1479 	return (legacy);
1480 }
1481 
1482 /*
1483  * sa_has_prop(optionset, prop)
1484  *
1485  * Is the specified property a member of the optionset?
1486  */
1487 
1488 int
1489 sa_has_prop(sa_optionset_t optionset, sa_property_t prop)
1490 {
1491 	char *name;
1492 	sa_property_t otherprop;
1493 	int result = 0;
1494 
1495 	if (optionset != NULL) {
1496 	    name = sa_get_property_attr(prop, "type");
1497 	    if (name != NULL) {
1498 		otherprop = sa_get_property(optionset, name);
1499 		if (otherprop != NULL)
1500 		    result = 1;
1501 		sa_free_attr_string(name);
1502 	    }
1503 	}
1504 	return (result);
1505 }
1506 
1507 /*
1508  * Update legacy files
1509  *
1510  * Provides functions to add/remove/modify individual entries
1511  * in dfstab and sharetab
1512  */
1513 
1514 void
1515 update_legacy_config(sa_handle_t handle)
1516 {
1517 	/*
1518 	 * no longer used -- this is a placeholder in case we need to
1519 	 * add it back later.
1520 	 */
1521 #ifdef lint
1522 	handle = handle;
1523 #endif
1524 }
1525 
1526 /*
1527  * sa_valid_property(object, proto, property)
1528  *
1529  * check to see if the specified property is valid relative to the
1530  * specified protocol. The protocol plugin is called to do the work.
1531  */
1532 
1533 int
1534 sa_valid_property(void *object, char *proto, sa_property_t property)
1535 {
1536 	int ret = SA_OK;
1537 
1538 	if (proto != NULL && property != NULL) {
1539 	    ret = sa_proto_valid_prop(proto, property, object);
1540 	}
1541 
1542 	return (ret);
1543 }
1544 
1545 /*
1546  * sa_fstype(path)
1547  *
1548  * Given path, return the string representing the path's file system
1549  * type. This is used to discover ZFS shares.
1550  */
1551 
1552 char *
1553 sa_fstype(char *path)
1554 {
1555 	int err;
1556 	struct stat st;
1557 
1558 	err = stat(path, &st);
1559 	if (err < 0) {
1560 	    err = SA_NO_SUCH_PATH;
1561 	} else {
1562 	    err = SA_OK;
1563 	}
1564 	if (err == SA_OK) {
1565 		/* have a valid path at this point */
1566 	    return (strdup(st.st_fstype));
1567 	}
1568 	return (NULL);
1569 }
1570 
1571 void
1572 sa_free_fstype(char *type)
1573 {
1574 	free(type);
1575 }
1576 
1577 /*
1578  * sa_get_derived_optionset(object, proto, hier)
1579  *
1580  *	Work backward to the top of the share object tree and start
1581  *	copying protocol specific optionsets into a newly created
1582  *	optionset that doesn't have a parent (it will be freed
1583  *	later). This provides for the property inheritence model. That
1584  *	is, properties closer to the share take precedence over group
1585  *	level. This also provides for groups of groups in the future.
1586  */
1587 
1588 sa_optionset_t
1589 sa_get_derived_optionset(void *object, char *proto, int hier)
1590 {
1591 	sa_optionset_t newoptionset;
1592 	sa_optionset_t optionset;
1593 	sa_group_t group;
1594 
1595 	if (hier &&
1596 	    (group = sa_get_parent_group((sa_share_t)object)) != NULL) {
1597 	    newoptionset = sa_get_derived_optionset((void *)group, proto, hier);
1598 	} else {
1599 	    newoptionset = (sa_optionset_t)xmlNewNode(NULL,
1600 						    (xmlChar *)"optionset");
1601 	    if (newoptionset != NULL) {
1602 		sa_set_optionset_attr(newoptionset, "type", proto);
1603 	    }
1604 	}
1605 	/* dont' do anything if memory wasn't allocated */
1606 	if (newoptionset == NULL)
1607 	    return (NULL);
1608 
1609 	/* found the top so working back down the stack */
1610 	optionset = sa_get_optionset((sa_optionset_t)object, proto);
1611 	if (optionset != NULL) {
1612 	    sa_property_t prop;
1613 	    /* add optionset to the newoptionset */
1614 	    for (prop = sa_get_property(optionset, NULL);
1615 		prop != NULL; prop = sa_get_next_property(prop)) {
1616 		sa_property_t newprop;
1617 		char *name;
1618 		char *value;
1619 		name = sa_get_property_attr(prop, "type");
1620 		value = sa_get_property_attr(prop, "value");
1621 		if (name != NULL) {
1622 		    newprop = sa_get_property(newoptionset, name);
1623 		    /* replace the value with the new value */
1624 		    if (newprop != NULL) {
1625 			/*
1626 			 * only set if value is non NULL, old value ok
1627 			 * if it is NULL.
1628 			 */
1629 			if (value != NULL)
1630 			    set_node_attr(newprop, "value", value);
1631 		    } else {
1632 			/* an entirely new property */
1633 			if (value != NULL) {
1634 			    newprop = sa_create_property(name, value);
1635 			    if (newprop != NULL) {
1636 				newprop = (sa_property_t)
1637 				    xmlAddChild((xmlNodePtr)newoptionset,
1638 						(xmlNodePtr)newprop);
1639 			    }
1640 			}
1641 		    }
1642 		    sa_free_attr_string(name);
1643 		}
1644 		if (value != NULL)
1645 		    sa_free_attr_string(value);
1646 	    }
1647 	}
1648 	return (newoptionset);
1649 }
1650 
1651 void
1652 sa_free_derived_optionset(sa_optionset_t optionset)
1653 {
1654 	/* while it shouldn't be linked, it doesn't hurt */
1655 	if (optionset != NULL) {
1656 	    xmlUnlinkNode((xmlNodePtr) optionset);
1657 	    xmlFreeNode((xmlNodePtr) optionset);
1658 	}
1659 }
1660 
1661 /*
1662  *  sa_get_all_security_types(object, proto, hier)
1663  *
1664  *	find all the security types set for this object.  This is
1665  *	preliminary to getting a derived security set. The return value is an
1666  *	optionset containg properties which are the sectype values found by
1667  *	walking up the XML document struture. The returned optionset
1668  *	is a derived optionset.
1669  *
1670  *	If hier is 0, only look at object. If non-zero, walk up the tree.
1671  */
1672 sa_optionset_t
1673 sa_get_all_security_types(void *object, char *proto, int hier)
1674 {
1675 	sa_optionset_t options;
1676 	sa_security_t security;
1677 	sa_group_t group;
1678 	sa_property_t prop;
1679 
1680 	options = NULL;
1681 
1682 	if (hier &&
1683 	    (group = sa_get_parent_group((sa_share_t)object)) != NULL) {
1684 	    options = sa_get_all_security_types((void *)group, proto, hier);
1685 	} else {
1686 	    options = (sa_optionset_t)xmlNewNode(NULL,
1687 						    (xmlChar *)"optionset");
1688 	}
1689 	/* hit the top so collect the security types working back */
1690 	if (options != NULL) {
1691 	    for (security = sa_get_security((sa_group_t)object, NULL, NULL);
1692 		security != NULL; security = sa_get_next_security(security)) {
1693 		char *type;
1694 		char *sectype;
1695 
1696 		type = sa_get_security_attr(security, "type");
1697 		if (type != NULL) {
1698 		    if (strcmp(type, proto) != 0) {
1699 			sa_free_attr_string(type);
1700 			continue;
1701 		    }
1702 		    sectype = sa_get_security_attr(security, "sectype");
1703 		    if (sectype != NULL) {
1704 			/*
1705 			 * have a security type, check to see if
1706 			 * already present in optionset and add if it
1707 			 * isn't.
1708 			 */
1709 			if (sa_get_property(options, sectype) == NULL) {
1710 			    prop = sa_create_property(sectype, "true");
1711 			    if (prop != NULL)
1712 				prop = (sa_property_t)
1713 				    xmlAddChild((xmlNodePtr)options,
1714 						(xmlNodePtr)prop);
1715 			}
1716 			sa_free_attr_string(sectype);
1717 		    }
1718 		    sa_free_attr_string(type);
1719 		}
1720 	    }
1721 	}
1722 	return (options);
1723 }
1724 
1725 /*
1726  * sa_get_derived_security(object, sectype, proto, hier)
1727  *
1728  * Get the derived security(named optionset) for the object given the
1729  * sectype and proto. If hier is non-zero, walk up the tree to get all
1730  * properties defined for this object, otherwise just those on the
1731  * object.
1732  */
1733 
1734 sa_security_t
1735 sa_get_derived_security(void *object, char *sectype, char *proto, int hier)
1736 {
1737 	sa_security_t newsecurity;
1738 	sa_security_t security;
1739 	sa_group_t group;
1740 
1741 	if (hier &&
1742 	    (group = sa_get_parent_group((sa_share_t)object)) != NULL) {
1743 	    newsecurity = sa_get_derived_security((void *)group,
1744 						    sectype, proto, hier);
1745 	} else {
1746 	    newsecurity = (sa_security_t)xmlNewNode(NULL,
1747 						    (xmlChar *)"security");
1748 	    if (newsecurity != NULL) {
1749 		sa_set_security_attr(newsecurity, "type", proto);
1750 		sa_set_security_attr(newsecurity, "sectype", sectype);
1751 	    }
1752 	}
1753 	/* dont' do anything if memory wasn't allocated */
1754 	if (newsecurity == NULL)
1755 	    return (NULL);
1756 
1757 	/* found the top so working back down the stack */
1758 	security = sa_get_security((sa_security_t)object, sectype, proto);
1759 	if (security != NULL) {
1760 	    sa_property_t prop;
1761 	    /* add security to the newsecurity */
1762 	    for (prop = sa_get_property(security, NULL);
1763 		prop != NULL; prop = sa_get_next_property(prop)) {
1764 		sa_property_t newprop;
1765 		char *name;
1766 		char *value;
1767 		name = sa_get_property_attr(prop, "type");
1768 		value = sa_get_property_attr(prop, "value");
1769 		if (name != NULL) {
1770 		    newprop = sa_get_property(newsecurity, name);
1771 		    /* replace the value with the new value */
1772 		    if (newprop != NULL) {
1773 			/*
1774 			 * only set if value is non NULL, old value ok
1775 			 * if it is NULL.
1776 			 */
1777 			if (value != NULL)
1778 			    set_node_attr(newprop, name, value);
1779 		    } else {
1780 			/* an entirely new property */
1781 			if (value != NULL) {
1782 			    newprop = sa_create_property(name, value);
1783 			    newprop = (sa_property_t)
1784 				xmlAddChild((xmlNodePtr)newsecurity,
1785 					    (xmlNodePtr)newprop);
1786 			}
1787 		    }
1788 		    sa_free_attr_string(name);
1789 		}
1790 		if (value != NULL)
1791 		    sa_free_attr_string(value);
1792 	    }
1793 	}
1794 	return (newsecurity);
1795 }
1796 
1797 void
1798 sa_free_derived_security(sa_security_t security)
1799 {
1800 	/* while it shouldn't be linked, it doesn't hurt */
1801 	if (security != NULL) {
1802 	    xmlUnlinkNode((xmlNodePtr)security);
1803 	    xmlFreeNode((xmlNodePtr)security);
1804 	}
1805 }
1806 
1807 /*
1808  * sharetab utility functions
1809  *
1810  * makes use of the original sharetab.c from fs.d/nfs/lib
1811  */
1812 
1813 /*
1814  * fillshare(share, proto, sh)
1815  *
1816  * Fill the struct share with values obtained from the share object.
1817  */
1818 static void
1819 fillshare(sa_share_t share, char *proto, struct share *sh)
1820 {
1821 	char *groupname = NULL;
1822 	char *value;
1823 	sa_group_t group;
1824 	char *buff;
1825 	char *zfs;
1826 
1827 	group = sa_get_parent_group(share);
1828 	if (group != NULL) {
1829 	    zfs = sa_get_group_attr(group, "zfs");
1830 	    groupname = sa_get_group_attr(group, "name");
1831 
1832 	    if (groupname != NULL &&
1833 		(strcmp(groupname, "default") == 0 || zfs != NULL)) {
1834 		/*
1835 		 * since the groupname is either "default" or the
1836 		 * group is a ZFS group, we don't want to keep
1837 		 * groupname. We do want it if it is any other type of
1838 		 * group.
1839 		 */
1840 		sa_free_attr_string(groupname);
1841 		groupname = NULL;
1842 	    }
1843 	    if (zfs != NULL)
1844 		sa_free_attr_string(zfs);
1845 	}
1846 
1847 	value = sa_get_share_attr(share, "path");
1848 	if (value != NULL) {
1849 	    sh->sh_path = strdup(value);
1850 	    sa_free_attr_string(value);
1851 	}
1852 
1853 	value = sa_get_share_attr(share, "resource");
1854 	if (value != NULL || groupname != NULL) {
1855 	    int len = 0;
1856 
1857 	    if (value != NULL)
1858 		len += strlen(value);
1859 	    if (groupname != NULL)
1860 		len += strlen(groupname);
1861 	    len += 3; /* worst case */
1862 	    buff = malloc(len);
1863 	    (void) snprintf(buff, len, "%s%s%s",
1864 		    (value != NULL && strlen(value) > 0) ? value : "-",
1865 		    groupname != NULL ? "@" : "",
1866 		    groupname != NULL ? groupname : "");
1867 	    sh->sh_res = buff;
1868 	    if (value != NULL)
1869 		sa_free_attr_string(value);
1870 	    if (groupname != NULL) {
1871 		sa_free_attr_string(groupname);
1872 		groupname = NULL;
1873 	    }
1874 	} else {
1875 	    sh->sh_res = strdup("-");
1876 	}
1877 
1878 	sh->sh_fstype = strdup(proto);
1879 	value = sa_proto_legacy_format(proto, share, 1);
1880 	if (value != NULL) {
1881 	    if (strlen(value) > 0)
1882 		sh->sh_opts = strdup(value);
1883 	    else
1884 		sh->sh_opts = strdup("rw");
1885 	    free(value);
1886 	} else
1887 	    sh->sh_opts = strdup("rw");
1888 
1889 	value = sa_get_share_description(share);
1890 	if (value != NULL) {
1891 	    sh->sh_descr = strdup(value);
1892 	    sa_free_share_description(value);
1893 	} else
1894 	    sh->sh_descr = strdup("");
1895 }
1896 
1897 /*
1898  * emptyshare(sh)
1899  *
1900  * Free the strings in the non-NULL members of sh.
1901  */
1902 
1903 static void
1904 emptyshare(struct share *sh)
1905 {
1906 	if (sh->sh_path != NULL)
1907 	    free(sh->sh_path);
1908 	sh->sh_path = NULL;
1909 	if (sh->sh_res != NULL)
1910 	    free(sh->sh_res);
1911 	sh->sh_res = NULL;
1912 	if (sh->sh_fstype != NULL)
1913 	    free(sh->sh_fstype);
1914 	sh->sh_fstype = NULL;
1915 	if (sh->sh_opts != NULL)
1916 	    free(sh->sh_opts);
1917 	sh->sh_opts = NULL;
1918 	if (sh->sh_descr != NULL)
1919 	    free(sh->sh_descr);
1920 	sh->sh_descr = NULL;
1921 }
1922 
1923 /*
1924  * sa_update_sharetab(share, proto)
1925  *
1926  * Update the sharetab file with info from the specified share.
1927  * This could be an update or add.
1928  */
1929 
1930 int
1931 sa_update_sharetab(sa_share_t share, char *proto)
1932 {
1933 	int	ret = SA_OK;
1934 	share_t	sh;
1935 	char	*path;
1936 
1937 	path = sa_get_share_attr(share, "path");
1938 	if (path != NULL) {
1939 		(void) memset(&sh, '\0', sizeof (sh));
1940 
1941 		/*
1942 		 * Fill in share structure and send it to the kernel.
1943 		 */
1944 		(void) fillshare(share, proto, &sh);
1945 		(void) sharefs(SHAREFS_ADD, &sh);
1946 		emptyshare(&sh);
1947 		sa_free_attr_string(path);
1948 	}
1949 
1950 	return (ret);
1951 }
1952 
1953 /*
1954  * sa_delete_sharetab(path, proto)
1955  *
1956  * remove the specified share from sharetab.
1957  */
1958 
1959 int
1960 sa_delete_sharetab(char *path, char *proto)
1961 {
1962 	int	ret = SA_OK;
1963 
1964 	share_t	sh;
1965 
1966 	/*
1967 	 * Both the path and the proto are
1968 	 * keys into the sharetab.
1969 	 */
1970 	if (path != NULL && proto != NULL) {
1971 		(void) memset(&sh, '\0', sizeof (sh));
1972 		sh.sh_path = path;
1973 		sh.sh_fstype = proto;
1974 
1975 		ret = sharefs(SHAREFS_REMOVE, &sh);
1976 	}
1977 
1978 	return (ret);
1979 }
1980