check_ldap.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. /******************************************************************************
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation; either version 2 of the License, or
  5. (at your option) any later version.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License
  11. along with this program; if not, write to the Free Software
  12. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  13. $Id$
  14. ******************************************************************************/
  15. /* progname may be check_ldaps */
  16. char *progname = "check_ldap";
  17. const char *revision = "$Revision$";
  18. const char *copyright = "2000-2004";
  19. const char *email = "nagiosplug-devel@lists.sourceforge.net";
  20. #include "common.h"
  21. #include "netutils.h"
  22. #include "utils.h"
  23. #include <lber.h>
  24. #include <ldap.h>
  25. enum {
  26. UNDEFINED = 0,
  27. #ifdef HAVE_LDAP_SET_OPTION
  28. DEFAULT_PROTOCOL = 2,
  29. #endif
  30. DEFAULT_PORT = 389
  31. };
  32. int process_arguments (int, char **);
  33. int validate_arguments (void);
  34. void print_help (void);
  35. void print_usage (void);
  36. char ld_defattr[] = "(objectclass=*)";
  37. char *ld_attr = ld_defattr;
  38. char *ld_host = NULL;
  39. char *ld_base = NULL;
  40. char *ld_passwd = NULL;
  41. char *ld_binddn = NULL;
  42. int ld_port = DEFAULT_PORT;
  43. #ifdef HAVE_LDAP_SET_OPTION
  44. int ld_protocol = DEFAULT_PROTOCOL;
  45. #endif
  46. double warn_time = UNDEFINED;
  47. double crit_time = UNDEFINED;
  48. struct timeval tv;
  49. /* for ldap tls */
  50. char *SERVICE = "LDAP";
  51. int
  52. main (int argc, char *argv[])
  53. {
  54. LDAP *ld;
  55. LDAPMessage *result;
  56. /* should be int result = STATE_UNKNOWN; */
  57. int status = STATE_UNKNOWN;
  58. long microsec;
  59. double elapsed_time;
  60. /* for ldap tls */
  61. int tls;
  62. int version=3;
  63. setlocale (LC_ALL, "");
  64. bindtextdomain (PACKAGE, LOCALEDIR);
  65. textdomain (PACKAGE);
  66. if (strstr(argv[0],"check_ldaps")) {
  67. asprintf (&progname, "check_ldaps");
  68. }
  69. if (process_arguments (argc, argv) == ERROR)
  70. usage4 (_("Could not parse arguments"));
  71. /* initialize alarm signal handling */
  72. signal (SIGALRM, socket_timeout_alarm_handler);
  73. /* set socket timeout */
  74. alarm (socket_timeout);
  75. /* get the start time */
  76. gettimeofday (&tv, NULL);
  77. /* initialize ldap */
  78. #ifdef HAVE_LDAP_INIT
  79. if (!(ld = ldap_init (ld_host, ld_port))) {
  80. printf ("Could not connect to the server at port %i\n", ld_port);
  81. return STATE_CRITICAL;
  82. }
  83. #else
  84. if (!(ld = ldap_open (ld_host, ld_port))) {
  85. /*ldap_perror(ld, "ldap_open"); */
  86. printf (_("Could not connect to the server at port %i\n"), ld_port);
  87. return STATE_CRITICAL;
  88. }
  89. #endif /* HAVE_LDAP_INIT */
  90. #ifdef HAVE_LDAP_SET_OPTION
  91. /* set ldap options */
  92. if (ldap_set_option (ld, LDAP_OPT_PROTOCOL_VERSION, &ld_protocol) !=
  93. LDAP_OPT_SUCCESS ) {
  94. printf(_("Could not set protocol version %d\n"), ld_protocol);
  95. return STATE_CRITICAL;
  96. }
  97. #endif
  98. if (strstr(argv[0],"check_ldaps")) {
  99. /* with TLS */
  100. if ( ld_port == LDAPS_PORT ) {
  101. asprintf (&SERVICE, "LDAPS");
  102. #if defined(HAVE_LDAP_SET_OPTION) && defined(LDAP_OPT_X_TLS)
  103. /* ldaps: set option tls */
  104. tls = LDAP_OPT_X_TLS_HARD;
  105. if (ldap_set_option (ld, LDAP_OPT_X_TLS, &tls) != LDAP_SUCCESS)
  106. {
  107. /*ldap_perror(ld, "ldaps_option"); */
  108. printf (_("Could not init TLS at port %i!\n"), ld_port);
  109. return STATE_CRITICAL;
  110. }
  111. #else
  112. printf (_("TLS not supported by the libraries!\n"), ld_port);
  113. return STATE_CRITICAL;
  114. #endif /* LDAP_OPT_X_TLS */
  115. } else {
  116. asprintf (&SERVICE, "LDAP-TLS");
  117. #if defined(HAVE_LDAP_SET_OPTION) && defined(HAVE_LDAP_START_TLS_S)
  118. /* ldap with startTLS: set option version */
  119. if (ldap_get_option(ld,LDAP_OPT_PROTOCOL_VERSION, &version) == LDAP_OPT_SUCCESS )
  120. {
  121. if (version < LDAP_VERSION3)
  122. {
  123. version = LDAP_VERSION3;
  124. ldap_set_option(ld, LDAP_OPT_PROTOCOL_VERSION, &version);
  125. }
  126. }
  127. /* call start_tls */
  128. if (ldap_start_tls_s(ld, NULL, NULL) != LDAP_SUCCESS)
  129. {
  130. /*ldap_perror(ld, "ldap_start_tls"); */
  131. printf (_("Could not init startTLS at port %i!\n"), ld_port);
  132. return STATE_CRITICAL;
  133. }
  134. #else
  135. printf (_("startTLS not supported by the library, needs LDAPv3!\n"));
  136. return STATE_CRITICAL;
  137. #endif /* HAVE_LDAP_START_TLS_S */
  138. }
  139. }
  140. /* bind to the ldap server */
  141. if (ldap_bind_s (ld, ld_binddn, ld_passwd, LDAP_AUTH_SIMPLE) !=
  142. LDAP_SUCCESS) {
  143. /*ldap_perror(ld, "ldap_bind"); */
  144. printf (_("Could not bind to the ldap-server\n"));
  145. return STATE_CRITICAL;
  146. }
  147. /* do a search of all objectclasses in the base dn */
  148. if (ldap_search_s (ld, ld_base, LDAP_SCOPE_BASE, ld_attr, NULL, 0, &result)
  149. != LDAP_SUCCESS) {
  150. /*ldap_perror(ld, "ldap_search"); */
  151. printf (_("Could not search/find objectclasses in %s\n"), ld_base);
  152. return STATE_CRITICAL;
  153. }
  154. /* unbind from the ldap server */
  155. ldap_unbind (ld);
  156. /* reset the alarm handler */
  157. alarm (0);
  158. /* calcutate the elapsed time and compare to thresholds */
  159. microsec = deltime (tv);
  160. elapsed_time = (double)microsec / 1.0e6;
  161. if (crit_time!=UNDEFINED && elapsed_time>crit_time)
  162. status = STATE_CRITICAL;
  163. else if (warn_time!=UNDEFINED && elapsed_time>warn_time)
  164. status = STATE_WARNING;
  165. else
  166. status = STATE_OK;
  167. /* print out the result */
  168. printf (_("LDAP %s - %.3f seconds response time|%s\n"),
  169. state_text (status),
  170. elapsed_time,
  171. fperfdata ("time", elapsed_time, "s",
  172. (int)warn_time, warn_time,
  173. (int)crit_time, crit_time,
  174. TRUE, 0, FALSE, 0));
  175. return status;
  176. }
  177. /* process command-line arguments */
  178. int
  179. process_arguments (int argc, char **argv)
  180. {
  181. int c;
  182. int option = 0;
  183. /* initialize the long option struct */
  184. static struct option longopts[] = {
  185. {"help", no_argument, 0, 'h'},
  186. {"version", no_argument, 0, 'V'},
  187. {"timeout", required_argument, 0, 't'},
  188. {"host", required_argument, 0, 'H'},
  189. {"base", required_argument, 0, 'b'},
  190. {"attr", required_argument, 0, 'a'},
  191. {"bind", required_argument, 0, 'D'},
  192. {"pass", required_argument, 0, 'P'},
  193. #ifdef HAVE_LDAP_SET_OPTION
  194. {"ver2", no_argument, 0, '2'},
  195. {"ver3", no_argument, 0, '3'},
  196. #endif
  197. {"use-ipv4", no_argument, 0, '4'},
  198. {"use-ipv6", no_argument, 0, '6'},
  199. {"port", required_argument, 0, 'p'},
  200. {"warn", required_argument, 0, 'w'},
  201. {"crit", required_argument, 0, 'c'},
  202. {0, 0, 0, 0}
  203. };
  204. if (argc < 2)
  205. return ERROR;
  206. for (c = 1; c < argc; c++) {
  207. if (strcmp ("-to", argv[c]) == 0)
  208. strcpy (argv[c], "-t");
  209. }
  210. while (1) {
  211. c = getopt_long (argc, argv, "hV2346t:c:w:H:b:p:a:D:P:", longopts, &option);
  212. if (c == -1 || c == EOF)
  213. break;
  214. switch (c) {
  215. case 'h': /* help */
  216. print_help ();
  217. exit (STATE_OK);
  218. case 'V': /* version */
  219. print_revision (progname, revision);
  220. exit (STATE_OK);
  221. case 't': /* timeout period */
  222. if (!is_intnonneg (optarg))
  223. usage2 (_("Timeout interval must be a positive integer"), optarg);
  224. else
  225. socket_timeout = atoi (optarg);
  226. break;
  227. case 'H':
  228. ld_host = optarg;
  229. break;
  230. case 'b':
  231. ld_base = optarg;
  232. break;
  233. case 'p':
  234. ld_port = atoi (optarg);
  235. break;
  236. case 'a':
  237. ld_attr = optarg;
  238. break;
  239. case 'D':
  240. ld_binddn = optarg;
  241. break;
  242. case 'P':
  243. ld_passwd = optarg;
  244. break;
  245. case 'w':
  246. warn_time = strtod (optarg, NULL);
  247. break;
  248. case 'c':
  249. crit_time = strtod (optarg, NULL);
  250. break;
  251. #ifdef HAVE_LDAP_SET_OPTION
  252. case '2':
  253. ld_protocol = 2;
  254. break;
  255. case '3':
  256. ld_protocol = 3;
  257. break;
  258. #endif
  259. case '4':
  260. address_family = AF_INET;
  261. break;
  262. case '6':
  263. #ifdef USE_IPV6
  264. address_family = AF_INET6;
  265. #else
  266. usage (_("IPv6 support not available\n"));
  267. #endif
  268. break;
  269. default:
  270. usage2 (_("Unknown argument"), optarg);
  271. }
  272. }
  273. c = optind;
  274. if (ld_host == NULL && is_host(argv[c]))
  275. ld_host = strdup (argv[c++]);
  276. if (ld_base == NULL && argv[c])
  277. ld_base = strdup (argv[c++]);
  278. return validate_arguments ();
  279. }
  280. int
  281. validate_arguments ()
  282. {
  283. if (ld_host==NULL || strlen(ld_host)==0)
  284. usage4 (_("Please specify the host name\n"));
  285. if (ld_base==NULL || strlen(ld_base)==0)
  286. usage4 (_("Please specify the LDAP base\n"));
  287. return OK;
  288. }
  289. void
  290. print_help (void)
  291. {
  292. char *myport;
  293. asprintf (&myport, "%d", DEFAULT_PORT);
  294. print_revision (progname, revision);
  295. printf ("Copyright (c) 1999 Didi Rieder (adrieder@sbox.tu-graz.ac.at)\n");
  296. printf (COPYRIGHT, copyright, email);
  297. print_usage ();
  298. printf (_(UT_HELP_VRSN));
  299. printf (_(UT_HOST_PORT), 'p', myport);
  300. printf (_(UT_IPv46));
  301. printf (_("\
  302. -a [--attr]\n\
  303. ldap attribute to search (default: \"(objectclass=*)\"\n\
  304. -b [--base]\n\
  305. ldap base (eg. ou=my unit, o=my org, c=at)\n\
  306. -D [--bind]\n\
  307. ldap bind DN (if required)\n\
  308. -P [--pass]\n\
  309. ldap password (if required)\n"));
  310. #ifdef HAVE_LDAP_SET_OPTION
  311. printf (_("\
  312. -2 [--ver2]\n\
  313. use ldap protocol version 2\n\
  314. -3 [--ver3]\n\
  315. use ldap protocol version 3\n\
  316. (default protocol version: %d)\n"),
  317. DEFAULT_PROTOCOL);
  318. #endif
  319. printf (_(UT_WARN_CRIT));
  320. printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
  321. printf (_(UT_VERBOSE));
  322. printf (_(UT_SUPPORT));
  323. }
  324. void
  325. print_usage (void)
  326. {
  327. printf ("\
  328. Usage: %s -H <host> -b <base_dn> [-p <port>] [-a <attr>] [-D <binddn>]\n\
  329. [-P <password>] [-w <warn_time>] [-c <crit_time>]\n\
  330. [-t timeout]%s\n",
  331. //(Note: all times are in seconds.)\n",
  332. progname,
  333. #ifdef HAVE_LDAP_SET_OPTION
  334. " [-2|-3] [-4|-6]"
  335. #else
  336. ""
  337. #endif
  338. );
  339. }