check_ldap.c 7.4 KB

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