/crypto/heimdal/lib/gssapi/krb5/copy_ccache.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 193 lines · 131 code · 30 blank · 32 comment · 30 complexity · 0c9460640724cae1119c78e79b3b8131 MD5 · raw file

  1. /*
  2. * Copyright (c) 2000 - 2001, 2003 Kungliga Tekniska Hรถgskolan
  3. * (Royal Institute of Technology, Stockholm, Sweden).
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. *
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. *
  17. * 3. Neither the name of the Institute nor the names of its contributors
  18. * may be used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
  22. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
  25. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31. * SUCH DAMAGE.
  32. */
  33. #include "gsskrb5_locl.h"
  34. #if 0
  35. OM_uint32
  36. gss_krb5_copy_ccache(OM_uint32 *minor_status,
  37. krb5_context context,
  38. gss_cred_id_t cred,
  39. krb5_ccache out)
  40. {
  41. krb5_error_code kret;
  42. HEIMDAL_MUTEX_lock(&cred->cred_id_mutex);
  43. if (cred->ccache == NULL) {
  44. HEIMDAL_MUTEX_unlock(&cred->cred_id_mutex);
  45. *minor_status = EINVAL;
  46. return GSS_S_FAILURE;
  47. }
  48. kret = krb5_cc_copy_cache(context, cred->ccache, out);
  49. HEIMDAL_MUTEX_unlock(&cred->cred_id_mutex);
  50. if (kret) {
  51. *minor_status = kret;
  52. return GSS_S_FAILURE;
  53. }
  54. *minor_status = 0;
  55. return GSS_S_COMPLETE;
  56. }
  57. #endif
  58. OM_uint32
  59. _gsskrb5_krb5_import_cred(OM_uint32 *minor_status,
  60. krb5_ccache id,
  61. krb5_principal keytab_principal,
  62. krb5_keytab keytab,
  63. gss_cred_id_t *cred)
  64. {
  65. krb5_context context;
  66. krb5_error_code kret;
  67. gsskrb5_cred handle;
  68. OM_uint32 ret;
  69. *cred = NULL;
  70. GSSAPI_KRB5_INIT (&context);
  71. handle = calloc(1, sizeof(*handle));
  72. if (handle == NULL) {
  73. _gsskrb5_clear_status ();
  74. *minor_status = ENOMEM;
  75. return (GSS_S_FAILURE);
  76. }
  77. HEIMDAL_MUTEX_init(&handle->cred_id_mutex);
  78. handle->usage = 0;
  79. if (id) {
  80. char *str;
  81. handle->usage |= GSS_C_INITIATE;
  82. kret = krb5_cc_get_principal(context, id,
  83. &handle->principal);
  84. if (kret) {
  85. free(handle);
  86. *minor_status = kret;
  87. return GSS_S_FAILURE;
  88. }
  89. if (keytab_principal) {
  90. krb5_boolean match;
  91. match = krb5_principal_compare(context,
  92. handle->principal,
  93. keytab_principal);
  94. if (match == FALSE) {
  95. krb5_free_principal(context, handle->principal);
  96. free(handle);
  97. _gsskrb5_clear_status ();
  98. *minor_status = EINVAL;
  99. return GSS_S_FAILURE;
  100. }
  101. }
  102. ret = __gsskrb5_ccache_lifetime(minor_status,
  103. context,
  104. id,
  105. handle->principal,
  106. &handle->lifetime);
  107. if (ret != GSS_S_COMPLETE) {
  108. krb5_free_principal(context, handle->principal);
  109. free(handle);
  110. return ret;
  111. }
  112. kret = krb5_cc_get_full_name(context, id, &str);
  113. if (kret)
  114. goto out;
  115. kret = krb5_cc_resolve(context, str, &handle->ccache);
  116. free(str);
  117. if (kret)
  118. goto out;
  119. }
  120. if (keytab) {
  121. char *str;
  122. handle->usage |= GSS_C_ACCEPT;
  123. if (keytab_principal && handle->principal == NULL) {
  124. kret = krb5_copy_principal(context,
  125. keytab_principal,
  126. &handle->principal);
  127. if (kret)
  128. goto out;
  129. }
  130. kret = krb5_kt_get_full_name(context, keytab, &str);
  131. if (kret)
  132. goto out;
  133. kret = krb5_kt_resolve(context, str, &handle->keytab);
  134. free(str);
  135. if (kret)
  136. goto out;
  137. }
  138. if (id || keytab) {
  139. ret = gss_create_empty_oid_set(minor_status, &handle->mechanisms);
  140. if (ret == GSS_S_COMPLETE)
  141. ret = gss_add_oid_set_member(minor_status, GSS_KRB5_MECHANISM,
  142. &handle->mechanisms);
  143. if (ret != GSS_S_COMPLETE) {
  144. kret = *minor_status;
  145. goto out;
  146. }
  147. }
  148. *minor_status = 0;
  149. *cred = (gss_cred_id_t)handle;
  150. return GSS_S_COMPLETE;
  151. out:
  152. gss_release_oid_set(minor_status, &handle->mechanisms);
  153. if (handle->ccache)
  154. krb5_cc_close(context, handle->ccache);
  155. if (handle->keytab)
  156. krb5_kt_close(context, handle->keytab);
  157. if (handle->principal)
  158. krb5_free_principal(context, handle->principal);
  159. HEIMDAL_MUTEX_destroy(&handle->cred_id_mutex);
  160. free(handle);
  161. *minor_status = kret;
  162. return GSS_S_FAILURE;
  163. }