xref: /freebsd/lib/libc/rpc/auth_unix.c (revision 4c0d7cdf5d3b64e235140553601c0dd5827429a7)
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 "namespace.h"
52 #include "reentrant.h"
53 #include <sys/param.h>
54 
55 #include <assert.h>
56 #include <err.h>
57 #include <stdio.h>
58 #include <stdlib.h>
59 #include <unistd.h>
60 #include <string.h>
61 
62 #include <rpc/types.h>
63 #include <rpc/xdr.h>
64 #include <rpc/auth.h>
65 #include <rpc/auth_unix.h>
66 #include "un-namespace.h"
67 #include "mt_misc.h"
68 
69 /* auth_unix.c */
70 static void authunix_nextverf (AUTH *);
71 static bool_t authunix_marshal (AUTH *, XDR *);
72 static bool_t authunix_validate (AUTH *, struct opaque_auth *);
73 static bool_t authunix_refresh (AUTH *, void *);
74 static void authunix_destroy (AUTH *);
75 static void marshal_new_auth (AUTH *);
76 static struct auth_ops *authunix_ops (void);
77 
78 /*
79  * This struct is pointed to by the ah_private field of an auth_handle.
80  */
81 struct audata {
82 	struct opaque_auth	au_origcred;	/* original credentials */
83 	struct opaque_auth	au_shcred;	/* short hand cred */
84 	u_long			au_shfaults;	/* short hand cache faults */
85 	char			au_marshed[MAX_AUTH_BYTES];
86 	u_int			au_mpos;	/* xdr pos at end of marshed */
87 };
88 #define	AUTH_PRIVATE(auth)	((struct audata *)auth->ah_private)
89 
90 /*
91  * Create a unix style authenticator.
92  * Returns an auth handle with the given stuff in it.
93  */
94 AUTH *
95 authunix_create(machname, uid, gid, len, aup_gids)
96 	char *machname;
97 	u_int uid;
98 	u_int gid;
99 	int len;
100 	u_int *aup_gids;
101 {
102 	struct authunix_parms aup;
103 	char mymem[MAX_AUTH_BYTES];
104 	struct timeval now;
105 	XDR xdrs;
106 	AUTH *auth;
107 	struct audata *au;
108 
109 	/*
110 	 * Allocate and set up auth handle
111 	 */
112 	au = NULL;
113 	auth = mem_alloc(sizeof(*auth));
114 #ifndef _KERNEL
115 	if (auth == NULL) {
116 		warnx("authunix_create: out of memory");
117 		goto cleanup_authunix_create;
118 	}
119 #endif
120 	au = mem_alloc(sizeof(*au));
121 #ifndef _KERNEL
122 	if (au == NULL) {
123 		warnx("authunix_create: out of memory");
124 		goto cleanup_authunix_create;
125 	}
126 #endif
127 	auth->ah_ops = authunix_ops();
128 	auth->ah_private = (caddr_t)au;
129 	auth->ah_verf = au->au_shcred = _null_auth;
130 	au->au_shfaults = 0;
131 	au->au_origcred.oa_base = NULL;
132 
133 	/*
134 	 * fill in param struct from the given params
135 	 */
136 	(void)gettimeofday(&now, NULL);
137 	aup.aup_time = now.tv_sec;
138 	aup.aup_machname = machname;
139 	aup.aup_uid = uid;
140 	aup.aup_gid = gid;
141 	aup.aup_len = (u_int)len;
142 	aup.aup_gids = aup_gids;
143 
144 	/*
145 	 * Serialize the parameters into origcred
146 	 */
147 	xdrmem_create(&xdrs, mymem, MAX_AUTH_BYTES, XDR_ENCODE);
148 	if (! xdr_authunix_parms(&xdrs, &aup))
149 		abort();
150 	au->au_origcred.oa_length = len = XDR_GETPOS(&xdrs);
151 	au->au_origcred.oa_flavor = AUTH_UNIX;
152 #ifdef _KERNEL
153 	au->au_origcred.oa_base = mem_alloc((u_int) len);
154 #else
155 	if ((au->au_origcred.oa_base = mem_alloc((u_int) len)) == NULL) {
156 		warnx("authunix_create: out of memory");
157 		goto cleanup_authunix_create;
158 	}
159 #endif
160 	memmove(au->au_origcred.oa_base, mymem, (size_t)len);
161 
162 	/*
163 	 * set auth handle to reflect new cred.
164 	 */
165 	auth->ah_cred = au->au_origcred;
166 	marshal_new_auth(auth);
167 	return (auth);
168 #ifndef _KERNEL
169  cleanup_authunix_create:
170 	if (auth)
171 		mem_free(auth, sizeof(*auth));
172 	if (au) {
173 		if (au->au_origcred.oa_base)
174 			mem_free(au->au_origcred.oa_base, (u_int)len);
175 		mem_free(au, sizeof(*au));
176 	}
177 	return (NULL);
178 #endif
179 }
180 
181 /*
182  * Returns an auth handle with parameters determined by doing lots of
183  * syscalls.
184  */
185 AUTH *
186 authunix_create_default()
187 {
188 	AUTH *auth;
189 	int ngids;
190 	long ngids_max;
191 	char machname[MAXHOSTNAMELEN + 1];
192 	uid_t uid;
193 	gid_t gid;
194 	gid_t *gids;
195 
196 	ngids_max = sysconf(_SC_NGROUPS_MAX) + 1;
197 	gids = malloc(sizeof(gid_t) * ngids_max);
198 	if (gids == NULL)
199 		return (NULL);
200 
201 	if (gethostname(machname, sizeof machname) == -1)
202 		abort();
203 	machname[sizeof(machname) - 1] = 0;
204 	uid = geteuid();
205 	gid = getegid();
206 	if ((ngids = getgroups(ngids_max, gids)) < 0)
207 		abort();
208 	if (ngids > NGRPS)
209 		ngids = NGRPS;
210 	/* XXX: interface problem; we should translate from uid_t and gid_t */
211 	auth = authunix_create(machname, uid, gid, ngids, gids);
212 	free(gids);
213 	return (auth);
214 }
215 
216 /*
217  * authunix operations
218  */
219 
220 /* ARGSUSED */
221 static void
222 authunix_nextverf(auth)
223 	AUTH *auth;
224 {
225 	/* no action necessary */
226 }
227 
228 static bool_t
229 authunix_marshal(auth, xdrs)
230 	AUTH *auth;
231 	XDR *xdrs;
232 {
233 	struct audata *au;
234 
235 	assert(auth != NULL);
236 	assert(xdrs != NULL);
237 
238 	au = AUTH_PRIVATE(auth);
239 	return (XDR_PUTBYTES(xdrs, au->au_marshed, au->au_mpos));
240 }
241 
242 static bool_t
243 authunix_validate(auth, verf)
244 	AUTH *auth;
245 	struct opaque_auth *verf;
246 {
247 	struct audata *au;
248 	XDR xdrs;
249 
250 	assert(auth != NULL);
251 	assert(verf != NULL);
252 
253 	if (verf->oa_flavor == AUTH_SHORT) {
254 		au = AUTH_PRIVATE(auth);
255 		xdrmem_create(&xdrs, verf->oa_base, verf->oa_length,
256 		    XDR_DECODE);
257 
258 		if (au->au_shcred.oa_base != NULL) {
259 			mem_free(au->au_shcred.oa_base,
260 			    au->au_shcred.oa_length);
261 			au->au_shcred.oa_base = NULL;
262 		}
263 		if (xdr_opaque_auth(&xdrs, &au->au_shcred)) {
264 			auth->ah_cred = au->au_shcred;
265 		} else {
266 			xdrs.x_op = XDR_FREE;
267 			(void)xdr_opaque_auth(&xdrs, &au->au_shcred);
268 			au->au_shcred.oa_base = NULL;
269 			auth->ah_cred = au->au_origcred;
270 		}
271 		marshal_new_auth(auth);
272 	}
273 	return (TRUE);
274 }
275 
276 static bool_t
277 authunix_refresh(AUTH *auth, void *dummy)
278 {
279 	struct audata *au = AUTH_PRIVATE(auth);
280 	struct authunix_parms aup;
281 	struct timeval now;
282 	XDR xdrs;
283 	int stat;
284 
285 	assert(auth != NULL);
286 
287 	if (auth->ah_cred.oa_base == au->au_origcred.oa_base) {
288 		/* there is no hope.  Punt */
289 		return (FALSE);
290 	}
291 	au->au_shfaults ++;
292 
293 	/* first deserialize the creds back into a struct authunix_parms */
294 	aup.aup_machname = NULL;
295 	aup.aup_gids = NULL;
296 	xdrmem_create(&xdrs, au->au_origcred.oa_base,
297 	    au->au_origcred.oa_length, XDR_DECODE);
298 	stat = xdr_authunix_parms(&xdrs, &aup);
299 	if (! stat)
300 		goto done;
301 
302 	/* update the time and serialize in place */
303 	(void)gettimeofday(&now, NULL);
304 	aup.aup_time = now.tv_sec;
305 	xdrs.x_op = XDR_ENCODE;
306 	XDR_SETPOS(&xdrs, 0);
307 	stat = xdr_authunix_parms(&xdrs, &aup);
308 	if (! stat)
309 		goto done;
310 	auth->ah_cred = au->au_origcred;
311 	marshal_new_auth(auth);
312 done:
313 	/* free the struct authunix_parms created by deserializing */
314 	xdrs.x_op = XDR_FREE;
315 	(void)xdr_authunix_parms(&xdrs, &aup);
316 	XDR_DESTROY(&xdrs);
317 	return (stat);
318 }
319 
320 static void
321 authunix_destroy(auth)
322 	AUTH *auth;
323 {
324 	struct audata *au;
325 
326 	assert(auth != NULL);
327 
328 	au = AUTH_PRIVATE(auth);
329 	mem_free(au->au_origcred.oa_base, au->au_origcred.oa_length);
330 
331 	if (au->au_shcred.oa_base != NULL)
332 		mem_free(au->au_shcred.oa_base, au->au_shcred.oa_length);
333 
334 	mem_free(auth->ah_private, sizeof(struct audata));
335 
336 	if (auth->ah_verf.oa_base != NULL)
337 		mem_free(auth->ah_verf.oa_base, auth->ah_verf.oa_length);
338 
339 	mem_free(auth, sizeof(*auth));
340 }
341 
342 /*
343  * Marshals (pre-serializes) an auth struct.
344  * sets private data, au_marshed and au_mpos
345  */
346 static void
347 marshal_new_auth(auth)
348 	AUTH *auth;
349 {
350 	XDR	xdr_stream;
351 	XDR	*xdrs = &xdr_stream;
352 	struct audata *au;
353 
354 	assert(auth != NULL);
355 
356 	au = AUTH_PRIVATE(auth);
357 	xdrmem_create(xdrs, au->au_marshed, MAX_AUTH_BYTES, XDR_ENCODE);
358 	if ((! xdr_opaque_auth(xdrs, &(auth->ah_cred))) ||
359 	    (! xdr_opaque_auth(xdrs, &(auth->ah_verf))))
360 		warnx("auth_none.c - Fatal marshalling problem");
361 	else
362 		au->au_mpos = XDR_GETPOS(xdrs);
363 	XDR_DESTROY(xdrs);
364 }
365 
366 static struct auth_ops *
367 authunix_ops()
368 {
369 	static struct auth_ops ops;
370 
371 	/* VARIABLES PROTECTED BY ops_lock: ops */
372 
373 	mutex_lock(&ops_lock);
374 	if (ops.ah_nextverf == NULL) {
375 		ops.ah_nextverf = authunix_nextverf;
376 		ops.ah_marshal = authunix_marshal;
377 		ops.ah_validate = authunix_validate;
378 		ops.ah_refresh = authunix_refresh;
379 		ops.ah_destroy = authunix_destroy;
380 	}
381 	mutex_unlock(&ops_lock);
382 	return (&ops);
383 }
384