6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * Copyright 2001 The FreeRADIUS server project
21 * Copyright 2001 Kostas Kalevras <kkalev@noc.ntua.gr>
25 #include "libradius.h"
35 #include "../../include/md5.h"
36 #include "../../include/sha1.h"
38 #define PAP_ENC_INVALID -1
39 #define PAP_ENC_CLEAR 0
40 #define PAP_ENC_CRYPT 1
42 #define PAP_ENC_SHA1 3
45 #define PAP_INST_FREE(inst) \
46 free((char *)inst->scheme); \
49 static const char rcsid[] = "$Id$";
52 * Define a structure for our module configuration.
54 * These variables do not need to be in a structure, but it's
55 * a lot cleaner to do so, and a pointer to the structure can
56 * be used as the instance handle.
58 typedef struct rlm_pap_t {
59 char *scheme; /* password encryption scheme */
64 * A mapping of configuration file names to internal variables.
66 * Note that the string is dynamically allocated, so it MUST
67 * be freed. When the configuration file parse re-reads the string,
68 * it free's the old one, and strdup's the new one, placing the pointer
69 * to the strdup'd string into 'config.string'. This gets around
72 static CONF_PARSER module_config[] = {
73 { "encryption_scheme", PW_TYPE_STRING_PTR, offsetof(rlm_pap_t,scheme), NULL, "crypt" },
74 { NULL, -1, 0, NULL, NULL }
77 static const char *pap_hextab = "0123456789abcdef";
80 * Smaller & faster than snprintf("%x");
81 * Completely stolen from ns_mta_md5 module
83 static void pap_hexify(char *buffer, char *str, int len)
89 for(i = 0;i < len; i ++) {
91 buffer[2*i] = pap_hextab[(ch>>4) & 15];
92 buffer[2*i + 1] = pap_hextab[ch & 15];
97 static int pap_instantiate(CONF_SECTION *conf, void **instance)
102 * Set up a storage area for instance data
104 inst = rad_malloc(sizeof(*inst));
108 memset(inst, 0, sizeof(*inst));
111 * If the configuration parameters can't be parsed, then
114 if (cf_section_parse(conf, inst, module_config) < 0) {
118 inst->sch = PAP_ENC_INVALID;
119 if (inst->scheme == NULL || strlen(inst->scheme) == 0){
120 radlog(L_ERR, "rlm_pap: Wrong password scheme passed");
124 if (strcasecmp(inst->scheme,"clear") == 0)
125 inst->sch = PAP_ENC_CLEAR;
126 else if (strcasecmp(inst->scheme,"crypt") == 0){
127 inst->sch = PAP_ENC_CRYPT;
129 else if (strcasecmp(inst->scheme,"md5") == 0)
130 inst->sch = PAP_ENC_MD5;
131 else if (strcasecmp(inst->scheme,"sha1") == 0)
132 inst->sch = PAP_ENC_SHA1;
134 radlog(L_ERR, "rlm_pap: Wrong password scheme passed");
145 * Find the named user in this modules database. Create the set
146 * of attribute-value pairs to check and reply with for this user
147 * from the database. The authentication code only needs to check
148 * the password, the rest is done here.
150 static int pap_authenticate(void *instance, REQUEST *request)
152 VALUE_PAIR *passwd_item;
153 VALUE_PAIR *module_fmsg_vp;
154 char module_fmsg[MAX_STRING_LEN];
156 SHA1_CTX sha1_context;
158 char buff[MAX_STRING_LEN];
159 rlm_pap_t *inst = (rlm_pap_t *) instance;
161 /* quiet the compiler */
165 if(!request->username){
166 radlog(L_AUTH, "rlm_pap: Attribute \"User-Name\" is required for authentication.\n");
167 return RLM_MODULE_INVALID;
170 if (!request->password){
171 radlog(L_AUTH, "rlm_pap: Attribute \"Password\" is required for authentication.");
172 return RLM_MODULE_INVALID;
175 if (request->password->attribute != PW_PASSWORD) {
176 radlog(L_AUTH, "rlm_pap: Attribute \"Password\" is required for authentication. Cannot use \"%s\".", request->password->name);
177 return RLM_MODULE_INVALID;
180 if (request->password->length == 0) {
181 snprintf(module_fmsg,sizeof(module_fmsg),"rlm_pap: empty password supplied");
182 module_fmsg_vp = pairmake("Module-Failure-Message", module_fmsg, T_OP_EQ);
183 pairadd(&request->packet->vps, module_fmsg_vp);
184 return RLM_MODULE_INVALID;
187 DEBUG("rlm_pap: login attempt by \"%s\" with password %s",
188 request->username->strvalue, request->password->strvalue);
190 if ((((passwd_item = pairfind(request->config_items, PW_PASSWORD)) == NULL) &&
191 ((passwd_item = pairfind(request->config_items, PW_CRYPT_PASSWORD)) == NULL)) ||
192 (passwd_item->length == 0) || (passwd_item->strvalue[0] == 0)) {
193 DEBUG("rlm_pap: No password (or empty password) to check against for for user %s",request->username->strvalue);
194 snprintf(module_fmsg,sizeof(module_fmsg),"rlm_pap: User password not available");
195 module_fmsg_vp = pairmake("Module-Failure-Message", module_fmsg, T_OP_EQ);
196 pairadd(&request->packet->vps, module_fmsg_vp);
197 return RLM_MODULE_INVALID;
199 if (passwd_item->attribute == PW_CRYPT_PASSWORD){
200 if (inst->sch != PAP_ENC_CRYPT){
201 radlog(L_ERR, "rlm_pap: Crypt-Password attribute but encryption scheme is not set to CRYPT");
202 return RLM_MODULE_FAIL;
206 DEBUG("rlm_pap: Using password \"%s\" for user %s authentication.",
207 passwd_item->strvalue, request->username->strvalue);
209 if (inst->sch == PAP_ENC_INVALID || inst->sch > PAP_MAX_ENC){
210 radlog(L_ERR, "rlm_pap: Wrong password scheme");
211 return RLM_MODULE_FAIL;
215 radlog(L_ERR, "rlm_pap: Wrong password scheme");
216 return RLM_MODULE_FAIL;
219 DEBUG("rlm_pap: Using clear text password.");
220 if (strcmp((char *) passwd_item->strvalue,
221 (char *) request->password->strvalue) != 0){
222 DEBUG("rlm_pap: Passwords don't match");
223 snprintf(module_fmsg,sizeof(module_fmsg),"rlm_pap: CLEAR TEXT password check failed");
224 module_fmsg_vp = pairmake("Module-Failure-Message",module_fmsg, T_OP_EQ);
225 pairadd(&request->packet->vps, module_fmsg_vp);
226 return RLM_MODULE_REJECT;
230 DEBUG("rlm_pap: Using CRYPT encryption.");
231 if (lrad_crypt_check((char *) request->password->strvalue,
232 (char *) passwd_item->strvalue) != 0) {
233 DEBUG("rlm_pap: Passwords don't match");
234 snprintf(module_fmsg,sizeof(module_fmsg),"rlm_pap: CRYPT password check failed");
235 module_fmsg_vp = pairmake("Module-Failure-Message",module_fmsg, T_OP_EQ);
236 pairadd(&request->packet->vps, module_fmsg_vp);
237 return RLM_MODULE_REJECT;
241 DEBUG("rlm_pap: Using MD5 encryption.");
243 if (passwd_item->length != 32) {
244 DEBUG("rlm_pap: Configured MD5 password has incorrect length");
245 snprintf(module_fmsg,sizeof(module_fmsg),"rlm_pap: Configured MD5 password has incorrect length");
246 module_fmsg_vp = pairmake("Module-Failure-Message",module_fmsg, T_OP_EQ);
247 pairadd(&request->packet->vps, module_fmsg_vp);
248 return RLM_MODULE_REJECT;
251 MD5Init(&md5_context);
252 MD5Update(&md5_context, request->password->strvalue, request->password->length);
253 MD5Final(digest, &md5_context);
254 pap_hexify(buff,digest,16);
256 if (strcmp((char *)passwd_item->strvalue, buff) != 0){
257 DEBUG("rlm_pap: Passwords don't match");
258 snprintf(module_fmsg,sizeof(module_fmsg),"rlm_pap: MD5 password check failed");
259 module_fmsg_vp = pairmake("Module-Failure-Message",module_fmsg, T_OP_EQ);
260 pairadd(&request->packet->vps, module_fmsg_vp);
261 return RLM_MODULE_REJECT;
266 DEBUG("rlm_pap: Using SHA1 encryption.");
268 if (passwd_item->length != 40) {
269 DEBUG("rlm_pap: Configured SHA1 password has incorrect length");
270 snprintf(module_fmsg,sizeof(module_fmsg),"rlm_pap: Configured SHA1 password has incorrect length");
271 module_fmsg_vp = pairmake("Module-Failure-Message",module_fmsg, T_OP_EQ);
272 pairadd(&request->packet->vps, module_fmsg_vp);
273 return RLM_MODULE_REJECT;
276 SHA1Init(&sha1_context);
277 SHA1Update(&sha1_context, request->password->strvalue, request->password->length);
278 SHA1Final(digest,&sha1_context);
279 pap_hexify(buff,digest,20);
281 if (strcmp((char *)passwd_item->strvalue, buff) != 0){
282 DEBUG("rlm_pap: Passwords don't match");
283 snprintf(module_fmsg,sizeof(module_fmsg),"rlm_pap: SHA1 password check failed");
284 module_fmsg_vp = pairmake("Module-Failure-Message",module_fmsg, T_OP_EQ);
285 pairadd(&request->packet->vps, module_fmsg_vp);
286 return RLM_MODULE_REJECT;
291 DEBUG("rlm_pap: User authenticated succesfully");
293 return RLM_MODULE_OK;
296 static int pap_detach(void *instance)
298 rlm_pap_t *inst = (rlm_pap_t *) instance;
306 * The module name should be the only globally exported symbol.
307 * That is, everything else should be 'static'.
309 * If the module needs to temporarily modify it's instantiation
310 * data, the type should be changed to RLM_TYPE_THREAD_UNSAFE.
311 * The server will then take care of ensuring that the module
312 * is single-threaded.
317 NULL, /* initialization */
318 pap_instantiate, /* instantiation */
320 pap_authenticate, /* authentication */
321 NULL, /* authorization */
322 NULL, /* preaccounting */
323 NULL, /* accounting */
324 NULL, /* checksimul */
325 NULL, /* pre-proxy */
326 NULL, /* post-proxy */
329 pap_detach, /* detach */