4
0

check_radius.c 10 KB

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