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