steffen: server/perl-kolab/Kolab-LDAP LDAP.pm.in,1.5,1.6

cvs at kolab.org cvs at kolab.org
Sun Jul 30 23:20:45 CEST 2006


Author: steffen

Update of /kolabrepository/server/perl-kolab/Kolab-LDAP
In directory doto:/tmp/cvs-serv17829/Kolab-LDAP

Modified Files:
	LDAP.pm.in 
Log Message:
Use LDAP timestamps (Issue1194)

Index: LDAP.pm.in
===================================================================
RCS file: /kolabrepository/server/perl-kolab/Kolab-LDAP/LDAP.pm.in,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -d -r1.5 -r1.6
--- LDAP.pm.in	10 Jun 2006 18:46:58 -0000	1.5
+++ LDAP.pm.in	30 Jul 2006 21:20:43 -0000	1.6
@@ -25,6 +25,7 @@
 use strict;
 use warnings;
 use UNIVERSAL;
+use Time::Local;
 use Net::LDAP qw( LDAP_SUCCESS LDAP_PROTOCOL_ERROR LDAP_REFERRAL );
 use Net::LDAPS;
 use Net::LDAP::Util;
@@ -62,6 +63,11 @@
 
 our $VERSION = '0.9';
 
+# Timestamp to keep track of changed objects
+our $user_timestamp = "";
+our $sf_timestamp = "";
+our $group_timestamp = "";
+
 sub startup
 {
     Kolab::log('L', 'Starting up');
@@ -522,9 +528,9 @@
     }
     %newuid_db = ();
 
-    syncBasic($cyrus, 'user', '', 0);
-    syncBasic($cyrus, 'sf', '', 1);
-    syncBasic($cyrus, 'group', '', 0);
+    $user_timestamp  = syncBasic($cyrus, 'user', '', $user_timestamp, 0);
+    $sf_timestamp    = syncBasic($cyrus, 'sf', '', $sf_timestamp, 1);
+    $group_timestamp = syncBasic($cyrus, 'group', '', $group_timestamp, 0);
 
     # Check that all mailboxes correspond to LDAP objects
     Kolab::log('L', 'Synchronising mailboxes');
@@ -549,7 +555,7 @@
     # deletion, i.e. either we missed the deletion notification or there was
     # an error when iterating through the objects (Lost connection, invalid DNs)
     foreach $guid (keys %uid_db) {
-        if (exists $objects{$uid_db{$guid}}) {
+        if (defined $uid_db{$guid} && exists $objects{$uid_db{$guid}}) {
             $gyard_db{$guid} = $uid_db{$guid};
             $gyard_ts_db{$guid} = time;
         }
@@ -572,11 +578,38 @@
     Kolab::log('L', 'Finished synchronisation');
 }
 
+# Date::Parse doesn't understand this format
+# so we have to hack it ourselves
+sub parse_generalized_time
+{
+  my $ts = shift;
+  # YYYYMMDDHHMMSSZ
+  if( $ts =~ /(\d\d\d\d)(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)Z/ ) {
+    my $t = 0;
+    eval { $t = timegm($6,$5,$4,$3,$2-1,$1); };
+    return $t;
+  } else {
+    return 0;
+  }
+}
+
+# Returns the largest of two string-rep.
+# of LDAP generalizedTime
+sub max_generalized_time
+{
+  my $ts1 = shift;
+  my $ts2 = shift;
+  if( parse_generalized_time($ts1) >
+      parse_generalized_time($ts2) ) { return $ts1; }
+  else { return $ts2; }
+}
+
 sub syncBasic
 {
     my $cyrus = shift;
     my $p = shift || 'user';
     my $add = shift || ($p eq 'user' ? '' : '');
+    my $ts = shift || "";
     my $doacls = shift || 0;
 
     Kolab::log('L', "Synchronising `$p' objects");
@@ -603,7 +636,7 @@
             scope   => 'sub',
             filter  => '(&(objectClass=' . $Kolab::config{$p . '_object_class'} . ")$add(" . $Kolab::config{$p . '_field_deleted'} . '='.$Kolab::config{'fqdnhostname'}.'))',
             attrs   => [
-                '*',
+                'objectClass',
                 $Kolab::config{$p . '_field_guid'},
                 $Kolab::config{$p . '_field_modified'},
                 $Kolab::config{$p . '_field_deleted'},
@@ -620,14 +653,22 @@
 
         # Now check that all objects in LDAP have corresponding mailboxes
         # This also resurrects any missing users, if neccessary
+	my $filter;
+	if( $ts eq "" ) {
+	  $filter = '(&(objectClass=' . $Kolab::config{$p . '_object_class'} . ")$add)",
+	} else {
+	  $filter = '(&(objectClass=' . $Kolab::config{$p . '_object_class'} . ")("
+	    .$Kolab::config{$p.'_field_modified'}.">=$ts)$add)";
+	}
+	Kolab::log('L', "filter is $filter", KOLAB_DEBUG);
         $ldapmesg = $ldap->search(
             base    => $dn,
             scope   => 'sub',
-            filter  => '(&(objectClass=' . $Kolab::config{$p . '_object_class'} . ")$add)",
+            filter  => $filter,
             attrs   => [
                 '*',
                 $Kolab::config{$p . '_field_guid'},
-                $Kolab::config{$p . '_field_modified'},
+		$Kolab::config{$p . '_field_modified'},
                 $Kolab::config{$p . '_field_quota'},
                 $Kolab::config{$p . '_field_deleted'},
             ],
@@ -636,6 +677,7 @@
         if ( UNIVERSAL::isa( $ldapmesg, 'Net::LDAP::Search') && $ldapmesg->code() <= 0) {
 	    while( $ldapobject = $ldapmesg->pop_entry ) {
                 createObject($ldap, $cyrus, $ldapobject, 1, $p, $doacls);
+		$ts = max_generalized_time($ts,$ldapobject->get_value($Kolab::config{$p . '_field_modified'}));
             }
         } else {
             Kolab::log('L', "Unable to locate `$p' objects in DN `$dn'", KOLAB_WARN);
@@ -647,6 +689,7 @@
     &destroy($ldap);
 
     Kolab::log('L', "Finished `$p' object synchronisation");
+    return $ts;
 }
 
 1;





More information about the commits mailing list