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 char const *radclient_version = "radclient version " RADIUSD_VERSION_STRING
77 #ifdef RADIUSD_VERSION_COMMIT
78 " (git #" STRINGIFY(RADIUSD_VERSION_COMMIT) ")"
80 ", built on " __DATE__ " at " __TIME__;
82 static void NEVER_RETURNS usage(void)
84 fprintf(stderr, "Usage: radclient [options] server[:port] <command> [<secret>]\n");
86 fprintf(stderr, " <command> One of auth, acct, status, coa, or disconnect.\n");
87 fprintf(stderr, " -4 Use IPv4 address of server\n");
88 fprintf(stderr, " -6 Use IPv6 address of server.\n");
89 fprintf(stderr, " -c <count> Send each packet 'count' times.\n");
90 fprintf(stderr, " -d <raddb> Set user dictionary directory (defaults to " RADDBDIR ").\n");
91 fprintf(stderr, " -D <dictdir> Set main dictionary directory (defaults to " DICTDIR ").\n");
92 fprintf(stderr, " -f <file>[:<file>] Read packets from file, not stdin.\n");
93 fprintf(stderr, " If a second file is provided, it will be used to verify responses\n");
94 fprintf(stderr, " -F Print the file name, packet number and reply code.\n");
95 fprintf(stderr, " -h Print usage help information.\n");
96 fprintf(stderr, " -i <id> Set request id to 'id'. Values may be 0..255\n");
97 fprintf(stderr, " -n <num> Send N requests/s\n");
98 fprintf(stderr, " -p <num> Send 'num' packets from a file in parallel.\n");
99 fprintf(stderr, " -q Do not print anything out.\n");
100 fprintf(stderr, " -r <retries> If timeout, retry sending the packet 'retries' times.\n");
101 fprintf(stderr, " -s Print out summary information of auth results.\n");
102 fprintf(stderr, " -S <file> read secret from file, not command line.\n");
103 fprintf(stderr, " -t <timeout> Wait 'timeout' seconds before retrying (may be a floating point number).\n");
104 fprintf(stderr, " -v Show program version information.\n");
105 fprintf(stderr, " -x Debugging mode.\n");
108 fprintf(stderr, " -P <proto> Use proto (tcp or udp) for transport.\n");
114 static const FR_NAME_NUMBER request_types[] = {
115 { "auth", PW_CODE_AUTHENTICATION_REQUEST },
116 { "challenge", PW_CODE_ACCESS_CHALLENGE },
117 { "acct", PW_CODE_ACCOUNTING_REQUEST },
118 { "status", PW_CODE_STATUS_SERVER },
119 { "disconnect", PW_CODE_DISCONNECT_REQUEST },
120 { "coa", PW_CODE_COA_REQUEST },
121 { "auto", PW_CODE_UNDEFINED },
127 * Free a radclient struct, which may (or may not)
128 * already be in the list.
130 static int _rc_request_free(rc_request_t *request)
132 rc_request_t *prev, *next;
134 prev = request->prev;
135 next = request->next;
138 assert(request_head != request);
140 } else if (request_head) {
141 assert(request_head == request);
146 assert(rc_request_tail != request);
148 } else if (rc_request_tail) {
149 assert(rc_request_tail == request);
150 rc_request_tail = prev;
156 static int mschapv1_encode(RADIUS_PACKET *packet, VALUE_PAIR **request,
157 char const *password)
161 VALUE_PAIR *challenge, *reply;
164 challenge = paircreate(packet, PW_MSCHAP_CHALLENGE, VENDORPEC_MICROSOFT);
169 pairadd(request, challenge);
170 challenge->length = 8;
171 challenge->vp_octets = p = talloc_array(challenge, uint8_t, challenge->length);
172 for (i = 0; i < challenge->length; i++) {
176 reply = paircreate(packet, PW_MSCHAP_RESPONSE, VENDORPEC_MICROSOFT);
181 pairadd(request, reply);
183 reply->vp_octets = p = talloc_array(reply, uint8_t, reply->length);
184 memset(p, 0, reply->length);
186 p[1] = 0x01; /* NT hash */
188 if (mschap_ntpwdhash(nthash, password) < 0) {
192 smbdes_mschap(nthash, challenge->vp_octets, p + 26);
197 static int getport(char const *name)
201 svp = getservbyname(name, "udp");
206 return ntohs(svp->s_port);
209 static void radclient_get_port(PW_CODE type, uint16_t *port)
213 case PW_CODE_AUTHENTICATION_REQUEST:
214 case PW_CODE_ACCESS_CHALLENGE:
215 case PW_CODE_STATUS_SERVER:
216 if (*port == 0) *port = getport("radius");
217 if (*port == 0) *port = PW_AUTH_UDP_PORT;
220 case PW_CODE_ACCOUNTING_REQUEST:
221 if (*port == 0) *port = getport("radacct");
222 if (*port == 0) *port = PW_ACCT_UDP_PORT;
225 case PW_CODE_DISCONNECT_REQUEST:
226 case PW_CODE_COA_REQUEST:
227 if (*port == 0) *port = PW_COA_UDP_PORT;
230 case PW_CODE_UNDEFINED:
231 if (*port == 0) *port = 0;
236 * Initialize a radclient data structure and add it to
237 * the global linked list.
239 static int radclient_init(TALLOC_CTX *ctx, rc_file_pair_t *files)
241 FILE *packets, *filters = NULL;
245 rc_request_t *request;
246 bool packets_done = false;
249 assert(files->packets != NULL);
252 * Determine where to read the VP's from.
254 if (strcmp(files->packets, "-") != 0) {
255 packets = fopen(files->packets, "r");
257 ERROR("Error opening %s: %s", files->packets, strerror(errno));
262 * Read in the pairs representing the expected response.
264 if (files->filters) {
265 filters = fopen(files->filters, "r");
267 ERROR("Error opening %s: %s", files->filters, strerror(errno));
278 * Loop until the file is done.
284 request = talloc_zero(ctx, rc_request_t);
286 ERROR("Out of memory");
290 request->packet = rad_alloc(request, 1);
291 if (!request->packet) {
292 ERROR("Out of memory");
297 request->packet->src_ipaddr = client_ipaddr;
298 request->packet->src_port = client_port;
299 request->packet->dst_ipaddr = server_ipaddr;
300 request->packet->dst_port = server_port;
301 request->packet->proto = ipproto;
304 request->files = files;
305 request->packet->id = -1; /* allocate when sending */
306 request->num = num++;
309 * Read the request VP's.
311 if (readvp2(&request->packet->vps, request->packet, packets, &packets_done) < 0) {
312 ERROR("Error parsing \"%s\"", files->packets);
319 if (!request->packet->vps) {
320 talloc_free(request);
324 fr_cursor_init(&cursor, &request->filter);
325 vp = fr_cursor_next_by_num(&cursor, PW_PACKET_TYPE, 0, TAG_ANY);
327 fr_cursor_remove(&cursor);
328 request->packet_code = vp->vp_integer;
331 request->packet_code = packet_code; /* Use the default set on the command line */
335 * Read in filter VP's.
340 if (readvp2(&request->filter, request, filters, &filters_done) < 0) {
341 ERROR("Error parsing \"%s\"", files->filters);
345 if (!request->filter) {
349 if (filters_done && !packets_done) {
350 ERROR("Differing number of packets/filters in %s:%s "
351 "(too many requests))", files->packets, files->filters);
355 if (!filters_done && packets_done) {
356 ERROR("Differing number of packets/filters in %s:%s "
357 "(too many filters))", files->packets, files->filters);
361 fr_cursor_init(&cursor, &request->filter);
362 vp = fr_cursor_next_by_num(&cursor, PW_PACKET_TYPE, 0, TAG_ANY);
364 fr_cursor_remove(&cursor);
365 request->filter_code = vp->vp_integer;
370 * xlat expansions aren't supported here
372 for (vp = fr_cursor_init(&cursor, &request->filter);
374 vp = fr_cursor_next(&cursor)) {
375 if (vp->type == VT_XLAT) {
377 vp->vp_strvalue = vp->value.xlat;
382 * This allows efficient list comparisons later
384 pairsort(&request->filter, attrtagcmp);
388 * Determine the response code from the request (if not already set)
390 if (!request->filter_code) {
391 switch (request->packet_code) {
392 case PW_CODE_AUTHENTICATION_REQUEST:
393 request->filter_code = PW_CODE_AUTHENTICATION_ACK;
396 case PW_CODE_ACCOUNTING_REQUEST:
397 request->filter_code = PW_CODE_ACCOUNTING_RESPONSE;
400 case PW_CODE_COA_REQUEST:
401 request->filter_code = PW_CODE_COA_ACK;
404 case PW_CODE_DISCONNECT_REQUEST:
405 request->filter_code = PW_CODE_DISCONNECT_ACK;
414 * Keep a copy of the the User-Password attribute.
416 if ((vp = pairfind(request->packet->vps, PW_USER_PASSWORD, 0, TAG_ANY)) != NULL) {
417 strlcpy(request->password, vp->vp_strvalue,
418 sizeof(request->password));
420 * Otherwise keep a copy of the CHAP-Password attribute.
422 } else if ((vp = pairfind(request->packet->vps, PW_CHAP_PASSWORD, 0, TAG_ANY)) != NULL) {
423 strlcpy(request->password, vp->vp_strvalue,
424 sizeof(request->password));
426 } else if ((vp = pairfind(request->packet->vps, PW_MS_CHAP_PASSWORD, 0, TAG_ANY)) != NULL) {
427 strlcpy(request->password, vp->vp_strvalue,
428 sizeof(request->password));
430 request->password[0] = '\0';
434 * Fix up Digest-Attributes issues
436 for (vp = fr_cursor_init(&cursor, &request->packet->vps);
438 vp = fr_cursor_next(&cursor)) {
440 * Double quoted strings get marked up as xlat expansions,
441 * but we don't support that in request.
443 if (vp->type == VT_XLAT) {
444 vp->vp_strvalue = vp->value.xlat;
445 vp->value.xlat = NULL;
449 if (!vp->da->vendor) switch (vp->da->attr) {
454 * Allow it to set the packet type in
455 * the attributes read from the file.
458 request->packet->code = vp->vp_integer;
461 case PW_PACKET_DST_PORT:
462 request->packet->dst_port = (vp->vp_integer & 0xffff);
465 case PW_PACKET_DST_IP_ADDRESS:
466 request->packet->dst_ipaddr.af = AF_INET;
467 request->packet->dst_ipaddr.ipaddr.ip4addr.s_addr = vp->vp_ipaddr;
470 case PW_PACKET_DST_IPV6_ADDRESS:
471 request->packet->dst_ipaddr.af = AF_INET6;
472 request->packet->dst_ipaddr.ipaddr.ip6addr = vp->vp_ipv6addr;
475 case PW_PACKET_SRC_PORT:
476 request->packet->src_port = (vp->vp_integer & 0xffff);
479 case PW_PACKET_SRC_IP_ADDRESS:
480 request->packet->src_ipaddr.af = AF_INET;
481 request->packet->src_ipaddr.ipaddr.ip4addr.s_addr = vp->vp_ipaddr;
484 case PW_PACKET_SRC_IPV6_ADDRESS:
485 request->packet->src_ipaddr.af = AF_INET6;
486 request->packet->src_ipaddr.ipaddr.ip6addr = vp->vp_ipv6addr;
489 case PW_DIGEST_REALM:
490 case PW_DIGEST_NONCE:
491 case PW_DIGEST_METHOD:
494 case PW_DIGEST_ALGORITHM:
495 case PW_DIGEST_BODY_DIGEST:
496 case PW_DIGEST_CNONCE:
497 case PW_DIGEST_NONCE_COUNT:
498 case PW_DIGEST_USER_NAME:
504 p = talloc_array(vp, uint8_t, vp->length + 2);
506 memcpy(p + 2, vp->vp_octets, vp->length);
507 p[0] = vp->da->attr - PW_DIGEST_REALM + 1;
511 da = dict_attrbyvalue(PW_DIGEST_ATTRIBUTES, 0);
513 ERROR("Out of memory");
519 * Re-do pairmemsteal ourselves,
520 * because we play games with
521 * vp->da, and pairmemsteal goes
522 * to GREAT lengths to sanitize
523 * and fix and change and
524 * double-check the various
527 memcpy(&q, &vp->vp_octets, sizeof(q));
530 vp->vp_octets = talloc_steal(vp, p);
538 } /* loop over the VP's we read in */
541 * Automatically set the port if we don't have a global
542 * or packet specific one.
544 if ((server_port == 0) && (request->packet->dst_port == 0)) {
545 radclient_get_port(request->packet->code, &request->packet->dst_port);
549 * Add it to the tail of the list.
552 assert(rc_request_tail == NULL);
553 request_head = request;
554 request->prev = NULL;
556 assert(rc_request_tail->next == NULL);
557 rc_request_tail->next = request;
558 request->prev = rc_request_tail;
560 rc_request_tail = request;
561 request->next = NULL;
562 talloc_set_destructor(request, _rc_request_free);
564 } while (!packets_done); /* loop until the file is done. */
566 if (packets != stdin) fclose(packets);
567 if (filters) fclose(filters);
575 talloc_free(request);
577 if (packets != stdin) fclose(packets);
578 if (filters) fclose(filters);
585 * Sanity check each argument.
587 static int radclient_sane(rc_request_t *request)
589 if (request->packet->dst_port == 0) {
590 request->packet->dst_port = server_port;
592 if (request->packet->dst_ipaddr.af == AF_UNSPEC) {
593 if (server_ipaddr.af == AF_UNSPEC) {
594 ERROR("No server was given, and request %" PRIu64 " in file %s did not contain "
595 "Packet-Dst-IP-Address", request->num, request->files->packets);
598 request->packet->dst_ipaddr = server_ipaddr;
600 if (request->packet->code == 0) {
601 if (packet_code == -1) {
602 ERROR("Request was \"auto\", and request %" PRIu64 " in file %s did not contain Packet-Type",
603 request->num, request->files->packets);
606 request->packet->code = packet_code;
608 request->packet->sockfd = -1;
615 * For request handling.
617 static int filename_cmp(void const *one, void const *two)
621 rc_file_pair_t const *a = one;
622 rc_file_pair_t const *b = two;
624 cmp = strcmp(a->packets, b->packets);
625 if (cmp != 0) return cmp;
627 return strcmp(a->filters, b->filters);
630 static int filename_walk(UNUSED void *context, void *data)
632 rc_file_pair_t *files = data;
635 * Read request(s) from the file.
637 if (!radclient_init(files, files)) {
638 return -1; /* stop walking */
646 * Deallocate packet ID, etc.
648 static void deallocate_id(rc_request_t *request)
650 if (!request || !request->packet ||
651 (request->packet->id < 0)) {
656 * One more unused RADIUS ID.
658 fr_packet_list_id_free(pl, request->packet, true);
661 * If we've already sent a packet, free up the old one,
662 * and ensure that the next packet has a unique
663 * authentication vector.
665 if (request->packet->data) {
666 TALLOC_FREE(request->packet->data);
669 if (request->reply) rad_free(&request->reply);
675 static int send_one_packet(rc_request_t *request)
677 assert(request->done == false);
680 * Remember when we have to wake up, to re-send the
681 * request, of we didn't receive a reply.
683 if ((sleep_time == -1) || (sleep_time > (int) timeout)) {
684 sleep_time = (int) timeout;
688 * Haven't sent the packet yet. Initialize it.
690 if (request->packet->id == -1) {
694 assert(request->reply == NULL);
697 * Didn't find a free packet ID, we're not done,
698 * we don't sleep, and we stop trying to process
702 request->packet->src_ipaddr.af = server_ipaddr.af;
703 rcode = fr_packet_list_id_alloc(pl, ipproto,
704 &request->packet, NULL);
710 mysockfd = fr_tcp_client_socket(NULL,
715 mysockfd = fr_socket(&client_ipaddr, 0);
717 ERROR("Can't open new socket: %s", strerror(errno));
720 if (!fr_packet_list_socket_add(pl, mysockfd, ipproto,
722 server_port, NULL)) {
723 ERROR("Can't add new socket");
729 assert(request->packet->id != -1);
730 assert(request->packet->data == NULL);
732 for (i = 0; i < 4; i++) {
733 ((uint32_t *) request->packet->vector)[i] = fr_rand();
737 * Update the password, so it can be encrypted with the
738 * new authentication vector.
740 if (request->password[0] != '\0') {
743 if ((vp = pairfind(request->packet->vps, PW_USER_PASSWORD, 0, TAG_ANY)) != NULL) {
744 pairstrcpy(vp, request->password);
746 } else if ((vp = pairfind(request->packet->vps, PW_CHAP_PASSWORD, 0, TAG_ANY)) != NULL) {
747 bool already_hex = false;
750 * If it's 17 octets, it *might* be already encoded.
751 * Or, it might just be a 17-character password (maybe UTF-8)
752 * Check it for non-printable characters. The odds of ALL
753 * of the characters being 32..255 is (1-7/8)^17, or (1/8)^17,
754 * or 1/(2^51), which is pretty much zero.
756 if (vp->length == 17) {
757 for (i = 0; i < 17; i++) {
758 if (vp->vp_octets[i] < 32) {
766 * Allow the user to specify ASCII or hex CHAP-Password
772 len = len2 = strlen(request->password);
773 if (len2 < 17) len2 = 17;
775 p = talloc_zero_array(vp, uint8_t, len2);
777 memcpy(p, request->password, len);
779 rad_chap_encode(request->packet,
781 fr_rand() & 0xff, vp);
785 } else if (pairfind(request->packet->vps, PW_MS_CHAP_PASSWORD, 0, TAG_ANY) != NULL) {
786 mschapv1_encode(request->packet,
787 &request->packet->vps,
790 DEBUG("WARNING: No password in the request");
794 request->timestamp = time(NULL);
802 if (client_port == 0) {
803 client_ipaddr = request->packet->src_ipaddr;
804 client_port = request->packet->src_port;
808 } else { /* request->packet->id >= 0 */
809 time_t now = time(NULL);
812 * FIXME: Accounting packets are never retried!
813 * The Acct-Delay-Time attribute is updated to
814 * reflect the delay, and the packet is re-sent
819 * Not time for a retry, do so.
821 if ((now - request->timestamp) < timeout) {
823 * When we walk over the tree sending
824 * packets, we update the minimum time
827 if ((sleep_time == -1) ||
828 (sleep_time > (now - request->timestamp))) {
829 sleep_time = now - request->timestamp;
835 * We're not trying later, maybe the packet is done.
837 if (request->tries == retries) {
838 assert(request->packet->id >= 0);
841 * Delete the request from the tree of
842 * outstanding requests.
844 fr_packet_list_yank(pl, request->packet);
846 REDEBUG("No reply from server for ID %d socket %d",
847 request->packet->id, request->packet->sockfd);
848 deallocate_id(request);
851 * Normally we mark it "done" when we've received
852 * the reply, but this is a special case.
854 if (request->resend == resend_count) {
855 request->done = true;
862 * We are trying later.
864 request->timestamp = now;
871 if (rad_send(request->packet, NULL, secret) < 0) {
872 REDEBUG("Failed to send packet for ID %d", request->packet->id);
879 * Receive one packet, maybe.
881 static int recv_one_packet(int wait_time)
885 rc_request_t *request;
886 RADIUS_PACKET *reply, **packet_p;
889 /* And wait for reply, timing out as necessary */
892 max_fd = fr_packet_list_fd_set(pl, &set);
893 if (max_fd < 0) exit(1); /* no sockets to listen on! */
895 if (wait_time <= 0) {
898 tv.tv_sec = wait_time;
903 * No packet was received.
905 if (select(max_fd, &set, NULL, NULL, &tv) <= 0) {
910 * Look for the packet.
912 reply = fr_packet_list_recv(pl, &set);
914 ERROR("Received bad packet");
917 * If the packet is bad, we close the socket.
918 * I'm not sure how to do that now, so we just
923 return -1; /* bad packet */
927 * udpfromto issues. We may have bound to "*",
928 * and we want to find the replies that are sent to
931 reply->dst_ipaddr = client_ipaddr;
932 reply->dst_port = client_port;
934 reply->src_ipaddr = server_ipaddr;
935 reply->src_port = server_port;
938 packet_p = fr_packet_list_find_byreply(pl, reply);
940 ERROR("Received reply to request we did not send. (id=%d socket %d)",
941 reply->id, reply->sockfd);
943 return -1; /* got reply to packet we didn't send */
945 request = fr_packet2myptr(rc_request_t, packet, packet_p);
948 * Fails the signature validation: not a real reply.
949 * FIXME: Silently drop it and listen for another packet.
951 if (rad_verify(reply, request->packet, secret) < 0) {
952 REDEBUG("Reply verification failed");
954 goto packet_done; /* shared secret is incorrect */
957 if (print_filename) {
958 RDEBUG("%s response code %d", request->files->packets, reply->code);
961 deallocate_id(request);
962 request->reply = reply;
966 * If this fails, we're out of memory.
968 if (rad_decode(request->reply, request->packet, secret) != 0) {
969 REDEBUG("Reply decode failed");
975 * Increment counters...
977 switch (request->reply->code) {
978 case PW_CODE_AUTHENTICATION_ACK:
979 case PW_CODE_ACCOUNTING_RESPONSE:
980 case PW_CODE_COA_ACK:
981 case PW_CODE_DISCONNECT_ACK:
985 case PW_CODE_ACCESS_CHALLENGE:
993 * If we had an expected response code, check to see if the
994 * packet matched that.
996 if (request->reply->code != request->filter_code) {
997 if (is_radius_code(request->packet_code)) {
998 REDEBUG("Expected %s got %s", fr_packet_codes[request->filter_code],
999 fr_packet_codes[request->reply->code]);
1001 REDEBUG("Expected %u got %i", request->filter_code,
1002 request->reply->code);
1006 * Check if the contents of the packet matched the filter
1008 } else if (!request->filter) {
1011 VALUE_PAIR const *failed[2];
1013 pairsort(&request->reply->vps, attrtagcmp);
1014 if (pairvalidate(failed, request->filter, request->reply->vps)) {
1015 RDEBUG("Response passed filter");
1018 pairvalidate_debug(request, failed);
1019 REDEBUG("Response failed filter");
1024 if (request->resend == resend_count) {
1025 request->done = true;
1029 rad_free(&request->reply);
1030 rad_free(&reply); /* may be NULL */
1035 int main(int argc, char **argv)
1038 char const *radius_dir = RADDBDIR;
1039 char const *dict_dir = DICTDIR;
1040 char filesecret[256];
1042 int do_summary = false;
1046 int force_af = AF_UNSPEC;
1052 if (fr_fault_setup(getenv("PANIC_ACTION"), argv[0]) < 0) {
1053 fr_perror("radclient");
1058 talloc_set_log_stderr();
1060 filename_tree = rbtree_create(filename_cmp, NULL, 0);
1061 if (!filename_tree) {
1063 ERROR("Out of memory");
1067 while ((c = getopt(argc, argv, "46c:d:D:f:Fhi:n:p:qr:sS:t:vx"
1071 )) != EOF) switch(c) {
1076 force_af = AF_INET6;
1079 if (!isdigit((int) *optarg))
1081 resend_count = atoi(optarg);
1087 radius_dir = optarg;
1092 rc_file_pair_t *files;
1094 files = talloc(talloc_autofree_context(), rc_file_pair_t);
1095 if (!files) goto oom;
1097 p = strchr(optarg, ':');
1099 files->packets = talloc_strndup(files, optarg, p - optarg);
1100 if (!files->packets) goto oom;
1101 files->filters = p + 1;
1103 files->packets = optarg;
1104 files->filters = NULL;
1106 rbtree_insert(filename_tree, (void *) files);
1110 print_filename = true;
1112 case 'i': /* currently broken */
1113 if (!isdigit((int) *optarg))
1115 last_used_id = atoi(optarg);
1116 if ((last_used_id < 0) || (last_used_id > 255)) {
1122 persec = atoi(optarg);
1123 if (persec <= 0) usage();
1127 * Note that sending MANY requests in
1128 * parallel can over-run the kernel
1129 * queues, and Linux will happily discard
1130 * packets. So even if the server responds,
1131 * the client may not see the reply.
1134 parallel = atoi(optarg);
1135 if (parallel <= 0) usage();
1141 if (strcmp(proto, "tcp") != 0) {
1142 if (strcmp(proto, "udp") == 0) {
1148 ipproto = IPPROTO_TCP;
1156 fr_log_fp = NULL; /* no output from you, either! */
1159 if (!isdigit((int) *optarg))
1161 retries = atoi(optarg);
1162 if ((retries == 0) || (retries > 1000)) usage();
1170 fp = fopen(optarg, "r");
1172 ERROR("Error opening %s: %s", optarg, fr_syserror(errno));
1175 if (fgets(filesecret, sizeof(filesecret), fp) == NULL) {
1176 ERROR("Error reading %s: %s", optarg, fr_syserror(errno));
1181 /* truncate newline */
1182 p = filesecret + strlen(filesecret) - 1;
1183 while ((p >= filesecret) &&
1189 if (strlen(filesecret) < 2) {
1190 ERROR("Secret in %s is too short", optarg);
1193 secret = filesecret;
1197 if (!isdigit((int) *optarg))
1199 timeout = atof(optarg);
1202 DEBUG("%s", radclient_version);
1213 argc -= (optind - 1);
1214 argv += (optind - 1);
1216 if ((argc < 3) || ((secret == NULL) && (argc < 4))) {
1217 ERROR("Insufficient arguments");
1221 * Mismatch between the binary and the libraries it depends on
1223 if (fr_check_lib_magic(RADIUSD_MAGIC_NUMBER) < 0) {
1224 fr_perror("radclient");
1228 if (dict_init(dict_dir, RADIUS_DICTIONARY) < 0) {
1229 fr_perror("radclient");
1233 if (dict_read(radius_dir, RADIUS_DICTIONARY) == -1) {
1234 fr_perror("radclient");
1237 fr_strerror(); /* Clear the error buffer */
1241 * Get the request type
1243 if (!isdigit((int) argv[2][0])) {
1244 packet_code = fr_str2int(request_types, argv[2], -2);
1245 if (packet_code == -2) {
1246 ERROR("Unrecognised request type \"%s\"", argv[2]);
1250 packet_code = atoi(argv[2]);
1256 if (force_af == AF_UNSPEC) force_af = AF_INET;
1257 server_ipaddr.af = force_af;
1258 if (strcmp(argv[1], "-") != 0) {
1260 char const *hostname = argv[1];
1261 char const *portname = argv[1];
1264 if (*argv[1] == '[') { /* IPv6 URL encoded */
1265 p = strchr(argv[1], ']');
1266 if ((size_t) (p - argv[1]) >= sizeof(buffer)) {
1270 memcpy(buffer, argv[1] + 1, p - argv[1] - 1);
1271 buffer[p - argv[1] - 1] = '\0';
1277 p = strchr(portname, ':');
1278 if (p && (strchr(p + 1, ':') == NULL)) {
1285 if (ip_hton(&server_ipaddr, force_af, hostname, false) < 0) {
1286 ERROR("Failed to find IP address for host %s: %s", hostname, strerror(errno));
1291 * Strip port from hostname if needed.
1293 if (portname) server_port = atoi(portname);
1296 radclient_get_port(packet_code, &server_port);
1301 if (argv[3]) secret = argv[3];
1304 * If no '-f' is specified, we're reading from stdin.
1306 if (rbtree_num_elements(filename_tree) == 0) {
1307 rc_file_pair_t *files;
1309 files = talloc_zero(talloc_autofree_context(), rc_file_pair_t);
1310 files->packets = "-";
1311 if (!radclient_init(files, files)) {
1317 * Walk over the list of filenames, creating the requests.
1319 if (rbtree_walk(filename_tree, RBTREE_IN_ORDER, filename_walk, NULL) != 0) {
1320 ERROR("Failed parsing input files");
1325 * No packets read. Die.
1327 if (!request_head) {
1328 ERROR("Nothing to send");
1333 * Bind to the first specified IP address and port.
1334 * This means we ignore later ones.
1336 if (request_head->packet->src_ipaddr.af == AF_UNSPEC) {
1337 memset(&client_ipaddr, 0, sizeof(client_ipaddr));
1338 client_ipaddr.af = server_ipaddr.af;
1341 client_ipaddr = request_head->packet->src_ipaddr;
1342 client_port = request_head->packet->src_port;
1346 sockfd = fr_tcp_client_socket(NULL, &server_ipaddr, server_port);
1349 sockfd = fr_socket(&client_ipaddr, client_port);
1351 ERROR("Error opening socket");
1355 pl = fr_packet_list_create(1);
1357 ERROR("Out of memory");
1361 if (!fr_packet_list_socket_add(pl, sockfd, ipproto, &server_ipaddr,
1362 server_port, NULL)) {
1363 ERROR("Out of memory");
1368 * Walk over the list of packets, sanity checking
1371 for (this = request_head; this != NULL; this = this->next) {
1372 this->packet->src_ipaddr = client_ipaddr;
1373 this->packet->src_port = client_port;
1374 if (radclient_sane(this) != 0) {
1380 * Walk over the packets to send, until
1383 * FIXME: This currently busy-loops until it receives
1384 * all of the packets. It should really have some sort of
1385 * send packet, get time to wait, select for time, etc.
1391 char const *filename = NULL;
1397 * Walk over the packets, sending them.
1400 for (this = request_head; this != NULL; this = next) {
1404 * If there's a packet to receive,
1405 * receive it, but don't wait for a
1411 * This packet is done. Delete it.
1419 * Packets from multiple '-f' are sent
1422 * Packets from one file are sent in
1423 * series, unless '-p' is specified, in
1424 * which case N packets from each file
1425 * are sent in parallel.
1427 if (this->files->packets != filename) {
1428 filename = this->files->packets;
1436 * Send the current packet.
1438 send_one_packet(this);
1441 * Wait a little before sending
1442 * the next packet, if told to.
1448 * Don't sleep elsewhere.
1457 tv.tv_usec = 1000000/persec;
1461 * Sleep for milliseconds,
1464 * If we get an error or
1465 * a signal, treat it like
1468 select(0, NULL, NULL, NULL, &tv);
1472 * If we haven't sent this packet
1473 * often enough, we're not done,
1474 * and we shouldn't sleep.
1476 if (this->resend < resend_count) {
1480 } else { /* haven't sent this packet, we're not done */
1481 assert(this->done == false);
1482 assert(this->reply == NULL);
1488 * Still have outstanding requests.
1490 if (fr_packet_list_num_elements(pl) > 0) {
1497 * Nothing to do until we receive a request, so
1498 * sleep until then. Once we receive one packet,
1499 * we go back, and walk through the whole list again,
1500 * sending more packets (if necessary), and updating
1503 if (!done && (sleep_time > 0)) {
1504 recv_one_packet(sleep_time);
1508 rbtree_free(filename_tree);
1509 fr_packet_list_free(pl);
1510 while (request_head) TALLOC_FREE(request_head);
1514 DEBUG("Packet summary:\n"
1515 "\tAccess-Accepts : %" PRIu64 "\n"
1516 "\tAccess-Rejects : %" PRIu64 "\n"
1517 "\tLost : %" PRIu64 "\n"
1518 "\tPassed filter : %" PRIu64 "\n"
1519 "\tFailed filter : %" PRIu64,
1528 if ((stats.lost > 0) || (stats.failed > 0)) {