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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 * Copyright 2000,2006,2014 The FreeRADIUS server project
21 * Copyright 2000 Miquel van Smoorenburg <miquels@cistron.nl>
22 * Copyright 2000 Alan DeKok <aland@ox.org>
27 #include <freeradius-devel/radclient.h>
28 #include <freeradius-devel/radpaths.h>
29 #include <freeradius-devel/conf.h>
38 typedef struct REQUEST REQUEST; /* to shut up warnings about mschap.h */
43 static int retries = 3;
44 static float timeout = 5;
45 static char const *secret = NULL;
46 static bool do_output = true;
48 static rc_stats_t stats;
50 static uint16_t server_port = 0;
51 static int packet_code = PW_CODE_UNDEFINED;
52 static fr_ipaddr_t server_ipaddr;
53 static int resend_count = 1;
54 static bool done = true;
55 static bool print_filename = false;
57 static fr_ipaddr_t client_ipaddr;
58 static uint16_t client_port = 0;
61 static int last_used_id = -1;
64 char const *proto = NULL;
66 static int ipproto = IPPROTO_UDP;
68 static rbtree_t *filename_tree = NULL;
69 static fr_packet_list_t *pl = NULL;
71 static int sleep_time = -1;
73 static rc_request_t *request_head = NULL;
74 static rc_request_t *rc_request_tail = NULL;
76 static int rc_debug_flag;
78 char const *radclient_version = "radclient version " RADIUSD_VERSION_STRING
79 #ifdef RADIUSD_VERSION_COMMIT
80 " (git #" STRINGIFY(RADIUSD_VERSION_COMMIT) ")"
82 ", built on " __DATE__ " at " __TIME__;
84 static void NEVER_RETURNS usage(void)
86 fprintf(stderr, "Usage: radclient [options] server[:port] <command> [<secret>]\n");
88 fprintf(stderr, " <command> One of auth, acct, status, coa, disconnect or auto.\n");
89 fprintf(stderr, " -4 Use IPv4 address of server\n");
90 fprintf(stderr, " -6 Use IPv6 address of server.\n");
91 fprintf(stderr, " -c <count> Send each packet 'count' times.\n");
92 fprintf(stderr, " -d <raddb> Set user dictionary directory (defaults to " RADDBDIR ").\n");
93 fprintf(stderr, " -D <dictdir> Set main dictionary directory (defaults to " DICTDIR ").\n");
94 fprintf(stderr, " -f <file>[:<file>] Read packets from file, not stdin.\n");
95 fprintf(stderr, " If a second file is provided, it will be used to verify responses\n");
96 fprintf(stderr, " -F Print the file name, packet number and reply code.\n");
97 fprintf(stderr, " -h Print usage help information.\n");
98 fprintf(stderr, " -i <id> Set request id to 'id'. Values may be 0..255\n");
99 fprintf(stderr, " -n <num> Send N requests/s\n");
100 fprintf(stderr, " -p <num> Send 'num' packets from a file in parallel.\n");
101 fprintf(stderr, " -q Do not print anything out.\n");
102 fprintf(stderr, " -r <retries> If timeout, retry sending the packet 'retries' times.\n");
103 fprintf(stderr, " -s Print out summary information of auth results.\n");
104 fprintf(stderr, " -S <file> read secret from file, not command line.\n");
105 fprintf(stderr, " -t <timeout> Wait 'timeout' seconds before retrying (may be a floating point number).\n");
106 fprintf(stderr, " -v Show program version information.\n");
107 fprintf(stderr, " -x Debugging mode.\n");
110 fprintf(stderr, " -P <proto> Use proto (tcp or udp) for transport.\n");
116 static const FR_NAME_NUMBER request_types[] = {
117 { "auth", PW_CODE_ACCESS_REQUEST },
118 { "challenge", PW_CODE_ACCESS_CHALLENGE },
119 { "acct", PW_CODE_ACCOUNTING_REQUEST },
120 { "status", PW_CODE_STATUS_SERVER },
121 { "disconnect", PW_CODE_DISCONNECT_REQUEST },
122 { "coa", PW_CODE_COA_REQUEST },
123 { "auto", PW_CODE_UNDEFINED },
129 * Free a radclient struct, which may (or may not)
130 * already be in the list.
132 static int _rc_request_free(rc_request_t *request)
134 rc_request_t *prev, *next;
136 prev = request->prev;
137 next = request->next;
140 assert(request_head != request);
142 } else if (request_head) {
143 assert(request_head == request);
148 assert(rc_request_tail != request);
150 } else if (rc_request_tail) {
151 assert(rc_request_tail == request);
152 rc_request_tail = prev;
158 static int mschapv1_encode(RADIUS_PACKET *packet, VALUE_PAIR **request,
159 char const *password)
163 VALUE_PAIR *challenge, *reply;
166 challenge = paircreate(packet, PW_MSCHAP_CHALLENGE, VENDORPEC_MICROSOFT);
171 pairadd(request, challenge);
172 challenge->length = 8;
173 challenge->vp_octets = p = talloc_array(challenge, uint8_t, challenge->length);
174 for (i = 0; i < challenge->length; i++) {
178 reply = paircreate(packet, PW_MSCHAP_RESPONSE, VENDORPEC_MICROSOFT);
183 pairadd(request, reply);
185 reply->vp_octets = p = talloc_array(reply, uint8_t, reply->length);
186 memset(p, 0, reply->length);
188 p[1] = 0x01; /* NT hash */
190 if (mschap_ntpwdhash(nthash, password) < 0) {
194 smbdes_mschap(nthash, challenge->vp_octets, p + 26);
199 static int getport(char const *name)
203 svp = getservbyname(name, "udp");
208 return ntohs(svp->s_port);
212 * Set a port from the request type if we don't already have one
214 static void radclient_get_port(PW_CODE type, uint16_t *port)
218 case PW_CODE_ACCESS_REQUEST:
219 case PW_CODE_ACCESS_CHALLENGE:
220 case PW_CODE_STATUS_SERVER:
221 if (*port == 0) *port = getport("radius");
222 if (*port == 0) *port = PW_AUTH_UDP_PORT;
225 case PW_CODE_ACCOUNTING_REQUEST:
226 if (*port == 0) *port = getport("radacct");
227 if (*port == 0) *port = PW_ACCT_UDP_PORT;
230 case PW_CODE_DISCONNECT_REQUEST:
231 if (*port == 0) *port = PW_POD_UDP_PORT;
234 case PW_CODE_COA_REQUEST:
235 if (*port == 0) *port = PW_COA_UDP_PORT;
238 case PW_CODE_UNDEFINED:
239 if (*port == 0) *port = 0;
245 * Resolve a port to a request type
247 static PW_CODE radclient_get_code(uint16_t port)
249 if ((port == getport("radius")) || (port == PW_AUTH_UDP_PORT) || (port == PW_AUTH_UDP_PORT_ALT)) {
250 return PW_CODE_ACCESS_REQUEST;
252 if ((port == getport("radacct")) || (port == PW_ACCT_UDP_PORT) || (port == PW_ACCT_UDP_PORT_ALT)) {
253 return PW_CODE_ACCOUNTING_REQUEST;
255 if (port == PW_COA_UDP_PORT) return PW_CODE_COA_REQUEST;
256 if (port == PW_POD_UDP_PORT) return PW_CODE_DISCONNECT_REQUEST;
258 return PW_CODE_UNDEFINED;
262 * Initialize a radclient data structure and add it to
263 * the global linked list.
265 static int radclient_init(TALLOC_CTX *ctx, rc_file_pair_t *files)
267 FILE *packets, *filters = NULL;
271 rc_request_t *request;
272 bool packets_done = false;
275 assert(files->packets != NULL);
278 * Determine where to read the VP's from.
280 if (strcmp(files->packets, "stdin") != 0) {
281 packets = fopen(files->packets, "r");
283 ERROR("Error opening %s: %s", files->packets, strerror(errno));
288 * Read in the pairs representing the expected response.
290 if (files->filters) {
291 filters = fopen(files->filters, "r");
293 ERROR("Error opening %s: %s", files->filters, strerror(errno));
304 * Loop until the file is done.
310 request = talloc_zero(ctx, rc_request_t);
312 ERROR("Out of memory");
316 request->packet = rad_alloc(request, true);
317 if (!request->packet) {
318 ERROR("Out of memory");
323 request->packet->src_ipaddr = client_ipaddr;
324 request->packet->src_port = client_port;
325 request->packet->dst_ipaddr = server_ipaddr;
326 request->packet->dst_port = server_port;
327 request->packet->proto = ipproto;
330 request->files = files;
331 request->packet->id = -1; /* allocate when sending */
332 request->num = num++;
335 * Read the request VP's.
337 if (readvp2(&request->packet->vps, request->packet, packets, &packets_done) < 0) {
338 REDEBUG("Error parsing \"%s\"", files->packets);
345 if (!request->packet->vps) {
346 talloc_free(request);
351 * Read in filter VP's.
356 if (readvp2(&request->filter, request, filters, &filters_done) < 0) {
357 REDEBUG("Error parsing \"%s\"", files->filters);
361 if (filters_done && !packets_done) {
362 REDEBUG("Differing number of packets/filters in %s:%s "
363 "(too many requests))", files->packets, files->filters);
367 if (!filters_done && packets_done) {
368 REDEBUG("Differing number of packets/filters in %s:%s "
369 "(too many filters))", files->packets, files->filters);
374 * xlat expansions aren't supported here
376 for (vp = fr_cursor_init(&cursor, &request->filter);
378 vp = fr_cursor_next(&cursor)) {
379 if (vp->type == VT_XLAT) {
381 vp->vp_strvalue = vp->value.xlat;
382 vp->length = talloc_array_length(vp->vp_strvalue) - 1;
385 if (vp->da->vendor == 0 ) switch (vp->da->attr) {
386 case PW_RESPONSE_PACKET_TYPE:
388 fr_cursor_remove(&cursor); /* so we don't break the filter */
389 request->filter_code = vp->vp_integer;
398 * This allows efficient list comparisons later
400 pairsort(&request->filter, attrtagcmp);
403 request->password[0] = '\0';
405 * Process special attributes
407 for (vp = fr_cursor_init(&cursor, &request->packet->vps);
409 vp = fr_cursor_next(&cursor)) {
411 * Double quoted strings get marked up as xlat expansions,
412 * but we don't support that in request.
414 if (vp->type == VT_XLAT) {
416 vp->vp_strvalue = vp->value.xlat;
417 vp->length = talloc_array_length(vp->vp_strvalue) - 1;
420 if (!vp->da->vendor) switch (vp->da->attr) {
425 * Allow it to set the packet type in
426 * the attributes read from the file.
429 request->packet->code = vp->vp_integer;
432 case PW_RESPONSE_PACKET_TYPE:
433 request->filter_code = vp->vp_integer;
436 case PW_PACKET_DST_PORT:
437 request->packet->dst_port = (vp->vp_integer & 0xffff);
440 case PW_PACKET_DST_IP_ADDRESS:
441 request->packet->dst_ipaddr.af = AF_INET;
442 request->packet->dst_ipaddr.ipaddr.ip4addr.s_addr = vp->vp_ipaddr;
445 case PW_PACKET_DST_IPV6_ADDRESS:
446 request->packet->dst_ipaddr.af = AF_INET6;
447 request->packet->dst_ipaddr.ipaddr.ip6addr = vp->vp_ipv6addr;
450 case PW_PACKET_SRC_PORT:
451 request->packet->src_port = (vp->vp_integer & 0xffff);
454 case PW_PACKET_SRC_IP_ADDRESS:
455 request->packet->src_ipaddr.af = AF_INET;
456 request->packet->src_ipaddr.ipaddr.ip4addr.s_addr = vp->vp_ipaddr;
459 case PW_PACKET_SRC_IPV6_ADDRESS:
460 request->packet->src_ipaddr.af = AF_INET6;
461 request->packet->src_ipaddr.ipaddr.ip6addr = vp->vp_ipv6addr;
464 case PW_DIGEST_REALM:
465 case PW_DIGEST_NONCE:
466 case PW_DIGEST_METHOD:
469 case PW_DIGEST_ALGORITHM:
470 case PW_DIGEST_BODY_DIGEST:
471 case PW_DIGEST_CNONCE:
472 case PW_DIGEST_NONCE_COUNT:
473 case PW_DIGEST_USER_NAME:
479 p = talloc_array(vp, uint8_t, vp->length + 2);
481 memcpy(p + 2, vp->vp_octets, vp->length);
482 p[0] = vp->da->attr - PW_DIGEST_REALM + 1;
486 da = dict_attrbyvalue(PW_DIGEST_ATTRIBUTES, 0);
488 ERROR("Out of memory");
494 * Re-do pairmemsteal ourselves,
495 * because we play games with
496 * vp->da, and pairmemsteal goes
497 * to GREAT lengths to sanitize
498 * and fix and change and
499 * double-check the various
502 memcpy(&q, &vp->vp_octets, sizeof(q));
505 vp->vp_octets = talloc_steal(vp, p);
513 * Keep a copy of the the password attribute.
515 case PW_USER_PASSWORD:
516 case PW_CHAP_PASSWORD:
517 case PW_MS_CHAP_PASSWORD:
518 strlcpy(request->password, vp->vp_strvalue, sizeof(request->password));
521 case PW_RADCLIENT_TEST_NAME:
522 request->name = vp->vp_strvalue;
525 } /* loop over the VP's we read in */
528 * Use the default set on the command line
530 if (request->packet->code == PW_CODE_UNDEFINED) request->packet->code = packet_code;
533 * Default to the filename
535 if (!request->name) request->name = request->files->packets;
538 * Automatically set the response code from the request code
539 * (if one wasn't already set).
541 if (request->filter_code == PW_CODE_UNDEFINED) {
542 switch (request->packet->code) {
543 case PW_CODE_ACCESS_REQUEST:
544 request->filter_code = PW_CODE_ACCESS_ACCEPT;
547 case PW_CODE_ACCOUNTING_REQUEST:
548 request->filter_code = PW_CODE_ACCOUNTING_RESPONSE;
551 case PW_CODE_COA_REQUEST:
552 request->filter_code = PW_CODE_COA_ACK;
555 case PW_CODE_DISCONNECT_REQUEST:
556 request->filter_code = PW_CODE_DISCONNECT_ACK;
559 case PW_CODE_STATUS_SERVER:
560 switch (radclient_get_code(request->packet->dst_port)) {
561 case PW_CODE_ACCESS_REQUEST:
562 request->filter_code = PW_CODE_ACCESS_ACCEPT;
565 case PW_CODE_ACCOUNTING_REQUEST:
566 request->filter_code = PW_CODE_ACCOUNTING_RESPONSE;
570 REDEBUG("Can't determine expected response to Status-Server request, specify "
571 "a well known RADIUS port, or add a Response-Packet-Type attribute "
572 "to the request of filter");
577 case PW_CODE_UNDEFINED:
578 REDEBUG("Both Packet-Type and Response-Packet-Type undefined, specify at least one, "
579 "or a well known RADIUS port");
583 REDEBUG("Can't determine expected Response-Packet-Type for Packet-Type %i",
584 request->packet->code);
588 * Automatically set the request code from the response code
589 * (if one wasn't already set).
591 } else if (request->packet->code == PW_CODE_UNDEFINED) {
592 switch (request->filter_code) {
593 case PW_CODE_ACCESS_ACCEPT:
594 case PW_CODE_ACCESS_REJECT:
595 request->packet->code = PW_CODE_ACCESS_REQUEST;
598 case PW_CODE_ACCOUNTING_RESPONSE:
599 request->packet->code = PW_CODE_ACCOUNTING_REQUEST;
602 case PW_CODE_DISCONNECT_ACK:
603 case PW_CODE_DISCONNECT_NAK:
604 request->packet->code = PW_CODE_DISCONNECT_REQUEST;
607 case PW_CODE_COA_ACK:
608 case PW_CODE_COA_NAK:
609 request->packet->code = PW_CODE_COA_REQUEST;
613 REDEBUG("Can't determine expected Packet-Type for Response-Packet-Type %i",
614 request->filter_code);
620 * Automatically set the dst port (if one wasn't already set).
622 if (request->packet->dst_port == 0) {
623 radclient_get_port(request->packet->code, &request->packet->dst_port);
624 if (request->packet->dst_port == 0) {
625 REDEBUG("Can't determine destination port");
631 * Add it to the tail of the list.
634 assert(rc_request_tail == NULL);
635 request_head = request;
636 request->prev = NULL;
638 assert(rc_request_tail->next == NULL);
639 rc_request_tail->next = request;
640 request->prev = rc_request_tail;
642 rc_request_tail = request;
643 request->next = NULL;
646 * Set the destructor so it removes itself from the
647 * request list when freed. We don't set this until
648 * the packet is actually in the list, else we trigger
649 * the asserts in the free callback.
651 talloc_set_destructor(request, _rc_request_free);
652 } while (!packets_done); /* loop until the file is done. */
654 if (packets != stdin) fclose(packets);
655 if (filters) fclose(filters);
663 talloc_free(request);
665 if (packets != stdin) fclose(packets);
666 if (filters) fclose(filters);
673 * Sanity check each argument.
675 static int radclient_sane(rc_request_t *request)
677 if (request->packet->dst_port == 0) {
678 request->packet->dst_port = server_port;
680 if (request->packet->dst_ipaddr.af == AF_UNSPEC) {
681 if (server_ipaddr.af == AF_UNSPEC) {
682 ERROR("No server was given, and request %" PRIu64 " in file %s did not contain "
683 "Packet-Dst-IP-Address", request->num, request->files->packets);
686 request->packet->dst_ipaddr = server_ipaddr;
688 if (request->packet->code == 0) {
689 if (packet_code == -1) {
690 ERROR("Request was \"auto\", and request %" PRIu64 " in file %s did not contain Packet-Type",
691 request->num, request->files->packets);
694 request->packet->code = packet_code;
696 request->packet->sockfd = -1;
703 * For request handling.
705 static int filename_cmp(void const *one, void const *two)
709 rc_file_pair_t const *a = one;
710 rc_file_pair_t const *b = two;
712 cmp = strcmp(a->packets, b->packets);
713 if (cmp != 0) return cmp;
715 return strcmp(a->filters, b->filters);
718 static int filename_walk(UNUSED void *context, void *data)
720 rc_file_pair_t *files = data;
723 * Read request(s) from the file.
725 if (!radclient_init(files, files)) return -1; /* stop walking */
732 * Deallocate packet ID, etc.
734 static void deallocate_id(rc_request_t *request)
736 if (!request || !request->packet ||
737 (request->packet->id < 0)) {
742 * One more unused RADIUS ID.
744 fr_packet_list_id_free(pl, request->packet, true);
747 * If we've already sent a packet, free up the old one,
748 * and ensure that the next packet has a unique
749 * authentication vector.
751 if (request->packet->data) TALLOC_FREE(request->packet->data);
752 if (request->reply) rad_free(&request->reply);
758 static int send_one_packet(rc_request_t *request)
760 assert(request->done == false);
763 * Remember when we have to wake up, to re-send the
764 * request, of we didn't receive a reply.
766 if ((sleep_time == -1) || (sleep_time > (int) timeout)) sleep_time = (int) timeout;
769 * Haven't sent the packet yet. Initialize it.
771 if (request->packet->id == -1) {
775 assert(request->reply == NULL);
778 * Didn't find a free packet ID, we're not done,
779 * we don't sleep, and we stop trying to process
783 request->packet->src_ipaddr.af = server_ipaddr.af;
784 rcode = fr_packet_list_id_alloc(pl, ipproto, &request->packet, NULL);
790 mysockfd = fr_tcp_client_socket(NULL,
791 &request->packet->dst_ipaddr,
792 request->packet->dst_port);
795 mysockfd = fr_socket(&client_ipaddr, 0);
797 ERROR("Failed opening socket");
800 if (!fr_packet_list_socket_add(pl, mysockfd, ipproto,
801 &request->packet->dst_ipaddr,
802 request->packet->dst_port, NULL)) {
803 ERROR("Can't add new socket");
809 assert(request->packet->id != -1);
810 assert(request->packet->data == NULL);
812 for (i = 0; i < 4; i++) {
813 ((uint32_t *) request->packet->vector)[i] = fr_rand();
817 * Update the password, so it can be encrypted with the
818 * new authentication vector.
820 if (request->password[0] != '\0') {
823 if ((vp = pairfind(request->packet->vps, PW_USER_PASSWORD, 0, TAG_ANY)) != NULL) {
824 pairstrcpy(vp, request->password);
825 } else if ((vp = pairfind(request->packet->vps, PW_CHAP_PASSWORD, 0, TAG_ANY)) != NULL) {
826 bool already_hex = false;
829 * If it's 17 octets, it *might* be already encoded.
830 * Or, it might just be a 17-character password (maybe UTF-8)
831 * Check it for non-printable characters. The odds of ALL
832 * of the characters being 32..255 is (1-7/8)^17, or (1/8)^17,
833 * or 1/(2^51), which is pretty much zero.
835 if (vp->length == 17) {
836 for (i = 0; i < 17; i++) {
837 if (vp->vp_octets[i] < 32) {
845 * Allow the user to specify ASCII or hex CHAP-Password
851 len = len2 = strlen(request->password);
852 if (len2 < 17) len2 = 17;
854 p = talloc_zero_array(vp, uint8_t, len2);
856 memcpy(p, request->password, len);
858 rad_chap_encode(request->packet,
860 fr_rand() & 0xff, vp);
864 } else if (pairfind(request->packet->vps, PW_MS_CHAP_PASSWORD, 0, TAG_ANY) != NULL) {
865 mschapv1_encode(request->packet, &request->packet->vps, request->password);
867 DEBUG("WARNING: No password in the request");
871 request->timestamp = time(NULL);
879 if (client_port == 0) {
880 client_ipaddr = request->packet->src_ipaddr;
881 client_port = request->packet->src_port;
885 } else { /* request->packet->id >= 0 */
886 time_t now = time(NULL);
889 * FIXME: Accounting packets are never retried!
890 * The Acct-Delay-Time attribute is updated to
891 * reflect the delay, and the packet is re-sent
896 * Not time for a retry, do so.
898 if ((now - request->timestamp) < timeout) {
900 * When we walk over the tree sending
901 * packets, we update the minimum time
904 if ((sleep_time == -1) ||
905 (sleep_time > (now - request->timestamp))) {
906 sleep_time = now - request->timestamp;
912 * We're not trying later, maybe the packet is done.
914 if (request->tries == retries) {
915 assert(request->packet->id >= 0);
918 * Delete the request from the tree of
919 * outstanding requests.
921 fr_packet_list_yank(pl, request->packet);
923 REDEBUG("No reply from server for ID %d socket %d",
924 request->packet->id, request->packet->sockfd);
925 deallocate_id(request);
928 * Normally we mark it "done" when we've received
929 * the reply, but this is a special case.
931 if (request->resend == resend_count) {
932 request->done = true;
939 * We are trying later.
941 request->timestamp = now;
948 if (rad_send(request->packet, NULL, secret) < 0) {
949 REDEBUG("Failed to send packet for ID %d", request->packet->id);
956 * Receive one packet, maybe.
958 static int recv_one_packet(int wait_time)
962 rc_request_t *request;
963 RADIUS_PACKET *reply, **packet_p;
966 /* And wait for reply, timing out as necessary */
969 max_fd = fr_packet_list_fd_set(pl, &set);
970 if (max_fd < 0) exit(1); /* no sockets to listen on! */
972 tv.tv_sec = (wait_time <= 0) ? 0 : wait_time;
976 * No packet was received.
978 if (select(max_fd, &set, NULL, NULL, &tv) <= 0) return 0;
981 * Look for the packet.
983 reply = fr_packet_list_recv(pl, &set);
985 ERROR("Received bad packet");
988 * If the packet is bad, we close the socket.
989 * I'm not sure how to do that now, so we just
994 return -1; /* bad packet */
998 * udpfromto issues. We may have bound to "*",
999 * and we want to find the replies that are sent to
1002 * This only works if were not using any of the
1003 * Packet-* attributes, or running with 'auto'.
1005 reply->dst_ipaddr = client_ipaddr;
1006 reply->dst_port = client_port;
1008 if (server_port > 0) {
1009 reply->src_ipaddr = server_ipaddr;
1010 reply->src_port = server_port;
1014 packet_p = fr_packet_list_find_byreply(pl, reply);
1016 ERROR("Received reply to request we did not send. (id=%d socket %d)",
1017 reply->id, reply->sockfd);
1019 return -1; /* got reply to packet we didn't send */
1021 request = fr_packet2myptr(rc_request_t, packet, packet_p);
1024 * Fails the signature validation: not a real reply.
1025 * FIXME: Silently drop it and listen for another packet.
1027 if (rad_verify(reply, request->packet, secret) < 0) {
1028 REDEBUG("Reply verification failed");
1030 goto packet_done; /* shared secret is incorrect */
1033 if (print_filename) {
1034 RDEBUG("%s response code %d", request->files->packets, reply->code);
1037 deallocate_id(request);
1038 request->reply = reply;
1042 * If this fails, we're out of memory.
1044 if (rad_decode(request->reply, request->packet, secret) != 0) {
1045 REDEBUG("Reply decode failed");
1051 * Increment counters...
1053 switch (request->reply->code) {
1054 case PW_CODE_ACCESS_ACCEPT:
1055 case PW_CODE_ACCOUNTING_RESPONSE:
1056 case PW_CODE_COA_ACK:
1057 case PW_CODE_DISCONNECT_ACK:
1061 case PW_CODE_ACCESS_CHALLENGE:
1069 * If we had an expected response code, check to see if the
1070 * packet matched that.
1072 if (request->reply->code != request->filter_code) {
1073 if (is_radius_code(request->reply->code)) {
1074 REDEBUG("%s: Expected %s got %s", request->name, fr_packet_codes[request->filter_code],
1075 fr_packet_codes[request->reply->code]);
1077 REDEBUG("%s: Expected %u got %i", request->name, request->filter_code,
1078 request->reply->code);
1082 * Check if the contents of the packet matched the filter
1084 } else if (!request->filter) {
1087 VALUE_PAIR const *failed[2];
1089 pairsort(&request->reply->vps, attrtagcmp);
1090 if (pairvalidate(failed, request->filter, request->reply->vps)) {
1091 RDEBUG("%s: Response passed filter", request->name);
1094 pairvalidate_debug(request, failed);
1095 REDEBUG("%s: Response for failed filter", request->name);
1100 if (request->resend == resend_count) {
1101 request->done = true;
1105 rad_free(&request->reply);
1106 rad_free(&reply); /* may be NULL */
1111 int main(int argc, char **argv)
1114 char const *radius_dir = RADDBDIR;
1115 char const *dict_dir = DICTDIR;
1116 char filesecret[256];
1118 int do_summary = false;
1122 int force_af = AF_UNSPEC;
1125 * It's easier having two sets of flags to set the
1126 * verbosity of library calls and the verbosity of
1134 if (fr_fault_setup(getenv("PANIC_ACTION"), argv[0]) < 0) {
1135 fr_perror("radclient");
1140 talloc_set_log_stderr();
1142 filename_tree = rbtree_create(NULL, filename_cmp, NULL, 0);
1143 if (!filename_tree) {
1145 ERROR("Out of memory");
1149 while ((c = getopt(argc, argv, "46c:d:D:f:Fhi:n:p:qr:sS:t:vx"
1153 )) != EOF) switch(c) {
1159 force_af = AF_INET6;
1163 if (!isdigit((int) *optarg))
1165 resend_count = atoi(optarg);
1173 radius_dir = optarg;
1179 rc_file_pair_t *files;
1181 files = talloc(talloc_autofree_context(), rc_file_pair_t);
1182 if (!files) goto oom;
1184 p = strchr(optarg, ':');
1186 files->packets = talloc_strndup(files, optarg, p - optarg);
1187 if (!files->packets) goto oom;
1188 files->filters = p + 1;
1190 files->packets = optarg;
1191 files->filters = NULL;
1193 rbtree_insert(filename_tree, (void *) files);
1198 print_filename = true;
1201 case 'i': /* currently broken */
1202 if (!isdigit((int) *optarg))
1204 last_used_id = atoi(optarg);
1205 if ((last_used_id < 0) || (last_used_id > 255)) {
1211 persec = atoi(optarg);
1212 if (persec <= 0) usage();
1216 * Note that sending MANY requests in
1217 * parallel can over-run the kernel
1218 * queues, and Linux will happily discard
1219 * packets. So even if the server responds,
1220 * the client may not see the reply.
1223 parallel = atoi(optarg);
1224 if (parallel <= 0) usage();
1230 if (strcmp(proto, "tcp") != 0) {
1231 if (strcmp(proto, "udp") == 0) {
1237 ipproto = IPPROTO_TCP;
1245 fr_log_fp = NULL; /* no output from you, either! */
1249 if (!isdigit((int) *optarg)) usage();
1250 retries = atoi(optarg);
1251 if ((retries == 0) || (retries > 1000)) usage();
1261 fp = fopen(optarg, "r");
1263 ERROR("Error opening %s: %s", optarg, fr_syserror(errno));
1266 if (fgets(filesecret, sizeof(filesecret), fp) == NULL) {
1267 ERROR("Error reading %s: %s", optarg, fr_syserror(errno));
1272 /* truncate newline */
1273 p = filesecret + strlen(filesecret) - 1;
1274 while ((p >= filesecret) &&
1280 if (strlen(filesecret) < 2) {
1281 ERROR("Secret in %s is too short", optarg);
1284 secret = filesecret;
1289 if (!isdigit((int) *optarg))
1291 timeout = atof(optarg);
1295 DEBUG("%s", radclient_version);
1309 argc -= (optind - 1);
1310 argv += (optind - 1);
1312 if ((argc < 3) || ((secret == NULL) && (argc < 4))) {
1313 ERROR("Insufficient arguments");
1317 * Mismatch between the binary and the libraries it depends on
1319 if (fr_check_lib_magic(RADIUSD_MAGIC_NUMBER) < 0) {
1320 fr_perror("radclient");
1324 if (dict_init(dict_dir, RADIUS_DICTIONARY) < 0) {
1325 fr_perror("radclient");
1329 if (dict_read(radius_dir, RADIUS_DICTIONARY) == -1) {
1330 fr_perror("radclient");
1333 fr_strerror(); /* Clear the error buffer */
1336 * Get the request type
1338 if (!isdigit((int) argv[2][0])) {
1339 packet_code = fr_str2int(request_types, argv[2], -2);
1340 if (packet_code == -2) {
1341 ERROR("Unrecognised request type \"%s\"", argv[2]);
1345 packet_code = atoi(argv[2]);
1351 if (force_af == AF_UNSPEC) force_af = AF_INET;
1352 server_ipaddr.af = force_af;
1353 if (strcmp(argv[1], "-") != 0) {
1355 char const *hostname = argv[1];
1356 char const *portname = argv[1];
1359 if (*argv[1] == '[') { /* IPv6 URL encoded */
1360 p = strchr(argv[1], ']');
1361 if ((size_t) (p - argv[1]) >= sizeof(buffer)) {
1365 memcpy(buffer, argv[1] + 1, p - argv[1] - 1);
1366 buffer[p - argv[1] - 1] = '\0';
1372 p = strchr(portname, ':');
1373 if (p && (strchr(p + 1, ':') == NULL)) {
1380 if (ip_hton(&server_ipaddr, force_af, hostname, false) < 0) {
1381 ERROR("Failed to find IP address for host %s: %s", hostname, strerror(errno));
1386 * Strip port from hostname if needed.
1388 if (portname) server_port = atoi(portname);
1391 * Work backwards from the port to determine the packet type
1393 if (packet_code == PW_CODE_UNDEFINED) packet_code = radclient_get_code(server_port);
1396 radclient_get_port(packet_code, &server_port);
1401 if (argv[3]) secret = argv[3];
1404 * If no '-f' is specified, we're reading from stdin.
1406 if (rbtree_num_elements(filename_tree) == 0) {
1407 rc_file_pair_t *files;
1409 files = talloc_zero(talloc_autofree_context(), rc_file_pair_t);
1410 files->packets = "stdin";
1411 if (!radclient_init(files, files)) {
1417 * Walk over the list of filenames, creating the requests.
1419 if (rbtree_walk(filename_tree, RBTREE_IN_ORDER, filename_walk, NULL) != 0) {
1420 ERROR("Failed parsing input files");
1425 * No packets read. Die.
1427 if (!request_head) {
1428 ERROR("Nothing to send");
1433 * Bind to the first specified IP address and port.
1434 * This means we ignore later ones.
1436 if (request_head->packet->src_ipaddr.af == AF_UNSPEC) {
1437 memset(&client_ipaddr, 0, sizeof(client_ipaddr));
1438 client_ipaddr.af = server_ipaddr.af;
1441 client_ipaddr = request_head->packet->src_ipaddr;
1442 client_port = request_head->packet->src_port;
1446 sockfd = fr_tcp_client_socket(NULL, &server_ipaddr, server_port);
1449 sockfd = fr_socket(&client_ipaddr, client_port);
1451 ERROR("Error opening socket");
1455 pl = fr_packet_list_create(1);
1457 ERROR("Out of memory");
1461 if (!fr_packet_list_socket_add(pl, sockfd, ipproto, &server_ipaddr,
1462 server_port, NULL)) {
1463 ERROR("Out of memory");
1468 * Walk over the list of packets, sanity checking
1471 for (this = request_head; this != NULL; this = this->next) {
1472 this->packet->src_ipaddr = client_ipaddr;
1473 this->packet->src_port = client_port;
1474 if (radclient_sane(this) != 0) {
1480 * Walk over the packets to send, until
1483 * FIXME: This currently busy-loops until it receives
1484 * all of the packets. It should really have some sort of
1485 * send packet, get time to wait, select for time, etc.
1491 char const *filename = NULL;
1497 * Walk over the packets, sending them.
1500 for (this = request_head; this != NULL; this = next) {
1504 * If there's a packet to receive,
1505 * receive it, but don't wait for a
1511 * This packet is done. Delete it.
1519 * Packets from multiple '-f' are sent
1522 * Packets from one file are sent in
1523 * series, unless '-p' is specified, in
1524 * which case N packets from each file
1525 * are sent in parallel.
1527 if (this->files->packets != filename) {
1528 filename = this->files->packets;
1536 * Send the current packet.
1538 send_one_packet(this);
1541 * Wait a little before sending
1542 * the next packet, if told to.
1548 * Don't sleep elsewhere.
1557 tv.tv_usec = 1000000/persec;
1561 * Sleep for milliseconds,
1564 * If we get an error or
1565 * a signal, treat it like
1568 select(0, NULL, NULL, NULL, &tv);
1572 * If we haven't sent this packet
1573 * often enough, we're not done,
1574 * and we shouldn't sleep.
1576 if (this->resend < resend_count) {
1580 } else { /* haven't sent this packet, we're not done */
1581 assert(this->done == false);
1582 assert(this->reply == NULL);
1588 * Still have outstanding requests.
1590 if (fr_packet_list_num_elements(pl) > 0) {
1597 * Nothing to do until we receive a request, so
1598 * sleep until then. Once we receive one packet,
1599 * we go back, and walk through the whole list again,
1600 * sending more packets (if necessary), and updating
1603 if (!done && (sleep_time > 0)) {
1604 recv_one_packet(sleep_time);
1608 rbtree_free(filename_tree);
1609 fr_packet_list_free(pl);
1610 while (request_head) TALLOC_FREE(request_head);
1614 DEBUG("Packet summary:\n"
1615 "\tAccepted : %" PRIu64 "\n"
1616 "\tRejected : %" PRIu64 "\n"
1617 "\tLost : %" PRIu64 "\n"
1618 "\tPassed filter : %" PRIu64 "\n"
1619 "\tFailed filter : %" PRIu64,
1628 if ((stats.lost > 0) || (stats.failed > 0)) {