xref: /freebsd/sys/rpc/svc_auth_unix.c (revision 9f0f30bc1f5f08d25243952bad3fdc6e13a75c2a)
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  */
59 enum 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_gid = 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 		/* XXXKE Fix this if cr_gid gets separated out. */
93 		for (i = 0; i < gid_len; i++) {
94 			if (i + 1 < XU_NGROUPS)
95 				xcr->cr_groups[i + 1] = IXDR_GET_INT32(buf);
96 			else
97 				buf++;
98 		}
99 		if (gid_len + 1 > XU_NGROUPS)
100 			xcr->cr_ngroups = XU_NGROUPS;
101 		else
102 			xcr->cr_ngroups = gid_len + 1;
103 
104 		/*
105 		 * five is the smallest unix credentials structure -
106 		 * timestamp, hostname len (0), uid, gid, and gids len (0).
107 		 */
108 		if ((5 + gid_len) * BYTES_PER_XDR_UNIT + str_len > auth_len) {
109 			(void) printf("bad auth_len gid %ld str %ld auth %u\n",
110 			    (long)gid_len, (long)str_len, auth_len);
111 			stat = AUTH_BADCRED;
112 			goto done;
113 		}
114 	} else if (! xdr_authunix_parms(&xdrs, &time, xcr)) {
115 		stat = AUTH_BADCRED;
116 		goto done;
117 	}
118 
119 	rqst->rq_verf = _null_auth;
120 	stat = AUTH_OK;
121 done:
122 	XDR_DESTROY(&xdrs);
123 
124 	return (stat);
125 }
126 
127 
128 /*
129  * Shorthand unix authenticator
130  * Looks up longhand in a cache.
131  */
132 /*ARGSUSED*/
133 enum auth_stat
134 _svcauth_short(struct svc_req *rqst, struct rpc_msg *msg)
135 {
136 	return (AUTH_REJECTEDCRED);
137 }
138