]>
Commit | Line | Data |
---|---|---|
2d21ac55 | 1 | /* |
3e170ce0 | 2 | * Copyright (c) 2007-2015 Apple Inc. All rights reserved. |
2d21ac55 A |
3 | * |
4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ | |
5 | * | |
6 | * This file contains Original Code and/or Modifications of Original Code | |
7 | * as defined in and that are subject to the Apple Public Source License | |
8 | * Version 2.0 (the 'License'). You may not use this file except in | |
9 | * compliance with the License. The rights granted to you under the License | |
10 | * may not be used to create, or enable the creation or redistribution of, | |
11 | * unlawful or unlicensed copies of an Apple operating system, or to | |
12 | * circumvent, violate, or enable the circumvention or violation of, any | |
13 | * terms of an Apple operating system software license agreement. | |
14 | * | |
15 | * Please obtain a copy of the License at | |
16 | * http://www.opensource.apple.com/apsl/ and read it before using this file. | |
17 | * | |
18 | * The Original Code and all software distributed under the License are | |
19 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
20 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, | |
21 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
22 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. | |
23 | * Please see the License for the specific language governing rights and | |
24 | * limitations under the License. | |
25 | * | |
26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ | |
27 | */ | |
28 | ||
29 | /************* | |
30 | * These functions implement RPCSEC_GSS security for the NFS client and server. | |
31 | * The code is specific to the use of Kerberos v5 and the use of DES MAC MD5 | |
32 | * protection as described in Internet RFC 2203 and 2623. | |
33 | * | |
34 | * In contrast to the original AUTH_SYS authentication, RPCSEC_GSS is stateful. | |
35 | * It requires the client and server negotiate a secure connection as part of a | |
36 | * security context. The context state is maintained in client and server structures. | |
37 | * On the client side, each user of an NFS mount is assigned their own context, | |
38 | * identified by UID, on their first use of the mount, and it persists until the | |
39 | * unmount or until the context is renewed. Each user context has a corresponding | |
40 | * server context which the server maintains until the client destroys it, or | |
41 | * until the context expires. | |
42 | * | |
43 | * The client and server contexts are set up dynamically. When a user attempts | |
44 | * to send an NFS request, if there is no context for the user, then one is | |
45 | * set up via an exchange of NFS null procedure calls as described in RFC 2203. | |
46 | * During this exchange, the client and server pass a security token that is | |
47 | * forwarded via Mach upcall to the gssd, which invokes the GSS-API to authenticate | |
48 | * the user to the server (and vice-versa). The client and server also receive | |
49 | * a unique session key that can be used to digitally sign the credentials and | |
50 | * verifier or optionally to provide data integrity and/or privacy. | |
51 | * | |
52 | * Once the context is complete, the client and server enter a normal data | |
53 | * exchange phase - beginning with the NFS request that prompted the context | |
54 | * creation. During this phase, the client's RPC header contains an RPCSEC_GSS | |
55 | * credential and verifier, and the server returns a verifier as well. | |
56 | * For simple authentication, the verifier contains a signed checksum of the | |
57 | * RPC header, including the credential. The server's verifier has a signed | |
58 | * checksum of the current sequence number. | |
59 | * | |
60 | * Each client call contains a sequence number that nominally increases by one | |
61 | * on each request. The sequence number is intended to prevent replay attacks. | |
62 | * Since the protocol can be used over UDP, there is some allowance for | |
63 | * out-of-sequence requests, so the server checks whether the sequence numbers | |
64 | * are within a sequence "window". If a sequence number is outside the lower | |
65 | * bound of the window, the server silently drops the request. This has some | |
66 | * implications for retransmission. If a request needs to be retransmitted, the | |
67 | * client must bump the sequence number even if the request XID is unchanged. | |
68 | * | |
69 | * When the NFS mount is unmounted, the client sends a "destroy" credential | |
70 | * to delete the server's context for each user of the mount. Since it's | |
71 | * possible for the client to crash or disconnect without sending the destroy | |
72 | * message, the server has a thread that reaps contexts that have been idle | |
73 | * too long. | |
74 | */ | |
75 | ||
76 | #include <stdint.h> | |
77 | #include <sys/param.h> | |
78 | #include <sys/systm.h> | |
79 | #include <sys/proc.h> | |
80 | #include <sys/kauth.h> | |
81 | #include <sys/kernel.h> | |
82 | #include <sys/mount_internal.h> | |
83 | #include <sys/vnode.h> | |
84 | #include <sys/ubc.h> | |
85 | #include <sys/malloc.h> | |
86 | #include <sys/kpi_mbuf.h> | |
39236c6e | 87 | #include <sys/ucred.h> |
2d21ac55 A |
88 | |
89 | #include <kern/host.h> | |
fe8ab488 | 90 | #include <kern/task.h> |
2d21ac55 A |
91 | #include <libkern/libkern.h> |
92 | ||
93 | #include <mach/task.h> | |
316670eb | 94 | #include <mach/host_special_ports.h> |
2d21ac55 A |
95 | #include <mach/host_priv.h> |
96 | #include <mach/thread_act.h> | |
97 | #include <mach/mig_errors.h> | |
98 | #include <mach/vm_map.h> | |
99 | #include <vm/vm_map.h> | |
100 | #include <vm/vm_kern.h> | |
101 | #include <gssd/gssd_mach.h> | |
102 | ||
103 | #include <nfs/rpcv2.h> | |
104 | #include <nfs/nfsproto.h> | |
105 | #include <nfs/nfs.h> | |
106 | #include <nfs/nfsnode.h> | |
107 | #include <nfs/nfs_gss.h> | |
108 | #include <nfs/nfsmount.h> | |
109 | #include <nfs/xdr_subs.h> | |
110 | #include <nfs/nfsm_subs.h> | |
111 | #include <nfs/nfs_gss.h> | |
3e170ce0 A |
112 | #include <mach_assert.h> |
113 | #include <kern/assert.h> | |
114 | ||
115 | #define ASSERT(EX) assert(EX) | |
b0d623f7 | 116 | |
2d21ac55 A |
117 | #define NFS_GSS_MACH_MAX_RETRIES 3 |
118 | ||
39236c6e A |
119 | #define NFS_GSS_DBG(...) NFS_DBG(NFS_FAC_GSS, 7, ## __VA_ARGS__) |
120 | #define NFS_GSS_ISDBG (NFS_DEBUG_FACILITY & NFS_FAC_GSS) | |
121 | ||
b0d623f7 | 122 | |
2d21ac55 A |
123 | #if NFSSERVER |
124 | u_long nfs_gss_svc_ctx_hash; | |
125 | struct nfs_gss_svc_ctx_hashhead *nfs_gss_svc_ctx_hashtbl; | |
126 | lck_mtx_t *nfs_gss_svc_ctx_mutex; | |
127 | lck_grp_t *nfs_gss_svc_grp; | |
b0d623f7 A |
128 | uint32_t nfsrv_gss_context_ttl = GSS_CTX_EXPIRE; |
129 | #define GSS_SVC_CTX_TTL ((uint64_t)max(2*GSS_CTX_PEND, nfsrv_gss_context_ttl) * NSEC_PER_SEC) | |
2d21ac55 A |
130 | #endif /* NFSSERVER */ |
131 | ||
132 | #if NFSCLIENT | |
133 | lck_grp_t *nfs_gss_clnt_grp; | |
134 | #endif /* NFSCLIENT */ | |
135 | ||
39037602 A |
136 | #define KRB5_MAX_MIC_SIZE 128 |
137 | uint8_t krb5_mech_oid[11] = { 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x12, 0x01, 0x02, 0x02 }; | |
138 | static uint8_t xdrpad[] = { 0x00, 0x00, 0x00, 0x00}; | |
2d21ac55 A |
139 | |
140 | #if NFSCLIENT | |
141 | static int nfs_gss_clnt_ctx_find(struct nfsreq *); | |
2d21ac55 | 142 | static int nfs_gss_clnt_ctx_init(struct nfsreq *, struct nfs_gss_clnt_ctx *); |
6d2010ae | 143 | static int nfs_gss_clnt_ctx_init_retry(struct nfsreq *, struct nfs_gss_clnt_ctx *); |
2d21ac55 | 144 | static int nfs_gss_clnt_ctx_callserver(struct nfsreq *, struct nfs_gss_clnt_ctx *); |
39236c6e | 145 | static uint8_t *nfs_gss_clnt_svcname(struct nfsmount *, gssd_nametype *, uint32_t *); |
39037602 | 146 | static int nfs_gss_clnt_gssd_upcall(struct nfsreq *, struct nfs_gss_clnt_ctx *, uint32_t); |
3e170ce0 | 147 | void nfs_gss_clnt_ctx_neg_cache_reap(struct nfsmount *); |
fe8ab488 | 148 | static void nfs_gss_clnt_ctx_clean(struct nfs_gss_clnt_ctx *); |
39037602 | 149 | static int nfs_gss_clnt_ctx_copy(struct nfs_gss_clnt_ctx *, struct nfs_gss_clnt_ctx **); |
fe8ab488 A |
150 | static void nfs_gss_clnt_ctx_destroy(struct nfs_gss_clnt_ctx *); |
151 | static void nfs_gss_clnt_log_error(struct nfsreq *, struct nfs_gss_clnt_ctx *, uint32_t, uint32_t); | |
2d21ac55 A |
152 | #endif /* NFSCLIENT */ |
153 | ||
154 | #if NFSSERVER | |
155 | static struct nfs_gss_svc_ctx *nfs_gss_svc_ctx_find(uint32_t); | |
156 | static void nfs_gss_svc_ctx_insert(struct nfs_gss_svc_ctx *); | |
157 | static void nfs_gss_svc_ctx_timer(void *, void *); | |
158 | static int nfs_gss_svc_gssd_upcall(struct nfs_gss_svc_ctx *); | |
159 | static int nfs_gss_svc_seqnum_valid(struct nfs_gss_svc_ctx *, uint32_t); | |
160 | #endif /* NFSSERVER */ | |
161 | ||
316670eb A |
162 | static void host_release_special_port(mach_port_t); |
163 | static mach_port_t host_copy_special_port(mach_port_t); | |
2d21ac55 A |
164 | static void nfs_gss_mach_alloc_buffer(u_char *, uint32_t, vm_map_copy_t *); |
165 | static int nfs_gss_mach_vmcopyout(vm_map_copy_t, uint32_t, u_char *); | |
39037602 | 166 | |
2d21ac55 A |
167 | static int nfs_gss_mchain_length(mbuf_t); |
168 | static int nfs_gss_append_chain(struct nfsm_chain *, mbuf_t); | |
169 | static void nfs_gss_nfsm_chain(struct nfsm_chain *, mbuf_t); | |
2d21ac55 A |
170 | |
171 | #if NFSSERVER | |
172 | thread_call_t nfs_gss_svc_ctx_timer_call; | |
173 | int nfs_gss_timer_on = 0; | |
174 | uint32_t nfs_gss_ctx_count = 0; | |
175 | const uint32_t nfs_gss_ctx_max = GSS_SVC_MAXCONTEXTS; | |
176 | #endif /* NFSSERVER */ | |
177 | ||
178 | /* | |
179 | * Initialization when NFS starts | |
180 | */ | |
181 | void | |
182 | nfs_gss_init(void) | |
183 | { | |
184 | #if NFSCLIENT | |
185 | nfs_gss_clnt_grp = lck_grp_alloc_init("rpcsec_gss_clnt", LCK_GRP_ATTR_NULL); | |
186 | #endif /* NFSCLIENT */ | |
187 | ||
188 | #if NFSSERVER | |
189 | nfs_gss_svc_grp = lck_grp_alloc_init("rpcsec_gss_svc", LCK_GRP_ATTR_NULL); | |
190 | ||
191 | nfs_gss_svc_ctx_hashtbl = hashinit(SVC_CTX_HASHSZ, M_TEMP, &nfs_gss_svc_ctx_hash); | |
192 | nfs_gss_svc_ctx_mutex = lck_mtx_alloc_init(nfs_gss_svc_grp, LCK_ATTR_NULL); | |
193 | ||
194 | nfs_gss_svc_ctx_timer_call = thread_call_allocate(nfs_gss_svc_ctx_timer, NULL); | |
195 | #endif /* NFSSERVER */ | |
196 | } | |
197 | ||
39037602 A |
198 | /* |
199 | * Common RPCSEC_GSS support routines | |
200 | */ | |
201 | ||
202 | static errno_t | |
203 | rpc_gss_prepend_32(mbuf_t *mb, uint32_t value) | |
204 | { | |
205 | int error; | |
206 | uint32_t *data; | |
207 | ||
208 | #if 0 | |
209 | data = mbuf_data(*mb); | |
210 | /* | |
211 | * If a wap token comes back and is not aligned | |
212 | * get a new buffer (which should be aligned) to put the | |
213 | * length in. | |
214 | */ | |
215 | if ((uintptr_t)data & 0x3) { | |
216 | mbuf_t nmb; | |
217 | ||
218 | error = mbuf_get(MBUF_WAITOK, MBUF_TYPE_DATA, &nmb); | |
219 | if (error) | |
220 | return (error); | |
221 | mbuf_setnext(nmb, *mb); | |
222 | *mb = nmb; | |
223 | } | |
224 | #endif | |
225 | error = mbuf_prepend(mb, sizeof(uint32_t), MBUF_WAITOK); | |
226 | if (error) | |
227 | return (error); | |
228 | ||
229 | data = mbuf_data(*mb); | |
230 | *data = txdr_unsigned(value); | |
231 | ||
232 | return (0); | |
233 | } | |
234 | ||
235 | /* | |
236 | * Prepend the sequence number to the xdr encode argumen or result | |
237 | * Sequence number is prepended in its own mbuf. | |
238 | * | |
239 | * On successful return mbp_head will point to the old mbuf chain | |
240 | * prepended with a new mbuf that has the sequence number. | |
241 | */ | |
242 | ||
243 | static errno_t | |
244 | rpc_gss_data_create(mbuf_t *mbp_head, uint32_t seqnum) | |
245 | { | |
246 | int error; | |
247 | mbuf_t mb; | |
248 | struct nfsm_chain nmc; | |
249 | struct nfsm_chain *nmcp = &nmc; | |
250 | uint8_t *data; | |
251 | ||
252 | error = mbuf_get(MBUF_WAITOK, MBUF_TYPE_DATA, &mb); | |
253 | if (error) | |
254 | return (error); | |
255 | data = mbuf_data(mb); | |
256 | #if 0 | |
257 | /* Reserve space for prepending */ | |
258 | len = mbuf_maxlen(mb); | |
259 | len = (len & ~0x3) - NFSX_UNSIGNED; | |
260 | printf("%s: data = %p, len = %d\n", __func__, data, (int)len); | |
261 | error = mbuf_setdata(mb, data + len, 0); | |
262 | if (error || mbuf_trailingspace(mb)) | |
263 | printf("%s: data = %p trailingspace = %d error = %d\n", __func__, mbuf_data(mb), (int)mbuf_trailingspace(mb), error); | |
264 | #endif | |
265 | /* Reserve 16 words for prepending */ | |
266 | error = mbuf_setdata(mb, data + 16*sizeof(uint32_t), 0); | |
267 | nfsm_chain_init(nmcp, mb); | |
268 | nfsm_chain_add_32(error, nmcp, seqnum); | |
269 | nfsm_chain_build_done(error, nmcp); | |
270 | if (error) | |
271 | return (EINVAL); | |
272 | mbuf_setnext(nmcp->nmc_mcur, *mbp_head); | |
273 | *mbp_head = nmcp->nmc_mhead; | |
274 | ||
275 | return (0); | |
276 | } | |
277 | ||
278 | /* | |
279 | * Create an rpc_gss_integ_data_t given an argument or result in mb_head. | |
280 | * On successful return mb_head will point to the rpc_gss_integ_data_t of length len. | |
281 | * Note mb_head will now point to a 4 byte sequence number. len does not include | |
282 | * any extra xdr padding. | |
283 | * Returns 0 on success, else an errno_t | |
284 | */ | |
285 | ||
286 | static errno_t | |
287 | rpc_gss_integ_data_create(gss_ctx_id_t ctx, mbuf_t *mb_head, uint32_t seqnum, uint32_t *len) | |
288 | { | |
289 | uint32_t error; | |
290 | uint32_t major; | |
291 | uint32_t length; | |
292 | gss_buffer_desc mic; | |
293 | struct nfsm_chain nmc; | |
294 | ||
295 | /* Length of the argument or result */ | |
296 | length = nfs_gss_mchain_length(*mb_head); | |
297 | if (len) | |
298 | *len = length; | |
299 | error = rpc_gss_data_create(mb_head, seqnum); | |
300 | if (error) | |
301 | return (error); | |
302 | ||
303 | /* | |
304 | * length is the length of the rpc_gss_data | |
305 | */ | |
306 | length += NFSX_UNSIGNED; /* Add the sequence number to the length */ | |
307 | major = gss_krb5_get_mic_mbuf(&error, ctx, 0, *mb_head, 0, length, &mic); | |
308 | if (major != GSS_S_COMPLETE) { | |
309 | printf("gss_krb5_get_mic_mbuf failed %d\n", error); | |
310 | return (error); | |
311 | } | |
312 | ||
313 | error = rpc_gss_prepend_32(mb_head, length); | |
314 | if (error) | |
315 | return (error); | |
316 | ||
317 | nfsm_chain_dissect_init(error, &nmc, *mb_head); | |
318 | /* Append GSS mic token by advancing rpc_gss_data_t length + NFSX_UNSIGNED (size of the length field) */ | |
319 | nfsm_chain_adv(error, &nmc, length + NFSX_UNSIGNED); | |
320 | nfsm_chain_finish_mbuf(error, &nmc); // Force the mic into its own sub chain. | |
321 | nfsm_chain_add_32(error, &nmc, mic.length); | |
322 | nfsm_chain_add_opaque(error, &nmc, mic.value, mic.length); | |
323 | nfsm_chain_build_done(error, &nmc); | |
324 | gss_release_buffer(NULL, &mic); | |
325 | ||
326 | // printmbuf("rpc_gss_integ_data_create done", *mb_head, 0, 0); | |
327 | assert(nmc.nmc_mhead == *mb_head); | |
328 | ||
329 | return (error); | |
330 | } | |
331 | ||
332 | /* | |
333 | * Create an rpc_gss_priv_data_t out of the supplied raw arguments or results in mb_head. | |
334 | * On successful return mb_head will point to a wrap token of lenght len. | |
335 | * Note len does not include any xdr padding | |
336 | * Returns 0 on success, else an errno_t | |
337 | */ | |
338 | static errno_t | |
339 | rpc_gss_priv_data_create(gss_ctx_id_t ctx, mbuf_t *mb_head, uint32_t seqnum, uint32_t *len) | |
340 | { | |
341 | uint32_t error; | |
342 | uint32_t major; | |
343 | struct nfsm_chain nmc; | |
344 | uint32_t pad; | |
345 | uint32_t length; | |
346 | ||
347 | error = rpc_gss_data_create(mb_head, seqnum); | |
348 | if (error) | |
349 | return (error); | |
350 | ||
351 | length = nfs_gss_mchain_length(*mb_head); | |
352 | major = gss_krb5_wrap_mbuf(&error, ctx, 1, 0, mb_head, 0, length, NULL); | |
353 | if (major != GSS_S_COMPLETE) | |
354 | return (error); | |
355 | ||
356 | length = nfs_gss_mchain_length(*mb_head); | |
357 | if (len) | |
358 | *len = length; | |
359 | pad = nfsm_pad(length); | |
360 | ||
361 | /* Prepend the opaque length of rep rpc_gss_priv_data */ | |
362 | error = rpc_gss_prepend_32(mb_head, length); | |
363 | ||
364 | if (error) | |
365 | return (error); | |
366 | if (pad) { | |
367 | nfsm_chain_dissect_init(error, &nmc, *mb_head); | |
368 | /* Advance the opauque size of length and length data */ | |
369 | nfsm_chain_adv(error, &nmc, NFSX_UNSIGNED + length); | |
370 | nfsm_chain_finish_mbuf(error, &nmc); | |
371 | nfsm_chain_add_opaque_nopad(error, &nmc, xdrpad, pad); | |
372 | nfsm_chain_build_done(error, &nmc); | |
373 | } | |
374 | ||
375 | return (error); | |
376 | } | |
377 | ||
2d21ac55 A |
378 | #if NFSCLIENT |
379 | ||
39037602 A |
380 | /* |
381 | * Restore the argument or result from an rpc_gss_integ_data mbuf chain | |
382 | * We have a four byte seqence number, len arguments, and an opaque | |
383 | * encoded mic, possibly followed by some pad bytes. The mic and possible | |
384 | * pad bytes are on their own sub mbuf chains. | |
385 | * | |
386 | * On successful return mb_head is the chain of the xdr args or results sans | |
387 | * the sequence number and mic and return 0. Otherwise return an errno. | |
388 | * | |
389 | */ | |
390 | static errno_t | |
391 | rpc_gss_integ_data_restore(gss_ctx_id_t ctx __unused, mbuf_t *mb_head, size_t len) | |
392 | { | |
393 | mbuf_t mb = *mb_head; | |
394 | mbuf_t tail = NULL, next; | |
395 | ||
396 | /* Chop of the opaque length and seq number */ | |
397 | mbuf_adj(mb, 2 * NFSX_UNSIGNED); | |
398 | ||
399 | /* should only be one, ... but */ | |
400 | for (; mb; mb = next) { | |
401 | next = mbuf_next(mb); | |
402 | if (mbuf_len(mb) == 0) | |
403 | mbuf_free(mb); | |
404 | else | |
405 | break; | |
406 | } | |
407 | *mb_head = mb; | |
408 | ||
409 | for (; mb && len; mb = mbuf_next(mb)) { | |
410 | tail = mb; | |
411 | if (mbuf_len(mb) <= len) | |
412 | len -= mbuf_len(mb); | |
413 | else | |
414 | return (EBADRPC); | |
415 | } | |
416 | /* drop the mic */ | |
417 | if (tail) { | |
418 | mbuf_setnext(tail, NULL); | |
419 | mbuf_freem(mb); | |
420 | } | |
421 | ||
422 | return (0); | |
423 | } | |
424 | ||
425 | /* | |
426 | * Restore the argument or result rfom an rpc_gss_priv_data mbuf chain | |
427 | * mb_head points to the wrap token of length len. | |
428 | * | |
429 | * On successful return mb_head is our original xdr arg or result an | |
430 | * the return value is 0. Otherise return an errno | |
431 | */ | |
432 | static errno_t | |
433 | rpc_gss_priv_data_restore(gss_ctx_id_t ctx, mbuf_t *mb_head, size_t len) | |
434 | { | |
435 | uint32_t major, error; | |
436 | mbuf_t mb = *mb_head, next; | |
437 | uint32_t plen; | |
438 | size_t length; | |
439 | gss_qop_t qop = GSS_C_QOP_REVERSE; | |
440 | ||
441 | /* Chop of the opaque length */ | |
442 | mbuf_adj(mb, NFSX_UNSIGNED); | |
443 | /* If we have padding, drop it */ | |
444 | plen = nfsm_pad(len); | |
445 | if (plen) { | |
446 | mbuf_t tail = NULL; | |
447 | ||
448 | for(length = 0; length < len && mb; mb = mbuf_next(mb)) { | |
449 | tail = mb; | |
450 | length += mbuf_len(mb); | |
451 | } | |
452 | if ((length != len) || (mb == NULL) || (tail == NULL)) | |
453 | return (EBADRPC); | |
454 | ||
455 | mbuf_freem(mb); | |
456 | mbuf_setnext(tail, NULL); | |
457 | } | |
458 | ||
459 | major = gss_krb5_unwrap_mbuf(&error, ctx, mb_head, 0, len, NULL, &qop); | |
460 | if (major != GSS_S_COMPLETE) { | |
461 | printf("gss_krb5_unwrap_mbuf failed. major = %d minor = %d\n", (int)major, error); | |
462 | return (error); | |
463 | } | |
464 | mb = *mb_head; | |
465 | ||
466 | /* Drop the seqence number */ | |
467 | mbuf_adj(mb, NFSX_UNSIGNED); | |
468 | assert(mbuf_len(mb) == 0); | |
469 | ||
470 | /* Chop of any empty mbufs */ | |
471 | for (mb = *mb_head; mb; mb = next) { | |
472 | next = mbuf_next(mb); | |
473 | if (mbuf_len(mb) == 0) | |
474 | mbuf_free(mb); | |
475 | else | |
476 | break; | |
477 | } | |
478 | *mb_head = mb; | |
479 | ||
480 | return (0); | |
481 | } | |
482 | ||
2d21ac55 A |
483 | /* |
484 | * Find the context for a particular user. | |
485 | * | |
486 | * If the context doesn't already exist | |
487 | * then create a new context for this user. | |
488 | * | |
489 | * Note that the code allows superuser (uid == 0) | |
490 | * to adopt the context of another user. | |
39236c6e A |
491 | * |
492 | * We'll match on the audit session ids, since those | |
493 | * processes will have acccess to the same credential cache. | |
2d21ac55 | 494 | */ |
39236c6e A |
495 | |
496 | #define kauth_cred_getasid(cred) ((cred)->cr_audit.as_aia_p->ai_asid) | |
497 | #define kauth_cred_getauid(cred) ((cred)->cr_audit.as_aia_p->ai_auid) | |
498 | ||
3e170ce0 A |
499 | #define SAFE_CAST_INTTYPE( type, intval ) \ |
500 | ( (type)(intval)/(sizeof(type) < sizeof(intval) ? 0 : 1) ) | |
501 | ||
502 | uid_t | |
503 | nfs_cred_getasid2uid(kauth_cred_t cred) | |
504 | { | |
505 | uid_t result = SAFE_CAST_INTTYPE(uid_t, kauth_cred_getasid(cred)); | |
506 | return (result); | |
507 | } | |
508 | ||
fe8ab488 A |
509 | /* |
510 | * Debugging | |
511 | */ | |
512 | static void | |
513 | nfs_gss_clnt_ctx_dump(struct nfsmount *nmp) | |
514 | { | |
515 | struct nfs_gss_clnt_ctx *cp; | |
516 | ||
517 | lck_mtx_lock(&nmp->nm_lock); | |
3e170ce0 | 518 | NFS_GSS_DBG("Enter\n"); |
fe8ab488 A |
519 | TAILQ_FOREACH(cp, &nmp->nm_gsscl, gss_clnt_entries) { |
520 | lck_mtx_lock(cp->gss_clnt_mtx); | |
521 | printf("context %d/%d: refcnt = %d, flags = %x\n", | |
522 | kauth_cred_getasid(cp->gss_clnt_cred), | |
523 | kauth_cred_getauid(cp->gss_clnt_cred), | |
524 | cp->gss_clnt_refcnt, cp->gss_clnt_flags); | |
525 | lck_mtx_unlock(cp->gss_clnt_mtx); | |
526 | } | |
3e170ce0 | 527 | NFS_GSS_DBG("Exit\n"); |
fe8ab488 A |
528 | lck_mtx_unlock(&nmp->nm_lock); |
529 | } | |
530 | ||
3e170ce0 A |
531 | static char * |
532 | nfs_gss_clnt_ctx_name(struct nfsmount *nmp, struct nfs_gss_clnt_ctx *cp, char *buf, int len) | |
533 | { | |
534 | char *np; | |
535 | int nlen; | |
536 | const char *server = ""; | |
537 | ||
538 | if (nmp && nmp->nm_mountp) | |
539 | server = vfs_statfs(nmp->nm_mountp)->f_mntfromname; | |
540 | ||
541 | if (cp == NULL) { | |
542 | snprintf(buf, len, "[%s] NULL context", server); | |
543 | return (buf); | |
544 | } | |
545 | ||
546 | if (cp->gss_clnt_principal && !cp->gss_clnt_display) { | |
547 | np = (char *)cp->gss_clnt_principal; | |
548 | nlen = cp->gss_clnt_prinlen; | |
549 | } else { | |
550 | np = cp->gss_clnt_display; | |
551 | nlen = np ? strlen(cp->gss_clnt_display) : 0; | |
552 | } | |
553 | if (nlen) | |
554 | snprintf(buf, len, "[%s] %.*s %d/%d %s", server, nlen, np, | |
555 | kauth_cred_getasid(cp->gss_clnt_cred), | |
556 | kauth_cred_getuid(cp->gss_clnt_cred), | |
557 | cp->gss_clnt_principal ? "" : "[from default cred] "); | |
558 | else | |
559 | snprintf(buf, len, "[%s] using default %d/%d ", server, | |
560 | kauth_cred_getasid(cp->gss_clnt_cred), | |
561 | kauth_cred_getuid(cp->gss_clnt_cred)); | |
562 | return (buf); | |
563 | } | |
564 | ||
565 | #define NFS_CTXBUFSZ 80 | |
566 | #define NFS_GSS_CTX(req, cp) nfs_gss_clnt_ctx_name((req)->r_nmp, cp ? cp : (req)->r_gss_ctx, CTXBUF, sizeof(CTXBUF)) | |
567 | ||
fe8ab488 A |
568 | #define NFS_GSS_CLNT_CTX_DUMP(nmp) \ |
569 | do { \ | |
570 | if (NFS_GSS_ISDBG && (NFS_DEBUG_FLAGS & 0x2)) \ | |
571 | nfs_gss_clnt_ctx_dump((nmp)); \ | |
572 | } while (0) | |
573 | ||
39236c6e A |
574 | static int |
575 | nfs_gss_clnt_ctx_cred_match(kauth_cred_t cred1, kauth_cred_t cred2) | |
576 | { | |
577 | if (kauth_cred_getasid(cred1) == kauth_cred_getasid(cred2)) | |
578 | return (1); | |
579 | return (0); | |
580 | } | |
581 | ||
3e170ce0 A |
582 | /* |
583 | * Busy the mount for each principal set on the mount | |
584 | * so that the automounter will not unmount the file | |
585 | * system underneath us. With out this, if an unmount | |
586 | * occurs the principal that is set for an audit session | |
587 | * will be lost and we may end up with a different identity. | |
588 | * | |
589 | * Note setting principals on the mount is a bad idea. This | |
590 | * really should be handle by KIM (Kerberos Identity Management) | |
591 | * so that defaults can be set by service identities. | |
592 | */ | |
593 | ||
594 | static void | |
595 | nfs_gss_clnt_mnt_ref(struct nfsmount *nmp) | |
596 | { | |
597 | int error; | |
598 | vnode_t rvp; | |
599 | ||
600 | if (nmp == NULL || | |
601 | !(vfs_flags(nmp->nm_mountp) & MNT_AUTOMOUNTED)) | |
602 | return; | |
603 | ||
604 | error = VFS_ROOT(nmp->nm_mountp, &rvp, NULL); | |
605 | if (!error) { | |
606 | vnode_ref(rvp); | |
607 | vnode_put(rvp); | |
608 | } | |
609 | } | |
610 | ||
611 | /* | |
612 | * Unbusy the mout. See above comment, | |
613 | */ | |
614 | ||
615 | static void | |
616 | nfs_gss_clnt_mnt_rele(struct nfsmount *nmp) | |
617 | { | |
618 | int error; | |
619 | vnode_t rvp; | |
620 | ||
621 | if (nmp == NULL || | |
622 | !(vfs_flags(nmp->nm_mountp) & MNT_AUTOMOUNTED)) | |
623 | return; | |
624 | ||
625 | error = VFS_ROOT(nmp->nm_mountp, &rvp, NULL); | |
626 | if (!error) { | |
627 | vnode_rele(rvp); | |
628 | vnode_put(rvp); | |
629 | } | |
630 | } | |
631 | ||
5ba3f43e | 632 | int nfs_root_steals_ctx = 0; |
3e170ce0 | 633 | |
2d21ac55 | 634 | static int |
3e170ce0 | 635 | nfs_gss_clnt_ctx_find_principal(struct nfsreq *req, uint8_t *principal, uint32_t plen, uint32_t nt) |
2d21ac55 A |
636 | { |
637 | struct nfsmount *nmp = req->r_nmp; | |
638 | struct nfs_gss_clnt_ctx *cp; | |
3e170ce0 | 639 | struct nfsreq treq; |
2d21ac55 | 640 | int error = 0; |
fe8ab488 | 641 | struct timeval now; |
3e170ce0 A |
642 | char CTXBUF[NFS_CTXBUFSZ]; |
643 | ||
644 | bzero(&treq, sizeof (struct nfsreq)); | |
645 | treq.r_nmp = nmp; | |
646 | ||
fe8ab488 | 647 | microuptime(&now); |
2d21ac55 A |
648 | lck_mtx_lock(&nmp->nm_lock); |
649 | TAILQ_FOREACH(cp, &nmp->nm_gsscl, gss_clnt_entries) { | |
fe8ab488 A |
650 | lck_mtx_lock(cp->gss_clnt_mtx); |
651 | if (cp->gss_clnt_flags & GSS_CTX_DESTROY) { | |
3e170ce0 A |
652 | NFS_GSS_DBG("Found destroyed context %s refcnt = %d continuing\n", |
653 | NFS_GSS_CTX(req, cp), | |
fe8ab488 A |
654 | cp->gss_clnt_refcnt); |
655 | lck_mtx_unlock(cp->gss_clnt_mtx); | |
656 | continue; | |
657 | } | |
39236c6e | 658 | if (nfs_gss_clnt_ctx_cred_match(cp->gss_clnt_cred, req->r_cred)) { |
fe8ab488 A |
659 | if (nmp->nm_gsscl.tqh_first != cp) { |
660 | TAILQ_REMOVE(&nmp->nm_gsscl, cp, gss_clnt_entries); | |
661 | TAILQ_INSERT_HEAD(&nmp->nm_gsscl, cp, gss_clnt_entries); | |
662 | } | |
3e170ce0 A |
663 | if (principal) { |
664 | /* | |
665 | * If we have a principal, but it does not match the current cred | |
666 | * mark it for removal | |
667 | */ | |
668 | if (cp->gss_clnt_prinlen != plen || cp->gss_clnt_prinnt != nt || | |
669 | bcmp(cp->gss_clnt_principal, principal, plen) != 0) { | |
670 | cp->gss_clnt_flags |= (GSS_CTX_INVAL | GSS_CTX_DESTROY); | |
671 | cp->gss_clnt_refcnt++; | |
672 | lck_mtx_unlock(cp->gss_clnt_mtx); | |
673 | NFS_GSS_DBG("Marking %s for deletion because %s does not match\n", | |
674 | NFS_GSS_CTX(req, cp), principal); | |
675 | NFS_GSS_DBG("len = (%d,%d), nt = (%d,%d)\n", cp->gss_clnt_prinlen, plen, | |
676 | cp->gss_clnt_prinnt, nt); | |
677 | treq.r_gss_ctx = cp; | |
678 | cp = NULL; | |
679 | break; | |
680 | } | |
681 | } | |
fe8ab488 | 682 | if (cp->gss_clnt_flags & GSS_CTX_INVAL) { |
3e170ce0 A |
683 | /* |
684 | * If we're still being used and we're not expired | |
685 | * just return and don't bother gssd again. Note if | |
686 | * gss_clnt_nctime is zero it is about to be set to now. | |
687 | */ | |
688 | if (cp->gss_clnt_nctime + GSS_NEG_CACHE_TO >= now.tv_sec || cp->gss_clnt_nctime == 0) { | |
689 | NFS_GSS_DBG("Context %s (refcnt = %d) not expired returning EAUTH nctime = %ld now = %ld\n", | |
690 | NFS_GSS_CTX(req, cp), cp->gss_clnt_refcnt, cp->gss_clnt_nctime, now.tv_sec); | |
691 | lck_mtx_unlock(cp->gss_clnt_mtx); | |
692 | lck_mtx_unlock(&nmp->nm_lock); | |
693 | return (NFSERR_EAUTH); | |
694 | } | |
695 | if (cp->gss_clnt_refcnt) { | |
696 | struct nfs_gss_clnt_ctx *ncp; | |
697 | /* | |
698 | * If this context has references, we can't use it so we mark if for | |
699 | * destruction and create a new context based on this one in the | |
700 | * same manner as renewing one. | |
701 | */ | |
702 | cp->gss_clnt_flags |= GSS_CTX_DESTROY; | |
703 | NFS_GSS_DBG("Context %s has expired but we still have %d references\n", | |
704 | NFS_GSS_CTX(req, cp), cp->gss_clnt_refcnt); | |
39037602 | 705 | error = nfs_gss_clnt_ctx_copy(cp, &ncp); |
3e170ce0 A |
706 | lck_mtx_unlock(cp->gss_clnt_mtx); |
707 | if (error) { | |
708 | lck_mtx_unlock(&nmp->nm_lock); | |
709 | return (error); | |
710 | } | |
711 | cp = ncp; | |
712 | break; | |
713 | } else { | |
3e170ce0 A |
714 | if (cp->gss_clnt_nctime) |
715 | nmp->nm_ncentries--; | |
716 | lck_mtx_unlock(cp->gss_clnt_mtx); | |
717 | TAILQ_REMOVE(&nmp->nm_gsscl, cp, gss_clnt_entries); | |
718 | break; | |
719 | } | |
fe8ab488 | 720 | } |
3e170ce0 A |
721 | /* Found a valid context to return */ |
722 | cp->gss_clnt_refcnt++; | |
723 | req->r_gss_ctx = cp; | |
fe8ab488 | 724 | lck_mtx_unlock(cp->gss_clnt_mtx); |
6d2010ae | 725 | lck_mtx_unlock(&nmp->nm_lock); |
2d21ac55 A |
726 | return (0); |
727 | } | |
fe8ab488 | 728 | lck_mtx_unlock(cp->gss_clnt_mtx); |
2d21ac55 A |
729 | } |
730 | ||
4bd07ac2 | 731 | if (!cp && nfs_root_steals_ctx && principal == NULL && kauth_cred_getuid(req->r_cred) == 0) { |
2d21ac55 A |
732 | /* |
733 | * If superuser is trying to get access, then co-opt | |
734 | * the first valid context in the list. | |
735 | * XXX Ultimately, we need to allow superuser to | |
736 | * go ahead and attempt to set up its own context | |
737 | * in case one is set up for it. | |
738 | */ | |
739 | TAILQ_FOREACH(cp, &nmp->nm_gsscl, gss_clnt_entries) { | |
fe8ab488 | 740 | if (!(cp->gss_clnt_flags & (GSS_CTX_INVAL|GSS_CTX_DESTROY))) { |
2d21ac55 | 741 | nfs_gss_clnt_ctx_ref(req, cp); |
6d2010ae | 742 | lck_mtx_unlock(&nmp->nm_lock); |
3e170ce0 | 743 | NFS_GSS_DBG("Root stole context %s\n", NFS_GSS_CTX(req, NULL)); |
2d21ac55 A |
744 | return (0); |
745 | } | |
746 | } | |
747 | } | |
748 | ||
3e170ce0 A |
749 | NFS_GSS_DBG("Context %s%sfound in Neg Cache @ %ld\n", |
750 | NFS_GSS_CTX(req, cp), | |
751 | cp == NULL ? " not " : "", | |
fe8ab488 | 752 | cp == NULL ? 0L : cp->gss_clnt_nctime); |
3e170ce0 | 753 | |
2d21ac55 | 754 | /* |
fe8ab488 | 755 | * Not found - create a new context |
2d21ac55 | 756 | */ |
2d21ac55 | 757 | |
2d21ac55 | 758 | if (cp == NULL) { |
fe8ab488 A |
759 | MALLOC(cp, struct nfs_gss_clnt_ctx *, sizeof(*cp), M_TEMP, M_WAITOK|M_ZERO); |
760 | if (cp == NULL) { | |
761 | lck_mtx_unlock(&nmp->nm_lock); | |
762 | return (ENOMEM); | |
763 | } | |
764 | cp->gss_clnt_cred = req->r_cred; | |
765 | kauth_cred_ref(cp->gss_clnt_cred); | |
766 | cp->gss_clnt_mtx = lck_mtx_alloc_init(nfs_gss_clnt_grp, LCK_ATTR_NULL); | |
767 | cp->gss_clnt_ptime = now.tv_sec - GSS_PRINT_DELAY; | |
3e170ce0 A |
768 | if (principal) { |
769 | MALLOC(cp->gss_clnt_principal, uint8_t *, plen+1, M_TEMP, M_WAITOK|M_ZERO); | |
770 | memcpy(cp->gss_clnt_principal, principal, plen); | |
771 | cp->gss_clnt_prinlen = plen; | |
772 | cp->gss_clnt_prinnt = nt; | |
773 | cp->gss_clnt_flags |= GSS_CTX_STICKY; | |
774 | nfs_gss_clnt_mnt_ref(nmp); | |
775 | } | |
fe8ab488 A |
776 | } else { |
777 | nfs_gss_clnt_ctx_clean(cp); | |
3e170ce0 A |
778 | if (principal) { |
779 | /* | |
780 | * If we have a principal and we found a matching audit | |
781 | * session, then to get here, the principal had to match. | |
782 | * In walking the context list if it has a principal | |
783 | * or the principal is not set then we mark the context | |
784 | * for destruction and set cp to NULL and we fall to the | |
785 | * if clause above. If the context still has references | |
786 | * again we copy the context which will preserve the principal | |
787 | * and we end up here with the correct principal set. | |
788 | * If we don't have references the the principal must have | |
789 | * match and we will fall through here. | |
790 | */ | |
791 | cp->gss_clnt_flags |= GSS_CTX_STICKY; | |
792 | } | |
2d21ac55 | 793 | } |
3e170ce0 | 794 | |
2d21ac55 A |
795 | cp->gss_clnt_thread = current_thread(); |
796 | nfs_gss_clnt_ctx_ref(req, cp); | |
fe8ab488 | 797 | TAILQ_INSERT_HEAD(&nmp->nm_gsscl, cp, gss_clnt_entries); |
2d21ac55 A |
798 | lck_mtx_unlock(&nmp->nm_lock); |
799 | ||
6d2010ae | 800 | error = nfs_gss_clnt_ctx_init_retry(req, cp); // Initialize new context |
3e170ce0 A |
801 | if (error) { |
802 | NFS_GSS_DBG("nfs_gss_clnt_ctx_init_retry returned %d for %s\n", error, NFS_GSS_CTX(req, cp)); | |
b0d623f7 | 803 | nfs_gss_clnt_ctx_unref(req); |
3e170ce0 A |
804 | } |
805 | ||
806 | /* Remove any old matching contex that had a different principal */ | |
807 | nfs_gss_clnt_ctx_unref(&treq); | |
b0d623f7 | 808 | |
2d21ac55 A |
809 | return (error); |
810 | } | |
811 | ||
3e170ce0 A |
812 | static int |
813 | nfs_gss_clnt_ctx_find(struct nfsreq *req) | |
814 | { | |
815 | return (nfs_gss_clnt_ctx_find_principal(req, NULL, 0, 0)); | |
816 | } | |
817 | ||
2d21ac55 A |
818 | /* |
819 | * Inserts an RPCSEC_GSS credential into an RPC header. | |
820 | * After the credential is inserted, the code continues | |
821 | * to build the verifier which contains a signed checksum | |
822 | * of the RPC header. | |
823 | */ | |
39037602 | 824 | |
2d21ac55 A |
825 | int |
826 | nfs_gss_clnt_cred_put(struct nfsreq *req, struct nfsm_chain *nmc, mbuf_t args) | |
827 | { | |
2d21ac55 A |
828 | struct nfs_gss_clnt_ctx *cp; |
829 | uint32_t seqnum = 0; | |
39037602 A |
830 | uint32_t major; |
831 | uint32_t error = 0; | |
832 | int slpflag, recordmark = 0, offset; | |
2d21ac55 | 833 | struct gss_seq *gsp; |
39037602 | 834 | gss_buffer_desc mic; |
3e170ce0 | 835 | |
b0d623f7 A |
836 | slpflag = (PZERO-1); |
837 | if (req->r_nmp) { | |
6d2010ae | 838 | slpflag |= (NMFLAG(req->r_nmp, INTR) && req->r_thread && !(req->r_flags & R_NOINTR)) ? PCATCH : 0; |
b0d623f7 A |
839 | recordmark = (req->r_nmp->nm_sotype == SOCK_STREAM); |
840 | } | |
3e170ce0 | 841 | |
2d21ac55 A |
842 | retry: |
843 | if (req->r_gss_ctx == NULL) { | |
844 | /* | |
845 | * Find the context for this user. | |
846 | * If no context is found, one will | |
847 | * be created. | |
848 | */ | |
849 | error = nfs_gss_clnt_ctx_find(req); | |
850 | if (error) | |
851 | return (error); | |
852 | } | |
853 | cp = req->r_gss_ctx; | |
854 | ||
2d21ac55 A |
855 | /* |
856 | * If the context thread isn't null, then the context isn't | |
857 | * yet complete and is for the exclusive use of the thread | |
858 | * doing the context setup. Wait until the context thread | |
859 | * is null. | |
860 | */ | |
861 | lck_mtx_lock(cp->gss_clnt_mtx); | |
862 | if (cp->gss_clnt_thread && cp->gss_clnt_thread != current_thread()) { | |
863 | cp->gss_clnt_flags |= GSS_NEEDCTX; | |
b0d623f7 | 864 | msleep(cp, cp->gss_clnt_mtx, slpflag | PDROP, "ctxwait", NULL); |
6d2010ae | 865 | slpflag &= ~PCATCH; |
b0d623f7 | 866 | if ((error = nfs_sigintr(req->r_nmp, req, req->r_thread, 0))) |
2d21ac55 A |
867 | return (error); |
868 | nfs_gss_clnt_ctx_unref(req); | |
869 | goto retry; | |
870 | } | |
871 | lck_mtx_unlock(cp->gss_clnt_mtx); | |
872 | ||
873 | if (cp->gss_clnt_flags & GSS_CTX_COMPLETE) { | |
874 | /* | |
875 | * Get a sequence number for this request. | |
876 | * Check whether the oldest request in the window is complete. | |
877 | * If it's still pending, then wait until it's done before | |
878 | * we allocate a new sequence number and allow this request | |
879 | * to proceed. | |
880 | */ | |
881 | lck_mtx_lock(cp->gss_clnt_mtx); | |
882 | while (win_getbit(cp->gss_clnt_seqbits, | |
883 | ((cp->gss_clnt_seqnum - cp->gss_clnt_seqwin) + 1) % cp->gss_clnt_seqwin)) { | |
884 | cp->gss_clnt_flags |= GSS_NEEDSEQ; | |
ebb1b9f4 | 885 | msleep(cp, cp->gss_clnt_mtx, slpflag | PDROP, "seqwin", NULL); |
6d2010ae | 886 | slpflag &= ~PCATCH; |
b0d623f7 | 887 | if ((error = nfs_sigintr(req->r_nmp, req, req->r_thread, 0))) { |
2d21ac55 A |
888 | return (error); |
889 | } | |
ebb1b9f4 | 890 | lck_mtx_lock(cp->gss_clnt_mtx); |
2d21ac55 A |
891 | if (cp->gss_clnt_flags & GSS_CTX_INVAL) { |
892 | /* Renewed while while we were waiting */ | |
893 | lck_mtx_unlock(cp->gss_clnt_mtx); | |
894 | nfs_gss_clnt_ctx_unref(req); | |
895 | goto retry; | |
896 | } | |
897 | } | |
898 | seqnum = ++cp->gss_clnt_seqnum; | |
899 | win_setbit(cp->gss_clnt_seqbits, seqnum % cp->gss_clnt_seqwin); | |
900 | lck_mtx_unlock(cp->gss_clnt_mtx); | |
901 | ||
902 | MALLOC(gsp, struct gss_seq *, sizeof(*gsp), M_TEMP, M_WAITOK|M_ZERO); | |
903 | if (gsp == NULL) | |
904 | return (ENOMEM); | |
905 | gsp->gss_seqnum = seqnum; | |
906 | SLIST_INSERT_HEAD(&req->r_gss_seqlist, gsp, gss_seqnext); | |
907 | } | |
908 | ||
909 | /* Insert the credential */ | |
910 | nfsm_chain_add_32(error, nmc, RPCSEC_GSS); | |
911 | nfsm_chain_add_32(error, nmc, 5 * NFSX_UNSIGNED + cp->gss_clnt_handle_len); | |
912 | nfsm_chain_add_32(error, nmc, RPCSEC_GSS_VERS_1); | |
913 | nfsm_chain_add_32(error, nmc, cp->gss_clnt_proc); | |
914 | nfsm_chain_add_32(error, nmc, seqnum); | |
915 | nfsm_chain_add_32(error, nmc, cp->gss_clnt_service); | |
916 | nfsm_chain_add_32(error, nmc, cp->gss_clnt_handle_len); | |
b0d623f7 A |
917 | if (cp->gss_clnt_handle_len > 0) { |
918 | if (cp->gss_clnt_handle == NULL) | |
919 | return (EBADRPC); | |
920 | nfsm_chain_add_opaque(error, nmc, cp->gss_clnt_handle, cp->gss_clnt_handle_len); | |
921 | } | |
922 | if (error) | |
923 | return(error); | |
2d21ac55 A |
924 | /* |
925 | * Now add the verifier | |
926 | */ | |
927 | if (cp->gss_clnt_proc == RPCSEC_GSS_INIT || | |
928 | cp->gss_clnt_proc == RPCSEC_GSS_CONTINUE_INIT) { | |
929 | /* | |
930 | * If the context is still being created | |
931 | * then use a null verifier. | |
932 | */ | |
933 | nfsm_chain_add_32(error, nmc, RPCAUTH_NULL); // flavor | |
934 | nfsm_chain_add_32(error, nmc, 0); // length | |
935 | nfsm_chain_build_done(error, nmc); | |
936 | if (!error) | |
937 | nfs_gss_append_chain(nmc, args); | |
938 | return (error); | |
939 | } | |
940 | ||
b0d623f7 | 941 | offset = recordmark ? NFSX_UNSIGNED : 0; // record mark |
2d21ac55 | 942 | nfsm_chain_build_done(error, nmc); |
2d21ac55 | 943 | |
39037602 A |
944 | major = gss_krb5_get_mic_mbuf((uint32_t *)&error, cp->gss_clnt_ctx_id, 0, nmc->nmc_mhead, offset, 0, &mic); |
945 | if (major != GSS_S_COMPLETE) { | |
946 | printf ("gss_krb5_get_mic_buf failed %d\n", error); | |
947 | return (error); | |
948 | } | |
949 | ||
2d21ac55 | 950 | nfsm_chain_add_32(error, nmc, RPCSEC_GSS); // flavor |
39037602 A |
951 | nfsm_chain_add_32(error, nmc, mic.length); // length |
952 | nfsm_chain_add_opaque(error, nmc, mic.value, mic.length); | |
953 | (void)gss_release_buffer(NULL, &mic); | |
2d21ac55 A |
954 | nfsm_chain_build_done(error, nmc); |
955 | if (error) | |
956 | return (error); | |
957 | ||
958 | /* | |
959 | * Now we may have to compute integrity or encrypt the call args | |
960 | * per RFC 2203 Section 5.3.2 | |
961 | */ | |
962 | switch (cp->gss_clnt_service) { | |
963 | case RPCSEC_GSS_SVC_NONE: | |
39037602 A |
964 | if (args) |
965 | nfs_gss_append_chain(nmc, args); | |
2d21ac55 A |
966 | break; |
967 | case RPCSEC_GSS_SVC_INTEGRITY: | |
39037602 A |
968 | /* |
969 | * r_gss_arglen is the length of args mbuf going into the routine. | |
970 | * Its used to find the mic if we need to restore the args. | |
971 | */ | |
972 | /* Note the mbufs that were used in r_mrest are being encapsulated in the rpc_gss_integ_data_t */ | |
973 | assert(req->r_mrest == args); | |
974 | nfsm_chain_finish_mbuf(error, nmc); | |
2d21ac55 A |
975 | if (error) |
976 | return (error); | |
39037602 A |
977 | error = rpc_gss_integ_data_create(cp->gss_clnt_ctx_id, &args, seqnum, &req->r_gss_arglen); |
978 | if (error) | |
979 | break; | |
980 | req->r_mrest = args; | |
981 | req->r_gss_argoff = nfsm_chain_offset(nmc); | |
982 | nfs_gss_append_chain(nmc, args); | |
2d21ac55 A |
983 | break; |
984 | case RPCSEC_GSS_SVC_PRIVACY: | |
2d21ac55 | 985 | /* |
39037602 A |
986 | * r_gss_arglen is the length of the wrap token sans any padding length. |
987 | * Its used to find any XDR padding of the wrap token. | |
2d21ac55 | 988 | */ |
39037602 A |
989 | /* Note the mbufs that were used in r_mrest are being encapsulated in the rpc_gss_priv_data_t */ |
990 | assert(req->r_mrest == args); | |
991 | nfsm_chain_finish_mbuf(error, nmc); | |
2d21ac55 A |
992 | if (error) |
993 | return (error); | |
39037602 A |
994 | error = rpc_gss_priv_data_create(cp->gss_clnt_ctx_id, &args, seqnum, &req->r_gss_arglen); |
995 | if (error) | |
996 | break; | |
997 | req->r_mrest = args; | |
998 | req->r_gss_argoff = nfsm_chain_offset(nmc); | |
999 | nfs_gss_append_chain(nmc, args); | |
2d21ac55 | 1000 | break; |
39037602 A |
1001 | default: |
1002 | return (EINVAL); | |
2d21ac55 A |
1003 | } |
1004 | ||
1005 | return (error); | |
1006 | } | |
1007 | ||
1008 | /* | |
1009 | * When receiving a reply, the client checks the verifier | |
1010 | * returned by the server. Check that the verifier is the | |
1011 | * correct type, then extract the sequence number checksum | |
1012 | * from the token in the credential and compare it with a | |
1013 | * computed checksum of the sequence number in the request | |
1014 | * that was sent. | |
1015 | */ | |
1016 | int | |
1017 | nfs_gss_clnt_verf_get( | |
1018 | struct nfsreq *req, | |
1019 | struct nfsm_chain *nmc, | |
1020 | uint32_t verftype, | |
1021 | uint32_t verflen, | |
1022 | uint32_t *accepted_statusp) | |
1023 | { | |
39037602 | 1024 | gss_buffer_desc cksum; |
2d21ac55 | 1025 | uint32_t seqnum = 0; |
39037602 | 1026 | uint32_t major; |
2d21ac55 A |
1027 | struct nfs_gss_clnt_ctx *cp = req->r_gss_ctx; |
1028 | struct nfsm_chain nmc_tmp; | |
1029 | struct gss_seq *gsp; | |
39037602 | 1030 | uint32_t reslen, offset; |
2d21ac55 | 1031 | int error = 0; |
39037602 A |
1032 | mbuf_t results_mbuf, prev_mbuf, pad_mbuf; |
1033 | size_t ressize; | |
2d21ac55 | 1034 | |
39037602 | 1035 | reslen = 0; |
2d21ac55 A |
1036 | *accepted_statusp = 0; |
1037 | ||
1038 | if (cp == NULL) | |
b0d623f7 | 1039 | return (NFSERR_EAUTH); |
2d21ac55 A |
1040 | /* |
1041 | * If it's not an RPCSEC_GSS verifier, then it has to | |
1042 | * be a null verifier that resulted from either | |
1043 | * a CONTINUE_NEEDED reply during context setup or | |
1044 | * from the reply to an AUTH_UNIX call from a dummy | |
1045 | * context that resulted from a fallback to sec=sys. | |
1046 | */ | |
1047 | if (verftype != RPCSEC_GSS) { | |
1048 | if (verftype != RPCAUTH_NULL) | |
b0d623f7 | 1049 | return (NFSERR_EAUTH); |
fe8ab488 | 1050 | if (cp->gss_clnt_flags & GSS_CTX_COMPLETE) |
b0d623f7 | 1051 | return (NFSERR_EAUTH); |
2d21ac55 A |
1052 | if (verflen > 0) |
1053 | nfsm_chain_adv(error, nmc, nfsm_rndup(verflen)); | |
1054 | nfsm_chain_get_32(error, nmc, *accepted_statusp); | |
1055 | return (error); | |
1056 | } | |
1057 | ||
2d21ac55 A |
1058 | /* |
1059 | * If we received an RPCSEC_GSS verifier but the | |
1060 | * context isn't yet complete, then it must be | |
1061 | * the context complete message from the server. | |
1062 | * The verifier will contain an encrypted checksum | |
1063 | * of the window but we don't have the session key | |
1064 | * yet so we can't decrypt it. Stash the verifier | |
1065 | * and check it later in nfs_gss_clnt_ctx_init() when | |
1066 | * the context is complete. | |
1067 | */ | |
1068 | if (!(cp->gss_clnt_flags & GSS_CTX_COMPLETE)) { | |
1069 | MALLOC(cp->gss_clnt_verf, u_char *, verflen, M_TEMP, M_WAITOK|M_ZERO); | |
1070 | if (cp->gss_clnt_verf == NULL) | |
1071 | return (ENOMEM); | |
39037602 | 1072 | cp->gss_clnt_verflen = verflen; |
2d21ac55 A |
1073 | nfsm_chain_get_opaque(error, nmc, verflen, cp->gss_clnt_verf); |
1074 | nfsm_chain_get_32(error, nmc, *accepted_statusp); | |
1075 | return (error); | |
1076 | } | |
1077 | ||
39037602 A |
1078 | if (verflen > KRB5_MAX_MIC_SIZE) |
1079 | return (EBADRPC); | |
1080 | cksum.length = verflen; | |
1081 | MALLOC(cksum.value, void *, verflen, M_TEMP, M_WAITOK); | |
b0d623f7 | 1082 | |
2d21ac55 | 1083 | /* |
39037602 | 1084 | * Get the gss mic |
2d21ac55 | 1085 | */ |
39037602 A |
1086 | nfsm_chain_get_opaque(error, nmc, verflen, cksum.value); |
1087 | if (error) { | |
1088 | FREE(cksum.value, M_TEMP); | |
2d21ac55 | 1089 | goto nfsmout; |
39037602 | 1090 | } |
2d21ac55 A |
1091 | |
1092 | /* | |
1093 | * Search the request sequence numbers for this reply, starting | |
1094 | * with the most recent, looking for a checksum that matches | |
1095 | * the one in the verifier returned by the server. | |
1096 | */ | |
1097 | SLIST_FOREACH(gsp, &req->r_gss_seqlist, gss_seqnext) { | |
39037602 A |
1098 | gss_buffer_desc seqnum_buf; |
1099 | uint32_t network_seqnum = htonl(gsp->gss_seqnum); | |
1100 | ||
1101 | seqnum_buf.length = sizeof(network_seqnum); | |
1102 | seqnum_buf.value = &network_seqnum; | |
1103 | major = gss_krb5_verify_mic(NULL, cp->gss_clnt_ctx_id, &seqnum_buf, &cksum, NULL); | |
1104 | if (major == GSS_S_COMPLETE) | |
2d21ac55 A |
1105 | break; |
1106 | } | |
39037602 | 1107 | FREE(cksum.value, M_TEMP); |
2d21ac55 | 1108 | if (gsp == NULL) |
b0d623f7 | 1109 | return (NFSERR_EAUTH); |
2d21ac55 A |
1110 | |
1111 | /* | |
1112 | * Get the RPC accepted status | |
1113 | */ | |
1114 | nfsm_chain_get_32(error, nmc, *accepted_statusp); | |
1115 | if (*accepted_statusp != RPC_SUCCESS) | |
1116 | return (0); | |
1117 | ||
1118 | /* | |
1119 | * Now we may have to check integrity or decrypt the results | |
1120 | * per RFC 2203 Section 5.3.2 | |
1121 | */ | |
1122 | switch (cp->gss_clnt_service) { | |
1123 | case RPCSEC_GSS_SVC_NONE: | |
1124 | /* nothing to do */ | |
1125 | break; | |
1126 | case RPCSEC_GSS_SVC_INTEGRITY: | |
1127 | /* | |
39037602 | 1128 | * Here's what we expect in the integrity results from RFC 2203: |
2d21ac55 A |
1129 | * |
1130 | * - length of seq num + results (4 bytes) | |
1131 | * - sequence number (4 bytes) | |
1132 | * - results (variable bytes) | |
39037602 A |
1133 | * - length of checksum token |
1134 | * - checksum of seqnum + results | |
2d21ac55 | 1135 | */ |
39037602 | 1136 | |
2d21ac55 A |
1137 | nfsm_chain_get_32(error, nmc, reslen); // length of results |
1138 | if (reslen > NFS_MAXPACKET) { | |
1139 | error = EBADRPC; | |
1140 | goto nfsmout; | |
1141 | } | |
1142 | ||
39037602 A |
1143 | /* Advance and fetch the mic */ |
1144 | nmc_tmp = *nmc; | |
1145 | nfsm_chain_adv(error, &nmc_tmp, reslen); // skip over the results | |
1146 | nfsm_chain_get_32(error, &nmc_tmp, cksum.length); | |
1147 | MALLOC(cksum.value, void *, cksum.length, M_TEMP, M_WAITOK); | |
1148 | nfsm_chain_get_opaque(error, &nmc_tmp, cksum.length, cksum.value); | |
1149 | //XXX chop offf the cksum? | |
1150 | ||
1151 | /* Call verify mic */ | |
1152 | offset = nfsm_chain_offset(nmc); | |
1153 | major = gss_krb5_verify_mic_mbuf((uint32_t *)&error, cp->gss_clnt_ctx_id, nmc->nmc_mhead, offset, reslen, &cksum, NULL); | |
1154 | FREE(cksum.value, M_TEMP); | |
1155 | if (major != GSS_S_COMPLETE) { | |
1156 | printf("client results: gss_krb5_verify_mic_mbuf failed %d\n", error); | |
1157 | error = EBADRPC; | |
1158 | goto nfsmout; | |
1159 | } | |
2d21ac55 A |
1160 | |
1161 | /* | |
1162 | * Get the sequence number prepended to the results | |
39037602 | 1163 | * and compare it against the header. |
2d21ac55 A |
1164 | */ |
1165 | nfsm_chain_get_32(error, nmc, seqnum); | |
39037602 A |
1166 | if (gsp->gss_seqnum != seqnum) { |
1167 | error = EBADRPC; | |
1168 | goto nfsmout; | |
1169 | } | |
1170 | #if 0 | |
2d21ac55 A |
1171 | SLIST_FOREACH(gsp, &req->r_gss_seqlist, gss_seqnext) { |
1172 | if (seqnum == gsp->gss_seqnum) | |
1173 | break; | |
1174 | } | |
1175 | if (gsp == NULL) { | |
1176 | error = EBADRPC; | |
1177 | goto nfsmout; | |
1178 | } | |
39037602 | 1179 | #endif |
2d21ac55 A |
1180 | break; |
1181 | case RPCSEC_GSS_SVC_PRIVACY: | |
1182 | /* | |
1183 | * Here's what we expect in the privacy results: | |
1184 | * | |
39037602 A |
1185 | * opaque encodeing of the wrap token |
1186 | * - length of wrap token | |
1187 | * - wrap token | |
2d21ac55 | 1188 | */ |
39037602 | 1189 | prev_mbuf = nmc->nmc_mcur; |
2d21ac55 | 1190 | nfsm_chain_get_32(error, nmc, reslen); // length of results |
39037602 | 1191 | if (reslen == 0 || reslen > NFS_MAXPACKET) { |
2d21ac55 A |
1192 | error = EBADRPC; |
1193 | goto nfsmout; | |
1194 | } | |
1195 | ||
39037602 A |
1196 | /* Get the wrap token (current mbuf in the chain starting at the current offset) */ |
1197 | offset = nmc->nmc_ptr - (caddr_t)mbuf_data(nmc->nmc_mcur); | |
1198 | ||
1199 | /* split out the wrap token */ | |
1200 | ressize = reslen; | |
1201 | error = gss_normalize_mbuf(nmc->nmc_mcur, offset, &ressize, &results_mbuf, &pad_mbuf, 0); | |
2d21ac55 A |
1202 | if (error) |
1203 | goto nfsmout; | |
2d21ac55 | 1204 | |
39037602 A |
1205 | if (pad_mbuf) { |
1206 | assert(nfsm_pad(reslen) == mbuf_len(pad_mbuf)); | |
1207 | mbuf_free(pad_mbuf); | |
1208 | } | |
2d21ac55 | 1209 | |
39037602 A |
1210 | major = gss_krb5_unwrap_mbuf((uint32_t *)&error, cp->gss_clnt_ctx_id, &results_mbuf, 0, ressize, NULL, NULL); |
1211 | if (major) { | |
1212 | printf("%s unwraped failed %d\n", __func__, error); | |
2d21ac55 A |
1213 | goto nfsmout; |
1214 | } | |
1215 | ||
39037602 A |
1216 | /* Now replace the wrapped arguments with the unwrapped ones */ |
1217 | mbuf_setnext(prev_mbuf, results_mbuf); | |
1218 | nmc->nmc_mcur = results_mbuf; | |
1219 | nmc->nmc_ptr = mbuf_data(results_mbuf); | |
1220 | nmc->nmc_left = mbuf_len(results_mbuf); | |
2d21ac55 A |
1221 | |
1222 | /* | |
1223 | * Get the sequence number prepended to the results | |
39037602 | 1224 | * and compare it against the header |
2d21ac55 A |
1225 | */ |
1226 | nfsm_chain_get_32(error, nmc, seqnum); | |
39037602 A |
1227 | if (gsp->gss_seqnum != seqnum) { |
1228 | printf("%s bad seqnum\n", __func__); | |
1229 | error = EBADRPC; | |
1230 | goto nfsmout; | |
1231 | } | |
1232 | #if 0 | |
2d21ac55 A |
1233 | SLIST_FOREACH(gsp, &req->r_gss_seqlist, gss_seqnext) { |
1234 | if (seqnum == gsp->gss_seqnum) | |
1235 | break; | |
1236 | } | |
1237 | if (gsp == NULL) { | |
1238 | error = EBADRPC; | |
1239 | goto nfsmout; | |
1240 | } | |
39037602 | 1241 | #endif |
2d21ac55 A |
1242 | break; |
1243 | } | |
1244 | nfsmout: | |
1245 | return (error); | |
1246 | } | |
1247 | ||
1248 | /* | |
1249 | * An RPCSEC_GSS request with no integrity or privacy consists | |
1250 | * of just the header mbufs followed by the arg mbufs. | |
1251 | * | |
39037602 A |
1252 | * However, integrity or privacy the original mbufs have mbufs |
1253 | * prepended and appended to, which means we have to do some work to | |
1254 | * restore the arg mbuf chain to its previous state in case we need to | |
1255 | * retransmit. | |
2d21ac55 A |
1256 | * |
1257 | * The location and length of the args is marked by two fields | |
1258 | * in the request structure: r_gss_argoff and r_gss_arglen, | |
1259 | * which are stashed when the NFS request is built. | |
3e170ce0 | 1260 | */ |
2d21ac55 A |
1261 | int |
1262 | nfs_gss_clnt_args_restore(struct nfsreq *req) | |
1263 | { | |
1264 | struct nfs_gss_clnt_ctx *cp = req->r_gss_ctx; | |
1265 | struct nfsm_chain mchain, *nmc = &mchain; | |
39037602 | 1266 | int error = 0, merr; |
2d21ac55 | 1267 | |
3e170ce0 | 1268 | if (cp == NULL) |
b0d623f7 | 1269 | return (NFSERR_EAUTH); |
2d21ac55 A |
1270 | |
1271 | if ((cp->gss_clnt_flags & GSS_CTX_COMPLETE) == 0) | |
1272 | return (ENEEDAUTH); | |
1273 | ||
39037602 A |
1274 | /* Nothing to restore for SVC_NONE */ |
1275 | if (cp->gss_clnt_service == RPCSEC_GSS_SVC_NONE) | |
1276 | return (0); | |
1277 | ||
2d21ac55 A |
1278 | nfsm_chain_dissect_init(error, nmc, req->r_mhead); // start at RPC header |
1279 | nfsm_chain_adv(error, nmc, req->r_gss_argoff); // advance to args | |
1280 | if (error) | |
1281 | return (error); | |
1282 | ||
39037602 A |
1283 | if (cp->gss_clnt_service == RPCSEC_GSS_SVC_INTEGRITY) |
1284 | error = rpc_gss_integ_data_restore(cp->gss_clnt_ctx_id, &req->r_mrest, req->r_gss_arglen); | |
1285 | else | |
1286 | error = rpc_gss_priv_data_restore(cp->gss_clnt_ctx_id, &req->r_mrest, req->r_gss_arglen); | |
2d21ac55 | 1287 | |
39037602 A |
1288 | merr = mbuf_setnext(nmc->nmc_mcur, req->r_mrest); /* Should always succeed */ |
1289 | assert (merr == 0); | |
2d21ac55 | 1290 | |
39037602 | 1291 | return (error ? error : merr); |
2d21ac55 A |
1292 | } |
1293 | ||
1294 | /* | |
1295 | * This function sets up a new context on the client. | |
1296 | * Context setup alternates upcalls to the gssd with NFS nullproc calls | |
1297 | * to the server. Each of these calls exchanges an opaque token, obtained | |
1298 | * via the gssd's calls into the GSS-API on either the client or the server. | |
1299 | * This cycle of calls ends when the client's upcall to the gssd and the | |
1300 | * server's response both return GSS_S_COMPLETE. At this point, the client | |
1301 | * should have its session key and a handle that it can use to refer to its | |
1302 | * new context on the server. | |
1303 | */ | |
1304 | static int | |
1305 | nfs_gss_clnt_ctx_init(struct nfsreq *req, struct nfs_gss_clnt_ctx *cp) | |
1306 | { | |
1307 | struct nfsmount *nmp = req->r_nmp; | |
39037602 A |
1308 | gss_buffer_desc cksum, window; |
1309 | uint32_t network_seqnum; | |
2d21ac55 A |
1310 | int client_complete = 0; |
1311 | int server_complete = 0; | |
2d21ac55 | 1312 | int error = 0; |
39037602 A |
1313 | int retrycnt = 0; |
1314 | uint32_t major; | |
2d21ac55 A |
1315 | |
1316 | /* Initialize a new client context */ | |
1317 | ||
2d21ac55 | 1318 | if (cp->gss_clnt_svcname == NULL) { |
fe8ab488 A |
1319 | cp->gss_clnt_svcname = nfs_gss_clnt_svcname(nmp, &cp->gss_clnt_svcnt, &cp->gss_clnt_svcnamlen); |
1320 | if (cp->gss_clnt_svcname == NULL) { | |
1321 | error = NFSERR_EAUTH; | |
1322 | goto nfsmout; | |
1323 | } | |
2d21ac55 | 1324 | } |
b0d623f7 | 1325 | |
2d21ac55 A |
1326 | cp->gss_clnt_proc = RPCSEC_GSS_INIT; |
1327 | ||
1328 | cp->gss_clnt_service = | |
6d2010ae A |
1329 | req->r_auth == RPCAUTH_KRB5 ? RPCSEC_GSS_SVC_NONE : |
1330 | req->r_auth == RPCAUTH_KRB5I ? RPCSEC_GSS_SVC_INTEGRITY : | |
1331 | req->r_auth == RPCAUTH_KRB5P ? RPCSEC_GSS_SVC_PRIVACY : 0; | |
2d21ac55 A |
1332 | |
1333 | /* | |
1334 | * Now loop around alternating gss_init_sec_context and | |
1335 | * gss_accept_sec_context upcalls to the gssd on the client | |
1336 | * and server side until the context is complete - or fails. | |
1337 | */ | |
1338 | for (;;) { | |
b0d623f7 | 1339 | retry: |
2d21ac55 | 1340 | /* Upcall to the gss_init_sec_context in the gssd */ |
39037602 | 1341 | error = nfs_gss_clnt_gssd_upcall(req, cp, retrycnt); |
2d21ac55 A |
1342 | if (error) |
1343 | goto nfsmout; | |
1344 | ||
1345 | if (cp->gss_clnt_major == GSS_S_COMPLETE) { | |
1346 | client_complete = 1; | |
39037602 | 1347 | NFS_GSS_DBG("Client complete\n"); |
2d21ac55 A |
1348 | if (server_complete) |
1349 | break; | |
1350 | } else if (cp->gss_clnt_major != GSS_S_CONTINUE_NEEDED) { | |
39037602 A |
1351 | /* |
1352 | * We may have gotten here because the accept sec context | |
1353 | * from the server failed and sent back a GSS token that | |
1354 | * encapsulates a kerberos error token per RFC 1964/4121 | |
1355 | * with a status of GSS_S_CONTINUE_NEEDED. That caused us | |
1356 | * to loop to the above up call and received the now | |
1357 | * decoded errors. | |
1358 | */ | |
1359 | retrycnt++; | |
1360 | cp->gss_clnt_gssd_flags |= GSSD_RESTART; | |
1361 | NFS_GSS_DBG("Retrying major = %x minor = %d\n", cp->gss_clnt_major, (int)cp->gss_clnt_minor); | |
1362 | goto retry; | |
2d21ac55 A |
1363 | } |
1364 | ||
1365 | /* | |
1366 | * Pass the token to the server. | |
1367 | */ | |
1368 | error = nfs_gss_clnt_ctx_callserver(req, cp); | |
b0d623f7 | 1369 | if (error) { |
39037602 A |
1370 | if (error == ENEEDAUTH && |
1371 | (cp->gss_clnt_proc == RPCSEC_GSS_INIT || | |
1372 | cp->gss_clnt_proc == RPCSEC_GSS_CONTINUE_INIT)) { | |
1373 | /* | |
1374 | * We got here because the server had a problem | |
1375 | * trying to establish a context and sent that there | |
1376 | * was a context problem at the rpc sec layer. Perhaps | |
1377 | * gss_accept_sec_context succeeded in user space, | |
1378 | * but the kernel could not handle the etype | |
1379 | * to generate the mic for the verifier of the rpc_sec | |
1380 | * window size. | |
1381 | */ | |
1382 | retrycnt++; | |
1383 | cp->gss_clnt_gssd_flags |= GSSD_RESTART; | |
1384 | NFS_GSS_DBG("Retrying major = %x minor = %d\n", cp->gss_clnt_major, (int)cp->gss_clnt_minor); | |
b0d623f7 A |
1385 | goto retry; |
1386 | } | |
2d21ac55 | 1387 | goto nfsmout; |
b0d623f7 | 1388 | } |
2d21ac55 | 1389 | if (cp->gss_clnt_major == GSS_S_COMPLETE) { |
39037602 | 1390 | NFS_GSS_DBG("Server complete\n"); |
2d21ac55 A |
1391 | server_complete = 1; |
1392 | if (client_complete) | |
1393 | break; | |
39037602 A |
1394 | } else if (cp->gss_clnt_major == GSS_S_CONTINUE_NEEDED) { |
1395 | cp->gss_clnt_proc = RPCSEC_GSS_CONTINUE_INIT; | |
1396 | } else { | |
1397 | /* Server didn't like us. Try something else */ | |
1398 | retrycnt++; | |
1399 | cp->gss_clnt_gssd_flags |= GSSD_RESTART; | |
1400 | NFS_GSS_DBG("Retrying major = %x minor = %d\n", cp->gss_clnt_major, (int)cp->gss_clnt_minor); | |
2d21ac55 | 1401 | } |
2d21ac55 A |
1402 | } |
1403 | ||
1404 | /* | |
1405 | * The context is apparently established successfully | |
1406 | */ | |
6d2010ae | 1407 | lck_mtx_lock(cp->gss_clnt_mtx); |
2d21ac55 | 1408 | cp->gss_clnt_flags |= GSS_CTX_COMPLETE; |
6d2010ae | 1409 | lck_mtx_unlock(cp->gss_clnt_mtx); |
2d21ac55 | 1410 | cp->gss_clnt_proc = RPCSEC_GSS_DATA; |
2d21ac55 | 1411 | |
39037602 A |
1412 | network_seqnum = htonl(cp->gss_clnt_seqwin); |
1413 | window.length = sizeof (cp->gss_clnt_seqwin); | |
1414 | window.value = &network_seqnum; | |
1415 | cksum.value = cp->gss_clnt_verf; | |
1416 | cksum.length = cp->gss_clnt_verflen; | |
1417 | major = gss_krb5_verify_mic((uint32_t *)&error, cp->gss_clnt_ctx_id, &window, &cksum, NULL); | |
1418 | cp->gss_clnt_verflen = 0; | |
2d21ac55 A |
1419 | FREE(cp->gss_clnt_verf, M_TEMP); |
1420 | cp->gss_clnt_verf = NULL; | |
39037602 A |
1421 | if (major != GSS_S_COMPLETE) { |
1422 | printf("%s: could not verify window\n", __func__); | |
b0d623f7 | 1423 | error = NFSERR_EAUTH; |
2d21ac55 A |
1424 | goto nfsmout; |
1425 | } | |
1426 | ||
1427 | /* | |
1428 | * Set an initial sequence number somewhat randomized. | |
1429 | * Start small so we don't overflow GSS_MAXSEQ too quickly. | |
1430 | * Add the size of the sequence window so seqbits arithmetic | |
1431 | * doesn't go negative. | |
1432 | */ | |
1433 | cp->gss_clnt_seqnum = (random() & 0xffff) + cp->gss_clnt_seqwin; | |
1434 | ||
1435 | /* | |
1436 | * Allocate a bitmap to keep track of which requests | |
1437 | * are pending within the sequence number window. | |
1438 | */ | |
1439 | MALLOC(cp->gss_clnt_seqbits, uint32_t *, | |
1440 | nfsm_rndup((cp->gss_clnt_seqwin + 7) / 8), M_TEMP, M_WAITOK|M_ZERO); | |
1441 | if (cp->gss_clnt_seqbits == NULL) | |
b0d623f7 | 1442 | error = NFSERR_EAUTH; |
39037602 | 1443 | |
2d21ac55 | 1444 | nfsmout: |
3e170ce0 | 1445 | /* |
b0d623f7 A |
1446 | * If the error is ENEEDAUTH we're not done, so no need |
1447 | * to wake up other threads again. This thread will retry in | |
1448 | * the find or renew routines. | |
1449 | */ | |
39037602 A |
1450 | if (error == ENEEDAUTH) { |
1451 | NFS_GSS_DBG("Returning ENEEDAUTH\n"); | |
b0d623f7 | 1452 | return (error); |
39037602 | 1453 | } |
b0d623f7 | 1454 | |
2d21ac55 A |
1455 | /* |
1456 | * If there's an error, just mark it as invalid. | |
1457 | * It will be removed when the reference count | |
1458 | * drops to zero. | |
1459 | */ | |
6d2010ae | 1460 | lck_mtx_lock(cp->gss_clnt_mtx); |
2d21ac55 A |
1461 | if (error) |
1462 | cp->gss_clnt_flags |= GSS_CTX_INVAL; | |
1463 | ||
1464 | /* | |
1465 | * Wake any threads waiting to use the context | |
1466 | */ | |
2d21ac55 A |
1467 | cp->gss_clnt_thread = NULL; |
1468 | if (cp->gss_clnt_flags & GSS_NEEDCTX) { | |
1469 | cp->gss_clnt_flags &= ~GSS_NEEDCTX; | |
1470 | wakeup(cp); | |
1471 | } | |
1472 | lck_mtx_unlock(cp->gss_clnt_mtx); | |
1473 | ||
39037602 | 1474 | NFS_GSS_DBG("Returning error = %d\n", error); |
2d21ac55 A |
1475 | return (error); |
1476 | } | |
1477 | ||
6d2010ae A |
1478 | /* |
1479 | * This function calls nfs_gss_clnt_ctx_init() to set up a new context. | |
1480 | * But if there's a failure in trying to establish the context it keeps | |
1481 | * retrying at progressively longer intervals in case the failure is | |
1482 | * due to some transient condition. For instance, the server might be | |
1483 | * failing the context setup because directory services is not coming | |
1484 | * up in a timely fashion. | |
1485 | */ | |
1486 | static int | |
1487 | nfs_gss_clnt_ctx_init_retry(struct nfsreq *req, struct nfs_gss_clnt_ctx *cp) | |
1488 | { | |
1489 | struct nfsmount *nmp = req->r_nmp; | |
1490 | struct timeval now; | |
1491 | time_t waituntil; | |
1492 | int error, slpflag; | |
1493 | int retries = 0; | |
1494 | int timeo = NFS_TRYLATERDEL; | |
1495 | ||
fe8ab488 | 1496 | if (nfs_mount_gone(nmp)) { |
6d2010ae A |
1497 | error = ENXIO; |
1498 | goto bad; | |
1499 | } | |
1500 | ||
1501 | /* For an "intr" mount allow a signal to interrupt the retries */ | |
1502 | slpflag = (NMFLAG(nmp, INTR) && !(req->r_flags & R_NOINTR)) ? PCATCH : 0; | |
1503 | ||
1504 | while ((error = nfs_gss_clnt_ctx_init(req, cp)) == ENEEDAUTH) { | |
1505 | microuptime(&now); | |
1506 | waituntil = now.tv_sec + timeo; | |
1507 | while (now.tv_sec < waituntil) { | |
39236c6e | 1508 | tsleep(NULL, PSOCK | slpflag, "nfs_gss_clnt_ctx_init_retry", hz); |
6d2010ae A |
1509 | slpflag = 0; |
1510 | error = nfs_sigintr(req->r_nmp, req, current_thread(), 0); | |
1511 | if (error) | |
1512 | goto bad; | |
1513 | microuptime(&now); | |
1514 | } | |
1515 | ||
1516 | retries++; | |
1517 | /* If it's a soft mount just give up after a while */ | |
fe8ab488 | 1518 | if ((NMFLAG(nmp, SOFT) || (req->r_flags & R_SOFT)) && (retries > nmp->nm_retry)) { |
6d2010ae A |
1519 | error = ETIMEDOUT; |
1520 | goto bad; | |
1521 | } | |
1522 | timeo *= 2; | |
1523 | if (timeo > 60) | |
1524 | timeo = 60; | |
1525 | } | |
1526 | ||
1527 | if (error == 0) | |
1528 | return 0; // success | |
1529 | bad: | |
1530 | /* | |
1531 | * Give up on this context | |
1532 | */ | |
1533 | lck_mtx_lock(cp->gss_clnt_mtx); | |
1534 | cp->gss_clnt_flags |= GSS_CTX_INVAL; | |
1535 | ||
1536 | /* | |
1537 | * Wake any threads waiting to use the context | |
1538 | */ | |
1539 | cp->gss_clnt_thread = NULL; | |
1540 | if (cp->gss_clnt_flags & GSS_NEEDCTX) { | |
1541 | cp->gss_clnt_flags &= ~GSS_NEEDCTX; | |
1542 | wakeup(cp); | |
1543 | } | |
1544 | lck_mtx_unlock(cp->gss_clnt_mtx); | |
1545 | ||
1546 | return error; | |
1547 | } | |
1548 | ||
2d21ac55 A |
1549 | /* |
1550 | * Call the NFS server using a null procedure for context setup. | |
1551 | * Even though it's a null procedure and nominally has no arguments | |
1552 | * RFC 2203 requires that the GSS-API token be passed as an argument | |
1553 | * and received as a reply. | |
1554 | */ | |
1555 | static int | |
1556 | nfs_gss_clnt_ctx_callserver(struct nfsreq *req, struct nfs_gss_clnt_ctx *cp) | |
1557 | { | |
2d21ac55 A |
1558 | struct nfsm_chain nmreq, nmrep; |
1559 | int error = 0, status; | |
fe8ab488 | 1560 | uint32_t major = cp->gss_clnt_major, minor = cp->gss_clnt_minor; |
2d21ac55 A |
1561 | int sz; |
1562 | ||
fe8ab488 | 1563 | if (nfs_mount_gone(req->r_nmp)) |
b0d623f7 | 1564 | return (ENXIO); |
2d21ac55 A |
1565 | nfsm_chain_null(&nmreq); |
1566 | nfsm_chain_null(&nmrep); | |
1567 | sz = NFSX_UNSIGNED + nfsm_rndup(cp->gss_clnt_tokenlen); | |
1568 | nfsm_chain_build_alloc_init(error, &nmreq, sz); | |
1569 | nfsm_chain_add_32(error, &nmreq, cp->gss_clnt_tokenlen); | |
b0d623f7 A |
1570 | if (cp->gss_clnt_tokenlen > 0) |
1571 | nfsm_chain_add_opaque(error, &nmreq, cp->gss_clnt_token, cp->gss_clnt_tokenlen); | |
2d21ac55 A |
1572 | nfsm_chain_build_done(error, &nmreq); |
1573 | if (error) | |
1574 | goto nfsmout; | |
1575 | ||
1576 | /* Call the server */ | |
b0d623f7 A |
1577 | error = nfs_request_gss(req->r_nmp->nm_mountp, &nmreq, req->r_thread, req->r_cred, |
1578 | (req->r_flags & R_OPTMASK), cp, &nmrep, &status); | |
2d21ac55 A |
1579 | if (cp->gss_clnt_token != NULL) { |
1580 | FREE(cp->gss_clnt_token, M_TEMP); | |
1581 | cp->gss_clnt_token = NULL; | |
1582 | } | |
1583 | if (!error) | |
1584 | error = status; | |
1585 | if (error) | |
1586 | goto nfsmout; | |
1587 | ||
1588 | /* Get the server's reply */ | |
1589 | ||
1590 | nfsm_chain_get_32(error, &nmrep, cp->gss_clnt_handle_len); | |
b0d623f7 | 1591 | if (cp->gss_clnt_handle != NULL) { |
2d21ac55 | 1592 | FREE(cp->gss_clnt_handle, M_TEMP); |
b0d623f7 A |
1593 | cp->gss_clnt_handle = NULL; |
1594 | } | |
2d21ac55 A |
1595 | if (cp->gss_clnt_handle_len > 0) { |
1596 | MALLOC(cp->gss_clnt_handle, u_char *, cp->gss_clnt_handle_len, M_TEMP, M_WAITOK); | |
1597 | if (cp->gss_clnt_handle == NULL) { | |
1598 | error = ENOMEM; | |
1599 | goto nfsmout; | |
1600 | } | |
1601 | nfsm_chain_get_opaque(error, &nmrep, cp->gss_clnt_handle_len, cp->gss_clnt_handle); | |
1602 | } | |
1603 | nfsm_chain_get_32(error, &nmrep, cp->gss_clnt_major); | |
1604 | nfsm_chain_get_32(error, &nmrep, cp->gss_clnt_minor); | |
1605 | nfsm_chain_get_32(error, &nmrep, cp->gss_clnt_seqwin); | |
1606 | nfsm_chain_get_32(error, &nmrep, cp->gss_clnt_tokenlen); | |
1607 | if (error) | |
1608 | goto nfsmout; | |
1609 | if (cp->gss_clnt_tokenlen > 0) { | |
1610 | MALLOC(cp->gss_clnt_token, u_char *, cp->gss_clnt_tokenlen, M_TEMP, M_WAITOK); | |
1611 | if (cp->gss_clnt_token == NULL) { | |
1612 | error = ENOMEM; | |
1613 | goto nfsmout; | |
1614 | } | |
1615 | nfsm_chain_get_opaque(error, &nmrep, cp->gss_clnt_tokenlen, cp->gss_clnt_token); | |
1616 | } | |
1617 | ||
1618 | /* | |
1619 | * Make sure any unusual errors are expanded and logged by gssd | |
1620 | */ | |
1621 | if (cp->gss_clnt_major != GSS_S_COMPLETE && | |
1622 | cp->gss_clnt_major != GSS_S_CONTINUE_NEEDED) { | |
2d21ac55 | 1623 | |
fe8ab488 A |
1624 | printf("nfs_gss_clnt_ctx_callserver: gss_clnt_major = %d\n", cp->gss_clnt_major); |
1625 | nfs_gss_clnt_log_error(req, cp, major, minor); | |
1626 | ||
2d21ac55 A |
1627 | } |
1628 | ||
1629 | nfsmout: | |
1630 | nfsm_chain_cleanup(&nmreq); | |
1631 | nfsm_chain_cleanup(&nmrep); | |
1632 | ||
1633 | return (error); | |
1634 | } | |
1635 | ||
1636 | /* | |
39236c6e A |
1637 | * We construct the service principal as a gss hostbased service principal of |
1638 | * the form nfs@<server>, unless the servers principal was passed down in the | |
1639 | * mount arguments. If the arguments don't specify the service principal, the | |
1640 | * server name is extracted the location passed in the mount argument if | |
1641 | * available. Otherwise assume a format of <server>:<path> in the | |
1642 | * mntfromname. We don't currently support url's or other bizarre formats like | |
1643 | * path@server. Mount_url will convert the nfs url into <server>:<path> when | |
1644 | * calling mount, so this works out well in practice. | |
1645 | * | |
2d21ac55 | 1646 | */ |
39236c6e A |
1647 | |
1648 | static uint8_t * | |
1649 | nfs_gss_clnt_svcname(struct nfsmount *nmp, gssd_nametype *nt, uint32_t *len) | |
2d21ac55 | 1650 | { |
39236c6e A |
1651 | char *svcname, *d, *server; |
1652 | int lindx, sindx; | |
2d21ac55 | 1653 | |
fe8ab488 | 1654 | if (nfs_mount_gone(nmp)) |
b0d623f7 | 1655 | return (NULL); |
2d21ac55 | 1656 | |
39236c6e A |
1657 | if (nmp->nm_sprinc) { |
1658 | *len = strlen(nmp->nm_sprinc) + 1; | |
1659 | MALLOC(svcname, char *, *len, M_TEMP, M_WAITOK); | |
1660 | *nt = GSSD_HOSTBASED; | |
1661 | if (svcname == NULL) | |
1662 | return (NULL); | |
1663 | strlcpy(svcname, nmp->nm_sprinc, *len); | |
1664 | ||
1665 | return ((uint8_t *)svcname); | |
1666 | } | |
1667 | ||
1668 | *nt = GSSD_HOSTBASED; | |
1669 | if (nmp->nm_locations.nl_numlocs && !(NFS_GSS_ISDBG && (NFS_DEBUG_FLAGS & 0x1))) { | |
1670 | lindx = nmp->nm_locations.nl_current.nli_loc; | |
1671 | sindx = nmp->nm_locations.nl_current.nli_serv; | |
1672 | server = nmp->nm_locations.nl_locations[lindx]->nl_servers[sindx]->ns_name; | |
1673 | *len = (uint32_t)strlen(server); | |
1674 | } else { | |
1675 | /* Older binaries using older mount args end up here */ | |
1676 | server = vfs_statfs(nmp->nm_mountp)->f_mntfromname; | |
1677 | NFS_GSS_DBG("nfs getting gss svcname from %s\n", server); | |
1678 | d = strchr(server, ':'); | |
1679 | *len = (uint32_t)(d ? (d - server) : strlen(server)); | |
1680 | } | |
1681 | ||
1682 | *len += 5; /* "nfs@" plus null */ | |
1683 | MALLOC(svcname, char *, *len, M_TEMP, M_WAITOK); | |
1684 | strlcpy(svcname, "nfs", *len); | |
1685 | strlcat(svcname, "@", *len); | |
1686 | strlcat(svcname, server, *len); | |
1687 | NFS_GSS_DBG("nfs svcname = %s\n", svcname); | |
1688 | ||
1689 | return ((uint8_t *)svcname); | |
2d21ac55 A |
1690 | } |
1691 | ||
316670eb A |
1692 | /* |
1693 | * Get a mach port to talk to gssd. | |
1694 | * gssd lives in the root bootstrap, so we call gssd's lookup routine | |
1695 | * to get a send right to talk to a new gssd instance that launchd has launched | |
1696 | * based on the cred's uid and audit session id. | |
1697 | */ | |
316670eb A |
1698 | |
1699 | static mach_port_t | |
1700 | nfs_gss_clnt_get_upcall_port(kauth_cred_t credp) | |
1701 | { | |
1702 | mach_port_t gssd_host_port, uc_port = IPC_PORT_NULL; | |
1703 | kern_return_t kr; | |
1704 | au_asid_t asid; | |
1705 | uid_t uid; | |
1706 | ||
1707 | kr = host_get_gssd_port(host_priv_self(), &gssd_host_port); | |
1708 | if (kr != KERN_SUCCESS) { | |
1709 | printf("nfs_gss_get_upcall_port: can't get gssd port, status %x (%d)\n", kr, kr); | |
1710 | return (IPC_PORT_NULL); | |
1711 | } | |
1712 | if (!IPC_PORT_VALID(gssd_host_port)) { | |
1713 | printf("nfs_gss_get_upcall_port: gssd port not valid\n"); | |
1714 | return (IPC_PORT_NULL); | |
1715 | } | |
1716 | ||
1717 | asid = kauth_cred_getasid(credp); | |
1718 | uid = kauth_cred_getauid(credp); | |
1719 | if (uid == AU_DEFAUDITID) | |
1720 | uid = kauth_cred_getuid(credp); | |
1721 | kr = mach_gss_lookup(gssd_host_port, uid, asid, &uc_port); | |
1722 | if (kr != KERN_SUCCESS) | |
1723 | printf("nfs_gss_clnt_get_upcall_port: mach_gssd_lookup failed: status %x (%d)\n", kr, kr); | |
3e170ce0 | 1724 | host_release_special_port(gssd_host_port); |
316670eb A |
1725 | |
1726 | return (uc_port); | |
1727 | } | |
1728 | ||
fe8ab488 A |
1729 | |
1730 | static void | |
1731 | nfs_gss_clnt_log_error(struct nfsreq *req, struct nfs_gss_clnt_ctx *cp, uint32_t major, uint32_t minor) | |
1732 | { | |
1733 | #define GETMAJERROR(x) (((x) >> GSS_C_ROUTINE_ERROR_OFFSET) & GSS_C_ROUTINE_ERROR_MASK) | |
1734 | struct nfsmount *nmp = req->r_nmp; | |
1735 | char who[] = "client"; | |
1736 | uint32_t gss_error = GETMAJERROR(cp->gss_clnt_major); | |
1737 | const char *procn = "unkown"; | |
1738 | proc_t proc; | |
1739 | pid_t pid = -1; | |
1740 | struct timeval now; | |
1741 | ||
1742 | if (req->r_thread) { | |
1743 | proc = (proc_t)get_bsdthreadtask_info(req->r_thread); | |
1744 | if (proc != NULL && (proc->p_fd == NULL || (proc->p_lflag & P_LVFORK))) | |
1745 | proc = NULL; | |
1746 | if (proc) { | |
1747 | if (*proc->p_comm) | |
1748 | procn = proc->p_comm; | |
1749 | pid = proc->p_pid; | |
1750 | } | |
1751 | } else { | |
1752 | procn = "kernproc"; | |
1753 | pid = 0; | |
1754 | } | |
1755 | ||
1756 | microuptime(&now); | |
1757 | if ((cp->gss_clnt_major != major || cp->gss_clnt_minor != minor || | |
1758 | cp->gss_clnt_ptime + GSS_PRINT_DELAY < now.tv_sec) && | |
1759 | (nmp->nm_state & NFSSTA_MOUNTED)) { | |
1760 | /* | |
1761 | * Will let gssd do some logging in hopes that it can translate | |
1762 | * the minor code. | |
1763 | */ | |
1764 | if (cp->gss_clnt_minor && cp->gss_clnt_minor != minor) { | |
1765 | (void) mach_gss_log_error( | |
1766 | cp->gss_clnt_mport, | |
1767 | vfs_statfs(nmp->nm_mountp)->f_mntfromname, | |
1768 | kauth_cred_getuid(cp->gss_clnt_cred), | |
1769 | who, | |
1770 | cp->gss_clnt_major, | |
1771 | cp->gss_clnt_minor); | |
1772 | } | |
1773 | gss_error = gss_error ? gss_error : cp->gss_clnt_major; | |
1774 | ||
1775 | /* | |
1776 | *%%% It would be really nice to get the terminal from the proc or auditinfo_addr struct and print that here. | |
1777 | */ | |
1778 | printf("NFS: gssd auth failure by %s on audit session %d uid %d proc %s/%d for mount %s. Error: major = %d minor = %d\n", | |
1779 | cp->gss_clnt_display ? cp->gss_clnt_display : who, kauth_cred_getasid(req->r_cred), kauth_cred_getuid(req->r_cred), | |
1780 | procn, pid, vfs_statfs(nmp->nm_mountp)->f_mntfromname, gss_error, (int32_t)cp->gss_clnt_minor); | |
1781 | cp->gss_clnt_ptime = now.tv_sec; | |
1782 | switch (gss_error) { | |
1783 | case 7: printf("NFS: gssd does not have credentials for session %d/%d, (kinit)?\n", | |
1784 | kauth_cred_getasid(req->r_cred), kauth_cred_getauid(req->r_cred)); | |
1785 | break; | |
1786 | case 11: printf("NFS: gssd has expired credentals for session %d/%d, (kinit)?\n", | |
1787 | kauth_cred_getasid(req->r_cred), kauth_cred_getauid(req->r_cred)); | |
1788 | break; | |
1789 | } | |
1790 | } else { | |
1791 | NFS_GSS_DBG("NFS: gssd auth failure by %s on audit session %d uid %d proc %s/%d for mount %s. Error: major = %d minor = %d\n", | |
1792 | cp->gss_clnt_display ? cp->gss_clnt_display : who, kauth_cred_getasid(req->r_cred), kauth_cred_getuid(req->r_cred), | |
1793 | procn, pid, vfs_statfs(nmp->nm_mountp)->f_mntfromname, gss_error, (int32_t)cp->gss_clnt_minor); | |
1794 | } | |
1795 | } | |
1796 | ||
2d21ac55 A |
1797 | /* |
1798 | * Make an upcall to the gssd using Mach RPC | |
316670eb | 1799 | * The upcall is made using a host special port. |
2d21ac55 A |
1800 | * This allows launchd to fire up the gssd in the |
1801 | * user's session. This is important, since gssd | |
1802 | * must have access to the user's credential cache. | |
1803 | */ | |
1804 | static int | |
39037602 | 1805 | nfs_gss_clnt_gssd_upcall(struct nfsreq *req, struct nfs_gss_clnt_ctx *cp, uint32_t retrycnt) |
2d21ac55 A |
1806 | { |
1807 | kern_return_t kr; | |
39037602 A |
1808 | gssd_byte_buffer octx = NULL; |
1809 | uint32_t lucidlen = 0; | |
1810 | void *lucid_ctx_buffer; | |
2d21ac55 A |
1811 | int retry_cnt = 0; |
1812 | vm_map_copy_t itoken = NULL; | |
6d2010ae | 1813 | gssd_byte_buffer otoken = NULL; |
b0d623f7 | 1814 | mach_msg_type_number_t otokenlen; |
2d21ac55 | 1815 | int error = 0; |
39236c6e A |
1816 | uint8_t *principal = NULL; |
1817 | uint32_t plen = 0; | |
1818 | int32_t nt = GSSD_STRING_NAME; | |
1819 | vm_map_copy_t pname = NULL; | |
1820 | vm_map_copy_t svcname = NULL; | |
1821 | char display_name[MAX_DISPLAY_STR] = ""; | |
b0d623f7 | 1822 | uint32_t ret_flags; |
39037602 | 1823 | struct nfsmount *nmp = req->r_nmp; |
fe8ab488 | 1824 | uint32_t major = cp->gss_clnt_major, minor = cp->gss_clnt_minor; |
39037602 A |
1825 | uint32_t selected = (uint32_t)-1; |
1826 | struct nfs_etype etype; | |
1827 | ||
1828 | if (nmp == NULL || vfs_isforce(nmp->nm_mountp) || (nmp->nm_state & (NFSSTA_FORCE | NFSSTA_DEAD))) | |
1829 | return (ENXIO); | |
1830 | ||
1831 | if (cp->gss_clnt_gssd_flags & GSSD_RESTART) { | |
1832 | if (cp->gss_clnt_token) | |
1833 | FREE(cp->gss_clnt_token, M_TEMP); | |
1834 | cp->gss_clnt_token = NULL; | |
1835 | cp->gss_clnt_tokenlen = 0; | |
1836 | cp->gss_clnt_proc = RPCSEC_GSS_INIT; | |
5ba3f43e A |
1837 | /* Server's handle isn't valid. Don't reuse */ |
1838 | cp->gss_clnt_handle_len = 0; | |
1839 | if (cp->gss_clnt_handle != NULL) { | |
1840 | FREE(cp->gss_clnt_handle, M_TEMP); | |
1841 | cp->gss_clnt_handle = NULL; | |
1842 | } | |
39037602 A |
1843 | } |
1844 | ||
1845 | NFS_GSS_DBG("Retrycnt = %d nm_etype.count = %d\n", retrycnt, nmp->nm_etype.count); | |
1846 | if (retrycnt >= nmp->nm_etype.count) | |
1847 | return (EACCES); | |
1848 | ||
1849 | /* Copy the mount etypes to an order set of etypes to try */ | |
1850 | etype = nmp->nm_etype; | |
1851 | ||
1852 | /* | |
1853 | * If we've already selected an etype, lets put that first in our | |
1854 | * array of etypes to try, since overwhelmingly, that is likely | |
1855 | * to be the etype we want. | |
1856 | */ | |
1857 | if (etype.selected < etype.count) { | |
1858 | etype.etypes[0] = nmp->nm_etype.etypes[etype.selected]; | |
1859 | for (uint32_t i = 0; i < etype.selected; i++) | |
1860 | etype.etypes[i+1] = nmp->nm_etype.etypes[i]; | |
1861 | for (uint32_t i = etype.selected + 1; i < etype.count; i++) | |
1862 | etype.etypes[i] = nmp->nm_etype.etypes[i]; | |
1863 | } | |
1864 | ||
1865 | /* Remove the ones we've already have tried */ | |
1866 | for (uint32_t i = retrycnt; i < etype.count; i++) | |
1867 | etype.etypes[i - retrycnt] = etype.etypes[i]; | |
1868 | etype.count = etype.count - retrycnt; | |
1869 | ||
1870 | NFS_GSS_DBG("etype count = %d preferred etype = %d\n", etype.count, etype.etypes[0]); | |
1871 | ||
1872 | /* | |
1873 | * NFS currently only supports default principals or | |
1874 | * principals based on the uid of the caller, unless | |
1875 | * the principal to use for the mounting cred was specified | |
1876 | * in the mount argmuments. If the realm to use was specified | |
1877 | * then will send that up as the principal since the realm is | |
39236c6e A |
1878 | * preceed by an "@" gssd that will try and select the default |
1879 | * principal for that realm. | |
2d21ac55 | 1880 | */ |
39236c6e | 1881 | |
39236c6e A |
1882 | if (cp->gss_clnt_principal && cp->gss_clnt_prinlen) { |
1883 | principal = cp->gss_clnt_principal; | |
1884 | plen = cp->gss_clnt_prinlen; | |
1885 | nt = cp->gss_clnt_prinnt; | |
1886 | } else if (nmp->nm_principal && IS_VALID_CRED(nmp->nm_mcred) && req->r_cred == nmp->nm_mcred) { | |
1887 | plen = (uint32_t)strlen(nmp->nm_principal); | |
5ba3f43e | 1888 | principal = (uint8_t *)nmp->nm_principal; |
39236c6e A |
1889 | cp->gss_clnt_prinnt = nt = GSSD_USER; |
1890 | } | |
1891 | else if (nmp->nm_realm) { | |
1892 | plen = (uint32_t)strlen(nmp->nm_realm); | |
1893 | principal = (uint8_t *)nmp->nm_realm; | |
1894 | nt = GSSD_USER; | |
1895 | } | |
1896 | ||
6d2010ae | 1897 | if (!IPC_PORT_VALID(cp->gss_clnt_mport)) { |
316670eb A |
1898 | cp->gss_clnt_mport = nfs_gss_clnt_get_upcall_port(req->r_cred); |
1899 | if (cp->gss_clnt_mport == IPC_PORT_NULL) | |
b0d623f7 | 1900 | goto out; |
2d21ac55 A |
1901 | } |
1902 | ||
39236c6e A |
1903 | if (plen) |
1904 | nfs_gss_mach_alloc_buffer(principal, plen, &pname); | |
1905 | if (cp->gss_clnt_svcnamlen) | |
1906 | nfs_gss_mach_alloc_buffer(cp->gss_clnt_svcname, cp->gss_clnt_svcnamlen, &svcname); | |
1907 | if (cp->gss_clnt_tokenlen) | |
2d21ac55 A |
1908 | nfs_gss_mach_alloc_buffer(cp->gss_clnt_token, cp->gss_clnt_tokenlen, &itoken); |
1909 | ||
39037602 A |
1910 | /* Always want to export the lucid context */ |
1911 | cp->gss_clnt_gssd_flags |= GSSD_LUCID_CONTEXT; | |
1912 | ||
2d21ac55 | 1913 | retry: |
39037602 | 1914 | kr = mach_gss_init_sec_context_v3( |
2d21ac55 | 1915 | cp->gss_clnt_mport, |
6d2010ae A |
1916 | GSSD_KRB5_MECH, |
1917 | (gssd_byte_buffer) itoken, (mach_msg_type_number_t) cp->gss_clnt_tokenlen, | |
39236c6e A |
1918 | kauth_cred_getuid(cp->gss_clnt_cred), |
1919 | nt, | |
1920 | (gssd_byte_buffer)pname, (mach_msg_type_number_t) plen, | |
1921 | cp->gss_clnt_svcnt, | |
1922 | (gssd_byte_buffer)svcname, (mach_msg_type_number_t) cp->gss_clnt_svcnamlen, | |
b0d623f7 | 1923 | GSSD_MUTUAL_FLAG, |
39037602 | 1924 | (gssd_etype_list)etype.etypes, (mach_msg_type_number_t)etype.count, |
39236c6e | 1925 | &cp->gss_clnt_gssd_flags, |
2d21ac55 A |
1926 | &cp->gss_clnt_context, |
1927 | &cp->gss_clnt_cred_handle, | |
b0d623f7 | 1928 | &ret_flags, |
39037602 | 1929 | &octx, (mach_msg_type_number_t *) &lucidlen, |
b0d623f7 | 1930 | &otoken, &otokenlen, |
39236c6e | 1931 | cp->gss_clnt_display ? NULL : display_name, |
2d21ac55 A |
1932 | &cp->gss_clnt_major, |
1933 | &cp->gss_clnt_minor); | |
1934 | ||
39037602 | 1935 | /* Clear the RESTART flag */ |
b0d623f7 | 1936 | cp->gss_clnt_gssd_flags &= ~GSSD_RESTART; |
39037602 A |
1937 | if (cp->gss_clnt_major != GSS_S_CONTINUE_NEEDED) { |
1938 | /* We're done with the gssd handles */ | |
1939 | cp->gss_clnt_context = 0; | |
1940 | cp->gss_clnt_cred_handle = 0; | |
1941 | } | |
39236c6e | 1942 | |
b0d623f7 A |
1943 | if (kr != KERN_SUCCESS) { |
1944 | printf("nfs_gss_clnt_gssd_upcall: mach_gss_init_sec_context failed: %x (%d)\n", kr, kr); | |
2d21ac55 | 1945 | if (kr == MIG_SERVER_DIED && cp->gss_clnt_cred_handle == 0 && |
39236c6e A |
1946 | retry_cnt++ < NFS_GSS_MACH_MAX_RETRIES && |
1947 | !vfs_isforce(nmp->nm_mountp) && (nmp->nm_state & (NFSSTA_FORCE | NFSSTA_DEAD)) == 0) { | |
1948 | if (plen) | |
1949 | nfs_gss_mach_alloc_buffer(principal, plen, &pname); | |
1950 | if (cp->gss_clnt_svcnamlen) | |
1951 | nfs_gss_mach_alloc_buffer(cp->gss_clnt_svcname, cp->gss_clnt_svcnamlen, &svcname); | |
b0d623f7 A |
1952 | if (cp->gss_clnt_tokenlen > 0) |
1953 | nfs_gss_mach_alloc_buffer(cp->gss_clnt_token, cp->gss_clnt_tokenlen, &itoken); | |
2d21ac55 | 1954 | goto retry; |
b0d623f7 | 1955 | } |
316670eb A |
1956 | |
1957 | host_release_special_port(cp->gss_clnt_mport); | |
1958 | cp->gss_clnt_mport = IPC_PORT_NULL; | |
b0d623f7 | 1959 | goto out; |
2d21ac55 A |
1960 | } |
1961 | ||
39236c6e A |
1962 | if (cp->gss_clnt_display == NULL && *display_name != '\0') { |
1963 | int dlen = strnlen(display_name, MAX_DISPLAY_STR) + 1; /* Add extra byte to include '\0' */ | |
39037602 | 1964 | |
39236c6e A |
1965 | if (dlen < MAX_DISPLAY_STR) { |
1966 | MALLOC(cp->gss_clnt_display, char *, dlen, M_TEMP, M_WAITOK); | |
1967 | if (cp->gss_clnt_display == NULL) | |
1968 | goto skip; | |
1969 | bcopy(display_name, cp->gss_clnt_display, dlen); | |
1970 | } else { | |
1971 | goto skip; | |
1972 | } | |
1973 | } | |
1974 | skip: | |
2d21ac55 A |
1975 | /* |
1976 | * Make sure any unusual errors are expanded and logged by gssd | |
39236c6e A |
1977 | * |
1978 | * XXXX, we need to rethink this and just have gssd return a string for the major and minor codes. | |
2d21ac55 A |
1979 | */ |
1980 | if (cp->gss_clnt_major != GSS_S_COMPLETE && | |
1981 | cp->gss_clnt_major != GSS_S_CONTINUE_NEEDED) { | |
39037602 | 1982 | NFS_GSS_DBG("Up call returned error\n"); |
fe8ab488 | 1983 | nfs_gss_clnt_log_error(req, cp, major, minor); |
5ba3f43e A |
1984 | /* Server's handle isn't valid. Don't reuse */ |
1985 | cp->gss_clnt_handle_len = 0; | |
1986 | if (cp->gss_clnt_handle != NULL) { | |
1987 | FREE(cp->gss_clnt_handle, M_TEMP); | |
1988 | cp->gss_clnt_handle = NULL; | |
1989 | } | |
2d21ac55 A |
1990 | } |
1991 | ||
39037602 A |
1992 | if (lucidlen > 0) { |
1993 | if (lucidlen > MAX_LUCIDLEN) { | |
1994 | printf("nfs_gss_clnt_gssd_upcall: bad context length (%d)\n", lucidlen); | |
1995 | vm_map_copy_discard((vm_map_copy_t) octx); | |
b0d623f7 A |
1996 | vm_map_copy_discard((vm_map_copy_t) otoken); |
1997 | goto out; | |
1998 | } | |
39037602 A |
1999 | MALLOC(lucid_ctx_buffer, void *, lucidlen, M_TEMP, M_WAITOK | M_ZERO); |
2000 | error = nfs_gss_mach_vmcopyout((vm_map_copy_t) octx, lucidlen, lucid_ctx_buffer); | |
b0d623f7 A |
2001 | if (error) { |
2002 | vm_map_copy_discard((vm_map_copy_t) otoken); | |
2003 | goto out; | |
2d21ac55 | 2004 | } |
39037602 A |
2005 | |
2006 | if (cp->gss_clnt_ctx_id) | |
2007 | gss_krb5_destroy_context(cp->gss_clnt_ctx_id); | |
2008 | cp->gss_clnt_ctx_id = gss_krb5_make_context(lucid_ctx_buffer, lucidlen); | |
2009 | if (cp->gss_clnt_ctx_id == NULL) { | |
2010 | printf("Failed to make context from lucid_ctx_buffer\n"); | |
b0d623f7 | 2011 | goto out; |
39037602 A |
2012 | } |
2013 | for (uint32_t i = 0; i < nmp->nm_etype.count; i++) { | |
2014 | if (nmp->nm_etype.etypes[i] == cp->gss_clnt_ctx_id->gss_cryptor.etype) { | |
2015 | selected = i; | |
2016 | break; | |
2017 | } | |
2018 | } | |
2d21ac55 A |
2019 | } |
2020 | ||
b0d623f7 A |
2021 | /* Free context token used as input */ |
2022 | if (cp->gss_clnt_token) | |
2023 | FREE(cp->gss_clnt_token, M_TEMP); | |
2024 | cp->gss_clnt_token = NULL; | |
2025 | cp->gss_clnt_tokenlen = 0; | |
2026 | ||
2027 | if (otokenlen > 0) { | |
2028 | /* Set context token to gss output token */ | |
2029 | MALLOC(cp->gss_clnt_token, u_char *, otokenlen, M_TEMP, M_WAITOK); | |
2030 | if (cp->gss_clnt_token == NULL) { | |
2031 | printf("nfs_gss_clnt_gssd_upcall: could not allocate %d bytes\n", otokenlen); | |
2032 | vm_map_copy_discard((vm_map_copy_t) otoken); | |
2d21ac55 | 2033 | return (ENOMEM); |
b0d623f7 A |
2034 | } |
2035 | error = nfs_gss_mach_vmcopyout((vm_map_copy_t) otoken, otokenlen, cp->gss_clnt_token); | |
2036 | if (error) { | |
39037602 | 2037 | printf("Could not copyout gss token\n"); |
b0d623f7 A |
2038 | FREE(cp->gss_clnt_token, M_TEMP); |
2039 | cp->gss_clnt_token = NULL; | |
2040 | return (NFSERR_EAUTH); | |
2041 | } | |
2042 | cp->gss_clnt_tokenlen = otokenlen; | |
2d21ac55 A |
2043 | } |
2044 | ||
39037602 A |
2045 | if (selected != (uint32_t)-1) { |
2046 | nmp->nm_etype.selected = selected; | |
2047 | NFS_GSS_DBG("etype selected = %d\n", nmp->nm_etype.etypes[selected]); | |
2048 | } | |
2049 | NFS_GSS_DBG("Up call succeeded major = %d\n", cp->gss_clnt_major); | |
2d21ac55 | 2050 | return (0); |
b0d623f7 A |
2051 | |
2052 | out: | |
2053 | if (cp->gss_clnt_token) | |
2054 | FREE(cp->gss_clnt_token, M_TEMP); | |
2055 | cp->gss_clnt_token = NULL; | |
2056 | cp->gss_clnt_tokenlen = 0; | |
5ba3f43e A |
2057 | /* Server's handle isn't valid. Don't reuse */ |
2058 | cp->gss_clnt_handle_len = 0; | |
2059 | if (cp->gss_clnt_handle != NULL) { | |
2060 | FREE(cp->gss_clnt_handle, M_TEMP); | |
2061 | cp->gss_clnt_handle = NULL; | |
2062 | } | |
b0d623f7 | 2063 | |
39037602 | 2064 | NFS_GSS_DBG("Up call returned NFSERR_EAUTH"); |
b0d623f7 | 2065 | return (NFSERR_EAUTH); |
2d21ac55 A |
2066 | } |
2067 | ||
2068 | /* | |
2069 | * Invoked at the completion of an RPC call that uses an RPCSEC_GSS | |
2070 | * credential. The sequence number window that the server returns | |
2071 | * at context setup indicates the maximum number of client calls that | |
2072 | * can be outstanding on a context. The client maintains a bitmap that | |
2073 | * represents the server's window. Each pending request has a bit set | |
2074 | * in the window bitmap. When a reply comes in or times out, we reset | |
2075 | * the bit in the bitmap and if there are any other threads waiting for | |
2076 | * a context slot we notify the waiting thread(s). | |
2077 | * | |
2078 | * Note that if a request is retransmitted, it will have a single XID | |
2079 | * but it may be associated with multiple sequence numbers. So we | |
2080 | * may have to reset multiple sequence number bits in the window bitmap. | |
2081 | */ | |
2082 | void | |
2083 | nfs_gss_clnt_rpcdone(struct nfsreq *req) | |
2084 | { | |
2085 | struct nfs_gss_clnt_ctx *cp = req->r_gss_ctx; | |
2086 | struct gss_seq *gsp, *ngsp; | |
2087 | int i = 0; | |
2088 | ||
2089 | if (cp == NULL || !(cp->gss_clnt_flags & GSS_CTX_COMPLETE)) | |
2090 | return; // no context - don't bother | |
2091 | /* | |
2092 | * Reset the bit for this request in the | |
2093 | * sequence number window to indicate it's done. | |
2094 | * We do this even if the request timed out. | |
2095 | */ | |
2096 | lck_mtx_lock(cp->gss_clnt_mtx); | |
2097 | gsp = SLIST_FIRST(&req->r_gss_seqlist); | |
2098 | if (gsp && gsp->gss_seqnum > (cp->gss_clnt_seqnum - cp->gss_clnt_seqwin)) | |
2099 | win_resetbit(cp->gss_clnt_seqbits, | |
2100 | gsp->gss_seqnum % cp->gss_clnt_seqwin); | |
2101 | ||
2102 | /* | |
2103 | * Limit the seqnum list to GSS_CLNT_SEQLISTMAX entries | |
2104 | */ | |
2105 | SLIST_FOREACH_SAFE(gsp, &req->r_gss_seqlist, gss_seqnext, ngsp) { | |
2106 | if (++i > GSS_CLNT_SEQLISTMAX) { | |
2107 | SLIST_REMOVE(&req->r_gss_seqlist, gsp, gss_seq, gss_seqnext); | |
2108 | FREE(gsp, M_TEMP); | |
2109 | } | |
2110 | } | |
2111 | ||
2112 | /* | |
2113 | * If there's a thread waiting for | |
2114 | * the window to advance, wake it up. | |
2115 | */ | |
2116 | if (cp->gss_clnt_flags & GSS_NEEDSEQ) { | |
2117 | cp->gss_clnt_flags &= ~GSS_NEEDSEQ; | |
2118 | wakeup(cp); | |
2119 | } | |
2120 | lck_mtx_unlock(cp->gss_clnt_mtx); | |
2121 | } | |
2122 | ||
2123 | /* | |
2124 | * Create a reference to a context from a request | |
2125 | * and bump the reference count | |
2126 | */ | |
2127 | void | |
2128 | nfs_gss_clnt_ctx_ref(struct nfsreq *req, struct nfs_gss_clnt_ctx *cp) | |
2129 | { | |
2130 | req->r_gss_ctx = cp; | |
2131 | ||
2132 | lck_mtx_lock(cp->gss_clnt_mtx); | |
2133 | cp->gss_clnt_refcnt++; | |
2134 | lck_mtx_unlock(cp->gss_clnt_mtx); | |
2135 | } | |
2136 | ||
2137 | /* | |
2138 | * Remove a context reference from a request | |
2139 | * If the reference count drops to zero, and the | |
2140 | * context is invalid, destroy the context | |
2141 | */ | |
2142 | void | |
2143 | nfs_gss_clnt_ctx_unref(struct nfsreq *req) | |
2144 | { | |
2145 | struct nfsmount *nmp = req->r_nmp; | |
2146 | struct nfs_gss_clnt_ctx *cp = req->r_gss_ctx; | |
fe8ab488 | 2147 | int on_neg_cache = 0; |
3e170ce0 | 2148 | int neg_cache = 0; |
fe8ab488 | 2149 | int destroy = 0; |
3e170ce0 A |
2150 | struct timeval now; |
2151 | char CTXBUF[NFS_CTXBUFSZ]; | |
2d21ac55 A |
2152 | |
2153 | if (cp == NULL) | |
2154 | return; | |
2155 | ||
2156 | req->r_gss_ctx = NULL; | |
2157 | ||
2158 | lck_mtx_lock(cp->gss_clnt_mtx); | |
fe8ab488 A |
2159 | if (--cp->gss_clnt_refcnt < 0) |
2160 | panic("Over release of gss context!\n"); | |
2161 | ||
3e170ce0 A |
2162 | if (cp->gss_clnt_refcnt == 0) { |
2163 | if ((cp->gss_clnt_flags & GSS_CTX_INVAL) && | |
39037602 A |
2164 | cp->gss_clnt_ctx_id) { |
2165 | gss_krb5_destroy_context(cp->gss_clnt_ctx_id); | |
2166 | cp->gss_clnt_ctx_id = NULL; | |
3e170ce0 A |
2167 | } |
2168 | if (cp->gss_clnt_flags & GSS_CTX_DESTROY) { | |
2169 | destroy = 1; | |
2170 | if (cp->gss_clnt_flags & GSS_CTX_STICKY) | |
2171 | nfs_gss_clnt_mnt_rele(nmp); | |
2172 | if (cp->gss_clnt_nctime) | |
2173 | on_neg_cache = 1; | |
2174 | } | |
2175 | } | |
2176 | if (!destroy && cp->gss_clnt_nctime == 0 && | |
2177 | (cp->gss_clnt_flags & GSS_CTX_INVAL)) { | |
2178 | microuptime(&now); | |
2179 | cp->gss_clnt_nctime = now.tv_sec; | |
fe8ab488 A |
2180 | neg_cache = 1; |
2181 | } | |
2182 | lck_mtx_unlock(cp->gss_clnt_mtx); | |
2183 | if (destroy) { | |
3e170ce0 | 2184 | NFS_GSS_DBG("Destroying context %s\n", NFS_GSS_CTX(req, cp)); |
fe8ab488 | 2185 | if (nmp) { |
2d21ac55 | 2186 | lck_mtx_lock(&nmp->nm_lock); |
fe8ab488 | 2187 | if (cp->gss_clnt_entries.tqe_next != NFSNOLIST) { |
3e170ce0 A |
2188 | TAILQ_REMOVE(&nmp->nm_gsscl, cp, gss_clnt_entries); |
2189 | } | |
2190 | if (on_neg_cache) { | |
2191 | nmp->nm_ncentries--; | |
fe8ab488 | 2192 | } |
2d21ac55 | 2193 | lck_mtx_unlock(&nmp->nm_lock); |
fe8ab488 A |
2194 | } |
2195 | nfs_gss_clnt_ctx_destroy(cp); | |
3e170ce0 A |
2196 | } else if (neg_cache) { |
2197 | NFS_GSS_DBG("Entering context %s into negative cache\n", NFS_GSS_CTX(req, cp)); | |
2198 | if (nmp) { | |
2199 | lck_mtx_lock(&nmp->nm_lock); | |
2200 | nmp->nm_ncentries++; | |
2201 | nfs_gss_clnt_ctx_neg_cache_reap(nmp); | |
2202 | lck_mtx_unlock(&nmp->nm_lock); | |
2203 | } | |
2204 | } | |
fe8ab488 A |
2205 | NFS_GSS_CLNT_CTX_DUMP(nmp); |
2206 | } | |
2207 | ||
2208 | /* | |
3e170ce0 | 2209 | * Try and reap any old negative cache entries. |
fe8ab488 A |
2210 | * cache queue. |
2211 | */ | |
2212 | void | |
3e170ce0 | 2213 | nfs_gss_clnt_ctx_neg_cache_reap(struct nfsmount *nmp) |
fe8ab488 | 2214 | { |
3e170ce0 | 2215 | struct nfs_gss_clnt_ctx *cp, *tcp; |
fe8ab488 A |
2216 | struct timeval now; |
2217 | int reaped = 0; | |
2d21ac55 | 2218 | |
fe8ab488 | 2219 | /* Try and reap old, unreferenced, expired contexts */ |
39037602 A |
2220 | microuptime(&now); |
2221 | ||
2222 | NFS_GSS_DBG("Reaping contexts ncentries = %d\n", nmp->nm_ncentries); | |
3e170ce0 A |
2223 | |
2224 | TAILQ_FOREACH_SAFE(cp, &nmp->nm_gsscl, gss_clnt_entries, tcp) { | |
fe8ab488 A |
2225 | int destroy = 0; |
2226 | ||
3e170ce0 A |
2227 | /* Don't reap STICKY contexts */ |
2228 | if ((cp->gss_clnt_flags & GSS_CTX_STICKY) || | |
2229 | !(cp->gss_clnt_flags & GSS_CTX_INVAL)) | |
2230 | continue; | |
fe8ab488 A |
2231 | /* Keep up to GSS_MAX_NEG_CACHE_ENTRIES */ |
2232 | if (nmp->nm_ncentries <= GSS_MAX_NEG_CACHE_ENTRIES) | |
2233 | break; | |
3e170ce0 A |
2234 | /* Contexts too young */ |
2235 | if (cp->gss_clnt_nctime + GSS_NEG_CACHE_TO >= now.tv_sec) | |
2236 | continue; | |
fe8ab488 | 2237 | /* Not referenced, remove it. */ |
3e170ce0 A |
2238 | lck_mtx_lock(cp->gss_clnt_mtx); |
2239 | if (cp->gss_clnt_refcnt == 0) { | |
2240 | cp->gss_clnt_flags |= GSS_CTX_DESTROY; | |
fe8ab488 A |
2241 | destroy = 1; |
2242 | } | |
3e170ce0 A |
2243 | lck_mtx_unlock(cp->gss_clnt_mtx); |
2244 | if (destroy) { | |
2245 | TAILQ_REMOVE(&nmp->nm_gsscl, cp, gss_clnt_entries); | |
2246 | nmp->nm_ncentries++; | |
2247 | reaped++; | |
2248 | nfs_gss_clnt_ctx_destroy(cp); | |
2249 | } | |
fe8ab488 A |
2250 | } |
2251 | NFS_GSS_DBG("Reaped %d contexts ncentries = %d\n", reaped, nmp->nm_ncentries); | |
fe8ab488 A |
2252 | } |
2253 | ||
2254 | /* | |
2255 | * Clean a context to be cached | |
2256 | */ | |
2257 | static void | |
2258 | nfs_gss_clnt_ctx_clean(struct nfs_gss_clnt_ctx *cp) | |
2259 | { | |
3e170ce0 A |
2260 | /* Preserve gss_clnt_mtx */ |
2261 | assert(cp->gss_clnt_thread == NULL); /* Will be set to this thread */ | |
2262 | /* gss_clnt_entries we should not be on any list at this point */ | |
fe8ab488 | 2263 | cp->gss_clnt_flags = 0; |
3e170ce0 A |
2264 | /* gss_clnt_refcnt should be zero */ |
2265 | assert(cp->gss_clnt_refcnt == 0); | |
2266 | /* | |
2267 | * We are who we are preserve: | |
2268 | * gss_clnt_cred | |
2269 | * gss_clnt_principal | |
2270 | * gss_clnt_prinlen | |
2271 | * gss_clnt_prinnt | |
2272 | * gss_clnt_desplay | |
2273 | */ | |
2274 | /* gss_clnt_proc will be set in nfs_gss_clnt_ctx_init */ | |
2275 | cp->gss_clnt_seqnum = 0; | |
2276 | /* Preserve gss_clnt_service, we're not changing flavors */ | |
fe8ab488 A |
2277 | if (cp->gss_clnt_handle) { |
2278 | FREE(cp->gss_clnt_handle, M_TEMP); | |
2279 | cp->gss_clnt_handle = NULL; | |
2280 | } | |
3e170ce0 A |
2281 | cp->gss_clnt_handle_len = 0; |
2282 | cp->gss_clnt_nctime = 0; | |
2283 | cp->gss_clnt_seqwin = 0; | |
fe8ab488 A |
2284 | if (cp->gss_clnt_seqbits) { |
2285 | FREE(cp->gss_clnt_seqbits, M_TEMP); | |
2286 | cp->gss_clnt_seqbits = NULL; | |
2287 | } | |
3e170ce0 A |
2288 | /* Preserve gss_clnt_mport. Still talking to the same gssd */ |
2289 | if (cp->gss_clnt_verf) { | |
2290 | FREE(cp->gss_clnt_verf, M_TEMP); | |
2291 | cp->gss_clnt_verf = NULL; | |
fe8ab488 | 2292 | } |
3e170ce0 | 2293 | /* Service name might change on failover, so reset it */ |
fe8ab488 A |
2294 | if (cp->gss_clnt_svcname) { |
2295 | FREE(cp->gss_clnt_svcname, M_TEMP); | |
2296 | cp->gss_clnt_svcname = NULL; | |
3e170ce0 | 2297 | cp->gss_clnt_svcnt = 0; |
fe8ab488 | 2298 | } |
3e170ce0 A |
2299 | cp->gss_clnt_svcnamlen = 0; |
2300 | cp->gss_clnt_cred_handle = 0; | |
2301 | cp->gss_clnt_context = 0; | |
2302 | if (cp->gss_clnt_token) { | |
2303 | FREE(cp->gss_clnt_token, M_TEMP); | |
2304 | cp->gss_clnt_token = NULL; | |
2305 | } | |
2306 | cp->gss_clnt_tokenlen = 0; | |
39037602 | 2307 | /* XXX gss_clnt_ctx_id ??? */ |
3e170ce0 A |
2308 | /* |
2309 | * Preserve: | |
2310 | * gss_clnt_gssd_flags | |
2311 | * gss_clnt_major | |
2312 | * gss_clnt_minor | |
2313 | * gss_clnt_ptime | |
2314 | */ | |
2315 | } | |
2316 | ||
2317 | /* | |
2318 | * Copy a source context to a new context. This is used to create a new context | |
2319 | * with the identity of the old context for renewal. The old context is invalid | |
2320 | * at this point but may have reference still to it, so it is not safe to use that | |
2321 | * context. | |
2322 | */ | |
2323 | static int | |
39037602 | 2324 | nfs_gss_clnt_ctx_copy(struct nfs_gss_clnt_ctx *scp, struct nfs_gss_clnt_ctx **dcpp) |
3e170ce0 A |
2325 | { |
2326 | struct nfs_gss_clnt_ctx *dcp; | |
2327 | ||
2328 | *dcpp = (struct nfs_gss_clnt_ctx *)NULL; | |
2329 | MALLOC(dcp, struct nfs_gss_clnt_ctx *, sizeof (struct nfs_gss_clnt_ctx), M_TEMP, M_WAITOK); | |
2330 | if (dcp == NULL) | |
2331 | return (ENOMEM); | |
2332 | bzero(dcp, sizeof (struct nfs_gss_clnt_ctx)); | |
3e170ce0 A |
2333 | dcp->gss_clnt_mtx = lck_mtx_alloc_init(nfs_gss_clnt_grp, LCK_ATTR_NULL); |
2334 | dcp->gss_clnt_cred = scp->gss_clnt_cred; | |
2335 | kauth_cred_ref(dcp->gss_clnt_cred); | |
2336 | dcp->gss_clnt_prinlen = scp->gss_clnt_prinlen; | |
2337 | dcp->gss_clnt_prinnt = scp->gss_clnt_prinnt; | |
2338 | if (scp->gss_clnt_principal) { | |
2339 | MALLOC(dcp->gss_clnt_principal, uint8_t *, dcp->gss_clnt_prinlen, M_TEMP, M_WAITOK | M_ZERO); | |
2340 | if (dcp->gss_clnt_principal == NULL) { | |
3e170ce0 A |
2341 | FREE(dcp, M_TEMP); |
2342 | return (ENOMEM); | |
2343 | } | |
2344 | bcopy(scp->gss_clnt_principal, dcp->gss_clnt_principal, dcp->gss_clnt_prinlen); | |
2345 | } | |
2346 | /* Note we don't preserve the display name, that will be set by a successful up call */ | |
2347 | dcp->gss_clnt_service = scp->gss_clnt_service; | |
2348 | dcp->gss_clnt_mport = host_copy_special_port(scp->gss_clnt_mport); | |
39037602 | 2349 | dcp->gss_clnt_ctx_id = NULL; /* Will be set from successful upcall */ |
3e170ce0 A |
2350 | dcp->gss_clnt_gssd_flags = scp->gss_clnt_gssd_flags; |
2351 | dcp->gss_clnt_major = scp->gss_clnt_major; | |
2352 | dcp->gss_clnt_minor = scp->gss_clnt_minor; | |
2353 | dcp->gss_clnt_ptime = scp->gss_clnt_ptime; | |
2354 | ||
2355 | *dcpp = dcp; | |
2356 | ||
2357 | return (0); | |
2d21ac55 A |
2358 | } |
2359 | ||
2360 | /* | |
2361 | * Remove a context | |
2362 | */ | |
2363 | static void | |
fe8ab488 | 2364 | nfs_gss_clnt_ctx_destroy(struct nfs_gss_clnt_ctx *cp) |
2d21ac55 | 2365 | { |
fe8ab488 A |
2366 | NFS_GSS_DBG("Destroying context %d/%d\n", |
2367 | kauth_cred_getasid(cp->gss_clnt_cred), | |
2368 | kauth_cred_getauid(cp->gss_clnt_cred)); | |
2d21ac55 | 2369 | |
316670eb | 2370 | host_release_special_port(cp->gss_clnt_mport); |
fe8ab488 | 2371 | cp->gss_clnt_mport = IPC_PORT_NULL; |
3e170ce0 | 2372 | |
fe8ab488 | 2373 | if (cp->gss_clnt_mtx) { |
2d21ac55 | 2374 | lck_mtx_destroy(cp->gss_clnt_mtx, nfs_gss_clnt_grp); |
fe8ab488 A |
2375 | cp->gss_clnt_mtx = (lck_mtx_t *)NULL; |
2376 | } | |
39236c6e A |
2377 | if (IS_VALID_CRED(cp->gss_clnt_cred)) |
2378 | kauth_cred_unref(&cp->gss_clnt_cred); | |
fe8ab488 A |
2379 | cp->gss_clnt_entries.tqe_next = NFSNOLIST; |
2380 | cp->gss_clnt_entries.tqe_prev = NFSNOLIST; | |
2381 | if (cp->gss_clnt_principal) { | |
39236c6e | 2382 | FREE(cp->gss_clnt_principal, M_TEMP); |
fe8ab488 A |
2383 | cp->gss_clnt_principal = NULL; |
2384 | } | |
2385 | if (cp->gss_clnt_display) { | |
39236c6e | 2386 | FREE(cp->gss_clnt_display, M_TEMP); |
fe8ab488 A |
2387 | cp->gss_clnt_display = NULL; |
2388 | } | |
39037602 A |
2389 | if (cp->gss_clnt_ctx_id) { |
2390 | gss_krb5_destroy_context(cp->gss_clnt_ctx_id); | |
2391 | cp->gss_clnt_ctx_id = NULL; | |
3e170ce0 A |
2392 | } |
2393 | ||
fe8ab488 | 2394 | nfs_gss_clnt_ctx_clean(cp); |
3e170ce0 | 2395 | |
2d21ac55 A |
2396 | FREE(cp, M_TEMP); |
2397 | } | |
2398 | ||
2399 | /* | |
2400 | * The context for a user is invalid. | |
2401 | * Mark the context as invalid, then | |
2402 | * create a new context. | |
2403 | */ | |
2404 | int | |
2405 | nfs_gss_clnt_ctx_renew(struct nfsreq *req) | |
2406 | { | |
2407 | struct nfs_gss_clnt_ctx *cp = req->r_gss_ctx; | |
2d21ac55 | 2408 | struct nfs_gss_clnt_ctx *ncp; |
3e170ce0 | 2409 | struct nfsmount *nmp; |
2d21ac55 | 2410 | int error = 0; |
3e170ce0 | 2411 | char CTXBUF[NFS_CTXBUFSZ]; |
2d21ac55 | 2412 | |
b0d623f7 | 2413 | if (cp == NULL) |
2d21ac55 A |
2414 | return (0); |
2415 | ||
3e170ce0 A |
2416 | if (req->r_nmp == NULL) |
2417 | return (ENXIO); | |
2418 | nmp = req->r_nmp; | |
2419 | ||
2d21ac55 A |
2420 | lck_mtx_lock(cp->gss_clnt_mtx); |
2421 | if (cp->gss_clnt_flags & GSS_CTX_INVAL) { | |
2422 | lck_mtx_unlock(cp->gss_clnt_mtx); | |
2423 | nfs_gss_clnt_ctx_unref(req); | |
2424 | return (0); // already being renewed | |
2425 | } | |
2d21ac55 | 2426 | |
fe8ab488 | 2427 | cp->gss_clnt_flags |= (GSS_CTX_INVAL | GSS_CTX_DESTROY); |
2d21ac55 | 2428 | |
2d21ac55 A |
2429 | if (cp->gss_clnt_flags & (GSS_NEEDCTX | GSS_NEEDSEQ)) { |
2430 | cp->gss_clnt_flags &= ~GSS_NEEDSEQ; | |
2431 | wakeup(cp); | |
2432 | } | |
2433 | lck_mtx_unlock(cp->gss_clnt_mtx); | |
2434 | ||
39037602 A |
2435 | if (cp->gss_clnt_proc == RPCSEC_GSS_DESTROY) |
2436 | return (EACCES); /* Destroying a context is best effort. Don't renew. */ | |
2437 | /* | |
2438 | * If we're setting up a context let nfs_gss_clnt_ctx_init know this is not working | |
2439 | * and to try some other etype. | |
2440 | */ | |
2441 | if (cp->gss_clnt_proc != RPCSEC_GSS_DATA) | |
2442 | return (ENEEDAUTH); | |
2443 | error = nfs_gss_clnt_ctx_copy(cp, &ncp); | |
3e170ce0 A |
2444 | NFS_GSS_DBG("Renewing context %s\n", NFS_GSS_CTX(req, ncp)); |
2445 | nfs_gss_clnt_ctx_unref(req); | |
2446 | if (error) | |
2447 | return (error); | |
2448 | ||
2449 | lck_mtx_lock(&nmp->nm_lock); | |
2d21ac55 | 2450 | /* |
3e170ce0 A |
2451 | * Note we don't bother taking the new context mutex as we're |
2452 | * not findable at the moment. | |
2d21ac55 | 2453 | */ |
2d21ac55 | 2454 | ncp->gss_clnt_thread = current_thread(); |
2d21ac55 | 2455 | nfs_gss_clnt_ctx_ref(req, ncp); |
3e170ce0 A |
2456 | TAILQ_INSERT_HEAD(&nmp->nm_gsscl, ncp, gss_clnt_entries); |
2457 | lck_mtx_unlock(&nmp->nm_lock); | |
2d21ac55 | 2458 | |
3e170ce0 | 2459 | error = nfs_gss_clnt_ctx_init_retry(req, ncp); // Initialize new context |
2d21ac55 A |
2460 | if (error) |
2461 | nfs_gss_clnt_ctx_unref(req); | |
3e170ce0 | 2462 | |
2d21ac55 A |
2463 | return (error); |
2464 | } | |
2465 | ||
fe8ab488 | 2466 | |
2d21ac55 A |
2467 | /* |
2468 | * Destroy all the contexts associated with a mount. | |
2469 | * The contexts are also destroyed by the server. | |
2470 | */ | |
2471 | void | |
6d2010ae | 2472 | nfs_gss_clnt_ctx_unmount(struct nfsmount *nmp) |
2d21ac55 A |
2473 | { |
2474 | struct nfs_gss_clnt_ctx *cp; | |
2d21ac55 | 2475 | struct nfsm_chain nmreq, nmrep; |
2d21ac55 A |
2476 | int error, status; |
2477 | struct nfsreq req; | |
2d21ac55 A |
2478 | req.r_nmp = nmp; |
2479 | ||
fe8ab488 A |
2480 | if (!nmp) |
2481 | return; | |
2482 | ||
3e170ce0 A |
2483 | |
2484 | lck_mtx_lock(&nmp->nm_lock); | |
2485 | while((cp = TAILQ_FIRST(&nmp->nm_gsscl))) { | |
2486 | TAILQ_REMOVE(&nmp->nm_gsscl, cp, gss_clnt_entries); | |
2487 | cp->gss_clnt_entries.tqe_next = NFSNOLIST; | |
fe8ab488 | 2488 | lck_mtx_lock(cp->gss_clnt_mtx); |
3e170ce0 A |
2489 | if (cp->gss_clnt_flags & GSS_CTX_DESTROY) { |
2490 | lck_mtx_unlock(cp->gss_clnt_mtx); | |
2491 | continue; | |
2492 | } | |
fe8ab488 A |
2493 | cp->gss_clnt_refcnt++; |
2494 | lck_mtx_unlock(cp->gss_clnt_mtx); | |
2495 | req.r_gss_ctx = cp; | |
2496 | ||
2d21ac55 | 2497 | lck_mtx_unlock(&nmp->nm_lock); |
2d21ac55 A |
2498 | /* |
2499 | * Tell the server to destroy its context. | |
fe8ab488 | 2500 | * But don't bother if it's a forced unmount. |
2d21ac55 | 2501 | */ |
3e170ce0 A |
2502 | if (!nfs_mount_gone(nmp) && |
2503 | (cp->gss_clnt_flags & (GSS_CTX_INVAL | GSS_CTX_DESTROY | GSS_CTX_COMPLETE)) == GSS_CTX_COMPLETE) { | |
2d21ac55 A |
2504 | cp->gss_clnt_proc = RPCSEC_GSS_DESTROY; |
2505 | ||
2506 | error = 0; | |
2507 | nfsm_chain_null(&nmreq); | |
2508 | nfsm_chain_null(&nmrep); | |
2509 | nfsm_chain_build_alloc_init(error, &nmreq, 0); | |
2510 | nfsm_chain_build_done(error, &nmreq); | |
2511 | if (!error) | |
b0d623f7 | 2512 | nfs_request_gss(nmp->nm_mountp, &nmreq, |
39236c6e | 2513 | current_thread(), cp->gss_clnt_cred, 0, cp, &nmrep, &status); |
2d21ac55 A |
2514 | nfsm_chain_cleanup(&nmreq); |
2515 | nfsm_chain_cleanup(&nmrep); | |
2d21ac55 A |
2516 | } |
2517 | ||
2518 | /* | |
2519 | * Mark the context invalid then drop | |
2520 | * the reference to remove it if its | |
2521 | * refcount is zero. | |
2522 | */ | |
6d2010ae | 2523 | lck_mtx_lock(cp->gss_clnt_mtx); |
fe8ab488 | 2524 | cp->gss_clnt_flags |= (GSS_CTX_INVAL | GSS_CTX_DESTROY); |
6d2010ae | 2525 | lck_mtx_unlock(cp->gss_clnt_mtx); |
2d21ac55 | 2526 | nfs_gss_clnt_ctx_unref(&req); |
fe8ab488 | 2527 | lck_mtx_lock(&nmp->nm_lock); |
fe8ab488 | 2528 | } |
3e170ce0 A |
2529 | lck_mtx_unlock(&nmp->nm_lock); |
2530 | assert(TAILQ_EMPTY(&nmp->nm_gsscl)); | |
2d21ac55 A |
2531 | } |
2532 | ||
3e170ce0 | 2533 | |
39236c6e | 2534 | /* |
fe8ab488 | 2535 | * Removes a mounts context for a credential |
39236c6e A |
2536 | */ |
2537 | int | |
fe8ab488 | 2538 | nfs_gss_clnt_ctx_remove(struct nfsmount *nmp, kauth_cred_t cred) |
39236c6e A |
2539 | { |
2540 | struct nfs_gss_clnt_ctx *cp; | |
2541 | struct nfsreq req; | |
2542 | ||
2543 | req.r_nmp = nmp; | |
2544 | ||
fe8ab488 A |
2545 | NFS_GSS_DBG("Enter\n"); |
2546 | NFS_GSS_CLNT_CTX_DUMP(nmp); | |
39236c6e A |
2547 | lck_mtx_lock(&nmp->nm_lock); |
2548 | TAILQ_FOREACH(cp, &nmp->nm_gsscl, gss_clnt_entries) { | |
fe8ab488 | 2549 | lck_mtx_lock(cp->gss_clnt_mtx); |
39236c6e | 2550 | if (nfs_gss_clnt_ctx_cred_match(cp->gss_clnt_cred, cred)) { |
fe8ab488 A |
2551 | if (cp->gss_clnt_flags & GSS_CTX_DESTROY) { |
2552 | NFS_GSS_DBG("Found destroyed context %d/%d. refcnt = %d continuing\n", | |
2553 | kauth_cred_getasid(cp->gss_clnt_cred), | |
2554 | kauth_cred_getauid(cp->gss_clnt_cred), | |
2555 | cp->gss_clnt_refcnt); | |
2556 | lck_mtx_unlock(cp->gss_clnt_mtx); | |
39236c6e | 2557 | continue; |
fe8ab488 | 2558 | } |
39236c6e | 2559 | cp->gss_clnt_refcnt++; |
fe8ab488 | 2560 | cp->gss_clnt_flags |= (GSS_CTX_INVAL | GSS_CTX_DESTROY); |
39236c6e A |
2561 | lck_mtx_unlock(cp->gss_clnt_mtx); |
2562 | req.r_gss_ctx = cp; | |
fe8ab488 A |
2563 | lck_mtx_unlock(&nmp->nm_lock); |
2564 | /* | |
2565 | * Drop the reference to remove it if its | |
2566 | * refcount is zero. | |
2567 | */ | |
2568 | NFS_GSS_DBG("Removed context %d/%d refcnt = %d\n", | |
2569 | kauth_cred_getasid(cp->gss_clnt_cred), | |
2570 | kauth_cred_getuid(cp->gss_clnt_cred), | |
2571 | cp->gss_clnt_refcnt); | |
2572 | nfs_gss_clnt_ctx_unref(&req); | |
2573 | return (0); | |
39236c6e | 2574 | } |
fe8ab488 | 2575 | lck_mtx_unlock(cp->gss_clnt_mtx); |
39236c6e | 2576 | } |
39236c6e | 2577 | |
3e170ce0 A |
2578 | lck_mtx_unlock(&nmp->nm_lock); |
2579 | ||
2580 | NFS_GSS_DBG("Returning ENOENT\n"); | |
2581 | return (ENOENT); | |
2582 | } | |
2583 | ||
2584 | /* | |
2585 | * Sets a mounts principal for a session associated with cred. | |
2586 | */ | |
2587 | int | |
2588 | nfs_gss_clnt_ctx_set_principal(struct nfsmount *nmp, vfs_context_t ctx, | |
2589 | uint8_t *principal, uint32_t princlen, uint32_t nametype) | |
2590 | ||
2591 | { | |
2592 | struct nfsreq req; | |
2593 | int error; | |
2594 | ||
2595 | NFS_GSS_DBG("Enter:\n"); | |
2596 | ||
2597 | bzero(&req, sizeof(struct nfsreq)); | |
2598 | req.r_nmp = nmp; | |
2599 | req.r_gss_ctx = NULL; | |
2600 | req.r_auth = nmp->nm_auth; | |
2601 | req.r_thread = vfs_context_thread(ctx); | |
2602 | req.r_cred = vfs_context_ucred(ctx); | |
2603 | ||
2604 | error = nfs_gss_clnt_ctx_find_principal(&req, principal, princlen, nametype); | |
2605 | NFS_GSS_DBG("nfs_gss_clnt_ctx_find_principal returned %d\n", error); | |
2606 | /* | |
2607 | * We don't care about auth errors. Those would indicate that the context is in the | |
2608 | * neagative cache and if and when the user has credentials for the principal | |
2609 | * we should be good to go in that we will select those credentials for this principal. | |
2610 | */ | |
2611 | if (error == EACCES || error == EAUTH || error == ENEEDAUTH) | |
2612 | error = 0; | |
2613 | ||
2614 | /* We're done with this request */ | |
2615 | nfs_gss_clnt_ctx_unref(&req); | |
2616 | ||
2617 | return (error); | |
2618 | } | |
2619 | ||
2620 | /* | |
2621 | * Gets a mounts principal from a session associated with cred | |
2622 | */ | |
2623 | int | |
2624 | nfs_gss_clnt_ctx_get_principal(struct nfsmount *nmp, vfs_context_t ctx, | |
2625 | struct user_nfs_gss_principal *p) | |
2626 | { | |
2627 | struct nfsreq req; | |
2628 | int error = 0; | |
2629 | struct nfs_gss_clnt_ctx *cp; | |
2630 | kauth_cred_t cred = vfs_context_ucred(ctx); | |
39037602 | 2631 | const char *princ = NULL; |
3e170ce0 A |
2632 | char CTXBUF[NFS_CTXBUFSZ]; |
2633 | ||
39037602 A |
2634 | /* Make sure the the members of the struct user_nfs_gss_principal are initialized */ |
2635 | p->nametype = GSSD_STRING_NAME; | |
2636 | p->principal = USER_ADDR_NULL; | |
2637 | p->princlen = 0; | |
2638 | p->flags = 0; | |
2639 | ||
3e170ce0 A |
2640 | req.r_nmp = nmp; |
2641 | lck_mtx_lock(&nmp->nm_lock); | |
2642 | TAILQ_FOREACH(cp, &nmp->nm_gsscl, gss_clnt_entries) { | |
fe8ab488 | 2643 | lck_mtx_lock(cp->gss_clnt_mtx); |
3e170ce0 A |
2644 | if (cp->gss_clnt_flags & GSS_CTX_DESTROY) { |
2645 | NFS_GSS_DBG("Found destroyed context %s refcnt = %d continuing\n", | |
2646 | NFS_GSS_CTX(&req, cp), | |
2647 | cp->gss_clnt_refcnt); | |
2648 | lck_mtx_unlock(cp->gss_clnt_mtx); | |
2649 | continue; | |
2650 | } | |
fe8ab488 | 2651 | if (nfs_gss_clnt_ctx_cred_match(cp->gss_clnt_cred, cred)) { |
fe8ab488 | 2652 | cp->gss_clnt_refcnt++; |
fe8ab488 | 2653 | lck_mtx_unlock(cp->gss_clnt_mtx); |
3e170ce0 | 2654 | goto out; |
fe8ab488 A |
2655 | } |
2656 | lck_mtx_unlock(cp->gss_clnt_mtx); | |
2657 | } | |
39236c6e | 2658 | |
3e170ce0 A |
2659 | out: |
2660 | if (cp == NULL) { | |
2661 | lck_mtx_unlock(&nmp->nm_lock); | |
39037602 | 2662 | p->flags |= NFS_IOC_NO_CRED_FLAG; /* No credentials, valid or invalid on this mount */ |
3e170ce0 A |
2663 | NFS_GSS_DBG("No context found for session %d by uid %d\n", |
2664 | kauth_cred_getasid(cred), kauth_cred_getuid(cred)); | |
2665 | return (0); | |
2666 | } | |
2667 | ||
39037602 A |
2668 | /* Indicate if the cred is INVALID */ |
2669 | if (cp->gss_clnt_flags & GSS_CTX_INVAL) | |
2670 | p->flags |= NFS_IOC_INVALID_CRED_FLAG; | |
2671 | ||
2672 | /* We have set a principal on the mount */ | |
2673 | if (cp->gss_clnt_principal) { | |
2674 | princ = (char *)cp->gss_clnt_principal; | |
2675 | p->princlen = cp->gss_clnt_prinlen; | |
2676 | p->nametype = cp->gss_clnt_prinnt; | |
2677 | } else if (cp->gss_clnt_display) { | |
2678 | /* We have a successful use the the default credential */ | |
2679 | princ = cp->gss_clnt_display; | |
2680 | p->princlen = strlen(cp->gss_clnt_display); | |
2681 | } | |
2682 | ||
2683 | /* | |
2684 | * If neither of the above is true we have an invalid default credential | |
2685 | * So from above p->principal is USER_ADDR_NULL and princ is NULL | |
2686 | */ | |
2687 | ||
3e170ce0 A |
2688 | if (princ) { |
2689 | char *pp; | |
39236c6e | 2690 | |
3e170ce0 | 2691 | MALLOC(pp, char *, p->princlen, M_TEMP, M_WAITOK); |
39037602 A |
2692 | bcopy(princ, pp, p->princlen); |
2693 | p->principal = CAST_USER_ADDR_T(pp); | |
3e170ce0 | 2694 | } |
39037602 | 2695 | |
3e170ce0 | 2696 | lck_mtx_unlock(&nmp->nm_lock); |
39236c6e | 2697 | |
3e170ce0 A |
2698 | req.r_gss_ctx = cp; |
2699 | NFS_GSS_DBG("Found context %s\n", NFS_GSS_CTX(&req, NULL)); | |
2700 | nfs_gss_clnt_ctx_unref(&req); | |
2701 | return (error); | |
2702 | } | |
2d21ac55 A |
2703 | #endif /* NFSCLIENT */ |
2704 | ||
2705 | /************* | |
2706 | * | |
2707 | * Server functions | |
2708 | */ | |
2709 | ||
2710 | #if NFSSERVER | |
2711 | ||
2712 | /* | |
2713 | * Find a server context based on a handle value received | |
2714 | * in an RPCSEC_GSS credential. | |
2715 | */ | |
2716 | static struct nfs_gss_svc_ctx * | |
2717 | nfs_gss_svc_ctx_find(uint32_t handle) | |
2718 | { | |
2719 | struct nfs_gss_svc_ctx_hashhead *head; | |
2720 | struct nfs_gss_svc_ctx *cp; | |
b0d623f7 A |
2721 | uint64_t timenow; |
2722 | ||
2723 | if (handle == 0) | |
2724 | return (NULL); | |
2725 | ||
2d21ac55 | 2726 | head = &nfs_gss_svc_ctx_hashtbl[SVC_CTX_HASH(handle)]; |
b0d623f7 A |
2727 | /* |
2728 | * Don't return a context that is going to expire in GSS_CTX_PEND seconds | |
2729 | */ | |
2730 | clock_interval_to_deadline(GSS_CTX_PEND, NSEC_PER_SEC, &timenow); | |
2d21ac55 A |
2731 | |
2732 | lck_mtx_lock(nfs_gss_svc_ctx_mutex); | |
b0d623f7 | 2733 | |
6d2010ae | 2734 | LIST_FOREACH(cp, head, gss_svc_entries) { |
b0d623f7 A |
2735 | if (cp->gss_svc_handle == handle) { |
2736 | if (timenow > cp->gss_svc_incarnation + GSS_SVC_CTX_TTL) { | |
2737 | /* | |
2738 | * Context has or is about to expire. Don't use. | |
2739 | * We'll return null and the client will have to create | |
2740 | * a new context. | |
2741 | */ | |
2742 | cp->gss_svc_handle = 0; | |
2743 | /* | |
6d2010ae | 2744 | * Make sure though that we stay around for GSS_CTX_PEND seconds |
b0d623f7 A |
2745 | * for other threads that might be using the context. |
2746 | */ | |
2747 | cp->gss_svc_incarnation = timenow; | |
6d2010ae | 2748 | |
b0d623f7 | 2749 | cp = NULL; |
6d2010ae | 2750 | break; |
b0d623f7 | 2751 | } |
6d2010ae A |
2752 | lck_mtx_lock(cp->gss_svc_mtx); |
2753 | cp->gss_svc_refcnt++; | |
2754 | lck_mtx_unlock(cp->gss_svc_mtx); | |
2d21ac55 | 2755 | break; |
b0d623f7 | 2756 | } |
6d2010ae | 2757 | } |
b0d623f7 | 2758 | |
2d21ac55 A |
2759 | lck_mtx_unlock(nfs_gss_svc_ctx_mutex); |
2760 | ||
2761 | return (cp); | |
2762 | } | |
2763 | ||
2764 | /* | |
2765 | * Insert a new server context into the hash table | |
2766 | * and start the context reap thread if necessary. | |
2767 | */ | |
2768 | static void | |
2769 | nfs_gss_svc_ctx_insert(struct nfs_gss_svc_ctx *cp) | |
2770 | { | |
2771 | struct nfs_gss_svc_ctx_hashhead *head; | |
6d2010ae | 2772 | struct nfs_gss_svc_ctx *p; |
2d21ac55 | 2773 | |
6d2010ae A |
2774 | lck_mtx_lock(nfs_gss_svc_ctx_mutex); |
2775 | ||
2776 | /* | |
2777 | * Give the client a random handle so that if we reboot | |
2778 | * it's unlikely the client will get a bad context match. | |
2779 | * Make sure it's not zero or already assigned. | |
2780 | */ | |
2781 | retry: | |
2782 | cp->gss_svc_handle = random(); | |
2783 | if (cp->gss_svc_handle == 0) | |
2784 | goto retry; | |
2d21ac55 | 2785 | head = &nfs_gss_svc_ctx_hashtbl[SVC_CTX_HASH(cp->gss_svc_handle)]; |
6d2010ae A |
2786 | LIST_FOREACH(p, head, gss_svc_entries) |
2787 | if (p->gss_svc_handle == cp->gss_svc_handle) | |
2788 | goto retry; | |
2d21ac55 | 2789 | |
6d2010ae A |
2790 | clock_interval_to_deadline(GSS_CTX_PEND, NSEC_PER_SEC, |
2791 | &cp->gss_svc_incarnation); | |
2d21ac55 A |
2792 | LIST_INSERT_HEAD(head, cp, gss_svc_entries); |
2793 | nfs_gss_ctx_count++; | |
2794 | ||
2795 | if (!nfs_gss_timer_on) { | |
2796 | nfs_gss_timer_on = 1; | |
b0d623f7 | 2797 | |
2d21ac55 | 2798 | nfs_interval_timer_start(nfs_gss_svc_ctx_timer_call, |
6d2010ae | 2799 | min(GSS_TIMER_PERIOD, max(GSS_CTX_TTL_MIN, nfsrv_gss_context_ttl)) * MSECS_PER_SEC); |
2d21ac55 | 2800 | } |
b0d623f7 | 2801 | |
2d21ac55 A |
2802 | lck_mtx_unlock(nfs_gss_svc_ctx_mutex); |
2803 | } | |
2804 | ||
2805 | /* | |
2806 | * This function is called via the kernel's callout | |
2807 | * mechanism. It runs only when there are | |
2808 | * cached RPCSEC_GSS contexts. | |
2809 | */ | |
2810 | void | |
2811 | nfs_gss_svc_ctx_timer(__unused void *param1, __unused void *param2) | |
2812 | { | |
2d21ac55 A |
2813 | struct nfs_gss_svc_ctx *cp, *next; |
2814 | uint64_t timenow; | |
2815 | int contexts = 0; | |
2816 | int i; | |
2817 | ||
2818 | lck_mtx_lock(nfs_gss_svc_ctx_mutex); | |
2819 | clock_get_uptime(&timenow); | |
2820 | ||
fe8ab488 A |
2821 | NFS_GSS_DBG("is running\n"); |
2822 | ||
2d21ac55 A |
2823 | /* |
2824 | * Scan all the hash chains | |
2d21ac55 A |
2825 | */ |
2826 | for (i = 0; i < SVC_CTX_HASHSZ; i++) { | |
2827 | /* | |
2828 | * For each hash chain, look for entries | |
2829 | * that haven't been used in a while. | |
2830 | */ | |
6d2010ae | 2831 | LIST_FOREACH_SAFE(cp, &nfs_gss_svc_ctx_hashtbl[i], gss_svc_entries, next) { |
2d21ac55 | 2832 | contexts++; |
6d2010ae A |
2833 | if (timenow > cp->gss_svc_incarnation + |
2834 | (cp->gss_svc_handle ? GSS_SVC_CTX_TTL : 0) | |
2835 | && cp->gss_svc_refcnt == 0) { | |
2d21ac55 A |
2836 | /* |
2837 | * A stale context - remove it | |
2838 | */ | |
2839 | LIST_REMOVE(cp, gss_svc_entries); | |
fe8ab488 | 2840 | NFS_GSS_DBG("Removing contex for %d\n", cp->gss_svc_uid); |
2d21ac55 A |
2841 | if (cp->gss_svc_seqbits) |
2842 | FREE(cp->gss_svc_seqbits, M_TEMP); | |
2843 | lck_mtx_destroy(cp->gss_svc_mtx, nfs_gss_svc_grp); | |
2844 | FREE(cp, M_TEMP); | |
2845 | contexts--; | |
2846 | } | |
2847 | } | |
2848 | } | |
2849 | ||
2850 | nfs_gss_ctx_count = contexts; | |
2851 | ||
2852 | /* | |
2853 | * If there are still some cached contexts left, | |
2854 | * set up another callout to check on them later. | |
2855 | */ | |
2856 | nfs_gss_timer_on = nfs_gss_ctx_count > 0; | |
2857 | if (nfs_gss_timer_on) | |
2858 | nfs_interval_timer_start(nfs_gss_svc_ctx_timer_call, | |
6d2010ae | 2859 | min(GSS_TIMER_PERIOD, max(GSS_CTX_TTL_MIN, nfsrv_gss_context_ttl)) * MSECS_PER_SEC); |
2d21ac55 A |
2860 | |
2861 | lck_mtx_unlock(nfs_gss_svc_ctx_mutex); | |
2862 | } | |
2863 | ||
2864 | /* | |
2865 | * Here the server receives an RPCSEC_GSS credential in an | |
2866 | * RPC call header. First there's some checking to make sure | |
2867 | * the credential is appropriate - whether the context is still | |
2868 | * being set up, or is complete. Then we use the handle to find | |
2869 | * the server's context and validate the verifier, which contains | |
2870 | * a signed checksum of the RPC header. If the verifier checks | |
2871 | * out, we extract the user's UID and groups from the context | |
2872 | * and use it to set up a UNIX credential for the user's request. | |
2873 | */ | |
2874 | int | |
2875 | nfs_gss_svc_cred_get(struct nfsrv_descript *nd, struct nfsm_chain *nmc) | |
2876 | { | |
2877 | uint32_t vers, proc, seqnum, service; | |
2878 | uint32_t handle, handle_len; | |
39037602 | 2879 | uint32_t major; |
2d21ac55 | 2880 | struct nfs_gss_svc_ctx *cp = NULL; |
39037602 | 2881 | uint32_t flavor = 0, header_len; |
2d21ac55 | 2882 | int error = 0; |
39037602 A |
2883 | uint32_t arglen, start; |
2884 | size_t argsize; | |
2885 | gss_buffer_desc cksum; | |
2d21ac55 | 2886 | struct nfsm_chain nmc_tmp; |
39037602 A |
2887 | mbuf_t reply_mbuf, prev_mbuf, pad_mbuf; |
2888 | ||
2d21ac55 | 2889 | vers = proc = seqnum = service = handle_len = 0; |
39037602 | 2890 | arglen = 0; |
2d21ac55 A |
2891 | |
2892 | nfsm_chain_get_32(error, nmc, vers); | |
2893 | if (vers != RPCSEC_GSS_VERS_1) { | |
2894 | error = NFSERR_AUTHERR | AUTH_REJECTCRED; | |
2895 | goto nfsmout; | |
2896 | } | |
2897 | ||
2898 | nfsm_chain_get_32(error, nmc, proc); | |
2899 | nfsm_chain_get_32(error, nmc, seqnum); | |
2900 | nfsm_chain_get_32(error, nmc, service); | |
2901 | nfsm_chain_get_32(error, nmc, handle_len); | |
2902 | if (error) | |
2903 | goto nfsmout; | |
2904 | ||
2905 | /* | |
2906 | * Make sure context setup/destroy is being done with a nullproc | |
2907 | */ | |
2908 | if (proc != RPCSEC_GSS_DATA && nd->nd_procnum != NFSPROC_NULL) { | |
2909 | error = NFSERR_AUTHERR | RPCSEC_GSS_CREDPROBLEM; | |
2910 | goto nfsmout; | |
2911 | } | |
2912 | ||
2913 | /* | |
2914 | * If the sequence number is greater than the max | |
2915 | * allowable, reject and have the client init a | |
2916 | * new context. | |
2917 | */ | |
2918 | if (seqnum > GSS_MAXSEQ) { | |
2919 | error = NFSERR_AUTHERR | RPCSEC_GSS_CTXPROBLEM; | |
2920 | goto nfsmout; | |
2921 | } | |
2922 | ||
2923 | nd->nd_sec = | |
2924 | service == RPCSEC_GSS_SVC_NONE ? RPCAUTH_KRB5 : | |
2925 | service == RPCSEC_GSS_SVC_INTEGRITY ? RPCAUTH_KRB5I : | |
2926 | service == RPCSEC_GSS_SVC_PRIVACY ? RPCAUTH_KRB5P : 0; | |
2927 | ||
2928 | if (proc == RPCSEC_GSS_INIT) { | |
2929 | /* | |
2930 | * Limit the total number of contexts | |
2931 | */ | |
2932 | if (nfs_gss_ctx_count > nfs_gss_ctx_max) { | |
2933 | error = NFSERR_AUTHERR | RPCSEC_GSS_CTXPROBLEM; | |
2934 | goto nfsmout; | |
2935 | } | |
2936 | ||
2937 | /* | |
2938 | * Set up a new context | |
2939 | */ | |
2940 | MALLOC(cp, struct nfs_gss_svc_ctx *, sizeof(*cp), M_TEMP, M_WAITOK|M_ZERO); | |
2941 | if (cp == NULL) { | |
2942 | error = ENOMEM; | |
2943 | goto nfsmout; | |
2944 | } | |
6d2010ae A |
2945 | cp->gss_svc_mtx = lck_mtx_alloc_init(nfs_gss_svc_grp, LCK_ATTR_NULL); |
2946 | cp->gss_svc_refcnt = 1; | |
2d21ac55 | 2947 | } else { |
2d21ac55 A |
2948 | /* |
2949 | * Use the handle to find the context | |
2950 | */ | |
2951 | if (handle_len != sizeof(handle)) { | |
2952 | error = NFSERR_AUTHERR | RPCSEC_GSS_CREDPROBLEM; | |
2953 | goto nfsmout; | |
2954 | } | |
2955 | nfsm_chain_get_32(error, nmc, handle); | |
2956 | if (error) | |
2957 | goto nfsmout; | |
2958 | cp = nfs_gss_svc_ctx_find(handle); | |
2959 | if (cp == NULL) { | |
2960 | error = NFSERR_AUTHERR | RPCSEC_GSS_CTXPROBLEM; | |
2961 | goto nfsmout; | |
2962 | } | |
2963 | } | |
2964 | ||
2965 | cp->gss_svc_proc = proc; | |
2966 | ||
2967 | if (proc == RPCSEC_GSS_DATA || proc == RPCSEC_GSS_DESTROY) { | |
6d2010ae | 2968 | struct posix_cred temp_pcred; |
2d21ac55 A |
2969 | |
2970 | if (cp->gss_svc_seqwin == 0) { | |
2971 | /* | |
2972 | * Context isn't complete | |
2973 | */ | |
2974 | error = NFSERR_AUTHERR | RPCSEC_GSS_CTXPROBLEM; | |
2975 | goto nfsmout; | |
2976 | } | |
2977 | ||
2978 | if (!nfs_gss_svc_seqnum_valid(cp, seqnum)) { | |
2979 | /* | |
2980 | * Sequence number is bad | |
2981 | */ | |
2982 | error = EINVAL; // drop the request | |
2983 | goto nfsmout; | |
2984 | } | |
2985 | ||
2d21ac55 A |
2986 | /* |
2987 | * Validate the verifier. | |
2988 | * The verifier contains an encrypted checksum | |
2989 | * of the call header from the XID up to and | |
2990 | * including the credential. We compute the | |
2991 | * checksum and compare it with what came in | |
2992 | * the verifier. | |
2993 | */ | |
39037602 | 2994 | header_len = nfsm_chain_offset(nmc); |
2d21ac55 | 2995 | nfsm_chain_get_32(error, nmc, flavor); |
39037602 | 2996 | nfsm_chain_get_32(error, nmc, cksum.length); |
6d2010ae A |
2997 | if (error) |
2998 | goto nfsmout; | |
39037602 | 2999 | if (flavor != RPCSEC_GSS || cksum.length > KRB5_MAX_MIC_SIZE) |
2d21ac55 | 3000 | error = NFSERR_AUTHERR | AUTH_BADVERF; |
39037602 A |
3001 | MALLOC(cksum.value, void *, cksum.length, M_TEMP, M_WAITOK); |
3002 | nfsm_chain_get_opaque(error, nmc, cksum.length, cksum.value); | |
2d21ac55 A |
3003 | if (error) |
3004 | goto nfsmout; | |
3005 | ||
39037602 A |
3006 | /* Now verify the client's call header checksum */ |
3007 | major = gss_krb5_verify_mic_mbuf((uint32_t *)&error, cp->gss_svc_ctx_id, nmc->nmc_mhead, 0, header_len, &cksum, NULL); | |
3008 | (void)gss_release_buffer(NULL, &cksum); | |
3009 | if (major != GSS_S_COMPLETE) { | |
3010 | printf("Server header: gss_krb5_verify_mic_mbuf failed %d\n", error); | |
2d21ac55 A |
3011 | error = NFSERR_AUTHERR | RPCSEC_GSS_CTXPROBLEM; |
3012 | goto nfsmout; | |
3013 | } | |
3014 | ||
3015 | nd->nd_gss_seqnum = seqnum; | |
3016 | ||
3017 | /* | |
3018 | * Set up the user's cred | |
3019 | */ | |
6d2010ae A |
3020 | bzero(&temp_pcred, sizeof(temp_pcred)); |
3021 | temp_pcred.cr_uid = cp->gss_svc_uid; | |
3022 | bcopy(cp->gss_svc_gids, temp_pcred.cr_groups, | |
2d21ac55 | 3023 | sizeof(gid_t) * cp->gss_svc_ngroups); |
6d2010ae | 3024 | temp_pcred.cr_ngroups = cp->gss_svc_ngroups; |
2d21ac55 | 3025 | |
6d2010ae | 3026 | nd->nd_cr = posix_cred_create(&temp_pcred); |
2d21ac55 A |
3027 | if (nd->nd_cr == NULL) { |
3028 | error = ENOMEM; | |
3029 | goto nfsmout; | |
3030 | } | |
b0d623f7 | 3031 | clock_get_uptime(&cp->gss_svc_incarnation); |
2d21ac55 A |
3032 | |
3033 | /* | |
3034 | * If the call arguments are integrity or privacy protected | |
3035 | * then we need to check them here. | |
3036 | */ | |
3037 | switch (service) { | |
3038 | case RPCSEC_GSS_SVC_NONE: | |
3039 | /* nothing to do */ | |
3040 | break; | |
3041 | case RPCSEC_GSS_SVC_INTEGRITY: | |
3042 | /* | |
3043 | * Here's what we expect in the integrity call args: | |
3044 | * | |
3045 | * - length of seq num + call args (4 bytes) | |
3046 | * - sequence number (4 bytes) | |
3047 | * - call args (variable bytes) | |
39037602 A |
3048 | * - length of checksum token |
3049 | * - checksum of seqnum + call args | |
2d21ac55 A |
3050 | */ |
3051 | nfsm_chain_get_32(error, nmc, arglen); // length of args | |
3052 | if (arglen > NFS_MAXPACKET) { | |
3053 | error = EBADRPC; | |
3054 | goto nfsmout; | |
3055 | } | |
3056 | ||
39037602 A |
3057 | nmc_tmp = *nmc; |
3058 | nfsm_chain_adv(error, &nmc_tmp, arglen); | |
3059 | nfsm_chain_get_32(error, &nmc_tmp, cksum.length); | |
3060 | MALLOC(cksum.value, void *, cksum.length, M_TEMP, M_WAITOK); | |
3061 | ||
3062 | if (cksum.value == NULL) { | |
3063 | error = EBADRPC; | |
3064 | goto nfsmout; | |
3065 | } | |
3066 | nfsm_chain_get_opaque(error, &nmc_tmp, cksum.length, cksum.value); | |
3067 | ||
3068 | /* Verify the checksum over the call args */ | |
2d21ac55 | 3069 | start = nfsm_chain_offset(nmc); |
39037602 A |
3070 | |
3071 | major = gss_krb5_verify_mic_mbuf((uint32_t *)&error, cp->gss_svc_ctx_id, | |
3072 | nmc->nmc_mhead, start, arglen, &cksum, NULL); | |
3073 | FREE(cksum.value, M_TEMP); | |
3074 | if (major != GSS_S_COMPLETE) { | |
3075 | printf("Server args: gss_krb5_verify_mic_mbuf failed %d\n", error); | |
3076 | error = EBADRPC; | |
3077 | goto nfsmout; | |
3078 | } | |
3079 | ||
2d21ac55 A |
3080 | /* |
3081 | * Get the sequence number prepended to the args | |
3082 | * and compare it against the one sent in the | |
3083 | * call credential. | |
3084 | */ | |
3085 | nfsm_chain_get_32(error, nmc, seqnum); | |
3086 | if (seqnum != nd->nd_gss_seqnum) { | |
3087 | error = EBADRPC; // returns as GARBAGEARGS | |
3088 | goto nfsmout; | |
3089 | } | |
2d21ac55 A |
3090 | break; |
3091 | case RPCSEC_GSS_SVC_PRIVACY: | |
3092 | /* | |
3093 | * Here's what we expect in the privacy call args: | |
3094 | * | |
39037602 | 3095 | * - length of wrap token |
2d21ac55 | 3096 | * - wrap token (37-40 bytes) |
2d21ac55 | 3097 | */ |
39037602 | 3098 | prev_mbuf = nmc->nmc_mcur; |
2d21ac55 A |
3099 | nfsm_chain_get_32(error, nmc, arglen); // length of args |
3100 | if (arglen > NFS_MAXPACKET) { | |
3101 | error = EBADRPC; | |
3102 | goto nfsmout; | |
3103 | } | |
39037602 A |
3104 | |
3105 | /* Get the wrap token (current mbuf in the chain starting at the current offset) */ | |
3106 | start = nmc->nmc_ptr - (caddr_t)mbuf_data(nmc->nmc_mcur); | |
3107 | ||
3108 | /* split out the wrap token */ | |
3109 | argsize = arglen; | |
3110 | error = gss_normalize_mbuf(nmc->nmc_mcur, start, &argsize, &reply_mbuf, &pad_mbuf, 0); | |
2d21ac55 A |
3111 | if (error) |
3112 | goto nfsmout; | |
39037602 A |
3113 | |
3114 | assert(argsize == arglen); | |
3115 | if (pad_mbuf) { | |
3116 | assert(nfsm_pad(arglen) == mbuf_len(pad_mbuf)); | |
3117 | mbuf_free(pad_mbuf); | |
3118 | } else { | |
3119 | assert(nfsm_pad(arglen) == 0); | |
3120 | } | |
3121 | ||
3122 | major = gss_krb5_unwrap_mbuf((uint32_t *)&error, cp->gss_svc_ctx_id, &reply_mbuf, 0, arglen, NULL, NULL); | |
3123 | if (major != GSS_S_COMPLETE) { | |
3124 | printf("%s: gss_krb5_unwrap_mbuf failes %d\n", __func__, error); | |
2d21ac55 A |
3125 | goto nfsmout; |
3126 | } | |
3127 | ||
39037602 A |
3128 | /* Now replace the wrapped arguments with the unwrapped ones */ |
3129 | mbuf_setnext(prev_mbuf, reply_mbuf); | |
3130 | nmc->nmc_mcur = reply_mbuf; | |
3131 | nmc->nmc_ptr = mbuf_data(reply_mbuf); | |
3132 | nmc->nmc_left = mbuf_len(reply_mbuf); | |
3133 | ||
3134 | /* | |
3135 | * - sequence number (4 bytes) | |
3136 | * - call args | |
3137 | */ | |
3138 | ||
3139 | // nfsm_chain_reverse(nmc, nfsm_pad(toklen)); | |
3140 | ||
2d21ac55 A |
3141 | /* |
3142 | * Get the sequence number prepended to the args | |
3143 | * and compare it against the one sent in the | |
3144 | * call credential. | |
3145 | */ | |
2d21ac55 A |
3146 | nfsm_chain_get_32(error, nmc, seqnum); |
3147 | if (seqnum != nd->nd_gss_seqnum) { | |
39037602 A |
3148 | printf("%s: Sequence number mismatch seqnum = %d nd->nd_gss_seqnum = %d\n", |
3149 | __func__, seqnum, nd->nd_gss_seqnum); | |
3150 | printmbuf("reply_mbuf", nmc->nmc_mhead, 0, 0); | |
3151 | printf("reply_mbuf %p nmc_head %p\n", reply_mbuf, nmc->nmc_mhead); | |
2d21ac55 A |
3152 | error = EBADRPC; // returns as GARBAGEARGS |
3153 | goto nfsmout; | |
3154 | } | |
3155 | break; | |
3156 | } | |
3157 | } else { | |
39037602 | 3158 | uint32_t verflen; |
2d21ac55 A |
3159 | /* |
3160 | * If the proc is RPCSEC_GSS_INIT or RPCSEC_GSS_CONTINUE_INIT | |
3161 | * then we expect a null verifier. | |
3162 | */ | |
3163 | nfsm_chain_get_32(error, nmc, flavor); | |
3164 | nfsm_chain_get_32(error, nmc, verflen); | |
3165 | if (error || flavor != RPCAUTH_NULL || verflen > 0) | |
3166 | error = NFSERR_AUTHERR | RPCSEC_GSS_CREDPROBLEM; | |
6d2010ae A |
3167 | if (error) { |
3168 | if (proc == RPCSEC_GSS_INIT) { | |
3169 | lck_mtx_destroy(cp->gss_svc_mtx, nfs_gss_svc_grp); | |
3170 | FREE(cp, M_TEMP); | |
3171 | cp = NULL; | |
3172 | } | |
2d21ac55 | 3173 | goto nfsmout; |
6d2010ae | 3174 | } |
2d21ac55 A |
3175 | } |
3176 | ||
3177 | nd->nd_gss_context = cp; | |
6d2010ae | 3178 | return 0; |
2d21ac55 | 3179 | nfsmout: |
6d2010ae A |
3180 | if (cp) |
3181 | nfs_gss_svc_ctx_deref(cp); | |
2d21ac55 A |
3182 | return (error); |
3183 | } | |
3184 | ||
3185 | /* | |
3186 | * Insert the server's verifier into the RPC reply header. | |
3187 | * It contains a signed checksum of the sequence number that | |
3188 | * was received in the RPC call. | |
3189 | * Then go on to add integrity or privacy if necessary. | |
3190 | */ | |
3191 | int | |
3192 | nfs_gss_svc_verf_put(struct nfsrv_descript *nd, struct nfsm_chain *nmc) | |
3193 | { | |
3194 | struct nfs_gss_svc_ctx *cp; | |
3195 | int error = 0; | |
39037602 A |
3196 | gss_buffer_desc cksum, seqbuf; |
3197 | uint32_t network_seqnum; | |
2d21ac55 | 3198 | cp = nd->nd_gss_context; |
39037602 A |
3199 | uint32_t major; |
3200 | ||
2d21ac55 A |
3201 | if (cp->gss_svc_major != GSS_S_COMPLETE) { |
3202 | /* | |
3203 | * If the context isn't yet complete | |
3204 | * then return a null verifier. | |
3205 | */ | |
3206 | nfsm_chain_add_32(error, nmc, RPCAUTH_NULL); | |
3207 | nfsm_chain_add_32(error, nmc, 0); | |
3208 | return (error); | |
3209 | } | |
3210 | ||
3211 | /* | |
3212 | * Compute checksum of the request seq number | |
3213 | * If it's the final reply of context setup | |
3214 | * then return the checksum of the context | |
3215 | * window size. | |
3216 | */ | |
39037602 | 3217 | seqbuf.length = NFSX_UNSIGNED; |
2d21ac55 A |
3218 | if (cp->gss_svc_proc == RPCSEC_GSS_INIT || |
3219 | cp->gss_svc_proc == RPCSEC_GSS_CONTINUE_INIT) | |
39037602 | 3220 | network_seqnum = htonl(cp->gss_svc_seqwin); |
2d21ac55 | 3221 | else |
39037602 A |
3222 | network_seqnum = htonl(nd->nd_gss_seqnum); |
3223 | seqbuf.value = &network_seqnum; | |
3224 | ||
3225 | major = gss_krb5_get_mic((uint32_t *)&error, cp->gss_svc_ctx_id, 0, &seqbuf, &cksum); | |
3226 | if (major != GSS_S_COMPLETE) | |
3227 | return (error); | |
3228 | ||
2d21ac55 A |
3229 | /* |
3230 | * Now wrap it in a token and add | |
3231 | * the verifier to the reply. | |
3232 | */ | |
2d21ac55 | 3233 | nfsm_chain_add_32(error, nmc, RPCSEC_GSS); |
39037602 A |
3234 | nfsm_chain_add_32(error, nmc, cksum.length); |
3235 | nfsm_chain_add_opaque(error, nmc, cksum.value, cksum.length); | |
3236 | gss_release_buffer(NULL, &cksum); | |
2d21ac55 A |
3237 | |
3238 | return (error); | |
3239 | } | |
3240 | ||
3241 | /* | |
3242 | * The results aren't available yet, but if they need to be | |
3243 | * checksummed for integrity protection or encrypted, then | |
3244 | * we can record the start offset here, insert a place-holder | |
3245 | * for the results length, as well as the sequence number. | |
3246 | * The rest of the work is done later by nfs_gss_svc_protect_reply() | |
3247 | * when the results are available. | |
3248 | */ | |
3249 | int | |
3250 | nfs_gss_svc_prepare_reply(struct nfsrv_descript *nd, struct nfsm_chain *nmc) | |
3251 | { | |
3252 | struct nfs_gss_svc_ctx *cp = nd->nd_gss_context; | |
3253 | int error = 0; | |
3254 | ||
3255 | if (cp->gss_svc_proc == RPCSEC_GSS_INIT || | |
3256 | cp->gss_svc_proc == RPCSEC_GSS_CONTINUE_INIT) | |
3257 | return (0); | |
3258 | ||
3259 | switch (nd->nd_sec) { | |
3260 | case RPCAUTH_KRB5: | |
3261 | /* Nothing to do */ | |
3262 | break; | |
3263 | case RPCAUTH_KRB5I: | |
2d21ac55 A |
3264 | case RPCAUTH_KRB5P: |
3265 | nd->nd_gss_mb = nmc->nmc_mcur; // record current mbuf | |
3266 | nfsm_chain_finish_mbuf(error, nmc); // split the chain here | |
2d21ac55 A |
3267 | break; |
3268 | } | |
3269 | ||
3270 | return (error); | |
3271 | } | |
3272 | ||
3273 | /* | |
3274 | * The results are checksummed or encrypted for return to the client | |
3275 | */ | |
3276 | int | |
39037602 | 3277 | nfs_gss_svc_protect_reply(struct nfsrv_descript *nd, mbuf_t mrep __unused) |
2d21ac55 A |
3278 | { |
3279 | struct nfs_gss_svc_ctx *cp = nd->nd_gss_context; | |
3280 | struct nfsm_chain nmrep_res, *nmc_res = &nmrep_res; | |
2d21ac55 A |
3281 | mbuf_t mb, results; |
3282 | uint32_t reslen; | |
2d21ac55 A |
3283 | int error = 0; |
3284 | ||
39037602 | 3285 | /* XXX |
2d21ac55 A |
3286 | * Using a reference to the mbuf where we previously split the reply |
3287 | * mbuf chain, we split the mbuf chain argument into two mbuf chains, | |
3288 | * one that allows us to prepend a length field or token, (nmc_pre) | |
3289 | * and the second which holds just the results that we're going to | |
3290 | * checksum and/or encrypt. When we're done, we join the chains back | |
3291 | * together. | |
3292 | */ | |
39037602 | 3293 | |
2d21ac55 A |
3294 | mb = nd->nd_gss_mb; // the mbuf where we split |
3295 | results = mbuf_next(mb); // first mbuf in the results | |
2d21ac55 A |
3296 | error = mbuf_setnext(mb, NULL); // disconnect the chains |
3297 | if (error) | |
3298 | return (error); | |
39037602 A |
3299 | nfs_gss_nfsm_chain(nmc_res, mb); // set up the prepend chain |
3300 | nfsm_chain_build_done(error, nmc_res); | |
3301 | if (error) | |
3302 | return (error); | |
2d21ac55 A |
3303 | |
3304 | if (nd->nd_sec == RPCAUTH_KRB5I) { | |
39037602 | 3305 | error = rpc_gss_integ_data_create(cp->gss_svc_ctx_id, &results, nd->nd_gss_seqnum, &reslen); |
2d21ac55 A |
3306 | } else { |
3307 | /* RPCAUTH_KRB5P */ | |
39037602 | 3308 | error = rpc_gss_priv_data_create(cp->gss_svc_ctx_id, &results, nd->nd_gss_seqnum, &reslen); |
2d21ac55 | 3309 | } |
39037602 A |
3310 | nfs_gss_append_chain(nmc_res, results); // Append the results mbufs |
3311 | nfsm_chain_build_done(error, nmc_res); | |
2d21ac55 A |
3312 | |
3313 | return (error); | |
3314 | } | |
3315 | ||
3316 | /* | |
3317 | * This function handles the context setup calls from the client. | |
3318 | * Essentially, it implements the NFS null procedure calls when | |
3319 | * an RPCSEC_GSS credential is used. | |
3320 | * This is the context maintenance function. It creates and | |
3321 | * destroys server contexts at the whim of the client. | |
3322 | * During context creation, it receives GSS-API tokens from the | |
3323 | * client, passes them up to gssd, and returns a received token | |
3324 | * back to the client in the null procedure reply. | |
3325 | */ | |
3326 | int | |
3327 | nfs_gss_svc_ctx_init(struct nfsrv_descript *nd, struct nfsrv_sock *slp, mbuf_t *mrepp) | |
3328 | { | |
3329 | struct nfs_gss_svc_ctx *cp = NULL; | |
2d21ac55 A |
3330 | int error = 0; |
3331 | int autherr = 0; | |
3332 | struct nfsm_chain *nmreq, nmrep; | |
3333 | int sz; | |
3334 | ||
3335 | nmreq = &nd->nd_nmreq; | |
3336 | nfsm_chain_null(&nmrep); | |
3337 | *mrepp = NULL; | |
3338 | cp = nd->nd_gss_context; | |
3339 | nd->nd_repstat = 0; | |
3340 | ||
3341 | switch (cp->gss_svc_proc) { | |
3342 | case RPCSEC_GSS_INIT: | |
2d21ac55 | 3343 | nfs_gss_svc_ctx_insert(cp); |
2d21ac55 A |
3344 | /* FALLTHRU */ |
3345 | ||
3346 | case RPCSEC_GSS_CONTINUE_INIT: | |
3347 | /* Get the token from the request */ | |
3348 | nfsm_chain_get_32(error, nmreq, cp->gss_svc_tokenlen); | |
3349 | if (cp->gss_svc_tokenlen == 0) { | |
3350 | autherr = RPCSEC_GSS_CREDPROBLEM; | |
3351 | break; | |
3352 | } | |
3353 | MALLOC(cp->gss_svc_token, u_char *, cp->gss_svc_tokenlen, M_TEMP, M_WAITOK); | |
3354 | if (cp->gss_svc_token == NULL) { | |
3355 | autherr = RPCSEC_GSS_CREDPROBLEM; | |
3356 | break; | |
3357 | } | |
3358 | nfsm_chain_get_opaque(error, nmreq, cp->gss_svc_tokenlen, cp->gss_svc_token); | |
3359 | ||
3360 | /* Use the token in a gss_accept_sec_context upcall */ | |
3361 | error = nfs_gss_svc_gssd_upcall(cp); | |
3362 | if (error) { | |
3363 | autherr = RPCSEC_GSS_CREDPROBLEM; | |
b0d623f7 | 3364 | if (error == NFSERR_EAUTH) |
2d21ac55 A |
3365 | error = 0; |
3366 | break; | |
3367 | } | |
3368 | ||
3369 | /* | |
3370 | * If the context isn't complete, pass the new token | |
3371 | * back to the client for another round. | |
3372 | */ | |
3373 | if (cp->gss_svc_major != GSS_S_COMPLETE) | |
3374 | break; | |
3375 | ||
3376 | /* | |
3377 | * Now the server context is complete. | |
3378 | * Finish setup. | |
3379 | */ | |
b0d623f7 A |
3380 | clock_get_uptime(&cp->gss_svc_incarnation); |
3381 | ||
2d21ac55 A |
3382 | cp->gss_svc_seqwin = GSS_SVC_SEQWINDOW; |
3383 | MALLOC(cp->gss_svc_seqbits, uint32_t *, | |
3384 | nfsm_rndup((cp->gss_svc_seqwin + 7) / 8), M_TEMP, M_WAITOK|M_ZERO); | |
3385 | if (cp->gss_svc_seqbits == NULL) { | |
3386 | autherr = RPCSEC_GSS_CREDPROBLEM; | |
3387 | break; | |
3388 | } | |
2d21ac55 A |
3389 | break; |
3390 | ||
3391 | case RPCSEC_GSS_DATA: | |
3392 | /* Just a nullproc ping - do nothing */ | |
3393 | break; | |
3394 | ||
3395 | case RPCSEC_GSS_DESTROY: | |
3396 | /* | |
3397 | * Don't destroy the context immediately because | |
3398 | * other active requests might still be using it. | |
3399 | * Instead, schedule it for destruction after | |
3400 | * GSS_CTX_PEND time has elapsed. | |
3401 | */ | |
3402 | cp = nfs_gss_svc_ctx_find(cp->gss_svc_handle); | |
3403 | if (cp != NULL) { | |
3404 | cp->gss_svc_handle = 0; // so it can't be found | |
3405 | lck_mtx_lock(cp->gss_svc_mtx); | |
3406 | clock_interval_to_deadline(GSS_CTX_PEND, NSEC_PER_SEC, | |
b0d623f7 | 3407 | &cp->gss_svc_incarnation); |
2d21ac55 A |
3408 | lck_mtx_unlock(cp->gss_svc_mtx); |
3409 | } | |
3410 | break; | |
3411 | default: | |
3412 | autherr = RPCSEC_GSS_CREDPROBLEM; | |
3413 | break; | |
3414 | } | |
3415 | ||
3416 | /* Now build the reply */ | |
3417 | ||
3418 | if (nd->nd_repstat == 0) | |
3419 | nd->nd_repstat = autherr ? (NFSERR_AUTHERR | autherr) : NFSERR_RETVOID; | |
3420 | sz = 7 * NFSX_UNSIGNED + nfsm_rndup(cp->gss_svc_tokenlen); // size of results | |
3421 | error = nfsrv_rephead(nd, slp, &nmrep, sz); | |
3422 | *mrepp = nmrep.nmc_mhead; | |
3423 | if (error || autherr) | |
3424 | goto nfsmout; | |
3425 | ||
3426 | if (cp->gss_svc_proc == RPCSEC_GSS_INIT || | |
3427 | cp->gss_svc_proc == RPCSEC_GSS_CONTINUE_INIT) { | |
3428 | nfsm_chain_add_32(error, &nmrep, sizeof(cp->gss_svc_handle)); | |
3429 | nfsm_chain_add_32(error, &nmrep, cp->gss_svc_handle); | |
3430 | ||
3431 | nfsm_chain_add_32(error, &nmrep, cp->gss_svc_major); | |
3432 | nfsm_chain_add_32(error, &nmrep, cp->gss_svc_minor); | |
3433 | nfsm_chain_add_32(error, &nmrep, cp->gss_svc_seqwin); | |
3434 | ||
3435 | nfsm_chain_add_32(error, &nmrep, cp->gss_svc_tokenlen); | |
2d21ac55 | 3436 | if (cp->gss_svc_token != NULL) { |
b0d623f7 | 3437 | nfsm_chain_add_opaque(error, &nmrep, cp->gss_svc_token, cp->gss_svc_tokenlen); |
2d21ac55 A |
3438 | FREE(cp->gss_svc_token, M_TEMP); |
3439 | cp->gss_svc_token = NULL; | |
3440 | } | |
3441 | } | |
3442 | ||
3443 | nfsmout: | |
3444 | if (autherr != 0) { | |
b0d623f7 | 3445 | nd->nd_gss_context = NULL; |
2d21ac55 A |
3446 | LIST_REMOVE(cp, gss_svc_entries); |
3447 | if (cp->gss_svc_seqbits != NULL) | |
3448 | FREE(cp->gss_svc_seqbits, M_TEMP); | |
3449 | if (cp->gss_svc_token != NULL) | |
3450 | FREE(cp->gss_svc_token, M_TEMP); | |
3451 | lck_mtx_destroy(cp->gss_svc_mtx, nfs_gss_svc_grp); | |
3452 | FREE(cp, M_TEMP); | |
3453 | } | |
3454 | ||
3455 | nfsm_chain_build_done(error, &nmrep); | |
3456 | if (error) { | |
3457 | nfsm_chain_cleanup(&nmrep); | |
3458 | *mrepp = NULL; | |
3459 | } | |
3460 | return (error); | |
3461 | } | |
3462 | ||
3463 | /* | |
3464 | * This is almost a mirror-image of the client side upcall. | |
3465 | * It passes and receives a token, but invokes gss_accept_sec_context. | |
3466 | * If it's the final call of the context setup, then gssd also returns | |
3467 | * the session key and the user's UID. | |
3468 | */ | |
3469 | static int | |
3470 | nfs_gss_svc_gssd_upcall(struct nfs_gss_svc_ctx *cp) | |
3471 | { | |
3472 | kern_return_t kr; | |
3473 | mach_port_t mp; | |
3474 | int retry_cnt = 0; | |
39037602 A |
3475 | gssd_byte_buffer octx = NULL; |
3476 | uint32_t lucidlen = 0; | |
3477 | void *lucid_ctx_buffer; | |
b0d623f7 | 3478 | uint32_t ret_flags; |
2d21ac55 | 3479 | vm_map_copy_t itoken = NULL; |
6d2010ae | 3480 | gssd_byte_buffer otoken = NULL; |
b0d623f7 | 3481 | mach_msg_type_number_t otokenlen; |
2d21ac55 A |
3482 | int error = 0; |
3483 | char svcname[] = "nfs"; | |
3484 | ||
316670eb | 3485 | kr = host_get_gssd_port(host_priv_self(), &mp); |
2d21ac55 | 3486 | if (kr != KERN_SUCCESS) { |
b0d623f7 A |
3487 | printf("nfs_gss_svc_gssd_upcall: can't get gssd port, status %x (%d)\n", kr, kr); |
3488 | goto out; | |
2d21ac55 A |
3489 | } |
3490 | if (!IPC_PORT_VALID(mp)) { | |
3491 | printf("nfs_gss_svc_gssd_upcall: gssd port not valid\n"); | |
b0d623f7 | 3492 | goto out; |
2d21ac55 A |
3493 | } |
3494 | ||
3495 | if (cp->gss_svc_tokenlen > 0) | |
3496 | nfs_gss_mach_alloc_buffer(cp->gss_svc_token, cp->gss_svc_tokenlen, &itoken); | |
3497 | ||
3498 | retry: | |
39037602 | 3499 | printf("Calling mach_gss_accept_sec_context\n"); |
2d21ac55 A |
3500 | kr = mach_gss_accept_sec_context( |
3501 | mp, | |
6d2010ae | 3502 | (gssd_byte_buffer) itoken, (mach_msg_type_number_t) cp->gss_svc_tokenlen, |
2d21ac55 A |
3503 | svcname, |
3504 | 0, | |
2d21ac55 A |
3505 | &cp->gss_svc_context, |
3506 | &cp->gss_svc_cred_handle, | |
b0d623f7 | 3507 | &ret_flags, |
2d21ac55 A |
3508 | &cp->gss_svc_uid, |
3509 | cp->gss_svc_gids, | |
3510 | &cp->gss_svc_ngroups, | |
39037602 | 3511 | &octx, (mach_msg_type_number_t *) &lucidlen, |
b0d623f7 | 3512 | &otoken, &otokenlen, |
2d21ac55 A |
3513 | &cp->gss_svc_major, |
3514 | &cp->gss_svc_minor); | |
3515 | ||
39037602 | 3516 | printf("mach_gss_accept_sec_context returned %d\n", kr); |
2d21ac55 | 3517 | if (kr != KERN_SUCCESS) { |
b0d623f7 | 3518 | printf("nfs_gss_svc_gssd_upcall failed: %x (%d)\n", kr, kr); |
2d21ac55 | 3519 | if (kr == MIG_SERVER_DIED && cp->gss_svc_context == 0 && |
b0d623f7 A |
3520 | retry_cnt++ < NFS_GSS_MACH_MAX_RETRIES) { |
3521 | if (cp->gss_svc_tokenlen > 0) | |
3522 | nfs_gss_mach_alloc_buffer(cp->gss_svc_token, cp->gss_svc_tokenlen, &itoken); | |
2d21ac55 | 3523 | goto retry; |
b0d623f7 | 3524 | } |
316670eb | 3525 | host_release_special_port(mp); |
b0d623f7 | 3526 | goto out; |
2d21ac55 A |
3527 | } |
3528 | ||
316670eb | 3529 | host_release_special_port(mp); |
b0d623f7 | 3530 | |
39037602 A |
3531 | if (lucidlen > 0) { |
3532 | if (lucidlen > MAX_LUCIDLEN) { | |
3533 | printf("nfs_gss_svc_gssd_upcall: bad context length (%d)\n", lucidlen); | |
3534 | vm_map_copy_discard((vm_map_copy_t) octx); | |
b0d623f7 A |
3535 | vm_map_copy_discard((vm_map_copy_t) otoken); |
3536 | goto out; | |
2d21ac55 | 3537 | } |
39037602 A |
3538 | MALLOC(lucid_ctx_buffer, void *, lucidlen, M_TEMP, M_WAITOK | M_ZERO); |
3539 | error = nfs_gss_mach_vmcopyout((vm_map_copy_t) octx, lucidlen, lucid_ctx_buffer); | |
b0d623f7 A |
3540 | if (error) { |
3541 | vm_map_copy_discard((vm_map_copy_t) otoken); | |
39037602 | 3542 | FREE(lucid_ctx_buffer, M_TEMP); |
b0d623f7 A |
3543 | goto out; |
3544 | } | |
39037602 A |
3545 | if (cp->gss_svc_ctx_id) |
3546 | gss_krb5_destroy_context(cp->gss_svc_ctx_id); | |
3547 | cp->gss_svc_ctx_id = gss_krb5_make_context(lucid_ctx_buffer, lucidlen); | |
3548 | if (cp->gss_svc_ctx_id == NULL) { | |
3549 | printf("Failed to make context from lucid_ctx_buffer\n"); | |
b0d623f7 | 3550 | goto out; |
39037602 | 3551 | } |
2d21ac55 A |
3552 | } |
3553 | ||
b0d623f7 A |
3554 | /* Free context token used as input */ |
3555 | if (cp->gss_svc_token) | |
3556 | FREE(cp->gss_svc_token, M_TEMP); | |
3557 | cp->gss_svc_token = NULL; | |
3558 | cp->gss_svc_tokenlen = 0; | |
3559 | ||
3560 | if (otokenlen > 0) { | |
3561 | /* Set context token to gss output token */ | |
3562 | MALLOC(cp->gss_svc_token, u_char *, otokenlen, M_TEMP, M_WAITOK); | |
3563 | if (cp->gss_svc_token == NULL) { | |
3564 | printf("nfs_gss_svc_gssd_upcall: could not allocate %d bytes\n", otokenlen); | |
3565 | vm_map_copy_discard((vm_map_copy_t) otoken); | |
2d21ac55 | 3566 | return (ENOMEM); |
b0d623f7 A |
3567 | } |
3568 | error = nfs_gss_mach_vmcopyout((vm_map_copy_t) otoken, otokenlen, cp->gss_svc_token); | |
3569 | if (error) { | |
3570 | FREE(cp->gss_svc_token, M_TEMP); | |
3571 | cp->gss_svc_token = NULL; | |
3572 | return (NFSERR_EAUTH); | |
3573 | } | |
3574 | cp->gss_svc_tokenlen = otokenlen; | |
2d21ac55 A |
3575 | } |
3576 | ||
b0d623f7 A |
3577 | return (0); |
3578 | ||
3579 | out: | |
3580 | FREE(cp->gss_svc_token, M_TEMP); | |
3581 | cp->gss_svc_tokenlen = 0; | |
3582 | cp->gss_svc_token = NULL; | |
3583 | ||
3584 | return (NFSERR_EAUTH); | |
2d21ac55 A |
3585 | } |
3586 | ||
3587 | /* | |
3588 | * Validate the sequence number in the credential as described | |
3589 | * in RFC 2203 Section 5.3.3.1 | |
3590 | * | |
3591 | * Here the window of valid sequence numbers is represented by | |
3592 | * a bitmap. As each sequence number is received, its bit is | |
3593 | * set in the bitmap. An invalid sequence number lies below | |
3594 | * the lower bound of the window, or is within the window but | |
3595 | * has its bit already set. | |
3596 | */ | |
3597 | static int | |
3598 | nfs_gss_svc_seqnum_valid(struct nfs_gss_svc_ctx *cp, uint32_t seq) | |
3599 | { | |
3600 | uint32_t *bits = cp->gss_svc_seqbits; | |
3601 | uint32_t win = cp->gss_svc_seqwin; | |
3602 | uint32_t i; | |
3603 | ||
3604 | lck_mtx_lock(cp->gss_svc_mtx); | |
3605 | ||
3606 | /* | |
3607 | * If greater than the window upper bound, | |
3608 | * move the window up, and set the bit. | |
3609 | */ | |
3610 | if (seq > cp->gss_svc_seqmax) { | |
3611 | if (seq - cp->gss_svc_seqmax > win) | |
3612 | bzero(bits, nfsm_rndup((win + 7) / 8)); | |
3613 | else | |
3614 | for (i = cp->gss_svc_seqmax + 1; i < seq; i++) | |
3615 | win_resetbit(bits, i % win); | |
3616 | win_setbit(bits, seq % win); | |
3617 | cp->gss_svc_seqmax = seq; | |
3618 | lck_mtx_unlock(cp->gss_svc_mtx); | |
3619 | return (1); | |
3620 | } | |
3621 | ||
3622 | /* | |
3623 | * Invalid if below the lower bound of the window | |
3624 | */ | |
3625 | if (seq <= cp->gss_svc_seqmax - win) { | |
3626 | lck_mtx_unlock(cp->gss_svc_mtx); | |
3627 | return (0); | |
3628 | } | |
3629 | ||
3630 | /* | |
3631 | * In the window, invalid if the bit is already set | |
3632 | */ | |
3633 | if (win_getbit(bits, seq % win)) { | |
3634 | lck_mtx_unlock(cp->gss_svc_mtx); | |
3635 | return (0); | |
3636 | } | |
3637 | win_setbit(bits, seq % win); | |
3638 | lck_mtx_unlock(cp->gss_svc_mtx); | |
3639 | return (1); | |
3640 | } | |
3641 | ||
6d2010ae A |
3642 | /* |
3643 | * Drop a reference to a context | |
3644 | * | |
3645 | * Note that it's OK for the context to exist | |
3646 | * with a refcount of zero. The refcount isn't | |
3647 | * checked until we're about to reap an expired one. | |
3648 | */ | |
3649 | void | |
3650 | nfs_gss_svc_ctx_deref(struct nfs_gss_svc_ctx *cp) | |
3651 | { | |
3652 | lck_mtx_lock(cp->gss_svc_mtx); | |
3653 | if (cp->gss_svc_refcnt > 0) | |
3654 | cp->gss_svc_refcnt--; | |
3655 | else | |
3656 | printf("nfs_gss_ctx_deref: zero refcount\n"); | |
3657 | lck_mtx_unlock(cp->gss_svc_mtx); | |
3658 | } | |
3659 | ||
2d21ac55 A |
3660 | /* |
3661 | * Called at NFS server shutdown - destroy all contexts | |
3662 | */ | |
3663 | void | |
3664 | nfs_gss_svc_cleanup(void) | |
3665 | { | |
3666 | struct nfs_gss_svc_ctx_hashhead *head; | |
3667 | struct nfs_gss_svc_ctx *cp, *ncp; | |
3668 | int i; | |
3669 | ||
3670 | lck_mtx_lock(nfs_gss_svc_ctx_mutex); | |
3671 | ||
3672 | /* | |
3673 | * Run through all the buckets | |
3674 | */ | |
3675 | for (i = 0; i < SVC_CTX_HASHSZ; i++) { | |
3676 | /* | |
3677 | * Remove and free all entries in the bucket | |
3678 | */ | |
3679 | head = &nfs_gss_svc_ctx_hashtbl[i]; | |
3680 | LIST_FOREACH_SAFE(cp, head, gss_svc_entries, ncp) { | |
3681 | LIST_REMOVE(cp, gss_svc_entries); | |
3682 | if (cp->gss_svc_seqbits) | |
3683 | FREE(cp->gss_svc_seqbits, M_TEMP); | |
3684 | lck_mtx_destroy(cp->gss_svc_mtx, nfs_gss_svc_grp); | |
3685 | FREE(cp, M_TEMP); | |
3686 | } | |
3687 | } | |
3688 | ||
3689 | lck_mtx_unlock(nfs_gss_svc_ctx_mutex); | |
3690 | } | |
3691 | ||
3692 | #endif /* NFSSERVER */ | |
3693 | ||
3694 | ||
3695 | /************* | |
3696 | * The following functions are used by both client and server. | |
3697 | */ | |
3698 | ||
3699 | /* | |
316670eb A |
3700 | * Release a host special port that was obtained by host_get_special_port |
3701 | * or one of its macros (host_get_gssd_port in this case). | |
2d21ac55 A |
3702 | * This really should be in a public kpi. |
3703 | */ | |
3704 | ||
3705 | /* This should be in a public header if this routine is not */ | |
3706 | extern void ipc_port_release_send(ipc_port_t); | |
3707 | extern ipc_port_t ipc_port_copy_send(ipc_port_t); | |
3708 | ||
3709 | static void | |
316670eb | 3710 | host_release_special_port(mach_port_t mp) |
2d21ac55 | 3711 | { |
6d2010ae A |
3712 | if (IPC_PORT_VALID(mp)) |
3713 | ipc_port_release_send(mp); | |
2d21ac55 A |
3714 | } |
3715 | ||
3716 | static mach_port_t | |
316670eb | 3717 | host_copy_special_port(mach_port_t mp) |
2d21ac55 | 3718 | { |
316670eb | 3719 | return (ipc_port_copy_send(mp)); |
2d21ac55 A |
3720 | } |
3721 | ||
3722 | /* | |
3723 | * The token that is sent and received in the gssd upcall | |
3724 | * has unbounded variable length. Mach RPC does not pass | |
3725 | * the token in-line. Instead it uses page mapping to handle | |
3726 | * these parameters. This function allocates a VM buffer | |
3727 | * to hold the token for an upcall and copies the token | |
3728 | * (received from the client) into it. The VM buffer is | |
3729 | * marked with a src_destroy flag so that the upcall will | |
3730 | * automatically de-allocate the buffer when the upcall is | |
3731 | * complete. | |
3732 | */ | |
3733 | static void | |
3734 | nfs_gss_mach_alloc_buffer(u_char *buf, uint32_t buflen, vm_map_copy_t *addr) | |
3735 | { | |
3736 | kern_return_t kr; | |
3737 | vm_offset_t kmem_buf; | |
3738 | vm_size_t tbuflen; | |
3739 | ||
3740 | *addr = NULL; | |
3741 | if (buf == NULL || buflen == 0) | |
3742 | return; | |
3743 | ||
39236c6e A |
3744 | tbuflen = vm_map_round_page(buflen, |
3745 | vm_map_page_mask(ipc_kernel_map)); | |
5ba3f43e | 3746 | kr = vm_allocate_kernel(ipc_kernel_map, &kmem_buf, tbuflen, VM_FLAGS_ANYWHERE, VM_KERN_MEMORY_FILE); |
2d21ac55 A |
3747 | if (kr != 0) { |
3748 | printf("nfs_gss_mach_alloc_buffer: vm_allocate failed\n"); | |
3749 | return; | |
3750 | } | |
3751 | ||
5ba3f43e | 3752 | kr = vm_map_wire_kernel(ipc_kernel_map, |
39236c6e A |
3753 | vm_map_trunc_page(kmem_buf, |
3754 | vm_map_page_mask(ipc_kernel_map)), | |
3755 | vm_map_round_page(kmem_buf + tbuflen, | |
3756 | vm_map_page_mask(ipc_kernel_map)), | |
5ba3f43e | 3757 | VM_PROT_READ|VM_PROT_WRITE, VM_KERN_MEMORY_FILE, FALSE); |
b0d623f7 A |
3758 | if (kr != 0) { |
3759 | printf("nfs_gss_mach_alloc_buffer: vm_map_wire failed\n"); | |
3760 | return; | |
3761 | } | |
3762 | ||
2d21ac55 | 3763 | bcopy(buf, (void *) kmem_buf, buflen); |
b0d623f7 A |
3764 | // Shouldn't need to bzero below since vm_allocate returns zeroed pages |
3765 | // bzero(kmem_buf + buflen, tbuflen - buflen); | |
3766 | ||
39236c6e A |
3767 | kr = vm_map_unwire(ipc_kernel_map, |
3768 | vm_map_trunc_page(kmem_buf, | |
3769 | vm_map_page_mask(ipc_kernel_map)), | |
3770 | vm_map_round_page(kmem_buf + tbuflen, | |
3771 | vm_map_page_mask(ipc_kernel_map)), | |
3772 | FALSE); | |
2d21ac55 A |
3773 | if (kr != 0) { |
3774 | printf("nfs_gss_mach_alloc_buffer: vm_map_unwire failed\n"); | |
3775 | return; | |
3776 | } | |
3777 | ||
3778 | kr = vm_map_copyin(ipc_kernel_map, (vm_map_address_t) kmem_buf, | |
3779 | (vm_map_size_t) buflen, TRUE, addr); | |
3780 | if (kr != 0) { | |
3781 | printf("nfs_gss_mach_alloc_buffer: vm_map_copyin failed\n"); | |
3782 | return; | |
3783 | } | |
2d21ac55 A |
3784 | } |
3785 | ||
3786 | /* | |
3787 | * Here we handle a token received from the gssd via an upcall. | |
3788 | * The received token resides in an allocate VM buffer. | |
3789 | * We copy the token out of this buffer to a chunk of malloc'ed | |
3790 | * memory of the right size, then de-allocate the VM buffer. | |
3791 | */ | |
3792 | static int | |
3793 | nfs_gss_mach_vmcopyout(vm_map_copy_t in, uint32_t len, u_char *out) | |
3794 | { | |
3795 | vm_map_offset_t map_data; | |
3796 | vm_offset_t data; | |
3797 | int error; | |
3798 | ||
3799 | error = vm_map_copyout(ipc_kernel_map, &map_data, in); | |
3800 | if (error) | |
3801 | return (error); | |
3802 | ||
3803 | data = CAST_DOWN(vm_offset_t, map_data); | |
3804 | bcopy((void *) data, out, len); | |
3805 | vm_deallocate(ipc_kernel_map, data, len); | |
3806 | ||
3807 | return (0); | |
3808 | } | |
3809 | ||
2d21ac55 A |
3810 | /* |
3811 | * Return the number of bytes in an mbuf chain. | |
3812 | */ | |
3813 | static int | |
3814 | nfs_gss_mchain_length(mbuf_t mhead) | |
3815 | { | |
3816 | mbuf_t mb; | |
3817 | int len = 0; | |
3818 | ||
3819 | for (mb = mhead; mb; mb = mbuf_next(mb)) | |
3820 | len += mbuf_len(mb); | |
3821 | ||
3822 | return (len); | |
3823 | } | |
3824 | ||
3825 | /* | |
3826 | * Append an args or results mbuf chain to the header chain | |
3827 | */ | |
3828 | static int | |
3829 | nfs_gss_append_chain(struct nfsm_chain *nmc, mbuf_t mc) | |
3830 | { | |
3831 | int error = 0; | |
3832 | mbuf_t mb, tail; | |
3833 | ||
3834 | /* Connect the mbuf chains */ | |
3835 | error = mbuf_setnext(nmc->nmc_mcur, mc); | |
3836 | if (error) | |
3837 | return (error); | |
3838 | ||
3839 | /* Find the last mbuf in the chain */ | |
3840 | tail = NULL; | |
3841 | for (mb = mc; mb; mb = mbuf_next(mb)) | |
3842 | tail = mb; | |
3843 | ||
3844 | nmc->nmc_mcur = tail; | |
3845 | nmc->nmc_ptr = (caddr_t) mbuf_data(tail) + mbuf_len(tail); | |
3846 | nmc->nmc_left = mbuf_trailingspace(tail); | |
3847 | ||
3848 | return (0); | |
3849 | } | |
3850 | ||
3851 | /* | |
3852 | * Convert an mbuf chain to an NFS mbuf chain | |
3853 | */ | |
3854 | static void | |
3855 | nfs_gss_nfsm_chain(struct nfsm_chain *nmc, mbuf_t mc) | |
3856 | { | |
3857 | mbuf_t mb, tail; | |
3858 | ||
3859 | /* Find the last mbuf in the chain */ | |
3860 | tail = NULL; | |
3861 | for (mb = mc; mb; mb = mbuf_next(mb)) | |
3862 | tail = mb; | |
3863 | ||
3864 | nmc->nmc_mhead = mc; | |
3865 | nmc->nmc_mcur = tail; | |
3866 | nmc->nmc_ptr = (caddr_t) mbuf_data(tail) + mbuf_len(tail); | |
3867 | nmc->nmc_left = mbuf_trailingspace(tail); | |
3868 | nmc->nmc_flags = 0; | |
3869 | } | |
3870 | ||
3871 | ||
b0d623f7 A |
3872 | |
3873 | #if 0 | |
3874 | #define DISPLAYLEN 16 | |
3875 | #define MAXDISPLAYLEN 256 | |
3876 | ||
3877 | static void | |
3878 | hexdump(const char *msg, void *data, size_t len) | |
3879 | { | |
3880 | size_t i, j; | |
3881 | u_char *d = data; | |
3882 | char *p, disbuf[3*DISPLAYLEN+1]; | |
3883 | ||
3884 | printf("NFS DEBUG %s len=%d:\n", msg, (uint32_t)len); | |
3885 | if (len > MAXDISPLAYLEN) | |
3886 | len = MAXDISPLAYLEN; | |
3887 | ||
3888 | for (i = 0; i < len; i += DISPLAYLEN) { | |
3889 | for (p = disbuf, j = 0; (j + i) < len && j < DISPLAYLEN; j++, p += 3) | |
3890 | snprintf(p, 4, "%02x ", d[i + j]); | |
3891 | printf("\t%s\n", disbuf); | |
2d21ac55 | 3892 | } |
2d21ac55 | 3893 | } |
b0d623f7 | 3894 | #endif |