1/*-
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>
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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.
14 *
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
25 * SUCH DAMAGE.
26 */
27
28#include <sys/cdefs.h>
29__FBSDID("$FreeBSD$");
30
31#include <sys/param.h>
32#include <sys/kernel.h>
33#include <sys/kobj.h>
34#include <sys/lock.h>
35#include <sys/malloc.h>
36#include <sys/module.h>
37#include <sys/mutex.h>
38#include <sys/priv.h>
39#include <sys/syscall.h>
40#include <sys/sysent.h>
41#include <sys/sysproto.h>
42
43#include <kgssapi/gssapi.h>
44#include <kgssapi/gssapi_impl.h>
45#include <rpc/rpc.h>
46#include <rpc/rpc_com.h>
47#include <rpc/rpcsec_gss.h>
48
49#include "gssd.h"
50#include "kgss_if.h"
51
52MALLOC_DEFINE(M_GSSAPI, "GSS-API", "GSS-API");
53
54/*
55 * Syscall hooks
56 */
57static int gssd_syscall_offset = SYS_gssd_syscall;
58static struct sysent gssd_syscall_prev_sysent;
59MAKE_SYSENT(gssd_syscall);
60static bool_t gssd_syscall_registered = FALSE;
61
62struct kgss_mech_list kgss_mechs;
63CLIENT *kgss_gssd_handle;
64struct mtx kgss_gssd_lock;
65
66static void
67kgss_init(void *dummy)
68{
69	int error;
70
71	LIST_INIT(&kgss_mechs);
72	error = syscall_register(&gssd_syscall_offset, &gssd_syscall_sysent,
73	    &gssd_syscall_prev_sysent);
74	if (error)
75		printf("Can't register GSSD syscall\n");
76	else
77		gssd_syscall_registered = TRUE;
78}
79SYSINIT(kgss_init, SI_SUB_LOCK, SI_ORDER_FIRST, kgss_init, NULL);
80
81static void
82kgss_uninit(void *dummy)
83{
84
85	if (gssd_syscall_registered)
86		syscall_deregister(&gssd_syscall_offset,
87		    &gssd_syscall_prev_sysent);
88}
89SYSUNINIT(kgss_uninit, SI_SUB_LOCK, SI_ORDER_FIRST, kgss_uninit, NULL);
90
91int
92sys_gssd_syscall(struct thread *td, struct gssd_syscall_args *uap)
93{
94        struct sockaddr_un sun;
95        struct netconfig *nconf;
96	char path[MAXPATHLEN];
97	int error;
98	CLIENT *cl, *oldcl;
99
100	error = priv_check(td, PRIV_NFS_DAEMON);
101	if (error)
102		return (error);
103
104	error = copyinstr(uap->path, path, sizeof(path), NULL);
105	if (error)
106		return (error);
107	if (strlen(path) + 1 > sizeof(sun.sun_path))
108		return (EINVAL);
109
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);
114
115		nconf = getnetconfigent("local");
116		cl = clnt_reconnect_create(nconf,
117		    (struct sockaddr *) &sun, GSSD, GSSDVERS,
118		    RPC_MAXDATASIZE, RPC_MAXDATASIZE);
119	} else
120		cl = NULL;
121
122	mtx_lock(&kgss_gssd_lock);
123	oldcl = kgss_gssd_handle;
124	kgss_gssd_handle = cl;
125	mtx_unlock(&kgss_gssd_lock);
126
127	if (oldcl != NULL) {
128		CLNT_CLOSE(oldcl);
129		CLNT_RELEASE(oldcl);
130	}
131
132	return (0);
133}
134
135int
136kgss_oid_equal(const gss_OID oid1, const gss_OID oid2)
137{
138
139	if (oid1 == oid2)
140		return (1);
141	if (!oid1 || !oid2)
142		return (0);
143	if (oid1->length != oid2->length)
144		return (0);
145	if (memcmp(oid1->elements, oid2->elements, oid1->length))
146		return (0);
147	return (1);
148}
149
150void
151kgss_install_mech(gss_OID mech_type, const char *name, struct kobj_class *cls)
152{
153	struct kgss_mech *km;
154
155	km = malloc(sizeof(struct kgss_mech), M_GSSAPI, M_WAITOK);
156	km->km_mech_type = mech_type;
157	km->km_mech_name = name;
158	km->km_class = cls;
159	LIST_INSERT_HEAD(&kgss_mechs, km, km_link);
160}
161
162void
163kgss_uninstall_mech(gss_OID mech_type)
164{
165	struct kgss_mech *km;
166
167	LIST_FOREACH(km, &kgss_mechs, km_link) {
168		if (kgss_oid_equal(km->km_mech_type, mech_type)) {
169			LIST_REMOVE(km, km_link);
170			free(km, M_GSSAPI);
171			return;
172		}
173	}
174}
175
176gss_OID
177kgss_find_mech_by_name(const char *name)
178{
179	struct kgss_mech *km;
180
181	LIST_FOREACH(km, &kgss_mechs, km_link) {
182		if (!strcmp(km->km_mech_name, name)) {
183			return (km->km_mech_type);
184		}
185	}
186	return (GSS_C_NO_OID);
187}
188
189const char *
190kgss_find_mech_by_oid(const gss_OID oid)
191{
192	struct kgss_mech *km;
193
194	LIST_FOREACH(km, &kgss_mechs, km_link) {
195		if (kgss_oid_equal(km->km_mech_type, oid)) {
196			return (km->km_mech_name);
197		}
198	}
199	return (NULL);
200}
201
202gss_ctx_id_t
203kgss_create_context(gss_OID mech_type)
204{
205	struct kgss_mech *km;
206	gss_ctx_id_t ctx;
207
208	LIST_FOREACH(km, &kgss_mechs, km_link) {
209		if (kgss_oid_equal(km->km_mech_type, mech_type))
210			break;
211	}
212	if (!km)
213		return (NULL);
214
215	ctx = (gss_ctx_id_t) kobj_create(km->km_class, M_GSSAPI, M_WAITOK);
216	KGSS_INIT(ctx);
217
218	return (ctx);
219}
220
221void
222kgss_delete_context(gss_ctx_id_t ctx, gss_buffer_t output_token)
223{
224
225	KGSS_DELETE(ctx, output_token);
226	kobj_delete((kobj_t) ctx, M_GSSAPI);
227}
228
229OM_uint32
230kgss_transfer_context(gss_ctx_id_t ctx)
231{
232	struct export_sec_context_res res;
233	struct export_sec_context_args args;
234	enum clnt_stat stat;
235	OM_uint32 maj_stat;
236
237	if (!kgss_gssd_handle)
238		return (GSS_S_FAILURE);
239
240	args.ctx = ctx->handle;
241	bzero(&res, sizeof(res));
242	stat = gssd_export_sec_context_1(&args, &res, kgss_gssd_handle);
243	if (stat != RPC_SUCCESS) {
244		return (GSS_S_FAILURE);
245	}
246
247	maj_stat = KGSS_IMPORT(ctx, res.format, &res.interprocess_token);
248	ctx->handle = 0;
249
250	xdr_free((xdrproc_t) xdr_export_sec_context_res, &res);
251
252	return (maj_stat);
253}
254
255void
256kgss_copy_buffer(const gss_buffer_t from, gss_buffer_t to)
257{
258	to->length = from->length;
259	if (from->length) {
260		to->value = malloc(from->length, M_GSSAPI, M_WAITOK);
261		bcopy(from->value, to->value, from->length);
262	} else {
263		to->value = NULL;
264	}
265}
266
267/*
268 * Acquire the kgss_gssd_handle and return it with a reference count,
269 * if it is available.
270 */
271CLIENT *
272kgss_gssd_client(void)
273{
274	CLIENT *cl;
275
276	mtx_lock(&kgss_gssd_lock);
277	cl = kgss_gssd_handle;
278	if (cl != NULL)
279		CLNT_ACQUIRE(cl);
280	mtx_unlock(&kgss_gssd_lock);
281	return (cl);
282}
283
284/*
285 * Kernel module glue
286 */
287static int
288kgssapi_modevent(module_t mod, int type, void *data)
289{
290	int error = 0;
291
292	switch (type) {
293	case MOD_LOAD:
294		rpc_gss_entries.rpc_gss_refresh_auth = rpc_gss_refresh_auth;
295		rpc_gss_entries.rpc_gss_secfind = rpc_gss_secfind;
296		rpc_gss_entries.rpc_gss_secpurge = rpc_gss_secpurge;
297		rpc_gss_entries.rpc_gss_seccreate = rpc_gss_seccreate;
298		rpc_gss_entries.rpc_gss_set_defaults = rpc_gss_set_defaults;
299		rpc_gss_entries.rpc_gss_max_data_length =
300		    rpc_gss_max_data_length;
301		rpc_gss_entries.rpc_gss_get_error = rpc_gss_get_error;
302		rpc_gss_entries.rpc_gss_mech_to_oid = rpc_gss_mech_to_oid;
303		rpc_gss_entries.rpc_gss_oid_to_mech = rpc_gss_oid_to_mech;
304		rpc_gss_entries.rpc_gss_qop_to_num = rpc_gss_qop_to_num;
305		rpc_gss_entries.rpc_gss_get_mechanisms = rpc_gss_get_mechanisms;
306		rpc_gss_entries.rpc_gss_get_versions = rpc_gss_get_versions;
307		rpc_gss_entries.rpc_gss_is_installed = rpc_gss_is_installed;
308		rpc_gss_entries.rpc_gss_set_svc_name = rpc_gss_set_svc_name;
309		rpc_gss_entries.rpc_gss_clear_svc_name = rpc_gss_clear_svc_name;
310		rpc_gss_entries.rpc_gss_getcred = rpc_gss_getcred;
311		rpc_gss_entries.rpc_gss_set_callback = rpc_gss_set_callback;
312		rpc_gss_entries.rpc_gss_clear_callback = rpc_gss_clear_callback;
313		rpc_gss_entries.rpc_gss_get_principal_name =
314		    rpc_gss_get_principal_name;
315		rpc_gss_entries.rpc_gss_svc_max_data_length =
316		    rpc_gss_svc_max_data_length;
317		mtx_init(&kgss_gssd_lock, "kgss_gssd_lock", NULL, MTX_DEF);
318		break;
319	case MOD_UNLOAD:
320		/*
321		 * Unloading of the kgssapi module is not currently supported.
322		 * If somebody wants this, we would need to keep track of
323		 * currently executing threads and make sure the count is 0.
324		 */
325		/* FALLTHROUGH */
326	default:
327		error = EOPNOTSUPP;
328	};
329	return (error);
330}
331static moduledata_t kgssapi_mod = {
332	"kgssapi",
333	kgssapi_modevent,
334	NULL,
335};
336DECLARE_MODULE(kgssapi, kgssapi_mod, SI_SUB_VFS, SI_ORDER_ANY);
337MODULE_DEPEND(kgssapi, krpc, 1, 1, 1);
338MODULE_VERSION(kgssapi, 1);
339