xref: /openbsd-src/lib/libc/rpc/auth_unix.c (revision c5f4fad510dd427c0c20c0f4d164f60ce24651b6)
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.18 2005/04/01 07:44:03 otto 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(AUTH *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(char *machname, int uid, int gid, int len, int *aup_gids)
95 {
96 	struct authunix_parms aup;
97 	char mymem[MAX_AUTH_BYTES];
98 	struct timeval now;
99 	XDR xdrs;
100 	AUTH *auth;
101 	struct audata *au;
102 
103 	/*
104 	 * Allocate and set up auth handle
105 	 */
106 	auth = (AUTH *)mem_alloc(sizeof(*auth));
107 #ifndef KERNEL
108 	if (auth == NULL) {
109 		(void)fprintf(stderr, "authunix_create: out of memory\n");
110 		return (NULL);
111 	}
112 #endif
113 	au = (struct audata *)mem_alloc(sizeof(*au));
114 #ifndef KERNEL
115 	if (au == NULL) {
116 		(void)fprintf(stderr, "authunix_create: out of memory\n");
117 		free(auth);
118 		return (NULL);
119 	}
120 #endif
121 	auth->ah_ops = &auth_unix_ops;
122 	auth->ah_private = (caddr_t)au;
123 	auth->ah_verf = au->au_shcred = _null_auth;
124 	au->au_shfaults = 0;
125 
126 	/*
127 	 * fill in param struct from the given params
128 	 */
129 	(void)gettimeofday(&now,  NULL);
130 	aup.aup_time = now.tv_sec;
131 	aup.aup_machname = machname;
132 	aup.aup_uid = uid;
133 	aup.aup_gid = gid;
134 	aup.aup_len = (u_int)len;
135 	aup.aup_gids = aup_gids;
136 
137 	/*
138 	 * Serialize the parameters into origcred
139 	 */
140 	xdrmem_create(&xdrs, mymem, MAX_AUTH_BYTES, XDR_ENCODE);
141 	if (!xdr_authunix_parms(&xdrs, &aup))
142 		abort();	/* XXX abort illegal in library */
143 	au->au_origcred.oa_length = len = XDR_GETPOS(&xdrs);
144 	au->au_origcred.oa_flavor = AUTH_UNIX;
145 #ifdef KERNEL
146 	au->au_origcred.oa_base = mem_alloc((u_int) len);
147 #else
148 	if ((au->au_origcred.oa_base = mem_alloc((u_int) len)) == NULL) {
149 		(void)fprintf(stderr, "authunix_create: out of memory\n");
150 		XDR_DESTROY(&xdrs);
151 		free(au);
152 		free(auth);
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(void)
172 {
173 	int len, i;
174 	char machname[MAX_MACHINE_NAME + 1];
175 	uid_t uid;
176 	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 *auth)
198 {
199 	/* no action necessary */
200 }
201 
202 static bool_t
203 authunix_marshal(AUTH *auth, XDR *xdrs)
204 {
205 	struct audata *au = AUTH_PRIVATE(auth);
206 
207 	return (XDR_PUTBYTES(xdrs, au->au_marshed, au->au_mpos));
208 }
209 
210 static bool_t
211 authunix_validate(AUTH *auth, struct opaque_auth *verf)
212 {
213 	struct audata *au;
214 	XDR xdrs;
215 
216 	if (verf->oa_flavor == AUTH_SHORT) {
217 		au = AUTH_PRIVATE(auth);
218 		xdrmem_create(&xdrs, verf->oa_base, verf->oa_length, XDR_DECODE);
219 
220 		if (au->au_shcred.oa_base != NULL) {
221 			mem_free(au->au_shcred.oa_base,
222 			    au->au_shcred.oa_length);
223 			au->au_shcred.oa_base = NULL;
224 		}
225 		if (xdr_opaque_auth(&xdrs, &au->au_shcred)) {
226 			auth->ah_cred = au->au_shcred;
227 		} else {
228 			xdrs.x_op = XDR_FREE;
229 			(void)xdr_opaque_auth(&xdrs, &au->au_shcred);
230 			au->au_shcred.oa_base = NULL;
231 			auth->ah_cred = au->au_origcred;
232 		}
233 		marshal_new_auth(auth);
234 	}
235 	return (TRUE);
236 }
237 
238 static bool_t
239 authunix_refresh(AUTH *auth)
240 {
241 	struct audata *au = AUTH_PRIVATE(auth);
242 	struct authunix_parms aup;
243 	struct timeval now;
244 	XDR xdrs;
245 	int stat;
246 
247 	if (auth->ah_cred.oa_base == au->au_origcred.oa_base) {
248 		/* there is no hope.  Punt */
249 		return (FALSE);
250 	}
251 	au->au_shfaults ++;
252 
253 	/* first deserialize the creds back into a struct authunix_parms */
254 	aup.aup_machname = NULL;
255 	aup.aup_gids = NULL;
256 	xdrmem_create(&xdrs, au->au_origcred.oa_base,
257 	    au->au_origcred.oa_length, XDR_DECODE);
258 	stat = xdr_authunix_parms(&xdrs, &aup);
259 	if (! stat)
260 		goto done;
261 
262 	/* update the time and serialize in place */
263 	(void)gettimeofday(&now, NULL);
264 	aup.aup_time = now.tv_sec;
265 	xdrs.x_op = XDR_ENCODE;
266 	XDR_SETPOS(&xdrs, 0);
267 	stat = xdr_authunix_parms(&xdrs, &aup);
268 	if (! stat)
269 		goto done;
270 	auth->ah_cred = au->au_origcred;
271 	marshal_new_auth(auth);
272 done:
273 	/* free the struct authunix_parms created by deserializing */
274 	xdrs.x_op = XDR_FREE;
275 	(void)xdr_authunix_parms(&xdrs, &aup);
276 	XDR_DESTROY(&xdrs);
277 	return (stat);
278 }
279 
280 static void
281 authunix_destroy(AUTH *auth)
282 {
283 	struct audata *au = AUTH_PRIVATE(auth);
284 
285 	mem_free(au->au_origcred.oa_base, au->au_origcred.oa_length);
286 
287 	if (au->au_shcred.oa_base != NULL)
288 		mem_free(au->au_shcred.oa_base, au->au_shcred.oa_length);
289 
290 	mem_free(auth->ah_private, sizeof(struct audata));
291 
292 	if (auth->ah_verf.oa_base != NULL)
293 		mem_free(auth->ah_verf.oa_base, auth->ah_verf.oa_length);
294 
295 	mem_free((caddr_t)auth, sizeof(*auth));
296 }
297 
298 /*
299  * Marshals (pre-serializes) an auth struct.
300  * sets private data, au_marshed and au_mpos
301  */
302 static void
303 marshal_new_auth(AUTH *auth)
304 {
305 	XDR		xdr_stream;
306 	XDR	*xdrs = &xdr_stream;
307 	struct audata *au = AUTH_PRIVATE(auth);
308 
309 	xdrmem_create(xdrs, au->au_marshed, MAX_AUTH_BYTES, XDR_ENCODE);
310 	if ((! xdr_opaque_auth(xdrs, &(auth->ah_cred))) ||
311 	    (! xdr_opaque_auth(xdrs, &(auth->ah_verf)))) {
312 		perror("auth_none.c - Fatal marshalling problem");
313 	} else {
314 		au->au_mpos = XDR_GETPOS(xdrs);
315 	}
316 	XDR_DESTROY(xdrs);
317 }
318