gss_rpc_upcall.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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. #include <linux/types.h>
  21. #include <linux/un.h>
  22. #include <linux/sunrpc/svcauth.h>
  23. #include "gss_rpc_upcall.h"
  24. #define GSSPROXY_SOCK_PATHNAME "/var/run/gssproxy.sock"
  25. #define GSSPROXY_PROGRAM (400112u)
  26. #define GSSPROXY_VERS_1 (1u)
  27. /*
  28. * Encoding/Decoding functions
  29. */
  30. enum {
  31. GSSX_NULL = 0, /* Unused */
  32. GSSX_INDICATE_MECHS = 1,
  33. GSSX_GET_CALL_CONTEXT = 2,
  34. GSSX_IMPORT_AND_CANON_NAME = 3,
  35. GSSX_EXPORT_CRED = 4,
  36. GSSX_IMPORT_CRED = 5,
  37. GSSX_ACQUIRE_CRED = 6,
  38. GSSX_STORE_CRED = 7,
  39. GSSX_INIT_SEC_CONTEXT = 8,
  40. GSSX_ACCEPT_SEC_CONTEXT = 9,
  41. GSSX_RELEASE_HANDLE = 10,
  42. GSSX_GET_MIC = 11,
  43. GSSX_VERIFY = 12,
  44. GSSX_WRAP = 13,
  45. GSSX_UNWRAP = 14,
  46. GSSX_WRAP_SIZE_LIMIT = 15,
  47. };
  48. #define PROC(proc, name) \
  49. [GSSX_##proc] = { \
  50. .p_proc = GSSX_##proc, \
  51. .p_encode = (kxdreproc_t)gssx_enc_##name, \
  52. .p_decode = (kxdrdproc_t)gssx_dec_##name, \
  53. .p_arglen = GSSX_ARG_##name##_sz, \
  54. .p_replen = GSSX_RES_##name##_sz, \
  55. .p_statidx = GSSX_##proc, \
  56. .p_name = #proc, \
  57. }
  58. static struct rpc_procinfo gssp_procedures[] = {
  59. PROC(INDICATE_MECHS, indicate_mechs),
  60. PROC(GET_CALL_CONTEXT, get_call_context),
  61. PROC(IMPORT_AND_CANON_NAME, import_and_canon_name),
  62. PROC(EXPORT_CRED, export_cred),
  63. PROC(IMPORT_CRED, import_cred),
  64. PROC(ACQUIRE_CRED, acquire_cred),
  65. PROC(STORE_CRED, store_cred),
  66. PROC(INIT_SEC_CONTEXT, init_sec_context),
  67. PROC(ACCEPT_SEC_CONTEXT, accept_sec_context),
  68. PROC(RELEASE_HANDLE, release_handle),
  69. PROC(GET_MIC, get_mic),
  70. PROC(VERIFY, verify),
  71. PROC(WRAP, wrap),
  72. PROC(UNWRAP, unwrap),
  73. PROC(WRAP_SIZE_LIMIT, wrap_size_limit),
  74. };
  75. /*
  76. * Common transport functions
  77. */
  78. static const struct rpc_program gssp_program;
  79. static 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. clnt = rpc_create(&args);
  106. if (IS_ERR(clnt)) {
  107. dprintk("RPC: failed to create AF_LOCAL gssproxy "
  108. "client (errno %ld).\n", PTR_ERR(clnt));
  109. result = PTR_ERR(clnt);
  110. *_clnt = NULL;
  111. goto out;
  112. }
  113. dprintk("RPC: created new gssp local client (gssp_local_clnt: "
  114. "%p)\n", clnt);
  115. *_clnt = clnt;
  116. out:
  117. return result;
  118. }
  119. void init_gssp_clnt(struct sunrpc_net *sn)
  120. {
  121. mutex_init(&sn->gssp_lock);
  122. sn->gssp_clnt = NULL;
  123. }
  124. int set_gssp_clnt(struct net *net)
  125. {
  126. struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
  127. struct rpc_clnt *clnt;
  128. int ret;
  129. mutex_lock(&sn->gssp_lock);
  130. ret = gssp_rpc_create(net, &clnt);
  131. if (!ret) {
  132. if (sn->gssp_clnt)
  133. rpc_shutdown_client(sn->gssp_clnt);
  134. sn->gssp_clnt = clnt;
  135. }
  136. mutex_unlock(&sn->gssp_lock);
  137. return ret;
  138. }
  139. void clear_gssp_clnt(struct sunrpc_net *sn)
  140. {
  141. mutex_lock(&sn->gssp_lock);
  142. if (sn->gssp_clnt) {
  143. rpc_shutdown_client(sn->gssp_clnt);
  144. sn->gssp_clnt = NULL;
  145. }
  146. mutex_unlock(&sn->gssp_lock);
  147. }
  148. static struct rpc_clnt *get_gssp_clnt(struct sunrpc_net *sn)
  149. {
  150. struct rpc_clnt *clnt;
  151. mutex_lock(&sn->gssp_lock);
  152. clnt = sn->gssp_clnt;
  153. if (clnt)
  154. atomic_inc(&clnt->cl_count);
  155. mutex_unlock(&sn->gssp_lock);
  156. return clnt;
  157. }
  158. static int gssp_call(struct net *net, struct rpc_message *msg)
  159. {
  160. struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
  161. struct rpc_clnt *clnt;
  162. int status;
  163. clnt = get_gssp_clnt(sn);
  164. if (!clnt)
  165. return -EIO;
  166. status = rpc_call_sync(clnt, msg, 0);
  167. if (status < 0) {
  168. dprintk("gssp: rpc_call returned error %d\n", -status);
  169. switch (status) {
  170. case -EPROTONOSUPPORT:
  171. status = -EINVAL;
  172. break;
  173. case -ECONNREFUSED:
  174. case -ETIMEDOUT:
  175. case -ENOTCONN:
  176. status = -EAGAIN;
  177. break;
  178. case -ERESTARTSYS:
  179. if (signalled ())
  180. status = -EINTR;
  181. break;
  182. default:
  183. break;
  184. }
  185. }
  186. rpc_release_client(clnt);
  187. return status;
  188. }
  189. static void gssp_free_receive_pages(struct gssx_arg_accept_sec_context *arg)
  190. {
  191. int i;
  192. for (i = 0; i < arg->npages && arg->pages[i]; i++)
  193. __free_page(arg->pages[i]);
  194. kfree(arg->pages);
  195. }
  196. static int gssp_alloc_receive_pages(struct gssx_arg_accept_sec_context *arg)
  197. {
  198. arg->npages = DIV_ROUND_UP(NGROUPS_MAX * 4, PAGE_SIZE);
  199. arg->pages = kzalloc(arg->npages * sizeof(struct page *), GFP_KERNEL);
  200. /*
  201. * XXX: actual pages are allocated by xdr layer in
  202. * xdr_partial_copy_from_skb.
  203. */
  204. if (!arg->pages)
  205. return -ENOMEM;
  206. return 0;
  207. }
  208. /*
  209. * Public functions
  210. */
  211. /* numbers somewhat arbitrary but large enough for current needs */
  212. #define GSSX_MAX_OUT_HANDLE 128
  213. #define GSSX_MAX_SRC_PRINC 256
  214. #define GSSX_KMEMBUF (GSSX_max_output_handle_sz + \
  215. GSSX_max_oid_sz + \
  216. GSSX_max_princ_sz + \
  217. sizeof(struct svc_cred))
  218. int gssp_accept_sec_context_upcall(struct net *net,
  219. struct gssp_upcall_data *data)
  220. {
  221. struct gssx_ctx ctxh = {
  222. .state = data->in_handle
  223. };
  224. struct gssx_arg_accept_sec_context arg = {
  225. .input_token = data->in_token,
  226. };
  227. struct gssx_ctx rctxh = {
  228. /*
  229. * pass in the max length we expect for each of these
  230. * buffers but let the xdr code kmalloc them:
  231. */
  232. .exported_context_token.len = GSSX_max_output_handle_sz,
  233. .mech.len = GSS_OID_MAX_LEN,
  234. .src_name.display_name.len = GSSX_max_princ_sz
  235. };
  236. struct gssx_res_accept_sec_context res = {
  237. .context_handle = &rctxh,
  238. .output_token = &data->out_token
  239. };
  240. struct rpc_message msg = {
  241. .rpc_proc = &gssp_procedures[GSSX_ACCEPT_SEC_CONTEXT],
  242. .rpc_argp = &arg,
  243. .rpc_resp = &res,
  244. .rpc_cred = NULL, /* FIXME ? */
  245. };
  246. struct xdr_netobj client_name = { 0 , NULL };
  247. int ret;
  248. if (data->in_handle.len != 0)
  249. arg.context_handle = &ctxh;
  250. res.output_token->len = GSSX_max_output_token_sz;
  251. ret = gssp_alloc_receive_pages(&arg);
  252. if (ret)
  253. return ret;
  254. /* use nfs/ for targ_name ? */
  255. ret = gssp_call(net, &msg);
  256. gssp_free_receive_pages(&arg);
  257. /* we need to fetch all data even in case of error so
  258. * that we can free special strctures is they have been allocated */
  259. data->major_status = res.status.major_status;
  260. data->minor_status = res.status.minor_status;
  261. if (res.context_handle) {
  262. data->out_handle = rctxh.exported_context_token;
  263. data->mech_oid.len = rctxh.mech.len;
  264. if (rctxh.mech.data)
  265. memcpy(data->mech_oid.data, rctxh.mech.data,
  266. data->mech_oid.len);
  267. client_name = rctxh.src_name.display_name;
  268. }
  269. if (res.options.count == 1) {
  270. gssx_buffer *value = &res.options.data[0].value;
  271. /* Currently we only decode CREDS_VALUE, if we add
  272. * anything else we'll have to loop and match on the
  273. * option name */
  274. if (value->len == 1) {
  275. /* steal group info from struct svc_cred */
  276. data->creds = *(struct svc_cred *)value->data;
  277. data->found_creds = 1;
  278. }
  279. /* whether we use it or not, free data */
  280. kfree(value->data);
  281. }
  282. if (res.options.count != 0) {
  283. kfree(res.options.data);
  284. }
  285. /* convert to GSS_NT_HOSTBASED_SERVICE form and set into creds */
  286. if (data->found_creds && client_name.data != NULL) {
  287. char *c;
  288. data->creds.cr_principal = kstrndup(client_name.data,
  289. client_name.len, GFP_KERNEL);
  290. if (data->creds.cr_principal) {
  291. /* terminate and remove realm part */
  292. c = strchr(data->creds.cr_principal, '@');
  293. if (c) {
  294. *c = '\0';
  295. /* change service-hostname delimiter */
  296. c = strchr(data->creds.cr_principal, '/');
  297. if (c) *c = '@';
  298. }
  299. if (!c) {
  300. /* not a service principal */
  301. kfree(data->creds.cr_principal);
  302. data->creds.cr_principal = NULL;
  303. }
  304. }
  305. }
  306. kfree(client_name.data);
  307. return ret;
  308. }
  309. void gssp_free_upcall_data(struct gssp_upcall_data *data)
  310. {
  311. kfree(data->in_handle.data);
  312. kfree(data->out_handle.data);
  313. kfree(data->out_token.data);
  314. free_svc_cred(&data->creds);
  315. }
  316. /*
  317. * Initialization stuff
  318. */
  319. static const struct rpc_version gssp_version1 = {
  320. .number = GSSPROXY_VERS_1,
  321. .nrprocs = ARRAY_SIZE(gssp_procedures),
  322. .procs = gssp_procedures,
  323. };
  324. static const struct rpc_version *gssp_version[] = {
  325. NULL,
  326. &gssp_version1,
  327. };
  328. static struct rpc_stat gssp_stats;
  329. static const struct rpc_program gssp_program = {
  330. .name = "gssproxy",
  331. .number = GSSPROXY_PROGRAM,
  332. .nrvers = ARRAY_SIZE(gssp_version),
  333. .version = gssp_version,
  334. .stats = &gssp_stats,
  335. };