check_ldap.c 11 KB

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