3 # $Id: svc_acct_sm.import,v 1.10 2001-08-21 02:43:18 ivan Exp $
6 use vars qw(%d_part_svc %m_part_svc);
7 use Term::Query qw(query);
9 use FS::UID qw(adminsuidsetup datasrc);
10 use FS::Record qw(qsearch qsearchs);
16 my $user = shift or die &usage;
19 my($spooldir)="/usr/local/etc/freeside/export.". datasrc;
29 map { $_->svcpart, $_ } qsearch('part_svc',{'svcdb'=>'svc_domain'});
31 map { $_->svcpart, $_ } qsearch('part_svc',{'svcdb'=>'svc_acct_sm'});
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;
37 ( join "\n", map "$_: ".$d_part_svc{$_}->svc, sort keys %d_part_svc ),
39 $^W=0; #Term::Query isn't -w-safe
41 query "Enter part number for domains: ", 'irk', [ keys %d_part_svc ];
45 ( join "\n", map "$_: ".$m_part_svc{$_}->svc, sort keys %m_part_svc ),
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 ];
53 Select your MTA from the following list.
55 print join "\n", map "$_: $mta{$_}", sort keys %mta;
57 $^W=0; #Term::Query isn't -w-safe
58 my $mta = query ":", 'irk', [ keys %mta ];
61 if ( $mta{$mta} eq "qmail" ) {
64 Enter the location and name of your qmail control directory, for example
65 "mail.isp.com:/var/qmail/control"
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");
72 # print "\n\n", <<END;
73 #Enter the name of the machine with your user .qmail files, for example
77 # my($shellmachine)=&getvalue;
79 } elsif ( $mta{$mta} eq "sendmail" ) {
82 Enter the location and name of your sendmail virtual user table, for example
83 "mail.isp.com:/etc/virtusertable"
85 my($virtusertable)=&getvalue(":");
86 iscp("root\@$virtusertable","$spooldir/virtusertable.import");
89 Enter the location and name of your sendmail.cw file, for example
90 "mail.isp.com:/etc/sendmail.cw"
92 my($sendmail_cw)=&getvalue(":");
93 iscp("root\@$sendmail_cw","$spooldir/sendmail.cw.import");
101 $^W=0; #Term::Query isn't -w-safe
102 my $data = query $prompt, '';
111 $FS::svc_domain::whois_hack=1;
112 $FS::svc_acct_sm::nossh_hack=1;
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: $!";
121 die "Unknown MTA!\n";
126 while (<RCPTHOSTS>) {
128 next if $mta{$mta} eq 'sendmail' && /^\s*$/; #blank lines
130 #or do { warn "Strange rcpthosts/sendmail.cw line: $_"; next; };
131 or die "Strange rcpthosts/sendmail.cw line: $_";
134 unless ( $svc_domain = qsearchs('svc_domain', {'domain'=>$domain} ) ) {
135 $svc_domain = new FS::svc_domain ({
137 'svcpart' => $domain_svcpart,
140 my $error = $svc_domain->insert;
141 #warn $error if $error;
142 die $error if $error;
144 $svcnum{$domain}=$svc_domain->svcnum;
148 #these two loops have enough similar parts they should probably be merged
149 if ( $mta{$mta} eq "qmail" ) {
151 open(VD_FIX,">$spooldir/virtualdomains.FIX");
152 print VD_FIX "#!/usr/bin/perl\n";
154 open(VIRTUALDOMAINS,"<$spooldir/virtualdomains.import")
155 or die "Can't open $spooldir/virtualdomains.import: $!";
156 while (<VIRTUALDOMAINS>) {
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);
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";
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
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"; }\'');
179 #issh("root\@$shellmachine",'perl -e \'foreach $a (<'. $dir. '/.qmail'. $dash_ext. '-*>) { $old=$a; $a =~ s/\\.qmail'. $dash_ext. '\\-/\\.qmail\\-'. $qdomain. '\\-/; rename $old, $a; }\'');
181 foreach \$file (<$dir/.qmail$dash_ext-*>) {
183 \$file =~ s/\.qmail$dash_ext\-/\.qmail\-$qdomain\-/;
184 rename \$old, \$file;
189 unless ( exists $svcnum{$domain} ) {
190 my($svc_domain) = new FS::svc_domain ({
192 'svcpart' => $domain_svcpart,
195 my $error = $svc_domain->insert;
196 #warn $error if $error;
197 die $error if $error;
198 $svcnum{$domain}=$svc_domain->svcnum;
201 my($svc_acct_sm)=new FS::svc_acct_sm ({
202 'domsvc' => $svcnum{$domain},
203 'domuid' => $svc_acct->uid,
205 'svcpart' => $mailalias_svcpart,
208 $error=$svc_acct_sm->insert;
209 #warn $error if $error;
210 die $error, ", domain $domain" if $error;
212 close VIRTUALDOMAINS;
215 } elsif ( $mta{$mta} eq "sendmail" ) {
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";
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,
239 $error=$svc_acct_sm->insert;
240 #warn $error if $error;
241 die $error if $error;
246 die "Unknown MTA!\n";
249 #open(RECIPIENTMAP,"<$spooldir/recipientmap.import");
252 print "\n\n", <<END if $mta{$mta} eq "qmail";
253 Don\'t forget to run $spooldir/virtualdomains.FIX before using
254 $spooldir/virtualdomains !
260 die "Usage:\n\n svc_acct_sm.import user\n";