1 /*****************************************************************************
2 *
3 * Monitoring check_radius plugin
4 *
5 * License: GPL
6 * Copyright (c) 1999-2008 Monitoring 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 *****************************************************************************/
30 
31 const char *progname = "check_radius";
32 const char *copyright = "2000-2008";
33 const char *email = "devel@monitoring-plugins.org";
34 
35 #include "common.h"
36 #include "utils.h"
37 #include "netutils.h"
38 
39 #if defined(HAVE_LIBRADCLI)
40 #include <radcli/radcli.h>
41 #elif defined(HAVE_LIBFREERADIUS_CLIENT)
42 #include <freeradius-client.h>
43 #elif defined(HAVE_LIBRADIUSCLIENT_NG)
44 #include <radiusclient-ng.h>
45 #else
46 #include <radiusclient.h>
47 #endif
48 
49 int process_arguments (int, char **);
50 void print_help (void);
51 void print_usage (void);
52 
53 #if defined(HAVE_LIBFREERADIUS_CLIENT) || defined(HAVE_LIBRADIUSCLIENT_NG) || defined(HAVE_LIBRADCLI)
54 #define my_rc_conf_str(a) rc_conf_str(rch,a)
55 #if defined(HAVE_LIBRADCLI)
56 #define my_rc_send_server(a,b) rc_send_server(rch,a,b,AUTH)
57 #else
58 #define my_rc_send_server(a,b) rc_send_server(rch,a,b)
59 #endif
60 #if defined(HAVE_LIBFREERADIUS_CLIENT) || defined(HAVE_LIBRADCLI)
61 #define my_rc_buildreq(a,b,c,d,e,f) rc_buildreq(rch,a,b,c,d,(a)->secret,e,f)
62 #else
63 #define my_rc_buildreq(a,b,c,d,e,f) rc_buildreq(rch,a,b,c,d,e,f)
64 #endif
65 #define my_rc_avpair_add(a,b,c,d) rc_avpair_add(rch,a,b,c,-1,d)
66 #define my_rc_read_dictionary(a) rc_read_dictionary(rch, a)
67 #else
68 #define my_rc_conf_str(a) rc_conf_str(a)
69 #define my_rc_send_server(a,b) rc_send_server(a, b)
70 #define my_rc_buildreq(a,b,c,d,e,f) rc_buildreq(a,b,c,d,e,f)
71 #define my_rc_avpair_add(a,b,c,d) rc_avpair_add(a, b, c, d)
72 #define my_rc_read_dictionary(a) rc_read_dictionary(a)
73 #endif
74 
75 /* REJECT_RC is only defined in some version of radiusclient. It has
76  * been reported from radiusclient-ng 0.5.6 on FreeBSD 7.2-RELEASE */
77 #ifndef REJECT_RC
78 #define REJECT_RC BADRESP_RC
79 #endif
80 
81 int my_rc_read_config(char *);
82 
83 #if defined(HAVE_LIBFREERADIUS_CLIENT) || defined(HAVE_LIBRADIUSCLIENT_NG) || defined(HAVE_LIBRADCLI)
84 rc_handle *rch = NULL;
85 #endif
86 
87 char *server = NULL;
88 char *username = NULL;
89 char *password = NULL;
90 char *nasid = NULL;
91 char *nasipaddress = NULL;
92 char *expect = NULL;
93 char *config_file = NULL;
94 unsigned short port = PW_AUTH_UDP_PORT;
95 int retries = 1;
96 int verbose = FALSE;
97 
98 /******************************************************************************
99 
100 The (psuedo?)literate programming XML is contained within \@\@\- <XML> \-\@\@
101 tags in the comments. With in the tags, the XML is assembled sequentially.
102 You can define entities in tags. You also have all the #defines available as
103 entities.
104 
105 Please note that all tags must be lowercase to use the DocBook XML DTD.
106 
107 @@-<article>
108 
109 <sect1>
110 <title>Quick Reference</title>
111 <!-- The refentry forms a manpage -->
112 <refentry>
113 <refmeta>
114 <manvolnum>5<manvolnum>
115 </refmeta>
116 <refnamdiv>
117 <refname>&progname;</refname>
118 <refpurpose>&SUMMARY;</refpurpose>
119 </refnamdiv>
120 </refentry>
121 </sect1>
122 
123 <sect1>
124 <title>FAQ</title>
125 </sect1>
126 
127 <sect1>
128 <title>Theory, Installation, and Operation</title>
129 
130 <sect2>
131 <title>General Description</title>
132 <para>
133 &DESCRIPTION;
134 </para>
135 </sect2>
136 
137 <sect2>
138 <title>Future Enhancements</title>
139 <para>Todo List</para>
140 <itemizedlist>
141 <listitem>Add option to get password from a secured file rather than the command line</listitem>
142 </itemizedlist>
143 </sect2>
144 
145 
146 <sect2>
147 <title>Functions</title>
148 -@@
149 ******************************************************************************/
150 
151 
152 
153 int
main(int argc,char ** argv)154 main (int argc, char **argv)
155 {
156 	struct sockaddr_storage ss;
157 	char name[HOST_NAME_MAX];
158 	char msg[BUFFER_LEN];
159 	SEND_DATA data;
160 	int result = STATE_UNKNOWN;
161 	uint32_t client_id, service;
162 	char *str;
163 
164 	setlocale (LC_ALL, "");
165 	bindtextdomain (PACKAGE, LOCALEDIR);
166 	textdomain (PACKAGE);
167 
168 	/* Parse extra opts if any */
169 	argv=np_extra_opts (&argc, argv, progname);
170 
171 	if (process_arguments (argc, argv) == ERROR)
172 		usage4 (_("Could not parse arguments"));
173 
174 	str = strdup ("dictionary");
175 	if ((config_file && my_rc_read_config (config_file)) ||
176 			my_rc_read_dictionary (my_rc_conf_str (str)))
177 		die (STATE_UNKNOWN, _("Config file error\n"));
178 
179 	service = PW_AUTHENTICATE_ONLY;
180 
181 	memset (&data, 0, sizeof(data));
182 	if (!(my_rc_avpair_add (&data.send_pairs, PW_SERVICE_TYPE, &service, 0) &&
183 				my_rc_avpair_add (&data.send_pairs, PW_USER_NAME, username, 0) &&
184 				my_rc_avpair_add (&data.send_pairs, PW_USER_PASSWORD, password, 0)
185 				))
186 		die (STATE_UNKNOWN, _("Out of Memory?\n"));
187 
188 	if (nasid != NULL) {
189 		if (!(my_rc_avpair_add (&data.send_pairs, PW_NAS_IDENTIFIER, nasid, 0)))
190 			die (STATE_UNKNOWN, _("Invalid NAS-Identifier\n"));
191 	}
192 
193 	if (nasipaddress == NULL) {
194 		if (gethostname (name, sizeof(name)) != 0)
195 			die (STATE_UNKNOWN, _("gethostname() failed!\n"));
196 		nasipaddress = name;
197 	}
198 	if (!dns_lookup (nasipaddress, &ss, AF_INET)) /* TODO: Support IPv6. */
199 		die (STATE_UNKNOWN, _("Invalid NAS-IP-Address\n"));
200 	client_id = ntohl (((struct sockaddr_in *)&ss)->sin_addr.s_addr);
201 	if (my_rc_avpair_add (&(data.send_pairs), PW_NAS_IP_ADDRESS, &client_id, 0) == NULL)
202 		die (STATE_UNKNOWN, _("Invalid NAS-IP-Address\n"));
203 
204 	my_rc_buildreq (&data, PW_ACCESS_REQUEST, server, port, (int)timeout_interval,
205 	             retries);
206 
207 	result = my_rc_send_server (&data, msg);
208 	rc_avpair_free (data.send_pairs);
209 	if (data.receive_pairs)
210 		rc_avpair_free (data.receive_pairs);
211 
212 	if (result == TIMEOUT_RC)
213 		die (STATE_CRITICAL, _("Timeout\n"));
214 	if (result == ERROR_RC)
215 		die (STATE_CRITICAL, _("Auth Error\n"));
216 	if (result == REJECT_RC)
217 		die (STATE_WARNING, _("Auth Failed\n"));
218 	if (result == BADRESP_RC)
219 		die (STATE_WARNING, _("Bad Response\n"));
220 	if (expect && !strstr (msg, expect))
221 		die (STATE_WARNING, "%s\n", msg);
222 	if (result == OK_RC)
223 		die (STATE_OK, _("Auth OK\n"));
224 	(void)snprintf(msg, sizeof(msg), _("Unexpected result code %d"), result);
225 	die (STATE_UNKNOWN, "%s\n", msg);
226 }
227 
228 
229 
230 /* process command-line arguments */
231 int
process_arguments(int argc,char ** argv)232 process_arguments (int argc, char **argv)
233 {
234 	int c;
235 
236 	int option = 0;
237 	static struct option longopts[] = {
238 		{"hostname", required_argument, 0, 'H'},
239 		{"port", required_argument, 0, 'P'},
240 		{"username", required_argument, 0, 'u'},
241 		{"password", required_argument, 0, 'p'},
242 		{"nas-id", required_argument, 0, 'n'},
243 		{"nas-ip-address", required_argument, 0, 'N'},
244 		{"filename", required_argument, 0, 'F'},
245 		{"expect", required_argument, 0, 'e'},
246 		{"retries", required_argument, 0, 'r'},
247 		{"timeout", required_argument, 0, 't'},
248 		{"verbose", no_argument, 0, 'v'},
249 		{"version", no_argument, 0, 'V'},
250 		{"help", no_argument, 0, 'h'},
251 		{0, 0, 0, 0}
252 	};
253 
254 	while (1) {
255 		c = getopt_long (argc, argv, "+hVvH:P:F:u:p:n:N:t:r:e:", longopts,
256 									 &option);
257 
258 		if (c == -1 || c == EOF || c == 1)
259 			break;
260 
261 		switch (c) {
262 		case '?':									/* print short usage statement if args not parsable */
263 			usage5 ();
264 		case 'h':									/* help */
265 			print_help ();
266 			exit (STATE_UNKNOWN);
267 		case 'V':									/* version */
268 			print_revision (progname, NP_VERSION);
269 			exit (STATE_UNKNOWN);
270 		case 'v':									/* verbose mode */
271 			verbose = TRUE;
272 			break;
273 		case 'H':									/* hostname */
274 			if (is_host (optarg) == FALSE) {
275 				usage2 (_("Invalid hostname/address"), optarg);
276 			}
277 			server = optarg;
278 			break;
279 		case 'P':									/* port */
280 			if (is_intnonneg (optarg))
281 				port = (unsigned short)atoi (optarg);
282 			else
283 				usage4 (_("Port must be a positive integer"));
284 			break;
285 		case 'u':									/* username */
286 			username = optarg;
287 			break;
288 		case 'p':									/* password */
289 			password = strdup(optarg);
290 
291 			/* Delete the password from process list */
292 			while (*optarg != '\0') {
293 				*optarg = 'X';
294 				optarg++;
295 			}
296 			break;
297 		case 'n':									/* nas id */
298 			nasid = optarg;
299 			break;
300 		case 'N':									/* nas ip address */
301 			nasipaddress = optarg;
302 			break;
303 		case 'F':									/* configuration file */
304 			config_file = optarg;
305 			break;
306 		case 'e':									/* expect */
307 			expect = optarg;
308 			break;
309 		case 'r':									/* retries */
310 			if (is_intpos (optarg))
311 				retries = atoi (optarg);
312 			else
313 				usage4 (_("Number of retries must be a positive integer"));
314 			break;
315 		case 't':									/* timeout */
316 			if (is_intpos (optarg))
317 				timeout_interval = (unsigned)atoi (optarg);
318 			else
319 				usage2 (_("Timeout interval must be a positive integer"), optarg);
320 			break;
321 		}
322 	}
323 
324 	if (server == NULL)
325 		usage4 (_("Hostname was not supplied"));
326 	if (username == NULL)
327 		usage4 (_("User not specified"));
328 	if (password == NULL)
329 		usage4 (_("Password not specified"));
330 	if (config_file == NULL)
331 		usage4 (_("Configuration file not specified"));
332 
333 	return OK;
334 }
335 
336 
337 
338 void
print_help(void)339 print_help (void)
340 {
341 	char *myport;
342 	xasprintf (&myport, "%d", PW_AUTH_UDP_PORT);
343 
344 	print_revision (progname, NP_VERSION);
345 
346 	printf ("Copyright (c) 1999 Robert August Vincent II\n");
347 	printf (COPYRIGHT, copyright, email);
348 
349 	printf("%s\n", _("Tests to see if a RADIUS server is accepting connections."));
350 
351   printf ("\n\n");
352 
353 	print_usage ();
354 
355 	printf (UT_HELP_VRSN);
356 	printf (UT_EXTRA_OPTS);
357 
358 	printf (UT_HOST_PORT, 'P', myport);
359 
360 	printf (" %s\n", "-u, --username=STRING");
361   printf ("    %s\n", _("The user to authenticate"));
362   printf (" %s\n", "-p, --password=STRING");
363   printf ("    %s\n", _("Password for authentication (SECURITY RISK)"));
364   printf (" %s\n", "-n, --nas-id=STRING");
365   printf ("    %s\n", _("NAS identifier"));
366   printf (" %s\n", "-N, --nas-ip-address=STRING");
367   printf ("    %s\n", _("NAS IP Address"));
368   printf (" %s\n", "-F, --filename=STRING");
369   printf ("    %s\n", _("Configuration file"));
370   printf (" %s\n", "-e, --expect=STRING");
371   printf ("    %s\n", _("Response string to expect from the server"));
372   printf (" %s\n", "-r, --retries=INTEGER");
373   printf ("    %s\n", _("Number of times to retry a failed connection"));
374 
375 	printf (UT_CONN_TIMEOUT, timeout_interval);
376 
377   printf ("\n");
378   printf ("%s\n", _("This plugin tests a RADIUS server to see if it is accepting connections."));
379   printf ("%s\n", _("The server to test must be specified in the invocation, as well as a user"));
380   printf ("%s\n", _("name and password. A configuration file may also be present. The format of"));
381   printf ("%s\n", _("the configuration file is described in the radiusclient library sources."));
382 	printf ("%s\n", _("The password option presents a substantial security issue because the"));
383   printf ("%s\n", _("password can possibly be determined by careful watching of the command line"));
384   printf ("%s\n", _("in a process listing. This risk is exacerbated because the plugin will"));
385   printf ("%s\n", _("typically be executed at regular predictable intervals. Please be sure that"));
386   printf ("%s\n", _("the password used does not allow access to sensitive system resources."));
387 
388 	printf (UT_SUPPORT);
389 }
390 
391 
392 
393 void
print_usage(void)394 print_usage (void)
395 {
396   printf ("%s\n", _("Usage:"));
397 	printf ("%s -H host -F config_file -u username -p password\n\
398 			[-P port] [-t timeout] [-r retries] [-e expect]\n\
399 			[-n nas-id] [-N nas-ip-addr]\n", progname);
400 }
401 
402 
403 
my_rc_read_config(char * a)404 int my_rc_read_config(char * a)
405 {
406 #if defined(HAVE_LIBFREERADIUS_CLIENT) || defined(HAVE_LIBRADIUSCLIENT_NG) || defined(HAVE_LIBRADCLI)
407 	rch = rc_read_config(a);
408 	return (rch == NULL) ? 1 : 0;
409 #else
410 	return rc_read_config(a);
411 #endif
412 }
413