-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathprotocols_radius.c
161 lines (129 loc) · 4.23 KB
/
protocols_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
/*
*
* Copyright (c) 2007-2020 The University of Waikato, Hamilton, New Zealand.
* All rights reserved.
*
* This file is part of libtrace.
*
* This code has been developed by the University of Waikato WAND
* research group. For further information please see http://www.wand.net.nz/
*
* libtrace 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 3 of the License, or
* (at your option) any later version.
*
* libtrace 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 program. If not, see <http://www.gnu.org/licenses/>.
*
*
*/
#include "libtrace_radius.h"
#include <stdlib.h>
#include <stdio.h>
#include "libtrace_int.h"
DLLEXPORT char *trace_get_radius_username(libtrace_radius_t *radius,
uint32_t radrem, uint8_t *namelen)
{
libtrace_radius_avp_t *username;
if ((username = trace_get_radius_avp(radius, radrem,
LIBTRACE_RADIUS_USERNAME)) != NULL) {
/* minus 2 for the avp header fields */
*namelen = username->length - 2;
return (char *)&username->data;
}
*namelen = 0;
return NULL;
}
DLLEXPORT char *trace_get_radius_nas_identifier(libtrace_radius_t *radius,
uint32_t radrem,
uint8_t *naslen)
{
libtrace_radius_avp_t *nas_ident;
if ((nas_ident = trace_get_radius_avp(radius, radrem,
LIBTRACE_RADIUS_NAS_IDENT)) != NULL) {
*naslen = nas_ident->length - 2;
return (char *)&nas_ident->data;
}
*naslen = 0;
return NULL;
}
DLLEXPORT libtrace_radius_t *trace_get_radius(libtrace_packet_t *packet,
uint32_t *remaining)
{
void *payload;
libtrace_radius_t *radius;
uint8_t proto;
uint32_t plen;
payload = trace_get_transport(packet, &proto, remaining);
if (payload == NULL) {
return NULL;
}
plen = trace_get_payload_length(packet);
switch (proto) {
case TRACE_IPPROTO_TCP:
payload =
trace_get_payload_from_tcp((libtrace_tcp_t *)payload, remaining);
break;
case TRACE_IPPROTO_UDP:
payload =
trace_get_payload_from_udp((libtrace_udp_t *)payload, remaining);
break;
default:
payload = NULL;
break;
}
if (payload == NULL) {
*remaining = 0;
return NULL;
}
/* we can only assume that the payload is RADIUS here -- ideally the
* caller will only pass in packets that they know are RADIUS (e.g. by
* matching the known IP and port of a RADIUS server), but we can at least
* do some basic sanity checks to rule out obvious non-RADIUS packets.
*/
/* enough data for radius header? */
if (plen < sizeof(libtrace_radius_t)) {
return NULL;
}
/* does the length in the radius length field match the remaining data */
radius = (libtrace_radius_t *)payload;
if (ntohs(radius->length) == plen) {
return radius;
}
return NULL;
}
DLLEXPORT libtrace_radius_avp_t *
trace_get_radius_avp(libtrace_radius_t *radius, uint32_t remaining,
libtrace_radius_avp_type type)
{
libtrace_radius_avp_t *c;
uint8_t *ptr;
uint32_t rem = ntohs(radius->length);
if (rem <= sizeof(libtrace_radius_t)) {
return NULL;
}
if (remaining < rem) {
rem = remaining;
}
rem -= sizeof(libtrace_radius_t);
ptr = (uint8_t *)radius + sizeof(libtrace_radius_t);
while (1) {
c = (libtrace_radius_avp_t *)ptr;
if (c->type == type) {
return c;
}
if (rem <= c->length) {
return NULL;
}
rem -= c->length;
ptr += c->length;
}
return NULL;
}
// vim: set sw=4 tabstop=4 softtabstop=4 expandtab :