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