svc_auth_unix.c revision 177685
1208946Sae/*
2208946Sae * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
3208946Sae * unrestricted use provided that this legend is included on all tape
4208946Sae * media and as a part of the software program in whole or part.  Users
5208946Sae * may copy or modify Sun RPC without charge, but are not authorized
6208946Sae * to license or distribute it to anyone else except as part of a product or
7208946Sae * program developed by the user.
8208946Sae *
9208946Sae * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
10208946Sae * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
11208946Sae * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
12208946Sae *
13208946Sae * Sun RPC is provided with no support and without any obligation on the
14208946Sae * part of Sun Microsystems, Inc. to assist in its use, correction,
15208946Sae * modification or enhancement.
16208946Sae *
17208946Sae * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
18208946Sae * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
19208946Sae * OR ANY PART THEREOF.
20208946Sae *
21208946Sae * In no event will Sun Microsystems, Inc. be liable for any lost revenue
22208946Sae * or profits or other special, indirect and consequential damages, even if
23208946Sae * Sun has been advised of the possibility of such damages.
24208946Sae *
25208946Sae * Sun Microsystems, Inc.
26208946Sae * 2550 Garcia Avenue
27208946Sae * Mountain View, California  94043
28208946Sae */
29208946Sae
30208946Sae#if defined(LIBC_SCCS) && !defined(lint)
31208946Saestatic char *sccsid2 = "@(#)svc_auth_unix.c 1.28 88/02/08 Copyr 1984 Sun Micro";
32208946Saestatic char *sccsid = "@(#)svc_auth_unix.c	2.3 88/08/01 4.0 RPCSRC";
33208989Sae#endif
34208946Sae#include <sys/cdefs.h>
35208946Sae__FBSDID("$FreeBSD: head/sys/rpc/svc_auth_unix.c 177685 2008-03-28 09:50:32Z dfr $");
36208989Sae
37208946Sae/*
38208946Sae * svc_auth_unix.c
39208989Sae * Handles UNIX flavor authentication parameters on the service side of rpc.
40208989Sae * There are two svc auth implementations here: AUTH_UNIX and AUTH_SHORT.
41208946Sae * _svcauth_unix does full blown unix style uid,gid+gids auth,
42208946Sae * _svcauth_short uses a shorthand auth to index into a cache of longhand auths.
43208946Sae * Note: the shorthand has been gutted for efficiency.
44208946Sae *
45208946Sae * Copyright (C) 1984, Sun Microsystems, Inc.
46208946Sae */
47208946Sae
48208946Sae#include <sys/param.h>
49208946Sae#include <sys/lock.h>
50208946Sae#include <sys/mutex.h>
51208946Sae#include <sys/systm.h>
52208946Sae#include <sys/ucred.h>
53208946Sae
54208946Sae#include <rpc/rpc.h>
55208946Sae
56208946Sae#include <rpc/rpc_com.h>
57208946Sae
58208946Sae#define MAX_MACHINE_NAME	255
59208946Sae#define NGRPS			16
60208946Sae
61208946Sae/*
62208946Sae * Unix longhand authenticator
63208946Sae */
64208946Saeenum auth_stat
65208946Sae_svcauth_unix(struct svc_req *rqst, struct rpc_msg *msg)
66208946Sae{
67208946Sae	enum auth_stat stat;
68208946Sae	XDR xdrs;
69208946Sae	int32_t *buf;
70208946Sae	uint32_t time;
71208946Sae	struct xucred *xcr;
72208946Sae	u_int auth_len;
73208989Sae	size_t str_len, gid_len;
74208946Sae	u_int i;
75208946Sae
76208946Sae	xcr = rqst->rq_clntcred;
77208946Sae	auth_len = (u_int)msg->rm_call.cb_cred.oa_length;
78208946Sae	xdrmem_create(&xdrs, msg->rm_call.cb_cred.oa_base, auth_len,
79208946Sae	    XDR_DECODE);
80208946Sae	buf = XDR_INLINE(&xdrs, auth_len);
81208946Sae	if (buf != NULL) {
82208946Sae		time = IXDR_GET_UINT32(buf);
83208946Sae		str_len = (size_t)IXDR_GET_UINT32(buf);
84208946Sae		if (str_len > MAX_MACHINE_NAME) {
85208946Sae			stat = AUTH_BADCRED;
86208946Sae			goto done;
87208989Sae		}
88208946Sae		str_len = RNDUP(str_len);
89208946Sae		buf += str_len / sizeof (int32_t);
90208946Sae		xcr->cr_uid = IXDR_GET_UINT32(buf);
91208946Sae		xcr->cr_groups[0] = IXDR_GET_UINT32(buf);
92208946Sae		gid_len = (size_t)IXDR_GET_UINT32(buf);
93208946Sae		if (gid_len > NGRPS) {
94208946Sae			stat = AUTH_BADCRED;
95208946Sae			goto done;
96208946Sae		}
97208946Sae		for (i = 0; i < gid_len; i++) {
98208946Sae			if (i + 1 < NGROUPS)
99208946Sae				xcr->cr_groups[i + 1] = IXDR_GET_INT32(buf);
100208989Sae			else
101208946Sae				buf++;
102208946Sae		}
103208946Sae		if (gid_len + 1 > NGROUPS)
104208946Sae			xcr->cr_ngroups = NGROUPS;
105208946Sae		else
106208946Sae			xcr->cr_ngroups = gid_len + 1;
107208946Sae
108		/*
109		 * five is the smallest unix credentials structure -
110		 * timestamp, hostname len (0), uid, gid, and gids len (0).
111		 */
112		if ((5 + gid_len) * BYTES_PER_XDR_UNIT + str_len > auth_len) {
113			(void) printf("bad auth_len gid %ld str %ld auth %u\n",
114			    (long)gid_len, (long)str_len, auth_len);
115			stat = AUTH_BADCRED;
116			goto done;
117		}
118	} else if (! xdr_authunix_parms(&xdrs, &time, xcr)) {
119		stat = AUTH_BADCRED;
120		goto done;
121	}
122
123	rqst->rq_xprt->xp_verf.oa_flavor = AUTH_NULL;
124	rqst->rq_xprt->xp_verf.oa_length = 0;
125	stat = AUTH_OK;
126done:
127	XDR_DESTROY(&xdrs);
128
129	return (stat);
130}
131
132
133/*
134 * Shorthand unix authenticator
135 * Looks up longhand in a cache.
136 */
137/*ARGSUSED*/
138enum auth_stat
139_svcauth_short(rqst, msg)
140	struct svc_req *rqst;
141	struct rpc_msg *msg;
142{
143	return (AUTH_REJECTEDCRED);
144}
145