check_ldap.c 11 KB

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