xref: /freebsd/lib/libc/rpc/svc_auth.c (revision 235baf269e492159336326a12da1ca09fbba85db)
18360efbdSAlfred Perlstein /*	$NetBSD: svc_auth.c,v 1.12 2000/07/06 03:10:35 christos Exp $	*/
28360efbdSAlfred Perlstein 
399064799SGarrett Wollman /*
499064799SGarrett Wollman  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
599064799SGarrett Wollman  * unrestricted use provided that this legend is included on all tape
699064799SGarrett Wollman  * media and as a part of the software program in whole or part.  Users
799064799SGarrett Wollman  * may copy or modify Sun RPC without charge, but are not authorized
899064799SGarrett Wollman  * to license or distribute it to anyone else except as part of a product or
999064799SGarrett Wollman  * program developed by the user.
1099064799SGarrett Wollman  *
1199064799SGarrett Wollman  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
1299064799SGarrett Wollman  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
1399064799SGarrett Wollman  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
1499064799SGarrett Wollman  *
1599064799SGarrett Wollman  * Sun RPC is provided with no support and without any obligation on the
1699064799SGarrett Wollman  * part of Sun Microsystems, Inc. to assist in its use, correction,
1799064799SGarrett Wollman  * modification or enhancement.
1899064799SGarrett Wollman  *
1999064799SGarrett Wollman  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
2099064799SGarrett Wollman  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
2199064799SGarrett Wollman  * OR ANY PART THEREOF.
2299064799SGarrett Wollman  *
2399064799SGarrett Wollman  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
2499064799SGarrett Wollman  * or profits or other special, indirect and consequential damages, even if
2599064799SGarrett Wollman  * Sun has been advised of the possibility of such damages.
2699064799SGarrett Wollman  *
2799064799SGarrett Wollman  * Sun Microsystems, Inc.
2899064799SGarrett Wollman  * 2550 Garcia Avenue
2999064799SGarrett Wollman  * Mountain View, California  94043
3099064799SGarrett Wollman  */
31ad133ed6SBill Paul /*
32ad133ed6SBill Paul  * Copyright (c) 1986-1991 by Sun Microsystems Inc.
33ad133ed6SBill Paul  */
3499064799SGarrett Wollman 
35a986ef57SDavid E. O'Brien #if defined(LIBC_SCCS) && !defined(lint)
36a986ef57SDavid E. O'Brien #ident	"@(#)svc_auth.c	1.16	94/04/24 SMI"
37ad133ed6SBill Paul static char sccsid[] = "@(#)svc_auth.c 1.26 89/02/07 Copyr 1984 Sun Micro";
38c4473420SPeter Wemm #endif
39d3d20c82SDavid E. O'Brien #include <sys/cdefs.h>
40d3d20c82SDavid E. O'Brien __FBSDID("$FreeBSD$");
4199064799SGarrett Wollman 
4299064799SGarrett Wollman /*
43ad133ed6SBill Paul  * svc_auth.c, Server-side rpc authenticator interface.
4499064799SGarrett Wollman  *
4599064799SGarrett Wollman  */
4699064799SGarrett Wollman 
478360efbdSAlfred Perlstein #include "namespace.h"
489f5afc13SIan Dowse #include "reentrant.h"
49ad133ed6SBill Paul #include <sys/types.h>
508360efbdSAlfred Perlstein #include <rpc/rpc.h>
518360efbdSAlfred Perlstein #include <stdlib.h>
528360efbdSAlfred Perlstein #include "un-namespace.h"
53235baf26SDaniel Eischen #include "mt_misc.h"
5499064799SGarrett Wollman 
5599064799SGarrett Wollman /*
5699064799SGarrett Wollman  * svcauthsw is the bdevsw of server side authentication.
5799064799SGarrett Wollman  *
5899064799SGarrett Wollman  * Server side authenticators are called from authenticate by
5999064799SGarrett Wollman  * using the client auth struct flavor field to index into svcauthsw.
6099064799SGarrett Wollman  * The server auth flavors must implement a routine that looks
6199064799SGarrett Wollman  * like:
6299064799SGarrett Wollman  *
6399064799SGarrett Wollman  *	enum auth_stat
6499064799SGarrett Wollman  *	flavorx_auth(rqst, msg)
658360efbdSAlfred Perlstein  *		struct svc_req *rqst;
668360efbdSAlfred Perlstein  *		struct rpc_msg *msg;
6799064799SGarrett Wollman  *
6899064799SGarrett Wollman  */
6999064799SGarrett Wollman 
70ad133ed6SBill Paul /* declarations to allow servers to specify new authentication flavors */
71ad133ed6SBill Paul struct authsvc {
72ad133ed6SBill Paul 	int	flavor;
73c05ac53bSDavid E. O'Brien 	enum	auth_stat (*handler)(struct svc_req *, struct rpc_msg *);
74ad133ed6SBill Paul 	struct	authsvc	  *next;
7599064799SGarrett Wollman };
76ad133ed6SBill Paul static struct authsvc *Auths = NULL;
7799064799SGarrett Wollman 
7899064799SGarrett Wollman /*
7999064799SGarrett Wollman  * The call rpc message, msg has been obtained from the wire.  The msg contains
8099064799SGarrett Wollman  * the raw form of credentials and verifiers.  authenticate returns AUTH_OK
8199064799SGarrett Wollman  * if the msg is successfully authenticated.  If AUTH_OK then the routine also
8299064799SGarrett Wollman  * does the following things:
8399064799SGarrett Wollman  * set rqst->rq_xprt->verf to the appropriate response verifier;
8499064799SGarrett Wollman  * sets rqst->rq_client_cred to the "cooked" form of the credentials.
8599064799SGarrett Wollman  *
8699064799SGarrett Wollman  * NB: rqst->rq_cxprt->verf must be pre-alloctaed;
8799064799SGarrett Wollman  * its length is set appropriately.
8899064799SGarrett Wollman  *
8999064799SGarrett Wollman  * The caller still owns and is responsible for msg->u.cmb.cred and
9099064799SGarrett Wollman  * msg->u.cmb.verf.  The authentication system retains ownership of
9199064799SGarrett Wollman  * rqst->rq_client_cred, the cooked credentials.
9299064799SGarrett Wollman  *
9399064799SGarrett Wollman  * There is an assumption that any flavour less than AUTH_NULL is
9499064799SGarrett Wollman  * invalid.
9599064799SGarrett Wollman  */
9699064799SGarrett Wollman enum auth_stat
9799064799SGarrett Wollman _authenticate(rqst, msg)
988360efbdSAlfred Perlstein 	struct svc_req *rqst;
9999064799SGarrett Wollman 	struct rpc_msg *msg;
10099064799SGarrett Wollman {
1018360efbdSAlfred Perlstein 	int cred_flavor;
1028360efbdSAlfred Perlstein 	struct authsvc *asp;
1038360efbdSAlfred Perlstein 	enum auth_stat dummy;
1048360efbdSAlfred Perlstein 
1058360efbdSAlfred Perlstein /* VARIABLES PROTECTED BY authsvc_lock: asp, Auths */
10699064799SGarrett Wollman 
10799064799SGarrett Wollman 	rqst->rq_cred = msg->rm_call.cb_cred;
10899064799SGarrett Wollman 	rqst->rq_xprt->xp_verf.oa_flavor = _null_auth.oa_flavor;
10999064799SGarrett Wollman 	rqst->rq_xprt->xp_verf.oa_length = 0;
11099064799SGarrett Wollman 	cred_flavor = rqst->rq_cred.oa_flavor;
111ad133ed6SBill Paul 	switch (cred_flavor) {
112ad133ed6SBill Paul 	case AUTH_NULL:
1138360efbdSAlfred Perlstein 		dummy = _svcauth_null(rqst, msg);
1148360efbdSAlfred Perlstein 		return (dummy);
1158360efbdSAlfred Perlstein 	case AUTH_SYS:
1168360efbdSAlfred Perlstein 		dummy = _svcauth_unix(rqst, msg);
1178360efbdSAlfred Perlstein 		return (dummy);
118ad133ed6SBill Paul 	case AUTH_SHORT:
1198360efbdSAlfred Perlstein 		dummy = _svcauth_short(rqst, msg);
1208360efbdSAlfred Perlstein 		return (dummy);
121ad133ed6SBill Paul #ifdef DES_BUILTIN
122ad133ed6SBill Paul 	case AUTH_DES:
1238360efbdSAlfred Perlstein 		dummy = _svcauth_des(rqst, msg);
1248360efbdSAlfred Perlstein 		return (dummy);
125ad133ed6SBill Paul #endif
1268360efbdSAlfred Perlstein 	default:
1278360efbdSAlfred Perlstein 		break;
128ad133ed6SBill Paul 	}
129ad133ed6SBill Paul 
130ad133ed6SBill Paul 	/* flavor doesn't match any of the builtin types, so try new ones */
1318360efbdSAlfred Perlstein 	mutex_lock(&authsvc_lock);
132ad133ed6SBill Paul 	for (asp = Auths; asp; asp = asp->next) {
133ad133ed6SBill Paul 		if (asp->flavor == cred_flavor) {
134ad133ed6SBill Paul 			enum auth_stat as;
135ad133ed6SBill Paul 
136ad133ed6SBill Paul 			as = (*asp->handler)(rqst, msg);
1378360efbdSAlfred Perlstein 			mutex_unlock(&authsvc_lock);
138ad133ed6SBill Paul 			return (as);
139ad133ed6SBill Paul 		}
14099064799SGarrett Wollman 	}
1418360efbdSAlfred Perlstein 	mutex_unlock(&authsvc_lock);
14299064799SGarrett Wollman 
14399064799SGarrett Wollman 	return (AUTH_REJECTEDCRED);
14499064799SGarrett Wollman }
14599064799SGarrett Wollman 
146ad133ed6SBill Paul /*ARGSUSED*/
14799064799SGarrett Wollman enum auth_stat
148ad133ed6SBill Paul _svcauth_null(rqst, msg)
149ad133ed6SBill Paul 	struct svc_req *rqst;
150ad133ed6SBill Paul 	struct rpc_msg *msg;
15199064799SGarrett Wollman {
15299064799SGarrett Wollman 	return (AUTH_OK);
15399064799SGarrett Wollman }
154ad133ed6SBill Paul 
155ad133ed6SBill Paul /*
156ad133ed6SBill Paul  *  Allow the rpc service to register new authentication types that it is
157ad133ed6SBill Paul  *  prepared to handle.  When an authentication flavor is registered,
158ad133ed6SBill Paul  *  the flavor is checked against already registered values.  If not
159ad133ed6SBill Paul  *  registered, then a new Auths entry is added on the list.
160ad133ed6SBill Paul  *
161ad133ed6SBill Paul  *  There is no provision to delete a registration once registered.
162ad133ed6SBill Paul  *
163ad133ed6SBill Paul  *  This routine returns:
164ad133ed6SBill Paul  *	 0 if registration successful
165ad133ed6SBill Paul  *	 1 if flavor already registered
166ad133ed6SBill Paul  *	-1 if can't register (errno set)
167ad133ed6SBill Paul  */
168ad133ed6SBill Paul 
169ad133ed6SBill Paul int
170ad133ed6SBill Paul svc_auth_reg(cred_flavor, handler)
1718360efbdSAlfred Perlstein 	int cred_flavor;
172c05ac53bSDavid E. O'Brien 	enum auth_stat (*handler)(struct svc_req *, struct rpc_msg *);
173ad133ed6SBill Paul {
1748360efbdSAlfred Perlstein 	struct authsvc *asp;
175ad133ed6SBill Paul 
176ad133ed6SBill Paul 	switch (cred_flavor) {
177ad133ed6SBill Paul 	    case AUTH_NULL:
1788360efbdSAlfred Perlstein 	    case AUTH_SYS:
179ad133ed6SBill Paul 	    case AUTH_SHORT:
180ad133ed6SBill Paul #ifdef DES_BUILTIN
181ad133ed6SBill Paul 	    case AUTH_DES:
182ad133ed6SBill Paul #endif
183ad133ed6SBill Paul 		/* already registered */
184ad133ed6SBill Paul 		return (1);
185ad133ed6SBill Paul 
186ad133ed6SBill Paul 	    default:
1878360efbdSAlfred Perlstein 		mutex_lock(&authsvc_lock);
188ad133ed6SBill Paul 		for (asp = Auths; asp; asp = asp->next) {
189ad133ed6SBill Paul 			if (asp->flavor == cred_flavor) {
190ad133ed6SBill Paul 				/* already registered */
1918360efbdSAlfred Perlstein 				mutex_unlock(&authsvc_lock);
192ad133ed6SBill Paul 				return (1);
193ad133ed6SBill Paul 			}
194ad133ed6SBill Paul 		}
195ad133ed6SBill Paul 
196ad133ed6SBill Paul 		/* this is a new one, so go ahead and register it */
1978360efbdSAlfred Perlstein 		asp = mem_alloc(sizeof (*asp));
198ad133ed6SBill Paul 		if (asp == NULL) {
1998360efbdSAlfred Perlstein 			mutex_unlock(&authsvc_lock);
200ad133ed6SBill Paul 			return (-1);
201ad133ed6SBill Paul 		}
202ad133ed6SBill Paul 		asp->flavor = cred_flavor;
203ad133ed6SBill Paul 		asp->handler = handler;
204ad133ed6SBill Paul 		asp->next = Auths;
205ad133ed6SBill Paul 		Auths = asp;
2068360efbdSAlfred Perlstein 		mutex_unlock(&authsvc_lock);
207ad133ed6SBill Paul 		break;
208ad133ed6SBill Paul 	}
209ad133ed6SBill Paul 	return (0);
210ad133ed6SBill Paul }
211