check_ldap.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. /*****************************************************************************
  2. *
  3. * Nagios check_ldap plugin
  4. *
  5. * License: GPL
  6. * Copyright (c) 2000-2008 Nagios Plugins Development Team
  7. *
  8. * Last Modified: $Date$
  9. *
  10. * Description:
  11. *
  12. * This file contains the check_ldap plugin
  13. *
  14. *
  15. * This program is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU General Public License as published by
  17. * the Free Software Foundation, either version 3 of the License, or
  18. * (at your option) any later version.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU General Public License
  26. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  27. *
  28. * $Id$
  29. *
  30. *****************************************************************************/
  31. /* progname may be check_ldaps */
  32. char *progname = "check_ldap";
  33. const char *revision = "$Revision$";
  34. const char *copyright = "2000-2008";
  35. const char *email = "nagiosplug-devel@lists.sourceforge.net";
  36. #include "common.h"
  37. #include "netutils.h"
  38. #include "utils.h"
  39. #include <lber.h>
  40. #include <ldap.h>
  41. enum {
  42. UNDEFINED = 0,
  43. #ifdef HAVE_LDAP_SET_OPTION
  44. DEFAULT_PROTOCOL = 2,
  45. #endif
  46. DEFAULT_PORT = 389
  47. };
  48. int process_arguments (int, char **);
  49. int validate_arguments (void);
  50. void print_help (void);
  51. void print_usage (void);
  52. char ld_defattr[] = "(objectclass=*)";
  53. char *ld_attr = ld_defattr;
  54. char *ld_host = NULL;
  55. char *ld_base = NULL;
  56. char *ld_passwd = NULL;
  57. char *ld_binddn = NULL;
  58. int ld_port = DEFAULT_PORT;
  59. #ifdef HAVE_LDAP_SET_OPTION
  60. int ld_protocol = DEFAULT_PROTOCOL;
  61. #endif
  62. #ifndef LDAP_OPT_SUCCESS
  63. # define LDAP_OPT_SUCCESS LDAP_SUCCESS
  64. #endif
  65. double warn_time = UNDEFINED;
  66. double crit_time = UNDEFINED;
  67. struct timeval tv;
  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. setlocale (LC_ALL, "");
  86. bindtextdomain (PACKAGE, LOCALEDIR);
  87. textdomain (PACKAGE);
  88. if (strstr(argv[0],"check_ldaps")) {
  89. asprintf (&progname, "check_ldaps");
  90. }
  91. /* Parse extra opts if any */
  92. argv=np_extra_opts (&argc, argv, progname);
  93. if (process_arguments (argc, argv) == ERROR)
  94. usage4 (_("Could not parse arguments"));
  95. if (strstr(argv[0],"check_ldaps") && ! starttls && ! ssl_on_connect)
  96. starttls = TRUE;
  97. /* initialize alarm signal handling */
  98. signal (SIGALRM, socket_timeout_alarm_handler);
  99. /* set socket timeout */
  100. alarm (socket_timeout);
  101. /* get the start time */
  102. gettimeofday (&tv, NULL);
  103. /* initialize ldap */
  104. #ifdef HAVE_LDAP_INIT
  105. if (!(ld = ldap_init (ld_host, ld_port))) {
  106. printf ("Could not connect to the server at port %i\n", ld_port);
  107. return STATE_CRITICAL;
  108. }
  109. #else
  110. if (!(ld = ldap_open (ld_host, ld_port))) {
  111. if (verbose)
  112. ldap_perror(ld, "ldap_open");
  113. printf (_("Could not connect to the server at port %i\n"), ld_port);
  114. return STATE_CRITICAL;
  115. }
  116. #endif /* HAVE_LDAP_INIT */
  117. #ifdef HAVE_LDAP_SET_OPTION
  118. /* set ldap options */
  119. if (ldap_set_option (ld, LDAP_OPT_PROTOCOL_VERSION, &ld_protocol) !=
  120. LDAP_OPT_SUCCESS ) {
  121. printf(_("Could not set protocol version %d\n"), ld_protocol);
  122. return STATE_CRITICAL;
  123. }
  124. #endif
  125. if (ld_port == LDAPS_PORT || ssl_on_connect) {
  126. asprintf (&SERVICE, "LDAPS");
  127. #if defined(HAVE_LDAP_SET_OPTION) && defined(LDAP_OPT_X_TLS)
  128. /* ldaps: set option tls */
  129. tls = LDAP_OPT_X_TLS_HARD;
  130. if (ldap_set_option (ld, LDAP_OPT_X_TLS, &tls) != LDAP_SUCCESS)
  131. {
  132. if (verbose)
  133. ldap_perror(ld, "ldaps_option");
  134. printf (_("Could not init TLS at port %i!\n"), ld_port);
  135. return STATE_CRITICAL;
  136. }
  137. #else
  138. printf (_("TLS not supported by the libraries!\n"));
  139. return STATE_CRITICAL;
  140. #endif /* LDAP_OPT_X_TLS */
  141. } else if (starttls) {
  142. asprintf (&SERVICE, "LDAP-TLS");
  143. #if defined(HAVE_LDAP_SET_OPTION) && defined(HAVE_LDAP_START_TLS_S)
  144. /* ldap with startTLS: set option version */
  145. if (ldap_get_option(ld,LDAP_OPT_PROTOCOL_VERSION, &version) == LDAP_OPT_SUCCESS )
  146. {
  147. if (version < LDAP_VERSION3)
  148. {
  149. version = LDAP_VERSION3;
  150. ldap_set_option(ld, LDAP_OPT_PROTOCOL_VERSION, &version);
  151. }
  152. }
  153. /* call start_tls */
  154. if (ldap_start_tls_s(ld, NULL, NULL) != LDAP_SUCCESS)
  155. {
  156. if (verbose)
  157. ldap_perror(ld, "ldap_start_tls");
  158. printf (_("Could not init startTLS at port %i!\n"), ld_port);
  159. return STATE_CRITICAL;
  160. }
  161. #else
  162. printf (_("startTLS not supported by the library, needs LDAPv3!\n"));
  163. return STATE_CRITICAL;
  164. #endif /* HAVE_LDAP_START_TLS_S */
  165. }
  166. /* bind to the ldap server */
  167. if (ldap_bind_s (ld, ld_binddn, ld_passwd, LDAP_AUTH_SIMPLE) !=
  168. LDAP_SUCCESS) {
  169. if (verbose)
  170. ldap_perror(ld, "ldap_bind");
  171. printf (_("Could not bind to the ldap-server\n"));
  172. return STATE_CRITICAL;
  173. }
  174. /* do a search of all objectclasses in the base dn */
  175. if (ldap_search_s (ld, ld_base, LDAP_SCOPE_BASE, ld_attr, NULL, 0, &result)
  176. != LDAP_SUCCESS) {
  177. if (verbose)
  178. ldap_perror(ld, "ldap_search");
  179. printf (_("Could not search/find objectclasses in %s\n"), ld_base);
  180. return STATE_CRITICAL;
  181. }
  182. /* unbind from the ldap server */
  183. ldap_unbind (ld);
  184. /* reset the alarm handler */
  185. alarm (0);
  186. /* calcutate the elapsed time and compare to thresholds */
  187. microsec = deltime (tv);
  188. elapsed_time = (double)microsec / 1.0e6;
  189. if (crit_time!=UNDEFINED && elapsed_time>crit_time)
  190. status = STATE_CRITICAL;
  191. else if (warn_time!=UNDEFINED && elapsed_time>warn_time)
  192. status = STATE_WARNING;
  193. else
  194. status = STATE_OK;
  195. /* print out the result */
  196. printf (_("LDAP %s - %.3f seconds response time|%s\n"),
  197. state_text (status),
  198. elapsed_time,
  199. fperfdata ("time", elapsed_time, "s",
  200. (int)warn_time, warn_time,
  201. (int)crit_time, crit_time,
  202. TRUE, 0, FALSE, 0));
  203. return status;
  204. }
  205. /* process command-line arguments */
  206. int
  207. process_arguments (int argc, char **argv)
  208. {
  209. int c;
  210. int option = 0;
  211. /* initialize the long option struct */
  212. static struct option longopts[] = {
  213. {"help", no_argument, 0, 'h'},
  214. {"version", no_argument, 0, 'V'},
  215. {"timeout", required_argument, 0, 't'},
  216. {"host", required_argument, 0, 'H'},
  217. {"base", required_argument, 0, 'b'},
  218. {"attr", required_argument, 0, 'a'},
  219. {"bind", required_argument, 0, 'D'},
  220. {"pass", required_argument, 0, 'P'},
  221. #ifdef HAVE_LDAP_SET_OPTION
  222. {"ver2", no_argument, 0, '2'},
  223. {"ver3", no_argument, 0, '3'},
  224. #endif
  225. {"starttls", no_argument, 0, 'T'},
  226. {"ssl", no_argument, 0, 'S'},
  227. {"use-ipv4", no_argument, 0, '4'},
  228. {"use-ipv6", no_argument, 0, '6'},
  229. {"port", required_argument, 0, 'p'},
  230. {"warn", required_argument, 0, 'w'},
  231. {"crit", required_argument, 0, 'c'},
  232. {"verbose", no_argument, 0, 'v'},
  233. {0, 0, 0, 0}
  234. };
  235. if (argc < 2)
  236. return ERROR;
  237. for (c = 1; c < argc; c++) {
  238. if (strcmp ("-to", argv[c]) == 0)
  239. strcpy (argv[c], "-t");
  240. }
  241. while (1) {
  242. c = getopt_long (argc, argv, "hvV234TS6t:c:w:H:b:p:a:D:P:", longopts, &option);
  243. if (c == -1 || c == EOF)
  244. break;
  245. switch (c) {
  246. case 'h': /* help */
  247. print_help ();
  248. exit (STATE_OK);
  249. case 'V': /* version */
  250. print_revision (progname, revision);
  251. exit (STATE_OK);
  252. case 't': /* timeout period */
  253. if (!is_intnonneg (optarg))
  254. usage2 (_("Timeout interval must be a positive integer"), optarg);
  255. else
  256. socket_timeout = atoi (optarg);
  257. break;
  258. case 'H':
  259. ld_host = optarg;
  260. break;
  261. case 'b':
  262. ld_base = optarg;
  263. break;
  264. case 'p':
  265. ld_port = atoi (optarg);
  266. break;
  267. case 'a':
  268. ld_attr = optarg;
  269. break;
  270. case 'D':
  271. ld_binddn = optarg;
  272. break;
  273. case 'P':
  274. ld_passwd = optarg;
  275. break;
  276. case 'w':
  277. warn_time = strtod (optarg, NULL);
  278. break;
  279. case 'c':
  280. crit_time = strtod (optarg, NULL);
  281. break;
  282. #ifdef HAVE_LDAP_SET_OPTION
  283. case '2':
  284. ld_protocol = 2;
  285. break;
  286. case '3':
  287. ld_protocol = 3;
  288. break;
  289. #endif
  290. case '4':
  291. address_family = AF_INET;
  292. break;
  293. case 'v':
  294. verbose++;
  295. break;
  296. case 'T':
  297. if (! ssl_on_connect)
  298. starttls = TRUE;
  299. else
  300. usage_va(_("%s cannot be combined with %s"), "-T/--starttls", "-S/--ssl");
  301. break;
  302. case 'S':
  303. if (! starttls) {
  304. ssl_on_connect = TRUE;
  305. ld_port = LDAPS_PORT;
  306. } else
  307. usage_va(_("%s cannot be combined with %s"), "-S/--ssl", "-T/--starttls");
  308. break;
  309. case '6':
  310. #ifdef USE_IPV6
  311. address_family = AF_INET6;
  312. #else
  313. usage (_("IPv6 support not available\n"));
  314. #endif
  315. break;
  316. default:
  317. usage5 ();
  318. }
  319. }
  320. c = optind;
  321. if (ld_host == NULL && is_host(argv[c]))
  322. ld_host = strdup (argv[c++]);
  323. if (ld_base == NULL && argv[c])
  324. ld_base = strdup (argv[c++]);
  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 || strlen(ld_base)==0)
  333. usage4 (_("Please specify the LDAP base\n"));
  334. return OK;
  335. }
  336. void
  337. print_help (void)
  338. {
  339. char *myport;
  340. asprintf (&myport, "%d", DEFAULT_PORT);
  341. print_revision (progname, revision);
  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. #ifdef NP_EXTRA_OPTS
  380. printf ("\n");
  381. printf (_(UT_EXTRA_OPTS_NOTES));
  382. #endif
  383. printf (_(UT_SUPPORT));
  384. }
  385. /* todo
  386. * add option -4 and -6 to the long manual
  387. *
  388. */
  389. void
  390. print_usage (void)
  391. {
  392. printf (_("Usage:"));
  393. printf (" %s -H <host> -b <base_dn> [-p <port>] [-a <attr>] [-D <binddn>]",progname);
  394. printf ("\n [-P <password>] [-w <warn_time>] [-c <crit_time>] [-t timeout]%s\n",
  395. #ifdef HAVE_LDAP_SET_OPTION
  396. "\n [-2|-3] [-4|-6]"
  397. #else
  398. ""
  399. #endif
  400. );
  401. }