xref: /openbsd-src/lib/libc/rpc/svc_auth_unix.c (revision 10632a79cc0d3f2730baa9fb977894fdccb4f611)
1*10632a79Sguenther /*	$OpenBSD: svc_auth_unix.c,v 1.13 2015/11/01 03:45:29 guenther Exp $ */
2cb7760d1Smillert 
3df930be7Sderaadt /*
4cb7760d1Smillert  * Copyright (c) 2010, Oracle America, Inc.
5df930be7Sderaadt  *
6cb7760d1Smillert  * Redistribution and use in source and binary forms, with or without
7cb7760d1Smillert  * modification, are permitted provided that the following conditions are
8cb7760d1Smillert  * met:
9df930be7Sderaadt  *
10cb7760d1Smillert  *     * Redistributions of source code must retain the above copyright
11cb7760d1Smillert  *       notice, this list of conditions and the following disclaimer.
12cb7760d1Smillert  *     * Redistributions in binary form must reproduce the above
13cb7760d1Smillert  *       copyright notice, this list of conditions and the following
14cb7760d1Smillert  *       disclaimer in the documentation and/or other materials
15cb7760d1Smillert  *       provided with the distribution.
16cb7760d1Smillert  *     * Neither the name of the "Oracle America, Inc." nor the names of its
17cb7760d1Smillert  *       contributors may be used to endorse or promote products derived
18cb7760d1Smillert  *       from this software without specific prior written permission.
19df930be7Sderaadt  *
20cb7760d1Smillert  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21cb7760d1Smillert  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22cb7760d1Smillert  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23cb7760d1Smillert  *   FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24cb7760d1Smillert  *   COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25cb7760d1Smillert  *   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26cb7760d1Smillert  *   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
27cb7760d1Smillert  *   GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28cb7760d1Smillert  *   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29cb7760d1Smillert  *   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30cb7760d1Smillert  *   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31cb7760d1Smillert  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32df930be7Sderaadt  */
33df930be7Sderaadt 
34df930be7Sderaadt /*
35df930be7Sderaadt  * svc_auth_unix.c
36df930be7Sderaadt  * Handles UNIX flavor authentication parameters on the service side of rpc.
37df930be7Sderaadt  * There are two svc auth implementations here: AUTH_UNIX and AUTH_SHORT.
38df930be7Sderaadt  * _svcauth_unix does full blown unix style uid,gid+gids auth,
39df930be7Sderaadt  * _svcauth_short uses a shorthand auth to index into a cache of longhand auths.
40df930be7Sderaadt  * Note: the shorthand has been gutted for efficiency.
41df930be7Sderaadt  */
42df930be7Sderaadt 
43df930be7Sderaadt #include <stdio.h>
44df930be7Sderaadt #include <rpc/rpc.h>
45f5c144acSetheisen #include <string.h>
46df930be7Sderaadt 
47df930be7Sderaadt /*
48df930be7Sderaadt  * Unix longhand authenticator
49df930be7Sderaadt  */
50df930be7Sderaadt enum auth_stat
_svcauth_unix(struct svc_req * rqst,struct rpc_msg * msg)51384ec30aSotto _svcauth_unix(struct svc_req *rqst, struct rpc_msg *msg)
52df930be7Sderaadt {
5341aa8645Sderaadt 	enum auth_stat stat;
54df930be7Sderaadt 	XDR xdrs;
5541aa8645Sderaadt 	struct authunix_parms *aup;
5641aa8645Sderaadt 	int32_t *buf;
57df930be7Sderaadt 	struct area {
58df930be7Sderaadt 		struct authunix_parms area_aup;
59df930be7Sderaadt 		char area_machname[MAX_MACHINE_NAME+1];
60df930be7Sderaadt 		int area_gids[NGRPS];
61df930be7Sderaadt 	} *area;
62df930be7Sderaadt 	u_int auth_len;
63e1ee68eeSderaadt 	u_int str_len, gid_len;
6441aa8645Sderaadt 	u_int i;
65df930be7Sderaadt 
66df930be7Sderaadt 	area = (struct area *) rqst->rq_clntcred;
67df930be7Sderaadt 	aup = &area->area_aup;
68df930be7Sderaadt 	aup->aup_machname = area->area_machname;
69df930be7Sderaadt 	aup->aup_gids = area->area_gids;
70df930be7Sderaadt 	auth_len = (u_int)msg->rm_call.cb_cred.oa_length;
71df930be7Sderaadt 	xdrmem_create(&xdrs, msg->rm_call.cb_cred.oa_base, auth_len,XDR_DECODE);
72df930be7Sderaadt 	buf = XDR_INLINE(&xdrs, auth_len);
73df930be7Sderaadt 	if (buf != NULL) {
74df930be7Sderaadt 		aup->aup_time = IXDR_GET_LONG(buf);
75df930be7Sderaadt 		str_len = IXDR_GET_U_LONG(buf);
76df930be7Sderaadt 		if (str_len > MAX_MACHINE_NAME) {
77df930be7Sderaadt 			stat = AUTH_BADCRED;
78df930be7Sderaadt 			goto done;
79df930be7Sderaadt 		}
8073b69cd9Sderaadt 		memcpy(aup->aup_machname, (caddr_t)buf, (u_int)str_len);
81df930be7Sderaadt 		aup->aup_machname[str_len] = 0;
82df930be7Sderaadt 		str_len = RNDUP(str_len);
83df930be7Sderaadt 		buf += str_len / sizeof (int32_t);
84df930be7Sderaadt 		aup->aup_uid = IXDR_GET_LONG(buf);
85df930be7Sderaadt 		aup->aup_gid = IXDR_GET_LONG(buf);
86df930be7Sderaadt 		gid_len = IXDR_GET_U_LONG(buf);
87df930be7Sderaadt 		if (gid_len > NGRPS) {
88df930be7Sderaadt 			stat = AUTH_BADCRED;
89df930be7Sderaadt 			goto done;
90df930be7Sderaadt 		}
91df930be7Sderaadt 		aup->aup_len = gid_len;
92df930be7Sderaadt 		for (i = 0; i < gid_len; i++) {
93df930be7Sderaadt 			aup->aup_gids[i] = IXDR_GET_LONG(buf);
94df930be7Sderaadt 		}
95df930be7Sderaadt 		/*
96df930be7Sderaadt 		 * five is the smallest unix credentials structure -
97df930be7Sderaadt 		 * timestamp, hostname len (0), uid, gid, and gids len (0).
98df930be7Sderaadt 		 */
99df930be7Sderaadt 		if ((5 + gid_len) * BYTES_PER_XDR_UNIT + str_len > auth_len) {
100df930be7Sderaadt 			stat = AUTH_BADCRED;
101df930be7Sderaadt 			goto done;
102df930be7Sderaadt 		}
103df930be7Sderaadt 	} else if (! xdr_authunix_parms(&xdrs, aup)) {
104df930be7Sderaadt 		xdrs.x_op = XDR_FREE;
105df930be7Sderaadt 		(void)xdr_authunix_parms(&xdrs, aup);
106df930be7Sderaadt 		stat = AUTH_BADCRED;
107df930be7Sderaadt 		goto done;
108df930be7Sderaadt 	}
109df930be7Sderaadt 	rqst->rq_xprt->xp_verf.oa_flavor = AUTH_NULL;
110df930be7Sderaadt 	rqst->rq_xprt->xp_verf.oa_length = 0;
111df930be7Sderaadt 	stat = AUTH_OK;
112df930be7Sderaadt done:
113df930be7Sderaadt 	XDR_DESTROY(&xdrs);
114df930be7Sderaadt 	return (stat);
115df930be7Sderaadt }
11685858ec2Sguenther DEF_STRONG(_svcauth_unix);
117df930be7Sderaadt 
118df930be7Sderaadt 
119df930be7Sderaadt /*
120df930be7Sderaadt  * Shorthand unix authenticator
121df930be7Sderaadt  * Looks up longhand in a cache.
122df930be7Sderaadt  */
123df930be7Sderaadt enum auth_stat
_svcauth_short(struct svc_req * rqst,struct rpc_msg * msg)124384ec30aSotto _svcauth_short(struct svc_req *rqst, struct rpc_msg *msg)
125df930be7Sderaadt {
126df930be7Sderaadt 	return (AUTH_REJECTEDCRED);
127df930be7Sderaadt }
12885858ec2Sguenther DEF_STRONG(_svcauth_short);
129