authunix_prot.c revision 202143
1177633Sdfr/*	$NetBSD: authunix_prot.c,v 1.12 2000/01/22 22:19:17 mycroft Exp $	*/
2177633Sdfr
3177633Sdfr/*
4177633Sdfr * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
5177633Sdfr * unrestricted use provided that this legend is included on all tape
6177633Sdfr * media and as a part of the software program in whole or part.  Users
7177633Sdfr * may copy or modify Sun RPC without charge, but are not authorized
8177633Sdfr * to license or distribute it to anyone else except as part of a product or
9177633Sdfr * program developed by the user.
10177633Sdfr *
11177633Sdfr * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
12177633Sdfr * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
13177633Sdfr * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
14177633Sdfr *
15177633Sdfr * Sun RPC is provided with no support and without any obligation on the
16177633Sdfr * part of Sun Microsystems, Inc. to assist in its use, correction,
17177633Sdfr * modification or enhancement.
18177633Sdfr *
19177633Sdfr * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
20177633Sdfr * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
21177633Sdfr * OR ANY PART THEREOF.
22177633Sdfr *
23177633Sdfr * In no event will Sun Microsystems, Inc. be liable for any lost revenue
24177633Sdfr * or profits or other special, indirect and consequential damages, even if
25177633Sdfr * Sun has been advised of the possibility of such damages.
26177633Sdfr *
27177633Sdfr * Sun Microsystems, Inc.
28177633Sdfr * 2550 Garcia Avenue
29177633Sdfr * Mountain View, California  94043
30177633Sdfr */
31177633Sdfr
32177633Sdfr#if defined(LIBC_SCCS) && !defined(lint)
33177633Sdfrstatic char *sccsid2 = "@(#)authunix_prot.c 1.15 87/08/11 Copyr 1984 Sun Micro";
34177633Sdfrstatic char *sccsid = "@(#)authunix_prot.c	2.1 88/07/29 4.0 RPCSRC";
35177633Sdfr#endif
36177633Sdfr#include <sys/cdefs.h>
37177633Sdfr__FBSDID("$FreeBSD: head/sys/rpc/authunix_prot.c 202143 2010-01-12 07:49:34Z brooks $");
38177633Sdfr
39177633Sdfr/*
40177633Sdfr * authunix_prot.c
41177633Sdfr * XDR for UNIX style authentication parameters for RPC
42177633Sdfr *
43177633Sdfr * Copyright (C) 1984, Sun Microsystems, Inc.
44177633Sdfr */
45177633Sdfr
46177633Sdfr#include <sys/param.h>
47193066Sjamie#include <sys/jail.h>
48177633Sdfr#include <sys/kernel.h>
49177633Sdfr#include <sys/systm.h>
50177633Sdfr#include <sys/ucred.h>
51177633Sdfr
52177633Sdfr#include <rpc/types.h>
53177633Sdfr#include <rpc/xdr.h>
54177633Sdfr#include <rpc/auth.h>
55177633Sdfr
56177685Sdfr#include <rpc/rpc_com.h>
57177633Sdfr
58177633Sdfr/* gids compose part of a credential; there may not be more than 16 of them */
59177633Sdfr#define NGRPS 16
60177633Sdfr
61177633Sdfr/*
62177633Sdfr * XDR for unix authentication parameters.
63177633Sdfr */
64177633Sdfrbool_t
65177633Sdfrxdr_authunix_parms(XDR *xdrs, uint32_t *time, struct xucred *cred)
66177633Sdfr{
67177633Sdfr	uint32_t namelen;
68177633Sdfr	uint32_t ngroups, i;
69177633Sdfr	uint32_t junk;
70193066Sjamie	char hostbuf[MAXHOSTNAMELEN];
71177633Sdfr
72177633Sdfr	if (xdrs->x_op == XDR_ENCODE) {
73180025Sdfr		/*
74180025Sdfr		 * Restrict name length to 255 according to RFC 1057.
75180025Sdfr		 */
76193066Sjamie		getcredhostname(NULL, hostbuf, sizeof(hostbuf));
77193066Sjamie		namelen = strlen(hostbuf);
78180025Sdfr		if (namelen > 255)
79180025Sdfr			namelen = 255;
80177633Sdfr	} else {
81177633Sdfr		namelen = 0;
82177633Sdfr	}
83177633Sdfr	junk = 0;
84177633Sdfr
85177633Sdfr	if (!xdr_uint32_t(xdrs, time)
86177633Sdfr	    || !xdr_uint32_t(xdrs, &namelen))
87177633Sdfr		return (FALSE);
88177633Sdfr
89177633Sdfr	/*
90177633Sdfr	 * Ignore the hostname on decode.
91177633Sdfr	 */
92177633Sdfr	if (xdrs->x_op == XDR_ENCODE) {
93193066Sjamie		if (!xdr_opaque(xdrs, hostbuf, namelen))
94177633Sdfr			return (FALSE);
95177633Sdfr	} else {
96177633Sdfr		xdr_setpos(xdrs, xdr_getpos(xdrs) + RNDUP(namelen));
97177633Sdfr	}
98177633Sdfr
99177633Sdfr	if (!xdr_uint32_t(xdrs, &cred->cr_uid))
100177633Sdfr		return (FALSE);
101177633Sdfr	if (!xdr_uint32_t(xdrs, &cred->cr_groups[0]))
102177633Sdfr		return (FALSE);
103177633Sdfr
104177633Sdfr	if (xdrs->x_op == XDR_ENCODE) {
105177633Sdfr		ngroups = cred->cr_ngroups - 1;
106177633Sdfr		if (ngroups > NGRPS)
107177633Sdfr			ngroups = NGRPS;
108177633Sdfr	}
109177633Sdfr
110177633Sdfr	if (!xdr_uint32_t(xdrs, &ngroups))
111177633Sdfr		return (FALSE);
112177633Sdfr	for (i = 0; i < ngroups; i++) {
113202143Sbrooks		if (i + 1 < ngroups_max + 1) {
114177633Sdfr			if (!xdr_uint32_t(xdrs, &cred->cr_groups[i + 1]))
115177633Sdfr				return (FALSE);
116177633Sdfr		} else {
117177633Sdfr			if (!xdr_uint32_t(xdrs, &junk))
118177633Sdfr				return (FALSE);
119177633Sdfr		}
120177633Sdfr	}
121177633Sdfr
122177633Sdfr	if (xdrs->x_op == XDR_DECODE) {
123202143Sbrooks		if (ngroups + 1 > ngroups_max + 1)
124202143Sbrooks			cred->cr_ngroups = ngroups_max + 1;
125177633Sdfr		else
126177633Sdfr			cred->cr_ngroups = ngroups + 1;
127177633Sdfr	}
128177633Sdfr
129177633Sdfr	return (TRUE);
130177633Sdfr}
131