xref: /onnv-gate/usr/src/lib/libfsmgt/common/fs_shares.c (revision 3957:86c9dda5df37)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*3957Sth199096  * Common Development and Distribution License (the "License").
6*3957Sth199096  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
21*3957Sth199096 
220Sstevel@tonic-gate /*
23*3957Sth199096  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate /*
300Sstevel@tonic-gate  * Traverses /etc/dfs/sharetab in order to find shared file systems
310Sstevel@tonic-gate  */
320Sstevel@tonic-gate 
330Sstevel@tonic-gate #include <stdio.h>
340Sstevel@tonic-gate #include <stdlib.h>
350Sstevel@tonic-gate #include <strings.h>
360Sstevel@tonic-gate #include <errno.h>
370Sstevel@tonic-gate #include <thread.h>
380Sstevel@tonic-gate #include <synch.h>
390Sstevel@tonic-gate #include "libfsmgt.h"
40*3957Sth199096 #include <sharefs/share.h>
410Sstevel@tonic-gate #include "sharetab.h"
420Sstevel@tonic-gate 
430Sstevel@tonic-gate #define	SECMODES 5
440Sstevel@tonic-gate 
450Sstevel@tonic-gate /*
460Sstevel@tonic-gate  * Private variables
470Sstevel@tonic-gate  */
480Sstevel@tonic-gate static mutex_t	sharetab_lock = DEFAULTMUTEX;
490Sstevel@tonic-gate 
500Sstevel@tonic-gate /*
510Sstevel@tonic-gate  * Private method declarations
520Sstevel@tonic-gate  */
530Sstevel@tonic-gate fs_sharelist_t	*create_sharelist_entry(struct share *sharetab_entry,
540Sstevel@tonic-gate 					int *errp);
550Sstevel@tonic-gate 
560Sstevel@tonic-gate /*
570Sstevel@tonic-gate  * Public methods
580Sstevel@tonic-gate  */
590Sstevel@tonic-gate 
600Sstevel@tonic-gate void
fs_free_share_list(fs_sharelist_t * headp)610Sstevel@tonic-gate fs_free_share_list(fs_sharelist_t *headp)
620Sstevel@tonic-gate {
630Sstevel@tonic-gate 	fs_sharelist_t	*tmp;
640Sstevel@tonic-gate 
650Sstevel@tonic-gate 	while (headp != NULL) {
660Sstevel@tonic-gate 		tmp = headp->next;
670Sstevel@tonic-gate 		free(headp->path);
680Sstevel@tonic-gate 		free(headp->resource);
690Sstevel@tonic-gate 		free(headp->fstype);
700Sstevel@tonic-gate 		free(headp->options);
710Sstevel@tonic-gate 		free(headp->description);
720Sstevel@tonic-gate 		headp->next = NULL;
730Sstevel@tonic-gate 		free(headp);
740Sstevel@tonic-gate 
750Sstevel@tonic-gate 		headp = tmp;
760Sstevel@tonic-gate 	}
770Sstevel@tonic-gate }
780Sstevel@tonic-gate 
790Sstevel@tonic-gate /*
800Sstevel@tonic-gate  * Get a linked list of all the shares on the system from /etc/dfs/dfstab
810Sstevel@tonic-gate  */
820Sstevel@tonic-gate fs_sharelist_t *
fs_get_share_list(int * errp)830Sstevel@tonic-gate fs_get_share_list(int *errp)
840Sstevel@tonic-gate {
850Sstevel@tonic-gate 	fs_sharelist_t	*newp;
860Sstevel@tonic-gate 	fs_sharelist_t	*headp;
870Sstevel@tonic-gate 	fs_sharelist_t	*tailp;
880Sstevel@tonic-gate 	FILE		*fp;
890Sstevel@tonic-gate 
900Sstevel@tonic-gate 	headp = NULL;
910Sstevel@tonic-gate 	tailp = NULL;
920Sstevel@tonic-gate 
930Sstevel@tonic-gate 	if ((fp = fopen(SHARETAB, "r")) != NULL) {
940Sstevel@tonic-gate 		struct share	*sharetab_entry;
950Sstevel@tonic-gate 
960Sstevel@tonic-gate 		(void) mutex_lock(&sharetab_lock);
970Sstevel@tonic-gate 		while (getshare(fp, &sharetab_entry) > 0) {
980Sstevel@tonic-gate 
990Sstevel@tonic-gate 			newp = create_sharelist_entry(sharetab_entry, errp);
1000Sstevel@tonic-gate 			if (newp == NULL) {
1010Sstevel@tonic-gate 				/*
1020Sstevel@tonic-gate 				 * Out of memory
1030Sstevel@tonic-gate 				 */
1040Sstevel@tonic-gate 				fs_free_share_list(headp);
1050Sstevel@tonic-gate 				(void) mutex_unlock(&sharetab_lock);
1060Sstevel@tonic-gate 				(void) fclose(fp);
1070Sstevel@tonic-gate 				return (NULL);
1080Sstevel@tonic-gate 			}
1090Sstevel@tonic-gate 
1100Sstevel@tonic-gate 			if (headp == NULL) {
1110Sstevel@tonic-gate 				headp = newp;
1120Sstevel@tonic-gate 				tailp = newp;
1130Sstevel@tonic-gate 			} else {
1140Sstevel@tonic-gate 				tailp->next = newp;
1150Sstevel@tonic-gate 				tailp = newp;
1160Sstevel@tonic-gate 			}
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate 		} /* while (getshare(fp, &sharetab_entry) != 0) */
1190Sstevel@tonic-gate 		(void) mutex_unlock(&sharetab_lock);
1200Sstevel@tonic-gate 		(void) fclose(fp);
1210Sstevel@tonic-gate 	} else {
1220Sstevel@tonic-gate 		*errp = errno;
1230Sstevel@tonic-gate 	} /* if ((fp = fopen(SHARETAB, "r")) != NULL) */
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate 	/*
1260Sstevel@tonic-gate 	 * Caller must free the mount list
1270Sstevel@tonic-gate 	 */
1280Sstevel@tonic-gate 	return (headp);
1290Sstevel@tonic-gate } /* fs_get_share_list */
1300Sstevel@tonic-gate 
1310Sstevel@tonic-gate 
1320Sstevel@tonic-gate /*
1330Sstevel@tonic-gate  * fs_parse_opts_for_sec_modes
1340Sstevel@tonic-gate  * Get an array of strings of all the security modes of the option string.
1350Sstevel@tonic-gate  *
1360Sstevel@tonic-gate  * char *cmd - The option string from the share command.
1370Sstevel@tonic-gate  * int *count - pointer to the number of elements in the returned array.
1380Sstevel@tonic-gate  * int *error - error pointer for returning any errors.
1390Sstevel@tonic-gate  */
1400Sstevel@tonic-gate char **
fs_parse_opts_for_sec_modes(char * cmd,int * count,int * error)1410Sstevel@tonic-gate fs_parse_opts_for_sec_modes(char *cmd, int *count, int *error)
1420Sstevel@tonic-gate {
1430Sstevel@tonic-gate 	char *temp_str;
1440Sstevel@tonic-gate 	char **secstringarray;
1450Sstevel@tonic-gate 	char *strptr;
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate 	*count = 0;
1480Sstevel@tonic-gate 	strptr = strdup(cmd);
1490Sstevel@tonic-gate 	if (strptr == NULL) {
1500Sstevel@tonic-gate 		*error = ENOMEM;
1510Sstevel@tonic-gate 		return (NULL);
1520Sstevel@tonic-gate 	}
1530Sstevel@tonic-gate 
1540Sstevel@tonic-gate 	temp_str = strptr;
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate 	secstringarray =
1570Sstevel@tonic-gate 	    (char **)calloc((size_t)SECMODES, (size_t)(sizeof (char *)));
1580Sstevel@tonic-gate 	if (secstringarray == NULL) {
1590Sstevel@tonic-gate 		*error = ENOMEM;
1600Sstevel@tonic-gate 		return (NULL);
1610Sstevel@tonic-gate 	}
1620Sstevel@tonic-gate 
1630Sstevel@tonic-gate 	if (strstr(strptr, "sec=") != NULL) {
1640Sstevel@tonic-gate 		char *next_str;
1650Sstevel@tonic-gate 		next_str = strptr;
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate 		while (next_str != NULL) {
1680Sstevel@tonic-gate 			next_str = strstr(strptr, "sec=");
1690Sstevel@tonic-gate 			if (next_str != NULL) {
1700Sstevel@tonic-gate 				if (strncmp(strptr, "sec=", 4) != 0) {
1710Sstevel@tonic-gate 					*(next_str - 1) = '\0';
1720Sstevel@tonic-gate 				}
1730Sstevel@tonic-gate 				strptr = next_str;
1740Sstevel@tonic-gate 				next_str = strstr(strptr + 4, "sec=");
1750Sstevel@tonic-gate 				if (next_str != NULL) {
1760Sstevel@tonic-gate 					*(next_str - 1) = '\0';
1770Sstevel@tonic-gate 				}
1780Sstevel@tonic-gate 				secstringarray[*count] = strdup(strptr);
1790Sstevel@tonic-gate 				if (secstringarray[*count] == NULL) {
1800Sstevel@tonic-gate 					*error = ENOMEM;
1810Sstevel@tonic-gate 					if (*count > 0) {
1820Sstevel@tonic-gate 						fileutil_free_string_array(
1830Sstevel@tonic-gate 						    secstringarray, *count);
1840Sstevel@tonic-gate 					} else {
1850Sstevel@tonic-gate 						free(secstringarray);
1860Sstevel@tonic-gate 					}
1870Sstevel@tonic-gate 					free(temp_str);
1880Sstevel@tonic-gate 					return (NULL);
1890Sstevel@tonic-gate 				}
1900Sstevel@tonic-gate 				strptr = next_str;
1910Sstevel@tonic-gate 				(*count)++;
1920Sstevel@tonic-gate 			}
1930Sstevel@tonic-gate 		}
1940Sstevel@tonic-gate 	} else {
1950Sstevel@tonic-gate 		secstringarray[*count] = strdup(temp_str);
1960Sstevel@tonic-gate 		if (secstringarray[*count] == NULL) {
1970Sstevel@tonic-gate 			*error = ENOMEM;
1980Sstevel@tonic-gate 			if (*count > 0) {
1990Sstevel@tonic-gate 				fileutil_free_string_array(
2000Sstevel@tonic-gate 				    secstringarray, *count);
2010Sstevel@tonic-gate 			} else {
2020Sstevel@tonic-gate 				free(secstringarray);
2030Sstevel@tonic-gate 			}
2040Sstevel@tonic-gate 			free(temp_str);
2050Sstevel@tonic-gate 			return (NULL);
2060Sstevel@tonic-gate 		}
2070Sstevel@tonic-gate 		(*count)++;
2080Sstevel@tonic-gate 	}
2090Sstevel@tonic-gate 	free(temp_str);
2100Sstevel@tonic-gate 	return (secstringarray);
2110Sstevel@tonic-gate }
2120Sstevel@tonic-gate 
2130Sstevel@tonic-gate /*
2140Sstevel@tonic-gate  * fs_create_array_from_accesslist
2150Sstevel@tonic-gate  * Takes the colon seperated access list parses the list into an array
2160Sstevel@tonic-gate  * containing all the elements of the list. The array created is returned
2170Sstevel@tonic-gate  * and count is set to the number of elements in the array.
2180Sstevel@tonic-gate  *
2190Sstevel@tonic-gate  * char *access_list - The string containing the colon sperated access list.
2200Sstevel@tonic-gate  * int *count - Will contain the number of elements in the array.
2210Sstevel@tonic-gate  * int *err - any errors encountered.
2220Sstevel@tonic-gate  */
2230Sstevel@tonic-gate char **
fs_create_array_from_accesslist(char * access_list,int * count,int * err)2240Sstevel@tonic-gate fs_create_array_from_accesslist(char *access_list, int *count, int *err)
2250Sstevel@tonic-gate {
2260Sstevel@tonic-gate 	char *delimiter = ":";
2270Sstevel@tonic-gate 	char *server_string;
2280Sstevel@tonic-gate 	char **list_array = NULL;
2290Sstevel@tonic-gate 	char *list_copy;
2300Sstevel@tonic-gate 
2310Sstevel@tonic-gate 	*count = 0;
2320Sstevel@tonic-gate 	if (access_list != NULL) {
2330Sstevel@tonic-gate 		list_copy = strdup(access_list);
2340Sstevel@tonic-gate 		if (list_copy != NULL) {
2350Sstevel@tonic-gate 			server_string = strtok(list_copy, delimiter);
2360Sstevel@tonic-gate 			if (server_string != NULL) {
2370Sstevel@tonic-gate 				while (server_string != NULL) {
2380Sstevel@tonic-gate 					if (!fileutil_add_string_to_array(
2390Sstevel@tonic-gate 					    &list_array, server_string, count,
2400Sstevel@tonic-gate 					    err)) {
2410Sstevel@tonic-gate 						fileutil_free_string_array(
2420Sstevel@tonic-gate 						    list_array, *count);
2430Sstevel@tonic-gate 						free(list_copy);
2440Sstevel@tonic-gate 						goto return_err;
2450Sstevel@tonic-gate 					}
2460Sstevel@tonic-gate 					server_string =
2470Sstevel@tonic-gate 					    strtok(NULL, delimiter);
2480Sstevel@tonic-gate 				}
2490Sstevel@tonic-gate 			} else {
2500Sstevel@tonic-gate 				list_array =
2510Sstevel@tonic-gate 				    (char **)calloc(((*count) + 1),
2520Sstevel@tonic-gate 				    sizeof (char *));
2530Sstevel@tonic-gate 				if (list_array == NULL) {
2540Sstevel@tonic-gate 					*err = ENOMEM;
2550Sstevel@tonic-gate 					free(list_copy);
2560Sstevel@tonic-gate 					goto return_err;
2570Sstevel@tonic-gate 				}
2580Sstevel@tonic-gate 				list_array[*count] = strdup(access_list);
2590Sstevel@tonic-gate 				if (list_array[*count] == NULL) {
2600Sstevel@tonic-gate 					*err = ENOMEM;
2610Sstevel@tonic-gate 					free(list_array);
2620Sstevel@tonic-gate 					list_array = NULL;
2630Sstevel@tonic-gate 					goto return_err;
2640Sstevel@tonic-gate 				}
2650Sstevel@tonic-gate 				(*count)++;
2660Sstevel@tonic-gate 			}
2670Sstevel@tonic-gate 			free(list_copy);
2680Sstevel@tonic-gate 		} else {
2690Sstevel@tonic-gate 			*err = ENOMEM;
2700Sstevel@tonic-gate 		}
2710Sstevel@tonic-gate 	}
2720Sstevel@tonic-gate return_err:
2730Sstevel@tonic-gate 	return (list_array);
2740Sstevel@tonic-gate } /* fs_create_array_from_accesslist */
2750Sstevel@tonic-gate 
2760Sstevel@tonic-gate 
2770Sstevel@tonic-gate /*
2780Sstevel@tonic-gate  * Private Methods
2790Sstevel@tonic-gate  */
2800Sstevel@tonic-gate 
2810Sstevel@tonic-gate fs_sharelist_t *
create_sharelist_entry(struct share * sharetab_entry,int * errp)2820Sstevel@tonic-gate create_sharelist_entry(struct share *sharetab_entry, int *errp)
2830Sstevel@tonic-gate {
2840Sstevel@tonic-gate 
2850Sstevel@tonic-gate 	fs_sharelist_t	*newp;
2860Sstevel@tonic-gate 
2870Sstevel@tonic-gate 	newp = (fs_sharelist_t *)calloc((size_t)1,
2880Sstevel@tonic-gate 	    (size_t)sizeof (fs_sharelist_t));
2890Sstevel@tonic-gate 
2900Sstevel@tonic-gate 	if (newp == NULL) {
2910Sstevel@tonic-gate 		/*
2920Sstevel@tonic-gate 		 * Out of memory
2930Sstevel@tonic-gate 		 */
2940Sstevel@tonic-gate 		*errp = errno;
2950Sstevel@tonic-gate 		return (NULL);
2960Sstevel@tonic-gate 	}
2970Sstevel@tonic-gate 
2980Sstevel@tonic-gate 	newp->path = strdup(sharetab_entry->sh_path);
2990Sstevel@tonic-gate 	if (newp->path == NULL) {
3000Sstevel@tonic-gate 		/*
3010Sstevel@tonic-gate 		 * Out of memory
3020Sstevel@tonic-gate 		 */
3030Sstevel@tonic-gate 		*errp = errno;
3040Sstevel@tonic-gate 		fs_free_share_list(newp);
3050Sstevel@tonic-gate 		return (NULL);
3060Sstevel@tonic-gate 	}
3070Sstevel@tonic-gate 
3080Sstevel@tonic-gate 	newp->resource = strdup(sharetab_entry->sh_res);
3090Sstevel@tonic-gate 	if (newp->path == NULL) {
3100Sstevel@tonic-gate 		/*
3110Sstevel@tonic-gate 		 * Out of memory
3120Sstevel@tonic-gate 		 */
3130Sstevel@tonic-gate 		*errp = errno;
3140Sstevel@tonic-gate 		fs_free_share_list(newp);
3150Sstevel@tonic-gate 		return (NULL);
3160Sstevel@tonic-gate 	}
3170Sstevel@tonic-gate 
3180Sstevel@tonic-gate 	newp->fstype = strdup(sharetab_entry->sh_fstype);
3190Sstevel@tonic-gate 	if (newp->fstype == NULL) {
3200Sstevel@tonic-gate 		/*
3210Sstevel@tonic-gate 		 * Out of memory
3220Sstevel@tonic-gate 		 */
3230Sstevel@tonic-gate 		*errp = errno;
3240Sstevel@tonic-gate 		fs_free_share_list(newp);
3250Sstevel@tonic-gate 		return (NULL);
3260Sstevel@tonic-gate 	}
3270Sstevel@tonic-gate 
3280Sstevel@tonic-gate 	newp->options = strdup(sharetab_entry->sh_opts);
3290Sstevel@tonic-gate 	if (newp->options == NULL) {
3300Sstevel@tonic-gate 		/*
3310Sstevel@tonic-gate 		 * Out of memory
3320Sstevel@tonic-gate 		 */
3330Sstevel@tonic-gate 		*errp = errno;
3340Sstevel@tonic-gate 		fs_free_share_list(newp);
3350Sstevel@tonic-gate 		return (NULL);
3360Sstevel@tonic-gate 	}
3370Sstevel@tonic-gate 
3380Sstevel@tonic-gate 	newp->description = strdup(sharetab_entry->sh_descr);
3390Sstevel@tonic-gate 	if (newp->description == NULL) {
3400Sstevel@tonic-gate 		/*
3410Sstevel@tonic-gate 		 * Out of memory
3420Sstevel@tonic-gate 		 */
3430Sstevel@tonic-gate 		*errp = errno;
3440Sstevel@tonic-gate 		fs_free_share_list(newp);
3450Sstevel@tonic-gate 		return (NULL);
3460Sstevel@tonic-gate 	}
3470Sstevel@tonic-gate 	newp->next = NULL;
3480Sstevel@tonic-gate 
3490Sstevel@tonic-gate 	return (newp);
3500Sstevel@tonic-gate } /* create_sharelist_entry */
351