check_ldap.c 11 KB

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