upgrade docs
[freeside.git] / bin / svc_acct_sm.import
1 #!/usr/bin/perl -Tw
2 #
3 # $Id: svc_acct_sm.import,v 1.10 2001-08-21 02:43:18 ivan Exp $
4
5 use strict;
6 use vars qw(%d_part_svc %m_part_svc);
7 use Term::Query qw(query);
8 use Net::SCP qw(iscp);
9 use FS::UID qw(adminsuidsetup datasrc);
10 use FS::Record qw(qsearch qsearchs);
11 use FS::svc_acct_sm;
12 use FS::svc_domain;
13 use FS::svc_acct;
14 use FS::part_svc;
15
16 my $user = shift or die &usage;
17 adminsuidsetup $user;
18
19 my($spooldir)="/usr/local/etc/freeside/export.". datasrc;
20
21 my(%mta) = (
22   1 => "qmail",
23   2 => "sendmail",
24 );
25
26 ###
27
28 %d_part_svc =
29   map { $_->svcpart, $_ } qsearch('part_svc',{'svcdb'=>'svc_domain'});
30 %m_part_svc =
31   map { $_->svcpart, $_ } qsearch('part_svc',{'svcdb'=>'svc_acct_sm'});
32
33 die "No services with svcdb svc_domain!\n" unless %d_part_svc;
34 die "No services with svcdb svc_svc_acct_sm!\n" unless %m_part_svc;
35
36 print "\n\n", 
37       ( join "\n", map "$_: ".$d_part_svc{$_}->svc, sort keys %d_part_svc ),
38       "\n\n";
39 $^W=0; #Term::Query isn't -w-safe
40 my $domain_svcpart = 
41   query "Enter part number for domains: ", 'irk', [ keys %d_part_svc ];
42 $^W=1;
43
44 print "\n\n", 
45       ( join "\n", map "$_: ".$m_part_svc{$_}->svc, sort keys %m_part_svc ),
46       "\n\n";
47 $^W=0; #Term::Query isn't -w-safe
48 my $mailalias_svcpart = 
49   query "Enter part number for mail aliases: ", 'irk', [ keys %m_part_svc ];
50 $^W=1;
51
52 print "\n\n", <<END;
53 Select your MTA from the following list.
54 END
55 print join "\n", map "$_: $mta{$_}", sort keys %mta;
56 print "\n\n";
57 $^W=0; #Term::Query isn't -w-safe
58 my $mta = query ":", 'irk', [ keys %mta ];
59 $^W=1;
60
61 if ( $mta{$mta} eq "qmail" ) {
62
63   print "\n\n", <<END;
64 Enter the location and name of your qmail control directory, for example
65 "mail.isp.com:/var/qmail/control"
66 END
67   my($control)=&getvalue(":");
68   iscp("root\@$control/rcpthosts","$spooldir/rcpthosts.import");
69 #  iscp("root\@$control/recipientmap","$spooldir/recipientmap.import");
70   iscp("root\@$control/virtualdomains","$spooldir/virtualdomains.import");
71
72 #  print "\n\n", <<END;
73 #Enter the name of the machine with your user .qmail files, for example
74 #"mail.isp.com"
75 #END
76 #  print ":";
77 #  my($shellmachine)=&getvalue;
78
79 } elsif ( $mta{$mta} eq "sendmail" ) {
80
81   print "\n\n", <<END;
82 Enter the location and name of your sendmail virtual user table, for example
83 "mail.isp.com:/etc/virtusertable"
84 END
85   my($virtusertable)=&getvalue(":");
86   iscp("root\@$virtusertable","$spooldir/virtusertable.import");
87
88   print "\n\n", <<END;
89 Enter the location and name of your sendmail.cw file, for example
90 "mail.isp.com:/etc/sendmail.cw"
91 END
92   my($sendmail_cw)=&getvalue(":");
93   iscp("root\@$sendmail_cw","$spooldir/sendmail.cw.import");
94
95 } else {
96   die "Unknown MTA!\n";
97 }
98
99 sub getvalue {
100   my $prompt = shift;
101   $^W=0; #Term::Query isn't -w-safe
102   my $data = query $prompt, '';
103   $^W=1;
104   $data;
105 }
106
107 print "\n\n";
108
109 ###
110
111 $FS::svc_domain::whois_hack=1;
112 $FS::svc_acct_sm::nossh_hack=1;
113
114 if ( $mta{$mta} eq "qmail" ) {
115   open(RCPTHOSTS,"<$spooldir/rcpthosts.import")
116     or die "Can't open $spooldir/rcpthosts.import: $!";
117 } elsif ( $mta{$mta} eq "sendmail" ) {
118   open(RCPTHOSTS,"<$spooldir/sendmail.cw.import")
119     or die "Can't open $spooldir/sendmail.cw.import: $!";
120 } else {
121   die "Unknown MTA!\n";
122 }
123
124 my(%svcnum);
125
126 while (<RCPTHOSTS>) {
127   next if /^(#|$)/;
128   next if $mta{$mta} eq 'sendmail' && /^\s*$/; #blank lines
129   /^\.?([\w\-\.]+)$/
130     #or do { warn "Strange rcpthosts/sendmail.cw line: $_"; next; };
131     or die "Strange rcpthosts/sendmail.cw line: $_";
132   my $domain = $1;
133   my($svc_domain);
134   unless ( $svc_domain = qsearchs('svc_domain', {'domain'=>$domain} ) ) {
135     $svc_domain = new FS::svc_domain ({
136       'domain'  => $domain,
137       'svcpart' => $domain_svcpart,
138       'action'  => 'N',
139     });
140     my $error = $svc_domain->insert;
141     #warn $error if $error;
142     die $error if $error;
143   }
144   $svcnum{$domain}=$svc_domain->svcnum;
145 }
146 close RCPTHOSTS; 
147
148 #these two loops have enough similar parts they should probably be merged
149 if ( $mta{$mta} eq "qmail" ) {
150
151   open(VD_FIX,">$spooldir/virtualdomains.FIX");
152   print VD_FIX "#!/usr/bin/perl\n";
153
154   open(VIRTUALDOMAINS,"<$spooldir/virtualdomains.import")
155     or die "Can't open $spooldir/virtualdomains.import: $!";
156   while (<VIRTUALDOMAINS>) {
157     next if /^#/;
158     /^\.?([\w\-\.]+):(\w+)(\-([\w\-\.]+))?$/
159       #or do { warn "Strange virtualdomains line: $_"; next; };
160       or die "Strange virtualdomains line: $_";
161     my($domain,$username,$dash_ext,$extension)=($1,$2,$3,$4);
162     $dash_ext ||= '';
163     $extension ||= '';
164     my($svc_acct)=qsearchs('svc_acct',{'username'=>$username});
165     unless ( $svc_acct ) {
166       #warn "Unknown user $username in virtualdomains; skipping\n";
167       #die "Unknown user $username in virtualdomains; skipping\n";
168       next;
169     }
170     if ( $domain ne $extension ) {
171       #warn "virtualdomains line $domain:$username$dash_ext changed to $domain:$username-$domain\n";
172       my($dir)=$svc_acct->dir;
173       my($qdomain)=$domain;
174       $qdomain =~ s/\./:/g; #see manpage for 'dot-qmail': EXTENSION ADDRESSES
175       #example to move .qmail files for virtual domains to their new location 
176       #dry run
177       #issh("root\@$shellmachine",'perl -e \'foreach $a (<'. $dir. '/.qmail'. $dash_ext. '-*>) { $old=$a; $a =~ s/\\.qmail'. $dash_ext. '\\-/\\.qmail\\-'. $qdomain. '\\-/; print " $old -> $a\n"; }\'');
178       #the real thing
179       #issh("root\@$shellmachine",'perl -e \'foreach $a (<'. $dir. '/.qmail'. $dash_ext. '-*>) { $old=$a; $a =~ s/\\.qmail'. $dash_ext. '\\-/\\.qmail\\-'. $qdomain. '\\-/; rename $old, $a; }\'');
180       print VD_FIX <<END;
181 foreach \$file (<$dir/.qmail$dash_ext-*>) {
182   \$old = \$file;
183   \$file =~ s/\.qmail$dash_ext\-/\.qmail\-$qdomain\-/;
184   rename \$old, \$file;
185 }
186 END
187     }
188
189     unless ( exists $svcnum{$domain} ) {
190       my($svc_domain) = new FS::svc_domain ({
191         'domain'  => $domain,
192         'svcpart' => $domain_svcpart,
193         'action'  => 'N',
194       });
195       my $error = $svc_domain->insert;
196       #warn $error if $error;
197       die $error if $error;
198       $svcnum{$domain}=$svc_domain->svcnum;
199     }
200
201     my($svc_acct_sm)=new FS::svc_acct_sm ({
202       'domsvc'  => $svcnum{$domain},
203       'domuid'  => $svc_acct->uid,
204       'domuser' => '*',
205       'svcpart' => $mailalias_svcpart,
206     });
207     my($error)='';
208     $error=$svc_acct_sm->insert;
209     #warn $error if $error;
210     die $error, ", domain $domain" if $error;
211   }
212   close VIRTUALDOMAINS;
213   close VD_FIX;
214
215 } elsif ( $mta{$mta} eq "sendmail" ) {
216
217   open(VIRTUSERTABLE,"<$spooldir/virtusertable.import")
218     or die "Can't open $spooldir/virtusertable.import: $!";
219   while (<VIRTUSERTABLE>) {
220     next if /^#/; #comments?
221     next if /^\s*$/; #blank lines
222     /^([\w\-\.]+)?\@([\w\-\.]+)\t+([\w\-\.]+)$/
223       #or do { warn "Strange virtusertable line: $_"; next; };
224       or die "Strange virtusertable line: $_";
225     my($domuser,$domain,$username)=($1,$2,$3);
226     my($svc_acct)=qsearchs('svc_acct',{'username'=>$username});
227     unless ( $svc_acct ) {
228       #warn "Unknown user $username in virtusertable";
229       die "Unknown user $username in virtusertable";
230       next;
231     }
232     my($svc_acct_sm)=new FS::svc_acct_sm ({
233       'domsvc'  => $svcnum{$domain},
234       'domuid'  => $svc_acct->uid,
235       'domuser' => $domuser || '*',
236       'svcpart' => $mailalias_svcpart,
237     });
238     my($error)='';
239     $error=$svc_acct_sm->insert;
240     #warn $error if $error;
241     die $error if $error;
242   }
243   close VIRTUSERTABLE;
244
245 } else {
246   die "Unknown MTA!\n";
247 }
248
249 #open(RECIPIENTMAP,"<$spooldir/recipientmap.import");
250 #close RECIPIENTMAP;
251
252 print "\n\n", <<END if $mta{$mta} eq "qmail";
253 Don\'t forget to run $spooldir/virtualdomains.FIX before using
254 $spooldir/virtualdomains !
255 END
256
257 #
258
259 sub usage {
260   die "Usage:\n\n  svc_acct_sm.import user\n";
261 }
262