summaryrefslogtreecommitdiff
path: root/usr/src/lib/libnsl/rpc/rpcsec_gss_if.c
blob: 9700d8b3b7a615ccb4b18d86bd5238e4dd05a07c (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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (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 2007 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

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

#include "mt.h"
#include "rpc_mt.h"
#include <stdio.h>
#include <atomic.h>
#include <sys/errno.h>
#include <dlfcn.h>
#include <rpc/rpc.h>

#define	RPCSEC	"rpcsec.so.1"

typedef struct {
	AUTH		*(*rpc_gss_seccreate)();
	bool_t		(*rpc_gss_set_defaults)();
	bool_t		(*rpc_gss_get_principal_name)();
	char		**(*rpc_gss_get_mechanisms)();
	char		**(*rpc_gss_get_mech_info)();
	bool_t		(*rpc_gss_get_versions)();
	bool_t		(*rpc_gss_is_installed)();
	bool_t		(*rpc_gss_set_svc_name)();
	bool_t		(*rpc_gss_set_callback)();
	bool_t		(*rpc_gss_getcred)();
	bool_t		(*rpc_gss_mech_to_oid)();
	bool_t		(*rpc_gss_qop_to_num)();
	enum auth_stat	(*__svcrpcsec_gss)();
	bool_t		(*__rpc_gss_wrap)();
	bool_t		(*__rpc_gss_unwrap)();
	int		(*rpc_gss_max_data_length)();
	int		(*rpc_gss_svc_max_data_length)();
	void		(*rpc_gss_get_error)();
} rpcgss_calls_t;

static rpcgss_calls_t calls;
static mutex_t rpcgss_calls_mutex = DEFAULTMUTEX;
static bool_t initialized = FALSE;

static bool_t
rpcgss_calls_init(void)
{
	void	*handle;
	bool_t	ret = FALSE;

	if (initialized) {
		membar_consumer();
		return (TRUE);
	}
	(void) mutex_lock(&rpcgss_calls_mutex);
	if (initialized) {
		(void) mutex_unlock(&rpcgss_calls_mutex);
		membar_consumer();
		return (TRUE);
	}

	if ((handle = dlopen(RPCSEC, RTLD_LAZY)) == NULL)
		goto done;

	if ((calls.rpc_gss_seccreate = (AUTH *(*)()) dlsym(handle,
					"__rpc_gss_seccreate")) == NULL)
		goto done;
	if ((calls.rpc_gss_set_defaults = (bool_t (*)()) dlsym(handle,
					"__rpc_gss_set_defaults")) == NULL)
		goto done;
	if ((calls.rpc_gss_get_principal_name = (bool_t (*)()) dlsym(handle,
				"__rpc_gss_get_principal_name")) == NULL)
		goto done;
	if ((calls.rpc_gss_get_mechanisms = (char **(*)()) dlsym(handle,
					"__rpc_gss_get_mechanisms")) == NULL)
		goto done;
	if ((calls.rpc_gss_get_mech_info = (char **(*)()) dlsym(handle,
					"__rpc_gss_get_mech_info")) == NULL)
		goto done;
	if ((calls.rpc_gss_get_versions = (bool_t (*)()) dlsym(handle,
					"__rpc_gss_get_versions")) == NULL)
		goto done;
	if ((calls.rpc_gss_is_installed = (bool_t (*)()) dlsym(handle,
					"__rpc_gss_is_installed")) == NULL)
		goto done;
	if ((calls.rpc_gss_set_svc_name = (bool_t (*)()) dlsym(handle,
					"__rpc_gss_set_svc_name")) == NULL)
		goto done;
	if ((calls.rpc_gss_set_callback = (bool_t (*)()) dlsym(handle,
					"__rpc_gss_set_callback")) == NULL)
		goto done;
	if ((calls.rpc_gss_getcred = (bool_t (*)()) dlsym(handle,
					"__rpc_gss_getcred")) == NULL)
		goto done;
	if ((calls.rpc_gss_mech_to_oid = (bool_t (*)()) dlsym(handle,
					"__rpc_gss_mech_to_oid")) == NULL)
		goto done;

	if ((calls.rpc_gss_qop_to_num = (bool_t (*)()) dlsym(handle,
					"__rpc_gss_qop_to_num")) == NULL)
		goto done;
	if ((calls.__svcrpcsec_gss = (enum auth_stat (*)()) dlsym(handle,
					"__svcrpcsec_gss")) == NULL)
		goto done;
	if ((calls.__rpc_gss_wrap = (bool_t (*)()) dlsym(handle,
					"__rpc_gss_wrap")) == NULL)
		goto done;
	if ((calls.__rpc_gss_unwrap = (bool_t (*)()) dlsym(handle,
					"__rpc_gss_unwrap")) == NULL)
		goto done;
	if ((calls.rpc_gss_max_data_length = (int (*)()) dlsym(handle,
					"__rpc_gss_max_data_length")) == NULL)
		goto done;
	if ((calls.rpc_gss_svc_max_data_length = (int (*)()) dlsym(handle,
				"__rpc_gss_svc_max_data_length")) == NULL)
		goto done;
	if ((calls.rpc_gss_get_error = (void (*)()) dlsym(handle,
					"__rpc_gss_get_error")) == NULL)
		goto done;
	ret = TRUE;
done:
	if (!ret) {
		if (handle != NULL)
			(void) dlclose(handle);
	}
	membar_producer();
	initialized = ret;
	(void) mutex_unlock(&rpcgss_calls_mutex);
	return (ret);
}

AUTH *
rpc_gss_seccreate(
	CLIENT			*clnt,		/* associated client handle */
	char			*principal,	/* server service principal */
	char			*mechanism,	/* security mechanism */
	rpc_gss_service_t	service_type,	/* security service */
	char			*qop,		/* requested QOP */
	rpc_gss_options_req_t	*options_req,	/* requested options */
	rpc_gss_options_ret_t	*options_ret)	/* returned options */
{
	if (!rpcgss_calls_init())
		return (NULL);
	return ((*calls.rpc_gss_seccreate)(clnt, principal, mechanism,
				service_type, qop, options_req, options_ret));
}

bool_t
rpc_gss_set_defaults(AUTH *auth, rpc_gss_service_t service, char *qop)
{
	if (!rpcgss_calls_init())
		return (FALSE);
	return ((*calls.rpc_gss_set_defaults)(auth, service, qop));
}

bool_t
rpc_gss_get_principal_name(
	rpc_gss_principal_t	*principal,
	char			*mechanism,
	char			*user_name,
	char			*node,
	char			*secdomain)
{
	if (!rpcgss_calls_init())
		return (FALSE);
	return ((*calls.rpc_gss_get_principal_name)(principal, mechanism,
					user_name, node, secdomain));
}

char **
rpc_gss_get_mechanisms(void)
{
	if (!rpcgss_calls_init())
		return (NULL);
	return ((*calls.rpc_gss_get_mechanisms)());
}

char **
rpc_gss_get_mech_info(char *mechanism, rpc_gss_service_t *service)
{
	if (!rpcgss_calls_init())
		return (NULL);
	return ((*calls.rpc_gss_get_mech_info)(mechanism, service));
}

bool_t
rpc_gss_get_versions(uint_t *vers_hi, uint_t *vers_lo)
{
	if (!rpcgss_calls_init())
		return (FALSE);
	return ((*calls.rpc_gss_get_versions)(vers_hi, vers_lo));
}

bool_t
rpc_gss_is_installed(char *mechanism)
{
	if (!rpcgss_calls_init())
		return (FALSE);
	return ((*calls.rpc_gss_is_installed)(mechanism));
}

bool_t
rpc_gss_set_svc_name(
	char			*principal, /* server service principal name */
	char			*mechanism,
	uint_t			req_time,
	uint_t			program,
	uint_t			version)
{
	if (!rpcgss_calls_init())
		return (FALSE);
	return ((*calls.rpc_gss_set_svc_name)(principal, mechanism, req_time,
						program, version));
}

bool_t
rpc_gss_set_callback(rpc_gss_callback_t *cb)
{
	if (!rpcgss_calls_init())
		return (FALSE);
	return ((*calls.rpc_gss_set_callback)(cb));
}

bool_t
rpc_gss_getcred(struct svc_req *req, rpc_gss_rawcred_t **rcred,
					rpc_gss_ucred_t **ucred, void **cookie)
{
	if (!rpcgss_calls_init())
		return (FALSE);
	return ((*calls.rpc_gss_getcred)(req, rcred, ucred, cookie));
}

bool_t
rpc_gss_mech_to_oid(char *mech, rpc_gss_OID *oid)
{
	if (!rpcgss_calls_init())
		return (FALSE);
	return ((*calls.rpc_gss_mech_to_oid)(mech, oid));
}

bool_t
rpc_gss_qop_to_num(char *qop, char *mech, uint_t *num)
{
	if (!rpcgss_calls_init())
		return (FALSE);
	return ((*calls.rpc_gss_qop_to_num)(qop, mech, num));
}

enum auth_stat
__svcrpcsec_gss(struct svc_req *rqst, struct rpc_msg *msg, bool_t *no_dispatch)
{
	if (!rpcgss_calls_init())
		return (AUTH_FAILED);
	return ((*calls.__svcrpcsec_gss)(rqst, msg, no_dispatch));
}

bool_t
__rpc_gss_wrap(AUTH *auth, char *buf, uint_t buflen, XDR *out_xdrs,
					bool_t (*xdr_func)(), caddr_t xdr_ptr)
{
	if (!rpcgss_calls_init())
		return (FALSE);
	return ((*calls.__rpc_gss_wrap)(auth, buf, buflen, out_xdrs,
							xdr_func, xdr_ptr));
}

bool_t
__rpc_gss_unwrap(AUTH *auth, XDR *in_xdrs, bool_t (*xdr_func)(),
								caddr_t xdr_ptr)
{
	if (!rpcgss_calls_init())
		return (FALSE);
	return ((*calls.__rpc_gss_unwrap)(auth, in_xdrs, xdr_func, xdr_ptr));
}

int
rpc_gss_max_data_length(AUTH *rpcgss_handle, int max_tp_unit_len)
{
	if (!rpcgss_calls_init())
		return (0);
	return ((*calls.rpc_gss_max_data_length)(rpcgss_handle,
					max_tp_unit_len));
}

int
rpc_gss_svc_max_data_length(struct svc_req *req, int max_tp_unit_len)
{
	if (!rpcgss_calls_init())
		return (0);
	return ((*calls.rpc_gss_svc_max_data_length)(req, max_tp_unit_len));
}

void
rpc_gss_get_error(rpc_gss_error_t *error)
{
	if (!rpcgss_calls_init()) {
		error->rpc_gss_error = RPC_GSS_ER_SYSTEMERROR;
		error->system_error = ENOTSUP;
		return;
	}
	(*calls.rpc_gss_get_error)(error);
}