4
0

check_radius.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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. #include <radiusclient.h>
  44. int process_arguments (int, char **);
  45. void print_help (void);
  46. void print_usage (void);
  47. char *server = NULL;
  48. char *username = NULL;
  49. char *password = NULL;
  50. char *nasid = NULL;
  51. char *expect = NULL;
  52. char *config_file = NULL;
  53. unsigned short port = PW_AUTH_UDP_PORT;
  54. int retries = 1;
  55. int verbose = FALSE;
  56. ENV *env = NULL;
  57. /******************************************************************************
  58. The (psuedo?)literate programming XML is contained within \@\@\- <XML> \-\@\@
  59. tags in the comments. With in the tags, the XML is assembled sequentially.
  60. You can define entities in tags. You also have all the #defines available as
  61. entities.
  62. Please note that all tags must be lowercase to use the DocBook XML DTD.
  63. @@-<article>
  64. <sect1>
  65. <title>Quick Reference</title>
  66. <!-- The refentry forms a manpage -->
  67. <refentry>
  68. <refmeta>
  69. <manvolnum>5<manvolnum>
  70. </refmeta>
  71. <refnamdiv>
  72. <refname>&progname;</refname>
  73. <refpurpose>&SUMMARY;</refpurpose>
  74. </refnamdiv>
  75. </refentry>
  76. </sect1>
  77. <sect1>
  78. <title>FAQ</title>
  79. </sect1>
  80. <sect1>
  81. <title>Theory, Installation, and Operation</title>
  82. <sect2>
  83. <title>General Description</title>
  84. <para>
  85. &DESCRIPTION;
  86. </para>
  87. </sect2>
  88. <sect2>
  89. <title>Future Enhancements</title>
  90. <para>Todo List</para>
  91. <itemizedlist>
  92. <listitem>Add option to get password from a secured file rather than the command line</listitem>
  93. </itemizedlist>
  94. </sect2>
  95. <sect2>
  96. <title>Functions</title>
  97. -@@
  98. ******************************************************************************/
  99. int
  100. main (int argc, char **argv)
  101. {
  102. UINT4 service;
  103. char msg[BUFFER_LEN];
  104. SEND_DATA data;
  105. int result = STATE_UNKNOWN;
  106. UINT4 client_id;
  107. char *str;
  108. setlocale (LC_ALL, "");
  109. bindtextdomain (PACKAGE, LOCALEDIR);
  110. textdomain (PACKAGE);
  111. if (process_arguments (argc, argv) == ERROR)
  112. usage4 (_("Could not parse arguments"));
  113. str = strdup ("dictionary");
  114. if ((config_file && rc_read_config (config_file)) ||
  115. rc_read_dictionary (rc_conf_str (str)))
  116. die (STATE_UNKNOWN, _("Config file error"));
  117. service = PW_AUTHENTICATE_ONLY;
  118. memset (&data, 0, sizeof(data));
  119. if (!(rc_avpair_add (&data.send_pairs, PW_SERVICE_TYPE, &service, 0) &&
  120. rc_avpair_add (&data.send_pairs, PW_USER_NAME, username, 0) &&
  121. rc_avpair_add (&data.send_pairs, PW_USER_PASSWORD, password, 0) &&
  122. (nasid==NULL || rc_avpair_add (&data.send_pairs, PW_NAS_IDENTIFIER, nasid, 0))))
  123. die (STATE_UNKNOWN, _("Out of Memory?"));
  124. /*
  125. * Fill in NAS-IP-Address
  126. */
  127. if ((client_id = rc_own_ipaddress ()) == 0)
  128. return (ERROR_RC);
  129. if (rc_avpair_add (&(data.send_pairs), PW_NAS_IP_ADDRESS, &client_id, 0) ==
  130. NULL) return (ERROR_RC);
  131. rc_buildreq (&data, PW_ACCESS_REQUEST, server, port, (int)timeout_interval,
  132. retries);
  133. result = rc_send_server (&data, msg);
  134. rc_avpair_free (data.send_pairs);
  135. if (data.receive_pairs)
  136. rc_avpair_free (data.receive_pairs);
  137. if (result == TIMEOUT_RC)
  138. die (STATE_CRITICAL, _("Timeout"));
  139. if (result == ERROR_RC)
  140. die (STATE_CRITICAL, _("Auth Error"));
  141. if (result == BADRESP_RC)
  142. die (STATE_WARNING, _("Auth Failed"));
  143. if (expect && !strstr (msg, expect))
  144. die (STATE_WARNING, "%s", msg);
  145. if (result == OK_RC)
  146. die (STATE_OK, _("Auth OK"));
  147. return (0);
  148. }
  149. /* process command-line arguments */
  150. int
  151. process_arguments (int argc, char **argv)
  152. {
  153. int c;
  154. int option = 0;
  155. static struct option longopts[] = {
  156. {"hostname", required_argument, 0, 'H'},
  157. {"port", required_argument, 0, 'P'},
  158. {"username", required_argument, 0, 'u'},
  159. {"password", required_argument, 0, 'p'},
  160. {"nas-id", required_argument, 0, 'n'},
  161. {"filename", required_argument, 0, 'F'},
  162. {"expect", required_argument, 0, 'e'},
  163. {"retries", required_argument, 0, 'r'},
  164. {"timeout", required_argument, 0, 't'},
  165. {"verbose", no_argument, 0, 'v'},
  166. {"version", no_argument, 0, 'V'},
  167. {"help", no_argument, 0, 'h'},
  168. {0, 0, 0, 0}
  169. };
  170. if (argc < 2)
  171. return ERROR;
  172. if (argc == 9) {
  173. config_file = argv[1];
  174. username = argv[2];
  175. password = argv[3];
  176. if (is_intpos (argv[4]))
  177. timeout_interval = atoi (argv[4]);
  178. else
  179. usage2 (_("Timeout interval must be a positive integer"), optarg);
  180. if (is_intpos (argv[5]))
  181. retries = atoi (argv[5]);
  182. else
  183. usage4 (_("Number of retries must be a positive integer"));
  184. server = argv[6];
  185. if (is_intpos (argv[7]))
  186. port = atoi (argv[7]);
  187. else
  188. usage4 (_("Port must be a positive integer"));
  189. expect = argv[8];
  190. return OK;
  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. return OK;
  251. }
  252. void
  253. print_help (void)
  254. {
  255. char *myport;
  256. asprintf (&myport, "%d", PW_AUTH_UDP_PORT);
  257. print_revision (progname, revision);
  258. printf ("Copyright (c) 1999 Robert August Vincent II\n");
  259. printf (COPYRIGHT, copyright, email);
  260. printf("%s\n", _("Tests to see if a radius server is accepting connections."));
  261. printf ("\n\n");
  262. print_usage ();
  263. printf (_(UT_HELP_VRSN));
  264. printf (_(UT_HOST_PORT), 'P', myport);
  265. printf (" %s\n", "-u, --username=STRING");
  266. printf (" %s\n", _("The user to authenticate"));
  267. printf (" %s\n", "-p, --password=STRING");
  268. printf (" %s\n", _("Password for autentication (SECURITY RISK)"));
  269. printf (" %s\n", "-n, --nas-id=STRING");
  270. printf (" %s\n", _("NAS identifier"));
  271. printf (" %s\n", "-F, --filename=STRING");
  272. printf (" %s\n", _("Configuration file"));
  273. printf (" %s\n", "-e, --expect=STRING");
  274. printf (" %s\n", _("Response string to expect from the server"));
  275. printf (" %s\n", "-r, --retries=INTEGER");
  276. printf (" %s\n", _("Number of times to retry a failed connection"));
  277. printf (_(UT_TIMEOUT), timeout_interval);
  278. printf ("%s\n", _("This plugin tests a radius server to see if it is accepting connections."));
  279. printf ("%s\n", _("The server to test must be specified in the invocation, as well as a user"));
  280. printf ("%s\n", _("name and password. A configuration file may also be present. The format of"));
  281. printf ("%s\n", _("the configuration file is described in the radiusclient library sources."));
  282. printf ("%s\n", _("The password option presents a substantial security issue because the"));
  283. printf ("%s\n", _("password can be determined by careful watching of the command line in"));
  284. printf ("%s\n", _("a process listing. This risk is exacerbated because nagios will"));
  285. printf ("%s\n", _("run the plugin at regular prdictable intervals. Please be sure that"));
  286. printf ("%s\n", _("the password used does not allow access to sensitive system resources,"));
  287. printf ("%s\n", _("otherwise compormise could occur."));
  288. printf (_(UT_SUPPORT));
  289. }
  290. void
  291. print_usage (void)
  292. {
  293. printf (_("Usage:"));
  294. printf ("%s -H host -F config_file -u username -p password [-n nas-id] [-P port]\n\
  295. [-t timeout] [-r retries] [-e expect]\n", progname);
  296. }