2 * Copyright (c) 2007 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@
29 #ifndef _NFS_NFS_GSS_H_
30 #define _NFS_NFS_GSS_H_
32 #include <gssd/gssd_mach.h>
33 #include <libkern/crypto/md5.h>
34 #include <crypto/des/des_locl.h>
35 #include <sys/param.h>
38 #define RPCSEC_GSS_VERS_1 1
40 enum rpcsec_gss_proc
{
43 RPCSEC_GSS_CONTINUE_INIT
= 2,
44 RPCSEC_GSS_DESTROY
= 3
47 enum rpcsec_gss_service
{
48 RPCSEC_GSS_SVC_NONE
= 1, // sec=krb5
49 RPCSEC_GSS_SVC_INTEGRITY
= 2, // sec=krb5i
50 RPCSEC_GSS_SVC_PRIVACY
= 3, // sec=krb5p
51 RPCSEC_GSS_SVC_SYS
= 4 // sec=sys (fallback)
57 #define GSS_S_COMPLETE 0
58 #define GSS_S_CONTINUE_NEEDED 1
60 #define GSS_MAXSEQ 0x80000000 // The biggest sequence number
61 #define GSS_SVC_MAXCONTEXTS 500000 // Max contexts supported
62 #define GSS_SVC_SEQWINDOW 256 // Server's sequence window
63 #define GSS_CLNT_SEQLISTMAX 32 // Max length of req seq num list
64 #define GSS_CLNT_SYS_VALID 300 // Valid time (sec) for failover ctx
66 #define SKEYLEN 8 // length of DES key
69 * The client's RPCSEC_GSS context information
71 struct nfs_gss_clnt_ctx
{
72 lck_mtx_t
*gss_clnt_mtx
;
73 thread_t gss_clnt_thread
; // Thread creating context
74 TAILQ_ENTRY(nfs_gss_clnt_ctx
) gss_clnt_entries
;
75 uint32_t gss_clnt_flags
; // Flag bits - see below
76 uint32_t gss_clnt_refcnt
; // Reference count
77 uid_t gss_clnt_uid
; // Owner of this context
78 uint32_t gss_clnt_proc
; // Current GSS proc for cred
79 uint32_t gss_clnt_seqnum
; // GSS sequence number
80 uint32_t gss_clnt_service
; // Indicates krb5, krb5i or krb5p
81 u_char
*gss_clnt_handle
; // Identifies server context
82 uint32_t gss_clnt_handle_len
; // Size of server's ctx handle
83 time_t gss_clnt_ctime
; // When context was created
84 uint32_t gss_clnt_seqwin
; // Server's seq num window
85 uint32_t *gss_clnt_seqbits
; // Bitmap to track seq numbers in use
86 mach_port_t gss_clnt_mport
; // Mach port for gssd upcall
87 u_char
*gss_clnt_verf
; // RPC verifier from server
88 uint64_t gss_clnt_gssd_verf
; // Verifier from gssd
89 char *gss_clnt_svcname
; // Service name e.g. "nfs/big.apple.com"
90 uint32_t gss_clnt_cred_handle
; // Opaque cred handle from gssd
91 uint32_t gss_clnt_context
; // Opaque context handle from gssd
92 u_char
*gss_clnt_token
; // GSS token exchanged via gssd & server
93 uint32_t gss_clnt_tokenlen
; // Length of token
94 u_char gss_clnt_skey
[SKEYLEN
]; // Context session key (DES)
95 des_key_schedule gss_clnt_sched
; // Schedule derived from key
96 uint32_t gss_clnt_major
; // GSS major result from gssd or server
97 uint32_t gss_clnt_minor
; // GSS minor result from gssd or server
103 #define GSS_CTX_COMPLETE 0x00000001 // Context is complete
104 #define GSS_CTX_INVAL 0x00000002 // Context is invalid
105 #define GSS_NEEDSEQ 0x00000004 // Need a sequence number
106 #define GSS_NEEDCTX 0x00000008 // Need the context
109 * The server's RPCSEC_GSS context information
111 struct nfs_gss_svc_ctx
{
112 lck_mtx_t
*gss_svc_mtx
;
113 LIST_ENTRY(nfs_gss_svc_ctx
) gss_svc_entries
;
114 uint32_t gss_svc_handle
; // Identifies server context to client
115 uint32_t gss_svc_proc
; // Current GSS proc from cred
116 uid_t gss_svc_uid
; // UID of this user
117 gid_t gss_svc_gids
[NGROUPS
]; // GIDs of this user
118 uint32_t gss_svc_ngroups
; // Count of gids
119 uint64_t gss_svc_expiretime
; // Delete ctx if we exceed this
120 uint32_t gss_svc_seqmax
; // Current max GSS sequence number
121 uint32_t gss_svc_seqwin
; // GSS sequence number window
122 uint32_t *gss_svc_seqbits
; // Bitmap to track seq numbers
123 uint64_t gss_svc_gssd_verf
; // Verifier from gssd
124 uint32_t gss_svc_cred_handle
; // Opaque cred handle from gssd
125 uint32_t gss_svc_context
; // Opaque context handle from gssd
126 u_char
*gss_svc_token
; // GSS token exchanged via gssd & client
127 uint32_t gss_svc_tokenlen
; // Length of token
128 u_char gss_svc_skey
[SKEYLEN
]; // Context session key (DES)
129 des_key_schedule gss_svc_sched
; // Schedule derived from key
130 uint32_t gss_svc_major
; // GSS major result from gssd
131 uint32_t gss_svc_minor
; // GSS minor result from gssd
134 #define SVC_CTX_HASHSZ 64
135 #define SVC_CTX_HASH(handle) ((handle) % SVC_CTX_HASHSZ)
136 LIST_HEAD(nfs_gss_svc_ctx_hashhead
, nfs_gss_svc_ctx
);
139 * Macros to manipulate bits in the sequence window
141 #define win_getbit(bits, bit) ((bits[(bit) / 32] & (1 << (bit) % 32)) != 0)
142 #define win_setbit(bits, bit) do { bits[(bit) / 32] |= (1 << (bit) % 32); } while (0)
143 #define win_resetbit(bits, bit) do { bits[(bit) / 32] &= ~(1 << (bit) % 32); } while (0)
146 * Server context stale times
148 #define GSS_CTX_PEND 5 // seconds
149 #define GSS_CTX_EXPIRE (8 * 3600) // seconds
150 #define GSS_TIMER_PERIOD 300 // seconds
151 #define MSECS_PER_SEC 1000
155 void nfs_gss_init(void);
156 int nfs_gss_clnt_cred_put(struct nfsreq
*, struct nfsm_chain
*, mbuf_t
);
157 int nfs_gss_clnt_verf_get(struct nfsreq
*, struct nfsm_chain
*,
158 uint32_t, uint32_t, uint32_t *);
159 void nfs_gss_clnt_rpcdone(struct nfsreq
*);
160 int nfs_gss_clnt_args_restore(struct nfsreq
*);
161 int nfs_gss_clnt_ctx_renew(struct nfsreq
*);
162 void nfs_gss_clnt_ctx_ref(struct nfsreq
*, struct nfs_gss_clnt_ctx
*);
163 void nfs_gss_clnt_ctx_unref(struct nfsreq
*);
164 void nfs_gss_clnt_ctx_unmount(struct nfsmount
*, int);
165 int nfs_gss_svc_cred_get(struct nfsrv_descript
*, struct nfsm_chain
*);
166 int nfs_gss_svc_verf_put(struct nfsrv_descript
*, struct nfsm_chain
*);
167 int nfs_gss_svc_ctx_init(struct nfsrv_descript
*, struct nfsrv_sock
*, mbuf_t
*);
168 int nfs_gss_svc_prepare_reply(struct nfsrv_descript
*, struct nfsm_chain
*);
169 int nfs_gss_svc_protect_reply(struct nfsrv_descript
*, mbuf_t
);
170 void nfs_gss_svc_cleanup(void);
173 #endif /* _NFS_NFS_GSS_H_ */