1 /*****************************************************************************
3 * Nagios check_radius plugin
6 * Copyright (c) 1999-2008 Nagios Plugins Development Team
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-2008";
33 const char *email
= "nagiosplug-devel@lists.sourceforge.net";
39 #ifdef HAVE_LIBRADIUSCLIENT_NG
40 #include <radiusclient-ng.h>
41 rc_handle
*rch
= NULL
;
43 #include <radiusclient.h>
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)
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)
66 int my_rc_read_config(char *);
69 char *username
= NULL
;
70 char *password
= NULL
;
73 char *config_file
= NULL
;
74 unsigned short port
= PW_AUTH_UDP_PORT
;
79 /******************************************************************************
81 The (psuedo?)literate programming XML is contained within \@\@\- <XML> \-\@\@
82 tags in the comments. With in the tags, the XML is assembled sequentially.
83 You can define entities in tags. You also have all the #defines available as
86 Please note that all tags must be lowercase to use the DocBook XML DTD.
91 <title>Quick Reference</title>
92 <!-- The refentry forms a manpage -->
95 <manvolnum>5<manvolnum>
98 <refname>&progname;</refname>
99 <refpurpose>&SUMMARY;</refpurpose>
109 <title>Theory, Installation, and Operation</title>
112 <title>General Description</title>
119 <title>Future Enhancements</title>
120 <para>Todo List</para>
122 <listitem>Add option to get password from a secured file rather than the command line</listitem>
128 <title>Functions</title>
130 ******************************************************************************/
135 main (int argc
, char **argv
)
138 char msg
[BUFFER_LEN
];
140 int result
= STATE_UNKNOWN
;
144 setlocale (LC_ALL
, "");
145 bindtextdomain (PACKAGE
, LOCALEDIR
);
146 textdomain (PACKAGE
);
148 /* Parse extra opts if any */
149 argv
=np_extra_opts (&argc
, argv
, progname
);
151 if (process_arguments (argc
, argv
) == ERROR
)
152 usage4 (_("Could not parse arguments"));
154 str
= strdup ("dictionary");
155 if ((config_file
&& my_rc_read_config (config_file
)) ||
156 my_rc_read_dictionary (my_rc_conf_str (str
)))
157 die (STATE_UNKNOWN
, _("Config file error"));
159 service
= PW_AUTHENTICATE_ONLY
;
161 memset (&data
, 0, sizeof(data
));
162 if (!(my_rc_avpair_add (&data
.send_pairs
, PW_SERVICE_TYPE
, &service
, 0) &&
163 my_rc_avpair_add (&data
.send_pairs
, PW_USER_NAME
, username
, 0) &&
164 my_rc_avpair_add (&data
.send_pairs
, PW_USER_PASSWORD
, password
, 0) &&
165 (nasid
==NULL
|| my_rc_avpair_add (&data
.send_pairs
, PW_NAS_IDENTIFIER
, nasid
, 0))))
166 die (STATE_UNKNOWN
, _("Out of Memory?"));
169 * Fill in NAS-IP-Address
172 if ((client_id
= my_rc_own_ipaddress ()) == 0)
175 if (my_rc_avpair_add (&(data
.send_pairs
), PW_NAS_IP_ADDRESS
, &client_id
, 0) ==
176 NULL
) return (ERROR_RC
);
178 my_rc_buildreq (&data
, PW_ACCESS_REQUEST
, server
, port
, (int)timeout_interval
,
181 result
= my_rc_send_server (&data
, msg
);
182 rc_avpair_free (data
.send_pairs
);
183 if (data
.receive_pairs
)
184 rc_avpair_free (data
.receive_pairs
);
186 if (result
== TIMEOUT_RC
)
187 die (STATE_CRITICAL
, _("Timeout"));
188 if (result
== ERROR_RC
)
189 die (STATE_CRITICAL
, _("Auth Error"));
190 if (result
== BADRESP_RC
)
191 die (STATE_WARNING
, _("Auth Failed"));
192 if (expect
&& !strstr (msg
, expect
))
193 die (STATE_WARNING
, "%s", msg
);
195 die (STATE_OK
, _("Auth OK"));
201 /* process command-line arguments */
203 process_arguments (int argc
, char **argv
)
208 static struct option longopts
[] = {
209 {"hostname", required_argument
, 0, 'H'},
210 {"port", required_argument
, 0, 'P'},
211 {"username", required_argument
, 0, 'u'},
212 {"password", required_argument
, 0, 'p'},
213 {"nas-id", required_argument
, 0, 'n'},
214 {"filename", required_argument
, 0, 'F'},
215 {"expect", required_argument
, 0, 'e'},
216 {"retries", required_argument
, 0, 'r'},
217 {"timeout", required_argument
, 0, 't'},
218 {"verbose", no_argument
, 0, 'v'},
219 {"version", no_argument
, 0, 'V'},
220 {"help", no_argument
, 0, 'h'},
225 c
= getopt_long (argc
, argv
, "+hVvH:P:F:u:p:n:t:r:e:", longopts
,
228 if (c
== -1 || c
== EOF
|| c
== 1)
232 case '?': /* print short usage statement if args not parsable */
237 case 'V': /* version */
238 print_revision (progname
, NP_VERSION
);
240 case 'v': /* verbose mode */
243 case 'H': /* hostname */
244 if (is_host (optarg
) == FALSE
) {
245 usage2 (_("Invalid hostname/address"), optarg
);
250 if (is_intnonneg (optarg
))
251 port
= atoi (optarg
);
253 usage4 (_("Port must be a positive integer"));
255 case 'u': /* username */
258 case 'p': /* password */
259 password
= strdup(optarg
);
261 /* Delete the password from process list */
262 while (*optarg
!= '\0') {
267 case 'n': /* nas id */
270 case 'F': /* configuration file */
271 config_file
= optarg
;
273 case 'e': /* expect */
276 case 'r': /* retries */
277 if (is_intpos (optarg
))
278 retries
= atoi (optarg
);
280 usage4 (_("Number of retries must be a positive integer"));
282 case 't': /* timeout */
283 if (is_intpos (optarg
))
284 timeout_interval
= atoi (optarg
);
286 usage2 (_("Timeout interval must be a positive integer"), optarg
);
292 usage4 (_("Hostname was not supplied"));
293 if (username
== NULL
)
294 usage4 (_("User not specified"));
295 if (password
== NULL
)
296 usage4 (_("Password not specified"));
297 if (config_file
== NULL
)
298 usage4 (_("Configuration file not specified"));
309 asprintf (&myport
, "%d", PW_AUTH_UDP_PORT
);
311 print_revision (progname
, NP_VERSION
);
313 printf ("Copyright (c) 1999 Robert August Vincent II\n");
314 printf (COPYRIGHT
, copyright
, email
);
316 printf("%s\n", _("Tests to see if a RADIUS server is accepting connections."));
322 printf (_(UT_HELP_VRSN
));
323 printf (_(UT_EXTRA_OPTS
));
325 printf (_(UT_HOST_PORT
), 'P', myport
);
327 printf (" %s\n", "-u, --username=STRING");
328 printf (" %s\n", _("The user to authenticate"));
329 printf (" %s\n", "-p, --password=STRING");
330 printf (" %s\n", _("Password for autentication (SECURITY RISK)"));
331 printf (" %s\n", "-n, --nas-id=STRING");
332 printf (" %s\n", _("NAS identifier"));
333 printf (" %s\n", "-F, --filename=STRING");
334 printf (" %s\n", _("Configuration file"));
335 printf (" %s\n", "-e, --expect=STRING");
336 printf (" %s\n", _("Response string to expect from the server"));
337 printf (" %s\n", "-r, --retries=INTEGER");
338 printf (" %s\n", _("Number of times to retry a failed connection"));
340 printf (_(UT_TIMEOUT
), timeout_interval
);
343 printf ("%s\n", _("This plugin tests a RADIUS server to see if it is accepting connections."));
344 printf ("%s\n", _("The server to test must be specified in the invocation, as well as a user"));
345 printf ("%s\n", _("name and password. A configuration file may also be present. The format of"));
346 printf ("%s\n", _("the configuration file is described in the radiusclient library sources."));
347 printf ("%s\n", _("The password option presents a substantial security issue because the"));
348 printf ("%s\n", _("password can possibly be determined by careful watching of the command line"));
349 printf ("%s\n", _("in a process listing. This risk is exacerbated because nagios will"));
350 printf ("%s\n", _("run the plugin at regular predictable intervals. Please be sure that"));
351 printf ("%s\n", _("the password used does not allow access to sensitive system resources."));
355 printf ("%s\n", _("Notes:"));
356 printf (_(UT_EXTRA_OPTS_NOTES
));
359 printf (_(UT_SUPPORT
));
367 printf (_("Usage:"));
368 printf ("%s -H host -F config_file -u username -p password [-n nas-id] [-P port]\n\
369 [-t timeout] [-r retries] [-e expect]\n", progname
);
374 int my_rc_read_config(char * a
)
376 #ifdef HAVE_LIBRADIUSCLIENT_NG
377 rch
= rc_read_config(a
);
378 return (rch
== NULL
) ? 1 : 0;
380 return rc_read_config(a
);