check_radius.c 10 KB

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