Length of match[i] = end - start, not just end.
[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                         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                         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->strvalue)];
307
308                         check_item->flags.do_xlat = 0;
309                         rcode = radius_xlat(buffer, sizeof(buffer),
310                                             check_item->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 = paircompare(req, auth_item, check_item, check, reply);
323
324                 switch (check_item->operator) {
325                         case T_OP_EQ:
326                         default:
327                                 radlog(L_ERR,  "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[9];
361
362                                 /*
363                                  *      Include substring matches.
364                                  */
365                                 regcomp(&reg, (char *)check_item->strvalue,
366                                         REG_EXTENDED);
367                                 compare = regexec(&reg,
368                                                   (char *)auth_item->strvalue,
369                                                   16, rxmatch, 0);
370                                 regfree(&reg);
371
372                                 /*
373                                  *      Add %{0}, %{1}, etc.
374                                  */
375                                 for (i = 0; i <= 8; i++) {
376                                         char *p;
377                                         char buffer[sizeof(check_item->strvalue)];
378
379                                         /*
380                                          *      Didn't match: delete old
381                                          *      match, if it existed.
382                                          */
383                                         if ((compare != 0) ||
384                                             (rxmatch[i].rm_so == -1)) {
385                                                 p = request_data_get(req, req,
386                                                                      REQUEST_DATA_REGEX | i);
387                                                 if (p) {
388                                                         free(p);
389                                                         continue;
390                                                 }
391
392                                                 /*
393                                                  *      No previous match
394                                                  *      to delete, stop.
395                                                  */
396                                                 break;
397                                         }
398                                         
399                                         /*
400                                          *      Copy substring into buffer.
401                                          */
402                                         memcpy(buffer,
403                                                auth_item->strvalue + rxmatch[i].rm_so,
404                                                rxmatch[i].rm_eo - rxmatch[i].rm_so);
405                                         buffer[rxmatch[i].rm_eo - rxmatch[i].rm_so] = '\0';
406
407                                         /*
408                                          *      Copy substring, and add it to
409                                          *      the request.
410                                          *
411                                          *      Note that we don't check
412                                          *      for out of memory, which is
413                                          *      the only error we can get...
414                                          */
415                                         p = strdup(buffer);
416                                         request_data_add(req,
417                                                          req,
418                                                          REQUEST_DATA_REGEX | i,
419                                                          p, free);
420                                 }
421                         }                               
422                                 if (compare != 0) result = -1;
423                                 break;
424
425                         case T_OP_REG_NE:
426                                 regcomp(&reg, (char *)check_item->strvalue, REG_EXTENDED|REG_NOSUB);
427                                 compare = regexec(&reg, (char *)auth_item->strvalue,
428                                                 0, NULL, 0);
429                                 regfree(&reg);
430                                 if (compare == 0) result = -1;
431                                 break;
432 #endif
433
434                 } /* switch over the operator of the check item */
435
436                 /*
437                  *      This attribute didn't match, but maybe there's
438                  *      another of the same attribute, which DOES match.
439                  */
440                 if (result != 0) {
441                         auth_item = auth_item->next;
442                         result = 0;
443                         goto try_again;
444                 }
445
446         } /* for every entry in the check item list */
447
448         return 0;               /* it matched */
449 }
450
451 /*
452  *      Compare two attributes simply.  Calls paircompare.
453  */
454
455 int simplepaircmp(REQUEST *req, VALUE_PAIR *first, VALUE_PAIR *second)
456 {
457         return paircompare( req, first, second, NULL, NULL );
458 }
459
460
461 /*
462  *      Compare a Connect-Info and a Connect-Rate
463  */
464 static int connectcmp(void *instance,
465                       REQUEST *req UNUSED,
466                       VALUE_PAIR *request,
467                       VALUE_PAIR *check,
468                       VALUE_PAIR *check_pairs,
469                       VALUE_PAIR **reply_pairs)
470 {
471         int rate;
472
473         instance = instance;
474         check_pairs = check_pairs; /* shut the compiler up */
475         reply_pairs = reply_pairs;
476
477         rate = atoi((char *)request->strvalue);
478         return rate - check->lvalue;
479 }
480
481
482 /*
483  *      Compare a portno with a range.
484  */
485 static int portcmp(void *instance,
486                    REQUEST *req UNUSED, VALUE_PAIR *request, VALUE_PAIR *check,
487         VALUE_PAIR *check_pairs, VALUE_PAIR **reply_pairs)
488 {
489         char buf[MAX_STRING_LEN];
490         char *s, *p;
491         uint32_t lo, hi;
492         uint32_t port = request->lvalue;
493
494         instance = instance;
495         check_pairs = check_pairs; /* shut the compiler up */
496         reply_pairs = reply_pairs;
497
498         if ((strchr((char *)check->strvalue, ',') == NULL) &&
499                         (strchr((char *)check->strvalue, '-') == NULL)) {
500                 return (request->lvalue - check->lvalue);
501         }
502
503         /* Same size */
504         strcpy(buf, (char *)check->strvalue);
505         s = strtok(buf, ",");
506
507         while (s != NULL) {
508                 if ((p = strchr(s, '-')) != NULL)
509                         p++;
510                 else
511                         p = s;
512                 lo = strtoul(s, NULL, 10);
513                 hi = strtoul(p, NULL, 10);
514                 if (lo <= port && port <= hi) {
515                         return 0;
516                 }
517                 s = strtok(NULL, ",");
518         }
519
520         return -1;
521 }
522
523 /*
524  *      Compare prefix/suffix.
525  *
526  *      If they compare:
527  *      - if PW_STRIP_USER_NAME is present in check_pairs,
528  *        strip the username of prefix/suffix.
529  *      - if PW_STRIP_USER_NAME is not present in check_pairs,
530  *        add a PW_STRIPPED_USER_NAME to the request.
531  */
532 static int presufcmp(void *instance,
533                      REQUEST *req UNUSED,
534                      VALUE_PAIR *request, VALUE_PAIR *check,
535         VALUE_PAIR *check_pairs, VALUE_PAIR **reply_pairs)
536 {
537         VALUE_PAIR *vp;
538         char *name = (char *)request->strvalue;
539         char rest[MAX_STRING_LEN];
540         int len, namelen;
541         int ret = -1;
542
543         instance = instance;
544         reply_pairs = reply_pairs; /* shut the compiler up */
545
546 #if 0 /* DEBUG */
547         printf("Comparing %s and %s, check->attr is %d\n",
548                 name, check->strvalue, check->attribute);
549 #endif
550
551         len = strlen((char *)check->strvalue);
552         switch (check->attribute) {
553                 case PW_PREFIX:
554                         ret = strncmp(name, (char *)check->strvalue, len);
555                         if (ret == 0 && rest)
556                                 strcpy(rest, name + len);
557                         break;
558                 case PW_SUFFIX:
559                         namelen = strlen(name);
560                         if (namelen < len)
561                                 break;
562                         ret = strcmp(name + namelen - len,
563                                         (char *)check->strvalue);
564                         if (ret == 0 && rest) {
565                                 strNcpy(rest, name, namelen - len + 1);
566                         }
567                         break;
568         }
569         if (ret != 0)
570                 return ret;
571
572         if (pairfind(check_pairs, PW_STRIP_USER_NAME)) {
573                 /*
574                  *      I don't think we want to update the User-Name
575                  *      attribute in place... - atd
576                  */
577                 strcpy((char *)request->strvalue, rest);
578                 request->length = strlen(rest);
579         } else {
580                 if ((vp = pairfind(check_pairs, PW_STRIPPED_USER_NAME)) != NULL){
581                         strcpy((char *)vp->strvalue, rest);
582                         vp->length = strlen(rest);
583                 } else if ((vp = paircreate(PW_STRIPPED_USER_NAME,
584                                 PW_TYPE_STRING)) != NULL) {
585                         strcpy((char *)vp->strvalue, rest);
586                         vp->length = strlen(rest);
587                         pairadd(&request, vp);
588                 } /* else no memory! Die, die!: FIXME!! */
589         }
590
591         return ret;
592 }
593
594
595 /*
596  *      Compare the current time to a range.
597  */
598 static int timecmp(void *instance,
599                    REQUEST *req UNUSED,
600                    VALUE_PAIR *request, VALUE_PAIR *check,
601         VALUE_PAIR *check_pairs, VALUE_PAIR **reply_pairs)
602 {
603         instance = instance;
604         request = request;      /* shut the compiler up */
605         check_pairs = check_pairs;
606         reply_pairs = reply_pairs;
607
608         if (timestr_match((char *)check->strvalue,
609                           req ? req->timestamp : time(NULL)) >= 0) {
610                 return 0;
611         }
612         return -1;
613 }
614
615 /*
616  *      Matches if there is NO SUCH ATTRIBUTE as the one named
617  *      in check->strvalue.  If there IS such an attribute, it
618  *      doesn't match.
619  *
620  *      This is ugly, and definitely non-optimal.  We should be
621  *      doing the lookup only ONCE, and storing the result
622  *      in check->lvalue...
623  */
624 static int attrcmp(void *instance,
625                    REQUEST *req UNUSED,
626                    VALUE_PAIR *request, VALUE_PAIR *check,
627                    VALUE_PAIR *check_pairs, VALUE_PAIR **reply_pairs)
628 {
629         VALUE_PAIR *pair;
630         DICT_ATTR  *dict;
631         int attr;
632
633         instance = instance;
634         check_pairs = check_pairs; /* shut the compiler up */
635         reply_pairs = reply_pairs;
636
637         if (check->lvalue == 0) {
638                 dict = dict_attrbyname((char *)check->strvalue);
639                 if (dict == NULL) {
640                         return -1;
641                 }
642                 attr = dict->attr;
643         } else {
644                 attr = check->lvalue;
645         }
646
647         /*
648          *      If there's no such attribute, then return MATCH,
649          *      else FAILURE.
650          */
651         pair = pairfind(request, attr);
652         if (pair == NULL) {
653                 return 0;
654         }
655
656         return -1;
657 }
658
659 /*
660  *      Compare the expiration date.
661  */
662 static int expirecmp(void *instance, REQUEST *req UNUSED,
663                      VALUE_PAIR *request, VALUE_PAIR *check,
664                      VALUE_PAIR *check_pairs, VALUE_PAIR **reply_pairs)
665 {
666         time_t now;
667
668         instance = instance;
669         request = request;      /* shut the compiler up */
670         check_pairs = check_pairs;
671         reply_pairs = reply_pairs;
672
673         /*
674          *  FIXME!  This should be request->timestamp!
675          */
676         now = time(NULL);
677
678         if (now <= (signed)check->lvalue) {
679                 return 0;
680         }
681
682         return +1;
683 }
684
685 /*
686  *      Compare the request packet type.
687  */
688 static int packetcmp(void *instance UNUSED, REQUEST *req,
689                      VALUE_PAIR *request UNUSED,
690                      VALUE_PAIR *check,
691                      VALUE_PAIR *check_pairs UNUSED,
692                      VALUE_PAIR **reply_pairs UNUSED)
693 {
694         if (req->packet->code == check->lvalue) {
695                 return 0;
696         }
697
698         return 1;
699 }
700
701 /*
702  *      Compare the response packet type.
703  */
704 static int responsecmp(void *instance UNUSED,
705                        REQUEST *req,
706                        VALUE_PAIR *request UNUSED,
707                        VALUE_PAIR *check,
708                        VALUE_PAIR *check_pairs UNUSED,
709                        VALUE_PAIR **reply_pairs UNUSED)
710 {
711         if (req->reply->code == check->lvalue) {
712                 return 0;
713         }
714
715         return 1;
716 }
717
718 /*
719  *      Register server-builtin special attributes.
720  */
721 void pair_builtincompare_init(void)
722 {
723         paircompare_register(PW_NAS_PORT, -1, portcmp, NULL);
724         paircompare_register(PW_PREFIX, PW_USER_NAME, presufcmp, NULL);
725         paircompare_register(PW_SUFFIX, PW_USER_NAME, presufcmp, NULL);
726         paircompare_register(PW_CONNECT_RATE, PW_CONNECT_INFO, connectcmp, NULL);
727         paircompare_register(PW_CURRENT_TIME, 0, timecmp, NULL);
728         paircompare_register(PW_NO_SUCH_ATTRIBUTE, 0, attrcmp, NULL);
729         paircompare_register(PW_EXPIRATION, 0, expirecmp, NULL);
730         paircompare_register(PW_PACKET_TYPE, 0, packetcmp, NULL);
731         paircompare_register(PW_RESPONSE_PACKET_TYPE, 0, responsecmp, NULL);
732 }
733
734 /*
735  *      Move pairs, replacing/over-writing them, and doing xlat.
736  */
737 /*
738  *      Move attributes from one list to the other
739  *      if not already present.
740  */
741 void pairxlatmove(REQUEST *req, VALUE_PAIR **to, VALUE_PAIR **from)
742 {
743         VALUE_PAIR **tailto, *i, *j, *next;
744         VALUE_PAIR *tailfrom = NULL;
745         VALUE_PAIR *found;
746
747         /*
748          *      Point "tailto" to the end of the "to" list.
749          */
750         tailto = to;
751         for(i = *to; i; i = i->next) {
752                 tailto = &i->next;
753         }
754
755         /*
756          *      Loop over the "from" list.
757          */
758         for(i = *from; i; i = next) {
759                 next = i->next;
760
761                 /*
762                  *      Don't move 'fallthrough' over.
763                  */
764                 if (i->attribute == PW_FALL_THROUGH) {
765                         continue;
766                 }
767
768                 /*
769                  *      We've got to xlat the string before moving
770                  *      it over.
771                  */
772                 if (i->flags.do_xlat) {
773                         int rcode;
774                         char buffer[sizeof(i->strvalue)];
775
776                         i->flags.do_xlat = 0;
777                         rcode = radius_xlat(buffer, sizeof(buffer),
778                                             i->strvalue,
779                                             req, NULL);
780
781                         /*
782                          *      Parse the string into a new value.
783                          */
784                         pairparsevalue(i, buffer);
785                 }
786
787                 found = pairfind(*to, i->attribute);
788                 switch (i->operator) {
789
790                         /*
791                          *  If a similar attribute is found,
792                          *  delete it.
793                          */
794                 case T_OP_SUB:          /* -= */
795                         if (found) {
796                                 if (!i->strvalue[0] ||
797                                     (strcmp((char *)found->strvalue,
798                                             (char *)i->strvalue) == 0)){
799                                         pairdelete(to, found->attribute);
800
801                                         /*
802                                          *      'tailto' may have been
803                                          *      deleted...
804                                          */
805                                         tailto = to;
806                                         for(j = *to; j; j = j->next) {
807                                                 tailto = &j->next;
808                                         }
809                                 }
810                         }
811                         tailfrom = i;
812                         continue;
813                         break;
814
815                         /*
816                          *  Add it, if it's not already there.
817                          */
818                 case T_OP_EQ:           /* = */
819                         if (found) {
820                                 tailfrom = i;
821                                 continue; /* with the loop */
822                         }
823                         break;
824
825                         /*
826                          *  If a similar attribute is found,
827                          *  replace it with the new one.  Otherwise,
828                          *  add the new one to the list.
829                          */
830                 case T_OP_SET:          /* := */
831                         if (found) {
832                                 VALUE_PAIR *vp;
833
834                                 vp = found->next;
835                                 memcpy(found, i, sizeof(*found));
836                                 found->next = vp;
837                                 continue;
838                         }
839                         break;
840
841                         /*
842                          *  FIXME: Add support for <=, >=, <, >
843                          *
844                          *  which will mean (for integers)
845                          *  'make the attribute the smaller, etc'
846                          */
847
848                         /*
849                          *  Add the new element to the list, even
850                          *  if similar ones already exist.
851                          */
852                 default:
853                 case T_OP_ADD:          /* += */
854                         break;
855                 }
856
857                 if (tailfrom)
858                         tailfrom->next = next;
859                 else
860                         *from = next;
861
862                 /*
863                  *      If ALL of the 'to' attributes have been deleted,
864                  *      then ensure that the 'tail' is updated to point
865                  *      to the head.
866                  */
867                 if (!*to) {
868                         tailto = to;
869                 }
870                 *tailto = i;
871                 if (i) {
872                         i->next = NULL;
873                         tailto = &i->next;
874                 }
875         } /* loop over the 'from' list */
876 }