check_ldap.c 7.5 KB

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