templates!!!
[freeside.git] / bin / svc_acct_sm.import
1 #!/usr/bin/perl -Tw
2 #
3 # $Id: svc_acct_sm.import,v 1.9 2001-04-22 01:56:15 ivan Exp $
4 #
5 # ivan@sisd.com 98-mar-9
6 #
7 # generalized svcparts ivan@sisd.com 98-mar-23
8
9 # You really need to enable ssh into a shell machine as this needs to rename
10 # .qmail-extension files.
11 #
12 # now an interactive script ivan@sisd.com 98-jun-30
13 #
14 # has an (untested) section for sendmail, s/warn/die/g and generates a program
15 # to run on your mail machine _later_ instead of ssh'ing for each user
16 # ivan@sisd.com 98-jul-13
17 #
18 # $Log: svc_acct_sm.import,v $
19 # Revision 1.9  2001-04-22 01:56:15  ivan
20 # get rid of FS::SSH.pm (became Net::SSH and Net::SCP on CPAN)
21 #
22 # Revision 1.8  2000/12/03 15:14:00  ivan
23 # bugfixes from Jeff Finucane <jeff@cmh.net>, thanks!
24 #
25 # Revision 1.7  2000/06/29 10:51:52  ivan
26 # oops, silly mistake
27 #
28 # Revision 1.6  2000/06/29 10:48:25  ivan
29 # make svc_acct_sm skip blank lines in sendmail import
30 #
31 # Revision 1.5  2000/02/03 05:16:52  ivan
32 # beginning of DNS and Apache support
33 #
34 # Revision 1.4  1999/03/25 08:42:20  ivan
35 # import stuff uses Term::Query and spits out (some kinds of) nonsensical input
36 #
37 # Revision 1.3  1999/03/24 00:51:55  ivan
38 # die if no relevant services... cvspain
39 #
40 # Revision 1.2  1998/12/10 07:23:18  ivan
41 # use FS::Conf, need user (for datasrc)
42 #
43
44 use strict;
45 use vars qw(%d_part_svc %m_part_svc);
46 use Term::Query qw(query);
47 use Net::SCP qw(iscp);
48 use FS::UID qw(adminsuidsetup datasrc);
49 use FS::Record qw(qsearch qsearchs);
50 use FS::svc_acct_sm;
51 use FS::svc_domain;
52 use FS::svc_acct;
53 use FS::part_svc;
54
55 my $user = shift or die &usage;
56 adminsuidsetup $user;
57
58 my($spooldir)="/usr/local/etc/freeside/export.". datasrc;
59
60 my(%mta) = (
61   1 => "qmail",
62   2 => "sendmail",
63 );
64
65 ###
66
67 %d_part_svc =
68   map { $_->svcpart, $_ } qsearch('part_svc',{'svcdb'=>'svc_domain'});
69 %m_part_svc =
70   map { $_->svcpart, $_ } qsearch('part_svc',{'svcdb'=>'svc_acct_sm'});
71
72 die "No services with svcdb svc_domain!\n" unless %d_part_svc;
73 die "No services with svcdb svc_svc_acct_sm!\n" unless %m_part_svc;
74
75 print "\n\n", 
76       ( join "\n", map "$_: ".$d_part_svc{$_}->svc, sort keys %d_part_svc ),
77       "\n\n";
78 $^W=0; #Term::Query isn't -w-safe
79 my $domain_svcpart = 
80   query "Enter part number for domains: ", 'irk', [ keys %d_part_svc ];
81 $^W=1;
82
83 print "\n\n", 
84       ( join "\n", map "$_: ".$m_part_svc{$_}->svc, sort keys %m_part_svc ),
85       "\n\n";
86 $^W=0; #Term::Query isn't -w-safe
87 my $mailalias_svcpart = 
88   query "Enter part number for mail aliases: ", 'irk', [ keys %m_part_svc ];
89 $^W=1;
90
91 print "\n\n", <<END;
92 Select your MTA from the following list.
93 END
94 print join "\n", map "$_: $mta{$_}", sort keys %mta;
95 print "\n\n";
96 $^W=0; #Term::Query isn't -w-safe
97 my $mta = query ":", 'irk', [ keys %mta ];
98 $^W=1;
99
100 if ( $mta{$mta} eq "qmail" ) {
101
102   print "\n\n", <<END;
103 Enter the location and name of your qmail control directory, for example
104 "mail.isp.com:/var/qmail/control"
105 END
106   my($control)=&getvalue(":");
107   iscp("root\@$control/rcpthosts","$spooldir/rcpthosts.import");
108 #  iscp("root\@$control/recipientmap","$spooldir/recipientmap.import");
109   iscp("root\@$control/virtualdomains","$spooldir/virtualdomains.import");
110
111 #  print "\n\n", <<END;
112 #Enter the name of the machine with your user .qmail files, for example
113 #"mail.isp.com"
114 #END
115 #  print ":";
116 #  my($shellmachine)=&getvalue;
117
118 } elsif ( $mta{$mta} eq "sendmail" ) {
119
120   print "\n\n", <<END;
121 Enter the location and name of your sendmail virtual user table, for example
122 "mail.isp.com:/etc/virtusertable"
123 END
124   my($virtusertable)=&getvalue(":");
125   iscp("root\@$virtusertable","$spooldir/virtusertable.import");
126
127   print "\n\n", <<END;
128 Enter the location and name of your sendmail.cw file, for example
129 "mail.isp.com:/etc/sendmail.cw"
130 END
131   my($sendmail_cw)=&getvalue(":");
132   iscp("root\@$sendmail_cw","$spooldir/sendmail.cw.import");
133
134 } else {
135   die "Unknown MTA!\n";
136 }
137
138 sub getvalue {
139   my $prompt = shift;
140   $^W=0; #Term::Query isn't -w-safe
141   my $data = query $prompt, '';
142   $^W=1;
143   $data;
144 }
145
146 print "\n\n";
147
148 ###
149
150 $FS::svc_domain::whois_hack=1;
151 $FS::svc_acct_sm::nossh_hack=1;
152
153 if ( $mta{$mta} eq "qmail" ) {
154   open(RCPTHOSTS,"<$spooldir/rcpthosts.import")
155     or die "Can't open $spooldir/rcpthosts.import: $!";
156 } elsif ( $mta{$mta} eq "sendmail" ) {
157   open(RCPTHOSTS,"<$spooldir/sendmail.cw.import")
158     or die "Can't open $spooldir/sendmail.cw.import: $!";
159 } else {
160   die "Unknown MTA!\n";
161 }
162
163 my(%svcnum);
164
165 while (<RCPTHOSTS>) {
166   next if /^(#|$)/;
167   next if $mta{$mta} eq 'sendmail' && /^\s*$/; #blank lines
168   /^\.?([\w\-\.]+)$/
169     #or do { warn "Strange rcpthosts/sendmail.cw line: $_"; next; };
170     or die "Strange rcpthosts/sendmail.cw line: $_";
171   my $domain = $1;
172   my($svc_domain);
173   unless ( $svc_domain = qsearchs('svc_domain', {'domain'=>$domain} ) ) {
174     $svc_domain = new FS::svc_domain ({
175       'domain'  => $domain,
176       'svcpart' => $domain_svcpart,
177       'action'  => 'N',
178     });
179     my $error = $svc_domain->insert;
180     #warn $error if $error;
181     die $error if $error;
182   }
183   $svcnum{$domain}=$svc_domain->svcnum;
184 }
185 close RCPTHOSTS; 
186
187 #these two loops have enough similar parts they should probably be merged
188 if ( $mta{$mta} eq "qmail" ) {
189
190   open(VD_FIX,">$spooldir/virtualdomains.FIX");
191   print VD_FIX "#!/usr/bin/perl\n";
192
193   open(VIRTUALDOMAINS,"<$spooldir/virtualdomains.import")
194     or die "Can't open $spooldir/virtualdomains.import: $!";
195   while (<VIRTUALDOMAINS>) {
196     next if /^#/;
197     /^\.?([\w\-\.]+):(\w+)(\-([\w\-\.]+))?$/
198       #or do { warn "Strange virtualdomains line: $_"; next; };
199       or die "Strange virtualdomains line: $_";
200     my($domain,$username,$dash_ext,$extension)=($1,$2,$3,$4);
201     $dash_ext ||= '';
202     $extension ||= '';
203     my($svc_acct)=qsearchs('svc_acct',{'username'=>$username});
204     unless ( $svc_acct ) {
205       #warn "Unknown user $username in virtualdomains; skipping\n";
206       #die "Unknown user $username in virtualdomains; skipping\n";
207       next;
208     }
209     if ( $domain ne $extension ) {
210       #warn "virtualdomains line $domain:$username$dash_ext changed to $domain:$username-$domain\n";
211       my($dir)=$svc_acct->dir;
212       my($qdomain)=$domain;
213       $qdomain =~ s/\./:/g; #see manpage for 'dot-qmail': EXTENSION ADDRESSES
214       #example to move .qmail files for virtual domains to their new location 
215       #dry run
216       #issh("root\@$shellmachine",'perl -e \'foreach $a (<'. $dir. '/.qmail'. $dash_ext. '-*>) { $old=$a; $a =~ s/\\.qmail'. $dash_ext. '\\-/\\.qmail\\-'. $qdomain. '\\-/; print " $old -> $a\n"; }\'');
217       #the real thing
218       #issh("root\@$shellmachine",'perl -e \'foreach $a (<'. $dir. '/.qmail'. $dash_ext. '-*>) { $old=$a; $a =~ s/\\.qmail'. $dash_ext. '\\-/\\.qmail\\-'. $qdomain. '\\-/; rename $old, $a; }\'');
219       print VD_FIX <<END;
220 foreach \$file (<$dir/.qmail$dash_ext-*>) {
221   \$old = \$file;
222   \$file =~ s/\.qmail$dash_ext\-/\.qmail\-$qdomain\-/;
223   rename \$old, \$file;
224 }
225 END
226     }
227
228     unless ( exists $svcnum{$domain} ) {
229       my($svc_domain) = new FS::svc_domain ({
230         'domain'  => $domain,
231         'svcpart' => $domain_svcpart,
232         'action'  => 'N',
233       });
234       my $error = $svc_domain->insert;
235       #warn $error if $error;
236       die $error if $error;
237       $svcnum{$domain}=$svc_domain->svcnum;
238     }
239
240     my($svc_acct_sm)=new FS::svc_acct_sm ({
241       'domsvc'  => $svcnum{$domain},
242       'domuid'  => $svc_acct->uid,
243       'domuser' => '*',
244       'svcpart' => $mailalias_svcpart,
245     });
246     my($error)='';
247     $error=$svc_acct_sm->insert;
248     #warn $error if $error;
249     die $error, ", domain $domain" if $error;
250   }
251   close VIRTUALDOMAINS;
252   close VD_FIX;
253
254 } elsif ( $mta{$mta} eq "sendmail" ) {
255
256   open(VIRTUSERTABLE,"<$spooldir/virtusertable.import")
257     or die "Can't open $spooldir/virtusertable.import: $!";
258   while (<VIRTUSERTABLE>) {
259     next if /^#/; #comments?
260     next if /^\s*$/; #blank lines
261     /^([\w\-\.]+)?\@([\w\-\.]+)\t+([\w\-\.]+)$/
262       #or do { warn "Strange virtusertable line: $_"; next; };
263       or die "Strange virtusertable line: $_";
264     my($domuser,$domain,$username)=($1,$2,$3);
265     my($svc_acct)=qsearchs('svc_acct',{'username'=>$username});
266     unless ( $svc_acct ) {
267       #warn "Unknown user $username in virtusertable";
268       die "Unknown user $username in virtusertable";
269       next;
270     }
271     my($svc_acct_sm)=new FS::svc_acct_sm ({
272       'domsvc'  => $svcnum{$domain},
273       'domuid'  => $svc_acct->uid,
274       'domuser' => $domuser || '*',
275       'svcpart' => $mailalias_svcpart,
276     });
277     my($error)='';
278     $error=$svc_acct_sm->insert;
279     #warn $error if $error;
280     die $error if $error;
281   }
282   close VIRTUSERTABLE;
283
284 } else {
285   die "Unknown MTA!\n";
286 }
287
288 #open(RECIPIENTMAP,"<$spooldir/recipientmap.import");
289 #close RECIPIENTMAP;
290
291 print "\n\n", <<END if $mta{$mta} eq "qmail";
292 Don\'t forget to run $spooldir/virtualdomains.FIX before using
293 $spooldir/virtualdomains !
294 END
295
296 #
297
298 sub usage {
299   die "Usage:\n\n  svc_acct_sm.import user\n";
300 }
301