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 "mountprov_methods.h"
30 #include "nfsprov_methods.h"
31 #include "util.h"
32 #include "libfsmgt.h"
33 #include "nfs_providers_msgstrings.h"
34 #include "nfs_provider_names.h"
35 #include <errno.h>
36 
37 #define	SHOWEXPORTS "/usr/sbin/showmount -e "
38 
39 /*
40  * Private methods
41  */
42 static char **create_export_array(char *exportList_in_string_form,
43 	int *num_elements, int *errp);
44 
45 /*
46  * Public methods
47  */
48 /*
49  * Method: delete_vfstab_entry
50  *
51  * Description: Deletes the /etc/vfstab entry with the corresponding resource
52  * and mount point as passed in with inParams.
53  *
54  * Parameters:
55  *	- CCIMPropertyList *inParams - The input parameters to the method.
56  *	The property list is expected to contain two elements in the following
57  *	order:
58  *	1.) string resource - the resource that is listed in the device to
59  *	mount column of /etc/vfstab.  Example: /dev/dsk/c0t0d0s5
60  *	2.) string mount point
61  *
62  * Returns:
63  *	- CCIMProperty * - A value telling the success or failure of the method.
64  */
65 CCIMProperty *
delete_vfstab_entry(CCIMPropertyList * inParams)66 delete_vfstab_entry(CCIMPropertyList *inParams) {
67 	fs_mntdefaults_t	*vfstabEnts, *vfstabEntToDelete;
68 	CCIMPropertyList	*currentParam;
69 	CCIMProperty		*resourceProp;
70 	CCIMProperty		*mountPointProp;
71 	char			*resource;
72 	char			*mountPoint;
73 	int			err = 0;
74 
75 	if (inParams == NULL) {
76 		util_handleError(DELETE_VFSTAB_ENT, CIM_ERR_INVALID_PARAMETER,
77 			NULL, NULL, &err);
78 		return ((CCIMProperty *)NULL);
79 	}
80 
81 	/*
82 	 * The inParams are expected to contain two elements in this order:
83 	 * 1.) string resource
84 	 * 2.) string mountPoint
85 	 */
86 	currentParam = inParams;
87 
88 	resourceProp = currentParam->mDataObject;
89 	if (resourceProp == NULL || resourceProp->mValue == NULL) {
90 		util_handleError(DELETE_VFSTAB_ENT, CIM_ERR_INVALID_PARAMETER,
91 			NULL, NULL, &err);
92 		return ((CCIMProperty *)NULL);
93 	}
94 
95 	resource = resourceProp->mValue;
96 
97 	currentParam = currentParam->mNext;
98 
99 	mountPointProp = currentParam->mDataObject;
100 	if (mountPointProp == NULL || mountPointProp->mValue == NULL) {
101 		util_handleError(DELETE_VFSTAB_ENT, CIM_ERR_INVALID_PARAMETER,
102 			NULL, NULL, &err);
103 		return ((CCIMProperty *)NULL);
104 	}
105 
106 	mountPoint = mountPointProp->mValue;
107 
108 	vfstabEntToDelete = calloc(1, sizeof (fs_mntdefaults_t));
109 
110 	vfstabEntToDelete->resource = strdup(resource);
111 	vfstabEntToDelete->mountp = strdup(mountPoint);
112 
113 	vfstabEnts = fs_del_mount_default_ent(vfstabEntToDelete, &err);
114 	if (vfstabEnts == NULL) {
115 		util_handleError(DELETE_VFSTAB_ENT, CIM_ERR_FAILED,
116 			FS_DEL_MNT_DEFAULT_FAILURE, NULL, &err);
117 		return ((CCIMProperty *)NULL);
118 	}
119 
120 	cim_logDebug("delete_vfstab_entry", "After fs_del_mount_default_ent");
121 	fs_free_mntdefaults_list(vfstabEnts);
122 	fs_free_mntdefaults_list(vfstabEntToDelete);
123 	return (cim_createProperty("Status", sint32, "0", NULL, cim_false));
124 } /* delete_vfstab_entry */
125 
126 /*
127  * Method: show_exports
128  *
129  * Description: Shows the list of shared file systems on a certain host by
130  * executing the showmount command.
131  *
132  * Parameters:
133  *	- CCIMPropertyList *inParams - The input parameters to the method.
134  *	The property list is expected to contain one element, a string value
135  *	representing the host to show exports on.
136  *	- CCIMPropertyList *outParams - The output of the showmount command.
137  *
138  * Returns:
139  *	- CCIMProperty * - A value telling the success or failure of the method.
140  *
141  * NOTE: This is a deprecated method, but is supported until the EOL process
142  * is done.  That date is TBD.
143  */
144 CCIMProperty *
show_exports(CCIMPropertyList * inParams,CCIMPropertyList * outParams)145 show_exports(CCIMPropertyList *inParams, CCIMPropertyList *outParams) {
146 	CCIMProperty	*hostProp;
147 	char		*showExportsCommand;
148 	char		*cmd_return;
149 	char		*host;
150 	int		commandLen;
151 	int		err = 0;
152 
153 	if (inParams == NULL) {
154 		util_handleError(SHOW_EXPORTS, CIM_ERR_INVALID_PARAMETER,
155 			NULL, NULL, &err);
156 		return ((CCIMProperty *)NULL);
157 	}
158 
159 	/*
160 	 * The inParams are expected to contain one element being:
161 	 * 1.) string host
162 	 */
163 	hostProp = inParams->mDataObject;
164 	if (hostProp == NULL || hostProp->mValue == NULL) {
165 		util_handleError(SHOW_EXPORTS, CIM_ERR_INVALID_PARAMETER,
166 			NULL, NULL, &err);
167 		return ((CCIMProperty *)NULL);
168 	}
169 
170 	host = hostProp->mValue;
171 
172 	commandLen = strlen(SHOWEXPORTS) + strlen(host) + 1;
173 
174 	showExportsCommand = calloc(commandLen, sizeof (char));
175 	if (showExportsCommand == NULL) {
176 		util_handleError(SHOW_EXPORTS, CIM_ERR_LOW_ON_MEMORY,
177 			NULL, NULL, &err);
178 		return ((CCIMProperty *)NULL);
179 	}
180 
181 	(void) snprintf(showExportsCommand, commandLen, "%s%s", SHOWEXPORTS,
182 	    host);
183 	cmd_return = cmd_execute_command_and_retrieve_string(showExportsCommand,
184 		&err);
185 	if (err != 0) {
186 		cim_logDebug(SHOW_EXPORTS, "err =%d", err);
187 		outParams = NULL;
188 		if (cmd_return != NULL) {
189 			cim_logDebug(SHOW_EXPORTS, "Command return =%s",
190 				cmd_return);
191 			util_handleError(SHOW_EXPORTS, CIM_ERR_FAILED,
192 				cmd_return, NULL, &err);
193 			free(cmd_return);
194 		} else {
195 			util_handleError(SHOW_EXPORTS, CIM_ERR_FAILED,
196 				CMD_EXEC_RETR_STR_FAILURE, NULL, &err);
197 		}
198 
199 		free(showExportsCommand);
200 		return ((CCIMProperty *)NULL);
201 	}
202 
203 	if (cmd_return != NULL) {
204 		char	**export_array;
205 		int	num_elements = 0;
206 
207 		cim_logDebug("show_exports", "Output =%s", cmd_return);
208 
209 		export_array = create_export_array(cmd_return, &num_elements,
210 			&err);
211 		if (export_array == NULL) {
212 			cim_logDebug("show_exports", "export_array == NULL");
213 			if (err != 0) {
214 				util_handleError(SHOW_EXPORTS,
215 					CIM_ERR_LOW_ON_MEMORY, NULL, NULL,
216 					&err);
217 			}
218 			return ((CCIMProperty *)NULL);
219 		}
220 
221 		create_outParams_list(outParams, export_array, num_elements,
222 			NULL);
223 		fileutil_free_string_array(export_array, num_elements);
224 	}
225 
226 	free(showExportsCommand);
227 	return (cim_createProperty("Status", sint32, "0", NULL, cim_false));
228 } /* show_exports */
229 
230 
231 /*
232  * Private methods
233  */
234 
235 /*
236  * Method: create_export_array
237  *
238  * Description: Creates an array from the export list given in string form.
239  *
240  * Parameters:
241  *	- char *exportList_in_string_form - The export list from the showmount
242  *	command.
243  *	- int *num_elements - The element counter which keeps track of the
244  *	number of elements returned in the string array.
245  *	- int *errp - The error pointer which gets set upon error.
246  *
247  * Returns:
248  *	- char ** - The string array containing the individual elements from
249  *	the showmount export list.
250  *	- NULL if an error occurred.
251  */
252 static char **
create_export_array(char * exportList_in_string_form,int * num_elements,int * errp)253 create_export_array(char *exportList_in_string_form, int *num_elements,
254 	int *errp) {
255 
256 	char	*endOfLine = "\n";
257 	char	*export;
258 	char	*listCopy;
259 	char	**export_array = NULL;
260 	int	i = 0;
261 
262 
263 	listCopy = strdup(exportList_in_string_form);
264 	if (listCopy == NULL) {
265 		*errp = errno;
266 		*num_elements = 0;
267 		return (NULL);
268 	}
269 
270 	/*
271 	 * Ignore the first line.  It is a header that is always printed out
272 	 * when using showmounts -e.
273 	 */
274 	export = strtok(listCopy, endOfLine);
275 
276 	/*
277 	 * Count the number of elements to be in the array.
278 	 */
279 	*num_elements = 0;
280 	for (export = strtok(NULL, endOfLine); export != NULL;
281 		export = strtok(NULL, endOfLine)) {
282 		*num_elements = *num_elements + 1;
283 	}
284 
285 	export_array = calloc((size_t)*num_elements, (size_t)sizeof (char *));
286 	if (export_array == NULL) {
287 		*errp = errno;
288 		*num_elements = 0;
289 		free(listCopy);
290 		return (NULL);
291 	}
292 
293 	free(listCopy);
294 	listCopy = strdup(exportList_in_string_form);
295 	if (listCopy == NULL) {
296 		*errp = errno;
297 		*num_elements = 0;
298 		fileutil_free_string_array(export_array, *num_elements);
299 		return (NULL);
300 	}
301 
302 	export = strtok(listCopy, endOfLine);
303 
304 	for (i = 0; i < *num_elements; i++) {
305 
306 		export = strtok(NULL, endOfLine);
307 
308 		if (export != NULL) {
309 			export_array[i] = strdup(export);
310 			if (export_array[i] == NULL) {
311 				*errp = errno;
312 				free(listCopy);
313 				fileutil_free_string_array(export_array,
314 					*num_elements);
315 				*num_elements = 0;
316 				return (NULL);
317 			}
318 		}
319 	}
320 
321 	free(listCopy);
322 	return (export_array);
323 } /* create_export_array */
324