summaryrefslogtreecommitdiff
path: root/usr/src/lib/libnsl/rpc/svc_auth.c
blob: 14895558d69daea300da83a0e632b0363eb91ccf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

/*
 * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */
/* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
/*
 * Portions of this source code were derived from Berkeley
 * 4.3 BSD under license from the Regents of the University of
 * California.
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

/*
 * svc_auth.c, Server-side rpc authenticator interface.
 *
 */

#include "mt.h"
#include "rpc_mt.h"
#include <rpc/rpc.h>
#include <sys/types.h>
#include <stdlib.h>

/*
 * svcauthsw is the bdevsw of server side authentication.
 *
 * Server side authenticators are called from authenticate by
 * using the client auth struct flavor field to index into svcauthsw.
 * The server auth flavors must implement a routine that looks
 * like:
 *
 *	enum auth_stat
 *	flavorx_auth(rqst, msg)
 *		struct svc_req *rqst;
 *		struct rpc_msg *msg;
 *
 * The RPCSEC_GSS flavor is an exception.  Its routine takes an
 * additional boolean parameter that gets set to TRUE when the call
 * is not to be dispatched to the server.
 */

enum auth_stat __svcauth_null();	/* no authentication */
enum auth_stat __svcauth_sys();		/* (system) unix style (uid, gids) */
enum auth_stat __svcauth_short();	/* short hand unix style */
enum auth_stat __svcauth_des();		/* des style */
enum auth_stat __svcauth_loopback();	/* (loopback) unix style (uid, gids) */
extern enum auth_stat __svcrpcsec_gss();	/* GSS style */

/* declarations to allow servers to specify new authentication flavors */
struct authsvc {
	int	flavor;
	enum	auth_stat (*handler)();
	struct	authsvc	  *next;
};
static struct authsvc *Auths = NULL;

/*
 * The call rpc message, msg has been obtained from the wire.  The msg contains
 * the raw form of credentials and verifiers.  no_dispatch is used and
 * dereferenced in subsequent gss function calls.  authenticate returns AUTH_OK
 * if the msg is successfully authenticated.  If AUTH_OK then the routine also
 * does the following things:
 * set rqst->rq_xprt->verf to the appropriate response verifier;
 * sets rqst->rq_client_cred to the "cooked" form of the credentials.
 *
 * NB: rqst->rq_cxprt->verf must be pre-alloctaed;
 * its length is set appropriately.
 *
 * The caller still owns and is responsible for msg->u.cmb.cred and
 * msg->u.cmb.verf.  The authentication system retains ownership of
 * rqst->rq_client_cred, the cooked credentials.
 *
 * There is an assumption that any flavour less than AUTH_NULL is
 * invalid.
 */
enum auth_stat
__gss_authenticate(struct svc_req *rqst, struct rpc_msg *msg,
							bool_t *no_dispatch)
{
	int cred_flavor;
	struct authsvc *asp;
	extern mutex_t authsvc_lock;

/* VARIABLES PROTECTED BY authsvc_lock: asp, Auths */

	rqst->rq_cred = msg->rm_call.cb_cred;
	rqst->rq_xprt->xp_verf.oa_flavor = _null_auth.oa_flavor;
	rqst->rq_xprt->xp_verf.oa_length = 0;
	cred_flavor = rqst->rq_cred.oa_flavor;
	*no_dispatch = FALSE;
	switch (cred_flavor) {
	case AUTH_NULL:
		return (__svcauth_null(rqst, msg));
	case AUTH_SYS:
		return (__svcauth_sys(rqst, msg));
	case AUTH_SHORT:
		return (__svcauth_short(rqst, msg));
	case AUTH_DES:
		return (__svcauth_des(rqst, msg));
	case AUTH_LOOPBACK:
		return (__svcauth_loopback(rqst, msg));
	case RPCSEC_GSS:
		return (__svcrpcsec_gss(rqst, msg, no_dispatch));
	}

	/* flavor doesn't match any of the builtin types, so try new ones */
	(void) mutex_lock(&authsvc_lock);
	for (asp = Auths; asp; asp = asp->next) {
		if (asp->flavor == cred_flavor) {
			enum auth_stat as;

			as = (*asp->handler)(rqst, msg);
			(void) mutex_unlock(&authsvc_lock);
			return (as);
		}
	}
	(void) mutex_unlock(&authsvc_lock);

	return (AUTH_REJECTEDCRED);
}

/*
 * The following function __authenticate(rqst, msg) is preserved for
 * backward compatibility.
 */
enum auth_stat
__authenticate(struct svc_req *rqst, struct rpc_msg *msg)
{
	bool_t no_dispatch;

	return (__gss_authenticate(rqst, msg, &no_dispatch));
}

/*ARGSUSED*/
enum auth_stat
__svcauth_null(struct svc_req *rqst, struct rpc_msg *msg)
{
	return (AUTH_OK);
}

/*
 *  Allow the rpc service to register new authentication types that it is
 *  prepared to handle.  When an authentication flavor is registered,
 *  the flavor is checked against already registered values.  If not
 *  registered, then a new Auths entry is added on the list.
 *
 *  There is no provision to delete a registration once registered.
 *
 *  This routine returns:
 *	 0 if registration successful
 *	 1 if flavor already registered
 *	-1 if can't register (errno set)
 */

int
svc_auth_reg(int cred_flavor, enum auth_stat (*handler)())
{
	struct authsvc *asp;
	extern mutex_t authsvc_lock;

	switch (cred_flavor) {
	case AUTH_NULL:
	case AUTH_SYS:
	case AUTH_SHORT:
	case AUTH_DES:
	case AUTH_LOOPBACK:
	case RPCSEC_GSS:
		/* already registered */
		return (1);
	}
	(void) mutex_lock(&authsvc_lock);
	for (asp = Auths; asp; asp = asp->next) {
		if (asp->flavor == cred_flavor) {
			/* already registered */
			(void) mutex_unlock(&authsvc_lock);
			return (1);
		}
	}

	/* this is a new one, so go ahead and register it */
	asp = malloc(sizeof (*asp));
	if (asp == NULL) {
		(void) mutex_unlock(&authsvc_lock);
		return (-1);
	}
	asp->flavor = cred_flavor;
	asp->handler = handler;
	asp->next = Auths;
	Auths = asp;
	(void) mutex_unlock(&authsvc_lock);
	return (0);
}