check_ldap.c 7.3 KB

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