Remove 'caseless' from VALUE_PAIR flags. It's not needed.
[freeradius.git] / src / lib / valuepair.c
1 /*
2  * valuepair.c  Functions to handle VALUE_PAIRs
3  *
4  * Version:     $Id$
5  *
6  *   This library is free software; you can redistribute it and/or
7  *   modify it under the terms of the GNU Lesser General Public
8  *   License as published by the Free Software Foundation; either
9  *   version 2.1 of the License, or (at your option) any later version.
10  *
11  *   This library is distributed in the hope that it will be useful,
12  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  *   Lesser General Public License for more details.
15  *
16  *   You should have received a copy of the GNU Lesser General Public
17  *   License along with this library; if not, write to the Free Software
18  *   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  *
20  * Copyright 2000,2006  The FreeRADIUS server project
21  */
22
23 #include <freeradius-devel/ident.h>
24 RCSID("$Id$")
25
26 #include        <freeradius-devel/libradius.h>
27
28 #include        <ctype.h>
29
30 #ifdef HAVE_MALLOC_H
31 #  include      <malloc.h>
32 #endif
33
34 #ifdef HAVE_REGEX_H
35 #  include      <regex.h>
36 #endif
37
38 static const char *months[] = {
39         "jan", "feb", "mar", "apr", "may", "jun",
40         "jul", "aug", "sep", "oct", "nov", "dec" };
41
42 /*
43  *      This padding is necessary only for attributes that are NOT
44  *      in the dictionary, and then only because the rest of the
45  *      code accesses vp->name directly, rather than through an
46  *      accessor function.
47  *
48  *      The name padding only has to large enough for:
49  *
50  *              Vendor-65535-Attr-65535
51  *
52  *      i.e. 23 characters, plus a zero.  We add another 8 bytes for
53  *      padding, because the VALUE_PAIR structure may be un-aligned.
54  *
55  *      The result is that for the normal case, the server uses a less
56  *      memory (36 bytes * number of VALUE_PAIRs).
57  */
58 #define FR_VP_NAME_PAD (32)
59 #define FR_VP_NAME_LEN (24)
60
61 VALUE_PAIR *pairalloc(DICT_ATTR *da)
62 {
63         size_t name_len = 0;
64         VALUE_PAIR *vp;
65
66         /*
67          *      Not in the dictionary: the name is allocated AFTER
68          *      the VALUE_PAIR struct.
69          */
70         if (!da) name_len = FR_VP_NAME_PAD;
71
72         vp = malloc(sizeof(*vp) + name_len);
73         if (!vp) return NULL;
74         memset(vp, 0, sizeof(*vp));
75
76         if (da) {
77                 vp->attribute = da->attr;
78                 vp->vendor = da->vendor;
79                 vp->type = da->type;
80                 vp->name = da->name;
81                 vp->flags = da->flags;
82         } else {
83                 vp->attribute = 0;
84                 vp->vendor = 0;
85                 vp->type = PW_TYPE_OCTETS;
86                 vp->name = NULL;
87                 memset(&vp->flags, 0, sizeof(vp->flags));
88                 vp->flags.unknown_attr = 1;
89         }
90
91         switch (vp->type) {
92                 case PW_TYPE_BYTE:
93                         vp->length = 1;
94                         break;
95
96                 case PW_TYPE_SHORT:
97                         vp->length = 2;
98                         break;
99
100                 case PW_TYPE_INTEGER:
101                 case PW_TYPE_IPADDR:
102                 case PW_TYPE_DATE:
103                         vp->length = 4;
104                         break;
105
106                 case PW_TYPE_IFID:
107                         vp->length = sizeof(vp->vp_ifid);
108                         break;
109
110                 case PW_TYPE_IPV6ADDR:
111                         vp->length = sizeof(vp->vp_ipv6addr);
112                         break;
113
114                 case PW_TYPE_IPV6PREFIX:
115                         vp->length = sizeof(vp->vp_ipv6prefix);
116                         break;
117
118                 case PW_TYPE_ETHERNET:
119                         vp->length = sizeof(vp->vp_ether);
120                         break;
121
122                 default:
123                         vp->length = 0;
124                         break;
125         }
126
127         return vp;
128 }
129
130
131 /*
132  *      Create a new valuepair.
133  */
134 VALUE_PAIR *paircreate(int attr, int type)
135 {
136         VALUE_PAIR      *vp;
137         DICT_ATTR       *da;
138
139         da = dict_attrbyvalue(attr);
140         if ((vp = pairalloc(da)) == NULL) {
141                 librad_log("out of memory");
142                 return NULL;
143         }
144         vp->operator = T_OP_EQ;
145
146         /*
147          *      It isn't in the dictionary: update the name.
148          */
149         if (!da) {
150                 size_t len = 0;
151                 char *p = (char *) (vp + 1);
152                 
153                 vp->vendor = VENDOR(attr);
154                 vp->attribute = attr;
155                 vp->name = p;
156                 vp->type = type; /* be forgiving */
157
158                 if (vp->vendor) {
159                         DICT_VENDOR *v;
160
161                         v = dict_vendorbyvalue(vp->vendor);
162                         if (v) {
163                                 snprintf(p, FR_VP_NAME_LEN, "%s-", v->name);
164                         } else {
165                                 snprintf(p, FR_VP_NAME_LEN, "Vendor-%u-", vp->vendor);
166                         }
167
168                         len = strlen(p);
169                         if (len == FR_VP_NAME_LEN) {
170                                 free(vp);
171                                 return NULL;
172                         }
173                 }
174                 
175                 snprintf(p + len, FR_VP_NAME_LEN - len, "Attr-%u",
176                          attr & 0xffff);
177                 len += strlen(p + len);
178                 if (len == FR_VP_NAME_LEN) {
179                         free(vp);
180                         return NULL;
181                 }
182         }
183
184         return vp;
185 }
186
187 /*
188  *      release the memory used by a single attribute-value pair
189  *      just a wrapper around free() for now.
190  */
191 void pairbasicfree(VALUE_PAIR *pair)
192 {
193         /* clear the memory here */
194         memset(pair, 0, sizeof(*pair));
195         free(pair);
196 }
197
198 /*
199  *      Release the memory used by a list of attribute-value
200  *      pairs, and sets the pair pointer to NULL.
201  */
202 void pairfree(VALUE_PAIR **pair_ptr)
203 {
204         VALUE_PAIR      *next, *pair;
205
206         if (!pair_ptr) return;
207         pair = *pair_ptr;
208
209         while (pair != NULL) {
210                 next = pair->next;
211                 pairbasicfree(pair);
212                 pair = next;
213         }
214
215         *pair_ptr = NULL;
216 }
217
218
219 /*
220  *      Find the pair with the matching attribute
221  */
222 VALUE_PAIR * pairfind(VALUE_PAIR *first, int attr)
223 {
224         while(first && first->attribute != attr)
225                 first = first->next;
226         return first;
227 }
228
229
230 /*
231  *      Delete the pair(s) with the matching attribute
232  */
233 void pairdelete(VALUE_PAIR **first, int attr)
234 {
235         VALUE_PAIR *i, *next;
236         VALUE_PAIR **last = first;
237
238         for(i = *first; i; i = next) {
239                 next = i->next;
240                 if (i->attribute == attr) {
241                         *last = next;
242                         pairbasicfree(i);
243                 } else {
244                         last = &i->next;
245                 }
246         }
247 }
248
249 /*
250  *      Add a pair at the end of a VALUE_PAIR list.
251  */
252 void pairadd(VALUE_PAIR **first, VALUE_PAIR *add)
253 {
254         VALUE_PAIR *i;
255
256         if (!add) return;
257
258         if (*first == NULL) {
259                 *first = add;
260                 return;
261         }
262         for(i = *first; i->next; i = i->next)
263                 ;
264         i->next = add;
265 }
266
267 /*
268  *      Add or replace a pair at the end of a VALUE_PAIR list.
269  */
270 void pairreplace(VALUE_PAIR **first, VALUE_PAIR *replace)
271 {
272         VALUE_PAIR *i, *next;
273         VALUE_PAIR **prev = first;
274
275         if (*first == NULL) {
276                 *first = replace;
277                 return;
278         }
279
280         /*
281          *      Not an empty list, so find item if it is there, and
282          *      replace it. Note, we always replace the first one, and
283          *      we ignore any others that might exist.
284          */
285         for(i = *first; i; i = next) {
286                 next = i->next;
287
288                 /*
289                  *      Found the first attribute, replace it,
290                  *      and return.
291                  */
292                 if (i->attribute == replace->attribute) {
293                         *prev = replace;
294
295                         /*
296                          *      Should really assert that replace->next == NULL
297                          */
298                         replace->next = next;
299                         pairbasicfree(i);
300                         return;
301                 }
302
303                 /*
304                  *      Point to where the attribute should go.
305                  */
306                 prev = &i->next;
307         }
308
309         /*
310          *      If we got here, we didn't find anything to replace, so
311          *      stopped at the last item, which we just append to.
312          */
313         *prev = replace;
314 }
315
316 /*
317  *      Copy just a certain type of pairs.
318  */
319 VALUE_PAIR *paircopy2(VALUE_PAIR *vp, int attr)
320 {
321         VALUE_PAIR      *first, *n, **last;
322
323         first = NULL;
324         last = &first;
325
326         while (vp) {
327                 size_t name_len;
328
329                 if (attr >= 0 && vp->attribute != attr) {
330                         vp = vp->next;
331                         continue;
332                 }
333
334                 if (!vp->flags.unknown_attr) {
335                         name_len = 0;
336                 } else {
337                         name_len = FR_VP_NAME_PAD;
338                 }
339                 
340                 if ((n = malloc(sizeof(*n))) == NULL) {
341                         librad_log("out of memory");
342                         return first;
343                 }
344                 memcpy(n, vp, sizeof(*n) + name_len);
345                 n->next = NULL;
346                 *last = n;
347                 last = &n->next;
348                 vp = vp->next;
349         }
350         return first;
351 }
352
353
354 /*
355  *      Copy a pairlist.
356  */
357 VALUE_PAIR *paircopy(VALUE_PAIR *vp)
358 {
359         return paircopy2(vp, -1);
360 }
361
362
363 /*
364  *      Move attributes from one list to the other
365  *      if not already present.
366  */
367 void pairmove(VALUE_PAIR **to, VALUE_PAIR **from)
368 {
369         VALUE_PAIR **tailto, *i, *j, *next;
370         VALUE_PAIR *tailfrom = NULL;
371         VALUE_PAIR *found;
372         int has_password = 0;
373
374         /*
375          *      First, see if there are any passwords here, and
376          *      point "tailto" to the end of the "to" list.
377          */
378         tailto = to;
379         for(i = *to; i; i = i->next) {
380                 if (i->attribute == PW_USER_PASSWORD ||
381                     i->attribute == PW_CRYPT_PASSWORD)
382                         has_password = 1;
383                 tailto = &i->next;
384         }
385
386         /*
387          *      Loop over the "from" list.
388          */
389         for(i = *from; i; i = next) {
390                 next = i->next;
391
392                 /*
393                  *      If there was a password in the "to" list,
394                  *      do not move any other password from the
395                  *      "from" to the "to" list.
396                  */
397                 if (has_password &&
398                     (i->attribute == PW_USER_PASSWORD ||
399                      i->attribute == PW_CRYPT_PASSWORD)) {
400                         tailfrom = i;
401                         continue;
402                 }
403
404                 switch (i->operator) {
405                         /*
406                          *      These are COMPARISON attributes
407                          *      from a check list, and are not
408                          *      supposed to be copied!
409                          */
410                         case T_OP_NE:
411                         case T_OP_GE:
412                         case T_OP_GT:
413                         case T_OP_LE:
414                         case T_OP_LT:
415                         case T_OP_CMP_TRUE:
416                         case T_OP_CMP_FALSE:
417                         case T_OP_CMP_EQ:
418                                 tailfrom = i;
419                                 continue;
420
421                         default:
422                                 break;
423                 }
424
425                 /*
426                  *      If the attribute is already present in "to",
427                  *      do not move it from "from" to "to". We make
428                  *      an exception for "Hint" which can appear multiple
429                  *      times, and we never move "Fall-Through".
430                  */
431                 if (i->attribute == PW_FALL_THROUGH ||
432                     (i->attribute != PW_HINT && i->attribute != PW_FRAMED_ROUTE)) {
433
434                         found = pairfind(*to, i->attribute);
435                         switch (i->operator) {
436
437                           /*
438                            *    If matching attributes are found,
439                            *    delete them.
440                            */
441                         case T_OP_SUB:          /* -= */
442                                 if (found) {
443                                         if (!i->vp_strvalue[0] ||
444                                             (strcmp((char *)found->vp_strvalue,
445                                                     (char *)i->vp_strvalue) == 0)){
446                                                 pairdelete(to, found->attribute);
447
448                                                 /*
449                                                  *      'tailto' may have been
450                                                  *      deleted...
451                                                  */
452                                                 tailto = to;
453                                                 for(j = *to; j; j = j->next) {
454                                                         tailto = &j->next;
455                                                 }
456                                         }
457                                 }
458                                 tailfrom = i;
459                                 continue;
460                                 break;
461
462 /* really HAVE_REGEX_H */
463 #if 0
464                                 /*
465                                  *  Attr-Name =~ "s/find/replace/"
466                                  *
467                                  *  Very bad code.  Barely working,
468                                  *  if at all.
469                                  */
470
471                         case T_OP_REG_EQ:
472                           if (found &&
473                               (i->vp_strvalue[0] == 's')) {
474                             regex_t             reg;
475                             regmatch_t          match[1];
476
477                             char *str;
478                             char *p, *q;
479
480                             p = i->vp_strvalue + 1;
481                             q = strchr(p + 1, *p);
482                             if (!q || (q[strlen(q) - 1] != *p)) {
483                               tailfrom = i;
484                               continue;
485                             }
486                             str = strdup(i->vp_strvalue + 2);
487                             q = strchr(str, *p);
488                             *(q++) = '\0';
489                             q[strlen(q) - 1] = '\0';
490
491                             regcomp(&reg, str, 0);
492                             if (regexec(&reg, found->vp_strvalue,
493                                         1, match, 0) == 0) {
494                               fprintf(stderr, "\"%s\" will have %d to %d replaced with %s\n",
495                                       found->vp_strvalue, match[0].rm_so,
496                                       match[0].rm_eo, q);
497
498                             }
499                             regfree(&reg);
500                             free(str);
501                           }
502                           tailfrom = i; /* don't copy it over */
503                           continue;
504                           break;
505 #endif
506                         case T_OP_EQ:           /* = */
507                                 /*
508                                  *  FIXME: Tunnel attributes with
509                                  *  different tags are different
510                                  *  attributes.
511                                  */
512                                 if (found) {
513                                         tailfrom = i;
514                                         continue; /* with the loop */
515                                 }
516                                 break;
517
518                           /*
519                            *  If a similar attribute is found,
520                            *  replace it with the new one.  Otherwise,
521                            *  add the new one to the list.
522                            */
523                         case T_OP_SET:          /* := */
524                                 if (found) {
525                                         VALUE_PAIR *mynext = found->next;
526
527                                         /*
528                                          *      Do NOT call pairdelete()
529                                          *      here, due to issues with
530                                          *      re-writing "request->username".
531                                          *
532                                          *      Everybody calls pairmove,
533                                          *      and expects it to work.
534                                          *      We can't update request->username
535                                          *      here, so instead we over-write
536                                          *      the vp that it's pointing to.
537                                          */
538                                         memcpy(found, i, sizeof(*found));
539                                         found->next = mynext;
540
541                                         pairdelete(&found->next, found->attribute);
542
543                                         /*
544                                          *      'tailto' may have been
545                                          *      deleted...
546                                          */
547                                         for(j = found; j; j = j->next) {
548                                                 tailto = &j->next;
549                                         }
550                                         continue;
551                                 }
552                                 break;
553
554                           /*
555                            *  Add the new element to the list, even
556                            *  if similar ones already exist.
557                            */
558                         default:
559                         case T_OP_ADD: /* += */
560                                 break;
561                         }
562                 }
563                 if (tailfrom)
564                         tailfrom->next = next;
565                 else
566                         *from = next;
567
568                 /*
569                  *      If ALL of the 'to' attributes have been deleted,
570                  *      then ensure that the 'tail' is updated to point
571                  *      to the head.
572                  */
573                 if (!*to) {
574                         tailto = to;
575                 }
576                 *tailto = i;
577                 if (i) {
578                         i->next = NULL;
579                         tailto = &i->next;
580                 }
581         }
582 }
583
584 /*
585  *      Move one kind of attributes from one list to the other
586  */
587 void pairmove2(VALUE_PAIR **to, VALUE_PAIR **from, int attr)
588 {
589         VALUE_PAIR *to_tail, *i, *next;
590         VALUE_PAIR *iprev = NULL;
591
592         /*
593          *      Find the last pair in the "to" list and put it in "to_tail".
594          */
595         if (*to != NULL) {
596                 to_tail = *to;
597                 for(i = *to; i; i = i->next)
598                         to_tail = i;
599         } else
600                 to_tail = NULL;
601
602         for(i = *from; i; i = next) {
603                 next = i->next;
604
605
606                 /*
607                  *      If the attribute to move is NOT a VSA, then it
608                  *      ignores any attributes which do not match exactly.
609                  */
610                 if ((attr != PW_VENDOR_SPECIFIC) &&
611                     (i->attribute != attr)) {
612                         iprev = i;
613                         continue;
614                 }
615
616                 /*
617                  *      If the attribute to move IS a VSA, then it ignores
618                  *      any non-VSA attribute.
619                  */
620                 if ((attr == PW_VENDOR_SPECIFIC) &&
621                     (VENDOR(i->attribute) == 0)) {
622                         iprev = i;
623                         continue;
624                 }
625
626                 /*
627                  *      Remove the attribute from the "from" list.
628                  */
629                 if (iprev)
630                         iprev->next = next;
631                 else
632                         *from = next;
633
634                 /*
635                  *      Add the attribute to the "to" list.
636                  */
637                 if (to_tail)
638                         to_tail->next = i;
639                 else
640                         *to = i;
641                 to_tail = i;
642                 i->next = NULL;
643         }
644 }
645
646
647 /*
648  *      Sort of strtok/strsep function.
649  */
650 static char *mystrtok(char **ptr, const char *sep)
651 {
652         char    *res;
653
654         if (**ptr == 0)
655                 return NULL;
656         while (**ptr && strchr(sep, **ptr))
657                 (*ptr)++;
658         if (**ptr == 0)
659                 return NULL;
660         res = *ptr;
661         while (**ptr && strchr(sep, **ptr) == NULL)
662                 (*ptr)++;
663         if (**ptr != 0)
664                 *(*ptr)++ = 0;
665         return res;
666 }
667
668 /*
669  *      Turn printable string into time_t
670  *      Returns -1 on error, 0 on OK.
671  */
672 static int gettime(const char *valstr, time_t *date)
673 {
674         int             i;
675         time_t          t;
676         struct tm       *tm, s_tm;
677         char            buf[64];
678         char            *p;
679         char            *f[4];
680         char            *tail = '\0';
681
682         /*
683          * Test for unix timestamp date
684          */
685         *date = strtoul(valstr, &tail, 10);
686         if (*tail == '\0') {
687                 return 0;
688         }
689
690         tm = &s_tm;
691         memset(tm, 0, sizeof(*tm));
692         tm->tm_isdst = -1;      /* don't know, and don't care about DST */
693
694         strlcpy(buf, valstr, sizeof(buf));
695
696         p = buf;
697         f[0] = mystrtok(&p, " \t");
698         f[1] = mystrtok(&p, " \t");
699         f[2] = mystrtok(&p, " \t");
700         f[3] = mystrtok(&p, " \t"); /* may, or may not, be present */
701         if (!f[0] || !f[1] || !f[2]) return -1;
702
703         /*
704          *      The time has a colon, where nothing else does.
705          *      So if we find it, bubble it to the back of the list.
706          */
707         if (f[3]) {
708                 for (i = 0; i < 3; i++) {
709                         if (strchr(f[i], ':')) {
710                                 p = f[3];
711                                 f[3] = f[i];
712                                 f[i] = p;
713                                 break;
714                         }
715                 }
716         }
717
718         /*
719          *  The month is text, which allows us to find it easily.
720          */
721         tm->tm_mon = 12;
722         for (i = 0; i < 3; i++) {
723                 if (isalpha( (int) *f[i])) {
724                         /*
725                          *  Bubble the month to the front of the list
726                          */
727                         p = f[0];
728                         f[0] = f[i];
729                         f[i] = p;
730
731                         for (i = 0; i < 12; i++) {
732                                 if (strncasecmp(months[i], f[0], 3) == 0) {
733                                         tm->tm_mon = i;
734                                         break;
735                                 }
736                         }
737                 }
738         }
739
740         /* month not found? */
741         if (tm->tm_mon == 12) return -1;
742
743         /*
744          *  The year may be in f[1], or in f[2]
745          */
746         tm->tm_year = atoi(f[1]);
747         tm->tm_mday = atoi(f[2]);
748
749         if (tm->tm_year >= 1900) {
750                 tm->tm_year -= 1900;
751
752         } else {
753                 /*
754                  *  We can't use 2-digit years any more, they make it
755                  *  impossible to tell what's the day, and what's the year.
756                  */
757                 if (tm->tm_mday < 1900) return -1;
758
759                 /*
760                  *  Swap the year and the day.
761                  */
762                 i = tm->tm_year;
763                 tm->tm_year = tm->tm_mday - 1900;
764                 tm->tm_mday = i;
765         }
766
767         /*
768          *  If the day is out of range, die.
769          */
770         if ((tm->tm_mday < 1) || (tm->tm_mday > 31)) {
771                 return -1;
772         }
773
774         /*
775          *      There may be %H:%M:%S.  Parse it in a hacky way.
776          */
777         if (f[3]) {
778                 f[0] = f[3];    /* HH */
779                 f[1] = strchr(f[0], ':'); /* find : separator */
780                 if (!f[1]) return -1;
781
782                 *(f[1]++) = '\0'; /* nuke it, and point to MM:SS */
783
784                 f[2] = strchr(f[1], ':'); /* find : separator */
785                 if (f[2]) {
786                   *(f[2]++) = '\0';     /* nuke it, and point to SS */
787                 } else {
788                   strcpy(f[2], "0");    /* assignment would discard const */
789                 }
790
791                 tm->tm_hour = atoi(f[0]);
792                 tm->tm_min = atoi(f[1]);
793                 tm->tm_sec = atoi(f[2]);
794         }
795
796         /*
797          *  Returns -1 on error.
798          */
799         t = mktime(tm);
800         if (t == (time_t) -1) return -1;
801
802         *date = t;
803
804         return 0;
805 }
806
807 static const char *hextab = "0123456789abcdef";
808
809 /*
810  *  Parse a string value into a given VALUE_PAIR
811  *
812  *  FIXME: we probably want to fix this function to accept
813  *  octets as values for any type of attribute.  We should then
814  *  double-check the parsed value, to be sure it's legal for that
815  *  type (length, etc.)
816  */
817 VALUE_PAIR *pairparsevalue(VALUE_PAIR *vp, const char *value)
818 {
819         char            *p, *s=0;
820         const char      *cp, *cs;
821         int             x;
822         size_t          length;
823         DICT_VALUE      *dval;
824
825         /*
826          *      Even for integers, dates and ip addresses we
827          *      keep the original string in vp->vp_strvalue.
828          */
829         strlcpy(vp->vp_strvalue, value, sizeof(vp->vp_strvalue));
830         vp->length = strlen(vp->vp_strvalue);
831
832         switch(vp->type) {
833                 case PW_TYPE_STRING:
834                         /*
835                          *      Do escaping here
836                          */
837                         p = vp->vp_strvalue;
838                         cp = value;
839                         length = 0;
840
841                         while (*cp && (length < sizeof(vp->vp_strvalue))) {
842                                 char c = *cp++;
843
844                                 if (c == '\\') {
845                                         switch (*cp) {
846                                         case 'r':
847                                                 c = '\r';
848                                                 cp++;
849                                                 break;
850                                         case 'n':
851                                                 c = '\n';
852                                                 cp++;
853                                                 break;
854                                         case 't':
855                                                 c = '\t';
856                                                 cp++;
857                                                 break;
858                                         case '"':
859                                                 c = '"';
860                                                 cp++;
861                                                 break;
862                                         case '\'':
863                                                 c = '\'';
864                                                 cp++;
865                                                 break;
866                                         case '`':
867                                                 c = '`';
868                                                 cp++;
869                                                 break;
870                                         case '\0':
871                                                 c = '\\'; /* no cp++ */
872                                                 break;
873                                         default:
874                                                 if ((cp[0] >= '0') &&
875                                                     (cp[0] <= '9') &&
876                                                     (cp[1] >= '0') &&
877                                                     (cp[1] <= '9') &&
878                                                     (cp[2] >= '0') &&
879                                                     (cp[2] <= '9') &&
880                                                     (sscanf(cp, "%3o", &x) == 1)) {
881                                                         c = x;
882                                                         cp += 3;
883                                                 } /* else just do '\\' */
884                                         }
885                                 }
886                                 *p++ = c;
887                                 length++;
888                         }
889                         vp->length = length;
890                         break;
891
892                 case PW_TYPE_IPADDR:
893                         /*
894                          *      It's a comparison, not a real IP.
895                          */
896                         if ((vp->operator == T_OP_REG_EQ) ||
897                             (vp->operator == T_OP_REG_NE)) {
898                                 break;
899                         }
900
901                         /*
902                          *      FIXME: complain if hostname
903                          *      cannot be resolved, or resolve later!
904                          */
905                         if ((p = strrchr(value, '+')) != NULL && !p[1]) {
906                                 cs = s = strdup(value);
907                                 p = strrchr(s, '+');
908                                 *p = 0;
909                                 vp->flags.addport = 1;
910                         } else {
911                                 p = NULL;
912                                 cs = value;
913                         }
914
915                         {
916                                 fr_ipaddr_t ipaddr;
917
918                                 if (ip_hton(cs, AF_INET, &ipaddr) < 0) {
919                                         librad_log("Failed to find IP address for %s", cs);
920                                         return NULL;
921                                 }
922
923                                 vp->vp_ipaddr = ipaddr.ipaddr.ip4addr.s_addr;
924                         }
925                         if (s) free(s);
926                         vp->length = 4;
927                         break;
928
929                 case PW_TYPE_BYTE:
930                         if ((value[0] == '0') && (value[1] == 'x')) {
931                                 goto do_octets;
932                         }
933
934                         /*
935                          *      Note that ALL integers are unsigned!
936                          */
937                         vp->vp_integer = (uint32_t) strtoul(value, &p, 10);
938                         if (!*p) {
939                                 if (vp->vp_integer > 255) {
940                                         librad_log("Byte value \"%s\" is larger than 255", value);
941                                         return NULL;
942                                 }
943                                 vp->length = 1;
944                                 break;
945                         }
946
947                         /*
948                          *      Look for the named value for the given
949                          *      attribute.
950                          */
951                         if ((dval = dict_valbyname(vp->attribute, value)) == NULL) {
952                                 librad_log("Unknown value %s for attribute %s",
953                                            value, vp->name);
954                                 return NULL;
955                         }
956                         vp->vp_integer = dval->value;
957                         vp->length = 1;
958                         break;
959
960                 case PW_TYPE_SHORT:
961                         /*
962                          *      Note that ALL integers are unsigned!
963                          */
964                         vp->vp_integer = (uint32_t) strtoul(value, &p, 10);
965                         if (!*p) {
966                                 if (vp->vp_integer > 65535) {
967                                         librad_log("Byte value \"%s\" is larger than 65535", value);
968                                         return NULL;
969                                 }
970                                 vp->length = 2;
971                                 break;
972                         }
973
974                         /*
975                          *      Look for the named value for the given
976                          *      attribute.
977                          */
978                         if ((dval = dict_valbyname(vp->attribute, value)) == NULL) {
979                                 librad_log("Unknown value %s for attribute %s",
980                                            value, vp->name);
981                                 return NULL;
982                         }
983                         vp->vp_integer = dval->value;
984                         vp->length = 2;
985                         break;
986
987                 case PW_TYPE_INTEGER:
988                         /*
989                          *      Note that ALL integers are unsigned!
990                          */
991                         vp->vp_integer = (uint32_t) strtoul(value, &p, 10);
992                         if (!*p) {
993                                 vp->length = 4;
994                                 break;
995                         }
996
997                         /*
998                          *      Look for the named value for the given
999                          *      attribute.
1000                          */
1001                         if ((dval = dict_valbyname(vp->attribute, value)) == NULL) {
1002                                 librad_log("Unknown value %s for attribute %s",
1003                                            value, vp->name);
1004                                 return NULL;
1005                         }
1006                         vp->vp_integer = dval->value;
1007                         vp->length = 4;
1008                         break;
1009
1010                 case PW_TYPE_DATE:
1011                         {
1012                                 /*
1013                                  *      time_t may be 64 bits, whule vp_date
1014                                  *      MUST be 32-bits.  We need an
1015                                  *      intermediary variable to handle
1016                                  *      the conversions.
1017                                  */
1018                                 time_t date;
1019
1020                                 if (gettime(value, &date) < 0) {
1021                                         librad_log("failed to parse time string "
1022                                                    "\"%s\"", value);
1023                                         return NULL;
1024                                 }
1025
1026                                 vp->vp_date = date;
1027                         }
1028                         vp->length = 4;
1029                         break;
1030
1031                 case PW_TYPE_ABINARY:
1032 #ifdef ASCEND_BINARY
1033                         if (strncasecmp(value, "0x", 2) == 0) {
1034                                 vp->type = PW_TYPE_OCTETS;
1035                                 goto do_octets;
1036                         }
1037
1038                         if (ascend_parse_filter(vp) < 0 ) {
1039                                 librad_log("failed to parse Ascend binary attribute: %s",
1040                                            librad_errstr);
1041                                 return NULL;
1042                         }
1043                         break;
1044
1045                         /*
1046                          *      If Ascend binary is NOT defined,
1047                          *      then fall through to raw octets, so that
1048                          *      the user can at least make them by hand...
1049                          */
1050 #endif
1051         do_octets:
1052                         /* raw octets: 0x01020304... */
1053                 case PW_TYPE_OCTETS:
1054                         if (strncasecmp(value, "0x", 2) == 0) {
1055                                 uint8_t *us;
1056                                 cp = value + 2;
1057                                 us = vp->vp_octets;
1058                                 vp->length = 0;
1059
1060
1061                                 /*
1062                                  *      There is only one character,
1063                                  *      die.
1064                                  */
1065                                 if ((strlen(cp) & 0x01) != 0) {
1066                                         librad_log("Hex string is not an even length string.");
1067                                         return NULL;
1068                                 }
1069
1070
1071                                 while (*cp &&
1072                                        (vp->length < MAX_STRING_LEN)) {
1073                                         unsigned int tmp;
1074
1075                                         if (sscanf(cp, "%02x", &tmp) != 1) {
1076                                                 librad_log("Non-hex characters at %c%c", cp[0], cp[1]);
1077                                                 return NULL;
1078                                         }
1079
1080                                         cp += 2;
1081                                         *(us++) = tmp;
1082                                         vp->length++;
1083                                 }
1084                         }
1085                         break;
1086
1087                 case PW_TYPE_IFID:
1088                         if (ifid_aton(value, (void *) &vp->vp_ifid) == NULL) {
1089                                 librad_log("failed to parse interface-id "
1090                                            "string \"%s\"", value);
1091                                 return NULL;
1092                         }
1093                         vp->length = 8;
1094                         break;
1095
1096                 case PW_TYPE_IPV6ADDR:
1097                         if (inet_pton(AF_INET6, value, &vp->vp_ipv6addr) <= 0) {
1098                                 librad_log("failed to parse IPv6 address "
1099                                            "string \"%s\"", value);
1100                                 return NULL;
1101                         }
1102                         vp->length = 16; /* length of IPv6 address */
1103                         break;
1104
1105                 case PW_TYPE_IPV6PREFIX:
1106                         p = strchr(value, '/');
1107                         if (!p || ((p - value) >= 256)) {
1108                                 librad_log("invalid IPv6 prefix "
1109                                            "string \"%s\"", value);
1110                                 return NULL;
1111                         } else {
1112                                 unsigned int prefix;
1113                                 char buffer[256], *eptr;
1114
1115                                 memcpy(buffer, value, p - value);
1116                                 buffer[p - value] = '\0';
1117
1118                                 if (inet_pton(AF_INET6, buffer, vp->vp_octets + 2) <= 0) {
1119                                         librad_log("failed to parse IPv6 address "
1120                                                    "string \"%s\"", value);
1121                                         return NULL;
1122                                 }
1123
1124                                 prefix = strtoul(p + 1, &eptr, 10);
1125                                 if ((prefix > 128) || *eptr) {
1126                                         librad_log("failed to parse IPv6 address "
1127                                                    "string \"%s\"", value);
1128                                         return NULL;
1129                                 }
1130                                 vp->vp_octets[1] = prefix;
1131                         }
1132                         vp->vp_octets[0] = '\0';
1133                         vp->length = 16 + 2;
1134                         break;
1135
1136                 case PW_TYPE_ETHERNET:
1137                         {
1138                                 const char *c1, *c2;
1139
1140                                 length = 0;
1141                                 cp = value;
1142                                 while (*cp) {
1143                                         if (cp[1] == ':') {
1144                                                 c1 = hextab;
1145                                                 c2 = memchr(hextab, tolower((int) cp[0]), 16);
1146                                                 cp += 2;
1147                                         } else if ((cp[1] != '\0') &&
1148                                                    ((cp[2] == ':') ||
1149                                                     (cp[2] == '\0'))) {
1150                                                    c1 = memchr(hextab, tolower((int) cp[0]), 16);
1151                                                    c2 = memchr(hextab, tolower((int) cp[1]), 16);
1152                                                    cp += 2;
1153                                                    if (*cp == ':') cp++;
1154                                         } else {
1155                                                 c1 = c2 = NULL;
1156                                         }
1157                                         if (!c1 || !c2 || (length >= sizeof(vp->vp_ether))) {
1158                                                 librad_log("failed to parse Ethernet address \"%s\"", value);
1159                                                 return NULL;
1160                                         }
1161                                         vp->vp_ether[length] = ((c1-hextab)<<4) + (c2-hextab);
1162                                         length++;
1163                                 }
1164                         }
1165                         vp->length = 6;
1166                         break;
1167
1168                         /*
1169                          *  Anything else.
1170                          */
1171                 default:
1172                         librad_log("unknown attribute type %d", vp->type);
1173                         return NULL;
1174         }
1175
1176         return vp;
1177 }
1178
1179 /*
1180  *      Create a VALUE_PAIR from an ASCII attribute and value,
1181  *      where the attribute name is in the form:
1182  *
1183  *      Attr-%d
1184  *      Vendor-%d-Attr-%d
1185  *      VendorName-Attr-%d
1186  */
1187 static VALUE_PAIR *pairmake_any(const char *attribute, const char *value,
1188                                 int operator)
1189 {
1190         int             attr, vendor;
1191         size_t          size;
1192         const char      *p = attribute;
1193         char            *q;
1194         VALUE_PAIR      *vp;
1195
1196         /*
1197          *      Unknown attributes MUST be of type 'octets'
1198          */
1199         if (value && (strncasecmp(value, "0x", 2) != 0)) {
1200                 librad_log("Invalid octet string \"%s\" for attribute name \"%s\"", value, attribute);
1201                 return NULL;
1202         }
1203
1204         attr = vendor = 0;
1205
1206         /*
1207          *      Pull off vendor prefix first.
1208          */
1209         if (strncasecmp(p, "Attr-", 5) != 0) {
1210                 if (strncasecmp(p, "Vendor-", 7) == 0) {
1211                         vendor = (int) strtol(p + 7, &q, 10);
1212                         if ((vendor == 0) || (vendor > 65535)) {
1213                                 librad_log("Invalid vendor value in attribute name \"%s\"", attribute);
1214                                 return NULL;
1215                         }
1216
1217                         p = q;
1218
1219                 } else {        /* must be vendor name */
1220                         q = strchr(p, '-');
1221                         char buffer[256];
1222
1223                         if (!q) {
1224                                 librad_log("Invalid vendor name in attribute name \"%s\"", attribute);
1225                                 return NULL;
1226                         }
1227
1228                         if ((q - p) >= sizeof(buffer)) {
1229                                 librad_log("Vendor name too long in attribute name \"%s\"", attribute);
1230                                 return NULL;
1231                         }
1232
1233                         memcpy(buffer, p, (q - p));
1234                         buffer[q - p] = '\0';
1235
1236                         vendor = dict_vendorbyname(buffer);
1237                         if (!vendor) {
1238                                 librad_log("Unknown vendor name in attribute name \"%s\"", attribute);
1239                                 return NULL;
1240                         }
1241
1242                         p = q;
1243                 }
1244
1245                 if (*p != '-') {
1246                         librad_log("Invalid text following vendor definition in attribute name \"%s\"", attribute);
1247                         return NULL;
1248                 }
1249                 p++;
1250         }
1251
1252         /*
1253          *      Attr-%d
1254          */
1255         if (strncasecmp(p, "Attr-", 5) != 0) {
1256                 librad_log("Invalid format in attribute name \"%s\"", attribute);
1257                 return NULL;
1258         }
1259
1260         attr = strtol(p + 5, &q, 10);
1261
1262         /*
1263          *      Invalid, or trailing text after number.
1264          */
1265         if ((attr == 0) || *q) {
1266                 librad_log("Invalid value in attribute name \"%s\"", attribute);
1267                 return NULL;
1268         }
1269
1270         /*
1271          *      Double-check the size of attr.
1272          */
1273         if (vendor) {
1274                 DICT_VENDOR *dv = dict_vendorbyvalue(vendor);
1275
1276                 if (!dv) {
1277                         if (attr > 255) {
1278                         attr_error:
1279                                 librad_log("Invalid attribute number in attribute name \"%s\"", attribute);
1280                                 return NULL;
1281                         }
1282
1283                 } else switch (dv->type) {
1284                         case 1:
1285                                 if (attr > 255) goto attr_error;
1286                                 break;
1287
1288                         case 2:
1289                                 if (attr > 65535) goto attr_error;
1290                                 break;
1291
1292                         case 4: /* Internal limitations! */
1293                                 if (attr > 65535) goto attr_error;
1294                                 break;
1295
1296                         default:
1297                                 librad_log("Internal sanity check failed");
1298                                 return NULL;
1299                 }
1300         }
1301
1302         attr |= vendor << 16;
1303
1304         /*
1305          *      We've now parsed the attribute properly, Let's create
1306          *      it.  This next stop also looks the attribute up in the
1307          *      dictionary, and creates the appropriate type for it.
1308          */
1309         if ((vp = paircreate(attr, PW_TYPE_OCTETS)) == NULL) {
1310                 librad_log("out of memory");
1311                 return NULL;
1312         }
1313
1314         size = strlen(value + 2);
1315
1316         /*
1317          *      We may be reading something like Attr-5.  i.e.
1318          *      who-ever wrote the text didn't understand it, but we
1319          *      do.
1320          */
1321         switch (vp->type) {
1322         default:
1323                 if (size == (vp->length * 2)) break;
1324                 vp->type = PW_TYPE_OCTETS;
1325                 /* FALL-THROUGH */
1326                 
1327         case PW_TYPE_STRING:
1328         case PW_TYPE_OCTETS:
1329         case PW_TYPE_ABINARY:
1330                 vp->length = size >> 1;
1331                 break;
1332         }
1333
1334         if (fr_hex2bin(value + 2, vp->vp_octets, size) != vp->length) {
1335                 librad_log("Invalid hex string");
1336                 free(vp);
1337                 return NULL;
1338         }
1339
1340         /*
1341          *      Move contents around based on type.  This is
1342          *      to work around the historical use of "lvalue".
1343          */
1344         switch (vp->type) {
1345         case PW_TYPE_DATE:
1346         case PW_TYPE_IPADDR:
1347         case PW_TYPE_INTEGER:
1348                 memcpy(&vp->lvalue, vp->vp_octets, sizeof(vp->lvalue));
1349                 vp->vp_strvalue[0] = '\0';
1350                 break;
1351                 
1352         default:
1353                 break;
1354         }
1355        
1356         vp->operator = (operator == 0) ? T_OP_EQ : operator;
1357
1358         return vp;
1359 }
1360
1361
1362 /*
1363  *      Create a VALUE_PAIR from an ASCII attribute and value.
1364  */
1365 VALUE_PAIR *pairmake(const char *attribute, const char *value, int operator)
1366 {
1367         DICT_ATTR       *da;
1368         VALUE_PAIR      *vp;
1369         char            *tc, *ts;
1370         signed char     tag;
1371         int             found_tag;
1372 #ifdef HAVE_REGEX_H
1373         int             res;
1374         regex_t         cre;
1375 #endif
1376
1377         /*
1378          *    Check for tags in 'Attribute:Tag' format.
1379          */
1380         found_tag = 0;
1381         tag = 0;
1382
1383         ts = strrchr(attribute, ':');
1384         if (ts && !ts[1]) {
1385                 librad_log("Invalid tag for attribute %s", attribute);
1386                 return NULL;
1387         }
1388
1389         if (ts && ts[1]) {
1390                  /* Colon found with something behind it */
1391                  if (ts[1] == '*' && ts[2] == 0) {
1392                          /* Wildcard tag for check items */
1393                          tag = TAG_ANY;
1394                          *ts = 0;
1395                  } else if ((ts[1] >= '0') && (ts[1] <= '9')) {
1396                          /* It's not a wild card tag */
1397                          tag = strtol(ts + 1, &tc, 0);
1398                          if (tc && !*tc && TAG_VALID_ZERO(tag))
1399                                  *ts = 0;
1400                          else tag = 0;
1401                  } else {
1402                          librad_log("Invalid tag for attribute %s", attribute);
1403                          return NULL;
1404                  }
1405                  found_tag = 1;
1406         }
1407
1408         /*
1409          *      It's not found in the dictionary, so we use
1410          *      another method to create the attribute.
1411          */
1412         if ((da = dict_attrbyname(attribute)) == NULL) {
1413                 return pairmake_any(attribute, value, operator);
1414         }
1415
1416         if ((vp = pairalloc(da)) == NULL) {
1417                 librad_log("out of memory");
1418                 return NULL;
1419         }
1420         vp->operator = (operator == 0) ? T_OP_EQ : operator;
1421
1422         /*      Check for a tag in the 'Merit' format of:
1423          *      :Tag:Value.  Print an error if we already found
1424          *      a tag in the Attribute.
1425          */
1426
1427         if (value && (*value == ':' && da->flags.has_tag)) {
1428                 /* If we already found a tag, this is invalid */
1429                 if(found_tag) {
1430                         pairbasicfree(vp);
1431                         librad_log("Duplicate tag %s for attribute %s",
1432                                    value, vp->name);
1433                         DEBUG("Duplicate tag %s for attribute %s\n",
1434                                    value, vp->name);
1435                         return NULL;
1436
1437                 }
1438                 /* Colon found and attribute allows a tag */
1439                 if (value[1] == '*' && value[2] == ':') {
1440                        /* Wildcard tag for check items */
1441                        tag = TAG_ANY;
1442                        value += 3;
1443                 } else {
1444                        /* Real tag */
1445                        tag = strtol(value + 1, &tc, 0);
1446                        if (tc && *tc==':' && TAG_VALID_ZERO(tag))
1447                             value = tc + 1;
1448                        else tag = 0;
1449                 }
1450                 found_tag = 1;
1451         }
1452
1453         if (found_tag) {
1454           vp->flags.tag = tag;
1455         }
1456
1457         switch (vp->operator) {
1458         default:
1459                 break;
1460
1461                 /*
1462                  *      For =* and !* operators, the value is irrelevant
1463                  *      so we return now.
1464                  */
1465         case T_OP_CMP_TRUE:
1466         case T_OP_CMP_FALSE:
1467                 vp->vp_strvalue[0] = '\0';
1468                 vp->length = 0;
1469                 return vp;
1470                 break;
1471
1472                 /*
1473                  *      Regular expression comparison of integer attributes
1474                  *      does a STRING comparison of the names of their
1475                  *      integer attributes.
1476                  */
1477         case T_OP_REG_EQ:       /* =~ */
1478         case T_OP_REG_NE:       /* !~ */
1479                 if (vp->type == PW_TYPE_INTEGER) {
1480                         return vp;
1481                 }
1482 #ifdef HAVE_REGEX_H
1483                 /*
1484                  *      Regular expression match with no regular
1485                  *      expression is wrong.
1486                  */
1487                 if (!value) {
1488                         pairfree(&vp);
1489                         return NULL;
1490                 }
1491
1492                 res = regcomp(&cre, value, REG_EXTENDED|REG_NOSUB);
1493                 if (res != 0) {
1494                         char    msg[128];
1495
1496                         regerror(res, &cre, msg, sizeof(msg));
1497                         librad_log("Illegal regular expression in attribute: %s: %s",
1498                                 vp->name, msg);
1499                         pairbasicfree(vp);
1500                         return NULL;
1501                 }
1502                 regfree(&cre);
1503 #else
1504                 librad_log("Regelar expressions not enabled in this build, error in attribute %s",
1505                                 vp->name);
1506                 pairbasicfree(vp);
1507                 return NULL;
1508 #endif
1509         }
1510
1511         /*
1512          *      FIXME: if (strcasecmp(attribute, vp->name) != 0)
1513          *      then the user MAY have typed in the attribute name
1514          *      as Vendor-%d-Attr-%d, and the value MAY be octets.
1515          *
1516          *      We probably want to fix pairparsevalue to accept
1517          *      octets as values for any attribute.
1518          */
1519         if (value && (pairparsevalue(vp, value) == NULL)) {
1520                 pairbasicfree(vp);
1521                 return NULL;
1522         }
1523
1524         return vp;
1525 }
1526
1527
1528 /*
1529  *      [a-zA-Z0-9_-:]+
1530  */
1531 static const int valid_attr_name[256] = {
1532         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1533         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1534         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0,
1535         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,
1536         0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1537         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1,
1538         0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1539         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,
1540         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1541         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1542         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1543         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1544         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1545         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1546         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1547         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
1548 };
1549
1550 /*
1551  *      Read a valuepair from a buffer, and advance pointer.
1552  *      Sets *eol to T_EOL if end of line was encountered.
1553  */
1554 VALUE_PAIR *pairread(char **ptr, FR_TOKEN *eol)
1555 {
1556         char            buf[64];
1557         char            attr[64];
1558         char            value[512];
1559         char            *p, *q;
1560         FR_TOKEN        token, t, xlat;
1561         VALUE_PAIR      *vp;
1562         size_t          len;
1563
1564         *eol = T_OP_INVALID;
1565
1566         p = *ptr;
1567         while ((*p == ' ') || (*p == '\t')) p++;
1568
1569         if (!*p) {
1570                 *eol = T_OP_INVALID;
1571                 librad_log("No token read where we expected an attribute name");
1572                 return NULL;
1573         }
1574
1575         if (*p == '#') {
1576                 *eol = T_HASH;
1577                 librad_log("Read a comment instead of a token");
1578                 return NULL;
1579         }
1580
1581         q = attr;
1582         for (len = 0; len < sizeof(attr); len++) {
1583                 if (valid_attr_name[(int)*p]) {
1584                         *q++ = *p++;
1585                         continue;
1586                 }
1587                 break;
1588         }
1589
1590         if (len == sizeof(attr)) {
1591                 *eol = T_OP_INVALID;
1592                 librad_log("Attribute name is too long");
1593                 return NULL;
1594         }
1595
1596         /*
1597          *      We may have Foo-Bar:= stuff, so back up.
1598          */
1599         if (attr[len - 1] == ':') {
1600                 p--;
1601                 len--;
1602         }
1603
1604         attr[len] = '\0';
1605         *ptr = p;
1606
1607         /* Now we should have an operator here. */
1608         token = gettoken(ptr, buf, sizeof(buf));
1609         if (token < T_EQSTART || token > T_EQEND) {
1610                 librad_log("expecting operator");
1611                 return NULL;
1612         }
1613
1614         /* Read value.  Note that empty string values are allowed */
1615         xlat = gettoken(ptr, value, sizeof(value));
1616         if (xlat == T_EOL) {
1617                 librad_log("failed to get value");
1618                 return NULL;
1619         }
1620
1621         /*
1622          *      Peek at the next token. Must be T_EOL, T_COMMA, or T_HASH
1623          */
1624         p = *ptr;
1625         t = gettoken(&p, buf, sizeof(buf));
1626         if (t != T_EOL && t != T_COMMA && t != T_HASH) {
1627                 librad_log("Expected end of line or comma");
1628                 return NULL;
1629         }
1630
1631         *eol = t;
1632         if (t == T_COMMA) {
1633                 *ptr = p;
1634         }
1635
1636         vp = NULL;
1637         switch (xlat) {
1638                 /*
1639                  *      Make the full pair now.
1640                  */
1641         default:
1642                 vp = pairmake(attr, value, token);
1643                 break;
1644
1645                 /*
1646                  *      Perhaps do xlat's
1647                  */
1648         case T_DOUBLE_QUOTED_STRING:
1649                 p = strchr(value, '%');
1650                 if (p && (p[1] == '{')) {
1651                         if (strlen(value) >= sizeof(vp->vp_strvalue)) {
1652                                 librad_log("Value too long");
1653                                 return NULL;
1654                         }
1655                         vp = pairmake(attr, NULL, token);
1656                         if (!vp) {
1657                                 *eol = T_OP_INVALID;
1658                                 return NULL;
1659                         }
1660
1661                         strlcpy(vp->vp_strvalue, value, sizeof(vp->vp_strvalue));
1662                         vp->flags.do_xlat = 1;
1663                         vp->length = 0;
1664                 } else {
1665                         vp = pairmake(attr, value, token);
1666                 }
1667                 break;
1668
1669
1670                 /*
1671                  *      Mark the pair to be allocated later.
1672                  */
1673         case T_BACK_QUOTED_STRING:
1674                 if (strlen(value) >= sizeof(vp->vp_strvalue)) {
1675                         librad_log("Value too long");
1676                         return NULL;
1677                 }
1678
1679                 vp = pairmake(attr, NULL, token);
1680                 if (!vp) {
1681                         *eol = T_OP_INVALID;
1682                         return NULL;
1683                 }
1684
1685                 vp->flags.do_xlat = 1;
1686                 strlcpy(vp->vp_strvalue, value, sizeof(vp->vp_strvalue));
1687                 vp->length = 0;
1688                 break;
1689         }
1690
1691         /*
1692          *      If we didn't make a pair, return an error.
1693          */
1694         if (!vp) {
1695                 *eol = T_OP_INVALID;
1696                 return NULL;
1697         }
1698
1699         return vp;
1700 }
1701
1702 /*
1703  *      Read one line of attribute/value pairs. This might contain
1704  *      multiple pairs seperated by comma's.
1705  */
1706 FR_TOKEN userparse(char *buffer, VALUE_PAIR **first_pair)
1707 {
1708         VALUE_PAIR      *vp;
1709         char            *p;
1710         FR_TOKEN        last_token = T_OP_INVALID;
1711         FR_TOKEN        previous_token;
1712
1713         /*
1714          *      We allow an empty line.
1715          */
1716         if (buffer[0] == 0)
1717                 return T_EOL;
1718
1719         p = buffer;
1720         do {
1721                 previous_token = last_token;
1722                 if ((vp = pairread(&p, &last_token)) == NULL) {
1723                         return last_token;
1724                 }
1725                 pairadd(first_pair, vp);
1726         } while (*p && (last_token == T_COMMA));
1727
1728         /*
1729          *      Don't tell the caller that there was a comment.
1730          */
1731         if (last_token == T_HASH) {
1732                 return previous_token;
1733         }
1734
1735         /*
1736          *      And return the last token which we read.
1737          */
1738         return last_token;
1739 }
1740
1741 /*
1742  *      Read valuepairs from the fp up to End-Of-File.
1743  *
1744  *      Hmm... this function is only used by radclient..
1745  */
1746 VALUE_PAIR *readvp2(FILE *fp, int *pfiledone, const char *errprefix)
1747 {
1748         char buf[8192];
1749         FR_TOKEN last_token = T_EOL;
1750         VALUE_PAIR *vp;
1751         VALUE_PAIR *list;
1752         int error = 0;
1753
1754         list = NULL;
1755
1756         while (!error && fgets(buf, sizeof(buf), fp) != NULL) {
1757                 /*
1758                  *      If we get a '\n' by itself, we assume that's
1759                  *      the end of that VP
1760                  */
1761                 if ((buf[0] == '\n') && (list)) {
1762                         return list;
1763                 }
1764                 if ((buf[0] == '\n') && (!list)) {
1765                         continue;
1766                 }
1767
1768                 /*
1769                  *      Comments get ignored
1770                  */
1771                 if (buf[0] == '#') continue;
1772
1773                 /*
1774                  *      Read all of the attributes on the current line.
1775                  */
1776                 vp = NULL;
1777                 last_token = userparse(buf, &vp);
1778                 if (!vp) {
1779                         if (last_token != T_EOL) {
1780                                 librad_perror("%s", errprefix);
1781                                 error = 1;
1782                                 break;
1783                         }
1784                         break;
1785                 }
1786
1787                 pairadd(&list, vp);
1788                 buf[0] = '\0';
1789         }
1790
1791         if (error) pairfree(&list);
1792
1793         *pfiledone = 1;
1794
1795         return error ? NULL: list;
1796 }
1797
1798
1799
1800 /*
1801  *      Compare two pairs, using the operator from "one".
1802  *
1803  *      i.e. given two attributes, it does:
1804  *
1805  *      (two->data) (one->operator) (one->data)
1806  *
1807  *      e.g. "foo" != "bar"
1808  *
1809  *      Returns true (comparison is true), or false (comparison is not true);
1810  */
1811 int paircmp(VALUE_PAIR *one, VALUE_PAIR *two)
1812 {
1813         int compare;
1814
1815         switch (one->operator) {
1816         case T_OP_CMP_TRUE:
1817                 return (two != NULL);
1818
1819         case T_OP_CMP_FALSE:
1820                 return (two == NULL);
1821
1822                 /*
1823                  *      One is a regex, compile it, print two to a string,
1824                  *      and then do string comparisons.
1825                  */
1826         case T_OP_REG_EQ:
1827         case T_OP_REG_NE:
1828 #ifndef HAVE_REGEX_H
1829                 return -1;
1830 #else
1831                 {
1832                         regex_t reg;
1833                         char buffer[MAX_STRING_LEN * 4 + 1];
1834
1835                         compare = regcomp(&reg, one->vp_strvalue,
1836                                           REG_EXTENDED);
1837                         if (compare != 0) {
1838                                 regerror(compare, &reg, buffer, sizeof(buffer));
1839                                 librad_log("Illegal regular expression in attribute: %s: %s",
1840                                            one->name, buffer);
1841                                 return -1;
1842                         }
1843
1844                         vp_prints_value(buffer, sizeof(buffer), two, 0);
1845
1846                         /*
1847                          *      Don't care about substring matches,
1848                          *      oh well...
1849                          */
1850                         compare = regexec(&reg, buffer, 0, NULL, 0);
1851
1852                         regfree(&reg);
1853                         if (one->operator == T_OP_REG_EQ) return (compare == 0);
1854                         return (compare != 0);
1855                 }
1856 #endif
1857
1858         default:                /* we're OK */
1859                 break;
1860         }
1861
1862         /*
1863          *      After doing the previous check for special comparisons,
1864          *      do the per-type comparison here.
1865          */
1866         switch (one->type) {
1867         case PW_TYPE_ABINARY:
1868         case PW_TYPE_OCTETS:
1869         {
1870                 size_t length;
1871                 const uint8_t *p, *q;
1872
1873                 if (one->length < two->length) {
1874                         length = one->length;
1875                 } else {
1876                         length = two->length;
1877                 }
1878
1879                 p = two->vp_octets;
1880                 q = one->vp_octets;
1881                 while (length) {
1882                         compare = ((int) *p) - ((int) *q);
1883                         if (compare != 0) goto type_switch;
1884                 }
1885
1886                 /*
1887                  *      Contents are the same.  The return code
1888                  *      is therefore the difference in lengths.
1889                  *
1890                  *      i.e. "0x00" is smaller than "0x0000"
1891                  */
1892                 compare = two->length - one->length;
1893         }
1894                 break;
1895
1896         case PW_TYPE_STRING:
1897                 compare = strcmp(two->vp_strvalue, one->vp_strvalue);
1898                 break;
1899
1900         case PW_TYPE_BYTE:
1901         case PW_TYPE_SHORT:
1902         case PW_TYPE_INTEGER:
1903         case PW_TYPE_DATE:
1904                 compare = two->vp_integer - one->vp_integer;
1905                 break;
1906
1907         case PW_TYPE_IPADDR:
1908                 compare = ntohl(two->vp_ipaddr) - ntohl(one->vp_ipaddr);
1909                 break;
1910
1911         case PW_TYPE_IPV6ADDR:
1912                 compare = memcmp(&two->vp_ipv6addr, &one->vp_ipv6addr,
1913                                  sizeof(two->vp_ipv6addr));
1914                 break;
1915
1916         case PW_TYPE_IPV6PREFIX:
1917                 compare = memcmp(&two->vp_ipv6prefix, &one->vp_ipv6prefix,
1918                                  sizeof(two->vp_ipv6prefix));
1919                 break;
1920
1921         case PW_TYPE_IFID:
1922                 compare = memcmp(&two->vp_ifid, &one->vp_ifid,
1923                                  sizeof(two->vp_ifid));
1924                 break;
1925
1926         default:
1927                 return 0;       /* unknown type */
1928         }
1929
1930         /*
1931          *      Now do the operator comparison.
1932          */
1933  type_switch:
1934         switch (one->operator) {
1935         case T_OP_CMP_EQ:
1936                 return (compare == 0);
1937
1938         case T_OP_NE:
1939                 return (compare != 0);
1940
1941         case T_OP_LT:
1942                 return (compare < 0);
1943
1944         case T_OP_GT:
1945                 return (compare > 0);
1946
1947         case T_OP_LE:
1948                 return (compare <= 0);
1949
1950         case T_OP_GE:
1951                 return (compare >= 0);
1952
1953         default:
1954                 return 0;
1955         }
1956
1957         return 0;
1958 }