Code

57b7090d77d2ec7cd5e71b38c5725d0e069c5a37
[nagiosplug.git] / plugins / check_radius.c
1 /*****************************************************************************
2
3 * Nagios check_radius plugin
4
5 * License: GPL
6 * Copyright (c) 1999-2008 Nagios Plugins Development Team
7
8 * Description:
9
10 * This file contains the check_radius plugin
11
12 * Tests to see if a radius server is accepting connections.
13
14
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.
19
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.
24
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/>.
27
28
29 *****************************************************************************/
31 const char *progname = "check_radius";
32 const char *copyright = "2000-2008";
33 const char *email = "nagiosplug-devel@lists.sourceforge.net";
35 #include "common.h"
36 #include "utils.h"
37 #include "netutils.h"
39 #ifdef HAVE_LIBRADIUSCLIENT_NG
40 #include <radiusclient-ng.h>
41 rc_handle *rch = NULL;
42 #else
43 #include <radiusclient.h>
44 #endif
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)
58 #else
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)
65 #endif
66 int my_rc_read_config(char *);
68 char *server = NULL;
69 char *username = NULL;
70 char *password = NULL;
71 char *nasid = NULL;
72 char *expect = NULL;
73 char *config_file = NULL;
74 unsigned short port = PW_AUTH_UDP_PORT;
75 int retries = 1;
76 int verbose = FALSE;
77 ENV *env = NULL;
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
84 entities.
86 Please note that all tags must be lowercase to use the DocBook XML DTD.
88 @@-<article>
90 <sect1>
91 <title>Quick Reference</title>
92 <!-- The refentry forms a manpage -->
93 <refentry>
94 <refmeta>
95 <manvolnum>5<manvolnum>
96 </refmeta>
97 <refnamdiv>
98 <refname>&progname;</refname>
99 <refpurpose>&SUMMARY;</refpurpose>
100 </refnamdiv>
101 </refentry>
102 </sect1>
104 <sect1>
105 <title>FAQ</title>
106 </sect1>
108 <sect1>
109 <title>Theory, Installation, and Operation</title>
111 <sect2>
112 <title>General Description</title>
113 <para>
114 &DESCRIPTION;
115 </para>
116 </sect2>
118 <sect2>
119 <title>Future Enhancements</title>
120 <para>Todo List</para>
121 <itemizedlist>
122 <listitem>Add option to get password from a secured file rather than the command line</listitem>
123 </itemizedlist>
124 </sect2>
127 <sect2>
128 <title>Functions</title>
129 -@@
130 ******************************************************************************/
134 int
135 main (int argc, char **argv)
137         UINT4 service;
138         char msg[BUFFER_LEN];
139         SEND_DATA data;
140         int result = STATE_UNKNOWN;
141         UINT4 client_id;
142         char *str;
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?"));
168         /*
169          * Fill in NAS-IP-Address
170          */
172         if ((client_id = my_rc_own_ipaddress ()) == 0)
173                 return (ERROR_RC);
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,
179                      retries);
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);
194         if (result == OK_RC)
195                 die (STATE_OK, _("Auth OK"));
196         return (0);
201 /* process command-line arguments */
202 int
203 process_arguments (int argc, char **argv)
205         int c;
207         int option = 0;
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'},
221                 {0, 0, 0, 0}
222         };
224         while (1) {
225                 c = getopt_long (argc, argv, "+hVvH:P:F:u:p:n:t:r:e:", longopts,
226                                                                          &option);
228                 if (c == -1 || c == EOF || c == 1)
229                         break;
231                 switch (c) {
232                 case '?':                                                                       /* print short usage statement if args not parsable */
233                         usage5 ();
234                 case 'h':                                                                       /* help */
235                         print_help ();
236                         exit (OK);
237                 case 'V':                                                                       /* version */
238                         print_revision (progname, NP_VERSION);
239                         exit (OK);
240                 case 'v':                                                                       /* verbose mode */
241                         verbose = TRUE;
242                         break;
243                 case 'H':                                                                       /* hostname */
244                         if (is_host (optarg) == FALSE) {
245                                 usage2 (_("Invalid hostname/address"), optarg);
246                         }
247                         server = optarg;
248                         break;
249                 case 'P':                                                                       /* port */
250                         if (is_intnonneg (optarg))
251                                 port = atoi (optarg);
252                         else
253                                 usage4 (_("Port must be a positive integer"));
254                         break;
255                 case 'u':                                                                       /* username */
256                         username = optarg;
257                         break;
258                 case 'p':                                                                       /* password */
259                         password = strdup(optarg);
261                         /* Delete the password from process list */
262                         while (*optarg != '\0') {
263                                 *optarg = 'X';
264                                 optarg++;
265                         }
266                         break;
267                 case 'n':                                                                       /* nas id */
268                         nasid = optarg;
269                         break;
270                 case 'F':                                                                       /* configuration file */
271                         config_file = optarg;
272                         break;
273                 case 'e':                                                                       /* expect */
274                         expect = optarg;
275                         break;
276                 case 'r':                                                                       /* retries */
277                         if (is_intpos (optarg))
278                                 retries = atoi (optarg);
279                         else
280                                 usage4 (_("Number of retries must be a positive integer"));
281                         break;
282                 case 't':                                                                       /* timeout */
283                         if (is_intpos (optarg))
284                                 timeout_interval = atoi (optarg);
285                         else
286                                 usage2 (_("Timeout interval must be a positive integer"), optarg);
287                         break;
288                 }
289         }
291         if (server == NULL)
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"));
300         return OK;
305 void
306 print_help (void)
308         char *myport;
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."));
318   printf ("\n\n");
320         print_usage ();
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);
342   printf ("\n");
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."));
353 #ifdef NP_EXTRA_OPTS
354   printf ("\n");
355   printf ("%s\n", _("Notes:"));
356   printf (_(UT_EXTRA_OPTS_NOTES));
357 #endif
359         printf (_(UT_SUPPORT));
364 void
365 print_usage (void)
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;
379 #else
380         return rc_read_config(a);
381 #endif