18360efbdSAlfred Perlstein /* $NetBSD: svc_auth.c,v 1.12 2000/07/06 03:10:35 christos Exp $ */
28360efbdSAlfred Perlstein
32e322d37SHiroki Sato /*-
48a16b7a1SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause
58a16b7a1SPedro F. Giffuni *
62e322d37SHiroki Sato * Copyright (c) 2009, Sun Microsystems, Inc.
72e322d37SHiroki Sato * All rights reserved.
899064799SGarrett Wollman *
92e322d37SHiroki Sato * Redistribution and use in source and binary forms, with or without
102e322d37SHiroki Sato * modification, are permitted provided that the following conditions are met:
112e322d37SHiroki Sato * - Redistributions of source code must retain the above copyright notice,
122e322d37SHiroki Sato * this list of conditions and the following disclaimer.
132e322d37SHiroki Sato * - Redistributions in binary form must reproduce the above copyright notice,
142e322d37SHiroki Sato * this list of conditions and the following disclaimer in the documentation
152e322d37SHiroki Sato * and/or other materials provided with the distribution.
162e322d37SHiroki Sato * - Neither the name of Sun Microsystems, Inc. nor the names of its
172e322d37SHiroki Sato * contributors may be used to endorse or promote products derived
182e322d37SHiroki Sato * from this software without specific prior written permission.
1999064799SGarrett Wollman *
202e322d37SHiroki Sato * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
212e322d37SHiroki Sato * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
222e322d37SHiroki Sato * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
232e322d37SHiroki Sato * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
242e322d37SHiroki Sato * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
252e322d37SHiroki Sato * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
262e322d37SHiroki Sato * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
272e322d37SHiroki Sato * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
282e322d37SHiroki Sato * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
292e322d37SHiroki Sato * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
302e322d37SHiroki Sato * POSSIBILITY OF SUCH DAMAGE.
3199064799SGarrett Wollman */
32ad133ed6SBill Paul /*
33ad133ed6SBill Paul * Copyright (c) 1986-1991 by Sun Microsystems Inc.
34ad133ed6SBill Paul */
3599064799SGarrett Wollman
3699064799SGarrett Wollman /*
37ad133ed6SBill Paul * svc_auth.c, Server-side rpc authenticator interface.
3899064799SGarrett Wollman *
3999064799SGarrett Wollman */
4099064799SGarrett Wollman
418360efbdSAlfred Perlstein #include "namespace.h"
429f5afc13SIan Dowse #include "reentrant.h"
43ad133ed6SBill Paul #include <sys/types.h>
448360efbdSAlfred Perlstein #include <rpc/rpc.h>
458360efbdSAlfred Perlstein #include <stdlib.h>
468360efbdSAlfred Perlstein #include "un-namespace.h"
47235baf26SDaniel Eischen #include "mt_misc.h"
4899064799SGarrett Wollman
4999064799SGarrett Wollman /*
5099064799SGarrett Wollman * svcauthsw is the bdevsw of server side authentication.
5199064799SGarrett Wollman *
5299064799SGarrett Wollman * Server side authenticators are called from authenticate by
5399064799SGarrett Wollman * using the client auth struct flavor field to index into svcauthsw.
5499064799SGarrett Wollman * The server auth flavors must implement a routine that looks
5599064799SGarrett Wollman * like:
5699064799SGarrett Wollman *
5799064799SGarrett Wollman * enum auth_stat
5899064799SGarrett Wollman * flavorx_auth(rqst, msg)
598360efbdSAlfred Perlstein * struct svc_req *rqst;
608360efbdSAlfred Perlstein * struct rpc_msg *msg;
6199064799SGarrett Wollman *
6299064799SGarrett Wollman */
6399064799SGarrett Wollman
64ad133ed6SBill Paul /* declarations to allow servers to specify new authentication flavors */
65ad133ed6SBill Paul struct authsvc {
66ad133ed6SBill Paul int flavor;
67c05ac53bSDavid E. O'Brien enum auth_stat (*handler)(struct svc_req *, struct rpc_msg *);
68ad133ed6SBill Paul struct authsvc *next;
6999064799SGarrett Wollman };
70ad133ed6SBill Paul static struct authsvc *Auths = NULL;
7199064799SGarrett Wollman
724efa8f3eSDoug Rabson struct svc_auth_ops svc_auth_null_ops;
738f55a568SDoug Rabson
7499064799SGarrett Wollman /*
7599064799SGarrett Wollman * The call rpc message, msg has been obtained from the wire. The msg contains
7699064799SGarrett Wollman * the raw form of credentials and verifiers. authenticate returns AUTH_OK
7799064799SGarrett Wollman * if the msg is successfully authenticated. If AUTH_OK then the routine also
7899064799SGarrett Wollman * does the following things:
7999064799SGarrett Wollman * set rqst->rq_xprt->verf to the appropriate response verifier;
8099064799SGarrett Wollman * sets rqst->rq_client_cred to the "cooked" form of the credentials.
8199064799SGarrett Wollman *
82*5c49e1cbSGordon Bergling * NB: rqst->rq_cxprt->verf must be pre-allocated;
8399064799SGarrett Wollman * its length is set appropriately.
8499064799SGarrett Wollman *
8599064799SGarrett Wollman * The caller still owns and is responsible for msg->u.cmb.cred and
8699064799SGarrett Wollman * msg->u.cmb.verf. The authentication system retains ownership of
8799064799SGarrett Wollman * rqst->rq_client_cred, the cooked credentials.
8899064799SGarrett Wollman *
8999064799SGarrett Wollman * There is an assumption that any flavour less than AUTH_NULL is
9099064799SGarrett Wollman * invalid.
9199064799SGarrett Wollman */
9299064799SGarrett Wollman enum auth_stat
_authenticate(struct svc_req * rqst,struct rpc_msg * msg)93587cf682SCraig Rodrigues _authenticate(struct svc_req *rqst, struct rpc_msg *msg)
9499064799SGarrett Wollman {
958360efbdSAlfred Perlstein int cred_flavor;
968360efbdSAlfred Perlstein struct authsvc *asp;
978360efbdSAlfred Perlstein enum auth_stat dummy;
988360efbdSAlfred Perlstein
998360efbdSAlfred Perlstein /* VARIABLES PROTECTED BY authsvc_lock: asp, Auths */
10099064799SGarrett Wollman
10199064799SGarrett Wollman rqst->rq_cred = msg->rm_call.cb_cred;
1028f55a568SDoug Rabson SVC_AUTH(rqst->rq_xprt).svc_ah_ops = &svc_auth_null_ops;
1038f55a568SDoug Rabson SVC_AUTH(rqst->rq_xprt).svc_ah_private = NULL;
10499064799SGarrett Wollman rqst->rq_xprt->xp_verf.oa_flavor = _null_auth.oa_flavor;
10599064799SGarrett Wollman rqst->rq_xprt->xp_verf.oa_length = 0;
10699064799SGarrett Wollman cred_flavor = rqst->rq_cred.oa_flavor;
107ad133ed6SBill Paul switch (cred_flavor) {
108ad133ed6SBill Paul case AUTH_NULL:
1098360efbdSAlfred Perlstein dummy = _svcauth_null(rqst, msg);
1108360efbdSAlfred Perlstein return (dummy);
1118360efbdSAlfred Perlstein case AUTH_SYS:
1128360efbdSAlfred Perlstein dummy = _svcauth_unix(rqst, msg);
1138360efbdSAlfred Perlstein return (dummy);
114ad133ed6SBill Paul case AUTH_SHORT:
1158360efbdSAlfred Perlstein dummy = _svcauth_short(rqst, msg);
1168360efbdSAlfred Perlstein return (dummy);
117ad133ed6SBill Paul #ifdef DES_BUILTIN
118ad133ed6SBill Paul case AUTH_DES:
1198360efbdSAlfred Perlstein dummy = _svcauth_des(rqst, msg);
1208360efbdSAlfred Perlstein return (dummy);
121ad133ed6SBill Paul #endif
1228360efbdSAlfred Perlstein default:
1238360efbdSAlfred Perlstein break;
124ad133ed6SBill Paul }
125ad133ed6SBill Paul
126ad133ed6SBill Paul /* flavor doesn't match any of the builtin types, so try new ones */
1278360efbdSAlfred Perlstein mutex_lock(&authsvc_lock);
128ad133ed6SBill Paul for (asp = Auths; asp; asp = asp->next) {
129ad133ed6SBill Paul if (asp->flavor == cred_flavor) {
130ad133ed6SBill Paul enum auth_stat as;
131ad133ed6SBill Paul
132ad133ed6SBill Paul as = (*asp->handler)(rqst, msg);
1338360efbdSAlfred Perlstein mutex_unlock(&authsvc_lock);
134ad133ed6SBill Paul return (as);
135ad133ed6SBill Paul }
13699064799SGarrett Wollman }
1378360efbdSAlfred Perlstein mutex_unlock(&authsvc_lock);
13899064799SGarrett Wollman
13999064799SGarrett Wollman return (AUTH_REJECTEDCRED);
14099064799SGarrett Wollman }
14199064799SGarrett Wollman
1428f55a568SDoug Rabson /*
1438f55a568SDoug Rabson * A set of null auth methods used by any authentication protocols
1448f55a568SDoug Rabson * that don't need to inspect or modify the message body.
1458f55a568SDoug Rabson */
1468f55a568SDoug Rabson static bool_t
svcauth_null_wrap(SVCAUTH * auth,XDR * xdrs,xdrproc_t xdr_func,caddr_t xdr_ptr)14768895e38SCraig Rodrigues svcauth_null_wrap(SVCAUTH *auth, XDR *xdrs, xdrproc_t xdr_func, caddr_t xdr_ptr)
1488f55a568SDoug Rabson {
1498f55a568SDoug Rabson
1508f55a568SDoug Rabson return (xdr_func(xdrs, xdr_ptr));
1518f55a568SDoug Rabson }
1528f55a568SDoug Rabson
1534efa8f3eSDoug Rabson struct svc_auth_ops svc_auth_null_ops = {
1548f55a568SDoug Rabson svcauth_null_wrap,
1558f55a568SDoug Rabson svcauth_null_wrap,
1568f55a568SDoug Rabson };
1578f55a568SDoug Rabson
158ad133ed6SBill Paul /*ARGSUSED*/
15999064799SGarrett Wollman enum auth_stat
_svcauth_null(struct svc_req * rqst,struct rpc_msg * msg)16068895e38SCraig Rodrigues _svcauth_null(struct svc_req *rqst, struct rpc_msg *msg)
16199064799SGarrett Wollman {
16299064799SGarrett Wollman return (AUTH_OK);
16399064799SGarrett Wollman }
164ad133ed6SBill Paul
165ad133ed6SBill Paul /*
166ad133ed6SBill Paul * Allow the rpc service to register new authentication types that it is
167ad133ed6SBill Paul * prepared to handle. When an authentication flavor is registered,
168ad133ed6SBill Paul * the flavor is checked against already registered values. If not
169ad133ed6SBill Paul * registered, then a new Auths entry is added on the list.
170ad133ed6SBill Paul *
171ad133ed6SBill Paul * There is no provision to delete a registration once registered.
172ad133ed6SBill Paul *
173ad133ed6SBill Paul * This routine returns:
174ad133ed6SBill Paul * 0 if registration successful
175ad133ed6SBill Paul * 1 if flavor already registered
176ad133ed6SBill Paul * -1 if can't register (errno set)
177ad133ed6SBill Paul */
178ad133ed6SBill Paul
179ad133ed6SBill Paul int
svc_auth_reg(int cred_flavor,enum auth_stat (* handler)(struct svc_req *,struct rpc_msg *))18068895e38SCraig Rodrigues svc_auth_reg(int cred_flavor,
18168895e38SCraig Rodrigues enum auth_stat (*handler)(struct svc_req *, struct rpc_msg *))
182ad133ed6SBill Paul {
1838360efbdSAlfred Perlstein struct authsvc *asp;
184ad133ed6SBill Paul
185ad133ed6SBill Paul switch (cred_flavor) {
186ad133ed6SBill Paul case AUTH_NULL:
1878360efbdSAlfred Perlstein case AUTH_SYS:
188ad133ed6SBill Paul case AUTH_SHORT:
189ad133ed6SBill Paul #ifdef DES_BUILTIN
190ad133ed6SBill Paul case AUTH_DES:
191ad133ed6SBill Paul #endif
192ad133ed6SBill Paul /* already registered */
193ad133ed6SBill Paul return (1);
194ad133ed6SBill Paul
195ad133ed6SBill Paul default:
1968360efbdSAlfred Perlstein mutex_lock(&authsvc_lock);
197ad133ed6SBill Paul for (asp = Auths; asp; asp = asp->next) {
198ad133ed6SBill Paul if (asp->flavor == cred_flavor) {
199ad133ed6SBill Paul /* already registered */
2008360efbdSAlfred Perlstein mutex_unlock(&authsvc_lock);
201ad133ed6SBill Paul return (1);
202ad133ed6SBill Paul }
203ad133ed6SBill Paul }
204ad133ed6SBill Paul
205ad133ed6SBill Paul /* this is a new one, so go ahead and register it */
2068360efbdSAlfred Perlstein asp = mem_alloc(sizeof (*asp));
207ad133ed6SBill Paul if (asp == NULL) {
2088360efbdSAlfred Perlstein mutex_unlock(&authsvc_lock);
209ad133ed6SBill Paul return (-1);
210ad133ed6SBill Paul }
211ad133ed6SBill Paul asp->flavor = cred_flavor;
212ad133ed6SBill Paul asp->handler = handler;
213ad133ed6SBill Paul asp->next = Auths;
214ad133ed6SBill Paul Auths = asp;
2158360efbdSAlfred Perlstein mutex_unlock(&authsvc_lock);
216ad133ed6SBill Paul break;
217ad133ed6SBill Paul }
218ad133ed6SBill Paul return (0);
219ad133ed6SBill Paul }
220