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. 2509c9e6dcSChris 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 */; 439*41e1be47SToomas Soome 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 /* 14368a981c33SDaniel Hoffman * parse_sharetab_impl(handle, tmplist, lgroup) 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. 14428a981c33SDaniel Hoffman * 14438a981c33SDaniel Hoffman * The return value is used to contribute to values indicating if it is a legacy 14448a981c33SDaniel Hoffman * share. Right now this is 0 or 1, but as multiple systems become legacy that 14458a981c33SDaniel Hoffman * could change. 14466185db85Sdougm */ 14476185db85Sdougm 14488a981c33SDaniel Hoffman static int 14498a981c33SDaniel Hoffman parse_sharetab_impl(sa_handle_t handle, xfs_sharelist_t *tmplist, 14508a981c33SDaniel Hoffman sa_group_t lgroup) 14516185db85Sdougm { 14526185db85Sdougm int err = 0; 14536185db85Sdougm sa_share_t share; 14546185db85Sdougm sa_group_t group; 14556185db85Sdougm char *groupname; 14566185db85Sdougm int legacy = 0; 1457da6c28aaSamw char shareopts[MAXNAMLEN]; 14586185db85Sdougm 14598a981c33SDaniel Hoffman group = NULL; 14608a981c33SDaniel Hoffman share = sa_find_share(handle, tmplist->path); 14618a981c33SDaniel Hoffman if (share != NULL) { 14628a981c33SDaniel Hoffman /* 14638a981c33SDaniel Hoffman * If this is a legacy share, mark as shared so we only update 14648a981c33SDaniel Hoffman * sharetab appropriately. We also keep the sharetab options in 14658a981c33SDaniel Hoffman * order to display for legacy share with no arguments. 14668a981c33SDaniel Hoffman */ 14678a981c33SDaniel Hoffman set_node_attr(share, "shared", "true"); 14688a981c33SDaniel Hoffman (void) snprintf(shareopts, MAXNAMLEN, "shareopts-%s", 14698a981c33SDaniel Hoffman tmplist->fstype); 14708a981c33SDaniel Hoffman set_node_attr(share, shareopts, tmplist->options); 14718a981c33SDaniel Hoffman return (1); 14728a981c33SDaniel Hoffman } 14736185db85Sdougm 14748a981c33SDaniel Hoffman /* 14758a981c33SDaniel Hoffman * This share is transient so needs to be added. Initially, this will be 14768a981c33SDaniel Hoffman * under default(legacy) unless it is a ZFS share. If zfs, we need a zfs 14778a981c33SDaniel Hoffman * group. 14788a981c33SDaniel Hoffman */ 14798a981c33SDaniel Hoffman if (tmplist->resource != NULL && 14808a981c33SDaniel Hoffman (groupname = strchr(tmplist->resource, '@')) != NULL) { 14818a981c33SDaniel Hoffman /* There is a defined group */ 14828a981c33SDaniel Hoffman *groupname++ = '\0'; 14838a981c33SDaniel Hoffman group = sa_get_group(handle, groupname); 14848a981c33SDaniel Hoffman if (group != NULL) { 14858a981c33SDaniel Hoffman share = _sa_add_share(group, tmplist->path, 14868a981c33SDaniel Hoffman SA_SHARE_TRANSIENT, &err, 14878a981c33SDaniel Hoffman (uint64_t)SA_FEATURE_NONE); 14888a981c33SDaniel Hoffman } else { 148925a68471Sdougm /* 14908a981c33SDaniel Hoffman * While this case shouldn't occur very often, it does 14918a981c33SDaniel Hoffman * occur out of a "zfs set sharenfs=off" when the 14928a981c33SDaniel Hoffman * dataset is also set to canmount=off. A warning will 14938a981c33SDaniel Hoffman * then cause the zfs command to abort. Since we add it 14948a981c33SDaniel Hoffman * to the default list, everything works properly anyway 14958a981c33SDaniel Hoffman * and the library doesn't need to give a warning. 149625a68471Sdougm */ 14978a981c33SDaniel Hoffman share = _sa_add_share(lgroup, 14988a981c33SDaniel Hoffman tmplist->path, SA_SHARE_TRANSIENT, 14998a981c33SDaniel Hoffman &err, (uint64_t)SA_FEATURE_NONE); 150025a68471Sdougm } 15018a981c33SDaniel Hoffman } else { 15028a981c33SDaniel Hoffman if (sa_zfs_is_shared(handle, tmplist->path)) { 15038a981c33SDaniel Hoffman group = sa_get_group(handle, "zfs"); 15048a981c33SDaniel Hoffman if (group == NULL) { 15058a981c33SDaniel Hoffman group = sa_create_group(handle, 15068a981c33SDaniel Hoffman "zfs", &err); 15078a981c33SDaniel Hoffman if (group == NULL && 15088a981c33SDaniel Hoffman err == SA_NO_PERMISSION) { 15098a981c33SDaniel Hoffman group = _sa_create_group( 15108a981c33SDaniel Hoffman (sa_handle_impl_t) 15118a981c33SDaniel Hoffman handle, 15128a981c33SDaniel Hoffman "zfs"); 15138a981c33SDaniel Hoffman } 15148a981c33SDaniel Hoffman if (group != NULL) { 15158a981c33SDaniel Hoffman (void) sa_create_optionset( 15168a981c33SDaniel Hoffman group, tmplist->fstype); 15178a981c33SDaniel Hoffman (void) sa_set_group_attr(group, 15188a981c33SDaniel Hoffman "zfs", "true"); 15198a981c33SDaniel Hoffman } 15208a981c33SDaniel Hoffman } 15216185db85Sdougm if (group != NULL) { 15228a981c33SDaniel Hoffman share = _sa_add_share(group, 152325a68471Sdougm tmplist->path, SA_SHARE_TRANSIENT, 1524da6c28aaSamw &err, (uint64_t)SA_FEATURE_NONE); 152525a68471Sdougm } 152625a68471Sdougm } else { 15278a981c33SDaniel Hoffman share = _sa_add_share(lgroup, tmplist->path, 15288a981c33SDaniel Hoffman SA_SHARE_TRANSIENT, &err, 15298a981c33SDaniel Hoffman (uint64_t)SA_FEATURE_NONE); 15306185db85Sdougm } 15318a981c33SDaniel Hoffman } 15328a981c33SDaniel Hoffman if (share == NULL) 15338a981c33SDaniel Hoffman (void) printf(dgettext(TEXT_DOMAIN, 15348a981c33SDaniel Hoffman "Problem with transient: %s\n"), sa_errorstr(err)); 15358a981c33SDaniel Hoffman if (share != NULL) 15368a981c33SDaniel Hoffman set_node_attr(share, "shared", "true"); 15378a981c33SDaniel Hoffman if (err == SA_OK) { 15388a981c33SDaniel Hoffman if (tmplist->options != NULL && 15398a981c33SDaniel Hoffman strlen(tmplist->options) > 0) { 15408a981c33SDaniel Hoffman (void) sa_parse_legacy_options(share, 15418a981c33SDaniel Hoffman tmplist->options, tmplist->fstype); 15428a981c33SDaniel Hoffman } 15438a981c33SDaniel Hoffman if (tmplist->resource != NULL && 15448a981c33SDaniel Hoffman strcmp(tmplist->resource, "-") != 0) 15458a981c33SDaniel Hoffman set_node_attr(share, "resource", 15468a981c33SDaniel Hoffman tmplist->resource); 15478a981c33SDaniel Hoffman if (tmplist->description != NULL) { 15488a981c33SDaniel Hoffman xmlNodePtr node; 15498a981c33SDaniel Hoffman node = xmlNewChild((xmlNodePtr)share, NULL, 15508a981c33SDaniel Hoffman (xmlChar *)"description", NULL); 15518a981c33SDaniel Hoffman xmlNodeSetContent(node, 15528a981c33SDaniel Hoffman (xmlChar *)tmplist->description); 15538a981c33SDaniel Hoffman } 15548a981c33SDaniel Hoffman legacy = 1; 15558a981c33SDaniel Hoffman } 15568a981c33SDaniel Hoffman return (legacy); 15578a981c33SDaniel Hoffman } 15588a981c33SDaniel Hoffman 15598a981c33SDaniel Hoffman /* 15608a981c33SDaniel Hoffman * Given an array of paths, parses the sharetab in /etc/dfs/sharetab looking for 15618a981c33SDaniel Hoffman * matches to each path that could be transients from that file. 15628a981c33SDaniel Hoffman * 15638a981c33SDaniel Hoffman * parse_sharetab_impl has more information on what exactly it is looking for. 15648a981c33SDaniel Hoffman */ 15658a981c33SDaniel Hoffman int 15668a981c33SDaniel Hoffman parse_sharetab_for_paths(sa_handle_t handle, char **paths, size_t paths_len) 15678a981c33SDaniel Hoffman { 15688a981c33SDaniel Hoffman int err = 0; 15698a981c33SDaniel Hoffman int legacy = 0; 15708a981c33SDaniel Hoffman sa_group_t lgroup; 15718a981c33SDaniel Hoffman xfs_sharelist_t *list = get_share_list(&err); 15728a981c33SDaniel Hoffman 15738a981c33SDaniel Hoffman if (list == NULL) 15748a981c33SDaniel Hoffman return (legacy); 15758a981c33SDaniel Hoffman 15768a981c33SDaniel Hoffman lgroup = sa_get_group(handle, "default"); 15778a981c33SDaniel Hoffman for (int i = 0; i < paths_len; ++i) { 15788a981c33SDaniel Hoffman xfs_sharelist_t *tmplist; 15798a981c33SDaniel Hoffman 15808a981c33SDaniel Hoffman for (tmplist = list; tmplist != NULL; tmplist = tmplist->next) { 15818a981c33SDaniel Hoffman if (strcmp(tmplist->path, paths[i]) == 0) { 15828a981c33SDaniel Hoffman break; 158325a68471Sdougm } 15846185db85Sdougm } 15858a981c33SDaniel Hoffman if (tmplist == NULL) { 15868a981c33SDaniel Hoffman continue; 15878a981c33SDaniel Hoffman } 15888a981c33SDaniel Hoffman 15898a981c33SDaniel Hoffman legacy = parse_sharetab_impl(handle, tmplist, lgroup); 15908a981c33SDaniel Hoffman 15918a981c33SDaniel Hoffman } 15928a981c33SDaniel Hoffman dfs_free_list(list); 15938a981c33SDaniel Hoffman return (legacy); 15948a981c33SDaniel Hoffman } 15958a981c33SDaniel Hoffman 15968a981c33SDaniel Hoffman /* 15978a981c33SDaniel Hoffman * Runs parse_sharetab_impl on all the different share lists on the system. 15988a981c33SDaniel Hoffman */ 15998a981c33SDaniel Hoffman int 16008a981c33SDaniel Hoffman parse_sharetab(sa_handle_t handle) 16018a981c33SDaniel Hoffman { 16028a981c33SDaniel Hoffman int err = 0; 16038a981c33SDaniel Hoffman int legacy = 0; 16048a981c33SDaniel Hoffman sa_group_t lgroup; 16058a981c33SDaniel Hoffman xfs_sharelist_t *list = get_share_list(&err); 16068a981c33SDaniel Hoffman 16078a981c33SDaniel Hoffman list = get_share_list(&err); 16088a981c33SDaniel Hoffman if (list == NULL) 16098a981c33SDaniel Hoffman return (legacy); 16108a981c33SDaniel Hoffman 16118a981c33SDaniel Hoffman lgroup = sa_get_group(handle, "default"); 16128a981c33SDaniel Hoffman 16138a981c33SDaniel Hoffman for (xfs_sharelist_t *tmplist = list; tmplist != NULL; 16148a981c33SDaniel Hoffman tmplist = tmplist->next) { 16158a981c33SDaniel Hoffman legacy |= parse_sharetab_impl(handle, tmplist, lgroup); 16166185db85Sdougm } 16171cea05afSdougm dfs_free_list(list); 16186185db85Sdougm return (legacy); 16196185db85Sdougm } 16206185db85Sdougm 16216185db85Sdougm /* 16228a981c33SDaniel Hoffman * This is the same as gettransients except when parsing sharetab the entries 16238a981c33SDaniel Hoffman * are only processed if the path matches an element of paths. 16248a981c33SDaniel Hoffman */ 16258a981c33SDaniel Hoffman int 16268a981c33SDaniel Hoffman get_one_transient(sa_handle_impl_t ihandle, xmlNodePtr *root, char **paths, 16278a981c33SDaniel Hoffman size_t paths_len) 16288a981c33SDaniel Hoffman { 16298a981c33SDaniel Hoffman int legacy = 0; 16308a981c33SDaniel Hoffman char **protocols = NULL; 16318a981c33SDaniel Hoffman 16328a981c33SDaniel Hoffman if (root != NULL) { 16338a981c33SDaniel Hoffman if (*root == NULL) 16348a981c33SDaniel Hoffman *root = xmlNewNode(NULL, (xmlChar *)"sharecfg"); 16358a981c33SDaniel Hoffman if (*root != NULL) { 16368a981c33SDaniel Hoffman legacy = parse_sharetab_for_paths(ihandle, paths, 16378a981c33SDaniel Hoffman paths_len); 16388a981c33SDaniel Hoffman int numproto = sa_get_protocols(&protocols); 16398a981c33SDaniel Hoffman for (int i = 0; i < numproto; i++) 16408a981c33SDaniel Hoffman legacy |= sa_proto_get_transients( 16418a981c33SDaniel Hoffman (sa_handle_t)ihandle, protocols[i]); 16428a981c33SDaniel Hoffman if (protocols != NULL) 16438a981c33SDaniel Hoffman free(protocols); 16448a981c33SDaniel Hoffman } 16458a981c33SDaniel Hoffman } 16468a981c33SDaniel Hoffman return (legacy); 16478a981c33SDaniel Hoffman } 16488a981c33SDaniel Hoffman 16498a981c33SDaniel Hoffman /* 16508a981c33SDaniel Hoffman * Get the transient shares from the sharetab (or other) file. Since 16516185db85Sdougm * these are transient, they only appear in the working file and not 16526185db85Sdougm * in a repository. 16536185db85Sdougm */ 16546185db85Sdougm int 1655549ec3ffSdougm gettransients(sa_handle_impl_t ihandle, xmlNodePtr *root) 16566185db85Sdougm { 16576185db85Sdougm int legacy = 0; 1658da6c28aaSamw int numproto; 1659da6c28aaSamw char **protocols = NULL; 1660da6c28aaSamw int i; 16616185db85Sdougm 16626185db85Sdougm if (root != NULL) { 166325a68471Sdougm if (*root == NULL) 166425a68471Sdougm *root = xmlNewNode(NULL, (xmlChar *)"sharecfg"); 1665da6c28aaSamw if (*root != NULL) { 166625a68471Sdougm legacy = parse_sharetab(ihandle); 1667da6c28aaSamw numproto = sa_get_protocols(&protocols); 1668da6c28aaSamw for (i = 0; i < numproto; i++) 1669da6c28aaSamw legacy |= sa_proto_get_transients( 1670da6c28aaSamw (sa_handle_t)ihandle, protocols[i]); 1671da6c28aaSamw if (protocols != NULL) 1672da6c28aaSamw free(protocols); 1673da6c28aaSamw } 16746185db85Sdougm } 16756185db85Sdougm return (legacy); 16766185db85Sdougm } 16776185db85Sdougm 16786185db85Sdougm /* 16796185db85Sdougm * sa_has_prop(optionset, prop) 16806185db85Sdougm * 16816185db85Sdougm * Is the specified property a member of the optionset? 16826185db85Sdougm */ 16836185db85Sdougm 16846185db85Sdougm int 16856185db85Sdougm sa_has_prop(sa_optionset_t optionset, sa_property_t prop) 16866185db85Sdougm { 16876185db85Sdougm char *name; 16886185db85Sdougm sa_property_t otherprop; 16896185db85Sdougm int result = 0; 16906185db85Sdougm 16916185db85Sdougm if (optionset != NULL) { 169225a68471Sdougm name = sa_get_property_attr(prop, "type"); 169325a68471Sdougm if (name != NULL) { 169425a68471Sdougm otherprop = sa_get_property(optionset, name); 169525a68471Sdougm if (otherprop != NULL) 169625a68471Sdougm result = 1; 169725a68471Sdougm sa_free_attr_string(name); 169825a68471Sdougm } 16996185db85Sdougm } 17006185db85Sdougm return (result); 17016185db85Sdougm } 17026185db85Sdougm 17036185db85Sdougm /* 17046185db85Sdougm * Update legacy files 17056185db85Sdougm * 17066185db85Sdougm * Provides functions to add/remove/modify individual entries 17076185db85Sdougm * in dfstab and sharetab 17086185db85Sdougm */ 17096185db85Sdougm 17106185db85Sdougm void 1711549ec3ffSdougm update_legacy_config(sa_handle_t handle) 17126185db85Sdougm { 17136185db85Sdougm /* 17146185db85Sdougm * no longer used -- this is a placeholder in case we need to 17156185db85Sdougm * add it back later. 17166185db85Sdougm */ 1717549ec3ffSdougm #ifdef lint 1718549ec3ffSdougm handle = handle; 1719549ec3ffSdougm #endif 17206185db85Sdougm } 17216185db85Sdougm 17226185db85Sdougm /* 1723687915e9Sdougm * sa_valid_property(handle, object, proto, property) 17246185db85Sdougm * 17256185db85Sdougm * check to see if the specified property is valid relative to the 17266185db85Sdougm * specified protocol. The protocol plugin is called to do the work. 17276185db85Sdougm */ 17286185db85Sdougm 17296185db85Sdougm int 1730687915e9Sdougm sa_valid_property(sa_handle_t handle, void *object, char *proto, 1731687915e9Sdougm sa_property_t property) 17326185db85Sdougm { 17336185db85Sdougm int ret = SA_OK; 17346185db85Sdougm 17356185db85Sdougm if (proto != NULL && property != NULL) { 1736687915e9Sdougm ret = sa_proto_valid_prop(handle, proto, property, object); 17376185db85Sdougm } 17386185db85Sdougm 17396185db85Sdougm return (ret); 17406185db85Sdougm } 17416185db85Sdougm 17426185db85Sdougm /* 17436185db85Sdougm * sa_fstype(path) 17446185db85Sdougm * 17456185db85Sdougm * Given path, return the string representing the path's file system 17466185db85Sdougm * type. This is used to discover ZFS shares. 17476185db85Sdougm */ 17486185db85Sdougm 17496185db85Sdougm char * 17506185db85Sdougm sa_fstype(char *path) 17516185db85Sdougm { 17526185db85Sdougm int err; 17536185db85Sdougm struct stat st; 17546185db85Sdougm 17556185db85Sdougm err = stat(path, &st); 175625a68471Sdougm if (err < 0) 175725a68471Sdougm err = SA_NO_SUCH_PATH; 175825a68471Sdougm else 175925a68471Sdougm err = SA_OK; 176025a68471Sdougm 176125a68471Sdougm /* 176225a68471Sdougm * If we have a valid path at this point ret, return the fstype. 176325a68471Sdougm */ 176425a68471Sdougm if (err == SA_OK) 176525a68471Sdougm return (strdup(st.st_fstype)); 176625a68471Sdougm 17676185db85Sdougm return (NULL); 17686185db85Sdougm } 17696185db85Sdougm 17706185db85Sdougm void 17716185db85Sdougm sa_free_fstype(char *type) 17726185db85Sdougm { 17736185db85Sdougm free(type); 17746185db85Sdougm } 17756185db85Sdougm 17766185db85Sdougm /* 17776185db85Sdougm * sa_get_derived_optionset(object, proto, hier) 17786185db85Sdougm * 17796185db85Sdougm * Work backward to the top of the share object tree and start 17806185db85Sdougm * copying protocol specific optionsets into a newly created 17816185db85Sdougm * optionset that doesn't have a parent (it will be freed 1782da6c28aaSamw * later). This provides for the property inheritance model. That 17836185db85Sdougm * is, properties closer to the share take precedence over group 17846185db85Sdougm * level. This also provides for groups of groups in the future. 17856185db85Sdougm */ 17866185db85Sdougm 17876185db85Sdougm sa_optionset_t 17886185db85Sdougm sa_get_derived_optionset(void *object, char *proto, int hier) 17896185db85Sdougm { 17906185db85Sdougm sa_optionset_t newoptionset; 17916185db85Sdougm sa_optionset_t optionset; 17926185db85Sdougm sa_group_t group; 17936185db85Sdougm 17946185db85Sdougm if (hier && 17956185db85Sdougm (group = sa_get_parent_group((sa_share_t)object)) != NULL) { 179625a68471Sdougm newoptionset = sa_get_derived_optionset((void *)group, proto, 179725a68471Sdougm hier); 17986185db85Sdougm } else { 179925a68471Sdougm newoptionset = (sa_optionset_t)xmlNewNode(NULL, 180025a68471Sdougm (xmlChar *)"optionset"); 180125a68471Sdougm if (newoptionset != NULL) { 180225a68471Sdougm sa_set_optionset_attr(newoptionset, "type", proto); 180325a68471Sdougm } 18046185db85Sdougm } 180525a68471Sdougm /* Dont' do anything if memory wasn't allocated */ 18066185db85Sdougm if (newoptionset == NULL) 180725a68471Sdougm return (NULL); 18086185db85Sdougm 180925a68471Sdougm /* Found the top so working back down the stack */ 18106185db85Sdougm optionset = sa_get_optionset((sa_optionset_t)object, proto); 18116185db85Sdougm if (optionset != NULL) { 181225a68471Sdougm sa_property_t prop; 181325a68471Sdougm /* add optionset to the newoptionset */ 181425a68471Sdougm for (prop = sa_get_property(optionset, NULL); 181525a68471Sdougm prop != NULL; 181625a68471Sdougm prop = sa_get_next_property(prop)) { 181725a68471Sdougm sa_property_t newprop; 181825a68471Sdougm char *name; 181925a68471Sdougm char *value; 182025a68471Sdougm name = sa_get_property_attr(prop, "type"); 182125a68471Sdougm value = sa_get_property_attr(prop, "value"); 182225a68471Sdougm if (name == NULL) 182325a68471Sdougm continue; 182425a68471Sdougm newprop = sa_get_property(newoptionset, name); 182525a68471Sdougm /* Replace the value with the new value */ 182625a68471Sdougm if (newprop != NULL) { 182725a68471Sdougm /* 182825a68471Sdougm * Only set if value is non NULL, old value ok 182925a68471Sdougm * if it is NULL. 183025a68471Sdougm */ 183125a68471Sdougm if (value != NULL) 183225a68471Sdougm set_node_attr(newprop, "value", value); 183325a68471Sdougm } else { 183425a68471Sdougm /* an entirely new property */ 183525a68471Sdougm if (value != NULL) { 183625a68471Sdougm newprop = sa_create_property(name, 183725a68471Sdougm value); 183825a68471Sdougm if (newprop != NULL) { 183925a68471Sdougm newprop = (sa_property_t) 184025a68471Sdougm xmlAddChild( 184125a68471Sdougm (xmlNodePtr)newoptionset, 184225a68471Sdougm (xmlNodePtr)newprop); 184325a68471Sdougm } 184425a68471Sdougm } 18456185db85Sdougm } 184625a68471Sdougm sa_free_attr_string(name); 184725a68471Sdougm 184825a68471Sdougm if (value != NULL) 184925a68471Sdougm sa_free_attr_string(value); 18506185db85Sdougm } 18516185db85Sdougm } 18526185db85Sdougm return (newoptionset); 18536185db85Sdougm } 18546185db85Sdougm 18556185db85Sdougm void 18566185db85Sdougm sa_free_derived_optionset(sa_optionset_t optionset) 18576185db85Sdougm { 185825a68471Sdougm /* While it shouldn't be linked, it doesn't hurt */ 18596185db85Sdougm if (optionset != NULL) { 186025a68471Sdougm xmlUnlinkNode((xmlNodePtr) optionset); 186125a68471Sdougm xmlFreeNode((xmlNodePtr) optionset); 18626185db85Sdougm } 18636185db85Sdougm } 18646185db85Sdougm 18656185db85Sdougm /* 18666185db85Sdougm * sa_get_all_security_types(object, proto, hier) 18676185db85Sdougm * 186825a68471Sdougm * Find all the security types set for this object. This is 18696185db85Sdougm * preliminary to getting a derived security set. The return value is an 18706185db85Sdougm * optionset containg properties which are the sectype values found by 1871da6c28aaSamw * walking up the XML document structure. The returned optionset 18726185db85Sdougm * is a derived optionset. 18736185db85Sdougm * 18746185db85Sdougm * If hier is 0, only look at object. If non-zero, walk up the tree. 18756185db85Sdougm */ 18766185db85Sdougm sa_optionset_t 18776185db85Sdougm sa_get_all_security_types(void *object, char *proto, int hier) 18786185db85Sdougm { 18796185db85Sdougm sa_optionset_t options; 18806185db85Sdougm sa_security_t security; 18816185db85Sdougm sa_group_t group; 18826185db85Sdougm sa_property_t prop; 18836185db85Sdougm 18846185db85Sdougm options = NULL; 18856185db85Sdougm 18866185db85Sdougm if (hier && 188725a68471Sdougm (group = sa_get_parent_group((sa_share_t)object)) != NULL) 188825a68471Sdougm options = sa_get_all_security_types((void *)group, proto, hier); 188925a68471Sdougm else 189025a68471Sdougm options = (sa_optionset_t)xmlNewNode(NULL, 189125a68471Sdougm (xmlChar *)"optionset"); 189225a68471Sdougm 189325a68471Sdougm if (options == NULL) 189425a68471Sdougm return (options); 189525a68471Sdougm 189625a68471Sdougm /* Hit the top so collect the security types working back. */ 189725a68471Sdougm for (security = sa_get_security((sa_group_t)object, NULL, NULL); 189825a68471Sdougm security != NULL; 189925a68471Sdougm security = sa_get_next_security(security)) { 19006185db85Sdougm char *type; 19016185db85Sdougm char *sectype; 19026185db85Sdougm 19036185db85Sdougm type = sa_get_security_attr(security, "type"); 19046185db85Sdougm if (type != NULL) { 190525a68471Sdougm if (strcmp(type, proto) != 0) { 190625a68471Sdougm sa_free_attr_string(type); 190725a68471Sdougm continue; 190825a68471Sdougm } 190925a68471Sdougm sectype = sa_get_security_attr(security, "sectype"); 191025a68471Sdougm if (sectype != NULL) { 191125a68471Sdougm /* 191225a68471Sdougm * Have a security type, check to see if 191325a68471Sdougm * already present in optionset and add if it 191425a68471Sdougm * isn't. 191525a68471Sdougm */ 191625a68471Sdougm if (sa_get_property(options, sectype) == NULL) { 191725a68471Sdougm prop = sa_create_property(sectype, 191825a68471Sdougm "true"); 191925a68471Sdougm if (prop != NULL) 192025a68471Sdougm prop = (sa_property_t) 192125a68471Sdougm xmlAddChild( 192225a68471Sdougm (xmlNodePtr)options, 192325a68471Sdougm (xmlNodePtr)prop); 192425a68471Sdougm } 192525a68471Sdougm sa_free_attr_string(sectype); 19266185db85Sdougm } 192725a68471Sdougm sa_free_attr_string(type); 19286185db85Sdougm } 19296185db85Sdougm } 193025a68471Sdougm 19316185db85Sdougm return (options); 19326185db85Sdougm } 19336185db85Sdougm 19346185db85Sdougm /* 19356185db85Sdougm * sa_get_derived_security(object, sectype, proto, hier) 19366185db85Sdougm * 19376185db85Sdougm * Get the derived security(named optionset) for the object given the 19386185db85Sdougm * sectype and proto. If hier is non-zero, walk up the tree to get all 19396185db85Sdougm * properties defined for this object, otherwise just those on the 19406185db85Sdougm * object. 19416185db85Sdougm */ 19426185db85Sdougm 19436185db85Sdougm sa_security_t 19446185db85Sdougm sa_get_derived_security(void *object, char *sectype, char *proto, int hier) 19456185db85Sdougm { 19466185db85Sdougm sa_security_t newsecurity; 19476185db85Sdougm sa_security_t security; 19486185db85Sdougm sa_group_t group; 194925a68471Sdougm sa_property_t prop; 19506185db85Sdougm 19516185db85Sdougm if (hier && 19526185db85Sdougm (group = sa_get_parent_group((sa_share_t)object)) != NULL) { 195325a68471Sdougm newsecurity = sa_get_derived_security((void *)group, 195425a68471Sdougm sectype, proto, hier); 19556185db85Sdougm } else { 195625a68471Sdougm newsecurity = (sa_security_t)xmlNewNode(NULL, 195725a68471Sdougm (xmlChar *)"security"); 195825a68471Sdougm if (newsecurity != NULL) { 195925a68471Sdougm sa_set_security_attr(newsecurity, "type", proto); 196025a68471Sdougm sa_set_security_attr(newsecurity, "sectype", sectype); 196125a68471Sdougm } 19626185db85Sdougm } 196325a68471Sdougm /* Don't do anything if memory wasn't allocated */ 19646185db85Sdougm if (newsecurity == NULL) 196525a68471Sdougm return (newsecurity); 19666185db85Sdougm 196725a68471Sdougm /* Found the top so working back down the stack. */ 19686185db85Sdougm security = sa_get_security((sa_security_t)object, sectype, proto); 196925a68471Sdougm if (security == NULL) 197025a68471Sdougm return (newsecurity); 197125a68471Sdougm 197225a68471Sdougm /* add security to the newsecurity */ 197325a68471Sdougm for (prop = sa_get_property(security, NULL); 197425a68471Sdougm prop != NULL; prop = sa_get_next_property(prop)) { 19756185db85Sdougm sa_property_t newprop; 19766185db85Sdougm char *name; 19776185db85Sdougm char *value; 19786185db85Sdougm name = sa_get_property_attr(prop, "type"); 19796185db85Sdougm value = sa_get_property_attr(prop, "value"); 19806185db85Sdougm if (name != NULL) { 198125a68471Sdougm newprop = sa_get_property(newsecurity, name); 198225a68471Sdougm /* Replace the value with the new value */ 198325a68471Sdougm if (newprop != NULL) { 198425a68471Sdougm /* 198597df5ac9Sdougm * Only set if value is non NULL, old 198697df5ac9Sdougm * value ok if it is NULL. The value 198797df5ac9Sdougm * must be associated with the "value" 198897df5ac9Sdougm * tag within XML. 198925a68471Sdougm */ 199025a68471Sdougm if (value != NULL) 199197df5ac9Sdougm set_node_attr(newprop, "value", value); 199225a68471Sdougm } else { 199325a68471Sdougm /* An entirely new property */ 199425a68471Sdougm if (value != NULL) { 199525a68471Sdougm newprop = sa_create_property(name, 199625a68471Sdougm value); 199725a68471Sdougm newprop = (sa_property_t) 199825a68471Sdougm xmlAddChild((xmlNodePtr)newsecurity, 19996185db85Sdougm (xmlNodePtr)newprop); 200025a68471Sdougm } 20016185db85Sdougm } 200225a68471Sdougm sa_free_attr_string(name); 20036185db85Sdougm } 20046185db85Sdougm if (value != NULL) 200525a68471Sdougm sa_free_attr_string(value); 20066185db85Sdougm } 20076185db85Sdougm return (newsecurity); 20086185db85Sdougm } 20096185db85Sdougm 20106185db85Sdougm void 20116185db85Sdougm sa_free_derived_security(sa_security_t security) 20126185db85Sdougm { 20136185db85Sdougm /* while it shouldn't be linked, it doesn't hurt */ 20146185db85Sdougm if (security != NULL) { 201525a68471Sdougm xmlUnlinkNode((xmlNodePtr)security); 201625a68471Sdougm xmlFreeNode((xmlNodePtr)security); 20176185db85Sdougm } 20186185db85Sdougm } 20196185db85Sdougm 20206185db85Sdougm /* 20216185db85Sdougm * sharetab utility functions 20226185db85Sdougm * 202325a68471Sdougm * Makes use of the original sharetab.c from fs.d/nfs/lib 20246185db85Sdougm */ 20256185db85Sdougm 20266185db85Sdougm /* 2027ecd6cf80Smarks * sa_fillshare(share, proto, sh) 20286185db85Sdougm * 20296185db85Sdougm * Fill the struct share with values obtained from the share object. 20306185db85Sdougm */ 2031ecd6cf80Smarks void 2032ecd6cf80Smarks sa_fillshare(sa_share_t share, char *proto, struct share *sh) 20336185db85Sdougm { 20346185db85Sdougm char *groupname = NULL; 20356185db85Sdougm char *value; 20366185db85Sdougm sa_group_t group; 20376185db85Sdougm char *buff; 20386185db85Sdougm char *zfs; 2039da6c28aaSamw sa_resource_t resource; 2040da6c28aaSamw char *rsrcname = NULL; 2041da6c28aaSamw char *defprop; 2042da6c28aaSamw 2043da6c28aaSamw /* 2044da6c28aaSamw * We only want to deal with the path level shares for the 2045da6c28aaSamw * sharetab file. If a resource, get the parent. 2046da6c28aaSamw */ 2047da6c28aaSamw if (sa_is_resource(share)) { 2048da6c28aaSamw resource = (sa_resource_t)share; 2049da6c28aaSamw share = sa_get_resource_parent(resource); 2050da6c28aaSamw rsrcname = sa_get_resource_attr(resource, "name"); 2051da6c28aaSamw } 20526185db85Sdougm 20536185db85Sdougm group = sa_get_parent_group(share); 20546185db85Sdougm if (group != NULL) { 205525a68471Sdougm zfs = sa_get_group_attr(group, "zfs"); 205625a68471Sdougm groupname = sa_get_group_attr(group, "name"); 20576185db85Sdougm 205825a68471Sdougm if (groupname != NULL && 205925a68471Sdougm (strcmp(groupname, "default") == 0 || zfs != NULL)) { 206025a68471Sdougm /* 206125a68471Sdougm * since the groupname is either "default" or the 206225a68471Sdougm * group is a ZFS group, we don't want to keep 206325a68471Sdougm * groupname. We do want it if it is any other type of 206425a68471Sdougm * group. 206525a68471Sdougm */ 206625a68471Sdougm sa_free_attr_string(groupname); 206725a68471Sdougm groupname = NULL; 206825a68471Sdougm } 206925a68471Sdougm if (zfs != NULL) 207025a68471Sdougm sa_free_attr_string(zfs); 20716185db85Sdougm } 20726185db85Sdougm 20736185db85Sdougm value = sa_get_share_attr(share, "path"); 20746185db85Sdougm if (value != NULL) { 207525a68471Sdougm sh->sh_path = strdup(value); 207625a68471Sdougm sa_free_attr_string(value); 20776185db85Sdougm } 20786185db85Sdougm 2079da6c28aaSamw if (rsrcname != NULL || groupname != NULL) { 208025a68471Sdougm int len = 0; 208125a68471Sdougm 2082da6c28aaSamw if (rsrcname != NULL) 2083da6c28aaSamw len += strlen(rsrcname); 208425a68471Sdougm if (groupname != NULL) 208525a68471Sdougm len += strlen(groupname); 208625a68471Sdougm len += 3; /* worst case */ 208725a68471Sdougm buff = malloc(len); 208825a68471Sdougm (void) snprintf(buff, len, "%s%s%s", 2089da6c28aaSamw (rsrcname != NULL && 2090da6c28aaSamw strlen(rsrcname) > 0) ? rsrcname : "-", 20916185db85Sdougm groupname != NULL ? "@" : "", 20926185db85Sdougm groupname != NULL ? groupname : ""); 209325a68471Sdougm sh->sh_res = buff; 2094da6c28aaSamw if (rsrcname != NULL) 2095da6c28aaSamw sa_free_attr_string(rsrcname); 2096da6c28aaSamw if (groupname != NULL) 209725a68471Sdougm sa_free_attr_string(groupname); 20986185db85Sdougm } else { 209925a68471Sdougm sh->sh_res = strdup("-"); 21006185db85Sdougm } 21016185db85Sdougm 2102da6c28aaSamw /* 2103da6c28aaSamw * Get correct default prop string. NFS uses "rw", others use 2104da6c28aaSamw * "". 2105da6c28aaSamw */ 2106da6c28aaSamw if (strcmp(proto, "nfs") != 0) 2107da6c28aaSamw defprop = "\"\""; 2108da6c28aaSamw else 2109da6c28aaSamw defprop = "rw"; 2110da6c28aaSamw 21116185db85Sdougm sh->sh_fstype = strdup(proto); 21126185db85Sdougm value = sa_proto_legacy_format(proto, share, 1); 21136185db85Sdougm if (value != NULL) { 211425a68471Sdougm if (strlen(value) > 0) 211525a68471Sdougm sh->sh_opts = strdup(value); 211625a68471Sdougm else 2117da6c28aaSamw sh->sh_opts = strdup(defprop); 211825a68471Sdougm free(value); 211925a68471Sdougm } else { 2120da6c28aaSamw sh->sh_opts = strdup(defprop); 212125a68471Sdougm } 21226185db85Sdougm 21236185db85Sdougm value = sa_get_share_description(share); 21246185db85Sdougm if (value != NULL) { 212525a68471Sdougm sh->sh_descr = strdup(value); 212625a68471Sdougm sa_free_share_description(value); 212725a68471Sdougm } else { 212825a68471Sdougm sh->sh_descr = strdup(""); 212925a68471Sdougm } 21306185db85Sdougm } 21316185db85Sdougm 21326185db85Sdougm /* 2133ecd6cf80Smarks * sa_emptyshare(sh) 21346185db85Sdougm * 21356185db85Sdougm * Free the strings in the non-NULL members of sh. 21366185db85Sdougm */ 21376185db85Sdougm 2138ecd6cf80Smarks void 2139ecd6cf80Smarks sa_emptyshare(struct share *sh) 21406185db85Sdougm { 21416185db85Sdougm if (sh->sh_path != NULL) 214225a68471Sdougm free(sh->sh_path); 21436185db85Sdougm sh->sh_path = NULL; 21446185db85Sdougm if (sh->sh_res != NULL) 214525a68471Sdougm free(sh->sh_res); 21466185db85Sdougm sh->sh_res = NULL; 21476185db85Sdougm if (sh->sh_fstype != NULL) 214825a68471Sdougm free(sh->sh_fstype); 21496185db85Sdougm sh->sh_fstype = NULL; 21506185db85Sdougm if (sh->sh_opts != NULL) 215125a68471Sdougm free(sh->sh_opts); 21526185db85Sdougm sh->sh_opts = NULL; 21536185db85Sdougm if (sh->sh_descr != NULL) 215425a68471Sdougm free(sh->sh_descr); 21556185db85Sdougm sh->sh_descr = NULL; 21566185db85Sdougm } 21576185db85Sdougm 21585b6e0c46Sdougm /* 21595b6e0c46Sdougm * sa_update_sharetab_ts(handle) 21605b6e0c46Sdougm * 21615b6e0c46Sdougm * Update the internal timestamp of when sharetab was last 21625b6e0c46Sdougm * changed. This needs to be public for ZFS to get at it. 21635b6e0c46Sdougm */ 21645b6e0c46Sdougm 21655b6e0c46Sdougm void 21665b6e0c46Sdougm sa_update_sharetab_ts(sa_handle_t handle) 21675b6e0c46Sdougm { 21685b6e0c46Sdougm struct stat st; 21695b6e0c46Sdougm sa_handle_impl_t implhandle = (sa_handle_impl_t)handle; 21705b6e0c46Sdougm 21715b6e0c46Sdougm if (implhandle != NULL && stat(SA_LEGACY_SHARETAB, &st) == 0) 21725b6e0c46Sdougm implhandle->tssharetab = TSTAMP(st.st_mtim); 21735b6e0c46Sdougm } 21745b6e0c46Sdougm 21756185db85Sdougm /* 21766185db85Sdougm * sa_update_sharetab(share, proto) 21776185db85Sdougm * 21786185db85Sdougm * Update the sharetab file with info from the specified share. 21796185db85Sdougm * This could be an update or add. 21806185db85Sdougm */ 21816185db85Sdougm 21826185db85Sdougm int 21836185db85Sdougm sa_update_sharetab(sa_share_t share, char *proto) 21846185db85Sdougm { 2185a237e38eSth int ret = SA_OK; 2186a237e38eSth share_t sh; 2187a237e38eSth char *path; 21885b6e0c46Sdougm sa_handle_t handle; 21896185db85Sdougm 21906185db85Sdougm path = sa_get_share_attr(share, "path"); 21916185db85Sdougm if (path != NULL) { 2192a237e38eSth (void) memset(&sh, '\0', sizeof (sh)); 2193a237e38eSth 21945b6e0c46Sdougm handle = sa_find_group_handle((sa_group_t)share); 21955b6e0c46Sdougm if (handle != NULL) { 21965b6e0c46Sdougm /* 21975b6e0c46Sdougm * Fill in share structure and send it to the kernel. 21985b6e0c46Sdougm */ 21995b6e0c46Sdougm (void) sa_fillshare(share, proto, &sh); 22005b6e0c46Sdougm (void) _sharefs(SHAREFS_ADD, &sh); 22015b6e0c46Sdougm /* 22025b6e0c46Sdougm * We need the timestamp of the sharetab file right 22035b6e0c46Sdougm * after the update was done. This lets us detect a 22045b6e0c46Sdougm * change that made by a different process. 22055b6e0c46Sdougm */ 22065b6e0c46Sdougm sa_update_sharetab_ts(handle); 22075b6e0c46Sdougm sa_emptyshare(&sh); 22085b6e0c46Sdougm } else { 22095b6e0c46Sdougm ret = SA_CONFIG_ERR; 22105b6e0c46Sdougm } 2211a237e38eSth sa_free_attr_string(path); 22126185db85Sdougm } 2213a237e38eSth 22146185db85Sdougm return (ret); 22156185db85Sdougm } 22166185db85Sdougm 22176185db85Sdougm /* 22185b6e0c46Sdougm * sa_delete_sharetab(handle, path, proto) 22196185db85Sdougm * 22206185db85Sdougm * remove the specified share from sharetab. 22216185db85Sdougm */ 22226185db85Sdougm 22236185db85Sdougm int 22245b6e0c46Sdougm sa_delete_sharetab(sa_handle_t handle, char *path, char *proto) 22256185db85Sdougm { 2226a237e38eSth int ret = SA_OK; 22275b6e0c46Sdougm struct stat st; 22286185db85Sdougm 2229a237e38eSth share_t sh; 2230a237e38eSth /* 2231a237e38eSth * Both the path and the proto are 2232a237e38eSth * keys into the sharetab. 2233a237e38eSth */ 2234a237e38eSth if (path != NULL && proto != NULL) { 2235a237e38eSth (void) memset(&sh, '\0', sizeof (sh)); 2236a237e38eSth sh.sh_path = path; 2237a237e38eSth sh.sh_fstype = proto; 2238a237e38eSth 2239a3175730Sth ret = _sharefs(SHAREFS_REMOVE, &sh); 22405b6e0c46Sdougm if (handle != NULL && stat(SA_LEGACY_SHARETAB, &st) == 0) 22415b6e0c46Sdougm sa_update_sharetab_ts(handle); 22426185db85Sdougm } 22436185db85Sdougm return (ret); 22446185db85Sdougm } 22455b6e0c46Sdougm 22465b6e0c46Sdougm /* 22475b6e0c46Sdougm * sa_needs_refresh(handle) 22485b6e0c46Sdougm * 224909c9e6dcSChris Williamson * Returns B_TRUE if the internal cache needs to be refreshed due to a 22505b6e0c46Sdougm * change by another process. B_FALSE returned otherwise. 22515b6e0c46Sdougm */ 22525b6e0c46Sdougm boolean_t 2253b83b3cacSdougm sa_needs_refresh(sa_handle_t handle) 22545b6e0c46Sdougm { 22555b6e0c46Sdougm sa_handle_impl_t implhandle = (sa_handle_impl_t)handle; 22565b6e0c46Sdougm struct stat st; 22575b6e0c46Sdougm char *str; 22585b6e0c46Sdougm uint64_t tstamp; 22595b6e0c46Sdougm scf_simple_prop_t *prop; 22605b6e0c46Sdougm 22618a981c33SDaniel Hoffman if (handle == NULL || implhandle->sa_service & 22628a981c33SDaniel Hoffman (SA_INIT_ONE_SHARE_FROM_NAME | SA_INIT_ONE_SHARE_FROM_HANDLE)) 22635b6e0c46Sdougm return (B_TRUE); 22645b6e0c46Sdougm 22655b6e0c46Sdougm /* 22665b6e0c46Sdougm * If sharetab has changed, then there was an external 22675b6e0c46Sdougm * change. Check sharetab first since it is updated by ZFS as 22685b6e0c46Sdougm * well as sharemgr. This is where external ZFS changes are 22695b6e0c46Sdougm * caught. 22705b6e0c46Sdougm */ 22715b6e0c46Sdougm if (stat(SA_LEGACY_SHARETAB, &st) == 0 && 22725b6e0c46Sdougm TSTAMP(st.st_mtim) != implhandle->tssharetab) 22735b6e0c46Sdougm return (B_TRUE); 22745b6e0c46Sdougm 22755b6e0c46Sdougm /* 22765b6e0c46Sdougm * If sharetab wasn't changed, check whether there were any 22775b6e0c46Sdougm * SMF transactions that modified the config but didn't 22785b6e0c46Sdougm * initiate a share. This is less common but does happen. 22795b6e0c46Sdougm */ 22805b6e0c46Sdougm prop = scf_simple_prop_get(implhandle->scfhandle->handle, 22815b6e0c46Sdougm (const char *)SA_SVC_FMRI_BASE ":default", "state", 22825b6e0c46Sdougm "lastupdate"); 22835b6e0c46Sdougm if (prop != NULL) { 22845b6e0c46Sdougm str = scf_simple_prop_next_astring(prop); 22855b6e0c46Sdougm if (str != NULL) 22865b6e0c46Sdougm tstamp = strtoull(str, NULL, 0); 22875b6e0c46Sdougm else 22885b6e0c46Sdougm tstamp = 0; 22895b6e0c46Sdougm scf_simple_prop_free(prop); 22905b6e0c46Sdougm if (tstamp != implhandle->tstrans) 22915b6e0c46Sdougm return (B_TRUE); 22925b6e0c46Sdougm } 22935b6e0c46Sdougm 22945b6e0c46Sdougm return (B_FALSE); 22955b6e0c46Sdougm } 22965b6e0c46Sdougm 2297da6c28aaSamw /* 2298da6c28aaSamw * sa_fix_resource_name(path) 2299da6c28aaSamw * 230089dc44ceSjose borrego * Convert invalid characters in a resource name (SMB share name) 230189dc44ceSjose borrego * to underscores ('_'). The list of invalid characters includes 230289dc44ceSjose borrego * control characters and the following: 230389dc44ceSjose borrego * 230489dc44ceSjose borrego * " / \ [ ] : | < > + ; , ? * = 230589dc44ceSjose borrego * 230689dc44ceSjose borrego * The caller must pass a valid path. Leading and trailing slashes 230789dc44ceSjose borrego * are stripped from the path before converting invalid characters. 230889dc44ceSjose borrego * Resource names are restricted to SA_MAX_RESOURCE_NAME characters. 2309da6c28aaSamw */ 2310da6c28aaSamw void 2311da6c28aaSamw sa_fix_resource_name(char *path) 2312da6c28aaSamw { 231389dc44ceSjose borrego char *invalid = "\"/\\[]:|<>+;,?*="; 231489dc44ceSjose borrego char *p = path; 231589dc44ceSjose borrego char *q; 2316da6c28aaSamw size_t len; 2317da6c28aaSamw 2318da6c28aaSamw assert(path != NULL); 2319da6c28aaSamw 232089dc44ceSjose borrego /* 232189dc44ceSjose borrego * Strip leading and trailing /'s. 232289dc44ceSjose borrego */ 232389dc44ceSjose borrego p += strspn(p, "/"); 232489dc44ceSjose borrego q = strchr(p, '\0'); 232589dc44ceSjose borrego if (q != NULL && q != path) { 232689dc44ceSjose borrego while ((--q, *q == '/')) 232789dc44ceSjose borrego *q = '\0'; 2328da6c28aaSamw } 232989dc44ceSjose borrego 233089dc44ceSjose borrego if (*p == '\0') { 233189dc44ceSjose borrego (void) strcpy(path, "_"); 233289dc44ceSjose borrego return; 2333da6c28aaSamw } 2334da6c28aaSamw 2335da6c28aaSamw /* 233689dc44ceSjose borrego * Stride over path components until the remaining 233789dc44ceSjose borrego * path is no longer than SA_MAX_RESOURCE_NAME. 2338da6c28aaSamw */ 233989dc44ceSjose borrego q = p; 234089dc44ceSjose borrego while ((q != NULL) && (strlen(q) > SA_MAX_RESOURCE_NAME)) { 234189dc44ceSjose borrego if ((q = strchr(q, '/')) != NULL) { 234289dc44ceSjose borrego ++q; 234389dc44ceSjose borrego p = q; 2344da6c28aaSamw } 234589dc44ceSjose borrego } 234689dc44ceSjose borrego 234789dc44ceSjose borrego /* 234889dc44ceSjose borrego * If the path is still longer than SA_MAX_RESOURCE_NAME, 234989dc44ceSjose borrego * take the trailing SA_MAX_RESOURCE_NAME characters. 235089dc44ceSjose borrego */ 235189dc44ceSjose borrego if ((len = strlen(p)) > SA_MAX_RESOURCE_NAME) { 235289dc44ceSjose borrego len = SA_MAX_RESOURCE_NAME; 235389dc44ceSjose borrego p = strchr(p, '\0') - (SA_MAX_RESOURCE_NAME - 1); 235489dc44ceSjose borrego } 235589dc44ceSjose borrego 235689dc44ceSjose borrego (void) memmove(path, p, len); 235789dc44ceSjose borrego path[len] = '\0'; 235889dc44ceSjose borrego 235989dc44ceSjose borrego for (p = path; *p != '\0'; ++p) { 236089dc44ceSjose borrego if ((iscntrl(*p)) || strchr(invalid, *p)) 236189dc44ceSjose borrego *p = '_'; 2362da6c28aaSamw } 2363da6c28aaSamw } 2364