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
7 * That, in fact, was again based on the original stuff
8 * from Jeph Blaize <jblaize@kiva.net> done in May 1997.
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.
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.
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
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>
31 #include <freeradius-devel/autoconf.h>
39 #ifdef HAVE_SECURITY_PAM_APPL_H
40 #include <security/pam_appl.h>
43 #ifdef HAVE_PAM_PAM_APPL_H
44 #include <pam/pam_appl.h>
52 #include <freeradius-devel/radiusd.h>
53 #include <freeradius-devel/modules.h>
55 typedef struct rlm_pam_t {
56 const char *pam_auth_name;
59 static const CONF_PARSER module_config[] = {
60 { "pam_auth", PW_TYPE_STRING_PTR, offsetof(rlm_pam_t,pam_auth_name),
62 { NULL, -1, 0, NULL, NULL }
66 * (Re-)read radiusd.conf into memory.
68 static int pam_instantiate(CONF_SECTION *conf, void **instance)
72 data = rad_malloc(sizeof(*data));
76 memset(data, 0, sizeof(*data));
78 if (cf_section_parse(conf, data, module_config) < 0) {
90 static int pam_detach(void *instance)
92 rlm_pam_t *data = (rlm_pam_t *) instance;
94 free((char *) data->pam_auth_name);
99 /*************************************************************************
103 * Purpose: Dialogue between RADIUS and PAM modules.
105 * jab - stolen from pop3d
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.
111 *************************************************************************/
113 typedef struct my_PAM {
114 const char *username;
115 const char *password;
119 static int PAM_conv (int num_msg,
120 const struct pam_message **msg,
121 struct pam_response **resp,
124 struct pam_response *reply;
125 my_PAM *pam_config = (my_PAM *) appdata_ptr;
127 /* strdup(NULL) doesn't work on some platforms */
128 #define COPY_STRING(s) ((s) ? strdup(s) : NULL)
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);
138 case PAM_PROMPT_ECHO_OFF:
139 reply[count].resp_retcode = PAM_SUCCESS;
140 reply[count].resp = COPY_STRING(pam_config->password);
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);
156 pam_config->error = 1;
161 /* PAM frees reply (including reply[].resp) */
166 /*************************************************************************
170 * Purpose: Check the users password against the standard UNIX
171 * password table + PAM.
174 *************************************************************************/
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)
182 static int pam_pass(const char *name, const char *passwd, const char *pamauth)
184 pam_handle_t *pamh=NULL;
187 struct pam_conv conv;
190 * Initialize the structures.
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;
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));
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);
215 * FreeBSD 3.x doesn't have account and session management
216 * functions in PAM, while 4.0 does.
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);
228 DEBUG("pam_pass: authentication succeeded for <%s>", name);
229 pam_end(pamh, retval);
233 /* translate between function declarations */
234 static int pam_auth(void *instance, REQUEST *request)
238 rlm_pam_t *data = (rlm_pam_t *) instance;
240 const char *pam_auth_string = data->pam_auth_name;
243 * We can only authenticate user requests which HAVE
244 * a User-Name attribute.
246 if (!request->username) {
247 radlog(L_AUTH, "rlm_pam: Attribute \"User-Name\" is required for authentication.");
248 return RLM_MODULE_INVALID;
252 * We can only authenticate user requests which HAVE
253 * a User-Password attribute.
255 if (!request->password) {
256 radlog(L_AUTH, "rlm_pam: Attribute \"User-Password\" is required for authentication.");
257 return RLM_MODULE_INVALID;
261 * Ensure that we're being passed a plain-text password,
262 * and not anything else.
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;
270 * Let the 'users' file over-ride the PAM auth name string,
271 * for backwards compatibility.
273 pair = pairfind(request->config_items, PAM_AUTH_ATTR);
274 if (pair) pam_auth_string = (char *)pair->vp_strvalue;
276 r = pam_pass((char *)request->username->vp_strvalue,
277 (char *)request->password->vp_strvalue,
281 if (!strcmp(radlog_dir, "syslog")) {
282 openlog(progname, LOG_PID, mainconfig.syslog_facility);
287 return RLM_MODULE_OK;
289 return RLM_MODULE_REJECT;
295 RLM_TYPE_THREAD_UNSAFE, /* The PAM libraries are not thread-safe */
296 pam_instantiate, /* instantiation */
297 pam_detach, /* detach */
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 */