3 # $Id: svc_acct_sm.import,v 1.9 2001-04-22 01:56:15 ivan Exp $
5 # ivan@sisd.com 98-mar-9
7 # generalized svcparts ivan@sisd.com 98-mar-23
9 # You really need to enable ssh into a shell machine as this needs to rename
10 # .qmail-extension files.
12 # now an interactive script ivan@sisd.com 98-jun-30
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
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)
22 # Revision 1.8 2000/12/03 15:14:00 ivan
23 # bugfixes from Jeff Finucane <jeff@cmh.net>, thanks!
25 # Revision 1.7 2000/06/29 10:51:52 ivan
28 # Revision 1.6 2000/06/29 10:48:25 ivan
29 # make svc_acct_sm skip blank lines in sendmail import
31 # Revision 1.5 2000/02/03 05:16:52 ivan
32 # beginning of DNS and Apache support
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
37 # Revision 1.3 1999/03/24 00:51:55 ivan
38 # die if no relevant services... cvspain
40 # Revision 1.2 1998/12/10 07:23:18 ivan
41 # use FS::Conf, need user (for datasrc)
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);
55 my $user = shift or die &usage;
58 my($spooldir)="/usr/local/etc/freeside/export.". datasrc;
68 map { $_->svcpart, $_ } qsearch('part_svc',{'svcdb'=>'svc_domain'});
70 map { $_->svcpart, $_ } qsearch('part_svc',{'svcdb'=>'svc_acct_sm'});
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;
76 ( join "\n", map "$_: ".$d_part_svc{$_}->svc, sort keys %d_part_svc ),
78 $^W=0; #Term::Query isn't -w-safe
80 query "Enter part number for domains: ", 'irk', [ keys %d_part_svc ];
84 ( join "\n", map "$_: ".$m_part_svc{$_}->svc, sort keys %m_part_svc ),
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 ];
92 Select your MTA from the following list.
94 print join "\n", map "$_: $mta{$_}", sort keys %mta;
96 $^W=0; #Term::Query isn't -w-safe
97 my $mta = query ":", 'irk', [ keys %mta ];
100 if ( $mta{$mta} eq "qmail" ) {
103 Enter the location and name of your qmail control directory, for example
104 "mail.isp.com:/var/qmail/control"
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");
111 # print "\n\n", <<END;
112 #Enter the name of the machine with your user .qmail files, for example
116 # my($shellmachine)=&getvalue;
118 } elsif ( $mta{$mta} eq "sendmail" ) {
121 Enter the location and name of your sendmail virtual user table, for example
122 "mail.isp.com:/etc/virtusertable"
124 my($virtusertable)=&getvalue(":");
125 iscp("root\@$virtusertable","$spooldir/virtusertable.import");
128 Enter the location and name of your sendmail.cw file, for example
129 "mail.isp.com:/etc/sendmail.cw"
131 my($sendmail_cw)=&getvalue(":");
132 iscp("root\@$sendmail_cw","$spooldir/sendmail.cw.import");
135 die "Unknown MTA!\n";
140 $^W=0; #Term::Query isn't -w-safe
141 my $data = query $prompt, '';
150 $FS::svc_domain::whois_hack=1;
151 $FS::svc_acct_sm::nossh_hack=1;
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: $!";
160 die "Unknown MTA!\n";
165 while (<RCPTHOSTS>) {
167 next if $mta{$mta} eq 'sendmail' && /^\s*$/; #blank lines
169 #or do { warn "Strange rcpthosts/sendmail.cw line: $_"; next; };
170 or die "Strange rcpthosts/sendmail.cw line: $_";
173 unless ( $svc_domain = qsearchs('svc_domain', {'domain'=>$domain} ) ) {
174 $svc_domain = new FS::svc_domain ({
176 'svcpart' => $domain_svcpart,
179 my $error = $svc_domain->insert;
180 #warn $error if $error;
181 die $error if $error;
183 $svcnum{$domain}=$svc_domain->svcnum;
187 #these two loops have enough similar parts they should probably be merged
188 if ( $mta{$mta} eq "qmail" ) {
190 open(VD_FIX,">$spooldir/virtualdomains.FIX");
191 print VD_FIX "#!/usr/bin/perl\n";
193 open(VIRTUALDOMAINS,"<$spooldir/virtualdomains.import")
194 or die "Can't open $spooldir/virtualdomains.import: $!";
195 while (<VIRTUALDOMAINS>) {
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);
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";
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
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"; }\'');
218 #issh("root\@$shellmachine",'perl -e \'foreach $a (<'. $dir. '/.qmail'. $dash_ext. '-*>) { $old=$a; $a =~ s/\\.qmail'. $dash_ext. '\\-/\\.qmail\\-'. $qdomain. '\\-/; rename $old, $a; }\'');
220 foreach \$file (<$dir/.qmail$dash_ext-*>) {
222 \$file =~ s/\.qmail$dash_ext\-/\.qmail\-$qdomain\-/;
223 rename \$old, \$file;
228 unless ( exists $svcnum{$domain} ) {
229 my($svc_domain) = new FS::svc_domain ({
231 'svcpart' => $domain_svcpart,
234 my $error = $svc_domain->insert;
235 #warn $error if $error;
236 die $error if $error;
237 $svcnum{$domain}=$svc_domain->svcnum;
240 my($svc_acct_sm)=new FS::svc_acct_sm ({
241 'domsvc' => $svcnum{$domain},
242 'domuid' => $svc_acct->uid,
244 'svcpart' => $mailalias_svcpart,
247 $error=$svc_acct_sm->insert;
248 #warn $error if $error;
249 die $error, ", domain $domain" if $error;
251 close VIRTUALDOMAINS;
254 } elsif ( $mta{$mta} eq "sendmail" ) {
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";
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,
278 $error=$svc_acct_sm->insert;
279 #warn $error if $error;
280 die $error if $error;
285 die "Unknown MTA!\n";
288 #open(RECIPIENTMAP,"<$spooldir/recipientmap.import");
291 print "\n\n", <<END if $mta{$mta} eq "qmail";
292 Don\'t forget to run $spooldir/virtualdomains.FIX before using
293 $spooldir/virtualdomains !
299 die "Usage:\n\n svc_acct_sm.import user\n";