xref: /onnv-gate/usr/src/uts/common/rpc/sec/auth_loopb.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28*0Sstevel@tonic-gate /*	  All Rights Reserved  	*/
29*0Sstevel@tonic-gate 
30*0Sstevel@tonic-gate /*
31*0Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley 4.3 BSD
32*0Sstevel@tonic-gate  * under license from the Regents of the University of California.
33*0Sstevel@tonic-gate  */
34*0Sstevel@tonic-gate 
35*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
36*0Sstevel@tonic-gate 
37*0Sstevel@tonic-gate /*
38*0Sstevel@tonic-gate  * auth_loopb.c, implements UNIX style authentication parameters in the
39*0Sstevel@tonic-gate  * kernel.  Interfaces with svc_auth_loopback on the server.  See
40*0Sstevel@tonic-gate  * auth_loopb.c for the user level implementation of the loopback auth.
41*0Sstevel@tonic-gate  *
42*0Sstevel@tonic-gate  */
43*0Sstevel@tonic-gate 
44*0Sstevel@tonic-gate #include <sys/param.h>
45*0Sstevel@tonic-gate #include <sys/time.h>
46*0Sstevel@tonic-gate #include <sys/types.h>
47*0Sstevel@tonic-gate #include <sys/systm.h>
48*0Sstevel@tonic-gate #include <sys/user.h>
49*0Sstevel@tonic-gate #include <sys/proc.h>
50*0Sstevel@tonic-gate #include <sys/utsname.h>
51*0Sstevel@tonic-gate #include <sys/cred.h>
52*0Sstevel@tonic-gate #include <sys/kmem.h>
53*0Sstevel@tonic-gate #include <sys/sysmacros.h>
54*0Sstevel@tonic-gate #include <sys/cmn_err.h>
55*0Sstevel@tonic-gate 
56*0Sstevel@tonic-gate #include <rpc/types.h>
57*0Sstevel@tonic-gate #include <rpc/xdr.h>
58*0Sstevel@tonic-gate #include <rpc/auth.h>
59*0Sstevel@tonic-gate #include <rpc/auth_unix.h>
60*0Sstevel@tonic-gate #include <rpc/clnt.h>
61*0Sstevel@tonic-gate #include <rpc/rpc_msg.h>
62*0Sstevel@tonic-gate 
63*0Sstevel@tonic-gate /*
64*0Sstevel@tonic-gate  * Unix authenticator operations vector
65*0Sstevel@tonic-gate  */
66*0Sstevel@tonic-gate static void	authloopback_nextverf(AUTH *);
67*0Sstevel@tonic-gate static bool_t	authloopback_marshal(AUTH *, XDR *, struct cred *);
68*0Sstevel@tonic-gate static bool_t	authloopback_validate(AUTH *, struct opaque_auth *);
69*0Sstevel@tonic-gate static bool_t	authloopback_refresh(AUTH *, struct rpc_msg *, cred_t *);
70*0Sstevel@tonic-gate static void	authloopback_destroy(AUTH *);
71*0Sstevel@tonic-gate 
72*0Sstevel@tonic-gate static struct auth_ops authloopback_ops = {
73*0Sstevel@tonic-gate 	authloopback_nextverf,
74*0Sstevel@tonic-gate 	authloopback_marshal,
75*0Sstevel@tonic-gate 	authloopback_validate,
76*0Sstevel@tonic-gate 	authloopback_refresh,
77*0Sstevel@tonic-gate 	authloopback_destroy,
78*0Sstevel@tonic-gate 	authany_wrap,
79*0Sstevel@tonic-gate 	authany_unwrap
80*0Sstevel@tonic-gate };
81*0Sstevel@tonic-gate 
82*0Sstevel@tonic-gate /*
83*0Sstevel@tonic-gate  * Create a kernel unix style authenticator.
84*0Sstevel@tonic-gate  * Returns an auth handle.
85*0Sstevel@tonic-gate  */
86*0Sstevel@tonic-gate AUTH *
authloopback_create(void)87*0Sstevel@tonic-gate authloopback_create(void)
88*0Sstevel@tonic-gate {
89*0Sstevel@tonic-gate 	/*
90*0Sstevel@tonic-gate 	 * Allocate and set up auth handle
91*0Sstevel@tonic-gate 	 */
92*0Sstevel@tonic-gate 	return (kmem_cache_alloc(authloopback_cache, KM_SLEEP));
93*0Sstevel@tonic-gate }
94*0Sstevel@tonic-gate 
95*0Sstevel@tonic-gate /*
96*0Sstevel@tonic-gate  *  The constructor of the authloopback_cache.
97*0Sstevel@tonic-gate  */
98*0Sstevel@tonic-gate /* ARGSUSED */
99*0Sstevel@tonic-gate int
authloopback_init(void * buf,void * cdrarg,int kmflags)100*0Sstevel@tonic-gate authloopback_init(void *buf, void *cdrarg, int kmflags)
101*0Sstevel@tonic-gate {
102*0Sstevel@tonic-gate 	AUTH *auth = (AUTH *)buf;
103*0Sstevel@tonic-gate 
104*0Sstevel@tonic-gate 	auth->ah_ops = &authloopback_ops;
105*0Sstevel@tonic-gate 	auth->ah_cred.oa_flavor = AUTH_LOOPBACK;
106*0Sstevel@tonic-gate 	auth->ah_verf = _null_auth;
107*0Sstevel@tonic-gate 
108*0Sstevel@tonic-gate 	return (0);
109*0Sstevel@tonic-gate }
110*0Sstevel@tonic-gate 
111*0Sstevel@tonic-gate /*
112*0Sstevel@tonic-gate  * authloopback operations
113*0Sstevel@tonic-gate  */
114*0Sstevel@tonic-gate /* ARGSUSED */
115*0Sstevel@tonic-gate static void
authloopback_nextverf(AUTH * auth)116*0Sstevel@tonic-gate authloopback_nextverf(AUTH *auth)
117*0Sstevel@tonic-gate {
118*0Sstevel@tonic-gate 
119*0Sstevel@tonic-gate 	/* no action necessary */
120*0Sstevel@tonic-gate }
121*0Sstevel@tonic-gate 
122*0Sstevel@tonic-gate static bool_t
authloopback_marshal(AUTH * auth,XDR * xdrs,struct cred * cr)123*0Sstevel@tonic-gate authloopback_marshal(AUTH *auth, XDR *xdrs, struct cred *cr)
124*0Sstevel@tonic-gate {
125*0Sstevel@tonic-gate 	char *sercred;
126*0Sstevel@tonic-gate 	XDR xdrm;
127*0Sstevel@tonic-gate 	struct opaque_auth *cred;
128*0Sstevel@tonic-gate 	bool_t ret = FALSE;
129*0Sstevel@tonic-gate 	const gid_t *gp, *gpend;
130*0Sstevel@tonic-gate 	int gidlen, credsize, namelen, rounded_namelen;
131*0Sstevel@tonic-gate 	int32_t *ptr;
132*0Sstevel@tonic-gate 
133*0Sstevel@tonic-gate 	/*
134*0Sstevel@tonic-gate 	 * First we try a fast path to get through
135*0Sstevel@tonic-gate 	 * this very common operation.
136*0Sstevel@tonic-gate 	 */
137*0Sstevel@tonic-gate 	gp = crgetgroups(cr);
138*0Sstevel@tonic-gate 	gidlen = crgetngroups(cr);
139*0Sstevel@tonic-gate 	if (gidlen > NGRPS_LOOPBACK)
140*0Sstevel@tonic-gate 		return (FALSE);
141*0Sstevel@tonic-gate 	gpend = &gp[gidlen-1];
142*0Sstevel@tonic-gate 
143*0Sstevel@tonic-gate 	namelen = (int)strlen(uts_nodename());
144*0Sstevel@tonic-gate 	rounded_namelen = RNDUP(namelen);
145*0Sstevel@tonic-gate 	credsize = 4 + 4 + rounded_namelen + 4 + 4 + 4 + gidlen * 4;
146*0Sstevel@tonic-gate 	ptr = XDR_INLINE(xdrs, 4 + 4 + credsize + 4 + 4);
147*0Sstevel@tonic-gate 	if (ptr) {
148*0Sstevel@tonic-gate 		/*
149*0Sstevel@tonic-gate 		 * We can do the fast path.
150*0Sstevel@tonic-gate 		 */
151*0Sstevel@tonic-gate 		IXDR_PUT_INT32(ptr, AUTH_LOOPBACK);	/* cred flavor */
152*0Sstevel@tonic-gate 		IXDR_PUT_INT32(ptr, credsize);	/* cred len */
153*0Sstevel@tonic-gate 		IXDR_PUT_INT32(ptr, gethrestime_sec());
154*0Sstevel@tonic-gate 		IXDR_PUT_INT32(ptr, namelen);
155*0Sstevel@tonic-gate 		bcopy(uts_nodename(), ptr, namelen);
156*0Sstevel@tonic-gate 		if (rounded_namelen - namelen)
157*0Sstevel@tonic-gate 			bzero(((caddr_t)ptr) + namelen,
158*0Sstevel@tonic-gate 				rounded_namelen - namelen);
159*0Sstevel@tonic-gate 		ptr += rounded_namelen / BYTES_PER_XDR_UNIT;
160*0Sstevel@tonic-gate 		IXDR_PUT_INT32(ptr, crgetuid(cr));
161*0Sstevel@tonic-gate 		IXDR_PUT_INT32(ptr, crgetgid(cr));
162*0Sstevel@tonic-gate 		IXDR_PUT_INT32(ptr, gidlen);
163*0Sstevel@tonic-gate 		while (gp <= gpend) {
164*0Sstevel@tonic-gate 			IXDR_PUT_INT32(ptr, *gp++);
165*0Sstevel@tonic-gate 		}
166*0Sstevel@tonic-gate 		IXDR_PUT_INT32(ptr, AUTH_NULL);	/* verf flavor */
167*0Sstevel@tonic-gate 		IXDR_PUT_INT32(ptr, 0);	/* verf len */
168*0Sstevel@tonic-gate 		return (TRUE);
169*0Sstevel@tonic-gate 	}
170*0Sstevel@tonic-gate 	sercred = kmem_alloc(MAX_AUTH_BYTES, KM_SLEEP);
171*0Sstevel@tonic-gate 	/*
172*0Sstevel@tonic-gate 	 * serialize u struct stuff into sercred
173*0Sstevel@tonic-gate 	 */
174*0Sstevel@tonic-gate 	xdrmem_create(&xdrm, sercred, MAX_AUTH_BYTES, XDR_ENCODE);
175*0Sstevel@tonic-gate 	if (!xdr_authloopback(&xdrm)) {
176*0Sstevel@tonic-gate 		printf("authloopback_marshal: xdr_authloopback failed\n");
177*0Sstevel@tonic-gate 		ret = FALSE;
178*0Sstevel@tonic-gate 		goto done;
179*0Sstevel@tonic-gate 	}
180*0Sstevel@tonic-gate 
181*0Sstevel@tonic-gate 	/*
182*0Sstevel@tonic-gate 	 * Make opaque auth credentials that point at serialized u struct
183*0Sstevel@tonic-gate 	 */
184*0Sstevel@tonic-gate 	cred = &(auth->ah_cred);
185*0Sstevel@tonic-gate 	cred->oa_length = XDR_GETPOS(&xdrm);
186*0Sstevel@tonic-gate 	cred->oa_base = sercred;
187*0Sstevel@tonic-gate 
188*0Sstevel@tonic-gate 	/*
189*0Sstevel@tonic-gate 	 * serialize credentials and verifiers (null)
190*0Sstevel@tonic-gate 	 */
191*0Sstevel@tonic-gate 	if ((xdr_opaque_auth(xdrs, &(auth->ah_cred))) &&
192*0Sstevel@tonic-gate 	    (xdr_opaque_auth(xdrs, &(auth->ah_verf))))
193*0Sstevel@tonic-gate 		ret = TRUE;
194*0Sstevel@tonic-gate 	else
195*0Sstevel@tonic-gate 		ret = FALSE;
196*0Sstevel@tonic-gate done:
197*0Sstevel@tonic-gate 	kmem_free(sercred, MAX_AUTH_BYTES);
198*0Sstevel@tonic-gate 	return (ret);
199*0Sstevel@tonic-gate }
200*0Sstevel@tonic-gate 
201*0Sstevel@tonic-gate /* ARGSUSED */
202*0Sstevel@tonic-gate static bool_t
authloopback_validate(AUTH * auth,struct opaque_auth * verf)203*0Sstevel@tonic-gate authloopback_validate(AUTH *auth, struct opaque_auth *verf)
204*0Sstevel@tonic-gate {
205*0Sstevel@tonic-gate 	return (TRUE);
206*0Sstevel@tonic-gate }
207*0Sstevel@tonic-gate 
208*0Sstevel@tonic-gate /* ARGSUSED */
209*0Sstevel@tonic-gate static bool_t
authloopback_refresh(AUTH * auth,struct rpc_msg * msg,cred_t * cr)210*0Sstevel@tonic-gate authloopback_refresh(AUTH *auth, struct rpc_msg *msg, cred_t *cr)
211*0Sstevel@tonic-gate {
212*0Sstevel@tonic-gate 	return (FALSE);
213*0Sstevel@tonic-gate }
214*0Sstevel@tonic-gate 
215*0Sstevel@tonic-gate static void
authloopback_destroy(register AUTH * auth)216*0Sstevel@tonic-gate authloopback_destroy(register AUTH *auth)
217*0Sstevel@tonic-gate {
218*0Sstevel@tonic-gate 	kmem_cache_free(authloopback_cache, auth);
219*0Sstevel@tonic-gate }
220