check_radius.c 12 KB

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