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