Code

Correct typo and make the --help notes a bit simpler.
[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 * 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 * 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.
21
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.
26
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/>.
29
30 * $Id$
31
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";
39 #include "common.h"
40 #include "utils.h"
41 #include "netutils.h"
43 #ifdef HAVE_LIBRADIUSCLIENT_NG
44 #include <radiusclient-ng.h>
45 rc_handle *rch = NULL;
46 #else
47 #include <radiusclient.h>
48 #endif
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)
62 #else
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)
69 #endif
70 int my_rc_read_config(char *);
72 char *server = NULL;
73 char *username = NULL;
74 char *password = NULL;
75 char *nasid = NULL;
76 char *expect = NULL;
77 char *config_file = NULL;
78 unsigned short port = PW_AUTH_UDP_PORT;
79 int retries = 1;
80 int verbose = FALSE;
81 ENV *env = NULL;
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
88 entities.
90 Please note that all tags must be lowercase to use the DocBook XML DTD.
92 @@-<article>
94 <sect1>
95 <title>Quick Reference</title>
96 <!-- The refentry forms a manpage -->
97 <refentry>
98 <refmeta>
99 <manvolnum>5<manvolnum>
100 </refmeta>
101 <refnamdiv>
102 <refname>&progname;</refname>
103 <refpurpose>&SUMMARY;</refpurpose>
104 </refnamdiv>
105 </refentry>
106 </sect1>
108 <sect1>
109 <title>FAQ</title>
110 </sect1>
112 <sect1>
113 <title>Theory, Installation, and Operation</title>
115 <sect2>
116 <title>General Description</title>
117 <para>
118 &DESCRIPTION;
119 </para>
120 </sect2>
122 <sect2>
123 <title>Future Enhancements</title>
124 <para>Todo List</para>
125 <itemizedlist>
126 <listitem>Add option to get password from a secured file rather than the command line</listitem>
127 </itemizedlist>
128 </sect2>
131 <sect2>
132 <title>Functions</title>
133 -@@
134 ******************************************************************************/
138 int
139 main (int argc, char **argv)
141         UINT4 service;
142         char msg[BUFFER_LEN];
143         SEND_DATA data;
144         int result = STATE_UNKNOWN;
145         UINT4 client_id;
146         char *str;
148         setlocale (LC_ALL, "");
149         bindtextdomain (PACKAGE, LOCALEDIR);
150         textdomain (PACKAGE);
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                                 (nasid==NULL || my_rc_avpair_add (&data.send_pairs, PW_NAS_IDENTIFIER, nasid, 0))))
167                 die (STATE_UNKNOWN, _("Out of Memory?"));
169         /* 
170          * Fill in NAS-IP-Address 
171          */
173         if ((client_id = my_rc_own_ipaddress ()) == 0)
174                 return (ERROR_RC);
176         if (my_rc_avpair_add (&(data.send_pairs), PW_NAS_IP_ADDRESS, &client_id, 0) ==
177                         NULL) return (ERROR_RC);
179         my_rc_buildreq (&data, PW_ACCESS_REQUEST, server, port, (int)timeout_interval,
180                      retries);
182         result = my_rc_send_server (&data, msg);
183         rc_avpair_free (data.send_pairs);
184         if (data.receive_pairs)
185                 rc_avpair_free (data.receive_pairs);
187         if (result == TIMEOUT_RC)
188                 die (STATE_CRITICAL, _("Timeout"));
189         if (result == ERROR_RC)
190                 die (STATE_CRITICAL, _("Auth Error"));
191         if (result == BADRESP_RC)
192                 die (STATE_WARNING, _("Auth Failed"));
193         if (expect && !strstr (msg, expect))
194                 die (STATE_WARNING, "%s", msg);
195         if (result == OK_RC)
196                 die (STATE_OK, _("Auth OK"));
197         return (0);
202 /* process command-line arguments */
203 int
204 process_arguments (int argc, char **argv)
206         int c;
208         int option = 0;
209         static struct option longopts[] = {
210                 {"hostname", required_argument, 0, 'H'},
211                 {"port", required_argument, 0, 'P'},
212                 {"username", required_argument, 0, 'u'},
213                 {"password", required_argument, 0, 'p'},
214                 {"nas-id", required_argument, 0, 'n'},
215                 {"filename", required_argument, 0, 'F'},
216                 {"expect", required_argument, 0, 'e'},
217                 {"retries", required_argument, 0, 'r'},
218                 {"timeout", required_argument, 0, 't'},
219                 {"verbose", no_argument, 0, 'v'},
220                 {"version", no_argument, 0, 'V'},
221                 {"help", no_argument, 0, 'h'},
222                 {0, 0, 0, 0}
223         };
225         while (1) {
226                 c = getopt_long (argc, argv, "+hVvH:P:F:u:p:n:t:r:e:", longopts,
227                                                                          &option);
229                 if (c == -1 || c == EOF || c == 1)
230                         break;
232                 switch (c) {
233                 case '?':                                                                       /* print short usage statement if args not parsable */
234                         usage5 ();
235                 case 'h':                                                                       /* help */
236                         print_help ();
237                         exit (OK);
238                 case 'V':                                                                       /* version */
239                         print_revision (progname, revision);
240                         exit (OK);
241                 case 'v':                                                                       /* verbose mode */
242                         verbose = TRUE;
243                         break;
244                 case 'H':                                                                       /* hostname */
245                         if (is_host (optarg) == FALSE) {
246                                 usage2 (_("Invalid hostname/address"), optarg);
247                         }
248                         server = optarg;
249                         break;
250                 case 'P':                                                                       /* port */
251                         if (is_intnonneg (optarg))
252                                 port = atoi (optarg);
253                         else
254                                 usage4 (_("Port must be a positive integer"));
255                         break;
256                 case 'u':                                                                       /* username */
257                         username = optarg;
258                         break;
259                 case 'p':                                                                       /* password */
260                         password = optarg;
261                         break;
262                 case 'n':                                                                       /* nas id */
263                         nasid = optarg;
264                         break;
265                 case 'F':                                                                       /* configuration file */
266                         config_file = optarg;
267                         break;
268                 case 'e':                                                                       /* expect */
269                         expect = optarg;
270                         break;
271                 case 'r':                                                                       /* retries */
272                         if (is_intpos (optarg))
273                                 retries = atoi (optarg);
274                         else
275                                 usage4 (_("Number of retries must be a positive integer"));
276                         break;
277                 case 't':                                                                       /* timeout */
278                         if (is_intpos (optarg))
279                                 timeout_interval = atoi (optarg);
280                         else
281                                 usage2 (_("Timeout interval must be a positive integer"), optarg);
282                         break;
283                 }
284         }
286         if (server == NULL)
287                 usage4 (_("Hostname was not supplied"));
288         if (username == NULL)
289                 usage4 (_("User not specified"));
290         if (password == NULL)
291                 usage4 (_("Password not specified"));
292         if (config_file == NULL)
293                 usage4 (_("Configuration file not specified"));
295         return OK;
300 void
301 print_help (void)
303         char *myport;
304         asprintf (&myport, "%d", PW_AUTH_UDP_PORT);
306         print_revision (progname, revision);
308         printf ("Copyright (c) 1999 Robert August Vincent II\n");
309         printf (COPYRIGHT, copyright, email);
311         printf("%s\n", _("Tests to see if a radius server is accepting connections."));
313   printf ("\n\n");
315         print_usage ();
317         printf (_(UT_HELP_VRSN));
319         printf (_(UT_HOST_PORT), 'P', myport);
321         printf (" %s\n", "-u, --username=STRING");
322   printf ("    %s\n", _("The user to authenticate"));
323   printf (" %s\n", "-p, --password=STRING");
324   printf ("    %s\n", _("Password for autentication (SECURITY RISK)"));
325   printf (" %s\n", "-n, --nas-id=STRING");
326   printf ("    %s\n", _("NAS identifier"));
327   printf (" %s\n", "-F, --filename=STRING");
328   printf ("    %s\n", _("Configuration file"));
329   printf (" %s\n", "-e, --expect=STRING");
330   printf ("    %s\n", _("Response string to expect from the server"));
331   printf (" %s\n", "-r, --retries=INTEGER");
332   printf ("    %s\n", _("Number of times to retry a failed connection"));
334         printf (_(UT_TIMEOUT), timeout_interval);
336         printf ("%s\n", _("This plugin tests a radius server to see if it is accepting connections."));
337   printf ("%s\n", _("The server to test must be specified in the invocation, as well as a user"));
338   printf ("%s\n", _("name and password. A configuration file may also be present. The format of"));
339   printf ("%s\n", _("the configuration file is described in the radiusclient library sources."));
340         printf ("%s\n", _("The password option presents a substantial security issue because the"));
341   printf ("%s\n", _("password can be determined by careful watching of the command line in"));
342   printf ("%s\n", _("a process listing.  This risk is exacerbated because nagios will"));
343   printf ("%s\n", _("run the plugin at regular predictable intervals.  Please be sure that"));
344   printf ("%s\n", _("the password used does not allow access to sensitive system resources."));
346         printf (_(UT_SUPPORT));
351 void
352 print_usage (void)
354   printf (_("Usage:"));
355         printf ("%s -H host -F config_file -u username -p password [-n nas-id] [-P port]\n\
356                   [-t timeout] [-r retries] [-e expect]\n", progname);
361 int my_rc_read_config(char * a)
363 #ifdef HAVE_LIBRADIUSCLIENT_NG
364         rch = rc_read_config(a);
365         return (rch == NULL) ? 1 : 0;
366 #else
367         return rc_read_config(a);
368 #endif