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