check_ldap.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. /******************************************************************************
  2. *
  3. * Nagios check_ldap plugin
  4. *
  5. * License: GPL
  6. * Copyright (c) 2000-2006 nagios-plugins team
  7. *
  8. * Last Modified: $Date$
  9. *
  10. * Description:
  11. *
  12. * This file contains the check_ldap plugin
  13. *
  14. * License Information:
  15. *
  16. * This program is free software; you can redistribute it and/or modify
  17. * it under the terms of the GNU General Public License as published by
  18. * the Free Software Foundation; either version 2 of the License, or
  19. * (at your option) any later version.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU General Public License
  27. * along with this program; if not, write to the Free Software
  28. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  29. $Id$
  30. ******************************************************************************/
  31. /* progname may be check_ldaps */
  32. char *progname = "check_ldap";
  33. const char *revision = "$Revision$";
  34. const char *copyright = "2000-2006";
  35. const char *email = "nagiosplug-devel@lists.sourceforge.net";
  36. #include "common.h"
  37. #include "netutils.h"
  38. #include "utils.h"
  39. #include <lber.h>
  40. #include <ldap.h>
  41. enum {
  42. UNDEFINED = 0,
  43. #ifdef HAVE_LDAP_SET_OPTION
  44. DEFAULT_PROTOCOL = 2,
  45. #endif
  46. DEFAULT_PORT = 389
  47. };
  48. int process_arguments (int, char **);
  49. int validate_arguments (void);
  50. void print_help (void);
  51. void print_usage (void);
  52. char ld_defattr[] = "(objectclass=*)";
  53. char *ld_attr = ld_defattr;
  54. char *ld_host = NULL;
  55. char *ld_base = NULL;
  56. char *ld_passwd = NULL;
  57. char *ld_binddn = NULL;
  58. int ld_port = DEFAULT_PORT;
  59. #ifdef HAVE_LDAP_SET_OPTION
  60. int ld_protocol = DEFAULT_PROTOCOL;
  61. #endif
  62. double warn_time = UNDEFINED;
  63. double crit_time = UNDEFINED;
  64. struct timeval tv;
  65. /* for ldap tls */
  66. char *SERVICE = "LDAP";
  67. int
  68. main (int argc, char *argv[])
  69. {
  70. LDAP *ld;
  71. LDAPMessage *result;
  72. /* should be int result = STATE_UNKNOWN; */
  73. int status = STATE_UNKNOWN;
  74. long microsec;
  75. double elapsed_time;
  76. /* for ldap tls */
  77. int tls;
  78. int version=3;
  79. setlocale (LC_ALL, "");
  80. bindtextdomain (PACKAGE, LOCALEDIR);
  81. textdomain (PACKAGE);
  82. if (strstr(argv[0],"check_ldaps")) {
  83. asprintf (&progname, "check_ldaps");
  84. }
  85. if (process_arguments (argc, argv) == ERROR)
  86. usage4 (_("Could not parse arguments"));
  87. /* initialize alarm signal handling */
  88. signal (SIGALRM, socket_timeout_alarm_handler);
  89. /* set socket timeout */
  90. alarm (socket_timeout);
  91. /* get the start time */
  92. gettimeofday (&tv, NULL);
  93. /* initialize ldap */
  94. #ifdef HAVE_LDAP_INIT
  95. if (!(ld = ldap_init (ld_host, ld_port))) {
  96. printf ("Could not connect to the server at port %i\n", ld_port);
  97. return STATE_CRITICAL;
  98. }
  99. #else
  100. if (!(ld = ldap_open (ld_host, ld_port))) {
  101. /*ldap_perror(ld, "ldap_open"); */
  102. printf (_("Could not connect to the server at port %i\n"), ld_port);
  103. return STATE_CRITICAL;
  104. }
  105. #endif /* HAVE_LDAP_INIT */
  106. #ifdef HAVE_LDAP_SET_OPTION
  107. /* set ldap options */
  108. if (ldap_set_option (ld, LDAP_OPT_PROTOCOL_VERSION, &ld_protocol) !=
  109. LDAP_OPT_SUCCESS ) {
  110. printf(_("Could not set protocol version %d\n"), ld_protocol);
  111. return STATE_CRITICAL;
  112. }
  113. #endif
  114. if (strstr(argv[0],"check_ldaps")) {
  115. /* with TLS */
  116. if ( ld_port == LDAPS_PORT ) {
  117. asprintf (&SERVICE, "LDAPS");
  118. #if defined(HAVE_LDAP_SET_OPTION) && defined(LDAP_OPT_X_TLS)
  119. /* ldaps: set option tls */
  120. tls = LDAP_OPT_X_TLS_HARD;
  121. if (ldap_set_option (ld, LDAP_OPT_X_TLS, &tls) != LDAP_SUCCESS)
  122. {
  123. /*ldap_perror(ld, "ldaps_option"); */
  124. printf (_("Could not init TLS at port %i!\n"), ld_port);
  125. return STATE_CRITICAL;
  126. }
  127. #else
  128. printf (_("TLS not supported by the libraries!\n"), ld_port);
  129. return STATE_CRITICAL;
  130. #endif /* LDAP_OPT_X_TLS */
  131. } else {
  132. asprintf (&SERVICE, "LDAP-TLS");
  133. #if defined(HAVE_LDAP_SET_OPTION) && defined(HAVE_LDAP_START_TLS_S)
  134. /* ldap with startTLS: set option version */
  135. if (ldap_get_option(ld,LDAP_OPT_PROTOCOL_VERSION, &version) == LDAP_OPT_SUCCESS )
  136. {
  137. if (version < LDAP_VERSION3)
  138. {
  139. version = LDAP_VERSION3;
  140. ldap_set_option(ld, LDAP_OPT_PROTOCOL_VERSION, &version);
  141. }
  142. }
  143. /* call start_tls */
  144. if (ldap_start_tls_s(ld, NULL, NULL) != LDAP_SUCCESS)
  145. {
  146. /*ldap_perror(ld, "ldap_start_tls"); */
  147. printf (_("Could not init startTLS at port %i!\n"), ld_port);
  148. return STATE_CRITICAL;
  149. }
  150. #else
  151. printf (_("startTLS not supported by the library, needs LDAPv3!\n"));
  152. return STATE_CRITICAL;
  153. #endif /* HAVE_LDAP_START_TLS_S */
  154. }
  155. }
  156. /* bind to the ldap server */
  157. if (ldap_bind_s (ld, ld_binddn, ld_passwd, LDAP_AUTH_SIMPLE) !=
  158. LDAP_SUCCESS) {
  159. /*ldap_perror(ld, "ldap_bind"); */
  160. printf (_("Could not bind to the ldap-server\n"));
  161. return STATE_CRITICAL;
  162. }
  163. /* do a search of all objectclasses in the base dn */
  164. if (ldap_search_s (ld, ld_base, LDAP_SCOPE_BASE, ld_attr, NULL, 0, &result)
  165. != LDAP_SUCCESS) {
  166. /*ldap_perror(ld, "ldap_search"); */
  167. printf (_("Could not search/find objectclasses in %s\n"), ld_base);
  168. return STATE_CRITICAL;
  169. }
  170. /* unbind from the ldap server */
  171. ldap_unbind (ld);
  172. /* reset the alarm handler */
  173. alarm (0);
  174. /* calcutate the elapsed time and compare to thresholds */
  175. microsec = deltime (tv);
  176. elapsed_time = (double)microsec / 1.0e6;
  177. if (crit_time!=UNDEFINED && elapsed_time>crit_time)
  178. status = STATE_CRITICAL;
  179. else if (warn_time!=UNDEFINED && elapsed_time>warn_time)
  180. status = STATE_WARNING;
  181. else
  182. status = STATE_OK;
  183. /* print out the result */
  184. printf (_("LDAP %s - %.3f seconds response time|%s\n"),
  185. state_text (status),
  186. elapsed_time,
  187. fperfdata ("time", elapsed_time, "s",
  188. (int)warn_time, warn_time,
  189. (int)crit_time, crit_time,
  190. TRUE, 0, FALSE, 0));
  191. return status;
  192. }
  193. /* process command-line arguments */
  194. int
  195. process_arguments (int argc, char **argv)
  196. {
  197. int c;
  198. int option = 0;
  199. /* initialize the long option struct */
  200. static struct option longopts[] = {
  201. {"help", no_argument, 0, 'h'},
  202. {"version", no_argument, 0, 'V'},
  203. {"timeout", required_argument, 0, 't'},
  204. {"host", required_argument, 0, 'H'},
  205. {"base", required_argument, 0, 'b'},
  206. {"attr", required_argument, 0, 'a'},
  207. {"bind", required_argument, 0, 'D'},
  208. {"pass", required_argument, 0, 'P'},
  209. #ifdef HAVE_LDAP_SET_OPTION
  210. {"ver2", no_argument, 0, '2'},
  211. {"ver3", no_argument, 0, '3'},
  212. #endif
  213. {"use-ipv4", no_argument, 0, '4'},
  214. {"use-ipv6", no_argument, 0, '6'},
  215. {"port", required_argument, 0, 'p'},
  216. {"warn", required_argument, 0, 'w'},
  217. {"crit", required_argument, 0, 'c'},
  218. {0, 0, 0, 0}
  219. };
  220. if (argc < 2)
  221. return ERROR;
  222. for (c = 1; c < argc; c++) {
  223. if (strcmp ("-to", argv[c]) == 0)
  224. strcpy (argv[c], "-t");
  225. }
  226. while (1) {
  227. c = getopt_long (argc, argv, "hV2346t:c:w:H:b:p:a:D:P:", longopts, &option);
  228. if (c == -1 || c == EOF)
  229. break;
  230. switch (c) {
  231. case 'h': /* help */
  232. print_help ();
  233. exit (STATE_OK);
  234. case 'V': /* version */
  235. print_revision (progname, revision);
  236. exit (STATE_OK);
  237. case 't': /* timeout period */
  238. if (!is_intnonneg (optarg))
  239. usage2 (_("Timeout interval must be a positive integer"), optarg);
  240. else
  241. socket_timeout = atoi (optarg);
  242. break;
  243. case 'H':
  244. ld_host = optarg;
  245. break;
  246. case 'b':
  247. ld_base = optarg;
  248. break;
  249. case 'p':
  250. ld_port = atoi (optarg);
  251. break;
  252. case 'a':
  253. ld_attr = optarg;
  254. break;
  255. case 'D':
  256. ld_binddn = optarg;
  257. break;
  258. case 'P':
  259. ld_passwd = optarg;
  260. break;
  261. case 'w':
  262. warn_time = strtod (optarg, NULL);
  263. break;
  264. case 'c':
  265. crit_time = strtod (optarg, NULL);
  266. break;
  267. #ifdef HAVE_LDAP_SET_OPTION
  268. case '2':
  269. ld_protocol = 2;
  270. break;
  271. case '3':
  272. ld_protocol = 3;
  273. break;
  274. #endif
  275. case '4':
  276. address_family = AF_INET;
  277. break;
  278. case '6':
  279. #ifdef USE_IPV6
  280. address_family = AF_INET6;
  281. #else
  282. usage (_("IPv6 support not available\n"));
  283. #endif
  284. break;
  285. default:
  286. usage2 (_("Unknown argument"), optarg);
  287. }
  288. }
  289. c = optind;
  290. if (ld_host == NULL && is_host(argv[c]))
  291. ld_host = strdup (argv[c++]);
  292. if (ld_base == NULL && argv[c])
  293. ld_base = strdup (argv[c++]);
  294. return validate_arguments ();
  295. }
  296. int
  297. validate_arguments ()
  298. {
  299. if (ld_host==NULL || strlen(ld_host)==0)
  300. usage4 (_("Please specify the host name\n"));
  301. if (ld_base==NULL || strlen(ld_base)==0)
  302. usage4 (_("Please specify the LDAP base\n"));
  303. return OK;
  304. }
  305. void
  306. print_help (void)
  307. {
  308. char *myport;
  309. asprintf (&myport, "%d", DEFAULT_PORT);
  310. print_revision (progname, revision);
  311. printf ("Copyright (c) 1999 Didi Rieder (adrieder@sbox.tu-graz.ac.at)\n");
  312. printf (COPYRIGHT, copyright, email);
  313. printf ("\n\n");
  314. print_usage ();
  315. printf (_(UT_HELP_VRSN));
  316. printf (_(UT_HOST_PORT), 'p', myport);
  317. printf (_(UT_IPv46));
  318. printf (" %s\n", "-a [--attr]");
  319. printf (" %s\n", _("ldap attribute to search (default: \"(objectclass=*)\""));
  320. printf (" %s\n", "-b [--base]");
  321. printf (" %s\n", _("ldap base (eg. ou=my unit, o=my org, c=at"));
  322. printf (" %s\n", "-D [--bind]");
  323. printf (" %s\n", _("ldap bind DN (if required)"));
  324. printf (" %s\n", "-P [--pass]");
  325. printf (" %s\n", _("ldap password (if required)"));
  326. #ifdef HAVE_LDAP_SET_OPTION
  327. printf (" %s\n", "-2 [--ver2]");
  328. printf (" %s\n", _("use ldap protocol version 2"));
  329. printf (" %s\n", "-3 [--ver3]");
  330. printf (" %s\n", _("use ldap protocol version 3"));
  331. printf ("(default protocol version: %d)", DEFAULT_PROTOCOL);
  332. #endif
  333. printf (_(UT_WARN_CRIT));
  334. printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
  335. printf (_(UT_VERBOSE));
  336. printf (_(UT_SUPPORT));
  337. }
  338. /* todo
  339. * add option -4 and -6 to the long manual
  340. *
  341. */
  342. void
  343. print_usage (void)
  344. {
  345. printf (_("Usage:"));
  346. printf (" %s -H <host> -b <base_dn> [-p <port>] [-a <attr>] [-D <binddn>]",progname);
  347. printf ("\n [-P <password>] [-w <warn_time>] [-c <crit_time>] [-t timeout]%s\n",
  348. #ifdef HAVE_LDAP_SET_OPTION
  349. "\n [-2|-3] [-4|-6]"
  350. #else
  351. ""
  352. #endif
  353. );
  354. }