gss_mech_switch.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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/socket.h>
  38. #include <linux/module.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. if (pf->auth_domain_name)
  60. kfree(pf->auth_domain_name);
  61. pf->auth_domain_name = NULL;
  62. }
  63. }
  64. static inline char *
  65. make_auth_domain_name(char *name)
  66. {
  67. static char *prefix = "gss/";
  68. char *new;
  69. new = kmalloc(strlen(name) + strlen(prefix) + 1, GFP_KERNEL);
  70. if (new) {
  71. strcpy(new, prefix);
  72. strcat(new, name);
  73. }
  74. return new;
  75. }
  76. static int
  77. gss_mech_svc_setup(struct gss_api_mech *gm)
  78. {
  79. struct pf_desc *pf;
  80. int i, status;
  81. for (i = 0; i < gm->gm_pf_num; i++) {
  82. pf = &gm->gm_pfs[i];
  83. pf->auth_domain_name = make_auth_domain_name(pf->name);
  84. status = -ENOMEM;
  85. if (pf->auth_domain_name == NULL)
  86. goto out;
  87. status = svcauth_gss_register_pseudoflavor(pf->pseudoflavor,
  88. pf->auth_domain_name);
  89. if (status)
  90. goto out;
  91. }
  92. return 0;
  93. out:
  94. gss_mech_free(gm);
  95. return status;
  96. }
  97. int
  98. gss_mech_register(struct gss_api_mech *gm)
  99. {
  100. int status;
  101. status = gss_mech_svc_setup(gm);
  102. if (status)
  103. return status;
  104. spin_lock(&registered_mechs_lock);
  105. list_add(&gm->gm_list, &registered_mechs);
  106. spin_unlock(&registered_mechs_lock);
  107. dprintk("RPC: registered gss mechanism %s\n", gm->gm_name);
  108. return 0;
  109. }
  110. EXPORT_SYMBOL(gss_mech_register);
  111. void
  112. gss_mech_unregister(struct gss_api_mech *gm)
  113. {
  114. spin_lock(&registered_mechs_lock);
  115. list_del(&gm->gm_list);
  116. spin_unlock(&registered_mechs_lock);
  117. dprintk("RPC: unregistered gss mechanism %s\n", gm->gm_name);
  118. gss_mech_free(gm);
  119. }
  120. EXPORT_SYMBOL(gss_mech_unregister);
  121. struct gss_api_mech *
  122. gss_mech_get(struct gss_api_mech *gm)
  123. {
  124. __module_get(gm->gm_owner);
  125. return gm;
  126. }
  127. EXPORT_SYMBOL(gss_mech_get);
  128. struct gss_api_mech *
  129. gss_mech_get_by_name(const char *name)
  130. {
  131. struct gss_api_mech *pos, *gm = NULL;
  132. spin_lock(&registered_mechs_lock);
  133. list_for_each_entry(pos, &registered_mechs, gm_list) {
  134. if (0 == strcmp(name, pos->gm_name)) {
  135. if (try_module_get(pos->gm_owner))
  136. gm = pos;
  137. break;
  138. }
  139. }
  140. spin_unlock(&registered_mechs_lock);
  141. return gm;
  142. }
  143. EXPORT_SYMBOL(gss_mech_get_by_name);
  144. static inline int
  145. mech_supports_pseudoflavor(struct gss_api_mech *gm, u32 pseudoflavor)
  146. {
  147. int i;
  148. for (i = 0; i < gm->gm_pf_num; i++) {
  149. if (gm->gm_pfs[i].pseudoflavor == pseudoflavor)
  150. return 1;
  151. }
  152. return 0;
  153. }
  154. struct gss_api_mech *
  155. gss_mech_get_by_pseudoflavor(u32 pseudoflavor)
  156. {
  157. struct gss_api_mech *pos, *gm = NULL;
  158. spin_lock(&registered_mechs_lock);
  159. list_for_each_entry(pos, &registered_mechs, gm_list) {
  160. if (!mech_supports_pseudoflavor(pos, pseudoflavor)) {
  161. module_put(pos->gm_owner);
  162. continue;
  163. }
  164. if (try_module_get(pos->gm_owner))
  165. gm = pos;
  166. break;
  167. }
  168. spin_unlock(&registered_mechs_lock);
  169. return gm;
  170. }
  171. EXPORT_SYMBOL(gss_mech_get_by_pseudoflavor);
  172. u32
  173. gss_pseudoflavor_to_service(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 gm->gm_pfs[i].service;
  179. }
  180. return 0;
  181. }
  182. EXPORT_SYMBOL(gss_pseudoflavor_to_service);
  183. char *
  184. gss_service_to_auth_domain_name(struct gss_api_mech *gm, u32 service)
  185. {
  186. int i;
  187. for (i = 0; i < gm->gm_pf_num; i++) {
  188. if (gm->gm_pfs[i].service == service)
  189. return gm->gm_pfs[i].auth_domain_name;
  190. }
  191. return NULL;
  192. }
  193. EXPORT_SYMBOL(gss_service_to_auth_domain_name);
  194. void
  195. gss_mech_put(struct gss_api_mech * gm)
  196. {
  197. module_put(gm->gm_owner);
  198. }
  199. EXPORT_SYMBOL(gss_mech_put);
  200. /* The mech could probably be determined from the token instead, but it's just
  201. * as easy for now to pass it in. */
  202. int
  203. gss_import_sec_context(const void *input_token, size_t bufsize,
  204. struct gss_api_mech *mech,
  205. struct gss_ctx **ctx_id)
  206. {
  207. if (!(*ctx_id = kmalloc(sizeof(**ctx_id), GFP_KERNEL)))
  208. return GSS_S_FAILURE;
  209. memset(*ctx_id, 0, sizeof(**ctx_id));
  210. (*ctx_id)->mech_type = gss_mech_get(mech);
  211. return mech->gm_ops
  212. ->gss_import_sec_context(input_token, bufsize, *ctx_id);
  213. }
  214. /* gss_get_mic: compute a mic over message and return mic_token. */
  215. u32
  216. gss_get_mic(struct gss_ctx *context_handle,
  217. u32 qop,
  218. struct xdr_buf *message,
  219. struct xdr_netobj *mic_token)
  220. {
  221. return context_handle->mech_type->gm_ops
  222. ->gss_get_mic(context_handle,
  223. qop,
  224. message,
  225. mic_token);
  226. }
  227. /* gss_verify_mic: check whether the provided mic_token verifies message. */
  228. u32
  229. gss_verify_mic(struct gss_ctx *context_handle,
  230. struct xdr_buf *message,
  231. struct xdr_netobj *mic_token,
  232. u32 *qstate)
  233. {
  234. return context_handle->mech_type->gm_ops
  235. ->gss_verify_mic(context_handle,
  236. message,
  237. mic_token,
  238. qstate);
  239. }
  240. /* gss_delete_sec_context: free all resources associated with context_handle.
  241. * Note this differs from the RFC 2744-specified prototype in that we don't
  242. * bother returning an output token, since it would never be used anyway. */
  243. u32
  244. gss_delete_sec_context(struct gss_ctx **context_handle)
  245. {
  246. dprintk("RPC: gss_delete_sec_context deleting %p\n",
  247. *context_handle);
  248. if (!*context_handle)
  249. return(GSS_S_NO_CONTEXT);
  250. if ((*context_handle)->internal_ctx_id != 0)
  251. (*context_handle)->mech_type->gm_ops
  252. ->gss_delete_sec_context((*context_handle)
  253. ->internal_ctx_id);
  254. if ((*context_handle)->mech_type)
  255. gss_mech_put((*context_handle)->mech_type);
  256. kfree(*context_handle);
  257. *context_handle=NULL;
  258. return GSS_S_COMPLETE;
  259. }