Move a few paircompare functions for specific attributes from the server core
[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 #ifdef 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                         if (check->flags.caseless) {
118                                 ret = strcasecmp((char *)request->strvalue,
119                                                  (char *)check->strvalue);
120                         } else {
121                                 ret = strcmp((char *)request->strvalue,
122                                              (char *)check->strvalue);
123                         }
124                         break;
125                 case PW_TYPE_INTEGER:
126                 case PW_TYPE_DATE:
127                         ret = request->lvalue - check->lvalue;
128                         break;
129                 case PW_TYPE_IPADDR:
130                         ret = ntohl(request->lvalue) - ntohl(check->lvalue);
131                         break;
132                 default:
133                         break;
134         }
135
136         return ret;
137 }
138
139
140 /*
141  *      See what attribute we want to compare with.
142  */
143 static int otherattr(int attr)
144 {
145         struct cmp      *c;
146
147         for (c = cmp; c; c = c->next) {
148                 if (c->attribute == attr)
149                         return c->otherattr;
150         }
151
152         return attr;
153 }
154
155 /*
156  *      Register a function as compare function.
157  *      compare_attr is the attribute in the request we want to
158  *      compare with. Normally this is the same as "attr".
159  *      You can set this to:
160  *
161  *      -1   the same as "attr"
162  *      0    always call compare function, not tied to request attribute
163  *      >0   Attribute to compare with.
164  *
165  *      For example, PW_GROUP in a check item needs to be compared
166  *      with PW_USER_NAME in the incoming request.
167  */
168 int paircompare_register(int attr, int compare_attr, RAD_COMPARE_FUNC fun, void *instance)
169 {
170         struct cmp      *c;
171
172         paircompare_unregister(attr, fun);
173
174         c = rad_malloc(sizeof(struct cmp));
175
176         if (compare_attr < 0)
177                 compare_attr = attr;
178         c->compare = fun;
179         c->attribute = attr;
180         c->otherattr = compare_attr;
181         c->instance = instance;
182         c->next = cmp;
183         cmp = c;
184
185         return 0;
186 }
187
188 /*
189  *      Unregister a function.
190  */
191 void paircompare_unregister(int attr, RAD_COMPARE_FUNC fun)
192 {
193         struct cmp      *c, *last;
194
195         last = NULL;
196         for (c = cmp; c; c = c->next) {
197                 if (c->attribute == attr && c->compare == fun)
198                         break;
199                 last = c;
200         }
201
202         if (c == NULL) return;
203
204         if (last != NULL)
205                 last->next = c->next;
206         else
207                 cmp = c->next;
208
209         free(c);
210 }
211
212 /*
213  *      Compare two pair lists except for the password information.
214  *      For every element in "check" at least one matching copy must
215  *      be present in "reply".
216  *
217  *      Return 0 on match.
218  */
219 int paircmp(REQUEST *req, VALUE_PAIR *request, VALUE_PAIR *check, VALUE_PAIR **reply)
220 {
221         VALUE_PAIR *check_item;
222         VALUE_PAIR *auth_item;
223         int result = 0;
224         int compare;
225         int other;
226 #ifdef HAVE_REGEX_H
227         regex_t reg;
228 #endif
229
230         for (check_item = check; check_item != NULL; check_item = check_item->next) {
231                 /*
232                  *      If the user is setting a configuration value,
233                  *      then don't bother comparing it to any attributes
234                  *      sent to us by the user.  It ALWAYS matches.
235                  */
236                 if ((check_item->operator == T_OP_SET) ||
237                     (check_item->operator == T_OP_ADD)) {
238                         continue;
239                 }
240
241                 switch (check_item->attribute) {
242                         /*
243                          *      Attributes we skip during comparison.
244                          *      These are "server" check items.
245                          */
246                         case PW_CRYPT_PASSWORD:
247                         case PW_AUTH_TYPE:
248                         case PW_AUTZ_TYPE:
249                         case PW_ACCT_TYPE:
250                         case PW_SESSION_TYPE:
251                         case PW_STRIP_USER_NAME:
252                                 continue;
253                                 break;
254
255                         /*
256                          *      IF the password attribute exists, THEN
257                          *      we can do comparisons against it.  If not,
258                          *      then the request did NOT contain a
259                          *      User-Password attribute, so we CANNOT do
260                          *      comparisons against it.
261                          *
262                          *      This hack makes CHAP-Password work..
263                          */
264                         case PW_PASSWORD:
265                                 if (pairfind(request, PW_PASSWORD) == NULL) {
266                                         continue;
267                                 }
268                                 break;
269                 }
270
271                 /*
272                  *      See if this item is present in the request.
273                  */
274                 other = otherattr(check_item->attribute);
275
276                 auth_item = request;
277         try_again:
278                 for (; auth_item != NULL; auth_item = auth_item->next) {
279                         if (auth_item->attribute == other || other == 0)
280                                 break;
281                 }
282
283                 /*
284                  *      Not found, it's not a match.
285                  */
286                 if (auth_item == NULL) {
287                         /*
288                          *      Didn't find it.  If we were *trying*
289                          *      to not find it, then we succeeded.
290                          */
291                         if (check_item->operator == T_OP_CMP_FALSE)
292                                 return 0;
293                         else
294                                 return -1;
295                 }
296
297                 /*
298                  *      Else we found it, but we were trying to not
299                  *      find it, so we failed.
300                  */
301                 if (check_item->operator == T_OP_CMP_FALSE)
302                         return -1;
303
304
305                 /*
306                  *      We've got to xlat the string before doing
307                  *      the comparison.
308                  */
309                 if (check_item->flags.do_xlat) {
310                         int rcode;
311                         char buffer[sizeof(check_item->strvalue)];
312
313                         check_item->flags.do_xlat = 0;
314                         rcode = radius_xlat(buffer, sizeof(buffer),
315                                             check_item->strvalue,
316                                             req, NULL);
317
318                         /*
319                          *      Parse the string into a new value.
320                          */
321                         pairparsevalue(check_item, buffer);
322                 }
323
324                 /*
325                  *      OK it is present now compare them.
326                  */
327                 compare = paircompare(req, auth_item, check_item, check, reply);
328
329                 switch (check_item->operator) {
330                         case T_OP_EQ:
331                         default:
332                                 radlog(L_INFO,  "Invalid operator for item %s: "
333                                                 "reverting to '=='", check_item->name);
334                                 /*FALLTHRU*/
335                         case T_OP_CMP_TRUE:    /* compare always == 0 */
336                         case T_OP_CMP_FALSE:   /* compare always == 1 */
337                         case T_OP_CMP_EQ:
338                                 if (compare != 0) result = -1;
339                                 break;
340
341                         case T_OP_NE:
342                                 if (compare == 0) result = -1;
343                                 break;
344
345                         case T_OP_LT:
346                                 if (compare >= 0) result = -1;
347                                 break;
348
349                         case T_OP_GT:
350                                 if (compare <= 0) result = -1;
351                                 break;
352
353                         case T_OP_LE:
354                                 if (compare > 0) result = -1;
355                                 break;
356
357                         case T_OP_GE:
358                                 if (compare < 0) result = -1;
359                                 break;
360
361 #ifdef HAVE_REGEX_H
362                         case T_OP_REG_EQ:
363                         {
364                                 int i;
365                                 regmatch_t rxmatch[9];
366
367                                 /*
368                                  *      Include substring matches.
369                                  */
370                                 regcomp(&reg, (char *)check_item->strvalue,
371                                         REG_EXTENDED);
372                                 compare = regexec(&reg,
373                                                   (char *)auth_item->strvalue,
374                                                   16, rxmatch, 0);
375                                 regfree(&reg);
376
377                                 /*
378                                  *      Add %{0}, %{1}, etc.
379                                  */
380                                 for (i = 0; i <= 8; i++) {
381                                         char *p;
382                                         char buffer[sizeof(check_item->strvalue)];
383
384                                         /*
385                                          *      Didn't match: delete old
386                                          *      match, if it existed.
387                                          */
388                                         if ((compare != 0) ||
389                                             (rxmatch[i].rm_so == -1)) {
390                                                 p = request_data_get(req, req,
391                                                                      REQUEST_DATA_REGEX | i);
392                                                 if (p) {
393                                                         free(p);
394                                                         continue;
395                                                 }
396
397                                                 /*
398                                                  *      No previous match
399                                                  *      to delete, stop.
400                                                  */
401                                                 break;
402                                         }
403                                         
404                                         /*
405                                          *      Copy substring into buffer.
406                                          */
407                                         memcpy(buffer,
408                                                auth_item->strvalue + rxmatch[i].rm_so,
409                                                rxmatch[i].rm_eo - rxmatch[i].rm_so);
410                                         buffer[rxmatch[i].rm_eo - rxmatch[i].rm_so] = '\0';
411
412                                         /*
413                                          *      Copy substring, and add it to
414                                          *      the request.
415                                          *
416                                          *      Note that we don't check
417                                          *      for out of memory, which is
418                                          *      the only error we can get...
419                                          */
420                                         p = strdup(buffer);
421                                         request_data_add(req,
422                                                          req,
423                                                          REQUEST_DATA_REGEX | i,
424                                                          p, free);
425                                 }
426                         }                               
427                                 if (compare != 0) result = -1;
428                                 break;
429
430                         case T_OP_REG_NE:
431                                 regcomp(&reg, (char *)check_item->strvalue, REG_EXTENDED|REG_NOSUB);
432                                 compare = regexec(&reg, (char *)auth_item->strvalue,
433                                                 0, NULL, 0);
434                                 regfree(&reg);
435                                 if (compare == 0) result = -1;
436                                 break;
437 #endif
438
439                 } /* switch over the operator of the check item */
440
441                 /*
442                  *      This attribute didn't match, but maybe there's
443                  *      another of the same attribute, which DOES match.
444                  */
445                 if (result != 0) {
446                         auth_item = auth_item->next;
447                         result = 0;
448                         goto try_again;
449                 }
450
451         } /* for every entry in the check item list */
452
453         return 0;               /* it matched */
454 }
455
456 /*
457  *      Compare two attributes simply.  Calls paircompare.
458  */
459
460 int simplepaircmp(REQUEST *req, VALUE_PAIR *first, VALUE_PAIR *second)
461 {
462         return paircompare( req, first, second, NULL, NULL );
463 }
464
465
466 /*
467  *      Move pairs, replacing/over-writing them, and doing xlat.
468  */
469 /*
470  *      Move attributes from one list to the other
471  *      if not already present.
472  */
473 void pairxlatmove(REQUEST *req, VALUE_PAIR **to, VALUE_PAIR **from)
474 {
475         VALUE_PAIR **tailto, *i, *j, *next;
476         VALUE_PAIR *tailfrom = NULL;
477         VALUE_PAIR *found;
478
479         /*
480          *      Point "tailto" to the end of the "to" list.
481          */
482         tailto = to;
483         for(i = *to; i; i = i->next) {
484                 tailto = &i->next;
485         }
486
487         /*
488          *      Loop over the "from" list.
489          */
490         for(i = *from; i; i = next) {
491                 next = i->next;
492
493                 /*
494                  *      Don't move 'fallthrough' over.
495                  */
496                 if (i->attribute == PW_FALL_THROUGH) {
497                         continue;
498                 }
499
500                 /*
501                  *      We've got to xlat the string before moving
502                  *      it over.
503                  */
504                 if (i->flags.do_xlat) {
505                         int rcode;
506                         char buffer[sizeof(i->strvalue)];
507
508                         i->flags.do_xlat = 0;
509                         rcode = radius_xlat(buffer, sizeof(buffer),
510                                             i->strvalue,
511                                             req, NULL);
512
513                         /*
514                          *      Parse the string into a new value.
515                          */
516                         pairparsevalue(i, buffer);
517                 }
518
519                 found = pairfind(*to, i->attribute);
520                 switch (i->operator) {
521
522                         /*
523                          *  If a similar attribute is found,
524                          *  delete it.
525                          */
526                 case T_OP_SUB:          /* -= */
527                         if (found) {
528                                 if (!i->strvalue[0] ||
529                                     (strcmp((char *)found->strvalue,
530                                             (char *)i->strvalue) == 0)){
531                                         pairdelete(to, found->attribute);
532
533                                         /*
534                                          *      'tailto' may have been
535                                          *      deleted...
536                                          */
537                                         tailto = to;
538                                         for(j = *to; j; j = j->next) {
539                                                 tailto = &j->next;
540                                         }
541                                 }
542                         }
543                         tailfrom = i;
544                         continue;
545                         break;
546
547                         /*
548                          *  Add it, if it's not already there.
549                          */
550                 case T_OP_EQ:           /* = */
551                         if (found) {
552                                 tailfrom = i;
553                                 continue; /* with the loop */
554                         }
555                         break;
556
557                         /*
558                          *  If a similar attribute is found,
559                          *  replace it with the new one.  Otherwise,
560                          *  add the new one to the list.
561                          */
562                 case T_OP_SET:          /* := */
563                         if (found) {
564                                 VALUE_PAIR *vp;
565
566                                 vp = found->next;
567                                 memcpy(found, i, sizeof(*found));
568                                 found->next = vp;
569                                 continue;
570                         }
571                         break;
572
573                         /*
574                          *  FIXME: Add support for <=, >=, <, >
575                          *
576                          *  which will mean (for integers)
577                          *  'make the attribute the smaller, etc'
578                          */
579
580                         /*
581                          *  Add the new element to the list, even
582                          *  if similar ones already exist.
583                          */
584                 default:
585                 case T_OP_ADD:          /* += */
586                         break;
587                 }
588
589                 if (tailfrom)
590                         tailfrom->next = next;
591                 else
592                         *from = next;
593
594                 /*
595                  *      If ALL of the 'to' attributes have been deleted,
596                  *      then ensure that the 'tail' is updated to point
597                  *      to the head.
598                  */
599                 if (!*to) {
600                         tailto = to;
601                 }
602                 *tailto = i;
603                 if (i) {
604                         i->next = NULL;
605                         tailto = &i->next;
606                 }
607         } /* loop over the 'from' list */
608 }