b1e6a5f519094e847979c40d891859e1be179dcc
[shibboleth/cpp-sp.git] / oncrpc / auth_uni.c
1 /*********************************************************************
2  * RPC for the Windows NT Operating System
3  * 1993 by Martin F. Gergeleit
4  * Users may use, copy or modify Sun RPC for the Windows NT Operating 
5  * System according to the Sun copyright below.
6  *
7  * RPC for the Windows NT Operating System COMES WITH ABSOLUTELY NO 
8  * WARRANTY, NOR WILL I BE LIABLE FOR ANY DAMAGES INCURRED FROM THE 
9  * USE OF. USE ENTIRELY AT YOUR OWN RISK!!!
10  *********************************************************************/
11
12 /* @(#)auth_unix.c      2.2 88/08/01 4.0 RPCSRC */
13 /*
14  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
15  * unrestricted use provided that this legend is included on all tape
16  * media and as a part of the software program in whole or part.  Users
17  * may copy or modify Sun RPC without charge, but are not authorized
18  * to license or distribute it to anyone else except as part of a product or
19  * program developed by the user.
20  *
21  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
22  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
24  *
25  * Sun RPC is provided with no support and without any obligation on the
26  * part of Sun Microsystems, Inc. to assist in its use, correction,
27  * modification or enhancement.
28  *
29  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
30  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
31  * OR ANY PART THEREOF.
32  *
33  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
34  * or profits or other special, indirect and consequential damages, even if
35  * Sun has been advised of the possibility of such damages.
36  *
37  * Sun Microsystems, Inc.
38  * 2550 Garcia Avenue
39  * Mountain View, California  94043
40  */
41 #if !defined(lint) && defined(SCCSIDS)
42 static char sccsid[] = "@(#)auth_unix.c 1.19 87/08/11 Copyr 1984 Sun Micro";
43 #endif
44
45 /*
46  * auth_unix.c, Implements UNIX style authentication parameters.
47  *
48  * Copyright (C) 1984, Sun Microsystems, Inc.
49  *
50  * The system is very weak.  The client uses no encryption for it's
51  * credentials and only sends null verifiers.  The server sends backs
52  * null verifiers or optionally a verifier that suggests a new short hand
53  * for the credentials.
54  *
55  */
56
57 #include <stdio.h>
58
59 #ifdef WIN32
60 #include <rpc/rpc.h>
61 #include <rpc/xdr.h>
62 #include <rpc/auth.h>
63 #include <rpc/auth_unix.h>
64 #else
65 #include <rpc/types.h>
66 #include <rpc/rpc.h>
67 #include <rpc/xdr.h>
68 #include <rpc/auth.h>
69 #include <rpc/auth_unix.h>
70 #endif
71
72 /*
73  * Unix authenticator operations vector
74  */
75 static void     authunix_nextverf();
76 static bool_t   authunix_marshal();
77 static bool_t   authunix_validate();
78 static bool_t   authunix_refresh();
79 static void     authunix_destroy();
80
81 static struct auth_ops auth_unix_ops = {
82         authunix_nextverf,
83         authunix_marshal,
84         authunix_validate,
85         authunix_refresh,
86         authunix_destroy
87 };
88
89 /*
90  * This struct is pointed to by the ah_private field of an auth_handle.
91  */
92 struct audata {
93         struct opaque_auth      au_origcred;    /* original credentials */
94         struct opaque_auth      au_shcred;      /* short hand cred */
95         u_long                  au_shfaults;    /* short hand cache faults */
96         char                    au_marshed[MAX_AUTH_BYTES];
97         u_int                   au_mpos;        /* xdr pos at end of marshed */
98 };
99 #define AUTH_PRIVATE(auth)      ((struct audata *)auth->ah_private)
100
101 static bool_t marshal_new_auth();
102
103
104 /*
105  * Create a unix style authenticator.
106  * Returns an auth handle with the given stuff in it.
107  */
108 AUTH *
109 authunix_create(machname, uid, gid, len, aup_gids)
110         char *machname;
111         int uid;
112         int gid;
113         register int len;
114         int *aup_gids;
115 {
116         struct authunix_parms aup;
117         char mymem[MAX_AUTH_BYTES];
118         struct timeval now;
119         XDR xdrs;
120         register AUTH *auth;
121         register struct audata *au;
122
123         /*
124          * Allocate and set up auth handle
125          */
126         auth = (AUTH *)mem_alloc(sizeof(*auth));
127 #ifndef KERNEL
128         if (auth == NULL) {
129 #ifdef WIN32
130                 nt_rpc_report("authunix_create: out of memory\n");
131 #else
132                 (void)fprintf(stderr, "authunix_create: out of memory\n");
133 #endif
134                 return (NULL);
135         }
136 #endif
137         au = (struct audata *)mem_alloc(sizeof(*au));
138 #ifndef KERNEL
139         if (au == NULL) {
140 #ifdef WIN32
141                 nt_rpc_report("authunix_create: out of memory\n");
142 #else
143                 (void)fprintf(stderr, "authunix_create: out of memory\n");
144 #endif
145                 return (NULL);
146         }
147 #endif
148         auth->ah_ops = &auth_unix_ops;
149         auth->ah_private = (caddr_t)au;
150         auth->ah_verf = au->au_shcred = _null_auth;
151         au->au_shfaults = 0;
152
153         /*
154          * fill in param struct from the given params
155          */
156         (void)gettimeofday(&now,  (struct timezone *)0);
157         aup.aup_time = now.tv_sec;
158         aup.aup_machname = machname;
159         aup.aup_uid = uid;
160         aup.aup_gid = gid;
161         aup.aup_len = (u_int)len;
162         aup.aup_gids = aup_gids;
163
164         /*
165          * Serialize the parameters into origcred
166          */
167         xdrmem_create(&xdrs, mymem, MAX_AUTH_BYTES, XDR_ENCODE);
168         if (! xdr_authunix_parms(&xdrs, &aup))
169                 abort();
170         au->au_origcred.oa_length = len = XDR_GETPOS(&xdrs);
171         au->au_origcred.oa_flavor = AUTH_UNIX;
172 #ifdef KERNEL
173         au->au_origcred.oa_base = mem_alloc((u_int) len);
174 #else
175         if ((au->au_origcred.oa_base = mem_alloc((u_int) len)) == NULL) {
176 #ifdef WIN32
177                 nt_rpc_report("authunix_create: out of memory\n");
178 #else
179                 (void)fprintf(stderr, "authunix_create: out of memory\n");
180 #endif
181                 return (NULL);
182         }
183 #endif
184         bcopy(mymem, au->au_origcred.oa_base, (u_int)len);
185
186         /*
187          * set auth handle to reflect new cred.
188          */
189         auth->ah_cred = au->au_origcred;
190         marshal_new_auth(auth);
191         return (auth);
192 }
193
194 /*
195  * Returns an auth handle with parameters determined by doing lots of
196  * syscalls.
197  */
198 AUTH *
199 authunix_create_default()
200 {
201         register int len;
202         char machname[MAX_MACHINE_NAME + 1];
203         register int uid;
204         register int gid;
205         int gids[NGRPS];
206
207         if (gethostname(machname, MAX_MACHINE_NAME) == -1)
208                 abort();
209         machname[MAX_MACHINE_NAME] = 0;
210 #ifdef WIN32
211 /* who knows anything better? */
212         uid = 0;
213         gid = 0;
214         len = 1;
215         gids[0] = 0;
216 #else
217         uid = geteuid();
218         gid = getegid();
219         if ((len = getgroups(NGRPS, gids)) < 0)
220                 abort();
221 #endif
222         return (authunix_create(machname, uid, gid, len, gids));
223 }
224
225 /*
226  * authunix operations
227  */
228
229 static void
230 authunix_nextverf(auth)
231         AUTH *auth;
232 {
233         /* no action necessary */
234 }
235
236 static bool_t
237 authunix_marshal(auth, xdrs)
238         AUTH *auth;
239         XDR *xdrs;
240 {
241         register struct audata *au = AUTH_PRIVATE(auth);
242
243         return (XDR_PUTBYTES(xdrs, au->au_marshed, au->au_mpos));
244 }
245
246 static bool_t
247 authunix_validate(auth, verf)
248         register AUTH *auth;
249         struct opaque_auth verf;
250 {
251         register struct audata *au;
252         XDR xdrs;
253
254         if (verf.oa_flavor == AUTH_SHORT) {
255                 au = AUTH_PRIVATE(auth);
256                 xdrmem_create(&xdrs, verf.oa_base, verf.oa_length, XDR_DECODE);
257
258                 if (au->au_shcred.oa_base != NULL) {
259                         mem_free(au->au_shcred.oa_base,
260                             au->au_shcred.oa_length);
261                         au->au_shcred.oa_base = NULL;
262                 }
263                 if (xdr_opaque_auth(&xdrs, &au->au_shcred)) {
264                         auth->ah_cred = au->au_shcred;
265                 } else {
266                         xdrs.x_op = XDR_FREE;
267                         (void)xdr_opaque_auth(&xdrs, &au->au_shcred);
268                         au->au_shcred.oa_base = NULL;
269                         auth->ah_cred = au->au_origcred;
270                 }
271                 marshal_new_auth(auth);
272         }
273         return (TRUE);
274 }
275
276 static bool_t
277 authunix_refresh(auth)
278         register AUTH *auth;
279 {
280         register struct audata *au = AUTH_PRIVATE(auth);
281         struct authunix_parms aup;
282         struct timeval now;
283         XDR xdrs;
284         register int stat;
285
286         if (auth->ah_cred.oa_base == au->au_origcred.oa_base) {
287                 /* there is no hope.  Punt */
288                 return (FALSE);
289         }
290         au->au_shfaults ++;
291
292         /* first deserialize the creds back into a struct authunix_parms */
293         aup.aup_machname = NULL;
294         aup.aup_gids = (int *)NULL;
295         xdrmem_create(&xdrs, au->au_origcred.oa_base,
296             au->au_origcred.oa_length, XDR_DECODE);
297         stat = xdr_authunix_parms(&xdrs, &aup);
298         if (! stat)
299                 goto done;
300
301         /* update the time and serialize in place */
302         (void)gettimeofday(&now, (struct timezone *)0);
303         aup.aup_time = now.tv_sec;
304         xdrs.x_op = XDR_ENCODE;
305         XDR_SETPOS(&xdrs, 0);
306         stat = xdr_authunix_parms(&xdrs, &aup);
307         if (! stat)
308                 goto done;
309         auth->ah_cred = au->au_origcred;
310         marshal_new_auth(auth);
311 done:
312         /* free the struct authunix_parms created by deserializing */
313         xdrs.x_op = XDR_FREE;
314         (void)xdr_authunix_parms(&xdrs, &aup);
315         XDR_DESTROY(&xdrs);
316         return (stat);
317 }
318
319 static void
320 authunix_destroy(auth)
321         register AUTH *auth;
322 {
323         register struct audata *au = AUTH_PRIVATE(auth);
324
325         mem_free(au->au_origcred.oa_base, au->au_origcred.oa_length);
326
327         if (au->au_shcred.oa_base != NULL)
328                 mem_free(au->au_shcred.oa_base, au->au_shcred.oa_length);
329
330         mem_free(auth->ah_private, sizeof(struct audata));
331
332         if (auth->ah_verf.oa_base != NULL)
333                 mem_free(auth->ah_verf.oa_base, auth->ah_verf.oa_length);
334
335         mem_free((caddr_t)auth, sizeof(*auth));
336 }
337
338 /*
339  * Marshals (pre-serializes) an auth struct.
340  * sets private data, au_marshed and au_mpos
341  */
342 static bool_t
343 marshal_new_auth(auth)
344         register AUTH *auth;
345 {
346         XDR             xdr_stream;
347         register XDR    *xdrs = &xdr_stream;
348         register struct audata *au = AUTH_PRIVATE(auth);
349
350         xdrmem_create(xdrs, au->au_marshed, MAX_AUTH_BYTES, XDR_ENCODE);
351         if ((! xdr_opaque_auth(xdrs, &(auth->ah_cred))) ||
352             (! xdr_opaque_auth(xdrs, &(auth->ah_verf)))) {
353                 perror("auth_none.c - Fatal marshalling problem");
354         XDR_DESTROY(xdrs);
355         return FALSE;
356         } else {
357                 au->au_mpos = XDR_GETPOS(xdrs);
358         }
359         XDR_DESTROY(xdrs);
360     return TRUE;
361 }