xref: /illumos-gate/usr/src/uts/common/rpc/auth.h (revision e8d3efa1c56e5f2b5368600a2baeb7b1d54a07f8)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Copyright 2017 Joyent Inc
24  * Use is subject to license terms.
25  */
26 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
27 /* All Rights Reserved */
28 /*
29  * Portions of this source code were derived from Berkeley
30  * 4.3 BSD under license from the Regents of the University of
31  * California.
32  */
33 
34 /*
35  * auth.h, Authentication interface.
36  *
37  * The data structures are completely opaque to the client. The client
38  * is required to pass a AUTH * to routines that create rpc
39  * "sessions".
40  */
41 
42 #ifndef	_RPC_AUTH_H
43 #define	_RPC_AUTH_H
44 
45 #include <rpc/xdr.h>
46 #include <rpc/clnt_stat.h>
47 #include <sys/cred.h>
48 #include <sys/tiuser.h>
49 #ifdef _KERNEL
50 #include <sys/zone.h>
51 #endif
52 
53 #ifdef	__cplusplus
54 extern "C" {
55 #endif
56 
57 #define	MAX_AUTH_BYTES	400	/* maximum length of an auth type, from RFC */
58 #define	MAXNETNAMELEN	255	/* maximum length of network user's name */
59 
60 /*
61  * NOTE: this value *must* be kept larger than the maximum size of all the
62  * structs that rq_clntcred is cast to in the different authentication types.
63  * If changes are made to any of these *_area structs, double-check they all
64  * still fit. If any new authentication mechanisms are added, add a note here.
65  *
66  * Currently these structs can be found in:
67  *  - __svcauth_sys (svc_auth_sys.c)
68  *  - __svcauth_des (svcauth_des.c)
69  *  - __svcauth_loopback (svc_auth_loopb.c)
70  */
71 #define	RQCRED_SIZE	700	/* size allocated for rq_clntcred */
72 
73 /*
74  *  Client side authentication/security data
75  */
76 typedef struct sec_data {
77 	uint_t	secmod;		/* security mode number e.g. in nfssec.conf */
78 	uint_t	rpcflavor;	/* rpc flavors:AUTH_UNIX,AUTH_DES,RPCSEC_GSS */
79 	int	flags;		/* AUTH_F_xxx flags */
80 	uid_t	uid;		/* uid of caller for all sec flavors (NFSv4)  */
81 	caddr_t	data;		/* opaque data per flavor */
82 } sec_data_t;
83 
84 #ifdef _SYSCALL32_IMPL
85 struct sec_data32 {
86 	uint32_t	secmod;	/* security mode number e.g. in nfssec.conf */
87 	uint32_t	rpcflavor; /* AUTH_UNIX,AUTH_DES,RPCSEC_GSS */
88 	int32_t		flags;	/* AUTH_F_xxx flags */
89 	uid_t		uid;	/* uid of caller for all sec flavors (NFSv4) */
90 	caddr32_t	data;	/* opaque data per flavor */
91 };
92 #endif /* _SYSCALL32_IMPL */
93 
94 /*
95  * AUTH_DES flavor specific data from sec_data opaque data field.
96  * AUTH_KERB has the same structure.
97  */
98 typedef struct des_clnt_data {
99 	struct netbuf	syncaddr;	/* time sync addr */
100 	struct knetconfig *knconf;	/* knetconfig info that associated */
101 					/* with the syncaddr. */
102 	char		*netname;	/* server's netname */
103 	int		netnamelen;	/* server's netname len */
104 } dh_k4_clntdata_t;
105 
106 #ifdef _SYSCALL32_IMPL
107 struct des_clnt_data32 {
108 	struct netbuf32 syncaddr;	/* time sync addr */
109 	caddr32_t knconf;		/* knetconfig info that associated */
110 					/* with the syncaddr. */
111 	caddr32_t netname;		/* server's netname */
112 	int32_t	netnamelen;		/* server's netname len */
113 };
114 #endif /* _SYSCALL32_IMPL */
115 
116 /*
117  * flavor specific data to hold the data for AUTH_DES/AUTH_KERB(v4)
118  * in sec_data->data opaque field.
119  */
120 typedef struct krb4_svc_data {
121 	int		window;		/* window option value */
122 } krb4_svcdata_t;
123 
124 typedef struct krb4_svc_data	des_svcdata_t;
125 
126 /*
127  * authentication/security specific flags
128  */
129 #define	AUTH_F_RPCTIMESYNC	0x001	/* use RPC to do time sync */
130 #define	AUTH_F_TRYNONE		0x002	/* allow fall back to AUTH_NONE */
131 
132 
133 /*
134  * Status returned from authentication check
135  */
136 enum auth_stat {
137 	AUTH_OK = 0,
138 	/*
139 	 * failed at remote end
140 	 */
141 	AUTH_BADCRED = 1,		/* bogus credentials (seal broken) */
142 	AUTH_REJECTEDCRED = 2,		/* client should begin new session */
143 	AUTH_BADVERF = 3,		/* bogus verifier (seal broken) */
144 	AUTH_REJECTEDVERF = 4,		/* verifier expired or was replayed */
145 	AUTH_TOOWEAK = 5,		/* rejected due to security reasons */
146 	/*
147 	 * failed locally
148 	 */
149 	AUTH_INVALIDRESP = 6,		/* bogus response verifier */
150 	AUTH_FAILED = 7,			/* some unknown reason */
151 	/*
152 	 * kerberos errors
153 	 */
154 	AUTH_KERB_GENERIC = 8,		/* kerberos generic error */
155 	AUTH_TIMEEXPIRE = 9,		/* time of credential expired */
156 	AUTH_TKT_FILE = 10,		/* something wrong with ticket file */
157 	AUTH_DECODE = 11,		/* can't decode authenticator */
158 	AUTH_NET_ADDR = 12,		/* wrong net address in ticket */
159 	/*
160 	 * GSS related errors
161 	 */
162 	RPCSEC_GSS_NOCRED = 13,		/* no credentials for user */
163 	RPCSEC_GSS_FAILED = 14		/* GSS failure, credentials deleted */
164 };
165 typedef enum auth_stat AUTH_STAT;
166 
167 union des_block {
168 	struct	{
169 		uint32_t high;
170 		uint32_t low;
171 	} key;
172 	char c[8];
173 };
174 typedef union des_block des_block;
175 
176 extern bool_t xdr_des_block(XDR *, des_block *);
177 
178 
179 /*
180  * Authentication info. Opaque to client.
181  */
182 struct opaque_auth {
183 	enum_t	oa_flavor;		/* flavor of auth */
184 	caddr_t	oa_base;		/* address of more auth stuff */
185 	uint_t	oa_length;		/* not to exceed MAX_AUTH_BYTES */
186 };
187 
188 
189 /*
190  * Auth handle, interface to client side authenticators.
191  */
192 typedef struct __auth {
193 	struct	opaque_auth	ah_cred;
194 	struct	opaque_auth	ah_verf;
195 	union	des_block	ah_key;
196 	struct auth_ops {
197 		void	(*ah_nextverf)(struct __auth *);
198 #ifdef _KERNEL
199 		int	(*ah_marshal)(struct __auth *, XDR *, struct cred *);
200 #else
201 		int	(*ah_marshal)(struct __auth *, XDR *);
202 #endif
203 		/* nextverf & serialize */
204 		int	(*ah_validate)(struct __auth *,
205 		    struct opaque_auth *);
206 		/* validate varifier */
207 #ifdef _KERNEL
208 		int	(*ah_refresh)(struct __auth *, struct rpc_msg *,
209 		    cred_t *);
210 #else
211 		int	(*ah_refresh)(struct __auth *, void *);
212 		/* refresh credentials */
213 #endif
214 		void	(*ah_destroy)(struct __auth *);
215 		/* destroy this structure */
216 
217 #ifdef _KERNEL
218 		int	(*ah_wrap)(struct __auth *, caddr_t, uint_t,
219 		    XDR *, xdrproc_t, caddr_t);
220 		int	(*ah_unwrap)(struct __auth *, XDR *, xdrproc_t,
221 		    caddr_t);
222 #endif
223 	} *ah_ops;
224 	caddr_t ah_private;
225 } AUTH;
226 
227 
228 /*
229  * Authentication ops.
230  * The ops and the auth handle provide the interface to the authenticators.
231  *
232  * AUTH	*auth;
233  * XDR	*xdrs;
234  * struct opaque_auth verf;
235  */
236 #define	AUTH_NEXTVERF(auth)		\
237 		((*((auth)->ah_ops->ah_nextverf))(auth))
238 #define	auth_nextverf(auth)		\
239 		((*((auth)->ah_ops->ah_nextverf))(auth))
240 
241 
242 #ifdef _KERNEL
243 #define	AUTH_MARSHALL(auth, xdrs, cred)	\
244 		((*((auth)->ah_ops->ah_marshal))(auth, xdrs, cred))
245 #define	auth_marshall(auth, xdrs, cred)	\
246 		((*((auth)->ah_ops->ah_marshal))(auth, xdrs, cred))
247 #else
248 #define	AUTH_MARSHALL(auth, xdrs)	\
249 		((*((auth)->ah_ops->ah_marshal))(auth, xdrs))
250 #define	auth_marshall(auth, xdrs)	\
251 		((*((auth)->ah_ops->ah_marshal))(auth, xdrs))
252 #endif
253 
254 
255 #define	AUTH_VALIDATE(auth, verfp)	\
256 		((*((auth)->ah_ops->ah_validate))((auth), verfp))
257 #define	auth_validate(auth, verfp)	\
258 		((*((auth)->ah_ops->ah_validate))((auth), verfp))
259 
260 #ifdef _KERNEL
261 #define	AUTH_REFRESH(auth, msg, cr)		\
262 		((*((auth)->ah_ops->ah_refresh))(auth, msg, cr))
263 #define	auth_refresh(auth, msg, cr)		\
264 		((*((auth)->ah_ops->ah_refresh))(auth, msg, cr))
265 #else
266 #define	AUTH_REFRESH(auth, msg)		\
267 		((*((auth)->ah_ops->ah_refresh))(auth, msg))
268 #define	auth_refresh(auth, msg)		\
269 		((*((auth)->ah_ops->ah_refresh))(auth, msg))
270 #endif
271 
272 #define	AUTH_DESTROY(auth)		\
273 		((*((auth)->ah_ops->ah_destroy))(auth))
274 #define	auth_destroy(auth)		\
275 		((*((auth)->ah_ops->ah_destroy))(auth))
276 
277 /*
278  * Auth flavors can now apply a transformation in addition to simple XDR
279  * on the body of a call/response in ways that depend on the flavor being
280  * used.  These interfaces provide a generic interface between the
281  * internal RPC frame and the auth flavor specific code to allow the
282  * auth flavor to encode (WRAP) or decode (UNWRAP) the body.
283  */
284 #ifdef _KERNEL
285 #define	AUTH_WRAP(auth, buf, buflen, xdrs, xfunc, xwhere)	\
286 		((*((auth)->ah_ops->ah_wrap))(auth, buf, buflen, \
287 				xdrs, xfunc, xwhere))
288 #define	auth_wrap(auth, buf, buflen, xdrs, xfunc, xwhere)	\
289 		((*((auth)->ah_ops->ah_wrap))(auth, buf, buflen, \
290 				xdrs, xfunc, xwhere))
291 
292 #define	AUTH_UNWRAP(auth, xdrs, xfunc, xwhere)	\
293 		((*((auth)->ah_ops->ah_unwrap))(auth, xdrs, xfunc, xwhere))
294 #define	auth_unwrap(auth, xdrs)	\
295 		((*((auth)->ah_ops->ah_unwrap))(auth, xdrs, xfunc, xwhere))
296 #endif
297 
298 extern struct opaque_auth _null_auth;
299 
300 /*
301  * These are the various implementations of client side authenticators.
302  */
303 
304 /*
305  * System style authentication
306  * AUTH *authsys_create(machname, uid, gid, len, aup_gids)
307  *	const char *machname;
308  *	const uid_t uid;
309  *	const gid_t gid;
310  *	const int len;
311  *	const gid_t *aup_gids;
312  */
313 #ifdef _KERNEL
314 extern AUTH *authkern_create(void);		/* takes no parameters */
315 extern int authkern_init(void *, void *, int);
316 extern struct kmem_cache *authkern_cache;
317 extern AUTH *authnone_create(void);		/* takes no parameters */
318 extern int authnone_init(void *, void *, int);
319 extern struct kmem_cache *authnone_cache;
320 extern AUTH *authloopback_create(void);		/* takes no parameters */
321 extern int authloopback_init(void *, void *, int);
322 extern struct kmem_cache *authloopback_cache;
323 #else /* _KERNEL */
324 extern AUTH *authsys_create(const char *, const uid_t, const gid_t, const int,
325     const gid_t *);
326 extern AUTH *authsys_create_default(void);	/* takes no parameters */
327 extern AUTH *authnone_create(void);		/* takes no parameters */
328 /* Will get obsolete in near future */
329 #define	authunix_create		authsys_create
330 #define	authunix_create_default authsys_create_default
331 #endif /* _KERNEL */
332 
333 /*
334  * DES style authentication
335  * AUTH *authdes_seccreate(servername, window, timehost, ckey)
336  *	const char *servername;		- network name of server
337  *	const uint_t window;			- time to live
338  *	const char *timehost;			- optional hostname to sync with
339  *	const des_block *ckey;		- optional conversation key to use
340  */
341 /* Will get obsolete in near future */
342 #ifdef _KERNEL
343 extern int authdes_create(char *, uint_t, struct netbuf *, struct knetconfig *,
344     des_block *, int, AUTH **retauth);
345 #else /* _KERNEL */
346 extern AUTH *authdes_seccreate(const char *, const uint_t, const  char *,
347     const des_block *);
348 #endif /* _KERNEL */
349 
350 /*
351  *  Netname manipulating functions
352  */
353 
354 #ifdef	_KERNEL
355 extern enum clnt_stat netname2user(char *, uid_t *, gid_t *, int *, gid_t *);
356 #endif
357 extern int getnetname(char *);
358 extern int host2netname(char *, const char *, const char *);
359 extern int user2netname(char *, const uid_t, const char *);
360 #ifndef	_KERNEL
361 extern int netname2user(const char *, uid_t *, gid_t *, int *, gid_t *);
362 #endif
363 extern int netname2host(const char *, char *, const int);
364 
365 /*
366  * These routines interface to the keyserv daemon
367  */
368 
369 #ifdef _KERNEL
370 extern enum clnt_stat key_decryptsession();
371 extern enum clnt_stat key_encryptsession();
372 extern enum clnt_stat key_gendes();
373 extern enum clnt_stat key_getnetname();
374 #endif
375 
376 #ifndef _KERNEL
377 extern int key_decryptsession(const char *, des_block *);
378 extern int key_encryptsession(const char *, des_block *);
379 extern int key_gendes(des_block *);
380 extern int key_setsecret(const char *);
381 extern int key_secretkey_is_set(void);
382 /*
383  * The following routines are private.
384  */
385 extern int key_setnet_ruid();
386 extern int key_setnet_g_ruid();
387 extern int key_removesecret_g_ruid();
388 extern int key_secretkey_is_set_g_ruid();
389 extern AUTH *authsys_create_ruid();
390 #endif
391 
392 
393 /*
394  * Kerberos style authentication
395  * AUTH *authkerb_seccreate(service, srv_inst, realm, window, timehost, status)
396  *	const char *service;			- service name
397  *	const char *srv_inst;			- server instance
398  *	const char *realm;			- server realm
399  *	const uint_t window;			- time to live
400  *	const char *timehost;			- optional hostname to sync with
401  *	int *status;			- kerberos status returned
402  */
403 #ifdef _KERNEL
404 extern int    authkerb_create(char *, char *, char *, uint_t,
405     struct netbuf *, int *, struct knetconfig *, int, AUTH **);
406 #else
407 extern AUTH *authkerb_seccreate(const char *, const char *, const  char *,
408     const uint_t, const char *, int *);
409 #endif /* _KERNEL */
410 
411 /*
412  * Map a kerberos credential into a unix cred.
413  *
414  *  authkerb_getucred(rqst, uid, gid, grouplen, groups)
415  *	const struct svc_req *rqst;		- request pointer
416  *	uid_t *uid;
417  *	gid_t *gid;
418  *	short *grouplen;
419  *	int   *groups;
420  *
421  */
422 struct svc_req;
423 extern int authkerb_getucred(struct svc_req *, uid_t *, gid_t *,
424     short *, int *);
425 
426 #ifdef _KERNEL
427 /*
428  * XDR an opaque authentication struct.  See auth.h.
429  */
430 extern bool_t xdr_opaque_auth(XDR *, struct opaque_auth *);
431 #endif
432 
433 #ifdef _KERNEL
434 extern int authany_wrap(AUTH *, caddr_t, uint_t, XDR *, xdrproc_t, caddr_t);
435 extern int authany_unwrap(AUTH *, XDR *, xdrproc_t, caddr_t);
436 #endif
437 
438 #define	AUTH_NONE	0		/* no authentication */
439 #define	AUTH_NULL	0		/* backward compatibility */
440 #define	AUTH_SYS	1		/* unix style (uid, gids) */
441 #define	AUTH_UNIX	AUTH_SYS
442 #define	AUTH_SHORT	2		/* short hand unix style */
443 #define	AUTH_DH		3		/* for Diffie-Hellman mechanism */
444 #define	AUTH_DES	AUTH_DH		/* for backward compatibility */
445 #define	AUTH_KERB	4		/* kerberos style */
446 #define	RPCSEC_GSS	6		/* GSS-API style */
447 
448 #define	AUTH_LOOPBACK	21982		/* unix style w/ expanded groups */
449 					/* for use over the local transport */
450 
451 #ifdef _KERNEL
452 extern char	loopback_name[];
453 
454 extern zone_key_t	auth_zone_key;
455 extern void *		auth_zone_init(zoneid_t);
456 extern void		auth_zone_fini(zoneid_t, void *);
457 #endif
458 
459 #ifdef	__cplusplus
460 }
461 #endif
462 
463 #endif	/* !_RPC_AUTH_H */
464