Debian Codegraph @

pppd/plugins/radius/avpair.c

raw
1 /*
2 * Copyright (C) 1995 Lars Fenneberg
3 *
4 * Copyright 1992 Livingston Enterprises, Inc.
5 *
6 * Copyright 1992,1993, 1994,1995 The Regents of the University of Michigan
7 * and Merit Network, Inc. All Rights Reserved
8 *
9 * See the file COPYRIGHT for the respective terms and conditions.
10 * If the file is missing contact me at [email protected]
11 * and I'll send you a copy.
12 *
13 */
14
15 #include <includes.h>
16 #include <radiusclient.h>
17
18 static void rc_extract_vendor_specific_attributes(int attrlen,
19 unsigned char *ptr,
20 VALUE_PAIR **vp);
21 /*
22 * Function: rc_avpair_add
23 *
24 * Purpose: add an attribute-value pair to the given list.
25 *
26 * Returns: pointer to added a/v pair upon success, NULL pointer upon failure.
27 *
28 * Remarks: Always appends the new pair to the end of the list.
29 *
30 */
31
32 VALUE_PAIR *rc_avpair_add (VALUE_PAIR **list, int attrid, const void *pval, int len,
33 int vendorcode)
34 {
35 VALUE_PAIR *vp;
36
37 vp = rc_avpair_new (attrid, pval, len, vendorcode);
38
39 if (vp != (VALUE_PAIR *) NULL)
40 {
41 rc_avpair_insert (list, (VALUE_PAIR *) NULL, vp);
42 }
43
44 return vp;
45
46 }
47
48 /*
49 * Function: rc_avpair_assign
50 *
51 * Purpose: assign the given value to an attribute-value pair.
52 *
53 * Returns: 0 on success,
54 * -1 on failure.
55 *
56 */
57
58 int rc_avpair_assign (VALUE_PAIR *vp, const void *pval, int len)
59 {
60 int result = -1;
61
62 switch (vp->type)
63 {
64 case PW_TYPE_STRING:
65
66 if (((len == 0) && (strlen ((const char *) pval)) > AUTH_STRING_LEN)
67 || (len > AUTH_STRING_LEN)) {
68 error("rc_avpair_assign: bad attribute length");
69 return result;
70 }
71
72 if (len > 0) {
73 memcpy(vp->strvalue, (const char *)pval, len);
74 vp->strvalue[len] = '\0';
75 vp->lvalue = len;
76 } else {
77 strncpy ((char*) vp->strvalue, (const char *) pval, AUTH_STRING_LEN);
78 vp->lvalue = strlen((const char *) pval);
79 }
80
81 result = 0;
82 break;
83
84 case PW_TYPE_DATE:
85 case PW_TYPE_INTEGER:
86 case PW_TYPE_IPADDR:
87
88 vp->lvalue = * (UINT4 *) pval;
89
90 result = 0;
91 break;
92
93 default:
94 error("rc_avpair_assign: unknown attribute %d", vp->type);
95 }
96 return result;
97 }
98
99 /*
100 * Function: rc_avpair_new
101 *
102 * Purpose: make a new attribute-value pair with given parameters.
103 *
104 * Returns: pointer to generated a/v pair when successful, NULL when failure.
105 *
106 */
107
108 VALUE_PAIR *rc_avpair_new (int attrid, const void *pval, int len, int vendorcode)
109 {
110 VALUE_PAIR *vp = (VALUE_PAIR *) NULL;
111 DICT_ATTR *pda;
112
113 if ((pda = rc_dict_getattr (attrid, vendorcode)) == (DICT_ATTR *) NULL)
114 {
115 error("rc_avpair_new: unknown attribute %d", attrid);
116 }
117 else
118 {
119 if ((vp = (VALUE_PAIR *) malloc (sizeof (VALUE_PAIR)))
120 != (VALUE_PAIR *) NULL)
121 {
122 strlcpy (vp->name, pda->name, NAME_LENGTH);
123 vp->attribute = attrid;
124 vp->vendorcode = vendorcode;
125 vp->next = (VALUE_PAIR *) NULL;
126 vp->type = pda->type;
127 if (rc_avpair_assign (vp, pval, len) == 0)
128 {
129 return vp;
130 }
131 free (vp);
132 vp = (VALUE_PAIR *) NULL;
133 }
134 else
135 novm("rc_avpair_new");
136 }
137 return vp;
138 }
139
140 /*
141 *
142 * Function: rc_avpair_gen
143 *
144 * Purpose: takes attribute/value pairs from buffer and builds a
145 * value_pair list using allocated memory.
146 *
147 * Returns: value_pair list or NULL on failure
148 */
149
150 VALUE_PAIR *rc_avpair_gen (AUTH_HDR *auth)
151 {
152 int length;
153 int x_len;
154 int attribute;
155 int attrlen;
156 UINT4 lvalue;
157 unsigned char *x_ptr;
158 unsigned char *ptr;
159 DICT_ATTR *attr;
160 VALUE_PAIR *vp;
161 VALUE_PAIR *pair;
162 char hex[3]; /* For hex string conversion. */
163 char buffer[512];
164
165 /*
166 * Extract attribute-value pairs
167 */
168 ptr = auth->data;
169 length = ntohs ((unsigned short) auth->length) - AUTH_HDR_LEN;
170 vp = (VALUE_PAIR *) NULL;
171
172 while (length > 0)
173 {
174 attribute = *ptr++;
175 attrlen = *ptr++;
176 if (attrlen < 2 || attrlen > length)
177 {
178 error("rc_avpair_gen: received attribute with invalid length");
179 break;
180 }
181 attrlen -= 2;
182
183 /* Handle vendor-specific specially */
184 if (attribute == PW_VENDOR_SPECIFIC) {
185 rc_extract_vendor_specific_attributes(attrlen, ptr, &vp);
186 ptr += attrlen;
187 length -= (attrlen + 2);
188 continue;
189 }
190 if ((attr = rc_dict_getattr (attribute, VENDOR_NONE)) == (DICT_ATTR *) NULL)
191 {
192 *buffer= '\0'; /* Initial length. */
193 for (x_ptr = ptr, x_len = attrlen ;
194 x_len > 0 ;
195 x_len--, x_ptr++)
196 {
197 sprintf (hex, "%2.2X", *x_ptr);
198 strcat (buffer, hex);
199 }
200 warn("rc_avpair_gen: received unknown attribute %d of length %d: 0x%s",
201 attribute, attrlen, buffer);
202 }
203 else
204 {
205 if ((pair =
206 (VALUE_PAIR *) malloc (sizeof (VALUE_PAIR))) ==
207 (VALUE_PAIR *) NULL)
208 {
209 novm("rc_avpair_gen");
210 rc_avpair_free(vp);
211 return NULL;
212 }
213 strcpy (pair->name, attr->name);
214 pair->attribute = attr->value;
215 pair->vendorcode = VENDOR_NONE;
216 pair->type = attr->type;
217 pair->next = (VALUE_PAIR *) NULL;
218
219 switch (attr->type)
220 {
221
222 case PW_TYPE_STRING:
223 case PW_TYPE_IFID:
224 case PW_TYPE_IPV6ADDR:
225 case PW_TYPE_IPV6PREFIX:
226 memcpy (pair->strvalue, (char *) ptr, (size_t) attrlen);
227 pair->strvalue[attrlen] = '\0';
228 pair->lvalue = attrlen;
229 rc_avpair_insert (&vp, (VALUE_PAIR *) NULL, pair);
230 break;
231
232 case PW_TYPE_INTEGER:
233 case PW_TYPE_IPADDR:
234 memcpy ((char *) &lvalue, (char *) ptr,
235 sizeof (UINT4));
236 pair->lvalue = ntohl (lvalue);
237 rc_avpair_insert (&vp, (VALUE_PAIR *) NULL, pair);
238 break;
239
240 default:
241 warn("rc_avpair_gen: %s has unknown type", attr->name);
242 free (pair);
243 break;
244 }
245
246 }
247 ptr += attrlen;
248 length -= attrlen + 2;
249 }
250 return (vp);
251 }
252
253 /*
254 * Function: rc_extract_vendor_specific_attributes
255 *
256 * Purpose: Extracts vendor-specific attributes, assuming they are in
257 * the "SHOULD" format recommended by RCF 2138.
258 *
259 * Returns: found value_pair
260 *
261 */
262 static void rc_extract_vendor_specific_attributes(int attrlen,
263 unsigned char *ptr,
264 VALUE_PAIR **vp)
265 {
266 int vendor_id;
267 int vtype;
268 int vlen;
269 UINT4 lvalue;
270 DICT_ATTR *attr;
271 VALUE_PAIR *pair;
272
273 /* ptr is sitting at vendor-ID */
274 if (attrlen < 8) {
275 /* Nothing to see here... */
276 return;
277 }
278
279 /* High-order octet of Vendor-Id must be zero (RFC2138) */
280 if (*ptr) {
281 return;
282 }
283
284 /* Extract vendor_id */
285 vendor_id = (int) (
286 ((unsigned int) ptr[1]) * 256 * 256 +
287 ((unsigned int) ptr[2]) * 256 +
288 ((unsigned int) ptr[3]));
289 /* Bump ptr up to contents */
290 ptr += 4;
291
292 /* Set attrlen to length of data */
293 attrlen -= 4;
294 for (; attrlen; attrlen -= vlen+2, ptr += vlen) {
295 vtype = *ptr++;
296 vlen = *ptr++;
297 vlen -= 2;
298 if (vlen < 0 || vlen > attrlen - 2) {
299 /* Do not log an error. We are supposed to be able to cope with
300 arbitrary vendor-specific gunk */
301 return;
302 }
303 /* Looks plausible... */
304 if ((attr = rc_dict_getattr(vtype, vendor_id)) == NULL) {
305 continue;
306 }
307
308 /* TODO: Check that length matches data size!!!!! */
309 pair = (VALUE_PAIR *) malloc(sizeof(VALUE_PAIR));
310 if (!pair) {
311 novm("rc_avpair_gen");
312 return;
313 }
314 strcpy(pair->name, attr->name);
315 pair->attribute = attr->value;
316 pair->vendorcode = vendor_id;
317 pair->type = attr->type;
318 pair->next = NULL;
319 switch (attr->type) {
320 case PW_TYPE_STRING:
321 memcpy (pair->strvalue, (char *) ptr, (size_t) vlen);
322 pair->strvalue[vlen] = '\0';
323 pair->lvalue = vlen;
324 rc_avpair_insert (vp, (VALUE_PAIR *) NULL, pair);
325 break;
326
327 case PW_TYPE_INTEGER:
328 case PW_TYPE_IPADDR:
329 memcpy ((char *) &lvalue, (char *) ptr,
330 sizeof (UINT4));
331 pair->lvalue = ntohl (lvalue);
332 rc_avpair_insert (vp, (VALUE_PAIR *) NULL, pair);
333 break;
334
335 default:
336 warn("rc_avpair_gen: %s has unknown type", attr->name);
337 free (pair);
338 break;
339 }
340 }
341 }
342
343 /*
344 * Function: rc_avpair_get
345 *
346 * Purpose: Find the first attribute value-pair (which matches the given
347 * attribute) from the specified value-pair list.
348 *
349 * Returns: found value_pair
350 *
351 */
352
353 VALUE_PAIR *rc_avpair_get (VALUE_PAIR *vp, UINT4 attr)
354 {
355 for (; vp != (VALUE_PAIR *) NULL && vp->attribute != attr; vp = vp->next)
356 {
357 continue;
358 }
359 return (vp);
360 }
361
362 /*
363 * Function: rc_avpair_copy
364 *
365 * Purpose: Return a copy of the existing list "p" ala strdup().
366 *
367 */
368 VALUE_PAIR *rc_avpair_copy(VALUE_PAIR *p)
369 {
370 VALUE_PAIR *vp, *fp = NULL, *lp = NULL;
371
372 while (p) {
373 vp = malloc(sizeof(VALUE_PAIR));
374 if (!vp) {
375 novm("rc_avpair_copy");
376 return NULL; /* leaks a little but so what */
377 }
378 *vp = *p;
379 if (!fp)
380 fp = vp;
381 if (lp)
382 lp->next = vp;
383 lp = vp;
384 p = p->next;
385 }
386
387 return fp;
388 }
389
390 /*
391 * Function: rc_avpair_insert
392 *
393 * Purpose: Given the address of an existing list "a" and a pointer
394 * to an entry "p" in that list, add the list "b" to
395 * the "a" list after the "p" entry. If "p" is NULL, add
396 * the list "b" to the end of "a".
397 *
398 */
399
400 void rc_avpair_insert (VALUE_PAIR **a, VALUE_PAIR *p, VALUE_PAIR *b)
401 {
402 VALUE_PAIR *this_node = NULL;
403 VALUE_PAIR *vp;
404
405 if (*a == (VALUE_PAIR *) NULL)
406 {
407 *a = b;
408 return;
409 }
410
411 if (!b)
412 return;
413
414 vp = *a;
415
416 if ( p == (VALUE_PAIR *) NULL) /* run to end of "a" list */
417 {
418 while (vp != (VALUE_PAIR *) NULL)
419 {
420 this_node = vp;
421 vp = vp->next;
422 }
423 }
424 else /* look for the "p" entry in the "a" list (or run to end) */
425 {
426 this_node = *a;
427 while (this_node->next != (VALUE_PAIR *) NULL)
428 {
429 if (this_node == p)
430 {
431 break;
432 }
433 this_node = this_node->next;
434 }
435 }
436
437 /* add "b" at this_node */
438 vp = this_node->next;
439 this_node->next = b;
440
441 /* run to end of "b" and connect the rest of "a" */
442 while (b->next)
443 b = b->next;
444 b->next = vp;
445
446 return;
447 }
448
449 /*
450 * Function: rc_avpair_free
451 *
452 * Purpose: frees all value_pairs in the list
453 *
454 */
455
456 void rc_avpair_free (VALUE_PAIR *pair)
457 {
458 VALUE_PAIR *next;
459
460 while (pair != (VALUE_PAIR *) NULL)
461 {
462 next = pair->next;
463 free (pair);
464 pair = next;
465 }
466 }
467
468 /*
469 * Function: rc_fieldcpy
470 *
471 * Purpose: Copy a data field from the buffer. Advance the buffer
472 * past the data field.
473 *
474 */
475
476 static void rc_fieldcpy (char *string, char **uptr)
477 {
478 char *ptr;
479
480 ptr = *uptr;
481 if (*ptr == '"')
482 {
483 ptr++;
484 while (*ptr != '"' && *ptr != '\0' && *ptr != '\n')
485 {
486 *string++ = *ptr++;
487 }
488 *string = '\0';
489 if (*ptr == '"')
490 {
491 ptr++;
492 }
493 *uptr = ptr;
494 return;
495 }
496
497 while (*ptr != ' ' && *ptr != '\t' && *ptr != '\0' && *ptr != '\n' &&
498 *ptr != '=' && *ptr != ',')
499 {
500 *string++ = *ptr++;
501 }
502 *string = '\0';
503 *uptr = ptr;
504 return;
505 }
506
507
508 /*
509 * Function: rc_avpair_parse
510 *
511 * Purpose: parses the buffer to extract the attribute-value pairs.
512 *
513 * Returns: 0 = successful parse of attribute-value pair,
514 * -1 = syntax (or other) error detected.
515 *
516 */
517
518 #define PARSE_MODE_NAME 0
519 #define PARSE_MODE_EQUAL 1
520 #define PARSE_MODE_VALUE 2
521 #define PARSE_MODE_INVALID 3
522
523 int rc_avpair_parse (char *buffer, VALUE_PAIR **first_pair)
524 {
525 int mode;
526 char attrstr[AUTH_ID_LEN];
527 char valstr[AUTH_ID_LEN];
528 DICT_ATTR *attr = NULL;
529 DICT_VALUE *dval;
530 VALUE_PAIR *pair;
531 VALUE_PAIR *link;
532 struct tm *tm;
533 time_t timeval;
534
535 mode = PARSE_MODE_NAME;
536 while (*buffer != '\n' && *buffer != '\0')
537 {
538 if (*buffer == ' ' || *buffer == '\t')
539 {
540 buffer++;
541 continue;
542 }
543
544 switch (mode)
545 {
546 case PARSE_MODE_NAME: /* Attribute Name */
547 rc_fieldcpy (attrstr, &buffer);
548 if ((attr =
549 rc_dict_findattr (attrstr)) == (DICT_ATTR *) NULL)
550 {
551 error("rc_avpair_parse: unknown attribute");
552 if (*first_pair) {
553 rc_avpair_free(*first_pair);
554 *first_pair = (VALUE_PAIR *) NULL;
555 }
556 return (-1);
557 }
558 mode = PARSE_MODE_EQUAL;
559 break;
560
561 case PARSE_MODE_EQUAL: /* Equal sign */
562 if (*buffer == '=')
563 {
564 mode = PARSE_MODE_VALUE;
565 buffer++;
566 }
567 else
568 {
569 error("rc_avpair_parse: missing or misplaced equal sign");
570 if (*first_pair) {
571 rc_avpair_free(*first_pair);
572 *first_pair = (VALUE_PAIR *) NULL;
573 }
574 return (-1);
575 }
576 break;
577
578 case PARSE_MODE_VALUE: /* Value */
579 rc_fieldcpy (valstr, &buffer);
580
581 if ((pair =
582 (VALUE_PAIR *) malloc (sizeof (VALUE_PAIR)))
583 == (VALUE_PAIR *) NULL)
584 {
585 novm("rc_avpair_parse");
586 if (*first_pair) {
587 rc_avpair_free(*first_pair);
588 *first_pair = (VALUE_PAIR *) NULL;
589 }
590 return (-1);
591 }
592 strcpy (pair->name, attr->name);
593 pair->attribute = attr->value;
594 pair->type = attr->type;
595 pair->vendorcode = attr->vendorcode;
596
597 switch (pair->type)
598 {
599
600 case PW_TYPE_STRING:
601 strcpy ((char*) pair->strvalue, valstr);
602 pair->lvalue = strlen(valstr);
603 break;
604
605 case PW_TYPE_INTEGER:
606 if (isdigit (*valstr))
607 {
608 pair->lvalue = atoi (valstr);
609 }
610 else
611 {
612 if ((dval = rc_dict_findval (valstr))
613 == (DICT_VALUE *) NULL)
614 {
615 error("rc_avpair_parse: unknown attribute value: %s", valstr);
616 if (*first_pair) {
617 rc_avpair_free(*first_pair);
618 *first_pair = (VALUE_PAIR *) NULL;
619 }
620 free (pair);
621 return (-1);
622 }
623 else
624 {
625 pair->lvalue = dval->value;
626 }
627 }
628 break;
629
630 case PW_TYPE_IPADDR:
631 pair->lvalue = rc_get_ipaddr(valstr);
632 break;
633
634 case PW_TYPE_DATE:
635 timeval = time (0);
636 tm = localtime (&timeval);
637 tm->tm_hour = 0;
638 tm->tm_min = 0;
639 tm->tm_sec = 0;
640 rc_str2tm (valstr, tm);
641 #ifdef TIMELOCAL
642 pair->lvalue = (UINT4) timelocal (tm);
643 #else /* TIMELOCAL */
644 pair->lvalue = (UINT4) mktime (tm);
645 #endif /* TIMELOCAL */
646 break;
647
648 default:
649 error("rc_avpair_parse: unknown attribute type %d", pair->type);
650 if (*first_pair) {
651 rc_avpair_free(*first_pair);
652 *first_pair = (VALUE_PAIR *) NULL;
653 }
654 free (pair);
655 return (-1);
656 }
657 pair->next = (VALUE_PAIR *) NULL;
658
659 if (*first_pair == (VALUE_PAIR *) NULL)
660 {
661 *first_pair = pair;
662 }
663 else
664 {
665 link = *first_pair;
666 while (link->next != (VALUE_PAIR *) NULL)
667 {
668 link = link->next;
669 }
670 link->next = pair;
671 }
672
673 mode = PARSE_MODE_NAME;
674 break;
675
676 default:
677 mode = PARSE_MODE_NAME;
678 break;
679 }
680 }
681 return (0);
682 }
683
684 /*
685 * Function: rc_avpair_tostr
686 *
687 * Purpose: Translate an av_pair into two strings
688 *
689 * Returns: 0 on success, -1 on failure
690 *
691 */
692
693 int rc_avpair_tostr (VALUE_PAIR *pair, char *name, int ln, char *value, int lv)
694 {
695 DICT_VALUE *dval;
696 char buffer[INET6_ADDRSTRLEN + 4]; // for a prefix: addr + '/' + prefixlen
697 struct in_addr inad;
698 unsigned char *ptr;
699 char *str;
700 time_t tmptime;
701
702 *name = *value = '\0';
703
704 if (!pair || pair->name[0] == '\0') {
705 error("rc_avpair_tostr: pair is NULL or empty");
706 return (-1);
707 }
708
709 strncpy(name, pair->name, (size_t) ln);
710
711 switch (pair->type)
712 {
713 case PW_TYPE_STRING:
714 lv--;
715 ptr = (unsigned char *) pair->strvalue;
716 while (*ptr != '\0')
717 {
718 if (!(isprint (*ptr)))
719 {
720 sprintf (buffer, "\\%03o", *ptr);
721 strncat(value, buffer, (size_t) lv);
722 lv -= 4;
723 if (lv < 0) break;
724 }
725 else
726 {
727 strncat(value, (char*) ptr, 1);
728 lv--;
729 if (lv < 0) break;
730 }
731 ptr++;
732 }
733 break;
734
735 case PW_TYPE_INTEGER:
736 dval = rc_dict_getval (pair->lvalue, pair->name);
737 if (dval != (DICT_VALUE *) NULL)
738 {
739 strncpy(value, dval->name, (size_t) lv-1);
740 }
741 else
742 {
743 sprintf (buffer, "%d", pair->lvalue);
744 strncpy(value, buffer, (size_t) lv);
745 }
746 break;
747
748 case PW_TYPE_IPADDR:
749 inad.s_addr = htonl(pair->lvalue);
750 strncpy (value, inet_ntoa (inad), (size_t) lv-1);
751 break;
752
753 case PW_TYPE_DATE:
754 tmptime = pair->lvalue;
755 strftime (buffer, sizeof (buffer), "%m/%d/%y %H:%M:%S",
756 gmtime (&tmptime));
757 strncpy(value, buffer, lv-1);
758 break;
759
760 case PW_TYPE_IFID:
761 ptr = pair->strvalue;
762 snprintf(buffer, sizeof (buffer), "%x:%x:%x:%x",
763 (ptr[0] << 8) + ptr[1], (ptr[2] << 8) + ptr[3],
764 (ptr[4] << 8) + ptr[5], (ptr[6] << 8) + ptr[7]);
765 strncpy(value, buffer, lv-1);
766 break;
767
768 case PW_TYPE_IPV6ADDR:
769 inet_ntop(AF_INET6, pair->strvalue, buffer, sizeof (buffer));
770 strncpy(value, buffer, lv-1);
771 break;
772
773 case PW_TYPE_IPV6PREFIX:
774 inet_ntop(AF_INET6, pair->strvalue + 2, buffer, sizeof (buffer));
775 str = buffer + strlen(buffer);
776 snprintf(str, sizeof (buffer) - (str - buffer), "/%d", *(pair->strvalue + 1));
777 strncpy(value, buffer, lv-1);
778 break;
779
780 default:
781 error("rc_avpair_tostr: unknown attribute type %d", pair->type);
782 return (-1);
783 break;
784 }
785
786 return 0;
787 }
788
789 /*
790 * Function: rc_avpair_readin
791 *
792 * Purpose: get a sequence of attribute value pairs from the file input
793 * and make them into a list of value_pairs
794 *
795 */
796
797 VALUE_PAIR *rc_avpair_readin(FILE *input)
798 {
799 VALUE_PAIR *vp = NULL;
800 char buffer[1024], *q;
801
802 while (fgets(buffer, sizeof(buffer), input) != NULL)
803 {
804 q = buffer;
805
806 while(*q && isspace(*q)) q++;
807
808 if ((*q == '\n') || (*q == '#') || (*q == '\0'))
809 continue;
810
811 if (rc_avpair_parse(q, &vp) < 0) {
812 error("rc_avpair_readin: malformed attribute: %s", buffer);
813 rc_avpair_free(vp);
814 return NULL;
815 }
816 }
817
818 return vp;
819 }