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