From 6fcc97400a8e6926e6700a050e33cd767d6e67f3 Mon Sep 17 00:00:00 2001 From: Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> Date: Wed, 12 Mar 2025 16:19:38 +0100 Subject: check_radius: clang-format --- plugins/check_radius.c | 451 ++++++++++++++++++++++++------------------------- 1 file changed, 221 insertions(+), 230 deletions(-) (limited to 'plugins') diff --git a/plugins/check_radius.c b/plugins/check_radius.c index d9ff8fa7..646ffcbe 100644 --- a/plugins/check_radius.c +++ b/plugins/check_radius.c @@ -1,32 +1,32 @@ /***************************************************************************** -* -* Monitoring check_radius plugin -* -* License: GPL -* Copyright (c) 1999-2024 Monitoring Plugins Development Team -* -* Description: -* -* This file contains the check_radius plugin -* -* Tests to see if a radius server is accepting connections. -* -* -* This program is free software: you can redistribute it and/or modify -* it under the terms of the GNU General Public License as published by -* the Free Software Foundation, either version 3 of the License, or -* (at your option) any later version. -* -* This program is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -* GNU General Public License for more details. -* -* You should have received a copy of the GNU General Public License -* along with this program. If not, see . -* -* -*****************************************************************************/ + * + * Monitoring check_radius plugin + * + * License: GPL + * Copyright (c) 1999-2024 Monitoring Plugins Development Team + * + * Description: + * + * This file contains the check_radius plugin + * + * Tests to see if a radius server is accepting connections. + * + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + * + *****************************************************************************/ const char *progname = "check_radius"; const char *copyright = "2000-2024"; @@ -37,45 +37,45 @@ const char *email = "devel@monitoring-plugins.org"; #include "netutils.h" #if defined(HAVE_LIBRADCLI) -#include +# include #elif defined(HAVE_LIBFREERADIUS_CLIENT) -#include +# include #elif defined(HAVE_LIBRADIUSCLIENT_NG) -#include +# include #else -#include +# include #endif -static int process_arguments (int /*argc*/, char ** /*argv*/); -static void print_help (void); -void print_usage (void); +static int process_arguments(int /*argc*/, char ** /*argv*/); +static void print_help(void); +void print_usage(void); #if defined(HAVE_LIBFREERADIUS_CLIENT) || defined(HAVE_LIBRADIUSCLIENT_NG) || defined(HAVE_LIBRADCLI) -#define my_rc_conf_str(a) rc_conf_str(rch,a) -#if defined(HAVE_LIBRADCLI) -#define my_rc_send_server(a,b) rc_send_server(rch,a,b,AUTH) -#else -#define my_rc_send_server(a,b) rc_send_server(rch,a,b) -#endif -#if defined(HAVE_LIBFREERADIUS_CLIENT) || defined(HAVE_LIBRADCLI) -#define my_rc_buildreq(a,b,c,d,e,f) rc_buildreq(rch,a,b,c,d,(a)->secret,e,f) -#else -#define my_rc_buildreq(a,b,c,d,e,f) rc_buildreq(rch,a,b,c,d,e,f) -#endif -#define my_rc_avpair_add(a,b,c,d) rc_avpair_add(rch,a,b,c,-1,d) -#define my_rc_read_dictionary(a) rc_read_dictionary(rch, a) +# define my_rc_conf_str(a) rc_conf_str(rch, a) +# if defined(HAVE_LIBRADCLI) +# define my_rc_send_server(a, b) rc_send_server(rch, a, b, AUTH) +# else +# define my_rc_send_server(a, b) rc_send_server(rch, a, b) +# endif +# if defined(HAVE_LIBFREERADIUS_CLIENT) || defined(HAVE_LIBRADCLI) +# define my_rc_buildreq(a, b, c, d, e, f) rc_buildreq(rch, a, b, c, d, (a)->secret, e, f) +# else +# define my_rc_buildreq(a, b, c, d, e, f) rc_buildreq(rch, a, b, c, d, e, f) +# endif +# define my_rc_avpair_add(a, b, c, d) rc_avpair_add(rch, a, b, c, -1, d) +# define my_rc_read_dictionary(a) rc_read_dictionary(rch, a) #else -#define my_rc_conf_str(a) rc_conf_str(a) -#define my_rc_send_server(a,b) rc_send_server(a, b) -#define my_rc_buildreq(a,b,c,d,e,f) rc_buildreq(a,b,c,d,e,f) -#define my_rc_avpair_add(a,b,c,d) rc_avpair_add(a, b, c, d) -#define my_rc_read_dictionary(a) rc_read_dictionary(a) +# define my_rc_conf_str(a) rc_conf_str(a) +# define my_rc_send_server(a, b) rc_send_server(a, b) +# define my_rc_buildreq(a, b, c, d, e, f) rc_buildreq(a, b, c, d, e, f) +# define my_rc_avpair_add(a, b, c, d) rc_avpair_add(a, b, c, d) +# define my_rc_read_dictionary(a) rc_read_dictionary(a) #endif /* REJECT_RC is only defined in some version of radiusclient. It has * been reported from radiusclient-ng 0.5.6 on FreeBSD 7.2-RELEASE */ #ifndef REJECT_RC -#define REJECT_RC BADRESP_RC +# define REJECT_RC BADRESP_RC #endif static int my_rc_read_config(char * /*a*/); @@ -148,11 +148,7 @@ Please note that all tags must be lowercase to use the DocBook XML DTD. -@@ ******************************************************************************/ - - -int -main (int argc, char **argv) -{ +int main(int argc, char **argv) { struct sockaddr_storage ss; char name[HOST_NAME_MAX]; #ifdef RC_BUFFER_LEN @@ -165,131 +161,130 @@ main (int argc, char **argv) uint32_t client_id, service; char *str; - setlocale (LC_ALL, ""); - bindtextdomain (PACKAGE, LOCALEDIR); - textdomain (PACKAGE); + setlocale(LC_ALL, ""); + bindtextdomain(PACKAGE, LOCALEDIR); + textdomain(PACKAGE); /* Parse extra opts if any */ - argv=np_extra_opts (&argc, argv, progname); + argv = np_extra_opts(&argc, argv, progname); - if (process_arguments (argc, argv) == ERROR) - usage4 (_("Could not parse arguments")); + if (process_arguments(argc, argv) == ERROR) { + usage4(_("Could not parse arguments")); + } - str = strdup ("dictionary"); - if ((config_file && my_rc_read_config (config_file)) || - my_rc_read_dictionary (my_rc_conf_str (str))) - die (STATE_UNKNOWN, _("Config file error\n")); + str = strdup("dictionary"); + if ((config_file && my_rc_read_config(config_file)) || my_rc_read_dictionary(my_rc_conf_str(str))) { + die(STATE_UNKNOWN, _("Config file error\n")); + } service = PW_AUTHENTICATE_ONLY; - memset (&data, 0, sizeof(data)); - if (!(my_rc_avpair_add (&data.send_pairs, PW_SERVICE_TYPE, &service, 0) && - my_rc_avpair_add (&data.send_pairs, PW_USER_NAME, username, 0) && - my_rc_avpair_add (&data.send_pairs, PW_USER_PASSWORD, password, 0) - )) - die (STATE_UNKNOWN, _("Out of Memory?\n")); + memset(&data, 0, sizeof(data)); + if (!(my_rc_avpair_add(&data.send_pairs, PW_SERVICE_TYPE, &service, 0) && + my_rc_avpair_add(&data.send_pairs, PW_USER_NAME, username, 0) && + my_rc_avpair_add(&data.send_pairs, PW_USER_PASSWORD, password, 0))) { + die(STATE_UNKNOWN, _("Out of Memory?\n")); + } if (nasid != NULL) { - if (!(my_rc_avpair_add (&data.send_pairs, PW_NAS_IDENTIFIER, nasid, 0))) - die (STATE_UNKNOWN, _("Invalid NAS-Identifier\n")); + if (!(my_rc_avpair_add(&data.send_pairs, PW_NAS_IDENTIFIER, nasid, 0))) { + die(STATE_UNKNOWN, _("Invalid NAS-Identifier\n")); + } } if (nasipaddress == NULL) { - if (gethostname (name, sizeof(name)) != 0) - die (STATE_UNKNOWN, _("gethostname() failed!\n")); + if (gethostname(name, sizeof(name)) != 0) { + die(STATE_UNKNOWN, _("gethostname() failed!\n")); + } nasipaddress = name; } - if (!dns_lookup (nasipaddress, &ss, AF_INET)) /* TODO: Support IPv6. */ - die (STATE_UNKNOWN, _("Invalid NAS-IP-Address\n")); - client_id = ntohl (((struct sockaddr_in *)&ss)->sin_addr.s_addr); - if (my_rc_avpair_add (&(data.send_pairs), PW_NAS_IP_ADDRESS, &client_id, 0) == NULL) - die (STATE_UNKNOWN, _("Invalid NAS-IP-Address\n")); - - my_rc_buildreq (&data, PW_ACCESS_REQUEST, server, port, (int)timeout_interval, - retries); - - result = my_rc_send_server (&data, msg); - rc_avpair_free (data.send_pairs); - if (data.receive_pairs) - rc_avpair_free (data.receive_pairs); - - if (result == TIMEOUT_RC) - die (STATE_CRITICAL, _("Timeout\n")); - if (result == ERROR_RC) - die (STATE_CRITICAL, _("Auth Error\n")); - if (result == REJECT_RC) - die (STATE_WARNING, _("Auth Failed\n")); - if (result == BADRESP_RC) - die (STATE_WARNING, _("Bad Response\n")); - if (expect && !strstr (msg, expect)) - die (STATE_WARNING, "%s\n", msg); - if (result == OK_RC) - die (STATE_OK, _("Auth OK\n")); - (void)snprintf(msg, sizeof(msg), _("Unexpected result code %d"), result); - die (STATE_UNKNOWN, "%s\n", msg); -} + if (!dns_lookup(nasipaddress, &ss, AF_INET)) { /* TODO: Support IPv6. */ + die(STATE_UNKNOWN, _("Invalid NAS-IP-Address\n")); + } + client_id = ntohl(((struct sockaddr_in *)&ss)->sin_addr.s_addr); + if (my_rc_avpair_add(&(data.send_pairs), PW_NAS_IP_ADDRESS, &client_id, 0) == NULL) { + die(STATE_UNKNOWN, _("Invalid NAS-IP-Address\n")); + } + + my_rc_buildreq(&data, PW_ACCESS_REQUEST, server, port, (int)timeout_interval, retries); + result = my_rc_send_server(&data, msg); + rc_avpair_free(data.send_pairs); + if (data.receive_pairs) { + rc_avpair_free(data.receive_pairs); + } + if (result == TIMEOUT_RC) { + die(STATE_CRITICAL, _("Timeout\n")); + } + if (result == ERROR_RC) { + die(STATE_CRITICAL, _("Auth Error\n")); + } + if (result == REJECT_RC) { + die(STATE_WARNING, _("Auth Failed\n")); + } + if (result == BADRESP_RC) { + die(STATE_WARNING, _("Bad Response\n")); + } + if (expect && !strstr(msg, expect)) { + die(STATE_WARNING, "%s\n", msg); + } + if (result == OK_RC) { + die(STATE_OK, _("Auth OK\n")); + } + (void)snprintf(msg, sizeof(msg), _("Unexpected result code %d"), result); + die(STATE_UNKNOWN, "%s\n", msg); +} /* process command-line arguments */ -int -process_arguments (int argc, char **argv) -{ +int process_arguments(int argc, char **argv) { int c; int option = 0; - static struct option longopts[] = { - {"hostname", required_argument, 0, 'H'}, - {"port", required_argument, 0, 'P'}, - {"username", required_argument, 0, 'u'}, - {"password", required_argument, 0, 'p'}, - {"nas-id", required_argument, 0, 'n'}, - {"nas-ip-address", required_argument, 0, 'N'}, - {"filename", required_argument, 0, 'F'}, - {"expect", required_argument, 0, 'e'}, - {"retries", required_argument, 0, 'r'}, - {"timeout", required_argument, 0, 't'}, - {"verbose", no_argument, 0, 'v'}, - {"version", no_argument, 0, 'V'}, - {"help", no_argument, 0, 'h'}, - {0, 0, 0, 0} - }; + static struct option longopts[] = {{"hostname", required_argument, 0, 'H'}, {"port", required_argument, 0, 'P'}, + {"username", required_argument, 0, 'u'}, {"password", required_argument, 0, 'p'}, + {"nas-id", required_argument, 0, 'n'}, {"nas-ip-address", required_argument, 0, 'N'}, + {"filename", required_argument, 0, 'F'}, {"expect", required_argument, 0, 'e'}, + {"retries", required_argument, 0, 'r'}, {"timeout", required_argument, 0, 't'}, + {"verbose", no_argument, 0, 'v'}, {"version", no_argument, 0, 'V'}, + {"help", no_argument, 0, 'h'}, {0, 0, 0, 0}}; while (1) { - c = getopt_long (argc, argv, "+hVvH:P:F:u:p:n:N:t:r:e:", longopts, - &option); + c = getopt_long(argc, argv, "+hVvH:P:F:u:p:n:N:t:r:e:", longopts, &option); - if (c == -1 || c == EOF || c == 1) + if (c == -1 || c == EOF || c == 1) { break; + } switch (c) { - case '?': /* print short usage statement if args not parsable */ - usage5 (); - case 'h': /* help */ - print_help (); - exit (STATE_UNKNOWN); - case 'V': /* version */ - print_revision (progname, NP_VERSION); - exit (STATE_UNKNOWN); - case 'v': /* verbose mode */ + case '?': /* print short usage statement if args not parsable */ + usage5(); + case 'h': /* help */ + print_help(); + exit(STATE_UNKNOWN); + case 'V': /* version */ + print_revision(progname, NP_VERSION); + exit(STATE_UNKNOWN); + case 'v': /* verbose mode */ verbose = true; break; - case 'H': /* hostname */ - if (!is_host (optarg)) { - usage2 (_("Invalid hostname/address"), optarg); + case 'H': /* hostname */ + if (!is_host(optarg)) { + usage2(_("Invalid hostname/address"), optarg); } server = optarg; break; - case 'P': /* port */ - if (is_intnonneg (optarg)) - port = (unsigned short)atoi (optarg); - else - usage4 (_("Port must be a positive integer")); + case 'P': /* port */ + if (is_intnonneg(optarg)) { + port = (unsigned short)atoi(optarg); + } else { + usage4(_("Port must be a positive integer")); + } break; - case 'u': /* username */ + case 'u': /* username */ username = optarg; break; - case 'p': /* password */ + case 'p': /* password */ password = strdup(optarg); /* Delete the password from process list */ @@ -298,115 +293,111 @@ process_arguments (int argc, char **argv) optarg++; } break; - case 'n': /* nas id */ + case 'n': /* nas id */ nasid = optarg; break; - case 'N': /* nas ip address */ + case 'N': /* nas ip address */ nasipaddress = optarg; break; - case 'F': /* configuration file */ + case 'F': /* configuration file */ config_file = optarg; break; - case 'e': /* expect */ + case 'e': /* expect */ expect = optarg; break; - case 'r': /* retries */ - if (is_intpos (optarg)) - retries = atoi (optarg); - else - usage4 (_("Number of retries must be a positive integer")); + case 'r': /* retries */ + if (is_intpos(optarg)) { + retries = atoi(optarg); + } else { + usage4(_("Number of retries must be a positive integer")); + } break; - case 't': /* timeout */ - if (is_intpos (optarg)) - timeout_interval = (unsigned)atoi (optarg); - else - usage2 (_("Timeout interval must be a positive integer"), optarg); + case 't': /* timeout */ + if (is_intpos(optarg)) { + timeout_interval = (unsigned)atoi(optarg); + } else { + usage2(_("Timeout interval must be a positive integer"), optarg); + } break; } } - if (server == NULL) - usage4 (_("Hostname was not supplied")); - if (username == NULL) - usage4 (_("User not specified")); - if (password == NULL) - usage4 (_("Password not specified")); - if (config_file == NULL) - usage4 (_("Configuration file not specified")); + if (server == NULL) { + usage4(_("Hostname was not supplied")); + } + if (username == NULL) { + usage4(_("User not specified")); + } + if (password == NULL) { + usage4(_("Password not specified")); + } + if (config_file == NULL) { + usage4(_("Configuration file not specified")); + } return OK; } - - -void -print_help (void) -{ +void print_help(void) { char *myport; - xasprintf (&myport, "%d", PW_AUTH_UDP_PORT); + xasprintf(&myport, "%d", PW_AUTH_UDP_PORT); - print_revision (progname, NP_VERSION); + print_revision(progname, NP_VERSION); - printf ("Copyright (c) 1999 Robert August Vincent II\n"); - printf (COPYRIGHT, copyright, email); + printf("Copyright (c) 1999 Robert August Vincent II\n"); + printf(COPYRIGHT, copyright, email); printf("%s\n", _("Tests to see if a RADIUS server is accepting connections.")); - printf ("\n\n"); - - print_usage (); - - printf (UT_HELP_VRSN); - printf (UT_EXTRA_OPTS); - - printf (UT_HOST_PORT, 'P', myport); - - printf (" %s\n", "-u, --username=STRING"); - printf (" %s\n", _("The user to authenticate")); - printf (" %s\n", "-p, --password=STRING"); - printf (" %s\n", _("Password for authentication (SECURITY RISK)")); - printf (" %s\n", "-n, --nas-id=STRING"); - printf (" %s\n", _("NAS identifier")); - printf (" %s\n", "-N, --nas-ip-address=STRING"); - printf (" %s\n", _("NAS IP Address")); - printf (" %s\n", "-F, --filename=STRING"); - printf (" %s\n", _("Configuration file")); - printf (" %s\n", "-e, --expect=STRING"); - printf (" %s\n", _("Response string to expect from the server")); - printf (" %s\n", "-r, --retries=INTEGER"); - printf (" %s\n", _("Number of times to retry a failed connection")); - - printf (UT_CONN_TIMEOUT, timeout_interval); - - printf ("\n"); - printf ("%s\n", _("This plugin tests a RADIUS server to see if it is accepting connections.")); - printf ("%s\n", _("The server to test must be specified in the invocation, as well as a user")); - printf ("%s\n", _("name and password. A configuration file must be present. The format of")); - printf ("%s\n", _("the configuration file is described in the radiusclient library sources.")); - printf ("%s\n", _("The password option presents a substantial security issue because the")); - printf ("%s\n", _("password can possibly be determined by careful watching of the command line")); - printf ("%s\n", _("in a process listing. This risk is exacerbated because the plugin will")); - printf ("%s\n", _("typically be executed at regular predictable intervals. Please be sure that")); - printf ("%s\n", _("the password used does not allow access to sensitive system resources.")); - - printf (UT_SUPPORT); + printf("\n\n"); + + print_usage(); + + printf(UT_HELP_VRSN); + printf(UT_EXTRA_OPTS); + + printf(UT_HOST_PORT, 'P', myport); + + printf(" %s\n", "-u, --username=STRING"); + printf(" %s\n", _("The user to authenticate")); + printf(" %s\n", "-p, --password=STRING"); + printf(" %s\n", _("Password for authentication (SECURITY RISK)")); + printf(" %s\n", "-n, --nas-id=STRING"); + printf(" %s\n", _("NAS identifier")); + printf(" %s\n", "-N, --nas-ip-address=STRING"); + printf(" %s\n", _("NAS IP Address")); + printf(" %s\n", "-F, --filename=STRING"); + printf(" %s\n", _("Configuration file")); + printf(" %s\n", "-e, --expect=STRING"); + printf(" %s\n", _("Response string to expect from the server")); + printf(" %s\n", "-r, --retries=INTEGER"); + printf(" %s\n", _("Number of times to retry a failed connection")); + + printf(UT_CONN_TIMEOUT, timeout_interval); + + printf("\n"); + printf("%s\n", _("This plugin tests a RADIUS server to see if it is accepting connections.")); + printf("%s\n", _("The server to test must be specified in the invocation, as well as a user")); + printf("%s\n", _("name and password. A configuration file must be present. The format of")); + printf("%s\n", _("the configuration file is described in the radiusclient library sources.")); + printf("%s\n", _("The password option presents a substantial security issue because the")); + printf("%s\n", _("password can possibly be determined by careful watching of the command line")); + printf("%s\n", _("in a process listing. This risk is exacerbated because the plugin will")); + printf("%s\n", _("typically be executed at regular predictable intervals. Please be sure that")); + printf("%s\n", _("the password used does not allow access to sensitive system resources.")); + + printf(UT_SUPPORT); } - - -void -print_usage (void) -{ - printf ("%s\n", _("Usage:")); - printf ("%s -H host -F config_file -u username -p password\n\ +void print_usage(void) { + printf("%s\n", _("Usage:")); + printf("%s -H host -F config_file -u username -p password\n\ [-P port] [-t timeout] [-r retries] [-e expect]\n\ - [-n nas-id] [-N nas-ip-addr]\n", progname); + [-n nas-id] [-N nas-ip-addr]\n", + progname); } - - -int my_rc_read_config(char * a) -{ +int my_rc_read_config(char *a) { #if defined(HAVE_LIBFREERADIUS_CLIENT) || defined(HAVE_LIBRADIUSCLIENT_NG) || defined(HAVE_LIBRADCLI) rch = rc_read_config(a); return (rch == NULL) ? 1 : 0; -- cgit v1.2.3-74-g34f1 From 0728456d73d4b3c62a2f26f4c918de571b4fbc3d Mon Sep 17 00:00:00 2001 From: Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> Date: Wed, 12 Mar 2025 16:29:53 +0100 Subject: check_radius: general refactoring --- plugins/check_radius.c | 44 +++++++++++++++++++++----------------------- 1 file changed, 21 insertions(+), 23 deletions(-) (limited to 'plugins') diff --git a/plugins/check_radius.c b/plugins/check_radius.c index 646ffcbe..ab7c6519 100644 --- a/plugins/check_radius.c +++ b/plugins/check_radius.c @@ -35,6 +35,7 @@ const char *email = "devel@monitoring-plugins.org"; #include "common.h" #include "utils.h" #include "netutils.h" +#include "states.h" #if defined(HAVE_LIBRADCLI) # include @@ -149,18 +150,6 @@ Please note that all tags must be lowercase to use the DocBook XML DTD. ******************************************************************************/ int main(int argc, char **argv) { - struct sockaddr_storage ss; - char name[HOST_NAME_MAX]; -#ifdef RC_BUFFER_LEN - char msg[RC_BUFFER_LEN]; -#else - char msg[BUFFER_LEN]; -#endif - SEND_DATA data; - int result = STATE_UNKNOWN; - uint32_t client_id, service; - char *str; - setlocale(LC_ALL, ""); bindtextdomain(PACKAGE, LOCALEDIR); textdomain(PACKAGE); @@ -172,13 +161,14 @@ int main(int argc, char **argv) { usage4(_("Could not parse arguments")); } - str = strdup("dictionary"); + char *str = strdup("dictionary"); if ((config_file && my_rc_read_config(config_file)) || my_rc_read_dictionary(my_rc_conf_str(str))) { die(STATE_UNKNOWN, _("Config file error\n")); } - service = PW_AUTHENTICATE_ONLY; + uint32_t service = PW_AUTHENTICATE_ONLY; + SEND_DATA data; memset(&data, 0, sizeof(data)); if (!(my_rc_avpair_add(&data.send_pairs, PW_SERVICE_TYPE, &service, 0) && my_rc_avpair_add(&data.send_pairs, PW_USER_NAME, username, 0) && @@ -192,23 +182,33 @@ int main(int argc, char **argv) { } } + char name[HOST_NAME_MAX]; if (nasipaddress == NULL) { if (gethostname(name, sizeof(name)) != 0) { die(STATE_UNKNOWN, _("gethostname() failed!\n")); } nasipaddress = name; } + + struct sockaddr_storage ss; if (!dns_lookup(nasipaddress, &ss, AF_INET)) { /* TODO: Support IPv6. */ die(STATE_UNKNOWN, _("Invalid NAS-IP-Address\n")); } - client_id = ntohl(((struct sockaddr_in *)&ss)->sin_addr.s_addr); + + uint32_t client_id = ntohl(((struct sockaddr_in *)&ss)->sin_addr.s_addr); if (my_rc_avpair_add(&(data.send_pairs), PW_NAS_IP_ADDRESS, &client_id, 0) == NULL) { die(STATE_UNKNOWN, _("Invalid NAS-IP-Address\n")); } my_rc_buildreq(&data, PW_ACCESS_REQUEST, server, port, (int)timeout_interval, retries); - result = my_rc_send_server(&data, msg); +#ifdef RC_BUFFER_LEN + char msg[RC_BUFFER_LEN]; +#else + char msg[BUFFER_LEN]; +#endif + + mp_state_enum result = my_rc_send_server(&data, msg); rc_avpair_free(data.send_pairs); if (data.receive_pairs) { rc_avpair_free(data.receive_pairs); @@ -238,9 +238,6 @@ int main(int argc, char **argv) { /* process command-line arguments */ int process_arguments(int argc, char **argv) { - int c; - - int option = 0; static struct option longopts[] = {{"hostname", required_argument, 0, 'H'}, {"port", required_argument, 0, 'P'}, {"username", required_argument, 0, 'u'}, {"password", required_argument, 0, 'p'}, {"nas-id", required_argument, 0, 'n'}, {"nas-ip-address", required_argument, 0, 'N'}, @@ -249,14 +246,15 @@ int process_arguments(int argc, char **argv) { {"verbose", no_argument, 0, 'v'}, {"version", no_argument, 0, 'V'}, {"help", no_argument, 0, 'h'}, {0, 0, 0, 0}}; - while (1) { - c = getopt_long(argc, argv, "+hVvH:P:F:u:p:n:N:t:r:e:", longopts, &option); + while (true) { + int option = 0; + int option_index = getopt_long(argc, argv, "+hVvH:P:F:u:p:n:N:t:r:e:", longopts, &option); - if (c == -1 || c == EOF || c == 1) { + if (option_index == -1 || option_index == EOF || option_index == 1) { break; } - switch (c) { + switch (option_index) { case '?': /* print short usage statement if args not parsable */ usage5(); case 'h': /* help */ -- cgit v1.2.3-74-g34f1 From 3008d521c1284f6f182d9cc10d56afdaa3978a04 Mon Sep 17 00:00:00 2001 From: Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> Date: Wed, 12 Mar 2025 16:51:02 +0100 Subject: Refactor check_radius --- plugins/Makefile.am | 1 + plugins/check_radius.c | 126 +++++++++++++++++++++++----------------- plugins/check_radius.d/config.h | 42 ++++++++++++++ 3 files changed, 115 insertions(+), 54 deletions(-) create mode 100644 plugins/check_radius.d/config.h (limited to 'plugins') diff --git a/plugins/Makefile.am b/plugins/Makefile.am index 6c582a15..dacd36e8 100644 --- a/plugins/Makefile.am +++ b/plugins/Makefile.am @@ -53,6 +53,7 @@ EXTRA_DIST = t \ check_ldap.d \ check_hpjd.d \ check_game.d \ + check_radius.d \ check_nagios.d \ check_dbi.d \ check_ssh.d \ diff --git a/plugins/check_radius.c b/plugins/check_radius.c index ab7c6519..6a27e113 100644 --- a/plugins/check_radius.c +++ b/plugins/check_radius.c @@ -36,6 +36,7 @@ const char *email = "devel@monitoring-plugins.org"; #include "utils.h" #include "netutils.h" #include "states.h" +#include "check_radius.d/config.h" #if defined(HAVE_LIBRADCLI) # include @@ -47,7 +48,11 @@ const char *email = "devel@monitoring-plugins.org"; # include #endif -static int process_arguments(int /*argc*/, char ** /*argv*/); +typedef struct { + int errorcode; + check_radius_config config; +} check_radius_config_wrapper; +static check_radius_config_wrapper process_arguments(int /*argc*/, char ** /*argv*/); static void print_help(void); void print_usage(void); @@ -79,21 +84,8 @@ void print_usage(void); # define REJECT_RC BADRESP_RC #endif -static int my_rc_read_config(char * /*a*/); +static int my_rc_read_config(char * /*a*/, rc_handle ** /*rch*/); -#if defined(HAVE_LIBFREERADIUS_CLIENT) || defined(HAVE_LIBRADIUSCLIENT_NG) || defined(HAVE_LIBRADCLI) -static rc_handle *rch = NULL; -#endif - -static char *server = NULL; -static char *username = NULL; -static char *password = NULL; -static char *nasid = NULL; -static char *nasipaddress = NULL; -static char *expect = NULL; -static char *config_file = NULL; -static unsigned short port = PW_AUTH_UDP_PORT; -static int retries = 1; static bool verbose = false; /****************************************************************************** @@ -157,12 +149,20 @@ int main(int argc, char **argv) { /* Parse extra opts if any */ argv = np_extra_opts(&argc, argv, progname); - if (process_arguments(argc, argv) == ERROR) { + check_radius_config_wrapper tmp_config = process_arguments(argc, argv); + + if (tmp_config.errorcode == ERROR) { usage4(_("Could not parse arguments")); } + check_radius_config config = tmp_config.config; + +#if defined(HAVE_LIBFREERADIUS_CLIENT) || defined(HAVE_LIBRADIUSCLIENT_NG) || defined(HAVE_LIBRADCLI) + rc_handle *rch = NULL; +#endif + char *str = strdup("dictionary"); - if ((config_file && my_rc_read_config(config_file)) || my_rc_read_dictionary(my_rc_conf_str(str))) { + if ((config.config_file && my_rc_read_config(config.config_file, &rch)) || my_rc_read_dictionary(my_rc_conf_str(str))) { die(STATE_UNKNOWN, _("Config file error\n")); } @@ -171,36 +171,36 @@ int main(int argc, char **argv) { SEND_DATA data; memset(&data, 0, sizeof(data)); if (!(my_rc_avpair_add(&data.send_pairs, PW_SERVICE_TYPE, &service, 0) && - my_rc_avpair_add(&data.send_pairs, PW_USER_NAME, username, 0) && - my_rc_avpair_add(&data.send_pairs, PW_USER_PASSWORD, password, 0))) { + my_rc_avpair_add(&data.send_pairs, PW_USER_NAME, config.username, 0) && + my_rc_avpair_add(&data.send_pairs, PW_USER_PASSWORD, config.password, 0))) { die(STATE_UNKNOWN, _("Out of Memory?\n")); } - if (nasid != NULL) { - if (!(my_rc_avpair_add(&data.send_pairs, PW_NAS_IDENTIFIER, nasid, 0))) { + if (config.nas_id != NULL) { + if (!(my_rc_avpair_add(&data.send_pairs, PW_NAS_IDENTIFIER, config.nas_id, 0))) { die(STATE_UNKNOWN, _("Invalid NAS-Identifier\n")); } } char name[HOST_NAME_MAX]; - if (nasipaddress == NULL) { + if (config.nas_ip_address == NULL) { if (gethostname(name, sizeof(name)) != 0) { die(STATE_UNKNOWN, _("gethostname() failed!\n")); } - nasipaddress = name; + config.nas_ip_address = name; } - struct sockaddr_storage ss; - if (!dns_lookup(nasipaddress, &ss, AF_INET)) { /* TODO: Support IPv6. */ + struct sockaddr_storage radius_server_socket; + if (!dns_lookup(config.nas_ip_address, &radius_server_socket, AF_INET)) { /* TODO: Support IPv6. */ die(STATE_UNKNOWN, _("Invalid NAS-IP-Address\n")); } - uint32_t client_id = ntohl(((struct sockaddr_in *)&ss)->sin_addr.s_addr); + uint32_t client_id = ntohl(((struct sockaddr_in *)&radius_server_socket)->sin_addr.s_addr); if (my_rc_avpair_add(&(data.send_pairs), PW_NAS_IP_ADDRESS, &client_id, 0) == NULL) { die(STATE_UNKNOWN, _("Invalid NAS-IP-Address\n")); } - my_rc_buildreq(&data, PW_ACCESS_REQUEST, server, port, (int)timeout_interval, retries); + my_rc_buildreq(&data, PW_ACCESS_REQUEST, config.server, config.port, (int)timeout_interval, config.retries); #ifdef RC_BUFFER_LEN char msg[RC_BUFFER_LEN]; @@ -208,36 +208,49 @@ int main(int argc, char **argv) { char msg[BUFFER_LEN]; #endif - mp_state_enum result = my_rc_send_server(&data, msg); + int result = my_rc_send_server(&data, msg); rc_avpair_free(data.send_pairs); if (data.receive_pairs) { rc_avpair_free(data.receive_pairs); } if (result == TIMEOUT_RC) { - die(STATE_CRITICAL, _("Timeout\n")); + printf("Timeout\n"); + exit(STATE_CRITICAL); } + if (result == ERROR_RC) { - die(STATE_CRITICAL, _("Auth Error\n")); + printf(_("Auth Error\n")); + exit(STATE_CRITICAL); } + if (result == REJECT_RC) { - die(STATE_WARNING, _("Auth Failed\n")); + printf(_("Auth Failed\n")); + exit(STATE_WARNING); } + if (result == BADRESP_RC) { - die(STATE_WARNING, _("Bad Response\n")); + printf(_("Bad Response\n")); + exit(STATE_WARNING); } - if (expect && !strstr(msg, expect)) { - die(STATE_WARNING, "%s\n", msg); + + if (config.expect && !strstr(msg, config.expect)) { + printf("%s\n", msg); + exit(STATE_WARNING); } + if (result == OK_RC) { - die(STATE_OK, _("Auth OK\n")); + printf(_("Auth OK\n")); + exit(STATE_OK); } + (void)snprintf(msg, sizeof(msg), _("Unexpected result code %d"), result); - die(STATE_UNKNOWN, "%s\n", msg); + printf("%s\n", msg); + exit(STATE_UNKNOWN); } /* process command-line arguments */ -int process_arguments(int argc, char **argv) { +check_radius_config_wrapper process_arguments(int argc, char **argv) { static struct option longopts[] = {{"hostname", required_argument, 0, 'H'}, {"port", required_argument, 0, 'P'}, {"username", required_argument, 0, 'u'}, {"password", required_argument, 0, 'p'}, {"nas-id", required_argument, 0, 'n'}, {"nas-ip-address", required_argument, 0, 'N'}, @@ -246,6 +259,11 @@ int process_arguments(int argc, char **argv) { {"verbose", no_argument, 0, 'v'}, {"version", no_argument, 0, 'V'}, {"help", no_argument, 0, 'h'}, {0, 0, 0, 0}}; + check_radius_config_wrapper result = { + .errorcode = OK, + .config = check_radius_config_init(), + }; + while (true) { int option = 0; int option_index = getopt_long(argc, argv, "+hVvH:P:F:u:p:n:N:t:r:e:", longopts, &option); @@ -270,20 +288,20 @@ int process_arguments(int argc, char **argv) { if (!is_host(optarg)) { usage2(_("Invalid hostname/address"), optarg); } - server = optarg; + result.config.server = optarg; break; case 'P': /* port */ if (is_intnonneg(optarg)) { - port = (unsigned short)atoi(optarg); + result.config.port = (unsigned short)atoi(optarg); } else { usage4(_("Port must be a positive integer")); } break; case 'u': /* username */ - username = optarg; + result.config.username = optarg; break; case 'p': /* password */ - password = strdup(optarg); + result.config.password = strdup(optarg); /* Delete the password from process list */ while (*optarg != '\0') { @@ -292,20 +310,20 @@ int process_arguments(int argc, char **argv) { } break; case 'n': /* nas id */ - nasid = optarg; + result.config.nas_id = optarg; break; case 'N': /* nas ip address */ - nasipaddress = optarg; + result.config.nas_ip_address = optarg; break; case 'F': /* configuration file */ - config_file = optarg; + result.config.config_file = optarg; break; case 'e': /* expect */ - expect = optarg; + result.config.expect = optarg; break; case 'r': /* retries */ if (is_intpos(optarg)) { - retries = atoi(optarg); + result.config.retries = atoi(optarg); } else { usage4(_("Number of retries must be a positive integer")); } @@ -320,20 +338,20 @@ int process_arguments(int argc, char **argv) { } } - if (server == NULL) { + if (result.config.server == NULL) { usage4(_("Hostname was not supplied")); } - if (username == NULL) { + if (result.config.username == NULL) { usage4(_("User not specified")); } - if (password == NULL) { + if (result.config.password == NULL) { usage4(_("Password not specified")); } - if (config_file == NULL) { + if (result.config.config_file == NULL) { usage4(_("Configuration file not specified")); } - return OK; + return result; } void print_help(void) { @@ -395,11 +413,11 @@ void print_usage(void) { progname); } -int my_rc_read_config(char *a) { +int my_rc_read_config(char *config_file_name, rc_handle **rch) { #if defined(HAVE_LIBFREERADIUS_CLIENT) || defined(HAVE_LIBRADIUSCLIENT_NG) || defined(HAVE_LIBRADCLI) - rch = rc_read_config(a); + *rch = rc_read_config(config_file_name); return (rch == NULL) ? 1 : 0; #else - return rc_read_config(a); + return rc_read_config(config_file_name); #endif } diff --git a/plugins/check_radius.d/config.h b/plugins/check_radius.d/config.h new file mode 100644 index 00000000..b27d31e7 --- /dev/null +++ b/plugins/check_radius.d/config.h @@ -0,0 +1,42 @@ +#pragma once + +#include "../../config.h" +#include +#if defined(HAVE_LIBRADCLI) +# include +#elif defined(HAVE_LIBFREERADIUS_CLIENT) +# include +#elif defined(HAVE_LIBRADIUSCLIENT_NG) +# include +#else +# include +#endif + +typedef struct { + char *server; + char *username; + char *password; + char *config_file; + char *nas_id; + char *nas_ip_address; + int retries; + unsigned short port; + + char *expect; +} check_radius_config; + +check_radius_config check_radius_config_init() { + check_radius_config tmp = { + .server = NULL, + .username = NULL, + .password = NULL, + .config_file = NULL, + .nas_id = NULL, + .nas_ip_address = NULL, + .retries = 1, + .port = PW_AUTH_UDP_PORT, + + .expect = NULL, + }; + return tmp; +} -- cgit v1.2.3-74-g34f1 From c4e2cf51cba4d62add8e689e621d979aa65278cd Mon Sep 17 00:00:00 2001 From: Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> Date: Wed, 12 Mar 2025 16:52:51 +0100 Subject: check_radius: allow IPv6 addresses --- plugins/check_radius.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'plugins') diff --git a/plugins/check_radius.c b/plugins/check_radius.c index 6a27e113..cc846709 100644 --- a/plugins/check_radius.c +++ b/plugins/check_radius.c @@ -191,7 +191,7 @@ int main(int argc, char **argv) { } struct sockaddr_storage radius_server_socket; - if (!dns_lookup(config.nas_ip_address, &radius_server_socket, AF_INET)) { /* TODO: Support IPv6. */ + if (!dns_lookup(config.nas_ip_address, &radius_server_socket, AF_UNSPEC)) { die(STATE_UNKNOWN, _("Invalid NAS-IP-Address\n")); } -- cgit v1.2.3-74-g34f1