]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/kgssapi/gss_impl.c
Update our devicetree to 4.19 for arm and arm64
[FreeBSD/FreeBSD.git] / sys / kgssapi / gss_impl.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2008 Isilon Inc http://www.isilon.com/
5  * Authors: Doug Rabson <dfr@rabson.org>
6  * Developed with Red Inc: Alfred Perlstein <alfred@freebsd.org>
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32
33 #include <sys/param.h>
34 #include <sys/kernel.h>
35 #include <sys/kobj.h>
36 #include <sys/lock.h>
37 #include <sys/malloc.h>
38 #include <sys/module.h>
39 #include <sys/mutex.h>
40 #include <sys/priv.h>
41 #include <sys/syscall.h>
42 #include <sys/sysent.h>
43 #include <sys/sysproto.h>
44
45 #include <kgssapi/gssapi.h>
46 #include <kgssapi/gssapi_impl.h>
47 #include <rpc/rpc.h>
48 #include <rpc/rpc_com.h>
49 #include <rpc/rpcsec_gss.h>
50
51 #include "gssd.h"
52 #include "kgss_if.h"
53
54 MALLOC_DEFINE(M_GSSAPI, "GSS-API", "GSS-API");
55
56 /*
57  * Syscall hooks
58  */
59 static struct syscall_helper_data gssd_syscalls[] = {
60         SYSCALL_INIT_HELPER(gssd_syscall),
61         SYSCALL_INIT_LAST
62 };
63
64 struct kgss_mech_list kgss_mechs;
65 CLIENT *kgss_gssd_handle;
66 struct mtx kgss_gssd_lock;
67
68 static int
69 kgss_load(void)
70 {
71         int error;
72
73         LIST_INIT(&kgss_mechs);
74         error = syscall_helper_register(gssd_syscalls, SY_THR_STATIC_KLD);
75         if (error != 0)
76                 return (error);
77         return (0);
78 }
79
80 static void
81 kgss_unload(void)
82 {
83
84         syscall_helper_unregister(gssd_syscalls);
85 }
86
87 int
88 sys_gssd_syscall(struct thread *td, struct gssd_syscall_args *uap)
89 {
90         struct sockaddr_un sun;
91         struct netconfig *nconf;
92         char path[MAXPATHLEN];
93         int error;
94         CLIENT *cl, *oldcl;
95         
96         error = priv_check(td, PRIV_NFS_DAEMON);
97         if (error)
98                 return (error);
99
100         error = copyinstr(uap->path, path, sizeof(path), NULL);
101         if (error)
102                 return (error);
103         if (strlen(path) + 1 > sizeof(sun.sun_path))
104                 return (EINVAL);
105
106         if (path[0] != '\0') {
107                 sun.sun_family = AF_LOCAL;
108                 strlcpy(sun.sun_path, path, sizeof(sun.sun_path));
109                 sun.sun_len = SUN_LEN(&sun);
110                 
111                 nconf = getnetconfigent("local");
112                 cl = clnt_reconnect_create(nconf,
113                     (struct sockaddr *) &sun, GSSD, GSSDVERS,
114                     RPC_MAXDATASIZE, RPC_MAXDATASIZE);
115         } else
116                 cl = NULL;
117
118         mtx_lock(&kgss_gssd_lock);
119         oldcl = kgss_gssd_handle;
120         kgss_gssd_handle = cl;
121         mtx_unlock(&kgss_gssd_lock);
122
123         if (oldcl != NULL) {
124                 CLNT_CLOSE(oldcl);
125                 CLNT_RELEASE(oldcl);
126         }
127
128         return (0);
129 }
130
131 int
132 kgss_oid_equal(const gss_OID oid1, const gss_OID oid2)
133 {
134
135         if (oid1 == oid2)
136                 return (1);
137         if (!oid1 || !oid2)
138                 return (0);
139         if (oid1->length != oid2->length)
140                 return (0);
141         if (memcmp(oid1->elements, oid2->elements, oid1->length))
142                 return (0);
143         return (1);
144 }
145
146 void
147 kgss_install_mech(gss_OID mech_type, const char *name, struct kobj_class *cls)
148 {
149         struct kgss_mech *km;
150
151         km = malloc(sizeof(struct kgss_mech), M_GSSAPI, M_WAITOK);
152         km->km_mech_type = mech_type;
153         km->km_mech_name = name;
154         km->km_class = cls;
155         LIST_INSERT_HEAD(&kgss_mechs, km, km_link);
156 }
157
158 void
159 kgss_uninstall_mech(gss_OID mech_type)
160 {
161         struct kgss_mech *km;
162
163         LIST_FOREACH(km, &kgss_mechs, km_link) {
164                 if (kgss_oid_equal(km->km_mech_type, mech_type)) {
165                         LIST_REMOVE(km, km_link);
166                         free(km, M_GSSAPI);
167                         return;
168                 }
169         }
170 }
171
172 gss_OID
173 kgss_find_mech_by_name(const char *name)
174 {
175         struct kgss_mech *km;
176
177         LIST_FOREACH(km, &kgss_mechs, km_link) {
178                 if (!strcmp(km->km_mech_name, name)) {
179                         return (km->km_mech_type);
180                 }
181         }
182         return (GSS_C_NO_OID);
183 }
184
185 const char *
186 kgss_find_mech_by_oid(const gss_OID oid)
187 {
188         struct kgss_mech *km;
189
190         LIST_FOREACH(km, &kgss_mechs, km_link) {
191                 if (kgss_oid_equal(km->km_mech_type, oid)) {
192                         return (km->km_mech_name);
193                 }
194         }
195         return (NULL);
196 }
197
198 gss_ctx_id_t
199 kgss_create_context(gss_OID mech_type)
200 {
201         struct kgss_mech *km;
202         gss_ctx_id_t ctx;
203
204         LIST_FOREACH(km, &kgss_mechs, km_link) {
205                 if (kgss_oid_equal(km->km_mech_type, mech_type))
206                         break;
207         }
208         if (!km)
209                 return (NULL);
210
211         ctx = (gss_ctx_id_t) kobj_create(km->km_class, M_GSSAPI, M_WAITOK);
212         KGSS_INIT(ctx);
213
214         return (ctx);
215 }
216
217 void
218 kgss_delete_context(gss_ctx_id_t ctx, gss_buffer_t output_token)
219 {
220
221         KGSS_DELETE(ctx, output_token);
222         kobj_delete((kobj_t) ctx, M_GSSAPI);
223 }
224
225 OM_uint32
226 kgss_transfer_context(gss_ctx_id_t ctx)
227 {
228         struct export_sec_context_res res;
229         struct export_sec_context_args args;
230         enum clnt_stat stat;
231         OM_uint32 maj_stat;
232
233         if (!kgss_gssd_handle)
234                 return (GSS_S_FAILURE);
235
236         args.ctx = ctx->handle;
237         bzero(&res, sizeof(res));
238         stat = gssd_export_sec_context_1(&args, &res, kgss_gssd_handle);
239         if (stat != RPC_SUCCESS) {
240                 return (GSS_S_FAILURE);
241         }
242
243         maj_stat = KGSS_IMPORT(ctx, res.format, &res.interprocess_token);
244         ctx->handle = 0;
245
246         xdr_free((xdrproc_t) xdr_export_sec_context_res, &res);
247
248         return (maj_stat);
249 }
250
251 void
252 kgss_copy_buffer(const gss_buffer_t from, gss_buffer_t to)
253 {
254         to->length = from->length;
255         if (from->length) {
256                 to->value = malloc(from->length, M_GSSAPI, M_WAITOK);
257                 bcopy(from->value, to->value, from->length);
258         } else {
259                 to->value = NULL;
260         }
261 }
262
263 /*
264  * Acquire the kgss_gssd_handle and return it with a reference count,
265  * if it is available.
266  */
267 CLIENT *
268 kgss_gssd_client(void)
269 {
270         CLIENT *cl;
271
272         mtx_lock(&kgss_gssd_lock);
273         cl = kgss_gssd_handle;
274         if (cl != NULL)
275                 CLNT_ACQUIRE(cl);
276         mtx_unlock(&kgss_gssd_lock);
277         return (cl);
278 }
279
280 /*
281  * Kernel module glue
282  */
283 static int
284 kgssapi_modevent(module_t mod, int type, void *data)
285 {
286         int error = 0;
287
288         switch (type) {
289         case MOD_LOAD:
290                 rpc_gss_entries.rpc_gss_refresh_auth = rpc_gss_refresh_auth;
291                 rpc_gss_entries.rpc_gss_secfind = rpc_gss_secfind;
292                 rpc_gss_entries.rpc_gss_secpurge = rpc_gss_secpurge;
293                 rpc_gss_entries.rpc_gss_seccreate = rpc_gss_seccreate;
294                 rpc_gss_entries.rpc_gss_set_defaults = rpc_gss_set_defaults;
295                 rpc_gss_entries.rpc_gss_max_data_length =
296                     rpc_gss_max_data_length;
297                 rpc_gss_entries.rpc_gss_get_error = rpc_gss_get_error;
298                 rpc_gss_entries.rpc_gss_mech_to_oid = rpc_gss_mech_to_oid;
299                 rpc_gss_entries.rpc_gss_oid_to_mech = rpc_gss_oid_to_mech;
300                 rpc_gss_entries.rpc_gss_qop_to_num = rpc_gss_qop_to_num;
301                 rpc_gss_entries.rpc_gss_get_mechanisms = rpc_gss_get_mechanisms;
302                 rpc_gss_entries.rpc_gss_get_versions = rpc_gss_get_versions;
303                 rpc_gss_entries.rpc_gss_is_installed = rpc_gss_is_installed;
304                 rpc_gss_entries.rpc_gss_set_svc_name = rpc_gss_set_svc_name;
305                 rpc_gss_entries.rpc_gss_clear_svc_name = rpc_gss_clear_svc_name;
306                 rpc_gss_entries.rpc_gss_getcred = rpc_gss_getcred;
307                 rpc_gss_entries.rpc_gss_set_callback = rpc_gss_set_callback;
308                 rpc_gss_entries.rpc_gss_clear_callback = rpc_gss_clear_callback;
309                 rpc_gss_entries.rpc_gss_get_principal_name =
310                     rpc_gss_get_principal_name;
311                 rpc_gss_entries.rpc_gss_svc_max_data_length =
312                     rpc_gss_svc_max_data_length;
313                 mtx_init(&kgss_gssd_lock, "kgss_gssd_lock", NULL, MTX_DEF);
314                 error = kgss_load();
315                 break;
316         case MOD_UNLOAD:
317                 kgss_unload();
318                 mtx_destroy(&kgss_gssd_lock);
319                 /*
320                  * Unloading of the kgssapi module is not currently supported.
321                  * If somebody wants this, we would need to keep track of
322                  * currently executing threads and make sure the count is 0.
323                  */
324                 /* FALLTHROUGH */
325         default:
326                 error = EOPNOTSUPP;
327         }
328         return (error);
329 }
330 static moduledata_t kgssapi_mod = {
331         "kgssapi",
332         kgssapi_modevent,
333         NULL,
334 };
335 DECLARE_MODULE(kgssapi, kgssapi_mod, SI_SUB_VFS, SI_ORDER_ANY);
336 MODULE_DEPEND(kgssapi, krpc, 1, 1, 1);
337 MODULE_VERSION(kgssapi, 1);