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 = 0;
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, or disconnect.\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_AUTHENTICATION_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_AUTHENTICATION_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;
233 case PW_CODE_COA_REQUEST:
234 if (*port == 0) *port = PW_COA_UDP_PORT;
237 case PW_CODE_UNDEFINED:
238 if (*port == 0) *port = 0;
243 * Resolve a port to a request type
245 static PW_CODE radclient_get_code(port)
247 if ((port == getport("radius")) || (port == PW_AUTH_UDP_PORT) || (port == PW_AUTH_UDP_PORT_ALT)) {
248 return PW_CODE_AUTHENTICATION_REQUEST;
250 if ((port == getport("radacct")) || (port == PW_ACCT_UDP_PORT) || (port == PW_ACCT_UDP_PORT_ALT)) {
251 return PW_CODE_ACCOUNTING_REQUEST;
253 if (port == PW_COA_UDP_PORT) return PW_CODE_COA_REQUEST;
254 if (port == PW_POD_UDP_PORT) return PW_CODE_DISCONNECT_REQUEST;
256 return PW_CODE_UNDEFINED;
260 * Initialize a radclient data structure and add it to
261 * the global linked list.
263 static int radclient_init(TALLOC_CTX *ctx, rc_file_pair_t *files)
265 FILE *packets, *filters = NULL;
269 rc_request_t *request;
270 bool packets_done = false;
273 assert(files->packets != NULL);
276 * Determine where to read the VP's from.
278 if (strcmp(files->packets, "stdin") != 0) {
279 packets = fopen(files->packets, "r");
281 ERROR("Error opening %s: %s", files->packets, strerror(errno));
286 * Read in the pairs representing the expected response.
288 if (files->filters) {
289 filters = fopen(files->filters, "r");
291 ERROR("Error opening %s: %s", files->filters, strerror(errno));
302 * Loop until the file is done.
308 request = talloc_zero(ctx, rc_request_t);
310 ERROR("Out of memory");
314 request->packet = rad_alloc(request, 1);
315 if (!request->packet) {
316 ERROR("Out of memory");
321 request->packet->src_ipaddr = client_ipaddr;
322 request->packet->src_port = client_port;
323 request->packet->dst_ipaddr = server_ipaddr;
324 request->packet->dst_port = server_port;
325 request->packet->proto = ipproto;
328 request->files = files;
329 request->packet->id = -1; /* allocate when sending */
330 request->num = num++;
333 * Read the request VP's.
335 if (readvp2(&request->packet->vps, request->packet, packets, &packets_done) < 0) {
336 REDEBUG("Error parsing \"%s\"", files->packets);
343 if (!request->packet->vps) {
344 talloc_free(request);
349 * Read in filter VP's.
354 if (readvp2(&request->filter, request, filters, &filters_done) < 0) {
355 REDEBUG("Error parsing \"%s\"", files->filters);
359 if (filters_done && !packets_done) {
360 REDEBUG("Differing number of packets/filters in %s:%s "
361 "(too many requests))", files->packets, files->filters);
365 if (!filters_done && packets_done) {
366 REDEBUG("Differing number of packets/filters in %s:%s "
367 "(too many filters))", files->packets, files->filters);
372 * xlat expansions aren't supported here
374 for (vp = fr_cursor_init(&cursor, &request->filter);
376 vp = fr_cursor_next(&cursor)) {
377 if (vp->type == VT_XLAT) {
379 vp->vp_strvalue = vp->value.xlat;
380 vp->length = talloc_array_length(vp->vp_strvalue) - 1;
383 if (vp->da->vendor == 0 ) switch (vp->da->attr) {
384 case PW_RESPONSE_PACKET_TYPE:
386 fr_cursor_remove(&cursor); /* so we don't break the filter */
387 request->filter_code = vp->vp_integer;
396 * This allows efficient list comparisons later
398 pairsort(&request->filter, attrtagcmp);
401 request->password[0] = '\0';
403 * Process special attributes
405 for (vp = fr_cursor_init(&cursor, &request->packet->vps);
407 vp = fr_cursor_next(&cursor)) {
409 * Double quoted strings get marked up as xlat expansions,
410 * but we don't support that in request.
412 if (vp->type == VT_XLAT) {
414 vp->vp_strvalue = vp->value.xlat;
415 vp->length = talloc_array_length(vp->vp_strvalue) - 1;
418 if (!vp->da->vendor) switch (vp->da->attr) {
423 * Allow it to set the packet type in
424 * the attributes read from the file.
427 request->packet->code = vp->vp_integer;
430 case PW_PACKET_DST_PORT:
431 request->packet->dst_port = (vp->vp_integer & 0xffff);
434 case PW_PACKET_DST_IP_ADDRESS:
435 request->packet->dst_ipaddr.af = AF_INET;
436 request->packet->dst_ipaddr.ipaddr.ip4addr.s_addr = vp->vp_ipaddr;
439 case PW_PACKET_DST_IPV6_ADDRESS:
440 request->packet->dst_ipaddr.af = AF_INET6;
441 request->packet->dst_ipaddr.ipaddr.ip6addr = vp->vp_ipv6addr;
444 case PW_PACKET_SRC_PORT:
445 request->packet->src_port = (vp->vp_integer & 0xffff);
448 case PW_PACKET_SRC_IP_ADDRESS:
449 request->packet->src_ipaddr.af = AF_INET;
450 request->packet->src_ipaddr.ipaddr.ip4addr.s_addr = vp->vp_ipaddr;
453 case PW_PACKET_SRC_IPV6_ADDRESS:
454 request->packet->src_ipaddr.af = AF_INET6;
455 request->packet->src_ipaddr.ipaddr.ip6addr = vp->vp_ipv6addr;
458 case PW_DIGEST_REALM:
459 case PW_DIGEST_NONCE:
460 case PW_DIGEST_METHOD:
463 case PW_DIGEST_ALGORITHM:
464 case PW_DIGEST_BODY_DIGEST:
465 case PW_DIGEST_CNONCE:
466 case PW_DIGEST_NONCE_COUNT:
467 case PW_DIGEST_USER_NAME:
473 p = talloc_array(vp, uint8_t, vp->length + 2);
475 memcpy(p + 2, vp->vp_octets, vp->length);
476 p[0] = vp->da->attr - PW_DIGEST_REALM + 1;
480 da = dict_attrbyvalue(PW_DIGEST_ATTRIBUTES, 0);
482 ERROR("Out of memory");
488 * Re-do pairmemsteal ourselves,
489 * because we play games with
490 * vp->da, and pairmemsteal goes
491 * to GREAT lengths to sanitize
492 * and fix and change and
493 * double-check the various
496 memcpy(&q, &vp->vp_octets, sizeof(q));
499 vp->vp_octets = talloc_steal(vp, p);
507 * Keep a copy of the the password attribute.
509 case PW_USER_PASSWORD:
510 case PW_CHAP_PASSWORD:
511 case PW_MS_CHAP_PASSWORD:
512 strlcpy(request->password, vp->vp_strvalue, sizeof(request->password));
515 case PW_RADCLIENT_TEST_NAME:
516 request->name = vp->vp_strvalue;
519 } /* loop over the VP's we read in */
522 * Use the default set on the command line
524 if (request->packet->code == PW_CODE_UNDEFINED) {
525 request->packet->code = packet_code;
529 * Default to the filename
531 if (!request->name) {
532 request->name = request->files->packets;
536 * Automatically set the response code from the request code
537 * (if one wasn't already set).
539 if (request->filter_code == PW_CODE_UNDEFINED) {
540 switch (request->packet->code) {
541 case PW_CODE_AUTHENTICATION_REQUEST:
542 request->filter_code = PW_CODE_AUTHENTICATION_ACK;
545 case PW_CODE_ACCOUNTING_REQUEST:
546 request->filter_code = PW_CODE_ACCOUNTING_RESPONSE;
549 case PW_CODE_COA_REQUEST:
550 request->filter_code = PW_CODE_COA_ACK;
553 case PW_CODE_DISCONNECT_REQUEST:
554 request->filter_code = PW_CODE_DISCONNECT_ACK;
557 case PW_CODE_STATUS_SERVER:
558 switch (radclient_get_code(request->packet->dst_port)) {
559 case PW_CODE_AUTHENTICATION_REQUEST:
560 request->filter_code = PW_CODE_AUTHENTICATION_ACK;
563 case PW_CODE_ACCOUNTING_REQUEST:
564 request->filter_code = PW_CODE_ACCOUNTING_RESPONSE;
568 REDEBUG("Can't determine expected response to Status-Server request, specify "
569 "a well known RADIUS port, or a response filter with a "
570 "Response-Packet-Type attribute");
574 case PW_CODE_UNDEFINED:
575 REDEBUG("Both Packet-Type and Response-Packet-Type, specify at least one, or a well "
576 "known RADIUS port");
580 REDEBUG("Can't determine expected Response-Packet-Type for Packet-Type %i",
581 request->packet->code);
585 * Automatically set the request code from the response code
586 * (if one wasn't already set).
588 } else if (request->packet->code == PW_CODE_UNDEFINED) {
589 switch (request->filter_code) {
590 case PW_CODE_AUTHENTICATION_ACK:
591 case PW_CODE_AUTHENTICATION_REJECT:
592 request->packet->code = PW_CODE_AUTHENTICATION_REQUEST;
595 case PW_CODE_ACCOUNTING_RESPONSE:
596 request->packet->code = PW_CODE_ACCOUNTING_REQUEST;
599 case PW_CODE_DISCONNECT_ACK:
600 case PW_CODE_DISCONNECT_NAK:
601 request->packet->code = PW_CODE_DISCONNECT_REQUEST;
604 case PW_CODE_COA_ACK:
605 case PW_CODE_COA_NAK:
606 request->packet->code = PW_CODE_COA_REQUEST;
610 REDEBUG("Can't determine expected Packet-Type for Response-Packet-Type %i",
611 request->filter_code);
617 * Automatically set the dst port (if one wasn't already set).
619 if (request->packet->dst_port == 0) {
620 radclient_get_port(request->packet->code, &request->packet->dst_port);
621 if (request->packet->dst_port == 0) {
622 REDEBUG("Can't determine destination port");
628 * Add it to the tail of the list.
631 assert(rc_request_tail == NULL);
632 request_head = request;
633 request->prev = NULL;
635 assert(rc_request_tail->next == NULL);
636 rc_request_tail->next = request;
637 request->prev = rc_request_tail;
639 rc_request_tail = request;
640 request->next = NULL;
643 * Set the destructor so it removes itself from the
644 * request list when freed. We don't set this until
645 * the packet is actually in the list, else we trigger
646 * the asserts in the free callback.
648 talloc_set_destructor(request, _rc_request_free);
649 } while (!packets_done); /* loop until the file is done. */
651 if (packets != stdin) fclose(packets);
652 if (filters) fclose(filters);
660 talloc_free(request);
662 if (packets != stdin) fclose(packets);
663 if (filters) fclose(filters);
670 * Sanity check each argument.
672 static int radclient_sane(rc_request_t *request)
674 if (request->packet->dst_port == 0) {
675 request->packet->dst_port = server_port;
677 if (request->packet->dst_ipaddr.af == AF_UNSPEC) {
678 if (server_ipaddr.af == AF_UNSPEC) {
679 ERROR("No server was given, and request %" PRIu64 " in file %s did not contain "
680 "Packet-Dst-IP-Address", request->num, request->files->packets);
683 request->packet->dst_ipaddr = server_ipaddr;
685 if (request->packet->code == 0) {
686 if (packet_code == -1) {
687 ERROR("Request was \"auto\", and request %" PRIu64 " in file %s did not contain Packet-Type",
688 request->num, request->files->packets);
691 request->packet->code = packet_code;
693 request->packet->sockfd = -1;
700 * For request handling.
702 static int filename_cmp(void const *one, void const *two)
706 rc_file_pair_t const *a = one;
707 rc_file_pair_t const *b = two;
709 cmp = strcmp(a->packets, b->packets);
710 if (cmp != 0) return cmp;
712 return strcmp(a->filters, b->filters);
715 static int filename_walk(UNUSED void *context, void *data)
717 rc_file_pair_t *files = data;
720 * Read request(s) from the file.
722 if (!radclient_init(files, files)) {
723 return -1; /* stop walking */
731 * Deallocate packet ID, etc.
733 static void deallocate_id(rc_request_t *request)
735 if (!request || !request->packet ||
736 (request->packet->id < 0)) {
741 * One more unused RADIUS ID.
743 fr_packet_list_id_free(pl, request->packet, true);
746 * If we've already sent a packet, free up the old one,
747 * and ensure that the next packet has a unique
748 * authentication vector.
750 if (request->packet->data) {
751 TALLOC_FREE(request->packet->data);
754 if (request->reply) rad_free(&request->reply);
760 static int send_one_packet(rc_request_t *request)
762 assert(request->done == false);
765 * Remember when we have to wake up, to re-send the
766 * request, of we didn't receive a reply.
768 if ((sleep_time == -1) || (sleep_time > (int) timeout)) {
769 sleep_time = (int) timeout;
773 * Haven't sent the packet yet. Initialize it.
775 if (request->packet->id == -1) {
779 assert(request->reply == NULL);
782 * Didn't find a free packet ID, we're not done,
783 * we don't sleep, and we stop trying to process
787 request->packet->src_ipaddr.af = server_ipaddr.af;
788 rcode = fr_packet_list_id_alloc(pl, ipproto,
789 &request->packet, NULL);
795 mysockfd = fr_tcp_client_socket(NULL,
796 &request->packet->dst_ipaddr,
797 request->packet->dst_port);
800 mysockfd = fr_socket(&client_ipaddr, 0);
802 ERROR("Failed opening socket");
805 if (!fr_packet_list_socket_add(pl, mysockfd, ipproto,
806 &request->packet->dst_ipaddr,
807 request->packet->dst_port, NULL)) {
808 ERROR("Can't add new socket");
814 assert(request->packet->id != -1);
815 assert(request->packet->data == NULL);
817 for (i = 0; i < 4; i++) {
818 ((uint32_t *) request->packet->vector)[i] = fr_rand();
822 * Update the password, so it can be encrypted with the
823 * new authentication vector.
825 if (request->password[0] != '\0') {
828 if ((vp = pairfind(request->packet->vps, PW_USER_PASSWORD, 0, TAG_ANY)) != NULL) {
829 pairstrcpy(vp, request->password);
831 } else if ((vp = pairfind(request->packet->vps, PW_CHAP_PASSWORD, 0, TAG_ANY)) != NULL) {
832 bool already_hex = false;
835 * If it's 17 octets, it *might* be already encoded.
836 * Or, it might just be a 17-character password (maybe UTF-8)
837 * Check it for non-printable characters. The odds of ALL
838 * of the characters being 32..255 is (1-7/8)^17, or (1/8)^17,
839 * or 1/(2^51), which is pretty much zero.
841 if (vp->length == 17) {
842 for (i = 0; i < 17; i++) {
843 if (vp->vp_octets[i] < 32) {
851 * Allow the user to specify ASCII or hex CHAP-Password
857 len = len2 = strlen(request->password);
858 if (len2 < 17) len2 = 17;
860 p = talloc_zero_array(vp, uint8_t, len2);
862 memcpy(p, request->password, len);
864 rad_chap_encode(request->packet,
866 fr_rand() & 0xff, vp);
870 } else if (pairfind(request->packet->vps, PW_MS_CHAP_PASSWORD, 0, TAG_ANY) != NULL) {
871 mschapv1_encode(request->packet,
872 &request->packet->vps,
875 DEBUG("WARNING: No password in the request");
879 request->timestamp = time(NULL);
887 if (client_port == 0) {
888 client_ipaddr = request->packet->src_ipaddr;
889 client_port = request->packet->src_port;
893 } else { /* request->packet->id >= 0 */
894 time_t now = time(NULL);
897 * FIXME: Accounting packets are never retried!
898 * The Acct-Delay-Time attribute is updated to
899 * reflect the delay, and the packet is re-sent
904 * Not time for a retry, do so.
906 if ((now - request->timestamp) < timeout) {
908 * When we walk over the tree sending
909 * packets, we update the minimum time
912 if ((sleep_time == -1) ||
913 (sleep_time > (now - request->timestamp))) {
914 sleep_time = now - request->timestamp;
920 * We're not trying later, maybe the packet is done.
922 if (request->tries == retries) {
923 assert(request->packet->id >= 0);
926 * Delete the request from the tree of
927 * outstanding requests.
929 fr_packet_list_yank(pl, request->packet);
931 REDEBUG("No reply from server for ID %d socket %d",
932 request->packet->id, request->packet->sockfd);
933 deallocate_id(request);
936 * Normally we mark it "done" when we've received
937 * the reply, but this is a special case.
939 if (request->resend == resend_count) {
940 request->done = true;
947 * We are trying later.
949 request->timestamp = now;
956 if (rad_send(request->packet, NULL, secret) < 0) {
957 REDEBUG("Failed to send packet for ID %d", request->packet->id);
964 * Receive one packet, maybe.
966 static int recv_one_packet(int wait_time)
970 rc_request_t *request;
971 RADIUS_PACKET *reply, **packet_p;
974 /* And wait for reply, timing out as necessary */
977 max_fd = fr_packet_list_fd_set(pl, &set);
978 if (max_fd < 0) exit(1); /* no sockets to listen on! */
980 if (wait_time <= 0) {
983 tv.tv_sec = wait_time;
988 * No packet was received.
990 if (select(max_fd, &set, NULL, NULL, &tv) <= 0) {
995 * Look for the packet.
997 reply = fr_packet_list_recv(pl, &set);
999 ERROR("Received bad packet");
1002 * If the packet is bad, we close the socket.
1003 * I'm not sure how to do that now, so we just
1008 return -1; /* bad packet */
1012 * udpfromto issues. We may have bound to "*",
1013 * and we want to find the replies that are sent to
1016 * This only works if were not using any of the
1017 * Packet-* attributes, or running with 'auto'.
1019 reply->dst_ipaddr = client_ipaddr;
1020 reply->dst_port = client_port;
1022 if (server_port > 0) {
1023 reply->src_ipaddr = server_ipaddr;
1024 reply->src_port = server_port;
1028 packet_p = fr_packet_list_find_byreply(pl, reply);
1030 ERROR("Received reply to request we did not send. (id=%d socket %d)",
1031 reply->id, reply->sockfd);
1033 return -1; /* got reply to packet we didn't send */
1035 request = fr_packet2myptr(rc_request_t, packet, packet_p);
1038 * Fails the signature validation: not a real reply.
1039 * FIXME: Silently drop it and listen for another packet.
1041 if (rad_verify(reply, request->packet, secret) < 0) {
1042 REDEBUG("Reply verification failed");
1044 goto packet_done; /* shared secret is incorrect */
1047 if (print_filename) {
1048 RDEBUG("%s response code %d", request->files->packets, reply->code);
1051 deallocate_id(request);
1052 request->reply = reply;
1056 * If this fails, we're out of memory.
1058 if (rad_decode(request->reply, request->packet, secret) != 0) {
1059 REDEBUG("Reply decode failed");
1065 * Increment counters...
1067 switch (request->reply->code) {
1068 case PW_CODE_AUTHENTICATION_ACK:
1069 case PW_CODE_ACCOUNTING_RESPONSE:
1070 case PW_CODE_COA_ACK:
1071 case PW_CODE_DISCONNECT_ACK:
1075 case PW_CODE_ACCESS_CHALLENGE:
1083 * If we had an expected response code, check to see if the
1084 * packet matched that.
1086 if (request->reply->code != request->filter_code) {
1087 if (is_radius_code(request->reply->code)) {
1088 REDEBUG("%s: Expected %s got %s", request->name, fr_packet_codes[request->filter_code],
1089 fr_packet_codes[request->reply->code]);
1091 REDEBUG("%s: Expected %u got %i", request->name, request->filter_code,
1092 request->reply->code);
1096 * Check if the contents of the packet matched the filter
1098 } else if (!request->filter) {
1101 VALUE_PAIR const *failed[2];
1103 pairsort(&request->reply->vps, attrtagcmp);
1104 if (pairvalidate(failed, request->filter, request->reply->vps)) {
1105 RDEBUG("%s: Response passed filter", request->name);
1108 pairvalidate_debug(request, failed);
1109 REDEBUG("%s: Response for failed filter", request->name);
1114 if (request->resend == resend_count) {
1115 request->done = true;
1119 rad_free(&request->reply);
1120 rad_free(&reply); /* may be NULL */
1125 int main(int argc, char **argv)
1128 char const *radius_dir = RADDBDIR;
1129 char const *dict_dir = DICTDIR;
1130 char filesecret[256];
1132 int do_summary = false;
1136 int force_af = AF_UNSPEC;
1139 * It's easier having two sets of flags to set the
1140 * verbosity of library calls and the verbosity of
1148 if (fr_fault_setup(getenv("PANIC_ACTION"), argv[0]) < 0) {
1149 fr_perror("radclient");
1154 talloc_set_log_stderr();
1156 filename_tree = rbtree_create(filename_cmp, NULL, 0);
1157 if (!filename_tree) {
1159 ERROR("Out of memory");
1163 while ((c = getopt(argc, argv, "46c:d:D:f:Fhi:n:p:qr:sS:t:vx"
1167 )) != EOF) switch(c) {
1172 force_af = AF_INET6;
1175 if (!isdigit((int) *optarg))
1177 resend_count = atoi(optarg);
1183 radius_dir = optarg;
1188 rc_file_pair_t *files;
1190 files = talloc(talloc_autofree_context(), rc_file_pair_t);
1191 if (!files) goto oom;
1193 p = strchr(optarg, ':');
1195 files->packets = talloc_strndup(files, optarg, p - optarg);
1196 if (!files->packets) goto oom;
1197 files->filters = p + 1;
1199 files->packets = optarg;
1200 files->filters = NULL;
1202 rbtree_insert(filename_tree, (void *) files);
1206 print_filename = true;
1208 case 'i': /* currently broken */
1209 if (!isdigit((int) *optarg))
1211 last_used_id = atoi(optarg);
1212 if ((last_used_id < 0) || (last_used_id > 255)) {
1218 persec = atoi(optarg);
1219 if (persec <= 0) usage();
1223 * Note that sending MANY requests in
1224 * parallel can over-run the kernel
1225 * queues, and Linux will happily discard
1226 * packets. So even if the server responds,
1227 * the client may not see the reply.
1230 parallel = atoi(optarg);
1231 if (parallel <= 0) usage();
1237 if (strcmp(proto, "tcp") != 0) {
1238 if (strcmp(proto, "udp") == 0) {
1244 ipproto = IPPROTO_TCP;
1252 fr_log_fp = NULL; /* no output from you, either! */
1256 if (!isdigit((int) *optarg))
1258 retries = atoi(optarg);
1259 if ((retries == 0) || (retries > 1000)) usage();
1267 fp = fopen(optarg, "r");
1269 ERROR("Error opening %s: %s", optarg, fr_syserror(errno));
1272 if (fgets(filesecret, sizeof(filesecret), fp) == NULL) {
1273 ERROR("Error reading %s: %s", optarg, fr_syserror(errno));
1278 /* truncate newline */
1279 p = filesecret + strlen(filesecret) - 1;
1280 while ((p >= filesecret) &&
1286 if (strlen(filesecret) < 2) {
1287 ERROR("Secret in %s is too short", optarg);
1290 secret = filesecret;
1294 if (!isdigit((int) *optarg))
1296 timeout = atof(optarg);
1299 DEBUG("%s", radclient_version);
1311 argc -= (optind - 1);
1312 argv += (optind - 1);
1314 if ((argc < 3) || ((secret == NULL) && (argc < 4))) {
1315 ERROR("Insufficient arguments");
1319 * Mismatch between the binary and the libraries it depends on
1321 if (fr_check_lib_magic(RADIUSD_MAGIC_NUMBER) < 0) {
1322 fr_perror("radclient");
1326 if (dict_init(dict_dir, RADIUS_DICTIONARY) < 0) {
1327 fr_perror("radclient");
1331 if (dict_read(radius_dir, RADIUS_DICTIONARY) == -1) {
1332 fr_perror("radclient");
1335 fr_strerror(); /* Clear the error buffer */
1339 * Get the request type
1341 if (!isdigit((int) argv[2][0])) {
1342 packet_code = fr_str2int(request_types, argv[2], -2);
1343 if (packet_code == -2) {
1344 ERROR("Unrecognised request type \"%s\"", argv[2]);
1348 packet_code = atoi(argv[2]);
1354 if (force_af == AF_UNSPEC) force_af = AF_INET;
1355 server_ipaddr.af = force_af;
1356 if (strcmp(argv[1], "-") != 0) {
1358 char const *hostname = argv[1];
1359 char const *portname = argv[1];
1362 if (*argv[1] == '[') { /* IPv6 URL encoded */
1363 p = strchr(argv[1], ']');
1364 if ((size_t) (p - argv[1]) >= sizeof(buffer)) {
1368 memcpy(buffer, argv[1] + 1, p - argv[1] - 1);
1369 buffer[p - argv[1] - 1] = '\0';
1375 p = strchr(portname, ':');
1376 if (p && (strchr(p + 1, ':') == NULL)) {
1383 if (ip_hton(&server_ipaddr, force_af, hostname, false) < 0) {
1384 ERROR("Failed to find IP address for host %s: %s", hostname, strerror(errno));
1389 * Strip port from hostname if needed.
1391 if (portname) server_port = atoi(portname);
1394 * Work backwards from the port to determine the packet type
1396 if (packet_code == PW_CODE_UNDEFINED) packet_code = radclient_get_code(server_port);
1399 radclient_get_port(packet_code, &server_port);
1404 if (argv[3]) secret = argv[3];
1407 * If no '-f' is specified, we're reading from stdin.
1409 if (rbtree_num_elements(filename_tree) == 0) {
1410 rc_file_pair_t *files;
1412 files = talloc_zero(talloc_autofree_context(), rc_file_pair_t);
1413 files->packets = "stdin";
1414 if (!radclient_init(files, files)) {
1420 * Walk over the list of filenames, creating the requests.
1422 if (rbtree_walk(filename_tree, RBTREE_IN_ORDER, filename_walk, NULL) != 0) {
1423 ERROR("Failed parsing input files");
1428 * No packets read. Die.
1430 if (!request_head) {
1431 ERROR("Nothing to send");
1436 * Bind to the first specified IP address and port.
1437 * This means we ignore later ones.
1439 if (request_head->packet->src_ipaddr.af == AF_UNSPEC) {
1440 memset(&client_ipaddr, 0, sizeof(client_ipaddr));
1441 client_ipaddr.af = server_ipaddr.af;
1444 client_ipaddr = request_head->packet->src_ipaddr;
1445 client_port = request_head->packet->src_port;
1449 sockfd = fr_tcp_client_socket(NULL, &server_ipaddr, server_port);
1452 sockfd = fr_socket(&client_ipaddr, client_port);
1454 ERROR("Error opening socket");
1458 pl = fr_packet_list_create(1);
1460 ERROR("Out of memory");
1464 if (!fr_packet_list_socket_add(pl, sockfd, ipproto, &server_ipaddr,
1465 server_port, NULL)) {
1466 ERROR("Out of memory");
1471 * Walk over the list of packets, sanity checking
1474 for (this = request_head; this != NULL; this = this->next) {
1475 this->packet->src_ipaddr = client_ipaddr;
1476 this->packet->src_port = client_port;
1477 if (radclient_sane(this) != 0) {
1483 * Walk over the packets to send, until
1486 * FIXME: This currently busy-loops until it receives
1487 * all of the packets. It should really have some sort of
1488 * send packet, get time to wait, select for time, etc.
1494 char const *filename = NULL;
1500 * Walk over the packets, sending them.
1503 for (this = request_head; this != NULL; this = next) {
1507 * If there's a packet to receive,
1508 * receive it, but don't wait for a
1514 * This packet is done. Delete it.
1522 * Packets from multiple '-f' are sent
1525 * Packets from one file are sent in
1526 * series, unless '-p' is specified, in
1527 * which case N packets from each file
1528 * are sent in parallel.
1530 if (this->files->packets != filename) {
1531 filename = this->files->packets;
1539 * Send the current packet.
1541 send_one_packet(this);
1544 * Wait a little before sending
1545 * the next packet, if told to.
1551 * Don't sleep elsewhere.
1560 tv.tv_usec = 1000000/persec;
1564 * Sleep for milliseconds,
1567 * If we get an error or
1568 * a signal, treat it like
1571 select(0, NULL, NULL, NULL, &tv);
1575 * If we haven't sent this packet
1576 * often enough, we're not done,
1577 * and we shouldn't sleep.
1579 if (this->resend < resend_count) {
1583 } else { /* haven't sent this packet, we're not done */
1584 assert(this->done == false);
1585 assert(this->reply == NULL);
1591 * Still have outstanding requests.
1593 if (fr_packet_list_num_elements(pl) > 0) {
1600 * Nothing to do until we receive a request, so
1601 * sleep until then. Once we receive one packet,
1602 * we go back, and walk through the whole list again,
1603 * sending more packets (if necessary), and updating
1606 if (!done && (sleep_time > 0)) {
1607 recv_one_packet(sleep_time);
1611 rbtree_free(filename_tree);
1612 fr_packet_list_free(pl);
1613 while (request_head) TALLOC_FREE(request_head);
1617 DEBUG("Packet summary:\n"
1618 "\tAccepted : %" PRIu64 "\n"
1619 "\tRejected : %" PRIu64 "\n"
1620 "\tLost : %" PRIu64 "\n"
1621 "\tPassed filter : %" PRIu64 "\n"
1622 "\tFailed filter : %" PRIu64,
1631 if ((stats.lost > 0) || (stats.failed > 0)) {