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