summaryrefslogtreecommitdiff
path: root/usr.sbin/sendmail/contrib/etrn.pl
blob: 1058ef4b67a78b752ef019961735f962181a1ac1 (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
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
#!/usr/bin/perl
'di ';
'ds 00 \\"';
'ig 00 ';
#
#       THIS PROGRAM IS ITS OWN MANUAL PAGE.  INSTALL IN man & bin.
#

# hardcoded constants, should work fine for BSD-based systems
use Socket;
use Getopt::Std;
$sockaddr = 'S n a4 x8';

# system requirements:
# 	must have 'hostname' program.

#############################################################################
#  Copyright (c) 1996 John T. Beck <john@beck.org>
#  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 John T. Beck.
#  4. The name of John Beck may not be used to endorse or promote products
#     derived from this software without specific prior written permission.
#
#  THIS SOFTWARE IS PROVIDED BY JOHN T. BECK ``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 JOHN T. BECK 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 derived from material copyrighted by the Regents
#  of the University of California.
#
#  Contributions accepted.
#############################################################################
#  Further disclaimer: the etrn.pl script was highly leveraged from the
#  expn.pl script which is (C) 1993 David Muir Sharnoff.
#############################################################################

$port = 'smtp';
$av0 = $0;
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 [-wd] host [args]";
getopts('dw');
$watch = $opt_w;
$debug = $opt_d;
$server = shift(@ARGV);
@hosts = @ARGV;
die $usage unless $server;

if (!@hosts) {
	push(@hosts,$hostname);

	$0 = "$av0 - parsing sendmail.cf";
	open(CF, "</etc/sendmail.cf") || die "open /etc/sendmail.cf: $!";
	while (<CF>){
		if (/^Fw.*$/){			# look for a line starting with "Fw"
			$cwfile = $_;
			chop($cwfile);
			$optional = /^Fw-o/;
			$cwfile =~ s,^Fw[^/]*,,;	# extract the file name
		}
		if (/^Cw(.*)$/){		# look for a line starting with "Cw"
			@cws = split (' ', $1);
			while (@cws) {
				$thishost = shift(@cws);
				push(@hosts, $thishost) unless $thishost =~ "$hostname|localhost";
			}
		}
	}
	close(CF);

	if ($cwfile){
		$0 = "$av0 - reading $cwfile";
		if (open(CW, "<$cwfile")){
			while (<CW>){
				$thishost = $_;
				chop($thishost);
				push(@hosts, $thishost) unless $thishost =~ $hostname;
			}
			close(CW);
		} else {
			die "open $cwfile: $!" unless $optional;
		}
	}
}

$0 = "$av0 - building local socket";
($name,$aliases,$proto) = getprotobyname('tcp');
($name,$aliases,$port) = getservbyname($port,'tcp')
	unless $port =~ /^\d+/;

# look it up
$0 = "$av0 - gethostbyname($server)";

($name,$aliases,$type,$len,$thataddr) = gethostbyname($server);
				
# get a connection
$0 = "$av0 - socket to $server";
$that = pack($sockaddr, &AF_INET, $port, $thataddr);
socket(S, &AF_INET, &SOCK_STREAM, $proto)
	|| die "socket: $!";
$0 = "$av0 - connect to $server";
print "debug = $debug server = $server\n" if $debug > 8;
if (! connect(S, $that)) {
	$0 = "$av0 - $server: could not connect: $!\n";
}
select((select(S),$| = 1)[0]); # don't buffer output to S

# 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 || $watch);
		}
		last if ($2 eq " ");
	} else {
		$0 = "$av0 - bad response from $server";
		print STDERR "$server: NOT 220 greeting: $_"
			if ($debug || $watch);
		close(S);
	}
	&alarm("greeting with $server",'');
}
alarm(0);
	
# if this causes problems, remove it
$0 = "$av0 - sending helo to $server";
&alarm("sending ehlo to $server","");
&ps("ehlo $hostname");
$etrn_support = 0;
while(<S>) {
	if (/^250([- ])ETRN(.+)$/){
		$etrn_support = 1;
	}
	print if $watch;
	last if /^\d+ /;
}
alarm(0);

if ($etrn_support){
	print "ETRN supported\n" if ($debug);
	&alarm("sending etrn to $server",'');
	while (@hosts) {
		$server = shift(@hosts);
		&ps("etrn $server");
		while(<S>) {
			print if $watch;
			last if /^\d+ /;
		}
		sleep(1);
	}
} else {
	print "\nETRN not supported\n\n"
}

&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);

select(STDOUT);
exit(0);

# print to the server (also to stdout, if -w)
sub ps
{
	local($p) = @_;
	print ">>> $p\n" if $watch;
	print S "$p\n";
}

sub alarm
{
	local($alarm_action,$alarm_redirect,$alarm_user) = @_;
	alarm(3600);
	$SIG{ALRM} = 'handle_alarm';
}

sub handle_alarm
{
	&giveup($alarm_redirect,"Timed out during $alarm_action",$alarm_user);
}

# 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;
}
# to pass perl -w:
@tp;
$flag_a;
$flag_d;
&handle_alarm;
################### BEGIN PERL/TROFF TRANSITION 
.00 ;	

'di
.nr nl 0-1
.nr % 0
.\\"'; __END__ 
.\" ############## END PERL/TROFF TRANSITION
.TH ETRN 1 "January 25, 1997"
.AT 3
.SH NAME
etrn \- start mail queue run
.SH SYNOPSIS
.B etrn
.RI [ -w ]
.RI [ -d ]
.IR hostname
.RI [ args ]
.SH DESCRIPTION
.B etrn
will use the SMTP
.B etrn
command to start mail delivery from the host given on the command line.
.B etrn
usually sends an
.B etrn
for each host the local sendmail accepts e-mail for, but if
.IR args
are specified,
.B etrn
uses these as arguments for the SMTP
.B etrn
commands passed to the host given on the command line.
.SH OPTIONS
.LP
The normal mode of operation for
.B etrn
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.
The 
.IR -w ,
watch, flag will cause
.B etrn
to show you its conversations with the mail daemons.
The 
.IR -d ,
debug, flag will expose many of the inner workings so that
it is possible to eliminate bugs.
.SH ENVIRONMENT
No enviroment variables are used.
.SH FILES
.B /etc/sendmail.cf
.SH SEE ALSO
.BR sendmail (8),
RFC 1985.
.SH BUGS
Not all mail daemons will implement 
.B etrn .
.LP
It is assumed that you are running domain names.
.SH CREDITS
Leveraged from David Muir Sharnoff's expn.pl script.
Christian von Roques added support for
.IR args
and fixed a couple of bugs.
.SH AVAILABILITY
The latest version of 
.B etrn
is available in the contrib directory of the sendmail
distribution through anonymous ftp at
.IR ftp://ftp.sendmail.org/ucb/src/sendmail/ .
.SH AUTHOR
.I John T. Beck\ \ \ \ <john@beck.org>