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