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