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