xref: /titanic_50/usr/src/lib/libnsl/rpc/svc_auth.c (revision 61961e0f20c7637a3846bb39786bb9dffa91dfb9)
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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate /*
387c478bd9Sstevel@tonic-gate  * svc_auth.c, Server-side rpc authenticator interface.
397c478bd9Sstevel@tonic-gate  *
407c478bd9Sstevel@tonic-gate  */
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate #include "mt.h"
437c478bd9Sstevel@tonic-gate #include "rpc_mt.h"
447c478bd9Sstevel@tonic-gate #include <rpc/rpc.h>
457c478bd9Sstevel@tonic-gate #include <sys/types.h>
467c478bd9Sstevel@tonic-gate #include <stdlib.h>
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate /*
497c478bd9Sstevel@tonic-gate  * svcauthsw is the bdevsw of server side authentication.
507c478bd9Sstevel@tonic-gate  *
517c478bd9Sstevel@tonic-gate  * Server side authenticators are called from authenticate by
527c478bd9Sstevel@tonic-gate  * using the client auth struct flavor field to index into svcauthsw.
537c478bd9Sstevel@tonic-gate  * The server auth flavors must implement a routine that looks
547c478bd9Sstevel@tonic-gate  * like:
557c478bd9Sstevel@tonic-gate  *
567c478bd9Sstevel@tonic-gate  *	enum auth_stat
577c478bd9Sstevel@tonic-gate  *	flavorx_auth(rqst, msg)
587c478bd9Sstevel@tonic-gate  *		struct svc_req *rqst;
597c478bd9Sstevel@tonic-gate  *		struct rpc_msg *msg;
607c478bd9Sstevel@tonic-gate  *
617c478bd9Sstevel@tonic-gate  * The RPCSEC_GSS flavor is an exception.  Its routine takes an
627c478bd9Sstevel@tonic-gate  * additional boolean parameter that gets set to TRUE when the call
637c478bd9Sstevel@tonic-gate  * is not to be dispatched to the server.
647c478bd9Sstevel@tonic-gate  */
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate enum auth_stat __svcauth_null();	/* no authentication */
677c478bd9Sstevel@tonic-gate enum auth_stat __svcauth_sys();		/* (system) unix style (uid, gids) */
687c478bd9Sstevel@tonic-gate enum auth_stat __svcauth_short();	/* short hand unix style */
697c478bd9Sstevel@tonic-gate enum auth_stat __svcauth_des();		/* des style */
707c478bd9Sstevel@tonic-gate enum auth_stat __svcauth_loopback();	/* (loopback) unix style (uid, gids) */
717c478bd9Sstevel@tonic-gate extern enum auth_stat __svcrpcsec_gss();	/* GSS style */
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate /* declarations to allow servers to specify new authentication flavors */
747c478bd9Sstevel@tonic-gate struct authsvc {
757c478bd9Sstevel@tonic-gate 	int	flavor;
767c478bd9Sstevel@tonic-gate 	enum	auth_stat (*handler)();
777c478bd9Sstevel@tonic-gate 	struct	authsvc	  *next;
787c478bd9Sstevel@tonic-gate };
797c478bd9Sstevel@tonic-gate static struct authsvc *Auths = NULL;
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate /*
827c478bd9Sstevel@tonic-gate  * The call rpc message, msg has been obtained from the wire.  The msg contains
837c478bd9Sstevel@tonic-gate  * the raw form of credentials and verifiers.  no_dispatch is used and
847c478bd9Sstevel@tonic-gate  * dereferenced in subsequent gss function calls.  authenticate returns AUTH_OK
857c478bd9Sstevel@tonic-gate  * if the msg is successfully authenticated.  If AUTH_OK then the routine also
867c478bd9Sstevel@tonic-gate  * does the following things:
877c478bd9Sstevel@tonic-gate  * set rqst->rq_xprt->verf to the appropriate response verifier;
887c478bd9Sstevel@tonic-gate  * sets rqst->rq_client_cred to the "cooked" form of the credentials.
897c478bd9Sstevel@tonic-gate  *
907c478bd9Sstevel@tonic-gate  * NB: rqst->rq_cxprt->verf must be pre-alloctaed;
917c478bd9Sstevel@tonic-gate  * its length is set appropriately.
927c478bd9Sstevel@tonic-gate  *
937c478bd9Sstevel@tonic-gate  * The caller still owns and is responsible for msg->u.cmb.cred and
947c478bd9Sstevel@tonic-gate  * msg->u.cmb.verf.  The authentication system retains ownership of
957c478bd9Sstevel@tonic-gate  * rqst->rq_client_cred, the cooked credentials.
967c478bd9Sstevel@tonic-gate  *
977c478bd9Sstevel@tonic-gate  * There is an assumption that any flavour less than AUTH_NULL is
987c478bd9Sstevel@tonic-gate  * invalid.
997c478bd9Sstevel@tonic-gate  */
1007c478bd9Sstevel@tonic-gate enum auth_stat
__gss_authenticate(struct svc_req * rqst,struct rpc_msg * msg,bool_t * no_dispatch)101*61961e0fSrobinson __gss_authenticate(struct svc_req *rqst, struct rpc_msg *msg,
102*61961e0fSrobinson 							bool_t *no_dispatch)
1037c478bd9Sstevel@tonic-gate {
1047c478bd9Sstevel@tonic-gate 	int cred_flavor;
1057c478bd9Sstevel@tonic-gate 	struct authsvc *asp;
1067c478bd9Sstevel@tonic-gate 	extern mutex_t authsvc_lock;
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate /* VARIABLES PROTECTED BY authsvc_lock: asp, Auths */
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate 	rqst->rq_cred = msg->rm_call.cb_cred;
1117c478bd9Sstevel@tonic-gate 	rqst->rq_xprt->xp_verf.oa_flavor = _null_auth.oa_flavor;
1127c478bd9Sstevel@tonic-gate 	rqst->rq_xprt->xp_verf.oa_length = 0;
1137c478bd9Sstevel@tonic-gate 	cred_flavor = rqst->rq_cred.oa_flavor;
1147c478bd9Sstevel@tonic-gate 	*no_dispatch = FALSE;
1157c478bd9Sstevel@tonic-gate 	switch (cred_flavor) {
1167c478bd9Sstevel@tonic-gate 	case AUTH_NULL:
117*61961e0fSrobinson 		return (__svcauth_null(rqst, msg));
1187c478bd9Sstevel@tonic-gate 	case AUTH_SYS:
119*61961e0fSrobinson 		return (__svcauth_sys(rqst, msg));
1207c478bd9Sstevel@tonic-gate 	case AUTH_SHORT:
121*61961e0fSrobinson 		return (__svcauth_short(rqst, msg));
1227c478bd9Sstevel@tonic-gate 	case AUTH_DES:
123*61961e0fSrobinson 		return (__svcauth_des(rqst, msg));
1247c478bd9Sstevel@tonic-gate 	case AUTH_LOOPBACK:
125*61961e0fSrobinson 		return (__svcauth_loopback(rqst, msg));
1267c478bd9Sstevel@tonic-gate 	case RPCSEC_GSS:
127*61961e0fSrobinson 		return (__svcrpcsec_gss(rqst, msg, no_dispatch));
1287c478bd9Sstevel@tonic-gate 	}
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate 	/* flavor doesn't match any of the builtin types, so try new ones */
131*61961e0fSrobinson 	(void) mutex_lock(&authsvc_lock);
1327c478bd9Sstevel@tonic-gate 	for (asp = Auths; asp; asp = asp->next) {
1337c478bd9Sstevel@tonic-gate 		if (asp->flavor == cred_flavor) {
1347c478bd9Sstevel@tonic-gate 			enum auth_stat as;
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate 			as = (*asp->handler)(rqst, msg);
137*61961e0fSrobinson 			(void) mutex_unlock(&authsvc_lock);
1387c478bd9Sstevel@tonic-gate 			return (as);
1397c478bd9Sstevel@tonic-gate 		}
1407c478bd9Sstevel@tonic-gate 	}
141*61961e0fSrobinson 	(void) mutex_unlock(&authsvc_lock);
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate 	return (AUTH_REJECTEDCRED);
1447c478bd9Sstevel@tonic-gate }
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate /*
1477c478bd9Sstevel@tonic-gate  * The following function __authenticate(rqst, msg) is preserved for
1487c478bd9Sstevel@tonic-gate  * backward compatibility.
1497c478bd9Sstevel@tonic-gate  */
1507c478bd9Sstevel@tonic-gate enum auth_stat
__authenticate(struct svc_req * rqst,struct rpc_msg * msg)151*61961e0fSrobinson __authenticate(struct svc_req *rqst, struct rpc_msg *msg)
1527c478bd9Sstevel@tonic-gate {
1537c478bd9Sstevel@tonic-gate 	bool_t no_dispatch;
1547c478bd9Sstevel@tonic-gate 
155*61961e0fSrobinson 	return (__gss_authenticate(rqst, msg, &no_dispatch));
1567c478bd9Sstevel@tonic-gate }
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1597c478bd9Sstevel@tonic-gate enum auth_stat
__svcauth_null(struct svc_req * rqst,struct rpc_msg * msg)160*61961e0fSrobinson __svcauth_null(struct svc_req *rqst, struct rpc_msg *msg)
1617c478bd9Sstevel@tonic-gate {
1627c478bd9Sstevel@tonic-gate 	return (AUTH_OK);
1637c478bd9Sstevel@tonic-gate }
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate /*
1667c478bd9Sstevel@tonic-gate  *  Allow the rpc service to register new authentication types that it is
1677c478bd9Sstevel@tonic-gate  *  prepared to handle.  When an authentication flavor is registered,
1687c478bd9Sstevel@tonic-gate  *  the flavor is checked against already registered values.  If not
1697c478bd9Sstevel@tonic-gate  *  registered, then a new Auths entry is added on the list.
1707c478bd9Sstevel@tonic-gate  *
1717c478bd9Sstevel@tonic-gate  *  There is no provision to delete a registration once registered.
1727c478bd9Sstevel@tonic-gate  *
1737c478bd9Sstevel@tonic-gate  *  This routine returns:
1747c478bd9Sstevel@tonic-gate  *	 0 if registration successful
1757c478bd9Sstevel@tonic-gate  *	 1 if flavor already registered
1767c478bd9Sstevel@tonic-gate  *	-1 if can't register (errno set)
1777c478bd9Sstevel@tonic-gate  */
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate int
svc_auth_reg(int cred_flavor,enum auth_stat (* handler)())180*61961e0fSrobinson svc_auth_reg(int cred_flavor, enum auth_stat (*handler)())
1817c478bd9Sstevel@tonic-gate {
1827c478bd9Sstevel@tonic-gate 	struct authsvc *asp;
1837c478bd9Sstevel@tonic-gate 	extern mutex_t authsvc_lock;
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate 	switch (cred_flavor) {
1867c478bd9Sstevel@tonic-gate 	case AUTH_NULL:
1877c478bd9Sstevel@tonic-gate 	case AUTH_SYS:
1887c478bd9Sstevel@tonic-gate 	case AUTH_SHORT:
1897c478bd9Sstevel@tonic-gate 	case AUTH_DES:
1907c478bd9Sstevel@tonic-gate 	case AUTH_LOOPBACK:
1917c478bd9Sstevel@tonic-gate 	case RPCSEC_GSS:
1927c478bd9Sstevel@tonic-gate 		/* already registered */
1937c478bd9Sstevel@tonic-gate 		return (1);
194*61961e0fSrobinson 	}
195*61961e0fSrobinson 	(void) mutex_lock(&authsvc_lock);
1967c478bd9Sstevel@tonic-gate 	for (asp = Auths; asp; asp = asp->next) {
1977c478bd9Sstevel@tonic-gate 		if (asp->flavor == cred_flavor) {
1987c478bd9Sstevel@tonic-gate 			/* already registered */
199*61961e0fSrobinson 			(void) mutex_unlock(&authsvc_lock);
2007c478bd9Sstevel@tonic-gate 			return (1);
2017c478bd9Sstevel@tonic-gate 		}
2027c478bd9Sstevel@tonic-gate 	}
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate 	/* this is a new one, so go ahead and register it */
205*61961e0fSrobinson 	asp = malloc(sizeof (*asp));
2067c478bd9Sstevel@tonic-gate 	if (asp == NULL) {
207*61961e0fSrobinson 		(void) mutex_unlock(&authsvc_lock);
2087c478bd9Sstevel@tonic-gate 		return (-1);
2097c478bd9Sstevel@tonic-gate 	}
2107c478bd9Sstevel@tonic-gate 	asp->flavor = cred_flavor;
2117c478bd9Sstevel@tonic-gate 	asp->handler = handler;
2127c478bd9Sstevel@tonic-gate 	asp->next = Auths;
2137c478bd9Sstevel@tonic-gate 	Auths = asp;
214*61961e0fSrobinson 	(void) mutex_unlock(&authsvc_lock);
2157c478bd9Sstevel@tonic-gate 	return (0);
2167c478bd9Sstevel@tonic-gate }
217