xref: /freebsd/sys/kgssapi/gss_impl.c (revision f6a3b357e9be4c6423c85eff9a847163a0d307c8)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2008 Isilon Inc http://www.isilon.com/
5  * Authors: Doug Rabson <dfr@rabson.org>
6  * Developed with Red Inc: Alfred Perlstein <alfred@freebsd.org>
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32 
33 #include <sys/param.h>
34 #include <sys/kernel.h>
35 #include <sys/kobj.h>
36 #include <sys/lock.h>
37 #include <sys/malloc.h>
38 #include <sys/module.h>
39 #include <sys/mutex.h>
40 #include <sys/priv.h>
41 #include <sys/syscall.h>
42 #include <sys/sysent.h>
43 #include <sys/sysproto.h>
44 
45 #include <kgssapi/gssapi.h>
46 #include <kgssapi/gssapi_impl.h>
47 #include <rpc/rpc.h>
48 #include <rpc/rpc_com.h>
49 #include <rpc/rpcsec_gss.h>
50 
51 #include "gssd.h"
52 #include "kgss_if.h"
53 
54 MALLOC_DEFINE(M_GSSAPI, "GSS-API", "GSS-API");
55 
56 /*
57  * Syscall hooks
58  */
59 static struct syscall_helper_data gssd_syscalls[] = {
60 	SYSCALL_INIT_HELPER(gssd_syscall),
61 	SYSCALL_INIT_LAST
62 };
63 
64 struct kgss_mech_list kgss_mechs;
65 CLIENT *kgss_gssd_handle;
66 struct mtx kgss_gssd_lock;
67 
68 static int
69 kgss_load(void)
70 {
71 	int error;
72 
73 	LIST_INIT(&kgss_mechs);
74 	error = syscall_helper_register(gssd_syscalls, SY_THR_STATIC_KLD);
75 	if (error != 0)
76 		return (error);
77 	return (0);
78 }
79 
80 static void
81 kgss_unload(void)
82 {
83 
84 	syscall_helper_unregister(gssd_syscalls);
85 }
86 
87 int
88 sys_gssd_syscall(struct thread *td, struct gssd_syscall_args *uap)
89 {
90         struct sockaddr_un sun;
91         struct netconfig *nconf;
92 	char path[MAXPATHLEN];
93 	int error;
94 	CLIENT *cl, *oldcl;
95 
96 	error = priv_check(td, PRIV_NFS_DAEMON);
97 	if (error)
98 		return (error);
99 
100 	error = copyinstr(uap->path, path, sizeof(path), NULL);
101 	if (error)
102 		return (error);
103 	if (strlen(path) + 1 > sizeof(sun.sun_path))
104 		return (EINVAL);
105 
106 	if (path[0] != '\0') {
107 		sun.sun_family = AF_LOCAL;
108 		strlcpy(sun.sun_path, path, sizeof(sun.sun_path));
109 		sun.sun_len = SUN_LEN(&sun);
110 
111 		nconf = getnetconfigent("local");
112 		cl = clnt_reconnect_create(nconf,
113 		    (struct sockaddr *) &sun, GSSD, GSSDVERS,
114 		    RPC_MAXDATASIZE, RPC_MAXDATASIZE);
115 		/*
116 		 * The number of retries defaults to INT_MAX, which effectively
117 		 * means an infinite, uninterruptable loop.  Limiting it to
118 		 * five retries keeps it from running forever.
119 		 */
120 		if (cl != NULL) {
121 			int retry_count = 5;
122 			CLNT_CONTROL(cl, CLSET_RETRIES, &retry_count);
123 		}
124 	} else
125 		cl = NULL;
126 
127 	mtx_lock(&kgss_gssd_lock);
128 	oldcl = kgss_gssd_handle;
129 	kgss_gssd_handle = cl;
130 	mtx_unlock(&kgss_gssd_lock);
131 
132 	if (oldcl != NULL) {
133 		CLNT_CLOSE(oldcl);
134 		CLNT_RELEASE(oldcl);
135 	}
136 
137 	return (0);
138 }
139 
140 int
141 kgss_oid_equal(const gss_OID oid1, const gss_OID oid2)
142 {
143 
144 	if (oid1 == oid2)
145 		return (1);
146 	if (!oid1 || !oid2)
147 		return (0);
148 	if (oid1->length != oid2->length)
149 		return (0);
150 	if (memcmp(oid1->elements, oid2->elements, oid1->length))
151 		return (0);
152 	return (1);
153 }
154 
155 void
156 kgss_install_mech(gss_OID mech_type, const char *name, struct kobj_class *cls)
157 {
158 	struct kgss_mech *km;
159 
160 	km = malloc(sizeof(struct kgss_mech), M_GSSAPI, M_WAITOK);
161 	km->km_mech_type = mech_type;
162 	km->km_mech_name = name;
163 	km->km_class = cls;
164 	LIST_INSERT_HEAD(&kgss_mechs, km, km_link);
165 }
166 
167 void
168 kgss_uninstall_mech(gss_OID mech_type)
169 {
170 	struct kgss_mech *km;
171 
172 	LIST_FOREACH(km, &kgss_mechs, km_link) {
173 		if (kgss_oid_equal(km->km_mech_type, mech_type)) {
174 			LIST_REMOVE(km, km_link);
175 			free(km, M_GSSAPI);
176 			return;
177 		}
178 	}
179 }
180 
181 gss_OID
182 kgss_find_mech_by_name(const char *name)
183 {
184 	struct kgss_mech *km;
185 
186 	LIST_FOREACH(km, &kgss_mechs, km_link) {
187 		if (!strcmp(km->km_mech_name, name)) {
188 			return (km->km_mech_type);
189 		}
190 	}
191 	return (GSS_C_NO_OID);
192 }
193 
194 const char *
195 kgss_find_mech_by_oid(const gss_OID oid)
196 {
197 	struct kgss_mech *km;
198 
199 	LIST_FOREACH(km, &kgss_mechs, km_link) {
200 		if (kgss_oid_equal(km->km_mech_type, oid)) {
201 			return (km->km_mech_name);
202 		}
203 	}
204 	return (NULL);
205 }
206 
207 gss_ctx_id_t
208 kgss_create_context(gss_OID mech_type)
209 {
210 	struct kgss_mech *km;
211 	gss_ctx_id_t ctx;
212 
213 	LIST_FOREACH(km, &kgss_mechs, km_link) {
214 		if (kgss_oid_equal(km->km_mech_type, mech_type))
215 			break;
216 	}
217 	if (!km)
218 		return (NULL);
219 
220 	ctx = (gss_ctx_id_t) kobj_create(km->km_class, M_GSSAPI, M_WAITOK);
221 	KGSS_INIT(ctx);
222 
223 	return (ctx);
224 }
225 
226 void
227 kgss_delete_context(gss_ctx_id_t ctx, gss_buffer_t output_token)
228 {
229 
230 	KGSS_DELETE(ctx, output_token);
231 	kobj_delete((kobj_t) ctx, M_GSSAPI);
232 }
233 
234 OM_uint32
235 kgss_transfer_context(gss_ctx_id_t ctx)
236 {
237 	struct export_sec_context_res res;
238 	struct export_sec_context_args args;
239 	enum clnt_stat stat;
240 	OM_uint32 maj_stat;
241 
242 	if (!kgss_gssd_handle)
243 		return (GSS_S_FAILURE);
244 
245 	args.ctx = ctx->handle;
246 	bzero(&res, sizeof(res));
247 	stat = gssd_export_sec_context_1(&args, &res, kgss_gssd_handle);
248 	if (stat != RPC_SUCCESS) {
249 		return (GSS_S_FAILURE);
250 	}
251 
252 	maj_stat = KGSS_IMPORT(ctx, res.format, &res.interprocess_token);
253 	ctx->handle = 0;
254 
255 	xdr_free((xdrproc_t) xdr_export_sec_context_res, &res);
256 
257 	return (maj_stat);
258 }
259 
260 void
261 kgss_copy_buffer(const gss_buffer_t from, gss_buffer_t to)
262 {
263 	to->length = from->length;
264 	if (from->length) {
265 		to->value = malloc(from->length, M_GSSAPI, M_WAITOK);
266 		bcopy(from->value, to->value, from->length);
267 	} else {
268 		to->value = NULL;
269 	}
270 }
271 
272 /*
273  * Acquire the kgss_gssd_handle and return it with a reference count,
274  * if it is available.
275  */
276 CLIENT *
277 kgss_gssd_client(void)
278 {
279 	CLIENT *cl;
280 
281 	mtx_lock(&kgss_gssd_lock);
282 	cl = kgss_gssd_handle;
283 	if (cl != NULL)
284 		CLNT_ACQUIRE(cl);
285 	mtx_unlock(&kgss_gssd_lock);
286 	return (cl);
287 }
288 
289 /*
290  * Kernel module glue
291  */
292 static int
293 kgssapi_modevent(module_t mod, int type, void *data)
294 {
295 	int error = 0;
296 
297 	switch (type) {
298 	case MOD_LOAD:
299 		rpc_gss_entries.rpc_gss_refresh_auth = rpc_gss_refresh_auth;
300 		rpc_gss_entries.rpc_gss_secfind = rpc_gss_secfind;
301 		rpc_gss_entries.rpc_gss_secpurge = rpc_gss_secpurge;
302 		rpc_gss_entries.rpc_gss_seccreate = rpc_gss_seccreate;
303 		rpc_gss_entries.rpc_gss_set_defaults = rpc_gss_set_defaults;
304 		rpc_gss_entries.rpc_gss_max_data_length =
305 		    rpc_gss_max_data_length;
306 		rpc_gss_entries.rpc_gss_get_error = rpc_gss_get_error;
307 		rpc_gss_entries.rpc_gss_mech_to_oid = rpc_gss_mech_to_oid;
308 		rpc_gss_entries.rpc_gss_oid_to_mech = rpc_gss_oid_to_mech;
309 		rpc_gss_entries.rpc_gss_qop_to_num = rpc_gss_qop_to_num;
310 		rpc_gss_entries.rpc_gss_get_mechanisms = rpc_gss_get_mechanisms;
311 		rpc_gss_entries.rpc_gss_get_versions = rpc_gss_get_versions;
312 		rpc_gss_entries.rpc_gss_is_installed = rpc_gss_is_installed;
313 		rpc_gss_entries.rpc_gss_set_svc_name = rpc_gss_set_svc_name;
314 		rpc_gss_entries.rpc_gss_clear_svc_name = rpc_gss_clear_svc_name;
315 		rpc_gss_entries.rpc_gss_getcred = rpc_gss_getcred;
316 		rpc_gss_entries.rpc_gss_set_callback = rpc_gss_set_callback;
317 		rpc_gss_entries.rpc_gss_clear_callback = rpc_gss_clear_callback;
318 		rpc_gss_entries.rpc_gss_get_principal_name =
319 		    rpc_gss_get_principal_name;
320 		rpc_gss_entries.rpc_gss_svc_max_data_length =
321 		    rpc_gss_svc_max_data_length;
322 		mtx_init(&kgss_gssd_lock, "kgss_gssd_lock", NULL, MTX_DEF);
323 		error = kgss_load();
324 		break;
325 	case MOD_UNLOAD:
326 		kgss_unload();
327 		mtx_destroy(&kgss_gssd_lock);
328 		/*
329 		 * Unloading of the kgssapi module is not currently supported.
330 		 * If somebody wants this, we would need to keep track of
331 		 * currently executing threads and make sure the count is 0.
332 		 */
333 		/* FALLTHROUGH */
334 	default:
335 		error = EOPNOTSUPP;
336 	}
337 	return (error);
338 }
339 static moduledata_t kgssapi_mod = {
340 	"kgssapi",
341 	kgssapi_modevent,
342 	NULL,
343 };
344 DECLARE_MODULE(kgssapi, kgssapi_mod, SI_SUB_VFS, SI_ORDER_ANY);
345 MODULE_DEPEND(kgssapi, krpc, 1, 1, 1);
346 MODULE_VERSION(kgssapi, 1);
347