4
0

check_ldap.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  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. int ldap_check_cert (LDAP *ld);
  48. void print_help (void);
  49. void print_usage (void);
  50. char ld_defattr[] = "(objectclass=*)";
  51. char *ld_attr = ld_defattr;
  52. char *ld_uri = NULL;
  53. char *ld_host = NULL;
  54. char *ld_base = NULL;
  55. char *ld_passwd = NULL;
  56. char *ld_binddn = NULL;
  57. int ld_port = -1;
  58. #ifdef HAVE_LDAP_SET_OPTION
  59. int ld_protocol = DEFAULT_PROTOCOL;
  60. #endif
  61. #ifndef LDAP_OPT_SUCCESS
  62. # define LDAP_OPT_SUCCESS LDAP_SUCCESS
  63. #endif
  64. double warn_time = UNDEFINED;
  65. double crit_time = UNDEFINED;
  66. thresholds *entries_thresholds = NULL;
  67. struct timeval tv;
  68. char* warn_entries = NULL;
  69. char* crit_entries = NULL;
  70. int starttls = FALSE;
  71. int ssl_on_connect = FALSE;
  72. int verbose = 0;
  73. int check_cert = FALSE;
  74. int days_till_exp_warn, days_till_exp_crit;
  75. /* for ldap tls */
  76. char *SERVICE = "LDAP";
  77. int
  78. main (int argc, char *argv[])
  79. {
  80. LDAP *ld;
  81. LDAPMessage *result;
  82. /* should be int result = STATE_UNKNOWN; */
  83. int status = STATE_UNKNOWN;
  84. long microsec;
  85. double elapsed_time;
  86. /* for ldap tls */
  87. int tls;
  88. int version=3;
  89. /* for entry counting */
  90. LDAPMessage *next_entry;
  91. int status_entries = STATE_OK;
  92. int num_entries = 0;
  93. setlocale (LC_ALL, "");
  94. bindtextdomain (PACKAGE, LOCALEDIR);
  95. textdomain (PACKAGE);
  96. if (strstr(argv[0],"check_ldaps")) {
  97. xasprintf (&progname, "check_ldaps");
  98. }
  99. /* Parse extra opts if any */
  100. argv=np_extra_opts (&argc, argv, progname);
  101. if (process_arguments (argc, argv) == ERROR)
  102. usage4 (_("Could not parse arguments"));
  103. if (strstr(argv[0],"check_ldaps") && ! starttls && ! ssl_on_connect)
  104. starttls = TRUE;
  105. /* initialize alarm signal handling */
  106. signal (SIGALRM, socket_timeout_alarm_handler);
  107. /* set socket timeout */
  108. alarm (timeout_interval);
  109. /* get the start time */
  110. gettimeofday (&tv, NULL);
  111. /* initialize ldap */
  112. if (ld_uri != NULL)
  113. {
  114. #ifdef HAVE_LDAP_INITIALIZE
  115. int result = ldap_initialize(&ld, ld_uri);
  116. if (result != LDAP_SUCCESS)
  117. {
  118. printf ("Failed to connect to LDAP server at %s: %s\n",
  119. ld_uri, ldap_err2string(result));
  120. return STATE_CRITICAL;
  121. }
  122. #else
  123. printf ("Sorry, this version of %s was compiled without URI support!\n",
  124. argv[0]);
  125. return STATE_CRITICAL;
  126. #endif
  127. }
  128. #ifdef HAVE_LDAP_INIT
  129. else if (!(ld = ldap_init (ld_host, ld_port))) {
  130. printf ("Could not connect to the server at port %i\n", ld_port);
  131. return STATE_CRITICAL;
  132. }
  133. #else
  134. else if (!(ld = ldap_open (ld_host, ld_port))) {
  135. if (verbose)
  136. ldap_perror(ld, "ldap_open");
  137. printf (_("Could not connect to the server at port %i\n"), ld_port);
  138. return STATE_CRITICAL;
  139. }
  140. #endif /* HAVE_LDAP_INIT */
  141. #ifdef HAVE_LDAP_SET_OPTION
  142. /* set ldap options */
  143. if (ldap_set_option (ld, LDAP_OPT_PROTOCOL_VERSION, &ld_protocol) !=
  144. LDAP_OPT_SUCCESS ) {
  145. printf(_("Could not set protocol version %d\n"), ld_protocol);
  146. return STATE_CRITICAL;
  147. }
  148. #endif
  149. if (ld_port == LDAPS_PORT || ssl_on_connect) {
  150. xasprintf (&SERVICE, "LDAPS");
  151. #if defined(HAVE_LDAP_SET_OPTION) && defined(LDAP_OPT_X_TLS)
  152. /* ldaps: set option tls */
  153. tls = LDAP_OPT_X_TLS_HARD;
  154. if (ldap_set_option (ld, LDAP_OPT_X_TLS, &tls) != LDAP_SUCCESS)
  155. {
  156. if (verbose)
  157. ldap_perror(ld, "ldaps_option");
  158. printf (_("Could not init TLS at port %i!\n"), ld_port);
  159. return STATE_CRITICAL;
  160. }
  161. if (check_cert == TRUE)
  162. return ldap_check_cert(ld);
  163. #else
  164. printf (_("TLS not supported by the libraries!\n"));
  165. return STATE_CRITICAL;
  166. #endif /* LDAP_OPT_X_TLS */
  167. } else if (starttls) {
  168. xasprintf (&SERVICE, "LDAP-TLS");
  169. #if defined(HAVE_LDAP_SET_OPTION) && defined(HAVE_LDAP_START_TLS_S)
  170. /* ldap with startTLS: set option version */
  171. if (ldap_get_option(ld,LDAP_OPT_PROTOCOL_VERSION, &version) == LDAP_OPT_SUCCESS )
  172. {
  173. if (version < LDAP_VERSION3)
  174. {
  175. version = LDAP_VERSION3;
  176. ldap_set_option(ld, LDAP_OPT_PROTOCOL_VERSION, &version);
  177. }
  178. }
  179. /* call start_tls */
  180. if (ldap_start_tls_s(ld, NULL, NULL) != LDAP_SUCCESS)
  181. {
  182. if (verbose)
  183. ldap_perror(ld, "ldap_start_tls");
  184. printf (_("Could not init startTLS at port %i!\n"), ld_port);
  185. return STATE_CRITICAL;
  186. }
  187. if (check_cert == TRUE)
  188. return ldap_check_cert(ld);
  189. #else
  190. printf (_("startTLS not supported by the library, needs LDAPv3!\n"));
  191. return STATE_CRITICAL;
  192. #endif /* HAVE_LDAP_START_TLS_S */
  193. }
  194. /* bind to the ldap server */
  195. if (ldap_bind_s (ld, ld_binddn, ld_passwd, LDAP_AUTH_SIMPLE) !=
  196. LDAP_SUCCESS) {
  197. if (verbose)
  198. ldap_perror(ld, "ldap_bind");
  199. printf (_("Could not bind to the LDAP server\n"));
  200. return STATE_CRITICAL;
  201. }
  202. /* do a search of all objectclasses in the base dn */
  203. if (ldap_search_s (ld, ld_base, (crit_entries!=NULL || warn_entries!=NULL) ? LDAP_SCOPE_SUBTREE : LDAP_SCOPE_BASE, ld_attr, NULL, 0, &result)
  204. != LDAP_SUCCESS) {
  205. if (verbose)
  206. ldap_perror(ld, "ldap_search");
  207. printf (_("Could not search/find objectclasses in %s\n"), ld_base);
  208. return STATE_CRITICAL;
  209. } else if (crit_entries!=NULL || warn_entries!=NULL) {
  210. num_entries = ldap_count_entries(ld, result);
  211. }
  212. /* unbind from the ldap server */
  213. ldap_unbind (ld);
  214. /* reset the alarm handler */
  215. alarm (0);
  216. /* calcutate the elapsed time and compare to thresholds */
  217. microsec = deltime (tv);
  218. elapsed_time = (double)microsec / 1.0e6;
  219. if (crit_time!=UNDEFINED && elapsed_time>crit_time)
  220. status = STATE_CRITICAL;
  221. else if (warn_time!=UNDEFINED && elapsed_time>warn_time)
  222. status = STATE_WARNING;
  223. else
  224. status = STATE_OK;
  225. if(entries_thresholds != NULL) {
  226. if (verbose) {
  227. printf ("entries found: %d\n", num_entries);
  228. print_thresholds("entry thresholds", entries_thresholds);
  229. }
  230. status_entries = get_status(num_entries, entries_thresholds);
  231. if (status_entries == STATE_CRITICAL) {
  232. status = STATE_CRITICAL;
  233. } else if (status != STATE_CRITICAL) {
  234. status = status_entries;
  235. }
  236. }
  237. /* print out the result */
  238. if (crit_entries!=NULL || warn_entries!=NULL) {
  239. printf (_("LDAP %s - found %d entries in %.3f seconds|%s %s\n"),
  240. state_text (status),
  241. num_entries,
  242. elapsed_time,
  243. fperfdata ("time", elapsed_time, "s",
  244. (int)warn_time, warn_time,
  245. (int)crit_time, crit_time,
  246. TRUE, 0, FALSE, 0),
  247. sperfdata ("entries", (double)num_entries, "",
  248. warn_entries,
  249. crit_entries,
  250. TRUE, 0.0, FALSE, 0.0));
  251. } else {
  252. printf (_("LDAP %s - %.3f seconds response time|%s\n"),
  253. state_text (status),
  254. elapsed_time,
  255. fperfdata ("time", elapsed_time, "s",
  256. (int)warn_time, warn_time,
  257. (int)crit_time, crit_time,
  258. TRUE, 0, FALSE, 0));
  259. }
  260. return status;
  261. }
  262. /* process command-line arguments */
  263. int
  264. process_arguments (int argc, char **argv)
  265. {
  266. int c;
  267. char *temp;
  268. int option = 0;
  269. /* initialize the long option struct */
  270. static struct option longopts[] = {
  271. {"help", no_argument, 0, 'h'},
  272. {"version", no_argument, 0, 'V'},
  273. {"timeout", required_argument, 0, 't'},
  274. {"hostname", required_argument, 0, 'H'},
  275. {"uri", required_argument, 0, 'U'},
  276. {"base", required_argument, 0, 'b'},
  277. {"attr", required_argument, 0, 'a'},
  278. {"bind", required_argument, 0, 'D'},
  279. {"pass", required_argument, 0, 'P'},
  280. #ifdef HAVE_LDAP_SET_OPTION
  281. {"ver2", no_argument, 0, '2'},
  282. {"ver3", no_argument, 0, '3'},
  283. #endif
  284. {"starttls", no_argument, 0, 'T'},
  285. {"ssl", no_argument, 0, 'S'},
  286. {"age", required_argument, 0, 'A'},
  287. {"use-ipv4", no_argument, 0, '4'},
  288. {"use-ipv6", no_argument, 0, '6'},
  289. {"port", required_argument, 0, 'p'},
  290. {"warn", required_argument, 0, 'w'},
  291. {"crit", required_argument, 0, 'c'},
  292. {"warn-entries", required_argument, 0, 'W'},
  293. {"crit-entries", required_argument, 0, 'C'},
  294. {"verbose", no_argument, 0, 'v'},
  295. {0, 0, 0, 0}
  296. };
  297. if (argc < 2)
  298. return ERROR;
  299. for (c = 1; c < argc; c++) {
  300. if (strcmp ("-to", argv[c]) == 0)
  301. strcpy (argv[c], "-t");
  302. }
  303. while (1) {
  304. c = getopt_long (argc, argv, "hvV234TS6t:c:w:H:b:p:a:D:P:U:C:W:A:", longopts, &option);
  305. if (c == -1 || c == EOF)
  306. break;
  307. switch (c) {
  308. case 'h': /* help */
  309. print_help ();
  310. exit (STATE_OK);
  311. case 'V': /* version */
  312. print_revision (progname, NP_VERSION);
  313. exit (STATE_OK);
  314. case 't': /* timeout period */
  315. timeout_interval = parse_timeout_string(optarg);
  316. break;
  317. case 'U':
  318. ld_uri = optarg;
  319. break;
  320. case 'H':
  321. ld_host = optarg;
  322. break;
  323. case 'b':
  324. ld_base = optarg;
  325. break;
  326. case 'p':
  327. ld_port = atoi (optarg);
  328. break;
  329. case 'a':
  330. ld_attr = optarg;
  331. break;
  332. case 'D':
  333. ld_binddn = optarg;
  334. break;
  335. case 'P':
  336. ld_passwd = optarg;
  337. break;
  338. case 'w':
  339. warn_time = strtod (optarg, NULL);
  340. break;
  341. case 'c':
  342. crit_time = strtod (optarg, NULL);
  343. break;
  344. case 'W':
  345. warn_entries = optarg;
  346. break;
  347. case 'C':
  348. crit_entries = optarg;
  349. break;
  350. #ifdef HAVE_LDAP_SET_OPTION
  351. case '2':
  352. ld_protocol = 2;
  353. break;
  354. case '3':
  355. ld_protocol = 3;
  356. break;
  357. #endif
  358. case '4':
  359. address_family = AF_INET;
  360. break;
  361. case 'v':
  362. verbose++;
  363. break;
  364. case 'T':
  365. if (! ssl_on_connect)
  366. starttls = TRUE;
  367. else
  368. usage_va(_("%s cannot be combined with %s"), "-T/--starttls", "-S/--ssl");
  369. break;
  370. case 'A': /* Check SSL cert validity */
  371. #ifndef HAVE_SSL
  372. usage4 (_("Invalid option - SSL is not available"));
  373. #else
  374. if (starttls || ssl_on_connect || strstr(argv[0],"check_ldaps")) {
  375. if ((temp=strchr(optarg,','))!=NULL) {
  376. *temp = '\0';
  377. if (!is_intnonneg (temp))
  378. usage2 (_("Invalid certificate expiration period"), optarg);
  379. days_till_exp_warn = atoi(optarg);
  380. *temp = ',';
  381. temp++;
  382. if (!is_intnonneg (temp))
  383. usage2 (_("Invalid certificate expiration period"), temp);
  384. days_till_exp_crit = atoi (temp);
  385. } else {
  386. days_till_exp_crit = 0;
  387. if (!is_intnonneg (optarg))
  388. usage2 (_("Invalid certificate expiration period"), optarg);
  389. days_till_exp_warn = atoi (optarg);
  390. }
  391. check_cert = TRUE;
  392. } else {
  393. usage_va(_("%s requires either %s or %s"), "-A/--age", "-S/--ssl", "-T/--starttls");
  394. }
  395. break;
  396. #endif
  397. case 'S':
  398. if (! starttls) {
  399. ssl_on_connect = TRUE;
  400. if (ld_port == -1)
  401. ld_port = LDAPS_PORT;
  402. } else
  403. usage_va(_("%s cannot be combined with %s"), "-S/--ssl", "-T/--starttls");
  404. break;
  405. case '6':
  406. #ifdef USE_IPV6
  407. address_family = AF_INET6;
  408. #else
  409. usage (_("IPv6 support not available\n"));
  410. #endif
  411. break;
  412. default:
  413. usage5 ();
  414. }
  415. }
  416. c = optind;
  417. if (ld_host == NULL && is_host(argv[c]))
  418. ld_host = strdup (argv[c++]);
  419. if (ld_base == NULL && argv[c])
  420. ld_base = strdup (argv[c++]);
  421. if (ld_port == -1)
  422. ld_port = DEFAULT_PORT;
  423. return validate_arguments ();
  424. }
  425. int
  426. validate_arguments ()
  427. {
  428. if ((ld_host==NULL || strlen(ld_host)==0) &&
  429. (ld_uri==NULL || strlen(ld_uri)==0))
  430. usage4 (_("Please specify the host name or LDAP URI\n"));
  431. if (ld_base==NULL)
  432. usage4 (_("Please specify the LDAP base DN\n"));
  433. if (crit_entries!=NULL || warn_entries!=NULL) {
  434. set_thresholds(&entries_thresholds,
  435. warn_entries, crit_entries);
  436. }
  437. return OK;
  438. }
  439. void
  440. print_help (void)
  441. {
  442. char *myport;
  443. xasprintf (&myport, "%d", DEFAULT_PORT);
  444. print_revision (progname, NP_VERSION);
  445. printf ("Copyright (c) 1999 Didi Rieder (adrieder@sbox.tu-graz.ac.at)\n");
  446. printf (COPYRIGHT, copyright, email);
  447. printf ("\n\n");
  448. print_usage ();
  449. printf (UT_HELP_VRSN);
  450. printf (UT_EXTRA_OPTS);
  451. printf (UT_HOST_PORT, 'p', myport);
  452. printf (UT_IPv46);
  453. printf (" %s\n", "-a, --attr=ATTRIBUTE");
  454. printf (" %s\n", _("ldap attribute to search (default: \"(objectclass=*)\""));
  455. printf (" %s\n", "-b, --base=BASE");
  456. printf (" %s\n", _("ldap base (eg. ou=my unit, o=my org, c=at"));
  457. printf (" %s\n", "-D, --bind=DN");
  458. printf (" %s\n", _("ldap bind DN (if required)"));
  459. printf (" %s\n", "-P, --pass=PASSWORD");
  460. printf (" %s\n", _("ldap password (if required)"));
  461. printf (" %s\n", "-T, --starttls");
  462. printf (" %s\n", _("use starttls mechanism introduced in protocol version 3"));
  463. printf (" %s\n", "-S, --ssl");
  464. printf (" %s %i\n", _("use ldaps (ldap v2 ssl method). this also sets the default port to"), LDAPS_PORT);
  465. printf (" %s\n", "-A, --age=INTEGER[,INTEGER]");
  466. printf (" %s\n", _("Minimum number of days a certificate has to be valid"));
  467. #ifdef HAVE_LDAP_SET_OPTION
  468. printf (" %s\n", "-2, --ver2");
  469. printf (" %s\n", _("use ldap protocol version 2"));
  470. printf (" %s\n", "-3, --ver3");
  471. printf (" %s\n", _("use ldap protocol version 3"));
  472. printf (" (%s %d)\n", _("default protocol version:"), DEFAULT_PROTOCOL);
  473. #endif
  474. printf (UT_WARN_CRIT);
  475. printf (" %s\n", "-W, --warn-entries=INTEGER");
  476. printf (" %s\n", _("Number of found entries to result in warning status"));
  477. printf (" %s\n", "-C, --crit-entries=INTEGER");
  478. printf (" %s\n", _("Number of found entries to result in critical status"));
  479. printf (UT_CONN_TIMEOUT, DEFAULT_SOCKET_TIMEOUT);
  480. printf (UT_VERBOSE);
  481. printf ("\n");
  482. printf ("%s\n", _("Notes:"));
  483. printf (" %s\n", _("If this plugin is called via 'check_ldaps', method 'STARTTLS' will be"));
  484. printf (_(" implied (using default port %i) unless --port=636 is specified. In that case\n"), DEFAULT_PORT);
  485. printf (" %s\n", _("'SSL on connect' will be used no matter how the plugin was called."));
  486. printf (" %s\n", _("This detection is deprecated, please use 'check_ldap' with the '--starttls' or '--ssl' flags"));
  487. printf (" %s\n", _("to define the behaviour explicitly instead."));
  488. printf (" %s\n", _("The parameters --warn-entries and --crit-entries are optional."));
  489. printf (UT_SUPPORT);
  490. }
  491. void
  492. print_usage (void)
  493. {
  494. printf ("%s\n", _("Usage:"));
  495. printf (" %s (-H <host>|-U <uri>) -b <base_dn> [-p <port>] [-a <attr>] [-D <binddn>]\n", progname);
  496. printf (" [-P <password>] [-w <warn_time>] [-c <crit_time>] [-t timeout] [-A <age>]%s\n",
  497. #ifdef HAVE_LDAP_SET_OPTION
  498. "\n [-2|-3] [-4|-6]"
  499. #else
  500. ""
  501. #endif
  502. );
  503. }
  504. #ifdef HAVE_SSL
  505. int ldap_check_cert (LDAP *ld)
  506. {
  507. SSL *ssl;
  508. int rc;
  509. rc = ldap_get_option(ld, LDAP_OPT_X_TLS_SSL_CTX, &ssl);
  510. if (rc == LDAP_OPT_ERROR || ssl == NULL) {
  511. printf ("%s\n",_("CRITICAL - Cannot retrieve ssl session from connection."));
  512. return STATE_CRITICAL;
  513. }
  514. return np_net_ssl_check_cert_real(ssl, days_till_exp_warn, days_till_exp_crit);
  515. }
  516. #else
  517. int ldap_check_cert (LDAP *ld) {
  518. return TRUE;
  519. }
  520. #endif