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