File Coverage

blib/lib/Kolab/Cyrus.pm
Criterion Covered Total %
statement 10 12 83.3
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 14 16 87.5


line stmt bran cond sub pod time code
1             package Kolab::Cyrus;
2              
3             ##
4             ## Copyright (c) 2003 Code Fusion cc
5             ##
6             ## Writen by Stuart Bingė
7             ##
8             ## This program is free software; you can redistribute it and/or
9             ## modify it under the terms of the GNU General Public License as
10             ## published by the Free Software Foundation; either version 2, or
11             ## (at your option) any later version.
12             ##
13             ## This program is distributed in the hope that it will be useful,
14             ## but WITHOUT ANY WARRANTY; without even the implied warranty of
15             ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16             ## General Public License for more details.
17             ##
18             ## You can view the GNU General Public License, online, at the GNU
19             ## Project's homepage; see .
20             ##
21              
22 1     1   36244 use 5.008;
  1         3  
  1         30  
23 1     1   5 use strict;
  1         2  
  1         32  
24 1     1   6 use warnings;
  1         6  
  1         38  
25 1     1   617 use Cyrus::IMAP::Admin;
  0            
  0            
26             use Kolab::Util;
27             use Kolab;
28              
29             require Exporter;
30              
31             our @ISA = qw(Exporter);
32              
33             our %EXPORT_TAGS = (
34             'all' => [ qw(
35             &create
36             &createUid
37             &createMailbox
38             &deleteMailbox
39             &setQuota
40             &setACL
41             ) ]
42             );
43              
44             our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
45              
46             our @EXPORT = qw(
47              
48             );
49              
50             our $VERSION = sprintf('%d.%02d', q$Revision: 1.1.1.1 $ =~ /(\d+)\.(\d+)/);
51              
52             sub create
53             {
54             Kolab::log('Y', 'Connecting to local Cyrus admin interface');
55              
56             my $cyrus = Cyrus::IMAP::Admin->new('localhost');
57              
58             if (!$cyrus) {
59             Kolab::log('Y', 'Unable to connect to local Cyrus admin interface', KOLAB_ERROR);
60             exit(1);
61             }
62              
63             if (!$cyrus->authenticate(
64             'User' => $Kolab::config{'cyrus_admin'},
65             'Password' => $Kolab::config{'cyrus_admin_pw'},
66             'mechanisms' => 'plaintext',
67             )) {
68             Kolab::log('Y', "Unable to authenticate with Cyrus admin interface, Error = `" . $cyrus->error . "'", KOLAB_ERROR);
69             exit(1);
70             }
71              
72             return $cyrus;
73             }
74              
75             sub createUid
76             {
77             my $user = shift;
78             my $sf = shift || 0;
79             return 'user' . ($sf ? '.' : '/') . $user;
80             }
81              
82             sub createMailbox
83             {
84             my $cyrus = shift;
85             my $uid = shift;
86             my $sf = shift || 0;
87             my $cyruid = &createUid($uid, $sf);
88              
89             my $mailbox = ($cyrus->list($cyruid))[0];
90             if ($uid && ($uid ne $Kolab::config{'cyrus_admin'}) && ($uid ne "freebusy") && ($uid ne "nobody") && !defined($mailbox)) {
91             Kolab::log('Y', "Creating mailbox `$cyruid'");
92             if (!$cyrus->create($cyruid)) {
93             Kolab::log('Y', "Unable to create mailbox `$cyruid', Error = `" . $cyrus->error . "'", KOLAB_WARN);
94             }
95             }
96             }
97              
98             sub setQuota
99             {
100             my $cyrus = shift;
101             my $uid = shift;
102             my $quota = shift || 0;
103             my $sf = shift || 0;
104             my $cyruid = &createUid($uid, $sf);
105              
106             if ($quota > 0) {
107             (my $root, my %quota) = $cyrus->quotaroot($cyruid);
108             my $setquota = $quota{'STORAGE'}[1];
109             if (!defined($setquota) || ($setquota != $quota)) {
110             Kolab::log('Y', "Setting quota of mailbox `$cyruid' to $quota");
111             if (!$cyrus->setquota($cyruid, 'STORAGE', $quota)) {
112             Kolab::log('Y', "Unable to set quota for mailbox `$cyruid', Error = `" . $cyrus->error . "'", KOLAB_WARN);
113             }
114             }
115             }
116             }
117              
118             sub deleteMailbox
119             {
120             my $cyrus = shift;
121             my $uid = shift;
122             my $sf = shift || 0;
123             my $cyruid = &createUid($uid, $sf);
124              
125             Kolab::log('Y', "Removing mailbox `$cyruid'");
126             if (!$cyrus->setacl($cyruid, $Kolab::config{'cyrus_admin'}, 'c')) {
127             Kolab::log('Y', "Unable to reset ACL of mailbox `$cyruid', Error = `" . $cyrus->error . "'", KOLAB_WARN);
128             }
129             if (!$cyrus->delete($cyruid)) {
130             Kolab::log('Y', "Unable to remove mailbox `$cyruid', Error = `" . $cyrus->error . "'", KOLAB_WARN);
131             }
132             }
133              
134             sub setACL
135             {
136             my $cyrus = shift;
137             my $uid = shift;
138             my $sf = shift || 0;
139             my $cyruid = &createUid($uid, $sf);
140              
141             Kolab::log('Y', "Setting up ACL of mailbox `$cyruid'");
142             my $prefix = $Kolab::config{'prefix'};
143             my @acls = `$prefix/etc/kolab/workaround.sh $cyruid $Kolab::config{'bind_pw'} | sed -e /localhost/d`;
144             my ($user, $entry, $acl);
145             Kolab::log('Y', "Removing users from ACL of $cyruid", KOLAB_DEBUG);
146             foreach $acl (@acls) {
147             $acl = trim($acl);
148             ($user, ) = split(/ /, $acl);
149             Kolab::log('Y', "Removing `$user' from the ACL of mailbox `$cyruid'");
150             if (!$cyrus->deleteacl($cyruid, $user)) {
151             Kolab::log('Y', "Unable to remove `$user' from the ACL of mailbox `$cyruid', Error = `" . $cyrus->error . "'", KOLAB_WARN);
152             }
153             }
154              
155             Kolab::log('Y', "Add users from ACL of $cyruid", KOLAB_DEBUG);
156             my $newacl = shift;
157             foreach $entry (@$newacl) {
158             Kolab::log('Y', "Setting up ACL `$entry'", KOLAB_DEBUG);
159             ($user, $acl) = split(/ /, $entry , 2);
160             Kolab::log('Y', "Split `$user' and `$acl'", KOLAB_DEBUG);
161             $user = trim($user);
162             $acl = trim($acl);
163             Kolab::log('Y', "Setting the ACL of user `$user' in mailbox `$cyruid' to $acl");
164             if (!$cyrus->setacl($cyruid, $user, $acl)) {
165             Kolab::log('Y', "Unable to set the ACL of user `$user' in mailbox `$cyruid' to $acl, Error = `" . $cyrus->error . "'", KOLAB_WARN);
166             }
167             }
168             Kolab::log('Y', "Finished modifying ACL of $cyruid", KOLAB_DEBUG);
169             }
170              
171             1;
172             __END__