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 /* 23148c5f43SAlan Wright * Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved. 245cb0d679SMarcel Telka * Copyright 2014 Nexenta Systems, Inc. All rights reserved. 25*09c9e6dcSChris Williamson * Copyright (c) 2016 by Delphix. All rights reserved. 266185db85Sdougm */ 276185db85Sdougm 286185db85Sdougm /* 296185db85Sdougm * core library for common functions across all config store types 306185db85Sdougm * and file systems to be exported. This includes legacy dfstab/sharetab 316185db85Sdougm * parsing. Need to eliminate XML where possible. 326185db85Sdougm */ 336185db85Sdougm 346185db85Sdougm #include <stdio.h> 356185db85Sdougm #include <string.h> 366185db85Sdougm #include <ctype.h> 376185db85Sdougm #include <unistd.h> 386185db85Sdougm #include <limits.h> 396185db85Sdougm #include <errno.h> 406185db85Sdougm #include <sys/types.h> 416185db85Sdougm #include <sys/stat.h> 426185db85Sdougm #include <libxml/parser.h> 436185db85Sdougm #include <libxml/tree.h> 446185db85Sdougm #include "libshare.h" 456185db85Sdougm #include "libshare_impl.h" 466185db85Sdougm #include <fcntl.h> 478d7e4166Sjose borrego #include <thread.h> 486185db85Sdougm #include <grp.h> 496185db85Sdougm #include <limits.h> 506185db85Sdougm #include <sys/param.h> 516185db85Sdougm #include <signal.h> 526185db85Sdougm #include <libintl.h> 53da6c28aaSamw #include <dirent.h> 546185db85Sdougm 55a237e38eSth #include <sharefs/share.h> 566185db85Sdougm #include "sharetab.h" 576185db85Sdougm 586185db85Sdougm #define DFSTAB_NOTICE_LINES 5 596185db85Sdougm static char *notice[DFSTAB_NOTICE_LINES] = { 606185db85Sdougm "# Do not modify this file directly.\n", 616185db85Sdougm "# Use the sharemgr(1m) command for all share management\n", 626185db85Sdougm "# This file is reconstructed and only maintained for backward\n", 636185db85Sdougm "# compatibility. Configuration lines could be lost.\n", 646185db85Sdougm "#\n" 656185db85Sdougm }; 666185db85Sdougm 676185db85Sdougm #define STRNCAT(x, y, z) (xmlChar *)strncat((char *)x, (char *)y, z) 686185db85Sdougm 696185db85Sdougm /* will be much smaller, but this handles bad syntax in the file */ 706185db85Sdougm #define MAXARGSFORSHARE 256 716185db85Sdougm 728d7e4166Sjose borrego static mutex_t sharetab_lock = DEFAULTMUTEX; 73148c5f43SAlan Wright extern mutex_t sa_dfstab_lock; 748d7e4166Sjose borrego 756185db85Sdougm /* used internally only */ 766185db85Sdougm typedef 776185db85Sdougm struct sharelist { 786185db85Sdougm struct sharelist *next; 796185db85Sdougm int persist; 806185db85Sdougm char *path; 816185db85Sdougm char *resource; 826185db85Sdougm char *fstype; 836185db85Sdougm char *options; 846185db85Sdougm char *description; 856185db85Sdougm char *group; 866185db85Sdougm char *origline; 876185db85Sdougm int lineno; 886185db85Sdougm } xfs_sharelist_t; 89549ec3ffSdougm static void parse_dfstab(sa_handle_t, char *, xmlNodePtr); 908d7e4166Sjose borrego extern char *_sa_get_token(char *); 916185db85Sdougm static void dfs_free_list(xfs_sharelist_t *); 926185db85Sdougm /* prototypes */ 93549ec3ffSdougm void getlegacyconfig(sa_handle_t, char *, xmlNodePtr *); 94da6c28aaSamw extern sa_share_t _sa_add_share(sa_group_t, char *, int, int *, uint64_t); 95549ec3ffSdougm extern sa_group_t _sa_create_group(sa_handle_impl_t, char *); 966185db85Sdougm static void outdfstab(FILE *, xfs_sharelist_t *); 976185db85Sdougm extern int _sa_remove_optionset(sa_optionset_t); 986185db85Sdougm extern int set_node_share(void *, char *, char *); 996185db85Sdougm extern void set_node_attr(void *, char *, char *); 1006185db85Sdougm 101a99982a7Sdougm /* 102a99982a7Sdougm * sablocksigs(*sigs) 103a99982a7Sdougm * 104a99982a7Sdougm * block important signals for a critical region. Arg is a pointer to 105a99982a7Sdougm * a sigset_t that is used later for the unblock. 106a99982a7Sdougm */ 107a99982a7Sdougm void 108a99982a7Sdougm sablocksigs(sigset_t *sigs) 109a99982a7Sdougm { 110a99982a7Sdougm sigset_t new; 111a99982a7Sdougm 112a99982a7Sdougm if (sigs != NULL) { 11325a68471Sdougm (void) sigprocmask(SIG_BLOCK, NULL, &new); 11425a68471Sdougm (void) sigaddset(&new, SIGHUP); 11525a68471Sdougm (void) sigaddset(&new, SIGINT); 11625a68471Sdougm (void) sigaddset(&new, SIGQUIT); 11725a68471Sdougm (void) sigaddset(&new, SIGTSTP); 11825a68471Sdougm (void) sigprocmask(SIG_SETMASK, &new, sigs); 119a99982a7Sdougm } 120a99982a7Sdougm } 121a99982a7Sdougm 122a99982a7Sdougm /* 123a99982a7Sdougm * saunblocksigs(*sigs) 124a99982a7Sdougm * 125a99982a7Sdougm * unblock previously blocked signals from the sigs arg. 126a99982a7Sdougm */ 127a99982a7Sdougm void 128a99982a7Sdougm saunblocksigs(sigset_t *sigs) 129a99982a7Sdougm { 130a99982a7Sdougm if (sigs != NULL) 13125a68471Sdougm (void) sigprocmask(SIG_SETMASK, sigs, NULL); 132a99982a7Sdougm } 133a99982a7Sdougm 1346185db85Sdougm /* 1356185db85Sdougm * alloc_sharelist() 1366185db85Sdougm * 1376185db85Sdougm * allocator function to return an zfs_sharelist_t 1386185db85Sdougm */ 1396185db85Sdougm 1406185db85Sdougm static xfs_sharelist_t * 1416185db85Sdougm alloc_sharelist() 1426185db85Sdougm { 1436185db85Sdougm xfs_sharelist_t *item; 1446185db85Sdougm 1456185db85Sdougm item = (xfs_sharelist_t *)malloc(sizeof (xfs_sharelist_t)); 1466185db85Sdougm if (item != NULL) 14725a68471Sdougm (void) memset(item, '\0', sizeof (xfs_sharelist_t)); 1486185db85Sdougm return (item); 1496185db85Sdougm } 1506185db85Sdougm 1516185db85Sdougm /* 1526185db85Sdougm * fix_notice(list) 1536185db85Sdougm * 1546185db85Sdougm * Look at the beginning of the current /etc/dfs/dfstab file and add 1556185db85Sdougm * the do not modify notice if it doesn't exist. 1566185db85Sdougm */ 1576185db85Sdougm 1586185db85Sdougm static xfs_sharelist_t * 1596185db85Sdougm fix_notice(xfs_sharelist_t *list) 1606185db85Sdougm { 1616185db85Sdougm xfs_sharelist_t *item, *prev; 1626185db85Sdougm int i; 1636185db85Sdougm 1647d968cb8Sdougm if (list == NULL) { 16525a68471Sdougm /* zero length dfstab */ 16625a68471Sdougm list = alloc_sharelist(); 16725a68471Sdougm if (list == NULL) 16825a68471Sdougm return (NULL); 16925a68471Sdougm list->description = strdup("#\n"); 1707d968cb8Sdougm } 1716185db85Sdougm if (list->path == NULL && list->description != NULL && 1726185db85Sdougm strcmp(list->description, notice[0]) != 0) { 17325a68471Sdougm for (prev = NULL, i = 0; i < DFSTAB_NOTICE_LINES; i++) { 17425a68471Sdougm item = alloc_sharelist(); 17525a68471Sdougm if (item != NULL) { 17625a68471Sdougm item->description = strdup(notice[i]); 17725a68471Sdougm if (prev == NULL) { 17825a68471Sdougm item->next = list; 17925a68471Sdougm prev = item; 18025a68471Sdougm list = item; 18125a68471Sdougm } else { 18225a68471Sdougm item->next = prev->next; 18325a68471Sdougm prev->next = item; 18425a68471Sdougm prev = item; 18525a68471Sdougm } 18625a68471Sdougm } 1876185db85Sdougm } 1886185db85Sdougm } 1896185db85Sdougm return (list); 1906185db85Sdougm } 1916185db85Sdougm 1926185db85Sdougm /* 1936185db85Sdougm * getdfstab(dfs) 1946185db85Sdougm * 1956185db85Sdougm * Returns an zfs_sharelist_t list of lines from the dfstab file 1966185db85Sdougm * pointed to by the FILE pointer dfs. Each entry is parsed and the 1976185db85Sdougm * original line is also preserved. Used in parsing and updating the 1986185db85Sdougm * dfstab file. 1996185db85Sdougm */ 2006185db85Sdougm 2016185db85Sdougm static xfs_sharelist_t * 2026185db85Sdougm getdfstab(FILE *dfs) 2036185db85Sdougm { 2046185db85Sdougm char buff[_POSIX_ARG_MAX]; /* reasonable size given syntax of share */ 2056185db85Sdougm char *bp; 2066185db85Sdougm char *token; 2076185db85Sdougm char *args[MAXARGSFORSHARE]; 2086185db85Sdougm int argc; 2096185db85Sdougm int c; 2106185db85Sdougm static int line = 0; 2117d968cb8Sdougm xfs_sharelist_t *item = NULL, *first = NULL, *last; 2126185db85Sdougm 2136185db85Sdougm if (dfs != NULL) { 21425a68471Sdougm first = NULL; 21525a68471Sdougm line = 0; 21625a68471Sdougm while (fgets(buff, sizeof (buff), dfs) != NULL) { 21725a68471Sdougm line++; 21825a68471Sdougm bp = buff; 21925a68471Sdougm if (buff[0] == '#') { 22025a68471Sdougm item = alloc_sharelist(); 22125a68471Sdougm if (item != NULL) { 22225a68471Sdougm /* if no path, then comment */ 22325a68471Sdougm item->lineno = line; 22425a68471Sdougm item->description = strdup(buff); 22525a68471Sdougm if (first == NULL) { 22625a68471Sdougm first = item; 22725a68471Sdougm last = item; 22825a68471Sdougm } else { 22925a68471Sdougm last->next = item; 23025a68471Sdougm last = item; 23125a68471Sdougm } 23225a68471Sdougm } else { 23325a68471Sdougm break; 23425a68471Sdougm } 23525a68471Sdougm continue; 23625a68471Sdougm } else if (buff[0] == '\n') { 23725a68471Sdougm continue; 23825a68471Sdougm } 23925a68471Sdougm optind = 1; 24025a68471Sdougm item = alloc_sharelist(); 24125a68471Sdougm if (item == NULL) { 24225a68471Sdougm break; 24325a68471Sdougm } else if (first == NULL) { 24425a68471Sdougm first = item; 24525a68471Sdougm last = item; 2466185db85Sdougm } else { 24725a68471Sdougm last->next = item; 24825a68471Sdougm last = item; 2496185db85Sdougm } 25025a68471Sdougm item->lineno = line; 25125a68471Sdougm item->origline = strdup(buff); 2528d7e4166Sjose borrego (void) _sa_get_token(NULL); /* reset to new pointers */ 25325a68471Sdougm argc = 0; 2548d7e4166Sjose borrego while ((token = _sa_get_token(bp)) != NULL) { 25525a68471Sdougm if (argc < MAXARGSFORSHARE) 25625a68471Sdougm args[argc++] = token; 2576185db85Sdougm } 25825a68471Sdougm while ((c = getopt(argc, args, "F:o:d:pg:")) != -1) { 25925a68471Sdougm switch (c) { 26025a68471Sdougm case 'p': 26125a68471Sdougm item->persist = 1; 26225a68471Sdougm break; 26325a68471Sdougm case 'F': 26425a68471Sdougm item->fstype = strdup(optarg); 26525a68471Sdougm break; 26625a68471Sdougm case 'o': 26725a68471Sdougm item->options = strdup(optarg); 26825a68471Sdougm break; 26925a68471Sdougm case 'd': 27025a68471Sdougm item->description = strdup(optarg); 27125a68471Sdougm break; 27225a68471Sdougm case 'g': 27325a68471Sdougm item->group = strdup(optarg); 27425a68471Sdougm break; 27525a68471Sdougm default: 27625a68471Sdougm break; 27725a68471Sdougm } 27825a68471Sdougm } 27925a68471Sdougm if (optind < argc) { 28025a68471Sdougm item->path = strdup(args[optind]); 28125a68471Sdougm optind++; 28225a68471Sdougm if (optind < argc) { 28325a68471Sdougm char *resource; 28425a68471Sdougm char *optgroup; 28525a68471Sdougm /* resource and/or groupname */ 28625a68471Sdougm resource = args[optind]; 28725a68471Sdougm optgroup = strchr(resource, '@'); 28825a68471Sdougm if (optgroup != NULL) 28925a68471Sdougm *optgroup++ = '\0'; 29025a68471Sdougm if (optgroup != NULL) 29125a68471Sdougm item->group = strdup(optgroup); 29225a68471Sdougm if (resource != NULL && 29325a68471Sdougm strlen(resource) > 0) 29425a68471Sdougm item->resource = 29525a68471Sdougm strdup(resource); 29625a68471Sdougm } 29725a68471Sdougm } 29825a68471Sdougm /* NFS is the default if none defined */ 29925a68471Sdougm if (item != NULL && item->fstype == NULL) 30025a68471Sdougm item->fstype = strdup("nfs"); 301a99982a7Sdougm } 3026185db85Sdougm } 3036185db85Sdougm first = fix_notice(first); 3046185db85Sdougm return (first); 3056185db85Sdougm } 3066185db85Sdougm 3076185db85Sdougm /* 3086185db85Sdougm * finddfsentry(list, path) 3096185db85Sdougm * 3107d968cb8Sdougm * Look for path in the zfs_sharelist_t list and return the entry if it 3116185db85Sdougm * exists. 3126185db85Sdougm */ 3136185db85Sdougm 3146185db85Sdougm static xfs_sharelist_t * 3156185db85Sdougm finddfsentry(xfs_sharelist_t *list, char *path) 3166185db85Sdougm { 3176185db85Sdougm xfs_sharelist_t *item; 3186185db85Sdougm 3196185db85Sdougm for (item = list; item != NULL; item = item->next) { 32025a68471Sdougm if (item->path != NULL && strcmp(item->path, path) == 0) 3216185db85Sdougm return (item); 3226185db85Sdougm } 3236185db85Sdougm return (NULL); 3246185db85Sdougm } 3256185db85Sdougm 3266185db85Sdougm /* 3276185db85Sdougm * remdfsentry(list, path, proto) 3286185db85Sdougm * 3296185db85Sdougm * Remove the specified path (with protocol) from the list. This will 3306185db85Sdougm * remove it from dfstab when the file is rewritten. 3316185db85Sdougm */ 3326185db85Sdougm 3336185db85Sdougm static xfs_sharelist_t * 3346185db85Sdougm remdfsentry(xfs_sharelist_t *list, char *path, char *proto) 3356185db85Sdougm { 3366185db85Sdougm xfs_sharelist_t *item, *prev = NULL; 3376185db85Sdougm 3386185db85Sdougm 3396185db85Sdougm for (item = prev = list; item != NULL; item = item->next) { 3406185db85Sdougm /* skip comment entry but don't lose it */ 34125a68471Sdougm if (item->path == NULL) { 34225a68471Sdougm prev = item; 34325a68471Sdougm continue; 34425a68471Sdougm } 34525a68471Sdougm /* if proto is NULL, remove all protocols */ 34625a68471Sdougm if (proto == NULL || (strcmp(item->path, path) == 0 && 34725a68471Sdougm (item->fstype != NULL && strcmp(item->fstype, proto) == 0))) 34825a68471Sdougm break; 34925a68471Sdougm if (item->fstype == NULL && 35025a68471Sdougm (proto == NULL || strcmp(proto, "nfs") == 0)) 35125a68471Sdougm break; 3526185db85Sdougm prev = item; 3536185db85Sdougm } 3546185db85Sdougm if (item != NULL) { 35525a68471Sdougm if (item == prev) 35625a68471Sdougm list = item->next; /* this must be the first one */ 35725a68471Sdougm else 35825a68471Sdougm prev->next = item->next; 35925a68471Sdougm item->next = NULL; 36025a68471Sdougm dfs_free_list(item); 3616185db85Sdougm } 3626185db85Sdougm return (list); 3636185db85Sdougm } 3646185db85Sdougm 3656185db85Sdougm /* 3666185db85Sdougm * remdfsline(list, line) 3676185db85Sdougm * 3686185db85Sdougm * Remove the line specified from the list. 3696185db85Sdougm */ 3706185db85Sdougm 3716185db85Sdougm static xfs_sharelist_t * 3726185db85Sdougm remdfsline(xfs_sharelist_t *list, char *line) 3736185db85Sdougm { 3746185db85Sdougm xfs_sharelist_t *item, *prev = NULL; 3756185db85Sdougm 3766185db85Sdougm for (item = prev = list; item != NULL; item = item->next) { 37725a68471Sdougm /* skip comment entry but don't lose it */ 37825a68471Sdougm if (item->path == NULL) { 3796185db85Sdougm prev = item; 3806185db85Sdougm continue; 38125a68471Sdougm } 38225a68471Sdougm if (strcmp(item->origline, line) == 0) 38325a68471Sdougm break; 38425a68471Sdougm prev = item; 3856185db85Sdougm } 3866185db85Sdougm if (item != NULL) { 38725a68471Sdougm if (item == prev) 38825a68471Sdougm list = item->next; /* this must be the first one */ 38925a68471Sdougm else 39025a68471Sdougm prev->next = item->next; 39125a68471Sdougm item->next = NULL; 39225a68471Sdougm dfs_free_list(item); 3936185db85Sdougm } 3946185db85Sdougm return (list); 3956185db85Sdougm } 3966185db85Sdougm 3976185db85Sdougm /* 3986185db85Sdougm * adddfsentry(list, share, proto) 3996185db85Sdougm * 4006185db85Sdougm * Add an entry to the dfstab list for share (relative to proto). This 4016185db85Sdougm * is used to update dfstab for legacy purposes. 4026185db85Sdougm */ 4036185db85Sdougm 4046185db85Sdougm static xfs_sharelist_t * 4056185db85Sdougm adddfsentry(xfs_sharelist_t *list, sa_share_t share, char *proto) 4066185db85Sdougm { 4076185db85Sdougm xfs_sharelist_t *item, *tmp; 4086185db85Sdougm sa_group_t parent; 4096185db85Sdougm char *groupname; 4106185db85Sdougm 4116185db85Sdougm item = alloc_sharelist(); 4126185db85Sdougm if (item != NULL) { 41325a68471Sdougm parent = sa_get_parent_group(share); 41425a68471Sdougm groupname = sa_get_group_attr(parent, "name"); 415fe1c642dSBill Krier if (groupname != NULL && strcmp(groupname, "default") == 0) { 41625a68471Sdougm sa_free_attr_string(groupname); 41725a68471Sdougm groupname = NULL; 41825a68471Sdougm } 41925a68471Sdougm item->path = sa_get_share_attr(share, "path"); 42025a68471Sdougm item->resource = sa_get_share_attr(share, "resource"); 42125a68471Sdougm item->group = groupname; 42225a68471Sdougm item->fstype = strdup(proto); 42325a68471Sdougm item->options = sa_proto_legacy_format(proto, share, 1); 42425a68471Sdougm if (item->options != NULL && strlen(item->options) == 0) { 42525a68471Sdougm free(item->options); 42625a68471Sdougm item->options = NULL; 42725a68471Sdougm } 42825a68471Sdougm item->description = sa_get_share_description(share); 42925a68471Sdougm if (item->description != NULL && 43025a68471Sdougm strlen(item->description) == 0) { 43125a68471Sdougm sa_free_share_description(item->description); 43225a68471Sdougm item->description = NULL; 43325a68471Sdougm } 43425a68471Sdougm if (list == NULL) { 43525a68471Sdougm list = item; 43625a68471Sdougm } else { 43725a68471Sdougm for (tmp = list; tmp->next != NULL; tmp = tmp->next) 43825a68471Sdougm /* do nothing */; 43925a68471Sdougm tmp->next = item; 44025a68471Sdougm } 4416185db85Sdougm } 4426185db85Sdougm return (list); 4436185db85Sdougm } 4446185db85Sdougm 4456185db85Sdougm /* 4466185db85Sdougm * outdfstab(dfstab, list) 4476185db85Sdougm * 4486185db85Sdougm * Output the list to dfstab making sure the file is truncated. 4496185db85Sdougm * Comments and errors are preserved. 4506185db85Sdougm */ 4516185db85Sdougm 4526185db85Sdougm static void 4536185db85Sdougm outdfstab(FILE *dfstab, xfs_sharelist_t *list) 4546185db85Sdougm { 4556185db85Sdougm xfs_sharelist_t *item; 4566185db85Sdougm 4576185db85Sdougm (void) ftruncate(fileno(dfstab), 0); 4586185db85Sdougm 4596185db85Sdougm for (item = list; item != NULL; item = item->next) { 46025a68471Sdougm if (item->path != NULL) { 46125a68471Sdougm if (*item->path == '/') { 46225a68471Sdougm (void) fprintf(dfstab, 46325a68471Sdougm "share %s%s%s%s%s%s%s %s%s%s%s%s\n", 46425a68471Sdougm (item->fstype != NULL) ? "-F " : "", 46525a68471Sdougm (item->fstype != NULL) ? item->fstype : "", 46625a68471Sdougm (item->options != NULL) ? " -o " : "", 46725a68471Sdougm (item->options != NULL) ? 46825a68471Sdougm item->options : "", 46925a68471Sdougm (item->description != NULL) ? 47025a68471Sdougm " -d \"" : "", 47125a68471Sdougm (item->description != NULL) ? 47225a68471Sdougm item->description : "", 47325a68471Sdougm (item->description != NULL) ? "\"" : "", 47425a68471Sdougm item->path, 47525a68471Sdougm ((item->resource != NULL) || 47625a68471Sdougm (item->group != NULL)) ? " " : "", 47725a68471Sdougm (item->resource != NULL) ? 47825a68471Sdougm item->resource : "", 47925a68471Sdougm item->group != NULL ? "@" : "", 48025a68471Sdougm item->group != NULL ? item->group : ""); 48125a68471Sdougm } else { 48225a68471Sdougm (void) fprintf(dfstab, "%s", item->origline); 48325a68471Sdougm } 4846185db85Sdougm } else { 48525a68471Sdougm if (item->description != NULL) 48625a68471Sdougm (void) fprintf(dfstab, "%s", item->description); 48725a68471Sdougm else 48825a68471Sdougm (void) fprintf(dfstab, "%s", item->origline); 4896185db85Sdougm } 4906185db85Sdougm } 4916185db85Sdougm } 4926185db85Sdougm 4936185db85Sdougm /* 4946185db85Sdougm * open_dfstab(file) 4956185db85Sdougm * 4966185db85Sdougm * Open the specified dfstab file. If the owner/group/perms are wrong, 4976185db85Sdougm * fix them. 4986185db85Sdougm */ 4996185db85Sdougm 5006185db85Sdougm static FILE * 5016185db85Sdougm open_dfstab(char *file) 5026185db85Sdougm { 5036185db85Sdougm struct group *grp; 5046185db85Sdougm struct group group; 5056185db85Sdougm char *buff; 5066185db85Sdougm int grsize; 5076185db85Sdougm FILE *dfstab; 5086185db85Sdougm 5096185db85Sdougm dfstab = fopen(file, "r+"); 5106185db85Sdougm if (dfstab == NULL) { 51125a68471Sdougm dfstab = fopen(file, "w+"); 5126185db85Sdougm } 5136185db85Sdougm if (dfstab != NULL) { 5146185db85Sdougm grsize = sysconf(_SC_GETGR_R_SIZE_MAX); 5156185db85Sdougm buff = malloc(grsize); 5166185db85Sdougm if (buff != NULL) 51725a68471Sdougm grp = getgrnam_r(SA_DEFAULT_FILE_GRP, &group, buff, 51825a68471Sdougm grsize); 5196185db85Sdougm else 52025a68471Sdougm grp = getgrnam(SA_DEFAULT_FILE_GRP); 5216185db85Sdougm (void) fchmod(fileno(dfstab), 0644); 5226185db85Sdougm (void) fchown(fileno(dfstab), 0, 52325a68471Sdougm grp != NULL ? grp->gr_gid : 3); 5246185db85Sdougm if (buff != NULL) 52525a68471Sdougm free(buff); 5266185db85Sdougm rewind(dfstab); 5276185db85Sdougm } 5286185db85Sdougm return (dfstab); 5296185db85Sdougm } 5306185db85Sdougm 5316185db85Sdougm /* 5326185db85Sdougm * sa_comment_line(line, err) 5336185db85Sdougm * 5346185db85Sdougm * Add a comment to the dfstab file with err as a prefix to the 5356185db85Sdougm * original line. 5366185db85Sdougm */ 5376185db85Sdougm 5386185db85Sdougm static void 5396185db85Sdougm sa_comment_line(char *line, char *err) 5406185db85Sdougm { 5416185db85Sdougm FILE *dfstab; 5426185db85Sdougm xfs_sharelist_t *list; 543a99982a7Sdougm sigset_t old; 5446185db85Sdougm 5456185db85Sdougm dfstab = open_dfstab(SA_LEGACY_DFSTAB); 5466185db85Sdougm if (dfstab != NULL) { 5476185db85Sdougm (void) setvbuf(dfstab, NULL, _IOLBF, BUFSIZ * 8); 548a99982a7Sdougm sablocksigs(&old); 5496185db85Sdougm (void) lockf(fileno(dfstab), F_LOCK, 0); 550148c5f43SAlan Wright (void) mutex_lock(&sa_dfstab_lock); 5516185db85Sdougm list = getdfstab(dfstab); 5526185db85Sdougm rewind(dfstab); 553f345c0beSdougm /* 554f345c0beSdougm * don't ignore the return since the list could have 555f345c0beSdougm * gone to NULL if the file only had one line in it. 556f345c0beSdougm */ 557f345c0beSdougm list = remdfsline(list, line); 5586185db85Sdougm outdfstab(dfstab, list); 5596185db85Sdougm (void) fprintf(dfstab, "# Error: %s: %s", err, line); 5606185db85Sdougm (void) fsync(fileno(dfstab)); 561148c5f43SAlan Wright (void) mutex_unlock(&sa_dfstab_lock); 5626185db85Sdougm (void) lockf(fileno(dfstab), F_ULOCK, 0); 5636185db85Sdougm (void) fclose(dfstab); 564a99982a7Sdougm saunblocksigs(&old); 5656185db85Sdougm if (list != NULL) 56625a68471Sdougm dfs_free_list(list); 5676185db85Sdougm } 5686185db85Sdougm } 5696185db85Sdougm 5706185db85Sdougm /* 571da6c28aaSamw * sa_delete_legacy(share, protocol) 5726185db85Sdougm * 5736185db85Sdougm * Delete the specified share from the legacy config file. 5746185db85Sdougm */ 5756185db85Sdougm 5766185db85Sdougm int 577da6c28aaSamw sa_delete_legacy(sa_share_t share, char *protocol) 5786185db85Sdougm { 5796185db85Sdougm FILE *dfstab; 5806185db85Sdougm int err; 5816185db85Sdougm int ret = SA_OK; 5826185db85Sdougm xfs_sharelist_t *list; 5836185db85Sdougm char *path; 5846185db85Sdougm sa_optionset_t optionset; 5856185db85Sdougm sa_group_t parent; 586a99982a7Sdougm sigset_t old; 5876185db85Sdougm 588da6c28aaSamw /* 589da6c28aaSamw * Protect against shares that don't have paths. This is not 590da6c28aaSamw * really an error at this point. 591da6c28aaSamw */ 592da6c28aaSamw path = sa_get_share_attr(share, "path"); 593da6c28aaSamw if (path == NULL) 594da6c28aaSamw return (ret); 595da6c28aaSamw 5966185db85Sdougm dfstab = open_dfstab(SA_LEGACY_DFSTAB); 5976185db85Sdougm if (dfstab != NULL) { 5986185db85Sdougm (void) setvbuf(dfstab, NULL, _IOLBF, BUFSIZ * 8); 599a99982a7Sdougm sablocksigs(&old); 6006185db85Sdougm parent = sa_get_parent_group(share); 6016185db85Sdougm if (parent != NULL) { 60225a68471Sdougm (void) lockf(fileno(dfstab), F_LOCK, 0); 603148c5f43SAlan Wright (void) mutex_lock(&sa_dfstab_lock); 60425a68471Sdougm list = getdfstab(dfstab); 60525a68471Sdougm rewind(dfstab); 606da6c28aaSamw if (protocol != NULL) { 607da6c28aaSamw if (list != NULL) 60825a68471Sdougm list = remdfsentry(list, path, 609da6c28aaSamw protocol); 610da6c28aaSamw } else { 611da6c28aaSamw for (optionset = sa_get_optionset(parent, NULL); 612da6c28aaSamw optionset != NULL; 613da6c28aaSamw optionset = 614da6c28aaSamw sa_get_next_optionset(optionset)) { 615da6c28aaSamw char *proto = sa_get_optionset_attr( 616da6c28aaSamw optionset, "type"); 617da6c28aaSamw 618da6c28aaSamw if (list != NULL && proto != NULL) 619da6c28aaSamw list = remdfsentry(list, path, 620da6c28aaSamw proto); 621da6c28aaSamw if (proto == NULL) 622da6c28aaSamw ret = SA_NO_MEMORY; 623da6c28aaSamw /* 624da6c28aaSamw * may want to only do the dfstab if 625da6c28aaSamw * this call returns NOT IMPLEMENTED 626da6c28aaSamw * but it shouldn't hurt. 627da6c28aaSamw */ 628da6c28aaSamw if (ret == SA_OK) { 629da6c28aaSamw err = sa_proto_delete_legacy( 630da6c28aaSamw proto, share); 631da6c28aaSamw if (err != SA_NOT_IMPLEMENTED) 632da6c28aaSamw ret = err; 633da6c28aaSamw } 634da6c28aaSamw if (proto != NULL) 635da6c28aaSamw sa_free_attr_string(proto); 63625a68471Sdougm } 6376185db85Sdougm } 63825a68471Sdougm outdfstab(dfstab, list); 63925a68471Sdougm if (list != NULL) 64025a68471Sdougm dfs_free_list(list); 64125a68471Sdougm (void) fflush(dfstab); 642148c5f43SAlan Wright (void) mutex_unlock(&sa_dfstab_lock); 64325a68471Sdougm (void) lockf(fileno(dfstab), F_ULOCK, 0); 6446185db85Sdougm } 6456185db85Sdougm (void) fsync(fileno(dfstab)); 646a99982a7Sdougm saunblocksigs(&old); 6476185db85Sdougm (void) fclose(dfstab); 6486185db85Sdougm } else { 64925a68471Sdougm if (errno == EACCES || errno == EPERM) 65025a68471Sdougm ret = SA_NO_PERMISSION; 65125a68471Sdougm else 65225a68471Sdougm ret = SA_CONFIG_ERR; 6536185db85Sdougm } 654da6c28aaSamw 655da6c28aaSamw if (path != NULL) 656da6c28aaSamw sa_free_attr_string(path); 657da6c28aaSamw 6586185db85Sdougm return (ret); 6596185db85Sdougm } 6606185db85Sdougm 6616185db85Sdougm /* 6626185db85Sdougm * sa_update_legacy(share, proto) 6636185db85Sdougm * 6646185db85Sdougm * There is an assumption that dfstab will be the most common form of 6656185db85Sdougm * legacy configuration file for shares, but not the only one. Because 6666185db85Sdougm * of that, dfstab handling is done in the main code with calls to 667da6c28aaSamw * this function and protocol specific calls to deal with formatting 6686185db85Sdougm * options into dfstab/share compatible syntax. Since not everything 6696185db85Sdougm * will be dfstab, there is a provision for calling a protocol 6706185db85Sdougm * specific plugin interface that allows the protocol plugin to do its 6716185db85Sdougm * own legacy files and skip the dfstab update. 6726185db85Sdougm */ 6736185db85Sdougm 6746185db85Sdougm int 6756185db85Sdougm sa_update_legacy(sa_share_t share, char *proto) 6766185db85Sdougm { 6776185db85Sdougm FILE *dfstab; 6786185db85Sdougm int ret = SA_OK; 6796185db85Sdougm xfs_sharelist_t *list; 6806185db85Sdougm char *path; 681a99982a7Sdougm sigset_t old; 6826185db85Sdougm char *persist; 683da6c28aaSamw uint64_t features; 6846185db85Sdougm 6856185db85Sdougm ret = sa_proto_update_legacy(proto, share); 6866185db85Sdougm if (ret != SA_NOT_IMPLEMENTED) 68725a68471Sdougm return (ret); 688da6c28aaSamw 689da6c28aaSamw features = sa_proto_get_featureset(proto); 690da6c28aaSamw if (!(features & SA_FEATURE_DFSTAB)) 691da6c28aaSamw return (ret); 692da6c28aaSamw 6936185db85Sdougm /* do the dfstab format */ 6946185db85Sdougm persist = sa_get_share_attr(share, "type"); 6956185db85Sdougm /* 6966185db85Sdougm * only update if the share is not transient -- no share type 6976185db85Sdougm * set or the type is not "transient". 6986185db85Sdougm */ 6996185db85Sdougm if (persist == NULL || strcmp(persist, "transient") != 0) { 700fe1c642dSBill Krier path = sa_get_share_attr(share, "path"); 701fe1c642dSBill Krier if (path == NULL) { 702fe1c642dSBill Krier ret = SA_NO_MEMORY; 703fe1c642dSBill Krier goto out; 704fe1c642dSBill Krier } 70525a68471Sdougm dfstab = open_dfstab(SA_LEGACY_DFSTAB); 70625a68471Sdougm if (dfstab != NULL) { 70725a68471Sdougm (void) setvbuf(dfstab, NULL, _IOLBF, BUFSIZ * 8); 70825a68471Sdougm sablocksigs(&old); 70925a68471Sdougm (void) lockf(fileno(dfstab), F_LOCK, 0); 710148c5f43SAlan Wright (void) mutex_lock(&sa_dfstab_lock); 71125a68471Sdougm list = getdfstab(dfstab); 71225a68471Sdougm rewind(dfstab); 71325a68471Sdougm if (list != NULL) 71425a68471Sdougm list = remdfsentry(list, path, proto); 71525a68471Sdougm list = adddfsentry(list, share, proto); 71625a68471Sdougm outdfstab(dfstab, list); 71725a68471Sdougm (void) fflush(dfstab); 718148c5f43SAlan Wright (void) mutex_unlock(&sa_dfstab_lock); 71925a68471Sdougm (void) lockf(fileno(dfstab), F_ULOCK, 0); 72025a68471Sdougm (void) fsync(fileno(dfstab)); 72125a68471Sdougm saunblocksigs(&old); 72225a68471Sdougm (void) fclose(dfstab); 72325a68471Sdougm if (list != NULL) 72425a68471Sdougm dfs_free_list(list); 7256185db85Sdougm } else { 72625a68471Sdougm if (errno == EACCES || errno == EPERM) 72725a68471Sdougm ret = SA_NO_PERMISSION; 72825a68471Sdougm else 72925a68471Sdougm ret = SA_CONFIG_ERR; 7306185db85Sdougm } 731fe1c642dSBill Krier sa_free_attr_string(path); 7326185db85Sdougm } 733fe1c642dSBill Krier out: 7346185db85Sdougm if (persist != NULL) 73525a68471Sdougm sa_free_attr_string(persist); 7366185db85Sdougm return (ret); 7376185db85Sdougm } 7386185db85Sdougm 7396185db85Sdougm /* 7406185db85Sdougm * sa_is_security(optname, proto) 7416185db85Sdougm * 7426185db85Sdougm * Check to see if optname is a security (named optionset) specific 7436185db85Sdougm * property for the specified protocol. 7446185db85Sdougm */ 7456185db85Sdougm 7466185db85Sdougm int 7476185db85Sdougm sa_is_security(char *optname, char *proto) 7486185db85Sdougm { 7496185db85Sdougm int ret = 0; 7506185db85Sdougm if (proto != NULL) 75125a68471Sdougm ret = sa_proto_security_prop(proto, optname); 7526185db85Sdougm return (ret); 7536185db85Sdougm } 7546185db85Sdougm 7556185db85Sdougm /* 7566185db85Sdougm * add_syntax_comment(root, line, err, todfstab) 7576185db85Sdougm * 75825a68471Sdougm * Add a comment to the document indicating a syntax error. If 7596185db85Sdougm * todfstab is set, write it back to the dfstab file as well. 7606185db85Sdougm */ 7616185db85Sdougm 7626185db85Sdougm static void 7636185db85Sdougm add_syntax_comment(xmlNodePtr root, char *line, char *err, int todfstab) 7646185db85Sdougm { 7656185db85Sdougm xmlNodePtr node; 7666185db85Sdougm 7676185db85Sdougm node = xmlNewChild(root, NULL, (xmlChar *)"error", (xmlChar *)line); 76825a68471Sdougm if (node != NULL) 7697a9d7716Sthurlow (void) xmlSetProp(node, (xmlChar *)"type", (xmlChar *)err); 7706185db85Sdougm if (todfstab) 77125a68471Sdougm sa_comment_line(line, err); 7726185db85Sdougm } 7736185db85Sdougm 7746185db85Sdougm /* 7756185db85Sdougm * sa_is_share(object) 7766185db85Sdougm * 7775cb0d679SMarcel Telka * returns true if the object is of type "share". 7786185db85Sdougm */ 7796185db85Sdougm 7806185db85Sdougm int 7816185db85Sdougm sa_is_share(void *object) 7826185db85Sdougm { 7836185db85Sdougm if (object != NULL) { 78425a68471Sdougm if (strcmp((char *)((xmlNodePtr)object)->name, "share") == 0) 7856185db85Sdougm return (1); 7866185db85Sdougm } 7876185db85Sdougm return (0); 7886185db85Sdougm } 789da6c28aaSamw /* 790da6c28aaSamw * sa_is_resource(object) 791da6c28aaSamw * 7925cb0d679SMarcel Telka * returns true if the object is of type "resource". 793da6c28aaSamw */ 794da6c28aaSamw 795da6c28aaSamw int 796da6c28aaSamw sa_is_resource(void *object) 797da6c28aaSamw { 798da6c28aaSamw if (object != NULL) { 799da6c28aaSamw if (strcmp((char *)((xmlNodePtr)object)->name, "resource") == 0) 800da6c28aaSamw return (1); 801da6c28aaSamw } 802da6c28aaSamw return (0); 803da6c28aaSamw } 8046185db85Sdougm 8056185db85Sdougm /* 8066185db85Sdougm * _sa_remove_property(property) 8076185db85Sdougm * 8086185db85Sdougm * remove a property only from the document. 8096185db85Sdougm */ 8106185db85Sdougm 8116185db85Sdougm static void 8126185db85Sdougm _sa_remove_property(sa_property_t property) 8136185db85Sdougm { 8146185db85Sdougm xmlUnlinkNode((xmlNodePtr)property); 8156185db85Sdougm xmlFreeNode((xmlNodePtr)property); 8166185db85Sdougm } 8176185db85Sdougm 81867331909Sdougm /* 81967331909Sdougm * _sa_create_dummy_share() 82067331909Sdougm * 82167331909Sdougm * Create a share entry suitable for parsing but not tied to any real 82267331909Sdougm * config tree. Need to have a parent as well as the node to parse 82367331909Sdougm * on. Free using _sa_free_dummy_share(share); 82467331909Sdougm */ 82567331909Sdougm 82667331909Sdougm static sa_group_t 82767331909Sdougm _sa_create_dummy_share() 82867331909Sdougm { 82967331909Sdougm xmlNodePtr parent_node = NULL; 83067331909Sdougm xmlNodePtr child_node = NULL; 83167331909Sdougm 83267331909Sdougm parent_node = xmlNewNode(NULL, (xmlChar *)"group"); 83367331909Sdougm if (parent_node != NULL) { 83425a68471Sdougm child_node = xmlNewChild(parent_node, NULL, (xmlChar *)"share", 83525a68471Sdougm NULL); 83625a68471Sdougm if (child_node != NULL) { 83725a68471Sdougm /* 83825a68471Sdougm * Use a "zfs" tag since that will make sure nothing 83925a68471Sdougm * really attempts to put values into the 84025a68471Sdougm * repository. Also ZFS is currently the only user of 84125a68471Sdougm * this interface. 84225a68471Sdougm */ 84325a68471Sdougm set_node_attr(parent_node, "type", "transient"); 84425a68471Sdougm set_node_attr(parent_node, "zfs", "true"); 84525a68471Sdougm set_node_attr(child_node, "type", "transient"); 84625a68471Sdougm set_node_attr(child_node, "zfs", "true"); 84725a68471Sdougm } else { 84825a68471Sdougm xmlFreeNode(parent_node); 84925a68471Sdougm } 85067331909Sdougm } 85167331909Sdougm return (child_node); 85267331909Sdougm } 85367331909Sdougm 85467331909Sdougm /* 85567331909Sdougm * _sa_free_dummy_share(share) 85667331909Sdougm * 85767331909Sdougm * Free the dummy share and its parent. It is an error to try and 85867331909Sdougm * free something that isn't a dummy. 85967331909Sdougm */ 86067331909Sdougm 86167331909Sdougm static int 86267331909Sdougm _sa_free_dummy_share(sa_share_t share) 86367331909Sdougm { 86467331909Sdougm xmlNodePtr node = (xmlNodePtr)share; 86567331909Sdougm xmlNodePtr parent; 86667331909Sdougm int ret = SA_OK; 86767331909Sdougm char *name; 86867331909Sdougm 86967331909Sdougm if (node != NULL) { 87025a68471Sdougm parent = node->parent; 87125a68471Sdougm name = (char *)xmlGetProp(node, (xmlChar *)"path"); 87225a68471Sdougm if (name != NULL) { 87325a68471Sdougm /* Real shares always have a path but a dummy doesn't */ 87425a68471Sdougm ret = SA_NOT_ALLOWED; 87525a68471Sdougm sa_free_attr_string(name); 87625a68471Sdougm } else { 87725a68471Sdougm /* 87825a68471Sdougm * If there is a parent, do the free on that since 87925a68471Sdougm * xmlFreeNode is a recursive function and free's an 88025a68471Sdougm * child nodes. 88125a68471Sdougm */ 88225a68471Sdougm if (parent != NULL) { 88325a68471Sdougm node = parent; 88425a68471Sdougm } 88525a68471Sdougm xmlUnlinkNode(node); 88625a68471Sdougm xmlFreeNode(node); 88767331909Sdougm } 88867331909Sdougm } 88967331909Sdougm return (ret); 89067331909Sdougm } 89167331909Sdougm 89267331909Sdougm 8936185db85Sdougm /* 8946185db85Sdougm * sa_parse_legacy_options(group, options, proto) 8956185db85Sdougm * 8966185db85Sdougm * In order to support legacy configurations, we allow the protocol 8976185db85Sdougm * specific plugin to parse legacy syntax options (like those in 8986185db85Sdougm * /etc/dfs/dfstab). This adds a new optionset to the group (or 8996185db85Sdougm * share). 9006185db85Sdougm * 9016185db85Sdougm * Once the optionset has been created, we then get the derived 9026185db85Sdougm * optionset of the parent (options from the optionset of the parent 9036185db85Sdougm * and any parent it might have) and remove those from the created 9046185db85Sdougm * optionset. This avoids duplication of options. 9056185db85Sdougm */ 9066185db85Sdougm 9076185db85Sdougm int 9086185db85Sdougm sa_parse_legacy_options(sa_group_t group, char *options, char *proto) 9096185db85Sdougm { 9106185db85Sdougm int ret = SA_INVALID_PROTOCOL; 9116185db85Sdougm sa_group_t parent; 91267331909Sdougm int using_dummy = B_FALSE; 913717a41ebSdougm char *pvalue; 91425a68471Sdougm sa_optionset_t optionset; 91525a68471Sdougm sa_property_t popt, prop; 91625a68471Sdougm sa_optionset_t localoptions; 91767331909Sdougm 91867331909Sdougm /* 91925a68471Sdougm * If "group" is NULL, this is just a parse without saving 92067331909Sdougm * anything in either SMF or ZFS. Create a dummy group to 92167331909Sdougm * handle this case. 92267331909Sdougm */ 92367331909Sdougm if (group == NULL) { 92425a68471Sdougm group = (sa_group_t)_sa_create_dummy_share(); 92525a68471Sdougm using_dummy = B_TRUE; 92667331909Sdougm } 92767331909Sdougm 9286185db85Sdougm parent = sa_get_parent_group(group); 9296185db85Sdougm 9306185db85Sdougm if (proto != NULL) 93125a68471Sdougm ret = sa_proto_legacy_opts(proto, group, options); 93267331909Sdougm 93367331909Sdougm if (using_dummy) { 93425a68471Sdougm /* Since this is a dummy parse, cleanup and quit here */ 93525a68471Sdougm (void) _sa_free_dummy_share(parent); 93625a68471Sdougm return (ret); 93767331909Sdougm } 93825a68471Sdougm 93925a68471Sdougm if (ret != SA_OK) 94025a68471Sdougm return (ret); 94125a68471Sdougm 9426185db85Sdougm /* 94325a68471Sdougm * If in a group, remove the inherited options and security 9446185db85Sdougm */ 94525a68471Sdougm 94625a68471Sdougm if (parent == NULL) 94725a68471Sdougm return (ret); 94825a68471Sdougm 94925a68471Sdougm /* Find parent options to remove from child */ 95025a68471Sdougm optionset = sa_get_derived_optionset(parent, proto, 1); 95125a68471Sdougm localoptions = sa_get_optionset(group, proto); 95225a68471Sdougm if (optionset != NULL) { 95325a68471Sdougm for (popt = sa_get_property(optionset, NULL); 95425a68471Sdougm popt != NULL; 95525a68471Sdougm popt = sa_get_next_property(popt)) { 9566185db85Sdougm char *tag; 957717a41ebSdougm char *value; 9586185db85Sdougm tag = sa_get_property_attr(popt, "type"); 95925a68471Sdougm if (tag == NULL) 96025a68471Sdougm continue; 96125a68471Sdougm prop = sa_get_property(localoptions, tag); 96225a68471Sdougm if (prop != NULL) { 96325a68471Sdougm value = sa_get_property_attr(popt, 96425a68471Sdougm "value"); 96525a68471Sdougm pvalue = sa_get_property_attr(prop, 96625a68471Sdougm "value"); 967717a41ebSdougm if (value != NULL && pvalue != NULL && 968717a41ebSdougm strcmp(value, pvalue) == 0) { 969717a41ebSdougm /* 97025a68471Sdougm * Remove the property 97125a68471Sdougm * from the 97225a68471Sdougm * child. While we 97325a68471Sdougm * removed it, we 97425a68471Sdougm * don't need to reset 97525a68471Sdougm * as we do below 97625a68471Sdougm * since we always 97725a68471Sdougm * search from the 97825a68471Sdougm * beginning. 979717a41ebSdougm */ 98025a68471Sdougm (void) _sa_remove_property( 98125a68471Sdougm prop); 9826185db85Sdougm } 983717a41ebSdougm if (value != NULL) 98425a68471Sdougm sa_free_attr_string(value); 985717a41ebSdougm if (pvalue != NULL) 98625a68471Sdougm sa_free_attr_string(pvalue); 9876185db85Sdougm } 98825a68471Sdougm sa_free_attr_string(tag); 98925a68471Sdougm } 99025a68471Sdougm prop = sa_get_property(localoptions, NULL); 99125a68471Sdougm if (prop == NULL && sa_is_share(group)) { 9926185db85Sdougm /* 99325a68471Sdougm * All properties removed so remove the 9946185db85Sdougm * optionset if it is on a share 9956185db85Sdougm */ 9966185db85Sdougm (void) _sa_remove_optionset(localoptions); 9976185db85Sdougm } 99825a68471Sdougm sa_free_derived_optionset(optionset); 99925a68471Sdougm } 100025a68471Sdougm /* 100125a68471Sdougm * Need to remove security here. If there are no 100225a68471Sdougm * security options on the local group/share, don't 100325a68471Sdougm * bother since those are the only ones that would be 100425a68471Sdougm * affected. 100525a68471Sdougm */ 100625a68471Sdougm localoptions = sa_get_all_security_types(group, proto, 0); 100725a68471Sdougm if (localoptions != NULL) { 100825a68471Sdougm for (prop = sa_get_property(localoptions, NULL); 100925a68471Sdougm prop != NULL; 101025a68471Sdougm prop = sa_get_next_property(prop)) { 10116185db85Sdougm char *tag; 10126185db85Sdougm sa_security_t security; 10136185db85Sdougm tag = sa_get_property_attr(prop, "type"); 10146185db85Sdougm if (tag != NULL) { 101525a68471Sdougm sa_property_t nextpopt = NULL; 101625a68471Sdougm security = sa_get_security(group, tag, proto); 101725a68471Sdougm sa_free_attr_string(tag); 1018717a41ebSdougm /* 101925a68471Sdougm * prop's value only changes outside this loop 1020717a41ebSdougm */ 102125a68471Sdougm pvalue = sa_get_property_attr(prop, "value"); 102225a68471Sdougm for (popt = sa_get_property(security, NULL); 102325a68471Sdougm popt != NULL; 102425a68471Sdougm popt = nextpopt) { 102525a68471Sdougm char *value; 102625a68471Sdougm /* 102725a68471Sdougm * Need to get the next prop 102825a68471Sdougm * now since we could break 102925a68471Sdougm * the list during removal. 103025a68471Sdougm */ 103125a68471Sdougm nextpopt = sa_get_next_property(popt); 103225a68471Sdougm /* remove Duplicates from this level */ 103325a68471Sdougm value = sa_get_property_attr(popt, 103425a68471Sdougm "value"); 103525a68471Sdougm if (value != NULL && pvalue != NULL && 103625a68471Sdougm strcmp(value, pvalue) == 0) { 103725a68471Sdougm /* 103825a68471Sdougm * remove the property 103925a68471Sdougm * from the child 104025a68471Sdougm */ 104125a68471Sdougm (void) _sa_remove_property 104225a68471Sdougm (popt); 104325a68471Sdougm } 104425a68471Sdougm if (value != NULL) 104525a68471Sdougm sa_free_attr_string(value); 10466185db85Sdougm } 104725a68471Sdougm if (pvalue != NULL) 104825a68471Sdougm sa_free_attr_string(pvalue); 10496185db85Sdougm } 10506185db85Sdougm } 105125a68471Sdougm (void) sa_destroy_optionset(localoptions); 10526185db85Sdougm } 10536185db85Sdougm return (ret); 10546185db85Sdougm } 10556185db85Sdougm 10566185db85Sdougm /* 10576185db85Sdougm * dfs_free_list(list) 10586185db85Sdougm * 10596185db85Sdougm * Free the data in each list entry of the list as well as freeing the 10606185db85Sdougm * entries themselves. We need to avoid memory leaks and don't want to 10616185db85Sdougm * dereference any NULL members. 10626185db85Sdougm */ 10636185db85Sdougm 10646185db85Sdougm static void 10656185db85Sdougm dfs_free_list(xfs_sharelist_t *list) 10666185db85Sdougm { 10676185db85Sdougm xfs_sharelist_t *entry; 10686185db85Sdougm for (entry = list; entry != NULL; entry = list) { 106925a68471Sdougm if (entry->path != NULL) 107025a68471Sdougm free(entry->path); 107125a68471Sdougm if (entry->resource != NULL) 107225a68471Sdougm free(entry->resource); 107325a68471Sdougm if (entry->fstype != NULL) 107425a68471Sdougm free(entry->fstype); 107525a68471Sdougm if (entry->options != NULL) 107625a68471Sdougm free(entry->options); 107725a68471Sdougm if (entry->description != NULL) 107825a68471Sdougm free(entry->description); 107925a68471Sdougm if (entry->origline != NULL) 108025a68471Sdougm free(entry->origline); 108125a68471Sdougm if (entry->group != NULL) 108225a68471Sdougm free(entry->group); 108325a68471Sdougm list = list->next; 108425a68471Sdougm free(entry); 10856185db85Sdougm } 10866185db85Sdougm } 10876185db85Sdougm 10886185db85Sdougm /* 10896185db85Sdougm * parse_dfstab(dfstab, root) 10906185db85Sdougm * 10916185db85Sdougm * Open and read the existing dfstab, parsing each line and adding it 10926185db85Sdougm * to the internal configuration. Make sure syntax errors, etc are 10936185db85Sdougm * preserved as comments. 10946185db85Sdougm */ 10956185db85Sdougm 10966185db85Sdougm static void 1097549ec3ffSdougm parse_dfstab(sa_handle_t handle, char *dfstab, xmlNodePtr root) 10986185db85Sdougm { 10996185db85Sdougm sa_share_t share; 11006185db85Sdougm sa_group_t group; 11016185db85Sdougm sa_group_t sgroup = NULL; 11026185db85Sdougm sa_group_t defgroup; 11036185db85Sdougm xfs_sharelist_t *head, *list; 11046185db85Sdougm int err; 11056185db85Sdougm int defined_group; 11066185db85Sdougm FILE *dfs; 11076185db85Sdougm char *oldprops; 11086185db85Sdougm 11096185db85Sdougm /* read the dfstab format file and fill in the doc tree */ 11106185db85Sdougm 11116185db85Sdougm dfs = fopen(dfstab, "r"); 111225a68471Sdougm if (dfs == NULL) 111325a68471Sdougm return; 11146185db85Sdougm 1115549ec3ffSdougm defgroup = sa_get_group(handle, "default"); 11166185db85Sdougm 11176185db85Sdougm for (head = list = getdfstab(dfs); 111825a68471Sdougm list != NULL; 111925a68471Sdougm list = list->next) { 112025a68471Sdougm share = NULL; 112125a68471Sdougm group = NULL; 112225a68471Sdougm defined_group = 0; 112325a68471Sdougm err = 0; 112425a68471Sdougm 112525a68471Sdougm if (list->origline == NULL) { 112625a68471Sdougm /* 112725a68471Sdougm * Comment line that we will likely skip. 112825a68471Sdougm * If the line has the syntax: 112925a68471Sdougm * # error: string: string 113025a68471Sdougm * It should be preserved until manually deleted. 113125a68471Sdougm */ 113225a68471Sdougm if (list->description != NULL && 113325a68471Sdougm strncmp(list->description, "# Error: ", 9) == 0) { 113425a68471Sdougm char *line; 113525a68471Sdougm char *error; 113625a68471Sdougm char *cmd; 113725a68471Sdougm line = strdup(list->description); 113825a68471Sdougm if (line != NULL) { 113925a68471Sdougm error = line + 9; 114025a68471Sdougm cmd = strchr(error, ':'); 114125a68471Sdougm if (cmd != NULL) { 114225a68471Sdougm int len; 114325a68471Sdougm *cmd = '\0'; 114425a68471Sdougm cmd += 2; 114525a68471Sdougm len = strlen(cmd); 114625a68471Sdougm cmd[len - 1] = '\0'; 114725a68471Sdougm add_syntax_comment(root, cmd, 114825a68471Sdougm error, 0); 114925a68471Sdougm } 115025a68471Sdougm free(line); 115125a68471Sdougm } 11526185db85Sdougm } 115325a68471Sdougm continue; 11546185db85Sdougm } 115525a68471Sdougm if (list->path != NULL && strlen(list->path) > 0 && 1156549ec3ffSdougm *list->path == '/') { 115725a68471Sdougm share = sa_find_share(handle, list->path); 115825a68471Sdougm if (share != NULL) 115925a68471Sdougm sgroup = sa_get_parent_group(share); 116025a68471Sdougm else 116125a68471Sdougm sgroup = NULL; 116225a68471Sdougm } else { 116325a68471Sdougm (void) printf(dgettext(TEXT_DOMAIN, 116425a68471Sdougm "No share specified in dfstab: " 116525a68471Sdougm "line %d: %s\n"), 116625a68471Sdougm list->lineno, list->origline); 116725a68471Sdougm add_syntax_comment(root, list->origline, 116825a68471Sdougm dgettext(TEXT_DOMAIN, "No share specified"), 1); 116925a68471Sdougm continue; 117025a68471Sdougm } 117125a68471Sdougm if (list->group != NULL && strlen(list->group) > 0) { 117225a68471Sdougm group = sa_get_group(handle, list->group); 117325a68471Sdougm defined_group = 1; 117425a68471Sdougm } else { 117525a68471Sdougm group = defgroup; 117625a68471Sdougm } 117725a68471Sdougm if (defined_group && group == NULL) { 117825a68471Sdougm (void) printf(dgettext(TEXT_DOMAIN, 117925a68471Sdougm "Unknown group used in dfstab: line %d: %s\n"), 118025a68471Sdougm list->lineno, list->origline); 118125a68471Sdougm add_syntax_comment(root, list->origline, 118225a68471Sdougm dgettext(TEXT_DOMAIN, "Unknown group specified"), 118325a68471Sdougm 1); 118425a68471Sdougm continue; 118525a68471Sdougm } 118625a68471Sdougm if (group == NULL) { 118725a68471Sdougm /* Shouldn't happen unless an SMF error */ 118825a68471Sdougm err = SA_CONFIG_ERR; 118925a68471Sdougm continue; 119025a68471Sdougm } 11916185db85Sdougm if (share == NULL) { 119225a68471Sdougm if (defined_group || group != defgroup) 119325a68471Sdougm continue; 119425a68471Sdougm /* This is an OK add for legacy */ 11956185db85Sdougm share = sa_add_share(defgroup, list->path, 119625a68471Sdougm SA_SHARE_PERMANENT | SA_SHARE_PARSER, &err); 11976185db85Sdougm if (share != NULL) { 119825a68471Sdougm if (list->description != NULL && 119925a68471Sdougm strlen(list->description) > 0) 120025a68471Sdougm (void) sa_set_share_description(share, 120125a68471Sdougm list->description); 120225a68471Sdougm if (list->options != NULL && 120325a68471Sdougm strlen(list->options) > 0) { 120425a68471Sdougm (void) sa_parse_legacy_options(share, 120525a68471Sdougm list->options, list->fstype); 120625a68471Sdougm } 120725a68471Sdougm if (list->resource != NULL) 120825a68471Sdougm (void) sa_set_share_attr(share, 120925a68471Sdougm "resource", list->resource); 12106185db85Sdougm } else { 121125a68471Sdougm (void) printf(dgettext(TEXT_DOMAIN, 121225a68471Sdougm "Error in dfstab: line %d: %s\n"), 12136185db85Sdougm list->lineno, list->origline); 121425a68471Sdougm if (err != SA_BAD_PATH) 121525a68471Sdougm add_syntax_comment(root, list->origline, 121625a68471Sdougm dgettext(TEXT_DOMAIN, "Syntax"), 1); 121725a68471Sdougm else 121825a68471Sdougm add_syntax_comment(root, list->origline, 121925a68471Sdougm dgettext(TEXT_DOMAIN, 122025a68471Sdougm "Path"), 1); 122125a68471Sdougm continue; 12226185db85Sdougm } 12236185db85Sdougm } else { 122425a68471Sdougm if (group != sgroup) { 122525a68471Sdougm (void) printf(dgettext(TEXT_DOMAIN, 122625a68471Sdougm "Attempt to change configuration in " 122725a68471Sdougm "dfstab: line %d: %s\n"), 122825a68471Sdougm list->lineno, list->origline); 122925a68471Sdougm add_syntax_comment(root, list->origline, 123025a68471Sdougm dgettext(TEXT_DOMAIN, 123125a68471Sdougm "Attempt to change configuration"), 1); 123225a68471Sdougm continue; 123325a68471Sdougm } 1234546405c3Sdougm /* 1235546405c3Sdougm * It is the same group but could have changed 1236546405c3Sdougm * options. Make sure we include the group's 1237546405c3Sdougm * properties so we don't end up moving them to 1238546405c3Sdougm * the share inadvertantly. The last arg being 1239546405c3Sdougm * true says to get the inherited properties as well 1240546405c3Sdougm * as the local properties. 1241546405c3Sdougm */ 124225a68471Sdougm oldprops = sa_proto_legacy_format(list->fstype, share, 124325a68471Sdougm B_TRUE); 124425a68471Sdougm 124525a68471Sdougm if (oldprops == NULL) 124625a68471Sdougm continue; 124725a68471Sdougm 12486185db85Sdougm if (list->options != NULL && 124925a68471Sdougm strcmp(oldprops, list->options) != 0) { 125025a68471Sdougm sa_optionset_t opts; 125125a68471Sdougm sa_security_t secs; 125225a68471Sdougm 125325a68471Sdougm /* possibly different values */ 125425a68471Sdougm opts = sa_get_optionset((sa_group_t) 125525a68471Sdougm share, list->fstype); 125625a68471Sdougm (void) sa_destroy_optionset(opts); 125725a68471Sdougm 125825a68471Sdougm for (secs = sa_get_security( 125925a68471Sdougm (sa_group_t)share, NULL, list->fstype); 126025a68471Sdougm secs != NULL; 126125a68471Sdougm secs = sa_get_security((sa_group_t)share, 126225a68471Sdougm NULL, list->fstype)) { 126325a68471Sdougm (void) sa_destroy_security( 126425a68471Sdougm secs); 126525a68471Sdougm } 126625a68471Sdougm (void) sa_parse_legacy_options(share, 126725a68471Sdougm list->options, list->fstype); 12686185db85Sdougm } 1269a99982a7Sdougm sa_format_free(oldprops); 12706185db85Sdougm } 12716185db85Sdougm } 12726185db85Sdougm dfs_free_list(head); 12736185db85Sdougm } 12746185db85Sdougm 12756185db85Sdougm /* 12766185db85Sdougm * legacy_removes(group, file) 12776185db85Sdougm * 12786185db85Sdougm * Find any shares that are "missing" from the legacy file. These 12796185db85Sdougm * should be removed from the configuration since they are likely from 12806185db85Sdougm * a legacy app or the admin modified the dfstab file directly. We 12816185db85Sdougm * have to support this even if it is not the recommended way to do 12826185db85Sdougm * things. 12836185db85Sdougm */ 12846185db85Sdougm 12856185db85Sdougm static void 12866185db85Sdougm legacy_removes(sa_group_t group, char *file) 12876185db85Sdougm { 12886185db85Sdougm sa_share_t share; 12896185db85Sdougm char *path; 12906185db85Sdougm xfs_sharelist_t *list, *item; 12916185db85Sdougm FILE *dfstab; 12926185db85Sdougm 12936185db85Sdougm dfstab = fopen(file, "r"); 12946185db85Sdougm if (dfstab != NULL) { 129525a68471Sdougm list = getdfstab(dfstab); 129625a68471Sdougm (void) fclose(dfstab); 1297f345c0beSdougm retry: 129825a68471Sdougm for (share = sa_get_share(group, NULL); 129925a68471Sdougm share != NULL; 130025a68471Sdougm share = sa_get_next_share(share)) { 130125a68471Sdougm /* now see if the share is in the dfstab file */ 130225a68471Sdougm path = sa_get_share_attr(share, "path"); 130325a68471Sdougm if (path != NULL) { 130425a68471Sdougm item = finddfsentry(list, path); 130525a68471Sdougm sa_free_attr_string(path); 130625a68471Sdougm if (item == NULL) { 130725a68471Sdougm /* The share was removed this way */ 130825a68471Sdougm (void) sa_remove_share(share); 130925a68471Sdougm 131025a68471Sdougm /* 131125a68471Sdougm * Start over since the list was broken 131225a68471Sdougm */ 131325a68471Sdougm goto retry; 131425a68471Sdougm } 131525a68471Sdougm } 13166185db85Sdougm } 131725a68471Sdougm if (list != NULL) 131825a68471Sdougm dfs_free_list(list); 13196185db85Sdougm } 13206185db85Sdougm } 13216185db85Sdougm 13226185db85Sdougm /* 13236185db85Sdougm * getlegacyconfig(path, root) 13246185db85Sdougm * 13256185db85Sdougm * Parse dfstab and build the legacy configuration. This only gets 13266185db85Sdougm * called when a change was detected. 13276185db85Sdougm */ 13286185db85Sdougm 13296185db85Sdougm void 1330549ec3ffSdougm getlegacyconfig(sa_handle_t handle, char *path, xmlNodePtr *root) 13316185db85Sdougm { 13326185db85Sdougm sa_group_t defgroup; 13336185db85Sdougm 13346185db85Sdougm if (root != NULL) { 133525a68471Sdougm if (*root == NULL) 133625a68471Sdougm *root = xmlNewNode(NULL, (xmlChar *)"sharecfg"); 133725a68471Sdougm if (*root != NULL) { 133825a68471Sdougm if (strcmp(path, SA_LEGACY_DFSTAB) == 0) { 133925a68471Sdougm /* 134025a68471Sdougm * Walk the default shares and find anything 134125a68471Sdougm * missing. we do this first to make sure it 134225a68471Sdougm * is cleaned up since there may be legacy 134325a68471Sdougm * code add/del via dfstab and we need to 134425a68471Sdougm * cleanup SMF. 134525a68471Sdougm */ 134625a68471Sdougm defgroup = sa_get_group(handle, "default"); 134725a68471Sdougm if (defgroup != NULL) 134825a68471Sdougm legacy_removes(defgroup, path); 134925a68471Sdougm /* Parse the dfstab and add anything new */ 135025a68471Sdougm parse_dfstab(handle, path, *root); 135125a68471Sdougm } 13526185db85Sdougm } 13536185db85Sdougm } 13546185db85Sdougm } 13556185db85Sdougm 13561cea05afSdougm /* 13571cea05afSdougm * get_share_list(&err) 13581cea05afSdougm * 13591cea05afSdougm * Get a linked list of all the shares on the system from 13601cea05afSdougm * /etc/dfs/sharetab. This is partially copied from libfsmgt which we 13611cea05afSdougm * can't use due to package dependencies. 13621cea05afSdougm */ 13631cea05afSdougm static xfs_sharelist_t * 13641cea05afSdougm get_share_list(int *errp) 13651cea05afSdougm { 13661cea05afSdougm xfs_sharelist_t *newp; 13671cea05afSdougm xfs_sharelist_t *headp; 13681cea05afSdougm xfs_sharelist_t *tailp; 13691cea05afSdougm FILE *fp; 13701cea05afSdougm 13711cea05afSdougm headp = NULL; 13721cea05afSdougm tailp = NULL; 13731cea05afSdougm 13741cea05afSdougm if ((fp = fopen(SHARETAB, "r")) != NULL) { 13751cea05afSdougm struct share *sharetab_entry; 13761cea05afSdougm 13778d7e4166Sjose borrego (void) lockf(fileno(fp), F_LOCK, 0); 13788d7e4166Sjose borrego (void) mutex_lock(&sharetab_lock); 13798d7e4166Sjose borrego 13801cea05afSdougm while (getshare(fp, &sharetab_entry) > 0) { 138125a68471Sdougm newp = alloc_sharelist(); 13828d7e4166Sjose borrego if (newp == NULL) { 13838d7e4166Sjose borrego (void) mutex_unlock(&sharetab_lock); 13848d7e4166Sjose borrego (void) lockf(fileno(fp), F_ULOCK, 0); 138525a68471Sdougm goto err; 13868d7e4166Sjose borrego } 13871cea05afSdougm 13881cea05afSdougm /* 138925a68471Sdougm * Link into the list here so we don't leak 13901cea05afSdougm * memory on a failure from strdup(). 13911cea05afSdougm */ 139225a68471Sdougm if (headp == NULL) { 139325a68471Sdougm headp = newp; 139425a68471Sdougm tailp = newp; 139525a68471Sdougm } else { 139625a68471Sdougm tailp->next = newp; 139725a68471Sdougm tailp = newp; 139825a68471Sdougm } 139925a68471Sdougm 140025a68471Sdougm newp->path = strdup(sharetab_entry->sh_path); 140125a68471Sdougm newp->resource = strdup(sharetab_entry->sh_res); 140225a68471Sdougm newp->fstype = strdup(sharetab_entry->sh_fstype); 140325a68471Sdougm newp->options = strdup(sharetab_entry->sh_opts); 140425a68471Sdougm newp->description = strdup(sharetab_entry->sh_descr); 14058d7e4166Sjose borrego 14068d7e4166Sjose borrego if (newp->path == NULL || newp->resource == NULL || 14078d7e4166Sjose borrego newp->fstype == NULL || newp->options == NULL || 14088d7e4166Sjose borrego newp->description == NULL) { 14098d7e4166Sjose borrego (void) mutex_unlock(&sharetab_lock); 14108d7e4166Sjose borrego (void) lockf(fileno(fp), F_ULOCK, 0); 141125a68471Sdougm goto err; 14128d7e4166Sjose borrego } 14131cea05afSdougm } 14148d7e4166Sjose borrego 14158d7e4166Sjose borrego (void) mutex_unlock(&sharetab_lock); 1416a99982a7Sdougm (void) lockf(fileno(fp), F_ULOCK, 0); 14171cea05afSdougm (void) fclose(fp); 14181cea05afSdougm } else { 141925a68471Sdougm *errp = errno; 14201cea05afSdougm } 14211cea05afSdougm 14221cea05afSdougm /* 14231cea05afSdougm * Caller must free the mount list 14241cea05afSdougm */ 14251cea05afSdougm return (headp); 14261cea05afSdougm err: 14271cea05afSdougm /* 14281cea05afSdougm * Out of memory so cleanup and leave. 14291cea05afSdougm */ 14301cea05afSdougm dfs_free_list(headp); 14311cea05afSdougm (void) fclose(fp); 14321cea05afSdougm return (NULL); 14331cea05afSdougm } 14341cea05afSdougm 14356185db85Sdougm /* 1436549ec3ffSdougm * parse_sharetab(handle) 14376185db85Sdougm * 14381cea05afSdougm * Read the /etc/dfs/sharetab file and see which entries don't exist 14391cea05afSdougm * in the repository. These shares are marked transient. We also need 14401cea05afSdougm * to see if they are ZFS shares since ZFS bypasses the SMF 14411cea05afSdougm * repository. 14426185db85Sdougm */ 14436185db85Sdougm 14446185db85Sdougm int 1445549ec3ffSdougm parse_sharetab(sa_handle_t handle) 14466185db85Sdougm { 14471cea05afSdougm xfs_sharelist_t *list, *tmplist; 14486185db85Sdougm int err = 0; 14496185db85Sdougm sa_share_t share; 14506185db85Sdougm sa_group_t group; 14516185db85Sdougm sa_group_t lgroup; 14526185db85Sdougm char *groupname; 14536185db85Sdougm int legacy = 0; 1454da6c28aaSamw char shareopts[MAXNAMLEN]; 14556185db85Sdougm 14561cea05afSdougm list = get_share_list(&err); 14576185db85Sdougm if (list == NULL) 145825a68471Sdougm return (legacy); 14596185db85Sdougm 1460549ec3ffSdougm lgroup = sa_get_group(handle, "default"); 14616185db85Sdougm 14626185db85Sdougm for (tmplist = list; tmplist != NULL; tmplist = tmplist->next) { 146325a68471Sdougm group = NULL; 146425a68471Sdougm share = sa_find_share(handle, tmplist->path); 146525a68471Sdougm if (share != NULL) { 146625a68471Sdougm /* 146725a68471Sdougm * If this is a legacy share, mark as shared so we 146825a68471Sdougm * only update sharetab appropriately. We also keep 146925a68471Sdougm * the sharetab options in order to display for legacy 147025a68471Sdougm * share with no arguments. 147125a68471Sdougm */ 147225a68471Sdougm set_node_attr(share, "shared", "true"); 1473da6c28aaSamw (void) snprintf(shareopts, MAXNAMLEN, "shareopts-%s", 1474da6c28aaSamw tmplist->fstype); 1475da6c28aaSamw set_node_attr(share, shareopts, tmplist->options); 147625a68471Sdougm continue; 147725a68471Sdougm } 147825a68471Sdougm 14796185db85Sdougm /* 148025a68471Sdougm * This share is transient so needs to be 14816185db85Sdougm * added. Initially, this will be under 14826185db85Sdougm * default(legacy) unless it is a ZFS 14836185db85Sdougm * share. If zfs, we need a zfs group. 14846185db85Sdougm */ 14856185db85Sdougm if (tmplist->resource != NULL && 14866185db85Sdougm (groupname = strchr(tmplist->resource, '@')) != NULL) { 148725a68471Sdougm /* There is a defined group */ 148825a68471Sdougm *groupname++ = '\0'; 148925a68471Sdougm group = sa_get_group(handle, groupname); 14906185db85Sdougm if (group != NULL) { 1491da6c28aaSamw share = _sa_add_share(group, tmplist->path, 1492da6c28aaSamw SA_SHARE_TRANSIENT, &err, 1493da6c28aaSamw (uint64_t)SA_FEATURE_NONE); 149425a68471Sdougm } else { 149525a68471Sdougm /* 149625a68471Sdougm * While this case shouldn't 149725a68471Sdougm * occur very often, it does 149825a68471Sdougm * occur out of a "zfs set 149925a68471Sdougm * sharenfs=off" when the 150025a68471Sdougm * dataset is also set to 150125a68471Sdougm * canmount=off. A warning 150225a68471Sdougm * will then cause the zfs 150325a68471Sdougm * command to abort. Since we 150425a68471Sdougm * add it to the default list, 150525a68471Sdougm * everything works properly 150625a68471Sdougm * anyway and the library 150725a68471Sdougm * doesn't need to give a 150825a68471Sdougm * warning. 150925a68471Sdougm */ 151025a68471Sdougm share = _sa_add_share(lgroup, 151125a68471Sdougm tmplist->path, SA_SHARE_TRANSIENT, 1512da6c28aaSamw &err, (uint64_t)SA_FEATURE_NONE); 151325a68471Sdougm } 151425a68471Sdougm } else { 151525a68471Sdougm if (sa_zfs_is_shared(handle, tmplist->path)) { 151625a68471Sdougm group = sa_get_group(handle, "zfs"); 151725a68471Sdougm if (group == NULL) { 151825a68471Sdougm group = sa_create_group(handle, 151925a68471Sdougm "zfs", &err); 152025a68471Sdougm if (group == NULL && 152125a68471Sdougm err == SA_NO_PERMISSION) { 152225a68471Sdougm group = _sa_create_group( 152325a68471Sdougm (sa_handle_impl_t) 152425a68471Sdougm handle, 152525a68471Sdougm "zfs"); 152625a68471Sdougm } 152725a68471Sdougm if (group != NULL) { 152825a68471Sdougm (void) sa_create_optionset( 152925a68471Sdougm group, tmplist->fstype); 153025a68471Sdougm (void) sa_set_group_attr(group, 153125a68471Sdougm "zfs", "true"); 153225a68471Sdougm } 153325a68471Sdougm } 153425a68471Sdougm if (group != NULL) { 153525a68471Sdougm share = _sa_add_share(group, 153625a68471Sdougm tmplist->path, SA_SHARE_TRANSIENT, 1537da6c28aaSamw &err, (uint64_t)SA_FEATURE_NONE); 153825a68471Sdougm } 153925a68471Sdougm } else { 154025a68471Sdougm share = _sa_add_share(lgroup, tmplist->path, 1541da6c28aaSamw SA_SHARE_TRANSIENT, &err, 1542da6c28aaSamw (uint64_t)SA_FEATURE_NONE); 15436185db85Sdougm } 15446185db85Sdougm } 15456185db85Sdougm if (share == NULL) 154625a68471Sdougm (void) printf(dgettext(TEXT_DOMAIN, 154725a68471Sdougm "Problem with transient: %s\n"), sa_errorstr(err)); 15486185db85Sdougm if (share != NULL) 154925a68471Sdougm set_node_attr(share, "shared", "true"); 15506185db85Sdougm if (err == SA_OK) { 155125a68471Sdougm if (tmplist->options != NULL && 155225a68471Sdougm strlen(tmplist->options) > 0) { 155325a68471Sdougm (void) sa_parse_legacy_options(share, 155425a68471Sdougm tmplist->options, tmplist->fstype); 155525a68471Sdougm } 155625a68471Sdougm if (tmplist->resource != NULL && 155725a68471Sdougm strcmp(tmplist->resource, "-") != 0) 155825a68471Sdougm set_node_attr(share, "resource", 155925a68471Sdougm tmplist->resource); 156025a68471Sdougm if (tmplist->description != NULL) { 156125a68471Sdougm xmlNodePtr node; 156225a68471Sdougm node = xmlNewChild((xmlNodePtr)share, NULL, 156325a68471Sdougm (xmlChar *)"description", NULL); 156425a68471Sdougm xmlNodeSetContent(node, 156525a68471Sdougm (xmlChar *)tmplist->description); 156625a68471Sdougm } 156725a68471Sdougm legacy = 1; 15686185db85Sdougm } 15696185db85Sdougm } 15701cea05afSdougm dfs_free_list(list); 15716185db85Sdougm return (legacy); 15726185db85Sdougm } 15736185db85Sdougm 15746185db85Sdougm /* 157525a68471Sdougm * Get the transient shares from the sharetab (or other) file. since 15766185db85Sdougm * these are transient, they only appear in the working file and not 15776185db85Sdougm * in a repository. 15786185db85Sdougm */ 15796185db85Sdougm int 1580549ec3ffSdougm gettransients(sa_handle_impl_t ihandle, xmlNodePtr *root) 15816185db85Sdougm { 15826185db85Sdougm int legacy = 0; 1583da6c28aaSamw int numproto; 1584da6c28aaSamw char **protocols = NULL; 1585da6c28aaSamw int i; 15866185db85Sdougm 15876185db85Sdougm if (root != NULL) { 158825a68471Sdougm if (*root == NULL) 158925a68471Sdougm *root = xmlNewNode(NULL, (xmlChar *)"sharecfg"); 1590da6c28aaSamw if (*root != NULL) { 159125a68471Sdougm legacy = parse_sharetab(ihandle); 1592da6c28aaSamw numproto = sa_get_protocols(&protocols); 1593da6c28aaSamw for (i = 0; i < numproto; i++) 1594da6c28aaSamw legacy |= sa_proto_get_transients( 1595da6c28aaSamw (sa_handle_t)ihandle, protocols[i]); 1596da6c28aaSamw if (protocols != NULL) 1597da6c28aaSamw free(protocols); 1598da6c28aaSamw } 15996185db85Sdougm } 16006185db85Sdougm return (legacy); 16016185db85Sdougm } 16026185db85Sdougm 16036185db85Sdougm /* 16046185db85Sdougm * sa_has_prop(optionset, prop) 16056185db85Sdougm * 16066185db85Sdougm * Is the specified property a member of the optionset? 16076185db85Sdougm */ 16086185db85Sdougm 16096185db85Sdougm int 16106185db85Sdougm sa_has_prop(sa_optionset_t optionset, sa_property_t prop) 16116185db85Sdougm { 16126185db85Sdougm char *name; 16136185db85Sdougm sa_property_t otherprop; 16146185db85Sdougm int result = 0; 16156185db85Sdougm 16166185db85Sdougm if (optionset != NULL) { 161725a68471Sdougm name = sa_get_property_attr(prop, "type"); 161825a68471Sdougm if (name != NULL) { 161925a68471Sdougm otherprop = sa_get_property(optionset, name); 162025a68471Sdougm if (otherprop != NULL) 162125a68471Sdougm result = 1; 162225a68471Sdougm sa_free_attr_string(name); 162325a68471Sdougm } 16246185db85Sdougm } 16256185db85Sdougm return (result); 16266185db85Sdougm } 16276185db85Sdougm 16286185db85Sdougm /* 16296185db85Sdougm * Update legacy files 16306185db85Sdougm * 16316185db85Sdougm * Provides functions to add/remove/modify individual entries 16326185db85Sdougm * in dfstab and sharetab 16336185db85Sdougm */ 16346185db85Sdougm 16356185db85Sdougm void 1636549ec3ffSdougm update_legacy_config(sa_handle_t handle) 16376185db85Sdougm { 16386185db85Sdougm /* 16396185db85Sdougm * no longer used -- this is a placeholder in case we need to 16406185db85Sdougm * add it back later. 16416185db85Sdougm */ 1642549ec3ffSdougm #ifdef lint 1643549ec3ffSdougm handle = handle; 1644549ec3ffSdougm #endif 16456185db85Sdougm } 16466185db85Sdougm 16476185db85Sdougm /* 1648687915e9Sdougm * sa_valid_property(handle, object, proto, property) 16496185db85Sdougm * 16506185db85Sdougm * check to see if the specified property is valid relative to the 16516185db85Sdougm * specified protocol. The protocol plugin is called to do the work. 16526185db85Sdougm */ 16536185db85Sdougm 16546185db85Sdougm int 1655687915e9Sdougm sa_valid_property(sa_handle_t handle, void *object, char *proto, 1656687915e9Sdougm sa_property_t property) 16576185db85Sdougm { 16586185db85Sdougm int ret = SA_OK; 16596185db85Sdougm 16606185db85Sdougm if (proto != NULL && property != NULL) { 1661687915e9Sdougm ret = sa_proto_valid_prop(handle, proto, property, object); 16626185db85Sdougm } 16636185db85Sdougm 16646185db85Sdougm return (ret); 16656185db85Sdougm } 16666185db85Sdougm 16676185db85Sdougm /* 16686185db85Sdougm * sa_fstype(path) 16696185db85Sdougm * 16706185db85Sdougm * Given path, return the string representing the path's file system 16716185db85Sdougm * type. This is used to discover ZFS shares. 16726185db85Sdougm */ 16736185db85Sdougm 16746185db85Sdougm char * 16756185db85Sdougm sa_fstype(char *path) 16766185db85Sdougm { 16776185db85Sdougm int err; 16786185db85Sdougm struct stat st; 16796185db85Sdougm 16806185db85Sdougm err = stat(path, &st); 168125a68471Sdougm if (err < 0) 168225a68471Sdougm err = SA_NO_SUCH_PATH; 168325a68471Sdougm else 168425a68471Sdougm err = SA_OK; 168525a68471Sdougm 168625a68471Sdougm /* 168725a68471Sdougm * If we have a valid path at this point ret, return the fstype. 168825a68471Sdougm */ 168925a68471Sdougm if (err == SA_OK) 169025a68471Sdougm return (strdup(st.st_fstype)); 169125a68471Sdougm 16926185db85Sdougm return (NULL); 16936185db85Sdougm } 16946185db85Sdougm 16956185db85Sdougm void 16966185db85Sdougm sa_free_fstype(char *type) 16976185db85Sdougm { 16986185db85Sdougm free(type); 16996185db85Sdougm } 17006185db85Sdougm 17016185db85Sdougm /* 17026185db85Sdougm * sa_get_derived_optionset(object, proto, hier) 17036185db85Sdougm * 17046185db85Sdougm * Work backward to the top of the share object tree and start 17056185db85Sdougm * copying protocol specific optionsets into a newly created 17066185db85Sdougm * optionset that doesn't have a parent (it will be freed 1707da6c28aaSamw * later). This provides for the property inheritance model. That 17086185db85Sdougm * is, properties closer to the share take precedence over group 17096185db85Sdougm * level. This also provides for groups of groups in the future. 17106185db85Sdougm */ 17116185db85Sdougm 17126185db85Sdougm sa_optionset_t 17136185db85Sdougm sa_get_derived_optionset(void *object, char *proto, int hier) 17146185db85Sdougm { 17156185db85Sdougm sa_optionset_t newoptionset; 17166185db85Sdougm sa_optionset_t optionset; 17176185db85Sdougm sa_group_t group; 17186185db85Sdougm 17196185db85Sdougm if (hier && 17206185db85Sdougm (group = sa_get_parent_group((sa_share_t)object)) != NULL) { 172125a68471Sdougm newoptionset = sa_get_derived_optionset((void *)group, proto, 172225a68471Sdougm hier); 17236185db85Sdougm } else { 172425a68471Sdougm newoptionset = (sa_optionset_t)xmlNewNode(NULL, 172525a68471Sdougm (xmlChar *)"optionset"); 172625a68471Sdougm if (newoptionset != NULL) { 172725a68471Sdougm sa_set_optionset_attr(newoptionset, "type", proto); 172825a68471Sdougm } 17296185db85Sdougm } 173025a68471Sdougm /* Dont' do anything if memory wasn't allocated */ 17316185db85Sdougm if (newoptionset == NULL) 173225a68471Sdougm return (NULL); 17336185db85Sdougm 173425a68471Sdougm /* Found the top so working back down the stack */ 17356185db85Sdougm optionset = sa_get_optionset((sa_optionset_t)object, proto); 17366185db85Sdougm if (optionset != NULL) { 173725a68471Sdougm sa_property_t prop; 173825a68471Sdougm /* add optionset to the newoptionset */ 173925a68471Sdougm for (prop = sa_get_property(optionset, NULL); 174025a68471Sdougm prop != NULL; 174125a68471Sdougm prop = sa_get_next_property(prop)) { 174225a68471Sdougm sa_property_t newprop; 174325a68471Sdougm char *name; 174425a68471Sdougm char *value; 174525a68471Sdougm name = sa_get_property_attr(prop, "type"); 174625a68471Sdougm value = sa_get_property_attr(prop, "value"); 174725a68471Sdougm if (name == NULL) 174825a68471Sdougm continue; 174925a68471Sdougm newprop = sa_get_property(newoptionset, name); 175025a68471Sdougm /* Replace the value with the new value */ 175125a68471Sdougm if (newprop != NULL) { 175225a68471Sdougm /* 175325a68471Sdougm * Only set if value is non NULL, old value ok 175425a68471Sdougm * if it is NULL. 175525a68471Sdougm */ 175625a68471Sdougm if (value != NULL) 175725a68471Sdougm set_node_attr(newprop, "value", value); 175825a68471Sdougm } else { 175925a68471Sdougm /* an entirely new property */ 176025a68471Sdougm if (value != NULL) { 176125a68471Sdougm newprop = sa_create_property(name, 176225a68471Sdougm value); 176325a68471Sdougm if (newprop != NULL) { 176425a68471Sdougm newprop = (sa_property_t) 176525a68471Sdougm xmlAddChild( 176625a68471Sdougm (xmlNodePtr)newoptionset, 176725a68471Sdougm (xmlNodePtr)newprop); 176825a68471Sdougm } 176925a68471Sdougm } 17706185db85Sdougm } 177125a68471Sdougm sa_free_attr_string(name); 177225a68471Sdougm 177325a68471Sdougm if (value != NULL) 177425a68471Sdougm sa_free_attr_string(value); 17756185db85Sdougm } 17766185db85Sdougm } 17776185db85Sdougm return (newoptionset); 17786185db85Sdougm } 17796185db85Sdougm 17806185db85Sdougm void 17816185db85Sdougm sa_free_derived_optionset(sa_optionset_t optionset) 17826185db85Sdougm { 178325a68471Sdougm /* While it shouldn't be linked, it doesn't hurt */ 17846185db85Sdougm if (optionset != NULL) { 178525a68471Sdougm xmlUnlinkNode((xmlNodePtr) optionset); 178625a68471Sdougm xmlFreeNode((xmlNodePtr) optionset); 17876185db85Sdougm } 17886185db85Sdougm } 17896185db85Sdougm 17906185db85Sdougm /* 17916185db85Sdougm * sa_get_all_security_types(object, proto, hier) 17926185db85Sdougm * 179325a68471Sdougm * Find all the security types set for this object. This is 17946185db85Sdougm * preliminary to getting a derived security set. The return value is an 17956185db85Sdougm * optionset containg properties which are the sectype values found by 1796da6c28aaSamw * walking up the XML document structure. The returned optionset 17976185db85Sdougm * is a derived optionset. 17986185db85Sdougm * 17996185db85Sdougm * If hier is 0, only look at object. If non-zero, walk up the tree. 18006185db85Sdougm */ 18016185db85Sdougm sa_optionset_t 18026185db85Sdougm sa_get_all_security_types(void *object, char *proto, int hier) 18036185db85Sdougm { 18046185db85Sdougm sa_optionset_t options; 18056185db85Sdougm sa_security_t security; 18066185db85Sdougm sa_group_t group; 18076185db85Sdougm sa_property_t prop; 18086185db85Sdougm 18096185db85Sdougm options = NULL; 18106185db85Sdougm 18116185db85Sdougm if (hier && 181225a68471Sdougm (group = sa_get_parent_group((sa_share_t)object)) != NULL) 181325a68471Sdougm options = sa_get_all_security_types((void *)group, proto, hier); 181425a68471Sdougm else 181525a68471Sdougm options = (sa_optionset_t)xmlNewNode(NULL, 181625a68471Sdougm (xmlChar *)"optionset"); 181725a68471Sdougm 181825a68471Sdougm if (options == NULL) 181925a68471Sdougm return (options); 182025a68471Sdougm 182125a68471Sdougm /* Hit the top so collect the security types working back. */ 182225a68471Sdougm for (security = sa_get_security((sa_group_t)object, NULL, NULL); 182325a68471Sdougm security != NULL; 182425a68471Sdougm security = sa_get_next_security(security)) { 18256185db85Sdougm char *type; 18266185db85Sdougm char *sectype; 18276185db85Sdougm 18286185db85Sdougm type = sa_get_security_attr(security, "type"); 18296185db85Sdougm if (type != NULL) { 183025a68471Sdougm if (strcmp(type, proto) != 0) { 183125a68471Sdougm sa_free_attr_string(type); 183225a68471Sdougm continue; 183325a68471Sdougm } 183425a68471Sdougm sectype = sa_get_security_attr(security, "sectype"); 183525a68471Sdougm if (sectype != NULL) { 183625a68471Sdougm /* 183725a68471Sdougm * Have a security type, check to see if 183825a68471Sdougm * already present in optionset and add if it 183925a68471Sdougm * isn't. 184025a68471Sdougm */ 184125a68471Sdougm if (sa_get_property(options, sectype) == NULL) { 184225a68471Sdougm prop = sa_create_property(sectype, 184325a68471Sdougm "true"); 184425a68471Sdougm if (prop != NULL) 184525a68471Sdougm prop = (sa_property_t) 184625a68471Sdougm xmlAddChild( 184725a68471Sdougm (xmlNodePtr)options, 184825a68471Sdougm (xmlNodePtr)prop); 184925a68471Sdougm } 185025a68471Sdougm sa_free_attr_string(sectype); 18516185db85Sdougm } 185225a68471Sdougm sa_free_attr_string(type); 18536185db85Sdougm } 18546185db85Sdougm } 185525a68471Sdougm 18566185db85Sdougm return (options); 18576185db85Sdougm } 18586185db85Sdougm 18596185db85Sdougm /* 18606185db85Sdougm * sa_get_derived_security(object, sectype, proto, hier) 18616185db85Sdougm * 18626185db85Sdougm * Get the derived security(named optionset) for the object given the 18636185db85Sdougm * sectype and proto. If hier is non-zero, walk up the tree to get all 18646185db85Sdougm * properties defined for this object, otherwise just those on the 18656185db85Sdougm * object. 18666185db85Sdougm */ 18676185db85Sdougm 18686185db85Sdougm sa_security_t 18696185db85Sdougm sa_get_derived_security(void *object, char *sectype, char *proto, int hier) 18706185db85Sdougm { 18716185db85Sdougm sa_security_t newsecurity; 18726185db85Sdougm sa_security_t security; 18736185db85Sdougm sa_group_t group; 187425a68471Sdougm sa_property_t prop; 18756185db85Sdougm 18766185db85Sdougm if (hier && 18776185db85Sdougm (group = sa_get_parent_group((sa_share_t)object)) != NULL) { 187825a68471Sdougm newsecurity = sa_get_derived_security((void *)group, 187925a68471Sdougm sectype, proto, hier); 18806185db85Sdougm } else { 188125a68471Sdougm newsecurity = (sa_security_t)xmlNewNode(NULL, 188225a68471Sdougm (xmlChar *)"security"); 188325a68471Sdougm if (newsecurity != NULL) { 188425a68471Sdougm sa_set_security_attr(newsecurity, "type", proto); 188525a68471Sdougm sa_set_security_attr(newsecurity, "sectype", sectype); 188625a68471Sdougm } 18876185db85Sdougm } 188825a68471Sdougm /* Don't do anything if memory wasn't allocated */ 18896185db85Sdougm if (newsecurity == NULL) 189025a68471Sdougm return (newsecurity); 18916185db85Sdougm 189225a68471Sdougm /* Found the top so working back down the stack. */ 18936185db85Sdougm security = sa_get_security((sa_security_t)object, sectype, proto); 189425a68471Sdougm if (security == NULL) 189525a68471Sdougm return (newsecurity); 189625a68471Sdougm 189725a68471Sdougm /* add security to the newsecurity */ 189825a68471Sdougm for (prop = sa_get_property(security, NULL); 189925a68471Sdougm prop != NULL; prop = sa_get_next_property(prop)) { 19006185db85Sdougm sa_property_t newprop; 19016185db85Sdougm char *name; 19026185db85Sdougm char *value; 19036185db85Sdougm name = sa_get_property_attr(prop, "type"); 19046185db85Sdougm value = sa_get_property_attr(prop, "value"); 19056185db85Sdougm if (name != NULL) { 190625a68471Sdougm newprop = sa_get_property(newsecurity, name); 190725a68471Sdougm /* Replace the value with the new value */ 190825a68471Sdougm if (newprop != NULL) { 190925a68471Sdougm /* 191097df5ac9Sdougm * Only set if value is non NULL, old 191197df5ac9Sdougm * value ok if it is NULL. The value 191297df5ac9Sdougm * must be associated with the "value" 191397df5ac9Sdougm * tag within XML. 191425a68471Sdougm */ 191525a68471Sdougm if (value != NULL) 191697df5ac9Sdougm set_node_attr(newprop, "value", value); 191725a68471Sdougm } else { 191825a68471Sdougm /* An entirely new property */ 191925a68471Sdougm if (value != NULL) { 192025a68471Sdougm newprop = sa_create_property(name, 192125a68471Sdougm value); 192225a68471Sdougm newprop = (sa_property_t) 192325a68471Sdougm xmlAddChild((xmlNodePtr)newsecurity, 19246185db85Sdougm (xmlNodePtr)newprop); 192525a68471Sdougm } 19266185db85Sdougm } 192725a68471Sdougm sa_free_attr_string(name); 19286185db85Sdougm } 19296185db85Sdougm if (value != NULL) 193025a68471Sdougm sa_free_attr_string(value); 19316185db85Sdougm } 19326185db85Sdougm return (newsecurity); 19336185db85Sdougm } 19346185db85Sdougm 19356185db85Sdougm void 19366185db85Sdougm sa_free_derived_security(sa_security_t security) 19376185db85Sdougm { 19386185db85Sdougm /* while it shouldn't be linked, it doesn't hurt */ 19396185db85Sdougm if (security != NULL) { 194025a68471Sdougm xmlUnlinkNode((xmlNodePtr)security); 194125a68471Sdougm xmlFreeNode((xmlNodePtr)security); 19426185db85Sdougm } 19436185db85Sdougm } 19446185db85Sdougm 19456185db85Sdougm /* 19466185db85Sdougm * sharetab utility functions 19476185db85Sdougm * 194825a68471Sdougm * Makes use of the original sharetab.c from fs.d/nfs/lib 19496185db85Sdougm */ 19506185db85Sdougm 19516185db85Sdougm /* 1952ecd6cf80Smarks * sa_fillshare(share, proto, sh) 19536185db85Sdougm * 19546185db85Sdougm * Fill the struct share with values obtained from the share object. 19556185db85Sdougm */ 1956ecd6cf80Smarks void 1957ecd6cf80Smarks sa_fillshare(sa_share_t share, char *proto, struct share *sh) 19586185db85Sdougm { 19596185db85Sdougm char *groupname = NULL; 19606185db85Sdougm char *value; 19616185db85Sdougm sa_group_t group; 19626185db85Sdougm char *buff; 19636185db85Sdougm char *zfs; 1964da6c28aaSamw sa_resource_t resource; 1965da6c28aaSamw char *rsrcname = NULL; 1966da6c28aaSamw char *defprop; 1967da6c28aaSamw 1968da6c28aaSamw /* 1969da6c28aaSamw * We only want to deal with the path level shares for the 1970da6c28aaSamw * sharetab file. If a resource, get the parent. 1971da6c28aaSamw */ 1972da6c28aaSamw if (sa_is_resource(share)) { 1973da6c28aaSamw resource = (sa_resource_t)share; 1974da6c28aaSamw share = sa_get_resource_parent(resource); 1975da6c28aaSamw rsrcname = sa_get_resource_attr(resource, "name"); 1976da6c28aaSamw } 19776185db85Sdougm 19786185db85Sdougm group = sa_get_parent_group(share); 19796185db85Sdougm if (group != NULL) { 198025a68471Sdougm zfs = sa_get_group_attr(group, "zfs"); 198125a68471Sdougm groupname = sa_get_group_attr(group, "name"); 19826185db85Sdougm 198325a68471Sdougm if (groupname != NULL && 198425a68471Sdougm (strcmp(groupname, "default") == 0 || zfs != NULL)) { 198525a68471Sdougm /* 198625a68471Sdougm * since the groupname is either "default" or the 198725a68471Sdougm * group is a ZFS group, we don't want to keep 198825a68471Sdougm * groupname. We do want it if it is any other type of 198925a68471Sdougm * group. 199025a68471Sdougm */ 199125a68471Sdougm sa_free_attr_string(groupname); 199225a68471Sdougm groupname = NULL; 199325a68471Sdougm } 199425a68471Sdougm if (zfs != NULL) 199525a68471Sdougm sa_free_attr_string(zfs); 19966185db85Sdougm } 19976185db85Sdougm 19986185db85Sdougm value = sa_get_share_attr(share, "path"); 19996185db85Sdougm if (value != NULL) { 200025a68471Sdougm sh->sh_path = strdup(value); 200125a68471Sdougm sa_free_attr_string(value); 20026185db85Sdougm } 20036185db85Sdougm 2004da6c28aaSamw if (rsrcname != NULL || groupname != NULL) { 200525a68471Sdougm int len = 0; 200625a68471Sdougm 2007da6c28aaSamw if (rsrcname != NULL) 2008da6c28aaSamw len += strlen(rsrcname); 200925a68471Sdougm if (groupname != NULL) 201025a68471Sdougm len += strlen(groupname); 201125a68471Sdougm len += 3; /* worst case */ 201225a68471Sdougm buff = malloc(len); 201325a68471Sdougm (void) snprintf(buff, len, "%s%s%s", 2014da6c28aaSamw (rsrcname != NULL && 2015da6c28aaSamw strlen(rsrcname) > 0) ? rsrcname : "-", 20166185db85Sdougm groupname != NULL ? "@" : "", 20176185db85Sdougm groupname != NULL ? groupname : ""); 201825a68471Sdougm sh->sh_res = buff; 2019da6c28aaSamw if (rsrcname != NULL) 2020da6c28aaSamw sa_free_attr_string(rsrcname); 2021da6c28aaSamw if (groupname != NULL) 202225a68471Sdougm sa_free_attr_string(groupname); 20236185db85Sdougm } else { 202425a68471Sdougm sh->sh_res = strdup("-"); 20256185db85Sdougm } 20266185db85Sdougm 2027da6c28aaSamw /* 2028da6c28aaSamw * Get correct default prop string. NFS uses "rw", others use 2029da6c28aaSamw * "". 2030da6c28aaSamw */ 2031da6c28aaSamw if (strcmp(proto, "nfs") != 0) 2032da6c28aaSamw defprop = "\"\""; 2033da6c28aaSamw else 2034da6c28aaSamw defprop = "rw"; 2035da6c28aaSamw 20366185db85Sdougm sh->sh_fstype = strdup(proto); 20376185db85Sdougm value = sa_proto_legacy_format(proto, share, 1); 20386185db85Sdougm if (value != NULL) { 203925a68471Sdougm if (strlen(value) > 0) 204025a68471Sdougm sh->sh_opts = strdup(value); 204125a68471Sdougm else 2042da6c28aaSamw sh->sh_opts = strdup(defprop); 204325a68471Sdougm free(value); 204425a68471Sdougm } else { 2045da6c28aaSamw sh->sh_opts = strdup(defprop); 204625a68471Sdougm } 20476185db85Sdougm 20486185db85Sdougm value = sa_get_share_description(share); 20496185db85Sdougm if (value != NULL) { 205025a68471Sdougm sh->sh_descr = strdup(value); 205125a68471Sdougm sa_free_share_description(value); 205225a68471Sdougm } else { 205325a68471Sdougm sh->sh_descr = strdup(""); 205425a68471Sdougm } 20556185db85Sdougm } 20566185db85Sdougm 20576185db85Sdougm /* 2058ecd6cf80Smarks * sa_emptyshare(sh) 20596185db85Sdougm * 20606185db85Sdougm * Free the strings in the non-NULL members of sh. 20616185db85Sdougm */ 20626185db85Sdougm 2063ecd6cf80Smarks void 2064ecd6cf80Smarks sa_emptyshare(struct share *sh) 20656185db85Sdougm { 20666185db85Sdougm if (sh->sh_path != NULL) 206725a68471Sdougm free(sh->sh_path); 20686185db85Sdougm sh->sh_path = NULL; 20696185db85Sdougm if (sh->sh_res != NULL) 207025a68471Sdougm free(sh->sh_res); 20716185db85Sdougm sh->sh_res = NULL; 20726185db85Sdougm if (sh->sh_fstype != NULL) 207325a68471Sdougm free(sh->sh_fstype); 20746185db85Sdougm sh->sh_fstype = NULL; 20756185db85Sdougm if (sh->sh_opts != NULL) 207625a68471Sdougm free(sh->sh_opts); 20776185db85Sdougm sh->sh_opts = NULL; 20786185db85Sdougm if (sh->sh_descr != NULL) 207925a68471Sdougm free(sh->sh_descr); 20806185db85Sdougm sh->sh_descr = NULL; 20816185db85Sdougm } 20826185db85Sdougm 20835b6e0c46Sdougm /* 20845b6e0c46Sdougm * sa_update_sharetab_ts(handle) 20855b6e0c46Sdougm * 20865b6e0c46Sdougm * Update the internal timestamp of when sharetab was last 20875b6e0c46Sdougm * changed. This needs to be public for ZFS to get at it. 20885b6e0c46Sdougm */ 20895b6e0c46Sdougm 20905b6e0c46Sdougm void 20915b6e0c46Sdougm sa_update_sharetab_ts(sa_handle_t handle) 20925b6e0c46Sdougm { 20935b6e0c46Sdougm struct stat st; 20945b6e0c46Sdougm sa_handle_impl_t implhandle = (sa_handle_impl_t)handle; 20955b6e0c46Sdougm 20965b6e0c46Sdougm if (implhandle != NULL && stat(SA_LEGACY_SHARETAB, &st) == 0) 20975b6e0c46Sdougm implhandle->tssharetab = TSTAMP(st.st_mtim); 20985b6e0c46Sdougm } 20995b6e0c46Sdougm 21006185db85Sdougm /* 21016185db85Sdougm * sa_update_sharetab(share, proto) 21026185db85Sdougm * 21036185db85Sdougm * Update the sharetab file with info from the specified share. 21046185db85Sdougm * This could be an update or add. 21056185db85Sdougm */ 21066185db85Sdougm 21076185db85Sdougm int 21086185db85Sdougm sa_update_sharetab(sa_share_t share, char *proto) 21096185db85Sdougm { 2110a237e38eSth int ret = SA_OK; 2111a237e38eSth share_t sh; 2112a237e38eSth char *path; 21135b6e0c46Sdougm sa_handle_t handle; 21146185db85Sdougm 21156185db85Sdougm path = sa_get_share_attr(share, "path"); 21166185db85Sdougm if (path != NULL) { 2117a237e38eSth (void) memset(&sh, '\0', sizeof (sh)); 2118a237e38eSth 21195b6e0c46Sdougm handle = sa_find_group_handle((sa_group_t)share); 21205b6e0c46Sdougm if (handle != NULL) { 21215b6e0c46Sdougm /* 21225b6e0c46Sdougm * Fill in share structure and send it to the kernel. 21235b6e0c46Sdougm */ 21245b6e0c46Sdougm (void) sa_fillshare(share, proto, &sh); 21255b6e0c46Sdougm (void) _sharefs(SHAREFS_ADD, &sh); 21265b6e0c46Sdougm /* 21275b6e0c46Sdougm * We need the timestamp of the sharetab file right 21285b6e0c46Sdougm * after the update was done. This lets us detect a 21295b6e0c46Sdougm * change that made by a different process. 21305b6e0c46Sdougm */ 21315b6e0c46Sdougm sa_update_sharetab_ts(handle); 21325b6e0c46Sdougm sa_emptyshare(&sh); 21335b6e0c46Sdougm } else { 21345b6e0c46Sdougm ret = SA_CONFIG_ERR; 21355b6e0c46Sdougm } 2136a237e38eSth sa_free_attr_string(path); 21376185db85Sdougm } 2138a237e38eSth 21396185db85Sdougm return (ret); 21406185db85Sdougm } 21416185db85Sdougm 21426185db85Sdougm /* 21435b6e0c46Sdougm * sa_delete_sharetab(handle, path, proto) 21446185db85Sdougm * 21456185db85Sdougm * remove the specified share from sharetab. 21466185db85Sdougm */ 21476185db85Sdougm 21486185db85Sdougm int 21495b6e0c46Sdougm sa_delete_sharetab(sa_handle_t handle, char *path, char *proto) 21506185db85Sdougm { 2151a237e38eSth int ret = SA_OK; 21525b6e0c46Sdougm struct stat st; 21536185db85Sdougm 2154a237e38eSth share_t sh; 2155a237e38eSth /* 2156a237e38eSth * Both the path and the proto are 2157a237e38eSth * keys into the sharetab. 2158a237e38eSth */ 2159a237e38eSth if (path != NULL && proto != NULL) { 2160a237e38eSth (void) memset(&sh, '\0', sizeof (sh)); 2161a237e38eSth sh.sh_path = path; 2162a237e38eSth sh.sh_fstype = proto; 2163a237e38eSth 2164a3175730Sth ret = _sharefs(SHAREFS_REMOVE, &sh); 21655b6e0c46Sdougm if (handle != NULL && stat(SA_LEGACY_SHARETAB, &st) == 0) 21665b6e0c46Sdougm sa_update_sharetab_ts(handle); 21676185db85Sdougm } 21686185db85Sdougm return (ret); 21696185db85Sdougm } 21705b6e0c46Sdougm 21715b6e0c46Sdougm /* 21725b6e0c46Sdougm * sa_needs_refresh(handle) 21735b6e0c46Sdougm * 2174*09c9e6dcSChris Williamson * Returns B_TRUE if the internal cache needs to be refreshed due to a 21755b6e0c46Sdougm * change by another process. B_FALSE returned otherwise. 21765b6e0c46Sdougm */ 21775b6e0c46Sdougm boolean_t 2178b83b3cacSdougm sa_needs_refresh(sa_handle_t handle) 21795b6e0c46Sdougm { 21805b6e0c46Sdougm sa_handle_impl_t implhandle = (sa_handle_impl_t)handle; 21815b6e0c46Sdougm struct stat st; 21825b6e0c46Sdougm char *str; 21835b6e0c46Sdougm uint64_t tstamp; 21845b6e0c46Sdougm scf_simple_prop_t *prop; 21855b6e0c46Sdougm 21865b6e0c46Sdougm if (handle == NULL) 21875b6e0c46Sdougm return (B_TRUE); 21885b6e0c46Sdougm 21895b6e0c46Sdougm /* 21905b6e0c46Sdougm * If sharetab has changed, then there was an external 21915b6e0c46Sdougm * change. Check sharetab first since it is updated by ZFS as 21925b6e0c46Sdougm * well as sharemgr. This is where external ZFS changes are 21935b6e0c46Sdougm * caught. 21945b6e0c46Sdougm */ 21955b6e0c46Sdougm if (stat(SA_LEGACY_SHARETAB, &st) == 0 && 21965b6e0c46Sdougm TSTAMP(st.st_mtim) != implhandle->tssharetab) 21975b6e0c46Sdougm return (B_TRUE); 21985b6e0c46Sdougm 21995b6e0c46Sdougm /* 22005b6e0c46Sdougm * If sharetab wasn't changed, check whether there were any 22015b6e0c46Sdougm * SMF transactions that modified the config but didn't 22025b6e0c46Sdougm * initiate a share. This is less common but does happen. 22035b6e0c46Sdougm */ 22045b6e0c46Sdougm prop = scf_simple_prop_get(implhandle->scfhandle->handle, 22055b6e0c46Sdougm (const char *)SA_SVC_FMRI_BASE ":default", "state", 22065b6e0c46Sdougm "lastupdate"); 22075b6e0c46Sdougm if (prop != NULL) { 22085b6e0c46Sdougm str = scf_simple_prop_next_astring(prop); 22095b6e0c46Sdougm if (str != NULL) 22105b6e0c46Sdougm tstamp = strtoull(str, NULL, 0); 22115b6e0c46Sdougm else 22125b6e0c46Sdougm tstamp = 0; 22135b6e0c46Sdougm scf_simple_prop_free(prop); 22145b6e0c46Sdougm if (tstamp != implhandle->tstrans) 22155b6e0c46Sdougm return (B_TRUE); 22165b6e0c46Sdougm } 22175b6e0c46Sdougm 22185b6e0c46Sdougm return (B_FALSE); 22195b6e0c46Sdougm } 22205b6e0c46Sdougm 2221da6c28aaSamw /* 2222da6c28aaSamw * sa_fix_resource_name(path) 2223da6c28aaSamw * 222489dc44ceSjose borrego * Convert invalid characters in a resource name (SMB share name) 222589dc44ceSjose borrego * to underscores ('_'). The list of invalid characters includes 222689dc44ceSjose borrego * control characters and the following: 222789dc44ceSjose borrego * 222889dc44ceSjose borrego * " / \ [ ] : | < > + ; , ? * = 222989dc44ceSjose borrego * 223089dc44ceSjose borrego * The caller must pass a valid path. Leading and trailing slashes 223189dc44ceSjose borrego * are stripped from the path before converting invalid characters. 223289dc44ceSjose borrego * Resource names are restricted to SA_MAX_RESOURCE_NAME characters. 2233da6c28aaSamw */ 2234da6c28aaSamw void 2235da6c28aaSamw sa_fix_resource_name(char *path) 2236da6c28aaSamw { 223789dc44ceSjose borrego char *invalid = "\"/\\[]:|<>+;,?*="; 223889dc44ceSjose borrego char *p = path; 223989dc44ceSjose borrego char *q; 2240da6c28aaSamw size_t len; 2241da6c28aaSamw 2242da6c28aaSamw assert(path != NULL); 2243da6c28aaSamw 224489dc44ceSjose borrego /* 224589dc44ceSjose borrego * Strip leading and trailing /'s. 224689dc44ceSjose borrego */ 224789dc44ceSjose borrego p += strspn(p, "/"); 224889dc44ceSjose borrego q = strchr(p, '\0'); 224989dc44ceSjose borrego if (q != NULL && q != path) { 225089dc44ceSjose borrego while ((--q, *q == '/')) 225189dc44ceSjose borrego *q = '\0'; 2252da6c28aaSamw } 225389dc44ceSjose borrego 225489dc44ceSjose borrego if (*p == '\0') { 225589dc44ceSjose borrego (void) strcpy(path, "_"); 225689dc44ceSjose borrego return; 2257da6c28aaSamw } 2258da6c28aaSamw 2259da6c28aaSamw /* 226089dc44ceSjose borrego * Stride over path components until the remaining 226189dc44ceSjose borrego * path is no longer than SA_MAX_RESOURCE_NAME. 2262da6c28aaSamw */ 226389dc44ceSjose borrego q = p; 226489dc44ceSjose borrego while ((q != NULL) && (strlen(q) > SA_MAX_RESOURCE_NAME)) { 226589dc44ceSjose borrego if ((q = strchr(q, '/')) != NULL) { 226689dc44ceSjose borrego ++q; 226789dc44ceSjose borrego p = q; 2268da6c28aaSamw } 226989dc44ceSjose borrego } 227089dc44ceSjose borrego 227189dc44ceSjose borrego /* 227289dc44ceSjose borrego * If the path is still longer than SA_MAX_RESOURCE_NAME, 227389dc44ceSjose borrego * take the trailing SA_MAX_RESOURCE_NAME characters. 227489dc44ceSjose borrego */ 227589dc44ceSjose borrego if ((len = strlen(p)) > SA_MAX_RESOURCE_NAME) { 227689dc44ceSjose borrego len = SA_MAX_RESOURCE_NAME; 227789dc44ceSjose borrego p = strchr(p, '\0') - (SA_MAX_RESOURCE_NAME - 1); 227889dc44ceSjose borrego } 227989dc44ceSjose borrego 228089dc44ceSjose borrego (void) memmove(path, p, len); 228189dc44ceSjose borrego path[len] = '\0'; 228289dc44ceSjose borrego 228389dc44ceSjose borrego for (p = path; *p != '\0'; ++p) { 228489dc44ceSjose borrego if ((iscntrl(*p)) || strchr(invalid, *p)) 228589dc44ceSjose borrego *p = '_'; 2286da6c28aaSamw } 2287da6c28aaSamw } 2288