]> git.saurik.com Git - apple/libinfo.git/blob - rpc.subproj/auth_unix.c
Libinfo-129.4.tar.gz
[apple/libinfo.git] / rpc.subproj / auth_unix.c
1 /*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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.
13 *
14 * The Original Code and all software distributed under the License are
15 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
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.
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";*/
56 static char *rcsid = "$Id: auth_unix.c,v 1.4 2002/02/19 20:36:22 epeyton Exp $";
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
71 #include <stdio.h>
72 #include <stdlib.h>
73 #include <string.h>
74 #include <unistd.h>
75 #include <sys/param.h>
76 #include <sys/types.h>
77 #include <netinet/in.h>
78
79 #include <rpc/types.h>
80 #include <rpc/xdr.h>
81 #include <rpc/auth.h>
82 #include <rpc/auth_unix.h>
83
84 extern bool_t xdr_opaque_auth();
85
86 /*
87 * Unix authenticator operations vector
88 */
89 static void authunix_nextverf();
90 static bool_t authunix_marshal();
91 static bool_t authunix_validate();
92 static bool_t authunix_refresh();
93 static void authunix_destroy();
94
95 static struct auth_ops auth_unix_ops = {
96 authunix_nextverf,
97 authunix_marshal,
98 authunix_validate,
99 authunix_refresh,
100 authunix_destroy
101 };
102
103 /*
104 * This struct is pointed to by the ah_private field of an auth_handle.
105 */
106 struct audata {
107 struct opaque_auth au_origcred; /* original credentials */
108 struct opaque_auth au_shcred; /* short hand cred */
109 u_long au_shfaults; /* short hand cache faults */
110 char au_marshed[MAX_AUTH_BYTES];
111 u_int au_mpos; /* xdr pos at end of marshed */
112 };
113 #define AUTH_PRIVATE(auth) ((struct audata *)auth->ah_private)
114
115 static void marshal_new_auth();
116
117
118 /*
119 * Create a unix style authenticator.
120 * Returns an auth handle with the given stuff in it.
121 */
122 AUTH *
123 authunix_create(machname, uid, gid, len, aup_gids)
124 char *machname;
125 int uid;
126 int gid;
127 register int len;
128 int *aup_gids;
129 {
130 struct authunix_parms aup;
131 char mymem[MAX_AUTH_BYTES];
132 struct timeval now;
133 XDR xdrs;
134 register AUTH *auth;
135 register struct audata *au;
136
137 /*
138 * Allocate and set up auth handle
139 */
140 auth = (AUTH *)mem_alloc(sizeof(*auth));
141 #ifndef KERNEL
142 if (auth == NULL) {
143 (void)fprintf(stderr, "authunix_create: out of memory\n");
144 return (NULL);
145 }
146 #endif
147 au = (struct audata *)mem_alloc(sizeof(*au));
148 #ifndef KERNEL
149 if (au == NULL) {
150 (void)fprintf(stderr, "authunix_create: out of memory\n");
151 return (NULL);
152 }
153 #endif
154 auth->ah_ops = &auth_unix_ops;
155 auth->ah_private = (caddr_t)au;
156 auth->ah_verf = au->au_shcred = _null_auth;
157 au->au_shfaults = 0;
158
159 /*
160 * fill in param struct from the given params
161 */
162 (void)gettimeofday(&now, (struct timezone *)0);
163 aup.aup_time = now.tv_sec;
164 aup.aup_machname = machname;
165 aup.aup_uid = uid;
166 aup.aup_gid = gid;
167 aup.aup_len = (u_int)len;
168 aup.aup_gids = aup_gids;
169
170 /*
171 * Serialize the parameters into origcred
172 */
173 xdrmem_create(&xdrs, mymem, MAX_AUTH_BYTES, XDR_ENCODE);
174 if (! xdr_authunix_parms(&xdrs, &aup))
175 abort();
176 au->au_origcred.oa_length = len = XDR_GETPOS(&xdrs);
177 au->au_origcred.oa_flavor = AUTH_UNIX;
178 #ifdef KERNEL
179 au->au_origcred.oa_base = mem_alloc((u_int) len);
180 #else
181 if ((au->au_origcred.oa_base = mem_alloc((u_int) len)) == NULL) {
182 (void)fprintf(stderr, "authunix_create: out of memory\n");
183 return (NULL);
184 }
185 #endif
186 bcopy(mymem, au->au_origcred.oa_base, (u_int)len);
187
188 /*
189 * set auth handle to reflect new cred.
190 */
191 auth->ah_cred = au->au_origcred;
192 marshal_new_auth(auth);
193 return (auth);
194 }
195
196 /*
197 * Some servers will refuse mounts if the group list is larger
198 * than it expects (like 8). This allows the application to set
199 * the maximum size of the group list that will be sent.
200 */
201
202 static int maxgrplist = NGROUPS;
203
204 void
205 set_rpc_maxgrouplist(num)
206 int num;
207 {
208 if (num < NGROUPS)
209 maxgrplist = num;
210 }
211
212 /*
213 * Returns an auth handle with parameters determined by doing lots of
214 * syscalls.
215 */
216 AUTH *
217 authunix_create_default()
218 {
219 register int len;
220 char machname[MAX_MACHINE_NAME + 1];
221 register int uid;
222 register int gid;
223 int gids[NGROUPS];
224
225 if (gethostname(machname, MAX_MACHINE_NAME) == -1)
226 abort();
227 machname[MAX_MACHINE_NAME] = 0;
228 uid = geteuid();
229 gid = getegid();
230 if ((len = getgroups(NGROUPS, gids)) < 0)
231 abort();
232 if (len > maxgrplist) {
233 len = maxgrplist;
234 }
235 return (authunix_create(machname, uid, gid, len, gids));
236 }
237
238 /*
239 * authunix operations
240 */
241
242 static void
243 authunix_nextverf(auth)
244 AUTH *auth;
245 {
246 /* no action necessary */
247 }
248
249 static bool_t
250 authunix_marshal(auth, xdrs)
251 AUTH *auth;
252 XDR *xdrs;
253 {
254 register struct audata *au = AUTH_PRIVATE(auth);
255
256 return (XDR_PUTBYTES(xdrs, au->au_marshed, au->au_mpos));
257 }
258
259 static bool_t
260 authunix_validate(auth, verf)
261 register AUTH *auth;
262 struct opaque_auth verf;
263 {
264 register struct audata *au;
265 XDR xdrs;
266
267 if (verf.oa_flavor == AUTH_SHORT) {
268 au = AUTH_PRIVATE(auth);
269 xdrmem_create(&xdrs, verf.oa_base, verf.oa_length, XDR_DECODE);
270
271 if (au->au_shcred.oa_base != NULL) {
272 mem_free(au->au_shcred.oa_base,
273 au->au_shcred.oa_length);
274 au->au_shcred.oa_base = NULL;
275 }
276 if (xdr_opaque_auth(&xdrs, &au->au_shcred)) {
277 auth->ah_cred = au->au_shcred;
278 } else {
279 xdrs.x_op = XDR_FREE;
280 (void)xdr_opaque_auth(&xdrs, &au->au_shcred);
281 au->au_shcred.oa_base = NULL;
282 auth->ah_cred = au->au_origcred;
283 }
284 marshal_new_auth(auth);
285 }
286 return (TRUE);
287 }
288
289 static bool_t
290 authunix_refresh(auth)
291 register AUTH *auth;
292 {
293 register struct audata *au = AUTH_PRIVATE(auth);
294 struct authunix_parms aup;
295 struct timeval now;
296 XDR xdrs;
297 register int stat;
298
299 if (auth->ah_cred.oa_base == au->au_origcred.oa_base) {
300 /* there is no hope. Punt */
301 return (FALSE);
302 }
303 au->au_shfaults ++;
304
305 /* first deserialize the creds back into a struct authunix_parms */
306 aup.aup_machname = NULL;
307 aup.aup_gids = (int *)NULL;
308 xdrmem_create(&xdrs, au->au_origcred.oa_base,
309 au->au_origcred.oa_length, XDR_DECODE);
310 stat = xdr_authunix_parms(&xdrs, &aup);
311 if (! stat)
312 goto done;
313
314 /* update the time and serialize in place */
315 (void)gettimeofday(&now, (struct timezone *)0);
316 aup.aup_time = now.tv_sec;
317 xdrs.x_op = XDR_ENCODE;
318 XDR_SETPOS(&xdrs, 0);
319 stat = xdr_authunix_parms(&xdrs, &aup);
320 if (! stat)
321 goto done;
322 auth->ah_cred = au->au_origcred;
323 marshal_new_auth(auth);
324 done:
325 /* free the struct authunix_parms created by deserializing */
326 xdrs.x_op = XDR_FREE;
327 (void)xdr_authunix_parms(&xdrs, &aup);
328 XDR_DESTROY(&xdrs);
329 return (stat);
330 }
331
332 static void
333 authunix_destroy(auth)
334 register AUTH *auth;
335 {
336 register struct audata *au = AUTH_PRIVATE(auth);
337
338 mem_free(au->au_origcred.oa_base, au->au_origcred.oa_length);
339
340 if (au->au_shcred.oa_base != NULL)
341 mem_free(au->au_shcred.oa_base, au->au_shcred.oa_length);
342
343 mem_free(auth->ah_private, sizeof(struct audata));
344
345 if (auth->ah_verf.oa_base != NULL)
346 mem_free(auth->ah_verf.oa_base, auth->ah_verf.oa_length);
347
348 mem_free((caddr_t)auth, sizeof(*auth));
349 }
350
351 /*
352 * Marshals (pre-serializes) an auth struct.
353 * sets private data, au_marshed and au_mpos
354 */
355 static void
356 marshal_new_auth(auth)
357 register AUTH *auth;
358 {
359 XDR xdr_stream;
360 register XDR *xdrs = &xdr_stream;
361 register struct audata *au = AUTH_PRIVATE(auth);
362
363 xdrmem_create(xdrs, au->au_marshed, MAX_AUTH_BYTES, XDR_ENCODE);
364 if ((! xdr_opaque_auth(xdrs, &(auth->ah_cred))) ||
365 (! xdr_opaque_auth(xdrs, &(auth->ah_verf)))) {
366 perror("auth_none.c - Fatal marshalling problem");
367 } else {
368 au->au_mpos = XDR_GETPOS(xdrs);
369 }
370 XDR_DESTROY(xdrs);
371 }