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 #include <errno.h>
28 #include <sys/types.h>
29 #include <nfs/nfs_sec.h>
30 #include <strings.h>
31 #include "libfsmgt.h"
32 
33 /*
34  * Public methods
35  */
36 
37 /*
38  * Method: nfssec_free_secmode_list
39  *
40  * Description: Frees the space allocated for the security mode list array.
41  *
42  * Parameters:
43  *	- char **seclist - the array to be freed.
44  *	- int num_elements - the number of elements in the array.
45  *
46  * Returns:
47  *	- Nothing
48  */
49 void
nfssec_free_secmode_list(char ** seclist,int num_elements)50 nfssec_free_secmode_list(char **seclist, int num_elements)
51 {
52 	fileutil_free_string_array(seclist, num_elements);
53 } /* nfssec_free_secmode_list */
54 
55 /*
56  * Method: nfssec_get_default_secmode
57  *
58  * Description: Retrieves the default security mode for NFS.
59  *
60  * Parameters:
61  *	- int *errp - the error indicator.  This will be set to a non-zero
62  *	value upon error.
63  *
64  * Returns:
65  *	- char * - the NFS security mode name.
66  *	- NULL if an error occurred.
67  *
68  * Note: Caller must free the space allocated for the return value.
69  */
70 char *
nfssec_get_default_secmode(int * errp)71 nfssec_get_default_secmode(int *errp)
72 {
73 	seconfig_t	secp, defsecp;
74 	char		*ret_val;
75 	int		err = 0;
76 
77 	*errp = 0;
78 	err = nfs_getseconfig_default(&secp);
79 	if (err != 0) {
80 		*errp = err;
81 		return (NULL);
82 	}
83 
84 	err = nfs_getseconfig_bynumber(secp.sc_nfsnum, &defsecp);
85 	if (err != 0) {
86 		*errp = err;
87 		return (NULL);
88 	}
89 
90 	ret_val = strdup(defsecp.sc_name);
91 	if (ret_val == NULL) {
92 		*errp = ENOMEM;
93 		return (NULL);
94 	}
95 
96 	return (ret_val);
97 } /* nfssec_get_default_secmode */
98 
99 /*
100  * Method: nfssec_get_nfs_secmode_list
101  *
102  * Description: Retrieves a list of the supported NFS security modes from
103  * /etc/nfssec.conf.
104  *
105  * Parameters:
106  *	- int *num_elements - integer pointer used to keep track of the number
107  *	of elements in the array.
108  *	- int *errp - the error indicator.  This will be set to a non-zero
109  *	value upon error.
110  *
111  * Returns:
112  *	- char ** - The array containing the supported security mode names as
113  *	elements.
114  *	- NULL if an error occurred.
115  *
116  * Note: The space allocated for the return array must be freed by the caller
117  * using nfssec_free_secmode_list.
118  */
119 char **
nfssec_get_nfs_secmode_list(int * num_elements,int * errp)120 nfssec_get_nfs_secmode_list(int *num_elements, int *errp)
121 {
122 	FILE	*fp;
123 	char	**seclist = NULL;
124 	int	err = 0;
125 
126 	*errp = 0;
127 	if ((fp = fopen(NFSSEC_CONF, "r")) == NULL) {
128 		/*
129 		 * The opening of nfssec.conf failed.
130 		 */
131 		*errp = errno;
132 		return (NULL);
133 	}
134 
135 	seclist = fileutil_get_first_column_data(fp, num_elements, &err);
136 	(void) fclose(fp);
137 	if (seclist == NULL) {
138 		*errp = err;
139 		return (NULL);
140 	}
141 
142 	return (seclist);
143 } /* nfssec_get_nfs_secmode_list */
144