10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
70Sstevel@tonic-gate * with the License.
80Sstevel@tonic-gate *
90Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate * See the License for the specific language governing permissions
120Sstevel@tonic-gate * and limitations under the License.
130Sstevel@tonic-gate *
140Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate *
200Sstevel@tonic-gate * CDDL HEADER END
21*132Srobinson */
22*132Srobinson
23*132Srobinson /*
24*132Srobinson * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
250Sstevel@tonic-gate * Use is subject to license terms.
260Sstevel@tonic-gate */
270Sstevel@tonic-gate /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
280Sstevel@tonic-gate /* All Rights Reserved */
290Sstevel@tonic-gate /*
300Sstevel@tonic-gate * Portions of this source code were derived from Berkeley
310Sstevel@tonic-gate * 4.3 BSD under license from the Regents of the University of
320Sstevel@tonic-gate * California.
330Sstevel@tonic-gate */
340Sstevel@tonic-gate
350Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
360Sstevel@tonic-gate
370Sstevel@tonic-gate /*
380Sstevel@tonic-gate * svc_auth.c, Server-side rpc authenticator interface.
390Sstevel@tonic-gate *
400Sstevel@tonic-gate */
410Sstevel@tonic-gate
420Sstevel@tonic-gate #include "mt.h"
430Sstevel@tonic-gate #include "rpc_mt.h"
440Sstevel@tonic-gate #include <rpc/rpc.h>
450Sstevel@tonic-gate #include <sys/types.h>
460Sstevel@tonic-gate #include <stdlib.h>
470Sstevel@tonic-gate
480Sstevel@tonic-gate /*
490Sstevel@tonic-gate * svcauthsw is the bdevsw of server side authentication.
500Sstevel@tonic-gate *
510Sstevel@tonic-gate * Server side authenticators are called from authenticate by
520Sstevel@tonic-gate * using the client auth struct flavor field to index into svcauthsw.
530Sstevel@tonic-gate * The server auth flavors must implement a routine that looks
540Sstevel@tonic-gate * like:
550Sstevel@tonic-gate *
560Sstevel@tonic-gate * enum auth_stat
570Sstevel@tonic-gate * flavorx_auth(rqst, msg)
580Sstevel@tonic-gate * struct svc_req *rqst;
590Sstevel@tonic-gate * struct rpc_msg *msg;
600Sstevel@tonic-gate *
610Sstevel@tonic-gate * The RPCSEC_GSS flavor is an exception. Its routine takes an
620Sstevel@tonic-gate * additional boolean parameter that gets set to TRUE when the call
630Sstevel@tonic-gate * is not to be dispatched to the server.
640Sstevel@tonic-gate */
650Sstevel@tonic-gate
660Sstevel@tonic-gate enum auth_stat __svcauth_null(); /* no authentication */
670Sstevel@tonic-gate enum auth_stat __svcauth_sys(); /* (system) unix style (uid, gids) */
680Sstevel@tonic-gate enum auth_stat __svcauth_short(); /* short hand unix style */
690Sstevel@tonic-gate enum auth_stat __svcauth_des(); /* des style */
700Sstevel@tonic-gate enum auth_stat __svcauth_loopback(); /* (loopback) unix style (uid, gids) */
710Sstevel@tonic-gate extern enum auth_stat __svcrpcsec_gss(); /* GSS style */
720Sstevel@tonic-gate
730Sstevel@tonic-gate /* declarations to allow servers to specify new authentication flavors */
740Sstevel@tonic-gate struct authsvc {
750Sstevel@tonic-gate int flavor;
760Sstevel@tonic-gate enum auth_stat (*handler)();
770Sstevel@tonic-gate struct authsvc *next;
780Sstevel@tonic-gate };
790Sstevel@tonic-gate static struct authsvc *Auths = NULL;
800Sstevel@tonic-gate
810Sstevel@tonic-gate /*
820Sstevel@tonic-gate * The call rpc message, msg has been obtained from the wire. The msg contains
830Sstevel@tonic-gate * the raw form of credentials and verifiers. no_dispatch is used and
840Sstevel@tonic-gate * dereferenced in subsequent gss function calls. authenticate returns AUTH_OK
850Sstevel@tonic-gate * if the msg is successfully authenticated. If AUTH_OK then the routine also
860Sstevel@tonic-gate * does the following things:
870Sstevel@tonic-gate * set rqst->rq_xprt->verf to the appropriate response verifier;
880Sstevel@tonic-gate * sets rqst->rq_client_cred to the "cooked" form of the credentials.
890Sstevel@tonic-gate *
900Sstevel@tonic-gate * NB: rqst->rq_cxprt->verf must be pre-alloctaed;
910Sstevel@tonic-gate * its length is set appropriately.
920Sstevel@tonic-gate *
930Sstevel@tonic-gate * The caller still owns and is responsible for msg->u.cmb.cred and
940Sstevel@tonic-gate * msg->u.cmb.verf. The authentication system retains ownership of
950Sstevel@tonic-gate * rqst->rq_client_cred, the cooked credentials.
960Sstevel@tonic-gate *
970Sstevel@tonic-gate * There is an assumption that any flavour less than AUTH_NULL is
980Sstevel@tonic-gate * invalid.
990Sstevel@tonic-gate */
1000Sstevel@tonic-gate enum auth_stat
__gss_authenticate(struct svc_req * rqst,struct rpc_msg * msg,bool_t * no_dispatch)101*132Srobinson __gss_authenticate(struct svc_req *rqst, struct rpc_msg *msg,
102*132Srobinson bool_t *no_dispatch)
1030Sstevel@tonic-gate {
1040Sstevel@tonic-gate int cred_flavor;
1050Sstevel@tonic-gate struct authsvc *asp;
1060Sstevel@tonic-gate extern mutex_t authsvc_lock;
1070Sstevel@tonic-gate
1080Sstevel@tonic-gate /* VARIABLES PROTECTED BY authsvc_lock: asp, Auths */
1090Sstevel@tonic-gate
1100Sstevel@tonic-gate rqst->rq_cred = msg->rm_call.cb_cred;
1110Sstevel@tonic-gate rqst->rq_xprt->xp_verf.oa_flavor = _null_auth.oa_flavor;
1120Sstevel@tonic-gate rqst->rq_xprt->xp_verf.oa_length = 0;
1130Sstevel@tonic-gate cred_flavor = rqst->rq_cred.oa_flavor;
1140Sstevel@tonic-gate *no_dispatch = FALSE;
1150Sstevel@tonic-gate switch (cred_flavor) {
1160Sstevel@tonic-gate case AUTH_NULL:
117*132Srobinson return (__svcauth_null(rqst, msg));
1180Sstevel@tonic-gate case AUTH_SYS:
119*132Srobinson return (__svcauth_sys(rqst, msg));
1200Sstevel@tonic-gate case AUTH_SHORT:
121*132Srobinson return (__svcauth_short(rqst, msg));
1220Sstevel@tonic-gate case AUTH_DES:
123*132Srobinson return (__svcauth_des(rqst, msg));
1240Sstevel@tonic-gate case AUTH_LOOPBACK:
125*132Srobinson return (__svcauth_loopback(rqst, msg));
1260Sstevel@tonic-gate case RPCSEC_GSS:
127*132Srobinson return (__svcrpcsec_gss(rqst, msg, no_dispatch));
1280Sstevel@tonic-gate }
1290Sstevel@tonic-gate
1300Sstevel@tonic-gate /* flavor doesn't match any of the builtin types, so try new ones */
131*132Srobinson (void) mutex_lock(&authsvc_lock);
1320Sstevel@tonic-gate for (asp = Auths; asp; asp = asp->next) {
1330Sstevel@tonic-gate if (asp->flavor == cred_flavor) {
1340Sstevel@tonic-gate enum auth_stat as;
1350Sstevel@tonic-gate
1360Sstevel@tonic-gate as = (*asp->handler)(rqst, msg);
137*132Srobinson (void) mutex_unlock(&authsvc_lock);
1380Sstevel@tonic-gate return (as);
1390Sstevel@tonic-gate }
1400Sstevel@tonic-gate }
141*132Srobinson (void) mutex_unlock(&authsvc_lock);
1420Sstevel@tonic-gate
1430Sstevel@tonic-gate return (AUTH_REJECTEDCRED);
1440Sstevel@tonic-gate }
1450Sstevel@tonic-gate
1460Sstevel@tonic-gate /*
1470Sstevel@tonic-gate * The following function __authenticate(rqst, msg) is preserved for
1480Sstevel@tonic-gate * backward compatibility.
1490Sstevel@tonic-gate */
1500Sstevel@tonic-gate enum auth_stat
__authenticate(struct svc_req * rqst,struct rpc_msg * msg)151*132Srobinson __authenticate(struct svc_req *rqst, struct rpc_msg *msg)
1520Sstevel@tonic-gate {
1530Sstevel@tonic-gate bool_t no_dispatch;
1540Sstevel@tonic-gate
155*132Srobinson return (__gss_authenticate(rqst, msg, &no_dispatch));
1560Sstevel@tonic-gate }
1570Sstevel@tonic-gate
1580Sstevel@tonic-gate /*ARGSUSED*/
1590Sstevel@tonic-gate enum auth_stat
__svcauth_null(struct svc_req * rqst,struct rpc_msg * msg)160*132Srobinson __svcauth_null(struct svc_req *rqst, struct rpc_msg *msg)
1610Sstevel@tonic-gate {
1620Sstevel@tonic-gate return (AUTH_OK);
1630Sstevel@tonic-gate }
1640Sstevel@tonic-gate
1650Sstevel@tonic-gate /*
1660Sstevel@tonic-gate * Allow the rpc service to register new authentication types that it is
1670Sstevel@tonic-gate * prepared to handle. When an authentication flavor is registered,
1680Sstevel@tonic-gate * the flavor is checked against already registered values. If not
1690Sstevel@tonic-gate * registered, then a new Auths entry is added on the list.
1700Sstevel@tonic-gate *
1710Sstevel@tonic-gate * There is no provision to delete a registration once registered.
1720Sstevel@tonic-gate *
1730Sstevel@tonic-gate * This routine returns:
1740Sstevel@tonic-gate * 0 if registration successful
1750Sstevel@tonic-gate * 1 if flavor already registered
1760Sstevel@tonic-gate * -1 if can't register (errno set)
1770Sstevel@tonic-gate */
1780Sstevel@tonic-gate
1790Sstevel@tonic-gate int
svc_auth_reg(int cred_flavor,enum auth_stat (* handler)())180*132Srobinson svc_auth_reg(int cred_flavor, enum auth_stat (*handler)())
1810Sstevel@tonic-gate {
1820Sstevel@tonic-gate struct authsvc *asp;
1830Sstevel@tonic-gate extern mutex_t authsvc_lock;
1840Sstevel@tonic-gate
1850Sstevel@tonic-gate switch (cred_flavor) {
186*132Srobinson case AUTH_NULL:
187*132Srobinson case AUTH_SYS:
188*132Srobinson case AUTH_SHORT:
189*132Srobinson case AUTH_DES:
190*132Srobinson case AUTH_LOOPBACK:
191*132Srobinson case RPCSEC_GSS:
1920Sstevel@tonic-gate /* already registered */
1930Sstevel@tonic-gate return (1);
194*132Srobinson }
195*132Srobinson (void) mutex_lock(&authsvc_lock);
196*132Srobinson for (asp = Auths; asp; asp = asp->next) {
197*132Srobinson if (asp->flavor == cred_flavor) {
198*132Srobinson /* already registered */
199*132Srobinson (void) mutex_unlock(&authsvc_lock);
200*132Srobinson return (1);
201*132Srobinson }
202*132Srobinson }
2030Sstevel@tonic-gate
204*132Srobinson /* this is a new one, so go ahead and register it */
205*132Srobinson asp = malloc(sizeof (*asp));
206*132Srobinson if (asp == NULL) {
207*132Srobinson (void) mutex_unlock(&authsvc_lock);
208*132Srobinson return (-1);
2090Sstevel@tonic-gate }
210*132Srobinson asp->flavor = cred_flavor;
211*132Srobinson asp->handler = handler;
212*132Srobinson asp->next = Auths;
213*132Srobinson Auths = asp;
214*132Srobinson (void) mutex_unlock(&authsvc_lock);
2150Sstevel@tonic-gate return (0);
2160Sstevel@tonic-gate }
217