]>
git.saurik.com Git - apple/libinfo.git/blob - rpc.subproj/auth_unix.c
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.3 2001/01/17 19:05:42 majka 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.
73 #include <sys/param.h>
75 #include <rpc/types.h>
78 #include <rpc/auth_unix.h>
81 * Unix authenticator operations vector
83 static void authunix_nextverf();
84 static bool_t
authunix_marshal();
85 static bool_t
authunix_validate();
86 static bool_t
authunix_refresh();
87 static void authunix_destroy();
89 static struct auth_ops auth_unix_ops
= {
98 * This struct is pointed to by the ah_private field of an auth_handle.
101 struct opaque_auth au_origcred
; /* original credentials */
102 struct opaque_auth au_shcred
; /* short hand cred */
103 u_long au_shfaults
; /* short hand cache faults */
104 char au_marshed
[MAX_AUTH_BYTES
];
105 u_int au_mpos
; /* xdr pos at end of marshed */
107 #define AUTH_PRIVATE(auth) ((struct audata *)auth->ah_private)
109 static bool_t
marshal_new_auth();
113 * Create a unix style authenticator.
114 * Returns an auth handle with the given stuff in it.
117 authunix_create(machname
, uid
, gid
, len
, aup_gids
)
124 struct authunix_parms aup
;
125 char mymem
[MAX_AUTH_BYTES
];
129 register struct audata
*au
;
132 * Allocate and set up auth handle
134 auth
= (AUTH
*)mem_alloc(sizeof(*auth
));
137 (void)fprintf(stderr
, "authunix_create: out of memory\n");
141 au
= (struct audata
*)mem_alloc(sizeof(*au
));
144 (void)fprintf(stderr
, "authunix_create: out of memory\n");
148 auth
->ah_ops
= &auth_unix_ops
;
149 auth
->ah_private
= (caddr_t
)au
;
150 auth
->ah_verf
= au
->au_shcred
= _null_auth
;
154 * fill in param struct from the given params
156 (void)gettimeofday(&now
, (struct timezone
*)0);
157 aup
.aup_time
= now
.tv_sec
;
158 aup
.aup_machname
= machname
;
161 aup
.aup_len
= (u_int
)len
;
162 aup
.aup_gids
= aup_gids
;
165 * Serialize the parameters into origcred
167 xdrmem_create(&xdrs
, mymem
, MAX_AUTH_BYTES
, XDR_ENCODE
);
168 if (! xdr_authunix_parms(&xdrs
, &aup
))
170 au
->au_origcred
.oa_length
= len
= XDR_GETPOS(&xdrs
);
171 au
->au_origcred
.oa_flavor
= AUTH_UNIX
;
173 au
->au_origcred
.oa_base
= mem_alloc((u_int
) len
);
175 if ((au
->au_origcred
.oa_base
= mem_alloc((u_int
) len
)) == NULL
) {
176 (void)fprintf(stderr
, "authunix_create: out of memory\n");
180 bcopy(mymem
, au
->au_origcred
.oa_base
, (u_int
)len
);
183 * set auth handle to reflect new cred.
185 auth
->ah_cred
= au
->au_origcred
;
186 marshal_new_auth(auth
);
191 * Some servers will refuse mounts if the group list is larger
192 * than it expects (like 8). This allows the application to set
193 * the maximum size of the group list that will be sent.
196 static maxgrplist
= NGROUPS
;
198 set_rpc_maxgrouplist(num
)
206 * Returns an auth handle with parameters determined by doing lots of
210 authunix_create_default()
213 char machname
[MAX_MACHINE_NAME
+ 1];
218 if (gethostname(machname
, MAX_MACHINE_NAME
) == -1)
220 machname
[MAX_MACHINE_NAME
] = 0;
223 if ((len
= getgroups(NGROUPS
, gids
)) < 0)
225 if (len
> maxgrplist
) {
228 return (authunix_create(machname
, uid
, gid
, len
, gids
));
232 * authunix operations
236 authunix_nextverf(auth
)
239 /* no action necessary */
243 authunix_marshal(auth
, xdrs
)
247 register struct audata
*au
= AUTH_PRIVATE(auth
);
249 return (XDR_PUTBYTES(xdrs
, au
->au_marshed
, au
->au_mpos
));
253 authunix_validate(auth
, verf
)
255 struct opaque_auth verf
;
257 register struct audata
*au
;
260 if (verf
.oa_flavor
== AUTH_SHORT
) {
261 au
= AUTH_PRIVATE(auth
);
262 xdrmem_create(&xdrs
, verf
.oa_base
, verf
.oa_length
, XDR_DECODE
);
264 if (au
->au_shcred
.oa_base
!= NULL
) {
265 mem_free(au
->au_shcred
.oa_base
,
266 au
->au_shcred
.oa_length
);
267 au
->au_shcred
.oa_base
= NULL
;
269 if (xdr_opaque_auth(&xdrs
, &au
->au_shcred
)) {
270 auth
->ah_cred
= au
->au_shcred
;
272 xdrs
.x_op
= XDR_FREE
;
273 (void)xdr_opaque_auth(&xdrs
, &au
->au_shcred
);
274 au
->au_shcred
.oa_base
= NULL
;
275 auth
->ah_cred
= au
->au_origcred
;
277 marshal_new_auth(auth
);
283 authunix_refresh(auth
)
286 register struct audata
*au
= AUTH_PRIVATE(auth
);
287 struct authunix_parms aup
;
292 if (auth
->ah_cred
.oa_base
== au
->au_origcred
.oa_base
) {
293 /* there is no hope. Punt */
298 /* first deserialize the creds back into a struct authunix_parms */
299 aup
.aup_machname
= NULL
;
300 aup
.aup_gids
= (int *)NULL
;
301 xdrmem_create(&xdrs
, au
->au_origcred
.oa_base
,
302 au
->au_origcred
.oa_length
, XDR_DECODE
);
303 stat
= xdr_authunix_parms(&xdrs
, &aup
);
307 /* update the time and serialize in place */
308 (void)gettimeofday(&now
, (struct timezone
*)0);
309 aup
.aup_time
= now
.tv_sec
;
310 xdrs
.x_op
= XDR_ENCODE
;
311 XDR_SETPOS(&xdrs
, 0);
312 stat
= xdr_authunix_parms(&xdrs
, &aup
);
315 auth
->ah_cred
= au
->au_origcred
;
316 marshal_new_auth(auth
);
318 /* free the struct authunix_parms created by deserializing */
319 xdrs
.x_op
= XDR_FREE
;
320 (void)xdr_authunix_parms(&xdrs
, &aup
);
326 authunix_destroy(auth
)
329 register struct audata
*au
= AUTH_PRIVATE(auth
);
331 mem_free(au
->au_origcred
.oa_base
, au
->au_origcred
.oa_length
);
333 if (au
->au_shcred
.oa_base
!= NULL
)
334 mem_free(au
->au_shcred
.oa_base
, au
->au_shcred
.oa_length
);
336 mem_free(auth
->ah_private
, sizeof(struct audata
));
338 if (auth
->ah_verf
.oa_base
!= NULL
)
339 mem_free(auth
->ah_verf
.oa_base
, auth
->ah_verf
.oa_length
);
341 mem_free((caddr_t
)auth
, sizeof(*auth
));
345 * Marshals (pre-serializes) an auth struct.
346 * sets private data, au_marshed and au_mpos
349 marshal_new_auth(auth
)
353 register XDR
*xdrs
= &xdr_stream
;
354 register struct audata
*au
= AUTH_PRIVATE(auth
);
356 xdrmem_create(xdrs
, au
->au_marshed
, MAX_AUTH_BYTES
, XDR_ENCODE
);
357 if ((! xdr_opaque_auth(xdrs
, &(auth
->ah_cred
))) ||
358 (! xdr_opaque_auth(xdrs
, &(auth
->ah_verf
)))) {
359 perror("auth_none.c - Fatal marshalling problem");
361 au
->au_mpos
= XDR_GETPOS(xdrs
);