pkg-monitoring-plugins/plugins/check_radius.c

417 lines
12 KiB
C
Raw Normal View History

2013-11-26 22:55:28 +00:00
/*****************************************************************************
*
2014-07-11 19:01:00 +00:00
* Monitoring check_radius plugin
2013-11-26 22:55:28 +00:00
*
2013-11-26 22:53:19 +00:00
* License: GPL
2014-07-11 19:01:00 +00:00
* Copyright (c) 1999-2008 Monitoring Plugins Development Team
2013-11-26 22:55:28 +00:00
*
2013-11-26 22:53:19 +00:00
* Description:
2013-11-26 22:55:28 +00:00
*
2013-11-26 22:53:19 +00:00
* This file contains the check_radius plugin
2013-11-26 22:55:28 +00:00
*
* Tests to see if a radius server is accepting connections.
*
*
* This program is free software: you can redistribute it and/or modify
2013-11-26 22:53:19 +00:00
* it under the terms of the GNU General Public License as published by
2013-11-26 22:55:28 +00:00
* the Free Software Foundation, either version 3 of the License, or
2013-11-26 22:53:19 +00:00
* (at your option) any later version.
2013-11-26 22:55:28 +00:00
*
2013-11-26 22:53:19 +00:00
* 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.
2013-11-26 22:55:28 +00:00
*
2013-11-26 22:53:19 +00:00
* You should have received a copy of the GNU General Public License
2013-11-26 22:55:28 +00:00
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
2013-11-26 22:53:19 +00:00
*
2013-11-26 22:55:28 +00:00
*****************************************************************************/
2013-11-26 22:53:19 +00:00
const char *progname = "check_radius";
2013-11-26 22:55:28 +00:00
const char *copyright = "2000-2008";
2014-07-11 19:01:00 +00:00
const char *email = "devel@monitoring-plugins.org";
2013-11-26 22:53:19 +00:00
#include "common.h"
#include "utils.h"
#include "netutils.h"
2016-11-30 11:36:55 +00:00
#if defined(HAVE_LIBRADCLI)
#include <radcli/radcli.h>
#elif defined(HAVE_LIBFREERADIUS_CLIENT)
2014-07-11 19:01:00 +00:00
#include <freeradius-client.h>
#elif defined(HAVE_LIBRADIUSCLIENT_NG)
2013-11-26 22:54:42 +00:00
#include <radiusclient-ng.h>
#else
2013-11-26 22:53:19 +00:00
#include <radiusclient.h>
2013-11-26 22:54:42 +00:00
#endif
2013-11-26 22:53:19 +00:00
int process_arguments (int, char **);
void print_help (void);
void print_usage (void);
2016-11-30 11:36:55 +00:00
#if defined(HAVE_LIBFREERADIUS_CLIENT) || defined(HAVE_LIBRADIUSCLIENT_NG) || defined(HAVE_LIBRADCLI)
2013-11-26 22:54:42 +00:00
#define my_rc_conf_str(a) rc_conf_str(rch,a)
2016-11-30 11:36:55 +00:00
#if defined(HAVE_LIBRADCLI)
#define my_rc_send_server(a,b) rc_send_server(rch,a,b,AUTH)
#else
2013-11-26 22:54:42 +00:00
#define my_rc_send_server(a,b) rc_send_server(rch,a,b)
2016-11-30 11:36:55 +00:00
#endif
#if defined(HAVE_LIBFREERADIUS_CLIENT) || defined(HAVE_LIBRADCLI)
2014-07-11 19:01:00 +00:00
#define my_rc_buildreq(a,b,c,d,e,f) rc_buildreq(rch,a,b,c,d,(a)->secret,e,f)
#else
2013-11-26 22:54:42 +00:00
#define my_rc_buildreq(a,b,c,d,e,f) rc_buildreq(rch,a,b,c,d,e,f)
2014-07-11 19:01:00 +00:00
#endif
2013-11-26 22:54:42 +00:00
#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)
#endif
2013-11-26 22:57:29 +00:00
/* 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
#endif
2013-11-26 22:54:42 +00:00
int my_rc_read_config(char *);
2016-11-30 11:36:55 +00:00
#if defined(HAVE_LIBFREERADIUS_CLIENT) || defined(HAVE_LIBRADIUSCLIENT_NG) || defined(HAVE_LIBRADCLI)
2014-07-11 19:01:00 +00:00
rc_handle *rch = NULL;
#endif
2013-11-26 22:53:19 +00:00
char *server = NULL;
char *username = NULL;
char *password = NULL;
char *nasid = NULL;
2013-11-26 22:57:29 +00:00
char *nasipaddress = NULL;
2013-11-26 22:53:19 +00:00
char *expect = NULL;
char *config_file = NULL;
unsigned short port = PW_AUTH_UDP_PORT;
int retries = 1;
int verbose = FALSE;
/******************************************************************************
2023-10-18 07:29:37 +00:00
The (pseudo?)literate programming XML is contained within \@\@\- <XML> \-\@\@
2013-11-26 22:53:19 +00:00
tags in the comments. With in the tags, the XML is assembled sequentially.
You can define entities in tags. You also have all the #defines available as
entities.
Please note that all tags must be lowercase to use the DocBook XML DTD.
@@-<article>
<sect1>
<title>Quick Reference</title>
<!-- The refentry forms a manpage -->
<refentry>
<refmeta>
<manvolnum>5<manvolnum>
</refmeta>
<refnamdiv>
<refname>&progname;</refname>
<refpurpose>&SUMMARY;</refpurpose>
</refnamdiv>
</refentry>
</sect1>
<sect1>
<title>FAQ</title>
</sect1>
<sect1>
<title>Theory, Installation, and Operation</title>
<sect2>
<title>General Description</title>
<para>
&DESCRIPTION;
</para>
</sect2>
<sect2>
<title>Future Enhancements</title>
<para>Todo List</para>
<itemizedlist>
<listitem>Add option to get password from a secured file rather than the command line</listitem>
</itemizedlist>
</sect2>
<sect2>
<title>Functions</title>
-@@
******************************************************************************/
int
main (int argc, char **argv)
{
2016-11-30 11:36:55 +00:00
struct sockaddr_storage ss;
char name[HOST_NAME_MAX];
2023-10-18 07:29:37 +00:00
#ifdef RC_BUFFER_LEN
char msg[RC_BUFFER_LEN];
#else
2013-11-26 22:53:19 +00:00
char msg[BUFFER_LEN];
2023-10-18 07:29:37 +00:00
#endif
2013-11-26 22:53:19 +00:00
SEND_DATA data;
int result = STATE_UNKNOWN;
2014-07-11 19:01:00 +00:00
uint32_t client_id, service;
2013-11-26 22:53:19 +00:00
char *str;
setlocale (LC_ALL, "");
bindtextdomain (PACKAGE, LOCALEDIR);
textdomain (PACKAGE);
2013-11-26 22:55:28 +00:00
/* Parse extra opts if any */
argv=np_extra_opts (&argc, argv, progname);
2013-11-26 22:53:19 +00:00
if (process_arguments (argc, argv) == ERROR)
usage4 (_("Could not parse arguments"));
str = strdup ("dictionary");
2013-11-26 22:54:42 +00:00
if ((config_file && my_rc_read_config (config_file)) ||
my_rc_read_dictionary (my_rc_conf_str (str)))
2014-07-11 19:01:00 +00:00
die (STATE_UNKNOWN, _("Config file error\n"));
2013-11-26 22:53:19 +00:00
service = PW_AUTHENTICATE_ONLY;
2013-11-26 22:53:44 +00:00
memset (&data, 0, sizeof(data));
2013-11-26 22:54:42 +00:00
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) &&
2013-11-26 22:57:29 +00:00
my_rc_avpair_add (&data.send_pairs, PW_USER_PASSWORD, password, 0)
))
2014-07-11 19:01:00 +00:00
die (STATE_UNKNOWN, _("Out of Memory?\n"));
2013-11-26 22:53:19 +00:00
2013-11-26 22:57:29 +00:00
if (nasid != NULL) {
if (!(my_rc_avpair_add (&data.send_pairs, PW_NAS_IDENTIFIER, nasid, 0)))
2014-07-11 19:01:00 +00:00
die (STATE_UNKNOWN, _("Invalid NAS-Identifier\n"));
2013-11-26 22:57:29 +00:00
}
2013-11-26 22:53:19 +00:00
2016-11-30 11:36:55 +00:00
if (nasipaddress == NULL) {
if (gethostname (name, sizeof(name)) != 0)
die (STATE_UNKNOWN, _("gethostname() failed!\n"));
nasipaddress = name;
2013-11-26 22:57:29 +00:00
}
2016-11-30 11:36:55 +00:00
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);
2013-11-26 22:57:29 +00:00
if (my_rc_avpair_add (&(data.send_pairs), PW_NAS_IP_ADDRESS, &client_id, 0) == NULL)
2014-07-11 19:01:00 +00:00
die (STATE_UNKNOWN, _("Invalid NAS-IP-Address\n"));
2013-11-26 22:53:19 +00:00
2013-11-26 22:54:42 +00:00
my_rc_buildreq (&data, PW_ACCESS_REQUEST, server, port, (int)timeout_interval,
2013-11-26 22:53:19 +00:00
retries);
2013-11-26 22:54:42 +00:00
result = my_rc_send_server (&data, msg);
2013-11-26 22:53:19 +00:00
rc_avpair_free (data.send_pairs);
if (data.receive_pairs)
rc_avpair_free (data.receive_pairs);
if (result == TIMEOUT_RC)
2014-07-11 19:01:00 +00:00
die (STATE_CRITICAL, _("Timeout\n"));
2013-11-26 22:53:19 +00:00
if (result == ERROR_RC)
2014-07-11 19:01:00 +00:00
die (STATE_CRITICAL, _("Auth Error\n"));
2013-11-26 22:57:29 +00:00
if (result == REJECT_RC)
2014-07-11 19:01:00 +00:00
die (STATE_WARNING, _("Auth Failed\n"));
2013-11-26 22:57:29 +00:00
if (result == BADRESP_RC)
2014-07-11 19:01:00 +00:00
die (STATE_WARNING, _("Bad Response\n"));
2013-11-26 22:53:19 +00:00
if (expect && !strstr (msg, expect))
2014-07-11 19:01:00 +00:00
die (STATE_WARNING, "%s\n", msg);
2013-11-26 22:53:19 +00:00
if (result == OK_RC)
2014-07-11 19:01:00 +00:00
die (STATE_OK, _("Auth OK\n"));
2013-11-26 22:57:29 +00:00
(void)snprintf(msg, sizeof(msg), _("Unexpected result code %d"), result);
2014-07-11 19:01:00 +00:00
die (STATE_UNKNOWN, "%s\n", msg);
2013-11-26 22:53:19 +00:00
}
/* 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'},
2013-11-26 22:57:29 +00:00
{"nas-ip-address", required_argument, 0, 'N'},
2013-11-26 22:53:19 +00:00
{"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) {
2013-11-26 22:57:29 +00:00
c = getopt_long (argc, argv, "+hVvH:P:F:u:p:n:N:t:r:e:", longopts,
2013-11-26 22:53:19 +00:00
&option);
if (c == -1 || c == EOF || c == 1)
break;
switch (c) {
case '?': /* print short usage statement if args not parsable */
2013-11-26 22:53:44 +00:00
usage5 ();
2013-11-26 22:53:19 +00:00
case 'h': /* help */
print_help ();
2016-11-30 11:36:55 +00:00
exit (STATE_UNKNOWN);
2013-11-26 22:53:19 +00:00
case 'V': /* version */
print_revision (progname, NP_VERSION);
2016-11-30 11:36:55 +00:00
exit (STATE_UNKNOWN);
2013-11-26 22:53:19 +00:00
case 'v': /* verbose mode */
verbose = TRUE;
break;
case 'H': /* hostname */
if (is_host (optarg) == FALSE) {
usage2 (_("Invalid hostname/address"), optarg);
}
server = optarg;
break;
case 'P': /* port */
if (is_intnonneg (optarg))
2016-11-30 11:36:55 +00:00
port = (unsigned short)atoi (optarg);
2013-11-26 22:53:19 +00:00
else
usage4 (_("Port must be a positive integer"));
break;
case 'u': /* username */
username = optarg;
break;
case 'p': /* password */
2013-11-26 22:55:28 +00:00
password = strdup(optarg);
/* Delete the password from process list */
while (*optarg != '\0') {
*optarg = 'X';
optarg++;
}
2013-11-26 22:53:19 +00:00
break;
case 'n': /* nas id */
nasid = optarg;
break;
2013-11-26 22:57:29 +00:00
case 'N': /* nas ip address */
nasipaddress = optarg;
break;
2013-11-26 22:53:19 +00:00
case 'F': /* configuration file */
config_file = optarg;
break;
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"));
break;
case 't': /* timeout */
if (is_intpos (optarg))
2016-11-30 11:36:55 +00:00
timeout_interval = (unsigned)atoi (optarg);
2013-11-26 22:53:19 +00:00
else
usage2 (_("Timeout interval must be a positive integer"), optarg);
break;
}
}
2013-11-26 22:54:42 +00:00
if (server == NULL)
2013-11-26 22:54:57 +00:00
usage4 (_("Hostname was not supplied"));
2013-11-26 22:54:42 +00:00
if (username == NULL)
usage4 (_("User not specified"));
if (password == NULL)
usage4 (_("Password not specified"));
if (config_file == NULL)
usage4 (_("Configuration file not specified"));
2013-11-26 22:53:19 +00:00
return OK;
}
void
print_help (void)
{
char *myport;
xasprintf (&myport, "%d", PW_AUTH_UDP_PORT);
2013-11-26 22:53:19 +00:00
print_revision (progname, NP_VERSION);
2013-11-26 22:53:19 +00:00
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."));
2013-11-26 22:53:19 +00:00
printf ("\n\n");
print_usage ();
2013-11-26 22:57:29 +00:00
printf (UT_HELP_VRSN);
printf (UT_EXTRA_OPTS);
2013-11-26 22:53:19 +00:00
2013-11-26 22:57:29 +00:00
printf (UT_HOST_PORT, 'P', myport);
2013-11-26 22:53:19 +00:00
printf (" %s\n", "-u, --username=STRING");
printf (" %s\n", _("The user to authenticate"));
printf (" %s\n", "-p, --password=STRING");
2020-12-10 20:00:09 +00:00
printf (" %s\n", _("Password for authentication (SECURITY RISK)"));
2013-11-26 22:53:19 +00:00
printf (" %s\n", "-n, --nas-id=STRING");
printf (" %s\n", _("NAS identifier"));
2013-11-26 22:57:29 +00:00
printf (" %s\n", "-N, --nas-ip-address=STRING");
printf (" %s\n", _("NAS IP Address"));
2013-11-26 22:53:19 +00:00
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"));
2014-07-11 19:01:00 +00:00
printf (UT_CONN_TIMEOUT, timeout_interval);
2013-11-26 22:53:19 +00:00
2013-11-26 22:55:28 +00:00
printf ("\n");
printf ("%s\n", _("This plugin tests a RADIUS server to see if it is accepting connections."));
2013-11-26 22:53:19 +00:00
printf ("%s\n", _("The server to test must be specified in the invocation, as well as a user"));
2023-10-18 07:29:37 +00:00
printf ("%s\n", _("name and password. A configuration file must be present. The format of"));
2013-11-26 22:53:19 +00:00
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"));
2013-11-26 22:55:28 +00:00
printf ("%s\n", _("password can possibly be determined by careful watching of the command line"));
2014-07-11 19:01:00 +00:00
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"));
2013-11-26 22:55:28 +00:00
printf ("%s\n", _("the password used does not allow access to sensitive system resources."));
2013-11-26 22:57:29 +00:00
printf (UT_SUPPORT);
2013-11-26 22:53:19 +00:00
}
void
print_usage (void)
{
2013-11-26 22:57:29 +00:00
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);
2013-11-26 22:53:19 +00:00
}
2013-11-26 22:54:42 +00:00
int my_rc_read_config(char * a)
{
2016-11-30 11:36:55 +00:00
#if defined(HAVE_LIBFREERADIUS_CLIENT) || defined(HAVE_LIBRADIUSCLIENT_NG) || defined(HAVE_LIBRADCLI)
2013-11-26 22:54:42 +00:00
rch = rc_read_config(a);
return (rch == NULL) ? 1 : 0;
#else
return rc_read_config(a);
#endif
}