ECKrbAuth.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * Copyright 2005 - 2016 Zarafa and its licensors
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU Affero General Public License, version 3,
  6. * as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU Affero General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Affero General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. *
  16. */
  17. #include "ECKrbAuth.h"
  18. #ifndef HAVE_KRB5
  19. namespace KC {
  20. ECRESULT ECKrb5AuthenticateUser(const std::string &strUsername, const std::string &strPassword, std::string *lpstrError)
  21. {
  22. *lpstrError = "Server is not compiled with kerberos support.";
  23. return KCERR_NO_SUPPORT;
  24. }
  25. }
  26. #else
  27. // error_message() is wrongly typed in c++ context
  28. extern "C" {
  29. #include <krb5.h>
  30. #include <et/com_err.h>
  31. }
  32. namespace KC {
  33. ECRESULT ECKrb5AuthenticateUser(const std::string &strUsername, const std::string &strPassword, std::string *lpstrError)
  34. {
  35. ECRESULT er = erSuccess;
  36. krb5_error_code code = 0;
  37. krb5_get_init_creds_opt options;
  38. krb5_creds my_creds;
  39. krb5_context ctx;
  40. krb5_principal me;
  41. char *name = NULL;
  42. memset(&ctx, 0, sizeof(ctx));
  43. memset(&me, 0, sizeof(me));
  44. code = krb5_init_context(&ctx);
  45. if (code) {
  46. *lpstrError = std::string("Unable to initialize kerberos 5 library: code ") + error_message(code);
  47. er = KCERR_INVALID_PARAMETER;
  48. goto exit;
  49. }
  50. code = krb5_parse_name(ctx, strUsername.c_str(), &me);
  51. if (code) {
  52. *lpstrError = std::string("Error parsing kerberos 5 username: code ") + error_message(code);
  53. er = KCERR_INVALID_PARAMETER;
  54. goto exit;
  55. }
  56. code = krb5_unparse_name(ctx, me, &name);
  57. if (code) {
  58. *lpstrError = std::string("Error unparsing kerberos 5 username: code ") + error_message(code);
  59. er = KCERR_INVALID_PARAMETER;
  60. goto exit;
  61. }
  62. krb5_get_init_creds_opt_init(&options);
  63. memset(&my_creds, 0, sizeof(my_creds));
  64. code = krb5_get_init_creds_password(ctx, &my_creds, me, (char*)strPassword.c_str(), 0, 0, 0, NULL, &options);
  65. if (code) {
  66. *lpstrError = error_message(code);
  67. er = KCERR_LOGON_FAILED;
  68. goto exit;
  69. }
  70. exit:
  71. if (name)
  72. krb5_free_unparsed_name(ctx, name);
  73. if (me)
  74. krb5_free_principal(ctx, me);
  75. if (ctx)
  76. krb5_free_context(ctx);
  77. memset(&ctx, 0, sizeof(ctx));
  78. memset(&me, 0, sizeof(me));
  79. return er;
  80. }
  81. } /* namespace */
  82. #endif