check_radius.c 10 KB

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