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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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>
32 #include "libradius.h"
40 #ifdef HAVE_SECURITY_PAM_APPL_H
41 #include <security/pam_appl.h>
44 #ifdef HAVE_PAM_PAM_APPL_H
45 #include <pam/pam_appl.h>
56 typedef struct rlm_pam_t {
57 const char *pam_auth_name;
60 static CONF_PARSER module_config[] = {
61 { "pam_auth", PW_TYPE_STRING_PTR, offsetof(rlm_pam_t,pam_auth_name),
63 { NULL, -1, 0, NULL, NULL }
67 * (Re-)read radiusd.conf into memory.
69 static int pam_instantiate(CONF_SECTION *conf, void **instance)
73 data = rad_malloc(sizeof(*data));
77 memset(data, 0, sizeof(*data));
79 if (cf_section_parse(conf, data, module_config) < 0) {
91 static int pam_detach(void *instance)
93 rlm_pam_t *data = (rlm_pam_t *) instance;
95 free((char *) data->pam_auth_name);
100 /*************************************************************************
104 * Purpose: Dialogue between RADIUS and PAM modules.
106 * jab - stolen from pop3d
108 * Alan DeKok: modified to use PAM's appdata_ptr, so that we're
109 * multi-threaded safe, and don't have any nasty static
110 * variables hanging around.
112 *************************************************************************/
114 typedef struct my_PAM {
115 const char *username;
116 const char *password;
120 static int PAM_conv (int num_msg,
121 const struct pam_message **msg,
122 struct pam_response **resp,
125 struct pam_response *reply;
126 my_PAM *pam_config = (my_PAM *) appdata_ptr;
128 /* strdup(NULL) doesn't work on some platforms */
129 #define COPY_STRING(s) ((s) ? strdup(s) : NULL)
131 reply = rad_malloc(num_msg * sizeof(struct pam_response));
132 memset(reply, 0, num_msg * sizeof(struct pam_response));
133 for (count = 0; count < num_msg; count++) {
134 switch (msg[count]->msg_style) {
135 case PAM_PROMPT_ECHO_ON:
136 reply[count].resp_retcode = PAM_SUCCESS;
137 reply[count].resp = COPY_STRING(pam_config->username);
139 case PAM_PROMPT_ECHO_OFF:
140 reply[count].resp_retcode = PAM_SUCCESS;
141 reply[count].resp = COPY_STRING(pam_config->password);
148 /* Must be an error of some sort... */
149 for (count = 0; count < num_msg; count++) {
150 if (reply[count].resp) {
151 /* could be a password, let's be sanitary */
152 memset(reply[count].resp, 0, strlen(reply[count].resp));
153 free(reply[count].resp);
157 pam_config->error = 1;
162 /* PAM frees reply (including reply[].resp) */
167 /*************************************************************************
171 * Purpose: Check the users password against the standard UNIX
172 * password table + PAM.
175 *************************************************************************/
179 * for most flexibility, passing a pamauth type to this function
180 * allows you to have multiple authentication types (i.e. multiple
181 * files associated with radius in /etc/pam.d)
183 static int pam_pass(const char *name, const char *passwd, const char *pamauth)
185 pam_handle_t *pamh=NULL;
188 struct pam_conv conv;
191 * Initialize the structures.
193 conv.conv = PAM_conv;
194 conv.appdata_ptr = &pam_config;
195 pam_config.username = name;
196 pam_config.password = passwd;
197 pam_config.error = 0;
199 DEBUG("pam_pass: using pamauth string <%s> for pam.conf lookup", pamauth);
200 retval = pam_start(pamauth, name, &conv, &pamh);
201 if (retval != PAM_SUCCESS) {
202 DEBUG("pam_pass: function pam_start FAILED for <%s>. Reason: %s",
203 name, pam_strerror(pamh, retval));
207 retval = pam_authenticate(pamh, 0);
208 if (retval != PAM_SUCCESS) {
209 DEBUG("pam_pass: function pam_authenticate FAILED for <%s>. Reason: %s",
210 name, pam_strerror(pamh, retval));
211 pam_end(pamh, retval);
216 * FreeBSD 3.x doesn't have account and session management
217 * functions in PAM, while 4.0 does.
219 #if !defined(__FreeBSD_version) || (__FreeBSD_version >= 400000)
220 retval = pam_acct_mgmt(pamh, 0);
221 if (retval != PAM_SUCCESS) {
222 DEBUG("pam_pass: function pam_acct_mgmt FAILED for <%s>. Reason: %s",
223 name, pam_strerror(pamh, retval));
224 pam_end(pamh, retval);
229 DEBUG("pam_pass: authentication succeeded for <%s>", name);
230 pam_end(pamh, retval);
234 /* translate between function declarations */
235 static int pam_auth(void *instance, REQUEST *request)
239 rlm_pam_t *data = (rlm_pam_t *) instance;
241 const char *pam_auth_string = data->pam_auth_name;
244 * We can only authenticate user requests which HAVE
245 * a User-Name attribute.
247 if (!request->username) {
248 radlog(L_AUTH, "rlm_pam: Attribute \"User-Name\" is required for authentication.");
249 return RLM_MODULE_INVALID;
253 * We can only authenticate user requests which HAVE
254 * a User-Password attribute.
256 if (!request->password) {
257 radlog(L_AUTH, "rlm_pam: Attribute \"User-Password\" is required for authentication.");
258 return RLM_MODULE_INVALID;
262 * Ensure that we're being passed a plain-text password,
263 * and not anything else.
265 if (request->password->attribute != PW_PASSWORD) {
266 radlog(L_AUTH, "rlm_pam: Attribute \"User-Password\" is required for authentication. Cannot use \"%s\".", request->password->name);
267 return RLM_MODULE_INVALID;
271 * Let the 'users' file over-ride the PAM auth name string,
272 * for backwards compatibility.
274 pair = pairfind(request->config_items, PAM_AUTH_ATTR);
275 if (pair) pam_auth_string = (char *)pair->strvalue;
277 r = pam_pass((char *)request->username->strvalue,
278 (char *)request->password->strvalue,
282 if (!strcmp(radlog_dir, "syslog")) {
283 openlog(progname, LOG_PID, syslog_facility);
288 return RLM_MODULE_OK;
290 return RLM_MODULE_REJECT;
295 RLM_TYPE_THREAD_UNSAFE, /* The PAM libraries are not thread-safe */
296 NULL, /* initialize */
297 pam_instantiate, /* instantiation */
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 */
308 pam_detach, /* detach */