16185db85Sdougm /* 26185db85Sdougm * CDDL HEADER START 36185db85Sdougm * 46185db85Sdougm * The contents of this file are subject to the terms of the 56185db85Sdougm * Common Development and Distribution License (the "License"). 66185db85Sdougm * You may not use this file except in compliance with the License. 76185db85Sdougm * 86185db85Sdougm * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 96185db85Sdougm * or http://www.opensolaris.org/os/licensing. 106185db85Sdougm * See the License for the specific language governing permissions 116185db85Sdougm * and limitations under the License. 126185db85Sdougm * 136185db85Sdougm * When distributing Covered Code, include this CDDL HEADER in each 146185db85Sdougm * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 156185db85Sdougm * If applicable, add the following below this CDDL HEADER, with the 166185db85Sdougm * fields enclosed by brackets "[]" replaced with your own identifying 176185db85Sdougm * information: Portions Copyright [yyyy] [name of copyright owner] 186185db85Sdougm * 196185db85Sdougm * CDDL HEADER END 206185db85Sdougm */ 216185db85Sdougm 226185db85Sdougm /* 23148c5f43SAlan Wright * Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved. 245cb0d679SMarcel Telka * Copyright 2014 Nexenta Systems, Inc. All rights reserved. 25*8a981c33SDaniel Hoffman * Copyright (c) 2016 by Delphix. All rights reserved. 266185db85Sdougm */ 276185db85Sdougm 286185db85Sdougm /* 296185db85Sdougm * Share control API 306185db85Sdougm */ 316185db85Sdougm #include <stdio.h> 326185db85Sdougm #include <string.h> 336185db85Sdougm #include <ctype.h> 346185db85Sdougm #include <sys/types.h> 356185db85Sdougm #include <sys/stat.h> 36a99982a7Sdougm #include <fcntl.h> 376185db85Sdougm #include <unistd.h> 386185db85Sdougm #include <libxml/parser.h> 396185db85Sdougm #include <libxml/tree.h> 406185db85Sdougm #include "libshare.h" 416185db85Sdougm #include "libshare_impl.h" 426185db85Sdougm #include <libscf.h> 436185db85Sdougm #include "scfutil.h" 446185db85Sdougm #include <ctype.h> 456185db85Sdougm #include <libintl.h> 46549ec3ffSdougm #include <thread.h> 47549ec3ffSdougm #include <synch.h> 48015408caSJean McCormack #include <errno.h> 496185db85Sdougm 50a99982a7Sdougm #define DFS_LOCK_FILE "/etc/dfs/fstypes" 5157b448deSdougm #define SA_STRSIZE 256 /* max string size for names */ 52a99982a7Sdougm 53da6c28aaSamw /* 54da6c28aaSamw * internal object type values returned by sa_get_object_type() 55da6c28aaSamw */ 56da6c28aaSamw #define SA_TYPE_UNKNOWN 0 57da6c28aaSamw #define SA_TYPE_GROUP 1 58da6c28aaSamw #define SA_TYPE_SHARE 2 59da6c28aaSamw #define SA_TYPE_RESOURCE 3 60da6c28aaSamw #define SA_TYPE_OPTIONSET 4 61da6c28aaSamw #define SA_TYPE_ALTSPACE 5 62da6c28aaSamw 636185db85Sdougm /* 646185db85Sdougm * internal data structures 656185db85Sdougm */ 666185db85Sdougm 676185db85Sdougm extern struct sa_proto_plugin *sap_proto_list; 686185db85Sdougm 696185db85Sdougm /* current SMF/SVC repository handle */ 70549ec3ffSdougm extern void getlegacyconfig(sa_handle_t, char *, xmlNodePtr *); 71549ec3ffSdougm extern int gettransients(sa_handle_impl_t, xmlNodePtr *); 72*8a981c33SDaniel Hoffman extern int get_one_transient(sa_handle_impl_t, xmlNodePtr *, char **, size_t); 736185db85Sdougm extern char *sa_fstype(char *); 746185db85Sdougm extern int sa_is_share(void *); 75da6c28aaSamw extern int sa_is_resource(void *); 766185db85Sdougm extern ssize_t scf_max_name_len; /* defined in scfutil during initialization */ 776185db85Sdougm extern int sa_group_is_zfs(sa_group_t); 786185db85Sdougm extern int sa_path_is_zfs(char *); 796185db85Sdougm extern int sa_zfs_set_sharenfs(sa_group_t, char *, int); 80da6c28aaSamw extern int sa_zfs_set_sharesmb(sa_group_t, char *, int); 81549ec3ffSdougm extern void update_legacy_config(sa_handle_t); 826185db85Sdougm extern int issubdir(char *, char *); 8357b448deSdougm extern int sa_zfs_init(sa_handle_impl_t); 84549ec3ffSdougm extern void sa_zfs_fini(sa_handle_impl_t); 85a99982a7Sdougm extern void sablocksigs(sigset_t *); 86a99982a7Sdougm extern void saunblocksigs(sigset_t *); 87da6c28aaSamw static sa_group_t sa_get_optionset_parent(sa_optionset_t); 88da6c28aaSamw static char *get_node_attr(void *, char *); 895b6e0c46Sdougm extern void sa_update_sharetab_ts(sa_handle_t); 906185db85Sdougm 91549ec3ffSdougm /* 92549ec3ffSdougm * Data structures for finding/managing the document root to access 93549ec3ffSdougm * handle mapping. The list isn't expected to grow very large so a 94549ec3ffSdougm * simple list is acceptable. The purpose is to provide a way to start 95549ec3ffSdougm * with a group or share and find the library handle needed for 96549ec3ffSdougm * various operations. 97549ec3ffSdougm */ 98549ec3ffSdougm mutex_t sa_global_lock; 99549ec3ffSdougm struct doc2handle { 100549ec3ffSdougm struct doc2handle *next; 101549ec3ffSdougm xmlNodePtr root; 102549ec3ffSdougm sa_handle_impl_t handle; 103549ec3ffSdougm }; 104549ec3ffSdougm 105148c5f43SAlan Wright mutex_t sa_dfstab_lock; 106148c5f43SAlan Wright 10757b448deSdougm /* definitions used in a couple of property functions */ 10857b448deSdougm #define SA_PROP_OP_REMOVE 1 10957b448deSdougm #define SA_PROP_OP_ADD 2 11057b448deSdougm #define SA_PROP_OP_UPDATE 3 11157b448deSdougm 112549ec3ffSdougm static struct doc2handle *sa_global_handles = NULL; 1136185db85Sdougm 1146185db85Sdougm /* helper functions */ 1156185db85Sdougm 116549ec3ffSdougm /* 117549ec3ffSdougm * sa_errorstr(err) 118549ec3ffSdougm * 119549ec3ffSdougm * convert an error value to an error string 120549ec3ffSdougm */ 121549ec3ffSdougm 1226185db85Sdougm char * 1236185db85Sdougm sa_errorstr(int err) 1246185db85Sdougm { 1256185db85Sdougm static char errstr[32]; 1266185db85Sdougm char *ret = NULL; 1276185db85Sdougm 1286185db85Sdougm switch (err) { 1296185db85Sdougm case SA_OK: 13057b448deSdougm ret = dgettext(TEXT_DOMAIN, "ok"); 13157b448deSdougm break; 1326185db85Sdougm case SA_NO_SUCH_PATH: 13357b448deSdougm ret = dgettext(TEXT_DOMAIN, "path doesn't exist"); 13457b448deSdougm break; 1356185db85Sdougm case SA_NO_MEMORY: 13657b448deSdougm ret = dgettext(TEXT_DOMAIN, "no memory"); 13757b448deSdougm break; 1386185db85Sdougm case SA_DUPLICATE_NAME: 13957b448deSdougm ret = dgettext(TEXT_DOMAIN, "name in use"); 14057b448deSdougm break; 1416185db85Sdougm case SA_BAD_PATH: 14257b448deSdougm ret = dgettext(TEXT_DOMAIN, "bad path"); 14357b448deSdougm break; 1446185db85Sdougm case SA_NO_SUCH_GROUP: 14557b448deSdougm ret = dgettext(TEXT_DOMAIN, "no such group"); 14657b448deSdougm break; 1476185db85Sdougm case SA_CONFIG_ERR: 14857b448deSdougm ret = dgettext(TEXT_DOMAIN, "configuration error"); 14957b448deSdougm break; 1506185db85Sdougm case SA_SYSTEM_ERR: 15157b448deSdougm ret = dgettext(TEXT_DOMAIN, "system error"); 15257b448deSdougm break; 1536185db85Sdougm case SA_SYNTAX_ERR: 15457b448deSdougm ret = dgettext(TEXT_DOMAIN, "syntax error"); 15557b448deSdougm break; 1566185db85Sdougm case SA_NO_PERMISSION: 15757b448deSdougm ret = dgettext(TEXT_DOMAIN, "no permission"); 15857b448deSdougm break; 1596185db85Sdougm case SA_BUSY: 16057b448deSdougm ret = dgettext(TEXT_DOMAIN, "busy"); 16157b448deSdougm break; 1626185db85Sdougm case SA_NO_SUCH_PROP: 16357b448deSdougm ret = dgettext(TEXT_DOMAIN, "no such property"); 16457b448deSdougm break; 1656185db85Sdougm case SA_INVALID_NAME: 16657b448deSdougm ret = dgettext(TEXT_DOMAIN, "invalid name"); 16757b448deSdougm break; 1686185db85Sdougm case SA_INVALID_PROTOCOL: 16957b448deSdougm ret = dgettext(TEXT_DOMAIN, "invalid protocol"); 17057b448deSdougm break; 1716185db85Sdougm case SA_NOT_ALLOWED: 17257b448deSdougm ret = dgettext(TEXT_DOMAIN, "operation not allowed"); 17357b448deSdougm break; 1746185db85Sdougm case SA_BAD_VALUE: 17557b448deSdougm ret = dgettext(TEXT_DOMAIN, "bad property value"); 17657b448deSdougm break; 1776185db85Sdougm case SA_INVALID_SECURITY: 17857b448deSdougm ret = dgettext(TEXT_DOMAIN, "invalid security type"); 17957b448deSdougm break; 1806185db85Sdougm case SA_NO_SUCH_SECURITY: 18157b448deSdougm ret = dgettext(TEXT_DOMAIN, "security type not found"); 18257b448deSdougm break; 1836185db85Sdougm case SA_VALUE_CONFLICT: 18457b448deSdougm ret = dgettext(TEXT_DOMAIN, "property value conflict"); 18557b448deSdougm break; 1866185db85Sdougm case SA_NOT_IMPLEMENTED: 18757b448deSdougm ret = dgettext(TEXT_DOMAIN, "not implemented"); 18857b448deSdougm break; 1896185db85Sdougm case SA_INVALID_PATH: 19057b448deSdougm ret = dgettext(TEXT_DOMAIN, "invalid path"); 19157b448deSdougm break; 1926185db85Sdougm case SA_NOT_SUPPORTED: 19357b448deSdougm ret = dgettext(TEXT_DOMAIN, "operation not supported"); 19457b448deSdougm break; 1956185db85Sdougm case SA_PROP_SHARE_ONLY: 19657b448deSdougm ret = dgettext(TEXT_DOMAIN, "property not valid for group"); 19757b448deSdougm break; 1986185db85Sdougm case SA_NOT_SHARED: 19957b448deSdougm ret = dgettext(TEXT_DOMAIN, "not shared"); 20057b448deSdougm break; 201da6c28aaSamw case SA_NO_SUCH_RESOURCE: 202da6c28aaSamw ret = dgettext(TEXT_DOMAIN, "no such resource"); 203da6c28aaSamw break; 204da6c28aaSamw case SA_RESOURCE_REQUIRED: 205da6c28aaSamw ret = dgettext(TEXT_DOMAIN, "resource name required"); 206da6c28aaSamw break; 207da6c28aaSamw case SA_MULTIPLE_ERROR: 208da6c28aaSamw ret = dgettext(TEXT_DOMAIN, "errors from multiple protocols"); 209da6c28aaSamw break; 210da6c28aaSamw case SA_PATH_IS_SUBDIR: 211da6c28aaSamw ret = dgettext(TEXT_DOMAIN, "path is a subpath of share"); 212da6c28aaSamw break; 213da6c28aaSamw case SA_PATH_IS_PARENTDIR: 214da6c28aaSamw ret = dgettext(TEXT_DOMAIN, "path is parent of a share"); 215da6c28aaSamw break; 2164bff34e3Sthurlow case SA_NO_SECTION: 2174bff34e3Sthurlow ret = dgettext(TEXT_DOMAIN, "protocol requires a section"); 2184bff34e3Sthurlow break; 2194bff34e3Sthurlow case SA_NO_PROPERTIES: 2204bff34e3Sthurlow ret = dgettext(TEXT_DOMAIN, "properties not found"); 2214bff34e3Sthurlow break; 2224bff34e3Sthurlow case SA_NO_SUCH_SECTION: 2234bff34e3Sthurlow ret = dgettext(TEXT_DOMAIN, "section not found"); 2244bff34e3Sthurlow break; 2254bff34e3Sthurlow case SA_PASSWORD_ENC: 2264bff34e3Sthurlow ret = dgettext(TEXT_DOMAIN, "passwords must be encrypted"); 2274bff34e3Sthurlow break; 2280616fd7fSPavel Filipensky case SA_SHARE_EXISTS: 2290616fd7fSPavel Filipensky ret = dgettext(TEXT_DOMAIN, "path or file is already shared"); 2300616fd7fSPavel Filipensky break; 2316185db85Sdougm default: 23257b448deSdougm (void) snprintf(errstr, sizeof (errstr), 23357b448deSdougm dgettext(TEXT_DOMAIN, "unknown %d"), err); 23457b448deSdougm ret = errstr; 2356185db85Sdougm } 2366185db85Sdougm return (ret); 2376185db85Sdougm } 2386185db85Sdougm 239549ec3ffSdougm /* 240549ec3ffSdougm * Document root to active handle mapping functions. These are only 241549ec3ffSdougm * used internally. A mutex is used to prevent access while the list 242549ec3ffSdougm * is changing. In general, the list will be relatively short - one 243549ec3ffSdougm * item per thread that has called sa_init(). 244549ec3ffSdougm */ 245549ec3ffSdougm 246549ec3ffSdougm sa_handle_impl_t 247549ec3ffSdougm get_handle_for_root(xmlNodePtr root) 248549ec3ffSdougm { 249549ec3ffSdougm struct doc2handle *item; 250549ec3ffSdougm 251549ec3ffSdougm (void) mutex_lock(&sa_global_lock); 252549ec3ffSdougm for (item = sa_global_handles; item != NULL; item = item->next) { 25357b448deSdougm if (item->root == root) 25457b448deSdougm break; 255549ec3ffSdougm } 256549ec3ffSdougm (void) mutex_unlock(&sa_global_lock); 257549ec3ffSdougm if (item != NULL) 25857b448deSdougm return (item->handle); 259549ec3ffSdougm return (NULL); 260549ec3ffSdougm } 261549ec3ffSdougm 262549ec3ffSdougm static int 263549ec3ffSdougm add_handle_for_root(xmlNodePtr root, sa_handle_impl_t handle) 264549ec3ffSdougm { 265549ec3ffSdougm struct doc2handle *item; 266549ec3ffSdougm int ret = SA_NO_MEMORY; 267549ec3ffSdougm 268549ec3ffSdougm item = (struct doc2handle *)calloc(sizeof (struct doc2handle), 1); 269549ec3ffSdougm if (item != NULL) { 27057b448deSdougm item->root = root; 27157b448deSdougm item->handle = handle; 27257b448deSdougm (void) mutex_lock(&sa_global_lock); 27357b448deSdougm item->next = sa_global_handles; 27457b448deSdougm sa_global_handles = item; 27557b448deSdougm (void) mutex_unlock(&sa_global_lock); 27657b448deSdougm ret = SA_OK; 277549ec3ffSdougm } 278549ec3ffSdougm return (ret); 279549ec3ffSdougm } 280549ec3ffSdougm 281549ec3ffSdougm /* 282549ec3ffSdougm * remove_handle_for_root(root) 283549ec3ffSdougm * 284549ec3ffSdougm * Walks the list of handles and removes the one for this "root" from 285549ec3ffSdougm * the list. It is up to the caller to free the data. 286549ec3ffSdougm */ 287549ec3ffSdougm 288549ec3ffSdougm static void 289549ec3ffSdougm remove_handle_for_root(xmlNodePtr root) 290549ec3ffSdougm { 291549ec3ffSdougm struct doc2handle *item, *prev; 292549ec3ffSdougm 293549ec3ffSdougm (void) mutex_lock(&sa_global_lock); 294549ec3ffSdougm for (prev = NULL, item = sa_global_handles; item != NULL; 29557b448deSdougm item = item->next) { 29657b448deSdougm if (item->root == root) { 29757b448deSdougm /* first in the list */ 29857b448deSdougm if (prev == NULL) 29957b448deSdougm sa_global_handles = sa_global_handles->next; 30057b448deSdougm else 30157b448deSdougm prev->next = item->next; 30257b448deSdougm /* Item is out of the list so free the list structure */ 30357b448deSdougm free(item); 30457b448deSdougm break; 305549ec3ffSdougm } 30657b448deSdougm prev = item; 307549ec3ffSdougm } 308549ec3ffSdougm (void) mutex_unlock(&sa_global_lock); 309549ec3ffSdougm } 310549ec3ffSdougm 311549ec3ffSdougm /* 312549ec3ffSdougm * sa_find_group_handle(sa_group_t group) 313549ec3ffSdougm * 314549ec3ffSdougm * Find the sa_handle_t for the configuration associated with this 315549ec3ffSdougm * group. 316549ec3ffSdougm */ 317549ec3ffSdougm sa_handle_t 318549ec3ffSdougm sa_find_group_handle(sa_group_t group) 319549ec3ffSdougm { 320549ec3ffSdougm xmlNodePtr node = (xmlNodePtr)group; 321549ec3ffSdougm sa_handle_t handle; 322549ec3ffSdougm 323549ec3ffSdougm while (node != NULL) { 32457b448deSdougm if (strcmp((char *)(node->name), "sharecfg") == 0) { 32557b448deSdougm /* have the root so get the handle */ 32657b448deSdougm handle = (sa_handle_t)get_handle_for_root(node); 32757b448deSdougm return (handle); 32857b448deSdougm } 32957b448deSdougm node = node->parent; 330549ec3ffSdougm } 331549ec3ffSdougm return (NULL); 332549ec3ffSdougm } 333549ec3ffSdougm 3346185db85Sdougm /* 3356185db85Sdougm * set_legacy_timestamp(root, path, timevalue) 3366185db85Sdougm * 3376185db85Sdougm * add the current timestamp value to the configuration for use in 3386185db85Sdougm * determining when to update the legacy files. For SMF, this 3396185db85Sdougm * property is kept in default/operation/legacy_timestamp 3406185db85Sdougm */ 3416185db85Sdougm 3426185db85Sdougm static void 3436185db85Sdougm set_legacy_timestamp(xmlNodePtr root, char *path, uint64_t tval) 3446185db85Sdougm { 3456185db85Sdougm xmlNodePtr node; 3466185db85Sdougm xmlChar *lpath = NULL; 347549ec3ffSdougm sa_handle_impl_t handle; 348549ec3ffSdougm 349549ec3ffSdougm /* Have to have a handle or else we weren't initialized. */ 350549ec3ffSdougm handle = get_handle_for_root(root); 351549ec3ffSdougm if (handle == NULL) 35257b448deSdougm return; 3536185db85Sdougm 3546185db85Sdougm for (node = root->xmlChildrenNode; node != NULL; 35557b448deSdougm node = node->next) { 35657b448deSdougm if (xmlStrcmp(node->name, (xmlChar *)"legacy") == 0) { 35757b448deSdougm /* a possible legacy node for this path */ 35857b448deSdougm lpath = xmlGetProp(node, (xmlChar *)"path"); 35957b448deSdougm if (lpath != NULL && 36057b448deSdougm xmlStrcmp(lpath, (xmlChar *)path) == 0) { 36157b448deSdougm xmlFree(lpath); 36257b448deSdougm break; 36357b448deSdougm } 36457b448deSdougm if (lpath != NULL) 36557b448deSdougm xmlFree(lpath); 3666185db85Sdougm } 3676185db85Sdougm } 3686185db85Sdougm if (node == NULL) { 36957b448deSdougm /* need to create the first legacy timestamp node */ 37057b448deSdougm node = xmlNewChild(root, NULL, (xmlChar *)"legacy", NULL); 3716185db85Sdougm } 3726185db85Sdougm if (node != NULL) { 37357b448deSdougm char tstring[32]; 37457b448deSdougm int ret; 37557b448deSdougm 37657b448deSdougm (void) snprintf(tstring, sizeof (tstring), "%lld", tval); 3774bff34e3Sthurlow (void) xmlSetProp(node, (xmlChar *)"timestamp", 3784bff34e3Sthurlow (xmlChar *)tstring); 3794bff34e3Sthurlow (void) xmlSetProp(node, (xmlChar *)"path", (xmlChar *)path); 38057b448deSdougm /* now commit to SMF */ 38157b448deSdougm ret = sa_get_instance(handle->scfhandle, "default"); 3826185db85Sdougm if (ret == SA_OK) { 38357b448deSdougm ret = sa_start_transaction(handle->scfhandle, 38457b448deSdougm "operation"); 38557b448deSdougm if (ret == SA_OK) { 38657b448deSdougm ret = sa_set_property(handle->scfhandle, 38757b448deSdougm "legacy-timestamp", tstring); 38857b448deSdougm if (ret == SA_OK) { 38957b448deSdougm (void) sa_end_transaction( 3905b6e0c46Sdougm handle->scfhandle, handle); 39157b448deSdougm } else { 39257b448deSdougm sa_abort_transaction(handle->scfhandle); 39357b448deSdougm } 39457b448deSdougm } 3956185db85Sdougm } 3966185db85Sdougm } 3976185db85Sdougm } 3986185db85Sdougm 3996185db85Sdougm /* 4006185db85Sdougm * is_shared(share) 4016185db85Sdougm * 4026185db85Sdougm * determine if the specified share is currently shared or not. 4036185db85Sdougm */ 4046185db85Sdougm static int 4056185db85Sdougm is_shared(sa_share_t share) 4066185db85Sdougm { 4076185db85Sdougm char *shared; 4086185db85Sdougm int result = 0; /* assume not */ 4096185db85Sdougm 4106185db85Sdougm shared = sa_get_share_attr(share, "shared"); 4116185db85Sdougm if (shared != NULL) { 41257b448deSdougm if (strcmp(shared, "true") == 0) 41357b448deSdougm result = 1; 41457b448deSdougm sa_free_attr_string(shared); 4156185db85Sdougm } 4166185db85Sdougm return (result); 4176185db85Sdougm } 4186185db85Sdougm 419da6c28aaSamw /* 420da6c28aaSamw * excluded_protocol(share, proto) 421da6c28aaSamw * 422da6c28aaSamw * Returns B_TRUE if the specified protocol appears in the "exclude" 423da6c28aaSamw * property. This is used to prevent sharing special case shares 424da6c28aaSamw * (e.g. subdirs when SMB wants a subdir and NFS doesn't. B_FALSE is 425da6c28aaSamw * returned if the protocol isn't in the list. 426da6c28aaSamw */ 427da6c28aaSamw static boolean_t 428da6c28aaSamw excluded_protocol(sa_share_t share, char *proto) 429da6c28aaSamw { 430da6c28aaSamw char *protolist; 431da6c28aaSamw char *str; 432da6c28aaSamw char *token; 433da6c28aaSamw 434da6c28aaSamw protolist = sa_get_share_attr(share, "exclude"); 435da6c28aaSamw if (protolist != NULL) { 436da6c28aaSamw str = protolist; 437da6c28aaSamw while ((token = strtok(str, ",")) != NULL) { 438da6c28aaSamw if (strcmp(token, proto) == 0) { 439da6c28aaSamw sa_free_attr_string(protolist); 440da6c28aaSamw return (B_TRUE); 441da6c28aaSamw } 442da6c28aaSamw str = NULL; 443da6c28aaSamw } 444da6c28aaSamw sa_free_attr_string(protolist); 445da6c28aaSamw } 446da6c28aaSamw return (B_FALSE); 447da6c28aaSamw } 448da6c28aaSamw 4496185db85Sdougm /* 450a99982a7Sdougm * checksubdirgroup(group, newpath, strictness) 451f345c0beSdougm * 452a99982a7Sdougm * check all the specified newpath against all the paths in the 453a99982a7Sdougm * group. This is a helper function for checksubdir to make it easier 454a99982a7Sdougm * to also check ZFS subgroups. 455a99982a7Sdougm * The strictness values mean: 456f345c0beSdougm * SA_CHECK_NORMAL == only check newpath against shares that are active 457f345c0beSdougm * SA_CHECK_STRICT == check newpath against both active shares and those 458f345c0beSdougm * stored in the repository 4596185db85Sdougm */ 4606185db85Sdougm static int 461a99982a7Sdougm checksubdirgroup(sa_group_t group, char *newpath, int strictness) 4626185db85Sdougm { 4636185db85Sdougm sa_share_t share; 464a99982a7Sdougm char *path; 465a99982a7Sdougm int issub = SA_OK; 466da6c28aaSamw int subdir; 467da6c28aaSamw int parent; 468da6c28aaSamw 469da6c28aaSamw if (newpath == NULL) 470da6c28aaSamw return (SA_INVALID_PATH); 4716185db85Sdougm 472a99982a7Sdougm for (share = sa_get_share(group, NULL); share != NULL; 473a99982a7Sdougm share = sa_get_next_share(share)) { 4746185db85Sdougm /* 4756185db85Sdougm * The original behavior of share never checked 4766185db85Sdougm * against the permanent configuration 4776185db85Sdougm * (/etc/dfs/dfstab). PIT has a number of cases where 4786185db85Sdougm * it depends on this older behavior even though it 4796185db85Sdougm * could be considered incorrect. We may tighten this 4806185db85Sdougm * up in the future. 4816185db85Sdougm */ 48257b448deSdougm if (strictness == SA_CHECK_NORMAL && !is_shared(share)) 48357b448deSdougm continue; 4846185db85Sdougm 48557b448deSdougm path = sa_get_share_attr(share, "path"); 486f345c0beSdougm /* 487f345c0beSdougm * If path is NULL, then a share is in the process of 488f345c0beSdougm * construction or someone has modified the property 489a99982a7Sdougm * group inappropriately. It should be 490a99982a7Sdougm * ignored. issubdir() comes from the original share 491a99982a7Sdougm * implementation and does the difficult part of 492a99982a7Sdougm * checking subdirectories. 493f345c0beSdougm */ 49457b448deSdougm if (path == NULL) 49557b448deSdougm continue; 496da6c28aaSamw 497da6c28aaSamw if (strcmp(path, newpath) == 0) { 49857b448deSdougm issub = SA_INVALID_PATH; 499da6c28aaSamw } else { 500da6c28aaSamw subdir = issubdir(newpath, path); 501da6c28aaSamw parent = issubdir(path, newpath); 502da6c28aaSamw if (subdir || parent) { 503da6c28aaSamw sa_free_attr_string(path); 504da6c28aaSamw path = NULL; 505da6c28aaSamw return (subdir ? 506da6c28aaSamw SA_PATH_IS_SUBDIR : SA_PATH_IS_PARENTDIR); 507da6c28aaSamw } 50857b448deSdougm } 5096185db85Sdougm sa_free_attr_string(path); 5106185db85Sdougm path = NULL; 511a99982a7Sdougm } 512a99982a7Sdougm return (issub); 513a99982a7Sdougm } 514a99982a7Sdougm 515a99982a7Sdougm /* 516a99982a7Sdougm * checksubdir(newpath, strictness) 517a99982a7Sdougm * 518a99982a7Sdougm * checksubdir determines if the specified path (newpath) is a 519a99982a7Sdougm * subdirectory of another share. It calls checksubdirgroup() to do 520a99982a7Sdougm * the complicated work. The strictness parameter determines how 521a99982a7Sdougm * strict a check to make against the path. The strictness values 522a99982a7Sdougm * mean: SA_CHECK_NORMAL == only check newpath against shares that are 523a99982a7Sdougm * active SA_CHECK_STRICT == check newpath against both active shares 524a99982a7Sdougm * and those * stored in the repository 525a99982a7Sdougm */ 526a99982a7Sdougm static int 527549ec3ffSdougm checksubdir(sa_handle_t handle, char *newpath, int strictness) 528a99982a7Sdougm { 529a99982a7Sdougm sa_group_t group; 530da6c28aaSamw int issub = SA_OK; 531a99982a7Sdougm char *path = NULL; 532a99982a7Sdougm 533da6c28aaSamw for (group = sa_get_group(handle, NULL); 534da6c28aaSamw group != NULL && issub == SA_OK; 535da6c28aaSamw group = sa_get_next_group(group)) { 53657b448deSdougm if (sa_group_is_zfs(group)) { 53757b448deSdougm sa_group_t subgroup; 53857b448deSdougm for (subgroup = sa_get_sub_group(group); 539da6c28aaSamw subgroup != NULL && issub == SA_OK; 54057b448deSdougm subgroup = sa_get_next_group(subgroup)) 54157b448deSdougm issub = checksubdirgroup(subgroup, newpath, 54257b448deSdougm strictness); 54357b448deSdougm } else { 54457b448deSdougm issub = checksubdirgroup(group, newpath, strictness); 54557b448deSdougm } 5466185db85Sdougm } 5476185db85Sdougm if (path != NULL) 54857b448deSdougm sa_free_attr_string(path); 5496185db85Sdougm return (issub); 5506185db85Sdougm } 5516185db85Sdougm 5526185db85Sdougm /* 553f345c0beSdougm * validpath(path, strictness) 5546185db85Sdougm * determine if the provided path is valid for a share. It shouldn't 5556185db85Sdougm * be a sub-dir of an already shared path or the parent directory of a 5566185db85Sdougm * share path. 5576185db85Sdougm */ 5586185db85Sdougm static int 559549ec3ffSdougm validpath(sa_handle_t handle, char *path, int strictness) 5606185db85Sdougm { 5616185db85Sdougm int error = SA_OK; 5626185db85Sdougm struct stat st; 5636185db85Sdougm sa_share_t share; 5646185db85Sdougm char *fstype; 5656185db85Sdougm 56657b448deSdougm if (*path != '/') 56757b448deSdougm return (SA_BAD_PATH); 56857b448deSdougm 5696185db85Sdougm if (stat(path, &st) < 0) { 57057b448deSdougm error = SA_NO_SUCH_PATH; 5716185db85Sdougm } else { 57257b448deSdougm share = sa_find_share(handle, path); 57357b448deSdougm if (share != NULL) 57457b448deSdougm error = SA_DUPLICATE_NAME; 57557b448deSdougm 57657b448deSdougm if (error == SA_OK) { 57757b448deSdougm /* 57857b448deSdougm * check for special case with file system 57957b448deSdougm * that might have restrictions. For now, ZFS 58057b448deSdougm * is the only case since it has its own idea 58157b448deSdougm * of how to configure shares. We do this 58257b448deSdougm * before subdir checking since things like 58357b448deSdougm * ZFS will do that for us. This should also 58457b448deSdougm * be done via plugin interface. 58557b448deSdougm */ 58657b448deSdougm fstype = sa_fstype(path); 58757b448deSdougm if (fstype != NULL && strcmp(fstype, "zfs") == 0) { 58857b448deSdougm if (sa_zfs_is_shared(handle, path)) 58957b448deSdougm error = SA_INVALID_NAME; 59057b448deSdougm } 59157b448deSdougm if (fstype != NULL) 59257b448deSdougm sa_free_fstype(fstype); 5936185db85Sdougm } 59457b448deSdougm if (error == SA_OK) 59557b448deSdougm error = checksubdir(handle, path, strictness); 5966185db85Sdougm } 5976185db85Sdougm return (error); 5986185db85Sdougm } 5996185db85Sdougm 6006185db85Sdougm /* 6016185db85Sdougm * check to see if group/share is persistent. 602da6c28aaSamw * 603da6c28aaSamw * "group" can be either an sa_group_t or an sa_share_t. (void *) 6045cb0d679SMarcel Telka * works since both these types are also void *. 605148c5f43SAlan Wright * If the share is a ZFS share, mark it as persistent. 6066185db85Sdougm */ 607da6c28aaSamw int 608da6c28aaSamw sa_is_persistent(void *group) 6096185db85Sdougm { 6106185db85Sdougm char *type; 6116185db85Sdougm int persist = 1; 612148c5f43SAlan Wright sa_group_t grp; 6136185db85Sdougm 614da6c28aaSamw type = sa_get_group_attr((sa_group_t)group, "type"); 615148c5f43SAlan Wright if (type != NULL) { 616148c5f43SAlan Wright if (strcmp(type, "transient") == 0) 617148c5f43SAlan Wright persist = 0; 61857b448deSdougm sa_free_attr_string(type); 619148c5f43SAlan Wright } 620148c5f43SAlan Wright 621148c5f43SAlan Wright grp = (sa_is_share(group)) ? sa_get_parent_group(group) : group; 622148c5f43SAlan Wright if (sa_group_is_zfs(grp)) 623148c5f43SAlan Wright persist = 1; 624148c5f43SAlan Wright 6256185db85Sdougm return (persist); 6266185db85Sdougm } 6276185db85Sdougm 6286185db85Sdougm /* 6296185db85Sdougm * sa_valid_group_name(name) 6306185db85Sdougm * 6316185db85Sdougm * check that the "name" contains only valid characters and otherwise 6326185db85Sdougm * fits the required naming conventions. Valid names must start with 6336185db85Sdougm * an alphabetic and the remainder may consist of only alphanumeric 6346185db85Sdougm * plus the '-' and '_' characters. This name limitation comes from 6356185db85Sdougm * inherent limitations in SMF. 6366185db85Sdougm */ 6376185db85Sdougm 6386185db85Sdougm int 6396185db85Sdougm sa_valid_group_name(char *name) 6406185db85Sdougm { 6416185db85Sdougm int ret = 1; 6426185db85Sdougm ssize_t len; 6436185db85Sdougm 6446185db85Sdougm if (name != NULL && isalpha(*name)) { 64557b448deSdougm char c; 64657b448deSdougm len = strlen(name); 64757b448deSdougm if (len < (scf_max_name_len - sizeof ("group:"))) { 64857b448deSdougm for (c = *name++; c != '\0' && ret != 0; c = *name++) { 64957b448deSdougm if (!isalnum(c) && c != '-' && c != '_') 65057b448deSdougm ret = 0; 65157b448deSdougm } 65257b448deSdougm } else { 6536185db85Sdougm ret = 0; 6546185db85Sdougm } 6556185db85Sdougm } else { 65657b448deSdougm ret = 0; 6576185db85Sdougm } 6586185db85Sdougm return (ret); 6596185db85Sdougm } 6606185db85Sdougm 6616185db85Sdougm 6626185db85Sdougm /* 6636185db85Sdougm * is_zfs_group(group) 6646185db85Sdougm * Determine if the specified group is a ZFS sharenfs group 6656185db85Sdougm */ 6666185db85Sdougm static int 6676185db85Sdougm is_zfs_group(sa_group_t group) 6686185db85Sdougm { 6696185db85Sdougm int ret = 0; 6706185db85Sdougm xmlNodePtr parent; 6716185db85Sdougm xmlChar *zfs; 6726185db85Sdougm 67357b448deSdougm if (strcmp((char *)((xmlNodePtr)group)->name, "share") == 0) 67457b448deSdougm parent = (xmlNodePtr)sa_get_parent_group(group); 67557b448deSdougm else 67657b448deSdougm parent = (xmlNodePtr)group; 6776185db85Sdougm zfs = xmlGetProp(parent, (xmlChar *)"zfs"); 6786185db85Sdougm if (zfs != NULL) { 67957b448deSdougm xmlFree(zfs); 68057b448deSdougm ret = 1; 6816185db85Sdougm } 6826185db85Sdougm return (ret); 6836185db85Sdougm } 6846185db85Sdougm 685da6c28aaSamw /* 686da6c28aaSamw * sa_get_object_type(object) 687da6c28aaSamw * 688da6c28aaSamw * This function returns a numeric value representing the object 689da6c28aaSamw * type. This allows using simpler checks when doing type specific 690da6c28aaSamw * operations. 691da6c28aaSamw */ 692da6c28aaSamw 693da6c28aaSamw static int 694da6c28aaSamw sa_get_object_type(void *object) 695da6c28aaSamw { 696da6c28aaSamw xmlNodePtr node = (xmlNodePtr)object; 697da6c28aaSamw int type; 698da6c28aaSamw 699da6c28aaSamw if (xmlStrcmp(node->name, (xmlChar *)"group") == 0) 700da6c28aaSamw type = SA_TYPE_GROUP; 701da6c28aaSamw else if (xmlStrcmp(node->name, (xmlChar *)"share") == 0) 702da6c28aaSamw type = SA_TYPE_SHARE; 703da6c28aaSamw else if (xmlStrcmp(node->name, (xmlChar *)"resource") == 0) 704da6c28aaSamw type = SA_TYPE_RESOURCE; 705da6c28aaSamw else if (xmlStrcmp(node->name, (xmlChar *)"optionset") == 0) 706da6c28aaSamw type = SA_TYPE_OPTIONSET; 707da6c28aaSamw else if (xmlStrcmp(node->name, (xmlChar *)"security") == 0) 708da6c28aaSamw type = SA_TYPE_ALTSPACE; 709da6c28aaSamw else 710da6c28aaSamw assert(0); 711da6c28aaSamw return (type); 712da6c28aaSamw } 713da6c28aaSamw 7146185db85Sdougm /* 7156185db85Sdougm * sa_optionset_name(optionset, oname, len, id) 7166185db85Sdougm * return the SMF name for the optionset. If id is not NULL, it 7176185db85Sdougm * will have the GUID value for a share and should be used 7186185db85Sdougm * instead of the keyword "optionset" which is used for 7196185db85Sdougm * groups. If the optionset doesn't have a protocol type 7206185db85Sdougm * associated with it, "default" is used. This shouldn't happen 7216185db85Sdougm * at this point but may be desirable in the future if there are 7226185db85Sdougm * protocol independent properties added. The name is returned in 7236185db85Sdougm * oname. 7246185db85Sdougm */ 7256185db85Sdougm 7266185db85Sdougm static int 7276185db85Sdougm sa_optionset_name(sa_optionset_t optionset, char *oname, size_t len, char *id) 7286185db85Sdougm { 7296185db85Sdougm char *proto; 730da6c28aaSamw void *parent; 731da6c28aaSamw int ptype; 7326185db85Sdougm 7336185db85Sdougm if (id == NULL) 73457b448deSdougm id = "optionset"; 7356185db85Sdougm 736da6c28aaSamw parent = sa_get_optionset_parent(optionset); 737da6c28aaSamw if (parent != NULL) { 738da6c28aaSamw ptype = sa_get_object_type(parent); 739da6c28aaSamw proto = sa_get_optionset_attr(optionset, "type"); 740da6c28aaSamw if (ptype != SA_TYPE_RESOURCE) { 741da6c28aaSamw len = snprintf(oname, len, "%s_%s", id, 742da6c28aaSamw proto ? proto : "default"); 743da6c28aaSamw } else { 744da6c28aaSamw char *index; 745da6c28aaSamw index = get_node_attr((void *)parent, "id"); 746fe1c642dSBill Krier if (index != NULL) { 747da6c28aaSamw len = snprintf(oname, len, "%s_%s_%s", id, 748da6c28aaSamw proto ? proto : "default", index); 749fe1c642dSBill Krier sa_free_attr_string(index); 750fe1c642dSBill Krier } else { 751da6c28aaSamw len = 0; 752fe1c642dSBill Krier } 753da6c28aaSamw } 7546185db85Sdougm 755da6c28aaSamw if (proto != NULL) 756da6c28aaSamw sa_free_attr_string(proto); 757da6c28aaSamw } else { 758da6c28aaSamw len = 0; 759da6c28aaSamw } 7606185db85Sdougm return (len); 7616185db85Sdougm } 7626185db85Sdougm 7636185db85Sdougm /* 7646185db85Sdougm * sa_security_name(optionset, oname, len, id) 7656185db85Sdougm * 7666185db85Sdougm * return the SMF name for the security. If id is not NULL, it will 7676185db85Sdougm * have the GUID value for a share and should be used instead of the 7686185db85Sdougm * keyword "optionset" which is used for groups. If the optionset 7696185db85Sdougm * doesn't have a protocol type associated with it, "default" is 7706185db85Sdougm * used. This shouldn't happen at this point but may be desirable in 7716185db85Sdougm * the future if there are protocol independent properties added. The 7726185db85Sdougm * name is returned in oname. The security type is also encoded into 7736185db85Sdougm * the name. In the future, this wil *be handled a bit differently. 7746185db85Sdougm */ 7756185db85Sdougm 7766185db85Sdougm static int 7776185db85Sdougm sa_security_name(sa_security_t security, char *oname, size_t len, char *id) 7786185db85Sdougm { 7796185db85Sdougm char *proto; 7806185db85Sdougm char *sectype; 7816185db85Sdougm 7826185db85Sdougm if (id == NULL) 78357b448deSdougm id = "optionset"; 7846185db85Sdougm 7856185db85Sdougm proto = sa_get_security_attr(security, "type"); 7866185db85Sdougm sectype = sa_get_security_attr(security, "sectype"); 78757b448deSdougm len = snprintf(oname, len, "%s_%s_%s", id, proto ? proto : "default", 78857b448deSdougm sectype ? sectype : "default"); 7896185db85Sdougm if (proto != NULL) 79057b448deSdougm sa_free_attr_string(proto); 7916185db85Sdougm if (sectype != NULL) 79257b448deSdougm sa_free_attr_string(sectype); 7936185db85Sdougm return (len); 7946185db85Sdougm } 7956185db85Sdougm 79657b448deSdougm /* 79757b448deSdougm * verifydefgroupopts(handle) 79857b448deSdougm * 79957b448deSdougm * Make sure a "default" group exists and has default protocols enabled. 80057b448deSdougm */ 80157b448deSdougm static void 80257b448deSdougm verifydefgroupopts(sa_handle_t handle) 80357b448deSdougm { 80457b448deSdougm sa_group_t defgrp; 80557b448deSdougm sa_optionset_t opt; 806da6c28aaSamw 80757b448deSdougm defgrp = sa_get_group(handle, "default"); 80857b448deSdougm if (defgrp != NULL) { 80957b448deSdougm opt = sa_get_optionset(defgrp, NULL); 81057b448deSdougm /* 81157b448deSdougm * NFS is the default for default group 81257b448deSdougm */ 81357b448deSdougm if (opt == NULL) 81457b448deSdougm opt = sa_create_optionset(defgrp, "nfs"); 81557b448deSdougm } 81657b448deSdougm } 81757b448deSdougm 8186185db85Sdougm /* 819*8a981c33SDaniel Hoffman * sa_init_impl(init_service, arg) 8206185db85Sdougm * Initialize the API 8216185db85Sdougm * find all the shared objects 8226185db85Sdougm * init the tables with all objects 8236185db85Sdougm * read in the current configuration 824*8a981c33SDaniel Hoffman * 825*8a981c33SDaniel Hoffman * arg is a parameter passed in whose meaning is based on the init_service. 826*8a981c33SDaniel Hoffman * See libshare.h under API initialization. 8276185db85Sdougm */ 82857b448deSdougm #define GETPROP(prop) scf_simple_prop_next_astring(prop) 82957b448deSdougm #define CHECKTSTAMP(st, tval) stat(SA_LEGACY_DFSTAB, &st) >= 0 && \ 83057b448deSdougm tval != TSTAMP(st.st_ctim) 831*8a981c33SDaniel Hoffman static sa_handle_t 832*8a981c33SDaniel Hoffman sa_init_impl(int init_service, void *arg) 8336185db85Sdougm { 8346185db85Sdougm struct stat st; 835*8a981c33SDaniel Hoffman /* legacy is used for debugging only as far as I can tell */ 8366185db85Sdougm int legacy = 0; 8376185db85Sdougm uint64_t tval = 0; 838a99982a7Sdougm int lockfd; 839a99982a7Sdougm sigset_t old; 840a99982a7Sdougm int updatelegacy = B_FALSE; 841a99982a7Sdougm scf_simple_prop_t *prop; 842549ec3ffSdougm sa_handle_impl_t handle; 843549ec3ffSdougm int err; 844549ec3ffSdougm 845549ec3ffSdougm handle = calloc(sizeof (struct sa_handle_impl), 1); 8466185db85Sdougm 847549ec3ffSdougm if (handle != NULL) { 848*8a981c33SDaniel Hoffman handle->sa_service = init_service; 849c5f58477Sdougm /* 850c5f58477Sdougm * Get protocol specific structures, but only if this 851c5f58477Sdougm * is the only handle. 852c5f58477Sdougm */ 853c5f58477Sdougm (void) mutex_lock(&sa_global_lock); 854c5f58477Sdougm if (sa_global_handles == NULL) 855c5f58477Sdougm (void) proto_plugin_init(); 856c5f58477Sdougm (void) mutex_unlock(&sa_global_lock); 857*8a981c33SDaniel Hoffman if (init_service & (SA_INIT_SHARE_API | 858*8a981c33SDaniel Hoffman SA_INIT_SHARE_API_SELECTIVE | SA_INIT_ONE_SHARE_FROM_NAME | 859*8a981c33SDaniel Hoffman SA_INIT_ONE_SHARE_FROM_HANDLE)) { 860a99982a7Sdougm /* 86157b448deSdougm * initialize access into libzfs. We use this 86257b448deSdougm * when collecting info about ZFS datasets and 86357b448deSdougm * shares. 864a99982a7Sdougm */ 86557b448deSdougm if (sa_zfs_init(handle) == B_FALSE) { 86657b448deSdougm free(handle); 86789dc44ceSjose borrego (void) mutex_lock(&sa_global_lock); 86857b448deSdougm (void) proto_plugin_fini(); 86989dc44ceSjose borrego (void) mutex_unlock(&sa_global_lock); 87057b448deSdougm return (NULL); 87157b448deSdougm } 872a99982a7Sdougm /* 87357b448deSdougm * since we want to use SMF, initialize an svc handle 87457b448deSdougm * and find out what is there. 875a99982a7Sdougm */ 87657b448deSdougm handle->scfhandle = sa_scf_init(handle); 87757b448deSdougm if (handle->scfhandle != NULL) { 87857b448deSdougm /* 87957b448deSdougm * Need to lock the extraction of the 88057b448deSdougm * configuration if the dfstab file has 88157b448deSdougm * changed. Lock everything now and release if 88257b448deSdougm * not needed. Use a file that isn't being 88357b448deSdougm * manipulated by other parts of the system in 88457b448deSdougm * order to not interfere with locking. Using 88557b448deSdougm * dfstab doesn't work. 88657b448deSdougm */ 88757b448deSdougm sablocksigs(&old); 88857b448deSdougm lockfd = open(DFS_LOCK_FILE, O_RDWR); 88957b448deSdougm if (lockfd >= 0) { 89057b448deSdougm errno = 0; 89157b448deSdougm (void) lockf(lockfd, F_LOCK, 0); 892148c5f43SAlan Wright (void) mutex_lock(&sa_dfstab_lock); 89357b448deSdougm /* 89457b448deSdougm * Check whether we are going to need 89557b448deSdougm * to merge any dfstab changes. This 89657b448deSdougm * is done by comparing the value of 89757b448deSdougm * legacy-timestamp with the current 89857b448deSdougm * st_ctim of the file. If they are 89957b448deSdougm * different, an update is needed and 90057b448deSdougm * the file must remain locked until 90157b448deSdougm * the merge is done in order to 90257b448deSdougm * prevent multiple startups from 90357b448deSdougm * changing the SMF repository at the 90457b448deSdougm * same time. The first to get the 90557b448deSdougm * lock will make any changes before 90657b448deSdougm * the others can read the repository. 90757b448deSdougm */ 90857b448deSdougm prop = scf_simple_prop_get 90957b448deSdougm (handle->scfhandle->handle, 91057b448deSdougm (const char *)SA_SVC_FMRI_BASE 91157b448deSdougm ":default", "operation", 91257b448deSdougm "legacy-timestamp"); 91357b448deSdougm if (prop != NULL) { 91457b448deSdougm char *i64; 91557b448deSdougm i64 = GETPROP(prop); 91657b448deSdougm if (i64 != NULL) 91757b448deSdougm tval = strtoull(i64, 91857b448deSdougm NULL, 0); 91957b448deSdougm if (CHECKTSTAMP(st, tval)) 92057b448deSdougm updatelegacy = B_TRUE; 92157b448deSdougm scf_simple_prop_free(prop); 92257b448deSdougm } else { 92357b448deSdougm /* 92457b448deSdougm * We haven't set the 92557b448deSdougm * timestamp before so do it. 92657b448deSdougm */ 92757b448deSdougm updatelegacy = B_TRUE; 92857b448deSdougm } 929148c5f43SAlan Wright if (updatelegacy == B_FALSE) { 930148c5f43SAlan Wright (void) mutex_unlock( 931148c5f43SAlan Wright &sa_dfstab_lock); 932148c5f43SAlan Wright (void) lockf(lockfd, F_ULOCK, 933148c5f43SAlan Wright 0); 934148c5f43SAlan Wright (void) close(lockfd); 935148c5f43SAlan Wright } 9361d1813a7Sdougm 937148c5f43SAlan Wright } 93857b448deSdougm /* 93957b448deSdougm * It is essential that the document tree and 94057b448deSdougm * the internal list of roots to handles be 94157b448deSdougm * setup before anything that might try to 94257b448deSdougm * create a new object is called. The document 94357b448deSdougm * tree is the combination of handle->doc and 94457b448deSdougm * handle->tree. This allows searches, 94557b448deSdougm * etc. when all you have is an object in the 94657b448deSdougm * tree. 94757b448deSdougm */ 94857b448deSdougm handle->doc = xmlNewDoc((xmlChar *)"1.0"); 94957b448deSdougm handle->tree = xmlNewNode(NULL, 95057b448deSdougm (xmlChar *)"sharecfg"); 95157b448deSdougm if (handle->doc != NULL && 95257b448deSdougm handle->tree != NULL) { 9534bff34e3Sthurlow (void) xmlDocSetRootElement(handle->doc, 95457b448deSdougm handle->tree); 95557b448deSdougm err = add_handle_for_root(handle->tree, 95657b448deSdougm handle); 95757b448deSdougm if (err == SA_OK) 95857b448deSdougm err = sa_get_config( 95957b448deSdougm handle->scfhandle, 9601d1813a7Sdougm handle->tree, handle); 96157b448deSdougm } else { 96257b448deSdougm if (handle->doc != NULL) 96357b448deSdougm xmlFreeDoc(handle->doc); 96457b448deSdougm if (handle->tree != NULL) 96557b448deSdougm xmlFreeNode(handle->tree); 96657b448deSdougm err = SA_NO_MEMORY; 96757b448deSdougm } 96857b448deSdougm 96957b448deSdougm saunblocksigs(&old); 97057b448deSdougm 97157b448deSdougm if (err != SA_OK) { 97257b448deSdougm /* 97357b448deSdougm * If we couldn't add the tree handle 97457b448deSdougm * to the list, then things are going 97557b448deSdougm * to fail badly. Might as well undo 97657b448deSdougm * everything now and fail the 97757b448deSdougm * sa_init(). 97857b448deSdougm */ 97957b448deSdougm sa_fini(handle); 980148c5f43SAlan Wright if (updatelegacy == B_TRUE) { 981148c5f43SAlan Wright (void) mutex_unlock( 982148c5f43SAlan Wright &sa_dfstab_lock); 983148c5f43SAlan Wright (void) lockf(lockfd, 984148c5f43SAlan Wright F_ULOCK, 0); 985148c5f43SAlan Wright (void) close(lockfd); 986148c5f43SAlan Wright } 98757b448deSdougm return (NULL); 98857b448deSdougm } 9891d1813a7Sdougm 99057b448deSdougm if (tval == 0) { 99157b448deSdougm /* 99257b448deSdougm * first time so make sure 99357b448deSdougm * default is setup 99457b448deSdougm */ 99557b448deSdougm verifydefgroupopts(handle); 99657b448deSdougm } 997549ec3ffSdougm 998546405c3Sdougm if (updatelegacy == B_TRUE) { 999546405c3Sdougm sablocksigs(&old); 1000546405c3Sdougm getlegacyconfig((sa_handle_t)handle, 1001546405c3Sdougm SA_LEGACY_DFSTAB, &handle->tree); 1002546405c3Sdougm if (stat(SA_LEGACY_DFSTAB, &st) >= 0) 1003546405c3Sdougm set_legacy_timestamp( 1004546405c3Sdougm handle->tree, 1005546405c3Sdougm SA_LEGACY_DFSTAB, 1006546405c3Sdougm TSTAMP(st.st_ctim)); 1007546405c3Sdougm saunblocksigs(&old); 1008546405c3Sdougm /* 1009546405c3Sdougm * Safe to unlock now to allow 1010546405c3Sdougm * others to run 1011546405c3Sdougm */ 1012148c5f43SAlan Wright (void) mutex_unlock(&sa_dfstab_lock); 1013546405c3Sdougm (void) lockf(lockfd, F_ULOCK, 0); 1014546405c3Sdougm (void) close(lockfd); 1015546405c3Sdougm } 10165b6e0c46Sdougm /* Get sharetab timestamp */ 10175b6e0c46Sdougm sa_update_sharetab_ts((sa_handle_t)handle); 10185b6e0c46Sdougm 10195b6e0c46Sdougm /* Get lastupdate (transaction) timestamp */ 10205b6e0c46Sdougm prop = scf_simple_prop_get( 10215b6e0c46Sdougm handle->scfhandle->handle, 10225b6e0c46Sdougm (const char *)SA_SVC_FMRI_BASE ":default", 10235b6e0c46Sdougm "state", "lastupdate"); 10245b6e0c46Sdougm if (prop != NULL) { 10255b6e0c46Sdougm char *str; 10265b6e0c46Sdougm str = 10275b6e0c46Sdougm scf_simple_prop_next_astring(prop); 10285b6e0c46Sdougm if (str != NULL) 10295b6e0c46Sdougm handle->tstrans = 10305b6e0c46Sdougm strtoull(str, NULL, 0); 10315b6e0c46Sdougm else 10325b6e0c46Sdougm handle->tstrans = 0; 10335b6e0c46Sdougm scf_simple_prop_free(prop); 10345b6e0c46Sdougm } 1035*8a981c33SDaniel Hoffman /* 1036*8a981c33SDaniel Hoffman * In this conditional the library reads from 1037*8a981c33SDaniel Hoffman * zfs and /etc/dfs/sharetab to find datasets 1038*8a981c33SDaniel Hoffman * that must be shared. The result is a tree of 1039*8a981c33SDaniel Hoffman * groups that are stored in the handle for 1040*8a981c33SDaniel Hoffman * libshare to utilize later when asked to share 1041*8a981c33SDaniel Hoffman * or unshare datasets. 1042*8a981c33SDaniel Hoffman */ 1043*8a981c33SDaniel Hoffman if (init_service & 1044*8a981c33SDaniel Hoffman SA_INIT_SHARE_API_SELECTIVE) { 1045*8a981c33SDaniel Hoffman char **paths; 1046*8a981c33SDaniel Hoffman size_t paths_len, i; 1047*8a981c33SDaniel Hoffman 1048*8a981c33SDaniel Hoffman legacy |= sa_get_one_zfs_share(handle, 1049*8a981c33SDaniel Hoffman "zfs", 1050*8a981c33SDaniel Hoffman (sa_init_selective_arg_t *)arg, 1051*8a981c33SDaniel Hoffman &paths, &paths_len); 1052*8a981c33SDaniel Hoffman legacy |= get_one_transient(handle, 1053*8a981c33SDaniel Hoffman &handle->tree, paths, paths_len); 1054*8a981c33SDaniel Hoffman for (i = 0; i < paths_len; ++i) { 1055*8a981c33SDaniel Hoffman free(paths[i]); 1056*8a981c33SDaniel Hoffman } 1057*8a981c33SDaniel Hoffman free(paths); 1058*8a981c33SDaniel Hoffman } else if (init_service & 1059*8a981c33SDaniel Hoffman SA_INIT_ONE_SHARE_FROM_NAME) { 1060*8a981c33SDaniel Hoffman char path[ZFS_MAXPROPLEN]; 1061*8a981c33SDaniel Hoffman char *ptr = path; 1062*8a981c33SDaniel Hoffman char **ptr_to_path = &ptr; 1063*8a981c33SDaniel Hoffman 1064*8a981c33SDaniel Hoffman legacy |= 1065*8a981c33SDaniel Hoffman sa_get_zfs_share_for_name(handle, 1066*8a981c33SDaniel Hoffman "zfs", (char *)arg, path); 1067*8a981c33SDaniel Hoffman legacy |= get_one_transient(handle, 1068*8a981c33SDaniel Hoffman &handle->tree, ptr_to_path, 1); 1069*8a981c33SDaniel Hoffman } else if (init_service & 1070*8a981c33SDaniel Hoffman SA_INIT_ONE_SHARE_FROM_HANDLE) { 1071*8a981c33SDaniel Hoffman char path[ZFS_MAXPROPLEN]; 1072*8a981c33SDaniel Hoffman char *ptr = path; 1073*8a981c33SDaniel Hoffman char **ptr_to_path = &ptr; 1074*8a981c33SDaniel Hoffman 1075*8a981c33SDaniel Hoffman legacy |= 1076*8a981c33SDaniel Hoffman sa_get_zfs_share_for_name(handle, 1077*8a981c33SDaniel Hoffman "zfs", 1078*8a981c33SDaniel Hoffman zfs_get_name( 1079*8a981c33SDaniel Hoffman (zfs_handle_t *)arg), 1080*8a981c33SDaniel Hoffman path); 1081*8a981c33SDaniel Hoffman legacy |= get_one_transient(handle, 1082*8a981c33SDaniel Hoffman &handle->tree, ptr_to_path, 1); 1083*8a981c33SDaniel Hoffman } else { 1084*8a981c33SDaniel Hoffman legacy |= sa_get_zfs_shares(handle, 1085*8a981c33SDaniel Hoffman "zfs"); 1086*8a981c33SDaniel Hoffman legacy |= gettransients(handle, 1087*8a981c33SDaniel Hoffman &handle->tree); 1088*8a981c33SDaniel Hoffman } 10896185db85Sdougm } 10906185db85Sdougm } 10916185db85Sdougm } 1092549ec3ffSdougm return ((sa_handle_t)handle); 10936185db85Sdougm } 10946185db85Sdougm 1095*8a981c33SDaniel Hoffman /* 1096*8a981c33SDaniel Hoffman * sa_init exists as a legacy interface, new consumers should use sa_init_arg. 1097*8a981c33SDaniel Hoffman */ 1098*8a981c33SDaniel Hoffman sa_handle_t 1099*8a981c33SDaniel Hoffman sa_init(int init_service) 1100*8a981c33SDaniel Hoffman { 1101*8a981c33SDaniel Hoffman return (sa_init_impl(init_service, NULL)); 1102*8a981c33SDaniel Hoffman } 1103*8a981c33SDaniel Hoffman 1104*8a981c33SDaniel Hoffman /* 1105*8a981c33SDaniel Hoffman * See libshare.h "API Initialization" section for valid values of init_service 1106*8a981c33SDaniel Hoffman * as well as the appropriate argument type for a given init_service. 1107*8a981c33SDaniel Hoffman */ 1108*8a981c33SDaniel Hoffman sa_handle_t 1109*8a981c33SDaniel Hoffman sa_init_arg(int init_service, void *arg) 1110*8a981c33SDaniel Hoffman { 1111*8a981c33SDaniel Hoffman return (sa_init_impl(init_service, arg)); 1112*8a981c33SDaniel Hoffman } 1113*8a981c33SDaniel Hoffman 11146185db85Sdougm /* 1115549ec3ffSdougm * sa_fini(handle) 11166185db85Sdougm * Uninitialize the API structures including the configuration 11171cea05afSdougm * data structures and ZFS related data. 11186185db85Sdougm */ 11196185db85Sdougm 11206185db85Sdougm void 1121549ec3ffSdougm sa_fini(sa_handle_t handle) 11226185db85Sdougm { 1123549ec3ffSdougm sa_handle_impl_t impl_handle = (sa_handle_impl_t)handle; 1124549ec3ffSdougm 1125549ec3ffSdougm if (impl_handle != NULL) { 1126549ec3ffSdougm /* 1127549ec3ffSdougm * Free the config trees and any other data structures 1128549ec3ffSdougm * used in the handle. 1129549ec3ffSdougm */ 1130549ec3ffSdougm if (impl_handle->doc != NULL) 1131549ec3ffSdougm xmlFreeDoc(impl_handle->doc); 1132549ec3ffSdougm 1133549ec3ffSdougm /* Remove and free the entry in the global list. */ 1134549ec3ffSdougm remove_handle_for_root(impl_handle->tree); 1135549ec3ffSdougm 1136549ec3ffSdougm /* 1137549ec3ffSdougm * If this was the last handle to release, unload the 1138c5f58477Sdougm * plugins that were loaded. Use a mutex in case 1139c5f58477Sdougm * another thread is reinitializing. 1140549ec3ffSdougm */ 1141c5f58477Sdougm (void) mutex_lock(&sa_global_lock); 1142549ec3ffSdougm if (sa_global_handles == NULL) 114357b448deSdougm (void) proto_plugin_fini(); 1144c5f58477Sdougm (void) mutex_unlock(&sa_global_lock); 1145549ec3ffSdougm 11460fd77660Sgwr sa_scf_fini(impl_handle->scfhandle); 11470fd77660Sgwr sa_zfs_fini(impl_handle); 11480fd77660Sgwr 11490fd77660Sgwr /* Make sure we free the handle */ 11500fd77660Sgwr free(impl_handle); 11510fd77660Sgwr 11526185db85Sdougm } 11536185db85Sdougm } 11546185db85Sdougm 11556185db85Sdougm /* 11566185db85Sdougm * sa_get_protocols(char **protocol) 11576185db85Sdougm * Get array of protocols that are supported 11586185db85Sdougm * Returns pointer to an allocated and NULL terminated 11596185db85Sdougm * array of strings. Caller must free. 11606185db85Sdougm * This really should be determined dynamically. 11616185db85Sdougm * If there aren't any defined, return -1. 11626185db85Sdougm * Use free() to return memory. 11636185db85Sdougm */ 11646185db85Sdougm 11656185db85Sdougm int 11666185db85Sdougm sa_get_protocols(char ***protocols) 11676185db85Sdougm { 11686185db85Sdougm int numproto = -1; 11696185db85Sdougm 11706185db85Sdougm if (protocols != NULL) { 117157b448deSdougm struct sa_proto_plugin *plug; 117257b448deSdougm for (numproto = 0, plug = sap_proto_list; plug != NULL; 11736185db85Sdougm plug = plug->plugin_next) { 117457b448deSdougm numproto++; 117557b448deSdougm } 117657b448deSdougm 117757b448deSdougm *protocols = calloc(numproto + 1, sizeof (char *)); 117857b448deSdougm if (*protocols != NULL) { 117957b448deSdougm int ret = 0; 118057b448deSdougm for (plug = sap_proto_list; plug != NULL; 118157b448deSdougm plug = plug->plugin_next) { 118257b448deSdougm /* faking for now */ 118357b448deSdougm (*protocols)[ret++] = 118457b448deSdougm plug->plugin_ops->sa_protocol; 118557b448deSdougm } 118657b448deSdougm } else { 118757b448deSdougm numproto = -1; 11886185db85Sdougm } 11896185db85Sdougm } 11906185db85Sdougm return (numproto); 11916185db85Sdougm } 11926185db85Sdougm 11936185db85Sdougm /* 11946185db85Sdougm * find_group_by_name(node, group) 11956185db85Sdougm * 11966185db85Sdougm * search the XML document subtree specified by node to find the group 11976185db85Sdougm * specified by group. Searching subtree allows subgroups to be 11986185db85Sdougm * searched for. 11996185db85Sdougm */ 12006185db85Sdougm 12016185db85Sdougm static xmlNodePtr 12026185db85Sdougm find_group_by_name(xmlNodePtr node, xmlChar *group) 12036185db85Sdougm { 12046185db85Sdougm xmlChar *name = NULL; 12056185db85Sdougm 12066185db85Sdougm for (node = node->xmlChildrenNode; node != NULL; 12076185db85Sdougm node = node->next) { 120857b448deSdougm if (xmlStrcmp(node->name, (xmlChar *)"group") == 0) { 120957b448deSdougm /* if no groupname, return the first found */ 121057b448deSdougm if (group == NULL) 121157b448deSdougm break; 121257b448deSdougm name = xmlGetProp(node, (xmlChar *)"name"); 121357b448deSdougm if (name != NULL && xmlStrcmp(name, group) == 0) 121457b448deSdougm break; 121557b448deSdougm if (name != NULL) { 121657b448deSdougm xmlFree(name); 121757b448deSdougm name = NULL; 121857b448deSdougm } 12196185db85Sdougm } 12206185db85Sdougm } 12216185db85Sdougm if (name != NULL) 122257b448deSdougm xmlFree(name); 12236185db85Sdougm return (node); 12246185db85Sdougm } 12256185db85Sdougm 12266185db85Sdougm /* 12276185db85Sdougm * sa_get_group(groupname) 12286185db85Sdougm * Return the "group" specified. If groupname is NULL, 12296185db85Sdougm * return the first group of the list of groups. 12306185db85Sdougm */ 12316185db85Sdougm sa_group_t 1232549ec3ffSdougm sa_get_group(sa_handle_t handle, char *groupname) 12336185db85Sdougm { 12346185db85Sdougm xmlNodePtr node = NULL; 12356185db85Sdougm char *subgroup = NULL; 12366185db85Sdougm char *group = NULL; 1237549ec3ffSdougm sa_handle_impl_t impl_handle = (sa_handle_impl_t)handle; 12386185db85Sdougm 1239549ec3ffSdougm if (impl_handle != NULL && impl_handle->tree != NULL) { 124057b448deSdougm if (groupname != NULL) { 124157b448deSdougm group = strdup(groupname); 1242a3351425Sdougm if (group != NULL) { 1243a3351425Sdougm subgroup = strchr(group, '/'); 1244a3351425Sdougm if (subgroup != NULL) 1245a3351425Sdougm *subgroup++ = '\0'; 1246a3351425Sdougm } 124757b448deSdougm } 1248a3351425Sdougm /* 1249a3351425Sdougm * We want to find the, possibly, named group. If 1250a3351425Sdougm * group is not NULL, then lookup the name. If it is 1251a3351425Sdougm * NULL, we only do the find if groupname is also 1252a3351425Sdougm * NULL. This allows lookup of the "first" group in 1253a3351425Sdougm * the internal list. 1254a3351425Sdougm */ 1255a3351425Sdougm if (group != NULL || groupname == NULL) 1256a3351425Sdougm node = find_group_by_name(impl_handle->tree, 1257a3351425Sdougm (xmlChar *)group); 1258a3351425Sdougm 125957b448deSdougm /* if a subgroup, find it before returning */ 126057b448deSdougm if (subgroup != NULL && node != NULL) 126157b448deSdougm node = find_group_by_name(node, (xmlChar *)subgroup); 12626185db85Sdougm } 12636185db85Sdougm if (node != NULL && (char *)group != NULL) 126457b448deSdougm (void) sa_get_instance(impl_handle->scfhandle, (char *)group); 12656185db85Sdougm if (group != NULL) 126657b448deSdougm free(group); 12676185db85Sdougm return ((sa_group_t)(node)); 12686185db85Sdougm } 12696185db85Sdougm 12706185db85Sdougm /* 12716185db85Sdougm * sa_get_next_group(group) 12726185db85Sdougm * Return the "next" group after the specified group from 12736185db85Sdougm * the internal group list. NULL if there are no more. 12746185db85Sdougm */ 12756185db85Sdougm sa_group_t 12766185db85Sdougm sa_get_next_group(sa_group_t group) 12776185db85Sdougm { 12786185db85Sdougm xmlNodePtr ngroup = NULL; 12796185db85Sdougm if (group != NULL) { 128057b448deSdougm for (ngroup = ((xmlNodePtr)group)->next; ngroup != NULL; 12816185db85Sdougm ngroup = ngroup->next) { 128257b448deSdougm if (xmlStrcmp(ngroup->name, (xmlChar *)"group") == 0) 128357b448deSdougm break; 128457b448deSdougm } 12856185db85Sdougm } 12866185db85Sdougm return ((sa_group_t)ngroup); 12876185db85Sdougm } 12886185db85Sdougm 12896185db85Sdougm /* 12906185db85Sdougm * sa_get_share(group, sharepath) 12916185db85Sdougm * Return the share object for the share specified. The share 12926185db85Sdougm * must be in the specified group. Return NULL if not found. 12936185db85Sdougm */ 12946185db85Sdougm sa_share_t 12956185db85Sdougm sa_get_share(sa_group_t group, char *sharepath) 12966185db85Sdougm { 12976185db85Sdougm xmlNodePtr node = NULL; 12986185db85Sdougm xmlChar *path; 12996185db85Sdougm 13006185db85Sdougm /* 13016185db85Sdougm * For future scalability, this should end up building a cache 13026185db85Sdougm * since it will get called regularly by the mountd and info 13036185db85Sdougm * services. 13046185db85Sdougm */ 13056185db85Sdougm if (group != NULL) { 130657b448deSdougm for (node = ((xmlNodePtr)group)->children; node != NULL; 13076185db85Sdougm node = node->next) { 130857b448deSdougm if (xmlStrcmp(node->name, (xmlChar *)"share") == 0) { 130957b448deSdougm if (sharepath == NULL) { 131057b448deSdougm break; 131157b448deSdougm } else { 131257b448deSdougm /* is it the correct share? */ 131357b448deSdougm path = xmlGetProp(node, 131457b448deSdougm (xmlChar *)"path"); 131557b448deSdougm if (path != NULL && 131657b448deSdougm xmlStrcmp(path, 131757b448deSdougm (xmlChar *)sharepath) == 0) { 131857b448deSdougm xmlFree(path); 131957b448deSdougm break; 132057b448deSdougm } 132157b448deSdougm xmlFree(path); 132257b448deSdougm } 13236185db85Sdougm } 13246185db85Sdougm } 13256185db85Sdougm } 13266185db85Sdougm return ((sa_share_t)node); 13276185db85Sdougm } 13286185db85Sdougm 13296185db85Sdougm /* 13306185db85Sdougm * sa_get_next_share(share) 13316185db85Sdougm * Return the next share following the specified share 13326185db85Sdougm * from the internal list of shares. Returns NULL if there 13336185db85Sdougm * are no more shares. The list is relative to the same 13346185db85Sdougm * group. 13356185db85Sdougm */ 13366185db85Sdougm sa_share_t 13376185db85Sdougm sa_get_next_share(sa_share_t share) 13386185db85Sdougm { 13396185db85Sdougm xmlNodePtr node = NULL; 13406185db85Sdougm 13416185db85Sdougm if (share != NULL) { 134257b448deSdougm for (node = ((xmlNodePtr)share)->next; node != NULL; 13436185db85Sdougm node = node->next) { 134457b448deSdougm if (xmlStrcmp(node->name, (xmlChar *)"share") == 0) { 134557b448deSdougm break; 134657b448deSdougm } 13476185db85Sdougm } 13486185db85Sdougm } 13496185db85Sdougm return ((sa_share_t)node); 13506185db85Sdougm } 13516185db85Sdougm 13526185db85Sdougm /* 13536185db85Sdougm * _sa_get_child_node(node, type) 13546185db85Sdougm * 13556185db85Sdougm * find the child node of the specified node that has "type". This is 13566185db85Sdougm * used to implement several internal functions. 13576185db85Sdougm */ 13586185db85Sdougm 13596185db85Sdougm static xmlNodePtr 13606185db85Sdougm _sa_get_child_node(xmlNodePtr node, xmlChar *type) 13616185db85Sdougm { 13626185db85Sdougm xmlNodePtr child; 13636185db85Sdougm for (child = node->xmlChildrenNode; child != NULL; 13646185db85Sdougm child = child->next) 136557b448deSdougm if (xmlStrcmp(child->name, type) == 0) 136657b448deSdougm return (child); 13676185db85Sdougm return ((xmlNodePtr)NULL); 13686185db85Sdougm } 13696185db85Sdougm 13706185db85Sdougm /* 13716185db85Sdougm * find_share(group, path) 13726185db85Sdougm * 13736185db85Sdougm * Search all the shares in the specified group for one that has the 13746185db85Sdougm * specified path. 13756185db85Sdougm */ 13766185db85Sdougm 13776185db85Sdougm static sa_share_t 13786185db85Sdougm find_share(sa_group_t group, char *sharepath) 13796185db85Sdougm { 13806185db85Sdougm sa_share_t share; 13816185db85Sdougm char *path; 13826185db85Sdougm 13836185db85Sdougm for (share = sa_get_share(group, NULL); share != NULL; 13846185db85Sdougm share = sa_get_next_share(share)) { 138557b448deSdougm path = sa_get_share_attr(share, "path"); 138657b448deSdougm if (path != NULL && strcmp(path, sharepath) == 0) { 138757b448deSdougm sa_free_attr_string(path); 138857b448deSdougm break; 138957b448deSdougm } 139057b448deSdougm if (path != NULL) 139157b448deSdougm sa_free_attr_string(path); 13926185db85Sdougm } 13936185db85Sdougm return (share); 13946185db85Sdougm } 13956185db85Sdougm 13966185db85Sdougm /* 13976185db85Sdougm * sa_get_sub_group(group) 13986185db85Sdougm * 13996185db85Sdougm * Get the first sub-group of group. The sa_get_next_group() function 14006185db85Sdougm * can be used to get the rest. This is currently only used for ZFS 14016185db85Sdougm * sub-groups but could be used to implement a more general mechanism. 14026185db85Sdougm */ 14036185db85Sdougm 14046185db85Sdougm sa_group_t 14056185db85Sdougm sa_get_sub_group(sa_group_t group) 14066185db85Sdougm { 14076185db85Sdougm return ((sa_group_t)_sa_get_child_node((xmlNodePtr)group, 140857b448deSdougm (xmlChar *)"group")); 14096185db85Sdougm } 14106185db85Sdougm 14116185db85Sdougm /* 14126185db85Sdougm * sa_find_share(sharepath) 14136185db85Sdougm * Finds a share regardless of group. In the future, this 14146185db85Sdougm * function should utilize a cache and hash table of some kind. 14156185db85Sdougm * The current assumption is that a path will only be shared 14166185db85Sdougm * once. In the future, this may change as implementation of 14176185db85Sdougm * resource names comes into being. 14186185db85Sdougm */ 14196185db85Sdougm sa_share_t 1420549ec3ffSdougm sa_find_share(sa_handle_t handle, char *sharepath) 14216185db85Sdougm { 14226185db85Sdougm sa_group_t group; 14236185db85Sdougm sa_group_t zgroup; 14246185db85Sdougm sa_share_t share = NULL; 14256185db85Sdougm int done = 0; 14266185db85Sdougm 1427549ec3ffSdougm for (group = sa_get_group(handle, NULL); group != NULL && !done; 142857b448deSdougm group = sa_get_next_group(group)) { 142957b448deSdougm if (is_zfs_group(group)) { 143057b448deSdougm for (zgroup = 143157b448deSdougm (sa_group_t)_sa_get_child_node((xmlNodePtr)group, 143257b448deSdougm (xmlChar *)"group"); 143357b448deSdougm zgroup != NULL; 143457b448deSdougm zgroup = sa_get_next_group(zgroup)) { 143557b448deSdougm share = find_share(zgroup, sharepath); 143657b448deSdougm if (share != NULL) 143757b448deSdougm break; 143857b448deSdougm } 143957b448deSdougm } else { 144057b448deSdougm share = find_share(group, sharepath); 14416185db85Sdougm } 144257b448deSdougm if (share != NULL) 144357b448deSdougm break; 14446185db85Sdougm } 14456185db85Sdougm return (share); 14466185db85Sdougm } 14476185db85Sdougm 14486185db85Sdougm /* 1449f345c0beSdougm * sa_check_path(group, path, strictness) 14506185db85Sdougm * 1451da6c28aaSamw * Check that path is a valid path relative to the group. Currently, 14526185db85Sdougm * we are ignoring the group and checking only the NFS rules. Later, 14536185db85Sdougm * we may want to use the group to then check against the protocols 1454f345c0beSdougm * enabled on the group. The strictness values mean: 1455f345c0beSdougm * SA_CHECK_NORMAL == only check newpath against shares that are active 1456f345c0beSdougm * SA_CHECK_STRICT == check newpath against both active shares and those 1457f345c0beSdougm * stored in the repository 14586185db85Sdougm */ 14596185db85Sdougm 14606185db85Sdougm int 1461f345c0beSdougm sa_check_path(sa_group_t group, char *path, int strictness) 14626185db85Sdougm { 1463549ec3ffSdougm sa_handle_t handle; 1464549ec3ffSdougm 1465549ec3ffSdougm handle = sa_find_group_handle(group); 14669fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States if (handle == NULL) 14679fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States return (SA_BAD_PATH); 14689fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 1469549ec3ffSdougm return (validpath(handle, path, strictness)); 14706185db85Sdougm } 14716185db85Sdougm 14726185db85Sdougm /* 1473da6c28aaSamw * mark_excluded_protos(group, share, flags) 1474da6c28aaSamw * 1475da6c28aaSamw * Walk through all the protocols enabled for the group and check to 1476da6c28aaSamw * see if the share has any of them should be in the exclude list 1477da6c28aaSamw * based on the featureset of the protocol. If there are any, add the 1478da6c28aaSamw * "exclude" property to the share. 1479da6c28aaSamw */ 1480da6c28aaSamw static void 1481da6c28aaSamw mark_excluded_protos(sa_group_t group, xmlNodePtr share, uint64_t flags) 1482da6c28aaSamw { 1483da6c28aaSamw sa_optionset_t optionset; 1484da6c28aaSamw char exclude_list[SA_STRSIZE]; 1485da6c28aaSamw char *sep = ""; 1486da6c28aaSamw 1487da6c28aaSamw exclude_list[0] = '\0'; 1488da6c28aaSamw for (optionset = sa_get_optionset(group, NULL); 1489da6c28aaSamw optionset != NULL; 1490da6c28aaSamw optionset = sa_get_next_optionset(optionset)) { 1491da6c28aaSamw char *value; 1492da6c28aaSamw uint64_t features; 1493da6c28aaSamw value = sa_get_optionset_attr(optionset, "type"); 1494da6c28aaSamw if (value == NULL) 1495da6c28aaSamw continue; 1496da6c28aaSamw features = sa_proto_get_featureset(value); 1497da6c28aaSamw if (!(features & flags)) { 1498da6c28aaSamw (void) strlcat(exclude_list, sep, 1499da6c28aaSamw sizeof (exclude_list)); 1500da6c28aaSamw (void) strlcat(exclude_list, value, 1501da6c28aaSamw sizeof (exclude_list)); 1502da6c28aaSamw sep = ","; 1503da6c28aaSamw } 1504fe1c642dSBill Krier sa_free_attr_string(value); 1505da6c28aaSamw } 1506da6c28aaSamw if (exclude_list[0] != '\0') 15074bff34e3Sthurlow (void) xmlSetProp(share, (xmlChar *)"exclude", 1508da6c28aaSamw (xmlChar *)exclude_list); 1509da6c28aaSamw } 1510da6c28aaSamw 1511da6c28aaSamw /* 1512da6c28aaSamw * get_all_features(group) 1513da6c28aaSamw * 1514da6c28aaSamw * Walk through all the protocols on the group and collect all 1515da6c28aaSamw * possible enabled features. This is the OR of all the featuresets. 1516da6c28aaSamw */ 1517da6c28aaSamw static uint64_t 1518da6c28aaSamw get_all_features(sa_group_t group) 1519da6c28aaSamw { 1520da6c28aaSamw sa_optionset_t optionset; 1521da6c28aaSamw uint64_t features = 0; 1522da6c28aaSamw 1523da6c28aaSamw for (optionset = sa_get_optionset(group, NULL); 1524da6c28aaSamw optionset != NULL; 1525da6c28aaSamw optionset = sa_get_next_optionset(optionset)) { 1526da6c28aaSamw char *value; 1527da6c28aaSamw value = sa_get_optionset_attr(optionset, "type"); 1528da6c28aaSamw if (value == NULL) 1529da6c28aaSamw continue; 1530da6c28aaSamw features |= sa_proto_get_featureset(value); 1531da6c28aaSamw sa_free_attr_string(value); 1532da6c28aaSamw } 1533da6c28aaSamw return (features); 1534da6c28aaSamw } 1535da6c28aaSamw 1536da6c28aaSamw 1537da6c28aaSamw /* 1538da6c28aaSamw * _sa_add_share(group, sharepath, persist, *error, flags) 15396185db85Sdougm * 1540da6c28aaSamw * Common code for all types of add_share. sa_add_share() is the 15416185db85Sdougm * public API, we also need to be able to do this when parsing legacy 15426185db85Sdougm * files and construction of the internal configuration while 1543da6c28aaSamw * extracting config info from SMF. "flags" indicates if some 1544da6c28aaSamw * protocols need relaxed rules while other don't. These values are 1545da6c28aaSamw * the featureset values defined in libshare.h. 15466185db85Sdougm */ 15476185db85Sdougm 15486185db85Sdougm sa_share_t 1549da6c28aaSamw _sa_add_share(sa_group_t group, char *sharepath, int persist, int *error, 1550da6c28aaSamw uint64_t flags) 15516185db85Sdougm { 15526185db85Sdougm xmlNodePtr node = NULL; 15536185db85Sdougm int err; 15546185db85Sdougm 15556185db85Sdougm err = SA_OK; /* assume success */ 15566185db85Sdougm 155757b448deSdougm node = xmlNewChild((xmlNodePtr)group, NULL, (xmlChar *)"share", NULL); 1558da6c28aaSamw if (node == NULL) { 1559da6c28aaSamw if (error != NULL) 1560da6c28aaSamw *error = SA_NO_MEMORY; 1561da6c28aaSamw return (node); 1562da6c28aaSamw } 1563da6c28aaSamw 15644bff34e3Sthurlow (void) xmlSetProp(node, (xmlChar *)"path", (xmlChar *)sharepath); 15654bff34e3Sthurlow (void) xmlSetProp(node, (xmlChar *)"type", 1566da6c28aaSamw persist ? (xmlChar *)"persist" : (xmlChar *)"transient"); 1567da6c28aaSamw if (flags != 0) 1568da6c28aaSamw mark_excluded_protos(group, node, flags); 1569da6c28aaSamw if (persist != SA_SHARE_TRANSIENT) { 1570da6c28aaSamw /* 1571da6c28aaSamw * persistent shares come in two flavors: SMF and 1572da6c28aaSamw * ZFS. Sort this one out based on target group and 1573da6c28aaSamw * path type. Both NFS and SMB are supported. First, 1574da6c28aaSamw * check to see if the protocol is enabled on the 1575da6c28aaSamw * subgroup and then setup the share appropriately. 1576da6c28aaSamw */ 1577da6c28aaSamw if (sa_group_is_zfs(group) && 1578da6c28aaSamw sa_path_is_zfs(sharepath)) { 1579da6c28aaSamw if (sa_get_optionset(group, "nfs") != NULL) 158057b448deSdougm err = sa_zfs_set_sharenfs(group, sharepath, 1); 1581da6c28aaSamw else if (sa_get_optionset(group, "smb") != NULL) 1582da6c28aaSamw err = sa_zfs_set_sharesmb(group, sharepath, 1); 1583da6c28aaSamw } else { 1584da6c28aaSamw sa_handle_impl_t impl_handle; 1585da6c28aaSamw impl_handle = 1586da6c28aaSamw (sa_handle_impl_t)sa_find_group_handle(group); 1587da6c28aaSamw if (impl_handle != NULL) { 1588da6c28aaSamw err = sa_commit_share(impl_handle->scfhandle, 1589da6c28aaSamw group, (sa_share_t)node); 159057b448deSdougm } else { 1591da6c28aaSamw err = SA_SYSTEM_ERR; 159257b448deSdougm } 159357b448deSdougm } 15946185db85Sdougm } 1595da6c28aaSamw if (err == SA_NO_PERMISSION && persist & SA_SHARE_PARSER) 1596da6c28aaSamw /* called by the dfstab parser so could be a show */ 1597da6c28aaSamw err = SA_OK; 1598da6c28aaSamw 1599da6c28aaSamw if (err != SA_OK) { 1600da6c28aaSamw /* 1601da6c28aaSamw * we couldn't commit to the repository so undo 1602da6c28aaSamw * our internal state to reflect reality. 1603da6c28aaSamw */ 1604da6c28aaSamw xmlUnlinkNode(node); 1605da6c28aaSamw xmlFreeNode(node); 1606da6c28aaSamw node = NULL; 1607da6c28aaSamw } 1608da6c28aaSamw 16096185db85Sdougm if (error != NULL) 161057b448deSdougm *error = err; 1611da6c28aaSamw 16126185db85Sdougm return (node); 16136185db85Sdougm } 16146185db85Sdougm 16156185db85Sdougm /* 16166185db85Sdougm * sa_add_share(group, sharepath, persist, *error) 16176185db85Sdougm * 16186185db85Sdougm * Add a new share object to the specified group. The share will 16196185db85Sdougm * have the specified sharepath and will only be constructed if 16206185db85Sdougm * it is a valid path to be shared. NULL is returned on error 16216185db85Sdougm * and a detailed error value will be returned via the error 16226185db85Sdougm * pointer. 16236185db85Sdougm */ 16246185db85Sdougm sa_share_t 16256185db85Sdougm sa_add_share(sa_group_t group, char *sharepath, int persist, int *error) 16266185db85Sdougm { 16276185db85Sdougm xmlNodePtr node = NULL; 1628f345c0beSdougm int strictness = SA_CHECK_NORMAL; 1629549ec3ffSdougm sa_handle_t handle; 1630da6c28aaSamw uint64_t special = 0; 1631da6c28aaSamw uint64_t features; 1632f345c0beSdougm 1633f345c0beSdougm /* 1634f345c0beSdougm * If the share is to be permanent, use strict checking so a 1635f345c0beSdougm * bad config doesn't get created. Transient shares only need 1636f345c0beSdougm * to check against the currently active 1637f345c0beSdougm * shares. SA_SHARE_PARSER is a modifier used internally to 1638f345c0beSdougm * indicate that we are being called by the dfstab parser and 1639f345c0beSdougm * that we need strict checking in all cases. Normally persist 1640f345c0beSdougm * is in integer value but SA_SHARE_PARSER may be or'd into 1641f345c0beSdougm * it as an override. 1642f345c0beSdougm */ 1643f345c0beSdougm if (persist & SA_SHARE_PARSER || persist == SA_SHARE_PERMANENT) 164457b448deSdougm strictness = SA_CHECK_STRICT; 16456185db85Sdougm 1646549ec3ffSdougm handle = sa_find_group_handle(group); 1647549ec3ffSdougm 1648da6c28aaSamw /* 1649da6c28aaSamw * need to determine if the share is valid. The rules are: 1650da6c28aaSamw * - The path must not already exist 1651da6c28aaSamw * - The path must not be a subdir or parent dir of an 1652da6c28aaSamw * existing path unless at least one protocol allows it. 1653da6c28aaSamw * The sub/parent check is done in sa_check_path(). 1654da6c28aaSamw */ 1655da6c28aaSamw 1656da6c28aaSamw if (sa_find_share(handle, sharepath) == NULL) { 1657da6c28aaSamw *error = sa_check_path(group, sharepath, strictness); 1658da6c28aaSamw features = get_all_features(group); 1659da6c28aaSamw switch (*error) { 1660da6c28aaSamw case SA_PATH_IS_SUBDIR: 1661da6c28aaSamw if (features & SA_FEATURE_ALLOWSUBDIRS) 1662da6c28aaSamw special |= SA_FEATURE_ALLOWSUBDIRS; 1663da6c28aaSamw break; 1664da6c28aaSamw case SA_PATH_IS_PARENTDIR: 1665da6c28aaSamw if (features & SA_FEATURE_ALLOWPARDIRS) 1666da6c28aaSamw special |= SA_FEATURE_ALLOWPARDIRS; 1667da6c28aaSamw break; 1668da6c28aaSamw } 1669da6c28aaSamw if (*error == SA_OK || special != SA_FEATURE_NONE) 1670da6c28aaSamw node = _sa_add_share(group, sharepath, persist, 1671da6c28aaSamw error, special); 1672da6c28aaSamw } else { 167357b448deSdougm *error = SA_DUPLICATE_NAME; 1674da6c28aaSamw } 16756185db85Sdougm 16766185db85Sdougm return ((sa_share_t)node); 16776185db85Sdougm } 16786185db85Sdougm 16796185db85Sdougm /* 16806185db85Sdougm * sa_enable_share(share, protocol) 16816185db85Sdougm * Enable the specified share to the specified protocol. 16826185db85Sdougm * If protocol is NULL, then all protocols. 16836185db85Sdougm */ 16846185db85Sdougm int 16856185db85Sdougm sa_enable_share(sa_share_t share, char *protocol) 16866185db85Sdougm { 16876185db85Sdougm char *sharepath; 16886185db85Sdougm struct stat st; 1689da6c28aaSamw int err = SA_OK; 1690da6c28aaSamw int ret; 16916185db85Sdougm 16926185db85Sdougm sharepath = sa_get_share_attr(share, "path"); 1693da6c28aaSamw if (sharepath == NULL) 1694da6c28aaSamw return (SA_NO_MEMORY); 16956185db85Sdougm if (stat(sharepath, &st) < 0) { 169657b448deSdougm err = SA_NO_SUCH_PATH; 16976185db85Sdougm } else { 169857b448deSdougm /* tell the server about the share */ 169957b448deSdougm if (protocol != NULL) { 1700da6c28aaSamw if (excluded_protocol(share, protocol)) 1701da6c28aaSamw goto done; 1702da6c28aaSamw 170357b448deSdougm /* lookup protocol specific handler */ 170457b448deSdougm err = sa_proto_share(protocol, share); 170557b448deSdougm if (err == SA_OK) 1706da6c28aaSamw (void) sa_set_share_attr(share, 1707da6c28aaSamw "shared", "true"); 170857b448deSdougm } else { 1709da6c28aaSamw /* Tell all protocols about the share */ 1710da6c28aaSamw sa_group_t group; 1711da6c28aaSamw sa_optionset_t optionset; 1712da6c28aaSamw 1713da6c28aaSamw group = sa_get_parent_group(share); 1714da6c28aaSamw 1715da6c28aaSamw for (optionset = sa_get_optionset(group, NULL); 1716da6c28aaSamw optionset != NULL; 1717da6c28aaSamw optionset = sa_get_next_optionset(optionset)) { 1718da6c28aaSamw char *proto; 1719da6c28aaSamw proto = sa_get_optionset_attr(optionset, 1720da6c28aaSamw "type"); 1721da6c28aaSamw if (proto != NULL) { 1722da6c28aaSamw if (!excluded_protocol(share, proto)) { 1723da6c28aaSamw ret = sa_proto_share(proto, 1724da6c28aaSamw share); 1725da6c28aaSamw if (ret != SA_OK) 1726da6c28aaSamw err = ret; 1727da6c28aaSamw } 1728da6c28aaSamw sa_free_attr_string(proto); 1729da6c28aaSamw } 1730da6c28aaSamw } 173157b448deSdougm (void) sa_set_share_attr(share, "shared", "true"); 173257b448deSdougm } 17336185db85Sdougm } 1734da6c28aaSamw done: 17356185db85Sdougm if (sharepath != NULL) 173657b448deSdougm sa_free_attr_string(sharepath); 17376185db85Sdougm return (err); 17386185db85Sdougm } 17396185db85Sdougm 17406185db85Sdougm /* 17416185db85Sdougm * sa_disable_share(share, protocol) 1742da6c28aaSamw * Disable the specified share to the specified protocol. If 1743da6c28aaSamw * protocol is NULL, then all protocols that are enabled for the 1744da6c28aaSamw * share should be disabled. 17456185db85Sdougm */ 17466185db85Sdougm int 17476185db85Sdougm sa_disable_share(sa_share_t share, char *protocol) 17486185db85Sdougm { 17496185db85Sdougm char *path; 1750da6c28aaSamw int err = SA_OK; 17516185db85Sdougm int ret = SA_OK; 17526185db85Sdougm 17536185db85Sdougm path = sa_get_share_attr(share, "path"); 17546185db85Sdougm 17556185db85Sdougm if (protocol != NULL) { 1756ecd6cf80Smarks ret = sa_proto_unshare(share, protocol, path); 17576185db85Sdougm } else { 175857b448deSdougm /* need to do all protocols */ 1759da6c28aaSamw sa_group_t group; 1760da6c28aaSamw sa_optionset_t optionset; 1761da6c28aaSamw 1762da6c28aaSamw group = sa_get_parent_group(share); 1763da6c28aaSamw 1764da6c28aaSamw /* Tell all protocols about the share */ 1765da6c28aaSamw for (optionset = sa_get_optionset(group, NULL); 1766da6c28aaSamw optionset != NULL; 1767da6c28aaSamw optionset = sa_get_next_optionset(optionset)) { 1768da6c28aaSamw char *proto; 1769da6c28aaSamw 1770da6c28aaSamw proto = sa_get_optionset_attr(optionset, "type"); 1771da6c28aaSamw if (proto != NULL) { 1772da6c28aaSamw err = sa_proto_unshare(share, proto, path); 1773da6c28aaSamw if (err != SA_OK) 1774da6c28aaSamw ret = err; 1775da6c28aaSamw sa_free_attr_string(proto); 1776da6c28aaSamw } 1777da6c28aaSamw } 17786185db85Sdougm } 17796185db85Sdougm if (ret == SA_OK) 17806185db85Sdougm (void) sa_set_share_attr(share, "shared", NULL); 17816185db85Sdougm if (path != NULL) 178257b448deSdougm sa_free_attr_string(path); 17836185db85Sdougm return (ret); 17846185db85Sdougm } 17856185db85Sdougm 17866185db85Sdougm /* 17876185db85Sdougm * sa_remove_share(share) 17886185db85Sdougm * 17896185db85Sdougm * remove the specified share from its containing group. 17906185db85Sdougm * Remove from the SMF or ZFS configuration space. 17916185db85Sdougm */ 17926185db85Sdougm 17936185db85Sdougm int 17946185db85Sdougm sa_remove_share(sa_share_t share) 17956185db85Sdougm { 17966185db85Sdougm sa_group_t group; 17976185db85Sdougm int ret = SA_OK; 17986185db85Sdougm char *type; 17996185db85Sdougm int transient = 0; 18006185db85Sdougm char *groupname; 18016185db85Sdougm char *zfs; 18026185db85Sdougm 18036185db85Sdougm type = sa_get_share_attr(share, "type"); 18046185db85Sdougm group = sa_get_parent_group(share); 18056185db85Sdougm zfs = sa_get_group_attr(group, "zfs"); 18066185db85Sdougm groupname = sa_get_group_attr(group, "name"); 18076185db85Sdougm if (type != NULL && strcmp(type, "persist") != 0) 180857b448deSdougm transient = 1; 18096185db85Sdougm if (type != NULL) 181057b448deSdougm sa_free_attr_string(type); 18116185db85Sdougm 18126185db85Sdougm /* remove the node from its group then free the memory */ 18136185db85Sdougm 18146185db85Sdougm /* 18156185db85Sdougm * need to test if "busy" 18166185db85Sdougm */ 18176185db85Sdougm /* only do SMF action if permanent */ 18186185db85Sdougm if (!transient || zfs != NULL) { 181957b448deSdougm /* remove from legacy dfstab as well as possible SMF */ 1820da6c28aaSamw ret = sa_delete_legacy(share, NULL); 182157b448deSdougm if (ret == SA_OK) { 182257b448deSdougm if (!sa_group_is_zfs(group)) { 182357b448deSdougm sa_handle_impl_t impl_handle; 182457b448deSdougm impl_handle = (sa_handle_impl_t) 182557b448deSdougm sa_find_group_handle(group); 182657b448deSdougm if (impl_handle != NULL) { 182757b448deSdougm ret = sa_delete_share( 182857b448deSdougm impl_handle->scfhandle, group, 182957b448deSdougm share); 183057b448deSdougm } else { 183157b448deSdougm ret = SA_SYSTEM_ERR; 183257b448deSdougm } 183357b448deSdougm } else { 183457b448deSdougm char *sharepath = sa_get_share_attr(share, 183557b448deSdougm "path"); 183657b448deSdougm if (sharepath != NULL) { 183757b448deSdougm ret = sa_zfs_set_sharenfs(group, 183857b448deSdougm sharepath, 0); 183957b448deSdougm sa_free_attr_string(sharepath); 184057b448deSdougm } 184157b448deSdougm } 18426185db85Sdougm } 18436185db85Sdougm } 18446185db85Sdougm if (groupname != NULL) 184557b448deSdougm sa_free_attr_string(groupname); 18466185db85Sdougm if (zfs != NULL) 184757b448deSdougm sa_free_attr_string(zfs); 18486185db85Sdougm 18496185db85Sdougm xmlUnlinkNode((xmlNodePtr)share); 18506185db85Sdougm xmlFreeNode((xmlNodePtr)share); 18516185db85Sdougm return (ret); 18526185db85Sdougm } 18536185db85Sdougm 18546185db85Sdougm /* 18556185db85Sdougm * sa_move_share(group, share) 18566185db85Sdougm * 18576185db85Sdougm * move the specified share to the specified group. Update SMF 18586185db85Sdougm * appropriately. 18596185db85Sdougm */ 18606185db85Sdougm 18616185db85Sdougm int 18626185db85Sdougm sa_move_share(sa_group_t group, sa_share_t share) 18636185db85Sdougm { 18646185db85Sdougm sa_group_t oldgroup; 18656185db85Sdougm int ret = SA_OK; 18666185db85Sdougm 18676185db85Sdougm /* remove the node from its group then free the memory */ 18686185db85Sdougm 18696185db85Sdougm oldgroup = sa_get_parent_group(share); 18706185db85Sdougm if (oldgroup != group) { 187157b448deSdougm sa_handle_impl_t impl_handle; 187257b448deSdougm xmlUnlinkNode((xmlNodePtr)share); 18736185db85Sdougm /* 187457b448deSdougm * now that the share isn't in its old group, add to 187557b448deSdougm * the new one 18766185db85Sdougm */ 18774bff34e3Sthurlow (void) xmlAddChild((xmlNodePtr)group, (xmlNodePtr)share); 187857b448deSdougm /* need to deal with SMF */ 187957b448deSdougm impl_handle = (sa_handle_impl_t)sa_find_group_handle(group); 188057b448deSdougm if (impl_handle != NULL) { 188157b448deSdougm /* 188257b448deSdougm * need to remove from old group first and then add to 188357b448deSdougm * new group. Ideally, we would do the other order but 188457b448deSdougm * need to avoid having the share in two groups at the 188557b448deSdougm * same time. 188657b448deSdougm */ 188757b448deSdougm ret = sa_delete_share(impl_handle->scfhandle, oldgroup, 188857b448deSdougm share); 188957b448deSdougm if (ret == SA_OK) 189057b448deSdougm ret = sa_commit_share(impl_handle->scfhandle, 189157b448deSdougm group, share); 189257b448deSdougm } else { 189357b448deSdougm ret = SA_SYSTEM_ERR; 189457b448deSdougm } 18956185db85Sdougm } 18966185db85Sdougm return (ret); 18976185db85Sdougm } 18986185db85Sdougm 18996185db85Sdougm /* 19006185db85Sdougm * sa_get_parent_group(share) 19016185db85Sdougm * 1902da6c28aaSamw * Return the containing group for the share. If a group was actually 19036185db85Sdougm * passed in, we don't want a parent so return NULL. 19046185db85Sdougm */ 19056185db85Sdougm 19066185db85Sdougm sa_group_t 19076185db85Sdougm sa_get_parent_group(sa_share_t share) 19086185db85Sdougm { 19096185db85Sdougm xmlNodePtr node = NULL; 19106185db85Sdougm if (share != NULL) { 191157b448deSdougm node = ((xmlNodePtr)share)->parent; 19126185db85Sdougm /* 19136185db85Sdougm * make sure parent is a group and not sharecfg since 19146185db85Sdougm * we may be cheating and passing in a group. 19156185db85Sdougm * Eventually, groups of groups might come into being. 19166185db85Sdougm */ 191757b448deSdougm if (node == NULL || 191857b448deSdougm xmlStrcmp(node->name, (xmlChar *)"sharecfg") == 0) 191957b448deSdougm node = NULL; 19206185db85Sdougm } 19216185db85Sdougm return ((sa_group_t)node); 19226185db85Sdougm } 19236185db85Sdougm 19246185db85Sdougm /* 1925549ec3ffSdougm * _sa_create_group(impl_handle, groupname) 19266185db85Sdougm * 19276185db85Sdougm * Create a group in the document. The caller will need to deal with 19286185db85Sdougm * configuration store and activation. 19296185db85Sdougm */ 19306185db85Sdougm 19316185db85Sdougm sa_group_t 1932549ec3ffSdougm _sa_create_group(sa_handle_impl_t impl_handle, char *groupname) 19336185db85Sdougm { 19346185db85Sdougm xmlNodePtr node = NULL; 19356185db85Sdougm 19366185db85Sdougm if (sa_valid_group_name(groupname)) { 193757b448deSdougm node = xmlNewChild(impl_handle->tree, NULL, (xmlChar *)"group", 193857b448deSdougm NULL); 193957b448deSdougm if (node != NULL) { 19404bff34e3Sthurlow (void) xmlSetProp(node, (xmlChar *)"name", 194157b448deSdougm (xmlChar *)groupname); 19424bff34e3Sthurlow (void) xmlSetProp(node, (xmlChar *)"state", 194357b448deSdougm (xmlChar *)"enabled"); 194457b448deSdougm } 19456185db85Sdougm } 19466185db85Sdougm return ((sa_group_t)node); 19476185db85Sdougm } 19486185db85Sdougm 19496185db85Sdougm /* 19506185db85Sdougm * _sa_create_zfs_group(group, groupname) 19516185db85Sdougm * 19526185db85Sdougm * Create a ZFS subgroup under the specified group. This may 19536185db85Sdougm * eventually form the basis of general sub-groups, but is currently 19546185db85Sdougm * restricted to ZFS. 19556185db85Sdougm */ 19566185db85Sdougm sa_group_t 19576185db85Sdougm _sa_create_zfs_group(sa_group_t group, char *groupname) 19586185db85Sdougm { 19596185db85Sdougm xmlNodePtr node = NULL; 19606185db85Sdougm 196157b448deSdougm node = xmlNewChild((xmlNodePtr)group, NULL, (xmlChar *)"group", NULL); 19626185db85Sdougm if (node != NULL) { 19634bff34e3Sthurlow (void) xmlSetProp(node, (xmlChar *)"name", 19644bff34e3Sthurlow (xmlChar *)groupname); 19654bff34e3Sthurlow (void) xmlSetProp(node, (xmlChar *)"state", 19664bff34e3Sthurlow (xmlChar *)"enabled"); 19676185db85Sdougm } 19686185db85Sdougm 19696185db85Sdougm return ((sa_group_t)node); 19706185db85Sdougm } 19716185db85Sdougm 19726185db85Sdougm /* 19736185db85Sdougm * sa_create_group(groupname, *error) 19746185db85Sdougm * 19756185db85Sdougm * Create a new group with groupname. Need to validate that it is a 19766185db85Sdougm * legal name for SMF and the construct the SMF service instance of 19776185db85Sdougm * svc:/network/shares/group to implement the group. All necessary 19786185db85Sdougm * operational properties must be added to the group at this point 19796185db85Sdougm * (via the SMF transaction model). 19806185db85Sdougm */ 19816185db85Sdougm sa_group_t 1982549ec3ffSdougm sa_create_group(sa_handle_t handle, char *groupname, int *error) 19836185db85Sdougm { 19846185db85Sdougm xmlNodePtr node = NULL; 19856185db85Sdougm sa_group_t group; 19866185db85Sdougm int ret; 198757b448deSdougm char rbacstr[SA_STRSIZE]; 1988549ec3ffSdougm sa_handle_impl_t impl_handle = (sa_handle_impl_t)handle; 19896185db85Sdougm 19906185db85Sdougm ret = SA_OK; 19916185db85Sdougm 1992549ec3ffSdougm if (impl_handle == NULL || impl_handle->scfhandle == NULL) { 199357b448deSdougm ret = SA_SYSTEM_ERR; 199457b448deSdougm goto err; 19956185db85Sdougm } 19966185db85Sdougm 1997549ec3ffSdougm group = sa_get_group(handle, groupname); 19986185db85Sdougm if (group != NULL) { 199957b448deSdougm ret = SA_DUPLICATE_NAME; 20006185db85Sdougm } else { 200157b448deSdougm if (sa_valid_group_name(groupname)) { 200257b448deSdougm node = xmlNewChild(impl_handle->tree, NULL, 200357b448deSdougm (xmlChar *)"group", NULL); 200457b448deSdougm if (node != NULL) { 20054bff34e3Sthurlow (void) xmlSetProp(node, (xmlChar *)"name", 200657b448deSdougm (xmlChar *)groupname); 200757b448deSdougm /* default to the group being enabled */ 20084bff34e3Sthurlow (void) xmlSetProp(node, (xmlChar *)"state", 200957b448deSdougm (xmlChar *)"enabled"); 201057b448deSdougm ret = sa_create_instance(impl_handle->scfhandle, 201157b448deSdougm groupname); 201257b448deSdougm if (ret == SA_OK) { 201357b448deSdougm ret = sa_start_transaction( 201457b448deSdougm impl_handle->scfhandle, 201557b448deSdougm "operation"); 201657b448deSdougm } 201757b448deSdougm if (ret == SA_OK) { 201857b448deSdougm ret = sa_set_property( 201957b448deSdougm impl_handle->scfhandle, 202057b448deSdougm "state", "enabled"); 202157b448deSdougm if (ret == SA_OK) { 202257b448deSdougm ret = sa_end_transaction( 20235b6e0c46Sdougm impl_handle->scfhandle, 20245b6e0c46Sdougm impl_handle); 202557b448deSdougm } else { 202657b448deSdougm sa_abort_transaction( 202757b448deSdougm impl_handle->scfhandle); 202857b448deSdougm } 202957b448deSdougm } 203057b448deSdougm if (ret == SA_OK) { 203157b448deSdougm /* initialize the RBAC strings */ 203257b448deSdougm ret = sa_start_transaction( 203357b448deSdougm impl_handle->scfhandle, 203457b448deSdougm "general"); 203557b448deSdougm if (ret == SA_OK) { 203657b448deSdougm (void) snprintf(rbacstr, 203757b448deSdougm sizeof (rbacstr), "%s.%s", 203857b448deSdougm SA_RBAC_MANAGE, groupname); 203957b448deSdougm ret = sa_set_property( 204057b448deSdougm impl_handle->scfhandle, 20416185db85Sdougm "action_authorization", 20426185db85Sdougm rbacstr); 204357b448deSdougm } 204457b448deSdougm if (ret == SA_OK) { 204557b448deSdougm (void) snprintf(rbacstr, 204657b448deSdougm sizeof (rbacstr), "%s.%s", 204757b448deSdougm SA_RBAC_VALUE, groupname); 204857b448deSdougm ret = sa_set_property( 204957b448deSdougm impl_handle->scfhandle, 20506185db85Sdougm "value_authorization", 20516185db85Sdougm rbacstr); 205257b448deSdougm } 205357b448deSdougm if (ret == SA_OK) { 205457b448deSdougm ret = sa_end_transaction( 20555b6e0c46Sdougm impl_handle->scfhandle, 20565b6e0c46Sdougm impl_handle); 205757b448deSdougm } else { 205857b448deSdougm sa_abort_transaction( 205957b448deSdougm impl_handle->scfhandle); 206057b448deSdougm } 206157b448deSdougm } 206257b448deSdougm if (ret != SA_OK) { 206357b448deSdougm /* 206457b448deSdougm * Couldn't commit the group 206557b448deSdougm * so we need to undo 206657b448deSdougm * internally. 206757b448deSdougm */ 206857b448deSdougm xmlUnlinkNode(node); 206957b448deSdougm xmlFreeNode(node); 207057b448deSdougm node = NULL; 207157b448deSdougm } 20726185db85Sdougm } else { 207357b448deSdougm ret = SA_NO_MEMORY; 20746185db85Sdougm } 20756185db85Sdougm } else { 207657b448deSdougm ret = SA_INVALID_NAME; 20776185db85Sdougm } 20786185db85Sdougm } 20796185db85Sdougm err: 20806185db85Sdougm if (error != NULL) 208157b448deSdougm *error = ret; 20826185db85Sdougm return ((sa_group_t)node); 20836185db85Sdougm } 20846185db85Sdougm 20856185db85Sdougm /* 20866185db85Sdougm * sa_remove_group(group) 20876185db85Sdougm * 20886185db85Sdougm * Remove the specified group. This deletes from the SMF repository. 20896185db85Sdougm * All property groups and properties are removed. 20906185db85Sdougm */ 20916185db85Sdougm 20926185db85Sdougm int 20936185db85Sdougm sa_remove_group(sa_group_t group) 20946185db85Sdougm { 20956185db85Sdougm char *name; 20966185db85Sdougm int ret = SA_OK; 2097549ec3ffSdougm sa_handle_impl_t impl_handle; 20986185db85Sdougm 2099549ec3ffSdougm impl_handle = (sa_handle_impl_t)sa_find_group_handle(group); 2100549ec3ffSdougm if (impl_handle != NULL) { 210157b448deSdougm name = sa_get_group_attr(group, "name"); 210257b448deSdougm if (name != NULL) { 210357b448deSdougm ret = sa_delete_instance(impl_handle->scfhandle, name); 210457b448deSdougm sa_free_attr_string(name); 210557b448deSdougm } 210657b448deSdougm xmlUnlinkNode((xmlNodePtr)group); /* make sure unlinked */ 210757b448deSdougm xmlFreeNode((xmlNodePtr)group); /* now it is gone */ 2108549ec3ffSdougm } else { 210957b448deSdougm ret = SA_SYSTEM_ERR; 21106185db85Sdougm } 21116185db85Sdougm return (ret); 21126185db85Sdougm } 21136185db85Sdougm 21146185db85Sdougm /* 21156185db85Sdougm * sa_update_config() 21166185db85Sdougm * 21176185db85Sdougm * Used to update legacy files that need to be updated in bulk 21186185db85Sdougm * Currently, this is a placeholder and will go away in a future 21196185db85Sdougm * release. 21206185db85Sdougm */ 21216185db85Sdougm 21226185db85Sdougm int 2123549ec3ffSdougm sa_update_config(sa_handle_t handle) 21246185db85Sdougm { 21256185db85Sdougm /* 21266185db85Sdougm * do legacy files first so we can tell when they change. 21276185db85Sdougm * This will go away when we start updating individual records 21286185db85Sdougm * rather than the whole file. 21296185db85Sdougm */ 2130549ec3ffSdougm update_legacy_config(handle); 21316185db85Sdougm return (SA_OK); 21326185db85Sdougm } 21336185db85Sdougm 21346185db85Sdougm /* 21356185db85Sdougm * get_node_attr(node, tag) 21366185db85Sdougm * 2137da6c28aaSamw * Get the specified tag(attribute) if it exists on the node. This is 21386185db85Sdougm * used internally by a number of attribute oriented functions. 21396185db85Sdougm */ 21406185db85Sdougm 21416185db85Sdougm static char * 21426185db85Sdougm get_node_attr(void *nodehdl, char *tag) 21436185db85Sdougm { 21446185db85Sdougm xmlNodePtr node = (xmlNodePtr)nodehdl; 21456185db85Sdougm xmlChar *name = NULL; 21466185db85Sdougm 214757b448deSdougm if (node != NULL) 21486185db85Sdougm name = xmlGetProp(node, (xmlChar *)tag); 21496185db85Sdougm return ((char *)name); 21506185db85Sdougm } 21516185db85Sdougm 21526185db85Sdougm /* 2153fe1c642dSBill Krier * set_node_attr(node, tag) 21546185db85Sdougm * 2155da6c28aaSamw * Set the specified tag(attribute) to the specified value This is 21566185db85Sdougm * used internally by a number of attribute oriented functions. It 21576185db85Sdougm * doesn't update the repository, only the internal document state. 21586185db85Sdougm */ 21596185db85Sdougm 21606185db85Sdougm void 21616185db85Sdougm set_node_attr(void *nodehdl, char *tag, char *value) 21626185db85Sdougm { 21636185db85Sdougm xmlNodePtr node = (xmlNodePtr)nodehdl; 21646185db85Sdougm if (node != NULL && tag != NULL) { 216557b448deSdougm if (value != NULL) 21664bff34e3Sthurlow (void) xmlSetProp(node, (xmlChar *)tag, 21674bff34e3Sthurlow (xmlChar *)value); 216857b448deSdougm else 21694bff34e3Sthurlow (void) xmlUnsetProp(node, (xmlChar *)tag); 21706185db85Sdougm } 21716185db85Sdougm } 21726185db85Sdougm 21736185db85Sdougm /* 21746185db85Sdougm * sa_get_group_attr(group, tag) 21756185db85Sdougm * 21766185db85Sdougm * Get the specied attribute, if defined, for the group. 21776185db85Sdougm */ 21786185db85Sdougm 21796185db85Sdougm char * 21806185db85Sdougm sa_get_group_attr(sa_group_t group, char *tag) 21816185db85Sdougm { 21826185db85Sdougm return (get_node_attr((void *)group, tag)); 21836185db85Sdougm } 21846185db85Sdougm 21856185db85Sdougm /* 21866185db85Sdougm * sa_set_group_attr(group, tag, value) 21876185db85Sdougm * 21886185db85Sdougm * set the specified tag/attribute on the group using value as its 21896185db85Sdougm * value. 21906185db85Sdougm * 21916185db85Sdougm * This will result in setting the property in the SMF repository as 21926185db85Sdougm * well as in the internal document. 21936185db85Sdougm */ 21946185db85Sdougm 21956185db85Sdougm int 21966185db85Sdougm sa_set_group_attr(sa_group_t group, char *tag, char *value) 21976185db85Sdougm { 21986185db85Sdougm int ret; 21996185db85Sdougm char *groupname; 2200549ec3ffSdougm sa_handle_impl_t impl_handle; 22016185db85Sdougm 2202da6c28aaSamw /* 2203da6c28aaSamw * ZFS group/subgroup doesn't need the handle so shortcut. 2204da6c28aaSamw */ 2205da6c28aaSamw if (sa_group_is_zfs(group)) { 2206da6c28aaSamw set_node_attr((void *)group, tag, value); 2207da6c28aaSamw return (SA_OK); 2208da6c28aaSamw } 2209da6c28aaSamw 2210549ec3ffSdougm impl_handle = (sa_handle_impl_t)sa_find_group_handle(group); 2211549ec3ffSdougm if (impl_handle != NULL) { 221257b448deSdougm groupname = sa_get_group_attr(group, "name"); 221357b448deSdougm ret = sa_get_instance(impl_handle->scfhandle, groupname); 2214549ec3ffSdougm if (ret == SA_OK) { 221557b448deSdougm set_node_attr((void *)group, tag, value); 221657b448deSdougm ret = sa_start_transaction(impl_handle->scfhandle, 221757b448deSdougm "operation"); 221857b448deSdougm if (ret == SA_OK) { 221957b448deSdougm ret = sa_set_property(impl_handle->scfhandle, 222057b448deSdougm tag, value); 222157b448deSdougm if (ret == SA_OK) 2222573b0c00Sdougm ret = sa_end_transaction( 22235b6e0c46Sdougm impl_handle->scfhandle, 22245b6e0c46Sdougm impl_handle); 222557b448deSdougm else 222657b448deSdougm sa_abort_transaction( 222757b448deSdougm impl_handle->scfhandle); 222857b448deSdougm } 2229573b0c00Sdougm if (ret == SA_SYSTEM_ERR) 2230573b0c00Sdougm ret = SA_NO_PERMISSION; 22316185db85Sdougm } 223257b448deSdougm if (groupname != NULL) 223357b448deSdougm sa_free_attr_string(groupname); 2234549ec3ffSdougm } else { 223557b448deSdougm ret = SA_SYSTEM_ERR; 22366185db85Sdougm } 22376185db85Sdougm return (ret); 22386185db85Sdougm } 22396185db85Sdougm 22406185db85Sdougm /* 22416185db85Sdougm * sa_get_share_attr(share, tag) 22426185db85Sdougm * 22436185db85Sdougm * Return the value of the tag/attribute set on the specified 22446185db85Sdougm * share. Returns NULL if the tag doesn't exist. 22456185db85Sdougm */ 22466185db85Sdougm 22476185db85Sdougm char * 22486185db85Sdougm sa_get_share_attr(sa_share_t share, char *tag) 22496185db85Sdougm { 22506185db85Sdougm return (get_node_attr((void *)share, tag)); 22516185db85Sdougm } 22526185db85Sdougm 22536185db85Sdougm /* 22546185db85Sdougm * _sa_set_share_description(share, description) 22556185db85Sdougm * 2256da6c28aaSamw * Add a description tag with text contents to the specified share. A 2257da6c28aaSamw * separate XML tag is used rather than a property. This can also be 2258da6c28aaSamw * used with resources. 22596185db85Sdougm */ 22606185db85Sdougm 22616185db85Sdougm xmlNodePtr 2262da6c28aaSamw _sa_set_share_description(void *share, char *content) 22636185db85Sdougm { 22646185db85Sdougm xmlNodePtr node; 226557b448deSdougm node = xmlNewChild((xmlNodePtr)share, NULL, (xmlChar *)"description", 226657b448deSdougm NULL); 22676185db85Sdougm xmlNodeSetContent(node, (xmlChar *)content); 22686185db85Sdougm return (node); 22696185db85Sdougm } 22706185db85Sdougm 22716185db85Sdougm /* 22726185db85Sdougm * sa_set_share_attr(share, tag, value) 22736185db85Sdougm * 22746185db85Sdougm * Set the share attribute specified by tag to the specified value. In 22756185db85Sdougm * the case of "resource", enforce a no duplicates in a group rule. If 22766185db85Sdougm * the share is not transient, commit the changes to the repository 22776185db85Sdougm * else just update the share internally. 22786185db85Sdougm */ 22796185db85Sdougm 22806185db85Sdougm int 22816185db85Sdougm sa_set_share_attr(sa_share_t share, char *tag, char *value) 22826185db85Sdougm { 22836185db85Sdougm sa_group_t group; 22846185db85Sdougm sa_share_t resource; 22856185db85Sdougm int ret = SA_OK; 22866185db85Sdougm 22876185db85Sdougm group = sa_get_parent_group(share); 22886185db85Sdougm 22896185db85Sdougm /* 22906185db85Sdougm * There are some attributes that may have specific 22916185db85Sdougm * restrictions on them. Initially, only "resource" has 22926185db85Sdougm * special meaning that needs to be checked. Only one instance 22936185db85Sdougm * of a resource name may exist within a group. 22946185db85Sdougm */ 22956185db85Sdougm 22966185db85Sdougm if (strcmp(tag, "resource") == 0) { 229757b448deSdougm resource = sa_get_resource(group, value); 229857b448deSdougm if (resource != share && resource != NULL) 229957b448deSdougm ret = SA_DUPLICATE_NAME; 23006185db85Sdougm } 23016185db85Sdougm if (ret == SA_OK) { 230257b448deSdougm set_node_attr((void *)share, tag, value); 230357b448deSdougm if (group != NULL) { 230457b448deSdougm char *type; 230557b448deSdougm /* we can probably optimize this some */ 230657b448deSdougm type = sa_get_share_attr(share, "type"); 230757b448deSdougm if (type == NULL || strcmp(type, "transient") != 0) { 230857b448deSdougm sa_handle_impl_t impl_handle; 230957b448deSdougm impl_handle = 231057b448deSdougm (sa_handle_impl_t)sa_find_group_handle( 231157b448deSdougm group); 231257b448deSdougm if (impl_handle != NULL) { 231357b448deSdougm ret = sa_commit_share( 231457b448deSdougm impl_handle->scfhandle, group, 231557b448deSdougm share); 231657b448deSdougm } else { 231757b448deSdougm ret = SA_SYSTEM_ERR; 231857b448deSdougm } 231957b448deSdougm } 232057b448deSdougm if (type != NULL) 232157b448deSdougm sa_free_attr_string(type); 2322549ec3ffSdougm } 23236185db85Sdougm } 23246185db85Sdougm return (ret); 23256185db85Sdougm } 23266185db85Sdougm 23276185db85Sdougm /* 23286185db85Sdougm * sa_get_property_attr(prop, tag) 23296185db85Sdougm * 23306185db85Sdougm * Get the value of the specified property attribute. Standard 23316185db85Sdougm * attributes are "type" and "value". 23326185db85Sdougm */ 23336185db85Sdougm 23346185db85Sdougm char * 23356185db85Sdougm sa_get_property_attr(sa_property_t prop, char *tag) 23366185db85Sdougm { 23376185db85Sdougm return (get_node_attr((void *)prop, tag)); 23386185db85Sdougm } 23396185db85Sdougm 23406185db85Sdougm /* 23416185db85Sdougm * sa_get_optionset_attr(prop, tag) 23426185db85Sdougm * 23436185db85Sdougm * Get the value of the specified property attribute. Standard 23446185db85Sdougm * attribute is "type". 23456185db85Sdougm */ 23466185db85Sdougm 23476185db85Sdougm char * 23486185db85Sdougm sa_get_optionset_attr(sa_property_t optionset, char *tag) 23496185db85Sdougm { 23506185db85Sdougm return (get_node_attr((void *)optionset, tag)); 23516185db85Sdougm 23526185db85Sdougm } 23536185db85Sdougm 23546185db85Sdougm /* 23556185db85Sdougm * sa_set_optionset_attr(optionset, tag, value) 23566185db85Sdougm * 23576185db85Sdougm * Set the specified attribute(tag) to the specified value on the 23586185db85Sdougm * optionset. 23596185db85Sdougm */ 23606185db85Sdougm 23616185db85Sdougm void 23626185db85Sdougm sa_set_optionset_attr(sa_group_t optionset, char *tag, char *value) 23636185db85Sdougm { 23646185db85Sdougm set_node_attr((void *)optionset, tag, value); 23656185db85Sdougm } 23666185db85Sdougm 23676185db85Sdougm /* 23686185db85Sdougm * sa_free_attr_string(string) 23696185db85Sdougm * 23706185db85Sdougm * Free the string that was returned in one of the sa_get_*_attr() 23716185db85Sdougm * functions. 23726185db85Sdougm */ 23736185db85Sdougm 23746185db85Sdougm void 23756185db85Sdougm sa_free_attr_string(char *string) 23766185db85Sdougm { 23776185db85Sdougm xmlFree((xmlChar *)string); 23786185db85Sdougm } 23796185db85Sdougm 23806185db85Sdougm /* 23816185db85Sdougm * sa_get_optionset(group, proto) 23826185db85Sdougm * 23836185db85Sdougm * Return the optionset, if it exists, that is associated with the 23846185db85Sdougm * specified protocol. 23856185db85Sdougm */ 23866185db85Sdougm 23876185db85Sdougm sa_optionset_t 23886185db85Sdougm sa_get_optionset(void *group, char *proto) 23896185db85Sdougm { 23906185db85Sdougm xmlNodePtr node; 23916185db85Sdougm xmlChar *value = NULL; 23926185db85Sdougm 23936185db85Sdougm for (node = ((xmlNodePtr)group)->children; node != NULL; 239457b448deSdougm node = node->next) { 23956185db85Sdougm if (xmlStrcmp(node->name, (xmlChar *)"optionset") == 0) { 239657b448deSdougm value = xmlGetProp(node, (xmlChar *)"type"); 239757b448deSdougm if (proto != NULL) { 239857b448deSdougm if (value != NULL && 239957b448deSdougm xmlStrcmp(value, (xmlChar *)proto) == 0) { 240057b448deSdougm break; 240157b448deSdougm } 240257b448deSdougm if (value != NULL) { 240357b448deSdougm xmlFree(value); 240457b448deSdougm value = NULL; 240557b448deSdougm } 240657b448deSdougm } else { 240757b448deSdougm break; 24086185db85Sdougm } 24096185db85Sdougm } 24106185db85Sdougm } 24116185db85Sdougm if (value != NULL) 241257b448deSdougm xmlFree(value); 24136185db85Sdougm return ((sa_optionset_t)node); 24146185db85Sdougm } 24156185db85Sdougm 24166185db85Sdougm /* 24176185db85Sdougm * sa_get_next_optionset(optionset) 24186185db85Sdougm * 24196185db85Sdougm * Return the next optionset in the group. NULL if this was the last. 24206185db85Sdougm */ 24216185db85Sdougm 24226185db85Sdougm sa_optionset_t 24236185db85Sdougm sa_get_next_optionset(sa_optionset_t optionset) 24246185db85Sdougm { 24256185db85Sdougm xmlNodePtr node; 24266185db85Sdougm 24276185db85Sdougm for (node = ((xmlNodePtr)optionset)->next; node != NULL; 242857b448deSdougm node = node->next) { 24296185db85Sdougm if (xmlStrcmp(node->name, (xmlChar *)"optionset") == 0) { 24306185db85Sdougm break; 24316185db85Sdougm } 24326185db85Sdougm } 24336185db85Sdougm return ((sa_optionset_t)node); 24346185db85Sdougm } 24356185db85Sdougm 24366185db85Sdougm /* 24376185db85Sdougm * sa_get_security(group, sectype, proto) 24386185db85Sdougm * 24396185db85Sdougm * Return the security optionset. The internal name is a hold over 24406185db85Sdougm * from the implementation and will be changed before the API is 24416185db85Sdougm * finalized. This is really a named optionset that can be negotiated 24426185db85Sdougm * as a group of properties (like NFS security options). 24436185db85Sdougm */ 24446185db85Sdougm 24456185db85Sdougm sa_security_t 24466185db85Sdougm sa_get_security(sa_group_t group, char *sectype, char *proto) 24476185db85Sdougm { 24486185db85Sdougm xmlNodePtr node; 24496185db85Sdougm xmlChar *value = NULL; 24506185db85Sdougm 24516185db85Sdougm for (node = ((xmlNodePtr)group)->children; node != NULL; 245257b448deSdougm node = node->next) { 245357b448deSdougm if (xmlStrcmp(node->name, (xmlChar *)"security") == 0) { 245457b448deSdougm if (proto != NULL) { 245557b448deSdougm value = xmlGetProp(node, (xmlChar *)"type"); 245657b448deSdougm if (value == NULL || 245757b448deSdougm (value != NULL && 245857b448deSdougm xmlStrcmp(value, (xmlChar *)proto) != 0)) { 245957b448deSdougm /* it doesn't match so continue */ 246057b448deSdougm xmlFree(value); 246157b448deSdougm value = NULL; 246257b448deSdougm continue; 246357b448deSdougm } 246457b448deSdougm } 246557b448deSdougm if (value != NULL) { 246657b448deSdougm xmlFree(value); 246757b448deSdougm value = NULL; 246857b448deSdougm } 246957b448deSdougm /* potential match */ 247057b448deSdougm if (sectype != NULL) { 247157b448deSdougm value = xmlGetProp(node, (xmlChar *)"sectype"); 247257b448deSdougm if (value != NULL && 247357b448deSdougm xmlStrcmp(value, (xmlChar *)sectype) == 0) { 247457b448deSdougm break; 247557b448deSdougm } 247657b448deSdougm } else { 247757b448deSdougm break; 247857b448deSdougm } 24796185db85Sdougm } 24806185db85Sdougm if (value != NULL) { 248157b448deSdougm xmlFree(value); 248257b448deSdougm value = NULL; 24836185db85Sdougm } 24846185db85Sdougm } 24856185db85Sdougm if (value != NULL) 248657b448deSdougm xmlFree(value); 24876185db85Sdougm return ((sa_security_t)node); 24886185db85Sdougm } 24896185db85Sdougm 24906185db85Sdougm /* 24916185db85Sdougm * sa_get_next_security(security) 24926185db85Sdougm * 24936185db85Sdougm * Get the next security optionset if one exists. 24946185db85Sdougm */ 24956185db85Sdougm 24966185db85Sdougm sa_security_t 24976185db85Sdougm sa_get_next_security(sa_security_t security) 24986185db85Sdougm { 24996185db85Sdougm xmlNodePtr node; 25006185db85Sdougm 25016185db85Sdougm for (node = ((xmlNodePtr)security)->next; node != NULL; 250257b448deSdougm node = node->next) { 25036185db85Sdougm if (xmlStrcmp(node->name, (xmlChar *)"security") == 0) { 25046185db85Sdougm break; 25056185db85Sdougm } 25066185db85Sdougm } 25076185db85Sdougm return ((sa_security_t)node); 25086185db85Sdougm } 25096185db85Sdougm 25106185db85Sdougm /* 25116185db85Sdougm * sa_get_property(optionset, prop) 25126185db85Sdougm * 25136185db85Sdougm * Get the property object with the name specified in prop from the 25146185db85Sdougm * optionset. 25156185db85Sdougm */ 25166185db85Sdougm 25176185db85Sdougm sa_property_t 25186185db85Sdougm sa_get_property(sa_optionset_t optionset, char *prop) 25196185db85Sdougm { 25206185db85Sdougm xmlNodePtr node = (xmlNodePtr)optionset; 25216185db85Sdougm xmlChar *value = NULL; 25226185db85Sdougm 25236185db85Sdougm if (optionset == NULL) 252457b448deSdougm return (NULL); 25256185db85Sdougm 25266185db85Sdougm for (node = node->children; node != NULL; 252757b448deSdougm node = node->next) { 252857b448deSdougm if (xmlStrcmp(node->name, (xmlChar *)"option") == 0) { 252957b448deSdougm if (prop == NULL) 253057b448deSdougm break; 253157b448deSdougm value = xmlGetProp(node, (xmlChar *)"type"); 253257b448deSdougm if (value != NULL && 253357b448deSdougm xmlStrcmp(value, (xmlChar *)prop) == 0) { 253457b448deSdougm break; 253557b448deSdougm } 253657b448deSdougm if (value != NULL) { 253757b448deSdougm xmlFree(value); 253857b448deSdougm value = NULL; 253957b448deSdougm } 25406185db85Sdougm } 25416185db85Sdougm } 25426185db85Sdougm if (value != NULL) 25436185db85Sdougm xmlFree(value); 25446185db85Sdougm if (node != NULL && xmlStrcmp(node->name, (xmlChar *)"option") != 0) { 254557b448deSdougm /* 254657b448deSdougm * avoid a non option node -- it is possible to be a 254757b448deSdougm * text node 254857b448deSdougm */ 254957b448deSdougm node = NULL; 25506185db85Sdougm } 25516185db85Sdougm return ((sa_property_t)node); 25526185db85Sdougm } 25536185db85Sdougm 25546185db85Sdougm /* 25556185db85Sdougm * sa_get_next_property(property) 25566185db85Sdougm * 25576185db85Sdougm * Get the next property following the specified property. NULL if 25586185db85Sdougm * this was the last. 25596185db85Sdougm */ 25606185db85Sdougm 25616185db85Sdougm sa_property_t 25626185db85Sdougm sa_get_next_property(sa_property_t property) 25636185db85Sdougm { 25646185db85Sdougm xmlNodePtr node; 25656185db85Sdougm 25666185db85Sdougm for (node = ((xmlNodePtr)property)->next; node != NULL; 256757b448deSdougm node = node->next) { 25686185db85Sdougm if (xmlStrcmp(node->name, (xmlChar *)"option") == 0) { 25696185db85Sdougm break; 25706185db85Sdougm } 25716185db85Sdougm } 25726185db85Sdougm return ((sa_property_t)node); 25736185db85Sdougm } 25746185db85Sdougm 25756185db85Sdougm /* 25766185db85Sdougm * sa_set_share_description(share, content) 25776185db85Sdougm * 25786185db85Sdougm * Set the description of share to content. 25796185db85Sdougm */ 25806185db85Sdougm 25816185db85Sdougm int 25826185db85Sdougm sa_set_share_description(sa_share_t share, char *content) 25836185db85Sdougm { 25846185db85Sdougm xmlNodePtr node; 25856185db85Sdougm sa_group_t group; 25866185db85Sdougm int ret = SA_OK; 25876185db85Sdougm 25886185db85Sdougm for (node = ((xmlNodePtr)share)->children; node != NULL; 258957b448deSdougm node = node->next) { 25906185db85Sdougm if (xmlStrcmp(node->name, (xmlChar *)"description") == 0) { 25916185db85Sdougm break; 25926185db85Sdougm } 25936185db85Sdougm } 25946185db85Sdougm /* no existing description but want to add */ 25956185db85Sdougm if (node == NULL && content != NULL) { 25966185db85Sdougm /* add a description */ 259757b448deSdougm node = _sa_set_share_description(share, content); 25986185db85Sdougm } else if (node != NULL && content != NULL) { 25996185db85Sdougm /* update a description */ 26006185db85Sdougm xmlNodeSetContent(node, (xmlChar *)content); 26016185db85Sdougm } else if (node != NULL && content == NULL) { 26026185db85Sdougm /* remove an existing description */ 26036185db85Sdougm xmlUnlinkNode(node); 26046185db85Sdougm xmlFreeNode(node); 26056185db85Sdougm } 2606da6c28aaSamw group = sa_get_parent_group(share); 2607148c5f43SAlan Wright if (group != NULL && 2608148c5f43SAlan Wright sa_is_persistent(share) && (!sa_group_is_zfs(group))) { 260957b448deSdougm sa_handle_impl_t impl_handle; 261057b448deSdougm impl_handle = (sa_handle_impl_t)sa_find_group_handle(group); 261157b448deSdougm if (impl_handle != NULL) { 261257b448deSdougm ret = sa_commit_share(impl_handle->scfhandle, group, 261357b448deSdougm share); 261457b448deSdougm } else { 261557b448deSdougm ret = SA_SYSTEM_ERR; 261657b448deSdougm } 2617549ec3ffSdougm } 26186185db85Sdougm return (ret); 26196185db85Sdougm } 26206185db85Sdougm 26216185db85Sdougm /* 26226185db85Sdougm * fixproblemchars(string) 26236185db85Sdougm * 26246185db85Sdougm * don't want any newline or tab characters in the text since these 26256185db85Sdougm * could break display of data and legacy file formats. 26266185db85Sdougm */ 26276185db85Sdougm static void 26286185db85Sdougm fixproblemchars(char *str) 26296185db85Sdougm { 26306185db85Sdougm int c; 26316185db85Sdougm for (c = *str; c != '\0'; c = *++str) { 263257b448deSdougm if (c == '\t' || c == '\n') 263357b448deSdougm *str = ' '; 263457b448deSdougm else if (c == '"') 263557b448deSdougm *str = '\''; 26366185db85Sdougm } 26376185db85Sdougm } 26386185db85Sdougm 26396185db85Sdougm /* 26406185db85Sdougm * sa_get_share_description(share) 26416185db85Sdougm * 26426185db85Sdougm * Return the description text for the specified share if it 26436185db85Sdougm * exists. NULL if no description exists. 26446185db85Sdougm */ 26456185db85Sdougm 26466185db85Sdougm char * 26476185db85Sdougm sa_get_share_description(sa_share_t share) 26486185db85Sdougm { 26496185db85Sdougm xmlChar *description = NULL; 26506185db85Sdougm xmlNodePtr node; 26516185db85Sdougm 26526185db85Sdougm for (node = ((xmlNodePtr)share)->children; node != NULL; 265357b448deSdougm node = node->next) { 265457b448deSdougm if (xmlStrcmp(node->name, (xmlChar *)"description") == 0) { 265557b448deSdougm break; 265657b448deSdougm } 26576185db85Sdougm } 26586185db85Sdougm if (node != NULL) { 2659da6c28aaSamw description = xmlNodeGetContent(node); 266057b448deSdougm fixproblemchars((char *)description); 26616185db85Sdougm } 26626185db85Sdougm return ((char *)description); 26636185db85Sdougm } 26646185db85Sdougm 26656185db85Sdougm /* 26666185db85Sdougm * sa_free(share_description(description) 26676185db85Sdougm * 26686185db85Sdougm * Free the description string. 26696185db85Sdougm */ 26706185db85Sdougm 26716185db85Sdougm void 26726185db85Sdougm sa_free_share_description(char *description) 26736185db85Sdougm { 26746185db85Sdougm xmlFree((xmlChar *)description); 26756185db85Sdougm } 26766185db85Sdougm 26776185db85Sdougm /* 26786185db85Sdougm * sa_create_optionset(group, proto) 26796185db85Sdougm * 26806185db85Sdougm * Create an optionset for the specified protocol in the specied 26816185db85Sdougm * group. This is manifested as a property group within SMF. 26826185db85Sdougm */ 26836185db85Sdougm 26846185db85Sdougm sa_optionset_t 26856185db85Sdougm sa_create_optionset(sa_group_t group, char *proto) 26866185db85Sdougm { 26876185db85Sdougm sa_optionset_t optionset; 26886185db85Sdougm sa_group_t parent = group; 2689da6c28aaSamw sa_share_t share = NULL; 2690da6c28aaSamw int err = SA_OK; 2691da6c28aaSamw char *id = NULL; 26926185db85Sdougm 26936185db85Sdougm optionset = sa_get_optionset(group, proto); 26946185db85Sdougm if (optionset != NULL) { 26956185db85Sdougm /* can't have a duplicate protocol */ 269657b448deSdougm optionset = NULL; 26976185db85Sdougm } else { 2698da6c28aaSamw /* 2699da6c28aaSamw * Account for resource names being slightly 2700da6c28aaSamw * different. 2701da6c28aaSamw */ 2702da6c28aaSamw if (sa_is_share(group)) { 2703da6c28aaSamw /* 2704da6c28aaSamw * Transient shares do not have an "id" so not an 2705da6c28aaSamw * error to not find one. 2706da6c28aaSamw */ 2707da6c28aaSamw id = sa_get_share_attr((sa_share_t)group, "id"); 2708da6c28aaSamw } else if (sa_is_resource(group)) { 2709da6c28aaSamw share = sa_get_resource_parent( 2710da6c28aaSamw (sa_resource_t)group); 2711da6c28aaSamw id = sa_get_resource_attr(share, "id"); 2712da6c28aaSamw 2713da6c28aaSamw /* id can be NULL if the group is transient (ZFS) */ 2714da6c28aaSamw if (id == NULL && sa_is_persistent(group)) 2715da6c28aaSamw err = SA_NO_MEMORY; 2716da6c28aaSamw } 2717da6c28aaSamw if (err == SA_NO_MEMORY) { 2718da6c28aaSamw /* 2719da6c28aaSamw * Couldn't get the id for the share or 2720da6c28aaSamw * resource. While this could be a 2721da6c28aaSamw * configuration issue, it is most likely an 2722da6c28aaSamw * out of memory. In any case, fail the create. 2723da6c28aaSamw */ 2724da6c28aaSamw return (NULL); 2725da6c28aaSamw } 2726da6c28aaSamw 272757b448deSdougm optionset = (sa_optionset_t)xmlNewChild((xmlNodePtr)group, 272857b448deSdougm NULL, (xmlChar *)"optionset", NULL); 27296185db85Sdougm /* 27306185db85Sdougm * only put to repository if on a group and we were 27316185db85Sdougm * able to create an optionset. 27326185db85Sdougm */ 273357b448deSdougm if (optionset != NULL) { 273457b448deSdougm char oname[SA_STRSIZE]; 273557b448deSdougm char *groupname; 27366185db85Sdougm 2737da6c28aaSamw /* 2738da6c28aaSamw * Need to get parent group in all cases, but also get 2739da6c28aaSamw * the share if this is a resource. 2740da6c28aaSamw */ 2741da6c28aaSamw if (sa_is_share(group)) { 274257b448deSdougm parent = sa_get_parent_group((sa_share_t)group); 2743da6c28aaSamw } else if (sa_is_resource(group)) { 2744da6c28aaSamw share = sa_get_resource_parent( 2745da6c28aaSamw (sa_resource_t)group); 2746da6c28aaSamw parent = sa_get_parent_group(share); 2747da6c28aaSamw } 27486185db85Sdougm 274957b448deSdougm sa_set_optionset_attr(optionset, "type", proto); 27506185db85Sdougm 275157b448deSdougm (void) sa_optionset_name(optionset, oname, 275257b448deSdougm sizeof (oname), id); 275357b448deSdougm groupname = sa_get_group_attr(parent, "name"); 2754da6c28aaSamw if (groupname != NULL && sa_is_persistent(group)) { 275557b448deSdougm sa_handle_impl_t impl_handle; 2756da6c28aaSamw impl_handle = 2757da6c28aaSamw (sa_handle_impl_t)sa_find_group_handle( 2758da6c28aaSamw group); 275957b448deSdougm assert(impl_handle != NULL); 276057b448deSdougm if (impl_handle != NULL) { 276157b448deSdougm (void) sa_get_instance( 2762da6c28aaSamw impl_handle->scfhandle, groupname); 276357b448deSdougm (void) sa_create_pgroup( 276457b448deSdougm impl_handle->scfhandle, oname); 276557b448deSdougm } 276657b448deSdougm } 276757b448deSdougm if (groupname != NULL) 276857b448deSdougm sa_free_attr_string(groupname); 27696185db85Sdougm } 27706185db85Sdougm } 2771da6c28aaSamw 2772da6c28aaSamw if (id != NULL) 2773da6c28aaSamw sa_free_attr_string(id); 27746185db85Sdougm return (optionset); 27756185db85Sdougm } 27766185db85Sdougm 27776185db85Sdougm /* 27786185db85Sdougm * sa_get_property_parent(property) 27796185db85Sdougm * 27806185db85Sdougm * Given a property, return the object it is a property of. This will 27816185db85Sdougm * be an optionset of some type. 27826185db85Sdougm */ 27836185db85Sdougm 27846185db85Sdougm static sa_optionset_t 27856185db85Sdougm sa_get_property_parent(sa_property_t property) 27866185db85Sdougm { 27876185db85Sdougm xmlNodePtr node = NULL; 27886185db85Sdougm 278957b448deSdougm if (property != NULL) 279057b448deSdougm node = ((xmlNodePtr)property)->parent; 27916185db85Sdougm return ((sa_optionset_t)node); 27926185db85Sdougm } 27936185db85Sdougm 27946185db85Sdougm /* 27956185db85Sdougm * sa_get_optionset_parent(optionset) 27966185db85Sdougm * 27976185db85Sdougm * Return the parent of the specified optionset. This could be a group 27986185db85Sdougm * or a share. 27996185db85Sdougm */ 28006185db85Sdougm 28016185db85Sdougm static sa_group_t 28026185db85Sdougm sa_get_optionset_parent(sa_optionset_t optionset) 28036185db85Sdougm { 28046185db85Sdougm xmlNodePtr node = NULL; 28056185db85Sdougm 280657b448deSdougm if (optionset != NULL) 280757b448deSdougm node = ((xmlNodePtr)optionset)->parent; 28086185db85Sdougm return ((sa_group_t)node); 28096185db85Sdougm } 28106185db85Sdougm 28116185db85Sdougm /* 28126185db85Sdougm * zfs_needs_update(share) 28136185db85Sdougm * 28146185db85Sdougm * In order to avoid making multiple updates to a ZFS share when 28156185db85Sdougm * setting properties, the share attribute "changed" will be set to 2816da6c28aaSamw * true when a property is added or modified. When done adding 28176185db85Sdougm * properties, we can then detect that an update is needed. We then 28186185db85Sdougm * clear the state here to detect additional changes. 28196185db85Sdougm */ 28206185db85Sdougm 28216185db85Sdougm static int 28226185db85Sdougm zfs_needs_update(sa_share_t share) 28236185db85Sdougm { 28246185db85Sdougm char *attr; 28256185db85Sdougm int result = 0; 28266185db85Sdougm 28276185db85Sdougm attr = sa_get_share_attr(share, "changed"); 28286185db85Sdougm if (attr != NULL) { 282957b448deSdougm sa_free_attr_string(attr); 28306185db85Sdougm result = 1; 28316185db85Sdougm } 28326185db85Sdougm set_node_attr((void *)share, "changed", NULL); 28336185db85Sdougm return (result); 28346185db85Sdougm } 28356185db85Sdougm 28366185db85Sdougm /* 28376185db85Sdougm * zfs_set_update(share) 28386185db85Sdougm * 28396185db85Sdougm * Set the changed attribute of the share to true. 28406185db85Sdougm */ 28416185db85Sdougm 28426185db85Sdougm static void 28436185db85Sdougm zfs_set_update(sa_share_t share) 28446185db85Sdougm { 28456185db85Sdougm set_node_attr((void *)share, "changed", "true"); 28466185db85Sdougm } 28476185db85Sdougm 28486185db85Sdougm /* 28496185db85Sdougm * sa_commit_properties(optionset, clear) 28506185db85Sdougm * 28516185db85Sdougm * Check if SMF or ZFS config and either update or abort the pending 28526185db85Sdougm * changes. 28536185db85Sdougm */ 28546185db85Sdougm 28556185db85Sdougm int 28566185db85Sdougm sa_commit_properties(sa_optionset_t optionset, int clear) 28576185db85Sdougm { 28586185db85Sdougm sa_group_t group; 28596185db85Sdougm sa_group_t parent; 28606185db85Sdougm int zfs = 0; 28616185db85Sdougm int needsupdate = 0; 28626185db85Sdougm int ret = SA_OK; 2863549ec3ffSdougm sa_handle_impl_t impl_handle; 28646185db85Sdougm 28656185db85Sdougm group = sa_get_optionset_parent(optionset); 28666185db85Sdougm if (group != NULL && (sa_is_share(group) || is_zfs_group(group))) { 286757b448deSdougm /* only update ZFS if on a share */ 286857b448deSdougm parent = sa_get_parent_group(group); 286957b448deSdougm zfs++; 287057b448deSdougm if (parent != NULL && is_zfs_group(parent)) 287157b448deSdougm needsupdate = zfs_needs_update(group); 287257b448deSdougm else 287357b448deSdougm zfs = 0; 28746185db85Sdougm } 28756185db85Sdougm if (zfs) { 287657b448deSdougm if (!clear && needsupdate) 287757b448deSdougm ret = sa_zfs_update((sa_share_t)group); 28786185db85Sdougm } else { 287957b448deSdougm impl_handle = (sa_handle_impl_t)sa_find_group_handle(group); 288057b448deSdougm if (impl_handle != NULL) { 288157b448deSdougm if (clear) { 288257b448deSdougm (void) sa_abort_transaction( 288357b448deSdougm impl_handle->scfhandle); 288457b448deSdougm } else { 288557b448deSdougm ret = sa_end_transaction( 28865b6e0c46Sdougm impl_handle->scfhandle, impl_handle); 288757b448deSdougm } 288857b448deSdougm } else { 288957b448deSdougm ret = SA_SYSTEM_ERR; 289057b448deSdougm } 28916185db85Sdougm } 28926185db85Sdougm return (ret); 28936185db85Sdougm } 28946185db85Sdougm 28956185db85Sdougm /* 28966185db85Sdougm * sa_destroy_optionset(optionset) 28976185db85Sdougm * 2898da6c28aaSamw * Remove the optionset from its group. Update the repository to 28996185db85Sdougm * reflect this change. 29006185db85Sdougm */ 29016185db85Sdougm 29026185db85Sdougm int 29036185db85Sdougm sa_destroy_optionset(sa_optionset_t optionset) 29046185db85Sdougm { 290557b448deSdougm char name[SA_STRSIZE]; 29066185db85Sdougm int len; 29076185db85Sdougm int ret; 29086185db85Sdougm char *id = NULL; 29096185db85Sdougm sa_group_t group; 29106185db85Sdougm int ispersist = 1; 29116185db85Sdougm 29126185db85Sdougm /* now delete the prop group */ 29136185db85Sdougm group = sa_get_optionset_parent(optionset); 2914da6c28aaSamw if (group != NULL) { 2915da6c28aaSamw if (sa_is_resource(group)) { 2916da6c28aaSamw sa_resource_t resource = group; 2917da6c28aaSamw sa_share_t share = sa_get_resource_parent(resource); 2918da6c28aaSamw group = sa_get_parent_group(share); 2919da6c28aaSamw id = sa_get_share_attr(share, "id"); 2920da6c28aaSamw } else if (sa_is_share(group)) { 2921da6c28aaSamw id = sa_get_share_attr((sa_share_t)group, "id"); 2922da6c28aaSamw } 2923da6c28aaSamw ispersist = sa_is_persistent(group); 29246185db85Sdougm } 29256185db85Sdougm if (ispersist) { 292657b448deSdougm sa_handle_impl_t impl_handle; 292757b448deSdougm len = sa_optionset_name(optionset, name, sizeof (name), id); 292857b448deSdougm impl_handle = (sa_handle_impl_t)sa_find_group_handle(group); 292957b448deSdougm if (impl_handle != NULL) { 293057b448deSdougm if (len > 0) { 293157b448deSdougm ret = sa_delete_pgroup(impl_handle->scfhandle, 293257b448deSdougm name); 293357b448deSdougm } 293457b448deSdougm } else { 293557b448deSdougm ret = SA_SYSTEM_ERR; 2936549ec3ffSdougm } 29376185db85Sdougm } 29386185db85Sdougm xmlUnlinkNode((xmlNodePtr)optionset); 29396185db85Sdougm xmlFreeNode((xmlNodePtr)optionset); 29406185db85Sdougm if (id != NULL) 294157b448deSdougm sa_free_attr_string(id); 29426185db85Sdougm return (ret); 29436185db85Sdougm } 29446185db85Sdougm 29456185db85Sdougm /* private to the implementation */ 29466185db85Sdougm int 29476185db85Sdougm _sa_remove_optionset(sa_optionset_t optionset) 29486185db85Sdougm { 29496185db85Sdougm int ret = SA_OK; 29506185db85Sdougm 29516185db85Sdougm xmlUnlinkNode((xmlNodePtr)optionset); 29526185db85Sdougm xmlFreeNode((xmlNodePtr)optionset); 29536185db85Sdougm return (ret); 29546185db85Sdougm } 29556185db85Sdougm 29566185db85Sdougm /* 29576185db85Sdougm * sa_create_security(group, sectype, proto) 29586185db85Sdougm * 29596185db85Sdougm * Create a security optionset (one that has a type name and a 29606185db85Sdougm * proto). Security is left over from a pure NFS implementation. The 29616185db85Sdougm * naming will change in the future when the API is released. 29626185db85Sdougm */ 29636185db85Sdougm sa_security_t 29646185db85Sdougm sa_create_security(sa_group_t group, char *sectype, char *proto) 29656185db85Sdougm { 29666185db85Sdougm sa_security_t security; 29676185db85Sdougm char *id = NULL; 29686185db85Sdougm sa_group_t parent; 29696185db85Sdougm char *groupname = NULL; 29706185db85Sdougm 29716185db85Sdougm if (group != NULL && sa_is_share(group)) { 297257b448deSdougm id = sa_get_share_attr((sa_share_t)group, "id"); 297357b448deSdougm parent = sa_get_parent_group(group); 297457b448deSdougm if (parent != NULL) 297557b448deSdougm groupname = sa_get_group_attr(parent, "name"); 29766185db85Sdougm } else if (group != NULL) { 297757b448deSdougm groupname = sa_get_group_attr(group, "name"); 29786185db85Sdougm } 29796185db85Sdougm 29806185db85Sdougm security = sa_get_security(group, sectype, proto); 29816185db85Sdougm if (security != NULL) { 29826185db85Sdougm /* can't have a duplicate security option */ 29836185db85Sdougm security = NULL; 29846185db85Sdougm } else { 29856185db85Sdougm security = (sa_security_t)xmlNewChild((xmlNodePtr)group, 298657b448deSdougm NULL, (xmlChar *)"security", NULL); 29876185db85Sdougm if (security != NULL) { 298857b448deSdougm char oname[SA_STRSIZE]; 29896185db85Sdougm sa_set_security_attr(security, "type", proto); 29906185db85Sdougm 29916185db85Sdougm sa_set_security_attr(security, "sectype", sectype); 29926185db85Sdougm (void) sa_security_name(security, oname, 299357b448deSdougm sizeof (oname), id); 2994da6c28aaSamw if (groupname != NULL && sa_is_persistent(group)) { 299557b448deSdougm sa_handle_impl_t impl_handle; 299657b448deSdougm impl_handle = 299757b448deSdougm (sa_handle_impl_t)sa_find_group_handle( 299857b448deSdougm group); 299957b448deSdougm if (impl_handle != NULL) { 300057b448deSdougm (void) sa_get_instance( 300157b448deSdougm impl_handle->scfhandle, groupname); 300257b448deSdougm (void) sa_create_pgroup( 300357b448deSdougm impl_handle->scfhandle, oname); 300457b448deSdougm } 30056185db85Sdougm } 30066185db85Sdougm } 30076185db85Sdougm } 3008fe1c642dSBill Krier if (id != NULL) 3009fe1c642dSBill Krier sa_free_attr_string(id); 30106185db85Sdougm if (groupname != NULL) 301157b448deSdougm sa_free_attr_string(groupname); 30126185db85Sdougm return (security); 30136185db85Sdougm } 30146185db85Sdougm 30156185db85Sdougm /* 30166185db85Sdougm * sa_destroy_security(security) 30176185db85Sdougm * 30186185db85Sdougm * Remove the specified optionset from the document and the 30196185db85Sdougm * configuration. 30206185db85Sdougm */ 30216185db85Sdougm 30226185db85Sdougm int 30236185db85Sdougm sa_destroy_security(sa_security_t security) 30246185db85Sdougm { 302557b448deSdougm char name[SA_STRSIZE]; 30266185db85Sdougm int len; 30276185db85Sdougm int ret = SA_OK; 30286185db85Sdougm char *id = NULL; 30296185db85Sdougm sa_group_t group; 30306185db85Sdougm int iszfs = 0; 30316185db85Sdougm int ispersist = 1; 30326185db85Sdougm 30336185db85Sdougm group = sa_get_optionset_parent(security); 30346185db85Sdougm 30356185db85Sdougm if (group != NULL) 303657b448deSdougm iszfs = sa_group_is_zfs(group); 30376185db85Sdougm 30386185db85Sdougm if (group != NULL && !iszfs) { 303957b448deSdougm if (sa_is_share(group)) 3040da6c28aaSamw ispersist = sa_is_persistent(group); 304157b448deSdougm id = sa_get_share_attr((sa_share_t)group, "id"); 30426185db85Sdougm } 30436185db85Sdougm if (ispersist) { 304457b448deSdougm len = sa_security_name(security, name, sizeof (name), id); 304557b448deSdougm if (!iszfs && len > 0) { 304657b448deSdougm sa_handle_impl_t impl_handle; 304757b448deSdougm impl_handle = 304857b448deSdougm (sa_handle_impl_t)sa_find_group_handle(group); 304957b448deSdougm if (impl_handle != NULL) { 305057b448deSdougm ret = sa_delete_pgroup(impl_handle->scfhandle, 305157b448deSdougm name); 305257b448deSdougm } else { 305357b448deSdougm ret = SA_SYSTEM_ERR; 305457b448deSdougm } 3055549ec3ffSdougm } 30566185db85Sdougm } 30576185db85Sdougm xmlUnlinkNode((xmlNodePtr)security); 30586185db85Sdougm xmlFreeNode((xmlNodePtr)security); 305957b448deSdougm if (iszfs) 306057b448deSdougm ret = sa_zfs_update(group); 30616185db85Sdougm if (id != NULL) 306257b448deSdougm sa_free_attr_string(id); 30636185db85Sdougm return (ret); 30646185db85Sdougm } 30656185db85Sdougm 30666185db85Sdougm /* 30676185db85Sdougm * sa_get_security_attr(optionset, tag) 30686185db85Sdougm * 30696185db85Sdougm * Return the specified attribute value from the optionset. 30706185db85Sdougm */ 30716185db85Sdougm 30726185db85Sdougm char * 30736185db85Sdougm sa_get_security_attr(sa_property_t optionset, char *tag) 30746185db85Sdougm { 30756185db85Sdougm return (get_node_attr((void *)optionset, tag)); 30766185db85Sdougm 30776185db85Sdougm } 30786185db85Sdougm 30796185db85Sdougm /* 30806185db85Sdougm * sa_set_security_attr(optionset, tag, value) 30816185db85Sdougm * 30826185db85Sdougm * Set the optioset attribute specied by tag to the specified value. 30836185db85Sdougm */ 30846185db85Sdougm 30856185db85Sdougm void 30866185db85Sdougm sa_set_security_attr(sa_group_t optionset, char *tag, char *value) 30876185db85Sdougm { 30886185db85Sdougm set_node_attr((void *)optionset, tag, value); 30896185db85Sdougm } 30906185db85Sdougm 30916185db85Sdougm /* 30926185db85Sdougm * is_nodetype(node, type) 30936185db85Sdougm * 30946185db85Sdougm * Check to see if node is of the type specified. 30956185db85Sdougm */ 30966185db85Sdougm 30976185db85Sdougm static int 30986185db85Sdougm is_nodetype(void *node, char *type) 30996185db85Sdougm { 31006185db85Sdougm return (strcmp((char *)((xmlNodePtr)node)->name, type) == 0); 31016185db85Sdougm } 31026185db85Sdougm 310357b448deSdougm /* 310457b448deSdougm * add_or_update() 310557b448deSdougm * 310657b448deSdougm * Add or update a property. Pulled out of sa_set_prop_by_prop for 310757b448deSdougm * readability. 310857b448deSdougm */ 310957b448deSdougm static int 311057b448deSdougm add_or_update(scfutilhandle_t *scf_handle, int type, scf_value_t *value, 311157b448deSdougm scf_transaction_entry_t *entry, char *name, char *valstr) 311257b448deSdougm { 311357b448deSdougm int ret = SA_SYSTEM_ERR; 311457b448deSdougm 311557b448deSdougm if (value != NULL) { 311657b448deSdougm if (type == SA_PROP_OP_ADD) 311757b448deSdougm ret = scf_transaction_property_new(scf_handle->trans, 311857b448deSdougm entry, name, SCF_TYPE_ASTRING); 311957b448deSdougm else 312057b448deSdougm ret = scf_transaction_property_change(scf_handle->trans, 312157b448deSdougm entry, name, SCF_TYPE_ASTRING); 312257b448deSdougm if (ret == 0) { 312357b448deSdougm ret = scf_value_set_astring(value, valstr); 312457b448deSdougm if (ret == 0) 312557b448deSdougm ret = scf_entry_add_value(entry, value); 312657b448deSdougm if (ret == 0) 312757b448deSdougm return (ret); 312857b448deSdougm scf_value_destroy(value); 312957b448deSdougm } else { 313057b448deSdougm scf_entry_destroy(entry); 313157b448deSdougm } 313257b448deSdougm } 313357b448deSdougm return (SA_SYSTEM_ERR); 313457b448deSdougm } 313557b448deSdougm 31366185db85Sdougm /* 31376185db85Sdougm * sa_set_prop_by_prop(optionset, group, prop, type) 31386185db85Sdougm * 31396185db85Sdougm * Add/remove/update the specified property prop into the optionset or 31406185db85Sdougm * share. If a share, sort out which property group based on GUID. In 31416185db85Sdougm * all cases, the appropriate transaction is set (or ZFS share is 31426185db85Sdougm * marked as needing an update) 31436185db85Sdougm */ 31446185db85Sdougm 31456185db85Sdougm static int 31466185db85Sdougm sa_set_prop_by_prop(sa_optionset_t optionset, sa_group_t group, 3147*8a981c33SDaniel Hoffman sa_property_t prop, int type) 31486185db85Sdougm { 31496185db85Sdougm char *name; 31506185db85Sdougm char *valstr; 31516185db85Sdougm int ret = SA_OK; 31526185db85Sdougm scf_transaction_entry_t *entry; 31536185db85Sdougm scf_value_t *value; 31546185db85Sdougm int opttype; /* 1 == optionset, 0 == security */ 31556185db85Sdougm char *id = NULL; 31566185db85Sdougm int iszfs = 0; 31576185db85Sdougm sa_group_t parent = NULL; 3158da6c28aaSamw sa_share_t share = NULL; 3159549ec3ffSdougm sa_handle_impl_t impl_handle; 3160549ec3ffSdougm scfutilhandle_t *scf_handle; 31616185db85Sdougm 3162da6c28aaSamw if (!sa_is_persistent(group)) { 31636185db85Sdougm /* 31646185db85Sdougm * if the group/share is not persistent we don't need 31656185db85Sdougm * to do anything here 31666185db85Sdougm */ 316757b448deSdougm return (SA_OK); 31686185db85Sdougm } 3169549ec3ffSdougm impl_handle = (sa_handle_impl_t)sa_find_group_handle(group); 317057b448deSdougm if (impl_handle == NULL || impl_handle->scfhandle == NULL) 317157b448deSdougm return (SA_SYSTEM_ERR); 3172549ec3ffSdougm scf_handle = impl_handle->scfhandle; 31736185db85Sdougm name = sa_get_property_attr(prop, "type"); 31746185db85Sdougm valstr = sa_get_property_attr(prop, "value"); 31756185db85Sdougm entry = scf_entry_create(scf_handle->handle); 31766185db85Sdougm opttype = is_nodetype((void *)optionset, "optionset"); 31776185db85Sdougm 3178da6c28aaSamw /* 3179da6c28aaSamw * Check for share vs. resource since they need slightly 3180da6c28aaSamw * different treatment given the hierarchy. 3181da6c28aaSamw */ 31826185db85Sdougm if (valstr != NULL && entry != NULL) { 318357b448deSdougm if (sa_is_share(group)) { 318457b448deSdougm parent = sa_get_parent_group(group); 3185da6c28aaSamw share = (sa_share_t)group; 318657b448deSdougm if (parent != NULL) 318757b448deSdougm iszfs = is_zfs_group(parent); 3188da6c28aaSamw } else if (sa_is_resource(group)) { 3189da6c28aaSamw share = sa_get_parent_group(group); 3190da6c28aaSamw if (share != NULL) 3191da6c28aaSamw parent = sa_get_parent_group(share); 319257b448deSdougm } else { 319357b448deSdougm iszfs = is_zfs_group(group); 31946185db85Sdougm } 319557b448deSdougm if (!iszfs) { 319657b448deSdougm if (scf_handle->trans == NULL) { 319757b448deSdougm char oname[SA_STRSIZE]; 319857b448deSdougm char *groupname = NULL; 3199da6c28aaSamw if (share != NULL) { 3200da6c28aaSamw if (parent != NULL) 320157b448deSdougm groupname = 320257b448deSdougm sa_get_group_attr(parent, 320357b448deSdougm "name"); 3204da6c28aaSamw id = sa_get_share_attr( 3205da6c28aaSamw (sa_share_t)share, "id"); 3206549ec3ffSdougm } else { 320757b448deSdougm groupname = sa_get_group_attr(group, 320857b448deSdougm "name"); 32096185db85Sdougm } 321057b448deSdougm if (groupname != NULL) { 321157b448deSdougm ret = sa_get_instance(scf_handle, 321257b448deSdougm groupname); 321357b448deSdougm sa_free_attr_string(groupname); 321457b448deSdougm } 321557b448deSdougm if (opttype) 321657b448deSdougm (void) sa_optionset_name(optionset, 321757b448deSdougm oname, sizeof (oname), id); 321857b448deSdougm else 321957b448deSdougm (void) sa_security_name(optionset, 322057b448deSdougm oname, sizeof (oname), id); 322157b448deSdougm ret = sa_start_transaction(scf_handle, oname); 3222fe1c642dSBill Krier if (id != NULL) 3223fe1c642dSBill Krier sa_free_attr_string(id); 32246185db85Sdougm } 322557b448deSdougm if (ret == SA_OK) { 322657b448deSdougm switch (type) { 322757b448deSdougm case SA_PROP_OP_REMOVE: 322857b448deSdougm ret = scf_transaction_property_delete( 322957b448deSdougm scf_handle->trans, entry, name); 323057b448deSdougm break; 323157b448deSdougm case SA_PROP_OP_ADD: 323257b448deSdougm case SA_PROP_OP_UPDATE: 323357b448deSdougm value = scf_value_create( 323457b448deSdougm scf_handle->handle); 323557b448deSdougm ret = add_or_update(scf_handle, type, 323657b448deSdougm value, entry, name, valstr); 323757b448deSdougm break; 323857b448deSdougm } 323957b448deSdougm } 324057b448deSdougm } else { 324157b448deSdougm /* 324257b448deSdougm * ZFS update. The calling function would have updated 324357b448deSdougm * the internal XML structure. Just need to flag it as 324457b448deSdougm * changed for ZFS. 324557b448deSdougm */ 324657b448deSdougm zfs_set_update((sa_share_t)group); 324757b448deSdougm } 32486185db85Sdougm } 32496185db85Sdougm 32506185db85Sdougm if (name != NULL) 325157b448deSdougm sa_free_attr_string(name); 32526185db85Sdougm if (valstr != NULL) 325357b448deSdougm sa_free_attr_string(valstr); 32546185db85Sdougm else if (entry != NULL) 325557b448deSdougm scf_entry_destroy(entry); 32566185db85Sdougm 32576185db85Sdougm if (ret == -1) 325857b448deSdougm ret = SA_SYSTEM_ERR; 32596185db85Sdougm 32606185db85Sdougm return (ret); 32616185db85Sdougm } 32626185db85Sdougm 32636185db85Sdougm /* 32644bff34e3Sthurlow * sa_create_section(name, value) 32654bff34e3Sthurlow * 32664bff34e3Sthurlow * Create a new section with the specified name and extra data. 32674bff34e3Sthurlow */ 32684bff34e3Sthurlow 32694bff34e3Sthurlow sa_property_t 32704bff34e3Sthurlow sa_create_section(char *name, char *extra) 32714bff34e3Sthurlow { 32724bff34e3Sthurlow xmlNodePtr node; 32734bff34e3Sthurlow 32744bff34e3Sthurlow node = xmlNewNode(NULL, (xmlChar *)"section"); 32754bff34e3Sthurlow if (node != NULL) { 32764bff34e3Sthurlow if (name != NULL) 32774bff34e3Sthurlow (void) xmlSetProp(node, (xmlChar *)"name", 32784bff34e3Sthurlow (xmlChar *)name); 32794bff34e3Sthurlow if (extra != NULL) 32804bff34e3Sthurlow (void) xmlSetProp(node, (xmlChar *)"extra", 32814bff34e3Sthurlow (xmlChar *)extra); 32824bff34e3Sthurlow } 32834bff34e3Sthurlow return ((sa_property_t)node); 32844bff34e3Sthurlow } 32854bff34e3Sthurlow 32864bff34e3Sthurlow void 32874bff34e3Sthurlow sa_set_section_attr(sa_property_t sect, char *name, char *value) 32884bff34e3Sthurlow { 32894bff34e3Sthurlow (void) xmlSetProp(sect, (xmlChar *)name, (xmlChar *)value); 32904bff34e3Sthurlow } 32914bff34e3Sthurlow 32924bff34e3Sthurlow /* 32934bff34e3Sthurlow * sa_create_property(section, name, value) 32946185db85Sdougm * 32956185db85Sdougm * Create a new property with the specified name and value. 32966185db85Sdougm */ 32976185db85Sdougm 32986185db85Sdougm sa_property_t 32996185db85Sdougm sa_create_property(char *name, char *value) 33006185db85Sdougm { 33016185db85Sdougm xmlNodePtr node; 33026185db85Sdougm 33036185db85Sdougm node = xmlNewNode(NULL, (xmlChar *)"option"); 33046185db85Sdougm if (node != NULL) { 33054bff34e3Sthurlow (void) xmlSetProp(node, (xmlChar *)"type", (xmlChar *)name); 33064bff34e3Sthurlow (void) xmlSetProp(node, (xmlChar *)"value", (xmlChar *)value); 33076185db85Sdougm } 33086185db85Sdougm return ((sa_property_t)node); 33096185db85Sdougm } 33106185db85Sdougm 33116185db85Sdougm /* 33126185db85Sdougm * sa_add_property(object, property) 33136185db85Sdougm * 33146185db85Sdougm * Add the specified property to the object. Issue the appropriate 33156185db85Sdougm * transaction or mark a ZFS object as needing an update. 33166185db85Sdougm */ 33176185db85Sdougm 33186185db85Sdougm int 33196185db85Sdougm sa_add_property(void *object, sa_property_t property) 33206185db85Sdougm { 33216185db85Sdougm int ret = SA_OK; 33226185db85Sdougm sa_group_t parent; 33236185db85Sdougm sa_group_t group; 33246185db85Sdougm char *proto; 33256185db85Sdougm 33266185db85Sdougm if (property != NULL) { 33273b61b335Sdougm sa_handle_t handle; 3328687915e9Sdougm handle = sa_find_group_handle((sa_group_t)object); 33293b61b335Sdougm /* It is legitimate to not find a handle */ 3330687915e9Sdougm proto = sa_get_optionset_attr(object, "type"); 3331687915e9Sdougm if ((ret = sa_valid_property(handle, object, proto, 3332687915e9Sdougm property)) == SA_OK) { 333357b448deSdougm property = (sa_property_t)xmlAddChild( 333457b448deSdougm (xmlNodePtr)object, (xmlNodePtr)property); 333557b448deSdougm } else { 333657b448deSdougm if (proto != NULL) 333757b448deSdougm sa_free_attr_string(proto); 333857b448deSdougm return (ret); 333957b448deSdougm } 3340687915e9Sdougm if (proto != NULL) 3341687915e9Sdougm sa_free_attr_string(proto); 33426185db85Sdougm } 33436185db85Sdougm 33446185db85Sdougm 33456185db85Sdougm parent = sa_get_parent_group(object); 3346da6c28aaSamw if (!sa_is_persistent(parent)) 334757b448deSdougm return (ret); 33486185db85Sdougm 3349da6c28aaSamw if (sa_is_resource(parent)) { 3350da6c28aaSamw /* 3351da6c28aaSamw * Resources are children of share. Need to go up two 3352da6c28aaSamw * levels to find the group but the parent needs to be 3353da6c28aaSamw * the share at this point in order to get the "id". 3354da6c28aaSamw */ 3355da6c28aaSamw parent = sa_get_parent_group(parent); 335657b448deSdougm group = sa_get_parent_group(parent); 3357da6c28aaSamw } else if (sa_is_share(parent)) { 3358da6c28aaSamw group = sa_get_parent_group(parent); 3359da6c28aaSamw } else { 336057b448deSdougm group = parent; 3361da6c28aaSamw } 3362549ec3ffSdougm 336357b448deSdougm if (property == NULL) { 336457b448deSdougm ret = SA_NO_MEMORY; 336557b448deSdougm } else { 336657b448deSdougm char oname[SA_STRSIZE]; 336757b448deSdougm 336857b448deSdougm if (!is_zfs_group(group)) { 336957b448deSdougm char *id = NULL; 337057b448deSdougm sa_handle_impl_t impl_handle; 337157b448deSdougm scfutilhandle_t *scf_handle; 337257b448deSdougm 337357b448deSdougm impl_handle = (sa_handle_impl_t)sa_find_group_handle( 337457b448deSdougm group); 337557b448deSdougm if (impl_handle == NULL || 337657b448deSdougm impl_handle->scfhandle == NULL) 337757b448deSdougm ret = SA_SYSTEM_ERR; 337857b448deSdougm if (ret == SA_OK) { 337957b448deSdougm scf_handle = impl_handle->scfhandle; 338057b448deSdougm if (sa_is_share((sa_group_t)parent)) { 338157b448deSdougm id = sa_get_share_attr( 338257b448deSdougm (sa_share_t)parent, "id"); 338357b448deSdougm } 338457b448deSdougm if (scf_handle->trans == NULL) { 338557b448deSdougm if (is_nodetype(object, "optionset")) { 338657b448deSdougm (void) sa_optionset_name( 338757b448deSdougm (sa_optionset_t)object, 338857b448deSdougm oname, sizeof (oname), id); 338957b448deSdougm } else { 339057b448deSdougm (void) sa_security_name( 339157b448deSdougm (sa_optionset_t)object, 339257b448deSdougm oname, sizeof (oname), id); 339357b448deSdougm } 339457b448deSdougm ret = sa_start_transaction(scf_handle, 339557b448deSdougm oname); 339657b448deSdougm } 339757b448deSdougm if (ret == SA_OK) { 339857b448deSdougm char *name; 339957b448deSdougm char *value; 340057b448deSdougm name = sa_get_property_attr(property, 340157b448deSdougm "type"); 340257b448deSdougm value = sa_get_property_attr(property, 340357b448deSdougm "value"); 340457b448deSdougm if (name != NULL && value != NULL) { 340557b448deSdougm if (scf_handle->scf_state == 340657b448deSdougm SCH_STATE_INIT) { 340757b448deSdougm ret = sa_set_property( 340857b448deSdougm scf_handle, name, 340957b448deSdougm value); 341057b448deSdougm } 341157b448deSdougm } else { 341257b448deSdougm ret = SA_CONFIG_ERR; 341357b448deSdougm } 341457b448deSdougm if (name != NULL) 341557b448deSdougm sa_free_attr_string( 341657b448deSdougm name); 341757b448deSdougm if (value != NULL) 341857b448deSdougm sa_free_attr_string(value); 341957b448deSdougm } 342057b448deSdougm if (id != NULL) 342157b448deSdougm sa_free_attr_string(id); 342257b448deSdougm } 342357b448deSdougm } else { 342457b448deSdougm /* 342557b448deSdougm * ZFS is a special case. We do want 342657b448deSdougm * to allow editing property/security 342757b448deSdougm * lists since we can have a better 342857b448deSdougm * syntax and we also want to keep 342957b448deSdougm * things consistent when possible. 343057b448deSdougm * 343157b448deSdougm * Right now, we defer until the 343257b448deSdougm * sa_commit_properties so we can get 343357b448deSdougm * them all at once. We do need to 343457b448deSdougm * mark the share as "changed" 343557b448deSdougm */ 343657b448deSdougm zfs_set_update((sa_share_t)parent); 34376185db85Sdougm } 34386185db85Sdougm } 34396185db85Sdougm return (ret); 34406185db85Sdougm } 34416185db85Sdougm 34426185db85Sdougm /* 34436185db85Sdougm * sa_remove_property(property) 34446185db85Sdougm * 34456185db85Sdougm * Remove the specied property from its containing object. Update the 34466185db85Sdougm * repository as appropriate. 34476185db85Sdougm */ 34486185db85Sdougm 34496185db85Sdougm int 34506185db85Sdougm sa_remove_property(sa_property_t property) 34516185db85Sdougm { 34526185db85Sdougm int ret = SA_OK; 34536185db85Sdougm 34546185db85Sdougm if (property != NULL) { 34556185db85Sdougm sa_optionset_t optionset; 34566185db85Sdougm sa_group_t group; 34576185db85Sdougm optionset = sa_get_property_parent(property); 34586185db85Sdougm if (optionset != NULL) { 345957b448deSdougm group = sa_get_optionset_parent(optionset); 346057b448deSdougm if (group != NULL) { 346157b448deSdougm ret = sa_set_prop_by_prop(optionset, group, 346257b448deSdougm property, SA_PROP_OP_REMOVE); 346357b448deSdougm } 34646185db85Sdougm } 34656185db85Sdougm xmlUnlinkNode((xmlNodePtr)property); 34666185db85Sdougm xmlFreeNode((xmlNodePtr)property); 34676185db85Sdougm } else { 346857b448deSdougm ret = SA_NO_SUCH_PROP; 34696185db85Sdougm } 34706185db85Sdougm return (ret); 34716185db85Sdougm } 34726185db85Sdougm 34736185db85Sdougm /* 34746185db85Sdougm * sa_update_property(property, value) 34756185db85Sdougm * 34766185db85Sdougm * Update the specified property to the new value. If value is NULL, 34776185db85Sdougm * we currently treat this as a remove. 34786185db85Sdougm */ 34796185db85Sdougm 34806185db85Sdougm int 34816185db85Sdougm sa_update_property(sa_property_t property, char *value) 34826185db85Sdougm { 34836185db85Sdougm int ret = SA_OK; 34846185db85Sdougm if (value == NULL) { 34856185db85Sdougm return (sa_remove_property(property)); 34866185db85Sdougm } else { 34876185db85Sdougm sa_optionset_t optionset; 34886185db85Sdougm sa_group_t group; 34896185db85Sdougm set_node_attr((void *)property, "value", value); 34906185db85Sdougm optionset = sa_get_property_parent(property); 34916185db85Sdougm if (optionset != NULL) { 349257b448deSdougm group = sa_get_optionset_parent(optionset); 349357b448deSdougm if (group != NULL) { 349457b448deSdougm ret = sa_set_prop_by_prop(optionset, group, 349557b448deSdougm property, SA_PROP_OP_UPDATE); 349657b448deSdougm } 34976185db85Sdougm } else { 349857b448deSdougm ret = SA_NO_SUCH_PROP; 34996185db85Sdougm } 35006185db85Sdougm } 35016185db85Sdougm return (ret); 35026185db85Sdougm } 35036185db85Sdougm 35044bff34e3Sthurlow /* 35054bff34e3Sthurlow * sa_get_protocol_section(propset, prop) 35064bff34e3Sthurlow * 35074bff34e3Sthurlow * Get the specified protocol specific section. These are global to 35084bff34e3Sthurlow * the protocol and not specific to a group or share. 35094bff34e3Sthurlow */ 35104bff34e3Sthurlow 35114bff34e3Sthurlow sa_protocol_properties_t 35124bff34e3Sthurlow sa_get_protocol_section(sa_protocol_properties_t propset, char *section) 35134bff34e3Sthurlow { 35144bff34e3Sthurlow xmlNodePtr node = (xmlNodePtr)propset; 35154bff34e3Sthurlow xmlChar *value = NULL; 35164bff34e3Sthurlow char *proto; 35174bff34e3Sthurlow 35184bff34e3Sthurlow proto = sa_get_optionset_attr(propset, "type"); 35199c9af259SGordon Ross if ((sa_proto_get_featureset(proto) & SA_FEATURE_HAS_SECTIONS) == 0) { 35209c9af259SGordon Ross if (proto != NULL) 35219c9af259SGordon Ross sa_free_attr_string(proto); 35224bff34e3Sthurlow return (propset); 35239c9af259SGordon Ross } 35244bff34e3Sthurlow 35254bff34e3Sthurlow for (node = node->children; node != NULL; 35264bff34e3Sthurlow node = node->next) { 35274bff34e3Sthurlow if (xmlStrcmp(node->name, (xmlChar *)"section") == 0) { 35284bff34e3Sthurlow if (section == NULL) 35294bff34e3Sthurlow break; 35304bff34e3Sthurlow value = xmlGetProp(node, (xmlChar *)"name"); 35314bff34e3Sthurlow if (value != NULL && 35324bff34e3Sthurlow xmlStrcasecmp(value, (xmlChar *)section) == 0) { 35334bff34e3Sthurlow break; 35344bff34e3Sthurlow } 35354bff34e3Sthurlow if (value != NULL) { 35364bff34e3Sthurlow xmlFree(value); 35374bff34e3Sthurlow value = NULL; 35384bff34e3Sthurlow } 35394bff34e3Sthurlow } 35404bff34e3Sthurlow } 35414bff34e3Sthurlow if (value != NULL) 35424bff34e3Sthurlow xmlFree(value); 35439c9af259SGordon Ross if (proto != NULL) 35449c9af259SGordon Ross sa_free_attr_string(proto); 35454bff34e3Sthurlow if (node != NULL && xmlStrcmp(node->name, (xmlChar *)"section") != 0) { 35464bff34e3Sthurlow /* 35474bff34e3Sthurlow * avoid a non option node -- it is possible to be a 35484bff34e3Sthurlow * text node 35494bff34e3Sthurlow */ 35504bff34e3Sthurlow node = NULL; 35514bff34e3Sthurlow } 35524bff34e3Sthurlow return ((sa_protocol_properties_t)node); 35534bff34e3Sthurlow } 35544bff34e3Sthurlow 35554bff34e3Sthurlow /* 35564bff34e3Sthurlow * sa_get_next_protocol_section(prop, find) 35574bff34e3Sthurlow * 35584bff34e3Sthurlow * Get the next protocol specific section in the list. 35594bff34e3Sthurlow */ 35604bff34e3Sthurlow 35614bff34e3Sthurlow sa_property_t 35624bff34e3Sthurlow sa_get_next_protocol_section(sa_property_t prop, char *find) 35634bff34e3Sthurlow { 35644bff34e3Sthurlow xmlNodePtr node; 35654bff34e3Sthurlow xmlChar *value = NULL; 35664bff34e3Sthurlow char *proto; 35674bff34e3Sthurlow 35684bff34e3Sthurlow proto = sa_get_optionset_attr(prop, "type"); 35699c9af259SGordon Ross if ((sa_proto_get_featureset(proto) & SA_FEATURE_HAS_SECTIONS) == 0) { 35709c9af259SGordon Ross if (proto != NULL) 35719c9af259SGordon Ross sa_free_attr_string(proto); 35724bff34e3Sthurlow return ((sa_property_t)NULL); 35739c9af259SGordon Ross } 35744bff34e3Sthurlow 35754bff34e3Sthurlow for (node = ((xmlNodePtr)prop)->next; node != NULL; 35764bff34e3Sthurlow node = node->next) { 35774bff34e3Sthurlow if (xmlStrcmp(node->name, (xmlChar *)"section") == 0) { 35784bff34e3Sthurlow if (find == NULL) 35794bff34e3Sthurlow break; 35804bff34e3Sthurlow value = xmlGetProp(node, (xmlChar *)"name"); 35814bff34e3Sthurlow if (value != NULL && 35824bff34e3Sthurlow xmlStrcasecmp(value, (xmlChar *)find) == 0) { 35834bff34e3Sthurlow break; 35844bff34e3Sthurlow } 35854bff34e3Sthurlow if (value != NULL) { 35864bff34e3Sthurlow xmlFree(value); 35874bff34e3Sthurlow value = NULL; 35884bff34e3Sthurlow } 35894bff34e3Sthurlow 35904bff34e3Sthurlow } 35914bff34e3Sthurlow } 35924bff34e3Sthurlow if (value != NULL) 35934bff34e3Sthurlow xmlFree(value); 35949c9af259SGordon Ross if (proto != NULL) 35959c9af259SGordon Ross sa_free_attr_string(proto); 35964bff34e3Sthurlow return ((sa_property_t)node); 35974bff34e3Sthurlow } 35984bff34e3Sthurlow 35996185db85Sdougm /* 36006185db85Sdougm * sa_get_protocol_property(propset, prop) 36016185db85Sdougm * 36026185db85Sdougm * Get the specified protocol specific property. These are global to 36036185db85Sdougm * the protocol and not specific to a group or share. 36046185db85Sdougm */ 36056185db85Sdougm 36066185db85Sdougm sa_property_t 36076185db85Sdougm sa_get_protocol_property(sa_protocol_properties_t propset, char *prop) 36086185db85Sdougm { 36096185db85Sdougm xmlNodePtr node = (xmlNodePtr)propset; 36106185db85Sdougm xmlChar *value = NULL; 36116185db85Sdougm 36124bff34e3Sthurlow if (propset == NULL) 36134bff34e3Sthurlow return (NULL); 36144bff34e3Sthurlow 36156185db85Sdougm for (node = node->children; node != NULL; 361657b448deSdougm node = node->next) { 361757b448deSdougm if (xmlStrcmp(node->name, (xmlChar *)"option") == 0) { 361857b448deSdougm if (prop == NULL) 361957b448deSdougm break; 362057b448deSdougm value = xmlGetProp(node, (xmlChar *)"type"); 362157b448deSdougm if (value != NULL && 362257b448deSdougm xmlStrcasecmp(value, (xmlChar *)prop) == 0) { 362357b448deSdougm break; 362457b448deSdougm } 362557b448deSdougm if (value != NULL) { 362657b448deSdougm xmlFree(value); 362757b448deSdougm value = NULL; 362857b448deSdougm } 36296185db85Sdougm } 36306185db85Sdougm } 36316185db85Sdougm if (value != NULL) 36326185db85Sdougm xmlFree(value); 36336185db85Sdougm if (node != NULL && xmlStrcmp(node->name, (xmlChar *)"option") != 0) { 363457b448deSdougm /* 363557b448deSdougm * avoid a non option node -- it is possible to be a 363657b448deSdougm * text node 363757b448deSdougm */ 363857b448deSdougm node = NULL; 36396185db85Sdougm } 36406185db85Sdougm return ((sa_property_t)node); 36416185db85Sdougm } 36426185db85Sdougm 36436185db85Sdougm /* 36446185db85Sdougm * sa_get_next_protocol_property(prop) 36456185db85Sdougm * 36466185db85Sdougm * Get the next protocol specific property in the list. 36476185db85Sdougm */ 36486185db85Sdougm 36496185db85Sdougm sa_property_t 36504bff34e3Sthurlow sa_get_next_protocol_property(sa_property_t prop, char *find) 36516185db85Sdougm { 36526185db85Sdougm xmlNodePtr node; 36534bff34e3Sthurlow xmlChar *value = NULL; 36546185db85Sdougm 36556185db85Sdougm for (node = ((xmlNodePtr)prop)->next; node != NULL; 365657b448deSdougm node = node->next) { 36576185db85Sdougm if (xmlStrcmp(node->name, (xmlChar *)"option") == 0) { 36584bff34e3Sthurlow if (find == NULL) 36594bff34e3Sthurlow break; 36604bff34e3Sthurlow value = xmlGetProp(node, (xmlChar *)"type"); 36614bff34e3Sthurlow if (value != NULL && 36624bff34e3Sthurlow xmlStrcasecmp(value, (xmlChar *)find) == 0) { 36634bff34e3Sthurlow break; 36644bff34e3Sthurlow } 36654bff34e3Sthurlow if (value != NULL) { 36664bff34e3Sthurlow xmlFree(value); 36674bff34e3Sthurlow value = NULL; 36684bff34e3Sthurlow } 36694bff34e3Sthurlow 36706185db85Sdougm } 36716185db85Sdougm } 36724bff34e3Sthurlow if (value != NULL) 36734bff34e3Sthurlow xmlFree(value); 36746185db85Sdougm return ((sa_property_t)node); 36756185db85Sdougm } 36766185db85Sdougm 36776185db85Sdougm /* 36786185db85Sdougm * sa_set_protocol_property(prop, value) 36796185db85Sdougm * 36806185db85Sdougm * Set the specified property to have the new value. The protocol 36816185db85Sdougm * specific plugin will then be called to update the property. 36826185db85Sdougm */ 36836185db85Sdougm 36846185db85Sdougm int 36854bff34e3Sthurlow sa_set_protocol_property(sa_property_t prop, char *section, char *value) 36866185db85Sdougm { 36876185db85Sdougm sa_protocol_properties_t propset; 36886185db85Sdougm char *proto; 36896185db85Sdougm int ret = SA_INVALID_PROTOCOL; 36906185db85Sdougm 36916185db85Sdougm propset = ((xmlNodePtr)prop)->parent; 36926185db85Sdougm if (propset != NULL) { 369357b448deSdougm proto = sa_get_optionset_attr(propset, "type"); 369457b448deSdougm if (proto != NULL) { 36954bff34e3Sthurlow if (section != NULL) 36964bff34e3Sthurlow set_node_attr((xmlNodePtr)prop, "section", 36974bff34e3Sthurlow section); 369857b448deSdougm set_node_attr((xmlNodePtr)prop, "value", value); 369957b448deSdougm ret = sa_proto_set_property(proto, prop); 370057b448deSdougm sa_free_attr_string(proto); 370157b448deSdougm } 37026185db85Sdougm } 37036185db85Sdougm return (ret); 37046185db85Sdougm } 37056185db85Sdougm 37066185db85Sdougm /* 37076185db85Sdougm * sa_add_protocol_property(propset, prop) 37086185db85Sdougm * 3709da6c28aaSamw * Add a new property to the protocol specific property set. 37106185db85Sdougm */ 37116185db85Sdougm 37126185db85Sdougm int 37136185db85Sdougm sa_add_protocol_property(sa_protocol_properties_t propset, sa_property_t prop) 37146185db85Sdougm { 37156185db85Sdougm xmlNodePtr node; 37166185db85Sdougm 37176185db85Sdougm /* should check for legitimacy */ 37186185db85Sdougm node = xmlAddChild((xmlNodePtr)propset, (xmlNodePtr)prop); 37196185db85Sdougm if (node != NULL) 372057b448deSdougm return (SA_OK); 37216185db85Sdougm return (SA_NO_MEMORY); 37226185db85Sdougm } 37236185db85Sdougm 37246185db85Sdougm /* 37256185db85Sdougm * sa_create_protocol_properties(proto) 37266185db85Sdougm * 3727da6c28aaSamw * Create a protocol specific property set. 37286185db85Sdougm */ 37296185db85Sdougm 37306185db85Sdougm sa_protocol_properties_t 37316185db85Sdougm sa_create_protocol_properties(char *proto) 37326185db85Sdougm { 37336185db85Sdougm xmlNodePtr node; 373457b448deSdougm 37356185db85Sdougm node = xmlNewNode(NULL, (xmlChar *)"propertyset"); 373657b448deSdougm if (node != NULL) 37374bff34e3Sthurlow (void) xmlSetProp(node, (xmlChar *)"type", (xmlChar *)proto); 37386185db85Sdougm return (node); 37396185db85Sdougm } 3740da6c28aaSamw 3741da6c28aaSamw /* 3742da6c28aaSamw * sa_get_share_resource(share, resource) 3743da6c28aaSamw * 3744da6c28aaSamw * Get the named resource from the share, if it exists. If resource is 3745da6c28aaSamw * NULL, get the first resource. 3746da6c28aaSamw */ 3747da6c28aaSamw 3748da6c28aaSamw sa_resource_t 3749da6c28aaSamw sa_get_share_resource(sa_share_t share, char *resource) 3750da6c28aaSamw { 3751da6c28aaSamw xmlNodePtr node = NULL; 3752da6c28aaSamw xmlChar *name; 3753da6c28aaSamw 3754da6c28aaSamw if (share != NULL) { 3755da6c28aaSamw for (node = ((xmlNodePtr)share)->children; node != NULL; 3756da6c28aaSamw node = node->next) { 3757da6c28aaSamw if (xmlStrcmp(node->name, (xmlChar *)"resource") == 0) { 3758da6c28aaSamw if (resource == NULL) { 3759da6c28aaSamw /* 3760da6c28aaSamw * We are looking for the first 3761da6c28aaSamw * resource node and not a names 3762da6c28aaSamw * resource. 3763da6c28aaSamw */ 3764da6c28aaSamw break; 3765da6c28aaSamw } else { 3766da6c28aaSamw /* is it the correct share? */ 3767da6c28aaSamw name = xmlGetProp(node, 3768da6c28aaSamw (xmlChar *)"name"); 3769da6c28aaSamw if (name != NULL && 3770da6c28aaSamw xmlStrcasecmp(name, 3771da6c28aaSamw (xmlChar *)resource) == 0) { 3772da6c28aaSamw xmlFree(name); 3773da6c28aaSamw break; 3774da6c28aaSamw } 3775da6c28aaSamw xmlFree(name); 3776da6c28aaSamw } 3777da6c28aaSamw } 3778da6c28aaSamw } 3779da6c28aaSamw } 3780da6c28aaSamw return ((sa_resource_t)node); 3781da6c28aaSamw } 3782da6c28aaSamw 3783da6c28aaSamw /* 3784da6c28aaSamw * sa_get_next_resource(resource) 3785da6c28aaSamw * Return the next share following the specified share 3786da6c28aaSamw * from the internal list of shares. Returns NULL if there 3787da6c28aaSamw * are no more shares. The list is relative to the same 3788da6c28aaSamw * group. 3789da6c28aaSamw */ 3790da6c28aaSamw sa_share_t 3791da6c28aaSamw sa_get_next_resource(sa_resource_t resource) 3792da6c28aaSamw { 3793da6c28aaSamw xmlNodePtr node = NULL; 3794da6c28aaSamw 3795da6c28aaSamw if (resource != NULL) { 3796da6c28aaSamw for (node = ((xmlNodePtr)resource)->next; node != NULL; 3797da6c28aaSamw node = node->next) { 3798da6c28aaSamw if (xmlStrcmp(node->name, (xmlChar *)"resource") == 0) 3799da6c28aaSamw break; 3800da6c28aaSamw } 3801da6c28aaSamw } 3802da6c28aaSamw return ((sa_share_t)node); 3803da6c28aaSamw } 3804da6c28aaSamw 3805da6c28aaSamw /* 3806da6c28aaSamw * _sa_get_next_resource_index(share) 3807da6c28aaSamw * 3808da6c28aaSamw * get the next resource index number (one greater then current largest) 3809da6c28aaSamw */ 3810da6c28aaSamw 3811da6c28aaSamw static int 3812da6c28aaSamw _sa_get_next_resource_index(sa_share_t share) 3813da6c28aaSamw { 3814da6c28aaSamw sa_resource_t resource; 3815da6c28aaSamw int index = 0; 3816da6c28aaSamw char *id; 3817da6c28aaSamw 3818da6c28aaSamw for (resource = sa_get_share_resource(share, NULL); 3819da6c28aaSamw resource != NULL; 3820da6c28aaSamw resource = sa_get_next_resource(resource)) { 3821da6c28aaSamw id = get_node_attr((void *)resource, "id"); 3822da6c28aaSamw if (id != NULL) { 3823da6c28aaSamw int val; 3824da6c28aaSamw val = atoi(id); 3825da6c28aaSamw if (val > index) 3826da6c28aaSamw index = val; 3827da6c28aaSamw sa_free_attr_string(id); 3828da6c28aaSamw } 3829da6c28aaSamw } 3830da6c28aaSamw return (index + 1); 3831da6c28aaSamw } 3832da6c28aaSamw 3833da6c28aaSamw 3834da6c28aaSamw /* 3835da6c28aaSamw * sa_add_resource(share, resource, persist, &err) 3836da6c28aaSamw * 3837da6c28aaSamw * Adds a new resource name associated with share. The resource name 3838da6c28aaSamw * must be unique in the system and will be case insensitive (eventually). 3839da6c28aaSamw */ 3840da6c28aaSamw 3841da6c28aaSamw sa_resource_t 3842da6c28aaSamw sa_add_resource(sa_share_t share, char *resource, int persist, int *error) 3843da6c28aaSamw { 3844da6c28aaSamw xmlNodePtr node; 3845da6c28aaSamw int err = SA_OK; 3846da6c28aaSamw sa_resource_t res; 3847da6c28aaSamw sa_group_t group; 3848da6c28aaSamw sa_handle_t handle; 3849da6c28aaSamw char istring[8]; /* just big enough for an integer value */ 3850da6c28aaSamw int index; 3851da6c28aaSamw 3852da6c28aaSamw group = sa_get_parent_group(share); 3853da6c28aaSamw handle = sa_find_group_handle(group); 3854da6c28aaSamw res = sa_find_resource(handle, resource); 3855da6c28aaSamw if (res != NULL) { 3856da6c28aaSamw err = SA_DUPLICATE_NAME; 3857da6c28aaSamw res = NULL; 3858da6c28aaSamw } else { 3859da6c28aaSamw node = xmlNewChild((xmlNodePtr)share, NULL, 3860da6c28aaSamw (xmlChar *)"resource", NULL); 3861da6c28aaSamw if (node != NULL) { 38624bff34e3Sthurlow (void) xmlSetProp(node, (xmlChar *)"name", 3863da6c28aaSamw (xmlChar *)resource); 38644bff34e3Sthurlow (void) xmlSetProp(node, (xmlChar *)"type", persist ? 3865da6c28aaSamw (xmlChar *)"persist" : (xmlChar *)"transient"); 3866da6c28aaSamw if (persist != SA_SHARE_TRANSIENT) { 3867da6c28aaSamw index = _sa_get_next_resource_index(share); 3868da6c28aaSamw (void) snprintf(istring, sizeof (istring), "%d", 3869da6c28aaSamw index); 38704bff34e3Sthurlow (void) xmlSetProp(node, (xmlChar *)"id", 3871da6c28aaSamw (xmlChar *)istring); 38721f713840SDoug McCallum 38731f713840SDoug McCallum if (!sa_is_persistent((sa_group_t)share)) 38741f713840SDoug McCallum goto done; 38751f713840SDoug McCallum 38761f713840SDoug McCallum if (!sa_group_is_zfs(group)) { 3877da6c28aaSamw /* ZFS doesn't use resource names */ 3878da6c28aaSamw sa_handle_impl_t ihandle; 38791f713840SDoug McCallum 3880da6c28aaSamw ihandle = (sa_handle_impl_t) 3881da6c28aaSamw sa_find_group_handle( 3882da6c28aaSamw group); 3883da6c28aaSamw if (ihandle != NULL) 3884da6c28aaSamw err = sa_commit_share( 3885da6c28aaSamw ihandle->scfhandle, group, 3886da6c28aaSamw share); 3887da6c28aaSamw else 3888da6c28aaSamw err = SA_SYSTEM_ERR; 38891f713840SDoug McCallum } else { 38901f713840SDoug McCallum err = sa_zfs_update((sa_share_t)group); 3891da6c28aaSamw } 3892da6c28aaSamw } 3893da6c28aaSamw } 3894da6c28aaSamw } 38951f713840SDoug McCallum done: 3896da6c28aaSamw if (error != NULL) 3897da6c28aaSamw *error = err; 3898da6c28aaSamw return ((sa_resource_t)node); 3899da6c28aaSamw } 3900da6c28aaSamw 3901da6c28aaSamw /* 3902da6c28aaSamw * sa_remove_resource(resource) 3903da6c28aaSamw * 3904da6c28aaSamw * Remove the resource name from the share (and the system) 3905da6c28aaSamw */ 3906da6c28aaSamw 3907da6c28aaSamw int 3908da6c28aaSamw sa_remove_resource(sa_resource_t resource) 3909da6c28aaSamw { 3910da6c28aaSamw sa_share_t share; 3911da6c28aaSamw sa_group_t group; 3912da6c28aaSamw char *type; 3913da6c28aaSamw int ret = SA_OK; 39141f713840SDoug McCallum boolean_t transient = B_FALSE; 391555bf511dSas sa_optionset_t opt; 3916da6c28aaSamw 3917da6c28aaSamw share = sa_get_resource_parent(resource); 3918da6c28aaSamw type = sa_get_share_attr(share, "type"); 3919da6c28aaSamw group = sa_get_parent_group(share); 3920da6c28aaSamw 3921da6c28aaSamw 3922da6c28aaSamw if (type != NULL) { 3923da6c28aaSamw if (strcmp(type, "persist") != 0) 39241f713840SDoug McCallum transient = B_TRUE; 3925da6c28aaSamw sa_free_attr_string(type); 3926da6c28aaSamw } 3927da6c28aaSamw 392855bf511dSas /* Disable the resource for all protocols. */ 392955bf511dSas (void) sa_disable_resource(resource, NULL); 393055bf511dSas 393155bf511dSas /* Remove any optionsets from the resource. */ 393255bf511dSas for (opt = sa_get_optionset(resource, NULL); 393355bf511dSas opt != NULL; 393455bf511dSas opt = sa_get_next_optionset(opt)) 393555bf511dSas (void) sa_destroy_optionset(opt); 393655bf511dSas 3937da6c28aaSamw /* Remove from the share */ 3938da6c28aaSamw xmlUnlinkNode((xmlNode *)resource); 3939da6c28aaSamw xmlFreeNode((xmlNode *)resource); 3940da6c28aaSamw 3941da6c28aaSamw /* only do SMF action if permanent and not ZFS */ 39421f713840SDoug McCallum if (transient) 39431f713840SDoug McCallum return (ret); 39441f713840SDoug McCallum 39451f713840SDoug McCallum if (!sa_group_is_zfs(group)) { 3946da6c28aaSamw sa_handle_impl_t ihandle; 3947da6c28aaSamw ihandle = (sa_handle_impl_t)sa_find_group_handle(group); 3948da6c28aaSamw if (ihandle != NULL) 3949da6c28aaSamw ret = sa_commit_share(ihandle->scfhandle, group, share); 3950da6c28aaSamw else 3951da6c28aaSamw ret = SA_SYSTEM_ERR; 39521f713840SDoug McCallum } else { 39531f713840SDoug McCallum ret = sa_zfs_update((sa_share_t)group); 3954da6c28aaSamw } 3955743a77edSAlan Wright 3956da6c28aaSamw return (ret); 3957da6c28aaSamw } 3958da6c28aaSamw 3959da6c28aaSamw /* 3960743a77edSAlan Wright * proto_rename_resource(handle, group, resource, newname) 3961da6c28aaSamw * 3962da6c28aaSamw * Helper function for sa_rename_resource that notifies the protocol 3963da6c28aaSamw * of a resource name change prior to a config repository update. 3964da6c28aaSamw */ 3965da6c28aaSamw static int 3966da6c28aaSamw proto_rename_resource(sa_handle_t handle, sa_group_t group, 3967da6c28aaSamw sa_resource_t resource, char *newname) 3968da6c28aaSamw { 3969da6c28aaSamw sa_optionset_t optionset; 3970da6c28aaSamw int ret = SA_OK; 3971da6c28aaSamw int err; 3972da6c28aaSamw 3973da6c28aaSamw for (optionset = sa_get_optionset(group, NULL); 3974da6c28aaSamw optionset != NULL; 3975da6c28aaSamw optionset = sa_get_next_optionset(optionset)) { 3976da6c28aaSamw char *type; 3977da6c28aaSamw type = sa_get_optionset_attr(optionset, "type"); 3978da6c28aaSamw if (type != NULL) { 3979da6c28aaSamw err = sa_proto_rename_resource(handle, type, resource, 3980da6c28aaSamw newname); 3981da6c28aaSamw if (err != SA_OK) 3982da6c28aaSamw ret = err; 3983da6c28aaSamw sa_free_attr_string(type); 3984da6c28aaSamw } 3985da6c28aaSamw } 3986da6c28aaSamw return (ret); 3987da6c28aaSamw } 3988da6c28aaSamw 3989da6c28aaSamw /* 3990da6c28aaSamw * sa_rename_resource(resource, newname) 3991da6c28aaSamw * 3992da6c28aaSamw * Rename the resource to the new name, if it is unique. 3993da6c28aaSamw */ 3994da6c28aaSamw 3995da6c28aaSamw int 3996da6c28aaSamw sa_rename_resource(sa_resource_t resource, char *newname) 3997da6c28aaSamw { 3998da6c28aaSamw sa_share_t share; 3999da6c28aaSamw sa_group_t group = NULL; 4000da6c28aaSamw sa_resource_t target; 4001da6c28aaSamw int ret = SA_CONFIG_ERR; 4002da6c28aaSamw sa_handle_t handle = NULL; 4003da6c28aaSamw 4004da6c28aaSamw share = sa_get_resource_parent(resource); 4005da6c28aaSamw if (share == NULL) 4006da6c28aaSamw return (ret); 4007da6c28aaSamw 4008da6c28aaSamw group = sa_get_parent_group(share); 4009da6c28aaSamw if (group == NULL) 4010da6c28aaSamw return (ret); 4011da6c28aaSamw 4012da6c28aaSamw handle = (sa_handle_impl_t)sa_find_group_handle(group); 4013da6c28aaSamw if (handle == NULL) 4014da6c28aaSamw return (ret); 4015da6c28aaSamw 4016da6c28aaSamw target = sa_find_resource(handle, newname); 4017da6c28aaSamw if (target != NULL) { 4018da6c28aaSamw ret = SA_DUPLICATE_NAME; 4019da6c28aaSamw } else { 4020da6c28aaSamw /* 4021da6c28aaSamw * Everything appears to be valid at this 4022da6c28aaSamw * point. Change the name of the active share and then 4023da6c28aaSamw * update the share in the appropriate repository. 4024da6c28aaSamw */ 4025da6c28aaSamw ret = proto_rename_resource(handle, group, resource, newname); 4026da6c28aaSamw set_node_attr(resource, "name", newname); 40271f713840SDoug McCallum 40281f713840SDoug McCallum if (!sa_is_persistent((sa_group_t)share)) 40291f713840SDoug McCallum return (ret); 40301f713840SDoug McCallum 40311f713840SDoug McCallum if (!sa_group_is_zfs(group)) { 4032da6c28aaSamw sa_handle_impl_t ihandle = (sa_handle_impl_t)handle; 4033da6c28aaSamw ret = sa_commit_share(ihandle->scfhandle, group, 4034da6c28aaSamw share); 40351f713840SDoug McCallum } else { 40361f713840SDoug McCallum ret = sa_zfs_update((sa_share_t)group); 4037da6c28aaSamw } 4038da6c28aaSamw } 4039da6c28aaSamw return (ret); 4040da6c28aaSamw } 4041da6c28aaSamw 4042da6c28aaSamw /* 4043da6c28aaSamw * sa_get_resource_attr(resource, tag) 4044da6c28aaSamw * 4045da6c28aaSamw * Get the named attribute of the resource. "name" and "id" are 4046da6c28aaSamw * currently defined. NULL if tag not defined. 4047da6c28aaSamw */ 4048da6c28aaSamw 4049da6c28aaSamw char * 4050da6c28aaSamw sa_get_resource_attr(sa_resource_t resource, char *tag) 4051da6c28aaSamw { 4052da6c28aaSamw return (get_node_attr((void *)resource, tag)); 4053da6c28aaSamw } 4054da6c28aaSamw 4055da6c28aaSamw /* 4056da6c28aaSamw * sa_set_resource_attr(resource, tag, value) 4057da6c28aaSamw * 4058da6c28aaSamw * Get the named attribute of the resource. "name" and "id" are 4059da6c28aaSamw * currently defined. NULL if tag not defined. Currently we don't do 4060da6c28aaSamw * much, but additional checking may be needed in the future. 4061da6c28aaSamw */ 4062da6c28aaSamw 4063da6c28aaSamw int 4064da6c28aaSamw sa_set_resource_attr(sa_resource_t resource, char *tag, char *value) 4065da6c28aaSamw { 4066da6c28aaSamw set_node_attr((void *)resource, tag, value); 4067da6c28aaSamw return (SA_OK); 4068da6c28aaSamw } 4069da6c28aaSamw 4070da6c28aaSamw /* 4071da6c28aaSamw * sa_get_resource_parent(resource_t) 4072da6c28aaSamw * 4073da6c28aaSamw * Returns the share associated with the resource. 4074da6c28aaSamw */ 4075da6c28aaSamw 4076da6c28aaSamw sa_share_t 4077da6c28aaSamw sa_get_resource_parent(sa_resource_t resource) 4078da6c28aaSamw { 4079da6c28aaSamw sa_share_t share = NULL; 4080da6c28aaSamw 4081da6c28aaSamw if (resource != NULL) 4082da6c28aaSamw share = (sa_share_t)((xmlNodePtr)resource)->parent; 4083da6c28aaSamw return (share); 4084da6c28aaSamw } 4085da6c28aaSamw 4086da6c28aaSamw /* 4087da6c28aaSamw * find_resource(group, name) 4088da6c28aaSamw * 4089da6c28aaSamw * Find the resource within the group. 4090da6c28aaSamw */ 4091da6c28aaSamw 4092da6c28aaSamw static sa_resource_t 4093da6c28aaSamw find_resource(sa_group_t group, char *resname) 4094da6c28aaSamw { 4095da6c28aaSamw sa_share_t share; 4096da6c28aaSamw sa_resource_t resource = NULL; 4097da6c28aaSamw char *name; 4098da6c28aaSamw 4099da6c28aaSamw /* Iterate over all the shares and resources in the group. */ 4100da6c28aaSamw for (share = sa_get_share(group, NULL); 4101da6c28aaSamw share != NULL && resource == NULL; 4102da6c28aaSamw share = sa_get_next_share(share)) { 4103da6c28aaSamw for (resource = sa_get_share_resource(share, NULL); 4104da6c28aaSamw resource != NULL; 4105da6c28aaSamw resource = sa_get_next_resource(resource)) { 4106da6c28aaSamw name = sa_get_resource_attr(resource, "name"); 4107da6c28aaSamw if (name != NULL && xmlStrcasecmp((xmlChar*)name, 4108da6c28aaSamw (xmlChar*)resname) == 0) { 4109da6c28aaSamw sa_free_attr_string(name); 4110da6c28aaSamw break; 4111da6c28aaSamw } 4112da6c28aaSamw if (name != NULL) { 4113da6c28aaSamw sa_free_attr_string(name); 4114da6c28aaSamw } 4115da6c28aaSamw } 4116da6c28aaSamw } 4117da6c28aaSamw return (resource); 4118da6c28aaSamw } 4119da6c28aaSamw 4120da6c28aaSamw /* 4121da6c28aaSamw * sa_find_resource(name) 4122da6c28aaSamw * 4123da6c28aaSamw * Find the named resource in the system. 4124da6c28aaSamw */ 4125da6c28aaSamw 4126da6c28aaSamw sa_resource_t 4127da6c28aaSamw sa_find_resource(sa_handle_t handle, char *name) 4128da6c28aaSamw { 4129da6c28aaSamw sa_group_t group; 4130da6c28aaSamw sa_group_t zgroup; 4131da6c28aaSamw sa_resource_t resource = NULL; 4132da6c28aaSamw 4133da6c28aaSamw /* 4134da6c28aaSamw * Iterate over all groups and zfs subgroups and check for 4135da6c28aaSamw * resource name in them. 4136da6c28aaSamw */ 4137da6c28aaSamw for (group = sa_get_group(handle, NULL); group != NULL; 4138da6c28aaSamw group = sa_get_next_group(group)) { 4139da6c28aaSamw 4140da6c28aaSamw if (is_zfs_group(group)) { 4141da6c28aaSamw for (zgroup = 4142da6c28aaSamw (sa_group_t)_sa_get_child_node((xmlNodePtr)group, 4143da6c28aaSamw (xmlChar *)"group"); 4144da6c28aaSamw zgroup != NULL && resource == NULL; 4145da6c28aaSamw zgroup = sa_get_next_group(zgroup)) { 4146da6c28aaSamw resource = find_resource(zgroup, name); 4147da6c28aaSamw } 4148da6c28aaSamw } else { 4149da6c28aaSamw resource = find_resource(group, name); 4150da6c28aaSamw } 4151da6c28aaSamw if (resource != NULL) 4152da6c28aaSamw break; 4153da6c28aaSamw } 4154da6c28aaSamw return (resource); 4155da6c28aaSamw } 4156da6c28aaSamw 4157da6c28aaSamw /* 4158da6c28aaSamw * sa_get_resource(group, resource) 4159da6c28aaSamw * 4160da6c28aaSamw * Search all the shares in the specified group for a share with a 4161da6c28aaSamw * resource name matching the one specified. 4162da6c28aaSamw * 4163da6c28aaSamw * In the future, it may be advantageous to allow group to be NULL and 4164da6c28aaSamw * search all groups but that isn't needed at present. 4165da6c28aaSamw */ 4166da6c28aaSamw 4167da6c28aaSamw sa_resource_t 4168da6c28aaSamw sa_get_resource(sa_group_t group, char *resource) 4169da6c28aaSamw { 4170da6c28aaSamw sa_share_t share = NULL; 4171da6c28aaSamw sa_resource_t res = NULL; 4172da6c28aaSamw 4173da6c28aaSamw if (resource != NULL) { 4174da6c28aaSamw for (share = sa_get_share(group, NULL); 4175da6c28aaSamw share != NULL && res == NULL; 4176da6c28aaSamw share = sa_get_next_share(share)) { 4177da6c28aaSamw res = sa_get_share_resource(share, resource); 4178da6c28aaSamw } 4179da6c28aaSamw } 4180da6c28aaSamw return (res); 4181da6c28aaSamw } 4182da6c28aaSamw 4183f85463f2Sdougm /* 4184f85463f2Sdougm * get_protocol_list(optionset, object) 4185f85463f2Sdougm * 4186f85463f2Sdougm * Get the protocol optionset list for the object and add them as 4187f85463f2Sdougm * properties to optionset. 4188f85463f2Sdougm */ 4189f85463f2Sdougm static int 4190f85463f2Sdougm get_protocol_list(sa_optionset_t optionset, void *object) 4191f85463f2Sdougm { 4192f85463f2Sdougm sa_property_t prop; 4193f85463f2Sdougm sa_optionset_t opts; 4194f85463f2Sdougm int ret = SA_OK; 4195f85463f2Sdougm 4196f85463f2Sdougm for (opts = sa_get_optionset(object, NULL); 4197f85463f2Sdougm opts != NULL; 4198f85463f2Sdougm opts = sa_get_next_optionset(opts)) { 4199f85463f2Sdougm char *type; 4200f85463f2Sdougm type = sa_get_optionset_attr(opts, "type"); 4201f85463f2Sdougm /* 4202f85463f2Sdougm * It is possible to have a non-protocol optionset. We 4203f85463f2Sdougm * skip any of those found. 4204f85463f2Sdougm */ 4205f85463f2Sdougm if (type == NULL) 4206f85463f2Sdougm continue; 4207f85463f2Sdougm prop = sa_create_property(type, "true"); 4208f85463f2Sdougm sa_free_attr_string(type); 4209f85463f2Sdougm if (prop != NULL) 4210f85463f2Sdougm prop = (sa_property_t)xmlAddChild((xmlNodePtr)optionset, 4211f85463f2Sdougm (xmlNodePtr)prop); 4212f85463f2Sdougm /* If prop is NULL, don't bother continuing */ 4213f85463f2Sdougm if (prop == NULL) { 4214f85463f2Sdougm ret = SA_NO_MEMORY; 4215f85463f2Sdougm break; 4216f85463f2Sdougm } 4217f85463f2Sdougm } 4218f85463f2Sdougm return (ret); 4219f85463f2Sdougm } 4220f85463f2Sdougm 4221f85463f2Sdougm /* 4222f85463f2Sdougm * sa_free_protoset(optionset) 4223f85463f2Sdougm * 4224f85463f2Sdougm * Free the protocol property optionset. 4225f85463f2Sdougm */ 4226f85463f2Sdougm static void 4227f85463f2Sdougm sa_free_protoset(sa_optionset_t optionset) 4228f85463f2Sdougm { 4229f85463f2Sdougm if (optionset != NULL) { 4230f85463f2Sdougm xmlUnlinkNode((xmlNodePtr) optionset); 4231f85463f2Sdougm xmlFreeNode((xmlNodePtr) optionset); 4232f85463f2Sdougm } 4233f85463f2Sdougm } 4234f85463f2Sdougm 4235f85463f2Sdougm /* 4236f85463f2Sdougm * sa_optionset_t sa_get_active_protocols(object) 4237f85463f2Sdougm * 4238f85463f2Sdougm * Return a list of the protocols that are active for the object. 4239f85463f2Sdougm * This is currently an internal helper function, but could be 4240f85463f2Sdougm * made visible if there is enough demand for it. 4241f85463f2Sdougm * 4242f85463f2Sdougm * The function finds the parent group and extracts the protocol 4243f85463f2Sdougm * optionsets creating a new optionset with the protocols as properties. 4244f85463f2Sdougm * 4245f85463f2Sdougm * The caller must free the returned optionset. 4246f85463f2Sdougm */ 4247f85463f2Sdougm 4248f85463f2Sdougm static sa_optionset_t 4249f85463f2Sdougm sa_get_active_protocols(void *object) 4250f85463f2Sdougm { 4251f85463f2Sdougm sa_optionset_t options; 4252f85463f2Sdougm sa_share_t share = NULL; 4253f85463f2Sdougm sa_group_t group = NULL; 4254f85463f2Sdougm sa_resource_t resource = NULL; 4255f85463f2Sdougm int ret = SA_OK; 4256f85463f2Sdougm 4257f85463f2Sdougm if (object == NULL) 4258f85463f2Sdougm return (NULL); 4259f85463f2Sdougm options = (sa_optionset_t)xmlNewNode(NULL, (xmlChar *)"optionset"); 4260f85463f2Sdougm if (options == NULL) 4261f85463f2Sdougm return (NULL); 4262f85463f2Sdougm 4263f85463f2Sdougm /* 4264f85463f2Sdougm * Find the objects up the tree that might have protocols 4265f85463f2Sdougm * enabled on them. 4266f85463f2Sdougm */ 4267f85463f2Sdougm if (sa_is_resource(object)) { 4268f85463f2Sdougm resource = (sa_resource_t)object; 4269f85463f2Sdougm share = sa_get_resource_parent(resource); 4270f85463f2Sdougm group = sa_get_parent_group(share); 4271f85463f2Sdougm } else if (sa_is_share(object)) { 4272f85463f2Sdougm share = (sa_share_t)object; 4273f85463f2Sdougm group = sa_get_parent_group(share); 4274f85463f2Sdougm } else { 4275f85463f2Sdougm group = (sa_group_t)group; 4276f85463f2Sdougm } 4277f85463f2Sdougm if (resource != NULL) 4278f85463f2Sdougm ret = get_protocol_list(options, resource); 4279f85463f2Sdougm if (ret == SA_OK && share != NULL) 4280f85463f2Sdougm ret = get_protocol_list(options, share); 4281f85463f2Sdougm if (ret == SA_OK && group != NULL) 4282f85463f2Sdougm ret = get_protocol_list(options, group); 4283f85463f2Sdougm 4284f85463f2Sdougm /* 4285f85463f2Sdougm * If there was an error, we won't have a complete list so 4286f85463f2Sdougm * abandon everything. The caller will have to deal with the 4287f85463f2Sdougm * issue. 4288f85463f2Sdougm */ 4289f85463f2Sdougm if (ret != SA_OK) { 4290f85463f2Sdougm sa_free_protoset(options); 4291f85463f2Sdougm options = NULL; 4292f85463f2Sdougm } 4293f85463f2Sdougm return (options); 4294f85463f2Sdougm } 4295f85463f2Sdougm 4296da6c28aaSamw /* 4297da6c28aaSamw * sa_enable_resource, protocol) 4298da6c28aaSamw * Disable the specified share to the specified protocol. 4299da6c28aaSamw * If protocol is NULL, then all protocols. 4300da6c28aaSamw */ 4301da6c28aaSamw int 4302da6c28aaSamw sa_enable_resource(sa_resource_t resource, char *protocol) 4303da6c28aaSamw { 4304da6c28aaSamw int ret = SA_OK; 4305da6c28aaSamw 4306da6c28aaSamw if (protocol != NULL) { 4307da6c28aaSamw ret = sa_proto_share_resource(protocol, resource); 4308da6c28aaSamw } else { 4309f85463f2Sdougm sa_optionset_t protoset; 4310f85463f2Sdougm sa_property_t prop; 4311f85463f2Sdougm char *proto; 4312f85463f2Sdougm int err; 4313f85463f2Sdougm 4314da6c28aaSamw /* need to do all protocols */ 4315f85463f2Sdougm protoset = sa_get_active_protocols(resource); 4316f85463f2Sdougm if (protoset == NULL) 4317f85463f2Sdougm return (SA_NO_MEMORY); 4318f85463f2Sdougm for (prop = sa_get_property(protoset, NULL); 4319f85463f2Sdougm prop != NULL; 4320f85463f2Sdougm prop = sa_get_next_property(prop)) { 4321f85463f2Sdougm proto = sa_get_property_attr(prop, "type"); 4322f85463f2Sdougm if (proto == NULL) { 4323f85463f2Sdougm ret = SA_NO_MEMORY; 4324f85463f2Sdougm continue; 4325da6c28aaSamw } 4326f85463f2Sdougm err = sa_proto_share_resource(proto, resource); 4327f85463f2Sdougm if (err != SA_OK) 4328f85463f2Sdougm ret = err; 4329f85463f2Sdougm sa_free_attr_string(proto); 4330da6c28aaSamw } 4331f85463f2Sdougm sa_free_protoset(protoset); 4332da6c28aaSamw } 4333da6c28aaSamw if (ret == SA_OK) 4334da6c28aaSamw (void) sa_set_resource_attr(resource, "shared", NULL); 4335da6c28aaSamw 4336da6c28aaSamw return (ret); 4337da6c28aaSamw } 4338da6c28aaSamw 4339da6c28aaSamw /* 4340da6c28aaSamw * sa_disable_resource(resource, protocol) 4341da6c28aaSamw * 4342da6c28aaSamw * Disable the specified share for the specified protocol. If 4343da6c28aaSamw * protocol is NULL, then all protocols. If the underlying 4344da6c28aaSamw * protocol doesn't implement disable at the resource level, we 4345da6c28aaSamw * disable at the share level. 4346da6c28aaSamw */ 4347da6c28aaSamw int 4348da6c28aaSamw sa_disable_resource(sa_resource_t resource, char *protocol) 4349da6c28aaSamw { 4350da6c28aaSamw int ret = SA_OK; 4351da6c28aaSamw 4352da6c28aaSamw if (protocol != NULL) { 4353da6c28aaSamw ret = sa_proto_unshare_resource(protocol, resource); 4354da6c28aaSamw if (ret == SA_NOT_IMPLEMENTED) { 4355da6c28aaSamw sa_share_t parent; 4356da6c28aaSamw /* 4357da6c28aaSamw * The protocol doesn't implement unshare 4358da6c28aaSamw * resource. That implies that resource names are 4359da6c28aaSamw * simple aliases for this protocol so we need to 4360da6c28aaSamw * unshare the share. 4361da6c28aaSamw */ 4362da6c28aaSamw parent = sa_get_resource_parent(resource); 4363da6c28aaSamw if (parent != NULL) 4364da6c28aaSamw ret = sa_disable_share(parent, protocol); 4365da6c28aaSamw else 4366da6c28aaSamw ret = SA_CONFIG_ERR; 4367da6c28aaSamw } 4368da6c28aaSamw } else { 4369f85463f2Sdougm sa_optionset_t protoset; 4370f85463f2Sdougm sa_property_t prop; 4371f85463f2Sdougm char *proto; 4372f85463f2Sdougm int err; 4373f85463f2Sdougm 4374da6c28aaSamw /* need to do all protocols */ 4375f85463f2Sdougm protoset = sa_get_active_protocols(resource); 4376f85463f2Sdougm if (protoset == NULL) 4377f85463f2Sdougm return (SA_NO_MEMORY); 4378f85463f2Sdougm for (prop = sa_get_property(protoset, NULL); 4379f85463f2Sdougm prop != NULL; 4380f85463f2Sdougm prop = sa_get_next_property(prop)) { 4381f85463f2Sdougm proto = sa_get_property_attr(prop, "type"); 4382f85463f2Sdougm if (proto == NULL) { 4383f85463f2Sdougm ret = SA_NO_MEMORY; 4384f85463f2Sdougm continue; 4385da6c28aaSamw } 4386f85463f2Sdougm err = sa_proto_unshare_resource(proto, resource); 4387f85463f2Sdougm if (err == SA_NOT_SUPPORTED) { 4388f85463f2Sdougm sa_share_t parent; 4389f85463f2Sdougm parent = sa_get_resource_parent(resource); 4390f85463f2Sdougm if (parent != NULL) 4391f85463f2Sdougm err = sa_disable_share(parent, proto); 4392f85463f2Sdougm else 4393f85463f2Sdougm err = SA_CONFIG_ERR; 4394f85463f2Sdougm } 4395f85463f2Sdougm if (err != SA_OK) 4396f85463f2Sdougm ret = err; 4397f85463f2Sdougm sa_free_attr_string(proto); 4398da6c28aaSamw } 4399f85463f2Sdougm sa_free_protoset(protoset); 4400da6c28aaSamw } 4401da6c28aaSamw if (ret == SA_OK) 4402da6c28aaSamw (void) sa_set_resource_attr(resource, "shared", NULL); 4403da6c28aaSamw 4404da6c28aaSamw return (ret); 4405da6c28aaSamw } 4406da6c28aaSamw 4407da6c28aaSamw /* 4408da6c28aaSamw * sa_set_resource_description(resource, content) 4409da6c28aaSamw * 4410da6c28aaSamw * Set the description of share to content. 4411da6c28aaSamw */ 4412da6c28aaSamw 4413da6c28aaSamw int 4414da6c28aaSamw sa_set_resource_description(sa_resource_t resource, char *content) 4415da6c28aaSamw { 4416da6c28aaSamw xmlNodePtr node; 4417da6c28aaSamw sa_group_t group; 4418da6c28aaSamw sa_share_t share; 4419da6c28aaSamw int ret = SA_OK; 4420da6c28aaSamw 4421da6c28aaSamw for (node = ((xmlNodePtr)resource)->children; 4422da6c28aaSamw node != NULL; 4423da6c28aaSamw node = node->next) { 4424da6c28aaSamw if (xmlStrcmp(node->name, (xmlChar *)"description") == 0) { 4425da6c28aaSamw break; 4426da6c28aaSamw } 4427da6c28aaSamw } 4428da6c28aaSamw 4429da6c28aaSamw /* no existing description but want to add */ 4430da6c28aaSamw if (node == NULL && content != NULL) { 4431da6c28aaSamw /* add a description */ 4432da6c28aaSamw node = _sa_set_share_description(resource, content); 4433da6c28aaSamw } else if (node != NULL && content != NULL) { 4434da6c28aaSamw /* update a description */ 4435da6c28aaSamw xmlNodeSetContent(node, (xmlChar *)content); 4436da6c28aaSamw } else if (node != NULL && content == NULL) { 4437da6c28aaSamw /* remove an existing description */ 4438da6c28aaSamw xmlUnlinkNode(node); 4439da6c28aaSamw xmlFreeNode(node); 4440da6c28aaSamw } 4441148c5f43SAlan Wright 4442da6c28aaSamw share = sa_get_resource_parent(resource); 4443da6c28aaSamw group = sa_get_parent_group(share); 4444148c5f43SAlan Wright if (group != NULL && 4445148c5f43SAlan Wright sa_is_persistent(share) && (!sa_group_is_zfs(group))) { 4446da6c28aaSamw sa_handle_impl_t impl_handle; 4447da6c28aaSamw impl_handle = (sa_handle_impl_t)sa_find_group_handle(group); 4448da6c28aaSamw if (impl_handle != NULL) 4449da6c28aaSamw ret = sa_commit_share(impl_handle->scfhandle, 4450da6c28aaSamw group, share); 4451da6c28aaSamw else 4452da6c28aaSamw ret = SA_SYSTEM_ERR; 4453da6c28aaSamw } 4454da6c28aaSamw return (ret); 4455da6c28aaSamw } 4456da6c28aaSamw 4457da6c28aaSamw /* 4458da6c28aaSamw * sa_get_resource_description(share) 4459da6c28aaSamw * 4460da6c28aaSamw * Return the description text for the specified share if it 4461da6c28aaSamw * exists. NULL if no description exists. 4462da6c28aaSamw */ 4463da6c28aaSamw 4464da6c28aaSamw char * 4465da6c28aaSamw sa_get_resource_description(sa_resource_t resource) 4466da6c28aaSamw { 4467da6c28aaSamw xmlChar *description = NULL; 4468da6c28aaSamw xmlNodePtr node; 4469da6c28aaSamw 4470da6c28aaSamw for (node = ((xmlNodePtr)resource)->children; node != NULL; 4471da6c28aaSamw node = node->next) { 4472da6c28aaSamw if (xmlStrcmp(node->name, (xmlChar *)"description") == 0) 4473da6c28aaSamw break; 4474da6c28aaSamw } 4475da6c28aaSamw if (node != NULL) { 4476da6c28aaSamw description = xmlNodeGetContent(node); 4477da6c28aaSamw fixproblemchars((char *)description); 4478da6c28aaSamw } 4479da6c28aaSamw return ((char *)description); 4480da6c28aaSamw } 4481