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