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 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 /*
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.
123 */
124 if (cl != NULL) {
125 int retry_count = 5;
126 CLNT_CONTROL(cl, CLSET_RETRIES, &retry_count);
127 }
128 } else
129 cl = NULL;
130
131 mtx_lock(&kgss_gssd_lock);
132 oldcl = kgss_gssd_handle;
133 kgss_gssd_handle = cl;
134 mtx_unlock(&kgss_gssd_lock);
135
136 if (oldcl != NULL) {
137 CLNT_CLOSE(oldcl);
138 CLNT_RELEASE(oldcl);
139 }
140
141 return (0);
142 }
143
144 int
145 kgss_oid_equal(const gss_OID oid1, const gss_OID oid2)
146 {
147
148 if (oid1 == oid2)
149 return (1);
150 if (!oid1 || !oid2)
151 return (0);
152 if (oid1->length != oid2->length)
153 return (0);
154 if (memcmp(oid1->elements, oid2->elements, oid1->length))
155 return (0);
156 return (1);
157 }
158
159 void
160 kgss_install_mech(gss_OID mech_type, const char *name, struct kobj_class *cls)
161 {
162 struct kgss_mech *km;
163
164 km = malloc(sizeof(struct kgss_mech), M_GSSAPI, M_WAITOK);
165 km->km_mech_type = mech_type;
166 km->km_mech_name = name;
167 km->km_class = cls;
168 LIST_INSERT_HEAD(&kgss_mechs, km, km_link);
169 }
170
171 void
172 kgss_uninstall_mech(gss_OID mech_type)
173 {
174 struct kgss_mech *km;
175
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);
179 free(km, M_GSSAPI);
180 return;
181 }
182 }
183 }
184
185 gss_OID
186 kgss_find_mech_by_name(const char *name)
187 {
188 struct kgss_mech *km;
189
190 LIST_FOREACH(km, &kgss_mechs, km_link) {
191 if (!strcmp(km->km_mech_name, name)) {
192 return (km->km_mech_type);
193 }
194 }
195 return (GSS_C_NO_OID);
196 }
197
198 const char *
199 kgss_find_mech_by_oid(const gss_OID oid)
200 {
201 struct kgss_mech *km;
202
203 LIST_FOREACH(km, &kgss_mechs, km_link) {
204 if (kgss_oid_equal(km->km_mech_type, oid)) {
205 return (km->km_mech_name);
206 }
207 }
208 return (NULL);
209 }
210
211 gss_ctx_id_t
212 kgss_create_context(gss_OID mech_type)
213 {
214 struct kgss_mech *km;
215 gss_ctx_id_t ctx;
216
217 LIST_FOREACH(km, &kgss_mechs, km_link) {
218 if (kgss_oid_equal(km->km_mech_type, mech_type))
219 break;
220 }
221 if (!km)
222 return (NULL);
223
224 ctx = (gss_ctx_id_t) kobj_create(km->km_class, M_GSSAPI, M_WAITOK);
225 KGSS_INIT(ctx);
226
227 return (ctx);
228 }
229
230 void
231 kgss_delete_context(gss_ctx_id_t ctx, gss_buffer_t output_token)
232 {
233
234 KGSS_DELETE(ctx, output_token);
235 kobj_delete((kobj_t) ctx, M_GSSAPI);
236 }
237
238 OM_uint32
239 kgss_transfer_context(gss_ctx_id_t ctx)
240 {
241 struct export_sec_context_res res;
242 struct export_sec_context_args args;
243 enum clnt_stat stat;
244 OM_uint32 maj_stat;
245
246 if (!kgss_gssd_handle)
247 return (GSS_S_FAILURE);
248
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);
254 }
255
256 maj_stat = KGSS_IMPORT(ctx, res.format, &res.interprocess_token);
257 ctx->handle = 0;
258
259 xdr_free((xdrproc_t) xdr_export_sec_context_res, &res);
260
261 return (maj_stat);
262 }
263
264 void
265 kgss_copy_buffer(const gss_buffer_t from, gss_buffer_t to)
266 {
267 to->length = from->length;
268 if (from->length) {
269 to->value = malloc(from->length, M_GSSAPI, M_WAITOK);
270 bcopy(from->value, to->value, from->length);
271 } else {
272 to->value = NULL;
273 }
274 }
275
276 /*
277 * Acquire the kgss_gssd_handle and return it with a reference count,
278 * if it is available.
279 */
280 CLIENT *
281 kgss_gssd_client(void)
282 {
283 CLIENT *cl;
284
285 mtx_lock(&kgss_gssd_lock);
286 cl = kgss_gssd_handle;
287 if (cl != NULL)
288 CLNT_ACQUIRE(cl);
289 mtx_unlock(&kgss_gssd_lock);
290 return (cl);
291 }
292
293 /*
294 * Kernel module glue
295 */
296 static int
297 kgssapi_modevent(module_t mod, int type, void *data)
298 {
299 int error = 0;
300
301 switch (type) {
302 case MOD_LOAD:
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);
327 break;
328 case MOD_UNLOAD:
329 /*
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.
333 */
334 /* FALLTHROUGH */
335 default:
336 error = EOPNOTSUPP;
337 }
338 return (error);
339 }
340 static moduledata_t kgssapi_mod = {
341 "kgssapi",
342 kgssapi_modevent,
343 NULL,
344 };
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);
Cache object: e59dc23d8968afbce0f4fbc14348fe65
|