4
0

check_ldap.c 13 KB

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