xref: /openbsd-src/lib/libc/rpc/auth_unix.c (revision ce7e0fc6a9d74d25b78fb6ad846387717f5172b6)
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.15 2002/01/02 23:00:10 deraadt 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/rpc.h>
54 #include <rpc/auth.h>
55 #include <rpc/auth_unix.h>
56 
57 /*
58  * Unix authenticator operations vector
59  */
60 static void	authunix_nextverf(struct __rpc_auth *);
61 static bool_t	authunix_marshal(struct __rpc_auth *, XDR *);
62 static bool_t	authunix_validate(struct __rpc_auth *, struct opaque_auth *);
63 static bool_t	authunix_refresh(struct __rpc_auth *);
64 static void	authunix_destroy(struct __rpc_auth *);
65 
66 static struct auth_ops auth_unix_ops = {
67 	authunix_nextverf,
68 	authunix_marshal,
69 	authunix_validate,
70 	authunix_refresh,
71 	authunix_destroy
72 };
73 
74 /*
75  * This struct is pointed to by the ah_private field of an auth_handle.
76  */
77 struct audata {
78 	struct opaque_auth	au_origcred;	/* original credentials */
79 	struct opaque_auth	au_shcred;	/* short hand cred */
80 	u_long			au_shfaults;	/* short hand cache faults */
81 	char			au_marshed[MAX_AUTH_BYTES];
82 	u_int			au_mpos;	/* xdr pos at end of marshed */
83 };
84 #define	AUTH_PRIVATE(auth)	((struct audata *)auth->ah_private)
85 
86 static void marshal_new_auth();
87 
88 
89 /*
90  * Create a unix style authenticator.
91  * Returns an auth handle with the given stuff in it.
92  */
93 AUTH *
94 authunix_create(machname, uid, gid, len, aup_gids)
95 	char *machname;
96 	int uid;
97 	int gid;
98 	int len;
99 	int *aup_gids;
100 {
101 	struct authunix_parms aup;
102 	char mymem[MAX_AUTH_BYTES];
103 	struct timeval now;
104 	XDR xdrs;
105 	AUTH *auth;
106 	struct audata *au;
107 
108 	/*
109 	 * Allocate and set up auth handle
110 	 */
111 	auth = (AUTH *)mem_alloc(sizeof(*auth));
112 #ifndef KERNEL
113 	if (auth == NULL) {
114 		(void)fprintf(stderr, "authunix_create: out of memory\n");
115 		return (NULL);
116 	}
117 #endif
118 	au = (struct audata *)mem_alloc(sizeof(*au));
119 #ifndef KERNEL
120 	if (au == NULL) {
121 		(void)fprintf(stderr, "authunix_create: out of memory\n");
122 		free(auth);
123 		return (NULL);
124 	}
125 #endif
126 	auth->ah_ops = &auth_unix_ops;
127 	auth->ah_private = (caddr_t)au;
128 	auth->ah_verf = au->au_shcred = _null_auth;
129 	au->au_shfaults = 0;
130 
131 	/*
132 	 * fill in param struct from the given params
133 	 */
134 	(void)gettimeofday(&now,  (struct timezone *)0);
135 	aup.aup_time = now.tv_sec;
136 	aup.aup_machname = machname;
137 	aup.aup_uid = uid;
138 	aup.aup_gid = gid;
139 	aup.aup_len = (u_int)len;
140 	aup.aup_gids = aup_gids;
141 
142 	/*
143 	 * Serialize the parameters into origcred
144 	 */
145 	xdrmem_create(&xdrs, mymem, MAX_AUTH_BYTES, XDR_ENCODE);
146 	if (!xdr_authunix_parms(&xdrs, &aup))
147 		abort();	/* XXX abort illegal in library */
148 	au->au_origcred.oa_length = len = XDR_GETPOS(&xdrs);
149 	au->au_origcred.oa_flavor = AUTH_UNIX;
150 #ifdef KERNEL
151 	au->au_origcred.oa_base = mem_alloc((u_int) len);
152 #else
153 	if ((au->au_origcred.oa_base = mem_alloc((u_int) len)) == NULL) {
154 		(void)fprintf(stderr, "authunix_create: out of memory\n");
155 		return (NULL);
156 	}
157 #endif
158 	memcpy(au->au_origcred.oa_base, mymem, (u_int)len);
159 
160 	/*
161 	 * set auth handle to reflect new cred.
162 	 */
163 	auth->ah_cred = au->au_origcred;
164 	marshal_new_auth(auth);
165 	return (auth);
166 }
167 
168 /*
169  * Returns an auth handle with parameters determined by doing lots of
170  * syscalls.
171  */
172 AUTH *
173 authunix_create_default()
174 {
175 	int len, i;
176 	char machname[MAX_MACHINE_NAME + 1];
177 	uid_t uid;
178 	gid_t gid;
179 	gid_t gids[NGRPS];
180 	int gids2[NGRPS];
181 
182 	if (gethostname(machname, sizeof machname) == -1)
183 		return (NULL);
184 	machname[MAX_MACHINE_NAME] = 0;
185 	uid = geteuid();
186 	gid = getegid();
187 	if ((len = getgroups(NGRPS, gids)) < 0)
188 		return (NULL);
189 	for (i = 0; i < len; i++)
190 		gids2[i] = gids[i];
191 	return (authunix_create(machname, uid, gid, len, gids2));
192 }
193 
194 /*
195  * authunix operations
196  */
197 /* ARGSUSED */
198 static void
199 authunix_nextverf(auth)
200 	AUTH *auth;
201 {
202 	/* no action necessary */
203 }
204 
205 static bool_t
206 authunix_marshal(auth, xdrs)
207 	AUTH *auth;
208 	XDR *xdrs;
209 {
210 	struct audata *au = AUTH_PRIVATE(auth);
211 
212 	return (XDR_PUTBYTES(xdrs, au->au_marshed, au->au_mpos));
213 }
214 
215 static bool_t
216 authunix_validate(auth, verf)
217 	AUTH *auth;
218 	struct opaque_auth *verf;
219 {
220 	struct audata *au;
221 	XDR xdrs;
222 
223 	if (verf->oa_flavor == AUTH_SHORT) {
224 		au = AUTH_PRIVATE(auth);
225 		xdrmem_create(&xdrs, verf->oa_base, verf->oa_length, XDR_DECODE);
226 
227 		if (au->au_shcred.oa_base != NULL) {
228 			mem_free(au->au_shcred.oa_base,
229 			    au->au_shcred.oa_length);
230 			au->au_shcred.oa_base = NULL;
231 		}
232 		if (xdr_opaque_auth(&xdrs, &au->au_shcred)) {
233 			auth->ah_cred = au->au_shcred;
234 		} else {
235 			xdrs.x_op = XDR_FREE;
236 			(void)xdr_opaque_auth(&xdrs, &au->au_shcred);
237 			au->au_shcred.oa_base = NULL;
238 			auth->ah_cred = au->au_origcred;
239 		}
240 		marshal_new_auth(auth);
241 	}
242 	return (TRUE);
243 }
244 
245 static bool_t
246 authunix_refresh(auth)
247 	AUTH *auth;
248 {
249 	struct audata *au = AUTH_PRIVATE(auth);
250 	struct authunix_parms aup;
251 	struct timeval now;
252 	XDR xdrs;
253 	int stat;
254 
255 	if (auth->ah_cred.oa_base == au->au_origcred.oa_base) {
256 		/* there is no hope.  Punt */
257 		return (FALSE);
258 	}
259 	au->au_shfaults ++;
260 
261 	/* first deserialize the creds back into a struct authunix_parms */
262 	aup.aup_machname = NULL;
263 	aup.aup_gids = (int *)NULL;
264 	xdrmem_create(&xdrs, au->au_origcred.oa_base,
265 	    au->au_origcred.oa_length, XDR_DECODE);
266 	stat = xdr_authunix_parms(&xdrs, &aup);
267 	if (! stat)
268 		goto done;
269 
270 	/* update the time and serialize in place */
271 	(void)gettimeofday(&now, (struct timezone *)0);
272 	aup.aup_time = now.tv_sec;
273 	xdrs.x_op = XDR_ENCODE;
274 	XDR_SETPOS(&xdrs, 0);
275 	stat = xdr_authunix_parms(&xdrs, &aup);
276 	if (! stat)
277 		goto done;
278 	auth->ah_cred = au->au_origcred;
279 	marshal_new_auth(auth);
280 done:
281 	/* free the struct authunix_parms created by deserializing */
282 	xdrs.x_op = XDR_FREE;
283 	(void)xdr_authunix_parms(&xdrs, &aup);
284 	XDR_DESTROY(&xdrs);
285 	return (stat);
286 }
287 
288 static void
289 authunix_destroy(auth)
290 	AUTH *auth;
291 {
292 	struct audata *au = AUTH_PRIVATE(auth);
293 
294 	mem_free(au->au_origcred.oa_base, au->au_origcred.oa_length);
295 
296 	if (au->au_shcred.oa_base != NULL)
297 		mem_free(au->au_shcred.oa_base, au->au_shcred.oa_length);
298 
299 	mem_free(auth->ah_private, sizeof(struct audata));
300 
301 	if (auth->ah_verf.oa_base != NULL)
302 		mem_free(auth->ah_verf.oa_base, auth->ah_verf.oa_length);
303 
304 	mem_free((caddr_t)auth, sizeof(*auth));
305 }
306 
307 /*
308  * Marshals (pre-serializes) an auth struct.
309  * sets private data, au_marshed and au_mpos
310  */
311 static void
312 marshal_new_auth(auth)
313 	AUTH *auth;
314 {
315 	XDR		xdr_stream;
316 	XDR	*xdrs = &xdr_stream;
317 	struct audata *au = AUTH_PRIVATE(auth);
318 
319 	xdrmem_create(xdrs, au->au_marshed, MAX_AUTH_BYTES, XDR_ENCODE);
320 	if ((! xdr_opaque_auth(xdrs, &(auth->ah_cred))) ||
321 	    (! xdr_opaque_auth(xdrs, &(auth->ah_verf)))) {
322 		perror("auth_none.c - Fatal marshalling problem");
323 	} else {
324 		au->au_mpos = XDR_GETPOS(xdrs);
325 	}
326 	XDR_DESTROY(xdrs);
327 }
328