Renamed paircmp to paircompare, as we should probably have a
[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
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32
33 #ifdef HAVE_NETINET_IN_H
34 #       include <netinet/in.h>
35 #endif
36
37 #ifdef HAVE_REGEX_H
38 #       include <regex.h>
39
40 /*
41  *  For POSIX Regular expressions.
42  *  (0) Means no extended regular expressions.
43  *  REG_EXTENDED means use extended regular expressions.
44  */
45 #ifndef REG_EXTENDED
46 #define REG_EXTENDED (0)
47 #endif
48
49 #ifndef REG_NOSUB
50 #define REG_NOSUB (0)
51 #endif
52 #endif
53
54 #include "radiusd.h"
55
56 struct cmp {
57         int attribute;
58         int otherattr;
59         void *instance; /* module instance */
60         RAD_COMPARE_FUNC compare;
61         struct cmp *next;
62 };
63 static struct cmp *cmp;
64
65
66 /*
67  *      Compare 2 attributes. May call the attribute compare function.
68  */
69 static int compare_pair(REQUEST *req, VALUE_PAIR *request, VALUE_PAIR *check,
70                        VALUE_PAIR *check_pairs, VALUE_PAIR **reply_pairs)
71 {
72         int ret = -2;
73         struct cmp *c;
74
75         /*
76          *      Check for =* and !* and return appropriately
77          */
78         if( check->operator == T_OP_CMP_TRUE )
79                  return 0;  /* always return 0/EQUAL */
80         if( check->operator == T_OP_CMP_FALSE )
81                  return 1;  /* always return 1/NOT EQUAL */
82
83         /*
84          *      See if there is a special compare function.
85          *
86          *      FIXME: use new RB-Tree code.
87          */
88         for (c = cmp; c; c = c->next)
89                 if (c->attribute == check->attribute)
90                         return (c->compare)(c->instance, req, request, check,
91                                 check_pairs, reply_pairs);
92
93         if (!request) return -1; /* doesn't exist, don't compare it */
94
95         switch(check->type) {
96 #ifdef ASCEND_BINARY
97                 /*
98                  *      Ascend binary attributes can be treated
99                  *      as opaque objects, I guess...
100                  */
101                 case PW_TYPE_ABINARY:
102 #endif
103                 case PW_TYPE_OCTETS:
104                         if (request->length != check->length) {
105                                 ret = 1; /* NOT equal */
106                                 break;
107                         }
108                         ret = memcmp(request->vp_strvalue, check->vp_strvalue,
109                                         request->length);
110                         break;
111                 case PW_TYPE_STRING:
112                         if (check->flags.caseless) {
113                                 ret = strcasecmp((char *)request->vp_strvalue,
114                                                  (char *)check->vp_strvalue);
115                         } else {
116                                 ret = strcmp((char *)request->vp_strvalue,
117                                              (char *)check->vp_strvalue);
118                         }
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 paircompare(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                         case PW_ACCT_TYPE:
245                         case PW_SESSION_TYPE:
246                         case PW_STRIP_USER_NAME:
247                                 continue;
248                                 break;
249
250                         /*
251                          *      IF the password attribute exists, THEN
252                          *      we can do comparisons against it.  If not,
253                          *      then the request did NOT contain a
254                          *      User-Password attribute, so we CANNOT do
255                          *      comparisons against it.
256                          *
257                          *      This hack makes CHAP-Password work..
258                          */
259                         case PW_PASSWORD:
260                                 if (pairfind(request, PW_PASSWORD) == NULL) {
261                                         continue;
262                                 }
263                                 break;
264                 }
265
266                 /*
267                  *      See if this item is present in the request.
268                  */
269                 other = otherattr(check_item->attribute);
270
271                 auth_item = request;
272         try_again:
273                 for (; auth_item != NULL; auth_item = auth_item->next) {
274                         if (auth_item->attribute == other || other == 0)
275                                 break;
276                 }
277
278                 /*
279                  *      Not found, it's not a match.
280                  */
281                 if (auth_item == NULL) {
282                         /*
283                          *      Didn't find it.  If we were *trying*
284                          *      to not find it, then we succeeded.
285                          */
286                         if (check_item->operator == T_OP_CMP_FALSE)
287                                 return 0;
288                         else
289                                 return -1;
290                 }
291
292                 /*
293                  *      Else we found it, but we were trying to not
294                  *      find it, so we failed.
295                  */
296                 if (check_item->operator == T_OP_CMP_FALSE)
297                         return -1;
298
299
300                 /*
301                  *      We've got to xlat the string before doing
302                  *      the comparison.
303                  */
304                 if (check_item->flags.do_xlat) {
305                         int rcode;
306                         char buffer[sizeof(check_item->vp_strvalue)];
307
308                         check_item->flags.do_xlat = 0;
309                         rcode = radius_xlat(buffer, sizeof(buffer),
310                                             check_item->vp_strvalue,
311                                             req, NULL);
312
313                         /*
314                          *      Parse the string into a new value.
315                          */
316                         pairparsevalue(check_item, buffer);
317                 }
318
319                 /*
320                  *      OK it is present now compare them.
321                  */
322                 compare = compare_pair(req, auth_item, check_item, check, reply);
323
324                 switch (check_item->operator) {
325                         case T_OP_EQ:
326                         default:
327                                 radlog(L_INFO,  "Invalid operator for item %s: "
328                                                 "reverting to '=='", check_item->name);
329                                 /*FALLTHRU*/
330                         case T_OP_CMP_TRUE:    /* compare always == 0 */
331                         case T_OP_CMP_FALSE:   /* compare always == 1 */
332                         case T_OP_CMP_EQ:
333                                 if (compare != 0) result = -1;
334                                 break;
335
336                         case T_OP_NE:
337                                 if (compare == 0) result = -1;
338                                 break;
339
340                         case T_OP_LT:
341                                 if (compare >= 0) result = -1;
342                                 break;
343
344                         case T_OP_GT:
345                                 if (compare <= 0) result = -1;
346                                 break;
347
348                         case T_OP_LE:
349                                 if (compare > 0) result = -1;
350                                 break;
351
352                         case T_OP_GE:
353                                 if (compare < 0) result = -1;
354                                 break;
355
356 #ifdef HAVE_REGEX_H
357                         case T_OP_REG_EQ:
358                         {
359                                 int i;
360                                 regmatch_t rxmatch[REQUEST_MAX_REGEX + 1];
361
362                                 if ((auth_item->type == PW_TYPE_IPADDR) &&
363                                     (auth_item->vp_strvalue[0] == '\0')) {
364                                   inet_ntop(AF_INET, &(auth_item->lvalue),
365                                             auth_item->vp_strvalue,
366                                             sizeof(auth_item->vp_strvalue));
367                                 }
368
369                                 /*
370                                  *      Include substring matches.
371                                  */
372                                 regcomp(&reg, (char *)check_item->vp_strvalue,
373                                         REG_EXTENDED);
374                                 compare = regexec(&reg,
375                                                   (char *)auth_item->vp_strvalue,
376                                                   REQUEST_MAX_REGEX + 1,
377                                                   rxmatch, 0);
378                                 regfree(&reg);
379
380                                 /*
381                                  *      Add %{0}, %{1}, etc.
382                                  */
383                                 for (i = 0; i <= REQUEST_MAX_REGEX; i++) {
384                                         char *p;
385                                         char buffer[sizeof(check_item->vp_strvalue)];
386
387                                         /*
388                                          *      Didn't match: delete old
389                                          *      match, if it existed.
390                                          */
391                                         if ((compare != 0) ||
392                                             (rxmatch[i].rm_so == -1)) {
393                                                 p = request_data_get(req, req,
394                                                                      REQUEST_DATA_REGEX | i);
395                                                 if (p) {
396                                                         free(p);
397                                                         continue;
398                                                 }
399
400                                                 /*
401                                                  *      No previous match
402                                                  *      to delete, stop.
403                                                  */
404                                                 break;
405                                         }
406                                         
407                                         /*
408                                          *      Copy substring into buffer.
409                                          */
410                                         memcpy(buffer,
411                                                auth_item->vp_strvalue + rxmatch[i].rm_so,
412                                                rxmatch[i].rm_eo - rxmatch[i].rm_so);
413                                         buffer[rxmatch[i].rm_eo - rxmatch[i].rm_so] = '\0';
414
415                                         /*
416                                          *      Copy substring, and add it to
417                                          *      the request.
418                                          *
419                                          *      Note that we don't check
420                                          *      for out of memory, which is
421                                          *      the only error we can get...
422                                          */
423                                         p = strdup(buffer);
424                                         request_data_add(req,
425                                                          req,
426                                                          REQUEST_DATA_REGEX | i,
427                                                          p, free);
428                                 }
429                         }                               
430                                 if (compare != 0) result = -1;
431                                 break;
432
433                         case T_OP_REG_NE:
434                                 if ((auth_item->type == PW_TYPE_IPADDR) &&
435                                     (auth_item->vp_strvalue[0] == '\0')) {
436                                   inet_ntop(AF_INET, &(auth_item->lvalue),
437                                             auth_item->vp_strvalue,
438                                             sizeof(auth_item->vp_strvalue));
439                                 }
440
441                                 regcomp(&reg, (char *)check_item->vp_strvalue, REG_EXTENDED|REG_NOSUB);
442                                 compare = regexec(&reg, (char *)auth_item->vp_strvalue,
443                                                 0, NULL, 0);
444                                 regfree(&reg);
445                                 if (compare == 0) result = -1;
446                                 break;
447 #endif
448
449                 } /* switch over the operator of the check item */
450
451                 /*
452                  *      This attribute didn't match, but maybe there's
453                  *      another of the same attribute, which DOES match.
454                  */
455                 if (result != 0) {
456                         auth_item = auth_item->next;
457                         result = 0;
458                         goto try_again;
459                 }
460
461         } /* for every entry in the check item list */
462
463         return 0;               /* it matched */
464 }
465
466 /*
467  *      Compare two attributes simply.  Calls compare_pair.
468  */
469
470 int simplepaircmp(REQUEST *req, VALUE_PAIR *first, VALUE_PAIR *second)
471 {
472         return compare_pair( req, first, second, NULL, NULL );
473 }
474
475
476 /*
477  *      Move pairs, replacing/over-writing them, and doing xlat.
478  */
479 /*
480  *      Move attributes from one list to the other
481  *      if not already present.
482  */
483 void pairxlatmove(REQUEST *req, VALUE_PAIR **to, VALUE_PAIR **from)
484 {
485         VALUE_PAIR **tailto, *i, *j, *next;
486         VALUE_PAIR *tailfrom = NULL;
487         VALUE_PAIR *found;
488
489         /*
490          *      Point "tailto" to the end of the "to" list.
491          */
492         tailto = to;
493         for(i = *to; i; i = i->next) {
494                 tailto = &i->next;
495         }
496
497         /*
498          *      Loop over the "from" list.
499          */
500         for(i = *from; i; i = next) {
501                 next = i->next;
502
503                 /*
504                  *      Don't move 'fallthrough' over.
505                  */
506                 if (i->attribute == PW_FALL_THROUGH) {
507                         continue;
508                 }
509
510                 /*
511                  *      We've got to xlat the string before moving
512                  *      it over.
513                  */
514                 if (i->flags.do_xlat) {
515                         int rcode;
516                         char buffer[sizeof(i->vp_strvalue)];
517
518                         i->flags.do_xlat = 0;
519                         rcode = radius_xlat(buffer, sizeof(buffer),
520                                             i->vp_strvalue,
521                                             req, NULL);
522
523                         /*
524                          *      Parse the string into a new value.
525                          */
526                         pairparsevalue(i, buffer);
527                 }
528
529                 found = pairfind(*to, i->attribute);
530                 switch (i->operator) {
531
532                         /*
533                          *  If a similar attribute is found,
534                          *  delete it.
535                          */
536                 case T_OP_SUB:          /* -= */
537                         if (found) {
538                                 if (!i->vp_strvalue[0] ||
539                                     (strcmp((char *)found->vp_strvalue,
540                                             (char *)i->vp_strvalue) == 0)){
541                                         pairdelete(to, found->attribute);
542
543                                         /*
544                                          *      'tailto' may have been
545                                          *      deleted...
546                                          */
547                                         tailto = to;
548                                         for(j = *to; j; j = j->next) {
549                                                 tailto = &j->next;
550                                         }
551                                 }
552                         }
553                         tailfrom = i;
554                         continue;
555                         break;
556
557                         /*
558                          *  Add it, if it's not already there.
559                          */
560                 case T_OP_EQ:           /* = */
561                         if (found) {
562                                 tailfrom = i;
563                                 continue; /* with the loop */
564                         }
565                         break;
566
567                         /*
568                          *  If a similar attribute is found,
569                          *  replace it with the new one.  Otherwise,
570                          *  add the new one to the list.
571                          */
572                 case T_OP_SET:          /* := */
573                         if (found) {
574                                 VALUE_PAIR *vp;
575
576                                 vp = found->next;
577                                 memcpy(found, i, sizeof(*found));
578                                 found->next = vp;
579                                 continue;
580                         }
581                         break;
582
583                         /*
584                          *  FIXME: Add support for <=, >=, <, >
585                          *
586                          *  which will mean (for integers)
587                          *  'make the attribute the smaller, etc'
588                          */
589
590                         /*
591                          *  Add the new element to the list, even
592                          *  if similar ones already exist.
593                          */
594                 default:
595                 case T_OP_ADD:          /* += */
596                         break;
597                 }
598
599                 if (tailfrom)
600                         tailfrom->next = next;
601                 else
602                         *from = next;
603
604                 /*
605                  *      If ALL of the 'to' attributes have been deleted,
606                  *      then ensure that the 'tail' is updated to point
607                  *      to the head.
608                  */
609                 if (!*to) {
610                         tailto = to;
611                 }
612                 *tailto = i;
613                 if (i) {
614                         i->next = NULL;
615                         tailto = &i->next;
616                 }
617         } /* loop over the 'from' list */
618 }