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 "nfsprov_methods.h"
30 #include "util.h"
31 #include "libfsmgt.h"
32 #include "util.h"
33 #include "cmdgen.h"
34 #include "nfs_providers_msgstrings.h"
35 #include "messageStrings.h"
36 #include "nfs_provider_names.h"
37 #include <sys/types.h>
38 
39 CCIMProperty *exec_command(char *cmd);
40 
41 /*
42  * Public methods
43  */
44 
45 /*
46  * Method: create_outParams_list
47  *
48  * Description: Creates a string or a string array property to be added to the
49  * passed in CCIMPropertyList*, outParams.
50  *
51  * Parameters:
52  *	- CCIMPropertyList *outParams - The property list to add the string
53  *	array property to.
54  *	- char **list - The string array to add to outParams.
55  *	- int num_elements - The number of elements in list.
56  *	- char *single_value - The string to add to outParams.
57  *
58  * Returns:
59  *	- Nothing
60  */
61 void
create_outParams_list(CCIMPropertyList * outParams,char ** list,int num_elements,char * single_value)62 create_outParams_list(CCIMPropertyList *outParams, char **list,
63 	int num_elements, char *single_value) {
64 
65 	CCIMProperty		*prop = NULL;
66 	CCIMException		*ex;
67 	cimchar			*outParamValues;
68 	int			err = 0;
69 
70 	if (list != NULL) {
71 		/*
72 		 * cim_encodeStringArray converts an array or strings into a
73 		 * regular string for placement into the CCIMProperty.
74 		 */
75 		outParamValues = cim_encodeStringArray(list, num_elements);
76 		if (outParamValues == NULL) {
77 			ex = cim_getLastError();
78 			util_handleError(CREATE_OUT_PARAMS, CIM_ERR_FAILED,
79 				ENCODE_STRING_ARRAY_FAILURE, ex, &err);
80 			outParams = NULL;
81 			return;
82 		}
83 		prop = cim_createProperty("outParams", string_array,
84 			outParamValues, NULL, cim_false);
85 	} else if (single_value != NULL) {
86 		prop = cim_createProperty("outParams", string, single_value,
87 			NULL, cim_false);
88 	}
89 
90 	if (prop == NULL) {
91 		ex = cim_getLastError();
92 		util_handleError(CREATE_OUT_PARAMS, CIM_ERR_FAILED,
93 			CREATE_PROPERTY_FAILURE, ex, &err);
94 		outParams = NULL;
95 		free(outParamValues);
96 		return;
97 	}
98 
99 	outParams = cim_addPropertyToPropertyList(outParams, prop);
100 	if (outParams == NULL) {
101 		ex = cim_getLastError();
102 		util_handleError(CREATE_OUT_PARAMS, CIM_ERR_FAILED,
103 			ADD_PROP_TO_PROPLIST_FAILURE, ex, &err);
104 		return;
105 	}
106 
107 } /* create_outParams_list */
108 
109 /*
110  * Method: del_all_with_duplicate_path
111  *
112  * Description: Deletes all /etc/dfs/dfstab entries having the same path as
113  * defined with the passed in parameter list.
114  *
115  * Parameters:
116  *	- CCIMPropertyList *inParams - The input parameter list containing
117  *	the path of the /etc/dfs/dfstab entries to delete.
118  *
119  * Returns:
120  *	- CCIMProperty * - A property defining the success or failure of the
121  *	method.
122  */
123 CCIMProperty *
del_all_with_duplicate_path(CCIMPropertyList * inParams)124 del_all_with_duplicate_path(CCIMPropertyList *inParams) {
125 	int		err = 0;
126 	CCIMProperty	*pathProp;
127 	char		*path;
128 
129 	if (inParams == NULL) {
130 		util_handleError(DELETE_DUP_PATHS, CIM_ERR_INVALID_PARAMETER,
131 			NULL, NULL, &err);
132 		return ((CCIMProperty *)NULL);
133 	}
134 
135 	pathProp = inParams->mDataObject;
136 	if (pathProp == NULL) {
137 		util_handleError(DELETE_DUP_PATHS, CIM_ERR_INVALID_PARAMETER,
138 			DEL_DUPLICATE_PATHS_FAILURE, NULL, &err);
139 		return ((CCIMProperty *)NULL);
140 	}
141 
142 	path = pathProp->mValue;
143 
144 	if (fs_del_All_DFStab_ents_with_Path(path, &err) == NULL) {
145 		if (err != 0) {
146 			util_handleError(DELETE_DUP_PATHS, CIM_ERR_FAILED,
147 				DEL_DUPLICATE_PATHS_FAILURE, NULL, &err);
148 			return ((CCIMProperty *)NULL);
149 		}
150 	}
151 	return (cim_createProperty("Status", sint32, "0", NULL, cim_false));
152 }
153 
154 /*
155  * Method: get_default_secmode
156  *
157  * Description: Retrieves the default security mode for the system and places
158  * it in the passed in outParams property list.
159  *
160  * Parameters:
161  *	- CCIMPropertyList *outParams - The property list for which to add the
162  *	security mode property.
163  *
164  * Returns:
165  *	- CCIMProperty * - A property defining the success or failure of the
166  *	method.
167  */
168 CCIMProperty *
get_default_secmode(CCIMPropertyList * outParams)169 get_default_secmode(CCIMPropertyList *outParams) {
170 	char    *defmode;
171 	int	err = 0;
172 
173 	defmode = nfssec_get_default_secmode(&err);
174 	if (defmode == NULL) {
175 		util_handleError(GET_DEF_SECMODE, CIM_ERR_FAILED,
176 			GET_DEFAULT_SECMODE_FAILURE, NULL, &err);
177 		return ((CCIMProperty *)NULL);
178 	}
179 
180 	create_outParams_list(outParams, NULL, NULL, defmode);
181 	if (outParams == NULL) {
182 		/*
183 		 * An error occurred in create_outParams_list.
184 		 */
185 		free(defmode);
186 		return ((CCIMProperty *)NULL);
187 	}
188 
189 	free(defmode);
190 	return (cim_createProperty("Status", sint32, "0", NULL, cim_false));
191 } /* get_default_secmode */
192 
193 /*
194  * Method: get_netconfig_list
195  *
196  * Description: Retrieves the network id list from /etc/netconfig and places
197  * it in the passed in outParams property list.
198  *
199  * Parameters:
200  *	- CCIMPropertyList *outParams - The property list for which to add the
201  *	network id list.
202  *
203  * Returns:
204  *	- CCIMProperty * - A property defining the success or failure of the
205  *	method.
206  */
207 CCIMProperty *
get_netconfig_list(CCIMPropertyList * outParams)208 get_netconfig_list(CCIMPropertyList *outParams) {
209 	char    **netid_list;
210 	int	num_elements = 0;
211 	int	err = 0;
212 
213 	netid_list = netcfg_get_networkid_list(&num_elements, &err);
214 	if (netid_list == NULL) {
215 		util_handleError(GET_NETCFG_LIST, CIM_ERR_FAILED,
216 			GET_NETID_LIST_FAILURE, NULL, &err);
217 		return ((CCIMProperty *)NULL);
218 	}
219 
220 	create_outParams_list(outParams, netid_list, num_elements, NULL);
221 	netcfg_free_networkid_list(netid_list, num_elements);
222 	if (outParams == NULL) {
223 		/*
224 		 * An error occurred in create_outParams_list.  It was
225 		 * handled in that function so just return NULL.
226 		 */
227 		return ((CCIMProperty *)NULL);
228 	}
229 
230 	return (cim_createProperty("Status", sint32, "0", NULL,
231 		cim_false));
232 } /* get_netconfig_list */
233 
234 /*
235  * Method: get_nfssec_list
236  *
237  * Description: Retrieves the list of nfs security modes from /etc/nfssec.conf
238  * and places it in the passed in outParams property list.
239  *
240  * Parameters:
241  *	- CCIMPropertyList *outParams - The property list for which to add the
242  *	nfs security modes list.
243  *
244  * Returns:
245  *	- CCIMProperty * - A property defining the success or failure of the
246  *	method.
247  */
248 CCIMProperty *
get_nfssec_list(CCIMPropertyList * outParams)249 get_nfssec_list(CCIMPropertyList *outParams) {
250 	char    **secmode_list;
251 	int	num_elements = 0;
252 	int	err = 0;
253 
254 	secmode_list = nfssec_get_nfs_secmode_list(&num_elements, &err);
255 	if (secmode_list == NULL) {
256 		util_handleError(GET_NFSSEC_LIST, CIM_ERR_FAILED,
257 			GET_SECMODE_LIST_FAILURE, NULL, &err);
258 		return ((CCIMProperty *)NULL);
259 	}
260 
261 	create_outParams_list(outParams, secmode_list, num_elements, NULL);
262 	if (outParams == NULL) {
263 		/*
264 		 * An error occurred in create_outParams_list.
265 		 */
266 		nfssec_free_secmode_list(secmode_list, num_elements);
267 		return ((CCIMProperty *)NULL);
268 	}
269 
270 	nfssec_free_secmode_list(secmode_list, num_elements);
271 
272 	return (cim_createProperty("Status", sint32, "0", NULL, cim_false));
273 
274 } /* get_nfssec_list */
275 
276 /*
277  * Method: mountall
278  *
279  * Description: Executes the mountall command with the options given in the
280  * inParams property list.
281  *
282  * Parameters:
283  *	- CCIMPropertyList *inParams - The property list containing the options
284  *	to be used when executing the mountall command.
285  *
286  * Returns:
287  *	- CCIMProperty * - A property defining the success or failure of the
288  *	method.
289  */
290 CCIMProperty *
mountall(CCIMPropertyList * inParams)291 mountall(CCIMPropertyList *inParams) {
292 	CCIMProperty	*retVal;
293 	char		*cmd = NULL;
294 	int		err = 0;
295 
296 	cmd = cmdgen_generate_command(CMDGEN_MOUNTALL, NULL, NULL, inParams,
297 		&err);
298 	if (cmd == NULL || err != 0) {
299 		cim_logDebug("mountall", "cmdgen_generate_command failed.");
300 		util_handleError(MOUNTALL_INVOKE_METH, CIM_ERR_FAILED,
301 			CMDGEN_GEN_CMD_FAILURE, NULL, &err);
302 		return ((CCIMProperty *)NULL);
303 	}
304 
305 	cim_logDebug("mountall", "Command generated is: %s", cmd);
306 
307 	retVal = exec_command(cmd);
308 	free(cmd);
309 	return (retVal);
310 } /* mountall */
311 
312 /*
313  * Method: shareall
314  *
315  * Description: Executes the shareall command with the options given in the
316  * inParams property list.
317  *
318  * Parameters:
319  *	- CCIMPropertyList *inParams - The property list containing the options
320  *	to be used when executing the shareall command.
321  *
322  * Returns:
323  *	- CCIMProperty * - A property defining the success or failure of the
324  *	method.
325  */
326 CCIMProperty *
shareall(CCIMPropertyList * inParams)327 shareall(CCIMPropertyList *inParams) {
328 	CCIMProperty	*retVal;
329 	char		*cmd = NULL;
330 	int		err = 0;
331 
332 	cmd = cmdgen_generate_command(CMDGEN_SHAREALL, NULL, NULL, inParams,
333 		&err);
334 	if (cmd == NULL || err != 0) {
335 		util_handleError(SHAREALL_INVOKE_METH, CIM_ERR_FAILED,
336 			CMDGEN_GEN_CMD_FAILURE, NULL, &err);
337 		return ((CCIMProperty *)NULL);
338 	}
339 
340 	cim_logDebug("shareall", "Command returned: %s", cmd);
341 
342 	retVal = exec_command(cmd);
343 	free(cmd);
344 	return (retVal);
345 } /* shareall */
346 
347 /*
348  * Method: unmountall
349  *
350  * Description: Executes the umountall command with the options given in the
351  * inParams property list.
352  *
353  * Parameters:
354  *	- CCIMPropertyList *inParams - The property list containing the options
355  *	to be used when executing the umountall command.
356  *
357  * Returns:
358  *	- CCIMProperty * - A property defining the success or failure of the
359  *	method.
360  */
361 CCIMProperty *
unmountall(CCIMPropertyList * inParams)362 unmountall(CCIMPropertyList *inParams) {
363 	CCIMProperty	*retVal;
364 	char		*cmd = NULL;
365 	int		err = 0;
366 
367 	cmd = cmdgen_generate_command(CMDGEN_UMOUNTALL, NULL, NULL, inParams,
368 		&err);
369 	if (cmd == NULL || err != 0) {
370 		util_handleError(UNMOUNTALL_INVOKE_METH, CIM_ERR_FAILED,
371 			CMDGEN_GEN_CMD_FAILURE, NULL, &err);
372 		return ((CCIMProperty *)NULL);
373 	}
374 
375 	cim_logDebug("unmountall", "Command returned: %s", cmd);
376 
377 	retVal = exec_command(cmd);
378 	free(cmd);
379 	return (retVal);
380 } /* unmountall */
381 
382 /*
383  * Method: unshareall
384  *
385  * Description: Executes the unshareall command with the options given in the
386  * inParams property list.
387  *
388  * Parameters:
389  *	- CCIMPropertyList *inParams - The property list containing the options
390  *	to be used when executing the unshareall command.
391  *
392  * Returns:
393  *	- CCIMProperty * - A property defining the success or failure of the
394  *	method.
395  */
396 CCIMProperty *
unshareall(CCIMPropertyList * inParams)397 unshareall(CCIMPropertyList *inParams) {
398 	CCIMProperty	*retVal;
399 	char		*cmd = NULL;
400 	int		err = 0;
401 
402 	cmd = cmdgen_generate_command(CMDGEN_UNSHAREALL, NULL, NULL, inParams,
403 		&err);
404 	if (cmd == NULL || err != 0) {
405 		util_handleError(UNSHAREALL_INVOKE_METH, CIM_ERR_FAILED,
406 			CMDGEN_GEN_CMD_FAILURE, NULL, &err);
407 		return ((CCIMProperty *)NULL);
408 	}
409 
410 	cim_logDebug("unshareall", "Command returned: %s", cmd);
411 
412 	retVal = exec_command(cmd);
413 	free(cmd);
414 	return (retVal);
415 } /* unshareall */
416 
417 /*
418  * Private Methods
419  */
420 
421 /*
422  * Method: exec_command
423  *
424  * Description: Executes the given command, returns a success/failure property
425  * and handles errors from the execution of the command if needed.
426  *
427  * Parameters:
428  *	- char *cmd - The command to execute.
429  *
430  * Returns:
431  *	- CCIMProperty * - A property defining the success or failure of the
432  *	method.
433  *	- NULL if an error occurred.
434  */
435 CCIMProperty *
exec_command(char * cmd)436 exec_command(char *cmd) {
437 	char	*cmd_return = NULL;
438 	int	err = 0;
439 
440 	cmd_return = cmd_execute_command_and_retrieve_string(cmd, &err);
441 	if (err != 0) {
442 		if (cmd_return != NULL) {
443 			util_handleError(EXEC_CMD, CIM_ERR_FAILED,
444 				cmd_return, NULL, &err);
445 			free(cmd_return);
446 			return ((CCIMProperty *)NULL);
447 		} else {
448 			util_handleError(EXEC_CMD, CIM_ERR_FAILED,
449 				CMD_EXEC_RETR_STR_FAILURE, NULL, &err);
450 			return ((CCIMProperty *)NULL);
451 		}
452 	}
453 
454 	if (cmd_return != NULL) {
455 		cim_logDebug("exec_command", "Exec command return =%s",
456 			cmd_return);
457 		free(cmd_return);
458 	}
459 
460 	return (cim_createProperty("Status", sint32, "0", NULL, cim_false));
461 } /* exec_command */
462