1 /*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
7 * with the License.
8 *
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
13 *
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
19 *
20 * CDDL HEADER END
21 */
22 /*
23 * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
25 */
26
27 #pragma ident "%Z%%M% %I% %E% SMI"
28
29 #include <sys/types.h>
30 #include <string.h>
31 #include "createprop_methods.h"
32 #include "messageStrings.h"
33 #include "nfs_providers_msgstrings.h"
34 #include "nfs_provider_names.h"
35 #include "nfs_keys.h"
36 #include "libfsmgt.h"
37 #include "util.h"
38
39 #define MAXSIZE 256
40
41 /*
42 * Method: add_property_to_instance
43 *
44 * Description: Creates a property corresponding to the input parameters and
45 * adds it to the instance passed in.
46 *
47 * Parameters:
48 * - cimchar *pName - The name of the property to be created.
49 * - CIMType pType - The type of the property.
50 * - cimchar *pValue - The value of the property if it is NOT to be a
51 * reference property.
52 * - CCIMObjectPath *pOP - The value of the property if it is to be a
53 * reference property.
54 * - CIMBool pIsKey - A boolean value representing whether or not the
55 * property is a key.
56 * - CCIMInstance *pInst - The instance that the property is added to.
57 *
58 * Returns:
59 * - A CIMBool which is true if the property was added to the instance and
60 * false if it was not.
61 */
62 CIMBool
add_property_to_instance(cimchar * pName,CIMType pType,cimchar * pValue,CCIMObjectPath * pOP,CIMBool pIsKey,CCIMInstance * pInst)63 add_property_to_instance(cimchar *pName, CIMType pType, cimchar *pValue,
64 CCIMObjectPath *pOP, CIMBool pIsKey, CCIMInstance *pInst) {
65
66 CCIMProperty *prop;
67 CCIMException *ex;
68 int err = 0;
69
70 if (pOP == NULL) {
71 prop = cim_createProperty(pName, pType, pValue, NULL, pIsKey);
72 } else {
73 prop = cim_createReferenceProperty(pName, pOP, pIsKey);
74 }
75
76 if (prop == NULL) {
77 ex = cim_getLastError();
78 util_handleError(ADD_PROP_TO_INST, CIM_ERR_FAILED,
79 CREATE_PROPERTY_FAILURE, ex, &err);
80 return (cim_false);
81 }
82
83 if (cim_addProperty(pInst, prop) == cim_false) {
84 ex = cim_getLastError();
85 util_handleError(ADD_PROP_TO_INST, CIM_ERR_FAILED,
86 ADD_PROPERTY_FAILURE, ex, &err);
87 cim_freeProperty(prop);
88 return (cim_false);
89 }
90
91 return (cim_true);
92 } /* add_property_to_instance */
93
94 /*
95 * Method: add_property_to_list
96 *
97 * Description: Creates a property corresponding to the input parameters and
98 * adds it to the property list passed in.
99 *
100 * Parameters:
101 * - cimchar *pName - The name of the property to be created.
102 * - CIMType pType - The type of the property.
103 * - cimchar *pValue - The value of the property if it is NOT to be a
104 * reference property.
105 * - CCIMObjectPath *pOP - The value of the property if it is to be a
106 * reference property.
107 * - CIMBool pIsKey - A boolean value representing whether or not the
108 * property is a key.
109 * - CCIMPropertyList *pPropList - The property list that the property is
110 * added to.
111 *
112 * Returns:
113 * - A pointer to the property list that the property was added to.
114 * - NULL if an error occurred.
115 *
116 * NOTE: Upon error, the passed in CCIMPropertyList*, pPropList, is freed.
117 * Since this is a wrapper for the cim_addPropertyToPropertyList function
118 * this is done to be consistent with the way that the CIM C API works.
119 * Upon error, the CCIMPropertyList passed into cim_addPropertyToPropertyList
120 * is freed.
121 */
122 CCIMPropertyList *
add_property_to_list(cimchar * pName,CIMType pType,cimchar * pValue,CCIMObjectPath * pOP,CIMBool pIsKey,CCIMPropertyList * pPropList)123 add_property_to_list(cimchar *pName, CIMType pType, cimchar *pValue,
124 CCIMObjectPath *pOP, CIMBool pIsKey, CCIMPropertyList *pPropList) {
125
126 CCIMProperty *prop;
127 CCIMException *ex;
128 int err = 0;
129
130 if (pOP == NULL) {
131 prop = cim_createProperty(pName, pType, pValue, NULL, pIsKey);
132 } else {
133 prop = cim_createReferenceProperty(pName, pOP, pIsKey);
134 }
135
136 /*
137 * If NULL, an error was encountered.
138 */
139 if (prop == NULL) {
140 ex = cim_getLastError();
141 util_handleError(ADD_PROP_TO_LIST, CIM_ERR_FAILED,
142 CREATE_PROPERTY_FAILURE, ex, &err);
143 cim_freePropertyList(pPropList);
144 return ((CCIMPropertyList *)NULL);
145 }
146
147 pPropList = cim_addPropertyToPropertyList(pPropList, prop);
148 if (pPropList == NULL) {
149 ex = cim_getLastError();
150 util_handleError(ADD_PROP_TO_LIST, CIM_ERR_FAILED,
151 ADD_PROP_TO_PROPLIST_FAILURE, ex, &err);
152 cim_freeProperty(prop);
153 return ((CCIMPropertyList *)NULL);
154 }
155
156 /*
157 * Debugging...
158 */
159 if (pValue != NULL) {
160 cim_logDebug("add_property_to_list", "Adding %s, value %s",
161 pName, pValue);
162 }
163
164 return (pPropList);
165 } /* add_property_to_list */
166
167 /*
168 * Method: get_property_from_opt_string
169 *
170 * Description: Determines if a property exists in the mount option string and
171 * returns a value of the property to be used in creating a CCIMProperty.
172 *
173 * Parameters:
174 * - char *mntopts - The mount option string to search.
175 * - char *option - The option to search for.
176 * - boolean_t optHasEquals - A boolean telling the method whether or not
177 * the option being searched for contains the "=" character.
178 * - int defaultValue - The value of the property if it is not found in
179 * the option string.
180 *
181 * Returns:
182 * - The string value of the property found.
183 * - NULL if an error occurred.
184 *
185 * NOTE: The caller must free space allocated for return value.
186 */
187 cimchar *
get_property_from_opt_string(char * mntopts,char * option,boolean_t optHasEquals,int defaultValue)188 get_property_from_opt_string(char *mntopts, char *option,
189 boolean_t optHasEquals, int defaultValue) {
190
191 cimchar propValue[MAXSIZE];
192 cimchar *retVal;
193 char *optionString = NULL;
194 char *optionFound;
195 int err = 0;
196
197 optionString = strdup(mntopts);
198 if (optionString == NULL) {
199 util_handleError(GET_PROP_FROM_OPTS,
200 CIM_ERR_LOW_ON_MEMORY, LOW_MEMORY, NULL, &err);
201 return (NULL);
202 } else {
203 optionFound = fs_parse_optlist_for_option(optionString,
204 option, &err);
205 /*
206 * Was the option found in the option string?
207 * If it was, propValue = true or propValue = optionFound.
208 */
209 if (optionFound != NULL) {
210 if (optHasEquals) {
211 (void) snprintf(propValue, MAXSIZE, "%s",
212 optionFound);
213 free(optionFound);
214 } else {
215 (void) snprintf(propValue, MAXSIZE, "%d",
216 B_TRUE);
217 free(optionFound);
218 }
219 } else {
220 /*
221 * Since the option was not found we know that
222 * propValue = false or the default value.
223 */
224 if (optHasEquals) {
225 (void) snprintf(propValue, MAXSIZE, "%d",
226 defaultValue);
227 } else {
228 (void) snprintf(propValue, MAXSIZE, "%d",
229 B_FALSE);
230 }
231 }
232 }
233
234 retVal = strdup(propValue);
235 if (retVal == NULL) {
236 util_handleError(GET_PROP_FROM_OPTS,
237 CIM_ERR_LOW_ON_MEMORY, LOW_MEMORY, NULL, &err);
238 return (NULL);
239 }
240
241 free(optionString);
242 return (retVal);
243 } /* get_property_from_opt_string */
244
245 /*
246 * Method set_dir_keyProperties_to_true
247 *
248 * Helper function to work around cimom bug 4649100 which causes
249 * cimom_getInstance to return the instance with the value of
250 * keyProperty set to cim_false instead of cim_true.
251 */
252 CIMBool
set_dir_keyProperties_to_true(CCIMInstance * dirInst)253 set_dir_keyProperties_to_true(CCIMInstance *dirInst) {
254 CCIMProperty *tempProp;
255 CCIMPropertyList *tempPList;
256 CIMBool return_value = cim_false;
257
258 for (tempPList = dirInst->mProperties; tempPList != NULL;
259 tempPList = tempPList->mNext) {
260
261 tempProp = tempPList->mDataObject;
262 if (strcmp(tempProp->mName, CREATION_CLASS) == 0) {
263 tempProp->mKeyProperty = cim_true;
264 return_value = cim_true;
265 } else if (strcmp(tempProp->mName, NAME) == 0) {
266 tempProp->mKeyProperty = cim_true;
267 return_value = cim_true;
268 } else if (strcmp(tempProp->mName, CS_CREATION_CLASS)
269 == 0) {
270 tempProp->mKeyProperty = cim_true;
271 return_value = cim_true;
272 } else if (strcmp(tempProp->mName, CSNAME) == 0) {
273 tempProp->mKeyProperty = cim_true;
274 return_value = cim_true;
275 } else if (strcmp(tempProp->mName, FS_CREATION_CLASS) == 0) {
276 tempProp->mKeyProperty = cim_true;
277 return_value = cim_true;
278 } else if (strcmp(tempProp->mName, FSNAME) == 0) {
279 tempProp->mKeyProperty = cim_true;
280 return_value = cim_true;
281 }
282 }
283 return (return_value);
284 } /* set_dir_keyProperties_to_true */
285
286 /*
287 * Method set_share_keyProperties_to_true
288 *
289 * Helper function to work around cimom bug 4649100 which causes
290 * cimom_getInstance to return the instance with the value of
291 * keyProperty set to cim_false instead of cim_true.
292 */
293 CIMBool
set_share_keyProperties_to_true(CCIMInstance * nfsShareInst)294 set_share_keyProperties_to_true(CCIMInstance *nfsShareInst) {
295 CCIMProperty *tempProp;
296 CCIMPropertyList *tempPList;
297 CIMBool return_value = cim_false;
298
299 for (tempPList = nfsShareInst->mProperties; tempPList != NULL;
300 tempPList = tempPList->mNext) {
301
302 tempProp = tempPList->mDataObject;
303 if (strcmp(tempProp->mName, CREATION_CLASS) == 0) {
304 tempProp->mKeyProperty = cim_true;
305 return_value = cim_true;
306 } else if (strcmp(tempProp->mName, NAME) == 0) {
307 tempProp->mKeyProperty = cim_true;
308 return_value = cim_true;
309 } else if (strcmp(tempProp->mName, SYS_CREATION_CLASS)
310 == 0) {
311 tempProp->mKeyProperty = cim_true;
312 return_value = cim_true;
313 } else if (strcmp(tempProp->mName, SYSTEM) == 0) {
314 tempProp->mKeyProperty = cim_true;
315 return_value = cim_true;
316 }
317 }
318 return (return_value);
319 } /* set_share_keyProperties_to_true */
320
321 /*
322 * Method set_shareSec_keyProperties_to_true
323 *
324 * Helper function to work around cimom bug 4649100 which causes
325 * cimom_getInstance to return the instance with the value of
326 * keyProperty set to cim_false instead of cim_true.
327 */
328 CIMBool
set_shareSec_keyProperties_to_true(CCIMInstance * nfsShareSecInst)329 set_shareSec_keyProperties_to_true(CCIMInstance *nfsShareSecInst) {
330 CCIMProperty *tempProp;
331 CCIMPropertyList *tempPList;
332 CIMBool return_value = cim_false;
333
334 for (tempPList = nfsShareSecInst->mProperties; tempPList != NULL;
335 tempPList = tempPList->mNext) {
336
337 tempProp = tempPList->mDataObject;
338 if (strcmp(tempProp->mName, MODE) == 0) {
339 tempProp->mKeyProperty = cim_true;
340 return_value = cim_true;
341 } else if (strcmp(tempProp->mName, SETTING_ID_LOWCASE) == 0) {
342 tempProp->mKeyProperty = cim_true;
343 return_value = cim_true;
344 }
345 }
346 return (return_value);
347 } /* set_shareSec_keyProperties_to_true */
348