1 /*****************************************************************************
3 * Nagios check_radius plugin
6 * Copyright (c) 1999-2008 Nagios Plugins Development Team
8 * Last Modified: $Date$
12 * This file contains the check_radius plugin
14 * Tests to see if a radius server is accepting connections.
17 * This program is free software: you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation, either version 3 of the License, or
20 * (at your option) any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
27 * You should have received a copy of the GNU General Public License
28 * along with this program. If not, see <http://www.gnu.org/licenses/>.
32 *****************************************************************************/
34 const char *progname
= "check_radius";
35 const char *revision
= "$Revision$";
36 const char *copyright
= "2000-2008";
37 const char *email
= "nagiosplug-devel@lists.sourceforge.net";
43 #ifdef HAVE_LIBRADIUSCLIENT_NG
44 #include <radiusclient-ng.h>
45 rc_handle
*rch
= NULL
;
47 #include <radiusclient.h>
50 int process_arguments (int, char **);
51 void print_help (void);
52 void print_usage (void);
54 /* libradiusclient(-ng) wrapper functions */
55 #ifdef HAVE_LIBRADIUSCLIENT_NG
56 #define my_rc_conf_str(a) rc_conf_str(rch,a)
57 #define my_rc_send_server(a,b) rc_send_server(rch,a,b)
58 #define my_rc_buildreq(a,b,c,d,e,f) rc_buildreq(rch,a,b,c,d,e,f)
59 #define my_rc_own_ipaddress() rc_own_ipaddress(rch)
60 #define my_rc_avpair_add(a,b,c,d) rc_avpair_add(rch,a,b,c,-1,d)
61 #define my_rc_read_dictionary(a) rc_read_dictionary(rch, a)
63 #define my_rc_conf_str(a) rc_conf_str(a)
64 #define my_rc_send_server(a,b) rc_send_server(a, b)
65 #define my_rc_buildreq(a,b,c,d,e,f) rc_buildreq(a,b,c,d,e,f)
66 #define my_rc_own_ipaddress() rc_own_ipaddress()
67 #define my_rc_avpair_add(a,b,c,d) rc_avpair_add(a, b, c, d)
68 #define my_rc_read_dictionary(a) rc_read_dictionary(a)
70 int my_rc_read_config(char *);
73 char *username
= NULL
;
74 char *password
= NULL
;
77 char *config_file
= NULL
;
78 unsigned short port
= PW_AUTH_UDP_PORT
;
83 /******************************************************************************
85 The (psuedo?)literate programming XML is contained within \@\@\- <XML> \-\@\@
86 tags in the comments. With in the tags, the XML is assembled sequentially.
87 You can define entities in tags. You also have all the #defines available as
90 Please note that all tags must be lowercase to use the DocBook XML DTD.
95 <title>Quick Reference</title>
96 <!-- The refentry forms a manpage -->
99 <manvolnum>5<manvolnum>
102 <refname>&progname;</refname>
103 <refpurpose>&SUMMARY;</refpurpose>
113 <title>Theory, Installation, and Operation</title>
116 <title>General Description</title>
123 <title>Future Enhancements</title>
124 <para>Todo List</para>
126 <listitem>Add option to get password from a secured file rather than the command line</listitem>
132 <title>Functions</title>
134 ******************************************************************************/
139 main (int argc
, char **argv
)
142 char msg
[BUFFER_LEN
];
144 int result
= STATE_UNKNOWN
;
148 setlocale (LC_ALL
, "");
149 bindtextdomain (PACKAGE
, LOCALEDIR
);
150 textdomain (PACKAGE
);
152 /* Parse extra opts if any */
153 argv
=np_extra_opts (&argc
, argv
, progname
);
155 if (process_arguments (argc
, argv
) == ERROR
)
156 usage4 (_("Could not parse arguments"));
158 str
= strdup ("dictionary");
159 if ((config_file
&& my_rc_read_config (config_file
)) ||
160 my_rc_read_dictionary (my_rc_conf_str (str
)))
161 die (STATE_UNKNOWN
, _("Config file error"));
163 service
= PW_AUTHENTICATE_ONLY
;
165 memset (&data
, 0, sizeof(data
));
166 if (!(my_rc_avpair_add (&data
.send_pairs
, PW_SERVICE_TYPE
, &service
, 0) &&
167 my_rc_avpair_add (&data
.send_pairs
, PW_USER_NAME
, username
, 0) &&
168 my_rc_avpair_add (&data
.send_pairs
, PW_USER_PASSWORD
, password
, 0) &&
169 (nasid
==NULL
|| my_rc_avpair_add (&data
.send_pairs
, PW_NAS_IDENTIFIER
, nasid
, 0))))
170 die (STATE_UNKNOWN
, _("Out of Memory?"));
173 * Fill in NAS-IP-Address
176 if ((client_id
= my_rc_own_ipaddress ()) == 0)
179 if (my_rc_avpair_add (&(data
.send_pairs
), PW_NAS_IP_ADDRESS
, &client_id
, 0) ==
180 NULL
) return (ERROR_RC
);
182 my_rc_buildreq (&data
, PW_ACCESS_REQUEST
, server
, port
, (int)timeout_interval
,
185 result
= my_rc_send_server (&data
, msg
);
186 rc_avpair_free (data
.send_pairs
);
187 if (data
.receive_pairs
)
188 rc_avpair_free (data
.receive_pairs
);
190 if (result
== TIMEOUT_RC
)
191 die (STATE_CRITICAL
, _("Timeout"));
192 if (result
== ERROR_RC
)
193 die (STATE_CRITICAL
, _("Auth Error"));
194 if (result
== BADRESP_RC
)
195 die (STATE_WARNING
, _("Auth Failed"));
196 if (expect
&& !strstr (msg
, expect
))
197 die (STATE_WARNING
, "%s", msg
);
199 die (STATE_OK
, _("Auth OK"));
205 /* process command-line arguments */
207 process_arguments (int argc
, char **argv
)
212 static struct option longopts
[] = {
213 {"hostname", required_argument
, 0, 'H'},
214 {"port", required_argument
, 0, 'P'},
215 {"username", required_argument
, 0, 'u'},
216 {"password", required_argument
, 0, 'p'},
217 {"nas-id", required_argument
, 0, 'n'},
218 {"filename", required_argument
, 0, 'F'},
219 {"expect", required_argument
, 0, 'e'},
220 {"retries", required_argument
, 0, 'r'},
221 {"timeout", required_argument
, 0, 't'},
222 {"verbose", no_argument
, 0, 'v'},
223 {"version", no_argument
, 0, 'V'},
224 {"help", no_argument
, 0, 'h'},
229 c
= getopt_long (argc
, argv
, "+hVvH:P:F:u:p:n:t:r:e:", longopts
,
232 if (c
== -1 || c
== EOF
|| c
== 1)
236 case '?': /* print short usage statement if args not parsable */
241 case 'V': /* version */
242 print_revision (progname
, revision
);
244 case 'v': /* verbose mode */
247 case 'H': /* hostname */
248 if (is_host (optarg
) == FALSE
) {
249 usage2 (_("Invalid hostname/address"), optarg
);
254 if (is_intnonneg (optarg
))
255 port
= atoi (optarg
);
257 usage4 (_("Port must be a positive integer"));
259 case 'u': /* username */
262 case 'p': /* password */
263 password
= strdup(optarg
);
265 /* Delete the password from process list */
266 while (*optarg
!= '\0') {
271 case 'n': /* nas id */
274 case 'F': /* configuration file */
275 config_file
= optarg
;
277 case 'e': /* expect */
280 case 'r': /* retries */
281 if (is_intpos (optarg
))
282 retries
= atoi (optarg
);
284 usage4 (_("Number of retries must be a positive integer"));
286 case 't': /* timeout */
287 if (is_intpos (optarg
))
288 timeout_interval
= atoi (optarg
);
290 usage2 (_("Timeout interval must be a positive integer"), optarg
);
296 usage4 (_("Hostname was not supplied"));
297 if (username
== NULL
)
298 usage4 (_("User not specified"));
299 if (password
== NULL
)
300 usage4 (_("Password not specified"));
301 if (config_file
== NULL
)
302 usage4 (_("Configuration file not specified"));
313 asprintf (&myport
, "%d", PW_AUTH_UDP_PORT
);
315 print_revision (progname
, revision
);
317 printf ("Copyright (c) 1999 Robert August Vincent II\n");
318 printf (COPYRIGHT
, copyright
, email
);
320 printf("%s\n", _("Tests to see if a radius server is accepting connections."));
326 printf (_(UT_HELP_VRSN
));
327 printf (_(UT_EXTRA_OPTS
));
329 printf (_(UT_HOST_PORT
), 'P', myport
);
331 printf (" %s\n", "-u, --username=STRING");
332 printf (" %s\n", _("The user to authenticate"));
333 printf (" %s\n", "-p, --password=STRING");
334 printf (" %s\n", _("Password for autentication (SECURITY RISK)"));
335 printf (" %s\n", "-n, --nas-id=STRING");
336 printf (" %s\n", _("NAS identifier"));
337 printf (" %s\n", "-F, --filename=STRING");
338 printf (" %s\n", _("Configuration file"));
339 printf (" %s\n", "-e, --expect=STRING");
340 printf (" %s\n", _("Response string to expect from the server"));
341 printf (" %s\n", "-r, --retries=INTEGER");
342 printf (" %s\n", _("Number of times to retry a failed connection"));
344 printf (_(UT_TIMEOUT
), timeout_interval
);
347 printf ("%s\n", _("This plugin tests a radius server to see if it is accepting connections."));
348 printf ("%s\n", _("The server to test must be specified in the invocation, as well as a user"));
349 printf ("%s\n", _("name and password. A configuration file may also be present. The format of"));
350 printf ("%s\n", _("the configuration file is described in the radiusclient library sources."));
351 printf ("%s\n", _("The password option presents a substantial security issue because the"));
352 printf ("%s\n", _("password can possibly be determined by careful watching of the command line"));
353 printf ("%s\n", _("in a process listing. This risk is exacerbated because nagios will"));
354 printf ("%s\n", _("run the plugin at regular predictable intervals. Please be sure that"));
355 printf ("%s\n", _("the password used does not allow access to sensitive system resources."));
359 printf ("%s\n", _("Notes:"));
360 printf (_(UT_EXTRA_OPTS_NOTES
));
363 printf (_(UT_SUPPORT
));
371 printf (_("Usage:"));
372 printf ("%s -H host -F config_file -u username -p password [-n nas-id] [-P port]\n\
373 [-t timeout] [-r retries] [-e expect]\n", progname
);
378 int my_rc_read_config(char * a
)
380 #ifdef HAVE_LIBRADIUSCLIENT_NG
381 rch
= rc_read_config(a
);
382 return (rch
== NULL
) ? 1 : 0;
384 return rc_read_config(a
);