Home | History | Annotate | Line # | Download | only in rpc
svc_auth.c revision 1.15.58.1
      1 /*	$NetBSD: svc_auth.c,v 1.15.58.1 2013/03/14 22:03:09 riz Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2010, Oracle America, Inc.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions are
      8  * met:
      9  *
     10  *     * Redistributions of source code must retain the above copyright
     11  *       notice, this list of conditions and the following disclaimer.
     12  *     * Redistributions in binary form must reproduce the above
     13  *       copyright notice, this list of conditions and the following
     14  *       disclaimer in the documentation and/or other materials
     15  *       provided with the distribution.
     16  *     * Neither the name of the "Oracle America, 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
     21  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     22  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
     23  *   FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
     24  *   COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
     25  *   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  *   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
     27  *   GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     28  *   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     29  *   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
     30  *   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32  */
     33 /*
     34  * Copyright (c) 1986-1991 by Sun Microsystems Inc.
     35  */
     36 
     37 /* #ident	"@(#)svc_auth.c	1.16	94/04/24 SMI" */
     38 
     39 #include <sys/cdefs.h>
     40 #if defined(LIBC_SCCS) && !defined(lint)
     41 #if 0
     42 static char sccsid[] = "@(#)svc_auth.c 1.26 89/02/07 Copyr 1984 Sun Micro";
     43 #else
     44 __RCSID("$NetBSD: svc_auth.c,v 1.15.58.1 2013/03/14 22:03:09 riz Exp $");
     45 #endif
     46 #endif
     47 
     48 /*
     49  * svc_auth.c, Server-side rpc authenticator interface.
     50  *
     51  */
     52 
     53 #include "namespace.h"
     54 #include "reentrant.h"
     55 #include <sys/types.h>
     56 #include <rpc/rpc.h>
     57 #include <assert.h>
     58 #include <stdlib.h>
     59 
     60 #ifdef __weak_alias
     61 __weak_alias(svc_auth_reg,_svc_auth_reg)
     62 #endif
     63 
     64 /*
     65  * svcauthsw is the bdevsw of server side authentication.
     66  *
     67  * Server side authenticators are called from authenticate by
     68  * using the client auth struct flavor field to index into svcauthsw.
     69  * The server auth flavors must implement a routine that looks
     70  * like:
     71  *
     72  *	enum auth_stat
     73  *	flavorx_auth(rqst, msg)
     74  *		struct svc_req *rqst;
     75  *		struct rpc_msg *msg;
     76  *
     77  */
     78 
     79 /* declarations to allow servers to specify new authentication flavors */
     80 struct authsvc {
     81 	int	flavor;
     82 	enum	auth_stat (*handler) __P((struct svc_req *, struct rpc_msg *));
     83 	struct	authsvc	  *next;
     84 };
     85 static struct authsvc *Auths = NULL;
     86 
     87 /*
     88  * The call rpc message, msg has been obtained from the wire.  The msg contains
     89  * the raw form of credentials and verifiers.  authenticate returns AUTH_OK
     90  * if the msg is successfully authenticated.  If AUTH_OK then the routine also
     91  * does the following things:
     92  * set rqst->rq_xprt->verf to the appropriate response verifier;
     93  * sets rqst->rq_client_cred to the "cooked" form of the credentials.
     94  *
     95  * NB: rqst->rq_cxprt->verf must be pre-alloctaed;
     96  * its length is set appropriately.
     97  *
     98  * The caller still owns and is responsible for msg->u.cmb.cred and
     99  * msg->u.cmb.verf.  The authentication system retains ownership of
    100  * rqst->rq_client_cred, the cooked credentials.
    101  *
    102  * There is an assumption that any flavour less than AUTH_NULL is
    103  * invalid.
    104  */
    105 enum auth_stat
    106 _authenticate(rqst, msg)
    107 	struct svc_req *rqst;
    108 	struct rpc_msg *msg;
    109 {
    110 	int cred_flavor;
    111 	struct authsvc *asp;
    112 	enum auth_stat dummy;
    113 #ifdef _REENTRANT
    114 	extern mutex_t authsvc_lock;
    115 #endif
    116 
    117 	_DIAGASSERT(rqst != NULL);
    118 	_DIAGASSERT(msg != NULL);
    119 
    120 /* VARIABLES PROTECTED BY authsvc_lock: asp, Auths */
    121 
    122 	rqst->rq_cred = msg->rm_call.cb_cred;
    123 	rqst->rq_xprt->xp_verf.oa_flavor = _null_auth.oa_flavor;
    124 	rqst->rq_xprt->xp_verf.oa_length = 0;
    125 	cred_flavor = rqst->rq_cred.oa_flavor;
    126 	switch (cred_flavor) {
    127 	case AUTH_NULL:
    128 		dummy = _svcauth_null(rqst, msg);
    129 		return (dummy);
    130 	case AUTH_SYS:
    131 		dummy = _svcauth_unix(rqst, msg);
    132 		return (dummy);
    133 	case AUTH_SHORT:
    134 		dummy = _svcauth_short(rqst, msg);
    135 		return (dummy);
    136 #if 0
    137 	case AUTH_DES:
    138 		dummy = __svcauth_des(rqst, msg);
    139 		return (dummy);
    140 #endif
    141 	default:
    142 		break;
    143 	}
    144 
    145 	/* flavor doesn't match any of the builtin types, so try new ones */
    146 	mutex_lock(&authsvc_lock);
    147 	for (asp = Auths; asp; asp = asp->next) {
    148 		if (asp->flavor == cred_flavor) {
    149 			enum auth_stat as;
    150 
    151 			as = (*asp->handler)(rqst, msg);
    152 			mutex_unlock(&authsvc_lock);
    153 			return (as);
    154 		}
    155 	}
    156 	mutex_unlock(&authsvc_lock);
    157 
    158 	return (AUTH_REJECTEDCRED);
    159 }
    160 
    161 /*ARGSUSED*/
    162 enum auth_stat
    163 _svcauth_null(rqst, msg)
    164 	struct svc_req *rqst;
    165 	struct rpc_msg *msg;
    166 {
    167 	return (AUTH_OK);
    168 }
    169 
    170 /*
    171  *  Allow the rpc service to register new authentication types that it is
    172  *  prepared to handle.  When an authentication flavor is registered,
    173  *  the flavor is checked against already registered values.  If not
    174  *  registered, then a new Auths entry is added on the list.
    175  *
    176  *  There is no provision to delete a registration once registered.
    177  *
    178  *  This routine returns:
    179  *	 0 if registration successful
    180  *	 1 if flavor already registered
    181  *	-1 if can't register (errno set)
    182  */
    183 
    184 int
    185 svc_auth_reg(cred_flavor, handler)
    186 	int cred_flavor;
    187 	enum auth_stat (*handler) __P((struct svc_req *, struct rpc_msg *));
    188 {
    189 	struct authsvc *asp;
    190 #ifdef _REENTRANT
    191 	extern mutex_t authsvc_lock;
    192 #endif
    193 
    194 	switch (cred_flavor) {
    195 	    case AUTH_NULL:
    196 	    case AUTH_SYS:
    197 	    case AUTH_SHORT:
    198 #if 0
    199 	    case AUTH_DES:
    200 #endif
    201 		/* already registered */
    202 		return (1);
    203 
    204 	    default:
    205 		mutex_lock(&authsvc_lock);
    206 		for (asp = Auths; asp; asp = asp->next) {
    207 			if (asp->flavor == cred_flavor) {
    208 				/* already registered */
    209 				mutex_unlock(&authsvc_lock);
    210 				return (1);
    211 			}
    212 		}
    213 
    214 		/* this is a new one, so go ahead and register it */
    215 		asp = mem_alloc(sizeof (*asp));
    216 		if (asp == NULL) {
    217 			mutex_unlock(&authsvc_lock);
    218 			return (-1);
    219 		}
    220 		asp->flavor = cred_flavor;
    221 		asp->handler = handler;
    222 		asp->next = Auths;
    223 		Auths = asp;
    224 		mutex_unlock(&authsvc_lock);
    225 		break;
    226 	}
    227 	return (0);
    228 }
    229