initial checkin of module files for proper perl installation
[freeside.git] / bin / svc_acct_sm.export
1 #!/usr/bin/perl -Tw
2 #
3 # $Id: svc_acct_sm.export,v 1.2 1998-12-10 07:23:17 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.2  1998-12-10 07:23:17  ivan
46 # use FS::Conf, need user (for datasrc)
47 #
48
49 use strict;
50 use vars qw($conf);
51 use Fcntl qw(:flock);
52 use FS::SSH qw(ssh scp);
53 use FS::UID qw(adminsuidsetup datasrc);
54 use FS::Record qw(qsearch qsearchs);
55 use FS::svc_acct;
56 use FS::svc_acct_sm;
57 use FS::svc_domain;
58
59 my $user = shift or die &usage;
60 adminsuidsetup $user;
61
62 $conf = new FS::Conf;
63
64 my($shellmachine);
65 my(@qmailmachines);
66 if ( $conf->exists('qmailmachines') ) {
67   $shellmachine = $conf->config('shellmachine');
68   @qmailmachines = $conf->config('qmailmachines');
69 }
70
71 my @sendmailmachines = $conf->config('sendmailmachines')
72   if $conf->exists('sendmailmachines');
73
74 my $mydomain = $conf->config('domain');
75
76 my $spooldir = "/usr/local/etc/freeside/export.". datasrc;
77 my $spoollock = "/usr/local/etc/freeside/svc_acct_sm.export.lock.". datasrc;
78
79 umask 066;
80
81 open(EXPORT,"+>>$spoollock") or die "Can't open $spoollock: $!";
82 select(EXPORT); $|=1; select(STDOUT);
83 unless ( flock(EXPORT,LOCK_EX|LOCK_NB) ) {
84   seek(EXPORT,0,0);
85   my($pid)=<EXPORT>;
86   chop($pid);
87   #no reason to start locks of blocking processes
88   die "Is another export process running under pid $pid?\n";
89 }
90 seek(EXPORT,0,0);
91 print EXPORT $$,"\n";
92
93 my(@svc_acct_sm)=qsearch('svc_acct_sm',{});
94
95 ( open(RCPTHOSTS,">$spooldir/rcpthosts")
96   and flock(RCPTHOSTS,LOCK_EX|LOCK_NB) 
97 ) or die "Can't open $spooldir/rcpthosts: $!";
98 ( open(RECIPIENTMAP,">$spooldir/recipientmap") 
99   and flock(RECIPIENTMAP,LOCK_EX|LOCK_NB) 
100 ) or die "Can't open $spooldir/recipientmap: $!";
101 ( open(VIRTUALDOMAINS,">$spooldir/virtualdomains") 
102   and flock(VIRTUALDOMAINS,LOCK_EX|LOCK_NB)
103 ) or die "Can't open $spooldir/virtualdomains: $!";
104 ( open(VIRTUSERTABLE,">$spooldir/virtusertable")
105   and flock(VIRTUSERTABLE,LOCK_EX|LOCK_NB)
106 ) or die "Can't open $spooldir/virtusertable: $!";
107 ( open(SENDMAIL_CW,">$spooldir/sendmail.cw")
108   and flock(SENDMAIL_CW,LOCK_EX|LOCK_NB)
109 ) or die "Can't open $spooldir/sendmail.cw: $!";
110
111 setpriority(0,0,10);
112
113 my($svc_domain,%domain);
114 foreach $svc_domain ( qsearch('svc_domain',{}) ) {
115   my($domain)=$svc_domain->domain;
116   $domain{$svc_domain->svcnum}=$domain;
117   print RCPTHOSTS "$domain\n.$domain\n";
118   print SENDMAIL_CW "$domain\n";
119 }
120
121 my(@sendmail);
122
123 my($svc_acct_sm);
124 foreach $svc_acct_sm ( qsearch('svc_acct_sm') ) { 
125   my($domsvc,$domuid,$domuser)=(
126     $svc_acct_sm->domsvc,
127     $svc_acct_sm->domuid,
128     $svc_acct_sm->domuser,
129   );
130   my($domain)=$domain{$domsvc};
131   my($svc_acct)=qsearchs('svc_acct',{'uid'=>$domuid});
132   my($username,$dir,$uid,$gid)=(
133     $svc_acct->username,
134     $svc_acct->dir,
135     $svc_acct->uid,
136     $svc_acct->gid,
137   );
138   next unless $username && $domain && $domuser;
139
140   if ($domuser eq '*') {
141     push @sendmail, "\@$domain\t$username\n";
142     print VIRTUALDOMAINS "$domain:$username-$domain\n",
143                          ".$domain:$username-$domain\n",
144     ;
145     ###
146     # qmail
147     ssh("root\@$shellmachine",
148       "[ -e $dir/.qmail-default ] || { touch $dir/.qmail-default; chown $uid:$gid $dir/.qmail-default; }"
149     ) if ( $shellmachine && $dir && $uid );
150
151   } else {
152     print VIRTUSERTABLE "$domuser\@$domain\t$username\n";
153     print RECIPIENTMAP "$domuser\@$domain:$username\@$mydomain\n";
154   }
155
156   print VIRTUSERTABLE @sendmail;
157
158 }
159
160 chmod 0644, "$spooldir/sendmail.cw",
161             "$spooldir/virtusertable",
162             "$spooldir/rcpthosts",
163             "$spooldir/recipientmap",
164             "$spooldir/virtualdomains",
165 ;
166
167 flock(SENDMAIL_CW,LOCK_UN);
168 flock(VIRTUSERTABLE,LOCK_UN);
169 flock(RCPTHOSTS,LOCK_UN);
170 flock(RECIPIENTMAP,LOCK_UN);
171 flock(VIRTUALDOMAINS,LOCK_UN);
172
173 close SENDMAIL_CW;
174 close VIRTUSERTABLE;
175 close RCPTHOSTS;
176 close RECIPIENTMAP;
177 close VIRTUALDOMAINS;
178
179 ###
180 # export stuff
181 #
182
183 my($sendmailmachine);
184 foreach $sendmailmachine (@sendmailmachines) {
185   scp("$spooldir/sendmail.cw","root\@$sendmailmachine:/etc/sendmail.cw.new")
186     == 0 or die "scp error: $!";
187   scp("$spooldir/virtusertable","root\@$sendmailmachine:/etc/virtusertable.new")
188     == 0 or die "scp error: $!";
189   ssh("root\@$sendmailmachine",
190     "( ".
191       "mv /etc/sendmail.cw.new /etc/sendmail.cw; ".
192       "mv /etc/virtusertable.new /etc/virtusertable; ".
193       #"/etc/init.d/sendmail restart; ".
194     " )"
195   )
196     == 0 or die "ssh error: $!";
197 }
198
199 my($qmailmachine);
200 foreach $qmailmachine (@qmailmachines) {
201   scp("$spooldir/recipientmap","root\@$qmailmachine:/var/qmail/control/recipientmap")
202     == 0 or die "scp error: $!";
203   scp("$spooldir/virtualdomains","root\@$qmailmachine:/var/qmail/control/virtualdomains")
204     == 0 or die "scp error: $!";
205   scp("$spooldir/rcpthosts","root\@$qmailmachine:/var/qmail/control/rcpthosts")
206     == 0 or die "scp error: $!";
207   #ssh("root\@$qmailmachine","/etc/init.d/qmail restart")
208   #  == 0 or die "ssh error: $!";
209 }
210
211 unlink $spoollock;
212 flock(EXPORT,LOCK_UN);
213 close EXPORT;
214
215 #
216
217 sub usage {
218   die "Usage:\n\n  svc_acct.export user\n";
219 }
220