gss_rpc_upcall.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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 = gssx_enc_##name, \
  52. .p_decode = 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 const 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 = kcalloc(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. static char *gssp_stringify(struct xdr_netobj *netobj)
  209. {
  210. return kstrndup(netobj->data, netobj->len, GFP_KERNEL);
  211. }
  212. static void gssp_hostbased_service(char **principal)
  213. {
  214. char *c;
  215. if (!*principal)
  216. return;
  217. /* terminate and remove realm part */
  218. c = strchr(*principal, '@');
  219. if (c) {
  220. *c = '\0';
  221. /* change service-hostname delimiter */
  222. c = strchr(*principal, '/');
  223. if (c)
  224. *c = '@';
  225. }
  226. if (!c) {
  227. /* not a service principal */
  228. kfree(*principal);
  229. *principal = NULL;
  230. }
  231. }
  232. /*
  233. * Public functions
  234. */
  235. /* numbers somewhat arbitrary but large enough for current needs */
  236. #define GSSX_MAX_OUT_HANDLE 128
  237. #define GSSX_MAX_SRC_PRINC 256
  238. #define GSSX_KMEMBUF (GSSX_max_output_handle_sz + \
  239. GSSX_max_oid_sz + \
  240. GSSX_max_princ_sz + \
  241. sizeof(struct svc_cred))
  242. int gssp_accept_sec_context_upcall(struct net *net,
  243. struct gssp_upcall_data *data)
  244. {
  245. struct gssx_ctx ctxh = {
  246. .state = data->in_handle
  247. };
  248. struct gssx_arg_accept_sec_context arg = {
  249. .input_token = data->in_token,
  250. };
  251. struct gssx_ctx rctxh = {
  252. /*
  253. * pass in the max length we expect for each of these
  254. * buffers but let the xdr code kmalloc them:
  255. */
  256. .exported_context_token.len = GSSX_max_output_handle_sz,
  257. .mech.len = GSS_OID_MAX_LEN,
  258. .targ_name.display_name.len = GSSX_max_princ_sz,
  259. .src_name.display_name.len = GSSX_max_princ_sz
  260. };
  261. struct gssx_res_accept_sec_context res = {
  262. .context_handle = &rctxh,
  263. .output_token = &data->out_token
  264. };
  265. struct rpc_message msg = {
  266. .rpc_proc = &gssp_procedures[GSSX_ACCEPT_SEC_CONTEXT],
  267. .rpc_argp = &arg,
  268. .rpc_resp = &res,
  269. .rpc_cred = NULL, /* FIXME ? */
  270. };
  271. struct xdr_netobj client_name = { 0 , NULL };
  272. struct xdr_netobj target_name = { 0, NULL };
  273. int ret;
  274. if (data->in_handle.len != 0)
  275. arg.context_handle = &ctxh;
  276. res.output_token->len = GSSX_max_output_token_sz;
  277. ret = gssp_alloc_receive_pages(&arg);
  278. if (ret)
  279. return ret;
  280. ret = gssp_call(net, &msg);
  281. gssp_free_receive_pages(&arg);
  282. /* we need to fetch all data even in case of error so
  283. * that we can free special strctures is they have been allocated */
  284. data->major_status = res.status.major_status;
  285. data->minor_status = res.status.minor_status;
  286. if (res.context_handle) {
  287. data->out_handle = rctxh.exported_context_token;
  288. data->mech_oid.len = rctxh.mech.len;
  289. if (rctxh.mech.data) {
  290. memcpy(data->mech_oid.data, rctxh.mech.data,
  291. data->mech_oid.len);
  292. kfree(rctxh.mech.data);
  293. }
  294. client_name = rctxh.src_name.display_name;
  295. target_name = rctxh.targ_name.display_name;
  296. }
  297. if (res.options.count == 1) {
  298. gssx_buffer *value = &res.options.data[0].value;
  299. /* Currently we only decode CREDS_VALUE, if we add
  300. * anything else we'll have to loop and match on the
  301. * option name */
  302. if (value->len == 1) {
  303. /* steal group info from struct svc_cred */
  304. data->creds = *(struct svc_cred *)value->data;
  305. data->found_creds = 1;
  306. }
  307. /* whether we use it or not, free data */
  308. kfree(value->data);
  309. }
  310. if (res.options.count != 0) {
  311. kfree(res.options.data);
  312. }
  313. /* convert to GSS_NT_HOSTBASED_SERVICE form and set into creds */
  314. if (data->found_creds) {
  315. if (client_name.data) {
  316. data->creds.cr_raw_principal =
  317. gssp_stringify(&client_name);
  318. data->creds.cr_principal =
  319. gssp_stringify(&client_name);
  320. gssp_hostbased_service(&data->creds.cr_principal);
  321. }
  322. if (target_name.data) {
  323. data->creds.cr_targ_princ =
  324. gssp_stringify(&target_name);
  325. gssp_hostbased_service(&data->creds.cr_targ_princ);
  326. }
  327. }
  328. kfree(client_name.data);
  329. kfree(target_name.data);
  330. return ret;
  331. }
  332. void gssp_free_upcall_data(struct gssp_upcall_data *data)
  333. {
  334. kfree(data->in_handle.data);
  335. kfree(data->out_handle.data);
  336. kfree(data->out_token.data);
  337. free_svc_cred(&data->creds);
  338. }
  339. /*
  340. * Initialization stuff
  341. */
  342. static unsigned int gssp_version1_counts[ARRAY_SIZE(gssp_procedures)];
  343. static const struct rpc_version gssp_version1 = {
  344. .number = GSSPROXY_VERS_1,
  345. .nrprocs = ARRAY_SIZE(gssp_procedures),
  346. .procs = gssp_procedures,
  347. .counts = gssp_version1_counts,
  348. };
  349. static const struct rpc_version *gssp_version[] = {
  350. NULL,
  351. &gssp_version1,
  352. };
  353. static struct rpc_stat gssp_stats;
  354. static const struct rpc_program gssp_program = {
  355. .name = "gssproxy",
  356. .number = GSSPROXY_PROGRAM,
  357. .nrvers = ARRAY_SIZE(gssp_version),
  358. .version = gssp_version,
  359. .stats = &gssp_stats,
  360. };