Handle missing hosts() function
authorJamie Cameron <jcameron@webmin.com>
Sun, 8 Feb 2009 20:53:58 +0000 (20:53 +0000)
committerJamie Cameron <jcameron@webmin.com>
Sun, 8 Feb 2009 20:53:58 +0000 (20:53 +0000)
ldap-client/CHANGELOG
ldap-client/check.cgi
ldap-client/ldap-client-lib.pl

index 8884dc2..d3ada7d 100644 (file)
@@ -9,3 +9,5 @@ On Debian/Ubuntu systems with separate PAM and NSS LDAP config files, offer to l
 Handle new LDAP config config file path seen on Ubuntu 8.04.
 ---- Changes since 1.440 ----
 Changed the LDAP client connection code to handle both pure-SSL and TLS modes, thanks to a patch from Paul R. Ganci.
+---- Changes since 1.450 ----
+Improved support for older versions of Net::LDAP without the hosts() function.
index f34e830..e18544a 100755 (executable)
@@ -26,6 +26,9 @@ if (!ref($ldap)) {
        goto END;
        }
 else {
+       local $ldaphost;
+       eval { $ldaphost = $ldap->host(); };
+       $ldaphost ||= &get_ldap_host();
        print &text('check_connected', $ldap->host()),"<p>\n";
        }
 
index 59b33c7..825e739 100644 (file)
@@ -199,7 +199,7 @@ if ($ldap_hosts) {
        local $port = $ldap_port ||
                      &find_svalue("port", $conf) ||
                      ($use_ssl == 1 ? 636 : 389);
-       foreach $host (@hosts) {
+       foreach my $host (@hosts) {
                $ldap = Net::LDAP->new($host, port => $port,
                                scheme => $use_ssl == 1 ? 'ldaps' : 'ldap');
                if (!$ldap) {
@@ -214,7 +214,7 @@ if ($ldap_hosts) {
        }
 elsif ($uri) {
        # Using uri directive
-       foreach $u (split(/\s+/, $uri)) {
+       foreach my $u (split(/\s+/, $uri)) {
                if ($u =~ /^(ldap|ldaps|ldapi):\/\/([a-z0-9\_\-\.]+)(:(\d+))?/) {
                        ($proto, $host, $port) = ($1, $2, $4);
                        if (!$port && $proto eq "ldap") {
@@ -326,5 +326,36 @@ if ($gconfig{'db_sizeusers'}) {
 return "<input type=button onClick='ifield = document.forms[$form].$field; chooser = window.open(\"popup_browser.cgi?node=$node&base=\"+escape(ifield.value), \"chooser\", \"toolbar=no,menubar=no,scrollbars=yes,width=$w,height=$h\"); chooser.ifield = ifield; window.ifield = ifield' value=\"...\">\n";
 }
 
+# get_ldap_host()
+# Returns the hostname probably used for connecting
+sub get_ldap_host
+{
+local @hosts;
+if ($config{'ldap_hosts'}) {
+       @hosts = split(/\s+/, $config{'ldap_hosts'});
+       }
+elsif (!-r $config{'auth_ldap'}) {
+       @hosts = ( );
+       }
+else {
+       local $conf = &get_config();
+       local $uri = &find_svalue("uri", $conf);
+       if ($uri) {
+               foreach my $u (split(/\s+/, $uri)) {
+                       if ($u =~ /^(ldap|ldaps|ldapi):\/\/([a-z0-9\_\-\.]+)(:(\d+))?/) {
+                               push(@hosts, $2);
+                               }
+                       }
+               }
+       else {
+               @hosts = split(/[ ,]+/, &find_svalue("host", $conf));
+               }
+       if (!@hosts) {
+               @hosts = ( "localhost" );
+               }
+       }
+return wantarray ? @hosts : $hosts[0];
+}
+
 1;