Code

Added radiusclient-ng support for check_radius. Thanks to Sebastien Guay (#1218438)
[nagiosplug.git] / plugins / check_radius.c
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 #ifdef HAVE_LIBRADIUSCLIENT_NG
47 #include <radiusclient-ng.h>
48 rc_handle *rch = NULL;
49 #else
50 #include <radiusclient.h>
51 #endif
53 int process_arguments (int, char **);
54 void print_help (void);
55 void print_usage (void);
57 /* libradiusclient(-ng) wrapper functions */
58 #ifdef HAVE_LIBRADIUSCLIENT_NG
59 #define my_rc_conf_str(a) rc_conf_str(rch,a)
60 #define my_rc_send_server(a,b) rc_send_server(rch,a,b)
61 #define my_rc_buildreq(a,b,c,d,e,f) rc_buildreq(rch,a,b,c,d,e,f)
62 #define my_rc_own_ipaddress() rc_own_ipaddress(rch)
63 #define my_rc_avpair_add(a,b,c,d) rc_avpair_add(rch,a,b,c,-1,d)
64 #define my_rc_read_dictionary(a) rc_read_dictionary(rch, a)
65 #else
66 #define my_rc_conf_str(a) rc_conf_str(a)
67 #define my_rc_send_server(a,b) rc_send_server(a, b)
68 #define my_rc_buildreq(a,b,c,d,e,f) rc_buildreq(a,b,c,d,e,f)
69 #define my_rc_own_ipaddress() rc_own_ipaddress()
70 #define my_rc_avpair_add(a,b,c,d) rc_avpair_add(a, b, c, d)
71 #define my_rc_read_dictionary(a) rc_read_dictionary(a)
72 #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 *expect = NULL;
80 char *config_file = NULL;
81 unsigned short port = PW_AUTH_UDP_PORT;
82 int retries = 1;
83 int verbose = FALSE;
84 ENV *env = NULL;
86 /******************************************************************************
88 The (psuedo?)literate programming XML is contained within \@\@\- <XML> \-\@\@
89 tags in the comments. With in the tags, the XML is assembled sequentially.
90 You can define entities in tags. You also have all the #defines available as
91 entities.
93 Please note that all tags must be lowercase to use the DocBook XML DTD.
95 @@-<article>
97 <sect1>
98 <title>Quick Reference</title>
99 <!-- The refentry forms a manpage -->
100 <refentry>
101 <refmeta>
102 <manvolnum>5<manvolnum>
103 </refmeta>
104 <refnamdiv>
105 <refname>&progname;</refname>
106 <refpurpose>&SUMMARY;</refpurpose>
107 </refnamdiv>
108 </refentry>
109 </sect1>
111 <sect1>
112 <title>FAQ</title>
113 </sect1>
115 <sect1>
116 <title>Theory, Installation, and Operation</title>
118 <sect2>
119 <title>General Description</title>
120 <para>
121 &DESCRIPTION;
122 </para>
123 </sect2>
125 <sect2>
126 <title>Future Enhancements</title>
127 <para>Todo List</para>
128 <itemizedlist>
129 <listitem>Add option to get password from a secured file rather than the command line</listitem>
130 </itemizedlist>
131 </sect2>
134 <sect2>
135 <title>Functions</title>
136 -@@
137 ******************************************************************************/
141 int
142 main (int argc, char **argv)
144         UINT4 service;
145         char msg[BUFFER_LEN];
146         SEND_DATA data;
147         int result = STATE_UNKNOWN;
148         UINT4 client_id;
149         char *str;
151         setlocale (LC_ALL, "");
152         bindtextdomain (PACKAGE, LOCALEDIR);
153         textdomain (PACKAGE);
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?"));
172         /* 
173          * Fill in NAS-IP-Address 
174          */
176         if ((client_id = my_rc_own_ipaddress ()) == 0)
177                 return (ERROR_RC);
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,
183                      retries);
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);
198         if (result == OK_RC)
199                 die (STATE_OK, _("Auth OK"));
200         return (0);
205 /* process command-line arguments */
206 int
207 process_arguments (int argc, char **argv)
209         int c;
211         int option = 0;
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'},
225                 {0, 0, 0, 0}
226         };
228         if (argc < 2)
229                 return ERROR;
231         if (argc == 9) {
232                 config_file = argv[1];
233                 username = argv[2];
234                 password = argv[3];
235                 if (is_intpos (argv[4]))
236                         timeout_interval = atoi (argv[4]);
237                 else
238                         usage2 (_("Timeout interval must be a positive integer"), optarg);
239                 if (is_intpos (argv[5]))
240                         retries = atoi (argv[5]);
241                 else
242                         usage4 (_("Number of retries must be a positive integer"));
243                 server = argv[6];
244                 if (is_intpos (argv[7]))
245                         port = atoi (argv[7]);
246                 else
247                         usage4 (_("Port must be a positive integer"));
248                 expect = argv[8];
249                 return OK;
250         }
252         while (1) {
253                 c = getopt_long (argc, argv, "+hVvH:P:F:u:p:n:t:r:e:", longopts,
254                                                                          &option);
256                 if (c == -1 || c == EOF || c == 1)
257                         break;
259                 switch (c) {
260                 case '?':                                                                       /* print short usage statement if args not parsable */
261                         usage5 ();
262                 case 'h':                                                                       /* help */
263                         print_help ();
264                         exit (OK);
265                 case 'V':                                                                       /* version */
266                         print_revision (progname, revision);
267                         exit (OK);
268                 case 'v':                                                                       /* verbose mode */
269                         verbose = TRUE;
270                         break;
271                 case 'H':                                                                       /* hostname */
272                         if (is_host (optarg) == FALSE) {
273                                 usage2 (_("Invalid hostname/address"), optarg);
274                         }
275                         server = optarg;
276                         break;
277                 case 'P':                                                                       /* port */
278                         if (is_intnonneg (optarg))
279                                 port = atoi (optarg);
280                         else
281                                 usage4 (_("Port must be a positive integer"));
282                         break;
283                 case 'u':                                                                       /* username */
284                         username = optarg;
285                         break;
286                 case 'p':                                                                       /* password */
287                         password = optarg;
288                         break;
289                 case 'n':                                                                       /* nas id */
290                         nasid = 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         }
312         return OK;
317 void
318 print_help (void)
320         char *myport;
321         asprintf (&myport, "%d", PW_AUTH_UDP_PORT);
323         print_revision (progname, revision);
325         printf ("Copyright (c) 1999 Robert August Vincent II\n");
326         printf (COPYRIGHT, copyright, email);
328         printf("%s\n", _("Tests to see if a radius server is accepting connections."));
330   printf ("\n\n");
332         print_usage ();
334         printf (_(UT_HELP_VRSN));
336         printf (_(UT_HOST_PORT), 'P', myport);
338         printf (" %s\n", "-u, --username=STRING");
339   printf ("    %s\n", _("The user to authenticate"));
340   printf (" %s\n", "-p, --password=STRING");
341   printf ("    %s\n", _("Password for autentication (SECURITY RISK)"));
342   printf (" %s\n", "-n, --nas-id=STRING");
343   printf ("    %s\n", _("NAS identifier"));
344   printf (" %s\n", "-F, --filename=STRING");
345   printf ("    %s\n", _("Configuration file"));
346   printf (" %s\n", "-e, --expect=STRING");
347   printf ("    %s\n", _("Response string to expect from the server"));
348   printf (" %s\n", "-r, --retries=INTEGER");
349   printf ("    %s\n", _("Number of times to retry a failed connection"));
351         printf (_(UT_TIMEOUT), timeout_interval);
353         printf ("%s\n", _("This plugin tests a radius server to see if it is accepting connections."));
354   printf ("%s\n", _("The server to test must be specified in the invocation, as well as a user"));
355   printf ("%s\n", _("name and password. A configuration file may also be present. The format of"));
356   printf ("%s\n", _("the configuration file is described in the radiusclient library sources."));
357         printf ("%s\n", _("The password option presents a substantial security issue because the"));
358   printf ("%s\n", _("password can be determined by careful watching of the command line in"));
359   printf ("%s\n", _("a process listing.  This risk is exacerbated because nagios will"));
360   printf ("%s\n", _("run the plugin at regular prdictable intervals.  Please be sure that"));
361   printf ("%s\n", _("the password used does not allow access to sensitive system resources,"));
362   printf ("%s\n", _("otherwise compormise could occur."));
364         printf (_(UT_SUPPORT));
369 void
370 print_usage (void)
372   printf (_("Usage:"));
373         printf ("%s -H host -F config_file -u username -p password [-n nas-id] [-P port]\n\
374                   [-t timeout] [-r retries] [-e expect]\n", progname);
379 int my_rc_read_config(char * a)
381 #ifdef HAVE_LIBRADIUSCLIENT_NG
382         rch = rc_read_config(a);
383         return (rch == NULL) ? 1 : 0;
384 #else
385         return rc_read_config(a);
386 #endif