pam.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * ngIRCd -- The Next Generation IRC Daemon
  3. * Copyright (c)2001-2014 Alexander Barton (alex@barton.de) and Contributors.
  4. *
  5. * This program 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. * Please read the file COPYING, README and AUTHORS for more information.
  10. */
  11. #include "portab.h"
  12. #ifdef PAM
  13. /**
  14. * @file
  15. * PAM User Authentication
  16. */
  17. #include <assert.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #ifdef HAVE_SECURITY_PAM_APPL_H
  21. # include <security/pam_appl.h>
  22. #endif
  23. #ifdef HAVE_PAM_PAM_APPL_H
  24. # include <pam/pam_appl.h>
  25. #endif
  26. #include "defines.h"
  27. #include "log.h"
  28. #include "conn.h"
  29. #include "client.h"
  30. #include "conf.h"
  31. #include "pam.h"
  32. static char *password;
  33. /**
  34. * PAM "conversation function".
  35. * This is a callback function used by the PAM library to get the password.
  36. * Please see the PAM documentation for details :-)
  37. */
  38. static int
  39. password_conversation(int num_msg, const struct pam_message **msg,
  40. struct pam_response **resp, void *appdata_ptr) {
  41. LogDebug("PAM: conv(%d, %d, '%s', '%s')",
  42. num_msg, msg[0]->msg_style, msg[0]->msg, appdata_ptr);
  43. /* Can we deal with this request? */
  44. if (num_msg != 1 || msg[0]->msg_style != PAM_PROMPT_ECHO_OFF) {
  45. Log(LOG_ERR, "PAM: Unexpected PAM conversation '%d:%s'!",
  46. msg[0]->msg_style, msg[0]->msg);
  47. return PAM_CONV_ERR;
  48. }
  49. if (!appdata_ptr) {
  50. /* Sometimes appdata_ptr gets lost!? */
  51. appdata_ptr = password;
  52. }
  53. /* Duplicate password ("application data") for the PAM library */
  54. *resp = calloc(num_msg, sizeof(struct pam_response));
  55. if (!*resp) {
  56. Log(LOG_ERR, "PAM: Out of memory!");
  57. return PAM_CONV_ERR;
  58. }
  59. (*resp)[0].resp = strdup((char *)appdata_ptr);
  60. (*resp)[0].resp_retcode = 0;
  61. return ((*resp)[0].resp ? PAM_SUCCESS : PAM_CONV_ERR);
  62. }
  63. /**
  64. * PAM "conversation" structure.
  65. */
  66. static struct pam_conv conv = {
  67. &password_conversation,
  68. NULL
  69. };
  70. /**
  71. * Authenticate a connecting client using PAM.
  72. * @param Client The client to authenticate.
  73. * @return true when authentication succeeded, false otherwise.
  74. */
  75. GLOBAL bool
  76. PAM_Authenticate(CLIENT *Client) {
  77. pam_handle_t *pam;
  78. int retval = PAM_SUCCESS;
  79. LogDebug("PAM: Authenticate \"%s\" (%s) ...",
  80. Client_OrigUser(Client), Client_Mask(Client));
  81. /* Set supplied client password */
  82. if (password)
  83. free(password);
  84. password = strdup(Conn_Password(Client_Conn(Client)));
  85. conv.appdata_ptr = Conn_Password(Client_Conn(Client));
  86. /* Initialize PAM */
  87. retval = pam_start(Conf_PAMServiceName, Client_OrigUser(Client), &conv, &pam);
  88. if (retval != PAM_SUCCESS) {
  89. Log(LOG_ERR, "PAM: Failed to create authenticator! (%d)", retval);
  90. return false;
  91. }
  92. pam_set_item(pam, PAM_RUSER, Client_User(Client));
  93. pam_set_item(pam, PAM_RHOST, Client_Hostname(Client));
  94. #if defined(HAVE_PAM_FAIL_DELAY) && !defined(NO_PAM_FAIL_DELAY)
  95. pam_fail_delay(pam, 0);
  96. #endif
  97. /* PAM authentication ... */
  98. retval = pam_authenticate(pam, 0);
  99. /* Success? */
  100. if (retval == PAM_SUCCESS)
  101. Log(LOG_INFO, "PAM: Authenticated \"%s\" (%s).",
  102. Client_OrigUser(Client), Client_Mask(Client));
  103. else
  104. Log(LOG_ERR, "PAM: Error on \"%s\" (%s): %s",
  105. Client_OrigUser(Client), Client_Mask(Client),
  106. pam_strerror(pam, retval));
  107. /* Free PAM structures */
  108. if (pam_end(pam, retval) != PAM_SUCCESS)
  109. Log(LOG_ERR, "PAM: Failed to release authenticator!");
  110. return (retval == PAM_SUCCESS);
  111. }
  112. #endif /* PAM */
  113. /* -eof- */