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