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