Code

Fix Debian bug #482947: No --nas-ip-address option
[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 *nasipaddress = NULL;
73 char *expect = NULL;
74 char *config_file = NULL;
75 unsigned short port = PW_AUTH_UDP_PORT;
76 int retries = 1;
77 int verbose = FALSE;
78 ENV *env = NULL;
80 /******************************************************************************
82 The (psuedo?)literate programming XML is contained within \@\@\- <XML> \-\@\@
83 tags in the comments. With in the tags, the XML is assembled sequentially.
84 You can define entities in tags. You also have all the #defines available as
85 entities.
87 Please note that all tags must be lowercase to use the DocBook XML DTD.
89 @@-<article>
91 <sect1>
92 <title>Quick Reference</title>
93 <!-- The refentry forms a manpage -->
94 <refentry>
95 <refmeta>
96 <manvolnum>5<manvolnum>
97 </refmeta>
98 <refnamdiv>
99 <refname>&progname;</refname>
100 <refpurpose>&SUMMARY;</refpurpose>
101 </refnamdiv>
102 </refentry>
103 </sect1>
105 <sect1>
106 <title>FAQ</title>
107 </sect1>
109 <sect1>
110 <title>Theory, Installation, and Operation</title>
112 <sect2>
113 <title>General Description</title>
114 <para>
115 &DESCRIPTION;
116 </para>
117 </sect2>
119 <sect2>
120 <title>Future Enhancements</title>
121 <para>Todo List</para>
122 <itemizedlist>
123 <listitem>Add option to get password from a secured file rather than the command line</listitem>
124 </itemizedlist>
125 </sect2>
128 <sect2>
129 <title>Functions</title>
130 -@@
131 ******************************************************************************/
135 int
136 main (int argc, char **argv)
138         UINT4 service;
139         char msg[BUFFER_LEN];
140         SEND_DATA data;
141         int result = STATE_UNKNOWN;
142         UINT4 client_id;
143         char *str;
145         setlocale (LC_ALL, "");
146         bindtextdomain (PACKAGE, LOCALEDIR);
147         textdomain (PACKAGE);
149         /* Parse extra opts if any */
150         argv=np_extra_opts (&argc, argv, progname);
152         if (process_arguments (argc, argv) == ERROR)
153                 usage4 (_("Could not parse arguments"));
155         str = strdup ("dictionary");
156         if ((config_file && my_rc_read_config (config_file)) ||
157                         my_rc_read_dictionary (my_rc_conf_str (str)))
158                 die (STATE_UNKNOWN, _("Config file error"));
160         service = PW_AUTHENTICATE_ONLY;
162         memset (&data, 0, sizeof(data));
163         if (!(my_rc_avpair_add (&data.send_pairs, PW_SERVICE_TYPE, &service, 0) &&
164                                 my_rc_avpair_add (&data.send_pairs, PW_USER_NAME, username, 0) &&
165                                 my_rc_avpair_add (&data.send_pairs, PW_USER_PASSWORD, password, 0)
166                                 ))
167                 die (STATE_UNKNOWN, _("Out of Memory?"));
169         if (nasid != NULL) {
170                 if (!(my_rc_avpair_add (&data.send_pairs, PW_NAS_IDENTIFIER, nasid, 0)))
171                         die (STATE_UNKNOWN, _("Invalid NAS-Identifier"));
172         }
174         if (nasipaddress != NULL) {
175                 if (rc_good_ipaddr (nasipaddress))
176                         die (STATE_UNKNOWN, _("Invalid NAS-IP-Address"));
177                 if ((client_id = rc_get_ipaddr(nasipaddress)) == 0)
178                         die (STATE_UNKNOWN, _("Invalid NAS-IP-Address"));
179         } else {
180                 if ((client_id = my_rc_own_ipaddress ()) == 0)
181                         die (STATE_UNKNOWN, _("Can't find local IP for NAS-IP-Address"));
182         }
183         if (my_rc_avpair_add (&(data.send_pairs), PW_NAS_IP_ADDRESS, &client_id, 0) == NULL)
184                 die (STATE_UNKNOWN, _("Invalid NAS-IP-Address"));
186         my_rc_buildreq (&data, PW_ACCESS_REQUEST, server, port, (int)timeout_interval,
187                      retries);
189         result = my_rc_send_server (&data, msg);
190         rc_avpair_free (data.send_pairs);
191         if (data.receive_pairs)
192                 rc_avpair_free (data.receive_pairs);
194         if (result == TIMEOUT_RC)
195                 die (STATE_CRITICAL, _("Timeout"));
196         if (result == ERROR_RC)
197                 die (STATE_CRITICAL, _("Auth Error"));
198         if (result == BADRESP_RC)
199                 die (STATE_WARNING, _("Auth Failed"));
200         if (expect && !strstr (msg, expect))
201                 die (STATE_WARNING, "%s", msg);
202         if (result == OK_RC)
203                 die (STATE_OK, _("Auth OK"));
204         return (0);
209 /* process command-line arguments */
210 int
211 process_arguments (int argc, char **argv)
213         int c;
215         int option = 0;
216         static struct option longopts[] = {
217                 {"hostname", required_argument, 0, 'H'},
218                 {"port", required_argument, 0, 'P'},
219                 {"username", required_argument, 0, 'u'},
220                 {"password", required_argument, 0, 'p'},
221                 {"nas-id", required_argument, 0, 'n'},
222                 {"nas-ip-address", required_argument, 0, 'N'},
223                 {"filename", required_argument, 0, 'F'},
224                 {"expect", required_argument, 0, 'e'},
225                 {"retries", required_argument, 0, 'r'},
226                 {"timeout", required_argument, 0, 't'},
227                 {"verbose", no_argument, 0, 'v'},
228                 {"version", no_argument, 0, 'V'},
229                 {"help", no_argument, 0, 'h'},
230                 {0, 0, 0, 0}
231         };
233         while (1) {
234                 c = getopt_long (argc, argv, "+hVvH:P:F:u:p:n:N:t:r:e:", longopts,
235                                                                          &option);
237                 if (c == -1 || c == EOF || c == 1)
238                         break;
240                 switch (c) {
241                 case '?':                                                                       /* print short usage statement if args not parsable */
242                         usage5 ();
243                 case 'h':                                                                       /* help */
244                         print_help ();
245                         exit (OK);
246                 case 'V':                                                                       /* version */
247                         print_revision (progname, NP_VERSION);
248                         exit (OK);
249                 case 'v':                                                                       /* verbose mode */
250                         verbose = TRUE;
251                         break;
252                 case 'H':                                                                       /* hostname */
253                         if (is_host (optarg) == FALSE) {
254                                 usage2 (_("Invalid hostname/address"), optarg);
255                         }
256                         server = optarg;
257                         break;
258                 case 'P':                                                                       /* port */
259                         if (is_intnonneg (optarg))
260                                 port = atoi (optarg);
261                         else
262                                 usage4 (_("Port must be a positive integer"));
263                         break;
264                 case 'u':                                                                       /* username */
265                         username = optarg;
266                         break;
267                 case 'p':                                                                       /* password */
268                         password = strdup(optarg);
270                         /* Delete the password from process list */
271                         while (*optarg != '\0') {
272                                 *optarg = 'X';
273                                 optarg++;
274                         }
275                         break;
276                 case 'n':                                                                       /* nas id */
277                         nasid = optarg;
278                         break;
279                 case 'N':                                                                       /* nas ip address */
280                         nasipaddress = optarg;
281                         break;
282                 case 'F':                                                                       /* configuration file */
283                         config_file = optarg;
284                         break;
285                 case 'e':                                                                       /* expect */
286                         expect = optarg;
287                         break;
288                 case 'r':                                                                       /* retries */
289                         if (is_intpos (optarg))
290                                 retries = atoi (optarg);
291                         else
292                                 usage4 (_("Number of retries must be a positive integer"));
293                         break;
294                 case 't':                                                                       /* timeout */
295                         if (is_intpos (optarg))
296                                 timeout_interval = atoi (optarg);
297                         else
298                                 usage2 (_("Timeout interval must be a positive integer"), optarg);
299                         break;
300                 }
301         }
303         if (server == NULL)
304                 usage4 (_("Hostname was not supplied"));
305         if (username == NULL)
306                 usage4 (_("User not specified"));
307         if (password == NULL)
308                 usage4 (_("Password not specified"));
309         if (config_file == NULL)
310                 usage4 (_("Configuration file not specified"));
312         return OK;
317 void
318 print_help (void)
320         char *myport;
321         asprintf (&myport, "%d", PW_AUTH_UDP_PORT);
323         print_revision (progname, NP_VERSION);
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));
335         printf (_(UT_EXTRA_OPTS));
337         printf (_(UT_HOST_PORT), 'P', myport);
339         printf (" %s\n", "-u, --username=STRING");
340   printf ("    %s\n", _("The user to authenticate"));
341   printf (" %s\n", "-p, --password=STRING");
342   printf ("    %s\n", _("Password for autentication (SECURITY RISK)"));
343   printf (" %s\n", "-n, --nas-id=STRING");
344   printf ("    %s\n", _("NAS identifier"));
345   printf (" %s\n", "-N, --nas-ip-address=STRING");
346   printf ("    %s\n", _("NAS IP Address"));
347   printf (" %s\n", "-F, --filename=STRING");
348   printf ("    %s\n", _("Configuration file"));
349   printf (" %s\n", "-e, --expect=STRING");
350   printf ("    %s\n", _("Response string to expect from the server"));
351   printf (" %s\n", "-r, --retries=INTEGER");
352   printf ("    %s\n", _("Number of times to retry a failed connection"));
354         printf (_(UT_TIMEOUT), timeout_interval);
356   printf ("\n");
357   printf ("%s\n", _("This plugin tests a RADIUS server to see if it is accepting connections."));
358   printf ("%s\n", _("The server to test must be specified in the invocation, as well as a user"));
359   printf ("%s\n", _("name and password. A configuration file may also be present. The format of"));
360   printf ("%s\n", _("the configuration file is described in the radiusclient library sources."));
361         printf ("%s\n", _("The password option presents a substantial security issue because the"));
362   printf ("%s\n", _("password can possibly be determined by careful watching of the command line"));
363   printf ("%s\n", _("in a process listing. This risk is exacerbated because nagios will"));
364   printf ("%s\n", _("run the plugin at regular predictable intervals. Please be sure that"));
365   printf ("%s\n", _("the password used does not allow access to sensitive system resources."));
367 #ifdef NP_EXTRA_OPTS
368   printf ("\n");
369   printf ("%s\n", _("Notes:"));
370   printf (_(UT_EXTRA_OPTS_NOTES));
371 #endif
373         printf (_(UT_SUPPORT));
378 void
379 print_usage (void)
381   printf (_("Usage:"));
382         printf ("%s -H host -F config_file -u username -p password\n\
383                         [-P port] [-t timeout] [-r retries] [-e expect]\n\
384                         [-n nas-id] [-N nas-ip-addr]\n", progname);
389 int my_rc_read_config(char * a)
391 #ifdef HAVE_LIBRADIUSCLIENT_NG
392         rch = rc_read_config(a);
393         return (rch == NULL) ? 1 : 0;
394 #else
395         return rc_read_config(a);
396 #endif