check_radius.c 12 KB

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