Update.
[glibc.git] / sunrpc / auth_unix.c
bloba03ce0219ab9013e7bd97494351aed8fcde3f97b
1 /*
2 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
3 * unrestricted use provided that this legend is included on all tape
4 * media and as a part of the software program in whole or part. Users
5 * may copy or modify Sun RPC without charge, but are not authorized
6 * to license or distribute it to anyone else except as part of a product or
7 * program developed by the user.
9 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
10 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
11 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
13 * Sun RPC is provided with no support and without any obligation on the
14 * part of Sun Microsystems, Inc. to assist in its use, correction,
15 * modification or enhancement.
17 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
18 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
19 * OR ANY PART THEREOF.
21 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
22 * or profits or other special, indirect and consequential damages, even if
23 * Sun has been advised of the possibility of such damages.
25 * Sun Microsystems, Inc.
26 * 2550 Garcia Avenue
27 * Mountain View, California 94043
30 * Copyright (C) 1984, Sun Microsystems, Inc.
33 * auth_unix.c, Implements UNIX style authentication parameters.
35 * The system is very weak. The client uses no encryption for it's
36 * credentials and only sends null verifiers. The server sends backs
37 * null verifiers or optionally a verifier that suggests a new short hand
38 * for the credentials.
41 #include <limits.h>
42 #include <stdio.h>
43 #include <string.h>
44 #include <unistd.h>
45 #include <libintl.h>
46 #include <sys/param.h>
48 #include <rpc/types.h>
49 #include <rpc/xdr.h>
50 #include <rpc/auth.h>
51 #include <rpc/auth_unix.h>
54 * Unix authenticator operations vector
56 static void authunix_nextverf (AUTH *);
57 static bool_t authunix_marshal (AUTH *, XDR *);
58 static bool_t authunix_validate (AUTH *, struct opaque_auth *);
59 static bool_t authunix_refresh (AUTH *);
60 static void authunix_destroy (AUTH *);
62 static struct auth_ops auth_unix_ops = {
63 authunix_nextverf,
64 authunix_marshal,
65 authunix_validate,
66 authunix_refresh,
67 authunix_destroy
71 * This struct is pointed to by the ah_private field of an auth_handle.
73 struct audata {
74 struct opaque_auth au_origcred; /* original credentials */
75 struct opaque_auth au_shcred; /* short hand cred */
76 u_long au_shfaults; /* short hand cache faults */
77 char au_marshed[MAX_AUTH_BYTES];
78 u_int au_mpos; /* xdr pos at end of marshed */
80 #define AUTH_PRIVATE(auth) ((struct audata *)auth->ah_private)
82 static bool_t marshal_new_auth (AUTH *) internal_function;
86 * Create a unix style authenticator.
87 * Returns an auth handle with the given stuff in it.
89 AUTH *
90 authunix_create (char *machname, uid_t uid, gid_t gid, int len,
91 gid_t *aup_gids)
93 struct authunix_parms aup;
94 char mymem[MAX_AUTH_BYTES];
95 struct timeval now;
96 XDR xdrs;
97 AUTH *auth;
98 struct audata *au;
101 * Allocate and set up auth handle
103 auth = (AUTH *) mem_alloc (sizeof (*auth));
104 if (auth == NULL)
106 (void) fprintf (stderr, _("authunix_create: out of memory\n"));
107 return NULL;
109 au = (struct audata *) mem_alloc (sizeof (*au));
110 if (au == NULL)
112 (void) fprintf (stderr, _("authunix_create: out of memory\n"));
113 return NULL;
115 auth->ah_ops = &auth_unix_ops;
116 auth->ah_private = (caddr_t) au;
117 auth->ah_verf = au->au_shcred = _null_auth;
118 au->au_shfaults = 0;
121 * fill in param struct from the given params
123 (void) __gettimeofday (&now, (struct timezone *) 0);
124 aup.aup_time = now.tv_sec;
125 aup.aup_machname = machname;
126 aup.aup_uid = uid;
127 aup.aup_gid = gid;
128 aup.aup_len = (u_int) len;
129 aup.aup_gids = aup_gids;
132 * Serialize the parameters into origcred
134 xdrmem_create (&xdrs, mymem, MAX_AUTH_BYTES, XDR_ENCODE);
135 if (!xdr_authunix_parms (&xdrs, &aup))
136 abort ();
137 au->au_origcred.oa_length = len = XDR_GETPOS (&xdrs);
138 au->au_origcred.oa_flavor = AUTH_UNIX;
139 au->au_origcred.oa_base = mem_alloc ((u_int) len);
140 if (au->au_origcred.oa_base == NULL)
142 (void) fputs (_("authunix_create: out of memory\n"), stderr);
143 return NULL;
145 memcpy(au->au_origcred.oa_base, mymem, (u_int) len);
148 * set auth handle to reflect new cred.
150 auth->ah_cred = au->au_origcred;
151 marshal_new_auth (auth);
152 return auth;
156 * Returns an auth handle with parameters determined by doing lots of
157 * syscalls.
159 AUTH *
160 authunix_create_default (void)
162 int len;
163 char machname[MAX_MACHINE_NAME + 1];
164 uid_t uid;
165 gid_t gid;
166 int max_nr_groups = __sysconf (_SC_NGROUPS_MAX);
167 gid_t gids[max_nr_groups];
169 if (__gethostname (machname, MAX_MACHINE_NAME) == -1)
170 abort ();
171 machname[MAX_MACHINE_NAME] = 0;
172 uid = __geteuid ();
173 gid = __getegid ();
175 if ((len = __getgroups (max_nr_groups, gids)) < 0)
176 abort ();
177 /* This braindamaged Sun code forces us here to truncate the
178 list of groups to NGRPS members since the code in
179 authuxprot.c transforms a fixed array. Grrr. */
180 return authunix_create (machname, uid, gid, MIN (NGRPS, len), gids);
184 * authunix operations
187 static void
188 authunix_nextverf (AUTH *auth)
190 /* no action necessary */
193 static bool_t
194 authunix_marshal (AUTH *auth, XDR *xdrs)
196 struct audata *au = AUTH_PRIVATE (auth);
198 return XDR_PUTBYTES (xdrs, au->au_marshed, au->au_mpos);
201 static bool_t
202 authunix_validate (AUTH *auth, struct opaque_auth *verf)
204 struct audata *au;
205 XDR xdrs;
207 if (verf->oa_flavor == AUTH_SHORT)
209 au = AUTH_PRIVATE (auth);
210 xdrmem_create (&xdrs, verf->oa_base, verf->oa_length,
211 XDR_DECODE);
213 if (au->au_shcred.oa_base != NULL)
215 mem_free (au->au_shcred.oa_base,
216 au->au_shcred.oa_length);
217 au->au_shcred.oa_base = NULL;
219 if (xdr_opaque_auth (&xdrs, &au->au_shcred))
221 auth->ah_cred = au->au_shcred;
223 else
225 xdrs.x_op = XDR_FREE;
226 (void) xdr_opaque_auth (&xdrs, &au->au_shcred);
227 au->au_shcred.oa_base = NULL;
228 auth->ah_cred = au->au_origcred;
230 marshal_new_auth (auth);
232 return TRUE;
235 static bool_t
236 authunix_refresh (AUTH *auth)
238 struct audata *au = AUTH_PRIVATE (auth);
239 struct authunix_parms aup;
240 struct timeval now;
241 XDR xdrs;
242 int stat;
244 if (auth->ah_cred.oa_base == au->au_origcred.oa_base)
246 /* there is no hope. Punt */
247 return FALSE;
249 au->au_shfaults++;
251 /* first deserialize the creds back into a struct authunix_parms */
252 aup.aup_machname = NULL;
253 aup.aup_gids = (gid_t *) NULL;
254 xdrmem_create (&xdrs, au->au_origcred.oa_base,
255 au->au_origcred.oa_length, XDR_DECODE);
256 stat = xdr_authunix_parms (&xdrs, &aup);
257 if (!stat)
258 goto done;
260 /* update the time and serialize in place */
261 (void) __gettimeofday (&now, (struct timezone *) 0);
262 aup.aup_time = now.tv_sec;
263 xdrs.x_op = XDR_ENCODE;
264 XDR_SETPOS (&xdrs, 0);
265 stat = xdr_authunix_parms (&xdrs, &aup);
266 if (!stat)
267 goto done;
268 auth->ah_cred = au->au_origcred;
269 marshal_new_auth (auth);
270 done:
271 /* free the struct authunix_parms created by deserializing */
272 xdrs.x_op = XDR_FREE;
273 (void) xdr_authunix_parms (&xdrs, &aup);
274 XDR_DESTROY (&xdrs);
275 return stat;
278 static void
279 authunix_destroy (AUTH *auth)
281 struct audata *au = AUTH_PRIVATE (auth);
283 mem_free (au->au_origcred.oa_base, au->au_origcred.oa_length);
285 if (au->au_shcred.oa_base != NULL)
286 mem_free (au->au_shcred.oa_base, au->au_shcred.oa_length);
288 mem_free (auth->ah_private, sizeof (struct audata));
290 if (auth->ah_verf.oa_base != NULL)
291 mem_free (auth->ah_verf.oa_base, auth->ah_verf.oa_length);
293 mem_free ((caddr_t) auth, sizeof (*auth));
297 * Marshals (pre-serializes) an auth struct.
298 * sets private data, au_marshed and au_mpos
300 static bool_t
301 internal_function
302 marshal_new_auth (AUTH *auth)
304 XDR xdr_stream;
305 XDR *xdrs = &xdr_stream;
306 struct audata *au = AUTH_PRIVATE (auth);
308 xdrmem_create (xdrs, au->au_marshed, MAX_AUTH_BYTES, XDR_ENCODE);
309 if ((!xdr_opaque_auth (xdrs, &(auth->ah_cred))) ||
310 (!xdr_opaque_auth (xdrs, &(auth->ah_verf))))
311 perror (_("auth_none.c - Fatal marshalling problem"));
312 else
313 au->au_mpos = XDR_GETPOS (xdrs);
315 XDR_DESTROY (xdrs);
317 return TRUE;