xref: /freebsd/sys/kgssapi/gss_impl.c (revision 39ee7a7a6bdd1557b1c3532abf60d139798ac88b)
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 
52 MALLOC_DEFINE(M_GSSAPI, "GSS-API", "GSS-API");
53 
54 /*
55  * Syscall hooks
56  */
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;
61 
62 struct kgss_mech_list kgss_mechs;
63 CLIENT *kgss_gssd_handle;
64 struct mtx kgss_gssd_lock;
65 
66 static void
67 kgss_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, SY_THR_STATIC_KLD);
74 	if (error)
75 		printf("Can't register GSSD syscall\n");
76 	else
77 		gssd_syscall_registered = TRUE;
78 }
79 SYSINIT(kgss_init, SI_SUB_LOCK, SI_ORDER_FIRST, kgss_init, NULL);
80 
81 static void
82 kgss_uninit(void *dummy)
83 {
84 
85 	if (gssd_syscall_registered)
86 		syscall_deregister(&gssd_syscall_offset,
87 		    &gssd_syscall_prev_sysent);
88 }
89 SYSUNINIT(kgss_uninit, SI_SUB_LOCK, SI_ORDER_FIRST, kgss_uninit, NULL);
90 
91 int
92 sys_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 
108         sun.sun_family = AF_LOCAL;
109         strcpy(sun.sun_path, 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 	mtx_lock(&kgss_gssd_lock);
118 	oldcl = kgss_gssd_handle;
119 	kgss_gssd_handle = cl;
120 	mtx_unlock(&kgss_gssd_lock);
121 
122 	if (oldcl != NULL) {
123 		CLNT_CLOSE(oldcl);
124 		CLNT_RELEASE(oldcl);
125 	}
126 
127 	return (0);
128 }
129 
130 int
131 kgss_oid_equal(const gss_OID oid1, const gss_OID oid2)
132 {
133 
134 	if (oid1 == oid2)
135 		return (1);
136 	if (!oid1 || !oid2)
137 		return (0);
138 	if (oid1->length != oid2->length)
139 		return (0);
140 	if (memcmp(oid1->elements, oid2->elements, oid1->length))
141 		return (0);
142 	return (1);
143 }
144 
145 void
146 kgss_install_mech(gss_OID mech_type, const char *name, struct kobj_class *cls)
147 {
148 	struct kgss_mech *km;
149 
150 	km = malloc(sizeof(struct kgss_mech), M_GSSAPI, M_WAITOK);
151 	km->km_mech_type = mech_type;
152 	km->km_mech_name = name;
153 	km->km_class = cls;
154 	LIST_INSERT_HEAD(&kgss_mechs, km, km_link);
155 }
156 
157 void
158 kgss_uninstall_mech(gss_OID mech_type)
159 {
160 	struct kgss_mech *km;
161 
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);
165 			free(km, M_GSSAPI);
166 			return;
167 		}
168 	}
169 }
170 
171 gss_OID
172 kgss_find_mech_by_name(const char *name)
173 {
174 	struct kgss_mech *km;
175 
176 	LIST_FOREACH(km, &kgss_mechs, km_link) {
177 		if (!strcmp(km->km_mech_name, name)) {
178 			return (km->km_mech_type);
179 		}
180 	}
181 	return (GSS_C_NO_OID);
182 }
183 
184 const char *
185 kgss_find_mech_by_oid(const gss_OID oid)
186 {
187 	struct kgss_mech *km;
188 
189 	LIST_FOREACH(km, &kgss_mechs, km_link) {
190 		if (kgss_oid_equal(km->km_mech_type, oid)) {
191 			return (km->km_mech_name);
192 		}
193 	}
194 	return (NULL);
195 }
196 
197 gss_ctx_id_t
198 kgss_create_context(gss_OID mech_type)
199 {
200 	struct kgss_mech *km;
201 	gss_ctx_id_t ctx;
202 
203 	LIST_FOREACH(km, &kgss_mechs, km_link) {
204 		if (kgss_oid_equal(km->km_mech_type, mech_type))
205 			break;
206 	}
207 	if (!km)
208 		return (NULL);
209 
210 	ctx = (gss_ctx_id_t) kobj_create(km->km_class, M_GSSAPI, M_WAITOK);
211 	KGSS_INIT(ctx);
212 
213 	return (ctx);
214 }
215 
216 void
217 kgss_delete_context(gss_ctx_id_t ctx, gss_buffer_t output_token)
218 {
219 
220 	KGSS_DELETE(ctx, output_token);
221 	kobj_delete((kobj_t) ctx, M_GSSAPI);
222 }
223 
224 OM_uint32
225 kgss_transfer_context(gss_ctx_id_t ctx)
226 {
227 	struct export_sec_context_res res;
228 	struct export_sec_context_args args;
229 	enum clnt_stat stat;
230 	OM_uint32 maj_stat;
231 
232 	if (!kgss_gssd_handle)
233 		return (GSS_S_FAILURE);
234 
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);
240 	}
241 
242 	maj_stat = KGSS_IMPORT(ctx, res.format, &res.interprocess_token);
243 	ctx->handle = 0;
244 
245 	xdr_free((xdrproc_t) xdr_export_sec_context_res, &res);
246 
247 	return (maj_stat);
248 }
249 
250 void
251 kgss_copy_buffer(const gss_buffer_t from, gss_buffer_t to)
252 {
253 	to->length = from->length;
254 	if (from->length) {
255 		to->value = malloc(from->length, M_GSSAPI, M_WAITOK);
256 		bcopy(from->value, to->value, from->length);
257 	} else {
258 		to->value = NULL;
259 	}
260 }
261 
262 /*
263  * Acquire the kgss_gssd_handle and return it with a reference count,
264  * if it is available.
265  */
266 CLIENT *
267 kgss_gssd_client(void)
268 {
269 	CLIENT *cl;
270 
271 	mtx_lock(&kgss_gssd_lock);
272 	cl = kgss_gssd_handle;
273 	if (cl != NULL)
274 		CLNT_ACQUIRE(cl);
275 	mtx_unlock(&kgss_gssd_lock);
276 	return (cl);
277 }
278 
279 /*
280  * Kernel module glue
281  */
282 static int
283 kgssapi_modevent(module_t mod, int type, void *data)
284 {
285 	int error = 0;
286 
287 	switch (type) {
288 	case MOD_LOAD:
289 		rpc_gss_entries.rpc_gss_refresh_auth = rpc_gss_refresh_auth;
290 		rpc_gss_entries.rpc_gss_secfind = rpc_gss_secfind;
291 		rpc_gss_entries.rpc_gss_secpurge = rpc_gss_secpurge;
292 		rpc_gss_entries.rpc_gss_seccreate = rpc_gss_seccreate;
293 		rpc_gss_entries.rpc_gss_set_defaults = rpc_gss_set_defaults;
294 		rpc_gss_entries.rpc_gss_max_data_length =
295 		    rpc_gss_max_data_length;
296 		rpc_gss_entries.rpc_gss_get_error = rpc_gss_get_error;
297 		rpc_gss_entries.rpc_gss_mech_to_oid = rpc_gss_mech_to_oid;
298 		rpc_gss_entries.rpc_gss_oid_to_mech = rpc_gss_oid_to_mech;
299 		rpc_gss_entries.rpc_gss_qop_to_num = rpc_gss_qop_to_num;
300 		rpc_gss_entries.rpc_gss_get_mechanisms = rpc_gss_get_mechanisms;
301 		rpc_gss_entries.rpc_gss_get_versions = rpc_gss_get_versions;
302 		rpc_gss_entries.rpc_gss_is_installed = rpc_gss_is_installed;
303 		rpc_gss_entries.rpc_gss_set_svc_name = rpc_gss_set_svc_name;
304 		rpc_gss_entries.rpc_gss_clear_svc_name = rpc_gss_clear_svc_name;
305 		rpc_gss_entries.rpc_gss_getcred = rpc_gss_getcred;
306 		rpc_gss_entries.rpc_gss_set_callback = rpc_gss_set_callback;
307 		rpc_gss_entries.rpc_gss_clear_callback = rpc_gss_clear_callback;
308 		rpc_gss_entries.rpc_gss_get_principal_name =
309 		    rpc_gss_get_principal_name;
310 		rpc_gss_entries.rpc_gss_svc_max_data_length =
311 		    rpc_gss_svc_max_data_length;
312 		mtx_init(&kgss_gssd_lock, "kgss_gssd_lock", NULL, MTX_DEF);
313 		break;
314 	case MOD_UNLOAD:
315 		/*
316 		 * Unloading of the kgssapi module is not currently supported.
317 		 * If somebody wants this, we would need to keep track of
318 		 * currently executing threads and make sure the count is 0.
319 		 */
320 		/* FALLTHROUGH */
321 	default:
322 		error = EOPNOTSUPP;
323 	};
324 	return (error);
325 }
326 static moduledata_t kgssapi_mod = {
327 	"kgssapi",
328 	kgssapi_modevent,
329 	NULL,
330 };
331 DECLARE_MODULE(kgssapi, kgssapi_mod, SI_SUB_VFS, SI_ORDER_ANY);
332 MODULE_DEPEND(kgssapi, krpc, 1, 1, 1);
333 MODULE_VERSION(kgssapi, 1);
334