check_ldap.c 11 KB

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