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