7 use FS::UID qw(adminsuidsetup datasrc);
8 use FS::Record qw(qsearch); # qsearchs);
14 my $user = shift or die &usage;
17 my $spooldir = "/usr/local/etc/freeside/export.". datasrc. "/postfix";
18 mkdir $spooldir, 0700 unless -d $spooldir;
20 my @exports = qsearch('part_export', { 'exporttype' => 'postfix' } );
22 my $rsync = File::Rsync->new({
27 foreach my $export ( @exports ) {
29 my $machine = $export->machine;
30 my $prefix = "$spooldir/$machine";
31 mkdir $prefix, 0700 unless -d $prefix;
33 #construct %domain hash
35 my $mydomain = $export->option('mydomain');
37 foreach my $svc_forward ( $export->svc_x ) {
39 my( $username, $domain );
40 my $srcsvc_acct = $svc_forward->srcsvc_acct;
42 ( $username, $domain ) = ( $srcsvc_acct->username, $srcsvc_acct->domain );
43 } elsif ( $svc_forward->src =~ /^([^@]*)\@([^@]+)$/ ) {
44 ( $username, $domain ) = ( $1, $2 );
46 die "bad svc_forward record? svcnum ". $svc_forward->svcnum. "\n";
49 my( $dusername, $ddomain );
50 my $dstsvc_acct = $svc_forward->dstsvc_acct;
52 $dusername = $dstsvc_acct->username;
53 $ddomain = $dstsvc_acct->domain;
54 } elsif ( $svc_forward->dst =~ /([^@]+)\@([^@]+)$/ ) {
55 ( $dusername, $ddomain ) = ( $1, $2 );
57 die "bad svc_forward record? svcnum ". $svc_forward->svcnum. "\n";
60 if ( $ddomain eq $mydomain ) {
63 $dest = "$dusername\@$ddomain";
66 push @{$domain{$domain}{$username}}, $dest;
72 my $aliases = delete $domain{$mydomain};
73 open(ALIASES, ">$prefix/aliases") or die "can't open $prefix/aliases: $!";
74 foreach my $alias ( keys %$aliases ) {
75 print ALIASES "$alias: ". join(',', @{ $aliases->{$alias} } ). "\n";
81 open(VIRTUAL, ">$prefix/virtual") or die "can't open $prefix/virtual: $!";
82 foreach my $domain ( keys %domain ) {
83 print VIRTUAL "$domain DOMAIN\n";
84 #foreach my $virtual ( sort { $a ne '' <=> $b ne '' } keys %{$domain{$domain}} ) {
85 foreach my $virtual ( sort { ( ($b ne '') <=> ($a ne '') ) || $a cmp $b } keys %{$domain{$domain}} ) {
86 print VIRTUAL "$virtual\@$domain ".
87 join(',', @{ $domain{$domain}{$virtual} } ). "\n";
95 my $user = $export->option('user');
97 src => "$prefix/aliases",
98 dest => "$user\@$machine:". $export->option('aliases'),
99 } ) or die "rsync to $machine failed: ". join(" / ", $rsync->err);
102 ssh("$user\@$machine", $export->option('newaliases') || 'newaliases');
103 # ssh("$user\@$machine", "postfix reload");
106 src => "$prefix/virtual",
107 dest => "$user\@$machine:". $export->option('virtual'),
108 } ) or die "rsync to $machine failed: ". join(" / ", $rsync->err);
110 ssh("$user\@$machine", $export->option('postmap')
111 || 'postmap hash:/etc/postfix/virtual');
112 ssh("$user\@$machine", $export->option('reload') || 'postfix reload');
119 die "Usage:\n postfix.export user\n";