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 /* 23dc20a302Sas200622 * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 246185db85Sdougm * Use is subject to license terms. 256185db85Sdougm */ 266185db85Sdougm 276185db85Sdougm #pragma ident "%Z%%M% %I% %E% SMI" 286185db85Sdougm 296185db85Sdougm /* 306185db85Sdougm * Share control API 316185db85Sdougm */ 326185db85Sdougm #include <stdio.h> 336185db85Sdougm #include <string.h> 346185db85Sdougm #include <ctype.h> 356185db85Sdougm #include <sys/types.h> 366185db85Sdougm #include <sys/stat.h> 37a99982a7Sdougm #include <fcntl.h> 386185db85Sdougm #include <unistd.h> 396185db85Sdougm #include <libxml/parser.h> 406185db85Sdougm #include <libxml/tree.h> 416185db85Sdougm #include "libshare.h" 426185db85Sdougm #include "libshare_impl.h" 436185db85Sdougm #include <libscf.h> 446185db85Sdougm #include "scfutil.h" 456185db85Sdougm #include <ctype.h> 466185db85Sdougm #include <libintl.h> 47549ec3ffSdougm #include <thread.h> 48549ec3ffSdougm #include <synch.h> 496185db85Sdougm 50a99982a7Sdougm #define DFS_LOCK_FILE "/etc/dfs/fstypes" 5157b448deSdougm #define SA_STRSIZE 256 /* max string size for names */ 52a99982a7Sdougm 536185db85Sdougm /* 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 63da6c28aaSamw /* 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 *); 726185db85Sdougm extern char *sa_fstype(char *); 736185db85Sdougm extern int sa_is_share(void *); 74da6c28aaSamw extern int sa_is_resource(void *); 756185db85Sdougm extern ssize_t scf_max_name_len; /* defined in scfutil during initialization */ 766185db85Sdougm extern int sa_group_is_zfs(sa_group_t); 776185db85Sdougm extern int sa_path_is_zfs(char *); 786185db85Sdougm extern int sa_zfs_set_sharenfs(sa_group_t, char *, int); 79da6c28aaSamw extern int sa_zfs_set_sharesmb(sa_group_t, char *, int); 80549ec3ffSdougm extern void update_legacy_config(sa_handle_t); 816185db85Sdougm extern int issubdir(char *, char *); 8257b448deSdougm extern int sa_zfs_init(sa_handle_impl_t); 83549ec3ffSdougm extern void sa_zfs_fini(sa_handle_impl_t); 84a99982a7Sdougm extern void sablocksigs(sigset_t *); 85a99982a7Sdougm extern void saunblocksigs(sigset_t *); 86da6c28aaSamw static sa_group_t sa_get_optionset_parent(sa_optionset_t); 87da6c28aaSamw static char *get_node_attr(void *, char *); 885b6e0c46Sdougm extern void sa_update_sharetab_ts(sa_handle_t); 896185db85Sdougm 90549ec3ffSdougm /* 91549ec3ffSdougm * Data structures for finding/managing the document root to access 92549ec3ffSdougm * handle mapping. The list isn't expected to grow very large so a 93549ec3ffSdougm * simple list is acceptable. The purpose is to provide a way to start 94549ec3ffSdougm * with a group or share and find the library handle needed for 95549ec3ffSdougm * various operations. 96549ec3ffSdougm */ 97549ec3ffSdougm mutex_t sa_global_lock; 98549ec3ffSdougm struct doc2handle { 99549ec3ffSdougm struct doc2handle *next; 100549ec3ffSdougm xmlNodePtr root; 101549ec3ffSdougm sa_handle_impl_t handle; 102549ec3ffSdougm }; 103549ec3ffSdougm 10457b448deSdougm /* definitions used in a couple of property functions */ 10557b448deSdougm #define SA_PROP_OP_REMOVE 1 10657b448deSdougm #define SA_PROP_OP_ADD 2 10757b448deSdougm #define SA_PROP_OP_UPDATE 3 10857b448deSdougm 109549ec3ffSdougm static struct doc2handle *sa_global_handles = NULL; 1106185db85Sdougm 1116185db85Sdougm /* helper functions */ 1126185db85Sdougm 113549ec3ffSdougm /* 114549ec3ffSdougm * sa_errorstr(err) 115549ec3ffSdougm * 116549ec3ffSdougm * convert an error value to an error string 117549ec3ffSdougm */ 118549ec3ffSdougm 1196185db85Sdougm char * 1206185db85Sdougm sa_errorstr(int err) 1216185db85Sdougm { 1226185db85Sdougm static char errstr[32]; 1236185db85Sdougm char *ret = NULL; 1246185db85Sdougm 1256185db85Sdougm switch (err) { 1266185db85Sdougm case SA_OK: 12724424a35Sdougm ret = dgettext(TEXT_DOMAIN, "ok"); 1286185db85Sdougm break; 1296185db85Sdougm case SA_NO_SUCH_PATH: 13024424a35Sdougm ret = dgettext(TEXT_DOMAIN, "path doesn't exist"); 1316185db85Sdougm break; 1326185db85Sdougm case SA_NO_MEMORY: 13324424a35Sdougm ret = dgettext(TEXT_DOMAIN, "no memory"); 1346185db85Sdougm break; 1356185db85Sdougm case SA_DUPLICATE_NAME: 13624424a35Sdougm ret = dgettext(TEXT_DOMAIN, "name in use"); 1376185db85Sdougm break; 1386185db85Sdougm case SA_BAD_PATH: 13924424a35Sdougm ret = dgettext(TEXT_DOMAIN, "bad path"); 1406185db85Sdougm break; 1416185db85Sdougm case SA_NO_SUCH_GROUP: 14224424a35Sdougm ret = dgettext(TEXT_DOMAIN, "no such group"); 1436185db85Sdougm break; 1446185db85Sdougm case SA_CONFIG_ERR: 14524424a35Sdougm ret = dgettext(TEXT_DOMAIN, "configuration error"); 1466185db85Sdougm break; 1476185db85Sdougm case SA_SYSTEM_ERR: 14824424a35Sdougm ret = dgettext(TEXT_DOMAIN, "system error"); 1496185db85Sdougm break; 1506185db85Sdougm case SA_SYNTAX_ERR: 15124424a35Sdougm ret = dgettext(TEXT_DOMAIN, "syntax error"); 1526185db85Sdougm break; 1536185db85Sdougm case SA_NO_PERMISSION: 15424424a35Sdougm ret = dgettext(TEXT_DOMAIN, "no permission"); 1556185db85Sdougm break; 1566185db85Sdougm case SA_BUSY: 15724424a35Sdougm ret = dgettext(TEXT_DOMAIN, "busy"); 1586185db85Sdougm break; 1596185db85Sdougm case SA_NO_SUCH_PROP: 16024424a35Sdougm ret = dgettext(TEXT_DOMAIN, "no such property"); 1616185db85Sdougm break; 1626185db85Sdougm case SA_INVALID_NAME: 16324424a35Sdougm ret = dgettext(TEXT_DOMAIN, "invalid name"); 1646185db85Sdougm break; 1656185db85Sdougm case SA_INVALID_PROTOCOL: 16624424a35Sdougm ret = dgettext(TEXT_DOMAIN, "invalid protocol"); 1676185db85Sdougm break; 1686185db85Sdougm case SA_NOT_ALLOWED: 16924424a35Sdougm ret = dgettext(TEXT_DOMAIN, "operation not allowed"); 1706185db85Sdougm break; 1716185db85Sdougm case SA_BAD_VALUE: 17224424a35Sdougm ret = dgettext(TEXT_DOMAIN, "bad property value"); 1736185db85Sdougm break; 1746185db85Sdougm case SA_INVALID_SECURITY: 17524424a35Sdougm ret = dgettext(TEXT_DOMAIN, "invalid security type"); 1766185db85Sdougm break; 1776185db85Sdougm case SA_NO_SUCH_SECURITY: 17824424a35Sdougm ret = dgettext(TEXT_DOMAIN, "security type not found"); 1796185db85Sdougm break; 1806185db85Sdougm case SA_VALUE_CONFLICT: 18124424a35Sdougm ret = dgettext(TEXT_DOMAIN, "property value conflict"); 1826185db85Sdougm break; 1836185db85Sdougm case SA_NOT_IMPLEMENTED: 18424424a35Sdougm ret = dgettext(TEXT_DOMAIN, "not implemented"); 1856185db85Sdougm break; 1866185db85Sdougm case SA_INVALID_PATH: 18724424a35Sdougm ret = dgettext(TEXT_DOMAIN, "invalid path"); 1886185db85Sdougm break; 1896185db85Sdougm case SA_NOT_SUPPORTED: 19024424a35Sdougm ret = dgettext(TEXT_DOMAIN, "operation not supported"); 1916185db85Sdougm break; 1926185db85Sdougm case SA_PROP_SHARE_ONLY: 19324424a35Sdougm ret = dgettext(TEXT_DOMAIN, "property not valid for group"); 1946185db85Sdougm break; 1956185db85Sdougm case SA_NOT_SHARED: 19624424a35Sdougm ret = dgettext(TEXT_DOMAIN, "not shared"); 1976185db85Sdougm break; 198da6c28aaSamw case SA_NO_SUCH_RESOURCE: 199da6c28aaSamw ret = dgettext(TEXT_DOMAIN, "no such resource"); 200da6c28aaSamw break; 201da6c28aaSamw case SA_RESOURCE_REQUIRED: 202da6c28aaSamw ret = dgettext(TEXT_DOMAIN, "resource name required"); 203da6c28aaSamw break; 204da6c28aaSamw case SA_MULTIPLE_ERROR: 205da6c28aaSamw ret = dgettext(TEXT_DOMAIN, "errors from multiple protocols"); 206da6c28aaSamw break; 207da6c28aaSamw case SA_PATH_IS_SUBDIR: 208da6c28aaSamw ret = dgettext(TEXT_DOMAIN, "path is a subpath of share"); 209da6c28aaSamw break; 210da6c28aaSamw case SA_PATH_IS_PARENTDIR: 211da6c28aaSamw ret = dgettext(TEXT_DOMAIN, "path is parent of a share"); 212da6c28aaSamw break; 2134bff34e3Sthurlow case SA_NO_SECTION: 2144bff34e3Sthurlow ret = dgettext(TEXT_DOMAIN, "protocol requires a section"); 2154bff34e3Sthurlow break; 2164bff34e3Sthurlow case SA_NO_PROPERTIES: 2174bff34e3Sthurlow ret = dgettext(TEXT_DOMAIN, "properties not found"); 2184bff34e3Sthurlow break; 2194bff34e3Sthurlow case SA_NO_SUCH_SECTION: 2204bff34e3Sthurlow ret = dgettext(TEXT_DOMAIN, "section not found"); 2214bff34e3Sthurlow break; 2224bff34e3Sthurlow case SA_PASSWORD_ENC: 2234bff34e3Sthurlow ret = dgettext(TEXT_DOMAIN, "passwords must be encrypted"); 2244bff34e3Sthurlow break; 2256185db85Sdougm default: 2266185db85Sdougm (void) snprintf(errstr, sizeof (errstr), 22724424a35Sdougm dgettext(TEXT_DOMAIN, "unknown %d"), err); 2286185db85Sdougm ret = errstr; 2296185db85Sdougm } 2306185db85Sdougm return (ret); 2316185db85Sdougm } 2326185db85Sdougm 2336185db85Sdougm /* 234549ec3ffSdougm * Document root to active handle mapping functions. These are only 235549ec3ffSdougm * used internally. A mutex is used to prevent access while the list 236549ec3ffSdougm * is changing. In general, the list will be relatively short - one 237549ec3ffSdougm * item per thread that has called sa_init(). 238549ec3ffSdougm */ 239549ec3ffSdougm 240549ec3ffSdougm sa_handle_impl_t 241549ec3ffSdougm get_handle_for_root(xmlNodePtr root) 242549ec3ffSdougm { 243549ec3ffSdougm struct doc2handle *item; 244549ec3ffSdougm 245549ec3ffSdougm (void) mutex_lock(&sa_global_lock); 246549ec3ffSdougm for (item = sa_global_handles; item != NULL; item = item->next) { 247549ec3ffSdougm if (item->root == root) 248549ec3ffSdougm break; 249549ec3ffSdougm } 250549ec3ffSdougm (void) mutex_unlock(&sa_global_lock); 251549ec3ffSdougm if (item != NULL) 252549ec3ffSdougm return (item->handle); 253549ec3ffSdougm return (NULL); 254549ec3ffSdougm } 255549ec3ffSdougm 256549ec3ffSdougm static int 257549ec3ffSdougm add_handle_for_root(xmlNodePtr root, sa_handle_impl_t handle) 258549ec3ffSdougm { 259549ec3ffSdougm struct doc2handle *item; 260549ec3ffSdougm int ret = SA_NO_MEMORY; 261549ec3ffSdougm 262549ec3ffSdougm item = (struct doc2handle *)calloc(sizeof (struct doc2handle), 1); 263549ec3ffSdougm if (item != NULL) { 264549ec3ffSdougm item->root = root; 265549ec3ffSdougm item->handle = handle; 266549ec3ffSdougm (void) mutex_lock(&sa_global_lock); 267549ec3ffSdougm item->next = sa_global_handles; 268549ec3ffSdougm sa_global_handles = item; 269549ec3ffSdougm (void) mutex_unlock(&sa_global_lock); 270549ec3ffSdougm ret = SA_OK; 271549ec3ffSdougm } 272549ec3ffSdougm return (ret); 273549ec3ffSdougm } 274549ec3ffSdougm 275549ec3ffSdougm /* 276549ec3ffSdougm * remove_handle_for_root(root) 277549ec3ffSdougm * 278549ec3ffSdougm * Walks the list of handles and removes the one for this "root" from 279549ec3ffSdougm * the list. It is up to the caller to free the data. 280549ec3ffSdougm */ 281549ec3ffSdougm 282549ec3ffSdougm static void 283549ec3ffSdougm remove_handle_for_root(xmlNodePtr root) 284549ec3ffSdougm { 285549ec3ffSdougm struct doc2handle *item, *prev; 286549ec3ffSdougm 287549ec3ffSdougm (void) mutex_lock(&sa_global_lock); 288549ec3ffSdougm for (prev = NULL, item = sa_global_handles; item != NULL; 289549ec3ffSdougm item = item->next) { 290549ec3ffSdougm if (item->root == root) { 291549ec3ffSdougm /* first in the list */ 29257b448deSdougm if (prev == NULL) 293549ec3ffSdougm sa_global_handles = sa_global_handles->next; 29457b448deSdougm else 295549ec3ffSdougm prev->next = item->next; 296549ec3ffSdougm /* Item is out of the list so free the list structure */ 297549ec3ffSdougm free(item); 298549ec3ffSdougm break; 299549ec3ffSdougm } 300549ec3ffSdougm prev = item; 301549ec3ffSdougm } 302549ec3ffSdougm (void) mutex_unlock(&sa_global_lock); 303549ec3ffSdougm } 304549ec3ffSdougm 305549ec3ffSdougm /* 306549ec3ffSdougm * sa_find_group_handle(sa_group_t group) 307549ec3ffSdougm * 308549ec3ffSdougm * Find the sa_handle_t for the configuration associated with this 309549ec3ffSdougm * group. 310549ec3ffSdougm */ 311549ec3ffSdougm sa_handle_t 312549ec3ffSdougm sa_find_group_handle(sa_group_t group) 313549ec3ffSdougm { 314549ec3ffSdougm xmlNodePtr node = (xmlNodePtr)group; 315549ec3ffSdougm sa_handle_t handle; 316549ec3ffSdougm 317549ec3ffSdougm while (node != NULL) { 318549ec3ffSdougm if (strcmp((char *)(node->name), "sharecfg") == 0) { 319549ec3ffSdougm /* have the root so get the handle */ 320549ec3ffSdougm handle = (sa_handle_t)get_handle_for_root(node); 321549ec3ffSdougm return (handle); 322549ec3ffSdougm } 323549ec3ffSdougm node = node->parent; 324549ec3ffSdougm } 325549ec3ffSdougm return (NULL); 326549ec3ffSdougm } 327549ec3ffSdougm 328549ec3ffSdougm /* 3296185db85Sdougm * set_legacy_timestamp(root, path, timevalue) 3306185db85Sdougm * 3316185db85Sdougm * add the current timestamp value to the configuration for use in 3326185db85Sdougm * determining when to update the legacy files. For SMF, this 3336185db85Sdougm * property is kept in default/operation/legacy_timestamp 3346185db85Sdougm */ 3356185db85Sdougm 3366185db85Sdougm static void 3376185db85Sdougm set_legacy_timestamp(xmlNodePtr root, char *path, uint64_t tval) 3386185db85Sdougm { 3396185db85Sdougm xmlNodePtr node; 3406185db85Sdougm xmlChar *lpath = NULL; 341549ec3ffSdougm sa_handle_impl_t handle; 342549ec3ffSdougm 343549ec3ffSdougm /* Have to have a handle or else we weren't initialized. */ 344549ec3ffSdougm handle = get_handle_for_root(root); 345549ec3ffSdougm if (handle == NULL) 346549ec3ffSdougm return; 3476185db85Sdougm 3486185db85Sdougm for (node = root->xmlChildrenNode; node != NULL; 3496185db85Sdougm node = node->next) { 3506185db85Sdougm if (xmlStrcmp(node->name, (xmlChar *)"legacy") == 0) { 3516185db85Sdougm /* a possible legacy node for this path */ 3526185db85Sdougm lpath = xmlGetProp(node, (xmlChar *)"path"); 35357b448deSdougm if (lpath != NULL && 35457b448deSdougm xmlStrcmp(lpath, (xmlChar *)path) == 0) { 3556185db85Sdougm xmlFree(lpath); 3566185db85Sdougm break; 3576185db85Sdougm } 3586185db85Sdougm if (lpath != NULL) 3596185db85Sdougm xmlFree(lpath); 3606185db85Sdougm } 3616185db85Sdougm } 3626185db85Sdougm if (node == NULL) { 3636185db85Sdougm /* need to create the first legacy timestamp node */ 3646185db85Sdougm node = xmlNewChild(root, NULL, (xmlChar *)"legacy", NULL); 3656185db85Sdougm } 3666185db85Sdougm if (node != NULL) { 3676185db85Sdougm char tstring[32]; 3686185db85Sdougm int ret; 3696185db85Sdougm 3706185db85Sdougm (void) snprintf(tstring, sizeof (tstring), "%lld", tval); 3714bff34e3Sthurlow (void) xmlSetProp(node, (xmlChar *)"timestamp", 3724bff34e3Sthurlow (xmlChar *)tstring); 3734bff34e3Sthurlow (void) xmlSetProp(node, (xmlChar *)"path", (xmlChar *)path); 3746185db85Sdougm /* now commit to SMF */ 375549ec3ffSdougm ret = sa_get_instance(handle->scfhandle, "default"); 3766185db85Sdougm if (ret == SA_OK) { 37757b448deSdougm ret = sa_start_transaction(handle->scfhandle, 37857b448deSdougm "operation"); 3796185db85Sdougm if (ret == SA_OK) { 38057b448deSdougm ret = sa_set_property(handle->scfhandle, 38157b448deSdougm "legacy-timestamp", tstring); 3826185db85Sdougm if (ret == SA_OK) { 38357b448deSdougm (void) sa_end_transaction( 3845b6e0c46Sdougm handle->scfhandle, handle); 3856185db85Sdougm } else { 386549ec3ffSdougm sa_abort_transaction(handle->scfhandle); 3876185db85Sdougm } 3886185db85Sdougm } 3896185db85Sdougm } 3906185db85Sdougm } 3916185db85Sdougm } 3926185db85Sdougm 3936185db85Sdougm /* 3946185db85Sdougm * is_shared(share) 3956185db85Sdougm * 3966185db85Sdougm * determine if the specified share is currently shared or not. 3976185db85Sdougm */ 3986185db85Sdougm static int 3996185db85Sdougm is_shared(sa_share_t share) 4006185db85Sdougm { 4016185db85Sdougm char *shared; 4026185db85Sdougm int result = 0; /* assume not */ 4036185db85Sdougm 4046185db85Sdougm shared = sa_get_share_attr(share, "shared"); 4056185db85Sdougm if (shared != NULL) { 4066185db85Sdougm if (strcmp(shared, "true") == 0) 4076185db85Sdougm result = 1; 4086185db85Sdougm sa_free_attr_string(shared); 4096185db85Sdougm } 4106185db85Sdougm return (result); 4116185db85Sdougm } 4126185db85Sdougm 4136185db85Sdougm /* 414da6c28aaSamw * excluded_protocol(share, proto) 415da6c28aaSamw * 416da6c28aaSamw * Returns B_TRUE if the specified protocol appears in the "exclude" 417da6c28aaSamw * property. This is used to prevent sharing special case shares 418da6c28aaSamw * (e.g. subdirs when SMB wants a subdir and NFS doesn't. B_FALSE is 419da6c28aaSamw * returned if the protocol isn't in the list. 420da6c28aaSamw */ 421da6c28aaSamw static boolean_t 422da6c28aaSamw excluded_protocol(sa_share_t share, char *proto) 423da6c28aaSamw { 424da6c28aaSamw char *protolist; 425da6c28aaSamw char *str; 426da6c28aaSamw char *token; 427da6c28aaSamw 428da6c28aaSamw protolist = sa_get_share_attr(share, "exclude"); 429da6c28aaSamw if (protolist != NULL) { 430da6c28aaSamw str = protolist; 431da6c28aaSamw while ((token = strtok(str, ",")) != NULL) { 432da6c28aaSamw if (strcmp(token, proto) == 0) { 433da6c28aaSamw sa_free_attr_string(protolist); 434da6c28aaSamw return (B_TRUE); 435da6c28aaSamw } 436da6c28aaSamw str = NULL; 437da6c28aaSamw } 438da6c28aaSamw sa_free_attr_string(protolist); 439da6c28aaSamw } 440da6c28aaSamw return (B_FALSE); 441da6c28aaSamw } 442da6c28aaSamw 443da6c28aaSamw /* 444a99982a7Sdougm * checksubdirgroup(group, newpath, strictness) 445f345c0beSdougm * 446a99982a7Sdougm * check all the specified newpath against all the paths in the 447a99982a7Sdougm * group. This is a helper function for checksubdir to make it easier 448a99982a7Sdougm * to also check ZFS subgroups. 449a99982a7Sdougm * The strictness values mean: 450f345c0beSdougm * SA_CHECK_NORMAL == only check newpath against shares that are active 451f345c0beSdougm * SA_CHECK_STRICT == check newpath against both active shares and those 452f345c0beSdougm * stored in the repository 4536185db85Sdougm */ 4546185db85Sdougm static int 455a99982a7Sdougm checksubdirgroup(sa_group_t group, char *newpath, int strictness) 4566185db85Sdougm { 4576185db85Sdougm sa_share_t share; 458a99982a7Sdougm char *path; 459a99982a7Sdougm int issub = SA_OK; 460da6c28aaSamw int subdir; 461da6c28aaSamw int parent; 462da6c28aaSamw 463da6c28aaSamw if (newpath == NULL) 464da6c28aaSamw return (SA_INVALID_PATH); 4656185db85Sdougm 4666185db85Sdougm for (share = sa_get_share(group, NULL); share != NULL; 4676185db85Sdougm share = sa_get_next_share(share)) { 4686185db85Sdougm /* 4696185db85Sdougm * The original behavior of share never checked 4706185db85Sdougm * against the permanent configuration 4716185db85Sdougm * (/etc/dfs/dfstab). PIT has a number of cases where 4726185db85Sdougm * it depends on this older behavior even though it 4736185db85Sdougm * could be considered incorrect. We may tighten this 4746185db85Sdougm * up in the future. 4756185db85Sdougm */ 476f345c0beSdougm if (strictness == SA_CHECK_NORMAL && !is_shared(share)) 4776185db85Sdougm continue; 4786185db85Sdougm 4796185db85Sdougm path = sa_get_share_attr(share, "path"); 480f345c0beSdougm /* 481f345c0beSdougm * If path is NULL, then a share is in the process of 482f345c0beSdougm * construction or someone has modified the property 483a99982a7Sdougm * group inappropriately. It should be 484a99982a7Sdougm * ignored. issubdir() comes from the original share 485a99982a7Sdougm * implementation and does the difficult part of 486a99982a7Sdougm * checking subdirectories. 487f345c0beSdougm */ 488f345c0beSdougm if (path == NULL) 489f345c0beSdougm continue; 490da6c28aaSamw 491da6c28aaSamw if (strcmp(path, newpath) == 0) { 492da6c28aaSamw issub = SA_INVALID_PATH; 493da6c28aaSamw } else { 494da6c28aaSamw subdir = issubdir(newpath, path); 495da6c28aaSamw parent = issubdir(path, newpath); 496da6c28aaSamw if (subdir || parent) { 4976185db85Sdougm sa_free_attr_string(path); 4986185db85Sdougm path = NULL; 499da6c28aaSamw return (subdir ? 500da6c28aaSamw SA_PATH_IS_SUBDIR : SA_PATH_IS_PARENTDIR); 501da6c28aaSamw } 5026185db85Sdougm } 5036185db85Sdougm sa_free_attr_string(path); 5046185db85Sdougm path = NULL; 5056185db85Sdougm } 506a99982a7Sdougm return (issub); 507a99982a7Sdougm } 508a99982a7Sdougm 509a99982a7Sdougm /* 510a99982a7Sdougm * checksubdir(newpath, strictness) 511a99982a7Sdougm * 512a99982a7Sdougm * checksubdir determines if the specified path (newpath) is a 513a99982a7Sdougm * subdirectory of another share. It calls checksubdirgroup() to do 514a99982a7Sdougm * the complicated work. The strictness parameter determines how 515a99982a7Sdougm * strict a check to make against the path. The strictness values 516a99982a7Sdougm * mean: SA_CHECK_NORMAL == only check newpath against shares that are 517a99982a7Sdougm * active SA_CHECK_STRICT == check newpath against both active shares 518a99982a7Sdougm * and those * stored in the repository 519a99982a7Sdougm */ 520a99982a7Sdougm static int 521549ec3ffSdougm checksubdir(sa_handle_t handle, char *newpath, int strictness) 522a99982a7Sdougm { 523a99982a7Sdougm sa_group_t group; 524da6c28aaSamw int issub = SA_OK; 525a99982a7Sdougm char *path = NULL; 526a99982a7Sdougm 527da6c28aaSamw for (group = sa_get_group(handle, NULL); 528da6c28aaSamw group != NULL && issub == SA_OK; 529da6c28aaSamw group = sa_get_next_group(group)) { 530a99982a7Sdougm if (sa_group_is_zfs(group)) { 531a99982a7Sdougm sa_group_t subgroup; 532a99982a7Sdougm for (subgroup = sa_get_sub_group(group); 533da6c28aaSamw subgroup != NULL && issub == SA_OK; 534a99982a7Sdougm subgroup = sa_get_next_group(subgroup)) 53557b448deSdougm issub = checksubdirgroup(subgroup, newpath, 53657b448deSdougm strictness); 537a99982a7Sdougm } else { 538a99982a7Sdougm issub = checksubdirgroup(group, newpath, strictness); 539a99982a7Sdougm } 5406185db85Sdougm } 5416185db85Sdougm if (path != NULL) 5426185db85Sdougm sa_free_attr_string(path); 5436185db85Sdougm return (issub); 5446185db85Sdougm } 5456185db85Sdougm 5466185db85Sdougm /* 547f345c0beSdougm * validpath(path, strictness) 5486185db85Sdougm * determine if the provided path is valid for a share. It shouldn't 5496185db85Sdougm * be a sub-dir of an already shared path or the parent directory of a 5506185db85Sdougm * share path. 5516185db85Sdougm */ 5526185db85Sdougm static int 553549ec3ffSdougm validpath(sa_handle_t handle, char *path, int strictness) 5546185db85Sdougm { 5556185db85Sdougm int error = SA_OK; 5566185db85Sdougm struct stat st; 5576185db85Sdougm sa_share_t share; 5586185db85Sdougm char *fstype; 5596185db85Sdougm 56057b448deSdougm if (*path != '/') 5616185db85Sdougm return (SA_BAD_PATH); 56257b448deSdougm 5636185db85Sdougm if (stat(path, &st) < 0) { 5646185db85Sdougm error = SA_NO_SUCH_PATH; 5656185db85Sdougm } else { 566549ec3ffSdougm share = sa_find_share(handle, path); 56757b448deSdougm if (share != NULL) 5686185db85Sdougm error = SA_DUPLICATE_NAME; 56957b448deSdougm 5706185db85Sdougm if (error == SA_OK) { 5716185db85Sdougm /* 57257b448deSdougm * check for special case with file system 57357b448deSdougm * that might have restrictions. For now, ZFS 57457b448deSdougm * is the only case since it has its own idea 57557b448deSdougm * of how to configure shares. We do this 57657b448deSdougm * before subdir checking since things like 57757b448deSdougm * ZFS will do that for us. This should also 57857b448deSdougm * be done via plugin interface. 5796185db85Sdougm */ 5806185db85Sdougm fstype = sa_fstype(path); 5816185db85Sdougm if (fstype != NULL && strcmp(fstype, "zfs") == 0) { 582549ec3ffSdougm if (sa_zfs_is_shared(handle, path)) 583a99982a7Sdougm error = SA_INVALID_NAME; 5846185db85Sdougm } 5856185db85Sdougm if (fstype != NULL) 5866185db85Sdougm sa_free_fstype(fstype); 5876185db85Sdougm } 58857b448deSdougm if (error == SA_OK) 589549ec3ffSdougm error = checksubdir(handle, path, strictness); 5906185db85Sdougm } 5916185db85Sdougm return (error); 5926185db85Sdougm } 5936185db85Sdougm 5946185db85Sdougm /* 5956185db85Sdougm * check to see if group/share is persistent. 596da6c28aaSamw * 597da6c28aaSamw * "group" can be either an sa_group_t or an sa_share_t. (void *) 598da6c28aaSamw * works since both thse types are also void *. 5996185db85Sdougm */ 600da6c28aaSamw int 601da6c28aaSamw sa_is_persistent(void *group) 6026185db85Sdougm { 6036185db85Sdougm char *type; 6046185db85Sdougm int persist = 1; 6056185db85Sdougm 606da6c28aaSamw type = sa_get_group_attr((sa_group_t)group, "type"); 6076185db85Sdougm if (type != NULL && strcmp(type, "transient") == 0) 6086185db85Sdougm persist = 0; 6096185db85Sdougm if (type != NULL) 6106185db85Sdougm sa_free_attr_string(type); 6116185db85Sdougm return (persist); 6126185db85Sdougm } 6136185db85Sdougm 6146185db85Sdougm /* 6156185db85Sdougm * sa_valid_group_name(name) 6166185db85Sdougm * 6176185db85Sdougm * check that the "name" contains only valid characters and otherwise 6186185db85Sdougm * fits the required naming conventions. Valid names must start with 6196185db85Sdougm * an alphabetic and the remainder may consist of only alphanumeric 6206185db85Sdougm * plus the '-' and '_' characters. This name limitation comes from 6216185db85Sdougm * inherent limitations in SMF. 6226185db85Sdougm */ 6236185db85Sdougm 6246185db85Sdougm int 6256185db85Sdougm sa_valid_group_name(char *name) 6266185db85Sdougm { 6276185db85Sdougm int ret = 1; 6286185db85Sdougm ssize_t len; 6296185db85Sdougm 6306185db85Sdougm if (name != NULL && isalpha(*name)) { 6316185db85Sdougm char c; 6326185db85Sdougm len = strlen(name); 6336185db85Sdougm if (len < (scf_max_name_len - sizeof ("group:"))) { 6346185db85Sdougm for (c = *name++; c != '\0' && ret != 0; c = *name++) { 6356185db85Sdougm if (!isalnum(c) && c != '-' && c != '_') 6366185db85Sdougm ret = 0; 6376185db85Sdougm } 6386185db85Sdougm } else { 6396185db85Sdougm ret = 0; 6406185db85Sdougm } 6416185db85Sdougm } else { 6426185db85Sdougm ret = 0; 6436185db85Sdougm } 6446185db85Sdougm return (ret); 6456185db85Sdougm } 6466185db85Sdougm 6476185db85Sdougm 6486185db85Sdougm /* 6496185db85Sdougm * is_zfs_group(group) 6506185db85Sdougm * Determine if the specified group is a ZFS sharenfs group 6516185db85Sdougm */ 6526185db85Sdougm static int 6536185db85Sdougm is_zfs_group(sa_group_t group) 6546185db85Sdougm { 6556185db85Sdougm int ret = 0; 6566185db85Sdougm xmlNodePtr parent; 6576185db85Sdougm xmlChar *zfs; 6586185db85Sdougm 65957b448deSdougm if (strcmp((char *)((xmlNodePtr)group)->name, "share") == 0) 6606185db85Sdougm parent = (xmlNodePtr)sa_get_parent_group(group); 66157b448deSdougm else 6626185db85Sdougm parent = (xmlNodePtr)group; 6636185db85Sdougm zfs = xmlGetProp(parent, (xmlChar *)"zfs"); 6646185db85Sdougm if (zfs != NULL) { 6656185db85Sdougm xmlFree(zfs); 6666185db85Sdougm ret = 1; 6676185db85Sdougm } 6686185db85Sdougm return (ret); 6696185db85Sdougm } 6706185db85Sdougm 6716185db85Sdougm /* 672da6c28aaSamw * sa_get_object_type(object) 673da6c28aaSamw * 674da6c28aaSamw * This function returns a numeric value representing the object 675da6c28aaSamw * type. This allows using simpler checks when doing type specific 676da6c28aaSamw * operations. 677da6c28aaSamw */ 678da6c28aaSamw 679da6c28aaSamw static int 680da6c28aaSamw sa_get_object_type(void *object) 681da6c28aaSamw { 682da6c28aaSamw xmlNodePtr node = (xmlNodePtr)object; 683da6c28aaSamw int type; 684da6c28aaSamw 685da6c28aaSamw if (xmlStrcmp(node->name, (xmlChar *)"group") == 0) 686da6c28aaSamw type = SA_TYPE_GROUP; 687da6c28aaSamw else if (xmlStrcmp(node->name, (xmlChar *)"share") == 0) 688da6c28aaSamw type = SA_TYPE_SHARE; 689da6c28aaSamw else if (xmlStrcmp(node->name, (xmlChar *)"resource") == 0) 690da6c28aaSamw type = SA_TYPE_RESOURCE; 691da6c28aaSamw else if (xmlStrcmp(node->name, (xmlChar *)"optionset") == 0) 692da6c28aaSamw type = SA_TYPE_OPTIONSET; 693da6c28aaSamw else if (xmlStrcmp(node->name, (xmlChar *)"security") == 0) 694da6c28aaSamw type = SA_TYPE_ALTSPACE; 695da6c28aaSamw else 696da6c28aaSamw assert(0); 697da6c28aaSamw return (type); 698da6c28aaSamw } 699da6c28aaSamw 700da6c28aaSamw /* 7016185db85Sdougm * sa_optionset_name(optionset, oname, len, id) 7026185db85Sdougm * return the SMF name for the optionset. If id is not NULL, it 7036185db85Sdougm * will have the GUID value for a share and should be used 7046185db85Sdougm * instead of the keyword "optionset" which is used for 7056185db85Sdougm * groups. If the optionset doesn't have a protocol type 7066185db85Sdougm * associated with it, "default" is used. This shouldn't happen 7076185db85Sdougm * at this point but may be desirable in the future if there are 7086185db85Sdougm * protocol independent properties added. The name is returned in 7096185db85Sdougm * oname. 7106185db85Sdougm */ 7116185db85Sdougm 7126185db85Sdougm static int 7136185db85Sdougm sa_optionset_name(sa_optionset_t optionset, char *oname, size_t len, char *id) 7146185db85Sdougm { 7156185db85Sdougm char *proto; 716da6c28aaSamw void *parent; 717da6c28aaSamw int ptype; 7186185db85Sdougm 7196185db85Sdougm if (id == NULL) 7206185db85Sdougm id = "optionset"; 7216185db85Sdougm 722da6c28aaSamw parent = sa_get_optionset_parent(optionset); 723da6c28aaSamw if (parent != NULL) { 724da6c28aaSamw ptype = sa_get_object_type(parent); 7256185db85Sdougm proto = sa_get_optionset_attr(optionset, "type"); 726da6c28aaSamw if (ptype != SA_TYPE_RESOURCE) { 727da6c28aaSamw len = snprintf(oname, len, "%s_%s", id, 728da6c28aaSamw proto ? proto : "default"); 729da6c28aaSamw } else { 730da6c28aaSamw char *index; 731da6c28aaSamw index = get_node_attr((void *)parent, "id"); 732da6c28aaSamw if (index != NULL) 733da6c28aaSamw len = snprintf(oname, len, "%s_%s_%s", id, 734da6c28aaSamw proto ? proto : "default", index); 735da6c28aaSamw else 736da6c28aaSamw len = 0; 737da6c28aaSamw } 7386185db85Sdougm 7396185db85Sdougm if (proto != NULL) 7406185db85Sdougm sa_free_attr_string(proto); 741da6c28aaSamw } else { 742da6c28aaSamw len = 0; 743da6c28aaSamw } 7446185db85Sdougm return (len); 7456185db85Sdougm } 7466185db85Sdougm 7476185db85Sdougm /* 7486185db85Sdougm * sa_security_name(optionset, oname, len, id) 7496185db85Sdougm * 7506185db85Sdougm * return the SMF name for the security. If id is not NULL, it will 7516185db85Sdougm * have the GUID value for a share and should be used instead of the 7526185db85Sdougm * keyword "optionset" which is used for groups. If the optionset 7536185db85Sdougm * doesn't have a protocol type associated with it, "default" is 7546185db85Sdougm * used. This shouldn't happen at this point but may be desirable in 7556185db85Sdougm * the future if there are protocol independent properties added. The 7566185db85Sdougm * name is returned in oname. The security type is also encoded into 7576185db85Sdougm * the name. In the future, this wil *be handled a bit differently. 7586185db85Sdougm */ 7596185db85Sdougm 7606185db85Sdougm static int 7616185db85Sdougm sa_security_name(sa_security_t security, char *oname, size_t len, char *id) 7626185db85Sdougm { 7636185db85Sdougm char *proto; 7646185db85Sdougm char *sectype; 7656185db85Sdougm 7666185db85Sdougm if (id == NULL) 7676185db85Sdougm id = "optionset"; 7686185db85Sdougm 7696185db85Sdougm proto = sa_get_security_attr(security, "type"); 7706185db85Sdougm sectype = sa_get_security_attr(security, "sectype"); 77157b448deSdougm len = snprintf(oname, len, "%s_%s_%s", id, proto ? proto : "default", 7726185db85Sdougm sectype ? sectype : "default"); 7736185db85Sdougm if (proto != NULL) 7746185db85Sdougm sa_free_attr_string(proto); 7756185db85Sdougm if (sectype != NULL) 7766185db85Sdougm sa_free_attr_string(sectype); 7776185db85Sdougm return (len); 7786185db85Sdougm } 7796185db85Sdougm 7806185db85Sdougm /* 78157b448deSdougm * verifydefgroupopts(handle) 78257b448deSdougm * 78357b448deSdougm * Make sure a "default" group exists and has default protocols enabled. 78457b448deSdougm */ 78557b448deSdougm static void 78657b448deSdougm verifydefgroupopts(sa_handle_t handle) 78757b448deSdougm { 78857b448deSdougm sa_group_t defgrp; 78957b448deSdougm sa_optionset_t opt; 790da6c28aaSamw 79157b448deSdougm defgrp = sa_get_group(handle, "default"); 79257b448deSdougm if (defgrp != NULL) { 79357b448deSdougm opt = sa_get_optionset(defgrp, NULL); 79457b448deSdougm /* 79557b448deSdougm * NFS is the default for default group 79657b448deSdougm */ 79757b448deSdougm if (opt == NULL) 79857b448deSdougm opt = sa_create_optionset(defgrp, "nfs"); 79957b448deSdougm } 80057b448deSdougm } 80157b448deSdougm 80257b448deSdougm /* 803f345c0beSdougm * sa_init(init_service) 8046185db85Sdougm * Initialize the API 8056185db85Sdougm * find all the shared objects 8066185db85Sdougm * init the tables with all objects 8076185db85Sdougm * read in the current configuration 8086185db85Sdougm */ 8096185db85Sdougm 81057b448deSdougm #define GETPROP(prop) scf_simple_prop_next_astring(prop) 81157b448deSdougm #define CHECKTSTAMP(st, tval) stat(SA_LEGACY_DFSTAB, &st) >= 0 && \ 81257b448deSdougm tval != TSTAMP(st.st_ctim) 81357b448deSdougm 814549ec3ffSdougm sa_handle_t 8156185db85Sdougm sa_init(int init_service) 8166185db85Sdougm { 8176185db85Sdougm struct stat st; 8186185db85Sdougm int legacy = 0; 8196185db85Sdougm uint64_t tval = 0; 820a99982a7Sdougm int lockfd; 821a99982a7Sdougm sigset_t old; 822a99982a7Sdougm int updatelegacy = B_FALSE; 823a99982a7Sdougm scf_simple_prop_t *prop; 824549ec3ffSdougm sa_handle_impl_t handle; 825549ec3ffSdougm int err; 8266185db85Sdougm 827549ec3ffSdougm handle = calloc(sizeof (struct sa_handle_impl), 1); 828549ec3ffSdougm 829549ec3ffSdougm if (handle != NULL) { 830*c5f58477Sdougm /* 831*c5f58477Sdougm * Get protocol specific structures, but only if this 832*c5f58477Sdougm * is the only handle. 833*c5f58477Sdougm */ 834*c5f58477Sdougm (void) mutex_lock(&sa_global_lock); 835*c5f58477Sdougm if (sa_global_handles == NULL) 8366185db85Sdougm (void) proto_plugin_init(); 837*c5f58477Sdougm (void) mutex_unlock(&sa_global_lock); 8386185db85Sdougm if (init_service & SA_INIT_SHARE_API) { 8396185db85Sdougm /* 84057b448deSdougm * initialize access into libzfs. We use this 84157b448deSdougm * when collecting info about ZFS datasets and 84257b448deSdougm * shares. 8431cea05afSdougm */ 84457b448deSdougm if (sa_zfs_init(handle) == B_FALSE) { 84557b448deSdougm free(handle); 84657b448deSdougm (void) proto_plugin_fini(); 84757b448deSdougm return (NULL); 84857b448deSdougm } 8491cea05afSdougm /* 8506185db85Sdougm * since we want to use SMF, initialize an svc handle 8516185db85Sdougm * and find out what is there. 8526185db85Sdougm */ 853549ec3ffSdougm handle->scfhandle = sa_scf_init(handle); 854549ec3ffSdougm if (handle->scfhandle != NULL) { 855a99982a7Sdougm /* 856a99982a7Sdougm * Need to lock the extraction of the 857a99982a7Sdougm * configuration if the dfstab file has 858a99982a7Sdougm * changed. Lock everything now and release if 859a99982a7Sdougm * not needed. Use a file that isn't being 860a99982a7Sdougm * manipulated by other parts of the system in 861a99982a7Sdougm * order to not interfere with locking. Using 862a99982a7Sdougm * dfstab doesn't work. 863a99982a7Sdougm */ 864a99982a7Sdougm sablocksigs(&old); 865a99982a7Sdougm lockfd = open(DFS_LOCK_FILE, O_RDWR); 866a99982a7Sdougm if (lockfd >= 0) { 867a99982a7Sdougm extern int errno; 868a99982a7Sdougm errno = 0; 869a99982a7Sdougm (void) lockf(lockfd, F_LOCK, 0); 870a99982a7Sdougm /* 87157b448deSdougm * Check whether we are going to need 87257b448deSdougm * to merge any dfstab changes. This 87357b448deSdougm * is done by comparing the value of 87457b448deSdougm * legacy-timestamp with the current 87557b448deSdougm * st_ctim of the file. If they are 87657b448deSdougm * different, an update is needed and 87757b448deSdougm * the file must remain locked until 87857b448deSdougm * the merge is done in order to 87957b448deSdougm * prevent multiple startups from 88057b448deSdougm * changing the SMF repository at the 88157b448deSdougm * same time. The first to get the 88257b448deSdougm * lock will make any changes before 88357b448deSdougm * the others can read the repository. 884a99982a7Sdougm */ 88557b448deSdougm prop = scf_simple_prop_get 88657b448deSdougm (handle->scfhandle->handle, 88757b448deSdougm (const char *)SA_SVC_FMRI_BASE 88857b448deSdougm ":default", "operation", 889a99982a7Sdougm "legacy-timestamp"); 890a99982a7Sdougm if (prop != NULL) { 891a99982a7Sdougm char *i64; 89257b448deSdougm i64 = GETPROP(prop); 89357b448deSdougm if (i64 != NULL) 89457b448deSdougm tval = strtoull(i64, 89557b448deSdougm NULL, 0); 89657b448deSdougm if (CHECKTSTAMP(st, tval)) 897a99982a7Sdougm updatelegacy = B_TRUE; 89857b448deSdougm scf_simple_prop_free(prop); 899a99982a7Sdougm } else { 90057b448deSdougm /* 90157b448deSdougm * We haven't set the 90257b448deSdougm * timestamp before so do it. 90357b448deSdougm */ 904a99982a7Sdougm updatelegacy = B_TRUE; 905a99982a7Sdougm } 906a99982a7Sdougm } 907a99982a7Sdougm if (updatelegacy == B_FALSE) { 908a99982a7Sdougm /* Don't need the lock anymore */ 909a99982a7Sdougm (void) lockf(lockfd, F_ULOCK, 0); 910a99982a7Sdougm (void) close(lockfd); 911a99982a7Sdougm } 9121d1813a7Sdougm 9131d1813a7Sdougm /* 9141d1813a7Sdougm * It is essential that the document tree and 9151d1813a7Sdougm * the internal list of roots to handles be 9161d1813a7Sdougm * setup before anything that might try to 9171d1813a7Sdougm * create a new object is called. The document 9181d1813a7Sdougm * tree is the combination of handle->doc and 9191d1813a7Sdougm * handle->tree. This allows searches, 9201d1813a7Sdougm * etc. when all you have is an object in the 9211d1813a7Sdougm * tree. 9221d1813a7Sdougm */ 9231d1813a7Sdougm handle->doc = xmlNewDoc((xmlChar *)"1.0"); 92457b448deSdougm handle->tree = xmlNewNode(NULL, 92557b448deSdougm (xmlChar *)"sharecfg"); 92657b448deSdougm if (handle->doc != NULL && 92757b448deSdougm handle->tree != NULL) { 9284bff34e3Sthurlow (void) xmlDocSetRootElement(handle->doc, 92957b448deSdougm handle->tree); 93057b448deSdougm err = add_handle_for_root(handle->tree, 93157b448deSdougm handle); 9321d1813a7Sdougm if (err == SA_OK) 93357b448deSdougm err = sa_get_config( 93457b448deSdougm handle->scfhandle, 9351d1813a7Sdougm handle->tree, handle); 9361d1813a7Sdougm } else { 9371d1813a7Sdougm if (handle->doc != NULL) 9381d1813a7Sdougm xmlFreeDoc(handle->doc); 9391d1813a7Sdougm if (handle->tree != NULL) 9401d1813a7Sdougm xmlFreeNode(handle->tree); 9411d1813a7Sdougm err = SA_NO_MEMORY; 9421d1813a7Sdougm } 9431d1813a7Sdougm 944a99982a7Sdougm saunblocksigs(&old); 945549ec3ffSdougm 946549ec3ffSdougm if (err != SA_OK) { 947549ec3ffSdougm /* 9481d1813a7Sdougm * If we couldn't add the tree handle 9491d1813a7Sdougm * to the list, then things are going 9501d1813a7Sdougm * to fail badly. Might as well undo 9511d1813a7Sdougm * everything now and fail the 9521d1813a7Sdougm * sa_init(). 953549ec3ffSdougm */ 954549ec3ffSdougm sa_fini(handle); 955549ec3ffSdougm return (NULL); 956549ec3ffSdougm } 957549ec3ffSdougm 9586185db85Sdougm if (tval == 0) { 95957b448deSdougm /* 96057b448deSdougm * first time so make sure 96157b448deSdougm * default is setup 96257b448deSdougm */ 96357b448deSdougm verifydefgroupopts(handle); 9646185db85Sdougm } 9651d1813a7Sdougm 966a99982a7Sdougm if (updatelegacy == B_TRUE) { 967a99982a7Sdougm sablocksigs(&old); 968549ec3ffSdougm getlegacyconfig((sa_handle_t)handle, 969549ec3ffSdougm SA_LEGACY_DFSTAB, &handle->tree); 9706185db85Sdougm if (stat(SA_LEGACY_DFSTAB, &st) >= 0) 971546405c3Sdougm set_legacy_timestamp( 972546405c3Sdougm handle->tree, 9736185db85Sdougm SA_LEGACY_DFSTAB, 9746185db85Sdougm TSTAMP(st.st_ctim)); 975a99982a7Sdougm saunblocksigs(&old); 976546405c3Sdougm /* 977546405c3Sdougm * Safe to unlock now to allow 978546405c3Sdougm * others to run 979546405c3Sdougm */ 980a99982a7Sdougm (void) lockf(lockfd, F_ULOCK, 0); 981a99982a7Sdougm (void) close(lockfd); 9826185db85Sdougm } 9835b6e0c46Sdougm /* Get sharetab timestamp */ 9845b6e0c46Sdougm sa_update_sharetab_ts((sa_handle_t)handle); 9855b6e0c46Sdougm 9865b6e0c46Sdougm /* Get lastupdate (transaction) timestamp */ 9875b6e0c46Sdougm prop = scf_simple_prop_get( 9885b6e0c46Sdougm handle->scfhandle->handle, 9895b6e0c46Sdougm (const char *)SA_SVC_FMRI_BASE ":default", 9905b6e0c46Sdougm "state", "lastupdate"); 9915b6e0c46Sdougm if (prop != NULL) { 9925b6e0c46Sdougm char *str; 9935b6e0c46Sdougm str = 9945b6e0c46Sdougm scf_simple_prop_next_astring(prop); 9955b6e0c46Sdougm if (str != NULL) 9965b6e0c46Sdougm handle->tstrans = 9975b6e0c46Sdougm strtoull(str, NULL, 0); 9985b6e0c46Sdougm else 9995b6e0c46Sdougm handle->tstrans = 0; 10005b6e0c46Sdougm scf_simple_prop_free(prop); 10015b6e0c46Sdougm } 1002549ec3ffSdougm legacy |= sa_get_zfs_shares(handle, "zfs"); 1003549ec3ffSdougm legacy |= gettransients(handle, &handle->tree); 10046185db85Sdougm } 10056185db85Sdougm } 10066185db85Sdougm } 1007549ec3ffSdougm return ((sa_handle_t)handle); 10086185db85Sdougm } 10096185db85Sdougm 10106185db85Sdougm /* 1011549ec3ffSdougm * sa_fini(handle) 10126185db85Sdougm * Uninitialize the API structures including the configuration 10131cea05afSdougm * data structures and ZFS related data. 10146185db85Sdougm */ 10156185db85Sdougm 10166185db85Sdougm void 1017549ec3ffSdougm sa_fini(sa_handle_t handle) 10186185db85Sdougm { 1019549ec3ffSdougm sa_handle_impl_t impl_handle = (sa_handle_impl_t)handle; 1020549ec3ffSdougm 1021549ec3ffSdougm if (impl_handle != NULL) { 1022549ec3ffSdougm /* 1023549ec3ffSdougm * Free the config trees and any other data structures 1024549ec3ffSdougm * used in the handle. 1025549ec3ffSdougm */ 1026549ec3ffSdougm if (impl_handle->doc != NULL) 1027549ec3ffSdougm xmlFreeDoc(impl_handle->doc); 1028549ec3ffSdougm sa_scf_fini(impl_handle->scfhandle); 1029549ec3ffSdougm sa_zfs_fini(impl_handle); 1030549ec3ffSdougm 1031549ec3ffSdougm /* Remove and free the entry in the global list. */ 1032549ec3ffSdougm remove_handle_for_root(impl_handle->tree); 1033549ec3ffSdougm 1034549ec3ffSdougm /* Make sure we free the handle */ 1035549ec3ffSdougm free(impl_handle); 1036549ec3ffSdougm 1037549ec3ffSdougm /* 1038549ec3ffSdougm * If this was the last handle to release, unload the 1039*c5f58477Sdougm * plugins that were loaded. Use a mutex in case 1040*c5f58477Sdougm * another thread is reinitializing. 1041549ec3ffSdougm */ 1042*c5f58477Sdougm (void) mutex_lock(&sa_global_lock); 1043549ec3ffSdougm if (sa_global_handles == NULL) 1044549ec3ffSdougm (void) proto_plugin_fini(); 1045*c5f58477Sdougm (void) mutex_unlock(&sa_global_lock); 1046549ec3ffSdougm 10476185db85Sdougm } 10486185db85Sdougm } 10496185db85Sdougm 10506185db85Sdougm /* 10516185db85Sdougm * sa_get_protocols(char **protocol) 10526185db85Sdougm * Get array of protocols that are supported 10536185db85Sdougm * Returns pointer to an allocated and NULL terminated 10546185db85Sdougm * array of strings. Caller must free. 10556185db85Sdougm * This really should be determined dynamically. 10566185db85Sdougm * If there aren't any defined, return -1. 10576185db85Sdougm * Use free() to return memory. 10586185db85Sdougm */ 10596185db85Sdougm 10606185db85Sdougm int 10616185db85Sdougm sa_get_protocols(char ***protocols) 10626185db85Sdougm { 10636185db85Sdougm int numproto = -1; 10646185db85Sdougm 10656185db85Sdougm if (protocols != NULL) { 10666185db85Sdougm struct sa_proto_plugin *plug; 10676185db85Sdougm for (numproto = 0, plug = sap_proto_list; plug != NULL; 10686185db85Sdougm plug = plug->plugin_next) { 10696185db85Sdougm numproto++; 10706185db85Sdougm } 10716185db85Sdougm 10726185db85Sdougm *protocols = calloc(numproto + 1, sizeof (char *)); 10736185db85Sdougm if (*protocols != NULL) { 10746185db85Sdougm int ret = 0; 10756185db85Sdougm for (plug = sap_proto_list; plug != NULL; 10766185db85Sdougm plug = plug->plugin_next) { 10776185db85Sdougm /* faking for now */ 107857b448deSdougm (*protocols)[ret++] = 107957b448deSdougm plug->plugin_ops->sa_protocol; 10806185db85Sdougm } 10816185db85Sdougm } else { 10826185db85Sdougm numproto = -1; 10836185db85Sdougm } 10846185db85Sdougm } 10856185db85Sdougm return (numproto); 10866185db85Sdougm } 10876185db85Sdougm 10886185db85Sdougm /* 10896185db85Sdougm * find_group_by_name(node, group) 10906185db85Sdougm * 10916185db85Sdougm * search the XML document subtree specified by node to find the group 10926185db85Sdougm * specified by group. Searching subtree allows subgroups to be 10936185db85Sdougm * searched for. 10946185db85Sdougm */ 10956185db85Sdougm 10966185db85Sdougm static xmlNodePtr 10976185db85Sdougm find_group_by_name(xmlNodePtr node, xmlChar *group) 10986185db85Sdougm { 10996185db85Sdougm xmlChar *name = NULL; 11006185db85Sdougm 11016185db85Sdougm for (node = node->xmlChildrenNode; node != NULL; 11026185db85Sdougm node = node->next) { 11036185db85Sdougm if (xmlStrcmp(node->name, (xmlChar *)"group") == 0) { 11046185db85Sdougm /* if no groupname, return the first found */ 11056185db85Sdougm if (group == NULL) 11066185db85Sdougm break; 11076185db85Sdougm name = xmlGetProp(node, (xmlChar *)"name"); 110857b448deSdougm if (name != NULL && xmlStrcmp(name, group) == 0) 11096185db85Sdougm break; 11106185db85Sdougm if (name != NULL) { 11116185db85Sdougm xmlFree(name); 11126185db85Sdougm name = NULL; 11136185db85Sdougm } 11146185db85Sdougm } 11156185db85Sdougm } 11166185db85Sdougm if (name != NULL) 11176185db85Sdougm xmlFree(name); 11186185db85Sdougm return (node); 11196185db85Sdougm } 11206185db85Sdougm 11216185db85Sdougm /* 11226185db85Sdougm * sa_get_group(groupname) 11236185db85Sdougm * Return the "group" specified. If groupname is NULL, 11246185db85Sdougm * return the first group of the list of groups. 11256185db85Sdougm */ 11266185db85Sdougm sa_group_t 1127549ec3ffSdougm sa_get_group(sa_handle_t handle, char *groupname) 11286185db85Sdougm { 11296185db85Sdougm xmlNodePtr node = NULL; 11306185db85Sdougm char *subgroup = NULL; 11316185db85Sdougm char *group = NULL; 1132549ec3ffSdougm sa_handle_impl_t impl_handle = (sa_handle_impl_t)handle; 11336185db85Sdougm 1134549ec3ffSdougm if (impl_handle != NULL && impl_handle->tree != NULL) { 11356185db85Sdougm if (groupname != NULL) { 11366185db85Sdougm group = strdup(groupname); 1137a3351425Sdougm if (group != NULL) { 11386185db85Sdougm subgroup = strchr(group, '/'); 11396185db85Sdougm if (subgroup != NULL) 11406185db85Sdougm *subgroup++ = '\0'; 11416185db85Sdougm } 1142a3351425Sdougm } 1143a3351425Sdougm /* 1144a3351425Sdougm * We want to find the, possibly, named group. If 1145a3351425Sdougm * group is not NULL, then lookup the name. If it is 1146a3351425Sdougm * NULL, we only do the find if groupname is also 1147a3351425Sdougm * NULL. This allows lookup of the "first" group in 1148a3351425Sdougm * the internal list. 1149a3351425Sdougm */ 1150a3351425Sdougm if (group != NULL || groupname == NULL) 1151a3351425Sdougm node = find_group_by_name(impl_handle->tree, 1152a3351425Sdougm (xmlChar *)group); 1153a3351425Sdougm 11546185db85Sdougm /* if a subgroup, find it before returning */ 115557b448deSdougm if (subgroup != NULL && node != NULL) 11566185db85Sdougm node = find_group_by_name(node, (xmlChar *)subgroup); 11576185db85Sdougm } 11586185db85Sdougm if (node != NULL && (char *)group != NULL) 1159549ec3ffSdougm (void) sa_get_instance(impl_handle->scfhandle, (char *)group); 11606185db85Sdougm if (group != NULL) 11616185db85Sdougm free(group); 11626185db85Sdougm return ((sa_group_t)(node)); 11636185db85Sdougm } 11646185db85Sdougm 11656185db85Sdougm /* 11666185db85Sdougm * sa_get_next_group(group) 11676185db85Sdougm * Return the "next" group after the specified group from 11686185db85Sdougm * the internal group list. NULL if there are no more. 11696185db85Sdougm */ 11706185db85Sdougm sa_group_t 11716185db85Sdougm sa_get_next_group(sa_group_t group) 11726185db85Sdougm { 11736185db85Sdougm xmlNodePtr ngroup = NULL; 11746185db85Sdougm if (group != NULL) { 11756185db85Sdougm for (ngroup = ((xmlNodePtr)group)->next; ngroup != NULL; 11766185db85Sdougm ngroup = ngroup->next) { 11776185db85Sdougm if (xmlStrcmp(ngroup->name, (xmlChar *)"group") == 0) 11786185db85Sdougm break; 11796185db85Sdougm } 11806185db85Sdougm } 11816185db85Sdougm return ((sa_group_t)ngroup); 11826185db85Sdougm } 11836185db85Sdougm 11846185db85Sdougm /* 11856185db85Sdougm * sa_get_share(group, sharepath) 11866185db85Sdougm * Return the share object for the share specified. The share 11876185db85Sdougm * must be in the specified group. Return NULL if not found. 11886185db85Sdougm */ 11896185db85Sdougm sa_share_t 11906185db85Sdougm sa_get_share(sa_group_t group, char *sharepath) 11916185db85Sdougm { 11926185db85Sdougm xmlNodePtr node = NULL; 11936185db85Sdougm xmlChar *path; 11946185db85Sdougm 11956185db85Sdougm /* 11966185db85Sdougm * For future scalability, this should end up building a cache 11976185db85Sdougm * since it will get called regularly by the mountd and info 11986185db85Sdougm * services. 11996185db85Sdougm */ 12006185db85Sdougm if (group != NULL) { 12016185db85Sdougm for (node = ((xmlNodePtr)group)->children; node != NULL; 12026185db85Sdougm node = node->next) { 12036185db85Sdougm if (xmlStrcmp(node->name, (xmlChar *)"share") == 0) { 12046185db85Sdougm if (sharepath == NULL) { 12056185db85Sdougm break; 12066185db85Sdougm } else { 12076185db85Sdougm /* is it the correct share? */ 120857b448deSdougm path = xmlGetProp(node, 120957b448deSdougm (xmlChar *)"path"); 12106185db85Sdougm if (path != NULL && 121157b448deSdougm xmlStrcmp(path, 121257b448deSdougm (xmlChar *)sharepath) == 0) { 12136185db85Sdougm xmlFree(path); 12146185db85Sdougm break; 12156185db85Sdougm } 12166185db85Sdougm xmlFree(path); 12176185db85Sdougm } 12186185db85Sdougm } 12196185db85Sdougm } 12206185db85Sdougm } 12216185db85Sdougm return ((sa_share_t)node); 12226185db85Sdougm } 12236185db85Sdougm 12246185db85Sdougm /* 12256185db85Sdougm * sa_get_next_share(share) 12266185db85Sdougm * Return the next share following the specified share 12276185db85Sdougm * from the internal list of shares. Returns NULL if there 12286185db85Sdougm * are no more shares. The list is relative to the same 12296185db85Sdougm * group. 12306185db85Sdougm */ 12316185db85Sdougm sa_share_t 12326185db85Sdougm sa_get_next_share(sa_share_t share) 12336185db85Sdougm { 12346185db85Sdougm xmlNodePtr node = NULL; 12356185db85Sdougm 12366185db85Sdougm if (share != NULL) { 12376185db85Sdougm for (node = ((xmlNodePtr)share)->next; node != NULL; 12386185db85Sdougm node = node->next) { 12396185db85Sdougm if (xmlStrcmp(node->name, (xmlChar *)"share") == 0) { 12406185db85Sdougm break; 12416185db85Sdougm } 12426185db85Sdougm } 12436185db85Sdougm } 12446185db85Sdougm return ((sa_share_t)node); 12456185db85Sdougm } 12466185db85Sdougm 12476185db85Sdougm /* 12486185db85Sdougm * _sa_get_child_node(node, type) 12496185db85Sdougm * 12506185db85Sdougm * find the child node of the specified node that has "type". This is 12516185db85Sdougm * used to implement several internal functions. 12526185db85Sdougm */ 12536185db85Sdougm 12546185db85Sdougm static xmlNodePtr 12556185db85Sdougm _sa_get_child_node(xmlNodePtr node, xmlChar *type) 12566185db85Sdougm { 12576185db85Sdougm xmlNodePtr child; 12586185db85Sdougm for (child = node->xmlChildrenNode; child != NULL; 12596185db85Sdougm child = child->next) 12606185db85Sdougm if (xmlStrcmp(child->name, type) == 0) 12616185db85Sdougm return (child); 12626185db85Sdougm return ((xmlNodePtr)NULL); 12636185db85Sdougm } 12646185db85Sdougm 12656185db85Sdougm /* 12666185db85Sdougm * find_share(group, path) 12676185db85Sdougm * 12686185db85Sdougm * Search all the shares in the specified group for one that has the 12696185db85Sdougm * specified path. 12706185db85Sdougm */ 12716185db85Sdougm 12726185db85Sdougm static sa_share_t 12736185db85Sdougm find_share(sa_group_t group, char *sharepath) 12746185db85Sdougm { 12756185db85Sdougm sa_share_t share; 12766185db85Sdougm char *path; 12776185db85Sdougm 12786185db85Sdougm for (share = sa_get_share(group, NULL); share != NULL; 12796185db85Sdougm share = sa_get_next_share(share)) { 12806185db85Sdougm path = sa_get_share_attr(share, "path"); 12816185db85Sdougm if (path != NULL && strcmp(path, sharepath) == 0) { 12826185db85Sdougm sa_free_attr_string(path); 12836185db85Sdougm break; 12846185db85Sdougm } 12856185db85Sdougm if (path != NULL) 12866185db85Sdougm sa_free_attr_string(path); 12876185db85Sdougm } 12886185db85Sdougm return (share); 12896185db85Sdougm } 12906185db85Sdougm 12916185db85Sdougm /* 12926185db85Sdougm * sa_get_sub_group(group) 12936185db85Sdougm * 12946185db85Sdougm * Get the first sub-group of group. The sa_get_next_group() function 12956185db85Sdougm * can be used to get the rest. This is currently only used for ZFS 12966185db85Sdougm * sub-groups but could be used to implement a more general mechanism. 12976185db85Sdougm */ 12986185db85Sdougm 12996185db85Sdougm sa_group_t 13006185db85Sdougm sa_get_sub_group(sa_group_t group) 13016185db85Sdougm { 13026185db85Sdougm return ((sa_group_t)_sa_get_child_node((xmlNodePtr)group, 13036185db85Sdougm (xmlChar *)"group")); 13046185db85Sdougm } 13056185db85Sdougm 13066185db85Sdougm /* 13076185db85Sdougm * sa_find_share(sharepath) 13086185db85Sdougm * Finds a share regardless of group. In the future, this 13096185db85Sdougm * function should utilize a cache and hash table of some kind. 13106185db85Sdougm * The current assumption is that a path will only be shared 13116185db85Sdougm * once. In the future, this may change as implementation of 13126185db85Sdougm * resource names comes into being. 13136185db85Sdougm */ 13146185db85Sdougm sa_share_t 1315549ec3ffSdougm sa_find_share(sa_handle_t handle, char *sharepath) 13166185db85Sdougm { 13176185db85Sdougm sa_group_t group; 13186185db85Sdougm sa_group_t zgroup; 13196185db85Sdougm sa_share_t share = NULL; 13206185db85Sdougm int done = 0; 13216185db85Sdougm 1322549ec3ffSdougm for (group = sa_get_group(handle, NULL); group != NULL && !done; 13236185db85Sdougm group = sa_get_next_group(group)) { 13246185db85Sdougm if (is_zfs_group(group)) { 132557b448deSdougm for (zgroup = 132657b448deSdougm (sa_group_t)_sa_get_child_node((xmlNodePtr)group, 13276185db85Sdougm (xmlChar *)"group"); 132857b448deSdougm zgroup != NULL; 132957b448deSdougm zgroup = sa_get_next_group(zgroup)) { 13306185db85Sdougm share = find_share(zgroup, sharepath); 13316185db85Sdougm if (share != NULL) 13326185db85Sdougm break; 13336185db85Sdougm } 13346185db85Sdougm } else { 13356185db85Sdougm share = find_share(group, sharepath); 13366185db85Sdougm } 13376185db85Sdougm if (share != NULL) 13386185db85Sdougm break; 13396185db85Sdougm } 13406185db85Sdougm return (share); 13416185db85Sdougm } 13426185db85Sdougm 13436185db85Sdougm /* 1344f345c0beSdougm * sa_check_path(group, path, strictness) 13456185db85Sdougm * 1346da6c28aaSamw * Check that path is a valid path relative to the group. Currently, 13476185db85Sdougm * we are ignoring the group and checking only the NFS rules. Later, 13486185db85Sdougm * we may want to use the group to then check against the protocols 1349f345c0beSdougm * enabled on the group. The strictness values mean: 1350f345c0beSdougm * SA_CHECK_NORMAL == only check newpath against shares that are active 1351f345c0beSdougm * SA_CHECK_STRICT == check newpath against both active shares and those 1352f345c0beSdougm * stored in the repository 13536185db85Sdougm */ 13546185db85Sdougm 13556185db85Sdougm int 1356f345c0beSdougm sa_check_path(sa_group_t group, char *path, int strictness) 13576185db85Sdougm { 1358549ec3ffSdougm sa_handle_t handle; 1359549ec3ffSdougm 1360549ec3ffSdougm handle = sa_find_group_handle(group); 1361549ec3ffSdougm return (validpath(handle, path, strictness)); 13626185db85Sdougm } 13636185db85Sdougm 13646185db85Sdougm /* 1365da6c28aaSamw * mark_excluded_protos(group, share, flags) 13666185db85Sdougm * 1367da6c28aaSamw * Walk through all the protocols enabled for the group and check to 1368da6c28aaSamw * see if the share has any of them should be in the exclude list 1369da6c28aaSamw * based on the featureset of the protocol. If there are any, add the 1370da6c28aaSamw * "exclude" property to the share. 1371da6c28aaSamw */ 1372da6c28aaSamw static void 1373da6c28aaSamw mark_excluded_protos(sa_group_t group, xmlNodePtr share, uint64_t flags) 1374da6c28aaSamw { 1375da6c28aaSamw sa_optionset_t optionset; 1376da6c28aaSamw char exclude_list[SA_STRSIZE]; 1377da6c28aaSamw char *sep = ""; 1378da6c28aaSamw 1379da6c28aaSamw exclude_list[0] = '\0'; 1380da6c28aaSamw for (optionset = sa_get_optionset(group, NULL); 1381da6c28aaSamw optionset != NULL; 1382da6c28aaSamw optionset = sa_get_next_optionset(optionset)) { 1383da6c28aaSamw char *value; 1384da6c28aaSamw uint64_t features; 1385da6c28aaSamw value = sa_get_optionset_attr(optionset, "type"); 1386da6c28aaSamw if (value == NULL) 1387da6c28aaSamw continue; 1388da6c28aaSamw features = sa_proto_get_featureset(value); 1389da6c28aaSamw sa_free_attr_string(value); 1390da6c28aaSamw if (!(features & flags)) { 1391da6c28aaSamw (void) strlcat(exclude_list, sep, 1392da6c28aaSamw sizeof (exclude_list)); 1393da6c28aaSamw (void) strlcat(exclude_list, value, 1394da6c28aaSamw sizeof (exclude_list)); 1395da6c28aaSamw sep = ","; 1396da6c28aaSamw } 1397da6c28aaSamw } 1398da6c28aaSamw if (exclude_list[0] != '\0') 13994bff34e3Sthurlow (void) xmlSetProp(share, (xmlChar *)"exclude", 1400da6c28aaSamw (xmlChar *)exclude_list); 1401da6c28aaSamw } 1402da6c28aaSamw 1403da6c28aaSamw /* 1404da6c28aaSamw * get_all_features(group) 1405da6c28aaSamw * 1406da6c28aaSamw * Walk through all the protocols on the group and collect all 1407da6c28aaSamw * possible enabled features. This is the OR of all the featuresets. 1408da6c28aaSamw */ 1409da6c28aaSamw static uint64_t 1410da6c28aaSamw get_all_features(sa_group_t group) 1411da6c28aaSamw { 1412da6c28aaSamw sa_optionset_t optionset; 1413da6c28aaSamw uint64_t features = 0; 1414da6c28aaSamw 1415da6c28aaSamw for (optionset = sa_get_optionset(group, NULL); 1416da6c28aaSamw optionset != NULL; 1417da6c28aaSamw optionset = sa_get_next_optionset(optionset)) { 1418da6c28aaSamw char *value; 1419da6c28aaSamw value = sa_get_optionset_attr(optionset, "type"); 1420da6c28aaSamw if (value == NULL) 1421da6c28aaSamw continue; 1422da6c28aaSamw features |= sa_proto_get_featureset(value); 1423da6c28aaSamw sa_free_attr_string(value); 1424da6c28aaSamw } 1425da6c28aaSamw return (features); 1426da6c28aaSamw } 1427da6c28aaSamw 1428da6c28aaSamw 1429da6c28aaSamw /* 1430da6c28aaSamw * _sa_add_share(group, sharepath, persist, *error, flags) 1431da6c28aaSamw * 1432da6c28aaSamw * Common code for all types of add_share. sa_add_share() is the 14336185db85Sdougm * public API, we also need to be able to do this when parsing legacy 14346185db85Sdougm * files and construction of the internal configuration while 1435da6c28aaSamw * extracting config info from SMF. "flags" indicates if some 1436da6c28aaSamw * protocols need relaxed rules while other don't. These values are 1437da6c28aaSamw * the featureset values defined in libshare.h. 14386185db85Sdougm */ 14396185db85Sdougm 14406185db85Sdougm sa_share_t 1441da6c28aaSamw _sa_add_share(sa_group_t group, char *sharepath, int persist, int *error, 1442da6c28aaSamw uint64_t flags) 14436185db85Sdougm { 14446185db85Sdougm xmlNodePtr node = NULL; 14456185db85Sdougm int err; 14466185db85Sdougm 14476185db85Sdougm err = SA_OK; /* assume success */ 14486185db85Sdougm 144957b448deSdougm node = xmlNewChild((xmlNodePtr)group, NULL, (xmlChar *)"share", NULL); 1450da6c28aaSamw if (node == NULL) { 1451da6c28aaSamw if (error != NULL) 1452da6c28aaSamw *error = SA_NO_MEMORY; 1453da6c28aaSamw return (node); 1454da6c28aaSamw } 1455da6c28aaSamw 14564bff34e3Sthurlow (void) xmlSetProp(node, (xmlChar *)"path", (xmlChar *)sharepath); 14574bff34e3Sthurlow (void) xmlSetProp(node, (xmlChar *)"type", 145857b448deSdougm persist ? (xmlChar *)"persist" : (xmlChar *)"transient"); 1459da6c28aaSamw if (flags != 0) 1460da6c28aaSamw mark_excluded_protos(group, node, flags); 14616185db85Sdougm if (persist != SA_SHARE_TRANSIENT) { 14626185db85Sdougm /* 14636185db85Sdougm * persistent shares come in two flavors: SMF and 14646185db85Sdougm * ZFS. Sort this one out based on target group and 1465da6c28aaSamw * path type. Both NFS and SMB are supported. First, 1466da6c28aaSamw * check to see if the protocol is enabled on the 1467da6c28aaSamw * subgroup and then setup the share appropriately. 14686185db85Sdougm */ 146957b448deSdougm if (sa_group_is_zfs(group) && 147057b448deSdougm sa_path_is_zfs(sharepath)) { 1471da6c28aaSamw if (sa_get_optionset(group, "nfs") != NULL) 14726185db85Sdougm err = sa_zfs_set_sharenfs(group, sharepath, 1); 1473da6c28aaSamw else if (sa_get_optionset(group, "smb") != NULL) 1474da6c28aaSamw err = sa_zfs_set_sharesmb(group, sharepath, 1); 14756185db85Sdougm } else { 1476549ec3ffSdougm sa_handle_impl_t impl_handle; 147757b448deSdougm impl_handle = 1478da6c28aaSamw (sa_handle_impl_t)sa_find_group_handle(group); 147957b448deSdougm if (impl_handle != NULL) { 1480da6c28aaSamw err = sa_commit_share(impl_handle->scfhandle, 1481da6c28aaSamw group, (sa_share_t)node); 148257b448deSdougm } else { 1483549ec3ffSdougm err = SA_SYSTEM_ERR; 14846185db85Sdougm } 14856185db85Sdougm } 148657b448deSdougm } 1487da6c28aaSamw if (err == SA_NO_PERMISSION && persist & SA_SHARE_PARSER) 14886185db85Sdougm /* called by the dfstab parser so could be a show */ 14896185db85Sdougm err = SA_OK; 1490da6c28aaSamw 14916185db85Sdougm if (err != SA_OK) { 14926185db85Sdougm /* 14936185db85Sdougm * we couldn't commit to the repository so undo 14946185db85Sdougm * our internal state to reflect reality. 14956185db85Sdougm */ 14966185db85Sdougm xmlUnlinkNode(node); 14976185db85Sdougm xmlFreeNode(node); 14986185db85Sdougm node = NULL; 14996185db85Sdougm } 1500da6c28aaSamw 15016185db85Sdougm if (error != NULL) 15026185db85Sdougm *error = err; 1503da6c28aaSamw 15046185db85Sdougm return (node); 15056185db85Sdougm } 15066185db85Sdougm 15076185db85Sdougm /* 15086185db85Sdougm * sa_add_share(group, sharepath, persist, *error) 15096185db85Sdougm * 15106185db85Sdougm * Add a new share object to the specified group. The share will 15116185db85Sdougm * have the specified sharepath and will only be constructed if 15126185db85Sdougm * it is a valid path to be shared. NULL is returned on error 15136185db85Sdougm * and a detailed error value will be returned via the error 15146185db85Sdougm * pointer. 15156185db85Sdougm */ 15166185db85Sdougm sa_share_t 15176185db85Sdougm sa_add_share(sa_group_t group, char *sharepath, int persist, int *error) 15186185db85Sdougm { 15196185db85Sdougm xmlNodePtr node = NULL; 1520f345c0beSdougm int strictness = SA_CHECK_NORMAL; 1521549ec3ffSdougm sa_handle_t handle; 1522da6c28aaSamw uint64_t special = 0; 1523da6c28aaSamw uint64_t features; 1524f345c0beSdougm 1525f345c0beSdougm /* 1526f345c0beSdougm * If the share is to be permanent, use strict checking so a 1527f345c0beSdougm * bad config doesn't get created. Transient shares only need 1528f345c0beSdougm * to check against the currently active 1529f345c0beSdougm * shares. SA_SHARE_PARSER is a modifier used internally to 1530f345c0beSdougm * indicate that we are being called by the dfstab parser and 1531f345c0beSdougm * that we need strict checking in all cases. Normally persist 1532f345c0beSdougm * is in integer value but SA_SHARE_PARSER may be or'd into 1533f345c0beSdougm * it as an override. 1534f345c0beSdougm */ 1535f345c0beSdougm if (persist & SA_SHARE_PARSER || persist == SA_SHARE_PERMANENT) 1536f345c0beSdougm strictness = SA_CHECK_STRICT; 15376185db85Sdougm 1538549ec3ffSdougm handle = sa_find_group_handle(group); 1539549ec3ffSdougm 1540da6c28aaSamw /* 1541da6c28aaSamw * need to determine if the share is valid. The rules are: 1542da6c28aaSamw * - The path must not already exist 1543da6c28aaSamw * - The path must not be a subdir or parent dir of an 1544da6c28aaSamw * existing path unless at least one protocol allows it. 1545da6c28aaSamw * The sub/parent check is done in sa_check_path(). 1546da6c28aaSamw */ 1547da6c28aaSamw 1548da6c28aaSamw if (sa_find_share(handle, sharepath) == NULL) { 1549da6c28aaSamw *error = sa_check_path(group, sharepath, strictness); 1550da6c28aaSamw features = get_all_features(group); 1551da6c28aaSamw switch (*error) { 1552da6c28aaSamw case SA_PATH_IS_SUBDIR: 1553da6c28aaSamw if (features & SA_FEATURE_ALLOWSUBDIRS) 1554da6c28aaSamw special |= SA_FEATURE_ALLOWSUBDIRS; 1555da6c28aaSamw break; 1556da6c28aaSamw case SA_PATH_IS_PARENTDIR: 1557da6c28aaSamw if (features & SA_FEATURE_ALLOWPARDIRS) 1558da6c28aaSamw special |= SA_FEATURE_ALLOWPARDIRS; 1559da6c28aaSamw break; 15606185db85Sdougm } 1561da6c28aaSamw if (*error == SA_OK || special != SA_FEATURE_NONE) 1562da6c28aaSamw node = _sa_add_share(group, sharepath, persist, 1563da6c28aaSamw error, special); 1564da6c28aaSamw } else { 15656185db85Sdougm *error = SA_DUPLICATE_NAME; 1566da6c28aaSamw } 15676185db85Sdougm 15686185db85Sdougm return ((sa_share_t)node); 15696185db85Sdougm } 15706185db85Sdougm 15716185db85Sdougm /* 15726185db85Sdougm * sa_enable_share(share, protocol) 15736185db85Sdougm * Enable the specified share to the specified protocol. 15746185db85Sdougm * If protocol is NULL, then all protocols. 15756185db85Sdougm */ 15766185db85Sdougm int 15776185db85Sdougm sa_enable_share(sa_share_t share, char *protocol) 15786185db85Sdougm { 15796185db85Sdougm char *sharepath; 15806185db85Sdougm struct stat st; 1581da6c28aaSamw int err = SA_OK; 1582da6c28aaSamw int ret; 15836185db85Sdougm 15846185db85Sdougm sharepath = sa_get_share_attr(share, "path"); 1585da6c28aaSamw if (sharepath == NULL) 1586da6c28aaSamw return (SA_NO_MEMORY); 15876185db85Sdougm if (stat(sharepath, &st) < 0) { 15886185db85Sdougm err = SA_NO_SUCH_PATH; 15896185db85Sdougm } else { 15906185db85Sdougm /* tell the server about the share */ 15916185db85Sdougm if (protocol != NULL) { 1592da6c28aaSamw if (excluded_protocol(share, protocol)) 1593da6c28aaSamw goto done; 1594da6c28aaSamw 15956185db85Sdougm /* lookup protocol specific handler */ 15966185db85Sdougm err = sa_proto_share(protocol, share); 15976185db85Sdougm if (err == SA_OK) 1598da6c28aaSamw (void) sa_set_share_attr(share, 1599da6c28aaSamw "shared", "true"); 16006185db85Sdougm } else { 1601da6c28aaSamw /* Tell all protocols about the share */ 1602da6c28aaSamw sa_group_t group; 1603da6c28aaSamw sa_optionset_t optionset; 1604da6c28aaSamw 1605da6c28aaSamw group = sa_get_parent_group(share); 1606da6c28aaSamw 1607da6c28aaSamw for (optionset = sa_get_optionset(group, NULL); 1608da6c28aaSamw optionset != NULL; 1609da6c28aaSamw optionset = sa_get_next_optionset(optionset)) { 1610da6c28aaSamw char *proto; 1611da6c28aaSamw proto = sa_get_optionset_attr(optionset, 1612da6c28aaSamw "type"); 1613da6c28aaSamw if (proto != NULL) { 1614da6c28aaSamw if (!excluded_protocol(share, proto)) { 1615da6c28aaSamw ret = sa_proto_share(proto, 1616da6c28aaSamw share); 1617da6c28aaSamw if (ret != SA_OK) 1618da6c28aaSamw err = ret; 1619da6c28aaSamw } 1620da6c28aaSamw sa_free_attr_string(proto); 1621da6c28aaSamw } 1622da6c28aaSamw } 16236185db85Sdougm (void) sa_set_share_attr(share, "shared", "true"); 16246185db85Sdougm } 16256185db85Sdougm } 1626da6c28aaSamw done: 16276185db85Sdougm if (sharepath != NULL) 16286185db85Sdougm sa_free_attr_string(sharepath); 16296185db85Sdougm return (err); 16306185db85Sdougm } 16316185db85Sdougm 16326185db85Sdougm /* 16336185db85Sdougm * sa_disable_share(share, protocol) 1634da6c28aaSamw * Disable the specified share to the specified protocol. If 1635da6c28aaSamw * protocol is NULL, then all protocols that are enabled for the 1636da6c28aaSamw * share should be disabled. 16376185db85Sdougm */ 16386185db85Sdougm int 16396185db85Sdougm sa_disable_share(sa_share_t share, char *protocol) 16406185db85Sdougm { 16416185db85Sdougm char *path; 1642da6c28aaSamw int err = SA_OK; 16436185db85Sdougm int ret = SA_OK; 16446185db85Sdougm 16456185db85Sdougm path = sa_get_share_attr(share, "path"); 16466185db85Sdougm 16476185db85Sdougm if (protocol != NULL) { 1648ecd6cf80Smarks ret = sa_proto_unshare(share, protocol, path); 16496185db85Sdougm } else { 16506185db85Sdougm /* need to do all protocols */ 1651da6c28aaSamw sa_group_t group; 1652da6c28aaSamw sa_optionset_t optionset; 1653da6c28aaSamw 1654da6c28aaSamw group = sa_get_parent_group(share); 1655da6c28aaSamw 1656da6c28aaSamw /* Tell all protocols about the share */ 1657da6c28aaSamw for (optionset = sa_get_optionset(group, NULL); 1658da6c28aaSamw optionset != NULL; 1659da6c28aaSamw optionset = sa_get_next_optionset(optionset)) { 1660da6c28aaSamw char *proto; 1661da6c28aaSamw 1662da6c28aaSamw proto = sa_get_optionset_attr(optionset, "type"); 1663da6c28aaSamw if (proto != NULL) { 1664da6c28aaSamw err = sa_proto_unshare(share, proto, path); 1665da6c28aaSamw if (err != SA_OK) 1666da6c28aaSamw ret = err; 1667da6c28aaSamw sa_free_attr_string(proto); 1668da6c28aaSamw } 1669da6c28aaSamw } 16706185db85Sdougm } 16716185db85Sdougm if (ret == SA_OK) 16726185db85Sdougm (void) sa_set_share_attr(share, "shared", NULL); 16736185db85Sdougm if (path != NULL) 16746185db85Sdougm sa_free_attr_string(path); 16756185db85Sdougm return (ret); 16766185db85Sdougm } 16776185db85Sdougm 16786185db85Sdougm /* 16796185db85Sdougm * sa_remove_share(share) 16806185db85Sdougm * 16816185db85Sdougm * remove the specified share from its containing group. 16826185db85Sdougm * Remove from the SMF or ZFS configuration space. 16836185db85Sdougm */ 16846185db85Sdougm 16856185db85Sdougm int 16866185db85Sdougm sa_remove_share(sa_share_t share) 16876185db85Sdougm { 16886185db85Sdougm sa_group_t group; 16896185db85Sdougm int ret = SA_OK; 16906185db85Sdougm char *type; 16916185db85Sdougm int transient = 0; 16926185db85Sdougm char *groupname; 16936185db85Sdougm char *zfs; 16946185db85Sdougm 16956185db85Sdougm type = sa_get_share_attr(share, "type"); 16966185db85Sdougm group = sa_get_parent_group(share); 16976185db85Sdougm zfs = sa_get_group_attr(group, "zfs"); 16986185db85Sdougm groupname = sa_get_group_attr(group, "name"); 16996185db85Sdougm if (type != NULL && strcmp(type, "persist") != 0) 17006185db85Sdougm transient = 1; 17016185db85Sdougm if (type != NULL) 17026185db85Sdougm sa_free_attr_string(type); 17036185db85Sdougm 17046185db85Sdougm /* remove the node from its group then free the memory */ 17056185db85Sdougm 17066185db85Sdougm /* 17076185db85Sdougm * need to test if "busy" 17086185db85Sdougm */ 17096185db85Sdougm /* only do SMF action if permanent */ 17106185db85Sdougm if (!transient || zfs != NULL) { 17116185db85Sdougm /* remove from legacy dfstab as well as possible SMF */ 1712da6c28aaSamw ret = sa_delete_legacy(share, NULL); 17136185db85Sdougm if (ret == SA_OK) { 17146185db85Sdougm if (!sa_group_is_zfs(group)) { 1715549ec3ffSdougm sa_handle_impl_t impl_handle; 171657b448deSdougm impl_handle = (sa_handle_impl_t) 171757b448deSdougm sa_find_group_handle(group); 171857b448deSdougm if (impl_handle != NULL) { 171957b448deSdougm ret = sa_delete_share( 172057b448deSdougm impl_handle->scfhandle, group, 172157b448deSdougm share); 17226185db85Sdougm } else { 172357b448deSdougm ret = SA_SYSTEM_ERR; 172457b448deSdougm } 172557b448deSdougm } else { 172657b448deSdougm char *sharepath = sa_get_share_attr(share, 172757b448deSdougm "path"); 17286185db85Sdougm if (sharepath != NULL) { 172957b448deSdougm ret = sa_zfs_set_sharenfs(group, 173057b448deSdougm sharepath, 0); 17316185db85Sdougm sa_free_attr_string(sharepath); 17326185db85Sdougm } 17336185db85Sdougm } 17346185db85Sdougm } 17356185db85Sdougm } 17366185db85Sdougm if (groupname != NULL) 17376185db85Sdougm sa_free_attr_string(groupname); 17386185db85Sdougm if (zfs != NULL) 17396185db85Sdougm sa_free_attr_string(zfs); 17406185db85Sdougm 17416185db85Sdougm xmlUnlinkNode((xmlNodePtr)share); 17426185db85Sdougm xmlFreeNode((xmlNodePtr)share); 17436185db85Sdougm return (ret); 17446185db85Sdougm } 17456185db85Sdougm 17466185db85Sdougm /* 17476185db85Sdougm * sa_move_share(group, share) 17486185db85Sdougm * 17496185db85Sdougm * move the specified share to the specified group. Update SMF 17506185db85Sdougm * appropriately. 17516185db85Sdougm */ 17526185db85Sdougm 17536185db85Sdougm int 17546185db85Sdougm sa_move_share(sa_group_t group, sa_share_t share) 17556185db85Sdougm { 17566185db85Sdougm sa_group_t oldgroup; 17576185db85Sdougm int ret = SA_OK; 17586185db85Sdougm 17596185db85Sdougm /* remove the node from its group then free the memory */ 17606185db85Sdougm 17616185db85Sdougm oldgroup = sa_get_parent_group(share); 17626185db85Sdougm if (oldgroup != group) { 1763549ec3ffSdougm sa_handle_impl_t impl_handle; 17646185db85Sdougm xmlUnlinkNode((xmlNodePtr)share); 176557b448deSdougm /* 176657b448deSdougm * now that the share isn't in its old group, add to 176757b448deSdougm * the new one 176857b448deSdougm */ 17694bff34e3Sthurlow (void) xmlAddChild((xmlNodePtr)group, (xmlNodePtr)share); 17706185db85Sdougm /* need to deal with SMF */ 1771549ec3ffSdougm impl_handle = (sa_handle_impl_t)sa_find_group_handle(group); 1772549ec3ffSdougm if (impl_handle != NULL) { 17736185db85Sdougm /* 17746185db85Sdougm * need to remove from old group first and then add to 17756185db85Sdougm * new group. Ideally, we would do the other order but 17766185db85Sdougm * need to avoid having the share in two groups at the 17776185db85Sdougm * same time. 17786185db85Sdougm */ 177957b448deSdougm ret = sa_delete_share(impl_handle->scfhandle, oldgroup, 178057b448deSdougm share); 1781549ec3ffSdougm if (ret == SA_OK) 178257b448deSdougm ret = sa_commit_share(impl_handle->scfhandle, 178357b448deSdougm group, share); 1784549ec3ffSdougm } else { 1785549ec3ffSdougm ret = SA_SYSTEM_ERR; 17866185db85Sdougm } 17876185db85Sdougm } 17886185db85Sdougm return (ret); 17896185db85Sdougm } 17906185db85Sdougm 17916185db85Sdougm /* 17926185db85Sdougm * sa_get_parent_group(share) 17936185db85Sdougm * 1794da6c28aaSamw * Return the containing group for the share. If a group was actually 17956185db85Sdougm * passed in, we don't want a parent so return NULL. 17966185db85Sdougm */ 17976185db85Sdougm 17986185db85Sdougm sa_group_t 17996185db85Sdougm sa_get_parent_group(sa_share_t share) 18006185db85Sdougm { 18016185db85Sdougm xmlNodePtr node = NULL; 18026185db85Sdougm if (share != NULL) { 18036185db85Sdougm node = ((xmlNodePtr)share)->parent; 18046185db85Sdougm /* 18056185db85Sdougm * make sure parent is a group and not sharecfg since 18066185db85Sdougm * we may be cheating and passing in a group. 18076185db85Sdougm * Eventually, groups of groups might come into being. 18086185db85Sdougm */ 18096185db85Sdougm if (node == NULL || 18106185db85Sdougm xmlStrcmp(node->name, (xmlChar *)"sharecfg") == 0) 18116185db85Sdougm node = NULL; 18126185db85Sdougm } 18136185db85Sdougm return ((sa_group_t)node); 18146185db85Sdougm } 18156185db85Sdougm 18166185db85Sdougm /* 1817549ec3ffSdougm * _sa_create_group(impl_handle, groupname) 18186185db85Sdougm * 18196185db85Sdougm * Create a group in the document. The caller will need to deal with 18206185db85Sdougm * configuration store and activation. 18216185db85Sdougm */ 18226185db85Sdougm 18236185db85Sdougm sa_group_t 1824549ec3ffSdougm _sa_create_group(sa_handle_impl_t impl_handle, char *groupname) 18256185db85Sdougm { 18266185db85Sdougm xmlNodePtr node = NULL; 18276185db85Sdougm 18286185db85Sdougm if (sa_valid_group_name(groupname)) { 182957b448deSdougm node = xmlNewChild(impl_handle->tree, NULL, (xmlChar *)"group", 183057b448deSdougm NULL); 18316185db85Sdougm if (node != NULL) { 18324bff34e3Sthurlow (void) xmlSetProp(node, (xmlChar *)"name", 183357b448deSdougm (xmlChar *)groupname); 18344bff34e3Sthurlow (void) xmlSetProp(node, (xmlChar *)"state", 183557b448deSdougm (xmlChar *)"enabled"); 18366185db85Sdougm } 18376185db85Sdougm } 18386185db85Sdougm return ((sa_group_t)node); 18396185db85Sdougm } 18406185db85Sdougm 18416185db85Sdougm /* 18426185db85Sdougm * _sa_create_zfs_group(group, groupname) 18436185db85Sdougm * 18446185db85Sdougm * Create a ZFS subgroup under the specified group. This may 18456185db85Sdougm * eventually form the basis of general sub-groups, but is currently 18466185db85Sdougm * restricted to ZFS. 18476185db85Sdougm */ 18486185db85Sdougm sa_group_t 18496185db85Sdougm _sa_create_zfs_group(sa_group_t group, char *groupname) 18506185db85Sdougm { 18516185db85Sdougm xmlNodePtr node = NULL; 18526185db85Sdougm 185357b448deSdougm node = xmlNewChild((xmlNodePtr)group, NULL, (xmlChar *)"group", NULL); 18546185db85Sdougm if (node != NULL) { 18554bff34e3Sthurlow (void) xmlSetProp(node, (xmlChar *)"name", 18564bff34e3Sthurlow (xmlChar *)groupname); 18574bff34e3Sthurlow (void) xmlSetProp(node, (xmlChar *)"state", 18584bff34e3Sthurlow (xmlChar *)"enabled"); 18596185db85Sdougm } 18606185db85Sdougm 18616185db85Sdougm return ((sa_group_t)node); 18626185db85Sdougm } 18636185db85Sdougm 18646185db85Sdougm /* 18656185db85Sdougm * sa_create_group(groupname, *error) 18666185db85Sdougm * 18676185db85Sdougm * Create a new group with groupname. Need to validate that it is a 18686185db85Sdougm * legal name for SMF and the construct the SMF service instance of 18696185db85Sdougm * svc:/network/shares/group to implement the group. All necessary 18706185db85Sdougm * operational properties must be added to the group at this point 18716185db85Sdougm * (via the SMF transaction model). 18726185db85Sdougm */ 18736185db85Sdougm sa_group_t 1874549ec3ffSdougm sa_create_group(sa_handle_t handle, char *groupname, int *error) 18756185db85Sdougm { 18766185db85Sdougm xmlNodePtr node = NULL; 18776185db85Sdougm sa_group_t group; 18786185db85Sdougm int ret; 187957b448deSdougm char rbacstr[SA_STRSIZE]; 1880549ec3ffSdougm sa_handle_impl_t impl_handle = (sa_handle_impl_t)handle; 18816185db85Sdougm 18826185db85Sdougm ret = SA_OK; 18836185db85Sdougm 1884549ec3ffSdougm if (impl_handle == NULL || impl_handle->scfhandle == NULL) { 18856185db85Sdougm ret = SA_SYSTEM_ERR; 18866185db85Sdougm goto err; 18876185db85Sdougm } 18886185db85Sdougm 1889549ec3ffSdougm group = sa_get_group(handle, groupname); 18906185db85Sdougm if (group != NULL) { 18916185db85Sdougm ret = SA_DUPLICATE_NAME; 18926185db85Sdougm } else { 18936185db85Sdougm if (sa_valid_group_name(groupname)) { 1894549ec3ffSdougm node = xmlNewChild(impl_handle->tree, NULL, 18956185db85Sdougm (xmlChar *)"group", NULL); 18966185db85Sdougm if (node != NULL) { 18974bff34e3Sthurlow (void) xmlSetProp(node, (xmlChar *)"name", 189857b448deSdougm (xmlChar *)groupname); 18996185db85Sdougm /* default to the group being enabled */ 19004bff34e3Sthurlow (void) xmlSetProp(node, (xmlChar *)"state", 190157b448deSdougm (xmlChar *)"enabled"); 190257b448deSdougm ret = sa_create_instance(impl_handle->scfhandle, 190357b448deSdougm groupname); 19046185db85Sdougm if (ret == SA_OK) { 190557b448deSdougm ret = sa_start_transaction( 190657b448deSdougm impl_handle->scfhandle, 1907549ec3ffSdougm "operation"); 19086185db85Sdougm } 19096185db85Sdougm if (ret == SA_OK) { 191057b448deSdougm ret = sa_set_property( 191157b448deSdougm impl_handle->scfhandle, 1912549ec3ffSdougm "state", "enabled"); 19136185db85Sdougm if (ret == SA_OK) { 191457b448deSdougm ret = sa_end_transaction( 19155b6e0c46Sdougm impl_handle->scfhandle, 19165b6e0c46Sdougm impl_handle); 19176185db85Sdougm } else { 191857b448deSdougm sa_abort_transaction( 191957b448deSdougm impl_handle->scfhandle); 19206185db85Sdougm } 19216185db85Sdougm } 19226185db85Sdougm if (ret == SA_OK) { 19236185db85Sdougm /* initialize the RBAC strings */ 192457b448deSdougm ret = sa_start_transaction( 192557b448deSdougm impl_handle->scfhandle, 1926549ec3ffSdougm "general"); 19276185db85Sdougm if (ret == SA_OK) { 192857b448deSdougm (void) snprintf(rbacstr, 192957b448deSdougm sizeof (rbacstr), "%s.%s", 19306185db85Sdougm SA_RBAC_MANAGE, groupname); 193157b448deSdougm ret = sa_set_property( 193257b448deSdougm impl_handle->scfhandle, 19336185db85Sdougm "action_authorization", 19346185db85Sdougm rbacstr); 19356185db85Sdougm } 19366185db85Sdougm if (ret == SA_OK) { 193757b448deSdougm (void) snprintf(rbacstr, 193857b448deSdougm sizeof (rbacstr), "%s.%s", 19396185db85Sdougm SA_RBAC_VALUE, groupname); 194057b448deSdougm ret = sa_set_property( 194157b448deSdougm impl_handle->scfhandle, 19426185db85Sdougm "value_authorization", 19436185db85Sdougm rbacstr); 19446185db85Sdougm } 19456185db85Sdougm if (ret == SA_OK) { 194657b448deSdougm ret = sa_end_transaction( 19475b6e0c46Sdougm impl_handle->scfhandle, 19485b6e0c46Sdougm impl_handle); 19496185db85Sdougm } else { 195057b448deSdougm sa_abort_transaction( 195157b448deSdougm impl_handle->scfhandle); 19526185db85Sdougm } 19536185db85Sdougm } 19546185db85Sdougm if (ret != SA_OK) { 19556185db85Sdougm /* 195657b448deSdougm * Couldn't commit the group 195757b448deSdougm * so we need to undo 195857b448deSdougm * internally. 19596185db85Sdougm */ 19606185db85Sdougm xmlUnlinkNode(node); 19616185db85Sdougm xmlFreeNode(node); 19626185db85Sdougm node = NULL; 19636185db85Sdougm } 19646185db85Sdougm } else { 19656185db85Sdougm ret = SA_NO_MEMORY; 19666185db85Sdougm } 19676185db85Sdougm } else { 19686185db85Sdougm ret = SA_INVALID_NAME; 19696185db85Sdougm } 19706185db85Sdougm } 19716185db85Sdougm err: 19726185db85Sdougm if (error != NULL) 19736185db85Sdougm *error = ret; 19746185db85Sdougm return ((sa_group_t)node); 19756185db85Sdougm } 19766185db85Sdougm 19776185db85Sdougm /* 19786185db85Sdougm * sa_remove_group(group) 19796185db85Sdougm * 19806185db85Sdougm * Remove the specified group. This deletes from the SMF repository. 19816185db85Sdougm * All property groups and properties are removed. 19826185db85Sdougm */ 19836185db85Sdougm 19846185db85Sdougm int 19856185db85Sdougm sa_remove_group(sa_group_t group) 19866185db85Sdougm { 19876185db85Sdougm char *name; 19886185db85Sdougm int ret = SA_OK; 1989549ec3ffSdougm sa_handle_impl_t impl_handle; 19906185db85Sdougm 1991549ec3ffSdougm impl_handle = (sa_handle_impl_t)sa_find_group_handle(group); 1992549ec3ffSdougm if (impl_handle != NULL) { 19936185db85Sdougm name = sa_get_group_attr(group, "name"); 19946185db85Sdougm if (name != NULL) { 1995549ec3ffSdougm ret = sa_delete_instance(impl_handle->scfhandle, name); 19966185db85Sdougm sa_free_attr_string(name); 19976185db85Sdougm } 19986185db85Sdougm xmlUnlinkNode((xmlNodePtr)group); /* make sure unlinked */ 19996185db85Sdougm xmlFreeNode((xmlNodePtr)group); /* now it is gone */ 2000549ec3ffSdougm } else { 2001549ec3ffSdougm ret = SA_SYSTEM_ERR; 2002549ec3ffSdougm } 20036185db85Sdougm return (ret); 20046185db85Sdougm } 20056185db85Sdougm 20066185db85Sdougm /* 20076185db85Sdougm * sa_update_config() 20086185db85Sdougm * 20096185db85Sdougm * Used to update legacy files that need to be updated in bulk 20106185db85Sdougm * Currently, this is a placeholder and will go away in a future 20116185db85Sdougm * release. 20126185db85Sdougm */ 20136185db85Sdougm 20146185db85Sdougm int 2015549ec3ffSdougm sa_update_config(sa_handle_t handle) 20166185db85Sdougm { 20176185db85Sdougm /* 20186185db85Sdougm * do legacy files first so we can tell when they change. 20196185db85Sdougm * This will go away when we start updating individual records 20206185db85Sdougm * rather than the whole file. 20216185db85Sdougm */ 2022549ec3ffSdougm update_legacy_config(handle); 20236185db85Sdougm return (SA_OK); 20246185db85Sdougm } 20256185db85Sdougm 20266185db85Sdougm /* 20276185db85Sdougm * get_node_attr(node, tag) 20286185db85Sdougm * 2029da6c28aaSamw * Get the specified tag(attribute) if it exists on the node. This is 20306185db85Sdougm * used internally by a number of attribute oriented functions. 20316185db85Sdougm */ 20326185db85Sdougm 20336185db85Sdougm static char * 20346185db85Sdougm get_node_attr(void *nodehdl, char *tag) 20356185db85Sdougm { 20366185db85Sdougm xmlNodePtr node = (xmlNodePtr)nodehdl; 20376185db85Sdougm xmlChar *name = NULL; 20386185db85Sdougm 203957b448deSdougm if (node != NULL) 20406185db85Sdougm name = xmlGetProp(node, (xmlChar *)tag); 20416185db85Sdougm return ((char *)name); 20426185db85Sdougm } 20436185db85Sdougm 20446185db85Sdougm /* 20456185db85Sdougm * get_node_attr(node, tag) 20466185db85Sdougm * 2047da6c28aaSamw * Set the specified tag(attribute) to the specified value This is 20486185db85Sdougm * used internally by a number of attribute oriented functions. It 20496185db85Sdougm * doesn't update the repository, only the internal document state. 20506185db85Sdougm */ 20516185db85Sdougm 20526185db85Sdougm void 20536185db85Sdougm set_node_attr(void *nodehdl, char *tag, char *value) 20546185db85Sdougm { 20556185db85Sdougm xmlNodePtr node = (xmlNodePtr)nodehdl; 20566185db85Sdougm if (node != NULL && tag != NULL) { 205757b448deSdougm if (value != NULL) 20584bff34e3Sthurlow (void) xmlSetProp(node, (xmlChar *)tag, 20594bff34e3Sthurlow (xmlChar *)value); 206057b448deSdougm else 20614bff34e3Sthurlow (void) xmlUnsetProp(node, (xmlChar *)tag); 20626185db85Sdougm } 20636185db85Sdougm } 20646185db85Sdougm 20656185db85Sdougm /* 20666185db85Sdougm * sa_get_group_attr(group, tag) 20676185db85Sdougm * 20686185db85Sdougm * Get the specied attribute, if defined, for the group. 20696185db85Sdougm */ 20706185db85Sdougm 20716185db85Sdougm char * 20726185db85Sdougm sa_get_group_attr(sa_group_t group, char *tag) 20736185db85Sdougm { 20746185db85Sdougm return (get_node_attr((void *)group, tag)); 20756185db85Sdougm } 20766185db85Sdougm 20776185db85Sdougm /* 20786185db85Sdougm * sa_set_group_attr(group, tag, value) 20796185db85Sdougm * 20806185db85Sdougm * set the specified tag/attribute on the group using value as its 20816185db85Sdougm * value. 20826185db85Sdougm * 20836185db85Sdougm * This will result in setting the property in the SMF repository as 20846185db85Sdougm * well as in the internal document. 20856185db85Sdougm */ 20866185db85Sdougm 20876185db85Sdougm int 20886185db85Sdougm sa_set_group_attr(sa_group_t group, char *tag, char *value) 20896185db85Sdougm { 20906185db85Sdougm int ret; 20916185db85Sdougm char *groupname; 2092549ec3ffSdougm sa_handle_impl_t impl_handle; 20936185db85Sdougm 2094da6c28aaSamw /* 2095da6c28aaSamw * ZFS group/subgroup doesn't need the handle so shortcut. 2096da6c28aaSamw */ 2097da6c28aaSamw if (sa_group_is_zfs(group)) { 2098da6c28aaSamw set_node_attr((void *)group, tag, value); 2099da6c28aaSamw return (SA_OK); 2100da6c28aaSamw } 2101da6c28aaSamw 2102549ec3ffSdougm impl_handle = (sa_handle_impl_t)sa_find_group_handle(group); 2103549ec3ffSdougm if (impl_handle != NULL) { 21046185db85Sdougm groupname = sa_get_group_attr(group, "name"); 2105549ec3ffSdougm ret = sa_get_instance(impl_handle->scfhandle, groupname); 21066185db85Sdougm if (ret == SA_OK) { 21076185db85Sdougm set_node_attr((void *)group, tag, value); 210857b448deSdougm ret = sa_start_transaction(impl_handle->scfhandle, 210957b448deSdougm "operation"); 21106185db85Sdougm if (ret == SA_OK) { 211157b448deSdougm ret = sa_set_property(impl_handle->scfhandle, 211257b448deSdougm tag, value); 21136185db85Sdougm if (ret == SA_OK) 2114573b0c00Sdougm ret = sa_end_transaction( 21155b6e0c46Sdougm impl_handle->scfhandle, 21165b6e0c46Sdougm impl_handle); 211757b448deSdougm else 211857b448deSdougm sa_abort_transaction( 211957b448deSdougm impl_handle->scfhandle); 21206185db85Sdougm } 2121573b0c00Sdougm if (ret == SA_SYSTEM_ERR) 2122573b0c00Sdougm ret = SA_NO_PERMISSION; 21236185db85Sdougm } 21246185db85Sdougm if (groupname != NULL) 21256185db85Sdougm sa_free_attr_string(groupname); 2126549ec3ffSdougm } else { 2127549ec3ffSdougm ret = SA_SYSTEM_ERR; 2128549ec3ffSdougm } 21296185db85Sdougm return (ret); 21306185db85Sdougm } 21316185db85Sdougm 21326185db85Sdougm /* 21336185db85Sdougm * sa_get_share_attr(share, tag) 21346185db85Sdougm * 21356185db85Sdougm * Return the value of the tag/attribute set on the specified 21366185db85Sdougm * share. Returns NULL if the tag doesn't exist. 21376185db85Sdougm */ 21386185db85Sdougm 21396185db85Sdougm char * 21406185db85Sdougm sa_get_share_attr(sa_share_t share, char *tag) 21416185db85Sdougm { 21426185db85Sdougm return (get_node_attr((void *)share, tag)); 21436185db85Sdougm } 21446185db85Sdougm 21456185db85Sdougm /* 21466185db85Sdougm * _sa_set_share_description(share, description) 21476185db85Sdougm * 2148da6c28aaSamw * Add a description tag with text contents to the specified share. A 2149da6c28aaSamw * separate XML tag is used rather than a property. This can also be 2150da6c28aaSamw * used with resources. 21516185db85Sdougm */ 21526185db85Sdougm 21536185db85Sdougm xmlNodePtr 2154da6c28aaSamw _sa_set_share_description(void *share, char *content) 21556185db85Sdougm { 21566185db85Sdougm xmlNodePtr node; 215757b448deSdougm node = xmlNewChild((xmlNodePtr)share, NULL, (xmlChar *)"description", 215857b448deSdougm NULL); 21596185db85Sdougm xmlNodeSetContent(node, (xmlChar *)content); 21606185db85Sdougm return (node); 21616185db85Sdougm } 21626185db85Sdougm 21636185db85Sdougm /* 21646185db85Sdougm * sa_set_share_attr(share, tag, value) 21656185db85Sdougm * 21666185db85Sdougm * Set the share attribute specified by tag to the specified value. In 21676185db85Sdougm * the case of "resource", enforce a no duplicates in a group rule. If 21686185db85Sdougm * the share is not transient, commit the changes to the repository 21696185db85Sdougm * else just update the share internally. 21706185db85Sdougm */ 21716185db85Sdougm 21726185db85Sdougm int 21736185db85Sdougm sa_set_share_attr(sa_share_t share, char *tag, char *value) 21746185db85Sdougm { 21756185db85Sdougm sa_group_t group; 21766185db85Sdougm sa_share_t resource; 21776185db85Sdougm int ret = SA_OK; 21786185db85Sdougm 21796185db85Sdougm group = sa_get_parent_group(share); 21806185db85Sdougm 21816185db85Sdougm /* 21826185db85Sdougm * There are some attributes that may have specific 21836185db85Sdougm * restrictions on them. Initially, only "resource" has 21846185db85Sdougm * special meaning that needs to be checked. Only one instance 21856185db85Sdougm * of a resource name may exist within a group. 21866185db85Sdougm */ 21876185db85Sdougm 21886185db85Sdougm if (strcmp(tag, "resource") == 0) { 21896185db85Sdougm resource = sa_get_resource(group, value); 21906185db85Sdougm if (resource != share && resource != NULL) 21916185db85Sdougm ret = SA_DUPLICATE_NAME; 21926185db85Sdougm } 21936185db85Sdougm if (ret == SA_OK) { 21946185db85Sdougm set_node_attr((void *)share, tag, value); 21956185db85Sdougm if (group != NULL) { 21966185db85Sdougm char *type; 21976185db85Sdougm /* we can probably optimize this some */ 21986185db85Sdougm type = sa_get_share_attr(share, "type"); 2199549ec3ffSdougm if (type == NULL || strcmp(type, "transient") != 0) { 2200549ec3ffSdougm sa_handle_impl_t impl_handle; 220157b448deSdougm impl_handle = 220257b448deSdougm (sa_handle_impl_t)sa_find_group_handle( 220357b448deSdougm group); 220457b448deSdougm if (impl_handle != NULL) { 220557b448deSdougm ret = sa_commit_share( 220657b448deSdougm impl_handle->scfhandle, group, 220757b448deSdougm share); 220857b448deSdougm } else { 2209549ec3ffSdougm ret = SA_SYSTEM_ERR; 2210549ec3ffSdougm } 221157b448deSdougm } 22126185db85Sdougm if (type != NULL) 22136185db85Sdougm sa_free_attr_string(type); 22146185db85Sdougm } 22156185db85Sdougm } 22166185db85Sdougm return (ret); 22176185db85Sdougm } 22186185db85Sdougm 22196185db85Sdougm /* 22206185db85Sdougm * sa_get_property_attr(prop, tag) 22216185db85Sdougm * 22226185db85Sdougm * Get the value of the specified property attribute. Standard 22236185db85Sdougm * attributes are "type" and "value". 22246185db85Sdougm */ 22256185db85Sdougm 22266185db85Sdougm char * 22276185db85Sdougm sa_get_property_attr(sa_property_t prop, char *tag) 22286185db85Sdougm { 22296185db85Sdougm return (get_node_attr((void *)prop, tag)); 22306185db85Sdougm } 22316185db85Sdougm 22326185db85Sdougm /* 22336185db85Sdougm * sa_get_optionset_attr(prop, tag) 22346185db85Sdougm * 22356185db85Sdougm * Get the value of the specified property attribute. Standard 22366185db85Sdougm * attribute is "type". 22376185db85Sdougm */ 22386185db85Sdougm 22396185db85Sdougm char * 22406185db85Sdougm sa_get_optionset_attr(sa_property_t optionset, char *tag) 22416185db85Sdougm { 22426185db85Sdougm return (get_node_attr((void *)optionset, tag)); 22436185db85Sdougm 22446185db85Sdougm } 22456185db85Sdougm 22466185db85Sdougm /* 22476185db85Sdougm * sa_set_optionset_attr(optionset, tag, value) 22486185db85Sdougm * 22496185db85Sdougm * Set the specified attribute(tag) to the specified value on the 22506185db85Sdougm * optionset. 22516185db85Sdougm */ 22526185db85Sdougm 22536185db85Sdougm void 22546185db85Sdougm sa_set_optionset_attr(sa_group_t optionset, char *tag, char *value) 22556185db85Sdougm { 22566185db85Sdougm set_node_attr((void *)optionset, tag, value); 22576185db85Sdougm } 22586185db85Sdougm 22596185db85Sdougm /* 22606185db85Sdougm * sa_free_attr_string(string) 22616185db85Sdougm * 22626185db85Sdougm * Free the string that was returned in one of the sa_get_*_attr() 22636185db85Sdougm * functions. 22646185db85Sdougm */ 22656185db85Sdougm 22666185db85Sdougm void 22676185db85Sdougm sa_free_attr_string(char *string) 22686185db85Sdougm { 22696185db85Sdougm xmlFree((xmlChar *)string); 22706185db85Sdougm } 22716185db85Sdougm 22726185db85Sdougm /* 22736185db85Sdougm * sa_get_optionset(group, proto) 22746185db85Sdougm * 22756185db85Sdougm * Return the optionset, if it exists, that is associated with the 22766185db85Sdougm * specified protocol. 22776185db85Sdougm */ 22786185db85Sdougm 22796185db85Sdougm sa_optionset_t 22806185db85Sdougm sa_get_optionset(void *group, char *proto) 22816185db85Sdougm { 22826185db85Sdougm xmlNodePtr node; 22836185db85Sdougm xmlChar *value = NULL; 22846185db85Sdougm 22856185db85Sdougm for (node = ((xmlNodePtr)group)->children; node != NULL; 22866185db85Sdougm node = node->next) { 22876185db85Sdougm if (xmlStrcmp(node->name, (xmlChar *)"optionset") == 0) { 22886185db85Sdougm value = xmlGetProp(node, (xmlChar *)"type"); 22896185db85Sdougm if (proto != NULL) { 22906185db85Sdougm if (value != NULL && 22916185db85Sdougm xmlStrcmp(value, (xmlChar *)proto) == 0) { 22926185db85Sdougm break; 22936185db85Sdougm } 22946185db85Sdougm if (value != NULL) { 22956185db85Sdougm xmlFree(value); 22966185db85Sdougm value = NULL; 22976185db85Sdougm } 22986185db85Sdougm } else { 22996185db85Sdougm break; 23006185db85Sdougm } 23016185db85Sdougm } 23026185db85Sdougm } 23036185db85Sdougm if (value != NULL) 23046185db85Sdougm xmlFree(value); 23056185db85Sdougm return ((sa_optionset_t)node); 23066185db85Sdougm } 23076185db85Sdougm 23086185db85Sdougm /* 23096185db85Sdougm * sa_get_next_optionset(optionset) 23106185db85Sdougm * 23116185db85Sdougm * Return the next optionset in the group. NULL if this was the last. 23126185db85Sdougm */ 23136185db85Sdougm 23146185db85Sdougm sa_optionset_t 23156185db85Sdougm sa_get_next_optionset(sa_optionset_t optionset) 23166185db85Sdougm { 23176185db85Sdougm xmlNodePtr node; 23186185db85Sdougm 23196185db85Sdougm for (node = ((xmlNodePtr)optionset)->next; node != NULL; 23206185db85Sdougm node = node->next) { 23216185db85Sdougm if (xmlStrcmp(node->name, (xmlChar *)"optionset") == 0) { 23226185db85Sdougm break; 23236185db85Sdougm } 23246185db85Sdougm } 23256185db85Sdougm return ((sa_optionset_t)node); 23266185db85Sdougm } 23276185db85Sdougm 23286185db85Sdougm /* 23296185db85Sdougm * sa_get_security(group, sectype, proto) 23306185db85Sdougm * 23316185db85Sdougm * Return the security optionset. The internal name is a hold over 23326185db85Sdougm * from the implementation and will be changed before the API is 23336185db85Sdougm * finalized. This is really a named optionset that can be negotiated 23346185db85Sdougm * as a group of properties (like NFS security options). 23356185db85Sdougm */ 23366185db85Sdougm 23376185db85Sdougm sa_security_t 23386185db85Sdougm sa_get_security(sa_group_t group, char *sectype, char *proto) 23396185db85Sdougm { 23406185db85Sdougm xmlNodePtr node; 23416185db85Sdougm xmlChar *value = NULL; 23426185db85Sdougm 23436185db85Sdougm for (node = ((xmlNodePtr)group)->children; node != NULL; 23446185db85Sdougm node = node->next) { 23456185db85Sdougm if (xmlStrcmp(node->name, (xmlChar *)"security") == 0) { 23466185db85Sdougm if (proto != NULL) { 23476185db85Sdougm value = xmlGetProp(node, (xmlChar *)"type"); 23486185db85Sdougm if (value == NULL || 23496185db85Sdougm (value != NULL && 23506185db85Sdougm xmlStrcmp(value, (xmlChar *)proto) != 0)) { 23516185db85Sdougm /* it doesn't match so continue */ 23526185db85Sdougm xmlFree(value); 23536185db85Sdougm value = NULL; 23546185db85Sdougm continue; 23556185db85Sdougm } 23566185db85Sdougm } 23576185db85Sdougm if (value != NULL) { 23586185db85Sdougm xmlFree(value); 23596185db85Sdougm value = NULL; 23606185db85Sdougm } 23616185db85Sdougm /* potential match */ 23626185db85Sdougm if (sectype != NULL) { 23636185db85Sdougm value = xmlGetProp(node, (xmlChar *)"sectype"); 23646185db85Sdougm if (value != NULL && 23656185db85Sdougm xmlStrcmp(value, (xmlChar *)sectype) == 0) { 23666185db85Sdougm break; 23676185db85Sdougm } 23686185db85Sdougm } else { 23696185db85Sdougm break; 23706185db85Sdougm } 23716185db85Sdougm } 23726185db85Sdougm if (value != NULL) { 23736185db85Sdougm xmlFree(value); 23746185db85Sdougm value = NULL; 23756185db85Sdougm } 23766185db85Sdougm } 23776185db85Sdougm if (value != NULL) 23786185db85Sdougm xmlFree(value); 23796185db85Sdougm return ((sa_security_t)node); 23806185db85Sdougm } 23816185db85Sdougm 23826185db85Sdougm /* 23836185db85Sdougm * sa_get_next_security(security) 23846185db85Sdougm * 23856185db85Sdougm * Get the next security optionset if one exists. 23866185db85Sdougm */ 23876185db85Sdougm 23886185db85Sdougm sa_security_t 23896185db85Sdougm sa_get_next_security(sa_security_t security) 23906185db85Sdougm { 23916185db85Sdougm xmlNodePtr node; 23926185db85Sdougm 23936185db85Sdougm for (node = ((xmlNodePtr)security)->next; node != NULL; 23946185db85Sdougm node = node->next) { 23956185db85Sdougm if (xmlStrcmp(node->name, (xmlChar *)"security") == 0) { 23966185db85Sdougm break; 23976185db85Sdougm } 23986185db85Sdougm } 23996185db85Sdougm return ((sa_security_t)node); 24006185db85Sdougm } 24016185db85Sdougm 24026185db85Sdougm /* 24036185db85Sdougm * sa_get_property(optionset, prop) 24046185db85Sdougm * 24056185db85Sdougm * Get the property object with the name specified in prop from the 24066185db85Sdougm * optionset. 24076185db85Sdougm */ 24086185db85Sdougm 24096185db85Sdougm sa_property_t 24106185db85Sdougm sa_get_property(sa_optionset_t optionset, char *prop) 24116185db85Sdougm { 24126185db85Sdougm xmlNodePtr node = (xmlNodePtr)optionset; 24136185db85Sdougm xmlChar *value = NULL; 24146185db85Sdougm 24156185db85Sdougm if (optionset == NULL) 24166185db85Sdougm return (NULL); 24176185db85Sdougm 24186185db85Sdougm for (node = node->children; node != NULL; 24196185db85Sdougm node = node->next) { 24206185db85Sdougm if (xmlStrcmp(node->name, (xmlChar *)"option") == 0) { 24216185db85Sdougm if (prop == NULL) 24226185db85Sdougm break; 24236185db85Sdougm value = xmlGetProp(node, (xmlChar *)"type"); 242457b448deSdougm if (value != NULL && 242557b448deSdougm xmlStrcmp(value, (xmlChar *)prop) == 0) { 24266185db85Sdougm break; 24276185db85Sdougm } 24286185db85Sdougm if (value != NULL) { 24296185db85Sdougm xmlFree(value); 24306185db85Sdougm value = NULL; 24316185db85Sdougm } 24326185db85Sdougm } 24336185db85Sdougm } 24346185db85Sdougm if (value != NULL) 24356185db85Sdougm xmlFree(value); 24366185db85Sdougm if (node != NULL && xmlStrcmp(node->name, (xmlChar *)"option") != 0) { 243757b448deSdougm /* 243857b448deSdougm * avoid a non option node -- it is possible to be a 243957b448deSdougm * text node 244057b448deSdougm */ 24416185db85Sdougm node = NULL; 24426185db85Sdougm } 24436185db85Sdougm return ((sa_property_t)node); 24446185db85Sdougm } 24456185db85Sdougm 24466185db85Sdougm /* 24476185db85Sdougm * sa_get_next_property(property) 24486185db85Sdougm * 24496185db85Sdougm * Get the next property following the specified property. NULL if 24506185db85Sdougm * this was the last. 24516185db85Sdougm */ 24526185db85Sdougm 24536185db85Sdougm sa_property_t 24546185db85Sdougm sa_get_next_property(sa_property_t property) 24556185db85Sdougm { 24566185db85Sdougm xmlNodePtr node; 24576185db85Sdougm 24586185db85Sdougm for (node = ((xmlNodePtr)property)->next; node != NULL; 24596185db85Sdougm node = node->next) { 24606185db85Sdougm if (xmlStrcmp(node->name, (xmlChar *)"option") == 0) { 24616185db85Sdougm break; 24626185db85Sdougm } 24636185db85Sdougm } 24646185db85Sdougm return ((sa_property_t)node); 24656185db85Sdougm } 24666185db85Sdougm 24676185db85Sdougm /* 24686185db85Sdougm * sa_set_share_description(share, content) 24696185db85Sdougm * 24706185db85Sdougm * Set the description of share to content. 24716185db85Sdougm */ 24726185db85Sdougm 24736185db85Sdougm int 24746185db85Sdougm sa_set_share_description(sa_share_t share, char *content) 24756185db85Sdougm { 24766185db85Sdougm xmlNodePtr node; 24776185db85Sdougm sa_group_t group; 24786185db85Sdougm int ret = SA_OK; 24796185db85Sdougm 24806185db85Sdougm for (node = ((xmlNodePtr)share)->children; node != NULL; 24816185db85Sdougm node = node->next) { 24826185db85Sdougm if (xmlStrcmp(node->name, (xmlChar *)"description") == 0) { 24836185db85Sdougm break; 24846185db85Sdougm } 24856185db85Sdougm } 24866185db85Sdougm /* no existing description but want to add */ 24876185db85Sdougm if (node == NULL && content != NULL) { 24886185db85Sdougm /* add a description */ 24896185db85Sdougm node = _sa_set_share_description(share, content); 24906185db85Sdougm } else if (node != NULL && content != NULL) { 24916185db85Sdougm /* update a description */ 24926185db85Sdougm xmlNodeSetContent(node, (xmlChar *)content); 24936185db85Sdougm } else if (node != NULL && content == NULL) { 24946185db85Sdougm /* remove an existing description */ 24956185db85Sdougm xmlUnlinkNode(node); 24966185db85Sdougm xmlFreeNode(node); 24976185db85Sdougm } 2498da6c28aaSamw group = sa_get_parent_group(share); 2499da6c28aaSamw if (group != NULL && sa_is_persistent(share)) { 2500549ec3ffSdougm sa_handle_impl_t impl_handle; 2501549ec3ffSdougm impl_handle = (sa_handle_impl_t)sa_find_group_handle(group); 250257b448deSdougm if (impl_handle != NULL) { 250357b448deSdougm ret = sa_commit_share(impl_handle->scfhandle, group, 250457b448deSdougm share); 250557b448deSdougm } else { 2506549ec3ffSdougm ret = SA_SYSTEM_ERR; 2507549ec3ffSdougm } 250857b448deSdougm } 25096185db85Sdougm return (ret); 25106185db85Sdougm } 25116185db85Sdougm 25126185db85Sdougm /* 25136185db85Sdougm * fixproblemchars(string) 25146185db85Sdougm * 25156185db85Sdougm * don't want any newline or tab characters in the text since these 25166185db85Sdougm * could break display of data and legacy file formats. 25176185db85Sdougm */ 25186185db85Sdougm static void 25196185db85Sdougm fixproblemchars(char *str) 25206185db85Sdougm { 25216185db85Sdougm int c; 25226185db85Sdougm for (c = *str; c != '\0'; c = *++str) { 25236185db85Sdougm if (c == '\t' || c == '\n') 25246185db85Sdougm *str = ' '; 25256185db85Sdougm else if (c == '"') 25266185db85Sdougm *str = '\''; 25276185db85Sdougm } 25286185db85Sdougm } 25296185db85Sdougm 25306185db85Sdougm /* 25316185db85Sdougm * sa_get_share_description(share) 25326185db85Sdougm * 25336185db85Sdougm * Return the description text for the specified share if it 25346185db85Sdougm * exists. NULL if no description exists. 25356185db85Sdougm */ 25366185db85Sdougm 25376185db85Sdougm char * 25386185db85Sdougm sa_get_share_description(sa_share_t share) 25396185db85Sdougm { 25406185db85Sdougm xmlChar *description = NULL; 25416185db85Sdougm xmlNodePtr node; 25426185db85Sdougm 25436185db85Sdougm for (node = ((xmlNodePtr)share)->children; node != NULL; 25446185db85Sdougm node = node->next) { 25456185db85Sdougm if (xmlStrcmp(node->name, (xmlChar *)"description") == 0) { 25466185db85Sdougm break; 25476185db85Sdougm } 25486185db85Sdougm } 25496185db85Sdougm if (node != NULL) { 2550da6c28aaSamw description = xmlNodeGetContent(node); 25516185db85Sdougm fixproblemchars((char *)description); 25526185db85Sdougm } 25536185db85Sdougm return ((char *)description); 25546185db85Sdougm } 25556185db85Sdougm 25566185db85Sdougm /* 25576185db85Sdougm * sa_free(share_description(description) 25586185db85Sdougm * 25596185db85Sdougm * Free the description string. 25606185db85Sdougm */ 25616185db85Sdougm 25626185db85Sdougm void 25636185db85Sdougm sa_free_share_description(char *description) 25646185db85Sdougm { 25656185db85Sdougm xmlFree((xmlChar *)description); 25666185db85Sdougm } 25676185db85Sdougm 25686185db85Sdougm /* 25696185db85Sdougm * sa_create_optionset(group, proto) 25706185db85Sdougm * 25716185db85Sdougm * Create an optionset for the specified protocol in the specied 25726185db85Sdougm * group. This is manifested as a property group within SMF. 25736185db85Sdougm */ 25746185db85Sdougm 25756185db85Sdougm sa_optionset_t 25766185db85Sdougm sa_create_optionset(sa_group_t group, char *proto) 25776185db85Sdougm { 25786185db85Sdougm sa_optionset_t optionset; 25796185db85Sdougm sa_group_t parent = group; 2580da6c28aaSamw sa_share_t share = NULL; 2581da6c28aaSamw int err = SA_OK; 2582da6c28aaSamw char *id = NULL; 25836185db85Sdougm 25846185db85Sdougm optionset = sa_get_optionset(group, proto); 25856185db85Sdougm if (optionset != NULL) { 25866185db85Sdougm /* can't have a duplicate protocol */ 25876185db85Sdougm optionset = NULL; 25886185db85Sdougm } else { 2589da6c28aaSamw /* 2590da6c28aaSamw * Account for resource names being slightly 2591da6c28aaSamw * different. 2592da6c28aaSamw */ 2593da6c28aaSamw if (sa_is_share(group)) { 2594da6c28aaSamw /* 2595da6c28aaSamw * Transient shares do not have an "id" so not an 2596da6c28aaSamw * error to not find one. 2597da6c28aaSamw */ 2598da6c28aaSamw id = sa_get_share_attr((sa_share_t)group, "id"); 2599da6c28aaSamw } else if (sa_is_resource(group)) { 2600da6c28aaSamw share = sa_get_resource_parent( 2601da6c28aaSamw (sa_resource_t)group); 2602da6c28aaSamw id = sa_get_resource_attr(share, "id"); 2603da6c28aaSamw 2604da6c28aaSamw /* id can be NULL if the group is transient (ZFS) */ 2605da6c28aaSamw if (id == NULL && sa_is_persistent(group)) 2606da6c28aaSamw err = SA_NO_MEMORY; 2607da6c28aaSamw } 2608da6c28aaSamw if (err == SA_NO_MEMORY) { 2609da6c28aaSamw /* 2610da6c28aaSamw * Couldn't get the id for the share or 2611da6c28aaSamw * resource. While this could be a 2612da6c28aaSamw * configuration issue, it is most likely an 2613da6c28aaSamw * out of memory. In any case, fail the create. 2614da6c28aaSamw */ 2615da6c28aaSamw return (NULL); 2616da6c28aaSamw } 2617da6c28aaSamw 26186185db85Sdougm optionset = (sa_optionset_t)xmlNewChild((xmlNodePtr)group, 261957b448deSdougm NULL, (xmlChar *)"optionset", NULL); 26206185db85Sdougm /* 26216185db85Sdougm * only put to repository if on a group and we were 26226185db85Sdougm * able to create an optionset. 26236185db85Sdougm */ 26246185db85Sdougm if (optionset != NULL) { 262557b448deSdougm char oname[SA_STRSIZE]; 26266185db85Sdougm char *groupname; 26276185db85Sdougm 2628da6c28aaSamw /* 2629da6c28aaSamw * Need to get parent group in all cases, but also get 2630da6c28aaSamw * the share if this is a resource. 2631da6c28aaSamw */ 2632da6c28aaSamw if (sa_is_share(group)) { 26336185db85Sdougm parent = sa_get_parent_group((sa_share_t)group); 2634da6c28aaSamw } else if (sa_is_resource(group)) { 2635da6c28aaSamw share = sa_get_resource_parent( 2636da6c28aaSamw (sa_resource_t)group); 2637da6c28aaSamw parent = sa_get_parent_group(share); 2638da6c28aaSamw } 26396185db85Sdougm 26406185db85Sdougm sa_set_optionset_attr(optionset, "type", proto); 26416185db85Sdougm 26426185db85Sdougm (void) sa_optionset_name(optionset, oname, 26436185db85Sdougm sizeof (oname), id); 26446185db85Sdougm groupname = sa_get_group_attr(parent, "name"); 2645da6c28aaSamw if (groupname != NULL && sa_is_persistent(group)) { 2646549ec3ffSdougm sa_handle_impl_t impl_handle; 2647da6c28aaSamw impl_handle = 2648da6c28aaSamw (sa_handle_impl_t)sa_find_group_handle( 2649da6c28aaSamw group); 2650549ec3ffSdougm assert(impl_handle != NULL); 2651549ec3ffSdougm if (impl_handle != NULL) { 265257b448deSdougm (void) sa_get_instance( 2653da6c28aaSamw impl_handle->scfhandle, groupname); 265457b448deSdougm (void) sa_create_pgroup( 265557b448deSdougm impl_handle->scfhandle, oname); 26566185db85Sdougm } 2657549ec3ffSdougm } 2658549ec3ffSdougm if (groupname != NULL) 2659549ec3ffSdougm sa_free_attr_string(groupname); 2660da6c28aaSamw } 2661da6c28aaSamw } 2662da6c28aaSamw 26636185db85Sdougm if (id != NULL) 26646185db85Sdougm sa_free_attr_string(id); 26656185db85Sdougm return (optionset); 26666185db85Sdougm } 26676185db85Sdougm 26686185db85Sdougm /* 26696185db85Sdougm * sa_get_property_parent(property) 26706185db85Sdougm * 26716185db85Sdougm * Given a property, return the object it is a property of. This will 26726185db85Sdougm * be an optionset of some type. 26736185db85Sdougm */ 26746185db85Sdougm 26756185db85Sdougm static sa_optionset_t 26766185db85Sdougm sa_get_property_parent(sa_property_t property) 26776185db85Sdougm { 26786185db85Sdougm xmlNodePtr node = NULL; 26796185db85Sdougm 268057b448deSdougm if (property != NULL) 26816185db85Sdougm node = ((xmlNodePtr)property)->parent; 26826185db85Sdougm return ((sa_optionset_t)node); 26836185db85Sdougm } 26846185db85Sdougm 26856185db85Sdougm /* 26866185db85Sdougm * sa_get_optionset_parent(optionset) 26876185db85Sdougm * 26886185db85Sdougm * Return the parent of the specified optionset. This could be a group 26896185db85Sdougm * or a share. 26906185db85Sdougm */ 26916185db85Sdougm 26926185db85Sdougm static sa_group_t 26936185db85Sdougm sa_get_optionset_parent(sa_optionset_t optionset) 26946185db85Sdougm { 26956185db85Sdougm xmlNodePtr node = NULL; 26966185db85Sdougm 269757b448deSdougm if (optionset != NULL) 26986185db85Sdougm node = ((xmlNodePtr)optionset)->parent; 26996185db85Sdougm return ((sa_group_t)node); 27006185db85Sdougm } 27016185db85Sdougm 27026185db85Sdougm /* 27036185db85Sdougm * zfs_needs_update(share) 27046185db85Sdougm * 27056185db85Sdougm * In order to avoid making multiple updates to a ZFS share when 27066185db85Sdougm * setting properties, the share attribute "changed" will be set to 2707da6c28aaSamw * true when a property is added or modified. When done adding 27086185db85Sdougm * properties, we can then detect that an update is needed. We then 27096185db85Sdougm * clear the state here to detect additional changes. 27106185db85Sdougm */ 27116185db85Sdougm 27126185db85Sdougm static int 27136185db85Sdougm zfs_needs_update(sa_share_t share) 27146185db85Sdougm { 27156185db85Sdougm char *attr; 27166185db85Sdougm int result = 0; 27176185db85Sdougm 27186185db85Sdougm attr = sa_get_share_attr(share, "changed"); 27196185db85Sdougm if (attr != NULL) { 27206185db85Sdougm sa_free_attr_string(attr); 27216185db85Sdougm result = 1; 27226185db85Sdougm } 27236185db85Sdougm set_node_attr((void *)share, "changed", NULL); 27246185db85Sdougm return (result); 27256185db85Sdougm } 27266185db85Sdougm 27276185db85Sdougm /* 27286185db85Sdougm * zfs_set_update(share) 27296185db85Sdougm * 27306185db85Sdougm * Set the changed attribute of the share to true. 27316185db85Sdougm */ 27326185db85Sdougm 27336185db85Sdougm static void 27346185db85Sdougm zfs_set_update(sa_share_t share) 27356185db85Sdougm { 27366185db85Sdougm set_node_attr((void *)share, "changed", "true"); 27376185db85Sdougm } 27386185db85Sdougm 27396185db85Sdougm /* 27406185db85Sdougm * sa_commit_properties(optionset, clear) 27416185db85Sdougm * 27426185db85Sdougm * Check if SMF or ZFS config and either update or abort the pending 27436185db85Sdougm * changes. 27446185db85Sdougm */ 27456185db85Sdougm 27466185db85Sdougm int 27476185db85Sdougm sa_commit_properties(sa_optionset_t optionset, int clear) 27486185db85Sdougm { 27496185db85Sdougm sa_group_t group; 27506185db85Sdougm sa_group_t parent; 27516185db85Sdougm int zfs = 0; 27526185db85Sdougm int needsupdate = 0; 27536185db85Sdougm int ret = SA_OK; 2754549ec3ffSdougm sa_handle_impl_t impl_handle; 27556185db85Sdougm 27566185db85Sdougm group = sa_get_optionset_parent(optionset); 27576185db85Sdougm if (group != NULL && (sa_is_share(group) || is_zfs_group(group))) { 27586185db85Sdougm /* only update ZFS if on a share */ 27596185db85Sdougm parent = sa_get_parent_group(group); 27606185db85Sdougm zfs++; 276157b448deSdougm if (parent != NULL && is_zfs_group(parent)) 27626185db85Sdougm needsupdate = zfs_needs_update(group); 276357b448deSdougm else 27646185db85Sdougm zfs = 0; 27656185db85Sdougm } 27666185db85Sdougm if (zfs) { 27676185db85Sdougm if (!clear && needsupdate) 27686185db85Sdougm ret = sa_zfs_update((sa_share_t)group); 27696185db85Sdougm } else { 2770549ec3ffSdougm impl_handle = (sa_handle_impl_t)sa_find_group_handle(group); 2771549ec3ffSdougm if (impl_handle != NULL) { 277257b448deSdougm if (clear) { 277357b448deSdougm (void) sa_abort_transaction( 277457b448deSdougm impl_handle->scfhandle); 277557b448deSdougm } else { 277657b448deSdougm ret = sa_end_transaction( 27775b6e0c46Sdougm impl_handle->scfhandle, impl_handle); 277857b448deSdougm } 2779549ec3ffSdougm } else { 2780549ec3ffSdougm ret = SA_SYSTEM_ERR; 2781549ec3ffSdougm } 27826185db85Sdougm } 27836185db85Sdougm return (ret); 27846185db85Sdougm } 27856185db85Sdougm 27866185db85Sdougm /* 27876185db85Sdougm * sa_destroy_optionset(optionset) 27886185db85Sdougm * 2789da6c28aaSamw * Remove the optionset from its group. Update the repository to 27906185db85Sdougm * reflect this change. 27916185db85Sdougm */ 27926185db85Sdougm 27936185db85Sdougm int 27946185db85Sdougm sa_destroy_optionset(sa_optionset_t optionset) 27956185db85Sdougm { 279657b448deSdougm char name[SA_STRSIZE]; 27976185db85Sdougm int len; 27986185db85Sdougm int ret; 27996185db85Sdougm char *id = NULL; 28006185db85Sdougm sa_group_t group; 28016185db85Sdougm int ispersist = 1; 28026185db85Sdougm 28036185db85Sdougm /* now delete the prop group */ 28046185db85Sdougm group = sa_get_optionset_parent(optionset); 2805da6c28aaSamw if (group != NULL) { 2806da6c28aaSamw if (sa_is_resource(group)) { 2807da6c28aaSamw sa_resource_t resource = group; 2808da6c28aaSamw sa_share_t share = sa_get_resource_parent(resource); 2809da6c28aaSamw group = sa_get_parent_group(share); 2810da6c28aaSamw id = sa_get_share_attr(share, "id"); 2811da6c28aaSamw } else if (sa_is_share(group)) { 28126185db85Sdougm id = sa_get_share_attr((sa_share_t)group, "id"); 28136185db85Sdougm } 2814da6c28aaSamw ispersist = sa_is_persistent(group); 2815da6c28aaSamw } 28166185db85Sdougm if (ispersist) { 2817549ec3ffSdougm sa_handle_impl_t impl_handle; 28186185db85Sdougm len = sa_optionset_name(optionset, name, sizeof (name), id); 2819549ec3ffSdougm impl_handle = (sa_handle_impl_t)sa_find_group_handle(group); 2820549ec3ffSdougm if (impl_handle != NULL) { 28216185db85Sdougm if (len > 0) { 282257b448deSdougm ret = sa_delete_pgroup(impl_handle->scfhandle, 282357b448deSdougm name); 2824549ec3ffSdougm } 2825549ec3ffSdougm } else { 2826549ec3ffSdougm ret = SA_SYSTEM_ERR; 28276185db85Sdougm } 28286185db85Sdougm } 28296185db85Sdougm xmlUnlinkNode((xmlNodePtr)optionset); 28306185db85Sdougm xmlFreeNode((xmlNodePtr)optionset); 28316185db85Sdougm if (id != NULL) 28326185db85Sdougm sa_free_attr_string(id); 28336185db85Sdougm return (ret); 28346185db85Sdougm } 28356185db85Sdougm 28366185db85Sdougm /* private to the implementation */ 28376185db85Sdougm int 28386185db85Sdougm _sa_remove_optionset(sa_optionset_t optionset) 28396185db85Sdougm { 28406185db85Sdougm int ret = SA_OK; 28416185db85Sdougm 28426185db85Sdougm xmlUnlinkNode((xmlNodePtr)optionset); 28436185db85Sdougm xmlFreeNode((xmlNodePtr)optionset); 28446185db85Sdougm return (ret); 28456185db85Sdougm } 28466185db85Sdougm 28476185db85Sdougm /* 28486185db85Sdougm * sa_create_security(group, sectype, proto) 28496185db85Sdougm * 28506185db85Sdougm * Create a security optionset (one that has a type name and a 28516185db85Sdougm * proto). Security is left over from a pure NFS implementation. The 28526185db85Sdougm * naming will change in the future when the API is released. 28536185db85Sdougm */ 28546185db85Sdougm sa_security_t 28556185db85Sdougm sa_create_security(sa_group_t group, char *sectype, char *proto) 28566185db85Sdougm { 28576185db85Sdougm sa_security_t security; 28586185db85Sdougm char *id = NULL; 28596185db85Sdougm sa_group_t parent; 28606185db85Sdougm char *groupname = NULL; 28616185db85Sdougm 28626185db85Sdougm if (group != NULL && sa_is_share(group)) { 28636185db85Sdougm id = sa_get_share_attr((sa_share_t)group, "id"); 28646185db85Sdougm parent = sa_get_parent_group(group); 28656185db85Sdougm if (parent != NULL) 28666185db85Sdougm groupname = sa_get_group_attr(parent, "name"); 28676185db85Sdougm } else if (group != NULL) { 28686185db85Sdougm groupname = sa_get_group_attr(group, "name"); 28696185db85Sdougm } 28706185db85Sdougm 28716185db85Sdougm security = sa_get_security(group, sectype, proto); 28726185db85Sdougm if (security != NULL) { 28736185db85Sdougm /* can't have a duplicate security option */ 28746185db85Sdougm security = NULL; 28756185db85Sdougm } else { 28766185db85Sdougm security = (sa_security_t)xmlNewChild((xmlNodePtr)group, 287757b448deSdougm NULL, (xmlChar *)"security", NULL); 28786185db85Sdougm if (security != NULL) { 287957b448deSdougm char oname[SA_STRSIZE]; 28806185db85Sdougm sa_set_security_attr(security, "type", proto); 28816185db85Sdougm 28826185db85Sdougm sa_set_security_attr(security, "sectype", sectype); 28836185db85Sdougm (void) sa_security_name(security, oname, 28846185db85Sdougm sizeof (oname), id); 2885da6c28aaSamw if (groupname != NULL && sa_is_persistent(group)) { 2886549ec3ffSdougm sa_handle_impl_t impl_handle; 2887549ec3ffSdougm impl_handle = 288857b448deSdougm (sa_handle_impl_t)sa_find_group_handle( 288957b448deSdougm group); 2890549ec3ffSdougm if (impl_handle != NULL) { 289157b448deSdougm (void) sa_get_instance( 289257b448deSdougm impl_handle->scfhandle, groupname); 289357b448deSdougm (void) sa_create_pgroup( 289457b448deSdougm impl_handle->scfhandle, oname); 2895549ec3ffSdougm } 28966185db85Sdougm } 28976185db85Sdougm } 28986185db85Sdougm } 28996185db85Sdougm if (groupname != NULL) 29006185db85Sdougm sa_free_attr_string(groupname); 29016185db85Sdougm return (security); 29026185db85Sdougm } 29036185db85Sdougm 29046185db85Sdougm /* 29056185db85Sdougm * sa_destroy_security(security) 29066185db85Sdougm * 29076185db85Sdougm * Remove the specified optionset from the document and the 29086185db85Sdougm * configuration. 29096185db85Sdougm */ 29106185db85Sdougm 29116185db85Sdougm int 29126185db85Sdougm sa_destroy_security(sa_security_t security) 29136185db85Sdougm { 291457b448deSdougm char name[SA_STRSIZE]; 29156185db85Sdougm int len; 29166185db85Sdougm int ret = SA_OK; 29176185db85Sdougm char *id = NULL; 29186185db85Sdougm sa_group_t group; 29196185db85Sdougm int iszfs = 0; 29206185db85Sdougm int ispersist = 1; 29216185db85Sdougm 29226185db85Sdougm group = sa_get_optionset_parent(security); 29236185db85Sdougm 29246185db85Sdougm if (group != NULL) 29256185db85Sdougm iszfs = sa_group_is_zfs(group); 29266185db85Sdougm 29276185db85Sdougm if (group != NULL && !iszfs) { 29286185db85Sdougm if (sa_is_share(group)) 2929da6c28aaSamw ispersist = sa_is_persistent(group); 29306185db85Sdougm id = sa_get_share_attr((sa_share_t)group, "id"); 29316185db85Sdougm } 29326185db85Sdougm if (ispersist) { 29336185db85Sdougm len = sa_security_name(security, name, sizeof (name), id); 29346185db85Sdougm if (!iszfs && len > 0) { 2935549ec3ffSdougm sa_handle_impl_t impl_handle; 293657b448deSdougm impl_handle = 293757b448deSdougm (sa_handle_impl_t)sa_find_group_handle(group); 2938549ec3ffSdougm if (impl_handle != NULL) { 293957b448deSdougm ret = sa_delete_pgroup(impl_handle->scfhandle, 294057b448deSdougm name); 2941549ec3ffSdougm } else { 2942549ec3ffSdougm ret = SA_SYSTEM_ERR; 2943549ec3ffSdougm } 29446185db85Sdougm } 29456185db85Sdougm } 29466185db85Sdougm xmlUnlinkNode((xmlNodePtr)security); 29476185db85Sdougm xmlFreeNode((xmlNodePtr)security); 294857b448deSdougm if (iszfs) 29496185db85Sdougm ret = sa_zfs_update(group); 29506185db85Sdougm if (id != NULL) 29516185db85Sdougm sa_free_attr_string(id); 29526185db85Sdougm return (ret); 29536185db85Sdougm } 29546185db85Sdougm 29556185db85Sdougm /* 29566185db85Sdougm * sa_get_security_attr(optionset, tag) 29576185db85Sdougm * 29586185db85Sdougm * Return the specified attribute value from the optionset. 29596185db85Sdougm */ 29606185db85Sdougm 29616185db85Sdougm char * 29626185db85Sdougm sa_get_security_attr(sa_property_t optionset, char *tag) 29636185db85Sdougm { 29646185db85Sdougm return (get_node_attr((void *)optionset, tag)); 29656185db85Sdougm 29666185db85Sdougm } 29676185db85Sdougm 29686185db85Sdougm /* 29696185db85Sdougm * sa_set_security_attr(optionset, tag, value) 29706185db85Sdougm * 29716185db85Sdougm * Set the optioset attribute specied by tag to the specified value. 29726185db85Sdougm */ 29736185db85Sdougm 29746185db85Sdougm void 29756185db85Sdougm sa_set_security_attr(sa_group_t optionset, char *tag, char *value) 29766185db85Sdougm { 29776185db85Sdougm set_node_attr((void *)optionset, tag, value); 29786185db85Sdougm } 29796185db85Sdougm 29806185db85Sdougm /* 29816185db85Sdougm * is_nodetype(node, type) 29826185db85Sdougm * 29836185db85Sdougm * Check to see if node is of the type specified. 29846185db85Sdougm */ 29856185db85Sdougm 29866185db85Sdougm static int 29876185db85Sdougm is_nodetype(void *node, char *type) 29886185db85Sdougm { 29896185db85Sdougm return (strcmp((char *)((xmlNodePtr)node)->name, type) == 0); 29906185db85Sdougm } 29916185db85Sdougm 299257b448deSdougm /* 299357b448deSdougm * add_or_update() 299457b448deSdougm * 299557b448deSdougm * Add or update a property. Pulled out of sa_set_prop_by_prop for 299657b448deSdougm * readability. 299757b448deSdougm */ 299857b448deSdougm static int 299957b448deSdougm add_or_update(scfutilhandle_t *scf_handle, int type, scf_value_t *value, 300057b448deSdougm scf_transaction_entry_t *entry, char *name, char *valstr) 300157b448deSdougm { 300257b448deSdougm int ret = SA_SYSTEM_ERR; 300357b448deSdougm 300457b448deSdougm if (value != NULL) { 300557b448deSdougm if (type == SA_PROP_OP_ADD) 300657b448deSdougm ret = scf_transaction_property_new(scf_handle->trans, 300757b448deSdougm entry, name, SCF_TYPE_ASTRING); 300857b448deSdougm else 300957b448deSdougm ret = scf_transaction_property_change(scf_handle->trans, 301057b448deSdougm entry, name, SCF_TYPE_ASTRING); 301157b448deSdougm if (ret == 0) { 301257b448deSdougm ret = scf_value_set_astring(value, valstr); 301357b448deSdougm if (ret == 0) 301457b448deSdougm ret = scf_entry_add_value(entry, value); 301557b448deSdougm if (ret == 0) 301657b448deSdougm return (ret); 301757b448deSdougm scf_value_destroy(value); 301857b448deSdougm } else { 301957b448deSdougm scf_entry_destroy(entry); 302057b448deSdougm } 302157b448deSdougm } 302257b448deSdougm return (SA_SYSTEM_ERR); 302357b448deSdougm } 302457b448deSdougm 30256185db85Sdougm /* 30266185db85Sdougm * sa_set_prop_by_prop(optionset, group, prop, type) 30276185db85Sdougm * 30286185db85Sdougm * Add/remove/update the specified property prop into the optionset or 30296185db85Sdougm * share. If a share, sort out which property group based on GUID. In 30306185db85Sdougm * all cases, the appropriate transaction is set (or ZFS share is 30316185db85Sdougm * marked as needing an update) 30326185db85Sdougm */ 30336185db85Sdougm 30346185db85Sdougm static int 30356185db85Sdougm sa_set_prop_by_prop(sa_optionset_t optionset, sa_group_t group, 30366185db85Sdougm sa_property_t prop, int type) 30376185db85Sdougm { 30386185db85Sdougm char *name; 30396185db85Sdougm char *valstr; 30406185db85Sdougm int ret = SA_OK; 30416185db85Sdougm scf_transaction_entry_t *entry; 30426185db85Sdougm scf_value_t *value; 30436185db85Sdougm int opttype; /* 1 == optionset, 0 == security */ 30446185db85Sdougm char *id = NULL; 30456185db85Sdougm int iszfs = 0; 30466185db85Sdougm sa_group_t parent = NULL; 3047da6c28aaSamw sa_share_t share = NULL; 3048549ec3ffSdougm sa_handle_impl_t impl_handle; 3049549ec3ffSdougm scfutilhandle_t *scf_handle; 30506185db85Sdougm 3051da6c28aaSamw if (!sa_is_persistent(group)) { 30526185db85Sdougm /* 30536185db85Sdougm * if the group/share is not persistent we don't need 30546185db85Sdougm * to do anything here 30556185db85Sdougm */ 30566185db85Sdougm return (SA_OK); 30576185db85Sdougm } 3058549ec3ffSdougm impl_handle = (sa_handle_impl_t)sa_find_group_handle(group); 305957b448deSdougm if (impl_handle == NULL || impl_handle->scfhandle == NULL) 3060549ec3ffSdougm return (SA_SYSTEM_ERR); 3061549ec3ffSdougm scf_handle = impl_handle->scfhandle; 30626185db85Sdougm name = sa_get_property_attr(prop, "type"); 30636185db85Sdougm valstr = sa_get_property_attr(prop, "value"); 30646185db85Sdougm entry = scf_entry_create(scf_handle->handle); 30656185db85Sdougm opttype = is_nodetype((void *)optionset, "optionset"); 30666185db85Sdougm 3067da6c28aaSamw /* 3068da6c28aaSamw * Check for share vs. resource since they need slightly 3069da6c28aaSamw * different treatment given the hierarchy. 3070da6c28aaSamw */ 30716185db85Sdougm if (valstr != NULL && entry != NULL) { 30726185db85Sdougm if (sa_is_share(group)) { 30736185db85Sdougm parent = sa_get_parent_group(group); 3074da6c28aaSamw share = (sa_share_t)group; 307557b448deSdougm if (parent != NULL) 30766185db85Sdougm iszfs = is_zfs_group(parent); 3077da6c28aaSamw } else if (sa_is_resource(group)) { 3078da6c28aaSamw share = sa_get_parent_group(group); 3079da6c28aaSamw if (share != NULL) 3080da6c28aaSamw parent = sa_get_parent_group(share); 30816185db85Sdougm } else { 30826185db85Sdougm iszfs = is_zfs_group(group); 30836185db85Sdougm } 30846185db85Sdougm if (!iszfs) { 30856185db85Sdougm if (scf_handle->trans == NULL) { 308657b448deSdougm char oname[SA_STRSIZE]; 30876185db85Sdougm char *groupname = NULL; 3088da6c28aaSamw if (share != NULL) { 3089da6c28aaSamw if (parent != NULL) 309057b448deSdougm groupname = 309157b448deSdougm sa_get_group_attr(parent, 309257b448deSdougm "name"); 3093da6c28aaSamw id = sa_get_share_attr( 3094da6c28aaSamw (sa_share_t)share, "id"); 30956185db85Sdougm } else { 309657b448deSdougm groupname = sa_get_group_attr(group, 309757b448deSdougm "name"); 30986185db85Sdougm } 30996185db85Sdougm if (groupname != NULL) { 310057b448deSdougm ret = sa_get_instance(scf_handle, 310157b448deSdougm groupname); 31026185db85Sdougm sa_free_attr_string(groupname); 31036185db85Sdougm } 31046185db85Sdougm if (opttype) 310557b448deSdougm (void) sa_optionset_name(optionset, 310657b448deSdougm oname, sizeof (oname), id); 31076185db85Sdougm else 310857b448deSdougm (void) sa_security_name(optionset, 310957b448deSdougm oname, sizeof (oname), id); 31106185db85Sdougm ret = sa_start_transaction(scf_handle, oname); 31116185db85Sdougm } 31126185db85Sdougm if (ret == SA_OK) { 31136185db85Sdougm switch (type) { 31146185db85Sdougm case SA_PROP_OP_REMOVE: 3115549ec3ffSdougm ret = scf_transaction_property_delete( 311657b448deSdougm scf_handle->trans, entry, name); 31176185db85Sdougm break; 31186185db85Sdougm case SA_PROP_OP_ADD: 31196185db85Sdougm case SA_PROP_OP_UPDATE: 312057b448deSdougm value = scf_value_create( 312157b448deSdougm scf_handle->handle); 312257b448deSdougm ret = add_or_update(scf_handle, type, 312357b448deSdougm value, entry, name, valstr); 31246185db85Sdougm break; 31256185db85Sdougm } 31266185db85Sdougm } 31276185db85Sdougm } else { 31286185db85Sdougm /* 31296185db85Sdougm * ZFS update. The calling function would have updated 31306185db85Sdougm * the internal XML structure. Just need to flag it as 31316185db85Sdougm * changed for ZFS. 31326185db85Sdougm */ 31336185db85Sdougm zfs_set_update((sa_share_t)group); 31346185db85Sdougm } 31356185db85Sdougm } 31366185db85Sdougm 31376185db85Sdougm if (name != NULL) 31386185db85Sdougm sa_free_attr_string(name); 31396185db85Sdougm if (valstr != NULL) 31406185db85Sdougm sa_free_attr_string(valstr); 31416185db85Sdougm else if (entry != NULL) 31426185db85Sdougm scf_entry_destroy(entry); 31436185db85Sdougm 31446185db85Sdougm if (ret == -1) 31456185db85Sdougm ret = SA_SYSTEM_ERR; 31466185db85Sdougm 31476185db85Sdougm return (ret); 31486185db85Sdougm } 31496185db85Sdougm 31506185db85Sdougm /* 31514bff34e3Sthurlow * sa_create_section(name, value) 31524bff34e3Sthurlow * 31534bff34e3Sthurlow * Create a new section with the specified name and extra data. 31544bff34e3Sthurlow */ 31554bff34e3Sthurlow 31564bff34e3Sthurlow sa_property_t 31574bff34e3Sthurlow sa_create_section(char *name, char *extra) 31584bff34e3Sthurlow { 31594bff34e3Sthurlow xmlNodePtr node; 31604bff34e3Sthurlow 31614bff34e3Sthurlow node = xmlNewNode(NULL, (xmlChar *)"section"); 31624bff34e3Sthurlow if (node != NULL) { 31634bff34e3Sthurlow if (name != NULL) 31644bff34e3Sthurlow (void) xmlSetProp(node, (xmlChar *)"name", 31654bff34e3Sthurlow (xmlChar *)name); 31664bff34e3Sthurlow if (extra != NULL) 31674bff34e3Sthurlow (void) xmlSetProp(node, (xmlChar *)"extra", 31684bff34e3Sthurlow (xmlChar *)extra); 31694bff34e3Sthurlow } 31704bff34e3Sthurlow return ((sa_property_t)node); 31714bff34e3Sthurlow } 31724bff34e3Sthurlow 31734bff34e3Sthurlow void 31744bff34e3Sthurlow sa_set_section_attr(sa_property_t sect, char *name, char *value) 31754bff34e3Sthurlow { 31764bff34e3Sthurlow (void) xmlSetProp(sect, (xmlChar *)name, (xmlChar *)value); 31774bff34e3Sthurlow } 31784bff34e3Sthurlow 31794bff34e3Sthurlow /* 31804bff34e3Sthurlow * sa_create_property(section, name, value) 31816185db85Sdougm * 31826185db85Sdougm * Create a new property with the specified name and value. 31836185db85Sdougm */ 31846185db85Sdougm 31856185db85Sdougm sa_property_t 31866185db85Sdougm sa_create_property(char *name, char *value) 31876185db85Sdougm { 31886185db85Sdougm xmlNodePtr node; 31896185db85Sdougm 31906185db85Sdougm node = xmlNewNode(NULL, (xmlChar *)"option"); 31916185db85Sdougm if (node != NULL) { 31924bff34e3Sthurlow (void) xmlSetProp(node, (xmlChar *)"type", (xmlChar *)name); 31934bff34e3Sthurlow (void) xmlSetProp(node, (xmlChar *)"value", (xmlChar *)value); 31946185db85Sdougm } 31956185db85Sdougm return ((sa_property_t)node); 31966185db85Sdougm } 31976185db85Sdougm 31986185db85Sdougm /* 31996185db85Sdougm * sa_add_property(object, property) 32006185db85Sdougm * 32016185db85Sdougm * Add the specified property to the object. Issue the appropriate 32026185db85Sdougm * transaction or mark a ZFS object as needing an update. 32036185db85Sdougm */ 32046185db85Sdougm 32056185db85Sdougm int 32066185db85Sdougm sa_add_property(void *object, sa_property_t property) 32076185db85Sdougm { 32086185db85Sdougm int ret = SA_OK; 32096185db85Sdougm sa_group_t parent; 32106185db85Sdougm sa_group_t group; 32116185db85Sdougm char *proto; 32126185db85Sdougm 32136185db85Sdougm if (property != NULL) { 32143b61b335Sdougm sa_handle_t handle; 3215687915e9Sdougm handle = sa_find_group_handle((sa_group_t)object); 32163b61b335Sdougm /* It is legitimate to not find a handle */ 3217687915e9Sdougm proto = sa_get_optionset_attr(object, "type"); 3218687915e9Sdougm if ((ret = sa_valid_property(handle, object, proto, 3219687915e9Sdougm property)) == SA_OK) { 322057b448deSdougm property = (sa_property_t)xmlAddChild( 322157b448deSdougm (xmlNodePtr)object, (xmlNodePtr)property); 32226185db85Sdougm } else { 32236185db85Sdougm if (proto != NULL) 32246185db85Sdougm sa_free_attr_string(proto); 32256185db85Sdougm return (ret); 32266185db85Sdougm } 32276185db85Sdougm if (proto != NULL) 32286185db85Sdougm sa_free_attr_string(proto); 3229687915e9Sdougm } 3230687915e9Sdougm 32316185db85Sdougm 32326185db85Sdougm parent = sa_get_parent_group(object); 3233da6c28aaSamw if (!sa_is_persistent(parent)) 32346185db85Sdougm return (ret); 32356185db85Sdougm 3236da6c28aaSamw if (sa_is_resource(parent)) { 3237da6c28aaSamw /* 3238da6c28aaSamw * Resources are children of share. Need to go up two 3239da6c28aaSamw * levels to find the group but the parent needs to be 3240da6c28aaSamw * the share at this point in order to get the "id". 3241da6c28aaSamw */ 3242da6c28aaSamw parent = sa_get_parent_group(parent); 32436185db85Sdougm group = sa_get_parent_group(parent); 3244da6c28aaSamw } else if (sa_is_share(parent)) { 3245da6c28aaSamw group = sa_get_parent_group(parent); 3246da6c28aaSamw } else { 32476185db85Sdougm group = parent; 3248da6c28aaSamw } 32496185db85Sdougm 325057b448deSdougm if (property == NULL) { 32516185db85Sdougm ret = SA_NO_MEMORY; 325257b448deSdougm } else { 325357b448deSdougm char oname[SA_STRSIZE]; 32546185db85Sdougm 32556185db85Sdougm if (!is_zfs_group(group)) { 32566185db85Sdougm char *id = NULL; 3257549ec3ffSdougm sa_handle_impl_t impl_handle; 3258549ec3ffSdougm scfutilhandle_t *scf_handle; 3259549ec3ffSdougm 326057b448deSdougm impl_handle = (sa_handle_impl_t)sa_find_group_handle( 326157b448deSdougm group); 326257b448deSdougm if (impl_handle == NULL || 326357b448deSdougm impl_handle->scfhandle == NULL) 3264549ec3ffSdougm ret = SA_SYSTEM_ERR; 3265549ec3ffSdougm if (ret == SA_OK) { 3266549ec3ffSdougm scf_handle = impl_handle->scfhandle; 32676185db85Sdougm if (sa_is_share((sa_group_t)parent)) { 326857b448deSdougm id = sa_get_share_attr( 326957b448deSdougm (sa_share_t)parent, "id"); 32706185db85Sdougm } 32716185db85Sdougm if (scf_handle->trans == NULL) { 327257b448deSdougm if (is_nodetype(object, "optionset")) { 327357b448deSdougm (void) sa_optionset_name( 327457b448deSdougm (sa_optionset_t)object, 32756185db85Sdougm oname, sizeof (oname), id); 327657b448deSdougm } else { 327757b448deSdougm (void) sa_security_name( 327857b448deSdougm (sa_optionset_t)object, 32796185db85Sdougm oname, sizeof (oname), id); 328057b448deSdougm } 328157b448deSdougm ret = sa_start_transaction(scf_handle, 328257b448deSdougm oname); 32836185db85Sdougm } 32846185db85Sdougm if (ret == SA_OK) { 32856185db85Sdougm char *name; 32866185db85Sdougm char *value; 328757b448deSdougm name = sa_get_property_attr(property, 328857b448deSdougm "type"); 328957b448deSdougm value = sa_get_property_attr(property, 329057b448deSdougm "value"); 32916185db85Sdougm if (name != NULL && value != NULL) { 329257b448deSdougm if (scf_handle->scf_state == 329357b448deSdougm SCH_STATE_INIT) { 329457b448deSdougm ret = sa_set_property( 329557b448deSdougm scf_handle, name, 329657b448deSdougm value); 329757b448deSdougm } 329857b448deSdougm } else { 32996185db85Sdougm ret = SA_CONFIG_ERR; 330057b448deSdougm } 33016185db85Sdougm if (name != NULL) 330257b448deSdougm sa_free_attr_string( 330357b448deSdougm name); 33046185db85Sdougm if (value != NULL) 33056185db85Sdougm sa_free_attr_string(value); 33066185db85Sdougm } 33076185db85Sdougm if (id != NULL) 33086185db85Sdougm sa_free_attr_string(id); 3309549ec3ffSdougm } 33106185db85Sdougm } else { 33116185db85Sdougm /* 331257b448deSdougm * ZFS is a special case. We do want 331357b448deSdougm * to allow editing property/security 331457b448deSdougm * lists since we can have a better 331557b448deSdougm * syntax and we also want to keep 331657b448deSdougm * things consistent when possible. 33176185db85Sdougm * 331857b448deSdougm * Right now, we defer until the 331957b448deSdougm * sa_commit_properties so we can get 332057b448deSdougm * them all at once. We do need to 332157b448deSdougm * mark the share as "changed" 33226185db85Sdougm */ 33236185db85Sdougm zfs_set_update((sa_share_t)parent); 33246185db85Sdougm } 33256185db85Sdougm } 33266185db85Sdougm return (ret); 33276185db85Sdougm } 33286185db85Sdougm 33296185db85Sdougm /* 33306185db85Sdougm * sa_remove_property(property) 33316185db85Sdougm * 33326185db85Sdougm * Remove the specied property from its containing object. Update the 33336185db85Sdougm * repository as appropriate. 33346185db85Sdougm */ 33356185db85Sdougm 33366185db85Sdougm int 33376185db85Sdougm sa_remove_property(sa_property_t property) 33386185db85Sdougm { 33396185db85Sdougm int ret = SA_OK; 33406185db85Sdougm 33416185db85Sdougm if (property != NULL) { 33426185db85Sdougm sa_optionset_t optionset; 33436185db85Sdougm sa_group_t group; 33446185db85Sdougm optionset = sa_get_property_parent(property); 33456185db85Sdougm if (optionset != NULL) { 33466185db85Sdougm group = sa_get_optionset_parent(optionset); 33476185db85Sdougm if (group != NULL) { 334857b448deSdougm ret = sa_set_prop_by_prop(optionset, group, 334957b448deSdougm property, SA_PROP_OP_REMOVE); 33506185db85Sdougm } 33516185db85Sdougm } 33526185db85Sdougm xmlUnlinkNode((xmlNodePtr)property); 33536185db85Sdougm xmlFreeNode((xmlNodePtr)property); 33546185db85Sdougm } else { 33556185db85Sdougm ret = SA_NO_SUCH_PROP; 33566185db85Sdougm } 33576185db85Sdougm return (ret); 33586185db85Sdougm } 33596185db85Sdougm 33606185db85Sdougm /* 33616185db85Sdougm * sa_update_property(property, value) 33626185db85Sdougm * 33636185db85Sdougm * Update the specified property to the new value. If value is NULL, 33646185db85Sdougm * we currently treat this as a remove. 33656185db85Sdougm */ 33666185db85Sdougm 33676185db85Sdougm int 33686185db85Sdougm sa_update_property(sa_property_t property, char *value) 33696185db85Sdougm { 33706185db85Sdougm int ret = SA_OK; 33716185db85Sdougm if (value == NULL) { 33726185db85Sdougm return (sa_remove_property(property)); 33736185db85Sdougm } else { 33746185db85Sdougm sa_optionset_t optionset; 33756185db85Sdougm sa_group_t group; 33766185db85Sdougm set_node_attr((void *)property, "value", value); 33776185db85Sdougm optionset = sa_get_property_parent(property); 33786185db85Sdougm if (optionset != NULL) { 33796185db85Sdougm group = sa_get_optionset_parent(optionset); 33806185db85Sdougm if (group != NULL) { 338157b448deSdougm ret = sa_set_prop_by_prop(optionset, group, 338257b448deSdougm property, SA_PROP_OP_UPDATE); 33836185db85Sdougm } 33846185db85Sdougm } else { 33856185db85Sdougm ret = SA_NO_SUCH_PROP; 33866185db85Sdougm } 33876185db85Sdougm } 33886185db85Sdougm return (ret); 33896185db85Sdougm } 33906185db85Sdougm 33916185db85Sdougm /* 33924bff34e3Sthurlow * sa_get_protocol_section(propset, prop) 33934bff34e3Sthurlow * 33944bff34e3Sthurlow * Get the specified protocol specific section. These are global to 33954bff34e3Sthurlow * the protocol and not specific to a group or share. 33964bff34e3Sthurlow */ 33974bff34e3Sthurlow 33984bff34e3Sthurlow sa_protocol_properties_t 33994bff34e3Sthurlow sa_get_protocol_section(sa_protocol_properties_t propset, char *section) 34004bff34e3Sthurlow { 34014bff34e3Sthurlow xmlNodePtr node = (xmlNodePtr)propset; 34024bff34e3Sthurlow xmlChar *value = NULL; 34034bff34e3Sthurlow char *proto; 34044bff34e3Sthurlow 34054bff34e3Sthurlow proto = sa_get_optionset_attr(propset, "type"); 34064bff34e3Sthurlow if ((sa_proto_get_featureset(proto) & SA_FEATURE_HAS_SECTIONS) == 0) 34074bff34e3Sthurlow return (propset); 34084bff34e3Sthurlow 34094bff34e3Sthurlow for (node = node->children; node != NULL; 34104bff34e3Sthurlow node = node->next) { 34114bff34e3Sthurlow if (xmlStrcmp(node->name, (xmlChar *)"section") == 0) { 34124bff34e3Sthurlow if (section == NULL) 34134bff34e3Sthurlow break; 34144bff34e3Sthurlow value = xmlGetProp(node, (xmlChar *)"name"); 34154bff34e3Sthurlow if (value != NULL && 34164bff34e3Sthurlow xmlStrcasecmp(value, (xmlChar *)section) == 0) { 34174bff34e3Sthurlow break; 34184bff34e3Sthurlow } 34194bff34e3Sthurlow if (value != NULL) { 34204bff34e3Sthurlow xmlFree(value); 34214bff34e3Sthurlow value = NULL; 34224bff34e3Sthurlow } 34234bff34e3Sthurlow } 34244bff34e3Sthurlow } 34254bff34e3Sthurlow if (value != NULL) 34264bff34e3Sthurlow xmlFree(value); 34274bff34e3Sthurlow if (node != NULL && xmlStrcmp(node->name, (xmlChar *)"section") != 0) { 34284bff34e3Sthurlow /* 34294bff34e3Sthurlow * avoid a non option node -- it is possible to be a 34304bff34e3Sthurlow * text node 34314bff34e3Sthurlow */ 34324bff34e3Sthurlow node = NULL; 34334bff34e3Sthurlow } 34344bff34e3Sthurlow return ((sa_protocol_properties_t)node); 34354bff34e3Sthurlow } 34364bff34e3Sthurlow 34374bff34e3Sthurlow /* 34384bff34e3Sthurlow * sa_get_next_protocol_section(prop, find) 34394bff34e3Sthurlow * 34404bff34e3Sthurlow * Get the next protocol specific section in the list. 34414bff34e3Sthurlow */ 34424bff34e3Sthurlow 34434bff34e3Sthurlow sa_property_t 34444bff34e3Sthurlow sa_get_next_protocol_section(sa_property_t prop, char *find) 34454bff34e3Sthurlow { 34464bff34e3Sthurlow xmlNodePtr node; 34474bff34e3Sthurlow xmlChar *value = NULL; 34484bff34e3Sthurlow char *proto; 34494bff34e3Sthurlow 34504bff34e3Sthurlow proto = sa_get_optionset_attr(prop, "type"); 34514bff34e3Sthurlow if ((sa_proto_get_featureset(proto) & SA_FEATURE_HAS_SECTIONS) == 0) 34524bff34e3Sthurlow return ((sa_property_t)NULL); 34534bff34e3Sthurlow 34544bff34e3Sthurlow for (node = ((xmlNodePtr)prop)->next; node != NULL; 34554bff34e3Sthurlow node = node->next) { 34564bff34e3Sthurlow if (xmlStrcmp(node->name, (xmlChar *)"section") == 0) { 34574bff34e3Sthurlow if (find == NULL) 34584bff34e3Sthurlow break; 34594bff34e3Sthurlow value = xmlGetProp(node, (xmlChar *)"name"); 34604bff34e3Sthurlow if (value != NULL && 34614bff34e3Sthurlow xmlStrcasecmp(value, (xmlChar *)find) == 0) { 34624bff34e3Sthurlow break; 34634bff34e3Sthurlow } 34644bff34e3Sthurlow if (value != NULL) { 34654bff34e3Sthurlow xmlFree(value); 34664bff34e3Sthurlow value = NULL; 34674bff34e3Sthurlow } 34684bff34e3Sthurlow 34694bff34e3Sthurlow } 34704bff34e3Sthurlow } 34714bff34e3Sthurlow if (value != NULL) 34724bff34e3Sthurlow xmlFree(value); 34734bff34e3Sthurlow return ((sa_property_t)node); 34744bff34e3Sthurlow } 34754bff34e3Sthurlow 34764bff34e3Sthurlow /* 34776185db85Sdougm * sa_get_protocol_property(propset, prop) 34786185db85Sdougm * 34796185db85Sdougm * Get the specified protocol specific property. These are global to 34806185db85Sdougm * the protocol and not specific to a group or share. 34816185db85Sdougm */ 34826185db85Sdougm 34836185db85Sdougm sa_property_t 34846185db85Sdougm sa_get_protocol_property(sa_protocol_properties_t propset, char *prop) 34856185db85Sdougm { 34866185db85Sdougm xmlNodePtr node = (xmlNodePtr)propset; 34876185db85Sdougm xmlChar *value = NULL; 34886185db85Sdougm 34894bff34e3Sthurlow if (propset == NULL) 34904bff34e3Sthurlow return (NULL); 34914bff34e3Sthurlow 34926185db85Sdougm for (node = node->children; node != NULL; 34936185db85Sdougm node = node->next) { 34946185db85Sdougm if (xmlStrcmp(node->name, (xmlChar *)"option") == 0) { 34956185db85Sdougm if (prop == NULL) 34966185db85Sdougm break; 34976185db85Sdougm value = xmlGetProp(node, (xmlChar *)"type"); 34986185db85Sdougm if (value != NULL && 34996185db85Sdougm xmlStrcasecmp(value, (xmlChar *)prop) == 0) { 35006185db85Sdougm break; 35016185db85Sdougm } 35026185db85Sdougm if (value != NULL) { 35036185db85Sdougm xmlFree(value); 35046185db85Sdougm value = NULL; 35056185db85Sdougm } 35066185db85Sdougm } 35076185db85Sdougm } 35086185db85Sdougm if (value != NULL) 35096185db85Sdougm xmlFree(value); 35106185db85Sdougm if (node != NULL && xmlStrcmp(node->name, (xmlChar *)"option") != 0) { 351157b448deSdougm /* 351257b448deSdougm * avoid a non option node -- it is possible to be a 351357b448deSdougm * text node 351457b448deSdougm */ 35156185db85Sdougm node = NULL; 35166185db85Sdougm } 35176185db85Sdougm return ((sa_property_t)node); 35186185db85Sdougm } 35196185db85Sdougm 35206185db85Sdougm /* 35216185db85Sdougm * sa_get_next_protocol_property(prop) 35226185db85Sdougm * 35236185db85Sdougm * Get the next protocol specific property in the list. 35246185db85Sdougm */ 35256185db85Sdougm 35266185db85Sdougm sa_property_t 35274bff34e3Sthurlow sa_get_next_protocol_property(sa_property_t prop, char *find) 35286185db85Sdougm { 35296185db85Sdougm xmlNodePtr node; 35304bff34e3Sthurlow xmlChar *value = NULL; 35316185db85Sdougm 35326185db85Sdougm for (node = ((xmlNodePtr)prop)->next; node != NULL; 35336185db85Sdougm node = node->next) { 35346185db85Sdougm if (xmlStrcmp(node->name, (xmlChar *)"option") == 0) { 35354bff34e3Sthurlow if (find == NULL) 35364bff34e3Sthurlow break; 35374bff34e3Sthurlow value = xmlGetProp(node, (xmlChar *)"type"); 35384bff34e3Sthurlow if (value != NULL && 35394bff34e3Sthurlow xmlStrcasecmp(value, (xmlChar *)find) == 0) { 35406185db85Sdougm break; 35416185db85Sdougm } 35424bff34e3Sthurlow if (value != NULL) { 35434bff34e3Sthurlow xmlFree(value); 35444bff34e3Sthurlow value = NULL; 35456185db85Sdougm } 35464bff34e3Sthurlow 35474bff34e3Sthurlow } 35484bff34e3Sthurlow } 35494bff34e3Sthurlow if (value != NULL) 35504bff34e3Sthurlow xmlFree(value); 35516185db85Sdougm return ((sa_property_t)node); 35526185db85Sdougm } 35536185db85Sdougm 35546185db85Sdougm /* 35556185db85Sdougm * sa_set_protocol_property(prop, value) 35566185db85Sdougm * 35576185db85Sdougm * Set the specified property to have the new value. The protocol 35586185db85Sdougm * specific plugin will then be called to update the property. 35596185db85Sdougm */ 35606185db85Sdougm 35616185db85Sdougm int 35624bff34e3Sthurlow sa_set_protocol_property(sa_property_t prop, char *section, char *value) 35636185db85Sdougm { 35646185db85Sdougm sa_protocol_properties_t propset; 35656185db85Sdougm char *proto; 35666185db85Sdougm int ret = SA_INVALID_PROTOCOL; 35676185db85Sdougm 35686185db85Sdougm propset = ((xmlNodePtr)prop)->parent; 35696185db85Sdougm if (propset != NULL) { 35706185db85Sdougm proto = sa_get_optionset_attr(propset, "type"); 35716185db85Sdougm if (proto != NULL) { 35724bff34e3Sthurlow if (section != NULL) 35734bff34e3Sthurlow set_node_attr((xmlNodePtr)prop, "section", 35744bff34e3Sthurlow section); 35756185db85Sdougm set_node_attr((xmlNodePtr)prop, "value", value); 35766185db85Sdougm ret = sa_proto_set_property(proto, prop); 35773472f5dcSdougm sa_free_attr_string(proto); 35786185db85Sdougm } 35796185db85Sdougm } 35806185db85Sdougm return (ret); 35816185db85Sdougm } 35826185db85Sdougm 35836185db85Sdougm /* 35846185db85Sdougm * sa_add_protocol_property(propset, prop) 35856185db85Sdougm * 3586da6c28aaSamw * Add a new property to the protocol specific property set. 35876185db85Sdougm */ 35886185db85Sdougm 35896185db85Sdougm int 35906185db85Sdougm sa_add_protocol_property(sa_protocol_properties_t propset, sa_property_t prop) 35916185db85Sdougm { 35926185db85Sdougm xmlNodePtr node; 35936185db85Sdougm 35946185db85Sdougm /* should check for legitimacy */ 35956185db85Sdougm node = xmlAddChild((xmlNodePtr)propset, (xmlNodePtr)prop); 35966185db85Sdougm if (node != NULL) 35976185db85Sdougm return (SA_OK); 35986185db85Sdougm return (SA_NO_MEMORY); 35996185db85Sdougm } 36006185db85Sdougm 36016185db85Sdougm /* 36026185db85Sdougm * sa_create_protocol_properties(proto) 36036185db85Sdougm * 3604da6c28aaSamw * Create a protocol specific property set. 36056185db85Sdougm */ 36066185db85Sdougm 36076185db85Sdougm sa_protocol_properties_t 36086185db85Sdougm sa_create_protocol_properties(char *proto) 36096185db85Sdougm { 36106185db85Sdougm xmlNodePtr node; 361157b448deSdougm 36126185db85Sdougm node = xmlNewNode(NULL, (xmlChar *)"propertyset"); 361357b448deSdougm if (node != NULL) 36144bff34e3Sthurlow (void) xmlSetProp(node, (xmlChar *)"type", (xmlChar *)proto); 36156185db85Sdougm return (node); 36166185db85Sdougm } 3617da6c28aaSamw 3618da6c28aaSamw /* 3619da6c28aaSamw * sa_get_share_resource(share, resource) 3620da6c28aaSamw * 3621da6c28aaSamw * Get the named resource from the share, if it exists. If resource is 3622da6c28aaSamw * NULL, get the first resource. 3623da6c28aaSamw */ 3624da6c28aaSamw 3625da6c28aaSamw sa_resource_t 3626da6c28aaSamw sa_get_share_resource(sa_share_t share, char *resource) 3627da6c28aaSamw { 3628da6c28aaSamw xmlNodePtr node = NULL; 3629da6c28aaSamw xmlChar *name; 3630da6c28aaSamw 3631da6c28aaSamw if (share != NULL) { 3632da6c28aaSamw for (node = ((xmlNodePtr)share)->children; node != NULL; 3633da6c28aaSamw node = node->next) { 3634da6c28aaSamw if (xmlStrcmp(node->name, (xmlChar *)"resource") == 0) { 3635da6c28aaSamw if (resource == NULL) { 3636da6c28aaSamw /* 3637da6c28aaSamw * We are looking for the first 3638da6c28aaSamw * resource node and not a names 3639da6c28aaSamw * resource. 3640da6c28aaSamw */ 3641da6c28aaSamw break; 3642da6c28aaSamw } else { 3643da6c28aaSamw /* is it the correct share? */ 3644da6c28aaSamw name = xmlGetProp(node, 3645da6c28aaSamw (xmlChar *)"name"); 3646da6c28aaSamw if (name != NULL && 3647da6c28aaSamw xmlStrcasecmp(name, 3648da6c28aaSamw (xmlChar *)resource) == 0) { 3649da6c28aaSamw xmlFree(name); 3650da6c28aaSamw break; 3651da6c28aaSamw } 3652da6c28aaSamw xmlFree(name); 3653da6c28aaSamw } 3654da6c28aaSamw } 3655da6c28aaSamw } 3656da6c28aaSamw } 3657da6c28aaSamw return ((sa_resource_t)node); 3658da6c28aaSamw } 3659da6c28aaSamw 3660da6c28aaSamw /* 3661da6c28aaSamw * sa_get_next_resource(resource) 3662da6c28aaSamw * Return the next share following the specified share 3663da6c28aaSamw * from the internal list of shares. Returns NULL if there 3664da6c28aaSamw * are no more shares. The list is relative to the same 3665da6c28aaSamw * group. 3666da6c28aaSamw */ 3667da6c28aaSamw sa_share_t 3668da6c28aaSamw sa_get_next_resource(sa_resource_t resource) 3669da6c28aaSamw { 3670da6c28aaSamw xmlNodePtr node = NULL; 3671da6c28aaSamw 3672da6c28aaSamw if (resource != NULL) { 3673da6c28aaSamw for (node = ((xmlNodePtr)resource)->next; node != NULL; 3674da6c28aaSamw node = node->next) { 3675da6c28aaSamw if (xmlStrcmp(node->name, (xmlChar *)"resource") == 0) 3676da6c28aaSamw break; 3677da6c28aaSamw } 3678da6c28aaSamw } 3679da6c28aaSamw return ((sa_share_t)node); 3680da6c28aaSamw } 3681da6c28aaSamw 3682da6c28aaSamw /* 3683da6c28aaSamw * _sa_get_next_resource_index(share) 3684da6c28aaSamw * 3685da6c28aaSamw * get the next resource index number (one greater then current largest) 3686da6c28aaSamw */ 3687da6c28aaSamw 3688da6c28aaSamw static int 3689da6c28aaSamw _sa_get_next_resource_index(sa_share_t share) 3690da6c28aaSamw { 3691da6c28aaSamw sa_resource_t resource; 3692da6c28aaSamw int index = 0; 3693da6c28aaSamw char *id; 3694da6c28aaSamw 3695da6c28aaSamw for (resource = sa_get_share_resource(share, NULL); 3696da6c28aaSamw resource != NULL; 3697da6c28aaSamw resource = sa_get_next_resource(resource)) { 3698da6c28aaSamw id = get_node_attr((void *)resource, "id"); 3699da6c28aaSamw if (id != NULL) { 3700da6c28aaSamw int val; 3701da6c28aaSamw val = atoi(id); 3702da6c28aaSamw if (val > index) 3703da6c28aaSamw index = val; 3704da6c28aaSamw sa_free_attr_string(id); 3705da6c28aaSamw } 3706da6c28aaSamw } 3707da6c28aaSamw return (index + 1); 3708da6c28aaSamw } 3709da6c28aaSamw 3710da6c28aaSamw 3711da6c28aaSamw /* 3712da6c28aaSamw * sa_add_resource(share, resource, persist, &err) 3713da6c28aaSamw * 3714da6c28aaSamw * Adds a new resource name associated with share. The resource name 3715da6c28aaSamw * must be unique in the system and will be case insensitive (eventually). 3716da6c28aaSamw */ 3717da6c28aaSamw 3718da6c28aaSamw sa_resource_t 3719da6c28aaSamw sa_add_resource(sa_share_t share, char *resource, int persist, int *error) 3720da6c28aaSamw { 3721da6c28aaSamw xmlNodePtr node; 3722da6c28aaSamw int err = SA_OK; 3723da6c28aaSamw sa_resource_t res; 3724da6c28aaSamw sa_group_t group; 3725da6c28aaSamw sa_handle_t handle; 3726da6c28aaSamw char istring[8]; /* just big enough for an integer value */ 3727da6c28aaSamw int index; 3728da6c28aaSamw 3729da6c28aaSamw group = sa_get_parent_group(share); 3730da6c28aaSamw handle = sa_find_group_handle(group); 3731da6c28aaSamw res = sa_find_resource(handle, resource); 3732da6c28aaSamw if (res != NULL) { 3733da6c28aaSamw err = SA_DUPLICATE_NAME; 3734da6c28aaSamw res = NULL; 3735da6c28aaSamw } else { 3736da6c28aaSamw node = xmlNewChild((xmlNodePtr)share, NULL, 3737da6c28aaSamw (xmlChar *)"resource", NULL); 3738da6c28aaSamw if (node != NULL) { 37394bff34e3Sthurlow (void) xmlSetProp(node, (xmlChar *)"name", 3740da6c28aaSamw (xmlChar *)resource); 37414bff34e3Sthurlow (void) xmlSetProp(node, (xmlChar *)"type", persist ? 3742da6c28aaSamw (xmlChar *)"persist" : (xmlChar *)"transient"); 3743da6c28aaSamw if (persist != SA_SHARE_TRANSIENT) { 3744da6c28aaSamw index = _sa_get_next_resource_index(share); 3745da6c28aaSamw (void) snprintf(istring, sizeof (istring), "%d", 3746da6c28aaSamw index); 37474bff34e3Sthurlow (void) xmlSetProp(node, (xmlChar *)"id", 3748da6c28aaSamw (xmlChar *)istring); 3749da6c28aaSamw if (!sa_group_is_zfs(group) && 3750da6c28aaSamw sa_is_persistent((sa_group_t)share)) { 3751da6c28aaSamw /* ZFS doesn't use resource names */ 3752da6c28aaSamw sa_handle_impl_t ihandle; 3753da6c28aaSamw ihandle = (sa_handle_impl_t) 3754da6c28aaSamw sa_find_group_handle( 3755da6c28aaSamw group); 3756da6c28aaSamw if (ihandle != NULL) 3757da6c28aaSamw err = sa_commit_share( 3758da6c28aaSamw ihandle->scfhandle, group, 3759da6c28aaSamw share); 3760da6c28aaSamw else 3761da6c28aaSamw err = SA_SYSTEM_ERR; 3762da6c28aaSamw } 3763da6c28aaSamw } 3764da6c28aaSamw } 3765da6c28aaSamw } 3766da6c28aaSamw if (error != NULL) 3767da6c28aaSamw *error = err; 3768da6c28aaSamw return ((sa_resource_t)node); 3769da6c28aaSamw } 3770da6c28aaSamw 3771da6c28aaSamw /* 3772da6c28aaSamw * sa_remove_resource(resource) 3773da6c28aaSamw * 3774da6c28aaSamw * Remove the resource name from the share (and the system) 3775da6c28aaSamw */ 3776da6c28aaSamw 3777da6c28aaSamw int 3778da6c28aaSamw sa_remove_resource(sa_resource_t resource) 3779da6c28aaSamw { 3780da6c28aaSamw sa_share_t share; 3781da6c28aaSamw sa_group_t group; 3782da6c28aaSamw char *type; 3783da6c28aaSamw int ret = SA_OK; 3784da6c28aaSamw int transient = 0; 378555bf511dSas200622 sa_optionset_t opt; 3786da6c28aaSamw 3787da6c28aaSamw share = sa_get_resource_parent(resource); 3788da6c28aaSamw type = sa_get_share_attr(share, "type"); 3789da6c28aaSamw group = sa_get_parent_group(share); 3790da6c28aaSamw 3791da6c28aaSamw 3792da6c28aaSamw if (type != NULL) { 3793da6c28aaSamw if (strcmp(type, "persist") != 0) 3794da6c28aaSamw transient = 1; 3795da6c28aaSamw sa_free_attr_string(type); 3796da6c28aaSamw } 3797da6c28aaSamw 379855bf511dSas200622 /* Disable the resource for all protocols. */ 379955bf511dSas200622 (void) sa_disable_resource(resource, NULL); 380055bf511dSas200622 380155bf511dSas200622 /* Remove any optionsets from the resource. */ 380255bf511dSas200622 for (opt = sa_get_optionset(resource, NULL); 380355bf511dSas200622 opt != NULL; 380455bf511dSas200622 opt = sa_get_next_optionset(opt)) 380555bf511dSas200622 (void) sa_destroy_optionset(opt); 380655bf511dSas200622 3807da6c28aaSamw /* Remove from the share */ 3808da6c28aaSamw xmlUnlinkNode((xmlNode *)resource); 3809da6c28aaSamw xmlFreeNode((xmlNode *)resource); 3810da6c28aaSamw 3811da6c28aaSamw /* only do SMF action if permanent and not ZFS */ 3812da6c28aaSamw if (!transient && !sa_group_is_zfs(group)) { 3813da6c28aaSamw sa_handle_impl_t ihandle; 3814da6c28aaSamw ihandle = (sa_handle_impl_t)sa_find_group_handle(group); 3815da6c28aaSamw if (ihandle != NULL) 3816da6c28aaSamw ret = sa_commit_share(ihandle->scfhandle, group, share); 3817da6c28aaSamw else 3818da6c28aaSamw ret = SA_SYSTEM_ERR; 3819da6c28aaSamw } 3820da6c28aaSamw return (ret); 3821da6c28aaSamw } 3822da6c28aaSamw 3823da6c28aaSamw /* 3824da6c28aaSamw * proto_resource_rename(handle, group, resource, newname) 3825da6c28aaSamw * 3826da6c28aaSamw * Helper function for sa_rename_resource that notifies the protocol 3827da6c28aaSamw * of a resource name change prior to a config repository update. 3828da6c28aaSamw */ 3829da6c28aaSamw static int 3830da6c28aaSamw proto_rename_resource(sa_handle_t handle, sa_group_t group, 3831da6c28aaSamw sa_resource_t resource, char *newname) 3832da6c28aaSamw { 3833da6c28aaSamw sa_optionset_t optionset; 3834da6c28aaSamw int ret = SA_OK; 3835da6c28aaSamw int err; 3836da6c28aaSamw 3837da6c28aaSamw for (optionset = sa_get_optionset(group, NULL); 3838da6c28aaSamw optionset != NULL; 3839da6c28aaSamw optionset = sa_get_next_optionset(optionset)) { 3840da6c28aaSamw char *type; 3841da6c28aaSamw type = sa_get_optionset_attr(optionset, "type"); 3842da6c28aaSamw if (type != NULL) { 3843da6c28aaSamw err = sa_proto_rename_resource(handle, type, resource, 3844da6c28aaSamw newname); 3845da6c28aaSamw if (err != SA_OK) 3846da6c28aaSamw ret = err; 3847da6c28aaSamw sa_free_attr_string(type); 3848da6c28aaSamw } 3849da6c28aaSamw } 3850da6c28aaSamw return (ret); 3851da6c28aaSamw } 3852da6c28aaSamw 3853da6c28aaSamw /* 3854da6c28aaSamw * sa_rename_resource(resource, newname) 3855da6c28aaSamw * 3856da6c28aaSamw * Rename the resource to the new name, if it is unique. 3857da6c28aaSamw */ 3858da6c28aaSamw 3859da6c28aaSamw int 3860da6c28aaSamw sa_rename_resource(sa_resource_t resource, char *newname) 3861da6c28aaSamw { 3862da6c28aaSamw sa_share_t share; 3863da6c28aaSamw sa_group_t group = NULL; 3864da6c28aaSamw sa_resource_t target; 3865da6c28aaSamw int ret = SA_CONFIG_ERR; 3866da6c28aaSamw sa_handle_t handle = NULL; 3867da6c28aaSamw 3868da6c28aaSamw share = sa_get_resource_parent(resource); 3869da6c28aaSamw if (share == NULL) 3870da6c28aaSamw return (ret); 3871da6c28aaSamw 3872da6c28aaSamw group = sa_get_parent_group(share); 3873da6c28aaSamw if (group == NULL) 3874da6c28aaSamw return (ret); 3875da6c28aaSamw 3876da6c28aaSamw handle = (sa_handle_impl_t)sa_find_group_handle(group); 3877da6c28aaSamw if (handle == NULL) 3878da6c28aaSamw return (ret); 3879da6c28aaSamw 3880da6c28aaSamw target = sa_find_resource(handle, newname); 3881da6c28aaSamw if (target != NULL) { 3882da6c28aaSamw ret = SA_DUPLICATE_NAME; 3883da6c28aaSamw } else { 3884da6c28aaSamw /* 3885da6c28aaSamw * Everything appears to be valid at this 3886da6c28aaSamw * point. Change the name of the active share and then 3887da6c28aaSamw * update the share in the appropriate repository. 3888da6c28aaSamw */ 3889da6c28aaSamw ret = proto_rename_resource(handle, group, resource, newname); 3890da6c28aaSamw set_node_attr(resource, "name", newname); 3891da6c28aaSamw if (!sa_group_is_zfs(group) && 3892da6c28aaSamw sa_is_persistent((sa_group_t)share)) { 3893da6c28aaSamw sa_handle_impl_t ihandle = (sa_handle_impl_t)handle; 3894da6c28aaSamw ret = sa_commit_share(ihandle->scfhandle, group, 3895da6c28aaSamw share); 3896da6c28aaSamw } 3897da6c28aaSamw } 3898da6c28aaSamw return (ret); 3899da6c28aaSamw } 3900da6c28aaSamw 3901da6c28aaSamw /* 3902da6c28aaSamw * sa_get_resource_attr(resource, tag) 3903da6c28aaSamw * 3904da6c28aaSamw * Get the named attribute of the resource. "name" and "id" are 3905da6c28aaSamw * currently defined. NULL if tag not defined. 3906da6c28aaSamw */ 3907da6c28aaSamw 3908da6c28aaSamw char * 3909da6c28aaSamw sa_get_resource_attr(sa_resource_t resource, char *tag) 3910da6c28aaSamw { 3911da6c28aaSamw return (get_node_attr((void *)resource, tag)); 3912da6c28aaSamw } 3913da6c28aaSamw 3914da6c28aaSamw /* 3915da6c28aaSamw * sa_set_resource_attr(resource, tag, value) 3916da6c28aaSamw * 3917da6c28aaSamw * Get the named attribute of the resource. "name" and "id" are 3918da6c28aaSamw * currently defined. NULL if tag not defined. Currently we don't do 3919da6c28aaSamw * much, but additional checking may be needed in the future. 3920da6c28aaSamw */ 3921da6c28aaSamw 3922da6c28aaSamw int 3923da6c28aaSamw sa_set_resource_attr(sa_resource_t resource, char *tag, char *value) 3924da6c28aaSamw { 3925da6c28aaSamw set_node_attr((void *)resource, tag, value); 3926da6c28aaSamw return (SA_OK); 3927da6c28aaSamw } 3928da6c28aaSamw 3929da6c28aaSamw /* 3930da6c28aaSamw * sa_get_resource_parent(resource_t) 3931da6c28aaSamw * 3932da6c28aaSamw * Returns the share associated with the resource. 3933da6c28aaSamw */ 3934da6c28aaSamw 3935da6c28aaSamw sa_share_t 3936da6c28aaSamw sa_get_resource_parent(sa_resource_t resource) 3937da6c28aaSamw { 3938da6c28aaSamw sa_share_t share = NULL; 3939da6c28aaSamw 3940da6c28aaSamw if (resource != NULL) 3941da6c28aaSamw share = (sa_share_t)((xmlNodePtr)resource)->parent; 3942da6c28aaSamw return (share); 3943da6c28aaSamw } 3944da6c28aaSamw 3945da6c28aaSamw /* 3946da6c28aaSamw * find_resource(group, name) 3947da6c28aaSamw * 3948da6c28aaSamw * Find the resource within the group. 3949da6c28aaSamw */ 3950da6c28aaSamw 3951da6c28aaSamw static sa_resource_t 3952da6c28aaSamw find_resource(sa_group_t group, char *resname) 3953da6c28aaSamw { 3954da6c28aaSamw sa_share_t share; 3955da6c28aaSamw sa_resource_t resource = NULL; 3956da6c28aaSamw char *name; 3957da6c28aaSamw 3958da6c28aaSamw /* Iterate over all the shares and resources in the group. */ 3959da6c28aaSamw for (share = sa_get_share(group, NULL); 3960da6c28aaSamw share != NULL && resource == NULL; 3961da6c28aaSamw share = sa_get_next_share(share)) { 3962da6c28aaSamw for (resource = sa_get_share_resource(share, NULL); 3963da6c28aaSamw resource != NULL; 3964da6c28aaSamw resource = sa_get_next_resource(resource)) { 3965da6c28aaSamw name = sa_get_resource_attr(resource, "name"); 3966da6c28aaSamw if (name != NULL && xmlStrcasecmp((xmlChar*)name, 3967da6c28aaSamw (xmlChar*)resname) == 0) { 3968da6c28aaSamw sa_free_attr_string(name); 3969da6c28aaSamw break; 3970da6c28aaSamw } 3971da6c28aaSamw if (name != NULL) { 3972da6c28aaSamw sa_free_attr_string(name); 3973da6c28aaSamw } 3974da6c28aaSamw } 3975da6c28aaSamw } 3976da6c28aaSamw return (resource); 3977da6c28aaSamw } 3978da6c28aaSamw 3979da6c28aaSamw /* 3980da6c28aaSamw * sa_find_resource(name) 3981da6c28aaSamw * 3982da6c28aaSamw * Find the named resource in the system. 3983da6c28aaSamw */ 3984da6c28aaSamw 3985da6c28aaSamw sa_resource_t 3986da6c28aaSamw sa_find_resource(sa_handle_t handle, char *name) 3987da6c28aaSamw { 3988da6c28aaSamw sa_group_t group; 3989da6c28aaSamw sa_group_t zgroup; 3990da6c28aaSamw sa_resource_t resource = NULL; 3991da6c28aaSamw 3992da6c28aaSamw /* 3993da6c28aaSamw * Iterate over all groups and zfs subgroups and check for 3994da6c28aaSamw * resource name in them. 3995da6c28aaSamw */ 3996da6c28aaSamw for (group = sa_get_group(handle, NULL); group != NULL; 3997da6c28aaSamw group = sa_get_next_group(group)) { 3998da6c28aaSamw 3999da6c28aaSamw if (is_zfs_group(group)) { 4000da6c28aaSamw for (zgroup = 4001da6c28aaSamw (sa_group_t)_sa_get_child_node((xmlNodePtr)group, 4002da6c28aaSamw (xmlChar *)"group"); 4003da6c28aaSamw zgroup != NULL && resource == NULL; 4004da6c28aaSamw zgroup = sa_get_next_group(zgroup)) { 4005da6c28aaSamw resource = find_resource(zgroup, name); 4006da6c28aaSamw } 4007da6c28aaSamw } else { 4008da6c28aaSamw resource = find_resource(group, name); 4009da6c28aaSamw } 4010da6c28aaSamw if (resource != NULL) 4011da6c28aaSamw break; 4012da6c28aaSamw } 4013da6c28aaSamw return (resource); 4014da6c28aaSamw } 4015da6c28aaSamw 4016da6c28aaSamw /* 4017da6c28aaSamw * sa_get_resource(group, resource) 4018da6c28aaSamw * 4019da6c28aaSamw * Search all the shares in the specified group for a share with a 4020da6c28aaSamw * resource name matching the one specified. 4021da6c28aaSamw * 4022da6c28aaSamw * In the future, it may be advantageous to allow group to be NULL and 4023da6c28aaSamw * search all groups but that isn't needed at present. 4024da6c28aaSamw */ 4025da6c28aaSamw 4026da6c28aaSamw sa_resource_t 4027da6c28aaSamw sa_get_resource(sa_group_t group, char *resource) 4028da6c28aaSamw { 4029da6c28aaSamw sa_share_t share = NULL; 4030da6c28aaSamw sa_resource_t res = NULL; 4031da6c28aaSamw 4032da6c28aaSamw if (resource != NULL) { 4033da6c28aaSamw for (share = sa_get_share(group, NULL); 4034da6c28aaSamw share != NULL && res == NULL; 4035da6c28aaSamw share = sa_get_next_share(share)) { 4036da6c28aaSamw res = sa_get_share_resource(share, resource); 4037da6c28aaSamw } 4038da6c28aaSamw } 4039da6c28aaSamw return (res); 4040da6c28aaSamw } 4041da6c28aaSamw 4042da6c28aaSamw /* 4043f85463f2Sdougm * get_protocol_list(optionset, object) 4044f85463f2Sdougm * 4045f85463f2Sdougm * Get the protocol optionset list for the object and add them as 4046f85463f2Sdougm * properties to optionset. 4047f85463f2Sdougm */ 4048f85463f2Sdougm static int 4049f85463f2Sdougm get_protocol_list(sa_optionset_t optionset, void *object) 4050f85463f2Sdougm { 4051f85463f2Sdougm sa_property_t prop; 4052f85463f2Sdougm sa_optionset_t opts; 4053f85463f2Sdougm int ret = SA_OK; 4054f85463f2Sdougm 4055f85463f2Sdougm for (opts = sa_get_optionset(object, NULL); 4056f85463f2Sdougm opts != NULL; 4057f85463f2Sdougm opts = sa_get_next_optionset(opts)) { 4058f85463f2Sdougm char *type; 4059f85463f2Sdougm type = sa_get_optionset_attr(opts, "type"); 4060f85463f2Sdougm /* 4061f85463f2Sdougm * It is possible to have a non-protocol optionset. We 4062f85463f2Sdougm * skip any of those found. 4063f85463f2Sdougm */ 4064f85463f2Sdougm if (type == NULL) 4065f85463f2Sdougm continue; 4066f85463f2Sdougm prop = sa_create_property(type, "true"); 4067f85463f2Sdougm sa_free_attr_string(type); 4068f85463f2Sdougm if (prop != NULL) 4069f85463f2Sdougm prop = (sa_property_t)xmlAddChild((xmlNodePtr)optionset, 4070f85463f2Sdougm (xmlNodePtr)prop); 4071f85463f2Sdougm /* If prop is NULL, don't bother continuing */ 4072f85463f2Sdougm if (prop == NULL) { 4073f85463f2Sdougm ret = SA_NO_MEMORY; 4074f85463f2Sdougm break; 4075f85463f2Sdougm } 4076f85463f2Sdougm } 4077f85463f2Sdougm return (ret); 4078f85463f2Sdougm } 4079f85463f2Sdougm 4080f85463f2Sdougm /* 4081f85463f2Sdougm * sa_free_protoset(optionset) 4082f85463f2Sdougm * 4083f85463f2Sdougm * Free the protocol property optionset. 4084f85463f2Sdougm */ 4085f85463f2Sdougm static void 4086f85463f2Sdougm sa_free_protoset(sa_optionset_t optionset) 4087f85463f2Sdougm { 4088f85463f2Sdougm if (optionset != NULL) { 4089f85463f2Sdougm xmlUnlinkNode((xmlNodePtr) optionset); 4090f85463f2Sdougm xmlFreeNode((xmlNodePtr) optionset); 4091f85463f2Sdougm } 4092f85463f2Sdougm } 4093f85463f2Sdougm 4094f85463f2Sdougm /* 4095f85463f2Sdougm * sa_optionset_t sa_get_active_protocols(object) 4096f85463f2Sdougm * 4097f85463f2Sdougm * Return a list of the protocols that are active for the object. 4098f85463f2Sdougm * This is currently an internal helper function, but could be 4099f85463f2Sdougm * made visible if there is enough demand for it. 4100f85463f2Sdougm * 4101f85463f2Sdougm * The function finds the parent group and extracts the protocol 4102f85463f2Sdougm * optionsets creating a new optionset with the protocols as properties. 4103f85463f2Sdougm * 4104f85463f2Sdougm * The caller must free the returned optionset. 4105f85463f2Sdougm */ 4106f85463f2Sdougm 4107f85463f2Sdougm static sa_optionset_t 4108f85463f2Sdougm sa_get_active_protocols(void *object) 4109f85463f2Sdougm { 4110f85463f2Sdougm sa_optionset_t options; 4111f85463f2Sdougm sa_share_t share = NULL; 4112f85463f2Sdougm sa_group_t group = NULL; 4113f85463f2Sdougm sa_resource_t resource = NULL; 4114f85463f2Sdougm int ret = SA_OK; 4115f85463f2Sdougm 4116f85463f2Sdougm if (object == NULL) 4117f85463f2Sdougm return (NULL); 4118f85463f2Sdougm options = (sa_optionset_t)xmlNewNode(NULL, (xmlChar *)"optionset"); 4119f85463f2Sdougm if (options == NULL) 4120f85463f2Sdougm return (NULL); 4121f85463f2Sdougm 4122f85463f2Sdougm /* 4123f85463f2Sdougm * Find the objects up the tree that might have protocols 4124f85463f2Sdougm * enabled on them. 4125f85463f2Sdougm */ 4126f85463f2Sdougm if (sa_is_resource(object)) { 4127f85463f2Sdougm resource = (sa_resource_t)object; 4128f85463f2Sdougm share = sa_get_resource_parent(resource); 4129f85463f2Sdougm group = sa_get_parent_group(share); 4130f85463f2Sdougm } else if (sa_is_share(object)) { 4131f85463f2Sdougm share = (sa_share_t)object; 4132f85463f2Sdougm group = sa_get_parent_group(share); 4133f85463f2Sdougm } else { 4134f85463f2Sdougm group = (sa_group_t)group; 4135f85463f2Sdougm } 4136f85463f2Sdougm if (resource != NULL) 4137f85463f2Sdougm ret = get_protocol_list(options, resource); 4138f85463f2Sdougm if (ret == SA_OK && share != NULL) 4139f85463f2Sdougm ret = get_protocol_list(options, share); 4140f85463f2Sdougm if (ret == SA_OK && group != NULL) 4141f85463f2Sdougm ret = get_protocol_list(options, group); 4142f85463f2Sdougm 4143f85463f2Sdougm /* 4144f85463f2Sdougm * If there was an error, we won't have a complete list so 4145f85463f2Sdougm * abandon everything. The caller will have to deal with the 4146f85463f2Sdougm * issue. 4147f85463f2Sdougm */ 4148f85463f2Sdougm if (ret != SA_OK) { 4149f85463f2Sdougm sa_free_protoset(options); 4150f85463f2Sdougm options = NULL; 4151f85463f2Sdougm } 4152f85463f2Sdougm return (options); 4153f85463f2Sdougm } 4154f85463f2Sdougm 4155f85463f2Sdougm /* 4156da6c28aaSamw * sa_enable_resource, protocol) 4157da6c28aaSamw * Disable the specified share to the specified protocol. 4158da6c28aaSamw * If protocol is NULL, then all protocols. 4159da6c28aaSamw */ 4160da6c28aaSamw int 4161da6c28aaSamw sa_enable_resource(sa_resource_t resource, char *protocol) 4162da6c28aaSamw { 4163da6c28aaSamw int ret = SA_OK; 4164da6c28aaSamw 4165da6c28aaSamw if (protocol != NULL) { 4166da6c28aaSamw ret = sa_proto_share_resource(protocol, resource); 4167da6c28aaSamw } else { 4168f85463f2Sdougm sa_optionset_t protoset; 4169f85463f2Sdougm sa_property_t prop; 4170f85463f2Sdougm char *proto; 4171f85463f2Sdougm int err; 4172f85463f2Sdougm 4173da6c28aaSamw /* need to do all protocols */ 4174f85463f2Sdougm protoset = sa_get_active_protocols(resource); 4175f85463f2Sdougm if (protoset == NULL) 4176f85463f2Sdougm return (SA_NO_MEMORY); 4177f85463f2Sdougm for (prop = sa_get_property(protoset, NULL); 4178f85463f2Sdougm prop != NULL; 4179f85463f2Sdougm prop = sa_get_next_property(prop)) { 4180f85463f2Sdougm proto = sa_get_property_attr(prop, "type"); 4181f85463f2Sdougm if (proto == NULL) { 4182f85463f2Sdougm ret = SA_NO_MEMORY; 4183f85463f2Sdougm continue; 4184f85463f2Sdougm } 4185f85463f2Sdougm err = sa_proto_share_resource(proto, resource); 4186da6c28aaSamw if (err != SA_OK) 4187da6c28aaSamw ret = err; 4188f85463f2Sdougm sa_free_attr_string(proto); 4189da6c28aaSamw } 4190f85463f2Sdougm sa_free_protoset(protoset); 4191da6c28aaSamw } 4192da6c28aaSamw if (ret == SA_OK) 4193da6c28aaSamw (void) sa_set_resource_attr(resource, "shared", NULL); 4194da6c28aaSamw 4195da6c28aaSamw return (ret); 4196da6c28aaSamw } 4197da6c28aaSamw 4198da6c28aaSamw /* 4199da6c28aaSamw * sa_disable_resource(resource, protocol) 4200da6c28aaSamw * 4201da6c28aaSamw * Disable the specified share for the specified protocol. If 4202da6c28aaSamw * protocol is NULL, then all protocols. If the underlying 4203da6c28aaSamw * protocol doesn't implement disable at the resource level, we 4204da6c28aaSamw * disable at the share level. 4205da6c28aaSamw */ 4206da6c28aaSamw int 4207da6c28aaSamw sa_disable_resource(sa_resource_t resource, char *protocol) 4208da6c28aaSamw { 4209da6c28aaSamw int ret = SA_OK; 4210da6c28aaSamw 4211da6c28aaSamw if (protocol != NULL) { 4212da6c28aaSamw ret = sa_proto_unshare_resource(protocol, resource); 4213da6c28aaSamw if (ret == SA_NOT_IMPLEMENTED) { 4214da6c28aaSamw sa_share_t parent; 4215da6c28aaSamw /* 4216da6c28aaSamw * The protocol doesn't implement unshare 4217da6c28aaSamw * resource. That implies that resource names are 4218da6c28aaSamw * simple aliases for this protocol so we need to 4219da6c28aaSamw * unshare the share. 4220da6c28aaSamw */ 4221da6c28aaSamw parent = sa_get_resource_parent(resource); 4222da6c28aaSamw if (parent != NULL) 4223da6c28aaSamw ret = sa_disable_share(parent, protocol); 4224da6c28aaSamw else 4225da6c28aaSamw ret = SA_CONFIG_ERR; 4226da6c28aaSamw } 4227da6c28aaSamw } else { 4228f85463f2Sdougm sa_optionset_t protoset; 4229f85463f2Sdougm sa_property_t prop; 4230f85463f2Sdougm char *proto; 4231f85463f2Sdougm int err; 4232f85463f2Sdougm 4233da6c28aaSamw /* need to do all protocols */ 4234f85463f2Sdougm protoset = sa_get_active_protocols(resource); 4235f85463f2Sdougm if (protoset == NULL) 4236f85463f2Sdougm return (SA_NO_MEMORY); 4237f85463f2Sdougm for (prop = sa_get_property(protoset, NULL); 4238f85463f2Sdougm prop != NULL; 4239f85463f2Sdougm prop = sa_get_next_property(prop)) { 4240f85463f2Sdougm proto = sa_get_property_attr(prop, "type"); 4241f85463f2Sdougm if (proto == NULL) { 4242f85463f2Sdougm ret = SA_NO_MEMORY; 4243f85463f2Sdougm continue; 4244f85463f2Sdougm } 4245f85463f2Sdougm err = sa_proto_unshare_resource(proto, resource); 4246da6c28aaSamw if (err == SA_NOT_SUPPORTED) { 4247da6c28aaSamw sa_share_t parent; 4248f85463f2Sdougm parent = sa_get_resource_parent(resource); 4249da6c28aaSamw if (parent != NULL) 4250f85463f2Sdougm err = sa_disable_share(parent, proto); 4251da6c28aaSamw else 4252da6c28aaSamw err = SA_CONFIG_ERR; 4253da6c28aaSamw } 4254da6c28aaSamw if (err != SA_OK) 4255da6c28aaSamw ret = err; 4256f85463f2Sdougm sa_free_attr_string(proto); 4257da6c28aaSamw } 4258f85463f2Sdougm sa_free_protoset(protoset); 4259da6c28aaSamw } 4260da6c28aaSamw if (ret == SA_OK) 4261da6c28aaSamw (void) sa_set_resource_attr(resource, "shared", NULL); 4262da6c28aaSamw 4263da6c28aaSamw return (ret); 4264da6c28aaSamw } 4265da6c28aaSamw 4266da6c28aaSamw /* 4267da6c28aaSamw * sa_set_resource_description(resource, content) 4268da6c28aaSamw * 4269da6c28aaSamw * Set the description of share to content. 4270da6c28aaSamw */ 4271da6c28aaSamw 4272da6c28aaSamw int 4273da6c28aaSamw sa_set_resource_description(sa_resource_t resource, char *content) 4274da6c28aaSamw { 4275da6c28aaSamw xmlNodePtr node; 4276da6c28aaSamw sa_group_t group; 4277da6c28aaSamw sa_share_t share; 4278da6c28aaSamw int ret = SA_OK; 4279da6c28aaSamw 4280da6c28aaSamw for (node = ((xmlNodePtr)resource)->children; 4281da6c28aaSamw node != NULL; 4282da6c28aaSamw node = node->next) { 4283da6c28aaSamw if (xmlStrcmp(node->name, (xmlChar *)"description") == 0) { 4284da6c28aaSamw break; 4285da6c28aaSamw } 4286da6c28aaSamw } 4287da6c28aaSamw 4288da6c28aaSamw /* no existing description but want to add */ 4289da6c28aaSamw if (node == NULL && content != NULL) { 4290da6c28aaSamw /* add a description */ 4291da6c28aaSamw node = _sa_set_share_description(resource, content); 4292da6c28aaSamw } else if (node != NULL && content != NULL) { 4293da6c28aaSamw /* update a description */ 4294da6c28aaSamw xmlNodeSetContent(node, (xmlChar *)content); 4295da6c28aaSamw } else if (node != NULL && content == NULL) { 4296da6c28aaSamw /* remove an existing description */ 4297da6c28aaSamw xmlUnlinkNode(node); 4298da6c28aaSamw xmlFreeNode(node); 4299da6c28aaSamw } 4300da6c28aaSamw share = sa_get_resource_parent(resource); 4301da6c28aaSamw group = sa_get_parent_group(share); 4302da6c28aaSamw if (group != NULL && sa_is_persistent(share)) { 4303da6c28aaSamw sa_handle_impl_t impl_handle; 4304da6c28aaSamw impl_handle = (sa_handle_impl_t)sa_find_group_handle(group); 4305da6c28aaSamw if (impl_handle != NULL) 4306da6c28aaSamw ret = sa_commit_share(impl_handle->scfhandle, 4307da6c28aaSamw group, share); 4308da6c28aaSamw else 4309da6c28aaSamw ret = SA_SYSTEM_ERR; 4310da6c28aaSamw } 4311da6c28aaSamw return (ret); 4312da6c28aaSamw } 4313da6c28aaSamw 4314da6c28aaSamw /* 4315da6c28aaSamw * sa_get_resource_description(share) 4316da6c28aaSamw * 4317da6c28aaSamw * Return the description text for the specified share if it 4318da6c28aaSamw * exists. NULL if no description exists. 4319da6c28aaSamw */ 4320da6c28aaSamw 4321da6c28aaSamw char * 4322da6c28aaSamw sa_get_resource_description(sa_resource_t resource) 4323da6c28aaSamw { 4324da6c28aaSamw xmlChar *description = NULL; 4325da6c28aaSamw xmlNodePtr node; 4326da6c28aaSamw 4327da6c28aaSamw for (node = ((xmlNodePtr)resource)->children; node != NULL; 4328da6c28aaSamw node = node->next) { 4329da6c28aaSamw if (xmlStrcmp(node->name, (xmlChar *)"description") == 0) 4330da6c28aaSamw break; 4331da6c28aaSamw } 4332da6c28aaSamw if (node != NULL) { 4333da6c28aaSamw description = xmlNodeGetContent(node); 4334da6c28aaSamw fixproblemchars((char *)description); 4335da6c28aaSamw } 4336da6c28aaSamw return ((char *)description); 4337da6c28aaSamw } 4338