check_radius.c 11 KB

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