/usr/bin/expn is in sendmail-base 8.14.4-8+deb8u2.
This file is owned by root:root, with mode 0o755.
The actual contents of the file can be viewed below.
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 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 | #!/usr/bin/perl
'di ';
'ds 00 \\"';
'ig 00 ';
#
# THIS PROGRAM IS ITS OWN MANUAL PAGE. INSTALL IN man & bin.
#
use 5.001;
use IO::Socket;
use Fcntl;
# system requirements:
# must have 'nslookup' and 'hostname' programs.
# $OrigHeader: /home/muir/bin/RCS/expn,v 3.11 1997/09/10 08:14:02 muir Exp muir $
# TODO:
# less magic should apply to command-line addresses
# less magic should apply to local addresses
# add magic to deal with cross-domain cnames
# disconnect & reconnect after 25 commands to the same sendmail 8.8.* host
# Checklist: (hard addresses)
# 250 Kimmo Suominen <"|/usr/local/mh/lib/slocal -user kim"@grendel.tac.nyc.ny.us>
# harry@hofmann.cs.Berkeley.EDU -> harry@tenet (.berkeley.edu) [dead]
# bks@cs.berkeley.edu -> shiva.CS (.berkeley.edu) [dead]
# dan@tc.cornell.edu -> brown@tiberius (.tc.cornell.edu)
#############################################################################
#
# Copyright (c) 1993 David Muir Sharnoff
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# 3. All advertising materials mentioning features or use of this software
# must display the following acknowledgement:
# This product includes software developed by the David Muir Sharnoff.
# 4. The name of David Sharnoff may not be used to endorse or promote products
# derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE DAVID MUIR SHARNOFF ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL DAVID MUIR SHARNOFF BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
# This copyright notice derrived from material copyrighted by the Regents
# of the University of California.
#
# Contributions accepted.
#
#############################################################################
# overall structure:
# in an effort to not trace each address individually, but rather
# ask each server in turn a whole bunch of questions, addresses to
# be expanded are queued up.
#
# This means that all accounting w.r.t. an address must be stored in
# various arrays. Generally these arrays are indexed by the
# string "$addr *** $server" where $addr is the address to be
# expanded "foo" or maybe "foo@bar" and $server is the hostname
# of the SMTP server to contact.
#
# important global variables:
#
# @hosts : list of servers still to be contacted
# $server : name of the current we are currently looking at
# @users = $users{@hosts[0]} : addresses to expand at this server
# $u = $users[0] : the current address being expanded
# $names{"$users[0] *** $server"} : the 'name' associated with the address
# $mxbacktrace{"$users[0] *** $server"} : record of mx expansion
# $mx_secondary{$server} : other mx relays at the same priority
# $domainify_fallback{"$users[0] *** $server"} : alternative names to try
# instead of $server if $server doesn't work
# $temporary_redirect{"$users[0] *** $server"} : when trying alternates,
# temporarily channel all tries along current path
# $giveup{$server} : do not bother expanding addresses at $server
# $verbose : -v
# $watch : -w
# $vw : -v or -w
# $debug : -d
# $valid : -a
# $levels : -1
# $S : the socket connection to $server
$have_nslookup = 1; # we have the nslookup program
$port = 'smtp';
$av0 = $0;
$ENV{'PATH'} .= ":/usr/etc" unless $ENV{'PATH'} =~ m,/usr/etc,;
$ENV{'PATH'} .= ":/usr/ucb" unless $ENV{'PATH'} =~ m,/usr/ucb,;
select(STDERR);
$0 = "$av0 - running hostname";
chop($name = `hostname || uname -n`);
$0 = "$av0 - lookup host FQDN and IP addr";
($hostname,$aliases,$type,$len,$thisaddr) = gethostbyname($name);
$0 = "$av0 - parsing args";
$usage = "Usage: $av0 [-1avwd] user[\@host] [user2[host2] ...]";
for $a (@ARGV) {
die $usage if $a eq "-";
while ($a =~ s/^(-.*)([1avwd])/$1/) {
eval '$'."flag_$2 += 1";
}
next if $a eq "-";
die $usage if $a =~ /^-/;
&expn(&parse($a,$hostname,undef,1));
}
$verbose = $flag_v;
$watch = $flag_w;
$vw = $flag_v + $flag_w;
$debug = $flag_d;
$valid = $flag_a;
$levels = $flag_1;
die $usage unless @hosts;
if ($valid) {
if ($valid == 1) {
$validRequirement = 0.8;
} elsif ($valid == 2) {
$validRequirement = 1.0;
} elsif ($valid == 3) {
$validRequirement = 0.9;
} else {
$validRequirement = (1 - (1/($valid-3)));
print "validRequirement = $validRequirement\n" if $debug;
}
}
HOST:
while (@hosts) {
$server = shift(@hosts);
@users = split(' ',$users{$server});
delete $users{$server};
# is this server already known to be bad?
$0 = "$av0 - looking up $server";
if ($giveup{$server}) {
&giveup('mx domainify',$giveup{$server});
next;
}
# do we already have an mx record for this host?
next HOST if &mxredirect($server,*users);
# look it up, or try for an mx.
$0 = "$av0 - gethostbyname($server)";
($name,$aliases,$type,$len,$thataddr) = gethostbyname($server);
# if we can't get an A record, try for an MX record.
unless($thataddr) {
&mxlookup(1,$server,"$server: could not resolve name",*users);
next HOST;
}
# get a connection, or look for an mx
$0 = "$av0 - socket to $server";
$S = new IO::Socket::INET (
'PeerAddr' => $server,
'PeerPort' => $port,
'Proto' => 'tcp');
if (! $S || ($debug == 10 && $server =~ /relay\d.UU.NET$/i)) {
$0 = "$av0 - $server: could not connect: $!\n";
$emsg = $!;
unless (&mxlookup(0,$server,"$server: could not connect: $!",*users)) {
&giveup('mx',"$server: Could not connect: $emsg");
}
next HOST;
}
$S->autoflush(1);
# read the greeting
$0 = "$av0 - talking to $server";
&alarm("greeting with $server",'');
while(<$S>) {
alarm(0);
print if $watch;
if (/^(\d+)([- ])/) {
if ($1 != 220) {
$0 = "$av0 - bad numeric response from $server";
&alarm("giving up after bad response from $server",'');
&read_response($2,$watch);
alarm(0);
print STDERR "$server: NOT 220 greeting: $_"
if ($debug || $vw);
if (&mxlookup(0,$server,"$server: did not respond with a 220 greeting",*users)) {
close($S);
next HOST;
}
}
last if ($2 eq " ");
} else {
$0 = "$av0 - bad response from $server";
print STDERR "$server: NOT 220 greeting: $_"
if ($debug || $vw);
unless (&mxlookup(0,$server,"$server: did not respond with SMTP codes",*users)) {
&giveup('',"$server: did not talk SMTP");
}
close($S);
next HOST;
}
&alarm("greeting with $server",'');
}
alarm(0);
# if this causes problems, remove it
$0 = "$av0 - sending helo to $server";
&alarm("sending helo to $server","");
&ps("helo $hostname");
while(<$S>) {
print if $watch;
last if /^\d+ /;
}
alarm(0);
# try the users, one by one
USER:
while(@users) {
$u = shift(@users);
$0 = "$av0 - expanding $u [\@$server]";
# do we already have a name for this user?
$oldname = $names{"$u *** $server"};
print &compact($u,$server)." ->\n" if ($verbose && ! $valid);
if ($valid) {
#
# when running with -a, we delay taking any action
# on the results of our query until we have looked
# at the complete output. @toFinal stores expansions
# that will be final if we take them. @toExpn stores
# expnansions that are not final. @isValid keeps
# track of our ability to send mail to each of the
# expansions.
#
@isValid = ();
@toFinal = ();
@toExpn = ();
}
# ($ecode,@expansion) = &expn_vrfy($u,$server);
(@foo) = &expn_vrfy($u,$server);
($ecode,@expansion) = @foo;
if ($ecode) {
&giveup('',$ecode,$u);
last USER;
}
for $s (@expansion) {
$s =~ s/[\n\r]//g;
$0 = "$av0 - parsing $server: $s";
$skipwatch = $watch;
if ($s =~ /^[25]51([- ]).*<(.+)>/) {
print "$s" if $watch;
print "(pretending 250$1<$2>)" if ($debug && $watch);
print "\n" if $watch;
$s = "250$1<$2>";
$skipwatch = 0;
}
if ($s =~ /^250([- ])(.+)/) {
print "$s\n" if $skipwatch;
($done,$addr) = ($1,$2);
($newhost, $newaddr, $newname) = &parse($addr,$server,$oldname, $#expansion == 0);
print "($newhost, $newaddr, $newname) = &parse($addr, $server, $oldname)\n" if $debug;
if (! $newhost) {
# no expansion is possible w/o a new server to call
if ($valid) {
push(@isValid, &validAddr($newaddr));
push(@toFinal,$newaddr,$server,$newname);
} else {
&verbose(&final($newaddr,$server,$newname));
}
} else {
$newmxhost = &mx($newhost,$newaddr);
print "$newmxhost = &mx($newhost)\n"
if ($debug && $newhost ne $newmxhost);
$0 = "$av0 - parsing $newaddr [@$newmxhost]";
print "levels = $levels, level{$u *** $server} = ".$level{"$u *** $server"}."\n" if ($debug > 1);
# If the new server is the current one,
# it would have expanded things for us
# if it could have. Mx records must be
# followed to compare server names.
# We are also done if the recursion
# count has been exceeded.
if (&trhost($newmxhost) eq &trhost($server) || ($levels && $level{"$u *** $server"} >= $levels)) {
if ($valid) {
push(@isValid, &validAddr($newaddr));
push(@toFinal,$newaddr,$newmxhost,$newname);
} else {
&verbose(&final($newaddr,$newmxhost,$newname));
}
} else {
# more work to do...
if ($valid) {
push(@isValid, &validAddr($newaddr));
push(@toExpn,$newmxhost,$newaddr,$newname,$level{"$u *** $server"});
} else {
&verbose(&expn($newmxhost,$newaddr,$newname,$level{"$u *** $server"}));
}
}
}
last if ($done eq " ");
next;
}
# 550 is a known code... Should the be
# included in -a output? Might be a bug
# here. Does it matter? Can assume that
# there won't be UNKNOWN USER responses
# mixed with valid users?
if ($s =~ /^(550)([- ])/) {
if ($valid) {
print STDERR "\@$server:$u ($oldname) USER UNKNOWN\n";
} else {
&verbose(&final($u,$server,$oldname,"USER UNKNOWN"));
}
last if ($2 eq " ");
next;
}
# 553 is a known code...
if ($s =~ /^(553)([- ])/) {
if ($valid) {
print STDERR "\@$server:$u ($oldname) USER AMBIGUOUS\n";
} else {
&verbose(&final($u,$server,$oldname,"USER AMBIGUOUS"));
}
last if ($2 eq " ");
next;
}
# 252 is a known code...
if ($s =~ /^(252)([- ])/) {
if ($valid) {
print STDERR "\@$server:$u ($oldname) REFUSED TO VRFY\n";
} else {
&verbose(&final($u,$server,$oldname,"REFUSED TO VRFY"));
}
last if ($2 eq " ");
next;
}
&giveup('',"$server: did not grok '$s'",$u);
last USER;
}
if ($valid) {
#
# now we decide if we are going to take these
# expansions or roll them back.
#
$avgValid = &average(@isValid);
print "avgValid = $avgValid\n" if $debug;
if ($avgValid >= $validRequirement) {
print &compact($u,$server)." ->\n" if $verbose;
while (@toExpn) {
&verbose(&expn(splice(@toExpn,0,4)));
}
while (@toFinal) {
&verbose(&final(splice(@toFinal,0,3)));
}
} else {
print "Tossing some valid to avoid invalid ".&compact($u,$server)."\n" if ($avgValid > 0.0 && ($vw || $debug));
print &compact($u,$server)." ->\n" if $verbose;
&verbose(&final($u,$server,$newname));
}
}
}
&alarm("sending 'quit' to $server",'');
$0 = "$av0 - sending 'quit' to $server";
&ps("quit");
while(<$S>) {
print if $watch;
last if /^\d+ /;
}
close($S);
alarm(0);
}
$0 = "$av0 - printing final results";
print "----------\n" if $vw;
select(STDOUT);
for $f (sort @final) {
print "$f\n";
}
unlink("/tmp/expn$$");
exit(0);
# abandon all attempts deliver to $server
# register the current addresses as the final ones
sub giveup
{
local($redirect_okay,$reason,$user) = @_;
local($us,@so,$nh,@remaining_users);
local($pk,$file,$line);
($pk, $file, $line) = caller;
$0 = "$av0 - giving up on $server: $reason";
#
# add back a user if we gave up in the middle
#
push(@users,$user) if $user;
#
# don't bother with this system anymore
#
unless ($giveup{$server}) {
$giveup{$server} = $reason;
print STDERR "$reason\n";
}
print "Giveup at $file:$line!!! redirect okay = $redirect_okay; $reason\n" if $debug;
#
# Wait!
# Before giving up, see if there is a chance that
# there is another host to redirect to!
# (Kids, don't do this at home! Hacking is a dangerous
# crime and you could end up behind bars.)
#
for $u (@users) {
if ($redirect_okay =~ /\bmx\b/) {
next if &try_fallback('mx',$u,*server,
*mx_secondary,
*already_mx_fellback);
}
if ($redirect_okay =~ /\bdomainify\b/) {
next if &try_fallback('domainify',$u,*server,
*domainify_fallback,
*already_domainify_fellback);
}
push(@remaining_users,$u);
}
@users = @remaining_users;
for $u (@users) {
print &compact($u,$server)." ->\n" if ($verbose && $valid && $u);
&verbose(&final($u,$server,$names{"$u *** $server"},$reason));
}
}
#
# This routine is used only within &giveup. It checks to
# see if we really have to giveup or if there is a second
# chance because we did something before that can be
# backtracked.
#
# %fallback{"$user *** $host"} tracks what is able to fallback
# %fellback{"$user *** $host"} tracks what has fallen back
#
# If there is a valid backtrack, then queue up the new possibility
#
sub try_fallback
{
local($method,$user,*host,*fall_table,*fellback) = @_;
local($us,$fallhost,$oldhost,$ft,$i);
if ($debug > 8) {
print "Fallback table $method:\n";
for $i (sort keys %fall_table) {
print "\t'$i'\t\t'$fall_table{$i}'\n";
}
print "Fellback table $method:\n";
for $i (sort keys %fellback) {
print "\t'$i'\t\t'$fellback{$i}'\n";
}
print "U: $user H: $host\n";
}
$us = "$user *** $host";
if (defined $fellback{$us}) {
#
# Undo a previous fallback so that we can try again
# Nested fallbacks are avoided because they could
# lead to infinite loops
#
$fallhost = $fellback{$us};
print "Already $method fell back from $us -> \n" if $debug;
$us = "$user *** $fallhost";
$oldhost = $fallhost;
} elsif (($method eq 'mx') && (defined $mxbacktrace{$us}) && (defined $mx_secondary{$mxbacktrace{$us}})) {
print "Fallback an MX expansion $us -> \n" if $debug;
$oldhost = $mxbacktrace{$us};
} else {
print "Oldhost($host, $us) = " if $debug;
$oldhost = $host;
}
print "$oldhost\n" if $debug;
if (((defined $fall_table{$us}) && ($ft = $us)) || ((defined $fall_table{$oldhost}) && ($ft = $oldhost))) {
print "$method Fallback = ".$fall_table{$ft}."\n" if $debug;
local(@so,$newhost);
@so = split(' ',$fall_table{$ft});
$newhost = shift(@so);
print "Falling back ($method) $us -> $newhost (from $oldhost)\n" if $debug;
if ($method eq 'mx') {
if (! defined ($mxbacktrace{"$user *** $newhost"})) {
if (defined $mxbacktrace{"$user *** $oldhost"}) {
print "resetting oldhost $oldhost to the original: " if $debug;
$oldhost = $mxbacktrace{"$user *** $oldhost"};
print "$oldhost\n" if $debug;
}
$mxbacktrace{"$user *** $newhost"} = $oldhost;
print "mxbacktrace $user *** $newhost -> $oldhost\n" if $debug;
}
$mx{&trhost($oldhost)} = $newhost;
} else {
$temporary_redirect{$us} = $newhost;
}
if (@so) {
print "Can still $method $us: @so\n" if $debug;
$fall_table{$ft} = join(' ',@so);
} else {
print "No more fallbacks for $us\n" if $debug;
delete $fall_table{$ft};
}
if (defined $create_host_backtrack{$us}) {
$create_host_backtrack{"$user *** $newhost"}
= $create_host_backtrack{$us};
}
$fellback{"$user *** $newhost"} = $oldhost;
&expn($newhost,$user,$names{$us},$level{$us});
return 1;
}
delete $temporary_redirect{$us};
$host = $oldhost;
return 0;
}
# return 1 if you could send mail to the address as is.
sub validAddr
{
local($addr) = @_;
$res = &do_validAddr($addr);
print "validAddr($addr) = $res\n" if $debug;
$res;
}
sub do_validAddr
{
local($addr) = @_;
local($urx) = "[-A-Za-z_.0-9+]+";
# \u
return 0 if ($addr =~ /^\\/);
# ?@h
return 1 if ($addr =~ /.\@$urx$/);
# @h:?
return 1 if ($addr =~ /^\@$urx\:./);
# h!u
return 1 if ($addr =~ /^$urx!./);
# u
return 1 if ($addr =~ /^$urx$/);
# ?
print "validAddr($addr) = ???\n" if $debug;
return 0;
}
# Some systems use expn and vrfy interchangeably. Some only
# implement one or the other. Some check expn against mailing
# lists and vrfy against users. It doesn't appear to be
# consistent.
#
# So, what do we do? We try everything!
#
#
# Ranking of result codes: good: 250, 251/551, 252, 550, anything else
#
# Ranking of inputs: best: user@host.domain, okay: user
#
# Return value: $error_string, @responses_from_server
sub expn_vrfy
{
local($u,$server) = @_;
local(@c) = ('expn', 'vrfy');
local(@try_u) = $u;
local(@ret,$code);
if (($u =~ /(.+)@(.+)/) && (&trhost($2) eq &trhost($server))) {
push(@try_u,$1);
}
TRY:
for $c (@c) {
for $try_u (@try_u) {
&alarm("${c}'ing $try_u on $server",'',$u);
&ps("$c $try_u");
alarm(0);
$s = <$S>;
if ($s eq '') {
return "$server: lost connection";
}
if ($s !~ /^(\d+)([- ])/) {
return "$server: garbled reply to '$c $try_u'";
}
if ($1 == 250) {
$code = 250;
@ret = ("",$s);
push(@ret,&read_response($2,$debug));
return (@ret);
}
if ($1 == 551 || $1 == 251) {
$code = $1;
@ret = ("",$s);
push(@ret,&read_response($2,$debug));
next;
}
if ($1 == 252 && ($code == 0 || $code == 550)) {
$code = 252;
@ret = ("",$s);
push(@ret,&read_response($2,$watch));
next;
}
if ($1 == 550 && $code == 0) {
$code = 550;
@ret = ("",$s);
push(@ret,&read_response($2,$watch));
next;
}
&read_response($2,$watch);
}
}
return "$server: expn/vrfy not implemented" unless @ret;
return @ret;
}
# sometimes the old parse routine (now parse2) didn't
# reject funky addresses.
sub parse
{
local($oldaddr,$server,$oldname,$one_to_one) = @_;
local($newhost, $newaddr, $newname, $um) = &parse2($oldaddr,$server,$oldname,$one_to_one);
if ($newaddr =~ m,^["/],) {
return (undef, $oldaddr, $newname) if $valid;
return (undef, $um, $newname);
}
return ($newhost, $newaddr, $newname);
}
# returns ($new_smtp_server,$new_address,$new_name)
# given a response from a SMTP server ($newaddr), the
# current host ($server), the old "name" and a flag that
# indicates if it is being called during the initial
# command line parsing ($parsing_args)
sub parse2
{
local($newaddr,$context_host,$old_name,$parsing_args) = @_;
local(@names) = $old_name;
local($urx) = "[-A-Za-z_.0-9+]+";
local($unmangle);
#
# first, separate out the address part.
#
#
# [NAME] <ADDR [(NAME)]>
# [NAME] <[(NAME)] ADDR
# ADDR [(NAME)]
# (NAME) ADDR
# [(NAME)] <ADDR>
#
if ($newaddr =~ /^\<(.*)\>$/) {
print "<A:$1>\n" if $debug;
($newaddr) = &trim($1);
print "na = $newaddr\n" if $debug;
}
if ($newaddr =~ /^([^\<\>]*)\<([^\<\>]*)\>([^\<\>]*)$/) {
# address has a < > pair in it.
print "N:$1 <A:$2> N:$3\n" if $debug;
($newaddr) = &trim($2);
unshift(@names, &trim($3,$1));
print "na = $newaddr\n" if $debug;
}
if ($newaddr =~ /^([^\(\)]*)\(([^\(\)]*)\)([^\(\)]*)$/) {
# address has a ( ) pair in it.
print "A:$1 (N:$2) A:$3\n" if $debug;
unshift(@names,&trim($2));
local($f,$l) = (&trim($1),&trim($3));
if (($f && $l) || !($f || $l)) {
# address looks like:
# foo (bar) baz or (bar)
# not allowed!
print STDERR "Could not parse $newaddr\n" if $vw;
return(undef,$newaddr,&firstname(@names));
}
$newaddr = $f if $f;
$newaddr = $l if $l;
print "newaddr now = $newaddr\n" if $debug;
}
#
# @foo:bar
# j%k@l
# a@b
# b!a
# a
#
$unmangle = $newaddr;
if ($newaddr =~ /^\@($urx)\:(.+)$/) {
print "(\@:)" if $debug;
# this is a bit of a cheat, but it seems necessary
return (&domainify($1,$context_host,$2),$2,&firstname(@names),$unmangle);
}
if ($newaddr =~ /^(.+)\@($urx)$/) {
print "(\@)" if $debug;
return (&domainify($2,$context_host,$newaddr),$newaddr,&firstname(@names),$unmangle);
}
if ($parsing_args) {
if ($newaddr =~ /^($urx)\!(.+)$/) {
return (&domainify($1,$context_host,$newaddr),$newaddr,&firstname(@names),$unmangle);
}
if ($newaddr =~ /^($urx)$/) {
return ($context_host,$newaddr,&firstname(@names),$unmangle);
}
print STDERR "Could not parse $newaddr\n";
}
print "(?)" if $debug;
return(undef,$newaddr,&firstname(@names),$unmangle);
}
# return $u (@$server) unless $u includes reference to $server
sub compact
{
local($u, $server) = @_;
local($se) = $server;
local($sp);
$se =~ s/(\W)/\\$1/g;
$sp = " (\@$server)";
if ($u !~ /$se/i) {
return "$u$sp";
}
return $u;
}
# remove empty (spaces don't count) members from an array
sub trim
{
local(@v) = @_;
local($v,@r);
for $v (@v) {
$v =~ s/^\s+//;
$v =~ s/\s+$//;
push(@r,$v) if ($v =~ /\S/);
}
return(@r);
}
# using the host part of an address, and the server name, add the
# servers' domain to the address if it doesn't already have a
# domain. Since this sometimes fails, save a back reference so
# it can be unrolled.
sub domainify
{
local($host,$domain_host,$u) = @_;
local($domain,$newhost);
# cut of trailing dots
$host =~ s/\.$//;
$domain_host =~ s/\.$//;
if ($domain_host !~ /\./) {
#
# domain host isn't, keep $host whatever it is
#
print "domainify($host,$domain_host) = $host\n" if $debug;
return $host;
}
#
# There are several weird situtations that need to be
# accounted for. They have to do with domain relay hosts.
#
# Examples:
# host server "right answer"
#
# shiva.cs cs.berkeley.edu shiva.cs.berkeley.edu
# shiva cs.berkeley.edu shiva.cs.berekley.edu
# cumulus reed.edu @reed.edu:cumulus.uucp
# tiberius tc.cornell.edu tiberius.tc.cornell.edu
#
# The first try must always be to cut the domain part out of
# the server and tack it onto the host.
#
# A reasonable second try is to tack the whole server part onto
# the host and for each possible repeated element, eliminate
# just that part.
#
# These extra "guesses" get put into the %domainify_fallback
# array. They will be used to give addresses a second chance
# in the &giveup routine
#
local(%fallback);
local($long);
$long = "$host $domain_host";
$long =~ tr/A-Z/a-z/;
print "long = $long\n" if $debug;
if ($long =~ s/^([^ ]+\.)([^ ]+) \2(\.[^ ]+\.[^ ]+)/$1$2$3/) {
# matches shiva.cs cs.berkeley.edu and returns shiva.cs.berkeley.edu
print "condensed fallback $host $domain_host -> $long\n" if $debug;
$fallback{$long} = 9;
}
local($fh);
$fh = $domain_host;
while ($fh =~ /\./) {
print "FALLBACK $host.$fh = 1\n" if $debug > 7;
$fallback{"$host.$fh"} = 1;
$fh =~ s/^[^\.]+\.//;
}
$fallback{"$host.$domain_host"} = 2;
($domain = $domain_host) =~ s/^[^\.]+//;
$fallback{"$host$domain"} = 6
if ($domain =~ /\./);
if ($host =~ /\./) {
#
# Host is already okay, but let's look for multiple
# interpretations
#
print "domainify($host,$domain_host) = $host\n" if $debug;
delete $fallback{$host};
$domainify_fallback{"$u *** $host"} = join(' ',sort {$fallback{$b} <=> $fallback{$a};} keys %fallback) if %fallback;
return $host;
}
$domain = ".$domain_host"
if ($domain !~ /\..*\./);
$newhost = "$host$domain";
$create_host_backtrack{"$u *** $newhost"} = $domain_host;
print "domainify($host,$domain_host) = $newhost\n" if $debug;
delete $fallback{$newhost};
$domainify_fallback{"$u *** $newhost"} = join(' ',sort {$fallback{$b} <=> $fallback{$a};} keys %fallback) if %fallback;
if ($debug) {
print "fallback = ";
print $domainify_fallback{"$u *** $newhost"}
if defined($domainify_fallback{"$u *** $newhost"});
print "\n";
}
return $newhost;
}
# return the first non-empty element of an array
sub firstname
{
local(@names) = @_;
local($n);
while(@names) {
$n = shift(@names);
return $n if $n =~ /\S/;
}
return undef;
}
# queue up more addresses to expand
sub expn
{
local($host,$addr,$name,$level) = @_;
if ($host) {
$host = &trhost($host);
if (($debug > 3) || (defined $giveup{$host})) {
unshift(@hosts,$host) unless $users{$host};
} else {
push(@hosts,$host) unless $users{$host};
}
$users{$host} .= " $addr";
$names{"$addr *** $host"} = $name;
$level{"$addr *** $host"} = $level + 1;
print "expn($host,$addr,$name)\n" if $debug;
return "\t$addr\n";
} else {
return &final($addr,'NONE',$name);
}
}
# compute the numerical average value of an array
sub average
{
local(@e) = @_;
return 0 unless @e;
local($e,$sum);
for $e (@e) {
$sum += $e;
}
$sum / @e;
}
# print to the server (also to stdout, if -w)
sub ps
{
local($p) = @_;
print ">>> $p\n" if $watch;
print $S "$p\n";
}
# return case-adjusted name for a host (for comparison purposes)
sub trhost
{
# treat foo.bar as an alias for Foo.BAR
local($host) = @_;
local($trhost) = $host;
$trhost =~ tr/A-Z/a-z/;
if ($trhost{$trhost}) {
$host = $trhost{$trhost};
} else {
$trhost{$trhost} = $host;
}
$trhost{$trhost};
}
# re-queue users if an mx record dictates a redirect
# don't allow a user to be redirected more than once
sub mxredirect
{
local($server,*users) = @_;
local($u,$nserver,@still_there);
$nserver = &mx($server);
if (&trhost($nserver) ne &trhost($server)) {
$0 = "$av0 - mx redirect $server -> $nserver\n";
for $u (@users) {
if (defined $mxbacktrace{"$u *** $nserver"}) {
push(@still_there,$u);
} else {
$mxbacktrace{"$u *** $nserver"} = $server;
print "mxbacktrace{$u *** $nserver} = $server\n"
if ($debug > 1);
&expn($nserver,$u,$names{"$u *** $server"});
}
}
@users = @still_there;
if (! @users) {
return $nserver;
} else {
return undef;
}
}
return undef;
}
# follow mx records, return a hostname
# also follow temporary redirections comming from &domainify and
# &mxlookup
sub mx
{
local($h,$u) = @_;
for (;;) {
if (defined $mx{&trhost($h)} && $h ne $mx{&trhost($h)}) {
$0 = "$av0 - mx expand $h";
$h = $mx{&trhost($h)};
return $h;
}
if ($u) {
if (defined $temporary_redirect{"$u *** $h"}) {
$0 = "$av0 - internal redirect $h";
print "Temporary redirect taken $u *** $h -> " if $debug;
$h = $temporary_redirect{"$u *** $h"};
print "$h\n" if $debug;
next;
}
$htr = &trhost($h);
if (defined $temporary_redirect{"$u *** $htr"}) {
$0 = "$av0 - internal redirect $h";
print "temporary redirect taken $u *** $h -> " if $debug;
$h = $temporary_redirect{"$u *** $htr"};
print "$h\n" if $debug;
next;
}
}
return $h;
}
}
# look up mx records with the name server.
# re-queue expansion requests if possible
# optionally give up on this host.
sub mxlookup
{
local($lastchance,$server,$giveup,*users) = @_;
local(*T);
local(*NSLOOKUP);
local($nh, $pref,$cpref);
local($o0) = $0;
local($nserver);
local($name,$aliases,$type,$len,$thataddr);
local(%fallback);
return 1 if &mxredirect($server,*users);
if ((defined $mx{$server}) || (! $have_nslookup)) {
return 0 unless $lastchance;
&giveup('mx domainify',$giveup);
return 0;
}
$0 = "$av0 - nslookup of $server";
sysopen(T,"/tmp/expn$$",O_RDWR|O_CREAT|O_EXCL,0600) || die "open > /tmp/expn$$: $!\n";
print T "set querytype=MX\n";
print T "$server\n";
close(T);
$cpref = 1.0E12;
undef $nserver;
open(NSLOOKUP,"nslookup < /tmp/expn$$ 2>&1 |") || die "open nslookup: $!";
while(<NSLOOKUP>) {
print if ($debug > 2);
if (/mail exchanger = ([-A-Za-z_.0-9+]+)/) {
$nh = $1;
if (/preference = (\d+)/) {
$pref = $1;
if ($pref < $cpref) {
$nserver = $nh;
$cpref = $pref;
} elsif ($pref) {
$fallback{$pref} .= " $nh";
}
}
}
if (/Non-existent domain/) {
#
# These addresss are hosed. Kaput! Dead!
# However, if we created the address in the
# first place then there is a chance of
# salvation.
#
1 while(<NSLOOKUP>);
close(NSLOOKUP);
return 0 unless $lastchance;
&giveup('domainify',"$server: Non-existent domain",undef,1);
return 0;
}
}
close(NSLOOKUP);
unlink("/tmp/expn$$");
unless ($nserver) {
$0 = "$o0 - finished mxlookup";
return 0 unless $lastchance;
&giveup('mx domainify',"$server: Could not resolve address");
return 0;
}
# provide fallbacks in case $nserver doesn't work out
if (defined $fallback{$cpref}) {
$mx_secondary{$server} = $fallback{$cpref};
}
$0 = "$av0 - gethostbyname($nserver)";
($name,$aliases,$type,$len,$thataddr) = gethostbyname($nserver);
unless ($thataddr) {
$0 = $o0;
return 0 unless $lastchance;
&giveup('mx domainify',"$nserver: could not resolve address");
return 0;
}
print "MX($server) = $nserver\n" if $debug;
print "$server -> $nserver\n" if $vw && !$debug;
$mx{&trhost($server)} = $nserver;
# redeploy the users
unless (&mxredirect($server,*users)) {
return 0 unless $lastchance;
&giveup('mx domainify',"$nserver: only one level of mx redirect allowed");
return 0;
}
$0 = "$o0 - finished mxlookup";
return 1;
}
# if mx expansion did not help to resolve an address
# (ie: foo@bar became @baz:foo@bar, then undo the
# expansion).
# this is only used by &final
sub mxunroll
{
local(*host,*addr) = @_;
local($r) = 0;
print "looking for mxbacktrace{$addr *** $host}\n"
if ($debug > 1);
while (defined $mxbacktrace{"$addr *** $host"}) {
print "Unrolling MX expnasion: \@$host:$addr -> "
if ($debug || $verbose);
$host = $mxbacktrace{"$addr *** $host"};
print "\@$host:$addr\n"
if ($debug || $verbose);
$r = 1;
}
return 1 if $r;
$addr = "\@$host:$addr"
if ($host =~ /\./);
return 0;
}
# register a completed expnasion. Make the final address as
# simple as possible.
sub final
{
local($addr,$host,$name,$error) = @_;
local($he);
local($hb,$hr);
local($au,$ah);
if ($error =~ /Non-existent domain/) {
#
# If we created the domain, then let's undo the
# damage...
#
if (defined $create_host_backtrack{"$addr *** $host"}) {
while (defined $create_host_backtrack{"$addr *** $host"}) {
print "Un&domainifying($host) = " if $debug;
$host = $create_host_backtrack{"$addr *** $host"};
print "$host\n" if $debug;
}
$error = "$host: could not locate";
} else {
#
# If we only want valid addresses, toss out
# bad host names.
#
if ($valid) {
print STDERR "\@$host:$addr ($name) Non-existent domain\n";
return "";
}
}
}
MXUNWIND: {
$0 = "$av0 - final parsing of \@$host:$addr";
($he = $host) =~ s/(\W)/\\$1/g;
if ($addr !~ /@/) {
# addr does not contain any host
$addr = "$addr@$host";
} elsif ($addr !~ /$he/i) {
# if host part really something else, use the something
# else.
if ($addr =~ m/(.*)\@([^\@]+)$/) {
($au,$ah) = ($1,$2);
print "au = $au ah = $ah\n" if $debug;
if (defined $temporary_redirect{"$addr *** $ah"}) {
$addr = "$au\@".$temporary_redirect{"$addr *** $ah"};
print "Rewrite! to $addr\n" if $debug;
next MXUNWIND;
}
}
# addr does not contain full host
if ($valid) {
if ($host =~ /^([^\.]+)(\..+)$/) {
# host part has a . in it - foo.bar
($hb, $hr) = ($1, $2);
if ($addr =~ /\@([^\.\@]+)$/ && ($1 eq $hb)) {
# addr part has not .
# and matches beginning of
# host part -- tack on a
# domain name.
$addr .= $hr;
} else {
&mxunroll(*host,*addr)
&& redo MXUNWIND;
}
} else {
&mxunroll(*host,*addr)
&& redo MXUNWIND;
}
} else {
$addr = "${addr}[\@$host]"
if ($host =~ /\./);
}
}
}
$name = "$name " if $name;
$error = " $error" if $error;
if ($valid) {
push(@final,"$name<$addr>");
} else {
push(@final,"$name<$addr>$error");
}
"\t$name<$addr>$error\n";
}
sub alarm
{
local($alarm_action,$alarm_redirect,$alarm_user) = @_;
alarm(3600);
$SIG{ALRM} = 'handle_alarm';
}
# this involves one great big ugly hack.
# the "next HOST" unwinds the stack!
sub handle_alarm
{
&giveup($alarm_redirect,"Timed out during $alarm_action",$alarm_user);
next HOST;
}
# read the rest of the current smtp daemon's response (and toss it away)
sub read_response
{
local($done,$watch) = @_;
local(@resp);
print $s if $watch;
while(($done eq "-") && ($s = <$S>) && ($s =~ /^\d+([- ])/)) {
print $s if $watch;
$done = $1;
push(@resp,$s);
}
return @resp;
}
# print args if verbose. Return them in any case
sub verbose
{
local(@tp) = @_;
print "@tp" if $verbose;
}
# to pass perl -w:
@tp;
$flag_a;
$flag_d;
$flag_1;
%already_domainify_fellback;
%already_mx_fellback;
&handle_alarm;
################### BEGIN PERL/TROFF TRANSITION
.00 ;
'di
.nr nl 0-1
.nr % 0
.\\"'; __END__
.\" ############## END PERL/TROFF TRANSITION
.TH EXPN 1 "March 11, 1993"
.AT 3
.SH NAME
expn \- recursively expand mail aliases
.SH SYNOPSIS
.B expn
.RI [ -a ]
.RI [ -v ]
.RI [ -w ]
.RI [ -d ]
.RI [ -1 ]
.IR user [@ hostname ]
.RI [ user [@ hostname ]]...
.SH DESCRIPTION
.B expn
will use the SMTP
.B expn
and
.B vrfy
commands to expand mail aliases.
It will first look up the addresses you provide on the command line.
If those expand into addresses on other systems, it will
connect to the other systems and expand again. It will keep
doing this until no further expansion is possible.
.SH OPTIONS
The default output of
.B expn
can contain many lines which are not valid
email addresses. With the
.I -aa
flag, only expansions that result in legal addresses
are used. Since many mailing lists have an illegal
address or two, the single
.IR -a ,
address, flag specifies that a few illegal addresses can
be mixed into the results. More
.I -a
flags vary the ratio. Read the source to track down
the formula. With the
.I -a
option, you should be able to construct a new mailing
list out of an existing one.
.LP
If you wish to limit the number of levels deep that
.B expn
will recurse as it traces addresses, use the
.I -1
option. For each
.I -1
another level will be traversed. So,
.I -111
will traverse no more than three levels deep.
.LP
The normal mode of operation for
.B expn
is to do all of its work silently.
The following options make it more verbose.
It is not necessary to make it verbose to see what it is
doing because as it works, it changes its
.BR argv [0]
variable to reflect its current activity.
To see how it is expanding things, the
.IR -v ,
verbose, flag will cause
.B expn
to show each address before
and after translation as it works.
The
.IR -w ,
watch, flag will cause
.B expn
to show you its conversations with the mail daemons.
Finally, the
.IR -d ,
debug, flag will expose many of the inner workings so that
it is possible to eliminate bugs.
.SH ENVIRONMENT
No environment variables are used.
.SH FILES
.PD 0
.B /tmp/expn$$
.B temporary file used as input to
.BR nslookup .
.SH SEE ALSO
.BR aliases (5),
.BR sendmail (8),
.BR nslookup (8),
RFC 823, and RFC 1123.
.SH BUGS
Not all mail daemons will implement
.B expn
or
.BR vrfy .
It is not possible to verify addresses that are served
by such daemons.
.LP
When attempting to connect to a system to verify an address,
.B expn
only tries one IP address. Most mail daemons
will try harder.
.LP
It is assumed that you are running domain names and that
the
.BR nslookup (8)
program is available. If not,
.B expn
will not be able to verify many addresses. It will also pause
for a long time unless you change the code where it says
.I $have_nslookup = 1
to read
.I $have_nslookup =
.IR 0 .
.LP
Lastly,
.B expn
does not handle every valid address. If you have an example,
please submit a bug report.
.SH CREDITS
In 1986 or so, Jon Broome wrote a program of the same name
that did about the same thing. It has since suffered bit rot
and Jon Broome has dropped off the face of the earth!
(Jon, if you are out there, drop me a line)
.SH AVAILABILITY
The latest version of
.B expn
is available through anonymous ftp at
.IR ftp://ftp.idiom.com/pub/muir-programs/expn .
.SH AUTHOR
.I David Muir Sharnoff\ \ \ \ <muir@idiom.com>
|