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