xref: /freebsd/sys/kgssapi/gss_impl.c (revision 315ee00f)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
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 #include <sys/param.h>
32 #include <sys/jail.h>
33 #include <sys/kernel.h>
34 #include <sys/kobj.h>
35 #include <sys/lock.h>
36 #include <sys/malloc.h>
37 #include <sys/module.h>
38 #include <sys/mutex.h>
39 #include <sys/priv.h>
40 #include <sys/proc.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 struct mtx kgss_gssd_lock;
66 
67 KGSS_VNET_DEFINE(CLIENT *, kgss_gssd_handle) = NULL;
68 
69 static int
70 kgss_load(void)
71 {
72 	int error;
73 
74 	LIST_INIT(&kgss_mechs);
75 	error = syscall_helper_register(gssd_syscalls, SY_THR_STATIC_KLD);
76 	if (error != 0)
77 		return (error);
78 	return (0);
79 }
80 
81 static void
82 kgss_unload(void)
83 {
84 
85 	syscall_helper_unregister(gssd_syscalls);
86 }
87 
88 int
89 sys_gssd_syscall(struct thread *td, struct gssd_syscall_args *uap)
90 {
91         struct sockaddr_un sun;
92         struct netconfig *nconf;
93 	char path[MAXPATHLEN];
94 	int error;
95 	CLIENT *cl, *oldcl;
96 
97 	error = priv_check(td, PRIV_NFS_DAEMON);
98 	if (error)
99 		return (error);
100 
101 	error = copyinstr(uap->path, path, sizeof(path), NULL);
102 	if (error)
103 		return (error);
104 	if (strlen(path) + 1 > sizeof(sun.sun_path))
105 		return (EINVAL);
106 
107 	if (path[0] != '\0') {
108 		sun.sun_family = AF_LOCAL;
109 		strlcpy(sun.sun_path, path, sizeof(sun.sun_path));
110 		sun.sun_len = SUN_LEN(&sun);
111 
112 		nconf = getnetconfigent("local");
113 		cl = clnt_reconnect_create(nconf,
114 		    (struct sockaddr *) &sun, GSSD, GSSDVERS,
115 		    RPC_MAXDATASIZE, RPC_MAXDATASIZE);
116 		/*
117 		 * The number of retries defaults to INT_MAX, which effectively
118 		 * means an infinite, uninterruptable loop.  Limiting it to
119 		 * five retries keeps it from running forever.
120 		 */
121 		if (cl != NULL) {
122 			int retry_count = 5;
123 			struct timeval timo;
124 			CLNT_CONTROL(cl, CLSET_RETRIES, &retry_count);
125 
126 			/*
127 			 * Set the timeout for an upcall to 5 minutes.  The
128 			 * default of 25 seconds is not long enough for some
129 			 * gss_XXX() calls done by the gssd(8) daemon.
130 			 */
131 			timo.tv_sec = 5 * 60;
132 			timo.tv_usec = 0;
133 			CLNT_CONTROL(cl, CLSET_TIMEOUT, &timo);
134 		}
135 	} else
136 		cl = NULL;
137 
138 	KGSS_CURVNET_SET_QUIET(KGSS_TD_TO_VNET(curthread));
139 	mtx_lock(&kgss_gssd_lock);
140 	oldcl = KGSS_VNET(kgss_gssd_handle);
141 	KGSS_VNET(kgss_gssd_handle) = cl;
142 	mtx_unlock(&kgss_gssd_lock);
143 	KGSS_CURVNET_RESTORE();
144 
145 	if (oldcl != NULL) {
146 		CLNT_CLOSE(oldcl);
147 		CLNT_RELEASE(oldcl);
148 	}
149 
150 	return (0);
151 }
152 
153 int
154 kgss_oid_equal(const gss_OID oid1, const gss_OID oid2)
155 {
156 
157 	if (oid1 == oid2)
158 		return (1);
159 	if (!oid1 || !oid2)
160 		return (0);
161 	if (oid1->length != oid2->length)
162 		return (0);
163 	if (memcmp(oid1->elements, oid2->elements, oid1->length))
164 		return (0);
165 	return (1);
166 }
167 
168 void
169 kgss_install_mech(gss_OID mech_type, const char *name, struct kobj_class *cls)
170 {
171 	struct kgss_mech *km;
172 
173 	km = malloc(sizeof(struct kgss_mech), M_GSSAPI, M_WAITOK);
174 	km->km_mech_type = mech_type;
175 	km->km_mech_name = name;
176 	km->km_class = cls;
177 	LIST_INSERT_HEAD(&kgss_mechs, km, km_link);
178 }
179 
180 void
181 kgss_uninstall_mech(gss_OID mech_type)
182 {
183 	struct kgss_mech *km;
184 
185 	LIST_FOREACH(km, &kgss_mechs, km_link) {
186 		if (kgss_oid_equal(km->km_mech_type, mech_type)) {
187 			LIST_REMOVE(km, km_link);
188 			free(km, M_GSSAPI);
189 			return;
190 		}
191 	}
192 }
193 
194 gss_OID
195 kgss_find_mech_by_name(const char *name)
196 {
197 	struct kgss_mech *km;
198 
199 	LIST_FOREACH(km, &kgss_mechs, km_link) {
200 		if (!strcmp(km->km_mech_name, name)) {
201 			return (km->km_mech_type);
202 		}
203 	}
204 	return (GSS_C_NO_OID);
205 }
206 
207 const char *
208 kgss_find_mech_by_oid(const gss_OID oid)
209 {
210 	struct kgss_mech *km;
211 
212 	LIST_FOREACH(km, &kgss_mechs, km_link) {
213 		if (kgss_oid_equal(km->km_mech_type, oid)) {
214 			return (km->km_mech_name);
215 		}
216 	}
217 	return (NULL);
218 }
219 
220 gss_ctx_id_t
221 kgss_create_context(gss_OID mech_type)
222 {
223 	struct kgss_mech *km;
224 	gss_ctx_id_t ctx;
225 
226 	LIST_FOREACH(km, &kgss_mechs, km_link) {
227 		if (kgss_oid_equal(km->km_mech_type, mech_type))
228 			break;
229 	}
230 	if (!km)
231 		return (NULL);
232 
233 	ctx = (gss_ctx_id_t) kobj_create(km->km_class, M_GSSAPI, M_WAITOK);
234 	KGSS_INIT(ctx);
235 
236 	return (ctx);
237 }
238 
239 void
240 kgss_delete_context(gss_ctx_id_t ctx, gss_buffer_t output_token)
241 {
242 
243 	KGSS_DELETE(ctx, output_token);
244 	kobj_delete((kobj_t) ctx, M_GSSAPI);
245 }
246 
247 OM_uint32
248 kgss_transfer_context(gss_ctx_id_t ctx)
249 {
250 	struct export_sec_context_res res;
251 	struct export_sec_context_args args;
252 	enum clnt_stat stat;
253 	OM_uint32 maj_stat;
254 
255 	KGSS_CURVNET_SET_QUIET(KGSS_TD_TO_VNET(curthread));
256 	if (!KGSS_VNET(kgss_gssd_handle)) {
257 		KGSS_CURVNET_RESTORE();
258 		return (GSS_S_FAILURE);
259 	}
260 
261 	args.ctx = ctx->handle;
262 	bzero(&res, sizeof(res));
263 	stat = gssd_export_sec_context_1(&args, &res, KGSS_VNET(kgss_gssd_handle));
264 	KGSS_CURVNET_RESTORE();
265 	if (stat != RPC_SUCCESS) {
266 		return (GSS_S_FAILURE);
267 	}
268 
269 	maj_stat = KGSS_IMPORT(ctx, res.format, &res.interprocess_token);
270 	ctx->handle = 0;
271 
272 	xdr_free((xdrproc_t) xdr_export_sec_context_res, &res);
273 
274 	return (maj_stat);
275 }
276 
277 void
278 kgss_copy_buffer(const gss_buffer_t from, gss_buffer_t to)
279 {
280 	to->length = from->length;
281 	if (from->length) {
282 		to->value = malloc(from->length, M_GSSAPI, M_WAITOK);
283 		bcopy(from->value, to->value, from->length);
284 	} else {
285 		to->value = NULL;
286 	}
287 }
288 
289 /*
290  * Acquire the kgss_gssd_handle and return it with a reference count,
291  * if it is available.
292  */
293 CLIENT *
294 kgss_gssd_client(void)
295 {
296 	CLIENT *cl;
297 
298 	KGSS_CURVNET_SET_QUIET(KGSS_TD_TO_VNET(curthread));
299 	mtx_lock(&kgss_gssd_lock);
300 	cl = KGSS_VNET(kgss_gssd_handle);
301 	if (cl != NULL)
302 		CLNT_ACQUIRE(cl);
303 	mtx_unlock(&kgss_gssd_lock);
304 	KGSS_CURVNET_RESTORE();
305 	return (cl);
306 }
307 
308 /*
309  * Kernel module glue
310  */
311 static int
312 kgssapi_modevent(module_t mod, int type, void *data)
313 {
314 	int error = 0;
315 
316 	switch (type) {
317 	case MOD_LOAD:
318 		rpc_gss_entries.rpc_gss_refresh_auth = rpc_gss_refresh_auth;
319 		rpc_gss_entries.rpc_gss_secfind = rpc_gss_secfind;
320 		rpc_gss_entries.rpc_gss_secpurge = rpc_gss_secpurge;
321 		rpc_gss_entries.rpc_gss_seccreate = rpc_gss_seccreate;
322 		rpc_gss_entries.rpc_gss_set_defaults = rpc_gss_set_defaults;
323 		rpc_gss_entries.rpc_gss_max_data_length =
324 		    rpc_gss_max_data_length;
325 		rpc_gss_entries.rpc_gss_get_error = rpc_gss_get_error;
326 		rpc_gss_entries.rpc_gss_mech_to_oid = rpc_gss_mech_to_oid;
327 		rpc_gss_entries.rpc_gss_oid_to_mech = rpc_gss_oid_to_mech;
328 		rpc_gss_entries.rpc_gss_qop_to_num = rpc_gss_qop_to_num;
329 		rpc_gss_entries.rpc_gss_get_mechanisms = rpc_gss_get_mechanisms;
330 		rpc_gss_entries.rpc_gss_get_versions = rpc_gss_get_versions;
331 		rpc_gss_entries.rpc_gss_is_installed = rpc_gss_is_installed;
332 		rpc_gss_entries.rpc_gss_set_svc_name = rpc_gss_set_svc_name;
333 		rpc_gss_entries.rpc_gss_clear_svc_name = rpc_gss_clear_svc_name;
334 		rpc_gss_entries.rpc_gss_getcred = rpc_gss_getcred;
335 		rpc_gss_entries.rpc_gss_set_callback = rpc_gss_set_callback;
336 		rpc_gss_entries.rpc_gss_clear_callback = rpc_gss_clear_callback;
337 		rpc_gss_entries.rpc_gss_get_principal_name =
338 		    rpc_gss_get_principal_name;
339 		rpc_gss_entries.rpc_gss_svc_max_data_length =
340 		    rpc_gss_svc_max_data_length;
341 		mtx_init(&kgss_gssd_lock, "kgss_gssd_lock", NULL, MTX_DEF);
342 		error = kgss_load();
343 		break;
344 	case MOD_UNLOAD:
345 		kgss_unload();
346 		mtx_destroy(&kgss_gssd_lock);
347 		/*
348 		 * Unloading of the kgssapi module is not currently supported.
349 		 * If somebody wants this, we would need to keep track of
350 		 * currently executing threads and make sure the count is 0.
351 		 */
352 		/* FALLTHROUGH */
353 	default:
354 		error = EOPNOTSUPP;
355 	}
356 	return (error);
357 }
358 static moduledata_t kgssapi_mod = {
359 	"kgssapi",
360 	kgssapi_modevent,
361 	NULL,
362 };
363 DECLARE_MODULE(kgssapi, kgssapi_mod, SI_SUB_VFS, SI_ORDER_ANY);
364 MODULE_DEPEND(kgssapi, xdr, 1, 1, 1);
365 MODULE_DEPEND(kgssapi, krpc, 1, 1, 1);
366 MODULE_VERSION(kgssapi, 1);
367