1/*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 2008 Doug Rabson
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 *
28 *	$FreeBSD$
29 */
30
31#ifndef _RPCSEC_GSS_H
32#define _RPCSEC_GSS_H
33
34#include <kgssapi/gssapi.h>
35
36#ifndef MAX_GSS_MECH
37#define MAX_GSS_MECH	64
38#endif
39
40/*
41 * Define the types of security service required for rpc_gss_seccreate().
42 */
43typedef enum {
44	rpc_gss_svc_default	= 0,
45	rpc_gss_svc_none	= 1,
46	rpc_gss_svc_integrity	= 2,
47	rpc_gss_svc_privacy	= 3
48} rpc_gss_service_t;
49
50/*
51 * Structure containing options for rpc_gss_seccreate().
52 */
53typedef struct {
54	int		req_flags;	/* GSS request bits */
55	int		time_req;	/* requested credential lifetime */
56	gss_cred_id_t	my_cred;	/* GSS credential */
57	gss_channel_bindings_t input_channel_bindings;
58} rpc_gss_options_req_t;
59
60/*
61 * Structure containing options returned by rpc_gss_seccreate().
62 */
63typedef struct {
64	int		major_status;
65	int		minor_status;
66	u_int		rpcsec_version;
67	int		ret_flags;
68	int		time_req;
69	gss_ctx_id_t	gss_context;
70	char		actual_mechanism[MAX_GSS_MECH];
71} rpc_gss_options_ret_t;
72
73/*
74 * Client principal type. Used as an argument to
75 * rpc_gss_get_principal_name(). Also referenced by the
76 * rpc_gss_rawcred_t structure.
77 */
78typedef struct {
79	int		len;
80	char		name[1];
81} *rpc_gss_principal_t;
82
83/*
84 * Structure for raw credentials used by rpc_gss_getcred() and
85 * rpc_gss_set_callback().
86 */
87typedef struct {
88	u_int		version;	/* RPC version number */
89	const char	*mechanism;	/* security mechanism */
90	const char	*qop;		/* quality of protection */
91	rpc_gss_principal_t client_principal; /* client name */
92	const char	*svc_principal;	/* server name */
93	rpc_gss_service_t service;	/* service type */
94} rpc_gss_rawcred_t;
95
96/*
97 * Unix credentials derived from raw credentials. Returned by
98 * rpc_gss_getcred().
99 */
100typedef struct {
101	uid_t		uid;		/* user ID */
102	gid_t		gid;		/* group ID */
103	short		gidlen;
104	gid_t		*gidlist;	/* list of groups */
105} rpc_gss_ucred_t;
106
107/*
108 * Structure used to enforce a particular QOP and service.
109 */
110typedef struct {
111	bool_t		locked;
112	rpc_gss_rawcred_t *raw_cred;
113} rpc_gss_lock_t;
114
115/*
116 * Callback structure used by rpc_gss_set_callback().
117 */
118typedef struct {
119	u_int		program;	/* RPC program number */
120	u_int		version;	/* RPC version number */
121					/* user defined callback */
122	bool_t		(*callback)(struct svc_req *req,
123				    gss_cred_id_t deleg,
124				    gss_ctx_id_t gss_context,
125				    rpc_gss_lock_t *lock,
126				    void **cookie);
127} rpc_gss_callback_t;
128
129/*
130 * Structure used to return error information by rpc_gss_get_error()
131 */
132typedef struct {
133	int		rpc_gss_error;
134	int		system_error;	/* same as errno */
135} rpc_gss_error_t;
136
137/*
138 * Values for rpc_gss_error
139 */
140#define RPC_GSS_ER_SUCCESS	0	/* no error */
141#define RPC_GSS_ER_SYSTEMERROR	1	/* system error */
142
143__BEGIN_DECLS
144
145#ifdef _KERNEL
146/*
147 * Set up a structure of entry points for the kgssapi module and inline
148 * functions named rpc_gss_XXX_call() to use them, so that the kgssapi
149 * module doesn't need to be loaded for the NFS modules to work using
150 * AUTH_SYS. The kgssapi modules will be loaded by the gssd(8) daemon
151 * when it is started up and the entry points will then be filled in.
152 */
153typedef AUTH	*rpc_gss_secfind_ftype(CLIENT *clnt, struct ucred *cred,
154		    const char *principal, gss_OID mech_oid,
155		    rpc_gss_service_t service);
156typedef void	rpc_gss_secpurge_ftype(CLIENT *clnt);
157typedef AUTH	*rpc_gss_seccreate_ftype(CLIENT *clnt, struct ucred *cred,
158		    const char *clnt_principal, const char *principal,
159		    const char *mechanism, rpc_gss_service_t service,
160		    const char *qop, rpc_gss_options_req_t *options_req,
161		    rpc_gss_options_ret_t *options_ret);
162typedef bool_t	rpc_gss_set_defaults_ftype(AUTH *auth,
163		    rpc_gss_service_t service, const char *qop);
164typedef int	rpc_gss_max_data_length_ftype(AUTH *handle,
165		    int max_tp_unit_len);
166typedef void	rpc_gss_get_error_ftype(rpc_gss_error_t *error);
167typedef bool_t	rpc_gss_mech_to_oid_ftype(const char *mech, gss_OID *oid_ret);
168typedef bool_t	rpc_gss_oid_to_mech_ftype(gss_OID oid, const char **mech_ret);
169typedef bool_t	rpc_gss_qop_to_num_ftype(const char *qop, const char *mech,
170		    u_int *num_ret);
171typedef const char **rpc_gss_get_mechanisms_ftype(void);
172typedef bool_t	rpc_gss_get_versions_ftype(u_int *vers_hi, u_int *vers_lo);
173typedef bool_t	rpc_gss_is_installed_ftype(const char *mech);
174typedef bool_t	rpc_gss_set_svc_name_ftype(const char *principal,
175		    const char *mechanism, u_int req_time, u_int program,
176		    u_int version);
177typedef void	rpc_gss_clear_svc_name_ftype(u_int program, u_int version);
178typedef bool_t	rpc_gss_getcred_ftype(struct svc_req *req,
179		    rpc_gss_rawcred_t **rcred,
180		    rpc_gss_ucred_t **ucred, void **cookie);
181typedef bool_t	rpc_gss_set_callback_ftype(rpc_gss_callback_t *cb);
182typedef void	rpc_gss_clear_callback_ftype(rpc_gss_callback_t *cb);
183typedef bool_t	rpc_gss_get_principal_name_ftype(rpc_gss_principal_t *principal,
184		    const char *mech, const char *name, const char *node,
185		    const char *domain);
186typedef int	rpc_gss_svc_max_data_length_ftype(struct svc_req *req,
187		    int max_tp_unit_len);
188typedef void	rpc_gss_refresh_auth_ftype(AUTH *auth);
189
190struct rpc_gss_entries {
191	rpc_gss_secfind_ftype		*rpc_gss_secfind;
192	rpc_gss_secpurge_ftype		*rpc_gss_secpurge;
193	rpc_gss_seccreate_ftype		*rpc_gss_seccreate;
194	rpc_gss_set_defaults_ftype	*rpc_gss_set_defaults;
195	rpc_gss_max_data_length_ftype	*rpc_gss_max_data_length;
196	rpc_gss_get_error_ftype		*rpc_gss_get_error;
197	rpc_gss_mech_to_oid_ftype	*rpc_gss_mech_to_oid;
198	rpc_gss_oid_to_mech_ftype	*rpc_gss_oid_to_mech;
199	rpc_gss_qop_to_num_ftype	*rpc_gss_qop_to_num;
200	rpc_gss_get_mechanisms_ftype	*rpc_gss_get_mechanisms;
201	rpc_gss_get_versions_ftype	*rpc_gss_get_versions;
202	rpc_gss_is_installed_ftype	*rpc_gss_is_installed;
203	rpc_gss_set_svc_name_ftype	*rpc_gss_set_svc_name;
204	rpc_gss_clear_svc_name_ftype	*rpc_gss_clear_svc_name;
205	rpc_gss_getcred_ftype		*rpc_gss_getcred;
206	rpc_gss_set_callback_ftype	*rpc_gss_set_callback;
207	rpc_gss_clear_callback_ftype	*rpc_gss_clear_callback;
208	rpc_gss_get_principal_name_ftype *rpc_gss_get_principal_name;
209	rpc_gss_svc_max_data_length_ftype *rpc_gss_svc_max_data_length;
210	rpc_gss_refresh_auth_ftype	*rpc_gss_refresh_auth;
211};
212extern struct rpc_gss_entries	rpc_gss_entries;
213
214/* Functions to access the entry points. */
215static __inline AUTH *
216rpc_gss_secfind_call(CLIENT *clnt, struct ucred *cred, const char *principal,
217    gss_OID mech_oid, rpc_gss_service_t service)
218{
219	AUTH *ret = NULL;
220
221	if (rpc_gss_entries.rpc_gss_secfind != NULL)
222		ret = (*rpc_gss_entries.rpc_gss_secfind)(clnt, cred, principal,
223		    mech_oid, service);
224	return (ret);
225}
226
227static __inline void
228rpc_gss_secpurge_call(CLIENT *clnt)
229{
230
231	if (rpc_gss_entries.rpc_gss_secpurge != NULL)
232		(*rpc_gss_entries.rpc_gss_secpurge)(clnt);
233}
234
235static __inline AUTH *
236rpc_gss_seccreate_call(CLIENT *clnt, struct ucred *cred,
237    const char *clnt_principal, const char *principal, const char *mechanism,
238    rpc_gss_service_t service, const char *qop,
239    rpc_gss_options_req_t *options_req, rpc_gss_options_ret_t *options_ret)
240{
241	AUTH *ret = NULL;
242
243	if (rpc_gss_entries.rpc_gss_seccreate != NULL)
244		ret = (*rpc_gss_entries.rpc_gss_seccreate)(clnt, cred,
245		    clnt_principal, principal, mechanism, service, qop,
246		    options_req, options_ret);
247	return (ret);
248}
249
250static __inline bool_t
251rpc_gss_set_defaults_call(AUTH *auth, rpc_gss_service_t service,
252    const char *qop)
253{
254	bool_t ret = 1;
255
256	if (rpc_gss_entries.rpc_gss_set_defaults != NULL)
257		ret = (*rpc_gss_entries.rpc_gss_set_defaults)(auth, service,
258		    qop);
259	return (ret);
260}
261
262static __inline int
263rpc_gss_max_data_length_call(AUTH *handle, int max_tp_unit_len)
264{
265	int ret = 0;
266
267	if (rpc_gss_entries.rpc_gss_max_data_length != NULL)
268		ret = (*rpc_gss_entries.rpc_gss_max_data_length)(handle,
269		    max_tp_unit_len);
270	return (ret);
271}
272
273static __inline void
274rpc_gss_get_error_call(rpc_gss_error_t *error)
275{
276
277	if (rpc_gss_entries.rpc_gss_get_error != NULL)
278		(*rpc_gss_entries.rpc_gss_get_error)(error);
279}
280
281static __inline bool_t
282rpc_gss_mech_to_oid_call(const char *mech, gss_OID *oid_ret)
283{
284	bool_t ret = 1;
285
286	if (rpc_gss_entries.rpc_gss_mech_to_oid != NULL)
287		ret = (*rpc_gss_entries.rpc_gss_mech_to_oid)(mech, oid_ret);
288	return (ret);
289}
290
291static __inline bool_t
292rpc_gss_oid_to_mech_call(gss_OID oid, const char **mech_ret)
293{
294	bool_t ret = 1;
295
296	if (rpc_gss_entries.rpc_gss_oid_to_mech != NULL)
297		ret = (*rpc_gss_entries.rpc_gss_oid_to_mech)(oid, mech_ret);
298	return (ret);
299}
300
301static __inline bool_t
302rpc_gss_qop_to_num_call(const char *qop, const char *mech, u_int *num_ret)
303{
304	bool_t ret = 1;
305
306	if (rpc_gss_entries.rpc_gss_qop_to_num != NULL)
307		ret = (*rpc_gss_entries.rpc_gss_qop_to_num)(qop, mech, num_ret);
308	return (ret);
309}
310
311static __inline const char **
312rpc_gss_get_mechanisms_call(void)
313{
314	const char **ret = NULL;
315
316	if (rpc_gss_entries.rpc_gss_get_mechanisms != NULL)
317		ret = (*rpc_gss_entries.rpc_gss_get_mechanisms)();
318	return (ret);
319}
320
321static __inline bool_t
322rpc_gss_get_versions_call(u_int *vers_hi, u_int *vers_lo)
323{
324	bool_t ret = 1;
325
326	if (rpc_gss_entries.rpc_gss_get_versions != NULL)
327		ret = (*rpc_gss_entries.rpc_gss_get_versions)(vers_hi, vers_lo);
328	return (ret);
329}
330
331static __inline bool_t
332rpc_gss_is_installed_call(const char *mech)
333{
334	bool_t ret = 1;
335
336	if (rpc_gss_entries.rpc_gss_is_installed != NULL)
337		ret = (*rpc_gss_entries.rpc_gss_is_installed)(mech);
338	return (ret);
339}
340
341static __inline bool_t
342rpc_gss_set_svc_name_call(const char *principal, const char *mechanism,
343    u_int req_time, u_int program, u_int version)
344{
345	bool_t ret = 1;
346
347	if (rpc_gss_entries.rpc_gss_set_svc_name != NULL)
348		ret = (*rpc_gss_entries.rpc_gss_set_svc_name)(principal,
349		    mechanism, req_time, program, version);
350	return (ret);
351}
352
353static __inline void
354rpc_gss_clear_svc_name_call(u_int program, u_int version)
355{
356
357	if (rpc_gss_entries.rpc_gss_clear_svc_name != NULL)
358		(*rpc_gss_entries.rpc_gss_clear_svc_name)(program, version);
359}
360
361static __inline bool_t
362rpc_gss_getcred_call(struct svc_req *req, rpc_gss_rawcred_t **rcred,
363    rpc_gss_ucred_t **ucred, void **cookie)
364{
365	bool_t ret = 1;
366
367	if (rpc_gss_entries.rpc_gss_getcred != NULL)
368		ret = (*rpc_gss_entries.rpc_gss_getcred)(req, rcred, ucred,
369		    cookie);
370	return (ret);
371}
372
373static __inline bool_t
374rpc_gss_set_callback_call(rpc_gss_callback_t *cb)
375{
376	bool_t ret = 1;
377
378	if (rpc_gss_entries.rpc_gss_set_callback != NULL)
379		ret = (*rpc_gss_entries.rpc_gss_set_callback)(cb);
380	return (ret);
381}
382
383static __inline void
384rpc_gss_clear_callback_call(rpc_gss_callback_t *cb)
385{
386
387	if (rpc_gss_entries.rpc_gss_clear_callback != NULL)
388		(*rpc_gss_entries.rpc_gss_clear_callback)(cb);
389}
390
391static __inline bool_t
392rpc_gss_get_principal_name_call(rpc_gss_principal_t *principal,
393    const char *mech, const char *name, const char *node, const char *domain)
394{
395	bool_t ret = 1;
396
397	if (rpc_gss_entries.rpc_gss_get_principal_name != NULL)
398		ret = (*rpc_gss_entries.rpc_gss_get_principal_name)(principal,
399		    mech, name, node, domain);
400	return (ret);
401}
402
403static __inline int
404rpc_gss_svc_max_data_length_call(struct svc_req *req, int max_tp_unit_len)
405{
406	int ret = 0;
407
408	if (rpc_gss_entries.rpc_gss_svc_max_data_length != NULL)
409		ret = (*rpc_gss_entries.rpc_gss_svc_max_data_length)(req,
410		    max_tp_unit_len);
411	return (ret);
412}
413
414static __inline void
415rpc_gss_refresh_auth_call(AUTH *auth)
416{
417
418	if (rpc_gss_entries.rpc_gss_refresh_auth != NULL)
419		(*rpc_gss_entries.rpc_gss_refresh_auth)(auth);
420}
421
422AUTH	*rpc_gss_secfind(CLIENT *clnt, struct ucred *cred,
423    const char *principal, gss_OID mech_oid, rpc_gss_service_t service);
424void	rpc_gss_secpurge(CLIENT *clnt);
425void	rpc_gss_refresh_auth(AUTH *auth);
426AUTH	*rpc_gss_seccreate(CLIENT *clnt, struct ucred *cred,
427    const char *clnt_principal, const char *principal,
428    const char *mechanism, rpc_gss_service_t service,
429    const char *qop, rpc_gss_options_req_t *options_req,
430    rpc_gss_options_ret_t *options_ret);
431#else	/* !_KERNEL */
432AUTH	*rpc_gss_seccreate(CLIENT *clnt, struct ucred *cred,
433    const char *principal, const char *mechanism, rpc_gss_service_t service,
434    const char *qop, rpc_gss_options_req_t *options_req,
435    rpc_gss_options_ret_t *options_ret);
436#endif	/* _KERNEL */
437bool_t	rpc_gss_set_defaults(AUTH *auth, rpc_gss_service_t service,
438    const char *qop);
439int	rpc_gss_max_data_length(AUTH *handle, int max_tp_unit_len);
440void	rpc_gss_get_error(rpc_gss_error_t *error);
441
442bool_t	rpc_gss_mech_to_oid(const char *mech, gss_OID *oid_ret);
443bool_t	rpc_gss_oid_to_mech(gss_OID oid, const char **mech_ret);
444bool_t	rpc_gss_qop_to_num(const char *qop, const char *mech, u_int *num_ret);
445const char **rpc_gss_get_mechanisms(void);
446const char **rpc_gss_get_mech_info(const char *mech, rpc_gss_service_t *service);
447bool_t	rpc_gss_get_versions(u_int *vers_hi, u_int *vers_lo);
448bool_t	rpc_gss_is_installed(const char *mech);
449
450bool_t	rpc_gss_set_svc_name(const char *principal, const char *mechanism,
451    u_int req_time, u_int program, u_int version);
452void rpc_gss_clear_svc_name(u_int program, u_int version);
453bool_t	rpc_gss_getcred(struct svc_req *req, rpc_gss_rawcred_t **rcred,
454    rpc_gss_ucred_t **ucred, void **cookie);
455bool_t	rpc_gss_set_callback(rpc_gss_callback_t *cb);
456void rpc_gss_clear_callback(rpc_gss_callback_t *cb);
457bool_t	rpc_gss_get_principal_name(rpc_gss_principal_t *principal,
458    const char *mech, const char *name, const char *node, const char *domain);
459int	rpc_gss_svc_max_data_length(struct svc_req *req, int max_tp_unit_len);
460
461/*
462 * Internal interface from the RPC implementation.
463 */
464#ifndef _KERNEL
465bool_t	__rpc_gss_wrap(AUTH *auth, void *header, size_t headerlen,
466    XDR* xdrs, xdrproc_t xdr_args, void *args_ptr);
467bool_t	__rpc_gss_unwrap(AUTH *auth, XDR* xdrs, xdrproc_t xdr_args,
468    void *args_ptr);
469#endif
470bool_t __rpc_gss_set_error(int rpc_gss_error, int system_error);
471
472__END_DECLS
473
474#endif /* !_RPCSEC_GSS_H */
475