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