2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.1 (the "License"). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
14 * The Original Code and all software distributed under the License are
15 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON- INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
22 * @APPLE_LICENSE_HEADER_END@
25 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
26 * unrestricted use provided that this legend is included on all tape
27 * media and as a part of the software program in whole or part. Users
28 * may copy or modify Sun RPC without charge, but are not authorized
29 * to license or distribute it to anyone else except as part of a product or
30 * program developed by the user.
32 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
33 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
34 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
36 * Sun RPC is provided with no support and without any obligation on the
37 * part of Sun Microsystems, Inc. to assist in its use, correction,
38 * modification or enhancement.
40 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
41 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
42 * OR ANY PART THEREOF.
44 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
45 * or profits or other special, indirect and consequential damages, even if
46 * Sun has been advised of the possibility of such damages.
48 * Sun Microsystems, Inc.
50 * Mountain View, California 94043
53 #if defined(LIBC_SCCS) && !defined(lint)
54 /*static char *sccsid = "from: @(#)auth_unix.c 1.19 87/08/11 Copyr 1984 Sun Micro";*/
55 /*static char *sccsid = "from: @(#)auth_unix.c 2.2 88/08/01 4.0 RPCSRC";*/
56 static char *rcsid
= "$Id: auth_unix.c,v 1.4 2002/02/19 20:36:22 epeyton Exp $";
60 * auth_unix.c, Implements UNIX style authentication parameters.
62 * Copyright (C) 1984, Sun Microsystems, Inc.
64 * The system is very weak. The client uses no encryption for it's
65 * credentials and only sends null verifiers. The server sends backs
66 * null verifiers or optionally a verifier that suggests a new short hand
67 * for the credentials.
75 #include <sys/param.h>
76 #include <sys/types.h>
77 #include <netinet/in.h>
79 #include <rpc/types.h>
82 #include <rpc/auth_unix.h>
84 extern bool_t
xdr_opaque_auth();
87 * Unix authenticator operations vector
89 static void authunix_nextverf();
90 static bool_t
authunix_marshal();
91 static bool_t
authunix_validate();
92 static bool_t
authunix_refresh();
93 static void authunix_destroy();
95 static struct auth_ops auth_unix_ops
= {
104 * This struct is pointed to by the ah_private field of an auth_handle.
107 struct opaque_auth au_origcred
; /* original credentials */
108 struct opaque_auth au_shcred
; /* short hand cred */
110 uint32_t au_shfaults
; /* short hand cache faults */
112 u_long au_shfaults
; /* short hand cache faults */
114 char au_marshed
[MAX_AUTH_BYTES
];
115 u_int au_mpos
; /* xdr pos at end of marshed */
117 #define AUTH_PRIVATE(auth) ((struct audata *)auth->ah_private)
119 static void marshal_new_auth();
123 * Create a unix style authenticator.
124 * Returns an auth handle with the given stuff in it.
127 authunix_create(machname
, uid
, gid
, len
, aup_gids
)
134 struct authunix_parms aup
;
135 char mymem
[MAX_AUTH_BYTES
];
139 register struct audata
*au
;
142 * Allocate and set up auth handle
144 auth
= (AUTH
*)mem_alloc(sizeof(*auth
));
147 (void)fprintf(stderr
, "authunix_create: out of memory\n");
151 au
= (struct audata
*)mem_alloc(sizeof(*au
));
154 mem_free(auth
, sizeof(*auth
));
155 (void)fprintf(stderr
, "authunix_create: out of memory\n");
159 auth
->ah_ops
= &auth_unix_ops
;
160 auth
->ah_private
= (caddr_t
)au
;
161 auth
->ah_verf
= au
->au_shcred
= _null_auth
;
165 * fill in param struct from the given params
167 (void)gettimeofday(&now
, (struct timezone
*)0);
168 aup
.aup_time
= now
.tv_sec
;
169 aup
.aup_machname
= machname
;
172 aup
.aup_len
= (u_int
)len
;
173 aup
.aup_gids
= aup_gids
;
176 * Serialize the parameters into origcred
178 xdrmem_create(&xdrs
, mymem
, MAX_AUTH_BYTES
, XDR_ENCODE
);
179 if (! xdr_authunix_parms(&xdrs
, &aup
))
181 au
->au_origcred
.oa_length
= len
= XDR_GETPOS(&xdrs
);
182 au
->au_origcred
.oa_flavor
= AUTH_UNIX
;
184 au
->au_origcred
.oa_base
= mem_alloc((u_int
) len
);
186 if ((au
->au_origcred
.oa_base
= mem_alloc((u_int
) len
)) == NULL
) {
187 mem_free(auth
, sizeof(*auth
));
188 mem_free(au
, sizeof(*au
));
189 (void)fprintf(stderr
, "authunix_create: out of memory\n");
193 bcopy(mymem
, au
->au_origcred
.oa_base
, (u_int
)len
);
196 * set auth handle to reflect new cred.
198 auth
->ah_cred
= au
->au_origcred
;
199 marshal_new_auth(auth
);
204 * Some servers will refuse mounts if the group list is larger
205 * than it expects (like 8). This allows the application to set
206 * the maximum size of the group list that will be sent.
209 static int maxgrplist
= NGROUPS
;
212 set_rpc_maxgrouplist(num
)
220 * Returns an auth handle with parameters determined by doing lots of
224 authunix_create_default()
227 char machname
[MAX_MACHINE_NAME
+ 1];
232 if (gethostname(machname
, MAX_MACHINE_NAME
) == -1)
234 machname
[MAX_MACHINE_NAME
] = 0;
237 if ((len
= getgroups(NGROUPS
, gids
)) < 0)
239 if (len
> maxgrplist
) {
242 return (authunix_create(machname
, uid
, gid
, len
, (int *)gids
));
246 * authunix operations
250 authunix_nextverf(auth
)
253 /* no action necessary */
257 authunix_marshal(auth
, xdrs
)
261 register struct audata
*au
= AUTH_PRIVATE(auth
);
263 return (XDR_PUTBYTES(xdrs
, au
->au_marshed
, au
->au_mpos
));
267 authunix_validate(auth
, verf
)
269 struct opaque_auth verf
;
271 register struct audata
*au
;
274 if (verf
.oa_flavor
== AUTH_SHORT
) {
275 au
= AUTH_PRIVATE(auth
);
276 xdrmem_create(&xdrs
, verf
.oa_base
, verf
.oa_length
, XDR_DECODE
);
278 if (au
->au_shcred
.oa_base
!= NULL
) {
279 mem_free(au
->au_shcred
.oa_base
,
280 au
->au_shcred
.oa_length
);
281 au
->au_shcred
.oa_base
= NULL
;
283 if (xdr_opaque_auth(&xdrs
, &au
->au_shcred
)) {
284 auth
->ah_cred
= au
->au_shcred
;
286 xdrs
.x_op
= XDR_FREE
;
287 (void)xdr_opaque_auth(&xdrs
, &au
->au_shcred
);
288 au
->au_shcred
.oa_base
= NULL
;
289 auth
->ah_cred
= au
->au_origcred
;
291 marshal_new_auth(auth
);
297 authunix_refresh(auth
)
300 register struct audata
*au
= AUTH_PRIVATE(auth
);
301 struct authunix_parms aup
;
306 if (auth
->ah_cred
.oa_base
== au
->au_origcred
.oa_base
) {
307 /* there is no hope. Punt */
312 /* first deserialize the creds back into a struct authunix_parms */
313 aup
.aup_machname
= NULL
;
314 aup
.aup_gids
= (int *)NULL
;
315 xdrmem_create(&xdrs
, au
->au_origcred
.oa_base
,
316 au
->au_origcred
.oa_length
, XDR_DECODE
);
317 stat
= xdr_authunix_parms(&xdrs
, &aup
);
321 /* update the time and serialize in place */
322 (void)gettimeofday(&now
, (struct timezone
*)0);
323 aup
.aup_time
= now
.tv_sec
;
324 xdrs
.x_op
= XDR_ENCODE
;
325 XDR_SETPOS(&xdrs
, 0);
326 stat
= xdr_authunix_parms(&xdrs
, &aup
);
329 auth
->ah_cred
= au
->au_origcred
;
330 marshal_new_auth(auth
);
332 /* free the struct authunix_parms created by deserializing */
333 xdrs
.x_op
= XDR_FREE
;
334 (void)xdr_authunix_parms(&xdrs
, &aup
);
340 authunix_destroy(auth
)
343 register struct audata
*au
= AUTH_PRIVATE(auth
);
345 mem_free(au
->au_origcred
.oa_base
, au
->au_origcred
.oa_length
);
347 if (au
->au_shcred
.oa_base
!= NULL
)
348 mem_free(au
->au_shcred
.oa_base
, au
->au_shcred
.oa_length
);
350 mem_free(auth
->ah_private
, sizeof(struct audata
));
352 if (auth
->ah_verf
.oa_base
!= NULL
)
353 mem_free(auth
->ah_verf
.oa_base
, auth
->ah_verf
.oa_length
);
355 mem_free((caddr_t
)auth
, sizeof(*auth
));
359 * Marshals (pre-serializes) an auth struct.
360 * sets private data, au_marshed and au_mpos
363 marshal_new_auth(auth
)
367 register XDR
*xdrs
= &xdr_stream
;
368 register struct audata
*au
= AUTH_PRIVATE(auth
);
370 xdrmem_create(xdrs
, au
->au_marshed
, MAX_AUTH_BYTES
, XDR_ENCODE
);
371 if ((! xdr_opaque_auth(xdrs
, &(auth
->ah_cred
))) ||
372 (! xdr_opaque_auth(xdrs
, &(auth
->ah_verf
)))) {
373 perror("auth_none.c - Fatal marshalling problem");
375 au
->au_mpos
= XDR_GETPOS(xdrs
);