check_ldap.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  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. 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 (socket_timeout);
  99. /* get the start time */
  100. gettimeofday (&tv, NULL);
  101. /* initialize ldap */
  102. #ifdef HAVE_LDAP_INIT
  103. if (!(ld = ldap_init (ld_host, ld_port))) {
  104. printf ("Could not connect to the server at port %i\n", ld_port);
  105. return STATE_CRITICAL;
  106. }
  107. #else
  108. if (!(ld = ldap_open (ld_host, ld_port))) {
  109. if (verbose)
  110. ldap_perror(ld, "ldap_open");
  111. printf (_("Could not connect to the server at port %i\n"), ld_port);
  112. return STATE_CRITICAL;
  113. }
  114. #endif /* HAVE_LDAP_INIT */
  115. #ifdef HAVE_LDAP_SET_OPTION
  116. /* set ldap options */
  117. if (ldap_set_option (ld, LDAP_OPT_PROTOCOL_VERSION, &ld_protocol) !=
  118. LDAP_OPT_SUCCESS ) {
  119. printf(_("Could not set protocol version %d\n"), ld_protocol);
  120. return STATE_CRITICAL;
  121. }
  122. #endif
  123. if (ld_port == LDAPS_PORT || ssl_on_connect) {
  124. asprintf (&SERVICE, "LDAPS");
  125. #if defined(HAVE_LDAP_SET_OPTION) && defined(LDAP_OPT_X_TLS)
  126. /* ldaps: set option tls */
  127. tls = LDAP_OPT_X_TLS_HARD;
  128. if (ldap_set_option (ld, LDAP_OPT_X_TLS, &tls) != LDAP_SUCCESS)
  129. {
  130. if (verbose)
  131. ldap_perror(ld, "ldaps_option");
  132. printf (_("Could not init TLS at port %i!\n"), ld_port);
  133. return STATE_CRITICAL;
  134. }
  135. #else
  136. printf (_("TLS not supported by the libraries!\n"));
  137. return STATE_CRITICAL;
  138. #endif /* LDAP_OPT_X_TLS */
  139. } else if (starttls) {
  140. asprintf (&SERVICE, "LDAP-TLS");
  141. #if defined(HAVE_LDAP_SET_OPTION) && defined(HAVE_LDAP_START_TLS_S)
  142. /* ldap with startTLS: set option version */
  143. if (ldap_get_option(ld,LDAP_OPT_PROTOCOL_VERSION, &version) == LDAP_OPT_SUCCESS )
  144. {
  145. if (version < LDAP_VERSION3)
  146. {
  147. version = LDAP_VERSION3;
  148. ldap_set_option(ld, LDAP_OPT_PROTOCOL_VERSION, &version);
  149. }
  150. }
  151. /* call start_tls */
  152. if (ldap_start_tls_s(ld, NULL, NULL) != LDAP_SUCCESS)
  153. {
  154. if (verbose)
  155. ldap_perror(ld, "ldap_start_tls");
  156. printf (_("Could not init startTLS at port %i!\n"), ld_port);
  157. return STATE_CRITICAL;
  158. }
  159. #else
  160. printf (_("startTLS not supported by the library, needs LDAPv3!\n"));
  161. return STATE_CRITICAL;
  162. #endif /* HAVE_LDAP_START_TLS_S */
  163. }
  164. /* bind to the ldap server */
  165. if (ldap_bind_s (ld, ld_binddn, ld_passwd, LDAP_AUTH_SIMPLE) !=
  166. LDAP_SUCCESS) {
  167. if (verbose)
  168. ldap_perror(ld, "ldap_bind");
  169. printf (_("Could not bind to the ldap-server\n"));
  170. return STATE_CRITICAL;
  171. }
  172. /* do a search of all objectclasses in the base dn */
  173. if (ldap_search_s (ld, ld_base, LDAP_SCOPE_BASE, ld_attr, NULL, 0, &result)
  174. != LDAP_SUCCESS) {
  175. if (verbose)
  176. ldap_perror(ld, "ldap_search");
  177. printf (_("Could not search/find objectclasses in %s\n"), ld_base);
  178. return STATE_CRITICAL;
  179. }
  180. /* unbind from the ldap server */
  181. ldap_unbind (ld);
  182. /* reset the alarm handler */
  183. alarm (0);
  184. /* calcutate the elapsed time and compare to thresholds */
  185. microsec = deltime (tv);
  186. elapsed_time = (double)microsec / 1.0e6;
  187. if (crit_time!=UNDEFINED && elapsed_time>crit_time)
  188. status = STATE_CRITICAL;
  189. else if (warn_time!=UNDEFINED && elapsed_time>warn_time)
  190. status = STATE_WARNING;
  191. else
  192. status = STATE_OK;
  193. /* print out the result */
  194. printf (_("LDAP %s - %.3f seconds response time|%s\n"),
  195. state_text (status),
  196. elapsed_time,
  197. fperfdata ("time", elapsed_time, "s",
  198. (int)warn_time, warn_time,
  199. (int)crit_time, crit_time,
  200. TRUE, 0, FALSE, 0));
  201. return status;
  202. }
  203. /* process command-line arguments */
  204. int
  205. process_arguments (int argc, char **argv)
  206. {
  207. int c;
  208. int option = 0;
  209. /* initialize the long option struct */
  210. static struct option longopts[] = {
  211. {"help", no_argument, 0, 'h'},
  212. {"version", no_argument, 0, 'V'},
  213. {"timeout", required_argument, 0, 't'},
  214. {"host", required_argument, 0, 'H'},
  215. {"base", required_argument, 0, 'b'},
  216. {"attr", required_argument, 0, 'a'},
  217. {"bind", required_argument, 0, 'D'},
  218. {"pass", required_argument, 0, 'P'},
  219. #ifdef HAVE_LDAP_SET_OPTION
  220. {"ver2", no_argument, 0, '2'},
  221. {"ver3", no_argument, 0, '3'},
  222. #endif
  223. {"starttls", no_argument, 0, 'T'},
  224. {"ssl", no_argument, 0, 'S'},
  225. {"use-ipv4", no_argument, 0, '4'},
  226. {"use-ipv6", no_argument, 0, '6'},
  227. {"port", required_argument, 0, 'p'},
  228. {"warn", required_argument, 0, 'w'},
  229. {"crit", required_argument, 0, 'c'},
  230. {"verbose", no_argument, 0, 'v'},
  231. {0, 0, 0, 0}
  232. };
  233. if (argc < 2)
  234. return ERROR;
  235. for (c = 1; c < argc; c++) {
  236. if (strcmp ("-to", argv[c]) == 0)
  237. strcpy (argv[c], "-t");
  238. }
  239. while (1) {
  240. c = getopt_long (argc, argv, "hvV234TS6t:c:w:H:b:p:a:D:P:", longopts, &option);
  241. if (c == -1 || c == EOF)
  242. break;
  243. switch (c) {
  244. case 'h': /* help */
  245. print_help ();
  246. exit (STATE_OK);
  247. case 'V': /* version */
  248. print_revision (progname, revision);
  249. exit (STATE_OK);
  250. case 't': /* timeout period */
  251. if (!is_intnonneg (optarg))
  252. usage2 (_("Timeout interval must be a positive integer"), optarg);
  253. else
  254. socket_timeout = atoi (optarg);
  255. break;
  256. case 'H':
  257. ld_host = optarg;
  258. break;
  259. case 'b':
  260. ld_base = optarg;
  261. break;
  262. case 'p':
  263. ld_port = atoi (optarg);
  264. break;
  265. case 'a':
  266. ld_attr = optarg;
  267. break;
  268. case 'D':
  269. ld_binddn = optarg;
  270. break;
  271. case 'P':
  272. ld_passwd = optarg;
  273. break;
  274. case 'w':
  275. warn_time = strtod (optarg, NULL);
  276. break;
  277. case 'c':
  278. crit_time = strtod (optarg, NULL);
  279. break;
  280. #ifdef HAVE_LDAP_SET_OPTION
  281. case '2':
  282. ld_protocol = 2;
  283. break;
  284. case '3':
  285. ld_protocol = 3;
  286. break;
  287. #endif
  288. case '4':
  289. address_family = AF_INET;
  290. break;
  291. case 'v':
  292. verbose++;
  293. break;
  294. case 'T':
  295. if (! ssl_on_connect)
  296. starttls = TRUE;
  297. else
  298. usage_va(_("%s cannot be combined with %s"), "-T/--starttls", "-S/--ssl");
  299. break;
  300. case 'S':
  301. if (! starttls) {
  302. ssl_on_connect = TRUE;
  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. return validate_arguments ();
  324. }
  325. int
  326. validate_arguments ()
  327. {
  328. if (ld_host==NULL || strlen(ld_host)==0)
  329. usage4 (_("Please specify the host name\n"));
  330. if (ld_base==NULL || strlen(ld_base)==0)
  331. usage4 (_("Please specify the LDAP base\n"));
  332. return OK;
  333. }
  334. void
  335. print_help (void)
  336. {
  337. char *myport;
  338. asprintf (&myport, "%d", DEFAULT_PORT);
  339. print_revision (progname, revision);
  340. printf ("Copyright (c) 1999 Didi Rieder (adrieder@sbox.tu-graz.ac.at)\n");
  341. printf (COPYRIGHT, copyright, email);
  342. printf ("\n\n");
  343. print_usage ();
  344. printf (_(UT_HELP_VRSN));
  345. printf (_(UT_HOST_PORT), 'p', myport);
  346. printf (_(UT_IPv46));
  347. printf (" %s\n", "-a [--attr]");
  348. printf (" %s\n", _("ldap attribute to search (default: \"(objectclass=*)\""));
  349. printf (" %s\n", "-b [--base]");
  350. printf (" %s\n", _("ldap base (eg. ou=my unit, o=my org, c=at"));
  351. printf (" %s\n", "-D [--bind]");
  352. printf (" %s\n", _("ldap bind DN (if required)"));
  353. printf (" %s\n", "-P [--pass]");
  354. printf (" %s\n", _("ldap password (if required)"));
  355. printf (" %s\n", "-T [--starttls]");
  356. printf (" %s\n", _("use starttls mechanism introduced in protocol version 3"));
  357. printf (" %s\n", "-S [--ssl]");
  358. printf (" %s %i\n", _("use ldaps (ldap v2 ssl method). this also sets the default port to"), LDAPS_PORT);
  359. #ifdef HAVE_LDAP_SET_OPTION
  360. printf (" %s\n", "-2 [--ver2]");
  361. printf (" %s\n", _("use ldap protocol version 2"));
  362. printf (" %s\n", "-3 [--ver3]");
  363. printf (" %s\n", _("use ldap protocol version 3"));
  364. printf (" (%s %d)\n", _("default protocol version:"), DEFAULT_PROTOCOL);
  365. #endif
  366. printf (_(UT_WARN_CRIT));
  367. printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
  368. printf (_(UT_VERBOSE));
  369. printf ("\n%s\n", _("Note:"));
  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 %s"), DEFAULT_PORT, "\n");
  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. printf (_(UT_SUPPORT));
  376. }
  377. /* todo
  378. * add option -4 and -6 to the long manual
  379. *
  380. */
  381. void
  382. print_usage (void)
  383. {
  384. printf (_("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. }