xref: /freebsd/sys/rpc/auth.h (revision 5def4c47d4bd90b209b9b4a4ba9faec15846d8fd)
1 /*	$NetBSD: auth.h,v 1.15 2000/06/02 22:57:55 fvdl Exp $	*/
2 
3 /*-
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  * Copyright (c) 2009, Sun Microsystems, Inc.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions are met:
11  * - Redistributions of source code must retain the above copyright notice,
12  *   this list of conditions and the following disclaimer.
13  * - Redistributions in binary form must reproduce the above copyright notice,
14  *   this list of conditions and the following disclaimer in the documentation
15  *   and/or other materials provided with the distribution.
16  * - Neither the name of Sun Microsystems, Inc. nor the names of its
17  *   contributors may be used to endorse or promote products derived
18  *   from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
24  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  *
32  *	from: @(#)auth.h 1.17 88/02/08 SMI
33  *	from: @(#)auth.h	2.3 88/08/07 4.0 RPCSRC
34  *	from: @(#)auth.h	1.43 	98/02/02 SMI
35  * $FreeBSD$
36  */
37 
38 /*
39  * auth.h, Authentication interface.
40  *
41  * Copyright (C) 1984, Sun Microsystems, Inc.
42  *
43  * The data structures are completely opaque to the client.  The client
44  * is required to pass an AUTH * to routines that create rpc
45  * "sessions".
46  */
47 
48 #ifndef _RPC_AUTH_H
49 #define _RPC_AUTH_H
50 #include <rpc/xdr.h>
51 #include <rpc/clnt_stat.h>
52 #include <sys/cdefs.h>
53 #include <sys/socket.h>
54 
55 #define MAX_AUTH_BYTES	400
56 #define MAXNETNAMELEN	255	/* maximum length of network user's name */
57 
58 /*
59  *  Client side authentication/security data
60  */
61 
62 typedef struct sec_data {
63 	u_int	secmod;		/* security mode number e.g. in nfssec.conf */
64 	u_int	rpcflavor;	/* rpc flavors:AUTH_UNIX,AUTH_DES,RPCSEC_GSS */
65 	int	flags;		/* AUTH_F_xxx flags */
66 	caddr_t data;		/* opaque data per flavor */
67 } sec_data_t;
68 
69 #ifdef _SYSCALL32_IMPL
70 struct sec_data32 {
71 	uint32_t secmod;	/* security mode number e.g. in nfssec.conf */
72 	uint32_t rpcflavor;	/* rpc flavors:AUTH_UNIX,AUTH_DES,RPCSEC_GSS */
73 	int32_t flags;		/* AUTH_F_xxx flags */
74 	caddr32_t data;		/* opaque data per flavor */
75 };
76 #endif /* _SYSCALL32_IMPL */
77 
78 /*
79  * AUTH_DES flavor specific data from sec_data opaque data field.
80  * AUTH_KERB has the same structure.
81  */
82 typedef struct des_clnt_data {
83 	struct netbuf	syncaddr;	/* time sync addr */
84 	struct knetconfig *knconf;	/* knetconfig info that associated */
85 					/* with the syncaddr. */
86 	char		*netname;	/* server's netname */
87 	int		netnamelen;	/* server's netname len */
88 } dh_k4_clntdata_t;
89 
90 #ifdef _SYSCALL32_IMPL
91 struct des_clnt_data32 {
92 	struct netbuf32 syncaddr;	/* time sync addr */
93 	caddr32_t knconf;		/* knetconfig info that associated */
94 					/* with the syncaddr. */
95 	caddr32_t netname;		/* server's netname */
96 	int32_t netnamelen;		/* server's netname len */
97 };
98 #endif /* _SYSCALL32_IMPL */
99 
100 #ifdef KERBEROS
101 /*
102  * flavor specific data to hold the data for AUTH_DES/AUTH_KERB(v4)
103  * in sec_data->data opaque field.
104  */
105 typedef struct krb4_svc_data {
106 	int		window;		/* window option value */
107 } krb4_svcdata_t;
108 
109 typedef struct krb4_svc_data	des_svcdata_t;
110 #endif /* KERBEROS */
111 
112 /*
113  * authentication/security specific flags
114  */
115 #define AUTH_F_RPCTIMESYNC	0x001	/* use RPC to do time sync */
116 #define AUTH_F_TRYNONE		0x002	/* allow fall back to AUTH_NONE */
117 
118 
119 /*
120  * Status returned from authentication check
121  */
122 enum auth_stat {
123 	AUTH_OK=0,
124 	/*
125 	 * failed at remote end
126 	 */
127 	AUTH_BADCRED=1,			/* bogus credentials (seal broken) */
128 	AUTH_REJECTEDCRED=2,		/* client should begin new session */
129 	AUTH_BADVERF=3,			/* bogus verifier (seal broken) */
130 	AUTH_REJECTEDVERF=4,		/* verifier expired or was replayed */
131 	AUTH_TOOWEAK=5,			/* rejected due to security reasons */
132 	/*
133 	 * failed locally
134 	*/
135 	AUTH_INVALIDRESP=6,		/* bogus response verifier */
136 	AUTH_FAILED=7,			/* some unknown reason */
137 #ifdef KERBEROS
138 	/*
139 	 * kerberos errors
140 	 */
141 	,
142 	AUTH_KERB_GENERIC = 8,		/* kerberos generic error */
143 	AUTH_TIMEEXPIRE = 9,		/* time of credential expired */
144 	AUTH_TKT_FILE = 10,		/* something wrong with ticket file */
145 	AUTH_DECODE = 11,			/* can't decode authenticator */
146 	AUTH_NET_ADDR = 12,		/* wrong net address in ticket */
147 #endif /* KERBEROS */
148 	/*
149 	 * RPCSEC_GSS errors
150 	 */
151 	RPCSEC_GSS_CREDPROBLEM = 13,
152 	RPCSEC_GSS_CTXPROBLEM = 14,
153 	/*
154 	 * RPC-over-TLS errors
155 	 */
156 	AUTH_NEEDS_TLS = 15,
157 	AUTH_NEEDS_TLS_MUTUAL_HOST = 16,
158 	/* Also used by RPCSEC_TLS for the same purpose */
159 	RPCSEC_GSS_NODISPATCH = 0x8000000
160 };
161 
162 union des_block {
163 	struct {
164 		uint32_t high;
165 		uint32_t low;
166 	} key;
167 	char c[8];
168 };
169 typedef union des_block des_block;
170 __BEGIN_DECLS
171 extern bool_t xdr_des_block(XDR *, des_block *);
172 __END_DECLS
173 
174 /*
175  * Authentication info.  Opaque to client.
176  */
177 struct opaque_auth {
178 	enum_t	oa_flavor;		/* flavor of auth */
179 	caddr_t	oa_base;		/* address of more auth stuff */
180 	u_int	oa_length;		/* not to exceed MAX_AUTH_BYTES */
181 };
182 
183 
184 /*
185  * Auth handle, interface to client side authenticators.
186  */
187 struct rpc_err;
188 typedef struct __auth {
189 	struct	opaque_auth	ah_cred;
190 	struct	opaque_auth	ah_verf;
191 	union	des_block	ah_key;
192 	struct auth_ops {
193 		void	(*ah_nextverf) (struct __auth *);
194 		/* nextverf & serialize */
195 		int	(*ah_marshal) (struct __auth *, uint32_t, XDR *,
196 		    struct mbuf *);
197 		/* validate verifier */
198 		int	(*ah_validate) (struct __auth *, uint32_t,
199 		    struct opaque_auth *, struct mbuf **);
200 		/* refresh credentials */
201 		int	(*ah_refresh) (struct __auth *, void *);
202 		/* destroy this structure */
203 		void	(*ah_destroy) (struct __auth *);
204 	} *ah_ops;
205 	void *ah_private;
206 } AUTH;
207 
208 
209 /*
210  * Authentication ops.
211  * The ops and the auth handle provide the interface to the authenticators.
212  *
213  * AUTH	*auth;
214  * XDR	*xdrs;
215  * struct opaque_auth verf;
216  */
217 #define AUTH_NEXTVERF(auth)		\
218 		((*((auth)->ah_ops->ah_nextverf))(auth))
219 
220 #define AUTH_MARSHALL(auth, xid, xdrs, args)	\
221 		((*((auth)->ah_ops->ah_marshal))(auth, xid, xdrs, args))
222 
223 #define AUTH_VALIDATE(auth, xid, verfp, resultsp) \
224 		((*((auth)->ah_ops->ah_validate))((auth), xid, verfp, resultsp))
225 
226 #define AUTH_REFRESH(auth, msg)		\
227 		((*((auth)->ah_ops->ah_refresh))(auth, msg))
228 
229 #define AUTH_DESTROY(auth)		\
230 		((*((auth)->ah_ops->ah_destroy))(auth))
231 
232 __BEGIN_DECLS
233 extern struct opaque_auth _null_auth;
234 __END_DECLS
235 
236 /*
237  * These are the various implementations of client side authenticators.
238  */
239 
240 /*
241  * System style authentication
242  * AUTH *authunix_create(machname, uid, gid, len, aup_gids)
243  *	char *machname;
244  *	u_int uid;
245  *	u_int gid;
246  *	int len;
247  *	u_int *aup_gids;
248  */
249 __BEGIN_DECLS
250 #ifdef _KERNEL
251 struct ucred;
252 extern AUTH *authunix_create(struct ucred *);
253 #else
254 extern AUTH *authunix_create(char *, u_int, u_int, int, u_int *);
255 extern AUTH *authunix_create_default(void);	/* takes no parameters */
256 #endif
257 extern AUTH *authnone_create(void);		/* takes no parameters */
258 extern AUTH *authtls_create(void);		/* takes no parameters */
259 __END_DECLS
260 /*
261  * DES style authentication
262  * AUTH *authsecdes_create(servername, window, timehost, ckey)
263  * 	char *servername;		- network name of server
264  *	u_int window;			- time to live
265  * 	const char *timehost;			- optional hostname to sync with
266  * 	des_block *ckey;		- optional conversation key to use
267  */
268 __BEGIN_DECLS
269 extern AUTH *authdes_create (char *, u_int, struct sockaddr *, des_block *);
270 extern AUTH *authdes_seccreate (const char *, const u_int, const  char *,
271     const  des_block *);
272 __END_DECLS
273 
274 __BEGIN_DECLS
275 extern bool_t xdr_opaque_auth		(XDR *, struct opaque_auth *);
276 __END_DECLS
277 
278 #define authsys_create(c,i1,i2,i3,ip) authunix_create((c),(i1),(i2),(i3),(ip))
279 #define authsys_create_default() authunix_create_default()
280 
281 /*
282  * Netname manipulation routines.
283  */
284 __BEGIN_DECLS
285 extern int getnetname(char *);
286 extern int host2netname(char *, const char *, const char *);
287 extern int user2netname(char *, const uid_t, const char *);
288 extern int netname2user(char *, uid_t *, gid_t *, int *, gid_t *);
289 extern int netname2host(char *, char *, const int);
290 extern void passwd2des ( char *, char * );
291 __END_DECLS
292 
293 /*
294  *
295  * These routines interface to the keyserv daemon
296  *
297  */
298 __BEGIN_DECLS
299 extern int key_decryptsession(const char *, des_block *);
300 extern int key_encryptsession(const char *, des_block *);
301 extern int key_gendes(des_block *);
302 extern int key_setsecret(const char *);
303 extern int key_secretkey_is_set(void);
304 __END_DECLS
305 
306 /*
307  * Publickey routines.
308  */
309 __BEGIN_DECLS
310 extern int getpublickey (const char *, char *);
311 extern int getpublicandprivatekey (const char *, char *);
312 extern int getsecretkey (char *, char *, char *);
313 __END_DECLS
314 
315 #ifdef KERBEROS
316 /*
317  * Kerberos style authentication
318  * AUTH *authkerb_seccreate(service, srv_inst, realm, window, timehost, status)
319  *	const char *service;			- service name
320  *	const char *srv_inst;			- server instance
321  *	const char *realm;			- server realm
322  *	const u_int window;			- time to live
323  *	const char *timehost;			- optional hostname to sync with
324  *	int *status;				- kerberos status returned
325  */
326 __BEGIN_DECLS
327 extern AUTH	*authkerb_seccreate(const char *, const char *, const  char *,
328 		    const u_int, const char *, int *);
329 __END_DECLS
330 
331 /*
332  * Map a kerberos credential into a unix cred.
333  *
334  *	authkerb_getucred(rqst, uid, gid, grouplen, groups)
335  *	const struct svc_req *rqst;		- request pointer
336  *	uid_t *uid;
337  *	gid_t *gid;
338  *	short *grouplen;
339  *	int *groups;
340  *
341  */
342 __BEGIN_DECLS
343 extern int	authkerb_getucred(/* struct svc_req *, uid_t *, gid_t *,
344 		    short *, int * */);
345 __END_DECLS
346 #endif /* KERBEROS */
347 
348 __BEGIN_DECLS
349 struct svc_req;
350 struct rpc_msg;
351 enum auth_stat _svcauth_null (struct svc_req *, struct rpc_msg *);
352 enum auth_stat _svcauth_short (struct svc_req *, struct rpc_msg *);
353 enum auth_stat _svcauth_unix (struct svc_req *, struct rpc_msg *);
354 enum auth_stat _svcauth_rpcsec_tls (struct svc_req *, struct rpc_msg *);
355 __END_DECLS
356 
357 #define AUTH_NONE	0		/* no authentication */
358 #define	AUTH_NULL	0		/* backward compatibility */
359 #define	AUTH_SYS	1		/* unix style (uid, gids) */
360 #define AUTH_UNIX	AUTH_SYS
361 #define	AUTH_SHORT	2		/* short hand unix style */
362 #define AUTH_DH		3		/* for Diffie-Hellman mechanism */
363 #define AUTH_DES	AUTH_DH		/* for backward compatibility */
364 #define AUTH_KERB	4		/* kerberos style */
365 #define RPCSEC_GSS	6		/* RPCSEC_GSS */
366 #define	AUTH_TLS	7		/* Initiate RPC-over-TLS */
367 
368 /*
369  * Pseudo auth flavors for RPCSEC_GSS.
370  */
371 #define	RPCSEC_GSS_KRB5		390003
372 #define	RPCSEC_GSS_KRB5I	390004
373 #define	RPCSEC_GSS_KRB5P	390005
374 
375 #endif /* !_RPC_AUTH_H */
376