24ebfe883d42f10d4931ed62a50b988222776b84
[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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25  *
26  * Copyright 2000,2006  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        <freeradius-devel/ident.h>
32 RCSID("$Id$")
33
34 #include        <freeradius-devel/radiusd.h>
35 #include        <freeradius-devel/modules.h>
36
37 #include        "config.h"
38
39 #ifdef HAVE_SECURITY_PAM_APPL_H
40 #include        <security/pam_appl.h>
41 #endif
42
43 #ifdef HAVE_PAM_PAM_APPL_H
44 #include        <pam/pam_appl.h>
45 #endif
46
47
48 #ifdef HAVE_SYSLOG_H
49 #include        <syslog.h>
50 #endif
51
52 typedef struct rlm_pam_t {
53         const char *pam_auth_name;
54 } rlm_pam_t;
55
56 static const CONF_PARSER module_config[] = {
57         { "pam_auth",    PW_TYPE_STRING_PTR, offsetof(rlm_pam_t,pam_auth_name),
58           NULL, "radiusd" },
59         { NULL, -1, 0, NULL, NULL }
60 };
61
62 /*
63  *      (Re-)read radiusd.conf into memory.
64  */
65 static int pam_instantiate(CONF_SECTION *conf, void **instance)
66 {
67         rlm_pam_t *data;
68
69         data = rad_malloc(sizeof(*data));
70         if (!data) {
71                 return -1;
72         }
73         memset(data, 0, sizeof(*data));
74
75         if (cf_section_parse(conf, data, module_config) < 0) {
76                 free(data);
77                 return -1;
78         }
79
80         *instance = data;
81         return 0;
82 }
83
84 /*
85  *      Clean up.
86  */
87 static int pam_detach(void *instance)
88 {
89         rlm_pam_t *data = (rlm_pam_t *) instance;
90
91         free((char *) data);
92         return 0;
93 }
94
95 /*************************************************************************
96  *
97  *      Function: PAM_conv
98  *
99  *      Purpose: Dialogue between RADIUS and PAM modules.
100  *
101  * jab - stolen from pop3d
102  *
103  * Alan DeKok: modified to use PAM's appdata_ptr, so that we're
104  *             multi-threaded safe, and don't have any nasty static
105  *             variables hanging around.
106  *
107  *************************************************************************/
108
109 typedef struct my_PAM {
110   const char *username;
111   const char *password;
112   int         error;
113 } my_PAM;
114
115 static int PAM_conv (int num_msg,
116                      const struct pam_message **msg,
117                      struct pam_response **resp,
118                      void *appdata_ptr) {
119   int count;
120   struct pam_response *reply;
121   my_PAM *pam_config = (my_PAM *) appdata_ptr;
122
123 /* strdup(NULL) doesn't work on some platforms */
124 #define COPY_STRING(s) ((s) ? strdup(s) : NULL)
125
126   reply = rad_malloc(num_msg * sizeof(struct pam_response));
127   memset(reply, 0, num_msg * sizeof(struct pam_response));
128   for (count = 0; count < num_msg; count++) {
129     switch (msg[count]->msg_style) {
130     case PAM_PROMPT_ECHO_ON:
131       reply[count].resp_retcode = PAM_SUCCESS;
132       reply[count].resp = COPY_STRING(pam_config->username);
133       break;
134     case PAM_PROMPT_ECHO_OFF:
135       reply[count].resp_retcode = PAM_SUCCESS;
136       reply[count].resp = COPY_STRING(pam_config->password);
137       break;
138     case PAM_TEXT_INFO:
139       /* ignore it... */
140       break;
141     case PAM_ERROR_MSG:
142     default:
143       /* Must be an error of some sort... */
144       for (count = 0; count < num_msg; count++) {
145         if (reply[count].resp) {
146           /* could be a password, let's be sanitary */
147           memset(reply[count].resp, 0, strlen(reply[count].resp));
148           free(reply[count].resp);
149         }
150       }
151       free(reply);
152       pam_config->error = 1;
153       return PAM_CONV_ERR;
154     }
155   }
156   *resp = reply;
157   /* PAM frees reply (including reply[].resp) */
158
159   return PAM_SUCCESS;
160 }
161
162 /*************************************************************************
163  *
164  *      Function: pam_pass
165  *
166  *      Purpose: Check the users password against the standard UNIX
167  *               password table + PAM.
168  *
169  * jab start 19970529
170  *************************************************************************/
171
172 /* cjd 19980706
173  *
174  * for most flexibility, passing a pamauth type to this function
175  * allows you to have multiple authentication types (i.e. multiple
176  * files associated with radius in /etc/pam.d)
177  */
178 static int pam_pass(const char *name, const char *passwd, const char *pamauth)
179 {
180     pam_handle_t *pamh=NULL;
181     int retval;
182     my_PAM pam_config;
183     struct pam_conv conv;
184
185     /*
186      *  Initialize the structures.
187      */
188     conv.conv = PAM_conv;
189     conv.appdata_ptr = &pam_config;
190     pam_config.username = name;
191     pam_config.password = passwd;
192     pam_config.error = 0;
193
194     DEBUG("pam_pass: using pamauth string <%s> for pam.conf lookup", pamauth);
195     retval = pam_start(pamauth, name, &conv, &pamh);
196     if (retval != PAM_SUCCESS) {
197       DEBUG("pam_pass: function pam_start FAILED for <%s>. Reason: %s",
198             name, pam_strerror(pamh, retval));
199       return -1;
200     }
201
202     retval = pam_authenticate(pamh, 0);
203     if (retval != PAM_SUCCESS) {
204       DEBUG("pam_pass: function pam_authenticate FAILED for <%s>. Reason: %s",
205             name, pam_strerror(pamh, retval));
206       pam_end(pamh, retval);
207       return -1;
208     }
209
210     /*
211      * FreeBSD 3.x doesn't have account and session management
212      * functions in PAM, while 4.0 does.
213      */
214 #if !defined(__FreeBSD_version) || (__FreeBSD_version >= 400000)
215     retval = pam_acct_mgmt(pamh, 0);
216     if (retval != PAM_SUCCESS) {
217       DEBUG("pam_pass: function pam_acct_mgmt FAILED for <%s>. Reason: %s",
218             name, pam_strerror(pamh, retval));
219       pam_end(pamh, retval);
220       return -1;
221     }
222 #endif
223
224     DEBUG("pam_pass: authentication succeeded for <%s>", name);
225     pam_end(pamh, retval);
226     return 0;
227 }
228
229 /* translate between function declarations */
230 static int pam_auth(void *instance, REQUEST *request)
231 {
232         int     r;
233         VALUE_PAIR *pair;
234         rlm_pam_t *data = (rlm_pam_t *) instance;
235
236         const char *pam_auth_string = data->pam_auth_name;
237
238         /*
239          *      We can only authenticate user requests which HAVE
240          *      a User-Name attribute.
241          */
242         if (!request->username) {
243                 radlog(L_AUTH, "rlm_pam: Attribute \"User-Name\" is required for authentication.");
244                 return RLM_MODULE_INVALID;
245         }
246
247         /*
248          *      We can only authenticate user requests which HAVE
249          *      a User-Password attribute.
250          */
251         if (!request->password) {
252                 radlog(L_AUTH, "rlm_pam: Attribute \"User-Password\" is required for authentication.");
253                 return RLM_MODULE_INVALID;
254         }
255
256         /*
257          *  Ensure that we're being passed a plain-text password,
258          *  and not anything else.
259          */
260         if (request->password->attribute != PW_USER_PASSWORD) {
261                 radlog(L_AUTH, "rlm_pam: Attribute \"User-Password\" is required for authentication.  Cannot use \"%s\".", request->password->name);
262                 return RLM_MODULE_INVALID;
263         }
264
265         /*
266          *      Let the 'users' file over-ride the PAM auth name string,
267          *      for backwards compatibility.
268          */
269         pair = pairfind(request->config_items, PAM_AUTH_ATTR, 0);
270         if (pair) pam_auth_string = (char *)pair->vp_strvalue;
271
272         r = pam_pass((char *)request->username->vp_strvalue,
273                      (char *)request->password->vp_strvalue,
274                      pam_auth_string);
275
276         if (r == 0) {
277                 return RLM_MODULE_OK;
278         }
279         return RLM_MODULE_REJECT;
280 }
281
282 module_t rlm_pam = {
283         RLM_MODULE_INIT,
284         "pam",
285         RLM_TYPE_THREAD_UNSAFE, /* The PAM libraries are not thread-safe */
286         pam_instantiate,                /* instantiation */
287         pam_detach,                     /* detach */
288         {
289                 pam_auth,               /* authenticate */
290                 NULL,                   /* authorize */
291                 NULL,                   /* pre-accounting */
292                 NULL,                   /* accounting */
293                 NULL,                   /* checksimul */
294                 NULL,                   /* pre-proxy */
295                 NULL,                   /* post-proxy */
296                 NULL                    /* post-auth */
297         },
298 };
299