Update.
[glibc.git] / sunrpc / auth_unix.c
blob9314119a7084ceb7c9d1a13e558c4ff1097549b4
1 /* @(#)auth_unix.c 2.2 88/08/01 4.0 RPCSRC */
2 /*
3 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
4 * unrestricted use provided that this legend is included on all tape
5 * media and as a part of the software program in whole or part. Users
6 * may copy or modify Sun RPC without charge, but are not authorized
7 * to license or distribute it to anyone else except as part of a product or
8 * program developed by the user.
10 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
11 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
12 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
14 * Sun RPC is provided with no support and without any obligation on the
15 * part of Sun Microsystems, Inc. to assist in its use, correction,
16 * modification or enhancement.
18 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
19 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
20 * OR ANY PART THEREOF.
22 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
23 * or profits or other special, indirect and consequential damages, even if
24 * Sun has been advised of the possibility of such damages.
26 * Sun Microsystems, Inc.
27 * 2550 Garcia Avenue
28 * Mountain View, California 94043
30 #if !defined(lint) && defined(SCCSIDS)
31 static char sccsid[] = "@(#)auth_unix.c 1.19 87/08/11 Copyr 1984 Sun Micro";
32 #endif
35 * auth_unix.c, Implements UNIX style authentication parameters.
37 * Copyright (C) 1984, Sun Microsystems, Inc.
39 * The system is very weak. The client uses no encryption for it's
40 * credentials and only sends null verifiers. The server sends backs
41 * null verifiers or optionally a verifier that suggests a new short hand
42 * for the credentials.
46 #include <limits.h>
47 #include <stdio.h>
48 #include <string.h>
49 #include <unistd.h>
50 #include <sys/param.h>
52 #include <rpc/types.h>
53 #include <rpc/xdr.h>
54 #include <rpc/auth.h>
55 #include <rpc/auth_unix.h>
58 * Unix authenticator operations vector
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 *);
64 static void authunix_destroy (AUTH *);
66 static struct auth_ops auth_unix_ops =
68 authunix_nextverf,
69 authunix_marshal,
70 authunix_validate,
71 authunix_refresh,
72 authunix_destroy
76 * This struct is pointed to by the ah_private field of an auth_handle.
78 struct audata
80 struct opaque_auth au_origcred; /* original credentials */
81 struct opaque_auth au_shcred; /* short hand cred */
82 u_long au_shfaults; /* short hand cache faults */
83 char au_marshed[MAX_AUTH_BYTES];
84 u_int au_mpos; /* xdr pos at end of marshed */
86 #define AUTH_PRIVATE(auth) ((struct audata *)auth->ah_private)
88 static bool_t marshal_new_auth (AUTH *) internal_function;
92 * Create a unix style authenticator.
93 * Returns an auth handle with the given stuff in it.
95 AUTH *
96 authunix_create (machname, uid, gid, len, aup_gids)
97 char *machname;
98 uid_t uid;
99 gid_t gid;
100 int len;
101 gid_t *aup_gids;
103 struct authunix_parms aup;
104 char mymem[MAX_AUTH_BYTES];
105 struct timeval now;
106 XDR xdrs;
107 AUTH *auth;
108 struct audata *au;
111 * Allocate and set up auth handle
113 auth = (AUTH *) mem_alloc (sizeof (*auth));
114 if (auth == NULL)
116 (void) fprintf (stderr, _("authunix_create: out of memory\n"));
117 return NULL;
119 au = (struct audata *) mem_alloc (sizeof (*au));
120 if (au == NULL)
122 (void) fprintf (stderr, _("authunix_create: out of memory\n"));
123 return NULL;
125 auth->ah_ops = &auth_unix_ops;
126 auth->ah_private = (caddr_t) au;
127 auth->ah_verf = au->au_shcred = _null_auth;
128 au->au_shfaults = 0;
131 * fill in param struct from the given params
133 (void) __gettimeofday (&now, (struct timezone *) 0);
134 aup.aup_time = now.tv_sec;
135 aup.aup_machname = machname;
136 aup.aup_uid = uid;
137 aup.aup_gid = gid;
138 aup.aup_len = (u_int) len;
139 aup.aup_gids = aup_gids;
142 * Serialize the parameters into origcred
144 xdrmem_create (&xdrs, mymem, MAX_AUTH_BYTES, XDR_ENCODE);
145 if (!xdr_authunix_parms (&xdrs, &aup))
146 abort ();
147 au->au_origcred.oa_length = len = XDR_GETPOS (&xdrs);
148 au->au_origcred.oa_flavor = AUTH_UNIX;
149 if ((au->au_origcred.oa_base = mem_alloc ((u_int) len)) == NULL)
151 (void) fprintf (stderr, _("authunix_create: out of memory\n"));
152 return NULL;
154 bcopy (mymem, au->au_origcred.oa_base, (u_int) len);
157 * set auth handle to reflect new cred.
159 auth->ah_cred = au->au_origcred;
160 marshal_new_auth (auth);
161 return auth;
165 * Returns an auth handle with parameters determined by doing lots of
166 * syscalls.
168 AUTH *
169 authunix_create_default (void)
171 int len;
172 char machname[MAX_MACHINE_NAME + 1];
173 uid_t uid;
174 gid_t gid;
175 int max_nr_groups = __sysconf (_SC_NGROUPS_MAX);
176 gid_t gids[max_nr_groups];
178 if (__gethostname (machname, MAX_MACHINE_NAME) == -1)
179 abort ();
180 machname[MAX_MACHINE_NAME] = 0;
181 uid = __geteuid ();
182 gid = __getegid ();
184 if ((len = __getgroups (max_nr_groups, gids)) < 0)
185 abort ();
186 /* This braindamaged Sun code forces us here to truncate the
187 list of groups to NGRPS members since the code in
188 authuxprot.c transforms a fixed array. Grrr. */
189 return authunix_create (machname, uid, gid, MIN (NGRPS, len), gids);
193 * authunix operations
196 static void
197 authunix_nextverf (AUTH *auth)
199 /* no action necessary */
202 static bool_t
203 authunix_marshal (AUTH *auth, XDR *xdrs)
205 struct audata *au = AUTH_PRIVATE (auth);
207 return XDR_PUTBYTES (xdrs, au->au_marshed, au->au_mpos);
210 static bool_t
211 authunix_validate (AUTH *auth, struct opaque_auth *verf)
213 struct audata *au;
214 XDR xdrs;
216 if (verf->oa_flavor == AUTH_SHORT)
218 au = AUTH_PRIVATE (auth);
219 xdrmem_create (&xdrs, verf->oa_base, verf->oa_length,
220 XDR_DECODE);
222 if (au->au_shcred.oa_base != NULL)
224 mem_free (au->au_shcred.oa_base,
225 au->au_shcred.oa_length);
226 au->au_shcred.oa_base = NULL;
228 if (xdr_opaque_auth (&xdrs, &au->au_shcred))
230 auth->ah_cred = au->au_shcred;
232 else
234 xdrs.x_op = XDR_FREE;
235 (void) xdr_opaque_auth (&xdrs, &au->au_shcred);
236 au->au_shcred.oa_base = NULL;
237 auth->ah_cred = au->au_origcred;
239 marshal_new_auth (auth);
241 return TRUE;
244 static bool_t
245 authunix_refresh (AUTH *auth)
247 struct audata *au = AUTH_PRIVATE (auth);
248 struct authunix_parms aup;
249 struct timeval now;
250 XDR xdrs;
251 int stat;
253 if (auth->ah_cred.oa_base == au->au_origcred.oa_base)
255 /* there is no hope. Punt */
256 return FALSE;
258 au->au_shfaults++;
260 /* first deserialize the creds back into a struct authunix_parms */
261 aup.aup_machname = NULL;
262 aup.aup_gids = (gid_t *) NULL;
263 xdrmem_create (&xdrs, au->au_origcred.oa_base,
264 au->au_origcred.oa_length, XDR_DECODE);
265 stat = xdr_authunix_parms (&xdrs, &aup);
266 if (!stat)
267 goto done;
269 /* update the time and serialize in place */
270 (void) __gettimeofday (&now, (struct timezone *) 0);
271 aup.aup_time = now.tv_sec;
272 xdrs.x_op = XDR_ENCODE;
273 XDR_SETPOS (&xdrs, 0);
274 stat = xdr_authunix_parms (&xdrs, &aup);
275 if (!stat)
276 goto done;
277 auth->ah_cred = au->au_origcred;
278 marshal_new_auth (auth);
279 done:
280 /* free the struct authunix_parms created by deserializing */
281 xdrs.x_op = XDR_FREE;
282 (void) xdr_authunix_parms (&xdrs, &aup);
283 XDR_DESTROY (&xdrs);
284 return stat;
287 static void
288 authunix_destroy (AUTH *auth)
290 struct audata *au = AUTH_PRIVATE (auth);
292 mem_free (au->au_origcred.oa_base, au->au_origcred.oa_length);
294 if (au->au_shcred.oa_base != NULL)
295 mem_free (au->au_shcred.oa_base, au->au_shcred.oa_length);
297 mem_free (auth->ah_private, sizeof (struct audata));
299 if (auth->ah_verf.oa_base != NULL)
300 mem_free (auth->ah_verf.oa_base, auth->ah_verf.oa_length);
302 mem_free ((caddr_t) auth, sizeof (*auth));
306 * Marshals (pre-serializes) an auth struct.
307 * sets private data, au_marshed and au_mpos
309 static bool_t
310 internal_function
311 marshal_new_auth (AUTH *auth)
313 XDR xdr_stream;
314 XDR *xdrs = &xdr_stream;
315 struct audata *au = AUTH_PRIVATE (auth);
317 xdrmem_create (xdrs, au->au_marshed, MAX_AUTH_BYTES, XDR_ENCODE);
318 if ((!xdr_opaque_auth (xdrs, &(auth->ah_cred))) ||
319 (!xdr_opaque_auth (xdrs, &(auth->ah_verf))))
321 perror (_("auth_none.c - Fatal marshalling problem"));
323 else
325 au->au_mpos = XDR_GETPOS (xdrs);
327 XDR_DESTROY (xdrs);
329 return TRUE;