Home | History | Annotate | Line # | Download | only in rpc
svc_auth.c revision 1.13.2.1
      1 /*	$NetBSD: svc_auth.c,v 1.13.2.1 2001/08/08 16:13:45 nathanw Exp $	*/
      2 
      3 /*
      4  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
      5  * unrestricted use provided that this legend is included on all tape
      6  * media and as a part of the software program in whole or part.  Users
      7  * may copy or modify Sun RPC without charge, but are not authorized
      8  * to license or distribute it to anyone else except as part of a product or
      9  * program developed by the user.
     10  *
     11  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
     12  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
     13  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
     14  *
     15  * Sun RPC is provided with no support and without any obligation on the
     16  * part of Sun Microsystems, Inc. to assist in its use, correction,
     17  * modification or enhancement.
     18  *
     19  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
     20  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
     21  * OR ANY PART THEREOF.
     22  *
     23  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
     24  * or profits or other special, indirect and consequential damages, even if
     25  * Sun has been advised of the possibility of such damages.
     26  *
     27  * Sun Microsystems, Inc.
     28  * 2550 Garcia Avenue
     29  * Mountain View, California  94043
     30  */
     31 /*
     32  * Copyright (c) 1986-1991 by Sun Microsystems Inc.
     33  */
     34 
     35 /* #ident	"@(#)svc_auth.c	1.16	94/04/24 SMI" */
     36 
     37 #if 0
     38 #if !defined(lint) && defined(SCCSIDS)
     39 static char sccsid[] = "@(#)svc_auth.c 1.26 89/02/07 Copyr 1984 Sun Micro";
     40 #endif
     41 #endif
     42 
     43 /*
     44  * svc_auth.c, Server-side rpc authenticator interface.
     45  *
     46  */
     47 
     48 #include "namespace.h"
     49 #include "reentrant.h"
     50 #include <sys/types.h>
     51 #include <rpc/rpc.h>
     52 #include <assert.h>
     53 #include <stdlib.h>
     54 
     55 #ifdef __weak_alias
     56 __weak_alias(svc_auth_reg,_svc_auth_reg)
     57 #endif
     58 
     59 /*
     60  * svcauthsw is the bdevsw of server side authentication.
     61  *
     62  * Server side authenticators are called from authenticate by
     63  * using the client auth struct flavor field to index into svcauthsw.
     64  * The server auth flavors must implement a routine that looks
     65  * like:
     66  *
     67  *	enum auth_stat
     68  *	flavorx_auth(rqst, msg)
     69  *		struct svc_req *rqst;
     70  *		struct rpc_msg *msg;
     71  *
     72  */
     73 
     74 /* declarations to allow servers to specify new authentication flavors */
     75 struct authsvc {
     76 	int	flavor;
     77 	enum	auth_stat (*handler) __P((struct svc_req *, struct rpc_msg *));
     78 	struct	authsvc	  *next;
     79 };
     80 static struct authsvc *Auths = NULL;
     81 
     82 /*
     83  * The call rpc message, msg has been obtained from the wire.  The msg contains
     84  * the raw form of credentials and verifiers.  authenticate returns AUTH_OK
     85  * if the msg is successfully authenticated.  If AUTH_OK then the routine also
     86  * does the following things:
     87  * set rqst->rq_xprt->verf to the appropriate response verifier;
     88  * sets rqst->rq_client_cred to the "cooked" form of the credentials.
     89  *
     90  * NB: rqst->rq_cxprt->verf must be pre-alloctaed;
     91  * its length is set appropriately.
     92  *
     93  * The caller still owns and is responsible for msg->u.cmb.cred and
     94  * msg->u.cmb.verf.  The authentication system retains ownership of
     95  * rqst->rq_client_cred, the cooked credentials.
     96  *
     97  * There is an assumption that any flavour less than AUTH_NULL is
     98  * invalid.
     99  */
    100 enum auth_stat
    101 _authenticate(rqst, msg)
    102 	struct svc_req *rqst;
    103 	struct rpc_msg *msg;
    104 {
    105 	int cred_flavor;
    106 	struct authsvc *asp;
    107 	enum auth_stat dummy;
    108 #ifdef _REENTRANT
    109 	extern mutex_t authsvc_lock;
    110 #endif
    111 
    112 	_DIAGASSERT(rqst != NULL);
    113 	_DIAGASSERT(msg != NULL);
    114 
    115 /* VARIABLES PROTECTED BY authsvc_lock: asp, Auths */
    116 
    117 	rqst->rq_cred = msg->rm_call.cb_cred;
    118 	rqst->rq_xprt->xp_verf.oa_flavor = _null_auth.oa_flavor;
    119 	rqst->rq_xprt->xp_verf.oa_length = 0;
    120 	cred_flavor = rqst->rq_cred.oa_flavor;
    121 	switch (cred_flavor) {
    122 	case AUTH_NULL:
    123 		dummy = _svcauth_null(rqst, msg);
    124 		return (dummy);
    125 	case AUTH_SYS:
    126 		dummy = _svcauth_unix(rqst, msg);
    127 		return (dummy);
    128 	case AUTH_SHORT:
    129 		dummy = _svcauth_short(rqst, msg);
    130 		return (dummy);
    131 #if 0
    132 	case AUTH_DES:
    133 		dummy = __svcauth_des(rqst, msg);
    134 		return (dummy);
    135 #endif
    136 	default:
    137 		break;
    138 	}
    139 
    140 	/* flavor doesn't match any of the builtin types, so try new ones */
    141 	mutex_lock(&authsvc_lock);
    142 	for (asp = Auths; asp; asp = asp->next) {
    143 		if (asp->flavor == cred_flavor) {
    144 			enum auth_stat as;
    145 
    146 			as = (*asp->handler)(rqst, msg);
    147 			mutex_unlock(&authsvc_lock);
    148 			return (as);
    149 		}
    150 	}
    151 	mutex_unlock(&authsvc_lock);
    152 
    153 	return (AUTH_REJECTEDCRED);
    154 }
    155 
    156 /*ARGSUSED*/
    157 enum auth_stat
    158 _svcauth_null(rqst, msg)
    159 	struct svc_req *rqst;
    160 	struct rpc_msg *msg;
    161 {
    162 	return (AUTH_OK);
    163 }
    164 
    165 /*
    166  *  Allow the rpc service to register new authentication types that it is
    167  *  prepared to handle.  When an authentication flavor is registered,
    168  *  the flavor is checked against already registered values.  If not
    169  *  registered, then a new Auths entry is added on the list.
    170  *
    171  *  There is no provision to delete a registration once registered.
    172  *
    173  *  This routine returns:
    174  *	 0 if registration successful
    175  *	 1 if flavor already registered
    176  *	-1 if can't register (errno set)
    177  */
    178 
    179 int
    180 svc_auth_reg(cred_flavor, handler)
    181 	int cred_flavor;
    182 	enum auth_stat (*handler) __P((struct svc_req *, struct rpc_msg *));
    183 {
    184 	struct authsvc *asp;
    185 #ifdef _REENTRANT
    186 	extern mutex_t authsvc_lock;
    187 #endif
    188 
    189 	switch (cred_flavor) {
    190 	    case AUTH_NULL:
    191 	    case AUTH_SYS:
    192 	    case AUTH_SHORT:
    193 #if 0
    194 	    case AUTH_DES:
    195 #endif
    196 		/* already registered */
    197 		return (1);
    198 
    199 	    default:
    200 		mutex_lock(&authsvc_lock);
    201 		for (asp = Auths; asp; asp = asp->next) {
    202 			if (asp->flavor == cred_flavor) {
    203 				/* already registered */
    204 				mutex_unlock(&authsvc_lock);
    205 				return (1);
    206 			}
    207 		}
    208 
    209 		/* this is a new one, so go ahead and register it */
    210 		asp = mem_alloc(sizeof (*asp));
    211 		if (asp == NULL) {
    212 			mutex_unlock(&authsvc_lock);
    213 			return (-1);
    214 		}
    215 		asp->flavor = cred_flavor;
    216 		asp->handler = handler;
    217 		asp->next = Auths;
    218 		Auths = asp;
    219 		mutex_unlock(&authsvc_lock);
    220 		break;
    221 	}
    222 	return (0);
    223 }
    224