1 /*-
2  * Copyright (c) 2008 Doug Rabson
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/kobj.h>
33 #include <sys/lock.h>
34 #include <sys/malloc.h>
35 #include <sys/mutex.h>
36 
37 #include <rpc/rpc.h>
38 #include <rpc/rpcsec_gss.h>
39 
40 #include "rpcsec_gss_int.h"
41 
42 bool_t
43 rpc_gss_mech_to_oid(const char *mech, gss_OID *oid_ret)
44 {
45 	gss_OID oid = kgss_find_mech_by_name(mech);
46 
47 	if (oid) {
48 		*oid_ret = oid;
49 		return (TRUE);
50 	}
51 	_rpc_gss_set_error(RPC_GSS_ER_SYSTEMERROR, ENOENT);
52 	return (FALSE);
53 }
54 
55 bool_t
56 rpc_gss_oid_to_mech(gss_OID oid, const char **mech_ret)
57 {
58 	const char *name = kgss_find_mech_by_oid(oid);
59 
60 	if (name) {
61 		*mech_ret = name;
62 		return (TRUE);
63 	}
64 	_rpc_gss_set_error(RPC_GSS_ER_SYSTEMERROR, ENOENT);
65 	return (FALSE);
66 }
67 
68 bool_t
69 rpc_gss_qop_to_num(const char *qop, const char *mech, u_int *num_ret)
70 {
71 
72 	if (!strcmp(qop, "default")) {
73 		*num_ret = GSS_C_QOP_DEFAULT;
74 		return (TRUE);
75 	}
76 	_rpc_gss_set_error(RPC_GSS_ER_SYSTEMERROR, ENOENT);
77 	return (FALSE);
78 }
79 
80 const char *
81 _rpc_gss_num_to_qop(const char *mech, u_int num)
82 {
83 
84 	if (num == GSS_C_QOP_DEFAULT)
85 		return "default";
86 
87 	return (NULL);
88 }
89 
90 const char **
91 rpc_gss_get_mechanisms(void)
92 {
93 	static const char **mech_names = NULL;
94 	struct kgss_mech *km;
95 	int count;
96 
97 	if (mech_names)
98 		return (mech_names);
99 
100 	count = 0;
101 	LIST_FOREACH(km, &kgss_mechs, km_link) {
102 		count++;
103 	}
104 	count++;
105 
106 	mech_names = malloc(count * sizeof(const char *), M_RPC, M_WAITOK);
107 	count = 0;
108 	LIST_FOREACH(km, &kgss_mechs, km_link) {
109 		mech_names[count++] = km->km_mech_name;
110 	}
111 	mech_names[count++] = NULL;
112 
113 	return (mech_names);
114 }
115 
116 #if 0
117 const char **
118 rpc_gss_get_mech_info(const char *mech, rpc_gss_service_t *service)
119 {
120 	struct mech_info *info;
121 
122 	_rpc_gss_load_mech();
123 	_rpc_gss_load_qop();
124 	SLIST_FOREACH(info, &mechs, link) {
125 		if (!strcmp(mech, info->name)) {
126 			/*
127 			 * I'm not sure what to do with service
128 			 * here. The Solaris manpages are not clear on
129 			 * the subject and the OpenSolaris code just
130 			 * sets it to rpc_gss_svc_privacy
131 			 * unconditionally with a comment noting that
132 			 * it is bogus.
133 			 */
134 			*service = rpc_gss_svc_privacy;
135 			return info->qops;
136 		}
137 	}
138 
139 	_rpc_gss_set_error(RPC_GSS_ER_SYSTEMERROR, ENOENT);
140 	return (NULL);
141 }
142 #endif
143 
144 bool_t
145 rpc_gss_get_versions(u_int *vers_hi, u_int *vers_lo)
146 {
147 
148 	*vers_hi = 1;
149 	*vers_lo = 1;
150 	return (TRUE);
151 }
152 
153 bool_t
154 rpc_gss_is_installed(const char *mech)
155 {
156 	gss_OID oid = kgss_find_mech_by_name(mech);
157 
158 	if (oid)
159 		return (TRUE);
160 	else
161 		return (FALSE);
162 }
163 
164