/
radius.c
4719 lines (3990 loc) · 106 KB
/
radius.c
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
/*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
/**
* $Id$
*
* @file radius.c
* @brief Functions to send/receive radius packets.
*
* @copyright 2000-2003,2006 The FreeRADIUS server project
*/
RCSID("$Id$")
#include <freeradius-devel/libradius.h>
#include <freeradius-devel/md5.h>
#include <fcntl.h>
#include <ctype.h>
#ifdef WITH_UDPFROMTO
#include <freeradius-devel/udpfromto.h>
#endif
/*
* Some messages get printed out only in debugging mode.
*/
#define FR_DEBUG_STRERROR_PRINTF if (fr_debug_lvl) fr_strerror_printf
#if 0
#define VP_TRACE printf
static void VP_HEXDUMP(char const *msg, uint8_t const *data, size_t len)
{
size_t i;
printf("--- %s ---\n", msg);
for (i = 0; i < len; i++) {
if ((i & 0x0f) == 0) printf("%04x: ", (unsigned int) i);
printf("%02x ", data[i]);
if ((i & 0x0f) == 0x0f) printf("\n");
}
if ((len == 0x0f) || ((len & 0x0f) != 0x0f)) printf("\n");
fflush(stdout);
}
#else
#define VP_TRACE(_x, ...)
#define VP_HEXDUMP(_x, _y, _z)
#endif
/*
* The RFC says 4096 octets max, and most packets are less than 256.
*/
#define MAX_PACKET_LEN 4096
/*
* The maximum number of attributes which we allow in an incoming
* request. If there are more attributes than this, the request
* is rejected.
*
* This helps to minimize the potential for a DoS, when an
* attacker spoofs Access-Request packets, which don't have a
* Message-Authenticator attribute. This means that the packet
* is unsigned, and the attacker can use resources on the server,
* even if the end request is rejected.
*/
uint32_t fr_max_attributes = 0;
FILE *fr_log_fp = NULL;
typedef struct radius_packet_t {
uint8_t code;
uint8_t id;
uint8_t length[2];
uint8_t vector[AUTH_VECTOR_LEN];
uint8_t data[1];
} radius_packet_t;
static fr_randctx fr_rand_pool; /* across multiple calls */
static int fr_rand_initialized = 0;
static unsigned int salt_offset = 0;
static uint8_t nullvector[AUTH_VECTOR_LEN] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; /* for CoA decode */
char const *fr_packet_codes[FR_MAX_PACKET_CODE] = {
"", //!< 0
"Access-Request",
"Access-Accept",
"Access-Reject",
"Accounting-Request",
"Accounting-Response",
"Accounting-Status",
"Password-Request",
"Password-Accept",
"Password-Reject",
"Accounting-Message", //!< 10
"Access-Challenge",
"Status-Server",
"Status-Client",
"14",
"15",
"16",
"17",
"18",
"19",
"20", //!< 20
"Resource-Free-Request",
"Resource-Free-Response",
"Resource-Query-Request",
"Resource-Query-Response",
"Alternate-Resource-Reclaim-Request",
"NAS-Reboot-Request",
"NAS-Reboot-Response",
"28",
"Next-Passcode",
"New-Pin", //!< 30
"Terminate-Session",
"Password-Expired",
"Event-Request",
"Event-Response",
"35",
"36",
"37",
"38",
"39",
"Disconnect-Request", //!< 40
"Disconnect-ACK",
"Disconnect-NAK",
"CoA-Request",
"CoA-ACK",
"CoA-NAK",
"46",
"47",
"48",
"49",
"IP-Address-Allocate",
"IP-Address-Release", //!< 50
};
void fr_printf_log(char const *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
if ((fr_debug_lvl == 0) || !fr_log_fp) {
va_end(ap);
return;
}
vfprintf(fr_log_fp, fmt, ap);
va_end(ap);
return;
}
static char const tabs[] = "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t";
static void print_hex_data(uint8_t const *ptr, int attrlen, int depth)
{
int i;
for (i = 0; i < attrlen; i++) {
if ((i > 0) && ((i & 0x0f) == 0x00))
fprintf(fr_log_fp, "%.*s", depth, tabs);
fprintf(fr_log_fp, "%02x ", ptr[i]);
if ((i & 0x0f) == 0x0f) fprintf(fr_log_fp, "\n");
}
if ((i & 0x0f) != 0) fprintf(fr_log_fp, "\n");
}
void rad_print_hex(RADIUS_PACKET *packet)
{
int i;
if (!packet->data || !fr_log_fp) return;
fprintf(fr_log_fp, " Code:\t\t%u\n", packet->data[0]);
fprintf(fr_log_fp, " Id:\t\t%u\n", packet->data[1]);
fprintf(fr_log_fp, " Length:\t%u\n", ((packet->data[2] << 8) |
(packet->data[3])));
fprintf(fr_log_fp, " Vector:\t");
for (i = 4; i < 20; i++) {
fprintf(fr_log_fp, "%02x", packet->data[i]);
}
fprintf(fr_log_fp, "\n");
if (packet->data_len > 20) {
int total;
uint8_t const *ptr;
fprintf(fr_log_fp, " Data:");
total = packet->data_len - 20;
ptr = packet->data + 20;
while (total > 0) {
int attrlen;
unsigned int vendor = 0;
fprintf(fr_log_fp, "\t\t");
if (total < 2) { /* too short */
fprintf(fr_log_fp, "%02x\n", *ptr);
break;
}
if (ptr[1] > total) { /* too long */
for (i = 0; i < total; i++) {
fprintf(fr_log_fp, "%02x ", ptr[i]);
}
break;
}
fprintf(fr_log_fp, "%02x %02x ", ptr[0], ptr[1]);
attrlen = ptr[1] - 2;
if ((ptr[0] == PW_VENDOR_SPECIFIC) &&
(attrlen > 4)) {
vendor = (ptr[3] << 16) | (ptr[4] << 8) | ptr[5];
fprintf(fr_log_fp, "%02x%02x%02x%02x (%u) ",
ptr[2], ptr[3], ptr[4], ptr[5], vendor);
attrlen -= 4;
ptr += 6;
total -= 6;
} else {
ptr += 2;
total -= 2;
}
print_hex_data(ptr, attrlen, 3);
ptr += attrlen;
total -= attrlen;
}
}
fflush(stdout);
}
/** Wrapper for sendto which handles sendfromto, IPv6, and all possible combinations
*
*/
static int rad_sendto(int sockfd, void *data, size_t data_len, int flags,
#ifdef WITH_UDPFROMTO
fr_ipaddr_t *src_ipaddr, uint16_t src_port,
#else
UNUSED fr_ipaddr_t *src_ipaddr, UNUSED uint16_t src_port,
#endif
fr_ipaddr_t *dst_ipaddr, uint16_t dst_port)
{
int rcode;
struct sockaddr_storage dst;
socklen_t sizeof_dst;
#ifdef WITH_UDPFROMTO
struct sockaddr_storage src;
socklen_t sizeof_src;
fr_ipaddr2sockaddr(src_ipaddr, src_port, &src, &sizeof_src);
#endif
if (!fr_ipaddr2sockaddr(dst_ipaddr, dst_port, &dst, &sizeof_dst)) {
return -1;
}
#ifdef WITH_UDPFROMTO
/*
* And if they don't specify a source IP address, don't
* use udpfromto.
*/
if (((dst_ipaddr->af == AF_INET) || (dst_ipaddr->af == AF_INET6)) &&
(src_ipaddr->af != AF_UNSPEC) &&
!fr_inaddr_any(src_ipaddr)) {
rcode = sendfromto(sockfd, data, data_len, flags,
(struct sockaddr *)&src, sizeof_src,
(struct sockaddr *)&dst, sizeof_dst);
goto done;
}
#endif
/*
* No udpfromto, fail gracefully.
*/
rcode = sendto(sockfd, data, data_len, flags,
(struct sockaddr *) &dst, sizeof_dst);
#ifdef WITH_UDPFROMTO
done:
#endif
if (rcode < 0) {
fr_strerror_printf("sendto failed: %s", fr_syserror(errno));
}
return rcode;
}
void rad_recv_discard(int sockfd)
{
uint8_t header[4];
struct sockaddr_storage src;
socklen_t sizeof_src = sizeof(src);
(void) recvfrom(sockfd, header, sizeof(header), 0,
(struct sockaddr *)&src, &sizeof_src);
}
/** Basic validation of RADIUS packet header
*
* @note fr_strerror errors are only available if fr_debug_lvl > 0. This is to reduce CPU time
* consumed when discarding malformed packet.
*
* @param[in] sockfd we're reading from.
* @param[out] src_ipaddr of the packet.
* @param[out] src_port of the packet.
* @param[out] code Pointer to where to write the packet code.
* @return
* - -1 on failure.
* - 1 on decode error.
* - >= RADIUS_HDR_LEN on success. This is the packet length as specified in the header.
*/
ssize_t rad_recv_header(int sockfd, fr_ipaddr_t *src_ipaddr, uint16_t *src_port, int *code)
{
ssize_t data_len, packet_len;
uint8_t header[4];
struct sockaddr_storage src;
socklen_t sizeof_src = sizeof(src);
data_len = recvfrom(sockfd, header, sizeof(header), MSG_PEEK,
(struct sockaddr *)&src, &sizeof_src);
if (data_len < 0) {
if ((errno == EAGAIN) || (errno == EINTR)) return 0;
return -1;
}
/*
* Too little data is available, discard the packet.
*/
if (data_len < 4) {
FR_DEBUG_STRERROR_PRINTF("Expected at least 4 bytes of header data, got %zu bytes", data_len);
rad_recv_discard(sockfd);
return 1;
} else { /* we got 4 bytes of data. */
/*
* See how long the packet says it is.
*/
packet_len = (header[2] * 256) + header[3];
/*
* The length in the packet says it's less than
* a RADIUS header length: discard it.
*/
if (packet_len < RADIUS_HDR_LEN) {
FR_DEBUG_STRERROR_PRINTF("Expected at least " STRINGIFY(RADIUS_HDR_LEN) " bytes of packet "
"data, got %zu bytes", packet_len);
rad_recv_discard(sockfd);
return 1;
/*
* Enforce RFC requirements, for sanity.
* Anything after 4k will be discarded.
*/
} else if (packet_len > MAX_PACKET_LEN) {
FR_DEBUG_STRERROR_PRINTF("Length field value too large, expected maximum of "
STRINGIFY(MAX_PACKET_LEN) " bytes, got %zu bytes", packet_len);
rad_recv_discard(sockfd);
return 1;
}
}
/*
* Convert AF. If unknown, discard packet.
*/
if (!fr_sockaddr2ipaddr(&src, sizeof_src, src_ipaddr, src_port)) {
FR_DEBUG_STRERROR_PRINTF("Unkown address family");
rad_recv_discard(sockfd);
return 1;
}
*code = header[0];
/*
* The packet says it's this long, but the actual UDP
* size could still be smaller.
*/
return packet_len;
}
/** Wrapper for recvfrom, which handles recvfromto, IPv6, and all possible combinations
*
*/
static ssize_t rad_recvfrom(int sockfd, RADIUS_PACKET *packet, int flags,
fr_ipaddr_t *src_ipaddr, uint16_t *src_port,
fr_ipaddr_t *dst_ipaddr, uint16_t *dst_port)
{
struct sockaddr_storage src;
struct sockaddr_storage dst;
socklen_t sizeof_src = sizeof(src);
socklen_t sizeof_dst = sizeof(dst);
ssize_t data_len;
uint8_t header[4];
size_t len;
uint16_t port;
memset(&src, 0, sizeof_src);
memset(&dst, 0, sizeof_dst);
/*
* Read the length of the packet, from the packet.
* This lets us allocate the buffer to use for
* reading the rest of the packet.
*/
data_len = recvfrom(sockfd, header, sizeof(header), MSG_PEEK,
(struct sockaddr *)&src, &sizeof_src);
if (data_len < 0) {
if ((errno == EAGAIN) || (errno == EINTR)) return 0;
return -1;
}
/*
* Too little data is available, discard the packet.
*/
if (data_len < 4) {
rad_recv_discard(sockfd);
return 0;
} else { /* we got 4 bytes of data. */
/*
* See how long the packet says it is.
*/
len = (header[2] * 256) + header[3];
/*
* The length in the packet says it's less than
* a RADIUS header length: discard it.
*/
if (len < RADIUS_HDR_LEN) {
recvfrom(sockfd, header, sizeof(header), flags,
(struct sockaddr *)&src, &sizeof_src);
return 0;
/*
* Enforce RFC requirements, for sanity.
* Anything after 4k will be discarded.
*/
} else if (len > MAX_PACKET_LEN) {
recvfrom(sockfd, header, sizeof(header), flags,
(struct sockaddr *)&src, &sizeof_src);
return len;
}
}
packet->data = talloc_array(packet, uint8_t, len);
if (!packet->data) return -1;
/*
* Receive the packet. The OS will discard any data in the
* packet after "len" bytes.
*/
#ifdef WITH_UDPFROMTO
data_len = recvfromto(sockfd, packet->data, len, flags,
(struct sockaddr *)&src, &sizeof_src,
(struct sockaddr *)&dst, &sizeof_dst);
#else
data_len = recvfrom(sockfd, packet->data, len, flags,
(struct sockaddr *)&src, &sizeof_src);
/*
* Get the destination address, too.
*/
if (getsockname(sockfd, (struct sockaddr *)&dst,
&sizeof_dst) < 0) return -1;
#endif
if (data_len < 0) {
return data_len;
}
if (!fr_sockaddr2ipaddr(&src, sizeof_src, src_ipaddr, &port)) {
return -1; /* Unknown address family, Die Die Die! */
}
*src_port = port;
fr_sockaddr2ipaddr(&dst, sizeof_dst, dst_ipaddr, &port);
*dst_port = port;
/*
* Different address families should never happen.
*/
if (src.ss_family != dst.ss_family) {
return -1;
}
return data_len;
}
#define AUTH_PASS_LEN (AUTH_VECTOR_LEN)
/** Build an encrypted secret value to return in a reply packet
*
* The secret is hidden by xoring with a MD5 digest created from
* the shared secret and the authentication vector.
* We put them into MD5 in the reverse order from that used when
* encrypting passwords to RADIUS.
*/
static void make_secret(uint8_t *digest, uint8_t const *vector,
char const *secret, uint8_t const *value)
{
FR_MD5_CTX context;
int i;
fr_md5_init(&context);
fr_md5_update(&context, vector, AUTH_VECTOR_LEN);
fr_md5_update(&context, (uint8_t const *) secret, strlen(secret));
fr_md5_final(digest, &context);
for ( i = 0; i < AUTH_VECTOR_LEN; i++ ) {
digest[i] ^= value[i];
}
}
#define MAX_PASS_LEN (128)
static void make_passwd(uint8_t *output, ssize_t *outlen,
uint8_t const *input, size_t inlen,
char const *secret, uint8_t const *vector)
{
FR_MD5_CTX context, old;
uint8_t digest[AUTH_VECTOR_LEN];
uint8_t passwd[MAX_PASS_LEN];
size_t i, n;
size_t len;
/*
* If the length is zero, round it up.
*/
len = inlen;
if (len > MAX_PASS_LEN) len = MAX_PASS_LEN;
memcpy(passwd, input, len);
if (len < sizeof(passwd)) memset(passwd + len, 0, sizeof(passwd) - len);
if (len == 0) {
len = AUTH_PASS_LEN;
}
else if ((len & 0x0f) != 0) {
len += 0x0f;
len &= ~0x0f;
}
*outlen = len;
fr_md5_init(&context);
fr_md5_update(&context, (uint8_t const *) secret, strlen(secret));
old = context;
/*
* Do first pass.
*/
fr_md5_update(&context, vector, AUTH_PASS_LEN);
for (n = 0; n < len; n += AUTH_PASS_LEN) {
if (n > 0) {
context = old;
fr_md5_update(&context,
passwd + n - AUTH_PASS_LEN,
AUTH_PASS_LEN);
}
fr_md5_final(digest, &context);
for (i = 0; i < AUTH_PASS_LEN; i++) {
passwd[i + n] ^= digest[i];
}
}
memcpy(output, passwd, len);
}
static void make_tunnel_passwd(uint8_t *output, ssize_t *outlen,
uint8_t const *input, size_t inlen, size_t room,
char const *secret, uint8_t const *vector)
{
FR_MD5_CTX context, old;
uint8_t digest[AUTH_VECTOR_LEN];
uint8_t passwd[MAX_STRING_LEN + AUTH_VECTOR_LEN];
int i, n;
int len;
/*
* Be paranoid.
*/
if (room > 253) room = 253;
/*
* Account for 2 bytes of the salt, and round the room
* available down to the nearest multiple of 16. Then,
* subtract one from that to account for the length byte,
* and the resulting number is the upper bound on the data
* to copy.
*
* We could short-cut this calculation just be forcing
* inlen to be no more than 239. It would work for all
* VSA's, as we don't pack multiple VSA's into one
* attribute.
*
* However, this calculation is more general, if a little
* complex. And it will work in the future for all possible
* kinds of weird attribute packing.
*/
room -= 2;
room -= (room & 0x0f);
room--;
if (inlen > room) inlen = room;
/*
* Length of the encrypted data is password length plus
* one byte for the length of the password.
*/
len = inlen + 1;
if ((len & 0x0f) != 0) {
len += 0x0f;
len &= ~0x0f;
}
*outlen = len + 2; /* account for the salt */
/*
* Copy the password over.
*/
memcpy(passwd + 3, input, inlen);
memset(passwd + 3 + inlen, 0, sizeof(passwd) - 3 - inlen);
/*
* Generate salt. The RFC's say:
*
* The high bit of salt[0] must be set, each salt in a
* packet should be unique, and they should be random
*
* So, we set the high bit, add in a counter, and then
* add in some CSPRNG data. should be OK..
*/
passwd[0] = (0x80 | ( ((salt_offset++) & 0x0f) << 3) |
(fr_rand() & 0x07));
passwd[1] = fr_rand();
passwd[2] = inlen; /* length of the password string */
fr_md5_init(&context);
fr_md5_update(&context, (uint8_t const *) secret, strlen(secret));
old = context;
fr_md5_update(&context, vector, AUTH_VECTOR_LEN);
fr_md5_update(&context, &passwd[0], 2);
for (n = 0; n < len; n += AUTH_PASS_LEN) {
if (n > 0) {
context = old;
fr_md5_update(&context,
passwd + 2 + n - AUTH_PASS_LEN,
AUTH_PASS_LEN);
}
fr_md5_final(digest, &context);
for (i = 0; i < AUTH_PASS_LEN; i++) {
passwd[i + 2 + n] ^= digest[i];
}
}
memcpy(output, passwd, len + 2);
}
static int do_next_tlv(VALUE_PAIR const *vp, VALUE_PAIR const *next, int nest)
{
unsigned int tlv1, tlv2;
if (nest > fr_attr_max_tlv) return 0;
if (!vp) return 0;
/*
* Keep encoding TLVs which have the same scope.
* e.g. two attributes of:
* ATTR.TLV1.TLV2.TLV3 = data1
* ATTR.TLV1.TLV2.TLV4 = data2
* both get put into a container of "ATTR.TLV1.TLV2"
*/
/*
* Nothing to follow, we're done.
*/
if (!next) return 0;
/*
* Not from the same vendor, skip it.
*/
if (vp->da->vendor != next->da->vendor) return 0;
/*
* In a different TLV space, skip it.
*/
tlv1 = vp->da->attr;
tlv2 = next->da->attr;
tlv1 &= ((1 << fr_attr_shift[nest]) - 1);
tlv2 &= ((1 << fr_attr_shift[nest]) - 1);
if (tlv1 != tlv2) return 0;
return 1;
}
static ssize_t vp2data_any(RADIUS_PACKET const *packet,
RADIUS_PACKET const *original,
char const *secret, int nest,
VALUE_PAIR const **pvp,
uint8_t *start, size_t room);
static ssize_t vp2attr_rfc(RADIUS_PACKET const *packet,
RADIUS_PACKET const *original,
char const *secret, VALUE_PAIR const **pvp,
unsigned int attribute, uint8_t *ptr, size_t room);
/** Encode the *data* portion of the TLV
*
* This is really a sub-function of vp2data_any(). It encodes the *data* portion
* of the TLV, and assumes that the encapsulating attribute has already been encoded.
*/
static ssize_t vp2data_tlvs(RADIUS_PACKET const *packet,
RADIUS_PACKET const *original,
char const *secret, int nest,
VALUE_PAIR const **pvp,
uint8_t *start, size_t room)
{
ssize_t len;
size_t my_room;
uint8_t *ptr = start;
VALUE_PAIR const *vp = *pvp;
VALUE_PAIR const *svp = vp;
if (!svp) return 0;
#ifndef NDEBUG
if (nest > fr_attr_max_tlv) {
fr_strerror_printf("vp2data_tlvs: attribute nesting overflow");
return -1;
}
#endif
while (vp) {
VERIFY_VP(vp);
if (room <= 2) return ptr - start;
ptr[0] = (vp->da->attr >> fr_attr_shift[nest]) & fr_attr_mask[nest];
ptr[1] = 2;
my_room = room;
if (room > 255) my_room = 255;
len = vp2data_any(packet, original, secret, nest,
&vp, ptr + 2, my_room - 2);
if (len < 0) return len;
if (len == 0) return ptr - start;
/* len can NEVER be more than 253 */
ptr[1] += len;
#ifndef NDEBUG
if ((fr_debug_lvl > 3) && fr_log_fp) {
fprintf(fr_log_fp, "\t\t%02x %02x ", ptr[0], ptr[1]);
print_hex_data(ptr + 2, len, 3);
}
#endif
room -= ptr[1];
ptr += ptr[1];
*pvp = vp;
if (!do_next_tlv(svp, vp, nest)) break;
}
#ifndef NDEBUG
if ((fr_debug_lvl > 3) && fr_log_fp) {
DICT_ATTR const *da;
da = dict_attrbyvalue(svp->da->attr & ((1 << fr_attr_shift[nest ]) - 1), svp->da->vendor);
if (da) fprintf(fr_log_fp, "\t%s = ...\n", da->name);
}
#endif
return ptr - start;
}
/** Encodes the data portion of an attribute
*
* @return
* - Length of the data portion.
* - -1 on failure.
*/
static ssize_t vp2data_any(RADIUS_PACKET const *packet,
RADIUS_PACKET const *original,
char const *secret, int nest,
VALUE_PAIR const **pvp,
uint8_t *start, size_t room)
{
uint32_t lvalue;
ssize_t len;
uint8_t const *data;
uint8_t *ptr = start;
uint8_t array[4];
uint64_t lvalue64;
VALUE_PAIR const *vp = *pvp;
VERIFY_VP(vp);
/*
* See if we need to encode a TLV. The low portion of
* the attribute has already been placed into the packer.
* If there are still attribute bytes left, then go
* encode them as TLVs.
*
* If we cared about the stack, we could unroll the loop.
*/
if (vp->da->flags.is_tlv && (nest < fr_attr_max_tlv) &&
((vp->da->attr >> fr_attr_shift[nest + 1]) != 0)) {
return vp2data_tlvs(packet, original, secret, nest + 1, pvp,
start, room);
}
/*
* Set up the default sources for the data.
*/
len = vp->vp_length;
switch (vp->da->type) {
case PW_TYPE_STRING:
case PW_TYPE_OCTETS:
case PW_TYPE_TLV:
data = vp->data.ptr;
if (!data) {
fr_strerror_printf("ERROR: Cannot encode NULL data");
return -1;
}
break;
case PW_TYPE_IFID:
case PW_TYPE_IPV4_ADDR:
case PW_TYPE_IPV6_ADDR:
case PW_TYPE_IPV6_PREFIX:
case PW_TYPE_IPV4_PREFIX:
case PW_TYPE_ABINARY:
case PW_TYPE_ETHERNET: /* just in case */
data = (uint8_t const *) &vp->data;
break;
case PW_TYPE_BYTE:
len = 1; /* just in case */
array[0] = vp->vp_byte;
data = array;
break;
case PW_TYPE_SHORT:
len = 2; /* just in case */
array[0] = (vp->vp_short >> 8) & 0xff;
array[1] = vp->vp_short & 0xff;
data = array;
break;
case PW_TYPE_INTEGER:
len = 4; /* just in case */
lvalue = htonl(vp->vp_integer);
memcpy(array, &lvalue, sizeof(lvalue));
data = array;
break;
case PW_TYPE_INTEGER64:
len = 8; /* just in case */
lvalue64 = htonll(vp->vp_integer64);
data = (uint8_t *) &lvalue64;
break;
/*
* There are no tagged date attributes.
*/
case PW_TYPE_DATE:
lvalue = htonl(vp->vp_date);
data = (uint8_t const *) &lvalue;
len = 4; /* just in case */
break;
case PW_TYPE_SIGNED:
{
int32_t slvalue;
len = 4; /* just in case */
slvalue = htonl(vp->vp_signed);
memcpy(array, &slvalue, sizeof(slvalue));
data = array;
break;
}
default: /* unknown type: ignore it */
fr_strerror_printf("ERROR: Unknown attribute type %d", vp->da->type);
return -1;
}
/*
* No data: skip it.
*/
if (len == 0) {
*pvp = vp->next;
return 0;
}
/*
* Bound the data to the calling size
*/
if (len > (ssize_t) room) len = room;
/*
* Encrypt the various password styles
*
* Attributes with encrypted values MUST be less than
* 128 bytes long.
*/
switch (vp->da->flags.encrypt) {
case FLAG_ENCRYPT_USER_PASSWORD:
make_passwd(ptr, &len, data, len,
secret, packet->vector);
break;
case FLAG_ENCRYPT_TUNNEL_PASSWORD:
lvalue = 0;
if (vp->da->flags.has_tag) lvalue = 1;
/*
* Check if there's enough room. If there isn't,
* we discard the attribute.
*
* This is ONLY a problem if we have multiple VSA's
* in one Vendor-Specific, though.
*/
if (room < (18 + lvalue)) return 0;
switch (packet->code) {
case PW_CODE_ACCESS_ACCEPT:
case PW_CODE_ACCESS_REJECT:
case PW_CODE_ACCESS_CHALLENGE:
default:
if (!original) {
fr_strerror_printf("ERROR: No request packet, cannot encrypt %s attribute in the vp.", vp->da->name);
return -1;
}
if (lvalue) ptr[0] = TAG_VALID(vp->tag) ? vp->tag : TAG_NONE;
make_tunnel_passwd(ptr + lvalue, &len, data, len,
room - lvalue,
secret, original->vector);
break;
case PW_CODE_ACCOUNTING_REQUEST:
case PW_CODE_DISCONNECT_REQUEST:
case PW_CODE_COA_REQUEST:
ptr[0] = TAG_VALID(vp->tag) ? vp->tag : TAG_NONE;
make_tunnel_passwd(ptr + 1, &len, data, len - 1, room,
secret, packet->vector);
break;
}
break;
/*
* The code above ensures that this attribute
* always fits.
*/
case FLAG_ENCRYPT_ASCEND_SECRET:
if (len != 16) return 0;
make_secret(ptr, packet->vector, secret, data);
len = AUTH_VECTOR_LEN;
break;
default: