gss_mech_switch.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. /*
  2. * linux/net/sunrpc/gss_mech_switch.c
  3. *
  4. * Copyright (c) 2001 The Regents of the University of Michigan.
  5. * All rights reserved.
  6. *
  7. * J. Bruce Fields <bfields@umich.edu>
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. *
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in the
  17. * documentation and/or other materials provided with the distribution.
  18. * 3. Neither the name of the University nor the names of its
  19. * contributors may be used to endorse or promote products derived
  20. * from this software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  23. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  24. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  25. * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  26. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  29. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  30. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  31. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  32. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. */
  35. #include <linux/types.h>
  36. #include <linux/slab.h>
  37. #include <linux/module.h>
  38. #include <linux/oid_registry.h>
  39. #include <linux/sunrpc/msg_prot.h>
  40. #include <linux/sunrpc/gss_asn1.h>
  41. #include <linux/sunrpc/auth_gss.h>
  42. #include <linux/sunrpc/svcauth_gss.h>
  43. #include <linux/sunrpc/gss_err.h>
  44. #include <linux/sunrpc/sched.h>
  45. #include <linux/sunrpc/gss_api.h>
  46. #include <linux/sunrpc/clnt.h>
  47. #ifdef RPC_DEBUG
  48. # define RPCDBG_FACILITY RPCDBG_AUTH
  49. #endif
  50. static LIST_HEAD(registered_mechs);
  51. static DEFINE_SPINLOCK(registered_mechs_lock);
  52. static void
  53. gss_mech_free(struct gss_api_mech *gm)
  54. {
  55. struct pf_desc *pf;
  56. int i;
  57. for (i = 0; i < gm->gm_pf_num; i++) {
  58. pf = &gm->gm_pfs[i];
  59. kfree(pf->auth_domain_name);
  60. pf->auth_domain_name = NULL;
  61. }
  62. }
  63. static inline char *
  64. make_auth_domain_name(char *name)
  65. {
  66. static char *prefix = "gss/";
  67. char *new;
  68. new = kmalloc(strlen(name) + strlen(prefix) + 1, GFP_KERNEL);
  69. if (new) {
  70. strcpy(new, prefix);
  71. strcat(new, name);
  72. }
  73. return new;
  74. }
  75. static int
  76. gss_mech_svc_setup(struct gss_api_mech *gm)
  77. {
  78. struct pf_desc *pf;
  79. int i, status;
  80. for (i = 0; i < gm->gm_pf_num; i++) {
  81. pf = &gm->gm_pfs[i];
  82. pf->auth_domain_name = make_auth_domain_name(pf->name);
  83. status = -ENOMEM;
  84. if (pf->auth_domain_name == NULL)
  85. goto out;
  86. status = svcauth_gss_register_pseudoflavor(pf->pseudoflavor,
  87. pf->auth_domain_name);
  88. if (status)
  89. goto out;
  90. }
  91. return 0;
  92. out:
  93. gss_mech_free(gm);
  94. return status;
  95. }
  96. int
  97. gss_mech_register(struct gss_api_mech *gm)
  98. {
  99. int status;
  100. status = gss_mech_svc_setup(gm);
  101. if (status)
  102. return status;
  103. spin_lock(&registered_mechs_lock);
  104. list_add(&gm->gm_list, &registered_mechs);
  105. spin_unlock(&registered_mechs_lock);
  106. dprintk("RPC: registered gss mechanism %s\n", gm->gm_name);
  107. return 0;
  108. }
  109. EXPORT_SYMBOL_GPL(gss_mech_register);
  110. void
  111. gss_mech_unregister(struct gss_api_mech *gm)
  112. {
  113. spin_lock(&registered_mechs_lock);
  114. list_del(&gm->gm_list);
  115. spin_unlock(&registered_mechs_lock);
  116. dprintk("RPC: unregistered gss mechanism %s\n", gm->gm_name);
  117. gss_mech_free(gm);
  118. }
  119. EXPORT_SYMBOL_GPL(gss_mech_unregister);
  120. static struct gss_api_mech *gss_mech_get(struct gss_api_mech *gm)
  121. {
  122. __module_get(gm->gm_owner);
  123. return gm;
  124. }
  125. static struct gss_api_mech *
  126. _gss_mech_get_by_name(const char *name)
  127. {
  128. struct gss_api_mech *pos, *gm = NULL;
  129. spin_lock(&registered_mechs_lock);
  130. list_for_each_entry(pos, &registered_mechs, gm_list) {
  131. if (0 == strcmp(name, pos->gm_name)) {
  132. if (try_module_get(pos->gm_owner))
  133. gm = pos;
  134. break;
  135. }
  136. }
  137. spin_unlock(&registered_mechs_lock);
  138. return gm;
  139. }
  140. struct gss_api_mech * gss_mech_get_by_name(const char *name)
  141. {
  142. struct gss_api_mech *gm = NULL;
  143. gm = _gss_mech_get_by_name(name);
  144. if (!gm) {
  145. request_module("rpc-auth-gss-%s", name);
  146. gm = _gss_mech_get_by_name(name);
  147. }
  148. return gm;
  149. }
  150. EXPORT_SYMBOL_GPL(gss_mech_get_by_name);
  151. static struct gss_api_mech *gss_mech_get_by_OID(struct rpcsec_gss_oid *obj)
  152. {
  153. struct gss_api_mech *pos, *gm = NULL;
  154. char buf[32];
  155. if (sprint_oid(obj->data, obj->len, buf, sizeof(buf)) < 0)
  156. return NULL;
  157. dprintk("RPC: %s(%s)\n", __func__, buf);
  158. request_module("rpc-auth-gss-%s", buf);
  159. spin_lock(&registered_mechs_lock);
  160. list_for_each_entry(pos, &registered_mechs, gm_list) {
  161. if (obj->len == pos->gm_oid.len) {
  162. if (0 == memcmp(obj->data, pos->gm_oid.data, obj->len)) {
  163. if (try_module_get(pos->gm_owner))
  164. gm = pos;
  165. break;
  166. }
  167. }
  168. }
  169. spin_unlock(&registered_mechs_lock);
  170. return gm;
  171. }
  172. static inline int
  173. mech_supports_pseudoflavor(struct gss_api_mech *gm, u32 pseudoflavor)
  174. {
  175. int i;
  176. for (i = 0; i < gm->gm_pf_num; i++) {
  177. if (gm->gm_pfs[i].pseudoflavor == pseudoflavor)
  178. return 1;
  179. }
  180. return 0;
  181. }
  182. static struct gss_api_mech *_gss_mech_get_by_pseudoflavor(u32 pseudoflavor)
  183. {
  184. struct gss_api_mech *gm = NULL, *pos;
  185. spin_lock(&registered_mechs_lock);
  186. list_for_each_entry(pos, &registered_mechs, gm_list) {
  187. if (!mech_supports_pseudoflavor(pos, pseudoflavor)) {
  188. module_put(pos->gm_owner);
  189. continue;
  190. }
  191. if (try_module_get(pos->gm_owner))
  192. gm = pos;
  193. break;
  194. }
  195. spin_unlock(&registered_mechs_lock);
  196. return gm;
  197. }
  198. struct gss_api_mech *
  199. gss_mech_get_by_pseudoflavor(u32 pseudoflavor)
  200. {
  201. struct gss_api_mech *gm;
  202. gm = _gss_mech_get_by_pseudoflavor(pseudoflavor);
  203. if (!gm) {
  204. request_module("rpc-auth-gss-%u", pseudoflavor);
  205. gm = _gss_mech_get_by_pseudoflavor(pseudoflavor);
  206. }
  207. return gm;
  208. }
  209. /**
  210. * gss_mech_list_pseudoflavors - Discover registered GSS pseudoflavors
  211. * @array: array to fill in
  212. * @size: size of "array"
  213. *
  214. * Returns the number of array items filled in, or a negative errno.
  215. *
  216. * The returned array is not sorted by any policy. Callers should not
  217. * rely on the order of the items in the returned array.
  218. */
  219. int gss_mech_list_pseudoflavors(rpc_authflavor_t *array_ptr, int size)
  220. {
  221. struct gss_api_mech *pos = NULL;
  222. int j, i = 0;
  223. spin_lock(&registered_mechs_lock);
  224. list_for_each_entry(pos, &registered_mechs, gm_list) {
  225. for (j = 0; j < pos->gm_pf_num; j++) {
  226. if (i >= size) {
  227. spin_unlock(&registered_mechs_lock);
  228. return -ENOMEM;
  229. }
  230. array_ptr[i++] = pos->gm_pfs[j].pseudoflavor;
  231. }
  232. }
  233. spin_unlock(&registered_mechs_lock);
  234. return i;
  235. }
  236. /**
  237. * gss_svc_to_pseudoflavor - map a GSS service number to a pseudoflavor
  238. * @gm: GSS mechanism handle
  239. * @qop: GSS quality-of-protection value
  240. * @service: GSS service value
  241. *
  242. * Returns a matching security flavor, or RPC_AUTH_MAXFLAVOR if none is found.
  243. */
  244. rpc_authflavor_t gss_svc_to_pseudoflavor(struct gss_api_mech *gm, u32 qop,
  245. u32 service)
  246. {
  247. int i;
  248. for (i = 0; i < gm->gm_pf_num; i++) {
  249. if (gm->gm_pfs[i].qop == qop &&
  250. gm->gm_pfs[i].service == service) {
  251. return gm->gm_pfs[i].pseudoflavor;
  252. }
  253. }
  254. return RPC_AUTH_MAXFLAVOR;
  255. }
  256. /**
  257. * gss_mech_info2flavor - look up a pseudoflavor given a GSS tuple
  258. * @info: a GSS mech OID, quality of protection, and service value
  259. *
  260. * Returns a matching pseudoflavor, or RPC_AUTH_MAXFLAVOR if the tuple is
  261. * not supported.
  262. */
  263. rpc_authflavor_t gss_mech_info2flavor(struct rpcsec_gss_info *info)
  264. {
  265. rpc_authflavor_t pseudoflavor;
  266. struct gss_api_mech *gm;
  267. gm = gss_mech_get_by_OID(&info->oid);
  268. if (gm == NULL)
  269. return RPC_AUTH_MAXFLAVOR;
  270. pseudoflavor = gss_svc_to_pseudoflavor(gm, info->qop, info->service);
  271. gss_mech_put(gm);
  272. return pseudoflavor;
  273. }
  274. /**
  275. * gss_mech_flavor2info - look up a GSS tuple for a given pseudoflavor
  276. * @pseudoflavor: GSS pseudoflavor to match
  277. * @info: rpcsec_gss_info structure to fill in
  278. *
  279. * Returns zero and fills in "info" if pseudoflavor matches a
  280. * supported mechanism. Otherwise a negative errno is returned.
  281. */
  282. int gss_mech_flavor2info(rpc_authflavor_t pseudoflavor,
  283. struct rpcsec_gss_info *info)
  284. {
  285. struct gss_api_mech *gm;
  286. int i;
  287. gm = gss_mech_get_by_pseudoflavor(pseudoflavor);
  288. if (gm == NULL)
  289. return -ENOENT;
  290. for (i = 0; i < gm->gm_pf_num; i++) {
  291. if (gm->gm_pfs[i].pseudoflavor == pseudoflavor) {
  292. memcpy(info->oid.data, gm->gm_oid.data, gm->gm_oid.len);
  293. info->oid.len = gm->gm_oid.len;
  294. info->qop = gm->gm_pfs[i].qop;
  295. info->service = gm->gm_pfs[i].service;
  296. gss_mech_put(gm);
  297. return 0;
  298. }
  299. }
  300. gss_mech_put(gm);
  301. return -ENOENT;
  302. }
  303. u32
  304. gss_pseudoflavor_to_service(struct gss_api_mech *gm, u32 pseudoflavor)
  305. {
  306. int i;
  307. for (i = 0; i < gm->gm_pf_num; i++) {
  308. if (gm->gm_pfs[i].pseudoflavor == pseudoflavor)
  309. return gm->gm_pfs[i].service;
  310. }
  311. return 0;
  312. }
  313. EXPORT_SYMBOL_GPL(gss_pseudoflavor_to_service);
  314. char *
  315. gss_service_to_auth_domain_name(struct gss_api_mech *gm, u32 service)
  316. {
  317. int i;
  318. for (i = 0; i < gm->gm_pf_num; i++) {
  319. if (gm->gm_pfs[i].service == service)
  320. return gm->gm_pfs[i].auth_domain_name;
  321. }
  322. return NULL;
  323. }
  324. EXPORT_SYMBOL_GPL(gss_service_to_auth_domain_name);
  325. void
  326. gss_mech_put(struct gss_api_mech * gm)
  327. {
  328. if (gm)
  329. module_put(gm->gm_owner);
  330. }
  331. EXPORT_SYMBOL_GPL(gss_mech_put);
  332. /* The mech could probably be determined from the token instead, but it's just
  333. * as easy for now to pass it in. */
  334. int
  335. gss_import_sec_context(const void *input_token, size_t bufsize,
  336. struct gss_api_mech *mech,
  337. struct gss_ctx **ctx_id,
  338. gfp_t gfp_mask)
  339. {
  340. if (!(*ctx_id = kzalloc(sizeof(**ctx_id), gfp_mask)))
  341. return -ENOMEM;
  342. (*ctx_id)->mech_type = gss_mech_get(mech);
  343. return mech->gm_ops
  344. ->gss_import_sec_context(input_token, bufsize, *ctx_id, gfp_mask);
  345. }
  346. /* gss_get_mic: compute a mic over message and return mic_token. */
  347. u32
  348. gss_get_mic(struct gss_ctx *context_handle,
  349. struct xdr_buf *message,
  350. struct xdr_netobj *mic_token)
  351. {
  352. return context_handle->mech_type->gm_ops
  353. ->gss_get_mic(context_handle,
  354. message,
  355. mic_token);
  356. }
  357. /* gss_verify_mic: check whether the provided mic_token verifies message. */
  358. u32
  359. gss_verify_mic(struct gss_ctx *context_handle,
  360. struct xdr_buf *message,
  361. struct xdr_netobj *mic_token)
  362. {
  363. return context_handle->mech_type->gm_ops
  364. ->gss_verify_mic(context_handle,
  365. message,
  366. mic_token);
  367. }
  368. /*
  369. * This function is called from both the client and server code.
  370. * Each makes guarantees about how much "slack" space is available
  371. * for the underlying function in "buf"'s head and tail while
  372. * performing the wrap.
  373. *
  374. * The client and server code allocate RPC_MAX_AUTH_SIZE extra
  375. * space in both the head and tail which is available for use by
  376. * the wrap function.
  377. *
  378. * Underlying functions should verify they do not use more than
  379. * RPC_MAX_AUTH_SIZE of extra space in either the head or tail
  380. * when performing the wrap.
  381. */
  382. u32
  383. gss_wrap(struct gss_ctx *ctx_id,
  384. int offset,
  385. struct xdr_buf *buf,
  386. struct page **inpages)
  387. {
  388. return ctx_id->mech_type->gm_ops
  389. ->gss_wrap(ctx_id, offset, buf, inpages);
  390. }
  391. u32
  392. gss_unwrap(struct gss_ctx *ctx_id,
  393. int offset,
  394. struct xdr_buf *buf)
  395. {
  396. return ctx_id->mech_type->gm_ops
  397. ->gss_unwrap(ctx_id, offset, buf);
  398. }
  399. /* gss_delete_sec_context: free all resources associated with context_handle.
  400. * Note this differs from the RFC 2744-specified prototype in that we don't
  401. * bother returning an output token, since it would never be used anyway. */
  402. u32
  403. gss_delete_sec_context(struct gss_ctx **context_handle)
  404. {
  405. dprintk("RPC: gss_delete_sec_context deleting %p\n",
  406. *context_handle);
  407. if (!*context_handle)
  408. return GSS_S_NO_CONTEXT;
  409. if ((*context_handle)->internal_ctx_id)
  410. (*context_handle)->mech_type->gm_ops
  411. ->gss_delete_sec_context((*context_handle)
  412. ->internal_ctx_id);
  413. gss_mech_put((*context_handle)->mech_type);
  414. kfree(*context_handle);
  415. *context_handle=NULL;
  416. return GSS_S_COMPLETE;
  417. }