xref: /openbsd-src/lib/libc/rpc/auth_unix.c (revision daf88648c0e349d5c02e1504293082072c981640)
1 /*	$OpenBSD: auth_unix.c,v 1.20 2006/11/10 17:29:31 grunk 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 /*
165  * Some servers will refuse mounts if the group list is larger
166  * than it expects (like 8). This allows the application to set
167  * the maximum size of the group list that will be sent.
168  */
169 static int maxgrplist = NGRPS;
170 
171 void
172 set_rpc_maxgrouplist(int num)
173 {
174 	if (num < NGRPS)
175 		maxgrplist = num;
176 }
177 
178 /*
179  * Returns an auth handle with parameters determined by doing lots of
180  * syscalls.
181  */
182 AUTH *
183 authunix_create_default(void)
184 {
185 	int len, i;
186 	char machname[MAX_MACHINE_NAME + 1];
187 	uid_t uid;
188 	gid_t gid;
189 	gid_t gids[NGRPS];
190 	int gids2[NGRPS];
191 
192 	if (gethostname(machname, sizeof machname) == -1)
193 		return (NULL);
194 	machname[MAX_MACHINE_NAME] = 0;
195 	uid = geteuid();
196 	gid = getegid();
197 	if ((len = getgroups(NGRPS, gids)) < 0)
198 		return (NULL);
199 	if (len > maxgrplist)
200 		len = maxgrplist;
201 	for (i = 0; i < len; i++)
202 		gids2[i] = gids[i];
203 	return (authunix_create(machname, uid, gid, len, gids2));
204 }
205 
206 /*
207  * authunix operations
208  */
209 /* ARGSUSED */
210 static void
211 authunix_nextverf(AUTH *auth)
212 {
213 	/* no action necessary */
214 }
215 
216 static bool_t
217 authunix_marshal(AUTH *auth, XDR *xdrs)
218 {
219 	struct audata *au = AUTH_PRIVATE(auth);
220 
221 	return (XDR_PUTBYTES(xdrs, au->au_marshed, au->au_mpos));
222 }
223 
224 static bool_t
225 authunix_validate(AUTH *auth, struct opaque_auth *verf)
226 {
227 	struct audata *au;
228 	XDR xdrs;
229 
230 	if (verf->oa_flavor == AUTH_SHORT) {
231 		au = AUTH_PRIVATE(auth);
232 		xdrmem_create(&xdrs, verf->oa_base, verf->oa_length, XDR_DECODE);
233 
234 		if (au->au_shcred.oa_base != NULL) {
235 			mem_free(au->au_shcred.oa_base,
236 			    au->au_shcred.oa_length);
237 			au->au_shcred.oa_base = NULL;
238 		}
239 		if (xdr_opaque_auth(&xdrs, &au->au_shcred)) {
240 			auth->ah_cred = au->au_shcred;
241 		} else {
242 			xdrs.x_op = XDR_FREE;
243 			(void)xdr_opaque_auth(&xdrs, &au->au_shcred);
244 			au->au_shcred.oa_base = NULL;
245 			auth->ah_cred = au->au_origcred;
246 		}
247 		marshal_new_auth(auth);
248 	}
249 	return (TRUE);
250 }
251 
252 static bool_t
253 authunix_refresh(AUTH *auth)
254 {
255 	struct audata *au = AUTH_PRIVATE(auth);
256 	struct authunix_parms aup;
257 	struct timeval now;
258 	XDR xdrs;
259 	int stat;
260 
261 	if (auth->ah_cred.oa_base == au->au_origcred.oa_base) {
262 		/* there is no hope.  Punt */
263 		return (FALSE);
264 	}
265 	au->au_shfaults ++;
266 
267 	/* first deserialize the creds back into a struct authunix_parms */
268 	aup.aup_machname = NULL;
269 	aup.aup_gids = NULL;
270 	xdrmem_create(&xdrs, au->au_origcred.oa_base,
271 	    au->au_origcred.oa_length, XDR_DECODE);
272 	stat = xdr_authunix_parms(&xdrs, &aup);
273 	if (! stat)
274 		goto done;
275 
276 	/* update the time and serialize in place */
277 	(void)gettimeofday(&now, NULL);
278 	aup.aup_time = now.tv_sec;
279 	xdrs.x_op = XDR_ENCODE;
280 	XDR_SETPOS(&xdrs, 0);
281 	stat = xdr_authunix_parms(&xdrs, &aup);
282 	if (! stat)
283 		goto done;
284 	auth->ah_cred = au->au_origcred;
285 	marshal_new_auth(auth);
286 done:
287 	/* free the struct authunix_parms created by deserializing */
288 	xdrs.x_op = XDR_FREE;
289 	(void)xdr_authunix_parms(&xdrs, &aup);
290 	XDR_DESTROY(&xdrs);
291 	return (stat);
292 }
293 
294 static void
295 authunix_destroy(AUTH *auth)
296 {
297 	struct audata *au = AUTH_PRIVATE(auth);
298 
299 	mem_free(au->au_origcred.oa_base, au->au_origcred.oa_length);
300 
301 	if (au->au_shcred.oa_base != NULL)
302 		mem_free(au->au_shcred.oa_base, au->au_shcred.oa_length);
303 
304 	mem_free(auth->ah_private, sizeof(struct audata));
305 
306 	if (auth->ah_verf.oa_base != NULL)
307 		mem_free(auth->ah_verf.oa_base, auth->ah_verf.oa_length);
308 
309 	mem_free((caddr_t)auth, sizeof(*auth));
310 }
311 
312 /*
313  * Marshals (pre-serializes) an auth struct.
314  * sets private data, au_marshed and au_mpos
315  */
316 static void
317 marshal_new_auth(AUTH *auth)
318 {
319 	XDR		xdr_stream;
320 	XDR	*xdrs = &xdr_stream;
321 	struct audata *au = AUTH_PRIVATE(auth);
322 
323 	xdrmem_create(xdrs, au->au_marshed, MAX_AUTH_BYTES, XDR_ENCODE);
324 	if ((! xdr_opaque_auth(xdrs, &(auth->ah_cred))) ||
325 	    (! xdr_opaque_auth(xdrs, &(auth->ah_verf)))) {
326 		perror("auth_none.c - Fatal marshalling problem");
327 	} else {
328 		au->au_mpos = XDR_GETPOS(xdrs);
329 	}
330 	XDR_DESTROY(xdrs);
331 }
332