1/*-
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright (c) 2009, Sun Microsystems, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 * - Redistributions of source code must retain the above copyright notice,
10 *   this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright notice,
12 *   this list of conditions and the following disclaimer in the documentation
13 *   and/or other materials provided with the distribution.
14 * - Neither the name of Sun Microsystems, Inc. nor the names of its
15 *   contributors may be used to endorse or promote products derived
16 *   from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
29 */
30
31#include <sys/cdefs.h>
32/*
33 * svc_auth_unix.c
34 * Handles UNIX flavor authentication parameters on the service side of rpc.
35 * There are two svc auth implementations here: AUTH_UNIX and AUTH_SHORT.
36 * _svcauth_unix does full blown unix style uid,gid+gids auth,
37 * _svcauth_short uses a shorthand auth to index into a cache of longhand auths.
38 * Note: the shorthand has been gutted for efficiency.
39 *
40 * Copyright (C) 1984, Sun Microsystems, Inc.
41 */
42
43#include <sys/param.h>
44#include <sys/lock.h>
45#include <sys/mutex.h>
46#include <sys/systm.h>
47#include <sys/ucred.h>
48
49#include <rpc/rpc.h>
50
51#include <rpc/rpc_com.h>
52
53#define MAX_MACHINE_NAME	255
54#define NGRPS			16
55
56/*
57 * Unix longhand authenticator
58 */
59enum auth_stat
60_svcauth_unix(struct svc_req *rqst, struct rpc_msg *msg)
61{
62	enum auth_stat stat;
63	XDR xdrs;
64	int32_t *buf;
65	uint32_t time;
66	struct xucred *xcr;
67	u_int auth_len;
68	size_t str_len, gid_len;
69	u_int i;
70
71	xcr = rqst->rq_clntcred;
72	auth_len = (u_int)msg->rm_call.cb_cred.oa_length;
73	xdrmem_create(&xdrs, msg->rm_call.cb_cred.oa_base, auth_len,
74	    XDR_DECODE);
75	buf = XDR_INLINE(&xdrs, auth_len);
76	if (buf != NULL) {
77		time = IXDR_GET_UINT32(buf);
78		str_len = (size_t)IXDR_GET_UINT32(buf);
79		if (str_len > MAX_MACHINE_NAME) {
80			stat = AUTH_BADCRED;
81			goto done;
82		}
83		str_len = RNDUP(str_len);
84		buf += str_len / sizeof (int32_t);
85		xcr->cr_uid = IXDR_GET_UINT32(buf);
86		xcr->cr_groups[0] = IXDR_GET_UINT32(buf);
87		gid_len = (size_t)IXDR_GET_UINT32(buf);
88		if (gid_len > NGRPS) {
89			stat = AUTH_BADCRED;
90			goto done;
91		}
92		for (i = 0; i < gid_len; i++) {
93			if (i + 1 < XU_NGROUPS)
94				xcr->cr_groups[i + 1] = IXDR_GET_INT32(buf);
95			else
96				buf++;
97		}
98		if (gid_len + 1 > XU_NGROUPS)
99			xcr->cr_ngroups = XU_NGROUPS;
100		else
101			xcr->cr_ngroups = gid_len + 1;
102
103		/*
104		 * five is the smallest unix credentials structure -
105		 * timestamp, hostname len (0), uid, gid, and gids len (0).
106		 */
107		if ((5 + gid_len) * BYTES_PER_XDR_UNIT + str_len > auth_len) {
108			(void) printf("bad auth_len gid %ld str %ld auth %u\n",
109			    (long)gid_len, (long)str_len, auth_len);
110			stat = AUTH_BADCRED;
111			goto done;
112		}
113	} else if (! xdr_authunix_parms(&xdrs, &time, xcr)) {
114		stat = AUTH_BADCRED;
115		goto done;
116	}
117
118	rqst->rq_verf = _null_auth;
119	stat = AUTH_OK;
120done:
121	XDR_DESTROY(&xdrs);
122
123	return (stat);
124}
125
126
127/*
128 * Shorthand unix authenticator
129 * Looks up longhand in a cache.
130 */
131/*ARGSUSED*/
132enum auth_stat
133_svcauth_short(struct svc_req *rqst, struct rpc_msg *msg)
134{
135	return (AUTH_REJECTEDCRED);
136}
137