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