xref: /freebsd/sys/rpc/svc_auth.c (revision 2e3f49888ec8851bafb22011533217487764fdb0)
1 /*	$NetBSD: svc_auth.c,v 1.12 2000/07/06 03:10:35 christos Exp $	*/
2 
3 /*-
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  * Copyright (c) 2009, Sun Microsystems, Inc.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions are met:
11  * - Redistributions of source code must retain the above copyright notice,
12  *   this list of conditions and the following disclaimer.
13  * - Redistributions in binary form must reproduce the above copyright notice,
14  *   this list of conditions and the following disclaimer in the documentation
15  *   and/or other materials provided with the distribution.
16  * - Neither the name of Sun Microsystems, Inc. nor the names of its
17  *   contributors may be used to endorse or promote products derived
18  *   from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
24  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 /*
33  * Copyright (c) 1986-1991 by Sun Microsystems Inc.
34  */
35 
36 /*
37  * svc_auth.c, Server-side rpc authenticator interface.
38  *
39  */
40 
41 #include <sys/param.h>
42 #include <sys/lock.h>
43 #include <sys/mutex.h>
44 #include <sys/proc.h>
45 #include <sys/systm.h>
46 #include <sys/jail.h>
47 #include <sys/ucred.h>
48 
49 #include <rpc/rpc.h>
50 #include <rpc/rpcsec_tls.h>
51 
52 static enum auth_stat (*_svcauth_rpcsec_gss)(struct svc_req *,
53     struct rpc_msg *) = NULL;
54 static int (*_svcauth_rpcsec_gss_getcred)(struct svc_req *,
55     struct ucred **, int *);
56 
57 static const struct svc_auth_ops svc_auth_null_ops;
58 
59 /*
60  * The call rpc message, msg has been obtained from the wire.  The msg contains
61  * the raw form of credentials and verifiers.  authenticate returns AUTH_OK
62  * if the msg is successfully authenticated.  If AUTH_OK then the routine also
63  * does the following things:
64  * set rqst->rq_xprt->verf to the appropriate response verifier;
65  * sets rqst->rq_client_cred to the "cooked" form of the credentials.
66  *
67  * NB: rqst->rq_cxprt->verf must be pre-allocated;
68  * its length is set appropriately.
69  *
70  * The caller still owns and is responsible for msg->u.cmb.cred and
71  * msg->u.cmb.verf.  The authentication system retains ownership of
72  * rqst->rq_client_cred, the cooked credentials.
73  *
74  * There is an assumption that any flavour less than AUTH_NULL is
75  * invalid.
76  */
77 enum auth_stat
78 _authenticate(struct svc_req *rqst, struct rpc_msg *msg)
79 {
80 	int cred_flavor;
81 	enum auth_stat dummy;
82 
83 	rqst->rq_cred = msg->rm_call.cb_cred;
84 	rqst->rq_auth.svc_ah_ops = &svc_auth_null_ops;
85 	rqst->rq_auth.svc_ah_private = NULL;
86 	cred_flavor = rqst->rq_cred.oa_flavor;
87 	switch (cred_flavor) {
88 	case AUTH_NULL:
89 		dummy = _svcauth_null(rqst, msg);
90 		return (dummy);
91 	case AUTH_SYS:
92 		if ((rqst->rq_xprt->xp_tls & RPCTLS_FLAGS_DISABLED) != 0)
93 			return (AUTH_REJECTEDCRED);
94 		dummy = _svcauth_unix(rqst, msg);
95 		return (dummy);
96 	case AUTH_SHORT:
97 		if ((rqst->rq_xprt->xp_tls & RPCTLS_FLAGS_DISABLED) != 0)
98 			return (AUTH_REJECTEDCRED);
99 		dummy = _svcauth_short(rqst, msg);
100 		return (dummy);
101 	case RPCSEC_GSS:
102 		if ((rqst->rq_xprt->xp_tls & RPCTLS_FLAGS_DISABLED) != 0)
103 			return (AUTH_REJECTEDCRED);
104 		if (!_svcauth_rpcsec_gss)
105 			return (AUTH_REJECTEDCRED);
106 		dummy = _svcauth_rpcsec_gss(rqst, msg);
107 		return (dummy);
108 	case AUTH_TLS:
109 		dummy = _svcauth_rpcsec_tls(rqst, msg);
110 		return (dummy);
111 	default:
112 		break;
113 	}
114 
115 	return (AUTH_REJECTEDCRED);
116 }
117 
118 /*
119  * A set of null auth methods used by any authentication protocols
120  * that don't need to inspect or modify the message body.
121  */
122 static bool_t
123 svcauth_null_wrap(SVCAUTH *auth, struct mbuf **mp)
124 {
125 
126 	return (TRUE);
127 }
128 
129 static bool_t
130 svcauth_null_unwrap(SVCAUTH *auth, struct mbuf **mp)
131 {
132 
133 	return (TRUE);
134 }
135 
136 static void
137 svcauth_null_release(SVCAUTH *auth)
138 {
139 
140 }
141 
142 static const struct svc_auth_ops svc_auth_null_ops = {
143 	.svc_ah_wrap = svcauth_null_wrap,
144 	.svc_ah_unwrap = svcauth_null_unwrap,
145 	.svc_ah_release = svcauth_null_release,
146 };
147 
148 /*ARGSUSED*/
149 enum auth_stat
150 _svcauth_null(struct svc_req *rqst, struct rpc_msg *msg)
151 {
152 
153 	rqst->rq_verf = _null_auth;
154 	return (AUTH_OK);
155 }
156 
157 int
158 svc_auth_reg(int flavor,
159     enum auth_stat (*svcauth)(struct svc_req *, struct rpc_msg *),
160     int (*getcred)(struct svc_req *, struct ucred **, int *))
161 {
162 
163 	if (flavor == RPCSEC_GSS) {
164 		_svcauth_rpcsec_gss = svcauth;
165 		_svcauth_rpcsec_gss_getcred = getcred;
166 	}
167 	return (TRUE);
168 }
169 
170 int
171 svc_getcred(struct svc_req *rqst, struct ucred **crp, int *flavorp)
172 {
173 	struct ucred *cr = NULL;
174 	int flavor;
175 	struct xucred *xcr;
176 	SVCXPRT *xprt = rqst->rq_xprt;
177 
178 	flavor = rqst->rq_cred.oa_flavor;
179 	if (flavorp)
180 		*flavorp = flavor;
181 
182 	/*
183 	 * If there are credentials acquired via a TLS
184 	 * certificate for this TCP connection, use those
185 	 * instead of what is in the RPC header.
186 	 */
187 	if ((xprt->xp_tls & (RPCTLS_FLAGS_CERTUSER |
188 	    RPCTLS_FLAGS_DISABLED)) == RPCTLS_FLAGS_CERTUSER &&
189 	    flavor == AUTH_UNIX) {
190 		cr = crget();
191 		cr->cr_uid = cr->cr_ruid = cr->cr_svuid = xprt->xp_uid;
192 		crsetgroups(cr, xprt->xp_ngrps, xprt->xp_gidp);
193 		cr->cr_rgid = cr->cr_svgid = xprt->xp_gidp[0];
194 		cr->cr_prison = curthread->td_ucred->cr_prison;
195 		prison_hold(cr->cr_prison);
196 		*crp = cr;
197 		return (TRUE);
198 	}
199 
200 	switch (flavor) {
201 	case AUTH_UNIX:
202 		xcr = (struct xucred *) rqst->rq_clntcred;
203 		cr = crget();
204 		cr->cr_uid = cr->cr_ruid = cr->cr_svuid = xcr->cr_uid;
205 		crsetgroups(cr, xcr->cr_ngroups, xcr->cr_groups);
206 		cr->cr_rgid = cr->cr_svgid = cr->cr_groups[0];
207 		cr->cr_prison = curthread->td_ucred->cr_prison;
208 		prison_hold(cr->cr_prison);
209 		*crp = cr;
210 		return (TRUE);
211 
212 	case RPCSEC_GSS:
213 		if (!_svcauth_rpcsec_gss_getcred)
214 			return (FALSE);
215 		return (_svcauth_rpcsec_gss_getcred(rqst, crp, flavorp));
216 
217 	default:
218 		return (FALSE);
219 	}
220 }
221 
222