rlm_eap: add eap_chbind.c to build
[freeradius.git] / src / modules / rlm_passwd / rlm_passwd.c
1 /*
2  * rlm_passwd.c
3  *
4  *   This program is free software; you can redistribute it and/or modify
5  *   it under the terms of the GNU General Public License as published by
6  *   the Free Software Foundation; either version 2 of the License, or
7  *   (at your option) any later version.
8  *
9  *   This program is distributed in the hope that it will be useful,
10  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *   GNU General Public License for more details.
13  *
14  *   You should have received a copy of the GNU General Public License
15  *   along with this program; if not, write to the Free Software
16  *   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17  *
18  * Copyright 2000,2006  The FreeRADIUS server project
19  */
20
21 #include <freeradius-devel/ident.h>
22 RCSID("$Id$")
23
24 #include <freeradius-devel/radiusd.h>
25 #include <freeradius-devel/modules.h>
26
27 struct mypasswd {
28         struct mypasswd *next;
29         char *listflag;
30         char *field[1];
31 };
32
33 struct hashtable {
34         int tablesize;
35         int keyfield;
36         int nfields;
37         int islist;
38         int ignorenis;
39         char * filename;
40         struct mypasswd **table;
41         struct mypasswd *last_found;
42         char buffer[1024];
43         FILE *fp;
44         char delimiter;
45 };
46
47
48 #ifdef TEST
49
50 #define rad_malloc(s) malloc(s)
51
52 void printpw(struct mypasswd *pw, int nfields){
53   int i;
54   if (pw) {
55         for( i = 0; i < nfields; i++ ) printf("%s:", pw->field[i]);
56         printf("\n");
57   }
58   else printf ("Not found\n");
59   fflush(stdout);
60 }
61 #endif
62
63
64 static struct mypasswd * mypasswd_malloc(const char* buffer, int nfields, size_t* len)
65 {
66         struct mypasswd *t;
67         /* reserve memory for (struct mypasswd) + listflag (nfields * sizeof (char*)) +
68         ** fields (nfields * sizeof (char)) + strlen (inst->format) + 1 */
69
70         *len=sizeof (struct mypasswd) + nfields * sizeof (char*) + nfields * sizeof (char ) + strlen(buffer) + 1;
71         t = (struct mypasswd *) rad_malloc(*len);
72         if (t) memset(t, 0, *len);
73         return (t);
74 }
75
76 static int string_to_entry(const char* string, int nfields, char delimiter,
77         struct mypasswd *passwd, size_t bufferlen)
78 {
79         char *str;
80         size_t len, i;
81         int fn=0;
82         char *data_beg;
83
84
85         len = strlen(string);
86         if(!len) return 0;
87         if (string[len-1] == '\n') len--;
88         if(!len) return 0;
89         if (string[len-1] == '\r') len--;
90         if(!len) return 0;
91         if (!len || !passwd ||
92             bufferlen < (len + nfields * sizeof (char*) + nfields * sizeof (char) + sizeof (struct mypasswd) + 1) ) return 0;
93         passwd->next = NULL;
94         data_beg=(char *)passwd + sizeof(struct mypasswd);
95         str = data_beg + nfields * sizeof (char) + nfields * sizeof (char*);
96         memcpy (str, string, len);
97         str[len] = 0;
98         passwd->field[fn++] = str;
99         passwd->listflag = data_beg + nfields * sizeof (char *);
100         for(i=0; i < len; i++){
101                 if (str[i] == delimiter) {
102                         str[i] = 0;
103                         passwd->field[fn++] = str + i + 1;
104                         if (fn == nfields) break;
105                 }
106         }
107         for (; fn < nfields; fn++) passwd->field[fn] = NULL;
108         return len + nfields * sizeof (char) + nfields * sizeof (char*) + sizeof (struct mypasswd) + 1;
109 }
110
111
112 static void destroy_password (struct mypasswd * pass)
113 {
114         struct mypasswd *p;
115         while ((p=pass)!=NULL) {
116                 pass = pass->next;
117                 free(p);
118         }
119 }
120
121
122 static unsigned int hash(const char * username, unsigned int tablesize)
123 {
124         int h=1;
125         while (*username) {
126                 h = h * 7907 + *username++;
127         }
128         return h%tablesize;
129 }
130
131 static void release_hash_table(struct hashtable * ht){
132         int i;
133
134         if (ht == NULL) return;
135         for (i = 0; i < ht->tablesize; i++)
136                 if (ht->table[i])
137                         destroy_password(ht->table[i]);
138         if (ht->table) {
139                 free(ht->table);
140                 ht->table = NULL;
141         }
142         if (ht->fp) {
143                 fclose(ht->fp);
144                 ht->fp = NULL;
145         }
146         ht->tablesize = 0;
147 }
148
149 static void release_ht(struct hashtable * ht){
150         if (!ht) return;
151         release_hash_table(ht);
152         if (ht->filename) free(ht->filename);
153         free(ht);
154 }
155
156 static struct hashtable * build_hash_table (const char * file, int nfields,
157         int keyfield, int islist, int tablesize, int ignorenis, char delimiter)
158 {
159 #define passwd ((struct mypasswd *) ht->buffer)
160         char buffer[1024];
161         struct hashtable* ht;
162         size_t len;
163         unsigned int h;
164         struct mypasswd *hashentry, *hashentry1;
165         char *list;
166         char *nextlist=0;
167         int i;
168
169         ht = (struct hashtable *) rad_malloc(sizeof(struct hashtable));
170         if(!ht) {
171                 return NULL;
172         }
173         memset(ht, 0, sizeof(struct hashtable));
174         ht->filename = strdup(file);
175         if(!ht->filename) {
176                 free(ht);
177                 return NULL;
178         }
179         ht->tablesize = tablesize;
180         ht->nfields = nfields;
181         ht->keyfield = keyfield;
182         ht->islist = islist;
183         ht->ignorenis = ignorenis;
184         if (delimiter) ht->delimiter = delimiter;
185         else ht->delimiter = ':';
186         if(!tablesize) return ht;
187         if(!(ht->fp = fopen(file,"r"))) {
188                 free(ht->filename);
189                 free(ht);
190                                return NULL;
191         }
192         memset(ht->buffer, 0, 1024);
193         ht->table = (struct mypasswd **) rad_malloc (tablesize * sizeof(struct mypasswd *));
194         if (!ht->table) {
195                 /*
196                  * Unable allocate memory for hash table
197                  * Still work without it
198                  */
199                 ht->tablesize = 0;
200                 return ht;
201         }
202         memset(ht->table, 0, tablesize * sizeof(struct mypasswd *));
203         while (fgets(buffer, 1024, ht->fp)) {
204                 if(*buffer && *buffer!='\n' && (!ignorenis || (*buffer != '+' && *buffer != '-')) ){
205                         if(!(hashentry = mypasswd_malloc(buffer, nfields, &len))){
206                                 release_hash_table(ht);
207                                 return ht;
208                         }
209                         len = string_to_entry(buffer, nfields, ht->delimiter, hashentry, len);
210                         if(!hashentry->field[keyfield] || *hashentry->field[keyfield] == '\0') {
211                                 free(hashentry);
212                                 continue;
213                         }
214
215                         if (islist) {
216                                 list = hashentry->field[keyfield];
217                                 for (nextlist = list; *nextlist && *nextlist!=','; nextlist++);
218                                 if (*nextlist) *nextlist++ = 0;
219                                 else nextlist = 0;
220                         }
221                         h = hash(hashentry->field[keyfield], tablesize);
222                         hashentry->next = ht->table[h];
223                         ht->table[h] = hashentry;
224                         if (islist) {
225                                 for(list=nextlist; nextlist; list = nextlist){
226                                         for (nextlist = list; *nextlist && *nextlist!=','; nextlist++);
227                                         if (*nextlist) *nextlist++ = 0;
228                                         else nextlist = 0;
229                                         if(!(hashentry1 = mypasswd_malloc("", nfields, &len))){
230                                                 release_hash_table(ht);
231                                                 return ht;
232                                         }
233                                         for (i=0; i<nfields; i++) hashentry1->field[i] = hashentry->field[i];
234                                         hashentry1->field[keyfield] = list;
235                                         h = hash(list, tablesize);
236                                         hashentry1->next = ht->table[h];
237                                         ht->table[h] = hashentry1;
238                                 }
239                         }
240                 }
241         }
242         fclose(ht->fp);
243         ht->fp = NULL;
244         return ht;
245 #undef passwd
246 }
247
248 static struct mypasswd * get_next(char *name, struct hashtable *ht)
249 {
250 #define passwd ((struct mypasswd *) ht->buffer)
251         struct mypasswd * hashentry;
252         char buffer[1024];
253         int len;
254         char *list, *nextlist;
255
256         if (ht->tablesize > 0) {
257                 /* get saved address of next item to check from buffer */
258                 hashentry = ht->last_found;
259                 for (; hashentry; hashentry = hashentry->next) {
260                         if (!strcmp(hashentry->field[ht->keyfield], name)) {
261                                 /* save new address */
262                                 ht->last_found = hashentry->next;
263                                 return hashentry;
264                         }
265                 }
266                 return NULL;
267         }
268         /*      printf("try to find in file\n"); */
269         if (!ht->fp) return NULL;
270         while (fgets(buffer, 1024,ht->fp)) {
271                 if(*buffer && *buffer!='\n' && (len = string_to_entry(buffer, ht->nfields, ht->delimiter, passwd, sizeof(ht->buffer)-1)) &&
272                         (!ht->ignorenis || (*buffer !='-' && *buffer != '+') ) ){
273                         if(!ht->islist) {
274                                 if(!strcmp(passwd->field[ht->keyfield], name))
275                                         return passwd;
276                         }
277                         else {
278                                 for (list = passwd->field[ht->keyfield], nextlist = list; nextlist; list = nextlist) {
279                                         for(nextlist = list; *nextlist && *nextlist!=','; nextlist++);
280                                         if(!*nextlist)nextlist = 0;
281                                         else *nextlist++ = 0;
282                                         if(!strcmp(list, name)) return passwd;
283                                 }
284                         }
285
286                 }
287         }
288         fclose(ht->fp);
289         ht->fp = NULL;
290         return NULL;
291 #undef passwd
292 }
293
294 static struct mypasswd * get_pw_nam(char * name, struct hashtable* ht)
295 {
296         int h;
297         struct mypasswd * hashentry;
298
299         if (!ht || !name || *name == '\0') return NULL;
300         ht->last_found = NULL;
301         if (ht->tablesize > 0) {
302                 h = hash (name, ht->tablesize);
303                 for (hashentry = ht->table[h]; hashentry; hashentry = hashentry->next)
304                         if (!strcmp(hashentry->field[ht->keyfield], name)){
305                                 /* save address of next item to check into buffer */
306                                 ht->last_found=hashentry->next;
307                                 return hashentry;
308                         }
309                 return NULL;
310         }
311         if (ht->fp) {
312                 fclose(ht->fp);
313                 ht->fp = NULL;
314         }
315         if (!(ht->fp=fopen(ht->filename, "r"))) return NULL;
316         return get_next(name, ht);
317 }
318
319 #ifdef TEST
320
321 #define MALLOC_CHECK_ 1
322
323 int main(void){
324  struct hashtable *ht;
325  char *buffer;
326  struct mypasswd* pw;
327  int i;
328
329  ht = build_hash_table("/etc/group", 4, 3, 1, 100, 0, ":");
330  if(!ht) {
331         printf("Hash table not built\n");
332         return -1;
333  }
334  for (i=0; i<ht->tablesize; i++) if (ht->table[i]) {
335   printf("%d:\n", i);
336   for(pw=ht->table[i]; pw; pw=pw->next) printpw(pw, 4);
337  }
338
339  while(fgets(buffer, 1024, stdin)){
340   buffer[strlen(buffer)-1] = 0;
341   pw = get_pw_nam(buffer, ht);
342   printpw(pw,4);
343   while (pw = get_next(buffer, ht)) printpw(pw,4);
344  }
345  release_ht(ht);
346 }
347
348 #else  /* TEST */
349 struct passwd_instance {
350         struct hashtable *ht;
351         struct mypasswd *pwdfmt;
352         char *filename;
353         char *format;
354         char * delimiter;
355         int allowmultiple;
356         int ignorenislike;
357         int hashsize;
358         int nfields;
359         int keyfield;
360         int listable;
361         DICT_ATTR *keyattr;
362         int ignoreempty;
363 };
364
365 static const CONF_PARSER module_config[] = {
366         { "filename",   PW_TYPE_FILENAME,
367            offsetof(struct passwd_instance, filename), NULL,  NULL },
368         { "format",   PW_TYPE_STRING_PTR,
369            offsetof(struct passwd_instance, format), NULL,  NULL },
370         { "delimiter",   PW_TYPE_STRING_PTR,
371            offsetof(struct passwd_instance, delimiter), NULL,  ":" },
372         { "ignorenislike",   PW_TYPE_BOOLEAN,
373            offsetof(struct passwd_instance, ignorenislike), NULL,  "yes" },
374         { "ignoreempty",   PW_TYPE_BOOLEAN,
375            offsetof(struct passwd_instance, ignoreempty), NULL,  "yes" },
376         { "allowmultiplekeys",   PW_TYPE_BOOLEAN,
377            offsetof(struct passwd_instance, allowmultiple), NULL,  "no" },
378         { "hashsize",   PW_TYPE_INTEGER,
379            offsetof(struct passwd_instance, hashsize), NULL,  "100" },
380         { NULL, -1, 0, NULL, NULL }
381 };
382
383 static int passwd_instantiate(CONF_SECTION *conf, void **instance)
384 {
385 #define inst ((struct passwd_instance *)*instance)
386         int nfields=0, keyfield=-1, listable=0;
387         char *s;
388         char *lf=NULL; /* destination list flags temporary */
389         size_t len;
390         int i;
391         DICT_ATTR * da;
392
393         *instance = rad_malloc(sizeof(struct passwd_instance));
394         if ( !*instance) {
395                 radlog(L_ERR, "rlm_passwd: cann't alloc instance");
396                 return -1;
397         }
398         memset(*instance, 0, sizeof(struct passwd_instance));
399         if (cf_section_parse(conf, inst, module_config) < 0) {
400                 free(inst);
401                 radlog(L_ERR, "rlm_passwd: cann't parse configuration");
402                 return -1;
403         }
404         if(!inst->filename || *inst->filename == '\0' || !inst->format || *inst->format == '\0') {
405                 radlog(L_ERR, "rlm_passwd: cann't find passwd file and/or format in configuration");
406                 return -1;
407         }
408         lf=strdup(inst->format);
409         if ( lf == NULL) {
410                 radlog(L_ERR, "rlm_passwd: memory allocation failed for lf");
411                 return -1;
412         }
413         memset(lf, 0, strlen(inst->format));
414         s = inst->format - 1;
415         do {
416                 if(s == inst->format - 1 || *s == ':'){
417                         if(*(s+1) == '*'){
418                                 keyfield = nfields;
419                                 s++;
420                         }
421                         if(*(s+1) == ','){
422                                 listable = 1;
423                                 s++;
424                         }
425                         if(*(s+1) == '='){
426                                 lf[nfields]=1;
427                                 s++;
428                         }
429                         if(*(s+1) == '~'){
430                                 lf[nfields]=2;
431                                 s++;
432                         }
433                         nfields++;
434                 }
435                 s++;
436         }while(*s);
437         if(keyfield < 0) {
438                 radlog(L_ERR, "rlm_passwd: no field market as key in format: %s", inst->format);
439                 free(lf);
440                 return -1;
441         }
442         if (! (inst->ht = build_hash_table (inst->filename, nfields, keyfield, listable, inst->hashsize, inst->ignorenislike, *inst->delimiter)) ){
443                 radlog(L_ERR, "rlm_passwd: can't build hashtable from passwd file");
444                 free(lf);
445                 return -1;
446         }
447         if (! (inst->pwdfmt = mypasswd_malloc(inst->format, nfields, &len)) ){
448                 radlog(L_ERR, "rlm_passwd: memory allocation failed");
449                 release_ht(inst->ht);
450                 free(lf);
451                 return -1;
452         }
453         if (!string_to_entry(inst->format, nfields, ':', inst->pwdfmt , len)) {
454                 radlog(L_ERR, "rlm_passwd: unable to convert format entry");
455                 release_ht(inst->ht);
456                 free(lf);
457                 return -1;
458         }
459
460         memcpy(inst->pwdfmt->listflag, lf, nfields);
461
462         free(lf);
463         for (i=0; i<nfields; i++) {
464                 if (*inst->pwdfmt->field[i] == '*') inst->pwdfmt->field[i]++;
465                 if (*inst->pwdfmt->field[i] == ',') inst->pwdfmt->field[i]++;
466                 if (*inst->pwdfmt->field[i] == '=') inst->pwdfmt->field[i]++;
467                 if (*inst->pwdfmt->field[i] == '~') inst->pwdfmt->field[i]++;
468         }
469         if (!*inst->pwdfmt->field[keyfield]) {
470                 radlog(L_ERR, "rlm_passwd: key field is empty");
471                 release_ht(inst->ht);
472                 return -1;
473         }
474         if (! (da = dict_attrbyname (inst->pwdfmt->field[keyfield])) ) {
475                 radlog(L_ERR, "rlm_passwd: unable to resolve attribute: %s", inst->pwdfmt->field[keyfield]);
476                 release_ht(inst->ht);
477                 return -1;
478         }
479         inst->keyattr = da;
480         inst->nfields = nfields;
481         inst->keyfield = keyfield;
482         inst->listable = listable;
483         DEBUG("rlm_passwd: nfields: %d keyfield %d(%s) listable: %s", nfields, keyfield, inst->pwdfmt->field[keyfield], listable?"yes":"no");
484         return 0;
485
486 #undef inst
487 }
488
489 static int passwd_detach (void *instance) {
490 #define inst ((struct passwd_instance *)instance)
491         if(inst->ht) release_ht(inst->ht);
492         free(instance);
493         return 0;
494 #undef inst
495 }
496
497 static void addresult (struct passwd_instance * inst, REQUEST *request, VALUE_PAIR ** vp, struct mypasswd * pw, char when, const char *listname)
498 {
499         int i;
500         VALUE_PAIR *newpair;
501
502         for (i=0; i<inst->nfields; i++) {
503                 if (inst->pwdfmt->field[i] && *inst->pwdfmt->field[i] && pw->field[i] && i != inst->keyfield  && inst->pwdfmt->listflag[i] == when) {
504                         if ( !inst->ignoreempty || pw->field[i][0] != 0 ) { /* if value in key/value pair is not empty */
505                                 if (! (newpair = pairmake (inst->pwdfmt->field[i], pw->field[i], T_OP_EQ))) {
506                                         radlog(L_AUTH, "rlm_passwd: Unable to create %s: %s", inst->pwdfmt->field[i], pw->field[i]);
507                                         return;
508                                 }
509                                 RDEBUG("Added %s: '%s' to %s ", inst->pwdfmt->field[i], pw->field[i], listname);
510                                 pairadd (vp, newpair);
511                         } else
512                                 RDEBUG("NOOP %s: '%s' to %s ", inst->pwdfmt->field[i], pw->field[i], listname);
513                 }
514         }
515 }
516
517 static int passwd_map(void *instance, REQUEST *request)
518 {
519 #define inst ((struct passwd_instance *)instance)
520         char buffer[1024];
521         VALUE_PAIR * key;
522         struct mypasswd * pw;
523         int found = 0;
524
525         for (key = request->packet->vps;
526              key && (key = pairfind (key, inst->keyattr->attr, inst->keyattr->vendor));
527           key = key->next ){
528                 /*
529                  *      Ensure we have the string form of the attribute
530                  */
531                 vp_prints_value(buffer, sizeof(buffer), key, 0);
532                 if (! (pw = get_pw_nam(buffer, inst->ht)) ) {
533                         continue;
534                 }
535                 do {
536                         addresult(inst, request, &request->config_items, pw, 0, "config_items");
537                         addresult(inst, request, &request->reply->vps, pw, 1, "reply_items");
538                         addresult(inst, request, &request->packet->vps,         pw, 2, "request_items");
539                 } while ( (pw = get_next(buffer, inst->ht)) );
540                 found++;
541                 if (!inst->allowmultiple) break;
542         }
543         if(!found) {
544                 return RLM_MODULE_NOTFOUND;
545         }
546         return RLM_MODULE_OK;
547
548 #undef inst
549 }
550
551 module_t rlm_passwd = {
552         RLM_MODULE_INIT,
553         "passwd",
554         RLM_TYPE_CHECK_CONFIG_SAFE | RLM_TYPE_HUP_SAFE,         /* type */
555         passwd_instantiate,             /* instantiation */
556         passwd_detach,                  /* detach */
557         {
558                 NULL,                   /* authentication */
559                 passwd_map,             /* authorization */
560                 NULL,                   /* pre-accounting */
561                 passwd_map,             /* accounting */
562                 NULL,                   /* checksimul */
563                 NULL,                   /* pre-proxy */
564                 NULL,                   /* post-proxy */
565                 passwd_map              /* post-auth */
566 #ifdef WITH_COA
567                 , passwd_map,
568                 passwd_map
569 #endif
570         },
571 };
572 #endif /* TEST */