get rid of FS::SSH.pm (became Net::SSH and Net::SCP on CPAN)
[freeside.git] / bin / svc_acct_sm.export
1 #!/usr/bin/perl -Tw
2 #
3 # $Id: svc_acct_sm.export,v 1.9 2001-04-22 01:56:15 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.9  2001-04-22 01:56:15  ivan
46 # get rid of FS::SSH.pm (became Net::SSH and Net::SCP on CPAN)
47 #
48 # Revision 1.8  2000/07/06 03:37:24  ivan
49 # don't error out on invalid svc_acct_sm.domuid's that can't be matched in
50 # svc_acct.uid - just warn.
51 #
52 # Revision 1.7  2000/07/03 09:13:10  ivan
53 # get rid of double sendmailrestart invocation; no need for multiple sessions
54 #
55 # Revision 1.6  2000/07/03 09:09:14  ivan
56 # typo
57 #
58 # Revision 1.5  2000/07/03 09:03:14  ivan
59 # added sendmailrestart and sendmailconfigpath config files
60 #
61 # Revision 1.4  2000/06/29 14:02:29  ivan
62 # add sendmailrestart configuration file
63 #
64 # Revision 1.3  2000/06/12 08:37:56  ivan
65 # sendmail fix from Jeff Finucane
66 #
67 # Revision 1.2  1998/12/10 07:23:17  ivan
68 # use FS::Conf, need user (for datasrc)
69 #
70
71 use strict;
72 use vars qw($conf);
73 use Fcntl qw(:flock);
74 use Net::SSH qw(ssh);
75 use Net::SCP qw(scp);
76 use FS::UID qw(adminsuidsetup datasrc);
77 use FS::Record qw(qsearch qsearchs);
78 use FS::svc_acct;
79 use FS::svc_acct_sm;
80 use FS::svc_domain;
81
82 my $user = shift or die &usage;
83 adminsuidsetup $user;
84
85 $conf = new FS::Conf;
86
87 my($shellmachine, @qmailmachines);
88 if ( $conf->exists('qmailmachines') ) {
89   $shellmachine = $conf->config('shellmachine');
90   @qmailmachines = $conf->config('qmailmachines');
91 }
92
93 my(@sendmailmachines, $sendmailconfigpath, $sendmailrestart);
94 if ( $conf->exists('sendmailmachines') ) {
95   @sendmailmachines = $conf->config('sendmailmachines');
96   $sendmailconfigpath = $conf->config('sendmailconfigpath') || '/etc';
97   $sendmailrestart = $conf->config('sendmailrestart');
98 }
99
100 my $mydomain = $conf->config('domain');
101
102 my $spooldir = "/usr/local/etc/freeside/export.". datasrc;
103 my $spoollock = "/usr/local/etc/freeside/svc_acct_sm.export.lock.". datasrc;
104
105 umask 066;
106
107 open(EXPORT,"+>>$spoollock") or die "Can't open $spoollock: $!";
108 select(EXPORT); $|=1; select(STDOUT);
109 unless ( flock(EXPORT,LOCK_EX|LOCK_NB) ) {
110   seek(EXPORT,0,0);
111   my($pid)=<EXPORT>;
112   chop($pid);
113   #no reason to start locks of blocking processes
114   die "Is another export process running under pid $pid?\n";
115 }
116 seek(EXPORT,0,0);
117 print EXPORT $$,"\n";
118
119 ( open(RCPTHOSTS,">$spooldir/rcpthosts")
120   and flock(RCPTHOSTS,LOCK_EX|LOCK_NB) 
121 ) or die "Can't open $spooldir/rcpthosts: $!";
122 ( open(RECIPIENTMAP,">$spooldir/recipientmap") 
123   and flock(RECIPIENTMAP,LOCK_EX|LOCK_NB) 
124 ) or die "Can't open $spooldir/recipientmap: $!";
125 ( open(VIRTUALDOMAINS,">$spooldir/virtualdomains") 
126   and flock(VIRTUALDOMAINS,LOCK_EX|LOCK_NB)
127 ) or die "Can't open $spooldir/virtualdomains: $!";
128 ( open(VIRTUSERTABLE,">$spooldir/virtusertable")
129   and flock(VIRTUSERTABLE,LOCK_EX|LOCK_NB)
130 ) or die "Can't open $spooldir/virtusertable: $!";
131 ( open(SENDMAIL_CW,">$spooldir/sendmail.cw")
132   and flock(SENDMAIL_CW,LOCK_EX|LOCK_NB)
133 ) or die "Can't open $spooldir/sendmail.cw: $!";
134
135 setpriority(0,0,10);
136
137 my($svc_domain,%domain);
138 foreach $svc_domain ( qsearch('svc_domain',{}) ) {
139   my($domain)=$svc_domain->domain;
140   $domain{$svc_domain->svcnum}=$domain;
141   print RCPTHOSTS "$domain\n.$domain\n";
142   print SENDMAIL_CW "$domain\n";
143 }
144
145 my(@sendmail);
146
147 my($svc_acct_sm);
148 foreach $svc_acct_sm ( qsearch('svc_acct_sm') ) { 
149   my($domsvc,$domuid,$domuser)=(
150     $svc_acct_sm->domsvc,
151     $svc_acct_sm->domuid,
152     $svc_acct_sm->domuser,
153   );
154   my($domain)=$domain{$domsvc};
155   my($svc_acct)=qsearchs('svc_acct',{'uid'=>$domuid});
156   unless ( $svc_acct ) {
157     warn "WARNING: couldn't find svc_acct.uid $domuid (svc_acct_sm.svcnum ".
158          $svc_acct_sm->svcnum. ") - corruped database?\n";
159     next;
160   }
161   my($username,$dir,$uid,$gid)=(
162     $svc_acct->username,
163     $svc_acct->dir,
164     $svc_acct->uid,
165     $svc_acct->gid,
166   );
167   next unless $username && $domain && $domuser;
168
169   if ($domuser eq '*') {
170     push @sendmail, "\@$domain\t$username\n";
171     print VIRTUALDOMAINS "$domain:$username-$domain\n",
172                          ".$domain:$username-$domain\n",
173     ;
174     ###
175     # qmail
176     ssh("root\@$shellmachine",
177       "[ -e $dir/.qmail-default ] || { touch $dir/.qmail-default; chown $uid:$gid $dir/.qmail-default; }"
178     ) if ( $shellmachine && $dir && $uid );
179
180   } else {
181     print VIRTUSERTABLE "$domuser\@$domain\t$username\n";
182     print RECIPIENTMAP "$domuser\@$domain:$username\@$mydomain\n";
183   }
184
185 }
186
187 print VIRTUSERTABLE @sendmail;
188
189 chmod 0644, "$spooldir/sendmail.cw",
190             "$spooldir/virtusertable",
191             "$spooldir/rcpthosts",
192             "$spooldir/recipientmap",
193             "$spooldir/virtualdomains",
194 ;
195
196 flock(SENDMAIL_CW,LOCK_UN);
197 flock(VIRTUSERTABLE,LOCK_UN);
198 flock(RCPTHOSTS,LOCK_UN);
199 flock(RECIPIENTMAP,LOCK_UN);
200 flock(VIRTUALDOMAINS,LOCK_UN);
201
202 close SENDMAIL_CW;
203 close VIRTUSERTABLE;
204 close RCPTHOSTS;
205 close RECIPIENTMAP;
206 close VIRTUALDOMAINS;
207
208 ###
209 # export stuff
210 #
211
212 my($sendmailmachine);
213 foreach $sendmailmachine (@sendmailmachines) {
214   scp("$spooldir/sendmail.cw","root\@$sendmailmachine:$sendmailconfigpath/sendmail.cw.new")
215     == 0 or die "scp error: $!";
216   scp("$spooldir/virtusertable","root\@$sendmailmachine:$sendmailconfigpath/virtusertable.new")
217     == 0 or die "scp error: $!";
218   ssh("root\@$sendmailmachine",
219     "( ".
220       "mv $sendmailconfigpath/sendmail.cw.new $sendmailconfigpath/sendmail.cw; ".
221       "mv $sendmailconfigpath/virtusertable.new $sendmailconfigpath/virtusertable; ".
222       $sendmailrestart.
223     " )"
224   )
225     == 0 or die "ssh error: $!";
226 }
227
228 my($qmailmachine);
229 foreach $qmailmachine (@qmailmachines) {
230   scp("$spooldir/recipientmap","root\@$qmailmachine:/var/qmail/control/recipientmap")
231     == 0 or die "scp error: $!";
232   scp("$spooldir/virtualdomains","root\@$qmailmachine:/var/qmail/control/virtualdomains")
233     == 0 or die "scp error: $!";
234   scp("$spooldir/rcpthosts","root\@$qmailmachine:/var/qmail/control/rcpthosts")
235     == 0 or die "scp error: $!";
236   #ssh("root\@$qmailmachine","/etc/init.d/qmail restart")
237   #  == 0 or die "ssh error: $!";
238 }
239
240 unlink $spoollock;
241 flock(EXPORT,LOCK_UN);
242 close EXPORT;
243
244 #
245
246 sub usage {
247   die "Usage:\n\n  svc_acct.export user\n";
248 }
249