2 * radclient.c General radius packet debug tool.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
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.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * Copyright 2000 The FreeRADIUS server project
21 * Copyright 2000 Miquel van Smoorenburg <miquels@cistron.nl>
22 * Copyright 2000 Alan DeKok <aland@ox.org>
24 static const char rcsid[] = "$Id$";
38 #include <sys/socket.h>
40 #ifdef HAVE_NETINET_IN_H
41 # include <netinet/in.h>
44 #ifdef HAVE_SYS_SELECT_H
45 # include <sys/select.h>
57 #include "libradius.h"
59 static int retries = 10;
60 static float timeout = 3;
61 static const char *secret = NULL;
62 static int do_output = 1;
63 static int totalapp = 0;
64 static int totaldeny = 0;
65 static int totallost = 0;
67 static int server_port = 0;
68 static int packet_code = 0;
69 static uint32_t server_ipaddr = 0;
70 static int resend_count = 1;
74 static int radius_id[256];
75 static int last_used_id = -1;
77 static rbtree_t *filename_tree = NULL;
78 static rbtree_t *request_tree = NULL;
80 static int sleep_time = -1;
82 typedef struct radclient_t {
83 struct radclient_t *prev;
84 struct radclient_t *next;
87 int packet_number; /* in the file */
90 RADIUS_PACKET *request;
97 static radclient_t *radclient_head = NULL;
98 static radclient_t *radclient_tail = NULL;
101 static void NEVER_RETURNS usage(void)
103 fprintf(stderr, "Usage: radclient [options] server[:port] <command> [<secret>]\n");
105 fprintf(stderr, " <command> One of auth, acct, status, coa, or disconnect.\n");
106 fprintf(stderr, " -c count Send each packet 'count' times.\n");
107 fprintf(stderr, " -d raddb Set dictionary directory.\n");
108 fprintf(stderr, " -f file Read packets from file, not stdin.\n");
109 fprintf(stderr, " -i id Set request id to 'id'. Values may be 0..255\n");
110 fprintf(stderr, " -n num Send N requests/s\n");
111 fprintf(stderr, " -p num Send 'num' packets from a file in parallel.\n");
112 fprintf(stderr, " -q Do not print anything out.\n");
113 fprintf(stderr, " -r retries If timeout, retry sending the packet 'retries' times.\n");
114 fprintf(stderr, " -s Print out summary information of auth results.\n");
115 fprintf(stderr, " -S file read secret from file, not command line.\n");
116 fprintf(stderr, " -t timeout Wait 'timeout' seconds before retrying (may be a floating point number).\n");
117 fprintf(stderr, " -v Show program version information.\n");
118 fprintf(stderr, " -x Debugging mode.\n");
124 * Free a radclient struct, which may (or may not)
125 * already be in the list.
127 static void radclient_free(radclient_t *radclient)
129 radclient_t *prev, *next;
131 if (radclient->request) rad_free(&radclient->request);
132 if (radclient->reply) rad_free(&radclient->reply);
134 prev = radclient->prev;
135 next = radclient->next;
138 assert(radclient_head != radclient);
140 } else if (radclient_head) {
141 assert(radclient_head == radclient);
142 radclient_head = next;
146 assert(radclient_tail != radclient);
148 } else if (radclient_tail) {
149 assert(radclient_tail == radclient);
150 radclient_tail = prev;
157 * Initialize a radclient data structure
159 static radclient_t *radclient_init(const char *filename)
163 radclient_t *start, *radclient, *prev = NULL;
165 int packet_number = 1;
168 assert(filename != NULL);
171 * Determine where to read the VP's from.
173 if (strcmp(filename, "-") != 0) {
174 fp = fopen(filename, "r");
176 fprintf(stderr, "radclient: Error opening %s: %s\n",
177 filename, strerror(errno));
185 * Loop until the file is done.
191 radclient = malloc(sizeof(*radclient));
193 perror("radclient: ");
194 return NULL; /* memory leak "start" */
196 memset(radclient, 0, sizeof(*radclient));
198 radclient->request = rad_alloc(1);
199 if (!radclient->request) {
200 librad_perror("radclient: ");
201 radclient_free(radclient);
202 return NULL; /* memory leak "start" */
205 radclient->filename = filename;
206 radclient->request->id = -1; /* allocate when sending */
207 radclient->packet_number = packet_number++;
212 radclient->request->vps = readvp2(fp, &filedone, "radclient:");
213 if (!radclient->request->vps) {
214 radclient_free(radclient);
215 return start; /* done: return the list */
219 * Keep a copy of the the User-Password attribute.
221 if ((vp = pairfind(radclient->request->vps, PW_PASSWORD)) != NULL) {
222 strNcpy(radclient->password, (char *)vp->strvalue, sizeof(radclient->password));
224 * Otherwise keep a copy of the CHAP-Password attribute.
226 } else if ((vp = pairfind(radclient->request->vps, PW_CHAP_PASSWORD)) != NULL) {
227 strNcpy(radclient->password, (char *)vp->strvalue, sizeof(radclient->password));
229 radclient->password[0] = '\0';
233 * Fix up Digest-Attributes issues
235 for (vp = radclient->request->vps; vp != NULL; vp = vp->next) {
236 switch (vp->attribute) {
241 * Allow it to set the packet type in
242 * the attributes read from the file.
245 radclient->request->code = vp->lvalue;
248 case PW_PACKET_DST_PORT:
249 radclient->request->dst_port = (vp->lvalue & 0xffff);
252 case PW_DIGEST_REALM:
253 case PW_DIGEST_NONCE:
254 case PW_DIGEST_METHOD:
257 case PW_DIGEST_ALGORITHM:
258 case PW_DIGEST_BODY_DIGEST:
259 case PW_DIGEST_CNONCE:
260 case PW_DIGEST_NONCE_COUNT:
261 case PW_DIGEST_USER_NAME:
263 memmove(&vp->strvalue[2], &vp->strvalue[0], vp->length);
264 vp->strvalue[0] = vp->attribute - PW_DIGEST_REALM + 1;
266 vp->strvalue[1] = vp->length;
267 vp->attribute = PW_DIGEST_ATTRIBUTES;
270 } /* loop over the VP's we read in */
276 prev->next = radclient;
277 radclient->prev = prev;
280 } while (!filedone); /* loop until the file is done. */
282 if (fp != stdin) fclose(fp);
292 * Sanity check each argument.
294 static int radclient_sane(radclient_t *radclient)
296 if (radclient->request->dst_port == 0) {
297 radclient->request->dst_port = server_port;
299 radclient->request->dst_ipaddr = server_ipaddr;
301 if (radclient->request->code == 0) {
302 if (packet_code == -1) {
303 fprintf(stderr, "radclient: Request was \"auto\", but request %d in file %s did not contain Packet-Type\n",
304 radclient->packet_number, radclient->filename);
308 radclient->request->code = packet_code;
310 radclient->request->sockfd = sockfd;
317 * For request handline.
319 static int filename_cmp(const void *one, const void *two)
321 return strcmp((const char *) one, (const char *) two);
324 static int filename_walk(void *context, void *data)
326 const char *filename = data;
327 radclient_t *radclient;
329 context = context; /* -Wunused */
332 * Initialize the request we're about
335 radclient = radclient_init(filename);
340 if (!radclient_head) {
341 assert(radclient_tail == NULL);
342 radclient_head = radclient;
344 assert(radclient_tail->next == NULL);
345 radclient_tail->next = radclient;
346 radclient->prev = radclient_tail;
350 * We may have had a list of "radclient" structures
353 while (radclient->next) radclient = radclient->next;
354 radclient_tail = radclient;
361 * Compare two RADIUS_PACKET data structures, based on a number
364 static int request_cmp(const void *one, const void *two)
366 const radclient_t *a = one;
367 const radclient_t *b = two;
370 * The following code looks unreasonable, but it's
371 * the only way to make the comparisons work.
373 if (a->request->id < b->request->id) return -1;
374 if (a->request->id > b->request->id) return +1;
376 if (a->request->dst_ipaddr < b->request->dst_ipaddr) return -1;
377 if (a->request->dst_ipaddr > b->request->dst_ipaddr) return +1;
379 if (a->request->dst_port < b->request->dst_port) return -1;
380 if (a->request->dst_port > b->request->dst_port) return +1;
383 * Everything's equal. Say so.
391 static void request_free(void *data)
393 radclient_t *radclient = (radclient_t *) data;
395 if (!radclient || !radclient->request ||
396 (radclient->request->id < 0)) {
401 * One more unused RADIUS ID.
403 radius_id[radclient->request->id] = 0;
404 radclient->request->id = -1;
407 * If we've already sent a packet, free up the old one,
408 * and ensure that the next packet has a unique
409 * authentication vector.
411 if (radclient->request->data) {
412 free(radclient->request->data);
413 radclient->request->data = NULL;
416 if (radclient->reply) rad_free(&radclient->reply);
423 static int send_one_packet(radclient_t *radclient)
427 assert(radclient->done == 0);
430 * Remember when we have to wake up, to re-send the
431 * request, of we didn't receive a response.
433 if ((sleep_time == -1) ||
434 (sleep_time > (int) timeout)) {
435 sleep_time = (int) timeout;
439 * Haven't sent the packet yet. Initialize it.
441 if (radclient->request->id == -1) {
444 assert(radclient->reply == NULL);
447 * Find a free packet Id
449 for (i = 0; i < 256; i++) {
450 if (radius_id[(last_used_id + i) & 0xff] == 0) {
451 last_used_id = (last_used_id + i) & 0xff;
452 radius_id[last_used_id] = 1;
453 radclient->request->id = last_used_id++;
460 * Didn't find a free packet ID, we're not done,
461 * we don't sleep, and we stop trying to process
470 assert(radclient->request->id != -1);
471 assert(radclient->request->data == NULL);
473 librad_md5_calc(radclient->request->vector, radclient->request->vector,
474 sizeof(radclient->request->vector));
477 * Update the password, so it can be encrypted with the
478 * new authentication vector.
480 if (radclient->password[0] != '\0') {
483 if ((vp = pairfind(radclient->request->vps, PW_PASSWORD)) != NULL) {
484 strNcpy((char *)vp->strvalue, radclient->password, sizeof(vp->strvalue));
485 vp->length = strlen(vp->strvalue);
487 } else if ((vp = pairfind(radclient->request->vps, PW_CHAP_PASSWORD)) != NULL) {
488 strNcpy((char *)vp->strvalue, radclient->password, sizeof(vp->strvalue));
489 vp->length = strlen(vp->strvalue);
491 rad_chap_encode(radclient->request, (char *) vp->strvalue, radclient->request->id, vp);
496 radclient->timestamp = time(NULL);
497 radclient->tries = 1;
501 * Duplicate found. Serious error!
503 if (rbtree_insert(request_tree, radclient) == 0) {
507 } else { /* radclient->request->id >= 0 */
508 time_t now = time(NULL);
511 * FIXME: Accounting packets are never retried!
512 * The Acct-Delay-Time attribute is updated to
513 * reflect the delay, and the packet is re-sent
518 * Not time for a retry, do so.
520 if ((now - radclient->timestamp) < timeout) {
522 * When we walk over the tree sending
523 * packets, we update the minimum time
526 if ((sleep_time == -1) ||
527 (sleep_time > (now - radclient->timestamp))) {
528 sleep_time = now - radclient->timestamp;
534 * We're not trying later, maybe the packet is done.
536 if (radclient->tries == retries) {
538 assert(radclient->request->id >= 0);
541 * Delete the request from the tree of
542 * outstanding requests.
544 node = rbtree_find(request_tree, radclient);
545 assert(node != NULL);
547 fprintf(stderr, "radclient: no response from server for ID %d\n", radclient->request->id);
548 rbtree_delete(request_tree, node);
551 * Normally we mark it "done" when we've received
552 * the response, but this is a special case.
554 if (radclient->resend == resend_count) {
562 * We are trying later.
564 radclient->timestamp = now;
572 if (rad_send(radclient->request, NULL, secret) < 0) {
573 fprintf(stderr, "radclient: Failed to send packet for ID %d: %s\n",
574 radclient->request->id, librad_errstr);
581 * Receive one packet, maybe.
583 static int recv_one_packet(int wait_time)
587 radclient_t myclient, *radclient;
588 RADIUS_PACKET myrequest, *reply;
592 /* And wait for reply, timing out as necessary */
594 FD_SET(sockfd, &set);
596 if (wait_time <= 0) {
599 tv.tv_sec = wait_time;
604 * No packet was received.
606 if (select(sockfd + 1, &set, NULL, NULL, &tv) != 1) {
611 * Look for the packet.
613 reply = rad_recv(sockfd);
615 fprintf(stderr, "radclient: received bad packet\n");
616 return -1; /* bad packet */
619 myclient.request = &myrequest;
620 myrequest.id = reply->id;
621 myrequest.dst_ipaddr = reply->src_ipaddr;
622 myrequest.dst_port = reply->src_port;
624 node = rbtree_find(request_tree, &myclient);
626 fprintf(stderr, "radclient: received response to request we did not send.\n");
628 return -1; /* got reply to packet we didn't send */
631 radclient = rbtree_node2data(request_tree, node);
632 assert(radclient != NULL);
633 rbtree_delete(request_tree, node);
634 assert(radclient->request->id == -1);
635 assert(radclient->request->data == NULL);
637 assert(radclient->reply == NULL);
638 radclient->reply = reply;
641 * FIXME: Do stuff to process the reply.
643 if (rad_decode(reply, radclient->request, secret) != 0) {
644 librad_perror("rad_decode");
646 goto packet_done; /* shared secret is incorrect */
649 /* libradius debug already prints out the value pairs for us */
650 if (!librad_debug && do_output) {
651 printf("Received response ID %d, code %d, length = %d\n",
652 reply->id, reply->code, reply->data_len);
653 vp_printlist(stdout, reply->vps);
655 if (reply->code != PW_AUTHENTICATION_REJECT) {
662 rad_free(&radclient->reply);
665 * Once we've sent the packet as many times as requested,
668 if (radclient->resend == resend_count) {
669 assert((node = rbtree_find(request_tree, radclient)) == NULL);
676 static int getport(const char *name)
680 svp = getservbyname (name, "udp");
685 return ntohs(svp->s_port);
688 int main(int argc, char **argv)
692 const char *radius_dir = RADDBDIR;
693 char filesecret[256];
702 filename_tree = rbtree_create(filename_cmp, NULL, 0);
703 if (!filename_tree) {
704 fprintf(stderr, "radclient: Out of memory\n");
708 request_tree = rbtree_create(request_cmp, request_free, 0);
710 fprintf(stderr, "radclient: Out of memory\n");
714 while ((c = getopt(argc, argv, "c:d:f:hi:n:p:qr:sS:t:vx")) != EOF) switch(c) {
716 if (!isdigit((int) *optarg))
718 resend_count = atoi(optarg);
724 rbtree_insert(filename_tree, optarg);
727 if (!isdigit((int) *optarg))
729 last_used_id = atoi(optarg);
730 if ((last_used_id < 0) || (last_used_id > 255)) {
736 persec = atoi(optarg);
737 if (persec <= 0) usage();
741 parallel = atoi(optarg);
742 if (parallel <= 0) usage();
749 if (!isdigit((int) *optarg))
751 retries = atoi(optarg);
752 if ((retries == 0) || (retries > 1000)) usage();
758 fp = fopen(optarg, "r");
760 fprintf(stderr, "radclient: Error opening %s: %s\n",
761 optarg, strerror(errno));
764 if (fgets(filesecret, sizeof(filesecret), fp) == NULL) {
765 fprintf(stderr, "radclient: Error reading %s: %s\n",
766 optarg, strerror(errno));
771 /* truncate newline */
772 p = filesecret + strlen(filesecret) - 1;
773 while ((p >= filesecret) &&
779 if (strlen(filesecret) < 2) {
780 fprintf(stderr, "radclient: Secret in %s is too short\n", optarg);
786 if (!isdigit((int) *optarg))
788 timeout = atof(optarg);
791 printf("radclient: $Id$ built on " __DATE__ " at " __TIME__ "\n");
802 argc -= (optind - 1);
803 argv += (optind - 1);
806 ((secret == NULL) && (argc < 4))) {
810 if (dict_init(radius_dir, RADIUS_DICTIONARY) < 0) {
811 librad_perror("radclient");
816 * Strip port from hostname if needed.
818 if ((p = strchr(argv[1], ':')) != NULL) {
820 server_port = atoi(p);
826 if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
827 perror("radclient: socket: ");
830 memset(radius_id, 0, sizeof(radius_id));
833 * See what kind of request we want to send.
835 if (strcmp(argv[2], "auth") == 0) {
836 if (server_port == 0) server_port = getport("radius");
837 if (server_port == 0) server_port = PW_AUTH_UDP_PORT;
838 packet_code = PW_AUTHENTICATION_REQUEST;
840 } else if (strcmp(argv[2], "challenge") == 0) {
841 if (server_port == 0) server_port = getport("radius");
842 if (server_port == 0) server_port = PW_AUTH_UDP_PORT;
843 packet_code = PW_ACCESS_CHALLENGE;
845 } else if (strcmp(argv[2], "acct") == 0) {
846 if (server_port == 0) server_port = getport("radacct");
847 if (server_port == 0) server_port = PW_ACCT_UDP_PORT;
848 packet_code = PW_ACCOUNTING_REQUEST;
851 } else if (strcmp(argv[2], "status") == 0) {
852 if (server_port == 0) server_port = getport("radius");
853 if (server_port == 0) server_port = PW_AUTH_UDP_PORT;
854 packet_code = PW_STATUS_SERVER;
856 } else if (strcmp(argv[2], "disconnect") == 0) {
857 if (server_port == 0) server_port = PW_POD_UDP_PORT;
858 packet_code = PW_DISCONNECT_REQUEST;
860 } else if (strcmp(argv[2], "coa") == 0) {
861 if (server_port == 0) server_port = PW_POD_UDP_PORT;
862 packet_code = PW_COA_REQUEST;
864 } else if (strcmp(argv[2], "auto") == 0) {
867 } else if (isdigit((int) argv[2][0])) {
868 if (server_port == 0) server_port = getport("radius");
869 if (server_port == 0) server_port = PW_AUTH_UDP_PORT;
870 packet_code = atoi(argv[2]);
878 server_ipaddr = ip_getaddr(argv[1]);
879 if (server_ipaddr == INADDR_NONE) {
880 fprintf(stderr, "radclient: Failed to find IP address for host %s\n", argv[1]);
887 if (argv[3]) secret = argv[3];
890 * If no '-f' is specified, we're reading from stdin.
892 if (rbtree_num_elements(filename_tree) == 0) {
893 rbtree_insert(filename_tree, "-");
897 * Walk over the list of filenames, creating the requests.
899 if (rbtree_walk(filename_tree, InOrder, filename_walk, NULL) != 0) {
904 * No packets read. Die.
906 if (!radclient_head) {
907 fprintf(stderr, "radclient: Nothing to send.\n");
912 * Walk over the list of packets, sanity checking
915 for (this = radclient_head; this != NULL; this = this->next) {
916 if (radclient_sane(this) != 0) {
921 if (last_used_id < 0) last_used_id = getpid() & 0xff;
924 * Walk over the packets to send, until
927 * FIXME: This currently busy-loops until it receives
928 * all of the packets. It should really have some sort of
929 * send packet, get time to wait, select for time, etc.
935 const char *filename = NULL;
941 * Walk over the packets, sending them.
944 for (this = radclient_head; this != NULL; this = next) {
948 * If there's a packet to receive,
949 * receive it, but don't wait for a
955 * This packet is done. Delete it.
958 radclient_free(this);
963 * Packets from multiple '-f' are sent
966 * Packets from one file are sent in
967 * series, unless '-p' is specified, in
968 * which case N packets from each file
969 * are sent in parallel.
971 if (this->filename != filename) {
972 filename = this->filename;
980 * Send the current packet.
982 send_one_packet(this);
985 * Wait a little before sending
986 * the next packet, if told to.
992 * Don't sleep elsewhere.
1001 tv.tv_usec = 1000000/persec;
1005 * Sleep for milliseconds,
1008 * If we get an error or
1009 * a signal, treat it like
1012 select(0, NULL, NULL, NULL, &tv);
1016 * If we haven't sent this packet
1017 * often enough, we're not done,
1018 * and we shouldn't sleep.
1020 if (this->resend < resend_count) {
1024 } else { /* haven't sent this packet, we're not done */
1025 assert(this->done == 0);
1026 assert(this->reply == NULL);
1032 * Still have outstanding requests.
1034 if (rbtree_num_elements(request_tree) > 0) {
1041 * Nothing to do until we receive a request, so
1042 * sleep until then. Once we receive one packet,
1043 * we go back, and walk through the whole list again,
1044 * sending more packets (if necessary), and updating
1047 if (!done && (sleep_time > 0)) {
1048 recv_one_packet(sleep_time);
1052 rbtree_free(filename_tree);
1053 rbtree_free(request_tree);
1056 printf("\n\t Total approved auths: %d\n", totalapp);
1057 printf("\t Total denied auths: %d\n", totaldeny);
1058 printf("\t Total lost auths: %d\n", totallost);