4
0

check_ldap.c 7.4 KB

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