2 * linux/net/sunrpc/gss_rpc_upcall.c
4 * Copyright (C) 2012 Simo Sorce <simo@redhat.com>
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.
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.
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.
21 #include <linux/types.h>
24 #include <linux/sunrpc/svcauth.h>
25 #include "gss_rpc_upcall.h"
27 #define GSSPROXY_SOCK_PATHNAME "/var/run/gssproxy.sock"
29 #define GSSPROXY_PROGRAM (400112u)
30 #define GSSPROXY_VERS_1 (1u)
33 * Encoding/Decoding functions
37 GSSX_NULL
= 0, /* Unused */
38 GSSX_INDICATE_MECHS
= 1,
39 GSSX_GET_CALL_CONTEXT
= 2,
40 GSSX_IMPORT_AND_CANON_NAME
= 3,
43 GSSX_ACQUIRE_CRED
= 6,
45 GSSX_INIT_SEC_CONTEXT
= 8,
46 GSSX_ACCEPT_SEC_CONTEXT
= 9,
47 GSSX_RELEASE_HANDLE
= 10,
52 GSSX_WRAP_SIZE_LIMIT
= 15,
55 #define PROC(proc, name) \
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, \
66 static 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
),
81 PROC(WRAP_SIZE_LIMIT
, wrap_size_limit
),
87 * Common transport functions
90 static const struct rpc_program gssp_program
;
92 static int gssp_rpc_create(struct net
*net
, struct rpc_clnt
**_clnt
)
94 static const struct sockaddr_un gssp_localaddr
= {
95 .sun_family
= AF_LOCAL
,
96 .sun_path
= GSSPROXY_SOCK_PATHNAME
,
98 struct rpc_create_args args
= {
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
,
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:
113 .flags
= RPC_CLNT_CREATE_NOPING
|
114 RPC_CLNT_CREATE_NO_IDLE_TIMEOUT
116 struct rpc_clnt
*clnt
;
119 clnt
= rpc_create(&args
);
121 dprintk("RPC: failed to create AF_LOCAL gssproxy "
122 "client (errno %ld).\n", PTR_ERR(clnt
));
123 result
= PTR_ERR(clnt
);
128 dprintk("RPC: created new gssp local client (gssp_local_clnt: "
136 void init_gssp_clnt(struct sunrpc_net
*sn
)
138 mutex_init(&sn
->gssp_lock
);
139 sn
->gssp_clnt
= NULL
;
142 int set_gssp_clnt(struct net
*net
)
144 struct sunrpc_net
*sn
= net_generic(net
, sunrpc_net_id
);
145 struct rpc_clnt
*clnt
;
148 mutex_lock(&sn
->gssp_lock
);
149 ret
= gssp_rpc_create(net
, &clnt
);
152 rpc_shutdown_client(sn
->gssp_clnt
);
153 sn
->gssp_clnt
= clnt
;
155 mutex_unlock(&sn
->gssp_lock
);
159 void clear_gssp_clnt(struct sunrpc_net
*sn
)
161 mutex_lock(&sn
->gssp_lock
);
163 rpc_shutdown_client(sn
->gssp_clnt
);
164 sn
->gssp_clnt
= NULL
;
166 mutex_unlock(&sn
->gssp_lock
);
169 static struct rpc_clnt
*get_gssp_clnt(struct sunrpc_net
*sn
)
171 struct rpc_clnt
*clnt
;
173 mutex_lock(&sn
->gssp_lock
);
174 clnt
= sn
->gssp_clnt
;
176 atomic_inc(&clnt
->cl_count
);
177 mutex_unlock(&sn
->gssp_lock
);
181 static int gssp_call(struct net
*net
, struct rpc_message
*msg
)
183 struct sunrpc_net
*sn
= net_generic(net
, sunrpc_net_id
);
184 struct rpc_clnt
*clnt
;
187 clnt
= get_gssp_clnt(sn
);
190 status
= rpc_call_sync(clnt
, msg
, 0);
192 dprintk("gssp: rpc_call returned error %d\n", -status
);
194 case -EPROTONOSUPPORT
:
210 rpc_release_client(clnt
);
214 static void gssp_free_receive_pages(struct gssx_arg_accept_sec_context
*arg
)
218 for (i
= 0; i
< arg
->npages
&& arg
->pages
[i
]; i
++)
219 __free_page(arg
->pages
[i
]);
222 static int gssp_alloc_receive_pages(struct gssx_arg_accept_sec_context
*arg
)
224 arg
->npages
= DIV_ROUND_UP(NGROUPS_MAX
* 4, PAGE_SIZE
);
225 arg
->pages
= kzalloc(arg
->npages
* sizeof(struct page
*), GFP_KERNEL
);
227 * XXX: actual pages are allocated by xdr layer in
228 * xdr_partial_copy_from_skb.
239 /* numbers somewhat arbitrary but large enough for current needs */
240 #define GSSX_MAX_OUT_HANDLE 128
241 #define GSSX_MAX_SRC_PRINC 256
242 #define GSSX_KMEMBUF (GSSX_max_output_handle_sz + \
244 GSSX_max_princ_sz + \
245 sizeof(struct svc_cred))
247 int gssp_accept_sec_context_upcall(struct net
*net
,
248 struct gssp_upcall_data
*data
)
250 struct gssx_ctx ctxh
= {
251 .state
= data
->in_handle
253 struct gssx_arg_accept_sec_context arg
= {
254 .input_token
= data
->in_token
,
256 struct gssx_ctx rctxh
= {
258 * pass in the max length we expect for each of these
259 * buffers but let the xdr code kmalloc them:
261 .exported_context_token
.len
= GSSX_max_output_handle_sz
,
262 .mech
.len
= GSS_OID_MAX_LEN
,
263 .src_name
.display_name
.len
= GSSX_max_princ_sz
265 struct gssx_res_accept_sec_context res
= {
266 .context_handle
= &rctxh
,
267 .output_token
= &data
->out_token
269 struct rpc_message msg
= {
270 .rpc_proc
= &gssp_procedures
[GSSX_ACCEPT_SEC_CONTEXT
],
273 .rpc_cred
= NULL
, /* FIXME ? */
275 struct xdr_netobj client_name
= { 0 , NULL
};
278 if (data
->in_handle
.len
!= 0)
279 arg
.context_handle
= &ctxh
;
280 res
.output_token
->len
= GSSX_max_output_token_sz
;
282 ret
= gssp_alloc_receive_pages(&arg
);
286 /* use nfs/ for targ_name ? */
288 ret
= gssp_call(net
, &msg
);
290 gssp_free_receive_pages(&arg
);
292 /* we need to fetch all data even in case of error so
293 * that we can free special strctures is they have been allocated */
294 data
->major_status
= res
.status
.major_status
;
295 data
->minor_status
= res
.status
.minor_status
;
296 if (res
.context_handle
) {
297 data
->out_handle
= rctxh
.exported_context_token
;
298 data
->mech_oid
.len
= rctxh
.mech
.len
;
299 memcpy(data
->mech_oid
.data
, rctxh
.mech
.data
,
301 client_name
= rctxh
.src_name
.display_name
;
304 if (res
.options
.count
== 1) {
305 gssx_buffer
*value
= &res
.options
.data
[0].value
;
306 /* Currently we only decode CREDS_VALUE, if we add
307 * anything else we'll have to loop and match on the
309 if (value
->len
== 1) {
310 /* steal group info from struct svc_cred */
311 data
->creds
= *(struct svc_cred
*)value
->data
;
312 data
->found_creds
= 1;
314 /* whether we use it or not, free data */
318 if (res
.options
.count
!= 0) {
319 kfree(res
.options
.data
);
322 /* convert to GSS_NT_HOSTBASED_SERVICE form and set into creds */
323 if (data
->found_creds
&& client_name
.data
!= NULL
) {
326 data
->creds
.cr_principal
= kstrndup(client_name
.data
,
327 client_name
.len
, GFP_KERNEL
);
328 if (data
->creds
.cr_principal
) {
329 /* terminate and remove realm part */
330 c
= strchr(data
->creds
.cr_principal
, '@');
334 /* change service-hostname delimiter */
335 c
= strchr(data
->creds
.cr_principal
, '/');
339 /* not a service principal */
340 kfree(data
->creds
.cr_principal
);
341 data
->creds
.cr_principal
= NULL
;
345 kfree(client_name
.data
);
350 void gssp_free_upcall_data(struct gssp_upcall_data
*data
)
352 kfree(data
->in_handle
.data
);
353 kfree(data
->out_handle
.data
);
354 kfree(data
->out_token
.data
);
355 free_svc_cred(&data
->creds
);
359 * Initialization stuff
362 static const struct rpc_version gssp_version1
= {
363 .number
= GSSPROXY_VERS_1
,
364 .nrprocs
= ARRAY_SIZE(gssp_procedures
),
365 .procs
= gssp_procedures
,
368 static const struct rpc_version
*gssp_version
[] = {
373 static struct rpc_stat gssp_stats
;
375 static const struct rpc_program gssp_program
= {
377 .number
= GSSPROXY_PROGRAM
,
378 .nrvers
= ARRAY_SIZE(gssp_version
),
379 .version
= gssp_version
,
380 .stats
= &gssp_stats
,