xref: /onnv-gate/usr/src/stand/lib/fs/nfs/auth_unix.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 2004 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) 1983, 1984, 1985, 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  * Adapted for use by the boot program.
39*0Sstevel@tonic-gate  *
40*0Sstevel@tonic-gate  * auth_unix.c, Implements UNIX style authentication parameters.
41*0Sstevel@tonic-gate  *
42*0Sstevel@tonic-gate  * The system is very weak.  The client uses no encryption for its
43*0Sstevel@tonic-gate  * credentials and only sends null verifiers.  The server sends backs
44*0Sstevel@tonic-gate  * null verifiers or optionally a verifier that suggests a new short hand
45*0Sstevel@tonic-gate  * for the credentials.
46*0Sstevel@tonic-gate  */
47*0Sstevel@tonic-gate 
48*0Sstevel@tonic-gate #include <stdlib.h>
49*0Sstevel@tonic-gate #include <sys/sysmacros.h>
50*0Sstevel@tonic-gate #include <rpc/types.h>
51*0Sstevel@tonic-gate #include <rpc/xdr.h>
52*0Sstevel@tonic-gate #include <rpc/auth.h>
53*0Sstevel@tonic-gate #include "clnt.h"
54*0Sstevel@tonic-gate #include <rpc/auth_unix.h>
55*0Sstevel@tonic-gate #include <sys/promif.h>
56*0Sstevel@tonic-gate #include <sys/salib.h>
57*0Sstevel@tonic-gate #include <sys/bootdebug.h>
58*0Sstevel@tonic-gate #include "nfs_inet.h"
59*0Sstevel@tonic-gate 
60*0Sstevel@tonic-gate static struct auth_ops *authunix_ops();
61*0Sstevel@tonic-gate /*
62*0Sstevel@tonic-gate  * This struct is pointed to by the ah_private field of an auth_handle.
63*0Sstevel@tonic-gate  */
64*0Sstevel@tonic-gate struct audata {
65*0Sstevel@tonic-gate 	struct opaque_auth	au_origcred;	/* original credentials */
66*0Sstevel@tonic-gate 	struct opaque_auth	au_shcred;	/* short hand cred */
67*0Sstevel@tonic-gate 	uint_t			au_shfaults;	/* short hand cache faults */
68*0Sstevel@tonic-gate 	char			au_marshed[MAX_AUTH_BYTES];
69*0Sstevel@tonic-gate 	uint_t			au_mpos;	/* xdr pos at end of marshed */
70*0Sstevel@tonic-gate };
71*0Sstevel@tonic-gate #define	AUTH_PRIVATE(auth)	((struct audata *)auth->ah_private)
72*0Sstevel@tonic-gate 
73*0Sstevel@tonic-gate static void marshal_new_auth(AUTH *);
74*0Sstevel@tonic-gate 
75*0Sstevel@tonic-gate #define	dprintf	if (boothowto & RB_DEBUG) printf
76*0Sstevel@tonic-gate 
77*0Sstevel@tonic-gate /*
78*0Sstevel@tonic-gate  * Create a unix style authenticator.
79*0Sstevel@tonic-gate  * Returns an auth handle with the given stuff in it.
80*0Sstevel@tonic-gate  */
81*0Sstevel@tonic-gate AUTH *
authunix_create(char * machname,uid_t uid,gid_t gid,int len,gid_t * aup_gids)82*0Sstevel@tonic-gate authunix_create(char *machname, uid_t uid, gid_t gid, int len, gid_t *aup_gids)
83*0Sstevel@tonic-gate {
84*0Sstevel@tonic-gate 	struct authunix_parms aup;
85*0Sstevel@tonic-gate 	char mymem[MAX_AUTH_BYTES];
86*0Sstevel@tonic-gate 	XDR xdrs;
87*0Sstevel@tonic-gate 	AUTH *auth;
88*0Sstevel@tonic-gate 	struct audata *au;
89*0Sstevel@tonic-gate 
90*0Sstevel@tonic-gate 	/*
91*0Sstevel@tonic-gate 	 * Allocate and set up auth handle
92*0Sstevel@tonic-gate 	 */
93*0Sstevel@tonic-gate 	auth = (AUTH *) bkmem_alloc(sizeof (*auth));
94*0Sstevel@tonic-gate 	if (auth == NULL) {
95*0Sstevel@tonic-gate 		prom_panic("authunix_create: Cannot allocate memory.");
96*0Sstevel@tonic-gate 		return (NULL);
97*0Sstevel@tonic-gate 	}
98*0Sstevel@tonic-gate 
99*0Sstevel@tonic-gate 	au = (struct audata *)bkmem_alloc(sizeof (*au));
100*0Sstevel@tonic-gate 	if (au == NULL) {
101*0Sstevel@tonic-gate 		prom_panic("authunix_create: Cannot allocate memory.");
102*0Sstevel@tonic-gate 		return (NULL);
103*0Sstevel@tonic-gate 	}
104*0Sstevel@tonic-gate 
105*0Sstevel@tonic-gate 	/* setup authenticator. */
106*0Sstevel@tonic-gate 	auth->ah_ops = authunix_ops();
107*0Sstevel@tonic-gate 	auth->ah_private = (caddr_t)au;
108*0Sstevel@tonic-gate 
109*0Sstevel@tonic-gate 	/* structure copies */
110*0Sstevel@tonic-gate 	auth->ah_verf = au->au_shcred = _null_auth;
111*0Sstevel@tonic-gate 
112*0Sstevel@tonic-gate 	au->au_shfaults = 0;
113*0Sstevel@tonic-gate 
114*0Sstevel@tonic-gate 	/*
115*0Sstevel@tonic-gate 	 * fill in param struct from the given params
116*0Sstevel@tonic-gate 	 */
117*0Sstevel@tonic-gate 	aup.aup_time = prom_gettime() / 1000;
118*0Sstevel@tonic-gate 	aup.aup_machname = machname;
119*0Sstevel@tonic-gate 	aup.aup_uid = uid;
120*0Sstevel@tonic-gate 	aup.aup_gid = gid;
121*0Sstevel@tonic-gate 	aup.aup_len = (uint_t)len;
122*0Sstevel@tonic-gate 	aup.aup_gids = (gid_t *)aup_gids;
123*0Sstevel@tonic-gate 
124*0Sstevel@tonic-gate 	/*
125*0Sstevel@tonic-gate 	 * Serialize the parameters into origcred
126*0Sstevel@tonic-gate 	 */
127*0Sstevel@tonic-gate 	xdrmem_create(&xdrs, mymem, MAX_AUTH_BYTES, XDR_ENCODE);
128*0Sstevel@tonic-gate 	if (!xdr_authunix_parms(&xdrs, &aup)) {
129*0Sstevel@tonic-gate 		prom_panic("authunix_create:  xdr_authunix_parms failed");
130*0Sstevel@tonic-gate 		bkmem_free(auth->ah_private, sizeof (struct audata));
131*0Sstevel@tonic-gate 		bkmem_free((caddr_t)auth, sizeof (*auth));
132*0Sstevel@tonic-gate 		return ((AUTH *)0);
133*0Sstevel@tonic-gate 	}
134*0Sstevel@tonic-gate 	au->au_origcred.oa_length = len = XDR_GETPOS(&xdrs);
135*0Sstevel@tonic-gate 	au->au_origcred.oa_flavor = (uint_t)AUTH_UNIX;
136*0Sstevel@tonic-gate 	if ((au->au_origcred.oa_base = bkmem_alloc((uint_t)len)) == NULL) {
137*0Sstevel@tonic-gate 		prom_panic("authunix_create: memory alloc failed");
138*0Sstevel@tonic-gate 		bkmem_free(auth->ah_private, sizeof (struct audata));
139*0Sstevel@tonic-gate 		bkmem_free((caddr_t)auth, sizeof (*auth));
140*0Sstevel@tonic-gate 		return ((AUTH *)0);
141*0Sstevel@tonic-gate 	}
142*0Sstevel@tonic-gate 	(void) bcopy(mymem, au->au_origcred.oa_base, (uint_t)len);
143*0Sstevel@tonic-gate 
144*0Sstevel@tonic-gate 	/*
145*0Sstevel@tonic-gate 	 * set auth handle to reflect new cred.
146*0Sstevel@tonic-gate 	 */
147*0Sstevel@tonic-gate 	auth->ah_cred = au->au_origcred;
148*0Sstevel@tonic-gate 	marshal_new_auth(auth);
149*0Sstevel@tonic-gate 	return (auth);
150*0Sstevel@tonic-gate }
151*0Sstevel@tonic-gate 
152*0Sstevel@tonic-gate /*
153*0Sstevel@tonic-gate  * authunix operations
154*0Sstevel@tonic-gate  */
155*0Sstevel@tonic-gate 
156*0Sstevel@tonic-gate /* ARGSUSED */
157*0Sstevel@tonic-gate static void
authunix_nextverf(AUTH * auth)158*0Sstevel@tonic-gate authunix_nextverf(AUTH *auth)
159*0Sstevel@tonic-gate {
160*0Sstevel@tonic-gate }
161*0Sstevel@tonic-gate 
162*0Sstevel@tonic-gate /* ARGSUSED */
163*0Sstevel@tonic-gate static bool_t
authunix_marshal(AUTH * auth,XDR * xdrs,cred_t * cr)164*0Sstevel@tonic-gate authunix_marshal(AUTH *auth, XDR *xdrs, cred_t *cr)
165*0Sstevel@tonic-gate {
166*0Sstevel@tonic-gate 	struct audata *au = AUTH_PRIVATE(auth);
167*0Sstevel@tonic-gate 
168*0Sstevel@tonic-gate 	return (XDR_PUTBYTES(xdrs, au->au_marshed, au->au_mpos));
169*0Sstevel@tonic-gate }
170*0Sstevel@tonic-gate 
171*0Sstevel@tonic-gate static bool_t
authunix_validate(AUTH * auth,struct opaque_auth * verf)172*0Sstevel@tonic-gate authunix_validate(AUTH *auth, struct opaque_auth *verf)
173*0Sstevel@tonic-gate {
174*0Sstevel@tonic-gate 	struct audata *au;
175*0Sstevel@tonic-gate 	XDR xdrs;
176*0Sstevel@tonic-gate 
177*0Sstevel@tonic-gate 	if (verf->oa_flavor == AUTH_SHORT) {
178*0Sstevel@tonic-gate 		au = AUTH_PRIVATE(auth);
179*0Sstevel@tonic-gate 
180*0Sstevel@tonic-gate 
181*0Sstevel@tonic-gate 		xdrmem_create(&xdrs, verf->oa_base, verf->oa_length,
182*0Sstevel@tonic-gate 		    XDR_DECODE);
183*0Sstevel@tonic-gate 
184*0Sstevel@tonic-gate 		if (xdr_opaque_auth(&xdrs, &au->au_shcred)) {
185*0Sstevel@tonic-gate 			auth->ah_cred = au->au_shcred;
186*0Sstevel@tonic-gate 		} else {
187*0Sstevel@tonic-gate 			xdrs.x_op = XDR_FREE;
188*0Sstevel@tonic-gate 			(void) xdr_opaque_auth(&xdrs, &au->au_shcred);
189*0Sstevel@tonic-gate 			au->au_shcred.oa_base = 0;
190*0Sstevel@tonic-gate 			auth->ah_cred = au->au_origcred;
191*0Sstevel@tonic-gate 		}
192*0Sstevel@tonic-gate 		marshal_new_auth(auth);
193*0Sstevel@tonic-gate 	}
194*0Sstevel@tonic-gate 
195*0Sstevel@tonic-gate 	return (TRUE);
196*0Sstevel@tonic-gate }
197*0Sstevel@tonic-gate 
198*0Sstevel@tonic-gate /*ARGSUSED*/
199*0Sstevel@tonic-gate static bool_t
authunix_refresh(AUTH * auth,struct rpc_msg * msg,cred_t * cr)200*0Sstevel@tonic-gate authunix_refresh(AUTH *auth, struct rpc_msg *msg, cred_t *cr)
201*0Sstevel@tonic-gate {
202*0Sstevel@tonic-gate 	struct audata *au = AUTH_PRIVATE(auth);
203*0Sstevel@tonic-gate 	struct authunix_parms aup;
204*0Sstevel@tonic-gate 	XDR xdrs;
205*0Sstevel@tonic-gate 	int stat;
206*0Sstevel@tonic-gate 
207*0Sstevel@tonic-gate 	if (auth->ah_cred.oa_base == au->au_origcred.oa_base) {
208*0Sstevel@tonic-gate 		/* there is no hope.  Punt */
209*0Sstevel@tonic-gate 		return (FALSE);
210*0Sstevel@tonic-gate 	}
211*0Sstevel@tonic-gate 	au->au_shfaults ++;
212*0Sstevel@tonic-gate 
213*0Sstevel@tonic-gate 	/* first deserialize the creds back into a struct authunix_parms */
214*0Sstevel@tonic-gate 	aup.aup_machname = (char *)0;
215*0Sstevel@tonic-gate 	aup.aup_gids = (gid_t *)0;
216*0Sstevel@tonic-gate 	xdrmem_create(&xdrs, au->au_origcred.oa_base,
217*0Sstevel@tonic-gate 			au->au_origcred.oa_length, XDR_DECODE);
218*0Sstevel@tonic-gate 	stat = xdr_authunix_parms(&xdrs, &aup);
219*0Sstevel@tonic-gate 	if (!stat)
220*0Sstevel@tonic-gate 		goto done;
221*0Sstevel@tonic-gate 
222*0Sstevel@tonic-gate 	/* update the time and serialize in place */
223*0Sstevel@tonic-gate 	aup.aup_time = (prom_gettime() / 1000);
224*0Sstevel@tonic-gate 	xdrs.x_op = XDR_ENCODE;
225*0Sstevel@tonic-gate 	XDR_SETPOS(&xdrs, 0);
226*0Sstevel@tonic-gate 	stat = xdr_authunix_parms(&xdrs, &aup);
227*0Sstevel@tonic-gate 	if (!stat)
228*0Sstevel@tonic-gate 		goto done;
229*0Sstevel@tonic-gate 	auth->ah_cred = au->au_origcred;
230*0Sstevel@tonic-gate 	marshal_new_auth(auth);
231*0Sstevel@tonic-gate done:
232*0Sstevel@tonic-gate 	/* free the struct authunix_parms created by deserializing */
233*0Sstevel@tonic-gate 	xdrs.x_op = XDR_FREE;
234*0Sstevel@tonic-gate 	(void) xdr_authunix_parms(&xdrs, &aup);
235*0Sstevel@tonic-gate 	XDR_DESTROY(&xdrs);
236*0Sstevel@tonic-gate 	return (stat);
237*0Sstevel@tonic-gate }
238*0Sstevel@tonic-gate 
239*0Sstevel@tonic-gate static void
authunix_destroy(AUTH * auth)240*0Sstevel@tonic-gate authunix_destroy(AUTH *auth)
241*0Sstevel@tonic-gate {
242*0Sstevel@tonic-gate 	struct audata *au = AUTH_PRIVATE(auth);
243*0Sstevel@tonic-gate 
244*0Sstevel@tonic-gate 	if (au->au_shcred.oa_base != NULL)
245*0Sstevel@tonic-gate 		bkmem_free(au->au_shcred.oa_base, au->au_shcred.oa_length);
246*0Sstevel@tonic-gate 	bkmem_free(auth->ah_private, sizeof (struct audata));
247*0Sstevel@tonic-gate 	if (auth->ah_verf.oa_base != NULL)
248*0Sstevel@tonic-gate 		bkmem_free(auth->ah_verf.oa_base, auth->ah_verf.oa_length);
249*0Sstevel@tonic-gate 	bkmem_free((caddr_t)auth, sizeof (*auth));
250*0Sstevel@tonic-gate }
251*0Sstevel@tonic-gate 
252*0Sstevel@tonic-gate /*
253*0Sstevel@tonic-gate  * Marshals (pre-serializes) an auth struct.
254*0Sstevel@tonic-gate  * sets private data, au_marshed and au_mpos
255*0Sstevel@tonic-gate  */
256*0Sstevel@tonic-gate static void
marshal_new_auth(AUTH * auth)257*0Sstevel@tonic-gate marshal_new_auth(AUTH *auth)
258*0Sstevel@tonic-gate {
259*0Sstevel@tonic-gate 	XDR xdr_stream;
260*0Sstevel@tonic-gate 	XDR *xdrs = &xdr_stream;
261*0Sstevel@tonic-gate 	struct audata *au = AUTH_PRIVATE(auth);
262*0Sstevel@tonic-gate 
263*0Sstevel@tonic-gate 	xdrmem_create(xdrs, au->au_marshed, MAX_AUTH_BYTES, XDR_ENCODE);
264*0Sstevel@tonic-gate 	if ((!xdr_opaque_auth(xdrs, &(auth->ah_cred))) ||
265*0Sstevel@tonic-gate 	    (!xdr_opaque_auth(xdrs, &(auth->ah_verf)))) {
266*0Sstevel@tonic-gate 		dprintf("marshal_new_auth - Fatal marshalling problem");
267*0Sstevel@tonic-gate 	} else {
268*0Sstevel@tonic-gate 		au->au_mpos = XDR_GETPOS(xdrs);
269*0Sstevel@tonic-gate 	}
270*0Sstevel@tonic-gate 	XDR_DESTROY(xdrs);
271*0Sstevel@tonic-gate }
272*0Sstevel@tonic-gate 
273*0Sstevel@tonic-gate 
274*0Sstevel@tonic-gate static struct auth_ops *
authunix_ops(void)275*0Sstevel@tonic-gate authunix_ops(void)
276*0Sstevel@tonic-gate {
277*0Sstevel@tonic-gate 	static struct auth_ops ops;
278*0Sstevel@tonic-gate 
279*0Sstevel@tonic-gate 	if (ops.ah_nextverf == 0) {
280*0Sstevel@tonic-gate 		ops.ah_nextverf = authunix_nextverf;
281*0Sstevel@tonic-gate 		ops.ah_marshal = authunix_marshal;
282*0Sstevel@tonic-gate 		ops.ah_validate = authunix_validate;
283*0Sstevel@tonic-gate 		ops.ah_refresh = authunix_refresh;
284*0Sstevel@tonic-gate 		ops.ah_destroy = authunix_destroy;
285*0Sstevel@tonic-gate 	}
286*0Sstevel@tonic-gate 	return (&ops);
287*0Sstevel@tonic-gate }
288*0Sstevel@tonic-gate 
289*0Sstevel@tonic-gate /*
290*0Sstevel@tonic-gate  * XDR for unix authentication parameters.
291*0Sstevel@tonic-gate  */
292*0Sstevel@tonic-gate bool_t
xdr_authunix_parms(XDR * xdrs,struct authunix_parms * p)293*0Sstevel@tonic-gate xdr_authunix_parms(XDR *xdrs, struct authunix_parms *p)
294*0Sstevel@tonic-gate {
295*0Sstevel@tonic-gate 	if (xdr_u_int(xdrs, &(p->aup_time)) &&
296*0Sstevel@tonic-gate 	    xdr_string(xdrs, &(p->aup_machname), MAX_MACHINE_NAME) &&
297*0Sstevel@tonic-gate 	    xdr_int(xdrs, (int *)&(p->aup_uid)) &&
298*0Sstevel@tonic-gate 	    xdr_int(xdrs, (int *)&(p->aup_gid)) &&
299*0Sstevel@tonic-gate 	    xdr_array(xdrs, (caddr_t *)&(p->aup_gids),
300*0Sstevel@tonic-gate 		    &(p->aup_len), NGRPS, sizeof (int), xdr_int)) {
301*0Sstevel@tonic-gate 		return (TRUE);
302*0Sstevel@tonic-gate 	}
303*0Sstevel@tonic-gate 	return (FALSE);
304*0Sstevel@tonic-gate }
305