2 * rlm_dbm.c authorize: authorize using ndbm database
6 * This program is is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License, version 2 if the
8 * License as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * Copyright 2001 Koulik Andrei, Sandy Service
26 #ifdef HAVE_GDBM_NDBM_H
27 #include <gdbm/ndbm.h>
30 #ifdef HAVE_GDBMNDBM_H
31 #include <gdbm-ndbm.h>
36 #include "libradius.h"
41 # include "sandymod.h"
44 static const char rcsid[] = "$Id$";
47 #define get_user_content dbm_fetch
49 #define SM_JOIN_ATTR 1029
52 # define SM_POOL_ATTR 510
55 typedef struct rlm_dbm_t {
65 typedef struct user_entry {
67 struct user_entry * next;
71 static CONF_PARSER module_config[] = {
72 { "usersfile", PW_TYPE_STRING_PTR,offsetof(struct rlm_dbm_t,userfile),
74 { NULL, -1, 0, NULL, NULL }
77 static void sm_user_list_wipe (SM_USER_ENTRY **ue) {
79 SM_USER_ENTRY * pue, *nue;
84 while ( pue != NULL ) {
86 DEBUG2("Remove %s from user list", pue -> username);
87 free(pue -> username);
95 * add username un to user list ue;
96 * return 0 if user succefuly added
97 * 1 - if username already exists
98 * -1 - error: no memmory
101 static int sm_user_list_add(SM_USER_ENTRY **ue, const char *un) {
104 if ( strcmp( (*ue) -> username, un) == 0 ) return 1;
105 ue = & ((*ue) -> next);
107 *ue = malloc(sizeof(SM_USER_ENTRY));
108 if ( !*ue ) return -1;
109 (*ue) -> username = strdup(un);
110 DEBUG2("Add %s to user list", (*ue) -> username);
111 (*ue) -> next = NULL ;
112 if ( ! (*ue) -> username ) {
129 static int isfallthrough(VALUE_PAIR *vp) {
132 tmp = pairfind(vp, PW_FALL_THROUGH);
133 return tmp ? tmp -> lvalue : 1; /* if no FALL_THROUGH - keep looking */
137 * find user, parse and return result
140 * username - user name from request
141 * request - pair originated from the nas
142 * mode - search mode SM_SM_ACCUM - accumulative search mode
145 * parsed_users - list of parsed user names for loop removal
148 static int sm_parse_user(DBM *pdb, const char * username, VALUE_PAIR const* request, VALUE_PAIR **config,
149 VALUE_PAIR **reply, SM_USER_ENTRY **ulist)
152 int retcod, found = RLM_MODULE_NOTFOUND, res ;
153 VALUE_PAIR *vp = NULL,* tmp_config = NULL, *tmp_reply = NULL, *nu_reply = NULL;
154 VALUE_PAIR *join_attr;
157 int parse_state = SMP_PATTERN;
158 int continue_search = 1;
162 DEBUG2("sm_parse_user.c: check for loops");
164 if ( (retcod = sm_user_list_add(ulist,username) ) ) {
165 if ( retcod < 0 ) radlog(L_ERR,"rlm_dbm: Couldn't allocate memory");
166 else radlog(L_ERR,"rlm_dbm: Invalid configuration: loop detected");
167 return RLM_MODULE_FAIL;
170 /* retrieve user content */
173 k.dsize = strlen(username) + 1 ; /* username stored with '\0' */
175 d = dbm_fetch(pdb, k);
176 if ( d.dptr == NULL ) {
177 DEBUG2("rlm_dbm: User <%s> not foud in database\n",username);
178 return RLM_MODULE_NOTFOUND;
182 ch [ d.dsize ] = '\0'; /* should be closed by 0 */
184 DEBUG2("sm_parse_user: start parsing: user: %s", username);
186 /* start parse content */
187 while ( parse_state != SMP_ERROR && *ch && continue_search ) {
191 while( *ch && *ch != '\n') ch++ ;
193 if ( *ch == '\n' ) { *ch = 0; ch++; }
195 DEBUG2("parse buffer: <<%s>>\n",beg);
197 retcod = userparse(beg,&vp);
198 if ( retcod == T_INVALID ) librad_perror("parse error ");
201 case T_COMMA: break; /* continue parse the current list */
202 case T_EOL: DEBUG2("rlm_dbm: recod parsed\n"); /* vp contains full pair list */
203 if ( parse_state == SMP_PATTERN ) { /* pattern line found */
204 DEBUG2("process pattern");
205 /* check pattern against request */
206 if ( paircmp(NULL, request, vp, reply ) == 0 ) {
207 DEBUG2("rlm_dbm: Pattern matched, look for request");
208 pairmove(&tmp_config, &vp);
210 parse_state = SMP_REPLY; /* look for reply */
213 DEBUG2("rlm_dbm: patern not matched, reply skiped");
215 while ( *ch && *ch !='\n' ) ch++;
216 if ( *ch == '\n' ) ch++;
218 } else { /* reply line found */
219 /* look for join-attribute */
220 DEBUG2("rlm_dbm: Reply found");
222 while( (join_attr = pairfind(join_attr,SM_JOIN_ATTR) ) != NULL ) {
223 DEBUG2("rlm_dbm: Proccess nested record: username %s",
224 (char *)join_attr->strvalue);
225 /* res = RLM_MODULE_NOTFOUND; */
226 res = sm_parse_user(pdb, (char *)join_attr->strvalue, request, &tmp_config,
228 DEBUG("rlm_dbm: recived: %d\n",res);
230 case RLM_MODULE_NOTFOUND:
233 default: /* seems error code */
234 parse_state = SMP_ERROR;
235 DEBUG2("rlm_dbm: Nested record error\n");
238 join_attr = join_attr -> next;
240 pairdelete(&vp,SM_JOIN_ATTR);
241 if ( parse_state != SMP_ERROR ) {
242 if ( ! isfallthrough(vp) ) {
244 DEBUG2("rlm_dbm: Break search due Fall-Through = no");
246 pairmove(&vp,&nu_reply);
248 pairmove(&tmp_reply,&vp);
250 parse_state = SMP_PATTERN;
251 found = RLM_MODULE_OK;
254 pairfree(&nu_reply); }
256 default: /* we do not wait that !!!! */
257 parse_state = SMP_ERROR;
258 DEBUG2("rlm_dbm: Unknown token: %d\n",retcod);
263 if ( parse_state == SMP_PATTERN ) {
264 pairmove(config,&tmp_config);
265 pairfree(&tmp_config);
266 pairmove(reply,&tmp_reply);
267 pairfree(&tmp_reply);
269 pairfree(&tmp_config);
270 pairfree(&tmp_reply);
272 DEBUG2("rlm_dbm: Bad final parse state: %d\n",parse_state);
273 found = RLM_MODULE_FAIL ;
279 static int sm_postprocessor(VALUE_PAIR **reply) {
283 static int rlm_dbm_instantiate(CONF_SECTION *conf, void **instance) {
284 struct rlm_dbm_t *inst;
286 inst = rad_malloc(sizeof(rlm_dbm_t));
288 if (cf_section_parse(conf, inst, module_config) < 0) {
295 static int rlm_dbm_authorize(void *instance, REQUEST *request)
297 VALUE_PAIR *namepair;
298 VALUE_PAIR *request_pairs;
299 VALUE_PAIR *check_tmp = NULL;
300 VALUE_PAIR *reply_tmp = NULL;
304 SM_USER_ENTRY *ulist = NULL;
307 struct rlm_dbm_t *inst = instance;
309 VALUE_PAIR **check_pairs, **reply_pairs;
311 request_pairs = request->packet->vps;
312 check_pairs = &request->config_items;
313 reply_pairs = &request->reply->vps;
316 * Grab the canonical user name.
318 namepair = request->username;
319 name = namepair ? (char *) namepair->strvalue : "NONE";
321 DEBUG2("rlm_dbm: try open database file: %s\n",inst -> userfile);
324 if ( ( pdb = dbm_open(inst -> userfile, O_RDONLY, 0600) ) != NULL ) {
325 DEBUG("rlm_dbm: Call parse_user:\n");
326 found = sm_parse_user(pdb, name, request_pairs, &check_tmp, &reply_tmp, &ulist);
327 if ( found == RLM_MODULE_NOTFOUND ) {
328 sm_user_list_wipe(&ulist);
329 found = sm_parse_user(pdb, "DEFAULT", request_pairs, &check_tmp, &reply_tmp, &ulist);
333 found = RLM_MODULE_FAIL;
334 DEBUG2("rlm_dbm: Cannot open database file: %s\n",strerror(errno));
337 if ( found == RLM_MODULE_OK ) {
338 /* do preprocessor for final reply-pair tranformation */
339 if ( !sm_postprocessor(&reply_tmp) ) {
340 pairmove(reply_pairs, &reply_tmp);
341 pairmove(check_pairs, &check_tmp);
342 } else found = RLM_MODULE_FAIL;
344 sm_user_list_wipe(&ulist);
345 pairfree(&reply_tmp);
346 pairfree(&check_tmp);
351 static int rlm_dbm_detach(void *instance)
353 struct rlm_dbm_t *inst = instance;
354 free(inst -> userfile);
360 /* globally exported name */
363 0, /* type: reserved */
364 NULL, /* initialization */
365 rlm_dbm_instantiate, /* instantiation */
367 NULL, /* authentication */
368 rlm_dbm_authorize, /* authorization */
369 NULL, /* preaccounting */
370 NULL, /* accounting */
371 NULL /* checksimul */
373 rlm_dbm_detach, /* detach */