gss_mech_switch.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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/sunrpc/msg_prot.h>
  39. #include <linux/sunrpc/gss_asn1.h>
  40. #include <linux/sunrpc/auth_gss.h>
  41. #include <linux/sunrpc/svcauth_gss.h>
  42. #include <linux/sunrpc/gss_err.h>
  43. #include <linux/sunrpc/sched.h>
  44. #include <linux/sunrpc/gss_api.h>
  45. #include <linux/sunrpc/clnt.h>
  46. #ifdef RPC_DEBUG
  47. # define RPCDBG_FACILITY RPCDBG_AUTH
  48. #endif
  49. static LIST_HEAD(registered_mechs);
  50. static DEFINE_SPINLOCK(registered_mechs_lock);
  51. static void
  52. gss_mech_free(struct gss_api_mech *gm)
  53. {
  54. struct pf_desc *pf;
  55. int i;
  56. for (i = 0; i < gm->gm_pf_num; i++) {
  57. pf = &gm->gm_pfs[i];
  58. kfree(pf->auth_domain_name);
  59. pf->auth_domain_name = NULL;
  60. }
  61. }
  62. static inline char *
  63. make_auth_domain_name(char *name)
  64. {
  65. static char *prefix = "gss/";
  66. char *new;
  67. new = kmalloc(strlen(name) + strlen(prefix) + 1, GFP_KERNEL);
  68. if (new) {
  69. strcpy(new, prefix);
  70. strcat(new, name);
  71. }
  72. return new;
  73. }
  74. static int
  75. gss_mech_svc_setup(struct gss_api_mech *gm)
  76. {
  77. struct pf_desc *pf;
  78. int i, status;
  79. for (i = 0; i < gm->gm_pf_num; i++) {
  80. pf = &gm->gm_pfs[i];
  81. pf->auth_domain_name = make_auth_domain_name(pf->name);
  82. status = -ENOMEM;
  83. if (pf->auth_domain_name == NULL)
  84. goto out;
  85. status = svcauth_gss_register_pseudoflavor(pf->pseudoflavor,
  86. pf->auth_domain_name);
  87. if (status)
  88. goto out;
  89. }
  90. return 0;
  91. out:
  92. gss_mech_free(gm);
  93. return status;
  94. }
  95. int
  96. gss_mech_register(struct gss_api_mech *gm)
  97. {
  98. int status;
  99. status = gss_mech_svc_setup(gm);
  100. if (status)
  101. return status;
  102. spin_lock(&registered_mechs_lock);
  103. list_add(&gm->gm_list, &registered_mechs);
  104. spin_unlock(&registered_mechs_lock);
  105. dprintk("RPC: registered gss mechanism %s\n", gm->gm_name);
  106. return 0;
  107. }
  108. EXPORT_SYMBOL_GPL(gss_mech_register);
  109. void
  110. gss_mech_unregister(struct gss_api_mech *gm)
  111. {
  112. spin_lock(&registered_mechs_lock);
  113. list_del(&gm->gm_list);
  114. spin_unlock(&registered_mechs_lock);
  115. dprintk("RPC: unregistered gss mechanism %s\n", gm->gm_name);
  116. gss_mech_free(gm);
  117. }
  118. EXPORT_SYMBOL_GPL(gss_mech_unregister);
  119. struct gss_api_mech *
  120. gss_mech_get(struct gss_api_mech *gm)
  121. {
  122. __module_get(gm->gm_owner);
  123. return gm;
  124. }
  125. EXPORT_SYMBOL_GPL(gss_mech_get);
  126. struct gss_api_mech *
  127. _gss_mech_get_by_name(const char *name)
  128. {
  129. struct gss_api_mech *pos, *gm = NULL;
  130. spin_lock(&registered_mechs_lock);
  131. list_for_each_entry(pos, &registered_mechs, gm_list) {
  132. if (0 == strcmp(name, pos->gm_name)) {
  133. if (try_module_get(pos->gm_owner))
  134. gm = pos;
  135. break;
  136. }
  137. }
  138. spin_unlock(&registered_mechs_lock);
  139. return gm;
  140. }
  141. struct gss_api_mech * gss_mech_get_by_name(const char *name)
  142. {
  143. struct gss_api_mech *gm = NULL;
  144. gm = _gss_mech_get_by_name(name);
  145. if (!gm) {
  146. request_module("rpc-auth-gss-%s", name);
  147. gm = _gss_mech_get_by_name(name);
  148. }
  149. return gm;
  150. }
  151. EXPORT_SYMBOL_GPL(gss_mech_get_by_name);
  152. struct gss_api_mech *
  153. gss_mech_get_by_OID(struct xdr_netobj *obj)
  154. {
  155. struct gss_api_mech *pos, *gm = NULL;
  156. spin_lock(&registered_mechs_lock);
  157. list_for_each_entry(pos, &registered_mechs, gm_list) {
  158. if (obj->len == pos->gm_oid.len) {
  159. if (0 == memcmp(obj->data, pos->gm_oid.data, obj->len)) {
  160. if (try_module_get(pos->gm_owner))
  161. gm = pos;
  162. break;
  163. }
  164. }
  165. }
  166. spin_unlock(&registered_mechs_lock);
  167. return gm;
  168. }
  169. EXPORT_SYMBOL_GPL(gss_mech_get_by_OID);
  170. static inline int
  171. mech_supports_pseudoflavor(struct gss_api_mech *gm, u32 pseudoflavor)
  172. {
  173. int i;
  174. for (i = 0; i < gm->gm_pf_num; i++) {
  175. if (gm->gm_pfs[i].pseudoflavor == pseudoflavor)
  176. return 1;
  177. }
  178. return 0;
  179. }
  180. struct gss_api_mech *_gss_mech_get_by_pseudoflavor(u32 pseudoflavor)
  181. {
  182. struct gss_api_mech *gm = NULL, *pos;
  183. spin_lock(&registered_mechs_lock);
  184. list_for_each_entry(pos, &registered_mechs, gm_list) {
  185. if (!mech_supports_pseudoflavor(pos, pseudoflavor)) {
  186. module_put(pos->gm_owner);
  187. continue;
  188. }
  189. if (try_module_get(pos->gm_owner))
  190. gm = pos;
  191. break;
  192. }
  193. spin_unlock(&registered_mechs_lock);
  194. return gm;
  195. }
  196. struct gss_api_mech *
  197. gss_mech_get_by_pseudoflavor(u32 pseudoflavor)
  198. {
  199. struct gss_api_mech *gm;
  200. gm = _gss_mech_get_by_pseudoflavor(pseudoflavor);
  201. if (!gm) {
  202. request_module("rpc-auth-gss-%u", pseudoflavor);
  203. gm = _gss_mech_get_by_pseudoflavor(pseudoflavor);
  204. }
  205. return gm;
  206. }
  207. EXPORT_SYMBOL_GPL(gss_mech_get_by_pseudoflavor);
  208. int gss_mech_list_pseudoflavors(rpc_authflavor_t *array_ptr)
  209. {
  210. struct gss_api_mech *pos = NULL;
  211. int j, i = 0;
  212. spin_lock(&registered_mechs_lock);
  213. list_for_each_entry(pos, &registered_mechs, gm_list) {
  214. for (j=0; j < pos->gm_pf_num; j++) {
  215. array_ptr[i++] = pos->gm_pfs[j].pseudoflavor;
  216. }
  217. }
  218. spin_unlock(&registered_mechs_lock);
  219. return i;
  220. }
  221. EXPORT_SYMBOL_GPL(gss_mech_list_pseudoflavors);
  222. u32
  223. gss_svc_to_pseudoflavor(struct gss_api_mech *gm, u32 service)
  224. {
  225. int i;
  226. for (i = 0; i < gm->gm_pf_num; i++) {
  227. if (gm->gm_pfs[i].service == service) {
  228. return gm->gm_pfs[i].pseudoflavor;
  229. }
  230. }
  231. return RPC_AUTH_MAXFLAVOR; /* illegal value */
  232. }
  233. EXPORT_SYMBOL_GPL(gss_svc_to_pseudoflavor);
  234. u32
  235. gss_pseudoflavor_to_service(struct gss_api_mech *gm, u32 pseudoflavor)
  236. {
  237. int i;
  238. for (i = 0; i < gm->gm_pf_num; i++) {
  239. if (gm->gm_pfs[i].pseudoflavor == pseudoflavor)
  240. return gm->gm_pfs[i].service;
  241. }
  242. return 0;
  243. }
  244. EXPORT_SYMBOL_GPL(gss_pseudoflavor_to_service);
  245. char *
  246. gss_service_to_auth_domain_name(struct gss_api_mech *gm, u32 service)
  247. {
  248. int i;
  249. for (i = 0; i < gm->gm_pf_num; i++) {
  250. if (gm->gm_pfs[i].service == service)
  251. return gm->gm_pfs[i].auth_domain_name;
  252. }
  253. return NULL;
  254. }
  255. EXPORT_SYMBOL_GPL(gss_service_to_auth_domain_name);
  256. void
  257. gss_mech_put(struct gss_api_mech * gm)
  258. {
  259. if (gm)
  260. module_put(gm->gm_owner);
  261. }
  262. EXPORT_SYMBOL_GPL(gss_mech_put);
  263. /* The mech could probably be determined from the token instead, but it's just
  264. * as easy for now to pass it in. */
  265. int
  266. gss_import_sec_context(const void *input_token, size_t bufsize,
  267. struct gss_api_mech *mech,
  268. struct gss_ctx **ctx_id,
  269. gfp_t gfp_mask)
  270. {
  271. if (!(*ctx_id = kzalloc(sizeof(**ctx_id), gfp_mask)))
  272. return -ENOMEM;
  273. (*ctx_id)->mech_type = gss_mech_get(mech);
  274. return mech->gm_ops
  275. ->gss_import_sec_context(input_token, bufsize, *ctx_id, gfp_mask);
  276. }
  277. /* gss_get_mic: compute a mic over message and return mic_token. */
  278. u32
  279. gss_get_mic(struct gss_ctx *context_handle,
  280. struct xdr_buf *message,
  281. struct xdr_netobj *mic_token)
  282. {
  283. return context_handle->mech_type->gm_ops
  284. ->gss_get_mic(context_handle,
  285. message,
  286. mic_token);
  287. }
  288. /* gss_verify_mic: check whether the provided mic_token verifies message. */
  289. u32
  290. gss_verify_mic(struct gss_ctx *context_handle,
  291. struct xdr_buf *message,
  292. struct xdr_netobj *mic_token)
  293. {
  294. return context_handle->mech_type->gm_ops
  295. ->gss_verify_mic(context_handle,
  296. message,
  297. mic_token);
  298. }
  299. /*
  300. * This function is called from both the client and server code.
  301. * Each makes guarantees about how much "slack" space is available
  302. * for the underlying function in "buf"'s head and tail while
  303. * performing the wrap.
  304. *
  305. * The client and server code allocate RPC_MAX_AUTH_SIZE extra
  306. * space in both the head and tail which is available for use by
  307. * the wrap function.
  308. *
  309. * Underlying functions should verify they do not use more than
  310. * RPC_MAX_AUTH_SIZE of extra space in either the head or tail
  311. * when performing the wrap.
  312. */
  313. u32
  314. gss_wrap(struct gss_ctx *ctx_id,
  315. int offset,
  316. struct xdr_buf *buf,
  317. struct page **inpages)
  318. {
  319. return ctx_id->mech_type->gm_ops
  320. ->gss_wrap(ctx_id, offset, buf, inpages);
  321. }
  322. u32
  323. gss_unwrap(struct gss_ctx *ctx_id,
  324. int offset,
  325. struct xdr_buf *buf)
  326. {
  327. return ctx_id->mech_type->gm_ops
  328. ->gss_unwrap(ctx_id, offset, buf);
  329. }
  330. /* gss_delete_sec_context: free all resources associated with context_handle.
  331. * Note this differs from the RFC 2744-specified prototype in that we don't
  332. * bother returning an output token, since it would never be used anyway. */
  333. u32
  334. gss_delete_sec_context(struct gss_ctx **context_handle)
  335. {
  336. dprintk("RPC: gss_delete_sec_context deleting %p\n",
  337. *context_handle);
  338. if (!*context_handle)
  339. return GSS_S_NO_CONTEXT;
  340. if ((*context_handle)->internal_ctx_id)
  341. (*context_handle)->mech_type->gm_ops
  342. ->gss_delete_sec_context((*context_handle)
  343. ->internal_ctx_id);
  344. gss_mech_put((*context_handle)->mech_type);
  345. kfree(*context_handle);
  346. *context_handle=NULL;
  347. return GSS_S_COMPLETE;
  348. }