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