check_ldap.c 12 KB

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