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