check_ldap.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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. int
  49. main (int argc, char *argv[])
  50. {
  51. LDAP *ld;
  52. LDAPMessage *result;
  53. /* should be int result = STATE_UNKNOWN; */
  54. int status = STATE_UNKNOWN;
  55. long microsec;
  56. double elapsed_time;
  57. setlocale (LC_ALL, "");
  58. bindtextdomain (PACKAGE, LOCALEDIR);
  59. textdomain (PACKAGE);
  60. if (process_arguments (argc, argv) != TRUE)
  61. usage4 (_("Could not parse arguments"));
  62. /* initialize alarm signal handling */
  63. signal (SIGALRM, socket_timeout_alarm_handler);
  64. /* set socket timeout */
  65. alarm (socket_timeout);
  66. /* get the start time */
  67. gettimeofday (&tv, NULL);
  68. /* initialize ldap */
  69. if (!(ld = ldap_open (ld_host, ld_port))) {
  70. /*ldap_perror(ld, "ldap_open"); */
  71. printf (_("Could not connect to the server at port %i\n"), ld_port);
  72. return STATE_CRITICAL;
  73. }
  74. #ifdef HAVE_LDAP_SET_OPTION
  75. /* set ldap options */
  76. if (ldap_set_option (ld, LDAP_OPT_PROTOCOL_VERSION, &ld_protocol) !=
  77. LDAP_OPT_SUCCESS ) {
  78. printf(_("Could not set protocol version %d\n"), ld_protocol);
  79. return STATE_CRITICAL;
  80. }
  81. #endif
  82. /* bind to the ldap server */
  83. if (ldap_bind_s (ld, ld_binddn, ld_passwd, LDAP_AUTH_SIMPLE) !=
  84. LDAP_SUCCESS) {
  85. /*ldap_perror(ld, "ldap_bind"); */
  86. printf (_("Could not bind to the ldap-server\n"));
  87. return STATE_CRITICAL;
  88. }
  89. /* do a search of all objectclasses in the base dn */
  90. if (ldap_search_s (ld, ld_base, LDAP_SCOPE_BASE, ld_attr, NULL, 0, &result)
  91. != LDAP_SUCCESS) {
  92. /*ldap_perror(ld, "ldap_search"); */
  93. printf (_("Could not search/find objectclasses in %s\n"), ld_base);
  94. return STATE_CRITICAL;
  95. }
  96. /* unbind from the ldap server */
  97. ldap_unbind (ld);
  98. /* reset the alarm handler */
  99. alarm (0);
  100. /* calcutate the elapsed time and compare to thresholds */
  101. microsec = deltime (tv);
  102. elapsed_time = (double)microsec / 1.0e6;
  103. if (crit_time!=UNDEFINED && elapsed_time>crit_time)
  104. status = STATE_CRITICAL;
  105. else if (warn_time!=UNDEFINED && elapsed_time>warn_time)
  106. status = STATE_WARNING;
  107. else
  108. status = STATE_OK;
  109. /* print out the result */
  110. printf (_("LDAP %s - %.3f seconds response time|%s\n"),
  111. state_text (status),
  112. elapsed_time,
  113. fperfdata ("time", elapsed_time, "s",
  114. (int)warn_time, warn_time,
  115. (int)crit_time, crit_time,
  116. TRUE, 0, FALSE, 0));
  117. return status;
  118. }
  119. /* process command-line arguments */
  120. int
  121. process_arguments (int argc, char **argv)
  122. {
  123. int c;
  124. int option = 0;
  125. /* initialize the long option struct */
  126. static struct option longopts[] = {
  127. {"help", no_argument, 0, 'h'},
  128. {"version", no_argument, 0, 'V'},
  129. {"timeout", required_argument, 0, 't'},
  130. {"host", required_argument, 0, 'H'},
  131. {"base", required_argument, 0, 'b'},
  132. {"attr", required_argument, 0, 'a'},
  133. {"bind", required_argument, 0, 'D'},
  134. {"pass", required_argument, 0, 'P'},
  135. #ifdef HAVE_LDAP_SET_OPTION
  136. {"ver2", no_argument, 0, '2'},
  137. {"ver3", no_argument, 0, '3'},
  138. #endif
  139. {"use-ipv4", no_argument, 0, '4'},
  140. {"use-ipv6", no_argument, 0, '6'},
  141. {"port", required_argument, 0, 'p'},
  142. {"warn", required_argument, 0, 'w'},
  143. {"crit", required_argument, 0, 'c'},
  144. {0, 0, 0, 0}
  145. };
  146. if (argc < 2)
  147. return ERROR;
  148. for (c = 1; c < argc; c++) {
  149. if (strcmp ("-to", argv[c]) == 0)
  150. strcpy (argv[c], "-t");
  151. }
  152. while (1) {
  153. c = getopt_long (argc, argv, "hV2346t:c:w:H:b:p:a:D:P:", longopts, &option);
  154. if (c == -1 || c == EOF)
  155. break;
  156. switch (c) {
  157. case 'h': /* help */
  158. print_help ();
  159. exit (STATE_OK);
  160. case 'V': /* version */
  161. print_revision (progname, revision);
  162. exit (STATE_OK);
  163. case 't': /* timeout period */
  164. if (!is_intnonneg (optarg))
  165. usage2 (_("Timeout interval must be a positive integer"), optarg);
  166. else
  167. socket_timeout = atoi (optarg);
  168. break;
  169. case 'H':
  170. ld_host = optarg;
  171. break;
  172. case 'b':
  173. ld_base = optarg;
  174. break;
  175. case 'p':
  176. ld_port = atoi (optarg);
  177. break;
  178. case 'a':
  179. ld_attr = optarg;
  180. break;
  181. case 'D':
  182. ld_binddn = optarg;
  183. break;
  184. case 'P':
  185. ld_passwd = optarg;
  186. break;
  187. case 'w':
  188. warn_time = strtod (optarg, NULL);
  189. break;
  190. case 'c':
  191. crit_time = strtod (optarg, NULL);
  192. break;
  193. #ifdef HAVE_LDAP_SET_OPTION
  194. case '2':
  195. ld_protocol = 2;
  196. break;
  197. case '3':
  198. ld_protocol = 3;
  199. break;
  200. #endif
  201. case '4':
  202. address_family = AF_INET;
  203. break;
  204. case '6':
  205. #ifdef USE_IPV6
  206. address_family = AF_INET6;
  207. #else
  208. usage (_("IPv6 support not available\n"));
  209. #endif
  210. break;
  211. default:
  212. printf (_("%s: Unknown argument: %s\n\n"), progname, optarg);
  213. print_usage ();
  214. exit (STATE_UNKNOWN);
  215. }
  216. }
  217. c = optind;
  218. if (ld_host == NULL && is_host(argv[c]))
  219. ld_host = strdup (argv[c++]);
  220. if (ld_base == NULL && argv[c])
  221. ld_base = strdup (argv[c++]);
  222. return validate_arguments ();
  223. }
  224. int
  225. validate_arguments ()
  226. {
  227. if (ld_host==NULL || strlen(ld_host)==0)
  228. usage (_("please specify the host name\n"));
  229. if (ld_base==NULL || strlen(ld_base)==0)
  230. usage (_("please specify the LDAP base\n"));
  231. return OK;
  232. }
  233. void
  234. print_help (void)
  235. {
  236. char *myport;
  237. asprintf (&myport, "%d", DEFAULT_PORT);
  238. print_revision (progname, revision);
  239. printf ("Copyright (c) 1999 Didi Rieder (adrieder@sbox.tu-graz.ac.at)\n");
  240. printf (COPYRIGHT, copyright, email);
  241. print_usage ();
  242. printf (_(UT_HELP_VRSN));
  243. printf (_(UT_HOST_PORT), 'p', myport);
  244. printf (_(UT_IPv46));
  245. printf (_("\
  246. -a [--attr]\n\
  247. ldap attribute to search (default: \"(objectclass=*)\"\n\
  248. -b [--base]\n\
  249. ldap base (eg. ou=my unit, o=my org, c=at)\n\
  250. -D [--bind]\n\
  251. ldap bind DN (if required)\n\
  252. -P [--pass]\n\
  253. ldap password (if required)\n"));
  254. #ifdef HAVE_LDAP_SET_OPTION
  255. printf (_("\
  256. -2 [--ver2]\n\
  257. use ldap protocol version 2\n\
  258. -3 [--ver3]\n\
  259. use ldap protocol version 3\n\
  260. (default protocol version: %d)\n"),
  261. DEFAULT_PROTOCOL);
  262. #endif
  263. printf (_(UT_WARN_CRIT));
  264. printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
  265. printf (_(UT_VERBOSE));
  266. printf (_(UT_SUPPORT));
  267. }
  268. void
  269. print_usage (void)
  270. {
  271. printf (_("\
  272. Usage: %s -H <host> -b <base_dn> [-p <port>] [-a <attr>] [-D <binddn>]\n\
  273. [-P <password>] [-w <warn_time>] [-c <crit_time>] [-t timeout]%s\n\
  274. (Note: all times are in seconds.)\n"),
  275. progname,
  276. #ifdef HAVE_LDAP_SET_OPTION
  277. " [-2|-3] [-4|-6]"
  278. #else
  279. ""
  280. #endif
  281. );
  282. printf (_(UT_HLP_VRS), progname, progname);
  283. }