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