ECPamAuth.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 "ECPamAuth.h"
  18. #ifndef HAVE_PAM
  19. namespace KC {
  20. ECRESULT ECPAMAuthenticateUser(const char* szPamService, const std::string &strUsername, const std::string &strPassword, std::string *lpstrError)
  21. {
  22. *lpstrError = "Server is not compiled with pam support.";
  23. return KCERR_NO_SUPPORT;
  24. }
  25. }
  26. #else
  27. #include <security/pam_appl.h>
  28. namespace KC {
  29. static int converse(int num_msg, const struct pam_message **msg,
  30. struct pam_response **resp, void *appdata_ptr)
  31. {
  32. int i = 0;
  33. struct pam_response *response = NULL;
  34. auto password = static_cast<const char *>(appdata_ptr);
  35. if (!resp || !msg || !password)
  36. return PAM_CONV_ERR;
  37. response = (struct pam_response *) malloc(num_msg * sizeof(struct pam_response));
  38. if (!response)
  39. return PAM_BUF_ERR;
  40. for (i = 0; i < num_msg; ++i) {
  41. response[i].resp_retcode = 0;
  42. response[i].resp = 0;
  43. if (msg[i]->msg_style == PAM_PROMPT_ECHO_OFF) {
  44. response[i].resp = strdup(password);
  45. } else {
  46. free(response);
  47. return PAM_CONV_ERR;
  48. }
  49. }
  50. *resp = response;
  51. return PAM_SUCCESS;
  52. }
  53. ECRESULT ECPAMAuthenticateUser(const char* szPamService, const std::string &strUsername, const std::string &strPassword, std::string *lpstrError)
  54. {
  55. int res = 0;
  56. pam_handle_t *pamh = NULL;
  57. struct pam_conv conv_info = { &converse, (void*)strPassword.c_str() };
  58. res = pam_start(szPamService, strUsername.c_str(), &conv_info, &pamh);
  59. if (res != PAM_SUCCESS)
  60. {
  61. *lpstrError = pam_strerror(NULL, res);
  62. return KCERR_LOGON_FAILED;
  63. }
  64. res = pam_authenticate(pamh, PAM_SILENT);
  65. pam_end(pamh, res);
  66. if (res != PAM_SUCCESS) {
  67. *lpstrError = pam_strerror(NULL, res);
  68. return KCERR_LOGON_FAILED;
  69. }
  70. return erSuccess;
  71. }
  72. } /* namespace */
  73. #endif