X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;f=plugins%2Fcheck_ldap.c;h=4f14328074cc92b964ab51633fdcec8db46ed9f4;hb=d4c0948266f261525e12c58d58e0fc68987a9818;hp=dfdd837476f6d27eb4868a342081fddd09e65a51;hpb=5e3fc41c6143676cf01b7aad789fc5f2cae5ce84;p=nagiosplug.git diff --git a/plugins/check_ldap.c b/plugins/check_ldap.c index dfdd837..4f14328 100644 --- a/plugins/check_ldap.c +++ b/plugins/check_ldap.c @@ -18,7 +18,8 @@ ******************************************************************************/ -const char *progname = "check_ldap"; +/* progname may be check_ldaps */ +char *progname = "check_ldap"; const char *revision = "$Revision$"; const char *copyright = "2000-2004"; const char *email = "nagiosplug-devel@lists.sourceforge.net"; @@ -57,6 +58,10 @@ double warn_time = UNDEFINED; double crit_time = UNDEFINED; struct timeval tv; +/* for ldap tls */ + +char *SERVICE = "LDAP"; + int main (int argc, char *argv[]) { @@ -69,12 +74,21 @@ main (int argc, char *argv[]) int status = STATE_UNKNOWN; long microsec; double elapsed_time; + + /* for ldap tls */ + + int tls; + int version=3; setlocale (LC_ALL, ""); bindtextdomain (PACKAGE, LOCALEDIR); textdomain (PACKAGE); - if (process_arguments (argc, argv) != TRUE) + if (strstr(argv[0],"check_ldaps")) { + asprintf (&progname, "check_ldaps"); + } + + if (process_arguments (argc, argv) == ERROR) usage4 (_("Could not parse arguments")); /* initialize alarm signal handling */ @@ -87,12 +101,19 @@ main (int argc, char *argv[]) gettimeofday (&tv, NULL); /* initialize ldap */ +#ifdef HAVE_LDAP_INIT + if (!(ld = ldap_init (ld_host, ld_port))) { + printf ("Could not connect to the server at port %i\n", ld_port); + return STATE_CRITICAL; + } +#else if (!(ld = ldap_open (ld_host, ld_port))) { /*ldap_perror(ld, "ldap_open"); */ printf (_("Could not connect to the server at port %i\n"), ld_port); return STATE_CRITICAL; } - +#endif /* HAVE_LDAP_INIT */ + #ifdef HAVE_LDAP_SET_OPTION /* set ldap options */ if (ldap_set_option (ld, LDAP_OPT_PROTOCOL_VERSION, &ld_protocol) != @@ -101,6 +122,51 @@ main (int argc, char *argv[]) return STATE_CRITICAL; } #endif + + if (strstr(argv[0],"check_ldaps")) { + /* with TLS */ + if ( ld_port == LDAPS_PORT ) { + asprintf (&SERVICE, "LDAPS"); +#if defined(HAVE_LDAP_SET_OPTION) && defined(LDAP_OPT_X_TLS) + /* ldaps: set option tls */ + tls = LDAP_OPT_X_TLS_HARD; + + if (ldap_set_option (ld, LDAP_OPT_X_TLS, &tls) != LDAP_SUCCESS) + { + /*ldap_perror(ld, "ldaps_option"); */ + printf (_("Could not init TLS at port %i!\n"), ld_port); + return STATE_CRITICAL; + } +#else + printf (_("TLS not supported by the libraries!\n"), ld_port); + return STATE_CRITICAL; +#endif /* LDAP_OPT_X_TLS */ + } else { + asprintf (&SERVICE, "LDAP-TLS"); +#if defined(HAVE_LDAP_SET_OPTION) && defined(HAVE_LDAP_START_TLS_S) + /* ldap with startTLS: set option version */ + if (ldap_get_option(ld,LDAP_OPT_PROTOCOL_VERSION, &version) == LDAP_OPT_SUCCESS ) + { + if (version < LDAP_VERSION3) + { + version = LDAP_VERSION3; + ldap_set_option(ld, LDAP_OPT_PROTOCOL_VERSION, &version); + } + } + /* call start_tls */ + if (ldap_start_tls_s(ld, NULL, NULL) != LDAP_SUCCESS) + { + /*ldap_perror(ld, "ldap_start_tls"); */ + printf (_("Could not init startTLS at port %i!\n"), ld_port); + return STATE_CRITICAL; + } +#else + printf (_("startTLS not supported by the library, needs LDAPv3!\n")); + return STATE_CRITICAL; +#endif /* HAVE_LDAP_START_TLS_S */ + } + } + /* bind to the ldap server */ if (ldap_bind_s (ld, ld_binddn, ld_passwd, LDAP_AUTH_SIMPLE) != LDAP_SUCCESS) { @@ -246,9 +312,7 @@ process_arguments (int argc, char **argv) #endif break; default: - printf (_("%s: Unknown argument: %s\n\n"), progname, optarg); - print_usage (); - exit (STATE_UNKNOWN); + usage2 (_("Unknown argument"), optarg); } } @@ -262,21 +326,20 @@ process_arguments (int argc, char **argv) return validate_arguments (); } + int validate_arguments () { if (ld_host==NULL || strlen(ld_host)==0) - usage (_("please specify the host name\n")); + usage4 (_("Please specify the host name\n")); if (ld_base==NULL || strlen(ld_base)==0) - usage (_("please specify the LDAP base\n")); + usage4 (_("Please specify the LDAP base\n")); return OK; - } - void print_help (void) { @@ -326,7 +389,6 @@ print_help (void) } - void print_usage (void) {