kerberosbasic.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /**
  2. * Copyright (c) 2006-2008 Apple Inc. All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. **/
  16. #include <Python.h>
  17. #include "kerberosbasic.h"
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #undef PRINTFS
  22. extern PyObject *BasicAuthException_class;
  23. static void set_basicauth_error(krb5_context context, krb5_error_code code);
  24. static krb5_error_code verify_krb5_user(krb5_context context, krb5_principal principal, const char *password, krb5_principal server);
  25. int authenticate_user_krb5pwd(const char *user, const char *pswd, const char *service, const char *default_realm)
  26. {
  27. krb5_context kcontext = NULL;
  28. krb5_error_code code;
  29. krb5_principal client = NULL;
  30. krb5_principal server = NULL;
  31. int ret = 0;
  32. char *name = NULL;
  33. char *p = NULL;
  34. code = krb5_init_context(&kcontext);
  35. if (code)
  36. {
  37. PyErr_SetObject(BasicAuthException_class, Py_BuildValue("((s:i))",
  38. "Cannot initialize Kerberos5 context", code));
  39. return 0;
  40. }
  41. ret = krb5_parse_name (kcontext, service, &server);
  42. if (ret)
  43. {
  44. set_basicauth_error(kcontext, ret);
  45. ret = 0;
  46. goto end;
  47. }
  48. code = krb5_unparse_name(kcontext, server, &name);
  49. if (code)
  50. {
  51. set_basicauth_error(kcontext, code);
  52. ret = 0;
  53. goto end;
  54. }
  55. #ifdef PRINTFS
  56. printf("Using %s as server principal for password verification\n", name);
  57. #endif
  58. free(name);
  59. name = NULL;
  60. name = (char *)malloc(256);
  61. p = strchr(user, '@');
  62. if (p == NULL)
  63. {
  64. snprintf(name, 256, "%s@%s", user, default_realm);
  65. }
  66. else
  67. {
  68. snprintf(name, 256, "%s", user);
  69. }
  70. code = krb5_parse_name(kcontext, name, &client);
  71. if (code)
  72. {
  73. set_basicauth_error(kcontext, code);
  74. ret = 0;
  75. goto end;
  76. }
  77. code = verify_krb5_user(kcontext, client, pswd, server);
  78. if (code)
  79. {
  80. ret = 0;
  81. goto end;
  82. }
  83. ret = 1;
  84. end:
  85. #ifdef PRINTFS
  86. printf("kerb_authenticate_user_krb5pwd ret=%d user=%s authtype=%s\n", ret, user, "Basic");
  87. #endif
  88. if (name)
  89. free(name);
  90. if (client)
  91. krb5_free_principal(kcontext, client);
  92. if (server)
  93. krb5_free_principal(kcontext, server);
  94. krb5_free_context(kcontext);
  95. return ret;
  96. }
  97. /* Inspired by krb5_verify_user from Heimdal */
  98. static krb5_error_code verify_krb5_user(krb5_context context, krb5_principal principal, const char *password, krb5_principal server)
  99. {
  100. krb5_creds creds;
  101. krb5_get_init_creds_opt gic_options;
  102. krb5_error_code ret;
  103. char *name = NULL;
  104. memset(&creds, 0, sizeof(creds));
  105. ret = krb5_unparse_name(context, principal, &name);
  106. if (ret == 0)
  107. {
  108. #ifdef PRINTFS
  109. printf("Trying to get TGT for user %s\n", name);
  110. #endif
  111. free(name);
  112. }
  113. krb5_get_init_creds_opt_init(&gic_options);
  114. ret = krb5_get_init_creds_password(context, &creds, principal, (char *)password, NULL, NULL, 0, NULL, &gic_options);
  115. if (ret)
  116. {
  117. set_basicauth_error(context, ret);
  118. goto end;
  119. }
  120. end:
  121. krb5_free_cred_contents(context, &creds);
  122. return ret;
  123. }
  124. static void set_basicauth_error(krb5_context context, krb5_error_code code)
  125. {
  126. PyErr_SetObject(BasicAuthException_class, Py_BuildValue("(s:i)", krb5_get_err_text(context, code), code));
  127. }