gss_rpc_upcall.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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. init_waitqueue_head(&sn->gssp_wq);
  124. }
  125. int set_gssp_clnt(struct net *net)
  126. {
  127. struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
  128. struct rpc_clnt *clnt;
  129. int ret;
  130. mutex_lock(&sn->gssp_lock);
  131. ret = gssp_rpc_create(net, &clnt);
  132. if (!ret) {
  133. if (sn->gssp_clnt)
  134. rpc_shutdown_client(sn->gssp_clnt);
  135. sn->gssp_clnt = clnt;
  136. }
  137. mutex_unlock(&sn->gssp_lock);
  138. wake_up(&sn->gssp_wq);
  139. return ret;
  140. }
  141. void clear_gssp_clnt(struct sunrpc_net *sn)
  142. {
  143. mutex_lock(&sn->gssp_lock);
  144. if (sn->gssp_clnt) {
  145. rpc_shutdown_client(sn->gssp_clnt);
  146. sn->gssp_clnt = NULL;
  147. }
  148. mutex_unlock(&sn->gssp_lock);
  149. }
  150. static struct rpc_clnt *get_gssp_clnt(struct sunrpc_net *sn)
  151. {
  152. struct rpc_clnt *clnt;
  153. mutex_lock(&sn->gssp_lock);
  154. clnt = sn->gssp_clnt;
  155. if (clnt)
  156. atomic_inc(&clnt->cl_count);
  157. mutex_unlock(&sn->gssp_lock);
  158. return clnt;
  159. }
  160. static int gssp_call(struct net *net, struct rpc_message *msg)
  161. {
  162. struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
  163. struct rpc_clnt *clnt;
  164. int status;
  165. clnt = get_gssp_clnt(sn);
  166. if (!clnt)
  167. return -EIO;
  168. status = rpc_call_sync(clnt, msg, 0);
  169. if (status < 0) {
  170. dprintk("gssp: rpc_call returned error %d\n", -status);
  171. switch (status) {
  172. case -EPROTONOSUPPORT:
  173. status = -EINVAL;
  174. break;
  175. case -ECONNREFUSED:
  176. case -ETIMEDOUT:
  177. case -ENOTCONN:
  178. status = -EAGAIN;
  179. break;
  180. case -ERESTARTSYS:
  181. if (signalled ())
  182. status = -EINTR;
  183. break;
  184. default:
  185. break;
  186. }
  187. }
  188. rpc_release_client(clnt);
  189. return status;
  190. }
  191. /*
  192. * Public functions
  193. */
  194. /* numbers somewhat arbitrary but large enough for current needs */
  195. #define GSSX_MAX_OUT_HANDLE 128
  196. #define GSSX_MAX_SRC_PRINC 256
  197. #define GSSX_KMEMBUF (GSSX_max_output_handle_sz + \
  198. GSSX_max_oid_sz + \
  199. GSSX_max_princ_sz + \
  200. sizeof(struct svc_cred))
  201. int gssp_accept_sec_context_upcall(struct net *net,
  202. struct gssp_upcall_data *data)
  203. {
  204. struct gssx_ctx ctxh = {
  205. .state = data->in_handle
  206. };
  207. struct gssx_arg_accept_sec_context arg = {
  208. .input_token = data->in_token,
  209. };
  210. struct gssx_ctx rctxh = {
  211. /*
  212. * pass in the max length we expect for each of these
  213. * buffers but let the xdr code kmalloc them:
  214. */
  215. .exported_context_token.len = GSSX_max_output_handle_sz,
  216. .mech.len = GSS_OID_MAX_LEN,
  217. .src_name.display_name.len = GSSX_max_princ_sz
  218. };
  219. struct gssx_res_accept_sec_context res = {
  220. .context_handle = &rctxh,
  221. .output_token = &data->out_token
  222. };
  223. struct rpc_message msg = {
  224. .rpc_proc = &gssp_procedures[GSSX_ACCEPT_SEC_CONTEXT],
  225. .rpc_argp = &arg,
  226. .rpc_resp = &res,
  227. .rpc_cred = NULL, /* FIXME ? */
  228. };
  229. struct xdr_netobj client_name = { 0 , NULL };
  230. int ret;
  231. if (data->in_handle.len != 0)
  232. arg.context_handle = &ctxh;
  233. res.output_token->len = GSSX_max_output_token_sz;
  234. /* use nfs/ for targ_name ? */
  235. ret = gssp_call(net, &msg);
  236. /* we need to fetch all data even in case of error so
  237. * that we can free special strctures is they have been allocated */
  238. data->major_status = res.status.major_status;
  239. data->minor_status = res.status.minor_status;
  240. if (res.context_handle) {
  241. data->out_handle = rctxh.exported_context_token;
  242. data->mech_oid.len = rctxh.mech.len;
  243. memcpy(data->mech_oid.data, rctxh.mech.data,
  244. data->mech_oid.len);
  245. client_name = rctxh.src_name.display_name;
  246. }
  247. if (res.options.count == 1) {
  248. gssx_buffer *value = &res.options.data[0].value;
  249. /* Currently we only decode CREDS_VALUE, if we add
  250. * anything else we'll have to loop and match on the
  251. * option name */
  252. if (value->len == 1) {
  253. /* steal group info from struct svc_cred */
  254. data->creds = *(struct svc_cred *)value->data;
  255. data->found_creds = 1;
  256. }
  257. /* whether we use it or not, free data */
  258. kfree(value->data);
  259. }
  260. if (res.options.count != 0) {
  261. kfree(res.options.data);
  262. }
  263. /* convert to GSS_NT_HOSTBASED_SERVICE form and set into creds */
  264. if (data->found_creds && client_name.data != NULL) {
  265. char *c;
  266. data->creds.cr_principal = kstrndup(client_name.data,
  267. client_name.len, GFP_KERNEL);
  268. if (data->creds.cr_principal) {
  269. /* terminate and remove realm part */
  270. c = strchr(data->creds.cr_principal, '@');
  271. if (c) {
  272. *c = '\0';
  273. /* change service-hostname delimiter */
  274. c = strchr(data->creds.cr_principal, '/');
  275. if (c) *c = '@';
  276. }
  277. if (!c) {
  278. /* not a service principal */
  279. kfree(data->creds.cr_principal);
  280. data->creds.cr_principal = NULL;
  281. }
  282. }
  283. }
  284. kfree(client_name.data);
  285. return ret;
  286. }
  287. void gssp_free_upcall_data(struct gssp_upcall_data *data)
  288. {
  289. kfree(data->in_handle.data);
  290. kfree(data->out_handle.data);
  291. kfree(data->out_token.data);
  292. kfree(data->mech_oid.data);
  293. free_svc_cred(&data->creds);
  294. }
  295. /*
  296. * Initialization stuff
  297. */
  298. static const struct rpc_version gssp_version1 = {
  299. .number = GSSPROXY_VERS_1,
  300. .nrprocs = ARRAY_SIZE(gssp_procedures),
  301. .procs = gssp_procedures,
  302. };
  303. static const struct rpc_version *gssp_version[] = {
  304. NULL,
  305. &gssp_version1,
  306. };
  307. static struct rpc_stat gssp_stats;
  308. static const struct rpc_program gssp_program = {
  309. .name = "gssproxy",
  310. .number = GSSPROXY_PROGRAM,
  311. .nrvers = ARRAY_SIZE(gssp_version),
  312. .version = gssp_version,
  313. .stats = &gssp_stats,
  314. };