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