check_radius.c 11 KB

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