Merge pull request #2045 from RincewindsHat/fix/calloc_argument_order
[monitoring-plugins.git] / plugins / check_radius.c
blobd9ff8fa7e5d644d5f9539a2849dc7db11fd9cb05
1 /*****************************************************************************
2 *
3 * Monitoring check_radius plugin
4 *
5 * License: GPL
6 * Copyright (c) 1999-2024 Monitoring Plugins Development Team
7 *
8 * Description:
9 *
10 * This file contains the check_radius plugin
12 * Tests to see if a radius server is accepting connections.
15 * This program is free software: you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation, either version 3 of the License, or
18 * (at your option) any later version.
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with this program. If not, see <http://www.gnu.org/licenses/>.
29 *****************************************************************************/
31 const char *progname = "check_radius";
32 const char *copyright = "2000-2024";
33 const char *email = "devel@monitoring-plugins.org";
35 #include "common.h"
36 #include "utils.h"
37 #include "netutils.h"
39 #if defined(HAVE_LIBRADCLI)
40 #include <radcli/radcli.h>
41 #elif defined(HAVE_LIBFREERADIUS_CLIENT)
42 #include <freeradius-client.h>
43 #elif defined(HAVE_LIBRADIUSCLIENT_NG)
44 #include <radiusclient-ng.h>
45 #else
46 #include <radiusclient.h>
47 #endif
49 static int process_arguments (int /*argc*/, char ** /*argv*/);
50 static void print_help (void);
51 void print_usage (void);
53 #if defined(HAVE_LIBFREERADIUS_CLIENT) || defined(HAVE_LIBRADIUSCLIENT_NG) || defined(HAVE_LIBRADCLI)
54 #define my_rc_conf_str(a) rc_conf_str(rch,a)
55 #if defined(HAVE_LIBRADCLI)
56 #define my_rc_send_server(a,b) rc_send_server(rch,a,b,AUTH)
57 #else
58 #define my_rc_send_server(a,b) rc_send_server(rch,a,b)
59 #endif
60 #if defined(HAVE_LIBFREERADIUS_CLIENT) || defined(HAVE_LIBRADCLI)
61 #define my_rc_buildreq(a,b,c,d,e,f) rc_buildreq(rch,a,b,c,d,(a)->secret,e,f)
62 #else
63 #define my_rc_buildreq(a,b,c,d,e,f) rc_buildreq(rch,a,b,c,d,e,f)
64 #endif
65 #define my_rc_avpair_add(a,b,c,d) rc_avpair_add(rch,a,b,c,-1,d)
66 #define my_rc_read_dictionary(a) rc_read_dictionary(rch, a)
67 #else
68 #define my_rc_conf_str(a) rc_conf_str(a)
69 #define my_rc_send_server(a,b) rc_send_server(a, b)
70 #define my_rc_buildreq(a,b,c,d,e,f) rc_buildreq(a,b,c,d,e,f)
71 #define my_rc_avpair_add(a,b,c,d) rc_avpair_add(a, b, c, d)
72 #define my_rc_read_dictionary(a) rc_read_dictionary(a)
73 #endif
75 /* REJECT_RC is only defined in some version of radiusclient. It has
76 * been reported from radiusclient-ng 0.5.6 on FreeBSD 7.2-RELEASE */
77 #ifndef REJECT_RC
78 #define REJECT_RC BADRESP_RC
79 #endif
81 static int my_rc_read_config(char * /*a*/);
83 #if defined(HAVE_LIBFREERADIUS_CLIENT) || defined(HAVE_LIBRADIUSCLIENT_NG) || defined(HAVE_LIBRADCLI)
84 static rc_handle *rch = NULL;
85 #endif
87 static char *server = NULL;
88 static char *username = NULL;
89 static char *password = NULL;
90 static char *nasid = NULL;
91 static char *nasipaddress = NULL;
92 static char *expect = NULL;
93 static char *config_file = NULL;
94 static unsigned short port = PW_AUTH_UDP_PORT;
95 static int retries = 1;
96 static bool verbose = false;
98 /******************************************************************************
100 The (pseudo?)literate programming XML is contained within \@\@\- <XML> \-\@\@
101 tags in the comments. With in the tags, the XML is assembled sequentially.
102 You can define entities in tags. You also have all the #defines available as
103 entities.
105 Please note that all tags must be lowercase to use the DocBook XML DTD.
107 @@-<article>
109 <sect1>
110 <title>Quick Reference</title>
111 <!-- The refentry forms a manpage -->
112 <refentry>
113 <refmeta>
114 <manvolnum>5<manvolnum>
115 </refmeta>
116 <refnamdiv>
117 <refname>&progname;</refname>
118 <refpurpose>&SUMMARY;</refpurpose>
119 </refnamdiv>
120 </refentry>
121 </sect1>
123 <sect1>
124 <title>FAQ</title>
125 </sect1>
127 <sect1>
128 <title>Theory, Installation, and Operation</title>
130 <sect2>
131 <title>General Description</title>
132 <para>
133 &DESCRIPTION;
134 </para>
135 </sect2>
137 <sect2>
138 <title>Future Enhancements</title>
139 <para>Todo List</para>
140 <itemizedlist>
141 <listitem>Add option to get password from a secured file rather than the command line</listitem>
142 </itemizedlist>
143 </sect2>
146 <sect2>
147 <title>Functions</title>
149 ******************************************************************************/
154 main (int argc, char **argv)
156 struct sockaddr_storage ss;
157 char name[HOST_NAME_MAX];
158 #ifdef RC_BUFFER_LEN
159 char msg[RC_BUFFER_LEN];
160 #else
161 char msg[BUFFER_LEN];
162 #endif
163 SEND_DATA data;
164 int result = STATE_UNKNOWN;
165 uint32_t client_id, service;
166 char *str;
168 setlocale (LC_ALL, "");
169 bindtextdomain (PACKAGE, LOCALEDIR);
170 textdomain (PACKAGE);
172 /* Parse extra opts if any */
173 argv=np_extra_opts (&argc, argv, progname);
175 if (process_arguments (argc, argv) == ERROR)
176 usage4 (_("Could not parse arguments"));
178 str = strdup ("dictionary");
179 if ((config_file && my_rc_read_config (config_file)) ||
180 my_rc_read_dictionary (my_rc_conf_str (str)))
181 die (STATE_UNKNOWN, _("Config file error\n"));
183 service = PW_AUTHENTICATE_ONLY;
185 memset (&data, 0, sizeof(data));
186 if (!(my_rc_avpair_add (&data.send_pairs, PW_SERVICE_TYPE, &service, 0) &&
187 my_rc_avpair_add (&data.send_pairs, PW_USER_NAME, username, 0) &&
188 my_rc_avpair_add (&data.send_pairs, PW_USER_PASSWORD, password, 0)
190 die (STATE_UNKNOWN, _("Out of Memory?\n"));
192 if (nasid != NULL) {
193 if (!(my_rc_avpair_add (&data.send_pairs, PW_NAS_IDENTIFIER, nasid, 0)))
194 die (STATE_UNKNOWN, _("Invalid NAS-Identifier\n"));
197 if (nasipaddress == NULL) {
198 if (gethostname (name, sizeof(name)) != 0)
199 die (STATE_UNKNOWN, _("gethostname() failed!\n"));
200 nasipaddress = name;
202 if (!dns_lookup (nasipaddress, &ss, AF_INET)) /* TODO: Support IPv6. */
203 die (STATE_UNKNOWN, _("Invalid NAS-IP-Address\n"));
204 client_id = ntohl (((struct sockaddr_in *)&ss)->sin_addr.s_addr);
205 if (my_rc_avpair_add (&(data.send_pairs), PW_NAS_IP_ADDRESS, &client_id, 0) == NULL)
206 die (STATE_UNKNOWN, _("Invalid NAS-IP-Address\n"));
208 my_rc_buildreq (&data, PW_ACCESS_REQUEST, server, port, (int)timeout_interval,
209 retries);
211 result = my_rc_send_server (&data, msg);
212 rc_avpair_free (data.send_pairs);
213 if (data.receive_pairs)
214 rc_avpair_free (data.receive_pairs);
216 if (result == TIMEOUT_RC)
217 die (STATE_CRITICAL, _("Timeout\n"));
218 if (result == ERROR_RC)
219 die (STATE_CRITICAL, _("Auth Error\n"));
220 if (result == REJECT_RC)
221 die (STATE_WARNING, _("Auth Failed\n"));
222 if (result == BADRESP_RC)
223 die (STATE_WARNING, _("Bad Response\n"));
224 if (expect && !strstr (msg, expect))
225 die (STATE_WARNING, "%s\n", msg);
226 if (result == OK_RC)
227 die (STATE_OK, _("Auth OK\n"));
228 (void)snprintf(msg, sizeof(msg), _("Unexpected result code %d"), result);
229 die (STATE_UNKNOWN, "%s\n", msg);
234 /* process command-line arguments */
236 process_arguments (int argc, char **argv)
238 int c;
240 int option = 0;
241 static struct option longopts[] = {
242 {"hostname", required_argument, 0, 'H'},
243 {"port", required_argument, 0, 'P'},
244 {"username", required_argument, 0, 'u'},
245 {"password", required_argument, 0, 'p'},
246 {"nas-id", required_argument, 0, 'n'},
247 {"nas-ip-address", required_argument, 0, 'N'},
248 {"filename", required_argument, 0, 'F'},
249 {"expect", required_argument, 0, 'e'},
250 {"retries", required_argument, 0, 'r'},
251 {"timeout", required_argument, 0, 't'},
252 {"verbose", no_argument, 0, 'v'},
253 {"version", no_argument, 0, 'V'},
254 {"help", no_argument, 0, 'h'},
255 {0, 0, 0, 0}
258 while (1) {
259 c = getopt_long (argc, argv, "+hVvH:P:F:u:p:n:N:t:r:e:", longopts,
260 &option);
262 if (c == -1 || c == EOF || c == 1)
263 break;
265 switch (c) {
266 case '?': /* print short usage statement if args not parsable */
267 usage5 ();
268 case 'h': /* help */
269 print_help ();
270 exit (STATE_UNKNOWN);
271 case 'V': /* version */
272 print_revision (progname, NP_VERSION);
273 exit (STATE_UNKNOWN);
274 case 'v': /* verbose mode */
275 verbose = true;
276 break;
277 case 'H': /* hostname */
278 if (!is_host (optarg)) {
279 usage2 (_("Invalid hostname/address"), optarg);
281 server = optarg;
282 break;
283 case 'P': /* port */
284 if (is_intnonneg (optarg))
285 port = (unsigned short)atoi (optarg);
286 else
287 usage4 (_("Port must be a positive integer"));
288 break;
289 case 'u': /* username */
290 username = optarg;
291 break;
292 case 'p': /* password */
293 password = strdup(optarg);
295 /* Delete the password from process list */
296 while (*optarg != '\0') {
297 *optarg = 'X';
298 optarg++;
300 break;
301 case 'n': /* nas id */
302 nasid = optarg;
303 break;
304 case 'N': /* nas ip address */
305 nasipaddress = optarg;
306 break;
307 case 'F': /* configuration file */
308 config_file = optarg;
309 break;
310 case 'e': /* expect */
311 expect = optarg;
312 break;
313 case 'r': /* retries */
314 if (is_intpos (optarg))
315 retries = atoi (optarg);
316 else
317 usage4 (_("Number of retries must be a positive integer"));
318 break;
319 case 't': /* timeout */
320 if (is_intpos (optarg))
321 timeout_interval = (unsigned)atoi (optarg);
322 else
323 usage2 (_("Timeout interval must be a positive integer"), optarg);
324 break;
328 if (server == NULL)
329 usage4 (_("Hostname was not supplied"));
330 if (username == NULL)
331 usage4 (_("User not specified"));
332 if (password == NULL)
333 usage4 (_("Password not specified"));
334 if (config_file == NULL)
335 usage4 (_("Configuration file not specified"));
337 return OK;
342 void
343 print_help (void)
345 char *myport;
346 xasprintf (&myport, "%d", PW_AUTH_UDP_PORT);
348 print_revision (progname, NP_VERSION);
350 printf ("Copyright (c) 1999 Robert August Vincent II\n");
351 printf (COPYRIGHT, copyright, email);
353 printf("%s\n", _("Tests to see if a RADIUS server is accepting connections."));
355 printf ("\n\n");
357 print_usage ();
359 printf (UT_HELP_VRSN);
360 printf (UT_EXTRA_OPTS);
362 printf (UT_HOST_PORT, 'P', myport);
364 printf (" %s\n", "-u, --username=STRING");
365 printf (" %s\n", _("The user to authenticate"));
366 printf (" %s\n", "-p, --password=STRING");
367 printf (" %s\n", _("Password for authentication (SECURITY RISK)"));
368 printf (" %s\n", "-n, --nas-id=STRING");
369 printf (" %s\n", _("NAS identifier"));
370 printf (" %s\n", "-N, --nas-ip-address=STRING");
371 printf (" %s\n", _("NAS IP Address"));
372 printf (" %s\n", "-F, --filename=STRING");
373 printf (" %s\n", _("Configuration file"));
374 printf (" %s\n", "-e, --expect=STRING");
375 printf (" %s\n", _("Response string to expect from the server"));
376 printf (" %s\n", "-r, --retries=INTEGER");
377 printf (" %s\n", _("Number of times to retry a failed connection"));
379 printf (UT_CONN_TIMEOUT, timeout_interval);
381 printf ("\n");
382 printf ("%s\n", _("This plugin tests a RADIUS server to see if it is accepting connections."));
383 printf ("%s\n", _("The server to test must be specified in the invocation, as well as a user"));
384 printf ("%s\n", _("name and password. A configuration file must be present. The format of"));
385 printf ("%s\n", _("the configuration file is described in the radiusclient library sources."));
386 printf ("%s\n", _("The password option presents a substantial security issue because the"));
387 printf ("%s\n", _("password can possibly be determined by careful watching of the command line"));
388 printf ("%s\n", _("in a process listing. This risk is exacerbated because the plugin will"));
389 printf ("%s\n", _("typically be executed at regular predictable intervals. Please be sure that"));
390 printf ("%s\n", _("the password used does not allow access to sensitive system resources."));
392 printf (UT_SUPPORT);
397 void
398 print_usage (void)
400 printf ("%s\n", _("Usage:"));
401 printf ("%s -H host -F config_file -u username -p password\n\
402 [-P port] [-t timeout] [-r retries] [-e expect]\n\
403 [-n nas-id] [-N nas-ip-addr]\n", progname);
408 int my_rc_read_config(char * a)
410 #if defined(HAVE_LIBFREERADIUS_CLIENT) || defined(HAVE_LIBRADIUSCLIENT_NG) || defined(HAVE_LIBRADCLI)
411 rch = rc_read_config(a);
412 return (rch == NULL) ? 1 : 0;
413 #else
414 return rc_read_config(a);
415 #endif