check_radius.c 13 KB

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