/* $NetBSD: svc_auth.c,v 1.12 2000/07/06 03:10:35 christos Exp $ */12/*-3* SPDX-License-Identifier: BSD-3-Clause4*5* Copyright (c) 2009, Sun Microsystems, Inc.6* All rights reserved.7*8* Redistribution and use in source and binary forms, with or without9* modification, are permitted provided that the following conditions are met:10* - Redistributions of source code must retain the above copyright notice,11* this list of conditions and the following disclaimer.12* - Redistributions in binary form must reproduce the above copyright notice,13* this list of conditions and the following disclaimer in the documentation14* and/or other materials provided with the distribution.15* - Neither the name of Sun Microsystems, Inc. nor the names of its16* contributors may be used to endorse or promote products derived17* from this software without specific prior written permission.18*19* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"20* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE21* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE22* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE23* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR24* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF25* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS26* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN27* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)28* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE29* POSSIBILITY OF SUCH DAMAGE.30*/31/*32* Copyright (c) 1986-1991 by Sun Microsystems Inc.33*/3435/*36* svc_auth.c, Server-side rpc authenticator interface.37*38*/3940#include <sys/param.h>41#include <sys/conf.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>4849#include <rpc/rpc.h>50#include <rpc/rpcsec_tls.h>5152static enum auth_stat (*_svcauth_rpcsec_gss)(struct svc_req *,53struct rpc_msg *) = NULL;54static int (*_svcauth_rpcsec_gss_getcred)(struct svc_req *,55struct ucred **, int *);5657static const struct svc_auth_ops svc_auth_null_ops;5859/*60* The call rpc message, msg has been obtained from the wire. The msg contains61* the raw form of credentials and verifiers. authenticate returns AUTH_OK62* if the msg is successfully authenticated. If AUTH_OK then the routine also63* 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 and71* msg->u.cmb.verf. The authentication system retains ownership of72* rqst->rq_client_cred, the cooked credentials.73*74* There is an assumption that any flavour less than AUTH_NULL is75* invalid.76*/77enum auth_stat78_authenticate(struct svc_req *rqst, struct rpc_msg *msg)79{80int cred_flavor;81enum auth_stat dummy;8283rqst->rq_cred = msg->rm_call.cb_cred;84rqst->rq_auth.svc_ah_ops = &svc_auth_null_ops;85rqst->rq_auth.svc_ah_private = NULL;86cred_flavor = rqst->rq_cred.oa_flavor;87switch (cred_flavor) {88case AUTH_NULL:89dummy = _svcauth_null(rqst, msg);90return (dummy);91case AUTH_SYS:92if ((rqst->rq_xprt->xp_tls & RPCTLS_FLAGS_DISABLED) != 0)93return (AUTH_REJECTEDCRED);94dummy = _svcauth_unix(rqst, msg);95return (dummy);96case AUTH_SHORT:97if ((rqst->rq_xprt->xp_tls & RPCTLS_FLAGS_DISABLED) != 0)98return (AUTH_REJECTEDCRED);99dummy = _svcauth_short(rqst, msg);100return (dummy);101case RPCSEC_GSS:102if ((rqst->rq_xprt->xp_tls & RPCTLS_FLAGS_DISABLED) != 0)103return (AUTH_REJECTEDCRED);104if (!_svcauth_rpcsec_gss)105return (AUTH_REJECTEDCRED);106dummy = _svcauth_rpcsec_gss(rqst, msg);107return (dummy);108case AUTH_TLS:109dummy = _svcauth_rpcsec_tls(rqst, msg);110return (dummy);111default:112break;113}114115return (AUTH_REJECTEDCRED);116}117118/*119* A set of null auth methods used by any authentication protocols120* that don't need to inspect or modify the message body.121*/122static bool_t123svcauth_null_wrap(SVCAUTH *auth, struct mbuf **mp)124{125126return (TRUE);127}128129static bool_t130svcauth_null_unwrap(SVCAUTH *auth, struct mbuf **mp)131{132133return (TRUE);134}135136static void137svcauth_null_release(SVCAUTH *auth)138{139140}141142static 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};147148/*ARGSUSED*/149enum auth_stat150_svcauth_null(struct svc_req *rqst, struct rpc_msg *msg)151{152153rqst->rq_verf = _null_auth;154return (AUTH_OK);155}156157int158svc_auth_reg(int flavor,159enum auth_stat (*svcauth)(struct svc_req *, struct rpc_msg *),160int (*getcred)(struct svc_req *, struct ucred **, int *))161{162163if (flavor == RPCSEC_GSS) {164_svcauth_rpcsec_gss = svcauth;165_svcauth_rpcsec_gss_getcred = getcred;166}167return (TRUE);168}169170int171svc_getcred(struct svc_req *rqst, struct ucred **crp, int *flavorp)172{173struct ucred *cr = NULL;174int flavor;175struct xucred *xcr;176SVCXPRT *xprt = rqst->rq_xprt;177178flavor = rqst->rq_cred.oa_flavor;179if (flavorp)180*flavorp = flavor;181182/*183* If there are credentials acquired via a TLS184* certificate for this TCP connection, use those185* instead of what is in the RPC header.186*/187if ((xprt->xp_tls & (RPCTLS_FLAGS_CERTUSER |188RPCTLS_FLAGS_DISABLED)) == RPCTLS_FLAGS_CERTUSER &&189flavor == AUTH_UNIX) {190if (xprt->xp_ngrps <= 0)191return (FALSE);192cr = crget();193cr->cr_uid = cr->cr_ruid = cr->cr_svuid = xprt->xp_uid;194crsetgroups_and_egid(cr, xprt->xp_ngrps, xprt->xp_gidp, GID_NOGROUP);195cr->cr_rgid = cr->cr_svgid = cr->cr_gid;196cr->cr_prison = curthread->td_ucred->cr_prison;197prison_hold(cr->cr_prison);198*crp = cr;199return (TRUE);200}201202switch (flavor) {203case AUTH_UNIX:204xcr = (struct xucred *) rqst->rq_clntcred;205if (xcr->cr_ngroups <= 0)206return (FALSE);207cr = crget();208cr->cr_uid = cr->cr_ruid = cr->cr_svuid = xcr->cr_uid;209crsetgroups_and_egid(cr, xcr->cr_ngroups, xcr->cr_groups, GID_NOGROUP);210cr->cr_rgid = cr->cr_svgid = cr->cr_gid;211cr->cr_prison = curthread->td_ucred->cr_prison;212prison_hold(cr->cr_prison);213*crp = cr;214return (TRUE);215216case RPCSEC_GSS:217if (!_svcauth_rpcsec_gss_getcred)218return (FALSE);219return (_svcauth_rpcsec_gss_getcred(rqst, crp, flavorp));220221default:222return (FALSE);223}224}225226227228