1*84d9c625SLionel Sambuc /* $NetBSD: svc_auth.c,v 1.17 2013/03/11 20:19:29 tron Exp $ */
22fe8fb19SBen Gras
32fe8fb19SBen Gras /*
4*84d9c625SLionel Sambuc * Copyright (c) 2010, Oracle America, Inc.
52fe8fb19SBen Gras *
6*84d9c625SLionel Sambuc * Redistribution and use in source and binary forms, with or without
7*84d9c625SLionel Sambuc * modification, are permitted provided that the following conditions are
8*84d9c625SLionel Sambuc * met:
92fe8fb19SBen Gras *
10*84d9c625SLionel Sambuc * * Redistributions of source code must retain the above copyright
11*84d9c625SLionel Sambuc * notice, this list of conditions and the following disclaimer.
12*84d9c625SLionel Sambuc * * Redistributions in binary form must reproduce the above
13*84d9c625SLionel Sambuc * copyright notice, this list of conditions and the following
14*84d9c625SLionel Sambuc * disclaimer in the documentation and/or other materials
15*84d9c625SLionel Sambuc * provided with the distribution.
16*84d9c625SLionel Sambuc * * Neither the name of the "Oracle America, Inc." nor the names of its
17*84d9c625SLionel Sambuc * contributors may be used to endorse or promote products derived
18*84d9c625SLionel Sambuc * from this software without specific prior written permission.
192fe8fb19SBen Gras *
20*84d9c625SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21*84d9c625SLionel Sambuc * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22*84d9c625SLionel Sambuc * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23*84d9c625SLionel Sambuc * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24*84d9c625SLionel Sambuc * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25*84d9c625SLionel Sambuc * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26*84d9c625SLionel Sambuc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
27*84d9c625SLionel Sambuc * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28*84d9c625SLionel Sambuc * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29*84d9c625SLionel Sambuc * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30*84d9c625SLionel Sambuc * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31*84d9c625SLionel Sambuc * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
322fe8fb19SBen Gras */
332fe8fb19SBen Gras /*
342fe8fb19SBen Gras * Copyright (c) 1986-1991 by Sun Microsystems Inc.
352fe8fb19SBen Gras */
362fe8fb19SBen Gras
372fe8fb19SBen Gras /* #ident "@(#)svc_auth.c 1.16 94/04/24 SMI" */
382fe8fb19SBen Gras
392fe8fb19SBen Gras #include <sys/cdefs.h>
402fe8fb19SBen Gras #if defined(LIBC_SCCS) && !defined(lint)
412fe8fb19SBen Gras #if 0
422fe8fb19SBen Gras static char sccsid[] = "@(#)svc_auth.c 1.26 89/02/07 Copyr 1984 Sun Micro";
432fe8fb19SBen Gras #else
44*84d9c625SLionel Sambuc __RCSID("$NetBSD: svc_auth.c,v 1.17 2013/03/11 20:19:29 tron Exp $");
452fe8fb19SBen Gras #endif
462fe8fb19SBen Gras #endif
472fe8fb19SBen Gras
482fe8fb19SBen Gras /*
492fe8fb19SBen Gras * svc_auth.c, Server-side rpc authenticator interface.
502fe8fb19SBen Gras *
512fe8fb19SBen Gras */
522fe8fb19SBen Gras
532fe8fb19SBen Gras #include "namespace.h"
542fe8fb19SBen Gras #include "reentrant.h"
552fe8fb19SBen Gras #include <sys/types.h>
562fe8fb19SBen Gras #include <rpc/rpc.h>
572fe8fb19SBen Gras #include <assert.h>
582fe8fb19SBen Gras #include <stdlib.h>
592fe8fb19SBen Gras
602fe8fb19SBen Gras #ifdef __weak_alias
612fe8fb19SBen Gras __weak_alias(svc_auth_reg,_svc_auth_reg)
622fe8fb19SBen Gras #endif
632fe8fb19SBen Gras
642fe8fb19SBen Gras /*
652fe8fb19SBen Gras * svcauthsw is the bdevsw of server side authentication.
662fe8fb19SBen Gras *
672fe8fb19SBen Gras * Server side authenticators are called from authenticate by
682fe8fb19SBen Gras * using the client auth struct flavor field to index into svcauthsw.
692fe8fb19SBen Gras * The server auth flavors must implement a routine that looks
702fe8fb19SBen Gras * like:
712fe8fb19SBen Gras *
722fe8fb19SBen Gras * enum auth_stat
732fe8fb19SBen Gras * flavorx_auth(rqst, msg)
742fe8fb19SBen Gras * struct svc_req *rqst;
752fe8fb19SBen Gras * struct rpc_msg *msg;
762fe8fb19SBen Gras *
772fe8fb19SBen Gras */
782fe8fb19SBen Gras
792fe8fb19SBen Gras /* declarations to allow servers to specify new authentication flavors */
802fe8fb19SBen Gras struct authsvc {
812fe8fb19SBen Gras int flavor;
82f14fb602SLionel Sambuc enum auth_stat (*handler)(struct svc_req *, struct rpc_msg *);
832fe8fb19SBen Gras struct authsvc *next;
842fe8fb19SBen Gras };
852fe8fb19SBen Gras static struct authsvc *Auths = NULL;
862fe8fb19SBen Gras
872fe8fb19SBen Gras /*
882fe8fb19SBen Gras * The call rpc message, msg has been obtained from the wire. The msg contains
892fe8fb19SBen Gras * the raw form of credentials and verifiers. authenticate returns AUTH_OK
902fe8fb19SBen Gras * if the msg is successfully authenticated. If AUTH_OK then the routine also
912fe8fb19SBen Gras * does the following things:
922fe8fb19SBen Gras * set rqst->rq_xprt->verf to the appropriate response verifier;
932fe8fb19SBen Gras * sets rqst->rq_client_cred to the "cooked" form of the credentials.
942fe8fb19SBen Gras *
952fe8fb19SBen Gras * NB: rqst->rq_cxprt->verf must be pre-alloctaed;
962fe8fb19SBen Gras * its length is set appropriately.
972fe8fb19SBen Gras *
982fe8fb19SBen Gras * The caller still owns and is responsible for msg->u.cmb.cred and
992fe8fb19SBen Gras * msg->u.cmb.verf. The authentication system retains ownership of
1002fe8fb19SBen Gras * rqst->rq_client_cred, the cooked credentials.
1012fe8fb19SBen Gras *
1022fe8fb19SBen Gras * There is an assumption that any flavour less than AUTH_NULL is
1032fe8fb19SBen Gras * invalid.
1042fe8fb19SBen Gras */
1052fe8fb19SBen Gras enum auth_stat
_authenticate(struct svc_req * rqst,struct rpc_msg * msg)106f14fb602SLionel Sambuc _authenticate(struct svc_req *rqst, struct rpc_msg *msg)
1072fe8fb19SBen Gras {
1082fe8fb19SBen Gras int cred_flavor;
1092fe8fb19SBen Gras struct authsvc *asp;
1102fe8fb19SBen Gras enum auth_stat dummy;
1112fe8fb19SBen Gras #ifdef _REENTRANT
1122fe8fb19SBen Gras extern mutex_t authsvc_lock;
1132fe8fb19SBen Gras #endif
1142fe8fb19SBen Gras
1152fe8fb19SBen Gras _DIAGASSERT(rqst != NULL);
1162fe8fb19SBen Gras _DIAGASSERT(msg != NULL);
1172fe8fb19SBen Gras
1182fe8fb19SBen Gras /* VARIABLES PROTECTED BY authsvc_lock: asp, Auths */
1192fe8fb19SBen Gras
1202fe8fb19SBen Gras rqst->rq_cred = msg->rm_call.cb_cred;
1212fe8fb19SBen Gras rqst->rq_xprt->xp_verf.oa_flavor = _null_auth.oa_flavor;
1222fe8fb19SBen Gras rqst->rq_xprt->xp_verf.oa_length = 0;
1232fe8fb19SBen Gras cred_flavor = rqst->rq_cred.oa_flavor;
1242fe8fb19SBen Gras switch (cred_flavor) {
1252fe8fb19SBen Gras case AUTH_NULL:
1262fe8fb19SBen Gras dummy = _svcauth_null(rqst, msg);
1272fe8fb19SBen Gras return (dummy);
1282fe8fb19SBen Gras case AUTH_SYS:
1292fe8fb19SBen Gras dummy = _svcauth_unix(rqst, msg);
1302fe8fb19SBen Gras return (dummy);
1312fe8fb19SBen Gras case AUTH_SHORT:
1322fe8fb19SBen Gras dummy = _svcauth_short(rqst, msg);
1332fe8fb19SBen Gras return (dummy);
1342fe8fb19SBen Gras #if 0
1352fe8fb19SBen Gras case AUTH_DES:
1362fe8fb19SBen Gras dummy = __svcauth_des(rqst, msg);
1372fe8fb19SBen Gras return (dummy);
1382fe8fb19SBen Gras #endif
1392fe8fb19SBen Gras default:
1402fe8fb19SBen Gras break;
1412fe8fb19SBen Gras }
1422fe8fb19SBen Gras
1432fe8fb19SBen Gras /* flavor doesn't match any of the builtin types, so try new ones */
1442fe8fb19SBen Gras mutex_lock(&authsvc_lock);
1452fe8fb19SBen Gras for (asp = Auths; asp; asp = asp->next) {
1462fe8fb19SBen Gras if (asp->flavor == cred_flavor) {
1472fe8fb19SBen Gras enum auth_stat as;
1482fe8fb19SBen Gras
1492fe8fb19SBen Gras as = (*asp->handler)(rqst, msg);
1502fe8fb19SBen Gras mutex_unlock(&authsvc_lock);
1512fe8fb19SBen Gras return (as);
1522fe8fb19SBen Gras }
1532fe8fb19SBen Gras }
1542fe8fb19SBen Gras mutex_unlock(&authsvc_lock);
1552fe8fb19SBen Gras
1562fe8fb19SBen Gras return (AUTH_REJECTEDCRED);
1572fe8fb19SBen Gras }
1582fe8fb19SBen Gras
1592fe8fb19SBen Gras /*ARGSUSED*/
1602fe8fb19SBen Gras enum auth_stat
_svcauth_null(struct svc_req * rqst,struct rpc_msg * msg)161f14fb602SLionel Sambuc _svcauth_null(struct svc_req *rqst, struct rpc_msg *msg)
1622fe8fb19SBen Gras {
1632fe8fb19SBen Gras return (AUTH_OK);
1642fe8fb19SBen Gras }
1652fe8fb19SBen Gras
1662fe8fb19SBen Gras /*
1672fe8fb19SBen Gras * Allow the rpc service to register new authentication types that it is
1682fe8fb19SBen Gras * prepared to handle. When an authentication flavor is registered,
1692fe8fb19SBen Gras * the flavor is checked against already registered values. If not
1702fe8fb19SBen Gras * registered, then a new Auths entry is added on the list.
1712fe8fb19SBen Gras *
1722fe8fb19SBen Gras * There is no provision to delete a registration once registered.
1732fe8fb19SBen Gras *
1742fe8fb19SBen Gras * This routine returns:
1752fe8fb19SBen Gras * 0 if registration successful
1762fe8fb19SBen Gras * 1 if flavor already registered
1772fe8fb19SBen Gras * -1 if can't register (errno set)
1782fe8fb19SBen Gras */
1792fe8fb19SBen Gras
1802fe8fb19SBen Gras int
svc_auth_reg(int cred_flavor,enum auth_stat (* handler)(struct svc_req *,struct rpc_msg *))181f14fb602SLionel Sambuc svc_auth_reg(
182f14fb602SLionel Sambuc int cred_flavor,
183f14fb602SLionel Sambuc enum auth_stat (*handler)(struct svc_req *, struct rpc_msg *))
1842fe8fb19SBen Gras {
1852fe8fb19SBen Gras struct authsvc *asp;
1862fe8fb19SBen Gras #ifdef _REENTRANT
1872fe8fb19SBen Gras extern mutex_t authsvc_lock;
1882fe8fb19SBen Gras #endif
1892fe8fb19SBen Gras
1902fe8fb19SBen Gras switch (cred_flavor) {
1912fe8fb19SBen Gras case AUTH_NULL:
1922fe8fb19SBen Gras case AUTH_SYS:
1932fe8fb19SBen Gras case AUTH_SHORT:
1942fe8fb19SBen Gras #if 0
1952fe8fb19SBen Gras case AUTH_DES:
1962fe8fb19SBen Gras #endif
1972fe8fb19SBen Gras /* already registered */
1982fe8fb19SBen Gras return (1);
1992fe8fb19SBen Gras
2002fe8fb19SBen Gras default:
2012fe8fb19SBen Gras mutex_lock(&authsvc_lock);
2022fe8fb19SBen Gras for (asp = Auths; asp; asp = asp->next) {
2032fe8fb19SBen Gras if (asp->flavor == cred_flavor) {
2042fe8fb19SBen Gras /* already registered */
2052fe8fb19SBen Gras mutex_unlock(&authsvc_lock);
2062fe8fb19SBen Gras return (1);
2072fe8fb19SBen Gras }
2082fe8fb19SBen Gras }
2092fe8fb19SBen Gras
2102fe8fb19SBen Gras /* this is a new one, so go ahead and register it */
2112fe8fb19SBen Gras asp = mem_alloc(sizeof (*asp));
2122fe8fb19SBen Gras if (asp == NULL) {
2132fe8fb19SBen Gras mutex_unlock(&authsvc_lock);
2142fe8fb19SBen Gras return (-1);
2152fe8fb19SBen Gras }
2162fe8fb19SBen Gras asp->flavor = cred_flavor;
2172fe8fb19SBen Gras asp->handler = handler;
2182fe8fb19SBen Gras asp->next = Auths;
2192fe8fb19SBen Gras Auths = asp;
2202fe8fb19SBen Gras mutex_unlock(&authsvc_lock);
2212fe8fb19SBen Gras break;
2222fe8fb19SBen Gras }
2232fe8fb19SBen Gras return (0);
2242fe8fb19SBen Gras }
225