xref: /illumos-gate/usr/src/lib/libshare/common/libsharecore.c (revision 56f33205c9ed776c3c909e07d52e94610a675740)
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 (groupname != NULL && 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 		path = sa_get_share_attr(share, "path");
695 		if (path == NULL) {
696 			ret = SA_NO_MEMORY;
697 			goto out;
698 		}
699 		dfstab = open_dfstab(SA_LEGACY_DFSTAB);
700 		if (dfstab != NULL) {
701 			(void) setvbuf(dfstab, NULL, _IOLBF, BUFSIZ * 8);
702 			sablocksigs(&old);
703 			(void) lockf(fileno(dfstab), F_LOCK, 0);
704 			list = getdfstab(dfstab);
705 			rewind(dfstab);
706 			if (list != NULL)
707 				list = remdfsentry(list, path, proto);
708 			list = adddfsentry(list, share, proto);
709 			outdfstab(dfstab, list);
710 			(void) fflush(dfstab);
711 			(void) lockf(fileno(dfstab), F_ULOCK, 0);
712 			(void) fsync(fileno(dfstab));
713 			saunblocksigs(&old);
714 			(void) fclose(dfstab);
715 			if (list != NULL)
716 				dfs_free_list(list);
717 		} else {
718 			if (errno == EACCES || errno == EPERM)
719 				ret = SA_NO_PERMISSION;
720 			else
721 				ret = SA_CONFIG_ERR;
722 		}
723 		sa_free_attr_string(path);
724 	}
725 out:
726 	if (persist != NULL)
727 		sa_free_attr_string(persist);
728 	return (ret);
729 }
730 
731 /*
732  * sa_is_security(optname, proto)
733  *
734  * Check to see if optname is a security (named optionset) specific
735  * property for the specified protocol.
736  */
737 
738 int
739 sa_is_security(char *optname, char *proto)
740 {
741 	int ret = 0;
742 	if (proto != NULL)
743 		ret = sa_proto_security_prop(proto, optname);
744 	return (ret);
745 }
746 
747 /*
748  * add_syntax_comment(root, line, err, todfstab)
749  *
750  * Add a comment to the document indicating a syntax error. If
751  * todfstab is set, write it back to the dfstab file as well.
752  */
753 
754 static void
755 add_syntax_comment(xmlNodePtr root, char *line, char *err, int todfstab)
756 {
757 	xmlNodePtr node;
758 
759 	node = xmlNewChild(root, NULL, (xmlChar *)"error", (xmlChar *)line);
760 	if (node != NULL)
761 		(void) xmlSetProp(node, (xmlChar *)"type", (xmlChar *)err);
762 	if (todfstab)
763 		sa_comment_line(line, err);
764 }
765 
766 /*
767  * sa_is_share(object)
768  *
769  * returns true of the object is of type "share".
770  */
771 
772 int
773 sa_is_share(void *object)
774 {
775 	if (object != NULL) {
776 		if (strcmp((char *)((xmlNodePtr)object)->name, "share") == 0)
777 		return (1);
778 	}
779 	return (0);
780 }
781 /*
782  * sa_is_resource(object)
783  *
784  * returns true of the object is of type "share".
785  */
786 
787 int
788 sa_is_resource(void *object)
789 {
790 	if (object != NULL) {
791 		if (strcmp((char *)((xmlNodePtr)object)->name, "resource") == 0)
792 			return (1);
793 	}
794 	return (0);
795 }
796 
797 /*
798  * _sa_remove_property(property)
799  *
800  * remove a property only from the document.
801  */
802 
803 static void
804 _sa_remove_property(sa_property_t property)
805 {
806 	xmlUnlinkNode((xmlNodePtr)property);
807 	xmlFreeNode((xmlNodePtr)property);
808 }
809 
810 /*
811  * _sa_create_dummy_share()
812  *
813  * Create a share entry suitable for parsing but not tied to any real
814  * config tree.  Need to have a parent as well as the node to parse
815  * on.  Free using _sa_free_dummy_share(share);
816  */
817 
818 static sa_group_t
819 _sa_create_dummy_share()
820 {
821 	xmlNodePtr parent_node = NULL;
822 	xmlNodePtr child_node = NULL;
823 
824 	parent_node = xmlNewNode(NULL, (xmlChar *)"group");
825 	if (parent_node != NULL) {
826 		child_node = xmlNewChild(parent_node, NULL, (xmlChar *)"share",
827 		    NULL);
828 		if (child_node != NULL) {
829 			/*
830 			 * Use a "zfs" tag since that will make sure nothing
831 			 * really attempts to put values into the
832 			 * repository. Also ZFS is currently the only user of
833 			 * this interface.
834 			 */
835 			set_node_attr(parent_node, "type", "transient");
836 			set_node_attr(parent_node, "zfs", "true");
837 			set_node_attr(child_node, "type", "transient");
838 			set_node_attr(child_node, "zfs", "true");
839 		} else {
840 			xmlFreeNode(parent_node);
841 		}
842 	}
843 	return (child_node);
844 }
845 
846 /*
847  * _sa_free_dummy_share(share)
848  *
849  * Free the dummy share and its parent.  It is an error to try and
850  * free something that isn't a dummy.
851  */
852 
853 static int
854 _sa_free_dummy_share(sa_share_t share)
855 {
856 	xmlNodePtr node = (xmlNodePtr)share;
857 	xmlNodePtr parent;
858 	int ret = SA_OK;
859 	char *name;
860 
861 	if (node != NULL) {
862 		parent = node->parent;
863 		name = (char *)xmlGetProp(node, (xmlChar *)"path");
864 		if (name != NULL) {
865 			/* Real shares always have a path but a dummy doesn't */
866 			ret = SA_NOT_ALLOWED;
867 			sa_free_attr_string(name);
868 		} else {
869 			/*
870 			 * If there is a parent, do the free on that since
871 			 * xmlFreeNode is a recursive function and free's an
872 			 * child nodes.
873 			 */
874 			if (parent != NULL) {
875 				node = parent;
876 			}
877 			xmlUnlinkNode(node);
878 			xmlFreeNode(node);
879 		}
880 	}
881 	return (ret);
882 }
883 
884 
885 /*
886  * sa_parse_legacy_options(group, options, proto)
887  *
888  * In order to support legacy configurations, we allow the protocol
889  * specific plugin to parse legacy syntax options (like those in
890  * /etc/dfs/dfstab). This adds a new optionset to the group (or
891  * share).
892  *
893  * Once the optionset has been created, we then get the derived
894  * optionset of the parent (options from the optionset of the parent
895  * and any parent it might have) and remove those from the created
896  * optionset. This avoids duplication of options.
897  */
898 
899 int
900 sa_parse_legacy_options(sa_group_t group, char *options, char *proto)
901 {
902 	int ret = SA_INVALID_PROTOCOL;
903 	sa_group_t parent;
904 	int using_dummy = B_FALSE;
905 	char *pvalue;
906 	sa_optionset_t optionset;
907 	sa_property_t popt, prop;
908 	sa_optionset_t localoptions;
909 
910 	/*
911 	 * If "group" is NULL, this is just a parse without saving
912 	 * anything in either SMF or ZFS.  Create a dummy group to
913 	 * handle this case.
914 	 */
915 	if (group == NULL) {
916 		group = (sa_group_t)_sa_create_dummy_share();
917 		using_dummy = B_TRUE;
918 	}
919 
920 	parent = sa_get_parent_group(group);
921 
922 	if (proto != NULL)
923 		ret = sa_proto_legacy_opts(proto, group, options);
924 
925 	if (using_dummy) {
926 		/* Since this is a dummy parse, cleanup and quit here */
927 		(void) _sa_free_dummy_share(parent);
928 		return (ret);
929 	}
930 
931 	if (ret != SA_OK)
932 		return (ret);
933 
934 	/*
935 	 * If in a group, remove the inherited options and security
936 	 */
937 
938 	if (parent == NULL)
939 		return (ret);
940 
941 	/* Find parent options to remove from child */
942 	optionset = sa_get_derived_optionset(parent, proto, 1);
943 	localoptions = sa_get_optionset(group, proto);
944 	if (optionset != NULL) {
945 		for (popt = sa_get_property(optionset, NULL);
946 		    popt != NULL;
947 		    popt = sa_get_next_property(popt)) {
948 			char *tag;
949 			char *value;
950 			tag = sa_get_property_attr(popt, "type");
951 			if (tag == NULL)
952 				continue;
953 			prop = sa_get_property(localoptions, tag);
954 			if (prop != NULL) {
955 				value = sa_get_property_attr(popt,
956 				    "value");
957 				pvalue = sa_get_property_attr(prop,
958 				    "value");
959 				if (value != NULL && pvalue != NULL &&
960 				    strcmp(value, pvalue) == 0) {
961 					/*
962 					 * Remove the property
963 					 * from the
964 					 * child. While we
965 					 * removed it, we
966 					 * don't need to reset
967 					 * as we do below
968 					 * since we always
969 					 * search from the
970 					 * beginning.
971 					 */
972 					(void) _sa_remove_property(
973 					    prop);
974 				}
975 				if (value != NULL)
976 					sa_free_attr_string(value);
977 				if (pvalue != NULL)
978 					sa_free_attr_string(pvalue);
979 			}
980 			sa_free_attr_string(tag);
981 		}
982 		prop = sa_get_property(localoptions, NULL);
983 		if (prop == NULL && sa_is_share(group)) {
984 			/*
985 			 * All properties removed so remove the
986 			 * optionset if it is on a share
987 			 */
988 			(void) _sa_remove_optionset(localoptions);
989 		}
990 		sa_free_derived_optionset(optionset);
991 	}
992 	/*
993 	 * Need to remove security here. If there are no
994 	 * security options on the local group/share, don't
995 	 * bother since those are the only ones that would be
996 	 * affected.
997 	 */
998 	localoptions = sa_get_all_security_types(group, proto, 0);
999 	if (localoptions != NULL) {
1000 		for (prop = sa_get_property(localoptions, NULL);
1001 		    prop != NULL;
1002 		    prop = sa_get_next_property(prop)) {
1003 			char *tag;
1004 			sa_security_t security;
1005 			tag = sa_get_property_attr(prop, "type");
1006 			if (tag != NULL) {
1007 				sa_property_t nextpopt = NULL;
1008 				security = sa_get_security(group, tag, proto);
1009 				sa_free_attr_string(tag);
1010 				/*
1011 				 * prop's value only changes outside this loop
1012 				 */
1013 				pvalue = sa_get_property_attr(prop, "value");
1014 				for (popt = sa_get_property(security, NULL);
1015 				    popt != NULL;
1016 				    popt = nextpopt) {
1017 					char *value;
1018 					/*
1019 					 * Need to get the next prop
1020 					 * now since we could break
1021 					 * the list during removal.
1022 					 */
1023 					nextpopt = sa_get_next_property(popt);
1024 					/* remove Duplicates from this level */
1025 					value = sa_get_property_attr(popt,
1026 					    "value");
1027 					if (value != NULL && pvalue != NULL &&
1028 					    strcmp(value, pvalue) == 0) {
1029 						/*
1030 						 * remove the property
1031 						 * from the child
1032 						 */
1033 						(void) _sa_remove_property
1034 						    (popt);
1035 					}
1036 					if (value != NULL)
1037 						sa_free_attr_string(value);
1038 				}
1039 				if (pvalue != NULL)
1040 					sa_free_attr_string(pvalue);
1041 			}
1042 		}
1043 		(void) sa_destroy_optionset(localoptions);
1044 	}
1045 	return (ret);
1046 }
1047 
1048 /*
1049  * dfs_free_list(list)
1050  *
1051  * Free the data in each list entry of the list as well as freeing the
1052  * entries themselves. We need to avoid memory leaks and don't want to
1053  * dereference any NULL members.
1054  */
1055 
1056 static void
1057 dfs_free_list(xfs_sharelist_t *list)
1058 {
1059 	xfs_sharelist_t *entry;
1060 	for (entry = list; entry != NULL; entry = list) {
1061 		if (entry->path != NULL)
1062 			free(entry->path);
1063 		if (entry->resource != NULL)
1064 			free(entry->resource);
1065 		if (entry->fstype != NULL)
1066 			free(entry->fstype);
1067 		if (entry->options != NULL)
1068 			free(entry->options);
1069 		if (entry->description != NULL)
1070 			free(entry->description);
1071 		if (entry->origline != NULL)
1072 			free(entry->origline);
1073 		if (entry->group != NULL)
1074 			free(entry->group);
1075 		list = list->next;
1076 			free(entry);
1077 	}
1078 }
1079 
1080 /*
1081  * parse_dfstab(dfstab, root)
1082  *
1083  * Open and read the existing dfstab, parsing each line and adding it
1084  * to the internal configuration. Make sure syntax errors, etc are
1085  * preserved as comments.
1086  */
1087 
1088 static void
1089 parse_dfstab(sa_handle_t handle, char *dfstab, xmlNodePtr root)
1090 {
1091 	sa_share_t share;
1092 	sa_group_t group;
1093 	sa_group_t sgroup = NULL;
1094 	sa_group_t defgroup;
1095 	xfs_sharelist_t *head, *list;
1096 	int err;
1097 	int defined_group;
1098 	FILE *dfs;
1099 	char *oldprops;
1100 
1101 	/* read the dfstab format file and fill in the doc tree */
1102 
1103 	dfs = fopen(dfstab, "r");
1104 	if (dfs == NULL)
1105 		return;
1106 
1107 	defgroup = sa_get_group(handle, "default");
1108 
1109 	for (head = list = getdfstab(dfs);
1110 	    list != NULL;
1111 	    list = list->next) {
1112 		share = NULL;
1113 		group = NULL;
1114 		defined_group = 0;
1115 		err = 0;
1116 
1117 		if (list->origline == NULL) {
1118 			/*
1119 			 * Comment line that we will likely skip.
1120 			 * If the line has the syntax:
1121 			 *	# error: string: string
1122 			 * It should be preserved until manually deleted.
1123 			 */
1124 			if (list->description != NULL &&
1125 			    strncmp(list->description, "# Error: ", 9) == 0) {
1126 				char *line;
1127 				char *error;
1128 				char *cmd;
1129 				line = strdup(list->description);
1130 				if (line != NULL) {
1131 					error = line + 9;
1132 					cmd = strchr(error, ':');
1133 					if (cmd != NULL) {
1134 						int len;
1135 						*cmd = '\0';
1136 						cmd += 2;
1137 						len = strlen(cmd);
1138 						cmd[len - 1] = '\0';
1139 						add_syntax_comment(root, cmd,
1140 						    error, 0);
1141 					}
1142 					free(line);
1143 				}
1144 			}
1145 			continue;
1146 		}
1147 		if (list->path != NULL && strlen(list->path) > 0 &&
1148 		    *list->path == '/') {
1149 			share = sa_find_share(handle, list->path);
1150 			if (share != NULL)
1151 				sgroup = sa_get_parent_group(share);
1152 			else
1153 				sgroup = NULL;
1154 		} else {
1155 			(void) printf(dgettext(TEXT_DOMAIN,
1156 			    "No share specified in dfstab: "
1157 			    "line %d: %s\n"),
1158 			    list->lineno, list->origline);
1159 			add_syntax_comment(root, list->origline,
1160 			    dgettext(TEXT_DOMAIN, "No share specified"), 1);
1161 			continue;
1162 		}
1163 		if (list->group != NULL && strlen(list->group) > 0) {
1164 			group = sa_get_group(handle, list->group);
1165 			defined_group = 1;
1166 		} else {
1167 			group = defgroup;
1168 		}
1169 		if (defined_group && group == NULL) {
1170 			(void) printf(dgettext(TEXT_DOMAIN,
1171 			    "Unknown group used in dfstab: line %d: %s\n"),
1172 			    list->lineno, list->origline);
1173 			add_syntax_comment(root, list->origline,
1174 			    dgettext(TEXT_DOMAIN, "Unknown group specified"),
1175 			    1);
1176 			continue;
1177 		}
1178 		if (group == NULL) {
1179 			/* Shouldn't happen unless an SMF error */
1180 			err = SA_CONFIG_ERR;
1181 			continue;
1182 		}
1183 		if (share == NULL) {
1184 			if (defined_group || group != defgroup)
1185 				continue;
1186 			/* This is an OK add for legacy */
1187 			share = sa_add_share(defgroup, list->path,
1188 			    SA_SHARE_PERMANENT | SA_SHARE_PARSER, &err);
1189 			if (share != NULL) {
1190 				if (list->description != NULL &&
1191 				    strlen(list->description) > 0)
1192 					(void) sa_set_share_description(share,
1193 					    list->description);
1194 				if (list->options != NULL &&
1195 				    strlen(list->options) > 0) {
1196 					(void) sa_parse_legacy_options(share,
1197 					    list->options, list->fstype);
1198 				}
1199 				if (list->resource != NULL)
1200 					(void) sa_set_share_attr(share,
1201 					    "resource", list->resource);
1202 			} else {
1203 				(void) printf(dgettext(TEXT_DOMAIN,
1204 				    "Error in dfstab: line %d: %s\n"),
1205 				    list->lineno, list->origline);
1206 				if (err != SA_BAD_PATH)
1207 					add_syntax_comment(root, list->origline,
1208 					    dgettext(TEXT_DOMAIN, "Syntax"), 1);
1209 				else
1210 					add_syntax_comment(root, list->origline,
1211 					    dgettext(TEXT_DOMAIN,
1212 					    "Path"), 1);
1213 				continue;
1214 			}
1215 		} else {
1216 			if (group != sgroup) {
1217 				(void) printf(dgettext(TEXT_DOMAIN,
1218 				    "Attempt to change configuration in "
1219 				    "dfstab: line %d: %s\n"),
1220 				    list->lineno, list->origline);
1221 				add_syntax_comment(root, list->origline,
1222 				    dgettext(TEXT_DOMAIN,
1223 				    "Attempt to change configuration"), 1);
1224 				continue;
1225 			}
1226 			/*
1227 			 * It is the same group but could have changed
1228 			 * options. Make sure we include the group's
1229 			 * properties so we don't end up moving them to
1230 			 * the share inadvertantly. The last arg being
1231 			 * true says to get the inherited properties as well
1232 			 * as the local properties.
1233 			 */
1234 			oldprops = sa_proto_legacy_format(list->fstype, share,
1235 			    B_TRUE);
1236 
1237 			if (oldprops == NULL)
1238 				continue;
1239 
1240 			if (list->options != NULL &&
1241 			    strcmp(oldprops, list->options) != 0) {
1242 				sa_optionset_t opts;
1243 				sa_security_t secs;
1244 
1245 				/* possibly different values */
1246 				opts = sa_get_optionset((sa_group_t)
1247 				    share, list->fstype);
1248 				(void) sa_destroy_optionset(opts);
1249 
1250 				for (secs = sa_get_security(
1251 				    (sa_group_t)share, NULL, list->fstype);
1252 				    secs != NULL;
1253 				    secs = sa_get_security((sa_group_t)share,
1254 				    NULL, list->fstype)) {
1255 					(void) sa_destroy_security(
1256 					    secs);
1257 				}
1258 				(void) sa_parse_legacy_options(share,
1259 				    list->options, list->fstype);
1260 			}
1261 			sa_format_free(oldprops);
1262 		}
1263 	}
1264 	dfs_free_list(head);
1265 }
1266 
1267 /*
1268  * legacy_removes(group, file)
1269  *
1270  * Find any shares that are "missing" from the legacy file. These
1271  * should be removed from the configuration since they are likely from
1272  * a legacy app or the admin modified the dfstab file directly. We
1273  * have to support this even if it is not the recommended way to do
1274  * things.
1275  */
1276 
1277 static void
1278 legacy_removes(sa_group_t group, char *file)
1279 {
1280 	sa_share_t share;
1281 	char *path;
1282 	xfs_sharelist_t *list, *item;
1283 	FILE *dfstab;
1284 
1285 	dfstab = fopen(file, "r");
1286 	if (dfstab != NULL) {
1287 		list = getdfstab(dfstab);
1288 		(void) fclose(dfstab);
1289 retry:
1290 		for (share = sa_get_share(group, NULL);
1291 		    share != NULL;
1292 		    share = sa_get_next_share(share)) {
1293 			/* now see if the share is in the dfstab file */
1294 			path = sa_get_share_attr(share, "path");
1295 			if (path != NULL) {
1296 				item = finddfsentry(list, path);
1297 				sa_free_attr_string(path);
1298 				if (item == NULL) {
1299 					/* The share was removed this way */
1300 					(void) sa_remove_share(share);
1301 
1302 					/*
1303 					 * Start over since the list was broken
1304 					 */
1305 					goto retry;
1306 				}
1307 			}
1308 		}
1309 		if (list != NULL)
1310 			dfs_free_list(list);
1311 	}
1312 }
1313 
1314 /*
1315  * getlegacyconfig(path, root)
1316  *
1317  * Parse dfstab and build the legacy configuration. This only gets
1318  * called when a change was detected.
1319  */
1320 
1321 void
1322 getlegacyconfig(sa_handle_t handle, char *path, xmlNodePtr *root)
1323 {
1324 	sa_group_t defgroup;
1325 
1326 	if (root != NULL) {
1327 		if (*root == NULL)
1328 			*root = xmlNewNode(NULL, (xmlChar *)"sharecfg");
1329 		if (*root != NULL) {
1330 			if (strcmp(path, SA_LEGACY_DFSTAB) == 0) {
1331 				/*
1332 				 * Walk the default shares and find anything
1333 				 * missing.  we do this first to make sure it
1334 				 * is cleaned up since there may be legacy
1335 				 * code add/del via dfstab and we need to
1336 				 * cleanup SMF.
1337 				 */
1338 				defgroup = sa_get_group(handle, "default");
1339 				if (defgroup != NULL)
1340 					legacy_removes(defgroup, path);
1341 				/* Parse the dfstab and add anything new */
1342 				parse_dfstab(handle, path, *root);
1343 			}
1344 		}
1345 	}
1346 }
1347 
1348 /*
1349  * get_share_list(&err)
1350  *
1351  * Get a linked list of all the shares on the system from
1352  * /etc/dfs/sharetab. This is partially copied from libfsmgt which we
1353  * can't use due to package dependencies.
1354  */
1355 static xfs_sharelist_t *
1356 get_share_list(int *errp)
1357 {
1358 	xfs_sharelist_t	*newp;
1359 	xfs_sharelist_t	*headp;
1360 	xfs_sharelist_t	*tailp;
1361 	FILE		*fp;
1362 
1363 	headp = NULL;
1364 	tailp = NULL;
1365 
1366 	if ((fp = fopen(SHARETAB, "r")) != NULL) {
1367 		struct share	*sharetab_entry;
1368 
1369 		(void) lockf(fileno(fp), F_LOCK, 0);
1370 		(void) mutex_lock(&sharetab_lock);
1371 
1372 		while (getshare(fp, &sharetab_entry) > 0) {
1373 			newp = alloc_sharelist();
1374 			if (newp == NULL) {
1375 				(void) mutex_unlock(&sharetab_lock);
1376 				(void) lockf(fileno(fp), F_ULOCK, 0);
1377 				goto err;
1378 			}
1379 
1380 			/*
1381 			 * Link into the list here so we don't leak
1382 			 * memory on a failure from strdup().
1383 			 */
1384 			if (headp == NULL) {
1385 				headp = newp;
1386 				tailp = newp;
1387 			} else {
1388 				tailp->next = newp;
1389 				tailp = newp;
1390 			}
1391 
1392 			newp->path = strdup(sharetab_entry->sh_path);
1393 			newp->resource = strdup(sharetab_entry->sh_res);
1394 			newp->fstype = strdup(sharetab_entry->sh_fstype);
1395 			newp->options = strdup(sharetab_entry->sh_opts);
1396 			newp->description = strdup(sharetab_entry->sh_descr);
1397 
1398 			if (newp->path == NULL || newp->resource == NULL ||
1399 			    newp->fstype == NULL || newp->options == NULL ||
1400 			    newp->description == NULL) {
1401 				(void) mutex_unlock(&sharetab_lock);
1402 				(void) lockf(fileno(fp), F_ULOCK, 0);
1403 				goto err;
1404 			}
1405 		}
1406 
1407 		(void) mutex_unlock(&sharetab_lock);
1408 		(void) lockf(fileno(fp), F_ULOCK, 0);
1409 		(void) fclose(fp);
1410 	} else {
1411 		*errp = errno;
1412 	}
1413 
1414 	/*
1415 	 * Caller must free the mount list
1416 	 */
1417 	return (headp);
1418 err:
1419 	/*
1420 	 * Out of memory so cleanup and leave.
1421 	 */
1422 	dfs_free_list(headp);
1423 	(void) fclose(fp);
1424 	return (NULL);
1425 }
1426 
1427 /*
1428  * parse_sharetab(handle)
1429  *
1430  * Read the /etc/dfs/sharetab file and see which entries don't exist
1431  * in the repository. These shares are marked transient.  We also need
1432  * to see if they are ZFS shares since ZFS bypasses the SMF
1433  * repository.
1434  */
1435 
1436 int
1437 parse_sharetab(sa_handle_t handle)
1438 {
1439 	xfs_sharelist_t *list, *tmplist;
1440 	int err = 0;
1441 	sa_share_t share;
1442 	sa_group_t group;
1443 	sa_group_t lgroup;
1444 	char *groupname;
1445 	int legacy = 0;
1446 	char shareopts[MAXNAMLEN];
1447 
1448 	list = get_share_list(&err);
1449 	if (list == NULL)
1450 		return (legacy);
1451 
1452 	lgroup = sa_get_group(handle, "default");
1453 
1454 	for (tmplist = list; tmplist != NULL; tmplist = tmplist->next) {
1455 		group = NULL;
1456 		share = sa_find_share(handle, tmplist->path);
1457 		if (share != NULL) {
1458 			/*
1459 			 * If this is a legacy share, mark as shared so we
1460 			 * only update sharetab appropriately. We also keep
1461 			 * the sharetab options in order to display for legacy
1462 			 * share with no arguments.
1463 			 */
1464 			set_node_attr(share, "shared", "true");
1465 			(void) snprintf(shareopts, MAXNAMLEN, "shareopts-%s",
1466 			    tmplist->fstype);
1467 			set_node_attr(share, shareopts, tmplist->options);
1468 			continue;
1469 		}
1470 
1471 		/*
1472 		 * This share is transient so needs to be
1473 		 * added. Initially, this will be under
1474 		 * default(legacy) unless it is a ZFS
1475 		 * share. If zfs, we need a zfs group.
1476 		 */
1477 		if (tmplist->resource != NULL &&
1478 		    (groupname = strchr(tmplist->resource, '@')) != NULL) {
1479 			/* There is a defined group */
1480 			*groupname++ = '\0';
1481 			group = sa_get_group(handle, groupname);
1482 			if (group != NULL) {
1483 				share = _sa_add_share(group, tmplist->path,
1484 				    SA_SHARE_TRANSIENT, &err,
1485 				    (uint64_t)SA_FEATURE_NONE);
1486 			} else {
1487 				/*
1488 				 * While this case shouldn't
1489 				 * occur very often, it does
1490 				 * occur out of a "zfs set
1491 				 * sharenfs=off" when the
1492 				 * dataset is also set to
1493 				 * canmount=off. A warning
1494 				 * will then cause the zfs
1495 				 * command to abort. Since we
1496 				 * add it to the default list,
1497 				 * everything works properly
1498 				 * anyway and the library
1499 				 * doesn't need to give a
1500 				 * warning.
1501 				 */
1502 				share = _sa_add_share(lgroup,
1503 				    tmplist->path, SA_SHARE_TRANSIENT,
1504 				    &err, (uint64_t)SA_FEATURE_NONE);
1505 			}
1506 		} else {
1507 			if (sa_zfs_is_shared(handle, tmplist->path)) {
1508 				group = sa_get_group(handle, "zfs");
1509 				if (group == NULL) {
1510 					group = sa_create_group(handle,
1511 					    "zfs", &err);
1512 					if (group == NULL &&
1513 					    err == SA_NO_PERMISSION) {
1514 						group = _sa_create_group(
1515 						    (sa_handle_impl_t)
1516 						    handle,
1517 						    "zfs");
1518 					}
1519 					if (group != NULL) {
1520 						(void) sa_create_optionset(
1521 						    group, tmplist->fstype);
1522 						(void) sa_set_group_attr(group,
1523 						    "zfs", "true");
1524 					}
1525 				}
1526 				if (group != NULL) {
1527 					share = _sa_add_share(group,
1528 					    tmplist->path, SA_SHARE_TRANSIENT,
1529 					    &err, (uint64_t)SA_FEATURE_NONE);
1530 				}
1531 			} else {
1532 				share = _sa_add_share(lgroup, tmplist->path,
1533 				    SA_SHARE_TRANSIENT, &err,
1534 				    (uint64_t)SA_FEATURE_NONE);
1535 			}
1536 		}
1537 		if (share == NULL)
1538 			(void) printf(dgettext(TEXT_DOMAIN,
1539 			    "Problem with transient: %s\n"), sa_errorstr(err));
1540 		if (share != NULL)
1541 			set_node_attr(share, "shared", "true");
1542 		if (err == SA_OK) {
1543 			if (tmplist->options != NULL &&
1544 			    strlen(tmplist->options) > 0) {
1545 				(void) sa_parse_legacy_options(share,
1546 				    tmplist->options, tmplist->fstype);
1547 			}
1548 			if (tmplist->resource != NULL &&
1549 			    strcmp(tmplist->resource, "-") != 0)
1550 				set_node_attr(share, "resource",
1551 				    tmplist->resource);
1552 			if (tmplist->description != NULL) {
1553 				xmlNodePtr node;
1554 				node = xmlNewChild((xmlNodePtr)share, NULL,
1555 				    (xmlChar *)"description", NULL);
1556 				xmlNodeSetContent(node,
1557 				    (xmlChar *)tmplist->description);
1558 			}
1559 			legacy = 1;
1560 		}
1561 	}
1562 	dfs_free_list(list);
1563 	return (legacy);
1564 }
1565 
1566 /*
1567  * Get the transient shares from the sharetab (or other) file.  since
1568  * these are transient, they only appear in the working file and not
1569  * in a repository.
1570  */
1571 int
1572 gettransients(sa_handle_impl_t ihandle, xmlNodePtr *root)
1573 {
1574 	int legacy = 0;
1575 	int numproto;
1576 	char **protocols = NULL;
1577 	int i;
1578 
1579 	if (root != NULL) {
1580 		if (*root == NULL)
1581 			*root = xmlNewNode(NULL, (xmlChar *)"sharecfg");
1582 		if (*root != NULL) {
1583 			legacy = parse_sharetab(ihandle);
1584 			numproto = sa_get_protocols(&protocols);
1585 			for (i = 0; i < numproto; i++)
1586 				legacy |= sa_proto_get_transients(
1587 				    (sa_handle_t)ihandle, protocols[i]);
1588 			if (protocols != NULL)
1589 				free(protocols);
1590 		}
1591 	}
1592 	return (legacy);
1593 }
1594 
1595 /*
1596  * sa_has_prop(optionset, prop)
1597  *
1598  * Is the specified property a member of the optionset?
1599  */
1600 
1601 int
1602 sa_has_prop(sa_optionset_t optionset, sa_property_t prop)
1603 {
1604 	char *name;
1605 	sa_property_t otherprop;
1606 	int result = 0;
1607 
1608 	if (optionset != NULL) {
1609 		name = sa_get_property_attr(prop, "type");
1610 		if (name != NULL) {
1611 			otherprop = sa_get_property(optionset, name);
1612 			if (otherprop != NULL)
1613 				result = 1;
1614 			sa_free_attr_string(name);
1615 		}
1616 	}
1617 	return (result);
1618 }
1619 
1620 /*
1621  * Update legacy files
1622  *
1623  * Provides functions to add/remove/modify individual entries
1624  * in dfstab and sharetab
1625  */
1626 
1627 void
1628 update_legacy_config(sa_handle_t handle)
1629 {
1630 	/*
1631 	 * no longer used -- this is a placeholder in case we need to
1632 	 * add it back later.
1633 	 */
1634 #ifdef lint
1635 	handle = handle;
1636 #endif
1637 }
1638 
1639 /*
1640  * sa_valid_property(handle, object, proto, property)
1641  *
1642  * check to see if the specified property is valid relative to the
1643  * specified protocol. The protocol plugin is called to do the work.
1644  */
1645 
1646 int
1647 sa_valid_property(sa_handle_t handle, void *object, char *proto,
1648     sa_property_t property)
1649 {
1650 	int ret = SA_OK;
1651 
1652 	if (proto != NULL && property != NULL) {
1653 		ret = sa_proto_valid_prop(handle, proto, property, object);
1654 	}
1655 
1656 	return (ret);
1657 }
1658 
1659 /*
1660  * sa_fstype(path)
1661  *
1662  * Given path, return the string representing the path's file system
1663  * type. This is used to discover ZFS shares.
1664  */
1665 
1666 char *
1667 sa_fstype(char *path)
1668 {
1669 	int err;
1670 	struct stat st;
1671 
1672 	err = stat(path, &st);
1673 	if (err < 0)
1674 		err = SA_NO_SUCH_PATH;
1675 	else
1676 		err = SA_OK;
1677 
1678 	/*
1679 	 * If we have a valid path at this point ret, return the fstype.
1680 	 */
1681 	if (err == SA_OK)
1682 		return (strdup(st.st_fstype));
1683 
1684 	return (NULL);
1685 }
1686 
1687 void
1688 sa_free_fstype(char *type)
1689 {
1690 	free(type);
1691 }
1692 
1693 /*
1694  * sa_get_derived_optionset(object, proto, hier)
1695  *
1696  *	Work backward to the top of the share object tree and start
1697  *	copying protocol specific optionsets into a newly created
1698  *	optionset that doesn't have a parent (it will be freed
1699  *	later). This provides for the property inheritance model. That
1700  *	is, properties closer to the share take precedence over group
1701  *	level. This also provides for groups of groups in the future.
1702  */
1703 
1704 sa_optionset_t
1705 sa_get_derived_optionset(void *object, char *proto, int hier)
1706 {
1707 	sa_optionset_t newoptionset;
1708 	sa_optionset_t optionset;
1709 	sa_group_t group;
1710 
1711 	if (hier &&
1712 	    (group = sa_get_parent_group((sa_share_t)object)) != NULL) {
1713 		newoptionset = sa_get_derived_optionset((void *)group, proto,
1714 		    hier);
1715 	} else {
1716 		newoptionset = (sa_optionset_t)xmlNewNode(NULL,
1717 		    (xmlChar *)"optionset");
1718 		if (newoptionset != NULL) {
1719 			sa_set_optionset_attr(newoptionset, "type", proto);
1720 		}
1721 	}
1722 	/* Dont' do anything if memory wasn't allocated */
1723 	if (newoptionset == NULL)
1724 		return (NULL);
1725 
1726 	/* Found the top so working back down the stack */
1727 	optionset = sa_get_optionset((sa_optionset_t)object, proto);
1728 	if (optionset != NULL) {
1729 		sa_property_t prop;
1730 		/* add optionset to the newoptionset */
1731 		for (prop = sa_get_property(optionset, NULL);
1732 		    prop != NULL;
1733 		    prop = sa_get_next_property(prop)) {
1734 			sa_property_t newprop;
1735 			char *name;
1736 			char *value;
1737 			name = sa_get_property_attr(prop, "type");
1738 			value = sa_get_property_attr(prop, "value");
1739 			if (name == NULL)
1740 				continue;
1741 			newprop = sa_get_property(newoptionset, name);
1742 			/* Replace the value with the new value */
1743 			if (newprop != NULL) {
1744 				/*
1745 				 * Only set if value is non NULL, old value ok
1746 				 * if it is NULL.
1747 				 */
1748 				if (value != NULL)
1749 					set_node_attr(newprop, "value", value);
1750 			} else {
1751 				/* an entirely new property */
1752 				if (value != NULL) {
1753 					newprop = sa_create_property(name,
1754 					    value);
1755 					if (newprop != NULL) {
1756 						newprop = (sa_property_t)
1757 						    xmlAddChild(
1758 						    (xmlNodePtr)newoptionset,
1759 						    (xmlNodePtr)newprop);
1760 					}
1761 				}
1762 			}
1763 			sa_free_attr_string(name);
1764 
1765 			if (value != NULL)
1766 				sa_free_attr_string(value);
1767 		}
1768 	}
1769 	return (newoptionset);
1770 }
1771 
1772 void
1773 sa_free_derived_optionset(sa_optionset_t optionset)
1774 {
1775 	/* While it shouldn't be linked, it doesn't hurt */
1776 	if (optionset != NULL) {
1777 		xmlUnlinkNode((xmlNodePtr) optionset);
1778 		xmlFreeNode((xmlNodePtr) optionset);
1779 	}
1780 }
1781 
1782 /*
1783  *  sa_get_all_security_types(object, proto, hier)
1784  *
1785  *	Find all the security types set for this object.  This is
1786  *	preliminary to getting a derived security set. The return value is an
1787  *	optionset containg properties which are the sectype values found by
1788  *	walking up the XML document structure. The returned optionset
1789  *	is a derived optionset.
1790  *
1791  *	If hier is 0, only look at object. If non-zero, walk up the tree.
1792  */
1793 sa_optionset_t
1794 sa_get_all_security_types(void *object, char *proto, int hier)
1795 {
1796 	sa_optionset_t options;
1797 	sa_security_t security;
1798 	sa_group_t group;
1799 	sa_property_t prop;
1800 
1801 	options = NULL;
1802 
1803 	if (hier &&
1804 	    (group = sa_get_parent_group((sa_share_t)object)) != NULL)
1805 		options = sa_get_all_security_types((void *)group, proto, hier);
1806 	else
1807 		options = (sa_optionset_t)xmlNewNode(NULL,
1808 		    (xmlChar *)"optionset");
1809 
1810 	if (options == NULL)
1811 		return (options);
1812 
1813 	/* Hit the top so collect the security types working back. */
1814 	for (security = sa_get_security((sa_group_t)object, NULL, NULL);
1815 	    security != NULL;
1816 	    security = sa_get_next_security(security)) {
1817 		char *type;
1818 		char *sectype;
1819 
1820 		type = sa_get_security_attr(security, "type");
1821 		if (type != NULL) {
1822 			if (strcmp(type, proto) != 0) {
1823 				sa_free_attr_string(type);
1824 				continue;
1825 			}
1826 			sectype = sa_get_security_attr(security, "sectype");
1827 			if (sectype != NULL) {
1828 				/*
1829 				 * Have a security type, check to see if
1830 				 * already present in optionset and add if it
1831 				 * isn't.
1832 				 */
1833 				if (sa_get_property(options, sectype) == NULL) {
1834 					prop = sa_create_property(sectype,
1835 					    "true");
1836 					if (prop != NULL)
1837 						prop = (sa_property_t)
1838 						    xmlAddChild(
1839 						    (xmlNodePtr)options,
1840 						    (xmlNodePtr)prop);
1841 				}
1842 				sa_free_attr_string(sectype);
1843 			}
1844 			sa_free_attr_string(type);
1845 		}
1846 	}
1847 
1848 	return (options);
1849 }
1850 
1851 /*
1852  * sa_get_derived_security(object, sectype, proto, hier)
1853  *
1854  * Get the derived security(named optionset) for the object given the
1855  * sectype and proto. If hier is non-zero, walk up the tree to get all
1856  * properties defined for this object, otherwise just those on the
1857  * object.
1858  */
1859 
1860 sa_security_t
1861 sa_get_derived_security(void *object, char *sectype, char *proto, int hier)
1862 {
1863 	sa_security_t newsecurity;
1864 	sa_security_t security;
1865 	sa_group_t group;
1866 	sa_property_t prop;
1867 
1868 	if (hier &&
1869 	    (group = sa_get_parent_group((sa_share_t)object)) != NULL) {
1870 		newsecurity = sa_get_derived_security((void *)group,
1871 		    sectype, proto, hier);
1872 	} else {
1873 		newsecurity = (sa_security_t)xmlNewNode(NULL,
1874 		    (xmlChar *)"security");
1875 		if (newsecurity != NULL) {
1876 			sa_set_security_attr(newsecurity, "type", proto);
1877 			sa_set_security_attr(newsecurity, "sectype", sectype);
1878 		}
1879 	}
1880 	/* Don't do anything if memory wasn't allocated */
1881 	if (newsecurity == NULL)
1882 		return (newsecurity);
1883 
1884 	/* Found the top so working back down the stack. */
1885 	security = sa_get_security((sa_security_t)object, sectype, proto);
1886 	if (security == NULL)
1887 		return (newsecurity);
1888 
1889 	/* add security to the newsecurity */
1890 	for (prop = sa_get_property(security, NULL);
1891 	    prop != NULL; prop = sa_get_next_property(prop)) {
1892 		sa_property_t newprop;
1893 		char *name;
1894 		char *value;
1895 		name = sa_get_property_attr(prop, "type");
1896 		value = sa_get_property_attr(prop, "value");
1897 		if (name != NULL) {
1898 			newprop = sa_get_property(newsecurity, name);
1899 			/* Replace the value with the new value */
1900 			if (newprop != NULL) {
1901 				/*
1902 				 * Only set if value is non NULL, old
1903 				 * value ok if it is NULL. The value
1904 				 * must be associated with the "value"
1905 				 * tag within XML.
1906 				 */
1907 				if (value != NULL)
1908 					set_node_attr(newprop, "value", value);
1909 			} else {
1910 				/* An entirely new property */
1911 				if (value != NULL) {
1912 					newprop = sa_create_property(name,
1913 					    value);
1914 					newprop = (sa_property_t)
1915 					    xmlAddChild((xmlNodePtr)newsecurity,
1916 					    (xmlNodePtr)newprop);
1917 				}
1918 			}
1919 			sa_free_attr_string(name);
1920 		}
1921 		if (value != NULL)
1922 			sa_free_attr_string(value);
1923 	}
1924 	return (newsecurity);
1925 }
1926 
1927 void
1928 sa_free_derived_security(sa_security_t security)
1929 {
1930 	/* while it shouldn't be linked, it doesn't hurt */
1931 	if (security != NULL) {
1932 		xmlUnlinkNode((xmlNodePtr)security);
1933 		xmlFreeNode((xmlNodePtr)security);
1934 	}
1935 }
1936 
1937 /*
1938  * sharetab utility functions
1939  *
1940  * Makes use of the original sharetab.c from fs.d/nfs/lib
1941  */
1942 
1943 /*
1944  * sa_fillshare(share, proto, sh)
1945  *
1946  * Fill the struct share with values obtained from the share object.
1947  */
1948 void
1949 sa_fillshare(sa_share_t share, char *proto, struct share *sh)
1950 {
1951 	char *groupname = NULL;
1952 	char *value;
1953 	sa_group_t group;
1954 	char *buff;
1955 	char *zfs;
1956 	sa_resource_t resource;
1957 	char *rsrcname = NULL;
1958 	char *defprop;
1959 
1960 	/*
1961 	 * We only want to deal with the path level shares for the
1962 	 * sharetab file. If a resource, get the parent.
1963 	 */
1964 	if (sa_is_resource(share)) {
1965 		resource = (sa_resource_t)share;
1966 		share = sa_get_resource_parent(resource);
1967 		rsrcname = sa_get_resource_attr(resource, "name");
1968 	}
1969 
1970 	group = sa_get_parent_group(share);
1971 	if (group != NULL) {
1972 		zfs = sa_get_group_attr(group, "zfs");
1973 		groupname = sa_get_group_attr(group, "name");
1974 
1975 		if (groupname != NULL &&
1976 		    (strcmp(groupname, "default") == 0 || zfs != NULL)) {
1977 			/*
1978 			 * since the groupname is either "default" or the
1979 			 * group is a ZFS group, we don't want to keep
1980 			 * groupname. We do want it if it is any other type of
1981 			 * group.
1982 			 */
1983 			sa_free_attr_string(groupname);
1984 			groupname = NULL;
1985 		}
1986 		if (zfs != NULL)
1987 			sa_free_attr_string(zfs);
1988 	}
1989 
1990 	value = sa_get_share_attr(share, "path");
1991 	if (value != NULL) {
1992 		sh->sh_path = strdup(value);
1993 		sa_free_attr_string(value);
1994 	}
1995 
1996 	if (rsrcname != NULL || groupname != NULL) {
1997 		int len = 0;
1998 
1999 		if (rsrcname != NULL)
2000 			len += strlen(rsrcname);
2001 		if (groupname != NULL)
2002 			len += strlen(groupname);
2003 		len += 3; /* worst case */
2004 		buff = malloc(len);
2005 		(void) snprintf(buff, len, "%s%s%s",
2006 		    (rsrcname != NULL &&
2007 		    strlen(rsrcname) > 0) ? rsrcname : "-",
2008 		    groupname != NULL ? "@" : "",
2009 		    groupname != NULL ? groupname : "");
2010 		sh->sh_res = buff;
2011 		if (rsrcname != NULL)
2012 			sa_free_attr_string(rsrcname);
2013 		if (groupname != NULL)
2014 			sa_free_attr_string(groupname);
2015 	} else {
2016 		sh->sh_res = strdup("-");
2017 	}
2018 
2019 	/*
2020 	 * Get correct default prop string. NFS uses "rw", others use
2021 	 * "".
2022 	 */
2023 	if (strcmp(proto, "nfs") != 0)
2024 		defprop = "\"\"";
2025 	else
2026 		defprop = "rw";
2027 
2028 	sh->sh_fstype = strdup(proto);
2029 	value = sa_proto_legacy_format(proto, share, 1);
2030 	if (value != NULL) {
2031 		if (strlen(value) > 0)
2032 			sh->sh_opts = strdup(value);
2033 		else
2034 			sh->sh_opts = strdup(defprop);
2035 		free(value);
2036 	} else {
2037 		sh->sh_opts = strdup(defprop);
2038 	}
2039 
2040 	value = sa_get_share_description(share);
2041 	if (value != NULL) {
2042 		sh->sh_descr = strdup(value);
2043 		sa_free_share_description(value);
2044 	} else {
2045 		sh->sh_descr = strdup("");
2046 	}
2047 }
2048 
2049 /*
2050  * sa_emptyshare(sh)
2051  *
2052  * Free the strings in the non-NULL members of sh.
2053  */
2054 
2055 void
2056 sa_emptyshare(struct share *sh)
2057 {
2058 	if (sh->sh_path != NULL)
2059 		free(sh->sh_path);
2060 	sh->sh_path = NULL;
2061 	if (sh->sh_res != NULL)
2062 		free(sh->sh_res);
2063 	sh->sh_res = NULL;
2064 	if (sh->sh_fstype != NULL)
2065 		free(sh->sh_fstype);
2066 	sh->sh_fstype = NULL;
2067 	if (sh->sh_opts != NULL)
2068 		free(sh->sh_opts);
2069 	sh->sh_opts = NULL;
2070 	if (sh->sh_descr != NULL)
2071 		free(sh->sh_descr);
2072 	sh->sh_descr = NULL;
2073 }
2074 
2075 /*
2076  * sa_update_sharetab_ts(handle)
2077  *
2078  * Update the internal timestamp of when sharetab was last
2079  * changed. This needs to be public for ZFS to get at it.
2080  */
2081 
2082 void
2083 sa_update_sharetab_ts(sa_handle_t handle)
2084 {
2085 	struct stat st;
2086 	sa_handle_impl_t implhandle = (sa_handle_impl_t)handle;
2087 
2088 	if (implhandle != NULL && stat(SA_LEGACY_SHARETAB, &st) == 0)
2089 		implhandle->tssharetab = TSTAMP(st.st_mtim);
2090 }
2091 
2092 /*
2093  * sa_update_sharetab(share, proto)
2094  *
2095  * Update the sharetab file with info from the specified share.
2096  * This could be an update or add.
2097  */
2098 
2099 int
2100 sa_update_sharetab(sa_share_t share, char *proto)
2101 {
2102 	int	ret = SA_OK;
2103 	share_t	sh;
2104 	char	*path;
2105 	sa_handle_t handle;
2106 
2107 	path = sa_get_share_attr(share, "path");
2108 	if (path != NULL) {
2109 		(void) memset(&sh, '\0', sizeof (sh));
2110 
2111 		handle = sa_find_group_handle((sa_group_t)share);
2112 		if (handle != NULL) {
2113 			/*
2114 			 * Fill in share structure and send it to the kernel.
2115 			 */
2116 			(void) sa_fillshare(share, proto, &sh);
2117 			(void) _sharefs(SHAREFS_ADD, &sh);
2118 			/*
2119 			 * We need the timestamp of the sharetab file right
2120 			 * after the update was done. This lets us detect a
2121 			 * change that made by a different process.
2122 			 */
2123 			sa_update_sharetab_ts(handle);
2124 			sa_emptyshare(&sh);
2125 		} else {
2126 			ret = SA_CONFIG_ERR;
2127 		}
2128 		sa_free_attr_string(path);
2129 	}
2130 
2131 	return (ret);
2132 }
2133 
2134 /*
2135  * sa_delete_sharetab(handle, path, proto)
2136  *
2137  * remove the specified share from sharetab.
2138  */
2139 
2140 int
2141 sa_delete_sharetab(sa_handle_t handle, char *path, char *proto)
2142 {
2143 	int	ret = SA_OK;
2144 	struct stat st;
2145 
2146 	share_t	sh;
2147 	/*
2148 	 * Both the path and the proto are
2149 	 * keys into the sharetab.
2150 	 */
2151 	if (path != NULL && proto != NULL) {
2152 		(void) memset(&sh, '\0', sizeof (sh));
2153 		sh.sh_path = path;
2154 		sh.sh_fstype = proto;
2155 
2156 		ret = _sharefs(SHAREFS_REMOVE, &sh);
2157 		if (handle != NULL && stat(SA_LEGACY_SHARETAB, &st) == 0)
2158 			sa_update_sharetab_ts(handle);
2159 	}
2160 	return (ret);
2161 }
2162 
2163 /*
2164  * sa_needs_refresh(handle)
2165  *
2166  * Returns B_TRUE if the internal cache needs to be refreshed do to a
2167  * change by another process.  B_FALSE returned otherwise.
2168  */
2169 boolean_t
2170 sa_needs_refresh(sa_handle_t handle)
2171 {
2172 	sa_handle_impl_t implhandle = (sa_handle_impl_t)handle;
2173 	struct stat st;
2174 	char *str;
2175 	uint64_t tstamp;
2176 	scf_simple_prop_t *prop;
2177 
2178 	if (handle == NULL)
2179 		return (B_TRUE);
2180 
2181 	/*
2182 	 * If sharetab has changed, then there was an external
2183 	 * change. Check sharetab first since it is updated by ZFS as
2184 	 * well as sharemgr.  This is where external ZFS changes are
2185 	 * caught.
2186 	 */
2187 	if (stat(SA_LEGACY_SHARETAB, &st) == 0 &&
2188 	    TSTAMP(st.st_mtim) != implhandle->tssharetab)
2189 		return (B_TRUE);
2190 
2191 	/*
2192 	 * If sharetab wasn't changed, check whether there were any
2193 	 * SMF transactions that modified the config but didn't
2194 	 * initiate a share.  This is less common but does happen.
2195 	 */
2196 	prop = scf_simple_prop_get(implhandle->scfhandle->handle,
2197 	    (const char *)SA_SVC_FMRI_BASE ":default", "state",
2198 	    "lastupdate");
2199 	if (prop != NULL) {
2200 		str = scf_simple_prop_next_astring(prop);
2201 		if (str != NULL)
2202 			tstamp = strtoull(str, NULL, 0);
2203 		else
2204 			tstamp = 0;
2205 		scf_simple_prop_free(prop);
2206 		if (tstamp != implhandle->tstrans)
2207 			return (B_TRUE);
2208 	}
2209 
2210 	return (B_FALSE);
2211 }
2212 
2213 /*
2214  * sa_fix_resource_name(path)
2215  *
2216  * Convert invalid characters in a resource name (SMB share name)
2217  * to underscores ('_').  The list of invalid characters includes
2218  * control characters and the following:
2219  *
2220  *	" / \ [ ] : | < > + ; , ? * =
2221  *
2222  * The caller must pass a valid path.  Leading and trailing slashes
2223  * are stripped from the path before converting invalid characters.
2224  * Resource names are restricted to SA_MAX_RESOURCE_NAME characters.
2225  */
2226 void
2227 sa_fix_resource_name(char *path)
2228 {
2229 	char *invalid = "\"/\\[]:|<>+;,?*=";
2230 	char *p = path;
2231 	char *q;
2232 	size_t len;
2233 
2234 	assert(path != NULL);
2235 
2236 	/*
2237 	 * Strip leading and trailing /'s.
2238 	 */
2239 	p += strspn(p, "/");
2240 	q = strchr(p, '\0');
2241 	if (q != NULL && q != path) {
2242 		while ((--q, *q == '/'))
2243 			*q = '\0';
2244 	}
2245 
2246 	if (*p == '\0') {
2247 		(void) strcpy(path, "_");
2248 		return;
2249 	}
2250 
2251 	/*
2252 	 * Stride over path components until the remaining
2253 	 * path is no longer than SA_MAX_RESOURCE_NAME.
2254 	 */
2255 	q = p;
2256 	while ((q != NULL) && (strlen(q) > SA_MAX_RESOURCE_NAME)) {
2257 		if ((q = strchr(q, '/')) != NULL) {
2258 			++q;
2259 			p = q;
2260 		}
2261 	}
2262 
2263 	/*
2264 	 * If the path is still longer than SA_MAX_RESOURCE_NAME,
2265 	 * take the trailing SA_MAX_RESOURCE_NAME characters.
2266 	 */
2267 	if ((len = strlen(p)) > SA_MAX_RESOURCE_NAME) {
2268 		len = SA_MAX_RESOURCE_NAME;
2269 		p = strchr(p, '\0') - (SA_MAX_RESOURCE_NAME - 1);
2270 	}
2271 
2272 	(void) memmove(path, p, len);
2273 	path[len] = '\0';
2274 
2275 	for (p = path; *p != '\0'; ++p) {
2276 		if ((iscntrl(*p)) || strchr(invalid, *p))
2277 			*p = '_';
2278 	}
2279 }
2280