2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
34 #include <sys/kernel.h>
37 #include <sys/malloc.h>
38 #include <sys/module.h>
39 #include <sys/mutex.h>
41 #include <sys/syscall.h>
42 #include <sys/sysent.h>
43 #include <sys/sysproto.h>
45 #include <kgssapi/gssapi.h>
46 #include <kgssapi/gssapi_impl.h>
48 #include <rpc/rpc_com.h>
49 #include <rpc/rpcsec_gss.h>
54 MALLOC_DEFINE(M_GSSAPI, "GSS-API", "GSS-API");
59 static struct syscall_helper_data gssd_syscalls[] = {
60 SYSCALL_INIT_HELPER(gssd_syscall),
64 struct kgss_mech_list kgss_mechs;
65 CLIENT *kgss_gssd_handle;
66 struct mtx kgss_gssd_lock;
73 LIST_INIT(&kgss_mechs);
74 error = syscall_helper_register(gssd_syscalls, SY_THR_STATIC_KLD);
84 syscall_helper_unregister(gssd_syscalls);
88 sys_gssd_syscall(struct thread *td, struct gssd_syscall_args *uap)
90 struct sockaddr_un sun;
91 struct netconfig *nconf;
92 char path[MAXPATHLEN];
96 error = priv_check(td, PRIV_NFS_DAEMON);
100 error = copyinstr(uap->path, path, sizeof(path), NULL);
103 if (strlen(path) + 1 > sizeof(sun.sun_path))
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);
111 nconf = getnetconfigent("local");
112 cl = clnt_reconnect_create(nconf,
113 (struct sockaddr *) &sun, GSSD, GSSDVERS,
114 RPC_MAXDATASIZE, RPC_MAXDATASIZE);
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.
122 CLNT_CONTROL(cl, CLSET_RETRIES, &retry_count);
127 mtx_lock(&kgss_gssd_lock);
128 oldcl = kgss_gssd_handle;
129 kgss_gssd_handle = cl;
130 mtx_unlock(&kgss_gssd_lock);
141 kgss_oid_equal(const gss_OID oid1, const gss_OID oid2)
148 if (oid1->length != oid2->length)
150 if (memcmp(oid1->elements, oid2->elements, oid1->length))
156 kgss_install_mech(gss_OID mech_type, const char *name, struct kobj_class *cls)
158 struct kgss_mech *km;
160 km = malloc(sizeof(struct kgss_mech), M_GSSAPI, M_WAITOK);
161 km->km_mech_type = mech_type;
162 km->km_mech_name = name;
164 LIST_INSERT_HEAD(&kgss_mechs, km, km_link);
168 kgss_uninstall_mech(gss_OID mech_type)
170 struct kgss_mech *km;
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);
182 kgss_find_mech_by_name(const char *name)
184 struct kgss_mech *km;
186 LIST_FOREACH(km, &kgss_mechs, km_link) {
187 if (!strcmp(km->km_mech_name, name)) {
188 return (km->km_mech_type);
191 return (GSS_C_NO_OID);
195 kgss_find_mech_by_oid(const gss_OID oid)
197 struct kgss_mech *km;
199 LIST_FOREACH(km, &kgss_mechs, km_link) {
200 if (kgss_oid_equal(km->km_mech_type, oid)) {
201 return (km->km_mech_name);
208 kgss_create_context(gss_OID mech_type)
210 struct kgss_mech *km;
213 LIST_FOREACH(km, &kgss_mechs, km_link) {
214 if (kgss_oid_equal(km->km_mech_type, mech_type))
220 ctx = (gss_ctx_id_t) kobj_create(km->km_class, M_GSSAPI, M_WAITOK);
227 kgss_delete_context(gss_ctx_id_t ctx, gss_buffer_t output_token)
230 KGSS_DELETE(ctx, output_token);
231 kobj_delete((kobj_t) ctx, M_GSSAPI);
235 kgss_transfer_context(gss_ctx_id_t ctx)
237 struct export_sec_context_res res;
238 struct export_sec_context_args args;
242 if (!kgss_gssd_handle)
243 return (GSS_S_FAILURE);
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);
252 maj_stat = KGSS_IMPORT(ctx, res.format, &res.interprocess_token);
255 xdr_free((xdrproc_t) xdr_export_sec_context_res, &res);
261 kgss_copy_buffer(const gss_buffer_t from, gss_buffer_t to)
263 to->length = from->length;
265 to->value = malloc(from->length, M_GSSAPI, M_WAITOK);
266 bcopy(from->value, to->value, from->length);
273 * Acquire the kgss_gssd_handle and return it with a reference count,
274 * if it is available.
277 kgss_gssd_client(void)
281 mtx_lock(&kgss_gssd_lock);
282 cl = kgss_gssd_handle;
285 mtx_unlock(&kgss_gssd_lock);
293 kgssapi_modevent(module_t mod, int type, void *data)
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);
327 mtx_destroy(&kgss_gssd_lock);
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.
339 static moduledata_t kgssapi_mod = {
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);