svc_auth.c revision 267654
1219019Sgabor/*	$NetBSD: svc_auth.c,v 1.12 2000/07/06 03:10:35 christos Exp $	*/
2219019Sgabor
3219019Sgabor/*-
4219019Sgabor * Copyright (c) 2009, Sun Microsystems, Inc.
5219019Sgabor * All rights reserved.
6219019Sgabor *
7219019Sgabor * Redistribution and use in source and binary forms, with or without
8219019Sgabor * modification, are permitted provided that the following conditions are met:
9219019Sgabor * - Redistributions of source code must retain the above copyright notice,
10219019Sgabor *   this list of conditions and the following disclaimer.
11219019Sgabor * - Redistributions in binary form must reproduce the above copyright notice,
12219019Sgabor *   this list of conditions and the following disclaimer in the documentation
13219019Sgabor *   and/or other materials provided with the distribution.
14219019Sgabor * - Neither the name of Sun Microsystems, Inc. nor the names of its
15219019Sgabor *   contributors may be used to endorse or promote products derived
16219019Sgabor *   from this software without specific prior written permission.
17219019Sgabor *
18219019Sgabor * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19219019Sgabor * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20219019Sgabor * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21219019Sgabor * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22219019Sgabor * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23219019Sgabor * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24219019Sgabor * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25219019Sgabor * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26219019Sgabor * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27219019Sgabor * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28219019Sgabor * POSSIBILITY OF SUCH DAMAGE.
29219019Sgabor */
30219019Sgabor/*
31219019Sgabor * Copyright (c) 1986-1991 by Sun Microsystems Inc.
32219019Sgabor */
33219019Sgabor
34219019Sgabor#if defined(LIBC_SCCS) && !defined(lint)
35219019Sgabor#ident	"@(#)svc_auth.c	1.16	94/04/24 SMI"
36219019Sgaborstatic char sccsid[] = "@(#)svc_auth.c 1.26 89/02/07 Copyr 1984 Sun Micro";
37219019Sgabor#endif
38219019Sgabor#include <sys/cdefs.h>
39219019Sgabor__FBSDID("$FreeBSD: releng/9.3/sys/rpc/svc_auth.c 261057 2014-01-23 00:28:17Z mav $");
40219019Sgabor
41219019Sgabor/*
42219019Sgabor * svc_auth.c, Server-side rpc authenticator interface.
43219019Sgabor *
44219019Sgabor */
45219019Sgabor
46219019Sgabor#include <sys/param.h>
47219019Sgabor#include <sys/lock.h>
48219019Sgabor#include <sys/mutex.h>
49219019Sgabor#include <sys/systm.h>
50219019Sgabor#include <sys/jail.h>
51219019Sgabor#include <sys/ucred.h>
52219019Sgabor
53219019Sgabor#include <rpc/rpc.h>
54219019Sgabor
55219019Sgaborstatic enum auth_stat (*_svcauth_rpcsec_gss)(struct svc_req *,
56219019Sgabor    struct rpc_msg *) = NULL;
57219019Sgaborstatic int (*_svcauth_rpcsec_gss_getcred)(struct svc_req *,
58219019Sgabor    struct ucred **, int *);
59219019Sgabor
60219019Sgaborstatic struct svc_auth_ops svc_auth_null_ops;
61219019Sgabor
62219019Sgabor/*
63219019Sgabor * The call rpc message, msg has been obtained from the wire.  The msg contains
64219019Sgabor * the raw form of credentials and verifiers.  authenticate returns AUTH_OK
65219019Sgabor * if the msg is successfully authenticated.  If AUTH_OK then the routine also
66219019Sgabor * does the following things:
67219019Sgabor * set rqst->rq_xprt->verf to the appropriate response verifier;
68219019Sgabor * sets rqst->rq_client_cred to the "cooked" form of the credentials.
69219019Sgabor *
70219019Sgabor * NB: rqst->rq_cxprt->verf must be pre-alloctaed;
71219019Sgabor * its length is set appropriately.
72219019Sgabor *
73219019Sgabor * The caller still owns and is responsible for msg->u.cmb.cred and
74219019Sgabor * msg->u.cmb.verf.  The authentication system retains ownership of
75219019Sgabor * rqst->rq_client_cred, the cooked credentials.
76219019Sgabor *
77219019Sgabor * There is an assumption that any flavour less than AUTH_NULL is
78219019Sgabor * invalid.
79219019Sgabor */
80219019Sgaborenum auth_stat
81219019Sgabor_authenticate(struct svc_req *rqst, struct rpc_msg *msg)
82219019Sgabor{
83219019Sgabor	int cred_flavor;
84219019Sgabor	enum auth_stat dummy;
85219019Sgabor
86219019Sgabor	rqst->rq_cred = msg->rm_call.cb_cred;
87219019Sgabor	rqst->rq_auth.svc_ah_ops = &svc_auth_null_ops;
88219019Sgabor	rqst->rq_auth.svc_ah_private = NULL;
89219019Sgabor	cred_flavor = rqst->rq_cred.oa_flavor;
90219019Sgabor	switch (cred_flavor) {
91219019Sgabor	case AUTH_NULL:
92219019Sgabor		dummy = _svcauth_null(rqst, msg);
93219019Sgabor		return (dummy);
94219019Sgabor	case AUTH_SYS:
95219019Sgabor		dummy = _svcauth_unix(rqst, msg);
96219019Sgabor		return (dummy);
97219019Sgabor	case AUTH_SHORT:
98219019Sgabor		dummy = _svcauth_short(rqst, msg);
99219019Sgabor		return (dummy);
100219019Sgabor	case RPCSEC_GSS:
101219019Sgabor		if (!_svcauth_rpcsec_gss)
102219019Sgabor			return (AUTH_REJECTEDCRED);
103219019Sgabor		dummy = _svcauth_rpcsec_gss(rqst, msg);
104219019Sgabor		return (dummy);
105219019Sgabor	default:
106219019Sgabor		break;
107219019Sgabor	}
108219019Sgabor
109219019Sgabor	return (AUTH_REJECTEDCRED);
110219019Sgabor}
111219019Sgabor
112219019Sgabor/*
113219019Sgabor * A set of null auth methods used by any authentication protocols
114219019Sgabor * that don't need to inspect or modify the message body.
115219019Sgabor */
116219019Sgaborstatic bool_t
117219019Sgaborsvcauth_null_wrap(SVCAUTH *auth, struct mbuf **mp)
118219019Sgabor{
119219019Sgabor
120219019Sgabor	return (TRUE);
121219019Sgabor}
122219019Sgabor
123219019Sgaborstatic bool_t
124219019Sgaborsvcauth_null_unwrap(SVCAUTH *auth, struct mbuf **mp)
125219019Sgabor{
126219019Sgabor
127219019Sgabor	return (TRUE);
128219019Sgabor}
129219019Sgabor
130219019Sgaborstatic void
131219019Sgaborsvcauth_null_release(SVCAUTH *auth)
132219019Sgabor{
133219019Sgabor
134219019Sgabor}
135219019Sgabor
136219019Sgaborstatic struct svc_auth_ops svc_auth_null_ops = {
137219019Sgabor	svcauth_null_wrap,
138219019Sgabor	svcauth_null_unwrap,
139219019Sgabor	svcauth_null_release,
140219019Sgabor};
141219019Sgabor
142219019Sgabor/*ARGSUSED*/
143219019Sgaborenum auth_stat
144219019Sgabor_svcauth_null(struct svc_req *rqst, struct rpc_msg *msg)
145219019Sgabor{
146219019Sgabor
147219019Sgabor	rqst->rq_verf = _null_auth;
148219019Sgabor	return (AUTH_OK);
149219019Sgabor}
150219019Sgabor
151219019Sgaborint
152219019Sgaborsvc_auth_reg(int flavor,
153219019Sgabor    enum auth_stat (*svcauth)(struct svc_req *, struct rpc_msg *),
154219019Sgabor    int (*getcred)(struct svc_req *, struct ucred **, int *))
155219019Sgabor{
156219019Sgabor
157219019Sgabor	if (flavor == RPCSEC_GSS) {
158219019Sgabor		_svcauth_rpcsec_gss = svcauth;
159219019Sgabor		_svcauth_rpcsec_gss_getcred = getcred;
160219019Sgabor	}
161219019Sgabor	return (TRUE);
162219019Sgabor}
163219019Sgabor
164219019Sgaborint
165219019Sgaborsvc_getcred(struct svc_req *rqst, struct ucred **crp, int *flavorp)
166219019Sgabor{
167219019Sgabor	struct ucred *cr = NULL;
168219019Sgabor	int flavor;
169219019Sgabor	struct xucred *xcr;
170219019Sgabor
171219019Sgabor	flavor = rqst->rq_cred.oa_flavor;
172219019Sgabor	if (flavorp)
173219019Sgabor		*flavorp = flavor;
174219019Sgabor
175219019Sgabor	switch (flavor) {
176219019Sgabor	case AUTH_UNIX:
177219019Sgabor		xcr = (struct xucred *) rqst->rq_clntcred;
178219019Sgabor		cr = crget();
179219019Sgabor		cr->cr_uid = cr->cr_ruid = cr->cr_svuid = xcr->cr_uid;
180219019Sgabor		crsetgroups(cr, xcr->cr_ngroups, xcr->cr_groups);
181219019Sgabor		cr->cr_rgid = cr->cr_svgid = cr->cr_groups[0];
182219019Sgabor		cr->cr_prison = &prison0;
183219019Sgabor		prison_hold(cr->cr_prison);
184219019Sgabor		*crp = cr;
185219019Sgabor		return (TRUE);
186219019Sgabor
187219019Sgabor	case RPCSEC_GSS:
188219019Sgabor		if (!_svcauth_rpcsec_gss_getcred)
189219019Sgabor			return (FALSE);
190219019Sgabor		return (_svcauth_rpcsec_gss_getcred(rqst, crp, flavorp));
191219019Sgabor
192219019Sgabor	default:
193219019Sgabor		return (FALSE);
194219019Sgabor	}
195219019Sgabor}
196219019Sgabor
197219019Sgabor