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 			/*
1165 			 * It is the same group but could have changed
1166 			 * options. Make sure we include the group's
1167 			 * properties so we don't end up moving them to
1168 			 * the share inadvertantly. The last arg being
1169 			 * true says to get the inherited properties as well
1170 			 * as the local properties.
1171 			 */
1172 		    oldprops = sa_proto_legacy_format(list->fstype, share,
1173 			B_TRUE);
1174 		    if (oldprops != NULL) {
1175 			if (list->options != NULL &&
1176 				strcmp(oldprops, list->options) != 0) {
1177 			    sa_optionset_t opts;
1178 			    sa_security_t secs;
1179 			    /* possibly different values */
1180 			    opts = sa_get_optionset((sa_group_t)share,
1181 							list->fstype);
1182 			    (void) sa_destroy_optionset(opts);
1183 			    for (secs = sa_get_security((sa_group_t)share,
1184 							NULL, list->fstype);
1185 				secs != NULL;
1186 				secs = sa_get_security((sa_group_t)share,
1187 							NULL, list->fstype)) {
1188 				(void) sa_destroy_security(secs);
1189 			    }
1190 			    (void) sa_parse_legacy_options(share,
1191 							    list->options,
1192 							    list->fstype);
1193 			}
1194 			sa_format_free(oldprops);
1195 		    }
1196 		}
1197 	    } else {
1198 		/* shouldn't happen */
1199 		err = SA_CONFIG_ERR;
1200 	    }
1201 
1202 	}
1203 	dfs_free_list(head);
1204 }
1205 
1206 /*
1207  * legacy_removes(group, file)
1208  *
1209  * Find any shares that are "missing" from the legacy file. These
1210  * should be removed from the configuration since they are likely from
1211  * a legacy app or the admin modified the dfstab file directly. We
1212  * have to support this even if it is not the recommended way to do
1213  * things.
1214  */
1215 
1216 static void
1217 legacy_removes(sa_group_t group, char *file)
1218 {
1219 	sa_share_t share;
1220 	char *path;
1221 	xfs_sharelist_t *list, *item;
1222 	FILE *dfstab;
1223 
1224 	dfstab = fopen(file, "r");
1225 	if (dfstab != NULL) {
1226 	    list = getdfstab(dfstab);
1227 	    (void) fclose(dfstab);
1228 retry:
1229 	    for (share = sa_get_share(group, NULL); share != NULL;
1230 		share = sa_get_next_share(share)) {
1231 		/* now see if the share is in the dfstab file */
1232 		path = sa_get_share_attr(share, "path");
1233 		if (path != NULL) {
1234 		    item = finddfsentry(list, path);
1235 		    sa_free_attr_string(path);
1236 		    if (item == NULL) {
1237 			/* the share was removed this way */
1238 			(void) sa_remove_share(share);
1239 
1240 			/* start over since the list was broken */
1241 			goto retry;
1242 		    }
1243 		}
1244 	    }
1245 	    if (list != NULL)
1246 		dfs_free_list(list);
1247 	}
1248 }
1249 
1250 /*
1251  * getlegacyconfig(path, root)
1252  *
1253  * Parse dfstab and build the legacy configuration. This only gets
1254  * called when a change was detected.
1255  */
1256 
1257 void
1258 getlegacyconfig(sa_handle_t handle, char *path, xmlNodePtr *root)
1259 {
1260 	sa_group_t defgroup;
1261 
1262 	if (root != NULL) {
1263 	    if (*root == NULL)
1264 		*root = xmlNewNode(NULL, (xmlChar *)"sharecfg");
1265 	    if (*root != NULL) {
1266 		if (strcmp(path, SA_LEGACY_DFSTAB) == 0) {
1267 			/*
1268 			 * walk the default shares and find anything
1269 			 * missing.  we do this first to make sure it
1270 			 * is cleaned up since there may be legacy
1271 			 * code add/del via dfstab and we need to
1272 			 * cleanup SMF.
1273 			 */
1274 		    defgroup = sa_get_group(handle, "default");
1275 		    if (defgroup != NULL) {
1276 			legacy_removes(defgroup, path);
1277 		    }
1278 		    /* parse the dfstab and add anything new */
1279 		    parse_dfstab(handle, path, *root);
1280 		}
1281 	    }
1282 	}
1283 }
1284 
1285 /*
1286  * get_share_list(&err)
1287  *
1288  * Get a linked list of all the shares on the system from
1289  * /etc/dfs/sharetab. This is partially copied from libfsmgt which we
1290  * can't use due to package dependencies.
1291  */
1292 static xfs_sharelist_t *
1293 get_share_list(int *errp)
1294 {
1295 	xfs_sharelist_t	*newp;
1296 	xfs_sharelist_t	*headp;
1297 	xfs_sharelist_t	*tailp;
1298 	FILE		*fp;
1299 
1300 	headp = NULL;
1301 	tailp = NULL;
1302 
1303 	if ((fp = fopen(SHARETAB, "r")) != NULL) {
1304 		struct share	*sharetab_entry;
1305 
1306 		while (getshare(fp, &sharetab_entry) > 0) {
1307 		    newp = alloc_sharelist();
1308 		    if (newp == NULL) {
1309 			goto err;
1310 		    }
1311 
1312 			/*
1313 			 * link into the list here so we don't leak
1314 			 * memory on a failure from strdup().
1315 			 */
1316 		    if (headp == NULL) {
1317 			headp = newp;
1318 			tailp = newp;
1319 		    } else {
1320 			tailp->next = newp;
1321 			tailp = newp;
1322 		    }
1323 
1324 		    newp->path = strdup(sharetab_entry->sh_path);
1325 		    if (newp->path == NULL)
1326 			goto err;
1327 		    newp->resource = strdup(sharetab_entry->sh_res);
1328 		    if (newp->resource == NULL)
1329 			goto err;
1330 		    newp->fstype = strdup(sharetab_entry->sh_fstype);
1331 		    if (newp->fstype == NULL)
1332 			goto err;
1333 		    newp->options = strdup(sharetab_entry->sh_opts);
1334 		    if (newp->options == NULL)
1335 			goto err;
1336 		    newp->description = strdup(sharetab_entry->sh_descr);
1337 		    if (newp->description == NULL)
1338 			goto err;
1339 		}
1340 		(void) lockf(fileno(fp), F_ULOCK, 0);
1341 		(void) fclose(fp);
1342 	} else {
1343 	    *errp = errno;
1344 	}
1345 
1346 	/*
1347 	 * Caller must free the mount list
1348 	 */
1349 	return (headp);
1350 err:
1351 	/*
1352 	 * Out of memory so cleanup and leave.
1353 	 */
1354 	dfs_free_list(headp);
1355 	(void) fclose(fp);
1356 	return (NULL);
1357 }
1358 
1359 /*
1360  * parse_sharetab(handle)
1361  *
1362  * Read the /etc/dfs/sharetab file and see which entries don't exist
1363  * in the repository. These shares are marked transient.  We also need
1364  * to see if they are ZFS shares since ZFS bypasses the SMF
1365  * repository.
1366  */
1367 
1368 int
1369 parse_sharetab(sa_handle_t handle)
1370 {
1371 	xfs_sharelist_t *list, *tmplist;
1372 	int err = 0;
1373 	sa_share_t share;
1374 	sa_group_t group;
1375 	sa_group_t lgroup;
1376 	char *groupname;
1377 	int legacy = 0;
1378 
1379 	list = get_share_list(&err);
1380 	if (list == NULL)
1381 	    return (legacy);
1382 
1383 	lgroup = sa_get_group(handle, "default");
1384 
1385 	for (tmplist = list; tmplist != NULL; tmplist = tmplist->next) {
1386 	    group = NULL;
1387 	    share = sa_find_share(handle, tmplist->path);
1388 	    if (share == NULL) {
1389 		/*
1390 		 * this share is transient so needs to be
1391 		 * added. Initially, this will be under
1392 		 * default(legacy) unless it is a ZFS
1393 		 * share. If zfs, we need a zfs group.
1394 		 */
1395 		if (tmplist->resource != NULL &&
1396 		    (groupname = strchr(tmplist->resource, '@')) != NULL) {
1397 		    /* there is a defined group */
1398 		    *groupname++ = '\0';
1399 		    group = sa_get_group(handle, groupname);
1400 		    if (group != NULL) {
1401 			share = _sa_add_share(group, tmplist->path,
1402 						SA_SHARE_TRANSIENT, &err);
1403 		    } else {
1404 			/*
1405 			 * While this case shouldn't occur very often,
1406 			 * it does occur out of a "zfs set
1407 			 * sharenfs=off" when the dataset is also set
1408 			 * to canmount=off. A warning will then cause
1409 			 * the zfs command to abort. Since we add it
1410 			 * to the default list, everything works
1411 			 * properly anyway and the library doesn't
1412 			 * need to give a warning.
1413 			 */
1414 			share = _sa_add_share(lgroup, tmplist->path,
1415 						SA_SHARE_TRANSIENT, &err);
1416 		    }
1417 		} else {
1418 		    if (sa_zfs_is_shared(handle, tmplist->path)) {
1419 			group = sa_get_group(handle, "zfs");
1420 			if (group == NULL) {
1421 			    group = sa_create_group(handle, "zfs", &err);
1422 			    if (group == NULL && err == SA_NO_PERMISSION) {
1423 				group = _sa_create_group(
1424 						(sa_handle_impl_t)handle,
1425 						"zfs");
1426 			    }
1427 			    if (group != NULL) {
1428 				(void) sa_create_optionset(group,
1429 							    tmplist->fstype);
1430 				(void) sa_set_group_attr(group, "zfs", "true");
1431 			    }
1432 			}
1433 			if (group != NULL) {
1434 			    share = _sa_add_share(group, tmplist->path,
1435 						    SA_SHARE_TRANSIENT, &err);
1436 			}
1437 		    } else {
1438 			share = _sa_add_share(lgroup, tmplist->path,
1439 						SA_SHARE_TRANSIENT, &err);
1440 		    }
1441 		}
1442 		if (share == NULL)
1443 		    (void) printf(dgettext(TEXT_DOMAIN,
1444 					    "Problem with transient: %s\n"),
1445 				    sa_errorstr(err));
1446 		if (share != NULL)
1447 		    set_node_attr(share, "shared", "true");
1448 
1449 		if (err == SA_OK) {
1450 		    if (tmplist->options != NULL &&
1451 			strlen(tmplist->options) > 0) {
1452 			(void) sa_parse_legacy_options(share,
1453 							tmplist->options,
1454 							tmplist->fstype);
1455 		    }
1456 		    if (tmplist->resource != NULL &&
1457 			strcmp(tmplist->resource, "-") != 0)
1458 			set_node_attr(share, "resource", tmplist->resource);
1459 		    if (tmplist->description != NULL) {
1460 			xmlNodePtr node;
1461 			node = xmlNewChild((xmlNodePtr)share, NULL,
1462 						(xmlChar *)"description", NULL);
1463 			xmlNodeSetContent(node,
1464 					    (xmlChar *)tmplist->description);
1465 		    }
1466 		    legacy = 1;
1467 		}
1468 	    } else {
1469 		/*
1470 		 * if this is a legacy share, mark as shared so we
1471 		 * only update sharetab appropriately. We also keep
1472 		 * the sharetab options in order to display for legacy
1473 		 * share with no arguments.
1474 		 */
1475 		set_node_attr(share, "shared", "true");
1476 		set_node_attr(share, "shareopts", tmplist->options);
1477 	    }
1478 	}
1479 	dfs_free_list(list);
1480 	return (legacy);
1481 }
1482 
1483 /*
1484  * get the transient shares from the sharetab (or other) file.  since
1485  * these are transient, they only appear in the working file and not
1486  * in a repository.
1487  */
1488 int
1489 gettransients(sa_handle_impl_t ihandle, xmlNodePtr *root)
1490 {
1491 	int legacy = 0;
1492 
1493 	if (root != NULL) {
1494 	    if (*root == NULL)
1495 		*root = xmlNewNode(NULL, (xmlChar *)"sharecfg");
1496 	    if (*root != NULL) {
1497 		legacy = parse_sharetab(ihandle);
1498 	    }
1499 	}
1500 	return (legacy);
1501 }
1502 
1503 /*
1504  * sa_has_prop(optionset, prop)
1505  *
1506  * Is the specified property a member of the optionset?
1507  */
1508 
1509 int
1510 sa_has_prop(sa_optionset_t optionset, sa_property_t prop)
1511 {
1512 	char *name;
1513 	sa_property_t otherprop;
1514 	int result = 0;
1515 
1516 	if (optionset != NULL) {
1517 	    name = sa_get_property_attr(prop, "type");
1518 	    if (name != NULL) {
1519 		otherprop = sa_get_property(optionset, name);
1520 		if (otherprop != NULL)
1521 		    result = 1;
1522 		sa_free_attr_string(name);
1523 	    }
1524 	}
1525 	return (result);
1526 }
1527 
1528 /*
1529  * Update legacy files
1530  *
1531  * Provides functions to add/remove/modify individual entries
1532  * in dfstab and sharetab
1533  */
1534 
1535 void
1536 update_legacy_config(sa_handle_t handle)
1537 {
1538 	/*
1539 	 * no longer used -- this is a placeholder in case we need to
1540 	 * add it back later.
1541 	 */
1542 #ifdef lint
1543 	handle = handle;
1544 #endif
1545 }
1546 
1547 /*
1548  * sa_valid_property(object, proto, property)
1549  *
1550  * check to see if the specified property is valid relative to the
1551  * specified protocol. The protocol plugin is called to do the work.
1552  */
1553 
1554 int
1555 sa_valid_property(void *object, char *proto, sa_property_t property)
1556 {
1557 	int ret = SA_OK;
1558 
1559 	if (proto != NULL && property != NULL) {
1560 	    ret = sa_proto_valid_prop(proto, property, object);
1561 	}
1562 
1563 	return (ret);
1564 }
1565 
1566 /*
1567  * sa_fstype(path)
1568  *
1569  * Given path, return the string representing the path's file system
1570  * type. This is used to discover ZFS shares.
1571  */
1572 
1573 char *
1574 sa_fstype(char *path)
1575 {
1576 	int err;
1577 	struct stat st;
1578 
1579 	err = stat(path, &st);
1580 	if (err < 0) {
1581 	    err = SA_NO_SUCH_PATH;
1582 	} else {
1583 	    err = SA_OK;
1584 	}
1585 	if (err == SA_OK) {
1586 		/* have a valid path at this point */
1587 	    return (strdup(st.st_fstype));
1588 	}
1589 	return (NULL);
1590 }
1591 
1592 void
1593 sa_free_fstype(char *type)
1594 {
1595 	free(type);
1596 }
1597 
1598 /*
1599  * sa_get_derived_optionset(object, proto, hier)
1600  *
1601  *	Work backward to the top of the share object tree and start
1602  *	copying protocol specific optionsets into a newly created
1603  *	optionset that doesn't have a parent (it will be freed
1604  *	later). This provides for the property inheritence model. That
1605  *	is, properties closer to the share take precedence over group
1606  *	level. This also provides for groups of groups in the future.
1607  */
1608 
1609 sa_optionset_t
1610 sa_get_derived_optionset(void *object, char *proto, int hier)
1611 {
1612 	sa_optionset_t newoptionset;
1613 	sa_optionset_t optionset;
1614 	sa_group_t group;
1615 
1616 	if (hier &&
1617 	    (group = sa_get_parent_group((sa_share_t)object)) != NULL) {
1618 	    newoptionset = sa_get_derived_optionset((void *)group, proto, hier);
1619 	} else {
1620 	    newoptionset = (sa_optionset_t)xmlNewNode(NULL,
1621 						    (xmlChar *)"optionset");
1622 	    if (newoptionset != NULL) {
1623 		sa_set_optionset_attr(newoptionset, "type", proto);
1624 	    }
1625 	}
1626 	/* dont' do anything if memory wasn't allocated */
1627 	if (newoptionset == NULL)
1628 	    return (NULL);
1629 
1630 	/* found the top so working back down the stack */
1631 	optionset = sa_get_optionset((sa_optionset_t)object, proto);
1632 	if (optionset != NULL) {
1633 	    sa_property_t prop;
1634 	    /* add optionset to the newoptionset */
1635 	    for (prop = sa_get_property(optionset, NULL);
1636 		prop != NULL; prop = sa_get_next_property(prop)) {
1637 		sa_property_t newprop;
1638 		char *name;
1639 		char *value;
1640 		name = sa_get_property_attr(prop, "type");
1641 		value = sa_get_property_attr(prop, "value");
1642 		if (name != NULL) {
1643 		    newprop = sa_get_property(newoptionset, name);
1644 		    /* replace the value with the new value */
1645 		    if (newprop != NULL) {
1646 			/*
1647 			 * only set if value is non NULL, old value ok
1648 			 * if it is NULL.
1649 			 */
1650 			if (value != NULL)
1651 			    set_node_attr(newprop, "value", value);
1652 		    } else {
1653 			/* an entirely new property */
1654 			if (value != NULL) {
1655 			    newprop = sa_create_property(name, value);
1656 			    if (newprop != NULL) {
1657 				newprop = (sa_property_t)
1658 				    xmlAddChild((xmlNodePtr)newoptionset,
1659 						(xmlNodePtr)newprop);
1660 			    }
1661 			}
1662 		    }
1663 		    sa_free_attr_string(name);
1664 		}
1665 		if (value != NULL)
1666 		    sa_free_attr_string(value);
1667 	    }
1668 	}
1669 	return (newoptionset);
1670 }
1671 
1672 void
1673 sa_free_derived_optionset(sa_optionset_t optionset)
1674 {
1675 	/* while it shouldn't be linked, it doesn't hurt */
1676 	if (optionset != NULL) {
1677 	    xmlUnlinkNode((xmlNodePtr) optionset);
1678 	    xmlFreeNode((xmlNodePtr) optionset);
1679 	}
1680 }
1681 
1682 /*
1683  *  sa_get_all_security_types(object, proto, hier)
1684  *
1685  *	find all the security types set for this object.  This is
1686  *	preliminary to getting a derived security set. The return value is an
1687  *	optionset containg properties which are the sectype values found by
1688  *	walking up the XML document struture. The returned optionset
1689  *	is a derived optionset.
1690  *
1691  *	If hier is 0, only look at object. If non-zero, walk up the tree.
1692  */
1693 sa_optionset_t
1694 sa_get_all_security_types(void *object, char *proto, int hier)
1695 {
1696 	sa_optionset_t options;
1697 	sa_security_t security;
1698 	sa_group_t group;
1699 	sa_property_t prop;
1700 
1701 	options = NULL;
1702 
1703 	if (hier &&
1704 	    (group = sa_get_parent_group((sa_share_t)object)) != NULL) {
1705 	    options = sa_get_all_security_types((void *)group, proto, hier);
1706 	} else {
1707 	    options = (sa_optionset_t)xmlNewNode(NULL,
1708 						    (xmlChar *)"optionset");
1709 	}
1710 	/* hit the top so collect the security types working back */
1711 	if (options != NULL) {
1712 	    for (security = sa_get_security((sa_group_t)object, NULL, NULL);
1713 		security != NULL; security = sa_get_next_security(security)) {
1714 		char *type;
1715 		char *sectype;
1716 
1717 		type = sa_get_security_attr(security, "type");
1718 		if (type != NULL) {
1719 		    if (strcmp(type, proto) != 0) {
1720 			sa_free_attr_string(type);
1721 			continue;
1722 		    }
1723 		    sectype = sa_get_security_attr(security, "sectype");
1724 		    if (sectype != NULL) {
1725 			/*
1726 			 * have a security type, check to see if
1727 			 * already present in optionset and add if it
1728 			 * isn't.
1729 			 */
1730 			if (sa_get_property(options, sectype) == NULL) {
1731 			    prop = sa_create_property(sectype, "true");
1732 			    if (prop != NULL)
1733 				prop = (sa_property_t)
1734 				    xmlAddChild((xmlNodePtr)options,
1735 						(xmlNodePtr)prop);
1736 			}
1737 			sa_free_attr_string(sectype);
1738 		    }
1739 		    sa_free_attr_string(type);
1740 		}
1741 	    }
1742 	}
1743 	return (options);
1744 }
1745 
1746 /*
1747  * sa_get_derived_security(object, sectype, proto, hier)
1748  *
1749  * Get the derived security(named optionset) for the object given the
1750  * sectype and proto. If hier is non-zero, walk up the tree to get all
1751  * properties defined for this object, otherwise just those on the
1752  * object.
1753  */
1754 
1755 sa_security_t
1756 sa_get_derived_security(void *object, char *sectype, char *proto, int hier)
1757 {
1758 	sa_security_t newsecurity;
1759 	sa_security_t security;
1760 	sa_group_t group;
1761 
1762 	if (hier &&
1763 	    (group = sa_get_parent_group((sa_share_t)object)) != NULL) {
1764 	    newsecurity = sa_get_derived_security((void *)group,
1765 						    sectype, proto, hier);
1766 	} else {
1767 	    newsecurity = (sa_security_t)xmlNewNode(NULL,
1768 						    (xmlChar *)"security");
1769 	    if (newsecurity != NULL) {
1770 		sa_set_security_attr(newsecurity, "type", proto);
1771 		sa_set_security_attr(newsecurity, "sectype", sectype);
1772 	    }
1773 	}
1774 	/* dont' do anything if memory wasn't allocated */
1775 	if (newsecurity == NULL)
1776 	    return (NULL);
1777 
1778 	/* found the top so working back down the stack */
1779 	security = sa_get_security((sa_security_t)object, sectype, proto);
1780 	if (security != NULL) {
1781 	    sa_property_t prop;
1782 	    /* add security to the newsecurity */
1783 	    for (prop = sa_get_property(security, NULL);
1784 		prop != NULL; prop = sa_get_next_property(prop)) {
1785 		sa_property_t newprop;
1786 		char *name;
1787 		char *value;
1788 		name = sa_get_property_attr(prop, "type");
1789 		value = sa_get_property_attr(prop, "value");
1790 		if (name != NULL) {
1791 		    newprop = sa_get_property(newsecurity, name);
1792 		    /* replace the value with the new value */
1793 		    if (newprop != NULL) {
1794 			/*
1795 			 * only set if value is non NULL, old value ok
1796 			 * if it is NULL.
1797 			 */
1798 			if (value != NULL)
1799 			    set_node_attr(newprop, name, value);
1800 		    } else {
1801 			/* an entirely new property */
1802 			if (value != NULL) {
1803 			    newprop = sa_create_property(name, value);
1804 			    newprop = (sa_property_t)
1805 				xmlAddChild((xmlNodePtr)newsecurity,
1806 					    (xmlNodePtr)newprop);
1807 			}
1808 		    }
1809 		    sa_free_attr_string(name);
1810 		}
1811 		if (value != NULL)
1812 		    sa_free_attr_string(value);
1813 	    }
1814 	}
1815 	return (newsecurity);
1816 }
1817 
1818 void
1819 sa_free_derived_security(sa_security_t security)
1820 {
1821 	/* while it shouldn't be linked, it doesn't hurt */
1822 	if (security != NULL) {
1823 	    xmlUnlinkNode((xmlNodePtr)security);
1824 	    xmlFreeNode((xmlNodePtr)security);
1825 	}
1826 }
1827 
1828 /*
1829  * sharetab utility functions
1830  *
1831  * makes use of the original sharetab.c from fs.d/nfs/lib
1832  */
1833 
1834 /*
1835  * fillshare(share, proto, sh)
1836  *
1837  * Fill the struct share with values obtained from the share object.
1838  */
1839 static void
1840 fillshare(sa_share_t share, char *proto, struct share *sh)
1841 {
1842 	char *groupname = NULL;
1843 	char *value;
1844 	sa_group_t group;
1845 	char *buff;
1846 	char *zfs;
1847 
1848 	group = sa_get_parent_group(share);
1849 	if (group != NULL) {
1850 	    zfs = sa_get_group_attr(group, "zfs");
1851 	    groupname = sa_get_group_attr(group, "name");
1852 
1853 	    if (groupname != NULL &&
1854 		(strcmp(groupname, "default") == 0 || zfs != NULL)) {
1855 		/*
1856 		 * since the groupname is either "default" or the
1857 		 * group is a ZFS group, we don't want to keep
1858 		 * groupname. We do want it if it is any other type of
1859 		 * group.
1860 		 */
1861 		sa_free_attr_string(groupname);
1862 		groupname = NULL;
1863 	    }
1864 	    if (zfs != NULL)
1865 		sa_free_attr_string(zfs);
1866 	}
1867 
1868 	value = sa_get_share_attr(share, "path");
1869 	if (value != NULL) {
1870 	    sh->sh_path = strdup(value);
1871 	    sa_free_attr_string(value);
1872 	}
1873 
1874 	value = sa_get_share_attr(share, "resource");
1875 	if (value != NULL || groupname != NULL) {
1876 	    int len = 0;
1877 
1878 	    if (value != NULL)
1879 		len += strlen(value);
1880 	    if (groupname != NULL)
1881 		len += strlen(groupname);
1882 	    len += 3; /* worst case */
1883 	    buff = malloc(len);
1884 	    (void) snprintf(buff, len, "%s%s%s",
1885 		    (value != NULL && strlen(value) > 0) ? value : "-",
1886 		    groupname != NULL ? "@" : "",
1887 		    groupname != NULL ? groupname : "");
1888 	    sh->sh_res = buff;
1889 	    if (value != NULL)
1890 		sa_free_attr_string(value);
1891 	    if (groupname != NULL) {
1892 		sa_free_attr_string(groupname);
1893 		groupname = NULL;
1894 	    }
1895 	} else {
1896 	    sh->sh_res = strdup("-");
1897 	}
1898 
1899 	sh->sh_fstype = strdup(proto);
1900 	value = sa_proto_legacy_format(proto, share, 1);
1901 	if (value != NULL) {
1902 	    if (strlen(value) > 0)
1903 		sh->sh_opts = strdup(value);
1904 	    else
1905 		sh->sh_opts = strdup("rw");
1906 	    free(value);
1907 	} else
1908 	    sh->sh_opts = strdup("rw");
1909 
1910 	value = sa_get_share_description(share);
1911 	if (value != NULL) {
1912 	    sh->sh_descr = strdup(value);
1913 	    sa_free_share_description(value);
1914 	} else
1915 	    sh->sh_descr = strdup("");
1916 }
1917 
1918 /*
1919  * emptyshare(sh)
1920  *
1921  * Free the strings in the non-NULL members of sh.
1922  */
1923 
1924 static void
1925 emptyshare(struct share *sh)
1926 {
1927 	if (sh->sh_path != NULL)
1928 	    free(sh->sh_path);
1929 	sh->sh_path = NULL;
1930 	if (sh->sh_res != NULL)
1931 	    free(sh->sh_res);
1932 	sh->sh_res = NULL;
1933 	if (sh->sh_fstype != NULL)
1934 	    free(sh->sh_fstype);
1935 	sh->sh_fstype = NULL;
1936 	if (sh->sh_opts != NULL)
1937 	    free(sh->sh_opts);
1938 	sh->sh_opts = NULL;
1939 	if (sh->sh_descr != NULL)
1940 	    free(sh->sh_descr);
1941 	sh->sh_descr = NULL;
1942 }
1943 
1944 /*
1945  * sa_update_sharetab(share, proto)
1946  *
1947  * Update the sharetab file with info from the specified share.
1948  * This could be an update or add.
1949  */
1950 
1951 int
1952 sa_update_sharetab(sa_share_t share, char *proto)
1953 {
1954 	int	ret = SA_OK;
1955 	share_t	sh;
1956 	char	*path;
1957 
1958 	path = sa_get_share_attr(share, "path");
1959 	if (path != NULL) {
1960 		(void) memset(&sh, '\0', sizeof (sh));
1961 
1962 		/*
1963 		 * Fill in share structure and send it to the kernel.
1964 		 */
1965 		(void) fillshare(share, proto, &sh);
1966 		(void) sharefs(SHAREFS_ADD, &sh);
1967 		emptyshare(&sh);
1968 		sa_free_attr_string(path);
1969 	}
1970 
1971 	return (ret);
1972 }
1973 
1974 /*
1975  * sa_delete_sharetab(path, proto)
1976  *
1977  * remove the specified share from sharetab.
1978  */
1979 
1980 int
1981 sa_delete_sharetab(char *path, char *proto)
1982 {
1983 	int	ret = SA_OK;
1984 
1985 	share_t	sh;
1986 
1987 	/*
1988 	 * Both the path and the proto are
1989 	 * keys into the sharetab.
1990 	 */
1991 	if (path != NULL && proto != NULL) {
1992 		(void) memset(&sh, '\0', sizeof (sh));
1993 		sh.sh_path = path;
1994 		sh.sh_fstype = proto;
1995 
1996 		ret = sharefs(SHAREFS_REMOVE, &sh);
1997 	}
1998 
1999 	return (ret);
2000 }
2001