check_radius.c 8.3 KB

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