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