Made names of Auth-Type, etc. more consistent across the
[freeradius.git] / src / main / valuepair.c
1 /*
2  * valuepair.c  Valuepair functions that are radiusd-specific
3  *              and as such do not belong in the library.
4  *
5  * Version:     $Id$
6  *
7  *   This program is free software; you can redistribute it and/or modify
8  *   it under the terms of the GNU General Public License as published by
9  *   the Free Software Foundation; either version 2 of the License, or
10  *   (at your option) any later version.
11  *
12  *   This program is distributed in the hope that it will be useful,
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *   GNU General Public License for more details.
16  *
17  *   You should have received a copy of the GNU General Public License
18  *   along with this program; if not, write to the Free Software
19  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  *
21  * Copyright 2000  The FreeRADIUS server project
22  * Copyright 2000  Alan DeKok <aland@ox.org>
23  */
24
25 static const char rcsid[] = "$Id$";
26
27 #include "autoconf.h"
28 #include "libradius.h"
29
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33
34 #if HAVE_NETINET_IN_H
35 #       include <netinet/in.h>
36 #endif
37
38 #ifdef HAVE_REGEX_H
39 #       include <regex.h>
40
41 /*
42  *  For POSIX Regular expressions.
43  *  (0) Means no extended regular expressions.
44  *  REG_EXTENDED means use extended regular expressions.
45  */
46 #ifndef REG_EXTENDED
47 #define REG_EXTENDED (0)
48 #endif
49
50 #ifndef REG_NOSUB
51 #define REG_NOSUB (0)
52 #endif
53 #endif
54
55 #include "radiusd.h"
56
57 struct cmp {
58         int attribute;
59         int otherattr;
60         void *instance; /* module instance */
61         RAD_COMPARE_FUNC compare;
62         struct cmp *next;
63 };
64 static struct cmp *cmp;
65
66
67 /*
68  *      Compare 2 attributes. May call the attribute compare function.
69  */
70 static int paircompare(REQUEST *req, VALUE_PAIR *request, VALUE_PAIR *check,
71                        VALUE_PAIR *check_pairs, VALUE_PAIR **reply_pairs)
72 {
73         int ret = -2;
74         struct cmp *c;
75
76         /*
77          *      Sanity check.
78          */
79 #if 0
80         if (request->attribute != check->attribute)
81                 return -2;
82 #endif
83
84         /*
85          *      Check for =* and !* and return appropriately
86          */
87         if( check->operator == T_OP_CMP_TRUE )
88                  return 0;  /* always return 0/EQUAL */
89         if( check->operator == T_OP_CMP_FALSE )
90                  return 1;  /* always return 1/NOT EQUAL */
91
92         /*
93          *      See if there is a special compare function.
94          */
95         for (c = cmp; c; c = c->next)
96                 if (c->attribute == check->attribute)
97                         return (c->compare)(c->instance, req, request, check,
98                                 check_pairs, reply_pairs);
99
100         switch(check->type) {
101 #ifdef ASCEND_BINARY
102                 /*
103                  *      Ascend binary attributes can be treated
104                  *      as opaque objects, I guess...
105                  */
106                 case PW_TYPE_ABINARY:
107 #endif
108                 case PW_TYPE_OCTETS:
109                         if (request->length != check->length) {
110                                 ret = 1; /* NOT equal */
111                                 break;
112                         }
113                         ret = memcmp(request->strvalue, check->strvalue,
114                                         request->length);
115                         break;
116                 case PW_TYPE_STRING:
117                         ret = strcmp((char *)request->strvalue,
118                                         (char *)check->strvalue);
119                         break;
120                 case PW_TYPE_INTEGER:
121                 case PW_TYPE_DATE:
122                         ret = request->lvalue - check->lvalue;
123                         break;
124                 case PW_TYPE_IPADDR:
125                         ret = ntohl(request->lvalue) - ntohl(check->lvalue);
126                         break;
127                 default:
128                         break;
129         }
130
131         return ret;
132 }
133
134
135 /*
136  *      See what attribute we want to compare with.
137  */
138 static int otherattr(int attr)
139 {
140         struct cmp      *c;
141
142         for (c = cmp; c; c = c->next) {
143                 if (c->attribute == attr)
144                         return c->otherattr;
145         }
146
147         return attr;
148 }
149
150 /*
151  *      Register a function as compare function.
152  *      compare_attr is the attribute in the request we want to
153  *      compare with. Normally this is the same as "attr".
154  *      You can set this to:
155  *
156  *      -1   the same as "attr"
157  *      0    always call compare function, not tied to request attribute
158  *      >0   Attribute to compare with.
159  *
160  *      For example, PW_GROUP in a check item needs to be compared
161  *      with PW_USER_NAME in the incoming request.
162  */
163 int paircompare_register(int attr, int compare_attr, RAD_COMPARE_FUNC fun, void *instance)
164 {
165         struct cmp      *c;
166
167         paircompare_unregister(attr, fun);
168
169         c = rad_malloc(sizeof(struct cmp));
170
171         if (compare_attr < 0) 
172                 compare_attr = attr;
173         c->compare = fun;
174         c->attribute = attr;
175         c->otherattr = compare_attr;
176         c->instance = instance;
177         c->next = cmp;
178         cmp = c;
179
180         return 0;
181 }
182
183 /*
184  *      Unregister a function.
185  */
186 void paircompare_unregister(int attr, RAD_COMPARE_FUNC fun)
187 {
188         struct cmp      *c, *last;
189
190         last = NULL;
191         for (c = cmp; c; c = c->next) {
192                 if (c->attribute == attr && c->compare == fun)
193                         break;
194                 last = c;
195         }
196
197         if (c == NULL) return;
198
199         if (last != NULL)
200                 last->next = c->next;
201         else
202                 cmp = c->next;
203
204         free(c);
205 }
206
207 /*
208  *      Compare two pair lists except for the password information.
209  *      For every element in "check" at least one matching copy must
210  *      be present in "reply".
211  *
212  *      Return 0 on match.
213  */
214 int paircmp(REQUEST *req, VALUE_PAIR *request, VALUE_PAIR *check, VALUE_PAIR **reply)
215 {
216         VALUE_PAIR *check_item;
217         VALUE_PAIR *auth_item;
218         int result = 0;
219         int compare;
220         int other;
221 #ifdef HAVE_REGEX_H
222         regex_t reg;
223 #endif
224
225         for (check_item = check; check_item != NULL; check_item = check_item->next) {
226                 /*
227                  *      If the user is setting a configuration value,
228                  *      then don't bother comparing it to any attributes
229                  *      sent to us by the user.  It ALWAYS matches.
230                  */
231                 if ((check_item->operator == T_OP_SET) ||
232                     (check_item->operator == T_OP_ADD)) {
233                         continue;
234                 }
235
236                 switch (check_item->attribute) {
237                         /*
238                          *      Attributes we skip during comparison.
239                          *      These are "server" check items.
240                          */
241                         case PW_CRYPT_PASSWORD:
242                         case PW_AUTH_TYPE:
243                         case PW_AUTZ_TYPE:
244                                 continue;
245                                 break;
246
247                         /*
248                          *      IF the password attribute exists, THEN
249                          *      we can do comparisons against it.  If not,
250                          *      then the request did NOT contain a
251                          *      User-Password attribute, so we CANNOT do
252                          *      comparisons against it.
253                          *
254                          *      This hack makes CHAP-Password work..
255                          */
256                         case PW_PASSWORD:
257                                 if (pairfind(request, PW_PASSWORD) == NULL) {
258                                         continue;
259                                 }
260                                 break;
261                 }
262
263                 /*
264                  *      See if this item is present in the request.
265                  */
266                 other = otherattr(check_item->attribute);
267
268                 auth_item = request;
269         try_again:
270                 for (; auth_item != NULL; auth_item = auth_item->next) {
271                         if (auth_item->attribute == other || other == 0)
272                                 break;
273                 }
274
275                 /*
276                  *      Not found, it's not a match.
277                  */
278                 if (auth_item == NULL) {
279                         return -1;
280                 }
281
282                 /*
283                  *      We've got to xlat the string before doing
284                  *      the comparison.
285                  */
286                 if (check_item->flags.do_xlat) {
287                         int rcode;
288                         char buffer[sizeof(check_item->strvalue)];
289
290                         check_item->flags.do_xlat = 0;
291                         rcode = radius_xlat(buffer, sizeof(buffer),
292                                             check_item->strvalue,
293                                             req, NULL);
294                         
295                         /*
296                          *      Parse the string into a new value.
297                          */
298                         pairparsevalue(check_item, buffer);
299                 }
300
301                 /*
302                  *      OK it is present now compare them.
303                  */
304                 compare = paircompare(req, auth_item, check_item, check, reply);
305
306                 switch (check_item->operator) {
307                         case T_OP_EQ:
308                         default:
309                                 radlog(L_ERR,  "Invalid operator for item %s: "
310                                                 "reverting to '=='", check_item->name);
311                                 /*FALLTHRU*/
312                         case T_OP_CMP_TRUE:    /* compare always == 0 */
313                         case T_OP_CMP_FALSE:   /* compare always == 1 */
314                         case T_OP_CMP_EQ:
315                                 if (compare != 0) result = -1;
316                                 break;
317
318                         case T_OP_NE:
319                                 if (compare == 0) result = -1;
320                                 break;
321
322                         case T_OP_LT:
323                                 if (compare >= 0) result = -1;
324                                 break;
325
326                         case T_OP_GT:
327                                 if (compare <= 0) result = -1;
328                                 break;
329                     
330                         case T_OP_LE:
331                                 if (compare > 0) result = -1;
332                                 break;
333
334                         case T_OP_GE:
335                                 if (compare < 0) result = -1;
336                                 break;
337
338 #ifdef HAVE_REGEX_H
339                         case T_OP_REG_EQ:
340                                 regcomp(&reg, (char *)check_item->strvalue, REG_EXTENDED|REG_NOSUB);
341                                 compare = regexec(&reg, (char *)auth_item->strvalue,
342                                                 0, NULL, 0);
343                                 regfree(&reg);
344                                 if (compare != 0) result = -1;
345                                 break;
346
347                         case T_OP_REG_NE:
348                                 regcomp(&reg, (char *)check_item->strvalue, REG_EXTENDED|REG_NOSUB);
349                                 compare = regexec(&reg, (char *)auth_item->strvalue,
350                                                 0, NULL, 0);
351                                 regfree(&reg);
352                                 if (compare == 0) result = -1;
353                                 break;
354 #endif
355
356                 } /* switch over the operator of the check item */
357
358                 /*
359                  *      This attribute didn't match, but maybe there's
360                  *      another of the same attribute, which DOES match.
361                  */
362                 if (result != 0) {
363                         auth_item = auth_item->next;
364                         result = 0;
365                         goto try_again;
366                 }
367
368         } /* for every entry in the check item list */
369
370         return 0;               /* it matched */
371 }
372
373 /*
374  *      Compare two attributes simply.  Calls paircompare.
375  */
376
377 int simplepaircmp(REQUEST *req, VALUE_PAIR *first, VALUE_PAIR *second)
378 {
379         return paircompare( req, first, second, NULL, NULL );
380 }
381
382
383 /*
384  *      Compare a Connect-Info and a Connect-Rate
385  */
386 static int connectcmp(void *instance, REQUEST *req, VALUE_PAIR *request, VALUE_PAIR *check,
387                 VALUE_PAIR *check_pairs, VALUE_PAIR **reply_pairs)
388 {
389         int rate;
390
391         instance = instance;
392         check_pairs = check_pairs; /* shut the compiler up */
393         reply_pairs = reply_pairs;
394
395         rate = atoi((char *)request->strvalue);
396         return rate - check->lvalue;
397 }
398
399
400 /*
401  *      Compare a portno with a range.
402  */
403 static int portcmp(void *instance, REQUEST *req, VALUE_PAIR *request, VALUE_PAIR *check,
404         VALUE_PAIR *check_pairs, VALUE_PAIR **reply_pairs)
405 {
406         char buf[MAX_STRING_LEN];
407         char *s, *p;
408         uint32_t lo, hi;
409         uint32_t port = request->lvalue;
410
411         instance = instance;
412         check_pairs = check_pairs; /* shut the compiler up */
413         reply_pairs = reply_pairs;
414
415         if ((strchr((char *)check->strvalue, ',') == NULL) &&
416                         (strchr((char *)check->strvalue, '-') == NULL)) {
417                 return (request->lvalue - check->lvalue);
418         }
419
420         /* Same size */
421         strcpy(buf, (char *)check->strvalue);
422         s = strtok(buf, ",");
423
424         while (s != NULL) {
425                 if ((p = strchr(s, '-')) != NULL)
426                         p++;
427                 else
428                         p = s;
429                 lo = strtoul(s, NULL, 10);
430                 hi = strtoul(p, NULL, 10);
431                 if (lo <= port && port <= hi) {
432                         return 0;
433                 }
434                 s = strtok(NULL, ",");
435         } 
436
437         return -1;
438 }
439
440 /*
441  *      Compare prefix/suffix.
442  *
443  *      If they compare: 
444  *      - if PW_STRIP_USER_NAME is present in check_pairs,
445  *        strip the username of prefix/suffix.
446  *      - if PW_STRIP_USER_NAME is not present in check_pairs,
447  *        add a PW_STRIPPED_USER_NAME to the request.
448  */
449 static int presufcmp(void *instance, REQUEST *req, VALUE_PAIR *request, VALUE_PAIR *check,
450         VALUE_PAIR *check_pairs, VALUE_PAIR **reply_pairs)
451 {
452         VALUE_PAIR *vp;
453         char *name = (char *)request->strvalue;
454         char rest[MAX_STRING_LEN];
455         int len, namelen;
456         int ret = -1;
457         
458         instance = instance;
459         reply_pairs = reply_pairs; /* shut the compiler up */
460
461 #if 0 /* DEBUG */
462         printf("Comparing %s and %s, check->attr is %d\n",
463                 name, check->strvalue, check->attribute);
464 #endif
465
466         len = strlen((char *)check->strvalue);
467         switch (check->attribute) {
468                 case PW_PREFIX:
469                         ret = strncmp(name, (char *)check->strvalue, len);
470                         if (ret == 0 && rest)
471                                 strcpy(rest, name + len);
472                         break;
473                 case PW_SUFFIX:
474                         namelen = strlen(name);
475                         if (namelen < len)
476                                 break;
477                         ret = strcmp(name + namelen - len,
478                                         (char *)check->strvalue);
479                         if (ret == 0 && rest) {
480                                 strncpy(rest, name, namelen - len);
481                                 rest[namelen - len] = 0;
482                         }
483                         break;
484         }
485         if (ret != 0)
486                 return ret;
487
488         if (pairfind(check_pairs, PW_STRIP_USER_NAME)) {
489                 /*
490                  *      I don't think we want to update the User-Name
491                  *      attribute in place... - atd
492                  */
493                 strcpy((char *)request->strvalue, rest);
494                 request->length = strlen(rest);
495         } else {
496                 if ((vp = pairfind(check_pairs, PW_STRIPPED_USER_NAME)) != NULL){
497                         strcpy((char *)vp->strvalue, rest);
498                         vp->length = strlen(rest);
499                 } else if ((vp = paircreate(PW_STRIPPED_USER_NAME,
500                                 PW_TYPE_STRING)) != NULL) {
501                         strcpy((char *)vp->strvalue, rest);
502                         vp->length = strlen(rest);
503                         pairadd(&request, vp);
504                 } /* else no memory! Die, die!: FIXME!! */
505         }
506
507         return ret;
508 }
509
510
511 /*
512  *      Compare the current time to a range.
513  *      Hmm... it would save work, and probably be better,
514  *      if we were passed the REQUEST data structure, so we
515  *      could use it's 'timestamp' element.  That way, we could
516  *      do the comparison against when the packet came in, not now,
517  *      and have one less system call to do.
518  */
519 static int timecmp(void *instance, REQUEST *req, VALUE_PAIR *request, VALUE_PAIR *check,
520         VALUE_PAIR *check_pairs, VALUE_PAIR **reply_pairs)
521 {
522         instance = instance;
523         request = request;      /* shut the compiler up */
524         check_pairs = check_pairs;
525         reply_pairs = reply_pairs;
526
527         if (timestr_match((char *)check->strvalue, time(NULL)) >= 0) {
528                 return 0;
529         }
530         return -1;
531 }
532
533 /*
534  *      Matches if there is NO SUCH ATTRIBUTE as the one named
535  *      in check->strvalue.  If there IS such an attribute, it
536  *      doesn't match.
537  *
538  *      This is ugly, and definitely non-optimal.  We should be
539  *      doing the lookup only ONCE, and storing the result
540  *      in check->lvalue...
541  */
542 static int attrcmp(void *instance, REQUEST *req, VALUE_PAIR *request, VALUE_PAIR *check,
543         VALUE_PAIR *check_pairs, VALUE_PAIR **reply_pairs)
544 {
545         VALUE_PAIR *pair;
546         DICT_ATTR  *dict;
547         int attr;
548
549         instance = instance;
550         check_pairs = check_pairs; /* shut the compiler up */
551         reply_pairs = reply_pairs;
552
553         if (check->lvalue == 0) {
554                 dict = dict_attrbyname((char *)check->strvalue);
555                 if (dict == NULL) {
556                         return -1;
557                 }
558                 attr = dict->attr;
559         } else {
560                 attr = check->lvalue;
561         }
562
563         /*
564          *      If there's no such attribute, then return MATCH,
565          *      else FAILURE.
566          */
567         pair = pairfind(request, attr);
568         if (pair == NULL) {
569                 return 0;
570         }
571
572         return -1;
573 }
574
575 /*
576  *      Compare the expiration date.
577  */
578 static int expirecmp(void *instance, REQUEST *req, VALUE_PAIR *request, VALUE_PAIR *check,
579                      VALUE_PAIR *check_pairs, VALUE_PAIR **reply_pairs)
580 {
581         time_t now;
582
583         instance = instance;
584         request = request;      /* shut the compiler up */
585         check_pairs = check_pairs;
586         reply_pairs = reply_pairs;
587
588         /*
589          *  FIXME!  This should be request->timestamp!
590          */
591         now = time(NULL);
592
593         if (now <= check->lvalue) {
594                 return 0;
595         }
596
597         return +1;
598 }
599
600 /*
601  *      Compare the request packet type.
602  */
603 static int packetcmp(void *instance, REQUEST *req, VALUE_PAIR *request,
604                      VALUE_PAIR *check,
605                      VALUE_PAIR *check_pairs, VALUE_PAIR **reply_pairs)
606 {
607         if (req->packet->code == check->lvalue) {
608                 return 0;
609         }
610
611         return 1;
612 }
613
614 /*
615  *      Compare the response packet type.
616  */
617 static int responsecmp(void *instance, REQUEST *req, VALUE_PAIR *request,
618                      VALUE_PAIR *check,
619                      VALUE_PAIR *check_pairs, VALUE_PAIR **reply_pairs)
620 {
621         if (req->reply->code == check->lvalue) {
622                 return 0;
623         }
624
625         return 1;
626 }
627
628 /*
629  *      Register server-builtin special attributes.
630  */
631 void pair_builtincompare_init(void)
632 {
633         paircompare_register(PW_NAS_PORT, -1, portcmp, NULL);
634         paircompare_register(PW_PREFIX, PW_USER_NAME, presufcmp, NULL);
635         paircompare_register(PW_SUFFIX, PW_USER_NAME, presufcmp, NULL);
636         paircompare_register(PW_CONNECT_RATE, PW_CONNECT_INFO, connectcmp, NULL);
637         paircompare_register(PW_CURRENT_TIME, 0, timecmp, NULL);
638         paircompare_register(PW_NO_SUCH_ATTRIBUTE, 0, attrcmp, NULL);
639         paircompare_register(PW_EXPIRATION, 0, expirecmp, NULL);
640         paircompare_register(PW_PACKET_TYPE, 0, packetcmp, NULL);
641         paircompare_register(PW_RESPONSE_PACKET_TYPE, 0, responsecmp, NULL);
642 }
643
644 /*
645  *      Move pairs, replacing/over-writing them, and doing xlat.
646  */
647 /*
648  *      Move attributes from one list to the other
649  *      if not already present.
650  */
651 void pairxlatmove(REQUEST *req, VALUE_PAIR **to, VALUE_PAIR **from)
652 {
653         VALUE_PAIR **tailto, *i, *j, *next;
654         VALUE_PAIR *tailfrom = NULL;
655         VALUE_PAIR *found;
656
657         /*
658          *      Point "tailto" to the end of the "to" list.
659          */
660         tailto = to;
661         for(i = *to; i; i = i->next) {
662                 tailto = &i->next;
663         }
664
665         /*
666          *      Loop over the "from" list.
667          */
668         for(i = *from; i; i = next) {
669                 next = i->next;
670
671                 /*
672                  *      Don't move 'fallthrough' over.
673                  */
674                 if (i->attribute == PW_FALL_THROUGH) {
675                         continue;
676                 }
677
678                 /*
679                  *      We've got to xlat the string before moving
680                  *      it over.
681                  */
682                 if (i->flags.do_xlat) {
683                         int rcode;
684                         char buffer[sizeof(i->strvalue)];
685
686                         i->flags.do_xlat = 0;
687                         rcode = radius_xlat(buffer, sizeof(buffer),
688                                             i->strvalue,
689                                             req, NULL);
690                         
691                         /*
692                          *      Parse the string into a new value.
693                          */
694                         pairparsevalue(i, buffer);
695                 }
696
697                 found = pairfind(*to, i->attribute);
698                 switch (i->operator) {
699                         
700                         /*
701                          *  If a similar attribute is found,
702                          *  delete it.
703                          */
704                 case T_OP_SUB:          /* -= */
705                         if (found) {
706                                 if (!i->strvalue[0] ||
707                                     (strcmp((char *)found->strvalue,
708                                             (char *)i->strvalue) == 0)){
709                                         pairdelete(to, found->attribute);
710                                         
711                                         /*
712                                          *      'tailto' may have been
713                                          *      deleted...
714                                          */
715                                         tailto = to;
716                                         for(j = *to; j; j = j->next) {
717                                                 tailto = &j->next;
718                                         }
719                                 }
720                         }
721                         tailfrom = i;
722                         continue;
723                         break;
724                         
725                         /*
726                          *  Add it, if it's not already there.
727                          */
728                 case T_OP_EQ:           /* = */
729                         if (found) {
730                                 tailfrom = i;
731                                 continue; /* with the loop */
732                         }
733                         break;
734                         
735                         /*
736                          *  If a similar attribute is found,
737                          *  replace it with the new one.  Otherwise,
738                          *  add the new one to the list.
739                          */
740                 case T_OP_SET:          /* := */
741                         if (found) {
742                                 VALUE_PAIR *vp;
743
744                                 vp = found->next;
745                                 memcpy(found, i, sizeof(*found));
746                                 found->next = vp;
747                         }
748                         break;
749                         
750                         /*
751                          *  FIXME: Add support for <=, >=, <, >
752                          *
753                          *  which will mean (for integers)
754                          *  'make the attribute the smaller, etc'
755                          */
756                         
757                         /*
758                          *  Add the new element to the list, even
759                          *  if similar ones already exist.
760                          */
761                 default:
762                 case T_OP_ADD:          /* += */
763                         break;
764                 }
765                 
766                 if (tailfrom)
767                         tailfrom->next = next;
768                 else
769                         *from = next;
770                 
771                 /*
772                  *      If ALL of the 'to' attributes have been deleted,
773                  *      then ensure that the 'tail' is updated to point
774                  *      to the head.
775                  */
776                 if (!*to) {
777                         tailto = to;
778                 }
779                 *tailto = i;
780                 if (i) {
781                         i->next = NULL;
782                         tailto = &i->next;
783                 }
784         } /* loop over the 'from' list */
785 }