summaryrefslogtreecommitdiff
path: root/bin/svc_acct_sm.import
blob: b668405f5f94b6c7c7b2bdb95bb6128bd8dee5a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#!/usr/bin/perl -Tw
#
# $Id: svc_acct_sm.import,v 1.10 2001-08-21 02:43:18 ivan Exp $

use strict;
use vars qw(%d_part_svc %m_part_svc);
use Term::Query qw(query);
use Net::SCP qw(iscp);
use FS::UID qw(adminsuidsetup datasrc);
use FS::Record qw(qsearch qsearchs);
use FS::svc_acct_sm;
use FS::svc_domain;
use FS::svc_acct;
use FS::part_svc;

my $user = shift or die &usage;
adminsuidsetup $user;

my($spooldir)="/usr/local/etc/freeside/export.". datasrc;

my(%mta) = (
  1 => "qmail",
  2 => "sendmail",
);

###

%d_part_svc =
  map { $_->svcpart, $_ } qsearch('part_svc',{'svcdb'=>'svc_domain'});
%m_part_svc =
  map { $_->svcpart, $_ } qsearch('part_svc',{'svcdb'=>'svc_acct_sm'});

die "No services with svcdb svc_domain!\n" unless %d_part_svc;
die "No services with svcdb svc_svc_acct_sm!\n" unless %m_part_svc;

print "\n\n", 
      ( join "\n", map "$_: ".$d_part_svc{$_}->svc, sort keys %d_part_svc ),
      "\n\n";
$^W=0; #Term::Query isn't -w-safe
my $domain_svcpart = 
  query "Enter part number for domains: ", 'irk', [ keys %d_part_svc ];
$^W=1;

print "\n\n", 
      ( join "\n", map "$_: ".$m_part_svc{$_}->svc, sort keys %m_part_svc ),
      "\n\n";
$^W=0; #Term::Query isn't -w-safe
my $mailalias_svcpart = 
  query "Enter part number for mail aliases: ", 'irk', [ keys %m_part_svc ];
$^W=1;

print "\n\n", <<END;
Select your MTA from the following list.
END
print join "\n", map "$_: $mta{$_}", sort keys %mta;
print "\n\n";
$^W=0; #Term::Query isn't -w-safe
my $mta = query ":", 'irk', [ keys %mta ];
$^W=1;

if ( $mta{$mta} eq "qmail" ) {

  print "\n\n", <<END;
Enter the location and name of your qmail control directory, for example
"mail.isp.com:/var/qmail/control"
END
  my($control)=&getvalue(":");
  iscp("root\@$control/rcpthosts","$spooldir/rcpthosts.import");
#  iscp("root\@$control/recipientmap","$spooldir/recipientmap.import");
  iscp("root\@$control/virtualdomains","$spooldir/virtualdomains.import");

#  print "\n\n", <<END;
#Enter the name of the machine with your user .qmail files, for example
#"mail.isp.com"
#END
#  print ":";
#  my($shellmachine)=&getvalue;

} elsif ( $mta{$mta} eq "sendmail" ) {

  print "\n\n", <<END;
Enter the location and name of your sendmail virtual user table, for example
"mail.isp.com:/etc/virtusertable"
END
  my($virtusertable)=&getvalue(":");
  iscp("root\@$virtusertable","$spooldir/virtusertable.import");

  print "\n\n", <<END;
Enter the location and name of your sendmail.cw file, for example
"mail.isp.com:/etc/sendmail.cw"
END
  my($sendmail_cw)=&getvalue(":");
  iscp("root\@$sendmail_cw","$spooldir/sendmail.cw.import");

} else {
  die "Unknown MTA!\n";
}

sub getvalue {
  my $prompt = shift;
  $^W=0; #Term::Query isn't -w-safe
  my $data = query $prompt, '';
  $^W=1;
  $data;
}

print "\n\n";

###

$FS::svc_domain::whois_hack=1;
$FS::svc_acct_sm::nossh_hack=1;

if ( $mta{$mta} eq "qmail" ) {
  open(RCPTHOSTS,"<$spooldir/rcpthosts.import")
    or die "Can't open $spooldir/rcpthosts.import: $!";
} elsif ( $mta{$mta} eq "sendmail" ) {
  open(RCPTHOSTS,"<$spooldir/sendmail.cw.import")
    or die "Can't open $spooldir/sendmail.cw.import: $!";
} else {
  die "Unknown MTA!\n";
}

my(%svcnum);

while (<RCPTHOSTS>) {
  next if /^(#|$)/;
  next if $mta{$mta} eq 'sendmail' && /^\s*$/; #blank lines
  /^\.?([\w\-\.]+)$/
    #or do { warn "Strange rcpthosts/sendmail.cw line: $_"; next; };
    or die "Strange rcpthosts/sendmail.cw line: $_";
  my $domain = $1;
  my($svc_domain);
  unless ( $svc_domain = qsearchs('svc_domain', {'domain'=>$domain} ) ) {
    $svc_domain = new FS::svc_domain ({
      'domain'  => $domain,
      'svcpart' => $domain_svcpart,
      'action'  => 'N',
    });
    my $error = $svc_domain->insert;
    #warn $error if $error;
    die $error if $error;
  }
  $svcnum{$domain}=$svc_domain->svcnum;
}
close RCPTHOSTS; 

#these two loops have enough similar parts they should probably be merged
if ( $mta{$mta} eq "qmail" ) {

  open(VD_FIX,">$spooldir/virtualdomains.FIX");
  print VD_FIX "#!/usr/bin/perl\n";

  open(VIRTUALDOMAINS,"<$spooldir/virtualdomains.import")
    or die "Can't open $spooldir/virtualdomains.import: $!";
  while (<VIRTUALDOMAINS>) {
    next if /^#/;
    /^\.?([\w\-\.]+):(\w+)(\-([\w\-\.]+))?$/
      #or do { warn "Strange virtualdomains line: $_"; next; };
      or die "Strange virtualdomains line: $_";
    my($domain,$username,$dash_ext,$extension)=($1,$2,$3,$4);
    $dash_ext ||= '';
    $extension ||= '';
    my($svc_acct)=qsearchs('svc_acct',{'username'=>$username});
    unless ( $svc_acct ) {
      #warn "Unknown user $username in virtualdomains; skipping\n";
      #die "Unknown user $username in virtualdomains; skipping\n";
      next;
    }
    if ( $domain ne $extension ) {
      #warn "virtualdomains line $domain:$username$dash_ext changed to $domain:$username-$domain\n";
      my($dir)=$svc_acct->dir;
      my($qdomain)=$domain;
      $qdomain =~ s/\./:/g; #see manpage for 'dot-qmail': EXTENSION ADDRESSES
      #example to move .qmail files for virtual domains to their new location 
      #dry run
      #issh("root\@$shellmachine",'perl -e \'foreach $a (<'. $dir. '/.qmail'. $dash_ext. '-*>) { $old=$a; $a =~ s/\\.qmail'. $dash_ext. '\\-/\\.qmail\\-'. $qdomain. '\\-/; print " $old -> $a\n"; }\'');
      #the real thing
      #issh("root\@$shellmachine",'perl -e \'foreach $a (<'. $dir. '/.qmail'. $dash_ext. '-*>) { $old=$a; $a =~ s/\\.qmail'. $dash_ext. '\\-/\\.qmail\\-'. $qdomain. '\\-/; rename $old, $a; }\'');
      print VD_FIX <<END;
foreach \$file (<$dir/.qmail$dash_ext-*>) {
  \$old = \$file;
  \$file =~ s/\.qmail$dash_ext\-/\.qmail\-$qdomain\-/;
  rename \$old, \$file;
}
END
    }

    unless ( exists $svcnum{$domain} ) {
      my($svc_domain) = new FS::svc_domain ({
        'domain'  => $domain,
        'svcpart' => $domain_svcpart,
        'action'  => 'N',
      });
      my $error = $svc_domain->insert;
      #warn $error if $error;
      die $error if $error;
      $svcnum{$domain}=$svc_domain->svcnum;
    }

    my($svc_acct_sm)=new FS::svc_acct_sm ({
      'domsvc'  => $svcnum{$domain},
      'domuid'  => $svc_acct->uid,
      'domuser' => '*',
      'svcpart' => $mailalias_svcpart,
    });
    my($error)='';
    $error=$svc_acct_sm->insert;
    #warn $error if $error;
    die $error, ", domain $domain" if $error;
  }
  close VIRTUALDOMAINS;
  close VD_FIX;

} elsif ( $mta{$mta} eq "sendmail" ) {

  open(VIRTUSERTABLE,"<$spooldir/virtusertable.import")
    or die "Can't open $spooldir/virtusertable.import: $!";
  while (<VIRTUSERTABLE>) {
    next if /^#/; #comments?
    next if /^\s*$/; #blank lines
    /^([\w\-\.]+)?\@([\w\-\.]+)\t+([\w\-\.]+)$/
      #or do { warn "Strange virtusertable line: $_"; next; };
      or die "Strange virtusertable line: $_";
    my($domuser,$domain,$username)=($1,$2,$3);
    my($svc_acct)=qsearchs('svc_acct',{'username'=>$username});
    unless ( $svc_acct ) {
      #warn "Unknown user $username in virtusertable";
      die "Unknown user $username in virtusertable";
      next;
    }
    my($svc_acct_sm)=new FS::svc_acct_sm ({
      'domsvc'  => $svcnum{$domain},
      'domuid'  => $svc_acct->uid,
      'domuser' => $domuser || '*',
      'svcpart' => $mailalias_svcpart,
    });
    my($error)='';
    $error=$svc_acct_sm->insert;
    #warn $error if $error;
    die $error if $error;
  }
  close VIRTUSERTABLE;

} else {
  die "Unknown MTA!\n";
}

#open(RECIPIENTMAP,"<$spooldir/recipientmap.import");
#close RECIPIENTMAP;

print "\n\n", <<END if $mta{$mta} eq "qmail";
Don\'t forget to run $spooldir/virtualdomains.FIX before using
$spooldir/virtualdomains !
END

#

sub usage {
  die "Usage:\n\n  svc_acct_sm.import user\n";
}