Linux Audio

Check our new training course

Loading...
v5.9
  1// SPDX-License-Identifier: GPL-2.0+
  2/*
  3 *  linux/net/sunrpc/gss_rpc_upcall.c
  4 *
  5 *  Copyright (C) 2012 Simo Sorce <simo@redhat.com>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  6 */
  7
  8#include <linux/types.h>
  9#include <linux/un.h>
 10
 11#include <linux/sunrpc/svcauth.h>
 12#include "gss_rpc_upcall.h"
 13
 14#define GSSPROXY_SOCK_PATHNAME	"/var/run/gssproxy.sock"
 15
 16#define GSSPROXY_PROGRAM	(400112u)
 17#define GSSPROXY_VERS_1		(1u)
 18
 19/*
 20 * Encoding/Decoding functions
 21 */
 22
 23enum {
 24	GSSX_NULL = 0,	/* Unused */
 25        GSSX_INDICATE_MECHS = 1,
 26        GSSX_GET_CALL_CONTEXT = 2,
 27        GSSX_IMPORT_AND_CANON_NAME = 3,
 28        GSSX_EXPORT_CRED = 4,
 29        GSSX_IMPORT_CRED = 5,
 30        GSSX_ACQUIRE_CRED = 6,
 31        GSSX_STORE_CRED = 7,
 32        GSSX_INIT_SEC_CONTEXT = 8,
 33        GSSX_ACCEPT_SEC_CONTEXT = 9,
 34        GSSX_RELEASE_HANDLE = 10,
 35        GSSX_GET_MIC = 11,
 36        GSSX_VERIFY = 12,
 37        GSSX_WRAP = 13,
 38        GSSX_UNWRAP = 14,
 39        GSSX_WRAP_SIZE_LIMIT = 15,
 40};
 41
 42#define PROC(proc, name)				\
 43[GSSX_##proc] = {					\
 44	.p_proc   = GSSX_##proc,			\
 45	.p_encode = gssx_enc_##name,	\
 46	.p_decode = gssx_dec_##name,	\
 47	.p_arglen = GSSX_ARG_##name##_sz,		\
 48	.p_replen = GSSX_RES_##name##_sz, 		\
 49	.p_statidx = GSSX_##proc,			\
 50	.p_name   = #proc,				\
 51}
 52
 53static const struct rpc_procinfo gssp_procedures[] = {
 54	PROC(INDICATE_MECHS, indicate_mechs),
 55        PROC(GET_CALL_CONTEXT, get_call_context),
 56        PROC(IMPORT_AND_CANON_NAME, import_and_canon_name),
 57        PROC(EXPORT_CRED, export_cred),
 58        PROC(IMPORT_CRED, import_cred),
 59        PROC(ACQUIRE_CRED, acquire_cred),
 60        PROC(STORE_CRED, store_cred),
 61        PROC(INIT_SEC_CONTEXT, init_sec_context),
 62        PROC(ACCEPT_SEC_CONTEXT, accept_sec_context),
 63        PROC(RELEASE_HANDLE, release_handle),
 64        PROC(GET_MIC, get_mic),
 65        PROC(VERIFY, verify),
 66        PROC(WRAP, wrap),
 67        PROC(UNWRAP, unwrap),
 68        PROC(WRAP_SIZE_LIMIT, wrap_size_limit),
 69};
 70
 71
 72
 73/*
 74 * Common transport functions
 75 */
 76
 77static const struct rpc_program gssp_program;
 78
 79static int gssp_rpc_create(struct net *net, struct rpc_clnt **_clnt)
 80{
 81	static const struct sockaddr_un gssp_localaddr = {
 82		.sun_family		= AF_LOCAL,
 83		.sun_path		= GSSPROXY_SOCK_PATHNAME,
 84	};
 85	struct rpc_create_args args = {
 86		.net		= net,
 87		.protocol	= XPRT_TRANSPORT_LOCAL,
 88		.address	= (struct sockaddr *)&gssp_localaddr,
 89		.addrsize	= sizeof(gssp_localaddr),
 90		.servername	= "localhost",
 91		.program	= &gssp_program,
 92		.version	= GSSPROXY_VERS_1,
 93		.authflavor	= RPC_AUTH_NULL,
 94		/*
 95		 * Note we want connection to be done in the caller's
 96		 * filesystem namespace.  We therefore turn off the idle
 97		 * timeout, which would result in reconnections being
 98		 * done without the correct namespace:
 99		 */
100		.flags		= RPC_CLNT_CREATE_NOPING |
101				  RPC_CLNT_CREATE_NO_IDLE_TIMEOUT
102	};
103	struct rpc_clnt *clnt;
104	int result = 0;
105
106	clnt = rpc_create(&args);
107	if (IS_ERR(clnt)) {
108		dprintk("RPC:       failed to create AF_LOCAL gssproxy "
109				"client (errno %ld).\n", PTR_ERR(clnt));
110		result = PTR_ERR(clnt);
111		*_clnt = NULL;
112		goto out;
113	}
114
115	dprintk("RPC:       created new gssp local client (gssp_local_clnt: "
116			"%p)\n", clnt);
117	*_clnt = clnt;
118
119out:
120	return result;
121}
122
123void init_gssp_clnt(struct sunrpc_net *sn)
124{
125	mutex_init(&sn->gssp_lock);
126	sn->gssp_clnt = NULL;
127}
128
129int set_gssp_clnt(struct net *net)
130{
131	struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
132	struct rpc_clnt *clnt;
133	int ret;
134
135	mutex_lock(&sn->gssp_lock);
136	ret = gssp_rpc_create(net, &clnt);
137	if (!ret) {
138		if (sn->gssp_clnt)
139			rpc_shutdown_client(sn->gssp_clnt);
140		sn->gssp_clnt = clnt;
141	}
142	mutex_unlock(&sn->gssp_lock);
143	return ret;
144}
145
146void clear_gssp_clnt(struct sunrpc_net *sn)
147{
148	mutex_lock(&sn->gssp_lock);
149	if (sn->gssp_clnt) {
150		rpc_shutdown_client(sn->gssp_clnt);
151		sn->gssp_clnt = NULL;
152	}
153	mutex_unlock(&sn->gssp_lock);
154}
155
156static struct rpc_clnt *get_gssp_clnt(struct sunrpc_net *sn)
157{
158	struct rpc_clnt *clnt;
159
160	mutex_lock(&sn->gssp_lock);
161	clnt = sn->gssp_clnt;
162	if (clnt)
163		atomic_inc(&clnt->cl_count);
164	mutex_unlock(&sn->gssp_lock);
165	return clnt;
166}
167
168static int gssp_call(struct net *net, struct rpc_message *msg)
169{
170	struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
171	struct rpc_clnt *clnt;
172	int status;
173
174	clnt = get_gssp_clnt(sn);
175	if (!clnt)
176		return -EIO;
177	status = rpc_call_sync(clnt, msg, 0);
178	if (status < 0) {
179		dprintk("gssp: rpc_call returned error %d\n", -status);
180		switch (status) {
181		case -EPROTONOSUPPORT:
182			status = -EINVAL;
183			break;
184		case -ECONNREFUSED:
185		case -ETIMEDOUT:
186		case -ENOTCONN:
187			status = -EAGAIN;
188			break;
189		case -ERESTARTSYS:
190			if (signalled ())
191				status = -EINTR;
192			break;
193		default:
194			break;
195		}
196	}
197	rpc_release_client(clnt);
198	return status;
199}
200
201static void gssp_free_receive_pages(struct gssx_arg_accept_sec_context *arg)
202{
203	int i;
204
205	for (i = 0; i < arg->npages && arg->pages[i]; i++)
206		__free_page(arg->pages[i]);
207
208	kfree(arg->pages);
209}
210
211static int gssp_alloc_receive_pages(struct gssx_arg_accept_sec_context *arg)
212{
213	arg->npages = DIV_ROUND_UP(NGROUPS_MAX * 4, PAGE_SIZE);
214	arg->pages = kcalloc(arg->npages, sizeof(struct page *), GFP_KERNEL);
215	/*
216	 * XXX: actual pages are allocated by xdr layer in
217	 * xdr_partial_copy_from_skb.
218	 */
219	if (!arg->pages)
220		return -ENOMEM;
221	return 0;
222}
223
224static char *gssp_stringify(struct xdr_netobj *netobj)
225{
226	return kmemdup_nul(netobj->data, netobj->len, GFP_KERNEL);
227}
228
229static void gssp_hostbased_service(char **principal)
230{
231	char *c;
232
233	if (!*principal)
234		return;
235
236	/* terminate and remove realm part */
237	c = strchr(*principal, '@');
238	if (c) {
239		*c = '\0';
240
241		/* change service-hostname delimiter */
242		c = strchr(*principal, '/');
243		if (c)
244			*c = '@';
245	}
246	if (!c) {
247		/* not a service principal */
248		kfree(*principal);
249		*principal = NULL;
250	}
251}
252
253/*
254 * Public functions
255 */
256
257/* numbers somewhat arbitrary but large enough for current needs */
258#define GSSX_MAX_OUT_HANDLE	128
259#define GSSX_MAX_SRC_PRINC	256
260#define GSSX_KMEMBUF (GSSX_max_output_handle_sz + \
261			GSSX_max_oid_sz + \
262			GSSX_max_princ_sz + \
263			sizeof(struct svc_cred))
264
265int gssp_accept_sec_context_upcall(struct net *net,
266				struct gssp_upcall_data *data)
267{
268	struct gssx_ctx ctxh = {
269		.state = data->in_handle
270	};
271	struct gssx_arg_accept_sec_context arg = {
272		.input_token = data->in_token,
273	};
274	struct gssx_ctx rctxh = {
275		/*
276		 * pass in the max length we expect for each of these
277		 * buffers but let the xdr code kmalloc them:
278		 */
279		.exported_context_token.len = GSSX_max_output_handle_sz,
280		.mech.len = GSS_OID_MAX_LEN,
281		.targ_name.display_name.len = GSSX_max_princ_sz,
282		.src_name.display_name.len = GSSX_max_princ_sz
283	};
284	struct gssx_res_accept_sec_context res = {
285		.context_handle = &rctxh,
286		.output_token = &data->out_token
287	};
288	struct rpc_message msg = {
289		.rpc_proc = &gssp_procedures[GSSX_ACCEPT_SEC_CONTEXT],
290		.rpc_argp = &arg,
291		.rpc_resp = &res,
292		.rpc_cred = NULL, /* FIXME ? */
293	};
294	struct xdr_netobj client_name = { 0 , NULL };
295	struct xdr_netobj target_name = { 0, NULL };
296	int ret;
297
298	if (data->in_handle.len != 0)
299		arg.context_handle = &ctxh;
300	res.output_token->len = GSSX_max_output_token_sz;
301
302	ret = gssp_alloc_receive_pages(&arg);
303	if (ret)
304		return ret;
305
 
 
306	ret = gssp_call(net, &msg);
307
308	gssp_free_receive_pages(&arg);
309
310	/* we need to fetch all data even in case of error so
311	 * that we can free special strctures is they have been allocated */
312	data->major_status = res.status.major_status;
313	data->minor_status = res.status.minor_status;
314	if (res.context_handle) {
315		data->out_handle = rctxh.exported_context_token;
316		data->mech_oid.len = rctxh.mech.len;
317		if (rctxh.mech.data) {
318			memcpy(data->mech_oid.data, rctxh.mech.data,
319						data->mech_oid.len);
320			kfree(rctxh.mech.data);
321		}
322		client_name = rctxh.src_name.display_name;
323		target_name = rctxh.targ_name.display_name;
324	}
325
326	if (res.options.count == 1) {
327		gssx_buffer *value = &res.options.data[0].value;
328		/* Currently we only decode CREDS_VALUE, if we add
329		 * anything else we'll have to loop and match on the
330		 * option name */
331		if (value->len == 1) {
332			/* steal group info from struct svc_cred */
333			data->creds = *(struct svc_cred *)value->data;
334			data->found_creds = 1;
335		}
336		/* whether we use it or not, free data */
337		kfree(value->data);
338	}
339
340	if (res.options.count != 0) {
341		kfree(res.options.data);
342	}
343
344	/* convert to GSS_NT_HOSTBASED_SERVICE form and set into creds */
345	if (data->found_creds) {
346		if (client_name.data) {
347			data->creds.cr_raw_principal =
348					gssp_stringify(&client_name);
349			data->creds.cr_principal =
350					gssp_stringify(&client_name);
351			gssp_hostbased_service(&data->creds.cr_principal);
352		}
353		if (target_name.data) {
354			data->creds.cr_targ_princ =
355					gssp_stringify(&target_name);
356			gssp_hostbased_service(&data->creds.cr_targ_princ);
 
 
 
 
 
 
 
 
 
 
 
357		}
358	}
359	kfree(client_name.data);
360	kfree(target_name.data);
361
362	return ret;
363}
364
365void gssp_free_upcall_data(struct gssp_upcall_data *data)
366{
367	kfree(data->in_handle.data);
368	kfree(data->out_handle.data);
369	kfree(data->out_token.data);
370	free_svc_cred(&data->creds);
371}
372
373/*
374 * Initialization stuff
375 */
376static unsigned int gssp_version1_counts[ARRAY_SIZE(gssp_procedures)];
377static const struct rpc_version gssp_version1 = {
378	.number		= GSSPROXY_VERS_1,
379	.nrprocs	= ARRAY_SIZE(gssp_procedures),
380	.procs		= gssp_procedures,
381	.counts		= gssp_version1_counts,
382};
383
384static const struct rpc_version *gssp_version[] = {
385	NULL,
386	&gssp_version1,
387};
388
389static struct rpc_stat gssp_stats;
390
391static const struct rpc_program gssp_program = {
392	.name		= "gssproxy",
393	.number		= GSSPROXY_PROGRAM,
394	.nrvers		= ARRAY_SIZE(gssp_version),
395	.version	= gssp_version,
396	.stats		= &gssp_stats,
397};
v4.6
 
  1/*
  2 *  linux/net/sunrpc/gss_rpc_upcall.c
  3 *
  4 *  Copyright (C) 2012 Simo Sorce <simo@redhat.com>
  5 *
  6 * This program is free software; you can redistribute it and/or modify
  7 * it under the terms of the GNU General Public License as published by
  8 * the Free Software Foundation; either version 2 of the License, or
  9 * (at your option) any later version.
 10 *
 11 * This program is distributed in the hope that it will be useful,
 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 14 * GNU General Public License for more details.
 15 *
 16 * You should have received a copy of the GNU General Public License
 17 * along with this program; if not, write to the Free Software
 18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 19 */
 20
 21#include <linux/types.h>
 22#include <linux/un.h>
 23
 24#include <linux/sunrpc/svcauth.h>
 25#include "gss_rpc_upcall.h"
 26
 27#define GSSPROXY_SOCK_PATHNAME	"/var/run/gssproxy.sock"
 28
 29#define GSSPROXY_PROGRAM	(400112u)
 30#define GSSPROXY_VERS_1		(1u)
 31
 32/*
 33 * Encoding/Decoding functions
 34 */
 35
 36enum {
 37	GSSX_NULL = 0,	/* Unused */
 38        GSSX_INDICATE_MECHS = 1,
 39        GSSX_GET_CALL_CONTEXT = 2,
 40        GSSX_IMPORT_AND_CANON_NAME = 3,
 41        GSSX_EXPORT_CRED = 4,
 42        GSSX_IMPORT_CRED = 5,
 43        GSSX_ACQUIRE_CRED = 6,
 44        GSSX_STORE_CRED = 7,
 45        GSSX_INIT_SEC_CONTEXT = 8,
 46        GSSX_ACCEPT_SEC_CONTEXT = 9,
 47        GSSX_RELEASE_HANDLE = 10,
 48        GSSX_GET_MIC = 11,
 49        GSSX_VERIFY = 12,
 50        GSSX_WRAP = 13,
 51        GSSX_UNWRAP = 14,
 52        GSSX_WRAP_SIZE_LIMIT = 15,
 53};
 54
 55#define PROC(proc, name)				\
 56[GSSX_##proc] = {					\
 57	.p_proc   = GSSX_##proc,			\
 58	.p_encode = (kxdreproc_t)gssx_enc_##name,	\
 59	.p_decode = (kxdrdproc_t)gssx_dec_##name,	\
 60	.p_arglen = GSSX_ARG_##name##_sz,		\
 61	.p_replen = GSSX_RES_##name##_sz, 		\
 62	.p_statidx = GSSX_##proc,			\
 63	.p_name   = #proc,				\
 64}
 65
 66static struct rpc_procinfo gssp_procedures[] = {
 67	PROC(INDICATE_MECHS, indicate_mechs),
 68        PROC(GET_CALL_CONTEXT, get_call_context),
 69        PROC(IMPORT_AND_CANON_NAME, import_and_canon_name),
 70        PROC(EXPORT_CRED, export_cred),
 71        PROC(IMPORT_CRED, import_cred),
 72        PROC(ACQUIRE_CRED, acquire_cred),
 73        PROC(STORE_CRED, store_cred),
 74        PROC(INIT_SEC_CONTEXT, init_sec_context),
 75        PROC(ACCEPT_SEC_CONTEXT, accept_sec_context),
 76        PROC(RELEASE_HANDLE, release_handle),
 77        PROC(GET_MIC, get_mic),
 78        PROC(VERIFY, verify),
 79        PROC(WRAP, wrap),
 80        PROC(UNWRAP, unwrap),
 81        PROC(WRAP_SIZE_LIMIT, wrap_size_limit),
 82};
 83
 84
 85
 86/*
 87 * Common transport functions
 88 */
 89
 90static const struct rpc_program gssp_program;
 91
 92static int gssp_rpc_create(struct net *net, struct rpc_clnt **_clnt)
 93{
 94	static const struct sockaddr_un gssp_localaddr = {
 95		.sun_family		= AF_LOCAL,
 96		.sun_path		= GSSPROXY_SOCK_PATHNAME,
 97	};
 98	struct rpc_create_args args = {
 99		.net		= net,
100		.protocol	= XPRT_TRANSPORT_LOCAL,
101		.address	= (struct sockaddr *)&gssp_localaddr,
102		.addrsize	= sizeof(gssp_localaddr),
103		.servername	= "localhost",
104		.program	= &gssp_program,
105		.version	= GSSPROXY_VERS_1,
106		.authflavor	= RPC_AUTH_NULL,
107		/*
108		 * Note we want connection to be done in the caller's
109		 * filesystem namespace.  We therefore turn off the idle
110		 * timeout, which would result in reconnections being
111		 * done without the correct namespace:
112		 */
113		.flags		= RPC_CLNT_CREATE_NOPING |
114				  RPC_CLNT_CREATE_NO_IDLE_TIMEOUT
115	};
116	struct rpc_clnt *clnt;
117	int result = 0;
118
119	clnt = rpc_create(&args);
120	if (IS_ERR(clnt)) {
121		dprintk("RPC:       failed to create AF_LOCAL gssproxy "
122				"client (errno %ld).\n", PTR_ERR(clnt));
123		result = PTR_ERR(clnt);
124		*_clnt = NULL;
125		goto out;
126	}
127
128	dprintk("RPC:       created new gssp local client (gssp_local_clnt: "
129			"%p)\n", clnt);
130	*_clnt = clnt;
131
132out:
133	return result;
134}
135
136void init_gssp_clnt(struct sunrpc_net *sn)
137{
138	mutex_init(&sn->gssp_lock);
139	sn->gssp_clnt = NULL;
140}
141
142int set_gssp_clnt(struct net *net)
143{
144	struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
145	struct rpc_clnt *clnt;
146	int ret;
147
148	mutex_lock(&sn->gssp_lock);
149	ret = gssp_rpc_create(net, &clnt);
150	if (!ret) {
151		if (sn->gssp_clnt)
152			rpc_shutdown_client(sn->gssp_clnt);
153		sn->gssp_clnt = clnt;
154	}
155	mutex_unlock(&sn->gssp_lock);
156	return ret;
157}
158
159void clear_gssp_clnt(struct sunrpc_net *sn)
160{
161	mutex_lock(&sn->gssp_lock);
162	if (sn->gssp_clnt) {
163		rpc_shutdown_client(sn->gssp_clnt);
164		sn->gssp_clnt = NULL;
165	}
166	mutex_unlock(&sn->gssp_lock);
167}
168
169static struct rpc_clnt *get_gssp_clnt(struct sunrpc_net *sn)
170{
171	struct rpc_clnt *clnt;
172
173	mutex_lock(&sn->gssp_lock);
174	clnt = sn->gssp_clnt;
175	if (clnt)
176		atomic_inc(&clnt->cl_count);
177	mutex_unlock(&sn->gssp_lock);
178	return clnt;
179}
180
181static int gssp_call(struct net *net, struct rpc_message *msg)
182{
183	struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
184	struct rpc_clnt *clnt;
185	int status;
186
187	clnt = get_gssp_clnt(sn);
188	if (!clnt)
189		return -EIO;
190	status = rpc_call_sync(clnt, msg, 0);
191	if (status < 0) {
192		dprintk("gssp: rpc_call returned error %d\n", -status);
193		switch (status) {
194		case -EPROTONOSUPPORT:
195			status = -EINVAL;
196			break;
197		case -ECONNREFUSED:
198		case -ETIMEDOUT:
199		case -ENOTCONN:
200			status = -EAGAIN;
201			break;
202		case -ERESTARTSYS:
203			if (signalled ())
204				status = -EINTR;
205			break;
206		default:
207			break;
208		}
209	}
210	rpc_release_client(clnt);
211	return status;
212}
213
214static void gssp_free_receive_pages(struct gssx_arg_accept_sec_context *arg)
215{
216	int i;
217
218	for (i = 0; i < arg->npages && arg->pages[i]; i++)
219		__free_page(arg->pages[i]);
220
221	kfree(arg->pages);
222}
223
224static int gssp_alloc_receive_pages(struct gssx_arg_accept_sec_context *arg)
225{
226	arg->npages = DIV_ROUND_UP(NGROUPS_MAX * 4, PAGE_SIZE);
227	arg->pages = kzalloc(arg->npages * sizeof(struct page *), GFP_KERNEL);
228	/*
229	 * XXX: actual pages are allocated by xdr layer in
230	 * xdr_partial_copy_from_skb.
231	 */
232	if (!arg->pages)
233		return -ENOMEM;
234	return 0;
235}
236
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
237/*
238 * Public functions
239 */
240
241/* numbers somewhat arbitrary but large enough for current needs */
242#define GSSX_MAX_OUT_HANDLE	128
243#define GSSX_MAX_SRC_PRINC	256
244#define GSSX_KMEMBUF (GSSX_max_output_handle_sz + \
245			GSSX_max_oid_sz + \
246			GSSX_max_princ_sz + \
247			sizeof(struct svc_cred))
248
249int gssp_accept_sec_context_upcall(struct net *net,
250				struct gssp_upcall_data *data)
251{
252	struct gssx_ctx ctxh = {
253		.state = data->in_handle
254	};
255	struct gssx_arg_accept_sec_context arg = {
256		.input_token = data->in_token,
257	};
258	struct gssx_ctx rctxh = {
259		/*
260		 * pass in the max length we expect for each of these
261		 * buffers but let the xdr code kmalloc them:
262		 */
263		.exported_context_token.len = GSSX_max_output_handle_sz,
264		.mech.len = GSS_OID_MAX_LEN,
 
265		.src_name.display_name.len = GSSX_max_princ_sz
266	};
267	struct gssx_res_accept_sec_context res = {
268		.context_handle = &rctxh,
269		.output_token = &data->out_token
270	};
271	struct rpc_message msg = {
272		.rpc_proc = &gssp_procedures[GSSX_ACCEPT_SEC_CONTEXT],
273		.rpc_argp = &arg,
274		.rpc_resp = &res,
275		.rpc_cred = NULL, /* FIXME ? */
276	};
277	struct xdr_netobj client_name = { 0 , NULL };
 
278	int ret;
279
280	if (data->in_handle.len != 0)
281		arg.context_handle = &ctxh;
282	res.output_token->len = GSSX_max_output_token_sz;
283
284	ret = gssp_alloc_receive_pages(&arg);
285	if (ret)
286		return ret;
287
288	/* use nfs/ for targ_name ? */
289
290	ret = gssp_call(net, &msg);
291
292	gssp_free_receive_pages(&arg);
293
294	/* we need to fetch all data even in case of error so
295	 * that we can free special strctures is they have been allocated */
296	data->major_status = res.status.major_status;
297	data->minor_status = res.status.minor_status;
298	if (res.context_handle) {
299		data->out_handle = rctxh.exported_context_token;
300		data->mech_oid.len = rctxh.mech.len;
301		if (rctxh.mech.data)
302			memcpy(data->mech_oid.data, rctxh.mech.data,
303						data->mech_oid.len);
 
 
304		client_name = rctxh.src_name.display_name;
 
305	}
306
307	if (res.options.count == 1) {
308		gssx_buffer *value = &res.options.data[0].value;
309		/* Currently we only decode CREDS_VALUE, if we add
310		 * anything else we'll have to loop and match on the
311		 * option name */
312		if (value->len == 1) {
313			/* steal group info from struct svc_cred */
314			data->creds = *(struct svc_cred *)value->data;
315			data->found_creds = 1;
316		}
317		/* whether we use it or not, free data */
318		kfree(value->data);
319	}
320
321	if (res.options.count != 0) {
322		kfree(res.options.data);
323	}
324
325	/* convert to GSS_NT_HOSTBASED_SERVICE form and set into creds */
326	if (data->found_creds && client_name.data != NULL) {
327		char *c;
328
329		data->creds.cr_raw_principal = kstrndup(client_name.data,
330						client_name.len, GFP_KERNEL);
331
332		data->creds.cr_principal = kstrndup(client_name.data,
333						client_name.len, GFP_KERNEL);
334		if (data->creds.cr_principal) {
335			/* terminate and remove realm part */
336			c = strchr(data->creds.cr_principal, '@');
337			if (c) {
338				*c = '\0';
339
340				/* change service-hostname delimiter */
341				c = strchr(data->creds.cr_principal, '/');
342				if (c) *c = '@';
343			}
344			if (!c) {
345				/* not a service principal */
346				kfree(data->creds.cr_principal);
347				data->creds.cr_principal = NULL;
348			}
349		}
350	}
351	kfree(client_name.data);
 
352
353	return ret;
354}
355
356void gssp_free_upcall_data(struct gssp_upcall_data *data)
357{
358	kfree(data->in_handle.data);
359	kfree(data->out_handle.data);
360	kfree(data->out_token.data);
361	free_svc_cred(&data->creds);
362}
363
364/*
365 * Initialization stuff
366 */
367
368static const struct rpc_version gssp_version1 = {
369	.number		= GSSPROXY_VERS_1,
370	.nrprocs	= ARRAY_SIZE(gssp_procedures),
371	.procs		= gssp_procedures,
 
372};
373
374static const struct rpc_version *gssp_version[] = {
375	NULL,
376	&gssp_version1,
377};
378
379static struct rpc_stat gssp_stats;
380
381static const struct rpc_program gssp_program = {
382	.name		= "gssproxy",
383	.number		= GSSPROXY_PROGRAM,
384	.nrvers		= ARRAY_SIZE(gssp_version),
385	.version	= gssp_version,
386	.stats		= &gssp_stats,
387};