svc_auth_unix.c revision 194498
1177633Sdfr/*
2177633Sdfr * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
3177633Sdfr * unrestricted use provided that this legend is included on all tape
4177633Sdfr * media and as a part of the software program in whole or part.  Users
5177633Sdfr * may copy or modify Sun RPC without charge, but are not authorized
6177633Sdfr * to license or distribute it to anyone else except as part of a product or
7177633Sdfr * program developed by the user.
8177633Sdfr *
9177633Sdfr * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
10177633Sdfr * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
11177633Sdfr * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
12177633Sdfr *
13177633Sdfr * Sun RPC is provided with no support and without any obligation on the
14177633Sdfr * part of Sun Microsystems, Inc. to assist in its use, correction,
15177633Sdfr * modification or enhancement.
16177633Sdfr *
17177633Sdfr * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
18177633Sdfr * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
19177633Sdfr * OR ANY PART THEREOF.
20177633Sdfr *
21177633Sdfr * In no event will Sun Microsystems, Inc. be liable for any lost revenue
22177633Sdfr * or profits or other special, indirect and consequential damages, even if
23177633Sdfr * Sun has been advised of the possibility of such damages.
24177633Sdfr *
25177633Sdfr * Sun Microsystems, Inc.
26177633Sdfr * 2550 Garcia Avenue
27177633Sdfr * Mountain View, California  94043
28177633Sdfr */
29177633Sdfr
30177633Sdfr#if defined(LIBC_SCCS) && !defined(lint)
31177633Sdfrstatic char *sccsid2 = "@(#)svc_auth_unix.c 1.28 88/02/08 Copyr 1984 Sun Micro";
32177633Sdfrstatic char *sccsid = "@(#)svc_auth_unix.c	2.3 88/08/01 4.0 RPCSRC";
33177633Sdfr#endif
34177633Sdfr#include <sys/cdefs.h>
35177633Sdfr__FBSDID("$FreeBSD: head/sys/rpc/svc_auth_unix.c 194498 2009-06-19 17:10:35Z brooks $");
36177633Sdfr
37177633Sdfr/*
38177633Sdfr * svc_auth_unix.c
39177633Sdfr * Handles UNIX flavor authentication parameters on the service side of rpc.
40177633Sdfr * There are two svc auth implementations here: AUTH_UNIX and AUTH_SHORT.
41177633Sdfr * _svcauth_unix does full blown unix style uid,gid+gids auth,
42177633Sdfr * _svcauth_short uses a shorthand auth to index into a cache of longhand auths.
43177633Sdfr * Note: the shorthand has been gutted for efficiency.
44177633Sdfr *
45177633Sdfr * Copyright (C) 1984, Sun Microsystems, Inc.
46177633Sdfr */
47177633Sdfr
48177633Sdfr#include <sys/param.h>
49177633Sdfr#include <sys/lock.h>
50177633Sdfr#include <sys/mutex.h>
51177633Sdfr#include <sys/systm.h>
52177633Sdfr#include <sys/ucred.h>
53177633Sdfr
54177633Sdfr#include <rpc/rpc.h>
55177633Sdfr
56177685Sdfr#include <rpc/rpc_com.h>
57177633Sdfr
58177633Sdfr#define MAX_MACHINE_NAME	255
59177633Sdfr#define NGRPS			16
60177633Sdfr
61177633Sdfr/*
62177633Sdfr * Unix longhand authenticator
63177633Sdfr */
64177633Sdfrenum auth_stat
65177633Sdfr_svcauth_unix(struct svc_req *rqst, struct rpc_msg *msg)
66177633Sdfr{
67177633Sdfr	enum auth_stat stat;
68177633Sdfr	XDR xdrs;
69177633Sdfr	int32_t *buf;
70177633Sdfr	uint32_t time;
71177633Sdfr	struct xucred *xcr;
72177633Sdfr	u_int auth_len;
73177633Sdfr	size_t str_len, gid_len;
74177633Sdfr	u_int i;
75177633Sdfr
76177633Sdfr	xcr = rqst->rq_clntcred;
77177633Sdfr	auth_len = (u_int)msg->rm_call.cb_cred.oa_length;
78177633Sdfr	xdrmem_create(&xdrs, msg->rm_call.cb_cred.oa_base, auth_len,
79177633Sdfr	    XDR_DECODE);
80177633Sdfr	buf = XDR_INLINE(&xdrs, auth_len);
81177633Sdfr	if (buf != NULL) {
82177633Sdfr		time = IXDR_GET_UINT32(buf);
83177633Sdfr		str_len = (size_t)IXDR_GET_UINT32(buf);
84177633Sdfr		if (str_len > MAX_MACHINE_NAME) {
85177633Sdfr			stat = AUTH_BADCRED;
86177633Sdfr			goto done;
87177633Sdfr		}
88177633Sdfr		str_len = RNDUP(str_len);
89177633Sdfr		buf += str_len / sizeof (int32_t);
90177633Sdfr		xcr->cr_uid = IXDR_GET_UINT32(buf);
91177633Sdfr		xcr->cr_groups[0] = IXDR_GET_UINT32(buf);
92177633Sdfr		gid_len = (size_t)IXDR_GET_UINT32(buf);
93177633Sdfr		if (gid_len > NGRPS) {
94177633Sdfr			stat = AUTH_BADCRED;
95177633Sdfr			goto done;
96177633Sdfr		}
97177633Sdfr		for (i = 0; i < gid_len; i++) {
98194498Sbrooks			if (i + 1 < XU_NGROUPS)
99177633Sdfr				xcr->cr_groups[i + 1] = IXDR_GET_INT32(buf);
100177633Sdfr			else
101177633Sdfr				buf++;
102177633Sdfr		}
103194498Sbrooks		if (gid_len + 1 > XU_NGROUPS)
104194498Sbrooks			xcr->cr_ngroups = XU_NGROUPS;
105177633Sdfr		else
106177633Sdfr			xcr->cr_ngroups = gid_len + 1;
107177633Sdfr
108177633Sdfr		/*
109177633Sdfr		 * five is the smallest unix credentials structure -
110177633Sdfr		 * timestamp, hostname len (0), uid, gid, and gids len (0).
111177633Sdfr		 */
112177633Sdfr		if ((5 + gid_len) * BYTES_PER_XDR_UNIT + str_len > auth_len) {
113177633Sdfr			(void) printf("bad auth_len gid %ld str %ld auth %u\n",
114177633Sdfr			    (long)gid_len, (long)str_len, auth_len);
115177633Sdfr			stat = AUTH_BADCRED;
116177633Sdfr			goto done;
117177633Sdfr		}
118177633Sdfr	} else if (! xdr_authunix_parms(&xdrs, &time, xcr)) {
119177633Sdfr		stat = AUTH_BADCRED;
120177633Sdfr		goto done;
121177633Sdfr	}
122177633Sdfr
123184588Sdfr	rqst->rq_verf = _null_auth;
124177633Sdfr	stat = AUTH_OK;
125177633Sdfrdone:
126177633Sdfr	XDR_DESTROY(&xdrs);
127177633Sdfr
128177633Sdfr	return (stat);
129177633Sdfr}
130177633Sdfr
131177633Sdfr
132177633Sdfr/*
133177633Sdfr * Shorthand unix authenticator
134177633Sdfr * Looks up longhand in a cache.
135177633Sdfr */
136177633Sdfr/*ARGSUSED*/
137177633Sdfrenum auth_stat
138177633Sdfr_svcauth_short(rqst, msg)
139177633Sdfr	struct svc_req *rqst;
140177633Sdfr	struct rpc_msg *msg;
141177633Sdfr{
142177633Sdfr	return (AUTH_REJECTEDCRED);
143177633Sdfr}
144