xref: /netbsd-src/lib/libc/rpc/auth_unix.c (revision 7c7c171d130af9949261bc7dce2150a03c3d239c)
1 /*	$NetBSD: auth_unix.c,v 1.10 1998/02/13 05:52:13 lukem 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 #include <sys/cdefs.h>
33 #if defined(LIBC_SCCS) && !defined(lint)
34 #if 0
35 static char *sccsid = "@(#)auth_unix.c 1.19 87/08/11 Copyr 1984 Sun Micro";
36 static char *sccsid = "@(#)auth_unix.c	2.2 88/08/01 4.0 RPCSRC";
37 #else
38 __RCSID("$NetBSD: auth_unix.c,v 1.10 1998/02/13 05:52:13 lukem Exp $");
39 #endif
40 #endif
41 
42 /*
43  * auth_unix.c, Implements UNIX style authentication parameters.
44  *
45  * Copyright (C) 1984, Sun Microsystems, Inc.
46  *
47  * The system is very weak.  The client uses no encryption for it's
48  * credentials and only sends null verifiers.  The server sends backs
49  * null verifiers or optionally a verifier that suggests a new short hand
50  * for the credentials.
51  *
52  */
53 
54 #include "namespace.h"
55 
56 #include <err.h>
57 #include <stdio.h>
58 #include <stdlib.h>
59 #include <string.h>
60 #include <unistd.h>
61 
62 #include <rpc/types.h>
63 #include <rpc/xdr.h>
64 #include <rpc/auth.h>
65 #include <rpc/auth_unix.h>
66 
67 #ifdef __weak_alias
68 __weak_alias(authunix_create,_authunix_create);
69 __weak_alias(authunix_create_default,_authunix_create_default);
70 #endif
71 
72 
73 /* auth_unix.c */
74 static void authunix_nextverf __P((AUTH *));
75 static bool_t authunix_marshal __P((AUTH *, XDR *));
76 static bool_t authunix_validate __P((AUTH *, struct opaque_auth *));
77 static bool_t authunix_refresh __P((AUTH *));
78 static void authunix_destroy __P((AUTH *));
79 static void marshal_new_auth __P((AUTH *));
80 
81 /*
82  * Unix authenticator operations vector
83  */
84 static struct auth_ops auth_unix_ops = {
85 	authunix_nextverf,
86 	authunix_marshal,
87 	authunix_validate,
88 	authunix_refresh,
89 	authunix_destroy
90 };
91 
92 /*
93  * This struct is pointed to by the ah_private field of an auth_handle.
94  */
95 struct audata {
96 	struct opaque_auth	au_origcred;	/* original credentials */
97 	struct opaque_auth	au_shcred;	/* short hand cred */
98 	u_long			au_shfaults;	/* short hand cache faults */
99 	char			au_marshed[MAX_AUTH_BYTES];
100 	u_int			au_mpos;	/* xdr pos at end of marshed */
101 };
102 #define	AUTH_PRIVATE(auth)	((struct audata *)auth->ah_private)
103 
104 /*
105  * Create a unix style authenticator.
106  * Returns an auth handle with the given stuff in it.
107  */
108 AUTH *
109 authunix_create(machname, uid, gid, len, aup_gids)
110 	char *machname;
111 	int uid;
112 	int gid;
113 	int len;
114 	int *aup_gids;
115 {
116 	struct authunix_parms aup;
117 	char mymem[MAX_AUTH_BYTES];
118 	struct timeval now;
119 	XDR xdrs;
120 	AUTH *auth;
121 	struct audata *au;
122 
123 	/*
124 	 * Allocate and set up auth handle
125 	 */
126 	auth = (AUTH *)mem_alloc(sizeof(*auth));
127 #ifndef KERNEL
128 	if (auth == NULL) {
129 		warnx("authunix_create: out of memory");
130 		return (NULL);
131 	}
132 #endif
133 	au = (struct audata *)mem_alloc(sizeof(*au));
134 #ifndef KERNEL
135 	if (au == NULL) {
136 		warnx("authunix_create: out of memory");
137 		return (NULL);
138 	}
139 #endif
140 	auth->ah_ops = &auth_unix_ops;
141 	auth->ah_private = (caddr_t)au;
142 	auth->ah_verf = au->au_shcred = _null_auth;
143 	au->au_shfaults = 0;
144 
145 	/*
146 	 * fill in param struct from the given params
147 	 */
148 	(void)gettimeofday(&now,  (struct timezone *)0);
149 	aup.aup_time = now.tv_sec;
150 	aup.aup_machname = machname;
151 	aup.aup_uid = uid;
152 	aup.aup_gid = gid;
153 	aup.aup_len = (u_int)len;
154 	aup.aup_gids = aup_gids;
155 
156 	/*
157 	 * Serialize the parameters into origcred
158 	 */
159 	xdrmem_create(&xdrs, mymem, MAX_AUTH_BYTES, XDR_ENCODE);
160 	if (! xdr_authunix_parms(&xdrs, &aup))
161 		abort();
162 	au->au_origcred.oa_length = len = XDR_GETPOS(&xdrs);
163 	au->au_origcred.oa_flavor = AUTH_UNIX;
164 #ifdef KERNEL
165 	au->au_origcred.oa_base = mem_alloc((u_int) len);
166 #else
167 	if ((au->au_origcred.oa_base = mem_alloc((u_int) len)) == NULL) {
168 		warnx("authunix_create: out of memory");
169 		return (NULL);
170 	}
171 #endif
172 	memmove(au->au_origcred.oa_base, mymem, (size_t)len);
173 
174 	/*
175 	 * set auth handle to reflect new cred.
176 	 */
177 	auth->ah_cred = au->au_origcred;
178 	marshal_new_auth(auth);
179 	return (auth);
180 }
181 
182 /*
183  * Returns an auth handle with parameters determined by doing lots of
184  * syscalls.
185  */
186 AUTH *
187 authunix_create_default()
188 {
189 	int len;
190 	char machname[MAX_MACHINE_NAME + 1];
191 	int uid;
192 	int gid;
193 	int gids[NGRPS];
194 
195 	if (gethostname(machname, MAX_MACHINE_NAME) == -1)
196 		abort();
197 	machname[MAX_MACHINE_NAME] = 0;
198 	uid = geteuid();
199 	gid = getegid();
200 	if ((len = getgroups(NGRPS, gids)) < 0)
201 		abort();
202 	return (authunix_create(machname, uid, gid, len, gids));
203 }
204 
205 /*
206  * authunix operations
207  */
208 
209 static void
210 authunix_nextverf(auth)
211 	AUTH *auth;
212 {
213 	/* no action necessary */
214 }
215 
216 static bool_t
217 authunix_marshal(auth, xdrs)
218 	AUTH *auth;
219 	XDR *xdrs;
220 {
221 	struct audata *au = AUTH_PRIVATE(auth);
222 
223 	return (XDR_PUTBYTES(xdrs, au->au_marshed, au->au_mpos));
224 }
225 
226 static bool_t
227 authunix_validate(auth, verf)
228 	AUTH *auth;
229 	struct opaque_auth *verf;
230 {
231 	struct audata *au;
232 	XDR xdrs;
233 
234 	if (verf->oa_flavor == AUTH_SHORT) {
235 		au = AUTH_PRIVATE(auth);
236 		xdrmem_create(&xdrs, verf->oa_base, verf->oa_length, XDR_DECODE);
237 
238 		if (au->au_shcred.oa_base != NULL) {
239 			mem_free(au->au_shcred.oa_base,
240 			    au->au_shcred.oa_length);
241 			au->au_shcred.oa_base = NULL;
242 		}
243 		if (xdr_opaque_auth(&xdrs, &au->au_shcred)) {
244 			auth->ah_cred = au->au_shcred;
245 		} else {
246 			xdrs.x_op = XDR_FREE;
247 			(void)xdr_opaque_auth(&xdrs, &au->au_shcred);
248 			au->au_shcred.oa_base = NULL;
249 			auth->ah_cred = au->au_origcred;
250 		}
251 		marshal_new_auth(auth);
252 	}
253 	return (TRUE);
254 }
255 
256 static bool_t
257 authunix_refresh(auth)
258 	AUTH *auth;
259 {
260 	struct audata *au = AUTH_PRIVATE(auth);
261 	struct authunix_parms aup;
262 	struct timeval now;
263 	XDR xdrs;
264 	int stat;
265 
266 	if (auth->ah_cred.oa_base == au->au_origcred.oa_base) {
267 		/* there is no hope.  Punt */
268 		return (FALSE);
269 	}
270 	au->au_shfaults ++;
271 
272 	/* first deserialize the creds back into a struct authunix_parms */
273 	aup.aup_machname = NULL;
274 	aup.aup_gids = (int *)NULL;
275 	xdrmem_create(&xdrs, au->au_origcred.oa_base,
276 	    au->au_origcred.oa_length, XDR_DECODE);
277 	stat = xdr_authunix_parms(&xdrs, &aup);
278 	if (! stat)
279 		goto done;
280 
281 	/* update the time and serialize in place */
282 	(void)gettimeofday(&now, (struct timezone *)0);
283 	aup.aup_time = now.tv_sec;
284 	xdrs.x_op = XDR_ENCODE;
285 	XDR_SETPOS(&xdrs, 0);
286 	stat = xdr_authunix_parms(&xdrs, &aup);
287 	if (! stat)
288 		goto done;
289 	auth->ah_cred = au->au_origcred;
290 	marshal_new_auth(auth);
291 done:
292 	/* free the struct authunix_parms created by deserializing */
293 	xdrs.x_op = XDR_FREE;
294 	(void)xdr_authunix_parms(&xdrs, &aup);
295 	XDR_DESTROY(&xdrs);
296 	return (stat);
297 }
298 
299 static void
300 authunix_destroy(auth)
301 	AUTH *auth;
302 {
303 	struct audata *au = AUTH_PRIVATE(auth);
304 
305 	mem_free(au->au_origcred.oa_base, au->au_origcred.oa_length);
306 
307 	if (au->au_shcred.oa_base != NULL)
308 		mem_free(au->au_shcred.oa_base, au->au_shcred.oa_length);
309 
310 	mem_free(auth->ah_private, sizeof(struct audata));
311 
312 	if (auth->ah_verf.oa_base != NULL)
313 		mem_free(auth->ah_verf.oa_base, auth->ah_verf.oa_length);
314 
315 	mem_free((caddr_t)auth, sizeof(*auth));
316 }
317 
318 /*
319  * Marshals (pre-serializes) an auth struct.
320  * sets private data, au_marshed and au_mpos
321  */
322 static void
323 marshal_new_auth(auth)
324 	AUTH *auth;
325 {
326 	XDR	xdr_stream;
327 	XDR	*xdrs = &xdr_stream;
328 	struct audata *au = AUTH_PRIVATE(auth);
329 
330 	xdrmem_create(xdrs, au->au_marshed, MAX_AUTH_BYTES, XDR_ENCODE);
331 	if ((! xdr_opaque_auth(xdrs, &(auth->ah_cred))) ||
332 	    (! xdr_opaque_auth(xdrs, &(auth->ah_verf))))
333 		warnx("auth_none.c - Fatal marshalling problem");
334 	else
335 		au->au_mpos = XDR_GETPOS(xdrs);
336 	XDR_DESTROY(xdrs);
337 }
338