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.4 2005/11/13 12:27:04 swildner 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(char *machname
, int uid
, int gid
, int len
, int *aup_gids
)
109 struct authunix_parms aup
;
110 char mymem
[MAX_AUTH_BYTES
];
117 * Allocate and set up auth handle
119 auth
= (AUTH
*)mem_alloc(sizeof(*auth
));
122 fprintf(stderr
, "authunix_create: out of memory\n");
126 au
= (struct audata
*)mem_alloc(sizeof(*au
));
129 fprintf(stderr
, "authunix_create: out of memory\n");
133 auth
->ah_ops
= &auth_unix_ops
;
134 auth
->ah_private
= (caddr_t
)au
;
135 auth
->ah_verf
= au
->au_shcred
= _null_auth
;
139 * fill in param struct from the given params
141 gettimeofday(&now
, (struct timezone
*)0);
142 aup
.aup_time
= now
.tv_sec
;
143 aup
.aup_machname
= machname
;
146 /* GW: continuation of max group list hack */
147 if(authunix_maxgrouplist
!= 0) {
148 aup
.aup_len
= ((len
< authunix_maxgrouplist
) ? len
149 : authunix_maxgrouplist
);
151 aup
.aup_len
= (u_int
)len
;
153 aup
.aup_gids
= aup_gids
;
156 * Serialize the parameters into origcred
158 xdrmem_create(&xdrs
, mymem
, MAX_AUTH_BYTES
, XDR_ENCODE
);
159 if (! xdr_authunix_parms(&xdrs
, &aup
))
161 au
->au_origcred
.oa_length
= len
= XDR_GETPOS(&xdrs
);
162 au
->au_origcred
.oa_flavor
= AUTH_UNIX
;
164 au
->au_origcred
.oa_base
= mem_alloc((u_int
) len
);
166 if ((au
->au_origcred
.oa_base
= mem_alloc((u_int
) len
)) == NULL
) {
167 fprintf(stderr
, "authunix_create: out of memory\n");
171 memcpy(au
->au_origcred
.oa_base
, mymem
, (u_int
)len
);
174 * set auth handle to reflect new cred.
176 auth
->ah_cred
= au
->au_origcred
;
177 marshal_new_auth(auth
);
182 * Returns an auth handle with parameters determined by doing lots of
186 authunix_create_default(void)
189 char machname
[MAX_MACHINE_NAME
+ 1];
194 gid_t real_gids
[NGROUPS
];
196 if (gethostname(machname
, MAX_MACHINE_NAME
) == -1)
198 machname
[MAX_MACHINE_NAME
] = 0;
199 uid
= (int)geteuid();
200 gid
= (int)getegid();
201 if ((len
= getgroups(NGROUPS
, real_gids
)) < 0)
203 if(len
> NGRPS
) len
= NGRPS
; /* GW: turn `gid_t's into `int's */
204 for(i
= 0; i
< len
; i
++) {
205 gids
[i
] = (int)real_gids
[i
];
207 return (authunix_create(machname
, uid
, gid
, len
, gids
));
211 * authunix operations
215 authunix_nextverf(AUTH
*auth
)
217 /* no action necessary */
221 authunix_marshal(AUTH
*auth
, XDR
*xdrs
)
223 struct audata
*au
= AUTH_PRIVATE(auth
);
225 return (XDR_PUTBYTES(xdrs
, au
->au_marshed
, au
->au_mpos
));
229 authunix_validate(AUTH
*auth
, struct opaque_auth verf
)
234 if (verf
.oa_flavor
== AUTH_SHORT
) {
235 au
= AUTH_PRIVATE(auth
);
236 xdrmem_create(&xdrs
, verf
.oa_base
, verf
.oa_length
, XDR_DECODE
);
238 if (au
->au_shcred
.oa_base
!= NULL
) {
239 mem_free(au
->au_shcred
.oa_base
,
240 au
->au_shcred
.oa_length
);
241 au
->au_shcred
.oa_base
= NULL
;
243 if (xdr_opaque_auth(&xdrs
, &au
->au_shcred
)) {
244 auth
->ah_cred
= au
->au_shcred
;
246 xdrs
.x_op
= XDR_FREE
;
247 xdr_opaque_auth(&xdrs
, &au
->au_shcred
);
248 au
->au_shcred
.oa_base
= NULL
;
249 auth
->ah_cred
= au
->au_origcred
;
251 marshal_new_auth(auth
);
257 authunix_refresh(AUTH
*auth
)
259 struct audata
*au
= AUTH_PRIVATE(auth
);
260 struct authunix_parms aup
;
265 if (auth
->ah_cred
.oa_base
== au
->au_origcred
.oa_base
) {
266 /* there is no hope. Punt */
271 /* first deserialize the creds back into a struct authunix_parms */
272 aup
.aup_machname
= NULL
;
273 aup
.aup_gids
= (int *)NULL
;
274 xdrmem_create(&xdrs
, au
->au_origcred
.oa_base
,
275 au
->au_origcred
.oa_length
, XDR_DECODE
);
276 stat
= xdr_authunix_parms(&xdrs
, &aup
);
280 /* update the time and serialize in place */
281 gettimeofday(&now
, (struct timezone
*)0);
282 aup
.aup_time
= now
.tv_sec
;
283 xdrs
.x_op
= XDR_ENCODE
;
284 XDR_SETPOS(&xdrs
, 0);
285 stat
= xdr_authunix_parms(&xdrs
, &aup
);
288 auth
->ah_cred
= au
->au_origcred
;
289 marshal_new_auth(auth
);
291 /* free the struct authunix_parms created by deserializing */
292 xdrs
.x_op
= XDR_FREE
;
293 xdr_authunix_parms(&xdrs
, &aup
);
299 authunix_destroy(AUTH
*auth
)
301 struct audata
*au
= AUTH_PRIVATE(auth
);
303 mem_free(au
->au_origcred
.oa_base
, au
->au_origcred
.oa_length
);
305 if (au
->au_shcred
.oa_base
!= NULL
)
306 mem_free(au
->au_shcred
.oa_base
, au
->au_shcred
.oa_length
);
308 mem_free(auth
->ah_private
, sizeof(struct audata
));
310 if (auth
->ah_verf
.oa_base
!= NULL
)
311 mem_free(auth
->ah_verf
.oa_base
, auth
->ah_verf
.oa_length
);
313 mem_free((caddr_t
)auth
, sizeof(*auth
));
317 * Marshals (pre-serializes) an auth struct.
318 * sets private data, au_marshed and au_mpos
321 marshal_new_auth(AUTH
*auth
)
324 XDR
*xdrs
= &xdr_stream
;
325 struct audata
*au
= AUTH_PRIVATE(auth
);
327 xdrmem_create(xdrs
, au
->au_marshed
, MAX_AUTH_BYTES
, XDR_ENCODE
);
328 if ((! xdr_opaque_auth(xdrs
, &(auth
->ah_cred
))) ||
329 (! xdr_opaque_auth(xdrs
, &(auth
->ah_verf
)))) {
330 perror("auth_none.c - Fatal marshalling problem");
332 au
->au_mpos
= XDR_GETPOS(xdrs
);