You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

pam.c 2.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. #include <stdlib.h>
  24. #include <string.h>
  25. #include <security/pam_appl.h>
  26. #include <rfb/pam.h>
  27. typedef struct
  28. {
  29. const char *username;
  30. const char *password;
  31. } AuthData;
  32. #if defined(__sun)
  33. static int pam_callback(int count, struct pam_message **in,
  34. struct pam_response **out, void *ptr)
  35. #else
  36. static int pam_callback(int count, const struct pam_message **in,
  37. struct pam_response **out, void *ptr)
  38. #endif
  39. {
  40. int i;
  41. AuthData *auth = (AuthData *) ptr;
  42. struct pam_response *resp =
  43. (struct pam_response *) malloc (sizeof (struct pam_response) * count);
  44. if (!resp && count)
  45. return PAM_CONV_ERR;
  46. for (i = 0; i < count; i++) {
  47. resp[i].resp_retcode = PAM_SUCCESS;
  48. switch (in[i]->msg_style) {
  49. case PAM_TEXT_INFO:
  50. case PAM_ERROR_MSG:
  51. resp[i].resp = 0;
  52. break;
  53. case PAM_PROMPT_ECHO_ON: /* Send Username */
  54. resp[i].resp = strdup(auth->username);
  55. break;
  56. case PAM_PROMPT_ECHO_OFF: /* Send Password */
  57. resp[i].resp = strdup(auth->password);
  58. break;
  59. default:
  60. free(resp);
  61. return PAM_CONV_ERR;
  62. }
  63. }
  64. *out = resp;
  65. return PAM_SUCCESS;
  66. }
  67. int do_pam_auth(const char *service, const char *username, const char *password)
  68. {
  69. int ret;
  70. AuthData auth = { username, password };
  71. struct pam_conv conv = {
  72. pam_callback,
  73. &auth
  74. };
  75. pam_handle_t *h = 0;
  76. ret = pam_start(service, username, &conv, &h);
  77. if (ret == PAM_SUCCESS)
  78. ret = pam_authenticate(h, 0);
  79. if (ret == PAM_SUCCESS)
  80. ret = pam_acct_mgmt(h, 0);
  81. pam_end(h, ret);
  82. return ret == PAM_SUCCESS ? 1 : 0;
  83. }