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>
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>
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,
123 int count = 0, replies = 0;
124 struct pam_response *reply = NULL;
125 int size = sizeof(struct pam_response);
126 my_PAM *pam_config = (my_PAM *) appdata_ptr;
128 #define GET_MEM if (reply) realloc(reply, size); else reply = rad_malloc(size); \
129 size += sizeof(struct pam_response)
130 #define COPY_STRING(s) ((s) ? strdup(s) : NULL)
132 for (count = 0; count < num_msg; count++) {
133 switch (msg[count]->msg_style) {
134 case PAM_PROMPT_ECHO_ON:
136 reply[replies].resp_retcode = PAM_SUCCESS;
137 reply[replies++].resp = COPY_STRING(pam_config->username);
140 case PAM_PROMPT_ECHO_OFF:
142 reply[replies].resp_retcode = PAM_SUCCESS;
143 reply[replies++].resp = COPY_STRING(pam_config->password);
151 /* Must be an error of some sort... */
153 pam_config->error = 1;
157 if (reply) *resp = reply;
162 /*************************************************************************
166 * Purpose: Check the users password against the standard UNIX
167 * password table + PAM.
170 *************************************************************************/
174 * for most flexibility, passing a pamauth type to this function
175 * allows you to have multiple authentication types (i.e. multiple
176 * files associated with radius in /etc/pam.d)
178 static int pam_pass(const char *name, const char *passwd, const char *pamauth)
180 pam_handle_t *pamh=NULL;
183 struct pam_conv conv;
186 * Initialize the structures.
188 conv.conv = PAM_conv;
189 conv.appdata_ptr = &pam_config;
190 pam_config.username = name;
191 pam_config.password = passwd;
192 pam_config.error = 0;
194 DEBUG("pam_pass: using pamauth string <%s> for pam.conf lookup", pamauth);
195 retval = pam_start(pamauth, name, &conv, &pamh);
196 if (retval != PAM_SUCCESS) {
197 DEBUG("pam_pass: function pam_start FAILED for <%s>. Reason: %s",
198 name, pam_strerror(pamh, retval));
202 retval = pam_authenticate(pamh, 0);
203 if (retval != PAM_SUCCESS) {
204 DEBUG("pam_pass: function pam_authenticate FAILED for <%s>. Reason: %s",
205 name, pam_strerror(pamh, retval));
206 pam_end(pamh, retval);
211 * FreeBSD 3.x doesn't have account and session management
212 * functions in PAM, while 4.0 does.
214 #if !defined(__FreeBSD_version) || (__FreeBSD_version >= 400000)
215 retval = pam_acct_mgmt(pamh, 0);
216 if (retval != PAM_SUCCESS) {
217 DEBUG("pam_pass: function pam_acct_mgmt FAILED for <%s>. Reason: %s",
218 name, pam_strerror(pamh, retval));
219 pam_end(pamh, retval);
224 DEBUG("pam_pass: authentication succeeded for <%s>", name);
225 pam_end(pamh, retval);
229 /* translate between function declarations */
230 static int pam_auth(void *instance, REQUEST *request)
234 rlm_pam_t *data = (rlm_pam_t *) instance;
236 const char *pam_auth_string = data->pam_auth_name;
239 * We can only authenticate user requests which HAVE
240 * a User-Name attribute.
242 if (!request->username) {
243 radlog(L_AUTH, "rlm_pam: Attribute \"User-Name\" is required for authentication.");
244 return RLM_MODULE_INVALID;
248 * We can only authenticate user requests which HAVE
249 * a User-Password attribute.
251 if (!request->password) {
252 radlog(L_AUTH, "rlm_pam: Attribute \"User-Password\" is required for authentication.");
253 return RLM_MODULE_INVALID;
257 * Ensure that we're being passed a plain-text password,
258 * and not anything else.
260 if (request->password->attribute != PW_PASSWORD) {
261 radlog(L_AUTH, "rlm_pam: Attribute \"User-Password\" is required for authentication. Cannot use \"%s\".", request->password->name);
262 return RLM_MODULE_INVALID;
266 * Let the 'users' file over-ride the PAM auth name string,
267 * for backwards compatibility.
269 pair = pairfind(request->config_items, PAM_AUTH_ATTR);
270 if (pair) pam_auth_string = (char *)pair->vp_strvalue;
272 r = pam_pass((char *)request->username->vp_strvalue,
273 (char *)request->password->vp_strvalue,
277 if (!strcmp(radlog_dir, "syslog")) {
278 openlog(progname, LOG_PID, mainconfig.syslog_facility);
283 return RLM_MODULE_OK;
285 return RLM_MODULE_REJECT;
291 RLM_TYPE_THREAD_UNSAFE, /* The PAM libraries are not thread-safe */
292 pam_instantiate, /* instantiation */
293 pam_detach, /* detach */
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 */