check_ldap.c 7.3 KB

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