56c6115a6e9883e03a3ac497218659d11cdb189d
[freeside.git] / bin / svc_acct_sm.export
1 #!/usr/bin/perl -Tw
2 #
3 # $Id: svc_acct_sm.export,v 1.7 2000-07-03 09:13:10 ivan Exp $
4
5 # Create and export config files for sendmail, qmail
6 #
7 # (used to) Create and export VoiceNet_quasar.m4
8 #
9 # ivan@voicenet.com late oct 96
10 #
11 # change priority (after copies) to 19, not 10
12 # ivan@voicenet.com 97-feb-5
13 #
14 # put file in different place and run different script, as per matt and
15 # mohamed
16 # ivan@voicenet.com 97-mar-10
17 #
18 # added exit if stuff is already locked ivan@voicenet.com 97-apr-15
19 #
20 # removed mail2
21 # ivan@voicenet.com 97-jul-10
22 #
23 # rewrote lots of the bits, now exports qmail "virtualdomain",
24 # "recipientmap" and "rcpthosts" files as well
25 #
26 # ivan@voicenet.com 97-sep-4
27 #
28 # adds ".extra" files
29 #
30 # ivan@voicenet.com 97-sep-29
31 #
32 # added ".pp" files, ugh.
33 #
34 # ivan@voicenet.com 97-oct-1
35 #
36 # rewrite ivan@sisd.com 98-mar-9
37 #
38 # now can create .qmail-default files ivan@sisd.com 98-mar-10
39 #
40 # put example $my_domain declaration in ivan@sisd.com 98-mar-23
41 #
42 # /var/spool/freeside/conf and sendmail updates ivan@sisd.com 98-aug-14
43 #
44 # $Log: svc_acct_sm.export,v $
45 # Revision 1.7  2000-07-03 09:13:10  ivan
46 # get rid of double sendmailrestart invocation; no need for multiple sessions
47 #
48 # Revision 1.6  2000/07/03 09:09:14  ivan
49 # typo
50 #
51 # Revision 1.5  2000/07/03 09:03:14  ivan
52 # added sendmailrestart and sendmailconfigpath config files
53 #
54 # Revision 1.4  2000/06/29 14:02:29  ivan
55 # add sendmailrestart configuration file
56 #
57 # Revision 1.3  2000/06/12 08:37:56  ivan
58 # sendmail fix from Jeff Finucane
59 #
60 # Revision 1.2  1998/12/10 07:23:17  ivan
61 # use FS::Conf, need user (for datasrc)
62 #
63
64 use strict;
65 use vars qw($conf);
66 use Fcntl qw(:flock);
67 use FS::SSH qw(ssh scp);
68 use FS::UID qw(adminsuidsetup datasrc);
69 use FS::Record qw(qsearch qsearchs);
70 use FS::svc_acct;
71 use FS::svc_acct_sm;
72 use FS::svc_domain;
73
74 my $user = shift or die &usage;
75 adminsuidsetup $user;
76
77 $conf = new FS::Conf;
78
79 my($shellmachine, @qmailmachines);
80 if ( $conf->exists('qmailmachines') ) {
81   $shellmachine = $conf->config('shellmachine');
82   @qmailmachines = $conf->config('qmailmachines');
83 }
84
85 my(@sendmailmachines, $sendmailconfigpath, $sendmailrestart);
86 if ( $conf->exists('sendmailmachines') ) {
87   @sendmailmachines = $conf->config('sendmailmachines');
88   $sendmailconfigpath = $conf->config('sendmailconfigpath') || '/etc';
89   $sendmailrestart = $conf->config('sendmailrestart');
90 }
91
92 my $mydomain = $conf->config('domain');
93
94 my $spooldir = "/usr/local/etc/freeside/export.". datasrc;
95 my $spoollock = "/usr/local/etc/freeside/svc_acct_sm.export.lock.". datasrc;
96
97 umask 066;
98
99 open(EXPORT,"+>>$spoollock") or die "Can't open $spoollock: $!";
100 select(EXPORT); $|=1; select(STDOUT);
101 unless ( flock(EXPORT,LOCK_EX|LOCK_NB) ) {
102   seek(EXPORT,0,0);
103   my($pid)=<EXPORT>;
104   chop($pid);
105   #no reason to start locks of blocking processes
106   die "Is another export process running under pid $pid?\n";
107 }
108 seek(EXPORT,0,0);
109 print EXPORT $$,"\n";
110
111 ( open(RCPTHOSTS,">$spooldir/rcpthosts")
112   and flock(RCPTHOSTS,LOCK_EX|LOCK_NB) 
113 ) or die "Can't open $spooldir/rcpthosts: $!";
114 ( open(RECIPIENTMAP,">$spooldir/recipientmap") 
115   and flock(RECIPIENTMAP,LOCK_EX|LOCK_NB) 
116 ) or die "Can't open $spooldir/recipientmap: $!";
117 ( open(VIRTUALDOMAINS,">$spooldir/virtualdomains") 
118   and flock(VIRTUALDOMAINS,LOCK_EX|LOCK_NB)
119 ) or die "Can't open $spooldir/virtualdomains: $!";
120 ( open(VIRTUSERTABLE,">$spooldir/virtusertable")
121   and flock(VIRTUSERTABLE,LOCK_EX|LOCK_NB)
122 ) or die "Can't open $spooldir/virtusertable: $!";
123 ( open(SENDMAIL_CW,">$spooldir/sendmail.cw")
124   and flock(SENDMAIL_CW,LOCK_EX|LOCK_NB)
125 ) or die "Can't open $spooldir/sendmail.cw: $!";
126
127 setpriority(0,0,10);
128
129 my($svc_domain,%domain);
130 foreach $svc_domain ( qsearch('svc_domain',{}) ) {
131   my($domain)=$svc_domain->domain;
132   $domain{$svc_domain->svcnum}=$domain;
133   print RCPTHOSTS "$domain\n.$domain\n";
134   print SENDMAIL_CW "$domain\n";
135 }
136
137 my(@sendmail);
138
139 my($svc_acct_sm);
140 foreach $svc_acct_sm ( qsearch('svc_acct_sm') ) { 
141   my($domsvc,$domuid,$domuser)=(
142     $svc_acct_sm->domsvc,
143     $svc_acct_sm->domuid,
144     $svc_acct_sm->domuser,
145   );
146   my($domain)=$domain{$domsvc};
147   my($svc_acct)=qsearchs('svc_acct',{'uid'=>$domuid});
148   my($username,$dir,$uid,$gid)=(
149     $svc_acct->username,
150     $svc_acct->dir,
151     $svc_acct->uid,
152     $svc_acct->gid,
153   );
154   next unless $username && $domain && $domuser;
155
156   if ($domuser eq '*') {
157     push @sendmail, "\@$domain\t$username\n";
158     print VIRTUALDOMAINS "$domain:$username-$domain\n",
159                          ".$domain:$username-$domain\n",
160     ;
161     ###
162     # qmail
163     ssh("root\@$shellmachine",
164       "[ -e $dir/.qmail-default ] || { touch $dir/.qmail-default; chown $uid:$gid $dir/.qmail-default; }"
165     ) if ( $shellmachine && $dir && $uid );
166
167   } else {
168     print VIRTUSERTABLE "$domuser\@$domain\t$username\n";
169     print RECIPIENTMAP "$domuser\@$domain:$username\@$mydomain\n";
170   }
171
172 }
173
174 print VIRTUSERTABLE @sendmail;
175
176 chmod 0644, "$spooldir/sendmail.cw",
177             "$spooldir/virtusertable",
178             "$spooldir/rcpthosts",
179             "$spooldir/recipientmap",
180             "$spooldir/virtualdomains",
181 ;
182
183 flock(SENDMAIL_CW,LOCK_UN);
184 flock(VIRTUSERTABLE,LOCK_UN);
185 flock(RCPTHOSTS,LOCK_UN);
186 flock(RECIPIENTMAP,LOCK_UN);
187 flock(VIRTUALDOMAINS,LOCK_UN);
188
189 close SENDMAIL_CW;
190 close VIRTUSERTABLE;
191 close RCPTHOSTS;
192 close RECIPIENTMAP;
193 close VIRTUALDOMAINS;
194
195 ###
196 # export stuff
197 #
198
199 my($sendmailmachine);
200 foreach $sendmailmachine (@sendmailmachines) {
201   scp("$spooldir/sendmail.cw","root\@$sendmailmachine:$sendmailconfigpath/sendmail.cw.new")
202     == 0 or die "scp error: $!";
203   scp("$spooldir/virtusertable","root\@$sendmailmachine:$sendmailconfigpath/virtusertable.new")
204     == 0 or die "scp error: $!";
205   ssh("root\@$sendmailmachine",
206     "( ".
207       "mv $sendmailconfigpath/sendmail.cw.new $sendmailconfigpath/sendmail.cw; ".
208       "mv $sendmailconfigpath/virtusertable.new $sendmailconfigpath/virtusertable; ".
209       $sendmailrestart.
210     " )"
211   )
212     == 0 or die "ssh error: $!";
213 }
214
215 my($qmailmachine);
216 foreach $qmailmachine (@qmailmachines) {
217   scp("$spooldir/recipientmap","root\@$qmailmachine:/var/qmail/control/recipientmap")
218     == 0 or die "scp error: $!";
219   scp("$spooldir/virtualdomains","root\@$qmailmachine:/var/qmail/control/virtualdomains")
220     == 0 or die "scp error: $!";
221   scp("$spooldir/rcpthosts","root\@$qmailmachine:/var/qmail/control/rcpthosts")
222     == 0 or die "scp error: $!";
223   #ssh("root\@$qmailmachine","/etc/init.d/qmail restart")
224   #  == 0 or die "ssh error: $!";
225 }
226
227 unlink $spoollock;
228 flock(EXPORT,LOCK_UN);
229 close EXPORT;
230
231 #
232
233 sub usage {
234   die "Usage:\n\n  svc_acct.export user\n";
235 }
236