1 |
#!/usr/bin/perl -w |
2 |
|
3 |
# 2001-03-05 Dobrica Pavlinusic <dpavlin@rot13.org> |
4 |
# |
5 |
# reads e-mail addresses from stdin and dump format for .htusers on stdout |
6 |
# which will use auth_pop3 for user authentification |
7 |
# |
8 |
# This tool is good for batch import and it will do it's best to resolve |
9 |
# all sendmail related output from expn. However, it's not tested on |
10 |
# other mailers. |
11 |
# |
12 |
# usage: |
13 |
# |
14 |
# cat emails.txt | email2htusers.pl [remote.smtp.server] [smtp_port] >> .htusers |
15 |
# |
16 |
|
17 |
use strict; |
18 |
use Socket; |
19 |
|
20 |
my $debug=0; |
21 |
|
22 |
my $reuse_conn=0; # if you set this to 1, it will try to do all expn |
23 |
# over single tcp connection. It nice to smtp server, |
24 |
# but sendmail slows down after 10 expns or so... |
25 |
|
26 |
my ($remote,$port, $iaddr, $paddr, $proto, $line); |
27 |
|
28 |
$remote = shift || 'localhost'; |
29 |
$port = shift || 25; |
30 |
|
31 |
print "# using remote $remote port $port\n" if ($debug); |
32 |
|
33 |
sub open_conn { |
34 |
if ($port =~ /\D/) { $port = getservbyname($port, 'tcp') } |
35 |
die "No port" unless $port; |
36 |
$iaddr = inet_aton($remote) || die "no host: $remote"; |
37 |
$paddr = sockaddr_in($port, $iaddr); |
38 |
|
39 |
$proto = getprotobyname('tcp'); |
40 |
socket(SOCK, PF_INET, SOCK_STREAM, $proto) || die "socket: $!"; |
41 |
connect(SOCK, $paddr) || die "connect: $!"; |
42 |
select(SOCK); $|=1; |
43 |
select(STDOUT); $|=1; |
44 |
|
45 |
my $hello=<SOCK>; # hello |
46 |
|
47 |
} |
48 |
|
49 |
sub close_conn { |
50 |
close (SOCK) || die "close: $!"; |
51 |
} |
52 |
|
53 |
my %tested; |
54 |
|
55 |
sub try_expn { |
56 |
my $login=$_[0]; |
57 |
if (! defined($tested{$login})) { |
58 |
$tested{$login}++; |
59 |
print SOCK "expn $login\n"; |
60 |
my $response=<SOCK>; |
61 |
while ($response =~ /^2\d\d-(.+)/) { |
62 |
my $rest=$1; |
63 |
print "# $response\n" if ($debug); |
64 |
if ($rest=~/[\d\. ]+([^<]+) <\\*(\w+)\@($remote[^>]*)>/) { |
65 |
print "# $2:$1:auth_pop3:$2\@$3\n"; |
66 |
} |
67 |
$response=<SOCK>; |
68 |
}; |
69 |
print "# $response\n" if ($debug); |
70 |
if ($response =~ /^2\d\d (.+)/) { |
71 |
my $rest=$1; |
72 |
if ($rest=~/[\d\. ]+([^<]+) <\\*(\w+)\@($remote[^>]*)>/) { |
73 |
print "$2:$1:auth_pop3:$2\@$3\n"; |
74 |
} |
75 |
} else { |
76 |
print "# can't add e-mail address $login\n"; |
77 |
} |
78 |
return 0; |
79 |
} |
80 |
} |
81 |
|
82 |
open_conn if ($reuse_conn); |
83 |
|
84 |
while(<STDIN>) { |
85 |
chomp; |
86 |
next if (/^#/ || /^$/); |
87 |
open_conn if (! $reuse_conn); |
88 |
try_expn($_); |
89 |
close_conn if (! $reuse_conn); |
90 |
} |
91 |
|
92 |
close_conn if ($reuse_conn); |
93 |
exit; |