xref: /freebsd/sys/rpc/svc_auth.c (revision ac00d4d59b18a76c6148ca5d7439bb446d38da5c)
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 #if defined(LIBC_SCCS) && !defined(lint)
37 static char sccsid[] = "@(#)svc_auth.c 1.26 89/02/07 Copyr 1984 Sun Micro";
38 #endif
39 #include <sys/cdefs.h>
40 /*
41  * svc_auth.c, Server-side rpc authenticator interface.
42  *
43  */
44 
45 #include <sys/param.h>
46 #include <sys/lock.h>
47 #include <sys/mutex.h>
48 #include <sys/proc.h>
49 #include <sys/systm.h>
50 #include <sys/jail.h>
51 #include <sys/ucred.h>
52 
53 #include <rpc/rpc.h>
54 #include <rpc/rpcsec_tls.h>
55 
56 static enum auth_stat (*_svcauth_rpcsec_gss)(struct svc_req *,
57     struct rpc_msg *) = NULL;
58 static int (*_svcauth_rpcsec_gss_getcred)(struct svc_req *,
59     struct ucred **, int *);
60 
61 static const struct svc_auth_ops svc_auth_null_ops;
62 
63 /*
64  * The call rpc message, msg has been obtained from the wire.  The msg contains
65  * the raw form of credentials and verifiers.  authenticate returns AUTH_OK
66  * if the msg is successfully authenticated.  If AUTH_OK then the routine also
67  * does the following things:
68  * set rqst->rq_xprt->verf to the appropriate response verifier;
69  * sets rqst->rq_client_cred to the "cooked" form of the credentials.
70  *
71  * NB: rqst->rq_cxprt->verf must be pre-allocated;
72  * its length is set appropriately.
73  *
74  * The caller still owns and is responsible for msg->u.cmb.cred and
75  * msg->u.cmb.verf.  The authentication system retains ownership of
76  * rqst->rq_client_cred, the cooked credentials.
77  *
78  * There is an assumption that any flavour less than AUTH_NULL is
79  * invalid.
80  */
81 enum auth_stat
82 _authenticate(struct svc_req *rqst, struct rpc_msg *msg)
83 {
84 	int cred_flavor;
85 	enum auth_stat dummy;
86 
87 	rqst->rq_cred = msg->rm_call.cb_cred;
88 	rqst->rq_auth.svc_ah_ops = &svc_auth_null_ops;
89 	rqst->rq_auth.svc_ah_private = NULL;
90 	cred_flavor = rqst->rq_cred.oa_flavor;
91 	switch (cred_flavor) {
92 	case AUTH_NULL:
93 		dummy = _svcauth_null(rqst, msg);
94 		return (dummy);
95 	case AUTH_SYS:
96 		if ((rqst->rq_xprt->xp_tls & RPCTLS_FLAGS_DISABLED) != 0)
97 			return (AUTH_REJECTEDCRED);
98 		dummy = _svcauth_unix(rqst, msg);
99 		return (dummy);
100 	case AUTH_SHORT:
101 		if ((rqst->rq_xprt->xp_tls & RPCTLS_FLAGS_DISABLED) != 0)
102 			return (AUTH_REJECTEDCRED);
103 		dummy = _svcauth_short(rqst, msg);
104 		return (dummy);
105 	case RPCSEC_GSS:
106 		if ((rqst->rq_xprt->xp_tls & RPCTLS_FLAGS_DISABLED) != 0)
107 			return (AUTH_REJECTEDCRED);
108 		if (!_svcauth_rpcsec_gss)
109 			return (AUTH_REJECTEDCRED);
110 		dummy = _svcauth_rpcsec_gss(rqst, msg);
111 		return (dummy);
112 	case AUTH_TLS:
113 		dummy = _svcauth_rpcsec_tls(rqst, msg);
114 		return (dummy);
115 	default:
116 		break;
117 	}
118 
119 	return (AUTH_REJECTEDCRED);
120 }
121 
122 /*
123  * A set of null auth methods used by any authentication protocols
124  * that don't need to inspect or modify the message body.
125  */
126 static bool_t
127 svcauth_null_wrap(SVCAUTH *auth, struct mbuf **mp)
128 {
129 
130 	return (TRUE);
131 }
132 
133 static bool_t
134 svcauth_null_unwrap(SVCAUTH *auth, struct mbuf **mp)
135 {
136 
137 	return (TRUE);
138 }
139 
140 static void
141 svcauth_null_release(SVCAUTH *auth)
142 {
143 
144 }
145 
146 static const struct svc_auth_ops svc_auth_null_ops = {
147 	.svc_ah_wrap = svcauth_null_wrap,
148 	.svc_ah_unwrap = svcauth_null_unwrap,
149 	.svc_ah_release = svcauth_null_release,
150 };
151 
152 /*ARGSUSED*/
153 enum auth_stat
154 _svcauth_null(struct svc_req *rqst, struct rpc_msg *msg)
155 {
156 
157 	rqst->rq_verf = _null_auth;
158 	return (AUTH_OK);
159 }
160 
161 int
162 svc_auth_reg(int flavor,
163     enum auth_stat (*svcauth)(struct svc_req *, struct rpc_msg *),
164     int (*getcred)(struct svc_req *, struct ucred **, int *))
165 {
166 
167 	if (flavor == RPCSEC_GSS) {
168 		_svcauth_rpcsec_gss = svcauth;
169 		_svcauth_rpcsec_gss_getcred = getcred;
170 	}
171 	return (TRUE);
172 }
173 
174 int
175 svc_getcred(struct svc_req *rqst, struct ucred **crp, int *flavorp)
176 {
177 	struct ucred *cr = NULL;
178 	int flavor;
179 	struct xucred *xcr;
180 	SVCXPRT *xprt = rqst->rq_xprt;
181 
182 	flavor = rqst->rq_cred.oa_flavor;
183 	if (flavorp)
184 		*flavorp = flavor;
185 
186 	/*
187 	 * If there are credentials acquired via a TLS
188 	 * certificate for this TCP connection, use those
189 	 * instead of what is in the RPC header.
190 	 */
191 	if ((xprt->xp_tls & (RPCTLS_FLAGS_CERTUSER |
192 	    RPCTLS_FLAGS_DISABLED)) == RPCTLS_FLAGS_CERTUSER &&
193 	    flavor == AUTH_UNIX) {
194 		cr = crget();
195 		cr->cr_uid = cr->cr_ruid = cr->cr_svuid = xprt->xp_uid;
196 		crsetgroups(cr, xprt->xp_ngrps, xprt->xp_gidp);
197 		cr->cr_rgid = cr->cr_svgid = xprt->xp_gidp[0];
198 		cr->cr_prison = curthread->td_ucred->cr_prison;
199 		prison_hold(cr->cr_prison);
200 		*crp = cr;
201 		return (TRUE);
202 	}
203 
204 	switch (flavor) {
205 	case AUTH_UNIX:
206 		xcr = (struct xucred *) rqst->rq_clntcred;
207 		cr = crget();
208 		cr->cr_uid = cr->cr_ruid = cr->cr_svuid = xcr->cr_uid;
209 		crsetgroups(cr, xcr->cr_ngroups, xcr->cr_groups);
210 		cr->cr_rgid = cr->cr_svgid = cr->cr_groups[0];
211 		cr->cr_prison = curthread->td_ucred->cr_prison;
212 		prison_hold(cr->cr_prison);
213 		*crp = cr;
214 		return (TRUE);
215 
216 	case RPCSEC_GSS:
217 		if (!_svcauth_rpcsec_gss_getcred)
218 			return (FALSE);
219 		return (_svcauth_rpcsec_gss_getcred(rqst, crp, flavorp));
220 
221 	default:
222 		return (FALSE);
223 	}
224 }
225 
226