1*7f2fe78bSCy Schubert /* @(#)auth_unix.c 2.2 88/08/01 4.0 RPCSRC */
2*7f2fe78bSCy Schubert /*
3*7f2fe78bSCy Schubert * Copyright (c) 2010, Oracle America, Inc.
4*7f2fe78bSCy Schubert *
5*7f2fe78bSCy Schubert * All rights reserved.
6*7f2fe78bSCy Schubert *
7*7f2fe78bSCy Schubert * Redistribution and use in source and binary forms, with or without
8*7f2fe78bSCy Schubert * modification, are permitted provided that the following conditions are met:
9*7f2fe78bSCy Schubert *
10*7f2fe78bSCy Schubert * * Redistributions of source code must retain the above copyright
11*7f2fe78bSCy Schubert * notice, this list of conditions and the following disclaimer.
12*7f2fe78bSCy Schubert *
13*7f2fe78bSCy Schubert * * Redistributions in binary form must reproduce the above copyright
14*7f2fe78bSCy Schubert * notice, this list of conditions and the following disclaimer in
15*7f2fe78bSCy Schubert * the documentation and/or other materials provided with the
16*7f2fe78bSCy Schubert * distribution.
17*7f2fe78bSCy Schubert *
18*7f2fe78bSCy Schubert * * Neither the name of the "Oracle America, Inc." nor the names of
19*7f2fe78bSCy Schubert * its contributors may be used to endorse or promote products
20*7f2fe78bSCy Schubert * derived from this software without specific prior written permission.
21*7f2fe78bSCy Schubert *
22*7f2fe78bSCy Schubert * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
23*7f2fe78bSCy Schubert * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24*7f2fe78bSCy Schubert * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
25*7f2fe78bSCy Schubert * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26*7f2fe78bSCy Schubert * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27*7f2fe78bSCy Schubert * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
28*7f2fe78bSCy Schubert * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
29*7f2fe78bSCy Schubert * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
30*7f2fe78bSCy Schubert * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
31*7f2fe78bSCy Schubert * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32*7f2fe78bSCy Schubert * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33*7f2fe78bSCy Schubert */
34*7f2fe78bSCy Schubert #if !defined(lint) && defined(SCCSIDS)
35*7f2fe78bSCy Schubert static char sccsid[] = "@(#)auth_unix.c 1.19 87/08/11 Copyr 1984 Sun Micro";
36*7f2fe78bSCy Schubert #endif
37*7f2fe78bSCy Schubert
38*7f2fe78bSCy Schubert /*
39*7f2fe78bSCy Schubert * auth_unix.c, Implements UNIX style authentication parameters.
40*7f2fe78bSCy Schubert *
41*7f2fe78bSCy Schubert * The system is very weak. The client uses no encryption for its
42*7f2fe78bSCy Schubert * credentials and only sends null verifiers. The server sends backs
43*7f2fe78bSCy Schubert * null verifiers or optionally a verifier that suggests a new short hand
44*7f2fe78bSCy Schubert * for the credentials.
45*7f2fe78bSCy Schubert *
46*7f2fe78bSCy Schubert */
47*7f2fe78bSCy Schubert
48*7f2fe78bSCy Schubert #include "autoconf.h"
49*7f2fe78bSCy Schubert #include <stdio.h>
50*7f2fe78bSCy Schubert #include <unistd.h>
51*7f2fe78bSCy Schubert #include <string.h>
52*7f2fe78bSCy Schubert
53*7f2fe78bSCy Schubert #include <gssrpc/types.h>
54*7f2fe78bSCy Schubert #include <gssrpc/xdr.h>
55*7f2fe78bSCy Schubert #include <gssrpc/auth.h>
56*7f2fe78bSCy Schubert #include <gssrpc/auth_unix.h>
57*7f2fe78bSCy Schubert
58*7f2fe78bSCy Schubert /*
59*7f2fe78bSCy Schubert * Unix authenticator operations vector
60*7f2fe78bSCy Schubert */
61*7f2fe78bSCy Schubert static void authunix_nextverf(AUTH *);
62*7f2fe78bSCy Schubert static bool_t authunix_marshal(AUTH *, XDR *);
63*7f2fe78bSCy Schubert static bool_t authunix_validate(AUTH *, struct opaque_auth *);
64*7f2fe78bSCy Schubert static bool_t authunix_refresh(AUTH *, struct rpc_msg *);
65*7f2fe78bSCy Schubert static void authunix_destroy(AUTH *);
66*7f2fe78bSCy Schubert static bool_t authunix_wrap(AUTH *, XDR *, xdrproc_t, caddr_t);
67*7f2fe78bSCy Schubert
68*7f2fe78bSCy Schubert static struct auth_ops auth_unix_ops = {
69*7f2fe78bSCy Schubert authunix_nextverf,
70*7f2fe78bSCy Schubert authunix_marshal,
71*7f2fe78bSCy Schubert authunix_validate,
72*7f2fe78bSCy Schubert authunix_refresh,
73*7f2fe78bSCy Schubert authunix_destroy,
74*7f2fe78bSCy Schubert authunix_wrap,
75*7f2fe78bSCy Schubert authunix_wrap
76*7f2fe78bSCy Schubert };
77*7f2fe78bSCy Schubert
78*7f2fe78bSCy Schubert /*
79*7f2fe78bSCy Schubert * This struct is pointed to by the ah_private field of an auth_handle.
80*7f2fe78bSCy Schubert */
81*7f2fe78bSCy Schubert struct audata {
82*7f2fe78bSCy Schubert struct opaque_auth au_origcred; /* original credentials */
83*7f2fe78bSCy Schubert struct opaque_auth au_shcred; /* short hand cred */
84*7f2fe78bSCy Schubert uint32_t au_shfaults; /* short hand cache faults */
85*7f2fe78bSCy Schubert char au_marshed[MAX_AUTH_BYTES];
86*7f2fe78bSCy Schubert u_int au_mpos; /* xdr pos at end of marshed */
87*7f2fe78bSCy Schubert };
88*7f2fe78bSCy Schubert #define AUTH_PRIVATE(auth) ((struct audata *)auth->ah_private)
89*7f2fe78bSCy Schubert
90*7f2fe78bSCy Schubert static void marshal_new_auth(AUTH *);
91*7f2fe78bSCy Schubert
92*7f2fe78bSCy Schubert
93*7f2fe78bSCy Schubert /*
94*7f2fe78bSCy Schubert * Create a unix style authenticator.
95*7f2fe78bSCy Schubert * Returns an auth handle with the given stuff in it.
96*7f2fe78bSCy Schubert */
97*7f2fe78bSCy Schubert AUTH *
authunix_create(char * machname,int uid,int gid,int len,int * aup_gids)98*7f2fe78bSCy Schubert authunix_create(
99*7f2fe78bSCy Schubert char *machname,
100*7f2fe78bSCy Schubert int uid,
101*7f2fe78bSCy Schubert int gid,
102*7f2fe78bSCy Schubert int len,
103*7f2fe78bSCy Schubert int *aup_gids)
104*7f2fe78bSCy Schubert {
105*7f2fe78bSCy Schubert struct authunix_parms aup;
106*7f2fe78bSCy Schubert char mymem[MAX_AUTH_BYTES];
107*7f2fe78bSCy Schubert struct timeval now;
108*7f2fe78bSCy Schubert XDR xdrs;
109*7f2fe78bSCy Schubert AUTH *auth;
110*7f2fe78bSCy Schubert struct audata *au;
111*7f2fe78bSCy Schubert
112*7f2fe78bSCy Schubert /*
113*7f2fe78bSCy Schubert * Allocate and set up auth handle
114*7f2fe78bSCy Schubert */
115*7f2fe78bSCy Schubert auth = (AUTH *)mem_alloc(sizeof(*auth));
116*7f2fe78bSCy Schubert #ifndef KERNEL
117*7f2fe78bSCy Schubert if (auth == NULL) {
118*7f2fe78bSCy Schubert (void)fprintf(stderr, "authunix_create: out of memory\n");
119*7f2fe78bSCy Schubert return (NULL);
120*7f2fe78bSCy Schubert }
121*7f2fe78bSCy Schubert #endif
122*7f2fe78bSCy Schubert au = (struct audata *)mem_alloc(sizeof(*au));
123*7f2fe78bSCy Schubert #ifndef KERNEL
124*7f2fe78bSCy Schubert if (au == NULL) {
125*7f2fe78bSCy Schubert (void)fprintf(stderr, "authunix_create: out of memory\n");
126*7f2fe78bSCy Schubert return (NULL);
127*7f2fe78bSCy Schubert }
128*7f2fe78bSCy Schubert #endif
129*7f2fe78bSCy Schubert auth->ah_ops = &auth_unix_ops;
130*7f2fe78bSCy Schubert auth->ah_private = (caddr_t)au;
131*7f2fe78bSCy Schubert auth->ah_verf = au->au_shcred = gssrpc__null_auth;
132*7f2fe78bSCy Schubert au->au_shfaults = 0;
133*7f2fe78bSCy Schubert
134*7f2fe78bSCy Schubert /*
135*7f2fe78bSCy Schubert * fill in param struct from the given params
136*7f2fe78bSCy Schubert */
137*7f2fe78bSCy Schubert (void)gettimeofday(&now, (struct timezone *)0);
138*7f2fe78bSCy Schubert aup.aup_time = now.tv_sec;
139*7f2fe78bSCy Schubert aup.aup_machname = machname;
140*7f2fe78bSCy Schubert aup.aup_uid = uid;
141*7f2fe78bSCy Schubert aup.aup_gid = gid;
142*7f2fe78bSCy Schubert aup.aup_len = (u_int)len;
143*7f2fe78bSCy Schubert aup.aup_gids = aup_gids;
144*7f2fe78bSCy Schubert
145*7f2fe78bSCy Schubert /*
146*7f2fe78bSCy Schubert * Serialize the parameters into origcred
147*7f2fe78bSCy Schubert */
148*7f2fe78bSCy Schubert xdrmem_create(&xdrs, mymem, MAX_AUTH_BYTES, XDR_ENCODE);
149*7f2fe78bSCy Schubert if (! xdr_authunix_parms(&xdrs, &aup))
150*7f2fe78bSCy Schubert abort();
151*7f2fe78bSCy Schubert au->au_origcred.oa_length = len = XDR_GETPOS(&xdrs);
152*7f2fe78bSCy Schubert au->au_origcred.oa_flavor = AUTH_UNIX;
153*7f2fe78bSCy Schubert #ifdef KERNEL
154*7f2fe78bSCy Schubert au->au_origcred.oa_base = mem_alloc((u_int) len);
155*7f2fe78bSCy Schubert #else
156*7f2fe78bSCy Schubert if ((au->au_origcred.oa_base = mem_alloc((u_int) len)) == NULL) {
157*7f2fe78bSCy Schubert (void)fprintf(stderr, "authunix_create: out of memory\n");
158*7f2fe78bSCy Schubert return (NULL);
159*7f2fe78bSCy Schubert }
160*7f2fe78bSCy Schubert #endif
161*7f2fe78bSCy Schubert memmove(au->au_origcred.oa_base, mymem, (u_int)len);
162*7f2fe78bSCy Schubert
163*7f2fe78bSCy Schubert /*
164*7f2fe78bSCy Schubert * set auth handle to reflect new cred.
165*7f2fe78bSCy Schubert */
166*7f2fe78bSCy Schubert auth->ah_cred = au->au_origcred;
167*7f2fe78bSCy Schubert marshal_new_auth(auth);
168*7f2fe78bSCy Schubert return (auth);
169*7f2fe78bSCy Schubert }
170*7f2fe78bSCy Schubert
171*7f2fe78bSCy Schubert /*
172*7f2fe78bSCy Schubert * Returns an auth handle with parameters determined by doing lots of
173*7f2fe78bSCy Schubert * syscalls.
174*7f2fe78bSCy Schubert */
175*7f2fe78bSCy Schubert AUTH *
authunix_create_default(void)176*7f2fe78bSCy Schubert authunix_create_default(void)
177*7f2fe78bSCy Schubert {
178*7f2fe78bSCy Schubert int len;
179*7f2fe78bSCy Schubert char machname[MAX_MACHINE_NAME + 1];
180*7f2fe78bSCy Schubert int uid;
181*7f2fe78bSCy Schubert int gid;
182*7f2fe78bSCy Schubert GETGROUPS_T gids[NGRPS];
183*7f2fe78bSCy Schubert int igids[NGRPS], i;
184*7f2fe78bSCy Schubert
185*7f2fe78bSCy Schubert if (gethostname(machname, MAX_MACHINE_NAME) == -1)
186*7f2fe78bSCy Schubert abort();
187*7f2fe78bSCy Schubert machname[MAX_MACHINE_NAME] = 0;
188*7f2fe78bSCy Schubert uid = geteuid();
189*7f2fe78bSCy Schubert gid = getegid();
190*7f2fe78bSCy Schubert if ((len = getgroups(NGRPS, gids)) < 0)
191*7f2fe78bSCy Schubert abort();
192*7f2fe78bSCy Schubert for(i = 0; i < NGRPS; i++) {
193*7f2fe78bSCy Schubert igids[i] = gids[i];
194*7f2fe78bSCy Schubert }
195*7f2fe78bSCy Schubert return (authunix_create(machname, uid, gid, len, igids));
196*7f2fe78bSCy Schubert }
197*7f2fe78bSCy Schubert
198*7f2fe78bSCy Schubert /*
199*7f2fe78bSCy Schubert * authunix operations
200*7f2fe78bSCy Schubert */
201*7f2fe78bSCy Schubert
202*7f2fe78bSCy Schubert static void
authunix_nextverf(AUTH * auth)203*7f2fe78bSCy Schubert authunix_nextverf(AUTH *auth)
204*7f2fe78bSCy Schubert {
205*7f2fe78bSCy Schubert /* no action necessary */
206*7f2fe78bSCy Schubert }
207*7f2fe78bSCy Schubert
208*7f2fe78bSCy Schubert static bool_t
authunix_marshal(AUTH * auth,XDR * xdrs)209*7f2fe78bSCy Schubert authunix_marshal(AUTH *auth, XDR *xdrs)
210*7f2fe78bSCy Schubert {
211*7f2fe78bSCy Schubert struct audata *au = AUTH_PRIVATE(auth);
212*7f2fe78bSCy Schubert
213*7f2fe78bSCy Schubert return (XDR_PUTBYTES(xdrs, au->au_marshed, au->au_mpos));
214*7f2fe78bSCy Schubert }
215*7f2fe78bSCy Schubert
216*7f2fe78bSCy Schubert static bool_t
authunix_validate(AUTH * auth,struct opaque_auth * verf)217*7f2fe78bSCy Schubert authunix_validate(AUTH *auth, struct opaque_auth *verf)
218*7f2fe78bSCy Schubert {
219*7f2fe78bSCy Schubert struct audata *au;
220*7f2fe78bSCy Schubert XDR xdrs;
221*7f2fe78bSCy Schubert
222*7f2fe78bSCy Schubert if (verf->oa_flavor == AUTH_SHORT) {
223*7f2fe78bSCy Schubert au = AUTH_PRIVATE(auth);
224*7f2fe78bSCy Schubert xdrmem_create(&xdrs, verf->oa_base, verf->oa_length, XDR_DECODE);
225*7f2fe78bSCy Schubert
226*7f2fe78bSCy Schubert if (au->au_shcred.oa_base != NULL) {
227*7f2fe78bSCy Schubert mem_free(au->au_shcred.oa_base,
228*7f2fe78bSCy Schubert au->au_shcred.oa_length);
229*7f2fe78bSCy Schubert au->au_shcred.oa_base = NULL;
230*7f2fe78bSCy Schubert }
231*7f2fe78bSCy Schubert if (xdr_opaque_auth(&xdrs, &au->au_shcred)) {
232*7f2fe78bSCy Schubert auth->ah_cred = au->au_shcred;
233*7f2fe78bSCy Schubert } else {
234*7f2fe78bSCy Schubert xdrs.x_op = XDR_FREE;
235*7f2fe78bSCy Schubert (void)xdr_opaque_auth(&xdrs, &au->au_shcred);
236*7f2fe78bSCy Schubert au->au_shcred.oa_base = NULL;
237*7f2fe78bSCy Schubert auth->ah_cred = au->au_origcred;
238*7f2fe78bSCy Schubert }
239*7f2fe78bSCy Schubert marshal_new_auth(auth);
240*7f2fe78bSCy Schubert }
241*7f2fe78bSCy Schubert return (TRUE);
242*7f2fe78bSCy Schubert }
243*7f2fe78bSCy Schubert
244*7f2fe78bSCy Schubert static bool_t
authunix_refresh(AUTH * auth,struct rpc_msg * msg)245*7f2fe78bSCy Schubert authunix_refresh(AUTH *auth, struct rpc_msg *msg)
246*7f2fe78bSCy Schubert {
247*7f2fe78bSCy Schubert struct audata *au = AUTH_PRIVATE(auth);
248*7f2fe78bSCy Schubert struct authunix_parms aup;
249*7f2fe78bSCy Schubert struct timeval now;
250*7f2fe78bSCy Schubert XDR xdrs;
251*7f2fe78bSCy Schubert int stat;
252*7f2fe78bSCy Schubert
253*7f2fe78bSCy Schubert if (auth->ah_cred.oa_base == au->au_origcred.oa_base) {
254*7f2fe78bSCy Schubert /* there is no hope. Punt */
255*7f2fe78bSCy Schubert return (FALSE);
256*7f2fe78bSCy Schubert }
257*7f2fe78bSCy Schubert au->au_shfaults ++;
258*7f2fe78bSCy Schubert
259*7f2fe78bSCy Schubert /* first deserialize the creds back into a struct authunix_parms */
260*7f2fe78bSCy Schubert aup.aup_machname = NULL;
261*7f2fe78bSCy Schubert aup.aup_gids = (int *)NULL;
262*7f2fe78bSCy Schubert xdrmem_create(&xdrs, au->au_origcred.oa_base,
263*7f2fe78bSCy Schubert au->au_origcred.oa_length, XDR_DECODE);
264*7f2fe78bSCy Schubert stat = xdr_authunix_parms(&xdrs, &aup);
265*7f2fe78bSCy Schubert if (! stat)
266*7f2fe78bSCy Schubert goto done;
267*7f2fe78bSCy Schubert
268*7f2fe78bSCy Schubert /* update the time and serialize in place */
269*7f2fe78bSCy Schubert (void)gettimeofday(&now, (struct timezone *)0);
270*7f2fe78bSCy Schubert aup.aup_time = now.tv_sec;
271*7f2fe78bSCy Schubert xdrs.x_op = XDR_ENCODE;
272*7f2fe78bSCy Schubert XDR_SETPOS(&xdrs, 0);
273*7f2fe78bSCy Schubert stat = xdr_authunix_parms(&xdrs, &aup);
274*7f2fe78bSCy Schubert if (! stat)
275*7f2fe78bSCy Schubert goto done;
276*7f2fe78bSCy Schubert auth->ah_cred = au->au_origcred;
277*7f2fe78bSCy Schubert marshal_new_auth(auth);
278*7f2fe78bSCy Schubert done:
279*7f2fe78bSCy Schubert /* free the struct authunix_parms created by deserializing */
280*7f2fe78bSCy Schubert xdrs.x_op = XDR_FREE;
281*7f2fe78bSCy Schubert (void)xdr_authunix_parms(&xdrs, &aup);
282*7f2fe78bSCy Schubert XDR_DESTROY(&xdrs);
283*7f2fe78bSCy Schubert return (stat);
284*7f2fe78bSCy Schubert }
285*7f2fe78bSCy Schubert
286*7f2fe78bSCy Schubert static void
authunix_destroy(AUTH * auth)287*7f2fe78bSCy Schubert authunix_destroy(AUTH *auth)
288*7f2fe78bSCy Schubert {
289*7f2fe78bSCy Schubert struct audata *au = AUTH_PRIVATE(auth);
290*7f2fe78bSCy Schubert
291*7f2fe78bSCy Schubert mem_free(au->au_origcred.oa_base, au->au_origcred.oa_length);
292*7f2fe78bSCy Schubert
293*7f2fe78bSCy Schubert if (au->au_shcred.oa_base != NULL)
294*7f2fe78bSCy Schubert mem_free(au->au_shcred.oa_base, au->au_shcred.oa_length);
295*7f2fe78bSCy Schubert
296*7f2fe78bSCy Schubert mem_free(auth->ah_private, sizeof(struct audata));
297*7f2fe78bSCy Schubert
298*7f2fe78bSCy Schubert if (auth->ah_verf.oa_base != NULL)
299*7f2fe78bSCy Schubert mem_free(auth->ah_verf.oa_base, auth->ah_verf.oa_length);
300*7f2fe78bSCy Schubert
301*7f2fe78bSCy Schubert mem_free((caddr_t)auth, sizeof(*auth));
302*7f2fe78bSCy Schubert }
303*7f2fe78bSCy Schubert
304*7f2fe78bSCy Schubert /*
305*7f2fe78bSCy Schubert * Marshals (pre-serializes) an auth struct.
306*7f2fe78bSCy Schubert * sets private data, au_marshed and au_mpos
307*7f2fe78bSCy Schubert */
308*7f2fe78bSCy Schubert static void
marshal_new_auth(AUTH * auth)309*7f2fe78bSCy Schubert marshal_new_auth(AUTH *auth)
310*7f2fe78bSCy Schubert {
311*7f2fe78bSCy Schubert XDR xdr_stream;
312*7f2fe78bSCy Schubert XDR *xdrs = &xdr_stream;
313*7f2fe78bSCy Schubert struct audata *au = AUTH_PRIVATE(auth);
314*7f2fe78bSCy Schubert
315*7f2fe78bSCy Schubert xdrmem_create(xdrs, au->au_marshed, MAX_AUTH_BYTES, XDR_ENCODE);
316*7f2fe78bSCy Schubert if ((! xdr_opaque_auth(xdrs, &(auth->ah_cred))) ||
317*7f2fe78bSCy Schubert (! xdr_opaque_auth(xdrs, &(auth->ah_verf)))) {
318*7f2fe78bSCy Schubert perror("auth_none.c - Fatal marshalling problem");
319*7f2fe78bSCy Schubert } else {
320*7f2fe78bSCy Schubert au->au_mpos = XDR_GETPOS(xdrs);
321*7f2fe78bSCy Schubert }
322*7f2fe78bSCy Schubert XDR_DESTROY(xdrs);
323*7f2fe78bSCy Schubert }
324*7f2fe78bSCy Schubert
325*7f2fe78bSCy Schubert static bool_t
authunix_wrap(AUTH * auth,XDR * xdrs,xdrproc_t xfunc,caddr_t xwhere)326*7f2fe78bSCy Schubert authunix_wrap(AUTH *auth, XDR *xdrs, xdrproc_t xfunc, caddr_t xwhere)
327*7f2fe78bSCy Schubert {
328*7f2fe78bSCy Schubert return ((*xfunc)(xdrs, xwhere));
329*7f2fe78bSCy Schubert }
330