xref: /csrg-svn/lib/librpc/rpc/auth_unix.c (revision 52990)
145073Smckusick /* @(#)auth_unix.c	2.2 88/08/01 4.0 RPCSRC */
245073Smckusick /*
345073Smckusick  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
445073Smckusick  * unrestricted use provided that this legend is included on all tape
545073Smckusick  * media and as a part of the software program in whole or part.  Users
645073Smckusick  * may copy or modify Sun RPC without charge, but are not authorized
745073Smckusick  * to license or distribute it to anyone else except as part of a product or
845073Smckusick  * program developed by the user.
945073Smckusick  *
1045073Smckusick  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
1145073Smckusick  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
1245073Smckusick  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
1345073Smckusick  *
1445073Smckusick  * Sun RPC is provided with no support and without any obligation on the
1545073Smckusick  * part of Sun Microsystems, Inc. to assist in its use, correction,
1645073Smckusick  * modification or enhancement.
1745073Smckusick  *
1845073Smckusick  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
1945073Smckusick  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
2045073Smckusick  * OR ANY PART THEREOF.
2145073Smckusick  *
2245073Smckusick  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
2345073Smckusick  * or profits or other special, indirect and consequential damages, even if
2445073Smckusick  * Sun has been advised of the possibility of such damages.
2545073Smckusick  *
2645073Smckusick  * Sun Microsystems, Inc.
2745073Smckusick  * 2550 Garcia Avenue
2845073Smckusick  * Mountain View, California  94043
2945073Smckusick  */
3045073Smckusick #if !defined(lint) && defined(SCCSIDS)
3145073Smckusick static char sccsid[] = "@(#)auth_unix.c 1.19 87/08/11 Copyr 1984 Sun Micro";
3245073Smckusick #endif
3345073Smckusick 
3445073Smckusick /*
3545073Smckusick  * auth_unix.c, Implements UNIX style authentication parameters.
3645073Smckusick  *
3745073Smckusick  * Copyright (C) 1984, Sun Microsystems, Inc.
3845073Smckusick  *
3945073Smckusick  * The system is very weak.  The client uses no encryption for it's
4045073Smckusick  * credentials and only sends null verifiers.  The server sends backs
4145073Smckusick  * null verifiers or optionally a verifier that suggests a new short hand
4245073Smckusick  * for the credentials.
4345073Smckusick  *
4445073Smckusick  */
4545073Smckusick 
4645073Smckusick #include <stdio.h>
4745073Smckusick 
4845073Smckusick #include <rpc/types.h>
4945073Smckusick #include <rpc/xdr.h>
5045073Smckusick #include <rpc/auth.h>
5145073Smckusick #include <rpc/auth_unix.h>
5245073Smckusick 
5345073Smckusick /*
5445073Smckusick  * Unix authenticator operations vector
5545073Smckusick  */
5645073Smckusick static void	authunix_nextverf();
5745073Smckusick static bool_t	authunix_marshal();
5845073Smckusick static bool_t	authunix_validate();
5945073Smckusick static bool_t	authunix_refresh();
6045073Smckusick static void	authunix_destroy();
6145073Smckusick 
6245073Smckusick static struct auth_ops auth_unix_ops = {
6345073Smckusick 	authunix_nextverf,
6445073Smckusick 	authunix_marshal,
6545073Smckusick 	authunix_validate,
6645073Smckusick 	authunix_refresh,
6745073Smckusick 	authunix_destroy
6845073Smckusick };
6945073Smckusick 
7045073Smckusick /*
7145073Smckusick  * This struct is pointed to by the ah_private field of an auth_handle.
7245073Smckusick  */
7345073Smckusick struct audata {
7445073Smckusick 	struct opaque_auth	au_origcred;	/* original credentials */
7545073Smckusick 	struct opaque_auth	au_shcred;	/* short hand cred */
7645073Smckusick 	u_long			au_shfaults;	/* short hand cache faults */
7745073Smckusick 	char			au_marshed[MAX_AUTH_BYTES];
7845073Smckusick 	u_int			au_mpos;	/* xdr pos at end of marshed */
7945073Smckusick };
8045073Smckusick #define	AUTH_PRIVATE(auth)	((struct audata *)auth->ah_private)
8145073Smckusick 
8245073Smckusick static bool_t marshal_new_auth();
8345073Smckusick 
8445073Smckusick 
8545073Smckusick /*
8645073Smckusick  * Create a unix style authenticator.
8745073Smckusick  * Returns an auth handle with the given stuff in it.
8845073Smckusick  */
8945073Smckusick AUTH *
authunix_create(machname,uid,gid,len,aup_gids)9045073Smckusick authunix_create(machname, uid, gid, len, aup_gids)
9145073Smckusick 	char *machname;
9245073Smckusick 	int uid;
9345073Smckusick 	int gid;
9445073Smckusick 	register int len;
9545073Smckusick 	int *aup_gids;
9645073Smckusick {
9745073Smckusick 	struct authunix_parms aup;
9845073Smckusick 	char mymem[MAX_AUTH_BYTES];
9945073Smckusick 	struct timeval now;
10045073Smckusick 	XDR xdrs;
10145073Smckusick 	register AUTH *auth;
10245073Smckusick 	register struct audata *au;
10345073Smckusick 
10445073Smckusick 	/*
10545073Smckusick 	 * Allocate and set up auth handle
10645073Smckusick 	 */
10745073Smckusick 	auth = (AUTH *)mem_alloc(sizeof(*auth));
10845073Smckusick #ifndef KERNEL
10945073Smckusick 	if (auth == NULL) {
11045073Smckusick 		(void)fprintf(stderr, "authunix_create: out of memory\n");
11145073Smckusick 		return (NULL);
11245073Smckusick 	}
11345073Smckusick #endif
11445073Smckusick 	au = (struct audata *)mem_alloc(sizeof(*au));
11545073Smckusick #ifndef KERNEL
11645073Smckusick 	if (au == NULL) {
11745073Smckusick 		(void)fprintf(stderr, "authunix_create: out of memory\n");
11845073Smckusick 		return (NULL);
11945073Smckusick 	}
12045073Smckusick #endif
12145073Smckusick 	auth->ah_ops = &auth_unix_ops;
12245073Smckusick 	auth->ah_private = (caddr_t)au;
12345073Smckusick 	auth->ah_verf = au->au_shcred = _null_auth;
12445073Smckusick 	au->au_shfaults = 0;
12545073Smckusick 
12645073Smckusick 	/*
12745073Smckusick 	 * fill in param struct from the given params
12845073Smckusick 	 */
12945073Smckusick 	(void)gettimeofday(&now,  (struct timezone *)0);
13045073Smckusick 	aup.aup_time = now.tv_sec;
13145073Smckusick 	aup.aup_machname = machname;
13245073Smckusick 	aup.aup_uid = uid;
13345073Smckusick 	aup.aup_gid = gid;
13445073Smckusick 	aup.aup_len = (u_int)len;
13545073Smckusick 	aup.aup_gids = aup_gids;
13645073Smckusick 
13745073Smckusick 	/*
13845073Smckusick 	 * Serialize the parameters into origcred
13945073Smckusick 	 */
14045073Smckusick 	xdrmem_create(&xdrs, mymem, MAX_AUTH_BYTES, XDR_ENCODE);
14145073Smckusick 	if (! xdr_authunix_parms(&xdrs, &aup))
14245073Smckusick 		abort();
14345073Smckusick 	au->au_origcred.oa_length = len = XDR_GETPOS(&xdrs);
14445073Smckusick 	au->au_origcred.oa_flavor = AUTH_UNIX;
14545073Smckusick #ifdef KERNEL
14645073Smckusick 	au->au_origcred.oa_base = mem_alloc((u_int) len);
14745073Smckusick #else
14845073Smckusick 	if ((au->au_origcred.oa_base = mem_alloc((u_int) len)) == NULL) {
14945073Smckusick 		(void)fprintf(stderr, "authunix_create: out of memory\n");
15045073Smckusick 		return (NULL);
15145073Smckusick 	}
15245073Smckusick #endif
15345073Smckusick 	bcopy(mymem, au->au_origcred.oa_base, (u_int)len);
15445073Smckusick 
15545073Smckusick 	/*
15645073Smckusick 	 * set auth handle to reflect new cred.
15745073Smckusick 	 */
15845073Smckusick 	auth->ah_cred = au->au_origcred;
15945073Smckusick 	marshal_new_auth(auth);
16045073Smckusick 	return (auth);
16145073Smckusick }
16245073Smckusick 
16345073Smckusick /*
164*52990Smckusick  * Some servers will refuse mounts if the group list is larger
165*52990Smckusick  * than it expects (like 8). This allows the application to set
166*52990Smckusick  * the maximum size of the group list that will be sent.
167*52990Smckusick  */
168*52990Smckusick 
169*52990Smckusick static maxgrplist = NGRPS;
170*52990Smckusick 
set_rpc_maxgrouplist(num)171*52990Smckusick set_rpc_maxgrouplist(num)
172*52990Smckusick 	int num;
173*52990Smckusick {
174*52990Smckusick 
175*52990Smckusick 	if (num < NGRPS)
176*52990Smckusick 		maxgrplist = num;
177*52990Smckusick }
178*52990Smckusick 
179*52990Smckusick /*
18045073Smckusick  * Returns an auth handle with parameters determined by doing lots of
18145073Smckusick  * syscalls.
18245073Smckusick  */
18345073Smckusick AUTH *
authunix_create_default()18445073Smckusick authunix_create_default()
18545073Smckusick {
18645073Smckusick 	register int len;
18745073Smckusick 	char machname[MAX_MACHINE_NAME + 1];
18845073Smckusick 	register int uid;
18945073Smckusick 	register int gid;
19045073Smckusick 	int gids[NGRPS];
19145073Smckusick 
19245073Smckusick 	if (gethostname(machname, MAX_MACHINE_NAME) == -1)
19345073Smckusick 		abort();
19445073Smckusick 	machname[MAX_MACHINE_NAME] = 0;
19545073Smckusick 	uid = geteuid();
19645073Smckusick 	gid = getegid();
19745073Smckusick 	if ((len = getgroups(NGRPS, gids)) < 0)
19845073Smckusick 		abort();
199*52990Smckusick 	if (len > maxgrplist)
200*52990Smckusick 		len = maxgrplist;
20145073Smckusick 	return (authunix_create(machname, uid, gid, len, gids));
20245073Smckusick }
20345073Smckusick 
20445073Smckusick /*
20545073Smckusick  * authunix operations
20645073Smckusick  */
20745073Smckusick 
20845073Smckusick static void
authunix_nextverf(auth)20945073Smckusick authunix_nextverf(auth)
21045073Smckusick 	AUTH *auth;
21145073Smckusick {
21245073Smckusick 	/* no action necessary */
21345073Smckusick }
21445073Smckusick 
21545073Smckusick static bool_t
authunix_marshal(auth,xdrs)21645073Smckusick authunix_marshal(auth, xdrs)
21745073Smckusick 	AUTH *auth;
21845073Smckusick 	XDR *xdrs;
21945073Smckusick {
22045073Smckusick 	register struct audata *au = AUTH_PRIVATE(auth);
22145073Smckusick 
22245073Smckusick 	return (XDR_PUTBYTES(xdrs, au->au_marshed, au->au_mpos));
22345073Smckusick }
22445073Smckusick 
22545073Smckusick static bool_t
authunix_validate(auth,verf)22645073Smckusick authunix_validate(auth, verf)
22745073Smckusick 	register AUTH *auth;
22845073Smckusick 	struct opaque_auth verf;
22945073Smckusick {
23045073Smckusick 	register struct audata *au;
23145073Smckusick 	XDR xdrs;
23245073Smckusick 
23345073Smckusick 	if (verf.oa_flavor == AUTH_SHORT) {
23445073Smckusick 		au = AUTH_PRIVATE(auth);
23545073Smckusick 		xdrmem_create(&xdrs, verf.oa_base, verf.oa_length, XDR_DECODE);
23645073Smckusick 
23745073Smckusick 		if (au->au_shcred.oa_base != NULL) {
23845073Smckusick 			mem_free(au->au_shcred.oa_base,
23945073Smckusick 			    au->au_shcred.oa_length);
24045073Smckusick 			au->au_shcred.oa_base = NULL;
24145073Smckusick 		}
24245073Smckusick 		if (xdr_opaque_auth(&xdrs, &au->au_shcred)) {
24345073Smckusick 			auth->ah_cred = au->au_shcred;
24445073Smckusick 		} else {
24545073Smckusick 			xdrs.x_op = XDR_FREE;
24645073Smckusick 			(void)xdr_opaque_auth(&xdrs, &au->au_shcred);
24745073Smckusick 			au->au_shcred.oa_base = NULL;
24845073Smckusick 			auth->ah_cred = au->au_origcred;
24945073Smckusick 		}
25045073Smckusick 		marshal_new_auth(auth);
25145073Smckusick 	}
25245073Smckusick 	return (TRUE);
25345073Smckusick }
25445073Smckusick 
25545073Smckusick static bool_t
authunix_refresh(auth)25645073Smckusick authunix_refresh(auth)
25745073Smckusick 	register AUTH *auth;
25845073Smckusick {
25945073Smckusick 	register struct audata *au = AUTH_PRIVATE(auth);
26045073Smckusick 	struct authunix_parms aup;
26145073Smckusick 	struct timeval now;
26245073Smckusick 	XDR xdrs;
26345073Smckusick 	register int stat;
26445073Smckusick 
26545073Smckusick 	if (auth->ah_cred.oa_base == au->au_origcred.oa_base) {
26645073Smckusick 		/* there is no hope.  Punt */
26745073Smckusick 		return (FALSE);
26845073Smckusick 	}
26945073Smckusick 	au->au_shfaults ++;
27045073Smckusick 
27145073Smckusick 	/* first deserialize the creds back into a struct authunix_parms */
27245073Smckusick 	aup.aup_machname = NULL;
27345073Smckusick 	aup.aup_gids = (int *)NULL;
27445073Smckusick 	xdrmem_create(&xdrs, au->au_origcred.oa_base,
27545073Smckusick 	    au->au_origcred.oa_length, XDR_DECODE);
27645073Smckusick 	stat = xdr_authunix_parms(&xdrs, &aup);
27745073Smckusick 	if (! stat)
27845073Smckusick 		goto done;
27945073Smckusick 
28045073Smckusick 	/* update the time and serialize in place */
28145073Smckusick 	(void)gettimeofday(&now, (struct timezone *)0);
28245073Smckusick 	aup.aup_time = now.tv_sec;
28345073Smckusick 	xdrs.x_op = XDR_ENCODE;
28445073Smckusick 	XDR_SETPOS(&xdrs, 0);
28545073Smckusick 	stat = xdr_authunix_parms(&xdrs, &aup);
28645073Smckusick 	if (! stat)
28745073Smckusick 		goto done;
28845073Smckusick 	auth->ah_cred = au->au_origcred;
28945073Smckusick 	marshal_new_auth(auth);
29045073Smckusick done:
29145073Smckusick 	/* free the struct authunix_parms created by deserializing */
29245073Smckusick 	xdrs.x_op = XDR_FREE;
29345073Smckusick 	(void)xdr_authunix_parms(&xdrs, &aup);
29445073Smckusick 	XDR_DESTROY(&xdrs);
29545073Smckusick 	return (stat);
29645073Smckusick }
29745073Smckusick 
29845073Smckusick static void
authunix_destroy(auth)29945073Smckusick authunix_destroy(auth)
30045073Smckusick 	register AUTH *auth;
30145073Smckusick {
30245073Smckusick 	register struct audata *au = AUTH_PRIVATE(auth);
30345073Smckusick 
30445073Smckusick 	mem_free(au->au_origcred.oa_base, au->au_origcred.oa_length);
30545073Smckusick 
30645073Smckusick 	if (au->au_shcred.oa_base != NULL)
30745073Smckusick 		mem_free(au->au_shcred.oa_base, au->au_shcred.oa_length);
30845073Smckusick 
30945073Smckusick 	mem_free(auth->ah_private, sizeof(struct audata));
31045073Smckusick 
31145073Smckusick 	if (auth->ah_verf.oa_base != NULL)
31245073Smckusick 		mem_free(auth->ah_verf.oa_base, auth->ah_verf.oa_length);
31345073Smckusick 
31445073Smckusick 	mem_free((caddr_t)auth, sizeof(*auth));
31545073Smckusick }
31645073Smckusick 
31745073Smckusick /*
31845073Smckusick  * Marshals (pre-serializes) an auth struct.
31945073Smckusick  * sets private data, au_marshed and au_mpos
32045073Smckusick  */
32145073Smckusick static bool_t
marshal_new_auth(auth)32245073Smckusick marshal_new_auth(auth)
32345073Smckusick 	register AUTH *auth;
32445073Smckusick {
32545073Smckusick 	XDR		xdr_stream;
32645073Smckusick 	register XDR	*xdrs = &xdr_stream;
32745073Smckusick 	register struct audata *au = AUTH_PRIVATE(auth);
32845073Smckusick 
32945073Smckusick 	xdrmem_create(xdrs, au->au_marshed, MAX_AUTH_BYTES, XDR_ENCODE);
33045073Smckusick 	if ((! xdr_opaque_auth(xdrs, &(auth->ah_cred))) ||
33145073Smckusick 	    (! xdr_opaque_auth(xdrs, &(auth->ah_verf)))) {
33245073Smckusick 		perror("auth_none.c - Fatal marshalling problem");
33345073Smckusick 	} else {
33445073Smckusick 		au->au_mpos = XDR_GETPOS(xdrs);
33545073Smckusick 	}
33645073Smckusick 	XDR_DESTROY(xdrs);
33745073Smckusick }
338