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,
124 int count = 0, replies = 0;
125 struct pam_response *reply = NULL;
126 int size = sizeof(struct pam_response);
127 my_PAM *pam_config = (my_PAM *) appdata_ptr;
129 #define GET_MEM if (reply) realloc(reply, size); else reply = rad_malloc(size); \
130 size += sizeof(struct pam_response)
131 #define COPY_STRING(s) ((s) ? strdup(s) : NULL)
133 for (count = 0; count < num_msg; count++) {
134 switch (msg[count]->msg_style) {
135 case PAM_PROMPT_ECHO_ON:
137 reply[replies].resp_retcode = PAM_SUCCESS;
138 reply[replies++].resp = COPY_STRING(pam_config->username);
141 case PAM_PROMPT_ECHO_OFF:
143 reply[replies].resp_retcode = PAM_SUCCESS;
144 reply[replies++].resp = COPY_STRING(pam_config->password);
152 /* Must be an error of some sort... */
154 pam_config->error = 1;
158 if (reply) *resp = reply;
163 /*************************************************************************
167 * Purpose: Check the users password against the standard UNIX
168 * password table + PAM.
171 *************************************************************************/
175 * for most flexibility, passing a pamauth type to this function
176 * allows you to have multiple authentication types (i.e. multiple
177 * files associated with radius in /etc/pam.d)
179 static int pam_pass(const char *name, const char *passwd, const char *pamauth)
181 pam_handle_t *pamh=NULL;
184 struct pam_conv conv;
187 * Initialize the structures.
189 conv.conv = PAM_conv;
190 conv.appdata_ptr = &pam_config;
191 pam_config.username = name;
192 pam_config.password = passwd;
193 pam_config.error = 0;
195 DEBUG("pam_pass: using pamauth string <%s> for pam.conf lookup", pamauth);
196 retval = pam_start(pamauth, name, &conv, &pamh);
197 if (retval != PAM_SUCCESS) {
198 DEBUG("pam_pass: function pam_start FAILED for <%s>. Reason: %s",
199 name, pam_strerror(pamh, retval));
203 retval = pam_authenticate(pamh, 0);
204 if (retval != PAM_SUCCESS) {
205 DEBUG("pam_pass: function pam_authenticate FAILED for <%s>. Reason: %s",
206 name, pam_strerror(pamh, retval));
207 pam_end(pamh, retval);
212 * FreeBSD 3.x doesn't have account and session management
213 * functions in PAM, while 4.0 does.
215 #if !defined(__FreeBSD_version) || (__FreeBSD_version >= 400000)
216 retval = pam_acct_mgmt(pamh, 0);
217 if (retval != PAM_SUCCESS) {
218 DEBUG("pam_pass: function pam_acct_mgmt FAILED for <%s>. Reason: %s",
219 name, pam_strerror(pamh, retval));
220 pam_end(pamh, retval);
225 DEBUG("pam_pass: authentication succeeded for <%s>", name);
226 pam_end(pamh, retval);
230 /* translate between function declarations */
231 static int pam_auth(void *instance, REQUEST *request)
235 rlm_pam_t *data = (rlm_pam_t *) instance;
237 const char *pam_auth_string = data->pam_auth_name;
240 * We can only authenticate user requests which HAVE
241 * a User-Name attribute.
243 if (!request->username) {
244 radlog(L_AUTH, "rlm_pam: Attribute \"User-Name\" is required for authentication.");
245 return RLM_MODULE_INVALID;
249 * We can only authenticate user requests which HAVE
250 * a User-Password attribute.
252 if (!request->password) {
253 radlog(L_AUTH, "rlm_pam: Attribute \"User-Password\" is required for authentication.");
254 return RLM_MODULE_INVALID;
258 * Ensure that we're being passed a plain-text password,
259 * and not anything else.
261 if (request->password->attribute != PW_PASSWORD) {
262 radlog(L_AUTH, "rlm_pam: Attribute \"User-Password\" is required for authentication. Cannot use \"%s\".", request->password->name);
263 return RLM_MODULE_INVALID;
267 * Let the 'users' file over-ride the PAM auth name string,
268 * for backwards compatibility.
270 pair = pairfind(request->config_items, PAM_AUTH_ATTR);
271 if (pair) pam_auth_string = (char *)pair->strvalue;
273 r = pam_pass((char *)request->username->strvalue,
274 (char *)request->password->strvalue,
278 if (!strcmp(radlog_dir, "syslog")) {
279 openlog(progname, LOG_PID, syslog_facility);
284 return RLM_MODULE_OK;
286 return RLM_MODULE_REJECT;
291 RLM_TYPE_THREAD_UNSAFE, /* The PAM libraries are not thread-safe */
292 NULL, /* initialize */
293 pam_instantiate, /* instantiation */
295 pam_auth, /* authenticate */
296 NULL, /* authorize */
297 NULL, /* pre-accounting */
298 NULL, /* accounting */
299 NULL, /* checksimul */
300 NULL, /* pre-proxy */
301 NULL, /* post-proxy */
304 pam_detach, /* detach */