1261057Smav/*-
2261057Smav * Copyright (c) 2009, Sun Microsystems, Inc.
3261057Smav * All rights reserved.
48870Srgrimes *
5261057Smav * Redistribution and use in source and binary forms, with or without
6261057Smav * modification, are permitted provided that the following conditions are met:
7261057Smav * - Redistributions of source code must retain the above copyright notice,
8261057Smav *   this list of conditions and the following disclaimer.
9261057Smav * - Redistributions in binary form must reproduce the above copyright notice,
10261057Smav *   this list of conditions and the following disclaimer in the documentation
11261057Smav *   and/or other materials provided with the distribution.
12261057Smav * - Neither the name of Sun Microsystems, Inc. nor the names of its
13261057Smav *   contributors may be used to endorse or promote products derived
14261057Smav *   from this software without specific prior written permission.
15261057Smav *
16261057Smav * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17261057Smav * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18261057Smav * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19261057Smav * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20261057Smav * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21261057Smav * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22261057Smav * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23261057Smav * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24261057Smav * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25261057Smav * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26261057Smav * POSSIBILITY OF SUCH DAMAGE.
271901Swollman */
281901Swollman
291901Swollman#if defined(LIBC_SCCS) && !defined(lint)
30136581Sobrienstatic char *sccsid2 = "@(#)svc_auth_unix.c 1.28 88/02/08 Copyr 1984 Sun Micro";
3174462Salfredstatic char *sccsid = "@(#)svc_auth_unix.c	2.3 88/08/01 4.0 RPCSRC";
321901Swollman#endif
3392990Sobrien#include <sys/cdefs.h>
3492990Sobrien__FBSDID("$FreeBSD$");
351901Swollman
361901Swollman/*
371901Swollman * svc_auth_unix.c
381901Swollman * Handles UNIX flavor authentication parameters on the service side of rpc.
391901Swollman * There are two svc auth implementations here: AUTH_UNIX and AUTH_SHORT.
401901Swollman * _svcauth_unix does full blown unix style uid,gid+gids auth,
411901Swollman * _svcauth_short uses a shorthand auth to index into a cache of longhand auths.
421901Swollman * Note: the shorthand has been gutted for efficiency.
431901Swollman *
441901Swollman * Copyright (C) 1984, Sun Microsystems, Inc.
451901Swollman */
461901Swollman
4774462Salfred#include "namespace.h"
4874462Salfred#include <assert.h>
491901Swollman#include <stdio.h>
5011666Sphk#include <string.h>
5174462Salfred
521901Swollman#include <rpc/rpc.h>
5374462Salfred#include "un-namespace.h"
541901Swollman
551901Swollman/*
561901Swollman * Unix longhand authenticator
571901Swollman */
581901Swollmanenum auth_stat
591901Swollman_svcauth_unix(rqst, msg)
6074462Salfred	struct svc_req *rqst;
6174462Salfred	struct rpc_msg *msg;
621901Swollman{
6374462Salfred	enum auth_stat stat;
641901Swollman	XDR xdrs;
6574462Salfred	struct authunix_parms *aup;
6674462Salfred	int32_t *buf;
671901Swollman	struct area {
681901Swollman		struct authunix_parms area_aup;
691901Swollman		char area_machname[MAX_MACHINE_NAME+1];
70241309Spfg		u_int area_gids[NGRPS];
711901Swollman	} *area;
721901Swollman	u_int auth_len;
7374462Salfred	size_t str_len, gid_len;
7474462Salfred	u_int i;
751901Swollman
7674462Salfred	assert(rqst != NULL);
7774462Salfred	assert(msg != NULL);
7874462Salfred
791901Swollman	area = (struct area *) rqst->rq_clntcred;
801901Swollman	aup = &area->area_aup;
811901Swollman	aup->aup_machname = area->area_machname;
821901Swollman	aup->aup_gids = area->area_gids;
831901Swollman	auth_len = (u_int)msg->rm_call.cb_cred.oa_length;
841901Swollman	xdrmem_create(&xdrs, msg->rm_call.cb_cred.oa_base, auth_len,XDR_DECODE);
851901Swollman	buf = XDR_INLINE(&xdrs, auth_len);
861901Swollman	if (buf != NULL) {
8774462Salfred		aup->aup_time = IXDR_GET_INT32(buf);
8874462Salfred		str_len = (size_t)IXDR_GET_U_INT32(buf);
891901Swollman		if (str_len > MAX_MACHINE_NAME) {
901901Swollman			stat = AUTH_BADCRED;
911901Swollman			goto done;
921901Swollman		}
9374462Salfred		memmove(aup->aup_machname, buf, str_len);
941901Swollman		aup->aup_machname[str_len] = 0;
951901Swollman		str_len = RNDUP(str_len);
9621088Speter		buf += str_len / sizeof (int32_t);
9774462Salfred		aup->aup_uid = (int)IXDR_GET_INT32(buf);
9874462Salfred		aup->aup_gid = (int)IXDR_GET_INT32(buf);
9974462Salfred		gid_len = (size_t)IXDR_GET_U_INT32(buf);
1001901Swollman		if (gid_len > NGRPS) {
1011901Swollman			stat = AUTH_BADCRED;
1021901Swollman			goto done;
1031901Swollman		}
1041901Swollman		aup->aup_len = gid_len;
1051901Swollman		for (i = 0; i < gid_len; i++) {
10674462Salfred			aup->aup_gids[i] = (int)IXDR_GET_INT32(buf);
1071901Swollman		}
1081901Swollman		/*
1091901Swollman		 * five is the smallest unix credentials structure -
1101901Swollman		 * timestamp, hostname len (0), uid, gid, and gids len (0).
1111901Swollman		 */
1121901Swollman		if ((5 + gid_len) * BYTES_PER_XDR_UNIT + str_len > auth_len) {
11374462Salfred			(void) printf("bad auth_len gid %ld str %ld auth %u\n",
11474462Salfred			    (long)gid_len, (long)str_len, auth_len);
1151901Swollman			stat = AUTH_BADCRED;
1161901Swollman			goto done;
1171901Swollman		}
1181901Swollman	} else if (! xdr_authunix_parms(&xdrs, aup)) {
1191901Swollman		xdrs.x_op = XDR_FREE;
1201901Swollman		(void)xdr_authunix_parms(&xdrs, aup);
1211901Swollman		stat = AUTH_BADCRED;
1221901Swollman		goto done;
1231901Swollman	}
12426221Swpaul
12574462Salfred       /* get the verifier */
12626221Swpaul	if ((u_int)msg->rm_call.cb_verf.oa_length) {
12774462Salfred		rqst->rq_xprt->xp_verf.oa_flavor =
12826221Swpaul			msg->rm_call.cb_verf.oa_flavor;
12974462Salfred		rqst->rq_xprt->xp_verf.oa_base =
13026221Swpaul			msg->rm_call.cb_verf.oa_base;
13174462Salfred		rqst->rq_xprt->xp_verf.oa_length =
13226221Swpaul			msg->rm_call.cb_verf.oa_length;
13326221Swpaul	} else {
13426221Swpaul		rqst->rq_xprt->xp_verf.oa_flavor = AUTH_NULL;
13526221Swpaul		rqst->rq_xprt->xp_verf.oa_length = 0;
13626221Swpaul	}
1371901Swollman	stat = AUTH_OK;
1381901Swollmandone:
1391901Swollman	XDR_DESTROY(&xdrs);
1401901Swollman	return (stat);
1411901Swollman}
1421901Swollman
1431901Swollman
1441901Swollman/*
1451901Swollman * Shorthand unix authenticator
1461901Swollman * Looks up longhand in a cache.
1471901Swollman */
1481901Swollman/*ARGSUSED*/
14974462Salfredenum auth_stat
1501901Swollman_svcauth_short(rqst, msg)
1511901Swollman	struct svc_req *rqst;
1521901Swollman	struct rpc_msg *msg;
1531901Swollman{
1541901Swollman	return (AUTH_REJECTEDCRED);
1551901Swollman}
156