4
0

check_ldap.c 9.5 KB

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