1 | #include <machine/rtems-bsd-user-space.h> |
---|
2 | |
---|
3 | /* $NetBSD: auth_unix.c,v 1.18 2000/07/06 03:03:30 christos Exp $ */ |
---|
4 | |
---|
5 | /*- |
---|
6 | * Copyright (c) 2009, Sun Microsystems, Inc. |
---|
7 | * All rights reserved. |
---|
8 | * |
---|
9 | * Redistribution and use in source and binary forms, with or without |
---|
10 | * modification, are permitted provided that the following conditions are met: |
---|
11 | * - Redistributions of source code must retain the above copyright notice, |
---|
12 | * this list of conditions and the following disclaimer. |
---|
13 | * - Redistributions in binary form must reproduce the above copyright notice, |
---|
14 | * this list of conditions and the following disclaimer in the documentation |
---|
15 | * and/or other materials provided with the distribution. |
---|
16 | * - Neither the name of Sun Microsystems, Inc. nor the names of its |
---|
17 | * contributors may be used to endorse or promote products derived |
---|
18 | * from this software without specific prior written permission. |
---|
19 | * |
---|
20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
---|
21 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
23 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
---|
24 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
---|
25 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
---|
26 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
---|
27 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
---|
28 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
---|
29 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
---|
30 | * POSSIBILITY OF SUCH DAMAGE. |
---|
31 | */ |
---|
32 | |
---|
33 | #if defined(LIBC_SCCS) && !defined(lint) |
---|
34 | static char *sccsid2 = "@(#)auth_unix.c 1.19 87/08/11 Copyr 1984 Sun Micro"; |
---|
35 | static char *sccsid = "@(#)auth_unix.c 2.2 88/08/01 4.0 RPCSRC"; |
---|
36 | #endif |
---|
37 | #include <sys/cdefs.h> |
---|
38 | __FBSDID("$FreeBSD$"); |
---|
39 | |
---|
40 | /* |
---|
41 | * auth_unix.c, Implements UNIX style authentication parameters. |
---|
42 | * |
---|
43 | * Copyright (C) 1984, Sun Microsystems, Inc. |
---|
44 | * |
---|
45 | * The system is very weak. The client uses no encryption for it's |
---|
46 | * credentials and only sends null verifiers. The server sends backs |
---|
47 | * null verifiers or optionally a verifier that suggests a new short hand |
---|
48 | * for the credentials. |
---|
49 | * |
---|
50 | */ |
---|
51 | |
---|
52 | #include "namespace.h" |
---|
53 | #include "reentrant.h" |
---|
54 | #include <rtems/bsd/sys/param.h> |
---|
55 | |
---|
56 | #include <assert.h> |
---|
57 | #include <err.h> |
---|
58 | #include <stdio.h> |
---|
59 | #include <stdlib.h> |
---|
60 | #include <unistd.h> |
---|
61 | #include <string.h> |
---|
62 | |
---|
63 | #include <rpc/types.h> |
---|
64 | #include <rpc/xdr.h> |
---|
65 | #include <rpc/auth.h> |
---|
66 | #include <rpc/auth_unix.h> |
---|
67 | #include "un-namespace.h" |
---|
68 | #include "mt_misc.h" |
---|
69 | |
---|
70 | /* auth_unix.c */ |
---|
71 | static void authunix_nextverf (AUTH *); |
---|
72 | static bool_t authunix_marshal (AUTH *, XDR *); |
---|
73 | static bool_t authunix_validate (AUTH *, struct opaque_auth *); |
---|
74 | static bool_t authunix_refresh (AUTH *, void *); |
---|
75 | static void authunix_destroy (AUTH *); |
---|
76 | static void marshal_new_auth (AUTH *); |
---|
77 | static struct auth_ops *authunix_ops (void); |
---|
78 | |
---|
79 | /* |
---|
80 | * This struct is pointed to by the ah_private field of an auth_handle. |
---|
81 | */ |
---|
82 | struct audata { |
---|
83 | struct opaque_auth au_origcred; /* original credentials */ |
---|
84 | struct opaque_auth au_shcred; /* short hand cred */ |
---|
85 | u_long au_shfaults; /* short hand cache faults */ |
---|
86 | char au_marshed[MAX_AUTH_BYTES]; |
---|
87 | u_int au_mpos; /* xdr pos at end of marshed */ |
---|
88 | }; |
---|
89 | #define AUTH_PRIVATE(auth) ((struct audata *)auth->ah_private) |
---|
90 | |
---|
91 | /* |
---|
92 | * Create a unix style authenticator. |
---|
93 | * Returns an auth handle with the given stuff in it. |
---|
94 | */ |
---|
95 | AUTH * |
---|
96 | authunix_create(machname, uid, gid, len, aup_gids) |
---|
97 | char *machname; |
---|
98 | u_int uid; |
---|
99 | u_int gid; |
---|
100 | int len; |
---|
101 | u_int *aup_gids; |
---|
102 | { |
---|
103 | struct authunix_parms aup; |
---|
104 | char mymem[MAX_AUTH_BYTES]; |
---|
105 | struct timeval now; |
---|
106 | XDR xdrs; |
---|
107 | AUTH *auth; |
---|
108 | struct audata *au; |
---|
109 | |
---|
110 | /* |
---|
111 | * Allocate and set up auth handle |
---|
112 | */ |
---|
113 | au = NULL; |
---|
114 | auth = mem_alloc(sizeof(*auth)); |
---|
115 | #ifndef _KERNEL |
---|
116 | if (auth == NULL) { |
---|
117 | warnx("authunix_create: out of memory"); |
---|
118 | goto cleanup_authunix_create; |
---|
119 | } |
---|
120 | #endif |
---|
121 | au = mem_alloc(sizeof(*au)); |
---|
122 | #ifndef _KERNEL |
---|
123 | if (au == NULL) { |
---|
124 | warnx("authunix_create: out of memory"); |
---|
125 | goto cleanup_authunix_create; |
---|
126 | } |
---|
127 | #endif |
---|
128 | auth->ah_ops = authunix_ops(); |
---|
129 | auth->ah_private = (caddr_t)au; |
---|
130 | auth->ah_verf = au->au_shcred = _null_auth; |
---|
131 | au->au_shfaults = 0; |
---|
132 | au->au_origcred.oa_base = NULL; |
---|
133 | |
---|
134 | /* |
---|
135 | * fill in param struct from the given params |
---|
136 | */ |
---|
137 | (void)gettimeofday(&now, NULL); |
---|
138 | aup.aup_time = now.tv_sec; |
---|
139 | aup.aup_machname = machname; |
---|
140 | aup.aup_uid = uid; |
---|
141 | aup.aup_gid = gid; |
---|
142 | aup.aup_len = (u_int)len; |
---|
143 | aup.aup_gids = aup_gids; |
---|
144 | |
---|
145 | /* |
---|
146 | * Serialize the parameters into origcred |
---|
147 | */ |
---|
148 | xdrmem_create(&xdrs, mymem, MAX_AUTH_BYTES, XDR_ENCODE); |
---|
149 | if (! xdr_authunix_parms(&xdrs, &aup)) |
---|
150 | abort(); |
---|
151 | au->au_origcred.oa_length = len = XDR_GETPOS(&xdrs); |
---|
152 | au->au_origcred.oa_flavor = AUTH_UNIX; |
---|
153 | #ifdef _KERNEL |
---|
154 | au->au_origcred.oa_base = mem_alloc((u_int) len); |
---|
155 | #else |
---|
156 | if ((au->au_origcred.oa_base = mem_alloc((u_int) len)) == NULL) { |
---|
157 | warnx("authunix_create: out of memory"); |
---|
158 | goto cleanup_authunix_create; |
---|
159 | } |
---|
160 | #endif |
---|
161 | memmove(au->au_origcred.oa_base, mymem, (size_t)len); |
---|
162 | |
---|
163 | /* |
---|
164 | * set auth handle to reflect new cred. |
---|
165 | */ |
---|
166 | auth->ah_cred = au->au_origcred; |
---|
167 | marshal_new_auth(auth); |
---|
168 | return (auth); |
---|
169 | #ifndef _KERNEL |
---|
170 | cleanup_authunix_create: |
---|
171 | if (auth) |
---|
172 | mem_free(auth, sizeof(*auth)); |
---|
173 | if (au) { |
---|
174 | if (au->au_origcred.oa_base) |
---|
175 | mem_free(au->au_origcred.oa_base, (u_int)len); |
---|
176 | mem_free(au, sizeof(*au)); |
---|
177 | } |
---|
178 | return (NULL); |
---|
179 | #endif |
---|
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 | AUTH *auth; |
---|
190 | int ngids; |
---|
191 | long ngids_max; |
---|
192 | char machname[MAXHOSTNAMELEN + 1]; |
---|
193 | u_int uid; |
---|
194 | u_int gid; |
---|
195 | u_int *gids; |
---|
196 | |
---|
197 | ngids_max = sysconf(_SC_NGROUPS_MAX) + 1; |
---|
198 | gids = malloc(sizeof(gid_t) * ngids_max); |
---|
199 | if (gids == NULL) |
---|
200 | return (NULL); |
---|
201 | |
---|
202 | if (gethostname(machname, sizeof machname) == -1) |
---|
203 | abort(); |
---|
204 | machname[sizeof(machname) - 1] = 0; |
---|
205 | uid = geteuid(); |
---|
206 | gid = getegid(); |
---|
207 | if ((ngids = getgroups(ngids_max, gids)) < 0) |
---|
208 | abort(); |
---|
209 | if (ngids > NGRPS) |
---|
210 | ngids = NGRPS; |
---|
211 | /* XXX: interface problem; we should translate from uid_t and gid_t */ |
---|
212 | auth = authunix_create(machname, uid, gid, ngids, gids); |
---|
213 | free(gids); |
---|
214 | return (auth); |
---|
215 | } |
---|
216 | |
---|
217 | /* |
---|
218 | * authunix operations |
---|
219 | */ |
---|
220 | |
---|
221 | /* ARGSUSED */ |
---|
222 | static void |
---|
223 | authunix_nextverf(auth) |
---|
224 | AUTH *auth; |
---|
225 | { |
---|
226 | /* no action necessary */ |
---|
227 | } |
---|
228 | |
---|
229 | static bool_t |
---|
230 | authunix_marshal(auth, xdrs) |
---|
231 | AUTH *auth; |
---|
232 | XDR *xdrs; |
---|
233 | { |
---|
234 | struct audata *au; |
---|
235 | |
---|
236 | assert(auth != NULL); |
---|
237 | assert(xdrs != NULL); |
---|
238 | |
---|
239 | au = AUTH_PRIVATE(auth); |
---|
240 | return (XDR_PUTBYTES(xdrs, au->au_marshed, au->au_mpos)); |
---|
241 | } |
---|
242 | |
---|
243 | static bool_t |
---|
244 | authunix_validate(auth, verf) |
---|
245 | AUTH *auth; |
---|
246 | struct opaque_auth *verf; |
---|
247 | { |
---|
248 | struct audata *au; |
---|
249 | XDR xdrs; |
---|
250 | |
---|
251 | assert(auth != NULL); |
---|
252 | assert(verf != NULL); |
---|
253 | |
---|
254 | if (verf->oa_flavor == AUTH_SHORT) { |
---|
255 | au = AUTH_PRIVATE(auth); |
---|
256 | xdrmem_create(&xdrs, verf->oa_base, verf->oa_length, |
---|
257 | XDR_DECODE); |
---|
258 | |
---|
259 | if (au->au_shcred.oa_base != NULL) { |
---|
260 | mem_free(au->au_shcred.oa_base, |
---|
261 | au->au_shcred.oa_length); |
---|
262 | au->au_shcred.oa_base = NULL; |
---|
263 | } |
---|
264 | if (xdr_opaque_auth(&xdrs, &au->au_shcred)) { |
---|
265 | auth->ah_cred = au->au_shcred; |
---|
266 | } else { |
---|
267 | xdrs.x_op = XDR_FREE; |
---|
268 | (void)xdr_opaque_auth(&xdrs, &au->au_shcred); |
---|
269 | au->au_shcred.oa_base = NULL; |
---|
270 | auth->ah_cred = au->au_origcred; |
---|
271 | } |
---|
272 | marshal_new_auth(auth); |
---|
273 | } |
---|
274 | return (TRUE); |
---|
275 | } |
---|
276 | |
---|
277 | static bool_t |
---|
278 | authunix_refresh(AUTH *auth, void *dummy) |
---|
279 | { |
---|
280 | struct audata *au = AUTH_PRIVATE(auth); |
---|
281 | struct authunix_parms aup; |
---|
282 | struct timeval now; |
---|
283 | XDR xdrs; |
---|
284 | int stat; |
---|
285 | |
---|
286 | assert(auth != NULL); |
---|
287 | |
---|
288 | if (auth->ah_cred.oa_base == au->au_origcred.oa_base) { |
---|
289 | /* there is no hope. Punt */ |
---|
290 | return (FALSE); |
---|
291 | } |
---|
292 | au->au_shfaults ++; |
---|
293 | |
---|
294 | /* first deserialize the creds back into a struct authunix_parms */ |
---|
295 | aup.aup_machname = NULL; |
---|
296 | aup.aup_gids = NULL; |
---|
297 | xdrmem_create(&xdrs, au->au_origcred.oa_base, |
---|
298 | au->au_origcred.oa_length, XDR_DECODE); |
---|
299 | stat = xdr_authunix_parms(&xdrs, &aup); |
---|
300 | if (! stat) |
---|
301 | goto done; |
---|
302 | |
---|
303 | /* update the time and serialize in place */ |
---|
304 | (void)gettimeofday(&now, NULL); |
---|
305 | aup.aup_time = now.tv_sec; |
---|
306 | xdrs.x_op = XDR_ENCODE; |
---|
307 | XDR_SETPOS(&xdrs, 0); |
---|
308 | stat = xdr_authunix_parms(&xdrs, &aup); |
---|
309 | if (! stat) |
---|
310 | goto done; |
---|
311 | auth->ah_cred = au->au_origcred; |
---|
312 | marshal_new_auth(auth); |
---|
313 | done: |
---|
314 | /* free the struct authunix_parms created by deserializing */ |
---|
315 | xdrs.x_op = XDR_FREE; |
---|
316 | (void)xdr_authunix_parms(&xdrs, &aup); |
---|
317 | XDR_DESTROY(&xdrs); |
---|
318 | return (stat); |
---|
319 | } |
---|
320 | |
---|
321 | static void |
---|
322 | authunix_destroy(auth) |
---|
323 | AUTH *auth; |
---|
324 | { |
---|
325 | struct audata *au; |
---|
326 | |
---|
327 | assert(auth != NULL); |
---|
328 | |
---|
329 | au = AUTH_PRIVATE(auth); |
---|
330 | mem_free(au->au_origcred.oa_base, au->au_origcred.oa_length); |
---|
331 | |
---|
332 | if (au->au_shcred.oa_base != NULL) |
---|
333 | mem_free(au->au_shcred.oa_base, au->au_shcred.oa_length); |
---|
334 | |
---|
335 | mem_free(auth->ah_private, sizeof(struct audata)); |
---|
336 | |
---|
337 | if (auth->ah_verf.oa_base != NULL) |
---|
338 | mem_free(auth->ah_verf.oa_base, auth->ah_verf.oa_length); |
---|
339 | |
---|
340 | mem_free(auth, sizeof(*auth)); |
---|
341 | } |
---|
342 | |
---|
343 | /* |
---|
344 | * Marshals (pre-serializes) an auth struct. |
---|
345 | * sets private data, au_marshed and au_mpos |
---|
346 | */ |
---|
347 | static void |
---|
348 | marshal_new_auth(auth) |
---|
349 | AUTH *auth; |
---|
350 | { |
---|
351 | XDR xdr_stream; |
---|
352 | XDR *xdrs = &xdr_stream; |
---|
353 | struct audata *au; |
---|
354 | |
---|
355 | assert(auth != NULL); |
---|
356 | |
---|
357 | au = AUTH_PRIVATE(auth); |
---|
358 | xdrmem_create(xdrs, au->au_marshed, MAX_AUTH_BYTES, XDR_ENCODE); |
---|
359 | if ((! xdr_opaque_auth(xdrs, &(auth->ah_cred))) || |
---|
360 | (! xdr_opaque_auth(xdrs, &(auth->ah_verf)))) |
---|
361 | warnx("auth_none.c - Fatal marshalling problem"); |
---|
362 | else |
---|
363 | au->au_mpos = XDR_GETPOS(xdrs); |
---|
364 | XDR_DESTROY(xdrs); |
---|
365 | } |
---|
366 | |
---|
367 | static struct auth_ops * |
---|
368 | authunix_ops() |
---|
369 | { |
---|
370 | static struct auth_ops ops; |
---|
371 | |
---|
372 | /* VARIABLES PROTECTED BY ops_lock: ops */ |
---|
373 | |
---|
374 | mutex_lock(&ops_lock); |
---|
375 | if (ops.ah_nextverf == NULL) { |
---|
376 | ops.ah_nextverf = authunix_nextverf; |
---|
377 | ops.ah_marshal = authunix_marshal; |
---|
378 | ops.ah_validate = authunix_validate; |
---|
379 | ops.ah_refresh = authunix_refresh; |
---|
380 | ops.ah_destroy = authunix_destroy; |
---|
381 | } |
---|
382 | mutex_unlock(&ops_lock); |
---|
383 | return (&ops); |
---|
384 | } |
---|