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