xref: /freebsd/lib/libc/rpc/svc_auth.c (revision d3d20c826754c093694818e42835c4ce4591a13e)
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 
358360efbdSAlfred Perlstein /* #ident	"@(#)svc_auth.c	1.16	94/04/24 SMI" */
36ad133ed6SBill Paul 
378360efbdSAlfred Perlstein #if !defined(lint) && defined(SCCSIDS)
38ad133ed6SBill Paul static char sccsid[] = "@(#)svc_auth.c 1.26 89/02/07 Copyr 1984 Sun Micro";
39c4473420SPeter Wemm #endif
40d3d20c82SDavid E. O'Brien #include <sys/cdefs.h>
41d3d20c82SDavid E. O'Brien __FBSDID("$FreeBSD$");
4299064799SGarrett Wollman 
4399064799SGarrett Wollman /*
44ad133ed6SBill Paul  * svc_auth.c, Server-side rpc authenticator interface.
4599064799SGarrett Wollman  *
4699064799SGarrett Wollman  */
4799064799SGarrett Wollman 
488360efbdSAlfred Perlstein #include "namespace.h"
499f5afc13SIan Dowse #include "reentrant.h"
50ad133ed6SBill Paul #include <sys/types.h>
518360efbdSAlfred Perlstein #include <rpc/rpc.h>
528360efbdSAlfred Perlstein #include <stdlib.h>
538360efbdSAlfred Perlstein #include "un-namespace.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 	extern mutex_t authsvc_lock;
1058360efbdSAlfred Perlstein 
1068360efbdSAlfred Perlstein /* VARIABLES PROTECTED BY authsvc_lock: asp, Auths */
10799064799SGarrett Wollman 
10899064799SGarrett Wollman 	rqst->rq_cred = msg->rm_call.cb_cred;
10999064799SGarrett Wollman 	rqst->rq_xprt->xp_verf.oa_flavor = _null_auth.oa_flavor;
11099064799SGarrett Wollman 	rqst->rq_xprt->xp_verf.oa_length = 0;
11199064799SGarrett Wollman 	cred_flavor = rqst->rq_cred.oa_flavor;
112ad133ed6SBill Paul 	switch (cred_flavor) {
113ad133ed6SBill Paul 	case AUTH_NULL:
1148360efbdSAlfred Perlstein 		dummy = _svcauth_null(rqst, msg);
1158360efbdSAlfred Perlstein 		return (dummy);
1168360efbdSAlfred Perlstein 	case AUTH_SYS:
1178360efbdSAlfred Perlstein 		dummy = _svcauth_unix(rqst, msg);
1188360efbdSAlfred Perlstein 		return (dummy);
119ad133ed6SBill Paul 	case AUTH_SHORT:
1208360efbdSAlfred Perlstein 		dummy = _svcauth_short(rqst, msg);
1218360efbdSAlfred Perlstein 		return (dummy);
122ad133ed6SBill Paul #ifdef DES_BUILTIN
123ad133ed6SBill Paul 	case AUTH_DES:
1248360efbdSAlfred Perlstein 		dummy = _svcauth_des(rqst, msg);
1258360efbdSAlfred Perlstein 		return (dummy);
126ad133ed6SBill Paul #endif
1278360efbdSAlfred Perlstein 	default:
1288360efbdSAlfred Perlstein 		break;
129ad133ed6SBill Paul 	}
130ad133ed6SBill Paul 
131ad133ed6SBill Paul 	/* flavor doesn't match any of the builtin types, so try new ones */
1328360efbdSAlfred Perlstein 	mutex_lock(&authsvc_lock);
133ad133ed6SBill Paul 	for (asp = Auths; asp; asp = asp->next) {
134ad133ed6SBill Paul 		if (asp->flavor == cred_flavor) {
135ad133ed6SBill Paul 			enum auth_stat as;
136ad133ed6SBill Paul 
137ad133ed6SBill Paul 			as = (*asp->handler)(rqst, msg);
1388360efbdSAlfred Perlstein 			mutex_unlock(&authsvc_lock);
139ad133ed6SBill Paul 			return (as);
140ad133ed6SBill Paul 		}
14199064799SGarrett Wollman 	}
1428360efbdSAlfred Perlstein 	mutex_unlock(&authsvc_lock);
14399064799SGarrett Wollman 
14499064799SGarrett Wollman 	return (AUTH_REJECTEDCRED);
14599064799SGarrett Wollman }
14699064799SGarrett Wollman 
147ad133ed6SBill Paul /*ARGSUSED*/
14899064799SGarrett Wollman enum auth_stat
149ad133ed6SBill Paul _svcauth_null(rqst, msg)
150ad133ed6SBill Paul 	struct svc_req *rqst;
151ad133ed6SBill Paul 	struct rpc_msg *msg;
15299064799SGarrett Wollman {
15399064799SGarrett Wollman 	return (AUTH_OK);
15499064799SGarrett Wollman }
155ad133ed6SBill Paul 
156ad133ed6SBill Paul /*
157ad133ed6SBill Paul  *  Allow the rpc service to register new authentication types that it is
158ad133ed6SBill Paul  *  prepared to handle.  When an authentication flavor is registered,
159ad133ed6SBill Paul  *  the flavor is checked against already registered values.  If not
160ad133ed6SBill Paul  *  registered, then a new Auths entry is added on the list.
161ad133ed6SBill Paul  *
162ad133ed6SBill Paul  *  There is no provision to delete a registration once registered.
163ad133ed6SBill Paul  *
164ad133ed6SBill Paul  *  This routine returns:
165ad133ed6SBill Paul  *	 0 if registration successful
166ad133ed6SBill Paul  *	 1 if flavor already registered
167ad133ed6SBill Paul  *	-1 if can't register (errno set)
168ad133ed6SBill Paul  */
169ad133ed6SBill Paul 
170ad133ed6SBill Paul int
171ad133ed6SBill Paul svc_auth_reg(cred_flavor, handler)
1728360efbdSAlfred Perlstein 	int cred_flavor;
173c05ac53bSDavid E. O'Brien 	enum auth_stat (*handler)(struct svc_req *, struct rpc_msg *);
174ad133ed6SBill Paul {
1758360efbdSAlfred Perlstein 	struct authsvc *asp;
1768360efbdSAlfred Perlstein 	extern mutex_t authsvc_lock;
177ad133ed6SBill Paul 
178ad133ed6SBill Paul 	switch (cred_flavor) {
179ad133ed6SBill Paul 	    case AUTH_NULL:
1808360efbdSAlfred Perlstein 	    case AUTH_SYS:
181ad133ed6SBill Paul 	    case AUTH_SHORT:
182ad133ed6SBill Paul #ifdef DES_BUILTIN
183ad133ed6SBill Paul 	    case AUTH_DES:
184ad133ed6SBill Paul #endif
185ad133ed6SBill Paul 		/* already registered */
186ad133ed6SBill Paul 		return (1);
187ad133ed6SBill Paul 
188ad133ed6SBill Paul 	    default:
1898360efbdSAlfred Perlstein 		mutex_lock(&authsvc_lock);
190ad133ed6SBill Paul 		for (asp = Auths; asp; asp = asp->next) {
191ad133ed6SBill Paul 			if (asp->flavor == cred_flavor) {
192ad133ed6SBill Paul 				/* already registered */
1938360efbdSAlfred Perlstein 				mutex_unlock(&authsvc_lock);
194ad133ed6SBill Paul 				return (1);
195ad133ed6SBill Paul 			}
196ad133ed6SBill Paul 		}
197ad133ed6SBill Paul 
198ad133ed6SBill Paul 		/* this is a new one, so go ahead and register it */
1998360efbdSAlfred Perlstein 		asp = mem_alloc(sizeof (*asp));
200ad133ed6SBill Paul 		if (asp == NULL) {
2018360efbdSAlfred Perlstein 			mutex_unlock(&authsvc_lock);
202ad133ed6SBill Paul 			return (-1);
203ad133ed6SBill Paul 		}
204ad133ed6SBill Paul 		asp->flavor = cred_flavor;
205ad133ed6SBill Paul 		asp->handler = handler;
206ad133ed6SBill Paul 		asp->next = Auths;
207ad133ed6SBill Paul 		Auths = asp;
2088360efbdSAlfred Perlstein 		mutex_unlock(&authsvc_lock);
209ad133ed6SBill Paul 		break;
210ad133ed6SBill Paul 	}
211ad133ed6SBill Paul 	return (0);
212ad133ed6SBill Paul }
213