summaryrefslogtreecommitdiff
path: root/bin/svc_acct_sm.export
blob: c2ec1e53f61c232e8181ca53415afc1fc21c6ec9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#!/usr/bin/perl -Tw
#
# Create and export VoiceNet_quasar.m4
#
# ivan@voicenet.com late oct 96
#
# change priority (after copies) to 19, not 10
# ivan@voicenet.com 97-feb-5
#
# put file in different place and run different script, as per matt and
# mohamed
# ivan@voicenet.com 97-mar-10
#
# added exit if stuff is already locked ivan@voicenet.com 97-apr-15
#
# removed mail2
# ivan@voicenet.com 97-jul-10
#
# rewrote lots of the bits, now exports qmail "virtualdomain",
# "recipientmap" and "rcpthosts" files as well
#
# ivan@voicenet.com 97-sep-4
#
# adds ".extra" files
#
# ivan@voicenet.com 97-sep-29
#
# added ".pp" files, ugh.
#
# ivan@voicenet.com 97-oct-1
#
# rewrite ivan@sisd.com 98-mar-9
#
# now can create .qmail-default files ivan@sisd.com 98-mar-10
#
# put example $my_domain declaration in ivan@sisd.com 98-mar-23
#
# /var/spool/freeside/conf and sendmail updates ivan@sisd.com 98-aug-14

use strict;
use Fcntl qw(:flock);
use FS::SSH qw(ssh scp);
use FS::UID qw(adminsuidsetup);
use FS::Record qw(qsearch qsearchs);

my($conf_shellm)="/var/spool/freeside/conf/shellmachine";
my($fqmailmachines)="/var/spool/freeside/conf/qmailmachines";
my($shellmachine);
my(@qmailmachines);
if ( -e $fqmailmachines ) {
  open(SHELLMACHINE,$conf_shellm) or die "Can't open $conf_shellm: $!";
  <SHELLMACHINE> =~ /^([\w\.\-]+)$/ or die "Illegal $conf_shellm";
  $shellmachine = $1;
  close SHELLMACHINE;
  open(QMAILMACHINES,$fqmailmachines);
  @qmailmachines=map {
    /^(.*)$/ or die "Illegal line in conf/qmailmachines"; #we trust the file
    $1;
  } grep $_ !~ /^(#|$)/, <QMAILMACHINES>;
  close QMAILMACHINES;
}

my($fsendmailmachines)="/var/spool/freeside/conf/sendmailmachines";
my(@sendmailmachines);
if ( -e $fsendmailmachines ) {
  open(SENDMAILMACHINES,$fsendmailmachines);
  @sendmailmachines=map {
    /^(.*)$/ or die "Illegal line in conf/sendmailmachines"; #we trust the file
    $1;
  } grep $_ !~ /^(#|$)/, <SENDMAILMACHINES>;
  close SENDMAILMACHINES;
}

my($conf_domain)="/var/spool/freeside/conf/domain";
open(DOMAIN,$conf_domain) or die "Can't open $conf_domain: $!";
my($mydomain)=map {
  /^(.*)$/ or die "Illegal line in $conf_domain!"; #yes, we trust the file
  $1
} grep $_ !~ /^(#|$)/, <DOMAIN>;
close DOMAIN;

my($spooldir)="/var/spool/freeside/export";
my($spoollock)="/var/spool/freeside/svc_acct_sm.export.lock";

adminsuidsetup;
umask 066;

open(EXPORT,"+>>$spoollock") or die "Can't open $spoollock: $!";
select(EXPORT); $|=1; select(STDOUT);
unless ( flock(EXPORT,LOCK_EX|LOCK_NB) ) {
  seek(EXPORT,0,0);
  my($pid)=<EXPORT>;
  chop($pid);
  #no reason to start locks of blocking processes
  die "Is another export process running under pid $pid?\n";
}
seek(EXPORT,0,0);
print EXPORT $$,"\n";

my(@svc_acct_sm)=qsearch('svc_acct_sm',{});

( open(RCPTHOSTS,">$spooldir/rcpthosts")
  and flock(RCPTHOSTS,LOCK_EX|LOCK_NB) 
) or die "Can't open $spooldir/rcpthosts: $!";
( open(RECIPIENTMAP,">$spooldir/recipientmap") 
  and flock(RECIPIENTMAP,LOCK_EX|LOCK_NB) 
) or die "Can't open $spooldir/recipientmap: $!";
( open(VIRTUALDOMAINS,">$spooldir/virtualdomains") 
  and flock(VIRTUALDOMAINS,LOCK_EX|LOCK_NB)
) or die "Can't open $spooldir/virtualdomains: $!";
( open(VIRTUSERTABLE,">$spooldir/virtusertable")
  and flock(VIRTUSERTABLE,LOCK_EX|LOCK_NB)
) or die "Can't open $spooldir/virtusertable: $!";
( open(SENDMAIL_CW,">$spooldir/sendmail.cw")
  and flock(SENDMAIL_CW,LOCK_EX|LOCK_NB)
) or die "Can't open $spooldir/sendmail.cw: $!";

setpriority(0,0,10);

my($svc_domain,%domain);
foreach $svc_domain ( qsearch('svc_domain',{}) ) {
  my($domain)=$svc_domain->domain;
  $domain{$svc_domain->svcnum}=$domain;
  print RCPTHOSTS "$domain\n.$domain\n";
  print SENDMAIL_CW "$domain\n";
}

my(@sendmail);

my($svc_acct_sm);
foreach $svc_acct_sm ( qsearch('svc_acct_sm') ) { 
  my($domsvc,$domuid,$domuser)=(
    $svc_acct_sm->domsvc,
    $svc_acct_sm->domuid,
    $svc_acct_sm->domuser,
  );
  my($domain)=$domain{$domsvc};
  my($svc_acct)=qsearchs('svc_acct',{'uid'=>$domuid});
  my($username,$dir,$uid,$gid)=(
    $svc_acct->username,
    $svc_acct->dir,
    $svc_acct->uid,
    $svc_acct->gid,
  );
  next unless $username && $domain && $domuser;

  if ($domuser eq '*') {
    push @sendmail, "\@$domain\t$username\n";
    print VIRTUALDOMAINS "$domain:$username-$domain\n",
                         ".$domain:$username-$domain\n",
    ;
    ###
    # qmail
    ssh("root\@$shellmachine",
      "[ -e $dir/.qmail-default ] || { touch $dir/.qmail-default; chown $uid:$gid $dir/.qmail-default; }"
    ) if ( $shellmachine && $dir && $uid );

  } else {
    print VIRTUSERTABLE "$domuser\@$domain\t$username\n";
    print RECIPIENTMAP "$domuser\@$domain:$username\@$mydomain\n";
  }

  print VIRTUSERTABLE @sendmail;

}

chmod 0644, "$spooldir/sendmail.cw",
            "$spooldir/virtusertable",
            "$spooldir/rcpthosts",
            "$spooldir/recipientmap",
            "$spooldir/virtualdomains",
;

flock(SENDMAIL_CW,LOCK_UN);
flock(VIRTUSERTABLE,LOCK_UN);
flock(RCPTHOSTS,LOCK_UN);
flock(RECIPIENTMAP,LOCK_UN);
flock(VIRTUALDOMAINS,LOCK_UN);

close SENDMAIL_CW;
close VIRTUSERTABLE;
close RCPTHOSTS;
close RECIPIENTMAP;
close VIRTUALDOMAINS;

###
# export stuff
#

my($sendmailmachine);
foreach $sendmailmachine (@sendmailmachines) {
  scp("$spooldir/sendmail.cw","root\@$sendmailmachine:/etc/sendmail.cw.new")
    == 0 or die "scp error: $!";
  scp("$spooldir/virtusertable","root\@$sendmailmachine:/etc/virtusertable.new")
    == 0 or die "scp error: $!";
  ssh("root\@$sendmailmachine",
    "( ".
      "mv /etc/sendmail.cw.new /etc/sendmail.cw; ".
      "mv /etc/virtusertable.new /etc/virtusertable; ".
      #"/etc/init.d/sendmail restart; ".
    " )"
  )
    == 0 or die "ssh error: $!";
}

my($qmailmachine);
foreach $qmailmachine (@qmailmachines) {
  scp("$spooldir/recipientmap","root\@$qmailmachine:/var/qmail/control/recipientmap")
    == 0 or die "scp error: $!";
  scp("$spooldir/virtualdomains","root\@$qmailmachine:/var/qmail/control/virtualdomains")
    == 0 or die "scp error: $!";
  scp("$spooldir/rcpthosts","root\@$qmailmachine:/var/qmail/control/rcpthosts")
    == 0 or die "scp error: $!";
  #ssh("root\@$qmailmachine","/etc/init.d/qmail restart")
  #  == 0 or die "ssh error: $!";
}

unlink $spoollock;
flock(EXPORT,LOCK_UN);
close EXPORT;