The FreeRADIUS server $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
Loading...
Searching...
No Matches
radclient.c
Go to the documentation of this file.
1/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
15 */
16
17/**
18 * $Id: 6be13ccedb81db0450743b83bf519c4c2cffbb80 $
19 *
20 * @file src/bin/radclient.c
21 * @brief General radius client and debug tool.
22 *
23 * @copyright 2000,2006,2014 The FreeRADIUS server project
24 * @copyright 2000 Miquel van Smoorenburg (miquels@cistron.nl)
25 * @copyright 2000 Alan DeKok (aland@freeradius.org)
26 */
27
28RCSID("$Id: 6be13ccedb81db0450743b83bf519c4c2cffbb80 $")
29
30#include <freeradius-devel/util/conf.h>
31#include <freeradius-devel/util/syserror.h>
32#include <freeradius-devel/util/atexit.h>
33#include <freeradius-devel/util/pair_legacy.h>
34#include <freeradius-devel/util/time.h>
35#include <freeradius-devel/server/packet.h>
36#include <freeradius-devel/radius/list.h>
37#include <freeradius-devel/radius/radius.h>
38#include <freeradius-devel/util/chap.h>
39#ifdef HAVE_OPENSSL_SSL_H
40#include <openssl/ssl.h>
41#include <freeradius-devel/util/md5.h>
42#include <freeradius-devel/util/md4.h>
43#endif
44#include <ctype.h>
45
46#ifdef HAVE_GETOPT_H
47# include <getopt.h>
48#endif
49
50#include <assert.h>
51
52typedef struct request_s request_t; /* to shut up warnings about mschap.h */
53
54#include "smbdes.h"
55#include "mschap.h"
56
57#include "radclient.h"
58
59#define pair_update_request(_attr, _da) do { \
60 _attr = fr_pair_find_by_da(&request->request_pairs, NULL, _da); \
61 if (!_attr) { \
62 _attr = fr_pair_afrom_da(request, _da); \
63 assert(_attr != NULL); \
64 fr_pair_append(&request->request_pairs, _attr); \
65 } \
66 } while (0)
67
68static int retries = 3;
69static fr_time_delta_t timeout = fr_time_delta_wrap((int64_t)5 * NSEC); /* 5 seconds */
71static char *secret = NULL;
72static bool do_output = true;
73
74static const char *attr_coa_filter_name = "User-Name";
75
77
81static int resend_count = 1;
82static int ignore_count = 0;
83static bool done = true;
84static bool print_filename = false;
85static bool blast_radius = false;
86
89
90static int sockfd;
91static int last_used_id = -1;
92
93static int ipproto = IPPROTO_UDP;
94
95static bool do_coa = false;
96static int coafd;
98static fr_rb_tree_t *coa_tree = NULL;
99
101
102static FR_DLIST_HEAD(rc_request_list) rc_request_list;
103
104static char const *radclient_version = RADIUSD_VERSION_BUILD("radclient");
105
106static fr_dict_t const *dict_freeradius;
107static fr_dict_t const *dict_radius;
108
109extern fr_dict_autoload_t radclient_dict[];
110fr_dict_autoload_t radclient_dict[] = {
111 { .out = &dict_freeradius, .proto = "freeradius" },
112 { .out = &dict_radius, .proto = "radius" },
114};
115
117
121
124
131
134
136
139 { .out = &attr_cleartext_password, .name = "Password.Cleartext", .type = FR_TYPE_STRING, .dict = &dict_freeradius },
140 { .out = &attr_ms_chap_challenge, .name = "Vendor-Specific.Microsoft.CHAP-Challenge", .type = FR_TYPE_OCTETS, .dict = &dict_radius },
141 { .out = &attr_ms_chap_password, .name = "Password.MS-CHAP", .type = FR_TYPE_STRING, .dict = &dict_freeradius },
142 { .out = &attr_ms_chap_response, .name = "Vendor-Specific.Microsoft.CHAP-Response", .type = FR_TYPE_OCTETS, .dict = &dict_radius },
143
144 { .out = &attr_radclient_test_name, .name = "Radclient-Test-Name", .type = FR_TYPE_STRING, .dict = &dict_freeradius },
145 { .out = &attr_request_authenticator, .name = "Request-Authenticator", .type = FR_TYPE_OCTETS, .dict = &dict_freeradius },
146
147 { .out = &attr_radclient_coa_filename, .name = "Radclient-CoA-Filename", .type = FR_TYPE_STRING, .dict = &dict_freeradius },
148 { .out = &attr_radclient_coa_filter, .name = "Radclient-CoA-Filter", .type = FR_TYPE_STRING, .dict = &dict_freeradius },
149
150 { .out = &attr_chap_password, .name = "CHAP-Password", .type = FR_TYPE_OCTETS, .dict = &dict_radius },
151 { .out = &attr_chap_challenge, .name = "CHAP-Challenge", .type = FR_TYPE_OCTETS, .dict = &dict_radius },
152 { .out = &attr_packet_type, .name = "Packet-Type", .type = FR_TYPE_UINT32, .dict = &dict_radius },
153 { .out = &attr_user_password, .name = "User-Password", .type = FR_TYPE_STRING, .dict = &dict_radius },
154 { .out = &attr_user_name, .name = "User-Name", .type = FR_TYPE_STRING, .dict = &dict_radius },
155 { .out = &attr_proxy_state, .name = "Proxy-State", .type = FR_TYPE_OCTETS, .dict = &dict_radius },
156
158};
159
160static NEVER_RETURNS void usage(void)
161{
162 fprintf(stderr, "Usage: radclient [options] server[:port] <command> [<secret>]\n");
163
164 fprintf(stderr, " <command> One of auth, acct, status, coa, disconnect or auto.\n");
165 fprintf(stderr, " -4 Use IPv4 address of server\n");
166 fprintf(stderr, " -6 Use IPv6 address of server.\n");
167 fprintf(stderr, " -A <attribute> Use named 'attribute' to match CoA requests to packets. Default is User-Name\n");
168 fprintf(stderr, " -b Mandate checks for Blast RADIUS issue (this is not set by default).\n");
169 fprintf(stderr, " -C [<client_ip>:]<client_port> Client source port and source IP address. Port values may be 1..65535\n");
170 fprintf(stderr, " -c <count> Send each packet 'count' times.\n");
171 fprintf(stderr, " -d <confdir> Set user dictionary directory (defaults to " CONFDIR ").\n");
172 fprintf(stderr, " -D <dictdir> Set main dictionary directory (defaults to " DICTDIR ").\n");
173 fprintf(stderr, " -f <request>[:<expected>][:<coa_reply>][:<coa_expected>] Read packets from file, not stdin.\n");
174 fprintf(stderr, " If a second file is provided, it will be used to verify responses\n");
175 fprintf(stderr, " -F Print the file name, packet number and reply code.\n");
176 fprintf(stderr, " -h Print usage help information.\n");
177 fprintf(stderr, " -i <id> Set request id to 'id'. Values may be 0..255\n");
178 fprintf(stderr, " -n <num> Send N requests/s\n");
179 fprintf(stderr, " -o <port> Set CoA listening port (defaults to 3799)\n");
180 fprintf(stderr, " -p <num> Send 'num' packets from a file in parallel.\n");
181 fprintf(stderr, " -P <proto> Use proto (tcp or udp) for transport.\n");
182 fprintf(stderr, " -r <retries> If timeout, retry sending the packet 'retries' times.\n");
183 fprintf(stderr, " -s Print out summary information of auth results.\n");
184 fprintf(stderr, " -S <file> read secret from file, not command line.\n");
185 fprintf(stderr, " -t <timeout> Wait 'timeout' seconds before retrying (may be a floating point number).\n");
186 fprintf(stderr, " -v Show program version information.\n");
187 fprintf(stderr, " -x Debugging mode.\n");
188
189 fr_exit_now(EXIT_SUCCESS);
190}
191
192/*
193 * Free a radclient struct, which may (or may not)
194 * already be in the list.
195 */
196static int _rc_request_free(rc_request_t *request)
197{
198 rc_request_list_remove(&rc_request_list, request);
199
200 if (do_coa) (void) fr_rb_delete_by_inline_node(coa_tree, &request->node);
201
202 return 0;
203}
204
205#ifdef HAVE_OPENSSL_SSL_H
206#include <openssl/provider.h>
207
208static OSSL_PROVIDER *openssl_default_provider = NULL;
209static OSSL_PROVIDER *openssl_legacy_provider = NULL;
210
211static int openssl3_init(void)
212{
213 /*
214 * Load the default provider for most algorithms
215 */
216 openssl_default_provider = OSSL_PROVIDER_load(NULL, "default");
217 if (!openssl_default_provider) {
218 ERROR("(TLS) Failed loading default provider");
219 return -1;
220 }
221
222 /*
223 * Needed for MD4
224 *
225 * https://www.openssl.org/docs/man3.0/man7/migration_guide.html#Legacy-Algorithms
226 */
227 openssl_legacy_provider = OSSL_PROVIDER_load(NULL, "legacy");
228 if (!openssl_legacy_provider) {
229 ERROR("(TLS) Failed loading legacy provider");
230 return -1;
231 }
232
233 fr_md5_openssl_init();
234 fr_md4_openssl_init();
235
236 return 0;
237}
238
239static void openssl3_free(void)
240{
241 if (openssl_default_provider && !OSSL_PROVIDER_unload(openssl_default_provider)) {
242 ERROR("Failed unloading default provider");
243 }
244 openssl_default_provider = NULL;
245
246 if (openssl_legacy_provider && !OSSL_PROVIDER_unload(openssl_legacy_provider)) {
247 ERROR("Failed unloading legacy provider");
248 }
249 openssl_legacy_provider = NULL;
250
251 fr_md5_openssl_free();
252 fr_md4_openssl_free();
253}
254#else
255#define openssl3_init()
256#define openssl3_free()
257#endif
258
260 char const *password)
261{
262 unsigned int i;
263 uint8_t *p;
264 fr_pair_t *challenge, *reply;
265 uint8_t nthash[16];
266
269
271
272 fr_pair_append(list, challenge);
273
274 MEM(p = talloc_array(challenge, uint8_t, 8));
275 fr_pair_value_memdup_buffer_shallow(challenge, p, false);
276
277 for (i = 0; i < challenge->vp_length; i++) {
278 p[i] = fr_rand();
279 }
280
282 fr_pair_append(list, reply);
283 p = talloc_zero_array(reply, uint8_t, 50); /* really reply->da->flags.length */
285
286 p[1] = 0x01; /* NT hash */
287
288 if (mschap_nt_password_hash(nthash, password) < 0) return 0;
289
290 smbdes_mschap(nthash, challenge->vp_octets, p + 26);
291 return 1;
292}
293
294
295static int getport(char const *name)
296{
297 struct servent *svp;
298
299 svp = getservbyname(name, "udp");
300 if (!svp) return 0;
301
302 return ntohs(svp->s_port);
303}
304
305/*
306 * Set a port from the request type if we don't already have one
307 */
309{
310 switch (type) {
311 default:
315 if (*port == 0) *port = getport("radius");
316 if (*port == 0) *port = FR_AUTH_UDP_PORT;
317 return;
318
320 if (*port == 0) *port = getport("radacct");
321 if (*port == 0) *port = FR_ACCT_UDP_PORT;
322 return;
323
325 if (*port == 0) *port = FR_POD_UDP_PORT;
326 return;
327
329 if (*port == 0) *port = FR_COA_UDP_PORT;
330 return;
331
333 if (*port == 0) *port = 0;
334 return;
335 }
336}
337
338/*
339 * Resolve a port to a request type
340 */
342{
343 /*
344 * getport returns 0 if the service doesn't exist
345 * so we need to return early, to avoid incorrect
346 * codes.
347 */
348 if (port == 0) return FR_RADIUS_CODE_UNDEFINED;
349
350 if ((port == getport("radius")) || (port == FR_AUTH_UDP_PORT) || (port == FR_AUTH_UDP_PORT_ALT)) {
352 }
353 if ((port == getport("radacct")) || (port == FR_ACCT_UDP_PORT) || (port == FR_ACCT_UDP_PORT_ALT)) {
355 }
357
359}
360
361
363{
364 size_t i;
365
366 if (!vp || (vp->vp_type != FR_TYPE_OCTETS)) return true;
367
368 /*
369 * If it's 17 octets, it *might* be already encoded.
370 * Or, it might just be a 17-character password (maybe UTF-8)
371 * Check it for non-printable characters. The odds of ALL
372 * of the characters being 32..255 is (1-7/8)^17, or (1/8)^17,
373 * or 1/(2^51), which is pretty much zero.
374 */
375 for (i = 0; i < vp->vp_length; i++) {
376 if (vp->vp_octets[i] < 32) {
377 return true;
378 }
379 }
380
381 return false;
382}
383
384/** Read one CoA reply and potentially a filter
385 *
386 *
387 */
389 FILE *coa_reply, char const *reply_filename, bool *coa_reply_done,
390 FILE *coa_filter, char const *filter_filename, bool *coa_filter_done)
391{
392 rc_request_t *request;
393 fr_pair_t *vp;
394
395 /*
396 * Allocate it.
397 */
398 MEM(request = talloc_zero(parent, rc_request_t));
399 MEM(request->reply = fr_packet_alloc(request, false));
400
401 /*
402 * Don't initialize src/dst IP/port, or anything else. That will be read from the network.
403 */
404 fr_pair_list_init(&request->filter);
407
408 /*
409 * Read the reply VP's.
410 */
412 &request->reply_pairs, coa_reply, coa_reply_done, true) < 0) {
413 REDEBUG("Error parsing \"%s\"", reply_filename);
414 error:
415 talloc_free(request);
416 return -1;
417 }
418
419 /*
420 * The reply can be empty. In which case we just send an empty ACK.
421 */
423 if (vp) request->reply->code = vp->vp_uint32;
424
425 /*
426 * Read in filter VP's.
427 */
428 if (coa_filter) {
430 &request->filter, coa_filter, coa_filter_done, true) < 0) {
431 REDEBUG("Error parsing \"%s\"", filter_filename);
432 goto error;
433 }
434
435 if (*coa_filter_done && !*coa_reply_done) {
436 REDEBUG("Differing number of replies/filters in %s:%s "
437 "(too many replies))", reply_filename, filter_filename);
438 goto error;
439 }
440
441 if (!*coa_filter_done && *coa_reply_done) {
442 REDEBUG("Differing number of replies/filters in %s:%s "
443 "(too many filters))", reply_filename, filter_filename);
444 goto error;
445 }
446
447 /*
448 * This allows efficient list comparisons later
449 */
451 }
452
453 request->name = parent->name;
454
455 /*
456 * Automatically set the response code from the request code
457 * (if one wasn't already set).
458 */
459 if (request->filter_code == FR_RADIUS_CODE_UNDEFINED) {
461 }
462
463 parent->coa = request;
464
465 /*
466 * Ensure that the packet is also tracked in the CoA tree.
467 */
470 ERROR("Failed inserting packet from %s into CoA tree", request->name);
471 fr_exit_now(1);
472 }
473
474 return 0;
475}
476
477/*
478 * Initialize a radclient data structure and add it to
479 * the global linked list.
480 */
481static int radclient_init(TALLOC_CTX *ctx, rc_file_pair_t *files)
482{
483 FILE *packets, *filters = NULL;
484
485 fr_pair_t *vp;
486 rc_request_t *request = NULL;
487 bool packets_done = false;
488 uint64_t num = 0;
489
490 FILE *coa_reply = NULL;
491 FILE *coa_filter = NULL;
492 bool coa_reply_done = false;
493 bool coa_filter_done = false;
494
495 assert(files->packets != NULL);
496
497 /*
498 * Determine where to read the VP's from.
499 */
500 if (strcmp(files->packets, "-") != 0) {
501 packets = fopen(files->packets, "r");
502 if (!packets) {
503 ERROR("Error opening %s: %s", files->packets, fr_syserror(errno));
504 return -1;
505 }
506
507 /*
508 * Read in the pairs representing the expected response.
509 */
510 if (files->filters) {
511 filters = fopen(files->filters, "r");
512 if (!filters) {
513 ERROR("Error opening %s: %s", files->filters, fr_syserror(errno));
514 goto error;
515 }
516 }
517
518 if (files->coa_reply) {
519 coa_reply = fopen(files->coa_reply, "r");
520 if (!coa_reply) {
521 ERROR("Error opening %s: %s", files->coa_reply, fr_syserror(errno));
522 goto error;
523 }
524 }
525
526 if (files->coa_filter) {
527 coa_filter = fopen(files->coa_filter, "r");
528 if (!coa_filter) {
529 ERROR("Error opening %s: %s", files->coa_filter, fr_syserror(errno));
530 goto error;
531 }
532 }
533 } else {
534 packets = stdin;
535 }
536
537 /*
538 * Loop until the file is done.
539 */
540 do {
541 char const *coa_reply_filename = NULL;
542 char const *coa_filter_filename = NULL;
543
544 /*
545 * Allocate it.
546 */
547 MEM(request = talloc_zero(ctx, rc_request_t));
548 MEM(request->packet = fr_packet_alloc(request, true));
549 request->packet->uctx = request;
550
551 request->packet->socket.inet.src_ipaddr = client_ipaddr;
552 request->packet->socket.inet.src_port = client_port;
553 request->packet->socket.inet.dst_ipaddr = server_ipaddr;
554 request->packet->socket.inet.dst_port = server_port;
555 request->packet->socket.type = (ipproto == IPPROTO_TCP) ? SOCK_STREAM : SOCK_DGRAM;
556
557 request->files = files;
558 request->packet->id = last_used_id;
559 request->num = num++;
560
561 fr_pair_list_init(&request->filter);
564
565 /*
566 * Read the request VP's.
567 */
569 &request->request_pairs, packets, &packets_done, true) < 0) {
570 char const *input;
571
572 if ((files->packets[0] == '-') && (files->packets[1] == '\0')) {
573 input = "stdin";
574 } else {
575 input = files->packets;
576 }
577
578 REDEBUG("Error parsing \"%s\"", input);
579 goto error;
580 }
581
582 /*
583 * Skip empty entries
584 */
585 if (fr_pair_list_empty(&request->request_pairs)) {
586 WARN("Skipping \"%s\": No Attributes", files->packets);
587 talloc_free(request);
588 continue;
589 }
590
591 /*
592 * Read in filter VP's.
593 */
594 if (filters) {
595 bool filters_done;
596
598 &request->filter, filters, &filters_done, true) < 0) {
599 REDEBUG("Error parsing \"%s\"", files->filters);
600 goto error;
601 }
602
603 if (filters_done && !packets_done) {
604 REDEBUG("Differing number of packets/filters in %s:%s "
605 "(too many requests))", files->packets, files->filters);
606 goto error;
607 }
608
609 if (!filters_done && packets_done) {
610 REDEBUG("Differing number of packets/filters in %s:%s "
611 "(too many filters))", files->packets, files->filters);
612 goto error;
613 }
614
615 vp = fr_pair_find_by_da(&request->filter, NULL, attr_packet_type);
616 if (vp) {
617 request->filter_code = vp->vp_uint32;
618 fr_pair_delete(&request->filter, vp);
619 }
620
621 /*
622 * This allows efficient list comparisons later
623 */
625 }
626
627 /*
628 * Process special attributes
629 */
630 for (vp = fr_pair_list_head(&request->request_pairs);
631 vp;
632 vp = fr_pair_list_next(&request->request_pairs, vp)) {
633 /*
634 * Allow it to set the packet type in
635 * the attributes read from the file.
636 */
637 if (vp->da == attr_packet_type) {
638 request->packet->code = vp->vp_uint32;
639 } else if (vp->da == attr_request_authenticator) {
640 if (vp->vp_length > sizeof(request->packet->vector)) {
641 memcpy(request->packet->vector, vp->vp_octets, sizeof(request->packet->vector));
642 } else {
643 memset(request->packet->vector, 0, sizeof(request->packet->vector));
644 memcpy(request->packet->vector, vp->vp_octets, vp->vp_length);
645 }
646 } else if (vp->da == attr_cleartext_password) {
647 request->password = vp;
648 /*
649 * Keep a copy of the the password attribute.
650 */
651 } else if (vp->da == attr_chap_password) {
652 /*
653 * If it's already hex, do nothing.
654 */
655 if ((vp->vp_length == 17) && (already_hex(vp))) continue;
656
657 /*
658 * CHAP-Password is octets, so it may not be zero terminated.
659 */
661 fr_pair_value_bstrndup(request->password, vp->vp_strvalue, vp->vp_length, true);
662 } else if (vp->da == attr_ms_chap_password) {
664 fr_pair_value_bstrndup(request->password, vp->vp_strvalue, vp->vp_length, true);
665
666 } else if (vp->da == attr_radclient_test_name) {
667 request->name = vp->vp_strvalue;
668
669 /*
670 * Allow these to be dynamically specified
671 * in the request file, as well as on the
672 * command line.
673 */
674 } else if (vp->da == attr_radclient_coa_filename) {
675 coa_reply_filename = vp->vp_strvalue;
676
677 } else if (vp->da == attr_radclient_coa_filter) {
678 coa_filter_filename = vp->vp_strvalue;
679 }
680 } /* loop over the VP's we read in */
681
682 /*
683 * Use the default set on the command line
684 */
685 if (request->packet->code == FR_RADIUS_CODE_UNDEFINED) request->packet->code = packet_code;
686
687 /*
688 * Fill in the packet header from attributes, and then
689 * re-realize the attributes.
690 */
691 fr_packet_net_from_pairs(request->packet, &request->request_pairs);
692
693 /*
694 * Default to the filename
695 */
696 if (!request->name) request->name = request->files->packets;
697
698 /*
699 * Automatically set the response code from the request code
700 * (if one wasn't already set).
701 */
702 if (request->filter_code == FR_RADIUS_CODE_UNDEFINED) {
703 switch (request->packet->code) {
706 break;
707
710 break;
711
714 break;
715
718 break;
719
721 switch (radclient_get_code(request->packet->socket.inet.dst_port)) {
724 break;
725
728 break;
729
730 default:
732 break;
733 }
734 break;
735
737 REDEBUG("Packet-Type must be defined,"
738 "or a well known RADIUS port");
739 goto error;
740
741 default:
742 REDEBUG("Can't determine expected reply.Packet-Type for Packet-Type %i",
743 request->packet->code);
744 goto error;
745 }
746 /*
747 * Automatically set the request code from the response code
748 * (if one wasn't already set).
749 */
750 } else if (request->packet->code == FR_RADIUS_CODE_UNDEFINED) {
751 switch (request->filter_code) {
755 break;
756
759 break;
760
764 break;
765
769 break;
770
771 default:
772 REDEBUG("Can't determine expected Packet-Type for reply.Packet-Type %i",
773 request->filter_code);
774 goto error;
775 }
776 }
777
778 /*
779 * Automatically set the dst port (if one wasn't already set).
780 */
781 if (request->packet->socket.inet.dst_port == 0) {
782 radclient_get_port(request->packet->code, &request->packet->socket.inet.dst_port);
783 if (request->packet->socket.inet.dst_port == 0) {
784 REDEBUG("Can't determine destination port");
785 goto error;
786 }
787 }
788
789 /*
790 * Read in the CoA filename and filter.
791 */
792 if (coa_reply_filename) {
793 if (coa_reply) {
794 RDEBUG("Cannot specify CoA file on both the command line and via Radclient-CoA-Filename");
795 goto error;
796 }
797
798 coa_reply = fopen(coa_reply_filename, "r");
799 if (!coa_reply) {
800 ERROR("Error opening %s: %s", coa_reply_filename, fr_syserror(errno));
801 goto error;
802 }
803
804 if (coa_filter_filename) {
805 coa_filter = fopen(coa_filter_filename, "r");
806 if (!coa_filter) {
807 ERROR("Error opening %s: %s", coa_filter_filename, fr_syserror(errno));
808 goto error;
809 }
810 } else {
811 coa_filter = NULL;
812 }
813
814 /*
815 * Read in one CoA reply and/or filter
816 */
817 if (coa_init(request,
818 coa_reply, coa_reply_filename, &coa_reply_done,
819 coa_filter, coa_filter_filename, &coa_filter_done) < 0) {
820 goto error;
821 }
822
823 fclose(coa_reply);
824 coa_reply = NULL;
825 if (coa_filter) {
826 fclose(coa_filter);
827 coa_filter = NULL;
828 }
829 do_coa = true;
830
831 } else if (coa_reply) {
832 if (coa_init(request,
833 coa_reply, coa_reply_filename, &coa_reply_done,
834 coa_filter, coa_filter_filename, &coa_filter_done) < 0) {
835 goto error;
836 }
837
838 if (coa_reply_done != packets_done) {
839 REDEBUG("Differing number of packets in input file and coa_reply in %s:%s ",
840 files->packets, files->coa_reply);
841 goto error;
842
843 }
844 }
845
846 /*
847 * Add it to the tail of the list.
848 */
849 rc_request_list_insert_tail(&rc_request_list, request);
850
851 /*
852 * Set the destructor so it removes itself from the
853 * request list when freed. We don't set this until
854 * the packet is actually in the list, else we trigger
855 * the asserts in the free callback.
856 */
857 talloc_set_destructor(request, _rc_request_free);
858 } while (!packets_done); /* loop until the file is done. */
859
860 if (packets != stdin) fclose(packets);
861 if (filters) fclose(filters);
862 if (coa_reply) fclose(coa_reply);
863 if (coa_filter) fclose(coa_filter);
864
865 /*
866 * And we're done.
867 */
868 return 0;
869
870error:
871 talloc_free(request);
872
873 if (packets != stdin) fclose(packets);
874 if (filters) fclose(filters);
875 if (coa_reply) fclose(coa_reply);
876 if (coa_filter) fclose(coa_filter);
877
878 return -1;
879}
880
881
882/*
883 * Sanity check each argument.
884 */
885static int radclient_sane(rc_request_t *request)
886{
887 if (request->packet->socket.inet.dst_port == 0) {
888 request->packet->socket.inet.dst_port = server_port;
889 }
890 if (request->packet->socket.inet.dst_ipaddr.af == AF_UNSPEC) {
891 if (server_ipaddr.af == AF_UNSPEC) {
892 ERROR("No server was given, and request %" PRIu64 " in file %s did not contain "
893 "Packet-Dst-IP-Address", request->num, request->files->packets);
894 return -1;
895 }
896 request->packet->socket.inet.dst_ipaddr = server_ipaddr;
897 }
898 if (request->packet->code == 0) {
899 if (packet_code == -1) {
900 ERROR("Request was \"auto\", and request %" PRIu64 " in file %s did not contain Packet-Type",
901 request->num, request->files->packets);
902 return -1;
903 }
904 request->packet->code = packet_code;
905 }
906 request->packet->socket.fd = -1;
907
908 return 0;
909}
910
911
912static int8_t request_cmp(void const *one, void const *two)
913{
914 rc_request_t const *a = one, *b = two;
915 fr_pair_t *vp1, *vp2;
916
918 vp2 = fr_pair_find_by_da(&b->request_pairs, NULL, attr_coa_match_attr);
919
920 if (!vp1) return -1;
921 if (!vp2) return +1;
922
923 return fr_value_box_cmp(&vp1->data, &vp2->data);
924}
925
926
927/*
928 * Deallocate packet ID, etc.
929 */
930static void deallocate_id(rc_request_t *request)
931{
932 if (!request || !request->packet ||
933 (request->packet->id < 0)) {
934 return;
935 }
936
937 /*
938 * One more unused RADIUS ID.
939 */
941
942 /*
943 * If we've already sent a packet, free up the old one,
944 * and ensure that the next packet has a unique
945 * authentication vector.
946 */
947 if (request->packet->data) TALLOC_FREE(request->packet->data);
948 if (request->reply) fr_packet_free(&request->reply);
949}
950
951/*
952 * Send one packet.
953 */
954static int send_one_packet(rc_request_t *request)
955{
956 assert(request->done == false);
957
958#ifdef STATIC_ANALYZER
959 if (!secret) fr_exit_now(1);
960#endif
961
962 /*
963 * Remember when we have to wake up, to re-send the
964 * request, of we didn't receive a reply.
965 */
968 }
969
970 /*
971 * Haven't sent the packet yet. Initialize it.
972 */
973 if (!request->tries || request->packet->id == -1) {
974 bool rcode;
975
976 assert(request->reply == NULL);
977
978 /*
979 * Didn't find a free packet ID, we're not done,
980 * we don't sleep, and we stop trying to process
981 * this packet.
982 */
983 retry:
984 request->packet->socket.inet.src_ipaddr.af = server_ipaddr.af;
985 rcode = fr_packet_list_id_alloc(packet_list, ipproto, request->packet, NULL);
986 if (!rcode) {
987 int mysockfd;
988
989 if (ipproto == IPPROTO_TCP) {
990 mysockfd = fr_socket_client_tcp(NULL, NULL,
991 &request->packet->socket.inet.dst_ipaddr,
992 request->packet->socket.inet.dst_port, false);
993 if (mysockfd < 0) {
994 fr_perror("Error opening socket");
995 return -1;
996 }
997 } else {
998 uint16_t port = 0;
999
1000 mysockfd = fr_socket_server_udp(&client_ipaddr, &port, NULL, true);
1001 if (mysockfd < 0) {
1002 fr_perror("Error opening socket");
1003 return -1;
1004 }
1005
1006 if (fr_socket_bind(mysockfd, NULL, &client_ipaddr, &port) < 0) {
1007 fr_perror("Error binding socket");
1008 return -1;
1009 }
1010 }
1011
1013 &request->packet->socket.inet.dst_ipaddr,
1014 request->packet->socket.inet.dst_port, NULL)) {
1015 ERROR("Can't add new socket");
1016 fr_exit_now(1);
1017 }
1018 goto retry;
1019 }
1020
1021 assert(request->packet->id != -1);
1022 assert(request->packet->data == NULL);
1023
1024 /*
1025 * Update the password, so it can be encrypted with the
1026 * new authentication vector.
1027 */
1028 if (request->password) {
1029 fr_pair_t *vp;
1030
1031 if ((vp = fr_pair_find_by_da(&request->request_pairs, NULL, attr_chap_password)) != NULL) {
1032 uint8_t buffer[17];
1033 fr_pair_t *challenge;
1034
1035 /*
1036 * Use CHAP-Challenge pair if present, otherwise create CHAP-Challenge and
1037 * populate with current Request Authenticator.
1038 *
1039 * Request Authenticator is re-calculated by fr_packet_sign
1040 */
1041 challenge = fr_pair_find_by_da(&request->request_pairs, NULL, attr_chap_challenge);
1042 if (!challenge || (challenge->vp_length < 7)) {
1044 fr_pair_value_memdup(challenge, request->packet->vector, RADIUS_AUTH_VECTOR_LENGTH, false);
1045 }
1046
1048 fr_rand() & 0xff, challenge->vp_octets, challenge->vp_length,
1049 request->password->vp_strvalue,
1050 request->password->vp_length);
1051 fr_pair_value_memdup(vp, buffer, sizeof(buffer), false);
1052
1053 } else if (fr_pair_find_by_da_nested(&request->request_pairs, NULL, attr_ms_chap_password) != NULL) {
1054 mschapv1_encode(request->packet, &request->request_pairs, request->password->vp_strvalue);
1055
1056 } else {
1057 DEBUG("WARNING: No password in the request");
1058 }
1059 }
1060
1061 request->timestamp = fr_time();
1062 request->tries = 1;
1063 request->resend++;
1064
1065 } else { /* request->packet->id >= 0 */
1066 fr_time_t now = fr_time();
1067
1068 /*
1069 * FIXME: Accounting packets are never retried!
1070 * The Acct-Delay-Time attribute is updated to
1071 * reflect the delay, and the packet is re-sent
1072 * from scratch!
1073 */
1074
1075 /*
1076 * Not time for a retry, do so.
1077 */
1078 if (fr_time_delta_lt(fr_time_sub(now, request->timestamp), timeout)) {
1079 /*
1080 * When we walk over the tree sending
1081 * packets, we update the minimum time
1082 * required to sleep.
1083 */
1086 sleep_time = fr_time_sub(now, request->timestamp);
1087 }
1088 return 0;
1089 }
1090
1091 /*
1092 * We're not trying later, maybe the packet is done.
1093 */
1094 if (request->tries == retries) {
1095 assert(request->packet->id >= 0);
1096
1097 /*
1098 * Delete the request from the tree of
1099 * outstanding requests.
1100 */
1102
1103 REDEBUG("No reply from server for ID %d socket %d",
1104 request->packet->id, request->packet->socket.fd);
1105 deallocate_id(request);
1106
1107 /*
1108 * Normally we mark it "done" when we've received
1109 * the reply, but this is a special case.
1110 */
1111 if (request->resend == resend_count) {
1112 request->done = true;
1113 }
1114 stats.lost++;
1115 return -1;
1116 }
1117
1118 /*
1119 * We are trying later.
1120 */
1121 request->timestamp = now;
1122 request->tries++;
1123 }
1124
1125 /*
1126 * Send the packet.
1127 */
1128 if (fr_packet_send(request->packet, &request->request_pairs, NULL, secret) < 0) {
1129 REDEBUG("Failed to send packet for ID %d", request->packet->id);
1130 deallocate_id(request);
1131 request->done = true;
1132 return -1;
1133 }
1134
1135 fr_radius_packet_log(&default_log, request->packet, &request->request_pairs, false);
1136
1137 return 0;
1138}
1139
1140/*
1141 * Receive a CoA packet, maybe.
1142 */
1144{
1145 fd_set set;
1146 fr_time_delta_t our_wait_time;
1147 rc_request_t *request, *parent;
1148 fr_packet_t *packet;
1150
1151#ifdef STATIC_ANALYZER
1152 if (!secret) fr_exit_now(1);
1153#endif
1154
1155 /* And wait for reply, timing out as necessary */
1156 FD_ZERO(&set);
1157 FD_SET(coafd, &set);
1158
1159 our_wait_time = !fr_time_delta_ispos(wait_time) ? fr_time_delta_from_sec(0) : wait_time;
1160
1161 /*
1162 * No packet was received.
1163 */
1164 if (select(coafd + 1, &set, NULL, NULL, &fr_time_delta_to_timeval(our_wait_time)) <= 0) return 0;
1165
1166 /*
1167 * Read a packet from a network.
1168 */
1169 packet = fr_packet_recv(NULL, coafd, 0, 200, false);
1170 if (!packet) {
1171 DEBUG("Failed reading CoA packet");
1172 return 0;
1173 }
1174
1175 /*
1176 * Fails the signature validation: not a real reply
1177 */
1178 if (fr_packet_verify(packet, NULL, secret) < 0) {
1179 DEBUG("CoA verification failed");
1180 return 0;
1181 }
1182
1183 fr_pair_list_init(&my.request_pairs);
1184
1185 /*
1186 * Decode the packet before looking up the parent, so that we can compare the pairs.
1187 */
1188 if (fr_radius_decode_simple(packet, &my.request_pairs,
1189 packet->data, packet->data_len,
1190 NULL, secret) < 0) {
1191 DEBUG("Failed decoding CoA packet");
1192 return 0;
1193 }
1194
1195 fr_radius_packet_log(&default_log, packet, &my.request_pairs, true);
1196
1197 /*
1198 * Find a Access-Request which has the same User-Name / etc. as this CoA packet.
1199 */
1200 my.name = "receive CoA request";
1201 my.packet = packet;
1202
1204 if (!parent) {
1205 DEBUG("No matching request packet for CoA packet %u %u", packet->data[0], packet->data[1]);
1206 talloc_free(packet);
1207 return 0;
1208 }
1209 assert(parent->coa);
1210
1211 request = parent->coa;
1212 request->packet = talloc_steal(request, packet);
1213
1215 fr_pair_list_append(&request->request_pairs, &my.request_pairs);
1216
1217 /*
1218 * If we had an expected response code, check to see if the
1219 * packet matched that.
1220 */
1221 if (request->packet->code != request->filter_code) {
1222 if (FR_RADIUS_PACKET_CODE_VALID(request->reply->code)) {
1223 REDEBUG("%s: Expected %s got %s", request->name, fr_radius_packet_name[request->filter_code],
1224 fr_radius_packet_name[request->packet->code]);
1225 } else {
1226 REDEBUG("%s: Expected %u got %i", request->name, request->filter_code,
1227 request->packet->code);
1228 }
1229 stats.failed++;
1230
1231 /*
1232 * Check if the contents of the packet matched the filter
1233 */
1234 } else if (fr_pair_list_empty(&request->filter)) {
1235 stats.passed++;
1236
1237 } else {
1238 fr_pair_t const *failed[2];
1239
1241 if (fr_pair_validate(failed, &request->filter, &request->request_pairs)) {
1242 RDEBUG("%s: CoA request passed filter", request->name);
1243 stats.passed++;
1244 } else {
1245 fr_pair_validate_debug(failed);
1246 REDEBUG("%s: CoA Request for failed filter", request->name);
1247 stats.failed++;
1248 }
1249 }
1250
1251 request->reply->id = request->packet->id;
1252
1253 request->reply->socket.type = SOCK_DGRAM;
1254 request->reply->socket.af = client_ipaddr.af;
1255 request->reply->socket.fd = coafd;
1256 request->reply->socket.inet.src_ipaddr = client_ipaddr;
1257 request->reply->socket.inet.src_port = coa_port;
1258 request->reply->socket.inet.dst_ipaddr = packet->socket.inet.src_ipaddr;
1259 request->reply->socket.inet.dst_port = packet->socket.inet.src_port;
1260
1261 if (!request->reply->code) switch (packet->code) {
1263 request->reply->code = FR_RADIUS_CODE_COA_ACK;
1264 break;
1265
1268 break;
1269
1270 default:
1271 RDEBUG("Failed getting reply packet type");
1272 return 0;
1273 }
1274
1275 fr_radius_packet_log(&default_log, request->reply, &request->reply_pairs, false);
1276
1277
1278 /*
1279 * Send reply.
1280 */
1281 if (fr_packet_send(request->reply, &request->reply_pairs, packet, secret) < 0) {
1282 REDEBUG("Failed sending CoA reply");
1283 return 0;
1284 }
1285
1286 fr_rb_remove(coa_tree, request);
1287
1288 /*
1289 * No longer waiting for a CoA packet for this request.
1290 */
1291 TALLOC_FREE(parent->coa);
1292 return 0;
1293}
1294
1295
1296/*
1297 * Do Blast RADIUS checks.
1298 *
1299 * The request is an Access-Request, and does NOT contain Proxy-State.
1300 *
1301 * The reply is a raw packet, and is NOT yet decoded.
1302 */
1303static int blast_radius_check(rc_request_t *request, fr_packet_t *reply)
1304{
1305 uint8_t *attr, *end;
1306 fr_pair_t *vp;
1307 bool have_message_authenticator = false;
1308
1309 /*
1310 * We've received a raw packet. Nothing has (as of yet) checked
1311 * anything in it other than the length, and that it's a
1312 * well-formed RADIUS packet.
1313 */
1314 switch (reply->data[0]) {
1319 if (reply->data[1] != request->packet->id) {
1320 ERROR("Invalid reply ID %d to Access-Request ID %d", reply->data[1], request->packet->id);
1321 return -1;
1322 }
1323 break;
1324
1325 default:
1326 ERROR("Invalid reply code %d to Access-Request", reply->data[0]);
1327 return -1;
1328 }
1329
1330 /*
1331 * If the reply has a Message-Authenticator, then it MIGHT be fine.
1332 */
1333 attr = reply->data + 20;
1334 end = reply->data + reply->data_len;
1335
1336 /*
1337 * It should be the first attribute, so we warn if it isn't there.
1338 *
1339 * But it's not a fatal error.
1340 */
1341 if (blast_radius && (attr[0] != FR_MESSAGE_AUTHENTICATOR)) {
1342 RDEBUG("WARNING The %s reply packet does not have Message-Authenticator as the first attribute. The packet may be vulnerable to Blast RADIUS attacks.",
1343 fr_radius_packet_name[reply->data[0]]);
1344 }
1345
1346 /*
1347 * Set up for Proxy-State checks.
1348 *
1349 * If we see a Proxy-State in the reply which we didn't send, then it's a Blast RADIUS attack.
1350 */
1352
1353 while (attr < end) {
1354 /*
1355 * Blast RADIUS work-arounds require that
1356 * Message-Authenticator is the first attribute in the
1357 * reply. Note that we don't check for it being the
1358 * first attribute, but simply that it exists.
1359 *
1360 * That check is a balance between securing the reply
1361 * packet from attacks, and not violating the RFCs which
1362 * say that there is no order to attributes in the
1363 * packet.
1364 *
1365 * However, no matter the status of the '-b' flag we
1366 * still can check for the signature of the attack, and
1367 * discard packets which are suspicious. This behavior
1368 * protects radclient from the attack, without mandating
1369 * new behavior on the server side.
1370 *
1371 * Note that we don't set the '-b' flag by default.
1372 * radclient is intended for testing / debugging, and is
1373 * not intended to be used as part of a secure login /
1374 * user checking system.
1375 */
1376 if (attr[0] == FR_MESSAGE_AUTHENTICATOR) {
1377 have_message_authenticator = true;
1378 goto next;
1379 }
1380
1381 /*
1382 * If there are Proxy-State attributes in the reply, they must
1383 * match EXACTLY the Proxy-State attributes in the request.
1384 *
1385 * Note that we don't care if there are more Proxy-States
1386 * in the request than in the reply. The Blast RADIUS
1387 * issue requires _adding_ Proxy-State attributes, and
1388 * cannot work when the server _deletes_ Proxy-State
1389 * attributes.
1390 */
1391 if (attr[0] == FR_PROXY_STATE) {
1392 if (!vp || (vp->vp_length != (size_t) (attr[1] - 2)) || (memcmp(vp->vp_octets, attr + 2, vp->vp_length) != 0)) {
1393 ERROR("Invalid reply to Access-Request ID %d - Discarding packet due to Blast RADIUS attack being detected.", request->packet->id);
1394 ERROR("We received a Proxy-State in the reply which we did not send, or which is different from what we sent.");
1395 return -1;
1396 }
1397
1399 }
1400
1401 next:
1402 attr += attr[1];
1403 }
1404
1405 /*
1406 * If "-b" is set, then we require Message-Authenticator in the reply.
1407 */
1408 if (blast_radius && !have_message_authenticator) {
1409 ERROR("The %s reply packet does not contain Message-Authenticator - discarding packet due to Blast RADIUS checks.",
1410 fr_radius_packet_name[reply->data[0]]);
1411 return -1;
1412 }
1413
1414 /*
1415 * The packet doesn't look like it's a Blast RADIUS attack. The
1416 * caller will now verify the packet signature.
1417 */
1418 return 0;
1419}
1420
1421/*
1422 * Receive one packet, maybe.
1423 */
1425{
1426 int rcode;
1427 fd_set set;
1428 fr_time_delta_t our_wait_time;
1429 rc_request_t *request;
1430 fr_packet_t *reply, *packet;
1431 volatile int max_fd;
1432
1433#ifdef STATIC_ANALYZER
1434 if (!secret) fr_exit_now(1);
1435#endif
1436
1437 /* And wait for reply, timing out as necessary */
1438 FD_ZERO(&set);
1439
1440 max_fd = fr_packet_list_fd_set(packet_list, &set);
1441 if (max_fd < 0) fr_exit_now(1); /* no sockets to listen on! */
1442
1443 our_wait_time = !fr_time_delta_ispos(wait_time) ? fr_time_delta_from_sec(0) : wait_time;
1444
1445 if (do_coa && fr_rb_num_elements(coa_tree) > 0) {
1446 FD_SET(coafd, &set);
1447 if (coafd >= max_fd) max_fd = coafd + 1;
1448 }
1449
1450 /*
1451 * See if a packet was received.
1452 */
1453retry:
1454 if (select(max_fd, &set, NULL, NULL, &fr_time_delta_to_timeval(our_wait_time)) <= 0) return 0;
1455
1456 /*
1457 * Read a CoA packet
1458 */
1459 if (FD_ISSET(coafd, &set)) {
1461 FD_CLR(coafd, &set);
1462 our_wait_time = fr_time_delta_from_sec(0);
1463 goto retry;
1464 }
1465
1466 /*
1467 * Look for the packet.
1468 */
1469 rcode = fr_packet_list_recv(packet_list, &set, NULL, &reply, RADIUS_MAX_ATTRIBUTES, false);
1470 if (rcode < 0) {
1471 ERROR("Received bad packet");
1472
1473 /*
1474 * If the packet is bad, we close the socket.
1475 * I'm not sure how to do that now, so we just
1476 * die...
1477 */
1478 if (ipproto == IPPROTO_TCP) fr_exit_now(1);
1479 return -1; /* bad packet */
1480 }
1481
1482 /*
1483 * We don't use udpfromto. So if we bind to "*", we want
1484 * to find replies sent to 192.0.2.4. Therefore, we
1485 * force all replies to have the one address we know
1486 * about, no matter what real address they were sent to.
1487 *
1488 * This only works if were not using any of the
1489 * Packet-* attributes, or running with 'auto'.
1490 */
1491 reply->socket.inet.dst_ipaddr = client_ipaddr;
1492 reply->socket.inet.dst_port = client_port;
1493
1494 /*
1495 * TCP sockets don't use recvmsg(), and thus don't get
1496 * the source IP/port. However, since they're TCP, we
1497 * know what the source IP/port is, because that's where
1498 * we connected to.
1499 */
1500 if (ipproto == IPPROTO_TCP) {
1501 reply->socket.inet.src_ipaddr = server_ipaddr;
1502 reply->socket.inet.src_port = server_port;
1503 }
1504
1506 if (!packet) {
1507 ERROR("Received reply to request we did not send. (id=%d socket %d)",
1508 reply->id, reply->socket.fd);
1509 fr_packet_free(&reply);
1510 return -1; /* got reply to packet we didn't send */
1511 }
1512 request = packet->uctx;
1513
1514 /*
1515 * We want radclient to be able to send any packet, including
1516 * imperfect ones. However, we do NOT want to be vulnerable to
1517 * the "Blast RADIUS" issue. Instead of adding command-line
1518 * flags to enable/disable similar flags to what the server
1519 * sends, we just do a few more smart checks to double-check
1520 * things.
1521 */
1522 if ((request->packet->code == FR_RADIUS_CODE_ACCESS_REQUEST) &&
1523 blast_radius_check(request, reply) < 0) {
1524 fr_packet_free(&reply);
1525 return -1;
1526 }
1527
1528 /*
1529 * Fails the signature validation: not a real reply.
1530 * FIXME: Silently drop it and listen for another packet.
1531 */
1532 if (fr_packet_verify(reply, request->packet, secret) < 0) {
1533 REDEBUG("Reply verification failed");
1534 stats.lost++;
1535 goto packet_done; /* shared secret is incorrect */
1536 }
1537
1538 if (print_filename) {
1539 RDEBUG("%s response code %d", request->files->packets, reply->code);
1540 }
1541
1542 if (request->tries < ignore_count) {
1543 RDEBUG("Ignoring response %d due to -e ignore_count=%d", request->tries, ignore_count);
1544 goto packet_done;
1545 }
1546
1547 deallocate_id(request);
1548 request->reply = reply;
1549 reply = NULL;
1550
1551 /*
1552 * If this fails, we're out of memory.
1553 */
1554 if (fr_radius_decode_simple(request, &request->reply_pairs,
1555 request->reply->data, request->reply->data_len,
1556 request->packet->vector, secret) < 0) {
1557 REDEBUG("Reply decode failed");
1558 stats.lost++;
1559 goto packet_done;
1560 }
1561 PAIR_LIST_VERIFY(&request->reply_pairs);
1562 fr_radius_packet_log(&default_log, request->reply, &request->reply_pairs, true);
1563
1564 /*
1565 * Increment counters...
1566 */
1567 switch (request->reply->code) {
1572 stats.accepted++;
1573 break;
1574
1576 break;
1577
1579 stats.error++;
1580 break;
1581 default:
1582 stats.rejected++;
1583 }
1584
1585 fr_strerror_clear(); /* Clear strerror buffer */
1586
1587 /*
1588 * If we had an expected response code, check to see if the
1589 * packet matched that.
1590 */
1591 if ((request->filter_code != FR_RADIUS_CODE_UNDEFINED) && (request->reply->code != request->filter_code)) {
1592 if (FR_RADIUS_PACKET_CODE_VALID(request->reply->code)) {
1593 REDEBUG("%s: Expected %s got %s", request->name, fr_radius_packet_name[request->filter_code],
1594 fr_radius_packet_name[request->reply->code]);
1595 } else {
1596 REDEBUG("%s: Expected %u got %i", request->name, request->filter_code,
1597 request->reply->code);
1598 }
1599 stats.failed++;
1600 /*
1601 * Check if the contents of the packet matched the filter
1602 */
1603 } else if (fr_pair_list_empty(&request->filter)) {
1604 stats.passed++;
1605 } else {
1606 fr_pair_t const *failed[2];
1607
1609 if (fr_pair_validate(failed, &request->filter, &request->reply_pairs)) {
1610 RDEBUG("%s: Response passed filter", request->name);
1611 stats.passed++;
1612 } else {
1613 fr_pair_validate_debug(failed);
1614 REDEBUG("%s: Response for failed filter", request->name);
1615 stats.failed++;
1616 }
1617 }
1618
1619 if (request->resend == resend_count) {
1620 request->done = true;
1621 }
1622
1623packet_done:
1624 fr_packet_free(&request->reply);
1625 fr_packet_free(&reply); /* may be NULL */
1626
1627 return 0;
1628}
1629
1630/**
1631 *
1632 * @hidecallgraph
1633 */
1634int main(int argc, char **argv)
1635{
1636 int ret = EXIT_SUCCESS;
1637 int c;
1638 char const *confdir = CONFDIR;
1639 char const *dict_dir = DICTDIR;
1640 char filesecret[256];
1641 FILE *fp;
1642 int do_summary = false;
1643 int persec = 0;
1644 int parallel = 1;
1645 int force_af = AF_UNSPEC;
1646#ifndef NDEBUG
1647 TALLOC_CTX *autofree;
1648#endif
1649 fr_dlist_head_t filenames;
1650 rc_request_t *request;
1651
1652 /*
1653 * It's easier having two sets of flags to set the
1654 * verbosity of library calls and the verbosity of
1655 * radclient.
1656 */
1657 fr_debug_lvl = 0;
1658 fr_log_fp = stdout;
1659
1660 /*
1661 * Must be called first, so the handler is called last
1662 */
1664
1665#ifndef NDEBUG
1667
1668 if (fr_fault_setup(autofree, getenv("PANIC_ACTION"), argv[0]) < 0) {
1669 fr_perror("radclient");
1670 fr_exit_now(EXIT_FAILURE);
1671 }
1672#endif
1673
1674 talloc_set_log_stderr();
1675
1676 rc_request_list_talloc_init(&rc_request_list);
1677
1678 fr_dlist_talloc_init(&filenames, rc_file_pair_t, entry);
1679
1680 /*
1681 * Always log to stdout
1682 */
1684 default_log.fd = STDOUT_FILENO;
1685 default_log.print_level = false;
1686
1687 while ((c = getopt(argc, argv, "46bc:A:C:d:D:e:f:Fhi:n:o:p:P:r:sS:t:vx")) != -1) switch (c) {
1688 case '4':
1689 force_af = AF_INET;
1690 break;
1691
1692 case '6':
1693 force_af = AF_INET6;
1694 break;
1695
1696 case 'A':
1697 attr_coa_filter_name = optarg;
1698 break;
1699
1700 case 'b':
1701 blast_radius = true;
1702 break;
1703
1704 case 'c':
1705 if (!isdigit((uint8_t) *optarg)) usage();
1706
1707 resend_count = atoi(optarg);
1708
1709 if (resend_count < 1) usage();
1710 break;
1711
1712 case 'C':
1713 {
1714 int tmp;
1715
1716 if (strchr(optarg, ':')) {
1718 optarg, -1, AF_UNSPEC, true, false) < 0) {
1719 fr_perror("Failed parsing source address");
1720 fr_exit_now(1);
1721 }
1722 break;
1723 }
1724
1725 tmp = atoi(optarg);
1726 if (tmp < 1 || tmp > 65535) usage();
1727
1728 client_port = (uint16_t)tmp;
1729 }
1730 break;
1731
1732 case 'D':
1733 dict_dir = optarg;
1734 break;
1735
1736 case 'd':
1737 confdir = optarg;
1738 break;
1739
1740 case 'e': /* magical extra stuff */
1741 if (strncmp(optarg, "ignore_count=", 13) == 0) {
1742 ignore_count = atoi(optarg + 13);
1743 break;
1744 }
1745 usage();
1746
1747 /*
1748 * packet,filter,coa_reply,coa_filter
1749 */
1750 case 'f':
1751 {
1752 char const *p;
1753 rc_file_pair_t *files;
1754
1755 MEM(files = talloc_zero(talloc_autofree_context(), rc_file_pair_t));
1756
1757 /*
1758 * Commas are nicer than colons.
1759 */
1760 c = ':';
1761
1762 p = strchr(optarg, c);
1763 if (!p) {
1764 c = ',';
1765 p = strchr(optarg, c);
1766 }
1767 if (!p) {
1768 files->packets = optarg;
1769 files->filters = NULL;
1770 } else {
1771 char *q;
1772
1773 MEM(files->packets = talloc_strndup(files, optarg, p - optarg));
1774 files->filters = p + 1;
1775
1776 /*
1777 * Look for CoA filename
1778 */
1779 q = strchr(files->filters, c);
1780 if (q) {
1781 do_coa = true;
1782
1783 *(q++) = '\0';
1784 files->coa_reply = q;
1785
1786 q = strchr(files->coa_reply, c);
1787 if (q) {
1788 *(q++) = '\0';
1789 files->coa_filter = q;
1790 }
1791 }
1792 }
1793 fr_dlist_insert_tail(&filenames, files);
1794 }
1795 break;
1796
1797 case 'F':
1798 print_filename = true;
1799 break;
1800
1801 case 'i':
1802 if (!isdigit((uint8_t) *optarg)) {
1803 usage();
1804 }
1805 last_used_id = atoi(optarg);
1806 if ((last_used_id < 0) || (last_used_id > 255)) {
1807 usage();
1808 }
1809 break;
1810
1811 case 'n':
1812 persec = atoi(optarg);
1813 if (persec <= 0) usage();
1814 break;
1815
1816 case 'o':
1817 coa_port = atoi(optarg);
1818 break;
1819
1820 /*
1821 * Note that sending MANY requests in
1822 * parallel can over-run the kernel
1823 * queues, and Linux will happily discard
1824 * packets. So even if the server responds,
1825 * the client may not see the reply.
1826 */
1827 case 'p':
1828 parallel = atoi(optarg);
1829 if (parallel <= 0) usage();
1830 break;
1831
1832 case 'P':
1833 if (!strcmp(optarg, "tcp")) {
1834 ipproto = IPPROTO_TCP;
1835 } else if (!strcmp(optarg, "udp")) {
1836 ipproto = IPPROTO_UDP;
1837 } else {
1838 usage();
1839 }
1840 break;
1841
1842 case 'r':
1843 if (!isdigit((uint8_t) *optarg)) usage();
1844 retries = atoi(optarg);
1845 if ((retries == 0) || (retries > 1000)) usage();
1846 break;
1847
1848 case 's':
1849 do_summary = true;
1850 break;
1851
1852 case 'S':
1853 {
1854 char *p;
1855 fp = fopen(optarg, "r");
1856 if (!fp) {
1857 ERROR("Error opening %s: %s", optarg, fr_syserror(errno));
1858 fr_exit_now(1);
1859 }
1860 if (fgets(filesecret, sizeof(filesecret), fp) == NULL) {
1861 ERROR("Error reading %s: %s", optarg, fr_syserror(errno));
1862 fr_exit_now(1);
1863 }
1864 fclose(fp);
1865
1866 /* truncate newline */
1867 p = filesecret + strlen(filesecret) - 1;
1868 while ((p >= filesecret) &&
1869 (*p < ' ')) {
1870 *p = '\0';
1871 --p;
1872 }
1873
1874 if (strlen(filesecret) < 2) {
1875 ERROR("Secret in %s is too short", optarg);
1876 fr_exit_now(1);
1877 }
1878 secret = talloc_strdup(NULL, filesecret);
1879 }
1880 break;
1881
1882 case 't':
1883 if (fr_time_delta_from_str(&timeout, optarg, strlen(optarg), FR_TIME_RES_SEC) < 0) {
1884 fr_perror("Failed parsing timeout value");
1885 fr_exit_now(EXIT_FAILURE);
1886 }
1887 break;
1888
1889 case 'v':
1890 fr_debug_lvl = 1;
1891 DEBUG("%s", radclient_version);
1892 fr_exit_now(0);
1893
1894 case 'x':
1895 fr_debug_lvl++;
1896 if (fr_debug_lvl > 1) default_log.print_level = true;
1897 break;
1898
1899 case 'h':
1900 default:
1901 usage();
1902 }
1903 argc -= (optind - 1);
1904 argv += (optind - 1);
1905
1906 if ((argc < 3) || ((secret == NULL) && (argc < 4))) {
1907 ERROR("Insufficient arguments");
1908 usage();
1909 }
1910 /*
1911 * Mismatch between the binary and the libraries it depends on
1912 */
1914 fr_perror("radclient");
1915 fr_exit_now(EXIT_FAILURE);
1916 }
1917
1918 if (!fr_dict_global_ctx_init(NULL, true, dict_dir)) {
1919 fr_perror("radclient");
1920 fr_exit_now(EXIT_FAILURE);
1921 }
1922
1923 if (fr_radius_global_init() < 0) {
1924 fr_perror("radclient");
1925 fr_exit_now(EXIT_FAILURE);
1926 }
1927
1928 if (fr_dict_autoload(radclient_dict) < 0) {
1929 fr_perror("radclient");
1930 exit(EXIT_FAILURE);
1931 }
1932
1934 fr_perror("radclient");
1935 exit(EXIT_FAILURE);
1936 }
1937
1939 fr_log_perror(&default_log, L_ERR, __FILE__, __LINE__, NULL,
1940 "Failed to initialize the dictionaries");
1941 exit(EXIT_FAILURE);
1942 }
1943
1944 if (do_coa) {
1946 if (!attr_coa_match_attr) {
1947 ERROR("Unknown or invalid CoA filter attribute %s", optarg);
1948 fr_exit_now(1);
1949 }
1950
1951 /*
1952 * If there's no attribute given to match CoA to requests, use User-Name
1953 */
1955
1957 }
1959
1960 fr_strerror_clear(); /* Clear the error buffer */
1961
1962 /*
1963 * Get the request type
1964 */
1965 if (!isdigit((uint8_t) argv[2][0])) {
1967 if (packet_code == -2) {
1968 ERROR("Unrecognised request type \"%s\"", argv[2]);
1969 usage();
1970 }
1971 } else {
1972 packet_code = atoi(argv[2]);
1973 }
1974
1975 /*
1976 * Resolve hostname.
1977 */
1978 if (strcmp(argv[1], "-") != 0) {
1979 if (fr_inet_pton_port(&server_ipaddr, &server_port, argv[1], -1, force_af, true, true) < 0) {
1980 fr_perror("radclient");
1981 fr_exit_now(1);
1982 }
1983
1984 /*
1985 * Work backwards from the port to determine the packet type
1986 */
1988 }
1990
1991 /*
1992 * Add the secret.
1993 */
1994 if (argv[3]) secret = talloc_strdup(NULL, argv[3]);
1995
1996 /*
1997 * If no '-f' is specified, we're reading from stdin.
1998 */
1999 if (fr_dlist_num_elements(&filenames) == 0) {
2000 rc_file_pair_t *files;
2001
2002 files = talloc_zero(talloc_autofree_context(), rc_file_pair_t);
2003 files->packets = "-";
2004 if (radclient_init(files, files) < 0) fr_exit_now(1);
2005 }
2006
2007 /*
2008 * Walk over the list of filenames, creating the requests.
2009 */
2010 fr_dlist_foreach(&filenames, rc_file_pair_t, files) {
2011 if (radclient_init(files, files)) {
2012 ERROR("Failed parsing input files");
2013 fr_exit_now(1);
2014 }
2015 }
2016
2017 /*
2018 * No packets read. Die.
2019 */
2020 if (!rc_request_list_num_elements(&rc_request_list)) {
2021 ERROR("Nothing to send");
2022 fr_exit_now(1);
2023 }
2024
2025 openssl3_init();
2026
2027 /*
2028 * Bind to the first specified IP address and port.
2029 * This means we ignore later ones.
2030 */
2031 request = rc_request_list_head(&rc_request_list);
2032
2033 if (client_ipaddr.af == AF_UNSPEC) {
2034 if (request->packet->socket.inet.src_ipaddr.af == AF_UNSPEC) {
2035 memset(&client_ipaddr, 0, sizeof(client_ipaddr));
2037 } else {
2038 client_ipaddr = request->packet->socket.inet.src_ipaddr;
2039 }
2040 }
2041
2042 if (client_port == 0) client_port = request->packet->socket.inet.src_port;
2043
2044 if (ipproto == IPPROTO_TCP) {
2045 sockfd = fr_socket_client_tcp(NULL, NULL, &server_ipaddr, server_port, false);
2046 if (sockfd < 0) {
2047 ERROR("Failed opening socket");
2048 return -1;
2049 }
2050
2051 } else {
2053 if (sockfd < 0) {
2054 fr_perror("Error opening socket");
2055 return -1;
2056 }
2057
2058 if (fr_socket_bind(sockfd, NULL, &client_ipaddr, &client_port) < 0) {
2059 fr_perror("Error binding socket");
2060 return -1;
2061 }
2062 }
2063
2064 if (do_coa) {
2066 if (coafd < 0) {
2067 fr_perror("Error opening CoA socket");
2068 return -1;
2069 }
2070
2071 if (fr_socket_bind(coafd, NULL, &client_ipaddr, &coa_port) < 0) {
2072 fr_perror("Error binding socket");
2073 return -1;
2074 }
2075 }
2076
2079 server_port, NULL)) {
2080 ERROR("Failed adding socket");
2081 fr_exit_now(1);
2082 }
2083
2084 /*
2085 * Walk over the list of packets, sanity checking
2086 * everything.
2087 */
2088 rc_request_list_foreach(&rc_request_list, this) {
2089 this->packet->socket.inet.src_ipaddr = client_ipaddr;
2090 this->packet->socket.inet.src_port = client_port;
2091 if (radclient_sane(this) != 0) {
2092 fr_exit_now(1);
2093 }
2094 }
2095
2096 /*
2097 * Walk over the packets to send, until
2098 * we're all done.
2099 *
2100 * FIXME: This currently busy-loops until it receives
2101 * all of the packets. It should really have some sort of
2102 * send packet, get time to wait, select for time, etc.
2103 * loop.
2104 */
2105 do {
2106 int n = parallel;
2107 char const *filename = NULL;
2108
2109 done = true;
2111
2112 /*
2113 * Walk over the packets, sending them.
2114 *
2115 * This essentially ends up handling
2116 * retries as well, as requests to
2117 * retry stay in the request list, and
2118 * then get sent again on the next
2119 * iteration of the outer loop.
2120 */
2121 rc_request_list_foreach(&rc_request_list, this) {
2122 /*
2123 * If there's a packet to receive,
2124 * receive it, but don't wait for a
2125 * packet.
2126 */
2128
2129 /*
2130 * This packet is done. Delete it.
2131 */
2132 if (this->done) {
2133 /*
2134 * We still have a CoA reply to
2135 * receive for this packet.
2136 */
2137 if (this->coa) {
2139 if (this->coa) continue;
2140 }
2141
2142 talloc_free(this);
2143 continue;
2144 }
2145
2146 /*
2147 * Packets from multiple '-f' are sent
2148 * in parallel.
2149 *
2150 * Packets from one file are sent in
2151 * series, unless '-p' is specified, in
2152 * which case N packets from each file
2153 * are sent in parallel.
2154 */
2155 if (this->files->packets != filename) {
2156 filename = this->files->packets;
2157 n = parallel;
2158 }
2159
2160 if (n > 0) {
2161 n--;
2162
2163 /*
2164 * Send the current packet.
2165 */
2166 if (send_one_packet(this) < 0) {
2167 talloc_free(this);
2168 break;
2169 }
2170
2171 /*
2172 * Wait a little before sending
2173 * the next packet, if told to.
2174 */
2175 if (persec) {
2176 fr_time_delta_t psec;
2177
2178 psec = (persec == 1) ? fr_time_delta_from_sec(1) : fr_time_delta_wrap(1000000 / persec);
2179
2180 /*
2181 * Don't sleep elsewhere.
2182 */
2184
2185
2186 /*
2187 * Sleep for milliseconds,
2188 * portably.
2189 *
2190 * If we get an error or
2191 * a signal, treat it like
2192 * a normal timeout.
2193 */
2194 select(0, NULL, NULL, NULL, &fr_time_delta_to_timeval(psec));
2195 }
2196
2197 /*
2198 * If we haven't sent this packet
2199 * often enough, we're not done,
2200 * and we shouldn't sleep.
2201 */
2202 if (this->resend < resend_count) {
2203 int i;
2204
2205 done = false;
2207
2208 for (i = 0; i < 4; i++) {
2209 ((uint32_t *) this->packet->vector)[i] = fr_rand();
2210 }
2211 }
2212 } else { /* haven't sent this packet, we're not done */
2213 assert(this->done == false);
2214 assert(this->reply == NULL);
2215 done = false;
2216 }
2217 }
2218
2219 /*
2220 * Still have outstanding requests.
2221 */
2223 done = false;
2224 } else {
2226 }
2227
2228 /*
2229 * Nothing to do until we receive a request, so
2230 * sleep until then. Once we receive one packet,
2231 * we go back, and walk through the whole list again,
2232 * sending more packets (if necessary), and updating
2233 * the sleep time.
2234 */
2237 }
2238 } while (!done);
2239
2241
2242 rc_request_list_talloc_free(&rc_request_list);
2243
2245
2247
2249
2250 if (fr_dict_autofree(radclient_dict) < 0) {
2251 fr_perror("radclient");
2252 ret = EXIT_FAILURE;
2253 }
2254
2255#ifndef NDEBUG
2257#endif
2258
2259 if (do_summary) {
2260 fr_perror("Packet summary:\n"
2261 "\tAccepted : %" PRIu64 "\n"
2262 "\tRejected : %" PRIu64 "\n"
2263 "\tLost : %" PRIu64 "\n"
2264 "\tErrored : %" PRIu64 "\n"
2265 "\tPassed filter : %" PRIu64 "\n"
2266 "\tFailed filter : %" PRIu64,
2269 stats.lost,
2270 stats.error,
2271 stats.passed,
2273 );
2274 }
2275
2276 /*
2277 * Ensure our atexit handlers run before any other
2278 * atexit handlers registered by third party libraries.
2279 */
2281
2282 if ((stats.lost > 0) || (stats.failed > 0)) return EXIT_FAILURE;
2283
2284 openssl3_free();
2285
2286 return ret;
2287}
static int const char char buffer[256]
Definition acutest.h:578
int n
Definition acutest.h:579
int fr_atexit_global_setup(void)
Setup the atexit handler, should be called at the start of a program's execution.
Definition atexit.c:160
int fr_atexit_global_trigger_all(void)
Cause all global free triggers to fire.
Definition atexit.c:286
static TALLOC_CTX * autofree
Definition fuzzer.c:46
#define RCSID(id)
Definition build.h:487
#define NEVER_RETURNS
Should be placed before the function return type.
Definition build.h:315
void fr_chap_encode(uint8_t out[static 1+FR_CHAP_CHALLENGE_LENGTH], uint8_t id, uint8_t const *challenge, size_t challenge_len, char const *password, size_t password_len)
Encode a CHAP password.
Definition chap.c:34
int fr_fault_setup(TALLOC_CTX *ctx, char const *cmd, char const *program)
Registers signal handlers to execute panic_action on fatal signal.
Definition debug.c:1056
#define MEM(x)
Definition debug.h:36
#define fr_exit_now(_x)
Exit without calling atexit() handlers, producing a log message in debug builds.
Definition debug.h:226
fr_radius_packet_code_t
RADIUS packet codes.
Definition defs.h:31
@ FR_RADIUS_CODE_ACCESS_CHALLENGE
RFC2865 - Access-Challenge.
Definition defs.h:43
@ FR_RADIUS_CODE_ACCESS_REQUEST
RFC2865 - Access-Request.
Definition defs.h:33
@ FR_RADIUS_CODE_DISCONNECT_REQUEST
RFC3575/RFC5176 - Disconnect-Request.
Definition defs.h:46
@ FR_RADIUS_CODE_DISCONNECT_ACK
RFC3575/RFC5176 - Disconnect-Ack (positive)
Definition defs.h:47
@ FR_RADIUS_CODE_STATUS_SERVER
RFC2865/RFC5997 - Status Server (request)
Definition defs.h:44
@ FR_RADIUS_CODE_COA_REQUEST
RFC3575/RFC5176 - CoA-Request.
Definition defs.h:49
@ FR_RADIUS_CODE_ACCESS_ACCEPT
RFC2865 - Access-Accept.
Definition defs.h:34
@ FR_RADIUS_CODE_ACCOUNTING_RESPONSE
RFC2866 - Accounting-Response.
Definition defs.h:37
@ FR_RADIUS_CODE_COA_NAK
RFC3575/RFC5176 - CoA-Nak (not willing to perform)
Definition defs.h:51
@ FR_RADIUS_CODE_UNDEFINED
Packet code has not been set.
Definition defs.h:32
@ FR_RADIUS_CODE_COA_ACK
RFC3575/RFC5176 - CoA-Ack (positive)
Definition defs.h:50
@ FR_RADIUS_CODE_DISCONNECT_NAK
RFC3575/RFC5176 - Disconnect-Nak (not willing to perform)
Definition defs.h:48
@ FR_RADIUS_CODE_PROTOCOL_ERROR
RFC7930 - Protocol-Error (generic NAK)
Definition defs.h:52
@ FR_RADIUS_CODE_ACCOUNTING_REQUEST
RFC2866 - Accounting-Request.
Definition defs.h:36
@ FR_RADIUS_CODE_ACCESS_REJECT
RFC2865 - Access-Reject.
Definition defs.h:35
#define FR_COA_UDP_PORT
Definition defs.h:63
#define FR_AUTH_UDP_PORT
Definition defs.h:57
#define FR_ACCT_UDP_PORT_ALT
Definition defs.h:60
#define FR_AUTH_UDP_PORT_ALT
Definition defs.h:58
#define FR_POD_UDP_PORT
Definition defs.h:61
#define FR_ACCT_UDP_PORT
Definition defs.h:59
#define ERROR(fmt,...)
Definition dhcpclient.c:41
static fr_dict_t const * dict_freeradius
Definition dhcpclient.c:79
#define DEBUG(fmt,...)
Definition dhcpclient.c:39
fr_dict_t * fr_dict_unconst(fr_dict_t const *dict)
Coerce to non-const.
Definition dict_util.c:4904
#define fr_dict_autofree(_to_free)
Definition dict.h:917
fr_dict_attr_t const * fr_dict_attr_by_name(fr_dict_attr_err_t *err, fr_dict_attr_t const *parent, char const *attr))
Locate a fr_dict_attr_t by its name.
Definition dict_util.c:3529
fr_dict_attr_t const * fr_dict_root(fr_dict_t const *dict)
Return the root attribute of a dictionary.
Definition dict_util.c:2666
fr_dict_attr_t const ** out
Where to write a pointer to the resolved fr_dict_attr_t.
Definition dict.h:294
fr_dict_t const ** out
Where to write a pointer to the loaded/resolved fr_dict_t.
Definition dict.h:307
int fr_dict_read(fr_dict_t *dict, char const *dict_dir, char const *filename))
Read supplementary attribute definitions into an existing dictionary.
int fr_dict_attr_autoload(fr_dict_attr_autoload_t const *to_load)
Process a dict_attr_autoload element to load/verify a dictionary attribute.
Definition dict_util.c:4396
#define fr_dict_autoload(_to_load)
Definition dict.h:914
#define DICT_AUTOLOAD_TERMINATOR
Definition dict.h:313
fr_dict_gctx_t * fr_dict_global_ctx_init(TALLOC_CTX *ctx, bool free_at_exit, char const *dict_dir))
Initialise the global protocol hashes.
Definition dict_util.c:4714
Specifies an attribute which must be present for the module to function.
Definition dict.h:293
Specifies a dictionary which must be loaded/loadable for the module to function.
Definition dict.h:306
#define fr_dlist_foreach(_list_head, _type, _iter)
Iterate over the contents of a list.
Definition dlist.h:98
static unsigned int fr_dlist_num_elements(fr_dlist_head_t const *head)
Return the number of elements in the dlist.
Definition dlist.h:921
static int fr_dlist_insert_tail(fr_dlist_head_t *list_head, void *ptr)
Insert an item into the tail of a list.
Definition dlist.h:360
#define FR_DLIST_HEAD(_name)
Expands to the type name used for the head wrapper structure.
Definition dlist.h:1104
#define fr_dlist_talloc_init(_head, _type, _field)
Initialise the head structure of a doubly linked list.
Definition dlist.h:257
Head of a doubly linked list.
Definition dlist.h:51
void fr_bio_shutdown & my
Definition fd_errno.h:70
talloc_free(hp)
int fr_inet_pton_port(fr_ipaddr_t *out, uint16_t *port_out, char const *value, ssize_t inlen, int af, bool resolve, bool mask)
Parses IPv4/6 address + port, to fr_ipaddr_t and integer (port)
Definition inet.c:945
int af
Address family.
Definition inet.h:64
IPv4/6 prefix.
int packet_global_init(void)
Initialises the Net.
Definition packet.c:200
void fr_packet_net_from_pairs(fr_packet_t *packet, fr_pair_list_t const *list)
Convert pairs to information in a packet.
Definition packet.c:154
int fr_debug_lvl
Definition log.c:40
FILE * fr_log_fp
Definition log.c:39
fr_log_t default_log
Definition log.c:288
void fr_log_perror(fr_log_t const *log, fr_log_type_t type, char const *file, int line, fr_log_perror_format_t const *rules, char const *fmt,...)
Drain any outstanding messages from the fr_strerror buffers.
Definition log.c:703
@ L_DST_STDOUT
Log to stdout.
Definition log.h:78
@ L_ERR
Error message.
Definition log.h:56
fr_packet_t * fr_packet_alloc(TALLOC_CTX *ctx, bool new_vector)
Allocate a new fr_packet_t.
Definition packet.c:38
void fr_packet_free(fr_packet_t **packet_p)
Free a fr_packet_t.
Definition packet.c:89
fr_packet_t * fr_packet_list_find_byreply(fr_packet_list_t *pl, fr_packet_t *reply)
Definition list.c:338
void fr_packet_list_free(fr_packet_list_t *pl)
Definition list.c:279
int fr_packet_list_recv(fr_packet_list_t *pl, fd_set *set, TALLOC_CTX *ctx, fr_packet_t **packet_p, uint32_t max_attributes, bool require_message_authenticator)
Definition list.c:687
fr_packet_list_t * fr_packet_list_create(int alloc_id)
Definition list.c:291
bool fr_packet_list_id_alloc(fr_packet_list_t *pl, int proto, fr_packet_t *request, void **pctx)
Definition list.c:410
bool fr_packet_list_yank(fr_packet_list_t *pl, fr_packet_t *request)
Definition list.c:376
bool fr_packet_list_socket_add(fr_packet_list_t *pl, int sockfd, int proto, fr_ipaddr_t *dst_ipaddr, uint16_t dst_port, void *ctx)
Definition list.c:193
uint32_t fr_packet_list_num_elements(fr_packet_list_t *pl)
Definition list.c:383
int fr_packet_list_fd_set(fr_packet_list_t *pl, fd_set *set)
Definition list.c:660
bool fr_packet_list_id_free(fr_packet_list_t *pl, fr_packet_t *request, bool yank)
Definition list.c:636
unsigned short uint16_t
@ FR_TYPE_STRING
String of printable characters.
@ FR_TYPE_UINT32
32 Bit unsigned integer.
@ FR_TYPE_OCTETS
Raw octets.
unsigned int uint32_t
unsigned char uint8_t
int mschap_nt_password_hash(uint8_t out[static NT_DIGEST_LENGTH], char const *password)
Converts Unicode password to 16-byte NT hash with MD4.
Definition mschap.c:52
#define RADIUS_AUTH_VECTOR_LENGTH
Definition net.h:89
int fr_pair_value_memdup(fr_pair_t *vp, uint8_t const *src, size_t len, bool tainted)
Copy data into an "octets" data type.
Definition pair.c:2947
void fr_pair_validate_debug(fr_pair_t const *failed[2])
Write an error to the library errorbuff detailing the mismatch.
Definition pair.c:2099
fr_pair_t * fr_pair_find_by_da_nested(fr_pair_list_t const *list, fr_pair_t const *prev, fr_dict_attr_t const *da)
Find a pair with a matching fr_dict_attr_t, by walking the nested fr_dict_attr_t tree.
Definition pair.c:783
fr_pair_t * fr_pair_find_by_da(fr_pair_list_t const *list, fr_pair_t const *prev, fr_dict_attr_t const *da)
Find the first pair with a matching da.
Definition pair.c:706
int fr_pair_append(fr_pair_list_t *list, fr_pair_t *to_add)
Add a VP to the end of the list.
Definition pair.c:1351
int fr_pair_delete_by_da(fr_pair_list_t *list, fr_dict_attr_t const *da)
Delete matching pairs from the specified list.
Definition pair.c:1695
fr_pair_t * fr_pair_afrom_da(TALLOC_CTX *ctx, fr_dict_attr_t const *da)
Dynamically allocate a new attribute and assign a fr_dict_attr_t.
Definition pair.c:290
bool fr_pair_validate(fr_pair_t const *failed[2], fr_pair_list_t *filter, fr_pair_list_t *list)
Uses fr_pair_cmp to verify all fr_pair_ts in list match the filter defined by check.
Definition pair.c:2134
void fr_pair_list_init(fr_pair_list_t *list)
Initialise a pair list header.
Definition pair.c:46
int fr_pair_value_bstrndup(fr_pair_t *vp, char const *src, size_t len, bool tainted)
Copy data into a "string" type value pair.
Definition pair.c:2797
int fr_pair_delete(fr_pair_list_t *list, fr_pair_t *vp)
Remove fr_pair_t from a list and free.
Definition pair.c:1832
void fr_pair_list_steal(TALLOC_CTX *ctx, fr_pair_list_t *list)
Steal a list of pairs to a new context.
Definition pair.c:2310
int8_t fr_pair_cmp_by_da(void const *a, void const *b)
Order attributes by their da, and tag.
Definition pair.c:1850
int fr_pair_value_memdup_buffer_shallow(fr_pair_t *vp, uint8_t const *src, bool tainted)
Assign a talloced buffer to a "octets" type value pair.
Definition pair.c:3022
int fr_pair_list_afrom_file(TALLOC_CTX *ctx, fr_dict_t const *dict, fr_pair_list_t *out, FILE *fp, bool *pfiledone, bool allow_exec)
Read valuepairs from the fp up to End-Of-File.
int fr_radius_global_init(void)
Definition base.c:1238
ssize_t fr_radius_decode_simple(TALLOC_CTX *ctx, fr_pair_list_t *out, uint8_t *packet, size_t packet_len, uint8_t const *vector, char const *secret)
Simple wrapper for callers who just need a shared secret.
Definition base.c:1216
void fr_radius_global_free(void)
Definition base.c:1262
fr_table_num_sorted_t const fr_radius_request_name_table[]
Definition base.c:105
char const * fr_radius_packet_name[FR_RADIUS_CODE_MAX]
Definition base.c:116
int fr_packet_verify(fr_packet_t *packet, fr_packet_t *original, char const *secret)
Verify the Request/Response Authenticator (and Message-Authenticator if present) of a packet.
Definition packet.c:129
fr_packet_t * fr_packet_recv(TALLOC_CTX *ctx, int fd, int flags, uint32_t max_attributes, bool require_message_authenticator)
Receive UDP client requests, and fill in the basics of a fr_packet_t structure.
Definition packet.c:191
int fr_packet_send(fr_packet_t *packet, fr_pair_list_t *list, fr_packet_t const *original, char const *secret)
Reply to the request.
Definition packet.c:277
void fr_radius_packet_log(fr_log_t const *log, fr_packet_t *packet, fr_pair_list_t *list, bool received)
Definition packet.c:478
#define fr_assert(_expr)
Definition rad_assert.h:38
#define REDEBUG(fmt,...)
#define RDEBUG(fmt,...)
#define rc_request_list_foreach(_list_head, _iter)
#define WARN(fmt,...)
static int blast_radius_check(rc_request_t *request, fr_packet_t *reply)
Definition radclient.c:1303
static fr_rb_tree_t * coa_tree
Definition radclient.c:98
static fr_ipaddr_t client_ipaddr
Definition radclient.c:87
static fr_dict_attr_t const * attr_packet_type
Definition radclient.c:127
static fr_dict_attr_t const * attr_request_authenticator
Definition radclient.c:123
static bool blast_radius
Definition radclient.c:85
static fr_dict_attr_t const * attr_user_password
Definition radclient.c:129
static int send_one_packet(rc_request_t *request)
Definition radclient.c:954
static int getport(char const *name)
Definition radclient.c:295
static bool done
Definition radclient.c:83
static int _rc_request_free(rc_request_t *request)
Definition radclient.c:196
static uint16_t client_port
Definition radclient.c:88
static int radclient_sane(rc_request_t *request)
Definition radclient.c:885
static int recv_one_packet(fr_time_delta_t wait_time)
Definition radclient.c:1424
static fr_dict_attr_t const * attr_coa_match_attr
Definition radclient.c:135
static fr_time_delta_t sleep_time
Definition radclient.c:70
static uint16_t coa_port
Definition radclient.c:97
int main(int argc, char **argv)
Definition radclient.c:1634
static fr_dict_attr_t const * attr_chap_password
Definition radclient.c:125
static int ignore_count
Definition radclient.c:82
static fr_packet_list_t * packet_list
Definition radclient.c:100
static int resend_count
Definition radclient.c:81
static rc_stats_t stats
Definition radclient.c:76
static fr_dict_attr_t const * attr_ms_chap_response
Definition radclient.c:120
static fr_dict_attr_t const * attr_ms_chap_challenge
Definition radclient.c:118
static int last_used_id
Definition radclient.c:91
static uint16_t server_port
Definition radclient.c:78
static int packet_code
Definition radclient.c:79
fr_dict_attr_autoload_t radclient_dict_attr[]
Definition radclient.c:138
static fr_dict_attr_t const * attr_chap_challenge
Definition radclient.c:126
static int recv_coa_packet(fr_time_delta_t wait_time)
Definition radclient.c:1143
static bool do_output
Definition radclient.c:72
static fr_ipaddr_t server_ipaddr
Definition radclient.c:80
#define openssl3_free()
Definition radclient.c:256
static int ipproto
Definition radclient.c:93
static int retries
Definition radclient.c:68
static fr_dict_attr_t const * attr_proxy_state
Definition radclient.c:130
static fr_dict_attr_t const * attr_cleartext_password
Definition radclient.c:116
static void deallocate_id(rc_request_t *request)
Definition radclient.c:930
static bool print_filename
Definition radclient.c:84
static bool do_coa
Definition radclient.c:95
#define openssl3_init()
Definition radclient.c:255
static fr_dict_attr_t const * attr_radclient_test_name
Definition radclient.c:122
static int radclient_init(TALLOC_CTX *ctx, rc_file_pair_t *files)
Definition radclient.c:481
static fr_dict_attr_t const * attr_radclient_coa_filename
Definition radclient.c:132
static int mschapv1_encode(fr_packet_t *packet, fr_pair_list_t *list, char const *password)
Definition radclient.c:259
static bool already_hex(fr_pair_t *vp)
Definition radclient.c:362
static fr_dict_attr_t const * attr_user_name
Definition radclient.c:128
static char * secret
Definition radclient.c:71
static int coafd
Definition radclient.c:96
static int sockfd
Definition radclient.c:90
static void radclient_get_port(fr_radius_packet_code_t type, uint16_t *port)
Definition radclient.c:308
static fr_dict_attr_t const * attr_ms_chap_password
Definition radclient.c:119
#define pair_update_request(_attr, _da)
Definition radclient.c:59
static int8_t request_cmp(void const *one, void const *two)
Definition radclient.c:912
static const char * attr_coa_filter_name
Definition radclient.c:74
static fr_time_delta_t timeout
Definition radclient.c:69
static fr_dict_attr_t const * attr_radclient_coa_filter
Definition radclient.c:133
static NEVER_RETURNS void usage(void)
Definition radclient.c:160
static fr_radius_packet_code_t radclient_get_code(uint16_t port)
Definition radclient.c:341
static int coa_init(rc_request_t *parent, FILE *coa_reply, char const *reply_filename, bool *coa_reply_done, FILE *coa_filter, char const *filter_filename, bool *coa_filter_done)
Read one CoA reply and potentially a filter.
Definition radclient.c:388
Structures for the radclient utility.
uint64_t accepted
Requests to which we received a accept.
uint64_t rejected
Requests to which we received a reject.
uint64_t lost
Requests to which we received no response.
uint64_t failed
Requests which failed a filter.
char const * coa_filter
file containing the CoA filter we want to match
uint64_t passed
Requests which passed a filter.
char const * filters
The file containing the definition of the packet we want to match.
char const * coa_reply
file containing the CoA filter we want to match
char const * packets
The file containing the request packet.
uint64_t error
Requests which received a Protocol-Error response.
#define RADIUS_MAX_ATTRIBUTES
Definition radius.h:40
#define FR_RADIUS_PACKET_CODE_VALID(_x)
Definition radius.h:52
static fr_dict_t const * dict_radius
Definition radsniff.c:94
uint32_t fr_rand(void)
Return a 32-bit random number.
Definition rand.c:105
uint32_t fr_rb_num_elements(fr_rb_tree_t *tree)
Return how many nodes there are in a tree.
Definition rb.c:781
void * fr_rb_remove(fr_rb_tree_t *tree, void const *data)
Remove an entry from the tree, without freeing the data.
Definition rb.c:695
bool fr_rb_delete_by_inline_node(fr_rb_tree_t *tree, fr_rb_node_t *node)
Remove node and free data (if a free function was specified)
Definition rb.c:767
void * fr_rb_find(fr_rb_tree_t const *tree, void const *data)
Find an element in the tree, returning the data, not the node.
Definition rb.c:577
bool fr_rb_insert(fr_rb_tree_t *tree, void const *data)
Insert data into a tree.
Definition rb.c:626
#define fr_rb_inline_talloc_alloc(_ctx, _type, _field, _data_cmp, _data_free)
Allocs a red black that verifies elements are of a specific talloc type.
Definition rb.h:246
The main red black tree structure.
Definition rb.h:73
fr_packet_t * reply
Outgoing response.
Definition request.h:252
char const *fr_packet_t * packet
< Module the request is currently being processed by.
Definition request.h:251
static char const * name
void smbdes_mschap(uint8_t const win_password[16], uint8_t const *challenge, uint8_t *response)
Definition smbdes.c:339
int fr_socket_server_udp(fr_ipaddr_t const *src_ipaddr, uint16_t *src_port, char const *port_name, bool async)
Open an IPv4/IPv6 unconnected UDP socket.
Definition socket.c:846
int fr_socket_client_tcp(char const *ifname, fr_ipaddr_t *src_ipaddr, fr_ipaddr_t const *dst_ipaddr, uint16_t dst_port, bool async)
Establish a connected TCP socket.
Definition socket.c:708
int fr_socket_bind(int sockfd, char const *ifname, fr_ipaddr_t *src_ipaddr, uint16_t *src_port)
Bind a UDP/TCP v4/v6 socket to a given ipaddr src port, and interface.
Definition socket.c:200
fr_aka_sim_id_type_t type
fr_pair_t * vp
#define fr_time()
Allow us to arbitrarily manipulate time.
Definition state_test.c:8
fr_log_dst_t dst
Log destination.
Definition log.h:97
int fd
File descriptor to write messages to.
Definition log.h:112
bool print_level
sometimes we don't want log levels printed
Definition log.h:106
uint64_t num
The number (within the file) of the request were reading.
fr_packet_t * reply
The incoming response.
fr_pair_t * password
Password.Cleartext.
char const * name
Test name (as specified in the request).
rc_file_pair_t * files
Request and response file names.
rc_request_t * coa
CoA filter and reply.
fr_packet_t * packet
The outgoing request.
fr_pair_list_t request_pairs
fr_rb_node_t node
rbtree node data for CoA
bool done
Whether the request is complete.
fr_pair_list_t filter
If the reply passes the filter, then the request passes.
fr_pair_list_t reply_pairs
fr_radius_packet_code_t filter_code
Expected code of the response packet.
fr_time_t timestamp
Stores an attribute, a value and various bits of other data.
Definition pair.h:68
fr_dict_attr_t const *_CONST da
Dictionary attribute defines the attribute number, vendor and type of the pair.
Definition pair.h:69
char const * fr_syserror(int num)
Guaranteed to be thread-safe version of strerror.
Definition syserror.c:243
#define fr_table_value_by_str(_table, _name, _def)
Convert a string to a value using a sorted or ordered table.
Definition table.h:653
#define talloc_strndup(_ctx, _str, _len)
Definition talloc.h:146
#define talloc_autofree_context
The original function is deprecated, so replace it with our version.
Definition talloc.h:51
#define talloc_strdup(_ctx, _str)
Definition talloc.h:145
fr_slen_t fr_time_delta_from_str(fr_time_delta_t *out, char const *in, size_t inlen, fr_time_res_t hint)
Create fr_time_delta_t from a string.
Definition time.c:412
#define fr_time_delta_lt(_a, _b)
Definition time.h:285
static fr_time_delta_t fr_time_delta_from_sec(int64_t sec)
Definition time.h:590
#define fr_time_delta_wrap(_time)
Definition time.h:152
#define fr_time_delta_ispos(_a)
Definition time.h:290
#define fr_time_delta_to_timeval(_delta)
Convert a delta to a timeval.
Definition time.h:656
#define fr_time_delta_eq(_a, _b)
Definition time.h:287
@ FR_TIME_RES_SEC
Definition time.h:50
#define NSEC
Definition time.h:379
#define fr_time_sub(_a, _b)
Subtract one time from another.
Definition time.h:229
#define fr_time_delta_gt(_a, _b)
Definition time.h:283
A time delta, a difference in time measured in nanoseconds.
Definition time.h:80
"server local" time.
Definition time.h:69
#define FR_DICTIONARY_FILE
Definition conf.h:7
void * uctx
Definition packet.h:79
unsigned int code
Packet code (type).
Definition packet.h:61
fr_socket_t socket
This packet was received on.
Definition packet.h:57
int id
Packet ID (used to link requests/responses).
Definition packet.h:60
uint8_t * data
Packet data (body).
Definition packet.h:63
size_t data_len
Length of packet data.
Definition packet.h:64
uint8_t vector[RADIUS_AUTH_VECTOR_LENGTH]
RADIUS authentication vector.
Definition packet.h:69
bool fr_pair_list_empty(fr_pair_list_t const *list)
Is a valuepair list empty.
void fr_pair_list_sort(fr_pair_list_t *list, fr_cmp_t cmp)
Sort a doubly linked list of fr_pair_ts using merge sort.
fr_pair_t * fr_pair_list_next(fr_pair_list_t const *list, fr_pair_t const *item))
Get the next item in a valuepair list after a specific entry.
Definition pair_inline.c:69
void fr_pair_list_append(fr_pair_list_t *dst, fr_pair_list_t *src)
Appends a list of fr_pair_t from a temporary list to a destination list.
#define PAIR_LIST_VERIFY(_x)
Definition pair.h:207
fr_pair_t * fr_pair_list_head(fr_pair_list_t const *list)
Get the head of a valuepair list.
Definition pair_inline.c:42
static fr_slen_t parent
Definition pair.h:858
int af
AF_INET, AF_INET6, or AF_UNIX.
Definition socket.h:78
int fd
File descriptor if this is a live socket.
Definition socket.h:81
int type
SOCK_STREAM, SOCK_DGRAM, etc.
Definition socket.h:79
void fr_perror(char const *fmt,...)
Print the current error to stderr with a prefix.
Definition strerror.c:732
void fr_strerror_clear(void)
Clears all pending messages from the talloc pools.
Definition strerror.c:576
int fr_check_lib_magic(uint64_t magic)
Check if the application linking to the library has the correct magic number.
Definition version.c:40
#define RADIUSD_VERSION_BUILD(_x)
Create a version string for a utility in the suite of FreeRADIUS utilities.
Definition version.h:58
#define RADIUSD_MAGIC_NUMBER
Definition version.h:81
int8_t fr_value_box_cmp(fr_value_box_t const *a, fr_value_box_t const *b)
Compare two values.
Definition value.c:749