1177633Sdfr/*	$NetBSD: authunix_prot.c,v 1.12 2000/01/22 22:19:17 mycroft Exp $	*/
2177633Sdfr
3258578Shrs/*-
4258578Shrs * Copyright (c) 2009, Sun Microsystems, Inc.
5258578Shrs * All rights reserved.
6177633Sdfr *
7258578Shrs * Redistribution and use in source and binary forms, with or without
8258578Shrs * modification, are permitted provided that the following conditions are met:
9258578Shrs * - Redistributions of source code must retain the above copyright notice,
10258578Shrs *   this list of conditions and the following disclaimer.
11258578Shrs * - Redistributions in binary form must reproduce the above copyright notice,
12258578Shrs *   this list of conditions and the following disclaimer in the documentation
13258578Shrs *   and/or other materials provided with the distribution.
14258578Shrs * - Neither the name of Sun Microsystems, Inc. nor the names of its
15258578Shrs *   contributors may be used to endorse or promote products derived
16258578Shrs *   from this software without specific prior written permission.
17258578Shrs *
18258578Shrs * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19258578Shrs * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20258578Shrs * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21258578Shrs * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22258578Shrs * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23258578Shrs * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24258578Shrs * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25258578Shrs * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26258578Shrs * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27258578Shrs * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28258578Shrs * POSSIBILITY OF SUCH DAMAGE.
29177633Sdfr */
30177633Sdfr
31177633Sdfr#if defined(LIBC_SCCS) && !defined(lint)
32177633Sdfrstatic char *sccsid2 = "@(#)authunix_prot.c 1.15 87/08/11 Copyr 1984 Sun Micro";
33177633Sdfrstatic char *sccsid = "@(#)authunix_prot.c	2.1 88/07/29 4.0 RPCSRC";
34177633Sdfr#endif
35177633Sdfr#include <sys/cdefs.h>
36177633Sdfr__FBSDID("$FreeBSD$");
37177633Sdfr
38177633Sdfr/*
39177633Sdfr * authunix_prot.c
40177633Sdfr * XDR for UNIX style authentication parameters for RPC
41177633Sdfr *
42177633Sdfr * Copyright (C) 1984, Sun Microsystems, Inc.
43177633Sdfr */
44177633Sdfr
45177633Sdfr#include <sys/param.h>
46193066Sjamie#include <sys/jail.h>
47177633Sdfr#include <sys/kernel.h>
48177633Sdfr#include <sys/systm.h>
49177633Sdfr#include <sys/ucred.h>
50177633Sdfr
51177633Sdfr#include <rpc/types.h>
52177633Sdfr#include <rpc/xdr.h>
53177633Sdfr#include <rpc/auth.h>
54177633Sdfr
55177685Sdfr#include <rpc/rpc_com.h>
56177633Sdfr
57177633Sdfr/* gids compose part of a credential; there may not be more than 16 of them */
58177633Sdfr#define NGRPS 16
59177633Sdfr
60177633Sdfr/*
61177633Sdfr * XDR for unix authentication parameters.
62177633Sdfr */
63177633Sdfrbool_t
64177633Sdfrxdr_authunix_parms(XDR *xdrs, uint32_t *time, struct xucred *cred)
65177633Sdfr{
66177633Sdfr	uint32_t namelen;
67177633Sdfr	uint32_t ngroups, i;
68177633Sdfr	uint32_t junk;
69193066Sjamie	char hostbuf[MAXHOSTNAMELEN];
70177633Sdfr
71177633Sdfr	if (xdrs->x_op == XDR_ENCODE) {
72180025Sdfr		/*
73180025Sdfr		 * Restrict name length to 255 according to RFC 1057.
74180025Sdfr		 */
75193066Sjamie		getcredhostname(NULL, hostbuf, sizeof(hostbuf));
76193066Sjamie		namelen = strlen(hostbuf);
77180025Sdfr		if (namelen > 255)
78180025Sdfr			namelen = 255;
79177633Sdfr	} else {
80177633Sdfr		namelen = 0;
81177633Sdfr	}
82177633Sdfr	junk = 0;
83177633Sdfr
84177633Sdfr	if (!xdr_uint32_t(xdrs, time)
85177633Sdfr	    || !xdr_uint32_t(xdrs, &namelen))
86177633Sdfr		return (FALSE);
87177633Sdfr
88177633Sdfr	/*
89177633Sdfr	 * Ignore the hostname on decode.
90177633Sdfr	 */
91177633Sdfr	if (xdrs->x_op == XDR_ENCODE) {
92193066Sjamie		if (!xdr_opaque(xdrs, hostbuf, namelen))
93177633Sdfr			return (FALSE);
94177633Sdfr	} else {
95177633Sdfr		xdr_setpos(xdrs, xdr_getpos(xdrs) + RNDUP(namelen));
96177633Sdfr	}
97177633Sdfr
98177633Sdfr	if (!xdr_uint32_t(xdrs, &cred->cr_uid))
99177633Sdfr		return (FALSE);
100177633Sdfr	if (!xdr_uint32_t(xdrs, &cred->cr_groups[0]))
101177633Sdfr		return (FALSE);
102177633Sdfr
103177633Sdfr	if (xdrs->x_op == XDR_ENCODE) {
104177633Sdfr		ngroups = cred->cr_ngroups - 1;
105177633Sdfr		if (ngroups > NGRPS)
106177633Sdfr			ngroups = NGRPS;
107177633Sdfr	}
108177633Sdfr
109177633Sdfr	if (!xdr_uint32_t(xdrs, &ngroups))
110177633Sdfr		return (FALSE);
111177633Sdfr	for (i = 0; i < ngroups; i++) {
112202143Sbrooks		if (i + 1 < ngroups_max + 1) {
113177633Sdfr			if (!xdr_uint32_t(xdrs, &cred->cr_groups[i + 1]))
114177633Sdfr				return (FALSE);
115177633Sdfr		} else {
116177633Sdfr			if (!xdr_uint32_t(xdrs, &junk))
117177633Sdfr				return (FALSE);
118177633Sdfr		}
119177633Sdfr	}
120177633Sdfr
121177633Sdfr	if (xdrs->x_op == XDR_DECODE) {
122202143Sbrooks		if (ngroups + 1 > ngroups_max + 1)
123202143Sbrooks			cred->cr_ngroups = ngroups_max + 1;
124177633Sdfr		else
125177633Sdfr			cred->cr_ngroups = ngroups + 1;
126177633Sdfr	}
127177633Sdfr
128177633Sdfr	return (TRUE);
129177633Sdfr}
130