2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2014 Alexander Barton (alex@barton.de) and Contributors.
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.
18 * PAM User Authentication
24 #ifdef HAVE_SECURITY_PAM_APPL_H
25 # include <security/pam_appl.h>
27 #ifdef HAVE_PAM_PAM_APPL_H
28 # include <pam/pam_appl.h>
38 static char *password;
41 * PAM "conversation function".
42 * This is a callback function used by the PAM library to get the password.
43 * Please see the PAM documentation for details :-)
46 password_conversation(int num_msg, const struct pam_message **msg,
47 struct pam_response **resp, void *appdata_ptr) {
48 LogDebug("PAM: conv(%d, %d, '%s', '%s')",
49 num_msg, msg[0]->msg_style, msg[0]->msg, appdata_ptr);
51 /* Can we deal with this request? */
52 if (num_msg != 1 || msg[0]->msg_style != PAM_PROMPT_ECHO_OFF) {
53 Log(LOG_ERR, "PAM: Unexpected PAM conversation '%d:%s'!",
54 msg[0]->msg_style, msg[0]->msg);
59 /* Sometimes appdata_ptr gets lost!? */
60 appdata_ptr = password;
63 /* Duplicate password ("application data") for the PAM library */
64 *resp = calloc(num_msg, sizeof(struct pam_response));
66 Log(LOG_ERR, "PAM: Out of memory!");
70 (*resp)[0].resp = strdup((char *)appdata_ptr);
71 (*resp)[0].resp_retcode = 0;
73 return ((*resp)[0].resp ? PAM_SUCCESS : PAM_CONV_ERR);
77 * PAM "conversation" structure.
79 static struct pam_conv conv = {
80 &password_conversation,
85 * Authenticate a connecting client using PAM.
86 * @param Client The client to authenticate.
87 * @return true when authentication succeeded, false otherwise.
90 PAM_Authenticate(CLIENT *Client) {
92 int retval = PAM_SUCCESS;
94 LogDebug("PAM: Authenticate \"%s\" (%s) ...",
95 Client_OrigUser(Client), Client_Mask(Client));
97 /* Set supplied client password */
100 password = strdup(Conn_Password(Client_Conn(Client)));
101 conv.appdata_ptr = Conn_Password(Client_Conn(Client));
104 retval = pam_start("ngircd", Client_OrigUser(Client), &conv, &pam);
105 if (retval != PAM_SUCCESS) {
106 Log(LOG_ERR, "PAM: Failed to create authenticator! (%d)", retval);
110 pam_set_item(pam, PAM_RUSER, Client_User(Client));
111 pam_set_item(pam, PAM_RHOST, Client_Hostname(Client));
112 #if defined(HAVE_PAM_FAIL_DELAY) && !defined(NO_PAM_FAIL_DELAY)
113 pam_fail_delay(pam, 0);
116 /* PAM authentication ... */
117 retval = pam_authenticate(pam, 0);
120 if (retval == PAM_SUCCESS)
121 Log(LOG_INFO, "PAM: Authenticated \"%s\" (%s).",
122 Client_OrigUser(Client), Client_Mask(Client));
124 Log(LOG_ERR, "PAM: Error on \"%s\" (%s): %s",
125 Client_OrigUser(Client), Client_Mask(Client),
126 pam_strerror(pam, retval));
128 /* Free PAM structures */
129 if (pam_end(pam, retval) != PAM_SUCCESS)
130 Log(LOG_ERR, "PAM: Failed to release authenticator!");
132 return (retval == PAM_SUCCESS);