1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
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/param.h>
31 #include <sys/kernel.h>
32 #include <sys/kobj.h>
33 #include <sys/lock.h>
34 #include <sys/malloc.h>
35 #include <sys/module.h>
36 #include <sys/mutex.h>
37 #include <sys/priv.h>
38 #include <sys/proc.h>
39
40 #include <kgssapi/gssapi.h>
41 #include <kgssapi/gssapi_impl.h>
42 #include <rpc/rpc.h>
43 #include <rpc/rpc_com.h>
44 #include <rpc/rpcsec_gss.h>
45
46 #include "gssd.h"
47 #include "kgss_if.h"
48
49 MALLOC_DEFINE(M_GSSAPI, "GSS-API", "GSS-API");
50
51 struct kgss_mech_list kgss_mechs;
52 struct mtx kgss_gssd_lock;
53
54 CLIENT *kgss_gssd_handle;
55
56 static int
kgss_load(void)57 kgss_load(void)
58 {
59 CLIENT *cl;
60 struct timeval nulltimo = {.tv_sec = 0, .tv_usec = 200000};
61
62 LIST_INIT(&kgss_mechs);
63
64 cl = client_nl_create("kgss", GSSD, GSSDVERS);
65 KASSERT(cl, ("%s: netlink client already exist", __func__));
66
67 /*
68 * The transport default is no retries at all, since there could
69 * be no userland listener to our messages. Initially, a short timeout
70 * is set, so that a Null RPC upcall will fail quickly if the daemon
71 * is not running. After the Null RPC succeeds, we will retry for 5
72 * minutes with 10 second interval. This will potentially cure hosts
73 * with misconfigured startup, where kernel starts sending GSS queries
74 * before userland had started up the gssd(8) daemon.
75 * The initial timeout of 200usec is for the Null RPC.
76 */
77 clnt_control(cl, CLSET_RETRIES, &(int){30});
78 clnt_control(cl, CLSET_TIMEOUT, &nulltimo);
79
80 /*
81 * We literally wait on gssd(8), let's see that in top(1).
82 */
83 clnt_control(cl, CLSET_WAITCHAN, "gssd");
84
85 mtx_lock(&kgss_gssd_lock);
86 kgss_gssd_handle = cl;
87 mtx_unlock(&kgss_gssd_lock);
88
89 return (0);
90 }
91
92 #if 0
93 static void
94 kgss_unload(void)
95 {
96
97 clnt_destroy(kgss_gssd_handle);
98 }
99 #endif
100
101 int
kgss_oid_equal(const gss_OID oid1,const gss_OID oid2)102 kgss_oid_equal(const gss_OID oid1, const gss_OID oid2)
103 {
104
105 if (oid1 == oid2)
106 return (1);
107 if (!oid1 || !oid2)
108 return (0);
109 if (oid1->length != oid2->length)
110 return (0);
111 if (memcmp(oid1->elements, oid2->elements, oid1->length))
112 return (0);
113 return (1);
114 }
115
116 void
kgss_install_mech(gss_OID mech_type,const char * name,struct kobj_class * cls)117 kgss_install_mech(gss_OID mech_type, const char *name, struct kobj_class *cls)
118 {
119 struct kgss_mech *km;
120
121 km = malloc(sizeof(struct kgss_mech), M_GSSAPI, M_WAITOK);
122 km->km_mech_type = mech_type;
123 km->km_mech_name = name;
124 km->km_class = cls;
125 LIST_INSERT_HEAD(&kgss_mechs, km, km_link);
126 }
127
128 void
kgss_uninstall_mech(gss_OID mech_type)129 kgss_uninstall_mech(gss_OID mech_type)
130 {
131 struct kgss_mech *km;
132
133 LIST_FOREACH(km, &kgss_mechs, km_link) {
134 if (kgss_oid_equal(km->km_mech_type, mech_type)) {
135 LIST_REMOVE(km, km_link);
136 free(km, M_GSSAPI);
137 return;
138 }
139 }
140 }
141
142 gss_OID
kgss_find_mech_by_name(const char * name)143 kgss_find_mech_by_name(const char *name)
144 {
145 struct kgss_mech *km;
146
147 LIST_FOREACH(km, &kgss_mechs, km_link) {
148 if (!strcmp(km->km_mech_name, name)) {
149 return (km->km_mech_type);
150 }
151 }
152 return (GSS_C_NO_OID);
153 }
154
155 const char *
kgss_find_mech_by_oid(const gss_OID oid)156 kgss_find_mech_by_oid(const gss_OID oid)
157 {
158 struct kgss_mech *km;
159
160 LIST_FOREACH(km, &kgss_mechs, km_link) {
161 if (kgss_oid_equal(km->km_mech_type, oid)) {
162 return (km->km_mech_name);
163 }
164 }
165 return (NULL);
166 }
167
168 gss_ctx_id_t
kgss_create_context(gss_OID mech_type)169 kgss_create_context(gss_OID mech_type)
170 {
171 struct kgss_mech *km;
172 gss_ctx_id_t ctx;
173
174 LIST_FOREACH(km, &kgss_mechs, km_link) {
175 if (kgss_oid_equal(km->km_mech_type, mech_type))
176 break;
177 }
178 if (!km)
179 return (NULL);
180
181 ctx = (gss_ctx_id_t) kobj_create(km->km_class, M_GSSAPI, M_WAITOK);
182 KGSS_INIT(ctx);
183
184 return (ctx);
185 }
186
187 void
kgss_delete_context(gss_ctx_id_t ctx,gss_buffer_t output_token)188 kgss_delete_context(gss_ctx_id_t ctx, gss_buffer_t output_token)
189 {
190
191 KGSS_DELETE(ctx, output_token);
192 kobj_delete((kobj_t) ctx, M_GSSAPI);
193 }
194
195 OM_uint32
kgss_transfer_context(gss_ctx_id_t ctx,void * lctx)196 kgss_transfer_context(gss_ctx_id_t ctx, void *lctx)
197 {
198 struct export_sec_context_res res;
199 struct export_sec_context_args args;
200 enum clnt_stat stat;
201 OM_uint32 maj_stat;
202
203 if (lctx != NULL) {
204 maj_stat = KGSS_IMPORT(ctx, MIT_V1, lctx);
205 ctx->handle = 0;
206 return (maj_stat);
207 }
208
209 args.ctx = ctx->handle;
210 bzero(&res, sizeof(res));
211 stat = gssd_export_sec_context_1(&args, &res, kgss_gssd_handle);
212 if (stat != RPC_SUCCESS) {
213 return (GSS_S_FAILURE);
214 }
215
216 maj_stat = KGSS_IMPORT(ctx, res.format, &res.interprocess_token);
217 ctx->handle = 0;
218
219 xdr_free((xdrproc_t) xdr_export_sec_context_res, &res);
220
221 return (maj_stat);
222 }
223
224 void
kgss_copy_buffer(const gss_buffer_t from,gss_buffer_t to)225 kgss_copy_buffer(const gss_buffer_t from, gss_buffer_t to)
226 {
227 to->length = from->length;
228 if (from->length) {
229 to->value = malloc(from->length, M_GSSAPI, M_WAITOK);
230 bcopy(from->value, to->value, from->length);
231 } else {
232 to->value = NULL;
233 }
234 }
235
236 /*
237 * Acquire the kgss_gssd_handle and return it with a reference count,
238 * if it is available.
239 */
240 CLIENT *
kgss_gssd_client(void)241 kgss_gssd_client(void)
242 {
243 CLIENT *cl;
244 struct rpc_callextra ext;
245 enum clnt_stat stat;
246 struct timeval rpctimo = {.tv_sec = 300, .tv_usec = 0};
247 static time_t nextrpc = 0;
248 static bool done_upcall = false;
249
250 mtx_lock(&kgss_gssd_lock);
251 cl = kgss_gssd_handle;
252 if (!done_upcall) {
253 mtx_unlock(&kgss_gssd_lock);
254 /*
255 * Can only be NULL if client_nl_create() called from
256 * kgss_load() returns NULL.
257 */
258 if (cl == NULL)
259 return (cl);
260 if (time_uptime < nextrpc)
261 return (NULL);
262 /*
263 * Do a Null RPC with a short timeout. If the RPC succeeds,
264 * the gssd daemon is running. If the Null RPC fails, don't
265 * again for 5sec.
266 */
267 memset(&ext, 0, sizeof(ext));
268 ext.rc_auth = authnone_create();
269 stat = clnt_call_private(cl, &ext, NULLPROC,
270 (xdrproc_t)xdr_void, NULL, (xdrproc_t)xdr_void, NULL,
271 rpctimo); /* rpctimo is ignored by clnt_nl_call(). */
272 AUTH_DESTROY(ext.rc_auth);
273 if (stat != RPC_SUCCESS) {
274 nextrpc = time_uptime + 5;
275 return (NULL);
276 }
277 CLNT_CONTROL(cl, CLSET_TIMEOUT, &rpctimo);
278 mtx_lock(&kgss_gssd_lock);
279 done_upcall = true;
280 }
281 if (cl != NULL)
282 CLNT_ACQUIRE(cl);
283 mtx_unlock(&kgss_gssd_lock);
284 return (cl);
285 }
286
287 /*
288 * Kernel module glue
289 */
290 static int
kgssapi_modevent(module_t mod,int type,void * data)291 kgssapi_modevent(module_t mod, int type, void *data)
292 {
293 int error = 0;
294
295 switch (type) {
296 case MOD_LOAD:
297 rpc_gss_entries.rpc_gss_refresh_auth = rpc_gss_refresh_auth;
298 rpc_gss_entries.rpc_gss_secfind = rpc_gss_secfind;
299 rpc_gss_entries.rpc_gss_secpurge = rpc_gss_secpurge;
300 rpc_gss_entries.rpc_gss_seccreate = rpc_gss_seccreate;
301 rpc_gss_entries.rpc_gss_set_defaults = rpc_gss_set_defaults;
302 rpc_gss_entries.rpc_gss_max_data_length =
303 rpc_gss_max_data_length;
304 rpc_gss_entries.rpc_gss_get_error = rpc_gss_get_error;
305 rpc_gss_entries.rpc_gss_mech_to_oid = rpc_gss_mech_to_oid;
306 rpc_gss_entries.rpc_gss_oid_to_mech = rpc_gss_oid_to_mech;
307 rpc_gss_entries.rpc_gss_qop_to_num = rpc_gss_qop_to_num;
308 rpc_gss_entries.rpc_gss_get_mechanisms = rpc_gss_get_mechanisms;
309 rpc_gss_entries.rpc_gss_get_versions = rpc_gss_get_versions;
310 rpc_gss_entries.rpc_gss_is_installed = rpc_gss_is_installed;
311 rpc_gss_entries.rpc_gss_set_svc_name = rpc_gss_set_svc_name;
312 rpc_gss_entries.rpc_gss_clear_svc_name = rpc_gss_clear_svc_name;
313 rpc_gss_entries.rpc_gss_getcred = rpc_gss_getcred;
314 rpc_gss_entries.rpc_gss_set_callback = rpc_gss_set_callback;
315 rpc_gss_entries.rpc_gss_clear_callback = rpc_gss_clear_callback;
316 rpc_gss_entries.rpc_gss_get_principal_name =
317 rpc_gss_get_principal_name;
318 rpc_gss_entries.rpc_gss_svc_max_data_length =
319 rpc_gss_svc_max_data_length;
320 rpc_gss_entries.rpc_gss_ip_to_srv_principal =
321 rpc_gss_ip_to_srv_principal;
322 mtx_init(&kgss_gssd_lock, "kgss_gssd_lock", NULL, MTX_DEF);
323 error = kgss_load();
324 break;
325 case MOD_UNLOAD:
326 #if 0
327 kgss_unload();
328 mtx_destroy(&kgss_gssd_lock);
329 #endif
330 /*
331 * Unloading of the kgssapi module is not currently supported.
332 * If somebody wants this, we would need to keep track of
333 * currently executing threads and make sure the count is 0.
334 */
335 /* FALLTHROUGH */
336 default:
337 error = EOPNOTSUPP;
338 }
339 return (error);
340 }
341 static moduledata_t kgssapi_mod = {
342 "kgssapi",
343 kgssapi_modevent,
344 NULL,
345 };
346 DECLARE_MODULE(kgssapi, kgssapi_mod, SI_SUB_VFS, SI_ORDER_SECOND);
347 MODULE_DEPEND(kgssapi, xdr, 1, 1, 1);
348 MODULE_DEPEND(kgssapi, krpc, 1, 1, 1);
349 MODULE_VERSION(kgssapi, 1);
350