1 package FS::part_export::vpopmail;
3 use vars qw(@ISA %info @saltset $exportdir);
7 use FS::UID qw( datasrc );
10 @ISA = qw(FS::part_export);
12 tie my %options, 'Tie::IxHash',
13 #'machine' => { label=>'vpopmail machine', },
14 'dir' => { label=>'directory', }, # ?more info? default?
15 'uid' => { label=>'vpopmail uid' },
16 'gid' => { label=>'vpopmail gid' },
17 'restart' => { label=> 'vpopmail restart command',
18 default=> 'cd /home/vpopmail/domains; for domain in *; do /home/vpopmail/bin/vmkpasswd $domain; done; /var/qmail/bin/qmail-newu; killall -HUP qmail-send',
24 'desc' => 'Real-time export to vpopmail text files',
25 'options' => \%options,
27 This export is currently unmaintained. See shellcommands_withdomain for an
28 export that uses vpopmail CLI commands instead.<BR>
30 Real time export to <a href="http://inter7.com/vpopmail/">vpopmail</a> text
31 files. <a href="http://search.cpan.org/dist/File-Rsync">File::Rsync</a>
32 must be installed, and you will need to
33 <a href="http://www.freeside.biz/mediawiki/index.php/Freeside:1.9:Documentation:Administration:SSH_Keys">setup SSH for unattended operation</a>
34 to <b>vpopmail</b>@<i>export.host</i>.
38 @saltset = ( 'a'..'z' , 'A'..'Z' , '0'..'9' , '.' , '/' );
40 sub rebless { shift; }
43 my($self, $svc_acct) = (shift, shift);
44 $self->vpopmail_queue( $svc_acct->svcnum, 'insert',
46 crypt($svc_acct->_password,$saltset[int(rand(64))].$saltset[int(rand(64))]),
54 my( $self, $new, $old ) = (shift, shift, shift);
56 my $cpassword = crypt(
57 $new->_password, $saltset[int(rand(64))].$saltset[int(rand(64))]
60 return "can't change username with vpopmail"
61 if $old->username ne $new->username;
63 #no.... if mail can't be preserved, better to disallow username changes
64 #if ($old->username ne $new->username || $old->domain ne $new->domain ) {
65 # vpopmail_queue( $svc_acct->svcnum, 'delete',
66 # $old->username, $old->domain
68 # vpopmail_queue( $svc_acct->svcnum, 'insert',
74 return '' unless $old->_password ne $new->_password;
76 $self->vpopmail_queue( $new->svcnum, 'replace',
77 $new->username, $cpassword, $new->domain, $new->quota, $new->finger );
81 my( $self, $svc_acct ) = (shift, shift);
82 $self->vpopmail_queue( $svc_acct->svcnum, 'delete',
83 $svc_acct->username, $svc_acct->domain );
86 #a good idea to queue anything that could fail or take any time
88 my( $self, $svcnum, $method ) = (shift, shift, shift);
90 my $exportdir = "%%%FREESIDE_EXPORT%%%/export." . datasrc;
91 mkdir $exportdir, 0700 or die $! unless -d $exportdir;
92 $exportdir .= "/vpopmail";
93 mkdir $exportdir, 0700 or die $! unless -d $exportdir;
94 $exportdir .= '/'. $self->machine;
95 mkdir $exportdir, 0700 or die $! unless -d $exportdir;
96 mkdir "$exportdir/domains", 0700 or die $! unless -d "$exportdir/domains";
98 my $queue = new FS::queue {
100 'job' => "FS::part_export::vpopmail::vpopmail_$method",
105 $self->option('dir'),
106 $self->option('uid'),
107 $self->option('gid'),
108 $self->option('restart'),
113 sub vpopmail_insert { #subroutine, not method
114 my( $exportdir, $machine, $dir, $uid, $gid, $restart ) = splice @_,0,6;
115 my( $username, $password, $domain, $quota, $finger ) = @_;
117 mkdir "$exportdir/domains/$domain", 0700 or die $!
118 unless -d "$exportdir/domains/$domain";
120 (open(VPASSWD, ">>$exportdir/domains/$domain/vpasswd")
121 and flock(VPASSWD,LOCK_EX)
122 ) or die "can't open vpasswd file for $username\@$domain: ".
123 "$exportdir/domains/$domain/vpasswd: $!";
124 print VPASSWD join(":",
130 "$dir/domains/$domain/$username",
131 $quota ? $quota.'S' : 'NOQUOTA',
134 flock(VPASSWD,LOCK_UN);
138 grep { ! -d $_ } map { "$exportdir/domains/$domain/$username$_" }
139 ( '', qw( /Maildir /Maildir/cur /Maildir/new /Maildir/tmp ) )
141 mkdir $mkdir, 0700 or die "can't mkdir $mkdir: $!";
144 vpopmail_sync( $exportdir, $machine, $dir, $uid, $gid, $restart );
148 sub vpopmail_replace { #subroutine, not method
149 my( $exportdir, $machine, $dir, $uid, $gid, $restart ) = splice @_,0,6;
150 my( $username, $password, $domain, $quota, $finger ) = @_;
152 (open(VPASSWD, "$exportdir/domains/$domain/vpasswd")
153 and flock(VPASSWD,LOCK_EX)
154 ) or die "can't open $exportdir/domains/$domain/vpasswd: $!";
156 open(VPASSWDTMP, ">$exportdir/domains/$domain/vpasswd.tmp")
157 or die "Can't open $exportdir/domains/$domain/vpasswd.tmp: $!";
160 my ($mailbox, $pw, $vuid, $vgid, $vfinger, $vdir, $vquota, @rest) =
162 if ( $username ne $mailbox ) {
166 print VPASSWDTMP join (':',
172 "$dir/domains/$domain/$username", #$vdir
173 $quota ? $quota.'S' : 'NOQUOTA',
179 rename "$exportdir/domains/$domain/vpasswd.tmp", "$exportdir/domains/$domain/vpasswd"
180 or die "Can't rename $exportdir/domains/$domain/vpasswd.tmp: $!";
182 flock(VPASSWD,LOCK_UN);
185 vpopmail_sync( $exportdir, $machine, $dir, $uid, $gid, $restart );
189 sub vpopmail_delete { #subroutine, not method
190 my( $exportdir, $machine, $dir, $uid, $gid, $restart ) = splice @_,0,6;
191 my( $username, $domain ) = @_;
193 (open(VPASSWD, "$exportdir/domains/$domain/vpasswd")
194 and flock(VPASSWD,LOCK_EX)
195 ) or die "can't open $exportdir/domains/$domain/vpasswd: $!";
197 open(VPASSWDTMP, ">$exportdir/domains/$domain/vpasswd.tmp")
198 or die "Can't open $exportdir/domains/$domain/vpasswd.tmp: $!";
201 my ($mailbox, $rest) = split(':', $_);
202 print VPASSWDTMP $_ unless $username eq $mailbox;
207 rename "$exportdir/domains/$domain/vpasswd.tmp",
208 "$exportdir/domains/$domain/vpasswd"
209 or die "Can't rename $exportdir/domains/$domain/vpasswd.tmp: $!";
211 flock(VPASSWD,LOCK_UN);
214 rmtree "$exportdir/domains/$domain/$username"
215 or die "can't rmtree $exportdir/domains/$domain/$username: $!";
217 vpopmail_sync( $exportdir, $machine, $dir, $uid, $gid, $restart );
221 my( $exportdir, $machine, $dir, $uid, $gid, $restart ) = splice @_,0,6;
224 # my @args = ( $rsync, "-rlpt", "-e", $ssh, "domains/",
225 # "vpopmail\@$machine:$dir/domains/" );
226 # system {$args[0]} @args;
228 eval "use File::Rsync;";
231 my $rsync = File::Rsync->new({ rsh => 'ssh' });
237 src => "$exportdir/domains/",
238 dest => "vpopmail\@$machine:$dir/domains/",
239 } ); # true/false return value from exec is not working, alas
241 die "error uploading to vpopmail\@$machine:$dir/domains/ : ".
242 'exit status: '. $rsync->status. ', '.
243 'STDERR: '. join(" / ", $rsync->err). ', '.
244 'STDOUT: '. join(" / ", $rsync->out);
247 eval "use Net::SSH qw(ssh);";
250 ssh("vpopmail\@$machine", $restart) if $restart;