check_ldap.c 11 KB

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