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