13910Sdougm /* 23910Sdougm * CDDL HEADER START 33910Sdougm * 43910Sdougm * The contents of this file are subject to the terms of the 53910Sdougm * Common Development and Distribution License (the "License"). 63910Sdougm * You may not use this file except in compliance with the License. 73910Sdougm * 83910Sdougm * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 93910Sdougm * or http://www.opensolaris.org/os/licensing. 103910Sdougm * See the License for the specific language governing permissions 113910Sdougm * and limitations under the License. 123910Sdougm * 133910Sdougm * When distributing Covered Code, include this CDDL HEADER in each 143910Sdougm * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 153910Sdougm * If applicable, add the following below this CDDL HEADER, with the 163910Sdougm * fields enclosed by brackets "[]" replaced with your own identifying 173910Sdougm * information: Portions Copyright [yyyy] [name of copyright owner] 183910Sdougm * 193910Sdougm * CDDL HEADER END 203910Sdougm */ 213910Sdougm 223910Sdougm /* 238845Samw@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 243910Sdougm * Use is subject to license terms. 253910Sdougm */ 263910Sdougm 273910Sdougm /* 283910Sdougm * NFS specific functions 293910Sdougm */ 303910Sdougm #include <stdio.h> 313910Sdougm #include <string.h> 323910Sdougm #include <ctype.h> 333910Sdougm #include <stdlib.h> 343910Sdougm #include <unistd.h> 353910Sdougm #include <zone.h> 363910Sdougm #include <errno.h> 373910Sdougm #include <locale.h> 383910Sdougm #include <signal.h> 393910Sdougm #include "libshare.h" 403910Sdougm #include "libshare_impl.h" 413910Sdougm #include <nfs/export.h> 423910Sdougm #include <pwd.h> 433910Sdougm #include <limits.h> 443910Sdougm #include <libscf.h> 453910Sdougm #include "nfslog_config.h" 463910Sdougm #include "nfslogtab.h" 473910Sdougm #include "libshare_nfs.h" 483910Sdougm #include <rpcsvc/daemon_utils.h> 493910Sdougm #include <nfs/nfs.h> 504543Smarks #include <nfs/nfssys.h> 513910Sdougm 523910Sdougm /* should really be in some global place */ 533910Sdougm #define DEF_WIN 30000 543910Sdougm #define OPT_CHUNK 1024 553910Sdougm 563910Sdougm int debug = 0; 573910Sdougm 584543Smarks #define NFS_SERVER_SVC "svc:/network/nfs/server:default" 593910Sdougm 603910Sdougm /* internal functions */ 613910Sdougm static int nfs_init(); 623910Sdougm static void nfs_fini(); 633910Sdougm static int nfs_enable_share(sa_share_t); 644543Smarks static int nfs_disable_share(sa_share_t, char *); 656214Sdougm static int nfs_validate_property(sa_handle_t, sa_property_t, sa_optionset_t); 663910Sdougm static int nfs_validate_security_mode(char *); 673910Sdougm static int nfs_is_security_opt(char *); 683910Sdougm static int nfs_parse_legacy_options(sa_group_t, char *); 693910Sdougm static char *nfs_format_options(sa_group_t, int); 703910Sdougm static int nfs_set_proto_prop(sa_property_t); 713910Sdougm static sa_protocol_properties_t nfs_get_proto_set(); 723910Sdougm static char *nfs_get_status(); 733910Sdougm static char *nfs_space_alias(char *); 745331Samw static uint64_t nfs_features(); 753910Sdougm 763910Sdougm /* 773910Sdougm * ops vector that provides the protocol specific info and operations 783910Sdougm * for share management. 793910Sdougm */ 803910Sdougm 813910Sdougm struct sa_plugin_ops sa_plugin_ops = { 823910Sdougm SA_PLUGIN_VERSION, 833910Sdougm "nfs", 843910Sdougm nfs_init, 853910Sdougm nfs_fini, 863910Sdougm nfs_enable_share, 873910Sdougm nfs_disable_share, 883910Sdougm nfs_validate_property, 893910Sdougm nfs_validate_security_mode, 903910Sdougm nfs_is_security_opt, 913910Sdougm nfs_parse_legacy_options, 923910Sdougm nfs_format_options, 933910Sdougm nfs_set_proto_prop, 943910Sdougm nfs_get_proto_set, 953910Sdougm nfs_get_status, 963910Sdougm nfs_space_alias, 975331Samw NULL, /* update_legacy */ 985331Samw NULL, /* delete_legacy */ 995331Samw NULL, /* change_notify */ 1005331Samw NULL, /* enable_resource */ 1015331Samw NULL, /* disable_resource */ 1025331Samw nfs_features, 1035331Samw NULL, /* transient shares */ 1045331Samw NULL, /* notify resource */ 1056007Sthurlow NULL, /* rename_resource */ 1066007Sthurlow NULL, /* run_command */ 1076007Sthurlow NULL, /* command_help */ 1086007Sthurlow NULL /* delete_proto_section */ 1093910Sdougm }; 1103910Sdougm 1113910Sdougm /* 1123910Sdougm * list of support services needed 1133910Sdougm * defines should come from head/rpcsvc/daemon_utils.h 1143910Sdougm */ 1153910Sdougm 1163910Sdougm static char *service_list_default[] = 1173910Sdougm { STATD, LOCKD, MOUNTD, NFSD, NFSMAPID, RQUOTAD, NULL }; 1183910Sdougm static char *service_list_logging[] = 1193910Sdougm { STATD, LOCKD, MOUNTD, NFSD, NFSMAPID, RQUOTAD, NFSLOGD, NULL }; 1203910Sdougm 1213910Sdougm /* 1223910Sdougm * option definitions. Make sure to keep the #define for the option 1233910Sdougm * index just before the entry it is the index for. Changing the order 1243910Sdougm * can cause breakage. E.g OPT_RW is index 1 and must precede the 1253910Sdougm * line that includes the SHOPT_RW and OPT_RW entries. 1263910Sdougm */ 1273910Sdougm 1283910Sdougm struct option_defs optdefs[] = { 1293910Sdougm #define OPT_RO 0 1303910Sdougm {SHOPT_RO, OPT_RO, OPT_TYPE_ACCLIST}, 1313910Sdougm #define OPT_RW 1 1323910Sdougm {SHOPT_RW, OPT_RW, OPT_TYPE_ACCLIST}, 1333910Sdougm #define OPT_ROOT 2 1343910Sdougm {SHOPT_ROOT, OPT_ROOT, OPT_TYPE_ACCLIST}, 1353910Sdougm #define OPT_SECURE 3 1363910Sdougm {SHOPT_SECURE, OPT_SECURE, OPT_TYPE_DEPRECATED}, 1373910Sdougm #define OPT_ANON 4 1383910Sdougm {SHOPT_ANON, OPT_ANON, OPT_TYPE_USER}, 1393910Sdougm #define OPT_WINDOW 5 1403910Sdougm {SHOPT_WINDOW, OPT_WINDOW, OPT_TYPE_NUMBER}, 1413910Sdougm #define OPT_NOSUID 6 1423910Sdougm {SHOPT_NOSUID, OPT_NOSUID, OPT_TYPE_BOOLEAN}, 1433910Sdougm #define OPT_ACLOK 7 1443910Sdougm {SHOPT_ACLOK, OPT_ACLOK, OPT_TYPE_BOOLEAN}, 1453910Sdougm #define OPT_NOSUB 8 1463910Sdougm {SHOPT_NOSUB, OPT_NOSUB, OPT_TYPE_BOOLEAN}, 1473910Sdougm #define OPT_SEC 9 1483910Sdougm {SHOPT_SEC, OPT_SEC, OPT_TYPE_SECURITY}, 1493910Sdougm #define OPT_PUBLIC 10 1503910Sdougm {SHOPT_PUBLIC, OPT_PUBLIC, OPT_TYPE_BOOLEAN, OPT_SHARE_ONLY}, 1513910Sdougm #define OPT_INDEX 11 1523910Sdougm {SHOPT_INDEX, OPT_INDEX, OPT_TYPE_FILE}, 1533910Sdougm #define OPT_LOG 12 1543910Sdougm {SHOPT_LOG, OPT_LOG, OPT_TYPE_LOGTAG}, 1553910Sdougm #define OPT_CKSUM 13 1563910Sdougm {SHOPT_CKSUM, OPT_CKSUM, OPT_TYPE_STRINGSET}, 1577961SNatalie.Li@Sun.COM #define OPT_NONE 14 1587961SNatalie.Li@Sun.COM {SHOPT_NONE, OPT_NONE, OPT_TYPE_ACCLIST}, 1597961SNatalie.Li@Sun.COM #define OPT_ROOT_MAPPING 15 1607961SNatalie.Li@Sun.COM {SHOPT_ROOT_MAPPING, OPT_ROOT_MAPPING, OPT_TYPE_USER}, 1617961SNatalie.Li@Sun.COM #define OPT_CHARSET_MAP 16 1627961SNatalie.Li@Sun.COM {"", OPT_CHARSET_MAP, OPT_TYPE_ACCLIST}, 1633910Sdougm #ifdef VOLATILE_FH_TEST /* XXX added for testing volatile fh's only */ 1647961SNatalie.Li@Sun.COM #define OPT_VOLFH 17 1653910Sdougm {SHOPT_VOLFH, OPT_VOLFH}, 1663910Sdougm #endif /* VOLATILE_FH_TEST */ 1673910Sdougm NULL 1683910Sdougm }; 1693910Sdougm 1703910Sdougm /* 1717961SNatalie.Li@Sun.COM * Codesets that may need to be converted to UTF-8 for file paths. 1727961SNatalie.Li@Sun.COM * Add new names here to add new property support. If we ever get a 1737961SNatalie.Li@Sun.COM * way to query the kernel for character sets, this should become 1747961SNatalie.Li@Sun.COM * dynamically loaded. Make sure changes here are reflected in 1757961SNatalie.Li@Sun.COM * cmd/fs.d/nfs/mountd/nfscmd.c 1767961SNatalie.Li@Sun.COM */ 1777961SNatalie.Li@Sun.COM 1787961SNatalie.Li@Sun.COM static char *legal_conv[] = { 1797961SNatalie.Li@Sun.COM "euc-cn", 1807961SNatalie.Li@Sun.COM "euc-jp", 1817961SNatalie.Li@Sun.COM "euc-jpms", 1827961SNatalie.Li@Sun.COM "euc-kr", 1837961SNatalie.Li@Sun.COM "euc-tw", 1847961SNatalie.Li@Sun.COM "iso8859-1", 1857961SNatalie.Li@Sun.COM "iso8859-2", 1867961SNatalie.Li@Sun.COM "iso8859-5", 1877961SNatalie.Li@Sun.COM "iso8859-6", 1887961SNatalie.Li@Sun.COM "iso8859-7", 1897961SNatalie.Li@Sun.COM "iso8859-8", 1907961SNatalie.Li@Sun.COM "iso8859-9", 1917961SNatalie.Li@Sun.COM "iso8859-13", 1927961SNatalie.Li@Sun.COM "iso8859-15", 1937961SNatalie.Li@Sun.COM "koi8-r", 1947961SNatalie.Li@Sun.COM NULL 1957961SNatalie.Li@Sun.COM }; 1967961SNatalie.Li@Sun.COM 1977961SNatalie.Li@Sun.COM /* 1983910Sdougm * list of properties that are related to security flavors. 1993910Sdougm */ 2003910Sdougm static char *seclist[] = { 2013910Sdougm SHOPT_RO, 2023910Sdougm SHOPT_RW, 2033910Sdougm SHOPT_ROOT, 2043910Sdougm SHOPT_WINDOW, 2057961SNatalie.Li@Sun.COM SHOPT_NONE, 2067961SNatalie.Li@Sun.COM SHOPT_ROOT_MAPPING, 2073910Sdougm NULL 2083910Sdougm }; 2093910Sdougm 2103910Sdougm /* structure for list of securities */ 2113910Sdougm struct securities { 2123910Sdougm sa_security_t security; 2133910Sdougm struct securities *next; 2143910Sdougm }; 2153910Sdougm 2163910Sdougm /* 2177961SNatalie.Li@Sun.COM * findcharset(charset) 2187961SNatalie.Li@Sun.COM * 2197961SNatalie.Li@Sun.COM * Returns B_TRUE if the charset is a legal conversion otherwise 2207961SNatalie.Li@Sun.COM * B_FALSE. This will need to be rewritten to be more efficient when 2217961SNatalie.Li@Sun.COM * we have a dynamic list of legal conversions. 2227961SNatalie.Li@Sun.COM */ 2237961SNatalie.Li@Sun.COM 2247961SNatalie.Li@Sun.COM static boolean_t 2257961SNatalie.Li@Sun.COM findcharset(char *charset) 2267961SNatalie.Li@Sun.COM { 2277961SNatalie.Li@Sun.COM int i; 2287961SNatalie.Li@Sun.COM 2297961SNatalie.Li@Sun.COM for (i = 0; legal_conv[i] != NULL; i++) 2307961SNatalie.Li@Sun.COM if (strcmp(charset, legal_conv[i]) == 0) 2317961SNatalie.Li@Sun.COM return (B_TRUE); 2327961SNatalie.Li@Sun.COM return (B_FALSE); 2337961SNatalie.Li@Sun.COM } 2347961SNatalie.Li@Sun.COM 2357961SNatalie.Li@Sun.COM /* 2363910Sdougm * findopt(name) 2373910Sdougm * 2383910Sdougm * Lookup option "name" in the option table and return the table 2393910Sdougm * index. 2403910Sdougm */ 2413910Sdougm 2423910Sdougm static int 2433910Sdougm findopt(char *name) 2443910Sdougm { 2453910Sdougm int i; 2463910Sdougm if (name != NULL) { 2474345Sdougm for (i = 0; optdefs[i].tag != NULL; i++) { 2484345Sdougm if (strcmp(optdefs[i].tag, name) == 0) 2494345Sdougm return (i); 2504345Sdougm } 2517961SNatalie.Li@Sun.COM if (findcharset(name)) 2527961SNatalie.Li@Sun.COM return (OPT_CHARSET_MAP); 2533910Sdougm } 2543910Sdougm return (-1); 2553910Sdougm } 2563910Sdougm 2573910Sdougm /* 2583910Sdougm * gettype(name) 2593910Sdougm * 2603910Sdougm * Return the type of option "name". 2613910Sdougm */ 2623910Sdougm 2633910Sdougm static int 2643910Sdougm gettype(char *name) 2653910Sdougm { 2663910Sdougm int optdef; 2673910Sdougm 2683910Sdougm optdef = findopt(name); 2693910Sdougm if (optdef != -1) 2704345Sdougm return (optdefs[optdef].type); 2713910Sdougm return (OPT_TYPE_ANY); 2723910Sdougm } 2733910Sdougm 2743910Sdougm /* 2753910Sdougm * nfs_validate_security_mode(mode) 2763910Sdougm * 2773910Sdougm * is the specified mode string a valid one for use with NFS? 2783910Sdougm */ 2793910Sdougm 2803910Sdougm static int 2813910Sdougm nfs_validate_security_mode(char *mode) 2823910Sdougm { 2833910Sdougm seconfig_t secinfo; 2843910Sdougm int err; 2853910Sdougm 2863910Sdougm (void) memset(&secinfo, '\0', sizeof (secinfo)); 2873910Sdougm err = nfs_getseconfig_byname(mode, &secinfo); 2883910Sdougm if (err == SC_NOERROR) 2894345Sdougm return (1); 2903910Sdougm return (0); 2913910Sdougm } 2923910Sdougm 2933910Sdougm /* 2943910Sdougm * nfs_is_security_opt(tok) 2953910Sdougm * 2963910Sdougm * check to see if tok represents an option that is only valid in some 2973910Sdougm * security flavor. 2983910Sdougm */ 2993910Sdougm 3003910Sdougm static int 3013910Sdougm nfs_is_security_opt(char *tok) 3023910Sdougm { 3033910Sdougm int i; 3043910Sdougm 3053910Sdougm for (i = 0; seclist[i] != NULL; i++) { 3064345Sdougm if (strcmp(tok, seclist[i]) == 0) 3074345Sdougm return (1); 3083910Sdougm } 3093910Sdougm return (0); 3103910Sdougm } 3113910Sdougm 3123910Sdougm /* 3133910Sdougm * find_security(seclist, sec) 3143910Sdougm * 3153910Sdougm * Walk the current list of security flavors and return true if it is 3163910Sdougm * present, else return false. 3173910Sdougm */ 3183910Sdougm 3193910Sdougm static int 3203910Sdougm find_security(struct securities *seclist, sa_security_t sec) 3213910Sdougm { 3223910Sdougm while (seclist != NULL) { 3234345Sdougm if (seclist->security == sec) 3244345Sdougm return (1); 3254345Sdougm seclist = seclist->next; 3263910Sdougm } 3273910Sdougm return (0); 3283910Sdougm } 3293910Sdougm 3303910Sdougm /* 3313910Sdougm * make_security_list(group, securitymodes, proto) 3323910Sdougm * go through the list of securitymodes and add them to the 3333910Sdougm * group's list of security optionsets. We also keep a list of 3343910Sdougm * those optionsets so we don't have to find them later. All of 3353910Sdougm * these will get copies of the same properties. 3363910Sdougm */ 3373910Sdougm 3383910Sdougm static struct securities * 3393910Sdougm make_security_list(sa_group_t group, char *securitymodes, char *proto) 3403910Sdougm { 3413910Sdougm char *tok, *next = NULL; 3423910Sdougm struct securities *curp, *headp = NULL, *prev; 3433910Sdougm sa_security_t check; 3443910Sdougm int freetok = 0; 3453910Sdougm 3463910Sdougm for (tok = securitymodes; tok != NULL; tok = next) { 3474345Sdougm next = strchr(tok, ':'); 3484345Sdougm if (next != NULL) 3494345Sdougm *next++ = '\0'; 3504345Sdougm if (strcmp(tok, "default") == 0) { 3514345Sdougm /* resolve default into the real type */ 3524345Sdougm tok = nfs_space_alias(tok); 3534345Sdougm freetok = 1; 3544345Sdougm } 3554345Sdougm check = sa_get_security(group, tok, proto); 3563910Sdougm 3574345Sdougm /* add to the security list if it isn't there already */ 3584345Sdougm if (check == NULL || !find_security(headp, check)) { 3594345Sdougm curp = (struct securities *)calloc(1, 3604345Sdougm sizeof (struct securities)); 3614345Sdougm if (curp != NULL) { 3624345Sdougm if (check == NULL) { 3634345Sdougm curp->security = sa_create_security( 3644345Sdougm group, tok, proto); 3654345Sdougm } else { 3664345Sdougm curp->security = check; 3674345Sdougm } 3684345Sdougm /* 3694345Sdougm * note that the first time through the loop, 3704345Sdougm * headp will be NULL and prev will be 3714345Sdougm * undefined. Since headp is NULL, we set 3724345Sdougm * both it and prev to the curp (first 3734345Sdougm * structure to be allocated). 3744345Sdougm * 3754345Sdougm * later passes through the loop will have 3764345Sdougm * headp not being NULL and prev will be used 3774345Sdougm * to allocate at the end of the list. 3784345Sdougm */ 3794345Sdougm if (headp == NULL) { 3804345Sdougm headp = curp; 3814345Sdougm prev = curp; 3824345Sdougm } else { 3834345Sdougm prev->next = curp; 3844345Sdougm prev = curp; 3854345Sdougm } 3864345Sdougm } 3873910Sdougm } 3883910Sdougm 3894345Sdougm if (freetok) { 3904345Sdougm freetok = 0; 3914345Sdougm sa_free_attr_string(tok); 3924345Sdougm } 3933910Sdougm } 3943910Sdougm return (headp); 3953910Sdougm } 3963910Sdougm 3973910Sdougm static void 3983910Sdougm free_security_list(struct securities *sec) 3993910Sdougm { 4003910Sdougm struct securities *next; 4013910Sdougm if (sec != NULL) { 4024345Sdougm for (next = sec->next; sec != NULL; sec = next) { 4034345Sdougm next = sec->next; 4044345Sdougm free(sec); 4054345Sdougm } 4063910Sdougm } 4073910Sdougm } 4083910Sdougm 4093910Sdougm /* 4103910Sdougm * nfs_alistcat(str1, str2, sep) 4113910Sdougm * 4123910Sdougm * concatenate str1 and str2 into a new string using sep as a separate 4133910Sdougm * character. If memory allocation fails, return NULL; 4143910Sdougm */ 4153910Sdougm 4163910Sdougm static char * 4173910Sdougm nfs_alistcat(char *str1, char *str2, char sep) 4183910Sdougm { 4193910Sdougm char *newstr; 4203910Sdougm size_t len; 4213910Sdougm 4223910Sdougm len = strlen(str1) + strlen(str2) + 2; 4233910Sdougm newstr = (char *)malloc(len); 4243910Sdougm if (newstr != NULL) 4254345Sdougm (void) snprintf(newstr, len, "%s%c%s", str1, sep, str2); 4263910Sdougm return (newstr); 4273910Sdougm } 4283910Sdougm 4293910Sdougm /* 4303910Sdougm * add_security_prop(sec, name, value, persist) 4313910Sdougm * 4323910Sdougm * Add the property to the securities structure. This accumulates 4333910Sdougm * properties for as part of parsing legacy options. 4343910Sdougm */ 4353910Sdougm 4363910Sdougm static int 4373910Sdougm add_security_prop(struct securities *sec, char *name, char *value, 4383910Sdougm int persist, int iszfs) 4393910Sdougm { 4403910Sdougm sa_property_t prop; 4413910Sdougm int ret = SA_OK; 4423910Sdougm 4433910Sdougm for (; sec != NULL; sec = sec->next) { 4444345Sdougm if (value == NULL) { 4454345Sdougm if (strcmp(name, SHOPT_RW) == 0 || 4464345Sdougm strcmp(name, SHOPT_RO) == 0) 4474345Sdougm value = "*"; 4484345Sdougm else 4494345Sdougm value = "true"; 4504345Sdougm } 4513910Sdougm 4523910Sdougm /* 4533910Sdougm * Get the existing property, if it exists, so we can 4543910Sdougm * determine what to do with it. The ro/rw/root 4553910Sdougm * properties can be merged if multiple instances of 4563910Sdougm * these properies are given. For example, if "rw" 4573910Sdougm * exists with a value "host1" and a later token of 4583910Sdougm * rw="host2" is seen, the values are merged into a 4593910Sdougm * single rw="host1:host2". 4603910Sdougm */ 4614345Sdougm prop = sa_get_property(sec->security, name); 4623910Sdougm 4634345Sdougm if (prop != NULL) { 4644345Sdougm char *oldvalue; 4654345Sdougm char *newvalue; 4663910Sdougm 4673910Sdougm /* 4684345Sdougm * The security options of ro/rw/root might appear 4694345Sdougm * multiple times. If they do, the values need to be 4704345Sdougm * merged into an access list. If it was previously 4714345Sdougm * empty, the new value alone is added. 4723910Sdougm */ 4734345Sdougm oldvalue = sa_get_property_attr(prop, "value"); 4744345Sdougm if (oldvalue != NULL) { 4754345Sdougm /* 4764345Sdougm * The general case is to concatenate the new 4774345Sdougm * value onto the old value for multiple 4784345Sdougm * rw(ro/root) properties. A special case 4794345Sdougm * exists when either the old or new is the 4804345Sdougm * "all" case. In the special case, if both 4814345Sdougm * are "all", then it is "all", else if one is 4824345Sdougm * an access-list, that replaces the "all". 4834345Sdougm */ 4844345Sdougm if (strcmp(oldvalue, "*") == 0) { 4854345Sdougm /* Replace old value with new value. */ 4864345Sdougm newvalue = strdup(value); 4875454Sdougm } else if (strcmp(value, "*") == 0 || 4885454Sdougm strcmp(oldvalue, value) == 0) { 4894345Sdougm /* 4904345Sdougm * Keep old value and ignore 4914345Sdougm * the new value. 4924345Sdougm */ 4934345Sdougm newvalue = NULL; 4944345Sdougm } else { 4954345Sdougm /* 4964345Sdougm * Make a new list of old plus new 4974345Sdougm * access-list. 4984345Sdougm */ 4994345Sdougm newvalue = nfs_alistcat(oldvalue, 5004345Sdougm value, ':'); 5014345Sdougm } 5023910Sdougm 5034345Sdougm if (newvalue != NULL) { 5044345Sdougm (void) sa_remove_property(prop); 5054345Sdougm prop = sa_create_property(name, 5064345Sdougm newvalue); 5074345Sdougm ret = sa_add_property(sec->security, 5084345Sdougm prop); 5094345Sdougm free(newvalue); 5104345Sdougm } 5114345Sdougm if (oldvalue != NULL) 5124345Sdougm sa_free_attr_string(oldvalue); 5134345Sdougm } 5144345Sdougm } else { 5154345Sdougm prop = sa_create_property(name, value); 5163910Sdougm ret = sa_add_property(sec->security, prop); 5173910Sdougm } 5184345Sdougm if (ret == SA_OK && !iszfs) { 5194345Sdougm ret = sa_commit_properties(sec->security, !persist); 5204345Sdougm } 5213910Sdougm } 5223910Sdougm return (ret); 5233910Sdougm } 5243910Sdougm 5253910Sdougm /* 5263910Sdougm * check to see if group/share is persistent. 5273910Sdougm */ 5283910Sdougm static int 5293910Sdougm is_persistent(sa_group_t group) 5303910Sdougm { 5313910Sdougm char *type; 5323910Sdougm int persist = 1; 5333910Sdougm 5343910Sdougm type = sa_get_group_attr(group, "type"); 5353910Sdougm if (type != NULL && strcmp(type, "persist") != 0) 5364345Sdougm persist = 0; 5373910Sdougm if (type != NULL) 5384345Sdougm sa_free_attr_string(type); 5393910Sdougm return (persist); 5403910Sdougm } 5413910Sdougm 5423910Sdougm /* 5433910Sdougm * invalid_security(options) 5443910Sdougm * 5453910Sdougm * search option string for any invalid sec= type. 5463910Sdougm * return true (1) if any are not valid else false (0) 5473910Sdougm */ 5483910Sdougm static int 5493910Sdougm invalid_security(char *options) 5503910Sdougm { 5513910Sdougm char *copy, *base, *token, *value; 5523910Sdougm int ret = 0; 5533910Sdougm 5543910Sdougm copy = strdup(options); 5553910Sdougm token = base = copy; 5563910Sdougm while (token != NULL && ret == 0) { 5574345Sdougm token = strtok(base, ","); 5584345Sdougm base = NULL; 5594345Sdougm if (token != NULL) { 5604345Sdougm value = strchr(token, '='); 5614345Sdougm if (value != NULL) 5624345Sdougm *value++ = '\0'; 5634345Sdougm if (strcmp(token, "sec") == 0) { 5644345Sdougm /* HAVE security flavors so check them */ 5654345Sdougm char *tok, *next; 5664345Sdougm for (next = NULL, tok = value; tok != NULL; 5674345Sdougm tok = next) { 5684345Sdougm next = strchr(tok, ':'); 5694345Sdougm if (next != NULL) 5704345Sdougm *next++ = '\0'; 5714345Sdougm ret = !nfs_validate_security_mode(tok); 5724345Sdougm if (ret) 5734345Sdougm break; 5744345Sdougm } 5754345Sdougm } 5763910Sdougm } 5773910Sdougm } 5783910Sdougm if (copy != NULL) 5794345Sdougm free(copy); 5803910Sdougm return (ret); 5813910Sdougm } 5823910Sdougm 5833910Sdougm /* 5843910Sdougm * nfs_parse_legacy_options(group, options) 5853910Sdougm * 5863910Sdougm * Parse the old style options into internal format and store on the 5873910Sdougm * specified group. Group could be a share for full legacy support. 5883910Sdougm */ 5893910Sdougm 5903910Sdougm static int 5913910Sdougm nfs_parse_legacy_options(sa_group_t group, char *options) 5923910Sdougm { 5934704Sdougm char *dup; 5943910Sdougm char *base; 5953910Sdougm char *token; 5963910Sdougm sa_optionset_t optionset; 5973910Sdougm struct securities *security_list = NULL; 5983910Sdougm sa_property_t prop; 5993910Sdougm int ret = SA_OK; 6003910Sdougm int iszfs = 0; 6013910Sdougm sa_group_t parent; 6023910Sdougm int persist = 0; 6033910Sdougm char *lasts; 6043910Sdougm 6053910Sdougm /* do we have an existing optionset? */ 6063910Sdougm optionset = sa_get_optionset(group, "nfs"); 6073910Sdougm if (optionset == NULL) { 6084345Sdougm /* didn't find existing optionset so create one */ 6094345Sdougm optionset = sa_create_optionset(group, "nfs"); 6103910Sdougm } else { 6113910Sdougm /* 6125331Samw * Have an existing optionset . Ideally, we would need 6135331Samw * to compare options in order to detect errors. For 6145331Samw * now, we assume that the first optionset is the 6155331Samw * correct one and the others will be the same. An 6165331Samw * empty optionset is the same as no optionset so we 6175331Samw * don't want to exit in that case. Getting an empty 6185331Samw * optionset can occur with ZFS property checking. 6193910Sdougm */ 6205331Samw if (sa_get_property(optionset, NULL) != NULL) 6215331Samw return (ret); 6223910Sdougm } 6233910Sdougm 6243910Sdougm if (strcmp(options, SHOPT_RW) == 0) { 6253910Sdougm /* 6263910Sdougm * there is a special case of only the option "rw" 6273910Sdougm * being the default option. We don't have to do 6283910Sdougm * anything. 6293910Sdougm */ 6304345Sdougm return (ret); 6313910Sdougm } 6323910Sdougm 6333910Sdougm /* 6343910Sdougm * check if security types are present and validate them. If 6353910Sdougm * any are not legal, fail. 6363910Sdougm */ 6373910Sdougm 6383910Sdougm if (invalid_security(options)) { 6394345Sdougm return (SA_INVALID_SECURITY); 6403910Sdougm } 6413910Sdougm 6423910Sdougm /* 6433910Sdougm * in order to not attempt to change ZFS properties unless 6443910Sdougm * absolutely necessary, we never do it in the legacy parsing. 6453910Sdougm */ 6463910Sdougm if (sa_is_share(group)) { 6474345Sdougm char *zfs; 6484345Sdougm parent = sa_get_parent_group(group); 6494345Sdougm if (parent != NULL) { 6504345Sdougm zfs = sa_get_group_attr(parent, "zfs"); 6514345Sdougm if (zfs != NULL) { 6524345Sdougm sa_free_attr_string(zfs); 6534345Sdougm iszfs++; 6544345Sdougm } 6553910Sdougm } 6563910Sdougm } else { 6574345Sdougm iszfs = sa_group_is_zfs(group); 6583910Sdougm } 6593910Sdougm 6604704Sdougm /* We need a copy of options for the next part. */ 6614704Sdougm dup = strdup(options); 6624704Sdougm if (dup == NULL) 6634704Sdougm return (SA_NO_MEMORY); 6644704Sdougm 6653910Sdougm /* 6663910Sdougm * we need to step through each option in the string and then 6673910Sdougm * add either the option or the security option as needed. If 6683910Sdougm * this is not a persistent share, don't commit to the 6693910Sdougm * repository. If there is an error, we also want to abort the 6703910Sdougm * processing and report it. 6713910Sdougm */ 6723910Sdougm persist = is_persistent(group); 6733910Sdougm base = dup; 6743910Sdougm token = dup; 6753910Sdougm lasts = NULL; 6763910Sdougm while (token != NULL && ret == SA_OK) { 6774345Sdougm ret = SA_OK; 6784345Sdougm token = strtok_r(base, ",", &lasts); 6794345Sdougm base = NULL; 6804345Sdougm if (token != NULL) { 6814345Sdougm char *value; 6823910Sdougm /* 6834345Sdougm * if the option has a value, it will have an '=' to 6844345Sdougm * separate the name from the value. The following 6854345Sdougm * code will result in value != NULL and token 6864345Sdougm * pointing to just the name if there is a value. 6873910Sdougm */ 6884345Sdougm value = strchr(token, '='); 6894345Sdougm if (value != NULL) { 6904345Sdougm *value++ = '\0'; 6914345Sdougm } 6924345Sdougm if (strcmp(token, "sec") == 0 || 6934345Sdougm strcmp(token, "secure") == 0) { 6943910Sdougm /* 6954345Sdougm * Once in security parsing, we only 6964345Sdougm * do security. We do need to move 6974345Sdougm * between the security node and the 6984345Sdougm * toplevel. The security tag goes on 6994345Sdougm * the root while the following ones 7004345Sdougm * go on the security. 7013910Sdougm */ 7024345Sdougm if (security_list != NULL) { 7034345Sdougm /* 7044345Sdougm * have an old list so close it and 7054345Sdougm * start the new 7064345Sdougm */ 7074345Sdougm free_security_list(security_list); 7084345Sdougm } 7094345Sdougm if (strcmp(token, "secure") == 0) { 7104345Sdougm value = "dh"; 7114345Sdougm } else { 7124345Sdougm if (value == NULL) { 7134345Sdougm ret = SA_SYNTAX_ERR; 7144345Sdougm break; 7154345Sdougm } 7164345Sdougm } 7174345Sdougm security_list = make_security_list(group, 7184345Sdougm value, "nfs"); 7193910Sdougm } else { 7204345Sdougm /* 7214345Sdougm * Note that the "old" syntax allowed a 7224345Sdougm * default security model This must be 7234345Sdougm * accounted for and internally converted to 7244345Sdougm * "standard" security structure. 7254345Sdougm */ 7264345Sdougm if (nfs_is_security_opt(token)) { 7274345Sdougm if (security_list == NULL) { 7284345Sdougm /* 7294345Sdougm * need to have a 7304345Sdougm * security 7314345Sdougm * option. This will 7324345Sdougm * be "closed" when a 7334345Sdougm * defined "sec=" 7344345Sdougm * option is 7354345Sdougm * seen. This is 7364345Sdougm * technically an 7374345Sdougm * error but will be 7384345Sdougm * allowed with 7394345Sdougm * warning. 7404345Sdougm */ 7414345Sdougm security_list = 7424345Sdougm make_security_list(group, 7434345Sdougm "default", 7444345Sdougm "nfs"); 7454345Sdougm } 7464345Sdougm if (security_list != NULL) { 7474345Sdougm ret = add_security_prop( 7484345Sdougm security_list, token, 7494345Sdougm value, persist, iszfs); 7504345Sdougm } else { 7514345Sdougm ret = SA_NO_MEMORY; 7524345Sdougm } 7534345Sdougm } else { 7544345Sdougm /* regular options */ 7554345Sdougm if (value == NULL) { 7564345Sdougm if (strcmp(token, SHOPT_RW) == 7574345Sdougm 0 || strcmp(token, 7584345Sdougm SHOPT_RO) == 0) { 7594345Sdougm value = "*"; 7604345Sdougm } else { 7614345Sdougm value = "global"; 7624345Sdougm if (strcmp(token, 7634345Sdougm SHOPT_LOG) != 0) { 7644345Sdougm value = "true"; 7654345Sdougm } 7664345Sdougm } 7674345Sdougm } 7684372Sdougm /* 7694372Sdougm * In all cases, create the 7704372Sdougm * property specified. If the 7714372Sdougm * value was NULL, the default 7724372Sdougm * value will have been 7734372Sdougm * substituted. 7744372Sdougm */ 7754372Sdougm prop = sa_create_property(token, value); 7764372Sdougm ret = sa_add_property(optionset, prop); 7774372Sdougm if (ret != SA_OK) 7784372Sdougm break; 7794372Sdougm 7804345Sdougm if (!iszfs) { 7814345Sdougm ret = sa_commit_properties( 7824345Sdougm optionset, !persist); 7834345Sdougm } 7844345Sdougm } 7853910Sdougm } 7863910Sdougm } 7873910Sdougm } 7883910Sdougm if (security_list != NULL) 7894345Sdougm free_security_list(security_list); 7904704Sdougm 7914704Sdougm free(dup); 7923910Sdougm return (ret); 7933910Sdougm } 7943910Sdougm 7953910Sdougm /* 7963910Sdougm * is_a_number(number) 7973910Sdougm * 7983910Sdougm * is the string a number in one of the forms we want to use? 7993910Sdougm */ 8003910Sdougm 8013910Sdougm static int 8023910Sdougm is_a_number(char *number) 8033910Sdougm { 8043910Sdougm int ret = 1; 8053910Sdougm int hex = 0; 8063910Sdougm 8073910Sdougm if (strncmp(number, "0x", 2) == 0) { 8084345Sdougm number += 2; 8094345Sdougm hex = 1; 8104345Sdougm } else if (*number == '-') { 8114345Sdougm number++; /* skip the minus */ 8124345Sdougm } 8133910Sdougm while (ret == 1 && *number != '\0') { 8144345Sdougm if (hex) { 8154345Sdougm ret = isxdigit(*number++); 8164345Sdougm } else { 8174345Sdougm ret = isdigit(*number++); 8184345Sdougm } 8193910Sdougm } 8203910Sdougm return (ret); 8213910Sdougm } 8223910Sdougm 8233910Sdougm /* 8243910Sdougm * Look for the specified tag in the configuration file. If it is found, 8253910Sdougm * enable logging and set the logging configuration information for exp. 8263910Sdougm */ 8273910Sdougm static void 8283910Sdougm configlog(struct exportdata *exp, char *tag) 8293910Sdougm { 8303910Sdougm nfsl_config_t *configlist = NULL, *configp; 8313910Sdougm int error = 0; 8323910Sdougm char globaltag[] = DEFAULTTAG; 8333910Sdougm 8343910Sdougm /* 8353910Sdougm * Sends config errors to stderr 8363910Sdougm */ 8373910Sdougm nfsl_errs_to_syslog = B_FALSE; 8383910Sdougm 8393910Sdougm /* 8403910Sdougm * get the list of configuration settings 8413910Sdougm */ 8423910Sdougm error = nfsl_getconfig_list(&configlist); 8433910Sdougm if (error) { 8443910Sdougm (void) fprintf(stderr, 8454345Sdougm dgettext(TEXT_DOMAIN, "Cannot get log configuration: %s\n"), 8464345Sdougm strerror(error)); 8473910Sdougm } 8483910Sdougm 8493910Sdougm if (tag == NULL) 8503910Sdougm tag = globaltag; 8513910Sdougm if ((configp = nfsl_findconfig(configlist, tag, &error)) == NULL) { 8523910Sdougm nfsl_freeconfig_list(&configlist); 8533910Sdougm (void) fprintf(stderr, 8544345Sdougm dgettext(TEXT_DOMAIN, "No tags matching \"%s\"\n"), tag); 8553910Sdougm /* bad configuration */ 8563910Sdougm error = ENOENT; 8573910Sdougm goto err; 8583910Sdougm } 8593910Sdougm 8603910Sdougm if ((exp->ex_tag = strdup(tag)) == NULL) { 8613910Sdougm error = ENOMEM; 8623910Sdougm goto out; 8633910Sdougm } 8643910Sdougm if ((exp->ex_log_buffer = strdup(configp->nc_bufferpath)) == NULL) { 8653910Sdougm error = ENOMEM; 8663910Sdougm goto out; 8673910Sdougm } 8683910Sdougm exp->ex_flags |= EX_LOG; 8693910Sdougm if (configp->nc_rpclogpath != NULL) 8703910Sdougm exp->ex_flags |= EX_LOG_ALLOPS; 8713910Sdougm out: 8723910Sdougm if (configlist != NULL) 8734345Sdougm nfsl_freeconfig_list(&configlist); 8743910Sdougm 8753910Sdougm err: 8763910Sdougm if (error != 0) { 8773910Sdougm if (exp->ex_flags != NULL) 8783910Sdougm free(exp->ex_tag); 8793910Sdougm if (exp->ex_log_buffer != NULL) 8803910Sdougm free(exp->ex_log_buffer); 8813910Sdougm (void) fprintf(stderr, 8824345Sdougm dgettext(TEXT_DOMAIN, "Cannot set log configuration: %s\n"), 8834345Sdougm strerror(error)); 8843910Sdougm } 8853910Sdougm } 8863910Sdougm 8873910Sdougm /* 8883910Sdougm * fill_export_from_optionset(export, optionset) 8893910Sdougm * 8903910Sdougm * In order to share, we need to set all the possible general options 8913910Sdougm * into the export structure. Share info will be filled in by the 8923910Sdougm * caller. Various property values get turned into structure specific 8933910Sdougm * values. 8943910Sdougm */ 8953910Sdougm 8963910Sdougm static int 8973910Sdougm fill_export_from_optionset(struct exportdata *export, sa_optionset_t optionset) 8983910Sdougm { 8993910Sdougm sa_property_t option; 9003910Sdougm int ret = SA_OK; 9013910Sdougm 9023910Sdougm for (option = sa_get_property(optionset, NULL); 9034345Sdougm option != NULL; option = sa_get_next_property(option)) { 9044345Sdougm char *name; 9054345Sdougm char *value; 9064345Sdougm uint32_t val; 9073910Sdougm 9084345Sdougm /* 9094345Sdougm * since options may be set/reset multiple times, always do an 9104345Sdougm * explicit set or clear of the option. This allows defaults 9115331Samw * to be set and then the protocol specific to override. 9124345Sdougm */ 9133910Sdougm 9144345Sdougm name = sa_get_property_attr(option, "type"); 9154345Sdougm value = sa_get_property_attr(option, "value"); 9164345Sdougm switch (findopt(name)) { 9174345Sdougm case OPT_ANON: 9184345Sdougm if (value != NULL && is_a_number(value)) { 9194345Sdougm val = strtoul(value, NULL, 0); 9204345Sdougm } else { 9214345Sdougm struct passwd *pw; 9224345Sdougm pw = getpwnam(value != NULL ? value : "nobody"); 9234345Sdougm if (pw != NULL) { 9244345Sdougm val = pw->pw_uid; 9254345Sdougm } else { 9264345Sdougm val = UID_NOBODY; 9274345Sdougm } 9284345Sdougm endpwent(); 9294345Sdougm } 9304345Sdougm export->ex_anon = val; 9314345Sdougm break; 9324345Sdougm case OPT_NOSUID: 9334345Sdougm if (value != NULL && (strcasecmp(value, "true") == 0 || 9344345Sdougm strcmp(value, "1") == 0)) 9354345Sdougm export->ex_flags |= EX_NOSUID; 9364345Sdougm else 9374345Sdougm export->ex_flags &= ~EX_NOSUID; 9384345Sdougm break; 9394345Sdougm case OPT_ACLOK: 9404345Sdougm if (value != NULL && (strcasecmp(value, "true") == 0 || 9414345Sdougm strcmp(value, "1") == 0)) 9424345Sdougm export->ex_flags |= EX_ACLOK; 9434345Sdougm else 9444345Sdougm export->ex_flags &= ~EX_ACLOK; 9454345Sdougm break; 9464345Sdougm case OPT_NOSUB: 9474345Sdougm if (value != NULL && (strcasecmp(value, "true") == 0 || 9484345Sdougm strcmp(value, "1") == 0)) 9494345Sdougm export->ex_flags |= EX_NOSUB; 9504345Sdougm else 9514345Sdougm export->ex_flags &= ~EX_NOSUB; 9524345Sdougm break; 9534345Sdougm case OPT_PUBLIC: 9544345Sdougm if (value != NULL && (strcasecmp(value, "true") == 0 || 9554345Sdougm strcmp(value, "1") == 0)) 9564345Sdougm export->ex_flags |= EX_PUBLIC; 9574345Sdougm else 9584345Sdougm export->ex_flags &= ~EX_PUBLIC; 9594345Sdougm break; 9604345Sdougm case OPT_INDEX: 9614345Sdougm if (value != NULL && (strcmp(value, "..") == 0 || 9624345Sdougm strchr(value, '/') != NULL)) { 9634345Sdougm /* this is an error */ 9644345Sdougm (void) printf(dgettext(TEXT_DOMAIN, 9654345Sdougm "NFS: index=\"%s\" not valid;" 9664345Sdougm "must be a filename.\n"), 9674345Sdougm value); 9684345Sdougm break; 9694345Sdougm } 9704345Sdougm if (value != NULL && *value != '\0' && 9714345Sdougm strcmp(value, ".") != 0) { 9724345Sdougm /* valid index file string */ 9734345Sdougm if (export->ex_index != NULL) { 9744345Sdougm /* left over from "default" */ 9754345Sdougm free(export->ex_index); 9764345Sdougm } 9774345Sdougm /* remember to free */ 9784345Sdougm export->ex_index = strdup(value); 9794345Sdougm if (export->ex_index == NULL) { 9804345Sdougm (void) printf(dgettext(TEXT_DOMAIN, 9814345Sdougm "NFS: out of memory setting " 9824345Sdougm "index property\n")); 9834345Sdougm break; 9844345Sdougm } 9854345Sdougm export->ex_flags |= EX_INDEX; 9864345Sdougm } 9874345Sdougm break; 9884345Sdougm case OPT_LOG: 9894345Sdougm if (value == NULL) 9904345Sdougm value = strdup("global"); 9914345Sdougm if (value != NULL) 9924345Sdougm configlog(export, 9934345Sdougm strlen(value) ? value : "global"); 9944345Sdougm break; 9957961SNatalie.Li@Sun.COM case OPT_CHARSET_MAP: 9967961SNatalie.Li@Sun.COM /* 9977961SNatalie.Li@Sun.COM * Set EX_CHARMAP when there is at least one 9987961SNatalie.Li@Sun.COM * charmap conversion property. This will get 9997961SNatalie.Li@Sun.COM * checked by the nfs server when it needs to. 10007961SNatalie.Li@Sun.COM */ 10017961SNatalie.Li@Sun.COM export->ex_flags |= EX_CHARMAP; 10027961SNatalie.Li@Sun.COM break; 10034345Sdougm default: 10044345Sdougm /* have a syntactic error */ 10054345Sdougm (void) printf(dgettext(TEXT_DOMAIN, 10064345Sdougm "NFS: unrecognized option %s=%s\n"), 10074345Sdougm name, value != NULL ? value : ""); 10084345Sdougm break; 10093910Sdougm } 10104345Sdougm if (name != NULL) 10114345Sdougm sa_free_attr_string(name); 10123910Sdougm if (value != NULL) 10134345Sdougm sa_free_attr_string(value); 10143910Sdougm } 10153910Sdougm return (ret); 10163910Sdougm } 10173910Sdougm 10183910Sdougm /* 10193910Sdougm * cleanup_export(export) 10203910Sdougm * 10213910Sdougm * Cleanup the allocated areas so we don't leak memory 10223910Sdougm */ 10233910Sdougm 10243910Sdougm static void 10253910Sdougm cleanup_export(struct exportdata *export) 10263910Sdougm { 10273910Sdougm int i; 10283910Sdougm 10293910Sdougm if (export->ex_index != NULL) 10304345Sdougm free(export->ex_index); 10313910Sdougm if (export->ex_secinfo != NULL) { 10324345Sdougm for (i = 0; i < export->ex_seccnt; i++) 10334345Sdougm if (export->ex_secinfo[i].s_rootnames != NULL) 10344345Sdougm free(export->ex_secinfo[i].s_rootnames); 10354345Sdougm free(export->ex_secinfo); 10363910Sdougm } 10373910Sdougm } 10383910Sdougm 10393910Sdougm /* 10403910Sdougm * Given a seconfig entry and a colon-separated 10413910Sdougm * list of names, allocate an array big enough 10423910Sdougm * to hold the root list, then convert each name to 10433910Sdougm * a principal name according to the security 10443910Sdougm * info and assign it to an array element. 10453910Sdougm * Return the array and its size. 10463910Sdougm */ 10473910Sdougm static caddr_t * 10483910Sdougm get_rootnames(seconfig_t *sec, char *list, int *count) 10493910Sdougm { 10503910Sdougm caddr_t *a; 10513910Sdougm int c, i; 10523910Sdougm char *host, *p; 10533910Sdougm 10543910Sdougm /* 10553910Sdougm * Count the number of strings in the list. 10563910Sdougm * This is the number of colon separators + 1. 10573910Sdougm */ 10583910Sdougm c = 1; 10593910Sdougm for (p = list; *p; p++) 10603910Sdougm if (*p == ':') 10613910Sdougm c++; 10623910Sdougm *count = c; 10633910Sdougm 10643910Sdougm a = (caddr_t *)malloc(c * sizeof (char *)); 10653910Sdougm if (a == NULL) { 10663910Sdougm (void) printf(dgettext(TEXT_DOMAIN, 10674345Sdougm "get_rootnames: no memory\n")); 10683910Sdougm } else { 10694345Sdougm for (i = 0; i < c; i++) { 10704345Sdougm host = strtok(list, ":"); 10714345Sdougm if (!nfs_get_root_principal(sec, host, &a[i])) { 10724345Sdougm free(a); 10734345Sdougm a = NULL; 10744345Sdougm break; 10754345Sdougm } 10764345Sdougm list = NULL; 10773910Sdougm } 10783910Sdougm } 10793910Sdougm 10803910Sdougm return (a); 10813910Sdougm } 10823910Sdougm 10833910Sdougm /* 10843910Sdougm * fill_security_from_secopts(sp, secopts) 10853910Sdougm * 10863910Sdougm * Fill the secinfo structure from the secopts optionset. 10873910Sdougm */ 10883910Sdougm 10893910Sdougm static int 10903910Sdougm fill_security_from_secopts(struct secinfo *sp, sa_security_t secopts) 10913910Sdougm { 10923910Sdougm sa_property_t prop; 10933910Sdougm char *type; 10943910Sdougm int longform; 10953910Sdougm int err = SC_NOERROR; 10967961SNatalie.Li@Sun.COM uint32_t val; 10973910Sdougm 10983910Sdougm type = sa_get_security_attr(secopts, "sectype"); 10993910Sdougm if (type != NULL) { 11004345Sdougm /* named security type needs secinfo to be filled in */ 11014345Sdougm err = nfs_getseconfig_byname(type, &sp->s_secinfo); 11024345Sdougm sa_free_attr_string(type); 11034345Sdougm if (err != SC_NOERROR) 11044345Sdougm return (err); 11053910Sdougm } else { 11064345Sdougm /* default case */ 11074345Sdougm err = nfs_getseconfig_default(&sp->s_secinfo); 11084345Sdougm if (err != SC_NOERROR) 11094345Sdougm return (err); 11103910Sdougm } 11113910Sdougm 11123910Sdougm err = SA_OK; 11133910Sdougm for (prop = sa_get_property(secopts, NULL); 11144345Sdougm prop != NULL && err == SA_OK; 11154345Sdougm prop = sa_get_next_property(prop)) { 11164345Sdougm char *name; 11174345Sdougm char *value; 11183910Sdougm 11194345Sdougm name = sa_get_property_attr(prop, "type"); 11204345Sdougm value = sa_get_property_attr(prop, "value"); 11213910Sdougm 11224345Sdougm longform = value != NULL && strcmp(value, "*") != 0; 11233910Sdougm 11244345Sdougm switch (findopt(name)) { 11254345Sdougm case OPT_RO: 11264345Sdougm sp->s_flags |= longform ? M_ROL : M_RO; 11274345Sdougm break; 11284345Sdougm case OPT_RW: 11294345Sdougm sp->s_flags |= longform ? M_RWL : M_RW; 11304345Sdougm break; 11314345Sdougm case OPT_ROOT: 11324345Sdougm sp->s_flags |= M_ROOT; 11334345Sdougm /* 11344345Sdougm * if we are using AUTH_UNIX, handle like other things 11354345Sdougm * such as RO/RW 11364345Sdougm */ 11374345Sdougm if (sp->s_secinfo.sc_rpcnum == AUTH_UNIX) 11384345Sdougm continue; 11394345Sdougm /* not AUTH_UNIX */ 11404345Sdougm if (value != NULL) { 11414345Sdougm sp->s_rootnames = get_rootnames(&sp->s_secinfo, 11424345Sdougm value, &sp->s_rootcnt); 11434345Sdougm if (sp->s_rootnames == NULL) { 11444345Sdougm err = SA_BAD_VALUE; 11454345Sdougm (void) fprintf(stderr, 11464345Sdougm dgettext(TEXT_DOMAIN, 11474345Sdougm "Bad root list\n")); 11484345Sdougm } 11494345Sdougm } 11504345Sdougm break; 11517961SNatalie.Li@Sun.COM case OPT_NONE: 11527961SNatalie.Li@Sun.COM sp->s_flags |= M_NONE; 11537961SNatalie.Li@Sun.COM break; 11544345Sdougm case OPT_WINDOW: 11554345Sdougm if (value != NULL) { 11564345Sdougm sp->s_window = atoi(value); 11574345Sdougm /* just in case */ 11584345Sdougm if (sp->s_window < 0) 11594345Sdougm sp->s_window = DEF_WIN; 11604345Sdougm } 11614345Sdougm break; 11627961SNatalie.Li@Sun.COM case OPT_ROOT_MAPPING: 11637961SNatalie.Li@Sun.COM if (value != NULL && is_a_number(value)) { 11647961SNatalie.Li@Sun.COM val = strtoul(value, NULL, 0); 11657961SNatalie.Li@Sun.COM } else { 11667961SNatalie.Li@Sun.COM struct passwd *pw; 11677961SNatalie.Li@Sun.COM pw = getpwnam(value != NULL ? value : "nobody"); 11687961SNatalie.Li@Sun.COM if (pw != NULL) { 11697961SNatalie.Li@Sun.COM val = pw->pw_uid; 11707961SNatalie.Li@Sun.COM } else { 11717961SNatalie.Li@Sun.COM val = UID_NOBODY; 11727961SNatalie.Li@Sun.COM } 11737961SNatalie.Li@Sun.COM endpwent(); 11747961SNatalie.Li@Sun.COM } 11757961SNatalie.Li@Sun.COM sp->s_rootid = val; 11767961SNatalie.Li@Sun.COM break; 11774345Sdougm default: 11784345Sdougm break; 11793910Sdougm } 11804345Sdougm if (name != NULL) 11814345Sdougm sa_free_attr_string(name); 11824345Sdougm if (value != NULL) 11834345Sdougm sa_free_attr_string(value); 11843910Sdougm } 11853910Sdougm /* if rw/ro options not set, use default of RW */ 11863910Sdougm if ((sp->s_flags & NFS_RWMODES) == 0) 11874345Sdougm sp->s_flags |= M_RW; 11883910Sdougm return (err); 11893910Sdougm } 11903910Sdougm 11913910Sdougm /* 11923910Sdougm * This is for testing only 11933910Sdougm * It displays the export structure that 11943910Sdougm * goes into the kernel. 11953910Sdougm */ 11963910Sdougm static void 11973910Sdougm printarg(char *path, struct exportdata *ep) 11983910Sdougm { 11993910Sdougm int i, j; 12003910Sdougm struct secinfo *sp; 12013910Sdougm 12023910Sdougm if (debug == 0) 12034345Sdougm return; 12043910Sdougm 12053910Sdougm (void) printf("%s:\n", path); 12063910Sdougm (void) printf("\tex_version = %d\n", ep->ex_version); 12073910Sdougm (void) printf("\tex_path = %s\n", ep->ex_path); 12083910Sdougm (void) printf("\tex_pathlen = %ld\n", (ulong_t)ep->ex_pathlen); 12093910Sdougm (void) printf("\tex_flags: (0x%02x) ", ep->ex_flags); 12103910Sdougm if (ep->ex_flags & EX_NOSUID) 12113910Sdougm (void) printf("NOSUID "); 12123910Sdougm if (ep->ex_flags & EX_ACLOK) 12133910Sdougm (void) printf("ACLOK "); 12143910Sdougm if (ep->ex_flags & EX_PUBLIC) 12153910Sdougm (void) printf("PUBLIC "); 12163910Sdougm if (ep->ex_flags & EX_NOSUB) 12173910Sdougm (void) printf("NOSUB "); 12183910Sdougm if (ep->ex_flags & EX_LOG) 12193910Sdougm (void) printf("LOG "); 12207961SNatalie.Li@Sun.COM if (ep->ex_flags & EX_CHARMAP) 12217961SNatalie.Li@Sun.COM (void) printf("CHARMAP "); 12223910Sdougm if (ep->ex_flags & EX_LOG_ALLOPS) 12233910Sdougm (void) printf("LOG_ALLOPS "); 12243910Sdougm if (ep->ex_flags == 0) 12253910Sdougm (void) printf("(none)"); 12263910Sdougm (void) printf("\n"); 12273910Sdougm if (ep->ex_flags & EX_LOG) { 12283910Sdougm (void) printf("\tex_log_buffer = %s\n", 12294345Sdougm (ep->ex_log_buffer ? ep->ex_log_buffer : "(NULL)")); 12303910Sdougm (void) printf("\tex_tag = %s\n", 12314345Sdougm (ep->ex_tag ? ep->ex_tag : "(NULL)")); 12323910Sdougm } 12333910Sdougm (void) printf("\tex_anon = %d\n", ep->ex_anon); 12343910Sdougm (void) printf("\tex_seccnt = %d\n", ep->ex_seccnt); 12353910Sdougm (void) printf("\n"); 12363910Sdougm for (i = 0; i < ep->ex_seccnt; i++) { 12373910Sdougm sp = &ep->ex_secinfo[i]; 12383910Sdougm (void) printf("\t\ts_secinfo = %s\n", sp->s_secinfo.sc_name); 12393910Sdougm (void) printf("\t\ts_flags: (0x%02x) ", sp->s_flags); 12403910Sdougm if (sp->s_flags & M_ROOT) (void) printf("M_ROOT "); 12413910Sdougm if (sp->s_flags & M_RO) (void) printf("M_RO "); 12423910Sdougm if (sp->s_flags & M_ROL) (void) printf("M_ROL "); 12433910Sdougm if (sp->s_flags & M_RW) (void) printf("M_RW "); 12443910Sdougm if (sp->s_flags & M_RWL) (void) printf("M_RWL "); 12457961SNatalie.Li@Sun.COM if (sp->s_flags & M_NONE) (void) printf("M_NONE "); 12463910Sdougm if (sp->s_flags == 0) (void) printf("(none)"); 12473910Sdougm (void) printf("\n"); 12483910Sdougm (void) printf("\t\ts_window = %d\n", sp->s_window); 12497961SNatalie.Li@Sun.COM (void) printf("\t\ts_rootid = %d\n", sp->s_rootid); 12503910Sdougm (void) printf("\t\ts_rootcnt = %d ", sp->s_rootcnt); 12513910Sdougm (void) fflush(stdout); 12523910Sdougm for (j = 0; j < sp->s_rootcnt; j++) 12533910Sdougm (void) printf("%s ", sp->s_rootnames[j] ? 12544345Sdougm sp->s_rootnames[j] : "<null>"); 12553910Sdougm (void) printf("\n\n"); 12563910Sdougm } 12573910Sdougm } 12583910Sdougm 12593910Sdougm /* 12603910Sdougm * count_security(opts) 12613910Sdougm * 12623910Sdougm * Count the number of security types (flavors). The optionset has 12633910Sdougm * been populated with the security flavors as a holding mechanism. 12643910Sdougm * We later use this number to allocate data structures. 12653910Sdougm */ 12663910Sdougm 12673910Sdougm static int 12683910Sdougm count_security(sa_optionset_t opts) 12693910Sdougm { 12703910Sdougm int count = 0; 12713910Sdougm sa_property_t prop; 12723910Sdougm if (opts != NULL) { 12734345Sdougm for (prop = sa_get_property(opts, NULL); prop != NULL; 12744345Sdougm prop = sa_get_next_property(prop)) { 12754345Sdougm count++; 12764345Sdougm } 12773910Sdougm } 12783910Sdougm return (count); 12793910Sdougm } 12803910Sdougm 12813910Sdougm /* 12823910Sdougm * nfs_sprint_option(rbuff, rbuffsize, incr, prop, sep) 12833910Sdougm * 12843910Sdougm * provides a mechanism to format NFS properties into legacy output 12853910Sdougm * format. If the buffer would overflow, it is reallocated and grown 12863910Sdougm * as appropriate. Special cases of converting internal form of values 12873910Sdougm * to those used by "share" are done. this function does one property 12883910Sdougm * at a time. 12893910Sdougm */ 12903910Sdougm 12915179Sdougm static int 12923910Sdougm nfs_sprint_option(char **rbuff, size_t *rbuffsize, size_t incr, 12933910Sdougm sa_property_t prop, int sep) 12943910Sdougm { 12953910Sdougm char *name; 12963910Sdougm char *value; 12973910Sdougm int curlen; 12983910Sdougm char *buff = *rbuff; 12993910Sdougm size_t buffsize = *rbuffsize; 13005179Sdougm int printed = B_FALSE; 13013910Sdougm 13023910Sdougm name = sa_get_property_attr(prop, "type"); 13033910Sdougm value = sa_get_property_attr(prop, "value"); 13043910Sdougm if (buff != NULL) 13054345Sdougm curlen = strlen(buff); 13063910Sdougm else 13074345Sdougm curlen = 0; 13083910Sdougm if (name != NULL) { 13094345Sdougm int len; 13104345Sdougm len = strlen(name) + sep; 13113910Sdougm 13123910Sdougm /* 13133910Sdougm * A future RFE would be to replace this with more 13143910Sdougm * generic code and to possibly handle more types. 13153910Sdougm */ 13164345Sdougm switch (gettype(name)) { 13174345Sdougm case OPT_TYPE_BOOLEAN: 13185179Sdougm /* 13195179Sdougm * For NFS, boolean value of FALSE means it 13205179Sdougm * doesn't show up in the option list at all. 13215179Sdougm */ 13224345Sdougm if (value != NULL && strcasecmp(value, "false") == 0) 13235179Sdougm goto skip; 13245179Sdougm if (value != NULL) { 13254345Sdougm sa_free_attr_string(value); 13265179Sdougm value = NULL; 13275179Sdougm } 13284345Sdougm break; 13294345Sdougm case OPT_TYPE_ACCLIST: 13304345Sdougm if (value != NULL && strcmp(value, "*") == 0) { 13314345Sdougm sa_free_attr_string(value); 13324345Sdougm value = NULL; 13334345Sdougm } else { 13344345Sdougm if (value != NULL) 13354345Sdougm len += 1 + strlen(value); 13364345Sdougm } 13374345Sdougm break; 13384345Sdougm case OPT_TYPE_LOGTAG: 13394345Sdougm if (value != NULL && strlen(value) == 0) { 13404345Sdougm sa_free_attr_string(value); 13414345Sdougm value = NULL; 13424345Sdougm } else { 13434345Sdougm if (value != NULL) 13444345Sdougm len += 1 + strlen(value); 13454345Sdougm } 13464345Sdougm break; 13474345Sdougm default: 13484345Sdougm if (value != NULL) 13494345Sdougm len += 1 + strlen(value); 13504345Sdougm break; 13513910Sdougm } 13524345Sdougm while (buffsize <= (curlen + len)) { 13534345Sdougm /* need more room */ 13544345Sdougm buffsize += incr; 13554345Sdougm buff = realloc(buff, buffsize); 13564345Sdougm if (buff == NULL) { 13574345Sdougm /* realloc failed so free everything */ 13584345Sdougm if (*rbuff != NULL) 13594345Sdougm free(*rbuff); 13604345Sdougm } 13614345Sdougm *rbuff = buff; 13624345Sdougm *rbuffsize = buffsize; 13635179Sdougm if (buff == NULL) 13645179Sdougm goto skip; 13655179Sdougm 13663910Sdougm } 13675179Sdougm 13684345Sdougm if (buff == NULL) 13695179Sdougm goto skip; 13705179Sdougm 13714345Sdougm if (value == NULL) { 13724345Sdougm (void) snprintf(buff + curlen, buffsize - curlen, 13734345Sdougm "%s%s", sep ? "," : "", 13744345Sdougm name, value != NULL ? value : ""); 13754345Sdougm } else { 13764345Sdougm (void) snprintf(buff + curlen, buffsize - curlen, 13774345Sdougm "%s%s=%s", sep ? "," : "", 13784345Sdougm name, value != NULL ? value : ""); 13793910Sdougm } 13805179Sdougm printed = B_TRUE; 13813910Sdougm } 13825179Sdougm skip: 13833910Sdougm if (name != NULL) 13844345Sdougm sa_free_attr_string(name); 13853910Sdougm if (value != NULL) 13864345Sdougm sa_free_attr_string(value); 13875179Sdougm return (printed); 13883910Sdougm } 13893910Sdougm 13903910Sdougm /* 13913910Sdougm * nfs_format_options(group, hier) 13923910Sdougm * 13933910Sdougm * format all the options on the group into an old-style option 13943910Sdougm * string. If hier is non-zero, walk up the tree to get inherited 13953910Sdougm * options. 13963910Sdougm */ 13973910Sdougm 13983910Sdougm static char * 13993910Sdougm nfs_format_options(sa_group_t group, int hier) 14003910Sdougm { 14013910Sdougm sa_optionset_t options = NULL; 14024345Sdougm sa_optionset_t secoptions = NULL; 14033910Sdougm sa_property_t prop, secprop; 14044345Sdougm sa_security_t security = NULL; 14053910Sdougm char *buff; 14063910Sdougm size_t buffsize; 14074345Sdougm char *sectype = NULL; 14084345Sdougm int sep = 0; 14094345Sdougm 14104345Sdougm 14114345Sdougm buff = malloc(OPT_CHUNK); 14124345Sdougm if (buff == NULL) { 14134345Sdougm return (NULL); 14144345Sdougm } 14154345Sdougm 14164345Sdougm buff[0] = '\0'; 14174345Sdougm buffsize = OPT_CHUNK; 14184345Sdougm 14194345Sdougm /* 14204345Sdougm * We may have a an optionset relative to this item. format 14214345Sdougm * these if we find them and then add any security definitions. 14224345Sdougm */ 14233910Sdougm 14243910Sdougm options = sa_get_derived_optionset(group, "nfs", hier); 14253910Sdougm 14263910Sdougm /* 14274345Sdougm * do the default set first but skip any option that is also 14284345Sdougm * in the protocol specific optionset. 14293910Sdougm */ 14304345Sdougm if (options != NULL) { 14314345Sdougm for (prop = sa_get_property(options, NULL); 14324345Sdougm prop != NULL; prop = sa_get_next_property(prop)) { 14333910Sdougm /* 14344345Sdougm * use this one since we skipped any 14354345Sdougm * of these that were also in 14364345Sdougm * optdefault 14373910Sdougm */ 14385179Sdougm if (nfs_sprint_option(&buff, &buffsize, OPT_CHUNK, 14395179Sdougm prop, sep)) 14405179Sdougm sep = 1; 14414345Sdougm if (buff == NULL) { 14424345Sdougm /* 14434345Sdougm * buff could become NULL if there 14444345Sdougm * isn't enough memory for 14454345Sdougm * nfs_sprint_option to realloc() 14464345Sdougm * as necessary. We can't really 14474345Sdougm * do anything about it at this 14484345Sdougm * point so we return NULL. The 14494345Sdougm * caller should handle the 14504345Sdougm * failure. 14514345Sdougm */ 14524345Sdougm if (options != NULL) 14534345Sdougm sa_free_derived_optionset( 14544345Sdougm options); 14554345Sdougm return (buff); 14564345Sdougm } 14573910Sdougm } 14584345Sdougm } 14594345Sdougm secoptions = (sa_optionset_t)sa_get_all_security_types(group, 14604345Sdougm "nfs", hier); 14614345Sdougm if (secoptions != NULL) { 14623910Sdougm for (secprop = sa_get_property(secoptions, NULL); 14634345Sdougm secprop != NULL; 14644345Sdougm secprop = sa_get_next_property(secprop)) { 14654345Sdougm sectype = sa_get_property_attr(secprop, "type"); 14664345Sdougm security = 14674345Sdougm (sa_security_t)sa_get_derived_security( 14684345Sdougm group, sectype, "nfs", hier); 14694345Sdougm if (security != NULL) { 14704345Sdougm if (sectype != NULL) { 14714345Sdougm prop = sa_create_property( 14724345Sdougm "sec", sectype); 14735179Sdougm if (prop == NULL) 14745179Sdougm goto err; 14755179Sdougm if (nfs_sprint_option(&buff, 14765179Sdougm &buffsize, OPT_CHUNK, prop, sep)) 14775179Sdougm sep = 1; 14784345Sdougm (void) sa_remove_property(prop); 14795179Sdougm if (buff == NULL) 14805179Sdougm goto err; 14814345Sdougm } 14824345Sdougm for (prop = sa_get_property(security, 14834345Sdougm NULL); prop != NULL; 14844345Sdougm prop = sa_get_next_property(prop)) { 14855179Sdougm if (nfs_sprint_option(&buff, 14865179Sdougm &buffsize, OPT_CHUNK, prop, sep)) 14875179Sdougm sep = 1; 14884345Sdougm if (buff == NULL) 14894345Sdougm goto err; 14904345Sdougm } 14914345Sdougm sa_free_derived_optionset(security); 14923910Sdougm } 14934345Sdougm if (sectype != NULL) 14944345Sdougm sa_free_attr_string(sectype); 14953910Sdougm } 14963910Sdougm sa_free_derived_optionset(secoptions); 14973910Sdougm } 14984345Sdougm 14993910Sdougm if (options != NULL) 15004345Sdougm sa_free_derived_optionset(options); 15014345Sdougm return (buff); 15024345Sdougm 15034345Sdougm err: 15044345Sdougm /* 15054345Sdougm * If we couldn't allocate memory for option printing, we need 15064345Sdougm * to break out of the nested loops, cleanup and return NULL. 15074345Sdougm */ 15084345Sdougm if (secoptions != NULL) 15094345Sdougm sa_free_derived_optionset(secoptions); 15104345Sdougm if (security != NULL) 15114345Sdougm sa_free_derived_optionset(security); 15124345Sdougm if (sectype != NULL) 15134345Sdougm sa_free_attr_string(sectype); 15144345Sdougm if (options != NULL) 15154345Sdougm sa_free_derived_optionset(options); 15168334SJose.Borrego@Sun.COM return (NULL); 15173910Sdougm } 15184345Sdougm 15193910Sdougm /* 15203910Sdougm * Append an entry to the nfslogtab file 15213910Sdougm */ 15223910Sdougm static int 15233910Sdougm nfslogtab_add(dir, buffer, tag) 15243910Sdougm char *dir, *buffer, *tag; 15253910Sdougm { 15263910Sdougm FILE *f; 15273910Sdougm struct logtab_ent lep; 15283910Sdougm int error = 0; 15293910Sdougm 15303910Sdougm /* 15313910Sdougm * Open the file for update and create it if necessary. 15323910Sdougm * This may leave the I/O offset at the end of the file, 15333910Sdougm * so rewind back to the beginning of the file. 15343910Sdougm */ 15353910Sdougm f = fopen(NFSLOGTAB, "a+"); 15363910Sdougm if (f == NULL) { 15373910Sdougm error = errno; 15383910Sdougm goto out; 15393910Sdougm } 15403910Sdougm rewind(f); 15413910Sdougm 15423910Sdougm if (lockf(fileno(f), F_LOCK, 0L) < 0) { 15433910Sdougm (void) fprintf(stderr, dgettext(TEXT_DOMAIN, 15444345Sdougm "share complete, however failed to lock %s " 15454345Sdougm "for update: %s\n"), NFSLOGTAB, strerror(errno)); 15463910Sdougm error = -1; 15473910Sdougm goto out; 15483910Sdougm } 15493910Sdougm 15503910Sdougm if (logtab_deactivate_after_boot(f) == -1) { 15513910Sdougm (void) fprintf(stderr, dgettext(TEXT_DOMAIN, 15524345Sdougm "share complete, however could not deactivate " 15534345Sdougm "entries in %s\n"), NFSLOGTAB); 15543910Sdougm error = -1; 15553910Sdougm goto out; 15563910Sdougm } 15573910Sdougm 15583910Sdougm /* 15593910Sdougm * Remove entries matching buffer and sharepoint since we're 15603910Sdougm * going to replace it with perhaps an entry with a new tag. 15613910Sdougm */ 15623910Sdougm if (logtab_rement(f, buffer, dir, NULL, -1)) { 15633910Sdougm (void) fprintf(stderr, dgettext(TEXT_DOMAIN, 15644345Sdougm "share complete, however could not remove matching " 15654345Sdougm "entries in %s\n"), NFSLOGTAB); 15663910Sdougm error = -1; 15673910Sdougm goto out; 15683910Sdougm } 15693910Sdougm 15703910Sdougm /* 15713910Sdougm * Deactivate all active entries matching this sharepoint 15723910Sdougm */ 15733910Sdougm if (logtab_deactivate(f, NULL, dir, NULL)) { 15743910Sdougm (void) fprintf(stderr, dgettext(TEXT_DOMAIN, 15754345Sdougm "share complete, however could not deactivate matching " 15764345Sdougm "entries in %s\n"), NFSLOGTAB); 15773910Sdougm error = -1; 15783910Sdougm goto out; 15793910Sdougm } 15803910Sdougm 15813910Sdougm lep.le_buffer = buffer; 15823910Sdougm lep.le_path = dir; 15833910Sdougm lep.le_tag = tag; 15843910Sdougm lep.le_state = LES_ACTIVE; 15853910Sdougm 15863910Sdougm /* 15873910Sdougm * Add new sharepoint / buffer location to nfslogtab 15883910Sdougm */ 15893910Sdougm if (logtab_putent(f, &lep) < 0) { 15903910Sdougm (void) fprintf(stderr, dgettext(TEXT_DOMAIN, 15914345Sdougm "share complete, however could not add %s to %s\n"), 15924345Sdougm dir, NFSLOGTAB); 15933910Sdougm error = -1; 15943910Sdougm } 15953910Sdougm 15963910Sdougm out: 15973910Sdougm if (f != NULL) 15983910Sdougm (void) fclose(f); 15993910Sdougm return (error); 16003910Sdougm } 16013910Sdougm 16023910Sdougm /* 16033910Sdougm * Deactivate an entry from the nfslogtab file 16043910Sdougm */ 16053910Sdougm static int 16063910Sdougm nfslogtab_deactivate(path) 16073910Sdougm char *path; 16083910Sdougm { 16093910Sdougm FILE *f; 16103910Sdougm int error = 0; 16113910Sdougm 16123910Sdougm f = fopen(NFSLOGTAB, "r+"); 16133910Sdougm if (f == NULL) { 16143910Sdougm error = errno; 16153910Sdougm goto out; 16163910Sdougm } 16173910Sdougm if (lockf(fileno(f), F_LOCK, 0L) < 0) { 16183910Sdougm error = errno; 16193910Sdougm (void) fprintf(stderr, dgettext(TEXT_DOMAIN, 16204345Sdougm "share complete, however could not lock %s for " 16214345Sdougm "update: %s\n"), NFSLOGTAB, strerror(error)); 16223910Sdougm goto out; 16233910Sdougm } 16243910Sdougm if (logtab_deactivate(f, NULL, path, NULL) == -1) { 16253910Sdougm error = -1; 16263910Sdougm (void) fprintf(stderr, 16274345Sdougm dgettext(TEXT_DOMAIN, 16284345Sdougm "share complete, however could not " 16294345Sdougm "deactivate %s in %s\n"), path, NFSLOGTAB); 16303910Sdougm goto out; 16313910Sdougm } 16323910Sdougm 16333910Sdougm out: if (f != NULL) 16343910Sdougm (void) fclose(f); 16353910Sdougm 16363910Sdougm return (error); 16373910Sdougm } 16383910Sdougm 16393910Sdougm /* 16404524Sdougm * check_public(group, skipshare) 16414524Sdougm * 16424524Sdougm * Check the group for any shares that have the public property 16434524Sdougm * enabled. We skip "skipshare" since that is the one we are 16444524Sdougm * working with. This is a separate function to make handling 16454524Sdougm * subgroups simpler. Returns true if there is a share with public. 16464524Sdougm */ 16474524Sdougm static int 16484524Sdougm check_public(sa_group_t group, sa_share_t skipshare) 16494524Sdougm { 16504524Sdougm int exists = B_FALSE; 16514524Sdougm sa_share_t share; 16524524Sdougm sa_optionset_t opt; 16534524Sdougm sa_property_t prop; 16544524Sdougm char *shared; 16554524Sdougm 16564524Sdougm for (share = sa_get_share(group, NULL); share != NULL; 16574524Sdougm share = sa_get_next_share(share)) { 16584524Sdougm if (share == skipshare) 16594524Sdougm continue; 16604524Sdougm 16614524Sdougm opt = sa_get_optionset(share, "nfs"); 16624524Sdougm if (opt == NULL) 16634524Sdougm continue; 16644524Sdougm prop = sa_get_property(opt, "public"); 16654524Sdougm if (prop == NULL) 16664524Sdougm continue; 16674524Sdougm shared = sa_get_share_attr(share, "shared"); 16684524Sdougm if (shared != NULL) { 16694524Sdougm exists = strcmp(shared, "true") == 0; 16704524Sdougm sa_free_attr_string(shared); 16714524Sdougm if (exists == B_TRUE) 16724524Sdougm break; 16734524Sdougm } 16744524Sdougm } 16754524Sdougm 16764524Sdougm return (exists); 16774524Sdougm } 16784524Sdougm 16794524Sdougm /* 16806214Sdougm * public_exists(handle, share) 16813910Sdougm * 16823910Sdougm * check to see if public option is set on any other share than the 16834524Sdougm * one specified. Need to check zfs sub-groups as well as the top 16844524Sdougm * level groups. 16853910Sdougm */ 16863910Sdougm static int 16876214Sdougm public_exists(sa_handle_t handle, sa_share_t skipshare) 16883910Sdougm { 16896214Sdougm sa_group_t group = NULL; 16903910Sdougm 16916271Sdougm /* 16926271Sdougm * If we don't have a handle, we can only do syntax check. We 16936271Sdougm * can't check against other shares so we assume OK and will 16946271Sdougm * catch the problem only when we actually try to apply it. 16956271Sdougm */ 16963910Sdougm if (handle == NULL) 16976271Sdougm return (SA_OK); 16983910Sdougm 16996214Sdougm if (skipshare != NULL) { 17006214Sdougm group = sa_get_parent_group(skipshare); 17016214Sdougm if (group == NULL) 17026214Sdougm return (SA_NO_SUCH_GROUP); 17036214Sdougm } 17046214Sdougm 17053910Sdougm for (group = sa_get_group(handle, NULL); group != NULL; 17063910Sdougm group = sa_get_next_group(group)) { 17074524Sdougm /* Walk any ZFS subgroups as well as all standard groups */ 17084524Sdougm if (sa_group_is_zfs(group)) { 17094524Sdougm sa_group_t subgroup; 17104524Sdougm for (subgroup = sa_get_sub_group(group); 17114524Sdougm subgroup != NULL; 17124524Sdougm subgroup = sa_get_next_group(subgroup)) { 17134524Sdougm if (check_public(subgroup, skipshare)) 17144524Sdougm return (B_TRUE); 17153910Sdougm } 17164524Sdougm } else { 17174524Sdougm if (check_public(group, skipshare)) 17184524Sdougm return (B_TRUE); 17193910Sdougm } 17203910Sdougm } 17214524Sdougm return (B_FALSE); 17223910Sdougm } 17233910Sdougm 17243910Sdougm /* 17253910Sdougm * sa_enable_share at the protocol level, enable_share must tell the 17263910Sdougm * implementation that it is to enable the share. This entails 17273910Sdougm * converting the path and options into the appropriate ioctl 17283910Sdougm * calls. It is assumed that all error checking of paths, etc. were 17293910Sdougm * done earlier. 17303910Sdougm */ 17313910Sdougm static int 17323910Sdougm nfs_enable_share(sa_share_t share) 17333910Sdougm { 17343910Sdougm struct exportdata export; 17353910Sdougm sa_optionset_t secoptlist; 17363910Sdougm struct secinfo *sp; 17373910Sdougm int num_secinfo; 17383910Sdougm sa_optionset_t opt; 17393910Sdougm sa_security_t sec; 17403910Sdougm sa_property_t prop; 17413910Sdougm char *path; 17423910Sdougm int err = SA_OK; 17434524Sdougm int i; 17444543Smarks int iszfs; 17456214Sdougm sa_handle_t handle; 17463910Sdougm 17473910Sdougm /* Don't drop core if the NFS module isn't loaded. */ 17483910Sdougm (void) signal(SIGSYS, SIG_IGN); 17493910Sdougm 17503910Sdougm /* get the path since it is important in several places */ 17513910Sdougm path = sa_get_share_attr(share, "path"); 17523910Sdougm if (path == NULL) 17534345Sdougm return (SA_NO_SUCH_PATH); 17543910Sdougm 17554543Smarks iszfs = sa_path_is_zfs(path); 17563910Sdougm /* 17573910Sdougm * find the optionsets and security sets. There may not be 17583910Sdougm * any or there could be one or two for each of optionset and 17593910Sdougm * security may have multiple, one per security type per 17603910Sdougm * protocol type. 17613910Sdougm */ 17623910Sdougm opt = sa_get_derived_optionset(share, "nfs", 1); 17633910Sdougm secoptlist = (sa_optionset_t)sa_get_all_security_types(share, "nfs", 1); 17643910Sdougm if (secoptlist != NULL) 17654345Sdougm num_secinfo = MAX(1, count_security(secoptlist)); 17663910Sdougm else 17674345Sdougm num_secinfo = 1; 17683910Sdougm 17693910Sdougm /* 17703910Sdougm * walk through the options and fill in the structure 17713910Sdougm * appropriately. 17723910Sdougm */ 17733910Sdougm 17743910Sdougm (void) memset(&export, '\0', sizeof (export)); 17753910Sdougm 17763910Sdougm /* 17773910Sdougm * do non-security options first since there is only one after 17783910Sdougm * the derived group is constructed. 17793910Sdougm */ 17803910Sdougm export.ex_version = EX_CURRENT_VERSION; 17813910Sdougm export.ex_anon = UID_NOBODY; /* this is our default value */ 17823910Sdougm export.ex_index = NULL; 17833910Sdougm export.ex_path = path; 17843910Sdougm export.ex_pathlen = strlen(path) + 1; 17853910Sdougm 17863910Sdougm if (opt != NULL) 17874345Sdougm err = fill_export_from_optionset(&export, opt); 17883910Sdougm 17893910Sdougm /* 17903910Sdougm * check to see if "public" is set. If it is, then make sure 17913910Sdougm * no other share has it set. If it is already used, fail. 17923910Sdougm */ 17933910Sdougm 17946214Sdougm handle = sa_find_group_handle((sa_group_t)share); 17956214Sdougm if (export.ex_flags & EX_PUBLIC && public_exists(handle, share)) { 17964345Sdougm (void) printf(dgettext(TEXT_DOMAIN, 17974345Sdougm "NFS: Cannot share more than one file " 17984345Sdougm "system with 'public' property\n")); 17994345Sdougm err = SA_NOT_ALLOWED; 18004345Sdougm goto out; 18013910Sdougm } 18023910Sdougm 18034524Sdougm sp = calloc(num_secinfo, sizeof (struct secinfo)); 18043910Sdougm if (sp == NULL) { 18054345Sdougm err = SA_NO_MEMORY; 18064524Sdougm (void) printf(dgettext(TEXT_DOMAIN, 18074524Sdougm "NFS: NFS: no memory for security\n")); 18084524Sdougm goto out; 18094524Sdougm } 18104524Sdougm export.ex_secinfo = sp; 18114524Sdougm /* get default secinfo */ 18124524Sdougm export.ex_seccnt = num_secinfo; 18134524Sdougm /* 18144524Sdougm * since we must have one security option defined, we 18154524Sdougm * init to the default and then override as we find 18164524Sdougm * defined security options. This handles the case 18174524Sdougm * where we have no defined options but we need to set 18184524Sdougm * up one. 18194524Sdougm */ 18204524Sdougm sp[0].s_window = DEF_WIN; 18214524Sdougm sp[0].s_rootnames = NULL; 18224524Sdougm /* setup a default in case no properties defined */ 18234524Sdougm if (nfs_getseconfig_default(&sp[0].s_secinfo)) { 18244524Sdougm (void) printf(dgettext(TEXT_DOMAIN, 18254524Sdougm "NFS: nfs_getseconfig_default: failed to " 18264524Sdougm "get default security mode\n")); 18274524Sdougm err = SA_CONFIG_ERR; 18284524Sdougm } 18294524Sdougm if (secoptlist != NULL) { 18304524Sdougm for (i = 0, prop = sa_get_property(secoptlist, NULL); 18314524Sdougm prop != NULL && i < num_secinfo; 18324524Sdougm prop = sa_get_next_property(prop), i++) { 18334524Sdougm char *sectype; 18344345Sdougm sectype = sa_get_property_attr(prop, "type"); 18354524Sdougm /* 18364524Sdougm * if sectype is NULL, we probably 18374524Sdougm * have a memory problem and can't get 18384524Sdougm * the correct values. Rather than 18394524Sdougm * exporting with incorrect security, 18404524Sdougm * don't share it. 18414524Sdougm */ 18424524Sdougm if (sectype == NULL) { 18434524Sdougm err = SA_NO_MEMORY; 18444524Sdougm (void) printf(dgettext(TEXT_DOMAIN, 18454524Sdougm "NFS: Cannot share %s: " 18464524Sdougm "no memory\n"), path); 18474524Sdougm goto out; 18484524Sdougm } 18494524Sdougm sec = (sa_security_t)sa_get_derived_security( 18504524Sdougm share, sectype, "nfs", 1); 18514524Sdougm sp[i].s_window = DEF_WIN; 18524524Sdougm sp[i].s_rootcnt = 0; 18534524Sdougm sp[i].s_rootnames = NULL; 18544345Sdougm (void) fill_security_from_secopts(&sp[i], sec); 18554524Sdougm if (sec != NULL) 18564524Sdougm sa_free_derived_security(sec); 18574524Sdougm if (sectype != NULL) 18584524Sdougm sa_free_attr_string(sectype); 18593910Sdougm } 18604524Sdougm } 18614524Sdougm /* 18624524Sdougm * when we get here, we can do the exportfs system call and 18634524Sdougm * initiate thinsg. We probably want to enable the nfs.server 18644524Sdougm * service first if it isn't running within SMF. 18654524Sdougm */ 18664524Sdougm /* check nfs.server status and start if needed */ 18674524Sdougm /* now add the share to the internal tables */ 18684524Sdougm printarg(path, &export); 18694524Sdougm /* 18704524Sdougm * call the exportfs system call which is implemented 18714524Sdougm * via the nfssys() call as the EXPORTFS subfunction. 18724524Sdougm */ 18734543Smarks if (iszfs) { 18744543Smarks struct exportfs_args ea; 18754543Smarks share_t sh; 18764543Smarks char *str; 18774543Smarks priv_set_t *priv_effective; 18784543Smarks int privileged; 18794543Smarks 18804543Smarks /* 18814543Smarks * If we aren't a privileged user 18824543Smarks * and NFS server service isn't running 18834543Smarks * then print out an error message 18844543Smarks * and return EPERM 18854543Smarks */ 18864543Smarks 18874543Smarks priv_effective = priv_allocset(); 18884543Smarks (void) getppriv(PRIV_EFFECTIVE, priv_effective); 18894543Smarks 18904543Smarks privileged = (priv_isfullset(priv_effective) == B_TRUE); 18914543Smarks priv_freeset(priv_effective); 18924543Smarks 18934543Smarks if (!privileged && 18944543Smarks (str = smf_get_state(NFS_SERVER_SVC)) != NULL) { 18954543Smarks err = 0; 18964543Smarks if (strcmp(str, SCF_STATE_STRING_ONLINE) != 0) { 18974543Smarks (void) printf(dgettext(TEXT_DOMAIN, 18984543Smarks "NFS: Cannot share remote " 18994543Smarks "filesystem: %s\n"), path); 19004543Smarks (void) printf(dgettext(TEXT_DOMAIN, 19014543Smarks "NFS: Service needs to be enabled " 19024543Smarks "by a privileged user\n")); 19034543Smarks err = SA_SYSTEM_ERR; 19044543Smarks errno = EPERM; 19054543Smarks } 19064543Smarks free(str); 19074543Smarks } 19084543Smarks 19094543Smarks if (err == 0) { 19104543Smarks ea.dname = path; 19114543Smarks ea.uex = &export; 19124543Smarks 19134543Smarks sa_sharetab_fill_zfs(share, &sh, "nfs"); 19148845Samw@Sun.COM err = sa_share_zfs(share, NULL, path, &sh, 19155331Samw &ea, ZFS_SHARE_NFS); 1916*10761SWilliam.Krier@Sun.COM if (err != SA_OK) { 1917*10761SWilliam.Krier@Sun.COM errno = err; 1918*10761SWilliam.Krier@Sun.COM err = -1; 1919*10761SWilliam.Krier@Sun.COM } 19204543Smarks sa_emptyshare(&sh); 19214543Smarks } 19224543Smarks } else { 19234543Smarks err = exportfs(path, &export); 19244543Smarks } 19254543Smarks 19264543Smarks if (err < 0) { 19274524Sdougm err = SA_SYSTEM_ERR; 19284524Sdougm switch (errno) { 19294524Sdougm case EREMOTE: 19304524Sdougm (void) printf(dgettext(TEXT_DOMAIN, 19314543Smarks "NFS: Cannot share filesystems " 19324543Smarks "in non-global zones: %s\n"), path); 19334543Smarks err = SA_NOT_SUPPORTED; 19344524Sdougm break; 19354524Sdougm case EPERM: 19364524Sdougm if (getzoneid() != GLOBAL_ZONEID) { 19374345Sdougm (void) printf(dgettext(TEXT_DOMAIN, 19384543Smarks "NFS: Cannot share file systems " 19394524Sdougm "in non-global zones: %s\n"), path); 19404524Sdougm err = SA_NOT_SUPPORTED; 19414345Sdougm break; 19424345Sdougm } 19434524Sdougm err = SA_NO_PERMISSION; 19444524Sdougm /* FALLTHROUGH */ 19454524Sdougm default: 19464524Sdougm break; 19473910Sdougm } 19484524Sdougm } else { 19494524Sdougm /* update sharetab with an add/modify */ 19504543Smarks if (!iszfs) { 19514543Smarks (void) sa_update_sharetab(share, "nfs"); 19524543Smarks } 19533910Sdougm } 19543910Sdougm 19553910Sdougm if (err == SA_OK) { 19563910Sdougm /* 19573910Sdougm * enable services as needed. This should probably be 19583910Sdougm * done elsewhere in order to minimize the calls to 19593910Sdougm * check services. 19603910Sdougm */ 19613910Sdougm /* 19623910Sdougm * check to see if logging and other services need to 19633910Sdougm * be triggered, but only if there wasn't an 19643910Sdougm * error. This is probably where sharetab should be 19653910Sdougm * updated with the NFS specific entry. 19663910Sdougm */ 19674345Sdougm if (export.ex_flags & EX_LOG) { 19684345Sdougm /* enable logging */ 19694345Sdougm if (nfslogtab_add(path, export.ex_log_buffer, 19704345Sdougm export.ex_tag) != 0) { 19714345Sdougm (void) fprintf(stderr, dgettext(TEXT_DOMAIN, 19724345Sdougm "Could not enable logging for %s\n"), 19734345Sdougm path); 19744345Sdougm } 19754345Sdougm _check_services(service_list_logging); 19764345Sdougm } else { 19774345Sdougm /* 19784345Sdougm * don't have logging so remove it from file. It might 19794345Sdougm * not be thre, but that doesn't matter. 19804345Sdougm */ 19814345Sdougm (void) nfslogtab_deactivate(path); 19824345Sdougm _check_services(service_list_default); 19833910Sdougm } 19843910Sdougm } 19853910Sdougm 19863910Sdougm out: 19873910Sdougm if (path != NULL) 19884345Sdougm free(path); 19893910Sdougm 19903910Sdougm cleanup_export(&export); 19913910Sdougm if (opt != NULL) 19924345Sdougm sa_free_derived_optionset(opt); 19933910Sdougm if (secoptlist != NULL) 19944345Sdougm (void) sa_destroy_optionset(secoptlist); 19953910Sdougm return (err); 19963910Sdougm } 19973910Sdougm 19983910Sdougm /* 19995800Sdougm * nfs_disable_share(share, path) 20003910Sdougm * 20015800Sdougm * Unshare the specified share. Note that "path" is the same path as 20025800Sdougm * what is in the "share" object. It is passed in to avoid an 20035800Sdougm * additional lookup. A missing "path" value makes this a no-op 20045800Sdougm * function. 20053910Sdougm */ 20063910Sdougm static int 20074543Smarks nfs_disable_share(sa_share_t share, char *path) 20083910Sdougm { 20093910Sdougm int err; 20103910Sdougm int ret = SA_OK; 20114543Smarks int iszfs; 20125800Sdougm sa_group_t parent; 20135951Sdougm sa_handle_t handle; 20144543Smarks 20155800Sdougm if (path == NULL) 20165800Sdougm return (ret); 20174543Smarks 20185800Sdougm /* 20195800Sdougm * If the share is in a ZFS group we need to handle it 20205800Sdougm * differently. Just being on a ZFS file system isn't 20215800Sdougm * enough since we may be in a legacy share case. 20225800Sdougm */ 20235800Sdougm parent = sa_get_parent_group(share); 20245800Sdougm iszfs = sa_group_is_zfs(parent); 20255800Sdougm if (iszfs) { 20265800Sdougm struct exportfs_args ea; 20275800Sdougm share_t sh = { 0 }; 20285800Sdougm ea.dname = path; 20295800Sdougm ea.uex = NULL; 20305800Sdougm sh.sh_path = path; 20315800Sdougm sh.sh_fstype = "nfs"; 20324543Smarks 20338845Samw@Sun.COM err = sa_share_zfs(share, NULL, path, &sh, 20345800Sdougm &ea, ZFS_UNSHARE_NFS); 2035*10761SWilliam.Krier@Sun.COM if (err != SA_OK) { 2036*10761SWilliam.Krier@Sun.COM errno = err; 2037*10761SWilliam.Krier@Sun.COM err = -1; 2038*10761SWilliam.Krier@Sun.COM } 20395800Sdougm } else { 20405800Sdougm err = exportfs(path, NULL); 20415800Sdougm } 20425800Sdougm if (err < 0) { 20435800Sdougm /* 20445800Sdougm * TBD: only an error in some 20455800Sdougm * cases - need better analysis 20465800Sdougm */ 20475800Sdougm switch (errno) { 20485800Sdougm case EPERM: 20495800Sdougm case EACCES: 20505800Sdougm ret = SA_NO_PERMISSION; 20515800Sdougm if (getzoneid() != GLOBAL_ZONEID) { 20525800Sdougm ret = SA_NOT_SUPPORTED; 20535800Sdougm } 20545800Sdougm break; 20555800Sdougm case EINVAL: 20565800Sdougm case ENOENT: 20575800Sdougm ret = SA_NO_SUCH_PATH; 20584543Smarks break; 2059*10761SWilliam.Krier@Sun.COM default: 2060*10761SWilliam.Krier@Sun.COM ret = SA_SYSTEM_ERR; 20614543Smarks break; 20623910Sdougm } 20635800Sdougm } 20645800Sdougm if (ret == SA_OK || ret == SA_NO_SUCH_PATH) { 20655951Sdougm handle = sa_find_group_handle((sa_group_t)share); 20665800Sdougm if (!iszfs) 20675951Sdougm (void) sa_delete_sharetab(handle, path, "nfs"); 20685800Sdougm /* just in case it was logged */ 20695800Sdougm (void) nfslogtab_deactivate(path); 20703910Sdougm } 20713910Sdougm return (ret); 20723910Sdougm } 20733910Sdougm 20743910Sdougm /* 20757961SNatalie.Li@Sun.COM * check_rorwnone(v1, v2, v3) 20767961SNatalie.Li@Sun.COM * 20777961SNatalie.Li@Sun.COM * check ro vs rw vs none values. Over time this may get beefed up. 20787961SNatalie.Li@Sun.COM * for now it just does simple checks. v1 is never NULL but v2 or v3 20797961SNatalie.Li@Sun.COM * could be. 20803910Sdougm */ 20813910Sdougm 20823910Sdougm static int 20837961SNatalie.Li@Sun.COM check_rorwnone(char *v1, char *v2, char *v3) 20843910Sdougm { 20853910Sdougm int ret = SA_OK; 20867961SNatalie.Li@Sun.COM if (v2 != NULL && strcmp(v1, v2) == 0) 20874345Sdougm ret = SA_VALUE_CONFLICT; 20887961SNatalie.Li@Sun.COM else if (v3 != NULL && strcmp(v1, v3) == 0) 20897961SNatalie.Li@Sun.COM ret = SA_VALUE_CONFLICT; 20907961SNatalie.Li@Sun.COM 20913910Sdougm return (ret); 20923910Sdougm } 20933910Sdougm 20943910Sdougm /* 20956214Sdougm * nfs_validate_property(handle, property, parent) 20963910Sdougm * 20973910Sdougm * Check that the property has a legitimate value for its type. 20983910Sdougm */ 20993910Sdougm 21003910Sdougm static int 21016214Sdougm nfs_validate_property(sa_handle_t handle, sa_property_t property, 21026214Sdougm sa_optionset_t parent) 21033910Sdougm { 21043910Sdougm int ret = SA_OK; 21053910Sdougm char *propname; 21067961SNatalie.Li@Sun.COM char *other1; 21077961SNatalie.Li@Sun.COM char *other2; 21083910Sdougm int optindex; 21093910Sdougm nfsl_config_t *configlist; 21103910Sdougm sa_group_t parent_group; 21113910Sdougm char *value; 21123910Sdougm 21133910Sdougm propname = sa_get_property_attr(property, "type"); 21143910Sdougm 21153910Sdougm if ((optindex = findopt(propname)) < 0) 21164345Sdougm ret = SA_NO_SUCH_PROP; 21173910Sdougm 21183910Sdougm /* need to validate value range here as well */ 21193910Sdougm 21203910Sdougm if (ret == SA_OK) { 21214345Sdougm parent_group = sa_get_parent_group((sa_share_t)parent); 21226214Sdougm if (optdefs[optindex].share && parent_group != NULL && 21236214Sdougm !sa_is_share(parent_group)) 21244345Sdougm ret = SA_PROP_SHARE_ONLY; 21253910Sdougm } 21263910Sdougm if (ret == SA_OK) { 21276214Sdougm if (optdefs[optindex].index == OPT_PUBLIC) { 21286214Sdougm /* 21296214Sdougm * Public is special in that only one instance can 21306214Sdougm * be in the repository at the same time. 21316214Sdougm */ 21326214Sdougm if (public_exists(handle, parent_group)) { 21337961SNatalie.Li@Sun.COM sa_free_attr_string(propname); 21346214Sdougm return (SA_VALUE_CONFLICT); 21356214Sdougm } 21366214Sdougm } 21374345Sdougm value = sa_get_property_attr(property, "value"); 21384345Sdougm if (value != NULL) { 21394345Sdougm /* first basic type checking */ 21404345Sdougm switch (optdefs[optindex].type) { 21414345Sdougm case OPT_TYPE_NUMBER: 21424345Sdougm /* check that the value is all digits */ 21434345Sdougm if (!is_a_number(value)) 21444345Sdougm ret = SA_BAD_VALUE; 21454345Sdougm break; 21464345Sdougm case OPT_TYPE_BOOLEAN: 21474345Sdougm if (strlen(value) == 0 || 21484345Sdougm strcasecmp(value, "true") == 0 || 21494345Sdougm strcmp(value, "1") == 0 || 21504345Sdougm strcasecmp(value, "false") == 0 || 21514345Sdougm strcmp(value, "0") == 0) { 21524345Sdougm ret = SA_OK; 21534345Sdougm } else { 21544345Sdougm ret = SA_BAD_VALUE; 21554345Sdougm } 21564345Sdougm break; 21574345Sdougm case OPT_TYPE_USER: 21584345Sdougm if (!is_a_number(value)) { 21594345Sdougm struct passwd *pw; 21604345Sdougm /* 21614345Sdougm * in this case it would have to be a 21624345Sdougm * user name 21634345Sdougm */ 21644345Sdougm pw = getpwnam(value); 21654345Sdougm if (pw == NULL) 21664345Sdougm ret = SA_BAD_VALUE; 21674345Sdougm endpwent(); 21684345Sdougm } else { 21694345Sdougm uint64_t intval; 21704345Sdougm intval = strtoull(value, NULL, 0); 21714345Sdougm if (intval > UID_MAX && intval != ~0) 21724345Sdougm ret = SA_BAD_VALUE; 21734345Sdougm } 21744345Sdougm break; 21754345Sdougm case OPT_TYPE_FILE: 21764345Sdougm if (strcmp(value, "..") == 0 || 21774345Sdougm strchr(value, '/') != NULL) { 21784345Sdougm ret = SA_BAD_VALUE; 21794345Sdougm } 21804345Sdougm break; 21817961SNatalie.Li@Sun.COM case OPT_TYPE_ACCLIST: { 21827961SNatalie.Li@Sun.COM sa_property_t oprop1; 21837961SNatalie.Li@Sun.COM sa_property_t oprop2; 21847961SNatalie.Li@Sun.COM char *ovalue1 = NULL; 21857961SNatalie.Li@Sun.COM char *ovalue2 = NULL; 21867961SNatalie.Li@Sun.COM 21877961SNatalie.Li@Sun.COM if (parent == NULL) 21887961SNatalie.Li@Sun.COM break; 21894345Sdougm /* 21904345Sdougm * access list handling. Should eventually 21914345Sdougm * validate that all the values make sense. 21924345Sdougm * Also, ro and rw may have cross value 21934345Sdougm * conflicts. 21944345Sdougm */ 21957961SNatalie.Li@Sun.COM if (strcmp(propname, SHOPT_RO) == 0) { 21967961SNatalie.Li@Sun.COM other1 = SHOPT_RW; 21977961SNatalie.Li@Sun.COM other2 = SHOPT_NONE; 21987961SNatalie.Li@Sun.COM } else if (strcmp(propname, SHOPT_RW) == 0) { 21997961SNatalie.Li@Sun.COM other1 = SHOPT_RO; 22007961SNatalie.Li@Sun.COM other2 = SHOPT_NONE; 22017961SNatalie.Li@Sun.COM } else if (strcmp(propname, SHOPT_NONE) == 0) { 22027961SNatalie.Li@Sun.COM other1 = SHOPT_RO; 22037961SNatalie.Li@Sun.COM other2 = SHOPT_RW; 22047961SNatalie.Li@Sun.COM } else { 22057961SNatalie.Li@Sun.COM other1 = NULL; 22067961SNatalie.Li@Sun.COM other2 = NULL; 22077961SNatalie.Li@Sun.COM } 22087961SNatalie.Li@Sun.COM if (other1 == NULL && other2 == NULL) 22097961SNatalie.Li@Sun.COM break; 22107961SNatalie.Li@Sun.COM 22117961SNatalie.Li@Sun.COM /* compare rw(ro) with ro(rw) */ 22124345Sdougm 22137961SNatalie.Li@Sun.COM oprop1 = sa_get_property(parent, other1); 22147961SNatalie.Li@Sun.COM oprop2 = sa_get_property(parent, other2); 22157961SNatalie.Li@Sun.COM if (oprop1 == NULL && oprop2 == NULL) 22167961SNatalie.Li@Sun.COM break; 22177961SNatalie.Li@Sun.COM /* 22187961SNatalie.Li@Sun.COM * Only potential confusion if other1 22197961SNatalie.Li@Sun.COM * or other2 exists. Check the values 22207961SNatalie.Li@Sun.COM * and run the check if there is a 22217961SNatalie.Li@Sun.COM * value other than the one we are 22227961SNatalie.Li@Sun.COM * explicitly looking at. 22237961SNatalie.Li@Sun.COM */ 22247961SNatalie.Li@Sun.COM ovalue1 = sa_get_property_attr(oprop1, "value"); 22257961SNatalie.Li@Sun.COM ovalue2 = sa_get_property_attr(oprop2, "value"); 22267961SNatalie.Li@Sun.COM if (ovalue1 != NULL || ovalue2 != NULL) 22277961SNatalie.Li@Sun.COM ret = check_rorwnone(value, ovalue1, 22287961SNatalie.Li@Sun.COM ovalue2); 22297961SNatalie.Li@Sun.COM 22307961SNatalie.Li@Sun.COM if (ovalue1 != NULL) 22317961SNatalie.Li@Sun.COM sa_free_attr_string(ovalue1); 22327961SNatalie.Li@Sun.COM if (ovalue2 != NULL) 22337961SNatalie.Li@Sun.COM sa_free_attr_string(ovalue2); 22344345Sdougm break; 22357961SNatalie.Li@Sun.COM } 22364345Sdougm case OPT_TYPE_LOGTAG: 22374345Sdougm if (nfsl_getconfig_list(&configlist) == 0) { 22384345Sdougm int error; 22394345Sdougm if (value == NULL || 22404345Sdougm strlen(value) == 0) { 22414345Sdougm if (value != NULL) 22424345Sdougm sa_free_attr_string( 22434345Sdougm value); 22444345Sdougm value = strdup("global"); 22454345Sdougm } 22464345Sdougm if (value != NULL && 22474345Sdougm nfsl_findconfig(configlist, value, 22484345Sdougm &error) == NULL) { 22494345Sdougm ret = SA_BAD_VALUE; 22504345Sdougm } 22515179Sdougm /* Must always free when done */ 22525179Sdougm nfsl_freeconfig_list(&configlist); 22534345Sdougm } else { 22544345Sdougm ret = SA_CONFIG_ERR; 22554345Sdougm } 22564345Sdougm break; 22574345Sdougm case OPT_TYPE_STRING: 22584345Sdougm /* whatever is here should be ok */ 22594345Sdougm break; 22604345Sdougm case OPT_TYPE_SECURITY: 22614345Sdougm /* 22624345Sdougm * The "sec" property isn't used in the 22634345Sdougm * non-legacy parts of sharemgr. We need to 22644345Sdougm * reject it here. For legacy, it is pulled 22654345Sdougm * out well before we get here. 22664345Sdougm */ 22674345Sdougm ret = SA_NO_SUCH_PROP; 22684345Sdougm break; 22694345Sdougm default: 22704345Sdougm break; 22713910Sdougm } 22725179Sdougm 22735179Sdougm if (value != NULL) 22745179Sdougm sa_free_attr_string(value); 22755179Sdougm 22764345Sdougm if (ret == SA_OK && optdefs[optindex].check != NULL) { 22774345Sdougm /* do the property specific check */ 22786214Sdougm ret = optdefs[optindex].check(handle, property); 22793910Sdougm } 22803910Sdougm } 22813910Sdougm } 22823910Sdougm 22833910Sdougm if (propname != NULL) 22844345Sdougm sa_free_attr_string(propname); 22853910Sdougm return (ret); 22863910Sdougm } 22873910Sdougm 22883910Sdougm /* 22893910Sdougm * Protocol management functions 22903910Sdougm * 22913910Sdougm * Properties defined in the default files are defined in 22923910Sdougm * proto_option_defs for parsing and validation. If "other" and 22933910Sdougm * "compare" are set, then the value for this property should be 22943910Sdougm * compared against the property specified in "other" using the 22953910Sdougm * "compare" check (either <= or >=) in order to ensure that the 22963910Sdougm * values are in the correct range. E.g. setting server_versmin 22973910Sdougm * higher than server_versmax should not be allowed. 22983910Sdougm */ 22993910Sdougm 23003910Sdougm struct proto_option_defs { 23013910Sdougm char *tag; 23023910Sdougm char *name; /* display name -- remove protocol identifier */ 23033910Sdougm int index; 23043910Sdougm int type; 23053910Sdougm union { 23063910Sdougm int intval; 23073910Sdougm char *string; 23083910Sdougm } defvalue; 23093910Sdougm uint32_t svcs; 23103910Sdougm int32_t minval; 23113910Sdougm int32_t maxval; 23123910Sdougm char *file; 23133910Sdougm char *other; 23143910Sdougm int compare; 23153910Sdougm #define OPT_CMP_GE 0 23163910Sdougm #define OPT_CMP_LE 1 23173910Sdougm int (*check)(char *); 23183910Sdougm } proto_options[] = { 23193910Sdougm #define PROTO_OPT_NFSD_SERVERS 0 23203910Sdougm {"nfsd_servers", 23213910Sdougm "servers", PROTO_OPT_NFSD_SERVERS, OPT_TYPE_NUMBER, 16, SVC_NFSD, 23223910Sdougm 1, INT32_MAX, NFSADMIN}, 23233910Sdougm #define PROTO_OPT_LOCKD_LISTEN_BACKLOG 1 23243910Sdougm {"lockd_listen_backlog", 23253910Sdougm "lockd_listen_backlog", PROTO_OPT_LOCKD_LISTEN_BACKLOG, 23263910Sdougm OPT_TYPE_NUMBER, 32, SVC_LOCKD, 32, INT32_MAX, NFSADMIN}, 23273910Sdougm #define PROTO_OPT_LOCKD_SERVERS 2 23283910Sdougm {"lockd_servers", 23293910Sdougm "lockd_servers", PROTO_OPT_LOCKD_SERVERS, OPT_TYPE_NUMBER, 20, 23303910Sdougm SVC_LOCKD, 1, INT32_MAX, NFSADMIN}, 23313910Sdougm #define PROTO_OPT_LOCKD_RETRANSMIT_TIMEOUT 3 23323910Sdougm {"lockd_retransmit_timeout", 23333910Sdougm "lockd_retransmit_timeout", PROTO_OPT_LOCKD_RETRANSMIT_TIMEOUT, 23343910Sdougm OPT_TYPE_NUMBER, 5, SVC_LOCKD, 0, INT32_MAX, NFSADMIN}, 23353910Sdougm #define PROTO_OPT_GRACE_PERIOD 4 23363910Sdougm {"grace_period", 23373910Sdougm "grace_period", PROTO_OPT_GRACE_PERIOD, OPT_TYPE_NUMBER, 90, 23383910Sdougm SVC_LOCKD, 0, INT32_MAX, NFSADMIN}, 23393910Sdougm #define PROTO_OPT_NFS_SERVER_VERSMIN 5 23403910Sdougm {"nfs_server_versmin", 23413910Sdougm "server_versmin", PROTO_OPT_NFS_SERVER_VERSMIN, OPT_TYPE_NUMBER, 23423910Sdougm (int)NFS_VERSMIN_DEFAULT, SVC_NFSD|SVC_MOUNTD, NFS_VERSMIN, 23433910Sdougm NFS_VERSMAX, NFSADMIN, "server_versmax", OPT_CMP_LE}, 23443910Sdougm #define PROTO_OPT_NFS_SERVER_VERSMAX 6 23453910Sdougm {"nfs_server_versmax", 23463910Sdougm "server_versmax", PROTO_OPT_NFS_SERVER_VERSMAX, OPT_TYPE_NUMBER, 23473910Sdougm (int)NFS_VERSMAX_DEFAULT, SVC_NFSD|SVC_MOUNTD, NFS_VERSMIN, 23483910Sdougm NFS_VERSMAX, NFSADMIN, "server_versmin", OPT_CMP_GE}, 23493910Sdougm #define PROTO_OPT_NFS_CLIENT_VERSMIN 7 23503910Sdougm {"nfs_client_versmin", 23513910Sdougm "client_versmin", PROTO_OPT_NFS_CLIENT_VERSMIN, OPT_TYPE_NUMBER, 23523910Sdougm (int)NFS_VERSMIN_DEFAULT, NULL, NFS_VERSMIN, NFS_VERSMAX, 23533910Sdougm NFSADMIN, "client_versmax", OPT_CMP_LE}, 23543910Sdougm #define PROTO_OPT_NFS_CLIENT_VERSMAX 8 23553910Sdougm {"nfs_client_versmax", 23563910Sdougm "client_versmax", PROTO_OPT_NFS_CLIENT_VERSMAX, OPT_TYPE_NUMBER, 23573910Sdougm (int)NFS_VERSMAX_DEFAULT, NULL, NFS_VERSMIN, NFS_VERSMAX, 23583910Sdougm NFSADMIN, "client_versmin", OPT_CMP_GE}, 23593910Sdougm #define PROTO_OPT_NFS_SERVER_DELEGATION 9 23603910Sdougm {"nfs_server_delegation", 23613910Sdougm "server_delegation", PROTO_OPT_NFS_SERVER_DELEGATION, 23623910Sdougm OPT_TYPE_ONOFF, NFS_SERVER_DELEGATION_DEFAULT, SVC_NFSD, 0, 0, 23633910Sdougm NFSADMIN}, 23643910Sdougm #define PROTO_OPT_NFSMAPID_DOMAIN 10 23653910Sdougm {"nfsmapid_domain", 23663910Sdougm "nfsmapid_domain", PROTO_OPT_NFSMAPID_DOMAIN, OPT_TYPE_DOMAIN, 23673910Sdougm NULL, SVC_NFSMAPID, 0, 0, NFSADMIN}, 23683910Sdougm #define PROTO_OPT_NFSD_MAX_CONNECTIONS 11 23693910Sdougm {"nfsd_max_connections", 23703910Sdougm "max_connections", PROTO_OPT_NFSD_MAX_CONNECTIONS, 23713910Sdougm OPT_TYPE_NUMBER, -1, SVC_NFSD, -1, INT32_MAX, NFSADMIN}, 23723910Sdougm #define PROTO_OPT_NFSD_PROTOCOL 12 23733910Sdougm {"nfsd_protocol", 23743910Sdougm "protocol", PROTO_OPT_NFSD_PROTOCOL, OPT_TYPE_PROTOCOL, 0, 23753910Sdougm SVC_NFSD, 0, 0, NFSADMIN}, 23763910Sdougm #define PROTO_OPT_NFSD_LISTEN_BACKLOG 13 23773910Sdougm {"nfsd_listen_backlog", 23783910Sdougm "listen_backlog", PROTO_OPT_NFSD_LISTEN_BACKLOG, 23793910Sdougm OPT_TYPE_NUMBER, 0, 23803910Sdougm SVC_LOCKD, 0, INT32_MAX, NFSADMIN}, 23813910Sdougm {NULL} 23823910Sdougm }; 23833910Sdougm 23843910Sdougm /* 23853910Sdougm * the protoset holds the defined options so we don't have to read 23863910Sdougm * them multiple times 23873910Sdougm */ 23885179Sdougm static sa_protocol_properties_t protoset; 23893910Sdougm 23903910Sdougm static int 23913910Sdougm findprotoopt(char *name, int whichname) 23923910Sdougm { 23933910Sdougm int i; 23943910Sdougm for (i = 0; proto_options[i].tag != NULL; i++) { 23954345Sdougm if (whichname == 1) { 23964345Sdougm if (strcasecmp(proto_options[i].name, name) == 0) 23973910Sdougm return (i); 23984345Sdougm } else { 23994345Sdougm if (strcasecmp(proto_options[i].tag, name) == 0) 24004345Sdougm return (i); 24014345Sdougm } 24023910Sdougm } 24033910Sdougm return (-1); 24043910Sdougm } 24053910Sdougm 24063910Sdougm /* 24073910Sdougm * fixcaselower(str) 24083910Sdougm * 24093910Sdougm * convert a string to lower case (inplace). 24103910Sdougm */ 24113910Sdougm 24123910Sdougm static void 24133910Sdougm fixcaselower(char *str) 24143910Sdougm { 24153910Sdougm while (*str) { 24164345Sdougm *str = tolower(*str); 24174345Sdougm str++; 24183910Sdougm } 24193910Sdougm } 24203910Sdougm 24213910Sdougm /* 24223910Sdougm * fixcaseupper(str) 24233910Sdougm * 24243910Sdougm * convert a string to upper case (inplace). 24253910Sdougm */ 24263910Sdougm 24273910Sdougm static void 24283910Sdougm fixcaseupper(char *str) 24293910Sdougm { 24303910Sdougm while (*str) { 24314345Sdougm *str = toupper(*str); 24324345Sdougm str++; 24333910Sdougm } 24343910Sdougm } 24353910Sdougm 24363910Sdougm /* 24374241Sdougm * skipwhitespace(str) 24384241Sdougm * 24394241Sdougm * Skip leading white space. It is assumed that it is called with a 24404241Sdougm * valid pointer. 24414241Sdougm */ 24424241Sdougm 24434241Sdougm static char * 24444241Sdougm skipwhitespace(char *str) 24454241Sdougm { 24464241Sdougm while (*str && isspace(*str)) 24474241Sdougm str++; 24484241Sdougm 24494241Sdougm return (str); 24504241Sdougm } 24514241Sdougm 24524241Sdougm /* 24534345Sdougm * extractprop() 24544345Sdougm * 24554345Sdougm * Extract the property and value out of the line and create the 24564345Sdougm * property in the optionset. 24574345Sdougm */ 24586019Sdougm static int 24594345Sdougm extractprop(char *name, char *value) 24604345Sdougm { 24614345Sdougm sa_property_t prop; 24624345Sdougm int index; 24636019Sdougm int ret = SA_OK; 24644345Sdougm /* 24654345Sdougm * Remove any leading 24664345Sdougm * white space. 24674345Sdougm */ 24684345Sdougm name = skipwhitespace(name); 24694345Sdougm 24704345Sdougm index = findprotoopt(name, 0); 24714345Sdougm if (index >= 0) { 24724345Sdougm fixcaselower(name); 24734345Sdougm prop = sa_create_property(proto_options[index].name, value); 24744345Sdougm if (prop != NULL) 24756019Sdougm ret = sa_add_protocol_property(protoset, prop); 24766019Sdougm else 24776019Sdougm ret = SA_NO_MEMORY; 24784345Sdougm } 24796019Sdougm return (ret); 24804345Sdougm } 24814345Sdougm 24824345Sdougm /* 24833910Sdougm * initprotofromdefault() 24843910Sdougm * 24856162Sdougm * Read the default file(s) and add the defined values to the 24863910Sdougm * protoset. Note that default values are known from the built in 24876162Sdougm * table in case the file doesn't have a definition. Not having the 24886162Sdougm * /etc/default/nfs file is OK since we have builtin default 24896162Sdougm * values. The default file will get constructed as needed if values 24906162Sdougm * are changed from the defaults. 24913910Sdougm */ 24923910Sdougm 24933910Sdougm static int 24943910Sdougm initprotofromdefault() 24953910Sdougm { 24963910Sdougm FILE *nfs; 24973910Sdougm char buff[BUFSIZ]; 24983910Sdougm char *name; 24993910Sdougm char *value; 25006019Sdougm int ret = SA_OK; 25013910Sdougm 25023910Sdougm protoset = sa_create_protocol_properties("nfs"); 25033910Sdougm 25043910Sdougm if (protoset != NULL) { 25054345Sdougm nfs = fopen(NFSADMIN, "r"); 25064345Sdougm if (nfs != NULL) { 25076019Sdougm while (ret == SA_OK && 25086019Sdougm fgets(buff, sizeof (buff), nfs) != NULL) { 25094345Sdougm switch (buff[0]) { 25104345Sdougm case '\n': 25114345Sdougm case '#': 25124345Sdougm /* skip */ 25134345Sdougm break; 25144345Sdougm default: 25154345Sdougm name = buff; 25164345Sdougm buff[strlen(buff) - 1] = '\0'; 25174345Sdougm value = strchr(name, '='); 25184345Sdougm if (value != NULL) { 25194345Sdougm *value++ = '\0'; 25206019Sdougm ret = extractprop(name, value); 25214345Sdougm } 25224345Sdougm } 25233910Sdougm } 25246019Sdougm (void) fclose(nfs); 25256019Sdougm } else { 25266162Sdougm switch (errno) { 25276162Sdougm case EPERM: 25286162Sdougm case EACCES: 25296162Sdougm ret = SA_NO_PERMISSION; 25306162Sdougm break; 25316162Sdougm case ENOENT: 25326162Sdougm break; 25336162Sdougm default: 25346162Sdougm ret = SA_SYSTEM_ERR; 25356162Sdougm break; 25366162Sdougm } 25373910Sdougm } 25386019Sdougm } else { 25396019Sdougm ret = SA_NO_MEMORY; 25403910Sdougm } 25416019Sdougm return (ret); 25423910Sdougm } 25433910Sdougm 25443910Sdougm /* 25454345Sdougm * add_defaults() 25463910Sdougm * 25473910Sdougm * Add the default values for any property not defined in the parsing 25483910Sdougm * of the default files. Values are set according to their defined 25493910Sdougm * types. 25503910Sdougm */ 25513910Sdougm 25523910Sdougm static void 25533910Sdougm add_defaults() 25543910Sdougm { 25553910Sdougm int i; 25563910Sdougm char number[MAXDIGITS]; 25573910Sdougm 25583910Sdougm for (i = 0; proto_options[i].tag != NULL; i++) { 25594345Sdougm sa_property_t prop; 25604345Sdougm prop = sa_get_protocol_property(protoset, 25614345Sdougm proto_options[i].name); 25624345Sdougm if (prop == NULL) { 25634345Sdougm /* add the default value */ 25644345Sdougm switch (proto_options[i].type) { 25654345Sdougm case OPT_TYPE_NUMBER: 25664345Sdougm (void) snprintf(number, sizeof (number), "%d", 25674345Sdougm proto_options[i].defvalue.intval); 25684345Sdougm prop = sa_create_property(proto_options[i].name, 25694345Sdougm number); 25704345Sdougm break; 25713910Sdougm 25724345Sdougm case OPT_TYPE_BOOLEAN: 25734345Sdougm prop = sa_create_property(proto_options[i].name, 25744345Sdougm proto_options[i].defvalue.intval ? 25754345Sdougm "true" : "false"); 25764345Sdougm break; 25773910Sdougm 25784345Sdougm case OPT_TYPE_ONOFF: 25794345Sdougm prop = sa_create_property(proto_options[i].name, 25804345Sdougm proto_options[i].defvalue.intval ? 25814345Sdougm "on" : "off"); 25824345Sdougm break; 25833910Sdougm 25844345Sdougm default: 25854345Sdougm /* treat as strings of zero length */ 25864345Sdougm prop = sa_create_property(proto_options[i].name, 25874345Sdougm ""); 25884345Sdougm break; 25894345Sdougm } 25904345Sdougm if (prop != NULL) 25914345Sdougm (void) sa_add_protocol_property(protoset, prop); 25923910Sdougm } 25933910Sdougm } 25943910Sdougm } 25953910Sdougm 25963910Sdougm static void 25973910Sdougm free_protoprops() 25983910Sdougm { 25995179Sdougm if (protoset != NULL) { 26005179Sdougm xmlFreeNode(protoset); 26015179Sdougm protoset = NULL; 26025179Sdougm } 26033910Sdougm } 26043910Sdougm 26053910Sdougm /* 26063910Sdougm * nfs_init() 26073910Sdougm * 26083910Sdougm * Initialize the NFS plugin. 26093910Sdougm */ 26103910Sdougm 26113910Sdougm static int 26123910Sdougm nfs_init() 26133910Sdougm { 26143910Sdougm int ret = SA_OK; 26153910Sdougm 26166162Sdougm if (sa_plugin_ops.sa_init != nfs_init) { 26174345Sdougm (void) printf(dgettext(TEXT_DOMAIN, 26184345Sdougm "NFS plugin not properly initialized\n")); 26196162Sdougm return (SA_CONFIG_ERR); 26206162Sdougm } 26213910Sdougm 26223910Sdougm ret = initprotofromdefault(); 26236162Sdougm if (ret != SA_OK) { 26246162Sdougm (void) printf(dgettext(TEXT_DOMAIN, 26256162Sdougm "NFS plugin problem with default file: %s\n"), 26266162Sdougm sa_errorstr(ret)); 26276162Sdougm ret = SA_OK; 26286162Sdougm } 26296162Sdougm add_defaults(); 26303910Sdougm 26313910Sdougm return (ret); 26323910Sdougm } 26333910Sdougm 26343910Sdougm /* 26353910Sdougm * nfs_fini() 26363910Sdougm * 26373910Sdougm * uninitialize the NFS plugin. Want to avoid memory leaks. 26383910Sdougm */ 26393910Sdougm 26403910Sdougm static void 26413910Sdougm nfs_fini() 26423910Sdougm { 26433910Sdougm free_protoprops(); 26443910Sdougm } 26453910Sdougm 26463910Sdougm /* 26473910Sdougm * nfs_get_proto_set() 26483910Sdougm * 26493910Sdougm * Return an optionset with all the protocol specific properties in 26503910Sdougm * it. 26513910Sdougm */ 26523910Sdougm 26533910Sdougm static sa_protocol_properties_t 26543910Sdougm nfs_get_proto_set() 26553910Sdougm { 26563910Sdougm return (protoset); 26573910Sdougm } 26583910Sdougm 26593910Sdougm struct deffile { 26603910Sdougm struct deffile *next; 26613910Sdougm char *line; 26623910Sdougm }; 26633910Sdougm 26643910Sdougm /* 26653910Sdougm * read_default_file(fname) 26663910Sdougm * 26673910Sdougm * Read the specified default file. We return a list of entries. This 26683910Sdougm * get used for adding or removing values. 26693910Sdougm */ 26703910Sdougm 26713910Sdougm static struct deffile * 26723910Sdougm read_default_file(char *fname) 26733910Sdougm { 26743910Sdougm FILE *file; 26753910Sdougm struct deffile *defs = NULL; 26763910Sdougm struct deffile *newdef; 26773910Sdougm struct deffile *prevdef = NULL; 26783910Sdougm char buff[BUFSIZ * 2]; 26793910Sdougm 26803910Sdougm file = fopen(fname, "r"); 26813910Sdougm if (file != NULL) { 26824345Sdougm while (fgets(buff, sizeof (buff), file) != NULL) { 26834345Sdougm newdef = (struct deffile *)calloc(1, 26844345Sdougm sizeof (struct deffile)); 26854345Sdougm if (newdef != NULL) { 26864345Sdougm /* Make sure we skip any leading whitespace. */ 26874345Sdougm newdef->line = strdup(skipwhitespace(buff)); 26884345Sdougm if (defs == NULL) { 26894345Sdougm prevdef = defs = newdef; 26904345Sdougm } else { 26914345Sdougm prevdef->next = newdef; 26924345Sdougm prevdef = newdef; 26934345Sdougm } 26944345Sdougm } 26953910Sdougm } 26966162Sdougm (void) fclose(file); 26976162Sdougm } else { 26986162Sdougm int ret = SA_OK; 26996162Sdougm switch (errno) { 27006162Sdougm case EPERM: 27016162Sdougm case EACCES: 27026162Sdougm ret = SA_NO_PERMISSION; 27036162Sdougm break; 27046162Sdougm case ENOENT: 27056162Sdougm break; 27066162Sdougm default: 27076162Sdougm ret = SA_SYSTEM_ERR; 27086162Sdougm break; 27096162Sdougm } 27106162Sdougm if (ret == SA_OK) { 27116162Sdougm /* Want at least one comment line */ 27126162Sdougm defs = (struct deffile *) 27136162Sdougm calloc(1, sizeof (struct deffile)); 27146162Sdougm defs->line = strdup("# NFS default file\n"); 27156162Sdougm } 27163910Sdougm } 27173910Sdougm return (defs); 27183910Sdougm } 27193910Sdougm 27203910Sdougm static void 27213910Sdougm free_default_file(struct deffile *defs) 27223910Sdougm { 27233910Sdougm struct deffile *curdefs = NULL; 27243910Sdougm 27253910Sdougm while (defs != NULL) { 27264345Sdougm curdefs = defs; 27274345Sdougm defs = defs->next; 27284345Sdougm if (curdefs->line != NULL) 27294345Sdougm free(curdefs->line); 27304345Sdougm free(curdefs); 27313910Sdougm } 27323910Sdougm } 27333910Sdougm 27343910Sdougm /* 27353910Sdougm * write_default_file(fname, defs) 27363910Sdougm * 27373910Sdougm * Write the default file back. 27383910Sdougm */ 27393910Sdougm 27403910Sdougm static int 27413910Sdougm write_default_file(char *fname, struct deffile *defs) 27423910Sdougm { 27433910Sdougm FILE *file; 27443910Sdougm int ret = SA_OK; 27453910Sdougm sigset_t old, new; 27463910Sdougm 27473910Sdougm file = fopen(fname, "w+"); 27483910Sdougm if (file != NULL) { 27494345Sdougm (void) sigprocmask(SIG_BLOCK, NULL, &new); 27504345Sdougm (void) sigaddset(&new, SIGHUP); 27514345Sdougm (void) sigaddset(&new, SIGINT); 27524345Sdougm (void) sigaddset(&new, SIGQUIT); 27534345Sdougm (void) sigaddset(&new, SIGTSTP); 27544345Sdougm (void) sigprocmask(SIG_SETMASK, &new, &old); 27554345Sdougm while (defs != NULL) { 27564345Sdougm (void) fputs(defs->line, file); 27574345Sdougm defs = defs->next; 27584345Sdougm } 27594345Sdougm (void) fsync(fileno(file)); 27604345Sdougm (void) sigprocmask(SIG_SETMASK, &old, NULL); 27614345Sdougm (void) fclose(file); 27623910Sdougm } else { 27634345Sdougm switch (errno) { 27644345Sdougm case EPERM: 27654345Sdougm case EACCES: 27664345Sdougm ret = SA_NO_PERMISSION; 27674345Sdougm break; 27684345Sdougm default: 27694345Sdougm ret = SA_SYSTEM_ERR; 27704345Sdougm } 27713910Sdougm } 27723910Sdougm return (ret); 27733910Sdougm } 27743910Sdougm 27753910Sdougm 27763910Sdougm /* 27773910Sdougm * set_default_file_value(tag, value) 27783910Sdougm * 27793910Sdougm * Set the default file value for tag to value. Then rewrite the file. 27803910Sdougm * tag and value are always set. The caller must ensure this. 27813910Sdougm */ 27823910Sdougm 27833910Sdougm #define MAX_STRING_LENGTH 256 27843910Sdougm static int 27853910Sdougm set_default_file_value(char *tag, char *value) 27863910Sdougm { 27873910Sdougm int ret = SA_OK; 27883910Sdougm struct deffile *root; 27893910Sdougm struct deffile *defs; 27903910Sdougm struct deffile *prev; 27913910Sdougm char string[MAX_STRING_LENGTH]; 27923910Sdougm int len; 27936162Sdougm boolean_t update = B_FALSE; 27943910Sdougm 27953910Sdougm (void) snprintf(string, MAX_STRING_LENGTH, "%s=", tag); 27963910Sdougm len = strlen(string); 27973910Sdougm 27983910Sdougm root = defs = read_default_file(NFSADMIN); 27993910Sdougm if (root == NULL) { 28006162Sdougm switch (errno) { 28016162Sdougm case EPERM: 28026162Sdougm case EACCES: 28034345Sdougm ret = SA_NO_PERMISSION; 28046162Sdougm break; 28056162Sdougm default: 28066162Sdougm ret = SA_NO_MEMORY; 28076162Sdougm break; 28086162Sdougm } 28096162Sdougm return (ret); 28106162Sdougm } 28116162Sdougm 28126162Sdougm while (defs != NULL) { 28136162Sdougm if (defs->line != NULL && 28146162Sdougm strncasecmp(defs->line, string, len) == 0) { 28156162Sdougm /* replace with the new value */ 28166162Sdougm free(defs->line); 28176162Sdougm fixcaseupper(tag); 28186162Sdougm (void) snprintf(string, sizeof (string), 28196162Sdougm "%s=%s\n", tag, value); 28206162Sdougm string[MAX_STRING_LENGTH - 1] = '\0'; 28216162Sdougm defs->line = strdup(string); 28226162Sdougm update = B_TRUE; 28236162Sdougm break; 28246162Sdougm } 28256162Sdougm defs = defs->next; 28266162Sdougm } 28276162Sdougm if (!update) { 28286162Sdougm defs = root; 28296162Sdougm /* didn't find, so see if it is a comment */ 28306162Sdougm (void) snprintf(string, MAX_STRING_LENGTH, "#%s=", tag); 28316162Sdougm len = strlen(string); 28323910Sdougm while (defs != NULL) { 28336162Sdougm if (strncasecmp(defs->line, string, len) == 0) { 28344345Sdougm /* replace with the new value */ 28354345Sdougm free(defs->line); 28364345Sdougm fixcaseupper(tag); 28374345Sdougm (void) snprintf(string, sizeof (string), 28383910Sdougm "%s=%s\n", tag, value); 28394345Sdougm string[MAX_STRING_LENGTH - 1] = '\0'; 28404345Sdougm defs->line = strdup(string); 28416162Sdougm update = B_TRUE; 28424345Sdougm break; 28434345Sdougm } 28444345Sdougm defs = defs->next; 28453910Sdougm } 28466162Sdougm } 28476162Sdougm if (!update) { 28486162Sdougm fixcaseupper(tag); 28496162Sdougm (void) snprintf(string, sizeof (string), "%s=%s\n", 28506162Sdougm tag, value); 28516162Sdougm prev = root; 28526162Sdougm while (prev->next != NULL) 28536162Sdougm prev = prev->next; 28546162Sdougm defs = malloc(sizeof (struct deffile)); 28556162Sdougm prev->next = defs; 28566162Sdougm if (defs != NULL) { 28576162Sdougm defs->next = NULL; 28586162Sdougm defs->line = strdup(string); 28596162Sdougm update = B_TRUE; 28603910Sdougm } 28613910Sdougm } 28626162Sdougm if (update) { 28636162Sdougm ret = write_default_file(NFSADMIN, root); 28646162Sdougm } 28656162Sdougm free_default_file(root); 28663910Sdougm return (ret); 28673910Sdougm } 28683910Sdougm 28693910Sdougm /* 28703910Sdougm * service_in_state(service, chkstate) 28713910Sdougm * 28723910Sdougm * Want to know if the specified service is in the desired state 28733910Sdougm * (chkstate) or not. Return true (1) if it is and false (0) if it 28743910Sdougm * isn't. 28753910Sdougm */ 28763910Sdougm static int 28773910Sdougm service_in_state(char *service, const char *chkstate) 28783910Sdougm { 28793910Sdougm char *state; 28803910Sdougm int ret = B_FALSE; 28813910Sdougm 28823910Sdougm state = smf_get_state(service); 28833910Sdougm if (state != NULL) { 28844345Sdougm /* got the state so get the equality for the return value */ 28854345Sdougm ret = strcmp(state, chkstate) == 0 ? B_TRUE : B_FALSE; 28864345Sdougm free(state); 28873910Sdougm } 28883910Sdougm return (ret); 28893910Sdougm } 28903910Sdougm 28913910Sdougm /* 28923910Sdougm * restart_service(svcs) 28933910Sdougm * 28943910Sdougm * Walk through the bit mask of services that need to be restarted in 28953910Sdougm * order to use the new property values. Some properties affect 28963910Sdougm * multiple daemons. Should only restart a service if it is currently 28973910Sdougm * enabled (online). 28983910Sdougm */ 28993910Sdougm 29003910Sdougm static void 29013910Sdougm restart_service(uint32_t svcs) 29023910Sdougm { 29033910Sdougm uint32_t mask; 29043910Sdougm int ret; 29053910Sdougm char *service; 29063910Sdougm 29073910Sdougm for (mask = 1; svcs != 0; mask <<= 1) { 29084345Sdougm switch (svcs & mask) { 29094345Sdougm case SVC_LOCKD: 29104345Sdougm service = LOCKD; 29114345Sdougm break; 29124345Sdougm case SVC_STATD: 29134345Sdougm service = STATD; 29144345Sdougm break; 29154345Sdougm case SVC_NFSD: 29164345Sdougm service = NFSD; 29174345Sdougm break; 29184345Sdougm case SVC_MOUNTD: 29194345Sdougm service = MOUNTD; 29204345Sdougm break; 29214345Sdougm case SVC_NFS4CBD: 29224345Sdougm service = NFS4CBD; 29234345Sdougm break; 29244345Sdougm case SVC_NFSMAPID: 29254345Sdougm service = NFSMAPID; 29264345Sdougm break; 29274345Sdougm case SVC_RQUOTAD: 29284345Sdougm service = RQUOTAD; 29294345Sdougm break; 29304345Sdougm case SVC_NFSLOGD: 29314345Sdougm service = NFSLOGD; 29324345Sdougm break; 29334345Sdougm default: 29344345Sdougm continue; 29354345Sdougm } 29363910Sdougm 29373910Sdougm /* 29383910Sdougm * Only attempt to restart the service if it is 29393910Sdougm * currently running. In the future, it may be 29403910Sdougm * desirable to use smf_refresh_instance if the NFS 29413910Sdougm * services ever implement the refresh method. 29423910Sdougm */ 29434345Sdougm if (service_in_state(service, SCF_STATE_STRING_ONLINE)) { 29444345Sdougm ret = smf_restart_instance(service); 29453910Sdougm /* 29464345Sdougm * There are only a few SMF errors at this point, but 29474345Sdougm * it is also possible that a bad value may have put 29484345Sdougm * the service into maintenance if there wasn't an 29494345Sdougm * SMF level error. 29503910Sdougm */ 29514345Sdougm if (ret != 0) { 29524345Sdougm (void) fprintf(stderr, 29534345Sdougm dgettext(TEXT_DOMAIN, 29544345Sdougm "%s failed to restart: %s\n"), 29554345Sdougm scf_strerror(scf_error())); 29564345Sdougm } else { 29574345Sdougm /* 29584345Sdougm * Check whether it has gone to "maintenance" 29594345Sdougm * mode or not. Maintenance implies something 29604345Sdougm * went wrong. 29614345Sdougm */ 29624345Sdougm if (service_in_state(service, 29634345Sdougm SCF_STATE_STRING_MAINT)) { 29644345Sdougm (void) fprintf(stderr, 29654345Sdougm dgettext(TEXT_DOMAIN, 29664345Sdougm "%s failed to restart\n"), 29674345Sdougm service); 29684345Sdougm } 29694345Sdougm } 29703910Sdougm } 29714345Sdougm svcs &= ~mask; 29723910Sdougm } 29733910Sdougm } 29743910Sdougm 29753910Sdougm /* 29763910Sdougm * nfs_minmax_check(name, value) 29773910Sdougm * 29783910Sdougm * Verify that the value for the property specified by index is valid 29793910Sdougm * relative to the opposite value in the case of a min/max variable. 29803910Sdougm * Currently, server_minvers/server_maxvers and 29813910Sdougm * client_minvers/client_maxvers are the only ones to check. 29823910Sdougm */ 29833910Sdougm 29843910Sdougm static int 29853910Sdougm nfs_minmax_check(int index, int value) 29863910Sdougm { 29873910Sdougm int val; 29883910Sdougm char *pval; 29893910Sdougm sa_property_t prop; 29903910Sdougm sa_optionset_t opts; 29913910Sdougm int ret = B_TRUE; 29923910Sdougm 29933910Sdougm if (proto_options[index].other != NULL) { 29944345Sdougm /* have a property to compare against */ 29954345Sdougm opts = nfs_get_proto_set(); 29964345Sdougm prop = sa_get_property(opts, proto_options[index].other); 29973910Sdougm /* 29983910Sdougm * If we don't find the property, assume default 29993910Sdougm * values which will work since the max will be at the 30003910Sdougm * max and the min at the min. 30013910Sdougm */ 30024345Sdougm if (prop != NULL) { 30034345Sdougm pval = sa_get_property_attr(prop, "value"); 30044345Sdougm if (pval != NULL) { 30054345Sdougm val = strtoul(pval, NULL, 0); 30064345Sdougm if (proto_options[index].compare == 30074345Sdougm OPT_CMP_LE) { 30084345Sdougm ret = value <= val ? B_TRUE : B_FALSE; 30094345Sdougm } else if (proto_options[index].compare == 30104345Sdougm OPT_CMP_GE) { 30114345Sdougm ret = value >= val ? B_TRUE : B_FALSE; 30124345Sdougm } 30134345Sdougm } 30143910Sdougm } 30153910Sdougm } 30163910Sdougm return (ret); 30173910Sdougm } 30183910Sdougm 30193910Sdougm /* 30203910Sdougm * nfs_validate_proto_prop(index, name, value) 30213910Sdougm * 30225331Samw * Verify that the property specified by name can take the new 30233910Sdougm * value. This is a sanity check to prevent bad values getting into 30243910Sdougm * the default files. All values need to be checked against what is 30253910Sdougm * allowed by their defined type. If a type isn't explicitly defined 30263910Sdougm * here, it is treated as a string. 30273910Sdougm * 30283910Sdougm * Note that OPT_TYPE_NUMBER will additionally check that the value is 30293910Sdougm * within the range specified and potentially against another property 30303910Sdougm * value as well as specified in the proto_options members other and 30313910Sdougm * compare. 30323910Sdougm */ 30333910Sdougm 30343910Sdougm static int 30353910Sdougm nfs_validate_proto_prop(int index, char *name, char *value) 30363910Sdougm { 30373910Sdougm int ret = SA_OK; 30383910Sdougm char *cp; 30393910Sdougm #ifdef lint 30403910Sdougm name = name; 30413910Sdougm #endif 30423910Sdougm 30433910Sdougm switch (proto_options[index].type) { 30443910Sdougm case OPT_TYPE_NUMBER: 30454345Sdougm if (!is_a_number(value)) 30464345Sdougm ret = SA_BAD_VALUE; 30474345Sdougm else { 30484345Sdougm int val; 30494345Sdougm val = strtoul(value, NULL, 0); 30504345Sdougm if (val < proto_options[index].minval || 30514345Sdougm val > proto_options[index].maxval) 30524345Sdougm ret = SA_BAD_VALUE; 30534345Sdougm /* 30544345Sdougm * For server_versmin/server_versmax and 30554345Sdougm * client_versmin/client_versmax, the value of the 30564345Sdougm * min(max) should be checked to be correct relative 30574345Sdougm * to the current max(min). 30584345Sdougm */ 30594345Sdougm if (!nfs_minmax_check(index, val)) { 30604345Sdougm ret = SA_BAD_VALUE; 30614345Sdougm } 30623910Sdougm } 30634345Sdougm break; 30643910Sdougm 30653910Sdougm case OPT_TYPE_DOMAIN: 30663910Sdougm /* 30673910Sdougm * needs to be a qualified domain so will have at 30683910Sdougm * least one period and other characters on either 30693910Sdougm * side of it. A zero length string is also allowed 30703910Sdougm * and is the way to turn off the override. 30713910Sdougm */ 30724345Sdougm if (strlen(value) == 0) 30734345Sdougm break; 30744345Sdougm cp = strchr(value, '.'); 30754345Sdougm if (cp == NULL || cp == value || strchr(value, '@') != NULL) 30764345Sdougm ret = SA_BAD_VALUE; 30773910Sdougm break; 30783910Sdougm 30793910Sdougm case OPT_TYPE_BOOLEAN: 30804345Sdougm if (strlen(value) == 0 || 30814345Sdougm strcasecmp(value, "true") == 0 || 30824345Sdougm strcmp(value, "1") == 0 || 30834345Sdougm strcasecmp(value, "false") == 0 || 30844345Sdougm strcmp(value, "0") == 0) { 30854345Sdougm ret = SA_OK; 30864345Sdougm } else { 30874345Sdougm ret = SA_BAD_VALUE; 30884345Sdougm } 30894345Sdougm break; 30903910Sdougm 30913910Sdougm case OPT_TYPE_ONOFF: 30924345Sdougm if (strcasecmp(value, "on") != 0 && 30934345Sdougm strcasecmp(value, "off") != 0) { 30944345Sdougm ret = SA_BAD_VALUE; 30954345Sdougm } 30964345Sdougm break; 30973910Sdougm 30983910Sdougm case OPT_TYPE_PROTOCOL: 30996162Sdougm if (strlen(value) != 0 && 31006162Sdougm strcasecmp(value, "all") != 0 && 31014345Sdougm strcasecmp(value, "tcp") != 0 && 31024345Sdougm strcasecmp(value, "udp") != 0) 31034345Sdougm ret = SA_BAD_VALUE; 31044345Sdougm break; 31053910Sdougm 31063910Sdougm default: 31074345Sdougm /* treat as a string */ 31084345Sdougm break; 31093910Sdougm } 31103910Sdougm return (ret); 31113910Sdougm } 31123910Sdougm 31133910Sdougm /* 31143910Sdougm * nfs_set_proto_prop(prop) 31153910Sdougm * 31163910Sdougm * check that prop is valid. 31173910Sdougm */ 31183910Sdougm 31193910Sdougm static int 31203910Sdougm nfs_set_proto_prop(sa_property_t prop) 31213910Sdougm { 31223910Sdougm int ret = SA_OK; 31233910Sdougm char *name; 31243910Sdougm char *value; 31253910Sdougm 31263910Sdougm name = sa_get_property_attr(prop, "type"); 31273910Sdougm value = sa_get_property_attr(prop, "value"); 31283910Sdougm if (name != NULL && value != NULL) { 31294345Sdougm int index = findprotoopt(name, 1); 31304345Sdougm if (index >= 0) { 31314345Sdougm /* should test for valid value */ 31324345Sdougm ret = nfs_validate_proto_prop(index, name, value); 31334345Sdougm if (ret == SA_OK) 31344345Sdougm ret = set_default_file_value( 31354345Sdougm proto_options[index].tag, value); 31364345Sdougm if (ret == SA_OK) 31374345Sdougm restart_service(proto_options[index].svcs); 31384345Sdougm } 31393910Sdougm } 31403910Sdougm if (name != NULL) 31414345Sdougm sa_free_attr_string(name); 31423910Sdougm if (value != NULL) 31434345Sdougm sa_free_attr_string(value); 31443910Sdougm return (ret); 31453910Sdougm } 31463910Sdougm 31473910Sdougm /* 31483910Sdougm * nfs_get_status() 31493910Sdougm * 31503910Sdougm * What is the current status of the nfsd? We use the SMF state here. 31513910Sdougm * Caller must free the returned value. 31523910Sdougm */ 31533910Sdougm 31543910Sdougm static char * 31553910Sdougm nfs_get_status() 31563910Sdougm { 31573910Sdougm char *state; 31583910Sdougm state = smf_get_state(NFSD); 31593910Sdougm return (state != NULL ? state : strdup("-")); 31603910Sdougm } 31613910Sdougm 31623910Sdougm /* 31633910Sdougm * nfs_space_alias(alias) 31643910Sdougm * 31653910Sdougm * Lookup the space (security) name. If it is default, convert to the 31663910Sdougm * real name. 31673910Sdougm */ 31683910Sdougm 31693910Sdougm static char * 31703910Sdougm nfs_space_alias(char *space) 31713910Sdougm { 31723910Sdougm char *name = space; 31733910Sdougm seconfig_t secconf; 31743910Sdougm 31753910Sdougm /* 31763910Sdougm * Only the space named "default" is special. If it is used, 31773910Sdougm * the default needs to be looked up and the real name used. 31783910Sdougm * This is normally "sys" but could be changed. We always 31793910Sdougm * change defautl to the real name. 31803910Sdougm */ 31813910Sdougm if (strcmp(space, "default") == 0 && 31823910Sdougm nfs_getseconfig_default(&secconf) == 0) { 31834345Sdougm if (nfs_getseconfig_bynumber(secconf.sc_nfsnum, &secconf) == 0) 31844345Sdougm name = secconf.sc_name; 31853910Sdougm } 31863910Sdougm return (strdup(name)); 31873910Sdougm } 31885331Samw 31895331Samw /* 31905331Samw * nfs_features() 31915331Samw * 31925331Samw * Return a mask of the features required. 31935331Samw */ 31945331Samw 31955331Samw static uint64_t 31965331Samw nfs_features() 31975331Samw { 31986088Sdougm return ((uint64_t)SA_FEATURE_DFSTAB | SA_FEATURE_SERVER); 31995331Samw } 3200