xref: /dragonfly/lib/libc/rpc/auth_unix.c (revision 9a92bb4c)
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  * @(#)auth_unix.c	1.19 87/08/11 Copyr 1984 Sun Micro
30  * @(#)auth_unix.c	2.2 88/08/01 4.0 RPCSRC
31  * $FreeBSD: src/lib/libc/rpc/auth_unix.c,v 1.12 1999/12/29 05:04:16 peter Exp $
32  * $DragonFly: src/lib/libc/rpc/auth_unix.c,v 1.4 2005/11/13 12:27:04 swildner Exp $
33  */
34 
35 /*
36  * auth_unix.c, Implements UNIX style authentication parameters.
37  *
38  * Copyright (C) 1984, Sun Microsystems, Inc.
39  *
40  * The system is very weak.  The client uses no encryption for it's
41  * credentials and only sends null verifiers.  The server sends backs
42  * null verifiers or optionally a verifier that suggests a new short hand
43  * for the credentials.
44  *
45  */
46 
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <unistd.h>
50 #include <string.h>
51 
52 #include <sys/param.h>
53 #include <rpc/types.h>
54 #include <rpc/xdr.h>
55 #include <rpc/auth.h>
56 #include <rpc/auth_unix.h>
57 
58 /*
59  * Unix authenticator operations vector
60  */
61 static void	authunix_nextverf();
62 static bool_t	authunix_marshal();
63 static bool_t	authunix_validate();
64 static bool_t	authunix_refresh();
65 static void	authunix_destroy();
66 
67 static struct auth_ops auth_unix_ops = {
68 	authunix_nextverf,
69 	authunix_marshal,
70 	authunix_validate,
71 	authunix_refresh,
72 	authunix_destroy
73 };
74 
75 /*
76  * This struct is pointed to by the ah_private field of an auth_handle.
77  */
78 struct audata {
79 	struct opaque_auth	au_origcred;	/* original credentials */
80 	struct opaque_auth	au_shcred;	/* short hand cred */
81 	u_long			au_shfaults;	/* short hand cache faults */
82 	char			au_marshed[MAX_AUTH_BYTES];
83 	u_int			au_mpos;	/* xdr pos at end of marshed */
84 };
85 #define	AUTH_PRIVATE(auth)	((struct audata *)auth->ah_private)
86 
87 static void marshal_new_auth();
88 
89 /*
90  * This goop is here because some servers refuse to accept a
91  * credential with more than some number (usually 8) supplementary
92  * groups.  Blargh!
93  */
94 static int authunix_maxgrouplist = 0;
95 
96 void
97 set_rpc_maxgrouplist(int num)
98 {
99 	authunix_maxgrouplist = num;
100 }
101 
102 /*
103  * Create a unix style authenticator.
104  * Returns an auth handle with the given stuff in it.
105  */
106 AUTH *
107 authunix_create(char *machname, int uid, int gid, int len, int *aup_gids)
108 {
109 	struct authunix_parms aup;
110 	char mymem[MAX_AUTH_BYTES];
111 	struct timeval now;
112 	XDR xdrs;
113 	AUTH *auth;
114 	struct audata *au;
115 
116 	/*
117 	 * Allocate and set up auth handle
118 	 */
119 	auth = (AUTH *)mem_alloc(sizeof(*auth));
120 #ifndef _KERNEL
121 	if (auth == NULL) {
122 		fprintf(stderr, "authunix_create: out of memory\n");
123 		return (NULL);
124 	}
125 #endif
126 	au = (struct audata *)mem_alloc(sizeof(*au));
127 #ifndef _KERNEL
128 	if (au == NULL) {
129 		fprintf(stderr, "authunix_create: out of memory\n");
130 		return (NULL);
131 	}
132 #endif
133 	auth->ah_ops = &auth_unix_ops;
134 	auth->ah_private = (caddr_t)au;
135 	auth->ah_verf = au->au_shcred = _null_auth;
136 	au->au_shfaults = 0;
137 
138 	/*
139 	 * fill in param struct from the given params
140 	 */
141 	gettimeofday(&now,  (struct timezone *)0);
142 	aup.aup_time = now.tv_sec;
143 	aup.aup_machname = machname;
144 	aup.aup_uid = uid;
145 	aup.aup_gid = gid;
146 	/* GW: continuation of max group list hack */
147 	if(authunix_maxgrouplist != 0) {
148 		aup.aup_len = ((len < authunix_maxgrouplist) ? len
149 			       : authunix_maxgrouplist);
150 	} else {
151 		aup.aup_len = (u_int)len;
152 	}
153 	aup.aup_gids = aup_gids;
154 
155 	/*
156 	 * Serialize the parameters into origcred
157 	 */
158 	xdrmem_create(&xdrs, mymem, MAX_AUTH_BYTES, XDR_ENCODE);
159 	if (! xdr_authunix_parms(&xdrs, &aup))
160 		abort();
161 	au->au_origcred.oa_length = len = XDR_GETPOS(&xdrs);
162 	au->au_origcred.oa_flavor = AUTH_UNIX;
163 #ifdef _KERNEL
164 	au->au_origcred.oa_base = mem_alloc((u_int) len);
165 #else
166 	if ((au->au_origcred.oa_base = mem_alloc((u_int) len)) == NULL) {
167 		fprintf(stderr, "authunix_create: out of memory\n");
168 		return (NULL);
169 	}
170 #endif
171 	memcpy(au->au_origcred.oa_base, mymem, (u_int)len);
172 
173 	/*
174 	 * set auth handle to reflect new cred.
175 	 */
176 	auth->ah_cred = au->au_origcred;
177 	marshal_new_auth(auth);
178 	return (auth);
179 }
180 
181 /*
182  * Returns an auth handle with parameters determined by doing lots of
183  * syscalls.
184  */
185 AUTH *
186 authunix_create_default(void)
187 {
188 	int len;
189 	char machname[MAX_MACHINE_NAME + 1];
190 	int uid;
191 	int gid;
192 	int gids[NGRPS];
193 	int i;
194 	gid_t real_gids[NGROUPS];
195 
196 	if (gethostname(machname, MAX_MACHINE_NAME) == -1)
197 		abort();
198 	machname[MAX_MACHINE_NAME] = 0;
199 	uid = (int)geteuid();
200 	gid = (int)getegid();
201 	if ((len = getgroups(NGROUPS, real_gids)) < 0)
202 		abort();
203 	if(len > NGRPS) len = NGRPS; /* GW: turn `gid_t's into `int's */
204 	for(i = 0; i < len; i++) {
205 		gids[i] = (int)real_gids[i];
206 	}
207 	return (authunix_create(machname, uid, gid, len, gids));
208 }
209 
210 /*
211  * authunix operations
212  */
213 
214 static void
215 authunix_nextverf(AUTH *auth)
216 {
217 	/* no action necessary */
218 }
219 
220 static bool_t
221 authunix_marshal(AUTH *auth, XDR *xdrs)
222 {
223 	struct audata *au = AUTH_PRIVATE(auth);
224 
225 	return (XDR_PUTBYTES(xdrs, au->au_marshed, au->au_mpos));
226 }
227 
228 static bool_t
229 authunix_validate(AUTH *auth, struct opaque_auth verf)
230 {
231 	struct audata *au;
232 	XDR xdrs;
233 
234 	if (verf.oa_flavor == AUTH_SHORT) {
235 		au = AUTH_PRIVATE(auth);
236 		xdrmem_create(&xdrs, verf.oa_base, verf.oa_length, XDR_DECODE);
237 
238 		if (au->au_shcred.oa_base != NULL) {
239 			mem_free(au->au_shcred.oa_base,
240 			    au->au_shcred.oa_length);
241 			au->au_shcred.oa_base = NULL;
242 		}
243 		if (xdr_opaque_auth(&xdrs, &au->au_shcred)) {
244 			auth->ah_cred = au->au_shcred;
245 		} else {
246 			xdrs.x_op = XDR_FREE;
247 			xdr_opaque_auth(&xdrs, &au->au_shcred);
248 			au->au_shcred.oa_base = NULL;
249 			auth->ah_cred = au->au_origcred;
250 		}
251 		marshal_new_auth(auth);
252 	}
253 	return (TRUE);
254 }
255 
256 static bool_t
257 authunix_refresh(AUTH *auth)
258 {
259 	struct audata *au = AUTH_PRIVATE(auth);
260 	struct authunix_parms aup;
261 	struct timeval now;
262 	XDR xdrs;
263 	int stat;
264 
265 	if (auth->ah_cred.oa_base == au->au_origcred.oa_base) {
266 		/* there is no hope.  Punt */
267 		return (FALSE);
268 	}
269 	au->au_shfaults ++;
270 
271 	/* first deserialize the creds back into a struct authunix_parms */
272 	aup.aup_machname = NULL;
273 	aup.aup_gids = (int *)NULL;
274 	xdrmem_create(&xdrs, au->au_origcred.oa_base,
275 	    au->au_origcred.oa_length, XDR_DECODE);
276 	stat = xdr_authunix_parms(&xdrs, &aup);
277 	if (! stat)
278 		goto done;
279 
280 	/* update the time and serialize in place */
281 	gettimeofday(&now, (struct timezone *)0);
282 	aup.aup_time = now.tv_sec;
283 	xdrs.x_op = XDR_ENCODE;
284 	XDR_SETPOS(&xdrs, 0);
285 	stat = xdr_authunix_parms(&xdrs, &aup);
286 	if (! stat)
287 		goto done;
288 	auth->ah_cred = au->au_origcred;
289 	marshal_new_auth(auth);
290 done:
291 	/* free the struct authunix_parms created by deserializing */
292 	xdrs.x_op = XDR_FREE;
293 	xdr_authunix_parms(&xdrs, &aup);
294 	XDR_DESTROY(&xdrs);
295 	return (stat);
296 }
297 
298 static void
299 authunix_destroy(AUTH *auth)
300 {
301 	struct audata *au = AUTH_PRIVATE(auth);
302 
303 	mem_free(au->au_origcred.oa_base, au->au_origcred.oa_length);
304 
305 	if (au->au_shcred.oa_base != NULL)
306 		mem_free(au->au_shcred.oa_base, au->au_shcred.oa_length);
307 
308 	mem_free(auth->ah_private, sizeof(struct audata));
309 
310 	if (auth->ah_verf.oa_base != NULL)
311 		mem_free(auth->ah_verf.oa_base, auth->ah_verf.oa_length);
312 
313 	mem_free((caddr_t)auth, sizeof(*auth));
314 }
315 
316 /*
317  * Marshals (pre-serializes) an auth struct.
318  * sets private data, au_marshed and au_mpos
319  */
320 static void
321 marshal_new_auth(AUTH *auth)
322 {
323 	XDR		xdr_stream;
324 	XDR	*xdrs = &xdr_stream;
325 	struct audata *au = AUTH_PRIVATE(auth);
326 
327 	xdrmem_create(xdrs, au->au_marshed, MAX_AUTH_BYTES, XDR_ENCODE);
328 	if ((! xdr_opaque_auth(xdrs, &(auth->ah_cred))) ||
329 	    (! xdr_opaque_auth(xdrs, &(auth->ah_verf)))) {
330 		perror("auth_none.c - Fatal marshalling problem");
331 	} else {
332 		au->au_mpos = XDR_GETPOS(xdrs);
333 	}
334 	XDR_DESTROY(xdrs);
335 }
336