Index: src/perl/ldapweb.cgi
===================================================================
--- src/perl/ldapweb.cgi	(revision 4848)
+++ src/perl/ldapweb.cgi	(revision 4849)
@@ -516,27 +516,29 @@
     #if main ldap server is down, a html file containing warning message will be returned
     $ldap = Net::LDAP->new($ldapurl, timeout => $timeout) or handleLDAPBindFailure($ldapurl);
     
-    #$ldap->start_tls( verify => 'require',
+    if ($ldap) {
+    	#$ldap->start_tls( verify => 'require',
                       #cafile => '/usr/share/ssl/ldapcerts/cacert.pem');
-    $ldap->start_tls( verify => 'none');
-    my $bindresult = $ldap->bind( version => 3, dn => $bindDN, 
+    	$ldap->start_tls( verify => 'none');
+    	my $bindresult = $ldap->bind( version => 3, dn => $bindDN, 
                                   password => $bindPass );
-    if ($bindresult->code) {
-        $errorMessage = "Failed to log in. Are you sure your connection credentails are " .
+    	if ($bindresult->code) {
+        	$errorMessage = "Failed to log in. Are you sure your connection credentails are " .
                         "correct? Please correct and try again...";
-        return $errorMessage;
-    }
+       	 	return $errorMessage;
+    	}
 
-    # Find the user here and change their entry
-    my $newpass = createSeededPassHash($userPass);
-    my $modifications = { userPassword => $newpass };
-    my $result = $ldap->modify( $userDN, replace => { %$modifications });
+    	# Find the user here and change their entry
+    	my $newpass = createSeededPassHash($userPass);
+    	my $modifications = { userPassword => $newpass };
+    	my $result = $ldap->modify( $userDN, replace => { %$modifications });
     
-    if ($result->code()) {
-        my $errorMessage = "There was an error changing the password." .
+    	if ($result->code()) {
+        	my $errorMessage = "There was an error changing the password." .
                            "<br />\n" . $result->error;
-    } 
-    $ldap->unbind;   # take down session
+    	} 
+    	$ldap->unbind;   # take down session
+    }
 
     return $errorMessage;
 }
@@ -576,36 +578,39 @@
 
     #if main ldap server is down, a html file containing warning message will be returned
     $ldap = Net::LDAP->new($ldapurl, timeout => $timeout) or handleLDAPBindFailure($ldapurl);
-    $ldap->start_tls( verify => 'none');
-    my $bindresult = $ldap->bind;
-    if ($bindresult->code) {
-        return $entry;
-    }
+    
+    if ($ldap) {
+    	$ldap->start_tls( verify => 'none');
+    	my $bindresult = $ldap->bind;
+    	if ($bindresult->code) {
+        	return $entry;
+    	}
 
-    if($ldapConfig->{$org}{'filter'}){
-        $mesg = $ldap->search ( base   => $base,
+    	if($ldapConfig->{$org}{'filter'}){
+        	$mesg = $ldap->search ( base   => $base,
                 filter => "(&(uid=$username)($ldapConfig->{$org}{'filter'}))");
-    } else {
-        $mesg = $ldap->search ( base   => $base, filter => "(uid=$username)");
-    }
+    	} else {
+        	$mesg = $ldap->search ( base   => $base, filter => "(uid=$username)");
+    	}
     
-    if ($mesg->count > 0) {
-        $entry = $mesg->pop_entry;
-        $ldap->unbind;   # take down session
-    } else {
-        $ldap->unbind;   # take down session
-        # Follow references by recursive call to self
-        my @references = $mesg->references();
-        for (my $i = 0; $i <= $#references; $i++) {
-            my $uri = URI->new($references[$i]);
-            my $host = $uri->host();
-            my $path = $uri->path();
-            $path =~ s/^\///;
-            $entry = &getLdapEntry($host, $path, $username, $org);
-            if ($entry) {
-                return $entry;
-            }
-        }
+    	if ($mesg->count > 0) {
+        	$entry = $mesg->pop_entry;
+        	$ldap->unbind;   # take down session
+    	} else {
+        	$ldap->unbind;   # take down session
+        	# Follow references by recursive call to self
+        	my @references = $mesg->references();
+        	for (my $i = 0; $i <= $#references; $i++) {
+            	my $uri = URI->new($references[$i]);
+            	my $host = $uri->host();
+            	my $path = $uri->path();
+            	$path =~ s/^\///;
+            	$entry = &getLdapEntry($host, $path, $username, $org);
+            	if ($entry) {
+                	return $entry;
+            	}
+        	}
+    	}
     }
     return $entry;
 }
@@ -773,19 +778,19 @@
         #if main ldap server is down, a html file containing warning message will be returned
         my $ldap = Net::LDAP->new($ldapurl, timeout => $timeout) or handleLDAPBindFailure($ldapurl);
         
+        if ($ldap) {
+        	$ldap->start_tls( verify => 'none');
+        	debug("Attempting to bind to LDAP server with dn = $ldapUsername, pwd = $ldapPassword");
+        	$ldap->bind( version => 3, dn => $ldapUsername, password => $ldapPassword );
         
-        $ldap->start_tls( verify => 'none');
-        debug("Attempting to bind to LDAP server with dn = $ldapUsername, pwd = $ldapPassword");
-        $ldap->bind( version => 3, dn => $ldapUsername, password => $ldapPassword );
-        
-        my $dn = 'uid=' . $query->param('uid') . ',' . $dnBase;
-        debug("Inserting new entry for: $dn");
+        	my $dn = 'uid=' . $query->param('uid') . ',' . $dnBase;
+        	debug("Inserting new entry for: $dn");
 
-        # Create a hashed version of the password
-        my $shapass = createSeededPassHash($query->param('userPassword'));
+        	# Create a hashed version of the password
+        	my $shapass = createSeededPassHash($query->param('userPassword'));
 
-        # Do the insertion
-        my $additions = [ 
+        	# Do the insertion
+        	my $additions = [ 
                 'uid'   => $query->param('uid'),
                 'o'   => $query->param('o'),
                 'cn'   => join(" ", $query->param('givenName'), 
@@ -796,34 +801,35 @@
                 'userPassword' => $shapass,
                 'objectclass' => ['top', 'person', 'organizationalPerson', 
                                 'inetOrgPerson', 'uidObject' ]
-            ];
-        if (defined($query->param('telephoneNumber')) && 
-            $query->param('telephoneNumber') &&
-            ! $query->param('telephoneNumber') =~ /^\s+$/) {
-            $$additions[$#$additions + 1] = 'telephoneNumber';
-            $$additions[$#$additions + 1] = $query->param('telephoneNumber');
-        }
-        if (defined($query->param('title')) && 
-            $query->param('title') &&
-            ! $query->param('title') =~ /^\s+$/) {
-            $$additions[$#$additions + 1] = 'title';
-            $$additions[$#$additions + 1] = $query->param('title');
-        }
-        my $result = $ldap->add ( 'dn' => $dn, 'attr' => [ @$additions ]);
+            	];
+        	if (defined($query->param('telephoneNumber')) && 
+            	$query->param('telephoneNumber') &&
+            	! $query->param('telephoneNumber') =~ /^\s+$/) {
+            	$$additions[$#$additions + 1] = 'telephoneNumber';
+            	$$additions[$#$additions + 1] = $query->param('telephoneNumber');
+        	}
+        	if (defined($query->param('title')) && 
+            	$query->param('title') &&
+            	! $query->param('title') =~ /^\s+$/) {
+            	$$additions[$#$additions + 1] = 'title';
+            	$$additions[$#$additions + 1] = $query->param('title');
+        	}
+        	my $result = $ldap->add ( 'dn' => $dn, 'attr' => [ @$additions ]);
     
-        if ($result->code()) {
-            fullTemplate( ['registerFailed', 'register'], { stage => "register",
+        	if ($result->code()) {
+            	fullTemplate( ['registerFailed', 'register'], { stage => "register",
                                                             allParams => $allParams,
                                                             errorMessage => $result->error });
-            # TODO SCW was included as separate errors, test this
-            #$templateVars    = setVars({ stage => "register",
-            #                     allParams => $allParams });
-            #$template->process( $templates->{'register'}, $templateVars);
-        } else {
-            fullTemplate( ['success'] );
+            	# TODO SCW was included as separate errors, test this
+           	 	#$templateVars    = setVars({ stage => "register",
+           	 	#                     allParams => $allParams });
+            	#$template->process( $templates->{'register'}, $templateVars);
+        	} else {
+            	fullTemplate( ['success'] );
+        	}
+
+        	$ldap->unbind;   # take down session
         }
-
-        $ldap->unbind;   # take down session
     }
 }
 
@@ -912,55 +918,58 @@
     my $filter = shift;
     my $attref = shift;
 
+	my $mesg;
     my $foundAccounts = 0;
     
     #if ldap server is down, a html file containing warning message will be returned
     my $ldap = Net::LDAP->new($ldapurl, timeout => $timeout) or handleLDAPBindFailure($ldapurl);
     
-    $ldap->start_tls( verify => 'none');
-    $ldap->bind( version => 3, anonymous => 1);
-    my $mesg = $ldap->search (
-        base   => $base,
-        filter => $filter,
-        attrs => @$attref,
-    );
+    if ($ldap) {
+    	$ldap->start_tls( verify => 'none');
+    	$ldap->bind( version => 3, anonymous => 1);
+    	my $mesg = $ldap->search (
+        	base   => $base,
+        	filter => $filter,
+        	attrs => @$attref,
+    	);
 
-    if ($mesg->count() > 0) {
-        $foundAccounts = "";
-        my $entry;
-        foreach $entry ($mesg->sorted(['sn'])) {
-          $foundAccounts .= "<tr>\n<td class=\"main\">\n";
-          $foundAccounts .= "<a href=\"" unless 
+    	if ($mesg->count() > 0) {
+        	$foundAccounts = "";
+        	my $entry;
+        	foreach $entry ($mesg->sorted(['sn'])) {
+          		$foundAccounts .= "<tr>\n<td class=\"main\">\n";
+          		$foundAccounts .= "<a href=\"" unless 
                     (!$entry->get_value('labeledURI'));
-          $foundAccounts .= $entry->get_value('labeledURI') unless
+         		 $foundAccounts .= $entry->get_value('labeledURI') unless
                     (!$entry->get_value('labeledURI'));
-          $foundAccounts .= "\">\n" unless 
+          		$foundAccounts .= "\">\n" unless 
                     (!$entry->get_value('labeledURI'));
-          $foundAccounts .= $entry->get_value('givenName');
-          $foundAccounts .= "</a>\n" unless 
+          		$foundAccounts .= $entry->get_value('givenName');
+          		$foundAccounts .= "</a>\n" unless 
                     (!$entry->get_value('labeledURI'));
-          $foundAccounts .= "\n</td>\n<td class=\"main\">\n";
-          $foundAccounts .= "<a href=\"" unless 
+          		$foundAccounts .= "\n</td>\n<td class=\"main\">\n";
+          		$foundAccounts .= "<a href=\"" unless 
                     (!$entry->get_value('labeledURI'));
-          $foundAccounts .= $entry->get_value('labeledURI') unless
+          		$foundAccounts .= $entry->get_value('labeledURI') unless
                     (!$entry->get_value('labeledURI'));
-          $foundAccounts .= "\">\n" unless 
+          		$foundAccounts .= "\">\n" unless 
                     (!$entry->get_value('labeledURI'));
-          $foundAccounts .= $entry->get_value('sn');
-          $foundAccounts .= "</a>\n";
-          $foundAccounts .= "\n</td>\n<td class=\"main\">\n";
-          $foundAccounts .= $entry->get_value('mail');
-          $foundAccounts .= "\n</td>\n<td class=\"main\">\n";
-          $foundAccounts .= $entry->get_value('telephonenumber');
-          $foundAccounts .= "\n</td>\n<td class=\"main\">\n";
-          $foundAccounts .= $entry->get_value('title');
-          $foundAccounts .= "\n</td>\n<td class=\"main\">\n";
-          $foundAccounts .= $entry->get_value('ou');
-          $foundAccounts .= "\n</td>\n";
-          $foundAccounts .= "</tr>\n";
-        }
+          		$foundAccounts .= $entry->get_value('sn');
+          		$foundAccounts .= "</a>\n";
+          		$foundAccounts .= "\n</td>\n<td class=\"main\">\n";
+          		$foundAccounts .= $entry->get_value('mail');
+          		$foundAccounts .= "\n</td>\n<td class=\"main\">\n";
+          		$foundAccounts .= $entry->get_value('telephonenumber');
+          		$foundAccounts .= "\n</td>\n<td class=\"main\">\n";
+          		$foundAccounts .= $entry->get_value('title');
+          		$foundAccounts .= "\n</td>\n<td class=\"main\">\n";
+          		$foundAccounts .= $entry->get_value('ou');
+          		$foundAccounts .= "\n</td>\n";
+          		$foundAccounts .= "</tr>\n";
+        	}
+    	}
+    	$ldap->unbind;   # take down session
     }
-    $ldap->unbind;   # take down session
     return $foundAccounts;
 }
 
