2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
26 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
27 * unrestricted use provided that this legend is included on all tape
28 * media and as a part of the software program in whole or part. Users
29 * may copy or modify Sun RPC without charge, but are not authorized
30 * to license or distribute it to anyone else except as part of a product or
31 * program developed by the user.
33 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
34 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
35 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
37 * Sun RPC is provided with no support and without any obligation on the
38 * part of Sun Microsystems, Inc. to assist in its use, correction,
39 * modification or enhancement.
41 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
42 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
43 * OR ANY PART THEREOF.
45 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
46 * or profits or other special, indirect and consequential damages, even if
47 * Sun has been advised of the possibility of such damages.
49 * Sun Microsystems, Inc.
51 * Mountain View, California 94043
54 #if defined(LIBC_SCCS) && !defined(lint)
55 /*static char *sccsid = "from: @(#)auth_unix.c 1.19 87/08/11 Copyr 1984 Sun Micro";*/
56 /*static char *sccsid = "from: @(#)auth_unix.c 2.2 88/08/01 4.0 RPCSRC";*/
57 static char *rcsid
= "$Id: auth_unix.c,v 1.4 2002/02/19 20:36:22 epeyton Exp $";
61 * auth_unix.c, Implements UNIX style authentication parameters.
63 * Copyright (C) 1984, Sun Microsystems, Inc.
65 * The system is very weak. The client uses no encryption for it's
66 * credentials and only sends null verifiers. The server sends backs
67 * null verifiers or optionally a verifier that suggests a new short hand
68 * for the credentials.
76 #include <sys/param.h>
77 #include <sys/types.h>
78 #include <netinet/in.h>
80 #include <rpc/types.h>
83 #include <rpc/auth_unix.h>
85 extern bool_t
xdr_opaque_auth();
88 * Unix authenticator operations vector
90 static void authunix_nextverf();
91 static bool_t
authunix_marshal();
92 static bool_t
authunix_validate();
93 static bool_t
authunix_refresh();
94 static void authunix_destroy();
96 static struct auth_ops auth_unix_ops
= {
105 * This struct is pointed to by the ah_private field of an auth_handle.
108 struct opaque_auth au_origcred
; /* original credentials */
109 struct opaque_auth au_shcred
; /* short hand cred */
110 u_long au_shfaults
; /* short hand cache faults */
111 char au_marshed
[MAX_AUTH_BYTES
];
112 u_int au_mpos
; /* xdr pos at end of marshed */
114 #define AUTH_PRIVATE(auth) ((struct audata *)auth->ah_private)
116 static void marshal_new_auth();
120 * Create a unix style authenticator.
121 * Returns an auth handle with the given stuff in it.
124 authunix_create(machname
, uid
, gid
, len
, aup_gids
)
131 struct authunix_parms aup
;
132 char mymem
[MAX_AUTH_BYTES
];
136 register struct audata
*au
;
139 * Allocate and set up auth handle
141 auth
= (AUTH
*)mem_alloc(sizeof(*auth
));
144 (void)fprintf(stderr
, "authunix_create: out of memory\n");
148 au
= (struct audata
*)mem_alloc(sizeof(*au
));
151 (void)fprintf(stderr
, "authunix_create: out of memory\n");
155 auth
->ah_ops
= &auth_unix_ops
;
156 auth
->ah_private
= (caddr_t
)au
;
157 auth
->ah_verf
= au
->au_shcred
= _null_auth
;
161 * fill in param struct from the given params
163 (void)gettimeofday(&now
, (struct timezone
*)0);
164 aup
.aup_time
= now
.tv_sec
;
165 aup
.aup_machname
= machname
;
168 aup
.aup_len
= (u_int
)len
;
169 aup
.aup_gids
= aup_gids
;
172 * Serialize the parameters into origcred
174 xdrmem_create(&xdrs
, mymem
, MAX_AUTH_BYTES
, XDR_ENCODE
);
175 if (! xdr_authunix_parms(&xdrs
, &aup
))
177 au
->au_origcred
.oa_length
= len
= XDR_GETPOS(&xdrs
);
178 au
->au_origcred
.oa_flavor
= AUTH_UNIX
;
180 au
->au_origcred
.oa_base
= mem_alloc((u_int
) len
);
182 if ((au
->au_origcred
.oa_base
= mem_alloc((u_int
) len
)) == NULL
) {
183 (void)fprintf(stderr
, "authunix_create: out of memory\n");
187 bcopy(mymem
, au
->au_origcred
.oa_base
, (u_int
)len
);
190 * set auth handle to reflect new cred.
192 auth
->ah_cred
= au
->au_origcred
;
193 marshal_new_auth(auth
);
198 * Some servers will refuse mounts if the group list is larger
199 * than it expects (like 8). This allows the application to set
200 * the maximum size of the group list that will be sent.
203 static int maxgrplist
= NGROUPS
;
206 set_rpc_maxgrouplist(num
)
214 * Returns an auth handle with parameters determined by doing lots of
218 authunix_create_default()
221 char machname
[MAX_MACHINE_NAME
+ 1];
226 if (gethostname(machname
, MAX_MACHINE_NAME
) == -1)
228 machname
[MAX_MACHINE_NAME
] = 0;
231 if ((len
= getgroups(NGROUPS
, gids
)) < 0)
233 if (len
> maxgrplist
) {
236 return (authunix_create(machname
, uid
, gid
, len
, gids
));
240 * authunix operations
244 authunix_nextverf(auth
)
247 /* no action necessary */
251 authunix_marshal(auth
, xdrs
)
255 register struct audata
*au
= AUTH_PRIVATE(auth
);
257 return (XDR_PUTBYTES(xdrs
, au
->au_marshed
, au
->au_mpos
));
261 authunix_validate(auth
, verf
)
263 struct opaque_auth verf
;
265 register struct audata
*au
;
268 if (verf
.oa_flavor
== AUTH_SHORT
) {
269 au
= AUTH_PRIVATE(auth
);
270 xdrmem_create(&xdrs
, verf
.oa_base
, verf
.oa_length
, XDR_DECODE
);
272 if (au
->au_shcred
.oa_base
!= NULL
) {
273 mem_free(au
->au_shcred
.oa_base
,
274 au
->au_shcred
.oa_length
);
275 au
->au_shcred
.oa_base
= NULL
;
277 if (xdr_opaque_auth(&xdrs
, &au
->au_shcred
)) {
278 auth
->ah_cred
= au
->au_shcred
;
280 xdrs
.x_op
= XDR_FREE
;
281 (void)xdr_opaque_auth(&xdrs
, &au
->au_shcred
);
282 au
->au_shcred
.oa_base
= NULL
;
283 auth
->ah_cred
= au
->au_origcred
;
285 marshal_new_auth(auth
);
291 authunix_refresh(auth
)
294 register struct audata
*au
= AUTH_PRIVATE(auth
);
295 struct authunix_parms aup
;
300 if (auth
->ah_cred
.oa_base
== au
->au_origcred
.oa_base
) {
301 /* there is no hope. Punt */
306 /* first deserialize the creds back into a struct authunix_parms */
307 aup
.aup_machname
= NULL
;
308 aup
.aup_gids
= (int *)NULL
;
309 xdrmem_create(&xdrs
, au
->au_origcred
.oa_base
,
310 au
->au_origcred
.oa_length
, XDR_DECODE
);
311 stat
= xdr_authunix_parms(&xdrs
, &aup
);
315 /* update the time and serialize in place */
316 (void)gettimeofday(&now
, (struct timezone
*)0);
317 aup
.aup_time
= now
.tv_sec
;
318 xdrs
.x_op
= XDR_ENCODE
;
319 XDR_SETPOS(&xdrs
, 0);
320 stat
= xdr_authunix_parms(&xdrs
, &aup
);
323 auth
->ah_cred
= au
->au_origcred
;
324 marshal_new_auth(auth
);
326 /* free the struct authunix_parms created by deserializing */
327 xdrs
.x_op
= XDR_FREE
;
328 (void)xdr_authunix_parms(&xdrs
, &aup
);
334 authunix_destroy(auth
)
337 register struct audata
*au
= AUTH_PRIVATE(auth
);
339 mem_free(au
->au_origcred
.oa_base
, au
->au_origcred
.oa_length
);
341 if (au
->au_shcred
.oa_base
!= NULL
)
342 mem_free(au
->au_shcred
.oa_base
, au
->au_shcred
.oa_length
);
344 mem_free(auth
->ah_private
, sizeof(struct audata
));
346 if (auth
->ah_verf
.oa_base
!= NULL
)
347 mem_free(auth
->ah_verf
.oa_base
, auth
->ah_verf
.oa_length
);
349 mem_free((caddr_t
)auth
, sizeof(*auth
));
353 * Marshals (pre-serializes) an auth struct.
354 * sets private data, au_marshed and au_mpos
357 marshal_new_auth(auth
)
361 register XDR
*xdrs
= &xdr_stream
;
362 register struct audata
*au
= AUTH_PRIVATE(auth
);
364 xdrmem_create(xdrs
, au
->au_marshed
, MAX_AUTH_BYTES
, XDR_ENCODE
);
365 if ((! xdr_opaque_auth(xdrs
, &(auth
->ah_cred
))) ||
366 (! xdr_opaque_auth(xdrs
, &(auth
->ah_verf
)))) {
367 perror("auth_none.c - Fatal marshalling problem");
369 au
->au_mpos
= XDR_GETPOS(xdrs
);