1 /******************************************************************************
2 *
3 * Nagios check_radius plugin
4 *
5 * License: GPL
6 * Copyright (c) 1999-2006 nagios-plugins team
7 *
8 * Last Modified: $Date$
9 *
10 * Description:
11 *
12 * This file contains the check_radius plugin
13 *
14 * Tests to see if a radius server is accepting connections.
15 *
16 *
17 * License Information:
18 *
19 * This program is free software; you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation; either version 2 of the License, or
22 * (at your option) any later version.
23 *
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
28 *
29 * You should have received a copy of the GNU General Public License
30 * along with this program; if not, write to the Free Software
31 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
32 *
33 * $Id$
34 *
35 *******************************************************************************/
37 const char *progname = "check_radius";
38 const char *revision = "$Revision$";
39 const char *copyright = "2000-2006";
40 const char *email = "nagiosplug-devel@lists.sourceforge.net";
42 #include "common.h"
43 #include "utils.h"
44 #include "netutils.h"
46 #include <radiusclient.h>
48 int process_arguments (int, char **);
49 void print_help (void);
50 void print_usage (void);
52 char *server = NULL;
53 char *username = NULL;
54 char *password = NULL;
55 char *nasid = NULL;
56 char *expect = NULL;
57 char *config_file = NULL;
58 unsigned short port = PW_AUTH_UDP_PORT;
59 int retries = 1;
60 int verbose = FALSE;
61 ENV *env = NULL;
63 /******************************************************************************
65 The (psuedo?)literate programming XML is contained within \@\@\- <XML> \-\@\@
66 tags in the comments. With in the tags, the XML is assembled sequentially.
67 You can define entities in tags. You also have all the #defines available as
68 entities.
70 Please note that all tags must be lowercase to use the DocBook XML DTD.
72 @@-<article>
74 <sect1>
75 <title>Quick Reference</title>
76 <!-- The refentry forms a manpage -->
77 <refentry>
78 <refmeta>
79 <manvolnum>5<manvolnum>
80 </refmeta>
81 <refnamdiv>
82 <refname>&progname;</refname>
83 <refpurpose>&SUMMARY;</refpurpose>
84 </refnamdiv>
85 </refentry>
86 </sect1>
88 <sect1>
89 <title>FAQ</title>
90 </sect1>
92 <sect1>
93 <title>Theory, Installation, and Operation</title>
95 <sect2>
96 <title>General Description</title>
97 <para>
98 &DESCRIPTION;
99 </para>
100 </sect2>
102 <sect2>
103 <title>Future Enhancements</title>
104 <para>Todo List</para>
105 <itemizedlist>
106 <listitem>Add option to get password from a secured file rather than the command line</listitem>
107 </itemizedlist>
108 </sect2>
111 <sect2>
112 <title>Functions</title>
113 -@@
114 ******************************************************************************/
118 int
119 main (int argc, char **argv)
120 {
121 UINT4 service;
122 char msg[BUFFER_LEN];
123 SEND_DATA data;
124 int result = STATE_UNKNOWN;
125 UINT4 client_id;
126 char *str;
128 setlocale (LC_ALL, "");
129 bindtextdomain (PACKAGE, LOCALEDIR);
130 textdomain (PACKAGE);
132 if (process_arguments (argc, argv) == ERROR)
133 usage4 (_("Could not parse arguments"));
135 str = strdup ("dictionary");
136 if ((config_file && rc_read_config (config_file)) ||
137 rc_read_dictionary (rc_conf_str (str)))
138 die (STATE_UNKNOWN, _("Config file error"));
140 service = PW_AUTHENTICATE_ONLY;
142 memset (&data, 0, sizeof(data));
143 if (!(rc_avpair_add (&data.send_pairs, PW_SERVICE_TYPE, &service, 0) &&
144 rc_avpair_add (&data.send_pairs, PW_USER_NAME, username, 0) &&
145 rc_avpair_add (&data.send_pairs, PW_USER_PASSWORD, password, 0) &&
146 (nasid==NULL || rc_avpair_add (&data.send_pairs, PW_NAS_IDENTIFIER, nasid, 0))))
147 die (STATE_UNKNOWN, _("Out of Memory?"));
149 /*
150 * Fill in NAS-IP-Address
151 */
153 if ((client_id = rc_own_ipaddress ()) == 0)
154 return (ERROR_RC);
156 if (rc_avpair_add (&(data.send_pairs), PW_NAS_IP_ADDRESS, &client_id, 0) ==
157 NULL) return (ERROR_RC);
159 rc_buildreq (&data, PW_ACCESS_REQUEST, server, port, (int)timeout_interval,
160 retries);
162 result = rc_send_server (&data, msg);
163 rc_avpair_free (data.send_pairs);
164 if (data.receive_pairs)
165 rc_avpair_free (data.receive_pairs);
167 if (result == TIMEOUT_RC)
168 die (STATE_CRITICAL, _("Timeout"));
169 if (result == ERROR_RC)
170 die (STATE_CRITICAL, _("Auth Error"));
171 if (result == BADRESP_RC)
172 die (STATE_WARNING, _("Auth Failed"));
173 if (expect && !strstr (msg, expect))
174 die (STATE_WARNING, "%s", msg);
175 if (result == OK_RC)
176 die (STATE_OK, _("Auth OK"));
177 return (0);
178 }
182 /* process command-line arguments */
183 int
184 process_arguments (int argc, char **argv)
185 {
186 int c;
188 int option = 0;
189 static struct option longopts[] = {
190 {"hostname", required_argument, 0, 'H'},
191 {"port", required_argument, 0, 'P'},
192 {"username", required_argument, 0, 'u'},
193 {"password", required_argument, 0, 'p'},
194 {"nas-id", required_argument, 0, 'n'},
195 {"filename", required_argument, 0, 'F'},
196 {"expect", required_argument, 0, 'e'},
197 {"retries", required_argument, 0, 'r'},
198 {"timeout", required_argument, 0, 't'},
199 {"verbose", no_argument, 0, 'v'},
200 {"version", no_argument, 0, 'V'},
201 {"help", no_argument, 0, 'h'},
202 {0, 0, 0, 0}
203 };
205 if (argc < 2)
206 return ERROR;
208 if (argc == 9) {
209 config_file = argv[1];
210 username = argv[2];
211 password = argv[3];
212 if (is_intpos (argv[4]))
213 timeout_interval = atoi (argv[4]);
214 else
215 usage2 (_("Timeout interval must be a positive integer"), optarg);
216 if (is_intpos (argv[5]))
217 retries = atoi (argv[5]);
218 else
219 usage4 (_("Number of retries must be a positive integer"));
220 server = argv[6];
221 if (is_intpos (argv[7]))
222 port = atoi (argv[7]);
223 else
224 usage4 (_("Port must be a positive integer"));
225 expect = argv[8];
226 return OK;
227 }
229 while (1) {
230 c = getopt_long (argc, argv, "+hVvH:P:F:u:p:n:t:r:e:", longopts,
231 &option);
233 if (c == -1 || c == EOF || c == 1)
234 break;
236 switch (c) {
237 case '?': /* print short usage statement if args not parsable */
238 usage5 ();
239 case 'h': /* help */
240 print_help ();
241 exit (OK);
242 case 'V': /* version */
243 print_revision (progname, revision);
244 exit (OK);
245 case 'v': /* verbose mode */
246 verbose = TRUE;
247 break;
248 case 'H': /* hostname */
249 if (is_host (optarg) == FALSE) {
250 usage2 (_("Invalid hostname/address"), optarg);
251 }
252 server = optarg;
253 break;
254 case 'P': /* port */
255 if (is_intnonneg (optarg))
256 port = atoi (optarg);
257 else
258 usage4 (_("Port must be a positive integer"));
259 break;
260 case 'u': /* username */
261 username = optarg;
262 break;
263 case 'p': /* password */
264 password = optarg;
265 break;
266 case 'n': /* nas id */
267 nasid = optarg;
268 break;
269 case 'F': /* configuration file */
270 config_file = optarg;
271 break;
272 case 'e': /* expect */
273 expect = optarg;
274 break;
275 case 'r': /* retries */
276 if (is_intpos (optarg))
277 retries = atoi (optarg);
278 else
279 usage4 (_("Number of retries must be a positive integer"));
280 break;
281 case 't': /* timeout */
282 if (is_intpos (optarg))
283 timeout_interval = atoi (optarg);
284 else
285 usage2 (_("Timeout interval must be a positive integer"), optarg);
286 break;
287 }
288 }
289 return OK;
290 }
294 void
295 print_help (void)
296 {
297 char *myport;
298 asprintf (&myport, "%d", PW_AUTH_UDP_PORT);
300 print_revision (progname, revision);
302 printf ("Copyright (c) 1999 Robert August Vincent II\n");
303 printf (COPYRIGHT, copyright, email);
305 printf("%s\n", _("Tests to see if a radius server is accepting connections."));
307 printf ("\n\n");
309 print_usage ();
311 printf (_(UT_HELP_VRSN));
313 printf (_(UT_HOST_PORT), 'P', myport);
315 printf (" %s\n", "-u, --username=STRING");
316 printf (" %s\n", _("The user to authenticate"));
317 printf (" %s\n", "-p, --password=STRING");
318 printf (" %s\n", _("Password for autentication (SECURITY RISK)"));
319 printf (" %s\n", "-n, --nas-id=STRING");
320 printf (" %s\n", _("NAS identifier"));
321 printf (" %s\n", "-F, --filename=STRING");
322 printf (" %s\n", _("Configuration file"));
323 printf (" %s\n", "-e, --expect=STRING");
324 printf (" %s\n", _("Response string to expect from the server"));
325 printf (" %s\n", "-r, --retries=INTEGER");
326 printf (" %s\n", _("Number of times to retry a failed connection"));
328 printf (_(UT_TIMEOUT), timeout_interval);
330 printf ("%s\n", _("This plugin tests a radius server to see if it is accepting connections."));
331 printf ("%s\n", _("The server to test must be specified in the invocation, as well as a user"));
332 printf ("%s\n", _("name and password. A configuration file may also be present. The format of"));
333 printf ("%s\n", _("the configuration file is described in the radiusclient library sources."));
334 printf ("%s\n", _("The password option presents a substantial security issue because the"));
335 printf ("%s\n", _("password can be determined by careful watching of the command line in"));
336 printf ("%s\n", _("a process listing. This risk is exacerbated because nagios will"));
337 printf ("%s\n", _("run the plugin at regular prdictable intervals. Please be sure that"));
338 printf ("%s\n", _("the password used does not allow access to sensitive system resources,"));
339 printf ("%s\n", _("otherwise compormise could occur."));
341 printf (_(UT_SUPPORT));
342 }
346 void
347 print_usage (void)
348 {
349 printf (_("Usage:"));
350 printf ("%s -H host -F config_file -u username -p password [-n nas-id] [-P port]\n\
351 [-t timeout] [-r retries] [-e expect]\n", progname);
352 }