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: 33c3f489a11a82de7dfe8af27cc1c189bc56f373 $
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: 33c3f489a11a82de7dfe8af27cc1c189bc56f373 $")
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
103
104static char const *radclient_version = RADIUSD_VERSION_BUILD("radclient");
105
107static fr_dict_t const *dict_radius;
108
111 { .out = &dict_freeradius, .proto = "freeradius" },
112 { .out = &dict_radius, .proto = "radius" },
114};
115
117
121
124
131
134
135static fr_dict_attr_t const *attr_coa_filter = NULL;
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 <raddb> Set user dictionary directory (defaults to " RADDBDIR ").\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{
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/*
385 * Read one CoA reply and possibly filter
386 */
387static 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)
388{
389 rc_request_t *request;
390 fr_pair_t *vp;
391
392 /*
393 * Allocate it.
394 */
395 MEM(request = talloc_zero(parent, rc_request_t));
396 MEM(request->reply = fr_packet_alloc(request, false));
397
398 /*
399 * Don't initialize src/dst IP/port, or anything else. That will be read from the network.
400 */
401 fr_pair_list_init(&request->filter);
404
405 /*
406 * Read the reply VP's.
407 */
409 &request->reply_pairs, coa_reply, coa_reply_done) < 0) {
410 REDEBUG("Error parsing \"%s\"", reply_filename);
411 error:
412 talloc_free(request);
413 return -1;
414 }
415
416 /*
417 * The reply can be empty. In which case we just send an empty ACK.
418 */
420 if (vp) request->reply->code = vp->vp_uint32;
421
422 /*
423 * Read in filter VP's.
424 */
425 if (coa_filter) {
427 &request->filter, coa_filter, coa_filter_done) < 0) {
428 REDEBUG("Error parsing \"%s\"", filter_filename);
429 goto error;
430 }
431
432 if (*coa_filter_done && !*coa_reply_done) {
433 REDEBUG("Differing number of replies/filters in %s:%s "
434 "(too many replies))", reply_filename, filter_filename);
435 goto error;
436 }
437
438 if (!*coa_filter_done && *coa_reply_done) {
439 REDEBUG("Differing number of replies/filters in %s:%s "
440 "(too many filters))", reply_filename, filter_filename);
441 goto error;
442 }
443
444 /*
445 * This allows efficient list comparisons later
446 */
448 }
449
450 request->name = parent->name;
451
452 /*
453 * Automatically set the response code from the request code
454 * (if one wasn't already set).
455 */
456 if (request->filter_code == FR_RADIUS_CODE_UNDEFINED) {
458 }
459
460 parent->coa = request;
461
462 /*
463 * Ensure that the packet is also tracked in the CoA tree.
464 */
467 ERROR("Failed inserting packet from %s into CoA tree", request->name);
468 fr_exit_now(1);
469 }
470
471 return 0;
472}
473
474/*
475 * Initialize a radclient data structure and add it to
476 * the global linked list.
477 */
478static int radclient_init(TALLOC_CTX *ctx, rc_file_pair_t *files)
479{
480 FILE *packets, *filters = NULL;
481
482 fr_pair_t *vp;
483 rc_request_t *request = NULL;
484 bool packets_done = false;
485 uint64_t num = 0;
486
487 FILE *coa_reply = NULL;
488 FILE *coa_filter = NULL;
489 bool coa_reply_done = false;
490 bool coa_filter_done = false;
491
492 assert(files->packets != NULL);
493
494 /*
495 * Determine where to read the VP's from.
496 */
497 if (strcmp(files->packets, "-") != 0) {
498 packets = fopen(files->packets, "r");
499 if (!packets) {
500 ERROR("Error opening %s: %s", files->packets, fr_syserror(errno));
501 return -1;
502 }
503
504 /*
505 * Read in the pairs representing the expected response.
506 */
507 if (files->filters) {
508 filters = fopen(files->filters, "r");
509 if (!filters) {
510 ERROR("Error opening %s: %s", files->filters, fr_syserror(errno));
511 goto error;
512 }
513 }
514
515 if (files->coa_reply) {
516 coa_reply = fopen(files->coa_reply, "r");
517 if (!coa_reply) {
518 ERROR("Error opening %s: %s", files->coa_reply, fr_syserror(errno));
519 goto error;
520 }
521 }
522
523 if (files->coa_filter) {
524 coa_filter = fopen(files->coa_filter, "r");
525 if (!coa_filter) {
526 ERROR("Error opening %s: %s", files->coa_filter, fr_syserror(errno));
527 goto error;
528 }
529 }
530 } else {
531 packets = stdin;
532 }
533
534 /*
535 * Loop until the file is done.
536 */
537 do {
538 char const *coa_reply_filename = NULL;
539 char const *coa_filter_filename = NULL;
540
541 /*
542 * Allocate it.
543 */
544 MEM(request = talloc_zero(ctx, rc_request_t));
545 MEM(request->packet = fr_packet_alloc(request, true));
546 request->packet->uctx = request;
547
548 request->packet->socket.inet.src_ipaddr = client_ipaddr;
549 request->packet->socket.inet.src_port = client_port;
550 request->packet->socket.inet.dst_ipaddr = server_ipaddr;
551 request->packet->socket.inet.dst_port = server_port;
552 request->packet->socket.type = (ipproto == IPPROTO_TCP) ? SOCK_STREAM : SOCK_DGRAM;
553
554 request->files = files;
555 request->packet->id = last_used_id;
556 request->num = num++;
557
558 fr_pair_list_init(&request->filter);
561
562 /*
563 * Read the request VP's.
564 */
566 &request->request_pairs, packets, &packets_done) < 0) {
567 char const *input;
568
569 if ((files->packets[0] == '-') && (files->packets[1] == '\0')) {
570 input = "stdin";
571 } else {
572 input = files->packets;
573 }
574
575 REDEBUG("Error parsing \"%s\"", input);
576 goto error;
577 }
578
579 /*
580 * Skip empty entries
581 */
582 if (fr_pair_list_empty(&request->request_pairs)) {
583 WARN("Skipping \"%s\": No Attributes", files->packets);
584 talloc_free(request);
585 continue;
586 }
587
588 /*
589 * Read in filter VP's.
590 */
591 if (filters) {
592 bool filters_done;
593
595 &request->filter, filters, &filters_done) < 0) {
596 REDEBUG("Error parsing \"%s\"", files->filters);
597 goto error;
598 }
599
600 if (filters_done && !packets_done) {
601 REDEBUG("Differing number of packets/filters in %s:%s "
602 "(too many requests))", files->packets, files->filters);
603 goto error;
604 }
605
606 if (!filters_done && packets_done) {
607 REDEBUG("Differing number of packets/filters in %s:%s "
608 "(too many filters))", files->packets, files->filters);
609 goto error;
610 }
611
612 vp = fr_pair_find_by_da(&request->filter, NULL, attr_packet_type);
613 if (vp) {
614 request->filter_code = vp->vp_uint32;
615 fr_pair_delete(&request->filter, vp);
616 }
617
618 /*
619 * This allows efficient list comparisons later
620 */
622 }
623
624 /*
625 * Process special attributes
626 */
627 for (vp = fr_pair_list_head(&request->request_pairs);
628 vp;
629 vp = fr_pair_list_next(&request->request_pairs, vp)) {
630 /*
631 * Allow it to set the packet type in
632 * the attributes read from the file.
633 */
634 if (vp->da == attr_packet_type) {
635 request->packet->code = vp->vp_uint32;
636 } else if (vp->da == attr_request_authenticator) {
637 if (vp->vp_length > sizeof(request->packet->vector)) {
638 memcpy(request->packet->vector, vp->vp_octets, sizeof(request->packet->vector));
639 } else {
640 memset(request->packet->vector, 0, sizeof(request->packet->vector));
641 memcpy(request->packet->vector, vp->vp_octets, vp->vp_length);
642 }
643 } else if (vp->da == attr_cleartext_password) {
644 request->password = vp;
645 /*
646 * Keep a copy of the the password attribute.
647 */
648 } else if (vp->da == attr_chap_password) {
649 /*
650 * If it's already hex, do nothing.
651 */
652 if ((vp->vp_length == 17) && (already_hex(vp))) continue;
653
654 /*
655 * CHAP-Password is octets, so it may not be zero terminated.
656 */
658 fr_pair_value_bstrndup(request->password, vp->vp_strvalue, vp->vp_length, true);
659 } else if (vp->da == attr_ms_chap_password) {
661 fr_pair_value_bstrndup(request->password, vp->vp_strvalue, vp->vp_length, true);
662
663 } else if (vp->da == attr_radclient_test_name) {
664 request->name = vp->vp_strvalue;
665
666 } else if (vp->da == attr_radclient_coa_filename) {
667 coa_reply_filename = vp->vp_strvalue;
668
669 } else if (vp->da == attr_radclient_coa_filter) {
670 coa_filter_filename = vp->vp_strvalue;
671 }
672 } /* loop over the VP's we read in */
673
674 /*
675 * Use the default set on the command line
676 */
677 if (request->packet->code == FR_RADIUS_CODE_UNDEFINED) request->packet->code = packet_code;
678
679 /*
680 * Fill in the packet header from attributes, and then
681 * re-realize the attributes.
682 */
683 fr_packet_net_from_pairs(request->packet, &request->request_pairs);
684
685 /*
686 * Default to the filename
687 */
688 if (!request->name) request->name = request->files->packets;
689
690 /*
691 * Automatically set the response code from the request code
692 * (if one wasn't already set).
693 */
694 if (request->filter_code == FR_RADIUS_CODE_UNDEFINED) {
695 switch (request->packet->code) {
698 break;
699
702 break;
703
706 break;
707
710 break;
711
713 switch (radclient_get_code(request->packet->socket.inet.dst_port)) {
716 break;
717
720 break;
721
722 default:
724 break;
725 }
726 break;
727
729 REDEBUG("Packet-Type must be defined,"
730 "or a well known RADIUS port");
731 goto error;
732
733 default:
734 REDEBUG("Can't determine expected reply.Packet-Type for Packet-Type %i",
735 request->packet->code);
736 goto error;
737 }
738 /*
739 * Automatically set the request code from the response code
740 * (if one wasn't already set).
741 */
742 } else if (request->packet->code == FR_RADIUS_CODE_UNDEFINED) {
743 switch (request->filter_code) {
747 break;
748
751 break;
752
756 break;
757
761 break;
762
763 default:
764 REDEBUG("Can't determine expected Packet-Type for reply.Packet-Type %i",
765 request->filter_code);
766 goto error;
767 }
768 }
769
770 /*
771 * Automatically set the dst port (if one wasn't already set).
772 */
773 if (request->packet->socket.inet.dst_port == 0) {
774 radclient_get_port(request->packet->code, &request->packet->socket.inet.dst_port);
775 if (request->packet->socket.inet.dst_port == 0) {
776 REDEBUG("Can't determine destination port");
777 goto error;
778 }
779 }
780
781 /*
782 * Read in the CoA filename and filter.
783 */
784 if (coa_reply_filename) {
785 if (coa_reply) {
786 RDEBUG("Cannot specify CoA file on both the command line and via Radclient-CoA-Filename");
787 goto error;
788 }
789
790 coa_reply = fopen(coa_reply_filename, "r");
791 if (!coa_reply) {
792 ERROR("Error opening %s: %s", coa_reply_filename, fr_syserror(errno));
793 goto error;
794 }
795
796 if (coa_filter_filename) {
797 coa_filter = fopen(coa_filter_filename, "r");
798 if (!coa_filter) {
799 ERROR("Error opening %s: %s", coa_filter_filename, fr_syserror(errno));
800 goto error;
801 }
802 } else {
803 coa_filter = NULL;
804 }
805
806 if (coa_init(request, coa_reply, coa_reply_filename, &coa_reply_done,
807 coa_filter, coa_filter_filename, &coa_filter_done) < 0) {
808 goto error;
809 }
810
811 fclose(coa_reply);
812 coa_reply = NULL;
813 if (coa_filter) {
814 fclose(coa_filter);
815 coa_filter = NULL;
816 }
817 do_coa = true;
818
819 } else if (coa_reply) {
820 if (coa_init(request, coa_reply, coa_reply_filename, &coa_reply_done,
821 coa_filter, coa_filter_filename, &coa_filter_done) < 0) {
822 goto error;
823 }
824
825 if (coa_reply_done != packets_done) {
826 REDEBUG("Differing number of packets in input file and coa_reply in %s:%s ",
827 files->packets, files->coa_reply);
828 goto error;
829
830 }
831 }
832
833 /*
834 * Add it to the tail of the list.
835 */
837
838 /*
839 * Set the destructor so it removes itself from the
840 * request list when freed. We don't set this until
841 * the packet is actually in the list, else we trigger
842 * the asserts in the free callback.
843 */
844 talloc_set_destructor(request, _rc_request_free);
845 } while (!packets_done); /* loop until the file is done. */
846
847 if (packets != stdin) fclose(packets);
848 if (filters) fclose(filters);
849 if (coa_reply) fclose(coa_reply);
850 if (coa_filter) fclose(coa_filter);
851
852 /*
853 * And we're done.
854 */
855 return 0;
856
857error:
858 talloc_free(request);
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 return -1;
866}
867
868
869/*
870 * Sanity check each argument.
871 */
872static int radclient_sane(rc_request_t *request)
873{
874 if (request->packet->socket.inet.dst_port == 0) {
875 request->packet->socket.inet.dst_port = server_port;
876 }
877 if (request->packet->socket.inet.dst_ipaddr.af == AF_UNSPEC) {
878 if (server_ipaddr.af == AF_UNSPEC) {
879 ERROR("No server was given, and request %" PRIu64 " in file %s did not contain "
880 "Packet-Dst-IP-Address", request->num, request->files->packets);
881 return -1;
882 }
883 request->packet->socket.inet.dst_ipaddr = server_ipaddr;
884 }
885 if (request->packet->code == 0) {
886 if (packet_code == -1) {
887 ERROR("Request was \"auto\", and request %" PRIu64 " in file %s did not contain Packet-Type",
888 request->num, request->files->packets);
889 return -1;
890 }
891 request->packet->code = packet_code;
892 }
893 request->packet->socket.fd = -1;
894
895 return 0;
896}
897
898
899static int8_t request_cmp(void const *one, void const *two)
900{
901 rc_request_t const *a = one, *b = two;
902 fr_pair_t *vp1, *vp2;
903
905 vp2 = fr_pair_find_by_da(&b->request_pairs, NULL, attr_coa_filter);
906
907 if (!vp1) return -1;
908 if (!vp2) return +1;
909
910 return fr_value_box_cmp(&vp1->data, &vp2->data);
911}
912
913
914/*
915 * Deallocate packet ID, etc.
916 */
917static void deallocate_id(rc_request_t *request)
918{
919 if (!request || !request->packet ||
920 (request->packet->id < 0)) {
921 return;
922 }
923
924 /*
925 * One more unused RADIUS ID.
926 */
928
929 /*
930 * If we've already sent a packet, free up the old one,
931 * and ensure that the next packet has a unique
932 * authentication vector.
933 */
934 if (request->packet->data) TALLOC_FREE(request->packet->data);
935 if (request->reply) fr_packet_free(&request->reply);
936}
937
938/*
939 * Send one packet.
940 */
941static int send_one_packet(rc_request_t *request)
942{
943 assert(request->done == false);
944
945#ifdef STATIC_ANALYZER
946 if (!secret) fr_exit_now(1);
947#endif
948
949 /*
950 * Remember when we have to wake up, to re-send the
951 * request, of we didn't receive a reply.
952 */
955 }
956
957 /*
958 * Haven't sent the packet yet. Initialize it.
959 */
960 if (!request->tries || request->packet->id == -1) {
961 bool rcode;
962
963 assert(request->reply == NULL);
964
965 /*
966 * Didn't find a free packet ID, we're not done,
967 * we don't sleep, and we stop trying to process
968 * this packet.
969 */
970 retry:
971 request->packet->socket.inet.src_ipaddr.af = server_ipaddr.af;
972 rcode = fr_packet_list_id_alloc(packet_list, ipproto, request->packet, NULL);
973 if (!rcode) {
974 int mysockfd;
975
976 if (ipproto == IPPROTO_TCP) {
977 mysockfd = fr_socket_client_tcp(NULL, NULL,
978 &request->packet->socket.inet.dst_ipaddr,
979 request->packet->socket.inet.dst_port, false);
980 if (mysockfd < 0) {
981 fr_perror("Error opening socket");
982 return -1;
983 }
984 } else {
985 uint16_t port = 0;
986
987 mysockfd = fr_socket_server_udp(&client_ipaddr, &port, NULL, true);
988 if (mysockfd < 0) {
989 fr_perror("Error opening socket");
990 return -1;
991 }
992
993 if (fr_socket_bind(mysockfd, NULL, &client_ipaddr, &port) < 0) {
994 fr_perror("Error binding socket");
995 return -1;
996 }
997 }
998
1000 &request->packet->socket.inet.dst_ipaddr,
1001 request->packet->socket.inet.dst_port, NULL)) {
1002 ERROR("Can't add new socket");
1003 fr_exit_now(1);
1004 }
1005 goto retry;
1006 }
1007
1008 assert(request->packet->id != -1);
1009 assert(request->packet->data == NULL);
1010
1011 /*
1012 * Update the password, so it can be encrypted with the
1013 * new authentication vector.
1014 */
1015 if (request->password) {
1016 fr_pair_t *vp;
1017
1018 if ((vp = fr_pair_find_by_da(&request->request_pairs, NULL, attr_chap_password)) != NULL) {
1019 uint8_t buffer[17];
1020 fr_pair_t *challenge;
1021
1022 /*
1023 * Use CHAP-Challenge pair if present, otherwise create CHAP-Challenge and
1024 * populate with current Request Authenticator.
1025 *
1026 * Request Authenticator is re-calculated by fr_packet_sign
1027 */
1028 challenge = fr_pair_find_by_da(&request->request_pairs, NULL, attr_chap_challenge);
1029 if (!challenge || (challenge->vp_length < 7)) {
1031 fr_pair_value_memdup(challenge, request->packet->vector, RADIUS_AUTH_VECTOR_LENGTH, false);
1032 }
1033
1035 fr_rand() & 0xff, challenge->vp_octets, challenge->vp_length,
1036 request->password->vp_strvalue,
1037 request->password->vp_length);
1038 fr_pair_value_memdup(vp, buffer, sizeof(buffer), false);
1039
1040 } else if (fr_pair_find_by_da_nested(&request->request_pairs, NULL, attr_ms_chap_password) != NULL) {
1041 mschapv1_encode(request->packet, &request->request_pairs, request->password->vp_strvalue);
1042
1043 } else {
1044 DEBUG("WARNING: No password in the request");
1045 }
1046 }
1047
1048 request->timestamp = fr_time();
1049 request->tries = 1;
1050 request->resend++;
1051
1052 } else { /* request->packet->id >= 0 */
1053 fr_time_t now = fr_time();
1054
1055 /*
1056 * FIXME: Accounting packets are never retried!
1057 * The Acct-Delay-Time attribute is updated to
1058 * reflect the delay, and the packet is re-sent
1059 * from scratch!
1060 */
1061
1062 /*
1063 * Not time for a retry, do so.
1064 */
1065 if (fr_time_delta_lt(fr_time_sub(now, request->timestamp), timeout)) {
1066 /*
1067 * When we walk over the tree sending
1068 * packets, we update the minimum time
1069 * required to sleep.
1070 */
1073 sleep_time = fr_time_sub(now, request->timestamp);
1074 }
1075 return 0;
1076 }
1077
1078 /*
1079 * We're not trying later, maybe the packet is done.
1080 */
1081 if (request->tries == retries) {
1082 assert(request->packet->id >= 0);
1083
1084 /*
1085 * Delete the request from the tree of
1086 * outstanding requests.
1087 */
1089
1090 REDEBUG("No reply from server for ID %d socket %d",
1091 request->packet->id, request->packet->socket.fd);
1092 deallocate_id(request);
1093
1094 /*
1095 * Normally we mark it "done" when we've received
1096 * the reply, but this is a special case.
1097 */
1098 if (request->resend == resend_count) {
1099 request->done = true;
1100 }
1101 stats.lost++;
1102 return -1;
1103 }
1104
1105 /*
1106 * We are trying later.
1107 */
1108 request->timestamp = now;
1109 request->tries++;
1110 }
1111
1112 /*
1113 * Send the packet.
1114 */
1115 if (fr_packet_send(request->packet, &request->request_pairs, NULL, secret) < 0) {
1116 REDEBUG("Failed to send packet for ID %d", request->packet->id);
1117 deallocate_id(request);
1118 request->done = true;
1119 return -1;
1120 }
1121
1122 fr_radius_packet_log(&default_log, request->packet, &request->request_pairs, false);
1123
1124 return 0;
1125}
1126
1127/*
1128 * Receive a CoA packet, maybe.
1129 */
1131{
1132 fd_set set;
1133 fr_time_delta_t our_wait_time;
1134 rc_request_t *request, *parent;
1135 fr_packet_t *packet;
1137
1138#ifdef STATIC_ANALYZER
1139 if (!secret) fr_exit_now(1);
1140#endif
1141
1142 /* And wait for reply, timing out as necessary */
1143 FD_ZERO(&set);
1144 FD_SET(coafd, &set);
1145
1146 our_wait_time = !fr_time_delta_ispos(wait_time) ? fr_time_delta_from_sec(0) : wait_time;
1147
1148 /*
1149 * No packet was received.
1150 */
1151 if (select(coafd + 1, &set, NULL, NULL, &fr_time_delta_to_timeval(our_wait_time)) <= 0) return 0;
1152
1153 /*
1154 * Read a packet from a network.
1155 */
1156 packet = fr_packet_recv(NULL, coafd, 0, 200, false);
1157 if (!packet) {
1158 DEBUG("Failed reading CoA packet");
1159 return 0;
1160 }
1161
1162 /*
1163 * Fails the signature validation: not a real reply.
1164 */
1165 if (fr_packet_verify(packet, NULL, secret) < 0) {
1166 DEBUG("CoA verification failed");
1167 return 0;
1168 }
1169
1170 fr_pair_list_init(&my.request_pairs);
1171
1172 /*
1173 * Decode the packet before looking up the parent, so that we can compare the pairs.
1174 */
1175 if (fr_radius_decode_simple(packet, &my.request_pairs,
1176 packet->data, packet->data_len,
1177 NULL, secret) < 0) {
1178 DEBUG("Failed decoding CoA packet");
1179 return 0;
1180 }
1181
1182 fr_radius_packet_log(&default_log, packet, &my.request_pairs, true);
1183
1184 /*
1185 * Find a Access-Request which has the same User-Name / etc. as this CoA packet.
1186 */
1187 my.name = "receive CoA request";
1188 my.packet = packet;
1189
1191 if (!parent) {
1192 DEBUG("No matching request packet for CoA packet %u %u", packet->data[0], packet->data[1]);
1193 talloc_free(packet);
1194 return 0;
1195 }
1196 assert(parent->coa);
1197
1198 request = parent->coa;
1199 request->packet = talloc_steal(request, packet);
1200
1202 fr_pair_list_append(&request->request_pairs, &my.request_pairs);
1203
1204 /*
1205 * If we had an expected response code, check to see if the
1206 * packet matched that.
1207 */
1208 if (request->packet->code != request->filter_code) {
1209 if (FR_RADIUS_PACKET_CODE_VALID(request->reply->code)) {
1210 REDEBUG("%s: Expected %s got %s", request->name, fr_radius_packet_name[request->filter_code],
1211 fr_radius_packet_name[request->packet->code]);
1212 } else {
1213 REDEBUG("%s: Expected %u got %i", request->name, request->filter_code,
1214 request->packet->code);
1215 }
1216 stats.failed++;
1217
1218 /*
1219 * Check if the contents of the packet matched the filter
1220 */
1221 } else if (fr_pair_list_empty(&request->filter)) {
1222 stats.passed++;
1223
1224 } else {
1225 fr_pair_t const *failed[2];
1226
1228 if (fr_pair_validate(failed, &request->filter, &request->request_pairs)) {
1229 RDEBUG("%s: CoA request passed filter", request->name);
1230 stats.passed++;
1231 } else {
1232 fr_pair_validate_debug(failed);
1233 REDEBUG("%s: CoA Request for failed filter", request->name);
1234 stats.failed++;
1235 }
1236 }
1237
1238 request->reply->id = request->packet->id;
1239
1240 request->reply->socket.type = SOCK_DGRAM;
1241 request->reply->socket.af = client_ipaddr.af;
1242 request->reply->socket.fd = coafd;
1243 request->reply->socket.inet.src_ipaddr = client_ipaddr;
1244 request->reply->socket.inet.src_port = coa_port;
1245 request->reply->socket.inet.dst_ipaddr = packet->socket.inet.src_ipaddr;
1246 request->reply->socket.inet.dst_port = packet->socket.inet.src_port;
1247
1248 if (!request->reply->code) switch (packet->code) {
1250 request->reply->code = FR_RADIUS_CODE_COA_ACK;
1251 break;
1252
1255 break;
1256
1257 default:
1258 RDEBUG("Failed getting reply packet type");
1259 return 0;
1260 }
1261
1262 fr_radius_packet_log(&default_log, request->reply, &request->reply_pairs, false);
1263
1264
1265 /*
1266 * Send reply.
1267 */
1268 if (fr_packet_send(request->reply, &request->reply_pairs, packet, secret) < 0) {
1269 REDEBUG("Failed sending CoA reply");
1270 return 0;
1271 }
1272
1273 fr_rb_remove(coa_tree, request);
1274
1275 /*
1276 * No longer waiting for a CoA packet for this request.
1277 */
1278 TALLOC_FREE(parent->coa);
1279 return 0;
1280}
1281
1282
1283/*
1284 * Do Blast RADIUS checks.
1285 *
1286 * The request is an Access-Request, and does NOT contain Proxy-State.
1287 *
1288 * The reply is a raw packet, and is NOT yet decoded.
1289 */
1290static int blast_radius_check(rc_request_t *request, fr_packet_t *reply)
1291{
1292 uint8_t *attr, *end;
1293 fr_pair_t *vp;
1294 bool have_message_authenticator = false;
1295
1296 /*
1297 * We've received a raw packet. Nothing has (as of yet) checked
1298 * anything in it other than the length, and that it's a
1299 * well-formed RADIUS packet.
1300 */
1301 switch (reply->data[0]) {
1306 if (reply->data[1] != request->packet->id) {
1307 ERROR("Invalid reply ID %d to Access-Request ID %d", reply->data[1], request->packet->id);
1308 return -1;
1309 }
1310 break;
1311
1312 default:
1313 ERROR("Invalid reply code %d to Access-Request", reply->data[0]);
1314 return -1;
1315 }
1316
1317 /*
1318 * If the reply has a Message-Authenticator, then it MIGHT be fine.
1319 */
1320 attr = reply->data + 20;
1321 end = reply->data + reply->data_len;
1322
1323 /*
1324 * It should be the first attribute, so we warn if it isn't there.
1325 *
1326 * But it's not a fatal error.
1327 */
1328 if (blast_radius && (attr[0] != FR_MESSAGE_AUTHENTICATOR)) {
1329 RDEBUG("WARNING The %s reply packet does not have Message-Authenticator as the first attribute. The packet may be vulnerable to Blast RADIUS attacks.",
1330 fr_radius_packet_name[reply->data[0]]);
1331 }
1332
1333 /*
1334 * Set up for Proxy-State checks.
1335 *
1336 * If we see a Proxy-State in the reply which we didn't send, then it's a Blast RADIUS attack.
1337 */
1339
1340 while (attr < end) {
1341 /*
1342 * Blast RADIUS work-arounds require that
1343 * Message-Authenticator is the first attribute in the
1344 * reply. Note that we don't check for it being the
1345 * first attribute, but simply that it exists.
1346 *
1347 * That check is a balance between securing the reply
1348 * packet from attacks, and not violating the RFCs which
1349 * say that there is no order to attributes in the
1350 * packet.
1351 *
1352 * However, no matter the status of the '-b' flag we
1353 * still can check for the signature of the attack, and
1354 * discard packets which are suspicious. This behavior
1355 * protects radclient from the attack, without mandating
1356 * new behavior on the server side.
1357 *
1358 * Note that we don't set the '-b' flag by default.
1359 * radclient is intended for testing / debugging, and is
1360 * not intended to be used as part of a secure login /
1361 * user checking system.
1362 */
1363 if (attr[0] == FR_MESSAGE_AUTHENTICATOR) {
1364 have_message_authenticator = true;
1365 goto next;
1366 }
1367
1368 /*
1369 * If there are Proxy-State attributes in the reply, they must
1370 * match EXACTLY the Proxy-State attributes in the request.
1371 *
1372 * Note that we don't care if there are more Proxy-States
1373 * in the request than in the reply. The Blast RADIUS
1374 * issue requires _adding_ Proxy-State attributes, and
1375 * cannot work when the server _deletes_ Proxy-State
1376 * attributes.
1377 */
1378 if (attr[0] == FR_PROXY_STATE) {
1379 if (!vp || (vp->vp_length != (size_t) (attr[1] - 2)) || (memcmp(vp->vp_octets, attr + 2, vp->vp_length) != 0)) {
1380 ERROR("Invalid reply to Access-Request ID %d - Discarding packet due to Blast RADIUS attack being detected.", request->packet->id);
1381 ERROR("We received a Proxy-State in the reply which we did not send, or which is different from what we sent.");
1382 return -1;
1383 }
1384
1386 }
1387
1388 next:
1389 attr += attr[1];
1390 }
1391
1392 /*
1393 * If "-b" is set, then we require Message-Authenticator in the reply.
1394 */
1395 if (blast_radius && !have_message_authenticator) {
1396 ERROR("The %s reply packet does not contain Message-Authenticator - discarding packet due to Blast RADIUS checks.",
1397 fr_radius_packet_name[reply->data[0]]);
1398 return -1;
1399 }
1400
1401 /*
1402 * The packet doesn't look like it's a Blast RADIUS attack. The
1403 * caller will now verify the packet signature.
1404 */
1405 return 0;
1406}
1407
1408/*
1409 * Receive one packet, maybe.
1410 */
1412{
1413 fd_set set;
1414 fr_time_delta_t our_wait_time;
1415 rc_request_t *request;
1416 fr_packet_t *reply, *packet;
1417 volatile int max_fd;
1418
1419#ifdef STATIC_ANALYZER
1420 if (!secret) fr_exit_now(1);
1421#endif
1422
1423 /* And wait for reply, timing out as necessary */
1424 FD_ZERO(&set);
1425
1426 max_fd = fr_packet_list_fd_set(packet_list, &set);
1427 if (max_fd < 0) fr_exit_now(1); /* no sockets to listen on! */
1428
1429 our_wait_time = !fr_time_delta_ispos(wait_time) ? fr_time_delta_from_sec(0) : wait_time;
1430
1431 if (do_coa && fr_rb_num_elements(coa_tree) > 0) {
1432 FD_SET(coafd, &set);
1433 if (coafd >= max_fd) max_fd = coafd + 1;
1434 }
1435
1436 /*
1437 * See if a packet was received.
1438 */
1439retry:
1440 if (select(max_fd, &set, NULL, NULL, &fr_time_delta_to_timeval(our_wait_time)) <= 0) return 0;
1441
1442 /*
1443 * Read a CoA packet
1444 */
1445 if (FD_ISSET(coafd, &set)) {
1447 FD_CLR(coafd, &set);
1448 our_wait_time = fr_time_delta_from_sec(0);
1449 goto retry;
1450 }
1451
1452 /*
1453 * Look for the packet.
1454 */
1456 if (!reply) {
1457 ERROR("Received bad packet");
1458
1459 /*
1460 * If the packet is bad, we close the socket.
1461 * I'm not sure how to do that now, so we just
1462 * die...
1463 */
1464 if (ipproto == IPPROTO_TCP) fr_exit_now(1);
1465 return -1; /* bad packet */
1466 }
1467
1468 /*
1469 * We don't use udpfromto. So if we bind to "*", we want
1470 * to find replies sent to 192.0.2.4. Therefore, we
1471 * force all replies to have the one address we know
1472 * about, no matter what real address they were sent to.
1473 *
1474 * This only works if were not using any of the
1475 * Packet-* attributes, or running with 'auto'.
1476 */
1477 reply->socket.inet.dst_ipaddr = client_ipaddr;
1478 reply->socket.inet.dst_port = client_port;
1479
1480 /*
1481 * TCP sockets don't use recvmsg(), and thus don't get
1482 * the source IP/port. However, since they're TCP, we
1483 * know what the source IP/port is, because that's where
1484 * we connected to.
1485 */
1486 if (ipproto == IPPROTO_TCP) {
1487 reply->socket.inet.src_ipaddr = server_ipaddr;
1488 reply->socket.inet.src_port = server_port;
1489 }
1490
1492 if (!packet) {
1493 ERROR("Received reply to request we did not send. (id=%d socket %d)",
1494 reply->id, reply->socket.fd);
1495 fr_packet_free(&reply);
1496 return -1; /* got reply to packet we didn't send */
1497 }
1498 request = packet->uctx;
1499
1500 /*
1501 * We want radclient to be able to send any packet, including
1502 * imperfect ones. However, we do NOT want to be vulnerable to
1503 * the "Blast RADIUS" issue. Instead of adding command-line
1504 * flags to enable/disable similar flags to what the server
1505 * sends, we just do a few more smart checks to double-check
1506 * things.
1507 */
1508 if ((request->packet->code == FR_RADIUS_CODE_ACCESS_REQUEST) &&
1509 blast_radius_check(request, reply) < 0) {
1510 fr_packet_free(&reply);
1511 return -1;
1512 }
1513
1514 /*
1515 * Fails the signature validation: not a real reply.
1516 * FIXME: Silently drop it and listen for another packet.
1517 */
1518 if (fr_packet_verify(reply, request->packet, secret) < 0) {
1519 REDEBUG("Reply verification failed");
1520 stats.lost++;
1521 goto packet_done; /* shared secret is incorrect */
1522 }
1523
1524 if (print_filename) {
1525 RDEBUG("%s response code %d", request->files->packets, reply->code);
1526 }
1527
1528 if (request->tries < ignore_count) {
1529 RDEBUG("Ignoring response %d due to -e ignore_count=%d", request->tries, ignore_count);
1530 goto packet_done;
1531 }
1532
1533 deallocate_id(request);
1534 request->reply = reply;
1535 reply = NULL;
1536
1537 /*
1538 * If this fails, we're out of memory.
1539 */
1540 if (fr_radius_decode_simple(request, &request->reply_pairs,
1541 request->reply->data, request->reply->data_len,
1542 request->packet->vector, secret) < 0) {
1543 REDEBUG("Reply decode failed");
1544 stats.lost++;
1545 goto packet_done;
1546 }
1547 PAIR_LIST_VERIFY(&request->reply_pairs);
1548 fr_radius_packet_log(&default_log, request->reply, &request->reply_pairs, true);
1549
1550 /*
1551 * Increment counters...
1552 */
1553 switch (request->reply->code) {
1558 stats.accepted++;
1559 break;
1560
1562 break;
1563
1565 stats.error++;
1566 break;
1567 default:
1568 stats.rejected++;
1569 }
1570
1571 fr_strerror_clear(); /* Clear strerror buffer */
1572
1573 /*
1574 * If we had an expected response code, check to see if the
1575 * packet matched that.
1576 */
1577 if ((request->filter_code != FR_RADIUS_CODE_UNDEFINED) && (request->reply->code != request->filter_code)) {
1578 if (FR_RADIUS_PACKET_CODE_VALID(request->reply->code)) {
1579 REDEBUG("%s: Expected %s got %s", request->name, fr_radius_packet_name[request->filter_code],
1580 fr_radius_packet_name[request->reply->code]);
1581 } else {
1582 REDEBUG("%s: Expected %u got %i", request->name, request->filter_code,
1583 request->reply->code);
1584 }
1585 stats.failed++;
1586 /*
1587 * Check if the contents of the packet matched the filter
1588 */
1589 } else if (fr_pair_list_empty(&request->filter)) {
1590 stats.passed++;
1591 } else {
1592 fr_pair_t const *failed[2];
1593
1595 if (fr_pair_validate(failed, &request->filter, &request->reply_pairs)) {
1596 RDEBUG("%s: Response passed filter", request->name);
1597 stats.passed++;
1598 } else {
1599 fr_pair_validate_debug(failed);
1600 REDEBUG("%s: Response for failed filter", request->name);
1601 stats.failed++;
1602 }
1603 }
1604
1605 if (request->resend == resend_count) {
1606 request->done = true;
1607 }
1608
1609packet_done:
1610 fr_packet_free(&request->reply);
1611 fr_packet_free(&reply); /* may be NULL */
1612
1613 return 0;
1614}
1615
1616/**
1617 *
1618 * @hidecallgraph
1619 */
1620int main(int argc, char **argv)
1621{
1622 int ret = EXIT_SUCCESS;
1623 int c;
1624 char const *raddb_dir = RADDBDIR;
1625 char const *dict_dir = DICTDIR;
1626 char filesecret[256];
1627 FILE *fp;
1628 int do_summary = false;
1629 int persec = 0;
1630 int parallel = 1;
1631 int force_af = AF_UNSPEC;
1632#ifndef NDEBUG
1633 TALLOC_CTX *autofree;
1634#endif
1635 fr_dlist_head_t filenames;
1636 rc_request_t *request;
1637
1638 /*
1639 * It's easier having two sets of flags to set the
1640 * verbosity of library calls and the verbosity of
1641 * radclient.
1642 */
1643 fr_debug_lvl = 0;
1644 fr_log_fp = stdout;
1645
1646 /*
1647 * Must be called first, so the handler is called last
1648 */
1650
1651#ifndef NDEBUG
1653
1654 if (fr_fault_setup(autofree, getenv("PANIC_ACTION"), argv[0]) < 0) {
1655 fr_perror("radclient");
1656 fr_exit_now(EXIT_FAILURE);
1657 }
1658#endif
1659
1660 talloc_set_log_stderr();
1661
1663
1664 fr_dlist_talloc_init(&filenames, rc_file_pair_t, entry);
1665
1666 /*
1667 * Always log to stdout
1668 */
1670 default_log.fd = STDOUT_FILENO;
1671 default_log.print_level = false;
1672
1673 while ((c = getopt(argc, argv, "46bc:A:C:d:D:e:f:Fhi:n:o:p:P:r:sS:t:vx")) != -1) switch (c) {
1674 case '4':
1675 force_af = AF_INET;
1676 break;
1677
1678 case '6':
1679 force_af = AF_INET6;
1680 break;
1681
1682 case 'A':
1683 attr_coa_filter_name = optarg;
1684 break;
1685
1686 case 'b':
1687 blast_radius = true;
1688 break;
1689
1690 case 'c':
1691 if (!isdigit((uint8_t) *optarg)) usage();
1692
1693 resend_count = atoi(optarg);
1694
1695 if (resend_count < 1) usage();
1696 break;
1697
1698 case 'C':
1699 {
1700 int tmp;
1701
1702 if (strchr(optarg, ':')) {
1704 optarg, -1, AF_UNSPEC, true, false) < 0) {
1705 fr_perror("Failed parsing source address");
1706 fr_exit_now(1);
1707 }
1708 break;
1709 }
1710
1711 tmp = atoi(optarg);
1712 if (tmp < 1 || tmp > 65535) usage();
1713
1714 client_port = (uint16_t)tmp;
1715 }
1716 break;
1717
1718 case 'D':
1719 dict_dir = optarg;
1720 break;
1721
1722 case 'd':
1723 raddb_dir = optarg;
1724 break;
1725
1726 case 'e': /* magical extra stuff */
1727 if (strncmp(optarg, "ignore_count=", 13) == 0) {
1728 ignore_count = atoi(optarg + 13);
1729 break;
1730 }
1731 usage();
1732
1733 /*
1734 * packet,filter,coa_reply,coa_filter
1735 */
1736 case 'f':
1737 {
1738 char const *p;
1739 rc_file_pair_t *files;
1740
1741 MEM(files = talloc_zero(talloc_autofree_context(), rc_file_pair_t));
1742
1743 /*
1744 * Commas are nicer than colons.
1745 */
1746 c = ':';
1747
1748 p = strchr(optarg, c);
1749 if (!p) {
1750 c = ',';
1751 p = strchr(optarg, c);
1752 }
1753 if (!p) {
1754 files->packets = optarg;
1755 files->filters = NULL;
1756 } else {
1757 char *q;
1758
1759 MEM(files->packets = talloc_strndup(files, optarg, p - optarg));
1760 files->filters = p + 1;
1761
1762 /*
1763 * Look for CoA filename
1764 */
1765 q = strchr(files->filters, c);
1766 if (q) {
1767 do_coa = true;
1768
1769 *(q++) = '\0';
1770 files->coa_reply = q;
1771
1772 q = strchr(files->coa_reply, c);
1773 if (q) {
1774 *(q++) = '\0';
1775 files->coa_filter = q;
1776 }
1777 }
1778 }
1779 fr_dlist_insert_tail(&filenames, files);
1780 }
1781 break;
1782
1783 case 'F':
1784 print_filename = true;
1785 break;
1786
1787 case 'i':
1788 if (!isdigit((uint8_t) *optarg))
1789 usage();
1790 last_used_id = atoi(optarg);
1791 if ((last_used_id < 0) || (last_used_id > 255)) {
1792 usage();
1793 }
1794 break;
1795
1796 case 'n':
1797 persec = atoi(optarg);
1798 if (persec <= 0) usage();
1799 break;
1800
1801 case 'o':
1802 coa_port = atoi(optarg);
1803 break;
1804
1805 /*
1806 * Note that sending MANY requests in
1807 * parallel can over-run the kernel
1808 * queues, and Linux will happily discard
1809 * packets. So even if the server responds,
1810 * the client may not see the reply.
1811 */
1812 case 'p':
1813 parallel = atoi(optarg);
1814 if (parallel <= 0) usage();
1815 break;
1816
1817 case 'P':
1818 if (!strcmp(optarg, "tcp")) {
1819 ipproto = IPPROTO_TCP;
1820 } else if (!strcmp(optarg, "udp")) {
1821 ipproto = IPPROTO_UDP;
1822 } else {
1823 usage();
1824 }
1825 break;
1826
1827 case 'r':
1828 if (!isdigit((uint8_t) *optarg)) usage();
1829 retries = atoi(optarg);
1830 if ((retries == 0) || (retries > 1000)) usage();
1831 break;
1832
1833 case 's':
1834 do_summary = true;
1835 break;
1836
1837 case 'S':
1838 {
1839 char *p;
1840 fp = fopen(optarg, "r");
1841 if (!fp) {
1842 ERROR("Error opening %s: %s", optarg, fr_syserror(errno));
1843 fr_exit_now(1);
1844 }
1845 if (fgets(filesecret, sizeof(filesecret), fp) == NULL) {
1846 ERROR("Error reading %s: %s", optarg, fr_syserror(errno));
1847 fr_exit_now(1);
1848 }
1849 fclose(fp);
1850
1851 /* truncate newline */
1852 p = filesecret + strlen(filesecret) - 1;
1853 while ((p >= filesecret) &&
1854 (*p < ' ')) {
1855 *p = '\0';
1856 --p;
1857 }
1858
1859 if (strlen(filesecret) < 2) {
1860 ERROR("Secret in %s is too short", optarg);
1861 fr_exit_now(1);
1862 }
1863 secret = talloc_strdup(NULL, filesecret);
1864 }
1865 break;
1866
1867 case 't':
1868 if (fr_time_delta_from_str(&timeout, optarg, strlen(optarg), FR_TIME_RES_SEC) < 0) {
1869 fr_perror("Failed parsing timeout value");
1870 fr_exit_now(EXIT_FAILURE);
1871 }
1872 break;
1873
1874 case 'v':
1875 fr_debug_lvl = 1;
1876 DEBUG("%s", radclient_version);
1877 fr_exit_now(0);
1878
1879 case 'x':
1880 fr_debug_lvl++;
1881 if (fr_debug_lvl > 1) default_log.print_level = true;
1882 break;
1883
1884 case 'h':
1885 default:
1886 usage();
1887 }
1888 argc -= (optind - 1);
1889 argv += (optind - 1);
1890
1891 if ((argc < 3) || ((secret == NULL) && (argc < 4))) {
1892 ERROR("Insufficient arguments");
1893 usage();
1894 }
1895 /*
1896 * Mismatch between the binary and the libraries it depends on
1897 */
1899 fr_perror("radclient");
1900 fr_exit_now(EXIT_FAILURE);
1901 }
1902
1903 if (!fr_dict_global_ctx_init(NULL, true, dict_dir)) {
1904 fr_perror("radclient");
1905 fr_exit_now(EXIT_FAILURE);
1906 }
1907
1908 if (fr_radius_global_init() < 0) {
1909 fr_perror("radclient");
1910 fr_exit_now(EXIT_FAILURE);
1911 }
1912
1914 fr_perror("radclient");
1915 exit(EXIT_FAILURE);
1916 }
1917
1919 fr_perror("radclient");
1920 exit(EXIT_FAILURE);
1921 }
1922
1924 fr_log_perror(&default_log, L_ERR, __FILE__, __LINE__, NULL,
1925 "Failed to initialize the dictionaries");
1926 exit(EXIT_FAILURE);
1927 }
1928
1929 if (do_coa) {
1931 if (!attr_coa_filter) {
1932 ERROR("Unknown or invalid CoA filter attribute %s", optarg);
1933 fr_exit_now(1);
1934 }
1935
1936 /*
1937 * If there's no attribute given to match CoA to requests, use User-Name
1938 */
1940
1942 }
1944
1945 fr_strerror_clear(); /* Clear the error buffer */
1946
1947 /*
1948 * Get the request type
1949 */
1950 if (!isdigit((uint8_t) argv[2][0])) {
1952 if (packet_code == -2) {
1953 ERROR("Unrecognised request type \"%s\"", argv[2]);
1954 usage();
1955 }
1956 } else {
1957 packet_code = atoi(argv[2]);
1958 }
1959
1960 /*
1961 * Resolve hostname.
1962 */
1963 if (strcmp(argv[1], "-") != 0) {
1964 if (fr_inet_pton_port(&server_ipaddr, &server_port, argv[1], -1, force_af, true, true) < 0) {
1965 fr_perror("radclient");
1966 fr_exit_now(1);
1967 }
1968
1969 /*
1970 * Work backwards from the port to determine the packet type
1971 */
1973 }
1975
1976 /*
1977 * Add the secret.
1978 */
1979 if (argv[3]) secret = talloc_strdup(NULL, argv[3]);
1980
1981 /*
1982 * If no '-f' is specified, we're reading from stdin.
1983 */
1984 if (fr_dlist_num_elements(&filenames) == 0) {
1985 rc_file_pair_t *files;
1986
1987 files = talloc_zero(talloc_autofree_context(), rc_file_pair_t);
1988 files->packets = "-";
1989 if (radclient_init(files, files) < 0) fr_exit_now(1);
1990 }
1991
1992 /*
1993 * Walk over the list of filenames, creating the requests.
1994 */
1995 fr_dlist_foreach(&filenames, rc_file_pair_t, files) {
1996 if (radclient_init(files, files)) {
1997 ERROR("Failed parsing input files");
1998 fr_exit_now(1);
1999 }
2000 }
2001
2002 /*
2003 * No packets read. Die.
2004 */
2006 ERROR("Nothing to send");
2007 fr_exit_now(1);
2008 }
2009
2010 openssl3_init();
2011
2012 /*
2013 * Bind to the first specified IP address and port.
2014 * This means we ignore later ones.
2015 */
2016 request = fr_dlist_head(&rc_request_list);
2017
2018 if (client_ipaddr.af == AF_UNSPEC) {
2019 if (request->packet->socket.inet.src_ipaddr.af == AF_UNSPEC) {
2020 memset(&client_ipaddr, 0, sizeof(client_ipaddr));
2022 } else {
2023 client_ipaddr = request->packet->socket.inet.src_ipaddr;
2024 }
2025 }
2026
2027 if (client_port == 0) client_port = request->packet->socket.inet.src_port;
2028
2029 if (ipproto == IPPROTO_TCP) {
2030 sockfd = fr_socket_client_tcp(NULL, NULL, &server_ipaddr, server_port, false);
2031 if (sockfd < 0) {
2032 ERROR("Failed opening socket");
2033 return -1;
2034 }
2035
2036 } else {
2038 if (sockfd < 0) {
2039 fr_perror("Error opening socket");
2040 return -1;
2041 }
2042
2043 if (fr_socket_bind(sockfd, NULL, &client_ipaddr, &client_port) < 0) {
2044 fr_perror("Error binding socket");
2045 return -1;
2046 }
2047 }
2048
2049 if (do_coa) {
2051 if (coafd < 0) {
2052 fr_perror("Error opening CoA socket");
2053 return -1;
2054 }
2055
2056 if (fr_socket_bind(coafd, NULL, &client_ipaddr, &coa_port) < 0) {
2057 fr_perror("Error binding socket");
2058 return -1;
2059 }
2060 }
2061
2064 server_port, NULL)) {
2065 ERROR("Failed adding socket");
2066 fr_exit_now(1);
2067 }
2068
2069 /*
2070 * Walk over the list of packets, sanity checking
2071 * everything.
2072 */
2074 this->packet->socket.inet.src_ipaddr = client_ipaddr;
2075 this->packet->socket.inet.src_port = client_port;
2076 if (radclient_sane(this) != 0) {
2077 fr_exit_now(1);
2078 }
2079 }
2080
2081 /*
2082 * Walk over the packets to send, until
2083 * we're all done.
2084 *
2085 * FIXME: This currently busy-loops until it receives
2086 * all of the packets. It should really have some sort of
2087 * send packet, get time to wait, select for time, etc.
2088 * loop.
2089 */
2090 do {
2091 int n = parallel;
2092 rc_request_t *this, *next;
2093 char const *filename = NULL;
2094
2095 done = true;
2097
2098 /*
2099 * Walk over the packets, sending them.
2100 */
2101
2102 for (this = fr_dlist_head(&rc_request_list);
2103 this != NULL;
2104 this = next) {
2105 next = fr_dlist_next(&rc_request_list, this);
2106
2107 /*
2108 * If there's a packet to receive,
2109 * receive it, but don't wait for a
2110 * packet.
2111 */
2113
2114 /*
2115 * This packet is done. Delete it.
2116 */
2117 if (this->done) {
2118 /*
2119 * We still have a CoA reply to
2120 * receive for this packet.
2121 */
2122 if (this->coa) {
2124 if (this->coa) continue;
2125 }
2126
2127 talloc_free(this);
2128 continue;
2129 }
2130
2131 /*
2132 * Packets from multiple '-f' are sent
2133 * in parallel.
2134 *
2135 * Packets from one file are sent in
2136 * series, unless '-p' is specified, in
2137 * which case N packets from each file
2138 * are sent in parallel.
2139 */
2140 if (this->files->packets != filename) {
2141 filename = this->files->packets;
2142 n = parallel;
2143 }
2144
2145 if (n > 0) {
2146 n--;
2147
2148 /*
2149 * Send the current packet.
2150 */
2151 if (send_one_packet(this) < 0) {
2152 talloc_free(this);
2153 break;
2154 }
2155
2156 /*
2157 * Wait a little before sending
2158 * the next packet, if told to.
2159 */
2160 if (persec) {
2161 fr_time_delta_t psec;
2162
2163 psec = (persec == 1) ? fr_time_delta_from_sec(1) : fr_time_delta_wrap(1000000 / persec);
2164
2165 /*
2166 * Don't sleep elsewhere.
2167 */
2169
2170
2171 /*
2172 * Sleep for milliseconds,
2173 * portably.
2174 *
2175 * If we get an error or
2176 * a signal, treat it like
2177 * a normal timeout.
2178 */
2179 select(0, NULL, NULL, NULL, &fr_time_delta_to_timeval(psec));
2180 }
2181
2182 /*
2183 * If we haven't sent this packet
2184 * often enough, we're not done,
2185 * and we shouldn't sleep.
2186 */
2187 if (this->resend < resend_count) {
2188 int i;
2189
2190 done = false;
2192
2193 for (i = 0; i < 4; i++) {
2194 ((uint32_t *) this->packet->vector)[i] = fr_rand();
2195 }
2196 }
2197 } else { /* haven't sent this packet, we're not done */
2198 assert(this->done == false);
2199 assert(this->reply == NULL);
2200 done = false;
2201 }
2202 }
2203
2204 /*
2205 * Still have outstanding requests.
2206 */
2208 done = false;
2209 } else {
2211 }
2212
2213 /*
2214 * Nothing to do until we receive a request, so
2215 * sleep until then. Once we receive one packet,
2216 * we go back, and walk through the whole list again,
2217 * sending more packets (if necessary), and updating
2218 * the sleep time.
2219 */
2222 }
2223 } while (!done);
2224
2226
2228
2230
2232
2234
2236 fr_perror("radclient");
2237 ret = EXIT_FAILURE;
2238 }
2239
2240#ifndef NDEBUG
2242#endif
2243
2244 if (do_summary) {
2245 fr_perror("Packet summary:\n"
2246 "\tAccepted : %" PRIu64 "\n"
2247 "\tRejected : %" PRIu64 "\n"
2248 "\tLost : %" PRIu64 "\n"
2249 "\tErrored : %" PRIu64 "\n"
2250 "\tPassed filter : %" PRIu64 "\n"
2251 "\tFailed filter : %" PRIu64,
2254 stats.lost,
2255 stats.error,
2256 stats.passed,
2258 );
2259 }
2260
2261 /*
2262 * Ensure our atexit handlers run before any other
2263 * atexit handlers registered by third party libraries.
2264 */
2266
2267 if ((stats.lost > 0) || (stats.failed > 0)) return EXIT_FAILURE;
2268
2269 openssl3_free();
2270
2271 return ret;
2272}
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:485
#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:1055
#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
#define DEBUG(fmt,...)
Definition dhcpclient.c:39
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:4737
fr_dict_t * fr_dict_unconst(fr_dict_t const *dict)
Coerce to non-const.
Definition dict_util.c:4932
#define fr_dict_autofree(_to_free)
Definition dict.h:918
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:3567
fr_dict_attr_t const * fr_dict_root(fr_dict_t const *dict)
Return the root attribute of a dictionary.
Definition dict_util.c:2704
fr_dict_attr_t const ** out
Where to write a pointer to the resolved fr_dict_attr_t.
Definition dict.h:293
fr_dict_t const ** out
Where to write a pointer to the loaded/resolved fr_dict_t.
Definition dict.h:306
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:4435
#define fr_dict_autoload(_to_load)
Definition dict.h:915
int fr_dict_read(fr_dict_t *dict, char const *dict_dir, char const *filename)
Read supplementary attribute definitions into an existing dictionary.
#define DICT_AUTOLOAD_TERMINATOR
Definition dict.h:312
Specifies an attribute which must be present for the module to function.
Definition dict.h:292
Specifies a dictionary which must be loaded/loadable for the module to function.
Definition dict.h:305
static void * fr_dlist_head(fr_dlist_head_t const *list_head)
Return the HEAD item of a list or NULL if the list is empty.
Definition dlist.h:486
#define fr_dlist_foreach(_list_head, _type, _iter)
Iterate over the contents of a list.
Definition dlist.h:94
static void * fr_dlist_remove(fr_dlist_head_t *list_head, void *ptr)
Remove an item from the list.
Definition dlist.h:638
static void fr_dlist_talloc_free(fr_dlist_head_t *head)
Free all items in a doubly linked list (with talloc)
Definition dlist.h:908
static unsigned int fr_dlist_num_elements(fr_dlist_head_t const *head)
Return the number of elements in the dlist.
Definition dlist.h:939
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:378
#define fr_dlist_talloc_init(_head, _type, _field)
Initialise the head structure of a doubly linked list.
Definition dlist.h:275
static void * fr_dlist_next(fr_dlist_head_t const *list_head, void const *ptr)
Get the next item in a list.
Definition dlist.h:555
Head of a doubly linked list.
Definition dlist.h:51
void fr_bio_shutdown & my
Definition fd_errno.h:70
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:938
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
talloc_free(reap)
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:323
void fr_packet_list_free(fr_packet_list_t *pl)
Definition list.c:264
fr_packet_t * fr_packet_list_recv(fr_packet_list_t *pl, fd_set *set, uint32_t max_attributes, bool require_message_authenticator)
Definition list.c:672
fr_packet_list_t * fr_packet_list_create(int alloc_id)
Definition list.c:276
bool fr_packet_list_id_alloc(fr_packet_list_t *pl, int proto, fr_packet_t *request, void **pctx)
Definition list.c:395
bool fr_packet_list_yank(fr_packet_list_t *pl, fr_packet_t *request)
Definition list.c:361
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:187
uint32_t fr_packet_list_num_elements(fr_packet_list_t *pl)
Definition list.c:368
int fr_packet_list_fd_set(fr_packet_list_t *pl, fd_set *set)
Definition list.c:645
bool fr_packet_list_id_free(fr_packet_list_t *pl, fr_packet_t *request, bool yank)
Definition list.c:621
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:2945
void fr_pair_validate_debug(fr_pair_t const *failed[2])
Write an error to the library errorbuff detailing the mismatch.
Definition pair.c:2096
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:780
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:703
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:1348
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:1692
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:289
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:2131
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:2795
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:1829
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:2307
int8_t fr_pair_cmp_by_da(void const *a, void const *b)
Order attributes by their da, and tag.
Definition pair.c:1847
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:3020
int fr_pair_list_afrom_file(TALLOC_CTX *ctx, fr_dict_t const *dict, fr_pair_list_t *out, FILE *fp, bool *pfiledone)
Read valuepairs from the fp up to End-Of-File.
int fr_radius_global_init(void)
Definition base.c:1245
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:1223
void fr_radius_global_free(void)
Definition base.c:1269
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:134
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:196
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:282
void fr_radius_packet_log(fr_log_t const *log, fr_packet_t *packet, fr_pair_list_t *list, bool received)
Definition packet.c:482
#define fr_assert(_expr)
Definition rad_assert.h:38
static int blast_radius_check(rc_request_t *request, fr_packet_t *reply)
Definition radclient.c:1290
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:941
static int getport(char const *name)
Definition radclient.c:295
static char const * radclient_version
Definition radclient.c:104
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:872
static int recv_one_packet(fr_time_delta_t wait_time)
Definition radclient.c:1411
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:1620
static fr_dict_t const * dict_freeradius
Definition radclient.c:106
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_t const * dict_radius
Definition radclient.c:107
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:1130
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:917
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:478
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
fr_dict_autoload_t radclient_dict[]
Definition radclient.c:110
static char * secret
Definition radclient.c:71
static fr_dlist_head_t rc_request_list
Definition radclient.c:102
static int coafd
Definition radclient.c:96
static int sockfd
Definition radclient.c:90
static fr_dict_attr_t const * attr_coa_filter
Definition radclient.c:135
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:899
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)
Definition radclient.c:387
Structures for the radclient utility.
#define REDEBUG(fmt,...)
Definition radclient.h:52
uint64_t num
The number (within the file) of the request were reading.
Definition radclient.h:77
#define RDEBUG(fmt,...)
Definition radclient.h:53
fr_pair_t * password
Password.Cleartext.
Definition radclient.h:86
uint64_t accepted
Requests to which we received a accept.
Definition radclient.h:57
fr_packet_t * reply
The incoming response.
Definition radclient.h:90
fr_pair_list_t request_pairs
Definition radclient.h:92
char const * coa_reply
file containing the CoA filter we want to match
Definition radclient.h:71
uint64_t rejected
Requests to which we received a reject.
Definition radclient.h:58
fr_rb_node_t node
rbtree node data for CoA
Definition radclient.h:84
char const * packets
The file containing the request packet.
Definition radclient.h:67
uint64_t lost
Requests to which we received no response.
Definition radclient.h:59
bool done
Whether the request is complete.
Definition radclient.h:100
char const * coa_filter
file containing the CoA filter we want to match
Definition radclient.h:70
fr_pair_list_t filter
If the reply passes the filter, then the request passes.
Definition radclient.h:95
uint64_t failed
Requests which failed a filter.
Definition radclient.h:62
fr_pair_list_t reply_pairs
Definition radclient.h:93
uint64_t passed
Requests which passed a filter.
Definition radclient.h:61
fr_radius_packet_code_t filter_code
Expected code of the response packet.
Definition radclient.h:96
fr_time_t timestamp
Definition radclient.h:87
rc_request_t * coa
CoA filter and reply.
Definition radclient.h:83
char const * name
Test name (as specified in the request).
Definition radclient.h:102
#define WARN(fmt,...)
Definition radclient.h:47
char const * filters
The file containing the definition of the packet we want to match.
Definition radclient.h:68
fr_packet_t * packet
The outgoing request.
Definition radclient.h:89
rc_file_pair_t * files
Request and response file names.
Definition radclient.h:81
uint64_t error
Requests which received a Protocol-Error response.
Definition radclient.h:60
#define RADIUS_MAX_ATTRIBUTES
Definition radius.h:40
#define FR_RADIUS_PACKET_CODE_VALID(_x)
Definition radius.h:52
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:873
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:735
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:227
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
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_autofree_context
The original function is deprecated, so replace it with our version.
Definition talloc.h:51
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:857
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:743