Răsfoiți Sursa

Add support for LDAP URIs.

Chris Wilson 11 ani în urmă
părinte
comite
a891a812f2
2 a modificat fișierele cu 31 adăugiri și 9 ștergeri
  1. 1 1
      configure.ac
  2. 30 8
      plugins/check_ldap.c

+ 1 - 1
configure.ac

@@ -316,7 +316,7 @@ AS_IF([test "x$with_ldap" != "xno"], [
     AC_SUBST(LDAPINCLUDE)
     AC_CHECK_FUNCS(ldap_set_option)
     EXTRAS="$EXTRAS check_ldap\$(EXEEXT)"
-  	AC_CHECK_FUNCS(ldap_init ldap_set_option ldap_get_option ldap_start_tls_s)
+  	AC_CHECK_FUNCS(ldap_initialize ldap_init ldap_set_option ldap_get_option ldap_start_tls_s)
   else
     AC_MSG_WARN([Skipping LDAP plugin])
     AC_MSG_WARN([install LDAP libs to compile this plugin (see REQUIREMENTS).])

+ 30 - 8
plugins/check_ldap.c

@@ -54,6 +54,7 @@ void print_usage (void);
 
 char ld_defattr[] = "(objectclass=*)";
 char *ld_attr = ld_defattr;
+char *ld_uri = NULL;
 char *ld_host = NULL;
 char *ld_base = NULL;
 char *ld_passwd = NULL;
@@ -121,13 +122,29 @@ main (int argc, char *argv[])
 	gettimeofday (&tv, NULL);
 
 	/* initialize ldap */
+	if (ld_uri != NULL)
+	{
+#ifdef HAVE_LDAP_INITIALIZE
+		int result = ldap_initialize(&ld, ld_uri);
+		if (result != LDAP_SUCCESS)
+		{
+			printf ("Failed to connect to LDAP server at %s: %s\n",
+				ld_uri, ldap_err2string(result));
+			return STATE_CRITICAL;
+		}
+#else
+		printf ("Sorry, this version of %s was compiled without URI support!\n",
+			argv[0]);
+		return STATE_CRITICAL;
+#endif
+	}
 #ifdef HAVE_LDAP_INIT
-	if (!(ld = ldap_init (ld_host, ld_port))) {
+	else 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))) {
+	else if (!(ld = ldap_open (ld_host, ld_port))) {
 		if (verbose)
 			ldap_perror(ld, "ldap_open");
 		printf (_("Could not connect to the server at port %i\n"), ld_port);
@@ -248,6 +265,7 @@ process_arguments (int argc, char **argv)
 		{"version", no_argument, 0, 'V'},
 		{"timeout", required_argument, 0, 't'},
 		{"hostname", required_argument, 0, 'H'},
+		{"uri", required_argument, 0, 'U'},
 		{"base", required_argument, 0, 'b'},
 		{"attr", required_argument, 0, 'a'},
 		{"bind", required_argument, 0, 'D'},
@@ -276,7 +294,7 @@ process_arguments (int argc, char **argv)
 	}
 
 	while (1) {
-		c = getopt_long (argc, argv, "hvV234TS6t:c:w:H:b:p:a:D:P:", longopts, &option);
+		c = getopt_long (argc, argv, "hvV234TS6t:c:w:H:b:p:a:D:P:U:", longopts, &option);
 
 		if (c == -1 || c == EOF)
 			break;
@@ -294,6 +312,9 @@ process_arguments (int argc, char **argv)
 			else
 				socket_timeout = atoi (optarg);
 			break;
+		case 'U':
+			ld_uri = optarg;
+			break;
 		case 'H':
 			ld_host = optarg;
 			break;
@@ -375,11 +396,12 @@ process_arguments (int argc, char **argv)
 int
 validate_arguments ()
 {
-	if (ld_host==NULL || strlen(ld_host)==0)
-		usage4 (_("Please specify the host name\n"));
+	if ((ld_host==NULL || strlen(ld_host)==0) &&
+		(ld_uri==NULL || strlen(ld_uri)==0))
+		usage4 (_("Please specify the host name or LDAP URI\n"));
 
 	if (ld_base==NULL)
-		usage4 (_("Please specify the LDAP base\n"));
+		usage4 (_("Please specify the LDAP base DN\n"));
 
 	return OK;
 }
@@ -449,8 +471,8 @@ void
 print_usage (void)
 {
   printf ("%s\n", _("Usage:"));
-	printf (" %s -H <host> -b <base_dn> [-p <port>] [-a <attr>] [-D <binddn>]",progname);
-  printf ("\n       [-P <password>] [-w <warn_time>] [-c <crit_time>] [-t timeout]%s\n",
+  printf (" %s (-H <host>|-U <uri>) -b <base_dn> [-p <port>] [-a <attr>] [-D <binddn>]\n", progname);
+  printf ("       [-P <password>] [-w <warn_time>] [-c <crit_time>] [-t timeout]%s\n",
 #ifdef HAVE_LDAP_SET_OPTION
 			"\n       [-2|-3] [-4|-6]"
 #else