check_ldap.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. /*****************************************************************************
  2. *
  3. * Nagios check_ldap plugin
  4. *
  5. * License: GPL
  6. * Copyright (c) 2000-2014 Nagios Plugins Development Team
  7. *
  8. * Description:
  9. *
  10. * This file contains the check_ldap plugin
  11. *
  12. *
  13. * This program is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation, either version 3 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  25. *
  26. *
  27. *****************************************************************************/
  28. /* progname may be check_ldaps */
  29. char *progname = "check_ldap";
  30. const char *copyright = "2000-2014";
  31. const char *email = "devel@nagios-plugins.org";
  32. #include "common.h"
  33. #include "netutils.h"
  34. #include "utils.h"
  35. #include <lber.h>
  36. #define LDAP_DEPRECATED 1
  37. #include <ldap.h>
  38. enum {
  39. UNDEFINED = 0,
  40. #ifdef HAVE_LDAP_SET_OPTION
  41. DEFAULT_PROTOCOL = 2,
  42. #endif
  43. DEFAULT_PORT = 389
  44. };
  45. int process_arguments (int, char **);
  46. int validate_arguments (void);
  47. void print_help (void);
  48. void print_usage (void);
  49. char ld_defattr[] = "(objectclass=*)";
  50. char *ld_attr = ld_defattr;
  51. char *ld_uri = NULL;
  52. char *ld_host = NULL;
  53. char *ld_base = NULL;
  54. char *ld_passwd = NULL;
  55. char *ld_binddn = NULL;
  56. int ld_port = -1;
  57. #ifdef HAVE_LDAP_SET_OPTION
  58. int ld_protocol = DEFAULT_PROTOCOL;
  59. #endif
  60. #ifndef LDAP_OPT_SUCCESS
  61. # define LDAP_OPT_SUCCESS LDAP_SUCCESS
  62. #endif
  63. double warn_time = UNDEFINED;
  64. double crit_time = UNDEFINED;
  65. thresholds *entries_thresholds = NULL;
  66. struct timeval tv;
  67. char* warn_entries = NULL;
  68. char* crit_entries = NULL;
  69. int starttls = FALSE;
  70. int ssl_on_connect = FALSE;
  71. int verbose = 0;
  72. /* for ldap tls */
  73. char *SERVICE = "LDAP";
  74. int
  75. main (int argc, char *argv[])
  76. {
  77. LDAP *ld;
  78. LDAPMessage *result;
  79. /* should be int result = STATE_UNKNOWN; */
  80. int status = STATE_UNKNOWN;
  81. long microsec;
  82. double elapsed_time;
  83. /* for ldap tls */
  84. int tls;
  85. int version=3;
  86. /* for entry counting */
  87. LDAPMessage *next_entry;
  88. int status_entries = STATE_OK;
  89. int num_entries = 0;
  90. setlocale (LC_ALL, "");
  91. bindtextdomain (PACKAGE, LOCALEDIR);
  92. textdomain (PACKAGE);
  93. if (strstr(argv[0],"check_ldaps")) {
  94. xasprintf (&progname, "check_ldaps");
  95. }
  96. /* Parse extra opts if any */
  97. argv=np_extra_opts (&argc, argv, progname);
  98. if (process_arguments (argc, argv) == ERROR)
  99. usage4 (_("Could not parse arguments"));
  100. if (strstr(argv[0],"check_ldaps") && ! starttls && ! ssl_on_connect)
  101. starttls = TRUE;
  102. /* initialize alarm signal handling */
  103. signal (SIGALRM, socket_timeout_alarm_handler);
  104. /* set socket timeout */
  105. alarm (timeout_interval);
  106. /* get the start time */
  107. gettimeofday (&tv, NULL);
  108. /* initialize ldap */
  109. if (ld_uri != NULL)
  110. {
  111. #ifdef HAVE_LDAP_INITIALIZE
  112. int result = ldap_initialize(&ld, ld_uri);
  113. if (result != LDAP_SUCCESS)
  114. {
  115. printf ("Failed to connect to LDAP server at %s: %s\n",
  116. ld_uri, ldap_err2string(result));
  117. return STATE_CRITICAL;
  118. }
  119. #else
  120. printf ("Sorry, this version of %s was compiled without URI support!\n",
  121. argv[0]);
  122. return STATE_CRITICAL;
  123. #endif
  124. }
  125. #ifdef HAVE_LDAP_INIT
  126. else if (!(ld = ldap_init (ld_host, ld_port))) {
  127. printf ("Could not connect to the server at port %i\n", ld_port);
  128. return STATE_CRITICAL;
  129. }
  130. #else
  131. else if (!(ld = ldap_open (ld_host, ld_port))) {
  132. if (verbose)
  133. ldap_perror(ld, "ldap_open");
  134. printf (_("Could not connect to the server at port %i\n"), ld_port);
  135. return STATE_CRITICAL;
  136. }
  137. #endif /* HAVE_LDAP_INIT */
  138. #ifdef HAVE_LDAP_SET_OPTION
  139. /* set ldap options */
  140. if (ldap_set_option (ld, LDAP_OPT_PROTOCOL_VERSION, &ld_protocol) !=
  141. LDAP_OPT_SUCCESS ) {
  142. printf(_("Could not set protocol version %d\n"), ld_protocol);
  143. return STATE_CRITICAL;
  144. }
  145. #endif
  146. if (ld_port == LDAPS_PORT || ssl_on_connect) {
  147. xasprintf (&SERVICE, "LDAPS");
  148. #if defined(HAVE_LDAP_SET_OPTION) && defined(LDAP_OPT_X_TLS)
  149. /* ldaps: set option tls */
  150. tls = LDAP_OPT_X_TLS_HARD;
  151. if (ldap_set_option (ld, LDAP_OPT_X_TLS, &tls) != LDAP_SUCCESS)
  152. {
  153. if (verbose)
  154. ldap_perror(ld, "ldaps_option");
  155. printf (_("Could not init TLS at port %i!\n"), ld_port);
  156. return STATE_CRITICAL;
  157. }
  158. #else
  159. printf (_("TLS not supported by the libraries!\n"));
  160. return STATE_CRITICAL;
  161. #endif /* LDAP_OPT_X_TLS */
  162. } else if (starttls) {
  163. xasprintf (&SERVICE, "LDAP-TLS");
  164. #if defined(HAVE_LDAP_SET_OPTION) && defined(HAVE_LDAP_START_TLS_S)
  165. /* ldap with startTLS: set option version */
  166. if (ldap_get_option(ld,LDAP_OPT_PROTOCOL_VERSION, &version) == LDAP_OPT_SUCCESS )
  167. {
  168. if (version < LDAP_VERSION3)
  169. {
  170. version = LDAP_VERSION3;
  171. ldap_set_option(ld, LDAP_OPT_PROTOCOL_VERSION, &version);
  172. }
  173. }
  174. /* call start_tls */
  175. if (ldap_start_tls_s(ld, NULL, NULL) != LDAP_SUCCESS)
  176. {
  177. if (verbose)
  178. ldap_perror(ld, "ldap_start_tls");
  179. printf (_("Could not init startTLS at port %i!\n"), ld_port);
  180. return STATE_CRITICAL;
  181. }
  182. #else
  183. printf (_("startTLS not supported by the library, needs LDAPv3!\n"));
  184. return STATE_CRITICAL;
  185. #endif /* HAVE_LDAP_START_TLS_S */
  186. }
  187. /* bind to the ldap server */
  188. if (ldap_bind_s (ld, ld_binddn, ld_passwd, LDAP_AUTH_SIMPLE) !=
  189. LDAP_SUCCESS) {
  190. if (verbose)
  191. ldap_perror(ld, "ldap_bind");
  192. printf (_("Could not bind to the LDAP server\n"));
  193. return STATE_CRITICAL;
  194. }
  195. /* do a search of all objectclasses in the base dn */
  196. if (ldap_search_s (ld, ld_base, (crit_entries!=NULL || warn_entries!=NULL) ? LDAP_SCOPE_SUBTREE : LDAP_SCOPE_BASE, ld_attr, NULL, 0, &result)
  197. != LDAP_SUCCESS) {
  198. if (verbose)
  199. ldap_perror(ld, "ldap_search");
  200. printf (_("Could not search/find objectclasses in %s\n"), ld_base);
  201. return STATE_CRITICAL;
  202. } else if (crit_entries!=NULL || warn_entries!=NULL) {
  203. num_entries = ldap_count_entries(ld, result);
  204. }
  205. /* unbind from the ldap server */
  206. ldap_unbind (ld);
  207. /* reset the alarm handler */
  208. alarm (0);
  209. /* calcutate the elapsed time and compare to thresholds */
  210. microsec = deltime (tv);
  211. elapsed_time = (double)microsec / 1.0e6;
  212. if (crit_time!=UNDEFINED && elapsed_time>crit_time)
  213. status = STATE_CRITICAL;
  214. else if (warn_time!=UNDEFINED && elapsed_time>warn_time)
  215. status = STATE_WARNING;
  216. else
  217. status = STATE_OK;
  218. if(entries_thresholds != NULL) {
  219. if (verbose) {
  220. printf ("entries found: %d\n", num_entries);
  221. print_thresholds("entry threasholds", entries_thresholds);
  222. }
  223. status_entries = get_status(num_entries, entries_thresholds);
  224. if (status_entries == STATE_CRITICAL) {
  225. status = STATE_CRITICAL;
  226. } else if (status != STATE_CRITICAL) {
  227. status = status_entries;
  228. }
  229. }
  230. /* print out the result */
  231. if (crit_entries!=NULL || warn_entries!=NULL) {
  232. printf (_("LDAP %s - found %d entries in %.3f seconds|%s %s\n"),
  233. state_text (status),
  234. num_entries,
  235. elapsed_time,
  236. fperfdata ("time", elapsed_time, "s",
  237. (int)warn_time, warn_time,
  238. (int)crit_time, crit_time,
  239. TRUE, 0, FALSE, 0),
  240. sperfdata ("entries", (double)num_entries, "",
  241. warn_entries,
  242. crit_entries,
  243. TRUE, 0.0, FALSE, 0.0));
  244. } else {
  245. printf (_("LDAP %s - %.3f seconds response time|%s\n"),
  246. state_text (status),
  247. elapsed_time,
  248. fperfdata ("time", elapsed_time, "s",
  249. (int)warn_time, warn_time,
  250. (int)crit_time, crit_time,
  251. TRUE, 0, FALSE, 0));
  252. }
  253. return status;
  254. }
  255. /* process command-line arguments */
  256. int
  257. process_arguments (int argc, char **argv)
  258. {
  259. int c;
  260. int option = 0;
  261. /* initialize the long option struct */
  262. static struct option longopts[] = {
  263. {"help", no_argument, 0, 'h'},
  264. {"version", no_argument, 0, 'V'},
  265. {"timeout", required_argument, 0, 't'},
  266. {"hostname", required_argument, 0, 'H'},
  267. {"uri", required_argument, 0, 'U'},
  268. {"base", required_argument, 0, 'b'},
  269. {"attr", required_argument, 0, 'a'},
  270. {"bind", required_argument, 0, 'D'},
  271. {"pass", required_argument, 0, 'P'},
  272. #ifdef HAVE_LDAP_SET_OPTION
  273. {"ver2", no_argument, 0, '2'},
  274. {"ver3", no_argument, 0, '3'},
  275. #endif
  276. {"starttls", no_argument, 0, 'T'},
  277. {"ssl", no_argument, 0, 'S'},
  278. {"use-ipv4", no_argument, 0, '4'},
  279. {"use-ipv6", no_argument, 0, '6'},
  280. {"port", required_argument, 0, 'p'},
  281. {"warn", required_argument, 0, 'w'},
  282. {"crit", required_argument, 0, 'c'},
  283. {"warn-entries", required_argument, 0, 'W'},
  284. {"crit-entries", required_argument, 0, 'C'},
  285. {"verbose", no_argument, 0, 'v'},
  286. {0, 0, 0, 0}
  287. };
  288. if (argc < 2)
  289. return ERROR;
  290. for (c = 1; c < argc; c++) {
  291. if (strcmp ("-to", argv[c]) == 0)
  292. strcpy (argv[c], "-t");
  293. }
  294. while (1) {
  295. c = getopt_long (argc, argv, "hvV234TS6t:c:w:H:b:p:a:D:P:U:C:W:", longopts, &option);
  296. if (c == -1 || c == EOF)
  297. break;
  298. switch (c) {
  299. case 'h': /* help */
  300. print_help ();
  301. exit (STATE_OK);
  302. case 'V': /* version */
  303. print_revision (progname, NP_VERSION);
  304. exit (STATE_OK);
  305. case 't': /* timeout period */
  306. timeout_interval = parse_timeout_string(optarg);
  307. break;
  308. case 'U':
  309. ld_uri = optarg;
  310. break;
  311. case 'H':
  312. ld_host = optarg;
  313. break;
  314. case 'b':
  315. ld_base = optarg;
  316. break;
  317. case 'p':
  318. ld_port = atoi (optarg);
  319. break;
  320. case 'a':
  321. ld_attr = optarg;
  322. break;
  323. case 'D':
  324. ld_binddn = optarg;
  325. break;
  326. case 'P':
  327. ld_passwd = optarg;
  328. break;
  329. case 'w':
  330. warn_time = strtod (optarg, NULL);
  331. break;
  332. case 'c':
  333. crit_time = strtod (optarg, NULL);
  334. break;
  335. case 'W':
  336. warn_entries = optarg;
  337. break;
  338. case 'C':
  339. crit_entries = optarg;
  340. break;
  341. #ifdef HAVE_LDAP_SET_OPTION
  342. case '2':
  343. ld_protocol = 2;
  344. break;
  345. case '3':
  346. ld_protocol = 3;
  347. break;
  348. #endif
  349. case '4':
  350. address_family = AF_INET;
  351. break;
  352. case 'v':
  353. verbose++;
  354. break;
  355. case 'T':
  356. if (! ssl_on_connect)
  357. starttls = TRUE;
  358. else
  359. usage_va(_("%s cannot be combined with %s"), "-T/--starttls", "-S/--ssl");
  360. break;
  361. case 'S':
  362. if (! starttls) {
  363. ssl_on_connect = TRUE;
  364. if (ld_port == -1)
  365. ld_port = LDAPS_PORT;
  366. } else
  367. usage_va(_("%s cannot be combined with %s"), "-S/--ssl", "-T/--starttls");
  368. break;
  369. case '6':
  370. #ifdef USE_IPV6
  371. address_family = AF_INET6;
  372. #else
  373. usage (_("IPv6 support not available\n"));
  374. #endif
  375. break;
  376. default:
  377. usage5 ();
  378. }
  379. }
  380. c = optind;
  381. if (ld_host == NULL && is_host(argv[c]))
  382. ld_host = strdup (argv[c++]);
  383. if (ld_base == NULL && argv[c])
  384. ld_base = strdup (argv[c++]);
  385. if (ld_port == -1)
  386. ld_port = DEFAULT_PORT;
  387. return validate_arguments ();
  388. }
  389. int
  390. validate_arguments ()
  391. {
  392. if ((ld_host==NULL || strlen(ld_host)==0) &&
  393. (ld_uri==NULL || strlen(ld_uri)==0))
  394. usage4 (_("Please specify the host name or LDAP URI\n"));
  395. if (ld_base==NULL)
  396. usage4 (_("Please specify the LDAP base DN\n"));
  397. if (crit_entries!=NULL || warn_entries!=NULL) {
  398. set_thresholds(&entries_thresholds,
  399. warn_entries, crit_entries);
  400. }
  401. return OK;
  402. }
  403. void
  404. print_help (void)
  405. {
  406. char *myport;
  407. xasprintf (&myport, "%d", DEFAULT_PORT);
  408. print_revision (progname, NP_VERSION);
  409. printf ("Copyright (c) 1999 Didi Rieder (adrieder@sbox.tu-graz.ac.at)\n");
  410. printf (COPYRIGHT, copyright, email);
  411. printf ("\n\n");
  412. print_usage ();
  413. printf (UT_HELP_VRSN);
  414. printf (UT_EXTRA_OPTS);
  415. printf (UT_HOST_PORT, 'p', myport);
  416. printf (UT_IPv46);
  417. printf (" %s\n", "-a [--attr]");
  418. printf (" %s\n", _("ldap attribute to search (default: \"(objectclass=*)\""));
  419. printf (" %s\n", "-b [--base]");
  420. printf (" %s\n", _("ldap base (eg. ou=my unit, o=my org, c=at"));
  421. printf (" %s\n", "-D [--bind]");
  422. printf (" %s\n", _("ldap bind DN (if required)"));
  423. printf (" %s\n", "-P [--pass]");
  424. printf (" %s\n", _("ldap password (if required)"));
  425. printf (" %s\n", "-T [--starttls]");
  426. printf (" %s\n", _("use starttls mechanism introduced in protocol version 3"));
  427. printf (" %s\n", "-S [--ssl]");
  428. printf (" %s %i\n", _("use ldaps (ldap v2 ssl method). this also sets the default port to"), LDAPS_PORT);
  429. #ifdef HAVE_LDAP_SET_OPTION
  430. printf (" %s\n", "-2 [--ver2]");
  431. printf (" %s\n", _("use ldap protocol version 2"));
  432. printf (" %s\n", "-3 [--ver3]");
  433. printf (" %s\n", _("use ldap protocol version 3"));
  434. printf (" (%s %d)\n", _("default protocol version:"), DEFAULT_PROTOCOL);
  435. #endif
  436. printf (UT_WARN_CRIT);
  437. printf (" %s\n", "-W [--warn-entries]");
  438. printf (" %s\n", _("Number of found entries to result in warning status"));
  439. printf (" %s\n", "-C [--crit-entries]");
  440. printf (" %s\n", _("Number of found entries to result in critical status"));
  441. printf (UT_CONN_TIMEOUT, DEFAULT_SOCKET_TIMEOUT);
  442. printf (UT_VERBOSE);
  443. printf ("\n");
  444. printf ("%s\n", _("Notes:"));
  445. printf (" %s\n", _("If this plugin is called via 'check_ldaps', method 'STARTTLS' will be"));
  446. printf (_(" implied (using default port %i) unless --port=636 is specified. In that case\n"), DEFAULT_PORT);
  447. printf (" %s\n", _("'SSL on connect' will be used no matter how the plugin was called."));
  448. printf (" %s\n", _("This detection is deprecated, please use 'check_ldap' with the '--starttls' or '--ssl' flags"));
  449. printf (" %s\n", _("to define the behaviour explicitly instead."));
  450. printf (" %s\n", _("The parameters --warn-entries and --crit-entries are optional."));
  451. printf (UT_SUPPORT);
  452. }
  453. void
  454. print_usage (void)
  455. {
  456. printf ("%s\n", _("Usage:"));
  457. printf (" %s (-H <host>|-U <uri>) -b <base_dn> [-p <port>] [-a <attr>] [-D <binddn>]\n", progname);
  458. printf (" [-P <password>] [-w <warn_time>] [-c <crit_time>] [-t timeout]%s\n",
  459. #ifdef HAVE_LDAP_SET_OPTION
  460. "\n [-2|-3] [-4|-6]"
  461. #else
  462. ""
  463. #endif
  464. );
  465. }