Commit | Line | Data |
---|---|---|
059ec3d9 | 1 | #! /bin/sh |
059ec3d9 | 2 | |
0a49a7a4 | 3 | # Copyright (c) University of Cambridge, 1995 - 2007 |
059ec3d9 PH |
4 | # See the file NOTICE for conditions of use and distribution. |
5 | ||
6 | # Except when they appear in comments, the following placeholders in this | |
7 | # source are replaced when it is turned into a runnable script: | |
8 | # | |
9 | # CONFIGURE_FILE_USE_NODE | |
10 | # CONFIGURE_FILE | |
11 | # BIN_DIRECTORY | |
12 | ||
13 | # PROCESSED_FLAG | |
14 | ||
15 | # A shell+perl script to fish out the next retry time for a given domain; | |
16 | # it first calls exim to find out which hosts are set up for that domain and | |
17 | # then fishes out the retry data for each one. | |
18 | ||
19 | # For testing the selection and formatting logic, and perhaps for use in | |
20 | # special cases, the script can have an argument -C <filename> to specify | |
21 | # the use of an alternate Exim configuration file. It may also have any number | |
22 | # of -D options to set macros that are passed to exim. | |
23 | ||
24 | config= | |
25 | eximmacdef= | |
26 | exim_path= | |
27 | ||
17792b53 NM |
28 | if expr -- $1 : '\-' >/dev/null ; then |
29 | while expr -- $1 : '\-' >/dev/null ; do | |
059ec3d9 PH |
30 | if [ "$1" = "-C" ]; then |
31 | config=$2 | |
32 | shift | |
33 | shift | |
17792b53 | 34 | elif expr -- $1 : '\-D' >/dev/null ; then |
059ec3d9 | 35 | eximmacdef="$eximmacdef $1" |
17792b53 NM |
36 | if expr -- $1 : '\-DEXIM_PATH=' >/dev/null ; then |
37 | exim_path=`expr -- $1 : '\-DEXIM_PATH=\(.*\)'` | |
059ec3d9 PH |
38 | fi |
39 | shift | |
40 | else | |
41 | break | |
42 | fi | |
43 | done | |
44 | fi | |
45 | ||
46 | # We need to save the script's argument because in the absence of -C we need to | |
47 | # use shell arguments for sorting out the configuration file name. | |
48 | ||
49 | argone=$1 | |
50 | ||
51 | # This is the normal case when no config file or macros are specified | |
52 | ||
53 | if [ "$config" = "" ]; then | |
54 | # See if this installation is using the esoteric "USE_NODE" feature of Exim, | |
55 | # in which it uses the host's name as a suffix for the configuration file name. | |
56 | ||
57 | if [ "CONFIGURE_FILE_USE_NODE" = "yes" ]; then | |
58 | hostsuffix=.`uname -n` | |
59 | fi | |
60 | ||
61 | # Now find the configuration file name. This has got complicated because | |
62 | # CONFIGURE_FILE may now be a list of files. The one that is used is the first | |
63 | # one that exists. Mimic the code in readconf.c by testing first for the | |
64 | # suffixed file in each case. | |
65 | ||
66 | set `awk -F: '{ for (i = 1; i <= NF; i++) print $i }' <<End | |
8825241f MH |
67 | CONFIGURE_FILE |
68 | End | |
69 | ` | |
059ec3d9 PH |
70 | while [ "$config" = "" -a $# -gt 0 ] ; do |
71 | if [ -f "$1$hostsuffix" ] ; then | |
72 | config="$1$hostsuffix" | |
73 | elif [ -f "$1" ] ; then | |
74 | config="$1" | |
75 | fi | |
76 | shift | |
77 | done | |
78 | fi | |
79 | ||
80 | # Determine where the spool directory is. Search for an exim_path setting | |
81 | # in the configure file; otherwise use the bin directory. Call that version of | |
82 | # Exim to find the spool directory and the qualify domain. BEWARE: a tab | |
83 | # character is needed in the command below. It has had a nasty tendency to get | |
84 | # lost in the past. Use a variable to hold a space and a tab to keep the tab in | |
85 | # one place. | |
86 | ||
87 | st=' ' | |
88 | ||
89 | if [ "$exim_path" = "" ]; then | |
90 | exim_path=`grep "^[$st]*exim_path" $config | sed "s/.*=[$st]*//"` | |
91 | fi | |
92 | ||
93 | if test "$exim_path" = ""; then exim_path=BIN_DIRECTORY/exim; fi | |
94 | spool_directory=`$exim_path $eximmacdef -C $config -bP spool_directory | sed 's/.*=[ ]*//'` | |
95 | qualify_domain=`$exim_path $eximmacdef -C $config -bP qualify_domain | sed 's/.*=[ ]*//'` | |
96 | ||
97 | # Now do the job. Perl uses $ so frequently that we don't want to have to | |
98 | # escape them all from the shell, so pass in shell variable values as | |
99 | # arguments. | |
100 | ||
101 | # 16-May-1996 Fixed it to do better if routing fails to complete. | |
102 | # Improved the format of the output. | |
103 | # 10-Jun-1996 Complain if no argument given. | |
104 | # 02-Aug-1996 Lower case the domain. | |
105 | # 14-Jan-1999 Add subject to want list even if remote host found, so as to | |
106 | # pick up routing delays after temporary recipient errors. | |
107 | # Also add unqualified subject if it looks like a message id. | |
108 | # 01-Apr-2004 Add the -C feature for testing | |
7a100415 | 109 | # 22-Dec-2005 Complete the -C feature (!) |
059ec3d9 PH |
110 | |
111 | if [ "$argone" = "" ]; then | |
112 | echo "Usage: exinext <address>|<domain>|<local-part>" | |
113 | exit 1 | |
114 | fi | |
115 | ||
7a100415 | 116 | perl - $exim_path "$eximmacdef" $argone $spool_directory $qualify_domain $config <<'End' |
059ec3d9 PH |
117 | |
118 | # Name the arguments | |
119 | ||
120 | $exim = $ARGV[0]; | |
121 | $eximmacdef = $ARGV[1]; | |
122 | $subject = $ARGV[2]; | |
123 | $spool = $ARGV[3]; | |
124 | $qualify = $ARGV[4]; | |
7a100415 | 125 | $config = $ARGV[5]; |
059ec3d9 PH |
126 | |
127 | # If the subject doesn't contain an @ then construct an address | |
128 | # for the domain, and ensure that in both cases the domain is | |
129 | # lower cased. | |
130 | ||
131 | $address = ($subject =~ /^([^\@]*)\@([^\@]*)$/)? | |
132 | "$1\@\L$2\E" : "User\@\L$subject\E"; | |
133 | ||
134 | # Run Exim to get a list of hosts for the given domain; for | |
135 | # each one construct the appropriate retry key. | |
136 | ||
7a100415 | 137 | open(LIST, "$exim -C $config -v -bt $address |") || |
059ec3d9 PH |
138 | die "can't run exim to route $address"; |
139 | ||
140 | while (<LIST>) | |
141 | { | |
142 | chop; | |
143 | push(@list, $_) if s/\s*host (\S+)\s+\[(.+)\].*/$1:$2/; | |
144 | print "$_\n" if /cannot be resolved/; | |
145 | } | |
146 | close(LIST); | |
147 | ||
148 | # If there were no hosts, assume that what was given was a local | |
149 | # username, unless it contains an @, and construct a suitable retry | |
150 | # key for that. Also, if it looks like a message id, search for that | |
151 | # as well, so as to pick up message-specific retry data. | |
152 | ||
153 | if (scalar(@list) == 0) | |
154 | { | |
155 | push(@list, $subject) if $subject =~ /^\w{6}-\w{6}-\w{2}$/; | |
156 | ||
157 | if ($subject !~ /\@/ && $subject !~ /\./) | |
158 | { | |
159 | push(@list, "$subject\@$qualify"); | |
160 | } | |
161 | else | |
162 | { | |
163 | print "No remote hosts found for $subject\n"; | |
164 | } | |
165 | } | |
166 | ||
167 | # Always search for the full address, even if hosts are found, in case | |
168 | # there is a routing delay caused by a temporary recipient error. | |
169 | ||
170 | push(@list, $subject); | |
171 | ||
172 | # Run exim_dumpdb to get out the retry data and pick off what we want | |
173 | ||
174 | open(DATA, "${exim}_dumpdb $spool retry |") || | |
175 | die "can't run exim_dumpdb"; | |
176 | ||
177 | while (<DATA>) | |
178 | { | |
179 | for ($i = 0; $i <= $#list; $i++) | |
180 | { | |
181 | if (/$list[$i]/) | |
182 | { | |
183 | $printed = 1; | |
184 | if (/^\s*T:[^:\s]*:/) | |
185 | { | |
186 | ($key,$error,$error2,$text) = /^\s*T:(\S+)\s+(\S+)\s+(\S+)\s*(.*)$/; | |
187 | ||
188 | # Parsing the keys is a nightmare because of IPv6. The design of the | |
189 | # format for the keys is a complete shambles. All my fault (PH). But | |
190 | # I don't want to change it just for this purpose. If they key | |
191 | # contains more than 3 colons, we have an IPv6 address, because | |
192 | # an IPv6 address must contain at least two colons. | |
193 | ||
194 | # Deal with IPv4 addresses (3 colons or fewer) | |
195 | ||
196 | if ($key !~ /:([^:]*?:){3}/) | |
197 | { | |
198 | ($host,$ip,$port,$msgid) = $key =~ | |
199 | /^([^:]*):([^:]*)(?::([^:]*)(?::(\S*)|)|)/; | |
200 | } | |
201 | ||
202 | # Deal with IPv6 addresses; sorting out the colons is a complete | |
203 | # mess. We should be able to find the host name and IP address from | |
204 | # further in the message. That seems the easiest escape plan here. We | |
205 | # can use those to match the rest of the key. | |
206 | ||
207 | else | |
208 | { | |
209 | ($host,$ip) = $text =~ /host\s(\S+)\s\[([^]]+)\]/; | |
210 | if (defined $host) | |
211 | { | |
212 | ($port,$msgid) = $key =~ | |
213 | /^$host:$ip(?::([^:]*)(?::(\S*)|)|)/; | |
214 | } | |
215 | ||
216 | # This will probably be wrong... | |
217 | ||
218 | else | |
219 | { | |
220 | ($host,$ip) = $key =~ /([^:]*):(.*)/; | |
221 | } | |
222 | } | |
223 | ||
224 | printf("Transport: %s [%s]", $host, $ip); | |
225 | print ":$port" if defined $port; | |
226 | print " $msgid" if defined $msgid; | |
227 | print " error $error: $text\n"; | |
228 | } | |
229 | ||
230 | else | |
231 | { | |
232 | ($type,$domain,$error,$error2,$text) = | |
233 | /^\s*(\S):(\S+)\s+(\S+)\s+(\S+)\s*(.*)$/; | |
234 | $type = ($type eq 'R')? "Route: " : | |
235 | ($type eq 'T')? "Transport: " : ""; | |
236 | print "$type$domain error $error: $text\n"; | |
237 | } | |
238 | $_ = <DATA>; | |
239 | ($first,$last,$next,$expired) = | |
240 | /^(\S+\s+\S+)\s+(\S+\s+\S+)\s+(\S+\s+\S+)\s*(\*?)/; | |
241 | print " first failed: $first\n"; | |
242 | print " last tried: $last\n"; | |
243 | print " next try at: $next\n"; | |
244 | print " past final cutoff time\n" if $expired eq "*"; | |
245 | } | |
246 | } | |
247 | } | |
248 | ||
249 | close(DATA); | |
250 | print "No retry data found for $subject\n" if !$printed; | |
251 | End | |
252 |