xref: /freebsd/sys/rpc/auth_unix.c (revision db612abe8df3355d1eb23bb3b50fdd97bc21e979)
1 /*	$NetBSD: auth_unix.c,v 1.18 2000/07/06 03:03:30 christos Exp $	*/
2 
3 /*
4  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
5  * unrestricted use provided that this legend is included on all tape
6  * media and as a part of the software program in whole or part.  Users
7  * may copy or modify Sun RPC without charge, but are not authorized
8  * to license or distribute it to anyone else except as part of a product or
9  * program developed by the user.
10  *
11  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
12  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
13  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
14  *
15  * Sun RPC is provided with no support and without any obligation on the
16  * part of Sun Microsystems, Inc. to assist in its use, correction,
17  * modification or enhancement.
18  *
19  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
20  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
21  * OR ANY PART THEREOF.
22  *
23  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
24  * or profits or other special, indirect and consequential damages, even if
25  * Sun has been advised of the possibility of such damages.
26  *
27  * Sun Microsystems, Inc.
28  * 2550 Garcia Avenue
29  * Mountain View, California  94043
30  */
31 
32 #if defined(LIBC_SCCS) && !defined(lint)
33 static char *sccsid2 = "@(#)auth_unix.c 1.19 87/08/11 Copyr 1984 Sun Micro";
34 static char *sccsid = "@(#)auth_unix.c	2.2 88/08/01 4.0 RPCSRC";
35 #endif
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
38 
39 /*
40  * auth_unix.c, Implements UNIX style authentication parameters.
41  *
42  * Copyright (C) 1984, Sun Microsystems, Inc.
43  *
44  * The system is very weak.  The client uses no encryption for it's
45  * credentials and only sends null verifiers.  The server sends backs
46  * null verifiers or optionally a verifier that suggests a new short hand
47  * for the credentials.
48  *
49  */
50 
51 #include <sys/param.h>
52 #include <sys/systm.h>
53 #include <sys/lock.h>
54 #include <sys/malloc.h>
55 #include <sys/mutex.h>
56 #include <sys/ucred.h>
57 
58 #include <rpc/types.h>
59 #include <rpc/xdr.h>
60 #include <rpc/auth.h>
61 
62 #include <rpc/rpc_com.h>
63 
64 /* auth_unix.c */
65 static void authunix_nextverf (AUTH *);
66 static bool_t authunix_marshal (AUTH *, XDR *);
67 static bool_t authunix_validate (AUTH *, struct opaque_auth *);
68 static bool_t authunix_refresh (AUTH *, void *);
69 static void authunix_destroy (AUTH *);
70 static void marshal_new_auth (AUTH *);
71 
72 static struct auth_ops authunix_ops = {
73 	.ah_nextverf =		authunix_nextverf,
74 	.ah_marshal =		authunix_marshal,
75 	.ah_validate =		authunix_validate,
76 	.ah_refresh =		authunix_refresh,
77 	.ah_destroy =		authunix_destroy
78 };
79 
80 /*
81  * This struct is pointed to by the ah_private field of an auth_handle.
82  */
83 struct audata {
84 	struct opaque_auth	au_origcred;	/* original credentials */
85 	struct opaque_auth	au_shcred;	/* short hand cred */
86 	u_long			au_shfaults;	/* short hand cache faults */
87 	char			au_marshed[MAX_AUTH_BYTES];
88 	u_int			au_mpos;	/* xdr pos at end of marshed */
89 };
90 #define	AUTH_PRIVATE(auth)	((struct audata *)auth->ah_private)
91 
92 /*
93  * Create a unix style authenticator.
94  * Returns an auth handle with the given stuff in it.
95  */
96 AUTH *
97 authunix_create(struct ucred *cred)
98 {
99 	struct xucred xcr;
100 	char mymem[MAX_AUTH_BYTES];
101 	XDR xdrs;
102 	AUTH *auth;
103 	struct audata *au;
104 	struct timeval now;
105 	uint32_t time;
106 	int len;
107 
108 	/*
109 	 * Allocate and set up auth handle
110 	 */
111 	au = NULL;
112 	auth = mem_alloc(sizeof(*auth));
113 #ifndef _KERNEL
114 	if (auth == NULL) {
115 		printf("authunix_create: out of memory");
116 		goto cleanup_authunix_create;
117 	}
118 #endif
119 	au = mem_alloc(sizeof(*au));
120 #ifndef _KERNEL
121 	if (au == NULL) {
122 		printf("authunix_create: out of memory");
123 		goto cleanup_authunix_create;
124 	}
125 #endif
126 	auth->ah_ops = &authunix_ops;
127 	auth->ah_private = (caddr_t)au;
128 	auth->ah_verf = au->au_shcred = _null_auth;
129 	au->au_shfaults = 0;
130 	au->au_origcred.oa_base = NULL;
131 
132 	getmicrotime(&now);
133 	time = now.tv_sec;
134 
135 	/*
136 	 * Serialize the parameters into origcred
137 	 */
138 	xdrmem_create(&xdrs, mymem, MAX_AUTH_BYTES, XDR_ENCODE);
139 	cru2x(cred, &xcr);
140 	if (! xdr_authunix_parms(&xdrs, &time, &xcr))
141 		panic("authunix_create: failed to encode creds");
142 	au->au_origcred.oa_length = len = XDR_GETPOS(&xdrs);
143 	au->au_origcred.oa_flavor = AUTH_UNIX;
144 #ifdef _KERNEL
145 	au->au_origcred.oa_base = mem_alloc((u_int) len);
146 #else
147 	if ((au->au_origcred.oa_base = mem_alloc((u_int) len)) == NULL) {
148 		printf("authunix_create: out of memory");
149 		goto cleanup_authunix_create;
150 	}
151 #endif
152 	memcpy(au->au_origcred.oa_base, mymem, (size_t)len);
153 
154 	/*
155 	 * set auth handle to reflect new cred.
156 	 */
157 	auth->ah_cred = au->au_origcred;
158 	marshal_new_auth(auth);
159 	return (auth);
160 #ifndef _KERNEL
161  cleanup_authunix_create:
162 	if (auth)
163 		mem_free(auth, sizeof(*auth));
164 	if (au) {
165 		if (au->au_origcred.oa_base)
166 			mem_free(au->au_origcred.oa_base, (u_int)len);
167 		mem_free(au, sizeof(*au));
168 	}
169 	return (NULL);
170 #endif
171 }
172 
173 /*
174  * authunix operations
175  */
176 
177 /* ARGSUSED */
178 static void
179 authunix_nextverf(AUTH *auth)
180 {
181 	/* no action necessary */
182 }
183 
184 static bool_t
185 authunix_marshal(AUTH *auth, XDR *xdrs)
186 {
187 	struct audata *au;
188 
189 	au = AUTH_PRIVATE(auth);
190 	return (XDR_PUTBYTES(xdrs, au->au_marshed, au->au_mpos));
191 }
192 
193 static bool_t
194 authunix_validate(AUTH *auth, struct opaque_auth *verf)
195 {
196 	struct audata *au;
197 	XDR xdrs;
198 
199 	if (verf->oa_flavor == AUTH_SHORT) {
200 		au = AUTH_PRIVATE(auth);
201 		xdrmem_create(&xdrs, verf->oa_base, verf->oa_length,
202 		    XDR_DECODE);
203 
204 		if (au->au_shcred.oa_base != NULL) {
205 			mem_free(au->au_shcred.oa_base,
206 			    au->au_shcred.oa_length);
207 			au->au_shcred.oa_base = NULL;
208 		}
209 		if (xdr_opaque_auth(&xdrs, &au->au_shcred)) {
210 			auth->ah_cred = au->au_shcred;
211 		} else {
212 			xdrs.x_op = XDR_FREE;
213 			(void)xdr_opaque_auth(&xdrs, &au->au_shcred);
214 			au->au_shcred.oa_base = NULL;
215 			auth->ah_cred = au->au_origcred;
216 		}
217 		marshal_new_auth(auth);
218 	}
219 	return (TRUE);
220 }
221 
222 static bool_t
223 authunix_refresh(AUTH *auth, void *dummy)
224 {
225 	struct audata *au = AUTH_PRIVATE(auth);
226 	struct xucred xcr;
227 	uint32_t time;
228 	struct timeval now;
229 	XDR xdrs;
230 	int stat;
231 
232 	if (auth->ah_cred.oa_base == au->au_origcred.oa_base) {
233 		/* there is no hope.  Punt */
234 		return (FALSE);
235 	}
236 	au->au_shfaults ++;
237 
238 	/* first deserialize the creds back into a struct ucred */
239 	xdrmem_create(&xdrs, au->au_origcred.oa_base,
240 	    au->au_origcred.oa_length, XDR_DECODE);
241 	stat = xdr_authunix_parms(&xdrs, &time, &xcr);
242 	if (! stat)
243 		goto done;
244 
245 	/* update the time and serialize in place */
246 	getmicrotime(&now);
247 	time = now.tv_sec;
248 	xdrs.x_op = XDR_ENCODE;
249 	XDR_SETPOS(&xdrs, 0);
250 
251 	stat = xdr_authunix_parms(&xdrs, &time, &xcr);
252 	if (! stat)
253 		goto done;
254 	auth->ah_cred = au->au_origcred;
255 	marshal_new_auth(auth);
256 done:
257 	XDR_DESTROY(&xdrs);
258 	return (stat);
259 }
260 
261 static void
262 authunix_destroy(AUTH *auth)
263 {
264 	struct audata *au;
265 
266 	au = AUTH_PRIVATE(auth);
267 	mem_free(au->au_origcred.oa_base, au->au_origcred.oa_length);
268 
269 	if (au->au_shcred.oa_base != NULL)
270 		mem_free(au->au_shcred.oa_base, au->au_shcred.oa_length);
271 
272 	mem_free(auth->ah_private, sizeof(struct audata));
273 
274 	if (auth->ah_verf.oa_base != NULL)
275 		mem_free(auth->ah_verf.oa_base, auth->ah_verf.oa_length);
276 
277 	mem_free(auth, sizeof(*auth));
278 }
279 
280 /*
281  * Marshals (pre-serializes) an auth struct.
282  * sets private data, au_marshed and au_mpos
283  */
284 static void
285 marshal_new_auth(AUTH *auth)
286 {
287 	XDR	xdr_stream;
288 	XDR	*xdrs = &xdr_stream;
289 	struct audata *au;
290 
291 	au = AUTH_PRIVATE(auth);
292 	xdrmem_create(xdrs, au->au_marshed, MAX_AUTH_BYTES, XDR_ENCODE);
293 	if ((! xdr_opaque_auth(xdrs, &(auth->ah_cred))) ||
294 	    (! xdr_opaque_auth(xdrs, &(auth->ah_verf))))
295 		printf("auth_none.c - Fatal marshalling problem");
296 	else
297 		au->au_mpos = XDR_GETPOS(xdrs);
298 	XDR_DESTROY(xdrs);
299 }
300