Add libtool-ltdl-devel as a prerequisite for building.
[freeradius.git] / src / modules / rlm_pam / rlm_pam.c
1 /*
2  * pam.c        Functions to access the PAM library. This was taken
3  *              from the hacks that miguel a.l. paraz <map@iphil.net>
4  *              did on radiusd-cistron-1.5.3 and migrated to a
5  *              separate file.
6  *
7  *              That, in fact, was again based on the original stuff
8  *              from Jeph Blaize <jblaize@kiva.net> done in May 1997.
9  *
10  * Version:     $Id$
11  *
12  *   This program is free software; you can redistribute it and/or modify
13  *   it under the terms of the GNU General Public License as published by
14  *   the Free Software Foundation; either version 2 of the License, or
15  *   (at your option) any later version.
16  *
17  *   This program is distributed in the hope that it will be useful,
18  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
19  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  *   GNU General Public License for more details.
21  *
22  *   You should have received a copy of the GNU General Public License
23  *   along with this program; if not, write to the Free Software
24  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
25  *
26  * Copyright 2000  The FreeRADIUS server project
27  * Copyright 1997  Jeph Blaize <jblaize@kiva.net>
28  * Copyright 1999  miguel a.l. paraz <map@iphil.net>
29  */
30
31 #include        "autoconf.h"
32 #include        "libradius.h"
33
34 #include        "config.h"
35
36 #include        <stdio.h>
37 #include        <stdlib.h>
38 #include        <string.h>
39
40 #ifdef HAVE_SECURITY_PAM_APPL_H
41 #include        <security/pam_appl.h>
42 #endif
43
44 #ifdef HAVE_PAM_PAM_APPL_H
45 #include        <pam/pam_appl.h>
46 #endif
47
48
49 #ifdef HAVE_SYSLOG_H
50 #include        <syslog.h>
51 #endif
52
53 #include        "radiusd.h"
54 #include        "modules.h"
55
56 typedef struct rlm_pam_t {
57         const char *pam_auth_name;
58 } rlm_pam_t;
59
60 static CONF_PARSER module_config[] = {
61         { "pam_auth",    PW_TYPE_STRING_PTR, offsetof(rlm_pam_t,pam_auth_name),
62           NULL, "radiusd" },
63         { NULL, -1, 0, NULL, NULL }
64 };
65
66 /*
67  *      (Re-)read radiusd.conf into memory.
68  */
69 static int pam_instantiate(CONF_SECTION *conf, void **instance)
70 {
71         rlm_pam_t *data;
72
73         data = rad_malloc(sizeof(*data));
74         if (!data) {
75                 return -1;
76         }
77         memset(data, 0, sizeof(*data));
78
79         if (cf_section_parse(conf, data, module_config) < 0) {
80                 free(data);
81                 return -1;
82         }
83
84         *instance = data;
85         return 0;
86 }
87
88 /*
89  *      Clean up.
90  */
91 static int pam_detach(void *instance)
92 {
93         rlm_pam_t *data = (rlm_pam_t *) instance;
94
95         free((char *) data->pam_auth_name);
96         free((char *) data);
97         return 0;
98 }
99
100 /*************************************************************************
101  *
102  *      Function: PAM_conv
103  *
104  *      Purpose: Dialogue between RADIUS and PAM modules.
105  *
106  * jab - stolen from pop3d
107  *
108  * Alan DeKok: modified to use PAM's appdata_ptr, so that we're
109  *             multi-threaded safe, and don't have any nasty static
110  *             variables hanging around.
111  *
112  *************************************************************************/
113
114 typedef struct my_PAM {
115   const char *username;
116   const char *password;
117   int         error;
118 } my_PAM;
119
120 static int PAM_conv (int num_msg,
121                      const struct pam_message **msg,
122                      struct pam_response **resp,
123                      void *appdata_ptr) {
124   int count;
125   struct pam_response *reply;
126   my_PAM *pam_config = (my_PAM *) appdata_ptr;
127
128 /* strdup(NULL) doesn't work on some platforms */
129 #define COPY_STRING(s) ((s) ? strdup(s) : NULL)
130
131   reply = rad_malloc(num_msg * sizeof(struct pam_response));
132   memset(reply, 0, num_msg * sizeof(struct pam_response));
133   for (count = 0; count < num_msg; count++) {
134     switch (msg[count]->msg_style) {
135     case PAM_PROMPT_ECHO_ON:
136       reply[count].resp_retcode = PAM_SUCCESS;
137       reply[count].resp = COPY_STRING(pam_config->username);
138       break;
139     case PAM_PROMPT_ECHO_OFF:
140       reply[count].resp_retcode = PAM_SUCCESS;
141       reply[count].resp = COPY_STRING(pam_config->password);
142       break;
143     case PAM_TEXT_INFO:
144       /* ignore it... */
145       break;
146     case PAM_ERROR_MSG:
147     default:
148       /* Must be an error of some sort... */
149       for (count = 0; count < num_msg; count++) {
150         if (reply[count].resp) {
151           /* could be a password, let's be sanitary */
152           memset(reply[count].resp, 0, strlen(reply[count].resp));
153           free(reply[count].resp);
154         }
155       }
156       free(reply);
157       pam_config->error = 1;
158       return PAM_CONV_ERR;
159     }
160   }
161   *resp = reply;
162   /* PAM frees reply (including reply[].resp) */
163
164   return PAM_SUCCESS;
165 }
166
167 /*************************************************************************
168  *
169  *      Function: pam_pass
170  *
171  *      Purpose: Check the users password against the standard UNIX
172  *               password table + PAM.
173  *
174  * jab start 19970529
175  *************************************************************************/
176
177 /* cjd 19980706
178  *
179  * for most flexibility, passing a pamauth type to this function
180  * allows you to have multiple authentication types (i.e. multiple
181  * files associated with radius in /etc/pam.d)
182  */
183 static int pam_pass(const char *name, const char *passwd, const char *pamauth)
184 {
185     pam_handle_t *pamh=NULL;
186     int retval;
187     my_PAM pam_config;
188     struct pam_conv conv;
189
190     /*
191      *  Initialize the structures.
192      */
193     conv.conv = PAM_conv;
194     conv.appdata_ptr = &pam_config;
195     pam_config.username = name;
196     pam_config.password = passwd;
197     pam_config.error = 0;
198
199     DEBUG("pam_pass: using pamauth string <%s> for pam.conf lookup", pamauth);
200     retval = pam_start(pamauth, name, &conv, &pamh);
201     if (retval != PAM_SUCCESS) {
202       DEBUG("pam_pass: function pam_start FAILED for <%s>. Reason: %s",
203             name, pam_strerror(pamh, retval));
204       return -1;
205     }
206
207     retval = pam_authenticate(pamh, 0);
208     if (retval != PAM_SUCCESS) {
209       DEBUG("pam_pass: function pam_authenticate FAILED for <%s>. Reason: %s",
210             name, pam_strerror(pamh, retval));
211       pam_end(pamh, retval);
212       return -1;
213     }
214
215     /*
216      * FreeBSD 3.x doesn't have account and session management
217      * functions in PAM, while 4.0 does.
218      */
219 #if !defined(__FreeBSD_version) || (__FreeBSD_version >= 400000)
220     retval = pam_acct_mgmt(pamh, 0);
221     if (retval != PAM_SUCCESS) {
222       DEBUG("pam_pass: function pam_acct_mgmt FAILED for <%s>. Reason: %s",
223             name, pam_strerror(pamh, retval));
224       pam_end(pamh, retval);
225       return -1;
226     }
227 #endif
228
229     DEBUG("pam_pass: authentication succeeded for <%s>", name);
230     pam_end(pamh, retval);
231     return 0;
232 }
233
234 /* translate between function declarations */
235 static int pam_auth(void *instance, REQUEST *request)
236 {
237         int     r;
238         VALUE_PAIR *pair;
239         rlm_pam_t *data = (rlm_pam_t *) instance;
240
241         const char *pam_auth_string = data->pam_auth_name;
242
243         /*
244          *      We can only authenticate user requests which HAVE
245          *      a User-Name attribute.
246          */
247         if (!request->username) {
248                 radlog(L_AUTH, "rlm_pam: Attribute \"User-Name\" is required for authentication.");
249                 return RLM_MODULE_INVALID;
250         }
251
252         /*
253          *      We can only authenticate user requests which HAVE
254          *      a User-Password attribute.
255          */
256         if (!request->password) {
257                 radlog(L_AUTH, "rlm_pam: Attribute \"User-Password\" is required for authentication.");
258                 return RLM_MODULE_INVALID;
259         }
260
261         /*
262          *  Ensure that we're being passed a plain-text password,
263          *  and not anything else.
264          */
265         if (request->password->attribute != PW_PASSWORD) {
266                 radlog(L_AUTH, "rlm_pam: Attribute \"User-Password\" is required for authentication.  Cannot use \"%s\".", request->password->name);
267                 return RLM_MODULE_INVALID;
268         }
269
270         /*
271          *      Let the 'users' file over-ride the PAM auth name string,
272          *      for backwards compatibility.
273          */
274         pair = pairfind(request->config_items, PAM_AUTH_ATTR);
275         if (pair) pam_auth_string = (char *)pair->strvalue;
276
277         r = pam_pass((char *)request->username->strvalue,
278                      (char *)request->password->strvalue,
279                      pam_auth_string);
280
281 #ifdef HAVE_SYSLOG_H
282         if (!strcmp(radlog_dir, "syslog")) {
283                 openlog(progname, LOG_PID, syslog_facility);
284         }
285 #endif
286
287         if (r == 0) {
288                 return RLM_MODULE_OK;
289         }
290         return RLM_MODULE_REJECT;
291 }
292
293 module_t rlm_pam = {
294   "Pam",
295   RLM_TYPE_THREAD_UNSAFE,       /* The PAM libraries are not thread-safe */
296   NULL,                         /* initialize */
297   pam_instantiate,              /* instantiation */
298   {
299           pam_auth,             /* authenticate */
300           NULL,                 /* authorize */
301           NULL,                 /* pre-accounting */
302           NULL,                 /* accounting */
303           NULL,                 /* checksimul */
304           NULL,                 /* pre-proxy */
305           NULL,                 /* post-proxy */
306           NULL                  /* post-auth */
307   },
308   pam_detach,                   /* detach */
309   NULL,                         /* destroy */
310 };
311