check_radius.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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. *******************************************************************************/
  36. const char *progname = "check_radius";
  37. const char *revision = "$Revision$";
  38. const char *copyright = "2000-2006";
  39. const char *email = "nagiosplug-devel@lists.sourceforge.net";
  40. #include "common.h"
  41. #include "utils.h"
  42. #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
  49. int process_arguments (int, char **);
  50. void print_help (void);
  51. void print_usage (void);
  52. /* libradiusclient(-ng) wrapper functions */
  53. #ifdef HAVE_LIBRADIUSCLIENT_NG
  54. #define my_rc_conf_str(a) rc_conf_str(rch,a)
  55. #define my_rc_send_server(a,b) rc_send_server(rch,a,b)
  56. #define my_rc_buildreq(a,b,c,d,e,f) rc_buildreq(rch,a,b,c,d,e,f)
  57. #define my_rc_own_ipaddress() rc_own_ipaddress(rch)
  58. #define my_rc_avpair_add(a,b,c,d) rc_avpair_add(rch,a,b,c,-1,d)
  59. #define my_rc_read_dictionary(a) rc_read_dictionary(rch, a)
  60. #else
  61. #define my_rc_conf_str(a) rc_conf_str(a)
  62. #define my_rc_send_server(a,b) rc_send_server(a, b)
  63. #define my_rc_buildreq(a,b,c,d,e,f) rc_buildreq(a,b,c,d,e,f)
  64. #define my_rc_own_ipaddress() rc_own_ipaddress()
  65. #define my_rc_avpair_add(a,b,c,d) rc_avpair_add(a, b, c, d)
  66. #define my_rc_read_dictionary(a) rc_read_dictionary(a)
  67. #endif
  68. int my_rc_read_config(char *);
  69. char *server = NULL;
  70. char *username = NULL;
  71. char *password = NULL;
  72. char *nasid = 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;
  79. /******************************************************************************
  80. The (psuedo?)literate programming XML is contained within \@\@\- <XML> \-\@\@
  81. tags in the comments. With in the tags, the XML is assembled sequentially.
  82. You can define entities in tags. You also have all the #defines available as
  83. entities.
  84. Please note that all tags must be lowercase to use the DocBook XML DTD.
  85. @@-<article>
  86. <sect1>
  87. <title>Quick Reference</title>
  88. <!-- The refentry forms a manpage -->
  89. <refentry>
  90. <refmeta>
  91. <manvolnum>5<manvolnum>
  92. </refmeta>
  93. <refnamdiv>
  94. <refname>&progname;</refname>
  95. <refpurpose>&SUMMARY;</refpurpose>
  96. </refnamdiv>
  97. </refentry>
  98. </sect1>
  99. <sect1>
  100. <title>FAQ</title>
  101. </sect1>
  102. <sect1>
  103. <title>Theory, Installation, and Operation</title>
  104. <sect2>
  105. <title>General Description</title>
  106. <para>
  107. &DESCRIPTION;
  108. </para>
  109. </sect2>
  110. <sect2>
  111. <title>Future Enhancements</title>
  112. <para>Todo List</para>
  113. <itemizedlist>
  114. <listitem>Add option to get password from a secured file rather than the command line</listitem>
  115. </itemizedlist>
  116. </sect2>
  117. <sect2>
  118. <title>Functions</title>
  119. -@@
  120. ******************************************************************************/
  121. int
  122. main (int argc, char **argv)
  123. {
  124. UINT4 service;
  125. char msg[BUFFER_LEN];
  126. SEND_DATA data;
  127. int result = STATE_UNKNOWN;
  128. UINT4 client_id;
  129. char *str;
  130. setlocale (LC_ALL, "");
  131. bindtextdomain (PACKAGE, LOCALEDIR);
  132. textdomain (PACKAGE);
  133. if (process_arguments (argc, argv) == ERROR)
  134. usage4 (_("Could not parse arguments"));
  135. str = strdup ("dictionary");
  136. if ((config_file && my_rc_read_config (config_file)) ||
  137. my_rc_read_dictionary (my_rc_conf_str (str)))
  138. die (STATE_UNKNOWN, _("Config file error"));
  139. service = PW_AUTHENTICATE_ONLY;
  140. memset (&data, 0, sizeof(data));
  141. if (!(my_rc_avpair_add (&data.send_pairs, PW_SERVICE_TYPE, &service, 0) &&
  142. my_rc_avpair_add (&data.send_pairs, PW_USER_NAME, username, 0) &&
  143. my_rc_avpair_add (&data.send_pairs, PW_USER_PASSWORD, password, 0) &&
  144. (nasid==NULL || my_rc_avpair_add (&data.send_pairs, PW_NAS_IDENTIFIER, nasid, 0))))
  145. die (STATE_UNKNOWN, _("Out of Memory?"));
  146. /*
  147. * Fill in NAS-IP-Address
  148. */
  149. if ((client_id = my_rc_own_ipaddress ()) == 0)
  150. return (ERROR_RC);
  151. if (my_rc_avpair_add (&(data.send_pairs), PW_NAS_IP_ADDRESS, &client_id, 0) ==
  152. NULL) return (ERROR_RC);
  153. my_rc_buildreq (&data, PW_ACCESS_REQUEST, server, port, (int)timeout_interval,
  154. retries);
  155. result = my_rc_send_server (&data, msg);
  156. rc_avpair_free (data.send_pairs);
  157. if (data.receive_pairs)
  158. rc_avpair_free (data.receive_pairs);
  159. if (result == TIMEOUT_RC)
  160. die (STATE_CRITICAL, _("Timeout"));
  161. if (result == ERROR_RC)
  162. die (STATE_CRITICAL, _("Auth Error"));
  163. if (result == BADRESP_RC)
  164. die (STATE_WARNING, _("Auth Failed"));
  165. if (expect && !strstr (msg, expect))
  166. die (STATE_WARNING, "%s", msg);
  167. if (result == OK_RC)
  168. die (STATE_OK, _("Auth OK"));
  169. return (0);
  170. }
  171. /* process command-line arguments */
  172. int
  173. process_arguments (int argc, char **argv)
  174. {
  175. int c;
  176. int option = 0;
  177. static struct option longopts[] = {
  178. {"hostname", required_argument, 0, 'H'},
  179. {"port", required_argument, 0, 'P'},
  180. {"username", required_argument, 0, 'u'},
  181. {"password", required_argument, 0, 'p'},
  182. {"nas-id", required_argument, 0, 'n'},
  183. {"filename", required_argument, 0, 'F'},
  184. {"expect", required_argument, 0, 'e'},
  185. {"retries", required_argument, 0, 'r'},
  186. {"timeout", required_argument, 0, 't'},
  187. {"verbose", no_argument, 0, 'v'},
  188. {"version", no_argument, 0, 'V'},
  189. {"help", no_argument, 0, 'h'},
  190. {0, 0, 0, 0}
  191. };
  192. while (1) {
  193. c = getopt_long (argc, argv, "+hVvH:P:F:u:p:n:t:r:e:", longopts,
  194. &option);
  195. if (c == -1 || c == EOF || c == 1)
  196. break;
  197. switch (c) {
  198. case '?': /* print short usage statement if args not parsable */
  199. usage5 ();
  200. case 'h': /* help */
  201. print_help ();
  202. exit (OK);
  203. case 'V': /* version */
  204. print_revision (progname, revision);
  205. exit (OK);
  206. case 'v': /* verbose mode */
  207. verbose = TRUE;
  208. break;
  209. case 'H': /* hostname */
  210. if (is_host (optarg) == FALSE) {
  211. usage2 (_("Invalid hostname/address"), optarg);
  212. }
  213. server = optarg;
  214. break;
  215. case 'P': /* port */
  216. if (is_intnonneg (optarg))
  217. port = atoi (optarg);
  218. else
  219. usage4 (_("Port must be a positive integer"));
  220. break;
  221. case 'u': /* username */
  222. username = optarg;
  223. break;
  224. case 'p': /* password */
  225. password = optarg;
  226. break;
  227. case 'n': /* nas id */
  228. nasid = optarg;
  229. break;
  230. case 'F': /* configuration file */
  231. config_file = optarg;
  232. break;
  233. case 'e': /* expect */
  234. expect = optarg;
  235. break;
  236. case 'r': /* retries */
  237. if (is_intpos (optarg))
  238. retries = atoi (optarg);
  239. else
  240. usage4 (_("Number of retries must be a positive integer"));
  241. break;
  242. case 't': /* timeout */
  243. if (is_intpos (optarg))
  244. timeout_interval = atoi (optarg);
  245. else
  246. usage2 (_("Timeout interval must be a positive integer"), optarg);
  247. break;
  248. }
  249. }
  250. if (server == NULL)
  251. usage4 (_("Host not specified"));
  252. if (username == NULL)
  253. usage4 (_("User not specified"));
  254. if (password == NULL)
  255. usage4 (_("Password not specified"));
  256. if (config_file == NULL)
  257. usage4 (_("Configuration file not specified"));
  258. return OK;
  259. }
  260. void
  261. print_help (void)
  262. {
  263. char *myport;
  264. asprintf (&myport, "%d", PW_AUTH_UDP_PORT);
  265. print_revision (progname, revision);
  266. printf ("Copyright (c) 1999 Robert August Vincent II\n");
  267. printf (COPYRIGHT, copyright, email);
  268. printf("%s\n", _("Tests to see if a radius server is accepting connections."));
  269. printf ("\n\n");
  270. print_usage ();
  271. printf (_(UT_HELP_VRSN));
  272. printf (_(UT_HOST_PORT), 'P', myport);
  273. printf (" %s\n", "-u, --username=STRING");
  274. printf (" %s\n", _("The user to authenticate"));
  275. printf (" %s\n", "-p, --password=STRING");
  276. printf (" %s\n", _("Password for autentication (SECURITY RISK)"));
  277. printf (" %s\n", "-n, --nas-id=STRING");
  278. printf (" %s\n", _("NAS identifier"));
  279. printf (" %s\n", "-F, --filename=STRING");
  280. printf (" %s\n", _("Configuration file"));
  281. printf (" %s\n", "-e, --expect=STRING");
  282. printf (" %s\n", _("Response string to expect from the server"));
  283. printf (" %s\n", "-r, --retries=INTEGER");
  284. printf (" %s\n", _("Number of times to retry a failed connection"));
  285. printf (_(UT_TIMEOUT), timeout_interval);
  286. printf ("%s\n", _("This plugin tests a radius server to see if it is accepting connections."));
  287. printf ("%s\n", _("The server to test must be specified in the invocation, as well as a user"));
  288. printf ("%s\n", _("name and password. A configuration file may also be present. The format of"));
  289. printf ("%s\n", _("the configuration file is described in the radiusclient library sources."));
  290. printf ("%s\n", _("The password option presents a substantial security issue because the"));
  291. printf ("%s\n", _("password can be determined by careful watching of the command line in"));
  292. printf ("%s\n", _("a process listing. This risk is exacerbated because nagios will"));
  293. printf ("%s\n", _("run the plugin at regular prdictable intervals. Please be sure that"));
  294. printf ("%s\n", _("the password used does not allow access to sensitive system resources,"));
  295. printf ("%s\n", _("otherwise compormise could occur."));
  296. printf (_(UT_SUPPORT));
  297. }
  298. void
  299. print_usage (void)
  300. {
  301. printf (_("Usage:"));
  302. printf ("%s -H host -F config_file -u username -p password [-n nas-id] [-P port]\n\
  303. [-t timeout] [-r retries] [-e expect]\n", progname);
  304. }
  305. int my_rc_read_config(char * a)
  306. {
  307. #ifdef HAVE_LIBRADIUSCLIENT_NG
  308. rch = rc_read_config(a);
  309. return (rch == NULL) ? 1 : 0;
  310. #else
  311. return rc_read_config(a);
  312. #endif
  313. }