File: avpair.c

package info (click to toggle)
radcli 1.2.11-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 1,768 kB
  • sloc: ansic: 6,089; sh: 767; makefile: 190; perl: 110
file content (1065 lines) | stat: -rw-r--r-- 25,968 bytes parent folder | download
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
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
/*
 * Copyright (C) 2015 Nikos Mavrogiannopoulos
 * 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 lf@elemental.net
 * and I'll send you a copy.
 *
 */
#include <config.h>
#include <includes.h>
#include <radcli/radcli.h>
#include "util.h"

#define PARSE_MODE_NAME		0
#define PARSE_MODE_EQUAL	1
#define PARSE_MODE_VALUE	2
#define PARSE_MODE_INVALID	3

/**
 * @defgroup radcli-api Main API
 * @brief Main API Functions
 *
 * @{
 */

/** Adds an attribute-value pair to the given list
 *
 * See rc_avpair_assign() for the format of the data.
 *
 * @note It always appends the new pair to the end of the list.
 *
 * @param rh a handle to parsed configuration.
 * @param list a VALUE_PAIR array of values; initially must be NULL.
 * @param attrid The attribute of the pair to add (e.g., PW_USER_NAME).
 * @param pval the value (e.g., the actual username).
 * @param len the length of pval, or -1 if to calculate (in case of strings).
 * @param vendorpec The vendor ID in case of a vendor specific value - 0 otherwise.
 * @return pointer to added a/v pair upon success, NULL pointer upon failure.
 */
VALUE_PAIR *rc_avpair_add (rc_handle const *rh, VALUE_PAIR **list, int attrid, void const *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;

}

/** Removes an attribute-value pair from the given list
 *
 * See rc_avpair_assign() for the format of the data.
 *
 * @param list a VALUE_PAIR array of values
 * @param attrid The attribute of the pair to remove (e.g., PW_USER_NAME).
 * @param vendorpec The vendor ID in case of a vendor specific value - 0 otherwise.
 */
void rc_avpair_remove (VALUE_PAIR **list, int attrid, int vendorpec)
{
	VALUE_PAIR *vp, *prev, *tmp;
	int vattrid;

	if(vendorpec != VENDOR_NONE)
		vattrid = attrid | (vendorpec << 16);
	else
		vattrid = attrid;

	prev = NULL;
	vp = *list;
	while(vp != NULL) {
		if (vp->attribute == vattrid) {
			/* found */
			if (prev == NULL) { /* first one */
				tmp = vp;
				vp = tmp->next;
				free(tmp);
				*list = vp;
			} else { /* somewhere in the middle */
				prev->next = vp->next;
				free(vp);
			}
			break;
		}

		prev = vp;
		vp = vp->next;
	};

	return;
}

/** Iterates through the attribute-value pairs
 *
 * The attribute-value are organized in a linked-list, and this
 * function provides a way to iterate them given the first element
 * initially.
 *
 * @param t the current pair.
 * @return pointer to the next pair, or NULL when finished.
 */
VALUE_PAIR *rc_avpair_next (VALUE_PAIR *t)
{
	return t->next;
}

/** Assigns the given value to an attribute-value pair
 *
 * If the value is of type PW_TYPE_STRING it must either be
 * a null terminated string with len set to -1, or raw data
 * with length properly set. For PW_TYPE_DATE, PW_TYPE_INTEGER,
 * and PW_TYPE_IPADDR an uint32_t number should be set at pval.
 * For IPv4 addresses it should be in host byte order.
 *
 * For PW_TYPE_IPV6ADDR type a 16-byte long address is expected, and
 * for PW_TYPE_IPV6PREFIX the rfc3162 prefix format is expected. Simply
 * that is a zero byte, a byte with the value of prefix (e.g., 112), and
 * the remaining bytes are the IPv6 address.
 *
 * @param vp a pointer to a VALUE_PAIR structure.
 * @param pval the value (e.g., the actual username).
 * @param len the length of pval, or -1 if to calculate (in case of strings).
 * @return 0 on success or -1 on failure.
 */
int rc_avpair_assign (VALUE_PAIR *vp, void const *pval, int len)
{

	switch (vp->type)
	{
		case PW_TYPE_STRING:
			if (len == -1)
				len = (uint32_t)strlen((char const *)pval);
			if (len > AUTH_STRING_LEN) {
		        	rc_log(LOG_ERR, "rc_avpair_assign: bad attribute length");
		        	return -1;
			}
			memcpy(vp->strvalue, (char const *)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;
	        case PW_TYPE_IPV6ADDR:
			if (len != 16) {
		        	rc_log(LOG_ERR, "rc_avpair_assign: bad IPv6 length");
		        	return -1;
			}
			memcpy(vp->strvalue, (char const *)pval, len);
			vp->lvalue = len;
			break;

	        case PW_TYPE_IPV6PREFIX:
			if (len < 2 || len > 18) {
		        	rc_log(LOG_ERR, "rc_avpair_assign: bad IPv6 prefix length");
		        	return -1;
			}
			memcpy(vp->strvalue, (char const *)pval, len);
			vp->lvalue = len;
			break;

		default:
			rc_log(LOG_ERR, "rc_avpair_assign: no attribute %d in dictionary", vp->type);
			return -1;
	}
	return 0;
}

/** Make a new attribute-value pair with given parameters
 *
 * See rc_avpair_assign() for the format of the data.
 *
 * @param rh a handle to parsed configuration.
 * @param attrid The attribute of the pair to add (e.g., PW_USER_NAME).
 * @param pval the value (e.g., the actual username).
 * @param len the length of pval, or -1 if to calculate (in case of strings).
 * @param vendorpec The vendor ID in case of a vendor specific value - 0 otherwise.
 * @return pointer to generated a/v pair when successful, NULL when failure.
 */
VALUE_PAIR *rc_avpair_new (rc_handle const *rh, int attrid, void const *pval, int len, int vendorpec)
{
	VALUE_PAIR     *vp = NULL;
	DICT_ATTR      *pda;
	int vattrid;

	if(vendorpec != VENDOR_NONE) {
		vattrid = attrid | (vendorpec << 16);
	} else {
		vattrid = attrid;
	}
	if ((pda = rc_dict_getattr (rh, vattrid)) == NULL)
	{
		rc_log(LOG_ERR,"rc_avpair_new: no attribute %d/%u in dictionary", vendorpec, attrid);
		return NULL;
	}
	if (vendorpec != 0 && rc_dict_getvend(rh, vendorpec) == NULL)
	{
		rc_log(LOG_ERR,"rc_avpair_new: no Vendor-Id %d in dictionary", vendorpec);
		return NULL;
	}
	if ((vp = malloc (sizeof (VALUE_PAIR))) != NULL)
	{
		strlcpy (vp->name, pda->name, sizeof (vp->name));
		vp->attribute = vattrid;
		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;
}

/** Takes attribute/value pairs from buffer and builds a value_pair list using allocated memory
 *
 * @note Uses recursion.
 *
 * @param rh a handle to parsed configuration.
 * @param pair a pointer to a VALUE_PAIR structure.
 * @param ptr the value (e.g., the actual username).
 * @param length the length of ptr, or -1 if to calculate (in case of strings).
 * @param vendorpec The vendor ID in case of a vendor specific value - 0 otherwise.
 * @return value_pair list or NULL on failure.
 */
VALUE_PAIR *rc_avpair_gen(rc_handle const *rh, VALUE_PAIR *pair, unsigned char const *ptr,
			  int length, int vendorpec)
{
	int attribute, attrlen, x_len;
	unsigned char const *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);
	}

	/* 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 skipit;
		}
		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);
			goto skipit;
		}
		/* 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++) {
			snprintf(hex, sizeof(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 skipit;
	}

	rpair = calloc(1, sizeof(*rpair));
	if (rpair == NULL) {
		rc_log(LOG_CRIT, "rc_avpair_gen: out of memory");
		goto shithappens;
	}

	/* Insert this new pair at the beginning of the list */
	rpair->next = pair;
	pair = rpair;
	strlcpy(pair->name, attr->name, sizeof(pair->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 skipit;
		}
	case PW_TYPE_IPADDR:
		if (attrlen != 4) {
			rc_log(LOG_ERR, "rc_avpair_gen: received IPADDR"
			    " attribute with invalid length");
			goto skipit;
		}
		memcpy((char *)&lvalue, (char *)ptr, 4);
		pair->lvalue = ntohl(lvalue);
		break;
	case PW_TYPE_IPV6ADDR:
		if (attrlen != 16) {
			rc_log(LOG_ERR, "rc_avpair_gen: received IPV6ADDR"
			    " attribute with invalid length");
			goto skipit;
		}
		memcpy(pair->strvalue, (char *)ptr, 16);
		pair->lvalue = attrlen;
		break;
	case PW_TYPE_IPV6PREFIX:
		if (attrlen > 18 || attrlen < 2) {
			rc_log(LOG_ERR, "rc_avpair_gen: received IPV6PREFIX"
			    " attribute with invalid length: %d", attrlen);
			goto skipit;
		}
		memcpy(pair->strvalue, (char *)ptr, attrlen);
		pair->lvalue = attrlen;
		break;
	case PW_TYPE_DATE:
		if (attrlen != 4) {
			rc_log(LOG_ERR, "rc_avpair_gen: received DATE "
			    "attribute with invalid length");
			goto skipit;
		}

	default:
		rc_log(LOG_WARNING, "rc_avpair_gen: %s has unknown type",
		    attr->name);
		goto skipit;
	}

skipit:
	return pair;

shithappens:
	while (pair != NULL) {
		rpair = pair->next;
		free(pair);
		pair = rpair;
	}
	return NULL;
}

/** Find the first attribute value-pair (which matches the given attribute) from the specified value-pair list
 *
 * @param vp a pointer to a VALUE_PAIR structure.
 * @param attrid The attribute of the pair to find (e.g., PW_USER_NAME).
 * @param vendorpec The vendor ID in case of a vendor specific value - 0 otherwise.
 * @return the value pair found.
 */
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;
}

/** Return a copy of the existing list "p" ala strdup().
 *
 * @param p a pointer to a VALUE_PAIR structure.
 * @return the copy of "p".
 *
 */
VALUE_PAIR *rc_avpair_copy(VALUE_PAIR *p)
{
	VALUE_PAIR *vp, *fp = NULL, *lp = NULL;

	while (p) {
		vp = malloc(sizeof(VALUE_PAIR));
		if (!vp) {
			while(fp) { /* free allocated memory */
				vp = fp;
				fp = fp->next;
				free(vp);
			}
			return NULL;
		}
		*vp = *p;
		if (!fp)
			fp = vp;
		if (lp)
			lp->next = vp;
		lp = vp;
		p = p->next;
	}

	return fp;
}

/** Insert a VALUE_PAIR into a list
 *
 * 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".
 *
 * @param a a VALUE_PAIR array of values.
 * @param p a pointer to a VALUE_PAIR in a.
 * @param b The VALUE_PAIR pointer to add in 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;
		}
	}

	if (this_node) {
		b->next = this_node->next;
		this_node->next = b;
	}

	return;
}

/** Frees all value_pairs in the list
 *
 * @param pair a pointer to a VALUE_PAIR structure.
 */
void rc_avpair_free (VALUE_PAIR *pair)
{
	VALUE_PAIR     *next;

	while (pair != NULL)
	{
		next = pair->next;
		free (pair);
		pair = next;
	}
}

/** 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'.
 *
 * @param string the provided string to copy.
 * @param uptr the current position of the buffer.
 * @param stopat characters to which parsing should stop.
 * @param len the maximum length of string.
 */
static void rc_fieldcpy(char *string, char const **uptr, char const *stopat, size_t len)
{
	char const *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;
}

/** Parses the buffer to extract the attribute-value pairs
 *
 * @param rh a handle to parsed configuration.
 * @param buffer the buffer to be parsed.
 * @param first_pair an allocated array of values.
 * @return 0 on successful parse of attribute-value pair, or -1 on syntax (or other) error detected.
 */
int rc_avpair_parse (rc_handle const *rh, char const *buffer, VALUE_PAIR **first_pair)
{
	int             mode;
	char            attrstr[AUTH_ID_LEN];
	char            valstr[AUTH_STRING_LEN + 1], *p;
	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 = 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:
					if (inet_pton(AF_INET, valstr, &pair->lvalue) == 0) {
						rc_log(LOG_ERR, "rc_avpair_parse: invalid IPv4 address %s", valstr);
						free(pair);
						return -1;
					}

					pair->lvalue = ntohl(pair->lvalue);
				break;

				case PW_TYPE_IPV6ADDR:
					if (inet_pton(AF_INET6, valstr, pair->strvalue) == 0) {
						rc_log(LOG_ERR, "rc_avpair_parse: invalid IPv6 address %s", valstr);
						free(pair);
						return -1;
					}
					pair->lvalue = 16;
				break;

				case PW_TYPE_IPV6PREFIX:
					p = strchr(valstr, '/');
					if (p == NULL) {
						rc_log(LOG_ERR, "rc_avpair_parse: invalid IPv6 prefix %s", valstr);
						free(pair);
						return -1;
					}
					*p = 0;
					p++;
					pair->strvalue[0] = 0;
					pair->strvalue[1] = atoi(p);

					if (inet_pton(AF_INET6, valstr, pair->strvalue+2) == 0) {
						rc_log(LOG_ERR, "rc_avpair_parse: invalid IPv6 prefix %s", valstr);
						free(pair);
						return -1;
					}
					pair->lvalue = 2+16;
				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;
				break;
			default:
				break;
			}

			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;
}

/** Translate an av_pair into printable strings
 *
 * @param rh a handle to parsed configuration.
 * @param pair a pointer to a VALUE_PAIR structure.
 * @param name the name of the pair.
 * @param ln the size of name.
 * @param value the value of the pair.
 * @param lv the size of value.
 * @return 0 on success, -1 on failure.
 */
int rc_avpair_tostr (rc_handle const *rh, VALUE_PAIR *pair, char *name, int ln, char *value, int lv)
{
	DICT_VALUE     *dval;
	struct in_addr  inad;
	unsigned char  *ptr;
	unsigned int    pos;
	unsigned int    slen;

	*name = *value = '\0';

	if (!pair || pair->name[0] == '\0') {
		rc_log(LOG_ERR, "rc_avpair_tostr: pair is NULL or empty");
		return -1;
	}

	strlcpy(name, pair->name, (size_t) ln);

	switch (pair->type)
	{
		case PW_TYPE_STRING:
		lv--;
		pos = 0;
		ptr = (unsigned char *) pair->strvalue;
		if (pair->attribute == PW_DIGEST_ATTRIBUTES) {
			slen = ptr[1] - 2;
			ptr += 2;
		} else {
			slen = pair->lvalue;
		}
		while (slen-- > 0)
		{
			if (!(isprint (*ptr)))
			{
				if (lv >= 4) {
					snprintf (&value[pos], lv, "\\%03o", *ptr);
					pos += 4;
					lv -= 4;
				} else {
					break;
				}
			}
			else
			{
				if (lv > 0) {
					value[pos++] = *ptr;
					lv--;
				} else {
					break;
				}
			}
			ptr++;
		}
		if (lv > 0)
			value[pos++] = 0;
		else
			value[pos-1] = 0;
		break;

		case PW_TYPE_INTEGER:
		dval = rc_dict_getval (rh, pair->lvalue, pair->name);
		if (dval != NULL)
		{
			strlcpy(value, dval->name, (size_t) lv);
		}
		else
		{
			snprintf(value, lv, "%ld", (long int)pair->lvalue);
		}
		break;

		case PW_TYPE_IPADDR:
		inad.s_addr = htonl(pair->lvalue);
		strlcpy (value, inet_ntoa (inad), (size_t) lv);
		break;

		case PW_TYPE_IPV6ADDR:
			if (inet_ntop(AF_INET6, pair->strvalue, value, lv) == NULL)
				return -1;
		break;

		case PW_TYPE_IPV6PREFIX: {
			uint8_t ip[16];
			uint8_t txt[48];
			if (pair->lvalue < 2)
				return -1;

			memset(ip, 0, sizeof(ip));
			memcpy(ip, pair->strvalue+2, pair->lvalue-2);

			if (inet_ntop(AF_INET6, ip, (void*)txt, sizeof(txt)) == NULL)
				return -1;
		snprintf(value, lv, "%s/%u", txt, (unsigned)pair->strvalue[1]);

		break;
		}
		case PW_TYPE_DATE:
		strftime (value, lv, "%m/%d/%y %H:%M:%S",
			  gmtime ((time_t *) & pair->lvalue));
		break;

		default:
		rc_log(LOG_ERR, "rc_avpair_tostr: unknown attribute type %d", pair->type);
		return -1;
		break;
	}

	return 0;
}

/** Format a sequence of attribute value pairs into a printable string
 *
 * The caller should provide a storage buffer and the buffer length.
 *
 * @param rh a handle to parsed configuration.
 * @param pair a pointer to a VALUE_PAIR structure.
 * @param buf will hold the string output of the pair.
 * @param buf_len the size of buf.
 * @return a pointer to provided storage buffer.
 */
char *rc_avpair_log(rc_handle const *rh, VALUE_PAIR *pair, char *buf, size_t buf_len)
{
	size_t len, nlen;
	VALUE_PAIR *vp;
	char name[33], value[256];

	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;
		if(nlen<buf_len-1) {
			sprintf(buf + len, "%-32s = '%s'\n", name, value);
		} else return buf;
		len = nlen - 1;
	}
	return buf;
}

/** Get the integer value of the given attribute value-pair
 *
 * This function is valid for PW_TYPE_INTEGER, PW_TYPE_IPADDR.
 * PW_TYPE_DATE. In PW_TYPE_IPADDR this value will contain the
 * IPv4 address in host by order.
 *
 * @param vp a pointer to a VALUE_PAIR structure.
 * @param res The integer value returned.
 * @return zero on success or -1 on failure.
 */
int rc_avpair_get_uint32 (VALUE_PAIR *vp, uint32_t *res)
{
	if (vp->type == PW_TYPE_DATE || vp->type == PW_TYPE_IPADDR ||
	    vp->type == PW_TYPE_INTEGER) {
			if (res)
				*res = vp->lvalue;
		return 0;
	} else {
		return -1;
	}
}

/** Get the IPv6 address and prefix value of the given attribute value-pair
 *
 * This function is valid for PW_TYPE_IPV6ADDR, PW_TYPE_IPV6PREFIX.
 *
 * @param vp a pointer to a VALUE_PAIR structure.
 * @param res An in6_addr structure for result to be copied in.
 * @param prefix If of type PW_TYPE_IPV6PREFIX the prefix will be copied (may be NULL).
 * @return zero on success or -1 on failure.
 */
int rc_avpair_get_in6 (VALUE_PAIR *vp, struct in6_addr *res, unsigned *prefix)
{
	if (vp->type == PW_TYPE_IPV6ADDR) {
		memcpy(res, vp->strvalue, 16);
		return 0;
	} else if (vp->type == PW_TYPE_IPV6PREFIX) {
			if (vp->lvalue < 2 || vp->lvalue > 18)
				return -1;

		if (res) {
			memset(res, 0, 16);
			memcpy(res, vp->strvalue+2, vp->lvalue-2);
		}

		if (prefix)
			*prefix = (unsigned char)vp->strvalue[1];
		return 0;
	}

	return -1;
}

/** Get the raw value of the given attribute value-pair
 *
 * This function is valid for PW_TYPE_STRING, PW_TYPE_IPV6ADDR,
 * PW_TYPE_IPV6PREFIX.
 *
 * @param vp a pointer to a VALUE_PAIR structure.
 * @param res Will contain pointer to the data value.
 * @param res_size Will contain the data size.
 * @return zero on success or -1 on failure.
 */
int rc_avpair_get_raw (VALUE_PAIR *vp, char **res, unsigned *res_size)
{
	if (vp->type == PW_TYPE_STRING || vp->type == PW_TYPE_IPV6ADDR ||
		vp->type == PW_TYPE_IPV6PREFIX) {
			if (res)
				*res = vp->strvalue;
		if (res_size)
			*res_size = vp->lvalue;
		return 0;
	} else {
		return -1;
	}
}

/** Get the attribute ID and type of the given attribute value-pair
 *
 * @param vp a pointer to a VALUE_PAIR structure.
 * @param type The attribute type, of type rc_attr_type
 * @param id The attribute identifier, of type rc_attr_id
 */
void rc_avpair_get_attr (VALUE_PAIR *vp, unsigned *type, unsigned *id)
{
	if (type)
		*type = vp->type;
	if (id)
		*id = vp->attribute;
}

/** @} */
/*
 * Local Variables:
 * c-basic-offset:8
 * c-style: whitesmith
 * End:
 */