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

https://bitbucket.org/freebsd/freebsd-head/ · C · 260 lines · 183 code · 43 blank · 34 comment · 71 complexity · 08e8c77cb6d7e91f5189f4f94506688b MD5 · raw file

  1. /*
  2. * Copyright (c) 2004, PADL Software Pty Ltd.
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. *
  16. * 3. Neither the name of PADL Software nor the names of its contributors
  17. * may be used to endorse or promote products derived from this software
  18. * without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY PADL SOFTWARE AND CONTRIBUTORS ``AS IS'' AND
  21. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23. * ARE DISCLAIMED. IN NO EVENT SHALL PADL SOFTWARE OR CONTRIBUTORS BE LIABLE
  24. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  25. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  26. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  27. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  28. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  29. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  30. * SUCH DAMAGE.
  31. */
  32. /*
  33. * glue routine for _gsskrb5_inquire_sec_context_by_oid
  34. */
  35. #include "gsskrb5_locl.h"
  36. static OM_uint32
  37. get_bool(OM_uint32 *minor_status,
  38. const gss_buffer_t value,
  39. int *flag)
  40. {
  41. if (value->value == NULL || value->length != 1) {
  42. *minor_status = EINVAL;
  43. return GSS_S_FAILURE;
  44. }
  45. *flag = *((const char *)value->value) != 0;
  46. return GSS_S_COMPLETE;
  47. }
  48. static OM_uint32
  49. get_string(OM_uint32 *minor_status,
  50. const gss_buffer_t value,
  51. char **str)
  52. {
  53. if (value == NULL || value->length == 0) {
  54. *str = NULL;
  55. } else {
  56. *str = malloc(value->length + 1);
  57. if (*str == NULL) {
  58. *minor_status = 0;
  59. return GSS_S_UNAVAILABLE;
  60. }
  61. memcpy(*str, value->value, value->length);
  62. (*str)[value->length] = '\0';
  63. }
  64. return GSS_S_COMPLETE;
  65. }
  66. static OM_uint32
  67. get_int32(OM_uint32 *minor_status,
  68. const gss_buffer_t value,
  69. OM_uint32 *ret)
  70. {
  71. *minor_status = 0;
  72. if (value == NULL || value->length == 0)
  73. *ret = 0;
  74. else if (value->length == sizeof(*ret))
  75. memcpy(ret, value->value, sizeof(*ret));
  76. else
  77. return GSS_S_UNAVAILABLE;
  78. return GSS_S_COMPLETE;
  79. }
  80. static OM_uint32
  81. set_int32(OM_uint32 *minor_status,
  82. const gss_buffer_t value,
  83. OM_uint32 set)
  84. {
  85. *minor_status = 0;
  86. if (value->length == sizeof(set))
  87. memcpy(value->value, &set, sizeof(set));
  88. else
  89. return GSS_S_UNAVAILABLE;
  90. return GSS_S_COMPLETE;
  91. }
  92. OM_uint32 GSSAPI_CALLCONV
  93. _gsskrb5_set_sec_context_option
  94. (OM_uint32 *minor_status,
  95. gss_ctx_id_t *context_handle,
  96. const gss_OID desired_object,
  97. const gss_buffer_t value)
  98. {
  99. krb5_context context;
  100. OM_uint32 maj_stat;
  101. GSSAPI_KRB5_INIT (&context);
  102. if (value == GSS_C_NO_BUFFER) {
  103. *minor_status = EINVAL;
  104. return GSS_S_FAILURE;
  105. }
  106. if (gss_oid_equal(desired_object, GSS_KRB5_COMPAT_DES3_MIC_X)) {
  107. gsskrb5_ctx ctx;
  108. int flag;
  109. if (*context_handle == GSS_C_NO_CONTEXT) {
  110. *minor_status = EINVAL;
  111. return GSS_S_NO_CONTEXT;
  112. }
  113. maj_stat = get_bool(minor_status, value, &flag);
  114. if (maj_stat != GSS_S_COMPLETE)
  115. return maj_stat;
  116. ctx = (gsskrb5_ctx)*context_handle;
  117. HEIMDAL_MUTEX_lock(&ctx->ctx_id_mutex);
  118. if (flag)
  119. ctx->more_flags |= COMPAT_OLD_DES3;
  120. else
  121. ctx->more_flags &= ~COMPAT_OLD_DES3;
  122. ctx->more_flags |= COMPAT_OLD_DES3_SELECTED;
  123. HEIMDAL_MUTEX_unlock(&ctx->ctx_id_mutex);
  124. return GSS_S_COMPLETE;
  125. } else if (gss_oid_equal(desired_object, GSS_KRB5_SET_DNS_CANONICALIZE_X)) {
  126. int flag;
  127. maj_stat = get_bool(minor_status, value, &flag);
  128. if (maj_stat != GSS_S_COMPLETE)
  129. return maj_stat;
  130. krb5_set_dns_canonicalize_hostname(context, flag);
  131. return GSS_S_COMPLETE;
  132. } else if (gss_oid_equal(desired_object, GSS_KRB5_REGISTER_ACCEPTOR_IDENTITY_X)) {
  133. char *str;
  134. maj_stat = get_string(minor_status, value, &str);
  135. if (maj_stat != GSS_S_COMPLETE)
  136. return maj_stat;
  137. maj_stat = _gsskrb5_register_acceptor_identity(minor_status, str);
  138. free(str);
  139. return maj_stat;
  140. } else if (gss_oid_equal(desired_object, GSS_KRB5_SET_DEFAULT_REALM_X)) {
  141. char *str;
  142. maj_stat = get_string(minor_status, value, &str);
  143. if (maj_stat != GSS_S_COMPLETE)
  144. return maj_stat;
  145. if (str == NULL) {
  146. *minor_status = 0;
  147. return GSS_S_CALL_INACCESSIBLE_READ;
  148. }
  149. krb5_set_default_realm(context, str);
  150. free(str);
  151. *minor_status = 0;
  152. return GSS_S_COMPLETE;
  153. } else if (gss_oid_equal(desired_object, GSS_KRB5_SEND_TO_KDC_X)) {
  154. if (value == NULL || value->length == 0) {
  155. krb5_set_send_to_kdc_func(context, NULL, NULL);
  156. } else {
  157. struct gsskrb5_send_to_kdc c;
  158. if (value->length != sizeof(c)) {
  159. *minor_status = EINVAL;
  160. return GSS_S_FAILURE;
  161. }
  162. memcpy(&c, value->value, sizeof(c));
  163. krb5_set_send_to_kdc_func(context,
  164. (krb5_send_to_kdc_func)c.func,
  165. c.ptr);
  166. }
  167. *minor_status = 0;
  168. return GSS_S_COMPLETE;
  169. } else if (gss_oid_equal(desired_object, GSS_KRB5_CCACHE_NAME_X)) {
  170. char *str;
  171. maj_stat = get_string(minor_status, value, &str);
  172. if (maj_stat != GSS_S_COMPLETE)
  173. return maj_stat;
  174. if (str == NULL) {
  175. *minor_status = 0;
  176. return GSS_S_CALL_INACCESSIBLE_READ;
  177. }
  178. *minor_status = krb5_cc_set_default_name(context, str);
  179. free(str);
  180. if (*minor_status)
  181. return GSS_S_FAILURE;
  182. return GSS_S_COMPLETE;
  183. } else if (gss_oid_equal(desired_object, GSS_KRB5_SET_TIME_OFFSET_X)) {
  184. OM_uint32 offset;
  185. time_t t;
  186. maj_stat = get_int32(minor_status, value, &offset);
  187. if (maj_stat != GSS_S_COMPLETE)
  188. return maj_stat;
  189. t = time(NULL) + offset;
  190. krb5_set_real_time(context, t, 0);
  191. *minor_status = 0;
  192. return GSS_S_COMPLETE;
  193. } else if (gss_oid_equal(desired_object, GSS_KRB5_GET_TIME_OFFSET_X)) {
  194. krb5_timestamp sec;
  195. int32_t usec;
  196. time_t t;
  197. t = time(NULL);
  198. krb5_us_timeofday (context, &sec, &usec);
  199. maj_stat = set_int32(minor_status, value, sec - t);
  200. if (maj_stat != GSS_S_COMPLETE)
  201. return maj_stat;
  202. *minor_status = 0;
  203. return GSS_S_COMPLETE;
  204. } else if (gss_oid_equal(desired_object, GSS_KRB5_PLUGIN_REGISTER_X)) {
  205. struct gsskrb5_krb5_plugin c;
  206. if (value->length != sizeof(c)) {
  207. *minor_status = EINVAL;
  208. return GSS_S_FAILURE;
  209. }
  210. memcpy(&c, value->value, sizeof(c));
  211. krb5_plugin_register(context, c.type, c.name, c.symbol);
  212. *minor_status = 0;
  213. return GSS_S_COMPLETE;
  214. }
  215. *minor_status = EINVAL;
  216. return GSS_S_FAILURE;
  217. }