check_radius.c 11 KB

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