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, SY_THR_STATIC_KLD);
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);
107 if (strlen(path) + 1 > sizeof(sun.sun_path))
110 if (path[0] != '\0') {
111 sun.sun_family = AF_LOCAL;
112 strlcpy(sun.sun_path, path, sizeof(sun.sun_path));
113 sun.sun_len = SUN_LEN(&sun);
115 nconf = getnetconfigent("local");
116 cl = clnt_reconnect_create(nconf,
117 (struct sockaddr *) &sun, GSSD, GSSDVERS,
118 RPC_MAXDATASIZE, RPC_MAXDATASIZE);
120 * The number of retries defaults to INT_MAX, which effectively
121 * means an infinite, uninterruptable loop. Limiting it to
122 * five retries keeps it from running forever.
126 CLNT_CONTROL(cl, CLSET_RETRIES, &retry_count);
131 mtx_lock(&kgss_gssd_lock);
132 oldcl = kgss_gssd_handle;
133 kgss_gssd_handle = cl;
134 mtx_unlock(&kgss_gssd_lock);
145 kgss_oid_equal(const gss_OID oid1, const gss_OID oid2)
152 if (oid1->length != oid2->length)
154 if (memcmp(oid1->elements, oid2->elements, oid1->length))
160 kgss_install_mech(gss_OID mech_type, const char *name, struct kobj_class *cls)
162 struct kgss_mech *km;
164 km = malloc(sizeof(struct kgss_mech), M_GSSAPI, M_WAITOK);
165 km->km_mech_type = mech_type;
166 km->km_mech_name = name;
168 LIST_INSERT_HEAD(&kgss_mechs, km, km_link);
172 kgss_uninstall_mech(gss_OID mech_type)
174 struct kgss_mech *km;
176 LIST_FOREACH(km, &kgss_mechs, km_link) {
177 if (kgss_oid_equal(km->km_mech_type, mech_type)) {
178 LIST_REMOVE(km, km_link);
186 kgss_find_mech_by_name(const char *name)
188 struct kgss_mech *km;
190 LIST_FOREACH(km, &kgss_mechs, km_link) {
191 if (!strcmp(km->km_mech_name, name)) {
192 return (km->km_mech_type);
195 return (GSS_C_NO_OID);
199 kgss_find_mech_by_oid(const gss_OID oid)
201 struct kgss_mech *km;
203 LIST_FOREACH(km, &kgss_mechs, km_link) {
204 if (kgss_oid_equal(km->km_mech_type, oid)) {
205 return (km->km_mech_name);
212 kgss_create_context(gss_OID mech_type)
214 struct kgss_mech *km;
217 LIST_FOREACH(km, &kgss_mechs, km_link) {
218 if (kgss_oid_equal(km->km_mech_type, mech_type))
224 ctx = (gss_ctx_id_t) kobj_create(km->km_class, M_GSSAPI, M_WAITOK);
231 kgss_delete_context(gss_ctx_id_t ctx, gss_buffer_t output_token)
234 KGSS_DELETE(ctx, output_token);
235 kobj_delete((kobj_t) ctx, M_GSSAPI);
239 kgss_transfer_context(gss_ctx_id_t ctx)
241 struct export_sec_context_res res;
242 struct export_sec_context_args args;
246 if (!kgss_gssd_handle)
247 return (GSS_S_FAILURE);
249 args.ctx = ctx->handle;
250 bzero(&res, sizeof(res));
251 stat = gssd_export_sec_context_1(&args, &res, kgss_gssd_handle);
252 if (stat != RPC_SUCCESS) {
253 return (GSS_S_FAILURE);
256 maj_stat = KGSS_IMPORT(ctx, res.format, &res.interprocess_token);
259 xdr_free((xdrproc_t) xdr_export_sec_context_res, &res);
265 kgss_copy_buffer(const gss_buffer_t from, gss_buffer_t to)
267 to->length = from->length;
269 to->value = malloc(from->length, M_GSSAPI, M_WAITOK);
270 bcopy(from->value, to->value, from->length);
277 * Acquire the kgss_gssd_handle and return it with a reference count,
278 * if it is available.
281 kgss_gssd_client(void)
285 mtx_lock(&kgss_gssd_lock);
286 cl = kgss_gssd_handle;
289 mtx_unlock(&kgss_gssd_lock);
297 kgssapi_modevent(module_t mod, int type, void *data)
303 rpc_gss_entries.rpc_gss_refresh_auth = rpc_gss_refresh_auth;
304 rpc_gss_entries.rpc_gss_secfind = rpc_gss_secfind;
305 rpc_gss_entries.rpc_gss_secpurge = rpc_gss_secpurge;
306 rpc_gss_entries.rpc_gss_seccreate = rpc_gss_seccreate;
307 rpc_gss_entries.rpc_gss_set_defaults = rpc_gss_set_defaults;
308 rpc_gss_entries.rpc_gss_max_data_length =
309 rpc_gss_max_data_length;
310 rpc_gss_entries.rpc_gss_get_error = rpc_gss_get_error;
311 rpc_gss_entries.rpc_gss_mech_to_oid = rpc_gss_mech_to_oid;
312 rpc_gss_entries.rpc_gss_oid_to_mech = rpc_gss_oid_to_mech;
313 rpc_gss_entries.rpc_gss_qop_to_num = rpc_gss_qop_to_num;
314 rpc_gss_entries.rpc_gss_get_mechanisms = rpc_gss_get_mechanisms;
315 rpc_gss_entries.rpc_gss_get_versions = rpc_gss_get_versions;
316 rpc_gss_entries.rpc_gss_is_installed = rpc_gss_is_installed;
317 rpc_gss_entries.rpc_gss_set_svc_name = rpc_gss_set_svc_name;
318 rpc_gss_entries.rpc_gss_clear_svc_name = rpc_gss_clear_svc_name;
319 rpc_gss_entries.rpc_gss_getcred = rpc_gss_getcred;
320 rpc_gss_entries.rpc_gss_set_callback = rpc_gss_set_callback;
321 rpc_gss_entries.rpc_gss_clear_callback = rpc_gss_clear_callback;
322 rpc_gss_entries.rpc_gss_get_principal_name =
323 rpc_gss_get_principal_name;
324 rpc_gss_entries.rpc_gss_svc_max_data_length =
325 rpc_gss_svc_max_data_length;
326 mtx_init(&kgss_gssd_lock, "kgss_gssd_lock", NULL, MTX_DEF);
330 * Unloading of the kgssapi module is not currently supported.
331 * If somebody wants this, we would need to keep track of
332 * currently executing threads and make sure the count is 0.
340 static moduledata_t kgssapi_mod = {
345 DECLARE_MODULE(kgssapi, kgssapi_mod, SI_SUB_VFS, SI_ORDER_ANY);
346 MODULE_DEPEND(kgssapi, krpc, 1, 1, 1);
347 MODULE_VERSION(kgssapi, 1);