Debian Codegraph @

extra/tirpc/libtirpc-1.3.5/src/auth_unix.c

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