d0d307
#!/usr/bin/perl
d0d307
use Cyrus::IMAP::Admin;
d0d307
d0d307
# This script was created by Kevin J. Menard, Jr. <kmenard@wpi.edu>.
d0d307
# It requires root privileges to write to a file in /etc.  Best use is
d0d307
# to set this up as cron job.  Works for me.  Hope it does for you.  
d0d307
# Any questions/complaints/praise/whatever, send 'em to the address
d0d307
# above.   -- 08/16/2001
d0d307
d0d307
d0d307
# These are the variables you might want to tweak.
d0d307
my $quota_attr = "mailQuota";
d0d307
my $mail_attr = "mailRoutingAddress";
d0d307
my $user = "cyrus";
d0d307
my $passwd = "blah";
d0d307
d0d307
# These are the ones that you shouldn't have to.
d0d307
my @entries = ();
d0d307
my $index = 0;
d0d307
my $counter = 0;
d0d307
my $old_timestamp = 0;
d0d307
my $timestamp = "199412161032Z";
d0d307
d0d307
# Open the /etc/cyrus_ldap_quota_time file; it's a long name, but
d0d307
# shouldn't interfere with existing files :)  This file contains 1 line,
d0d307
# the generalized time format of the last time the script ran.  This is
d0d307
# used for the search, so we only update quotas that have been modified
d0d307
# since then.
d0d307
d0d307
{
d0d307
    if (-e "/etc/cyrus_ldap_quota_time")
d0d307
    {
d0d307
         open(TIME_FILE, "/etc/cyrus_ldap_quota_time") or die "could not
d0d307
                              open the time file: $!\n";
d0d307
     
d0d307
         while(<TIME_FILE>) { $old_timestamp = $_; }
d0d307
d0d307
         close(TIME_FILE);
d0d307
    }  
d0d307
d0d307
    # Now we deal with the case where the file doesn't exist, that is to
d0d307
    # say the first time the script was run.
d0d307
d0d307
    unless ($old_timestamp == 0) { $timestamp = $old_timestamp; }
d0d307
d0d307
d0d307
    # Now that we have that information, we can overwrite the file with
d0d307
    # the new timestamp.  Maybe this overkill, but this is only a
d0d307
    # temporary solution anyway.
d0d307
d0d307
    open(TIME_FILE, ">/etc/cyrus_ldap_quota_time") or die "could not
d0d307
                              create file: $!\n";
d0d307
d0d307
    my @time = (localtime)[0..5];
d0d307
d0d307
    printf TIME_FILE $time[5] + 1900;
d0d307
    printf TIME_FILE "%02d", $time[4] + 1;
d0d307
    for (my $i = 3; $i >= 0; $i--) { printf TIME_FILE "%02d", $time[$i];}
d0d307
    print TIME_FILE 'Z';
d0d307
d0d307
    close(TIME_FILE);
d0d307
}
d0d307
d0d307
d0d307
# This is where we do the search and then parse the results into a
d0d307
# useable form.  In this case, an arry of hash entries.
d0d307
{
d0d307
    # Okay, this very ugly line sets up the LDAP search, and the strips
d0d307
    # away the meaningless stuff.  This could be prettier, but I didn't 
d0d307
    # want to add a dependency for an LDAP module, and everyone should
d0d307
    # have LDAP search.  The greps are just to make things simpler.
d0d307
d0d307
    (my $query = "ldapsearch -x '(&(modifyTimestamp>=$timestamp)($quota_attr=*))' $quota_attr $mail_attr 
d0d307
    | grep -v ^# | grep -v ^dn | grep -v ^version | grep -v ^search | grep -v ^result | grep -v ^\$") =~ s!\n!!;
d0d307
d0d307
    # Now actually do the commands in the above line.
d0d307
    my $result = `$query`;
d0d307
d0d307
d0d307
    # Split the output into an array, one entry per line.
d0d307
    my @output = split(/\n/, $result);
d0d307
d0d307
    # Now go through each line . . .
d0d307
    foreach (@output)
d0d307
    {
d0d307
         # Split on the attribute:value boundary.
d0d307
         (my $key, my $value) = split(/: /);
d0d307
d0d307
         # Handle mailRoutingAddress; strip away everything after '@'.
d0d307
         if ($value =~ m!@!)
d0d307
         {
d0d307
	     ($value, undef) = split (/@/, $value);
d0d307
         }
d0d307
d0d307
         # Add each doctored up attribute:value pair to the entries array.
d0d307
         $entries[$index]{$key} = $value;
d0d307
d0d307
         # A crude hack to make sure each of the two attributes makes it
d0d307
         # into one of the entries array element.
d0d307
         if ($counter % 2)
d0d307
         {
d0d307
  	     $index++;
d0d307
         }
d0d307
d0d307
         $counter++;
d0d307
    }
d0d307
}
d0d307
d0d307
# Now here's the actual interaction with Cyrus IMAPd.  It's all pretty
d0d307
# self-explanatory.
d0d307
{
d0d307
     my $imap = Cyrus::IMAP::Admin->new('localhost') or die "imap:
d0d307
                         cannot connect to server: $!\n";
d0d307
d0d307
     $imap->send(undef, undef, "LOGIN %s %s", $user, $passwd) or die
d0d307
                 "could not send user:pass to the server: $!\n";
d0d307
d0d307
     for (my $i = 0; $i <= $#entries; $i++)
d0d307
     {
d0d307
          $imap->setquota("user." . $entries[$i]{$mail_attr}, "STORAGE",
d0d307
                          $entries[$i]{$quota_attr}) 
d0d307
               or die "imap: could not set quota for
d0d307
                            user.$entries[$i]{$mail_attr}: $!\n";
d0d307
     }
d0d307
d0d307
     $imap=undef;
d0d307
}