check_radius.c 12 KB

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