xref: /illumos-gate/usr/src/lib/libnsl/rpc/svc_auth.c (revision 1da57d55)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*61961e0fSrobinson  */
22*61961e0fSrobinson 
23*61961e0fSrobinson /*
24*61961e0fSrobinson  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
257c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
287c478bd9Sstevel@tonic-gate /* All Rights Reserved */
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley
317c478bd9Sstevel@tonic-gate  * 4.3 BSD under license from the Regents of the University of
327c478bd9Sstevel@tonic-gate  * California.
337c478bd9Sstevel@tonic-gate  */
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate /*
367c478bd9Sstevel@tonic-gate  * svc_auth.c, Server-side rpc authenticator interface.
377c478bd9Sstevel@tonic-gate  *
387c478bd9Sstevel@tonic-gate  */
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate #include "mt.h"
417c478bd9Sstevel@tonic-gate #include "rpc_mt.h"
427c478bd9Sstevel@tonic-gate #include <rpc/rpc.h>
437c478bd9Sstevel@tonic-gate #include <sys/types.h>
447c478bd9Sstevel@tonic-gate #include <stdlib.h>
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate /*
477c478bd9Sstevel@tonic-gate  * svcauthsw is the bdevsw of server side authentication.
487c478bd9Sstevel@tonic-gate  *
497c478bd9Sstevel@tonic-gate  * Server side authenticators are called from authenticate by
507c478bd9Sstevel@tonic-gate  * using the client auth struct flavor field to index into svcauthsw.
517c478bd9Sstevel@tonic-gate  * The server auth flavors must implement a routine that looks
527c478bd9Sstevel@tonic-gate  * like:
537c478bd9Sstevel@tonic-gate  *
547c478bd9Sstevel@tonic-gate  *	enum auth_stat
557c478bd9Sstevel@tonic-gate  *	flavorx_auth(rqst, msg)
567c478bd9Sstevel@tonic-gate  *		struct svc_req *rqst;
577c478bd9Sstevel@tonic-gate  *		struct rpc_msg *msg;
587c478bd9Sstevel@tonic-gate  *
597c478bd9Sstevel@tonic-gate  * The RPCSEC_GSS flavor is an exception.  Its routine takes an
607c478bd9Sstevel@tonic-gate  * additional boolean parameter that gets set to TRUE when the call
617c478bd9Sstevel@tonic-gate  * is not to be dispatched to the server.
627c478bd9Sstevel@tonic-gate  */
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate enum auth_stat __svcauth_null();	/* no authentication */
657c478bd9Sstevel@tonic-gate enum auth_stat __svcauth_sys();		/* (system) unix style (uid, gids) */
667c478bd9Sstevel@tonic-gate enum auth_stat __svcauth_short();	/* short hand unix style */
677c478bd9Sstevel@tonic-gate enum auth_stat __svcauth_des();		/* des style */
687c478bd9Sstevel@tonic-gate enum auth_stat __svcauth_loopback();	/* (loopback) unix style (uid, gids) */
697c478bd9Sstevel@tonic-gate extern enum auth_stat __svcrpcsec_gss();	/* GSS style */
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate /* declarations to allow servers to specify new authentication flavors */
727c478bd9Sstevel@tonic-gate struct authsvc {
737c478bd9Sstevel@tonic-gate 	int	flavor;
747c478bd9Sstevel@tonic-gate 	enum	auth_stat (*handler)();
757c478bd9Sstevel@tonic-gate 	struct	authsvc	  *next;
767c478bd9Sstevel@tonic-gate };
777c478bd9Sstevel@tonic-gate static struct authsvc *Auths = NULL;
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate /*
807c478bd9Sstevel@tonic-gate  * The call rpc message, msg has been obtained from the wire.  The msg contains
817c478bd9Sstevel@tonic-gate  * the raw form of credentials and verifiers.  no_dispatch is used and
827c478bd9Sstevel@tonic-gate  * dereferenced in subsequent gss function calls.  authenticate returns AUTH_OK
837c478bd9Sstevel@tonic-gate  * if the msg is successfully authenticated.  If AUTH_OK then the routine also
847c478bd9Sstevel@tonic-gate  * does the following things:
857c478bd9Sstevel@tonic-gate  * set rqst->rq_xprt->verf to the appropriate response verifier;
867c478bd9Sstevel@tonic-gate  * sets rqst->rq_client_cred to the "cooked" form of the credentials.
877c478bd9Sstevel@tonic-gate  *
887c478bd9Sstevel@tonic-gate  * NB: rqst->rq_cxprt->verf must be pre-alloctaed;
897c478bd9Sstevel@tonic-gate  * its length is set appropriately.
907c478bd9Sstevel@tonic-gate  *
917c478bd9Sstevel@tonic-gate  * The caller still owns and is responsible for msg->u.cmb.cred and
927c478bd9Sstevel@tonic-gate  * msg->u.cmb.verf.  The authentication system retains ownership of
937c478bd9Sstevel@tonic-gate  * rqst->rq_client_cred, the cooked credentials.
947c478bd9Sstevel@tonic-gate  *
957c478bd9Sstevel@tonic-gate  * There is an assumption that any flavour less than AUTH_NULL is
967c478bd9Sstevel@tonic-gate  * invalid.
977c478bd9Sstevel@tonic-gate  */
987c478bd9Sstevel@tonic-gate enum auth_stat
__gss_authenticate(struct svc_req * rqst,struct rpc_msg * msg,bool_t * no_dispatch)99*61961e0fSrobinson __gss_authenticate(struct svc_req *rqst, struct rpc_msg *msg,
100*61961e0fSrobinson 							bool_t *no_dispatch)
1017c478bd9Sstevel@tonic-gate {
1027c478bd9Sstevel@tonic-gate 	int cred_flavor;
1037c478bd9Sstevel@tonic-gate 	struct authsvc *asp;
1047c478bd9Sstevel@tonic-gate 	extern mutex_t authsvc_lock;
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate /* VARIABLES PROTECTED BY authsvc_lock: asp, Auths */
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate 	rqst->rq_cred = msg->rm_call.cb_cred;
1097c478bd9Sstevel@tonic-gate 	rqst->rq_xprt->xp_verf.oa_flavor = _null_auth.oa_flavor;
1107c478bd9Sstevel@tonic-gate 	rqst->rq_xprt->xp_verf.oa_length = 0;
1117c478bd9Sstevel@tonic-gate 	cred_flavor = rqst->rq_cred.oa_flavor;
1127c478bd9Sstevel@tonic-gate 	*no_dispatch = FALSE;
1137c478bd9Sstevel@tonic-gate 	switch (cred_flavor) {
1147c478bd9Sstevel@tonic-gate 	case AUTH_NULL:
115*61961e0fSrobinson 		return (__svcauth_null(rqst, msg));
1167c478bd9Sstevel@tonic-gate 	case AUTH_SYS:
117*61961e0fSrobinson 		return (__svcauth_sys(rqst, msg));
1187c478bd9Sstevel@tonic-gate 	case AUTH_SHORT:
119*61961e0fSrobinson 		return (__svcauth_short(rqst, msg));
1207c478bd9Sstevel@tonic-gate 	case AUTH_DES:
121*61961e0fSrobinson 		return (__svcauth_des(rqst, msg));
1227c478bd9Sstevel@tonic-gate 	case AUTH_LOOPBACK:
123*61961e0fSrobinson 		return (__svcauth_loopback(rqst, msg));
1247c478bd9Sstevel@tonic-gate 	case RPCSEC_GSS:
125*61961e0fSrobinson 		return (__svcrpcsec_gss(rqst, msg, no_dispatch));
1267c478bd9Sstevel@tonic-gate 	}
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate 	/* flavor doesn't match any of the builtin types, so try new ones */
129*61961e0fSrobinson 	(void) mutex_lock(&authsvc_lock);
1307c478bd9Sstevel@tonic-gate 	for (asp = Auths; asp; asp = asp->next) {
1317c478bd9Sstevel@tonic-gate 		if (asp->flavor == cred_flavor) {
1327c478bd9Sstevel@tonic-gate 			enum auth_stat as;
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate 			as = (*asp->handler)(rqst, msg);
135*61961e0fSrobinson 			(void) mutex_unlock(&authsvc_lock);
1367c478bd9Sstevel@tonic-gate 			return (as);
1377c478bd9Sstevel@tonic-gate 		}
1387c478bd9Sstevel@tonic-gate 	}
139*61961e0fSrobinson 	(void) mutex_unlock(&authsvc_lock);
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate 	return (AUTH_REJECTEDCRED);
1427c478bd9Sstevel@tonic-gate }
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate /*
1457c478bd9Sstevel@tonic-gate  * The following function __authenticate(rqst, msg) is preserved for
1467c478bd9Sstevel@tonic-gate  * backward compatibility.
1477c478bd9Sstevel@tonic-gate  */
1487c478bd9Sstevel@tonic-gate enum auth_stat
__authenticate(struct svc_req * rqst,struct rpc_msg * msg)149*61961e0fSrobinson __authenticate(struct svc_req *rqst, struct rpc_msg *msg)
1507c478bd9Sstevel@tonic-gate {
1517c478bd9Sstevel@tonic-gate 	bool_t no_dispatch;
1527c478bd9Sstevel@tonic-gate 
153*61961e0fSrobinson 	return (__gss_authenticate(rqst, msg, &no_dispatch));
1547c478bd9Sstevel@tonic-gate }
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1577c478bd9Sstevel@tonic-gate enum auth_stat
__svcauth_null(struct svc_req * rqst,struct rpc_msg * msg)158*61961e0fSrobinson __svcauth_null(struct svc_req *rqst, struct rpc_msg *msg)
1597c478bd9Sstevel@tonic-gate {
1607c478bd9Sstevel@tonic-gate 	return (AUTH_OK);
1617c478bd9Sstevel@tonic-gate }
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate /*
1647c478bd9Sstevel@tonic-gate  *  Allow the rpc service to register new authentication types that it is
1657c478bd9Sstevel@tonic-gate  *  prepared to handle.  When an authentication flavor is registered,
1667c478bd9Sstevel@tonic-gate  *  the flavor is checked against already registered values.  If not
1677c478bd9Sstevel@tonic-gate  *  registered, then a new Auths entry is added on the list.
1687c478bd9Sstevel@tonic-gate  *
1697c478bd9Sstevel@tonic-gate  *  There is no provision to delete a registration once registered.
1707c478bd9Sstevel@tonic-gate  *
1717c478bd9Sstevel@tonic-gate  *  This routine returns:
1727c478bd9Sstevel@tonic-gate  *	 0 if registration successful
1737c478bd9Sstevel@tonic-gate  *	 1 if flavor already registered
1747c478bd9Sstevel@tonic-gate  *	-1 if can't register (errno set)
1757c478bd9Sstevel@tonic-gate  */
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate int
svc_auth_reg(int cred_flavor,enum auth_stat (* handler)())178*61961e0fSrobinson svc_auth_reg(int cred_flavor, enum auth_stat (*handler)())
1797c478bd9Sstevel@tonic-gate {
1807c478bd9Sstevel@tonic-gate 	struct authsvc *asp;
1817c478bd9Sstevel@tonic-gate 	extern mutex_t authsvc_lock;
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate 	switch (cred_flavor) {
1847c478bd9Sstevel@tonic-gate 	case AUTH_NULL:
1857c478bd9Sstevel@tonic-gate 	case AUTH_SYS:
1867c478bd9Sstevel@tonic-gate 	case AUTH_SHORT:
1877c478bd9Sstevel@tonic-gate 	case AUTH_DES:
1887c478bd9Sstevel@tonic-gate 	case AUTH_LOOPBACK:
1897c478bd9Sstevel@tonic-gate 	case RPCSEC_GSS:
1907c478bd9Sstevel@tonic-gate 		/* already registered */
1917c478bd9Sstevel@tonic-gate 		return (1);
192*61961e0fSrobinson 	}
193*61961e0fSrobinson 	(void) mutex_lock(&authsvc_lock);
1947c478bd9Sstevel@tonic-gate 	for (asp = Auths; asp; asp = asp->next) {
1957c478bd9Sstevel@tonic-gate 		if (asp->flavor == cred_flavor) {
1967c478bd9Sstevel@tonic-gate 			/* already registered */
197*61961e0fSrobinson 			(void) mutex_unlock(&authsvc_lock);
1987c478bd9Sstevel@tonic-gate 			return (1);
1997c478bd9Sstevel@tonic-gate 		}
2007c478bd9Sstevel@tonic-gate 	}
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate 	/* this is a new one, so go ahead and register it */
203*61961e0fSrobinson 	asp = malloc(sizeof (*asp));
2047c478bd9Sstevel@tonic-gate 	if (asp == NULL) {
205*61961e0fSrobinson 		(void) mutex_unlock(&authsvc_lock);
2067c478bd9Sstevel@tonic-gate 		return (-1);
2077c478bd9Sstevel@tonic-gate 	}
2087c478bd9Sstevel@tonic-gate 	asp->flavor = cred_flavor;
2097c478bd9Sstevel@tonic-gate 	asp->handler = handler;
2107c478bd9Sstevel@tonic-gate 	asp->next = Auths;
2117c478bd9Sstevel@tonic-gate 	Auths = asp;
212*61961e0fSrobinson 	(void) mutex_unlock(&authsvc_lock);
2137c478bd9Sstevel@tonic-gate 	return (0);
2147c478bd9Sstevel@tonic-gate }
215