import from branch_1_1:
[freeradius.git] / src / main / radsniff.c
1 /*
2  *  radsniff.c  Display the RADIUS traffic on the network.
3  *
4  *  Version:    $Id$
5  *
6  *  This program is free software; you can redistribute it and/or
7  *  modify it under the terms of the GNU General Public License
8  *  as published by the Free Software Foundation; either version 2
9  *  of the License, or (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  *
20  *  Copyright 2006  The FreeRADIUS server project
21  *  Copyright 2006  Nicolas Baradakis <nicolas.baradakis@cegetel.net>
22  */
23
24 #include <freeradius-devel/ident.h>
25 RCSID("$Id$")
26
27 #define _LIBRADIUS 1
28 #include <freeradius-devel/libradius.h>
29
30 #include <pcap.h>
31
32 #include <freeradius-devel/radpaths.h>
33 #include <freeradius-devel/conf.h>
34 #include <freeradius-devel/radsniff.h>
35
36 static const char *radius_secret = "testing123";
37 static VALUE_PAIR *filter_vps = NULL;
38
39 static const char *packet_codes[] = {
40   "",
41   "Access-Request",
42   "Access-Accept",
43   "Access-Reject",
44   "Accounting-Request",
45   "Accounting-Response",
46   "Accounting-Status",
47   "Password-Request",
48   "Password-Accept",
49   "Password-Reject",
50   "Accounting-Message",
51   "Access-Challenge",
52   "Status-Server",
53   "Status-Client",
54   "14",
55   "15",
56   "16",
57   "17",
58   "18",
59   "19",
60   "20",
61   "Resource-Free-Request",
62   "Resource-Free-Response",
63   "Resource-Query-Request",
64   "Resource-Query-Response",
65   "Alternate-Resource-Reclaim-Request",
66   "NAS-Reboot-Request",
67   "NAS-Reboot-Response",
68   "28",
69   "Next-Passcode",
70   "New-Pin",
71   "Terminate-Session",
72   "Password-Expired",
73   "Event-Request",
74   "Event-Response",
75   "35",
76   "36",
77   "37",
78   "38",
79   "39",
80   "Disconnect-Request",
81   "Disconnect-ACK",
82   "Disconnect-NAK",
83   "CoF-Request",
84   "CoF-ACK",
85   "CoF-NAK",
86   "46",
87   "47",
88   "48",
89   "49",
90   "IP-Address-Allocate",
91   "IP-Address-Release"
92 };
93
94 /*
95  *      Stolen from rad_recv() in ../lib/radius.c
96  */
97 static RADIUS_PACKET *init_packet(const uint8_t *data, size_t data_len)
98 {
99         RADIUS_PACKET           *packet;
100
101         /*
102          *      Allocate the new request data structure
103          */
104         if ((packet = malloc(sizeof(*packet))) == NULL) {
105                 librad_log("out of memory");
106                 return NULL;
107         }
108         memset(packet, 0, sizeof(*packet));
109
110         packet->data = data;
111         packet->data_len = data_len;
112
113         if (!rad_packet_ok(packet)) {
114                 rad_free(&packet);
115                 return NULL;
116         }
117
118         /*
119          *      Explicitely set the VP list to empty.
120          */
121         packet->vps = NULL;
122
123         return packet;
124 }
125
126 static int filter_packet(RADIUS_PACKET *packet)
127 {
128         VALUE_PAIR *check_item;
129         VALUE_PAIR *vp;
130         unsigned int pass, fail;
131         int compare;
132
133         pass = fail = 0;
134         for (vp = packet->vps; vp != NULL; vp = vp->next) {
135                 for (check_item = filter_vps;
136                      check_item != NULL;
137                      check_item = check_item->next)
138                         if ((check_item->attribute == vp->attribute)
139                          && (check_item->operator != T_OP_SET)) {
140                                 compare = paircmp(check_item, vp);
141                                 if (compare == 1)
142                                         pass++;
143                                 else
144                                         fail++;
145                         }
146         }
147         if (fail == 0 && pass != 0) {
148                 return 0;
149         }
150
151         return 1;
152 }
153
154 static void got_packet(uint8_t *args, const struct pcap_pkthdr *header, const uint8_t *packet)
155 {
156         /* Just a counter of how many packets we've had */
157         static int count = 1;
158         /* Define pointers for packet's attributes */
159         const struct ethernet_header *ethernet;  /* The ethernet header */
160         const struct ip_header *ip;              /* The IP header */
161         const struct udp_header *udp;            /* The UDP header */
162         const uint8_t *payload;                     /* Packet payload */
163         /* And define the size of the structures we're using */
164         int size_ethernet = sizeof(struct ethernet_header);
165         int size_ip = sizeof(struct ip_header);
166         int size_udp = sizeof(struct udp_header);
167         /* For FreeRADIUS */
168         RADIUS_PACKET *request;
169
170         args = args;            /* -Wunused */
171
172         /* Define our packet's attributes */
173         ethernet = (const struct ethernet_header*)(packet);
174         ip = (const struct ip_header*)(packet + size_ethernet);
175         udp = (const struct udp_header*)(packet + size_ethernet + size_ip);
176         payload = (const uint8_t *)(packet + size_ethernet + size_ip + size_udp);
177
178         /* Read the RADIUS packet structure */
179         request = init_packet(payload, header->len - size_ethernet - size_ip - size_udp);
180         if (request == NULL) {
181                 librad_perror("check");
182                 return;
183         }
184         request->src_ipaddr.ipaddr.ip4addr.s_addr = ip->ip_src.s_addr;
185         request->src_port = ntohs(udp->udp_sport);
186         request->dst_ipaddr.ipaddr.ip4addr.s_addr = ip->ip_dst.s_addr;
187         request->dst_port = ntohs(udp->udp_dport);
188
189         /*
190          *      Decode the data without bothering to check the signatures.
191          */
192         if (rad_decode(request, NULL, radius_secret) != 0) {
193                 librad_perror("decode");
194                 return;
195         }
196         if (filter_vps && filter_packet(request)) {
197                 /* printf("Packet number %d doesn't match\n", count++); */
198                 return;
199         }
200
201         /* Print the RADIUS packet */
202         printf("Packet number %d has just been sniffed\n", count++);
203         printf("\tFrom:    %s:%d\n", inet_ntoa(ip->ip_src), ntohs(udp->udp_sport));
204         printf("\tTo:      %s:%d\n", inet_ntoa(ip->ip_dst), ntohs(udp->udp_dport));
205         printf("\tType:    %s\n", packet_codes[request->code]);
206         if (request->vps != NULL) {
207                 vp_printlist(stdout, request->vps);
208                 pairfree(&request->vps);
209         }
210         free(request);
211 }
212
213 static void NEVER_RETURNS usage(int status)
214 {
215         FILE *output = status ? stderr : stdout;
216         fprintf(output, "usage: radsniff [options]\n");
217         fprintf(output, "options:\n");
218         fprintf(output, "\t-c count\tNumber of packets to capture.\n");
219         fprintf(output, "\t-d directory\tDirectory where the dictionaries are found\n");
220         fprintf(output, "\t-f filter\tPCAP filter. (default is udp port 1812 or 1813 or 1814)\n");
221         fprintf(output, "\t-h\t\tPrint this help message.\n");
222         fprintf(output, "\t-i interface\tInterface to capture.\n");
223         fprintf(output, "\t-p port\tList for packets on port.\n");
224         fprintf(output, "\t-r filter\tRADIUS attribute filter.\n");
225         fprintf(output, "\t-s secret\tRADIUS secret.\n");
226         exit(status);
227 }
228
229 int main(int argc, char *argv[])
230 {
231         char *dev;                      /* sniffing device */
232         char errbuf[PCAP_ERRBUF_SIZE];  /* error buffer */
233         pcap_t *descr;                  /* sniff handler */
234         struct bpf_program fp;          /* hold compiled program */
235         bpf_u_int32 maskp;              /* subnet mask */
236         bpf_u_int32 netp;               /* ip */
237         char buffer[1024];
238         char *pcap_filter = NULL;
239         char *radius_filter = NULL;
240         int packet_count = -1;          /* how many packets to sniff */
241         int opt;
242         LRAD_TOKEN parsecode;
243         const char *radius_dir = RADIUS_DIR;
244         int port = 1812;
245
246         /* Default device */
247         dev = pcap_lookupdev(errbuf);
248
249         /* Get options */
250         while ((opt = getopt(argc, argv, "c:d:f:hi:p:r:s:")) != EOF) {
251                 switch (opt)
252                 {
253                 case 'c':
254                         packet_count = atoi(optarg);
255                         if (packet_count <= 0) {
256                                 fprintf(stderr, "radsniff: Invalid number of packets \"%s\"\n", optarg);
257                                 exit(1);
258                         }
259                         break;
260                 case 'd':
261                         radius_dir = optarg;
262                         break;
263                 case 'f':
264                         pcap_filter = optarg;
265                         break;
266                 case 'h':
267                         usage(0);
268                         break;
269                 case 'i':
270                         dev = optarg;
271                         break;
272                 case 'p':
273                         port = atoi(optarg);
274                         break;
275                 case 'r':
276                         radius_filter = optarg;
277                         parsecode = userparse(radius_filter, &filter_vps);
278                         if (parsecode == T_OP_INVALID || filter_vps == NULL) {
279                                 fprintf(stderr, "radsniff: Invalid RADIUS filter \"%s\"\n", optarg);
280                                 exit(1);
281                         }
282                         break;
283                 case 's':
284                         radius_secret = optarg;
285                         break;
286                 default:
287                         usage(1);
288                 }
289         }
290
291         if (!pcap_filter) {
292                 pcap_filter = buffer;
293                 snprintf(buffer, sizeof(buffer), "udp port %d or %d or %d",
294                          port, port + 1, port + 2);
295         }
296
297         if (dict_init(radius_dir, RADIUS_DICTIONARY) < 0) {
298                 librad_perror("radsniff");
299                 return 1;
300         }
301         /* Set our device */
302         pcap_lookupnet(dev, &netp, &maskp, errbuf);
303
304         /* Print device to the user */
305         printf("Device: [%s]\n", dev);
306         if (packet_count > 0) {
307                 printf("Num of packets: [%d]\n", packet_count);
308         }
309         printf("PCAP filter: [%s]\n", pcap_filter);
310         if (filter_vps != NULL) {
311                 printf("RADIUS filter:\n");
312                 vp_printlist(stdout, filter_vps);
313         }
314         printf("RADIUS secret: [%s]\n", radius_secret);
315
316         /* Open the device so we can spy */
317         descr = pcap_open_live(dev, SNAPLEN, 1, 0, errbuf);
318         if (descr == NULL)
319         {
320                 printf("radsniff: pcap_open_live failed (%s)\n", errbuf);
321                 exit(1);
322         }
323
324         /* Apply the rules */
325         if( pcap_compile(descr, &fp, pcap_filter, 0, netp) == -1)
326         {
327                 printf("radsniff: pcap_compile failed\n");
328                 exit(1);
329         }
330         if (pcap_setfilter(descr, &fp) == -1)
331         {
332                 printf("radsniff: pcap_setfilter failed\n");
333                 exit(1);
334         }
335
336         /* Now we can set our callback function */
337         pcap_loop(descr, packet_count, got_packet, NULL);
338         pcap_close(descr);
339
340         printf("Done sniffing\n");
341         return 0;
342 }