check_ldap.c 7.3 KB

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