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