2 * Copyright (c) 2007-2015 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
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.
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.
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.
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.
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.
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
77 #include <sys/param.h>
78 #include <sys/systm.h>
80 #include <sys/kauth.h>
81 #include <sys/kernel.h>
82 #include <sys/mount_internal.h>
83 #include <sys/vnode.h>
85 #include <sys/malloc.h>
86 #include <sys/kpi_mbuf.h>
87 #include <sys/ucred.h>
89 #include <kern/host.h>
90 #include <kern/task.h>
91 #include <libkern/libkern.h>
93 #include <mach/task.h>
94 #include <mach/host_special_ports.h>
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>
103 #include <nfs/rpcv2.h>
104 #include <nfs/nfsproto.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>
112 #include <mach_assert.h>
113 #include <kern/assert.h>
115 #define ASSERT(EX) assert(EX)
117 #define NFS_GSS_MACH_MAX_RETRIES 3
119 #define NFS_GSS_DBG(...) NFS_DBG(NFS_FAC_GSS, 7, ## __VA_ARGS__)
120 #define NFS_GSS_ISDBG (NFS_DEBUG_FACILITY & NFS_FAC_GSS)
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
;
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)
130 #endif /* NFSSERVER */
133 lck_grp_t
*nfs_gss_clnt_grp
;
134 #endif /* NFSCLIENT */
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};
141 static int nfs_gss_clnt_ctx_find(struct nfsreq
*);
142 static int nfs_gss_clnt_ctx_init(struct nfsreq
*, struct nfs_gss_clnt_ctx
*);
143 static int nfs_gss_clnt_ctx_init_retry(struct nfsreq
*, struct nfs_gss_clnt_ctx
*);
144 static int nfs_gss_clnt_ctx_callserver(struct nfsreq
*, struct nfs_gss_clnt_ctx
*);
145 static uint8_t *nfs_gss_clnt_svcname(struct nfsmount
*, gssd_nametype
*, uint32_t *);
146 static int nfs_gss_clnt_gssd_upcall(struct nfsreq
*, struct nfs_gss_clnt_ctx
*, uint32_t);
147 void nfs_gss_clnt_ctx_neg_cache_reap(struct nfsmount
*);
148 static void nfs_gss_clnt_ctx_clean(struct nfs_gss_clnt_ctx
*);
149 static int nfs_gss_clnt_ctx_copy(struct nfs_gss_clnt_ctx
*, struct nfs_gss_clnt_ctx
**);
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);
152 #endif /* NFSCLIENT */
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 */
162 static void host_release_special_port(mach_port_t
);
163 static mach_port_t
host_copy_special_port(mach_port_t
);
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
*);
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
);
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 */
179 * Initialization when NFS starts
185 nfs_gss_clnt_grp
= lck_grp_alloc_init("rpcsec_gss_clnt", LCK_GRP_ATTR_NULL
);
186 #endif /* NFSCLIENT */
189 nfs_gss_svc_grp
= lck_grp_alloc_init("rpcsec_gss_svc", LCK_GRP_ATTR_NULL
);
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
);
194 nfs_gss_svc_ctx_timer_call
= thread_call_allocate(nfs_gss_svc_ctx_timer
, NULL
);
195 #endif /* NFSSERVER */
199 * Common RPCSEC_GSS support routines
203 rpc_gss_prepend_32(mbuf_t
*mb
, uint32_t value
)
209 data
= mbuf_data(*mb
);
211 * If a wap token comes back and is not aligned
212 * get a new buffer (which should be aligned) to put the
215 if ((uintptr_t)data
& 0x3) {
218 error
= mbuf_get(MBUF_WAITOK
, MBUF_TYPE_DATA
, &nmb
);
221 mbuf_setnext(nmb
, *mb
);
225 error
= mbuf_prepend(mb
, sizeof(uint32_t), MBUF_WAITOK
);
229 data
= mbuf_data(*mb
);
230 *data
= txdr_unsigned(value
);
236 * Prepend the sequence number to the xdr encode argumen or result
237 * Sequence number is prepended in its own mbuf.
239 * On successful return mbp_head will point to the old mbuf chain
240 * prepended with a new mbuf that has the sequence number.
244 rpc_gss_data_create(mbuf_t
*mbp_head
, uint32_t seqnum
)
248 struct nfsm_chain nmc
;
249 struct nfsm_chain
*nmcp
= &nmc
;
252 error
= mbuf_get(MBUF_WAITOK
, MBUF_TYPE_DATA
, &mb
);
255 data
= mbuf_data(mb
);
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
);
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
);
272 mbuf_setnext(nmcp
->nmc_mcur
, *mbp_head
);
273 *mbp_head
= nmcp
->nmc_mhead
;
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
287 rpc_gss_integ_data_create(gss_ctx_id_t ctx
, mbuf_t
*mb_head
, uint32_t seqnum
, uint32_t *len
)
293 struct nfsm_chain nmc
;
295 /* Length of the argument or result */
296 length
= nfs_gss_mchain_length(*mb_head
);
299 error
= rpc_gss_data_create(mb_head
, seqnum
);
304 * length is the length of the rpc_gss_data
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
);
313 error
= rpc_gss_prepend_32(mb_head
, length
);
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
);
326 // printmbuf("rpc_gss_integ_data_create done", *mb_head, 0, 0);
327 assert(nmc
.nmc_mhead
== *mb_head
);
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
339 rpc_gss_priv_data_create(gss_ctx_id_t ctx
, mbuf_t
*mb_head
, uint32_t seqnum
, uint32_t *len
)
343 struct nfsm_chain nmc
;
347 error
= rpc_gss_data_create(mb_head
, seqnum
);
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
)
356 length
= nfs_gss_mchain_length(*mb_head
);
359 pad
= nfsm_pad(length
);
361 /* Prepend the opaque length of rep rpc_gss_priv_data */
362 error
= rpc_gss_prepend_32(mb_head
, length
);
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
);
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.
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.
391 rpc_gss_integ_data_restore(gss_ctx_id_t ctx __unused
, mbuf_t
*mb_head
, size_t len
)
393 mbuf_t mb
= *mb_head
;
394 mbuf_t tail
= NULL
, next
;
396 /* Chop of the opaque length and seq number */
397 mbuf_adj(mb
, 2 * NFSX_UNSIGNED
);
399 /* should only be one, ... but */
400 for (; mb
; mb
= next
) {
401 next
= mbuf_next(mb
);
402 if (mbuf_len(mb
) == 0)
409 for (; mb
&& len
; mb
= mbuf_next(mb
)) {
411 if (mbuf_len(mb
) <= len
)
418 mbuf_setnext(tail
, NULL
);
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.
429 * On successful return mb_head is our original xdr arg or result an
430 * the return value is 0. Otherise return an errno
433 rpc_gss_priv_data_restore(gss_ctx_id_t ctx
, mbuf_t
*mb_head
, size_t len
)
435 uint32_t major
, error
;
436 mbuf_t mb
= *mb_head
, next
;
439 gss_qop_t qop
= GSS_C_QOP_REVERSE
;
441 /* Chop of the opaque length */
442 mbuf_adj(mb
, NFSX_UNSIGNED
);
443 /* If we have padding, drop it */
444 plen
= nfsm_pad(len
);
448 for(length
= 0; length
< len
&& mb
; mb
= mbuf_next(mb
)) {
450 length
+= mbuf_len(mb
);
452 if ((length
!= len
) || (mb
== NULL
) || (tail
== NULL
))
456 mbuf_setnext(tail
, NULL
);
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
);
466 /* Drop the seqence number */
467 mbuf_adj(mb
, NFSX_UNSIGNED
);
468 assert(mbuf_len(mb
) == 0);
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)
484 * Find the context for a particular user.
486 * If the context doesn't already exist
487 * then create a new context for this user.
489 * Note that the code allows superuser (uid == 0)
490 * to adopt the context of another user.
492 * We'll match on the audit session ids, since those
493 * processes will have acccess to the same credential cache.
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)
499 #define SAFE_CAST_INTTYPE( type, intval ) \
500 ( (type)(intval)/(sizeof(type) < sizeof(intval) ? 0 : 1) )
503 nfs_cred_getasid2uid(kauth_cred_t cred
)
505 uid_t result
= SAFE_CAST_INTTYPE(uid_t
, kauth_cred_getasid(cred
));
513 nfs_gss_clnt_ctx_dump(struct nfsmount
*nmp
)
515 struct nfs_gss_clnt_ctx
*cp
;
517 lck_mtx_lock(&nmp
->nm_lock
);
518 NFS_GSS_DBG("Enter\n");
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
);
527 NFS_GSS_DBG("Exit\n");
528 lck_mtx_unlock(&nmp
->nm_lock
);
532 nfs_gss_clnt_ctx_name(struct nfsmount
*nmp
, struct nfs_gss_clnt_ctx
*cp
, char *buf
, int len
)
536 const char *server
= "";
538 if (nmp
&& nmp
->nm_mountp
)
539 server
= vfs_statfs(nmp
->nm_mountp
)->f_mntfromname
;
542 snprintf(buf
, len
, "[%s] NULL context", server
);
546 if (cp
->gss_clnt_principal
&& !cp
->gss_clnt_display
) {
547 np
= (char *)cp
->gss_clnt_principal
;
548 nlen
= cp
->gss_clnt_prinlen
;
550 np
= cp
->gss_clnt_display
;
551 nlen
= np
? strlen(cp
->gss_clnt_display
) : 0;
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] ");
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
));
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))
568 #define NFS_GSS_CLNT_CTX_DUMP(nmp) \
570 if (NFS_GSS_ISDBG && (NFS_DEBUG_FLAGS & 0x2)) \
571 nfs_gss_clnt_ctx_dump((nmp)); \
575 nfs_gss_clnt_ctx_cred_match(kauth_cred_t cred1
, kauth_cred_t cred2
)
577 if (kauth_cred_getasid(cred1
) == kauth_cred_getasid(cred2
))
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.
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.
595 nfs_gss_clnt_mnt_ref(struct nfsmount
*nmp
)
601 !(vfs_flags(nmp
->nm_mountp
) & MNT_AUTOMOUNTED
))
604 error
= VFS_ROOT(nmp
->nm_mountp
, &rvp
, NULL
);
612 * Unbusy the mout. See above comment,
616 nfs_gss_clnt_mnt_rele(struct nfsmount
*nmp
)
622 !(vfs_flags(nmp
->nm_mountp
) & MNT_AUTOMOUNTED
))
625 error
= VFS_ROOT(nmp
->nm_mountp
, &rvp
, NULL
);
632 int nfs_root_steals_ctx
= 1;
635 nfs_gss_clnt_ctx_find_principal(struct nfsreq
*req
, uint8_t *principal
, uint32_t plen
, uint32_t nt
)
637 struct nfsmount
*nmp
= req
->r_nmp
;
638 struct nfs_gss_clnt_ctx
*cp
;
642 char CTXBUF
[NFS_CTXBUFSZ
];
644 bzero(&treq
, sizeof (struct nfsreq
));
648 lck_mtx_lock(&nmp
->nm_lock
);
649 TAILQ_FOREACH(cp
, &nmp
->nm_gsscl
, gss_clnt_entries
) {
650 lck_mtx_lock(cp
->gss_clnt_mtx
);
651 if (cp
->gss_clnt_flags
& GSS_CTX_DESTROY
) {
652 NFS_GSS_DBG("Found destroyed context %s refcnt = %d continuing\n",
653 NFS_GSS_CTX(req
, cp
),
654 cp
->gss_clnt_refcnt
);
655 lck_mtx_unlock(cp
->gss_clnt_mtx
);
658 if (nfs_gss_clnt_ctx_cred_match(cp
->gss_clnt_cred
, req
->r_cred
)) {
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
);
665 * If we have a principal, but it does not match the current cred
666 * mark it for removal
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
);
682 if (cp
->gss_clnt_flags
& GSS_CTX_INVAL
) {
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.
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
);
695 if (cp
->gss_clnt_refcnt
) {
696 struct nfs_gss_clnt_ctx
*ncp
;
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.
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
);
705 error
= nfs_gss_clnt_ctx_copy(cp
, &ncp
);
706 lck_mtx_unlock(cp
->gss_clnt_mtx
);
708 lck_mtx_unlock(&nmp
->nm_lock
);
714 if (cp
->gss_clnt_nctime
)
716 lck_mtx_unlock(cp
->gss_clnt_mtx
);
717 TAILQ_REMOVE(&nmp
->nm_gsscl
, cp
, gss_clnt_entries
);
721 /* Found a valid context to return */
722 cp
->gss_clnt_refcnt
++;
724 lck_mtx_unlock(cp
->gss_clnt_mtx
);
725 lck_mtx_unlock(&nmp
->nm_lock
);
728 lck_mtx_unlock(cp
->gss_clnt_mtx
);
731 if (!cp
&& nfs_root_steals_ctx
&& principal
== NULL
&& kauth_cred_getuid(req
->r_cred
) == 0) {
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.
739 TAILQ_FOREACH(cp
, &nmp
->nm_gsscl
, gss_clnt_entries
) {
740 if (!(cp
->gss_clnt_flags
& (GSS_CTX_INVAL
|GSS_CTX_DESTROY
))) {
741 nfs_gss_clnt_ctx_ref(req
, cp
);
742 lck_mtx_unlock(&nmp
->nm_lock
);
743 NFS_GSS_DBG("Root stole context %s\n", NFS_GSS_CTX(req
, NULL
));
749 NFS_GSS_DBG("Context %s%sfound in Neg Cache @ %ld\n",
750 NFS_GSS_CTX(req
, cp
),
751 cp
== NULL
? " not " : "",
752 cp
== NULL
? 0L : cp
->gss_clnt_nctime
);
755 * Not found - create a new context
759 MALLOC(cp
, struct nfs_gss_clnt_ctx
*, sizeof(*cp
), M_TEMP
, M_WAITOK
|M_ZERO
);
761 lck_mtx_unlock(&nmp
->nm_lock
);
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
;
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
);
777 nfs_gss_clnt_ctx_clean(cp
);
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.
791 cp
->gss_clnt_flags
|= GSS_CTX_STICKY
;
795 cp
->gss_clnt_thread
= current_thread();
796 nfs_gss_clnt_ctx_ref(req
, cp
);
797 TAILQ_INSERT_HEAD(&nmp
->nm_gsscl
, cp
, gss_clnt_entries
);
798 lck_mtx_unlock(&nmp
->nm_lock
);
800 error
= nfs_gss_clnt_ctx_init_retry(req
, cp
); // Initialize new context
802 NFS_GSS_DBG("nfs_gss_clnt_ctx_init_retry returned %d for %s\n", error
, NFS_GSS_CTX(req
, cp
));
803 nfs_gss_clnt_ctx_unref(req
);
806 /* Remove any old matching contex that had a different principal */
807 nfs_gss_clnt_ctx_unref(&treq
);
813 nfs_gss_clnt_ctx_find(struct nfsreq
*req
)
815 return (nfs_gss_clnt_ctx_find_principal(req
, NULL
, 0, 0));
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
826 nfs_gss_clnt_cred_put(struct nfsreq
*req
, struct nfsm_chain
*nmc
, mbuf_t args
)
828 struct nfs_gss_clnt_ctx
*cp
;
832 int slpflag
, recordmark
= 0, offset
;
838 slpflag
|= (NMFLAG(req
->r_nmp
, INTR
) && req
->r_thread
&& !(req
->r_flags
& R_NOINTR
)) ? PCATCH
: 0;
839 recordmark
= (req
->r_nmp
->nm_sotype
== SOCK_STREAM
);
843 if (req
->r_gss_ctx
== NULL
) {
845 * Find the context for this user.
846 * If no context is found, one will
849 error
= nfs_gss_clnt_ctx_find(req
);
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
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
;
864 msleep(cp
, cp
->gss_clnt_mtx
, slpflag
| PDROP
, "ctxwait", NULL
);
866 if ((error
= nfs_sigintr(req
->r_nmp
, req
, req
->r_thread
, 0)))
868 nfs_gss_clnt_ctx_unref(req
);
871 lck_mtx_unlock(cp
->gss_clnt_mtx
);
873 if (cp
->gss_clnt_flags
& GSS_CTX_COMPLETE
) {
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
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
;
885 msleep(cp
, cp
->gss_clnt_mtx
, slpflag
| PDROP
, "seqwin", NULL
);
887 if ((error
= nfs_sigintr(req
->r_nmp
, req
, req
->r_thread
, 0))) {
890 lck_mtx_lock(cp
->gss_clnt_mtx
);
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
);
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
);
902 MALLOC(gsp
, struct gss_seq
*, sizeof(*gsp
), M_TEMP
, M_WAITOK
|M_ZERO
);
905 gsp
->gss_seqnum
= seqnum
;
906 SLIST_INSERT_HEAD(&req
->r_gss_seqlist
, gsp
, gss_seqnext
);
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
);
917 if (cp
->gss_clnt_handle_len
> 0) {
918 if (cp
->gss_clnt_handle
== NULL
)
920 nfsm_chain_add_opaque(error
, nmc
, cp
->gss_clnt_handle
, cp
->gss_clnt_handle_len
);
925 * Now add the verifier
927 if (cp
->gss_clnt_proc
== RPCSEC_GSS_INIT
||
928 cp
->gss_clnt_proc
== RPCSEC_GSS_CONTINUE_INIT
) {
930 * If the context is still being created
931 * then use a null verifier.
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
);
937 nfs_gss_append_chain(nmc
, args
);
941 offset
= recordmark
? NFSX_UNSIGNED
: 0; // record mark
942 nfsm_chain_build_done(error
, nmc
);
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
);
950 nfsm_chain_add_32(error
, nmc
, RPCSEC_GSS
); // flavor
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
);
954 nfsm_chain_build_done(error
, nmc
);
959 * Now we may have to compute integrity or encrypt the call args
960 * per RFC 2203 Section 5.3.2
962 switch (cp
->gss_clnt_service
) {
963 case RPCSEC_GSS_SVC_NONE
:
965 nfs_gss_append_chain(nmc
, args
);
967 case RPCSEC_GSS_SVC_INTEGRITY
:
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.
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
);
977 error
= rpc_gss_integ_data_create(cp
->gss_clnt_ctx_id
, &args
, seqnum
, &req
->r_gss_arglen
);
981 req
->r_gss_argoff
= nfsm_chain_offset(nmc
);
982 nfs_gss_append_chain(nmc
, args
);
984 case RPCSEC_GSS_SVC_PRIVACY
:
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.
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
);
994 error
= rpc_gss_priv_data_create(cp
->gss_clnt_ctx_id
, &args
, seqnum
, &req
->r_gss_arglen
);
998 req
->r_gss_argoff
= nfsm_chain_offset(nmc
);
999 nfs_gss_append_chain(nmc
, args
);
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
1017 nfs_gss_clnt_verf_get(
1019 struct nfsm_chain
*nmc
,
1022 uint32_t *accepted_statusp
)
1024 gss_buffer_desc cksum
;
1025 uint32_t seqnum
= 0;
1027 struct nfs_gss_clnt_ctx
*cp
= req
->r_gss_ctx
;
1028 struct nfsm_chain nmc_tmp
;
1029 struct gss_seq
*gsp
;
1030 uint32_t reslen
, offset
;
1032 mbuf_t results_mbuf
, prev_mbuf
, pad_mbuf
;
1036 *accepted_statusp
= 0;
1039 return (NFSERR_EAUTH
);
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.
1047 if (verftype
!= RPCSEC_GSS
) {
1048 if (verftype
!= RPCAUTH_NULL
)
1049 return (NFSERR_EAUTH
);
1050 if (cp
->gss_clnt_flags
& GSS_CTX_COMPLETE
)
1051 return (NFSERR_EAUTH
);
1053 nfsm_chain_adv(error
, nmc
, nfsm_rndup(verflen
));
1054 nfsm_chain_get_32(error
, nmc
, *accepted_statusp
);
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.
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
)
1072 cp
->gss_clnt_verflen
= verflen
;
1073 nfsm_chain_get_opaque(error
, nmc
, verflen
, cp
->gss_clnt_verf
);
1074 nfsm_chain_get_32(error
, nmc
, *accepted_statusp
);
1078 if (verflen
> KRB5_MAX_MIC_SIZE
)
1080 cksum
.length
= verflen
;
1081 MALLOC(cksum
.value
, void *, verflen
, M_TEMP
, M_WAITOK
);
1086 nfsm_chain_get_opaque(error
, nmc
, verflen
, cksum
.value
);
1088 FREE(cksum
.value
, M_TEMP
);
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.
1097 SLIST_FOREACH(gsp
, &req
->r_gss_seqlist
, gss_seqnext
) {
1098 gss_buffer_desc seqnum_buf
;
1099 uint32_t network_seqnum
= htonl(gsp
->gss_seqnum
);
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
)
1107 FREE(cksum
.value
, M_TEMP
);
1109 return (NFSERR_EAUTH
);
1112 * Get the RPC accepted status
1114 nfsm_chain_get_32(error
, nmc
, *accepted_statusp
);
1115 if (*accepted_statusp
!= RPC_SUCCESS
)
1119 * Now we may have to check integrity or decrypt the results
1120 * per RFC 2203 Section 5.3.2
1122 switch (cp
->gss_clnt_service
) {
1123 case RPCSEC_GSS_SVC_NONE
:
1126 case RPCSEC_GSS_SVC_INTEGRITY
:
1128 * Here's what we expect in the integrity results from RFC 2203:
1130 * - length of seq num + results (4 bytes)
1131 * - sequence number (4 bytes)
1132 * - results (variable bytes)
1133 * - length of checksum token
1134 * - checksum of seqnum + results
1137 nfsm_chain_get_32(error
, nmc
, reslen
); // length of results
1138 if (reslen
> NFS_MAXPACKET
) {
1143 /* Advance and fetch the mic */
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?
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
);
1162 * Get the sequence number prepended to the results
1163 * and compare it against the header.
1165 nfsm_chain_get_32(error
, nmc
, seqnum
);
1166 if (gsp
->gss_seqnum
!= seqnum
) {
1171 SLIST_FOREACH(gsp
, &req
->r_gss_seqlist
, gss_seqnext
) {
1172 if (seqnum
== gsp
->gss_seqnum
)
1181 case RPCSEC_GSS_SVC_PRIVACY
:
1183 * Here's what we expect in the privacy results:
1185 * opaque encodeing of the wrap token
1186 * - length of wrap token
1189 prev_mbuf
= nmc
->nmc_mcur
;
1190 nfsm_chain_get_32(error
, nmc
, reslen
); // length of results
1191 if (reslen
== 0 || reslen
> NFS_MAXPACKET
) {
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
);
1199 /* split out the wrap token */
1201 error
= gss_normalize_mbuf(nmc
->nmc_mcur
, offset
, &ressize
, &results_mbuf
, &pad_mbuf
, 0);
1206 assert(nfsm_pad(reslen
) == mbuf_len(pad_mbuf
));
1207 mbuf_free(pad_mbuf
);
1210 major
= gss_krb5_unwrap_mbuf((uint32_t *)&error
, cp
->gss_clnt_ctx_id
, &results_mbuf
, 0, ressize
, NULL
, NULL
);
1212 printf("%s unwraped failed %d\n", __func__
, error
);
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
);
1223 * Get the sequence number prepended to the results
1224 * and compare it against the header
1226 nfsm_chain_get_32(error
, nmc
, seqnum
);
1227 if (gsp
->gss_seqnum
!= seqnum
) {
1228 printf("%s bad seqnum\n", __func__
);
1233 SLIST_FOREACH(gsp
, &req
->r_gss_seqlist
, gss_seqnext
) {
1234 if (seqnum
== gsp
->gss_seqnum
)
1249 * An RPCSEC_GSS request with no integrity or privacy consists
1250 * of just the header mbufs followed by the arg mbufs.
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
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.
1262 nfs_gss_clnt_args_restore(struct nfsreq
*req
)
1264 struct nfs_gss_clnt_ctx
*cp
= req
->r_gss_ctx
;
1265 struct nfsm_chain mchain
, *nmc
= &mchain
;
1266 int error
= 0, merr
;
1269 return (NFSERR_EAUTH
);
1271 if ((cp
->gss_clnt_flags
& GSS_CTX_COMPLETE
) == 0)
1274 /* Nothing to restore for SVC_NONE */
1275 if (cp
->gss_clnt_service
== RPCSEC_GSS_SVC_NONE
)
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
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
);
1286 error
= rpc_gss_priv_data_restore(cp
->gss_clnt_ctx_id
, &req
->r_mrest
, req
->r_gss_arglen
);
1288 merr
= mbuf_setnext(nmc
->nmc_mcur
, req
->r_mrest
); /* Should always succeed */
1291 return (error
? error
: merr
);
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.
1305 nfs_gss_clnt_ctx_init(struct nfsreq
*req
, struct nfs_gss_clnt_ctx
*cp
)
1307 struct nfsmount
*nmp
= req
->r_nmp
;
1308 gss_buffer_desc cksum
, window
;
1309 uint32_t network_seqnum
;
1310 int client_complete
= 0;
1311 int server_complete
= 0;
1316 /* Initialize a new client context */
1318 if (cp
->gss_clnt_svcname
== NULL
) {
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
;
1326 cp
->gss_clnt_proc
= RPCSEC_GSS_INIT
;
1328 cp
->gss_clnt_service
=
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;
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.
1340 /* Upcall to the gss_init_sec_context in the gssd */
1341 error
= nfs_gss_clnt_gssd_upcall(req
, cp
, retrycnt
);
1345 if (cp
->gss_clnt_major
== GSS_S_COMPLETE
) {
1346 client_complete
= 1;
1347 NFS_GSS_DBG("Client complete\n");
1348 if (server_complete
)
1350 } else if (cp
->gss_clnt_major
!= GSS_S_CONTINUE_NEEDED
) {
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
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
);
1366 * Pass the token to the server.
1368 error
= nfs_gss_clnt_ctx_callserver(req
, cp
);
1370 if (error
== ENEEDAUTH
&&
1371 (cp
->gss_clnt_proc
== RPCSEC_GSS_INIT
||
1372 cp
->gss_clnt_proc
== RPCSEC_GSS_CONTINUE_INIT
)) {
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
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
);
1389 if (cp
->gss_clnt_major
== GSS_S_COMPLETE
) {
1390 NFS_GSS_DBG("Server complete\n");
1391 server_complete
= 1;
1392 if (client_complete
)
1394 } else if (cp
->gss_clnt_major
== GSS_S_CONTINUE_NEEDED
) {
1395 cp
->gss_clnt_proc
= RPCSEC_GSS_CONTINUE_INIT
;
1397 /* Server didn't like us. Try something else */
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
);
1405 * The context is apparently established successfully
1407 lck_mtx_lock(cp
->gss_clnt_mtx
);
1408 cp
->gss_clnt_flags
|= GSS_CTX_COMPLETE
;
1409 lck_mtx_unlock(cp
->gss_clnt_mtx
);
1410 cp
->gss_clnt_proc
= RPCSEC_GSS_DATA
;
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;
1419 FREE(cp
->gss_clnt_verf
, M_TEMP
);
1420 cp
->gss_clnt_verf
= NULL
;
1421 if (major
!= GSS_S_COMPLETE
) {
1422 printf("%s: could not verify window\n", __func__
);
1423 error
= NFSERR_EAUTH
;
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.
1433 cp
->gss_clnt_seqnum
= (random() & 0xffff) + cp
->gss_clnt_seqwin
;
1436 * Allocate a bitmap to keep track of which requests
1437 * are pending within the sequence number window.
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
)
1442 error
= NFSERR_EAUTH
;
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.
1450 if (error
== ENEEDAUTH
) {
1451 NFS_GSS_DBG("Returning ENEEDAUTH\n");
1456 * If there's an error, just mark it as invalid.
1457 * It will be removed when the reference count
1460 lck_mtx_lock(cp
->gss_clnt_mtx
);
1462 cp
->gss_clnt_flags
|= GSS_CTX_INVAL
;
1465 * Wake any threads waiting to use the context
1467 cp
->gss_clnt_thread
= NULL
;
1468 if (cp
->gss_clnt_flags
& GSS_NEEDCTX
) {
1469 cp
->gss_clnt_flags
&= ~GSS_NEEDCTX
;
1472 lck_mtx_unlock(cp
->gss_clnt_mtx
);
1474 NFS_GSS_DBG("Returning error = %d\n", error
);
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.
1487 nfs_gss_clnt_ctx_init_retry(struct nfsreq
*req
, struct nfs_gss_clnt_ctx
*cp
)
1489 struct nfsmount
*nmp
= req
->r_nmp
;
1494 int timeo
= NFS_TRYLATERDEL
;
1496 if (nfs_mount_gone(nmp
)) {
1501 /* For an "intr" mount allow a signal to interrupt the retries */
1502 slpflag
= (NMFLAG(nmp
, INTR
) && !(req
->r_flags
& R_NOINTR
)) ? PCATCH
: 0;
1504 while ((error
= nfs_gss_clnt_ctx_init(req
, cp
)) == ENEEDAUTH
) {
1506 waituntil
= now
.tv_sec
+ timeo
;
1507 while (now
.tv_sec
< waituntil
) {
1508 tsleep(NULL
, PSOCK
| slpflag
, "nfs_gss_clnt_ctx_init_retry", hz
);
1510 error
= nfs_sigintr(req
->r_nmp
, req
, current_thread(), 0);
1517 /* If it's a soft mount just give up after a while */
1518 if ((NMFLAG(nmp
, SOFT
) || (req
->r_flags
& R_SOFT
)) && (retries
> nmp
->nm_retry
)) {
1528 return 0; // success
1531 * Give up on this context
1533 lck_mtx_lock(cp
->gss_clnt_mtx
);
1534 cp
->gss_clnt_flags
|= GSS_CTX_INVAL
;
1537 * Wake any threads waiting to use the context
1539 cp
->gss_clnt_thread
= NULL
;
1540 if (cp
->gss_clnt_flags
& GSS_NEEDCTX
) {
1541 cp
->gss_clnt_flags
&= ~GSS_NEEDCTX
;
1544 lck_mtx_unlock(cp
->gss_clnt_mtx
);
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.
1556 nfs_gss_clnt_ctx_callserver(struct nfsreq
*req
, struct nfs_gss_clnt_ctx
*cp
)
1558 struct nfsm_chain nmreq
, nmrep
;
1559 int error
= 0, status
;
1560 uint32_t major
= cp
->gss_clnt_major
, minor
= cp
->gss_clnt_minor
;
1563 if (nfs_mount_gone(req
->r_nmp
))
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
);
1570 if (cp
->gss_clnt_tokenlen
> 0)
1571 nfsm_chain_add_opaque(error
, &nmreq
, cp
->gss_clnt_token
, cp
->gss_clnt_tokenlen
);
1572 nfsm_chain_build_done(error
, &nmreq
);
1576 /* Call the server */
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
);
1579 if (cp
->gss_clnt_token
!= NULL
) {
1580 FREE(cp
->gss_clnt_token
, M_TEMP
);
1581 cp
->gss_clnt_token
= NULL
;
1588 /* Get the server's reply */
1590 nfsm_chain_get_32(error
, &nmrep
, cp
->gss_clnt_handle_len
);
1591 if (cp
->gss_clnt_handle
!= NULL
) {
1592 FREE(cp
->gss_clnt_handle
, M_TEMP
);
1593 cp
->gss_clnt_handle
= NULL
;
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
) {
1601 nfsm_chain_get_opaque(error
, &nmrep
, cp
->gss_clnt_handle_len
, cp
->gss_clnt_handle
);
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
);
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
) {
1615 nfsm_chain_get_opaque(error
, &nmrep
, cp
->gss_clnt_tokenlen
, cp
->gss_clnt_token
);
1619 * Make sure any unusual errors are expanded and logged by gssd
1621 if (cp
->gss_clnt_major
!= GSS_S_COMPLETE
&&
1622 cp
->gss_clnt_major
!= GSS_S_CONTINUE_NEEDED
) {
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
);
1630 nfsm_chain_cleanup(&nmreq
);
1631 nfsm_chain_cleanup(&nmrep
);
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.
1649 nfs_gss_clnt_svcname(struct nfsmount
*nmp
, gssd_nametype
*nt
, uint32_t *len
)
1651 char *svcname
, *d
, *server
;
1654 if (nfs_mount_gone(nmp
))
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
)
1663 strlcpy(svcname
, nmp
->nm_sprinc
, *len
);
1665 return ((uint8_t *)svcname
);
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
);
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
));
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
);
1689 return ((uint8_t *)svcname
);
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.
1700 nfs_gss_clnt_get_upcall_port(kauth_cred_t credp
)
1702 mach_port_t gssd_host_port
, uc_port
= IPC_PORT_NULL
;
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
);
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
);
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
);
1724 host_release_special_port(gssd_host_port
);
1731 nfs_gss_clnt_log_error(struct nfsreq
*req
, struct nfs_gss_clnt_ctx
*cp
, uint32_t major
, uint32_t minor
)
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";
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
)))
1748 procn
= proc
->p_comm
;
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
)) {
1761 * Will let gssd do some logging in hopes that it can translate
1764 if (cp
->gss_clnt_minor
&& cp
->gss_clnt_minor
!= minor
) {
1765 (void) mach_gss_log_error(
1767 vfs_statfs(nmp
->nm_mountp
)->f_mntfromname
,
1768 kauth_cred_getuid(cp
->gss_clnt_cred
),
1771 cp
->gss_clnt_minor
);
1773 gss_error
= gss_error
? gss_error
: cp
->gss_clnt_major
;
1776 *%%% It would be really nice to get the terminal from the proc or auditinfo_addr struct and print that here.
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
));
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
));
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
);
1798 * Make an upcall to the gssd using Mach RPC
1799 * The upcall is made using a host special port.
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.
1805 nfs_gss_clnt_gssd_upcall(struct nfsreq
*req
, struct nfs_gss_clnt_ctx
*cp
, uint32_t retrycnt
)
1808 gssd_byte_buffer octx
= NULL
;
1809 uint32_t lucidlen
= 0;
1810 void *lucid_ctx_buffer
;
1812 vm_map_copy_t itoken
= NULL
;
1813 gssd_byte_buffer otoken
= NULL
;
1814 mach_msg_type_number_t otokenlen
;
1816 uint8_t *principal
= NULL
;
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
] = "";
1823 struct nfsmount
*nmp
= req
->r_nmp
;
1824 uint32_t major
= cp
->gss_clnt_major
, minor
= cp
->gss_clnt_minor
;
1825 uint32_t selected
= (uint32_t)-1;
1826 struct nfs_etype etype
;
1828 if (nmp
== NULL
|| vfs_isforce(nmp
->nm_mountp
) || (nmp
->nm_state
& (NFSSTA_FORCE
| NFSSTA_DEAD
)))
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
;
1839 NFS_GSS_DBG("Retrycnt = %d nm_etype.count = %d\n", retrycnt
, nmp
->nm_etype
.count
);
1840 if (retrycnt
>= nmp
->nm_etype
.count
)
1843 /* Copy the mount etypes to an order set of etypes to try */
1844 etype
= nmp
->nm_etype
;
1847 * If we've already selected an etype, lets put that first in our
1848 * array of etypes to try, since overwhelmingly, that is likely
1849 * to be the etype we want.
1851 if (etype
.selected
< etype
.count
) {
1852 etype
.etypes
[0] = nmp
->nm_etype
.etypes
[etype
.selected
];
1853 for (uint32_t i
= 0; i
< etype
.selected
; i
++)
1854 etype
.etypes
[i
+1] = nmp
->nm_etype
.etypes
[i
];
1855 for (uint32_t i
= etype
.selected
+ 1; i
< etype
.count
; i
++)
1856 etype
.etypes
[i
] = nmp
->nm_etype
.etypes
[i
];
1859 /* Remove the ones we've already have tried */
1860 for (uint32_t i
= retrycnt
; i
< etype
.count
; i
++)
1861 etype
.etypes
[i
- retrycnt
] = etype
.etypes
[i
];
1862 etype
.count
= etype
.count
- retrycnt
;
1864 NFS_GSS_DBG("etype count = %d preferred etype = %d\n", etype
.count
, etype
.etypes
[0]);
1867 * NFS currently only supports default principals or
1868 * principals based on the uid of the caller, unless
1869 * the principal to use for the mounting cred was specified
1870 * in the mount argmuments. If the realm to use was specified
1871 * then will send that up as the principal since the realm is
1872 * preceed by an "@" gssd that will try and select the default
1873 * principal for that realm.
1876 if (cp
->gss_clnt_principal
&& cp
->gss_clnt_prinlen
) {
1877 principal
= cp
->gss_clnt_principal
;
1878 plen
= cp
->gss_clnt_prinlen
;
1879 nt
= cp
->gss_clnt_prinnt
;
1880 } else if (nmp
->nm_principal
&& IS_VALID_CRED(nmp
->nm_mcred
) && req
->r_cred
== nmp
->nm_mcred
) {
1881 plen
= (uint32_t)strlen(nmp
->nm_principal
);
1882 MALLOC(principal
, uint8_t *, plen
, M_TEMP
, M_WAITOK
| M_ZERO
);
1883 if (principal
== NULL
)
1885 bcopy(nmp
->nm_principal
, principal
, plen
);
1886 cp
->gss_clnt_prinnt
= nt
= GSSD_USER
;
1888 else if (nmp
->nm_realm
) {
1889 plen
= (uint32_t)strlen(nmp
->nm_realm
);
1890 principal
= (uint8_t *)nmp
->nm_realm
;
1894 if (!IPC_PORT_VALID(cp
->gss_clnt_mport
)) {
1895 cp
->gss_clnt_mport
= nfs_gss_clnt_get_upcall_port(req
->r_cred
);
1896 if (cp
->gss_clnt_mport
== IPC_PORT_NULL
)
1901 nfs_gss_mach_alloc_buffer(principal
, plen
, &pname
);
1902 if (cp
->gss_clnt_svcnamlen
)
1903 nfs_gss_mach_alloc_buffer(cp
->gss_clnt_svcname
, cp
->gss_clnt_svcnamlen
, &svcname
);
1904 if (cp
->gss_clnt_tokenlen
)
1905 nfs_gss_mach_alloc_buffer(cp
->gss_clnt_token
, cp
->gss_clnt_tokenlen
, &itoken
);
1907 /* Always want to export the lucid context */
1908 cp
->gss_clnt_gssd_flags
|= GSSD_LUCID_CONTEXT
;
1911 kr
= mach_gss_init_sec_context_v3(
1914 (gssd_byte_buffer
) itoken
, (mach_msg_type_number_t
) cp
->gss_clnt_tokenlen
,
1915 kauth_cred_getuid(cp
->gss_clnt_cred
),
1917 (gssd_byte_buffer
)pname
, (mach_msg_type_number_t
) plen
,
1919 (gssd_byte_buffer
)svcname
, (mach_msg_type_number_t
) cp
->gss_clnt_svcnamlen
,
1921 (gssd_etype_list
)etype
.etypes
, (mach_msg_type_number_t
)etype
.count
,
1922 &cp
->gss_clnt_gssd_flags
,
1923 &cp
->gss_clnt_context
,
1924 &cp
->gss_clnt_cred_handle
,
1926 &octx
, (mach_msg_type_number_t
*) &lucidlen
,
1927 &otoken
, &otokenlen
,
1928 cp
->gss_clnt_display
? NULL
: display_name
,
1929 &cp
->gss_clnt_major
,
1930 &cp
->gss_clnt_minor
);
1932 /* Clear the RESTART flag */
1933 cp
->gss_clnt_gssd_flags
&= ~GSSD_RESTART
;
1934 if (cp
->gss_clnt_major
!= GSS_S_CONTINUE_NEEDED
) {
1935 /* We're done with the gssd handles */
1936 cp
->gss_clnt_context
= 0;
1937 cp
->gss_clnt_cred_handle
= 0;
1940 if (kr
!= KERN_SUCCESS
) {
1941 printf("nfs_gss_clnt_gssd_upcall: mach_gss_init_sec_context failed: %x (%d)\n", kr
, kr
);
1942 if (kr
== MIG_SERVER_DIED
&& cp
->gss_clnt_cred_handle
== 0 &&
1943 retry_cnt
++ < NFS_GSS_MACH_MAX_RETRIES
&&
1944 !vfs_isforce(nmp
->nm_mountp
) && (nmp
->nm_state
& (NFSSTA_FORCE
| NFSSTA_DEAD
)) == 0) {
1946 nfs_gss_mach_alloc_buffer(principal
, plen
, &pname
);
1947 if (cp
->gss_clnt_svcnamlen
)
1948 nfs_gss_mach_alloc_buffer(cp
->gss_clnt_svcname
, cp
->gss_clnt_svcnamlen
, &svcname
);
1949 if (cp
->gss_clnt_tokenlen
> 0)
1950 nfs_gss_mach_alloc_buffer(cp
->gss_clnt_token
, cp
->gss_clnt_tokenlen
, &itoken
);
1954 host_release_special_port(cp
->gss_clnt_mport
);
1955 cp
->gss_clnt_mport
= IPC_PORT_NULL
;
1959 if (cp
->gss_clnt_display
== NULL
&& *display_name
!= '\0') {
1960 int dlen
= strnlen(display_name
, MAX_DISPLAY_STR
) + 1; /* Add extra byte to include '\0' */
1962 if (dlen
< MAX_DISPLAY_STR
) {
1963 MALLOC(cp
->gss_clnt_display
, char *, dlen
, M_TEMP
, M_WAITOK
);
1964 if (cp
->gss_clnt_display
== NULL
)
1966 bcopy(display_name
, cp
->gss_clnt_display
, dlen
);
1973 * Make sure any unusual errors are expanded and logged by gssd
1975 * XXXX, we need to rethink this and just have gssd return a string for the major and minor codes.
1977 if (cp
->gss_clnt_major
!= GSS_S_COMPLETE
&&
1978 cp
->gss_clnt_major
!= GSS_S_CONTINUE_NEEDED
) {
1979 NFS_GSS_DBG("Up call returned error\n");
1980 nfs_gss_clnt_log_error(req
, cp
, major
, minor
);
1984 if (lucidlen
> MAX_LUCIDLEN
) {
1985 printf("nfs_gss_clnt_gssd_upcall: bad context length (%d)\n", lucidlen
);
1986 vm_map_copy_discard((vm_map_copy_t
) octx
);
1987 vm_map_copy_discard((vm_map_copy_t
) otoken
);
1990 MALLOC(lucid_ctx_buffer
, void *, lucidlen
, M_TEMP
, M_WAITOK
| M_ZERO
);
1991 error
= nfs_gss_mach_vmcopyout((vm_map_copy_t
) octx
, lucidlen
, lucid_ctx_buffer
);
1993 vm_map_copy_discard((vm_map_copy_t
) otoken
);
1997 if (cp
->gss_clnt_ctx_id
)
1998 gss_krb5_destroy_context(cp
->gss_clnt_ctx_id
);
1999 cp
->gss_clnt_ctx_id
= gss_krb5_make_context(lucid_ctx_buffer
, lucidlen
);
2000 if (cp
->gss_clnt_ctx_id
== NULL
) {
2001 printf("Failed to make context from lucid_ctx_buffer\n");
2004 for (uint32_t i
= 0; i
< nmp
->nm_etype
.count
; i
++) {
2005 if (nmp
->nm_etype
.etypes
[i
] == cp
->gss_clnt_ctx_id
->gss_cryptor
.etype
) {
2012 /* Free context token used as input */
2013 if (cp
->gss_clnt_token
)
2014 FREE(cp
->gss_clnt_token
, M_TEMP
);
2015 cp
->gss_clnt_token
= NULL
;
2016 cp
->gss_clnt_tokenlen
= 0;
2018 if (otokenlen
> 0) {
2019 /* Set context token to gss output token */
2020 MALLOC(cp
->gss_clnt_token
, u_char
*, otokenlen
, M_TEMP
, M_WAITOK
);
2021 if (cp
->gss_clnt_token
== NULL
) {
2022 printf("nfs_gss_clnt_gssd_upcall: could not allocate %d bytes\n", otokenlen
);
2023 vm_map_copy_discard((vm_map_copy_t
) otoken
);
2026 error
= nfs_gss_mach_vmcopyout((vm_map_copy_t
) otoken
, otokenlen
, cp
->gss_clnt_token
);
2028 printf("Could not copyout gss token\n");
2029 FREE(cp
->gss_clnt_token
, M_TEMP
);
2030 cp
->gss_clnt_token
= NULL
;
2031 return (NFSERR_EAUTH
);
2033 cp
->gss_clnt_tokenlen
= otokenlen
;
2036 if (selected
!= (uint32_t)-1) {
2037 nmp
->nm_etype
.selected
= selected
;
2038 NFS_GSS_DBG("etype selected = %d\n", nmp
->nm_etype
.etypes
[selected
]);
2040 NFS_GSS_DBG("Up call succeeded major = %d\n", cp
->gss_clnt_major
);
2044 if (cp
->gss_clnt_token
)
2045 FREE(cp
->gss_clnt_token
, M_TEMP
);
2046 cp
->gss_clnt_token
= NULL
;
2047 cp
->gss_clnt_tokenlen
= 0;
2049 NFS_GSS_DBG("Up call returned NFSERR_EAUTH");
2050 return (NFSERR_EAUTH
);
2054 * Invoked at the completion of an RPC call that uses an RPCSEC_GSS
2055 * credential. The sequence number window that the server returns
2056 * at context setup indicates the maximum number of client calls that
2057 * can be outstanding on a context. The client maintains a bitmap that
2058 * represents the server's window. Each pending request has a bit set
2059 * in the window bitmap. When a reply comes in or times out, we reset
2060 * the bit in the bitmap and if there are any other threads waiting for
2061 * a context slot we notify the waiting thread(s).
2063 * Note that if a request is retransmitted, it will have a single XID
2064 * but it may be associated with multiple sequence numbers. So we
2065 * may have to reset multiple sequence number bits in the window bitmap.
2068 nfs_gss_clnt_rpcdone(struct nfsreq
*req
)
2070 struct nfs_gss_clnt_ctx
*cp
= req
->r_gss_ctx
;
2071 struct gss_seq
*gsp
, *ngsp
;
2074 if (cp
== NULL
|| !(cp
->gss_clnt_flags
& GSS_CTX_COMPLETE
))
2075 return; // no context - don't bother
2077 * Reset the bit for this request in the
2078 * sequence number window to indicate it's done.
2079 * We do this even if the request timed out.
2081 lck_mtx_lock(cp
->gss_clnt_mtx
);
2082 gsp
= SLIST_FIRST(&req
->r_gss_seqlist
);
2083 if (gsp
&& gsp
->gss_seqnum
> (cp
->gss_clnt_seqnum
- cp
->gss_clnt_seqwin
))
2084 win_resetbit(cp
->gss_clnt_seqbits
,
2085 gsp
->gss_seqnum
% cp
->gss_clnt_seqwin
);
2088 * Limit the seqnum list to GSS_CLNT_SEQLISTMAX entries
2090 SLIST_FOREACH_SAFE(gsp
, &req
->r_gss_seqlist
, gss_seqnext
, ngsp
) {
2091 if (++i
> GSS_CLNT_SEQLISTMAX
) {
2092 SLIST_REMOVE(&req
->r_gss_seqlist
, gsp
, gss_seq
, gss_seqnext
);
2098 * If there's a thread waiting for
2099 * the window to advance, wake it up.
2101 if (cp
->gss_clnt_flags
& GSS_NEEDSEQ
) {
2102 cp
->gss_clnt_flags
&= ~GSS_NEEDSEQ
;
2105 lck_mtx_unlock(cp
->gss_clnt_mtx
);
2109 * Create a reference to a context from a request
2110 * and bump the reference count
2113 nfs_gss_clnt_ctx_ref(struct nfsreq
*req
, struct nfs_gss_clnt_ctx
*cp
)
2115 req
->r_gss_ctx
= cp
;
2117 lck_mtx_lock(cp
->gss_clnt_mtx
);
2118 cp
->gss_clnt_refcnt
++;
2119 lck_mtx_unlock(cp
->gss_clnt_mtx
);
2123 * Remove a context reference from a request
2124 * If the reference count drops to zero, and the
2125 * context is invalid, destroy the context
2128 nfs_gss_clnt_ctx_unref(struct nfsreq
*req
)
2130 struct nfsmount
*nmp
= req
->r_nmp
;
2131 struct nfs_gss_clnt_ctx
*cp
= req
->r_gss_ctx
;
2132 int on_neg_cache
= 0;
2136 char CTXBUF
[NFS_CTXBUFSZ
];
2141 req
->r_gss_ctx
= NULL
;
2143 lck_mtx_lock(cp
->gss_clnt_mtx
);
2144 if (--cp
->gss_clnt_refcnt
< 0)
2145 panic("Over release of gss context!\n");
2147 if (cp
->gss_clnt_refcnt
== 0) {
2148 if ((cp
->gss_clnt_flags
& GSS_CTX_INVAL
) &&
2149 cp
->gss_clnt_ctx_id
) {
2150 gss_krb5_destroy_context(cp
->gss_clnt_ctx_id
);
2151 cp
->gss_clnt_ctx_id
= NULL
;
2153 if (cp
->gss_clnt_flags
& GSS_CTX_DESTROY
) {
2155 if (cp
->gss_clnt_flags
& GSS_CTX_STICKY
)
2156 nfs_gss_clnt_mnt_rele(nmp
);
2157 if (cp
->gss_clnt_nctime
)
2161 if (!destroy
&& cp
->gss_clnt_nctime
== 0 &&
2162 (cp
->gss_clnt_flags
& GSS_CTX_INVAL
)) {
2164 cp
->gss_clnt_nctime
= now
.tv_sec
;
2167 lck_mtx_unlock(cp
->gss_clnt_mtx
);
2169 NFS_GSS_DBG("Destroying context %s\n", NFS_GSS_CTX(req
, cp
));
2171 lck_mtx_lock(&nmp
->nm_lock
);
2172 if (cp
->gss_clnt_entries
.tqe_next
!= NFSNOLIST
) {
2173 TAILQ_REMOVE(&nmp
->nm_gsscl
, cp
, gss_clnt_entries
);
2176 nmp
->nm_ncentries
--;
2178 lck_mtx_unlock(&nmp
->nm_lock
);
2180 nfs_gss_clnt_ctx_destroy(cp
);
2181 } else if (neg_cache
) {
2182 NFS_GSS_DBG("Entering context %s into negative cache\n", NFS_GSS_CTX(req
, cp
));
2184 lck_mtx_lock(&nmp
->nm_lock
);
2185 nmp
->nm_ncentries
++;
2186 nfs_gss_clnt_ctx_neg_cache_reap(nmp
);
2187 lck_mtx_unlock(&nmp
->nm_lock
);
2190 NFS_GSS_CLNT_CTX_DUMP(nmp
);
2194 * Try and reap any old negative cache entries.
2198 nfs_gss_clnt_ctx_neg_cache_reap(struct nfsmount
*nmp
)
2200 struct nfs_gss_clnt_ctx
*cp
, *tcp
;
2204 /* Try and reap old, unreferenced, expired contexts */
2207 NFS_GSS_DBG("Reaping contexts ncentries = %d\n", nmp
->nm_ncentries
);
2209 TAILQ_FOREACH_SAFE(cp
, &nmp
->nm_gsscl
, gss_clnt_entries
, tcp
) {
2212 /* Don't reap STICKY contexts */
2213 if ((cp
->gss_clnt_flags
& GSS_CTX_STICKY
) ||
2214 !(cp
->gss_clnt_flags
& GSS_CTX_INVAL
))
2216 /* Keep up to GSS_MAX_NEG_CACHE_ENTRIES */
2217 if (nmp
->nm_ncentries
<= GSS_MAX_NEG_CACHE_ENTRIES
)
2219 /* Contexts too young */
2220 if (cp
->gss_clnt_nctime
+ GSS_NEG_CACHE_TO
>= now
.tv_sec
)
2222 /* Not referenced, remove it. */
2223 lck_mtx_lock(cp
->gss_clnt_mtx
);
2224 if (cp
->gss_clnt_refcnt
== 0) {
2225 cp
->gss_clnt_flags
|= GSS_CTX_DESTROY
;
2228 lck_mtx_unlock(cp
->gss_clnt_mtx
);
2230 TAILQ_REMOVE(&nmp
->nm_gsscl
, cp
, gss_clnt_entries
);
2231 nmp
->nm_ncentries
++;
2233 nfs_gss_clnt_ctx_destroy(cp
);
2236 NFS_GSS_DBG("Reaped %d contexts ncentries = %d\n", reaped
, nmp
->nm_ncentries
);
2240 * Clean a context to be cached
2243 nfs_gss_clnt_ctx_clean(struct nfs_gss_clnt_ctx
*cp
)
2245 /* Preserve gss_clnt_mtx */
2246 assert(cp
->gss_clnt_thread
== NULL
); /* Will be set to this thread */
2247 /* gss_clnt_entries we should not be on any list at this point */
2248 cp
->gss_clnt_flags
= 0;
2249 /* gss_clnt_refcnt should be zero */
2250 assert(cp
->gss_clnt_refcnt
== 0);
2252 * We are who we are preserve:
2254 * gss_clnt_principal
2259 /* gss_clnt_proc will be set in nfs_gss_clnt_ctx_init */
2260 cp
->gss_clnt_seqnum
= 0;
2261 /* Preserve gss_clnt_service, we're not changing flavors */
2262 if (cp
->gss_clnt_handle
) {
2263 FREE(cp
->gss_clnt_handle
, M_TEMP
);
2264 cp
->gss_clnt_handle
= NULL
;
2266 cp
->gss_clnt_handle_len
= 0;
2267 cp
->gss_clnt_nctime
= 0;
2268 cp
->gss_clnt_seqwin
= 0;
2269 if (cp
->gss_clnt_seqbits
) {
2270 FREE(cp
->gss_clnt_seqbits
, M_TEMP
);
2271 cp
->gss_clnt_seqbits
= NULL
;
2273 /* Preserve gss_clnt_mport. Still talking to the same gssd */
2274 if (cp
->gss_clnt_verf
) {
2275 FREE(cp
->gss_clnt_verf
, M_TEMP
);
2276 cp
->gss_clnt_verf
= NULL
;
2278 /* Service name might change on failover, so reset it */
2279 if (cp
->gss_clnt_svcname
) {
2280 FREE(cp
->gss_clnt_svcname
, M_TEMP
);
2281 cp
->gss_clnt_svcname
= NULL
;
2282 cp
->gss_clnt_svcnt
= 0;
2284 cp
->gss_clnt_svcnamlen
= 0;
2285 cp
->gss_clnt_cred_handle
= 0;
2286 cp
->gss_clnt_context
= 0;
2287 if (cp
->gss_clnt_token
) {
2288 FREE(cp
->gss_clnt_token
, M_TEMP
);
2289 cp
->gss_clnt_token
= NULL
;
2291 cp
->gss_clnt_tokenlen
= 0;
2292 /* XXX gss_clnt_ctx_id ??? */
2295 * gss_clnt_gssd_flags
2303 * Copy a source context to a new context. This is used to create a new context
2304 * with the identity of the old context for renewal. The old context is invalid
2305 * at this point but may have reference still to it, so it is not safe to use that
2309 nfs_gss_clnt_ctx_copy(struct nfs_gss_clnt_ctx
*scp
, struct nfs_gss_clnt_ctx
**dcpp
)
2311 struct nfs_gss_clnt_ctx
*dcp
;
2313 *dcpp
= (struct nfs_gss_clnt_ctx
*)NULL
;
2314 MALLOC(dcp
, struct nfs_gss_clnt_ctx
*, sizeof (struct nfs_gss_clnt_ctx
), M_TEMP
, M_WAITOK
);
2317 bzero(dcp
, sizeof (struct nfs_gss_clnt_ctx
));
2318 dcp
->gss_clnt_mtx
= lck_mtx_alloc_init(nfs_gss_clnt_grp
, LCK_ATTR_NULL
);
2319 dcp
->gss_clnt_cred
= scp
->gss_clnt_cred
;
2320 kauth_cred_ref(dcp
->gss_clnt_cred
);
2321 dcp
->gss_clnt_prinlen
= scp
->gss_clnt_prinlen
;
2322 dcp
->gss_clnt_prinnt
= scp
->gss_clnt_prinnt
;
2323 if (scp
->gss_clnt_principal
) {
2324 MALLOC(dcp
->gss_clnt_principal
, uint8_t *, dcp
->gss_clnt_prinlen
, M_TEMP
, M_WAITOK
| M_ZERO
);
2325 if (dcp
->gss_clnt_principal
== NULL
) {
2329 bcopy(scp
->gss_clnt_principal
, dcp
->gss_clnt_principal
, dcp
->gss_clnt_prinlen
);
2331 /* Note we don't preserve the display name, that will be set by a successful up call */
2332 dcp
->gss_clnt_service
= scp
->gss_clnt_service
;
2333 dcp
->gss_clnt_mport
= host_copy_special_port(scp
->gss_clnt_mport
);
2334 dcp
->gss_clnt_ctx_id
= NULL
; /* Will be set from successful upcall */
2335 dcp
->gss_clnt_gssd_flags
= scp
->gss_clnt_gssd_flags
;
2336 dcp
->gss_clnt_major
= scp
->gss_clnt_major
;
2337 dcp
->gss_clnt_minor
= scp
->gss_clnt_minor
;
2338 dcp
->gss_clnt_ptime
= scp
->gss_clnt_ptime
;
2349 nfs_gss_clnt_ctx_destroy(struct nfs_gss_clnt_ctx
*cp
)
2351 NFS_GSS_DBG("Destroying context %d/%d\n",
2352 kauth_cred_getasid(cp
->gss_clnt_cred
),
2353 kauth_cred_getauid(cp
->gss_clnt_cred
));
2355 host_release_special_port(cp
->gss_clnt_mport
);
2356 cp
->gss_clnt_mport
= IPC_PORT_NULL
;
2358 if (cp
->gss_clnt_mtx
) {
2359 lck_mtx_destroy(cp
->gss_clnt_mtx
, nfs_gss_clnt_grp
);
2360 cp
->gss_clnt_mtx
= (lck_mtx_t
*)NULL
;
2362 if (IS_VALID_CRED(cp
->gss_clnt_cred
))
2363 kauth_cred_unref(&cp
->gss_clnt_cred
);
2364 cp
->gss_clnt_entries
.tqe_next
= NFSNOLIST
;
2365 cp
->gss_clnt_entries
.tqe_prev
= NFSNOLIST
;
2366 if (cp
->gss_clnt_principal
) {
2367 FREE(cp
->gss_clnt_principal
, M_TEMP
);
2368 cp
->gss_clnt_principal
= NULL
;
2370 if (cp
->gss_clnt_display
) {
2371 FREE(cp
->gss_clnt_display
, M_TEMP
);
2372 cp
->gss_clnt_display
= NULL
;
2374 if (cp
->gss_clnt_ctx_id
) {
2375 gss_krb5_destroy_context(cp
->gss_clnt_ctx_id
);
2376 cp
->gss_clnt_ctx_id
= NULL
;
2379 nfs_gss_clnt_ctx_clean(cp
);
2385 * The context for a user is invalid.
2386 * Mark the context as invalid, then
2387 * create a new context.
2390 nfs_gss_clnt_ctx_renew(struct nfsreq
*req
)
2392 struct nfs_gss_clnt_ctx
*cp
= req
->r_gss_ctx
;
2393 struct nfs_gss_clnt_ctx
*ncp
;
2394 struct nfsmount
*nmp
;
2396 char CTXBUF
[NFS_CTXBUFSZ
];
2401 if (req
->r_nmp
== NULL
)
2405 lck_mtx_lock(cp
->gss_clnt_mtx
);
2406 if (cp
->gss_clnt_flags
& GSS_CTX_INVAL
) {
2407 lck_mtx_unlock(cp
->gss_clnt_mtx
);
2408 nfs_gss_clnt_ctx_unref(req
);
2409 return (0); // already being renewed
2412 cp
->gss_clnt_flags
|= (GSS_CTX_INVAL
| GSS_CTX_DESTROY
);
2414 if (cp
->gss_clnt_flags
& (GSS_NEEDCTX
| GSS_NEEDSEQ
)) {
2415 cp
->gss_clnt_flags
&= ~GSS_NEEDSEQ
;
2418 lck_mtx_unlock(cp
->gss_clnt_mtx
);
2420 if (cp
->gss_clnt_proc
== RPCSEC_GSS_DESTROY
)
2421 return (EACCES
); /* Destroying a context is best effort. Don't renew. */
2423 * If we're setting up a context let nfs_gss_clnt_ctx_init know this is not working
2424 * and to try some other etype.
2426 if (cp
->gss_clnt_proc
!= RPCSEC_GSS_DATA
)
2428 error
= nfs_gss_clnt_ctx_copy(cp
, &ncp
);
2429 NFS_GSS_DBG("Renewing context %s\n", NFS_GSS_CTX(req
, ncp
));
2430 nfs_gss_clnt_ctx_unref(req
);
2434 lck_mtx_lock(&nmp
->nm_lock
);
2436 * Note we don't bother taking the new context mutex as we're
2437 * not findable at the moment.
2439 ncp
->gss_clnt_thread
= current_thread();
2440 nfs_gss_clnt_ctx_ref(req
, ncp
);
2441 TAILQ_INSERT_HEAD(&nmp
->nm_gsscl
, ncp
, gss_clnt_entries
);
2442 lck_mtx_unlock(&nmp
->nm_lock
);
2444 error
= nfs_gss_clnt_ctx_init_retry(req
, ncp
); // Initialize new context
2446 nfs_gss_clnt_ctx_unref(req
);
2453 * Destroy all the contexts associated with a mount.
2454 * The contexts are also destroyed by the server.
2457 nfs_gss_clnt_ctx_unmount(struct nfsmount
*nmp
)
2459 struct nfs_gss_clnt_ctx
*cp
;
2460 struct nfsm_chain nmreq
, nmrep
;
2469 lck_mtx_lock(&nmp
->nm_lock
);
2470 while((cp
= TAILQ_FIRST(&nmp
->nm_gsscl
))) {
2471 TAILQ_REMOVE(&nmp
->nm_gsscl
, cp
, gss_clnt_entries
);
2472 cp
->gss_clnt_entries
.tqe_next
= NFSNOLIST
;
2473 lck_mtx_lock(cp
->gss_clnt_mtx
);
2474 if (cp
->gss_clnt_flags
& GSS_CTX_DESTROY
) {
2475 lck_mtx_unlock(cp
->gss_clnt_mtx
);
2478 cp
->gss_clnt_refcnt
++;
2479 lck_mtx_unlock(cp
->gss_clnt_mtx
);
2482 lck_mtx_unlock(&nmp
->nm_lock
);
2484 * Tell the server to destroy its context.
2485 * But don't bother if it's a forced unmount.
2487 if (!nfs_mount_gone(nmp
) &&
2488 (cp
->gss_clnt_flags
& (GSS_CTX_INVAL
| GSS_CTX_DESTROY
| GSS_CTX_COMPLETE
)) == GSS_CTX_COMPLETE
) {
2489 cp
->gss_clnt_proc
= RPCSEC_GSS_DESTROY
;
2492 nfsm_chain_null(&nmreq
);
2493 nfsm_chain_null(&nmrep
);
2494 nfsm_chain_build_alloc_init(error
, &nmreq
, 0);
2495 nfsm_chain_build_done(error
, &nmreq
);
2497 nfs_request_gss(nmp
->nm_mountp
, &nmreq
,
2498 current_thread(), cp
->gss_clnt_cred
, 0, cp
, &nmrep
, &status
);
2499 nfsm_chain_cleanup(&nmreq
);
2500 nfsm_chain_cleanup(&nmrep
);
2504 * Mark the context invalid then drop
2505 * the reference to remove it if its
2508 lck_mtx_lock(cp
->gss_clnt_mtx
);
2509 cp
->gss_clnt_flags
|= (GSS_CTX_INVAL
| GSS_CTX_DESTROY
);
2510 lck_mtx_unlock(cp
->gss_clnt_mtx
);
2511 nfs_gss_clnt_ctx_unref(&req
);
2512 lck_mtx_lock(&nmp
->nm_lock
);
2514 lck_mtx_unlock(&nmp
->nm_lock
);
2515 assert(TAILQ_EMPTY(&nmp
->nm_gsscl
));
2520 * Removes a mounts context for a credential
2523 nfs_gss_clnt_ctx_remove(struct nfsmount
*nmp
, kauth_cred_t cred
)
2525 struct nfs_gss_clnt_ctx
*cp
;
2530 NFS_GSS_DBG("Enter\n");
2531 NFS_GSS_CLNT_CTX_DUMP(nmp
);
2532 lck_mtx_lock(&nmp
->nm_lock
);
2533 TAILQ_FOREACH(cp
, &nmp
->nm_gsscl
, gss_clnt_entries
) {
2534 lck_mtx_lock(cp
->gss_clnt_mtx
);
2535 if (nfs_gss_clnt_ctx_cred_match(cp
->gss_clnt_cred
, cred
)) {
2536 if (cp
->gss_clnt_flags
& GSS_CTX_DESTROY
) {
2537 NFS_GSS_DBG("Found destroyed context %d/%d. refcnt = %d continuing\n",
2538 kauth_cred_getasid(cp
->gss_clnt_cred
),
2539 kauth_cred_getauid(cp
->gss_clnt_cred
),
2540 cp
->gss_clnt_refcnt
);
2541 lck_mtx_unlock(cp
->gss_clnt_mtx
);
2544 cp
->gss_clnt_refcnt
++;
2545 cp
->gss_clnt_flags
|= (GSS_CTX_INVAL
| GSS_CTX_DESTROY
);
2546 lck_mtx_unlock(cp
->gss_clnt_mtx
);
2548 lck_mtx_unlock(&nmp
->nm_lock
);
2550 * Drop the reference to remove it if its
2553 NFS_GSS_DBG("Removed context %d/%d refcnt = %d\n",
2554 kauth_cred_getasid(cp
->gss_clnt_cred
),
2555 kauth_cred_getuid(cp
->gss_clnt_cred
),
2556 cp
->gss_clnt_refcnt
);
2557 nfs_gss_clnt_ctx_unref(&req
);
2560 lck_mtx_unlock(cp
->gss_clnt_mtx
);
2563 lck_mtx_unlock(&nmp
->nm_lock
);
2565 NFS_GSS_DBG("Returning ENOENT\n");
2570 * Sets a mounts principal for a session associated with cred.
2573 nfs_gss_clnt_ctx_set_principal(struct nfsmount
*nmp
, vfs_context_t ctx
,
2574 uint8_t *principal
, uint32_t princlen
, uint32_t nametype
)
2580 NFS_GSS_DBG("Enter:\n");
2582 bzero(&req
, sizeof(struct nfsreq
));
2584 req
.r_gss_ctx
= NULL
;
2585 req
.r_auth
= nmp
->nm_auth
;
2586 req
.r_thread
= vfs_context_thread(ctx
);
2587 req
.r_cred
= vfs_context_ucred(ctx
);
2589 error
= nfs_gss_clnt_ctx_find_principal(&req
, principal
, princlen
, nametype
);
2590 NFS_GSS_DBG("nfs_gss_clnt_ctx_find_principal returned %d\n", error
);
2592 * We don't care about auth errors. Those would indicate that the context is in the
2593 * neagative cache and if and when the user has credentials for the principal
2594 * we should be good to go in that we will select those credentials for this principal.
2596 if (error
== EACCES
|| error
== EAUTH
|| error
== ENEEDAUTH
)
2599 /* We're done with this request */
2600 nfs_gss_clnt_ctx_unref(&req
);
2606 * Gets a mounts principal from a session associated with cred
2609 nfs_gss_clnt_ctx_get_principal(struct nfsmount
*nmp
, vfs_context_t ctx
,
2610 struct user_nfs_gss_principal
*p
)
2614 struct nfs_gss_clnt_ctx
*cp
;
2615 kauth_cred_t cred
= vfs_context_ucred(ctx
);
2616 const char *princ
= NULL
;
2617 char CTXBUF
[NFS_CTXBUFSZ
];
2619 /* Make sure the the members of the struct user_nfs_gss_principal are initialized */
2620 p
->nametype
= GSSD_STRING_NAME
;
2621 p
->principal
= USER_ADDR_NULL
;
2626 lck_mtx_lock(&nmp
->nm_lock
);
2627 TAILQ_FOREACH(cp
, &nmp
->nm_gsscl
, gss_clnt_entries
) {
2628 lck_mtx_lock(cp
->gss_clnt_mtx
);
2629 if (cp
->gss_clnt_flags
& GSS_CTX_DESTROY
) {
2630 NFS_GSS_DBG("Found destroyed context %s refcnt = %d continuing\n",
2631 NFS_GSS_CTX(&req
, cp
),
2632 cp
->gss_clnt_refcnt
);
2633 lck_mtx_unlock(cp
->gss_clnt_mtx
);
2636 if (nfs_gss_clnt_ctx_cred_match(cp
->gss_clnt_cred
, cred
)) {
2637 cp
->gss_clnt_refcnt
++;
2638 lck_mtx_unlock(cp
->gss_clnt_mtx
);
2641 lck_mtx_unlock(cp
->gss_clnt_mtx
);
2646 lck_mtx_unlock(&nmp
->nm_lock
);
2647 p
->flags
|= NFS_IOC_NO_CRED_FLAG
; /* No credentials, valid or invalid on this mount */
2648 NFS_GSS_DBG("No context found for session %d by uid %d\n",
2649 kauth_cred_getasid(cred
), kauth_cred_getuid(cred
));
2653 /* Indicate if the cred is INVALID */
2654 if (cp
->gss_clnt_flags
& GSS_CTX_INVAL
)
2655 p
->flags
|= NFS_IOC_INVALID_CRED_FLAG
;
2657 /* We have set a principal on the mount */
2658 if (cp
->gss_clnt_principal
) {
2659 princ
= (char *)cp
->gss_clnt_principal
;
2660 p
->princlen
= cp
->gss_clnt_prinlen
;
2661 p
->nametype
= cp
->gss_clnt_prinnt
;
2662 } else if (cp
->gss_clnt_display
) {
2663 /* We have a successful use the the default credential */
2664 princ
= cp
->gss_clnt_display
;
2665 p
->princlen
= strlen(cp
->gss_clnt_display
);
2669 * If neither of the above is true we have an invalid default credential
2670 * So from above p->principal is USER_ADDR_NULL and princ is NULL
2676 MALLOC(pp
, char *, p
->princlen
, M_TEMP
, M_WAITOK
);
2677 bcopy(princ
, pp
, p
->princlen
);
2678 p
->principal
= CAST_USER_ADDR_T(pp
);
2681 lck_mtx_unlock(&nmp
->nm_lock
);
2684 NFS_GSS_DBG("Found context %s\n", NFS_GSS_CTX(&req
, NULL
));
2685 nfs_gss_clnt_ctx_unref(&req
);
2688 #endif /* NFSCLIENT */
2698 * Find a server context based on a handle value received
2699 * in an RPCSEC_GSS credential.
2701 static struct nfs_gss_svc_ctx
*
2702 nfs_gss_svc_ctx_find(uint32_t handle
)
2704 struct nfs_gss_svc_ctx_hashhead
*head
;
2705 struct nfs_gss_svc_ctx
*cp
;
2711 head
= &nfs_gss_svc_ctx_hashtbl
[SVC_CTX_HASH(handle
)];
2713 * Don't return a context that is going to expire in GSS_CTX_PEND seconds
2715 clock_interval_to_deadline(GSS_CTX_PEND
, NSEC_PER_SEC
, &timenow
);
2717 lck_mtx_lock(nfs_gss_svc_ctx_mutex
);
2719 LIST_FOREACH(cp
, head
, gss_svc_entries
) {
2720 if (cp
->gss_svc_handle
== handle
) {
2721 if (timenow
> cp
->gss_svc_incarnation
+ GSS_SVC_CTX_TTL
) {
2723 * Context has or is about to expire. Don't use.
2724 * We'll return null and the client will have to create
2727 cp
->gss_svc_handle
= 0;
2729 * Make sure though that we stay around for GSS_CTX_PEND seconds
2730 * for other threads that might be using the context.
2732 cp
->gss_svc_incarnation
= timenow
;
2737 lck_mtx_lock(cp
->gss_svc_mtx
);
2738 cp
->gss_svc_refcnt
++;
2739 lck_mtx_unlock(cp
->gss_svc_mtx
);
2744 lck_mtx_unlock(nfs_gss_svc_ctx_mutex
);
2750 * Insert a new server context into the hash table
2751 * and start the context reap thread if necessary.
2754 nfs_gss_svc_ctx_insert(struct nfs_gss_svc_ctx
*cp
)
2756 struct nfs_gss_svc_ctx_hashhead
*head
;
2757 struct nfs_gss_svc_ctx
*p
;
2759 lck_mtx_lock(nfs_gss_svc_ctx_mutex
);
2762 * Give the client a random handle so that if we reboot
2763 * it's unlikely the client will get a bad context match.
2764 * Make sure it's not zero or already assigned.
2767 cp
->gss_svc_handle
= random();
2768 if (cp
->gss_svc_handle
== 0)
2770 head
= &nfs_gss_svc_ctx_hashtbl
[SVC_CTX_HASH(cp
->gss_svc_handle
)];
2771 LIST_FOREACH(p
, head
, gss_svc_entries
)
2772 if (p
->gss_svc_handle
== cp
->gss_svc_handle
)
2775 clock_interval_to_deadline(GSS_CTX_PEND
, NSEC_PER_SEC
,
2776 &cp
->gss_svc_incarnation
);
2777 LIST_INSERT_HEAD(head
, cp
, gss_svc_entries
);
2778 nfs_gss_ctx_count
++;
2780 if (!nfs_gss_timer_on
) {
2781 nfs_gss_timer_on
= 1;
2783 nfs_interval_timer_start(nfs_gss_svc_ctx_timer_call
,
2784 min(GSS_TIMER_PERIOD
, max(GSS_CTX_TTL_MIN
, nfsrv_gss_context_ttl
)) * MSECS_PER_SEC
);
2787 lck_mtx_unlock(nfs_gss_svc_ctx_mutex
);
2791 * This function is called via the kernel's callout
2792 * mechanism. It runs only when there are
2793 * cached RPCSEC_GSS contexts.
2796 nfs_gss_svc_ctx_timer(__unused
void *param1
, __unused
void *param2
)
2798 struct nfs_gss_svc_ctx
*cp
, *next
;
2803 lck_mtx_lock(nfs_gss_svc_ctx_mutex
);
2804 clock_get_uptime(&timenow
);
2806 NFS_GSS_DBG("is running\n");
2809 * Scan all the hash chains
2811 for (i
= 0; i
< SVC_CTX_HASHSZ
; i
++) {
2813 * For each hash chain, look for entries
2814 * that haven't been used in a while.
2816 LIST_FOREACH_SAFE(cp
, &nfs_gss_svc_ctx_hashtbl
[i
], gss_svc_entries
, next
) {
2818 if (timenow
> cp
->gss_svc_incarnation
+
2819 (cp
->gss_svc_handle
? GSS_SVC_CTX_TTL
: 0)
2820 && cp
->gss_svc_refcnt
== 0) {
2822 * A stale context - remove it
2824 LIST_REMOVE(cp
, gss_svc_entries
);
2825 NFS_GSS_DBG("Removing contex for %d\n", cp
->gss_svc_uid
);
2826 if (cp
->gss_svc_seqbits
)
2827 FREE(cp
->gss_svc_seqbits
, M_TEMP
);
2828 lck_mtx_destroy(cp
->gss_svc_mtx
, nfs_gss_svc_grp
);
2835 nfs_gss_ctx_count
= contexts
;
2838 * If there are still some cached contexts left,
2839 * set up another callout to check on them later.
2841 nfs_gss_timer_on
= nfs_gss_ctx_count
> 0;
2842 if (nfs_gss_timer_on
)
2843 nfs_interval_timer_start(nfs_gss_svc_ctx_timer_call
,
2844 min(GSS_TIMER_PERIOD
, max(GSS_CTX_TTL_MIN
, nfsrv_gss_context_ttl
)) * MSECS_PER_SEC
);
2846 lck_mtx_unlock(nfs_gss_svc_ctx_mutex
);
2850 * Here the server receives an RPCSEC_GSS credential in an
2851 * RPC call header. First there's some checking to make sure
2852 * the credential is appropriate - whether the context is still
2853 * being set up, or is complete. Then we use the handle to find
2854 * the server's context and validate the verifier, which contains
2855 * a signed checksum of the RPC header. If the verifier checks
2856 * out, we extract the user's UID and groups from the context
2857 * and use it to set up a UNIX credential for the user's request.
2860 nfs_gss_svc_cred_get(struct nfsrv_descript
*nd
, struct nfsm_chain
*nmc
)
2862 uint32_t vers
, proc
, seqnum
, service
;
2863 uint32_t handle
, handle_len
;
2865 struct nfs_gss_svc_ctx
*cp
= NULL
;
2866 uint32_t flavor
= 0, header_len
;
2868 uint32_t arglen
, start
;
2870 gss_buffer_desc cksum
;
2871 struct nfsm_chain nmc_tmp
;
2872 mbuf_t reply_mbuf
, prev_mbuf
, pad_mbuf
;
2874 vers
= proc
= seqnum
= service
= handle_len
= 0;
2877 nfsm_chain_get_32(error
, nmc
, vers
);
2878 if (vers
!= RPCSEC_GSS_VERS_1
) {
2879 error
= NFSERR_AUTHERR
| AUTH_REJECTCRED
;
2883 nfsm_chain_get_32(error
, nmc
, proc
);
2884 nfsm_chain_get_32(error
, nmc
, seqnum
);
2885 nfsm_chain_get_32(error
, nmc
, service
);
2886 nfsm_chain_get_32(error
, nmc
, handle_len
);
2891 * Make sure context setup/destroy is being done with a nullproc
2893 if (proc
!= RPCSEC_GSS_DATA
&& nd
->nd_procnum
!= NFSPROC_NULL
) {
2894 error
= NFSERR_AUTHERR
| RPCSEC_GSS_CREDPROBLEM
;
2899 * If the sequence number is greater than the max
2900 * allowable, reject and have the client init a
2903 if (seqnum
> GSS_MAXSEQ
) {
2904 error
= NFSERR_AUTHERR
| RPCSEC_GSS_CTXPROBLEM
;
2909 service
== RPCSEC_GSS_SVC_NONE
? RPCAUTH_KRB5
:
2910 service
== RPCSEC_GSS_SVC_INTEGRITY
? RPCAUTH_KRB5I
:
2911 service
== RPCSEC_GSS_SVC_PRIVACY
? RPCAUTH_KRB5P
: 0;
2913 if (proc
== RPCSEC_GSS_INIT
) {
2915 * Limit the total number of contexts
2917 if (nfs_gss_ctx_count
> nfs_gss_ctx_max
) {
2918 error
= NFSERR_AUTHERR
| RPCSEC_GSS_CTXPROBLEM
;
2923 * Set up a new context
2925 MALLOC(cp
, struct nfs_gss_svc_ctx
*, sizeof(*cp
), M_TEMP
, M_WAITOK
|M_ZERO
);
2930 cp
->gss_svc_mtx
= lck_mtx_alloc_init(nfs_gss_svc_grp
, LCK_ATTR_NULL
);
2931 cp
->gss_svc_refcnt
= 1;
2934 * Use the handle to find the context
2936 if (handle_len
!= sizeof(handle
)) {
2937 error
= NFSERR_AUTHERR
| RPCSEC_GSS_CREDPROBLEM
;
2940 nfsm_chain_get_32(error
, nmc
, handle
);
2943 cp
= nfs_gss_svc_ctx_find(handle
);
2945 error
= NFSERR_AUTHERR
| RPCSEC_GSS_CTXPROBLEM
;
2950 cp
->gss_svc_proc
= proc
;
2952 if (proc
== RPCSEC_GSS_DATA
|| proc
== RPCSEC_GSS_DESTROY
) {
2953 struct posix_cred temp_pcred
;
2955 if (cp
->gss_svc_seqwin
== 0) {
2957 * Context isn't complete
2959 error
= NFSERR_AUTHERR
| RPCSEC_GSS_CTXPROBLEM
;
2963 if (!nfs_gss_svc_seqnum_valid(cp
, seqnum
)) {
2965 * Sequence number is bad
2967 error
= EINVAL
; // drop the request
2972 * Validate the verifier.
2973 * The verifier contains an encrypted checksum
2974 * of the call header from the XID up to and
2975 * including the credential. We compute the
2976 * checksum and compare it with what came in
2979 header_len
= nfsm_chain_offset(nmc
);
2980 nfsm_chain_get_32(error
, nmc
, flavor
);
2981 nfsm_chain_get_32(error
, nmc
, cksum
.length
);
2984 if (flavor
!= RPCSEC_GSS
|| cksum
.length
> KRB5_MAX_MIC_SIZE
)
2985 error
= NFSERR_AUTHERR
| AUTH_BADVERF
;
2986 MALLOC(cksum
.value
, void *, cksum
.length
, M_TEMP
, M_WAITOK
);
2987 nfsm_chain_get_opaque(error
, nmc
, cksum
.length
, cksum
.value
);
2991 /* Now verify the client's call header checksum */
2992 major
= gss_krb5_verify_mic_mbuf((uint32_t *)&error
, cp
->gss_svc_ctx_id
, nmc
->nmc_mhead
, 0, header_len
, &cksum
, NULL
);
2993 (void)gss_release_buffer(NULL
, &cksum
);
2994 if (major
!= GSS_S_COMPLETE
) {
2995 printf("Server header: gss_krb5_verify_mic_mbuf failed %d\n", error
);
2996 error
= NFSERR_AUTHERR
| RPCSEC_GSS_CTXPROBLEM
;
3000 nd
->nd_gss_seqnum
= seqnum
;
3003 * Set up the user's cred
3005 bzero(&temp_pcred
, sizeof(temp_pcred
));
3006 temp_pcred
.cr_uid
= cp
->gss_svc_uid
;
3007 bcopy(cp
->gss_svc_gids
, temp_pcred
.cr_groups
,
3008 sizeof(gid_t
) * cp
->gss_svc_ngroups
);
3009 temp_pcred
.cr_ngroups
= cp
->gss_svc_ngroups
;
3011 nd
->nd_cr
= posix_cred_create(&temp_pcred
);
3012 if (nd
->nd_cr
== NULL
) {
3016 clock_get_uptime(&cp
->gss_svc_incarnation
);
3019 * If the call arguments are integrity or privacy protected
3020 * then we need to check them here.
3023 case RPCSEC_GSS_SVC_NONE
:
3026 case RPCSEC_GSS_SVC_INTEGRITY
:
3028 * Here's what we expect in the integrity call args:
3030 * - length of seq num + call args (4 bytes)
3031 * - sequence number (4 bytes)
3032 * - call args (variable bytes)
3033 * - length of checksum token
3034 * - checksum of seqnum + call args
3036 nfsm_chain_get_32(error
, nmc
, arglen
); // length of args
3037 if (arglen
> NFS_MAXPACKET
) {
3043 nfsm_chain_adv(error
, &nmc_tmp
, arglen
);
3044 nfsm_chain_get_32(error
, &nmc_tmp
, cksum
.length
);
3045 MALLOC(cksum
.value
, void *, cksum
.length
, M_TEMP
, M_WAITOK
);
3047 if (cksum
.value
== NULL
) {
3051 nfsm_chain_get_opaque(error
, &nmc_tmp
, cksum
.length
, cksum
.value
);
3053 /* Verify the checksum over the call args */
3054 start
= nfsm_chain_offset(nmc
);
3056 major
= gss_krb5_verify_mic_mbuf((uint32_t *)&error
, cp
->gss_svc_ctx_id
,
3057 nmc
->nmc_mhead
, start
, arglen
, &cksum
, NULL
);
3058 FREE(cksum
.value
, M_TEMP
);
3059 if (major
!= GSS_S_COMPLETE
) {
3060 printf("Server args: gss_krb5_verify_mic_mbuf failed %d\n", error
);
3066 * Get the sequence number prepended to the args
3067 * and compare it against the one sent in the
3070 nfsm_chain_get_32(error
, nmc
, seqnum
);
3071 if (seqnum
!= nd
->nd_gss_seqnum
) {
3072 error
= EBADRPC
; // returns as GARBAGEARGS
3076 case RPCSEC_GSS_SVC_PRIVACY
:
3078 * Here's what we expect in the privacy call args:
3080 * - length of wrap token
3081 * - wrap token (37-40 bytes)
3083 prev_mbuf
= nmc
->nmc_mcur
;
3084 nfsm_chain_get_32(error
, nmc
, arglen
); // length of args
3085 if (arglen
> NFS_MAXPACKET
) {
3090 /* Get the wrap token (current mbuf in the chain starting at the current offset) */
3091 start
= nmc
->nmc_ptr
- (caddr_t
)mbuf_data(nmc
->nmc_mcur
);
3093 /* split out the wrap token */
3095 error
= gss_normalize_mbuf(nmc
->nmc_mcur
, start
, &argsize
, &reply_mbuf
, &pad_mbuf
, 0);
3099 assert(argsize
== arglen
);
3101 assert(nfsm_pad(arglen
) == mbuf_len(pad_mbuf
));
3102 mbuf_free(pad_mbuf
);
3104 assert(nfsm_pad(arglen
) == 0);
3107 major
= gss_krb5_unwrap_mbuf((uint32_t *)&error
, cp
->gss_svc_ctx_id
, &reply_mbuf
, 0, arglen
, NULL
, NULL
);
3108 if (major
!= GSS_S_COMPLETE
) {
3109 printf("%s: gss_krb5_unwrap_mbuf failes %d\n", __func__
, error
);
3113 /* Now replace the wrapped arguments with the unwrapped ones */
3114 mbuf_setnext(prev_mbuf
, reply_mbuf
);
3115 nmc
->nmc_mcur
= reply_mbuf
;
3116 nmc
->nmc_ptr
= mbuf_data(reply_mbuf
);
3117 nmc
->nmc_left
= mbuf_len(reply_mbuf
);
3120 * - sequence number (4 bytes)
3124 // nfsm_chain_reverse(nmc, nfsm_pad(toklen));
3127 * Get the sequence number prepended to the args
3128 * and compare it against the one sent in the
3131 nfsm_chain_get_32(error
, nmc
, seqnum
);
3132 if (seqnum
!= nd
->nd_gss_seqnum
) {
3133 printf("%s: Sequence number mismatch seqnum = %d nd->nd_gss_seqnum = %d\n",
3134 __func__
, seqnum
, nd
->nd_gss_seqnum
);
3135 printmbuf("reply_mbuf", nmc
->nmc_mhead
, 0, 0);
3136 printf("reply_mbuf %p nmc_head %p\n", reply_mbuf
, nmc
->nmc_mhead
);
3137 error
= EBADRPC
; // returns as GARBAGEARGS
3145 * If the proc is RPCSEC_GSS_INIT or RPCSEC_GSS_CONTINUE_INIT
3146 * then we expect a null verifier.
3148 nfsm_chain_get_32(error
, nmc
, flavor
);
3149 nfsm_chain_get_32(error
, nmc
, verflen
);
3150 if (error
|| flavor
!= RPCAUTH_NULL
|| verflen
> 0)
3151 error
= NFSERR_AUTHERR
| RPCSEC_GSS_CREDPROBLEM
;
3153 if (proc
== RPCSEC_GSS_INIT
) {
3154 lck_mtx_destroy(cp
->gss_svc_mtx
, nfs_gss_svc_grp
);
3162 nd
->nd_gss_context
= cp
;
3166 nfs_gss_svc_ctx_deref(cp
);
3171 * Insert the server's verifier into the RPC reply header.
3172 * It contains a signed checksum of the sequence number that
3173 * was received in the RPC call.
3174 * Then go on to add integrity or privacy if necessary.
3177 nfs_gss_svc_verf_put(struct nfsrv_descript
*nd
, struct nfsm_chain
*nmc
)
3179 struct nfs_gss_svc_ctx
*cp
;
3181 gss_buffer_desc cksum
, seqbuf
;
3182 uint32_t network_seqnum
;
3183 cp
= nd
->nd_gss_context
;
3186 if (cp
->gss_svc_major
!= GSS_S_COMPLETE
) {
3188 * If the context isn't yet complete
3189 * then return a null verifier.
3191 nfsm_chain_add_32(error
, nmc
, RPCAUTH_NULL
);
3192 nfsm_chain_add_32(error
, nmc
, 0);
3197 * Compute checksum of the request seq number
3198 * If it's the final reply of context setup
3199 * then return the checksum of the context
3202 seqbuf
.length
= NFSX_UNSIGNED
;
3203 if (cp
->gss_svc_proc
== RPCSEC_GSS_INIT
||
3204 cp
->gss_svc_proc
== RPCSEC_GSS_CONTINUE_INIT
)
3205 network_seqnum
= htonl(cp
->gss_svc_seqwin
);
3207 network_seqnum
= htonl(nd
->nd_gss_seqnum
);
3208 seqbuf
.value
= &network_seqnum
;
3210 major
= gss_krb5_get_mic((uint32_t *)&error
, cp
->gss_svc_ctx_id
, 0, &seqbuf
, &cksum
);
3211 if (major
!= GSS_S_COMPLETE
)
3215 * Now wrap it in a token and add
3216 * the verifier to the reply.
3218 nfsm_chain_add_32(error
, nmc
, RPCSEC_GSS
);
3219 nfsm_chain_add_32(error
, nmc
, cksum
.length
);
3220 nfsm_chain_add_opaque(error
, nmc
, cksum
.value
, cksum
.length
);
3221 gss_release_buffer(NULL
, &cksum
);
3227 * The results aren't available yet, but if they need to be
3228 * checksummed for integrity protection or encrypted, then
3229 * we can record the start offset here, insert a place-holder
3230 * for the results length, as well as the sequence number.
3231 * The rest of the work is done later by nfs_gss_svc_protect_reply()
3232 * when the results are available.
3235 nfs_gss_svc_prepare_reply(struct nfsrv_descript
*nd
, struct nfsm_chain
*nmc
)
3237 struct nfs_gss_svc_ctx
*cp
= nd
->nd_gss_context
;
3240 if (cp
->gss_svc_proc
== RPCSEC_GSS_INIT
||
3241 cp
->gss_svc_proc
== RPCSEC_GSS_CONTINUE_INIT
)
3244 switch (nd
->nd_sec
) {
3250 nd
->nd_gss_mb
= nmc
->nmc_mcur
; // record current mbuf
3251 nfsm_chain_finish_mbuf(error
, nmc
); // split the chain here
3259 * The results are checksummed or encrypted for return to the client
3262 nfs_gss_svc_protect_reply(struct nfsrv_descript
*nd
, mbuf_t mrep __unused
)
3264 struct nfs_gss_svc_ctx
*cp
= nd
->nd_gss_context
;
3265 struct nfsm_chain nmrep_res
, *nmc_res
= &nmrep_res
;
3271 * Using a reference to the mbuf where we previously split the reply
3272 * mbuf chain, we split the mbuf chain argument into two mbuf chains,
3273 * one that allows us to prepend a length field or token, (nmc_pre)
3274 * and the second which holds just the results that we're going to
3275 * checksum and/or encrypt. When we're done, we join the chains back
3279 mb
= nd
->nd_gss_mb
; // the mbuf where we split
3280 results
= mbuf_next(mb
); // first mbuf in the results
3281 error
= mbuf_setnext(mb
, NULL
); // disconnect the chains
3284 nfs_gss_nfsm_chain(nmc_res
, mb
); // set up the prepend chain
3285 nfsm_chain_build_done(error
, nmc_res
);
3289 if (nd
->nd_sec
== RPCAUTH_KRB5I
) {
3290 error
= rpc_gss_integ_data_create(cp
->gss_svc_ctx_id
, &results
, nd
->nd_gss_seqnum
, &reslen
);
3293 error
= rpc_gss_priv_data_create(cp
->gss_svc_ctx_id
, &results
, nd
->nd_gss_seqnum
, &reslen
);
3295 nfs_gss_append_chain(nmc_res
, results
); // Append the results mbufs
3296 nfsm_chain_build_done(error
, nmc_res
);
3302 * This function handles the context setup calls from the client.
3303 * Essentially, it implements the NFS null procedure calls when
3304 * an RPCSEC_GSS credential is used.
3305 * This is the context maintenance function. It creates and
3306 * destroys server contexts at the whim of the client.
3307 * During context creation, it receives GSS-API tokens from the
3308 * client, passes them up to gssd, and returns a received token
3309 * back to the client in the null procedure reply.
3312 nfs_gss_svc_ctx_init(struct nfsrv_descript
*nd
, struct nfsrv_sock
*slp
, mbuf_t
*mrepp
)
3314 struct nfs_gss_svc_ctx
*cp
= NULL
;
3317 struct nfsm_chain
*nmreq
, nmrep
;
3320 nmreq
= &nd
->nd_nmreq
;
3321 nfsm_chain_null(&nmrep
);
3323 cp
= nd
->nd_gss_context
;
3326 switch (cp
->gss_svc_proc
) {
3327 case RPCSEC_GSS_INIT
:
3328 nfs_gss_svc_ctx_insert(cp
);
3331 case RPCSEC_GSS_CONTINUE_INIT
:
3332 /* Get the token from the request */
3333 nfsm_chain_get_32(error
, nmreq
, cp
->gss_svc_tokenlen
);
3334 if (cp
->gss_svc_tokenlen
== 0) {
3335 autherr
= RPCSEC_GSS_CREDPROBLEM
;
3338 MALLOC(cp
->gss_svc_token
, u_char
*, cp
->gss_svc_tokenlen
, M_TEMP
, M_WAITOK
);
3339 if (cp
->gss_svc_token
== NULL
) {
3340 autherr
= RPCSEC_GSS_CREDPROBLEM
;
3343 nfsm_chain_get_opaque(error
, nmreq
, cp
->gss_svc_tokenlen
, cp
->gss_svc_token
);
3345 /* Use the token in a gss_accept_sec_context upcall */
3346 error
= nfs_gss_svc_gssd_upcall(cp
);
3348 autherr
= RPCSEC_GSS_CREDPROBLEM
;
3349 if (error
== NFSERR_EAUTH
)
3355 * If the context isn't complete, pass the new token
3356 * back to the client for another round.
3358 if (cp
->gss_svc_major
!= GSS_S_COMPLETE
)
3362 * Now the server context is complete.
3365 clock_get_uptime(&cp
->gss_svc_incarnation
);
3367 cp
->gss_svc_seqwin
= GSS_SVC_SEQWINDOW
;
3368 MALLOC(cp
->gss_svc_seqbits
, uint32_t *,
3369 nfsm_rndup((cp
->gss_svc_seqwin
+ 7) / 8), M_TEMP
, M_WAITOK
|M_ZERO
);
3370 if (cp
->gss_svc_seqbits
== NULL
) {
3371 autherr
= RPCSEC_GSS_CREDPROBLEM
;
3376 case RPCSEC_GSS_DATA
:
3377 /* Just a nullproc ping - do nothing */
3380 case RPCSEC_GSS_DESTROY
:
3382 * Don't destroy the context immediately because
3383 * other active requests might still be using it.
3384 * Instead, schedule it for destruction after
3385 * GSS_CTX_PEND time has elapsed.
3387 cp
= nfs_gss_svc_ctx_find(cp
->gss_svc_handle
);
3389 cp
->gss_svc_handle
= 0; // so it can't be found
3390 lck_mtx_lock(cp
->gss_svc_mtx
);
3391 clock_interval_to_deadline(GSS_CTX_PEND
, NSEC_PER_SEC
,
3392 &cp
->gss_svc_incarnation
);
3393 lck_mtx_unlock(cp
->gss_svc_mtx
);
3397 autherr
= RPCSEC_GSS_CREDPROBLEM
;
3401 /* Now build the reply */
3403 if (nd
->nd_repstat
== 0)
3404 nd
->nd_repstat
= autherr
? (NFSERR_AUTHERR
| autherr
) : NFSERR_RETVOID
;
3405 sz
= 7 * NFSX_UNSIGNED
+ nfsm_rndup(cp
->gss_svc_tokenlen
); // size of results
3406 error
= nfsrv_rephead(nd
, slp
, &nmrep
, sz
);
3407 *mrepp
= nmrep
.nmc_mhead
;
3408 if (error
|| autherr
)
3411 if (cp
->gss_svc_proc
== RPCSEC_GSS_INIT
||
3412 cp
->gss_svc_proc
== RPCSEC_GSS_CONTINUE_INIT
) {
3413 nfsm_chain_add_32(error
, &nmrep
, sizeof(cp
->gss_svc_handle
));
3414 nfsm_chain_add_32(error
, &nmrep
, cp
->gss_svc_handle
);
3416 nfsm_chain_add_32(error
, &nmrep
, cp
->gss_svc_major
);
3417 nfsm_chain_add_32(error
, &nmrep
, cp
->gss_svc_minor
);
3418 nfsm_chain_add_32(error
, &nmrep
, cp
->gss_svc_seqwin
);
3420 nfsm_chain_add_32(error
, &nmrep
, cp
->gss_svc_tokenlen
);
3421 if (cp
->gss_svc_token
!= NULL
) {
3422 nfsm_chain_add_opaque(error
, &nmrep
, cp
->gss_svc_token
, cp
->gss_svc_tokenlen
);
3423 FREE(cp
->gss_svc_token
, M_TEMP
);
3424 cp
->gss_svc_token
= NULL
;
3430 nd
->nd_gss_context
= NULL
;
3431 LIST_REMOVE(cp
, gss_svc_entries
);
3432 if (cp
->gss_svc_seqbits
!= NULL
)
3433 FREE(cp
->gss_svc_seqbits
, M_TEMP
);
3434 if (cp
->gss_svc_token
!= NULL
)
3435 FREE(cp
->gss_svc_token
, M_TEMP
);
3436 lck_mtx_destroy(cp
->gss_svc_mtx
, nfs_gss_svc_grp
);
3440 nfsm_chain_build_done(error
, &nmrep
);
3442 nfsm_chain_cleanup(&nmrep
);
3449 * This is almost a mirror-image of the client side upcall.
3450 * It passes and receives a token, but invokes gss_accept_sec_context.
3451 * If it's the final call of the context setup, then gssd also returns
3452 * the session key and the user's UID.
3455 nfs_gss_svc_gssd_upcall(struct nfs_gss_svc_ctx
*cp
)
3460 gssd_byte_buffer octx
= NULL
;
3461 uint32_t lucidlen
= 0;
3462 void *lucid_ctx_buffer
;
3464 vm_map_copy_t itoken
= NULL
;
3465 gssd_byte_buffer otoken
= NULL
;
3466 mach_msg_type_number_t otokenlen
;
3468 char svcname
[] = "nfs";
3470 kr
= host_get_gssd_port(host_priv_self(), &mp
);
3471 if (kr
!= KERN_SUCCESS
) {
3472 printf("nfs_gss_svc_gssd_upcall: can't get gssd port, status %x (%d)\n", kr
, kr
);
3475 if (!IPC_PORT_VALID(mp
)) {
3476 printf("nfs_gss_svc_gssd_upcall: gssd port not valid\n");
3480 if (cp
->gss_svc_tokenlen
> 0)
3481 nfs_gss_mach_alloc_buffer(cp
->gss_svc_token
, cp
->gss_svc_tokenlen
, &itoken
);
3484 printf("Calling mach_gss_accept_sec_context\n");
3485 kr
= mach_gss_accept_sec_context(
3487 (gssd_byte_buffer
) itoken
, (mach_msg_type_number_t
) cp
->gss_svc_tokenlen
,
3490 &cp
->gss_svc_context
,
3491 &cp
->gss_svc_cred_handle
,
3495 &cp
->gss_svc_ngroups
,
3496 &octx
, (mach_msg_type_number_t
*) &lucidlen
,
3497 &otoken
, &otokenlen
,
3499 &cp
->gss_svc_minor
);
3501 printf("mach_gss_accept_sec_context returned %d\n", kr
);
3502 if (kr
!= KERN_SUCCESS
) {
3503 printf("nfs_gss_svc_gssd_upcall failed: %x (%d)\n", kr
, kr
);
3504 if (kr
== MIG_SERVER_DIED
&& cp
->gss_svc_context
== 0 &&
3505 retry_cnt
++ < NFS_GSS_MACH_MAX_RETRIES
) {
3506 if (cp
->gss_svc_tokenlen
> 0)
3507 nfs_gss_mach_alloc_buffer(cp
->gss_svc_token
, cp
->gss_svc_tokenlen
, &itoken
);
3510 host_release_special_port(mp
);
3514 host_release_special_port(mp
);
3517 if (lucidlen
> MAX_LUCIDLEN
) {
3518 printf("nfs_gss_svc_gssd_upcall: bad context length (%d)\n", lucidlen
);
3519 vm_map_copy_discard((vm_map_copy_t
) octx
);
3520 vm_map_copy_discard((vm_map_copy_t
) otoken
);
3523 MALLOC(lucid_ctx_buffer
, void *, lucidlen
, M_TEMP
, M_WAITOK
| M_ZERO
);
3524 error
= nfs_gss_mach_vmcopyout((vm_map_copy_t
) octx
, lucidlen
, lucid_ctx_buffer
);
3526 vm_map_copy_discard((vm_map_copy_t
) otoken
);
3527 FREE(lucid_ctx_buffer
, M_TEMP
);
3530 if (cp
->gss_svc_ctx_id
)
3531 gss_krb5_destroy_context(cp
->gss_svc_ctx_id
);
3532 cp
->gss_svc_ctx_id
= gss_krb5_make_context(lucid_ctx_buffer
, lucidlen
);
3533 if (cp
->gss_svc_ctx_id
== NULL
) {
3534 printf("Failed to make context from lucid_ctx_buffer\n");
3539 /* Free context token used as input */
3540 if (cp
->gss_svc_token
)
3541 FREE(cp
->gss_svc_token
, M_TEMP
);
3542 cp
->gss_svc_token
= NULL
;
3543 cp
->gss_svc_tokenlen
= 0;
3545 if (otokenlen
> 0) {
3546 /* Set context token to gss output token */
3547 MALLOC(cp
->gss_svc_token
, u_char
*, otokenlen
, M_TEMP
, M_WAITOK
);
3548 if (cp
->gss_svc_token
== NULL
) {
3549 printf("nfs_gss_svc_gssd_upcall: could not allocate %d bytes\n", otokenlen
);
3550 vm_map_copy_discard((vm_map_copy_t
) otoken
);
3553 error
= nfs_gss_mach_vmcopyout((vm_map_copy_t
) otoken
, otokenlen
, cp
->gss_svc_token
);
3555 FREE(cp
->gss_svc_token
, M_TEMP
);
3556 cp
->gss_svc_token
= NULL
;
3557 return (NFSERR_EAUTH
);
3559 cp
->gss_svc_tokenlen
= otokenlen
;
3565 FREE(cp
->gss_svc_token
, M_TEMP
);
3566 cp
->gss_svc_tokenlen
= 0;
3567 cp
->gss_svc_token
= NULL
;
3569 return (NFSERR_EAUTH
);
3573 * Validate the sequence number in the credential as described
3574 * in RFC 2203 Section 5.3.3.1
3576 * Here the window of valid sequence numbers is represented by
3577 * a bitmap. As each sequence number is received, its bit is
3578 * set in the bitmap. An invalid sequence number lies below
3579 * the lower bound of the window, or is within the window but
3580 * has its bit already set.
3583 nfs_gss_svc_seqnum_valid(struct nfs_gss_svc_ctx
*cp
, uint32_t seq
)
3585 uint32_t *bits
= cp
->gss_svc_seqbits
;
3586 uint32_t win
= cp
->gss_svc_seqwin
;
3589 lck_mtx_lock(cp
->gss_svc_mtx
);
3592 * If greater than the window upper bound,
3593 * move the window up, and set the bit.
3595 if (seq
> cp
->gss_svc_seqmax
) {
3596 if (seq
- cp
->gss_svc_seqmax
> win
)
3597 bzero(bits
, nfsm_rndup((win
+ 7) / 8));
3599 for (i
= cp
->gss_svc_seqmax
+ 1; i
< seq
; i
++)
3600 win_resetbit(bits
, i
% win
);
3601 win_setbit(bits
, seq
% win
);
3602 cp
->gss_svc_seqmax
= seq
;
3603 lck_mtx_unlock(cp
->gss_svc_mtx
);
3608 * Invalid if below the lower bound of the window
3610 if (seq
<= cp
->gss_svc_seqmax
- win
) {
3611 lck_mtx_unlock(cp
->gss_svc_mtx
);
3616 * In the window, invalid if the bit is already set
3618 if (win_getbit(bits
, seq
% win
)) {
3619 lck_mtx_unlock(cp
->gss_svc_mtx
);
3622 win_setbit(bits
, seq
% win
);
3623 lck_mtx_unlock(cp
->gss_svc_mtx
);
3628 * Drop a reference to a context
3630 * Note that it's OK for the context to exist
3631 * with a refcount of zero. The refcount isn't
3632 * checked until we're about to reap an expired one.
3635 nfs_gss_svc_ctx_deref(struct nfs_gss_svc_ctx
*cp
)
3637 lck_mtx_lock(cp
->gss_svc_mtx
);
3638 if (cp
->gss_svc_refcnt
> 0)
3639 cp
->gss_svc_refcnt
--;
3641 printf("nfs_gss_ctx_deref: zero refcount\n");
3642 lck_mtx_unlock(cp
->gss_svc_mtx
);
3646 * Called at NFS server shutdown - destroy all contexts
3649 nfs_gss_svc_cleanup(void)
3651 struct nfs_gss_svc_ctx_hashhead
*head
;
3652 struct nfs_gss_svc_ctx
*cp
, *ncp
;
3655 lck_mtx_lock(nfs_gss_svc_ctx_mutex
);
3658 * Run through all the buckets
3660 for (i
= 0; i
< SVC_CTX_HASHSZ
; i
++) {
3662 * Remove and free all entries in the bucket
3664 head
= &nfs_gss_svc_ctx_hashtbl
[i
];
3665 LIST_FOREACH_SAFE(cp
, head
, gss_svc_entries
, ncp
) {
3666 LIST_REMOVE(cp
, gss_svc_entries
);
3667 if (cp
->gss_svc_seqbits
)
3668 FREE(cp
->gss_svc_seqbits
, M_TEMP
);
3669 lck_mtx_destroy(cp
->gss_svc_mtx
, nfs_gss_svc_grp
);
3674 lck_mtx_unlock(nfs_gss_svc_ctx_mutex
);
3677 #endif /* NFSSERVER */
3681 * The following functions are used by both client and server.
3685 * Release a host special port that was obtained by host_get_special_port
3686 * or one of its macros (host_get_gssd_port in this case).
3687 * This really should be in a public kpi.
3690 /* This should be in a public header if this routine is not */
3691 extern void ipc_port_release_send(ipc_port_t
);
3692 extern ipc_port_t
ipc_port_copy_send(ipc_port_t
);
3695 host_release_special_port(mach_port_t mp
)
3697 if (IPC_PORT_VALID(mp
))
3698 ipc_port_release_send(mp
);
3702 host_copy_special_port(mach_port_t mp
)
3704 return (ipc_port_copy_send(mp
));
3708 * The token that is sent and received in the gssd upcall
3709 * has unbounded variable length. Mach RPC does not pass
3710 * the token in-line. Instead it uses page mapping to handle
3711 * these parameters. This function allocates a VM buffer
3712 * to hold the token for an upcall and copies the token
3713 * (received from the client) into it. The VM buffer is
3714 * marked with a src_destroy flag so that the upcall will
3715 * automatically de-allocate the buffer when the upcall is
3719 nfs_gss_mach_alloc_buffer(u_char
*buf
, uint32_t buflen
, vm_map_copy_t
*addr
)
3722 vm_offset_t kmem_buf
;
3726 if (buf
== NULL
|| buflen
== 0)
3729 tbuflen
= vm_map_round_page(buflen
,
3730 vm_map_page_mask(ipc_kernel_map
));
3731 kr
= vm_allocate(ipc_kernel_map
, &kmem_buf
, tbuflen
, VM_FLAGS_ANYWHERE
| VM_MAKE_TAG(VM_KERN_MEMORY_FILE
));
3733 printf("nfs_gss_mach_alloc_buffer: vm_allocate failed\n");
3737 kr
= vm_map_wire(ipc_kernel_map
,
3738 vm_map_trunc_page(kmem_buf
,
3739 vm_map_page_mask(ipc_kernel_map
)),
3740 vm_map_round_page(kmem_buf
+ tbuflen
,
3741 vm_map_page_mask(ipc_kernel_map
)),
3742 VM_PROT_READ
|VM_PROT_WRITE
|VM_PROT_MEMORY_TAG_MAKE(VM_KERN_MEMORY_FILE
), FALSE
);
3744 printf("nfs_gss_mach_alloc_buffer: vm_map_wire failed\n");
3748 bcopy(buf
, (void *) kmem_buf
, buflen
);
3749 // Shouldn't need to bzero below since vm_allocate returns zeroed pages
3750 // bzero(kmem_buf + buflen, tbuflen - buflen);
3752 kr
= vm_map_unwire(ipc_kernel_map
,
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
)),
3759 printf("nfs_gss_mach_alloc_buffer: vm_map_unwire failed\n");
3763 kr
= vm_map_copyin(ipc_kernel_map
, (vm_map_address_t
) kmem_buf
,
3764 (vm_map_size_t
) buflen
, TRUE
, addr
);
3766 printf("nfs_gss_mach_alloc_buffer: vm_map_copyin failed\n");
3772 * Here we handle a token received from the gssd via an upcall.
3773 * The received token resides in an allocate VM buffer.
3774 * We copy the token out of this buffer to a chunk of malloc'ed
3775 * memory of the right size, then de-allocate the VM buffer.
3778 nfs_gss_mach_vmcopyout(vm_map_copy_t in
, uint32_t len
, u_char
*out
)
3780 vm_map_offset_t map_data
;
3784 error
= vm_map_copyout(ipc_kernel_map
, &map_data
, in
);
3788 data
= CAST_DOWN(vm_offset_t
, map_data
);
3789 bcopy((void *) data
, out
, len
);
3790 vm_deallocate(ipc_kernel_map
, data
, len
);
3796 * Return the number of bytes in an mbuf chain.
3799 nfs_gss_mchain_length(mbuf_t mhead
)
3804 for (mb
= mhead
; mb
; mb
= mbuf_next(mb
))
3805 len
+= mbuf_len(mb
);
3811 * Append an args or results mbuf chain to the header chain
3814 nfs_gss_append_chain(struct nfsm_chain
*nmc
, mbuf_t mc
)
3819 /* Connect the mbuf chains */
3820 error
= mbuf_setnext(nmc
->nmc_mcur
, mc
);
3824 /* Find the last mbuf in the chain */
3826 for (mb
= mc
; mb
; mb
= mbuf_next(mb
))
3829 nmc
->nmc_mcur
= tail
;
3830 nmc
->nmc_ptr
= (caddr_t
) mbuf_data(tail
) + mbuf_len(tail
);
3831 nmc
->nmc_left
= mbuf_trailingspace(tail
);
3837 * Convert an mbuf chain to an NFS mbuf chain
3840 nfs_gss_nfsm_chain(struct nfsm_chain
*nmc
, mbuf_t mc
)
3844 /* Find the last mbuf in the chain */
3846 for (mb
= mc
; mb
; mb
= mbuf_next(mb
))
3849 nmc
->nmc_mhead
= mc
;
3850 nmc
->nmc_mcur
= tail
;
3851 nmc
->nmc_ptr
= (caddr_t
) mbuf_data(tail
) + mbuf_len(tail
);
3852 nmc
->nmc_left
= mbuf_trailingspace(tail
);
3859 #define DISPLAYLEN 16
3860 #define MAXDISPLAYLEN 256
3863 hexdump(const char *msg
, void *data
, size_t len
)
3867 char *p
, disbuf
[3*DISPLAYLEN
+1];
3869 printf("NFS DEBUG %s len=%d:\n", msg
, (uint32_t)len
);
3870 if (len
> MAXDISPLAYLEN
)
3871 len
= MAXDISPLAYLEN
;
3873 for (i
= 0; i
< len
; i
+= DISPLAYLEN
) {
3874 for (p
= disbuf
, j
= 0; (j
+ i
) < len
&& j
< DISPLAYLEN
; j
++, p
+= 3)
3875 snprintf(p
, 4, "%02x ", d
[i
+ j
]);
3876 printf("\t%s\n", disbuf
);