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