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");
289 talloc_set_destructor(request, _rc_request_free);
291 request->packet = rad_alloc(request, 1);
292 if (!request->packet) {
293 ERROR("Out of memory");
298 request->packet->src_ipaddr = client_ipaddr;
299 request->packet->src_port = client_port;
300 request->packet->dst_ipaddr = server_ipaddr;
301 request->packet->dst_port = server_port;
302 request->packet->proto = ipproto;
305 request->files = files;
306 request->packet->id = -1; /* allocate when sending */
307 request->num = num++;
310 * Read the request VP's.
312 if (readvp2(&request->packet->vps, request->packet, packets, &packets_done) < 0) {
313 ERROR("Error parsing \"%s\"", files->packets);
317 fr_cursor_init(&cursor, &request->filter);
318 vp = fr_cursor_next_by_num(&cursor, PW_PACKET_TYPE, 0, TAG_ANY);
320 fr_cursor_remove(&cursor);
321 request->packet_code = vp->vp_integer;
324 request->packet_code = packet_code; /* Use the default set on the command line */
328 * Read in filter VP's.
333 if (readvp2(&request->filter, request, filters, &filters_done) < 0) {
334 ERROR("Error parsing \"%s\"", files->filters);
338 if (!request->filter) {
342 if (filters_done && !packets_done) {
343 ERROR("Differing number of packets/filters in %s:%s "
344 "(too many requests))", files->packets, files->filters);
348 if (!filters_done && packets_done) {
349 ERROR("Differing number of packets/filters in %s:%s "
350 "(too many filters))", files->packets, files->filters);
354 fr_cursor_init(&cursor, &request->filter);
355 vp = fr_cursor_next_by_num(&cursor, PW_PACKET_TYPE, 0, TAG_ANY);
357 fr_cursor_remove(&cursor);
358 request->filter_code = vp->vp_integer;
363 * xlat expansions aren't supported here
365 for (vp = fr_cursor_init(&cursor, &request->filter);
367 vp = fr_cursor_next(&cursor)) {
368 if (vp->type == VT_XLAT) {
370 vp->vp_strvalue = vp->value.xlat;
375 * This allows efficient list comparisons later
377 pairsort(&request->filter, attrtagcmp);
381 * Determine the response code from the request (if not already set)
383 if (!request->filter_code) {
384 switch (request->packet_code) {
385 case PW_CODE_AUTHENTICATION_REQUEST:
386 request->filter_code = PW_CODE_AUTHENTICATION_ACK;
389 case PW_CODE_ACCOUNTING_REQUEST:
390 request->filter_code = PW_CODE_ACCOUNTING_RESPONSE;
393 case PW_CODE_COA_REQUEST:
394 request->filter_code = PW_CODE_COA_ACK;
397 case PW_CODE_DISCONNECT_REQUEST:
398 request->filter_code = PW_CODE_DISCONNECT_ACK;
407 * Keep a copy of the the User-Password attribute.
409 if ((vp = pairfind(request->packet->vps, PW_USER_PASSWORD, 0, TAG_ANY)) != NULL) {
410 strlcpy(request->password, vp->vp_strvalue,
411 sizeof(request->password));
413 * Otherwise keep a copy of the CHAP-Password attribute.
415 } else if ((vp = pairfind(request->packet->vps, PW_CHAP_PASSWORD, 0, TAG_ANY)) != NULL) {
416 strlcpy(request->password, vp->vp_strvalue,
417 sizeof(request->password));
419 } else if ((vp = pairfind(request->packet->vps, PW_MSCHAP_PASSWORD, 0, TAG_ANY)) != NULL) {
420 strlcpy(request->password, vp->vp_strvalue,
421 sizeof(request->password));
423 request->password[0] = '\0';
427 * Fix up Digest-Attributes issues
429 for (vp = fr_cursor_init(&cursor, &request->packet->vps);
431 vp = fr_cursor_next(&cursor)) {
433 * Double quoted strings get marked up as xlat expansions,
434 * but we don't support that in request.
436 if (vp->type == VT_XLAT) {
437 vp->vp_strvalue = vp->value.xlat;
438 vp->value.xlat = NULL;
442 if (!vp->da->vendor) switch (vp->da->attr) {
447 * Allow it to set the packet type in
448 * the attributes read from the file.
451 request->packet->code = vp->vp_integer;
454 case PW_PACKET_DST_PORT:
455 request->packet->dst_port = (vp->vp_integer & 0xffff);
458 case PW_PACKET_DST_IP_ADDRESS:
459 request->packet->dst_ipaddr.af = AF_INET;
460 request->packet->dst_ipaddr.ipaddr.ip4addr.s_addr = vp->vp_ipaddr;
463 case PW_PACKET_DST_IPV6_ADDRESS:
464 request->packet->dst_ipaddr.af = AF_INET6;
465 request->packet->dst_ipaddr.ipaddr.ip6addr = vp->vp_ipv6addr;
468 case PW_PACKET_SRC_PORT:
469 request->packet->src_port = (vp->vp_integer & 0xffff);
472 case PW_PACKET_SRC_IP_ADDRESS:
473 request->packet->src_ipaddr.af = AF_INET;
474 request->packet->src_ipaddr.ipaddr.ip4addr.s_addr = vp->vp_ipaddr;
477 case PW_PACKET_SRC_IPV6_ADDRESS:
478 request->packet->src_ipaddr.af = AF_INET6;
479 request->packet->src_ipaddr.ipaddr.ip6addr = vp->vp_ipv6addr;
482 case PW_DIGEST_REALM:
483 case PW_DIGEST_NONCE:
484 case PW_DIGEST_METHOD:
487 case PW_DIGEST_ALGORITHM:
488 case PW_DIGEST_BODY_DIGEST:
489 case PW_DIGEST_CNONCE:
490 case PW_DIGEST_NONCE_COUNT:
491 case PW_DIGEST_USER_NAME:
497 p = talloc_array(vp, uint8_t, vp->length + 2);
499 memcpy(p + 2, vp->vp_octets, vp->length);
500 p[0] = vp->da->attr - PW_DIGEST_REALM + 1;
504 da = dict_attrbyvalue(PW_DIGEST_ATTRIBUTES, 0);
506 ERROR("Out of memory");
512 * Re-do pairmemsteal ourselves,
513 * because we play games with
514 * vp->da, and pairmemsteal goes
515 * to GREAT lengths to sanitize
516 * and fix and change and
517 * double-check the various
520 memcpy(&q, &vp->vp_octets, sizeof(q));
523 vp->vp_octets = talloc_steal(vp, p);
531 } /* loop over the VP's we read in */
534 * Automatically set the port if we don't have a global
535 * or packet specific one.
537 if ((server_port == 0) && (request->packet->dst_port == 0)) {
538 radclient_get_port(request->packet->code, &request->packet->dst_port);
542 * Add it to the tail of the list.
545 assert(rc_request_tail == NULL);
546 request_head = request;
547 request->prev = NULL;
549 assert(rc_request_tail->next == NULL);
550 rc_request_tail->next = request;
551 request->prev = rc_request_tail;
553 rc_request_tail = request;
554 request->next = NULL;
556 } while (!packets_done); /* loop until the file is done. */
558 if (packets != stdin) fclose(packets);
559 if (filters) fclose(filters);
567 talloc_free(request);
569 if (packets != stdin) fclose(packets);
570 if (filters) fclose(filters);
577 * Sanity check each argument.
579 static int radclient_sane(rc_request_t *request)
581 if (request->packet->dst_port == 0) {
582 request->packet->dst_port = server_port;
584 if (request->packet->dst_ipaddr.af == AF_UNSPEC) {
585 if (server_ipaddr.af == AF_UNSPEC) {
586 ERROR("No server was given, and request %" PRIu64 " in file %s did not contain "
587 "Packet-Dst-IP-Address", request->num, request->files->packets);
590 request->packet->dst_ipaddr = server_ipaddr;
592 if (request->packet->code == 0) {
593 if (packet_code == -1) {
594 ERROR("Request was \"auto\", and request %" PRIu64 " in file %s did not contain Packet-Type",
595 request->num, request->files->packets);
598 request->packet->code = packet_code;
600 request->packet->sockfd = -1;
607 * For request handling.
609 static int filename_cmp(void const *one, void const *two)
613 rc_file_pair_t const *a = one;
614 rc_file_pair_t const *b = two;
616 cmp = strcmp(a->packets, b->packets);
617 if (cmp != 0) return cmp;
619 return strcmp(a->filters, b->filters);
622 static int filename_walk(UNUSED void *context, void *data)
624 rc_file_pair_t *files = data;
627 * Read request(s) from the file.
629 if (!radclient_init(files, files)) {
630 return -1; /* stop walking */
638 * Deallocate packet ID, etc.
640 static void deallocate_id(rc_request_t *request)
642 if (!request || !request->packet ||
643 (request->packet->id < 0)) {
648 * One more unused RADIUS ID.
650 fr_packet_list_id_free(pl, request->packet, true);
653 * If we've already sent a packet, free up the old one,
654 * and ensure that the next packet has a unique
655 * authentication vector.
657 if (request->packet->data) {
658 TALLOC_FREE(request->packet->data);
661 if (request->reply) rad_free(&request->reply);
667 static int send_one_packet(rc_request_t *request)
669 assert(request->done == false);
672 * Remember when we have to wake up, to re-send the
673 * request, of we didn't receive a reply.
675 if ((sleep_time == -1) || (sleep_time > (int) timeout)) {
676 sleep_time = (int) timeout;
680 * Haven't sent the packet yet. Initialize it.
682 if (request->packet->id == -1) {
686 assert(request->reply == NULL);
689 * Didn't find a free packet ID, we're not done,
690 * we don't sleep, and we stop trying to process
694 request->packet->src_ipaddr.af = server_ipaddr.af;
695 rcode = fr_packet_list_id_alloc(pl, ipproto,
696 &request->packet, NULL);
702 mysockfd = fr_tcp_client_socket(NULL,
707 mysockfd = fr_socket(&client_ipaddr, 0);
709 ERROR("Can't open new socket: %s", strerror(errno));
712 if (!fr_packet_list_socket_add(pl, mysockfd, ipproto,
714 server_port, NULL)) {
715 ERROR("Can't add new socket");
721 assert(request->packet->id != -1);
722 assert(request->packet->data == NULL);
724 for (i = 0; i < 4; i++) {
725 ((uint32_t *) request->packet->vector)[i] = fr_rand();
729 * Update the password, so it can be encrypted with the
730 * new authentication vector.
732 if (request->password[0] != '\0') {
735 if ((vp = pairfind(request->packet->vps, PW_USER_PASSWORD, 0, TAG_ANY)) != NULL) {
736 pairstrcpy(vp, request->password);
738 } else if ((vp = pairfind(request->packet->vps, PW_CHAP_PASSWORD, 0, TAG_ANY)) != NULL) {
739 bool already_hex = false;
742 * If it's 17 octets, it *might* be already encoded.
743 * Or, it might just be a 17-character password (maybe UTF-8)
744 * Check it for non-printable characters. The odds of ALL
745 * of the characters being 32..255 is (1-7/8)^17, or (1/8)^17,
746 * or 1/(2^51), which is pretty much zero.
748 if (vp->length == 17) {
749 for (i = 0; i < 17; i++) {
750 if (vp->vp_octets[i] < 32) {
758 * Allow the user to specify ASCII or hex CHAP-Password
764 len = len2 = strlen(request->password);
765 if (len2 < 17) len2 = 17;
767 p = talloc_zero_array(vp, uint8_t, len2);
769 memcpy(p, request->password, len);
771 rad_chap_encode(request->packet,
773 fr_rand() & 0xff, vp);
777 } else if (pairfind(request->packet->vps, PW_MSCHAP_PASSWORD, 0, TAG_ANY) != NULL) {
778 mschapv1_encode(request->packet,
779 &request->packet->vps,
782 DEBUG("WARNING: No password in the request");
786 request->timestamp = time(NULL);
794 if (client_port == 0) {
795 client_ipaddr = request->packet->src_ipaddr;
796 client_port = request->packet->src_port;
800 } else { /* request->packet->id >= 0 */
801 time_t now = time(NULL);
804 * FIXME: Accounting packets are never retried!
805 * The Acct-Delay-Time attribute is updated to
806 * reflect the delay, and the packet is re-sent
811 * Not time for a retry, do so.
813 if ((now - request->timestamp) < timeout) {
815 * When we walk over the tree sending
816 * packets, we update the minimum time
819 if ((sleep_time == -1) ||
820 (sleep_time > (now - request->timestamp))) {
821 sleep_time = now - request->timestamp;
827 * We're not trying later, maybe the packet is done.
829 if (request->tries == retries) {
830 assert(request->packet->id >= 0);
833 * Delete the request from the tree of
834 * outstanding requests.
836 fr_packet_list_yank(pl, request->packet);
838 REDEBUG("No reply from server for ID %d socket %d",
839 request->packet->id, request->packet->sockfd);
840 deallocate_id(request);
843 * Normally we mark it "done" when we've received
844 * the reply, but this is a special case.
846 if (request->resend == resend_count) {
847 request->done = true;
854 * We are trying later.
856 request->timestamp = now;
863 if (rad_send(request->packet, NULL, secret) < 0) {
864 REDEBUG("Failed to send packet for ID %d", request->packet->id);
871 * Receive one packet, maybe.
873 static int recv_one_packet(int wait_time)
877 rc_request_t *request;
878 RADIUS_PACKET *reply, **packet_p;
881 /* And wait for reply, timing out as necessary */
884 max_fd = fr_packet_list_fd_set(pl, &set);
885 if (max_fd < 0) exit(1); /* no sockets to listen on! */
887 if (wait_time <= 0) {
890 tv.tv_sec = wait_time;
895 * No packet was received.
897 if (select(max_fd, &set, NULL, NULL, &tv) <= 0) {
902 * Look for the packet.
904 reply = fr_packet_list_recv(pl, &set);
906 ERROR("Received bad packet");
909 * If the packet is bad, we close the socket.
910 * I'm not sure how to do that now, so we just
915 return -1; /* bad packet */
919 * udpfromto issues. We may have bound to "*",
920 * and we want to find the replies that are sent to
923 reply->dst_ipaddr = client_ipaddr;
924 reply->dst_port = client_port;
926 reply->src_ipaddr = server_ipaddr;
927 reply->src_port = server_port;
930 packet_p = fr_packet_list_find_byreply(pl, reply);
932 ERROR("Received reply to request we did not send. (id=%d socket %d)",
933 reply->id, reply->sockfd);
935 return -1; /* got reply to packet we didn't send */
937 request = fr_packet2myptr(rc_request_t, packet, packet_p);
940 * Fails the signature validation: not a real reply.
941 * FIXME: Silently drop it and listen for another packet.
943 if (rad_verify(reply, request->packet, secret) < 0) {
944 REDEBUG("Reply verification failed");
946 goto packet_done; /* shared secret is incorrect */
949 if (print_filename) {
950 RDEBUG("%s response code %d", request->files->packets, reply->code);
953 deallocate_id(request);
954 request->reply = reply;
958 * If this fails, we're out of memory.
960 if (rad_decode(request->reply, request->packet, secret) != 0) {
961 REDEBUG("Reply decode failed");
967 * Increment counters...
969 switch (request->reply->code) {
970 case PW_CODE_AUTHENTICATION_ACK:
971 case PW_CODE_ACCOUNTING_RESPONSE:
972 case PW_CODE_COA_ACK:
973 case PW_CODE_DISCONNECT_ACK:
977 case PW_CODE_ACCESS_CHALLENGE:
985 * If we had an expected response code, check to see if the
986 * packet matched that.
988 if (request->reply->code != request->filter_code) {
989 if (is_radius_code(request->packet_code)) {
990 REDEBUG("Expected %s got %s", fr_packet_codes[request->filter_code],
991 fr_packet_codes[request->reply->code]);
993 REDEBUG("Expected %u got %i", request->filter_code,
994 request->reply->code);
998 * Check if the contents of the packet matched the filter
1000 } else if (!request->filter) {
1003 VALUE_PAIR const *failed[2];
1005 pairsort(&request->reply->vps, attrtagcmp);
1006 if (pairvalidate(failed, request->filter, request->reply->vps)) {
1007 RDEBUG("Response passed filter");
1010 pairvalidate_debug(request, failed);
1011 REDEBUG("Response failed filter");
1016 if (request->resend == resend_count) {
1017 request->done = true;
1021 rad_free(&request->reply);
1022 rad_free(&reply); /* may be NULL */
1027 int main(int argc, char **argv)
1030 char const *radius_dir = RADDBDIR;
1031 char const *dict_dir = DICTDIR;
1032 char filesecret[256];
1034 int do_summary = false;
1038 int force_af = AF_UNSPEC;
1044 if (fr_fault_setup(getenv("PANIC_ACTION"), argv[0]) < 0) {
1045 fr_perror("radclient");
1050 talloc_set_log_stderr();
1052 filename_tree = rbtree_create(filename_cmp, NULL, 0);
1053 if (!filename_tree) {
1055 ERROR("Out of memory");
1059 while ((c = getopt(argc, argv, "46c:d:D:f:Fhi:n:p:qr:sS:t:vx"
1063 )) != EOF) switch(c) {
1068 force_af = AF_INET6;
1071 if (!isdigit((int) *optarg))
1073 resend_count = atoi(optarg);
1079 radius_dir = optarg;
1084 rc_file_pair_t *files;
1086 files = talloc(talloc_autofree_context(), rc_file_pair_t);
1087 if (!files) goto oom;
1089 p = strchr(optarg, ':');
1091 files->packets = talloc_strndup(files, optarg, p - optarg);
1092 if (!files->packets) goto oom;
1093 files->filters = p + 1;
1095 files->packets = optarg;
1096 files->filters = NULL;
1098 rbtree_insert(filename_tree, (void *) files);
1102 print_filename = true;
1104 case 'i': /* currently broken */
1105 if (!isdigit((int) *optarg))
1107 last_used_id = atoi(optarg);
1108 if ((last_used_id < 0) || (last_used_id > 255)) {
1114 persec = atoi(optarg);
1115 if (persec <= 0) usage();
1119 * Note that sending MANY requests in
1120 * parallel can over-run the kernel
1121 * queues, and Linux will happily discard
1122 * packets. So even if the server responds,
1123 * the client may not see the reply.
1126 parallel = atoi(optarg);
1127 if (parallel <= 0) usage();
1133 if (strcmp(proto, "tcp") != 0) {
1134 if (strcmp(proto, "udp") == 0) {
1140 ipproto = IPPROTO_TCP;
1148 fr_log_fp = NULL; /* no output from you, either! */
1151 if (!isdigit((int) *optarg))
1153 retries = atoi(optarg);
1154 if ((retries == 0) || (retries > 1000)) usage();
1162 fp = fopen(optarg, "r");
1164 ERROR("Error opening %s: %s", optarg, fr_syserror(errno));
1167 if (fgets(filesecret, sizeof(filesecret), fp) == NULL) {
1168 ERROR("Error reading %s: %s", optarg, fr_syserror(errno));
1173 /* truncate newline */
1174 p = filesecret + strlen(filesecret) - 1;
1175 while ((p >= filesecret) &&
1181 if (strlen(filesecret) < 2) {
1182 ERROR("Secret in %s is too short", optarg);
1185 secret = filesecret;
1189 if (!isdigit((int) *optarg))
1191 timeout = atof(optarg);
1194 DEBUG("%s", radclient_version);
1205 argc -= (optind - 1);
1206 argv += (optind - 1);
1208 if ((argc < 3) || ((secret == NULL) && (argc < 4))) {
1209 ERROR("Insufficient arguments");
1213 * Mismatch between the binary and the libraries it depends on
1215 if (fr_check_lib_magic(RADIUSD_MAGIC_NUMBER) < 0) {
1216 fr_perror("radclient");
1220 if (dict_init(dict_dir, RADIUS_DICTIONARY) < 0) {
1221 fr_perror("radclient");
1225 if (dict_read(radius_dir, RADIUS_DICTIONARY) == -1) {
1226 fr_perror("radclient");
1229 fr_strerror(); /* Clear the error buffer */
1233 * Get the request type
1235 if (!isdigit((int) argv[2][0])) {
1236 packet_code = fr_str2int(request_types, argv[2], -2);
1237 if (packet_code == -2) {
1238 ERROR("Unrecognised request type \"%s\"", argv[2]);
1242 packet_code = atoi(argv[2]);
1248 if (force_af == AF_UNSPEC) force_af = AF_INET;
1249 server_ipaddr.af = force_af;
1250 if (strcmp(argv[1], "-") != 0) {
1252 char const *hostname = argv[1];
1253 char const *portname = argv[1];
1256 if (*argv[1] == '[') { /* IPv6 URL encoded */
1257 p = strchr(argv[1], ']');
1258 if ((size_t) (p - argv[1]) >= sizeof(buffer)) {
1262 memcpy(buffer, argv[1] + 1, p - argv[1] - 1);
1263 buffer[p - argv[1] - 1] = '\0';
1269 p = strchr(portname, ':');
1270 if (p && (strchr(p + 1, ':') == NULL)) {
1277 if (ip_hton(force_af, hostname, &server_ipaddr) < 0) {
1278 ERROR("Failed to find IP address for host %s: %s", hostname, strerror(errno));
1283 * Strip port from hostname if needed.
1285 if (portname) server_port = atoi(portname);
1288 radclient_get_port(packet_code, &server_port);
1293 if (argv[3]) secret = argv[3];
1296 * If no '-f' is specified, we're reading from stdin.
1298 if (rbtree_num_elements(filename_tree) == 0) {
1299 rc_file_pair_t *files;
1301 files = talloc_zero(talloc_autofree_context(), rc_file_pair_t);
1302 files->packets = "-";
1303 if (!radclient_init(files, files)) {
1309 * Walk over the list of filenames, creating the requests.
1311 if (rbtree_walk(filename_tree, RBTREE_IN_ORDER, filename_walk, NULL) != 0) {
1312 ERROR("Failed parsing input files");
1317 * No packets read. Die.
1319 if (!request_head) {
1320 ERROR("Nothing to send");
1325 * Bind to the first specified IP address and port.
1326 * This means we ignore later ones.
1328 if (request_head->packet->src_ipaddr.af == AF_UNSPEC) {
1329 memset(&client_ipaddr, 0, sizeof(client_ipaddr));
1330 client_ipaddr.af = server_ipaddr.af;
1333 client_ipaddr = request_head->packet->src_ipaddr;
1334 client_port = request_head->packet->src_port;
1338 sockfd = fr_tcp_client_socket(NULL, &server_ipaddr, server_port);
1341 sockfd = fr_socket(&client_ipaddr, client_port);
1343 ERROR("Error opening socket");
1347 pl = fr_packet_list_create(1);
1349 ERROR("Out of memory");
1353 if (!fr_packet_list_socket_add(pl, sockfd, ipproto, &server_ipaddr,
1354 server_port, NULL)) {
1355 ERROR("Out of memory");
1360 * Walk over the list of packets, sanity checking
1363 for (this = request_head; this != NULL; this = this->next) {
1364 this->packet->src_ipaddr = client_ipaddr;
1365 this->packet->src_port = client_port;
1366 if (radclient_sane(this) != 0) {
1372 * Walk over the packets to send, until
1375 * FIXME: This currently busy-loops until it receives
1376 * all of the packets. It should really have some sort of
1377 * send packet, get time to wait, select for time, etc.
1383 char const *filename = NULL;
1389 * Walk over the packets, sending them.
1392 for (this = request_head; this != NULL; this = next) {
1396 * If there's a packet to receive,
1397 * receive it, but don't wait for a
1403 * This packet is done. Delete it.
1411 * Packets from multiple '-f' are sent
1414 * Packets from one file are sent in
1415 * series, unless '-p' is specified, in
1416 * which case N packets from each file
1417 * are sent in parallel.
1419 if (this->files->packets != filename) {
1420 filename = this->files->packets;
1428 * Send the current packet.
1430 send_one_packet(this);
1433 * Wait a little before sending
1434 * the next packet, if told to.
1440 * Don't sleep elsewhere.
1449 tv.tv_usec = 1000000/persec;
1453 * Sleep for milliseconds,
1456 * If we get an error or
1457 * a signal, treat it like
1460 select(0, NULL, NULL, NULL, &tv);
1464 * If we haven't sent this packet
1465 * often enough, we're not done,
1466 * and we shouldn't sleep.
1468 if (this->resend < resend_count) {
1472 } else { /* haven't sent this packet, we're not done */
1473 assert(this->done == false);
1474 assert(this->reply == NULL);
1480 * Still have outstanding requests.
1482 if (fr_packet_list_num_elements(pl) > 0) {
1489 * Nothing to do until we receive a request, so
1490 * sleep until then. Once we receive one packet,
1491 * we go back, and walk through the whole list again,
1492 * sending more packets (if necessary), and updating
1495 if (!done && (sleep_time > 0)) {
1496 recv_one_packet(sleep_time);
1500 rbtree_free(filename_tree);
1501 fr_packet_list_free(pl);
1502 while (request_head) TALLOC_FREE(request_head);
1506 DEBUG("Packet summary:\n"
1507 "\tAccess-Accepts : %" PRIu64 "\n"
1508 "\tAccess-Rejects : %" PRIu64 "\n"
1509 "\tLost : %" PRIu64 "\n"
1510 "\tPassed filter : %" PRIu64 "\n"
1511 "\tFailed filter : %" PRIu64,
1520 if ((stats.lost > 0) || (stats.failed > 0)) {