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