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