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.
27 * Mountain View, California 94043
29 * @(#)auth_unix.c 1.19 87/08/11 Copyr 1984 Sun Micro
30 * @(#)auth_unix.c 2.2 88/08/01 4.0 RPCSRC
31 * $FreeBSD: src/lib/libc/rpc/auth_unix.c,v 1.12 1999/12/29 05:04:16 peter Exp $
32 * $DragonFly: src/lib/libc/rpc/auth_unix.c,v 1.3 2004/10/25 19:38:01 drhodus Exp $
36 * auth_unix.c, Implements UNIX style authentication parameters.
38 * Copyright (C) 1984, Sun Microsystems, Inc.
40 * The system is very weak. The client uses no encryption for it's
41 * credentials and only sends null verifiers. The server sends backs
42 * null verifiers or optionally a verifier that suggests a new short hand
43 * for the credentials.
52 #include <sys/param.h>
53 #include <rpc/types.h>
56 #include <rpc/auth_unix.h>
59 * Unix authenticator operations vector
61 static void authunix_nextverf();
62 static bool_t authunix_marshal();
63 static bool_t authunix_validate();
64 static bool_t authunix_refresh();
65 static void authunix_destroy();
67 static struct auth_ops auth_unix_ops = {
76 * This struct is pointed to by the ah_private field of an auth_handle.
79 struct opaque_auth au_origcred; /* original credentials */
80 struct opaque_auth au_shcred; /* short hand cred */
81 u_long au_shfaults; /* short hand cache faults */
82 char au_marshed[MAX_AUTH_BYTES];
83 u_int au_mpos; /* xdr pos at end of marshed */
85 #define AUTH_PRIVATE(auth) ((struct audata *)auth->ah_private)
87 static void marshal_new_auth();
90 * This goop is here because some servers refuse to accept a
91 * credential with more than some number (usually 8) supplementary
94 static int authunix_maxgrouplist = 0;
97 set_rpc_maxgrouplist(int num)
99 authunix_maxgrouplist = num;
103 * Create a unix style authenticator.
104 * Returns an auth handle with the given stuff in it.
107 authunix_create(machname, uid, gid, len, aup_gids)
114 struct authunix_parms aup;
115 char mymem[MAX_AUTH_BYTES];
122 * Allocate and set up auth handle
124 auth = (AUTH *)mem_alloc(sizeof(*auth));
127 (void)fprintf(stderr, "authunix_create: out of memory\n");
131 au = (struct audata *)mem_alloc(sizeof(*au));
134 (void)fprintf(stderr, "authunix_create: out of memory\n");
138 auth->ah_ops = &auth_unix_ops;
139 auth->ah_private = (caddr_t)au;
140 auth->ah_verf = au->au_shcred = _null_auth;
144 * fill in param struct from the given params
146 (void)gettimeofday(&now, (struct timezone *)0);
147 aup.aup_time = now.tv_sec;
148 aup.aup_machname = machname;
151 /* GW: continuation of max group list hack */
152 if(authunix_maxgrouplist != 0) {
153 aup.aup_len = ((len < authunix_maxgrouplist) ? len
154 : authunix_maxgrouplist);
156 aup.aup_len = (u_int)len;
158 aup.aup_gids = aup_gids;
161 * Serialize the parameters into origcred
163 xdrmem_create(&xdrs, mymem, MAX_AUTH_BYTES, XDR_ENCODE);
164 if (! xdr_authunix_parms(&xdrs, &aup))
166 au->au_origcred.oa_length = len = XDR_GETPOS(&xdrs);
167 au->au_origcred.oa_flavor = AUTH_UNIX;
169 au->au_origcred.oa_base = mem_alloc((u_int) len);
171 if ((au->au_origcred.oa_base = mem_alloc((u_int) len)) == NULL) {
172 (void)fprintf(stderr, "authunix_create: out of memory\n");
176 memcpy(au->au_origcred.oa_base, mymem, (u_int)len);
179 * set auth handle to reflect new cred.
181 auth->ah_cred = au->au_origcred;
182 marshal_new_auth(auth);
187 * Returns an auth handle with parameters determined by doing lots of
191 authunix_create_default()
194 char machname[MAX_MACHINE_NAME + 1];
199 gid_t real_gids[NGROUPS];
201 if (gethostname(machname, MAX_MACHINE_NAME) == -1)
203 machname[MAX_MACHINE_NAME] = 0;
204 uid = (int)geteuid();
205 gid = (int)getegid();
206 if ((len = getgroups(NGROUPS, real_gids)) < 0)
208 if(len > NGRPS) len = NGRPS; /* GW: turn `gid_t's into `int's */
209 for(i = 0; i < len; i++) {
210 gids[i] = (int)real_gids[i];
212 return (authunix_create(machname, uid, gid, len, gids));
216 * authunix operations
220 authunix_nextverf(auth)
223 /* no action necessary */
227 authunix_marshal(auth, xdrs)
231 struct audata *au = AUTH_PRIVATE(auth);
233 return (XDR_PUTBYTES(xdrs, au->au_marshed, au->au_mpos));
237 authunix_validate(auth, verf)
239 struct opaque_auth verf;
244 if (verf.oa_flavor == AUTH_SHORT) {
245 au = AUTH_PRIVATE(auth);
246 xdrmem_create(&xdrs, verf.oa_base, verf.oa_length, XDR_DECODE);
248 if (au->au_shcred.oa_base != NULL) {
249 mem_free(au->au_shcred.oa_base,
250 au->au_shcred.oa_length);
251 au->au_shcred.oa_base = NULL;
253 if (xdr_opaque_auth(&xdrs, &au->au_shcred)) {
254 auth->ah_cred = au->au_shcred;
256 xdrs.x_op = XDR_FREE;
257 (void)xdr_opaque_auth(&xdrs, &au->au_shcred);
258 au->au_shcred.oa_base = NULL;
259 auth->ah_cred = au->au_origcred;
261 marshal_new_auth(auth);
267 authunix_refresh(auth)
270 struct audata *au = AUTH_PRIVATE(auth);
271 struct authunix_parms aup;
276 if (auth->ah_cred.oa_base == au->au_origcred.oa_base) {
277 /* there is no hope. Punt */
282 /* first deserialize the creds back into a struct authunix_parms */
283 aup.aup_machname = NULL;
284 aup.aup_gids = (int *)NULL;
285 xdrmem_create(&xdrs, au->au_origcred.oa_base,
286 au->au_origcred.oa_length, XDR_DECODE);
287 stat = xdr_authunix_parms(&xdrs, &aup);
291 /* update the time and serialize in place */
292 (void)gettimeofday(&now, (struct timezone *)0);
293 aup.aup_time = now.tv_sec;
294 xdrs.x_op = XDR_ENCODE;
295 XDR_SETPOS(&xdrs, 0);
296 stat = xdr_authunix_parms(&xdrs, &aup);
299 auth->ah_cred = au->au_origcred;
300 marshal_new_auth(auth);
302 /* free the struct authunix_parms created by deserializing */
303 xdrs.x_op = XDR_FREE;
304 (void)xdr_authunix_parms(&xdrs, &aup);
310 authunix_destroy(auth)
313 struct audata *au = AUTH_PRIVATE(auth);
315 mem_free(au->au_origcred.oa_base, au->au_origcred.oa_length);
317 if (au->au_shcred.oa_base != NULL)
318 mem_free(au->au_shcred.oa_base, au->au_shcred.oa_length);
320 mem_free(auth->ah_private, sizeof(struct audata));
322 if (auth->ah_verf.oa_base != NULL)
323 mem_free(auth->ah_verf.oa_base, auth->ah_verf.oa_length);
325 mem_free((caddr_t)auth, sizeof(*auth));
329 * Marshals (pre-serializes) an auth struct.
330 * sets private data, au_marshed and au_mpos
333 marshal_new_auth(auth)
337 XDR *xdrs = &xdr_stream;
338 struct audata *au = AUTH_PRIVATE(auth);
340 xdrmem_create(xdrs, au->au_marshed, MAX_AUTH_BYTES, XDR_ENCODE);
341 if ((! xdr_opaque_auth(xdrs, &(auth->ah_cred))) ||
342 (! xdr_opaque_auth(xdrs, &(auth->ah_verf)))) {
343 perror("auth_none.c - Fatal marshalling problem");
345 au->au_mpos = XDR_GETPOS(xdrs);