pam.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 "pam.h"
  31. static char *password;
  32. /**
  33. * PAM "conversation function".
  34. * This is a callback function used by the PAM library to get the password.
  35. * Please see the PAM documentation for details :-)
  36. */
  37. static int
  38. password_conversation(int num_msg, const struct pam_message **msg,
  39. struct pam_response **resp, void *appdata_ptr) {
  40. LogDebug("PAM: conv(%d, %d, '%s', '%s')",
  41. num_msg, msg[0]->msg_style, msg[0]->msg, appdata_ptr);
  42. /* Can we deal with this request? */
  43. if (num_msg != 1 || msg[0]->msg_style != PAM_PROMPT_ECHO_OFF) {
  44. Log(LOG_ERR, "PAM: Unexpected PAM conversation '%d:%s'!",
  45. msg[0]->msg_style, msg[0]->msg);
  46. return PAM_CONV_ERR;
  47. }
  48. if (!appdata_ptr) {
  49. /* Sometimes appdata_ptr gets lost!? */
  50. appdata_ptr = password;
  51. }
  52. /* Duplicate password ("application data") for the PAM library */
  53. *resp = calloc(num_msg, sizeof(struct pam_response));
  54. if (!*resp) {
  55. Log(LOG_ERR, "PAM: Out of memory!");
  56. return PAM_CONV_ERR;
  57. }
  58. (*resp)[0].resp = strdup((char *)appdata_ptr);
  59. (*resp)[0].resp_retcode = 0;
  60. return ((*resp)[0].resp ? PAM_SUCCESS : PAM_CONV_ERR);
  61. }
  62. /**
  63. * PAM "conversation" structure.
  64. */
  65. static struct pam_conv conv = {
  66. &password_conversation,
  67. NULL
  68. };
  69. /**
  70. * Authenticate a connecting client using PAM.
  71. * @param Client The client to authenticate.
  72. * @return true when authentication succeeded, false otherwise.
  73. */
  74. GLOBAL bool
  75. PAM_Authenticate(CLIENT *Client) {
  76. pam_handle_t *pam;
  77. int retval = PAM_SUCCESS;
  78. LogDebug("PAM: Authenticate \"%s\" (%s) ...",
  79. Client_OrigUser(Client), Client_Mask(Client));
  80. /* Set supplied client password */
  81. if (password)
  82. free(password);
  83. password = strdup(Conn_Password(Client_Conn(Client)));
  84. conv.appdata_ptr = Conn_Password(Client_Conn(Client));
  85. /* Initialize PAM */
  86. retval = pam_start("ngircd", Client_OrigUser(Client), &conv, &pam);
  87. if (retval != PAM_SUCCESS) {
  88. Log(LOG_ERR, "PAM: Failed to create authenticator! (%d)", retval);
  89. return false;
  90. }
  91. pam_set_item(pam, PAM_RUSER, Client_User(Client));
  92. pam_set_item(pam, PAM_RHOST, Client_Hostname(Client));
  93. #if defined(HAVE_PAM_FAIL_DELAY) && !defined(NO_PAM_FAIL_DELAY)
  94. pam_fail_delay(pam, 0);
  95. #endif
  96. /* PAM authentication ... */
  97. retval = pam_authenticate(pam, 0);
  98. /* Success? */
  99. if (retval == PAM_SUCCESS)
  100. Log(LOG_INFO, "PAM: Authenticated \"%s\" (%s).",
  101. Client_OrigUser(Client), Client_Mask(Client));
  102. else
  103. Log(LOG_ERR, "PAM: Error on \"%s\" (%s): %s",
  104. Client_OrigUser(Client), Client_Mask(Client),
  105. pam_strerror(pam, retval));
  106. /* Free PAM structures */
  107. if (pam_end(pam, retval) != PAM_SUCCESS)
  108. Log(LOG_ERR, "PAM: Failed to release authenticator!");
  109. return (retval == PAM_SUCCESS);
  110. }
  111. #endif /* PAM */
  112. /* -eof- */