16185db85Sdougm /* 26185db85Sdougm * CDDL HEADER START 36185db85Sdougm * 46185db85Sdougm * The contents of this file are subject to the terms of the 56185db85Sdougm * Common Development and Distribution License (the "License"). 66185db85Sdougm * You may not use this file except in compliance with the License. 76185db85Sdougm * 86185db85Sdougm * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 96185db85Sdougm * or http://www.opensolaris.org/os/licensing. 106185db85Sdougm * See the License for the specific language governing permissions 116185db85Sdougm * and limitations under the License. 126185db85Sdougm * 136185db85Sdougm * When distributing Covered Code, include this CDDL HEADER in each 146185db85Sdougm * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 156185db85Sdougm * If applicable, add the following below this CDDL HEADER, with the 166185db85Sdougm * fields enclosed by brackets "[]" replaced with your own identifying 176185db85Sdougm * information: Portions Copyright [yyyy] [name of copyright owner] 186185db85Sdougm * 196185db85Sdougm * CDDL HEADER END 206185db85Sdougm */ 216185db85Sdougm 226185db85Sdougm /* 235b6e0c46Sdougm * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 246185db85Sdougm * Use is subject to license terms. 256185db85Sdougm */ 266185db85Sdougm 276185db85Sdougm /* 286185db85Sdougm * core library for common functions across all config store types 296185db85Sdougm * and file systems to be exported. This includes legacy dfstab/sharetab 306185db85Sdougm * parsing. Need to eliminate XML where possible. 316185db85Sdougm */ 326185db85Sdougm 336185db85Sdougm #include <stdio.h> 346185db85Sdougm #include <string.h> 356185db85Sdougm #include <ctype.h> 366185db85Sdougm #include <unistd.h> 376185db85Sdougm #include <limits.h> 386185db85Sdougm #include <errno.h> 396185db85Sdougm #include <sys/types.h> 406185db85Sdougm #include <sys/stat.h> 416185db85Sdougm #include <libxml/parser.h> 426185db85Sdougm #include <libxml/tree.h> 436185db85Sdougm #include "libshare.h" 446185db85Sdougm #include "libshare_impl.h" 456185db85Sdougm #include <fcntl.h> 46*8d7e4166Sjose borrego #include <thread.h> 476185db85Sdougm #include <grp.h> 486185db85Sdougm #include <limits.h> 496185db85Sdougm #include <sys/param.h> 506185db85Sdougm #include <signal.h> 516185db85Sdougm #include <libintl.h> 52da6c28aaSamw #include <dirent.h> 536185db85Sdougm 54a237e38eSth #include <sharefs/share.h> 556185db85Sdougm #include "sharetab.h" 566185db85Sdougm 576185db85Sdougm #define DFSTAB_NOTICE_LINES 5 586185db85Sdougm static char *notice[DFSTAB_NOTICE_LINES] = { 596185db85Sdougm "# Do not modify this file directly.\n", 606185db85Sdougm "# Use the sharemgr(1m) command for all share management\n", 616185db85Sdougm "# This file is reconstructed and only maintained for backward\n", 626185db85Sdougm "# compatibility. Configuration lines could be lost.\n", 636185db85Sdougm "#\n" 646185db85Sdougm }; 656185db85Sdougm 666185db85Sdougm #define STRNCAT(x, y, z) (xmlChar *)strncat((char *)x, (char *)y, z) 676185db85Sdougm 686185db85Sdougm /* will be much smaller, but this handles bad syntax in the file */ 696185db85Sdougm #define MAXARGSFORSHARE 256 706185db85Sdougm 71*8d7e4166Sjose borrego static mutex_t sharetab_lock = DEFAULTMUTEX; 72*8d7e4166Sjose borrego 736185db85Sdougm /* used internally only */ 746185db85Sdougm typedef 756185db85Sdougm struct sharelist { 766185db85Sdougm struct sharelist *next; 776185db85Sdougm int persist; 786185db85Sdougm char *path; 796185db85Sdougm char *resource; 806185db85Sdougm char *fstype; 816185db85Sdougm char *options; 826185db85Sdougm char *description; 836185db85Sdougm char *group; 846185db85Sdougm char *origline; 856185db85Sdougm int lineno; 866185db85Sdougm } xfs_sharelist_t; 87549ec3ffSdougm static void parse_dfstab(sa_handle_t, char *, xmlNodePtr); 88*8d7e4166Sjose borrego extern char *_sa_get_token(char *); 896185db85Sdougm static void dfs_free_list(xfs_sharelist_t *); 906185db85Sdougm /* prototypes */ 91549ec3ffSdougm void getlegacyconfig(sa_handle_t, char *, xmlNodePtr *); 92da6c28aaSamw extern sa_share_t _sa_add_share(sa_group_t, char *, int, int *, uint64_t); 93549ec3ffSdougm extern sa_group_t _sa_create_group(sa_handle_impl_t, char *); 946185db85Sdougm static void outdfstab(FILE *, xfs_sharelist_t *); 956185db85Sdougm extern int _sa_remove_optionset(sa_optionset_t); 966185db85Sdougm extern int set_node_share(void *, char *, char *); 976185db85Sdougm extern void set_node_attr(void *, char *, char *); 986185db85Sdougm 99a99982a7Sdougm /* 100a99982a7Sdougm * sablocksigs(*sigs) 101a99982a7Sdougm * 102a99982a7Sdougm * block important signals for a critical region. Arg is a pointer to 103a99982a7Sdougm * a sigset_t that is used later for the unblock. 104a99982a7Sdougm */ 105a99982a7Sdougm void 106a99982a7Sdougm sablocksigs(sigset_t *sigs) 107a99982a7Sdougm { 108a99982a7Sdougm sigset_t new; 109a99982a7Sdougm 110a99982a7Sdougm if (sigs != NULL) { 11125a68471Sdougm (void) sigprocmask(SIG_BLOCK, NULL, &new); 11225a68471Sdougm (void) sigaddset(&new, SIGHUP); 11325a68471Sdougm (void) sigaddset(&new, SIGINT); 11425a68471Sdougm (void) sigaddset(&new, SIGQUIT); 11525a68471Sdougm (void) sigaddset(&new, SIGTSTP); 11625a68471Sdougm (void) sigprocmask(SIG_SETMASK, &new, sigs); 117a99982a7Sdougm } 118a99982a7Sdougm } 119a99982a7Sdougm 120a99982a7Sdougm /* 121a99982a7Sdougm * saunblocksigs(*sigs) 122a99982a7Sdougm * 123a99982a7Sdougm * unblock previously blocked signals from the sigs arg. 124a99982a7Sdougm */ 125a99982a7Sdougm void 126a99982a7Sdougm saunblocksigs(sigset_t *sigs) 127a99982a7Sdougm { 128a99982a7Sdougm if (sigs != NULL) 12925a68471Sdougm (void) sigprocmask(SIG_SETMASK, sigs, NULL); 130a99982a7Sdougm } 131a99982a7Sdougm 1326185db85Sdougm /* 1336185db85Sdougm * alloc_sharelist() 1346185db85Sdougm * 1356185db85Sdougm * allocator function to return an zfs_sharelist_t 1366185db85Sdougm */ 1376185db85Sdougm 1386185db85Sdougm static xfs_sharelist_t * 1396185db85Sdougm alloc_sharelist() 1406185db85Sdougm { 1416185db85Sdougm xfs_sharelist_t *item; 1426185db85Sdougm 1436185db85Sdougm item = (xfs_sharelist_t *)malloc(sizeof (xfs_sharelist_t)); 1446185db85Sdougm if (item != NULL) 14525a68471Sdougm (void) memset(item, '\0', sizeof (xfs_sharelist_t)); 1466185db85Sdougm return (item); 1476185db85Sdougm } 1486185db85Sdougm 1496185db85Sdougm /* 1506185db85Sdougm * fix_notice(list) 1516185db85Sdougm * 1526185db85Sdougm * Look at the beginning of the current /etc/dfs/dfstab file and add 1536185db85Sdougm * the do not modify notice if it doesn't exist. 1546185db85Sdougm */ 1556185db85Sdougm 1566185db85Sdougm static xfs_sharelist_t * 1576185db85Sdougm fix_notice(xfs_sharelist_t *list) 1586185db85Sdougm { 1596185db85Sdougm xfs_sharelist_t *item, *prev; 1606185db85Sdougm int i; 1616185db85Sdougm 1627d968cb8Sdougm if (list == NULL) { 16325a68471Sdougm /* zero length dfstab */ 16425a68471Sdougm list = alloc_sharelist(); 16525a68471Sdougm if (list == NULL) 16625a68471Sdougm return (NULL); 16725a68471Sdougm list->description = strdup("#\n"); 1687d968cb8Sdougm } 1696185db85Sdougm if (list->path == NULL && list->description != NULL && 1706185db85Sdougm strcmp(list->description, notice[0]) != 0) { 17125a68471Sdougm for (prev = NULL, i = 0; i < DFSTAB_NOTICE_LINES; i++) { 17225a68471Sdougm item = alloc_sharelist(); 17325a68471Sdougm if (item != NULL) { 17425a68471Sdougm item->description = strdup(notice[i]); 17525a68471Sdougm if (prev == NULL) { 17625a68471Sdougm item->next = list; 17725a68471Sdougm prev = item; 17825a68471Sdougm list = item; 17925a68471Sdougm } else { 18025a68471Sdougm item->next = prev->next; 18125a68471Sdougm prev->next = item; 18225a68471Sdougm prev = item; 18325a68471Sdougm } 18425a68471Sdougm } 1856185db85Sdougm } 1866185db85Sdougm } 1876185db85Sdougm return (list); 1886185db85Sdougm } 1896185db85Sdougm 1906185db85Sdougm /* 1916185db85Sdougm * getdfstab(dfs) 1926185db85Sdougm * 1936185db85Sdougm * Returns an zfs_sharelist_t list of lines from the dfstab file 1946185db85Sdougm * pointed to by the FILE pointer dfs. Each entry is parsed and the 1956185db85Sdougm * original line is also preserved. Used in parsing and updating the 1966185db85Sdougm * dfstab file. 1976185db85Sdougm */ 1986185db85Sdougm 1996185db85Sdougm static xfs_sharelist_t * 2006185db85Sdougm getdfstab(FILE *dfs) 2016185db85Sdougm { 2026185db85Sdougm char buff[_POSIX_ARG_MAX]; /* reasonable size given syntax of share */ 2036185db85Sdougm char *bp; 2046185db85Sdougm char *token; 2056185db85Sdougm char *args[MAXARGSFORSHARE]; 2066185db85Sdougm int argc; 2076185db85Sdougm int c; 2086185db85Sdougm static int line = 0; 2097d968cb8Sdougm xfs_sharelist_t *item = NULL, *first = NULL, *last; 2106185db85Sdougm 2116185db85Sdougm if (dfs != NULL) { 21225a68471Sdougm first = NULL; 21325a68471Sdougm line = 0; 21425a68471Sdougm while (fgets(buff, sizeof (buff), dfs) != NULL) { 21525a68471Sdougm line++; 21625a68471Sdougm bp = buff; 21725a68471Sdougm if (buff[0] == '#') { 21825a68471Sdougm item = alloc_sharelist(); 21925a68471Sdougm if (item != NULL) { 22025a68471Sdougm /* if no path, then comment */ 22125a68471Sdougm item->lineno = line; 22225a68471Sdougm item->description = strdup(buff); 22325a68471Sdougm if (first == NULL) { 22425a68471Sdougm first = item; 22525a68471Sdougm last = item; 22625a68471Sdougm } else { 22725a68471Sdougm last->next = item; 22825a68471Sdougm last = item; 22925a68471Sdougm } 23025a68471Sdougm } else { 23125a68471Sdougm break; 23225a68471Sdougm } 23325a68471Sdougm continue; 23425a68471Sdougm } else if (buff[0] == '\n') { 23525a68471Sdougm continue; 23625a68471Sdougm } 23725a68471Sdougm optind = 1; 23825a68471Sdougm item = alloc_sharelist(); 23925a68471Sdougm if (item == NULL) { 24025a68471Sdougm break; 24125a68471Sdougm } else if (first == NULL) { 24225a68471Sdougm first = item; 24325a68471Sdougm last = item; 2446185db85Sdougm } else { 24525a68471Sdougm last->next = item; 24625a68471Sdougm last = item; 2476185db85Sdougm } 24825a68471Sdougm item->lineno = line; 24925a68471Sdougm item->origline = strdup(buff); 250*8d7e4166Sjose borrego (void) _sa_get_token(NULL); /* reset to new pointers */ 25125a68471Sdougm argc = 0; 252*8d7e4166Sjose borrego while ((token = _sa_get_token(bp)) != NULL) { 25325a68471Sdougm if (argc < MAXARGSFORSHARE) 25425a68471Sdougm args[argc++] = token; 2556185db85Sdougm } 25625a68471Sdougm while ((c = getopt(argc, args, "F:o:d:pg:")) != -1) { 25725a68471Sdougm switch (c) { 25825a68471Sdougm case 'p': 25925a68471Sdougm item->persist = 1; 26025a68471Sdougm break; 26125a68471Sdougm case 'F': 26225a68471Sdougm item->fstype = strdup(optarg); 26325a68471Sdougm break; 26425a68471Sdougm case 'o': 26525a68471Sdougm item->options = strdup(optarg); 26625a68471Sdougm break; 26725a68471Sdougm case 'd': 26825a68471Sdougm item->description = strdup(optarg); 26925a68471Sdougm break; 27025a68471Sdougm case 'g': 27125a68471Sdougm item->group = strdup(optarg); 27225a68471Sdougm break; 27325a68471Sdougm default: 27425a68471Sdougm break; 27525a68471Sdougm } 27625a68471Sdougm } 27725a68471Sdougm if (optind < argc) { 27825a68471Sdougm item->path = strdup(args[optind]); 27925a68471Sdougm optind++; 28025a68471Sdougm if (optind < argc) { 28125a68471Sdougm char *resource; 28225a68471Sdougm char *optgroup; 28325a68471Sdougm /* resource and/or groupname */ 28425a68471Sdougm resource = args[optind]; 28525a68471Sdougm optgroup = strchr(resource, '@'); 28625a68471Sdougm if (optgroup != NULL) 28725a68471Sdougm *optgroup++ = '\0'; 28825a68471Sdougm if (optgroup != NULL) 28925a68471Sdougm item->group = strdup(optgroup); 29025a68471Sdougm if (resource != NULL && 29125a68471Sdougm strlen(resource) > 0) 29225a68471Sdougm item->resource = 29325a68471Sdougm strdup(resource); 29425a68471Sdougm } 29525a68471Sdougm } 29625a68471Sdougm /* NFS is the default if none defined */ 29725a68471Sdougm if (item != NULL && item->fstype == NULL) 29825a68471Sdougm item->fstype = strdup("nfs"); 299a99982a7Sdougm } 3006185db85Sdougm } 3016185db85Sdougm first = fix_notice(first); 3026185db85Sdougm return (first); 3036185db85Sdougm } 3046185db85Sdougm 3056185db85Sdougm /* 3066185db85Sdougm * finddfsentry(list, path) 3076185db85Sdougm * 3087d968cb8Sdougm * Look for path in the zfs_sharelist_t list and return the entry if it 3096185db85Sdougm * exists. 3106185db85Sdougm */ 3116185db85Sdougm 3126185db85Sdougm static xfs_sharelist_t * 3136185db85Sdougm finddfsentry(xfs_sharelist_t *list, char *path) 3146185db85Sdougm { 3156185db85Sdougm xfs_sharelist_t *item; 3166185db85Sdougm 3176185db85Sdougm for (item = list; item != NULL; item = item->next) { 31825a68471Sdougm if (item->path != NULL && strcmp(item->path, path) == 0) 3196185db85Sdougm return (item); 3206185db85Sdougm } 3216185db85Sdougm return (NULL); 3226185db85Sdougm } 3236185db85Sdougm 3246185db85Sdougm /* 3256185db85Sdougm * remdfsentry(list, path, proto) 3266185db85Sdougm * 3276185db85Sdougm * Remove the specified path (with protocol) from the list. This will 3286185db85Sdougm * remove it from dfstab when the file is rewritten. 3296185db85Sdougm */ 3306185db85Sdougm 3316185db85Sdougm static xfs_sharelist_t * 3326185db85Sdougm remdfsentry(xfs_sharelist_t *list, char *path, char *proto) 3336185db85Sdougm { 3346185db85Sdougm xfs_sharelist_t *item, *prev = NULL; 3356185db85Sdougm 3366185db85Sdougm 3376185db85Sdougm for (item = prev = list; item != NULL; item = item->next) { 3386185db85Sdougm /* skip comment entry but don't lose it */ 33925a68471Sdougm if (item->path == NULL) { 34025a68471Sdougm prev = item; 34125a68471Sdougm continue; 34225a68471Sdougm } 34325a68471Sdougm /* if proto is NULL, remove all protocols */ 34425a68471Sdougm if (proto == NULL || (strcmp(item->path, path) == 0 && 34525a68471Sdougm (item->fstype != NULL && strcmp(item->fstype, proto) == 0))) 34625a68471Sdougm break; 34725a68471Sdougm if (item->fstype == NULL && 34825a68471Sdougm (proto == NULL || strcmp(proto, "nfs") == 0)) 34925a68471Sdougm break; 3506185db85Sdougm prev = item; 3516185db85Sdougm } 3526185db85Sdougm if (item != NULL) { 35325a68471Sdougm if (item == prev) 35425a68471Sdougm list = item->next; /* this must be the first one */ 35525a68471Sdougm else 35625a68471Sdougm prev->next = item->next; 35725a68471Sdougm item->next = NULL; 35825a68471Sdougm dfs_free_list(item); 3596185db85Sdougm } 3606185db85Sdougm return (list); 3616185db85Sdougm } 3626185db85Sdougm 3636185db85Sdougm /* 3646185db85Sdougm * remdfsline(list, line) 3656185db85Sdougm * 3666185db85Sdougm * Remove the line specified from the list. 3676185db85Sdougm */ 3686185db85Sdougm 3696185db85Sdougm static xfs_sharelist_t * 3706185db85Sdougm remdfsline(xfs_sharelist_t *list, char *line) 3716185db85Sdougm { 3726185db85Sdougm xfs_sharelist_t *item, *prev = NULL; 3736185db85Sdougm 3746185db85Sdougm for (item = prev = list; item != NULL; item = item->next) { 37525a68471Sdougm /* skip comment entry but don't lose it */ 37625a68471Sdougm if (item->path == NULL) { 3776185db85Sdougm prev = item; 3786185db85Sdougm continue; 37925a68471Sdougm } 38025a68471Sdougm if (strcmp(item->origline, line) == 0) 38125a68471Sdougm break; 38225a68471Sdougm prev = item; 3836185db85Sdougm } 3846185db85Sdougm if (item != NULL) { 38525a68471Sdougm if (item == prev) 38625a68471Sdougm list = item->next; /* this must be the first one */ 38725a68471Sdougm else 38825a68471Sdougm prev->next = item->next; 38925a68471Sdougm item->next = NULL; 39025a68471Sdougm dfs_free_list(item); 3916185db85Sdougm } 3926185db85Sdougm return (list); 3936185db85Sdougm } 3946185db85Sdougm 3956185db85Sdougm /* 3966185db85Sdougm * adddfsentry(list, share, proto) 3976185db85Sdougm * 3986185db85Sdougm * Add an entry to the dfstab list for share (relative to proto). This 3996185db85Sdougm * is used to update dfstab for legacy purposes. 4006185db85Sdougm */ 4016185db85Sdougm 4026185db85Sdougm static xfs_sharelist_t * 4036185db85Sdougm adddfsentry(xfs_sharelist_t *list, sa_share_t share, char *proto) 4046185db85Sdougm { 4056185db85Sdougm xfs_sharelist_t *item, *tmp; 4066185db85Sdougm sa_group_t parent; 4076185db85Sdougm char *groupname; 4086185db85Sdougm 4096185db85Sdougm item = alloc_sharelist(); 4106185db85Sdougm if (item != NULL) { 41125a68471Sdougm parent = sa_get_parent_group(share); 41225a68471Sdougm groupname = sa_get_group_attr(parent, "name"); 41325a68471Sdougm if (strcmp(groupname, "default") == 0) { 41425a68471Sdougm sa_free_attr_string(groupname); 41525a68471Sdougm groupname = NULL; 41625a68471Sdougm } 41725a68471Sdougm item->path = sa_get_share_attr(share, "path"); 41825a68471Sdougm item->resource = sa_get_share_attr(share, "resource"); 41925a68471Sdougm item->group = groupname; 42025a68471Sdougm item->fstype = strdup(proto); 42125a68471Sdougm item->options = sa_proto_legacy_format(proto, share, 1); 42225a68471Sdougm if (item->options != NULL && strlen(item->options) == 0) { 42325a68471Sdougm free(item->options); 42425a68471Sdougm item->options = NULL; 42525a68471Sdougm } 42625a68471Sdougm item->description = sa_get_share_description(share); 42725a68471Sdougm if (item->description != NULL && 42825a68471Sdougm strlen(item->description) == 0) { 42925a68471Sdougm sa_free_share_description(item->description); 43025a68471Sdougm item->description = NULL; 43125a68471Sdougm } 43225a68471Sdougm if (list == NULL) { 43325a68471Sdougm list = item; 43425a68471Sdougm } else { 43525a68471Sdougm for (tmp = list; tmp->next != NULL; tmp = tmp->next) 43625a68471Sdougm /* do nothing */; 43725a68471Sdougm tmp->next = item; 43825a68471Sdougm } 4396185db85Sdougm } 4406185db85Sdougm return (list); 4416185db85Sdougm } 4426185db85Sdougm 4436185db85Sdougm /* 4446185db85Sdougm * outdfstab(dfstab, list) 4456185db85Sdougm * 4466185db85Sdougm * Output the list to dfstab making sure the file is truncated. 4476185db85Sdougm * Comments and errors are preserved. 4486185db85Sdougm */ 4496185db85Sdougm 4506185db85Sdougm static void 4516185db85Sdougm outdfstab(FILE *dfstab, xfs_sharelist_t *list) 4526185db85Sdougm { 4536185db85Sdougm xfs_sharelist_t *item; 4546185db85Sdougm 4556185db85Sdougm (void) ftruncate(fileno(dfstab), 0); 4566185db85Sdougm 4576185db85Sdougm for (item = list; item != NULL; item = item->next) { 45825a68471Sdougm if (item->path != NULL) { 45925a68471Sdougm if (*item->path == '/') { 46025a68471Sdougm (void) fprintf(dfstab, 46125a68471Sdougm "share %s%s%s%s%s%s%s %s%s%s%s%s\n", 46225a68471Sdougm (item->fstype != NULL) ? "-F " : "", 46325a68471Sdougm (item->fstype != NULL) ? item->fstype : "", 46425a68471Sdougm (item->options != NULL) ? " -o " : "", 46525a68471Sdougm (item->options != NULL) ? 46625a68471Sdougm item->options : "", 46725a68471Sdougm (item->description != NULL) ? 46825a68471Sdougm " -d \"" : "", 46925a68471Sdougm (item->description != NULL) ? 47025a68471Sdougm item->description : "", 47125a68471Sdougm (item->description != NULL) ? "\"" : "", 47225a68471Sdougm item->path, 47325a68471Sdougm ((item->resource != NULL) || 47425a68471Sdougm (item->group != NULL)) ? " " : "", 47525a68471Sdougm (item->resource != NULL) ? 47625a68471Sdougm item->resource : "", 47725a68471Sdougm item->group != NULL ? "@" : "", 47825a68471Sdougm item->group != NULL ? item->group : ""); 47925a68471Sdougm } else { 48025a68471Sdougm (void) fprintf(dfstab, "%s", item->origline); 48125a68471Sdougm } 4826185db85Sdougm } else { 48325a68471Sdougm if (item->description != NULL) 48425a68471Sdougm (void) fprintf(dfstab, "%s", item->description); 48525a68471Sdougm else 48625a68471Sdougm (void) fprintf(dfstab, "%s", item->origline); 4876185db85Sdougm } 4886185db85Sdougm } 4896185db85Sdougm } 4906185db85Sdougm 4916185db85Sdougm /* 4926185db85Sdougm * open_dfstab(file) 4936185db85Sdougm * 4946185db85Sdougm * Open the specified dfstab file. If the owner/group/perms are wrong, 4956185db85Sdougm * fix them. 4966185db85Sdougm */ 4976185db85Sdougm 4986185db85Sdougm static FILE * 4996185db85Sdougm open_dfstab(char *file) 5006185db85Sdougm { 5016185db85Sdougm struct group *grp; 5026185db85Sdougm struct group group; 5036185db85Sdougm char *buff; 5046185db85Sdougm int grsize; 5056185db85Sdougm FILE *dfstab; 5066185db85Sdougm 5076185db85Sdougm dfstab = fopen(file, "r+"); 5086185db85Sdougm if (dfstab == NULL) { 50925a68471Sdougm dfstab = fopen(file, "w+"); 5106185db85Sdougm } 5116185db85Sdougm if (dfstab != NULL) { 5126185db85Sdougm grsize = sysconf(_SC_GETGR_R_SIZE_MAX); 5136185db85Sdougm buff = malloc(grsize); 5146185db85Sdougm if (buff != NULL) 51525a68471Sdougm grp = getgrnam_r(SA_DEFAULT_FILE_GRP, &group, buff, 51625a68471Sdougm grsize); 5176185db85Sdougm else 51825a68471Sdougm grp = getgrnam(SA_DEFAULT_FILE_GRP); 5196185db85Sdougm (void) fchmod(fileno(dfstab), 0644); 5206185db85Sdougm (void) fchown(fileno(dfstab), 0, 52125a68471Sdougm grp != NULL ? grp->gr_gid : 3); 5226185db85Sdougm if (buff != NULL) 52325a68471Sdougm free(buff); 5246185db85Sdougm rewind(dfstab); 5256185db85Sdougm } 5266185db85Sdougm return (dfstab); 5276185db85Sdougm } 5286185db85Sdougm 5296185db85Sdougm /* 5306185db85Sdougm * sa_comment_line(line, err) 5316185db85Sdougm * 5326185db85Sdougm * Add a comment to the dfstab file with err as a prefix to the 5336185db85Sdougm * original line. 5346185db85Sdougm */ 5356185db85Sdougm 5366185db85Sdougm static void 5376185db85Sdougm sa_comment_line(char *line, char *err) 5386185db85Sdougm { 5396185db85Sdougm FILE *dfstab; 5406185db85Sdougm xfs_sharelist_t *list; 541a99982a7Sdougm sigset_t old; 5426185db85Sdougm 5436185db85Sdougm dfstab = open_dfstab(SA_LEGACY_DFSTAB); 5446185db85Sdougm if (dfstab != NULL) { 5456185db85Sdougm (void) setvbuf(dfstab, NULL, _IOLBF, BUFSIZ * 8); 546a99982a7Sdougm sablocksigs(&old); 5476185db85Sdougm (void) lockf(fileno(dfstab), F_LOCK, 0); 5486185db85Sdougm list = getdfstab(dfstab); 5496185db85Sdougm rewind(dfstab); 550f345c0beSdougm /* 551f345c0beSdougm * don't ignore the return since the list could have 552f345c0beSdougm * gone to NULL if the file only had one line in it. 553f345c0beSdougm */ 554f345c0beSdougm list = remdfsline(list, line); 5556185db85Sdougm outdfstab(dfstab, list); 5566185db85Sdougm (void) fprintf(dfstab, "# Error: %s: %s", err, line); 5576185db85Sdougm (void) fsync(fileno(dfstab)); 5586185db85Sdougm (void) lockf(fileno(dfstab), F_ULOCK, 0); 5596185db85Sdougm (void) fclose(dfstab); 560a99982a7Sdougm saunblocksigs(&old); 5616185db85Sdougm if (list != NULL) 56225a68471Sdougm dfs_free_list(list); 5636185db85Sdougm } 5646185db85Sdougm } 5656185db85Sdougm 5666185db85Sdougm /* 567da6c28aaSamw * sa_delete_legacy(share, protocol) 5686185db85Sdougm * 5696185db85Sdougm * Delete the specified share from the legacy config file. 5706185db85Sdougm */ 5716185db85Sdougm 5726185db85Sdougm int 573da6c28aaSamw sa_delete_legacy(sa_share_t share, char *protocol) 5746185db85Sdougm { 5756185db85Sdougm FILE *dfstab; 5766185db85Sdougm int err; 5776185db85Sdougm int ret = SA_OK; 5786185db85Sdougm xfs_sharelist_t *list; 5796185db85Sdougm char *path; 5806185db85Sdougm sa_optionset_t optionset; 5816185db85Sdougm sa_group_t parent; 582a99982a7Sdougm sigset_t old; 5836185db85Sdougm 584da6c28aaSamw /* 585da6c28aaSamw * Protect against shares that don't have paths. This is not 586da6c28aaSamw * really an error at this point. 587da6c28aaSamw */ 588da6c28aaSamw path = sa_get_share_attr(share, "path"); 589da6c28aaSamw if (path == NULL) 590da6c28aaSamw return (ret); 591da6c28aaSamw 5926185db85Sdougm dfstab = open_dfstab(SA_LEGACY_DFSTAB); 5936185db85Sdougm if (dfstab != NULL) { 5946185db85Sdougm (void) setvbuf(dfstab, NULL, _IOLBF, BUFSIZ * 8); 595a99982a7Sdougm sablocksigs(&old); 5966185db85Sdougm parent = sa_get_parent_group(share); 5976185db85Sdougm if (parent != NULL) { 59825a68471Sdougm (void) lockf(fileno(dfstab), F_LOCK, 0); 59925a68471Sdougm list = getdfstab(dfstab); 60025a68471Sdougm rewind(dfstab); 601da6c28aaSamw if (protocol != NULL) { 602da6c28aaSamw if (list != NULL) 60325a68471Sdougm list = remdfsentry(list, path, 604da6c28aaSamw protocol); 605da6c28aaSamw } else { 606da6c28aaSamw for (optionset = sa_get_optionset(parent, NULL); 607da6c28aaSamw optionset != NULL; 608da6c28aaSamw optionset = 609da6c28aaSamw sa_get_next_optionset(optionset)) { 610da6c28aaSamw char *proto = sa_get_optionset_attr( 611da6c28aaSamw optionset, "type"); 612da6c28aaSamw 613da6c28aaSamw if (list != NULL && proto != NULL) 614da6c28aaSamw list = remdfsentry(list, path, 615da6c28aaSamw proto); 616da6c28aaSamw if (proto == NULL) 617da6c28aaSamw ret = SA_NO_MEMORY; 618da6c28aaSamw /* 619da6c28aaSamw * may want to only do the dfstab if 620da6c28aaSamw * this call returns NOT IMPLEMENTED 621da6c28aaSamw * but it shouldn't hurt. 622da6c28aaSamw */ 623da6c28aaSamw if (ret == SA_OK) { 624da6c28aaSamw err = sa_proto_delete_legacy( 625da6c28aaSamw proto, share); 626da6c28aaSamw if (err != SA_NOT_IMPLEMENTED) 627da6c28aaSamw ret = err; 628da6c28aaSamw } 629da6c28aaSamw if (proto != NULL) 630da6c28aaSamw sa_free_attr_string(proto); 63125a68471Sdougm } 6326185db85Sdougm } 63325a68471Sdougm outdfstab(dfstab, list); 63425a68471Sdougm if (list != NULL) 63525a68471Sdougm dfs_free_list(list); 63625a68471Sdougm (void) fflush(dfstab); 63725a68471Sdougm (void) lockf(fileno(dfstab), F_ULOCK, 0); 6386185db85Sdougm } 6396185db85Sdougm (void) fsync(fileno(dfstab)); 640a99982a7Sdougm saunblocksigs(&old); 6416185db85Sdougm (void) fclose(dfstab); 6426185db85Sdougm } else { 64325a68471Sdougm if (errno == EACCES || errno == EPERM) 64425a68471Sdougm ret = SA_NO_PERMISSION; 64525a68471Sdougm else 64625a68471Sdougm ret = SA_CONFIG_ERR; 6476185db85Sdougm } 648da6c28aaSamw 649da6c28aaSamw if (path != NULL) 650da6c28aaSamw sa_free_attr_string(path); 651da6c28aaSamw 6526185db85Sdougm return (ret); 6536185db85Sdougm } 6546185db85Sdougm 6556185db85Sdougm /* 6566185db85Sdougm * sa_update_legacy(share, proto) 6576185db85Sdougm * 6586185db85Sdougm * There is an assumption that dfstab will be the most common form of 6596185db85Sdougm * legacy configuration file for shares, but not the only one. Because 6606185db85Sdougm * of that, dfstab handling is done in the main code with calls to 661da6c28aaSamw * this function and protocol specific calls to deal with formatting 6626185db85Sdougm * options into dfstab/share compatible syntax. Since not everything 6636185db85Sdougm * will be dfstab, there is a provision for calling a protocol 6646185db85Sdougm * specific plugin interface that allows the protocol plugin to do its 6656185db85Sdougm * own legacy files and skip the dfstab update. 6666185db85Sdougm */ 6676185db85Sdougm 6686185db85Sdougm int 6696185db85Sdougm sa_update_legacy(sa_share_t share, char *proto) 6706185db85Sdougm { 6716185db85Sdougm FILE *dfstab; 6726185db85Sdougm int ret = SA_OK; 6736185db85Sdougm xfs_sharelist_t *list; 6746185db85Sdougm char *path; 675a99982a7Sdougm sigset_t old; 6766185db85Sdougm char *persist; 677da6c28aaSamw uint64_t features; 6786185db85Sdougm 6796185db85Sdougm ret = sa_proto_update_legacy(proto, share); 6806185db85Sdougm if (ret != SA_NOT_IMPLEMENTED) 68125a68471Sdougm return (ret); 682da6c28aaSamw 683da6c28aaSamw features = sa_proto_get_featureset(proto); 684da6c28aaSamw if (!(features & SA_FEATURE_DFSTAB)) 685da6c28aaSamw return (ret); 686da6c28aaSamw 6876185db85Sdougm /* do the dfstab format */ 6886185db85Sdougm persist = sa_get_share_attr(share, "type"); 6896185db85Sdougm /* 6906185db85Sdougm * only update if the share is not transient -- no share type 6916185db85Sdougm * set or the type is not "transient". 6926185db85Sdougm */ 6936185db85Sdougm if (persist == NULL || strcmp(persist, "transient") != 0) { 69425a68471Sdougm dfstab = open_dfstab(SA_LEGACY_DFSTAB); 69525a68471Sdougm if (dfstab != NULL) { 69625a68471Sdougm (void) setvbuf(dfstab, NULL, _IOLBF, BUFSIZ * 8); 69725a68471Sdougm sablocksigs(&old); 69825a68471Sdougm path = sa_get_share_attr(share, "path"); 69925a68471Sdougm (void) lockf(fileno(dfstab), F_LOCK, 0); 70025a68471Sdougm list = getdfstab(dfstab); 70125a68471Sdougm rewind(dfstab); 70225a68471Sdougm if (list != NULL) 70325a68471Sdougm list = remdfsentry(list, path, proto); 70425a68471Sdougm list = adddfsentry(list, share, proto); 70525a68471Sdougm outdfstab(dfstab, list); 70625a68471Sdougm (void) fflush(dfstab); 70725a68471Sdougm (void) lockf(fileno(dfstab), F_ULOCK, 0); 70825a68471Sdougm (void) fsync(fileno(dfstab)); 70925a68471Sdougm saunblocksigs(&old); 71025a68471Sdougm (void) fclose(dfstab); 71125a68471Sdougm sa_free_attr_string(path); 71225a68471Sdougm if (list != NULL) 71325a68471Sdougm dfs_free_list(list); 7146185db85Sdougm } else { 71525a68471Sdougm if (errno == EACCES || errno == EPERM) 71625a68471Sdougm ret = SA_NO_PERMISSION; 71725a68471Sdougm else 71825a68471Sdougm ret = SA_CONFIG_ERR; 7196185db85Sdougm } 7206185db85Sdougm } 7216185db85Sdougm if (persist != NULL) 72225a68471Sdougm sa_free_attr_string(persist); 7236185db85Sdougm return (ret); 7246185db85Sdougm } 7256185db85Sdougm 7266185db85Sdougm /* 7276185db85Sdougm * sa_is_security(optname, proto) 7286185db85Sdougm * 7296185db85Sdougm * Check to see if optname is a security (named optionset) specific 7306185db85Sdougm * property for the specified protocol. 7316185db85Sdougm */ 7326185db85Sdougm 7336185db85Sdougm int 7346185db85Sdougm sa_is_security(char *optname, char *proto) 7356185db85Sdougm { 7366185db85Sdougm int ret = 0; 7376185db85Sdougm if (proto != NULL) 73825a68471Sdougm ret = sa_proto_security_prop(proto, optname); 7396185db85Sdougm return (ret); 7406185db85Sdougm } 7416185db85Sdougm 7426185db85Sdougm /* 7436185db85Sdougm * add_syntax_comment(root, line, err, todfstab) 7446185db85Sdougm * 74525a68471Sdougm * Add a comment to the document indicating a syntax error. If 7466185db85Sdougm * todfstab is set, write it back to the dfstab file as well. 7476185db85Sdougm */ 7486185db85Sdougm 7496185db85Sdougm static void 7506185db85Sdougm add_syntax_comment(xmlNodePtr root, char *line, char *err, int todfstab) 7516185db85Sdougm { 7526185db85Sdougm xmlNodePtr node; 7536185db85Sdougm 7546185db85Sdougm node = xmlNewChild(root, NULL, (xmlChar *)"error", (xmlChar *)line); 75525a68471Sdougm if (node != NULL) 7567a9d7716Sthurlow (void) xmlSetProp(node, (xmlChar *)"type", (xmlChar *)err); 7576185db85Sdougm if (todfstab) 75825a68471Sdougm sa_comment_line(line, err); 7596185db85Sdougm } 7606185db85Sdougm 7616185db85Sdougm /* 7626185db85Sdougm * sa_is_share(object) 7636185db85Sdougm * 7646185db85Sdougm * returns true of the object is of type "share". 7656185db85Sdougm */ 7666185db85Sdougm 7676185db85Sdougm int 7686185db85Sdougm sa_is_share(void *object) 7696185db85Sdougm { 7706185db85Sdougm if (object != NULL) { 77125a68471Sdougm if (strcmp((char *)((xmlNodePtr)object)->name, "share") == 0) 7726185db85Sdougm return (1); 7736185db85Sdougm } 7746185db85Sdougm return (0); 7756185db85Sdougm } 776da6c28aaSamw /* 777da6c28aaSamw * sa_is_resource(object) 778da6c28aaSamw * 779da6c28aaSamw * returns true of the object is of type "share". 780da6c28aaSamw */ 781da6c28aaSamw 782da6c28aaSamw int 783da6c28aaSamw sa_is_resource(void *object) 784da6c28aaSamw { 785da6c28aaSamw if (object != NULL) { 786da6c28aaSamw if (strcmp((char *)((xmlNodePtr)object)->name, "resource") == 0) 787da6c28aaSamw return (1); 788da6c28aaSamw } 789da6c28aaSamw return (0); 790da6c28aaSamw } 7916185db85Sdougm 7926185db85Sdougm /* 7936185db85Sdougm * _sa_remove_property(property) 7946185db85Sdougm * 7956185db85Sdougm * remove a property only from the document. 7966185db85Sdougm */ 7976185db85Sdougm 7986185db85Sdougm static void 7996185db85Sdougm _sa_remove_property(sa_property_t property) 8006185db85Sdougm { 8016185db85Sdougm xmlUnlinkNode((xmlNodePtr)property); 8026185db85Sdougm xmlFreeNode((xmlNodePtr)property); 8036185db85Sdougm } 8046185db85Sdougm 80567331909Sdougm /* 80667331909Sdougm * _sa_create_dummy_share() 80767331909Sdougm * 80867331909Sdougm * Create a share entry suitable for parsing but not tied to any real 80967331909Sdougm * config tree. Need to have a parent as well as the node to parse 81067331909Sdougm * on. Free using _sa_free_dummy_share(share); 81167331909Sdougm */ 81267331909Sdougm 81367331909Sdougm static sa_group_t 81467331909Sdougm _sa_create_dummy_share() 81567331909Sdougm { 81667331909Sdougm xmlNodePtr parent_node = NULL; 81767331909Sdougm xmlNodePtr child_node = NULL; 81867331909Sdougm 81967331909Sdougm parent_node = xmlNewNode(NULL, (xmlChar *)"group"); 82067331909Sdougm if (parent_node != NULL) { 82125a68471Sdougm child_node = xmlNewChild(parent_node, NULL, (xmlChar *)"share", 82225a68471Sdougm NULL); 82325a68471Sdougm if (child_node != NULL) { 82425a68471Sdougm /* 82525a68471Sdougm * Use a "zfs" tag since that will make sure nothing 82625a68471Sdougm * really attempts to put values into the 82725a68471Sdougm * repository. Also ZFS is currently the only user of 82825a68471Sdougm * this interface. 82925a68471Sdougm */ 83025a68471Sdougm set_node_attr(parent_node, "type", "transient"); 83125a68471Sdougm set_node_attr(parent_node, "zfs", "true"); 83225a68471Sdougm set_node_attr(child_node, "type", "transient"); 83325a68471Sdougm set_node_attr(child_node, "zfs", "true"); 83425a68471Sdougm } else { 83525a68471Sdougm xmlFreeNode(parent_node); 83625a68471Sdougm } 83767331909Sdougm } 83867331909Sdougm return (child_node); 83967331909Sdougm } 84067331909Sdougm 84167331909Sdougm /* 84267331909Sdougm * _sa_free_dummy_share(share) 84367331909Sdougm * 84467331909Sdougm * Free the dummy share and its parent. It is an error to try and 84567331909Sdougm * free something that isn't a dummy. 84667331909Sdougm */ 84767331909Sdougm 84867331909Sdougm static int 84967331909Sdougm _sa_free_dummy_share(sa_share_t share) 85067331909Sdougm { 85167331909Sdougm xmlNodePtr node = (xmlNodePtr)share; 85267331909Sdougm xmlNodePtr parent; 85367331909Sdougm int ret = SA_OK; 85467331909Sdougm char *name; 85567331909Sdougm 85667331909Sdougm if (node != NULL) { 85725a68471Sdougm parent = node->parent; 85825a68471Sdougm name = (char *)xmlGetProp(node, (xmlChar *)"path"); 85925a68471Sdougm if (name != NULL) { 86025a68471Sdougm /* Real shares always have a path but a dummy doesn't */ 86125a68471Sdougm ret = SA_NOT_ALLOWED; 86225a68471Sdougm sa_free_attr_string(name); 86325a68471Sdougm } else { 86425a68471Sdougm /* 86525a68471Sdougm * If there is a parent, do the free on that since 86625a68471Sdougm * xmlFreeNode is a recursive function and free's an 86725a68471Sdougm * child nodes. 86825a68471Sdougm */ 86925a68471Sdougm if (parent != NULL) { 87025a68471Sdougm node = parent; 87125a68471Sdougm } 87225a68471Sdougm xmlUnlinkNode(node); 87325a68471Sdougm xmlFreeNode(node); 87467331909Sdougm } 87567331909Sdougm } 87667331909Sdougm return (ret); 87767331909Sdougm } 87867331909Sdougm 87967331909Sdougm 8806185db85Sdougm /* 8816185db85Sdougm * sa_parse_legacy_options(group, options, proto) 8826185db85Sdougm * 8836185db85Sdougm * In order to support legacy configurations, we allow the protocol 8846185db85Sdougm * specific plugin to parse legacy syntax options (like those in 8856185db85Sdougm * /etc/dfs/dfstab). This adds a new optionset to the group (or 8866185db85Sdougm * share). 8876185db85Sdougm * 8886185db85Sdougm * Once the optionset has been created, we then get the derived 8896185db85Sdougm * optionset of the parent (options from the optionset of the parent 8906185db85Sdougm * and any parent it might have) and remove those from the created 8916185db85Sdougm * optionset. This avoids duplication of options. 8926185db85Sdougm */ 8936185db85Sdougm 8946185db85Sdougm int 8956185db85Sdougm sa_parse_legacy_options(sa_group_t group, char *options, char *proto) 8966185db85Sdougm { 8976185db85Sdougm int ret = SA_INVALID_PROTOCOL; 8986185db85Sdougm sa_group_t parent; 89967331909Sdougm int using_dummy = B_FALSE; 900717a41ebSdougm char *pvalue; 90125a68471Sdougm sa_optionset_t optionset; 90225a68471Sdougm sa_property_t popt, prop; 90325a68471Sdougm sa_optionset_t localoptions; 90467331909Sdougm 90567331909Sdougm /* 90625a68471Sdougm * If "group" is NULL, this is just a parse without saving 90767331909Sdougm * anything in either SMF or ZFS. Create a dummy group to 90867331909Sdougm * handle this case. 90967331909Sdougm */ 91067331909Sdougm if (group == NULL) { 91125a68471Sdougm group = (sa_group_t)_sa_create_dummy_share(); 91225a68471Sdougm using_dummy = B_TRUE; 91367331909Sdougm } 91467331909Sdougm 9156185db85Sdougm parent = sa_get_parent_group(group); 9166185db85Sdougm 9176185db85Sdougm if (proto != NULL) 91825a68471Sdougm ret = sa_proto_legacy_opts(proto, group, options); 91967331909Sdougm 92067331909Sdougm if (using_dummy) { 92125a68471Sdougm /* Since this is a dummy parse, cleanup and quit here */ 92225a68471Sdougm (void) _sa_free_dummy_share(parent); 92325a68471Sdougm return (ret); 92467331909Sdougm } 92525a68471Sdougm 92625a68471Sdougm if (ret != SA_OK) 92725a68471Sdougm return (ret); 92825a68471Sdougm 9296185db85Sdougm /* 93025a68471Sdougm * If in a group, remove the inherited options and security 9316185db85Sdougm */ 93225a68471Sdougm 93325a68471Sdougm if (parent == NULL) 93425a68471Sdougm return (ret); 93525a68471Sdougm 93625a68471Sdougm /* Find parent options to remove from child */ 93725a68471Sdougm optionset = sa_get_derived_optionset(parent, proto, 1); 93825a68471Sdougm localoptions = sa_get_optionset(group, proto); 93925a68471Sdougm if (optionset != NULL) { 94025a68471Sdougm for (popt = sa_get_property(optionset, NULL); 94125a68471Sdougm popt != NULL; 94225a68471Sdougm popt = sa_get_next_property(popt)) { 9436185db85Sdougm char *tag; 944717a41ebSdougm char *value; 9456185db85Sdougm tag = sa_get_property_attr(popt, "type"); 94625a68471Sdougm if (tag == NULL) 94725a68471Sdougm continue; 94825a68471Sdougm prop = sa_get_property(localoptions, tag); 94925a68471Sdougm if (prop != NULL) { 95025a68471Sdougm value = sa_get_property_attr(popt, 95125a68471Sdougm "value"); 95225a68471Sdougm pvalue = sa_get_property_attr(prop, 95325a68471Sdougm "value"); 954717a41ebSdougm if (value != NULL && pvalue != NULL && 955717a41ebSdougm strcmp(value, pvalue) == 0) { 956717a41ebSdougm /* 95725a68471Sdougm * Remove the property 95825a68471Sdougm * from the 95925a68471Sdougm * child. While we 96025a68471Sdougm * removed it, we 96125a68471Sdougm * don't need to reset 96225a68471Sdougm * as we do below 96325a68471Sdougm * since we always 96425a68471Sdougm * search from the 96525a68471Sdougm * beginning. 966717a41ebSdougm */ 96725a68471Sdougm (void) _sa_remove_property( 96825a68471Sdougm prop); 9696185db85Sdougm } 970717a41ebSdougm if (value != NULL) 97125a68471Sdougm sa_free_attr_string(value); 972717a41ebSdougm if (pvalue != NULL) 97325a68471Sdougm sa_free_attr_string(pvalue); 9746185db85Sdougm } 97525a68471Sdougm sa_free_attr_string(tag); 97625a68471Sdougm } 97725a68471Sdougm prop = sa_get_property(localoptions, NULL); 97825a68471Sdougm if (prop == NULL && sa_is_share(group)) { 9796185db85Sdougm /* 98025a68471Sdougm * All properties removed so remove the 9816185db85Sdougm * optionset if it is on a share 9826185db85Sdougm */ 9836185db85Sdougm (void) _sa_remove_optionset(localoptions); 9846185db85Sdougm } 98525a68471Sdougm sa_free_derived_optionset(optionset); 98625a68471Sdougm } 98725a68471Sdougm /* 98825a68471Sdougm * Need to remove security here. If there are no 98925a68471Sdougm * security options on the local group/share, don't 99025a68471Sdougm * bother since those are the only ones that would be 99125a68471Sdougm * affected. 99225a68471Sdougm */ 99325a68471Sdougm localoptions = sa_get_all_security_types(group, proto, 0); 99425a68471Sdougm if (localoptions != NULL) { 99525a68471Sdougm for (prop = sa_get_property(localoptions, NULL); 99625a68471Sdougm prop != NULL; 99725a68471Sdougm prop = sa_get_next_property(prop)) { 9986185db85Sdougm char *tag; 9996185db85Sdougm sa_security_t security; 10006185db85Sdougm tag = sa_get_property_attr(prop, "type"); 10016185db85Sdougm if (tag != NULL) { 100225a68471Sdougm sa_property_t nextpopt = NULL; 100325a68471Sdougm security = sa_get_security(group, tag, proto); 100425a68471Sdougm sa_free_attr_string(tag); 1005717a41ebSdougm /* 100625a68471Sdougm * prop's value only changes outside this loop 1007717a41ebSdougm */ 100825a68471Sdougm pvalue = sa_get_property_attr(prop, "value"); 100925a68471Sdougm for (popt = sa_get_property(security, NULL); 101025a68471Sdougm popt != NULL; 101125a68471Sdougm popt = nextpopt) { 101225a68471Sdougm char *value; 101325a68471Sdougm /* 101425a68471Sdougm * Need to get the next prop 101525a68471Sdougm * now since we could break 101625a68471Sdougm * the list during removal. 101725a68471Sdougm */ 101825a68471Sdougm nextpopt = sa_get_next_property(popt); 101925a68471Sdougm /* remove Duplicates from this level */ 102025a68471Sdougm value = sa_get_property_attr(popt, 102125a68471Sdougm "value"); 102225a68471Sdougm if (value != NULL && pvalue != NULL && 102325a68471Sdougm strcmp(value, pvalue) == 0) { 102425a68471Sdougm /* 102525a68471Sdougm * remove the property 102625a68471Sdougm * from the child 102725a68471Sdougm */ 102825a68471Sdougm (void) _sa_remove_property 102925a68471Sdougm (popt); 103025a68471Sdougm } 103125a68471Sdougm if (value != NULL) 103225a68471Sdougm sa_free_attr_string(value); 10336185db85Sdougm } 103425a68471Sdougm if (pvalue != NULL) 103525a68471Sdougm sa_free_attr_string(pvalue); 10366185db85Sdougm } 10376185db85Sdougm } 103825a68471Sdougm (void) sa_destroy_optionset(localoptions); 10396185db85Sdougm } 10406185db85Sdougm return (ret); 10416185db85Sdougm } 10426185db85Sdougm 10436185db85Sdougm /* 10446185db85Sdougm * dfs_free_list(list) 10456185db85Sdougm * 10466185db85Sdougm * Free the data in each list entry of the list as well as freeing the 10476185db85Sdougm * entries themselves. We need to avoid memory leaks and don't want to 10486185db85Sdougm * dereference any NULL members. 10496185db85Sdougm */ 10506185db85Sdougm 10516185db85Sdougm static void 10526185db85Sdougm dfs_free_list(xfs_sharelist_t *list) 10536185db85Sdougm { 10546185db85Sdougm xfs_sharelist_t *entry; 10556185db85Sdougm for (entry = list; entry != NULL; entry = list) { 105625a68471Sdougm if (entry->path != NULL) 105725a68471Sdougm free(entry->path); 105825a68471Sdougm if (entry->resource != NULL) 105925a68471Sdougm free(entry->resource); 106025a68471Sdougm if (entry->fstype != NULL) 106125a68471Sdougm free(entry->fstype); 106225a68471Sdougm if (entry->options != NULL) 106325a68471Sdougm free(entry->options); 106425a68471Sdougm if (entry->description != NULL) 106525a68471Sdougm free(entry->description); 106625a68471Sdougm if (entry->origline != NULL) 106725a68471Sdougm free(entry->origline); 106825a68471Sdougm if (entry->group != NULL) 106925a68471Sdougm free(entry->group); 107025a68471Sdougm list = list->next; 107125a68471Sdougm free(entry); 10726185db85Sdougm } 10736185db85Sdougm } 10746185db85Sdougm 10756185db85Sdougm /* 10766185db85Sdougm * parse_dfstab(dfstab, root) 10776185db85Sdougm * 10786185db85Sdougm * Open and read the existing dfstab, parsing each line and adding it 10796185db85Sdougm * to the internal configuration. Make sure syntax errors, etc are 10806185db85Sdougm * preserved as comments. 10816185db85Sdougm */ 10826185db85Sdougm 10836185db85Sdougm static void 1084549ec3ffSdougm parse_dfstab(sa_handle_t handle, char *dfstab, xmlNodePtr root) 10856185db85Sdougm { 10866185db85Sdougm sa_share_t share; 10876185db85Sdougm sa_group_t group; 10886185db85Sdougm sa_group_t sgroup = NULL; 10896185db85Sdougm sa_group_t defgroup; 10906185db85Sdougm xfs_sharelist_t *head, *list; 10916185db85Sdougm int err; 10926185db85Sdougm int defined_group; 10936185db85Sdougm FILE *dfs; 10946185db85Sdougm char *oldprops; 10956185db85Sdougm 10966185db85Sdougm /* read the dfstab format file and fill in the doc tree */ 10976185db85Sdougm 10986185db85Sdougm dfs = fopen(dfstab, "r"); 109925a68471Sdougm if (dfs == NULL) 110025a68471Sdougm return; 11016185db85Sdougm 1102549ec3ffSdougm defgroup = sa_get_group(handle, "default"); 11036185db85Sdougm 11046185db85Sdougm for (head = list = getdfstab(dfs); 110525a68471Sdougm list != NULL; 110625a68471Sdougm list = list->next) { 110725a68471Sdougm share = NULL; 110825a68471Sdougm group = NULL; 110925a68471Sdougm defined_group = 0; 111025a68471Sdougm err = 0; 111125a68471Sdougm 111225a68471Sdougm if (list->origline == NULL) { 111325a68471Sdougm /* 111425a68471Sdougm * Comment line that we will likely skip. 111525a68471Sdougm * If the line has the syntax: 111625a68471Sdougm * # error: string: string 111725a68471Sdougm * It should be preserved until manually deleted. 111825a68471Sdougm */ 111925a68471Sdougm if (list->description != NULL && 112025a68471Sdougm strncmp(list->description, "# Error: ", 9) == 0) { 112125a68471Sdougm char *line; 112225a68471Sdougm char *error; 112325a68471Sdougm char *cmd; 112425a68471Sdougm line = strdup(list->description); 112525a68471Sdougm if (line != NULL) { 112625a68471Sdougm error = line + 9; 112725a68471Sdougm cmd = strchr(error, ':'); 112825a68471Sdougm if (cmd != NULL) { 112925a68471Sdougm int len; 113025a68471Sdougm *cmd = '\0'; 113125a68471Sdougm cmd += 2; 113225a68471Sdougm len = strlen(cmd); 113325a68471Sdougm cmd[len - 1] = '\0'; 113425a68471Sdougm add_syntax_comment(root, cmd, 113525a68471Sdougm error, 0); 113625a68471Sdougm } 113725a68471Sdougm free(line); 113825a68471Sdougm } 11396185db85Sdougm } 114025a68471Sdougm continue; 11416185db85Sdougm } 114225a68471Sdougm if (list->path != NULL && strlen(list->path) > 0 && 1143549ec3ffSdougm *list->path == '/') { 114425a68471Sdougm share = sa_find_share(handle, list->path); 114525a68471Sdougm if (share != NULL) 114625a68471Sdougm sgroup = sa_get_parent_group(share); 114725a68471Sdougm else 114825a68471Sdougm sgroup = NULL; 114925a68471Sdougm } else { 115025a68471Sdougm (void) printf(dgettext(TEXT_DOMAIN, 115125a68471Sdougm "No share specified in dfstab: " 115225a68471Sdougm "line %d: %s\n"), 115325a68471Sdougm list->lineno, list->origline); 115425a68471Sdougm add_syntax_comment(root, list->origline, 115525a68471Sdougm dgettext(TEXT_DOMAIN, "No share specified"), 1); 115625a68471Sdougm continue; 115725a68471Sdougm } 115825a68471Sdougm if (list->group != NULL && strlen(list->group) > 0) { 115925a68471Sdougm group = sa_get_group(handle, list->group); 116025a68471Sdougm defined_group = 1; 116125a68471Sdougm } else { 116225a68471Sdougm group = defgroup; 116325a68471Sdougm } 116425a68471Sdougm if (defined_group && group == NULL) { 116525a68471Sdougm (void) printf(dgettext(TEXT_DOMAIN, 116625a68471Sdougm "Unknown group used in dfstab: line %d: %s\n"), 116725a68471Sdougm list->lineno, list->origline); 116825a68471Sdougm add_syntax_comment(root, list->origline, 116925a68471Sdougm dgettext(TEXT_DOMAIN, "Unknown group specified"), 117025a68471Sdougm 1); 117125a68471Sdougm continue; 117225a68471Sdougm } 117325a68471Sdougm if (group == NULL) { 117425a68471Sdougm /* Shouldn't happen unless an SMF error */ 117525a68471Sdougm err = SA_CONFIG_ERR; 117625a68471Sdougm continue; 117725a68471Sdougm } 11786185db85Sdougm if (share == NULL) { 117925a68471Sdougm if (defined_group || group != defgroup) 118025a68471Sdougm continue; 118125a68471Sdougm /* This is an OK add for legacy */ 11826185db85Sdougm share = sa_add_share(defgroup, list->path, 118325a68471Sdougm SA_SHARE_PERMANENT | SA_SHARE_PARSER, &err); 11846185db85Sdougm if (share != NULL) { 118525a68471Sdougm if (list->description != NULL && 118625a68471Sdougm strlen(list->description) > 0) 118725a68471Sdougm (void) sa_set_share_description(share, 118825a68471Sdougm list->description); 118925a68471Sdougm if (list->options != NULL && 119025a68471Sdougm strlen(list->options) > 0) { 119125a68471Sdougm (void) sa_parse_legacy_options(share, 119225a68471Sdougm list->options, list->fstype); 119325a68471Sdougm } 119425a68471Sdougm if (list->resource != NULL) 119525a68471Sdougm (void) sa_set_share_attr(share, 119625a68471Sdougm "resource", list->resource); 11976185db85Sdougm } else { 119825a68471Sdougm (void) printf(dgettext(TEXT_DOMAIN, 119925a68471Sdougm "Error in dfstab: line %d: %s\n"), 12006185db85Sdougm list->lineno, list->origline); 120125a68471Sdougm if (err != SA_BAD_PATH) 120225a68471Sdougm add_syntax_comment(root, list->origline, 120325a68471Sdougm dgettext(TEXT_DOMAIN, "Syntax"), 1); 120425a68471Sdougm else 120525a68471Sdougm add_syntax_comment(root, list->origline, 120625a68471Sdougm dgettext(TEXT_DOMAIN, 120725a68471Sdougm "Path"), 1); 120825a68471Sdougm continue; 12096185db85Sdougm } 12106185db85Sdougm } else { 121125a68471Sdougm if (group != sgroup) { 121225a68471Sdougm (void) printf(dgettext(TEXT_DOMAIN, 121325a68471Sdougm "Attempt to change configuration in " 121425a68471Sdougm "dfstab: line %d: %s\n"), 121525a68471Sdougm list->lineno, list->origline); 121625a68471Sdougm add_syntax_comment(root, list->origline, 121725a68471Sdougm dgettext(TEXT_DOMAIN, 121825a68471Sdougm "Attempt to change configuration"), 1); 121925a68471Sdougm continue; 122025a68471Sdougm } 1221546405c3Sdougm /* 1222546405c3Sdougm * It is the same group but could have changed 1223546405c3Sdougm * options. Make sure we include the group's 1224546405c3Sdougm * properties so we don't end up moving them to 1225546405c3Sdougm * the share inadvertantly. The last arg being 1226546405c3Sdougm * true says to get the inherited properties as well 1227546405c3Sdougm * as the local properties. 1228546405c3Sdougm */ 122925a68471Sdougm oldprops = sa_proto_legacy_format(list->fstype, share, 123025a68471Sdougm B_TRUE); 123125a68471Sdougm 123225a68471Sdougm if (oldprops == NULL) 123325a68471Sdougm continue; 123425a68471Sdougm 12356185db85Sdougm if (list->options != NULL && 123625a68471Sdougm strcmp(oldprops, list->options) != 0) { 123725a68471Sdougm sa_optionset_t opts; 123825a68471Sdougm sa_security_t secs; 123925a68471Sdougm 124025a68471Sdougm /* possibly different values */ 124125a68471Sdougm opts = sa_get_optionset((sa_group_t) 124225a68471Sdougm share, list->fstype); 124325a68471Sdougm (void) sa_destroy_optionset(opts); 124425a68471Sdougm 124525a68471Sdougm for (secs = sa_get_security( 124625a68471Sdougm (sa_group_t)share, NULL, list->fstype); 124725a68471Sdougm secs != NULL; 124825a68471Sdougm secs = sa_get_security((sa_group_t)share, 124925a68471Sdougm NULL, list->fstype)) { 125025a68471Sdougm (void) sa_destroy_security( 125125a68471Sdougm secs); 125225a68471Sdougm } 125325a68471Sdougm (void) sa_parse_legacy_options(share, 125425a68471Sdougm list->options, list->fstype); 12556185db85Sdougm } 1256a99982a7Sdougm sa_format_free(oldprops); 12576185db85Sdougm } 12586185db85Sdougm } 12596185db85Sdougm dfs_free_list(head); 12606185db85Sdougm } 12616185db85Sdougm 12626185db85Sdougm /* 12636185db85Sdougm * legacy_removes(group, file) 12646185db85Sdougm * 12656185db85Sdougm * Find any shares that are "missing" from the legacy file. These 12666185db85Sdougm * should be removed from the configuration since they are likely from 12676185db85Sdougm * a legacy app or the admin modified the dfstab file directly. We 12686185db85Sdougm * have to support this even if it is not the recommended way to do 12696185db85Sdougm * things. 12706185db85Sdougm */ 12716185db85Sdougm 12726185db85Sdougm static void 12736185db85Sdougm legacy_removes(sa_group_t group, char *file) 12746185db85Sdougm { 12756185db85Sdougm sa_share_t share; 12766185db85Sdougm char *path; 12776185db85Sdougm xfs_sharelist_t *list, *item; 12786185db85Sdougm FILE *dfstab; 12796185db85Sdougm 12806185db85Sdougm dfstab = fopen(file, "r"); 12816185db85Sdougm if (dfstab != NULL) { 128225a68471Sdougm list = getdfstab(dfstab); 128325a68471Sdougm (void) fclose(dfstab); 1284f345c0beSdougm retry: 128525a68471Sdougm for (share = sa_get_share(group, NULL); 128625a68471Sdougm share != NULL; 128725a68471Sdougm share = sa_get_next_share(share)) { 128825a68471Sdougm /* now see if the share is in the dfstab file */ 128925a68471Sdougm path = sa_get_share_attr(share, "path"); 129025a68471Sdougm if (path != NULL) { 129125a68471Sdougm item = finddfsentry(list, path); 129225a68471Sdougm sa_free_attr_string(path); 129325a68471Sdougm if (item == NULL) { 129425a68471Sdougm /* The share was removed this way */ 129525a68471Sdougm (void) sa_remove_share(share); 129625a68471Sdougm 129725a68471Sdougm /* 129825a68471Sdougm * Start over since the list was broken 129925a68471Sdougm */ 130025a68471Sdougm goto retry; 130125a68471Sdougm } 130225a68471Sdougm } 13036185db85Sdougm } 130425a68471Sdougm if (list != NULL) 130525a68471Sdougm dfs_free_list(list); 13066185db85Sdougm } 13076185db85Sdougm } 13086185db85Sdougm 13096185db85Sdougm /* 13106185db85Sdougm * getlegacyconfig(path, root) 13116185db85Sdougm * 13126185db85Sdougm * Parse dfstab and build the legacy configuration. This only gets 13136185db85Sdougm * called when a change was detected. 13146185db85Sdougm */ 13156185db85Sdougm 13166185db85Sdougm void 1317549ec3ffSdougm getlegacyconfig(sa_handle_t handle, char *path, xmlNodePtr *root) 13186185db85Sdougm { 13196185db85Sdougm sa_group_t defgroup; 13206185db85Sdougm 13216185db85Sdougm if (root != NULL) { 132225a68471Sdougm if (*root == NULL) 132325a68471Sdougm *root = xmlNewNode(NULL, (xmlChar *)"sharecfg"); 132425a68471Sdougm if (*root != NULL) { 132525a68471Sdougm if (strcmp(path, SA_LEGACY_DFSTAB) == 0) { 132625a68471Sdougm /* 132725a68471Sdougm * Walk the default shares and find anything 132825a68471Sdougm * missing. we do this first to make sure it 132925a68471Sdougm * is cleaned up since there may be legacy 133025a68471Sdougm * code add/del via dfstab and we need to 133125a68471Sdougm * cleanup SMF. 133225a68471Sdougm */ 133325a68471Sdougm defgroup = sa_get_group(handle, "default"); 133425a68471Sdougm if (defgroup != NULL) 133525a68471Sdougm legacy_removes(defgroup, path); 133625a68471Sdougm /* Parse the dfstab and add anything new */ 133725a68471Sdougm parse_dfstab(handle, path, *root); 133825a68471Sdougm } 13396185db85Sdougm } 13406185db85Sdougm } 13416185db85Sdougm } 13426185db85Sdougm 13431cea05afSdougm /* 13441cea05afSdougm * get_share_list(&err) 13451cea05afSdougm * 13461cea05afSdougm * Get a linked list of all the shares on the system from 13471cea05afSdougm * /etc/dfs/sharetab. This is partially copied from libfsmgt which we 13481cea05afSdougm * can't use due to package dependencies. 13491cea05afSdougm */ 13501cea05afSdougm static xfs_sharelist_t * 13511cea05afSdougm get_share_list(int *errp) 13521cea05afSdougm { 13531cea05afSdougm xfs_sharelist_t *newp; 13541cea05afSdougm xfs_sharelist_t *headp; 13551cea05afSdougm xfs_sharelist_t *tailp; 13561cea05afSdougm FILE *fp; 13571cea05afSdougm 13581cea05afSdougm headp = NULL; 13591cea05afSdougm tailp = NULL; 13601cea05afSdougm 13611cea05afSdougm if ((fp = fopen(SHARETAB, "r")) != NULL) { 13621cea05afSdougm struct share *sharetab_entry; 13631cea05afSdougm 1364*8d7e4166Sjose borrego (void) lockf(fileno(fp), F_LOCK, 0); 1365*8d7e4166Sjose borrego (void) mutex_lock(&sharetab_lock); 1366*8d7e4166Sjose borrego 13671cea05afSdougm while (getshare(fp, &sharetab_entry) > 0) { 136825a68471Sdougm newp = alloc_sharelist(); 1369*8d7e4166Sjose borrego if (newp == NULL) { 1370*8d7e4166Sjose borrego (void) mutex_unlock(&sharetab_lock); 1371*8d7e4166Sjose borrego (void) lockf(fileno(fp), F_ULOCK, 0); 137225a68471Sdougm goto err; 1373*8d7e4166Sjose borrego } 13741cea05afSdougm 13751cea05afSdougm /* 137625a68471Sdougm * Link into the list here so we don't leak 13771cea05afSdougm * memory on a failure from strdup(). 13781cea05afSdougm */ 137925a68471Sdougm if (headp == NULL) { 138025a68471Sdougm headp = newp; 138125a68471Sdougm tailp = newp; 138225a68471Sdougm } else { 138325a68471Sdougm tailp->next = newp; 138425a68471Sdougm tailp = newp; 138525a68471Sdougm } 138625a68471Sdougm 138725a68471Sdougm newp->path = strdup(sharetab_entry->sh_path); 138825a68471Sdougm newp->resource = strdup(sharetab_entry->sh_res); 138925a68471Sdougm newp->fstype = strdup(sharetab_entry->sh_fstype); 139025a68471Sdougm newp->options = strdup(sharetab_entry->sh_opts); 139125a68471Sdougm newp->description = strdup(sharetab_entry->sh_descr); 1392*8d7e4166Sjose borrego 1393*8d7e4166Sjose borrego if (newp->path == NULL || newp->resource == NULL || 1394*8d7e4166Sjose borrego newp->fstype == NULL || newp->options == NULL || 1395*8d7e4166Sjose borrego newp->description == NULL) { 1396*8d7e4166Sjose borrego (void) mutex_unlock(&sharetab_lock); 1397*8d7e4166Sjose borrego (void) lockf(fileno(fp), F_ULOCK, 0); 139825a68471Sdougm goto err; 1399*8d7e4166Sjose borrego } 14001cea05afSdougm } 1401*8d7e4166Sjose borrego 1402*8d7e4166Sjose borrego (void) mutex_unlock(&sharetab_lock); 1403a99982a7Sdougm (void) lockf(fileno(fp), F_ULOCK, 0); 14041cea05afSdougm (void) fclose(fp); 14051cea05afSdougm } else { 140625a68471Sdougm *errp = errno; 14071cea05afSdougm } 14081cea05afSdougm 14091cea05afSdougm /* 14101cea05afSdougm * Caller must free the mount list 14111cea05afSdougm */ 14121cea05afSdougm return (headp); 14131cea05afSdougm err: 14141cea05afSdougm /* 14151cea05afSdougm * Out of memory so cleanup and leave. 14161cea05afSdougm */ 14171cea05afSdougm dfs_free_list(headp); 14181cea05afSdougm (void) fclose(fp); 14191cea05afSdougm return (NULL); 14201cea05afSdougm } 14211cea05afSdougm 14226185db85Sdougm /* 1423549ec3ffSdougm * parse_sharetab(handle) 14246185db85Sdougm * 14251cea05afSdougm * Read the /etc/dfs/sharetab file and see which entries don't exist 14261cea05afSdougm * in the repository. These shares are marked transient. We also need 14271cea05afSdougm * to see if they are ZFS shares since ZFS bypasses the SMF 14281cea05afSdougm * repository. 14296185db85Sdougm */ 14306185db85Sdougm 14316185db85Sdougm int 1432549ec3ffSdougm parse_sharetab(sa_handle_t handle) 14336185db85Sdougm { 14341cea05afSdougm xfs_sharelist_t *list, *tmplist; 14356185db85Sdougm int err = 0; 14366185db85Sdougm sa_share_t share; 14376185db85Sdougm sa_group_t group; 14386185db85Sdougm sa_group_t lgroup; 14396185db85Sdougm char *groupname; 14406185db85Sdougm int legacy = 0; 1441da6c28aaSamw char shareopts[MAXNAMLEN]; 14426185db85Sdougm 14431cea05afSdougm list = get_share_list(&err); 14446185db85Sdougm if (list == NULL) 144525a68471Sdougm return (legacy); 14466185db85Sdougm 1447549ec3ffSdougm lgroup = sa_get_group(handle, "default"); 14486185db85Sdougm 14496185db85Sdougm for (tmplist = list; tmplist != NULL; tmplist = tmplist->next) { 145025a68471Sdougm group = NULL; 145125a68471Sdougm share = sa_find_share(handle, tmplist->path); 145225a68471Sdougm if (share != NULL) { 145325a68471Sdougm /* 145425a68471Sdougm * If this is a legacy share, mark as shared so we 145525a68471Sdougm * only update sharetab appropriately. We also keep 145625a68471Sdougm * the sharetab options in order to display for legacy 145725a68471Sdougm * share with no arguments. 145825a68471Sdougm */ 145925a68471Sdougm set_node_attr(share, "shared", "true"); 1460da6c28aaSamw (void) snprintf(shareopts, MAXNAMLEN, "shareopts-%s", 1461da6c28aaSamw tmplist->fstype); 1462da6c28aaSamw set_node_attr(share, shareopts, tmplist->options); 146325a68471Sdougm continue; 146425a68471Sdougm } 146525a68471Sdougm 14666185db85Sdougm /* 146725a68471Sdougm * This share is transient so needs to be 14686185db85Sdougm * added. Initially, this will be under 14696185db85Sdougm * default(legacy) unless it is a ZFS 14706185db85Sdougm * share. If zfs, we need a zfs group. 14716185db85Sdougm */ 14726185db85Sdougm if (tmplist->resource != NULL && 14736185db85Sdougm (groupname = strchr(tmplist->resource, '@')) != NULL) { 147425a68471Sdougm /* There is a defined group */ 147525a68471Sdougm *groupname++ = '\0'; 147625a68471Sdougm group = sa_get_group(handle, groupname); 14776185db85Sdougm if (group != NULL) { 1478da6c28aaSamw share = _sa_add_share(group, tmplist->path, 1479da6c28aaSamw SA_SHARE_TRANSIENT, &err, 1480da6c28aaSamw (uint64_t)SA_FEATURE_NONE); 148125a68471Sdougm } else { 148225a68471Sdougm /* 148325a68471Sdougm * While this case shouldn't 148425a68471Sdougm * occur very often, it does 148525a68471Sdougm * occur out of a "zfs set 148625a68471Sdougm * sharenfs=off" when the 148725a68471Sdougm * dataset is also set to 148825a68471Sdougm * canmount=off. A warning 148925a68471Sdougm * will then cause the zfs 149025a68471Sdougm * command to abort. Since we 149125a68471Sdougm * add it to the default list, 149225a68471Sdougm * everything works properly 149325a68471Sdougm * anyway and the library 149425a68471Sdougm * doesn't need to give a 149525a68471Sdougm * warning. 149625a68471Sdougm */ 149725a68471Sdougm share = _sa_add_share(lgroup, 149825a68471Sdougm tmplist->path, SA_SHARE_TRANSIENT, 1499da6c28aaSamw &err, (uint64_t)SA_FEATURE_NONE); 150025a68471Sdougm } 150125a68471Sdougm } else { 150225a68471Sdougm if (sa_zfs_is_shared(handle, tmplist->path)) { 150325a68471Sdougm group = sa_get_group(handle, "zfs"); 150425a68471Sdougm if (group == NULL) { 150525a68471Sdougm group = sa_create_group(handle, 150625a68471Sdougm "zfs", &err); 150725a68471Sdougm if (group == NULL && 150825a68471Sdougm err == SA_NO_PERMISSION) { 150925a68471Sdougm group = _sa_create_group( 151025a68471Sdougm (sa_handle_impl_t) 151125a68471Sdougm handle, 151225a68471Sdougm "zfs"); 151325a68471Sdougm } 151425a68471Sdougm if (group != NULL) { 151525a68471Sdougm (void) sa_create_optionset( 151625a68471Sdougm group, tmplist->fstype); 151725a68471Sdougm (void) sa_set_group_attr(group, 151825a68471Sdougm "zfs", "true"); 151925a68471Sdougm } 152025a68471Sdougm } 152125a68471Sdougm if (group != NULL) { 152225a68471Sdougm share = _sa_add_share(group, 152325a68471Sdougm tmplist->path, SA_SHARE_TRANSIENT, 1524da6c28aaSamw &err, (uint64_t)SA_FEATURE_NONE); 152525a68471Sdougm } 152625a68471Sdougm } else { 152725a68471Sdougm share = _sa_add_share(lgroup, tmplist->path, 1528da6c28aaSamw SA_SHARE_TRANSIENT, &err, 1529da6c28aaSamw (uint64_t)SA_FEATURE_NONE); 15306185db85Sdougm } 15316185db85Sdougm } 15326185db85Sdougm if (share == NULL) 153325a68471Sdougm (void) printf(dgettext(TEXT_DOMAIN, 153425a68471Sdougm "Problem with transient: %s\n"), sa_errorstr(err)); 15356185db85Sdougm if (share != NULL) 153625a68471Sdougm set_node_attr(share, "shared", "true"); 15376185db85Sdougm if (err == SA_OK) { 153825a68471Sdougm if (tmplist->options != NULL && 153925a68471Sdougm strlen(tmplist->options) > 0) { 154025a68471Sdougm (void) sa_parse_legacy_options(share, 154125a68471Sdougm tmplist->options, tmplist->fstype); 154225a68471Sdougm } 154325a68471Sdougm if (tmplist->resource != NULL && 154425a68471Sdougm strcmp(tmplist->resource, "-") != 0) 154525a68471Sdougm set_node_attr(share, "resource", 154625a68471Sdougm tmplist->resource); 154725a68471Sdougm if (tmplist->description != NULL) { 154825a68471Sdougm xmlNodePtr node; 154925a68471Sdougm node = xmlNewChild((xmlNodePtr)share, NULL, 155025a68471Sdougm (xmlChar *)"description", NULL); 155125a68471Sdougm xmlNodeSetContent(node, 155225a68471Sdougm (xmlChar *)tmplist->description); 155325a68471Sdougm } 155425a68471Sdougm legacy = 1; 15556185db85Sdougm } 15566185db85Sdougm } 15571cea05afSdougm dfs_free_list(list); 15586185db85Sdougm return (legacy); 15596185db85Sdougm } 15606185db85Sdougm 15616185db85Sdougm /* 156225a68471Sdougm * Get the transient shares from the sharetab (or other) file. since 15636185db85Sdougm * these are transient, they only appear in the working file and not 15646185db85Sdougm * in a repository. 15656185db85Sdougm */ 15666185db85Sdougm int 1567549ec3ffSdougm gettransients(sa_handle_impl_t ihandle, xmlNodePtr *root) 15686185db85Sdougm { 15696185db85Sdougm int legacy = 0; 1570da6c28aaSamw int numproto; 1571da6c28aaSamw char **protocols = NULL; 1572da6c28aaSamw int i; 15736185db85Sdougm 15746185db85Sdougm if (root != NULL) { 157525a68471Sdougm if (*root == NULL) 157625a68471Sdougm *root = xmlNewNode(NULL, (xmlChar *)"sharecfg"); 1577da6c28aaSamw if (*root != NULL) { 157825a68471Sdougm legacy = parse_sharetab(ihandle); 1579da6c28aaSamw numproto = sa_get_protocols(&protocols); 1580da6c28aaSamw for (i = 0; i < numproto; i++) 1581da6c28aaSamw legacy |= sa_proto_get_transients( 1582da6c28aaSamw (sa_handle_t)ihandle, protocols[i]); 1583da6c28aaSamw if (protocols != NULL) 1584da6c28aaSamw free(protocols); 1585da6c28aaSamw } 15866185db85Sdougm } 15876185db85Sdougm return (legacy); 15886185db85Sdougm } 15896185db85Sdougm 15906185db85Sdougm /* 15916185db85Sdougm * sa_has_prop(optionset, prop) 15926185db85Sdougm * 15936185db85Sdougm * Is the specified property a member of the optionset? 15946185db85Sdougm */ 15956185db85Sdougm 15966185db85Sdougm int 15976185db85Sdougm sa_has_prop(sa_optionset_t optionset, sa_property_t prop) 15986185db85Sdougm { 15996185db85Sdougm char *name; 16006185db85Sdougm sa_property_t otherprop; 16016185db85Sdougm int result = 0; 16026185db85Sdougm 16036185db85Sdougm if (optionset != NULL) { 160425a68471Sdougm name = sa_get_property_attr(prop, "type"); 160525a68471Sdougm if (name != NULL) { 160625a68471Sdougm otherprop = sa_get_property(optionset, name); 160725a68471Sdougm if (otherprop != NULL) 160825a68471Sdougm result = 1; 160925a68471Sdougm sa_free_attr_string(name); 161025a68471Sdougm } 16116185db85Sdougm } 16126185db85Sdougm return (result); 16136185db85Sdougm } 16146185db85Sdougm 16156185db85Sdougm /* 16166185db85Sdougm * Update legacy files 16176185db85Sdougm * 16186185db85Sdougm * Provides functions to add/remove/modify individual entries 16196185db85Sdougm * in dfstab and sharetab 16206185db85Sdougm */ 16216185db85Sdougm 16226185db85Sdougm void 1623549ec3ffSdougm update_legacy_config(sa_handle_t handle) 16246185db85Sdougm { 16256185db85Sdougm /* 16266185db85Sdougm * no longer used -- this is a placeholder in case we need to 16276185db85Sdougm * add it back later. 16286185db85Sdougm */ 1629549ec3ffSdougm #ifdef lint 1630549ec3ffSdougm handle = handle; 1631549ec3ffSdougm #endif 16326185db85Sdougm } 16336185db85Sdougm 16346185db85Sdougm /* 1635687915e9Sdougm * sa_valid_property(handle, object, proto, property) 16366185db85Sdougm * 16376185db85Sdougm * check to see if the specified property is valid relative to the 16386185db85Sdougm * specified protocol. The protocol plugin is called to do the work. 16396185db85Sdougm */ 16406185db85Sdougm 16416185db85Sdougm int 1642687915e9Sdougm sa_valid_property(sa_handle_t handle, void *object, char *proto, 1643687915e9Sdougm sa_property_t property) 16446185db85Sdougm { 16456185db85Sdougm int ret = SA_OK; 16466185db85Sdougm 16476185db85Sdougm if (proto != NULL && property != NULL) { 1648687915e9Sdougm ret = sa_proto_valid_prop(handle, proto, property, object); 16496185db85Sdougm } 16506185db85Sdougm 16516185db85Sdougm return (ret); 16526185db85Sdougm } 16536185db85Sdougm 16546185db85Sdougm /* 16556185db85Sdougm * sa_fstype(path) 16566185db85Sdougm * 16576185db85Sdougm * Given path, return the string representing the path's file system 16586185db85Sdougm * type. This is used to discover ZFS shares. 16596185db85Sdougm */ 16606185db85Sdougm 16616185db85Sdougm char * 16626185db85Sdougm sa_fstype(char *path) 16636185db85Sdougm { 16646185db85Sdougm int err; 16656185db85Sdougm struct stat st; 16666185db85Sdougm 16676185db85Sdougm err = stat(path, &st); 166825a68471Sdougm if (err < 0) 166925a68471Sdougm err = SA_NO_SUCH_PATH; 167025a68471Sdougm else 167125a68471Sdougm err = SA_OK; 167225a68471Sdougm 167325a68471Sdougm /* 167425a68471Sdougm * If we have a valid path at this point ret, return the fstype. 167525a68471Sdougm */ 167625a68471Sdougm if (err == SA_OK) 167725a68471Sdougm return (strdup(st.st_fstype)); 167825a68471Sdougm 16796185db85Sdougm return (NULL); 16806185db85Sdougm } 16816185db85Sdougm 16826185db85Sdougm void 16836185db85Sdougm sa_free_fstype(char *type) 16846185db85Sdougm { 16856185db85Sdougm free(type); 16866185db85Sdougm } 16876185db85Sdougm 16886185db85Sdougm /* 16896185db85Sdougm * sa_get_derived_optionset(object, proto, hier) 16906185db85Sdougm * 16916185db85Sdougm * Work backward to the top of the share object tree and start 16926185db85Sdougm * copying protocol specific optionsets into a newly created 16936185db85Sdougm * optionset that doesn't have a parent (it will be freed 1694da6c28aaSamw * later). This provides for the property inheritance model. That 16956185db85Sdougm * is, properties closer to the share take precedence over group 16966185db85Sdougm * level. This also provides for groups of groups in the future. 16976185db85Sdougm */ 16986185db85Sdougm 16996185db85Sdougm sa_optionset_t 17006185db85Sdougm sa_get_derived_optionset(void *object, char *proto, int hier) 17016185db85Sdougm { 17026185db85Sdougm sa_optionset_t newoptionset; 17036185db85Sdougm sa_optionset_t optionset; 17046185db85Sdougm sa_group_t group; 17056185db85Sdougm 17066185db85Sdougm if (hier && 17076185db85Sdougm (group = sa_get_parent_group((sa_share_t)object)) != NULL) { 170825a68471Sdougm newoptionset = sa_get_derived_optionset((void *)group, proto, 170925a68471Sdougm hier); 17106185db85Sdougm } else { 171125a68471Sdougm newoptionset = (sa_optionset_t)xmlNewNode(NULL, 171225a68471Sdougm (xmlChar *)"optionset"); 171325a68471Sdougm if (newoptionset != NULL) { 171425a68471Sdougm sa_set_optionset_attr(newoptionset, "type", proto); 171525a68471Sdougm } 17166185db85Sdougm } 171725a68471Sdougm /* Dont' do anything if memory wasn't allocated */ 17186185db85Sdougm if (newoptionset == NULL) 171925a68471Sdougm return (NULL); 17206185db85Sdougm 172125a68471Sdougm /* Found the top so working back down the stack */ 17226185db85Sdougm optionset = sa_get_optionset((sa_optionset_t)object, proto); 17236185db85Sdougm if (optionset != NULL) { 172425a68471Sdougm sa_property_t prop; 172525a68471Sdougm /* add optionset to the newoptionset */ 172625a68471Sdougm for (prop = sa_get_property(optionset, NULL); 172725a68471Sdougm prop != NULL; 172825a68471Sdougm prop = sa_get_next_property(prop)) { 172925a68471Sdougm sa_property_t newprop; 173025a68471Sdougm char *name; 173125a68471Sdougm char *value; 173225a68471Sdougm name = sa_get_property_attr(prop, "type"); 173325a68471Sdougm value = sa_get_property_attr(prop, "value"); 173425a68471Sdougm if (name == NULL) 173525a68471Sdougm continue; 173625a68471Sdougm newprop = sa_get_property(newoptionset, name); 173725a68471Sdougm /* Replace the value with the new value */ 173825a68471Sdougm if (newprop != NULL) { 173925a68471Sdougm /* 174025a68471Sdougm * Only set if value is non NULL, old value ok 174125a68471Sdougm * if it is NULL. 174225a68471Sdougm */ 174325a68471Sdougm if (value != NULL) 174425a68471Sdougm set_node_attr(newprop, "value", value); 174525a68471Sdougm } else { 174625a68471Sdougm /* an entirely new property */ 174725a68471Sdougm if (value != NULL) { 174825a68471Sdougm newprop = sa_create_property(name, 174925a68471Sdougm value); 175025a68471Sdougm if (newprop != NULL) { 175125a68471Sdougm newprop = (sa_property_t) 175225a68471Sdougm xmlAddChild( 175325a68471Sdougm (xmlNodePtr)newoptionset, 175425a68471Sdougm (xmlNodePtr)newprop); 175525a68471Sdougm } 175625a68471Sdougm } 17576185db85Sdougm } 175825a68471Sdougm sa_free_attr_string(name); 175925a68471Sdougm 176025a68471Sdougm if (value != NULL) 176125a68471Sdougm sa_free_attr_string(value); 17626185db85Sdougm } 17636185db85Sdougm } 17646185db85Sdougm return (newoptionset); 17656185db85Sdougm } 17666185db85Sdougm 17676185db85Sdougm void 17686185db85Sdougm sa_free_derived_optionset(sa_optionset_t optionset) 17696185db85Sdougm { 177025a68471Sdougm /* While it shouldn't be linked, it doesn't hurt */ 17716185db85Sdougm if (optionset != NULL) { 177225a68471Sdougm xmlUnlinkNode((xmlNodePtr) optionset); 177325a68471Sdougm xmlFreeNode((xmlNodePtr) optionset); 17746185db85Sdougm } 17756185db85Sdougm } 17766185db85Sdougm 17776185db85Sdougm /* 17786185db85Sdougm * sa_get_all_security_types(object, proto, hier) 17796185db85Sdougm * 178025a68471Sdougm * Find all the security types set for this object. This is 17816185db85Sdougm * preliminary to getting a derived security set. The return value is an 17826185db85Sdougm * optionset containg properties which are the sectype values found by 1783da6c28aaSamw * walking up the XML document structure. The returned optionset 17846185db85Sdougm * is a derived optionset. 17856185db85Sdougm * 17866185db85Sdougm * If hier is 0, only look at object. If non-zero, walk up the tree. 17876185db85Sdougm */ 17886185db85Sdougm sa_optionset_t 17896185db85Sdougm sa_get_all_security_types(void *object, char *proto, int hier) 17906185db85Sdougm { 17916185db85Sdougm sa_optionset_t options; 17926185db85Sdougm sa_security_t security; 17936185db85Sdougm sa_group_t group; 17946185db85Sdougm sa_property_t prop; 17956185db85Sdougm 17966185db85Sdougm options = NULL; 17976185db85Sdougm 17986185db85Sdougm if (hier && 179925a68471Sdougm (group = sa_get_parent_group((sa_share_t)object)) != NULL) 180025a68471Sdougm options = sa_get_all_security_types((void *)group, proto, hier); 180125a68471Sdougm else 180225a68471Sdougm options = (sa_optionset_t)xmlNewNode(NULL, 180325a68471Sdougm (xmlChar *)"optionset"); 180425a68471Sdougm 180525a68471Sdougm if (options == NULL) 180625a68471Sdougm return (options); 180725a68471Sdougm 180825a68471Sdougm /* Hit the top so collect the security types working back. */ 180925a68471Sdougm for (security = sa_get_security((sa_group_t)object, NULL, NULL); 181025a68471Sdougm security != NULL; 181125a68471Sdougm security = sa_get_next_security(security)) { 18126185db85Sdougm char *type; 18136185db85Sdougm char *sectype; 18146185db85Sdougm 18156185db85Sdougm type = sa_get_security_attr(security, "type"); 18166185db85Sdougm if (type != NULL) { 181725a68471Sdougm if (strcmp(type, proto) != 0) { 181825a68471Sdougm sa_free_attr_string(type); 181925a68471Sdougm continue; 182025a68471Sdougm } 182125a68471Sdougm sectype = sa_get_security_attr(security, "sectype"); 182225a68471Sdougm if (sectype != NULL) { 182325a68471Sdougm /* 182425a68471Sdougm * Have a security type, check to see if 182525a68471Sdougm * already present in optionset and add if it 182625a68471Sdougm * isn't. 182725a68471Sdougm */ 182825a68471Sdougm if (sa_get_property(options, sectype) == NULL) { 182925a68471Sdougm prop = sa_create_property(sectype, 183025a68471Sdougm "true"); 183125a68471Sdougm if (prop != NULL) 183225a68471Sdougm prop = (sa_property_t) 183325a68471Sdougm xmlAddChild( 183425a68471Sdougm (xmlNodePtr)options, 183525a68471Sdougm (xmlNodePtr)prop); 183625a68471Sdougm } 183725a68471Sdougm sa_free_attr_string(sectype); 18386185db85Sdougm } 183925a68471Sdougm sa_free_attr_string(type); 18406185db85Sdougm } 18416185db85Sdougm } 184225a68471Sdougm 18436185db85Sdougm return (options); 18446185db85Sdougm } 18456185db85Sdougm 18466185db85Sdougm /* 18476185db85Sdougm * sa_get_derived_security(object, sectype, proto, hier) 18486185db85Sdougm * 18496185db85Sdougm * Get the derived security(named optionset) for the object given the 18506185db85Sdougm * sectype and proto. If hier is non-zero, walk up the tree to get all 18516185db85Sdougm * properties defined for this object, otherwise just those on the 18526185db85Sdougm * object. 18536185db85Sdougm */ 18546185db85Sdougm 18556185db85Sdougm sa_security_t 18566185db85Sdougm sa_get_derived_security(void *object, char *sectype, char *proto, int hier) 18576185db85Sdougm { 18586185db85Sdougm sa_security_t newsecurity; 18596185db85Sdougm sa_security_t security; 18606185db85Sdougm sa_group_t group; 186125a68471Sdougm sa_property_t prop; 18626185db85Sdougm 18636185db85Sdougm if (hier && 18646185db85Sdougm (group = sa_get_parent_group((sa_share_t)object)) != NULL) { 186525a68471Sdougm newsecurity = sa_get_derived_security((void *)group, 186625a68471Sdougm sectype, proto, hier); 18676185db85Sdougm } else { 186825a68471Sdougm newsecurity = (sa_security_t)xmlNewNode(NULL, 186925a68471Sdougm (xmlChar *)"security"); 187025a68471Sdougm if (newsecurity != NULL) { 187125a68471Sdougm sa_set_security_attr(newsecurity, "type", proto); 187225a68471Sdougm sa_set_security_attr(newsecurity, "sectype", sectype); 187325a68471Sdougm } 18746185db85Sdougm } 187525a68471Sdougm /* Don't do anything if memory wasn't allocated */ 18766185db85Sdougm if (newsecurity == NULL) 187725a68471Sdougm return (newsecurity); 18786185db85Sdougm 187925a68471Sdougm /* Found the top so working back down the stack. */ 18806185db85Sdougm security = sa_get_security((sa_security_t)object, sectype, proto); 188125a68471Sdougm if (security == NULL) 188225a68471Sdougm return (newsecurity); 188325a68471Sdougm 188425a68471Sdougm /* add security to the newsecurity */ 188525a68471Sdougm for (prop = sa_get_property(security, NULL); 188625a68471Sdougm prop != NULL; prop = sa_get_next_property(prop)) { 18876185db85Sdougm sa_property_t newprop; 18886185db85Sdougm char *name; 18896185db85Sdougm char *value; 18906185db85Sdougm name = sa_get_property_attr(prop, "type"); 18916185db85Sdougm value = sa_get_property_attr(prop, "value"); 18926185db85Sdougm if (name != NULL) { 189325a68471Sdougm newprop = sa_get_property(newsecurity, name); 189425a68471Sdougm /* Replace the value with the new value */ 189525a68471Sdougm if (newprop != NULL) { 189625a68471Sdougm /* 189797df5ac9Sdougm * Only set if value is non NULL, old 189897df5ac9Sdougm * value ok if it is NULL. The value 189997df5ac9Sdougm * must be associated with the "value" 190097df5ac9Sdougm * tag within XML. 190125a68471Sdougm */ 190225a68471Sdougm if (value != NULL) 190397df5ac9Sdougm set_node_attr(newprop, "value", value); 190425a68471Sdougm } else { 190525a68471Sdougm /* An entirely new property */ 190625a68471Sdougm if (value != NULL) { 190725a68471Sdougm newprop = sa_create_property(name, 190825a68471Sdougm value); 190925a68471Sdougm newprop = (sa_property_t) 191025a68471Sdougm xmlAddChild((xmlNodePtr)newsecurity, 19116185db85Sdougm (xmlNodePtr)newprop); 191225a68471Sdougm } 19136185db85Sdougm } 191425a68471Sdougm sa_free_attr_string(name); 19156185db85Sdougm } 19166185db85Sdougm if (value != NULL) 191725a68471Sdougm sa_free_attr_string(value); 19186185db85Sdougm } 19196185db85Sdougm return (newsecurity); 19206185db85Sdougm } 19216185db85Sdougm 19226185db85Sdougm void 19236185db85Sdougm sa_free_derived_security(sa_security_t security) 19246185db85Sdougm { 19256185db85Sdougm /* while it shouldn't be linked, it doesn't hurt */ 19266185db85Sdougm if (security != NULL) { 192725a68471Sdougm xmlUnlinkNode((xmlNodePtr)security); 192825a68471Sdougm xmlFreeNode((xmlNodePtr)security); 19296185db85Sdougm } 19306185db85Sdougm } 19316185db85Sdougm 19326185db85Sdougm /* 19336185db85Sdougm * sharetab utility functions 19346185db85Sdougm * 193525a68471Sdougm * Makes use of the original sharetab.c from fs.d/nfs/lib 19366185db85Sdougm */ 19376185db85Sdougm 19386185db85Sdougm /* 1939ecd6cf80Smarks * sa_fillshare(share, proto, sh) 19406185db85Sdougm * 19416185db85Sdougm * Fill the struct share with values obtained from the share object. 19426185db85Sdougm */ 1943ecd6cf80Smarks void 1944ecd6cf80Smarks sa_fillshare(sa_share_t share, char *proto, struct share *sh) 19456185db85Sdougm { 19466185db85Sdougm char *groupname = NULL; 19476185db85Sdougm char *value; 19486185db85Sdougm sa_group_t group; 19496185db85Sdougm char *buff; 19506185db85Sdougm char *zfs; 1951da6c28aaSamw sa_resource_t resource; 1952da6c28aaSamw char *rsrcname = NULL; 1953da6c28aaSamw char *defprop; 1954da6c28aaSamw 1955da6c28aaSamw /* 1956da6c28aaSamw * We only want to deal with the path level shares for the 1957da6c28aaSamw * sharetab file. If a resource, get the parent. 1958da6c28aaSamw */ 1959da6c28aaSamw if (sa_is_resource(share)) { 1960da6c28aaSamw resource = (sa_resource_t)share; 1961da6c28aaSamw share = sa_get_resource_parent(resource); 1962da6c28aaSamw rsrcname = sa_get_resource_attr(resource, "name"); 1963da6c28aaSamw } 19646185db85Sdougm 19656185db85Sdougm group = sa_get_parent_group(share); 19666185db85Sdougm if (group != NULL) { 196725a68471Sdougm zfs = sa_get_group_attr(group, "zfs"); 196825a68471Sdougm groupname = sa_get_group_attr(group, "name"); 19696185db85Sdougm 197025a68471Sdougm if (groupname != NULL && 197125a68471Sdougm (strcmp(groupname, "default") == 0 || zfs != NULL)) { 197225a68471Sdougm /* 197325a68471Sdougm * since the groupname is either "default" or the 197425a68471Sdougm * group is a ZFS group, we don't want to keep 197525a68471Sdougm * groupname. We do want it if it is any other type of 197625a68471Sdougm * group. 197725a68471Sdougm */ 197825a68471Sdougm sa_free_attr_string(groupname); 197925a68471Sdougm groupname = NULL; 198025a68471Sdougm } 198125a68471Sdougm if (zfs != NULL) 198225a68471Sdougm sa_free_attr_string(zfs); 19836185db85Sdougm } 19846185db85Sdougm 19856185db85Sdougm value = sa_get_share_attr(share, "path"); 19866185db85Sdougm if (value != NULL) { 198725a68471Sdougm sh->sh_path = strdup(value); 198825a68471Sdougm sa_free_attr_string(value); 19896185db85Sdougm } 19906185db85Sdougm 1991da6c28aaSamw if (rsrcname != NULL || groupname != NULL) { 199225a68471Sdougm int len = 0; 199325a68471Sdougm 1994da6c28aaSamw if (rsrcname != NULL) 1995da6c28aaSamw len += strlen(rsrcname); 199625a68471Sdougm if (groupname != NULL) 199725a68471Sdougm len += strlen(groupname); 199825a68471Sdougm len += 3; /* worst case */ 199925a68471Sdougm buff = malloc(len); 200025a68471Sdougm (void) snprintf(buff, len, "%s%s%s", 2001da6c28aaSamw (rsrcname != NULL && 2002da6c28aaSamw strlen(rsrcname) > 0) ? rsrcname : "-", 20036185db85Sdougm groupname != NULL ? "@" : "", 20046185db85Sdougm groupname != NULL ? groupname : ""); 200525a68471Sdougm sh->sh_res = buff; 2006da6c28aaSamw if (rsrcname != NULL) 2007da6c28aaSamw sa_free_attr_string(rsrcname); 2008da6c28aaSamw if (groupname != NULL) 200925a68471Sdougm sa_free_attr_string(groupname); 20106185db85Sdougm } else { 201125a68471Sdougm sh->sh_res = strdup("-"); 20126185db85Sdougm } 20136185db85Sdougm 2014da6c28aaSamw /* 2015da6c28aaSamw * Get correct default prop string. NFS uses "rw", others use 2016da6c28aaSamw * "". 2017da6c28aaSamw */ 2018da6c28aaSamw if (strcmp(proto, "nfs") != 0) 2019da6c28aaSamw defprop = "\"\""; 2020da6c28aaSamw else 2021da6c28aaSamw defprop = "rw"; 2022da6c28aaSamw 20236185db85Sdougm sh->sh_fstype = strdup(proto); 20246185db85Sdougm value = sa_proto_legacy_format(proto, share, 1); 20256185db85Sdougm if (value != NULL) { 202625a68471Sdougm if (strlen(value) > 0) 202725a68471Sdougm sh->sh_opts = strdup(value); 202825a68471Sdougm else 2029da6c28aaSamw sh->sh_opts = strdup(defprop); 203025a68471Sdougm free(value); 203125a68471Sdougm } else { 2032da6c28aaSamw sh->sh_opts = strdup(defprop); 203325a68471Sdougm } 20346185db85Sdougm 20356185db85Sdougm value = sa_get_share_description(share); 20366185db85Sdougm if (value != NULL) { 203725a68471Sdougm sh->sh_descr = strdup(value); 203825a68471Sdougm sa_free_share_description(value); 203925a68471Sdougm } else { 204025a68471Sdougm sh->sh_descr = strdup(""); 204125a68471Sdougm } 20426185db85Sdougm } 20436185db85Sdougm 20446185db85Sdougm /* 2045ecd6cf80Smarks * sa_emptyshare(sh) 20466185db85Sdougm * 20476185db85Sdougm * Free the strings in the non-NULL members of sh. 20486185db85Sdougm */ 20496185db85Sdougm 2050ecd6cf80Smarks void 2051ecd6cf80Smarks sa_emptyshare(struct share *sh) 20526185db85Sdougm { 20536185db85Sdougm if (sh->sh_path != NULL) 205425a68471Sdougm free(sh->sh_path); 20556185db85Sdougm sh->sh_path = NULL; 20566185db85Sdougm if (sh->sh_res != NULL) 205725a68471Sdougm free(sh->sh_res); 20586185db85Sdougm sh->sh_res = NULL; 20596185db85Sdougm if (sh->sh_fstype != NULL) 206025a68471Sdougm free(sh->sh_fstype); 20616185db85Sdougm sh->sh_fstype = NULL; 20626185db85Sdougm if (sh->sh_opts != NULL) 206325a68471Sdougm free(sh->sh_opts); 20646185db85Sdougm sh->sh_opts = NULL; 20656185db85Sdougm if (sh->sh_descr != NULL) 206625a68471Sdougm free(sh->sh_descr); 20676185db85Sdougm sh->sh_descr = NULL; 20686185db85Sdougm } 20696185db85Sdougm 20705b6e0c46Sdougm /* 20715b6e0c46Sdougm * sa_update_sharetab_ts(handle) 20725b6e0c46Sdougm * 20735b6e0c46Sdougm * Update the internal timestamp of when sharetab was last 20745b6e0c46Sdougm * changed. This needs to be public for ZFS to get at it. 20755b6e0c46Sdougm */ 20765b6e0c46Sdougm 20775b6e0c46Sdougm void 20785b6e0c46Sdougm sa_update_sharetab_ts(sa_handle_t handle) 20795b6e0c46Sdougm { 20805b6e0c46Sdougm struct stat st; 20815b6e0c46Sdougm sa_handle_impl_t implhandle = (sa_handle_impl_t)handle; 20825b6e0c46Sdougm 20835b6e0c46Sdougm if (implhandle != NULL && stat(SA_LEGACY_SHARETAB, &st) == 0) 20845b6e0c46Sdougm implhandle->tssharetab = TSTAMP(st.st_mtim); 20855b6e0c46Sdougm } 20865b6e0c46Sdougm 20876185db85Sdougm /* 20886185db85Sdougm * sa_update_sharetab(share, proto) 20896185db85Sdougm * 20906185db85Sdougm * Update the sharetab file with info from the specified share. 20916185db85Sdougm * This could be an update or add. 20926185db85Sdougm */ 20936185db85Sdougm 20946185db85Sdougm int 20956185db85Sdougm sa_update_sharetab(sa_share_t share, char *proto) 20966185db85Sdougm { 2097a237e38eSth int ret = SA_OK; 2098a237e38eSth share_t sh; 2099a237e38eSth char *path; 21005b6e0c46Sdougm sa_handle_t handle; 21016185db85Sdougm 21026185db85Sdougm path = sa_get_share_attr(share, "path"); 21036185db85Sdougm if (path != NULL) { 2104a237e38eSth (void) memset(&sh, '\0', sizeof (sh)); 2105a237e38eSth 21065b6e0c46Sdougm handle = sa_find_group_handle((sa_group_t)share); 21075b6e0c46Sdougm if (handle != NULL) { 21085b6e0c46Sdougm /* 21095b6e0c46Sdougm * Fill in share structure and send it to the kernel. 21105b6e0c46Sdougm */ 21115b6e0c46Sdougm (void) sa_fillshare(share, proto, &sh); 21125b6e0c46Sdougm (void) _sharefs(SHAREFS_ADD, &sh); 21135b6e0c46Sdougm /* 21145b6e0c46Sdougm * We need the timestamp of the sharetab file right 21155b6e0c46Sdougm * after the update was done. This lets us detect a 21165b6e0c46Sdougm * change that made by a different process. 21175b6e0c46Sdougm */ 21185b6e0c46Sdougm sa_update_sharetab_ts(handle); 21195b6e0c46Sdougm sa_emptyshare(&sh); 21205b6e0c46Sdougm } else { 21215b6e0c46Sdougm ret = SA_CONFIG_ERR; 21225b6e0c46Sdougm } 2123a237e38eSth sa_free_attr_string(path); 21246185db85Sdougm } 2125a237e38eSth 21266185db85Sdougm return (ret); 21276185db85Sdougm } 21286185db85Sdougm 21296185db85Sdougm /* 21305b6e0c46Sdougm * sa_delete_sharetab(handle, path, proto) 21316185db85Sdougm * 21326185db85Sdougm * remove the specified share from sharetab. 21336185db85Sdougm */ 21346185db85Sdougm 21356185db85Sdougm int 21365b6e0c46Sdougm sa_delete_sharetab(sa_handle_t handle, char *path, char *proto) 21376185db85Sdougm { 2138a237e38eSth int ret = SA_OK; 21395b6e0c46Sdougm struct stat st; 21406185db85Sdougm 2141a237e38eSth share_t sh; 2142a237e38eSth /* 2143a237e38eSth * Both the path and the proto are 2144a237e38eSth * keys into the sharetab. 2145a237e38eSth */ 2146a237e38eSth if (path != NULL && proto != NULL) { 2147a237e38eSth (void) memset(&sh, '\0', sizeof (sh)); 2148a237e38eSth sh.sh_path = path; 2149a237e38eSth sh.sh_fstype = proto; 2150a237e38eSth 2151a3175730Sth ret = _sharefs(SHAREFS_REMOVE, &sh); 21525b6e0c46Sdougm if (handle != NULL && stat(SA_LEGACY_SHARETAB, &st) == 0) 21535b6e0c46Sdougm sa_update_sharetab_ts(handle); 21546185db85Sdougm } 21556185db85Sdougm return (ret); 21566185db85Sdougm } 21575b6e0c46Sdougm 21585b6e0c46Sdougm /* 21595b6e0c46Sdougm * sa_needs_refresh(handle) 21605b6e0c46Sdougm * 21615b6e0c46Sdougm * Returns B_TRUE if the internal cache needs to be refreshed do to a 21625b6e0c46Sdougm * change by another process. B_FALSE returned otherwise. 21635b6e0c46Sdougm */ 21645b6e0c46Sdougm boolean_t 2165b83b3cacSdougm sa_needs_refresh(sa_handle_t handle) 21665b6e0c46Sdougm { 21675b6e0c46Sdougm sa_handle_impl_t implhandle = (sa_handle_impl_t)handle; 21685b6e0c46Sdougm struct stat st; 21695b6e0c46Sdougm char *str; 21705b6e0c46Sdougm uint64_t tstamp; 21715b6e0c46Sdougm scf_simple_prop_t *prop; 21725b6e0c46Sdougm 21735b6e0c46Sdougm if (handle == NULL) 21745b6e0c46Sdougm return (B_TRUE); 21755b6e0c46Sdougm 21765b6e0c46Sdougm /* 21775b6e0c46Sdougm * If sharetab has changed, then there was an external 21785b6e0c46Sdougm * change. Check sharetab first since it is updated by ZFS as 21795b6e0c46Sdougm * well as sharemgr. This is where external ZFS changes are 21805b6e0c46Sdougm * caught. 21815b6e0c46Sdougm */ 21825b6e0c46Sdougm if (stat(SA_LEGACY_SHARETAB, &st) == 0 && 21835b6e0c46Sdougm TSTAMP(st.st_mtim) != implhandle->tssharetab) 21845b6e0c46Sdougm return (B_TRUE); 21855b6e0c46Sdougm 21865b6e0c46Sdougm /* 21875b6e0c46Sdougm * If sharetab wasn't changed, check whether there were any 21885b6e0c46Sdougm * SMF transactions that modified the config but didn't 21895b6e0c46Sdougm * initiate a share. This is less common but does happen. 21905b6e0c46Sdougm */ 21915b6e0c46Sdougm prop = scf_simple_prop_get(implhandle->scfhandle->handle, 21925b6e0c46Sdougm (const char *)SA_SVC_FMRI_BASE ":default", "state", 21935b6e0c46Sdougm "lastupdate"); 21945b6e0c46Sdougm if (prop != NULL) { 21955b6e0c46Sdougm str = scf_simple_prop_next_astring(prop); 21965b6e0c46Sdougm if (str != NULL) 21975b6e0c46Sdougm tstamp = strtoull(str, NULL, 0); 21985b6e0c46Sdougm else 21995b6e0c46Sdougm tstamp = 0; 22005b6e0c46Sdougm scf_simple_prop_free(prop); 22015b6e0c46Sdougm if (tstamp != implhandle->tstrans) 22025b6e0c46Sdougm return (B_TRUE); 22035b6e0c46Sdougm } 22045b6e0c46Sdougm 22055b6e0c46Sdougm return (B_FALSE); 22065b6e0c46Sdougm } 22075b6e0c46Sdougm 2208da6c28aaSamw /* 2209da6c28aaSamw * sa_fix_resource_name(path) 2210da6c28aaSamw * 2211da6c28aaSamw * change all illegal characters to something else. For now, all get 2212da6c28aaSamw * converted to '_' and the leading '/' is stripped off. This is used 2213da6c28aaSamw * to construct an resource name (SMB share name) that is valid. 2214da6c28aaSamw * Caller must pass a valid path. 2215da6c28aaSamw */ 2216da6c28aaSamw void 2217da6c28aaSamw sa_fix_resource_name(char *path) 2218da6c28aaSamw { 2219da6c28aaSamw char *cp; 2220da6c28aaSamw size_t len; 2221da6c28aaSamw 2222da6c28aaSamw assert(path != NULL); 2223da6c28aaSamw 2224da6c28aaSamw /* make sure we are appropriate length */ 2225da6c28aaSamw cp = path; 2226da6c28aaSamw if (*cp == '/') 2227da6c28aaSamw cp++; /* skip leading slash */ 2228da6c28aaSamw while (cp != NULL && strlen(cp) > SA_MAX_RESOURCE_NAME) { 2229da6c28aaSamw cp = strchr(cp, '/'); 2230da6c28aaSamw if (cp != NULL) 2231da6c28aaSamw cp++; 2232da6c28aaSamw } 2233da6c28aaSamw /* two cases - cp == NULL and cp is substring of path */ 2234da6c28aaSamw if (cp == NULL) { 2235da6c28aaSamw /* just take last SA_MAX_RESOURCE_NAME chars */ 2236da6c28aaSamw len = 1 + strlen(path) - SA_MAX_RESOURCE_NAME; 2237da6c28aaSamw (void) memmove(path, path + len, SA_MAX_RESOURCE_NAME); 2238da6c28aaSamw path[SA_MAX_RESOURCE_NAME] = '\0'; 2239da6c28aaSamw } else { 2240da6c28aaSamw len = strlen(cp) + 1; 2241da6c28aaSamw (void) memmove(path, cp, len); 2242da6c28aaSamw } 2243da6c28aaSamw 2244da6c28aaSamw /* 2245da6c28aaSamw * Don't want any of the characters that are not allowed 2246da6c28aaSamw * in an SMB share name. Replace them with '_'. 2247da6c28aaSamw */ 2248da6c28aaSamw while (*path) { 2249da6c28aaSamw switch (*path) { 2250da6c28aaSamw case '/': 2251da6c28aaSamw case '"': 2252da6c28aaSamw case '\\': 2253da6c28aaSamw case '[': 2254da6c28aaSamw case ']': 2255da6c28aaSamw case ':': 2256da6c28aaSamw case '|': 2257da6c28aaSamw case '<': 2258da6c28aaSamw case '>': 2259da6c28aaSamw case '+': 2260da6c28aaSamw case ';': 2261da6c28aaSamw case ',': 2262da6c28aaSamw case '?': 2263da6c28aaSamw case '*': 2264da6c28aaSamw case '=': 2265da6c28aaSamw case '\t': 2266da6c28aaSamw *path = '_'; 2267da6c28aaSamw break; 2268da6c28aaSamw } 2269da6c28aaSamw path++; 2270da6c28aaSamw } 2271da6c28aaSamw } 2272