xref: /illumos-gate/usr/src/lib/libshare/common/libsharecore.c (revision 8dfe5547fbf0979fc1065a8b6fddc1e940a7cf4f)
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 2014 Nexenta Systems, 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(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 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 	list = get_share_list(&err);
1608 	if (list == NULL)
1609 		return (legacy);
1610 
1611 	lgroup = sa_get_group(handle, "default");
1612 
1613 	for (xfs_sharelist_t *tmplist = list; tmplist != NULL;
1614 	    tmplist = tmplist->next) {
1615 		legacy |= parse_sharetab_impl(handle, tmplist, lgroup);
1616 	}
1617 	dfs_free_list(list);
1618 	return (legacy);
1619 }
1620 
1621 /*
1622  * This is the same as gettransients except when parsing sharetab the entries
1623  * are only processed if the path matches an element of paths.
1624  */
1625 int
1626 get_one_transient(sa_handle_impl_t ihandle, xmlNodePtr *root, char **paths,
1627     size_t paths_len)
1628 {
1629 	int legacy = 0;
1630 	char **protocols = NULL;
1631 
1632 	if (root != NULL) {
1633 		if (*root == NULL)
1634 			*root = xmlNewNode(NULL, (xmlChar *)"sharecfg");
1635 		if (*root != NULL) {
1636 			legacy = parse_sharetab_for_paths(ihandle, paths,
1637 			    paths_len);
1638 			int numproto = sa_get_protocols(&protocols);
1639 			for (int i = 0; i < numproto; i++)
1640 				legacy |= sa_proto_get_transients(
1641 				    (sa_handle_t)ihandle, protocols[i]);
1642 			if (protocols != NULL)
1643 				free(protocols);
1644 		}
1645 	}
1646 	return (legacy);
1647 }
1648 
1649 /*
1650  * Get the transient shares from the sharetab (or other) file. Since
1651  * these are transient, they only appear in the working file and not
1652  * in a repository.
1653  */
1654 int
1655 gettransients(sa_handle_impl_t ihandle, xmlNodePtr *root)
1656 {
1657 	int legacy = 0;
1658 	int numproto;
1659 	char **protocols = NULL;
1660 	int i;
1661 
1662 	if (root != NULL) {
1663 		if (*root == NULL)
1664 			*root = xmlNewNode(NULL, (xmlChar *)"sharecfg");
1665 		if (*root != NULL) {
1666 			legacy = parse_sharetab(ihandle);
1667 			numproto = sa_get_protocols(&protocols);
1668 			for (i = 0; i < numproto; i++)
1669 				legacy |= sa_proto_get_transients(
1670 				    (sa_handle_t)ihandle, protocols[i]);
1671 			if (protocols != NULL)
1672 				free(protocols);
1673 		}
1674 	}
1675 	return (legacy);
1676 }
1677 
1678 /*
1679  * sa_has_prop(optionset, prop)
1680  *
1681  * Is the specified property a member of the optionset?
1682  */
1683 
1684 int
1685 sa_has_prop(sa_optionset_t optionset, sa_property_t prop)
1686 {
1687 	char *name;
1688 	sa_property_t otherprop;
1689 	int result = 0;
1690 
1691 	if (optionset != NULL) {
1692 		name = sa_get_property_attr(prop, "type");
1693 		if (name != NULL) {
1694 			otherprop = sa_get_property(optionset, name);
1695 			if (otherprop != NULL)
1696 				result = 1;
1697 			sa_free_attr_string(name);
1698 		}
1699 	}
1700 	return (result);
1701 }
1702 
1703 /*
1704  * Update legacy files
1705  *
1706  * Provides functions to add/remove/modify individual entries
1707  * in dfstab and sharetab
1708  */
1709 
1710 void
1711 update_legacy_config(sa_handle_t handle)
1712 {
1713 	/*
1714 	 * no longer used -- this is a placeholder in case we need to
1715 	 * add it back later.
1716 	 */
1717 #ifdef lint
1718 	handle = handle;
1719 #endif
1720 }
1721 
1722 /*
1723  * sa_valid_property(handle, object, proto, property)
1724  *
1725  * check to see if the specified property is valid relative to the
1726  * specified protocol. The protocol plugin is called to do the work.
1727  */
1728 
1729 int
1730 sa_valid_property(sa_handle_t handle, void *object, char *proto,
1731     sa_property_t property)
1732 {
1733 	int ret = SA_OK;
1734 
1735 	if (proto != NULL && property != NULL) {
1736 		ret = sa_proto_valid_prop(handle, proto, property, object);
1737 	}
1738 
1739 	return (ret);
1740 }
1741 
1742 /*
1743  * sa_fstype(path)
1744  *
1745  * Given path, return the string representing the path's file system
1746  * type. This is used to discover ZFS shares.
1747  */
1748 
1749 char *
1750 sa_fstype(char *path)
1751 {
1752 	int err;
1753 	struct stat st;
1754 
1755 	err = stat(path, &st);
1756 	if (err < 0)
1757 		err = SA_NO_SUCH_PATH;
1758 	else
1759 		err = SA_OK;
1760 
1761 	/*
1762 	 * If we have a valid path at this point ret, return the fstype.
1763 	 */
1764 	if (err == SA_OK)
1765 		return (strdup(st.st_fstype));
1766 
1767 	return (NULL);
1768 }
1769 
1770 void
1771 sa_free_fstype(char *type)
1772 {
1773 	free(type);
1774 }
1775 
1776 /*
1777  * sa_get_derived_optionset(object, proto, hier)
1778  *
1779  *	Work backward to the top of the share object tree and start
1780  *	copying protocol specific optionsets into a newly created
1781  *	optionset that doesn't have a parent (it will be freed
1782  *	later). This provides for the property inheritance model. That
1783  *	is, properties closer to the share take precedence over group
1784  *	level. This also provides for groups of groups in the future.
1785  */
1786 
1787 sa_optionset_t
1788 sa_get_derived_optionset(void *object, char *proto, int hier)
1789 {
1790 	sa_optionset_t newoptionset;
1791 	sa_optionset_t optionset;
1792 	sa_group_t group;
1793 
1794 	if (hier &&
1795 	    (group = sa_get_parent_group((sa_share_t)object)) != NULL) {
1796 		newoptionset = sa_get_derived_optionset((void *)group, proto,
1797 		    hier);
1798 	} else {
1799 		newoptionset = (sa_optionset_t)xmlNewNode(NULL,
1800 		    (xmlChar *)"optionset");
1801 		if (newoptionset != NULL) {
1802 			sa_set_optionset_attr(newoptionset, "type", proto);
1803 		}
1804 	}
1805 	/* Dont' do anything if memory wasn't allocated */
1806 	if (newoptionset == NULL)
1807 		return (NULL);
1808 
1809 	/* Found the top so working back down the stack */
1810 	optionset = sa_get_optionset((sa_optionset_t)object, proto);
1811 	if (optionset != NULL) {
1812 		sa_property_t prop;
1813 		/* add optionset to the newoptionset */
1814 		for (prop = sa_get_property(optionset, NULL);
1815 		    prop != NULL;
1816 		    prop = sa_get_next_property(prop)) {
1817 			sa_property_t newprop;
1818 			char *name;
1819 			char *value;
1820 			name = sa_get_property_attr(prop, "type");
1821 			value = sa_get_property_attr(prop, "value");
1822 			if (name == NULL)
1823 				continue;
1824 			newprop = sa_get_property(newoptionset, name);
1825 			/* Replace the value with the new value */
1826 			if (newprop != NULL) {
1827 				/*
1828 				 * Only set if value is non NULL, old value ok
1829 				 * if it is NULL.
1830 				 */
1831 				if (value != NULL)
1832 					set_node_attr(newprop, "value", value);
1833 			} else {
1834 				/* an entirely new property */
1835 				if (value != NULL) {
1836 					newprop = sa_create_property(name,
1837 					    value);
1838 					if (newprop != NULL) {
1839 						newprop = (sa_property_t)
1840 						    xmlAddChild(
1841 						    (xmlNodePtr)newoptionset,
1842 						    (xmlNodePtr)newprop);
1843 					}
1844 				}
1845 			}
1846 			sa_free_attr_string(name);
1847 
1848 			if (value != NULL)
1849 				sa_free_attr_string(value);
1850 		}
1851 	}
1852 	return (newoptionset);
1853 }
1854 
1855 void
1856 sa_free_derived_optionset(sa_optionset_t optionset)
1857 {
1858 	/* While it shouldn't be linked, it doesn't hurt */
1859 	if (optionset != NULL) {
1860 		xmlUnlinkNode((xmlNodePtr) optionset);
1861 		xmlFreeNode((xmlNodePtr) optionset);
1862 	}
1863 }
1864 
1865 /*
1866  *  sa_get_all_security_types(object, proto, hier)
1867  *
1868  *	Find all the security types set for this object.  This is
1869  *	preliminary to getting a derived security set. The return value is an
1870  *	optionset containg properties which are the sectype values found by
1871  *	walking up the XML document structure. The returned optionset
1872  *	is a derived optionset.
1873  *
1874  *	If hier is 0, only look at object. If non-zero, walk up the tree.
1875  */
1876 sa_optionset_t
1877 sa_get_all_security_types(void *object, char *proto, int hier)
1878 {
1879 	sa_optionset_t options;
1880 	sa_security_t security;
1881 	sa_group_t group;
1882 	sa_property_t prop;
1883 
1884 	options = NULL;
1885 
1886 	if (hier &&
1887 	    (group = sa_get_parent_group((sa_share_t)object)) != NULL)
1888 		options = sa_get_all_security_types((void *)group, proto, hier);
1889 	else
1890 		options = (sa_optionset_t)xmlNewNode(NULL,
1891 		    (xmlChar *)"optionset");
1892 
1893 	if (options == NULL)
1894 		return (options);
1895 
1896 	/* Hit the top so collect the security types working back. */
1897 	for (security = sa_get_security((sa_group_t)object, NULL, NULL);
1898 	    security != NULL;
1899 	    security = sa_get_next_security(security)) {
1900 		char *type;
1901 		char *sectype;
1902 
1903 		type = sa_get_security_attr(security, "type");
1904 		if (type != NULL) {
1905 			if (strcmp(type, proto) != 0) {
1906 				sa_free_attr_string(type);
1907 				continue;
1908 			}
1909 			sectype = sa_get_security_attr(security, "sectype");
1910 			if (sectype != NULL) {
1911 				/*
1912 				 * Have a security type, check to see if
1913 				 * already present in optionset and add if it
1914 				 * isn't.
1915 				 */
1916 				if (sa_get_property(options, sectype) == NULL) {
1917 					prop = sa_create_property(sectype,
1918 					    "true");
1919 					if (prop != NULL)
1920 						prop = (sa_property_t)
1921 						    xmlAddChild(
1922 						    (xmlNodePtr)options,
1923 						    (xmlNodePtr)prop);
1924 				}
1925 				sa_free_attr_string(sectype);
1926 			}
1927 			sa_free_attr_string(type);
1928 		}
1929 	}
1930 
1931 	return (options);
1932 }
1933 
1934 /*
1935  * sa_get_derived_security(object, sectype, proto, hier)
1936  *
1937  * Get the derived security(named optionset) for the object given the
1938  * sectype and proto. If hier is non-zero, walk up the tree to get all
1939  * properties defined for this object, otherwise just those on the
1940  * object.
1941  */
1942 
1943 sa_security_t
1944 sa_get_derived_security(void *object, char *sectype, char *proto, int hier)
1945 {
1946 	sa_security_t newsecurity;
1947 	sa_security_t security;
1948 	sa_group_t group;
1949 	sa_property_t prop;
1950 
1951 	if (hier &&
1952 	    (group = sa_get_parent_group((sa_share_t)object)) != NULL) {
1953 		newsecurity = sa_get_derived_security((void *)group,
1954 		    sectype, proto, hier);
1955 	} else {
1956 		newsecurity = (sa_security_t)xmlNewNode(NULL,
1957 		    (xmlChar *)"security");
1958 		if (newsecurity != NULL) {
1959 			sa_set_security_attr(newsecurity, "type", proto);
1960 			sa_set_security_attr(newsecurity, "sectype", sectype);
1961 		}
1962 	}
1963 	/* Don't do anything if memory wasn't allocated */
1964 	if (newsecurity == NULL)
1965 		return (newsecurity);
1966 
1967 	/* Found the top so working back down the stack. */
1968 	security = sa_get_security((sa_security_t)object, sectype, proto);
1969 	if (security == NULL)
1970 		return (newsecurity);
1971 
1972 	/* add security to the newsecurity */
1973 	for (prop = sa_get_property(security, NULL);
1974 	    prop != NULL; prop = sa_get_next_property(prop)) {
1975 		sa_property_t newprop;
1976 		char *name;
1977 		char *value;
1978 		name = sa_get_property_attr(prop, "type");
1979 		value = sa_get_property_attr(prop, "value");
1980 		if (name != NULL) {
1981 			newprop = sa_get_property(newsecurity, name);
1982 			/* Replace the value with the new value */
1983 			if (newprop != NULL) {
1984 				/*
1985 				 * Only set if value is non NULL, old
1986 				 * value ok if it is NULL. The value
1987 				 * must be associated with the "value"
1988 				 * tag within XML.
1989 				 */
1990 				if (value != NULL)
1991 					set_node_attr(newprop, "value", value);
1992 			} else {
1993 				/* An entirely new property */
1994 				if (value != NULL) {
1995 					newprop = sa_create_property(name,
1996 					    value);
1997 					newprop = (sa_property_t)
1998 					    xmlAddChild((xmlNodePtr)newsecurity,
1999 					    (xmlNodePtr)newprop);
2000 				}
2001 			}
2002 			sa_free_attr_string(name);
2003 		}
2004 		if (value != NULL)
2005 			sa_free_attr_string(value);
2006 	}
2007 	return (newsecurity);
2008 }
2009 
2010 void
2011 sa_free_derived_security(sa_security_t security)
2012 {
2013 	/* while it shouldn't be linked, it doesn't hurt */
2014 	if (security != NULL) {
2015 		xmlUnlinkNode((xmlNodePtr)security);
2016 		xmlFreeNode((xmlNodePtr)security);
2017 	}
2018 }
2019 
2020 /*
2021  * sharetab utility functions
2022  *
2023  * Makes use of the original sharetab.c from fs.d/nfs/lib
2024  */
2025 
2026 /*
2027  * sa_fillshare(share, proto, sh)
2028  *
2029  * Fill the struct share with values obtained from the share object.
2030  */
2031 void
2032 sa_fillshare(sa_share_t share, char *proto, struct share *sh)
2033 {
2034 	char *groupname = NULL;
2035 	char *value;
2036 	sa_group_t group;
2037 	char *buff;
2038 	char *zfs;
2039 	sa_resource_t resource;
2040 	char *rsrcname = NULL;
2041 	char *defprop;
2042 
2043 	/*
2044 	 * We only want to deal with the path level shares for the
2045 	 * sharetab file. If a resource, get the parent.
2046 	 */
2047 	if (sa_is_resource(share)) {
2048 		resource = (sa_resource_t)share;
2049 		share = sa_get_resource_parent(resource);
2050 		rsrcname = sa_get_resource_attr(resource, "name");
2051 	}
2052 
2053 	group = sa_get_parent_group(share);
2054 	if (group != NULL) {
2055 		zfs = sa_get_group_attr(group, "zfs");
2056 		groupname = sa_get_group_attr(group, "name");
2057 
2058 		if (groupname != NULL &&
2059 		    (strcmp(groupname, "default") == 0 || zfs != NULL)) {
2060 			/*
2061 			 * since the groupname is either "default" or the
2062 			 * group is a ZFS group, we don't want to keep
2063 			 * groupname. We do want it if it is any other type of
2064 			 * group.
2065 			 */
2066 			sa_free_attr_string(groupname);
2067 			groupname = NULL;
2068 		}
2069 		if (zfs != NULL)
2070 			sa_free_attr_string(zfs);
2071 	}
2072 
2073 	value = sa_get_share_attr(share, "path");
2074 	if (value != NULL) {
2075 		sh->sh_path = strdup(value);
2076 		sa_free_attr_string(value);
2077 	}
2078 
2079 	if (rsrcname != NULL || groupname != NULL) {
2080 		int len = 0;
2081 
2082 		if (rsrcname != NULL)
2083 			len += strlen(rsrcname);
2084 		if (groupname != NULL)
2085 			len += strlen(groupname);
2086 		len += 3; /* worst case */
2087 		buff = malloc(len);
2088 		(void) snprintf(buff, len, "%s%s%s",
2089 		    (rsrcname != NULL &&
2090 		    strlen(rsrcname) > 0) ? rsrcname : "-",
2091 		    groupname != NULL ? "@" : "",
2092 		    groupname != NULL ? groupname : "");
2093 		sh->sh_res = buff;
2094 		if (rsrcname != NULL)
2095 			sa_free_attr_string(rsrcname);
2096 		if (groupname != NULL)
2097 			sa_free_attr_string(groupname);
2098 	} else {
2099 		sh->sh_res = strdup("-");
2100 	}
2101 
2102 	/*
2103 	 * Get correct default prop string. NFS uses "rw", others use
2104 	 * "".
2105 	 */
2106 	if (strcmp(proto, "nfs") != 0)
2107 		defprop = "\"\"";
2108 	else
2109 		defprop = "rw";
2110 
2111 	sh->sh_fstype = strdup(proto);
2112 	value = sa_proto_legacy_format(proto, share, 1);
2113 	if (value != NULL) {
2114 		if (strlen(value) > 0)
2115 			sh->sh_opts = strdup(value);
2116 		else
2117 			sh->sh_opts = strdup(defprop);
2118 		free(value);
2119 	} else {
2120 		sh->sh_opts = strdup(defprop);
2121 	}
2122 
2123 	value = sa_get_share_description(share);
2124 	if (value != NULL) {
2125 		sh->sh_descr = strdup(value);
2126 		sa_free_share_description(value);
2127 	} else {
2128 		sh->sh_descr = strdup("");
2129 	}
2130 }
2131 
2132 /*
2133  * sa_emptyshare(sh)
2134  *
2135  * Free the strings in the non-NULL members of sh.
2136  */
2137 
2138 void
2139 sa_emptyshare(struct share *sh)
2140 {
2141 	if (sh->sh_path != NULL)
2142 		free(sh->sh_path);
2143 	sh->sh_path = NULL;
2144 	if (sh->sh_res != NULL)
2145 		free(sh->sh_res);
2146 	sh->sh_res = NULL;
2147 	if (sh->sh_fstype != NULL)
2148 		free(sh->sh_fstype);
2149 	sh->sh_fstype = NULL;
2150 	if (sh->sh_opts != NULL)
2151 		free(sh->sh_opts);
2152 	sh->sh_opts = NULL;
2153 	if (sh->sh_descr != NULL)
2154 		free(sh->sh_descr);
2155 	sh->sh_descr = NULL;
2156 }
2157 
2158 /*
2159  * sa_update_sharetab_ts(handle)
2160  *
2161  * Update the internal timestamp of when sharetab was last
2162  * changed. This needs to be public for ZFS to get at it.
2163  */
2164 
2165 void
2166 sa_update_sharetab_ts(sa_handle_t handle)
2167 {
2168 	struct stat st;
2169 	sa_handle_impl_t implhandle = (sa_handle_impl_t)handle;
2170 
2171 	if (implhandle != NULL && stat(SA_LEGACY_SHARETAB, &st) == 0)
2172 		implhandle->tssharetab = TSTAMP(st.st_mtim);
2173 }
2174 
2175 /*
2176  * sa_update_sharetab(share, proto)
2177  *
2178  * Update the sharetab file with info from the specified share.
2179  * This could be an update or add.
2180  */
2181 
2182 int
2183 sa_update_sharetab(sa_share_t share, char *proto)
2184 {
2185 	int	ret = SA_OK;
2186 	share_t	sh;
2187 	char	*path;
2188 	sa_handle_t handle;
2189 
2190 	path = sa_get_share_attr(share, "path");
2191 	if (path != NULL) {
2192 		(void) memset(&sh, '\0', sizeof (sh));
2193 
2194 		handle = sa_find_group_handle((sa_group_t)share);
2195 		if (handle != NULL) {
2196 			/*
2197 			 * Fill in share structure and send it to the kernel.
2198 			 */
2199 			(void) sa_fillshare(share, proto, &sh);
2200 			(void) _sharefs(SHAREFS_ADD, &sh);
2201 			/*
2202 			 * We need the timestamp of the sharetab file right
2203 			 * after the update was done. This lets us detect a
2204 			 * change that made by a different process.
2205 			 */
2206 			sa_update_sharetab_ts(handle);
2207 			sa_emptyshare(&sh);
2208 		} else {
2209 			ret = SA_CONFIG_ERR;
2210 		}
2211 		sa_free_attr_string(path);
2212 	}
2213 
2214 	return (ret);
2215 }
2216 
2217 /*
2218  * sa_delete_sharetab(handle, path, proto)
2219  *
2220  * remove the specified share from sharetab.
2221  */
2222 
2223 int
2224 sa_delete_sharetab(sa_handle_t handle, char *path, char *proto)
2225 {
2226 	int	ret = SA_OK;
2227 	struct stat st;
2228 
2229 	share_t	sh;
2230 	/*
2231 	 * Both the path and the proto are
2232 	 * keys into the sharetab.
2233 	 */
2234 	if (path != NULL && proto != NULL) {
2235 		(void) memset(&sh, '\0', sizeof (sh));
2236 		sh.sh_path = path;
2237 		sh.sh_fstype = proto;
2238 
2239 		ret = _sharefs(SHAREFS_REMOVE, &sh);
2240 		if (handle != NULL && stat(SA_LEGACY_SHARETAB, &st) == 0)
2241 			sa_update_sharetab_ts(handle);
2242 	}
2243 	return (ret);
2244 }
2245 
2246 /*
2247  * sa_needs_refresh(handle)
2248  *
2249  * Returns B_TRUE if the internal cache needs to be refreshed due to a
2250  * change by another process.  B_FALSE returned otherwise.
2251  */
2252 boolean_t
2253 sa_needs_refresh(sa_handle_t handle)
2254 {
2255 	sa_handle_impl_t implhandle = (sa_handle_impl_t)handle;
2256 	struct stat st;
2257 	char *str;
2258 	uint64_t tstamp;
2259 	scf_simple_prop_t *prop;
2260 
2261 	if (handle == NULL || implhandle->sa_service &
2262 	    (SA_INIT_ONE_SHARE_FROM_NAME | SA_INIT_ONE_SHARE_FROM_HANDLE))
2263 		return (B_TRUE);
2264 
2265 	/*
2266 	 * If sharetab has changed, then there was an external
2267 	 * change. Check sharetab first since it is updated by ZFS as
2268 	 * well as sharemgr.  This is where external ZFS changes are
2269 	 * caught.
2270 	 */
2271 	if (stat(SA_LEGACY_SHARETAB, &st) == 0 &&
2272 	    TSTAMP(st.st_mtim) != implhandle->tssharetab)
2273 		return (B_TRUE);
2274 
2275 	/*
2276 	 * If sharetab wasn't changed, check whether there were any
2277 	 * SMF transactions that modified the config but didn't
2278 	 * initiate a share.  This is less common but does happen.
2279 	 */
2280 	prop = scf_simple_prop_get(implhandle->scfhandle->handle,
2281 	    (const char *)SA_SVC_FMRI_BASE ":default", "state",
2282 	    "lastupdate");
2283 	if (prop != NULL) {
2284 		str = scf_simple_prop_next_astring(prop);
2285 		if (str != NULL)
2286 			tstamp = strtoull(str, NULL, 0);
2287 		else
2288 			tstamp = 0;
2289 		scf_simple_prop_free(prop);
2290 		if (tstamp != implhandle->tstrans)
2291 			return (B_TRUE);
2292 	}
2293 
2294 	return (B_FALSE);
2295 }
2296 
2297 /*
2298  * sa_fix_resource_name(path)
2299  *
2300  * Convert invalid characters in a resource name (SMB share name)
2301  * to underscores ('_').  The list of invalid characters includes
2302  * control characters and the following:
2303  *
2304  *	" / \ [ ] : | < > + ; , ? * =
2305  *
2306  * The caller must pass a valid path.  Leading and trailing slashes
2307  * are stripped from the path before converting invalid characters.
2308  * Resource names are restricted to SA_MAX_RESOURCE_NAME characters.
2309  */
2310 void
2311 sa_fix_resource_name(char *path)
2312 {
2313 	char *invalid = "\"/\\[]:|<>+;,?*=";
2314 	char *p = path;
2315 	char *q;
2316 	size_t len;
2317 
2318 	assert(path != NULL);
2319 
2320 	/*
2321 	 * Strip leading and trailing /'s.
2322 	 */
2323 	p += strspn(p, "/");
2324 	q = strchr(p, '\0');
2325 	if (q != NULL && q != path) {
2326 		while ((--q, *q == '/'))
2327 			*q = '\0';
2328 	}
2329 
2330 	if (*p == '\0') {
2331 		(void) strcpy(path, "_");
2332 		return;
2333 	}
2334 
2335 	/*
2336 	 * Stride over path components until the remaining
2337 	 * path is no longer than SA_MAX_RESOURCE_NAME.
2338 	 */
2339 	q = p;
2340 	while ((q != NULL) && (strlen(q) > SA_MAX_RESOURCE_NAME)) {
2341 		if ((q = strchr(q, '/')) != NULL) {
2342 			++q;
2343 			p = q;
2344 		}
2345 	}
2346 
2347 	/*
2348 	 * If the path is still longer than SA_MAX_RESOURCE_NAME,
2349 	 * take the trailing SA_MAX_RESOURCE_NAME characters.
2350 	 */
2351 	if ((len = strlen(p)) > SA_MAX_RESOURCE_NAME) {
2352 		len = SA_MAX_RESOURCE_NAME;
2353 		p = strchr(p, '\0') - (SA_MAX_RESOURCE_NAME - 1);
2354 	}
2355 
2356 	(void) memmove(path, p, len);
2357 	path[len] = '\0';
2358 
2359 	for (p = path; *p != '\0'; ++p) {
2360 		if ((iscntrl(*p)) || strchr(invalid, *p))
2361 			*p = '_';
2362 	}
2363 }
2364