2 * Copyright (c) 2008 Isilon Inc http://www.isilon.com/
3 * Authors: Doug Rabson <dfr@rabson.org>
4 * Developed with Red Inc: Alfred Perlstein <alfred@freebsd.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
32 #include <sys/kernel.h>
35 #include <sys/malloc.h>
36 #include <sys/module.h>
37 #include <sys/mutex.h>
39 #include <sys/syscall.h>
40 #include <sys/sysent.h>
41 #include <sys/sysproto.h>
43 #include <kgssapi/gssapi.h>
44 #include <kgssapi/gssapi_impl.h>
46 #include <rpc/rpc_com.h>
47 #include <rpc/rpcsec_gss.h>
52 MALLOC_DEFINE(M_GSSAPI, "GSS-API", "GSS-API");
57 static int gssd_syscall_offset = SYS_gssd_syscall;
58 static struct sysent gssd_syscall_prev_sysent;
59 MAKE_SYSENT(gssd_syscall);
60 static bool_t gssd_syscall_registered = FALSE;
62 struct kgss_mech_list kgss_mechs;
63 CLIENT *kgss_gssd_handle;
64 struct mtx kgss_gssd_lock;
67 kgss_init(void *dummy)
71 LIST_INIT(&kgss_mechs);
72 error = syscall_register(&gssd_syscall_offset, &gssd_syscall_sysent,
73 &gssd_syscall_prev_sysent);
75 printf("Can't register GSSD syscall\n");
77 gssd_syscall_registered = TRUE;
79 SYSINIT(kgss_init, SI_SUB_LOCK, SI_ORDER_FIRST, kgss_init, NULL);
82 kgss_uninit(void *dummy)
85 if (gssd_syscall_registered)
86 syscall_deregister(&gssd_syscall_offset,
87 &gssd_syscall_prev_sysent);
89 SYSUNINIT(kgss_uninit, SI_SUB_LOCK, SI_ORDER_FIRST, kgss_uninit, NULL);
92 sys_gssd_syscall(struct thread *td, struct gssd_syscall_args *uap)
94 struct sockaddr_un sun;
95 struct netconfig *nconf;
96 char path[MAXPATHLEN];
100 error = priv_check(td, PRIV_NFS_DAEMON);
104 error = copyinstr(uap->path, path, sizeof(path), NULL);
108 sun.sun_family = AF_LOCAL;
109 strcpy(sun.sun_path, path);
110 sun.sun_len = SUN_LEN(&sun);
112 nconf = getnetconfigent("local");
113 cl = clnt_reconnect_create(nconf,
114 (struct sockaddr *) &sun, GSSD, GSSDVERS,
115 RPC_MAXDATASIZE, RPC_MAXDATASIZE);
117 mtx_lock(&kgss_gssd_lock);
118 oldcl = kgss_gssd_handle;
119 kgss_gssd_handle = cl;
120 mtx_unlock(&kgss_gssd_lock);
131 kgss_oid_equal(const gss_OID oid1, const gss_OID oid2)
138 if (oid1->length != oid2->length)
140 if (memcmp(oid1->elements, oid2->elements, oid1->length))
146 kgss_install_mech(gss_OID mech_type, const char *name, struct kobj_class *cls)
148 struct kgss_mech *km;
150 km = malloc(sizeof(struct kgss_mech), M_GSSAPI, M_WAITOK);
151 km->km_mech_type = mech_type;
152 km->km_mech_name = name;
154 LIST_INSERT_HEAD(&kgss_mechs, km, km_link);
158 kgss_uninstall_mech(gss_OID mech_type)
160 struct kgss_mech *km;
162 LIST_FOREACH(km, &kgss_mechs, km_link) {
163 if (kgss_oid_equal(km->km_mech_type, mech_type)) {
164 LIST_REMOVE(km, km_link);
172 kgss_find_mech_by_name(const char *name)
174 struct kgss_mech *km;
176 LIST_FOREACH(km, &kgss_mechs, km_link) {
177 if (!strcmp(km->km_mech_name, name)) {
178 return (km->km_mech_type);
181 return (GSS_C_NO_OID);
185 kgss_find_mech_by_oid(const gss_OID oid)
187 struct kgss_mech *km;
189 LIST_FOREACH(km, &kgss_mechs, km_link) {
190 if (kgss_oid_equal(km->km_mech_type, oid)) {
191 return (km->km_mech_name);
198 kgss_create_context(gss_OID mech_type)
200 struct kgss_mech *km;
203 LIST_FOREACH(km, &kgss_mechs, km_link) {
204 if (kgss_oid_equal(km->km_mech_type, mech_type))
210 ctx = (gss_ctx_id_t) kobj_create(km->km_class, M_GSSAPI, M_WAITOK);
217 kgss_delete_context(gss_ctx_id_t ctx, gss_buffer_t output_token)
220 KGSS_DELETE(ctx, output_token);
221 kobj_delete((kobj_t) ctx, M_GSSAPI);
225 kgss_transfer_context(gss_ctx_id_t ctx)
227 struct export_sec_context_res res;
228 struct export_sec_context_args args;
232 if (!kgss_gssd_handle)
233 return (GSS_S_FAILURE);
235 args.ctx = ctx->handle;
236 bzero(&res, sizeof(res));
237 stat = gssd_export_sec_context_1(&args, &res, kgss_gssd_handle);
238 if (stat != RPC_SUCCESS) {
239 return (GSS_S_FAILURE);
242 maj_stat = KGSS_IMPORT(ctx, res.format, &res.interprocess_token);
245 xdr_free((xdrproc_t) xdr_export_sec_context_res, &res);
251 kgss_copy_buffer(const gss_buffer_t from, gss_buffer_t to)
253 to->length = from->length;
255 to->value = malloc(from->length, M_GSSAPI, M_WAITOK);
256 bcopy(from->value, to->value, from->length);
263 * Acquire the kgss_gssd_handle and return it with a reference count,
264 * if it is available.
267 kgss_gssd_client(void)
271 mtx_lock(&kgss_gssd_lock);
272 cl = kgss_gssd_handle;
275 mtx_unlock(&kgss_gssd_lock);
283 kgssapi_modevent(module_t mod, int type, void *data)
289 rpc_gss_entries.rpc_gss_secfind = rpc_gss_secfind;
290 rpc_gss_entries.rpc_gss_secpurge = rpc_gss_secpurge;
291 rpc_gss_entries.rpc_gss_seccreate = rpc_gss_seccreate;
292 rpc_gss_entries.rpc_gss_set_defaults = rpc_gss_set_defaults;
293 rpc_gss_entries.rpc_gss_max_data_length =
294 rpc_gss_max_data_length;
295 rpc_gss_entries.rpc_gss_get_error = rpc_gss_get_error;
296 rpc_gss_entries.rpc_gss_mech_to_oid = rpc_gss_mech_to_oid;
297 rpc_gss_entries.rpc_gss_oid_to_mech = rpc_gss_oid_to_mech;
298 rpc_gss_entries.rpc_gss_qop_to_num = rpc_gss_qop_to_num;
299 rpc_gss_entries.rpc_gss_get_mechanisms = rpc_gss_get_mechanisms;
300 rpc_gss_entries.rpc_gss_get_versions = rpc_gss_get_versions;
301 rpc_gss_entries.rpc_gss_is_installed = rpc_gss_is_installed;
302 rpc_gss_entries.rpc_gss_set_svc_name = rpc_gss_set_svc_name;
303 rpc_gss_entries.rpc_gss_clear_svc_name = rpc_gss_clear_svc_name;
304 rpc_gss_entries.rpc_gss_getcred = rpc_gss_getcred;
305 rpc_gss_entries.rpc_gss_set_callback = rpc_gss_set_callback;
306 rpc_gss_entries.rpc_gss_clear_callback = rpc_gss_clear_callback;
307 rpc_gss_entries.rpc_gss_get_principal_name =
308 rpc_gss_get_principal_name;
309 rpc_gss_entries.rpc_gss_svc_max_data_length =
310 rpc_gss_svc_max_data_length;
311 mtx_init(&kgss_gssd_lock, "kgss_gssd_lock", NULL, MTX_DEF);
315 * Unloading of the kgssapi module is not currently supported.
316 * If somebody wants this, we would need to keep track of
317 * currently executing threads and make sure the count is 0.
325 static moduledata_t kgssapi_mod = {
330 DECLARE_MODULE(kgssapi, kgssapi_mod, SI_SUB_VFS, SI_ORDER_ANY);
331 MODULE_DEPEND(kgssapi, krpc, 1, 1, 1);
332 MODULE_VERSION(kgssapi, 1);