15331Samw /* 25331Samw * CDDL HEADER START 35331Samw * 45331Samw * The contents of this file are subject to the terms of the 55331Samw * Common Development and Distribution License (the "License"). 65331Samw * You may not use this file except in compliance with the License. 75331Samw * 85331Samw * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 95331Samw * or http://www.opensolaris.org/os/licensing. 105331Samw * See the License for the specific language governing permissions 115331Samw * and limitations under the License. 125331Samw * 135331Samw * When distributing Covered Code, include this CDDL HEADER in each 145331Samw * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 155331Samw * If applicable, add the following below this CDDL HEADER, with the 165331Samw * fields enclosed by brackets "[]" replaced with your own identifying 175331Samw * information: Portions Copyright [yyyy] [name of copyright owner] 185331Samw * 195331Samw * CDDL HEADER END 205331Samw */ 215331Samw 225331Samw /* 235772Sas200622 * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 245331Samw * Use is subject to license terms. 255331Samw */ 265331Samw 27*7348SJose.Borrego@Sun.COM #pragma ident "@(#)libshare_smb.c 1.16 08/08/05 SMI" 285331Samw 295331Samw /* 305331Samw * SMB specific functions 315331Samw */ 325331Samw #include <stdio.h> 335331Samw #include <string.h> 345331Samw #include <ctype.h> 355331Samw #include <stdlib.h> 365331Samw #include <unistd.h> 375331Samw #include <zone.h> 385331Samw #include <errno.h> 395331Samw #include <locale.h> 405331Samw #include <fcntl.h> 415331Samw #include <sys/types.h> 425331Samw #include <sys/stat.h> 435331Samw #include <syslog.h> 445331Samw #include "libshare.h" 455331Samw #include "libshare_impl.h" 465331Samw #include <pwd.h> 475331Samw #include <limits.h> 485331Samw #include <libscf.h> 495331Samw #include <strings.h> 505331Samw #include "libshare_smb.h" 515331Samw #include <rpcsvc/daemon_utils.h> 527052Samw #include <smbsrv/smb_share.h> 535331Samw #include <smbsrv/smbinfo.h> 545331Samw #include <smbsrv/libsmb.h> 555331Samw 565331Samw /* internal functions */ 575331Samw static int smb_share_init(void); 585331Samw static void smb_share_fini(void); 595331Samw static int smb_enable_share(sa_share_t); 605331Samw static int smb_share_changed(sa_share_t); 615331Samw static int smb_resource_changed(sa_resource_t); 625331Samw static int smb_rename_resource(sa_handle_t, sa_resource_t, char *); 635331Samw static int smb_disable_share(sa_share_t share, char *); 646214Sdougm static int smb_validate_property(sa_handle_t, sa_property_t, sa_optionset_t); 655331Samw static int smb_set_proto_prop(sa_property_t); 665331Samw static sa_protocol_properties_t smb_get_proto_set(void); 675331Samw static char *smb_get_status(void); 685331Samw static int smb_parse_optstring(sa_group_t, char *); 695331Samw static char *smb_format_options(sa_group_t, int); 705331Samw 715331Samw static int smb_enable_service(void); 725331Samw 735331Samw static int range_check_validator(int, char *); 745331Samw static int range_check_validator_zero_ok(int, char *); 755331Samw static int string_length_check_validator(int, char *); 765331Samw static int true_false_validator(int, char *); 775331Samw static int ip_address_validator_empty_ok(int, char *); 785331Samw static int ip_address_csv_list_validator_empty_ok(int, char *); 795331Samw static int path_validator(int, char *); 805331Samw 815331Samw static int smb_enable_resource(sa_resource_t); 825331Samw static int smb_disable_resource(sa_resource_t); 835331Samw static uint64_t smb_share_features(void); 845331Samw static int smb_list_transient(sa_handle_t); 855772Sas200622 86*7348SJose.Borrego@Sun.COM static int smb_build_shareinfo(sa_share_t, sa_resource_t, smb_share_t *); 87*7348SJose.Borrego@Sun.COM static sa_group_t smb_get_defaultgrp(sa_handle_t); 88*7348SJose.Borrego@Sun.COM 895331Samw /* size of basic format allocation */ 905331Samw #define OPT_CHUNK 1024 915331Samw 925772Sas200622 /* size of string for types - big enough to hold "dependency" */ 935772Sas200622 #define SCFTYPE_LEN 32 945772Sas200622 955331Samw /* 965331Samw * Indexes of entries in smb_proto_options table. 975331Samw * Changes to smb_proto_options table may require 985331Samw * an update to these values. 995331Samw */ 1005331Samw #define PROTO_OPT_WINS1 6 1015331Samw #define PROTO_OPT_WINS_EXCLUDE 8 1025331Samw 1035331Samw 1045331Samw /* 1055331Samw * ops vector that provides the protocol specific info and operations 1065331Samw * for share management. 1075331Samw */ 1085331Samw 1095331Samw struct sa_plugin_ops sa_plugin_ops = { 1105331Samw SA_PLUGIN_VERSION, 1115331Samw SMB_PROTOCOL_NAME, 1125331Samw smb_share_init, 1135331Samw smb_share_fini, 1145331Samw smb_enable_share, 1155331Samw smb_disable_share, 1165331Samw smb_validate_property, 1176007Sthurlow NULL, /* valid_space */ 1186007Sthurlow NULL, /* security_prop */ 1195331Samw smb_parse_optstring, 1205331Samw smb_format_options, 1215331Samw smb_set_proto_prop, 1225331Samw smb_get_proto_set, 1235331Samw smb_get_status, 1246007Sthurlow NULL, /* space_alias */ 1256007Sthurlow NULL, /* update_legacy */ 1266007Sthurlow NULL, /* delete_legacy */ 1275331Samw smb_share_changed, 1285331Samw smb_enable_resource, 1295331Samw smb_disable_resource, 1305331Samw smb_share_features, 1315331Samw smb_list_transient, 1325331Samw smb_resource_changed, 1335331Samw smb_rename_resource, 1346007Sthurlow NULL, /* run_command */ 1356007Sthurlow NULL, /* command_help */ 1366007Sthurlow NULL /* delete_proto_section */ 1375331Samw }; 1385331Samw 1395331Samw /* 1405331Samw * option definitions. Make sure to keep the #define for the option 1415331Samw * index just before the entry it is the index for. Changing the order 1425331Samw * can cause breakage. 1435331Samw */ 1445331Samw 1455331Samw struct option_defs optdefs[] = { 1467052Samw {SMB_SHROPT_AD_CONTAINER, OPT_TYPE_STRING}, 1477052Samw {SMB_SHROPT_NAME, OPT_TYPE_NAME}, 1485331Samw {NULL, NULL}, 1495331Samw }; 1505331Samw 1515331Samw /* 1525331Samw * findopt(name) 1535331Samw * 1545331Samw * Lookup option "name" in the option table and return the table 1555331Samw * index. 1565331Samw */ 1575331Samw static int 1585331Samw findopt(char *name) 1595331Samw { 1605331Samw int i; 1615331Samw if (name != NULL) { 1625331Samw for (i = 0; optdefs[i].tag != NULL; i++) { 1635331Samw if (strcmp(optdefs[i].tag, name) == 0) 1645331Samw return (i); 1655331Samw } 1665331Samw } 1675331Samw return (-1); 1685331Samw } 1695331Samw 1705331Samw /* 1715331Samw * is_a_number(number) 1725331Samw * 1735331Samw * is the string a number in one of the forms we want to use? 1745331Samw */ 175*7348SJose.Borrego@Sun.COM static boolean_t 1765331Samw is_a_number(char *number) 1775331Samw { 178*7348SJose.Borrego@Sun.COM boolean_t isnum = B_TRUE; 179*7348SJose.Borrego@Sun.COM boolean_t ishex = B_FALSE; 180*7348SJose.Borrego@Sun.COM 181*7348SJose.Borrego@Sun.COM if (number == NULL || *number == '\0') 182*7348SJose.Borrego@Sun.COM return (B_FALSE); 1835331Samw 184*7348SJose.Borrego@Sun.COM if (strncasecmp(number, "0x", 2) == 0) { 1855331Samw number += 2; 186*7348SJose.Borrego@Sun.COM ishex = B_TRUE; 1875331Samw } else if (*number == '-') { 188*7348SJose.Borrego@Sun.COM number++; 1895331Samw } 1905331Samw 191*7348SJose.Borrego@Sun.COM while (isnum && (*number != '\0')) { 192*7348SJose.Borrego@Sun.COM isnum = (ishex) ? isxdigit(*number) : isdigit(*number); 193*7348SJose.Borrego@Sun.COM number++; 1945331Samw } 195*7348SJose.Borrego@Sun.COM 196*7348SJose.Borrego@Sun.COM return (isnum); 1975331Samw } 1985331Samw 1995331Samw /* 2005331Samw * validresource(name) 2015331Samw * 2025331Samw * Check that name only has valid characters in it. The current valid 2035331Samw * set are the printable characters but not including: 2045331Samw * " / \ [ ] : | < > + ; , ? * = \t 2055331Samw * Note that space is included and there is a maximum length. 2065331Samw */ 207*7348SJose.Borrego@Sun.COM static boolean_t 2085331Samw validresource(const char *name) 2095331Samw { 2105331Samw const char *cp; 2115331Samw size_t len; 2125331Samw 2135331Samw if (name == NULL) 2145331Samw return (B_FALSE); 2155331Samw 2165331Samw len = strlen(name); 2175331Samw if (len == 0 || len > SA_MAX_RESOURCE_NAME) 2185331Samw return (B_FALSE); 2195331Samw 2205331Samw if (strpbrk(name, "\"/\\[]:|<>+;,?*=\t") != NULL) { 2215331Samw return (B_FALSE); 2225331Samw } 2235331Samw 2245331Samw for (cp = name; *cp != '\0'; cp++) 2255331Samw if (iscntrl(*cp)) 2265331Samw return (B_FALSE); 2275331Samw 2285331Samw return (B_TRUE); 2295331Samw } 2305331Samw 2315331Samw /* 2325331Samw * smb_isonline() 2335331Samw * 2345331Samw * Determine if the SMF service instance is in the online state or 2355331Samw * not. A number of operations depend on this state. 2365331Samw */ 2375331Samw static boolean_t 2385331Samw smb_isonline(void) 2395331Samw { 2405331Samw char *str; 2415331Samw boolean_t ret = B_FALSE; 2425331Samw 2435331Samw if ((str = smf_get_state(SMBD_DEFAULT_INSTANCE_FMRI)) != NULL) { 2445331Samw ret = (strcmp(str, SCF_STATE_STRING_ONLINE) == 0); 2455331Samw free(str); 2465331Samw } 2475331Samw return (ret); 2485331Samw } 2495331Samw 2505331Samw /* 2515772Sas200622 * smb_isdisabled() 2525772Sas200622 * 2535772Sas200622 * Determine if the SMF service instance is in the disabled state or 2545772Sas200622 * not. A number of operations depend on this state. 2555772Sas200622 */ 2565772Sas200622 static boolean_t 2575772Sas200622 smb_isdisabled(void) 2585772Sas200622 { 2595772Sas200622 char *str; 2605772Sas200622 boolean_t ret = B_FALSE; 2615772Sas200622 2625772Sas200622 if ((str = smf_get_state(SMBD_DEFAULT_INSTANCE_FMRI)) != NULL) { 2635772Sas200622 ret = (strcmp(str, SCF_STATE_STRING_DISABLED) == 0); 2645772Sas200622 free(str); 2655772Sas200622 } 2665772Sas200622 return (ret); 2675772Sas200622 } 2685772Sas200622 2695772Sas200622 /* 2705772Sas200622 * smb_isautoenable() 2715772Sas200622 * 2725772Sas200622 * Determine if the SMF service instance auto_enabled set or not. A 2735772Sas200622 * number of operations depend on this state. The property not being 2745772Sas200622 * set or being set to true means autoenable. Only being set to false 2755772Sas200622 * is not autoenabled. 2765772Sas200622 */ 2775772Sas200622 static boolean_t 2785772Sas200622 smb_isautoenable(void) 2795772Sas200622 { 2805772Sas200622 boolean_t ret = B_TRUE; 2815772Sas200622 scf_simple_prop_t *prop; 2825772Sas200622 uint8_t *retstr; 2835772Sas200622 2845772Sas200622 prop = scf_simple_prop_get(NULL, SMBD_DEFAULT_INSTANCE_FMRI, 2855772Sas200622 "application", "auto_enable"); 2865772Sas200622 if (prop != NULL) { 2875772Sas200622 retstr = scf_simple_prop_next_boolean(prop); 2885772Sas200622 ret = *retstr != 0; 2895772Sas200622 scf_simple_prop_free(prop); 2905772Sas200622 } 2915772Sas200622 return (ret); 2925772Sas200622 } 2935772Sas200622 2945772Sas200622 /* 2956030Sjb150015 * smb_ismaint() 2966030Sjb150015 * 2976030Sjb150015 * Determine if the SMF service instance is in the disabled state or 2986030Sjb150015 * not. A number of operations depend on this state. 2996030Sjb150015 */ 3006030Sjb150015 static boolean_t 3016030Sjb150015 smb_ismaint(void) 3026030Sjb150015 { 3036030Sjb150015 char *str; 3046030Sjb150015 boolean_t ret = B_FALSE; 3056030Sjb150015 3066030Sjb150015 if ((str = smf_get_state(SMBD_DEFAULT_INSTANCE_FMRI)) != NULL) { 3076030Sjb150015 ret = (strcmp(str, SCF_STATE_STRING_MAINT) == 0); 3086030Sjb150015 free(str); 3096030Sjb150015 } 3106030Sjb150015 return (ret); 3116030Sjb150015 } 3126030Sjb150015 3136030Sjb150015 /* 3145331Samw * smb_enable_share tells the implementation that it is to enable the share. 3155331Samw * This entails converting the path and options into the appropriate ioctl 3165331Samw * calls. It is assumed that all error checking of paths, etc. were 3175331Samw * done earlier. 3185331Samw */ 3195331Samw static int 3205331Samw smb_enable_share(sa_share_t share) 3215331Samw { 3225331Samw char *path; 3237052Samw smb_share_t si; 3245331Samw sa_resource_t resource; 3255331Samw boolean_t iszfs; 3265331Samw boolean_t privileged; 3275331Samw int err = SA_OK; 3285331Samw priv_set_t *priv_effective; 3295331Samw boolean_t online; 3305331Samw 3316270Sdougm /* 3326270Sdougm * We only start in the global zone and only run if we aren't 3336270Sdougm * running Trusted Extensions. 3346270Sdougm */ 3356270Sdougm if (getzoneid() != GLOBAL_ZONEID) { 3366270Sdougm (void) printf(dgettext(TEXT_DOMAIN, 3376270Sdougm "SMB: service not supported in local zone\n")); 3386270Sdougm return (SA_NOT_SUPPORTED); 3396270Sdougm } 3406270Sdougm if (is_system_labeled()) { 3416270Sdougm (void) printf(dgettext(TEXT_DOMAIN, 3426270Sdougm "SMB: service not supported with Trusted Extensions\n")); 3436270Sdougm return (SA_NOT_SUPPORTED); 3446270Sdougm } 3456270Sdougm 3465331Samw priv_effective = priv_allocset(); 3475331Samw (void) getppriv(PRIV_EFFECTIVE, priv_effective); 3485331Samw privileged = (priv_isfullset(priv_effective) == B_TRUE); 3495331Samw priv_freeset(priv_effective); 3505331Samw 3515331Samw /* get the path since it is important in several places */ 3525331Samw path = sa_get_share_attr(share, "path"); 3535331Samw if (path == NULL) 3545331Samw return (SA_NO_SUCH_PATH); 3555331Samw 3565772Sas200622 /* 3575772Sas200622 * If administratively disabled, don't try to start anything. 3585772Sas200622 */ 3595331Samw online = smb_isonline(); 3605772Sas200622 if (!online && !smb_isautoenable() && smb_isdisabled()) 3615772Sas200622 goto done; 3625331Samw 3635331Samw iszfs = sa_path_is_zfs(path); 3645331Samw 3655331Samw if (iszfs) { 3665331Samw 3675331Samw if (privileged == B_FALSE && !online) { 3685331Samw 3695331Samw if (!online) { 3705331Samw (void) printf(dgettext(TEXT_DOMAIN, 3715331Samw "SMB: Cannot share remove " 3725331Samw "file system: %s\n"), path); 3735331Samw (void) printf(dgettext(TEXT_DOMAIN, 3745331Samw "SMB: Service needs to be enabled " 3755331Samw "by a privileged user\n")); 3765331Samw err = SA_NO_PERMISSION; 3775331Samw errno = EPERM; 3785331Samw } 3795331Samw if (err) { 3805331Samw sa_free_attr_string(path); 3815331Samw return (err); 3825331Samw } 3835331Samw 3845331Samw } 3855331Samw } 3865331Samw 3875331Samw if (privileged == B_TRUE && !online) { 3885331Samw err = smb_enable_service(); 3895331Samw if (err != SA_OK) { 3905331Samw (void) printf(dgettext(TEXT_DOMAIN, 3915331Samw "SMB: Unable to enable service\n")); 3925331Samw /* 3935331Samw * For now, it is OK to not be able to enable 3945331Samw * the service. 3955331Samw */ 3966030Sjb150015 if (err == SA_BUSY || err == SA_SYSTEM_ERR) 3975331Samw err = SA_OK; 3985331Samw } else { 3995331Samw online = B_TRUE; 4005331Samw } 4015331Samw } 4025331Samw 4035331Samw /* 4045331Samw * Don't bother trying to start shares if the service isn't 4055331Samw * running. 4065331Samw */ 4075331Samw if (!online) 4085331Samw goto done; 4095331Samw 4105331Samw /* Each share can have multiple resources */ 4115331Samw for (resource = sa_get_share_resource(share, NULL); 4125331Samw resource != NULL; 4135331Samw resource = sa_get_next_resource(resource)) { 414*7348SJose.Borrego@Sun.COM err = smb_build_shareinfo(share, resource, &si); 415*7348SJose.Borrego@Sun.COM if (err != SA_OK) { 4165331Samw sa_free_attr_string(path); 417*7348SJose.Borrego@Sun.COM return (err); 4185331Samw } 4195331Samw 4205331Samw if (!iszfs) { 421*7348SJose.Borrego@Sun.COM err = smb_share_create(&si); 4225331Samw } else { 4235331Samw share_t sh; 4245331Samw 4255331Samw sa_sharetab_fill_zfs(share, &sh, "smb"); 4265331Samw err = sa_share_zfs(share, (char *)path, &sh, 4275331Samw &si, ZFS_SHARE_SMB); 4285331Samw 4295331Samw sa_emptyshare(&sh); 4305331Samw } 4315331Samw } 4325331Samw if (!iszfs) 4335331Samw (void) sa_update_sharetab(share, "smb"); 4345331Samw done: 4355331Samw sa_free_attr_string(path); 4365331Samw 4375331Samw return (err == NERR_DuplicateShare ? 0 : err); 4385331Samw } 4395331Samw 4405331Samw /* 4415331Samw * This is the share for CIFS all shares have resource names. 4425331Samw * Enable tells the smb server to update its hash. If it fails 4435331Samw * because smb server is down, we just ignore as smb server loads 4445331Samw * the resources from sharemanager at startup. 4455331Samw */ 4465331Samw 4475331Samw static int 4485331Samw smb_enable_resource(sa_resource_t resource) 4495331Samw { 4505331Samw sa_share_t share; 4517052Samw smb_share_t si; 4525772Sas200622 int ret = SA_OK; 4535772Sas200622 int err; 4545772Sas200622 boolean_t isonline; 4555331Samw 4565331Samw share = sa_get_resource_parent(resource); 4575331Samw if (share == NULL) 4585331Samw return (SA_NO_SUCH_PATH); 4595772Sas200622 4605772Sas200622 /* 4615772Sas200622 * If administratively disabled, don't try to start anything. 4625772Sas200622 */ 4635772Sas200622 isonline = smb_isonline(); 4645772Sas200622 if (!isonline && !smb_isautoenable() && smb_isdisabled()) 465*7348SJose.Borrego@Sun.COM return (SA_OK); 4665772Sas200622 467*7348SJose.Borrego@Sun.COM if (!isonline) { 468*7348SJose.Borrego@Sun.COM (void) smb_enable_service(); 469*7348SJose.Borrego@Sun.COM 470*7348SJose.Borrego@Sun.COM if (!smb_isonline()) 471*7348SJose.Borrego@Sun.COM return (SA_OK); 4725772Sas200622 } 4735772Sas200622 474*7348SJose.Borrego@Sun.COM if ((ret = smb_build_shareinfo(share, resource, &si)) != SA_OK) 475*7348SJose.Borrego@Sun.COM return (ret); 4765772Sas200622 4775772Sas200622 /* 4785772Sas200622 * Attempt to add the share. Any error that occurs if it was 4795772Sas200622 * online is an error but don't count NERR_DuplicateName if 4805772Sas200622 * smb/server had to be brought online since bringing the 4815772Sas200622 * service up will enable the share that was just added prior 4825772Sas200622 * to the attempt to enable. 4835772Sas200622 */ 484*7348SJose.Borrego@Sun.COM err = smb_share_create(&si); 4855772Sas200622 if (err == NERR_Success || !(!isonline && err == NERR_DuplicateName)) 4865772Sas200622 (void) sa_update_sharetab(share, "smb"); 4875772Sas200622 else 4885331Samw return (SA_NOT_SHARED); 4895331Samw 490*7348SJose.Borrego@Sun.COM return (SA_OK); 4915331Samw } 4925331Samw 4935331Samw /* 4945331Samw * Remove it from smb server hash. 4955331Samw */ 4965331Samw static int 4975331Samw smb_disable_resource(sa_resource_t resource) 4985331Samw { 4995331Samw char *rname; 5007052Samw uint32_t res; 5015331Samw sa_share_t share; 5025331Samw 5035331Samw rname = sa_get_resource_attr(resource, "name"); 5045331Samw if (rname == NULL) 5055331Samw return (SA_NO_SUCH_RESOURCE); 5065331Samw 5075331Samw if (smb_isonline()) { 508*7348SJose.Borrego@Sun.COM res = smb_share_delete(rname); 5095331Samw if (res != NERR_Success) { 5105331Samw sa_free_attr_string(rname); 5115331Samw return (SA_CONFIG_ERR); 5125331Samw } 5135331Samw } 5145951Sdougm 5155951Sdougm sa_free_attr_string(rname); 5165951Sdougm 5175331Samw share = sa_get_resource_parent(resource); 5185331Samw if (share != NULL) { 5195331Samw rname = sa_get_share_attr(share, "path"); 5205331Samw if (rname != NULL) { 5215951Sdougm sa_handle_t handle; 5225951Sdougm 5235951Sdougm handle = sa_find_group_handle((sa_group_t)resource); 5245951Sdougm (void) sa_delete_sharetab(handle, rname, "smb"); 5255331Samw sa_free_attr_string(rname); 5265331Samw } 5275331Samw } 5285331Samw /* 5295331Samw * Always return OK as smb/server may be down and 5305331Samw * Shares will be picked up when loaded. 5315331Samw */ 5325331Samw return (SA_OK); 5335331Samw } 5345331Samw 5355331Samw /* 5365331Samw * smb_share_changed(sa_share_t share) 5375331Samw * 5385331Samw * The specified share has changed. 5395331Samw */ 5405331Samw static int 5415331Samw smb_share_changed(sa_share_t share) 5425331Samw { 5435331Samw char *path; 5445331Samw sa_resource_t resource; 5455331Samw 546*7348SJose.Borrego@Sun.COM if (!smb_isonline()) 547*7348SJose.Borrego@Sun.COM return (SA_OK); 548*7348SJose.Borrego@Sun.COM 5495331Samw /* get the path since it is important in several places */ 5505331Samw path = sa_get_share_attr(share, "path"); 5515331Samw if (path == NULL) 5525331Samw return (SA_NO_SUCH_PATH); 553*7348SJose.Borrego@Sun.COM 5545331Samw for (resource = sa_get_share_resource(share, NULL); 5555331Samw resource != NULL; 5565331Samw resource = sa_get_next_resource(resource)) 5575331Samw (void) smb_resource_changed(resource); 5585331Samw 5595331Samw sa_free_attr_string(path); 5605331Samw 5615331Samw return (SA_OK); 5625331Samw } 5635331Samw 5645331Samw /* 5655331Samw * smb_resource_changed(sa_resource_t resource) 5665331Samw * 5675331Samw * The specified resource has changed. 5685331Samw */ 5695331Samw static int 5705331Samw smb_resource_changed(sa_resource_t resource) 5715331Samw { 5727052Samw uint32_t res; 573*7348SJose.Borrego@Sun.COM sa_share_t share; 5747052Samw smb_share_t si; 5755331Samw 576*7348SJose.Borrego@Sun.COM if (!smb_isonline()) 577*7348SJose.Borrego@Sun.COM return (SA_OK); 5785331Samw 579*7348SJose.Borrego@Sun.COM if ((share = sa_get_resource_parent(resource)) == NULL) 580*7348SJose.Borrego@Sun.COM return (SA_CONFIG_ERR); 5815331Samw 582*7348SJose.Borrego@Sun.COM if ((res = smb_build_shareinfo(share, resource, &si)) != SA_OK) 583*7348SJose.Borrego@Sun.COM return (res); 5845331Samw 585*7348SJose.Borrego@Sun.COM res = smb_share_modify(si.shr_name, si.shr_cmnt, si.shr_container); 586*7348SJose.Borrego@Sun.COM 587*7348SJose.Borrego@Sun.COM if (res != NERR_Success) 5885331Samw return (SA_CONFIG_ERR); 5895331Samw 5905331Samw return (smb_enable_service()); 5915331Samw } 5925331Samw 5935331Samw /* 5945800Sdougm * smb_disable_share(sa_share_t share, char *path) 5955331Samw * 5965800Sdougm * Unshare the specified share. Note that "path" is the same 5975800Sdougm * path as what is in the "share" object. It is passed in to avoid an 5985800Sdougm * additional lookup. A missing "path" value makes this a no-op 5995800Sdougm * function. 6005331Samw */ 6015331Samw static int 6025331Samw smb_disable_share(sa_share_t share, char *path) 6035331Samw { 6045331Samw char *rname; 6055331Samw sa_resource_t resource; 6065800Sdougm sa_group_t parent; 6075331Samw boolean_t iszfs; 6085331Samw int err = SA_OK; 6095951Sdougm sa_handle_t handle; 6105331Samw 6115800Sdougm if (path == NULL) 6125800Sdougm return (err); 6135800Sdougm 6145800Sdougm /* 6155800Sdougm * If the share is in a ZFS group we need to handle it 6165800Sdougm * differently. Just being on a ZFS file system isn't 6175800Sdougm * enough since we may be in a legacy share case. 6185800Sdougm */ 6195800Sdougm parent = sa_get_parent_group(share); 6205800Sdougm iszfs = sa_group_is_zfs(parent); 6215800Sdougm 6225331Samw if (!smb_isonline()) 6235331Samw goto done; 6245331Samw 6255331Samw for (resource = sa_get_share_resource(share, NULL); 6265331Samw resource != NULL; 6275331Samw resource = sa_get_next_resource(resource)) { 6285331Samw rname = sa_get_resource_attr(resource, "name"); 6295331Samw if (rname == NULL) { 6305331Samw continue; 6315331Samw } 6325331Samw if (!iszfs) { 633*7348SJose.Borrego@Sun.COM err = smb_share_delete(rname); 6345331Samw switch (err) { 6355331Samw case NERR_NetNameNotFound: 6365331Samw case NERR_Success: 6375331Samw err = SA_OK; 6385331Samw break; 6395331Samw default: 6405331Samw err = SA_CONFIG_ERR; 6415331Samw break; 6425331Samw } 6435331Samw } else { 6445331Samw share_t sh; 6455331Samw 6465331Samw sa_sharetab_fill_zfs(share, &sh, "smb"); 6475331Samw err = sa_share_zfs(share, (char *)path, &sh, 6485331Samw rname, ZFS_UNSHARE_SMB); 6495331Samw sa_emptyshare(&sh); 6505331Samw } 6515331Samw sa_free_attr_string(rname); 6525331Samw } 6535331Samw done: 6545951Sdougm if (!iszfs) { 6555951Sdougm handle = sa_find_group_handle((sa_group_t)share); 6565951Sdougm if (handle != NULL) 6575951Sdougm (void) sa_delete_sharetab(handle, path, "smb"); 6585951Sdougm else 6595951Sdougm err = SA_SYSTEM_ERR; 6605951Sdougm } 6615331Samw return (err); 6625331Samw } 6635331Samw 6645331Samw /* 6656214Sdougm * smb_validate_property(handle, property, parent) 6665331Samw * 6675331Samw * Check that the property has a legitimate value for its type. 6686214Sdougm * Handle isn't currently used but may need to be in the future. 6695331Samw */ 6705331Samw 6716214Sdougm /*ARGSUSED*/ 6725331Samw static int 6736214Sdougm smb_validate_property(sa_handle_t handle, sa_property_t property, 6746214Sdougm sa_optionset_t parent) 6755331Samw { 6765331Samw int ret = SA_OK; 6775331Samw char *propname; 6785331Samw int optindex; 6795331Samw sa_group_t parent_group; 6805331Samw char *value; 6815331Samw 6825331Samw propname = sa_get_property_attr(property, "type"); 6835331Samw 6845331Samw if ((optindex = findopt(propname)) < 0) 6855331Samw ret = SA_NO_SUCH_PROP; 6865331Samw 6875331Samw /* need to validate value range here as well */ 6885331Samw if (ret == SA_OK) { 6895331Samw parent_group = sa_get_parent_group((sa_share_t)parent); 6905331Samw if (optdefs[optindex].share && !sa_is_share(parent_group)) 6915331Samw ret = SA_PROP_SHARE_ONLY; 6925331Samw } 6935331Samw if (ret != SA_OK) { 6945331Samw if (propname != NULL) 6955331Samw sa_free_attr_string(propname); 6965331Samw return (ret); 6975331Samw } 6985331Samw 6995331Samw value = sa_get_property_attr(property, "value"); 7005331Samw if (value != NULL) { 7015331Samw /* first basic type checking */ 7025331Samw switch (optdefs[optindex].type) { 7035331Samw case OPT_TYPE_NUMBER: 7045331Samw /* check that the value is all digits */ 7055331Samw if (!is_a_number(value)) 7065331Samw ret = SA_BAD_VALUE; 7075331Samw break; 7085331Samw case OPT_TYPE_BOOLEAN: 7095331Samw if (strlen(value) == 0 || 7105331Samw strcasecmp(value, "true") == 0 || 7115331Samw strcmp(value, "1") == 0 || 7125331Samw strcasecmp(value, "false") == 0 || 7135331Samw strcmp(value, "0") == 0) { 7145331Samw ret = SA_OK; 7155331Samw } else { 7165331Samw ret = SA_BAD_VALUE; 7175331Samw } 7185331Samw break; 7195331Samw case OPT_TYPE_NAME: 7205331Samw /* 7215331Samw * Make sure no invalid characters 7225331Samw */ 7235331Samw if (validresource(value) == B_FALSE) 7245331Samw ret = SA_BAD_VALUE; 7255331Samw break; 7265331Samw case OPT_TYPE_STRING: 7275331Samw /* whatever is here should be ok */ 7285331Samw break; 7295331Samw default: 7305331Samw break; 7315331Samw } 7325331Samw } 7335331Samw 7345331Samw if (value != NULL) 7355331Samw sa_free_attr_string(value); 7365331Samw if (ret == SA_OK && optdefs[optindex].check != NULL) 7375331Samw /* do the property specific check */ 7385331Samw ret = optdefs[optindex].check(property); 7395331Samw 7405331Samw if (propname != NULL) 7415331Samw sa_free_attr_string(propname); 7425331Samw return (ret); 7435331Samw } 7445331Samw 7455331Samw /* 7465331Samw * Protocol management functions 7475331Samw * 7485331Samw * properties defined in the default files are defined in 7495331Samw * proto_option_defs for parsing and validation. 7505331Samw */ 7515331Samw 7525331Samw struct smb_proto_option_defs { 7535331Samw int smb_index; 7545331Samw int32_t minval; 7555331Samw int32_t maxval; /* In case of length of string this should be max */ 7565331Samw int (*validator)(int, char *); 7575331Samw int32_t refresh; 7585331Samw } smb_proto_options[] = { 7595772Sas200622 { SMB_CI_SYS_CMNT, 0, MAX_VALUE_BUFLEN, 7605772Sas200622 string_length_check_validator, SMB_REFRESH_REFRESH }, 7615772Sas200622 { SMB_CI_MAX_WORKERS, 64, 1024, range_check_validator, 7625772Sas200622 SMB_REFRESH_REFRESH }, 7635772Sas200622 { SMB_CI_NBSCOPE, 0, MAX_VALUE_BUFLEN, 7645772Sas200622 string_length_check_validator, 0 }, 7655772Sas200622 { SMB_CI_LM_LEVEL, 2, 5, range_check_validator, 0 }, 7665772Sas200622 { SMB_CI_KEEPALIVE, 20, 5400, range_check_validator_zero_ok, 7675772Sas200622 SMB_REFRESH_REFRESH }, 7685772Sas200622 { SMB_CI_WINS_SRV1, 0, MAX_VALUE_BUFLEN, 7695772Sas200622 ip_address_validator_empty_ok, SMB_REFRESH_REFRESH }, 7705772Sas200622 { SMB_CI_WINS_SRV2, 0, MAX_VALUE_BUFLEN, 7715772Sas200622 ip_address_validator_empty_ok, SMB_REFRESH_REFRESH }, 7725772Sas200622 { SMB_CI_WINS_EXCL, 0, MAX_VALUE_BUFLEN, 7735772Sas200622 ip_address_csv_list_validator_empty_ok, SMB_REFRESH_REFRESH }, 7745772Sas200622 { SMB_CI_SIGNING_ENABLE, 0, 0, true_false_validator, 7755772Sas200622 SMB_REFRESH_REFRESH }, 7765772Sas200622 { SMB_CI_SIGNING_REQD, 0, 0, true_false_validator, 7775772Sas200622 SMB_REFRESH_REFRESH }, 7785772Sas200622 { SMB_CI_RESTRICT_ANON, 0, 0, true_false_validator, 7795772Sas200622 SMB_REFRESH_REFRESH }, 7805772Sas200622 { SMB_CI_DOMAIN_SRV, 0, MAX_VALUE_BUFLEN, 7815772Sas200622 ip_address_validator_empty_ok, 0 }, 7825772Sas200622 { SMB_CI_ADS_SITE, 0, MAX_VALUE_BUFLEN, 7835772Sas200622 string_length_check_validator, SMB_REFRESH_REFRESH }, 7845772Sas200622 { SMB_CI_DYNDNS_ENABLE, 0, 0, true_false_validator, 0 }, 7855772Sas200622 { SMB_CI_AUTOHOME_MAP, 0, MAX_VALUE_BUFLEN, path_validator, 0 }, 7865331Samw }; 7875331Samw 7885772Sas200622 #define SMB_OPT_NUM \ 7895772Sas200622 (sizeof (smb_proto_options) / sizeof (smb_proto_options[0])) 7905772Sas200622 7915331Samw /* 7925331Samw * Check the range of value as int range. 7935331Samw */ 7945331Samw static int 7955331Samw range_check_validator(int index, char *value) 7965331Samw { 7975331Samw int ret = SA_OK; 7985331Samw 7995331Samw if (!is_a_number(value)) { 8005331Samw ret = SA_BAD_VALUE; 8015331Samw } else { 8025331Samw int val; 8035331Samw val = strtoul(value, NULL, 0); 8045331Samw if (val < smb_proto_options[index].minval || 8055331Samw val > smb_proto_options[index].maxval) 8065331Samw ret = SA_BAD_VALUE; 8075331Samw } 8085331Samw return (ret); 8095331Samw } 8105331Samw 8115331Samw /* 8125331Samw * Check the range of value as int range. 8135331Samw */ 8145331Samw static int 8155331Samw range_check_validator_zero_ok(int index, char *value) 8165331Samw { 8175331Samw int ret = SA_OK; 8185331Samw 8195331Samw if (!is_a_number(value)) { 8205331Samw ret = SA_BAD_VALUE; 8215331Samw } else { 8225331Samw int val; 8235331Samw val = strtoul(value, NULL, 0); 8245331Samw if (val == 0) 8255331Samw ret = SA_OK; 8265331Samw else { 8275331Samw if (val < smb_proto_options[index].minval || 8285331Samw val > smb_proto_options[index].maxval) 8295331Samw ret = SA_BAD_VALUE; 8305331Samw } 8315331Samw } 8325331Samw return (ret); 8335331Samw } 8345331Samw 8355331Samw /* 8365331Samw * Check the length of the string 8375331Samw */ 8385331Samw static int 8395331Samw string_length_check_validator(int index, char *value) 8405331Samw { 8415331Samw int ret = SA_OK; 8425331Samw 8435331Samw if (value == NULL) 8445331Samw return (SA_BAD_VALUE); 8455331Samw if (strlen(value) > smb_proto_options[index].maxval) 8465331Samw ret = SA_BAD_VALUE; 8475331Samw return (ret); 8485331Samw } 8495331Samw 8505331Samw /* 8515331Samw * Check yes/no 8525331Samw */ 8535331Samw /*ARGSUSED*/ 8545331Samw static int 8555331Samw true_false_validator(int index, char *value) 8565331Samw { 8575331Samw if (value == NULL) 8585331Samw return (SA_BAD_VALUE); 8595331Samw if ((strcasecmp(value, "true") == 0) || 8605331Samw (strcasecmp(value, "false") == 0)) 8615331Samw return (SA_OK); 8625331Samw return (SA_BAD_VALUE); 8635331Samw } 8645331Samw 8655331Samw /* 8665331Samw * Check IP address. 8675331Samw */ 8685331Samw /*ARGSUSED*/ 8695331Samw static int 8705331Samw ip_address_validator_empty_ok(int index, char *value) 8715331Samw { 8725331Samw char sbytes[16]; 8735331Samw int len; 8745331Samw 8755331Samw if (value == NULL) 8765331Samw return (SA_OK); 8775331Samw len = strlen(value); 8785331Samw if (len == 0) 8795331Samw return (SA_OK); 8805331Samw if (inet_pton(AF_INET, value, (void *)sbytes) != 1) 8815331Samw return (SA_BAD_VALUE); 8825331Samw 8835331Samw return (SA_OK); 8845331Samw } 8855331Samw 8865331Samw /* 8875331Samw * Check IP address list 8885331Samw */ 8895331Samw /*ARGSUSED*/ 8905331Samw static int 8915331Samw ip_address_csv_list_validator_empty_ok(int index, char *value) 8925331Samw { 8935331Samw char sbytes[16]; 8945331Samw char *ip, *tmp, *ctx; 8955331Samw 8965331Samw if (value == NULL || *value == '\0') 8975331Samw return (SA_OK); 8985331Samw 8995331Samw if (strlen(value) > MAX_VALUE_BUFLEN) 9005331Samw return (SA_BAD_VALUE); 9015331Samw 9025331Samw if ((tmp = strdup(value)) == NULL) 9035331Samw return (SA_NO_MEMORY); 9045331Samw 9055331Samw ip = strtok_r(tmp, ",", &ctx); 9065331Samw while (ip) { 9075331Samw if (strlen(ip) == 0) { 9085331Samw free(tmp); 9095331Samw return (SA_BAD_VALUE); 9105331Samw } 9115331Samw if (*ip != 0) { 9125331Samw if (inet_pton(AF_INET, ip, 9135331Samw (void *)sbytes) != 1) { 9145331Samw free(tmp); 9155331Samw return (SA_BAD_VALUE); 9165331Samw } 9175331Samw } 9185331Samw ip = strtok_r(0, ",", &ctx); 9195331Samw } 9205331Samw 9215331Samw free(tmp); 9225331Samw return (SA_OK); 9235331Samw } 9245331Samw 9255331Samw /* 9265331Samw * Check path 9275331Samw */ 9285331Samw /*ARGSUSED*/ 9295331Samw static int 9305331Samw path_validator(int index, char *value) 9315331Samw { 9325331Samw struct stat buffer; 9335331Samw int fd, status; 9345331Samw 9355331Samw if (value == NULL) 9365331Samw return (SA_BAD_VALUE); 9375331Samw 9385331Samw fd = open(value, O_RDONLY); 9395331Samw if (fd < 0) 9405331Samw return (SA_BAD_VALUE); 9415331Samw 9425331Samw status = fstat(fd, &buffer); 9435331Samw (void) close(fd); 9445331Samw 9455331Samw if (status < 0) 9465331Samw return (SA_BAD_VALUE); 9475331Samw 9485331Samw if (buffer.st_mode & S_IFDIR) 9495331Samw return (SA_OK); 9505331Samw return (SA_BAD_VALUE); 9515331Samw } 9525331Samw 9535331Samw /* 9545331Samw * the protoset holds the defined options so we don't have to read 9555331Samw * them multiple times 9565331Samw */ 9575331Samw static sa_protocol_properties_t protoset; 9585331Samw 9595331Samw static int 9605331Samw findprotoopt(char *name) 9615331Samw { 9625331Samw int i; 9635772Sas200622 char *sc_name; 9645772Sas200622 9655772Sas200622 for (i = 0; i < SMB_OPT_NUM; i++) { 9665772Sas200622 sc_name = smb_config_getname(smb_proto_options[i].smb_index); 9675772Sas200622 if (strcasecmp(sc_name, name) == 0) 9685331Samw return (i); 9695331Samw } 9705772Sas200622 9715331Samw return (-1); 9725331Samw } 9735331Samw 9745331Samw /* 9755331Samw * smb_load_proto_properties() 9765331Samw * 9775331Samw * read the smb config values from SMF. 9785331Samw */ 9795331Samw 9805331Samw static int 9815331Samw smb_load_proto_properties() 9825331Samw { 9835331Samw sa_property_t prop; 9845772Sas200622 char value[MAX_VALUE_BUFLEN]; 9855772Sas200622 char *name; 9865331Samw int index; 9876019Sdougm int ret = SA_OK; 9885772Sas200622 int rc; 9895331Samw 9905331Samw protoset = sa_create_protocol_properties(SMB_PROTOCOL_NAME); 9915331Samw if (protoset == NULL) 9925331Samw return (SA_NO_MEMORY); 9935331Samw 9946019Sdougm for (index = 0; index < SMB_OPT_NUM && ret == SA_OK; index++) { 9955772Sas200622 rc = smb_config_get(smb_proto_options[index].smb_index, 9965772Sas200622 value, sizeof (value)); 9975772Sas200622 if (rc != SMBD_SMF_OK) 9985772Sas200622 continue; 9995772Sas200622 name = smb_config_getname(smb_proto_options[index].smb_index); 10005772Sas200622 prop = sa_create_property(name, value); 10015454Sdougm if (prop != NULL) 10026019Sdougm ret = sa_add_protocol_property(protoset, prop); 10036019Sdougm else 10046019Sdougm ret = SA_NO_MEMORY; 10055331Samw } 10066019Sdougm return (ret); 10075331Samw } 10085331Samw 10095331Samw /* 10105331Samw * smb_share_init() 10115331Samw * 10125331Samw * Initialize the smb plugin. 10135331Samw */ 10145331Samw 10155331Samw static int 10165331Samw smb_share_init(void) 10175331Samw { 10185331Samw if (sa_plugin_ops.sa_init != smb_share_init) 10195331Samw return (SA_SYSTEM_ERR); 10205331Samw 1021*7348SJose.Borrego@Sun.COM return (smb_load_proto_properties()); 10225331Samw } 10235331Samw 10245331Samw /* 10255331Samw * smb_share_fini() 10265331Samw * 10275331Samw */ 10285331Samw static void 10295331Samw smb_share_fini(void) 10305331Samw { 10315331Samw xmlFreeNode(protoset); 10325331Samw protoset = NULL; 10335772Sas200622 10347052Samw (void) smb_share_dclose(); 10355331Samw } 10365331Samw 10375331Samw /* 10385331Samw * smb_get_proto_set() 10395331Samw * 10405331Samw * Return an optionset with all the protocol specific properties in 10415331Samw * it. 10425331Samw */ 10435331Samw static sa_protocol_properties_t 10445331Samw smb_get_proto_set(void) 10455331Samw { 10465331Samw return (protoset); 10475331Samw } 10485331Samw 10495331Samw /* 10505772Sas200622 * smb_enable_dependencies() 10515772Sas200622 * 10525772Sas200622 * SMBD_DEFAULT_INSTANCE_FMRI may have some dependencies that aren't 10535772Sas200622 * enabled. This will attempt to enable all of them. 10545772Sas200622 */ 10555772Sas200622 static void 10565772Sas200622 smb_enable_dependencies(const char *fmri) 10575772Sas200622 { 10585772Sas200622 scf_handle_t *handle; 10595772Sas200622 scf_service_t *service; 10605772Sas200622 scf_instance_t *inst = NULL; 10615772Sas200622 scf_iter_t *iter; 10625772Sas200622 scf_property_t *prop; 10635772Sas200622 scf_value_t *value; 10645772Sas200622 scf_propertygroup_t *pg; 10655772Sas200622 scf_scope_t *scope; 10665772Sas200622 char type[SCFTYPE_LEN]; 10675772Sas200622 char *dependency; 10685772Sas200622 char *servname; 10695772Sas200622 int maxlen; 10705772Sas200622 10715772Sas200622 /* 10725772Sas200622 * Get all required handles and storage. 10735772Sas200622 */ 10745772Sas200622 handle = scf_handle_create(SCF_VERSION); 10755772Sas200622 if (handle == NULL) 10765772Sas200622 return; 10775772Sas200622 10785772Sas200622 if (scf_handle_bind(handle) != 0) { 10795772Sas200622 scf_handle_destroy(handle); 10805772Sas200622 return; 10815772Sas200622 } 10825772Sas200622 10835772Sas200622 maxlen = scf_limit(SCF_LIMIT_MAX_VALUE_LENGTH); 10845772Sas200622 if (maxlen == (ssize_t)-1) 10855772Sas200622 maxlen = MAXPATHLEN; 10865772Sas200622 10875772Sas200622 dependency = malloc(maxlen); 10885772Sas200622 10895772Sas200622 service = scf_service_create(handle); 10905772Sas200622 10915772Sas200622 iter = scf_iter_create(handle); 10925772Sas200622 10935772Sas200622 pg = scf_pg_create(handle); 10945772Sas200622 10955772Sas200622 prop = scf_property_create(handle); 10965772Sas200622 10975772Sas200622 value = scf_value_create(handle); 10985772Sas200622 10995772Sas200622 scope = scf_scope_create(handle); 11005772Sas200622 11015772Sas200622 if (service == NULL || iter == NULL || pg == NULL || prop == NULL || 11025772Sas200622 value == NULL || scope == NULL || dependency == NULL) 11035772Sas200622 goto done; 11045772Sas200622 11055772Sas200622 /* 11065772Sas200622 * We passed in the FMRI for the default instance but for 11075772Sas200622 * some things we need the simple form so construct it. Since 11085772Sas200622 * we reuse the storage that dependency points to, we need to 11095772Sas200622 * use the servname early. 11105772Sas200622 */ 11115772Sas200622 (void) snprintf(dependency, maxlen, "%s", fmri + sizeof ("svc:")); 11125772Sas200622 servname = strrchr(dependency, ':'); 11135772Sas200622 if (servname == NULL) 11145772Sas200622 goto done; 11155772Sas200622 *servname = '\0'; 11165772Sas200622 servname = dependency; 11175772Sas200622 11185772Sas200622 /* 11195772Sas200622 * Setup to iterate over the service property groups, only 11205772Sas200622 * looking at those that are "dependency" types. The "entity" 11215772Sas200622 * property will have the FMRI of the service we are dependent 11225772Sas200622 * on. 11235772Sas200622 */ 11245772Sas200622 if (scf_handle_get_scope(handle, SCF_SCOPE_LOCAL, scope) != 0) 11255772Sas200622 goto done; 11265772Sas200622 11275772Sas200622 if (scf_scope_get_service(scope, servname, service) != 0) 11285772Sas200622 goto done; 11295772Sas200622 11305772Sas200622 if (scf_iter_service_pgs(iter, service) != 0) 11315772Sas200622 goto done; 11325772Sas200622 11335772Sas200622 while (scf_iter_next_pg(iter, pg) > 0) { 11345772Sas200622 char *services[2]; 11355772Sas200622 /* 11365772Sas200622 * Have a property group for the service. See if it is 11375772Sas200622 * a dependency pg and only do operations on those. 11385772Sas200622 */ 11395772Sas200622 if (scf_pg_get_type(pg, type, SCFTYPE_LEN) <= 0) 11405772Sas200622 continue; 11415772Sas200622 11425772Sas200622 if (strncmp(type, SCF_GROUP_DEPENDENCY, SCFTYPE_LEN) != 0) 11435772Sas200622 continue; 11445772Sas200622 /* 11455772Sas200622 * Have a dependency. Attempt to enable it. 11465772Sas200622 */ 11475772Sas200622 if (scf_pg_get_property(pg, SCF_PROPERTY_ENTITIES, prop) != 0) 11485772Sas200622 continue; 11495772Sas200622 11505772Sas200622 if (scf_property_get_value(prop, value) != 0) 11515772Sas200622 continue; 11525772Sas200622 11535772Sas200622 services[1] = NULL; 11545772Sas200622 11555772Sas200622 if (scf_value_get_as_string(value, dependency, maxlen) > 0) { 11565772Sas200622 services[0] = dependency; 11575772Sas200622 _check_services(services); 11585772Sas200622 } 11595772Sas200622 } 11605772Sas200622 11615772Sas200622 done: 11625772Sas200622 if (dependency != NULL) 11635772Sas200622 free(dependency); 11645772Sas200622 if (value != NULL) 11655772Sas200622 scf_value_destroy(value); 11665772Sas200622 if (prop != NULL) 11675772Sas200622 scf_property_destroy(prop); 11685772Sas200622 if (pg != NULL) 11695772Sas200622 scf_pg_destroy(pg); 11705772Sas200622 if (iter != NULL) 11715772Sas200622 scf_iter_destroy(iter); 11725772Sas200622 if (scope != NULL) 11735772Sas200622 scf_scope_destroy(scope); 11745772Sas200622 if (inst != NULL) 11755772Sas200622 scf_instance_destroy(inst); 11765772Sas200622 if (service != NULL) 11775772Sas200622 scf_service_destroy(service); 11785772Sas200622 11795772Sas200622 (void) scf_handle_unbind(handle); 11805772Sas200622 scf_handle_destroy(handle); 11815772Sas200622 } 11825772Sas200622 11835772Sas200622 /* 11845331Samw * How long to wait for service to come online 11855331Samw */ 11865331Samw #define WAIT_FOR_SERVICE 15 11875331Samw 11885331Samw /* 11895331Samw * smb_enable_service() 11905331Samw * 11915331Samw */ 11925331Samw static int 11935331Samw smb_enable_service(void) 11945331Samw { 11955331Samw int i; 11965331Samw int ret = SA_OK; 11975772Sas200622 char *service[] = { SMBD_DEFAULT_INSTANCE_FMRI, NULL }; 11985331Samw 11995331Samw if (!smb_isonline()) { 12005772Sas200622 /* 12015772Sas200622 * Attempt to start the idmap, and other dependent 12025772Sas200622 * services, first. If it fails, the SMB service will 12035772Sas200622 * ultimately fail so we use that as the error. If we 12045772Sas200622 * don't try to enable idmap, smb won't start the 12055772Sas200622 * first time unless the admin has done it 12065772Sas200622 * manually. The service could be administratively 12075772Sas200622 * disabled so we won't always get started. 12085772Sas200622 */ 12095772Sas200622 smb_enable_dependencies(SMBD_DEFAULT_INSTANCE_FMRI); 12105772Sas200622 _check_services(service); 12115331Samw 12125331Samw /* Wait for service to come online */ 12135331Samw for (i = 0; i < WAIT_FOR_SERVICE; i++) { 12145331Samw if (smb_isonline()) { 12155772Sas200622 ret = SA_OK; 12165331Samw break; 12176030Sjb150015 } else if (smb_ismaint()) { 12186030Sjb150015 /* maintenance requires help */ 12196030Sjb150015 ret = SA_SYSTEM_ERR; 12206030Sjb150015 break; 12216030Sjb150015 } else if (smb_isdisabled()) { 12226030Sjb150015 /* disabled is ok */ 12236030Sjb150015 ret = SA_OK; 12246030Sjb150015 break; 12255331Samw } else { 12266030Sjb150015 /* try another time */ 12275331Samw ret = SA_BUSY; 12285331Samw (void) sleep(1); 12295331Samw } 12305331Samw } 12315331Samw } 12325331Samw return (ret); 12335331Samw } 12345331Samw 12355331Samw /* 12365331Samw * smb_validate_proto_prop(index, name, value) 12375331Samw * 12385331Samw * Verify that the property specified by name can take the new 12395331Samw * value. This is a sanity check to prevent bad values getting into 12405331Samw * the default files. 12415331Samw */ 12425331Samw static int 12435331Samw smb_validate_proto_prop(int index, char *name, char *value) 12445331Samw { 12455331Samw if ((name == NULL) || (index < 0)) 12465331Samw return (SA_BAD_VALUE); 12475331Samw 12485331Samw if (smb_proto_options[index].validator == NULL) 12495331Samw return (SA_OK); 12505331Samw 12515331Samw if (smb_proto_options[index].validator(index, value) == SA_OK) 12525331Samw return (SA_OK); 12535331Samw return (SA_BAD_VALUE); 12545331Samw } 12555331Samw 12565331Samw /* 12575331Samw * smb_set_proto_prop(prop) 12585331Samw * 12595331Samw * check that prop is valid. 12605331Samw */ 12615331Samw /*ARGSUSED*/ 12625331Samw static int 12635331Samw smb_set_proto_prop(sa_property_t prop) 12645331Samw { 12655331Samw int ret = SA_OK; 12665331Samw char *name; 12675331Samw char *value; 12685331Samw int index = -1; 12695521Sas200622 struct smb_proto_option_defs *opt; 12705331Samw 12715331Samw name = sa_get_property_attr(prop, "type"); 12725331Samw value = sa_get_property_attr(prop, "value"); 12735331Samw if (name != NULL && value != NULL) { 12745331Samw index = findprotoopt(name); 12755331Samw if (index >= 0) { 12765331Samw /* should test for valid value */ 12775331Samw ret = smb_validate_proto_prop(index, name, value); 12785331Samw if (ret == SA_OK) { 12795521Sas200622 opt = &smb_proto_options[index]; 12805521Sas200622 12815331Samw /* Save to SMF */ 12825772Sas200622 (void) smb_config_set(opt->smb_index, value); 12835331Samw /* 12845331Samw * Specialized refresh mechanisms can 12855331Samw * be flagged in the proto_options and 12865331Samw * processed here. 12875331Samw */ 12885521Sas200622 if (opt->refresh & SMB_REFRESH_REFRESH) 12896139Sjb150015 (void) smf_refresh_instance( 12906139Sjb150015 SMBD_DEFAULT_INSTANCE_FMRI); 12915521Sas200622 else if (opt->refresh & SMB_REFRESH_RESTART) 12925331Samw (void) smf_restart_instance( 12935331Samw SMBD_DEFAULT_INSTANCE_FMRI); 12945331Samw } 12955331Samw } 12965331Samw } 12975521Sas200622 12985331Samw if (name != NULL) 12995331Samw sa_free_attr_string(name); 13005331Samw if (value != NULL) 13015331Samw sa_free_attr_string(value); 13025331Samw 13035331Samw return (ret); 13045331Samw } 13055331Samw 13065331Samw /* 13075331Samw * smb_get_status() 13085331Samw * 13095331Samw * What is the current status of the smbd? We use the SMF state here. 13105331Samw * Caller must free the returned value. 13115331Samw */ 13125331Samw 13135331Samw static char * 13145331Samw smb_get_status(void) 13155331Samw { 13165331Samw char *state = NULL; 13175331Samw state = smf_get_state(SMBD_DEFAULT_INSTANCE_FMRI); 13185331Samw return (state != NULL ? state : "-"); 13195331Samw } 13205331Samw 13215331Samw /* 13225331Samw * This protocol plugin require resource names 13235331Samw */ 13245331Samw static uint64_t 13255331Samw smb_share_features(void) 13265331Samw { 13275331Samw return (SA_FEATURE_RESOURCE | SA_FEATURE_ALLOWSUBDIRS | 13286088Sdougm SA_FEATURE_ALLOWPARDIRS | SA_FEATURE_SERVER); 13295331Samw } 13305331Samw 13315331Samw /* 1332*7348SJose.Borrego@Sun.COM * This should be used to convert smb_share_t to sa_resource_t 1333*7348SJose.Borrego@Sun.COM * Should only be needed to build transient shares/resources to be 1334*7348SJose.Borrego@Sun.COM * supplied to sharemgr to display. 13355331Samw */ 13365331Samw static int 1337*7348SJose.Borrego@Sun.COM smb_add_transient(sa_handle_t handle, smb_share_t *si) 13385331Samw { 13395331Samw int err; 13405331Samw sa_share_t share; 13415331Samw sa_group_t group; 13425331Samw sa_resource_t resource; 13435331Samw 13445331Samw if (si == NULL) 13455331Samw return (SA_INVALID_NAME); 13465331Samw 1347*7348SJose.Borrego@Sun.COM if ((share = sa_find_share(handle, si->shr_path)) == NULL) { 1348*7348SJose.Borrego@Sun.COM if ((group = smb_get_defaultgrp(handle)) == NULL) 1349*7348SJose.Borrego@Sun.COM return (SA_NO_SUCH_GROUP); 13505331Samw 13517052Samw share = sa_get_share(group, si->shr_path); 13525331Samw if (share == NULL) { 13537052Samw share = sa_add_share(group, si->shr_path, 13545331Samw SA_SHARE_TRANSIENT, &err); 13555331Samw if (share == NULL) 13565331Samw return (SA_NO_SUCH_PATH); 13575331Samw } 13585331Samw } 13595331Samw 13605331Samw /* 13615331Samw * Now handle the resource. Make sure that the resource is 13625331Samw * transient and added to the share. 13635331Samw */ 13647052Samw resource = sa_get_share_resource(share, si->shr_name); 13655331Samw if (resource == NULL) { 13665331Samw resource = sa_add_resource(share, 13677052Samw si->shr_name, SA_SHARE_TRANSIENT, &err); 13685331Samw if (resource == NULL) 13695331Samw return (SA_NO_SUCH_RESOURCE); 13705331Samw } 13715331Samw 13725331Samw /* set resource attributes now */ 13737052Samw (void) sa_set_resource_attr(resource, "description", si->shr_cmnt); 13747052Samw (void) sa_set_resource_attr(resource, SMB_SHROPT_AD_CONTAINER, 13757052Samw si->shr_container); 13765331Samw 13775331Samw return (SA_OK); 13785331Samw } 13795331Samw 13805331Samw /* 1381*7348SJose.Borrego@Sun.COM * Return smb transient shares. 13825331Samw */ 13835331Samw static int 13845331Samw smb_list_transient(sa_handle_t handle) 13855331Samw { 1386*7348SJose.Borrego@Sun.COM int i, offset; 13877052Samw smb_shrlist_t list; 13885331Samw int res; 13895331Samw 1390*7348SJose.Borrego@Sun.COM if (smb_share_count() <= 0) 13915331Samw return (SA_OK); 1392*7348SJose.Borrego@Sun.COM 13935331Samw offset = 0; 1394*7348SJose.Borrego@Sun.COM while (smb_share_list(offset, &list) == NERR_Success) { 1395*7348SJose.Borrego@Sun.COM if (list.sl_cnt == 0) 13965331Samw break; 1397*7348SJose.Borrego@Sun.COM 1398*7348SJose.Borrego@Sun.COM for (i = 0; i < list.sl_cnt; i++) { 1399*7348SJose.Borrego@Sun.COM res = smb_add_transient(handle, &(list.sl_shares[i])); 14005331Samw if (res != SA_OK) 14015331Samw return (res); 14025331Samw } 1403*7348SJose.Borrego@Sun.COM offset += list.sl_cnt; 14045331Samw } 14055331Samw 14065331Samw return (SA_OK); 14075331Samw } 14085331Samw 14095331Samw /* 14105331Samw * fix_resource_name(share, name, prefix) 14115331Samw * 14125331Samw * Construct a name where the ZFS dataset has the prefix replaced with "name". 14135331Samw */ 14145331Samw static char * 14155331Samw fix_resource_name(sa_share_t share, char *name, char *prefix) 14165331Samw { 14175331Samw char *dataset = NULL; 14185331Samw char *newname = NULL; 14195331Samw size_t psize; 14205331Samw size_t nsize; 14215331Samw 14225331Samw dataset = sa_get_share_attr(share, "dataset"); 14235331Samw 14245331Samw if (dataset != NULL && strcmp(dataset, prefix) != 0) { 14255331Samw psize = strlen(prefix); 14265331Samw if (strncmp(dataset, prefix, psize) == 0) { 14275331Samw /* need string plus ',' and NULL */ 14285331Samw nsize = (strlen(dataset) - psize) + strlen(name) + 2; 14295331Samw newname = calloc(nsize, 1); 14305331Samw if (newname != NULL) { 14315331Samw (void) snprintf(newname, nsize, "%s%s", name, 14325331Samw dataset + psize); 14335331Samw sa_fix_resource_name(newname); 14345331Samw } 14355331Samw sa_free_attr_string(dataset); 14365331Samw return (newname); 14375331Samw } 14385331Samw } 14395331Samw if (dataset != NULL) 14405331Samw sa_free_attr_string(dataset); 14415331Samw return (strdup(name)); 14425331Samw } 14435331Samw 14445331Samw /* 14455331Samw * smb_parse_optstring(group, options) 14465331Samw * 14475331Samw * parse a compact option string into individual options. This allows 14485331Samw * ZFS sharesmb and sharemgr "share" command to work. group can be a 14495331Samw * group, a share or a resource. 14505331Samw */ 14515331Samw static int 14525331Samw smb_parse_optstring(sa_group_t group, char *options) 14535331Samw { 14545331Samw char *dup; 14555331Samw char *base; 14565331Samw char *lasts; 14575331Samw char *token; 14585331Samw sa_optionset_t optionset; 14595331Samw sa_group_t parent = NULL; 14605331Samw sa_resource_t resource = NULL; 14615331Samw int iszfs = 0; 14625331Samw int persist = 0; 14635331Samw int need_optionset = 0; 14645331Samw int ret = SA_OK; 14655331Samw sa_property_t prop; 14665331Samw 14675331Samw /* 14685331Samw * In order to not attempt to change ZFS properties unless 14695331Samw * absolutely necessary, we never do it in the legacy parsing 14705331Samw * so we need to keep track of this. 14715331Samw */ 14725331Samw if (sa_is_share(group)) { 14735331Samw char *zfs; 14745331Samw 14755331Samw parent = sa_get_parent_group(group); 14765331Samw if (parent != NULL) { 14775331Samw zfs = sa_get_group_attr(parent, "zfs"); 14785331Samw if (zfs != NULL) { 14795331Samw sa_free_attr_string(zfs); 14805331Samw iszfs = 1; 14815331Samw } 14825331Samw } 14835331Samw } else { 14845331Samw iszfs = sa_group_is_zfs(group); 14855331Samw /* 14865331Samw * If a ZFS group, then we need to see if a resource 14875331Samw * name is being set. If so, bail with 14885331Samw * SA_PROP_SHARE_ONLY, so we come back in with a share 14895331Samw * instead of a group. 14905331Samw */ 14915331Samw if (strncmp(options, "name=", sizeof ("name=") - 1) == 0 || 14925331Samw strstr(options, ",name=") != NULL) { 14935331Samw return (SA_PROP_SHARE_ONLY); 14945331Samw } 14955331Samw } 14965331Samw 14975331Samw /* do we have an existing optionset? */ 14985331Samw optionset = sa_get_optionset(group, "smb"); 14995331Samw if (optionset == NULL) { 15005331Samw /* didn't find existing optionset so create one */ 15015331Samw optionset = sa_create_optionset(group, "smb"); 15025331Samw if (optionset == NULL) 15035331Samw return (SA_NO_MEMORY); 15045331Samw } else { 15055331Samw /* 15065331Samw * If an optionset already exists, we've come through 15075331Samw * twice so ignore the second time. 15085331Samw */ 15095331Samw return (ret); 15105331Samw } 15115331Samw 15125331Samw /* We need a copy of options for the next part. */ 15135331Samw dup = strdup(options); 15145331Samw if (dup == NULL) 15155331Samw return (SA_NO_MEMORY); 15165331Samw 15175331Samw /* 15185331Samw * SMB properties are straightforward and are strings, 15195331Samw * integers or booleans. Properties are separated by 15205331Samw * commas. It will be necessary to parse quotes due to some 15215331Samw * strings not having a restricted characters set. 15225331Samw * 15235331Samw * Note that names will create a resource. For now, if there 15245331Samw * is a set of properties "before" the first name="", those 15255331Samw * properties will be placed on the group. 15265331Samw */ 15275331Samw persist = sa_is_persistent(group); 15285331Samw base = dup; 15295331Samw token = dup; 15305331Samw lasts = NULL; 15315331Samw while (token != NULL && ret == SA_OK) { 15325331Samw ret = SA_OK; 15335331Samw token = strtok_r(base, ",", &lasts); 15345331Samw base = NULL; 15355331Samw if (token != NULL) { 15365331Samw char *value; 15375331Samw /* 15385331Samw * All SMB properties have values so there 15395331Samw * MUST be an '=' character. If it doesn't, 15405331Samw * it is a syntax error. 15415331Samw */ 15425331Samw value = strchr(token, '='); 15435331Samw if (value != NULL) { 15445331Samw *value++ = '\0'; 15455331Samw } else { 15465331Samw ret = SA_SYNTAX_ERR; 15475331Samw break; 15485331Samw } 15495331Samw /* 15505331Samw * We may need to handle a "name" property 15515331Samw * that is a ZFS imposed resource name. Each 15525331Samw * name would trigger getting a new "resource" 15535331Samw * to put properties on. For now, assume no 15545331Samw * "name" property for special handling. 15555331Samw */ 15565331Samw 15575331Samw if (strcmp(token, "name") == 0) { 15585331Samw char *prefix; 15595331Samw char *name = NULL; 15605331Samw /* 15615331Samw * We have a name, so now work on the 15625331Samw * resource level. We have a "share" 15635331Samw * in "group" due to the caller having 15645331Samw * added it. If we are called with a 15655331Samw * group, the check for group/share 15665331Samw * at the beginning of this function 15675331Samw * will bail out the parse if there is a 15685331Samw * "name" but no share. 15695331Samw */ 15705331Samw if (!iszfs) { 15715331Samw ret = SA_SYNTAX_ERR; 15725331Samw break; 15735331Samw } 15745331Samw /* 15755331Samw * Make sure the parent group has the 15765331Samw * "prefix" property since we will 15775331Samw * need to use this for constructing 15785331Samw * inherited name= values. 15795331Samw */ 15805331Samw prefix = sa_get_group_attr(parent, "prefix"); 15815331Samw if (prefix == NULL) { 15825331Samw prefix = sa_get_group_attr(parent, 15835331Samw "name"); 15845331Samw if (prefix != NULL) { 15855331Samw (void) sa_set_group_attr(parent, 15865331Samw "prefix", prefix); 15875331Samw } 15885331Samw } 15895331Samw name = fix_resource_name((sa_share_t)group, 15905331Samw value, prefix); 15915331Samw if (name != NULL) { 15925331Samw resource = sa_add_resource( 15935331Samw (sa_share_t)group, name, 15945331Samw SA_SHARE_TRANSIENT, &ret); 15955331Samw sa_free_attr_string(name); 15965331Samw } else { 15975331Samw ret = SA_NO_MEMORY; 15985331Samw } 15995331Samw if (prefix != NULL) 16005331Samw sa_free_attr_string(prefix); 16015331Samw 16025331Samw /* A resource level optionset is needed */ 16035331Samw 16045331Samw need_optionset = 1; 16055331Samw if (resource == NULL) { 16065331Samw ret = SA_NO_MEMORY; 16075331Samw break; 16085331Samw } 16095331Samw continue; 16105331Samw } 16115331Samw 16125331Samw if (need_optionset) { 16135331Samw optionset = sa_create_optionset(resource, 16145331Samw "smb"); 16155331Samw need_optionset = 0; 16165331Samw } 16175331Samw 16185331Samw prop = sa_create_property(token, value); 16195331Samw if (prop == NULL) 16205331Samw ret = SA_NO_MEMORY; 16215331Samw else 16225331Samw ret = sa_add_property(optionset, prop); 16235331Samw if (ret != SA_OK) 16245331Samw break; 16255331Samw if (!iszfs) 16265331Samw ret = sa_commit_properties(optionset, !persist); 16275331Samw } 16285331Samw } 16295331Samw free(dup); 16305331Samw return (ret); 16315331Samw } 16325331Samw 16335331Samw /* 16345331Samw * smb_sprint_option(rbuff, rbuffsize, incr, prop, sep) 16355331Samw * 16365331Samw * provides a mechanism to format SMB properties into legacy output 16375331Samw * format. If the buffer would overflow, it is reallocated and grown 16385331Samw * as appropriate. Special cases of converting internal form of values 16395331Samw * to those used by "share" are done. this function does one property 16405331Samw * at a time. 16415331Samw */ 16425331Samw 16435331Samw static void 16445331Samw smb_sprint_option(char **rbuff, size_t *rbuffsize, size_t incr, 16455331Samw sa_property_t prop, int sep) 16465331Samw { 16475331Samw char *name; 16485331Samw char *value; 16495331Samw int curlen; 16505331Samw char *buff = *rbuff; 16515331Samw size_t buffsize = *rbuffsize; 16525331Samw 16535331Samw name = sa_get_property_attr(prop, "type"); 16545331Samw value = sa_get_property_attr(prop, "value"); 16555331Samw if (buff != NULL) 16565331Samw curlen = strlen(buff); 16575331Samw else 16585331Samw curlen = 0; 16595331Samw if (name != NULL) { 16605331Samw int len; 16615331Samw len = strlen(name) + sep; 16625331Samw 16635331Samw /* 16645331Samw * A future RFE would be to replace this with more 16655331Samw * generic code and to possibly handle more types. 16665331Samw * 16675331Samw * For now, everything else is treated as a string. If 16685331Samw * we get any properties that aren't exactly 16695331Samw * name/value pairs, we may need to 16705331Samw * interpret/transform. 16715331Samw */ 16725331Samw if (value != NULL) 16735331Samw len += 1 + strlen(value); 16745331Samw 16755331Samw while (buffsize <= (curlen + len)) { 16765331Samw /* need more room */ 16775331Samw buffsize += incr; 16785331Samw buff = realloc(buff, buffsize); 16795331Samw *rbuff = buff; 16805331Samw *rbuffsize = buffsize; 16815331Samw if (buff == NULL) { 16825331Samw /* realloc failed so free everything */ 16835331Samw if (*rbuff != NULL) 16845331Samw free(*rbuff); 16855331Samw goto err; 16865331Samw } 16875331Samw } 16885331Samw if (buff == NULL) 16895331Samw goto err; 16905331Samw (void) snprintf(buff + curlen, buffsize - curlen, 16915331Samw "%s%s=%s", sep ? "," : "", 16925331Samw name, value != NULL ? value : "\"\""); 16935331Samw 16945331Samw } 16955331Samw err: 16965331Samw if (name != NULL) 16975331Samw sa_free_attr_string(name); 16985331Samw if (value != NULL) 16995331Samw sa_free_attr_string(value); 17005331Samw } 17015331Samw 17025331Samw /* 17035331Samw * smb_format_resource_options(resource, hier) 17045331Samw * 17055331Samw * format all the options on the group into a flattened option 17065331Samw * string. If hier is non-zero, walk up the tree to get inherited 17075331Samw * options. 17085331Samw */ 17095331Samw 17105331Samw static char * 17115331Samw smb_format_options(sa_group_t group, int hier) 17125331Samw { 17135331Samw sa_optionset_t options = NULL; 17145331Samw sa_property_t prop; 17155331Samw int sep = 0; 17165331Samw char *buff; 17175331Samw size_t buffsize; 17185331Samw 17195331Samw 17205331Samw buff = malloc(OPT_CHUNK); 17215331Samw if (buff == NULL) 17225331Samw return (NULL); 17235331Samw 17245331Samw buff[0] = '\0'; 17255331Samw buffsize = OPT_CHUNK; 17265331Samw 17275331Samw /* 17285331Samw * We may have a an optionset relative to this item. format 17295331Samw * these if we find them and then add any security definitions. 17305331Samw */ 17315331Samw 17325331Samw options = sa_get_derived_optionset(group, "smb", hier); 17335331Samw 17345331Samw /* 17355331Samw * do the default set first but skip any option that is also 17365331Samw * in the protocol specific optionset. 17375331Samw */ 17385331Samw if (options != NULL) { 17395331Samw for (prop = sa_get_property(options, NULL); 17405331Samw prop != NULL; prop = sa_get_next_property(prop)) { 17415331Samw /* 17425331Samw * use this one since we skipped any 17435331Samw * of these that were also in 17445331Samw * optdefault 17455331Samw */ 17465331Samw smb_sprint_option(&buff, &buffsize, OPT_CHUNK, 17475331Samw prop, sep); 17485331Samw if (buff == NULL) { 17495331Samw /* 17505331Samw * buff could become NULL if there 17515331Samw * isn't enough memory for 17525331Samw * smb_sprint_option to realloc() 17535331Samw * as necessary. We can't really 17545331Samw * do anything about it at this 17555331Samw * point so we return NULL. The 17565331Samw * caller should handle the 17575331Samw * failure. 17585331Samw */ 17595331Samw if (options != NULL) 17605331Samw sa_free_derived_optionset( 17615331Samw options); 17625331Samw return (buff); 17635331Samw } 17645331Samw sep = 1; 17655331Samw } 17665331Samw } 17675331Samw 17685331Samw if (options != NULL) 17695331Samw sa_free_derived_optionset(options); 17705331Samw return (buff); 17715331Samw } 17725331Samw 17735331Samw /* 17745331Samw * smb_rename_resource(resource, newname) 17755331Samw * 17765331Samw * Change the current exported name of the resource to newname. 17775331Samw */ 17785331Samw /*ARGSUSED*/ 17795331Samw int 17805331Samw smb_rename_resource(sa_handle_t handle, sa_resource_t resource, char *newname) 17815331Samw { 17825331Samw int ret = SA_OK; 17835331Samw int err; 17845331Samw char *oldname; 17855331Samw 1786*7348SJose.Borrego@Sun.COM if (!smb_isonline()) 1787*7348SJose.Borrego@Sun.COM return (SA_OK); 1788*7348SJose.Borrego@Sun.COM 17895331Samw oldname = sa_get_resource_attr(resource, "name"); 17905331Samw if (oldname == NULL) 17915331Samw return (SA_NO_SUCH_RESOURCE); 17925331Samw 1793*7348SJose.Borrego@Sun.COM err = smb_share_rename(oldname, newname); 17945331Samw 17955331Samw /* improve error values somewhat */ 17965331Samw switch (err) { 17975331Samw case NERR_Success: 17985331Samw break; 17995331Samw case NERR_InternalError: 18005331Samw ret = SA_SYSTEM_ERR; 18015331Samw break; 18025331Samw case NERR_DuplicateShare: 18035331Samw ret = SA_DUPLICATE_NAME; 18045331Samw break; 18055331Samw default: 18065331Samw ret = SA_CONFIG_ERR; 18075331Samw break; 18085331Samw } 18095331Samw 18105331Samw return (ret); 18115331Samw } 1812*7348SJose.Borrego@Sun.COM 1813*7348SJose.Borrego@Sun.COM static int 1814*7348SJose.Borrego@Sun.COM smb_build_shareinfo(sa_share_t share, sa_resource_t resource, smb_share_t *si) 1815*7348SJose.Borrego@Sun.COM { 1816*7348SJose.Borrego@Sun.COM sa_property_t prop; 1817*7348SJose.Borrego@Sun.COM sa_optionset_t opts; 1818*7348SJose.Borrego@Sun.COM char *path; 1819*7348SJose.Borrego@Sun.COM char *rname; 1820*7348SJose.Borrego@Sun.COM char *val = NULL; 1821*7348SJose.Borrego@Sun.COM 1822*7348SJose.Borrego@Sun.COM bzero(si, sizeof (smb_share_t)); 1823*7348SJose.Borrego@Sun.COM 1824*7348SJose.Borrego@Sun.COM if ((path = sa_get_share_attr(share, "path")) == NULL) 1825*7348SJose.Borrego@Sun.COM return (SA_NO_SUCH_PATH); 1826*7348SJose.Borrego@Sun.COM 1827*7348SJose.Borrego@Sun.COM if ((rname = sa_get_resource_attr(resource, "name")) == NULL) { 1828*7348SJose.Borrego@Sun.COM sa_free_attr_string(path); 1829*7348SJose.Borrego@Sun.COM return (SA_NO_SUCH_RESOURCE); 1830*7348SJose.Borrego@Sun.COM } 1831*7348SJose.Borrego@Sun.COM 1832*7348SJose.Borrego@Sun.COM si->shr_flags = (sa_is_persistent(share)) 1833*7348SJose.Borrego@Sun.COM ? SMB_SHRF_PERM : SMB_SHRF_TRANS; 1834*7348SJose.Borrego@Sun.COM 1835*7348SJose.Borrego@Sun.COM (void) strlcpy(si->shr_path, path, sizeof (si->shr_path)); 1836*7348SJose.Borrego@Sun.COM (void) strlcpy(si->shr_name, rname, sizeof (si->shr_name)); 1837*7348SJose.Borrego@Sun.COM sa_free_attr_string(path); 1838*7348SJose.Borrego@Sun.COM sa_free_attr_string(rname); 1839*7348SJose.Borrego@Sun.COM 1840*7348SJose.Borrego@Sun.COM val = sa_get_resource_description(resource); 1841*7348SJose.Borrego@Sun.COM if (val == NULL) 1842*7348SJose.Borrego@Sun.COM val = sa_get_share_description(share); 1843*7348SJose.Borrego@Sun.COM 1844*7348SJose.Borrego@Sun.COM if (val != NULL) { 1845*7348SJose.Borrego@Sun.COM (void) strlcpy(si->shr_cmnt, val, sizeof (si->shr_cmnt)); 1846*7348SJose.Borrego@Sun.COM sa_free_share_description(val); 1847*7348SJose.Borrego@Sun.COM } 1848*7348SJose.Borrego@Sun.COM 1849*7348SJose.Borrego@Sun.COM opts = sa_get_derived_optionset(resource, SMB_PROTOCOL_NAME, 1); 1850*7348SJose.Borrego@Sun.COM if (opts == NULL) 1851*7348SJose.Borrego@Sun.COM return (SA_OK); 1852*7348SJose.Borrego@Sun.COM 1853*7348SJose.Borrego@Sun.COM prop = sa_get_property(opts, SMB_SHROPT_AD_CONTAINER); 1854*7348SJose.Borrego@Sun.COM if (prop != NULL) { 1855*7348SJose.Borrego@Sun.COM if ((val = sa_get_property_attr(prop, "value")) != NULL) { 1856*7348SJose.Borrego@Sun.COM (void) strlcpy(si->shr_container, val, 1857*7348SJose.Borrego@Sun.COM sizeof (si->shr_container)); 1858*7348SJose.Borrego@Sun.COM free(val); 1859*7348SJose.Borrego@Sun.COM } 1860*7348SJose.Borrego@Sun.COM } 1861*7348SJose.Borrego@Sun.COM 1862*7348SJose.Borrego@Sun.COM sa_free_derived_optionset(opts); 1863*7348SJose.Borrego@Sun.COM return (SA_OK); 1864*7348SJose.Borrego@Sun.COM } 1865*7348SJose.Borrego@Sun.COM 1866*7348SJose.Borrego@Sun.COM /* 1867*7348SJose.Borrego@Sun.COM * smb_get_defaultgrp 1868*7348SJose.Borrego@Sun.COM * 1869*7348SJose.Borrego@Sun.COM * If default group for CIFS shares (i.e. "smb") exists 1870*7348SJose.Borrego@Sun.COM * then it will return the group handle, otherwise it will 1871*7348SJose.Borrego@Sun.COM * create the group and return the handle. 1872*7348SJose.Borrego@Sun.COM * 1873*7348SJose.Borrego@Sun.COM * All the shares created by CIFS clients (this is only possible 1874*7348SJose.Borrego@Sun.COM * via RPC) will be added to "smb" groups. 1875*7348SJose.Borrego@Sun.COM */ 1876*7348SJose.Borrego@Sun.COM static sa_group_t 1877*7348SJose.Borrego@Sun.COM smb_get_defaultgrp(sa_handle_t handle) 1878*7348SJose.Borrego@Sun.COM { 1879*7348SJose.Borrego@Sun.COM sa_group_t group = NULL; 1880*7348SJose.Borrego@Sun.COM int err; 1881*7348SJose.Borrego@Sun.COM 1882*7348SJose.Borrego@Sun.COM group = sa_get_group(handle, SMB_DEFAULT_SHARE_GROUP); 1883*7348SJose.Borrego@Sun.COM if (group != NULL) 1884*7348SJose.Borrego@Sun.COM return (group); 1885*7348SJose.Borrego@Sun.COM 1886*7348SJose.Borrego@Sun.COM group = sa_create_group(handle, SMB_DEFAULT_SHARE_GROUP, &err); 1887*7348SJose.Borrego@Sun.COM if (group == NULL) 1888*7348SJose.Borrego@Sun.COM return (NULL); 1889*7348SJose.Borrego@Sun.COM 1890*7348SJose.Borrego@Sun.COM if (sa_create_optionset(group, SMB_DEFAULT_SHARE_GROUP) == NULL) { 1891*7348SJose.Borrego@Sun.COM (void) sa_remove_group(group); 1892*7348SJose.Borrego@Sun.COM group = NULL; 1893*7348SJose.Borrego@Sun.COM } 1894*7348SJose.Borrego@Sun.COM 1895*7348SJose.Borrego@Sun.COM return (group); 1896*7348SJose.Borrego@Sun.COM } 1897