Code

another round of localization cleaning
[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 #include <radiusclient.h>
48 int process_arguments (int, char **);
49 void print_help (void);
50 void print_usage (void);
52 char *server = NULL;
53 char *username = NULL;
54 char *password = NULL;
55 char *nasid = NULL;
56 char *expect = NULL;
57 char *config_file = NULL;
58 unsigned short port = PW_AUTH_UDP_PORT;
59 int retries = 1;
60 int verbose = FALSE;
61 ENV *env = NULL;
63 /******************************************************************************
65 The (psuedo?)literate programming XML is contained within \@\@\- <XML> \-\@\@
66 tags in the comments. With in the tags, the XML is assembled sequentially.
67 You can define entities in tags. You also have all the #defines available as
68 entities.
70 Please note that all tags must be lowercase to use the DocBook XML DTD.
72 @@-<article>
74 <sect1>
75 <title>Quick Reference</title>
76 <!-- The refentry forms a manpage -->
77 <refentry>
78 <refmeta>
79 <manvolnum>5<manvolnum>
80 </refmeta>
81 <refnamdiv>
82 <refname>&progname;</refname>
83 <refpurpose>&SUMMARY;</refpurpose>
84 </refnamdiv>
85 </refentry>
86 </sect1>
88 <sect1>
89 <title>FAQ</title>
90 </sect1>
92 <sect1>
93 <title>Theory, Installation, and Operation</title>
95 <sect2>
96 <title>General Description</title>
97 <para>
98 &DESCRIPTION;
99 </para>
100 </sect2>
102 <sect2>
103 <title>Future Enhancements</title>
104 <para>Todo List</para>
105 <itemizedlist>
106 <listitem>Add option to get password from a secured file rather than the command line</listitem>
107 </itemizedlist>
108 </sect2>
111 <sect2>
112 <title>Functions</title>
113 -@@
114 ******************************************************************************/
118 int
119 main (int argc, char **argv)
121         UINT4 service;
122         char msg[BUFFER_LEN];
123         SEND_DATA data;
124         int result = STATE_UNKNOWN;
125         UINT4 client_id;
126         char *str;
128         setlocale (LC_ALL, "");
129         bindtextdomain (PACKAGE, LOCALEDIR);
130         textdomain (PACKAGE);
132         if (process_arguments (argc, argv) == ERROR)
133                 usage4 (_("Could not parse arguments"));
135         str = strdup ("dictionary");
136         if ((config_file && rc_read_config (config_file)) ||
137                         rc_read_dictionary (rc_conf_str (str)))
138                 die (STATE_UNKNOWN, _("Config file error"));
140         service = PW_AUTHENTICATE_ONLY;
142         if (!(rc_avpair_add (&data.send_pairs, PW_SERVICE_TYPE, &service, 0) &&
143                                 rc_avpair_add (&data.send_pairs, PW_USER_NAME, username, 0) &&
144                                 rc_avpair_add (&data.send_pairs, PW_USER_PASSWORD, password, 0) &&
145                                 (nasid==NULL || rc_avpair_add (&data.send_pairs, PW_NAS_IDENTIFIER, nasid, 0))))
146                 die (STATE_UNKNOWN, _("Out of Memory?"));
148         /* 
149          * Fill in NAS-IP-Address 
150          */
152         if ((client_id = rc_own_ipaddress ()) == 0)
153                 return (ERROR_RC);
155         if (rc_avpair_add (&(data.send_pairs), PW_NAS_IP_ADDRESS, &client_id, 0) ==
156                         NULL) return (ERROR_RC);
158         rc_buildreq (&data, PW_ACCESS_REQUEST, server, port, (int)timeout_interval,
159                      retries);
161         result = rc_send_server (&data, msg);
162         rc_avpair_free (data.send_pairs);
163         if (data.receive_pairs)
164                 rc_avpair_free (data.receive_pairs);
166         if (result == TIMEOUT_RC)
167                 die (STATE_CRITICAL, _("Timeout"));
168         if (result == ERROR_RC)
169                 die (STATE_CRITICAL, _("Auth Error"));
170         if (result == BADRESP_RC)
171                 die (STATE_WARNING, _("Auth Failed"));
172         if (expect && !strstr (msg, expect))
173                 die (STATE_WARNING, "%s", msg);
174         if (result == OK_RC)
175                 die (STATE_OK, _("Auth OK"));
176         return (0);
181 /* process command-line arguments */
182 int
183 process_arguments (int argc, char **argv)
185         int c;
187         int option = 0;
188         static struct option longopts[] = {
189                 {"hostname", required_argument, 0, 'H'},
190                 {"port", required_argument, 0, 'P'},
191                 {"username", required_argument, 0, 'u'},
192                 {"password", required_argument, 0, 'p'},
193                 {"nas-id", required_argument, 0, 'n'},
194                 {"filename", required_argument, 0, 'F'},
195                 {"expect", required_argument, 0, 'e'},
196                 {"retries", required_argument, 0, 'r'},
197                 {"timeout", required_argument, 0, 't'},
198                 {"verbose", no_argument, 0, 'v'},
199                 {"version", no_argument, 0, 'V'},
200                 {"help", no_argument, 0, 'h'},
201                 {0, 0, 0, 0}
202         };
204         if (argc < 2)
205                 return ERROR;
207         if (argc == 9) {
208                 config_file = argv[1];
209                 username = argv[2];
210                 password = argv[3];
211                 if (is_intpos (argv[4]))
212                         timeout_interval = atoi (argv[4]);
213                 else
214                         usage2 (_("Timeout interval must be a positive integer"), optarg);
215                 if (is_intpos (argv[5]))
216                         retries = atoi (argv[5]);
217                 else
218                         usage4 (_("Number of retries must be a positive integer"));
219                 server = argv[6];
220                 if (is_intpos (argv[7]))
221                         port = atoi (argv[7]);
222                 else
223                         usage4 (_("Port must be a positive integer"));
224                 expect = argv[8];
225                 return OK;
226         }
228         while (1) {
229                 c = getopt_long (argc, argv, "+hVvH:P:F:u:p:n:t:r:e:", longopts,
230                                                                          &option);
232                 if (c == -1 || c == EOF || c == 1)
233                         break;
235                 switch (c) {
236                 case '?':                                                                       /* print short usage statement if args not parsable */
237                         usage2 (_("Unknown argument"), optarg);
238                 case 'h':                                                                       /* help */
239                         print_help ();
240                         exit (OK);
241                 case 'V':                                                                       /* version */
242                         print_revision (progname, revision);
243                         exit (OK);
244                 case 'v':                                                                       /* verbose mode */
245                         verbose = TRUE;
246                         break;
247                 case 'H':                                                                       /* hostname */
248                         if (is_host (optarg) == FALSE) {
249                                 usage2 (_("Invalid hostname/address"), optarg);
250                         }
251                         server = optarg;
252                         break;
253                 case 'P':                                                                       /* port */
254                         if (is_intnonneg (optarg))
255                                 port = atoi (optarg);
256                         else
257                                 usage4 (_("Port must be a positive integer"));
258                         break;
259                 case 'u':                                                                       /* username */
260                         username = optarg;
261                         break;
262                 case 'p':                                                                       /* password */
263                         password = optarg;
264                         break;
265                 case 'n':                                                                       /* nas id */
266                         nasid = optarg;
267                         break;
268                 case 'F':                                                                       /* configuration file */
269                         config_file = optarg;
270                         break;
271                 case 'e':                                                                       /* expect */
272                         expect = optarg;
273                         break;
274                 case 'r':                                                                       /* retries */
275                         if (is_intpos (optarg))
276                                 retries = atoi (optarg);
277                         else
278                                 usage4 (_("Number of retries must be a positive integer"));
279                         break;
280                 case 't':                                                                       /* timeout */
281                         if (is_intpos (optarg))
282                                 timeout_interval = atoi (optarg);
283                         else
284                                 usage2 (_("Timeout interval must be a positive integer"), optarg);
285                         break;
286                 }
287         }
288         return OK;
293 void
294 print_help (void)
296         char *myport;
297         asprintf (&myport, "%d", PW_AUTH_UDP_PORT);
299         print_revision (progname, revision);
301         printf ("Copyright (c) 1999 Robert August Vincent II\n");
302         printf (COPYRIGHT, copyright, email);
304         printf("%s\n", _("Tests to see if a radius server is accepting connections."));
306   printf ("\n\n");
308         print_usage ();
310         printf (_(UT_HELP_VRSN));
312         printf (_(UT_HOST_PORT), 'P', myport);
314         printf (" %s\n", "-u, --username=STRING");
315   printf ("    %s\n", _("The user to authenticate"));
316   printf (" %s\n", "-p, --password=STRING");
317   printf ("    %s\n", _("Password for autentication (SECURITY RISK)"));
318   printf (" %s\n", "-n, --nas-id=STRING");
319   printf ("    %s\n", _("NAS identifier"));
320   printf (" %s\n", "-F, --filename=STRING");
321   printf ("    %s\n", _("Configuration file"));
322   printf (" %s\n", "-e, --expect=STRING");
323   printf ("    %s\n", _("Response string to expect from the server"));
324   printf (" %s\n", "-r, --retries=INTEGER");
325   printf ("    %s\n", _("Number of times to retry a failed connection"));
327         printf (_(UT_TIMEOUT), timeout_interval);
329         printf ("%s\n", _("This plugin tests a radius server to see if it is accepting connections."));
330   printf ("%s\n", _("The server to test must be specified in the invocation, as well as a user"));
331   printf ("%s\n", _("name and password. A configuration file may also be present. The format of"));
332   printf ("%s\n", _("the configuration file is described in the radiusclient library sources."));
333         printf ("%s\n", _("The password option presents a substantial security issue because the"));
334   printf ("%s\n", _("password can be determined by careful watching of the command line in"));
335   printf ("%s\n", _("a process listing.  This risk is exacerbated because nagios will"));
336   printf ("%s\n", _("run the plugin at regular prdictable intervals.  Please be sure that"));
337   printf ("%s\n", _("the password used does not allow access to sensitive system resources,"));
338   printf ("%s\n", _("otherwise compormise could occur."));
340         printf (_(UT_SUPPORT));
345 void
346 print_usage (void)
348   printf (_("Usage:"));
349         printf ("%s -H host -F config_file -u username -p password [-n nas-id] [-P port]\n\
350                   [-t timeout] [-r retries] [-e expect]\n", progname);