3 # $Id: svc_acct_sm.import,v 1.4 1999-03-25 08:42:20 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.4 1999-03-25 08:42:20 ivan
20 # import stuff uses Term::Query and spits out (some kinds of) nonsensical input
22 # Revision 1.3 1999/03/24 00:51:55 ivan
23 # die if no relevant services... cvspain
25 # Revision 1.2 1998/12/10 07:23:18 ivan
26 # use FS::Conf, need user (for datasrc)
30 use vars qw(%d_part_svc %m_part_svc);
31 use Term::Query qw(query);
33 use FS::UID qw(adminsuidsetup datasrc);
34 use FS::Record qw(qsearch qsearchs);
40 my $user = shift or die &usage;
43 my($spooldir)="/usr/local/etc/freeside/export.". datasrc;
53 map { $_->svcpart, $_ } qsearch('part_svc',{'svcdb'=>'svc_domain'});
55 map { $_->svcpart, $_ } qsearch('part_svc',{'svcdb'=>'svc_acct_sm'});
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;
61 ( join "\n", map "$_: ".$d_part_svc{$_}->svc, sort keys %d_part_svc ),
63 $^W=0; #Term::Query isn't -w-safe
65 query "Enter part number for domains: ", 'irk', [ keys %d_part_svc ];
69 ( join "\n", map "$_: ".$m_part_svc{$_}->svc, sort keys %m_part_svc ),
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 ];
77 Select your MTA from the following list.
79 print join "\n", map "$_: $mta{$_}", sort keys %mta;
81 $^W=0; #Term::Query isn't -w-safe
82 my $mta = query ":", 'irk', [ keys %mta ];
85 if ( $mta{$mta} eq "qmail" ) {
88 Enter the location and name of your qmail control directory, for example
89 "mail.isp.com:/var/qmail/control"
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");
96 # print "\n\n", <<END;
97 #Enter the name of the machine with your user .qmail files, for example
101 # my($shellmachine)=&getvalue;
103 } elsif ( $mta{$mta} eq "sendmail" ) {
106 Enter the location and name of your sendmail virtual user table, for example
107 "mail.isp.com:/etc/virtusertable"
109 my($virtusertable)=&getvalue(":");
110 iscp("root\@$virtusertable","$spooldir/virtusertable.import");
113 Enter the location and name of your sendmail.cw file, for example
114 "mail.isp.com:/etc/sendmail.cw"
116 my($sendmail_cw)=&getvalue(":");
117 iscp("root\@$sendmail_cw","$spooldir/sendmail.cw.import");
120 die "Unknown MTA!\n";
125 $^W=0; #Term::Query isn't -w-safe
134 $FS::svc_domain::whois_hack=1;
135 $FS::svc_acct_sm::nossh_hack=1;
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: $!";
144 die "Unknown MTA!\n";
149 while (<RCPTHOSTS>) {
152 #or do { warn "Strange rcpthosts/sendmail.cw line: $_"; next; };
153 or die "Strange rcpthosts/sendmail.cw line: $_";
156 unless ( $svc_domain = qsearchs('svc_domain', {'domain'=>$domain} ) ) {
157 $svc_domain = new FS::svc_domain ({
159 'svcpart' => $domain_svcpart,
162 my $error = $svc_domain->insert;
163 #warn $error if $error;
164 die $error if $error;
166 $svcnum{$domain}=$svc_domain->svcnum;
170 #these two loops have enough similar parts they should probably be merged
171 if ( $mta{$mta} eq "qmail" ) {
173 open(VD_FIX,">$spooldir/virtualdomains.FIX");
174 print VD_FIX "#!/usr/bin/perl\n";
176 open(VIRTUALDOMAINS,"<$spooldir/virtualdomains.import")
177 or die "Can't open $spooldir/virtualdomains.import: $!";
178 while (<VIRTUALDOMAINS>) {
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);
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";
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
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"; }\'');
201 #issh("root\@$shellmachine",'perl -e \'foreach $a (<'. $dir. '/.qmail'. $dash_ext. '-*>) { $old=$a; $a =~ s/\\.qmail'. $dash_ext. '\\-/\\.qmail\\-'. $qdomain. '\\-/; rename $old, $a; }\'');
203 foreach \$file (<$dir/.qmail$dash_ext-*>) {
205 \$file =~ s/\.qmail$dash_ext\-/\.qmail\-$qdomain\-/;
206 rename \$old, \$file;
211 unless ( exists $svcnum{$domain} ) {
212 my($svc_domain) = new FS::svc_domain ({
214 'svcpart' => $domain_svcpart,
217 my $error = $svc_domain->insert;
218 #warn $error if $error;
219 die $error if $error;
220 $svcnum{$domain}=$svc_domain->svcnum;
223 my($svc_acct_sm)=new FS::svc_acct_sm ({
224 'domsvc' => $svcnum{$domain},
225 'domuid' => $svc_acct->uid,
227 'svcpart' => $mailalias_svcpart,
230 $error=$svc_acct_sm->insert;
231 #warn $error if $error;
232 die $error, ", domain $domain" if $error;
234 close VIRTUALDOMAINS;
237 } elsif ( $mta{$mta} eq "sendmail" ) {
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";
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,
260 $error=$svc_acct_sm->insert;
261 #warn $error if $error;
262 die $error if $error;
267 die "Unknown MTA!\n";
270 #open(RECIPIENTMAP,"<$spooldir/recipientmap.import");
273 print "\n\n", <<END if $mta{$mta} eq "qmail";
274 Don\'t forget to run $spooldir/virtualdomains.FIX before using
275 $spooldir/virtualdomains !
281 die "Usage:\n\n svc_acct_sm.export user\n";