xref: /netbsd-src/lib/libc/rpc/svc_auth_unix.c (revision 47c0e0c312cd964546204bd0febab9a56fa11df9)
1*47c0e0c3Stron /*	$NetBSD: svc_auth_unix.c,v 1.21 2013/03/11 20:19:29 tron Exp $	*/
29e15c989Scgd 
363d7b677Scgd /*
4*47c0e0c3Stron  * Copyright (c) 2010, Oracle America, Inc.
563d7b677Scgd  *
6*47c0e0c3Stron  * Redistribution and use in source and binary forms, with or without
7*47c0e0c3Stron  * modification, are permitted provided that the following conditions are
8*47c0e0c3Stron  * met:
963d7b677Scgd  *
10*47c0e0c3Stron  *     * Redistributions of source code must retain the above copyright
11*47c0e0c3Stron  *       notice, this list of conditions and the following disclaimer.
12*47c0e0c3Stron  *     * Redistributions in binary form must reproduce the above
13*47c0e0c3Stron  *       copyright notice, this list of conditions and the following
14*47c0e0c3Stron  *       disclaimer in the documentation and/or other materials
15*47c0e0c3Stron  *       provided with the distribution.
16*47c0e0c3Stron  *     * Neither the name of the "Oracle America, Inc." nor the names of its
17*47c0e0c3Stron  *       contributors may be used to endorse or promote products derived
18*47c0e0c3Stron  *       from this software without specific prior written permission.
1963d7b677Scgd  *
20*47c0e0c3Stron  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21*47c0e0c3Stron  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22*47c0e0c3Stron  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23*47c0e0c3Stron  *   FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24*47c0e0c3Stron  *   COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25*47c0e0c3Stron  *   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26*47c0e0c3Stron  *   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
27*47c0e0c3Stron  *   GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28*47c0e0c3Stron  *   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29*47c0e0c3Stron  *   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30*47c0e0c3Stron  *   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31*47c0e0c3Stron  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3263d7b677Scgd  */
3363d7b677Scgd 
34c63c52b2Schristos #include <sys/cdefs.h>
3563d7b677Scgd #if defined(LIBC_SCCS) && !defined(lint)
36c63c52b2Schristos #if 0
37c63c52b2Schristos static char *sccsid = "@(#)svc_auth_unix.c 1.28 88/02/08 Copyr 1984 Sun Micro";
38c63c52b2Schristos static char *sccsid = "@(#)svc_auth_unix.c	2.3 88/08/01 4.0 RPCSRC";
39c63c52b2Schristos #else
40*47c0e0c3Stron __RCSID("$NetBSD: svc_auth_unix.c,v 1.21 2013/03/11 20:19:29 tron Exp $");
41c63c52b2Schristos #endif
4263d7b677Scgd #endif
4363d7b677Scgd 
4463d7b677Scgd /*
4563d7b677Scgd  * svc_auth_unix.c
4663d7b677Scgd  * Handles UNIX flavor authentication parameters on the service side of rpc.
4763d7b677Scgd  * There are two svc auth implementations here: AUTH_UNIX and AUTH_SHORT.
4863d7b677Scgd  * _svcauth_unix does full blown unix style uid,gid+gids auth,
4963d7b677Scgd  * _svcauth_short uses a shorthand auth to index into a cache of longhand auths.
5063d7b677Scgd  * Note: the shorthand has been gutted for efficiency.
5163d7b677Scgd  *
5263d7b677Scgd  * Copyright (C) 1984, Sun Microsystems, Inc.
5363d7b677Scgd  */
5463d7b677Scgd 
5543fa6fe3Sjtc #include "namespace.h"
5646e6c5e8Slukem 
57b48252f3Slukem #include <assert.h>
5863d7b677Scgd #include <stdio.h>
59ebd5bdf6Scgd #include <string.h>
6046e6c5e8Slukem 
6163d7b677Scgd #include <rpc/rpc.h>
6263d7b677Scgd 
6363d7b677Scgd /*
6463d7b677Scgd  * Unix longhand authenticator
6563d7b677Scgd  */
6663d7b677Scgd enum auth_stat
_svcauth_unix(struct svc_req * rqst,struct rpc_msg * msg)679e66e6d7Sabs _svcauth_unix(struct svc_req *rqst, struct rpc_msg *msg)
6863d7b677Scgd {
6946e6c5e8Slukem 	enum auth_stat stat;
7063d7b677Scgd 	XDR xdrs;
7146e6c5e8Slukem 	struct authunix_parms *aup;
7246e6c5e8Slukem 	int32_t *buf;
7363d7b677Scgd 	struct area {
7463d7b677Scgd 		struct authunix_parms area_aup;
7563d7b677Scgd 		char area_machname[MAX_MACHINE_NAME+1];
7663d7b677Scgd 		int area_gids[NGRPS];
7763d7b677Scgd 	} *area;
78ce147c1cSlukem 	u_int auth_len;
7908a7b345Sthorpej 	size_t str_len, gid_len, i;
8063d7b677Scgd 
81b48252f3Slukem 	_DIAGASSERT(rqst != NULL);
82b48252f3Slukem 	_DIAGASSERT(msg != NULL);
83b48252f3Slukem 
8463d7b677Scgd 	area = (struct area *) rqst->rq_clntcred;
8563d7b677Scgd 	aup = &area->area_aup;
8663d7b677Scgd 	aup->aup_machname = area->area_machname;
8763d7b677Scgd 	aup->aup_gids = area->area_gids;
88ce147c1cSlukem 	auth_len = (u_int)msg->rm_call.cb_cred.oa_length;
8963d7b677Scgd 	xdrmem_create(&xdrs, msg->rm_call.cb_cred.oa_base, auth_len,XDR_DECODE);
9063d7b677Scgd 	buf = XDR_INLINE(&xdrs, auth_len);
9163d7b677Scgd 	if (buf != NULL) {
927c5c8962Sfvdl 		aup->aup_time = IXDR_GET_INT32(buf);
937c5c8962Sfvdl 		str_len = (size_t)IXDR_GET_U_INT32(buf);
9463d7b677Scgd 		if (str_len > MAX_MACHINE_NAME) {
9563d7b677Scgd 			stat = AUTH_BADCRED;
9663d7b677Scgd 			goto done;
9763d7b677Scgd 		}
981325a26dSchristos 		memmove(aup->aup_machname, buf, str_len);
9963d7b677Scgd 		aup->aup_machname[str_len] = 0;
10063d7b677Scgd 		str_len = RNDUP(str_len);
1012e2a3a25Scgd 		buf += str_len / sizeof (int32_t);
1027c5c8962Sfvdl 		aup->aup_uid = (int)IXDR_GET_INT32(buf);
1037c5c8962Sfvdl 		aup->aup_gid = (int)IXDR_GET_INT32(buf);
1047c5c8962Sfvdl 		gid_len = (size_t)IXDR_GET_U_INT32(buf);
10563d7b677Scgd 		if (gid_len > NGRPS) {
10663d7b677Scgd 			stat = AUTH_BADCRED;
10763d7b677Scgd 			goto done;
10863d7b677Scgd 		}
109c5e820caSchristos 		_DIAGASSERT(__type_fit(u_int, gid_len));
110c5e820caSchristos 		aup->aup_len = (u_int)gid_len;
11163d7b677Scgd 		for (i = 0; i < gid_len; i++) {
1127c5c8962Sfvdl 			aup->aup_gids[i] = (int)IXDR_GET_INT32(buf);
11363d7b677Scgd 		}
11463d7b677Scgd 		/*
11563d7b677Scgd 		 * five is the smallest unix credentials structure -
11663d7b677Scgd 		 * timestamp, hostname len (0), uid, gid, and gids len (0).
11763d7b677Scgd 		 */
11863d7b677Scgd 		if ((5 + gid_len) * BYTES_PER_XDR_UNIT + str_len > auth_len) {
119f228fbeaSmrg 			(void) printf("bad auth_len gid %ld str %ld auth %u\n",
120f228fbeaSmrg 			    (long)gid_len, (long)str_len, auth_len);
12163d7b677Scgd 			stat = AUTH_BADCRED;
12263d7b677Scgd 			goto done;
12363d7b677Scgd 		}
12463d7b677Scgd 	} else if (! xdr_authunix_parms(&xdrs, aup)) {
12563d7b677Scgd 		xdrs.x_op = XDR_FREE;
12663d7b677Scgd 		(void)xdr_authunix_parms(&xdrs, aup);
12763d7b677Scgd 		stat = AUTH_BADCRED;
12863d7b677Scgd 		goto done;
12963d7b677Scgd 	}
13063d7b677Scgd 	rqst->rq_xprt->xp_verf.oa_flavor = AUTH_NULL;
13163d7b677Scgd 	rqst->rq_xprt->xp_verf.oa_length = 0;
13263d7b677Scgd 	stat = AUTH_OK;
13363d7b677Scgd done:
13463d7b677Scgd 	XDR_DESTROY(&xdrs);
13563d7b677Scgd 	return (stat);
13663d7b677Scgd }
13763d7b677Scgd 
13863d7b677Scgd 
13963d7b677Scgd /*
14063d7b677Scgd  * Shorthand unix authenticator
14163d7b677Scgd  * Looks up longhand in a cache.
14263d7b677Scgd  */
14363d7b677Scgd /*ARGSUSED*/
14463d7b677Scgd enum auth_stat
_svcauth_short(struct svc_req * rqst,struct rpc_msg * msg)1459e66e6d7Sabs _svcauth_short(struct svc_req *rqst, struct rpc_msg *msg)
14663d7b677Scgd {
14763d7b677Scgd 	return (AUTH_REJECTEDCRED);
14863d7b677Scgd }
149