auth_unix.c revision 1.21.8.2 1 1.21.8.2 christos /* $NetBSD: auth_unix.c,v 1.21.8.2 2008/11/08 21:45:38 christos Exp $ */
2 1.21.8.2 christos
3 1.21.8.2 christos /*
4 1.21.8.2 christos * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
5 1.21.8.2 christos * unrestricted use provided that this legend is included on all tape
6 1.21.8.2 christos * media and as a part of the software program in whole or part. Users
7 1.21.8.2 christos * may copy or modify Sun RPC without charge, but are not authorized
8 1.21.8.2 christos * to license or distribute it to anyone else except as part of a product or
9 1.21.8.2 christos * program developed by the user.
10 1.21.8.2 christos *
11 1.21.8.2 christos * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
12 1.21.8.2 christos * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
13 1.21.8.2 christos * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
14 1.21.8.2 christos *
15 1.21.8.2 christos * Sun RPC is provided with no support and without any obligation on the
16 1.21.8.2 christos * part of Sun Microsystems, Inc. to assist in its use, correction,
17 1.21.8.2 christos * modification or enhancement.
18 1.21.8.2 christos *
19 1.21.8.2 christos * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
20 1.21.8.2 christos * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
21 1.21.8.2 christos * OR ANY PART THEREOF.
22 1.21.8.2 christos *
23 1.21.8.2 christos * In no event will Sun Microsystems, Inc. be liable for any lost revenue
24 1.21.8.2 christos * or profits or other special, indirect and consequential damages, even if
25 1.21.8.2 christos * Sun has been advised of the possibility of such damages.
26 1.21.8.2 christos *
27 1.21.8.2 christos * Sun Microsystems, Inc.
28 1.21.8.2 christos * 2550 Garcia Avenue
29 1.21.8.2 christos * Mountain View, California 94043
30 1.21.8.2 christos */
31 1.21.8.2 christos
32 1.21.8.2 christos #include <sys/cdefs.h>
33 1.21.8.2 christos #if defined(LIBC_SCCS) && !defined(lint)
34 1.21.8.2 christos #if 0
35 1.21.8.2 christos static char *sccsid = "@(#)auth_unix.c 1.19 87/08/11 Copyr 1984 Sun Micro";
36 1.21.8.2 christos static char *sccsid = "@(#)auth_unix.c 2.2 88/08/01 4.0 RPCSRC";
37 1.21.8.2 christos #else
38 1.21.8.2 christos __RCSID("$NetBSD: auth_unix.c,v 1.21.8.2 2008/11/08 21:45:38 christos Exp $");
39 1.21.8.2 christos #endif
40 1.21.8.2 christos #endif
41 1.21.8.2 christos
42 1.21.8.2 christos /*
43 1.21.8.2 christos * auth_unix.c, Implements UNIX style authentication parameters.
44 1.21.8.2 christos *
45 1.21.8.2 christos * Copyright (C) 1984, Sun Microsystems, Inc.
46 1.21.8.2 christos *
47 1.21.8.2 christos * The system is very weak. The client uses no encryption for it's
48 1.21.8.2 christos * credentials and only sends null verifiers. The server sends backs
49 1.21.8.2 christos * null verifiers or optionally a verifier that suggests a new short hand
50 1.21.8.2 christos * for the credentials.
51 1.21.8.2 christos *
52 1.21.8.2 christos */
53 1.21.8.2 christos
54 1.21.8.2 christos #include "namespace.h"
55 1.21.8.2 christos #include "reentrant.h"
56 1.21.8.2 christos #include <sys/param.h>
57 1.21.8.2 christos
58 1.21.8.2 christos #include <assert.h>
59 1.21.8.2 christos #include <err.h>
60 1.21.8.2 christos #include <stdio.h>
61 1.21.8.2 christos #include <stdlib.h>
62 1.21.8.2 christos #include <string.h>
63 1.21.8.2 christos #include <unistd.h>
64 1.21.8.2 christos
65 1.21.8.2 christos #include <rpc/types.h>
66 1.21.8.2 christos #include <rpc/xdr.h>
67 1.21.8.2 christos #include <rpc/auth.h>
68 1.21.8.2 christos #include <rpc/auth_unix.h>
69 1.21.8.2 christos
70 1.21.8.2 christos #ifdef __weak_alias
71 1.21.8.2 christos __weak_alias(authunix_create,_authunix_create)
72 1.21.8.2 christos __weak_alias(authunix_create_default,_authunix_create_default)
73 1.21.8.2 christos #endif
74 1.21.8.2 christos
75 1.21.8.2 christos
76 1.21.8.2 christos /* auth_unix.c */
77 1.21.8.2 christos static void authunix_nextverf __P((AUTH *));
78 1.21.8.2 christos static bool_t authunix_marshal __P((AUTH *, XDR *));
79 1.21.8.2 christos static bool_t authunix_validate __P((AUTH *, struct opaque_auth *));
80 1.21.8.2 christos static bool_t authunix_refresh __P((AUTH *));
81 1.21.8.2 christos static void authunix_destroy __P((AUTH *));
82 1.21.8.2 christos static void marshal_new_auth __P((AUTH *));
83 1.21.8.2 christos static const struct auth_ops *authunix_ops __P((void));
84 1.21.8.2 christos
85 1.21.8.2 christos /*
86 1.21.8.2 christos * This struct is pointed to by the ah_private field of an auth_handle.
87 1.21.8.2 christos */
88 1.21.8.2 christos struct audata {
89 1.21.8.2 christos struct opaque_auth au_origcred; /* original credentials */
90 1.21.8.2 christos struct opaque_auth au_shcred; /* short hand cred */
91 1.21.8.2 christos u_long au_shfaults; /* short hand cache faults */
92 1.21.8.2 christos char au_marshed[MAX_AUTH_BYTES];
93 1.21.8.2 christos u_int au_mpos; /* xdr pos at end of marshed */
94 1.21.8.2 christos };
95 1.21.8.2 christos #define AUTH_PRIVATE(auth) ((struct audata *)auth->ah_private)
96 1.21.8.2 christos
97 1.21.8.2 christos /*
98 1.21.8.2 christos * Create a unix style authenticator.
99 1.21.8.2 christos * Returns an auth handle with the given stuff in it.
100 1.21.8.2 christos */
101 1.21.8.2 christos AUTH *
102 1.21.8.2 christos authunix_create(machname, uid, gid, len, aup_gids)
103 1.21.8.2 christos char *machname;
104 1.21.8.2 christos int uid;
105 1.21.8.2 christos int gid;
106 1.21.8.2 christos int len;
107 1.21.8.2 christos int *aup_gids;
108 1.21.8.2 christos {
109 1.21.8.2 christos struct authunix_parms aup;
110 1.21.8.2 christos char mymem[MAX_AUTH_BYTES];
111 1.21.8.2 christos struct timeval now;
112 1.21.8.2 christos XDR xdrs;
113 1.21.8.2 christos AUTH *auth;
114 1.21.8.2 christos struct audata *au;
115 1.21.8.2 christos
116 1.21.8.2 christos /*
117 1.21.8.2 christos * Allocate and set up auth handle
118 1.21.8.2 christos */
119 1.21.8.2 christos au = NULL;
120 1.21.8.2 christos auth = mem_alloc(sizeof(*auth));
121 1.21.8.2 christos #ifndef KERNEL
122 1.21.8.2 christos if (auth == NULL) {
123 1.21.8.2 christos warnx("authunix_create: out of memory");
124 1.21.8.2 christos goto cleanup_authunix_create;
125 1.21.8.2 christos }
126 1.21.8.2 christos #endif
127 1.21.8.2 christos au = mem_alloc(sizeof(*au));
128 1.21.8.2 christos #ifndef KERNEL
129 1.21.8.2 christos if (au == NULL) {
130 1.21.8.2 christos warnx("authunix_create: out of memory");
131 1.21.8.2 christos goto cleanup_authunix_create;
132 1.21.8.2 christos }
133 1.21.8.2 christos #endif
134 1.21.8.2 christos auth->ah_ops = authunix_ops();
135 1.21.8.2 christos auth->ah_private = au;
136 1.21.8.2 christos auth->ah_verf = au->au_shcred = _null_auth;
137 1.21.8.2 christos au->au_shfaults = 0;
138 1.21.8.2 christos au->au_origcred.oa_base = NULL;
139 1.21.8.2 christos
140 1.21.8.2 christos /*
141 1.21.8.2 christos * fill in param struct from the given params
142 1.21.8.2 christos */
143 1.21.8.2 christos (void)gettimeofday(&now, NULL);
144 1.21.8.2 christos aup.aup_time = (u_long)now.tv_sec; /* XXX: truncate on 32 bit */
145 1.21.8.2 christos aup.aup_machname = machname;
146 1.21.8.2 christos aup.aup_uid = uid;
147 1.21.8.2 christos aup.aup_gid = gid;
148 1.21.8.2 christos aup.aup_len = (u_int)len;
149 1.21.8.2 christos aup.aup_gids = aup_gids;
150 1.21.8.2 christos
151 1.21.8.2 christos /*
152 1.21.8.2 christos * Serialize the parameters into origcred
153 1.21.8.2 christos */
154 1.21.8.2 christos xdrmem_create(&xdrs, mymem, MAX_AUTH_BYTES, XDR_ENCODE);
155 1.21.8.2 christos if (! xdr_authunix_parms(&xdrs, &aup))
156 1.21.8.2 christos abort();
157 1.21.8.2 christos au->au_origcred.oa_length = len = XDR_GETPOS(&xdrs);
158 1.21.8.2 christos au->au_origcred.oa_flavor = AUTH_UNIX;
159 1.21.8.2 christos #ifdef KERNEL
160 1.21.8.2 christos au->au_origcred.oa_base = mem_alloc((size_t)len);
161 1.21.8.2 christos #else
162 1.21.8.2 christos if ((au->au_origcred.oa_base = mem_alloc((size_t)len)) == NULL) {
163 1.21.8.2 christos warnx("authunix_create: out of memory");
164 1.21.8.2 christos goto cleanup_authunix_create;
165 1.21.8.2 christos }
166 1.21.8.2 christos #endif
167 1.21.8.2 christos memmove(au->au_origcred.oa_base, mymem, (size_t)len);
168 1.21.8.2 christos
169 1.21.8.2 christos /*
170 1.21.8.2 christos * set auth handle to reflect new cred.
171 1.21.8.2 christos */
172 1.21.8.2 christos auth->ah_cred = au->au_origcred;
173 1.21.8.2 christos marshal_new_auth(auth);
174 1.21.8.2 christos return (auth);
175 1.21.8.2 christos #ifndef KERNEL
176 1.21.8.2 christos cleanup_authunix_create:
177 1.21.8.2 christos if (auth)
178 1.21.8.2 christos mem_free(auth, sizeof(*auth));
179 1.21.8.2 christos if (au) {
180 1.21.8.2 christos if (au->au_origcred.oa_base)
181 1.21.8.2 christos mem_free(au->au_origcred.oa_base, (u_int)len);
182 1.21.8.2 christos mem_free(au, sizeof(*au));
183 1.21.8.2 christos }
184 1.21.8.2 christos return (NULL);
185 1.21.8.2 christos #endif
186 1.21.8.2 christos }
187 1.21.8.2 christos
188 1.21.8.2 christos /*
189 1.21.8.2 christos * Returns an auth handle with parameters determined by doing lots of
190 1.21.8.2 christos * syscalls.
191 1.21.8.2 christos */
192 1.21.8.2 christos AUTH *
193 1.21.8.2 christos authunix_create_default()
194 1.21.8.2 christos {
195 1.21.8.2 christos int len;
196 1.21.8.2 christos char machname[MAXHOSTNAMELEN + 1];
197 1.21.8.2 christos uid_t uid;
198 1.21.8.2 christos gid_t gid;
199 1.21.8.2 christos gid_t gids[NGRPS];
200 1.21.8.2 christos
201 1.21.8.2 christos if (gethostname(machname, sizeof machname) == -1)
202 1.21.8.2 christos abort();
203 1.21.8.2 christos machname[sizeof(machname) - 1] = 0;
204 1.21.8.2 christos uid = geteuid();
205 1.21.8.2 christos gid = getegid();
206 1.21.8.2 christos if ((len = getgroups(NGRPS, gids)) < 0)
207 1.21.8.2 christos abort();
208 1.21.8.2 christos /* XXX: interface problem; those should all have been unsigned */
209 1.21.8.2 christos return (authunix_create(machname, (int)uid, (int)gid, len,
210 1.21.8.2 christos (int *)gids));
211 1.21.8.2 christos }
212 1.21.8.2 christos
213 1.21.8.2 christos /*
214 1.21.8.2 christos * authunix operations
215 1.21.8.2 christos */
216 1.21.8.2 christos
217 1.21.8.2 christos /* ARGSUSED */
218 1.21.8.2 christos static void
219 1.21.8.2 christos authunix_nextverf(auth)
220 1.21.8.2 christos AUTH *auth;
221 1.21.8.2 christos {
222 1.21.8.2 christos /* no action necessary */
223 1.21.8.2 christos }
224 1.21.8.2 christos
225 1.21.8.2 christos static bool_t
226 1.21.8.2 christos authunix_marshal(auth, xdrs)
227 1.21.8.2 christos AUTH *auth;
228 1.21.8.2 christos XDR *xdrs;
229 1.21.8.2 christos {
230 1.21.8.2 christos struct audata *au;
231 1.21.8.2 christos
232 1.21.8.2 christos _DIAGASSERT(auth != NULL);
233 1.21.8.2 christos _DIAGASSERT(xdrs != NULL);
234 1.21.8.2 christos
235 1.21.8.2 christos au = AUTH_PRIVATE(auth);
236 1.21.8.2 christos return (XDR_PUTBYTES(xdrs, au->au_marshed, au->au_mpos));
237 1.21.8.2 christos }
238 1.21.8.2 christos
239 1.21.8.2 christos static bool_t
240 1.21.8.2 christos authunix_validate(auth, verf)
241 1.21.8.2 christos AUTH *auth;
242 1.21.8.2 christos struct opaque_auth *verf;
243 1.21.8.2 christos {
244 1.21.8.2 christos struct audata *au;
245 1.21.8.2 christos XDR xdrs;
246 1.21.8.2 christos
247 1.21.8.2 christos _DIAGASSERT(auth != NULL);
248 1.21.8.2 christos _DIAGASSERT(verf != NULL);
249 1.21.8.2 christos
250 1.21.8.2 christos if (verf->oa_flavor == AUTH_SHORT) {
251 1.21.8.2 christos au = AUTH_PRIVATE(auth);
252 1.21.8.2 christos xdrmem_create(&xdrs, verf->oa_base, verf->oa_length,
253 1.21.8.2 christos XDR_DECODE);
254 1.21.8.2 christos
255 1.21.8.2 christos if (au->au_shcred.oa_base != NULL) {
256 1.21.8.2 christos mem_free(au->au_shcred.oa_base,
257 1.21.8.2 christos au->au_shcred.oa_length);
258 1.21.8.2 christos au->au_shcred.oa_base = NULL;
259 1.21.8.2 christos }
260 1.21.8.2 christos if (xdr_opaque_auth(&xdrs, &au->au_shcred)) {
261 1.21.8.2 christos auth->ah_cred = au->au_shcred;
262 1.21.8.2 christos } else {
263 1.21.8.2 christos xdrs.x_op = XDR_FREE;
264 1.21.8.2 christos (void)xdr_opaque_auth(&xdrs, &au->au_shcred);
265 1.21.8.2 christos au->au_shcred.oa_base = NULL;
266 1.21.8.2 christos auth->ah_cred = au->au_origcred;
267 1.21.8.2 christos }
268 1.21.8.2 christos marshal_new_auth(auth);
269 1.21.8.2 christos }
270 1.21.8.2 christos return (TRUE);
271 1.21.8.2 christos }
272 1.21.8.2 christos
273 1.21.8.2 christos static bool_t
274 1.21.8.2 christos authunix_refresh(auth)
275 1.21.8.2 christos AUTH *auth;
276 1.21.8.2 christos {
277 1.21.8.2 christos struct audata *au = AUTH_PRIVATE(auth);
278 1.21.8.2 christos struct authunix_parms aup;
279 1.21.8.2 christos struct timeval now;
280 1.21.8.2 christos XDR xdrs;
281 1.21.8.2 christos int stat;
282 1.21.8.2 christos
283 1.21.8.2 christos _DIAGASSERT(auth != NULL);
284 1.21.8.2 christos
285 1.21.8.2 christos if (auth->ah_cred.oa_base == au->au_origcred.oa_base) {
286 1.21.8.2 christos /* there is no hope. Punt */
287 1.21.8.2 christos return (FALSE);
288 1.21.8.2 christos }
289 1.21.8.2 christos au->au_shfaults++;
290 1.21.8.2 christos
291 1.21.8.2 christos /* first deserialize the creds back into a struct authunix_parms */
292 1.21.8.2 christos aup.aup_machname = NULL;
293 1.21.8.2 christos aup.aup_gids = NULL;
294 1.21.8.2 christos xdrmem_create(&xdrs, au->au_origcred.oa_base,
295 1.21.8.2 christos au->au_origcred.oa_length, XDR_DECODE);
296 1.21.8.2 christos stat = xdr_authunix_parms(&xdrs, &aup);
297 1.21.8.2 christos if (! stat)
298 1.21.8.2 christos goto done;
299 1.21.8.2 christos
300 1.21.8.2 christos /* update the time and serialize in place */
301 1.21.8.2 christos (void)gettimeofday(&now, NULL);
302 1.21.8.2 christos aup.aup_time = (u_long)now.tv_sec; /* XXX: truncate on 32 bit */
303 1.21.8.2 christos xdrs.x_op = XDR_ENCODE;
304 1.21.8.2 christos XDR_SETPOS(&xdrs, 0);
305 1.21.8.2 christos stat = xdr_authunix_parms(&xdrs, &aup);
306 1.21.8.2 christos if (! stat)
307 1.21.8.2 christos goto done;
308 1.21.8.2 christos auth->ah_cred = au->au_origcred;
309 1.21.8.2 christos marshal_new_auth(auth);
310 1.21.8.2 christos done:
311 1.21.8.2 christos /* free the struct authunix_parms created by deserializing */
312 1.21.8.2 christos xdrs.x_op = XDR_FREE;
313 1.21.8.2 christos (void)xdr_authunix_parms(&xdrs, &aup);
314 1.21.8.2 christos XDR_DESTROY(&xdrs);
315 1.21.8.2 christos return (stat);
316 1.21.8.2 christos }
317 1.21.8.2 christos
318 1.21.8.2 christos static void
319 1.21.8.2 christos authunix_destroy(auth)
320 1.21.8.2 christos AUTH *auth;
321 1.21.8.2 christos {
322 1.21.8.2 christos struct audata *au;
323 1.21.8.2 christos
324 1.21.8.2 christos _DIAGASSERT(auth != NULL);
325 1.21.8.2 christos
326 1.21.8.2 christos au = AUTH_PRIVATE(auth);
327 1.21.8.2 christos mem_free(au->au_origcred.oa_base, au->au_origcred.oa_length);
328 1.21.8.2 christos
329 1.21.8.2 christos if (au->au_shcred.oa_base != NULL)
330 1.21.8.2 christos mem_free(au->au_shcred.oa_base, au->au_shcred.oa_length);
331 1.21.8.2 christos
332 1.21.8.2 christos mem_free(auth->ah_private, sizeof(struct audata));
333 1.21.8.2 christos
334 1.21.8.2 christos if (auth->ah_verf.oa_base != NULL)
335 1.21.8.2 christos mem_free(auth->ah_verf.oa_base, auth->ah_verf.oa_length);
336 1.21.8.2 christos
337 1.21.8.2 christos mem_free(auth, sizeof(*auth));
338 1.21.8.2 christos }
339 1.21.8.2 christos
340 1.21.8.2 christos /*
341 1.21.8.2 christos * Marshals (pre-serializes) an auth struct.
342 1.21.8.2 christos * sets private data, au_marshed and au_mpos
343 1.21.8.2 christos */
344 1.21.8.2 christos static void
345 1.21.8.2 christos marshal_new_auth(auth)
346 1.21.8.2 christos AUTH *auth;
347 1.21.8.2 christos {
348 1.21.8.2 christos XDR xdr_stream;
349 1.21.8.2 christos XDR *xdrs = &xdr_stream;
350 1.21.8.2 christos struct audata *au;
351 1.21.8.2 christos
352 1.21.8.2 christos _DIAGASSERT(auth != NULL);
353 1.21.8.2 christos
354 1.21.8.2 christos au = AUTH_PRIVATE(auth);
355 1.21.8.2 christos xdrmem_create(xdrs, au->au_marshed, MAX_AUTH_BYTES, XDR_ENCODE);
356 1.21.8.2 christos if ((! xdr_opaque_auth(xdrs, &(auth->ah_cred))) ||
357 1.21.8.2 christos (! xdr_opaque_auth(xdrs, &(auth->ah_verf))))
358 1.21.8.2 christos warnx("auth_none.c - Fatal marshalling problem");
359 1.21.8.2 christos else
360 1.21.8.2 christos au->au_mpos = XDR_GETPOS(xdrs);
361 1.21.8.2 christos XDR_DESTROY(xdrs);
362 1.21.8.2 christos }
363 1.21.8.2 christos
364 1.21.8.2 christos static const struct auth_ops *
365 1.21.8.2 christos authunix_ops()
366 1.21.8.2 christos {
367 1.21.8.2 christos static struct auth_ops ops;
368 1.21.8.2 christos #ifdef _REENTRANT
369 1.21.8.2 christos extern mutex_t ops_lock;
370 1.21.8.2 christos #endif
371 1.21.8.2 christos
372 1.21.8.2 christos /* VARIABLES PROTECTED BY ops_lock: ops */
373 1.21.8.2 christos
374 1.21.8.2 christos mutex_lock(&ops_lock);
375 1.21.8.2 christos if (ops.ah_nextverf == NULL) {
376 1.21.8.2 christos ops.ah_nextverf = authunix_nextverf;
377 1.21.8.2 christos ops.ah_marshal = authunix_marshal;
378 1.21.8.2 christos ops.ah_validate = authunix_validate;
379 1.21.8.2 christos ops.ah_refresh = authunix_refresh;
380 1.21.8.2 christos ops.ah_destroy = authunix_destroy;
381 1.21.8.2 christos }
382 1.21.8.2 christos mutex_unlock(&ops_lock);
383 1.21.8.2 christos return (&ops);
384 1.21.8.2 christos }
385