1 /*****************************************************************************
2 *
3 * Nagios check_radius plugin
4 *
5 * License: GPL
6 * Copyright (c) 1999-2008 Nagios Plugins Development Team
7 *
8 * Description:
9 *
10 * This file contains the check_radius plugin
11 *
12 * Tests to see if a radius server is accepting connections.
13 *
14 *
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.
19 *
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.
24 *
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/>.
27 *
28 *
29 *****************************************************************************/
31 const char *progname = "check_radius";
32 const char *copyright = "2000-2008";
33 const char *email = "nagiosplug-devel@lists.sourceforge.net";
35 #include "common.h"
36 #include "utils.h"
37 #include "netutils.h"
39 #ifdef HAVE_LIBRADIUSCLIENT_NG
40 #include <radiusclient-ng.h>
41 rc_handle *rch = NULL;
42 #else
43 #include <radiusclient.h>
44 #endif
46 int process_arguments (int, char **);
47 void print_help (void);
48 void print_usage (void);
50 /* libradiusclient(-ng) wrapper functions */
51 #ifdef HAVE_LIBRADIUSCLIENT_NG
52 #define my_rc_conf_str(a) rc_conf_str(rch,a)
53 #define my_rc_send_server(a,b) rc_send_server(rch,a,b)
54 #define my_rc_buildreq(a,b,c,d,e,f) rc_buildreq(rch,a,b,c,d,e,f)
55 #define my_rc_own_ipaddress() rc_own_ipaddress(rch)
56 #define my_rc_avpair_add(a,b,c,d) rc_avpair_add(rch,a,b,c,-1,d)
57 #define my_rc_read_dictionary(a) rc_read_dictionary(rch, a)
58 #else
59 #define my_rc_conf_str(a) rc_conf_str(a)
60 #define my_rc_send_server(a,b) rc_send_server(a, b)
61 #define my_rc_buildreq(a,b,c,d,e,f) rc_buildreq(a,b,c,d,e,f)
62 #define my_rc_own_ipaddress() rc_own_ipaddress()
63 #define my_rc_avpair_add(a,b,c,d) rc_avpair_add(a, b, c, d)
64 #define my_rc_read_dictionary(a) rc_read_dictionary(a)
65 #endif
67 /* REJECT_RC is only defined in some version of radiusclient. It has
68 * been reported from radiusclient-ng 0.5.6 on FreeBSD 7.2-RELEASE */
69 #ifndef REJECT_RC
70 #define REJECT_RC BADRESP_RC
71 #endif
73 int my_rc_read_config(char *);
75 char *server = NULL;
76 char *username = NULL;
77 char *password = NULL;
78 char *nasid = NULL;
79 char *nasipaddress = NULL;
80 char *expect = NULL;
81 char *config_file = NULL;
82 unsigned short port = PW_AUTH_UDP_PORT;
83 int retries = 1;
84 int verbose = FALSE;
85 ENV *env = NULL;
87 /******************************************************************************
89 The (psuedo?)literate programming XML is contained within \@\@\- <XML> \-\@\@
90 tags in the comments. With in the tags, the XML is assembled sequentially.
91 You can define entities in tags. You also have all the #defines available as
92 entities.
94 Please note that all tags must be lowercase to use the DocBook XML DTD.
96 @@-<article>
98 <sect1>
99 <title>Quick Reference</title>
100 <!-- The refentry forms a manpage -->
101 <refentry>
102 <refmeta>
103 <manvolnum>5<manvolnum>
104 </refmeta>
105 <refnamdiv>
106 <refname>&progname;</refname>
107 <refpurpose>&SUMMARY;</refpurpose>
108 </refnamdiv>
109 </refentry>
110 </sect1>
112 <sect1>
113 <title>FAQ</title>
114 </sect1>
116 <sect1>
117 <title>Theory, Installation, and Operation</title>
119 <sect2>
120 <title>General Description</title>
121 <para>
122 &DESCRIPTION;
123 </para>
124 </sect2>
126 <sect2>
127 <title>Future Enhancements</title>
128 <para>Todo List</para>
129 <itemizedlist>
130 <listitem>Add option to get password from a secured file rather than the command line</listitem>
131 </itemizedlist>
132 </sect2>
135 <sect2>
136 <title>Functions</title>
137 -@@
138 ******************************************************************************/
142 int
143 main (int argc, char **argv)
144 {
145 UINT4 service;
146 char msg[BUFFER_LEN];
147 SEND_DATA data;
148 int result = STATE_UNKNOWN;
149 UINT4 client_id;
150 char *str;
152 setlocale (LC_ALL, "");
153 bindtextdomain (PACKAGE, LOCALEDIR);
154 textdomain (PACKAGE);
156 /* Parse extra opts if any */
157 argv=np_extra_opts (&argc, argv, progname);
159 if (process_arguments (argc, argv) == ERROR)
160 usage4 (_("Could not parse arguments"));
162 str = strdup ("dictionary");
163 if ((config_file && my_rc_read_config (config_file)) ||
164 my_rc_read_dictionary (my_rc_conf_str (str)))
165 die (STATE_UNKNOWN, _("Config file error"));
167 service = PW_AUTHENTICATE_ONLY;
169 memset (&data, 0, sizeof(data));
170 if (!(my_rc_avpair_add (&data.send_pairs, PW_SERVICE_TYPE, &service, 0) &&
171 my_rc_avpair_add (&data.send_pairs, PW_USER_NAME, username, 0) &&
172 my_rc_avpair_add (&data.send_pairs, PW_USER_PASSWORD, password, 0)
173 ))
174 die (STATE_UNKNOWN, _("Out of Memory?"));
176 if (nasid != NULL) {
177 if (!(my_rc_avpair_add (&data.send_pairs, PW_NAS_IDENTIFIER, nasid, 0)))
178 die (STATE_UNKNOWN, _("Invalid NAS-Identifier"));
179 }
181 if (nasipaddress != NULL) {
182 if (rc_good_ipaddr (nasipaddress))
183 die (STATE_UNKNOWN, _("Invalid NAS-IP-Address"));
184 if ((client_id = rc_get_ipaddr(nasipaddress)) == 0)
185 die (STATE_UNKNOWN, _("Invalid NAS-IP-Address"));
186 } else {
187 if ((client_id = my_rc_own_ipaddress ()) == 0)
188 die (STATE_UNKNOWN, _("Can't find local IP for NAS-IP-Address"));
189 }
190 if (my_rc_avpair_add (&(data.send_pairs), PW_NAS_IP_ADDRESS, &client_id, 0) == NULL)
191 die (STATE_UNKNOWN, _("Invalid NAS-IP-Address"));
193 my_rc_buildreq (&data, PW_ACCESS_REQUEST, server, port, (int)timeout_interval,
194 retries);
196 result = my_rc_send_server (&data, msg);
197 rc_avpair_free (data.send_pairs);
198 if (data.receive_pairs)
199 rc_avpair_free (data.receive_pairs);
201 if (result == TIMEOUT_RC)
202 die (STATE_CRITICAL, _("Timeout"));
203 if (result == ERROR_RC)
204 die (STATE_CRITICAL, _("Auth Error"));
205 if (result == REJECT_RC)
206 die (STATE_WARNING, _("Auth Failed"));
207 if (result == BADRESP_RC)
208 die (STATE_WARNING, _("Bad Response"));
209 if (expect && !strstr (msg, expect))
210 die (STATE_WARNING, "%s", msg);
211 if (result == OK_RC)
212 die (STATE_OK, _("Auth OK"));
213 (void)snprintf(msg, sizeof(msg), _("Unexpected result code %d"), result);
214 die (STATE_UNKNOWN, msg);
215 }
219 /* process command-line arguments */
220 int
221 process_arguments (int argc, char **argv)
222 {
223 int c;
225 int option = 0;
226 static struct option longopts[] = {
227 {"hostname", required_argument, 0, 'H'},
228 {"port", required_argument, 0, 'P'},
229 {"username", required_argument, 0, 'u'},
230 {"password", required_argument, 0, 'p'},
231 {"nas-id", required_argument, 0, 'n'},
232 {"nas-ip-address", required_argument, 0, 'N'},
233 {"filename", required_argument, 0, 'F'},
234 {"expect", required_argument, 0, 'e'},
235 {"retries", required_argument, 0, 'r'},
236 {"timeout", required_argument, 0, 't'},
237 {"verbose", no_argument, 0, 'v'},
238 {"version", no_argument, 0, 'V'},
239 {"help", no_argument, 0, 'h'},
240 {0, 0, 0, 0}
241 };
243 while (1) {
244 c = getopt_long (argc, argv, "+hVvH:P:F:u:p:n:N:t:r:e:", longopts,
245 &option);
247 if (c == -1 || c == EOF || c == 1)
248 break;
250 switch (c) {
251 case '?': /* print short usage statement if args not parsable */
252 usage5 ();
253 case 'h': /* help */
254 print_help ();
255 exit (OK);
256 case 'V': /* version */
257 print_revision (progname, NP_VERSION);
258 exit (OK);
259 case 'v': /* verbose mode */
260 verbose = TRUE;
261 break;
262 case 'H': /* hostname */
263 if (is_host (optarg) == FALSE) {
264 usage2 (_("Invalid hostname/address"), optarg);
265 }
266 server = optarg;
267 break;
268 case 'P': /* port */
269 if (is_intnonneg (optarg))
270 port = atoi (optarg);
271 else
272 usage4 (_("Port must be a positive integer"));
273 break;
274 case 'u': /* username */
275 username = optarg;
276 break;
277 case 'p': /* password */
278 password = strdup(optarg);
280 /* Delete the password from process list */
281 while (*optarg != '\0') {
282 *optarg = 'X';
283 optarg++;
284 }
285 break;
286 case 'n': /* nas id */
287 nasid = optarg;
288 break;
289 case 'N': /* nas ip address */
290 nasipaddress = optarg;
291 break;
292 case 'F': /* configuration file */
293 config_file = optarg;
294 break;
295 case 'e': /* expect */
296 expect = optarg;
297 break;
298 case 'r': /* retries */
299 if (is_intpos (optarg))
300 retries = atoi (optarg);
301 else
302 usage4 (_("Number of retries must be a positive integer"));
303 break;
304 case 't': /* timeout */
305 if (is_intpos (optarg))
306 timeout_interval = atoi (optarg);
307 else
308 usage2 (_("Timeout interval must be a positive integer"), optarg);
309 break;
310 }
311 }
313 if (server == NULL)
314 usage4 (_("Hostname was not supplied"));
315 if (username == NULL)
316 usage4 (_("User not specified"));
317 if (password == NULL)
318 usage4 (_("Password not specified"));
319 if (config_file == NULL)
320 usage4 (_("Configuration file not specified"));
322 return OK;
323 }
327 void
328 print_help (void)
329 {
330 char *myport;
331 asprintf (&myport, "%d", PW_AUTH_UDP_PORT);
333 print_revision (progname, NP_VERSION);
335 printf ("Copyright (c) 1999 Robert August Vincent II\n");
336 printf (COPYRIGHT, copyright, email);
338 printf("%s\n", _("Tests to see if a RADIUS server is accepting connections."));
340 printf ("\n\n");
342 print_usage ();
344 printf (UT_HELP_VRSN);
345 printf (UT_EXTRA_OPTS);
347 printf (UT_HOST_PORT, 'P', myport);
349 printf (" %s\n", "-u, --username=STRING");
350 printf (" %s\n", _("The user to authenticate"));
351 printf (" %s\n", "-p, --password=STRING");
352 printf (" %s\n", _("Password for autentication (SECURITY RISK)"));
353 printf (" %s\n", "-n, --nas-id=STRING");
354 printf (" %s\n", _("NAS identifier"));
355 printf (" %s\n", "-N, --nas-ip-address=STRING");
356 printf (" %s\n", _("NAS IP Address"));
357 printf (" %s\n", "-F, --filename=STRING");
358 printf (" %s\n", _("Configuration file"));
359 printf (" %s\n", "-e, --expect=STRING");
360 printf (" %s\n", _("Response string to expect from the server"));
361 printf (" %s\n", "-r, --retries=INTEGER");
362 printf (" %s\n", _("Number of times to retry a failed connection"));
364 printf (UT_TIMEOUT, timeout_interval);
366 printf ("\n");
367 printf ("%s\n", _("This plugin tests a RADIUS server to see if it is accepting connections."));
368 printf ("%s\n", _("The server to test must be specified in the invocation, as well as a user"));
369 printf ("%s\n", _("name and password. A configuration file may also be present. The format of"));
370 printf ("%s\n", _("the configuration file is described in the radiusclient library sources."));
371 printf ("%s\n", _("The password option presents a substantial security issue because the"));
372 printf ("%s\n", _("password can possibly be determined by careful watching of the command line"));
373 printf ("%s\n", _("in a process listing. This risk is exacerbated because nagios will"));
374 printf ("%s\n", _("run the plugin at regular predictable intervals. Please be sure that"));
375 printf ("%s\n", _("the password used does not allow access to sensitive system resources."));
377 #ifdef NP_EXTRA_OPTS
378 printf ("\n");
379 printf ("%s\n", _("Notes:"));
380 printf (UT_EXTRA_OPTS_NOTES);
381 #endif
383 printf (UT_SUPPORT);
384 }
388 void
389 print_usage (void)
390 {
391 printf (_("Usage:"));
392 printf ("%s -H host -F config_file -u username -p password\n\
393 [-P port] [-t timeout] [-r retries] [-e expect]\n\
394 [-n nas-id] [-N nas-ip-addr]\n", progname);
395 }
399 int my_rc_read_config(char * a)
400 {
401 #ifdef HAVE_LIBRADIUSCLIENT_NG
402 rch = rc_read_config(a);
403 return (rch == NULL) ? 1 : 0;
404 #else
405 return rc_read_config(a);
406 #endif
407 }