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"
38 #include <security/pam_appl.h>
46 typedef struct rlm_pam_t {
47 const char *pam_auth_name;
50 static CONF_PARSER module_config[] = {
51 { "pam_auth", PW_TYPE_STRING_PTR, offsetof(rlm_pam_t,pam_auth_name),
53 { NULL, -1, 0, NULL, NULL }
57 * (Re-)read radiusd.conf into memory.
59 static int pam_instantiate(CONF_SECTION *conf, void **instance)
63 data = rad_malloc(sizeof(*data));
65 if (cf_section_parse(conf, data, module_config) < 0) {
77 static int pam_detach(void *instance)
79 rlm_pam_t *data = (rlm_pam_t *) instance;
81 free((char *) data->pam_auth_name);
86 /*************************************************************************
90 * Purpose: Dialogue between RADIUS and PAM modules.
92 * jab - stolen from pop3d
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.
98 *************************************************************************/
100 typedef struct my_PAM {
101 const char *username;
102 const char *password;
106 static int PAM_conv (int num_msg,
107 const struct pam_message **msg,
108 struct pam_response **resp,
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;
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)
119 for (count = 0; count < num_msg; count++) {
120 switch (msg[count]->msg_style) {
121 case PAM_PROMPT_ECHO_ON:
123 reply[replies].resp_retcode = PAM_SUCCESS;
124 reply[replies++].resp = COPY_STRING(pam_config->username);
127 case PAM_PROMPT_ECHO_OFF:
129 reply[replies].resp_retcode = PAM_SUCCESS;
130 reply[replies++].resp = COPY_STRING(pam_config->password);
138 /* Must be an error of some sort... */
140 pam_config->error = 1;
144 if (reply) *resp = reply;
149 /*************************************************************************
153 * Purpose: Check the users password against the standard UNIX
154 * password table + PAM.
157 *************************************************************************/
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)
165 static int pam_pass(const char *name, const char *passwd, const char *pamauth)
167 pam_handle_t *pamh=NULL;
170 struct pam_conv conv;
173 * Initialize the structures.
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;
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));
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));
198 * FreeBSD 3.x doesn't have account and session management
199 * functions in PAM, while 4.0 does.
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));
211 DEBUG("pam_pass: authentication succeeded for <%s>", name);
216 /* translate between function declarations */
217 static int pam_auth(void *instance, REQUEST *request)
221 rlm_pam_t *data = (rlm_pam_t *) instance;
223 const char *pam_auth_string = data->pam_auth_name;
226 * We can only authenticate user requests which HAVE
227 * a User-Name attribute.
229 if (!request->username) {
230 radlog(L_AUTH, "rlm_pam: Attribute \"User-Name\" is required for authentication.");
231 return RLM_MODULE_INVALID;
235 * We can only authenticate user requests which HAVE
236 * a User-Password attribute.
238 if (!request->password) {
239 radlog(L_AUTH, "rlm_pam: Attribute \"User-Password\" is required for authentication.");
240 return RLM_MODULE_INVALID;
244 * Ensure that we're being passed a plain-text password,
245 * and not anything else.
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;
253 * Let the 'users' file over-ride the PAM auth name string,
254 * for backwards compatibility.
256 pair = pairfind(request->config_items, PAM_AUTH_ATTR);
257 if (pair) pam_auth_string = (char *)pair->strvalue;
259 r = pam_pass((char *)request->username->strvalue,
260 (char *)request->password->strvalue,
264 if (!strcmp(radlog_dir, "syslog")) {
265 openlog(progname, LOG_PID, syslog_facility);
270 return RLM_MODULE_OK;
272 return RLM_MODULE_REJECT;
277 0, /* type: reserved */
278 NULL, /* initialize */
279 pam_instantiate, /* instantiation */
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 */
290 pam_detach, /* detach */