xref: /illumos-gate/usr/src/lib/libshare/common/libsharecore.c (revision dd72704bd9e794056c558153663c739e2012d721)
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 (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Copyright 2022 Tintri by DDN, Inc. All rights reserved.
25  * Copyright (c) 2016 by Delphix. All rights reserved.
26  */
27 
28 /*
29  * core library for common functions across all config store types
30  * and file systems to be exported. This includes legacy dfstab/sharetab
31  * parsing. Need to eliminate XML where possible.
32  */
33 
34 #include <stdio.h>
35 #include <string.h>
36 #include <ctype.h>
37 #include <unistd.h>
38 #include <limits.h>
39 #include <errno.h>
40 #include <sys/types.h>
41 #include <sys/stat.h>
42 #include <libxml/parser.h>
43 #include <libxml/tree.h>
44 #include "libshare.h"
45 #include "libshare_impl.h"
46 #include <fcntl.h>
47 #include <thread.h>
48 #include <grp.h>
49 #include <limits.h>
50 #include <sys/param.h>
51 #include <signal.h>
52 #include <libintl.h>
53 #include <dirent.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(8) 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 static mutex_t sharetab_lock = DEFAULTMUTEX;
73 extern mutex_t sa_dfstab_lock;
74 
75 /* used internally only */
76 typedef
77 struct sharelist {
78     struct sharelist *next;
79     int   persist;
80     char *path;
81     char *resource;
82     char *fstype;
83     char *options;
84     char *description;
85     char *group;
86     char *origline;
87     int lineno;
88 } xfs_sharelist_t;
89 static void parse_dfstab(sa_handle_t, char *, xmlNodePtr);
90 extern char *_sa_get_token(char *);
91 static void dfs_free_list(xfs_sharelist_t *);
92 /* prototypes */
93 void getlegacyconfig(sa_handle_t, char *, xmlNodePtr *);
94 extern sa_share_t _sa_add_share(sa_group_t, char *, int, int *, uint64_t);
95 extern sa_group_t _sa_create_group(sa_handle_impl_t, char *);
96 static void outdfstab(FILE *, xfs_sharelist_t *);
97 extern int _sa_remove_optionset(sa_optionset_t);
98 extern int set_node_share(void *, char *, char *);
99 extern void set_node_attr(void *, char *, char *);
100 
101 /*
102  * sablocksigs(*sigs)
103  *
104  * block important signals for a critical region. Arg is a pointer to
105  * a sigset_t that is used later for the unblock.
106  */
107 void
108 sablocksigs(sigset_t *sigs)
109 {
110 	sigset_t new;
111 
112 	if (sigs != NULL) {
113 		(void) sigprocmask(SIG_BLOCK, NULL, &new);
114 		(void) sigaddset(&new, SIGHUP);
115 		(void) sigaddset(&new, SIGINT);
116 		(void) sigaddset(&new, SIGQUIT);
117 		(void) sigaddset(&new, SIGTSTP);
118 		(void) sigprocmask(SIG_SETMASK, &new, sigs);
119 	}
120 }
121 
122 /*
123  * saunblocksigs(*sigs)
124  *
125  * unblock previously blocked signals from the sigs arg.
126  */
127 void
128 saunblocksigs(sigset_t *sigs)
129 {
130 	if (sigs != NULL)
131 		(void) sigprocmask(SIG_SETMASK, sigs, NULL);
132 }
133 
134 /*
135  * alloc_sharelist()
136  *
137  * allocator function to return an zfs_sharelist_t
138  */
139 
140 static xfs_sharelist_t *
141 alloc_sharelist()
142 {
143 	xfs_sharelist_t *item;
144 
145 	item = (xfs_sharelist_t *)malloc(sizeof (xfs_sharelist_t));
146 	if (item != NULL)
147 		(void) memset(item, '\0', sizeof (xfs_sharelist_t));
148 	return (item);
149 }
150 
151 /*
152  * fix_notice(list)
153  *
154  * Look at the beginning of the current /etc/dfs/dfstab file and add
155  * the do not modify notice if it doesn't exist.
156  */
157 
158 static xfs_sharelist_t *
159 fix_notice(xfs_sharelist_t *list)
160 {
161 	xfs_sharelist_t *item, *prev;
162 	int i;
163 
164 	if (list == NULL) {
165 		/* zero length dfstab */
166 		list = alloc_sharelist();
167 		if (list == NULL)
168 			return (NULL);
169 		list->description = strdup("#\n");
170 	}
171 	if (list->path == NULL && list->description != NULL &&
172 	    strcmp(list->description, notice[0]) != 0) {
173 		for (prev = NULL, i = 0; i < DFSTAB_NOTICE_LINES; i++) {
174 			item = alloc_sharelist();
175 			if (item != NULL) {
176 				item->description = strdup(notice[i]);
177 				if (prev == NULL) {
178 					item->next = list;
179 					prev = item;
180 					list = item;
181 				} else {
182 					item->next = prev->next;
183 					prev->next = item;
184 					prev = item;
185 				}
186 			}
187 		}
188 	}
189 	return (list);
190 }
191 
192 /*
193  * getdfstab(dfs)
194  *
195  * Returns an zfs_sharelist_t list of lines from the dfstab file
196  * pointed to by the FILE pointer dfs. Each entry is parsed and the
197  * original line is also preserved. Used in parsing and updating the
198  * dfstab file.
199  */
200 
201 static xfs_sharelist_t *
202 getdfstab(FILE *dfs)
203 {
204 	char buff[_POSIX_ARG_MAX]; /* reasonable size given syntax of share */
205 	char *bp;
206 	char *token;
207 	char *args[MAXARGSFORSHARE];
208 	int argc;
209 	int c;
210 	static int line = 0;
211 	xfs_sharelist_t *item = NULL, *first = NULL, *last;
212 
213 	if (dfs != NULL) {
214 		first = NULL;
215 		line = 0;
216 		while (fgets(buff, sizeof (buff), dfs) != NULL) {
217 			line++;
218 			bp = buff;
219 			if (buff[0] == '#') {
220 				item = alloc_sharelist();
221 				if (item != NULL) {
222 					/* if no path, then comment */
223 					item->lineno = line;
224 					item->description = strdup(buff);
225 					if (first == NULL) {
226 						first = item;
227 						last = item;
228 					} else {
229 						last->next = item;
230 						last = item;
231 					}
232 				} else {
233 					break;
234 				}
235 				continue;
236 			} else if (buff[0] == '\n') {
237 				continue;
238 			}
239 			optind = 1;
240 			item = alloc_sharelist();
241 			if (item == NULL) {
242 				break;
243 			} else if (first == NULL) {
244 				first = item;
245 				last = item;
246 			} else {
247 				last->next = item;
248 				last = item;
249 			}
250 			item->lineno = line;
251 			item->origline = strdup(buff);
252 			(void) _sa_get_token(NULL); /* reset to new pointers */
253 			argc = 0;
254 			while ((token = _sa_get_token(bp)) != NULL) {
255 				if (argc < MAXARGSFORSHARE)
256 					args[argc++] = token;
257 			}
258 			while ((c = getopt(argc, args, "F:o:d:pg:")) != -1) {
259 				switch (c) {
260 				case 'p':
261 					item->persist = 1;
262 					break;
263 				case 'F':
264 					item->fstype = strdup(optarg);
265 					break;
266 				case 'o':
267 					item->options = strdup(optarg);
268 					break;
269 				case 'd':
270 					item->description = strdup(optarg);
271 					break;
272 				case 'g':
273 					item->group = strdup(optarg);
274 					break;
275 				default:
276 					break;
277 				}
278 			}
279 			if (optind < argc) {
280 				item->path = strdup(args[optind]);
281 				optind++;
282 				if (optind < argc) {
283 					char *resource;
284 					char *optgroup;
285 					/* resource and/or groupname */
286 					resource = args[optind];
287 					optgroup = strchr(resource, '@');
288 					if (optgroup != NULL)
289 						*optgroup++ = '\0';
290 					if (optgroup != NULL)
291 						item->group = strdup(optgroup);
292 					if (resource != NULL &&
293 					    strlen(resource) > 0)
294 						item->resource =
295 						    strdup(resource);
296 				}
297 			}
298 			/* NFS is the default if none defined */
299 			if (item != NULL && item->fstype == NULL)
300 				item->fstype = strdup("nfs");
301 		}
302 	}
303 	first = fix_notice(first);
304 	return (first);
305 }
306 
307 /*
308  * finddfsentry(list, path)
309  *
310  * Look for path in the zfs_sharelist_t list and return the entry if it
311  * exists.
312  */
313 
314 static xfs_sharelist_t *
315 finddfsentry(xfs_sharelist_t *list, char *path)
316 {
317 	xfs_sharelist_t *item;
318 
319 	for (item = list; item != NULL; item = item->next) {
320 		if (item->path != NULL && strcmp(item->path, path) == 0)
321 		return (item);
322 	}
323 	return (NULL);
324 }
325 
326 /*
327  * remdfsentry(list, path, proto)
328  *
329  * Remove the specified path (with protocol) from the list. This will
330  * remove it from dfstab when the file is rewritten.
331  */
332 
333 static xfs_sharelist_t *
334 remdfsentry(xfs_sharelist_t *list, char *path, char *proto)
335 {
336 	xfs_sharelist_t *item, *prev = NULL;
337 
338 
339 	for (item = prev = list; item != NULL; item = item->next) {
340 	    /* skip comment entry but don't lose it */
341 		if (item->path == NULL) {
342 			prev = item;
343 			continue;
344 		}
345 		/* if proto is NULL, remove all protocols */
346 		if (proto == NULL || (strcmp(item->path, path) == 0 &&
347 		    (item->fstype != NULL && strcmp(item->fstype, proto) == 0)))
348 			break;
349 		if (item->fstype == NULL &&
350 		    (proto == NULL || strcmp(proto, "nfs") == 0))
351 			break;
352 		prev = item;
353 	}
354 	if (item != NULL) {
355 		if (item == prev)
356 			list = item->next; /* this must be the first one */
357 		else
358 			prev->next = item->next;
359 		item->next = NULL;
360 		dfs_free_list(item);
361 	}
362 	return (list);
363 }
364 
365 /*
366  * remdfsline(list, line)
367  *
368  * Remove the line specified from the list.
369  */
370 
371 static xfs_sharelist_t *
372 remdfsline(xfs_sharelist_t *list, char *line)
373 {
374 	xfs_sharelist_t *item, *prev = NULL;
375 
376 	for (item = prev = list; item != NULL; item = item->next) {
377 		/* skip comment entry but don't lose it */
378 		if (item->path == NULL) {
379 		prev = item;
380 		continue;
381 		}
382 		if (strcmp(item->origline, line) == 0)
383 			break;
384 		prev = item;
385 	}
386 	if (item != NULL) {
387 		if (item == prev)
388 			list = item->next; /* this must be the first one */
389 		else
390 			prev->next = item->next;
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 (groupname != NULL && 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 &&
430 		    strlen(item->description) == 0) {
431 			sa_free_share_description(item->description);
432 			item->description = NULL;
433 		}
434 		if (list == NULL) {
435 			list = item;
436 		} else {
437 			for (tmp = list; tmp->next != NULL; tmp = tmp->next)
438 				/* do nothing */;
439 			tmp->next = item;
440 		}
441 	}
442 	return (list);
443 }
444 
445 /*
446  * outdfstab(dfstab, list)
447  *
448  * Output the list to dfstab making sure the file is truncated.
449  * Comments and errors are preserved.
450  */
451 
452 static void
453 outdfstab(FILE *dfstab, xfs_sharelist_t *list)
454 {
455 	xfs_sharelist_t *item;
456 
457 	(void) ftruncate(fileno(dfstab), 0);
458 
459 	for (item = list; item != NULL; item = item->next) {
460 		if (item->path != NULL) {
461 			if (*item->path == '/') {
462 				(void) fprintf(dfstab,
463 				    "share %s%s%s%s%s%s%s %s%s%s%s%s\n",
464 				    (item->fstype != NULL) ? "-F " : "",
465 				    (item->fstype != NULL) ? item->fstype : "",
466 				    (item->options != NULL) ? " -o " : "",
467 				    (item->options != NULL) ?
468 				    item->options : "",
469 				    (item->description != NULL) ?
470 				    " -d \"" : "",
471 				    (item->description != NULL) ?
472 				    item->description : "",
473 				    (item->description != NULL) ? "\"" : "",
474 				    item->path,
475 				    ((item->resource != NULL) ||
476 				    (item->group != NULL)) ? " " : "",
477 				    (item->resource != NULL) ?
478 				    item->resource : "",
479 				    item->group != NULL ? "@" : "",
480 				    item->group != NULL ? item->group : "");
481 			} else {
482 				(void) fprintf(dfstab, "%s", item->origline);
483 			}
484 		} else {
485 			if (item->description != NULL)
486 				(void) fprintf(dfstab, "%s", item->description);
487 			else
488 				(void) fprintf(dfstab, "%s", item->origline);
489 		}
490 	}
491 }
492 
493 /*
494  * open_dfstab(file)
495  *
496  * Open the specified dfstab file. If the owner/group/perms are wrong,
497  * fix them.
498  */
499 
500 static FILE *
501 open_dfstab(char *file)
502 {
503 	struct group *grp;
504 	struct group group;
505 	char *buff;
506 	int grsize;
507 	FILE *dfstab;
508 
509 	dfstab = fopen(file, "r+");
510 	if (dfstab == NULL) {
511 		dfstab = fopen(file, "w+");
512 	}
513 	if (dfstab != NULL) {
514 		grsize = sysconf(_SC_GETGR_R_SIZE_MAX);
515 		buff = malloc(grsize);
516 		if (buff != NULL)
517 			grp = getgrnam_r(SA_DEFAULT_FILE_GRP, &group, buff,
518 			    grsize);
519 		else
520 			grp = getgrnam(SA_DEFAULT_FILE_GRP);
521 		(void) fchmod(fileno(dfstab), 0644);
522 		(void) fchown(fileno(dfstab), 0,
523 		    grp != NULL ? grp->gr_gid : 3);
524 		if (buff != NULL)
525 			free(buff);
526 		rewind(dfstab);
527 	}
528 	return (dfstab);
529 }
530 
531 /*
532  * sa_comment_line(line, err)
533  *
534  * Add a comment to the dfstab file with err as a prefix to the
535  * original line.
536  */
537 
538 static void
539 sa_comment_line(char *line, char *err)
540 {
541 	FILE *dfstab;
542 	xfs_sharelist_t *list;
543 	sigset_t old;
544 
545 	dfstab = open_dfstab(SA_LEGACY_DFSTAB);
546 	if (dfstab != NULL) {
547 		(void) setvbuf(dfstab, NULL, _IOLBF, BUFSIZ * 8);
548 		sablocksigs(&old);
549 		(void) lockf(fileno(dfstab), F_LOCK, 0);
550 		(void) mutex_lock(&sa_dfstab_lock);
551 		list = getdfstab(dfstab);
552 		rewind(dfstab);
553 		/*
554 		 * don't ignore the return since the list could have
555 		 * gone to NULL if the file only had one line in it.
556 		 */
557 		list = remdfsline(list, line);
558 		outdfstab(dfstab, list);
559 		(void) fprintf(dfstab, "# Error: %s: %s", err, line);
560 		(void) fsync(fileno(dfstab));
561 		(void) mutex_unlock(&sa_dfstab_lock);
562 		(void) lockf(fileno(dfstab), F_ULOCK, 0);
563 		(void) fclose(dfstab);
564 		saunblocksigs(&old);
565 		if (list != NULL)
566 			dfs_free_list(list);
567 	}
568 }
569 
570 /*
571  * sa_delete_legacy(share, protocol)
572  *
573  * Delete the specified share from the legacy config file.
574  */
575 
576 int
577 sa_delete_legacy(sa_share_t share, char *protocol)
578 {
579 	FILE *dfstab;
580 	int err;
581 	int ret = SA_OK;
582 	xfs_sharelist_t *list;
583 	char *path;
584 	sa_optionset_t optionset;
585 	sa_group_t parent;
586 	sigset_t old;
587 
588 	/*
589 	 * Protect against shares that don't have paths. This is not
590 	 * really an error at this point.
591 	 */
592 	path = sa_get_share_attr(share, "path");
593 	if (path == NULL)
594 		return (ret);
595 
596 	dfstab = open_dfstab(SA_LEGACY_DFSTAB);
597 	if (dfstab != NULL) {
598 		(void) setvbuf(dfstab, NULL, _IOLBF, BUFSIZ * 8);
599 		sablocksigs(&old);
600 		parent = sa_get_parent_group(share);
601 		if (parent != NULL) {
602 			(void) lockf(fileno(dfstab), F_LOCK, 0);
603 			(void) mutex_lock(&sa_dfstab_lock);
604 			list = getdfstab(dfstab);
605 			rewind(dfstab);
606 			if (protocol != NULL) {
607 				if (list != NULL)
608 					list = remdfsentry(list, path,
609 					    protocol);
610 			} else {
611 				for (optionset = sa_get_optionset(parent, NULL);
612 				    optionset != NULL;
613 				    optionset =
614 				    sa_get_next_optionset(optionset)) {
615 					char *proto = sa_get_optionset_attr(
616 					    optionset, "type");
617 
618 					if (list != NULL && proto != NULL)
619 						list = remdfsentry(list, path,
620 						    proto);
621 					if (proto == NULL)
622 						ret = SA_NO_MEMORY;
623 					/*
624 					 * may want to only do the dfstab if
625 					 * this call returns NOT IMPLEMENTED
626 					 * but it shouldn't hurt.
627 					 */
628 					if (ret == SA_OK) {
629 						err = sa_proto_delete_legacy(
630 						    proto, share);
631 						if (err != SA_NOT_IMPLEMENTED)
632 							ret = err;
633 					}
634 					if (proto != NULL)
635 						sa_free_attr_string(proto);
636 				}
637 			}
638 			outdfstab(dfstab, list);
639 			if (list != NULL)
640 				dfs_free_list(list);
641 			(void) fflush(dfstab);
642 			(void) mutex_unlock(&sa_dfstab_lock);
643 			(void) lockf(fileno(dfstab), F_ULOCK, 0);
644 		}
645 		(void) fsync(fileno(dfstab));
646 		saunblocksigs(&old);
647 		(void) fclose(dfstab);
648 	} else {
649 		if (errno == EACCES || errno == EPERM)
650 			ret = SA_NO_PERMISSION;
651 		else
652 			ret = SA_CONFIG_ERR;
653 	}
654 
655 	if (path != NULL)
656 		sa_free_attr_string(path);
657 
658 	return (ret);
659 }
660 
661 /*
662  * sa_update_legacy(share, proto)
663  *
664  * There is an assumption that dfstab will be the most common form of
665  * legacy configuration file for shares, but not the only one. Because
666  * of that, dfstab handling is done in the main code with calls to
667  * this function and protocol specific calls to deal with formatting
668  * options into dfstab/share compatible syntax. Since not everything
669  * will be dfstab, there is a provision for calling a protocol
670  * specific plugin interface that allows the protocol plugin to do its
671  * own legacy files and skip the dfstab update.
672  */
673 
674 int
675 sa_update_legacy(sa_share_t share, char *proto)
676 {
677 	FILE *dfstab;
678 	int ret = SA_OK;
679 	xfs_sharelist_t *list;
680 	char *path;
681 	sigset_t old;
682 	char *persist;
683 	uint64_t features;
684 
685 	ret = sa_proto_update_legacy(proto, share);
686 	if (ret != SA_NOT_IMPLEMENTED)
687 		return (ret);
688 
689 	features = sa_proto_get_featureset(proto);
690 	if (!(features & SA_FEATURE_DFSTAB))
691 		return (ret);
692 
693 	/* do the dfstab format */
694 	persist = sa_get_share_attr(share, "type");
695 	/*
696 	 * only update if the share is not transient -- no share type
697 	 * set or the type is not "transient".
698 	 */
699 	if (persist == NULL || strcmp(persist, "transient") != 0) {
700 		path = sa_get_share_attr(share, "path");
701 		if (path == NULL) {
702 			ret = SA_NO_MEMORY;
703 			goto out;
704 		}
705 		dfstab = open_dfstab(SA_LEGACY_DFSTAB);
706 		if (dfstab != NULL) {
707 			(void) setvbuf(dfstab, NULL, _IOLBF, BUFSIZ * 8);
708 			sablocksigs(&old);
709 			(void) lockf(fileno(dfstab), F_LOCK, 0);
710 			(void) mutex_lock(&sa_dfstab_lock);
711 			list = getdfstab(dfstab);
712 			rewind(dfstab);
713 			if (list != NULL)
714 				list = remdfsentry(list, path, proto);
715 			list = adddfsentry(list, share, proto);
716 			outdfstab(dfstab, list);
717 			(void) fflush(dfstab);
718 			(void) mutex_unlock(&sa_dfstab_lock);
719 			(void) lockf(fileno(dfstab), F_ULOCK, 0);
720 			(void) fsync(fileno(dfstab));
721 			saunblocksigs(&old);
722 			(void) fclose(dfstab);
723 			if (list != NULL)
724 				dfs_free_list(list);
725 		} else {
726 			if (errno == EACCES || errno == EPERM)
727 				ret = SA_NO_PERMISSION;
728 			else
729 				ret = SA_CONFIG_ERR;
730 		}
731 		sa_free_attr_string(path);
732 	}
733 out:
734 	if (persist != NULL)
735 		sa_free_attr_string(persist);
736 	return (ret);
737 }
738 
739 /*
740  * sa_is_security(optname, proto)
741  *
742  * Check to see if optname is a security (named optionset) specific
743  * property for the specified protocol.
744  */
745 
746 int
747 sa_is_security(char *optname, char *proto)
748 {
749 	int ret = 0;
750 	if (proto != NULL)
751 		ret = sa_proto_security_prop(proto, optname);
752 	return (ret);
753 }
754 
755 /*
756  * add_syntax_comment(root, line, err, todfstab)
757  *
758  * Add a comment to the document indicating a syntax error. If
759  * todfstab is set, write it back to the dfstab file as well.
760  */
761 
762 static void
763 add_syntax_comment(xmlNodePtr root, char *line, char *err, int todfstab)
764 {
765 	xmlNodePtr node;
766 
767 	node = xmlNewChild(root, NULL, (xmlChar *)"error", (xmlChar *)line);
768 	if (node != NULL)
769 		(void) xmlSetProp(node, (xmlChar *)"type", (xmlChar *)err);
770 	if (todfstab)
771 		sa_comment_line(line, err);
772 }
773 
774 /*
775  * sa_is_share(object)
776  *
777  * returns true if the object is of type "share".
778  */
779 
780 int
781 sa_is_share(void *object)
782 {
783 	if (object != NULL) {
784 		if (strcmp((char *)((xmlNodePtr)object)->name, "share") == 0)
785 		return (1);
786 	}
787 	return (0);
788 }
789 /*
790  * sa_is_resource(object)
791  *
792  * returns true if the object is of type "resource".
793  */
794 
795 int
796 sa_is_resource(void *object)
797 {
798 	if (object != NULL) {
799 		if (strcmp((char *)((xmlNodePtr)object)->name, "resource") == 0)
800 			return (1);
801 	}
802 	return (0);
803 }
804 
805 /*
806  * _sa_remove_property(property)
807  *
808  * remove a property only from the document.
809  */
810 
811 static void
812 _sa_remove_property(sa_property_t property)
813 {
814 	xmlUnlinkNode((xmlNodePtr)property);
815 	xmlFreeNode((xmlNodePtr)property);
816 }
817 
818 /*
819  * _sa_create_dummy_share()
820  *
821  * Create a share entry suitable for parsing but not tied to any real
822  * config tree.  Need to have a parent as well as the node to parse
823  * on.  Free using _sa_free_dummy_share(share);
824  */
825 
826 static sa_group_t
827 _sa_create_dummy_share()
828 {
829 	xmlNodePtr parent_node = NULL;
830 	xmlNodePtr child_node = NULL;
831 
832 	parent_node = xmlNewNode(NULL, (xmlChar *)"group");
833 	if (parent_node != NULL) {
834 		child_node = xmlNewChild(parent_node, NULL, (xmlChar *)"share",
835 		    NULL);
836 		if (child_node != NULL) {
837 			/*
838 			 * Use a "zfs" tag since that will make sure nothing
839 			 * really attempts to put values into the
840 			 * repository. Also ZFS is currently the only user of
841 			 * this interface.
842 			 */
843 			set_node_attr(parent_node, "type", "transient");
844 			set_node_attr(parent_node, "zfs", "true");
845 			set_node_attr(child_node, "type", "transient");
846 			set_node_attr(child_node, "zfs", "true");
847 		} else {
848 			xmlFreeNode(parent_node);
849 		}
850 	}
851 	return (child_node);
852 }
853 
854 /*
855  * _sa_free_dummy_share(share)
856  *
857  * Free the dummy share and its parent.  It is an error to try and
858  * free something that isn't a dummy.
859  */
860 
861 static int
862 _sa_free_dummy_share(sa_share_t share)
863 {
864 	xmlNodePtr node = (xmlNodePtr)share;
865 	xmlNodePtr parent;
866 	int ret = SA_OK;
867 	char *name;
868 
869 	if (node != NULL) {
870 		parent = node->parent;
871 		name = (char *)xmlGetProp(node, (xmlChar *)"path");
872 		if (name != NULL) {
873 			/* Real shares always have a path but a dummy doesn't */
874 			ret = SA_NOT_ALLOWED;
875 			sa_free_attr_string(name);
876 		} else {
877 			/*
878 			 * If there is a parent, do the free on that since
879 			 * xmlFreeNode is a recursive function and free's an
880 			 * child nodes.
881 			 */
882 			if (parent != NULL) {
883 				node = parent;
884 			}
885 			xmlUnlinkNode(node);
886 			xmlFreeNode(node);
887 		}
888 	}
889 	return (ret);
890 }
891 
892 
893 /*
894  * sa_parse_legacy_options(group, options, proto)
895  *
896  * In order to support legacy configurations, we allow the protocol
897  * specific plugin to parse legacy syntax options (like those in
898  * /etc/dfs/dfstab). This adds a new optionset to the group (or
899  * share).
900  *
901  * Once the optionset has been created, we then get the derived
902  * optionset of the parent (options from the optionset of the parent
903  * and any parent it might have) and remove those from the created
904  * optionset. This avoids duplication of options.
905  */
906 
907 int
908 sa_parse_legacy_options(sa_group_t group, char *options, char *proto)
909 {
910 	int ret = SA_INVALID_PROTOCOL;
911 	sa_group_t parent;
912 	int using_dummy = B_FALSE;
913 	char *pvalue;
914 	sa_optionset_t optionset;
915 	sa_property_t popt, prop;
916 	sa_optionset_t localoptions;
917 
918 	/*
919 	 * If "group" is NULL, this is just a parse without saving
920 	 * anything in either SMF or ZFS.  Create a dummy group to
921 	 * handle this case.
922 	 */
923 	if (group == NULL) {
924 		group = (sa_group_t)_sa_create_dummy_share();
925 		using_dummy = B_TRUE;
926 	}
927 
928 	parent = sa_get_parent_group(group);
929 
930 	if (proto != NULL)
931 		ret = sa_proto_legacy_opts(proto, group, options);
932 
933 	if (using_dummy) {
934 		/* Since this is a dummy parse, cleanup and quit here */
935 		(void) _sa_free_dummy_share(parent);
936 		return (ret);
937 	}
938 
939 	if (ret != SA_OK)
940 		return (ret);
941 
942 	/*
943 	 * If in a group, remove the inherited options and security
944 	 */
945 
946 	if (parent == NULL)
947 		return (ret);
948 
949 	/* Find parent options to remove from child */
950 	optionset = sa_get_derived_optionset(parent, proto, 1);
951 	localoptions = sa_get_optionset(group, proto);
952 	if (optionset != NULL) {
953 		for (popt = sa_get_property(optionset, NULL);
954 		    popt != NULL;
955 		    popt = sa_get_next_property(popt)) {
956 			char *tag;
957 			char *value;
958 			tag = sa_get_property_attr(popt, "type");
959 			if (tag == NULL)
960 				continue;
961 			prop = sa_get_property(localoptions, tag);
962 			if (prop != NULL) {
963 				value = sa_get_property_attr(popt,
964 				    "value");
965 				pvalue = sa_get_property_attr(prop,
966 				    "value");
967 				if (value != NULL && pvalue != NULL &&
968 				    strcmp(value, pvalue) == 0) {
969 					/*
970 					 * Remove the property
971 					 * from the
972 					 * child. While we
973 					 * removed it, we
974 					 * don't need to reset
975 					 * as we do below
976 					 * since we always
977 					 * search from the
978 					 * beginning.
979 					 */
980 					(void) _sa_remove_property(
981 					    prop);
982 				}
983 				if (value != NULL)
984 					sa_free_attr_string(value);
985 				if (pvalue != NULL)
986 					sa_free_attr_string(pvalue);
987 			}
988 			sa_free_attr_string(tag);
989 		}
990 		prop = sa_get_property(localoptions, NULL);
991 		if (prop == NULL && sa_is_share(group)) {
992 			/*
993 			 * All properties removed so remove the
994 			 * optionset if it is on a share
995 			 */
996 			(void) _sa_remove_optionset(localoptions);
997 		}
998 		sa_free_derived_optionset(optionset);
999 	}
1000 	/*
1001 	 * Need to remove security here. If there are no
1002 	 * security options on the local group/share, don't
1003 	 * bother since those are the only ones that would be
1004 	 * affected.
1005 	 */
1006 	localoptions = sa_get_all_security_types(group, proto, 0);
1007 	if (localoptions != NULL) {
1008 		for (prop = sa_get_property(localoptions, NULL);
1009 		    prop != NULL;
1010 		    prop = sa_get_next_property(prop)) {
1011 			char *tag;
1012 			sa_security_t security;
1013 			tag = sa_get_property_attr(prop, "type");
1014 			if (tag != NULL) {
1015 				sa_property_t nextpopt = NULL;
1016 				security = sa_get_security(group, tag, proto);
1017 				sa_free_attr_string(tag);
1018 				/*
1019 				 * prop's value only changes outside this loop
1020 				 */
1021 				pvalue = sa_get_property_attr(prop, "value");
1022 				for (popt = sa_get_property(security, NULL);
1023 				    popt != NULL;
1024 				    popt = nextpopt) {
1025 					char *value;
1026 					/*
1027 					 * Need to get the next prop
1028 					 * now since we could break
1029 					 * the list during removal.
1030 					 */
1031 					nextpopt = sa_get_next_property(popt);
1032 					/* remove Duplicates from this level */
1033 					value = sa_get_property_attr(popt,
1034 					    "value");
1035 					if (value != NULL && pvalue != NULL &&
1036 					    strcmp(value, pvalue) == 0) {
1037 						/*
1038 						 * remove the property
1039 						 * from the child
1040 						 */
1041 						(void) _sa_remove_property
1042 						    (popt);
1043 					}
1044 					if (value != NULL)
1045 						sa_free_attr_string(value);
1046 				}
1047 				if (pvalue != NULL)
1048 					sa_free_attr_string(pvalue);
1049 			}
1050 		}
1051 		(void) sa_destroy_optionset(localoptions);
1052 	}
1053 	return (ret);
1054 }
1055 
1056 /*
1057  * dfs_free_list(list)
1058  *
1059  * Free the data in each list entry of the list as well as freeing the
1060  * entries themselves. We need to avoid memory leaks and don't want to
1061  * dereference any NULL members.
1062  */
1063 
1064 static void
1065 dfs_free_list(xfs_sharelist_t *list)
1066 {
1067 	xfs_sharelist_t *entry;
1068 	for (entry = list; entry != NULL; entry = list) {
1069 		if (entry->path != NULL)
1070 			free(entry->path);
1071 		if (entry->resource != NULL)
1072 			free(entry->resource);
1073 		if (entry->fstype != NULL)
1074 			free(entry->fstype);
1075 		if (entry->options != NULL)
1076 			free(entry->options);
1077 		if (entry->description != NULL)
1078 			free(entry->description);
1079 		if (entry->origline != NULL)
1080 			free(entry->origline);
1081 		if (entry->group != NULL)
1082 			free(entry->group);
1083 		list = list->next;
1084 			free(entry);
1085 	}
1086 }
1087 
1088 /*
1089  * parse_dfstab(dfstab, root)
1090  *
1091  * Open and read the existing dfstab, parsing each line and adding it
1092  * to the internal configuration. Make sure syntax errors, etc are
1093  * preserved as comments.
1094  */
1095 
1096 static void
1097 parse_dfstab(sa_handle_t handle, char *dfstab, xmlNodePtr root)
1098 {
1099 	sa_share_t share;
1100 	sa_group_t group;
1101 	sa_group_t sgroup = NULL;
1102 	sa_group_t defgroup;
1103 	xfs_sharelist_t *head, *list;
1104 	int err;
1105 	int defined_group;
1106 	FILE *dfs;
1107 	char *oldprops;
1108 
1109 	/* read the dfstab format file and fill in the doc tree */
1110 
1111 	dfs = fopen(dfstab, "r");
1112 	if (dfs == NULL)
1113 		return;
1114 
1115 	defgroup = sa_get_group(handle, "default");
1116 
1117 	for (head = list = getdfstab(dfs);
1118 	    list != NULL;
1119 	    list = list->next) {
1120 		share = NULL;
1121 		group = NULL;
1122 		defined_group = 0;
1123 		err = 0;
1124 
1125 		if (list->origline == NULL) {
1126 			/*
1127 			 * Comment line that we will likely skip.
1128 			 * If the line has the syntax:
1129 			 *	# error: string: string
1130 			 * It should be preserved until manually deleted.
1131 			 */
1132 			if (list->description != NULL &&
1133 			    strncmp(list->description, "# Error: ", 9) == 0) {
1134 				char *line;
1135 				char *error;
1136 				char *cmd;
1137 				line = strdup(list->description);
1138 				if (line != NULL) {
1139 					error = line + 9;
1140 					cmd = strchr(error, ':');
1141 					if (cmd != NULL) {
1142 						int len;
1143 						*cmd = '\0';
1144 						cmd += 2;
1145 						len = strlen(cmd);
1146 						cmd[len - 1] = '\0';
1147 						add_syntax_comment(root, cmd,
1148 						    error, 0);
1149 					}
1150 					free(line);
1151 				}
1152 			}
1153 			continue;
1154 		}
1155 		if (list->path != NULL && strlen(list->path) > 0 &&
1156 		    *list->path == '/') {
1157 			share = sa_find_share(handle, list->path);
1158 			if (share != NULL)
1159 				sgroup = sa_get_parent_group(share);
1160 			else
1161 				sgroup = NULL;
1162 		} else {
1163 			(void) printf(dgettext(TEXT_DOMAIN,
1164 			    "No share specified in dfstab: "
1165 			    "line %d: %s\n"),
1166 			    list->lineno, list->origline);
1167 			add_syntax_comment(root, list->origline,
1168 			    dgettext(TEXT_DOMAIN, "No share specified"), 1);
1169 			continue;
1170 		}
1171 		if (list->group != NULL && strlen(list->group) > 0) {
1172 			group = sa_get_group(handle, list->group);
1173 			defined_group = 1;
1174 		} else {
1175 			group = defgroup;
1176 		}
1177 		if (defined_group && group == NULL) {
1178 			(void) printf(dgettext(TEXT_DOMAIN,
1179 			    "Unknown group used in dfstab: line %d: %s\n"),
1180 			    list->lineno, list->origline);
1181 			add_syntax_comment(root, list->origline,
1182 			    dgettext(TEXT_DOMAIN, "Unknown group specified"),
1183 			    1);
1184 			continue;
1185 		}
1186 		if (group == NULL) {
1187 			/* Shouldn't happen unless an SMF error */
1188 			err = SA_CONFIG_ERR;
1189 			continue;
1190 		}
1191 		if (share == NULL) {
1192 			if (defined_group || group != defgroup)
1193 				continue;
1194 			/* This is an OK add for legacy */
1195 			share = sa_add_share(defgroup, list->path,
1196 			    SA_SHARE_PERMANENT | SA_SHARE_PARSER, &err);
1197 			if (share != NULL) {
1198 				if (list->description != NULL &&
1199 				    strlen(list->description) > 0)
1200 					(void) sa_set_share_description(share,
1201 					    list->description);
1202 				if (list->options != NULL &&
1203 				    strlen(list->options) > 0) {
1204 					(void) sa_parse_legacy_options(share,
1205 					    list->options, list->fstype);
1206 				}
1207 				if (list->resource != NULL)
1208 					(void) sa_set_share_attr(share,
1209 					    "resource", list->resource);
1210 			} else {
1211 				(void) printf(dgettext(TEXT_DOMAIN,
1212 				    "Error in dfstab: line %d: %s\n"),
1213 				    list->lineno, list->origline);
1214 				if (err != SA_BAD_PATH)
1215 					add_syntax_comment(root, list->origline,
1216 					    dgettext(TEXT_DOMAIN, "Syntax"), 1);
1217 				else
1218 					add_syntax_comment(root, list->origline,
1219 					    dgettext(TEXT_DOMAIN,
1220 					    "Path"), 1);
1221 				continue;
1222 			}
1223 		} else {
1224 			if (group != sgroup) {
1225 				(void) printf(dgettext(TEXT_DOMAIN,
1226 				    "Attempt to change configuration in "
1227 				    "dfstab: line %d: %s\n"),
1228 				    list->lineno, list->origline);
1229 				add_syntax_comment(root, list->origline,
1230 				    dgettext(TEXT_DOMAIN,
1231 				    "Attempt to change configuration"), 1);
1232 				continue;
1233 			}
1234 			/*
1235 			 * It is the same group but could have changed
1236 			 * options. Make sure we include the group's
1237 			 * properties so we don't end up moving them to
1238 			 * the share inadvertantly. The last arg being
1239 			 * true says to get the inherited properties as well
1240 			 * as the local properties.
1241 			 */
1242 			oldprops = sa_proto_legacy_format(list->fstype, share,
1243 			    B_TRUE);
1244 
1245 			if (oldprops == NULL)
1246 				continue;
1247 
1248 			if (list->options != NULL &&
1249 			    strcmp(oldprops, list->options) != 0) {
1250 				sa_optionset_t opts;
1251 				sa_security_t secs;
1252 
1253 				/* possibly different values */
1254 				opts = sa_get_optionset((sa_group_t)
1255 				    share, list->fstype);
1256 				(void) sa_destroy_optionset(opts);
1257 
1258 				for (secs = sa_get_security(
1259 				    (sa_group_t)share, NULL, list->fstype);
1260 				    secs != NULL;
1261 				    secs = sa_get_security((sa_group_t)share,
1262 				    NULL, list->fstype)) {
1263 					(void) sa_destroy_security(
1264 					    secs);
1265 				}
1266 				(void) sa_parse_legacy_options(share,
1267 				    list->options, list->fstype);
1268 			}
1269 			sa_format_free(oldprops);
1270 		}
1271 	}
1272 	dfs_free_list(head);
1273 }
1274 
1275 /*
1276  * legacy_removes(group, file)
1277  *
1278  * Find any shares that are "missing" from the legacy file. These
1279  * should be removed from the configuration since they are likely from
1280  * a legacy app or the admin modified the dfstab file directly. We
1281  * have to support this even if it is not the recommended way to do
1282  * things.
1283  */
1284 
1285 static void
1286 legacy_removes(sa_group_t group, char *file)
1287 {
1288 	sa_share_t share;
1289 	char *path;
1290 	xfs_sharelist_t *list, *item;
1291 	FILE *dfstab;
1292 
1293 	dfstab = fopen(file, "r");
1294 	if (dfstab != NULL) {
1295 		list = getdfstab(dfstab);
1296 		(void) fclose(dfstab);
1297 retry:
1298 		for (share = sa_get_share(group, NULL);
1299 		    share != NULL;
1300 		    share = sa_get_next_share(share)) {
1301 			/* now see if the share is in the dfstab file */
1302 			path = sa_get_share_attr(share, "path");
1303 			if (path != NULL) {
1304 				item = finddfsentry(list, path);
1305 				sa_free_attr_string(path);
1306 				if (item == NULL) {
1307 					/* The share was removed this way */
1308 					(void) sa_remove_share(share);
1309 
1310 					/*
1311 					 * Start over since the list was broken
1312 					 */
1313 					goto retry;
1314 				}
1315 			}
1316 		}
1317 		if (list != NULL)
1318 			dfs_free_list(list);
1319 	}
1320 }
1321 
1322 /*
1323  * getlegacyconfig(path, root)
1324  *
1325  * Parse dfstab and build the legacy configuration. This only gets
1326  * called when a change was detected.
1327  */
1328 
1329 void
1330 getlegacyconfig(sa_handle_t handle, char *path, xmlNodePtr *root)
1331 {
1332 	sa_group_t defgroup;
1333 
1334 	if (root != NULL) {
1335 		if (*root == NULL)
1336 			*root = xmlNewNode(NULL, (xmlChar *)"sharecfg");
1337 		if (*root != NULL) {
1338 			if (strcmp(path, SA_LEGACY_DFSTAB) == 0) {
1339 				/*
1340 				 * Walk the default shares and find anything
1341 				 * missing.  we do this first to make sure it
1342 				 * is cleaned up since there may be legacy
1343 				 * code add/del via dfstab and we need to
1344 				 * cleanup SMF.
1345 				 */
1346 				defgroup = sa_get_group(handle, "default");
1347 				if (defgroup != NULL)
1348 					legacy_removes(defgroup, path);
1349 				/* Parse the dfstab and add anything new */
1350 				parse_dfstab(handle, path, *root);
1351 			}
1352 		}
1353 	}
1354 }
1355 
1356 /*
1357  * get_share_list(&err)
1358  *
1359  * Get a linked list of all the shares on the system from
1360  * /etc/dfs/sharetab. This is partially copied from libfsmgt which we
1361  * can't use due to package dependencies.
1362  */
1363 static xfs_sharelist_t *
1364 get_share_list(int *errp)
1365 {
1366 	xfs_sharelist_t	*newp;
1367 	xfs_sharelist_t	*headp;
1368 	xfs_sharelist_t	*tailp;
1369 	FILE		*fp;
1370 
1371 	headp = NULL;
1372 	tailp = NULL;
1373 
1374 	if ((fp = fopen(SHARETAB, "r")) != NULL) {
1375 		struct share	*sharetab_entry;
1376 
1377 		(void) lockf(fileno(fp), F_LOCK, 0);
1378 		(void) mutex_lock(&sharetab_lock);
1379 
1380 		while (getshare(fp, &sharetab_entry) > 0) {
1381 			newp = alloc_sharelist();
1382 			if (newp == NULL) {
1383 				(void) mutex_unlock(&sharetab_lock);
1384 				(void) lockf(fileno(fp), F_ULOCK, 0);
1385 				goto err;
1386 			}
1387 
1388 			/*
1389 			 * Link into the list here so we don't leak
1390 			 * memory on a failure from strdup().
1391 			 */
1392 			if (headp == NULL) {
1393 				headp = newp;
1394 				tailp = newp;
1395 			} else {
1396 				tailp->next = newp;
1397 				tailp = newp;
1398 			}
1399 
1400 			newp->path = strdup(sharetab_entry->sh_path);
1401 			newp->resource = strdup(sharetab_entry->sh_res);
1402 			newp->fstype = strdup(sharetab_entry->sh_fstype);
1403 			newp->options = strdup(sharetab_entry->sh_opts);
1404 			newp->description = strdup(sharetab_entry->sh_descr);
1405 
1406 			if (newp->path == NULL || newp->resource == NULL ||
1407 			    newp->fstype == NULL || newp->options == NULL ||
1408 			    newp->description == NULL) {
1409 				(void) mutex_unlock(&sharetab_lock);
1410 				(void) lockf(fileno(fp), F_ULOCK, 0);
1411 				goto err;
1412 			}
1413 		}
1414 
1415 		(void) mutex_unlock(&sharetab_lock);
1416 		(void) lockf(fileno(fp), F_ULOCK, 0);
1417 		(void) fclose(fp);
1418 	} else {
1419 		*errp = errno;
1420 	}
1421 
1422 	/*
1423 	 * Caller must free the mount list
1424 	 */
1425 	return (headp);
1426 err:
1427 	/*
1428 	 * Out of memory so cleanup and leave.
1429 	 */
1430 	dfs_free_list(headp);
1431 	(void) fclose(fp);
1432 	return (NULL);
1433 }
1434 
1435 /*
1436  * parse_sharetab_impl(handle, tmplist, lgroup)
1437  *
1438  * Read the /etc/dfs/sharetab file and see which entries don't exist
1439  * in the repository. These shares are marked transient.  We also need
1440  * to see if they are ZFS shares since ZFS bypasses the SMF
1441  * repository.
1442  *
1443  * The return value is used to contribute to values indicating if it is a legacy
1444  * share. Right now this is 0 or 1, but as multiple systems become legacy that
1445  * could change.
1446  */
1447 
1448 static int
1449 parse_sharetab_impl(sa_handle_t handle, xfs_sharelist_t *tmplist,
1450     sa_group_t lgroup)
1451 {
1452 	int err = 0;
1453 	sa_share_t share;
1454 	sa_group_t group;
1455 	char *groupname;
1456 	int legacy = 0;
1457 	char shareopts[MAXNAMLEN];
1458 
1459 	group = NULL;
1460 	share = sa_find_share(handle, tmplist->path);
1461 	if (share != NULL) {
1462 		/*
1463 		 * If this is a legacy share, mark as shared so we only update
1464 		 * sharetab appropriately. We also keep the sharetab options in
1465 		 * order to display for legacy share with no arguments.
1466 		 */
1467 		set_node_attr(share, "shared", "true");
1468 		(void) snprintf(shareopts, MAXNAMLEN, "shareopts-%s",
1469 		    tmplist->fstype);
1470 		set_node_attr(share, shareopts, tmplist->options);
1471 		return (1);
1472 	}
1473 
1474 	/*
1475 	 * This share is transient so needs to be added. Initially, this will be
1476 	 * under default(legacy) unless it is a ZFS share. If zfs, we need a zfs
1477 	 * group.
1478 	 */
1479 	if (tmplist->resource != NULL &&
1480 	    (groupname = strchr(tmplist->resource, '@')) != NULL) {
1481 		/* There is a defined group */
1482 		*groupname++ = '\0';
1483 		group = sa_get_group(handle, groupname);
1484 		if (group != NULL) {
1485 			share = _sa_add_share(group, tmplist->path,
1486 			    SA_SHARE_TRANSIENT, &err,
1487 			    (uint64_t)SA_FEATURE_NONE);
1488 		} else {
1489 			/*
1490 			 * While this case shouldn't occur very often, it does
1491 			 * occur out of a "zfs set sharenfs=off" when the
1492 			 * dataset is also set to canmount=off. A warning will
1493 			 * then cause the zfs command to abort. Since we add it
1494 			 * to the default list, everything works properly anyway
1495 			 * and the library doesn't need to give a warning.
1496 			 */
1497 			share = _sa_add_share(lgroup,
1498 			    tmplist->path, SA_SHARE_TRANSIENT,
1499 			    &err, (uint64_t)SA_FEATURE_NONE);
1500 		}
1501 	} else {
1502 		if (sa_zfs_is_shared(handle, tmplist->path)) {
1503 			group = sa_get_group(handle, "zfs");
1504 			if (group == NULL) {
1505 				group = sa_create_group(handle,
1506 				    "zfs", &err);
1507 				if (group == NULL &&
1508 				    err == SA_NO_PERMISSION) {
1509 					group = _sa_create_group(
1510 					    (sa_handle_impl_t)
1511 					    handle,
1512 					    "zfs");
1513 				}
1514 				if (group != NULL) {
1515 					(void) sa_create_optionset(
1516 					    group, tmplist->fstype);
1517 					(void) sa_set_group_attr(group,
1518 					    "zfs", "true");
1519 				}
1520 			}
1521 			if (group != NULL) {
1522 				share = _sa_add_share(group,
1523 				    tmplist->path, SA_SHARE_TRANSIENT,
1524 				    &err, (uint64_t)SA_FEATURE_NONE);
1525 			}
1526 		} else {
1527 			share = _sa_add_share(lgroup, tmplist->path,
1528 			    SA_SHARE_TRANSIENT, &err,
1529 			    (uint64_t)SA_FEATURE_NONE);
1530 		}
1531 	}
1532 	if (share == NULL)
1533 		(void) printf(dgettext(TEXT_DOMAIN,
1534 		    "Problem with transient: %s\n"), sa_errorstr(err));
1535 	if (share != NULL)
1536 		set_node_attr(share, "shared", "true");
1537 	if (err == SA_OK) {
1538 		if (tmplist->options != NULL &&
1539 		    strlen(tmplist->options) > 0) {
1540 			(void) sa_parse_legacy_options(share,
1541 			    tmplist->options, tmplist->fstype);
1542 		}
1543 		if (tmplist->resource != NULL &&
1544 		    strcmp(tmplist->resource, "-") != 0)
1545 			set_node_attr(share, "resource",
1546 			    tmplist->resource);
1547 		if (tmplist->description != NULL) {
1548 			xmlNodePtr node;
1549 			node = xmlNewChild((xmlNodePtr)share, NULL,
1550 			    (xmlChar *)"description", NULL);
1551 			xmlNodeSetContent(node,
1552 			    (xmlChar *)tmplist->description);
1553 		}
1554 		legacy = 1;
1555 	}
1556 	return (legacy);
1557 }
1558 
1559 /*
1560  * Given an array of paths, parses the sharetab in /etc/dfs/sharetab looking for
1561  * matches to each path that could be transients from that file.
1562  *
1563  * parse_sharetab_impl has more information on what exactly it is looking for.
1564  */
1565 int
1566 parse_sharetab_for_paths(sa_handle_t handle, char **paths, size_t paths_len)
1567 {
1568 	int err = 0;
1569 	int legacy = 0;
1570 	sa_group_t lgroup;
1571 	xfs_sharelist_t *list = get_share_list(&err);
1572 
1573 	if (list == NULL)
1574 		return (legacy);
1575 
1576 	lgroup = sa_get_group(handle, "default");
1577 	for (int i = 0; i < paths_len; ++i) {
1578 		xfs_sharelist_t *tmplist;
1579 
1580 		for (tmplist = list; tmplist != NULL; tmplist = tmplist->next) {
1581 			if (strcmp(tmplist->path, paths[i]) == 0) {
1582 				break;
1583 			}
1584 		}
1585 		if (tmplist == NULL) {
1586 			continue;
1587 		}
1588 
1589 		legacy = parse_sharetab_impl(handle, tmplist, lgroup);
1590 
1591 	}
1592 	dfs_free_list(list);
1593 	return (legacy);
1594 }
1595 
1596 /*
1597  * Runs parse_sharetab_impl on all the different share lists on the system.
1598  */
1599 int
1600 parse_sharetab(sa_handle_t handle)
1601 {
1602 	int err = 0;
1603 	int legacy = 0;
1604 	sa_group_t lgroup;
1605 	xfs_sharelist_t *list = get_share_list(&err);
1606 
1607 	if (list == NULL)
1608 		return (legacy);
1609 
1610 	lgroup = sa_get_group(handle, "default");
1611 
1612 	for (xfs_sharelist_t *tmplist = list; tmplist != NULL;
1613 	    tmplist = tmplist->next) {
1614 		legacy |= parse_sharetab_impl(handle, tmplist, lgroup);
1615 	}
1616 	dfs_free_list(list);
1617 	return (legacy);
1618 }
1619 
1620 /*
1621  * This is the same as gettransients except when parsing sharetab the entries
1622  * are only processed if the path matches an element of paths.
1623  */
1624 int
1625 get_one_transient(sa_handle_impl_t ihandle, xmlNodePtr *root, char **paths,
1626     size_t paths_len)
1627 {
1628 	int legacy = 0;
1629 	char **protocols = NULL;
1630 
1631 	if (root != NULL) {
1632 		if (*root == NULL)
1633 			*root = xmlNewNode(NULL, (xmlChar *)"sharecfg");
1634 		if (*root != NULL) {
1635 			legacy = parse_sharetab_for_paths(ihandle, paths,
1636 			    paths_len);
1637 			int numproto = sa_get_protocols(&protocols);
1638 			for (int i = 0; i < numproto; i++)
1639 				legacy |= sa_proto_get_transients(
1640 				    (sa_handle_t)ihandle, protocols[i]);
1641 			if (protocols != NULL)
1642 				free(protocols);
1643 		}
1644 	}
1645 	return (legacy);
1646 }
1647 
1648 /*
1649  * Get the transient shares from the sharetab (or other) file. Since
1650  * these are transient, they only appear in the working file and not
1651  * in a repository.
1652  */
1653 int
1654 gettransients(sa_handle_impl_t ihandle, xmlNodePtr *root)
1655 {
1656 	int legacy = 0;
1657 	int numproto;
1658 	char **protocols = NULL;
1659 	int i;
1660 
1661 	if (root != NULL) {
1662 		if (*root == NULL)
1663 			*root = xmlNewNode(NULL, (xmlChar *)"sharecfg");
1664 		if (*root != NULL) {
1665 			legacy = parse_sharetab(ihandle);
1666 			numproto = sa_get_protocols(&protocols);
1667 			for (i = 0; i < numproto; i++)
1668 				legacy |= sa_proto_get_transients(
1669 				    (sa_handle_t)ihandle, protocols[i]);
1670 			if (protocols != NULL)
1671 				free(protocols);
1672 		}
1673 	}
1674 	return (legacy);
1675 }
1676 
1677 /*
1678  * sa_has_prop(optionset, prop)
1679  *
1680  * Is the specified property a member of the optionset?
1681  */
1682 
1683 int
1684 sa_has_prop(sa_optionset_t optionset, sa_property_t prop)
1685 {
1686 	char *name;
1687 	sa_property_t otherprop;
1688 	int result = 0;
1689 
1690 	if (optionset != NULL) {
1691 		name = sa_get_property_attr(prop, "type");
1692 		if (name != NULL) {
1693 			otherprop = sa_get_property(optionset, name);
1694 			if (otherprop != NULL)
1695 				result = 1;
1696 			sa_free_attr_string(name);
1697 		}
1698 	}
1699 	return (result);
1700 }
1701 
1702 /*
1703  * Update legacy files
1704  *
1705  * Provides functions to add/remove/modify individual entries
1706  * in dfstab and sharetab
1707  */
1708 
1709 void
1710 update_legacy_config(sa_handle_t handle)
1711 {
1712 	/*
1713 	 * no longer used -- this is a placeholder in case we need to
1714 	 * add it back later.
1715 	 */
1716 #ifdef lint
1717 	handle = handle;
1718 #endif
1719 }
1720 
1721 /*
1722  * sa_valid_property(handle, object, proto, property)
1723  *
1724  * check to see if the specified property is valid relative to the
1725  * specified protocol. The protocol plugin is called to do the work.
1726  */
1727 
1728 int
1729 sa_valid_property(sa_handle_t handle, void *object, char *proto,
1730     sa_property_t property)
1731 {
1732 	int ret = SA_OK;
1733 
1734 	if (proto != NULL && property != NULL) {
1735 		ret = sa_proto_valid_prop(handle, proto, property, object);
1736 	}
1737 
1738 	return (ret);
1739 }
1740 
1741 /*
1742  * sa_fstype(path)
1743  *
1744  * Given path, return the string representing the path's file system
1745  * type. This is used to discover ZFS shares.
1746  */
1747 
1748 char *
1749 sa_fstype(char *path)
1750 {
1751 	int err;
1752 	struct stat st;
1753 
1754 	err = stat(path, &st);
1755 	if (err < 0)
1756 		err = SA_NO_SUCH_PATH;
1757 	else
1758 		err = SA_OK;
1759 
1760 	/*
1761 	 * If we have a valid path at this point ret, return the fstype.
1762 	 */
1763 	if (err == SA_OK)
1764 		return (strdup(st.st_fstype));
1765 
1766 	return (NULL);
1767 }
1768 
1769 void
1770 sa_free_fstype(char *type)
1771 {
1772 	free(type);
1773 }
1774 
1775 /*
1776  * sa_get_derived_optionset(object, proto, hier)
1777  *
1778  *	Work backward to the top of the share object tree and start
1779  *	copying protocol specific optionsets into a newly created
1780  *	optionset that doesn't have a parent (it will be freed
1781  *	later). This provides for the property inheritance model. That
1782  *	is, properties closer to the share take precedence over group
1783  *	level. This also provides for groups of groups in the future.
1784  */
1785 
1786 sa_optionset_t
1787 sa_get_derived_optionset(void *object, char *proto, int hier)
1788 {
1789 	sa_optionset_t newoptionset;
1790 	sa_optionset_t optionset;
1791 	sa_group_t group;
1792 
1793 	if (hier &&
1794 	    (group = sa_get_parent_group((sa_share_t)object)) != NULL) {
1795 		newoptionset = sa_get_derived_optionset((void *)group, proto,
1796 		    hier);
1797 	} else {
1798 		newoptionset = (sa_optionset_t)xmlNewNode(NULL,
1799 		    (xmlChar *)"optionset");
1800 		if (newoptionset != NULL) {
1801 			sa_set_optionset_attr(newoptionset, "type", proto);
1802 		}
1803 	}
1804 	/* Dont' do anything if memory wasn't allocated */
1805 	if (newoptionset == NULL)
1806 		return (NULL);
1807 
1808 	/* Found the top so working back down the stack */
1809 	optionset = sa_get_optionset((sa_optionset_t)object, proto);
1810 	if (optionset != NULL) {
1811 		sa_property_t prop;
1812 		/* add optionset to the newoptionset */
1813 		for (prop = sa_get_property(optionset, NULL);
1814 		    prop != NULL;
1815 		    prop = sa_get_next_property(prop)) {
1816 			sa_property_t newprop;
1817 			char *name;
1818 			char *value;
1819 			name = sa_get_property_attr(prop, "type");
1820 			value = sa_get_property_attr(prop, "value");
1821 			if (name == NULL)
1822 				continue;
1823 			newprop = sa_get_property(newoptionset, name);
1824 			/* Replace the value with the new value */
1825 			if (newprop != NULL) {
1826 				/*
1827 				 * Only set if value is non NULL, old value ok
1828 				 * if it is NULL.
1829 				 */
1830 				if (value != NULL)
1831 					set_node_attr(newprop, "value", value);
1832 			} else {
1833 				/* an entirely new property */
1834 				if (value != NULL) {
1835 					newprop = sa_create_property(name,
1836 					    value);
1837 					if (newprop != NULL) {
1838 						newprop = (sa_property_t)
1839 						    xmlAddChild(
1840 						    (xmlNodePtr)newoptionset,
1841 						    (xmlNodePtr)newprop);
1842 					}
1843 				}
1844 			}
1845 			sa_free_attr_string(name);
1846 
1847 			if (value != NULL)
1848 				sa_free_attr_string(value);
1849 		}
1850 	}
1851 	return (newoptionset);
1852 }
1853 
1854 void
1855 sa_free_derived_optionset(sa_optionset_t optionset)
1856 {
1857 	/* While it shouldn't be linked, it doesn't hurt */
1858 	if (optionset != NULL) {
1859 		xmlUnlinkNode((xmlNodePtr) optionset);
1860 		xmlFreeNode((xmlNodePtr) optionset);
1861 	}
1862 }
1863 
1864 /*
1865  *  sa_get_all_security_types(object, proto, hier)
1866  *
1867  *	Find all the security types set for this object.  This is
1868  *	preliminary to getting a derived security set. The return value is an
1869  *	optionset containg properties which are the sectype values found by
1870  *	walking up the XML document structure. The returned optionset
1871  *	is a derived optionset.
1872  *
1873  *	If hier is 0, only look at object. If non-zero, walk up the tree.
1874  */
1875 sa_optionset_t
1876 sa_get_all_security_types(void *object, char *proto, int hier)
1877 {
1878 	sa_optionset_t options;
1879 	sa_security_t security;
1880 	sa_group_t group;
1881 	sa_property_t prop;
1882 
1883 	options = NULL;
1884 
1885 	if (hier &&
1886 	    (group = sa_get_parent_group((sa_share_t)object)) != NULL)
1887 		options = sa_get_all_security_types((void *)group, proto, hier);
1888 	else
1889 		options = (sa_optionset_t)xmlNewNode(NULL,
1890 		    (xmlChar *)"optionset");
1891 
1892 	if (options == NULL)
1893 		return (options);
1894 
1895 	/* Hit the top so collect the security types working back. */
1896 	for (security = sa_get_security((sa_group_t)object, NULL, NULL);
1897 	    security != NULL;
1898 	    security = sa_get_next_security(security)) {
1899 		char *type;
1900 		char *sectype;
1901 
1902 		type = sa_get_security_attr(security, "type");
1903 		if (type != NULL) {
1904 			if (strcmp(type, proto) != 0) {
1905 				sa_free_attr_string(type);
1906 				continue;
1907 			}
1908 			sectype = sa_get_security_attr(security, "sectype");
1909 			if (sectype != NULL) {
1910 				/*
1911 				 * Have a security type, check to see if
1912 				 * already present in optionset and add if it
1913 				 * isn't.
1914 				 */
1915 				if (sa_get_property(options, sectype) == NULL) {
1916 					prop = sa_create_property(sectype,
1917 					    "true");
1918 					if (prop != NULL)
1919 						prop = (sa_property_t)
1920 						    xmlAddChild(
1921 						    (xmlNodePtr)options,
1922 						    (xmlNodePtr)prop);
1923 				}
1924 				sa_free_attr_string(sectype);
1925 			}
1926 			sa_free_attr_string(type);
1927 		}
1928 	}
1929 
1930 	return (options);
1931 }
1932 
1933 /*
1934  * sa_get_derived_security(object, sectype, proto, hier)
1935  *
1936  * Get the derived security(named optionset) for the object given the
1937  * sectype and proto. If hier is non-zero, walk up the tree to get all
1938  * properties defined for this object, otherwise just those on the
1939  * object.
1940  */
1941 
1942 sa_security_t
1943 sa_get_derived_security(void *object, char *sectype, char *proto, int hier)
1944 {
1945 	sa_security_t newsecurity;
1946 	sa_security_t security;
1947 	sa_group_t group;
1948 	sa_property_t prop;
1949 
1950 	if (hier &&
1951 	    (group = sa_get_parent_group((sa_share_t)object)) != NULL) {
1952 		newsecurity = sa_get_derived_security((void *)group,
1953 		    sectype, proto, hier);
1954 	} else {
1955 		newsecurity = (sa_security_t)xmlNewNode(NULL,
1956 		    (xmlChar *)"security");
1957 		if (newsecurity != NULL) {
1958 			sa_set_security_attr(newsecurity, "type", proto);
1959 			sa_set_security_attr(newsecurity, "sectype", sectype);
1960 		}
1961 	}
1962 	/* Don't do anything if memory wasn't allocated */
1963 	if (newsecurity == NULL)
1964 		return (newsecurity);
1965 
1966 	/* Found the top so working back down the stack. */
1967 	security = sa_get_security((sa_security_t)object, sectype, proto);
1968 	if (security == NULL)
1969 		return (newsecurity);
1970 
1971 	/* add security to the newsecurity */
1972 	for (prop = sa_get_property(security, NULL);
1973 	    prop != NULL; prop = sa_get_next_property(prop)) {
1974 		sa_property_t newprop;
1975 		char *name;
1976 		char *value;
1977 		name = sa_get_property_attr(prop, "type");
1978 		value = sa_get_property_attr(prop, "value");
1979 		if (name != NULL) {
1980 			newprop = sa_get_property(newsecurity, name);
1981 			/* Replace the value with the new value */
1982 			if (newprop != NULL) {
1983 				/*
1984 				 * Only set if value is non NULL, old
1985 				 * value ok if it is NULL. The value
1986 				 * must be associated with the "value"
1987 				 * tag within XML.
1988 				 */
1989 				if (value != NULL)
1990 					set_node_attr(newprop, "value", value);
1991 			} else {
1992 				/* An entirely new property */
1993 				if (value != NULL) {
1994 					newprop = sa_create_property(name,
1995 					    value);
1996 					newprop = (sa_property_t)
1997 					    xmlAddChild((xmlNodePtr)newsecurity,
1998 					    (xmlNodePtr)newprop);
1999 				}
2000 			}
2001 			sa_free_attr_string(name);
2002 		}
2003 		if (value != NULL)
2004 			sa_free_attr_string(value);
2005 	}
2006 	return (newsecurity);
2007 }
2008 
2009 void
2010 sa_free_derived_security(sa_security_t security)
2011 {
2012 	/* while it shouldn't be linked, it doesn't hurt */
2013 	if (security != NULL) {
2014 		xmlUnlinkNode((xmlNodePtr)security);
2015 		xmlFreeNode((xmlNodePtr)security);
2016 	}
2017 }
2018 
2019 /*
2020  * sharetab utility functions
2021  *
2022  * Makes use of the original sharetab.c from fs.d/nfs/lib
2023  */
2024 
2025 /*
2026  * sa_fillshare(share, proto, sh)
2027  *
2028  * Fill the struct share with values obtained from the share object.
2029  */
2030 void
2031 sa_fillshare(sa_share_t share, char *proto, struct share *sh)
2032 {
2033 	char *groupname = NULL;
2034 	char *value;
2035 	sa_group_t group;
2036 	char *buff;
2037 	char *zfs;
2038 	sa_resource_t resource;
2039 	char *rsrcname = NULL;
2040 	char *defprop;
2041 
2042 	/*
2043 	 * We only want to deal with the path level shares for the
2044 	 * sharetab file. If a resource, get the parent.
2045 	 */
2046 	if (sa_is_resource(share)) {
2047 		resource = (sa_resource_t)share;
2048 		share = sa_get_resource_parent(resource);
2049 		rsrcname = sa_get_resource_attr(resource, "name");
2050 	}
2051 
2052 	group = sa_get_parent_group(share);
2053 	if (group != NULL) {
2054 		zfs = sa_get_group_attr(group, "zfs");
2055 		groupname = sa_get_group_attr(group, "name");
2056 
2057 		if (groupname != NULL &&
2058 		    (strcmp(groupname, "default") == 0 || zfs != NULL)) {
2059 			/*
2060 			 * since the groupname is either "default" or the
2061 			 * group is a ZFS group, we don't want to keep
2062 			 * groupname. We do want it if it is any other type of
2063 			 * group.
2064 			 */
2065 			sa_free_attr_string(groupname);
2066 			groupname = NULL;
2067 		}
2068 		if (zfs != NULL)
2069 			sa_free_attr_string(zfs);
2070 	}
2071 
2072 	value = sa_get_share_attr(share, "path");
2073 	if (value != NULL) {
2074 		sh->sh_path = strdup(value);
2075 		sa_free_attr_string(value);
2076 	}
2077 
2078 	if (rsrcname != NULL || groupname != NULL) {
2079 		int len = 0;
2080 
2081 		if (rsrcname != NULL)
2082 			len += strlen(rsrcname);
2083 		if (groupname != NULL)
2084 			len += strlen(groupname);
2085 		len += 3; /* worst case */
2086 		buff = malloc(len);
2087 		(void) snprintf(buff, len, "%s%s%s",
2088 		    (rsrcname != NULL &&
2089 		    strlen(rsrcname) > 0) ? rsrcname : "-",
2090 		    groupname != NULL ? "@" : "",
2091 		    groupname != NULL ? groupname : "");
2092 		sh->sh_res = buff;
2093 		if (rsrcname != NULL)
2094 			sa_free_attr_string(rsrcname);
2095 		if (groupname != NULL)
2096 			sa_free_attr_string(groupname);
2097 	} else {
2098 		sh->sh_res = strdup("-");
2099 	}
2100 
2101 	/*
2102 	 * Get correct default prop string. NFS uses "rw", others use
2103 	 * "".
2104 	 */
2105 	if (strcmp(proto, "nfs") != 0)
2106 		defprop = "\"\"";
2107 	else
2108 		defprop = "rw";
2109 
2110 	sh->sh_fstype = strdup(proto);
2111 	value = sa_proto_legacy_format(proto, share, 1);
2112 	if (value != NULL) {
2113 		if (strlen(value) > 0)
2114 			sh->sh_opts = strdup(value);
2115 		else
2116 			sh->sh_opts = strdup(defprop);
2117 		free(value);
2118 	} else {
2119 		sh->sh_opts = strdup(defprop);
2120 	}
2121 
2122 	value = sa_get_share_description(share);
2123 	if (value != NULL) {
2124 		sh->sh_descr = strdup(value);
2125 		sa_free_share_description(value);
2126 	} else {
2127 		sh->sh_descr = strdup("");
2128 	}
2129 }
2130 
2131 /*
2132  * sa_emptyshare(sh)
2133  *
2134  * Free the strings in the non-NULL members of sh.
2135  */
2136 
2137 void
2138 sa_emptyshare(struct share *sh)
2139 {
2140 	if (sh->sh_path != NULL)
2141 		free(sh->sh_path);
2142 	sh->sh_path = NULL;
2143 	if (sh->sh_res != NULL)
2144 		free(sh->sh_res);
2145 	sh->sh_res = NULL;
2146 	if (sh->sh_fstype != NULL)
2147 		free(sh->sh_fstype);
2148 	sh->sh_fstype = NULL;
2149 	if (sh->sh_opts != NULL)
2150 		free(sh->sh_opts);
2151 	sh->sh_opts = NULL;
2152 	if (sh->sh_descr != NULL)
2153 		free(sh->sh_descr);
2154 	sh->sh_descr = NULL;
2155 }
2156 
2157 /*
2158  * sa_update_sharetab_ts(handle)
2159  *
2160  * Update the internal timestamp of when sharetab was last
2161  * changed. This needs to be public for ZFS to get at it.
2162  */
2163 
2164 void
2165 sa_update_sharetab_ts(sa_handle_t handle)
2166 {
2167 	struct stat st;
2168 	sa_handle_impl_t implhandle = (sa_handle_impl_t)handle;
2169 
2170 	if (implhandle != NULL && stat(SA_LEGACY_SHARETAB, &st) == 0)
2171 		implhandle->tssharetab = TSTAMP(st.st_mtim);
2172 }
2173 
2174 /*
2175  * sa_update_sharetab(share, proto)
2176  *
2177  * Update the sharetab file with info from the specified share.
2178  * This could be an update or add.
2179  */
2180 
2181 int
2182 sa_update_sharetab(sa_share_t share, char *proto)
2183 {
2184 	int	ret = SA_OK;
2185 	share_t	sh;
2186 	char	*path;
2187 	sa_handle_t handle;
2188 
2189 	path = sa_get_share_attr(share, "path");
2190 	if (path != NULL) {
2191 		(void) memset(&sh, '\0', sizeof (sh));
2192 
2193 		handle = sa_find_group_handle((sa_group_t)share);
2194 		if (handle != NULL) {
2195 			/*
2196 			 * Fill in share structure and send it to the kernel.
2197 			 */
2198 			(void) sa_fillshare(share, proto, &sh);
2199 			(void) _sharefs(SHAREFS_ADD, &sh);
2200 			/*
2201 			 * We need the timestamp of the sharetab file right
2202 			 * after the update was done. This lets us detect a
2203 			 * change that made by a different process.
2204 			 */
2205 			sa_update_sharetab_ts(handle);
2206 			sa_emptyshare(&sh);
2207 		} else {
2208 			ret = SA_CONFIG_ERR;
2209 		}
2210 		sa_free_attr_string(path);
2211 	}
2212 
2213 	return (ret);
2214 }
2215 
2216 /*
2217  * sa_delete_sharetab(handle, path, proto)
2218  *
2219  * remove the specified share from sharetab.
2220  */
2221 
2222 int
2223 sa_delete_sharetab(sa_handle_t handle, char *path, char *proto)
2224 {
2225 	int	ret = SA_OK;
2226 	struct stat st;
2227 
2228 	share_t	sh;
2229 	/*
2230 	 * Both the path and the proto are
2231 	 * keys into the sharetab.
2232 	 */
2233 	if (path != NULL && proto != NULL) {
2234 		(void) memset(&sh, '\0', sizeof (sh));
2235 		sh.sh_path = path;
2236 		sh.sh_fstype = proto;
2237 
2238 		ret = _sharefs(SHAREFS_REMOVE, &sh);
2239 		if (handle != NULL && stat(SA_LEGACY_SHARETAB, &st) == 0)
2240 			sa_update_sharetab_ts(handle);
2241 	}
2242 	return (ret);
2243 }
2244 
2245 /*
2246  * sa_needs_refresh(handle)
2247  *
2248  * Returns B_TRUE if the internal cache needs to be refreshed due to a
2249  * change by another process.  B_FALSE returned otherwise.
2250  */
2251 boolean_t
2252 sa_needs_refresh(sa_handle_t handle)
2253 {
2254 	sa_handle_impl_t implhandle = (sa_handle_impl_t)handle;
2255 	struct stat st;
2256 	char *str;
2257 	uint64_t tstamp;
2258 	scf_simple_prop_t *prop;
2259 
2260 	if (handle == NULL || implhandle->sa_service &
2261 	    (SA_INIT_ONE_SHARE_FROM_NAME | SA_INIT_ONE_SHARE_FROM_HANDLE))
2262 		return (B_TRUE);
2263 
2264 	/*
2265 	 * If sharetab has changed, then there was an external
2266 	 * change. Check sharetab first since it is updated by ZFS as
2267 	 * well as sharemgr.  This is where external ZFS changes are
2268 	 * caught.
2269 	 */
2270 	if (stat(SA_LEGACY_SHARETAB, &st) == 0 &&
2271 	    TSTAMP(st.st_mtim) != implhandle->tssharetab)
2272 		return (B_TRUE);
2273 
2274 	/*
2275 	 * If sharetab wasn't changed, check whether there were any
2276 	 * SMF transactions that modified the config but didn't
2277 	 * initiate a share.  This is less common but does happen.
2278 	 */
2279 	prop = scf_simple_prop_get(implhandle->scfhandle->handle,
2280 	    (const char *)SA_SVC_FMRI_BASE ":default", "state",
2281 	    "lastupdate");
2282 	if (prop != NULL) {
2283 		str = scf_simple_prop_next_astring(prop);
2284 		if (str != NULL)
2285 			tstamp = strtoull(str, NULL, 0);
2286 		else
2287 			tstamp = 0;
2288 		scf_simple_prop_free(prop);
2289 		if (tstamp != implhandle->tstrans)
2290 			return (B_TRUE);
2291 	}
2292 
2293 	return (B_FALSE);
2294 }
2295 
2296 /*
2297  * sa_fix_resource_name(path)
2298  *
2299  * Convert invalid characters in a resource name (SMB share name)
2300  * to underscores ('_').  The list of invalid characters includes
2301  * control characters and the following:
2302  *
2303  *	" / \ [ ] : | < > + ; , ? * =
2304  *
2305  * The caller must pass a valid path.  Leading and trailing slashes
2306  * are stripped from the path before converting invalid characters.
2307  * Resource names are restricted to SA_MAX_RESOURCE_NAME characters.
2308  */
2309 void
2310 sa_fix_resource_name(char *path)
2311 {
2312 	char *invalid = "\"/\\[]:|<>+;,?*=";
2313 	char *p = path;
2314 	char *q;
2315 	size_t len;
2316 
2317 	assert(path != NULL);
2318 
2319 	/*
2320 	 * Strip leading and trailing /'s.
2321 	 */
2322 	p += strspn(p, "/");
2323 	q = strchr(p, '\0');
2324 	if (q != NULL && q != path) {
2325 		while ((--q, *q == '/'))
2326 			*q = '\0';
2327 	}
2328 
2329 	if (*p == '\0') {
2330 		(void) strcpy(path, "_");
2331 		return;
2332 	}
2333 
2334 	/*
2335 	 * Stride over path components until the remaining
2336 	 * path is no longer than SA_MAX_RESOURCE_NAME.
2337 	 */
2338 	q = p;
2339 	while ((q != NULL) && (strlen(q) > SA_MAX_RESOURCE_NAME)) {
2340 		if ((q = strchr(q, '/')) != NULL) {
2341 			++q;
2342 			p = q;
2343 		}
2344 	}
2345 
2346 	/*
2347 	 * If the path is still longer than SA_MAX_RESOURCE_NAME,
2348 	 * take the trailing SA_MAX_RESOURCE_NAME characters.
2349 	 */
2350 	if ((len = strlen(p)) > SA_MAX_RESOURCE_NAME) {
2351 		len = SA_MAX_RESOURCE_NAME;
2352 		p = strchr(p, '\0') - (SA_MAX_RESOURCE_NAME - 1);
2353 	}
2354 
2355 	(void) memmove(path, p, len);
2356 	path[len] = '\0';
2357 
2358 	for (p = path; *p != '\0'; ++p) {
2359 		if ((iscntrl(*p)) || strchr(invalid, *p))
2360 			*p = '_';
2361 	}
2362 }
2363