Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

pam.c 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * Copyright (C) 2006 Martin Koegler
  3. * Copyright (C) 2010 TigerVNC Team
  4. *
  5. * This is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This software is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this software; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  18. * USA.
  19. */
  20. #ifdef HAVE_CONFIG_H
  21. #include <config.h>
  22. #endif
  23. #ifndef HAVE_PAM
  24. #error "This source should not be compiled when PAM is unsupported"
  25. #endif
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include <security/pam_appl.h>
  29. #include <rfb/pam.h>
  30. typedef struct
  31. {
  32. const char *username;
  33. const char *password;
  34. } AuthData;
  35. #if defined(__sun)
  36. static int pam_callback(int count, struct pam_message **in,
  37. struct pam_response **out, void *ptr)
  38. #else
  39. static int pam_callback(int count, const struct pam_message **in,
  40. struct pam_response **out, void *ptr)
  41. #endif
  42. {
  43. int i;
  44. AuthData *auth = (AuthData *) ptr;
  45. struct pam_response *resp =
  46. (struct pam_response *) malloc (sizeof (struct pam_response) * count);
  47. if (!resp && count)
  48. return PAM_CONV_ERR;
  49. for (i = 0; i < count; i++) {
  50. resp[i].resp_retcode = PAM_SUCCESS;
  51. switch (in[i]->msg_style) {
  52. case PAM_TEXT_INFO:
  53. case PAM_ERROR_MSG:
  54. resp[i].resp = 0;
  55. break;
  56. case PAM_PROMPT_ECHO_ON: /* Send Username */
  57. resp[i].resp = strdup(auth->username);
  58. break;
  59. case PAM_PROMPT_ECHO_OFF: /* Send Password */
  60. resp[i].resp = strdup(auth->password);
  61. break;
  62. default:
  63. free(resp);
  64. return PAM_CONV_ERR;
  65. }
  66. }
  67. *out = resp;
  68. return PAM_SUCCESS;
  69. }
  70. int do_pam_auth(const char *service, const char *username, const char *password)
  71. {
  72. int ret;
  73. AuthData auth = { username, password };
  74. struct pam_conv conv = {
  75. pam_callback,
  76. &auth
  77. };
  78. pam_handle_t *h = 0;
  79. ret = pam_start(service, username, &conv, &h);
  80. if (ret == PAM_SUCCESS)
  81. ret = pam_authenticate(h, 0);
  82. if (ret == PAM_SUCCESS)
  83. ret = pam_acct_mgmt(h, 0);
  84. pam_end(h, ret);
  85. return ret == PAM_SUCCESS ? 1 : 0;
  86. }